2 * Copyright © 2013 Canonical Limited
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published
6 * by the Free Software Foundation; either version 2 of the licence or (at
7 * your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General
15 * Public License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17 * Boston, MA 02111-1307, USA.
19 * Authors: Ryan Lortie <desrt@desrt.ca>
23 #include <gio/gdesktopappinfo.h>
25 #include "gdbus-sessionbus.h"
27 static GDesktopAppInfo *appinfo;
28 static int current_state;
29 static gboolean saw_startup_id;
30 static gboolean requested_startup_id;
33 static GType test_app_launch_context_get_type (void);
34 typedef GAppLaunchContext TestAppLaunchContext;
35 typedef GAppLaunchContextClass TestAppLaunchContextClass;
36 G_DEFINE_TYPE (TestAppLaunchContext, test_app_launch_context, G_TYPE_APP_LAUNCH_CONTEXT)
39 test_app_launch_context_get_startup_notify_id (GAppLaunchContext *context,
43 requested_startup_id = TRUE;
44 return g_strdup ("expected startup id");
48 test_app_launch_context_init (TestAppLaunchContext *ctx)
53 test_app_launch_context_class_init (GAppLaunchContextClass *class)
55 class->get_startup_notify_id = test_app_launch_context_get_startup_notify_id;
58 static GType test_application_get_type (void);
59 typedef GApplication TestApplication;
60 typedef GApplicationClass TestApplicationClass;
61 G_DEFINE_TYPE (TestApplication, test_application, G_TYPE_APPLICATION)
64 saw_action (const gchar *action)
66 /* This is the main driver of the test. It's a bit of a state
69 * Each time some event arrives on the app, it calls here to report
70 * which event it was. The initial activation of the app is what
71 * starts everything in motion (starting from state 0). At each
72 * state, we assert that we receive the expected event, send the next
73 * event, then update the current_state variable so we do the correct
77 switch (current_state)
79 case 0: g_assert_cmpstr (action, ==, "activate");
81 /* Let's try another activation... */
82 g_app_info_launch (G_APP_INFO (appinfo), NULL, NULL, NULL);
83 current_state = 1; return; case 1: g_assert_cmpstr (action, ==, "activate");
86 /* Now let's try opening some files... */
90 files = g_list_prepend (NULL, g_file_new_for_uri ("file:///a/b"));
91 files = g_list_append (files, g_file_new_for_uri ("file:///c/d"));
92 g_app_info_launch (G_APP_INFO (appinfo), files, NULL, NULL);
93 g_list_free_full (files, g_object_unref);
95 current_state = 2; return; case 2: g_assert_cmpstr (action, ==, "open");
97 /* Now action activations... */
98 g_desktop_app_info_launch_action (appinfo, "frob", NULL);
99 current_state = 3; return; case 3: g_assert_cmpstr (action, ==, "frob");
101 g_desktop_app_info_launch_action (appinfo, "tweak", NULL);
102 current_state = 4; return; case 4: g_assert_cmpstr (action, ==, "tweak");
104 g_desktop_app_info_launch_action (appinfo, "twiddle", NULL);
105 current_state = 5; return; case 5: g_assert_cmpstr (action, ==, "twiddle");
107 /* Now launch the app with startup notification */
109 GAppLaunchContext *ctx;
111 g_assert (saw_startup_id == FALSE);
112 ctx = g_object_new (test_app_launch_context_get_type (), NULL);
113 g_app_info_launch (G_APP_INFO (appinfo), NULL, ctx, NULL);
114 g_assert (requested_startup_id);
115 requested_startup_id = FALSE;
116 g_object_unref (ctx);
118 current_state = 6; return; case 6: g_assert_cmpstr (action, ==, "activate"); g_assert (saw_startup_id);
119 saw_startup_id = FALSE;
121 /* Now do the same for an action */
123 GAppLaunchContext *ctx;
125 g_assert (saw_startup_id == FALSE);
126 ctx = g_object_new (test_app_launch_context_get_type (), NULL);
127 g_desktop_app_info_launch_action (appinfo, "frob", ctx);
128 g_assert (requested_startup_id);
129 requested_startup_id = FALSE;
130 g_object_unref (ctx);
132 current_state = 7; return; case 7: g_assert_cmpstr (action, ==, "frob"); g_assert (saw_startup_id);
133 saw_startup_id = FALSE;
136 g_desktop_app_info_launch_action (appinfo, "quit", NULL);
137 current_state = 8; return; case 8: g_assert_not_reached ();
142 test_application_frob (GSimpleAction *action,
146 g_assert (parameter == NULL);
151 test_application_tweak (GSimpleAction *action,
155 g_assert (parameter == NULL);
156 saw_action ("tweak");
160 test_application_twiddle (GSimpleAction *action,
164 g_assert (parameter == NULL);
165 saw_action ("twiddle");
169 test_application_quit (GSimpleAction *action,
173 GApplication *application = user_data;
175 g_application_quit (application);
178 static const GActionEntry app_actions[] = {
179 { "frob", test_application_frob },
180 { "tweak", test_application_tweak },
181 { "twiddle", test_application_twiddle },
182 { "quit", test_application_quit }
186 test_application_activate (GApplication *application)
188 /* Unbalanced, but that's OK because we will quit() */
189 g_application_hold (application);
191 saw_action ("activate");
195 test_application_open (GApplication *application,
202 g_assert_cmpstr (hint, ==, "");
204 g_assert_cmpint (n_files, ==, 2);
205 f = g_file_new_for_uri ("file:///a/b");
206 g_assert (g_file_equal (files[0], f));
208 f = g_file_new_for_uri ("file:///c/d");
209 g_assert (g_file_equal (files[1], f));
216 test_application_startup (GApplication *application)
218 G_APPLICATION_CLASS (test_application_parent_class)
219 ->startup (application);
221 g_action_map_add_action_entries (G_ACTION_MAP (application), app_actions, G_N_ELEMENTS (app_actions), application);
225 test_application_before_emit (GApplication *application,
226 GVariant *platform_data)
228 const gchar *startup_id;
230 g_assert (!saw_startup_id);
232 if (!g_variant_lookup (platform_data, "desktop-startup-id", "&s", &startup_id))
235 g_assert_cmpstr (startup_id, ==, "expected startup id");
236 saw_startup_id = TRUE;
240 test_application_init (TestApplication *app)
245 test_application_class_init (GApplicationClass *class)
247 class->before_emit = test_application_before_emit;
248 class->startup = test_application_startup;
249 class->activate = test_application_activate;
250 class->open = test_application_open;
254 test_dbus_appinfo (void)
256 const gchar *argv[] = { "myapp", NULL };
257 TestApplication *app;
260 appinfo = g_desktop_app_info_new_from_filename (g_test_build_filename (G_TEST_DIST,
261 "org.gtk.test.dbusappinfo.desktop",
263 g_assert (appinfo != NULL);
265 app = g_object_new (test_application_get_type (),
266 "application-id", "org.gtk.test.dbusappinfo",
267 "flags", G_APPLICATION_HANDLES_OPEN,
269 status = g_application_run (app, 1, (gchar **) argv);
271 g_assert_cmpint (status, ==, 0);
272 g_assert_cmpint (current_state, ==, 8);
274 g_object_unref (appinfo);
275 g_object_unref (app);
279 main (int argc, char **argv)
281 g_test_init (&argc, &argv, NULL);
283 g_test_add_func ("/appinfo/dbusappinfo", test_dbus_appinfo);
285 return session_bus_run ();