Merge branch '976-disable-assert-checks' into 'master'
[glib.git] / gio / tests / desktop-app-info.c
blob4871d85a541f2cd0b1f575f32428937724636ef1
1 /*
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.1 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 <locale.h>
22 #include <glib/glib.h>
23 #include <glib/gstdio.h>
24 #include <gio/gio.h>
25 #include <gio/gdesktopappinfo.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
30 static char *basedir;
32 static GAppInfo *
33 create_app_info (const char *name)
35 GError *error;
36 GAppInfo *info;
38 error = NULL;
39 info = g_app_info_create_from_commandline ("true blah",
40 name,
41 G_APP_INFO_CREATE_NONE,
42 &error);
43 g_assert (error == NULL);
45 /* this is necessary to ensure that the info is saved */
46 g_app_info_set_as_default_for_type (info, "application/x-blah", &error);
47 g_assert (error == NULL);
48 g_app_info_remove_supports_type (info, "application/x-blah", &error);
49 g_assert (error == NULL);
50 g_app_info_reset_type_associations ("application/x-blah");
52 return info;
55 static void
56 test_delete (void)
58 GAppInfo *info;
60 const char *id;
61 char *filename;
62 gboolean res;
64 info = create_app_info ("Blah");
66 id = g_app_info_get_id (info);
67 g_assert (id != NULL);
69 filename = g_build_filename (basedir, "applications", id, NULL);
71 res = g_file_test (filename, G_FILE_TEST_EXISTS);
72 g_assert (res);
74 res = g_app_info_can_delete (info);
75 g_assert (res);
77 res = g_app_info_delete (info);
78 g_assert (res);
80 res = g_file_test (filename, G_FILE_TEST_EXISTS);
81 g_assert (!res);
83 g_object_unref (info);
85 if (g_file_test ("/usr/share/applications/gedit.desktop", G_FILE_TEST_EXISTS))
87 info = (GAppInfo*)g_desktop_app_info_new_from_filename ("/usr/share/applications/gedit.desktop");
88 g_assert (info);
90 res = g_app_info_can_delete (info);
91 g_assert (!res);
93 res = g_app_info_delete (info);
94 g_assert (!res);
97 g_free (filename);
100 static void
101 test_default (void)
103 GAppInfo *info, *info1, *info2, *info3;
104 GList *list;
105 GError *error = NULL;
107 info1 = create_app_info ("Blah1");
108 info2 = create_app_info ("Blah2");
109 info3 = create_app_info ("Blah3");
111 g_app_info_set_as_default_for_type (info1, "application/x-test", &error);
112 g_assert (error == NULL);
114 g_app_info_set_as_default_for_type (info2, "application/x-test", &error);
115 g_assert (error == NULL);
117 info = g_app_info_get_default_for_type ("application/x-test", FALSE);
118 g_assert (info != NULL);
119 g_assert_cmpstr (g_app_info_get_id (info), ==, g_app_info_get_id (info2));
120 g_object_unref (info);
122 /* now try adding something, but not setting as default */
123 g_app_info_add_supports_type (info3, "application/x-test", &error);
124 g_assert (error == NULL);
126 /* check that info2 is still default */
127 info = g_app_info_get_default_for_type ("application/x-test", FALSE);
128 g_assert (info != NULL);
129 g_assert_cmpstr (g_app_info_get_id (info), ==, g_app_info_get_id (info2));
130 g_object_unref (info);
132 /* now remove info1 again */
133 g_app_info_remove_supports_type (info1, "application/x-test", &error);
134 g_assert (error == NULL);
136 /* and make sure info2 is still default */
137 info = g_app_info_get_default_for_type ("application/x-test", FALSE);
138 g_assert (info != NULL);
139 g_assert_cmpstr (g_app_info_get_id (info), ==, g_app_info_get_id (info2));
140 g_object_unref (info);
142 /* now clean it all up */
143 g_app_info_reset_type_associations ("application/x-test");
145 list = g_app_info_get_all_for_type ("application/x-test");
146 g_assert (list == NULL);
148 g_app_info_delete (info1);
149 g_app_info_delete (info2);
150 g_app_info_delete (info3);
152 g_object_unref (info1);
153 g_object_unref (info2);
154 g_object_unref (info3);
157 static void
158 test_fallback (void)
160 GAppInfo *info1, *info2, *app;
161 GList *apps, *recomm, *fallback, *list, *l, *m;
162 GError *error = NULL;
163 gint old_length;
165 info1 = create_app_info ("Test1");
166 info2 = create_app_info ("Test2");
168 g_assert (g_content_type_is_a ("text/x-python", "text/plain"));
170 apps = g_app_info_get_all_for_type ("text/x-python");
171 old_length = g_list_length (apps);
172 g_list_free_full (apps, g_object_unref);
174 g_app_info_add_supports_type (info1, "text/x-python", &error);
175 g_assert (error == NULL);
177 g_app_info_add_supports_type (info2, "text/plain", &error);
178 g_assert (error == NULL);
180 /* check that both apps are registered */
181 apps = g_app_info_get_all_for_type ("text/x-python");
182 g_assert_cmpint (g_list_length (apps), ==, old_length + 2);
184 /* check that Test1 is among the recommended apps */
185 recomm = g_app_info_get_recommended_for_type ("text/x-python");
186 g_assert (recomm != NULL);
187 for (l = recomm; l; l = l->next)
189 app = l->data;
190 if (g_app_info_equal (info1, app))
191 break;
193 g_assert (g_app_info_equal (info1, app));
195 /* and that Test2 is among the fallback apps */
196 fallback = g_app_info_get_fallback_for_type ("text/x-python");
197 g_assert (fallback != NULL);
198 for (l = fallback; l; l = l->next)
200 app = l->data;
201 if (g_app_info_equal (info2, app))
202 break;
204 g_assert_cmpstr (g_app_info_get_name (app), ==, "Test2");
206 /* check that recomm + fallback = all applications */
207 list = g_list_concat (g_list_copy (recomm), g_list_copy (fallback));
208 g_assert (g_list_length (list) == g_list_length (apps));
210 for (l = list, m = apps; l != NULL && m != NULL; l = l->next, m = m->next)
212 g_assert (g_app_info_equal (l->data, m->data));
215 g_list_free (list);
217 g_list_free_full (apps, g_object_unref);
218 g_list_free_full (recomm, g_object_unref);
219 g_list_free_full (fallback, g_object_unref);
221 g_app_info_reset_type_associations ("text/x-python");
222 g_app_info_reset_type_associations ("text/plain");
224 g_app_info_delete (info1);
225 g_app_info_delete (info2);
227 g_object_unref (info1);
228 g_object_unref (info2);
231 static void
232 test_last_used (void)
234 GList *applications;
235 GAppInfo *info1, *info2, *default_app;
236 GError *error = NULL;
238 info1 = create_app_info ("Test1");
239 info2 = create_app_info ("Test2");
241 g_app_info_set_as_default_for_type (info1, "application/x-test", &error);
242 g_assert (error == NULL);
244 g_app_info_add_supports_type (info2, "application/x-test", &error);
245 g_assert (error == NULL);
247 applications = g_app_info_get_recommended_for_type ("application/x-test");
248 g_assert (g_list_length (applications) == 2);
250 /* the first should be the default app now */
251 g_assert (g_app_info_equal (g_list_nth_data (applications, 0), info1));
252 g_assert (g_app_info_equal (g_list_nth_data (applications, 1), info2));
254 g_list_free_full (applications, g_object_unref);
256 g_app_info_set_as_last_used_for_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 default_app = g_app_info_get_default_for_type ("application/x-test", FALSE);
263 g_assert (g_app_info_equal (default_app, info1));
265 /* the first should be the other app now */
266 g_assert (g_app_info_equal (g_list_nth_data (applications, 0), info2));
267 g_assert (g_app_info_equal (g_list_nth_data (applications, 1), info1));
269 g_list_free_full (applications, g_object_unref);
271 g_app_info_reset_type_associations ("application/x-test");
273 g_app_info_delete (info1);
274 g_app_info_delete (info2);
276 g_object_unref (info1);
277 g_object_unref (info2);
278 g_object_unref (default_app);
281 static gboolean
282 cleanup_dir_recurse (GFile *parent,
283 GFile *root,
284 GError **error)
286 gboolean ret = FALSE;
287 GFileEnumerator *enumerator;
288 GError *local_error = NULL;
290 g_assert (root != NULL);
292 enumerator =
293 g_file_enumerate_children (parent, "*",
294 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL,
295 &local_error);
296 if (!enumerator)
298 if (g_error_matches (local_error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND))
300 g_clear_error (&local_error);
301 ret = TRUE;
303 goto out;
306 while (TRUE)
308 GFile *child;
309 GFileInfo *finfo;
310 char *relative_path;
312 if (!g_file_enumerator_iterate (enumerator, &finfo, &child, NULL, error))
313 goto out;
314 if (!finfo)
315 break;
317 relative_path = g_file_get_relative_path (root, child);
318 g_assert (relative_path != NULL);
319 g_free (relative_path);
321 if (g_file_info_get_file_type (finfo) == G_FILE_TYPE_DIRECTORY)
323 if (!cleanup_dir_recurse (child, root, error))
324 goto out;
327 if (!g_file_delete (child, NULL, error))
328 goto out;
331 ret = TRUE;
332 out:
333 g_clear_object (&enumerator);
335 return ret;
338 static void
339 cleanup_subdirs (const char *base_dir)
341 GFile *base, *file;
342 GError *error = NULL;
344 base = g_file_new_for_path (base_dir);
345 file = g_file_get_child (base, "applications");
346 (void) cleanup_dir_recurse (file, file, &error);
347 g_assert_no_error (error);
348 g_object_unref (file);
349 file = g_file_get_child (base, "mime");
350 (void) cleanup_dir_recurse (file, file, &error);
351 g_assert_no_error (error);
352 g_object_unref (file);
353 g_object_unref (base);
356 static void
357 test_extra_getters (void)
359 GDesktopAppInfo *appinfo;
360 const gchar *lang;
361 gchar *s;
362 gboolean b;
364 lang = setlocale (LC_ALL, NULL);
365 g_setenv ("LANGUAGE", "de_DE.UTF8", TRUE);
366 setlocale (LC_ALL, "");
368 appinfo = g_desktop_app_info_new_from_filename (g_test_get_filename (G_TEST_DIST, "appinfo-test.desktop", NULL));
369 g_assert (appinfo != NULL);
371 g_assert (g_desktop_app_info_has_key (appinfo, "Terminal"));
372 g_assert (!g_desktop_app_info_has_key (appinfo, "Bratwurst"));
374 s = g_desktop_app_info_get_string (appinfo, "StartupWMClass");
375 g_assert_cmpstr (s, ==, "appinfo-class");
376 g_free (s);
378 s = g_desktop_app_info_get_locale_string (appinfo, "X-JunkFood");
379 g_assert_cmpstr (s, ==, "Bratwurst");
380 g_free (s);
382 g_setenv ("LANGUAGE", "sv_SV.UTF8", TRUE);
383 setlocale (LC_ALL, "");
385 s = g_desktop_app_info_get_locale_string (appinfo, "X-JunkFood");
386 g_assert_cmpstr (s, ==, "Burger"); /* fallback */
387 g_free (s);
389 b = g_desktop_app_info_get_boolean (appinfo, "Terminal");
390 g_assert (b);
392 g_object_unref (appinfo);
394 g_setenv ("LANGUAGE", lang, TRUE);
395 setlocale (LC_ALL, "");
398 static void
399 wait_for_file (const gchar *want_this,
400 const gchar *but_not_this,
401 const gchar *or_this)
403 gint retries = 600;
405 /* I hate time-based conditions in tests, but this will wait up to one
406 * whole minute for "touch file" to finish running. I think it should
407 * be OK.
409 * 600 * 100ms = 60 seconds.
411 while (access (want_this, F_OK) != 0)
413 g_usleep (100000); /* 100ms */
414 g_assert (retries);
415 retries--;
418 g_assert (access (but_not_this, F_OK) != 0);
419 g_assert (access (or_this, F_OK) != 0);
421 unlink (want_this);
422 unlink (but_not_this);
423 unlink (or_this);
426 static void
427 test_actions (void)
429 const gchar * const *actions;
430 GDesktopAppInfo *appinfo;
431 gchar *name;
433 appinfo = g_desktop_app_info_new_from_filename (g_test_get_filename (G_TEST_DIST, "appinfo-test-actions.desktop", NULL));
434 g_assert (appinfo != NULL);
436 actions = g_desktop_app_info_list_actions (appinfo);
437 g_assert_cmpstr (actions[0], ==, "frob");
438 g_assert_cmpstr (actions[1], ==, "tweak");
439 g_assert_cmpstr (actions[2], ==, "twiddle");
440 g_assert_cmpstr (actions[3], ==, "broken");
441 g_assert_cmpstr (actions[4], ==, NULL);
443 name = g_desktop_app_info_get_action_name (appinfo, "frob");
444 g_assert_cmpstr (name, ==, "Frobnicate");
445 g_free (name);
447 name = g_desktop_app_info_get_action_name (appinfo, "tweak");
448 g_assert_cmpstr (name, ==, "Tweak");
449 g_free (name);
451 name = g_desktop_app_info_get_action_name (appinfo, "twiddle");
452 g_assert_cmpstr (name, ==, "Twiddle");
453 g_free (name);
455 name = g_desktop_app_info_get_action_name (appinfo, "broken");
456 g_assert (name != NULL);
457 g_assert (g_utf8_validate (name, -1, NULL));
458 g_free (name);
460 unlink ("frob"); unlink ("tweak"); unlink ("twiddle");
462 g_desktop_app_info_launch_action (appinfo, "frob", NULL);
463 wait_for_file ("frob", "tweak", "twiddle");
465 g_desktop_app_info_launch_action (appinfo, "tweak", NULL);
466 wait_for_file ("tweak", "frob", "twiddle");
468 g_desktop_app_info_launch_action (appinfo, "twiddle", NULL);
469 wait_for_file ("twiddle", "frob", "tweak");
471 g_object_unref (appinfo);
474 static gchar *
475 run_apps (const gchar *command,
476 const gchar *arg,
477 gboolean with_usr,
478 gboolean with_home,
479 const gchar *locale_name,
480 const gchar *language,
481 const gchar *xdg_current_desktop)
483 gboolean success;
484 gchar **envp;
485 gchar **argv;
486 gint status;
487 gchar *out;
489 argv = g_new (gchar *, 4);
490 argv[0] = g_test_build_filename (G_TEST_BUILT, "apps", NULL);
491 argv[1] = g_strdup (command);
492 argv[2] = g_strdup (arg);
493 argv[3] = NULL;
495 envp = g_get_environ ();
497 if (with_usr)
499 gchar *tmp = g_test_build_filename (G_TEST_DIST, "desktop-files", "usr", NULL);
500 envp = g_environ_setenv (envp, "XDG_DATA_DIRS", tmp, TRUE);
501 g_free (tmp);
503 else
504 envp = g_environ_setenv (envp, "XDG_DATA_DIRS", "/does-not-exist", TRUE);
506 if (with_home)
508 gchar *tmp = g_test_build_filename (G_TEST_DIST, "desktop-files", "home", NULL);
509 envp = g_environ_setenv (envp, "XDG_DATA_HOME", tmp, TRUE);
510 g_free (tmp);
512 else
513 envp = g_environ_setenv (envp, "XDG_DATA_HOME", "/does-not-exist", TRUE);
515 if (locale_name)
516 envp = g_environ_setenv (envp, "LC_ALL", locale_name, TRUE);
517 else
518 envp = g_environ_setenv (envp, "LC_ALL", "C", TRUE);
520 if (language)
521 envp = g_environ_setenv (envp, "LANGUAGE", language, TRUE);
522 else
523 envp = g_environ_unsetenv (envp, "LANGUAGE");
525 if (xdg_current_desktop)
526 envp = g_environ_setenv (envp, "XDG_CURRENT_DESKTOP", xdg_current_desktop, TRUE);
527 else
528 envp = g_environ_unsetenv (envp, "XDG_CURRENT_DESKTOP");
530 success = g_spawn_sync (NULL, argv, envp, 0, NULL, NULL, &out, NULL, &status, NULL);
531 g_assert (success);
532 g_assert (status == 0);
534 g_strfreev (envp);
535 g_strfreev (argv);
537 return out;
540 static void
541 assert_strings_equivalent (const gchar *expected,
542 const gchar *result)
544 gchar **expected_words;
545 gchar **result_words;
546 gint i, j;
548 expected_words = g_strsplit (expected, " ", 0);
549 result_words = g_strsplit_set (result, " \n", 0);
551 for (i = 0; expected_words[i]; i++)
553 for (j = 0; result_words[j]; j++)
554 if (g_str_equal (expected_words[i], result_words[j]))
555 goto got_it;
557 g_test_message ("Unable to find expected string '%s' in result '%s'", expected_words[i], result);
558 g_test_fail ();
560 got_it:
561 continue;
564 g_assert_cmpint (g_strv_length (expected_words), ==, g_strv_length (result_words));
565 g_strfreev (expected_words);
566 g_strfreev (result_words);
569 static void
570 assert_list (const gchar *expected,
571 gboolean with_usr,
572 gboolean with_home,
573 const gchar *locale_name,
574 const gchar *language)
576 gchar *result;
578 result = run_apps ("list", NULL, with_usr, with_home, locale_name, language, NULL);
579 g_strchomp (result);
580 assert_strings_equivalent (expected, result);
581 g_free (result);
584 static void
585 assert_info (const gchar *desktop_id,
586 const gchar *expected,
587 gboolean with_usr,
588 gboolean with_home,
589 const gchar *locale_name,
590 const gchar *language)
592 gchar *result;
594 result = run_apps ("show-info", desktop_id, with_usr, with_home, locale_name, language, NULL);
595 g_assert_cmpstr (result, ==, expected);
596 g_free (result);
599 static void
600 assert_search (const gchar *search_string,
601 const gchar *expected,
602 gboolean with_usr,
603 gboolean with_home,
604 const gchar *locale_name,
605 const gchar *language)
607 gchar **expected_lines;
608 gchar **result_lines;
609 gchar *result;
610 gint i;
612 expected_lines = g_strsplit (expected, "\n", -1);
613 result = run_apps ("search", search_string, with_usr, with_home, locale_name, language, NULL);
614 result_lines = g_strsplit (result, "\n", -1);
615 g_assert_cmpint (g_strv_length (expected_lines), ==, g_strv_length (result_lines));
616 for (i = 0; expected_lines[i]; i++)
617 assert_strings_equivalent (expected_lines[i], result_lines[i]);
618 g_strfreev (expected_lines);
619 g_strfreev (result_lines);
620 g_free (result);
623 static void
624 assert_implementations (const gchar *interface,
625 const gchar *expected,
626 gboolean with_usr,
627 gboolean with_home)
629 gchar *result;
631 result = run_apps ("implementations", interface, with_usr, with_home, NULL, NULL, NULL);
632 g_strchomp (result);
633 assert_strings_equivalent (expected, result);
634 g_free (result);
637 #define ALL_USR_APPS "evince-previewer.desktop nautilus-classic.desktop gnome-font-viewer.desktop " \
638 "baobab.desktop yelp.desktop eog.desktop cheese.desktop org.gnome.clocks.desktop " \
639 "gnome-contacts.desktop kde4-kate.desktop gcr-prompter.desktop totem.desktop " \
640 "gnome-terminal.desktop nautilus-autorun-software.desktop gcr-viewer.desktop " \
641 "nautilus-connect-server.desktop kde4-dolphin.desktop gnome-music.desktop " \
642 "kde4-konqbrowser.desktop gucharmap.desktop kde4-okular.desktop nautilus.desktop " \
643 "gedit.desktop evince.desktop file-roller.desktop dconf-editor.desktop glade.desktop"
644 #define HOME_APPS "epiphany-weather-for-toronto-island-9c6a4e022b17686306243dada811d550d25eb1fb.desktop"
645 #define ALL_HOME_APPS HOME_APPS " eog.desktop"
647 static void
648 test_search (void)
650 assert_list ("", FALSE, FALSE, NULL, NULL);
651 assert_list (ALL_USR_APPS, TRUE, FALSE, NULL, NULL);
652 assert_list (ALL_HOME_APPS, FALSE, TRUE, NULL, NULL);
653 assert_list (ALL_USR_APPS " " HOME_APPS, TRUE, TRUE, NULL, NULL);
655 /* The user has "installed" their own version of eog.desktop which
656 * calls it "Eye of GNOME". Do some testing based on that.
658 * We should always find "Pictures" keyword no matter where we look.
660 assert_search ("Picture", "eog.desktop\n", TRUE, TRUE, NULL, NULL);
661 assert_search ("Picture", "eog.desktop\n", TRUE, FALSE, NULL, NULL);
662 assert_search ("Picture", "eog.desktop\n", FALSE, TRUE, NULL, NULL);
663 assert_search ("Picture", "", FALSE, FALSE, NULL, NULL);
665 /* We should only find it called "eye of gnome" when using the user's
666 * directory.
668 assert_search ("eye gnome", "", TRUE, FALSE, NULL, NULL);
669 assert_search ("eye gnome", "eog.desktop\n", FALSE, TRUE, NULL, NULL);
670 assert_search ("eye gnome", "eog.desktop\n", TRUE, TRUE, NULL, NULL);
672 /* We should only find it called "image viewer" when _not_ using the
673 * user's directory.
675 assert_search ("image viewer", "eog.desktop\n", TRUE, FALSE, NULL, NULL);
676 assert_search ("image viewer", "", FALSE, TRUE, NULL, NULL);
677 assert_search ("image viewer", "", TRUE, TRUE, NULL, NULL);
679 /* There're "flatpak" apps (clocks) installed as well - they should *not*
680 * match the prefix command ("/bin/sh") in the Exec= line though.
682 assert_search ("sh", "gnome-terminal.desktop\n", TRUE, FALSE, NULL, NULL);
684 /* "frobnicator.desktop" is ignored by get_all() because the binary is
685 * missing, but search should still find it (to avoid either stale results
686 * from the cache or expensive stat() calls for each potential result)
688 assert_search ("frobni", "frobnicator.desktop\n", TRUE, FALSE, NULL, NULL);
690 /* Obvious multi-word search */
691 assert_search ("gno hel", "yelp.desktop\n", TRUE, TRUE, NULL, NULL);
693 /* Repeated search terms should do nothing... */
694 assert_search ("files file fil fi f", "nautilus.desktop\n"
695 "gedit.desktop\n", TRUE, TRUE, NULL, NULL);
697 /* "con" will match "connect" and "contacts" on name but dconf only on
698 * the "config" keyword
700 assert_search ("con", "nautilus-connect-server.desktop gnome-contacts.desktop\n"
701 "dconf-editor.desktop\n", TRUE, TRUE, NULL, NULL);
703 /* "gnome" will match "eye of gnome" from the user's directory, plus
704 * matching "GNOME Clocks" X-GNOME-FullName. It's only a comment on
705 * yelp and gnome-contacts, though.
707 assert_search ("gnome", "eog.desktop\n"
708 "org.gnome.clocks.desktop\n"
709 "yelp.desktop gnome-contacts.desktop\n", TRUE, TRUE, NULL, NULL);
711 /* eog has exec name 'false' in usr only */
712 assert_search ("false", "eog.desktop\n", TRUE, FALSE, NULL, NULL);
713 assert_search ("false", "", FALSE, TRUE, NULL, NULL);
714 assert_search ("false", "", TRUE, TRUE, NULL, NULL);
715 assert_search ("false", "", FALSE, FALSE, NULL, NULL);
717 /* make sure we only search the first component */
718 assert_search ("nonsearchable", "", TRUE, FALSE, NULL, NULL);
720 /* "gnome con" will match only gnome contacts; via the name for
721 * "contacts" and the comment for "gnome"
723 assert_search ("gnome con", "gnome-contacts.desktop\n", TRUE, TRUE, NULL, NULL);
725 /* make sure we get the correct kde4- prefix on the application IDs
726 * from subdirectories
728 assert_search ("konq", "kde4-konqbrowser.desktop\n", TRUE, TRUE, NULL, NULL);
729 assert_search ("kate", "kde4-kate.desktop\n", TRUE, TRUE, NULL, NULL);
731 /* make sure we can lookup apps by name properly */
732 assert_info ("kde4-kate.desktop",
733 "kde4-kate.desktop\n"
734 "Kate\n"
735 "Kate\n"
736 "nil\n", TRUE, TRUE, NULL, NULL);
738 assert_info ("nautilus.desktop",
739 "nautilus.desktop\n"
740 "Files\n"
741 "Files\n"
742 "Access and organize files\n", TRUE, TRUE, NULL, NULL);
744 /* make sure localised searching works properly */
745 assert_search ("foliumi", "nautilus.desktop\n"
746 "kde4-konqbrowser.desktop\n"
747 "eog.desktop\n", TRUE, FALSE, "en_US.UTF-8", "eo");
748 /* the user's eog.desktop has no translations... */
749 assert_search ("foliumi", "nautilus.desktop\n"
750 "kde4-konqbrowser.desktop\n", TRUE, TRUE, "en_US.UTF-8", "eo");
753 static void
754 test_implements (void)
756 /* Make sure we can find our search providers... */
757 assert_implementations ("org.gnome.Shell.SearchProvider2",
758 "gnome-music.desktop gnome-contacts.desktop eog.desktop",
759 TRUE, FALSE);
761 /* And our image acquisition possibilities... */
762 assert_implementations ("org.freedesktop.ImageProvider",
763 "cheese.desktop",
764 TRUE, FALSE);
766 /* Make sure the user's eog is properly masking the system one */
767 assert_implementations ("org.gnome.Shell.SearchProvider2",
768 "gnome-music.desktop gnome-contacts.desktop",
769 TRUE, TRUE);
771 /* Make sure we get nothing if we have nothing */
772 assert_implementations ("org.gnome.Shell.SearchProvider2", "", FALSE, FALSE);
775 static void
776 assert_shown (const gchar *desktop_id,
777 gboolean expected,
778 const gchar *xdg_current_desktop)
780 gchar *result;
782 result = run_apps ("should-show", desktop_id, TRUE, TRUE, NULL, NULL, xdg_current_desktop);
783 g_assert_cmpstr (result, ==, expected ? "true\n" : "false\n");
784 g_free (result);
787 static void
788 test_show_in (void)
790 assert_shown ("gcr-prompter.desktop", FALSE, NULL);
791 assert_shown ("gcr-prompter.desktop", FALSE, "GNOME");
792 assert_shown ("gcr-prompter.desktop", FALSE, "KDE");
793 assert_shown ("gcr-prompter.desktop", FALSE, "GNOME:GNOME-Classic");
794 assert_shown ("gcr-prompter.desktop", TRUE, "GNOME-Classic:GNOME");
795 assert_shown ("gcr-prompter.desktop", TRUE, "GNOME-Classic");
796 assert_shown ("gcr-prompter.desktop", TRUE, "GNOME-Classic:KDE");
797 assert_shown ("gcr-prompter.desktop", TRUE, "KDE:GNOME-Classic");
800 /* Test g_desktop_app_info_launch_uris_as_manager() and
801 * g_desktop_app_info_launch_uris_as_manager_with_fds()
803 static void
804 test_launch_as_manager (void)
806 GDesktopAppInfo *appinfo;
807 GError *error = NULL;
808 gboolean retval;
809 const gchar *path;
811 if (g_getenv ("DISPLAY") == NULL || g_getenv ("DISPLAY")[0] == '\0')
813 g_test_skip ("No DISPLAY. Skipping test.");
814 return;
817 path = g_test_get_filename (G_TEST_DIST, "appinfo-test.desktop", NULL);
818 appinfo = g_desktop_app_info_new_from_filename (path);
819 g_assert_nonnull (appinfo);
821 retval = g_desktop_app_info_launch_uris_as_manager (appinfo, NULL, NULL, 0,
822 NULL, NULL,
823 NULL, NULL,
824 &error);
825 g_assert_no_error (error);
826 g_assert_true (retval);
828 retval = g_desktop_app_info_launch_uris_as_manager_with_fds (appinfo,
829 NULL, NULL, 0,
830 NULL, NULL,
831 NULL, NULL,
832 -1, -1, -1,
833 &error);
834 g_assert_no_error (error);
835 g_assert_true (retval);
837 g_object_unref (appinfo);
841 main (int argc,
842 char *argv[])
844 const gchar *build_dir;
845 gint result;
847 /* With Meson build we need to change into right directory, so that the
848 * appinfo-test binary can be found. */
849 build_dir = g_getenv ("G_TEST_BUILDDIR");
850 if (build_dir)
851 g_chdir (build_dir);
853 g_test_init (&argc, &argv, NULL);
855 basedir = g_get_current_dir ();
856 g_setenv ("XDG_DATA_HOME", basedir, TRUE);
857 cleanup_subdirs (basedir);
859 g_test_add_func ("/desktop-app-info/delete", test_delete);
860 g_test_add_func ("/desktop-app-info/default", test_default);
861 g_test_add_func ("/desktop-app-info/fallback", test_fallback);
862 g_test_add_func ("/desktop-app-info/lastused", test_last_used);
863 g_test_add_func ("/desktop-app-info/extra-getters", test_extra_getters);
864 g_test_add_func ("/desktop-app-info/actions", test_actions);
865 g_test_add_func ("/desktop-app-info/search", test_search);
866 g_test_add_func ("/desktop-app-info/implements", test_implements);
867 g_test_add_func ("/desktop-app-info/show-in", test_show_in);
868 g_test_add_func ("/desktop-app-info/launch-as-manager", test_launch_as_manager);
870 result = g_test_run ();
872 cleanup_subdirs (basedir);
874 return result;