1 #include <glib/gstdio.h>
3 #include <gio/gdesktopappinfo.h>
6 strv_equal (gchar **strv, ...)
15 va_start (list, strv);
18 str = va_arg (list, const gchar *);
21 if (g_strcmp0 (str, strv[count]) != 0)
31 res = g_strv_length (strv) == count;
36 const gchar *myapp_data =
44 const gchar *myapp2_data =
52 const gchar *myapp3_data =
59 "MimeType=image/png;";
61 const gchar *myapp4_data =
68 "MimeType=image/bmp;";
70 const gchar *myapp5_data =
77 "MimeType=image/bmp;x-scheme-handler/ftp;";
79 const gchar *nosuchapp_data =
84 "Exec=no_such_application %f\n"
87 const gchar *defaults_data =
88 "[Default Applications]\n"
89 "image/bmp=myapp4.desktop;\n"
90 "image/png=myapp3.desktop;\n"
91 "x-scheme-handler/ftp=myapp5.desktop;\n";
93 const gchar *mimecache_data =
95 "image/bmp=myapp4.desktop;myapp5.desktop;\n"
96 "image/png=myapp3.desktop;\n";
100 gchar *mimeapps_list_home; /* (owned) */
103 /* Set up XDG_DATA_HOME and XDG_DATA_DIRS.
104 * XDG_DATA_DIRS/applications will contain mimeapps.list
105 * XDG_DATA_HOME/applications will contain myapp.desktop
106 * and myapp2.desktop, and no mimeapps.list
109 setup (Fixture *fixture,
110 gconstpointer test_data)
112 const gchar *xdgdatahome;
113 const gchar * const *xdgdatadirs;
119 GError *error = NULL;
121 /* These are already set to a temporary directory through our use of
122 * %G_TEST_OPTION_ISOLATE_DIRS below. */
123 xdgdatahome = g_get_user_data_dir ();
124 xdgdatadirs = g_get_system_data_dirs ();
126 appdir = g_build_filename (xdgdatadirs[0], "applications", NULL);
127 g_test_message ("creating '%s'", appdir);
128 res = g_mkdir_with_parents (appdir, 0700);
129 g_assert_cmpint (res, ==, 0);
131 name = g_build_filename (appdir, "mimeapps.list", NULL);
132 g_test_message ("creating '%s'", name);
133 g_file_set_contents (name, defaults_data, -1, &error);
134 g_assert_no_error (error);
137 apphome = g_build_filename (xdgdatahome, "applications", NULL);
138 g_test_message ("creating '%s'", apphome);
139 res = g_mkdir_with_parents (apphome, 0700);
140 g_assert_cmpint (res, ==, 0);
142 name = g_build_filename (apphome, "myapp.desktop", NULL);
143 g_test_message ("creating '%s'", name);
144 g_file_set_contents (name, myapp_data, -1, &error);
145 g_assert_no_error (error);
148 name = g_build_filename (apphome, "myapp2.desktop", NULL);
149 g_test_message ("creating '%s'", name);
150 g_file_set_contents (name, myapp2_data, -1, &error);
151 g_assert_no_error (error);
154 name = g_build_filename (apphome, "myapp3.desktop", NULL);
155 g_test_message ("creating '%s'", name);
156 g_file_set_contents (name, myapp3_data, -1, &error);
157 g_assert_no_error (error);
160 name = g_build_filename (apphome, "myapp4.desktop", NULL);
161 g_test_message ("creating '%s'", name);
162 g_file_set_contents (name, myapp4_data, -1, &error);
163 g_assert_no_error (error);
166 name = g_build_filename (apphome, "myapp5.desktop", NULL);
167 g_test_message ("creating '%s'", name);
168 g_file_set_contents (name, myapp5_data, -1, &error);
169 g_assert_no_error (error);
172 name = g_build_filename (apphome, "nosuchapp.desktop", NULL);
173 g_test_message ("creating '%s'", name);
174 g_file_set_contents (name, nosuchapp_data, -1, &error);
175 g_assert_no_error (error);
178 mimeapps = g_build_filename (apphome, "mimeapps.list", NULL);
179 g_test_message ("removing '%s'", mimeapps);
182 name = g_build_filename (apphome, "mimeinfo.cache", NULL);
183 g_test_message ("creating '%s'", name);
184 g_file_set_contents (name, mimecache_data, -1, &error);
185 g_assert_no_error (error);
192 /* Pointer to one of the temporary directories. */
193 fixture->mimeapps_list_home = g_build_filename (g_get_user_config_dir (), "mimeapps.list", NULL);
197 teardown (Fixture *fixture,
198 gconstpointer test_data)
200 g_free (fixture->mimeapps_list_home);
204 test_mime_api (Fixture *fixture,
205 gconstpointer test_data)
209 GError *error = NULL;
212 const gchar *contenttype = "application/pdf";
214 /* clear things out */
215 g_app_info_reset_type_associations (contenttype);
217 appinfo = (GAppInfo*)g_desktop_app_info_new ("myapp.desktop");
218 appinfo2 = (GAppInfo*)g_desktop_app_info_new ("myapp2.desktop");
220 def = g_app_info_get_default_for_type (contenttype, FALSE);
221 list = g_app_info_get_recommended_for_type (contenttype);
223 g_assert_null (list);
225 /* 1. add a non-default association */
226 g_app_info_add_supports_type (appinfo, contenttype, &error);
227 g_assert_no_error (error);
229 def = g_app_info_get_default_for_type (contenttype, FALSE);
230 list = g_app_info_get_recommended_for_type (contenttype);
231 g_assert_true (g_app_info_equal (def, appinfo));
232 g_assert_cmpint (g_list_length (list), ==, 1);
233 g_assert_true (g_app_info_equal ((GAppInfo*)list->data, appinfo));
234 g_object_unref (def);
235 g_list_free_full (list, g_object_unref);
237 /* 2. add another non-default association */
238 g_app_info_add_supports_type (appinfo2, contenttype, &error);
239 g_assert_no_error (error);
241 def = g_app_info_get_default_for_type (contenttype, FALSE);
242 list = g_app_info_get_recommended_for_type (contenttype);
243 g_assert_true (g_app_info_equal (def, appinfo));
244 g_assert_cmpint (g_list_length (list), ==, 2);
245 g_assert_true (g_app_info_equal ((GAppInfo*)list->data, appinfo));
246 g_assert_true (g_app_info_equal ((GAppInfo*)list->next->data, appinfo2));
247 g_object_unref (def);
248 g_list_free_full (list, g_object_unref);
250 /* 3. make the first app the default */
251 g_app_info_set_as_default_for_type (appinfo, contenttype, &error);
252 g_assert_no_error (error);
254 def = g_app_info_get_default_for_type (contenttype, FALSE);
255 list = g_app_info_get_recommended_for_type (contenttype);
256 g_assert_true (g_app_info_equal (def, appinfo));
257 g_assert_cmpint (g_list_length (list), ==, 2);
258 g_assert_true (g_app_info_equal ((GAppInfo*)list->data, appinfo));
259 g_assert_true (g_app_info_equal ((GAppInfo*)list->next->data, appinfo2));
260 g_object_unref (def);
261 g_list_free_full (list, g_object_unref);
263 /* 4. make the second app the last used one */
264 g_app_info_set_as_last_used_for_type (appinfo2, contenttype, &error);
265 g_assert_no_error (error);
267 def = g_app_info_get_default_for_type (contenttype, FALSE);
268 list = g_app_info_get_recommended_for_type (contenttype);
269 g_assert_true (g_app_info_equal (def, appinfo));
270 g_assert_cmpint (g_list_length (list), ==, 2);
271 g_assert_true (g_app_info_equal ((GAppInfo*)list->data, appinfo2));
272 g_assert_true (g_app_info_equal ((GAppInfo*)list->next->data, appinfo));
273 g_object_unref (def);
274 g_list_free_full (list, g_object_unref);
276 /* 5. reset everything */
277 g_app_info_reset_type_associations (contenttype);
279 def = g_app_info_get_default_for_type (contenttype, FALSE);
280 list = g_app_info_get_recommended_for_type (contenttype);
282 g_assert_null (list);
284 g_object_unref (appinfo);
285 g_object_unref (appinfo2);
288 /* Repeat the same tests, this time checking that we handle
289 * mimeapps.list as expected. These tests are different from
290 * the ones in test_mime_api() in that we directly parse
291 * mimeapps.list to verify the results.
294 test_mime_file (Fixture *fixture,
295 gconstpointer test_data)
300 GError *error = NULL;
306 const gchar *contenttype = "application/pdf";
308 /* clear things out */
309 g_app_info_reset_type_associations (contenttype);
311 appinfo = (GAppInfo*)g_desktop_app_info_new ("myapp.desktop");
312 appinfo2 = (GAppInfo*)g_desktop_app_info_new ("myapp2.desktop");
314 def = g_app_info_get_default_for_type (contenttype, FALSE);
315 list = g_app_info_get_recommended_for_type (contenttype);
317 g_assert_null (list);
319 /* 1. add a non-default association */
320 g_app_info_add_supports_type (appinfo, contenttype, &error);
321 g_assert_no_error (error);
323 keyfile = g_key_file_new ();
324 g_key_file_load_from_file (keyfile, fixture->mimeapps_list_home, G_KEY_FILE_NONE, &error);
325 g_assert_no_error (error);
327 assoc = g_key_file_get_string_list (keyfile, "Added Associations", contenttype, NULL, &error);
328 g_assert_no_error (error);
329 g_assert_true (strv_equal (assoc, "myapp.desktop", NULL));
332 /* we've unset XDG_DATA_DIRS so there should be no default */
333 assoc = g_key_file_get_string_list (keyfile, "Default Applications", contenttype, NULL, &error);
334 g_assert_nonnull (error);
335 g_clear_error (&error);
337 g_key_file_free (keyfile);
339 /* 2. add another non-default association */
340 g_app_info_add_supports_type (appinfo2, contenttype, &error);
341 g_assert_no_error (error);
343 keyfile = g_key_file_new ();
344 g_key_file_load_from_file (keyfile, fixture->mimeapps_list_home, G_KEY_FILE_NONE, &error);
345 g_assert_no_error (error);
347 assoc = g_key_file_get_string_list (keyfile, "Added Associations", contenttype, NULL, &error);
348 g_assert_no_error (error);
349 g_assert_true (strv_equal (assoc, "myapp.desktop", "myapp2.desktop", NULL));
352 assoc = g_key_file_get_string_list (keyfile, "Default Applications", contenttype, NULL, &error);
353 g_assert_nonnull (error);
354 g_clear_error (&error);
356 g_key_file_free (keyfile);
358 /* 3. make the first app the default */
359 g_app_info_set_as_default_for_type (appinfo, contenttype, &error);
360 g_assert_no_error (error);
362 keyfile = g_key_file_new ();
363 g_key_file_load_from_file (keyfile, fixture->mimeapps_list_home, G_KEY_FILE_NONE, &error);
364 g_assert_no_error (error);
366 assoc = g_key_file_get_string_list (keyfile, "Added Associations", contenttype, NULL, &error);
367 g_assert_no_error (error);
368 g_assert_true (strv_equal (assoc, "myapp.desktop", "myapp2.desktop", NULL));
371 str = g_key_file_get_string (keyfile, "Default Applications", contenttype, &error);
372 g_assert_no_error (error);
373 g_assert_cmpstr (str, ==, "myapp.desktop");
376 g_key_file_free (keyfile);
378 /* 4. make the second app the last used one */
379 g_app_info_set_as_last_used_for_type (appinfo2, contenttype, &error);
380 g_assert_no_error (error);
382 keyfile = g_key_file_new ();
383 g_key_file_load_from_file (keyfile, fixture->mimeapps_list_home, G_KEY_FILE_NONE, &error);
384 g_assert_no_error (error);
386 assoc = g_key_file_get_string_list (keyfile, "Added Associations", contenttype, NULL, &error);
387 g_assert_no_error (error);
388 g_assert_true (strv_equal (assoc, "myapp2.desktop", "myapp.desktop", NULL));
391 g_key_file_free (keyfile);
393 /* 5. reset everything */
394 g_app_info_reset_type_associations (contenttype);
396 keyfile = g_key_file_new ();
397 g_key_file_load_from_file (keyfile, fixture->mimeapps_list_home, G_KEY_FILE_NONE, &error);
398 g_assert_no_error (error);
400 res = g_key_file_has_key (keyfile, "Added Associations", contenttype, NULL);
401 g_assert_false (res);
403 res = g_key_file_has_key (keyfile, "Default Applications", contenttype, NULL);
404 g_assert_false (res);
406 g_key_file_free (keyfile);
408 g_object_unref (appinfo);
409 g_object_unref (appinfo2);
412 /* test interaction between mimeapps.list at different levels */
414 test_mime_default (Fixture *fixture,
415 gconstpointer test_data)
420 GError *error = NULL;
423 const gchar *contenttype = "image/png";
425 /* clear things out */
426 g_app_info_reset_type_associations (contenttype);
428 appinfo = (GAppInfo*)g_desktop_app_info_new ("myapp.desktop");
429 appinfo2 = (GAppInfo*)g_desktop_app_info_new ("myapp2.desktop");
430 appinfo3 = (GAppInfo*)g_desktop_app_info_new ("myapp3.desktop");
432 /* myapp3 is set as the default in defaults.list */
433 def = g_app_info_get_default_for_type (contenttype, FALSE);
434 list = g_app_info_get_recommended_for_type (contenttype);
435 g_assert_true (g_app_info_equal (def, appinfo3));
436 g_assert_cmpint (g_list_length (list), ==, 1);
437 g_assert_true (g_app_info_equal ((GAppInfo*)list->data, appinfo3));
438 g_object_unref (def);
439 g_list_free_full (list, g_object_unref);
441 /* 1. add a non-default association */
442 g_app_info_add_supports_type (appinfo, contenttype, &error);
443 g_assert_no_error (error);
445 def = g_app_info_get_default_for_type (contenttype, FALSE);
446 list = g_app_info_get_recommended_for_type (contenttype);
447 g_assert_true (g_app_info_equal (def, appinfo3)); /* default is unaffected */
448 g_assert_cmpint (g_list_length (list), ==, 2);
449 g_assert_true (g_app_info_equal ((GAppInfo*)list->data, appinfo));
450 g_assert_true (g_app_info_equal ((GAppInfo*)list->next->data, appinfo3));
451 g_object_unref (def);
452 g_list_free_full (list, g_object_unref);
454 /* 2. add another non-default association */
455 g_app_info_add_supports_type (appinfo2, contenttype, &error);
456 g_assert_no_error (error);
458 def = g_app_info_get_default_for_type (contenttype, FALSE);
459 list = g_app_info_get_recommended_for_type (contenttype);
460 g_assert_true (g_app_info_equal (def, appinfo3));
461 g_assert_cmpint (g_list_length (list), ==, 3);
462 g_assert_true (g_app_info_equal ((GAppInfo*)list->data, appinfo));
463 g_assert_true (g_app_info_equal ((GAppInfo*)list->next->data, appinfo2));
464 g_assert_true (g_app_info_equal ((GAppInfo*)list->next->next->data, appinfo3));
465 g_object_unref (def);
466 g_list_free_full (list, g_object_unref);
468 /* 3. make the first app the default */
469 g_app_info_set_as_default_for_type (appinfo, contenttype, &error);
470 g_assert_no_error (error);
472 def = g_app_info_get_default_for_type (contenttype, FALSE);
473 list = g_app_info_get_recommended_for_type (contenttype);
474 g_assert_true (g_app_info_equal (def, appinfo));
475 g_assert_cmpint (g_list_length (list), ==, 3);
476 g_assert_true (g_app_info_equal ((GAppInfo*)list->data, appinfo));
477 g_assert_true (g_app_info_equal ((GAppInfo*)list->next->data, appinfo2));
478 g_assert_true (g_app_info_equal ((GAppInfo*)list->next->next->data, appinfo3));
479 g_object_unref (def);
480 g_list_free_full (list, g_object_unref);
482 g_object_unref (appinfo);
483 g_object_unref (appinfo2);
484 g_object_unref (appinfo3);
487 /* test interaction between mimeinfo.cache, defaults.list and mimeapps.list
488 * to ensure g_app_info_set_as_last_used_for_type doesn't incorrectly
492 test_mime_default_last_used (Fixture *fixture,
493 gconstpointer test_data)
497 GError *error = NULL;
500 const gchar *contenttype = "image/bmp";
502 /* clear things out */
503 g_app_info_reset_type_associations (contenttype);
505 appinfo4 = (GAppInfo*)g_desktop_app_info_new ("myapp4.desktop");
506 appinfo5 = (GAppInfo*)g_desktop_app_info_new ("myapp5.desktop");
508 /* myapp4 is set as the default in defaults.list */
509 /* myapp4 and myapp5 can both handle image/bmp */
510 def = g_app_info_get_default_for_type (contenttype, FALSE);
511 list = g_app_info_get_recommended_for_type (contenttype);
512 g_assert_true (g_app_info_equal (def, appinfo4));
513 g_assert_cmpint (g_list_length (list), ==, 2);
514 g_assert_true (g_app_info_equal ((GAppInfo*)list->data, appinfo4));
515 g_assert_true (g_app_info_equal ((GAppInfo*)list->next->data, appinfo5));
516 g_object_unref (def);
517 g_list_free_full (list, g_object_unref);
519 /* 1. set default (myapp4) as last used */
520 g_app_info_set_as_last_used_for_type (appinfo4, contenttype, &error);
521 g_assert_no_error (error);
523 def = g_app_info_get_default_for_type (contenttype, FALSE);
524 list = g_app_info_get_recommended_for_type (contenttype);
525 g_assert_true (g_app_info_equal (def, appinfo4)); /* default is unaffected */
526 g_assert_cmpint (g_list_length (list), ==, 2);
527 g_assert_true (g_app_info_equal ((GAppInfo*)list->data, appinfo4));
528 g_assert_true (g_app_info_equal ((GAppInfo*)list->next->data, appinfo5));
529 g_object_unref (def);
530 g_list_free_full (list, g_object_unref);
532 /* 2. set other (myapp5) as last used */
533 g_app_info_set_as_last_used_for_type (appinfo5, contenttype, &error);
534 g_assert_no_error (error);
536 def = g_app_info_get_default_for_type (contenttype, FALSE);
537 list = g_app_info_get_recommended_for_type (contenttype);
538 g_assert_true (g_app_info_equal (def, appinfo4));
539 g_assert_cmpint (g_list_length (list), ==, 2);
540 g_assert_true (g_app_info_equal ((GAppInfo*)list->data, appinfo5));
541 g_assert_true (g_app_info_equal ((GAppInfo*)list->next->data, appinfo4));
542 g_object_unref (def);
543 g_list_free_full (list, g_object_unref);
545 /* 3. change the default to myapp5 */
546 g_app_info_set_as_default_for_type (appinfo5, contenttype, &error);
547 g_assert_no_error (error);
549 def = g_app_info_get_default_for_type (contenttype, FALSE);
550 list = g_app_info_get_recommended_for_type (contenttype);
551 g_assert_true (g_app_info_equal (def, appinfo5));
552 g_assert_cmpint (g_list_length (list), ==, 2);
553 g_assert_true (g_app_info_equal ((GAppInfo*)list->data, appinfo5));
554 g_assert_true (g_app_info_equal ((GAppInfo*)list->next->data, appinfo4));
555 g_object_unref (def);
556 g_list_free_full (list, g_object_unref);
558 /* 4. set myapp4 as last used */
559 g_app_info_set_as_last_used_for_type (appinfo4, contenttype, &error);
560 g_assert_no_error (error);
562 def = g_app_info_get_default_for_type (contenttype, FALSE);
563 list = g_app_info_get_recommended_for_type (contenttype);
564 g_assert_true (g_app_info_equal (def, appinfo5));
565 g_assert_cmpint (g_list_length (list), ==, 2);
566 g_assert_true (g_app_info_equal ((GAppInfo*)list->data, appinfo4));
567 g_assert_true (g_app_info_equal ((GAppInfo*)list->next->data, appinfo5));
568 g_object_unref (def);
569 g_list_free_full (list, g_object_unref);
571 /* 5. set myapp5 as last used again */
572 g_app_info_set_as_last_used_for_type (appinfo5, contenttype, &error);
573 g_assert_no_error (error);
575 def = g_app_info_get_default_for_type (contenttype, FALSE);
576 list = g_app_info_get_recommended_for_type (contenttype);
577 g_assert_true (g_app_info_equal (def, appinfo5));
578 g_assert_cmpint (g_list_length (list), ==, 2);
579 g_assert_true (g_app_info_equal ((GAppInfo*)list->data, appinfo5));
580 g_assert_true (g_app_info_equal ((GAppInfo*)list->next->data, appinfo4));
581 g_object_unref (def);
582 g_list_free_full (list, g_object_unref);
584 g_object_unref (appinfo4);
585 g_object_unref (appinfo5);
589 test_scheme_handler (Fixture *fixture,
590 gconstpointer test_data)
592 GAppInfo *info, *info5;
594 info5 = (GAppInfo*)g_desktop_app_info_new ("myapp5.desktop");
595 info = g_app_info_get_default_for_uri_scheme ("ftp");
596 g_assert_true (g_app_info_equal (info, info5));
598 g_object_unref (info);
599 g_object_unref (info5);
602 /* test that g_app_info_* ignores desktop files with nonexisting executables
605 test_mime_ignore_nonexisting (Fixture *fixture,
606 gconstpointer test_data)
610 appinfo = (GAppInfo*)g_desktop_app_info_new ("nosuchapp.desktop");
611 g_assert_null (appinfo);
615 test_all (Fixture *fixture,
616 gconstpointer test_data)
620 all = g_app_info_get_all ();
622 for (l = all; l; l = l->next)
623 g_assert_true (G_IS_APP_INFO (l->data));
625 g_list_free_full (all, g_object_unref);
629 main (int argc, char *argv[])
631 g_test_init (&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL);
633 g_test_add ("/appinfo/mime/api", Fixture, NULL, setup,
634 test_mime_api, teardown);
635 g_test_add ("/appinfo/mime/default", Fixture, NULL, setup,
636 test_mime_default, teardown);
637 g_test_add ("/appinfo/mime/file", Fixture, NULL, setup,
638 test_mime_file, teardown);
639 g_test_add ("/appinfo/mime/scheme-handler", Fixture, NULL, setup,
640 test_scheme_handler, teardown);
641 g_test_add ("/appinfo/mime/default-last-used", Fixture, NULL, setup,
642 test_mime_default_last_used, teardown);
643 g_test_add ("/appinfo/mime/ignore-nonexisting", Fixture, NULL, setup,
644 test_mime_ignore_nonexisting, teardown);
645 g_test_add ("/appinfo/all", Fixture, NULL, setup, test_all, teardown);
647 return g_test_run ();