1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
|
/*
* Copyright © 2013 Canonical Limited
*
* SPDX-License-Identifier: LGPL-2.1-or-later
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
*
* Authors: Ryan Lortie <desrt@desrt.ca>
*/
#include <gio/gio.h>
#include <gio/gdesktopappinfo.h>
#include "gdbus-sessionbus.h"
static GDesktopAppInfo *appinfo;
static int current_state;
static gboolean saw_startup_id;
static gboolean requested_startup_id;
static GType test_app_launch_context_get_type (void);
typedef GAppLaunchContext TestAppLaunchContext;
typedef GAppLaunchContextClass TestAppLaunchContextClass;
G_DEFINE_TYPE (TestAppLaunchContext, test_app_launch_context, G_TYPE_APP_LAUNCH_CONTEXT)
static gchar *
test_app_launch_context_get_startup_notify_id (GAppLaunchContext *context,
GAppInfo *info,
GList *uris)
{
requested_startup_id = TRUE;
return g_strdup ("expected startup id");
}
static void
test_app_launch_context_init (TestAppLaunchContext *ctx)
{
}
static void
test_app_launch_context_class_init (GAppLaunchContextClass *class)
{
class->get_startup_notify_id = test_app_launch_context_get_startup_notify_id;
}
static GType test_application_get_type (void);
typedef GApplication TestApplication;
typedef GApplicationClass TestApplicationClass;
G_DEFINE_TYPE (TestApplication, test_application, G_TYPE_APPLICATION)
static void
saw_action (const gchar *action)
{
/* This is the main driver of the test. It's a bit of a state
* machine.
*
* Each time some event arrives on the app, it calls here to report
* which event it was. The initial activation of the app is what
* starts everything in motion (starting from state 0). At each
* state, we assert that we receive the expected event, send the next
* event, then update the current_state variable so we do the correct
* thing next time.
*/
switch (current_state)
{
case 0: g_assert_cmpstr (action, ==, "activate");
/* Let's try another activation... */
g_app_info_launch (G_APP_INFO (appinfo), NULL, NULL, NULL);
current_state = 1; return; case 1: g_assert_cmpstr (action, ==, "activate");
/* Now let's try opening some files... */
{
GList *files;
files = g_list_prepend (NULL, g_file_new_for_uri ("file:///a/b"));
files = g_list_append (files, g_file_new_for_uri ("file:///c/d"));
g_app_info_launch (G_APP_INFO (appinfo), files, NULL, NULL);
g_list_free_full (files, g_object_unref);
}
current_state = 2; return; case 2: g_assert_cmpstr (action, ==, "open");
/* Now action activations... */
g_desktop_app_info_launch_action (appinfo, "frob", NULL);
current_state = 3; return; case 3: g_assert_cmpstr (action, ==, "frob");
g_desktop_app_info_launch_action (appinfo, "tweak", NULL);
current_state = 4; return; case 4: g_assert_cmpstr (action, ==, "tweak");
g_desktop_app_info_launch_action (appinfo, "twiddle", NULL);
current_state = 5; return; case 5: g_assert_cmpstr (action, ==, "twiddle");
/* Now launch the app with startup notification */
{
GAppLaunchContext *ctx;
g_assert (saw_startup_id == FALSE);
ctx = g_object_new (test_app_launch_context_get_type (), NULL);
g_app_info_launch (G_APP_INFO (appinfo), NULL, ctx, NULL);
g_assert (requested_startup_id);
requested_startup_id = FALSE;
g_object_unref (ctx);
}
current_state = 6; return; case 6: g_assert_cmpstr (action, ==, "activate"); g_assert (saw_startup_id);
saw_startup_id = FALSE;
/* Now do the same for an action */
{
GAppLaunchContext *ctx;
g_assert (saw_startup_id == FALSE);
ctx = g_object_new (test_app_launch_context_get_type (), NULL);
g_desktop_app_info_launch_action (appinfo, "frob", ctx);
g_assert (requested_startup_id);
requested_startup_id = FALSE;
g_object_unref (ctx);
}
current_state = 7; return; case 7: g_assert_cmpstr (action, ==, "frob"); g_assert (saw_startup_id);
saw_startup_id = FALSE;
/* Now quit... */
g_desktop_app_info_launch_action (appinfo, "quit", NULL);
current_state = 8; return; case 8: g_assert_not_reached ();
}
}
static void
test_application_frob (GSimpleAction *action,
GVariant *parameter,
gpointer user_data)
{
g_assert (parameter == NULL);
saw_action ("frob");
}
static void
test_application_tweak (GSimpleAction *action,
GVariant *parameter,
gpointer user_data)
{
g_assert (parameter == NULL);
saw_action ("tweak");
}
static void
test_application_twiddle (GSimpleAction *action,
GVariant *parameter,
gpointer user_data)
{
g_assert (parameter == NULL);
saw_action ("twiddle");
}
static void
test_application_quit (GSimpleAction *action,
GVariant *parameter,
gpointer user_data)
{
GApplication *application = user_data;
g_application_quit (application);
}
static const GActionEntry app_actions[] = {
{ "frob", test_application_frob, NULL, NULL, NULL, { 0 } },
{ "tweak", test_application_tweak, NULL, NULL, NULL, { 0 } },
{ "twiddle", test_application_twiddle, NULL, NULL, NULL, { 0 } },
{ "quit", test_application_quit, NULL, NULL, NULL, { 0 } }
};
static void
test_application_activate (GApplication *application)
{
/* Unbalanced, but that's OK because we will quit() */
g_application_hold (application);
saw_action ("activate");
}
static void
test_application_open (GApplication *application,
GFile **files,
gint n_files,
const gchar *hint)
{
GFile *f;
g_assert_cmpstr (hint, ==, "");
g_assert_cmpint (n_files, ==, 2);
f = g_file_new_for_uri ("file:///a/b");
g_assert (g_file_equal (files[0], f));
g_object_unref (f);
f = g_file_new_for_uri ("file:///c/d");
g_assert (g_file_equal (files[1], f));
g_object_unref (f);
saw_action ("open");
}
static void
test_application_startup (GApplication *application)
{
G_APPLICATION_CLASS (test_application_parent_class)
->startup (application);
g_action_map_add_action_entries (G_ACTION_MAP (application), app_actions, G_N_ELEMENTS (app_actions), application);
}
static void
test_application_before_emit (GApplication *application,
GVariant *platform_data)
{
const gchar *startup_id;
gsize i;
g_assert (!saw_startup_id);
const gchar *startup_id_keys[] = {
"desktop-startup-id",
"activation-token",
NULL,
};
for (i = 0; startup_id_keys[i] != NULL; i++)
{
if (!g_variant_lookup (platform_data, startup_id_keys[i], "&s", &startup_id))
return;
g_assert_cmpstr (startup_id, ==, "expected startup id");
}
saw_startup_id = TRUE;
}
static void
test_application_init (TestApplication *app)
{
}
static void
test_application_class_init (GApplicationClass *class)
{
class->before_emit = test_application_before_emit;
class->startup = test_application_startup;
class->activate = test_application_activate;
class->open = test_application_open;
}
static void
test_dbus_appinfo (void)
{
const gchar *argv[] = { "myapp", NULL };
TestApplication *app;
int status;
gchar *desktop_file = NULL;
desktop_file = g_test_build_filename (G_TEST_DIST,
"org.gtk.test.dbusappinfo.desktop",
NULL);
appinfo = g_desktop_app_info_new_from_filename (desktop_file);
g_assert (appinfo != NULL);
g_free (desktop_file);
app = g_object_new (test_application_get_type (),
"application-id", "org.gtk.test.dbusappinfo",
"flags", G_APPLICATION_HANDLES_OPEN,
NULL);
status = g_application_run (app, 1, (gchar **) argv);
g_assert_cmpint (status, ==, 0);
g_assert_cmpint (current_state, ==, 8);
g_object_unref (appinfo);
g_object_unref (app);
}
static void
on_flatpak_launch_uris_finish (GObject *object,
GAsyncResult *result,
gpointer user_data)
{
GApplication *app = user_data;
GError *error = NULL;
g_app_info_launch_uris_finish (G_APP_INFO (object), result, &error);
g_assert_no_error (error);
g_application_release (app);
}
static void
on_flatpak_activate (GApplication *app,
gpointer user_data)
{
GDesktopAppInfo *flatpak_appinfo = user_data;
char *uri;
GList *uris;
/* The app will be released in on_flatpak_launch_uris_finish */
g_application_hold (app);
uri = g_filename_to_uri (g_desktop_app_info_get_filename (flatpak_appinfo), NULL, NULL);
g_assert_nonnull (uri);
uris = g_list_prepend (NULL, uri);
g_app_info_launch_uris_async (G_APP_INFO (flatpak_appinfo), uris, NULL,
NULL, on_flatpak_launch_uris_finish, app);
g_list_free (uris);
g_free (uri);
}
static void
on_flatpak_open (GApplication *app,
GFile **files,
gint n_files,
const char *hint)
{
GFile *f;
g_assert_cmpint (n_files, ==, 1);
g_test_message ("on_flatpak_open received file '%s'", g_file_peek_path (files[0]));
/* The file has been exported via the document portal */
f = g_file_new_for_uri ("file:///document-portal/document-id/org.gtk.test.dbusappinfo.flatpak.desktop");
g_assert_true (g_file_equal (files[0], f));
g_object_unref (f);
}
static void
test_flatpak_doc_export (void)
{
const gchar *argv[] = { "myapp", NULL };
gchar *desktop_file = NULL;
GDesktopAppInfo *flatpak_appinfo;
GApplication *app;
int status;
g_test_summary ("Test that files launched via Flatpak apps are made available via the document portal.");
desktop_file = g_test_build_filename (G_TEST_DIST,
"org.gtk.test.dbusappinfo.flatpak.desktop",
NULL);
flatpak_appinfo = g_desktop_app_info_new_from_filename (desktop_file);
g_assert_nonnull (flatpak_appinfo);
g_free (desktop_file);
app = g_application_new ("org.gtk.test.dbusappinfo.flatpak",
G_APPLICATION_HANDLES_OPEN);
g_signal_connect (app, "activate", G_CALLBACK (on_flatpak_activate),
flatpak_appinfo);
g_signal_connect (app, "open", G_CALLBACK (on_flatpak_open), NULL);
status = g_application_run (app, 1, (gchar **) argv);
g_assert_cmpint (status, ==, 0);
g_object_unref (app);
g_object_unref (flatpak_appinfo);
}
static void
on_flatpak_launch_invalid_uri_finish (GObject *object,
GAsyncResult *result,
gpointer user_data)
{
GApplication *app = user_data;
GError *error = NULL;
g_app_info_launch_uris_finish (G_APP_INFO (object), result, &error);
g_assert_no_error (error);
g_application_release (app);
}
static void
on_flatpak_activate_invalid_uri (GApplication *app,
gpointer user_data)
{
GDesktopAppInfo *flatpak_appinfo = user_data;
GList *uris;
/* The app will be released in on_flatpak_launch_uris_finish */
g_application_hold (app);
uris = g_list_prepend (NULL, "file:///hopefully/an/invalid/path.desktop");
g_app_info_launch_uris_async (G_APP_INFO (flatpak_appinfo), uris, NULL,
NULL, on_flatpak_launch_invalid_uri_finish, app);
g_list_free (uris);
}
static void
on_flatpak_open_invalid_uri (GApplication *app,
GFile **files,
gint n_files,
const char *hint)
{
GFile *f;
g_assert_cmpint (n_files, ==, 1);
g_test_message ("on_flatpak_open received file '%s'", g_file_peek_path (files[0]));
/* The file has been exported via the document portal */
f = g_file_new_for_uri ("file:///hopefully/an/invalid/path.desktop");
g_assert_true (g_file_equal (files[0], f));
g_object_unref (f);
}
static void
test_flatpak_missing_doc_export (void)
{
const gchar *argv[] = { "myapp", NULL };
gchar *desktop_file = NULL;
GDesktopAppInfo *flatpak_appinfo;
GApplication *app;
int status;
g_test_summary ("Test that files launched via Flatpak apps are made available via the document portal.");
desktop_file = g_test_build_filename (G_TEST_DIST,
"org.gtk.test.dbusappinfo.flatpak.desktop",
NULL);
flatpak_appinfo = g_desktop_app_info_new_from_filename (desktop_file);
g_assert_nonnull (flatpak_appinfo);
app = g_application_new ("org.gtk.test.dbusappinfo.flatpak",
G_APPLICATION_HANDLES_OPEN);
g_signal_connect (app, "activate", G_CALLBACK (on_flatpak_activate_invalid_uri),
flatpak_appinfo);
g_signal_connect (app, "open", G_CALLBACK (on_flatpak_open_invalid_uri), NULL);
status = g_application_run (app, 1, (gchar **) argv);
g_assert_cmpint (status, ==, 0);
g_object_unref (app);
g_object_unref (flatpak_appinfo);
g_free (desktop_file);
}
int
main (int argc, char **argv)
{
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/appinfo/dbusappinfo", test_dbus_appinfo);
g_test_add_func ("/appinfo/flatpak-doc-export", test_flatpak_doc_export);
g_test_add_func ("/appinfo/flatpak-missing-doc-export", test_flatpak_missing_doc_export);
return session_bus_run ();
}
|