Fix minor mem leak in test case
[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   path = g_test_get_filename (G_TEST_DIST, "appinfo-test.desktop", NULL);
140   appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (path);
141   g_assert (g_app_info_should_show (appinfo));
142   g_object_unref (appinfo);
143
144   path = g_test_get_filename (G_TEST_DIST, "appinfo-test-gnome.desktop", NULL);
145   appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (path);
146   g_assert (g_app_info_should_show (appinfo));
147   g_object_unref (appinfo);
148
149   path = g_test_get_filename (G_TEST_DIST, "appinfo-test-notgnome.desktop", NULL);
150   appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (path);
151   g_assert (!g_app_info_should_show (appinfo));
152   g_object_unref (appinfo);
153 }
154
155 static void
156 test_commandline (void)
157 {
158   GAppInfo *appinfo;
159   GError *error;
160   gchar *cmdline;
161   gchar *cmdline_out;
162
163   cmdline = g_strconcat (g_test_get_dir (G_TEST_BUILT), "/appinfo-test --option", NULL);
164   cmdline_out = g_strconcat (cmdline, " %u", NULL);
165
166   error = NULL;
167   appinfo = g_app_info_create_from_commandline (cmdline,
168                                                 "cmdline-app-test",
169                                                 G_APP_INFO_CREATE_SUPPORTS_URIS,
170                                                 &error);
171   g_assert (appinfo != NULL);
172   g_assert_no_error (error);
173   g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "cmdline-app-test");
174   g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, cmdline_out);
175   g_assert (g_app_info_supports_uris (appinfo));
176   g_assert (!g_app_info_supports_files (appinfo));
177
178   g_object_unref (appinfo);
179
180   g_free (cmdline_out);
181   cmdline_out = g_strconcat (cmdline, " %f", NULL);
182
183   error = NULL;
184   appinfo = g_app_info_create_from_commandline (cmdline,
185                                                 "cmdline-app-test",
186                                                 G_APP_INFO_CREATE_NONE,
187                                                 &error);
188   g_assert (appinfo != NULL);
189   g_assert_no_error (error);
190   g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "cmdline-app-test");
191   g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, cmdline_out);
192   g_assert (!g_app_info_supports_uris (appinfo));
193   g_assert (g_app_info_supports_files (appinfo));
194
195   g_object_unref (appinfo);
196
197   g_free (cmdline);
198   g_free (cmdline_out);
199 }
200
201 static void
202 test_launch_context (void)
203 {
204   GAppLaunchContext *context;
205   GAppInfo *appinfo;
206   gchar *str;
207   gchar *cmdline;
208
209   cmdline = g_strconcat (g_test_get_dir (G_TEST_BUILT), "/appinfo-test --option", NULL);
210
211   context = g_app_launch_context_new ();
212   appinfo = g_app_info_create_from_commandline (cmdline,
213                                                 "cmdline-app-test",
214                                                 G_APP_INFO_CREATE_SUPPORTS_URIS,
215                                                 NULL);
216
217   str = g_app_launch_context_get_display (context, appinfo, NULL);
218   g_assert (str == NULL);
219
220   str = g_app_launch_context_get_startup_notify_id (context, appinfo, NULL);
221   g_assert (str == NULL);
222
223   g_object_unref (appinfo);
224   g_object_unref (context);
225
226   g_free (cmdline);
227 }
228
229 static gboolean launched_reached;
230
231 static void
232 launched (GAppLaunchContext *context,
233           GAppInfo          *info,
234           GVariant          *platform_data,
235           gpointer           user_data)
236 {
237   gint pid;
238
239   pid = 0;
240   g_assert (g_variant_lookup (platform_data, "pid", "i", &pid));
241   g_assert (pid != 0);
242
243   launched_reached = TRUE;
244 }
245
246 static void
247 launch_failed (GAppLaunchContext *context,
248                const gchar       *startup_notify_id)
249 {
250   g_assert_not_reached ();
251 }
252
253 static void
254 test_launch_context_signals (void)
255 {
256   GAppLaunchContext *context;
257   GAppInfo *appinfo;
258   GError *error = NULL;
259   gchar *cmdline;
260
261   cmdline = g_strconcat (g_test_get_dir (G_TEST_BUILT), "/appinfo-test --option", NULL);
262
263   context = g_app_launch_context_new ();
264   g_signal_connect (context, "launched", G_CALLBACK (launched), NULL);
265   g_signal_connect (context, "launch_failed", G_CALLBACK (launch_failed), NULL);
266   appinfo = g_app_info_create_from_commandline (cmdline,
267                                                 "cmdline-app-test",
268                                                 G_APP_INFO_CREATE_SUPPORTS_URIS,
269                                                 NULL);
270
271   error = NULL;
272   g_assert (g_app_info_launch (appinfo, NULL, context, &error));
273   g_assert_no_error (error);
274
275   g_assert (launched_reached);
276
277   g_object_unref (appinfo);
278   g_object_unref (context);
279
280   g_free (cmdline);
281 }
282
283 static void
284 test_tryexec (void)
285 {
286   GAppInfo *appinfo;
287   const gchar *path;
288
289   path = g_test_get_filename (G_TEST_DIST, "appinfo-test2.desktop", NULL);
290   appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (path);
291
292   g_assert (appinfo == NULL);
293 }
294
295 /* Test that we can set an appinfo as default for a mime type or
296  * file extension, and also add and remove handled mime types.
297  */
298 static void
299 test_associations (void)
300 {
301   GAppInfo *appinfo;
302   GAppInfo *appinfo2;
303   GError *error;
304   gboolean result;
305   GList *list;
306   gchar *cmdline;
307
308   cmdline = g_strconcat (g_test_get_dir (G_TEST_BUILT), "/appinfo-test --option", NULL);
309   appinfo = g_app_info_create_from_commandline (cmdline,
310                                                 "cmdline-app-test",
311                                                 G_APP_INFO_CREATE_SUPPORTS_URIS,
312                                                 NULL);
313
314   error = NULL;
315   result = g_app_info_set_as_default_for_type (appinfo, "application/x-glib-test", &error);
316
317   g_assert (result);
318   g_assert_no_error (error);
319
320   appinfo2 = g_app_info_get_default_for_type ("application/x-glib-test", FALSE);
321
322   g_assert (appinfo2);
323   g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, g_app_info_get_commandline (appinfo2));
324
325   g_object_unref (appinfo2);
326
327   result = g_app_info_set_as_default_for_extension (appinfo, "gio-tests", &error);
328   g_assert (result);
329   g_assert_no_error (error);
330
331   appinfo2 = g_app_info_get_default_for_type ("application/x-extension-gio-tests", FALSE);
332
333   g_assert (appinfo2);
334   g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, g_app_info_get_commandline (appinfo2));
335
336   g_object_unref (appinfo2);
337
338   result = g_app_info_add_supports_type (appinfo, "application/x-gio-test", &error);
339   g_assert (result);
340   g_assert_no_error (error);
341
342   list = g_app_info_get_all_for_type ("application/x-gio-test");
343   g_assert_cmpint (g_list_length (list), ==, 1);
344   appinfo2 = list->data;
345   g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, g_app_info_get_commandline (appinfo2));
346   g_object_unref (appinfo2);
347   g_list_free (list);
348
349   g_assert (g_app_info_can_remove_supports_type (appinfo));
350   g_assert (g_app_info_remove_supports_type (appinfo, "application/x-gio-test", &error));
351   g_assert_no_error (error);
352
353   g_assert (g_app_info_can_delete (appinfo));
354   g_assert (g_app_info_delete (appinfo));
355   g_object_unref (appinfo);
356 }
357
358 static void
359 test_environment (void)
360 {
361   GAppLaunchContext *ctx;
362   gchar **env;
363   const gchar *path;
364
365   g_unsetenv ("FOO");
366   g_unsetenv ("BLA");
367   path = g_getenv ("PATH");
368
369   ctx = g_app_launch_context_new ();
370
371   env = g_app_launch_context_get_environment (ctx);
372
373   g_assert (g_environ_getenv (env, "FOO") == NULL);
374   g_assert (g_environ_getenv (env, "BLA") == NULL);
375   g_assert_cmpstr (g_environ_getenv (env, "PATH"), ==, path);
376
377   g_strfreev (env);
378
379   g_app_launch_context_setenv (ctx, "FOO", "bar");
380   g_app_launch_context_setenv (ctx, "BLA", "bla");
381
382   env = g_app_launch_context_get_environment (ctx);
383
384   g_assert_cmpstr (g_environ_getenv (env, "FOO"), ==, "bar");
385   g_assert_cmpstr (g_environ_getenv (env, "BLA"), ==, "bla");
386   g_assert_cmpstr (g_environ_getenv (env, "PATH"), ==, path);
387
388   g_strfreev (env);
389
390   g_app_launch_context_setenv (ctx, "FOO", "baz");
391   g_app_launch_context_unsetenv (ctx, "BLA");
392
393   env = g_app_launch_context_get_environment (ctx);
394
395   g_assert_cmpstr (g_environ_getenv (env, "FOO"), ==, "baz");
396   g_assert (g_environ_getenv (env, "BLA") == NULL);
397
398   g_strfreev (env);
399
400   g_object_unref (ctx);
401 }
402
403 static void
404 test_startup_wm_class (void)
405 {
406   GDesktopAppInfo *appinfo;
407   const char *wm_class;
408   const gchar *path;
409
410   path = g_test_get_filename (G_TEST_DIST, "appinfo-test.desktop", NULL);
411   appinfo = g_desktop_app_info_new_from_filename (path);
412   wm_class = g_desktop_app_info_get_startup_wm_class (appinfo);
413
414   g_assert_cmpstr (wm_class, ==, "appinfo-class");
415
416   g_object_unref (appinfo);
417 }
418
419 static void
420 test_supported_types (void)
421 {
422   GAppInfo *appinfo;
423   const char * const *content_types;
424   const gchar *path;
425
426   path = g_test_get_filename (G_TEST_DIST, "appinfo-test.desktop", NULL);
427   appinfo = G_APP_INFO (g_desktop_app_info_new_from_filename (path));
428   content_types = g_app_info_get_supported_types (appinfo);
429
430   g_assert_cmpint (g_strv_length ((char**)content_types), ==, 2);
431   g_assert_cmpstr (content_types[0], ==, "image/png");
432
433   g_object_unref (appinfo);
434 }
435
436 static void
437 test_from_keyfile (void)
438 {
439   GDesktopAppInfo *info;
440   GKeyFile *kf;
441   GError *error = NULL;
442   const gchar *categories;
443   gchar **keywords;
444   const gchar *file;
445   const gchar *name;
446   const gchar *path;
447
448   path = g_test_get_filename (G_TEST_DIST, "appinfo-test.desktop", NULL);
449   kf = g_key_file_new ();
450   g_key_file_load_from_file (kf, path, G_KEY_FILE_NONE, &error);
451   g_assert_no_error (error);
452   info = g_desktop_app_info_new_from_keyfile (kf);
453   g_key_file_free (kf);
454   g_assert (info != NULL);
455
456   g_object_get (info, "filename", &file, NULL);
457   g_assert (file == NULL);
458
459   file = g_desktop_app_info_get_filename (info);
460   g_assert (file == NULL);
461   categories = g_desktop_app_info_get_categories (info);
462   g_assert_cmpstr (categories, ==, "GNOME;GTK;");
463   keywords = (gchar **)g_desktop_app_info_get_keywords (info);
464   g_assert_cmpint (g_strv_length (keywords), ==, 2);
465   g_assert_cmpstr (keywords[0], ==, "keyword1");
466   g_assert_cmpstr (keywords[1], ==, "test keyword");
467   name = g_desktop_app_info_get_generic_name (info);
468   g_assert_cmpstr (name, ==, "generic-appinfo-test");
469   g_assert (!g_desktop_app_info_get_nodisplay (info));
470
471   g_object_unref (info);
472 }
473
474 int
475 main (int argc, char *argv[])
476 {
477   g_setenv ("XDG_CURRENT_DESKTOP", "GNOME", TRUE);
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