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_foreach (list, (GFunc)g_object_unref, NULL);
126 /* now try adding something at the end */
127 g_app_info_add_supports_type (info3, "application/x-test", &error);
128 g_assert (error == NULL);
130 list = g_app_info_get_all_for_type ("application/x-test");
131 g_assert (g_list_length (list) == 3);
133 /* check that all are in the list, info2, info1, info3 */
134 info = (GAppInfo *)list->data;
135 g_assert (g_strcmp0 (g_app_info_get_id (info), g_app_info_get_id (info2)) == 0);
137 info = (GAppInfo *)list->next->data;
138 g_assert (g_strcmp0 (g_app_info_get_id (info), g_app_info_get_id (info1)) == 0);
140 info = (GAppInfo *)list->next->next->data;
141 g_assert (g_strcmp0 (g_app_info_get_id (info), g_app_info_get_id (info3)) == 0);
143 g_list_foreach (list, (GFunc)g_object_unref, NULL);
146 /* now remove info1 again */
147 g_app_info_remove_supports_type (info1, "application/x-test", &error);
148 g_assert (error == NULL);
150 list = g_app_info_get_all_for_type ("application/x-test");
151 g_assert (g_list_length (list) == 2);
153 /* check that both are in the list, info2 before info3 */
154 info = (GAppInfo *)list->data;
155 g_assert (g_strcmp0 (g_app_info_get_id (info), g_app_info_get_id (info2)) == 0);
157 info = (GAppInfo *)list->next->data;
158 g_assert (g_strcmp0 (g_app_info_get_id (info), g_app_info_get_id (info3)) == 0);
160 g_list_foreach (list, (GFunc)g_object_unref, NULL);
163 /* now clean it all up */
164 g_app_info_reset_type_associations ("application/x-test");
166 list = g_app_info_get_all_for_type ("application/x-test");
167 g_assert (list == NULL);
169 g_app_info_delete (info1);
170 g_app_info_delete (info2);
171 g_app_info_delete (info3);
173 g_object_unref (info1);
174 g_object_unref (info2);
180 GAppInfo *info1, *info2, *app;
181 GList *apps, *recomm, *fallback, *list, *l, *m;
182 GError *error = NULL;
185 info1 = create_app_info ("Test1");
186 info2 = create_app_info ("Test2");
188 g_assert (g_content_type_is_a ("text/x-python", "text/plain"));
190 apps = g_app_info_get_all_for_type ("text/x-python");
191 old_length = g_list_length (apps);
192 g_list_free_full (apps, g_object_unref);
194 g_app_info_set_as_default_for_type (info1, "text/x-python", &error);
195 g_assert (error == NULL);
197 g_app_info_set_as_default_for_type (info2, "text/plain", &error);
198 g_assert (error == NULL);
200 /* check that both apps are registered */
201 apps = g_app_info_get_all_for_type ("text/x-python");
202 g_assert (g_list_length (apps) == old_length + 2);
204 /* check the ordering */
205 app = g_list_nth_data (apps, 0);
206 g_assert (g_app_info_equal (info1, app));
208 /* check that Test1 is the first recommended app */
209 recomm = g_app_info_get_recommended_for_type ("text/x-python");
210 g_assert (recomm != NULL);
211 app = g_list_nth_data (recomm, 0);
212 g_assert (g_app_info_equal (info1, app));
214 /* and that Test2 is the first fallback */
215 fallback = g_app_info_get_fallback_for_type ("text/x-python");
216 g_assert (fallback != NULL);
217 app = g_list_nth_data (fallback, 0);
218 g_assert (g_app_info_equal (info2, app));
220 /* check that recomm + fallback = all applications */
221 list = g_list_concat (g_list_copy (recomm), g_list_copy (fallback));
222 g_assert (g_list_length (list) == g_list_length (apps));
224 for (l = list, m = apps; l != NULL && m != NULL; l = l->next, m = m->next)
226 g_assert (g_app_info_equal (l->data, m->data));
231 g_list_free_full (apps, g_object_unref);
232 g_list_free_full (recomm, g_object_unref);
233 g_list_free_full (fallback, g_object_unref);
235 g_app_info_reset_type_associations ("text/x-python");
236 g_app_info_reset_type_associations ("text/plain");
238 g_app_info_delete (info1);
239 g_app_info_delete (info2);
241 g_object_unref (info1);
242 g_object_unref (info2);
246 test_last_used (void)
249 GAppInfo *info1, *info2, *default_app;
250 GError *error = NULL;
252 info1 = create_app_info ("Test1");
253 info2 = create_app_info ("Test2");
255 g_app_info_set_as_default_for_type (info1, "application/x-test", &error);
256 g_assert (error == NULL);
258 g_app_info_add_supports_type (info2, "application/x-test", &error);
259 g_assert (error == NULL);
261 applications = g_app_info_get_recommended_for_type ("application/x-test");
262 g_assert (g_list_length (applications) == 2);
264 /* the first should be the default app now */
265 g_assert (g_app_info_equal (g_list_nth_data (applications, 0), info1));
266 g_assert (g_app_info_equal (g_list_nth_data (applications, 1), info2));
268 g_list_free_full (applications, g_object_unref);
270 g_app_info_set_as_last_used_for_type (info2, "application/x-test", &error);
271 g_assert (error == NULL);
273 applications = g_app_info_get_recommended_for_type ("application/x-test");
274 g_assert (g_list_length (applications) == 2);
276 default_app = g_app_info_get_default_for_type ("application/x-test", FALSE);
277 g_assert (g_app_info_equal (default_app, info1));
279 /* the first should be the other app now */
280 g_assert (g_app_info_equal (g_list_nth_data (applications, 0), info2));
281 g_assert (g_app_info_equal (g_list_nth_data (applications, 1), info1));
283 g_list_free_full (applications, g_object_unref);
285 g_app_info_reset_type_associations ("application/x-test");
287 g_app_info_delete (info1);
288 g_app_info_delete (info2);
290 g_object_unref (info1);
291 g_object_unref (info2);
292 g_object_unref (default_app);
296 cleanup_dir_recurse (GFile *parent, GFile *root)
300 GFileEnumerator *enumerator;
305 g_assert (root != NULL);
309 g_file_enumerate_children (parent, "*",
310 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL,
315 info = g_file_enumerator_next_file (enumerator, NULL, &error);
316 while ((info) && (!error))
318 descend = g_file_get_child (parent, g_file_info_get_name (info));
319 g_assert (descend != NULL);
320 relative_path = g_file_get_relative_path (root, descend);
321 g_assert (relative_path != NULL);
323 if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
324 cleanup_dir_recurse (descend, root);
327 res = g_file_delete (descend, NULL, &error);
328 g_assert_cmpint (res, ==, TRUE);
330 g_object_unref (descend);
332 info = g_file_enumerator_next_file (enumerator, NULL, &error);
334 g_assert (error == NULL);
337 res = g_file_enumerator_close (enumerator, NULL, &error);
338 g_assert_cmpint (res, ==, TRUE);
339 g_assert (error == NULL);
343 cleanup_subdirs (const char *base_dir)
347 base = g_file_new_for_path (base_dir);
348 file = g_file_get_child (base, "applications");
349 cleanup_dir_recurse (file, file);
350 g_object_unref (file);
351 file = g_file_get_child (base, "mime");
352 cleanup_dir_recurse (file, file);
353 g_object_unref (file);
363 g_test_init (&argc, &argv, NULL);
365 basedir = g_get_current_dir ();
366 g_setenv ("XDG_DATA_HOME", basedir, TRUE);
367 cleanup_subdirs (basedir);
369 g_test_add_func ("/desktop-app-info/delete", test_delete);
370 g_test_add_func ("/desktop-app-info/default", test_default);
371 g_test_add_func ("/desktop-app-info/fallback", test_fallback);
372 g_test_add_func ("/desktop-app-info/lastused", test_last_used);
374 result = g_test_run ();
376 cleanup_subdirs (basedir);