[folks] Add utility function to compare LoadableIcon content.



commit 22a72f0579e37a6ddb113846eb373a5c8995cfed
Author: Travis Reitter <travis reitter collabora co uk>
Date:   Tue Jul 26 15:53:02 2011 -0700

    Add utility function to compare LoadableIcon content.
    
    Unlike the various "equal()" functions for LoadableIcon and related classes,
    this function actually compares image content (which can be necessary when
    testing vs. images in the avatar cache, which may have a different path than an
    otherwise-identical image).
    
    Helps: bgo#655374 - Un-break Tracker avatar tests

 NEWS                           |    1 +
 tests/eds/add-persona.vala     |   24 ++++++----
 tests/eds/avatar-details.vala  |    8 ++-
 tests/eds/set-avatar.vala      |   13 ++++--
 tests/folks/avatar-cache.vala  |   53 ++++++-----------------
 tests/lib/Makefile.am          |    7 ++-
 tests/lib/test-utils.vala      |   94 ++++++++++++++++++++++++++++++++++++++++
 tests/tracker/add-persona.vala |   23 +++++++---
 tests/tracker/set-avatar.vala  |   19 +++++---
 9 files changed, 168 insertions(+), 74 deletions(-)
---
diff --git a/NEWS b/NEWS
index 2787c10..097dc1b 100644
--- a/NEWS
+++ b/NEWS
@@ -3,6 +3,7 @@ Overview of changes from libfolks 0.6.0 to libfolks 0.6.1
 
 Bugs fixed:
 * Bug 656659 â Use vcards for postal addresses
+* Bug 655374 â Un-break avatar tests
 
 Overview of changes from libfolks 0.5.2 to libfolks 0.5.3
 =========================================================
diff --git a/tests/eds/add-persona.vala b/tests/eds/add-persona.vala
index d635c8b..b813aae 100644
--- a/tests/eds/add-persona.vala
+++ b/tests/eds/add-persona.vala
@@ -342,16 +342,6 @@ public class AddPersonaTests : Folks.TestCase
             }
         }
 
-      if (i.avatar != null)
-        {
-          var b = new FileIcon (File.new_for_path (this._avatar_path));
-
-          if (b.equal (i.avatar) == true)
-            {
-              this._properties_found.replace ("avatar", true);
-            }
-        }
-
       foreach (var proto in i.im_addresses.get_keys ())
         {
           var im_fds = i.im_addresses.get (proto);
@@ -397,6 +387,20 @@ public class AddPersonaTests : Folks.TestCase
             }
         }
 
+      if (i.avatar != null)
+        {
+          var b = new FileIcon (File.new_for_path (this._avatar_path));
+
+          TestUtils.loadable_icons_content_equal.begin (b, i.avatar, -1,
+              (obj, result) =>
+            {
+              if (TestUtils.loadable_icons_content_equal.end (result))
+                this._properties_found.replace ("avatar", true);
+
+              this._exit_if_all_properties_found ();
+            });
+        }
+
       this._exit_if_all_properties_found ();
     }
 
diff --git a/tests/eds/avatar-details.vala b/tests/eds/avatar-details.vala
index ae55c48..9011104 100644
--- a/tests/eds/avatar-details.vala
+++ b/tests/eds/avatar-details.vala
@@ -119,11 +119,13 @@ public class AvatarDetailsTests : Folks.TestCase
             {
               var b = new FileIcon (File.new_for_path (this._avatar_path));
 
-              if (b.equal (i.avatar) == true)
+              Utils.loadable_icons_content_equal (b, i.avatar, -1,
+                  (object, result) =>
                 {
-                  this._avatars_are_equal = true;
+                  this._avatars_are_equal =
+                      Utils.loadable_icons_content_equal.end (result);
                   this._main_loop.quit ();
-                }
+                });
             }
         }
    }
