2 * Copyright (C) 2008 Red Hat, Inc
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General
15 * Public License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17 * Boston, MA 02111-1307, USA.
19 * Author: Matthias Clasen
22 #include <glib/glib.h>
24 #include <gio/gdesktopappinfo.h>
32 create_app_info (const char *name)
38 info = g_app_info_create_from_commandline ("true blah",
40 G_APP_INFO_CREATE_NONE,
42 g_assert (error == NULL);
44 /* this is necessary to ensure that the info is saved */
45 g_app_info_set_as_default_for_type (info, "application/x-blah", &error);
46 g_assert (error == NULL);
47 g_app_info_remove_supports_type (info, "application/x-blah", &error);
48 g_assert (error == NULL);
49 g_app_info_reset_type_associations ("application/x-blah");
63 info = create_app_info ("Blah");
65 id = g_app_info_get_id (info);
66 g_assert (id != NULL);
68 filename = g_build_filename (basedir, "applications", id, NULL);
70 res = g_file_test (filename, G_FILE_TEST_EXISTS);
73 res = g_app_info_can_delete (info);
76 res = g_app_info_delete (info);
79 res = g_file_test (filename, G_FILE_TEST_EXISTS);
82 g_object_unref (info);
84 if (g_file_test ("/usr/share/applications/gedit.desktop", G_FILE_TEST_EXISTS))
86 info = (GAppInfo*)g_desktop_app_info_new_from_filename ("/usr/share/applications/gedit.desktop");
89 res = g_app_info_can_delete (info);
92 res = g_app_info_delete (info);
100 GAppInfo *info, *info1, *info2, *info3;
102 GError *error = NULL;
104 info1 = create_app_info ("Blah1");
105 info2 = create_app_info ("Blah2");
106 info3 = create_app_info ("Blah3");
108 g_app_info_set_as_default_for_type (info1, "application/x-test", &error);
109 g_assert (error == NULL);
111 g_app_info_set_as_default_for_type (info2, "application/x-test", &error);
112 g_assert (error == NULL);
114 list = g_app_info_get_all_for_type ("application/x-test");
115 g_assert (g_list_length (list) == 2);
117 /* check that both are in the list, info2 before info1 */
118 info = (GAppInfo *)list->data;
119 g_assert (g_strcmp0 (g_app_info_get_id (info), g_app_info_get_id (info2)) == 0);
121 info = (GAppInfo *)list->next->data;
122 g_assert (g_strcmp0 (g_app_info_get_id (info), g_app_info_get_id (info1)) == 0);
124 g_list_free_full (list, g_object_unref);
126 /* now try adding something at the end */
127 g_app_info_add_supports_type (info3, "application/x-test", &error);
128 g_assert (error == NULL);
130 list = g_app_info_get_all_for_type ("application/x-test");
131 g_assert (g_list_length (list) == 3);
133 /* check that all are in the list, info2, info1, info3 */
134 info = (GAppInfo *)list->data;
135 g_assert (g_strcmp0 (g_app_info_get_id (info), g_app_info_get_id (info2)) == 0);
137 info = (GAppInfo *)list->next->data;
138 g_assert (g_strcmp0 (g_app_info_get_id (info), g_app_info_get_id (info1)) == 0);
140 info = (GAppInfo *)list->next->next->data;
141 g_assert (g_strcmp0 (g_app_info_get_id (info), g_app_info_get_id (info3)) == 0);
143 g_list_free_full (list, g_object_unref);
145 /* now remove info1 again */
146 g_app_info_remove_supports_type (info1, "application/x-test", &error);
147 g_assert (error == NULL);
149 list = g_app_info_get_all_for_type ("application/x-test");
150 g_assert (g_list_length (list) == 2);
152 /* check that both are in the list, info2 before info3 */
153 info = (GAppInfo *)list->data;
154 g_assert (g_strcmp0 (g_app_info_get_id (info), g_app_info_get_id (info2)) == 0);
156 info = (GAppInfo *)list->next->data;
157 g_assert (g_strcmp0 (g_app_info_get_id (info), g_app_info_get_id (info3)) == 0);
159 g_list_free_full (list, g_object_unref);
161 /* now clean it all up */
162 g_app_info_reset_type_associations ("application/x-test");
164 list = g_app_info_get_all_for_type ("application/x-test");
165 g_assert (list == NULL);
167 g_app_info_delete (info1);
168 g_app_info_delete (info2);
169 g_app_info_delete (info3);
171 g_object_unref (info1);
172 g_object_unref (info2);
178 GAppInfo *info1, *info2, *app;
179 GList *apps, *recomm, *fallback, *list, *l, *m;
180 GError *error = NULL;
183 info1 = create_app_info ("Test1");
184 info2 = create_app_info ("Test2");
186 g_assert (g_content_type_is_a ("text/x-python", "text/plain"));
188 apps = g_app_info_get_all_for_type ("text/x-python");
189 old_length = g_list_length (apps);
190 g_list_free_full (apps, g_object_unref);
192 g_app_info_set_as_default_for_type (info1, "text/x-python", &error);
193 g_assert (error == NULL);
195 g_app_info_set_as_default_for_type (info2, "text/plain", &error);
196 g_assert (error == NULL);
198 /* check that both apps are registered */
199 apps = g_app_info_get_all_for_type ("text/x-python");
200 g_assert (g_list_length (apps) == old_length + 2);
202 /* check the ordering */
203 app = g_list_nth_data (apps, 0);
204 g_assert (g_app_info_equal (info1, app));
206 /* check that Test1 is the first recommended app */
207 recomm = g_app_info_get_recommended_for_type ("text/x-python");
208 g_assert (recomm != NULL);
209 app = g_list_nth_data (recomm, 0);
210 g_assert (g_app_info_equal (info1, app));
212 /* and that Test2 is the first fallback */
213 fallback = g_app_info_get_fallback_for_type ("text/x-python");
214 g_assert (fallback != NULL);
215 app = g_list_nth_data (fallback, 0);
216 g_assert (g_app_info_equal (info2, app));
218 /* check that recomm + fallback = all applications */
219 list = g_list_concat (g_list_copy (recomm), g_list_copy (fallback));
220 g_assert (g_list_length (list) == g_list_length (apps));
222 for (l = list, m = apps; l != NULL && m != NULL; l = l->next, m = m->next)
224 g_assert (g_app_info_equal (l->data, m->data));
229 g_list_free_full (apps, g_object_unref);
230 g_list_free_full (recomm, g_object_unref);
231 g_list_free_full (fallback, g_object_unref);
233 g_app_info_reset_type_associations ("text/x-python");
234 g_app_info_reset_type_associations ("text/plain");
236 g_app_info_delete (info1);
237 g_app_info_delete (info2);
239 g_object_unref (info1);
240 g_object_unref (info2);
244 test_last_used (void)
247 GAppInfo *info1, *info2, *default_app;
248 GError *error = NULL;
250 info1 = create_app_info ("Test1");
251 info2 = create_app_info ("Test2");
253 g_app_info_set_as_default_for_type (info1, "application/x-test", &error);
254 g_assert (error == NULL);
256 g_app_info_add_supports_type (info2, "application/x-test", &error);
257 g_assert (error == NULL);
259 applications = g_app_info_get_recommended_for_type ("application/x-test");
260 g_assert (g_list_length (applications) == 2);
262 /* the first should be the default app now */
263 g_assert (g_app_info_equal (g_list_nth_data (applications, 0), info1));
264 g_assert (g_app_info_equal (g_list_nth_data (applications, 1), info2));
266 g_list_free_full (applications, g_object_unref);
268 g_app_info_set_as_last_used_for_type (info2, "application/x-test", &error);
269 g_assert (error == NULL);
271 applications = g_app_info_get_recommended_for_type ("application/x-test");
272 g_assert (g_list_length (applications) == 2);
274 default_app = g_app_info_get_default_for_type ("application/x-test", FALSE);
275 g_assert (g_app_info_equal (default_app, info1));
277 /* the first should be the other app now */
278 g_assert (g_app_info_equal (g_list_nth_data (applications, 0), info2));
279 g_assert (g_app_info_equal (g_list_nth_data (applications, 1), info1));
281 g_list_free_full (applications, g_object_unref);
283 g_app_info_reset_type_associations ("application/x-test");
285 g_app_info_delete (info1);
286 g_app_info_delete (info2);
288 g_object_unref (info1);
289 g_object_unref (info2);
290 g_object_unref (default_app);
294 cleanup_dir_recurse (GFile *parent, GFile *root)
298 GFileEnumerator *enumerator;
303 g_assert (root != NULL);
307 g_file_enumerate_children (parent, "*",
308 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL,
313 info = g_file_enumerator_next_file (enumerator, NULL, &error);
314 while ((info) && (!error))
316 descend = g_file_get_child (parent, g_file_info_get_name (info));
317 g_assert (descend != NULL);
318 relative_path = g_file_get_relative_path (root, descend);
319 g_assert (relative_path != NULL);
321 if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
322 cleanup_dir_recurse (descend, root);
325 res = g_file_delete (descend, NULL, &error);
326 g_assert_cmpint (res, ==, TRUE);
328 g_object_unref (descend);
330 info = g_file_enumerator_next_file (enumerator, NULL, &error);
332 g_assert (error == NULL);
335 res = g_file_enumerator_close (enumerator, NULL, &error);
336 g_assert_cmpint (res, ==, TRUE);
337 g_assert (error == NULL);
341 cleanup_subdirs (const char *base_dir)
345 base = g_file_new_for_path (base_dir);
346 file = g_file_get_child (base, "applications");
347 cleanup_dir_recurse (file, file);
348 g_object_unref (file);
349 file = g_file_get_child (base, "mime");
350 cleanup_dir_recurse (file, file);
351 g_object_unref (file);
355 test_extra_getters (void)
357 GDesktopAppInfo *appinfo;
361 appinfo = g_desktop_app_info_new_from_filename (g_test_get_filename (G_TEST_DIST, "appinfo-test.desktop", NULL));
362 g_assert (appinfo != NULL);
364 g_assert (g_desktop_app_info_has_key (appinfo, "Terminal"));
365 g_assert (!g_desktop_app_info_has_key (appinfo, "Bratwurst"));
367 s = g_desktop_app_info_get_string (appinfo, "StartupWMClass");
368 g_assert_cmpstr (s, ==, "appinfo-class");
371 b = g_desktop_app_info_get_boolean (appinfo, "Terminal");
374 g_object_unref (appinfo);
378 wait_for_file (const gchar *want_this,
379 const gchar *but_not_this,
380 const gchar *or_this)
384 /* I hate time-based conditions in tests, but this will wait up to one
385 * whole minute for "touch file" to finish running. I think it should
388 * 600 * 100ms = 60 seconds.
390 while (access (want_this, F_OK) != 0)
392 g_usleep (100000); /* 100ms */
397 g_assert (access (but_not_this, F_OK) != 0);
398 g_assert (access (or_this, F_OK) != 0);
401 unlink (but_not_this);
408 const gchar * const *actions;
409 GDesktopAppInfo *appinfo;
412 appinfo = g_desktop_app_info_new_from_filename (g_test_get_filename (G_TEST_DIST, "appinfo-test-actions.desktop", NULL));
413 g_assert (appinfo != NULL);
415 actions = g_desktop_app_info_list_actions (appinfo);
416 g_assert_cmpstr (actions[0], ==, "frob");
417 g_assert_cmpstr (actions[1], ==, "tweak");
418 g_assert_cmpstr (actions[2], ==, "twiddle");
419 g_assert_cmpstr (actions[3], ==, "broken");
420 g_assert_cmpstr (actions[4], ==, NULL);
422 name = g_desktop_app_info_get_action_name (appinfo, "frob");
423 g_assert_cmpstr (name, ==, "Frobnicate");
426 name = g_desktop_app_info_get_action_name (appinfo, "tweak");
427 g_assert_cmpstr (name, ==, "Tweak");
430 name = g_desktop_app_info_get_action_name (appinfo, "twiddle");
431 g_assert_cmpstr (name, ==, "Twiddle");
434 name = g_desktop_app_info_get_action_name (appinfo, "broken");
435 g_assert (name != NULL);
436 g_assert (g_utf8_validate (name, -1, NULL));
439 unlink ("frob"); unlink ("tweak"); unlink ("twiddle");
441 g_desktop_app_info_launch_action (appinfo, "frob", NULL);
442 wait_for_file ("frob", "tweak", "twiddle");
444 g_desktop_app_info_launch_action (appinfo, "tweak", NULL);
445 wait_for_file ("tweak", "frob", "twiddle");
447 g_desktop_app_info_launch_action (appinfo, "twiddle", NULL);
448 wait_for_file ("twiddle", "frob", "tweak");
450 g_object_unref (appinfo);
454 run_apps (const gchar *command,
458 const gchar *locale_name,
459 const gchar *language)
467 argv = g_new (gchar *, 4);
468 argv[0] = g_test_build_filename (G_TEST_BUILT, "apps", NULL);
469 argv[1] = g_strdup (command);
470 argv[2] = g_strdup (arg);
473 envp = g_get_environ ();
477 gchar *tmp = g_test_build_filename (G_TEST_DIST, "desktop-files", "usr", NULL);
478 envp = g_environ_setenv (envp, "XDG_DATA_DIRS", tmp, TRUE);
482 envp = g_environ_setenv (envp, "XDG_DATA_DIRS", "/does-not-exist", TRUE);
486 gchar *tmp = g_test_build_filename (G_TEST_DIST, "desktop-files", "home", NULL);
487 envp = g_environ_setenv (envp, "XDG_DATA_HOME", tmp, TRUE);
491 envp = g_environ_setenv (envp, "XDG_DATA_HOME", "/does-not-exist", TRUE);
494 envp = g_environ_setenv (envp, "LC_ALL", locale_name, TRUE);
496 envp = g_environ_setenv (envp, "LC_ALL", "C", TRUE);
499 envp = g_environ_setenv (envp, "LANGUAGE", language, TRUE);
501 envp = g_environ_unsetenv (envp, "LANGUAGE");
503 success = g_spawn_sync (NULL, argv, envp, 0, NULL, NULL, &out, NULL, &status, NULL);
505 g_assert (status == 0);
514 assert_strings_equivalent (const gchar *expected,
517 gchar **expected_words;
518 gchar **result_words;
521 expected_words = g_strsplit (expected, " ", 0);
522 result_words = g_strsplit (result, " ", 0);
524 for (i = 0; expected_words[i]; i++)
526 for (j = 0; result_words[j]; j++)
527 if (g_str_equal (expected_words[i], result_words[j]))
530 g_test_message ("Unable to find expected string '%s' in result '%s'", expected_words[i], result);
537 g_assert_cmpint (g_strv_length (expected_words), ==, g_strv_length (result_words));
538 g_strfreev (expected_words);
539 g_strfreev (result_words);
543 assert_list (const gchar *expected,
546 const gchar *locale_name,
547 const gchar *language)
551 result = run_apps ("list", NULL, with_usr, with_home, locale_name, language);
553 assert_strings_equivalent (expected, result);
558 assert_info (const gchar *desktop_id,
559 const gchar *expected,
562 const gchar *locale_name,
563 const gchar *language)
567 result = run_apps ("show-info", desktop_id, with_usr, with_home, locale_name, language);
568 g_assert_cmpstr (result, ==, expected);
573 assert_search (const gchar *search_string,
574 const gchar *expected,
577 const gchar *locale_name,
578 const gchar *language)
580 gchar **expected_lines;
581 gchar **result_lines;
585 expected_lines = g_strsplit (expected, "\n", -1);
586 result = run_apps ("search", search_string, with_usr, with_home, locale_name, language);
587 result_lines = g_strsplit (result, "\n", -1);
588 g_assert_cmpint (g_strv_length (expected_lines), ==, g_strv_length (result_lines));
589 for (i = 0; expected_lines[i]; i++)
590 assert_strings_equivalent (expected_lines[i], result_lines[i]);
591 g_strfreev (expected_lines);
592 g_strfreev (result_lines);
596 #define ALL_USR_APPS "evince-previewer.desktop nautilus-classic.desktop gnome-font-viewer.desktop " \
597 "baobab.desktop yelp.desktop eog.desktop cheese.desktop gnome-clocks.desktop " \
598 "gnome-contacts.desktop kde4-kate.desktop gcr-prompter.desktop totem.desktop " \
599 "gnome-terminal.desktop nautilus-autorun-software.desktop gcr-viewer.desktop " \
600 "nautilus-connect-server.desktop kde4-dolphin.desktop gnome-music.desktop " \
601 "kde4-konqbrowser.desktop gucharmap.desktop kde4-okular.desktop nautilus.desktop " \
602 "gedit.desktop evince.desktop file-roller.desktop dconf-editor.desktop glade.desktop"
603 #define HOME_APPS "epiphany-weather-for-toronto-island-9c6a4e022b17686306243dada811d550d25eb1fb.desktop"
604 #define ALL_HOME_APPS HOME_APPS " eog.desktop"
609 assert_list ("", FALSE, FALSE, NULL, NULL);
610 assert_list (ALL_USR_APPS, TRUE, FALSE, NULL, NULL);
611 assert_list (ALL_HOME_APPS, FALSE, TRUE, NULL, NULL);
612 assert_list (ALL_USR_APPS " " HOME_APPS, TRUE, TRUE, NULL, NULL);
614 /* The user has "installed" their own version of eog.desktop which
615 * calls it "Eye of GNOME". Do some testing based on that.
617 * We should always find "Pictures" keyword no matter where we look.
619 assert_search ("Picture", "eog.desktop\n", TRUE, TRUE, NULL, NULL);
620 assert_search ("Picture", "eog.desktop\n", TRUE, FALSE, NULL, NULL);
621 assert_search ("Picture", "eog.desktop\n", FALSE, TRUE, NULL, NULL);
622 assert_search ("Picture", "", FALSE, FALSE, NULL, NULL);
624 /* We should only find it called "eye of gnome" when using the user's
627 assert_search ("eye gnome", "", TRUE, FALSE, NULL, NULL);
628 assert_search ("eye gnome", "eog.desktop\n", FALSE, TRUE, NULL, NULL);
629 assert_search ("eye gnome", "eog.desktop\n", TRUE, TRUE, NULL, NULL);
631 /* We should only find it called "image viewer" when _not_ using the
634 assert_search ("image viewer", "eog.desktop\n", TRUE, FALSE, NULL, NULL);
635 assert_search ("image viewer", "", FALSE, TRUE, NULL, NULL);
636 assert_search ("image viewer", "", TRUE, TRUE, NULL, NULL);
638 /* Obvious multi-word search */
639 assert_search ("gno hel", "yelp.desktop\n", TRUE, TRUE, NULL, NULL);
641 /* Repeated search terms should do nothing... */
642 assert_search ("files file fil fi f", "nautilus.desktop\n"
643 "gedit.desktop\n", TRUE, TRUE, NULL, NULL);
645 /* "con" will match "connect" and "contacts" on name but dconf only on
646 * the "config" keyword
648 assert_search ("con", "nautilus-connect-server.desktop gnome-contacts.desktop\n"
649 "dconf-editor.desktop\n", TRUE, TRUE, NULL, NULL);
651 /* "gnome" will match "eye of gnome" from the user's directory, plus
652 * matching "GNOME Clocks" X-GNOME-FullName. It's only a comment on
653 * yelp and gnome-contacts, though.
655 assert_search ("gnome", "eog.desktop\n"
656 "gnome-clocks.desktop\n"
657 "yelp.desktop gnome-contacts.desktop\n", TRUE, TRUE, NULL, NULL);
659 /* "gnome con" will match only gnome contacts; via the name for
660 * "contacts" and the comment for "gnome"
662 assert_search ("gnome con", "gnome-contacts.desktop\n", TRUE, TRUE, NULL, NULL);
664 /* make sure we get the correct kde4- prefix on the application IDs
665 * from subdirectories
667 assert_search ("konq", "kde4-konqbrowser.desktop\n", TRUE, TRUE, NULL, NULL);
668 assert_search ("kate", "kde4-kate.desktop\n", TRUE, TRUE, NULL, NULL);
670 /* make sure we can lookup apps by name properly */
671 assert_info ("kde4-kate.desktop",
672 "kde4-kate.desktop\n"
675 "nil\n", TRUE, TRUE, NULL, NULL);
677 assert_info ("nautilus.desktop",
681 "Access and organize files\n", TRUE, TRUE, NULL, NULL);
683 /* make sure localised searching works properly */
684 assert_search ("foliumi", "nautilus.desktop\n"
685 "kde4-konqbrowser.desktop\n"
686 "eog.desktop\n", TRUE, FALSE, "en_US.UTF-8", "eo");
687 /* the user's eog.desktop has no translations... */
688 assert_search ("foliumi", "nautilus.desktop\n"
689 "kde4-konqbrowser.desktop\n", TRUE, TRUE, "en_US.UTF-8", "eo");
698 g_test_init (&argc, &argv, NULL);
700 basedir = g_get_current_dir ();
701 g_setenv ("XDG_DATA_HOME", basedir, TRUE);
702 cleanup_subdirs (basedir);
704 g_test_add_func ("/desktop-app-info/delete", test_delete);
705 g_test_add_func ("/desktop-app-info/default", test_default);
706 g_test_add_func ("/desktop-app-info/fallback", test_fallback);
707 g_test_add_func ("/desktop-app-info/lastused", test_last_used);
708 g_test_add_func ("/desktop-app-info/extra-getters", test_extra_getters);
709 g_test_add_func ("/desktop-app-info/actions", test_actions);
710 g_test_add_func ("/desktop-app-info/search", test_search);
712 result = g_test_run ();
714 cleanup_subdirs (basedir);