• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2008 Red Hat, Inc
3  *
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.
8  *
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.
13  *
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/>.
16  *
17  * Author: Matthias Clasen
18  */
19 
20 #include <locale.h>
21 
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>
29 
30 static GAppInfo *
create_app_info(const char * name)31 create_app_info (const char *name)
32 {
33   GError *error;
34   GAppInfo *info;
35 
36   error = NULL;
37   info = g_app_info_create_from_commandline ("true blah",
38                                              name,
39                                              G_APP_INFO_CREATE_NONE,
40                                              &error);
41   g_assert_no_error (error);
42 
43   /* this is necessary to ensure that the info is saved */
44   g_app_info_set_as_default_for_type (info, "application/x-blah", &error);
45   g_assert_no_error (error);
46   g_app_info_remove_supports_type (info, "application/x-blah", &error);
47   g_assert_no_error (error);
48   g_app_info_reset_type_associations ("application/x-blah");
49 
50   return info;
51 }
52 
53 static void
test_delete(void)54 test_delete (void)
55 {
56   GAppInfo *info;
57 
58   const char *id;
59   char *filename;
60   gboolean res;
61 
62   info = create_app_info ("Blah");
63 
64   id = g_app_info_get_id (info);
65   g_assert_nonnull (id);
66 
67   filename = g_build_filename (g_get_user_data_dir (), "applications", id, NULL);
68 
69   res = g_file_test (filename, G_FILE_TEST_EXISTS);
70   g_assert_true (res);
71 
72   res = g_app_info_can_delete (info);
73   g_assert_true (res);
74 
75   res = g_app_info_delete (info);
76   g_assert_true (res);
77 
78   res = g_file_test (filename, G_FILE_TEST_EXISTS);
79   g_assert_false (res);
80 
81   g_object_unref (info);
82 
83   if (g_file_test ("/usr/share/applications/gedit.desktop", G_FILE_TEST_EXISTS))
84     {
85       info = (GAppInfo*)g_desktop_app_info_new_from_filename ("/usr/share/applications/gedit.desktop");
86       g_assert_nonnull (info);
87 
88       res = g_app_info_can_delete (info);
89       g_assert_false (res);
90 
91       res = g_app_info_delete (info);
92       g_assert_false (res);
93     }
94 
95   g_free (filename);
96 }
97 
98 static void
test_default(void)99 test_default (void)
100 {
101   GAppInfo *info, *info1, *info2, *info3;
102   GList *list;
103   GError *error = NULL;
104 
105   info1 = create_app_info ("Blah1");
106   info2 = create_app_info ("Blah2");
107   info3 = create_app_info ("Blah3");
108 
109   g_app_info_set_as_default_for_type (info1, "application/x-test", &error);
110   g_assert_no_error (error);
111 
112   g_app_info_set_as_default_for_type (info2, "application/x-test", &error);
113   g_assert_no_error (error);
114 
115   info = g_app_info_get_default_for_type ("application/x-test", FALSE);
116   g_assert_nonnull (info);
117   g_assert_cmpstr (g_app_info_get_id (info), ==, g_app_info_get_id (info2));
118   g_object_unref (info);
119 
120   /* now try adding something, but not setting as default */
121   g_app_info_add_supports_type (info3, "application/x-test", &error);
122   g_assert_no_error (error);
123 
124   /* check that info2 is still default */
125   info = g_app_info_get_default_for_type ("application/x-test", FALSE);
126   g_assert_nonnull (info);
127   g_assert_cmpstr (g_app_info_get_id (info), ==, g_app_info_get_id (info2));
128   g_object_unref (info);
129 
130   /* now remove info1 again */
131   g_app_info_remove_supports_type (info1, "application/x-test", &error);
132   g_assert_no_error (error);
133 
134   /* and make sure info2 is still default */
135   info = g_app_info_get_default_for_type ("application/x-test", FALSE);
136   g_assert_nonnull (info);
137   g_assert_cmpstr (g_app_info_get_id (info), ==, g_app_info_get_id (info2));
138   g_object_unref (info);
139 
140   /* now clean it all up */
141   g_app_info_reset_type_associations ("application/x-test");
142 
143   list = g_app_info_get_all_for_type ("application/x-test");
144   g_assert_null (list);
145 
146   g_app_info_delete (info1);
147   g_app_info_delete (info2);
148   g_app_info_delete (info3);
149 
150   g_object_unref (info1);
151   g_object_unref (info2);
152   g_object_unref (info3);
153 }
154 
155 static void
test_fallback(void)156 test_fallback (void)
157 {
158   GAppInfo *info1, *info2, *app = NULL;
159   GList *apps, *recomm, *fallback, *list, *l, *m;
160   GError *error = NULL;
161   gint old_length;
162 
163   info1 = create_app_info ("Test1");
164   info2 = create_app_info ("Test2");
165 
166   g_assert_true (g_content_type_is_a ("text/x-python", "text/plain"));
167 
168   apps = g_app_info_get_all_for_type ("text/x-python");
169   old_length = g_list_length (apps);
170   g_list_free_full (apps, g_object_unref);
171 
172   g_app_info_add_supports_type (info1, "text/x-python", &error);
173   g_assert_no_error (error);
174 
175   g_app_info_add_supports_type (info2, "text/plain", &error);
176   g_assert_no_error (error);
177 
178   /* check that both apps are registered */
179   apps = g_app_info_get_all_for_type ("text/x-python");
180   g_assert_cmpint (g_list_length (apps), ==, old_length + 2);
181 
182   /* check that Test1 is among the recommended apps */
183   recomm = g_app_info_get_recommended_for_type ("text/x-python");
184   g_assert_nonnull (recomm);
185   for (l = recomm; l; l = l->next)
186     {
187       app = l->data;
188       if (g_app_info_equal (info1, app))
189         break;
190     }
191   g_assert_nonnull (app);
192   g_assert_true (g_app_info_equal (info1, app));
193 
194   /* and that Test2 is among the fallback apps */
195   fallback = g_app_info_get_fallback_for_type ("text/x-python");
196   g_assert_nonnull (fallback);
197   for (l = fallback; l; l = l->next)
198     {
199       app = l->data;
200       if (g_app_info_equal (info2, app))
201         break;
202     }
203   g_assert_cmpstr (g_app_info_get_name (app), ==, "Test2");
204 
205   /* check that recomm + fallback = all applications */
206   list = g_list_concat (g_list_copy (recomm), g_list_copy (fallback));
207   g_assert_cmpuint (g_list_length (list), ==, g_list_length (apps));
208 
209   for (l = list, m = apps; l != NULL && m != NULL; l = l->next, m = m->next)
210     {
211       g_assert_true (g_app_info_equal (l->data, m->data));
212     }
213 
214   g_list_free (list);
215 
216   g_list_free_full (apps, g_object_unref);
217   g_list_free_full (recomm, g_object_unref);
218   g_list_free_full (fallback, g_object_unref);
219 
220   g_app_info_reset_type_associations ("text/x-python");
221   g_app_info_reset_type_associations ("text/plain");
222 
223   g_app_info_delete (info1);
224   g_app_info_delete (info2);
225 
226   g_object_unref (info1);
227   g_object_unref (info2);
228 }
229 
230 static void
test_last_used(void)231 test_last_used (void)
232 {
233   GList *applications;
234   GAppInfo *info1, *info2, *default_app;
235   GError *error = NULL;
236 
237   info1 = create_app_info ("Test1");
238   info2 = create_app_info ("Test2");
239 
240   g_app_info_set_as_default_for_type (info1, "application/x-test", &error);
241   g_assert_no_error (error);
242 
243   g_app_info_add_supports_type (info2, "application/x-test", &error);
244   g_assert_no_error (error);
245 
246   applications = g_app_info_get_recommended_for_type ("application/x-test");
247   g_assert_cmpuint (g_list_length (applications), ==, 2);
248 
249   /* the first should be the default app now */
250   g_assert_true (g_app_info_equal (g_list_nth_data (applications, 0), info1));
251   g_assert_true (g_app_info_equal (g_list_nth_data (applications, 1), info2));
252 
253   g_list_free_full (applications, g_object_unref);
254 
255   g_app_info_set_as_last_used_for_type (info2, "application/x-test", &error);
256   g_assert_no_error (error);
257 
258   applications = g_app_info_get_recommended_for_type ("application/x-test");
259   g_assert_cmpuint (g_list_length (applications), ==, 2);
260 
261   default_app = g_app_info_get_default_for_type ("application/x-test", FALSE);
262   g_assert_true (g_app_info_equal (default_app, info1));
263 
264   /* the first should be the other app now */
265   g_assert_true (g_app_info_equal (g_list_nth_data (applications, 0), info2));
266   g_assert_true (g_app_info_equal (g_list_nth_data (applications, 1), info1));
267 
268   g_list_free_full (applications, g_object_unref);
269 
270   g_app_info_reset_type_associations ("application/x-test");
271 
272   g_app_info_delete (info1);
273   g_app_info_delete (info2);
274 
275   g_object_unref (info1);
276   g_object_unref (info2);
277   g_object_unref (default_app);
278 }
279 
280 static void
test_extra_getters(void)281 test_extra_getters (void)
282 {
283   GDesktopAppInfo *appinfo;
284   const gchar *lang;
285   gchar *s;
286   gboolean b;
287 
288   lang = setlocale (LC_ALL, NULL);
289   g_setenv ("LANGUAGE", "de_DE.UTF8", TRUE);
290   setlocale (LC_ALL, "");
291 
292   appinfo = g_desktop_app_info_new_from_filename (g_test_get_filename (G_TEST_DIST, "appinfo-test-static.desktop", NULL));
293   g_assert_nonnull (appinfo);
294 
295   g_assert_true (g_desktop_app_info_has_key (appinfo, "Terminal"));
296   g_assert_false (g_desktop_app_info_has_key (appinfo, "Bratwurst"));
297 
298   s = g_desktop_app_info_get_string (appinfo, "StartupWMClass");
299   g_assert_cmpstr (s, ==, "appinfo-class");
300   g_free (s);
301 
302   s = g_desktop_app_info_get_locale_string (appinfo, "X-JunkFood");
303   g_assert_cmpstr (s, ==, "Bratwurst");
304   g_free (s);
305 
306   g_setenv ("LANGUAGE", "sv_SV.UTF8", TRUE);
307   setlocale (LC_ALL, "");
308 
309   s = g_desktop_app_info_get_locale_string (appinfo, "X-JunkFood");
310   g_assert_cmpstr (s, ==, "Burger"); /* fallback */
311   g_free (s);
312 
313   b = g_desktop_app_info_get_boolean (appinfo, "Terminal");
314   g_assert_true (b);
315 
316   g_object_unref (appinfo);
317 
318   g_setenv ("LANGUAGE", lang, TRUE);
319   setlocale (LC_ALL, "");
320 }
321 
322 static void
wait_for_file(const gchar * want_this,const gchar * but_not_this,const gchar * or_this)323 wait_for_file (const gchar *want_this,
324                const gchar *but_not_this,
325                const gchar *or_this)
326 {
327   guint retries = 600;
328 
329   /* I hate time-based conditions in tests, but this will wait up to one
330    * whole minute for "touch file" to finish running.  I think it should
331    * be OK.
332    *
333    * 600 * 100ms = 60 seconds.
334    */
335   while (access (want_this, F_OK) != 0)
336     {
337       g_usleep (100000); /* 100ms */
338       g_assert_cmpuint (retries, >, 0);
339       retries--;
340     }
341 
342   g_assert_cmpuint (access (but_not_this, F_OK), !=, 0);
343   g_assert_cmpuint (access (or_this, F_OK), !=, 0);
344 
345   unlink (want_this);
346   unlink (but_not_this);
347   unlink (or_this);
348 }
349 
350 static void
test_actions(void)351 test_actions (void)
352 {
353   const gchar * const *actions;
354   GDesktopAppInfo *appinfo;
355   gchar *name;
356 
357   appinfo = g_desktop_app_info_new_from_filename (g_test_get_filename (G_TEST_DIST, "appinfo-test-actions.desktop", NULL));
358   g_assert_nonnull (appinfo);
359 
360   actions = g_desktop_app_info_list_actions (appinfo);
361   g_assert_cmpstr (actions[0], ==, "frob");
362   g_assert_cmpstr (actions[1], ==, "tweak");
363   g_assert_cmpstr (actions[2], ==, "twiddle");
364   g_assert_cmpstr (actions[3], ==, "broken");
365   g_assert_cmpstr (actions[4], ==, NULL);
366 
367   name = g_desktop_app_info_get_action_name (appinfo, "frob");
368   g_assert_cmpstr (name, ==, "Frobnicate");
369   g_free (name);
370 
371   name = g_desktop_app_info_get_action_name (appinfo, "tweak");
372   g_assert_cmpstr (name, ==, "Tweak");
373   g_free (name);
374 
375   name = g_desktop_app_info_get_action_name (appinfo, "twiddle");
376   g_assert_cmpstr (name, ==, "Twiddle");
377   g_free (name);
378 
379   name = g_desktop_app_info_get_action_name (appinfo, "broken");
380   g_assert_nonnull (name);
381   g_assert_true (g_utf8_validate (name, -1, NULL));
382   g_free (name);
383 
384   unlink ("frob"); unlink ("tweak"); unlink ("twiddle");
385 
386   g_desktop_app_info_launch_action (appinfo, "frob", NULL);
387   wait_for_file ("frob", "tweak", "twiddle");
388 
389   g_desktop_app_info_launch_action (appinfo, "tweak", NULL);
390   wait_for_file ("tweak", "frob", "twiddle");
391 
392   g_desktop_app_info_launch_action (appinfo, "twiddle", NULL);
393   wait_for_file ("twiddle", "frob", "tweak");
394 
395   g_object_unref (appinfo);
396 }
397 
398 static gchar *
run_apps(const gchar * command,const gchar * arg,gboolean with_usr,gboolean with_home,const gchar * locale_name,const gchar * language,const gchar * xdg_current_desktop)399 run_apps (const gchar *command,
400           const gchar *arg,
401           gboolean     with_usr,
402           gboolean     with_home,
403           const gchar *locale_name,
404           const gchar *language,
405           const gchar *xdg_current_desktop)
406 {
407   gboolean success;
408   gchar **envp;
409   gchar **argv;
410   gint status;
411   gchar *out;
412   gchar *argv_str = NULL;
413 
414   argv = g_new (gchar *, 4);
415   argv[0] = g_test_build_filename (G_TEST_BUILT, "apps", NULL);
416   argv[1] = g_strdup (command);
417   argv[2] = g_strdup (arg);
418   argv[3] = NULL;
419 
420   envp = g_get_environ ();
421 
422   if (with_usr)
423     {
424       gchar *tmp = g_test_build_filename (G_TEST_DIST, "desktop-files", "usr", NULL);
425       envp = g_environ_setenv (envp, "XDG_DATA_DIRS", tmp, TRUE);
426       g_free (tmp);
427     }
428   else
429     envp = g_environ_setenv (envp, "XDG_DATA_DIRS", "/does-not-exist", TRUE);
430 
431   if (with_home)
432     {
433       gchar *tmp = g_test_build_filename (G_TEST_DIST, "desktop-files", "home", NULL);
434       envp = g_environ_setenv (envp, "XDG_DATA_HOME", tmp, TRUE);
435       g_free (tmp);
436     }
437   else
438     envp = g_environ_setenv (envp, "XDG_DATA_HOME", "/does-not-exist", TRUE);
439 
440   if (locale_name)
441     envp = g_environ_setenv (envp, "LC_ALL", locale_name, TRUE);
442   else
443     envp = g_environ_setenv (envp, "LC_ALL", "C", TRUE);
444 
445   if (language)
446     envp = g_environ_setenv (envp, "LANGUAGE", language, TRUE);
447   else
448     envp = g_environ_unsetenv (envp, "LANGUAGE");
449 
450   if (xdg_current_desktop)
451     envp = g_environ_setenv (envp, "XDG_CURRENT_DESKTOP", xdg_current_desktop, TRUE);
452   else
453     envp = g_environ_unsetenv (envp, "XDG_CURRENT_DESKTOP");
454 
455   envp = g_environ_setenv (envp, "G_MESSAGES_DEBUG", "", TRUE);
456 
457   success = g_spawn_sync (NULL, argv, envp, 0, NULL, NULL, &out, NULL, &status, NULL);
458   g_assert_true (success);
459   g_assert_cmpuint (status, ==, 0);
460 
461   argv_str = g_strjoinv (" ", argv);
462   g_test_message ("%s: `%s` returned: %s", G_STRFUNC, argv_str, out);
463   g_free (argv_str);
464 
465   g_strfreev (envp);
466   g_strfreev (argv);
467 
468   return out;
469 }
470 
471 static void
assert_strings_equivalent(const gchar * expected,const gchar * result)472 assert_strings_equivalent (const gchar *expected,
473                            const gchar *result)
474 {
475   gchar **expected_words;
476   gchar **result_words;
477   gint i, j;
478 
479   expected_words = g_strsplit (expected, " ", 0);
480   result_words = g_strsplit_set (result, " \n", 0);
481 
482   for (i = 0; expected_words[i]; i++)
483     {
484       for (j = 0; result_words[j]; j++)
485         if (g_str_equal (expected_words[i], result_words[j]))
486           goto got_it;
487 
488       g_test_message ("Unable to find expected string '%s' in result '%s'", expected_words[i], result);
489       g_test_fail ();
490 
491 got_it:
492       continue;
493     }
494 
495   g_assert_cmpint (g_strv_length (expected_words), ==, g_strv_length (result_words));
496   g_strfreev (expected_words);
497   g_strfreev (result_words);
498 }
499 
500 static void
assert_list(const gchar * expected,gboolean with_usr,gboolean with_home,const gchar * locale_name,const gchar * language)501 assert_list (const gchar *expected,
502              gboolean     with_usr,
503              gboolean     with_home,
504              const gchar *locale_name,
505              const gchar *language)
506 {
507   gchar *result;
508 
509   result = run_apps ("list", NULL, with_usr, with_home, locale_name, language, NULL);
510   g_strchomp (result);
511   assert_strings_equivalent (expected, result);
512   g_free (result);
513 }
514 
515 static void
assert_info(const gchar * desktop_id,const gchar * expected,gboolean with_usr,gboolean with_home,const gchar * locale_name,const gchar * language)516 assert_info (const gchar *desktop_id,
517              const gchar *expected,
518              gboolean     with_usr,
519              gboolean     with_home,
520              const gchar *locale_name,
521              const gchar *language)
522 {
523   gchar *result;
524 
525   result = run_apps ("show-info", desktop_id, with_usr, with_home, locale_name, language, NULL);
526   g_assert_cmpstr (result, ==, expected);
527   g_free (result);
528 }
529 
530 static void
assert_search(const gchar * search_string,const gchar * expected,gboolean with_usr,gboolean with_home,const gchar * locale_name,const gchar * language)531 assert_search (const gchar *search_string,
532                const gchar *expected,
533                gboolean     with_usr,
534                gboolean     with_home,
535                const gchar *locale_name,
536                const gchar *language)
537 {
538   gchar **expected_lines;
539   gchar **result_lines;
540   gchar *result;
541   gint i;
542 
543   expected_lines = g_strsplit (expected, "\n", -1);
544   result = run_apps ("search", search_string, with_usr, with_home, locale_name, language, NULL);
545   result_lines = g_strsplit (result, "\n", -1);
546   g_assert_cmpint (g_strv_length (expected_lines), ==, g_strv_length (result_lines));
547   for (i = 0; expected_lines[i]; i++)
548     assert_strings_equivalent (expected_lines[i], result_lines[i]);
549   g_strfreev (expected_lines);
550   g_strfreev (result_lines);
551   g_free (result);
552 }
553 
554 static void
assert_implementations(const gchar * interface,const gchar * expected,gboolean with_usr,gboolean with_home)555 assert_implementations (const gchar *interface,
556                         const gchar *expected,
557                         gboolean     with_usr,
558                         gboolean     with_home)
559 {
560   gchar *result;
561 
562   result = run_apps ("implementations", interface, with_usr, with_home, NULL, NULL, NULL);
563   g_strchomp (result);
564   assert_strings_equivalent (expected, result);
565   g_free (result);
566 }
567 
568 #define ALL_USR_APPS  "evince-previewer.desktop nautilus-classic.desktop gnome-font-viewer.desktop "         \
569                       "baobab.desktop yelp.desktop eog.desktop cheese.desktop org.gnome.clocks.desktop "         \
570                       "gnome-contacts.desktop kde4-kate.desktop gcr-prompter.desktop totem.desktop "         \
571                       "gnome-terminal.desktop nautilus-autorun-software.desktop gcr-viewer.desktop "         \
572                       "nautilus-connect-server.desktop kde4-dolphin.desktop gnome-music.desktop "            \
573                       "kde4-konqbrowser.desktop gucharmap.desktop kde4-okular.desktop nautilus.desktop "     \
574                       "gedit.desktop evince.desktop file-roller.desktop dconf-editor.desktop glade.desktop"
575 #define HOME_APPS     "epiphany-weather-for-toronto-island-9c6a4e022b17686306243dada811d550d25eb1fb.desktop"
576 #define ALL_HOME_APPS HOME_APPS " eog.desktop"
577 
578 static void
test_search(void)579 test_search (void)
580 {
581   assert_list ("", FALSE, FALSE, NULL, NULL);
582   assert_list (ALL_USR_APPS, TRUE, FALSE, NULL, NULL);
583   assert_list (ALL_HOME_APPS, FALSE, TRUE, NULL, NULL);
584   assert_list (ALL_USR_APPS " " HOME_APPS, TRUE, TRUE, NULL, NULL);
585 
586   /* The user has "installed" their own version of eog.desktop which
587    * calls it "Eye of GNOME".  Do some testing based on that.
588    *
589    * We should always find "Pictures" keyword no matter where we look.
590    */
591   assert_search ("Picture", "eog.desktop\n", TRUE, TRUE, NULL, NULL);
592   assert_search ("Picture", "eog.desktop\n", TRUE, FALSE, NULL, NULL);
593   assert_search ("Picture", "eog.desktop\n", FALSE, TRUE, NULL, NULL);
594   assert_search ("Picture", "", FALSE, FALSE, NULL, NULL);
595 
596   /* We should only find it called "eye of gnome" when using the user's
597    * directory.
598    */
599   assert_search ("eye gnome", "", TRUE, FALSE, NULL, NULL);
600   assert_search ("eye gnome", "eog.desktop\n", FALSE, TRUE, NULL, NULL);
601   assert_search ("eye gnome", "eog.desktop\n", TRUE, TRUE, NULL, NULL);
602 
603   /* We should only find it called "image viewer" when _not_ using the
604    * user's directory.
605    */
606   assert_search ("image viewer", "eog.desktop\n", TRUE, FALSE, NULL, NULL);
607   assert_search ("image viewer", "", FALSE, TRUE, NULL, NULL);
608   assert_search ("image viewer", "", TRUE, TRUE, NULL, NULL);
609 
610   /* There're "flatpak" apps (clocks) installed as well - they should *not*
611    * match the prefix command ("/bin/sh") in the Exec= line though.
612    */
613   assert_search ("sh", "gnome-terminal.desktop\n", TRUE, FALSE, NULL, NULL);
614 
615   /* "frobnicator.desktop" is ignored by get_all() because the binary is
616    * missing, but search should still find it (to avoid either stale results
617    * from the cache or expensive stat() calls for each potential result)
618    */
619   assert_search ("frobni", "frobnicator.desktop\n", TRUE, FALSE, NULL, NULL);
620 
621   /* Obvious multi-word search */
622   assert_search ("gno hel", "yelp.desktop\n", TRUE, TRUE, NULL, NULL);
623 
624   /* Repeated search terms should do nothing... */
625   assert_search ("files file fil fi f", "nautilus.desktop\n"
626                                         "gedit.desktop\n", TRUE, TRUE, NULL, NULL);
627 
628   /* "con" will match "connect" and "contacts" on name but dconf only on
629    * the "config" keyword
630    */
631   assert_search ("con", "nautilus-connect-server.desktop gnome-contacts.desktop\n"
632                         "dconf-editor.desktop\n", TRUE, TRUE, NULL, NULL);
633 
634   /* "gnome" will match "eye of gnome" from the user's directory, plus
635    * matching "GNOME Clocks" X-GNOME-FullName.  It's only a comment on
636    * yelp and gnome-contacts, though.
637    */
638   assert_search ("gnome", "eog.desktop\n"
639                           "org.gnome.clocks.desktop\n"
640                           "yelp.desktop gnome-contacts.desktop\n", TRUE, TRUE, NULL, NULL);
641 
642   /* eog has exec name 'false' in usr only */
643   assert_search ("false", "eog.desktop\n", TRUE, FALSE, NULL, NULL);
644   assert_search ("false", "", FALSE, TRUE, NULL, NULL);
645   assert_search ("false", "", TRUE, TRUE, NULL, NULL);
646   assert_search ("false", "", FALSE, FALSE, NULL, NULL);
647 
648   /* make sure we only search the first component */
649   assert_search ("nonsearchable", "", TRUE, FALSE, NULL, NULL);
650 
651   /* "gnome con" will match only gnome contacts; via the name for
652    * "contacts" and the comment for "gnome"
653    */
654   assert_search ("gnome con", "gnome-contacts.desktop\n", TRUE, TRUE, NULL, NULL);
655 
656   /* make sure we get the correct kde4- prefix on the application IDs
657    * from subdirectories
658    */
659   assert_search ("konq", "kde4-konqbrowser.desktop\n", TRUE, TRUE, NULL, NULL);
660   assert_search ("kate", "kde4-kate.desktop\n", TRUE, TRUE, NULL, NULL);
661 
662   /* make sure we can look up apps by name properly */
663   assert_info ("kde4-kate.desktop",
664                "kde4-kate.desktop\n"
665                "Kate\n"
666                "Kate\n"
667                "nil\n", TRUE, TRUE, NULL, NULL);
668 
669   assert_info ("nautilus.desktop",
670                "nautilus.desktop\n"
671                "Files\n"
672                "Files\n"
673                "Access and organize files\n", TRUE, TRUE, NULL, NULL);
674 
675   /* make sure localised searching works properly */
676   assert_search ("foliumi", "nautilus.desktop\n"
677                             "kde4-konqbrowser.desktop\n"
678                             "eog.desktop\n", TRUE, FALSE, "en_US.UTF-8", "eo");
679   /* the user's eog.desktop has no translations... */
680   assert_search ("foliumi", "nautilus.desktop\n"
681                             "kde4-konqbrowser.desktop\n", TRUE, TRUE, "en_US.UTF-8", "eo");
682 }
683 
684 static void
test_implements(void)685 test_implements (void)
686 {
687   /* Make sure we can find our search providers... */
688   assert_implementations ("org.gnome.Shell.SearchProvider2",
689                        "gnome-music.desktop gnome-contacts.desktop eog.desktop",
690                        TRUE, FALSE);
691 
692   /* And our image acquisition possibilities... */
693   assert_implementations ("org.freedesktop.ImageProvider",
694                        "cheese.desktop",
695                        TRUE, FALSE);
696 
697   /* Make sure the user's eog is properly masking the system one */
698   assert_implementations ("org.gnome.Shell.SearchProvider2",
699                        "gnome-music.desktop gnome-contacts.desktop",
700                        TRUE, TRUE);
701 
702   /* Make sure we get nothing if we have nothing */
703   assert_implementations ("org.gnome.Shell.SearchProvider2", "", FALSE, FALSE);
704 }
705 
706 static void
assert_shown(const gchar * desktop_id,gboolean expected,const gchar * xdg_current_desktop)707 assert_shown (const gchar *desktop_id,
708               gboolean     expected,
709               const gchar *xdg_current_desktop)
710 {
711   gchar *result;
712 
713   result = run_apps ("should-show", desktop_id, TRUE, TRUE, NULL, NULL, xdg_current_desktop);
714   g_assert_cmpstr (result, ==, expected ? "true\n" : "false\n");
715   g_free (result);
716 }
717 
718 static void
test_show_in(void)719 test_show_in (void)
720 {
721   assert_shown ("gcr-prompter.desktop", FALSE, NULL);
722   assert_shown ("gcr-prompter.desktop", FALSE, "GNOME");
723   assert_shown ("gcr-prompter.desktop", FALSE, "KDE");
724   assert_shown ("gcr-prompter.desktop", FALSE, "GNOME:GNOME-Classic");
725   assert_shown ("gcr-prompter.desktop", TRUE, "GNOME-Classic:GNOME");
726   assert_shown ("gcr-prompter.desktop", TRUE, "GNOME-Classic");
727   assert_shown ("gcr-prompter.desktop", TRUE, "GNOME-Classic:KDE");
728   assert_shown ("gcr-prompter.desktop", TRUE, "KDE:GNOME-Classic");
729 }
730 
731 /* Test g_desktop_app_info_launch_uris_as_manager() and
732  * g_desktop_app_info_launch_uris_as_manager_with_fds()
733  */
734 static void
test_launch_as_manager(void)735 test_launch_as_manager (void)
736 {
737   GDesktopAppInfo *appinfo;
738   GError *error = NULL;
739   gboolean retval;
740   const gchar *path;
741 
742   if (g_getenv ("DISPLAY") == NULL || g_getenv ("DISPLAY")[0] == '\0')
743     {
744       g_test_skip ("No DISPLAY.  Skipping test.");
745       return;
746     }
747 
748   path = g_test_get_filename (G_TEST_BUILT, "appinfo-test.desktop", NULL);
749   appinfo = g_desktop_app_info_new_from_filename (path);
750 
751   if (appinfo == NULL)
752     {
753       g_test_skip ("appinfo-test binary not installed");
754       return;
755     }
756 
757   retval = g_desktop_app_info_launch_uris_as_manager (appinfo, NULL, NULL, 0,
758                                                       NULL, NULL,
759                                                       NULL, NULL,
760                                                       &error);
761   g_assert_no_error (error);
762   g_assert_true (retval);
763 
764   retval = g_desktop_app_info_launch_uris_as_manager_with_fds (appinfo,
765                                                                NULL, NULL, 0,
766                                                                NULL, NULL,
767                                                                NULL, NULL,
768                                                                -1, -1, -1,
769                                                                &error);
770   g_assert_no_error (error);
771   g_assert_true (retval);
772 
773   g_object_unref (appinfo);
774 }
775 
776 int
main(int argc,char * argv[])777 main (int   argc,
778       char *argv[])
779 {
780   /* While we use %G_TEST_OPTION_ISOLATE_DIRS to create temporary directories
781    * for each of the tests, we want to use the system MIME registry, assuming
782    * that it exists and correctly has shared-mime-info installed. */
783   g_content_type_set_mime_dirs (NULL);
784 
785   g_test_init (&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL);
786 
787   g_test_add_func ("/desktop-app-info/delete", test_delete);
788   g_test_add_func ("/desktop-app-info/default", test_default);
789   g_test_add_func ("/desktop-app-info/fallback", test_fallback);
790   g_test_add_func ("/desktop-app-info/lastused", test_last_used);
791   g_test_add_func ("/desktop-app-info/extra-getters", test_extra_getters);
792   g_test_add_func ("/desktop-app-info/actions", test_actions);
793   g_test_add_func ("/desktop-app-info/search", test_search);
794   g_test_add_func ("/desktop-app-info/implements", test_implements);
795   g_test_add_func ("/desktop-app-info/show-in", test_show_in);
796   g_test_add_func ("/desktop-app-info/launch-as-manager", test_launch_as_manager);
797 
798   return g_test_run ();
799 }
800