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 the first fallback */
186 fallback = g_app_info_get_fallback_for_type ("text/x-python");
187 g_assert (fallback != NULL);
188 app = g_list_nth_data (fallback, 0);
189 g_assert (g_app_info_equal (info2, app));
191 /* check that recomm + fallback = all applications */
192 list = g_list_concat (g_list_copy (recomm), g_list_copy (fallback));
193 g_assert (g_list_length (list) == g_list_length (apps));
195 for (l = list, m = apps; l != NULL && m != NULL; l = l->next, m = m->next)
197 g_assert (g_app_info_equal (l->data, m->data));
202 g_list_free_full (apps, g_object_unref);
203 g_list_free_full (recomm, g_object_unref);
204 g_list_free_full (fallback, g_object_unref);
206 g_app_info_reset_type_associations ("text/x-python");
207 g_app_info_reset_type_associations ("text/plain");
209 g_app_info_delete (info1);
210 g_app_info_delete (info2);
212 g_object_unref (info1);
213 g_object_unref (info2);
217 test_last_used (void)
220 GAppInfo *info1, *info2, *default_app;
221 GError *error = NULL;
223 info1 = create_app_info ("Test1");
224 info2 = create_app_info ("Test2");
226 g_app_info_set_as_default_for_type (info1, "application/x-test", &error);
227 g_assert (error == NULL);
229 g_app_info_add_supports_type (info2, "application/x-test", &error);
230 g_assert (error == NULL);
232 applications = g_app_info_get_recommended_for_type ("application/x-test");
233 g_assert (g_list_length (applications) == 2);
235 /* the first should be the default app now */
236 g_assert (g_app_info_equal (g_list_nth_data (applications, 0), info1));
237 g_assert (g_app_info_equal (g_list_nth_data (applications, 1), info2));
239 g_list_free_full (applications, g_object_unref);
241 g_app_info_set_as_last_used_for_type (info2, "application/x-test", &error);
242 g_assert (error == NULL);
244 applications = g_app_info_get_recommended_for_type ("application/x-test");
245 g_assert (g_list_length (applications) == 2);
247 default_app = g_app_info_get_default_for_type ("application/x-test", FALSE);
248 g_assert (g_app_info_equal (default_app, info1));
250 /* the first should be the other app now */
251 g_assert (g_app_info_equal (g_list_nth_data (applications, 0), info2));
252 g_assert (g_app_info_equal (g_list_nth_data (applications, 1), info1));
254 g_list_free_full (applications, g_object_unref);
256 g_app_info_reset_type_associations ("application/x-test");
258 g_app_info_delete (info1);
259 g_app_info_delete (info2);
261 g_object_unref (info1);
262 g_object_unref (info2);
263 g_object_unref (default_app);
267 cleanup_dir_recurse (GFile *parent, GFile *root)
271 GFileEnumerator *enumerator;
276 g_assert (root != NULL);
280 g_file_enumerate_children (parent, "*",
281 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL,
286 info = g_file_enumerator_next_file (enumerator, NULL, &error);
287 while ((info) && (!error))
289 descend = g_file_get_child (parent, g_file_info_get_name (info));
290 g_assert (descend != NULL);
291 relative_path = g_file_get_relative_path (root, descend);
292 g_assert (relative_path != NULL);
294 if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
295 cleanup_dir_recurse (descend, root);
298 res = g_file_delete (descend, NULL, &error);
299 g_assert_cmpint (res, ==, TRUE);
301 g_object_unref (descend);
303 info = g_file_enumerator_next_file (enumerator, NULL, &error);
305 g_assert (error == NULL);
308 res = g_file_enumerator_close (enumerator, NULL, &error);
309 g_assert_cmpint (res, ==, TRUE);
310 g_assert (error == NULL);
314 cleanup_subdirs (const char *base_dir)
318 base = g_file_new_for_path (base_dir);
319 file = g_file_get_child (base, "applications");
320 cleanup_dir_recurse (file, file);
321 g_object_unref (file);
322 file = g_file_get_child (base, "mime");
323 cleanup_dir_recurse (file, file);
324 g_object_unref (file);
328 test_extra_getters (void)
330 GDesktopAppInfo *appinfo;
334 appinfo = g_desktop_app_info_new_from_filename (g_test_get_filename (G_TEST_DIST, "appinfo-test.desktop", NULL));
335 g_assert (appinfo != NULL);
337 g_assert (g_desktop_app_info_has_key (appinfo, "Terminal"));
338 g_assert (!g_desktop_app_info_has_key (appinfo, "Bratwurst"));
340 s = g_desktop_app_info_get_string (appinfo, "StartupWMClass");
341 g_assert_cmpstr (s, ==, "appinfo-class");
344 b = g_desktop_app_info_get_boolean (appinfo, "Terminal");
347 g_object_unref (appinfo);
351 wait_for_file (const gchar *want_this,
352 const gchar *but_not_this,
353 const gchar *or_this)
357 /* I hate time-based conditions in tests, but this will wait up to one
358 * whole minute for "touch file" to finish running. I think it should
361 * 600 * 100ms = 60 seconds.
363 while (access (want_this, F_OK) != 0)
365 g_usleep (100000); /* 100ms */
370 g_assert (access (but_not_this, F_OK) != 0);
371 g_assert (access (or_this, F_OK) != 0);
374 unlink (but_not_this);
381 const gchar * const *actions;
382 GDesktopAppInfo *appinfo;
385 appinfo = g_desktop_app_info_new_from_filename (g_test_get_filename (G_TEST_DIST, "appinfo-test-actions.desktop", NULL));
386 g_assert (appinfo != NULL);
388 actions = g_desktop_app_info_list_actions (appinfo);
389 g_assert_cmpstr (actions[0], ==, "frob");
390 g_assert_cmpstr (actions[1], ==, "tweak");
391 g_assert_cmpstr (actions[2], ==, "twiddle");
392 g_assert_cmpstr (actions[3], ==, "broken");
393 g_assert_cmpstr (actions[4], ==, NULL);
395 name = g_desktop_app_info_get_action_name (appinfo, "frob");
396 g_assert_cmpstr (name, ==, "Frobnicate");
399 name = g_desktop_app_info_get_action_name (appinfo, "tweak");
400 g_assert_cmpstr (name, ==, "Tweak");
403 name = g_desktop_app_info_get_action_name (appinfo, "twiddle");
404 g_assert_cmpstr (name, ==, "Twiddle");
407 name = g_desktop_app_info_get_action_name (appinfo, "broken");
408 g_assert (name != NULL);
409 g_assert (g_utf8_validate (name, -1, NULL));
412 unlink ("frob"); unlink ("tweak"); unlink ("twiddle");
414 g_desktop_app_info_launch_action (appinfo, "frob", NULL);
415 wait_for_file ("frob", "tweak", "twiddle");
417 g_desktop_app_info_launch_action (appinfo, "tweak", NULL);
418 wait_for_file ("tweak", "frob", "twiddle");
420 g_desktop_app_info_launch_action (appinfo, "twiddle", NULL);
421 wait_for_file ("twiddle", "frob", "tweak");
423 g_object_unref (appinfo);
427 run_apps (const gchar *command,
431 const gchar *locale_name,
432 const gchar *language)
440 argv = g_new (gchar *, 4);
441 argv[0] = g_test_build_filename (G_TEST_BUILT, "apps", NULL);
442 argv[1] = g_strdup (command);
443 argv[2] = g_strdup (arg);
446 envp = g_get_environ ();
450 gchar *tmp = g_test_build_filename (G_TEST_DIST, "desktop-files", "usr", NULL);
451 envp = g_environ_setenv (envp, "XDG_DATA_DIRS", tmp, TRUE);
455 envp = g_environ_setenv (envp, "XDG_DATA_DIRS", "/does-not-exist", TRUE);
459 gchar *tmp = g_test_build_filename (G_TEST_DIST, "desktop-files", "home", NULL);
460 envp = g_environ_setenv (envp, "XDG_DATA_HOME", tmp, TRUE);
464 envp = g_environ_setenv (envp, "XDG_DATA_HOME", "/does-not-exist", TRUE);
467 envp = g_environ_setenv (envp, "LC_ALL", locale_name, TRUE);
469 envp = g_environ_setenv (envp, "LC_ALL", "C", TRUE);
472 envp = g_environ_setenv (envp, "LANGUAGE", language, TRUE);
474 envp = g_environ_unsetenv (envp, "LANGUAGE");
476 success = g_spawn_sync (NULL, argv, envp, 0, NULL, NULL, &out, NULL, &status, NULL);
478 g_assert (status == 0);
487 assert_strings_equivalent (const gchar *expected,
490 gchar **expected_words;
491 gchar **result_words;
494 expected_words = g_strsplit (expected, " ", 0);
495 result_words = g_strsplit (result, " ", 0);
497 for (i = 0; expected_words[i]; i++)
499 for (j = 0; result_words[j]; j++)
500 if (g_str_equal (expected_words[i], result_words[j]))
503 g_test_message ("Unable to find expected string '%s' in result '%s'", expected_words[i], result);
510 g_assert_cmpint (g_strv_length (expected_words), ==, g_strv_length (result_words));
511 g_strfreev (expected_words);
512 g_strfreev (result_words);
516 assert_list (const gchar *expected,
519 const gchar *locale_name,
520 const gchar *language)
524 result = run_apps ("list", NULL, with_usr, with_home, locale_name, language);
526 assert_strings_equivalent (expected, result);
531 assert_info (const gchar *desktop_id,
532 const gchar *expected,
535 const gchar *locale_name,
536 const gchar *language)
540 result = run_apps ("show-info", desktop_id, with_usr, with_home, locale_name, language);
541 g_assert_cmpstr (result, ==, expected);
546 assert_search (const gchar *search_string,
547 const gchar *expected,
550 const gchar *locale_name,
551 const gchar *language)
553 gchar **expected_lines;
554 gchar **result_lines;
558 expected_lines = g_strsplit (expected, "\n", -1);
559 result = run_apps ("search", search_string, with_usr, with_home, locale_name, language);
560 result_lines = g_strsplit (result, "\n", -1);
561 g_assert_cmpint (g_strv_length (expected_lines), ==, g_strv_length (result_lines));
562 for (i = 0; expected_lines[i]; i++)
563 assert_strings_equivalent (expected_lines[i], result_lines[i]);
564 g_strfreev (expected_lines);
565 g_strfreev (result_lines);
569 #define ALL_USR_APPS "evince-previewer.desktop nautilus-classic.desktop gnome-font-viewer.desktop " \
570 "baobab.desktop yelp.desktop eog.desktop cheese.desktop gnome-clocks.desktop " \
571 "gnome-contacts.desktop kde4-kate.desktop gcr-prompter.desktop totem.desktop " \
572 "gnome-terminal.desktop nautilus-autorun-software.desktop gcr-viewer.desktop " \
573 "nautilus-connect-server.desktop kde4-dolphin.desktop gnome-music.desktop " \
574 "kde4-konqbrowser.desktop gucharmap.desktop kde4-okular.desktop nautilus.desktop " \
575 "gedit.desktop evince.desktop file-roller.desktop dconf-editor.desktop glade.desktop"
576 #define HOME_APPS "epiphany-weather-for-toronto-island-9c6a4e022b17686306243dada811d550d25eb1fb.desktop"
577 #define ALL_HOME_APPS HOME_APPS " eog.desktop"
582 assert_list ("", FALSE, FALSE, NULL, NULL);
583 assert_list (ALL_USR_APPS, TRUE, FALSE, NULL, NULL);
584 assert_list (ALL_HOME_APPS, FALSE, TRUE, NULL, NULL);
585 assert_list (ALL_USR_APPS " " HOME_APPS, TRUE, TRUE, NULL, NULL);
587 /* The user has "installed" their own version of eog.desktop which
588 * calls it "Eye of GNOME". Do some testing based on that.
590 * We should always find "Pictures" keyword no matter where we look.
592 assert_search ("Picture", "eog.desktop\n", TRUE, TRUE, NULL, NULL);
593 assert_search ("Picture", "eog.desktop\n", TRUE, FALSE, NULL, NULL);
594 assert_search ("Picture", "eog.desktop\n", FALSE, TRUE, NULL, NULL);
595 assert_search ("Picture", "", FALSE, FALSE, NULL, NULL);
597 /* We should only find it called "eye of gnome" when using the user's
600 assert_search ("eye gnome", "", TRUE, FALSE, NULL, NULL);
601 assert_search ("eye gnome", "eog.desktop\n", FALSE, TRUE, NULL, NULL);
602 assert_search ("eye gnome", "eog.desktop\n", TRUE, TRUE, NULL, NULL);
604 /* We should only find it called "image viewer" when _not_ using the
607 assert_search ("image viewer", "eog.desktop\n", TRUE, FALSE, NULL, NULL);
608 assert_search ("image viewer", "", FALSE, TRUE, NULL, NULL);
609 assert_search ("image viewer", "", TRUE, TRUE, NULL, NULL);
611 /* Obvious multi-word search */
612 assert_search ("gno hel", "yelp.desktop\n", TRUE, TRUE, NULL, NULL);
614 /* Repeated search terms should do nothing... */
615 assert_search ("files file fil fi f", "nautilus.desktop\n"
616 "gedit.desktop\n", TRUE, TRUE, NULL, NULL);
618 /* "con" will match "connect" and "contacts" on name but dconf only on
619 * the "config" keyword
621 assert_search ("con", "nautilus-connect-server.desktop gnome-contacts.desktop\n"
622 "dconf-editor.desktop\n", TRUE, TRUE, NULL, NULL);
624 /* "gnome" will match "eye of gnome" from the user's directory, plus
625 * matching "GNOME Clocks" X-GNOME-FullName. It's only a comment on
626 * yelp and gnome-contacts, though.
628 assert_search ("gnome", "eog.desktop\n"
629 "gnome-clocks.desktop\n"
630 "yelp.desktop gnome-contacts.desktop\n", TRUE, TRUE, NULL, NULL);
632 /* eog has exec name 'false' in usr only */
633 assert_search ("false", "eog.desktop\n", TRUE, FALSE, NULL, NULL);
634 assert_search ("false", "", FALSE, TRUE, NULL, NULL);
635 assert_search ("false", "", TRUE, TRUE, NULL, NULL);
636 assert_search ("false", "", FALSE, FALSE, NULL, NULL);
638 /* make sure we only search the first component */
639 assert_search ("nonsearchable", "", TRUE, FALSE, NULL, NULL);
641 /* "gnome con" will match only gnome contacts; via the name for
642 * "contacts" and the comment for "gnome"
644 assert_search ("gnome con", "gnome-contacts.desktop\n", TRUE, TRUE, NULL, NULL);
646 /* make sure we get the correct kde4- prefix on the application IDs
647 * from subdirectories
649 assert_search ("konq", "kde4-konqbrowser.desktop\n", TRUE, TRUE, NULL, NULL);
650 assert_search ("kate", "kde4-kate.desktop\n", TRUE, TRUE, NULL, NULL);
652 /* make sure we can lookup apps by name properly */
653 assert_info ("kde4-kate.desktop",
654 "kde4-kate.desktop\n"
657 "nil\n", TRUE, TRUE, NULL, NULL);
659 assert_info ("nautilus.desktop",
663 "Access and organize files\n", TRUE, TRUE, NULL, NULL);
665 /* make sure localised searching works properly */
666 assert_search ("foliumi", "nautilus.desktop\n"
667 "kde4-konqbrowser.desktop\n"
668 "eog.desktop\n", TRUE, FALSE, "en_US.UTF-8", "eo");
669 /* the user's eog.desktop has no translations... */
670 assert_search ("foliumi", "nautilus.desktop\n"
671 "kde4-konqbrowser.desktop\n", TRUE, TRUE, "en_US.UTF-8", "eo");
680 g_test_init (&argc, &argv, NULL);
682 basedir = g_get_current_dir ();
683 g_setenv ("XDG_DATA_HOME", basedir, TRUE);
684 cleanup_subdirs (basedir);
686 g_test_add_func ("/desktop-app-info/delete", test_delete);
687 g_test_add_func ("/desktop-app-info/default", test_default);
688 g_test_add_func ("/desktop-app-info/fallback", test_fallback);
689 g_test_add_func ("/desktop-app-info/lastused", test_last_used);
690 g_test_add_func ("/desktop-app-info/extra-getters", test_extra_getters);
691 g_test_add_func ("/desktop-app-info/actions", test_actions);
692 g_test_add_func ("/desktop-app-info/search", test_search);
694 result = g_test_run ();
696 cleanup_subdirs (basedir);