5 #include <glib/gstdio.h>
7 #include <gio/gdesktopappinfo.h>
10 test_launch_for_app_info (GAppInfo
*appinfo
)
18 if (g_getenv ("DISPLAY") == NULL
|| g_getenv ("DISPLAY")[0] == '\0')
20 g_printerr ("No DISPLAY. Skipping test. ");
25 g_assert (g_app_info_launch (appinfo
, NULL
, NULL
, &error
));
26 g_assert_no_error (error
);
28 g_assert (g_app_info_launch_uris (appinfo
, NULL
, NULL
, &error
));
29 g_assert_no_error (error
);
31 path
= g_test_get_filename (G_TEST_DIST
, "appinfo-test.desktop", NULL
);
32 file
= g_file_new_for_path (path
);
34 l
= g_list_append (l
, file
);
36 g_assert (g_app_info_launch (appinfo
, l
, NULL
, &error
));
37 g_assert_no_error (error
);
39 g_object_unref (file
);
42 uri
= g_strconcat ("file://", g_test_get_dir (G_TEST_DIST
), "/appinfo-test.desktop", NULL
);
43 l
= g_list_append (l
, uri
);
44 l
= g_list_append (l
, "file:///etc/group#adm");
46 g_assert (g_app_info_launch_uris (appinfo
, l
, NULL
, &error
));
47 g_assert_no_error (error
);
58 path
= g_test_get_filename (G_TEST_DIST
, "appinfo-test.desktop", NULL
);
59 appinfo
= (GAppInfo
*)g_desktop_app_info_new_from_filename (path
);
60 g_assert (appinfo
!= NULL
);
62 test_launch_for_app_info (appinfo
);
63 g_object_unref (appinfo
);
67 test_launch_no_app_id (void)
69 const gchar desktop_file_base_contents
[] =
72 "GenericName=generic-appinfo-test\n"
74 "Name[de]=appinfo-test-de\n"
75 "X-GNOME-FullName=example\n"
76 "X-GNOME-FullName[de]=Beispiel\n"
77 "Comment=GAppInfo example\n"
78 "Comment[de]=GAppInfo Beispiel\n"
81 "StartupNotify=true\n"
82 "StartupWMClass=appinfo-class\n"
83 "MimeType=image/png;image/jpeg;\n"
84 "Keywords=keyword1;test keyword;\n"
85 "Categories=GNOME;GTK;\n";
87 const char *exec_line_variants
[] = {
88 "Exec=./appinfo-test --option %U %i --name %c --filename %k %m %%",
89 "Exec=./appinfo-test --option %u %i --name %c --filename %k %m %%"
94 g_test_bug ("791337");
96 for (i
= 0; i
< G_N_ELEMENTS (exec_line_variants
); i
++)
98 gchar
*desktop_file_contents
;
99 GKeyFile
*fake_desktop_file
;
103 g_test_message ("Exec line variant #%" G_GSIZE_FORMAT
, i
);
105 desktop_file_contents
= g_strdup_printf ("%s\n%s",
106 desktop_file_base_contents
,
107 exec_line_variants
[i
]);
109 /* We load a desktop file from memory to force the app not
110 * to have an app ID, which would check different codepaths.
112 fake_desktop_file
= g_key_file_new ();
113 loaded
= g_key_file_load_from_data (fake_desktop_file
, desktop_file_contents
, -1, G_KEY_FILE_NONE
, NULL
);
114 g_assert_true (loaded
);
116 appinfo
= (GAppInfo
*)g_desktop_app_info_new_from_keyfile (fake_desktop_file
);
117 g_assert (appinfo
!= NULL
);
119 test_launch_for_app_info (appinfo
);
121 g_free (desktop_file_contents
);
122 g_object_unref (appinfo
);
123 g_key_file_unref (fake_desktop_file
);
128 test_locale (const char *locale
)
134 orig
= setlocale (LC_ALL
, NULL
);
135 g_setenv ("LANGUAGE", locale
, TRUE
);
136 setlocale (LC_ALL
, "");
138 path
= g_test_get_filename (G_TEST_DIST
, "appinfo-test.desktop", NULL
);
139 appinfo
= (GAppInfo
*)g_desktop_app_info_new_from_filename (path
);
141 if (g_strcmp0 (locale
, "C") == 0)
143 g_assert_cmpstr (g_app_info_get_name (appinfo
), ==, "appinfo-test");
144 g_assert_cmpstr (g_app_info_get_description (appinfo
), ==, "GAppInfo example");
145 g_assert_cmpstr (g_app_info_get_display_name (appinfo
), ==, "example");
147 else if (g_str_has_prefix (locale
, "en"))
149 g_assert_cmpstr (g_app_info_get_name (appinfo
), ==, "appinfo-test");
150 g_assert_cmpstr (g_app_info_get_description (appinfo
), ==, "GAppInfo example");
151 g_assert_cmpstr (g_app_info_get_display_name (appinfo
), ==, "example");
153 else if (g_str_has_prefix (locale
, "de"))
155 g_assert_cmpstr (g_app_info_get_name (appinfo
), ==, "appinfo-test-de");
156 g_assert_cmpstr (g_app_info_get_description (appinfo
), ==, "GAppInfo Beispiel");
157 g_assert_cmpstr (g_app_info_get_display_name (appinfo
), ==, "Beispiel");
160 g_object_unref (appinfo
);
162 g_setenv ("LANGUAGE", orig
, TRUE
);
163 setlocale (LC_ALL
, "");
170 test_locale ("en_US");
172 test_locale ("de_DE.UTF-8");
183 path
= g_test_get_filename (G_TEST_DIST
, "appinfo-test.desktop", NULL
);
184 appinfo
= (GAppInfo
*)g_desktop_app_info_new_from_filename (path
);
186 g_assert_cmpstr (g_app_info_get_id (appinfo
), ==, "appinfo-test.desktop");
187 g_assert (strstr (g_app_info_get_executable (appinfo
), "appinfo-test") != NULL
);
189 icon
= g_app_info_get_icon (appinfo
);
190 g_assert (G_IS_THEMED_ICON (icon
));
191 icon2
= g_themed_icon_new ("testicon");
192 g_assert (g_icon_equal (icon
, icon2
));
193 g_object_unref (icon2
);
195 appinfo2
= g_app_info_dup (appinfo
);
196 g_assert_cmpstr (g_app_info_get_id (appinfo
), ==, g_app_info_get_id (appinfo2
));
197 g_assert_cmpstr (g_app_info_get_commandline (appinfo
), ==, g_app_info_get_commandline (appinfo2
));
199 g_object_unref (appinfo
);
200 g_object_unref (appinfo2
);
209 path
= g_test_get_filename (G_TEST_DIST
, "appinfo-test.desktop", NULL
);
210 appinfo
= (GAppInfo
*)g_desktop_app_info_new_from_filename (path
);
211 g_assert (g_app_info_should_show (appinfo
));
212 g_object_unref (appinfo
);
214 path
= g_test_get_filename (G_TEST_DIST
, "appinfo-test-gnome.desktop", NULL
);
215 appinfo
= (GAppInfo
*)g_desktop_app_info_new_from_filename (path
);
216 g_assert (g_app_info_should_show (appinfo
));
217 g_object_unref (appinfo
);
219 path
= g_test_get_filename (G_TEST_DIST
, "appinfo-test-notgnome.desktop", NULL
);
220 appinfo
= (GAppInfo
*)g_desktop_app_info_new_from_filename (path
);
221 g_assert (!g_app_info_should_show (appinfo
));
222 g_object_unref (appinfo
);
226 test_commandline (void)
233 cmdline
= g_strconcat (g_test_get_dir (G_TEST_BUILT
), "/appinfo-test --option", NULL
);
234 cmdline_out
= g_strconcat (cmdline
, " %u", NULL
);
237 appinfo
= g_app_info_create_from_commandline (cmdline
,
239 G_APP_INFO_CREATE_SUPPORTS_URIS
,
241 g_assert (appinfo
!= NULL
);
242 g_assert_no_error (error
);
243 g_assert_cmpstr (g_app_info_get_name (appinfo
), ==, "cmdline-app-test");
244 g_assert_cmpstr (g_app_info_get_commandline (appinfo
), ==, cmdline_out
);
245 g_assert (g_app_info_supports_uris (appinfo
));
246 g_assert (!g_app_info_supports_files (appinfo
));
248 g_object_unref (appinfo
);
250 g_free (cmdline_out
);
251 cmdline_out
= g_strconcat (cmdline
, " %f", NULL
);
254 appinfo
= g_app_info_create_from_commandline (cmdline
,
256 G_APP_INFO_CREATE_NONE
,
258 g_assert (appinfo
!= NULL
);
259 g_assert_no_error (error
);
260 g_assert_cmpstr (g_app_info_get_name (appinfo
), ==, "cmdline-app-test");
261 g_assert_cmpstr (g_app_info_get_commandline (appinfo
), ==, cmdline_out
);
262 g_assert (!g_app_info_supports_uris (appinfo
));
263 g_assert (g_app_info_supports_files (appinfo
));
265 g_object_unref (appinfo
);
268 g_free (cmdline_out
);
272 test_launch_context (void)
274 GAppLaunchContext
*context
;
279 cmdline
= g_strconcat (g_test_get_dir (G_TEST_BUILT
), "/appinfo-test --option", NULL
);
281 context
= g_app_launch_context_new ();
282 appinfo
= g_app_info_create_from_commandline (cmdline
,
284 G_APP_INFO_CREATE_SUPPORTS_URIS
,
287 str
= g_app_launch_context_get_display (context
, appinfo
, NULL
);
288 g_assert (str
== NULL
);
290 str
= g_app_launch_context_get_startup_notify_id (context
, appinfo
, NULL
);
291 g_assert (str
== NULL
);
293 g_object_unref (appinfo
);
294 g_object_unref (context
);
299 static gboolean launched_reached
;
302 launched (GAppLaunchContext
*context
,
304 GVariant
*platform_data
,
310 g_assert (g_variant_lookup (platform_data
, "pid", "i", &pid
));
313 launched_reached
= TRUE
;
317 launch_failed (GAppLaunchContext
*context
,
318 const gchar
*startup_notify_id
)
320 g_assert_not_reached ();
324 test_launch_context_signals (void)
326 GAppLaunchContext
*context
;
328 GError
*error
= NULL
;
331 cmdline
= g_strconcat (g_test_get_dir (G_TEST_BUILT
), "/appinfo-test --option", NULL
);
333 context
= g_app_launch_context_new ();
334 g_signal_connect (context
, "launched", G_CALLBACK (launched
), NULL
);
335 g_signal_connect (context
, "launch_failed", G_CALLBACK (launch_failed
), NULL
);
336 appinfo
= g_app_info_create_from_commandline (cmdline
,
338 G_APP_INFO_CREATE_SUPPORTS_URIS
,
342 g_assert (g_app_info_launch (appinfo
, NULL
, context
, &error
));
343 g_assert_no_error (error
);
345 g_assert (launched_reached
);
347 g_object_unref (appinfo
);
348 g_object_unref (context
);
359 path
= g_test_get_filename (G_TEST_DIST
, "appinfo-test2.desktop", NULL
);
360 appinfo
= (GAppInfo
*)g_desktop_app_info_new_from_filename (path
);
362 g_assert (appinfo
== NULL
);
365 /* Test that we can set an appinfo as default for a mime type or
366 * file extension, and also add and remove handled mime types.
369 test_associations (void)
378 cmdline
= g_strconcat (g_test_get_dir (G_TEST_BUILT
), "/appinfo-test --option", NULL
);
379 appinfo
= g_app_info_create_from_commandline (cmdline
,
381 G_APP_INFO_CREATE_SUPPORTS_URIS
,
385 result
= g_app_info_set_as_default_for_type (appinfo
, "application/x-glib-test", &error
);
388 g_assert_no_error (error
);
390 appinfo2
= g_app_info_get_default_for_type ("application/x-glib-test", FALSE
);
393 g_assert_cmpstr (g_app_info_get_commandline (appinfo
), ==, g_app_info_get_commandline (appinfo2
));
395 g_object_unref (appinfo2
);
397 result
= g_app_info_set_as_default_for_extension (appinfo
, "gio-tests", &error
);
399 g_assert_no_error (error
);
401 appinfo2
= g_app_info_get_default_for_type ("application/x-extension-gio-tests", FALSE
);
404 g_assert_cmpstr (g_app_info_get_commandline (appinfo
), ==, g_app_info_get_commandline (appinfo2
));
406 g_object_unref (appinfo2
);
408 result
= g_app_info_add_supports_type (appinfo
, "application/x-gio-test", &error
);
410 g_assert_no_error (error
);
412 list
= g_app_info_get_all_for_type ("application/x-gio-test");
413 g_assert_cmpint (g_list_length (list
), ==, 1);
414 appinfo2
= list
->data
;
415 g_assert_cmpstr (g_app_info_get_commandline (appinfo
), ==, g_app_info_get_commandline (appinfo2
));
416 g_object_unref (appinfo2
);
419 g_assert (g_app_info_can_remove_supports_type (appinfo
));
420 g_assert (g_app_info_remove_supports_type (appinfo
, "application/x-gio-test", &error
));
421 g_assert_no_error (error
);
423 g_assert (g_app_info_can_delete (appinfo
));
424 g_assert (g_app_info_delete (appinfo
));
425 g_object_unref (appinfo
);
429 test_environment (void)
431 GAppLaunchContext
*ctx
;
437 path
= g_getenv ("PATH");
439 ctx
= g_app_launch_context_new ();
441 env
= g_app_launch_context_get_environment (ctx
);
443 g_assert (g_environ_getenv (env
, "FOO") == NULL
);
444 g_assert (g_environ_getenv (env
, "BLA") == NULL
);
445 g_assert_cmpstr (g_environ_getenv (env
, "PATH"), ==, path
);
449 g_app_launch_context_setenv (ctx
, "FOO", "bar");
450 g_app_launch_context_setenv (ctx
, "BLA", "bla");
452 env
= g_app_launch_context_get_environment (ctx
);
454 g_assert_cmpstr (g_environ_getenv (env
, "FOO"), ==, "bar");
455 g_assert_cmpstr (g_environ_getenv (env
, "BLA"), ==, "bla");
456 g_assert_cmpstr (g_environ_getenv (env
, "PATH"), ==, path
);
460 g_app_launch_context_setenv (ctx
, "FOO", "baz");
461 g_app_launch_context_unsetenv (ctx
, "BLA");
463 env
= g_app_launch_context_get_environment (ctx
);
465 g_assert_cmpstr (g_environ_getenv (env
, "FOO"), ==, "baz");
466 g_assert (g_environ_getenv (env
, "BLA") == NULL
);
470 g_object_unref (ctx
);
474 test_startup_wm_class (void)
476 GDesktopAppInfo
*appinfo
;
477 const char *wm_class
;
480 path
= g_test_get_filename (G_TEST_DIST
, "appinfo-test.desktop", NULL
);
481 appinfo
= g_desktop_app_info_new_from_filename (path
);
482 wm_class
= g_desktop_app_info_get_startup_wm_class (appinfo
);
484 g_assert_cmpstr (wm_class
, ==, "appinfo-class");
486 g_object_unref (appinfo
);
490 test_supported_types (void)
493 const char * const *content_types
;
496 path
= g_test_get_filename (G_TEST_DIST
, "appinfo-test.desktop", NULL
);
497 appinfo
= G_APP_INFO (g_desktop_app_info_new_from_filename (path
));
498 content_types
= g_app_info_get_supported_types (appinfo
);
500 g_assert_cmpint (g_strv_length ((char**)content_types
), ==, 2);
501 g_assert_cmpstr (content_types
[0], ==, "image/png");
503 g_object_unref (appinfo
);
507 test_from_keyfile (void)
509 GDesktopAppInfo
*info
;
511 GError
*error
= NULL
;
512 const gchar
*categories
;
518 path
= g_test_get_filename (G_TEST_DIST
, "appinfo-test.desktop", NULL
);
519 kf
= g_key_file_new ();
520 g_key_file_load_from_file (kf
, path
, G_KEY_FILE_NONE
, &error
);
521 g_assert_no_error (error
);
522 info
= g_desktop_app_info_new_from_keyfile (kf
);
523 g_key_file_free (kf
);
524 g_assert (info
!= NULL
);
526 g_object_get (info
, "filename", &file
, NULL
);
527 g_assert (file
== NULL
);
529 file
= g_desktop_app_info_get_filename (info
);
530 g_assert (file
== NULL
);
531 categories
= g_desktop_app_info_get_categories (info
);
532 g_assert_cmpstr (categories
, ==, "GNOME;GTK;");
533 keywords
= (gchar
**)g_desktop_app_info_get_keywords (info
);
534 g_assert_cmpint (g_strv_length (keywords
), ==, 2);
535 g_assert_cmpstr (keywords
[0], ==, "keyword1");
536 g_assert_cmpstr (keywords
[1], ==, "test keyword");
537 name
= g_desktop_app_info_get_generic_name (info
);
538 g_assert_cmpstr (name
, ==, "generic-appinfo-test");
539 g_assert (!g_desktop_app_info_get_nodisplay (info
));
541 g_object_unref (info
);
545 main (int argc
, char *argv
[])
547 const gchar
*build_dir
;
549 g_setenv ("XDG_CURRENT_DESKTOP", "GNOME", TRUE
);
551 g_test_init (&argc
, &argv
, NULL
);
552 g_test_bug_base ("https://bugzilla.gnome.org/show_bug.cgi?id=");
554 /* With Meson build we need to change into right directory, so that the
555 * appinfo-test binary can be found. */
556 build_dir
= g_getenv ("G_TEST_BUILDDIR");
560 g_test_add_func ("/appinfo/basic", test_basic
);
561 g_test_add_func ("/appinfo/text", test_text
);
562 g_test_add_func ("/appinfo/launch", test_launch
);
563 g_test_add_func ("/appinfo/launch/no-appid", test_launch_no_app_id
);
564 g_test_add_func ("/appinfo/show-in", test_show_in
);
565 g_test_add_func ("/appinfo/commandline", test_commandline
);
566 g_test_add_func ("/appinfo/launch-context", test_launch_context
);
567 g_test_add_func ("/appinfo/launch-context-signals", test_launch_context_signals
);
568 g_test_add_func ("/appinfo/tryexec", test_tryexec
);
569 g_test_add_func ("/appinfo/associations", test_associations
);
570 g_test_add_func ("/appinfo/environment", test_environment
);
571 g_test_add_func ("/appinfo/startup-wm-class", test_startup_wm_class
);
572 g_test_add_func ("/appinfo/supported-types", test_supported_types
);
573 g_test_add_func ("/appinfo/from-keyfile", test_from_keyfile
);
575 return g_test_run ();