From b5f2a043099ff4b256c2a63a2c3bacea9e3ecafe Mon Sep 17 00:00:00 2001 From: Colin Clark Date: Sun, 24 Dec 2023 15:39:31 +0000 Subject: [PATCH 1/2] Check pixbuf loader signals This test will detect the problem noted in #73. --- tests/meson.build | 4 ++- tests/t_signals.c | 83 +++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 tests/t_signals.c diff --git a/tests/meson.build b/tests/meson.build index f78a94b..a3ac617 100644 --- a/tests/meson.build +++ b/tests/meson.build @@ -10,6 +10,7 @@ t_null_error = executable('t_null_error', 't_null_error.c', dependencies : [gdk t_scaled = executable('t_scaled', 't_scaled.c', dependencies : [gdkpb, webp, webpdemux]) t_jpeg = executable('t_jpeg', 't_jpeg.c', dependencies : [gdkpb, webp, webpdemux]) t_large = executable('t_large', 't_large.c', dependencies : [gdkpb, webp, webpdemux, gio]) +t_signals = executable('t_signals', 't_signals.c', dependencies : [gdkpb, webp, webpdemux, gio]) loaders_data = configuration_data() loaders_data.set('MODULE_PATH', fs.as_posix(pbl_webp.full_path())) @@ -29,4 +30,5 @@ test('icc data', t_icc, env : test_env + [ test_file_base / 't2.webp'] test('NULL GError', t_null_error, env : test_env + [ test_file_base / 't2.webp']) test('large file', t_large, env : test_env + [ test_file_base / 't_large.webp']) test('scaled image', t_scaled, env : test_env + [ test_file_base / 't2.webp', 'TEST_FILE_ANIM=' + meson.current_source_dir() / 'data' / 't3.webp']) -test('jpeg with .webp extension', t_jpeg, env : test_env + [ test_file_base / 'test_jpeg_as_webp.webp']) \ No newline at end of file +test('jpeg with .webp extension', t_jpeg, env : test_env + [ test_file_base / 'test_jpeg_as_webp.webp']) +test('signal handling', t_signals, env : test_env + [ test_file_base / 't2.webp']) diff --git a/tests/t_signals.c b/tests/t_signals.c new file mode 100644 index 0000000..010dffb --- /dev/null +++ b/tests/t_signals.c @@ -0,0 +1,83 @@ +#include + +gint area_prepared_cb_count = 0; +gint area_updated_cb_count = 0; +gint closed_cb_count = 0; +gint size_prepared_cb_count = 0; + +void +area_prepared_cb (GdkPixbufLoader* self, gpointer user_data) +{ + area_prepared_cb_count++; +} + +void +area_updated_cb (GdkPixbufLoader* self, gint x, gint y, gint width, gint height, gpointer user_data) +{ + area_updated_cb_count++; + + g_assert_cmpint (x, ==, 0); + g_assert_cmpint (y, ==, 0); + g_assert_cmpint (width, ==, 200); + g_assert_cmpint (height, ==, 200); +} + +void +closed_cb (GdkPixbufLoader* self, gpointer user_data) +{ + closed_cb_count++; +} + +void +size_prepared_cb (GdkPixbufLoader* self, gint width, gint height, gpointer user_data) +{ + size_prepared_cb_count++; + + g_assert_cmpint (width, ==, 200); + g_assert_cmpint (height, ==, 200); +} + +gint +main (gint argc, gchar **argv) +{ + gchar **env = g_get_environ (); + GdkPixbufLoader *loader; + GError *error = NULL; + gsize pixbuf_size; + guchar *pixbuf_data; + + loader = gdk_pixbuf_loader_new(); + + g_signal_connect (G_OBJECT(loader), "area-prepared", G_CALLBACK(area_prepared_cb), NULL); + g_signal_connect (G_OBJECT(loader), "area-updated", G_CALLBACK(area_updated_cb), NULL); + g_signal_connect (G_OBJECT(loader), "closed", G_CALLBACK(closed_cb), NULL); + g_signal_connect (G_OBJECT(loader), "size-prepared", G_CALLBACK(size_prepared_cb), NULL); + + g_file_get_contents (g_environ_getenv (env, "TEST_FILE"), (gchar**)&pixbuf_data, &pixbuf_size, &error); + if (error) + g_error ("%s", error->message); + g_assert (error == NULL); + + gdk_pixbuf_loader_write(loader, pixbuf_data, pixbuf_size, &error); + if (error) + g_error ("%s", error->message); + g_assert (error == NULL); + + g_free(pixbuf_data); + + gdk_pixbuf_loader_close(loader, &error); + if (error) + g_error ("%s", error->message); + g_assert (error == NULL); + + g_object_unref(G_OBJECT(loader)); + + g_strfreev (env); + + g_assert_cmpint (area_prepared_cb_count, ==, 1); + g_assert_cmpint (area_updated_cb_count, ==, 1); + g_assert_cmpint (closed_cb_count, ==, 1); + g_assert_cmpint (size_prepared_cb_count, ==, 1); + + return 0; +} From 7c4d927b40c4e31086ddab3fb37c66605c0aebac Mon Sep 17 00:00:00 2001 From: Colin Clark Date: Thu, 18 Jan 2024 14:09:51 +0000 Subject: [PATCH 2/2] Requested code amendments --- tests/t_signals.c | 52 ++++++++++++++++++++++++++++++----------------- 1 file changed, 33 insertions(+), 19 deletions(-) diff --git a/tests/t_signals.c b/tests/t_signals.c index 010dffb..332ee35 100644 --- a/tests/t_signals.c +++ b/tests/t_signals.c @@ -1,20 +1,27 @@ #include -gint area_prepared_cb_count = 0; -gint area_updated_cb_count = 0; -gint closed_cb_count = 0; -gint size_prepared_cb_count = 0; +typedef struct +{ + gint area_prepared_cb_count; + gint area_updated_cb_count; + gint closed_cb_count; + gint size_prepared_cb_count; +} SignalsCounters; void area_prepared_cb (GdkPixbufLoader* self, gpointer user_data) { - area_prepared_cb_count++; + SignalsCounters *counters = (SignalsCounters *) user_data; + + counters->area_prepared_cb_count++; } void area_updated_cb (GdkPixbufLoader* self, gint x, gint y, gint width, gint height, gpointer user_data) { - area_updated_cb_count++; + SignalsCounters *counters = (SignalsCounters *) user_data; + + counters->area_updated_cb_count++; g_assert_cmpint (x, ==, 0); g_assert_cmpint (y, ==, 0); @@ -25,13 +32,17 @@ area_updated_cb (GdkPixbufLoader* self, gint x, gint y, gint width, gint height, void closed_cb (GdkPixbufLoader* self, gpointer user_data) { - closed_cb_count++; + SignalsCounters *counters = (SignalsCounters *) user_data; + + counters->closed_cb_count++; } void size_prepared_cb (GdkPixbufLoader* self, gint width, gint height, gpointer user_data) { - size_prepared_cb_count++; + SignalsCounters *counters = (SignalsCounters *) user_data; + + counters->size_prepared_cb_count++; g_assert_cmpint (width, ==, 200); g_assert_cmpint (height, ==, 200); @@ -45,13 +56,14 @@ main (gint argc, gchar **argv) GError *error = NULL; gsize pixbuf_size; guchar *pixbuf_data; + SignalsCounters *counters = g_new0(SignalsCounters, 1); loader = gdk_pixbuf_loader_new(); - g_signal_connect (G_OBJECT(loader), "area-prepared", G_CALLBACK(area_prepared_cb), NULL); - g_signal_connect (G_OBJECT(loader), "area-updated", G_CALLBACK(area_updated_cb), NULL); - g_signal_connect (G_OBJECT(loader), "closed", G_CALLBACK(closed_cb), NULL); - g_signal_connect (G_OBJECT(loader), "size-prepared", G_CALLBACK(size_prepared_cb), NULL); + g_signal_connect (G_OBJECT(loader), "area-prepared", G_CALLBACK(area_prepared_cb), counters); + g_signal_connect (G_OBJECT(loader), "area-updated", G_CALLBACK(area_updated_cb), counters); + g_signal_connect (G_OBJECT(loader), "closed", G_CALLBACK(closed_cb), counters); + g_signal_connect (G_OBJECT(loader), "size-prepared", G_CALLBACK(size_prepared_cb), counters); g_file_get_contents (g_environ_getenv (env, "TEST_FILE"), (gchar**)&pixbuf_data, &pixbuf_size, &error); if (error) @@ -63,21 +75,23 @@ main (gint argc, gchar **argv) g_error ("%s", error->message); g_assert (error == NULL); - g_free(pixbuf_data); + g_clear_pointer(&pixbuf_data, g_free); gdk_pixbuf_loader_close(loader, &error); if (error) g_error ("%s", error->message); g_assert (error == NULL); - g_object_unref(G_OBJECT(loader)); + g_clear_object(&loader); + + g_clear_pointer(&env, g_strfreev); - g_strfreev (env); + g_assert_cmpint (counters->area_prepared_cb_count, ==, 1); + g_assert_cmpint (counters->area_updated_cb_count, ==, 1); + g_assert_cmpint (counters->closed_cb_count, ==, 1); + g_assert_cmpint (counters->size_prepared_cb_count, ==, 1); - g_assert_cmpint (area_prepared_cb_count, ==, 1); - g_assert_cmpint (area_updated_cb_count, ==, 1); - g_assert_cmpint (closed_cb_count, ==, 1); - g_assert_cmpint (size_prepared_cb_count, ==, 1); + g_clear_pointer(&counters, g_free); return 0; }