• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 #include <locale.h>
3 #include <string.h>
4 
5 #include <glib/gstdio.h>
6 #include <gio/gio.h>
7 #include <gio/gdesktopappinfo.h>
8 
9 static void
test_launch_for_app_info(GAppInfo * appinfo)10 test_launch_for_app_info (GAppInfo *appinfo)
11 {
12   GError *error = NULL;
13   gboolean success;
14   GFile *file;
15   GList *l;
16   const gchar *path;
17   gchar *uri;
18 
19   if (g_getenv ("DISPLAY") == NULL || g_getenv ("DISPLAY")[0] == '\0')
20     {
21       g_test_skip ("No DISPLAY set");
22       return;
23     }
24 
25   success = g_app_info_launch (appinfo, NULL, NULL, &error);
26   g_assert_no_error (error);
27   g_assert_true (success);
28 
29   success = g_app_info_launch_uris (appinfo, NULL, NULL, &error);
30   g_assert_no_error (error);
31   g_assert_true (success);
32 
33   path = g_test_get_filename (G_TEST_BUILT, "appinfo-test.desktop", NULL);
34   file = g_file_new_for_path (path);
35   l = NULL;
36   l = g_list_append (l, file);
37 
38   success = g_app_info_launch (appinfo, l, NULL, &error);
39   g_assert_no_error (error);
40   g_assert_true (success);
41   g_list_free (l);
42   g_object_unref (file);
43 
44   l = NULL;
45   uri = g_strconcat ("file://", g_test_get_dir (G_TEST_BUILT), "/appinfo-test.desktop", NULL);
46   l = g_list_append (l, uri);
47   l = g_list_append (l, "file:///etc/group#adm");
48 
49   success = g_app_info_launch_uris (appinfo, l, NULL, &error);
50   g_assert_no_error (error);
51   g_assert_true (success);
52 
53   g_list_free (l);
54   g_free (uri);
55 }
56 
57 static void
test_launch(void)58 test_launch (void)
59 {
60   GAppInfo *appinfo;
61   const gchar *path;
62 
63   path = g_test_get_filename (G_TEST_BUILT, "appinfo-test.desktop", NULL);
64   appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (path);
65 
66   if (appinfo == NULL)
67     {
68       g_test_skip ("appinfo-test binary not installed");
69       return;
70     }
71 
72   test_launch_for_app_info (appinfo);
73   g_object_unref (appinfo);
74 }
75 
76 static void
test_launch_no_app_id(void)77 test_launch_no_app_id (void)
78 {
79   const gchar desktop_file_base_contents[] =
80     "[Desktop Entry]\n"
81     "Type=Application\n"
82     "GenericName=generic-appinfo-test\n"
83     "Name=appinfo-test\n"
84     "Name[de]=appinfo-test-de\n"
85     "X-GNOME-FullName=example\n"
86     "X-GNOME-FullName[de]=Beispiel\n"
87     "Comment=GAppInfo example\n"
88     "Comment[de]=GAppInfo Beispiel\n"
89     "Icon=testicon.svg\n"
90     "Terminal=false\n"
91     "StartupNotify=true\n"
92     "StartupWMClass=appinfo-class\n"
93     "MimeType=image/png;image/jpeg;\n"
94     "Keywords=keyword1;test keyword;\n"
95     "Categories=GNOME;GTK;\n";
96 
97   gchar *exec_line_variants[2];
98   gsize i;
99 
100   exec_line_variants[0] = g_strdup_printf (
101       "Exec=%s/appinfo-test --option %%U %%i --name %%c --filename %%k %%m %%%%",
102       g_test_get_dir (G_TEST_BUILT));
103   exec_line_variants[1] = g_strdup_printf (
104       "Exec=%s/appinfo-test --option %%u %%i --name %%c --filename %%k %%m %%%%",
105       g_test_get_dir (G_TEST_BUILT));
106 
107   g_test_bug ("791337");
108 
109   for (i = 0; i < G_N_ELEMENTS (exec_line_variants); i++)
110     {
111       gchar *desktop_file_contents;
112       GKeyFile *fake_desktop_file;
113       GAppInfo *appinfo;
114       gboolean loaded;
115 
116       g_test_message ("Exec line variant #%" G_GSIZE_FORMAT, i);
117 
118       desktop_file_contents = g_strdup_printf ("%s\n%s",
119                                                desktop_file_base_contents,
120                                                exec_line_variants[i]);
121 
122       /* We load a desktop file from memory to force the app not
123        * to have an app ID, which would check different codepaths.
124        */
125       fake_desktop_file = g_key_file_new ();
126       loaded = g_key_file_load_from_data (fake_desktop_file, desktop_file_contents, -1, G_KEY_FILE_NONE, NULL);
127       g_assert_true (loaded);
128 
129       appinfo = (GAppInfo*)g_desktop_app_info_new_from_keyfile (fake_desktop_file);
130       g_assert_nonnull (appinfo);
131 
132       test_launch_for_app_info (appinfo);
133 
134       g_free (desktop_file_contents);
135       g_object_unref (appinfo);
136       g_key_file_unref (fake_desktop_file);
137     }
138 
139   g_free (exec_line_variants[1]);
140   g_free (exec_line_variants[0]);
141 }
142 
143 static void
test_locale(const char * locale)144 test_locale (const char *locale)
145 {
146   GAppInfo *appinfo;
147   gchar *orig = NULL;
148   const gchar *path;
149 
150   orig = g_strdup (setlocale (LC_ALL, NULL));
151   g_setenv ("LANGUAGE", locale, TRUE);
152   setlocale (LC_ALL, "");
153 
154   path = g_test_get_filename (G_TEST_DIST, "appinfo-test-static.desktop", NULL);
155   appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (path);
156 
157   if (g_strcmp0 (locale, "C") == 0)
158     {
159       g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "appinfo-test");
160       g_assert_cmpstr (g_app_info_get_description (appinfo), ==, "GAppInfo example");
161       g_assert_cmpstr (g_app_info_get_display_name (appinfo), ==, "example");
162     }
163   else if (g_str_has_prefix (locale, "en"))
164     {
165       g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "appinfo-test");
166       g_assert_cmpstr (g_app_info_get_description (appinfo), ==, "GAppInfo example");
167       g_assert_cmpstr (g_app_info_get_display_name (appinfo), ==, "example");
168     }
169   else if (g_str_has_prefix (locale, "de"))
170     {
171       g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "appinfo-test-de");
172       g_assert_cmpstr (g_app_info_get_description (appinfo), ==, "GAppInfo Beispiel");
173       g_assert_cmpstr (g_app_info_get_display_name (appinfo), ==, "Beispiel");
174     }
175 
176   g_object_unref (appinfo);
177 
178   g_setenv ("LANGUAGE", orig, TRUE);
179   setlocale (LC_ALL, "");
180   g_free (orig);
181 }
182 
183 static void
test_text(void)184 test_text (void)
185 {
186   test_locale ("C");
187   test_locale ("en_US");
188   test_locale ("de");
189   test_locale ("de_DE.UTF-8");
190 }
191 
192 static void
test_basic(void)193 test_basic (void)
194 {
195   GAppInfo *appinfo;
196   GAppInfo *appinfo2;
197   GIcon *icon, *icon2;
198   const gchar *path;
199 
200   path = g_test_get_filename (G_TEST_DIST, "appinfo-test-static.desktop", NULL);
201   appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (path);
202   g_assert_nonnull (appinfo);
203 
204   g_assert_cmpstr (g_app_info_get_id (appinfo), ==, "appinfo-test-static.desktop");
205   g_assert_nonnull (strstr (g_app_info_get_executable (appinfo), "true"));
206 
207   icon = g_app_info_get_icon (appinfo);
208   g_assert_true (G_IS_THEMED_ICON (icon));
209   icon2 = g_themed_icon_new ("testicon");
210   g_assert_true (g_icon_equal (icon, icon2));
211   g_object_unref (icon2);
212 
213   appinfo2 = g_app_info_dup (appinfo);
214   g_assert_cmpstr (g_app_info_get_id (appinfo), ==, g_app_info_get_id (appinfo2));
215   g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, g_app_info_get_commandline (appinfo2));
216 
217   g_object_unref (appinfo);
218   g_object_unref (appinfo2);
219 }
220 
221 static void
test_show_in(void)222 test_show_in (void)
223 {
224   GAppInfo *appinfo;
225   const gchar *path;
226 
227   path = g_test_get_filename (G_TEST_BUILT, "appinfo-test.desktop", NULL);
228   appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (path);
229 
230   if (appinfo == NULL)
231     {
232       g_test_skip ("appinfo-test binary not installed");
233       return;
234     }
235 
236   g_assert_true (g_app_info_should_show (appinfo));
237   g_object_unref (appinfo);
238 
239   path = g_test_get_filename (G_TEST_BUILT, "appinfo-test-gnome.desktop", NULL);
240   appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (path);
241   g_assert_true (g_app_info_should_show (appinfo));
242   g_object_unref (appinfo);
243 
244   path = g_test_get_filename (G_TEST_BUILT, "appinfo-test-notgnome.desktop", NULL);
245   appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (path);
246   g_assert_false (g_app_info_should_show (appinfo));
247   g_object_unref (appinfo);
248 }
249 
250 static void
test_commandline(void)251 test_commandline (void)
252 {
253   GAppInfo *appinfo;
254   GError *error;
255   gchar *cmdline;
256   gchar *cmdline_out;
257 
258   cmdline = g_strconcat (g_test_get_dir (G_TEST_BUILT), "/appinfo-test --option", NULL);
259   cmdline_out = g_strconcat (cmdline, " %u", NULL);
260 
261   error = NULL;
262   appinfo = g_app_info_create_from_commandline (cmdline,
263                                                 "cmdline-app-test",
264                                                 G_APP_INFO_CREATE_SUPPORTS_URIS,
265                                                 &error);
266   g_assert_no_error (error);
267   g_assert_nonnull (appinfo);
268   g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "cmdline-app-test");
269   g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, cmdline_out);
270   g_assert_true (g_app_info_supports_uris (appinfo));
271   g_assert_false (g_app_info_supports_files (appinfo));
272 
273   g_object_unref (appinfo);
274 
275   g_free (cmdline_out);
276   cmdline_out = g_strconcat (cmdline, " %f", NULL);
277 
278   error = NULL;
279   appinfo = g_app_info_create_from_commandline (cmdline,
280                                                 "cmdline-app-test",
281                                                 G_APP_INFO_CREATE_NONE,
282                                                 &error);
283   g_assert_no_error (error);
284   g_assert_nonnull (appinfo);
285   g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "cmdline-app-test");
286   g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, cmdline_out);
287   g_assert_false (g_app_info_supports_uris (appinfo));
288   g_assert_true (g_app_info_supports_files (appinfo));
289 
290   g_object_unref (appinfo);
291 
292   g_free (cmdline);
293   g_free (cmdline_out);
294 }
295 
296 static void
test_launch_context(void)297 test_launch_context (void)
298 {
299   GAppLaunchContext *context;
300   GAppInfo *appinfo;
301   gchar *str;
302   gchar *cmdline;
303 
304   cmdline = g_strconcat (g_test_get_dir (G_TEST_BUILT), "/appinfo-test --option", NULL);
305 
306   context = g_app_launch_context_new ();
307   appinfo = g_app_info_create_from_commandline (cmdline,
308                                                 "cmdline-app-test",
309                                                 G_APP_INFO_CREATE_SUPPORTS_URIS,
310                                                 NULL);
311 
312   str = g_app_launch_context_get_display (context, appinfo, NULL);
313   g_assert_null (str);
314 
315   str = g_app_launch_context_get_startup_notify_id (context, appinfo, NULL);
316   g_assert_null (str);
317 
318   g_object_unref (appinfo);
319   g_object_unref (context);
320 
321   g_free (cmdline);
322 }
323 
324 static gboolean launched_reached;
325 
326 static void
launched(GAppLaunchContext * context,GAppInfo * info,GVariant * platform_data,gpointer user_data)327 launched (GAppLaunchContext *context,
328           GAppInfo          *info,
329           GVariant          *platform_data,
330           gpointer           user_data)
331 {
332   gint pid;
333 
334   pid = 0;
335   g_assert_true (g_variant_lookup (platform_data, "pid", "i", &pid));
336   g_assert_cmpint (pid, !=, 0);
337 
338   launched_reached = TRUE;
339 }
340 
341 static void
launch_failed(GAppLaunchContext * context,const gchar * startup_notify_id)342 launch_failed (GAppLaunchContext *context,
343                const gchar       *startup_notify_id)
344 {
345   g_assert_not_reached ();
346 }
347 
348 static void
test_launch_context_signals(void)349 test_launch_context_signals (void)
350 {
351   GAppLaunchContext *context;
352   GAppInfo *appinfo;
353   GError *error = NULL;
354   gboolean success;
355   gchar *cmdline;
356 
357   cmdline = g_strconcat (g_test_get_dir (G_TEST_BUILT), "/appinfo-test --option", NULL);
358 
359   context = g_app_launch_context_new ();
360   g_signal_connect (context, "launched", G_CALLBACK (launched), NULL);
361   g_signal_connect (context, "launch_failed", G_CALLBACK (launch_failed), NULL);
362   appinfo = g_app_info_create_from_commandline (cmdline,
363                                                 "cmdline-app-test",
364                                                 G_APP_INFO_CREATE_SUPPORTS_URIS,
365                                                 NULL);
366 
367   success = g_app_info_launch (appinfo, NULL, context, &error);
368   g_assert_no_error (error);
369   g_assert_true (success);
370 
371   g_assert_true (launched_reached);
372 
373   g_object_unref (appinfo);
374   g_object_unref (context);
375 
376   g_free (cmdline);
377 }
378 
379 static void
test_tryexec(void)380 test_tryexec (void)
381 {
382   GAppInfo *appinfo;
383   const gchar *path;
384 
385   path = g_test_get_filename (G_TEST_BUILT, "appinfo-test2.desktop", NULL);
386   appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (path);
387 
388   g_assert_null (appinfo);
389 }
390 
391 /* Test that we can set an appinfo as default for a mime type or
392  * file extension, and also add and remove handled mime types.
393  */
394 static void
test_associations(void)395 test_associations (void)
396 {
397   GAppInfo *appinfo;
398   GAppInfo *appinfo2;
399   GError *error = NULL;
400   gboolean result;
401   GList *list;
402   gchar *cmdline;
403 
404   cmdline = g_strconcat (g_test_get_dir (G_TEST_BUILT), "/appinfo-test --option", NULL);
405   appinfo = g_app_info_create_from_commandline (cmdline,
406                                                 "cmdline-app-test",
407                                                 G_APP_INFO_CREATE_SUPPORTS_URIS,
408                                                 NULL);
409   g_free (cmdline);
410 
411   result = g_app_info_set_as_default_for_type (appinfo, "application/x-glib-test", &error);
412   g_assert_no_error (error);
413   g_assert_true (result);
414 
415   appinfo2 = g_app_info_get_default_for_type ("application/x-glib-test", FALSE);
416 
417   g_assert_nonnull (appinfo2);
418   g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, g_app_info_get_commandline (appinfo2));
419 
420   g_object_unref (appinfo2);
421 
422   result = g_app_info_set_as_default_for_extension (appinfo, "gio-tests", &error);
423   g_assert_no_error (error);
424   g_assert_true (result);
425 
426   appinfo2 = g_app_info_get_default_for_type ("application/x-extension-gio-tests", FALSE);
427 
428   g_assert_nonnull (appinfo2);
429   g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, g_app_info_get_commandline (appinfo2));
430 
431   g_object_unref (appinfo2);
432 
433   result = g_app_info_add_supports_type (appinfo, "application/x-gio-test", &error);
434   g_assert_no_error (error);
435   g_assert_true (result);
436 
437   list = g_app_info_get_all_for_type ("application/x-gio-test");
438   g_assert_cmpint (g_list_length (list), ==, 1);
439   appinfo2 = list->data;
440   g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, g_app_info_get_commandline (appinfo2));
441   g_object_unref (appinfo2);
442   g_list_free (list);
443 
444   g_assert_true (g_app_info_can_remove_supports_type (appinfo));
445   result = g_app_info_remove_supports_type (appinfo, "application/x-gio-test", &error);
446   g_assert_no_error (error);
447   g_assert_true (result);
448 
449   g_assert_true (g_app_info_can_delete (appinfo));
450   g_assert_true (g_app_info_delete (appinfo));
451   g_object_unref (appinfo);
452 }
453 
454 static void
test_environment(void)455 test_environment (void)
456 {
457   GAppLaunchContext *ctx;
458   gchar **env;
459   const gchar *path;
460 
461   g_unsetenv ("FOO");
462   g_unsetenv ("BLA");
463   path = g_getenv ("PATH");
464 
465   ctx = g_app_launch_context_new ();
466 
467   env = g_app_launch_context_get_environment (ctx);
468 
469   g_assert_null (g_environ_getenv (env, "FOO"));
470   g_assert_null (g_environ_getenv (env, "BLA"));
471   g_assert_cmpstr (g_environ_getenv (env, "PATH"), ==, path);
472 
473   g_strfreev (env);
474 
475   g_app_launch_context_setenv (ctx, "FOO", "bar");
476   g_app_launch_context_setenv (ctx, "BLA", "bla");
477 
478   env = g_app_launch_context_get_environment (ctx);
479 
480   g_assert_cmpstr (g_environ_getenv (env, "FOO"), ==, "bar");
481   g_assert_cmpstr (g_environ_getenv (env, "BLA"), ==, "bla");
482   g_assert_cmpstr (g_environ_getenv (env, "PATH"), ==, path);
483 
484   g_strfreev (env);
485 
486   g_app_launch_context_setenv (ctx, "FOO", "baz");
487   g_app_launch_context_unsetenv (ctx, "BLA");
488 
489   env = g_app_launch_context_get_environment (ctx);
490 
491   g_assert_cmpstr (g_environ_getenv (env, "FOO"), ==, "baz");
492   g_assert_null (g_environ_getenv (env, "BLA"));
493 
494   g_strfreev (env);
495 
496   g_object_unref (ctx);
497 }
498 
499 static void
test_startup_wm_class(void)500 test_startup_wm_class (void)
501 {
502   GDesktopAppInfo *appinfo;
503   const char *wm_class;
504   const gchar *path;
505 
506   path = g_test_get_filename (G_TEST_DIST, "appinfo-test-static.desktop", NULL);
507   appinfo = g_desktop_app_info_new_from_filename (path);
508   wm_class = g_desktop_app_info_get_startup_wm_class (appinfo);
509 
510   g_assert_cmpstr (wm_class, ==, "appinfo-class");
511 
512   g_object_unref (appinfo);
513 }
514 
515 static void
test_supported_types(void)516 test_supported_types (void)
517 {
518   GAppInfo *appinfo;
519   const char * const *content_types;
520   const gchar *path;
521 
522   path = g_test_get_filename (G_TEST_DIST, "appinfo-test-static.desktop", NULL);
523   appinfo = G_APP_INFO (g_desktop_app_info_new_from_filename (path));
524   content_types = g_app_info_get_supported_types (appinfo);
525 
526   g_assert_cmpint (g_strv_length ((char**)content_types), ==, 2);
527   g_assert_cmpstr (content_types[0], ==, "image/png");
528 
529   g_object_unref (appinfo);
530 }
531 
532 static void
test_from_keyfile(void)533 test_from_keyfile (void)
534 {
535   GDesktopAppInfo *info;
536   GKeyFile *kf;
537   GError *error = NULL;
538   const gchar *categories;
539   gchar **categories_list;
540   gsize categories_count;
541   gchar **keywords;
542   const gchar *file;
543   const gchar *name;
544   const gchar *path;
545 
546   path = g_test_get_filename (G_TEST_DIST, "appinfo-test-static.desktop", NULL);
547   kf = g_key_file_new ();
548   g_key_file_load_from_file (kf, path, G_KEY_FILE_NONE, &error);
549   g_assert_no_error (error);
550   info = g_desktop_app_info_new_from_keyfile (kf);
551   g_key_file_unref (kf);
552   g_assert_nonnull (info);
553 
554   g_object_get (info, "filename", &file, NULL);
555   g_assert_null (file);
556 
557   file = g_desktop_app_info_get_filename (info);
558   g_assert_null (file);
559   categories = g_desktop_app_info_get_categories (info);
560   g_assert_cmpstr (categories, ==, "GNOME;GTK;");
561   categories_list = g_desktop_app_info_get_string_list (info, "Categories", &categories_count);
562   g_assert_cmpint (categories_count, ==, 2);
563   g_assert_cmpint (g_strv_length (categories_list), ==, 2);
564   g_assert_cmpstr (categories_list[0], ==, "GNOME");
565   g_assert_cmpstr (categories_list[1], ==, "GTK");
566   keywords = (gchar **)g_desktop_app_info_get_keywords (info);
567   g_assert_cmpint (g_strv_length (keywords), ==, 2);
568   g_assert_cmpstr (keywords[0], ==, "keyword1");
569   g_assert_cmpstr (keywords[1], ==, "test keyword");
570   name = g_desktop_app_info_get_generic_name (info);
571   g_assert_cmpstr (name, ==, "generic-appinfo-test");
572   g_assert_false (g_desktop_app_info_get_nodisplay (info));
573 
574   g_strfreev (categories_list);
575   g_object_unref (info);
576 }
577 
578 int
main(int argc,char * argv[])579 main (int argc, char *argv[])
580 {
581   g_setenv ("XDG_CURRENT_DESKTOP", "GNOME", TRUE);
582 
583   g_test_init (&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL);
584   g_test_bug_base ("https://bugzilla.gnome.org/show_bug.cgi?id=");
585 
586   g_test_add_func ("/appinfo/basic", test_basic);
587   g_test_add_func ("/appinfo/text", test_text);
588   g_test_add_func ("/appinfo/launch", test_launch);
589   g_test_add_func ("/appinfo/launch/no-appid", test_launch_no_app_id);
590   g_test_add_func ("/appinfo/show-in", test_show_in);
591   g_test_add_func ("/appinfo/commandline", test_commandline);
592   g_test_add_func ("/appinfo/launch-context", test_launch_context);
593   g_test_add_func ("/appinfo/launch-context-signals", test_launch_context_signals);
594   g_test_add_func ("/appinfo/tryexec", test_tryexec);
595   g_test_add_func ("/appinfo/associations", test_associations);
596   g_test_add_func ("/appinfo/environment", test_environment);
597   g_test_add_func ("/appinfo/startup-wm-class", test_startup_wm_class);
598   g_test_add_func ("/appinfo/supported-types", test_supported_types);
599   g_test_add_func ("/appinfo/from-keyfile", test_from_keyfile);
600 
601   return g_test_run ();
602 }
603