Revert "GDesktopAppInfo: add an accessor for StartupWMClass"
[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
13   appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (SRCDIR "/appinfo-test.desktop");
14   g_assert (appinfo != NULL);
15
16   error = NULL;
17   g_assert (g_app_info_launch (appinfo, NULL, NULL, &error));
18   g_assert_no_error (error);
19
20   g_assert (g_app_info_launch_uris (appinfo, NULL, NULL, &error));
21   g_assert_no_error (error);
22 }
23
24 static void
25 test_locale (const char *locale)
26 {
27   GAppInfo *appinfo;
28   const gchar *orig;
29
30   orig = setlocale (LC_ALL, NULL);
31   g_setenv ("LANGUAGE", locale, TRUE);
32   setlocale (LC_ALL, "");
33
34   appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (SRCDIR "/appinfo-test.desktop");
35
36   if (g_strcmp0 (locale, "C") == 0)
37     {
38       g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "appinfo-test");
39       g_assert_cmpstr (g_app_info_get_description (appinfo), ==, "GAppInfo example");
40       g_assert_cmpstr (g_app_info_get_display_name (appinfo), ==, "example");
41     }
42   else if (g_str_has_prefix (locale, "en"))
43     {
44       g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "appinfo-test");
45       g_assert_cmpstr (g_app_info_get_description (appinfo), ==, "GAppInfo example");
46       g_assert_cmpstr (g_app_info_get_display_name (appinfo), ==, "example");
47     }
48   else if (g_str_has_prefix (locale, "de"))
49     {
50       g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "appinfo-test-de");
51       g_assert_cmpstr (g_app_info_get_description (appinfo), ==, "GAppInfo Beispiel");
52       g_assert_cmpstr (g_app_info_get_display_name (appinfo), ==, "Beispiel");
53     }
54
55   g_object_unref (appinfo);
56
57   g_setenv ("LANGUAGE", orig, TRUE);
58   setlocale (LC_ALL, "");
59 }
60
61 static void
62 test_text (void)
63 {
64   test_locale ("C");
65   test_locale ("en_US");
66   test_locale ("de");
67   test_locale ("de_DE.UTF-8");
68 }
69
70 static void
71 test_basic (void)
72 {
73   GAppInfo *appinfo;
74   GAppInfo *appinfo2;
75   GIcon *icon, *icon2;
76
77   appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (SRCDIR "/appinfo-test.desktop");
78
79   g_assert (g_app_info_get_id (appinfo) == NULL);
80   g_assert_cmpstr (g_app_info_get_executable (appinfo), ==, "./appinfo-test");
81   g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, "./appinfo-test --option");
82
83   icon = g_app_info_get_icon (appinfo);
84   g_assert (G_IS_THEMED_ICON (icon));
85   icon2 = g_themed_icon_new ("testicon");
86   g_assert (g_icon_equal (icon, icon2));
87   g_object_unref (icon2);
88
89   appinfo2 = g_app_info_dup (appinfo);
90   g_assert (g_app_info_get_id (appinfo) == g_app_info_get_id (appinfo2));
91   g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, g_app_info_get_commandline (appinfo2));
92
93   g_object_unref (appinfo);
94   g_object_unref (appinfo2);
95 }
96
97 static void
98 test_show_in (void)
99 {
100   GAppInfo *appinfo;
101
102   g_desktop_app_info_set_desktop_env ("GNOME");
103
104   appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (SRCDIR "/appinfo-test.desktop");
105   g_assert (g_app_info_should_show (appinfo));
106   g_object_unref (appinfo);
107
108   appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (SRCDIR "/appinfo-test-gnome.desktop");
109   g_assert (g_app_info_should_show (appinfo));
110   g_object_unref (appinfo);
111
112   appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (SRCDIR "/appinfo-test-notgnome.desktop");
113   g_assert (!g_app_info_should_show (appinfo));
114   g_object_unref (appinfo);
115 }
116
117 static void
118 test_commandline (void)
119 {
120   GAppInfo *appinfo;
121   GError *error;
122
123   error = NULL;
124   appinfo = g_app_info_create_from_commandline ("./appinfo-test --option",
125                                                 "cmdline-app-test",
126                                                 G_APP_INFO_CREATE_SUPPORTS_URIS,
127                                                 &error);
128   g_assert (appinfo != NULL);
129   g_assert_no_error (error);
130   g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "cmdline-app-test");
131   g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, "./appinfo-test --option %u");
132   g_assert (g_app_info_supports_uris (appinfo));
133   g_assert (!g_app_info_supports_files (appinfo));
134
135   g_object_unref (appinfo);
136
137   error = NULL;
138   appinfo = g_app_info_create_from_commandline ("./appinfo-test --option",
139                                                 "cmdline-app-test",
140                                                 G_APP_INFO_CREATE_NONE,
141                                                 &error);
142   g_assert (appinfo != NULL);
143   g_assert_no_error (error);
144   g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "cmdline-app-test");
145   g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, "./appinfo-test --option %f");
146   g_assert (!g_app_info_supports_uris (appinfo));
147   g_assert (g_app_info_supports_files (appinfo));
148
149   g_object_unref (appinfo);
150 }
151
152 static void
153 test_launch_context (void)
154 {
155   GAppLaunchContext *context;
156   GAppInfo *appinfo;
157   gchar *str;
158
159   context = g_app_launch_context_new ();
160   appinfo = g_app_info_create_from_commandline ("./appinfo-test --option",
161                                                 "cmdline-app-test",
162                                                 G_APP_INFO_CREATE_SUPPORTS_URIS,
163                                                 NULL);
164
165   str = g_app_launch_context_get_display (context, appinfo, NULL);
166   g_assert (str == NULL);
167
168   str = g_app_launch_context_get_startup_notify_id (context, appinfo, NULL);
169   g_assert (str == NULL);
170
171   g_object_unref (appinfo);
172   g_object_unref (context);
173 }
174
175 static void
176 test_tryexec (void)
177 {
178   GAppInfo *appinfo;
179
180   appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (SRCDIR "/appinfo-test2.desktop");
181
182   g_assert (appinfo == NULL);
183 }
184
185 /* Test that we can set an appinfo as default for a mime type or
186  * file extension, and also add and remove handled mime types.
187  */
188 static void
189 test_associations (void)
190 {
191   GAppInfo *appinfo;
192   GAppInfo *appinfo2;
193   GError *error;
194   gboolean result;
195   GList *list;
196
197   appinfo = g_app_info_create_from_commandline ("./appinfo-test --option",
198                                                 "cmdline-app-test",
199                                                 G_APP_INFO_CREATE_SUPPORTS_URIS,
200                                                 NULL);
201
202   error = NULL;
203   result = g_app_info_set_as_default_for_type (appinfo, "application/x-glib-test", &error);
204
205   g_assert (result);
206   g_assert_no_error (error);
207
208   appinfo2 = g_app_info_get_default_for_type ("application/x-glib-test", FALSE);
209
210   g_assert (appinfo2);
211   g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, g_app_info_get_commandline (appinfo2));
212
213   g_object_unref (appinfo2);
214
215   result = g_app_info_set_as_default_for_extension (appinfo, "gio-tests", &error);
216   g_assert (result);
217   g_assert_no_error (error);
218
219   appinfo2 = g_app_info_get_default_for_type ("application/x-extension-gio-tests", FALSE);
220
221   g_assert (appinfo2);
222   g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, g_app_info_get_commandline (appinfo2));
223
224   g_object_unref (appinfo2);
225
226   result = g_app_info_add_supports_type (appinfo, "application/x-gio-test", &error);
227   g_assert (result);
228   g_assert_no_error (error);
229
230   list = g_app_info_get_all_for_type ("application/x-gio-test");
231   g_assert_cmpint (g_list_length (list), ==, 1);
232   appinfo2 = list->data;
233   g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, g_app_info_get_commandline (appinfo2));
234   g_object_unref (appinfo2);
235   g_list_free (list);
236
237   g_assert (g_app_info_can_remove_supports_type (appinfo));
238   g_assert (g_app_info_remove_supports_type (appinfo, "application/x-gio-test", &error));
239   g_assert_no_error (error);
240
241   g_assert (g_app_info_can_delete (appinfo));
242   g_assert (g_app_info_delete (appinfo));
243   g_object_unref (appinfo);
244 }
245
246 static void
247 test_environment (void)
248 {
249   GAppLaunchContext *ctx;
250   gchar **env;
251
252   ctx = g_app_launch_context_new ();
253   g_app_launch_context_setenv (ctx, "FOO", "bar");
254   g_app_launch_context_setenv (ctx, "BLA", "bla");
255
256   env = g_app_launch_context_get_environment (ctx);
257
258   g_assert_cmpstr (g_environ_getenv (env, "FOO"), ==, "bar");
259   g_assert_cmpstr (g_environ_getenv (env, "BLA"), ==, "bla");
260
261   g_strfreev (env);
262
263   g_app_launch_context_setenv (ctx, "FOO", "baz");
264   g_app_launch_context_unsetenv (ctx, "BLA");
265
266   env = g_app_launch_context_get_environment (ctx);
267
268   g_assert_cmpstr (g_environ_getenv (env, "FOO"), ==, "baz");
269   g_assert (g_environ_getenv (env, "BLA") == NULL);
270
271   g_strfreev (env);
272
273   g_object_unref (ctx);
274 }
275
276 int
277 main (int argc, char *argv[])
278 {
279   g_type_init ();
280   g_test_init (&argc, &argv, NULL);
281
282   g_test_add_func ("/appinfo/basic", test_basic);
283   g_test_add_func ("/appinfo/text", test_text);
284   g_test_add_func ("/appinfo/launch", test_launch);
285   g_test_add_func ("/appinfo/show-in", test_show_in);
286   g_test_add_func ("/appinfo/commandline", test_commandline);
287   g_test_add_func ("/appinfo/launch-context", test_launch_context);
288   g_test_add_func ("/appinfo/tryexec", test_tryexec);
289   g_test_add_func ("/appinfo/associations", test_associations);
290   g_test_add_func ("/appinfo/environment", test_environment);
291
292   return g_test_run ();
293 }
294