[gtk/image-loading: 11/15] Add contentserializer tests for textures




commit 67f4dd40c7de5c8f8b2c6db25c6ec294ea08b260
Author: Matthias Clasen <mclasen redhat com>
Date:   Sat Sep 11 21:38:31 2021 -0400

    Add contentserializer tests for textures

 testsuite/gdk/contentserializer.c | 74 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 74 insertions(+)
---
diff --git a/testsuite/gdk/contentserializer.c b/testsuite/gdk/contentserializer.c
index d20a60d99e..c0befad4ce 100644
--- a/testsuite/gdk/contentserializer.c
+++ b/testsuite/gdk/contentserializer.c
@@ -30,6 +30,49 @@ compare_rgba_values (GValue *v1, GValue *v2)
                          (GdkRGBA *)g_value_get_boxed (v2));
 }
 
+static gboolean
+textures_equal (GdkTexture *t1,  GdkTexture *t2)
+{
+  guchar *d1, *d2;
+  int width, height;
+  gboolean ret;
+
+  width = gdk_texture_get_width (t1);
+  height = gdk_texture_get_height (t1);
+
+  if (width != gdk_texture_get_width (t2))
+    return FALSE;
+  if (height != gdk_texture_get_height (t2))
+    return FALSE;
+
+  d1 = g_malloc (width * height * 4);
+  d2 = g_malloc (width * height * 4);
+
+  gdk_texture_download (t1, d1, width * 4);
+  gdk_texture_download (t2, d2, width * 4);
+
+  ret = memcmp (d1, d2, width * height * 4) == 0;
+
+  if (!ret)
+    {
+      gdk_texture_save_to_png (t1, "texture1.png");
+      gdk_texture_save_to_png (t2, "texture2.png");
+    }
+  g_free (d1);
+  g_free (d2);
+
+  return ret;
+}
+
+static gboolean
+compare_texture_values (GValue *v1, GValue *v2)
+{
+  return G_VALUE_TYPE (v1) == GDK_TYPE_TEXTURE &&
+         G_VALUE_TYPE (v2) == GDK_TYPE_TEXTURE &&
+         textures_equal ((GdkTexture *)g_value_get_object (v1),
+                         (GdkTexture *)g_value_get_object (v2));
+}
+
 static gboolean
 compare_file_values (GValue *v1, GValue *v2)
 {
@@ -182,6 +225,35 @@ test_content_color (void)
   g_value_unset (&value);
 }
 
+static void
+test_content_texture (gconstpointer data)
+{
+  const char *mimetype = data;
+  GValue value = G_VALUE_INIT;
+  const char *filename;
+  char *path;
+  GFile *file;
+  GdkTexture *texture;
+  GError *error = NULL;
+
+  if (strcmp (mimetype, "image/jpeg") == 0)
+    filename = "image.jpeg";
+  else
+    filename = "image.png";
+
+  path = g_test_build_filename (G_TEST_DIST, "clipboard-data", filename, NULL);
+  file = g_file_new_for_path (path);
+  texture = gdk_texture_new_from_file (file, &error);
+  g_assert_no_error (error);
+  g_object_unref (file);
+  g_free (path);
+
+  g_value_init (&value, GDK_TYPE_TEXTURE);
+  g_value_set_object (&value, texture);
+  test_content_roundtrip (&value, mimetype, compare_texture_values);
+  g_value_unset (&value);
+}
+
 static void
 test_content_file (void)
 {
@@ -406,6 +478,8 @@ main (int argc, char *argv[])
   g_test_add_func ("/content/text_plain_utf8", test_content_text_plain_utf8);
   g_test_add_func ("/content/text_plain", test_content_text_plain);
   g_test_add_func ("/content/color", test_content_color);
+  g_test_add_data_func ("/content/texture/png", "image/png", test_content_texture);
+  g_test_add_data_func ("/content/texture/tiff", "image/tiff", test_content_texture);
   g_test_add_func ("/content/file", test_content_file);
   g_test_add_func ("/content/files", test_content_files);
   g_test_add_func ("/content/custom", test_custom_format);


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