[gtk/image-loading: 4/14] Add tests for the png loader




commit f6ebbec3a62bdb715e6a83223f1b9dc3eccb3427
Author: Matthias Clasen <mclasen redhat com>
Date:   Sat Sep 11 20:27:13 2021 -0400

    Add tests for the png loader

 testsuite/gdk/image-data/image.png | Bin 0 -> 1609 bytes
 testsuite/gdk/image.c              | 266 +++++++++++++++++++++++++++++++++++++
 testsuite/gdk/meson.build          |  26 ++++
 3 files changed, 292 insertions(+)
---
diff --git a/testsuite/gdk/image-data/image.png b/testsuite/gdk/image-data/image.png
new file mode 100644
index 0000000000..49ee0d459e
Binary files /dev/null and b/testsuite/gdk/image-data/image.png differ
diff --git a/testsuite/gdk/image.c b/testsuite/gdk/image.c
new file mode 100644
index 0000000000..b2958ed766
--- /dev/null
+++ b/testsuite/gdk/image.c
@@ -0,0 +1,266 @@
+#include <gtk/gtk.h>
+#include "gdk/gdkpngprivate.h"
+
+static void
+test_load_image (gconstpointer data)
+{
+  const char *filename = data;
+  GdkTexture *texture;
+  char *path;
+  GFile *file;
+  GInputStream *stream;
+  GError *error = NULL;
+
+  path = g_test_build_filename (G_TEST_DIST, "image-data", filename, NULL);
+  file = g_file_new_for_path (path);
+  stream = G_INPUT_STREAM (g_file_read (file, NULL, &error));
+  g_assert_no_error (error);
+
+  if (g_str_has_suffix (filename, ".png"))
+    texture = gdk_load_png (stream, &error);
+  else
+    g_assert_not_reached ();
+
+  g_assert_no_error (error);
+  g_assert_true (GDK_IS_TEXTURE (texture));
+  g_assert_cmpint (gdk_texture_get_width (texture), ==, 32);
+  g_assert_cmpint (gdk_texture_get_height (texture), ==, 32);
+
+  g_object_unref (texture);
+  g_object_unref (stream);
+  g_object_unref (file);
+  g_free (path);
+}
+
+typedef struct {
+  const char *filename;
+  GdkTexture *texture;
+  GError *error;
+  gboolean done;
+} LoadData;
+
+static void
+load_image_done (GObject      *source,
+                 GAsyncResult *result,
+                 gpointer      data)
+{
+  LoadData *ldata = data;
+
+  if (g_str_has_suffix (ldata->filename, ".png"))
+    ldata->texture = gdk_load_png_finish (result, &ldata->error);
+  else
+    g_assert_not_reached ();
+  ldata->done = TRUE;
+
+  g_main_context_wakeup (NULL);
+}
+
+static void
+test_load_image_async (gconstpointer data)
+{
+  const char *filename = data;
+  char *path;
+  GFile *file;
+  GInputStream *stream;
+  GError *error = NULL;
+  LoadData ldata = { filename, NULL, NULL, FALSE };
+
+  path = g_test_build_filename (G_TEST_DIST, "image-data", filename, NULL);
+  file = g_file_new_for_path (path);
+  stream = G_INPUT_STREAM (g_file_read (file, NULL, &error));
+  g_assert_no_error (error);
+
+  if (g_str_has_suffix (filename, ".png"))
+    gdk_load_png_async (stream, NULL, load_image_done, &ldata);
+  else
+    g_assert_not_reached ();
+  while (!ldata.done)
+    g_main_context_iteration (NULL, TRUE);
+
+  g_assert_no_error (ldata.error);
+  g_assert_true (GDK_IS_TEXTURE (ldata.texture));
+  g_assert_cmpint (gdk_texture_get_width (ldata.texture), ==, 32);
+  g_assert_cmpint (gdk_texture_get_height (ldata.texture), ==, 32);
+
+  g_object_unref (ldata.texture);
+  g_object_unref (stream);
+  g_object_unref (file);
+  g_free (path);
+}
+
+static void
+test_save_image (gconstpointer test_data)
+{
+  const char *filename = test_data;
+  char *path;
+  GFile *file;
+  GdkTexture *texture;
+  int width, height;
+  guchar *data;
+  GFile *file2;
+  GIOStream *stream;
+  gboolean ret;
+  GdkTexture *texture2;
+  guchar *data2;
+  int width2, height2;
+  GError *error = NULL;
+
+  path = g_test_build_filename (G_TEST_DIST, "image-data", filename, NULL);
+  file = g_file_new_for_path (path);
+  texture = gdk_texture_new_from_file (file, &error);
+  g_assert_no_error (error);
+
+  width = gdk_texture_get_width (texture);
+  height = gdk_texture_get_height (texture);
+  data = g_malloc (width * height * 4);
+  gdk_texture_download (texture, data, width * 4);
+
+  file2 = g_file_new_tmp ("imageXXXXXX", (GFileIOStream **)&stream, NULL);
+
+  if (g_str_has_suffix (filename, ".png"))
+    ret = gdk_save_png (g_io_stream_get_output_stream (stream),
+                        data,
+                        width, height, width * 4,
+                        GDK_MEMORY_DEFAULT,
+                        &error);
+  else
+    g_assert_not_reached ();
+
+  g_assert_true (ret);
+  g_assert_no_error (error);
+
+  g_io_stream_close (stream, NULL, NULL);
+  g_object_unref (stream);
+
+  texture2 = gdk_texture_new_from_file (file2, &error);
+  g_assert_no_error (error);
+  width2 = gdk_texture_get_width (texture2);
+  height2 = gdk_texture_get_height (texture2);
+  data2 = g_malloc (width2 * height2 * 4);
+  gdk_texture_download (texture2, data2, width2 * 4);
+
+  g_assert_cmpint (width, ==, width2);
+  g_assert_cmpint (height, ==, height2);
+
+  if (memcmp (data, data2, width * height * 4) != 0)
+    {
+      g_test_fail ();
+      g_print ("file %s did not match %s\n", g_file_get_path (file2), g_file_get_path (file));
+      return;
+    }
+  g_assert_true (memcmp (data, data2, width * height * 4) == 0);
+
+  g_free (data2);
+  g_object_unref (texture2);
+
+  g_free (data);
+  g_object_unref (file2);
+  g_object_unref (texture);
+  g_object_unref (file);
+  g_free (path);
+}
+
+typedef struct {
+  const char *filename;
+  GError *error;
+  gboolean ret;
+  gboolean done;
+} SaveData;
+
+static void
+save_image_done (GObject      *source,
+                 GAsyncResult *result,
+                 gpointer      data)
+{
+  SaveData *sdata = data;
+
+  if (g_str_has_suffix (sdata->filename, ".png"))
+    sdata->ret = gdk_save_png_finish (result, &sdata->error);
+  else
+    g_assert_not_reached ();
+  sdata->done = TRUE;
+
+  g_main_context_wakeup (NULL);
+}
+static void
+test_save_image_async (gconstpointer test_data)
+{
+  const char *filename = test_data;
+  char *path;
+  GFile *file;
+  GdkTexture *texture;
+  int width, height;
+  guchar *data;
+  GFile *file2;
+  GIOStream *stream;
+  GdkTexture *texture2;
+  guchar *data2;
+  int width2, height2;
+  GError *error = NULL;
+  SaveData sdata = { filename, NULL, FALSE, FALSE };
+
+  path = g_test_build_filename (G_TEST_DIST, "image-data", filename, NULL);
+  file = g_file_new_for_path (path);
+  texture = gdk_texture_new_from_file (file, &error);
+  g_assert_no_error (error);
+
+  width = gdk_texture_get_width (texture);
+  height = gdk_texture_get_height (texture);
+  data = g_malloc (width * height * 4);
+  gdk_texture_download (texture, data, width * 4);
+
+  file2 = g_file_new_tmp ("pngXXXXXX", (GFileIOStream **)&stream, NULL);
+
+  if (g_str_has_suffix (filename, ".png"))
+    gdk_save_png_async (g_io_stream_get_output_stream (stream),
+                        data,
+                        width, height, width * 4,
+                        GDK_MEMORY_DEFAULT,
+                        NULL,
+                        save_image_done,
+                        &sdata);
+  else
+    g_assert_not_reached ();
+
+  while (!sdata.done)
+    g_main_context_iteration (NULL, TRUE);
+
+  g_assert_true (sdata.ret);
+  g_assert_no_error (sdata.error);
+
+  g_io_stream_close (stream, NULL, NULL);
+  g_object_unref (stream);
+
+  texture2 = gdk_texture_new_from_file (file2, &error);
+  g_assert_no_error (error);
+  width2 = gdk_texture_get_width (texture2);
+  height2 = gdk_texture_get_height (texture2);
+  data2 = g_malloc (width2 * height2 * 4);
+  gdk_texture_download (texture2, data2, width2 * 4);
+
+  g_assert_cmpint (width, ==, width2);
+  g_assert_cmpint (height, ==, height2);
+  g_assert_true (memcmp (data, data2, width * height * 4) == 0);
+
+  g_free (data2);
+  g_object_unref (texture2);
+
+  g_free (data);
+  g_object_unref (file2);
+  g_object_unref (texture);
+  g_object_unref (file);
+  g_free (path);
+}
+
+int
+main (int argc, char *argv[])
+{
+  (g_test_init) (&argc, &argv, NULL);
+
+  g_test_add_data_func ("/image/load/png/sync", "image.png", test_load_image);
+  g_test_add_data_func ("/image/load/png/async", "image.png", test_load_image_async);
+  g_test_add_data_func ("/image/save/png/sync", "image.png", test_save_image);
+  g_test_add_data_func ("/image/save/png/async", "image.png", test_save_image_async);
+
+  return g_test_run ();
+}
diff --git a/testsuite/gdk/meson.build b/testsuite/gdk/meson.build
index 5e62e98775..3933ed8587 100644
--- a/testsuite/gdk/meson.build
+++ b/testsuite/gdk/meson.build
@@ -48,6 +48,31 @@ foreach t : tests
   )
 endforeach
 
+internal_tests = [
+  'image'
+]
+
+foreach t : internal_tests
+  test_exe = executable(t, '@0@.c'.format(t),
+    c_args: common_cflags,
+    dependencies: libgtk_static_dep,
+    install: get_option('install-tests'),
+    install_dir: testexecdir,
+  )
+
+  test(t, test_exe,
+    args: [ '--tap', '-k' ],
+    protocol: 'tap',
+    env: [
+      'G_TEST_SRCDIR=@0@'.format(meson.current_source_dir()),
+      'G_TEST_BUILDDIR=@0@'.format(meson.current_build_dir()),
+      'DBUS_SESSION_BUS_ADDRESS=',
+    ],
+    suite: 'gdk',
+  )
+endforeach
+
+
 if get_option('install-tests')
   foreach t : tests
     test_cdata = configuration_data()
@@ -62,4 +87,5 @@ if get_option('install-tests')
   endforeach
 
   install_subdir('clipboard-data', install_dir: testexecdir)
+  install_subdir('image-data', install_dir: testexecdir)
 endif


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]