diff --git a/test/dbus.c b/test/dbus.c index f212468..659731a 100644 --- a/test/dbus.c +++ b/test/dbus.c @@ -6,6 +6,7 @@ #include #include +#include "helpers.h" #include "queues.h" 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) { - GdkPixbuf *pb = gdk_pixbuf_new_from_file(path, NULL); - - if (!pb) + GVariant *hint = notification_setup_raw_image(path); + if (!hint) 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_strdup("image-data"), g_variant_ref_sink(hint)); - g_object_unref(pb); } /////// TESTS diff --git a/test/helpers.c b/test/helpers.c new file mode 100644 index 0000000..da3cde2 --- /dev/null +++ b/test/helpers.c @@ -0,0 +1,35 @@ +#include + +#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: */ diff --git a/test/helpers.h b/test/helpers.h new file mode 100644 index 0000000..0b9867a --- /dev/null +++ b/test/helpers.h @@ -0,0 +1,9 @@ +#ifndef DUNST_TEST_HELPERS_H +#define DUNST_TEST_HELPERS_H + +#include + +GVariant *notification_setup_raw_image(const char *path); + +#endif +/* vim: set tabstop=8 shiftwidth=8 expandtab textwidth=0: */ diff --git a/test/icon.c b/test/icon.c index 5bd96aa..94a29ce 100644 --- a/test/icon.c +++ b/test/icon.c @@ -113,6 +113,61 @@ TEST test_get_pixbuf_from_icon_fileuri(void) 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) { 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_filename); 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); } diff --git a/test/notification.c b/test/notification.c index 69040f6..ad73f34 100644 --- a/test/notification.c +++ b/test/notification.c @@ -1,5 +1,6 @@ #include "../src/notification.c" #include "greatest.h" +#include "helpers.h" #include "../src/option_parser.h" #include "../src/settings.h" @@ -124,6 +125,76 @@ TEST test_notification_referencing(void) 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) { n->format = format; @@ -167,6 +238,10 @@ SUITE(suite_notification) RUN_TEST(test_notification_is_duplicate); RUN_TEST(test_notification_replace_single_field); 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 struct notification *a = notification_create();