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 path
= g_test_get_filename (G_TEST_DIST
, "appinfo-test.desktop", NULL
);
140 appinfo
= (GAppInfo
*)g_desktop_app_info_new_from_filename (path
);
141 g_assert (g_app_info_should_show (appinfo
));
142 g_object_unref (appinfo
);
144 path
= g_test_get_filename (G_TEST_DIST
, "appinfo-test-gnome.desktop", NULL
);
145 appinfo
= (GAppInfo
*)g_desktop_app_info_new_from_filename (path
);
146 g_assert (g_app_info_should_show (appinfo
));
147 g_object_unref (appinfo
);
149 path
= g_test_get_filename (G_TEST_DIST
, "appinfo-test-notgnome.desktop", NULL
);
150 appinfo
= (GAppInfo
*)g_desktop_app_info_new_from_filename (path
);
151 g_assert (!g_app_info_should_show (appinfo
));
152 g_object_unref (appinfo
);
156 test_commandline (void)
163 cmdline
= g_strconcat (g_test_get_dir (G_TEST_BUILT
), "/appinfo-test --option", NULL
);
164 cmdline_out
= g_strconcat (cmdline
, " %u", NULL
);
167 appinfo
= g_app_info_create_from_commandline (cmdline
,
169 G_APP_INFO_CREATE_SUPPORTS_URIS
,
171 g_assert (appinfo
!= NULL
);
172 g_assert_no_error (error
);
173 g_assert_cmpstr (g_app_info_get_name (appinfo
), ==, "cmdline-app-test");
174 g_assert_cmpstr (g_app_info_get_commandline (appinfo
), ==, cmdline_out
);
175 g_assert (g_app_info_supports_uris (appinfo
));
176 g_assert (!g_app_info_supports_files (appinfo
));
178 g_object_unref (appinfo
);
180 g_free (cmdline_out
);
181 cmdline_out
= g_strconcat (cmdline
, " %f", NULL
);
184 appinfo
= g_app_info_create_from_commandline (cmdline
,
186 G_APP_INFO_CREATE_NONE
,
188 g_assert (appinfo
!= NULL
);
189 g_assert_no_error (error
);
190 g_assert_cmpstr (g_app_info_get_name (appinfo
), ==, "cmdline-app-test");
191 g_assert_cmpstr (g_app_info_get_commandline (appinfo
), ==, cmdline_out
);
192 g_assert (!g_app_info_supports_uris (appinfo
));
193 g_assert (g_app_info_supports_files (appinfo
));
195 g_object_unref (appinfo
);
198 g_free (cmdline_out
);
202 test_launch_context (void)
204 GAppLaunchContext
*context
;
209 cmdline
= g_strconcat (g_test_get_dir (G_TEST_BUILT
), "/appinfo-test --option", NULL
);
211 context
= g_app_launch_context_new ();
212 appinfo
= g_app_info_create_from_commandline (cmdline
,
214 G_APP_INFO_CREATE_SUPPORTS_URIS
,
217 str
= g_app_launch_context_get_display (context
, appinfo
, NULL
);
218 g_assert (str
== NULL
);
220 str
= g_app_launch_context_get_startup_notify_id (context
, appinfo
, NULL
);
221 g_assert (str
== NULL
);
223 g_object_unref (appinfo
);
224 g_object_unref (context
);
229 static gboolean launched_reached
;
232 launched (GAppLaunchContext
*context
,
234 GVariant
*platform_data
,
240 g_assert (g_variant_lookup (platform_data
, "pid", "i", &pid
));
243 launched_reached
= TRUE
;
247 launch_failed (GAppLaunchContext
*context
,
248 const gchar
*startup_notify_id
)
250 g_assert_not_reached ();
254 test_launch_context_signals (void)
256 GAppLaunchContext
*context
;
258 GError
*error
= NULL
;
261 cmdline
= g_strconcat (g_test_get_dir (G_TEST_BUILT
), "/appinfo-test --option", NULL
);
263 context
= g_app_launch_context_new ();
264 g_signal_connect (context
, "launched", G_CALLBACK (launched
), NULL
);
265 g_signal_connect (context
, "launch_failed", G_CALLBACK (launch_failed
), NULL
);
266 appinfo
= g_app_info_create_from_commandline (cmdline
,
268 G_APP_INFO_CREATE_SUPPORTS_URIS
,
272 g_assert (g_app_info_launch (appinfo
, NULL
, context
, &error
));
273 g_assert_no_error (error
);
275 g_assert (launched_reached
);
277 g_object_unref (appinfo
);
278 g_object_unref (context
);
289 path
= g_test_get_filename (G_TEST_DIST
, "appinfo-test2.desktop", NULL
);
290 appinfo
= (GAppInfo
*)g_desktop_app_info_new_from_filename (path
);
292 g_assert (appinfo
== NULL
);
295 /* Test that we can set an appinfo as default for a mime type or
296 * file extension, and also add and remove handled mime types.
299 test_associations (void)
308 cmdline
= g_strconcat (g_test_get_dir (G_TEST_BUILT
), "/appinfo-test --option", NULL
);
309 appinfo
= g_app_info_create_from_commandline (cmdline
,
311 G_APP_INFO_CREATE_SUPPORTS_URIS
,
315 result
= g_app_info_set_as_default_for_type (appinfo
, "application/x-glib-test", &error
);
318 g_assert_no_error (error
);
320 appinfo2
= g_app_info_get_default_for_type ("application/x-glib-test", FALSE
);
323 g_assert_cmpstr (g_app_info_get_commandline (appinfo
), ==, g_app_info_get_commandline (appinfo2
));
325 g_object_unref (appinfo2
);
327 result
= g_app_info_set_as_default_for_extension (appinfo
, "gio-tests", &error
);
329 g_assert_no_error (error
);
331 appinfo2
= g_app_info_get_default_for_type ("application/x-extension-gio-tests", FALSE
);
334 g_assert_cmpstr (g_app_info_get_commandline (appinfo
), ==, g_app_info_get_commandline (appinfo2
));
336 g_object_unref (appinfo2
);
338 result
= g_app_info_add_supports_type (appinfo
, "application/x-gio-test", &error
);
340 g_assert_no_error (error
);
342 list
= g_app_info_get_all_for_type ("application/x-gio-test");
343 g_assert_cmpint (g_list_length (list
), ==, 1);
344 appinfo2
= list
->data
;
345 g_assert_cmpstr (g_app_info_get_commandline (appinfo
), ==, g_app_info_get_commandline (appinfo2
));
346 g_object_unref (appinfo2
);
349 g_assert (g_app_info_can_remove_supports_type (appinfo
));
350 g_assert (g_app_info_remove_supports_type (appinfo
, "application/x-gio-test", &error
));
351 g_assert_no_error (error
);
353 g_assert (g_app_info_can_delete (appinfo
));
354 g_assert (g_app_info_delete (appinfo
));
355 g_object_unref (appinfo
);
359 test_environment (void)
361 GAppLaunchContext
*ctx
;
367 path
= g_getenv ("PATH");
369 ctx
= g_app_launch_context_new ();
371 env
= g_app_launch_context_get_environment (ctx
);
373 g_assert (g_environ_getenv (env
, "FOO") == NULL
);
374 g_assert (g_environ_getenv (env
, "BLA") == NULL
);
375 g_assert_cmpstr (g_environ_getenv (env
, "PATH"), ==, path
);
379 g_app_launch_context_setenv (ctx
, "FOO", "bar");
380 g_app_launch_context_setenv (ctx
, "BLA", "bla");
382 env
= g_app_launch_context_get_environment (ctx
);
384 g_assert_cmpstr (g_environ_getenv (env
, "FOO"), ==, "bar");
385 g_assert_cmpstr (g_environ_getenv (env
, "BLA"), ==, "bla");
386 g_assert_cmpstr (g_environ_getenv (env
, "PATH"), ==, path
);
390 g_app_launch_context_setenv (ctx
, "FOO", "baz");
391 g_app_launch_context_unsetenv (ctx
, "BLA");
393 env
= g_app_launch_context_get_environment (ctx
);
395 g_assert_cmpstr (g_environ_getenv (env
, "FOO"), ==, "baz");
396 g_assert (g_environ_getenv (env
, "BLA") == NULL
);
400 g_object_unref (ctx
);
404 test_startup_wm_class (void)
406 GDesktopAppInfo
*appinfo
;
407 const char *wm_class
;
410 path
= g_test_get_filename (G_TEST_DIST
, "appinfo-test.desktop", NULL
);
411 appinfo
= g_desktop_app_info_new_from_filename (path
);
412 wm_class
= g_desktop_app_info_get_startup_wm_class (appinfo
);
414 g_assert_cmpstr (wm_class
, ==, "appinfo-class");
416 g_object_unref (appinfo
);
420 test_supported_types (void)
423 const char * const *content_types
;
426 path
= g_test_get_filename (G_TEST_DIST
, "appinfo-test.desktop", NULL
);
427 appinfo
= G_APP_INFO (g_desktop_app_info_new_from_filename (path
));
428 content_types
= g_app_info_get_supported_types (appinfo
);
430 g_assert_cmpint (g_strv_length ((char**)content_types
), ==, 2);
431 g_assert_cmpstr (content_types
[0], ==, "image/png");
433 g_object_unref (appinfo
);
437 test_from_keyfile (void)
439 GDesktopAppInfo
*info
;
441 GError
*error
= NULL
;
442 const gchar
*categories
;
448 path
= g_test_get_filename (G_TEST_DIST
, "appinfo-test.desktop", NULL
);
449 kf
= g_key_file_new ();
450 g_key_file_load_from_file (kf
, path
, G_KEY_FILE_NONE
, &error
);
451 g_assert_no_error (error
);
452 info
= g_desktop_app_info_new_from_keyfile (kf
);
453 g_key_file_free (kf
);
454 g_assert (info
!= NULL
);
456 g_object_get (info
, "filename", &file
, NULL
);
457 g_assert (file
== NULL
);
459 file
= g_desktop_app_info_get_filename (info
);
460 g_assert (file
== NULL
);
461 categories
= g_desktop_app_info_get_categories (info
);
462 g_assert_cmpstr (categories
, ==, "GNOME;GTK;");
463 keywords
= (gchar
**)g_desktop_app_info_get_keywords (info
);
464 g_assert_cmpint (g_strv_length (keywords
), ==, 2);
465 g_assert_cmpstr (keywords
[0], ==, "keyword1");
466 g_assert_cmpstr (keywords
[1], ==, "test keyword");
467 name
= g_desktop_app_info_get_generic_name (info
);
468 g_assert_cmpstr (name
, ==, "generic-appinfo-test");
469 g_assert (!g_desktop_app_info_get_nodisplay (info
));
471 g_object_unref (info
);
475 main (int argc
, char *argv
[])
477 g_setenv ("XDG_CURRENT_DESKTOP", "GNOME", TRUE
);
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 ();