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, see <http://www.gnu.org/licenses/>.
17 * Author: Matthias Clasen
20 #include <glib/glib.h>
22 #include <gio/gdesktopappinfo.h>
30 create_app_info (const char *name)
36 info = g_app_info_create_from_commandline ("true blah",
38 G_APP_INFO_CREATE_NONE,
40 g_assert (error == NULL);
42 /* this is necessary to ensure that the info is saved */
43 g_app_info_set_as_default_for_type (info, "application/x-blah", &error);
44 g_assert (error == NULL);
45 g_app_info_remove_supports_type (info, "application/x-blah", &error);
46 g_assert (error == NULL);
47 g_app_info_reset_type_associations ("application/x-blah");
61 info = create_app_info ("Blah");
63 id = g_app_info_get_id (info);
64 g_assert (id != NULL);
66 filename = g_build_filename (basedir, "applications", id, NULL);
68 res = g_file_test (filename, G_FILE_TEST_EXISTS);
71 res = g_app_info_can_delete (info);
74 res = g_app_info_delete (info);
77 res = g_file_test (filename, G_FILE_TEST_EXISTS);
80 g_object_unref (info);
82 if (g_file_test ("/usr/share/applications/gedit.desktop", G_FILE_TEST_EXISTS))
84 info = (GAppInfo*)g_desktop_app_info_new_from_filename ("/usr/share/applications/gedit.desktop");
87 res = g_app_info_can_delete (info);
90 res = g_app_info_delete (info);
98 GAppInfo *info, *info1, *info2, *info3;
100 GError *error = NULL;
102 info1 = create_app_info ("Blah1");
103 info2 = create_app_info ("Blah2");
104 info3 = create_app_info ("Blah3");
106 g_app_info_set_as_default_for_type (info1, "application/x-test", &error);
107 g_assert (error == NULL);
109 g_app_info_set_as_default_for_type (info2, "application/x-test", &error);
110 g_assert (error == NULL);
112 info = g_app_info_get_default_for_type ("application/x-test", FALSE);
113 g_assert (info != NULL);
114 g_assert_cmpstr (g_app_info_get_id (info), ==, g_app_info_get_id (info2));
116 /* now try adding something, but not setting as default */
117 g_app_info_add_supports_type (info3, "application/x-test", &error);
118 g_assert (error == NULL);
120 /* check that info2 is still default */
121 info = g_app_info_get_default_for_type ("application/x-test", FALSE);
122 g_assert (info != NULL);
123 g_assert_cmpstr (g_app_info_get_id (info), ==, g_app_info_get_id (info2));
125 /* now remove info1 again */
126 g_app_info_remove_supports_type (info1, "application/x-test", &error);
127 g_assert (error == NULL);
129 /* and make sure info2 is still default */
130 info = g_app_info_get_default_for_type ("application/x-test", FALSE);
131 g_assert (info != NULL);
132 g_assert_cmpstr (g_app_info_get_id (info), ==, g_app_info_get_id (info2));
134 /* now clean it all up */
135 g_app_info_reset_type_associations ("application/x-test");
137 list = g_app_info_get_all_for_type ("application/x-test");
138 g_assert (list == NULL);
140 g_app_info_delete (info1);
141 g_app_info_delete (info2);
142 g_app_info_delete (info3);
144 g_object_unref (info1);
145 g_object_unref (info2);
151 GAppInfo *info1, *info2, *app;
152 GList *apps, *recomm, *fallback, *list, *l, *m;
153 GError *error = NULL;
156 info1 = create_app_info ("Test1");
157 info2 = create_app_info ("Test2");
159 g_assert (g_content_type_is_a ("text/x-python", "text/plain"));
161 apps = g_app_info_get_all_for_type ("text/x-python");
162 old_length = g_list_length (apps);
163 g_list_free_full (apps, g_object_unref);
165 g_app_info_add_supports_type (info1, "text/x-python", &error);
166 g_assert (error == NULL);
168 g_app_info_add_supports_type (info2, "text/plain", &error);
169 g_assert (error == NULL);
171 /* check that both apps are registered */
172 apps = g_app_info_get_all_for_type ("text/x-python");
173 g_assert_cmpint (g_list_length (apps), ==, old_length + 2);
175 /* check the ordering */
176 app = g_list_nth_data (apps, 0);
177 g_assert (g_app_info_equal (info1, app));
179 /* check that Test1 is the first recommended app */
180 recomm = g_app_info_get_recommended_for_type ("text/x-python");
181 g_assert (recomm != NULL);
182 app = g_list_nth_data (recomm, 0);
183 g_assert (g_app_info_equal (info1, app));
185 /* and that Test2 is among the fallback apps */
186 fallback = g_app_info_get_fallback_for_type ("text/x-python");
187 g_assert (fallback != NULL);
188 for (l = fallback; l; l = l->next)
191 if (g_app_info_equal (info2, app))
194 g_assert_cmpstr (g_app_info_get_name (app), ==, "Test2");
196 /* check that recomm + fallback = all applications */
197 list = g_list_concat (g_list_copy (recomm), g_list_copy (fallback));
198 g_assert (g_list_length (list) == g_list_length (apps));
200 for (l = list, m = apps; l != NULL && m != NULL; l = l->next, m = m->next)
202 g_assert (g_app_info_equal (l->data, m->data));
207 g_list_free_full (apps, g_object_unref);
208 g_list_free_full (recomm, g_object_unref);
209 g_list_free_full (fallback, g_object_unref);
211 g_app_info_reset_type_associations ("text/x-python");
212 g_app_info_reset_type_associations ("text/plain");
214 g_app_info_delete (info1);
215 g_app_info_delete (info2);
217 g_object_unref (info1);
218 g_object_unref (info2);
222 test_last_used (void)
225 GAppInfo *info1, *info2, *default_app;
226 GError *error = NULL;
228 info1 = create_app_info ("Test1");
229 info2 = create_app_info ("Test2");
231 g_app_info_set_as_default_for_type (info1, "application/x-test", &error);
232 g_assert (error == NULL);
234 g_app_info_add_supports_type (info2, "application/x-test", &error);
235 g_assert (error == NULL);
237 applications = g_app_info_get_recommended_for_type ("application/x-test");
238 g_assert (g_list_length (applications) == 2);
240 /* the first should be the default app now */
241 g_assert (g_app_info_equal (g_list_nth_data (applications, 0), info1));
242 g_assert (g_app_info_equal (g_list_nth_data (applications, 1), info2));
244 g_list_free_full (applications, g_object_unref);
246 g_app_info_set_as_last_used_for_type (info2, "application/x-test", &error);
247 g_assert (error == NULL);
249 applications = g_app_info_get_recommended_for_type ("application/x-test");
250 g_assert (g_list_length (applications) == 2);
252 default_app = g_app_info_get_default_for_type ("application/x-test", FALSE);
253 g_assert (g_app_info_equal (default_app, info1));
255 /* the first should be the other app now */
256 g_assert (g_app_info_equal (g_list_nth_data (applications, 0), info2));
257 g_assert (g_app_info_equal (g_list_nth_data (applications, 1), info1));
259 g_list_free_full (applications, g_object_unref);
261 g_app_info_reset_type_associations ("application/x-test");
263 g_app_info_delete (info1);
264 g_app_info_delete (info2);
266 g_object_unref (info1);
267 g_object_unref (info2);
268 g_object_unref (default_app);
272 cleanup_dir_recurse (GFile *parent, GFile *root)
276 GFileEnumerator *enumerator;
281 g_assert (root != NULL);
285 g_file_enumerate_children (parent, "*",
286 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL,
291 info = g_file_enumerator_next_file (enumerator, NULL, &error);
292 while ((info) && (!error))
294 descend = g_file_get_child (parent, g_file_info_get_name (info));
295 g_assert (descend != NULL);
296 relative_path = g_file_get_relative_path (root, descend);
297 g_assert (relative_path != NULL);
299 if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
300 cleanup_dir_recurse (descend, root);
303 res = g_file_delete (descend, NULL, &error);
304 g_assert_cmpint (res, ==, TRUE);
306 g_object_unref (descend);
308 info = g_file_enumerator_next_file (enumerator, NULL, &error);
310 g_assert (error == NULL);
313 res = g_file_enumerator_close (enumerator, NULL, &error);
314 g_assert_cmpint (res, ==, TRUE);
315 g_assert (error == NULL);
319 cleanup_subdirs (const char *base_dir)
323 base = g_file_new_for_path (base_dir);
324 file = g_file_get_child (base, "applications");
325 cleanup_dir_recurse (file, file);
326 g_object_unref (file);
327 file = g_file_get_child (base, "mime");
328 cleanup_dir_recurse (file, file);
329 g_object_unref (file);
333 test_extra_getters (void)
335 GDesktopAppInfo *appinfo;
339 appinfo = g_desktop_app_info_new_from_filename (g_test_get_filename (G_TEST_DIST, "appinfo-test.desktop", NULL));
340 g_assert (appinfo != NULL);
342 g_assert (g_desktop_app_info_has_key (appinfo, "Terminal"));
343 g_assert (!g_desktop_app_info_has_key (appinfo, "Bratwurst"));
345 s = g_desktop_app_info_get_string (appinfo, "StartupWMClass");
346 g_assert_cmpstr (s, ==, "appinfo-class");
349 b = g_desktop_app_info_get_boolean (appinfo, "Terminal");
352 g_object_unref (appinfo);
356 wait_for_file (const gchar *want_this,
357 const gchar *but_not_this,
358 const gchar *or_this)
362 /* I hate time-based conditions in tests, but this will wait up to one
363 * whole minute for "touch file" to finish running. I think it should
366 * 600 * 100ms = 60 seconds.
368 while (access (want_this, F_OK) != 0)
370 g_usleep (100000); /* 100ms */
375 g_assert (access (but_not_this, F_OK) != 0);
376 g_assert (access (or_this, F_OK) != 0);
379 unlink (but_not_this);
386 const gchar * const *actions;
387 GDesktopAppInfo *appinfo;
390 appinfo = g_desktop_app_info_new_from_filename (g_test_get_filename (G_TEST_DIST, "appinfo-test-actions.desktop", NULL));
391 g_assert (appinfo != NULL);
393 actions = g_desktop_app_info_list_actions (appinfo);
394 g_assert_cmpstr (actions[0], ==, "frob");
395 g_assert_cmpstr (actions[1], ==, "tweak");
396 g_assert_cmpstr (actions[2], ==, "twiddle");
397 g_assert_cmpstr (actions[3], ==, "broken");
398 g_assert_cmpstr (actions[4], ==, NULL);
400 name = g_desktop_app_info_get_action_name (appinfo, "frob");
401 g_assert_cmpstr (name, ==, "Frobnicate");
404 name = g_desktop_app_info_get_action_name (appinfo, "tweak");
405 g_assert_cmpstr (name, ==, "Tweak");
408 name = g_desktop_app_info_get_action_name (appinfo, "twiddle");
409 g_assert_cmpstr (name, ==, "Twiddle");
412 name = g_desktop_app_info_get_action_name (appinfo, "broken");
413 g_assert (name != NULL);
414 g_assert (g_utf8_validate (name, -1, NULL));
417 unlink ("frob"); unlink ("tweak"); unlink ("twiddle");
419 g_desktop_app_info_launch_action (appinfo, "frob", NULL);
420 wait_for_file ("frob", "tweak", "twiddle");
422 g_desktop_app_info_launch_action (appinfo, "tweak", NULL);
423 wait_for_file ("tweak", "frob", "twiddle");
425 g_desktop_app_info_launch_action (appinfo, "twiddle", NULL);
426 wait_for_file ("twiddle", "frob", "tweak");
428 g_object_unref (appinfo);
432 run_apps (const gchar *command,
436 const gchar *locale_name,
437 const gchar *language,
438 const gchar *xdg_current_desktop)
446 argv = g_new (gchar *, 4);
447 argv[0] = g_test_build_filename (G_TEST_BUILT, "apps", NULL);
448 argv[1] = g_strdup (command);
449 argv[2] = g_strdup (arg);
452 envp = g_get_environ ();
456 gchar *tmp = g_test_build_filename (G_TEST_DIST, "desktop-files", "usr", NULL);
457 envp = g_environ_setenv (envp, "XDG_DATA_DIRS", tmp, TRUE);
461 envp = g_environ_setenv (envp, "XDG_DATA_DIRS", "/does-not-exist", TRUE);
465 gchar *tmp = g_test_build_filename (G_TEST_DIST, "desktop-files", "home", NULL);
466 envp = g_environ_setenv (envp, "XDG_DATA_HOME", tmp, TRUE);
470 envp = g_environ_setenv (envp, "XDG_DATA_HOME", "/does-not-exist", TRUE);
473 envp = g_environ_setenv (envp, "LC_ALL", locale_name, TRUE);
475 envp = g_environ_setenv (envp, "LC_ALL", "C", TRUE);
478 envp = g_environ_setenv (envp, "LANGUAGE", language, TRUE);
480 envp = g_environ_unsetenv (envp, "LANGUAGE");
482 if (xdg_current_desktop)
483 envp = g_environ_setenv (envp, "XDG_CURRENT_DESKTOP", xdg_current_desktop, TRUE);
485 envp = g_environ_unsetenv (envp, "XDG_CURRENT_DESKTOP");
487 success = g_spawn_sync (NULL, argv, envp, 0, NULL, NULL, &out, NULL, &status, NULL);
489 g_assert (status == 0);
498 assert_strings_equivalent (const gchar *expected,
501 gchar **expected_words;
502 gchar **result_words;
505 expected_words = g_strsplit (expected, " ", 0);
506 result_words = g_strsplit_set (result, " \n", 0);
508 for (i = 0; expected_words[i]; i++)
510 for (j = 0; result_words[j]; j++)
511 if (g_str_equal (expected_words[i], result_words[j]))
514 g_test_message ("Unable to find expected string '%s' in result '%s'", expected_words[i], result);
521 g_assert_cmpint (g_strv_length (expected_words), ==, g_strv_length (result_words));
522 g_strfreev (expected_words);
523 g_strfreev (result_words);
527 assert_list (const gchar *expected,
530 const gchar *locale_name,
531 const gchar *language)
535 result = run_apps ("list", NULL, with_usr, with_home, locale_name, language, NULL);
537 assert_strings_equivalent (expected, result);
542 assert_info (const gchar *desktop_id,
543 const gchar *expected,
546 const gchar *locale_name,
547 const gchar *language)
551 result = run_apps ("show-info", desktop_id, with_usr, with_home, locale_name, language, NULL);
552 g_assert_cmpstr (result, ==, expected);
557 assert_search (const gchar *search_string,
558 const gchar *expected,
561 const gchar *locale_name,
562 const gchar *language)
564 gchar **expected_lines;
565 gchar **result_lines;
569 expected_lines = g_strsplit (expected, "\n", -1);
570 result = run_apps ("search", search_string, with_usr, with_home, locale_name, language, NULL);
571 result_lines = g_strsplit (result, "\n", -1);
572 g_assert_cmpint (g_strv_length (expected_lines), ==, g_strv_length (result_lines));
573 for (i = 0; expected_lines[i]; i++)
574 assert_strings_equivalent (expected_lines[i], result_lines[i]);
575 g_strfreev (expected_lines);
576 g_strfreev (result_lines);
581 assert_implementations (const gchar *interface,
582 const gchar *expected,
588 result = run_apps ("implementations", interface, with_usr, with_home, NULL, NULL, NULL);
590 assert_strings_equivalent (expected, result);
594 #define ALL_USR_APPS "evince-previewer.desktop nautilus-classic.desktop gnome-font-viewer.desktop " \
595 "baobab.desktop yelp.desktop eog.desktop cheese.desktop gnome-clocks.desktop " \
596 "gnome-contacts.desktop kde4-kate.desktop gcr-prompter.desktop totem.desktop " \
597 "gnome-terminal.desktop nautilus-autorun-software.desktop gcr-viewer.desktop " \
598 "nautilus-connect-server.desktop kde4-dolphin.desktop gnome-music.desktop " \
599 "kde4-konqbrowser.desktop gucharmap.desktop kde4-okular.desktop nautilus.desktop " \
600 "gedit.desktop evince.desktop file-roller.desktop dconf-editor.desktop glade.desktop"
601 #define HOME_APPS "epiphany-weather-for-toronto-island-9c6a4e022b17686306243dada811d550d25eb1fb.desktop"
602 #define ALL_HOME_APPS HOME_APPS " eog.desktop"
607 assert_list ("", FALSE, FALSE, NULL, NULL);
608 assert_list (ALL_USR_APPS, TRUE, FALSE, NULL, NULL);
609 assert_list (ALL_HOME_APPS, FALSE, TRUE, NULL, NULL);
610 assert_list (ALL_USR_APPS " " HOME_APPS, TRUE, TRUE, NULL, NULL);
612 /* The user has "installed" their own version of eog.desktop which
613 * calls it "Eye of GNOME". Do some testing based on that.
615 * We should always find "Pictures" keyword no matter where we look.
617 assert_search ("Picture", "eog.desktop\n", TRUE, TRUE, NULL, NULL);
618 assert_search ("Picture", "eog.desktop\n", TRUE, FALSE, NULL, NULL);
619 assert_search ("Picture", "eog.desktop\n", FALSE, TRUE, NULL, NULL);
620 assert_search ("Picture", "", FALSE, FALSE, NULL, NULL);
622 /* We should only find it called "eye of gnome" when using the user's
625 assert_search ("eye gnome", "", TRUE, FALSE, NULL, NULL);
626 assert_search ("eye gnome", "eog.desktop\n", FALSE, TRUE, NULL, NULL);
627 assert_search ("eye gnome", "eog.desktop\n", TRUE, TRUE, NULL, NULL);
629 /* We should only find it called "image viewer" when _not_ using the
632 assert_search ("image viewer", "eog.desktop\n", TRUE, FALSE, NULL, NULL);
633 assert_search ("image viewer", "", FALSE, TRUE, NULL, NULL);
634 assert_search ("image viewer", "", TRUE, TRUE, NULL, NULL);
636 /* Obvious multi-word search */
637 assert_search ("gno hel", "yelp.desktop\n", TRUE, TRUE, NULL, NULL);
639 /* Repeated search terms should do nothing... */
640 assert_search ("files file fil fi f", "nautilus.desktop\n"
641 "gedit.desktop\n", TRUE, TRUE, NULL, NULL);
643 /* "con" will match "connect" and "contacts" on name but dconf only on
644 * the "config" keyword
646 assert_search ("con", "nautilus-connect-server.desktop gnome-contacts.desktop\n"
647 "dconf-editor.desktop\n", TRUE, TRUE, NULL, NULL);
649 /* "gnome" will match "eye of gnome" from the user's directory, plus
650 * matching "GNOME Clocks" X-GNOME-FullName. It's only a comment on
651 * yelp and gnome-contacts, though.
653 assert_search ("gnome", "eog.desktop\n"
654 "gnome-clocks.desktop\n"
655 "yelp.desktop gnome-contacts.desktop\n", TRUE, TRUE, NULL, NULL);
657 /* eog has exec name 'false' in usr only */
658 assert_search ("false", "eog.desktop\n", TRUE, FALSE, NULL, NULL);
659 assert_search ("false", "", FALSE, TRUE, NULL, NULL);
660 assert_search ("false", "", TRUE, TRUE, NULL, NULL);
661 assert_search ("false", "", FALSE, FALSE, NULL, NULL);
663 /* make sure we only search the first component */
664 assert_search ("nonsearchable", "", TRUE, FALSE, NULL, NULL);
666 /* "gnome con" will match only gnome contacts; via the name for
667 * "contacts" and the comment for "gnome"
669 assert_search ("gnome con", "gnome-contacts.desktop\n", TRUE, TRUE, NULL, NULL);
671 /* make sure we get the correct kde4- prefix on the application IDs
672 * from subdirectories
674 assert_search ("konq", "kde4-konqbrowser.desktop\n", TRUE, TRUE, NULL, NULL);
675 assert_search ("kate", "kde4-kate.desktop\n", TRUE, TRUE, NULL, NULL);
677 /* make sure we can lookup apps by name properly */
678 assert_info ("kde4-kate.desktop",
679 "kde4-kate.desktop\n"
682 "nil\n", TRUE, TRUE, NULL, NULL);
684 assert_info ("nautilus.desktop",
688 "Access and organize files\n", TRUE, TRUE, NULL, NULL);
690 /* make sure localised searching works properly */
691 assert_search ("foliumi", "nautilus.desktop\n"
692 "kde4-konqbrowser.desktop\n"
693 "eog.desktop\n", TRUE, FALSE, "en_US.UTF-8", "eo");
694 /* the user's eog.desktop has no translations... */
695 assert_search ("foliumi", "nautilus.desktop\n"
696 "kde4-konqbrowser.desktop\n", TRUE, TRUE, "en_US.UTF-8", "eo");
700 test_implements (void)
702 /* Make sure we can find our search providers... */
703 assert_implementations ("org.gnome.Shell.SearchProvider2",
704 "gnome-music.desktop gnome-contacts.desktop eog.desktop",
707 /* And our image acquisition possibilities... */
708 assert_implementations ("org.freedesktop.ImageProvider",
712 /* Make sure the user's eog is properly masking the system one */
713 assert_implementations ("org.gnome.Shell.SearchProvider2",
714 "gnome-music.desktop gnome-contacts.desktop",
717 /* Make sure we get nothing if we have nothing */
718 assert_implementations ("org.gnome.Shell.SearchProvider2", "", FALSE, FALSE);
722 assert_shown (const gchar *desktop_id,
724 const gchar *xdg_current_desktop)
728 result = run_apps ("should-show", desktop_id, TRUE, TRUE, NULL, NULL, xdg_current_desktop);
729 g_assert_cmpstr (result, ==, expected ? "true\n" : "false\n");
736 assert_shown ("gcr-prompter.desktop", FALSE, NULL);
737 assert_shown ("gcr-prompter.desktop", FALSE, "GNOME");
738 assert_shown ("gcr-prompter.desktop", FALSE, "KDE");
739 assert_shown ("gcr-prompter.desktop", FALSE, "GNOME:GNOME-Classic");
740 assert_shown ("gcr-prompter.desktop", TRUE, "GNOME-Classic:GNOME");
741 assert_shown ("gcr-prompter.desktop", TRUE, "GNOME-Classic");
742 assert_shown ("gcr-prompter.desktop", TRUE, "GNOME-Classic:KDE");
743 assert_shown ("gcr-prompter.desktop", TRUE, "KDE:GNOME-Classic");
752 g_test_init (&argc, &argv, NULL);
754 basedir = g_get_current_dir ();
755 g_setenv ("XDG_DATA_HOME", basedir, TRUE);
756 cleanup_subdirs (basedir);
758 g_test_add_func ("/desktop-app-info/delete", test_delete);
759 g_test_add_func ("/desktop-app-info/default", test_default);
760 g_test_add_func ("/desktop-app-info/fallback", test_fallback);
761 g_test_add_func ("/desktop-app-info/lastused", test_last_used);
762 g_test_add_func ("/desktop-app-info/extra-getters", test_extra_getters);
763 g_test_add_func ("/desktop-app-info/actions", test_actions);
764 g_test_add_func ("/desktop-app-info/search", test_search);
765 g_test_add_func ("/desktop-app-info/implements", test_implements);
766 g_test_add_func ("/desktop-app-info/show-in", test_show_in);
768 result = g_test_run ();
770 cleanup_subdirs (basedir);