2 * Copyright (C) 2008 Red Hat, Inc
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General
15 * Public License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
17 * Boston, MA 02111-1307, USA.
19 * Author: Matthias Clasen
22 #include <glib/glib.h>
24 #include <gio/gdesktopappinfo.h>
31 create_app_info (const char *name)
37 info = g_app_info_create_from_commandline ("/usr/bin/true blah",
39 G_APP_INFO_CREATE_NONE,
41 g_assert (error == NULL);
43 /* this is necessary to ensure that the info is saved */
44 g_app_info_set_as_default_for_type (info, "application/x-blah", &error);
45 g_assert (error == NULL);
46 g_app_info_remove_supports_type (info, "application/x-blah", &error);
47 g_assert (error == NULL);
48 g_app_info_reset_type_associations ("application/x-blah");
62 info = create_app_info ("Blah");
64 id = g_app_info_get_id (info);
65 g_assert (id != NULL);
67 filename = g_build_filename (basedir, "applications", id, NULL);
69 res = g_file_test (filename, G_FILE_TEST_EXISTS);
72 res = g_app_info_can_delete (info);
75 res = g_app_info_delete (info);
78 res = g_file_test (filename, G_FILE_TEST_EXISTS);
81 g_object_unref (info);
83 if (g_file_test ("/usr/share/applications/gedit.desktop", G_FILE_TEST_EXISTS))
85 info = (GAppInfo*)g_desktop_app_info_new_from_filename ("/usr/share/applications/gedit.desktop");
88 res = g_app_info_can_delete (info);
91 res = g_app_info_delete (info);
99 GAppInfo *info, *info1, *info2, *info3;
101 GError *error = NULL;
103 info1 = create_app_info ("Blah1");
104 info2 = create_app_info ("Blah2");
105 info3 = create_app_info ("Blah3");
107 g_app_info_set_as_default_for_type (info1, "application/x-test", &error);
108 g_assert (error == NULL);
110 g_app_info_set_as_default_for_type (info2, "application/x-test", &error);
111 g_assert (error == NULL);
113 list = g_app_info_get_all_for_type ("application/x-test");
114 g_assert (g_list_length (list) == 2);
116 /* check that both are in the list, info2 before info1 */
117 info = (GAppInfo *)list->data;
118 g_assert (g_strcmp0 (g_app_info_get_id (info), g_app_info_get_id (info2)) == 0);
120 info = (GAppInfo *)list->next->data;
121 g_assert (g_strcmp0 (g_app_info_get_id (info), g_app_info_get_id (info1)) == 0);
123 g_list_free_full (list, g_object_unref);
125 /* now try adding something at the end */
126 g_app_info_add_supports_type (info3, "application/x-test", &error);
127 g_assert (error == NULL);
129 list = g_app_info_get_all_for_type ("application/x-test");
130 g_assert (g_list_length (list) == 3);
132 /* check that all are in the list, info2, info1, info3 */
133 info = (GAppInfo *)list->data;
134 g_assert (g_strcmp0 (g_app_info_get_id (info), g_app_info_get_id (info2)) == 0);
136 info = (GAppInfo *)list->next->data;
137 g_assert (g_strcmp0 (g_app_info_get_id (info), g_app_info_get_id (info1)) == 0);
139 info = (GAppInfo *)list->next->next->data;
140 g_assert (g_strcmp0 (g_app_info_get_id (info), g_app_info_get_id (info3)) == 0);
142 g_list_free_full (list, g_object_unref);
144 /* now remove info1 again */
145 g_app_info_remove_supports_type (info1, "application/x-test", &error);
146 g_assert (error == NULL);
148 list = g_app_info_get_all_for_type ("application/x-test");
149 g_assert (g_list_length (list) == 2);
151 /* check that both are in the list, info2 before info3 */
152 info = (GAppInfo *)list->data;
153 g_assert (g_strcmp0 (g_app_info_get_id (info), g_app_info_get_id (info2)) == 0);
155 info = (GAppInfo *)list->next->data;
156 g_assert (g_strcmp0 (g_app_info_get_id (info), g_app_info_get_id (info3)) == 0);
158 g_list_free_full (list, g_object_unref);
160 /* now clean it all up */
161 g_app_info_reset_type_associations ("application/x-test");
163 list = g_app_info_get_all_for_type ("application/x-test");
164 g_assert (list == NULL);
166 g_app_info_delete (info1);
167 g_app_info_delete (info2);
168 g_app_info_delete (info3);
170 g_object_unref (info1);
171 g_object_unref (info2);
177 GAppInfo *info1, *info2, *app;
178 GList *apps, *recomm, *fallback, *list, *l, *m;
179 GError *error = NULL;
182 info1 = create_app_info ("Test1");
183 info2 = create_app_info ("Test2");
185 g_assert (g_content_type_is_a ("text/x-python", "text/plain"));
187 apps = g_app_info_get_all_for_type ("text/x-python");
188 old_length = g_list_length (apps);
189 g_list_free_full (apps, g_object_unref);
191 g_app_info_set_as_default_for_type (info1, "text/x-python", &error);
192 g_assert (error == NULL);
194 g_app_info_set_as_default_for_type (info2, "text/plain", &error);
195 g_assert (error == NULL);
197 /* check that both apps are registered */
198 apps = g_app_info_get_all_for_type ("text/x-python");
199 g_assert (g_list_length (apps) == old_length + 2);
201 /* check the ordering */
202 app = g_list_nth_data (apps, 0);
203 g_assert (g_app_info_equal (info1, app));
205 /* check that Test1 is the first recommended app */
206 recomm = g_app_info_get_recommended_for_type ("text/x-python");
207 g_assert (recomm != NULL);
208 app = g_list_nth_data (recomm, 0);
209 g_assert (g_app_info_equal (info1, app));
211 /* and that Test2 is the first fallback */
212 fallback = g_app_info_get_fallback_for_type ("text/x-python");
213 g_assert (fallback != NULL);
214 app = g_list_nth_data (fallback, 0);
215 g_assert (g_app_info_equal (info2, app));
217 /* check that recomm + fallback = all applications */
218 list = g_list_concat (g_list_copy (recomm), g_list_copy (fallback));
219 g_assert (g_list_length (list) == g_list_length (apps));
221 for (l = list, m = apps; l != NULL && m != NULL; l = l->next, m = m->next)
223 g_assert (g_app_info_equal (l->data, m->data));
228 g_list_free_full (apps, g_object_unref);
229 g_list_free_full (recomm, g_object_unref);
230 g_list_free_full (fallback, g_object_unref);
232 g_app_info_reset_type_associations ("text/x-python");
233 g_app_info_reset_type_associations ("text/plain");
235 g_app_info_delete (info1);
236 g_app_info_delete (info2);
238 g_object_unref (info1);
239 g_object_unref (info2);
243 test_last_used (void)
246 GAppInfo *info1, *info2, *default_app;
247 GError *error = NULL;
249 info1 = create_app_info ("Test1");
250 info2 = create_app_info ("Test2");
252 g_app_info_set_as_default_for_type (info1, "application/x-test", &error);
253 g_assert (error == NULL);
255 g_app_info_add_supports_type (info2, "application/x-test", &error);
256 g_assert (error == NULL);
258 applications = g_app_info_get_recommended_for_type ("application/x-test");
259 g_assert (g_list_length (applications) == 2);
261 /* the first should be the default app now */
262 g_assert (g_app_info_equal (g_list_nth_data (applications, 0), info1));
263 g_assert (g_app_info_equal (g_list_nth_data (applications, 1), info2));
265 g_list_free_full (applications, g_object_unref);
267 g_app_info_set_as_last_used_for_type (info2, "application/x-test", &error);
268 g_assert (error == NULL);
270 applications = g_app_info_get_recommended_for_type ("application/x-test");
271 g_assert (g_list_length (applications) == 2);
273 default_app = g_app_info_get_default_for_type ("application/x-test", FALSE);
274 g_assert (g_app_info_equal (default_app, info1));
276 /* the first should be the other app now */
277 g_assert (g_app_info_equal (g_list_nth_data (applications, 0), info2));
278 g_assert (g_app_info_equal (g_list_nth_data (applications, 1), info1));
280 g_list_free_full (applications, g_object_unref);
282 g_app_info_reset_type_associations ("application/x-test");
284 g_app_info_delete (info1);
285 g_app_info_delete (info2);
287 g_object_unref (info1);
288 g_object_unref (info2);
289 g_object_unref (default_app);
293 cleanup_dir_recurse (GFile *parent, GFile *root)
297 GFileEnumerator *enumerator;
302 g_assert (root != NULL);
306 g_file_enumerate_children (parent, "*",
307 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL,
312 info = g_file_enumerator_next_file (enumerator, NULL, &error);
313 while ((info) && (!error))
315 descend = g_file_get_child (parent, g_file_info_get_name (info));
316 g_assert (descend != NULL);
317 relative_path = g_file_get_relative_path (root, descend);
318 g_assert (relative_path != NULL);
320 if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
321 cleanup_dir_recurse (descend, root);
324 res = g_file_delete (descend, NULL, &error);
325 g_assert_cmpint (res, ==, TRUE);
327 g_object_unref (descend);
329 info = g_file_enumerator_next_file (enumerator, NULL, &error);
331 g_assert (error == NULL);
334 res = g_file_enumerator_close (enumerator, NULL, &error);
335 g_assert_cmpint (res, ==, TRUE);
336 g_assert (error == NULL);
340 cleanup_subdirs (const char *base_dir)
344 base = g_file_new_for_path (base_dir);
345 file = g_file_get_child (base, "applications");
346 cleanup_dir_recurse (file, file);
347 g_object_unref (file);
348 file = g_file_get_child (base, "mime");
349 cleanup_dir_recurse (file, file);
350 g_object_unref (file);
360 g_test_init (&argc, &argv, NULL);
362 basedir = g_get_current_dir ();
363 g_setenv ("XDG_DATA_HOME", basedir, TRUE);
364 cleanup_subdirs (basedir);
366 g_test_add_func ("/desktop-app-info/delete", test_delete);
367 g_test_add_func ("/desktop-app-info/default", test_default);
368 g_test_add_func ("/desktop-app-info/fallback", test_fallback);
369 g_test_add_func ("/desktop-app-info/lastused", test_last_used);
371 result = g_test_run ();
373 cleanup_subdirs (basedir);