5 #include <gio/gdesktopappinfo.h>
15 appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (SRCDIR "/appinfo-test.desktop");
16 g_assert (appinfo != NULL);
19 g_assert (g_app_info_launch (appinfo, NULL, NULL, &error));
20 g_assert_no_error (error);
22 g_assert (g_app_info_launch_uris (appinfo, NULL, NULL, &error));
23 g_assert_no_error (error);
25 file = g_file_new_for_path (SRCDIR "/appinfo-test.desktop");
27 l = g_list_append (l, file);
29 g_assert (g_app_info_launch (appinfo, l, NULL, &error));
30 g_assert_no_error (error);
32 g_object_unref (file);
35 l = g_list_append (l, "file://" SRCDIR "/appinfo-test.desktop");
36 l = g_list_append (l, "file:///etc/group#adm");
38 g_assert (g_app_info_launch_uris (appinfo, l, NULL, &error));
39 g_assert_no_error (error);
42 g_object_unref (appinfo);
46 test_locale (const char *locale)
51 orig = setlocale (LC_ALL, NULL);
52 g_setenv ("LANGUAGE", locale, TRUE);
53 setlocale (LC_ALL, "");
55 appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (SRCDIR "/appinfo-test.desktop");
57 if (g_strcmp0 (locale, "C") == 0)
59 g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "appinfo-test");
60 g_assert_cmpstr (g_app_info_get_description (appinfo), ==, "GAppInfo example");
61 g_assert_cmpstr (g_app_info_get_display_name (appinfo), ==, "example");
63 else if (g_str_has_prefix (locale, "en"))
65 g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "appinfo-test");
66 g_assert_cmpstr (g_app_info_get_description (appinfo), ==, "GAppInfo example");
67 g_assert_cmpstr (g_app_info_get_display_name (appinfo), ==, "example");
69 else if (g_str_has_prefix (locale, "de"))
71 g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "appinfo-test-de");
72 g_assert_cmpstr (g_app_info_get_description (appinfo), ==, "GAppInfo Beispiel");
73 g_assert_cmpstr (g_app_info_get_display_name (appinfo), ==, "Beispiel");
76 g_object_unref (appinfo);
78 g_setenv ("LANGUAGE", orig, TRUE);
79 setlocale (LC_ALL, "");
86 test_locale ("en_US");
88 test_locale ("de_DE.UTF-8");
98 appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (SRCDIR "/appinfo-test.desktop");
100 g_assert (g_app_info_get_id (appinfo) == NULL);
101 g_assert_cmpstr (g_app_info_get_executable (appinfo), ==, "./appinfo-test");
103 icon = g_app_info_get_icon (appinfo);
104 g_assert (G_IS_THEMED_ICON (icon));
105 icon2 = g_themed_icon_new ("testicon");
106 g_assert (g_icon_equal (icon, icon2));
107 g_object_unref (icon2);
109 appinfo2 = g_app_info_dup (appinfo);
110 g_assert (g_app_info_get_id (appinfo) == g_app_info_get_id (appinfo2));
111 g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, g_app_info_get_commandline (appinfo2));
113 g_object_unref (appinfo);
114 g_object_unref (appinfo2);
122 g_desktop_app_info_set_desktop_env ("GNOME");
124 appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (SRCDIR "/appinfo-test.desktop");
125 g_assert (g_app_info_should_show (appinfo));
126 g_object_unref (appinfo);
128 appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (SRCDIR "/appinfo-test-gnome.desktop");
129 g_assert (g_app_info_should_show (appinfo));
130 g_object_unref (appinfo);
132 appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (SRCDIR "/appinfo-test-notgnome.desktop");
133 g_assert (!g_app_info_should_show (appinfo));
134 g_object_unref (appinfo);
138 test_commandline (void)
144 appinfo = g_app_info_create_from_commandline ("./appinfo-test --option",
146 G_APP_INFO_CREATE_SUPPORTS_URIS,
148 g_assert (appinfo != NULL);
149 g_assert_no_error (error);
150 g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "cmdline-app-test");
151 g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, "./appinfo-test --option %u");
152 g_assert (g_app_info_supports_uris (appinfo));
153 g_assert (!g_app_info_supports_files (appinfo));
155 g_object_unref (appinfo);
158 appinfo = g_app_info_create_from_commandline ("./appinfo-test --option",
160 G_APP_INFO_CREATE_NONE,
162 g_assert (appinfo != NULL);
163 g_assert_no_error (error);
164 g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "cmdline-app-test");
165 g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, "./appinfo-test --option %f");
166 g_assert (!g_app_info_supports_uris (appinfo));
167 g_assert (g_app_info_supports_files (appinfo));
169 g_object_unref (appinfo);
173 test_launch_context (void)
175 GAppLaunchContext *context;
179 context = g_app_launch_context_new ();
180 appinfo = g_app_info_create_from_commandline ("./appinfo-test --option",
182 G_APP_INFO_CREATE_SUPPORTS_URIS,
185 str = g_app_launch_context_get_display (context, appinfo, NULL);
186 g_assert (str == NULL);
188 str = g_app_launch_context_get_startup_notify_id (context, appinfo, NULL);
189 g_assert (str == NULL);
191 g_object_unref (appinfo);
192 g_object_unref (context);
195 static gboolean launched_reached;
198 launched (GAppLaunchContext *context,
200 GVariant *platform_data,
206 g_assert (g_variant_lookup (platform_data, "pid", "i", &pid));
209 launched_reached = TRUE;
213 launch_failed (GAppLaunchContext *context,
214 const gchar *startup_notify_id)
216 g_assert_not_reached ();
220 test_launch_context_signals (void)
222 GAppLaunchContext *context;
224 GError *error = NULL;
226 context = g_app_launch_context_new ();
227 g_signal_connect (context, "launched", G_CALLBACK (launched), NULL);
228 g_signal_connect (context, "launch_failed", G_CALLBACK (launch_failed), NULL);
229 appinfo = g_app_info_create_from_commandline ("./appinfo-test --option",
231 G_APP_INFO_CREATE_SUPPORTS_URIS,
235 g_assert (g_app_info_launch (appinfo, NULL, context, &error));
236 g_assert_no_error (error);
238 g_assert (launched_reached);
240 g_object_unref (appinfo);
241 g_object_unref (context);
249 appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (SRCDIR "/appinfo-test2.desktop");
251 g_assert (appinfo == NULL);
254 /* Test that we can set an appinfo as default for a mime type or
255 * file extension, and also add and remove handled mime types.
258 test_associations (void)
266 appinfo = g_app_info_create_from_commandline ("./appinfo-test --option",
268 G_APP_INFO_CREATE_SUPPORTS_URIS,
272 result = g_app_info_set_as_default_for_type (appinfo, "application/x-glib-test", &error);
275 g_assert_no_error (error);
277 appinfo2 = g_app_info_get_default_for_type ("application/x-glib-test", FALSE);
280 g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, g_app_info_get_commandline (appinfo2));
282 g_object_unref (appinfo2);
284 result = g_app_info_set_as_default_for_extension (appinfo, "gio-tests", &error);
286 g_assert_no_error (error);
288 appinfo2 = g_app_info_get_default_for_type ("application/x-extension-gio-tests", FALSE);
291 g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, g_app_info_get_commandline (appinfo2));
293 g_object_unref (appinfo2);
295 result = g_app_info_add_supports_type (appinfo, "application/x-gio-test", &error);
297 g_assert_no_error (error);
299 list = g_app_info_get_all_for_type ("application/x-gio-test");
300 g_assert_cmpint (g_list_length (list), ==, 1);
301 appinfo2 = list->data;
302 g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, g_app_info_get_commandline (appinfo2));
303 g_object_unref (appinfo2);
306 g_assert (g_app_info_can_remove_supports_type (appinfo));
307 g_assert (g_app_info_remove_supports_type (appinfo, "application/x-gio-test", &error));
308 g_assert_no_error (error);
310 g_assert (g_app_info_can_delete (appinfo));
311 g_assert (g_app_info_delete (appinfo));
312 g_object_unref (appinfo);
316 test_environment (void)
318 GAppLaunchContext *ctx;
324 path = g_getenv ("PATH");
326 ctx = g_app_launch_context_new ();
328 env = g_app_launch_context_get_environment (ctx);
330 g_assert (g_environ_getenv (env, "FOO") == NULL);
331 g_assert (g_environ_getenv (env, "BLA") == NULL);
332 g_assert_cmpstr (g_environ_getenv (env, "PATH"), ==, path);
336 g_app_launch_context_setenv (ctx, "FOO", "bar");
337 g_app_launch_context_setenv (ctx, "BLA", "bla");
339 env = g_app_launch_context_get_environment (ctx);
341 g_assert_cmpstr (g_environ_getenv (env, "FOO"), ==, "bar");
342 g_assert_cmpstr (g_environ_getenv (env, "BLA"), ==, "bla");
343 g_assert_cmpstr (g_environ_getenv (env, "PATH"), ==, path);
347 g_app_launch_context_setenv (ctx, "FOO", "baz");
348 g_app_launch_context_unsetenv (ctx, "BLA");
350 env = g_app_launch_context_get_environment (ctx);
352 g_assert_cmpstr (g_environ_getenv (env, "FOO"), ==, "baz");
353 g_assert (g_environ_getenv (env, "BLA") == NULL);
357 g_object_unref (ctx);
361 test_startup_wm_class (void)
363 GDesktopAppInfo *appinfo;
364 const char *wm_class;
366 appinfo = g_desktop_app_info_new_from_filename (SRCDIR "/appinfo-test.desktop");
367 wm_class = g_desktop_app_info_get_startup_wm_class (appinfo);
369 g_assert_cmpstr (wm_class, ==, "appinfo-class");
371 g_object_unref (appinfo);
375 test_supported_types (void)
378 const char * const *content_types;
380 appinfo = G_APP_INFO (g_desktop_app_info_new_from_filename (SRCDIR "/appinfo-test.desktop"));
381 content_types = g_app_info_get_supported_types (appinfo);
383 g_assert_cmpint (g_strv_length ((char**)content_types), ==, 2);
384 g_assert_cmpstr (content_types[0], ==, "image/png");
386 g_object_unref (appinfo);
390 test_from_keyfile (void)
392 GDesktopAppInfo *info;
394 GError *error = NULL;
395 const gchar *categories;
400 kf = g_key_file_new ();
401 g_key_file_load_from_file (kf,
402 SRCDIR "/appinfo-test.desktop",
405 g_assert_no_error (error);
406 info = g_desktop_app_info_new_from_keyfile (kf);
407 g_key_file_free (kf);
408 g_assert (info != NULL);
410 g_object_get (info, "filename", &file, NULL);
411 g_assert (file == NULL);
413 file = g_desktop_app_info_get_filename (info);
414 g_assert (file == NULL);
415 categories = g_desktop_app_info_get_categories (info);
416 g_assert_cmpstr (categories, ==, "GNOME;GTK;");
417 keywords = (gchar **)g_desktop_app_info_get_keywords (info);
418 g_assert_cmpint (g_strv_length (keywords), ==, 2);
419 g_assert_cmpstr (keywords[0], ==, "keyword1");
420 g_assert_cmpstr (keywords[1], ==, "test keyword");
421 name = g_desktop_app_info_get_generic_name (info);
422 g_assert_cmpstr (name, ==, "generic-appinfo-test");
423 g_assert (!g_desktop_app_info_get_nodisplay (info));
425 g_object_unref (info);
429 main (int argc, char *argv[])
431 g_test_init (&argc, &argv, NULL);
433 g_test_add_func ("/appinfo/basic", test_basic);
434 g_test_add_func ("/appinfo/text", test_text);
435 g_test_add_func ("/appinfo/launch", test_launch);
436 g_test_add_func ("/appinfo/show-in", test_show_in);
437 g_test_add_func ("/appinfo/commandline", test_commandline);
438 g_test_add_func ("/appinfo/launch-context", test_launch_context);
439 g_test_add_func ("/appinfo/launch-context-signals", test_launch_context_signals);
440 g_test_add_func ("/appinfo/tryexec", test_tryexec);
441 g_test_add_func ("/appinfo/associations", test_associations);
442 g_test_add_func ("/appinfo/environment", test_environment);
443 g_test_add_func ("/appinfo/startup-wm-class", test_startup_wm_class);
444 g_test_add_func ("/appinfo/supported-types", test_supported_types);
445 g_test_add_func ("/appinfo/from-keyfile", test_from_keyfile);
447 return g_test_run ();