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 ("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);
178 cleanup_dir_recurse (GFile *parent, GFile *root)
182 GFileEnumerator *enumerator;
187 g_assert (root != NULL);
191 g_file_enumerate_children (parent, "*",
192 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL,
197 info = g_file_enumerator_next_file (enumerator, NULL, &error);
198 while ((info) && (!error))
200 descend = g_file_get_child (parent, g_file_info_get_name (info));
201 g_assert (descend != NULL);
202 relative_path = g_file_get_relative_path (root, descend);
203 g_assert (relative_path != NULL);
205 if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
206 cleanup_dir_recurse (descend, root);
209 res = g_file_delete (descend, NULL, &error);
210 g_assert_cmpint (res, ==, TRUE);
212 g_object_unref (descend);
214 info = g_file_enumerator_next_file (enumerator, NULL, &error);
216 g_assert (error == NULL);
219 res = g_file_enumerator_close (enumerator, NULL, &error);
220 g_assert_cmpint (res, ==, TRUE);
221 g_assert (error == NULL);
225 cleanup_subdirs (const char *base_dir)
229 base = g_file_new_for_path (base_dir);
230 file = g_file_get_child (base, "applications");
231 cleanup_dir_recurse (file, file);
232 g_object_unref (file);
233 file = g_file_get_child (base, "mime");
234 cleanup_dir_recurse (file, file);
235 g_object_unref (file);
245 g_test_init (&argc, &argv, NULL);
247 basedir = g_get_current_dir ();
248 g_setenv ("XDG_DATA_HOME", basedir, TRUE);
249 cleanup_subdirs (basedir);
251 g_test_add_func ("/desktop-app-info/delete", test_delete);
252 g_test_add_func ("/desktop-app-info/default", test_default);
254 result = g_test_run ();
256 cleanup_subdirs (basedir);