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