Install all test data
[platform/upstream/glib.git] / gio / tests / gapplication.c
index a1faa5c..d970f75 100644 (file)
@@ -61,15 +61,18 @@ spawn (const gchar *expected_stdout,
   gchar **args;
   va_list ap;
   GPid pid;
+  GPollFD fd;
 
   va_start (ap, first_arg);
   array = g_ptr_array_new ();
-  g_ptr_array_add (array, g_strdup ("./basic-application"));
+  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);
+
   data = g_slice_new (ChildData);
   data->expected_stdout = expected_stdout;
 
@@ -81,6 +84,14 @@ spawn (const gchar *expected_stdout,
 
   g_child_watch_add (pid, child_quit, data);
   outstanding_watches++;
+
+  /* we block until the children write to stdout to make sure
+   * they have started, as they need to be executed in order;
+   * see https://bugzilla.gnome.org/show_bug.cgi?id=664627
+   */
+  fd.fd = data->stdout_pipe;
+  fd.events = G_IO_IN | G_IO_HUP | G_IO_ERR;
+  g_poll (&fd, 1, -1);
 }
 
 static void
@@ -96,31 +107,17 @@ basic (void)
   /* spawn the master */
   spawn ("activated\n"
          "open file:///a file:///b\n"
-         "cmdline '40 +' '2'\n"
          "exit status: 0\n",
          "./app", NULL);
 
-  /* make sure it becomes the master */
-  g_usleep (100000);
-
   /* send it some files */
   spawn ("exit status: 0\n",
          "./app", "/a", "/b", NULL);
 
-  /* make sure the commandline arrives after the files */
-  g_usleep (100000);
-
-  spawn ("40 + 2 = 42\n"
-         "exit status: 42\n",
-         "./cmd", "40 +", "2", NULL);
-
   g_main_loop_run (main_loop);
 
-  session_bus_down ();
-  _g_object_wait_for_single_ref_do (c);
   g_object_unref (c);
-
-  g_assert (g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL) == NULL);
+  session_bus_down ();
 }
 
 
@@ -250,16 +247,17 @@ properties (void)
   g_assert (registered);
   g_assert (!remote);
 
+  g_object_set (app,
+                "inactivity-timeout", 1000,
+                NULL);
+
   g_application_quit (G_APPLICATION (app));
 
+  g_object_unref (c);
   g_object_unref (app);
   g_free (id);
 
   session_bus_down ();
-  _g_object_wait_for_single_ref (c);
-  g_object_unref (c);
-
-  g_assert (g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL) == NULL);
 }
 
 static void
@@ -307,24 +305,60 @@ nodbus_activate (GApplication *app)
 {
   nodbus_activated = TRUE;
   g_application_hold (app);
+
+  g_assert (g_application_get_dbus_connection (app) == NULL);
+  g_assert (g_application_get_dbus_object_path (app) == NULL);
+
   g_idle_add (release_app, app);
 }
 
 static void
 test_nodbus (void)
 {
-  gchar *argv[] = { "./unimportant", NULL };
+  char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
+  gchar *argv[] = { binpath, NULL };
   GApplication *app;
 
-  app = g_application_new ("org.gtk.Unimportant",
-                           G_APPLICATION_FLAGS_NONE);
+  app = g_application_new ("org.gtk.Unimportant", G_APPLICATION_FLAGS_NONE);
   g_signal_connect (app, "activate", G_CALLBACK (nodbus_activate), NULL);
   g_application_run (app, 1, argv);
   g_object_unref (app);
 
   g_assert (nodbus_activated);
+  g_free (binpath);
 }
 
