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);
44 test_locale (const char *locale)
49 orig = setlocale (LC_ALL, NULL);
50 g_setenv ("LANGUAGE", locale, TRUE);
51 setlocale (LC_ALL, "");
53 appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (SRCDIR "/appinfo-test.desktop");
55 if (g_strcmp0 (locale, "C") == 0)
57 g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "appinfo-test");
58 g_assert_cmpstr (g_app_info_get_description (appinfo), ==, "GAppInfo example");
59 g_assert_cmpstr (g_app_info_get_display_name (appinfo), ==, "example");
61 else if (g_str_has_prefix (locale, "en"))
63 g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "appinfo-test");
64 g_assert_cmpstr (g_app_info_get_description (appinfo), ==, "GAppInfo example");
65 g_assert_cmpstr (g_app_info_get_display_name (appinfo), ==, "example");
67 else if (g_str_has_prefix (locale, "de"))
69 g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "appinfo-test-de");
70 g_assert_cmpstr (g_app_info_get_description (appinfo), ==, "GAppInfo Beispiel");
71 g_assert_cmpstr (g_app_info_get_display_name (appinfo), ==, "Beispiel");
74 g_object_unref (appinfo);
76 g_setenv ("LANGUAGE", orig, TRUE);
77 setlocale (LC_ALL, "");
84 test_locale ("en_US");
86 test_locale ("de_DE.UTF-8");
96 appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (SRCDIR "/appinfo-test.desktop");
98 g_assert (g_app_info_get_id (appinfo) == NULL);
99 g_assert_cmpstr (g_app_info_get_executable (appinfo), ==, "./appinfo-test");
101 icon = g_app_info_get_icon (appinfo);
102 g_assert (G_IS_THEMED_ICON (icon));
103 icon2 = g_themed_icon_new ("testicon");
104 g_assert (g_icon_equal (icon, icon2));
105 g_object_unref (icon2);
107 appinfo2 = g_app_info_dup (appinfo);
108 g_assert (g_app_info_get_id (appinfo) == g_app_info_get_id (appinfo2));
109 g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, g_app_info_get_commandline (appinfo2));
111 g_object_unref (appinfo);
112 g_object_unref (appinfo2);
120 g_desktop_app_info_set_desktop_env ("GNOME");
122 appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (SRCDIR "/appinfo-test.desktop");
123 g_assert (g_app_info_should_show (appinfo));
124 g_object_unref (appinfo);
126 appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (SRCDIR "/appinfo-test-gnome.desktop");
127 g_assert (g_app_info_should_show (appinfo));
128 g_object_unref (appinfo);
130 appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (SRCDIR "/appinfo-test-notgnome.desktop");
131 g_assert (!g_app_info_should_show (appinfo));
132 g_object_unref (appinfo);
136 test_commandline (void)
142 appinfo = g_app_info_create_from_commandline ("./appinfo-test --option",
144 G_APP_INFO_CREATE_SUPPORTS_URIS,
146 g_assert (appinfo != NULL);
147 g_assert_no_error (error);
148 g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "cmdline-app-test");
149 g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, "./appinfo-test --option %u");
150 g_assert (g_app_info_supports_uris (appinfo));
151 g_assert (!g_app_info_supports_files (appinfo));
153 g_object_unref (appinfo);
156 appinfo = g_app_info_create_from_commandline ("./appinfo-test --option",
158 G_APP_INFO_CREATE_NONE,
160 g_assert (appinfo != NULL);
161 g_assert_no_error (error);
162 g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "cmdline-app-test");
163 g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, "./appinfo-test --option %f");
164 g_assert (!g_app_info_supports_uris (appinfo));
165 g_assert (g_app_info_supports_files (appinfo));
167 g_object_unref (appinfo);
171 test_launch_context (void)
173 GAppLaunchContext *context;
177 context = g_app_launch_context_new ();
178 appinfo = g_app_info_create_from_commandline ("./appinfo-test --option",
180 G_APP_INFO_CREATE_SUPPORTS_URIS,
183 str = g_app_launch_context_get_display (context, appinfo, NULL);
184 g_assert (str == NULL);
186 str = g_app_launch_context_get_startup_notify_id (context, appinfo, NULL);
187 g_assert (str == NULL);
189 g_object_unref (appinfo);
190 g_object_unref (context);
198 appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (SRCDIR "/appinfo-test2.desktop");
200 g_assert (appinfo == NULL);
203 /* Test that we can set an appinfo as default for a mime type or
204 * file extension, and also add and remove handled mime types.
207 test_associations (void)
215 appinfo = g_app_info_create_from_commandline ("./appinfo-test --option",
217 G_APP_INFO_CREATE_SUPPORTS_URIS,
221 result = g_app_info_set_as_default_for_type (appinfo, "application/x-glib-test", &error);
224 g_assert_no_error (error);
226 appinfo2 = g_app_info_get_default_for_type ("application/x-glib-test", FALSE);
229 g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, g_app_info_get_commandline (appinfo2));
231 g_object_unref (appinfo2);
233 result = g_app_info_set_as_default_for_extension (appinfo, "gio-tests", &error);
235 g_assert_no_error (error);
237 appinfo2 = g_app_info_get_default_for_type ("application/x-extension-gio-tests", FALSE);
240 g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, g_app_info_get_commandline (appinfo2));
242 g_object_unref (appinfo2);
244 result = g_app_info_add_supports_type (appinfo, "application/x-gio-test", &error);
246 g_assert_no_error (error);
248 list = g_app_info_get_all_for_type ("application/x-gio-test");
249 g_assert_cmpint (g_list_length (list), ==, 1);
250 appinfo2 = list->data;
251 g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, g_app_info_get_commandline (appinfo2));
252 g_object_unref (appinfo2);
255 g_assert (g_app_info_can_remove_supports_type (appinfo));
256 g_assert (g_app_info_remove_supports_type (appinfo, "application/x-gio-test", &error));
257 g_assert_no_error (error);
259 g_assert (g_app_info_can_delete (appinfo));
260 g_assert (g_app_info_delete (appinfo));
261 g_object_unref (appinfo);
265 test_environment (void)
267 GAppLaunchContext *ctx;
273 path = g_getenv ("PATH");
275 ctx = g_app_launch_context_new ();
277 env = g_app_launch_context_get_environment (ctx);
279 g_assert (g_environ_getenv (env, "FOO") == NULL);
280 g_assert (g_environ_getenv (env, "BAR") == NULL);
281 g_assert_cmpstr (g_environ_getenv (env, "PATH"), ==, path);
285 g_app_launch_context_setenv (ctx, "FOO", "bar");
286 g_app_launch_context_setenv (ctx, "BLA", "bla");
288 env = g_app_launch_context_get_environment (ctx);
290 g_assert_cmpstr (g_environ_getenv (env, "FOO"), ==, "bar");
291 g_assert_cmpstr (g_environ_getenv (env, "BLA"), ==, "bla");
292 g_assert_cmpstr (g_environ_getenv (env, "PATH"), ==, path);
296 g_app_launch_context_setenv (ctx, "FOO", "baz");
297 g_app_launch_context_unsetenv (ctx, "BLA");
299 env = g_app_launch_context_get_environment (ctx);
301 g_assert_cmpstr (g_environ_getenv (env, "FOO"), ==, "baz");
302 g_assert (g_environ_getenv (env, "BLA") == NULL);
306 g_object_unref (ctx);
310 test_startup_wm_class (void)
312 GDesktopAppInfo *appinfo;
313 const char *wm_class;
315 appinfo = g_desktop_app_info_new_from_filename (SRCDIR "/appinfo-test.desktop");
316 wm_class = g_desktop_app_info_get_startup_wm_class (appinfo);
318 g_assert_cmpstr (wm_class, ==, "appinfo-class");
320 g_object_unref (appinfo);
324 test_supported_types (void)
327 const char * const *content_types;
329 appinfo = G_APP_INFO (g_desktop_app_info_new_from_filename (SRCDIR "/appinfo-test.desktop"));
330 content_types = g_app_info_get_supported_types (appinfo);
332 g_assert_cmpint (g_strv_length ((char**)content_types), ==, 2);
333 g_assert_cmpstr (content_types[0], ==, "image/png");
335 g_object_unref (appinfo);
339 test_from_keyfile (void)
341 GDesktopAppInfo *info;
343 GError *error = NULL;
344 const gchar *categories;
349 kf = g_key_file_new ();
350 g_key_file_load_from_file (kf,
351 SRCDIR "/appinfo-test.desktop",
354 g_assert_no_error (error);
355 info = g_desktop_app_info_new_from_keyfile (kf);
356 g_key_file_free (kf);
357 g_assert (info != NULL);
359 g_object_get (info, "filename", &file, NULL);
360 g_assert (file == NULL);
362 file = g_desktop_app_info_get_filename (info);
363 g_assert (file == NULL);
364 categories = g_desktop_app_info_get_categories (info);
365 g_assert_cmpstr (categories, ==, "GNOME;GTK;");
366 keywords = (gchar **)g_desktop_app_info_get_keywords (info);
367 g_assert_cmpint (g_strv_length (keywords), ==, 2);
368 g_assert_cmpstr (keywords[0], ==, "keyword1");
369 g_assert_cmpstr (keywords[1], ==, "test keyword");
370 name = g_desktop_app_info_get_generic_name (info);
371 g_assert_cmpstr (name, ==, "generic-appinfo-test");
372 g_assert (!g_desktop_app_info_get_nodisplay (info));
374 g_object_unref (info);
378 main (int argc, char *argv[])
381 g_test_init (&argc, &argv, NULL);
383 g_test_add_func ("/appinfo/basic", test_basic);
384 g_test_add_func ("/appinfo/text", test_text);
385 g_test_add_func ("/appinfo/launch", test_launch);
386 g_test_add_func ("/appinfo/show-in", test_show_in);
387 g_test_add_func ("/appinfo/commandline", test_commandline);
388 g_test_add_func ("/appinfo/launch-context", test_launch_context);
389 g_test_add_func ("/appinfo/tryexec", test_tryexec);
390 g_test_add_func ("/appinfo/associations", test_associations);
391 g_test_add_func ("/appinfo/environment", test_environment);
392 g_test_add_func ("/appinfo/startup-wm-class", test_startup_wm_class);
393 g_test_add_func ("/appinfo/supported-types", test_supported_types);
394 g_test_add_func ("/appinfo/from-keyfile", test_from_keyfile);
396 return g_test_run ();