6 #include <gio/gdesktopappinfo.h>
18 if (g_getenv ("DISPLAY") == NULL || g_getenv ("DISPLAY")[0] == '\0')
20 g_printerr ("No DISPLAY. Skipping test. ");
24 path = g_test_get_filename (G_TEST_DIST, "appinfo-test.desktop", NULL);
25 appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (path);
26 g_assert (appinfo != NULL);
29 g_assert (g_app_info_launch (appinfo, NULL, NULL, &error));
30 g_assert_no_error (error);
32 g_assert (g_app_info_launch_uris (appinfo, NULL, NULL, &error));
33 g_assert_no_error (error);
35 file = g_file_new_for_path (path);
37 l = g_list_append (l, file);
39 g_assert (g_app_info_launch (appinfo, l, NULL, &error));
40 g_assert_no_error (error);
42 g_object_unref (file);
45 uri = g_strconcat ("file://", g_test_get_dir (G_TEST_DIST), "/appinfo-test.desktop", NULL);
46 l = g_list_append (l, uri);
47 l = g_list_append (l, "file:///etc/group#adm");
49 g_assert (g_app_info_launch_uris (appinfo, l, NULL, &error));
50 g_assert_no_error (error);
54 g_object_unref (appinfo);
58 test_locale (const char *locale)
64 orig = setlocale (LC_ALL, NULL);
65 g_setenv ("LANGUAGE", locale, TRUE);
66 setlocale (LC_ALL, "");
68 path = g_test_get_filename (G_TEST_DIST, "appinfo-test.desktop", NULL);
69 appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (path);
71 if (g_strcmp0 (locale, "C") == 0)
73 g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "appinfo-test");
74 g_assert_cmpstr (g_app_info_get_description (appinfo), ==, "GAppInfo example");
75 g_assert_cmpstr (g_app_info_get_display_name (appinfo), ==, "example");
77 else if (g_str_has_prefix (locale, "en"))
79 g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "appinfo-test");
80 g_assert_cmpstr (g_app_info_get_description (appinfo), ==, "GAppInfo example");
81 g_assert_cmpstr (g_app_info_get_display_name (appinfo), ==, "example");
83 else if (g_str_has_prefix (locale, "de"))
85 g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "appinfo-test-de");
86 g_assert_cmpstr (g_app_info_get_description (appinfo), ==, "GAppInfo Beispiel");
87 g_assert_cmpstr (g_app_info_get_display_name (appinfo), ==, "Beispiel");
90 g_object_unref (appinfo);
92 g_setenv ("LANGUAGE", orig, TRUE);
93 setlocale (LC_ALL, "");
100 test_locale ("en_US");
102 test_locale ("de_DE.UTF-8");
113 path = g_test_get_filename (G_TEST_DIST, "appinfo-test.desktop", NULL);
114 appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (path);
116 g_assert_cmpstr (g_app_info_get_id (appinfo), ==, "appinfo-test.desktop");
117 g_assert (strstr (g_app_info_get_executable (appinfo), "appinfo-test") != NULL);
119 icon = g_app_info_get_icon (appinfo);
120 g_assert (G_IS_THEMED_ICON (icon));
121 icon2 = g_themed_icon_new ("testicon");
122 g_assert (g_icon_equal (icon, icon2));
123 g_object_unref (icon2);
125 appinfo2 = g_app_info_dup (appinfo);
126 g_assert_cmpstr (g_app_info_get_id (appinfo), ==, g_app_info_get_id (appinfo2));
127 g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, g_app_info_get_commandline (appinfo2));
129 g_object_unref (appinfo);
130 g_object_unref (appinfo2);
139 g_desktop_app_info_set_desktop_env ("GNOME");
141 path = g_test_get_filename (G_TEST_DIST, "appinfo-test.desktop", NULL);
142 appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (path);
143 g_assert (g_app_info_should_show (appinfo));
144 g_object_unref (appinfo);
146 path = g_test_get_filename (G_TEST_DIST, "appinfo-test-gnome.desktop", NULL);
147 appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (path);
148 g_assert (g_app_info_should_show (appinfo));
149 g_object_unref (appinfo);
151 path = g_test_get_filename (G_TEST_DIST, "appinfo-test-notgnome.desktop", NULL);
152 appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (path);
153 g_assert (!g_app_info_should_show (appinfo));
154 g_object_unref (appinfo);
158 test_commandline (void)
165 cmdline = g_strconcat (g_test_get_dir (G_TEST_BUILT), "/appinfo-test --option", NULL);
166 cmdline_out = g_strconcat (cmdline, " %u", NULL);
169 appinfo = g_app_info_create_from_commandline (cmdline,
171 G_APP_INFO_CREATE_SUPPORTS_URIS,
173 g_assert (appinfo != NULL);
174 g_assert_no_error (error);
175 g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "cmdline-app-test");
176 g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, cmdline_out);
177 g_assert (g_app_info_supports_uris (appinfo));
178 g_assert (!g_app_info_supports_files (appinfo));
180 g_object_unref (appinfo);
182 g_free (cmdline_out);
183 cmdline_out = g_strconcat (cmdline, " %f", NULL);
186 appinfo = g_app_info_create_from_commandline (cmdline,
188 G_APP_INFO_CREATE_NONE,
190 g_assert (appinfo != NULL);
191 g_assert_no_error (error);
192 g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "cmdline-app-test");
193 g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, cmdline_out);
194 g_assert (!g_app_info_supports_uris (appinfo));
195 g_assert (g_app_info_supports_files (appinfo));
197 g_object_unref (appinfo);
200 g_free (cmdline_out);
204 test_launch_context (void)
206 GAppLaunchContext *context;
211 cmdline = g_strconcat (g_test_get_dir (G_TEST_BUILT), "/appinfo-test --option", NULL);
213 context = g_app_launch_context_new ();
214 appinfo = g_app_info_create_from_commandline (cmdline,
216 G_APP_INFO_CREATE_SUPPORTS_URIS,
219 str = g_app_launch_context_get_display (context, appinfo, NULL);
220 g_assert (str == NULL);
222 str = g_app_launch_context_get_startup_notify_id (context, appinfo, NULL);
223 g_assert (str == NULL);
225 g_object_unref (appinfo);
226 g_object_unref (context);
231 static gboolean launched_reached;
234 launched (GAppLaunchContext *context,
236 GVariant *platform_data,
242 g_assert (g_variant_lookup (platform_data, "pid", "i", &pid));
245 launched_reached = TRUE;
249 launch_failed (GAppLaunchContext *context,
250 const gchar *startup_notify_id)
252 g_assert_not_reached ();
256 test_launch_context_signals (void)
258 GAppLaunchContext *context;
260 GError *error = NULL;
263 cmdline = g_strconcat (g_test_get_dir (G_TEST_BUILT), "/appinfo-test --option", NULL);
265 context = g_app_launch_context_new ();
266 g_signal_connect (context, "launched", G_CALLBACK (launched), NULL);
267 g_signal_connect (context, "launch_failed", G_CALLBACK (launch_failed), NULL);
268 appinfo = g_app_info_create_from_commandline (cmdline,
270 G_APP_INFO_CREATE_SUPPORTS_URIS,
274 g_assert (g_app_info_launch (appinfo, NULL, context, &error));
275 g_assert_no_error (error);
277 g_assert (launched_reached);
279 g_object_unref (appinfo);
280 g_object_unref (context);
291 path = g_test_get_filename (G_TEST_DIST, "appinfo-test2.desktop", NULL);
292 appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (path);
294 g_assert (appinfo == NULL);
297 /* Test that we can set an appinfo as default for a mime type or
298 * file extension, and also add and remove handled mime types.
301 test_associations (void)
310 cmdline = g_strconcat (g_test_get_dir (G_TEST_BUILT), "/appinfo-test --option", NULL);
311 appinfo = g_app_info_create_from_commandline (cmdline,
313 G_APP_INFO_CREATE_SUPPORTS_URIS,
317 result = g_app_info_set_as_default_for_type (appinfo, "application/x-glib-test", &error);
320 g_assert_no_error (error);
322 appinfo2 = g_app_info_get_default_for_type ("application/x-glib-test", FALSE);
325 g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, g_app_info_get_commandline (appinfo2));
327 g_object_unref (appinfo2);
329 result = g_app_info_set_as_default_for_extension (appinfo, "gio-tests", &error);
331 g_assert_no_error (error);
333 appinfo2 = g_app_info_get_default_for_type ("application/x-extension-gio-tests", FALSE);
336 g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, g_app_info_get_commandline (appinfo2));
338 g_object_unref (appinfo2);
340 result = g_app_info_add_supports_type (appinfo, "application/x-gio-test", &error);
342 g_assert_no_error (error);
344 list = g_app_info_get_all_for_type ("application/x-gio-test");
345 g_assert_cmpint (g_list_length (list), ==, 1);
346 appinfo2 = list->data;
347 g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, g_app_info_get_commandline (appinfo2));
348 g_object_unref (appinfo2);
351 g_assert (g_app_info_can_remove_supports_type (appinfo));
352 g_assert (g_app_info_remove_supports_type (appinfo, "application/x-gio-test", &error));
353 g_assert_no_error (error);
355 g_assert (g_app_info_can_delete (appinfo));
356 g_assert (g_app_info_delete (appinfo));
357 g_object_unref (appinfo);
361 test_environment (void)
363 GAppLaunchContext *ctx;
369 path = g_getenv ("PATH");
371 ctx = g_app_launch_context_new ();
373 env = g_app_launch_context_get_environment (ctx);
375 g_assert (g_environ_getenv (env, "FOO") == NULL);
376 g_assert (g_environ_getenv (env, "BLA") == NULL);
377 g_assert_cmpstr (g_environ_getenv (env, "PATH"), ==, path);
381 g_app_launch_context_setenv (ctx, "FOO", "bar");
382 g_app_launch_context_setenv (ctx, "BLA", "bla");
384 env = g_app_launch_context_get_environment (ctx);
386 g_assert_cmpstr (g_environ_getenv (env, "FOO"), ==, "bar");
387 g_assert_cmpstr (g_environ_getenv (env, "BLA"), ==, "bla");
388 g_assert_cmpstr (g_environ_getenv (env, "PATH"), ==, path);
392 g_app_launch_context_setenv (ctx, "FOO", "baz");
393 g_app_launch_context_unsetenv (ctx, "BLA");
395 env = g_app_launch_context_get_environment (ctx);
397 g_assert_cmpstr (g_environ_getenv (env, "FOO"), ==, "baz");
398 g_assert (g_environ_getenv (env, "BLA") == NULL);
402 g_object_unref (ctx);
406 test_startup_wm_class (void)
408 GDesktopAppInfo *appinfo;
409 const char *wm_class;
412 path = g_test_get_filename (G_TEST_DIST, "appinfo-test.desktop", NULL);
413 appinfo = g_desktop_app_info_new_from_filename (path);
414 wm_class = g_desktop_app_info_get_startup_wm_class (appinfo);
416 g_assert_cmpstr (wm_class, ==, "appinfo-class");
418 g_object_unref (appinfo);
422 test_supported_types (void)
425 const char * const *content_types;
428 path = g_test_get_filename (G_TEST_DIST, "appinfo-test.desktop", NULL);
429 appinfo = G_APP_INFO (g_desktop_app_info_new_from_filename (path));
430 content_types = g_app_info_get_supported_types (appinfo);
432 g_assert_cmpint (g_strv_length ((char**)content_types), ==, 2);
433 g_assert_cmpstr (content_types[0], ==, "image/png");
435 g_object_unref (appinfo);
439 test_from_keyfile (void)
441 GDesktopAppInfo *info;
443 GError *error = NULL;
444 const gchar *categories;
450 path = g_test_get_filename (G_TEST_DIST, "appinfo-test.desktop", NULL);
451 kf = g_key_file_new ();
452 g_key_file_load_from_file (kf, path, G_KEY_FILE_NONE, &error);
453 g_assert_no_error (error);
454 info = g_desktop_app_info_new_from_keyfile (kf);
455 g_key_file_free (kf);
456 g_assert (info != NULL);
458 g_object_get (info, "filename", &file, NULL);
459 g_assert (file == NULL);
461 file = g_desktop_app_info_get_filename (info);
462 g_assert (file == NULL);
463 categories = g_desktop_app_info_get_categories (info);
464 g_assert_cmpstr (categories, ==, "GNOME;GTK;");
465 keywords = (gchar **)g_desktop_app_info_get_keywords (info);
466 g_assert_cmpint (g_strv_length (keywords), ==, 2);
467 g_assert_cmpstr (keywords[0], ==, "keyword1");
468 g_assert_cmpstr (keywords[1], ==, "test keyword");
469 name = g_desktop_app_info_get_generic_name (info);
470 g_assert_cmpstr (name, ==, "generic-appinfo-test");
471 g_assert (!g_desktop_app_info_get_nodisplay (info));
473 g_object_unref (info);
477 main (int argc, char *argv[])
479 g_test_init (&argc, &argv, NULL);
481 g_test_add_func ("/appinfo/basic", test_basic);
482 g_test_add_func ("/appinfo/text", test_text);
483 g_test_add_func ("/appinfo/launch", test_launch);
484 g_test_add_func ("/appinfo/show-in", test_show_in);
485 g_test_add_func ("/appinfo/commandline", test_commandline);
486 g_test_add_func ("/appinfo/launch-context", test_launch_context);
487 g_test_add_func ("/appinfo/launch-context-signals", test_launch_context_signals);
488 g_test_add_func ("/appinfo/tryexec", test_tryexec);
489 g_test_add_func ("/appinfo/associations", test_associations);
490 g_test_add_func ("/appinfo/environment", test_environment);
491 g_test_add_func ("/appinfo/startup-wm-class", test_startup_wm_class);
492 g_test_add_func ("/appinfo/supported-types", test_supported_types);
493 g_test_add_func ("/appinfo/from-keyfile", test_from_keyfile);
495 return g_test_run ();