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);
200 appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (SRCDIR "/appinfo-test2.desktop");
202 g_assert (appinfo == NULL);
205 /* Test that we can set an appinfo as default for a mime type or
206 * file extension, and also add and remove handled mime types.
209 test_associations (void)
217 appinfo = g_app_info_create_from_commandline ("./appinfo-test --option",
219 G_APP_INFO_CREATE_SUPPORTS_URIS,
223 result = g_app_info_set_as_default_for_type (appinfo, "application/x-glib-test", &error);
226 g_assert_no_error (error);
228 appinfo2 = g_app_info_get_default_for_type ("application/x-glib-test", FALSE);
231 g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, g_app_info_get_commandline (appinfo2));
233 g_object_unref (appinfo2);
235 result = g_app_info_set_as_default_for_extension (appinfo, "gio-tests", &error);
237 g_assert_no_error (error);
239 appinfo2 = g_app_info_get_default_for_type ("application/x-extension-gio-tests", FALSE);
242 g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, g_app_info_get_commandline (appinfo2));
244 g_object_unref (appinfo2);
246 result = g_app_info_add_supports_type (appinfo, "application/x-gio-test", &error);
248 g_assert_no_error (error);
250 list = g_app_info_get_all_for_type ("application/x-gio-test");
251 g_assert_cmpint (g_list_length (list), ==, 1);
252 appinfo2 = list->data;
253 g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, g_app_info_get_commandline (appinfo2));
254 g_object_unref (appinfo2);
257 g_assert (g_app_info_can_remove_supports_type (appinfo));
258 g_assert (g_app_info_remove_supports_type (appinfo, "application/x-gio-test", &error));
259 g_assert_no_error (error);
261 g_assert (g_app_info_can_delete (appinfo));
262 g_assert (g_app_info_delete (appinfo));
263 g_object_unref (appinfo);
267 test_environment (void)
269 GAppLaunchContext *ctx;
275 path = g_getenv ("PATH");
277 ctx = g_app_launch_context_new ();
279 env = g_app_launch_context_get_environment (ctx);
281 g_assert (g_environ_getenv (env, "FOO") == NULL);
282 g_assert (g_environ_getenv (env, "BAR") == NULL);
283 g_assert_cmpstr (g_environ_getenv (env, "PATH"), ==, path);
287 g_app_launch_context_setenv (ctx, "FOO", "bar");
288 g_app_launch_context_setenv (ctx, "BLA", "bla");
290 env = g_app_launch_context_get_environment (ctx);
292 g_assert_cmpstr (g_environ_getenv (env, "FOO"), ==, "bar");
293 g_assert_cmpstr (g_environ_getenv (env, "BLA"), ==, "bla");
294 g_assert_cmpstr (g_environ_getenv (env, "PATH"), ==, path);
298 g_app_launch_context_setenv (ctx, "FOO", "baz");
299 g_app_launch_context_unsetenv (ctx, "BLA");
301 env = g_app_launch_context_get_environment (ctx);
303 g_assert_cmpstr (g_environ_getenv (env, "FOO"), ==, "baz");
304 g_assert (g_environ_getenv (env, "BLA") == NULL);
308 g_object_unref (ctx);
312 test_startup_wm_class (void)
314 GDesktopAppInfo *appinfo;
315 const char *wm_class;
317 appinfo = g_desktop_app_info_new_from_filename (SRCDIR "/appinfo-test.desktop");
318 wm_class = g_desktop_app_info_get_startup_wm_class (appinfo);
320 g_assert_cmpstr (wm_class, ==, "appinfo-class");
322 g_object_unref (appinfo);
326 test_supported_types (void)
329 const char * const *content_types;
331 appinfo = G_APP_INFO (g_desktop_app_info_new_from_filename (SRCDIR "/appinfo-test.desktop"));
332 content_types = g_app_info_get_supported_types (appinfo);
334 g_assert_cmpint (g_strv_length ((char**)content_types), ==, 2);
335 g_assert_cmpstr (content_types[0], ==, "image/png");
337 g_object_unref (appinfo);
341 test_from_keyfile (void)
343 GDesktopAppInfo *info;
345 GError *error = NULL;
346 const gchar *categories;
351 kf = g_key_file_new ();
352 g_key_file_load_from_file (kf,
353 SRCDIR "/appinfo-test.desktop",
356 g_assert_no_error (error);
357 info = g_desktop_app_info_new_from_keyfile (kf);
358 g_key_file_free (kf);
359 g_assert (info != NULL);
361 g_object_get (info, "filename", &file, NULL);
362 g_assert (file == NULL);
364 file = g_desktop_app_info_get_filename (info);
365 g_assert (file == NULL);
366 categories = g_desktop_app_info_get_categories (info);
367 g_assert_cmpstr (categories, ==, "GNOME;GTK;");
368 keywords = (gchar **)g_desktop_app_info_get_keywords (info);
369 g_assert_cmpint (g_strv_length (keywords), ==, 2);
370 g_assert_cmpstr (keywords[0], ==, "keyword1");
371 g_assert_cmpstr (keywords[1], ==, "test keyword");
372 name = g_desktop_app_info_get_generic_name (info);
373 g_assert_cmpstr (name, ==, "generic-appinfo-test");
374 g_assert (!g_desktop_app_info_get_nodisplay (info));
376 g_object_unref (info);
380 main (int argc, char *argv[])
383 g_test_init (&argc, &argv, NULL);
385 g_test_add_func ("/appinfo/basic", test_basic);
386 g_test_add_func ("/appinfo/text", test_text);
387 g_test_add_func ("/appinfo/launch", test_launch);
388 g_test_add_func ("/appinfo/show-in", test_show_in);
389 g_test_add_func ("/appinfo/commandline", test_commandline);
390 g_test_add_func ("/appinfo/launch-context", test_launch_context);
391 g_test_add_func ("/appinfo/tryexec", test_tryexec);
392 g_test_add_func ("/appinfo/associations", test_associations);
393 g_test_add_func ("/appinfo/environment", test_environment);
394 g_test_add_func ("/appinfo/startup-wm-class", test_startup_wm_class);
395 g_test_add_func ("/appinfo/supported-types", test_supported_types);
396 g_test_add_func ("/appinfo/from-keyfile", test_from_keyfile);
398 return g_test_run ();