diff --git a/tests/eds/set-avatar.vala b/tests/eds/set-avatar.vala
index 5f17f0c..afa493d 100644
--- a/tests/eds/set-avatar.vala
+++ b/tests/eds/set-avatar.vala
@@ -132,11 +132,16 @@ public class SetAvatarTests : Folks.TestCase
       var name = (Folks.NameDetails) i;
       if (name.full_name == "bernie h. innocenti")
         {
-          if (this._avatar.equal (i.avatar) == true)
+          TestUtils.loadable_icons_content_equal.begin (i.avatar,
+              this._avatar, -1,
+              (obj, result) =>
             {
-              this._found_after_update = true;
-              this._main_loop.quit ();
-            }
+              if (TestUtils.loadable_icons_content_equal.end (result))
+                {
+                  this._found_after_update = true;
+                  this._main_loop.quit ();
+                }
+            });
         }
     }
 }
diff --git a/tests/folks/avatar-cache.vala b/tests/folks/avatar-cache.vala
index f9d0062..9d0684c 100644
--- a/tests/folks/avatar-cache.vala
+++ b/tests/folks/avatar-cache.vala
@@ -113,44 +113,6 @@ public class AvatarCacheTests : Folks.TestCase
         }
     }
 
-  protected bool _avatars_equal (LoadableIcon avatar1,
-      LoadableIcon avatar2)
-    {
-      if (avatar1.equal (avatar2) == true)
-        {
-          return true;
-        }
-
-      // Compare content instead.
-      try
-        {
-          var stream1 = avatar1.load (-1, null, null);
-          var stream2 = avatar2.load (-1, null, null);
-
-          var content1 = new uint8[512];
-          var content2 = new uint8[512];
-
-          ssize_t read1 = -1;
-          do
-            {
-              read1 = stream1.read (content1, null);
-              var read2 = stream2.read (content2, null);
-
-              if (read1 != read2 || Memory.cmp (content1, content2, read1) != 0)
-                {
-                  return false;
-                }
-            }
-          while (read1 > 0);
-        }
-      catch (GLib.Error e)
-        {
-          return false;
-        }
-
-      return true;
-    }
-
   protected void _assert_store_avatar (string id, LoadableIcon avatar)
     {
       this._cache.store_avatar.begin (id, avatar, (obj, res) =>
@@ -212,6 +174,17 @@ public class AvatarCacheTests : Folks.TestCase
       this._main_loop.run ();
     }
 
+  protected void _assert_avatars_equal (LoadableIcon a, LoadableIcon b)
+    {
+      TestUtils.loadable_icons_content_equal (a, b, -1, (object, result) =>
+        {
+          assert (TestUtils.loadable_icons_content_equal.end (result));
+          this._main_loop.quit ();
+        });
+
+      this._main_loop.run ();
+    }
+
   public void test_store_and_load_avatar ()
     {
       // Store the avatar.
@@ -223,7 +196,7 @@ public class AvatarCacheTests : Folks.TestCase
       // Check the avatar's OK
       assert (avatar != null);
       assert (avatar is LoadableIcon);
-      assert (this._avatars_equal (this._avatar, avatar) == true);
+      this._assert_avatars_equal (this._avatar, avatar);
     }
 
   public void test_store_avatar_overwrite ()
@@ -238,7 +211,7 @@ public class AvatarCacheTests : Folks.TestCase
       // Check the avatar's OK
       assert (avatar != null);
       assert (avatar is LoadableIcon);
-      assert (this._avatars_equal (this._avatar, avatar) == true);
+      this._assert_avatars_equal (this._avatar, avatar);
     }
 
   public void test_load_avatar_non_existent ()
diff --git a/tests/lib/Makefile.am b/tests/lib/Makefile.am
index 5f7a4dd..eed9207 100644
--- a/tests/lib/Makefile.am
+++ b/tests/lib/Makefile.am
@@ -25,13 +25,14 @@ DIST_SUBDIRS = \
 
 noinst_LTLIBRARIES = libfolks-test.la
 
