Add tests for icon scaling math & loading

This commit is contained in:
Jonas Berlin 2019-12-17 22:16:29 +02:00
parent 03253e82f7
commit ad5d20bd6a
5 changed files with 202 additions and 22 deletions

View File

@ -6,6 +6,7 @@
#include <gdk-pixbuf/gdk-pixbuf.h> #include <gdk-pixbuf/gdk-pixbuf.h>
#include <gio/gio.h> #include <gio/gio.h>
#include "helpers.h"
#include "queues.h" #include "queues.h"
extern const char *base; extern const char *base;
@ -252,33 +253,13 @@ bool dbus_notification_fire(struct dbus_notification *n, uint *id)
void dbus_notification_set_raw_image(struct dbus_notification *n_dbus, const char *path) void dbus_notification_set_raw_image(struct dbus_notification *n_dbus, const char *path)
{ {
GdkPixbuf *pb = gdk_pixbuf_new_from_file(path, NULL); GVariant *hint = notification_setup_raw_image(path);
if (!hint)
if (!pb)
return; return;
GVariant *hint_data = g_variant_new_from_data(
G_VARIANT_TYPE("ay"),
gdk_pixbuf_read_pixels(pb),
gdk_pixbuf_get_byte_length(pb),
TRUE,
(GDestroyNotify) g_object_unref,
g_object_ref(pb));
GVariant *hint = g_variant_new(
"(iiibii@ay)",
gdk_pixbuf_get_width(pb),
gdk_pixbuf_get_height(pb),
gdk_pixbuf_get_rowstride(pb),
gdk_pixbuf_get_has_alpha(pb),
gdk_pixbuf_get_bits_per_sample(pb),
gdk_pixbuf_get_n_channels(pb),
hint_data);
g_hash_table_insert(n_dbus->hints, g_hash_table_insert(n_dbus->hints,
g_strdup("image-data"), g_strdup("image-data"),
g_variant_ref_sink(hint)); g_variant_ref_sink(hint));
g_object_unref(pb);
} }
/////// TESTS /////// TESTS

35
test/helpers.c Normal file
View File

@ -0,0 +1,35 @@
#include <gdk-pixbuf/gdk-pixbuf.h>
#include "helpers.h"
GVariant *notification_setup_raw_image(const char *path)
{
GdkPixbuf *pb = gdk_pixbuf_new_from_file(path, NULL);
if (!pb)
return NULL;
GVariant *hint_data = g_variant_new_from_data(
G_VARIANT_TYPE("ay"),
gdk_pixbuf_read_pixels(pb),
gdk_pixbuf_get_byte_length(pb),
TRUE,
(GDestroyNotify) g_object_unref,
g_object_ref(pb));
GVariant *hint = g_variant_new(
"(iiibii@ay)",
gdk_pixbuf_get_width(pb),
gdk_pixbuf_get_height(pb),
gdk_pixbuf_get_rowstride(pb),
gdk_pixbuf_get_has_alpha(pb),
gdk_pixbuf_get_bits_per_sample(pb),
gdk_pixbuf_get_n_channels(pb),
hint_data);
g_object_unref(pb);
return hint;
}
/* vim: set tabstop=8 shiftwidth=8 expandtab textwidth=0: */

9
test/helpers.h Normal file
View File

@ -0,0 +1,9 @@
#ifndef DUNST_TEST_HELPERS_H
#define DUNST_TEST_HELPERS_H
#include <glib.h>
GVariant *notification_setup_raw_image(const char *path);
#endif
/* vim: set tabstop=8 shiftwidth=8 expandtab textwidth=0: */

View File

