gapplication tests: test resource base path
[platform/upstream/glib.git] / gio / tests / gapplication.c
index 81d5892..249aa6e 100644 (file)
@@ -6,47 +6,70 @@
 #include "gdbus-tests.h"
 #include "gdbus-sessionbus.h"
 
-static const gchar *datapath;
-static const gchar *binpath;
-
+#if 0
+/* These tests are racy -- there is no guarantee about the order of data
+ * arriving over D-Bus.
+ *
+ * They're also a bit ridiculous -- GApplication was never meant to be
+ * abused in this way...
+ *
+ * We need new tests.
+ */
 static gint outstanding_watches;
 static GMainLoop *main_loop;
 
 typedef struct
 {
-  const gchar *expected_stdout;
+  gchar *expected_stdout;
   gint stdout_pipe;
+  gchar *expected_stderr;
+  gint stderr_pipe;
 } ChildData;
 
 static void
+check_data (gint fd, const gchar *expected)
+{
+  gssize len, actual;
+  gchar *buffer;
+  
+  len = strlen (expected);
+  buffer = g_alloca (len + 100);
+  actual = read (fd, buffer, len + 100);
+
+  g_assert_cmpint (actual, >=, 0);
+
+  if (actual != len ||
+      memcmp (buffer, expected, len) != 0)
+    {
+      buffer[MIN(len + 100, actual)] = '\0';
+
+      g_error ("\nExpected\n-----\n%s-----\nGot (%s)\n-----\n%s-----\n",
+               expected,
+               (actual > len) ? "truncated" : "full", buffer);
+    }
+}
+
+static void
 child_quit (GPid     pid,
             gint     status,
             gpointer data)
 {
   ChildData *child = data;
-  gssize expected, actual;
-  gchar *buffer;
 
   g_assert_cmpint (status, ==, 0);
 
   if (--outstanding_watches == 0)
     g_main_loop_quit (main_loop);
 
-  expected = strlen (child->expected_stdout);
-  buffer = g_alloca (expected + 100);
-  actual = read (child->stdout_pipe, buffer, expected + 100);
+  check_data (child->stdout_pipe, child->expected_stdout);
   close (child->stdout_pipe);
+  g_free (child->expected_stdout);
 
-  g_assert_cmpint (actual, >=, 0);
-
-  if (actual != expected ||
-      memcmp (buffer, child->expected_stdout, expected) != 0)
+  if (child->expected_stderr)
     {
-      buffer[MIN(expected + 100, actual)] = '\0';
-
-      g_error ("\nExpected\n-----\n%s-----\nGot (%s)\n-----\n%s-----\n",
-               child->expected_stdout,
-               (actual > expected) ? "truncated" : "full", buffer);
+      check_data (child->stderr_pipe, child->expected_stderr);
+      close (child->stderr_pipe);
+      g_free (child->expected_stderr);
     }
 
   g_slice_free (ChildData, child);
@@ -54,6 +77,7 @@ child_quit (GPid     pid,
 
 static void
 spawn (const gchar *expected_stdout,
+       const gchar *expected_stderr,
        const gchar *first_arg,
        ...)
 {
@@ -65,26 +89,33 @@ spawn (const gchar *expected_stdout,
   va_list ap;
   GPid pid;
   GPollFD fd;
+  gchar **env;
 
   va_start (ap, first_arg);
   array = g_ptr_array_new ();
-  g_ptr_array_add (array, g_build_filename (binpath, "basic-application", NULL));
+  g_ptr_array_add (array, g_test_build_filename (G_TEST_BUILT, "basic-application", NULL));
   for (arg = first_arg; arg; arg = va_arg (ap, const gchar *))
     g_ptr_array_add (array, g_strdup (arg));
   g_ptr_array_add (array, NULL);
   args = (gchar **) g_ptr_array_free (array, FALSE);
-
   va_end (ap);
 
+  env = g_environ_setenv (g_get_environ (), "TEST", "1", TRUE);
+
   data = g_slice_new (ChildData);
-  data->expected_stdout = expected_stdout;
+  data->expected_stdout = g_strdup (expected_stdout);
+  data->expected_stderr = g_strdup (expected_stderr);
 
-  g_spawn_async_with_pipes (NULL, args, NULL,
+  g_spawn_async_with_pipes (NULL, args, env,
                             G_SPAWN_DO_NOT_REAP_CHILD,
                             NULL, NULL, &pid, NULL,
-                            &data->stdout_pipe, NULL, &error);
+                            &data->stdout_pipe,
+                            expected_stderr ? &data->stderr_pipe : NULL,
+                            &error);
   g_assert_no_error (error);
 
+  g_strfreev (env);
+
   g_child_watch_add (pid, child_quit, data);
   outstanding_watches++;
 
@@ -102,6 +133,8 @@ basic (void)
 {
   GDBusConnection *c;
 
+  g_assert (outstanding_watches == 0);
+
   session_bus_up ();
   c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
 
@@ -110,24 +143,143 @@ basic (void)
   /* spawn the master */
   spawn ("activated\n"
          "open file:///a file:///b\n"
-         "cmdline '40 +' '2'\n"
-         "exit status: 0\n",
+         "exit status: 0\n", NULL,
          "./app", NULL);
 
   /* send it some files */
-  spawn ("exit status: 0\n",
+  spawn ("exit status: 0\n", NULL,
          "./app", "/a", "/b", NULL);
 
-  spawn ("40 + 2 = 42\n"
-         "exit status: 42\n",
-         "./cmd", "40 +", "2", NULL);
+  g_main_loop_run (main_loop);
+
+  g_object_unref (c);
+  session_bus_down ();
+
+  g_main_loop_unref (main_loop);
+}
+
+static void
+test_remote_command_line (void)
+{
+  GDBusConnection *c;
+  GFile *file;
+  gchar *replies;
+  gchar *cwd;
+
+  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);
+
+  file = g_file_new_for_commandline_arg ("foo");
+  cwd = g_get_current_dir ();
+
+  replies = g_strconcat ("got ./cmd 0\n",
+                         "got ./cmd 1\n",
+                         "cmdline ./cmd echo --abc -d\n",
+                         "environment TEST=1\n",
+                         "getenv TEST=1\n",
+                         "file ", g_file_get_path (file), "\n",
+                         "properties ok\n",
+                         "cwd ", cwd, "\n",
+                         "busy\n",
+                         "idle\n",
+                         "stdin ok\n",        
+                         "exit status: 0\n",
+                         NULL);
+  g_object_unref (file);
+
+  /* spawn the master */
+  spawn (replies, NULL,
+         "./cmd", NULL);
+
+  g_free (replies);
+
+  /* send it a few commandlines */
+  spawn ("exit status: 0\n", NULL,
+         "./cmd", NULL);
+
+  spawn ("exit status: 0\n", NULL,
+         "./cmd", "echo", "--abc", "-d", NULL);
+
+  spawn ("exit status: 0\n", NULL,
+         "./cmd", "env", NULL);
+
+  spawn ("exit status: 0\n", NULL,
+         "./cmd", "getenv", NULL);
+
+  spawn ("print test\n"
+         "exit status: 0\n", NULL,
+         "./cmd", "print", "test", NULL);
+
+  spawn ("exit status: 0\n", "printerr test\n",
+         "./cmd", "printerr", "test", NULL);
+
+  spawn ("exit status: 0\n", NULL,
+         "./cmd", "file", "foo", NULL);
+
+  spawn ("exit status: 0\n", NULL,
+         "./cmd", "properties", NULL);
+
+  spawn ("exit status: 0\n", NULL,
+         "./cmd", "cwd", NULL);
+
+  spawn ("exit status: 0\n", NULL,
+         "./cmd", "busy", NULL);
+
+  spawn ("exit status: 0\n", NULL,
+         "./cmd", "idle", NULL);
+
+  spawn ("exit status: 0\n", NULL,
+         "./cmd", "stdin", NULL);
 
   g_main_loop_run (main_loop);
 
   g_object_unref (c);
   session_bus_down ();
+
+  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);
+}
+#endif
 
 #if 0
 /* Now that we register non-unique apps on the bus we need to fix the
@@ -323,7 +475,7 @@ nodbus_activate (GApplication *app)
 static void
 test_nodbus (void)
 {
-  char *binpath = g_build_filename (datapath, "unimportant", NULL);
+  char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
   gchar *argv[] = { binpath, NULL };
   GApplication *app;
 
@@ -353,7 +505,7 @@ noappid_activate (GApplication *app)
 static void
 test_noappid (void)
 {
-  char *binpath = g_build_filename (datapath, "unimportant", NULL);
+  char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
   gchar *argv[] = { binpath, NULL };
   GApplication *app;
 
@@ -366,20 +518,21 @@ test_noappid (void)
   g_free (binpath);
 }
 
+static gboolean activated;
+static gboolean quitted;
 
 static gboolean
 quit_app (gpointer user_data)
 {
+  quitted = TRUE;
   g_application_quit (user_data);
   return G_SOURCE_REMOVE;
 }
 
-static gboolean quit_activated;
-
 static void
 quit_activate (GApplication *app)
 {
-  quit_activated = TRUE;
+  activated = TRUE;
   g_application_hold (app);
 
   g_assert (g_application_get_dbus_connection (app) != NULL);
@@ -392,7 +545,7 @@ static void
 test_quit (void)
 {
   GDBusConnection *c;
-  char *binpath = g_build_filename (datapath, "unimportant", NULL);
+  char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
   gchar *argv[] = { binpath, NULL };
   GApplication *app;
 
@@ -401,12 +554,15 @@ test_quit (void)
 
   app = g_application_new ("org.gtk.Unimportant",
                            G_APPLICATION_FLAGS_NONE);
+  activated = FALSE;
+  quitted = FALSE;
   g_signal_connect (app, "activate", G_CALLBACK (quit_activate), NULL);
   g_application_run (app, 1, argv);
   g_object_unref (app);
   g_object_unref (c);
 
-  g_assert (quit_activated);
+  g_assert (activated);
+  g_assert (quitted);
 
   session_bus_down ();
   g_free (binpath);
@@ -436,24 +592,23 @@ on_activate (GApplication *app)
   state = g_action_group_get_action_state (G_ACTION_GROUP (app), "test");
   g_assert (g_variant_get_boolean (state) == TRUE);
 
+  action = g_action_map_lookup_action (G_ACTION_MAP (app), "test");
+  g_assert (action != NULL);
+
   g_action_map_remove_action (G_ACTION_MAP (app), "test");
 
   actions = g_action_group_list_actions (G_ACTION_GROUP (app));
   g_assert (g_strv_length (actions) == 0);
   g_strfreev (actions);
-
-  g_idle_add (quit_app, app);
 }
 
 static void
-test_actions (void)
+test_local_actions (void)
 {
-  char *binpath = g_build_filename (datapath, "unimportant", NULL);
+  char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
   gchar *argv[] = { binpath, NULL };
   GApplication *app;
 
-  g_unsetenv ("DBUS_SESSION_BUS_ADDRESS");
-
   app = g_application_new ("org.gtk.Unimportant",
                            G_APPLICATION_FLAGS_NONE);
   g_signal_connect (app, "activate", G_CALLBACK (on_activate), NULL);
@@ -504,12 +659,10 @@ test_loc_cmd_app_class_init (TestLocCmdAppClass *klass)
 static void
 test_local_command_line (void)
 {
-  char *binpath = g_build_filename (datapath, "unimportant", NULL);
+  char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
   gchar *argv[] = { binpath, "-invalid", NULL };
   GApplication *app;
 
-  g_unsetenv ("DBUS_SESSION_BUS_ADDRESS");
-
   app = g_object_new (test_loc_cmd_app_get_type (),
                       "application-id", "org.gtk.Unimportant",
                       "flags", G_APPLICATION_FLAGS_NONE,
@@ -519,30 +672,58 @@ test_local_command_line (void)
   g_free (binpath);
 }
 
+static void
+test_resource_path (void)
+{
+  GApplication *app;
+
+  app = g_application_new ("x.y.z", 0);
+  g_assert_cmpstr (g_application_get_resource_base_path (app), ==, "/x/y/z");
+
+  /* this should not change anything */
+  g_application_set_application_id (app, "a.b.c");
+  g_assert_cmpstr (g_application_get_resource_base_path (app), ==, "/x/y/z");
+
+  /* but this should... */
+  g_application_set_resource_base_path (app, "/x");
+  g_assert_cmpstr (g_application_get_resource_base_path (app), ==, "/x");
+
+  /* ... and this */
+  g_application_set_resource_base_path (app, NULL);
+  g_assert_cmpstr (g_application_get_resource_base_path (app), ==, NULL);
+
+  g_object_unref (app);
+
+  /* Make sure that overriding at construction time works properly */
+  app = g_object_new (G_TYPE_APPLICATION, "application-id", "x.y.z", "resource-base-path", "/a", NULL);
+  g_assert_cmpstr (g_application_get_resource_base_path (app), ==, "/a");
+  g_object_unref (app);
+
+  /* ... particularly if we override to NULL */
+  app = g_object_new (G_TYPE_APPLICATION, "application-id", "x.y.z", "resource-base-path", NULL, NULL);
+  g_assert_cmpstr (g_application_get_resource_base_path (app), ==, NULL);
+  g_object_unref (app);
+}
+
 int
 main (int argc, char **argv)
 {
-  if (g_getenv ("G_TEST_DATA"))
-    datapath = binpath = g_getenv ("G_TEST_DATA");
-  else
-    {
-      datapath = SRCDIR;
-      binpath = BUILDDIR;
-    }
-
   g_test_init (&argc, &argv, NULL);
 
   g_test_dbus_unset ();
 
   g_test_add_func ("/gapplication/no-dbus", test_nodbus);
-  g_test_add_func ("/gapplication/basic", basic);
+/*  g_test_add_func ("/gapplication/basic", basic); */
   g_test_add_func ("/gapplication/no-appid", test_noappid);
 /*  g_test_add_func ("/gapplication/non-unique", test_nonunique); */
   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); */
+  g_test_add_func ("/gapplication/resource-path", test_resource_path);
 
   return g_test_run ();
 }