Add some more cases to the app-id unit tests
[glib.git] / gio / tests / appinfo.c
blobb1a60f0b8f32cd374d3242f75a57f661d12bd90b
2 #include <locale.h>
3 #include <string.h>
5 #include <gio/gio.h>
6 #include <gio/gdesktopappinfo.h>
8 static void
9 test_launch (void)
11 GAppInfo *appinfo;
12 GError *error;
13 GFile *file;
14 GList *l;
15 const gchar *path;
16 gchar *uri;
18 if (g_getenv ("DISPLAY") == NULL || g_getenv ("DISPLAY")[0] == '\0')
20 g_printerr ("No DISPLAY. Skipping test. ");
21 return;
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);
28 error = NULL;
29 g_assert (g_app_info_launch (appinfo, NULL, NULL, &error));
30 g_assert_no_error (error);
32 g_assert (g_app_info_launch_uris (appinfo, NULL, NULL, &error));
33 g_assert_no_error (error);
35 file = g_file_new_for_path (path);
36 l = NULL;
37 l = g_list_append (l, file);
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);
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");
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);
54 g_object_unref (appinfo);
57 static void
58 test_locale (const char *locale)
60 GAppInfo *appinfo;
61 const gchar *orig;
62 const gchar *path;
64 orig = setlocale (LC_ALL, NULL);
65 g_setenv ("LANGUAGE", locale, TRUE);
66 setlocale (LC_ALL, "");
68 path = g_test_get_filename (G_TEST_DIST, "appinfo-test.desktop", NULL);
69 appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (path);
71 if (g_strcmp0 (locale, "C") == 0)
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");
77 else if (g_str_has_prefix (locale, "en"))
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");
83 else if (g_str_has_prefix (locale, "de"))
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");
90 g_object_unref (appinfo);
92 g_setenv ("LANGUAGE", orig, TRUE);
93 setlocale (LC_ALL, "");
96 static void
97 test_text (void)
99 test_locale ("C");
100 test_locale ("en_US");
101 test_locale ("de");
102 test_locale ("de_DE.UTF-8");
105 static void
106 test_basic (void)
108 GAppInfo *appinfo;
109 GAppInfo *appinfo2;
110 GIcon *icon, *icon2;
111 const gchar *path;
113 path = g_test_get_filename (G_TEST_DIST, "appinfo-test.desktop", NULL);
114 appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (path);
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);
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);
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));
129 g_object_unref (appinfo);
130 g_object_unref (appinfo2);
133 static void
134 test_show_in (void)
136 GAppInfo *appinfo;
137 const gchar *path;
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);
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);
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);
155 static void
156 test_commandline (void)
158 GAppInfo *appinfo;
159 GError *error;
160 gchar *cmdline;
161 gchar *cmdline_out;
163 cmdline = g_strconcat (g_test_get_dir (G_TEST_BUILT), "/appinfo-test --option", NULL);
164 cmdline_out = g_strconcat (cmdline, " %u", NULL);
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));
178 g_object_unref (appinfo);
180 g_free (cmdline_out);
181 cmdline_out = g_strconcat (cmdline, " %f", NULL);
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));
195 g_object_unref (appinfo);
197 g_free (cmdline);
198 g_free (cmdline_out);
201 static void
202 test_launch_context (void)
204 GAppLaunchContext *context;
205 GAppInfo *appinfo;
206 gchar *str;
207 gchar *cmdline;
209 cmdline = g_strconcat (g_test_get_dir (G_TEST_BUILT), "/appinfo-test --option", NULL);
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);
217 str = g_app_launch_context_get_display (context, appinfo, NULL);
218 g_assert (str == NULL);
220 str = g_app_launch_context_get_startup_notify_id (context, appinfo, NULL);
221 g_assert (str == NULL);
223 g_object_unref (appinfo);
224 g_object_unref (context);
226 g_free (cmdline);
229 static gboolean launched_reached;
231 static void
232 launched (GAppLaunchContext *context,
233 GAppInfo *info,
234 GVariant *platform_data,
235 gpointer user_data)
237 gint pid;
239 pid = 0;
240 g_assert (g_variant_lookup (platform_data, "pid", "i", &pid));
241 g_assert (pid != 0);
243 launched_reached = TRUE;
246 static void
247 launch_failed (GAppLaunchContext *context,
248 const gchar *startup_notify_id)
250 g_assert_not_reached ();
253 static void
254 test_launch_context_signals (void)
256 GAppLaunchContext *context;
257 GAppInfo *appinfo;
258 GError *error = NULL;
259 gchar *cmdline;
261 cmdline = g_strconcat (g_test_get_dir (G_TEST_BUILT), "/appinfo-test --option", NULL);
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);
271 error = NULL;
272 g_assert (g_app_info_launch (appinfo, NULL, context, &error));
273 g_assert_no_error (error);
275 g_assert (launched_reached);
277 g_object_unref (appinfo);
278 g_object_unref (context);
280 g_free (cmdline);
283 static void
284 test_tryexec (void)
286 GAppInfo *appinfo;
287 const gchar *path;
289 path = g_test_get_filename (G_TEST_DIST, "appinfo-test2.desktop", NULL);
290 appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (path);
292 g_assert (appinfo == NULL);
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.
298 static void
299 test_associations (void)
301 GAppInfo *appinfo;
302 GAppInfo *appinfo2;
303 GError *error;
304 gboolean result;
305 GList *list;
306 gchar *cmdline;
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);
314 error = NULL;
315 result = g_app_info_set_as_default_for_type (appinfo, "application/x-glib-test", &error);
317 g_assert (result);
318 g_assert_no_error (error);
320 appinfo2 = g_app_info_get_default_for_type ("application/x-glib-test", FALSE);
322 g_assert (appinfo2);
323 g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, g_app_info_get_commandline (appinfo2));
325 g_object_unref (appinfo2);
327 result = g_app_info_set_as_default_for_extension (appinfo, "gio-tests", &error);
328 g_assert (result);
329 g_assert_no_error (error);
331 appinfo2 = g_app_info_get_default_for_type ("application/x-extension-gio-tests", FALSE);
333 g_assert (appinfo2);
334 g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, g_app_info_get_commandline (appinfo2));
336 g_object_unref (appinfo2);
338 result = g_app_info_add_supports_type (appinfo, "application/x-gio-test", &error);
339 g_assert (result);
340 g_assert_no_error (error);
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);
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);
353 g_assert (g_app_info_can_delete (appinfo));
354 g_assert (g_app_info_delete (appinfo));
355 g_object_unref (appinfo);
358 static void
359 test_environment (void)
361 GAppLaunchContext *ctx;
362 gchar **env;
363 const gchar *path;
365 g_unsetenv ("FOO");
366 g_unsetenv ("BLA");
367 path = g_getenv ("PATH");
369 ctx = g_app_launch_context_new ();
371 env = g_app_launch_context_get_environment (ctx);
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);
377 g_strfreev (env);
379 g_app_launch_context_setenv (ctx, "FOO", "bar");
380 g_app_launch_context_setenv (ctx, "BLA", "bla");
382 env = g_app_launch_context_get_environment (ctx);
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);
388 g_strfreev (env);
390 g_app_launch_context_setenv (ctx, "FOO", "baz");
391 g_app_launch_context_unsetenv (ctx, "BLA");
393 env = g_app_launch_context_get_environment (ctx);
395 g_assert_cmpstr (g_environ_getenv (env, "FOO"), ==, "baz");
396 g_assert (g_environ_getenv (env, "BLA") == NULL);
398 g_strfreev (env);
400 g_object_unref (ctx);
403 static void
404 test_startup_wm_class (void)
406 GDesktopAppInfo *appinfo;
407 const char *wm_class;
408 const gchar *path;
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);
414 g_assert_cmpstr (wm_class, ==, "appinfo-class");
416 g_object_unref (appinfo);
419 static void
420 test_supported_types (void)
422 GAppInfo *appinfo;
423 const char * const *content_types;
424 const gchar *path;
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);
430 g_assert_cmpint (g_strv_length ((char**)content_types), ==, 2);
431 g_assert_cmpstr (content_types[0], ==, "image/png");
433 g_object_unref (appinfo);
436 static void
437 test_from_keyfile (void)
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;
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);
456 g_object_get (info, "filename", &file, NULL);
457 g_assert (file == NULL);
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));
471 g_object_unref (info);
475 main (int argc, char *argv[])
477 g_setenv ("XDG_CURRENT_DESKTOP", "GNOME", TRUE);
479 g_test_init (&argc, &argv, NULL);
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);
495 return g_test_run ();