+static gboolean noappid_activated;
+
+static void
+noappid_activate (GApplication *app)
+{
+  noappid_activated = TRUE;
+  g_application_hold (app);
+
+  g_assert (g_application_get_flags (app) & G_APPLICATION_NON_UNIQUE);
+
+  g_idle_add (release_app, app);
+}
+
+/* test that no appid -> non-unique */
+static void
+test_noappid (void)
+{
+  char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
+  gchar *argv[] = { binpath, NULL };
+  GApplication *app;
+
+  app = g_application_new (NULL, G_APPLICATION_FLAGS_NONE);
+  g_signal_connect (app, "activate", G_CALLBACK (noappid_activate), NULL);
+  g_application_run (app, 1, argv);
+  g_object_unref (app);
+
+  g_assert (noappid_activated);
+  g_free (binpath);
+}
+
+
 static gboolean
 quit_app (gpointer user_data)
 {
@@ -339,6 +373,10 @@ quit_activate (GApplication *app)
 {
   quit_activated = TRUE;
   g_application_hold (app);
+
+  g_assert (g_application_get_dbus_connection (app) != NULL);
+  g_assert (g_application_get_dbus_object_path (app) != NULL);
+
   g_idle_add (quit_app, app);
 }
 
@@ -346,7 +384,8 @@ static void
 test_quit (void)
 {
   GDBusConnection *c;
-  gchar *argv[] = { "./unimportant", NULL };
+  char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
+  gchar *argv[] = { binpath, NULL };
   GApplication *app;
 
   session_bus_up ();
@@ -357,15 +396,12 @@ test_quit (void)
   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);
 
   session_bus_down ();
-
-  _g_object_wait_for_single_ref (c);
-  g_object_unref (c);
-
-  g_assert (g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL) == NULL);
+  g_free (binpath);
 }
 
 static void
@@ -392,6 +428,9 @@ 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));
@@ -404,39 +443,89 @@ on_activate (GApplication *app)
 static void
 test_actions (void)
 {
-  gchar *argv[] = { "./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);
   g_application_run (app, 1, argv);
   g_object_unref (app);
+  g_free (binpath);
+}
+
+typedef GApplication TestLocCmdApp;
+typedef GApplicationClass TestLocCmdAppClass;
+
+static GType test_loc_cmd_app_get_type (void);
+G_DEFINE_TYPE (TestLocCmdApp, test_loc_cmd_app, G_TYPE_APPLICATION)
+
+static void
+test_loc_cmd_app_init (TestLocCmdApp *app)
+{
+}
+
+static void
+test_loc_cmd_app_startup (GApplication *app)
+{
+  g_assert_not_reached ();
+}
+
+static void
+test_loc_cmd_app_shutdown (GApplication *app)
+{
+  g_assert_not_reached ();
+}
+
+static gboolean
+test_loc_cmd_app_local_command_line (GApplication   *application,
+                                     gchar        ***arguments,
+                                     gint           *exit_status)
+{
+  return TRUE;
+}
+
+static void
+test_loc_cmd_app_class_init (TestLocCmdAppClass *klass)
+{
+  G_APPLICATION_CLASS (klass)->startup = test_loc_cmd_app_startup;
+  G_APPLICATION_CLASS (klass)->shutdown = test_loc_cmd_app_shutdown;
+  G_APPLICATION_CLASS (klass)->local_command_line = test_loc_cmd_app_local_command_line;
+}
+
+static void
+test_local_command_line (void)
+{
+  char *binpath = g_test_build_filename (G_TEST_BUILT, "unimportant", NULL);
+  gchar *argv[] = { binpath, "-invalid", NULL };
+  GApplication *app;
+
+  app = g_object_new (test_loc_cmd_app_get_type (),
+                      "application-id", "org.gtk.Unimportant",
+                      "flags", G_APPLICATION_FLAGS_NONE,
+                      NULL);
+  g_application_run (app, 1, argv);
+  g_object_unref (app);
+  g_free (binpath);
 }
 
 int
 main (int argc, char **argv)
 {
-  g_type_init ();
-
   g_test_init (&argc, &argv, NULL);
 
-  /* all the tests use a session bus with a well-known address
-   * that we can bring up and down using session_bus_up() and
-   * session_bus_down().
-   */
-  g_unsetenv ("DISPLAY");
-  g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE);
+  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/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-command-line", test_local_command_line);
 
   return g_test_run ();
 }