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

source code of gtk/subprojects/glib/gio/tests/appinfo.c