[glib/wip/gapplication] Add a test for action changes



commit e5ad9e6e376aba3b15d5083524701d84ecab2d4d
Author: Matthias Clasen <mclasen redhat com>
Date:   Thu May 20 23:30:13 2010 -0400

    Add a test for action changes

 gio/tests/testapp.c  |   10 ++++++
 gio/tests/testapps.c |   82 ++++++++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 89 insertions(+), 3 deletions(-)
---
diff --git a/gio/tests/testapp.c b/gio/tests/testapp.c
index 67927a0..4a26f50 100644
--- a/gio/tests/testapp.c
+++ b/gio/tests/testapp.c
@@ -3,6 +3,8 @@
 #include <gstdio.h>
 #include <string.h>
 
+static gboolean action3_added = FALSE;
+
 static void
 on_app_action (GApplication *application,
                const gchar  *action_name,
@@ -10,6 +12,14 @@ on_app_action (GApplication *application,
 {
   if (strcmp (action_name, "action1") == 0)
     exit (1);
+  else  if (strcmp (action_name, "action2") == 0)
+    {
+      if (action3_added)
+        g_application_remove_action (application, "action3");
+      else
+        g_application_add_action (application, "action3", "An extra action");
+      action3_added = !action3_added;
+    }
 }
 
 static gboolean
diff --git a/gio/tests/testapps.c b/gio/tests/testapps.c
index a31b4f9..30935be 100644
--- a/gio/tests/testapps.c
+++ b/gio/tests/testapps.c
@@ -5,6 +5,7 @@
 
 static gint appeared;
 static gint disappeared;
+static gint changed;
 static gboolean died;
 static gboolean timed_out;
 GPid pid;
@@ -303,8 +304,9 @@ test_list_actions (void)
 }
 
 static gboolean
-invoke_action1 (gpointer data)
+invoke_action (gpointer data)
 {
+  const gchar *action = data;
   GDBusConnection *connection;
   GVariant *res;
 
@@ -315,7 +317,7 @@ invoke_action1 (gpointer data)
                                      "org.freedesktop.Application",
                                      "InvokeAction",
                                      g_variant_new ("(su)",
-                                                    "action1",
+                                                    action,
                                                     0),
                                      G_DBUS_CALL_FLAGS_NONE,
                                      -1,
@@ -373,7 +375,7 @@ test_invoke (void)
 
   id1 = g_child_watch_add (pid, exit_with_code_1, loop);
 
-  id2 = g_timeout_add (500, invoke_action1, NULL);
+  id2 = g_timeout_add (500, invoke_action, "action1");
 
   id3 = g_timeout_add (5000, timeout, loop);
 
@@ -436,6 +438,79 @@ test_remote (void)
   kill (pid2, SIGTERM);
 }
 
+static void
+actions_changed (GDBusConnection *connection,
+                 const gchar     *sender_name,
+                 const gchar     *object_path,
+                 const gchar     *interface_name,
+                 const gchar     *signal_name,
+                 GVariant        *parameters,
+                 gpointer         user_data)
+{
+  GMainLoop *loop = user_data;
+
+  g_assert_cmpstr (interface_name, ==, "org.freedesktop.Application");
+  g_assert_cmpstr (signal_name, ==, "ActionsChanged");
+
+  changed++;
+
+  g_main_loop_quit (loop);
+}
+
+static void
+test_change_action (void)
+{
+  GMainLoop *loop;
+  gint watch;
+  guint id;
+  GPid pid1;
+  gchar *argv[] = { "./testapp", NULL, NULL };
+  GDBusConnection *connection;
+
+  appeared = 0;
+  changed = 0;
+  timed_out = FALSE;
+
+  loop = g_main_loop_new (NULL, FALSE);
+  g_timeout_add (5000, timeout, loop);
+
+  watch = g_bus_watch_name (G_BUS_TYPE_SESSION,
+                            "org.gtk.test.app",
+                            0,
+                            name_appeared,
+                            NULL,
+                            loop,
+                            NULL);
+
+  g_assert (g_spawn_async (NULL, argv, NULL, G_SPAWN_DO_NOT_REAP_CHILD, NULL, NULL, &pid1, NULL));
+  g_main_loop_run (loop);
+
+  g_assert_cmpint (appeared, ==, 1);
+
+  connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
+  id = g_dbus_connection_signal_subscribe (connection,
+                                           NULL,
+                                           "org.freedesktop.Application",
+                                           "ActionsChanged",
+                                           "/org/freedesktop/Application",
+                                           NULL,
+                                           actions_changed,
+                                           loop,
+                                           NULL);
+
+  g_timeout_add (1000, invoke_action, "action2");
+
+  g_main_loop_run (loop);
+
+  g_assert_cmpint (changed, >, 0);
+  g_assert_cmpint (timed_out, ==, FALSE);
+
+  g_dbus_connection_signal_unsubscribe (connection, id);
+  g_object_unref (connection);
+  g_main_loop_unref (loop);
+  g_bus_unwatch_name (watch);
+}
+
 int
 main (int argc, char *argv[])
 {
@@ -447,6 +522,7 @@ main (int argc, char *argv[])
   g_test_add_func ("/application/list-actions", test_list_actions);
   g_test_add_func ("/application/invoke", test_invoke);
   g_test_add_func ("/application/remote", test_remote);
+  g_test_add_func ("/application/change-action", test_change_action);
 
   return g_test_run ();
 }



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