[glib/wip/menus: 42/61] GMenuModel tests: add 'roundtrip' test



commit a85d04be53adffc9ecbe5fb2c44186d1ad7826e5
Author: Ryan Lortie <desrt desrt ca>
Date:   Sat Oct 22 15:22:42 2011 -0400

    GMenuModel tests: add 'roundtrip' test
    
    Create a random menu and publish it on DBus.  Subscribe to it with a
    proxy.  Make a lot of changes and make sure the proxy always ends up
    equal to the original.

 gio/tests/gmenumodel.c |   65 ++++++++++++++++++++++++++++++++++++++++++++++++
 1 files changed, 65 insertions(+), 0 deletions(-)
---
diff --git a/gio/tests/gmenumodel.c b/gio/tests/gmenumodel.c
index b8b5245..96e0c0e 100644
--- a/gio/tests/gmenumodel.c
+++ b/gio/tests/gmenumodel.c
@@ -548,6 +548,70 @@ test_random (void)
   g_rand_free (rand);
 }
 
+struct roundtrip_state
+{
+  RandomMenu *random;
+  GMenuProxy *proxy;
+  GMainLoop *loop;
+  GRand *rand;
+  gint success;
+  gint count;
+};
+
+static gboolean
+roundtrip_step (gpointer data)
+{
+  struct roundtrip_state *state = data;
+
+  if (check_menus_equal (G_MENU_MODEL (state->random), G_MENU_MODEL (state->proxy)))
+    {
+      state->success++;
+      state->count = 0;
+
+      if (state->success < 100)
+        random_menu_change (state->random, state->rand);
+      else
+        g_main_loop_quit (state->loop);
+    }
+  else if (state->count == 100)
+    {
+      assert_menus_equal (G_MENU_MODEL (state->random), G_MENU_MODEL (state->proxy));
+      g_assert_not_reached ();
+    }
+  else
+    state->count++;
+
+  return G_SOURCE_CONTINUE;
+}
+
+static void
+test_roundtrip (void)
+{
+  struct roundtrip_state state;
+  GDBusConnection *bus;
+
+  bus = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
+
+  state.rand = g_rand_new_with_seed (g_test_rand_int ());
+
+  state.random = random_menu_new (state.rand, TOP_ORDER);
+  g_menu_exporter_export (bus, "/", G_MENU_MODEL (state.random), NULL);
+  state.proxy = g_menu_proxy_get (bus, g_dbus_connection_get_unique_name (bus), "/");
+  state.count = 0;
+  state.success = 0;
+
+  g_timeout_add (10, roundtrip_step, &state);
+
+  state.loop = g_main_loop_new (NULL, FALSE);
+  g_main_loop_run (state.loop);
+
+  g_main_loop_unref (state.loop);
+  g_object_unref (state.proxy);
+  g_menu_exporter_stop (G_MENU_MODEL (state.random));
+  g_object_unref (state.random);
+  g_object_unref (bus);
+}
+
 /* Epilogue {{{1 */
 int
 main (int argc, char **argv)
@@ -558,6 +622,7 @@ main (int argc, char **argv)
 
   g_test_add_func ("/gmenu/equality", test_equality);
   g_test_add_func ("/gmenu/random", test_random);
+  g_test_add_func ("/gmenu/roundtrip", test_roundtrip);
 
   return g_test_run ();
 }



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