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_test_skip ("No DISPLAY set");
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
= g_strdup (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
, "");
171 test_locale ("en_US");
173 test_locale ("de_DE.UTF-8");
184 path
= g_test_get_filename (G_TEST_DIST
, "appinfo-test.desktop", NULL
);
185 appinfo
= (GAppInfo
*)g_desktop_app_info_new_from_filename (path
);
187 g_assert_cmpstr (g_app_info_get_id (appinfo
), ==, "appinfo-test.desktop");
188 g_assert (strstr (g_app_info_get_executable (appinfo
), "appinfo-test") != NULL
);
190 icon
= g_app_info_get_icon (appinfo
);
191 g_assert (G_IS_THEMED_ICON (icon
));
192 icon2
= g_themed_icon_new ("testicon");
193 g_assert (g_icon_equal (icon
, icon2
));
194 g_object_unref (icon2
);
196 appinfo2
= g_app_info_dup (appinfo
);
197 g_assert_cmpstr (g_app_info_get_id (appinfo
), ==, g_app_info_get_id (appinfo2
));
198 g_assert_cmpstr (g_app_info_get_commandline (appinfo
), ==, g_app_info_get_commandline (appinfo2
));
200 g_object_unref (appinfo
);
201 g_object_unref (appinfo2
);
210 path
= g_test_get_filename (G_TEST_DIST
, "appinfo-test.desktop", NULL
);
211 appinfo
= (GAppInfo
*)g_desktop_app_info_new_from_filename (path
);
212 g_assert (g_app_info_should_show (appinfo
));
213 g_object_unref (appinfo
);
215 path
= g_test_get_filename (G_TEST_DIST
, "appinfo-test-gnome.desktop", NULL
);
216 appinfo
= (GAppInfo
*)g_desktop_app_info_new_from_filename (path
);
217 g_assert (g_app_info_should_show (appinfo
));
218 g_object_unref (appinfo
);
220 path
= g_test_get_filename (G_TEST_DIST
, "appinfo-test-notgnome.desktop", NULL
);
221 appinfo
= (GAppInfo
*)g_desktop_app_info_new_from_filename (path
);
222 g_assert (!g_app_info_should_show (appinfo
));
223 g_object_unref (appinfo
);
227 test_commandline (void)
234 cmdline
= g_strconcat (g_test_get_dir (G_TEST_BUILT
), "/appinfo-test --option", NULL
);
235 cmdline_out
= g_strconcat (cmdline
, " %u", NULL
);
238 appinfo
= g_app_info_create_from_commandline (cmdline
,
240 G_APP_INFO_CREATE_SUPPORTS_URIS
,
242 g_assert (appinfo
!= NULL
);
243 g_assert_no_error (error
);
244 g_assert_cmpstr (g_app_info_get_name (appinfo
), ==, "cmdline-app-test");
245 g_assert_cmpstr (g_app_info_get_commandline (appinfo
), ==, cmdline_out
);
246 g_assert (g_app_info_supports_uris (appinfo
));
247 g_assert (!g_app_info_supports_files (appinfo
));
249 g_object_unref (appinfo
);
251 g_free (cmdline_out
);
252 cmdline_out
= g_strconcat (cmdline
, " %f", NULL
);
255 appinfo
= g_app_info_create_from_commandline (cmdline
,
257 G_APP_INFO_CREATE_NONE
,
259 g_assert (appinfo
!= NULL
);
260 g_assert_no_error (error
);
261 g_assert_cmpstr (g_app_info_get_name (appinfo
), ==, "cmdline-app-test");
262 g_assert_cmpstr (g_app_info_get_commandline (appinfo
), ==, cmdline_out
);
263 g_assert (!g_app_info_supports_uris (appinfo
));
264 g_assert (g_app_info_supports_files (appinfo
));
266 g_object_unref (appinfo
);
269 g_free (cmdline_out
);
273 test_launch_context (void)
275 GAppLaunchContext
*context
;
280 cmdline
= g_strconcat (g_test_get_dir (G_TEST_BUILT
), "/appinfo-test --option", NULL
);
282 context
= g_app_launch_context_new ();
283 appinfo
= g_app_info_create_from_commandline (cmdline
,
285 G_APP_INFO_CREATE_SUPPORTS_URIS
,
288 str
= g_app_launch_context_get_display (context
, appinfo
, NULL
);
289 g_assert (str
== NULL
);
291 str
= g_app_launch_context_get_startup_notify_id (context
, appinfo
, NULL
);
292 g_assert (str
== NULL
);
294 g_object_unref (appinfo
);
295 g_object_unref (context
);
300 static gboolean launched_reached
;
303 launched (GAppLaunchContext
*context
,
305 GVariant
*platform_data
,
311 g_assert (g_variant_lookup (platform_data
, "pid", "i", &pid
));
314 launched_reached
= TRUE
;
318 launch_failed (GAppLaunchContext
*context
,
319 const gchar
*startup_notify_id
)
321 g_assert_not_reached ();
325 test_launch_context_signals (void)
327 GAppLaunchContext
*context
;
329 GError
*error
= NULL
;
332 cmdline
= g_strconcat (g_test_get_dir (G_TEST_BUILT
), "/appinfo-test --option", NULL
);
334 context
= g_app_launch_context_new ();
335 g_signal_connect (context
, "launched", G_CALLBACK (launched
), NULL
);
336 g_signal_connect (context
, "launch_failed", G_CALLBACK (launch_failed
), NULL
);
337 appinfo
= g_app_info_create_from_commandline (cmdline
,
339 G_APP_INFO_CREATE_SUPPORTS_URIS
,
343 g_assert (g_app_info_launch (appinfo
, NULL
, context
, &error
));
344 g_assert_no_error (error
);
346 g_assert (launched_reached
);
348 g_object_unref (appinfo
);
349 g_object_unref (context
);
360 path
= g_test_get_filename (G_TEST_DIST
, "appinfo-test2.desktop", NULL
);
361 appinfo
= (GAppInfo
*)g_desktop_app_info_new_from_filename (path
);
363 g_assert (appinfo
== NULL
);
366 /* Test that we can set an appinfo as default for a mime type or
367 * file extension, and also add and remove handled mime types.
370 test_associations (void)
379 cmdline
= g_strconcat (g_test_get_dir (G_TEST_BUILT
), "/appinfo-test --option", NULL
);
380 appinfo
= g_app_info_create_from_commandline (cmdline
,
382 G_APP_INFO_CREATE_SUPPORTS_URIS
,
387 result
= g_app_info_set_as_default_for_type (appinfo
, "application/x-glib-test", &error
);
390 g_assert_no_error (error
);
392 appinfo2
= g_app_info_get_default_for_type ("application/x-glib-test", FALSE
);
395 g_assert_cmpstr (g_app_info_get_commandline (appinfo
), ==, g_app_info_get_commandline (appinfo2
));
397 g_object_unref (appinfo2
);
399 result
= g_app_info_set_as_default_for_extension (appinfo
, "gio-tests", &error
);
401 g_assert_no_error (error
);
403 appinfo2
= g_app_info_get_default_for_type ("application/x-extension-gio-tests", FALSE
);
406 g_assert_cmpstr (g_app_info_get_commandline (appinfo
), ==, g_app_info_get_commandline (appinfo2
));
408 g_object_unref (appinfo2
);
410 result
= g_app_info_add_supports_type (appinfo
, "application/x-gio-test", &error
);
412 g_assert_no_error (error
);
414 list
= g_app_info_get_all_for_type ("application/x-gio-test");
415 g_assert_cmpint (g_list_length (list
), ==, 1);
416 appinfo2
= list
->data
;
417 g_assert_cmpstr (g_app_info_get_commandline (appinfo
), ==, g_app_info_get_commandline (appinfo2
));
418 g_object_unref (appinfo2
);
421 g_assert (g_app_info_can_remove_supports_type (appinfo
));
422 g_assert (g_app_info_remove_supports_type (appinfo
, "application/x-gio-test", &error
));
423 g_assert_no_error (error
);
425 g_assert (g_app_info_can_delete (appinfo
));
426 g_assert (g_app_info_delete (appinfo
));
427 g_object_unref (appinfo
);
431 test_environment (void)
433 GAppLaunchContext
*ctx
;
439 path
= g_getenv ("PATH");
441 ctx
= g_app_launch_context_new ();
443 env
= g_app_launch_context_get_environment (ctx
);
445 g_assert (g_environ_getenv (env
, "FOO") == NULL
);
446 g_assert (g_environ_getenv (env
, "BLA") == NULL
);
447 g_assert_cmpstr (g_environ_getenv (env
, "PATH"), ==, path
);
451 g_app_launch_context_setenv (ctx
, "FOO", "bar");
452 g_app_launch_context_setenv (ctx
, "BLA", "bla");
454 env
= g_app_launch_context_get_environment (ctx
);
456 g_assert_cmpstr (g_environ_getenv (env
, "FOO"), ==, "bar");
457 g_assert_cmpstr (g_environ_getenv (env
, "BLA"), ==, "bla");
458 g_assert_cmpstr (g_environ_getenv (env
, "PATH"), ==, path
);
462 g_app_launch_context_setenv (ctx
, "FOO", "baz");
463 g_app_launch_context_unsetenv (ctx
, "BLA");
465 env
= g_app_launch_context_get_environment (ctx
);
467 g_assert_cmpstr (g_environ_getenv (env
, "FOO"), ==, "baz");
468 g_assert (g_environ_getenv (env
, "BLA") == NULL
);
472 g_object_unref (ctx
);
476 test_startup_wm_class (void)
478 GDesktopAppInfo
*appinfo
;
479 const char *wm_class
;
482 path
= g_test_get_filename (G_TEST_DIST
, "appinfo-test.desktop", NULL
);
483 appinfo
= g_desktop_app_info_new_from_filename (path
);
484 wm_class
= g_desktop_app_info_get_startup_wm_class (appinfo
);
486 g_assert_cmpstr (wm_class
, ==, "appinfo-class");
488 g_object_unref (appinfo
);
492 test_supported_types (void)
495 const char * const *content_types
;
498 path
= g_test_get_filename (G_TEST_DIST
, "appinfo-test.desktop", NULL
);
499 appinfo
= G_APP_INFO (g_desktop_app_info_new_from_filename (path
));
500 content_types
= g_app_info_get_supported_types (appinfo
);
502 g_assert_cmpint (g_strv_length ((char**)content_types
), ==, 2);
503 g_assert_cmpstr (content_types
[0], ==, "image/png");
505 g_object_unref (appinfo
);
509 test_from_keyfile (void)
511 GDesktopAppInfo
*info
;
513 GError
*error
= NULL
;
514 const gchar
*categories
;
520 path
= g_test_get_filename (G_TEST_DIST
, "appinfo-test.desktop", NULL
);
521 kf
= g_key_file_new ();
522 g_key_file_load_from_file (kf
, path
, G_KEY_FILE_NONE
, &error
);
523 g_assert_no_error (error
);
524 info
= g_desktop_app_info_new_from_keyfile (kf
);
525 g_key_file_free (kf
);
526 g_assert (info
!= NULL
);
528 g_object_get (info
, "filename", &file
, NULL
);
529 g_assert (file
== NULL
);
531 file
= g_desktop_app_info_get_filename (info
);
532 g_assert (file
== NULL
);
533 categories
= g_desktop_app_info_get_categories (info
);
534 g_assert_cmpstr (categories
, ==, "GNOME;GTK;");
535 keywords
= (gchar
**)g_desktop_app_info_get_keywords (info
);
536 g_assert_cmpint (g_strv_length (keywords
), ==, 2);
537 g_assert_cmpstr (keywords
[0], ==, "keyword1");
538 g_assert_cmpstr (keywords
[1], ==, "test keyword");
539 name
= g_desktop_app_info_get_generic_name (info
);
540 g_assert_cmpstr (name
, ==, "generic-appinfo-test");
541 g_assert (!g_desktop_app_info_get_nodisplay (info
));
543 g_object_unref (info
);
547 main (int argc
, char *argv
[])
549 const gchar
*build_dir
;
551 g_setenv ("XDG_CURRENT_DESKTOP", "GNOME", TRUE
);
553 g_test_init (&argc
, &argv
, NULL
);
554 g_test_bug_base ("https://bugzilla.gnome.org/show_bug.cgi?id=");
556 /* With Meson build we need to change into right directory, so that the
557 * appinfo-test binary can be found. */
558 build_dir
= g_getenv ("G_TEST_BUILDDIR");
562 g_test_add_func ("/appinfo/basic", test_basic
);
563 g_test_add_func ("/appinfo/text", test_text
);
564 g_test_add_func ("/appinfo/launch", test_launch
);
565 g_test_add_func ("/appinfo/launch/no-appid", test_launch_no_app_id
);
566 g_test_add_func ("/appinfo/show-in", test_show_in
);
567 g_test_add_func ("/appinfo/commandline", test_commandline
);
568 g_test_add_func ("/appinfo/launch-context", test_launch_context
);
569 g_test_add_func ("/appinfo/launch-context-signals", test_launch_context_signals
);
570 g_test_add_func ("/appinfo/tryexec", test_tryexec
);
571 g_test_add_func ("/appinfo/associations", test_associations
);
572 g_test_add_func ("/appinfo/environment", test_environment
);
573 g_test_add_func ("/appinfo/startup-wm-class", test_startup_wm_class
);
574 g_test_add_func ("/appinfo/supported-types", test_supported_types
);
575 g_test_add_func ("/appinfo/from-keyfile", test_from_keyfile
);
577 return g_test_run ();