[gdk-pixbuf/wip/otte/tga] tests: Make add_test_for_all_images() GFile based



commit 27efe4d434f25551ccb31dd1dcbbfd7264b86e89
Author: Benjamin Otte <otte redhat com>
Date:   Mon Oct 12 07:22:11 2015 +0200

    tests: Make add_test_for_all_images() GFile based
    
    Use that to make pixbuf-reftest accept arguments on the command line to
    specify a file to test.

 tests/pixbuf-randomly-modified.c |    9 +++-
 tests/pixbuf-reftest.c           |   85 ++++++++++++++++++++++++++++++--------
 tests/test-common.c              |   81 +++++++++++++++++++++++++++++-------
 tests/test-common.h              |    5 +-
 4 files changed, 141 insertions(+), 39 deletions(-)
---
diff --git a/tests/pixbuf-randomly-modified.c b/tests/pixbuf-randomly-modified.c
index f45f53c..a6ae9ef 100644
--- a/tests/pixbuf-randomly-modified.c
+++ b/tests/pixbuf-randomly-modified.c
@@ -65,14 +65,14 @@ randomly_modify (const gchar *image, guint size)
 static void
 test_randomly_modified (gconstpointer data)
 {
-  const gchar *file = data;
+  GFile *file = G_FILE (data);
   gchar *buffer;
   gsize size;
   gint iterations;
   gint i;
   GError *error = NULL;
 
-  g_file_get_contents (file, &buffer, &size, &error);
+  g_file_load_contents (file, NULL, &buffer, &size, NULL, &error);
   g_assert_no_error (error);
 
   if (g_test_thorough ())
@@ -90,6 +90,7 @@ int
 main (int argc, char **argv)
 {
   gchar *test_images_dir;
+  GFile *test_images;
 #ifdef HAVE_SETRLIMIT
   struct rlimit max_mem_size;
 
@@ -104,7 +105,9 @@ main (int argc, char **argv)
   g_test_init (&argc, &argv, NULL);
 
   test_images_dir = g_build_filename (g_test_get_dir (G_TEST_DIST), "test-images/randomly-modified", NULL);
-  add_test_for_all_images ("/pixbuf/randomly-modified", test_images_dir, test_randomly_modified, NULL);
+  test_images = g_file_new_for_path (test_images_dir);
+  add_test_for_all_images ("/pixbuf/randomly-modified", test_images, test_images, test_randomly_modified, 
NULL);
+  g_object_unref (test_images);
   g_free (test_images_dir);
 
   g_test_message ("Modified image is written to pixbuf-randomly-modified-image");
diff --git a/tests/pixbuf-reftest.c b/tests/pixbuf-reftest.c
index 95a2782..b368378 100644
--- a/tests/pixbuf-reftest.c
+++ b/tests/pixbuf-reftest.c
@@ -71,38 +71,64 @@ loader_area_updated (GdkPixbufLoader  *loader,
                         x, y);
 }
 
-static char *
-make_ref_filename (const char *filename)
+static GFile *
+make_ref_file (GFile *file)
 {
-  return g_strconcat (filename, ".ref.png", NULL);
+  char *uri, *ref_uri;
+  GFile *result;
+
+  uri = g_file_get_uri (file);
+  ref_uri = g_strconcat (uri, ".ref.png", NULL);
+
+  result = g_file_new_for_uri (ref_uri);
+
+  g_free (ref_uri);
+  g_free (uri);
+
+  return result;
 }
 
 static gboolean
-is_not_ref_image (const char *filename)
+is_not_ref_image (GFile *file)
 {
-  return !g_str_has_suffix (filename, ".ref.png");
+  char *uri;
+  gboolean result;
+
+  uri = g_file_get_uri (file);
+
+  result = !g_str_has_suffix (uri, ".ref.png");
+
+  g_free (uri);
+
+  return result;
 }
 
 static void
-test_reftest (gconstpointer file)
+test_reftest (gconstpointer data)
 {
   GdkPixbufLoader *loader;
   GdkPixbuf *reference, *loaded = NULL;
   GError *error = NULL;
-  const char *filename;
-  char *ref_filename;
+  GFile *file, *ref_file;
+  GInputStream *stream;
   guchar *contents;
   gsize i, contents_length;
-  char *content_type, *mime_type;
+  char *filename, *content_type, *mime_type;
   gboolean success;
 
-  filename = file;
-  ref_filename = make_ref_filename (filename);
-  reference = gdk_pixbuf_new_from_file (ref_filename, &error);
+  file = G_FILE (data);
+  filename = g_file_get_path (file);
+  ref_file = make_ref_file (file);
+
+  stream = G_INPUT_STREAM (g_file_read (ref_file, NULL, &error));
+  g_assert_no_error (error);
+  g_assert (stream != NULL);
+  reference = gdk_pixbuf_new_from_stream (stream, NULL, &error);
   g_assert_no_error (error);
   g_assert (reference != NULL);
+  g_object_unref (stream);
 
-  success = g_file_get_contents (filename, (gchar **) &contents, &contents_length, &error);
+  success = g_file_load_contents (file, NULL, (gchar **) &contents, &contents_length, NULL, &error);
   g_assert_no_error (error);
   g_assert (success);
 
@@ -140,19 +166,42 @@ test_reftest (gconstpointer file)
   g_object_unref (loaded);
   g_object_unref (loader);
   g_object_unref (reference);
-  g_free (ref_filename);
+  g_object_unref (ref_file);
+  g_free (filename);
 }
 
 int
 main (int argc, char **argv)
 {
-  gchar *tga_test_images;
 
   g_test_init (&argc, &argv, NULL);
 
-  tga_test_images = g_build_filename (g_test_get_dir (G_TEST_DIST), "test-images/reftests", NULL);
-  add_test_for_all_images ("/pixbuf/reftest", tga_test_images, test_reftest, is_not_ref_image);
-  g_free (tga_test_images);
+  if (argc < 2)
+    {
+      GFile *dir;
+      gchar *test_images;
+
+      test_images = g_build_filename (g_test_get_dir (G_TEST_DIST), "test-images/reftests", NULL);
+      dir = g_file_new_for_path (test_images);
+      
+      add_test_for_all_images ("/pixbuf/reftest", dir, dir, test_reftest, is_not_ref_image);
+
+      g_object_unref (dir);
+      g_free (test_images);
+    }
+  else
+    {
+      guint i;
+
+      for (i = 1; i < argc; i++)
+        {
+          GFile *file = g_file_new_for_commandline_arg (argv[i]);
+
+          add_test_for_all_images ("/pixbuf/reftest", NULL, file, test_reftest, is_not_ref_image);
+
+          g_object_unref (file);
+        }
+    }
 
   return g_test_run ();
 }
diff --git a/tests/test-common.c b/tests/test-common.c
index 0f4e4c0..611a36f 100644
--- a/tests/test-common.c
+++ b/tests/test-common.c
@@ -166,32 +166,81 @@ pixdata_equal (GdkPixbuf  *test,
   return TRUE;
 }
 
+static int
+compare_files (gconstpointer a, gconstpointer b)
+{
+  GFile *file1 = G_FILE (a);
+  GFile *file2 = G_FILE (b);
+  char *uri1, *uri2;
+  int result;
+
+  uri1 = g_file_get_uri (file1);
+  uri2 = g_file_get_uri (file2);
+
+  result = strcmp (uri1, uri2);
+
+  g_free (uri1);
+  g_free (uri2);
+
+  return result;
+}
+
 void
 add_test_for_all_images (const gchar   *prefix,
-                         const gchar   *path,
+                         GFile         *base,
+                         GFile         *file,
                          GTestDataFunc  test_func,
                          AddTestFunc    add_test_func)
 {
-  GDir *dir;
-  const gchar *name;
+  GFileEnumerator *enumerator;
+  GFileInfo *info;
+  GList *l, *files;
+  GError *error = NULL;
+
 
-  dir = g_dir_open (path, 0, NULL);
-  while ((name = g_dir_read_name (dir)) != NULL)
+  if (g_file_query_file_type (file, 0, NULL) != G_FILE_TYPE_DIRECTORY)
     {
       gchar *test_path;
-      gchar *dir_path;
+      gchar *relative_path;
+
+      if (base)
+        relative_path = g_file_get_relative_path (base, file);
+      else
+        relative_path = g_file_get_path (file);
+
+      test_path = g_strconcat (prefix, "/", relative_path, NULL);
+      
+      g_test_add_data_func_full (test_path, g_object_ref (file), test_func, g_object_unref);
+      return;
+    }
 
-      test_path = g_strconcat (prefix, "/", name, NULL);
-      dir_path = g_strconcat (path, "/", name, NULL);
-      if (add_test_func == NULL || add_test_func (dir_path))
+
+  enumerator = g_file_enumerate_children (file, G_FILE_ATTRIBUTE_STANDARD_NAME, 0, NULL, &error);
+  g_assert_no_error (error);
+  files = NULL;
+
+  while ((info = g_file_enumerator_next_file (enumerator, NULL, &error)))
+    {
+      GFile *next_file = g_file_get_child (file, g_file_info_get_name (info));
+
+      if (add_test_func == NULL || add_test_func (next_file))
         {
-          if (g_file_test (dir_path, G_FILE_TEST_IS_DIR))
-            add_test_for_all_images (test_path, dir_path, test_func, add_test_func);
-          else
-            g_test_add_data_func_full (test_path, g_strdup (dir_path), test_func, g_free);
+          files = g_list_prepend (files, g_object_ref (next_file));
         }
-      g_free (test_path);
-      g_free (dir_path);
+
+      g_object_unref (next_file);
+      g_object_unref (info);
     }
-  g_dir_close (dir);
+  
+  g_assert_no_error (error);
+  g_object_unref (enumerator);
+
+  files = g_list_sort (files, compare_files);
+
+  for (l = files; l; l = l->next)
+    {
+      add_test_for_all_images (prefix, base, l->data, test_func, add_test_func);
+    }
+
+  g_list_free_full (files, g_object_unref);
 }
diff --git a/tests/test-common.h b/tests/test-common.h
index 6b53242..f0e1b1a 100644
--- a/tests/test-common.h
+++ b/tests/test-common.h
@@ -27,13 +27,14 @@
 
 G_BEGIN_DECLS
 
-typedef gboolean (* AddTestFunc) (const gchar *filename);
+typedef gboolean (* AddTestFunc) (GFile *file);
 
 gboolean format_supported (const gchar *filename);
 gboolean skip_if_insufficient_memory (GError **err);
 gboolean pixdata_equal (GdkPixbuf *test, GdkPixbuf *ref, GError **error);
 void add_test_for_all_images (const gchar   *prefix,
-                              const gchar   *dir,
+                              GFile         *base,
+                              GFile         *file,
                               GTestDataFunc  test_func,
                               AddTestFunc    add_test_func);
 


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