6 #include <gio/gdesktopappinfo.h>
8 static const gchar *datapath;
9 static const gchar *binpath;
20 path = g_build_filename (datapath, "appinfo-test.desktop", NULL);
21 appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (path);
22 g_assert (appinfo != NULL);
26 g_assert (g_app_info_launch (appinfo, NULL, NULL, &error));
27 g_assert_no_error (error);
29 g_assert (g_app_info_launch_uris (appinfo, NULL, NULL, &error));
30 g_assert_no_error (error);
32 path = g_build_filename (datapath, "appinfo-test.desktop", NULL);
33 file = g_file_new_for_path (path);
36 l = g_list_append (l, file);
38 g_assert (g_app_info_launch (appinfo, l, NULL, &error));
39 g_assert_no_error (error);
41 g_object_unref (file);
44 path = g_strconcat ("file://", datapath, "/appinfo-test.desktop", NULL);
45 l = g_list_append (l, path);
46 l = g_list_append (l, "file:///etc/group#adm");
48 g_assert (g_app_info_launch_uris (appinfo, l, NULL, &error));
49 g_assert_no_error (error);
53 g_object_unref (appinfo);
57 test_locale (const char *locale)
63 orig = setlocale (LC_ALL, NULL);
64 g_setenv ("LANGUAGE", locale, TRUE);
65 setlocale (LC_ALL, "");
67 path = g_build_filename (datapath, "appinfo-test.desktop", NULL);
68 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_build_filename (datapath, "appinfo-test.desktop", NULL);
114 appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (path);
117 g_assert_cmpstr (g_app_info_get_id (appinfo), ==, "appinfo-test.desktop");
118 g_assert (strstr (g_app_info_get_executable (appinfo), "appinfo-test") != NULL);
120 icon = g_app_info_get_icon (appinfo);
121 g_assert (G_IS_THEMED_ICON (icon));
122 icon2 = g_themed_icon_new ("testicon");
123 g_assert (g_icon_equal (icon, icon2));
124 g_object_unref (icon2);
126 appinfo2 = g_app_info_dup (appinfo);
127 g_assert_cmpstr (g_app_info_get_id (appinfo), ==, g_app_info_get_id (appinfo2));
128 g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, g_app_info_get_commandline (appinfo2));
130 g_object_unref (appinfo);
131 g_object_unref (appinfo2);
140 g_desktop_app_info_set_desktop_env ("GNOME");
142 path = g_build_filename (datapath, "appinfo-test.desktop", NULL);
143 appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (path);
144 g_assert (g_app_info_should_show (appinfo));
145 g_object_unref (appinfo);
148 path = g_build_filename (datapath, "appinfo-test-gnome.desktop", NULL);
149 appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (path);
150 g_assert (g_app_info_should_show (appinfo));
151 g_object_unref (appinfo);
154 path = g_build_filename (datapath, "appinfo-test-notgnome.desktop", NULL);
155 appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (path);
156 g_assert (!g_app_info_should_show (appinfo));
157 g_object_unref (appinfo);
162 test_commandline (void)
169 cmdline = g_strconcat (binpath, "/appinfo-test --option", NULL);
170 cmdline_out = g_strconcat (cmdline, " %u", NULL);
173 appinfo = g_app_info_create_from_commandline (cmdline,
175 G_APP_INFO_CREATE_SUPPORTS_URIS,
177 g_assert (appinfo != NULL);
178 g_assert_no_error (error);
179 g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "cmdline-app-test");
180 g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, cmdline_out);
181 g_assert (g_app_info_supports_uris (appinfo));
182 g_assert (!g_app_info_supports_files (appinfo));
184 g_object_unref (appinfo);
186 g_free (cmdline_out);
187 cmdline_out = g_strconcat (cmdline, " %f", NULL);
190 appinfo = g_app_info_create_from_commandline (cmdline,
192 G_APP_INFO_CREATE_NONE,
194 g_assert (appinfo != NULL);
195 g_assert_no_error (error);
196 g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "cmdline-app-test");
197 g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, cmdline_out);
198 g_assert (!g_app_info_supports_uris (appinfo));
199 g_assert (g_app_info_supports_files (appinfo));
201 g_object_unref (appinfo);
204 g_free (cmdline_out);
208 test_launch_context (void)
210 GAppLaunchContext *context;
215 cmdline = g_strconcat (binpath, "/appinfo-test --option", NULL);
217 context = g_app_launch_context_new ();
218 appinfo = g_app_info_create_from_commandline (cmdline,
220 G_APP_INFO_CREATE_SUPPORTS_URIS,
223 str = g_app_launch_context_get_display (context, appinfo, NULL);
224 g_assert (str == NULL);
226 str = g_app_launch_context_get_startup_notify_id (context, appinfo, NULL);
227 g_assert (str == NULL);
229 g_object_unref (appinfo);
230 g_object_unref (context);
235 static gboolean launched_reached;
238 launched (GAppLaunchContext *context,
240 GVariant *platform_data,
246 g_assert (g_variant_lookup (platform_data, "pid", "i", &pid));
249 launched_reached = TRUE;
253 launch_failed (GAppLaunchContext *context,
254 const gchar *startup_notify_id)
256 g_assert_not_reached ();
260 test_launch_context_signals (void)
262 GAppLaunchContext *context;
264 GError *error = NULL;
267 cmdline = g_strconcat (binpath, "/appinfo-test --option", NULL);
269 context = g_app_launch_context_new ();
270 g_signal_connect (context, "launched", G_CALLBACK (launched), NULL);
271 g_signal_connect (context, "launch_failed", G_CALLBACK (launch_failed), NULL);
272 appinfo = g_app_info_create_from_commandline (cmdline,
274 G_APP_INFO_CREATE_SUPPORTS_URIS,
278 g_assert (g_app_info_launch (appinfo, NULL, context, &error));
279 g_assert_no_error (error);
281 g_assert (launched_reached);
283 g_object_unref (appinfo);
284 g_object_unref (context);
295 path = g_build_filename (datapath, "appinfo-test2.desktop", NULL);
296 appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (path);
298 g_assert (appinfo == NULL);
303 /* Test that we can set an appinfo as default for a mime type or
304 * file extension, and also add and remove handled mime types.
307 test_associations (void)
316 cmdline = g_strconcat (binpath, "/appinfo-test --option", NULL);
317 appinfo = g_app_info_create_from_commandline (cmdline,
319 G_APP_INFO_CREATE_SUPPORTS_URIS,
323 result = g_app_info_set_as_default_for_type (appinfo, "application/x-glib-test", &error);
326 g_assert_no_error (error);
328 appinfo2 = g_app_info_get_default_for_type ("application/x-glib-test", FALSE);
331 g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, g_app_info_get_commandline (appinfo2));
333 g_object_unref (appinfo2);
335 result = g_app_info_set_as_default_for_extension (appinfo, "gio-tests", &error);
337 g_assert_no_error (error);
339 appinfo2 = g_app_info_get_default_for_type ("application/x-extension-gio-tests", FALSE);
342 g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, g_app_info_get_commandline (appinfo2));
344 g_object_unref (appinfo2);
346 result = g_app_info_add_supports_type (appinfo, "application/x-gio-test", &error);
348 g_assert_no_error (error);
350 list = g_app_info_get_all_for_type ("application/x-gio-test");
351 g_assert_cmpint (g_list_length (list), ==, 1);
352 appinfo2 = list->data;
353 g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, g_app_info_get_commandline (appinfo2));
354 g_object_unref (appinfo2);
357 g_assert (g_app_info_can_remove_supports_type (appinfo));
358 g_assert (g_app_info_remove_supports_type (appinfo, "application/x-gio-test", &error));
359 g_assert_no_error (error);
361 g_assert (g_app_info_can_delete (appinfo));
362 g_assert (g_app_info_delete (appinfo));
363 g_object_unref (appinfo);
367 test_environment (void)
369 GAppLaunchContext *ctx;
375 path = g_getenv ("PATH");
377 ctx = g_app_launch_context_new ();
379 env = g_app_launch_context_get_environment (ctx);
381 g_assert (g_environ_getenv (env, "FOO") == NULL);
382 g_assert (g_environ_getenv (env, "BLA") == NULL);
383 g_assert_cmpstr (g_environ_getenv (env, "PATH"), ==, path);
387 g_app_launch_context_setenv (ctx, "FOO", "bar");
388 g_app_launch_context_setenv (ctx, "BLA", "bla");
390 env = g_app_launch_context_get_environment (ctx);
392 g_assert_cmpstr (g_environ_getenv (env, "FOO"), ==, "bar");
393 g_assert_cmpstr (g_environ_getenv (env, "BLA"), ==, "bla");
394 g_assert_cmpstr (g_environ_getenv (env, "PATH"), ==, path);
398 g_app_launch_context_setenv (ctx, "FOO", "baz");
399 g_app_launch_context_unsetenv (ctx, "BLA");
401 env = g_app_launch_context_get_environment (ctx);
403 g_assert_cmpstr (g_environ_getenv (env, "FOO"), ==, "baz");
404 g_assert (g_environ_getenv (env, "BLA") == NULL);
408 g_object_unref (ctx);
412 test_startup_wm_class (void)
414 GDesktopAppInfo *appinfo;
415 const char *wm_class;
418 path = g_build_filename (datapath, "appinfo-test.desktop", NULL);
419 appinfo = g_desktop_app_info_new_from_filename (path);
420 wm_class = g_desktop_app_info_get_startup_wm_class (appinfo);
422 g_assert_cmpstr (wm_class, ==, "appinfo-class");
424 g_object_unref (appinfo);
429 test_supported_types (void)
432 const char * const *content_types;
435 path = g_build_filename (datapath, "appinfo-test.desktop", NULL);
436 appinfo = G_APP_INFO (g_desktop_app_info_new_from_filename (path));
437 content_types = g_app_info_get_supported_types (appinfo);
439 g_assert_cmpint (g_strv_length ((char**)content_types), ==, 2);
440 g_assert_cmpstr (content_types[0], ==, "image/png");
442 g_object_unref (appinfo);
447 test_from_keyfile (void)
449 GDesktopAppInfo *info;
451 GError *error = NULL;
452 const gchar *categories;
458 path = g_build_filename (datapath, "appinfo-test.desktop", NULL);
459 kf = g_key_file_new ();
460 g_key_file_load_from_file (kf, path, G_KEY_FILE_NONE, &error);
461 g_assert_no_error (error);
462 info = g_desktop_app_info_new_from_keyfile (kf);
463 g_key_file_free (kf);
464 g_assert (info != NULL);
467 g_object_get (info, "filename", &file, NULL);
468 g_assert (file == NULL);
470 file = g_desktop_app_info_get_filename (info);
471 g_assert (file == NULL);
472 categories = g_desktop_app_info_get_categories (info);
473 g_assert_cmpstr (categories, ==, "GNOME;GTK;");
474 keywords = (gchar **)g_desktop_app_info_get_keywords (info);
475 g_assert_cmpint (g_strv_length (keywords), ==, 2);
476 g_assert_cmpstr (keywords[0], ==, "keyword1");
477 g_assert_cmpstr (keywords[1], ==, "test keyword");
478 name = g_desktop_app_info_get_generic_name (info);
479 g_assert_cmpstr (name, ==, "generic-appinfo-test");
480 g_assert (!g_desktop_app_info_get_nodisplay (info));
482 g_object_unref (info);
486 main (int argc, char *argv[])
488 if (g_getenv ("G_TEST_DATA"))
489 datapath = binpath = g_getenv ("G_TEST_DATA");
496 g_test_init (&argc, &argv, NULL);
498 g_test_add_func ("/appinfo/basic", test_basic);
499 g_test_add_func ("/appinfo/text", test_text);
500 g_test_add_func ("/appinfo/launch", test_launch);
501 g_test_add_func ("/appinfo/show-in", test_show_in);
502 g_test_add_func ("/appinfo/commandline", test_commandline);
503 g_test_add_func ("/appinfo/launch-context", test_launch_context);
504 g_test_add_func ("/appinfo/launch-context-signals", test_launch_context_signals);
505 g_test_add_func ("/appinfo/tryexec", test_tryexec);
506 g_test_add_func ("/appinfo/associations", test_associations);
507 g_test_add_func ("/appinfo/environment", test_environment);
508 g_test_add_func ("/appinfo/startup-wm-class", test_startup_wm_class);
509 g_test_add_func ("/appinfo/supported-types", test_supported_types);
510 g_test_add_func ("/appinfo/from-keyfile", test_from_keyfile);
512 return g_test_run ();