-libfolks_test_la_SOURCES = test-case.vala
-libfolks_test_la_CFLAGS = $(GLIB_CFLAGS)
-libfolks_test_la_LIBADD = $(GLIB_LIBS)
+libfolks_test_la_SOURCES = test-case.vala test-utils.vala
+libfolks_test_la_CFLAGS = $(GLIB_CFLAGS) $(GIO_CFLAGS)
+libfolks_test_la_LIBADD = $(GLIB_LIBS) $(GIO_LIBS)
 
 libfolks_test_la_VALAFLAGS = \
 	$(VALAFLAGS) \
 	--pkg gobject-2.0 \
+	--pkg gio-2.0 \
 	--library folks-test \
 	-H folks-test.h \
 	$(NULL)
diff --git a/tests/lib/test-utils.vala b/tests/lib/test-utils.vala
new file mode 100644
index 0000000..0318f86
--- /dev/null
+++ b/tests/lib/test-utils.vala
@@ -0,0 +1,94 @@
+/*
+ * Copyright (C) 2011 Collabora Ltd.
+ *
+ * This library is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation, either version 2.1 of the License, or
+ * (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this library.  If not, see <http://www.gnu.org/licenses/>.
+ *
+ * Authors: Travis Reitter <travis reitter collabora com>
+ *
+ */
+
+using GLib;
+
+public class Folks.TestUtils
+{
+  /**
+   * Compare the content of two { link LoadableIcon}s for equality.
+   *
+   * This is in contrast to { link Icon.equal}, which returns `false` for
+   * identical icons stored in different subclasses or in different storage
+   * locations.
+   *
+   * This function is particularly useful for tests which compare an avatar set
+   * on an { link AvatarDetails}'s with that object's final avatar, since it may
+   * be stored in a location in the avatar cache.
+   *
+   * @param a the first icon
+   * @param b the second icon
+   * @param size the size at which to compare the icons
+   * @return `true` if the instances are equal, `false` otherwise
+   */
+  public static async bool loadable_icons_content_equal (LoadableIcon a,
+      LoadableIcon b,
+      int icon_size)
+    {
+      bool retval = false;
+
+      if (a == b)
+        return true;
+
+      if (a != null && b != null)
+        {
+          try
+            {
+              string a_type;
+              var a_input = yield a.load_async (icon_size, null, out a_type);
+              string b_type;
+              var b_input = yield b.load_async (icon_size, null, out b_type);
+
+              /* arbitrary value */
+              size_t bufsize = 2048;
+              var a_data = new uint8[bufsize];
+              var b_data = new uint8[bufsize];
+
+              /* assume equal until proven otherwise */
+              retval = true;
+              size_t a_read_size = -1;
+              do
+                {
+                  a_read_size = yield a_input.read_async (a_data);
+                  var b_read_size = yield b_input.read_async (b_data);
+
+                  if (a_read_size != b_read_size ||
+                      Memory.cmp (a_data, b_data, a_read_size) != 0)
+                    {
+                      retval = false;
+                      break;
+                    }
+                }
+              while (a_read_size > 0);
+
+              yield a_input.close_async ();
+              yield b_input.close_async ();
+            }
+          catch (GLib.Error e1)
+            {
+              retval = false;
+              warning ("Failed to read loadable icon for comparison: %s",
+                  e1.message);
+            }
+        }
+
+      return retval;
+    }
+}
diff --git a/tests/tracker/add-persona.vala b/tests/tracker/add-persona.vala
index f814935..84d1edf 100644
--- a/tests/tracker/add-persona.vala
+++ b/tests/tracker/add-persona.vala
@@ -35,7 +35,6 @@ public class AddPersonaTests : Folks.TestCase
   private HashTable<string, bool> _properties_found;
   private string _persona_iid;
   private LoadableIcon _avatar;
