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.
79 dir = g_get_current_dir ();
80 xdgdir = g_build_filename (dir, "xdgdatahome", NULL);
81 g_test_message ("setting XDG_DATA_HOME to '%s'\n", xdgdir);
82 g_setenv ("XDG_DATA_HOME", xdgdir, TRUE);
83 g_setenv ("XDG_DATA_DIRS", " ", TRUE);
85 appdir = g_build_filename (xdgdir, "applications", NULL);
86 g_test_message ("creating '%s'\n", appdir);
87 res = g_mkdir_with_parents (appdir, 0700);
90 name = g_build_filename (appdir, "myapp.desktop", NULL);
91 g_test_message ("creating '%s'\n", name);
92 g_file_set_contents (name, myapp_data, -1, &error);
93 g_assert_no_error (error);
96 name = g_build_filename (appdir, "myapp2.desktop", NULL);
97 g_test_message ("creating '%s'\n", name);
98 g_file_set_contents (name, myapp2_data, -1, &error);
99 g_assert_no_error (error);
102 mimeapps = g_build_filename (appdir, "mimeapps.list", NULL);
103 g_test_message ("removing '%s'\n", mimeapps);
106 /* 1. add a non-default association */
107 appinfo = (GAppInfo*)g_desktop_app_info_new ("myapp.desktop");
108 g_app_info_add_supports_type (appinfo, "application/pdf", &error);
109 g_assert_no_error (error);
111 /* check api results */
112 def = g_app_info_get_default_for_type ("application/pdf", FALSE);
113 list = g_app_info_get_recommended_for_type ("application/pdf");
114 g_assert (g_app_info_equal (def, appinfo));
115 g_assert_cmpint (g_list_length (list), ==, 1);
116 g_assert (g_app_info_equal ((GAppInfo*)list->data, appinfo));
117 g_object_unref (def);
118 g_list_free_full (list, g_object_unref);
120 /* check mimeapps.list */
121 keyfile = g_key_file_new ();
122 g_key_file_load_from_file (keyfile, mimeapps, G_KEY_FILE_NONE, &error);
123 g_assert_no_error (error);
125 assoc = g_key_file_get_string_list (keyfile, "Added Associations", "application/pdf", NULL, &error);
126 g_assert_no_error (error);
127 g_assert (strv_equal (assoc, "myapp.desktop", NULL));
130 /* we've unset XDG_DATA_DIRS so there should be no default */
131 assoc = g_key_file_get_string_list (keyfile, "Default Applications", "application/pdf", NULL, &error);
132 g_assert_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
133 g_clear_error (&error);
135 g_key_file_free (keyfile);
137 /* 2. add another non-default association */
138 appinfo2 = (GAppInfo*)g_desktop_app_info_new ("myapp2.desktop");
139 g_app_info_add_supports_type (appinfo2, "application/pdf", &error);
140 g_assert_no_error (error);
142 /* check api results */
143 def = g_app_info_get_default_for_type ("application/pdf", FALSE);
144 list = g_app_info_get_recommended_for_type ("application/pdf");
145 g_assert (g_app_info_equal (def, appinfo));
146 g_assert_cmpint (g_list_length (list), ==, 2);
147 g_assert (g_app_info_equal ((GAppInfo*)list->data, appinfo));
148 g_assert (g_app_info_equal ((GAppInfo*)list->next->data, appinfo2));
149 g_object_unref (def);
150 g_list_free_full (list, g_object_unref);
152 /* check mimeapps.list */
153 keyfile = g_key_file_new ();
154 g_key_file_load_from_file (keyfile, mimeapps, G_KEY_FILE_NONE, &error);
155 g_assert_no_error (error);
157 assoc = g_key_file_get_string_list (keyfile, "Added Associations", "application/pdf", NULL, &error);
158 g_assert_no_error (error);
159 g_assert (strv_equal (assoc, "myapp.desktop", "myapp2.desktop", NULL));
162 assoc = g_key_file_get_string_list (keyfile, "Default Applications", "application/pdf", NULL, &error);
163 g_assert_error (error, G_KEY_FILE_ERROR, G_KEY_FILE_ERROR_GROUP_NOT_FOUND);
164 g_clear_error (&error);
166 g_key_file_free (keyfile);
168 /* 3. make the first app the default */
169 g_app_info_set_as_default_for_type (appinfo, "application/pdf", &error);
170 g_assert_no_error (error);
172 /* check api results */
173 def = g_app_info_get_default_for_type ("application/pdf", FALSE);
174 list = g_app_info_get_recommended_for_type ("application/pdf");
175 g_assert (g_app_info_equal (def, appinfo));
176 g_assert_cmpint (g_list_length (list), ==, 2);
177 g_assert (g_app_info_equal ((GAppInfo*)list->data, appinfo));
178 g_assert (g_app_info_equal ((GAppInfo*)list->next->data, appinfo2));
179 g_object_unref (def);
180 g_list_free_full (list, g_object_unref);
182 /* check mimeapps.list */
183 keyfile = g_key_file_new ();
184 g_key_file_load_from_file (keyfile, mimeapps, G_KEY_FILE_NONE, &error);
185 g_assert_no_error (error);
187 assoc = g_key_file_get_string_list (keyfile, "Added Associations", "application/pdf", NULL, &error);
188 g_assert_no_error (error);
189 g_assert (strv_equal (assoc, "myapp.desktop", "myapp2.desktop", NULL));
192 str = g_key_file_get_string (keyfile, "Default Applications", "application/pdf", &error);
193 g_assert_no_error (error);
194 g_assert_cmpstr (str, ==, "myapp.desktop");
196 g_key_file_free (keyfile);
198 /* 4. make the second app the last used one */
199 g_app_info_set_as_last_used_for_type (appinfo2, "application/pdf", &error);
200 g_assert_no_error (error);
202 /* check api results */
203 def = g_app_info_get_default_for_type ("application/pdf", FALSE);
204 list = g_app_info_get_recommended_for_type ("application/pdf");
205 g_assert (g_app_info_equal (def, appinfo));
206 g_assert_cmpint (g_list_length (list), ==, 2);
207 g_assert (g_app_info_equal ((GAppInfo*)list->data, appinfo2));
208 g_assert (g_app_info_equal ((GAppInfo*)list->next->data, appinfo));
209 g_object_unref (def);
210 g_list_free_full (list, g_object_unref);
212 /* check mimeapps.list */
213 keyfile = g_key_file_new ();
214 g_key_file_load_from_file (keyfile, mimeapps, G_KEY_FILE_NONE, &error);
215 g_assert_no_error (error);
217 assoc = g_key_file_get_string_list (keyfile, "Added Associations", "application/pdf", NULL, &error);
218 g_assert_no_error (error);
219 g_assert (strv_equal (assoc, "myapp2.desktop", "myapp.desktop", NULL));
222 g_key_file_free (keyfile);
224 /* 5. reset everything */
225 g_app_info_reset_type_associations ("application/pdf");
227 /* check api results */
228 def = g_app_info_get_default_for_type ("application/pdf", FALSE);
229 list = g_app_info_get_recommended_for_type ("application/pdf");
230 g_assert (def == NULL);
231 g_assert (list == NULL);
233 /* check mimeapps.list */
234 keyfile = g_key_file_new ();
235 g_key_file_load_from_file (keyfile, mimeapps, G_KEY_FILE_NONE, &error);
236 g_assert_no_error (error);
238 res = g_key_file_has_key (keyfile, "Added Associations", "application/pdf", NULL);
241 res = g_key_file_has_key (keyfile, "Default Applications", "application/pdf", NULL);
244 g_key_file_free (keyfile);
246 g_object_unref (appinfo);
247 g_object_unref (appinfo2);
256 main (int argc, char *argv[])
259 g_test_init (&argc, &argv, NULL);
261 g_test_add_func ("/appinfo/mimeapps", test_mimeapps);
263 return g_test_run ();