Convert remaining gio tests to installed
[platform/upstream/glib.git] / gio / tests / appinfo.c
1
2 #include <locale.h>
3
4 #include <gio/gio.h>
5 #include <gio/gdesktopappinfo.h>
6
7 static const gchar *datapath;
8
9 static void
10 test_launch (void)
11 {
12   GAppInfo *appinfo;
13   GError *error;
14   GFile *file;
15   GList *l;
16   gchar *path;
17
18   path = g_build_filename (datapath, "appinfo-test.desktop", NULL);
19   appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (path);
20   g_assert (appinfo != NULL);
21   g_free (path);
22
23   error = NULL;
24   g_assert (g_app_info_launch (appinfo, NULL, NULL, &error));
25   g_assert_no_error (error);
26
27   g_assert (g_app_info_launch_uris (appinfo, NULL, NULL, &error));
28   g_assert_no_error (error);
29
30   path = g_build_filename (datapath, "appinfo-test.desktop", NULL);
31   file = g_file_new_for_path (path);
32   g_free (path);
33   l = NULL;
34   l = g_list_append (l, file);
35
36   g_assert (g_app_info_launch (appinfo, l, NULL, &error));
37   g_assert_no_error (error);
38   g_list_free (l);
39   g_object_unref (file);
40
41   l = NULL;
42   path = g_strconcat ("file://", datapath, "/appinfo-test.desktop", NULL);
43   l = g_list_append (l, path);
44   l = g_list_append (l, "file:///etc/group#adm");
45
46   g_assert (g_app_info_launch_uris (appinfo, l, NULL, &error));
47   g_assert_no_error (error);
48   g_list_free (l);
49   g_free (path);
50
51   g_object_unref (appinfo);
52 }
53
54 static void
55 test_locale (const char *locale)
56 {
57   GAppInfo *appinfo;
58   const gchar *orig;
59   gchar *path;
60
61   orig = setlocale (LC_ALL, NULL);
62   g_setenv ("LANGUAGE", locale, TRUE);
63   setlocale (LC_ALL, "");
64
65   path = g_build_filename (datapath, "appinfo-test.desktop", NULL);
66   appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (path);
67   g_free (path);
68
69   if (g_strcmp0 (locale, "C") == 0)
70     {
71       g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "appinfo-test");
72       g_assert_cmpstr (g_app_info_get_description (appinfo), ==, "GAppInfo example");
73       g_assert_cmpstr (g_app_info_get_display_name (appinfo), ==, "example");
74     }
75   else if (g_str_has_prefix (locale, "en"))
76     {
77       g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "appinfo-test");
78       g_assert_cmpstr (g_app_info_get_description (appinfo), ==, "GAppInfo example");
79       g_assert_cmpstr (g_app_info_get_display_name (appinfo), ==, "example");
80     }
81   else if (g_str_has_prefix (locale, "de"))
82     {
83       g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "appinfo-test-de");
84       g_assert_cmpstr (g_app_info_get_description (appinfo), ==, "GAppInfo Beispiel");
85       g_assert_cmpstr (g_app_info_get_display_name (appinfo), ==, "Beispiel");
86     }
87
88   g_object_unref (appinfo);
89
90   g_setenv ("LANGUAGE", orig, TRUE);
91   setlocale (LC_ALL, "");
92 }
93
94 static void
95 test_text (void)
96 {
97   test_locale ("C");
98   test_locale ("en_US");
99   test_locale ("de");
100   test_locale ("de_DE.UTF-8");
101 }
102
103 static void
104 test_basic (void)
105 {
106   GAppInfo *appinfo;
107   GAppInfo *appinfo2;
108   GIcon *icon, *icon2;
109   gchar *path;
110
111   path = g_build_filename (datapath, "appinfo-test.desktop", NULL);
112   appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (path);
113   g_free (path);
114
115   g_assert_cmpstr (g_app_info_get_id (appinfo), ==, "appinfo-test.desktop");
116   g_assert_cmpstr (g_app_info_get_executable (appinfo), ==, "./appinfo-test");
117
118   icon = g_app_info_get_icon (appinfo);
119   g_assert (G_IS_THEMED_ICON (icon));
120   icon2 = g_themed_icon_new ("testicon");
121   g_assert (g_icon_equal (icon, icon2));
122   g_object_unref (icon2);
123
124   appinfo2 = g_app_info_dup (appinfo);
125   g_assert_cmpstr (g_app_info_get_id (appinfo), ==, g_app_info_get_id (appinfo2));
126   g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, g_app_info_get_commandline (appinfo2));
127
128   g_object_unref (appinfo);
129   g_object_unref (appinfo2);
130 }
131
132 static void
133 test_show_in (void)
134 {
135   GAppInfo *appinfo;
136   gchar *path;
137
138   g_desktop_app_info_set_desktop_env ("GNOME");
139
140   path = g_build_filename (datapath, "appinfo-test.desktop", NULL);
141   appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (path);
142   g_assert (g_app_info_should_show (appinfo));
143   g_object_unref (appinfo);
144   g_free (path);
145
146   path = g_build_filename (datapath, "appinfo-test-gnome.desktop", NULL);
147   appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (path);
148   g_assert (g_app_info_should_show (appinfo));
149   g_object_unref (appinfo);
150   g_free (path);
151
152   path = g_build_filename (datapath, "appinfo-test-notgnome.desktop", NULL);
153   appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (path);
154   g_assert (!g_app_info_should_show (appinfo));
155   g_object_unref (appinfo);
156   g_free (path);
157 }
158
159 static void
160 test_commandline (void)
161 {
162   GAppInfo *appinfo;
163   GError *error;
164   gchar *cmdline;
165   gchar *cmdline_out;
166
167   cmdline = g_strconcat (datapath, "/appinfo-test --option", NULL);
168   cmdline_out = g_strconcat (cmdline, " %u", NULL);
169
170   error = NULL;
171   appinfo = g_app_info_create_from_commandline (cmdline,
172                                                 "cmdline-app-test",
173                                                 G_APP_INFO_CREATE_SUPPORTS_URIS,
174                                                 &error);
175   g_assert (appinfo != NULL);
176   g_assert_no_error (error);
177   g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "cmdline-app-test");
178   g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, cmdline_out);
179   g_assert (g_app_info_supports_uris (appinfo));
180   g_assert (!g_app_info_supports_files (appinfo));
181
182   g_object_unref (appinfo);
183
184   g_free (cmdline_out);
185   cmdline_out = g_strconcat (cmdline, " %f", NULL);
186
187   error = NULL;
188   appinfo = g_app_info_create_from_commandline (cmdline,
189                                                 "cmdline-app-test",
190                                                 G_APP_INFO_CREATE_NONE,
191                                                 &error);
192   g_assert (appinfo != NULL);
193   g_assert_no_error (error);
194   g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "cmdline-app-test");
195   g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, cmdline_out);
196   g_assert (!g_app_info_supports_uris (appinfo));
197   g_assert (g_app_info_supports_files (appinfo));
198
199   g_object_unref (appinfo);
200
201   g_free (cmdline);
202   g_free (cmdline_out);
203 }
204
205 static void
206 test_launch_context (void)
207 {
208   GAppLaunchContext *context;
209   GAppInfo *appinfo;
210   gchar *str;
211   gchar *cmdline;
212
213   cmdline = g_strconcat (datapath, "/appinfo-test --option", NULL);
214
215   context = g_app_launch_context_new ();
216   appinfo = g_app_info_create_from_commandline (cmdline,
217                                                 "cmdline-app-test",
218                                                 G_APP_INFO_CREATE_SUPPORTS_URIS,
219                                                 NULL);
220
221   str = g_app_launch_context_get_display (context, appinfo, NULL);
222   g_assert (str == NULL);
223
224   str = g_app_launch_context_get_startup_notify_id (context, appinfo, NULL);
225   g_assert (str == NULL);
226
227   g_object_unref (appinfo);
228   g_object_unref (context);
229
230   g_free (cmdline);
231 }
232
233 static gboolean launched_reached;
234
235 static void
236 launched (GAppLaunchContext *context,
237           GAppInfo          *info,
238           GVariant          *platform_data,
239           gpointer           user_data)
240 {
241   gint pid;
242
243   pid = 0;
244   g_assert (g_variant_lookup (platform_data, "pid", "i", &pid));
245   g_assert (pid != 0);
246
247   launched_reached = TRUE;
248 }
249
250 static void
251 launch_failed (GAppLaunchContext *context,
252                const gchar       *startup_notify_id)
253 {
254   g_assert_not_reached ();
255 }
256
257 static void
258 test_launch_context_signals (void)
259 {
260   GAppLaunchContext *context;
261   GAppInfo *appinfo;
262   GError *error = NULL;
263   gchar *cmdline;
264
265   cmdline = g_strconcat (datapath, "/appinfo-test --option", NULL);
266
267   context = g_app_launch_context_new ();
268   g_signal_connect (context, "launched", G_CALLBACK (launched), NULL);
269   g_signal_connect (context, "launch_failed", G_CALLBACK (launch_failed), NULL);
270   appinfo = g_app_info_create_from_commandline (cmdline,
271                                                 "cmdline-app-test",
272                                                 G_APP_INFO_CREATE_SUPPORTS_URIS,
273                                                 NULL);
274
275   error = NULL;
276   g_assert (g_app_info_launch (appinfo, NULL, context, &error));
277   g_assert_no_error (error);
278
279   g_assert (launched_reached);
280
281   g_object_unref (appinfo);
282   g_object_unref (context);
283
284   g_free (cmdline);
285 }
286
287 static void
288 test_tryexec (void)
289 {
290   GAppInfo *appinfo;
291   gchar *path;
292
293   path = g_build_filename (datapath, "appinfo-test2.desktop", NULL);
294   appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (path);
295
296   g_assert (appinfo == NULL);
297
298   g_free (path);
299 }
300
301 /* Test that we can set an appinfo as default for a mime type or
302  * file extension, and also add and remove handled mime types.
303  */
304 static void
305 test_associations (void)
306 {
307   GAppInfo *appinfo;
308   GAppInfo *appinfo2;
309   GError *error;
310   gboolean result;
311   GList *list;
312   gchar *cmdline;
313
314   cmdline = g_strconcat (datapath, "/appinfo-test --option", NULL);
315   appinfo = g_app_info_create_from_commandline (cmdline,
316                                                 "cmdline-app-test",
317                                                 G_APP_INFO_CREATE_SUPPORTS_URIS,
318                                                 NULL);
319
320   error = NULL;
321   result = g_app_info_set_as_default_for_type (appinfo, "application/x-glib-test", &error);
322
323   g_assert (result);
324   g_assert_no_error (error);
325
326   appinfo2 = g_app_info_get_default_for_type ("application/x-glib-test", FALSE);
327
328   g_assert (appinfo2);
329   g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, g_app_info_get_commandline (appinfo2));
330
331   g_object_unref (appinfo2);
332
333   result = g_app_info_set_as_default_for_extension (appinfo, "gio-tests", &error);
334   g_assert (result);
335   g_assert_no_error (error);
336
337   appinfo2 = g_app_info_get_default_for_type ("application/x-extension-gio-tests", FALSE);
338
339   g_assert (appinfo2);
340   g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, g_app_info_get_commandline (appinfo2));
341
342   g_object_unref (appinfo2);
343
344   result = g_app_info_add_supports_type (appinfo, "application/x-gio-test", &error);
345   g_assert (result);
346   g_assert_no_error (error);
347
348   list = g_app_info_get_all_for_type ("application/x-gio-test");
349   g_assert_cmpint (g_list_length (list), ==, 1);
350   appinfo2 = list->data;
351   g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, g_app_info_get_commandline (appinfo2));
352   g_object_unref (appinfo2);
353   g_list_free (list);
354
355   g_assert (g_app_info_can_remove_supports_type (appinfo));
356   g_assert (g_app_info_remove_supports_type (appinfo, "application/x-gio-test", &error));
357   g_assert_no_error (error);
358
359   g_assert (g_app_info_can_delete (appinfo));
360   g_assert (g_app_info_delete (appinfo));
361   g_object_unref (appinfo);
362 }
363
364 static void
365 test_environment (void)
366 {
367   GAppLaunchContext *ctx;
368   gchar **env;
369   const gchar *path;
370
371   g_unsetenv ("FOO");
372   g_unsetenv ("BLA");
373   path = g_getenv ("PATH");
374
375   ctx = g_app_launch_context_new ();
376
377   env = g_app_launch_context_get_environment (ctx);
378
379   g_assert (g_environ_getenv (env, "FOO") == NULL);
380   g_assert (g_environ_getenv (env, "BLA") == NULL);
381   g_assert_cmpstr (g_environ_getenv (env, "PATH"), ==, path);
382
383   g_strfreev (env);
384
385   g_app_launch_context_setenv (ctx, "FOO", "bar");
386   g_app_launch_context_setenv (ctx, "BLA", "bla");
387
388   env = g_app_launch_context_get_environment (ctx);
389
390   g_assert_cmpstr (g_environ_getenv (env, "FOO"), ==, "bar");
391   g_assert_cmpstr (g_environ_getenv (env, "BLA"), ==, "bla");
392   g_assert_cmpstr (g_environ_getenv (env, "PATH"), ==, path);
393
394   g_strfreev (env);
395
396   g_app_launch_context_setenv (ctx, "FOO", "baz");
397   g_app_launch_context_unsetenv (ctx, "BLA");
398
399   env = g_app_launch_context_get_environment (ctx);
400
401   g_assert_cmpstr (g_environ_getenv (env, "FOO"), ==, "baz");
402   g_assert (g_environ_getenv (env, "BLA") == NULL);
403
404   g_strfreev (env);
405
406   g_object_unref (ctx);
407 }
408
409 static void
410 test_startup_wm_class (void)
411 {
412   GDesktopAppInfo *appinfo;
413   const char *wm_class;
414   gchar *path;
415
416   path = g_build_filename (datapath, "appinfo-test.desktop", NULL);
417   appinfo = g_desktop_app_info_new_from_filename (path);
418   wm_class = g_desktop_app_info_get_startup_wm_class (appinfo);
419
420   g_assert_cmpstr (wm_class, ==, "appinfo-class");
421
422   g_object_unref (appinfo);
423   g_free (path);
424 }
425
426 static void
427 test_supported_types (void)
428 {
429   GAppInfo *appinfo;
430   const char * const *content_types;
431   gchar *path;
432
433   path = g_build_filename (datapath, "appinfo-test.desktop", NULL);
434   appinfo = G_APP_INFO (g_desktop_app_info_new_from_filename (path));
435   content_types = g_app_info_get_supported_types (appinfo);
436
437   g_assert_cmpint (g_strv_length ((char**)content_types), ==, 2);
438   g_assert_cmpstr (content_types[0], ==, "image/png");
439
440   g_object_unref (appinfo);
441   g_free (path);
442 }
443
444 static void
445 test_from_keyfile (void)
446 {
447   GDesktopAppInfo *info;
448   GKeyFile *kf;
449   GError *error = NULL;
450   const gchar *categories;
451   gchar **keywords;
452   const gchar *file;
453   const gchar *name;
454   gchar *path;
455
456   path = g_build_filename (datapath, "appinfo-test.desktop", NULL);
457   kf = g_key_file_new ();
458   g_key_file_load_from_file (kf, path, G_KEY_FILE_NONE, &error);
459   g_assert_no_error (error);
460   info = g_desktop_app_info_new_from_keyfile (kf);
461   g_key_file_free (kf);
462   g_assert (info != NULL);
463   g_free (path);
464
465   g_object_get (info, "filename", &file, NULL);
466   g_assert (file == NULL);
467
468   file = g_desktop_app_info_get_filename (info);
469   g_assert (file == NULL);
470   categories = g_desktop_app_info_get_categories (info);
471   g_assert_cmpstr (categories, ==, "GNOME;GTK;");
472   keywords = (gchar **)g_desktop_app_info_get_keywords (info);
473   g_assert_cmpint (g_strv_length (keywords), ==, 2);
474   g_assert_cmpstr (keywords[0], ==, "keyword1");
475   g_assert_cmpstr (keywords[1], ==, "test keyword");
476   name = g_desktop_app_info_get_generic_name (info);
477   g_assert_cmpstr (name, ==, "generic-appinfo-test");
478   g_assert (!g_desktop_app_info_get_nodisplay (info));
479
480   g_object_unref (info);
481 }
482
483 int
484 main (int argc, char *argv[])
485 {
486   if (g_getenv ("G_TEST_DATA"))
487     datapath = g_getenv ("G_TEST_DATA");
488   else
489     datapath = SRCDIR;
490
491   g_test_init (&argc, &argv, NULL);
492
493   g_test_add_func ("/appinfo/basic", test_basic);
494   g_test_add_func ("/appinfo/text", test_text);
495   g_test_add_func ("/appinfo/launch", test_launch);
496   g_test_add_func ("/appinfo/show-in", test_show_in);
497   g_test_add_func ("/appinfo/commandline", test_commandline);
498   g_test_add_func ("/appinfo/launch-context", test_launch_context);
499   g_test_add_func ("/appinfo/launch-context-signals", test_launch_context_signals);
500   g_test_add_func ("/appinfo/tryexec", test_tryexec);
501   g_test_add_func ("/appinfo/associations", test_associations);
502   g_test_add_func ("/appinfo/environment", test_environment);
503   g_test_add_func ("/appinfo/startup-wm-class", test_startup_wm_class);
504   g_test_add_func ("/appinfo/supported-types", test_supported_types);
505   g_test_add_func ("/appinfo/from-keyfile", test_from_keyfile);
506
507   return g_test_run ();
508 }
509