6 #include <gio/gdesktopappinfo.h>
8 static const gchar *datapath;
19 path = g_build_filename (datapath, "appinfo-test.desktop", NULL);
20 appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (path);
21 g_assert (appinfo != NULL);
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_build_filename (datapath, "appinfo-test.desktop", NULL);
32 file = g_file_new_for_path (path);
35 l = g_list_append (l, file);
37 g_assert (g_app_info_launch (appinfo, l, NULL, &error));
38 g_assert_no_error (error);
40 g_object_unref (file);
43 path = g_strconcat ("file://", datapath, "/appinfo-test.desktop", NULL);
44 l = g_list_append (l, path);
45 l = g_list_append (l, "file:///etc/group#adm");
47 g_assert (g_app_info_launch_uris (appinfo, l, NULL, &error));
48 g_assert_no_error (error);
52 g_object_unref (appinfo);
56 test_locale (const char *locale)
62 orig = setlocale (LC_ALL, NULL);
63 g_setenv ("LANGUAGE", locale, TRUE);
64 setlocale (LC_ALL, "");
66 path = g_build_filename (datapath, "appinfo-test.desktop", NULL);
67 appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (path);
70 if (g_strcmp0 (locale, "C") == 0)
72 g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "appinfo-test");
73 g_assert_cmpstr (g_app_info_get_description (appinfo), ==, "GAppInfo example");
74 g_assert_cmpstr (g_app_info_get_display_name (appinfo), ==, "example");
76 else if (g_str_has_prefix (locale, "en"))
78 g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "appinfo-test");
79 g_assert_cmpstr (g_app_info_get_description (appinfo), ==, "GAppInfo example");
80 g_assert_cmpstr (g_app_info_get_display_name (appinfo), ==, "example");
82 else if (g_str_has_prefix (locale, "de"))
84 g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "appinfo-test-de");
85 g_assert_cmpstr (g_app_info_get_description (appinfo), ==, "GAppInfo Beispiel");
86 g_assert_cmpstr (g_app_info_get_display_name (appinfo), ==, "Beispiel");
89 g_object_unref (appinfo);
91 g_setenv ("LANGUAGE", orig, TRUE);
92 setlocale (LC_ALL, "");
99 test_locale ("en_US");
101 test_locale ("de_DE.UTF-8");
112 path = g_build_filename (datapath, "appinfo-test.desktop", NULL);
113 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_build_filename (datapath, "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);
147 path = g_build_filename (datapath, "appinfo-test-gnome.desktop", NULL);
148 appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (path);
149 g_assert (g_app_info_should_show (appinfo));
150 g_object_unref (appinfo);
153 path = g_build_filename (datapath, "appinfo-test-notgnome.desktop", NULL);
154 appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (path);
155 g_assert (!g_app_info_should_show (appinfo));
156 g_object_unref (appinfo);
161 test_commandline (void)
168 cmdline = g_strconcat (datapath, "/appinfo-test --option", NULL);
169 cmdline_out = g_strconcat (cmdline, " %u", NULL);
172 appinfo = g_app_info_create_from_commandline (cmdline,
174 G_APP_INFO_CREATE_SUPPORTS_URIS,
176 g_assert (appinfo != NULL);
177 g_assert_no_error (error);
178 g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "cmdline-app-test");
179 g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, cmdline_out);
180 g_assert (g_app_info_supports_uris (appinfo));
181 g_assert (!g_app_info_supports_files (appinfo));
183 g_object_unref (appinfo);
185 g_free (cmdline_out);
186 cmdline_out = g_strconcat (cmdline, " %f", NULL);
189 appinfo = g_app_info_create_from_commandline (cmdline,
191 G_APP_INFO_CREATE_NONE,
193 g_assert (appinfo != NULL);
194 g_assert_no_error (error);
195 g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "cmdline-app-test");
196 g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, cmdline_out);
197 g_assert (!g_app_info_supports_uris (appinfo));
198 g_assert (g_app_info_supports_files (appinfo));
200 g_object_unref (appinfo);
203 g_free (cmdline_out);
207 test_launch_context (void)
209 GAppLaunchContext *context;
214 cmdline = g_strconcat (datapath, "/appinfo-test --option", NULL);
216 context = g_app_launch_context_new ();
217 appinfo = g_app_info_create_from_commandline (cmdline,
219 G_APP_INFO_CREATE_SUPPORTS_URIS,
222 str = g_app_launch_context_get_display (context, appinfo, NULL);
223 g_assert (str == NULL);
225 str = g_app_launch_context_get_startup_notify_id (context, appinfo, NULL);
226 g_assert (str == NULL);
228 g_object_unref (appinfo);
229 g_object_unref (context);
234 static gboolean launched_reached;
237 launched (GAppLaunchContext *context,
239 GVariant *platform_data,
245 g_assert (g_variant_lookup (platform_data, "pid", "i", &pid));
248 launched_reached = TRUE;
252 launch_failed (GAppLaunchContext *context,
253 const gchar *startup_notify_id)
255 g_assert_not_reached ();
259 test_launch_context_signals (void)
261 GAppLaunchContext *context;
263 GError *error = NULL;
266 cmdline = g_strconcat (datapath, "/appinfo-test --option", NULL);
268 context = g_app_launch_context_new ();
269 g_signal_connect (context, "launched", G_CALLBACK (launched), NULL);
270 g_signal_connect (context, "launch_failed", G_CALLBACK (launch_failed), NULL);
271 appinfo = g_app_info_create_from_commandline (cmdline,
273 G_APP_INFO_CREATE_SUPPORTS_URIS,
277 g_assert (g_app_info_launch (appinfo, NULL, context, &error));
278 g_assert_no_error (error);
280 g_assert (launched_reached);
282 g_object_unref (appinfo);
283 g_object_unref (context);
294 path = g_build_filename (datapath, "appinfo-test2.desktop", NULL);
295 appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (path);
297 g_assert (appinfo == NULL);
302 /* Test that we can set an appinfo as default for a mime type or
303 * file extension, and also add and remove handled mime types.
306 test_associations (void)
315 cmdline = g_strconcat (datapath, "/appinfo-test --option", NULL);
316 appinfo = g_app_info_create_from_commandline (cmdline,
318 G_APP_INFO_CREATE_SUPPORTS_URIS,
322 result = g_app_info_set_as_default_for_type (appinfo, "application/x-glib-test", &error);
325 g_assert_no_error (error);
327 appinfo2 = g_app_info_get_default_for_type ("application/x-glib-test", FALSE);
330 g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, g_app_info_get_commandline (appinfo2));
332 g_object_unref (appinfo2);
334 result = g_app_info_set_as_default_for_extension (appinfo, "gio-tests", &error);
336 g_assert_no_error (error);
338 appinfo2 = g_app_info_get_default_for_type ("application/x-extension-gio-tests", FALSE);
341 g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, g_app_info_get_commandline (appinfo2));
343 g_object_unref (appinfo2);
345 result = g_app_info_add_supports_type (appinfo, "application/x-gio-test", &error);
347 g_assert_no_error (error);
349 list = g_app_info_get_all_for_type ("application/x-gio-test");
350 g_assert_cmpint (g_list_length (list), ==, 1);
351 appinfo2 = list->data;
352 g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, g_app_info_get_commandline (appinfo2));
353 g_object_unref (appinfo2);
356 g_assert (g_app_info_can_remove_supports_type (appinfo));
357 g_assert (g_app_info_remove_supports_type (appinfo, "application/x-gio-test", &error));
358 g_assert_no_error (error);
360 g_assert (g_app_info_can_delete (appinfo));
361 g_assert (g_app_info_delete (appinfo));
362 g_object_unref (appinfo);
366 test_environment (void)
368 GAppLaunchContext *ctx;
374 path = g_getenv ("PATH");
376 ctx = g_app_launch_context_new ();
378 env = g_app_launch_context_get_environment (ctx);
380 g_assert (g_environ_getenv (env, "FOO") == NULL);
381 g_assert (g_environ_getenv (env, "BLA") == NULL);
382 g_assert_cmpstr (g_environ_getenv (env, "PATH"), ==, path);
386 g_app_launch_context_setenv (ctx, "FOO", "bar");
387 g_app_launch_context_setenv (ctx, "BLA", "bla");
389 env = g_app_launch_context_get_environment (ctx);
391 g_assert_cmpstr (g_environ_getenv (env, "FOO"), ==, "bar");
392 g_assert_cmpstr (g_environ_getenv (env, "BLA"), ==, "bla");
393 g_assert_cmpstr (g_environ_getenv (env, "PATH"), ==, path);
397 g_app_launch_context_setenv (ctx, "FOO", "baz");
398 g_app_launch_context_unsetenv (ctx, "BLA");
400 env = g_app_launch_context_get_environment (ctx);
402 g_assert_cmpstr (g_environ_getenv (env, "FOO"), ==, "baz");
403 g_assert (g_environ_getenv (env, "BLA") == NULL);
407 g_object_unref (ctx);
411 test_startup_wm_class (void)
413 GDesktopAppInfo *appinfo;
414 const char *wm_class;
417 path = g_build_filename (datapath, "appinfo-test.desktop", NULL);
418 appinfo = g_desktop_app_info_new_from_filename (path);
419 wm_class = g_desktop_app_info_get_startup_wm_class (appinfo);
421 g_assert_cmpstr (wm_class, ==, "appinfo-class");
423 g_object_unref (appinfo);
428 test_supported_types (void)
431 const char * const *content_types;
434 path = g_build_filename (datapath, "appinfo-test.desktop", NULL);
435 appinfo = G_APP_INFO (g_desktop_app_info_new_from_filename (path));
436 content_types = g_app_info_get_supported_types (appinfo);
438 g_assert_cmpint (g_strv_length ((char**)content_types), ==, 2);
439 g_assert_cmpstr (content_types[0], ==, "image/png");
441 g_object_unref (appinfo);
446 test_from_keyfile (void)
448 GDesktopAppInfo *info;
450 GError *error = NULL;
451 const gchar *categories;
457 path = g_build_filename (datapath, "appinfo-test.desktop", NULL);
458 kf = g_key_file_new ();
459 g_key_file_load_from_file (kf, path, G_KEY_FILE_NONE, &error);
460 g_assert_no_error (error);
461 info = g_desktop_app_info_new_from_keyfile (kf);
462 g_key_file_free (kf);
463 g_assert (info != NULL);
466 g_object_get (info, "filename", &file, NULL);
467 g_assert (file == NULL);
469 file = g_desktop_app_info_get_filename (info);
470 g_assert (file == NULL);
471 categories = g_desktop_app_info_get_categories (info);
472 g_assert_cmpstr (categories, ==, "GNOME;GTK;");
473 keywords = (gchar **)g_desktop_app_info_get_keywords (info);
474 g_assert_cmpint (g_strv_length (keywords), ==, 2);
475 g_assert_cmpstr (keywords[0], ==, "keyword1");
476 g_assert_cmpstr (keywords[1], ==, "test keyword");
477 name = g_desktop_app_info_get_generic_name (info);
478 g_assert_cmpstr (name, ==, "generic-appinfo-test");
479 g_assert (!g_desktop_app_info_get_nodisplay (info));
481 g_object_unref (info);
485 main (int argc, char *argv[])
487 if (g_getenv ("G_TEST_DATA"))
488 datapath = g_getenv ("G_TEST_DATA");
492 g_test_init (&argc, &argv, NULL);
494 g_test_add_func ("/appinfo/basic", test_basic);
495 g_test_add_func ("/appinfo/text", test_text);
496 g_test_add_func ("/appinfo/launch", test_launch);
497 g_test_add_func ("/appinfo/show-in", test_show_in);
498 g_test_add_func ("/appinfo/commandline", test_commandline);
499 g_test_add_func ("/appinfo/launch-context", test_launch_context);
500 g_test_add_func ("/appinfo/launch-context-signals", test_launch_context_signals);
501 g_test_add_func ("/appinfo/tryexec", test_tryexec);
502 g_test_add_func ("/appinfo/associations", test_associations);
503 g_test_add_func ("/appinfo/environment", test_environment);
504 g_test_add_func ("/appinfo/startup-wm-class", test_startup_wm_class);
505 g_test_add_func ("/appinfo/supported-types", test_supported_types);
506 g_test_add_func ("/appinfo/from-keyfile", test_from_keyfile);
508 return g_test_run ();