[glib] Add tests for remote actions



commit 354ae1d61c0a03c245ab1c191780e470c2608679
Author: Matthias Clasen <mclasen redhat com>
Date:   Tue Dec 24 00:04:14 2013 -0500

    Add tests for remote actions
    
    This improves the GApplication test coverage.

 gio/tests/basic-application.c |  111 +++++++++++++++++++++++++++++++++++++++++
 gio/tests/gapplication.c      |   42 +++++++++++++++-
 2 files changed, 151 insertions(+), 2 deletions(-)
---
diff --git a/gio/tests/basic-application.c b/gio/tests/basic-application.c
index ea31888..d1f9857 100644
--- a/gio/tests/basic-application.c
+++ b/gio/tests/basic-application.c
@@ -2,6 +2,70 @@
 #include <string.h>
 
 static void
+new_activated (GSimpleAction *action,
+               GVariant      *parameter,
+               gpointer       user_data)
+{
+  GApplication *app = user_data;
+
+  g_application_activate (app);
+}
+
+static void
+quit_activated (GSimpleAction *action,
+                GVariant      *parameter,
+                gpointer       user_data)
+{
+  GApplication *app = user_data;
+
+  g_application_quit (app);
+}
+
+static void
+action1_activated (GSimpleAction *action,
+                   GVariant      *parameter,
+                   gpointer       user_data)
+{
+  g_print ("activate action1\n");
+}
+
+static void
+action2_activated (GSimpleAction *action,
+                   GVariant      *parameter,
+                   gpointer       user_data)
+{
+  GVariant *state;
+
+  state = g_action_get_state (G_ACTION (action));
+  g_action_change_state (G_ACTION (action), g_variant_new_boolean (!g_variant_get_boolean (state)));
+  g_print ("activate action2 %d\n", !g_variant_get_boolean (state));
+  g_variant_unref (state);
+}
+
+static void
+change_action2 (GSimpleAction *action,
+                GVariant      *state,
+                gpointer       user_data)
+{
+  g_print ("change action2 %d\n", g_variant_get_boolean (state));
+}
+
+static void
+startup (GApplication *app)
+{
+  static GActionEntry actions[] = {
+    { "new", new_activated, NULL, NULL, NULL },
+    { "quit", quit_activated, NULL, NULL, NULL },
+    { "action1", action1_activated, NULL, NULL, NULL },
+    { "action2", action2_activated, "b", "false", change_action2 }
+  };
+
+  g_action_map_add_action_entries (G_ACTION_MAP (app),
+                                   actions, G_N_ELEMENTS (actions),
+                                   app);
+}
+
+static void
 activate (GApplication *application)
 {
   g_application_hold (application);
@@ -134,6 +198,44 @@ command_line (GApplication            *application,
   return 0;
 }
 
+static gboolean
+action_cb (gpointer data)
+{
+  gchar **argv = data;
+  GApplication *app;
+  gchar **actions;
+  gint i;
+
+  if (g_strcmp0 (argv[1], "./actions") == 0)
+    {
+      app = g_application_get_default ();
+
+      if (g_strcmp0 (argv[2], "list") == 0)
+        {
+          g_print ("actions");
+          actions = g_action_group_list_actions (G_ACTION_GROUP (app));
+          for (i = 0; actions[i]; i++)
+            g_print (" %s", actions[i]);
+          g_print ("\n");
+          g_strfreev (actions);
+        }
+      else if (g_strcmp0 (argv[2], "activate") == 0)
+        {
+          g_action_group_activate_action (G_ACTION_GROUP (app),
+                                          "action1", NULL);
+        }
+      else if (g_strcmp0 (argv[2], "set-state") == 0)
+        {
+          g_action_group_change_action_state (G_ACTION_GROUP (app),
+                                              "action2",
+                                              g_variant_new_boolean (TRUE));
+        }
+    }
+  g_application_release (app);
+
+  return G_SOURCE_REMOVE;
+}
+
 int
 main (int argc, char **argv)
 {
@@ -145,6 +247,7 @@ main (int argc, char **argv)
                            (g_strcmp0 (argv[1], "./cmd") == 0
                              ? G_APPLICATION_HANDLES_COMMAND_LINE
                              : G_APPLICATION_HANDLES_OPEN));
+  g_signal_connect (app, "startup", G_CALLBACK (startup), NULL);
   g_signal_connect (app, "activate", G_CALLBACK (activate), NULL);
   g_signal_connect (app, "open", G_CALLBACK (open), NULL);
   g_signal_connect (app, "command-line", G_CALLBACK (command_line), NULL);
@@ -153,6 +256,14 @@ main (int argc, char **argv)
 #else
   g_application_set_inactivity_timeout (app, 1000);
 #endif
+
+  if (g_strcmp0 (argv[1], "./actions") == 0)
+    {
+      g_application_set_inactivity_timeout (app, 0);
+      g_application_hold (app);
+      g_idle_add (action_cb, argv);
+    }
+
   status = g_application_run (app, argc - 1, argv + 1);
 
   g_object_unref (app);
diff --git a/gio/tests/gapplication.c b/gio/tests/gapplication.c
index f72432d..41f70b5 100644
--- a/gio/tests/gapplication.c
+++ b/gio/tests/gapplication.c
@@ -234,6 +234,43 @@ test_remote_command_line (void)
   g_main_loop_unref (main_loop);
 }
 
+static void
+test_remote_actions (void)
+{
+  GDBusConnection *c;
+
+  g_assert (outstanding_watches == 0);
+
+  session_bus_up ();
+  c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
+
+  main_loop = g_main_loop_new (NULL, 0);
+
+  /* spawn the master */
+  spawn ("got ./cmd 0\n"
+         "activate action1\n"
+         "change action2 1\n"
+         "exit status: 0\n", NULL,
+         "./cmd", NULL);
+
+  spawn ("actions quit new action1 action2\n"
+         "exit status: 0\n", NULL,
+         "./actions", "list", NULL);
+
+  spawn ("exit status: 0\n", NULL,
+         "./actions", "activate", NULL);
+
+  spawn ("exit status: 0\n", NULL,
+         "./actions", "set-state", NULL);
+
+  g_main_loop_run (main_loop);
+
+  g_object_unref (c);
+  session_bus_down ();
+
+  g_main_loop_unref (main_loop);
+}
+
 #if 0
 /* Now that we register non-unique apps on the bus we need to fix the
  * following test not to assume that it's safe to create multiple instances
@@ -556,7 +593,7 @@ on_activate (GApplication *app)
 }
 
 static void
-test_actions (void)
+test_local_actions (void)
 {
   char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
   gchar *argv[] = { binpath, NULL };
@@ -639,7 +676,8 @@ main (int argc, char **argv)
   g_test_add_func ("/gapplication/properties", properties);
   g_test_add_func ("/gapplication/app-id", appid);
   g_test_add_func ("/gapplication/quit", test_quit);
-  g_test_add_func ("/gapplication/actions", test_actions);
+  g_test_add_func ("/gapplication/local-actions", test_local_actions);
+  g_test_add_func ("/gapplication/remote-actions", test_remote_actions);
   g_test_add_func ("/gapplication/local-command-line", test_local_command_line);
   g_test_add_func ("/gapplication/remote-command-line", test_remote_command_line);
 


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