Debian Codegraph @

glib/gio/tests/appinfo.c

raw
1
2 #include <locale.h>
3 #include <string.h>
4
5 #include <glib/gstdio.h>
6 #include <gio/gio.h>
7 #include <gio/gdesktopappinfo.h>
8
9 /* Should be called inside a #GTestDBus environment. */
10 static void
11 test_launch_for_app_info (GAppInfo *appinfo)
12 {
13 GError *error = NULL;
14 gboolean success;
15 GFile *file;
16 GList *l;
17 const gchar *path;
18 gchar *uri;
19
20 success = g_app_info_launch (appinfo, NULL, NULL, &error);
21 g_assert_no_error (error);
22 g_assert_true (success);
23
24 success = g_app_info_launch_uris (appinfo, NULL, NULL, &error);
25 g_assert_no_error (error);
26 g_assert_true (success);
27
28 path = g_test_get_filename (G_TEST_BUILT, "appinfo-test.desktop", NULL);
29 file = g_file_new_for_path (path);
30 l = NULL;
31 l = g_list_append (l, file);
32
33 success = g_app_info_launch (appinfo, l, NULL, &error);
34 g_assert_no_error (error);
35 g_assert_true (success);
36 g_list_free (l);
37 g_object_unref (file);
38
39 l = NULL;
40 uri = g_strconcat ("file://", g_test_get_dir (G_TEST_BUILT), "/appinfo-test.desktop", NULL);
41 l = g_list_append (l, uri);
42 l = g_list_append (l, "file:///etc/group#adm");
43
44 success = g_app_info_launch_uris (appinfo, l, NULL, &error);
45 g_assert_no_error (error);
46 g_assert_true (success);
47
48 g_list_free (l);
49 g_free (uri);
50 }
51
52 static void
53 test_launch (void)
54 {
55 GTestDBus *bus = NULL;
56 GAppInfo *appinfo;
57 const gchar *path;
58
59 /* Set up a test session bus to keep D-Bus traffic off the real session bus. */
60 bus = g_test_dbus_new (G_TEST_DBUS_NONE);
61 g_test_dbus_up (bus);
62
63 path = g_test_get_filename (G_TEST_BUILT, "appinfo-test.desktop", NULL);
64 appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (path);
65 g_assert_true (G_IS_APP_INFO (appinfo));
66
67 test_launch_for_app_info (appinfo);
68 g_object_unref (appinfo);
69
70 g_test_dbus_down (bus);
71 g_clear_object (&bus);
72 }
73
74 static void
75 test_launch_no_app_id (void)
76 {
77 const gchar desktop_file_base_contents[] =
78 "[Desktop Entry]\n"
79 "Type=Application\n"
80 "GenericName=generic-appinfo-test\n"
81 "Name=appinfo-test\n"
82 "Name[de]=appinfo-test-de\n"
83 "X-GNOME-FullName=example\n"
84 "X-GNOME-FullName[de]=Beispiel\n"
85 "Comment=GAppInfo example\n"
86 "Comment[de]=GAppInfo Beispiel\n"
87 "Icon=testicon.svg\n"
88 "Terminal=false\n"
89 "StartupNotify=true\n"
90 "StartupWMClass=appinfo-class\n"
91 "MimeType=image/png;image/jpeg;\n"
92 "Keywords=keyword1;test keyword;\n"
93 "Categories=GNOME;GTK;\n";
94
95 GTestDBus *bus = NULL;
96 gchar *exec_line_variants[2];
97 gsize i;
98
99 exec_line_variants[0] = g_strdup_printf (
100 "Exec=%s/appinfo-test --option %%U %%i --name %%c --filename %%k %%m %%%%",
101 g_test_get_dir (G_TEST_BUILT));
102 exec_line_variants[1] = g_strdup_printf (
103 "Exec=%s/appinfo-test --option %%u %%i --name %%c --filename %%k %%m %%%%",
104 g_test_get_dir (G_TEST_BUILT));
105
106 g_test_bug ("https://bugzilla.gnome.org/show_bug.cgi?id=791337");
107
108 /* Set up a test session bus to keep D-Bus traffic off the real session bus. */
109 bus = g_test_dbus_new (G_TEST_DBUS_NONE);
110 g_test_dbus_up (bus);
111
112 for (i = 0; i < G_N_ELEMENTS (exec_line_variants); i++)
113 {
114 gchar *desktop_file_contents;
115 GKeyFile *fake_desktop_file;
116 GAppInfo *appinfo;
117 gboolean loaded;
118
119 g_test_message ("Exec line variant #%" G_GSIZE_FORMAT, i);
120
121 desktop_file_contents = g_strdup_printf ("%s\n%s",
122 desktop_file_base_contents,
123 exec_line_variants[i]);
124
125 /* We load a desktop file from memory to force the app not
126 * to have an app ID, which would check different codepaths.
127 */
128 fake_desktop_file = g_key_file_new ();
129 loaded = g_key_file_load_from_data (fake_desktop_file, desktop_file_contents, -1, G_KEY_FILE_NONE, NULL);
130 g_assert_true (loaded);
131
132 appinfo = (GAppInfo*)g_desktop_app_info_new_from_keyfile (fake_desktop_file);
133 g_assert_nonnull (appinfo);
134
135 test_launch_for_app_info (appinfo);
136
137 g_free (desktop_file_contents);
138 g_object_unref (appinfo);
139 g_key_file_unref (fake_desktop_file);
140 }
141
142 g_test_dbus_down (bus);
143 g_clear_object (&bus);
144
145 g_free (exec_line_variants[1]);
146 g_free (exec_line_variants[0]);
147 }
148
149 static void
150 test_locale (const char *locale)
151 {
152 GAppInfo *appinfo;
153 gchar *orig = NULL;
154 const gchar *path;
155
156 orig = g_strdup (setlocale (LC_ALL, NULL));
157 g_setenv ("LANGUAGE", locale, TRUE);
158 setlocale (LC_ALL, "");
159
160 path = g_test_get_filename (G_TEST_DIST, "appinfo-test-static.desktop", NULL);
161 appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (path);
162
163 if (g_strcmp0 (locale, "C") == 0)
164 {
165 g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "appinfo-test");
166 g_assert_cmpstr (g_app_info_get_description (appinfo), ==, "GAppInfo example");
167 g_assert_cmpstr (g_app_info_get_display_name (appinfo), ==, "example");
168 }
169 else if (g_str_has_prefix (locale, "en"))
170 {
171 g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "appinfo-test");
172 g_assert_cmpstr (g_app_info_get_description (appinfo), ==, "GAppInfo example");
173 g_assert_cmpstr (g_app_info_get_display_name (appinfo), ==, "example");
174 }
175 else if (g_str_has_prefix (locale, "de"))
176 {
177 g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "appinfo-test-de");
178 g_assert_cmpstr (g_app_info_get_description (appinfo), ==, "GAppInfo Beispiel");
179 g_assert_cmpstr (g_app_info_get_display_name (appinfo), ==, "Beispiel");
180 }
181
182 g_object_unref (appinfo);
183
184 g_setenv ("LANGUAGE", orig, TRUE);
185 setlocale (LC_ALL, "");
186 g_free (orig);
187 }
188
189 static void
190 test_text (void)
191 {
192 test_locale ("C");
193 test_locale ("en_US");
194 test_locale ("de");
195 test_locale ("de_DE.UTF-8");
196 }
197
198 static void
199 test_basic (void)
200 {
201 GAppInfo *appinfo;
202 GAppInfo *appinfo2;
203 GIcon *icon, *icon2;
204 const gchar *path;
205
206 path = g_test_get_filename (G_TEST_DIST, "appinfo-test-static.desktop", NULL);
207 appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (path);
208 g_assert_nonnull (appinfo);
209
210 g_assert_cmpstr (g_app_info_get_id (appinfo), ==, "appinfo-test-static.desktop");
211 g_assert_nonnull (strstr (g_app_info_get_executable (appinfo), "true"));
212
213 icon = g_app_info_get_icon (appinfo);
214 g_assert_true (G_IS_THEMED_ICON (icon));
215 icon2 = g_themed_icon_new ("testicon");
216 g_assert_true (g_icon_equal (icon, icon2));
217 g_object_unref (icon2);
218
219 appinfo2 = g_app_info_dup (appinfo);
220 g_assert_cmpstr (g_app_info_get_id (appinfo), ==, g_app_info_get_id (appinfo2));
221 g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, g_app_info_get_commandline (appinfo2));
222
223 g_object_unref (appinfo);
224 g_object_unref (appinfo2);
225 }
226
227 static void
228 test_show_in (void)
229 {
230 GAppInfo *appinfo;
231 const gchar *path;
232
233 path = g_test_get_filename (G_TEST_BUILT, "appinfo-test.desktop", NULL);
234 appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (path);
235 g_assert_true (G_IS_APP_INFO (appinfo));
236
237 g_assert_true (g_app_info_should_show (appinfo));
238 g_object_unref (appinfo);
239
240 path = g_test_get_filename (G_TEST_BUILT, "appinfo-test-gnome.desktop", NULL);
241 appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (path);
242 g_assert_true (g_app_info_should_show (appinfo));
243 g_object_unref (appinfo);
244
245 path = g_test_get_filename (G_TEST_BUILT, "appinfo-test-notgnome.desktop", NULL);
246 appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (path);
247 g_assert_false (g_app_info_should_show (appinfo));
248 g_object_unref (appinfo);
249 }
250
251 static void
252 test_commandline (void)
253 {
254 GAppInfo *appinfo;
255 GError *error;
256 gchar *cmdline;
257 gchar *cmdline_out;
258
259 cmdline = g_strconcat (g_test_get_dir (G_TEST_BUILT), "/appinfo-test --option", NULL);
260 cmdline_out = g_strconcat (cmdline, " %u", NULL);
261
262 error = NULL;
263 appinfo = g_app_info_create_from_commandline (cmdline,
264 "cmdline-app-test",
265 G_APP_INFO_CREATE_SUPPORTS_URIS,
266 &error);
267 g_assert_no_error (error);
268 g_assert_nonnull (appinfo);
269 g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "cmdline-app-test");
270 g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, cmdline_out);
271 g_assert_true (g_app_info_supports_uris (appinfo));
272 g_assert_false (g_app_info_supports_files (appinfo));
273
274 g_object_unref (appinfo);
275
276 g_free (cmdline_out);
277 cmdline_out = g_strconcat (cmdline, " %f", NULL);
278
279 error = NULL;
280 appinfo = g_app_info_create_from_commandline (cmdline,
281 "cmdline-app-test",
282 G_APP_INFO_CREATE_NONE,
283 &error);
284 g_assert_no_error (error);
285 g_assert_nonnull (appinfo);
286 g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "cmdline-app-test");
287 g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, cmdline_out);
288 g_assert_false (g_app_info_supports_uris (appinfo));
289 g_assert_true (g_app_info_supports_files (appinfo));
290
291 g_object_unref (appinfo);
292
293 g_free (cmdline);
294 g_free (cmdline_out);
295 }
296
297 static void
298 test_launch_context (void)
299 {
300 GAppLaunchContext *context;
301 GAppInfo *appinfo;
302 gchar *str;
303 gchar *cmdline;
304
305 cmdline = g_strconcat (g_test_get_dir (G_TEST_BUILT), "/appinfo-test --option", NULL);
306
307 context = g_app_launch_context_new ();
308 appinfo = g_app_info_create_from_commandline (cmdline,
309 "cmdline-app-test",
310 G_APP_INFO_CREATE_SUPPORTS_URIS,
311 NULL);
312
313 str = g_app_launch_context_get_display (context, appinfo, NULL);
314 g_assert_null (str);
315
316 str = g_app_launch_context_get_startup_notify_id (context, appinfo, NULL);
317 g_assert_null (str);
318
319 g_object_unref (appinfo);
320 g_object_unref (context);
321
322 g_free (cmdline);
323 }
324
325 static gboolean launched_reached;
326
327 static void
328 launched (GAppLaunchContext *context,
329 GAppInfo *info,
330 GVariant *platform_data,
331 gpointer user_data)
332 {
333 gint pid;
334
335 pid = 0;
336 g_assert_true (g_variant_lookup (platform_data, "pid", "i", &pid));
337 g_assert_cmpint (pid, !=, 0);
338
339 launched_reached = TRUE;
340 }
341
342 static void
343 launch_failed (GAppLaunchContext *context,
344 const gchar *startup_notify_id)
345 {
346 g_assert_not_reached ();
347 }
348
349 static void
350 test_launch_context_signals (void)
351 {
352 GTestDBus *bus = NULL;
353 GAppLaunchContext *context;
354 GAppInfo *appinfo;
355 GError *error = NULL;
356 gboolean success;
357 gchar *cmdline;
358
359 /* Set up a test session bus to keep D-Bus traffic off the real session bus. */
360 bus = g_test_dbus_new (G_TEST_DBUS_NONE);
361 g_test_dbus_up (bus);
362
363 cmdline = g_strconcat (g_test_get_dir (G_TEST_BUILT), "/appinfo-test --option", NULL);
364
365 context = g_app_launch_context_new ();
366 g_signal_connect (context, "launched", G_CALLBACK (launched), NULL);
367 g_signal_connect (context, "launch_failed", G_CALLBACK (launch_failed), NULL);
368 appinfo = g_app_info_create_from_commandline (cmdline,
369 "cmdline-app-test",
370 G_APP_INFO_CREATE_SUPPORTS_URIS,
371 NULL);
372
373 success = g_app_info_launch (appinfo, NULL, context, &error);
374 g_assert_no_error (error);
375 g_assert_true (success);
376
377 g_assert_true (launched_reached);
378
379 g_object_unref (appinfo);
380 g_object_unref (context);
381
382 g_free (cmdline);
383
384 g_test_dbus_down (bus);
385 g_clear_object (&bus);
386 }
387
388 static void
389 test_tryexec (void)
390 {
391 GAppInfo *appinfo;
392 const gchar *path;
393
394 path = g_test_get_filename (G_TEST_BUILT, "appinfo-test2.desktop", NULL);
395 appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (path);
396
397 g_assert_null (appinfo);
398 }
399
400 /* Test that we can set an appinfo as default for a mime type or
401 * file extension, and also add and remove handled mime types.
402 */
403 static void
404 test_associations (void)
405 {
406 GAppInfo *appinfo;
407 GAppInfo *appinfo2;
408 GError *error = NULL;
409 gboolean result;
410 GList *list;
411 gchar *cmdline;
412 gchar *update_desktop_database = NULL, *update_mime_database = NULL;
413
414 update_desktop_database = g_find_program_in_path ("update-desktop-database");
415 update_mime_database = g_find_program_in_path ("update-mime-database");
416
417 if (update_desktop_database == NULL || update_mime_database == NULL)
418 {
419 g_test_skip ("update-desktop-database and update-mime-database are needed to change file associations");
420 g_free (update_desktop_database);
421 g_free (update_mime_database);
422 return;
423 }
424
425 g_free (update_desktop_database);
426 g_free (update_mime_database);
427
428 cmdline = g_strconcat (g_test_get_dir (G_TEST_BUILT), "/appinfo-test --option", NULL);
429 appinfo = g_app_info_create_from_commandline (cmdline,
430 "cmdline-app-test",
431 G_APP_INFO_CREATE_SUPPORTS_URIS,
432 NULL);
433 g_free (cmdline);
434
435 result = g_app_info_set_as_default_for_type (appinfo, "application/x-glib-test", &error);
436 g_assert_no_error (error);
437 g_assert_true (result);
438
439 appinfo2 = g_app_info_get_default_for_type ("application/x-glib-test", FALSE);
440
441 g_assert_nonnull (appinfo2);
442 g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, g_app_info_get_commandline (appinfo2));
443
444 g_object_unref (appinfo2);
445
446 result = g_app_info_set_as_default_for_extension (appinfo, "gio-tests", &error);
447 g_assert_no_error (error);
448 g_assert_true (result);
449
450 appinfo2 = g_app_info_get_default_for_type ("application/x-extension-gio-tests", FALSE);
451
452 g_assert_nonnull (appinfo2);
453 g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, g_app_info_get_commandline (appinfo2));
454
455 g_object_unref (appinfo2);
456
457 result = g_app_info_add_supports_type (appinfo, "application/x-gio-test", &error);
458 g_assert_no_error (error);
459 g_assert_true (result);
460
461 list = g_app_info_get_all_for_type ("application/x-gio-test");
462 g_assert_cmpint (g_list_length (list), ==, 1);
463 appinfo2 = list->data;
464 g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, g_app_info_get_commandline (appinfo2));
465 g_object_unref (appinfo2);
466 g_list_free (list);
467
468 g_assert_true (g_app_info_can_remove_supports_type (appinfo));
469 result = g_app_info_remove_supports_type (appinfo, "application/x-gio-test", &error);
470 g_assert_no_error (error);
471 g_assert_true (result);
472
473 g_assert_true (g_app_info_can_delete (appinfo));
474 g_assert_true (g_app_info_delete (appinfo));
475 g_object_unref (appinfo);
476 }
477
478 static void
479 test_environment (void)
480 {
481 GAppLaunchContext *ctx;
482 gchar **env;
483 const gchar *path;
484
485 g_unsetenv ("FOO");
486 g_unsetenv ("BLA");
487 path = g_getenv ("PATH");
488
489 ctx = g_app_launch_context_new ();
490
491 env = g_app_launch_context_get_environment (ctx);
492
493 g_assert_null (g_environ_getenv (env, "FOO"));
494 g_assert_null (g_environ_getenv (env, "BLA"));
495 g_assert_cmpstr (g_environ_getenv (env, "PATH"), ==, path);
496
497 g_strfreev (env);
498
499 g_app_launch_context_setenv (ctx, "FOO", "bar");
500 g_app_launch_context_setenv (ctx, "BLA", "bla");
501
502 env = g_app_launch_context_get_environment (ctx);
503
504 g_assert_cmpstr (g_environ_getenv (env, "FOO"), ==, "bar");
505 g_assert_cmpstr (g_environ_getenv (env, "BLA"), ==, "bla");
506 g_assert_cmpstr (g_environ_getenv (env, "PATH"), ==, path);
507
508 g_strfreev (env);
509
510 g_app_launch_context_setenv (ctx, "FOO", "baz");
511 g_app_launch_context_unsetenv (ctx, "BLA");
512
513 env = g_app_launch_context_get_environment (ctx);
514
515 g_assert_cmpstr (g_environ_getenv (env, "FOO"), ==, "baz");
516 g_assert_null (g_environ_getenv (env, "BLA"));
517
518 g_strfreev (env);
519
520 g_object_unref (ctx);
521 }
522
523 static void
524 test_startup_wm_class (void)
525 {
526 GDesktopAppInfo *appinfo;
527 const char *wm_class;
528 const gchar *path;
529
530 path = g_test_get_filename (G_TEST_DIST, "appinfo-test-static.desktop", NULL);
531 appinfo = g_desktop_app_info_new_from_filename (path);
532 wm_class = g_desktop_app_info_get_startup_wm_class (appinfo);
533
534 g_assert_cmpstr (wm_class, ==, "appinfo-class");
535
536 g_object_unref (appinfo);
537 }
538
539 static void
540 test_supported_types (void)
541 {
542 GAppInfo *appinfo;
543 const char * const *content_types;
544 const gchar *path;
545
546 path = g_test_get_filename (G_TEST_DIST, "appinfo-test-static.desktop", NULL);
547 appinfo = G_APP_INFO (g_desktop_app_info_new_from_filename (path));
548 content_types = g_app_info_get_supported_types (appinfo);
549
550 g_assert_cmpint (g_strv_length ((char**)content_types), ==, 2);
551 g_assert_cmpstr (content_types[0], ==, "image/png");
552
553 g_object_unref (appinfo);
554 }
555
556 static void
557 test_from_keyfile (void)
558 {
559 GDesktopAppInfo *info;
560 GKeyFile *kf;
561 GError *error = NULL;
562 const gchar *categories;
563 gchar **categories_list;
564 gsize categories_count;
565 gchar **keywords;
566 const gchar *file;
567 const gchar *name;
568 const gchar *path;
569
570 path = g_test_get_filename (G_TEST_DIST, "appinfo-test-static.desktop", NULL);
571 kf = g_key_file_new ();
572 g_key_file_load_from_file (kf, path, G_KEY_FILE_NONE, &error);
573 g_assert_no_error (error);
574 info = g_desktop_app_info_new_from_keyfile (kf);
575 g_key_file_unref (kf);
576 g_assert_nonnull (info);
577
578 g_object_get (info, "filename", &file, NULL);
579 g_assert_null (file);
580
581 file = g_desktop_app_info_get_filename (info);
582 g_assert_null (file);
583 categories = g_desktop_app_info_get_categories (info);
584 g_assert_cmpstr (categories, ==, "GNOME;GTK;");
585 categories_list = g_desktop_app_info_get_string_list (info, "Categories", &categories_count);
586 g_assert_cmpint (categories_count, ==, 2);
587 g_assert_cmpint (g_strv_length (categories_list), ==, 2);
588 g_assert_cmpstr (categories_list[0], ==, "GNOME");
589 g_assert_cmpstr (categories_list[1], ==, "GTK");
590 keywords = (gchar **)g_desktop_app_info_get_keywords (info);
591 g_assert_cmpint (g_strv_length (keywords), ==, 2);
592 g_assert_cmpstr (keywords[0], ==, "keyword1");
593 g_assert_cmpstr (keywords[1], ==, "test keyword");
594 name = g_desktop_app_info_get_generic_name (info);
595 g_assert_cmpstr (name, ==, "generic-appinfo-test");
596 g_assert_false (g_desktop_app_info_get_nodisplay (info));
597
598 g_strfreev (categories_list);
599 g_object_unref (info);
600 }
601
602 int
603 main (int argc, char *argv[])
604 {
605 g_setenv ("XDG_CURRENT_DESKTOP", "GNOME", TRUE);
606
607 g_test_init (&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL);
608
609 g_test_add_func ("/appinfo/basic", test_basic);
610 g_test_add_func ("/appinfo/text", test_text);
611 g_test_add_func ("/appinfo/launch", test_launch);
612 g_test_add_func ("/appinfo/launch/no-appid", test_launch_no_app_id);
613 g_test_add_func ("/appinfo/show-in", test_show_in);
614 g_test_add_func ("/appinfo/commandline", test_commandline);
615 g_test_add_func ("/appinfo/launch-context", test_launch_context);
616 g_test_add_func ("/appinfo/launch-context-signals", test_launch_context_signals);
617 g_test_add_func ("/appinfo/tryexec", test_tryexec);
618 g_test_add_func ("/appinfo/associations", test_associations);
619 g_test_add_func ("/appinfo/environment", test_environment);
620 g_test_add_func ("/appinfo/startup-wm-class", test_startup_wm_class);
621 g_test_add_func ("/appinfo/supported-types", test_supported_types);
622 g_test_add_func ("/appinfo/from-keyfile", test_from_keyfile);
623
624 return g_test_run ();
625 }