@ -113,6 +113,61 @@ TEST test_get_pixbuf_from_icon_fileuri(void)
PASS(); PASS();
} }
TEST test_icon_size_clamp_too_small(void)
{
int w = 12, h = 24;
bool resized = icon_size_clamp(&w, &h);
ASSERT(resized);
ASSERT_EQ(w, 16);
ASSERT_EQ(h, 32);
PASS();
}
TEST test_icon_size_clamp_not_necessary(void)
{
int w = 20, h = 30;
bool resized = icon_size_clamp(&w, &h);
ASSERT(!resized);
ASSERT_EQ(w, 20);
ASSERT_EQ(h, 30);
PASS();
}
TEST test_icon_size_clamp_too_big(void)
{
int w = 75, h = 150;
bool resized = icon_size_clamp(&w, &h);
ASSERT(resized);
ASSERT_EQ(w, 50);
ASSERT_EQ(h, 100);
PASS();
}
TEST test_icon_size_clamp_too_small_then_too_big(void)
{
int w = 8, h = 80;
bool resized = icon_size_clamp(&w, &h);
ASSERT(resized);
ASSERT_EQ(w, 10);
ASSERT_EQ(h, 100);
PASS();
}
TEST test_get_pixbuf_from_icon_both_is_scaled(void)
{
GdkPixbuf *pixbuf = get_pixbuf_from_icon("onlypng");
ASSERT(pixbuf);
ASSERT_EQ(gdk_pixbuf_get_width(pixbuf), 16);
ASSERT_EQ(gdk_pixbuf_get_height(pixbuf), 16);
g_clear_pointer(&pixbuf, g_object_unref);
PASS();
}
SUITE(suite_icon) SUITE(suite_icon)
{ {
settings.icon_path = g_strconcat( settings.icon_path = g_strconcat(
@ -129,6 +184,31 @@ SUITE(suite_icon)
RUN_TEST(test_get_pixbuf_from_icon_onlypng); RUN_TEST(test_get_pixbuf_from_icon_onlypng);
RUN_TEST(test_get_pixbuf_from_icon_filename); RUN_TEST(test_get_pixbuf_from_icon_filename);
RUN_TEST(test_get_pixbuf_from_icon_fileuri); RUN_TEST(test_get_pixbuf_from_icon_fileuri);
RUN_TEST(test_icon_size_clamp_not_necessary);
settings.min_icon_size = 16;
settings.max_icon_size = 100;
RUN_TEST(test_get_pixbuf_from_icon_both_is_scaled);
RUN_TEST(test_icon_size_clamp_too_small);
RUN_TEST(test_icon_size_clamp_not_necessary);
RUN_TEST(test_icon_size_clamp_too_big);
RUN_TEST(test_icon_size_clamp_too_small_then_too_big);
settings.min_icon_size = 16;
settings.max_icon_size = 0;
RUN_TEST(test_icon_size_clamp_too_small);
RUN_TEST(test_icon_size_clamp_not_necessary);
settings.min_icon_size = 0;
settings.max_icon_size = 100;
RUN_TEST(test_icon_size_clamp_not_necessary);
RUN_TEST(test_icon_size_clamp_too_big);
settings.min_icon_size = 0;
settings.max_icon_size = 0;
g_clear_pointer(&settings.icon_path, g_free); g_clear_pointer(&settings.icon_path, g_free);
} }

View File

@ -1,5 +1,6 @@
#include "../src/notification.c" #include "../src/notification.c"
#include "greatest.h" #include "greatest.h"
#include "helpers.h"
#include "../src/option_parser.h" #include "../src/option_parser.h"
#include "../src/settings.h" #include "../src/settings.h"
@ -124,6 +125,76 @@ TEST test_notification_referencing(void)
PASS(); PASS();
} }
static struct notification *notification_load_icon_with_scaling(int min_icon_size, int max_icon_size)
{
struct notification *n = notification_create();
char *path = g_strconcat(base, "/data/icons/valid.svg", NULL); // 16x16
GVariant *rawIcon = notification_setup_raw_image(path);
settings.min_icon_size = min_icon_size;
settings.max_icon_size = max_icon_size;
notification_icon_replace_data(n, rawIcon);
settings.min_icon_size = 0;
settings.max_icon_size = 0;
g_variant_unref(rawIcon);
g_free(path);
return n;
}
TEST test_notification_icon_scaling_toosmall(void)
{
struct notification *n = notification_load_icon_with_scaling(20, 100);
ASSERT_EQ(gdk_pixbuf_get_width(n->icon), 20);
ASSERT_EQ(gdk_pixbuf_get_height(n->icon), 20);
notification_unref(n);
PASS();
}
TEST test_notification_icon_scaling_toolarge(void)
{
struct notification *n = notification_load_icon_with_scaling(5, 10);
ASSERT_EQ(gdk_pixbuf_get_width(n->icon), 10);
ASSERT_EQ(gdk_pixbuf_get_height(n->icon), 10);
notification_unref(n);
PASS();
}
TEST test_notification_icon_scaling_notconfigured(void)
{
struct notification *n = notification_load_icon_with_scaling(0, 0);
ASSERT_EQ(gdk_pixbuf_get_width(n->icon), 16);
ASSERT_EQ(gdk_pixbuf_get_height(n->icon), 16);
notification_unref(n);
PASS();
}
TEST test_notification_icon_scaling_notneeded(void)
{
struct notification *n = notification_load_icon_with_scaling(10, 20);
ASSERT_EQ(gdk_pixbuf_get_width(n->icon), 16);
ASSERT_EQ(gdk_pixbuf_get_height(n->icon), 16);
notification_unref(n);
PASS();
}
TEST test_notification_format_message(struct notification *n, const char *format, const char *exp) TEST test_notification_format_message(struct notification *n, const char *format, const char *exp)
{ {
n->format = format; n->format = format;
@ -167,6 +238,10 @@ SUITE(suite_notification)
RUN_TEST(test_notification_is_duplicate); RUN_TEST(test_notification_is_duplicate);
RUN_TEST(test_notification_replace_single_field); RUN_TEST(test_notification_replace_single_field);
RUN_TEST(test_notification_referencing); RUN_TEST(test_notification_referencing);
RUN_TEST(test_notification_icon_scaling_toosmall);
RUN_TEST(test_notification_icon_scaling_toolarge);
RUN_TEST(test_notification_icon_scaling_notconfigured);
RUN_TEST(test_notification_icon_scaling_notneeded);
// TEST notification_format_message // TEST notification_format_message
struct notification *a = notification_create(); struct notification *a = notification_create();