5 #include <gio/gdesktopappinfo.h>
7 static const gchar *datapath;
18 path = g_build_filename (datapath, "appinfo-test.desktop", NULL);
19 appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (path);
20 g_assert (appinfo != NULL);
24 g_assert (g_app_info_launch (appinfo, NULL, NULL, &error));
25 g_assert_no_error (error);
27 g_assert (g_app_info_launch_uris (appinfo, NULL, NULL, &error));
28 g_assert_no_error (error);
30 path = g_build_filename (datapath, "appinfo-test.desktop", NULL);
31 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 path = g_strconcat ("file://", datapath, "/appinfo-test.desktop", NULL);
43 l = g_list_append (l, path);
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);
51 g_object_unref (appinfo);
55 test_locale (const char *locale)
61 orig = setlocale (LC_ALL, NULL);
62 g_setenv ("LANGUAGE", locale, TRUE);
63 setlocale (LC_ALL, "");
65 path = g_build_filename (datapath, "appinfo-test.desktop", NULL);
66 appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (path);
69 if (g_strcmp0 (locale, "C") == 0)
71 g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "appinfo-test");
72 g_assert_cmpstr (g_app_info_get_description (appinfo), ==, "GAppInfo example");
73 g_assert_cmpstr (g_app_info_get_display_name (appinfo), ==, "example");
75 else if (g_str_has_prefix (locale, "en"))
77 g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "appinfo-test");
78 g_assert_cmpstr (g_app_info_get_description (appinfo), ==, "GAppInfo example");
79 g_assert_cmpstr (g_app_info_get_display_name (appinfo), ==, "example");
81 else if (g_str_has_prefix (locale, "de"))
83 g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "appinfo-test-de");
84 g_assert_cmpstr (g_app_info_get_description (appinfo), ==, "GAppInfo Beispiel");
85 g_assert_cmpstr (g_app_info_get_display_name (appinfo), ==, "Beispiel");
88 g_object_unref (appinfo);
90 g_setenv ("LANGUAGE", orig, TRUE);
91 setlocale (LC_ALL, "");
98 test_locale ("en_US");
100 test_locale ("de_DE.UTF-8");
111 path = g_build_filename (datapath, "appinfo-test.desktop", NULL);
112 appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (path);
115 g_assert_cmpstr (g_app_info_get_id (appinfo), ==, "appinfo-test.desktop");
116 g_assert_cmpstr (g_app_info_get_executable (appinfo), ==, "./appinfo-test");
118 icon = g_app_info_get_icon (appinfo);
119 g_assert (G_IS_THEMED_ICON (icon));
120 icon2 = g_themed_icon_new ("testicon");
121 g_assert (g_icon_equal (icon, icon2));
122 g_object_unref (icon2);
124 appinfo2 = g_app_info_dup (appinfo);
125 g_assert_cmpstr (g_app_info_get_id (appinfo), ==, g_app_info_get_id (appinfo2));
126 g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, g_app_info_get_commandline (appinfo2));
128 g_object_unref (appinfo);
129 g_object_unref (appinfo2);
138 g_desktop_app_info_set_desktop_env ("GNOME");
140 path = g_build_filename (datapath, "appinfo-test.desktop", NULL);
141 appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (path);
142 g_assert (g_app_info_should_show (appinfo));
143 g_object_unref (appinfo);
146 path = g_build_filename (datapath, "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);
152 path = g_build_filename (datapath, "appinfo-test-notgnome.desktop", NULL);
153 appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (path);
154 g_assert (!g_app_info_should_show (appinfo));
155 g_object_unref (appinfo);
160 test_commandline (void)
167 cmdline = g_strconcat (datapath, "/appinfo-test --option", NULL);
168 cmdline_out = g_strconcat (cmdline, " %u", NULL);
171 appinfo = g_app_info_create_from_commandline (cmdline,
173 G_APP_INFO_CREATE_SUPPORTS_URIS,
175 g_assert (appinfo != NULL);
176 g_assert_no_error (error);
177 g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "cmdline-app-test");
178 g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, cmdline_out);
179 g_assert (g_app_info_supports_uris (appinfo));
180 g_assert (!g_app_info_supports_files (appinfo));
182 g_object_unref (appinfo);
184 g_free (cmdline_out);
185 cmdline_out = g_strconcat (cmdline, " %f", NULL);
188 appinfo = g_app_info_create_from_commandline (cmdline,
190 G_APP_INFO_CREATE_NONE,
192 g_assert (appinfo != NULL);
193 g_assert_no_error (error);
194 g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "cmdline-app-test");
195 g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, cmdline_out);
196 g_assert (!g_app_info_supports_uris (appinfo));
197 g_assert (g_app_info_supports_files (appinfo));
199 g_object_unref (appinfo);
202 g_free (cmdline_out);
206 test_launch_context (void)
208 GAppLaunchContext *context;
213 cmdline = g_strconcat (datapath, "/appinfo-test --option", NULL);
215 context = g_app_launch_context_new ();
216 appinfo = g_app_info_create_from_commandline (cmdline,
218 G_APP_INFO_CREATE_SUPPORTS_URIS,
221 str = g_app_launch_context_get_display (context, appinfo, NULL);
222 g_assert (str == NULL);
224 str = g_app_launch_context_get_startup_notify_id (context, appinfo, NULL);
225 g_assert (str == NULL);
227 g_object_unref (appinfo);
228 g_object_unref (context);
233 static gboolean launched_reached;
236 launched (GAppLaunchContext *context,
238 GVariant *platform_data,
244 g_assert (g_variant_lookup (platform_data, "pid", "i", &pid));
247 launched_reached = TRUE;
251 launch_failed (GAppLaunchContext *context,
252 const gchar *startup_notify_id)
254 g_assert_not_reached ();
258 test_launch_context_signals (void)
260 GAppLaunchContext *context;
262 GError *error = NULL;
265 cmdline = g_strconcat (datapath, "/appinfo-test --option", NULL);
267 context = g_app_launch_context_new ();
268 g_signal_connect (context, "launched", G_CALLBACK (launched), NULL);
269 g_signal_connect (context, "launch_failed", G_CALLBACK (launch_failed), NULL);
270 appinfo = g_app_info_create_from_commandline (cmdline,
272 G_APP_INFO_CREATE_SUPPORTS_URIS,
276 g_assert (g_app_info_launch (appinfo, NULL, context, &error));
277 g_assert_no_error (error);
279 g_assert (launched_reached);
281 g_object_unref (appinfo);
282 g_object_unref (context);
293 path = g_build_filename (datapath, "appinfo-test2.desktop", NULL);
294 appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (path);
296 g_assert (appinfo == NULL);
301 /* Test that we can set an appinfo as default for a mime type or
302 * file extension, and also add and remove handled mime types.
305 test_associations (void)
314 cmdline = g_strconcat (datapath, "/appinfo-test --option", NULL);
315 appinfo = g_app_info_create_from_commandline (cmdline,
317 G_APP_INFO_CREATE_SUPPORTS_URIS,
321 result = g_app_info_set_as_default_for_type (appinfo, "application/x-glib-test", &error);
324 g_assert_no_error (error);
326 appinfo2 = g_app_info_get_default_for_type ("application/x-glib-test", FALSE);
329 g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, g_app_info_get_commandline (appinfo2));
331 g_object_unref (appinfo2);
333 result = g_app_info_set_as_default_for_extension (appinfo, "gio-tests", &error);
335 g_assert_no_error (error);
337 appinfo2 = g_app_info_get_default_for_type ("application/x-extension-gio-tests", FALSE);
340 g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, g_app_info_get_commandline (appinfo2));
342 g_object_unref (appinfo2);
344 result = g_app_info_add_supports_type (appinfo, "application/x-gio-test", &error);
346 g_assert_no_error (error);
348 list = g_app_info_get_all_for_type ("application/x-gio-test");
349 g_assert_cmpint (g_list_length (list), ==, 1);
350 appinfo2 = list->data;
351 g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, g_app_info_get_commandline (appinfo2));
352 g_object_unref (appinfo2);
355 g_assert (g_app_info_can_remove_supports_type (appinfo));
356 g_assert (g_app_info_remove_supports_type (appinfo, "application/x-gio-test", &error));
357 g_assert_no_error (error);
359 g_assert (g_app_info_can_delete (appinfo));
360 g_assert (g_app_info_delete (appinfo));
361 g_object_unref (appinfo);
365 test_environment (void)
367 GAppLaunchContext *ctx;
373 path = g_getenv ("PATH");
375 ctx = g_app_launch_context_new ();
377 env = g_app_launch_context_get_environment (ctx);
379 g_assert (g_environ_getenv (env, "FOO") == NULL);
380 g_assert (g_environ_getenv (env, "BLA") == NULL);
381 g_assert_cmpstr (g_environ_getenv (env, "PATH"), ==, path);
385 g_app_launch_context_setenv (ctx, "FOO", "bar");
386 g_app_launch_context_setenv (ctx, "BLA", "bla");
388 env = g_app_launch_context_get_environment (ctx);
390 g_assert_cmpstr (g_environ_getenv (env, "FOO"), ==, "bar");
391 g_assert_cmpstr (g_environ_getenv (env, "BLA"), ==, "bla");
392 g_assert_cmpstr (g_environ_getenv (env, "PATH"), ==, path);
396 g_app_launch_context_setenv (ctx, "FOO", "baz");
397 g_app_launch_context_unsetenv (ctx, "BLA");
399 env = g_app_launch_context_get_environment (ctx);
401 g_assert_cmpstr (g_environ_getenv (env, "FOO"), ==, "baz");
402 g_assert (g_environ_getenv (env, "BLA") == NULL);
406 g_object_unref (ctx);
410 test_startup_wm_class (void)
412 GDesktopAppInfo *appinfo;
413 const char *wm_class;
416 path = g_build_filename (datapath, "appinfo-test.desktop", NULL);
417 appinfo = g_desktop_app_info_new_from_filename (path);
418 wm_class = g_desktop_app_info_get_startup_wm_class (appinfo);
420 g_assert_cmpstr (wm_class, ==, "appinfo-class");
422 g_object_unref (appinfo);
427 test_supported_types (void)
430 const char * const *content_types;
433 path = g_build_filename (datapath, "appinfo-test.desktop", NULL);
434 appinfo = G_APP_INFO (g_desktop_app_info_new_from_filename (path));
435 content_types = g_app_info_get_supported_types (appinfo);
437 g_assert_cmpint (g_strv_length ((char**)content_types), ==, 2);
438 g_assert_cmpstr (content_types[0], ==, "image/png");
440 g_object_unref (appinfo);
445 test_from_keyfile (void)
447 GDesktopAppInfo *info;
449 GError *error = NULL;
450 const gchar *categories;
456 path = g_build_filename (datapath, "appinfo-test.desktop", NULL);
457 kf = g_key_file_new ();
458 g_key_file_load_from_file (kf, path, G_KEY_FILE_NONE, &error);
459 g_assert_no_error (error);
460 info = g_desktop_app_info_new_from_keyfile (kf);
461 g_key_file_free (kf);
462 g_assert (info != NULL);
465 g_object_get (info, "filename", &file, NULL);
466 g_assert (file == NULL);
468 file = g_desktop_app_info_get_filename (info);
469 g_assert (file == NULL);
470 categories = g_desktop_app_info_get_categories (info);
471 g_assert_cmpstr (categories, ==, "GNOME;GTK;");
472 keywords = (gchar **)g_desktop_app_info_get_keywords (info);
473 g_assert_cmpint (g_strv_length (keywords), ==, 2);
474 g_assert_cmpstr (keywords[0], ==, "keyword1");
475 g_assert_cmpstr (keywords[1], ==, "test keyword");
476 name = g_desktop_app_info_get_generic_name (info);
477 g_assert_cmpstr (name, ==, "generic-appinfo-test");
478 g_assert (!g_desktop_app_info_get_nodisplay (info));
480 g_object_unref (info);
484 main (int argc, char *argv[])
486 if (g_getenv ("G_TEST_DATA"))
487 datapath = g_getenv ("G_TEST_DATA");
491 g_test_init (&argc, &argv, NULL);
493 g_test_add_func ("/appinfo/basic", test_basic);
494 g_test_add_func ("/appinfo/text", test_text);
495 g_test_add_func ("/appinfo/launch", test_launch);
496 g_test_add_func ("/appinfo/show-in", test_show_in);
497 g_test_add_func ("/appinfo/commandline", test_commandline);
498 g_test_add_func ("/appinfo/launch-context", test_launch_context);
499 g_test_add_func ("/appinfo/launch-context-signals", test_launch_context_signals);
500 g_test_add_func ("/appinfo/tryexec", test_tryexec);
501 g_test_add_func ("/appinfo/associations", test_associations);
502 g_test_add_func ("/appinfo/environment", test_environment);
503 g_test_add_func ("/appinfo/startup-wm-class", test_startup_wm_class);
504 g_test_add_func ("/appinfo/supported-types", test_supported_types);
505 g_test_add_func ("/appinfo/from-keyfile", test_from_keyfile);
507 return g_test_run ();