From 3f7de41732b42fbedb6e5a0643f9966be4dc1298 Mon Sep 17 00:00:00 2001 From: Benedikt Heine Date: Sat, 10 Mar 2018 14:38:38 +0100 Subject: [PATCH] Add tests for icon.c --- .valgrind.suppressions | 29 ++++++++ test/data/icons/invalid.png | 1 + test/data/icons/invalid.svg | 1 + test/data/icons/path/invalid/icon1.png | 1 + test/data/icons/path/invalid/icon1.svg | 1 + test/data/icons/path/valid/icon1.png | 1 + test/data/icons/path/valid/icon1.svg | 1 + test/data/icons/path/valid/onlypng.png | 1 + test/data/icons/path/valid/onlysvg.svg | 1 + test/data/icons/valid.png | Bin 0 -> 193 bytes test/data/icons/valid.svg | 65 ++++++++++++++++ test/icon.c | 98 +++++++++++++++++++++++++ test/notification.c | 2 + test/test.c | 2 + 14 files changed, 204 insertions(+) create mode 100644 test/data/icons/invalid.png create mode 100644 test/data/icons/invalid.svg create mode 120000 test/data/icons/path/invalid/icon1.png create mode 120000 test/data/icons/path/invalid/icon1.svg create mode 120000 test/data/icons/path/valid/icon1.png create mode 120000 test/data/icons/path/valid/icon1.svg create mode 120000 test/data/icons/path/valid/onlypng.png create mode 120000 test/data/icons/path/valid/onlysvg.svg create mode 100644 test/data/icons/valid.png create mode 100644 test/data/icons/valid.svg create mode 100644 test/icon.c diff --git a/.valgrind.suppressions b/.valgrind.suppressions index de42697df..2b694ff48 100644 --- a/.valgrind.suppressions +++ b/.valgrind.suppressions @@ -8,3 +8,32 @@ ... fun:main } + +# librsvg leaks some memory, when an invalid svg file is read +# TODO: find the memory leak and fix it upstream +{ + invalid_svgs1 + Memcheck:Leak + ... + fun:gdk_pixbuf__svg_image_load_increment + ... + fun:get_pixbuf_from_file +} + +{ + invalid_svgs2 + Memcheck:Leak + ... + fun:gdk_pixbuf__svg_image_begin_load + ... + fun:get_pixbuf_from_file +} + +{ + invalid_svgs3 + Memcheck:Leak + ... + fun:rsvg_handle_write + ... + fun:get_pixbuf_from_file +} diff --git a/test/data/icons/invalid.png b/test/data/icons/invalid.png new file mode 100644 index 000000000..07623af69 --- /dev/null +++ b/test/data/icons/invalid.png @@ -0,0 +1 @@ +Got'cha! This has to be invalid! diff --git a/test/data/icons/invalid.svg b/test/data/icons/invalid.svg new file mode 100644 index 000000000..07623af69 --- /dev/null +++ b/test/data/icons/invalid.svg @@ -0,0 +1 @@ +Got'cha! This has to be invalid! diff --git a/test/data/icons/path/invalid/icon1.png b/test/data/icons/path/invalid/icon1.png new file mode 120000 index 000000000..d6b006b8f --- /dev/null +++ b/test/data/icons/path/invalid/icon1.png @@ -0,0 +1 @@ +../../invalid.png \ No newline at end of file diff --git a/test/data/icons/path/invalid/icon1.svg b/test/data/icons/path/invalid/icon1.svg new file mode 120000 index 000000000..b5f5825fc --- /dev/null +++ b/test/data/icons/path/invalid/icon1.svg @@ -0,0 +1 @@ +../../invalid.svg \ No newline at end of file diff --git a/test/data/icons/path/valid/icon1.png b/test/data/icons/path/valid/icon1.png new file mode 120000 index 000000000..28a474c36 --- /dev/null +++ b/test/data/icons/path/valid/icon1.png @@ -0,0 +1 @@ +../../valid.png \ No newline at end of file diff --git a/test/data/icons/path/valid/icon1.svg b/test/data/icons/path/valid/icon1.svg new file mode 120000 index 000000000..95267e345 --- /dev/null +++ b/test/data/icons/path/valid/icon1.svg @@ -0,0 +1 @@ +../../valid.svg \ No newline at end of file diff --git a/test/data/icons/path/valid/onlypng.png b/test/data/icons/path/valid/onlypng.png new file mode 120000 index 000000000..28a474c36 --- /dev/null +++ b/test/data/icons/path/valid/onlypng.png @@ -0,0 +1 @@ +../../valid.png \ No newline at end of file diff --git a/test/data/icons/path/valid/onlysvg.svg b/test/data/icons/path/valid/onlysvg.svg new file mode 120000 index 000000000..95267e345 --- /dev/null +++ b/test/data/icons/path/valid/onlysvg.svg @@ -0,0 +1 @@ +../../valid.svg \ No newline at end of file diff --git a/test/data/icons/valid.png b/test/data/icons/valid.png new file mode 100644 index 0000000000000000000000000000000000000000..014e6a7d318b3c524d84741683be66f10881c346 GIT binary patch literal 193 zcmeAS@N?(olHy`uVBq!ia0vp^EFjFm1|(O0oL2{=*pj^6T^Rm@;DWu&Cj&(|3p^r= z85p>QL70(Y)*K0-AbW|YuPgf_W-cyy?iW!vVu3=knIRD+&iT2ysd*(pE(3#eQEFmI zYKlU6W=V#EyQgnJie4%^P+Zm1#W95Ay6LHnybKBqhYhCxuh(Nv?Ov)8y42NwAL}O> couquT*e!nqerF%P8UQlM)78&qol`;+0G19l%K!iX literal 0 HcmV?d00001 diff --git a/test/data/icons/valid.svg b/test/data/icons/valid.svg new file mode 100644 index 000000000..7a1cb7f35 --- /dev/null +++ b/test/data/icons/valid.svg @@ -0,0 +1,65 @@ + + + + + + + + + + image/svg+xml + + + + + + + + + diff --git a/test/icon.c b/test/icon.c new file mode 100644 index 000000000..56abb07a1 --- /dev/null +++ b/test/icon.c @@ -0,0 +1,98 @@ +#include "greatest.h" +#include "../src/icon.h" +#include "../src/utils.h" + +#include +#include + +#define ICONPREFIX "./data/icons/path" + +/* As there are no hints to test if the loaded GdkPixbuf is + * read from a PNG or an SVG file, the sample icons in the + * test structure have different sizes + */ +#define IS_ICON_PNG(pb) 4 == gdk_pixbuf_get_width(pb) +#define IS_ICON_SVG(pb) 16 == gdk_pixbuf_get_width(pb) + +TEST test_get_pixbuf_from_path_invalid(void) +{ + GdkPixbuf *pixbuf = get_pixbuf_from_path("invalid"); + ASSERT(pixbuf == NULL); + g_clear_pointer(&pixbuf, g_object_unref); + + PASS(); +} + +TEST test_get_pixbuf_from_path_both(void) +{ + GdkPixbuf *pixbuf = get_pixbuf_from_path("icon1"); + ASSERT(pixbuf); + ASSERTm("SVG pixbuf hasn't precedence", IS_ICON_SVG(pixbuf)); + g_clear_pointer(&pixbuf, g_object_unref); + + PASS(); +} + +TEST test_get_pixbuf_from_path_onlysvg(void) +{ + GdkPixbuf *pixbuf = get_pixbuf_from_path("onlysvg"); + ASSERT(pixbuf); + ASSERTm("SVG pixbuf isn't loaded", IS_ICON_SVG(pixbuf)); + g_clear_pointer(&pixbuf, g_object_unref); + + PASS(); +} + +TEST test_get_pixbuf_from_path_onlypng(void) +{ + GdkPixbuf *pixbuf = get_pixbuf_from_path("onlypng"); + ASSERT(pixbuf); + ASSERTm("PNG pixbuf isn't loaded", IS_ICON_PNG(pixbuf)); + g_clear_pointer(&pixbuf, g_object_unref); + + PASS(); +} + +TEST test_get_pixbuf_from_path_filename(void) +{ + char *icon = string_append(g_get_current_dir(), "/data/icons/valid.png", NULL); + GdkPixbuf *pixbuf = get_pixbuf_from_path(icon); + ASSERT(pixbuf); + ASSERTm("PNG pixbuf isn't loaded", IS_ICON_PNG(pixbuf)); + g_clear_pointer(&pixbuf, g_object_unref); + + g_free(icon); + PASS(); +} + +TEST test_get_pixbuf_from_path_fileuri(void) +{ + char *curdir = g_get_current_dir(); + char *icon = g_strconcat("file://", curdir,"/data/icons/valid.svg", NULL); + GdkPixbuf *pixbuf = get_pixbuf_from_path(icon); + ASSERT(pixbuf); + ASSERTm("SVG pixbuf isn't loaded", IS_ICON_SVG(pixbuf)); + g_clear_pointer(&pixbuf, g_object_unref); + + g_free(icon); + g_free(curdir); + PASS(); +} + +SUITE(suite_icon) +{ + settings.icon_path = + ICONPREFIX "/invalid" + ":" ICONPREFIX "/valid" + ":" ICONPREFIX "/both"; + + RUN_TEST(test_get_pixbuf_from_path_invalid); + RUN_TEST(test_get_pixbuf_from_path_both); + RUN_TEST(test_get_pixbuf_from_path_onlysvg); + RUN_TEST(test_get_pixbuf_from_path_onlypng); + RUN_TEST(test_get_pixbuf_from_path_filename); + RUN_TEST(test_get_pixbuf_from_path_fileuri); + + settings.icon_path = NULL; +} +/* vim: set tabstop=8 shiftwidth=8 expandtab textwidth=0: */ diff --git a/test/notification.c b/test/notification.c index c4e349440..a9c02d611 100644 --- a/test/notification.c +++ b/test/notification.c @@ -120,6 +120,8 @@ SUITE(suite_notification) g_free(b); RUN_TEST(test_notification_replace_single_field); + + g_clear_pointer(&settings.icon_path, g_free); } /* vim: set tabstop=8 shiftwidth=8 expandtab textwidth=0: */ diff --git a/test/test.c b/test/test.c index 935bda942..09835fde0 100644 --- a/test/test.c +++ b/test/test.c @@ -8,6 +8,7 @@ SUITE_EXTERN(suite_utils); SUITE_EXTERN(suite_option_parser); SUITE_EXTERN(suite_notification); SUITE_EXTERN(suite_markup); +SUITE_EXTERN(suite_icon); GREATEST_MAIN_DEFS(); @@ -20,6 +21,7 @@ int main(int argc, char *argv[]) { RUN_SUITE(suite_option_parser); RUN_SUITE(suite_notification); RUN_SUITE(suite_markup); + RUN_SUITE(suite_icon); GREATEST_MAIN_END(); } /* vim: set tabstop=8 shiftwidth=8 expandtab textwidth=0: */