Remove developer script not needed in git repository
[glib.git] / gio / tests / appinfo.c
blob2e69da0511a8fcc8dcf6227dfd2295b90f1fdbf5
2 #include <locale.h>
3 #include <string.h>
5 #include <glib/gstdio.h>
6 #include <gio/gio.h>
7 #include <gio/gdesktopappinfo.h>
9 static void
10 test_launch_for_app_info (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 error = NULL;
25 g_assert (g_app_info_launch (appinfo, NULL, NULL, &error));
26 g_assert_no_error (error);
28 g_assert (g_app_info_launch_uris (appinfo, NULL, NULL, &error));
29 g_assert_no_error (error);
31 path = g_test_get_filename (G_TEST_DIST, "appinfo-test.desktop", NULL);
32 file = g_file_new_for_path (path);
33 l = NULL;
34 l = g_list_append (l, file);
36 g_assert (g_app_info_launch (appinfo, l, NULL, &error));
37 g_assert_no_error (error);
38 g_list_free (l);
39 g_object_unref (file);
41 l = NULL;
42 uri = g_strconcat ("file://", g_test_get_dir (G_TEST_DIST), "/appinfo-test.desktop", NULL);
43 l = g_list_append (l, uri);
44 l = g_list_append (l, "file:///etc/group#adm");
46 g_assert (g_app_info_launch_uris (appinfo, l, NULL, &error));
47 g_assert_no_error (error);
48 g_list_free (l);
49 g_free (uri);
52 static void
53 test_launch (void)
55 GAppInfo *appinfo;
56 const gchar *path;
58 path = g_test_get_filename (G_TEST_DIST, "appinfo-test.desktop", NULL);
59 appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (path);
60 g_assert (appinfo != NULL);
62 test_launch_for_app_info (appinfo);
63 g_object_unref (appinfo);
66 static void
67 test_launch_no_app_id (void)
69 const gchar desktop_file_base_contents[] =
70 "[Desktop Entry]\n"
71 "Type=Application\n"
72 "GenericName=generic-appinfo-test\n"
73 "Name=appinfo-test\n"
74 "Name[de]=appinfo-test-de\n"
75 "X-GNOME-FullName=example\n"
76 "X-GNOME-FullName[de]=Beispiel\n"
77 "Comment=GAppInfo example\n"
78 "Comment[de]=GAppInfo Beispiel\n"
79 "Icon=testicon.svg\n"
80 "Terminal=true\n"
81 "StartupNotify=true\n"
82 "StartupWMClass=appinfo-class\n"
83 "MimeType=image/png;image/jpeg;\n"
84 "Keywords=keyword1;test keyword;\n"
85 "Categories=GNOME;GTK;\n";
87 const char *exec_line_variants[] = {
88 "Exec=./appinfo-test --option %U %i --name %c --filename %k %m %%",
89 "Exec=./appinfo-test --option %u %i --name %c --filename %k %m %%"
92 gsize i;
94 g_test_bug ("791337");
96 for (i = 0; i < G_N_ELEMENTS (exec_line_variants); i++)
98 gchar *desktop_file_contents;
99 GKeyFile *fake_desktop_file;
100 GAppInfo *appinfo;
101 gboolean loaded;
103 g_test_message ("Exec line variant #%" G_GSIZE_FORMAT, i);
105 desktop_file_contents = g_strdup_printf ("%s\n%s",
106 desktop_file_base_contents,
107 exec_line_variants[i]);
109 /* We load a desktop file from memory to force the app not
110 * to have an app ID, which would check different codepaths.
112 fake_desktop_file = g_key_file_new ();
113 loaded = g_key_file_load_from_data (fake_desktop_file, desktop_file_contents, -1, G_KEY_FILE_NONE, NULL);
114 g_assert_true (loaded);
116 appinfo = (GAppInfo*)g_desktop_app_info_new_from_keyfile (fake_desktop_file);
117 g_assert (appinfo != NULL);
119 test_launch_for_app_info (appinfo);
121 g_free (desktop_file_contents);
122 g_object_unref (appinfo);
123 g_key_file_unref (fake_desktop_file);
127 static void
128 test_locale (const char *locale)
130 GAppInfo *appinfo;
131 const gchar *orig;
132 const gchar *path;
134 orig = setlocale (LC_ALL, NULL);
135 g_setenv ("LANGUAGE", locale, TRUE);
136 setlocale (LC_ALL, "");
138 path = g_test_get_filename (G_TEST_DIST, "appinfo-test.desktop", NULL);
139 appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (path);
141 if (g_strcmp0 (locale, "C") == 0)
143 g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "appinfo-test");
144 g_assert_cmpstr (g_app_info_get_description (appinfo), ==, "GAppInfo example");
145 g_assert_cmpstr (g_app_info_get_display_name (appinfo), ==, "example");
147 else if (g_str_has_prefix (locale, "en"))
149 g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "appinfo-test");
150 g_assert_cmpstr (g_app_info_get_description (appinfo), ==, "GAppInfo example");
151 g_assert_cmpstr (g_app_info_get_display_name (appinfo), ==, "example");
153 else if (g_str_has_prefix (locale, "de"))
155 g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "appinfo-test-de");
156 g_assert_cmpstr (g_app_info_get_description (appinfo), ==, "GAppInfo Beispiel");
157 g_assert_cmpstr (g_app_info_get_display_name (appinfo), ==, "Beispiel");
160 g_object_unref (appinfo);
162 g_setenv ("LANGUAGE", orig, TRUE);
163 setlocale (LC_ALL, "");
166 static void
167 test_text (void)
169 test_locale ("C");
170 test_locale ("en_US");
171 test_locale ("de");
172 test_locale ("de_DE.UTF-8");
175 static void
176 test_basic (void)
178 GAppInfo *appinfo;
179 GAppInfo *appinfo2;
180 GIcon *icon, *icon2;
181 const gchar *path;
183 path = g_test_get_filename (G_TEST_DIST, "appinfo-test.desktop", NULL);
184 appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (path);
186 g_assert_cmpstr (g_app_info_get_id (appinfo), ==, "appinfo-test.desktop");
187 g_assert (strstr (g_app_info_get_executable (appinfo), "appinfo-test") != NULL);
189 icon = g_app_info_get_icon (appinfo);
190 g_assert (G_IS_THEMED_ICON (icon));
191 icon2 = g_themed_icon_new ("testicon");
192 g_assert (g_icon_equal (icon, icon2));
193 g_object_unref (icon2);
195 appinfo2 = g_app_info_dup (appinfo);
196 g_assert_cmpstr (g_app_info_get_id (appinfo), ==, g_app_info_get_id (appinfo2));
197 g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, g_app_info_get_commandline (appinfo2));
199 g_object_unref (appinfo);
200 g_object_unref (appinfo2);
203 static void
204 test_show_in (void)
206 GAppInfo *appinfo;
207 const gchar *path;
209 path = g_test_get_filename (G_TEST_DIST, "appinfo-test.desktop", NULL);
210 appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (path);
211 g_assert (g_app_info_should_show (appinfo));
212 g_object_unref (appinfo);
214 path = g_test_get_filename (G_TEST_DIST, "appinfo-test-gnome.desktop", NULL);
215 appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (path);
216 g_assert (g_app_info_should_show (appinfo));
217 g_object_unref (appinfo);
219 path = g_test_get_filename (G_TEST_DIST, "appinfo-test-notgnome.desktop", NULL);
220 appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (path);
221 g_assert (!g_app_info_should_show (appinfo));
222 g_object_unref (appinfo);
225 static void
226 test_commandline (void)
228 GAppInfo *appinfo;
229 GError *error;
230 gchar *cmdline;
231 gchar *cmdline_out;
233 cmdline = g_strconcat (g_test_get_dir (G_TEST_BUILT), "/appinfo-test --option", NULL);
234 cmdline_out = g_strconcat (cmdline, " %u", NULL);
236 error = NULL;
237 appinfo = g_app_info_create_from_commandline (cmdline,
238 "cmdline-app-test",
239 G_APP_INFO_CREATE_SUPPORTS_URIS,
240 &error);
241 g_assert (appinfo != NULL);
242 g_assert_no_error (error);
243 g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "cmdline-app-test");
244 g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, cmdline_out);
245 g_assert (g_app_info_supports_uris (appinfo));
246 g_assert (!g_app_info_supports_files (appinfo));
248 g_object_unref (appinfo);
250 g_free (cmdline_out);
251 cmdline_out = g_strconcat (cmdline, " %f", NULL);
253 error = NULL;
254 appinfo = g_app_info_create_from_commandline (cmdline,
255 "cmdline-app-test",
256 G_APP_INFO_CREATE_NONE,
257 &error);
258 g_assert (appinfo != NULL);
259 g_assert_no_error (error);
260 g_assert_cmpstr (g_app_info_get_name (appinfo), ==, "cmdline-app-test");
261 g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, cmdline_out);
262 g_assert (!g_app_info_supports_uris (appinfo));
263 g_assert (g_app_info_supports_files (appinfo));
265 g_object_unref (appinfo);
267 g_free (cmdline);
268 g_free (cmdline_out);
271 static void
272 test_launch_context (void)
274 GAppLaunchContext *context;
275 GAppInfo *appinfo;
276 gchar *str;
277 gchar *cmdline;
279 cmdline = g_strconcat (g_test_get_dir (G_TEST_BUILT), "/appinfo-test --option", NULL);
281 context = g_app_launch_context_new ();
282 appinfo = g_app_info_create_from_commandline (cmdline,
283 "cmdline-app-test",
284 G_APP_INFO_CREATE_SUPPORTS_URIS,
285 NULL);
287 str = g_app_launch_context_get_display (context, appinfo, NULL);
288 g_assert (str == NULL);
290 str = g_app_launch_context_get_startup_notify_id (context, appinfo, NULL);
291 g_assert (str == NULL);
293 g_object_unref (appinfo);
294 g_object_unref (context);
296 g_free (cmdline);
299 static gboolean launched_reached;
301 static void
302 launched (GAppLaunchContext *context,
303 GAppInfo *info,
304 GVariant *platform_data,
305 gpointer user_data)
307 gint pid;
309 pid = 0;
310 g_assert (g_variant_lookup (platform_data, "pid", "i", &pid));
311 g_assert (pid != 0);
313 launched_reached = TRUE;
316 static void
317 launch_failed (GAppLaunchContext *context,
318 const gchar *startup_notify_id)
320 g_assert_not_reached ();
323 static void
324 test_launch_context_signals (void)
326 GAppLaunchContext *context;
327 GAppInfo *appinfo;
328 GError *error = NULL;
329 gchar *cmdline;
331 cmdline = g_strconcat (g_test_get_dir (G_TEST_BUILT), "/appinfo-test --option", NULL);
333 context = g_app_launch_context_new ();
334 g_signal_connect (context, "launched", G_CALLBACK (launched), NULL);
335 g_signal_connect (context, "launch_failed", G_CALLBACK (launch_failed), NULL);
336 appinfo = g_app_info_create_from_commandline (cmdline,
337 "cmdline-app-test",
338 G_APP_INFO_CREATE_SUPPORTS_URIS,
339 NULL);
341 error = NULL;
342 g_assert (g_app_info_launch (appinfo, NULL, context, &error));
343 g_assert_no_error (error);
345 g_assert (launched_reached);
347 g_object_unref (appinfo);
348 g_object_unref (context);
350 g_free (cmdline);
353 static void
354 test_tryexec (void)
356 GAppInfo *appinfo;
357 const gchar *path;
359 path = g_test_get_filename (G_TEST_DIST, "appinfo-test2.desktop", NULL);
360 appinfo = (GAppInfo*)g_desktop_app_info_new_from_filename (path);
362 g_assert (appinfo == NULL);
365 /* Test that we can set an appinfo as default for a mime type or
366 * file extension, and also add and remove handled mime types.
368 static void
369 test_associations (void)
371 GAppInfo *appinfo;
372 GAppInfo *appinfo2;
373 GError *error;
374 gboolean result;
375 GList *list;
376 gchar *cmdline;
378 cmdline = g_strconcat (g_test_get_dir (G_TEST_BUILT), "/appinfo-test --option", NULL);
379 appinfo = g_app_info_create_from_commandline (cmdline,
380 "cmdline-app-test",
381 G_APP_INFO_CREATE_SUPPORTS_URIS,
382 NULL);
384 error = NULL;
385 result = g_app_info_set_as_default_for_type (appinfo, "application/x-glib-test", &error);
387 g_assert (result);
388 g_assert_no_error (error);
390 appinfo2 = g_app_info_get_default_for_type ("application/x-glib-test", FALSE);
392 g_assert (appinfo2);
393 g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, g_app_info_get_commandline (appinfo2));
395 g_object_unref (appinfo2);
397 result = g_app_info_set_as_default_for_extension (appinfo, "gio-tests", &error);
398 g_assert (result);
399 g_assert_no_error (error);
401 appinfo2 = g_app_info_get_default_for_type ("application/x-extension-gio-tests", FALSE);
403 g_assert (appinfo2);
404 g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, g_app_info_get_commandline (appinfo2));
406 g_object_unref (appinfo2);
408 result = g_app_info_add_supports_type (appinfo, "application/x-gio-test", &error);
409 g_assert (result);
410 g_assert_no_error (error);
412 list = g_app_info_get_all_for_type ("application/x-gio-test");
413 g_assert_cmpint (g_list_length (list), ==, 1);
414 appinfo2 = list->data;
415 g_assert_cmpstr (g_app_info_get_commandline (appinfo), ==, g_app_info_get_commandline (appinfo2));
416 g_object_unref (appinfo2);
417 g_list_free (list);
419 g_assert (g_app_info_can_remove_supports_type (appinfo));
420 g_assert (g_app_info_remove_supports_type (appinfo, "application/x-gio-test", &error));
421 g_assert_no_error (error);
423 g_assert (g_app_info_can_delete (appinfo));
424 g_assert (g_app_info_delete (appinfo));
425 g_object_unref (appinfo);
428 static void
429 test_environment (void)
431 GAppLaunchContext *ctx;
432 gchar **env;
433 const gchar *path;
435 g_unsetenv ("FOO");
436 g_unsetenv ("BLA");
437 path = g_getenv ("PATH");
439 ctx = g_app_launch_context_new ();
441 env = g_app_launch_context_get_environment (ctx);
443 g_assert (g_environ_getenv (env, "FOO") == NULL);
444 g_assert (g_environ_getenv (env, "BLA") == NULL);
445 g_assert_cmpstr (g_environ_getenv (env, "PATH"), ==, path);
447 g_strfreev (env);
449 g_app_launch_context_setenv (ctx, "FOO", "bar");
450 g_app_launch_context_setenv (ctx, "BLA", "bla");
452 env = g_app_launch_context_get_environment (ctx);
454 g_assert_cmpstr (g_environ_getenv (env, "FOO"), ==, "bar");
455 g_assert_cmpstr (g_environ_getenv (env, "BLA"), ==, "bla");
456 g_assert_cmpstr (g_environ_getenv (env, "PATH"), ==, path);
458 g_strfreev (env);
460 g_app_launch_context_setenv (ctx, "FOO", "baz");
461 g_app_launch_context_unsetenv (ctx, "BLA");
463 env = g_app_launch_context_get_environment (ctx);
465 g_assert_cmpstr (g_environ_getenv (env, "FOO"), ==, "baz");
466 g_assert (g_environ_getenv (env, "BLA") == NULL);
468 g_strfreev (env);
470 g_object_unref (ctx);
473 static void
474 test_startup_wm_class (void)
476 GDesktopAppInfo *appinfo;
477 const char *wm_class;
478 const gchar *path;
480 path = g_test_get_filename (G_TEST_DIST, "appinfo-test.desktop", NULL);
481 appinfo = g_desktop_app_info_new_from_filename (path);
482 wm_class = g_desktop_app_info_get_startup_wm_class (appinfo);
484 g_assert_cmpstr (wm_class, ==, "appinfo-class");
486 g_object_unref (appinfo);
489 static void
490 test_supported_types (void)
492 GAppInfo *appinfo;
493 const char * const *content_types;
494 const gchar *path;
496 path = g_test_get_filename (G_TEST_DIST, "appinfo-test.desktop", NULL);
497 appinfo = G_APP_INFO (g_desktop_app_info_new_from_filename (path));
498 content_types = g_app_info_get_supported_types (appinfo);
500 g_assert_cmpint (g_strv_length ((char**)content_types), ==, 2);
501 g_assert_cmpstr (content_types[0], ==, "image/png");
503 g_object_unref (appinfo);
506 static void
507 test_from_keyfile (void)
509 GDesktopAppInfo *info;
510 GKeyFile *kf;
511 GError *error = NULL;
512 const gchar *categories;
513 gchar **keywords;
514 const gchar *file;
515 const gchar *name;
516 const gchar *path;
518 path = g_test_get_filename (G_TEST_DIST, "appinfo-test.desktop", NULL);
519 kf = g_key_file_new ();
520 g_key_file_load_from_file (kf, path, G_KEY_FILE_NONE, &error);
521 g_assert_no_error (error);
522 info = g_desktop_app_info_new_from_keyfile (kf);
523 g_key_file_free (kf);
524 g_assert (info != NULL);
526 g_object_get (info, "filename", &file, NULL);
527 g_assert (file == NULL);
529 file = g_desktop_app_info_get_filename (info);
530 g_assert (file == NULL);
531 categories = g_desktop_app_info_get_categories (info);
532 g_assert_cmpstr (categories, ==, "GNOME;GTK;");
533 keywords = (gchar **)g_desktop_app_info_get_keywords (info);
534 g_assert_cmpint (g_strv_length (keywords), ==, 2);
535 g_assert_cmpstr (keywords[0], ==, "keyword1");
536 g_assert_cmpstr (keywords[1], ==, "test keyword");
537 name = g_desktop_app_info_get_generic_name (info);
538 g_assert_cmpstr (name, ==, "generic-appinfo-test");
539 g_assert (!g_desktop_app_info_get_nodisplay (info));
541 g_object_unref (info);
545 main (int argc, char *argv[])
547 const gchar *build_dir;
549 g_setenv ("XDG_CURRENT_DESKTOP", "GNOME", TRUE);
551 g_test_init (&argc, &argv, NULL);
552 g_test_bug_base ("https://bugzilla.gnome.org/show_bug.cgi?id=");
554 /* With Meson build we need to change into right directory, so that the
555 * appinfo-test binary can be found. */
556 build_dir = g_getenv ("G_TEST_BUILDDIR");
557 if (build_dir)
558 g_chdir (build_dir);
560 g_test_add_func ("/appinfo/basic", test_basic);
561 g_test_add_func ("/appinfo/text", test_text);
562 g_test_add_func ("/appinfo/launch", test_launch);
563 g_test_add_func ("/appinfo/launch/no-appid", test_launch_no_app_id);
564 g_test_add_func ("/appinfo/show-in", test_show_in);
565 g_test_add_func ("/appinfo/commandline", test_commandline);
566 g_test_add_func ("/appinfo/launch-context", test_launch_context);
567 g_test_add_func ("/appinfo/launch-context-signals", test_launch_context_signals);
568 g_test_add_func ("/appinfo/tryexec", test_tryexec);
569 g_test_add_func ("/appinfo/associations", test_associations);
570 g_test_add_func ("/appinfo/environment", test_environment);
571 g_test_add_func ("/appinfo/startup-wm-class", test_startup_wm_class);
572 g_test_add_func ("/appinfo/supported-types", test_supported_types);
573 g_test_add_func ("/appinfo/from-keyfile", test_from_keyfile);
575 return g_test_run ();