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;
37 const gchar *myapp_data =
45 const gchar *myapp2_data =
53 /* Test that we handle mimeapps.list as expected.
54 * These tests are different from the ones in appinfo.c
55 * in that we directly parse mimeapps.list here
56 * to verify the results.
58 * We need to keep this test in a separate binary, since
59 * g_get_user_data_dir() doesn't get updated at runtime.
77 dir = g_get_current_dir ();
78 xdgdir = g_build_filename (dir, "xdgdatahome", NULL);
79 g_test_message ("setting XDG_DATA_HOME to '%s'\n", xdgdir);
80 g_setenv ("XDG_DATA_HOME", xdgdir, TRUE);
81 g_setenv ("XDG_DATA_DIRS", " ", TRUE);
83 appdir = g_build_filename (xdgdir, "applications", NULL);
84 g_test_message ("creating '%s'\n", appdir);
85 res = g_mkdir_with_parents (appdir, 0700);
88 name = g_build_filename (appdir, "myapp.desktop", NULL);
89 g_test_message ("creating '%s'\n", name);
90 g_file_set_contents (name, myapp_data, -1, &error);
91 g_assert_no_error (error);
94 name = g_build_filename (appdir, "myapp2.desktop", NULL);
95 g_test_message ("creating '%s'\n", name);
96 g_file_set_contents (name, myapp2_data, -1, &error);
97 g_assert_no_error (error);
100 mimeapps = g_build_filename (appdir, "mimeapps.list", NULL);
101 g_test_message ("removing '%s'\n", mimeapps);
104 /* 1. add a non-default association */
105 appinfo = (GAppInfo*)g_desktop_app_info_new ("myapp.desktop");
106 g_app_info_add_supports_type (appinfo, "application/pdf", &error);
107 g_assert_no_error (error);
109 keyfile = g_key_file_new ();
110 g_key_file_load_from_file (keyfile, mimeapps, G_KEY_FILE_NONE, &error);
111 g_assert_no_error (error);
113 assoc = g_key_file_get_string_list (keyfile, "Added Associations", "application/pdf", NULL, &error);
114 g_assert_no_error (error);
115 g_assert (strv_equal (assoc, "myapp.desktop", NULL));
118 /* we've unset XDG_DATA_DIRS so there should be no default */
119 assoc = g_key_file_get_string_list (keyfile, "Default Applications", "application/pdf", NULL, &error);
120 g_assert_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
121 g_clear_error (&error);
123 g_key_file_free (keyfile);
125 /* 2. add another non-default association */
126 appinfo2 = (GAppInfo*)g_desktop_app_info_new ("myapp2.desktop");
127 g_app_info_add_supports_type (appinfo2, "application/pdf", &error);
128 g_assert_no_error (error);
130 keyfile = g_key_file_new ();
131 g_key_file_load_from_file (keyfile, mimeapps, G_KEY_FILE_NONE, &error);
132 g_assert_no_error (error);
134 assoc = g_key_file_get_string_list (keyfile, "Added Associations", "application/pdf", NULL, &error);
135 g_assert_no_error (error);
136 g_assert (strv_equal (assoc, "myapp.desktop", "myapp2.desktop", NULL));
139 assoc = g_key_file_get_string_list (keyfile, "Default Applications", "application/pdf", NULL, &error);
140 g_assert_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
141 g_clear_error (&error);
143 g_key_file_free (keyfile);
145 /* 3. make the first app the default */
146 g_app_info_set_as_default_for_type (appinfo, "application/pdf", &error);
147 g_assert_no_error (error);
149 keyfile = g_key_file_new ();
150 g_key_file_load_from_file (keyfile, mimeapps, G_KEY_FILE_NONE, &error);
151 g_assert_no_error (error);
153 assoc = g_key_file_get_string_list (keyfile, "Added Associations", "application/pdf", NULL, &error);
154 g_assert_no_error (error);
155 g_assert (strv_equal (assoc, "myapp.desktop", "myapp2.desktop", NULL));
158 str = g_key_file_get_string (keyfile, "Default Applications", "application/pdf", &error);
159 g_assert_no_error (error);
160 g_assert_cmpstr (str, ==, "myapp.desktop");
162 g_key_file_free (keyfile);
164 /* 4. make the second app the last used one */
165 g_app_info_set_as_last_used_for_type (appinfo2, "application/pdf", &error);
166 g_assert_no_error (error);
168 keyfile = g_key_file_new ();
169 g_key_file_load_from_file (keyfile, mimeapps, G_KEY_FILE_NONE, &error);
170 g_assert_no_error (error);
172 assoc = g_key_file_get_string_list (keyfile, "Added Associations", "application/pdf", NULL, &error);
173 g_assert_no_error (error);
174 g_assert (strv_equal (assoc, "myapp2.desktop", "myapp.desktop", NULL));
177 g_key_file_free (keyfile);
179 /* 5. reset everything */
180 g_app_info_reset_type_associations ("application/pdf");
182 keyfile = g_key_file_new ();
183 g_key_file_load_from_file (keyfile, mimeapps, G_KEY_FILE_NONE, &error);
184 g_assert_no_error (error);
186 res = g_key_file_has_key (keyfile, "Added Associations", "application/pdf", NULL);
189 res = g_key_file_has_key (keyfile, "Default Applications", "application/pdf", NULL);
192 g_key_file_free (keyfile);
194 g_object_unref (appinfo);
195 g_object_unref (appinfo2);
204 main (int argc, char *argv[])
207 g_test_init (&argc, &argv, NULL);
209 g_test_add_func ("/appinfo/mimeapps", test_mimeapps);
211 return g_test_run ();