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