Improve GAppInfo test coverage
[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 void
8 test_launch (void)
9 {
10   GAppInfo *appinfo;
11   GError *error;
12   GFile *file;
13   GList *l;
14
15   appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (SRCDIR "/appinfo-test.desktop");
16   g_assert (appinfo != NULL);
17
18   error = NULL;
19   g_assert (g_app_info_launch (appinfo, NULL, NULL, &error));
20   g_assert_no_error (error);
21
22   g_assert (g_app_info_launch_uris (appinfo, NULL, NULL, &error));
23   g_assert_no_error (error);
24
25   file = g_file_new_for_path (SRCDIR "/appinfo-test.desktop");
26   l = NULL;
27   l = g_list_append (l, file);
28
29   g_assert (g_app_info_launch (appinfo, l, NULL, &error));
30   g_assert_no_error (error);
31   g_list_free (l);
32   g_object_unref (file);
33
34   l = NULL;
35   l = g_list_append (l, "file://" SRCDIR "/appinfo-test.desktop");
36   l = g_list_append (l, "file:///etc/group#adm");
37
38   g_assert (g_app_info_launch_uris (appinfo, l, NULL, &error));
39   g_assert_no_error (error);
40   g_list_free (l);
41 }
42
43 static void
44 test_locale (const char *locale)
45 {
46   GAppInfo *appinfo;
47   const gchar *orig;
48
49   orig = setlocale (LC_ALL, NULL);
50   g_setenv ("LANGUAGE", locale, TRUE);
51   setlocale (LC_ALL, "");
52
53   appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (SRCDIR "/appinfo-test.desktop");
54
55   if (g_strcmp0 (locale, "C") == 0)
56     {
57       g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "appinfo-test");
58       g_assert_cmpstr (g_app_info_get_description (appinfo), ==, "GAppInfo example");
59       g_assert_cmpstr (g_app_info_get_display_name (appinfo), ==, "example");
60     }
61   else if (g_str_has_prefix (locale, "en"))
62     {
63       g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "appinfo-test");
64       g_assert_cmpstr (g_app_info_get_description (appinfo), ==, "GAppInfo example");
65       g_assert_cmpstr (g_app_info_get_display_name (appinfo), ==, "example");
66     }
67   else if (g_str_has_prefix (locale, "de"))
68     {
69       g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "appinfo-test-de");
70       g_assert_cmpstr (g_app_info_get_description (appinfo), ==, "GAppInfo Beispiel");
71       g_assert_cmpstr (g_app_info_get_display_name (appinfo), ==, "Beispiel");
72     }
73
74   g_object_unref (appinfo);
75
76   g_setenv ("LANGUAGE", orig, TRUE);
77   setlocale (LC_ALL, "");
78 }
79
80 static void
81 test_text (void)
82 {
83   test_locale ("C");
84   test_locale ("en_US");
85   test_locale ("de");
86   test_locale ("de_DE.UTF-8");
87 }
88
89 static void
90 test_basic (void)
91 {
92   GAppInfo *appinfo;
93   GAppInfo *appinfo2;
94   GIcon *icon, *icon2;
95
96   appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (SRCDIR "/appinfo-test.desktop");
97
98   g_assert (g_app_info_get_id (appinfo) == NULL);
99   g_assert_cmpstr (g_app_info_get_executable (appinfo), ==, "./appinfo-test");
100
101   icon = g_app_info_get_icon (appinfo);
102   g_assert (G_IS_THEMED_ICON (icon));
103   icon2 = g_themed_icon_new ("testicon");
104   g_assert (g_icon_equal (icon, icon2));
105   g_object_unref (icon2);
106
107   appinfo2 = g_app_info_dup (appinfo);
108   g_assert (g_app_info_get_id (appinfo) == g_app_info_get_id (appinfo2));
109   g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, g_app_info_get_commandline (appinfo2));
110
111   g_object_unref (appinfo);
112   g_object_unref (appinfo2);
113 }
114
115 static void
116 test_show_in (void)
117 {
118   GAppInfo *appinfo;
119
120   g_desktop_app_info_set_desktop_env ("GNOME");
121
122   appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (SRCDIR "/appinfo-test.desktop");
123   g_assert (g_app_info_should_show (appinfo));
124   g_object_unref (appinfo);
125
126   appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (SRCDIR "/appinfo-test-gnome.desktop");
127   g_assert (g_app_info_should_show (appinfo));
128   g_object_unref (appinfo);
129
130   appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (SRCDIR "/appinfo-test-notgnome.desktop");
131   g_assert (!g_app_info_should_show (appinfo));
132   g_object_unref (appinfo);
133 }
134
135 static void
136 test_commandline (void)
137 {
138   GAppInfo *appinfo;
139   GError *error;
140
141   error = NULL;
142   appinfo = g_app_info_create_from_commandline ("./appinfo-test --option",
143                                                 "cmdline-app-test",
144                                                 G_APP_INFO_CREATE_SUPPORTS_URIS,
145                                                 &error);
146   g_assert (appinfo != NULL);
147   g_assert_no_error (error);
148   g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "cmdline-app-test");
149   g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, "./appinfo-test --option %u");
150   g_assert (g_app_info_supports_uris (appinfo));
151   g_assert (!g_app_info_supports_files (appinfo));
152
153   g_object_unref (appinfo);
154
155   error = NULL;
156   appinfo = g_app_info_create_from_commandline ("./appinfo-test --option",
157                                                 "cmdline-app-test",
158                                                 G_APP_INFO_CREATE_NONE,
159                                                 &error);
160   g_assert (appinfo != NULL);
161   g_assert_no_error (error);
162   g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "cmdline-app-test");
163   g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, "./appinfo-test --option %f");
164   g_assert (!g_app_info_supports_uris (appinfo));
165   g_assert (g_app_info_supports_files (appinfo));
166
167   g_object_unref (appinfo);
168 }
169
170 static void
171 test_launch_context (void)
172 {
173   GAppLaunchContext *context;
174   GAppInfo *appinfo;
175   gchar *str;
176
177   context = g_app_launch_context_new ();
178   appinfo = g_app_info_create_from_commandline ("./appinfo-test --option",
179                                                 "cmdline-app-test",
180                                                 G_APP_INFO_CREATE_SUPPORTS_URIS,
181                                                 NULL);
182
183   str = g_app_launch_context_get_display (context, appinfo, NULL);
184   g_assert (str == NULL);
185
186   str = g_app_launch_context_get_startup_notify_id (context, appinfo, NULL);
187   g_assert (str == NULL);
188
189   g_object_unref (appinfo);
190   g_object_unref (context);
191 }
192
193 static void
194 test_tryexec (void)
195 {
196   GAppInfo *appinfo;
197
198   appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (SRCDIR "/appinfo-test2.desktop");
199
200   g_assert (appinfo == NULL);
201 }
202
203 /* Test that we can set an appinfo as default for a mime type or
204  * file extension, and also add and remove handled mime types.
205  */
206 static void
207 test_associations (void)
208 {
209   GAppInfo *appinfo;
210   GAppInfo *appinfo2;
211   GError *error;
212   gboolean result;
213   GList *list;
214
215   appinfo = g_app_info_create_from_commandline ("./appinfo-test --option",
216                                                 "cmdline-app-test",
217                                                 G_APP_INFO_CREATE_SUPPORTS_URIS,
218                                                 NULL);
219
220   error = NULL;
221   result = g_app_info_set_as_default_for_type (appinfo, "application/x-glib-test", &error);
222
223   g_assert (result);
224   g_assert_no_error (error);
225
226   appinfo2 = g_app_info_get_default_for_type ("application/x-glib-test", FALSE);
227
228   g_assert (appinfo2);
229   g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, g_app_info_get_commandline (appinfo2));
230
231   g_object_unref (appinfo2);
232
233   result = g_app_info_set_as_default_for_extension (appinfo, "gio-tests", &error);
234   g_assert (result);
235   g_assert_no_error (error);
236
237   appinfo2 = g_app_info_get_default_for_type ("application/x-extension-gio-tests", FALSE);
238
239   g_assert (appinfo2);
240   g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, g_app_info_get_commandline (appinfo2));
241
242   g_object_unref (appinfo2);
243
244   result = g_app_info_add_supports_type (appinfo, "application/x-gio-test", &error);
245   g_assert (result);
246   g_assert_no_error (error);
247
248   list = g_app_info_get_all_for_type ("application/x-gio-test");
249   g_assert_cmpint (g_list_length (list), ==, 1);
250   appinfo2 = list->data;
251   g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, g_app_info_get_commandline (appinfo2));
252   g_object_unref (appinfo2);
253   g_list_free (list);
254
255   g_assert (g_app_info_can_remove_supports_type (appinfo));
256   g_assert (g_app_info_remove_supports_type (appinfo, "application/x-gio-test", &error));
257   g_assert_no_error (error);
258
259   g_assert (g_app_info_can_delete (appinfo));
260   g_assert (g_app_info_delete (appinfo));
261   g_object_unref (appinfo);
262 }
263
264 static void
265 test_environment (void)
266 {
267   GAppLaunchContext *ctx;
268   gchar **env;
269   const gchar *path;
270
271   g_unsetenv ("FOO");
272   g_unsetenv ("BLA");
273   path = g_getenv ("PATH");
274
275   ctx = g_app_launch_context_new ();
276
277   env = g_app_launch_context_get_environment (ctx);
278
279   g_assert (g_environ_getenv (env, "FOO") == NULL);
280   g_assert (g_environ_getenv (env, "BAR") == NULL);
281   g_assert_cmpstr (g_environ_getenv (env, "PATH"), ==, path);
282
283   g_strfreev (env);
284
285   g_app_launch_context_setenv (ctx, "FOO", "bar");
286   g_app_launch_context_setenv (ctx, "BLA", "bla");
287
288   env = g_app_launch_context_get_environment (ctx);
289
290   g_assert_cmpstr (g_environ_getenv (env, "FOO"), ==, "bar");
291   g_assert_cmpstr (g_environ_getenv (env, "BLA"), ==, "bla");
292   g_assert_cmpstr (g_environ_getenv (env, "PATH"), ==, path);
293
294   g_strfreev (env);
295
296   g_app_launch_context_setenv (ctx, "FOO", "baz");
297   g_app_launch_context_unsetenv (ctx, "BLA");
298
299   env = g_app_launch_context_get_environment (ctx);
300
301   g_assert_cmpstr (g_environ_getenv (env, "FOO"), ==, "baz");
302   g_assert (g_environ_getenv (env, "BLA") == NULL);
303
304   g_strfreev (env);
305
306   g_object_unref (ctx);
307 }
308
309 static void
310 test_startup_wm_class (void)
311 {
312   GDesktopAppInfo *appinfo;
313   const char *wm_class;
314
315   appinfo = g_desktop_app_info_new_from_filename (SRCDIR "/appinfo-test.desktop");
316   wm_class = g_desktop_app_info_get_startup_wm_class (appinfo);
317
318   g_assert_cmpstr (wm_class, ==, "appinfo-class");
319
320   g_object_unref (appinfo);
321 }
322
323 static void
324 test_supported_types (void)
325 {
326   GAppInfo *appinfo;
327   const char * const *content_types;
328
329   appinfo = G_APP_INFO (g_desktop_app_info_new_from_filename (SRCDIR "/appinfo-test.desktop"));
330   content_types = g_app_info_get_supported_types (appinfo);
331
332   g_assert_cmpint (g_strv_length ((char**)content_types), ==, 2);
333   g_assert_cmpstr (content_types[0], ==, "image/png");
334
335   g_object_unref (appinfo);
336 }
337
338 static void
339 test_from_keyfile (void)
340 {
341   GDesktopAppInfo *info;
342   GKeyFile *kf;
343   GError *error = NULL;
344   const gchar *categories;
345   gchar **keywords;
346   const gchar *file;
347   const gchar *name;
348
349   kf = g_key_file_new ();
350   g_key_file_load_from_file (kf,
351                              SRCDIR "/appinfo-test.desktop",
352                              G_KEY_FILE_NONE,
353                              &error);
354   g_assert_no_error (error);
355   info = g_desktop_app_info_new_from_keyfile (kf);
356   g_key_file_free (kf);
357   g_assert (info != NULL);
358
359   g_object_get (info, "filename", &file, NULL);
360   g_assert (file == NULL);
361
362   file = g_desktop_app_info_get_filename (info);
363   g_assert (file == NULL);
364   categories = g_desktop_app_info_get_categories (info);
365   g_assert_cmpstr (categories, ==, "GNOME;GTK;");
366   keywords = (gchar **)g_desktop_app_info_get_keywords (info);
367   g_assert_cmpint (g_strv_length (keywords), ==, 2);
368   g_assert_cmpstr (keywords[0], ==, "keyword1");
369   g_assert_cmpstr (keywords[1], ==, "test keyword");
370   name = g_desktop_app_info_get_generic_name (info);
371   g_assert_cmpstr (name, ==, "generic-appinfo-test");
372   g_assert (!g_desktop_app_info_get_nodisplay (info));
373
374   g_object_unref (info);
375 }
376
377 int
378 main (int argc, char *argv[])
379 {
380   g_type_init ();
381   g_test_init (&argc, &argv, NULL);
382
383   g_test_add_func ("/appinfo/basic", test_basic);
384   g_test_add_func ("/appinfo/text", test_text);
385   g_test_add_func ("/appinfo/launch", test_launch);
386   g_test_add_func ("/appinfo/show-in", test_show_in);
387   g_test_add_func ("/appinfo/commandline", test_commandline);
388   g_test_add_func ("/appinfo/launch-context", test_launch_context);
389   g_test_add_func ("/appinfo/tryexec", test_tryexec);
390   g_test_add_func ("/appinfo/associations", test_associations);
391   g_test_add_func ("/appinfo/environment", test_environment);
392   g_test_add_func ("/appinfo/startup-wm-class", test_startup_wm_class);
393   g_test_add_func ("/appinfo/supported-types", test_supported_types);
394   g_test_add_func ("/appinfo/from-keyfile", test_from_keyfile);
395
396   return g_test_run ();
397 }
398