-  private string _file_uri;
   private string _birthday;
   private DateTime _bday;
   private string _email_1;
@@ -86,8 +85,8 @@ public class AddPersonaTests : Folks.TestCase
       this._family_name = "family";
       this._given_name = "given";
       this._persona_iid = "";
-      this._file_uri = "file:///tmp/some-avatar.jpg";
-      this._avatar = new FileIcon (File.new_for_uri (this._file_uri));
+      var _avatar_path = Environment.get_variable ("AVATAR_FILE_PATH");
+      this._avatar = new FileIcon (File.new_for_path (_avatar_path));
       this._birthday = "2001-10-26T20:32:52Z";
       this._email_1 = "someone-1 example org";
       this._email_2 = "someone-2 example org";
@@ -423,10 +422,6 @@ public class AddPersonaTests : Folks.TestCase
           this._check_sname (i.structured_name);
         }
 
-      if (i.avatar != null &&
-          i.avatar.equal (this._avatar))
-        this._properties_found.replace ("avatar", true);
-
       if (i.birthday != null &&
           i.birthday.compare (this._bday) == 0)
         this._properties_found.replace ("birthday", true);
@@ -502,6 +497,20 @@ public class AddPersonaTests : Folks.TestCase
             this._properties_found.replace ("url-2", true);
         }
 
+      if (i.avatar != null)
+        {
+          /* arbitrary icon size, but it might as well be on the small side */
+          TestUtils.loadable_icons_content_equal.begin (i.avatar,
+              this._avatar, 100,
+              (obj, result) =>
+            {
+              if (TestUtils.loadable_icons_content_equal.end (result))
+                this._properties_found.replace ("avatar", true);
+
+              this._exit_if_all_properties_found ();
+            });
+        }
+
       this._exit_if_all_properties_found ();
     }
 
diff --git a/tests/tracker/set-avatar.vala b/tests/tracker/set-avatar.vala
index 0bd9b4d..81b6b57 100644
--- a/tests/tracker/set-avatar.vala
+++ b/tests/tracker/set-avatar.vala
@@ -29,7 +29,6 @@ public class SetAvatarTests : Folks.TestCase
   private TrackerTest.Backend _tracker_backend;
   private IndividualAggregator _aggregator;
   private string _persona_fullname;
-  private string _avatar_uri;
   private LoadableIcon _avatar;
   private bool _avatar_found;
 
@@ -55,8 +54,8 @@ public class SetAvatarTests : Folks.TestCase
       this._main_loop = new GLib.MainLoop (null, false);
       Gee.HashMap<string, string> c1 = new Gee.HashMap<string, string> ();
       this._persona_fullname = "persona #1";
-      this._avatar_uri = "file:///tmp/some-avatar.jpg";
-      this._avatar = new FileIcon (File.new_for_uri (this._avatar_uri));
+      var _avatar_path = Environment.get_variable ("AVATAR_FILE_PATH");
+      this._avatar = new FileIcon (File.new_for_path (_avatar_path));
 
       c1.set (Trf.OntologyDefs.NCO_FULLNAME, this._persona_fullname);
       this._tracker_backend.add_contact (c1);
@@ -125,11 +124,17 @@ public class SetAvatarTests : Folks.TestCase
       Folks.Individual i = (Folks.Individual) individual_obj;
       if (i.full_name == this._persona_fullname)
         {
-          if (i.avatar.equal (this._avatar))
+          /* arbitrary icon size, but it might as well be on the small side */
+          TestUtils.loadable_icons_content_equal.begin (i.avatar,
+              this._avatar, 100,
+              (obj, result) =>
             {
-              this._avatar_found = true;
-              this._main_loop.quit ();
-            }
+              if (TestUtils.loadable_icons_content_equal.end (result))
+                {
+                  this._avatar_found = true;
+                  this._main_loop.quit ();
+                }
+            });
         }
    }
 }



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