62ccd03a792c373b58b42711d4b68072108c5cf4
[platform/upstream/glib.git] / gio / tests / desktop-app-info.c
1 /* 
2  * Copyright (C) 2008 Red Hat, Inc
3  *
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.
8  *
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.
13  *
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.
18  * 
19  * Author: Matthias Clasen
20  */
21
22 #include <glib/glib.h>
23 #include <gio/gio.h>
24 #include <gio/gdesktopappinfo.h>
25 #include <stdlib.h>
26 #include <string.h>
27
28 static char *basedir;
29
30 static GAppInfo * 
31 create_app_info (const char *name)
32 {
33   GError *error;
34   GAppInfo *info;
35
36   error = NULL;
37   info = g_app_info_create_from_commandline ("/usr/bin/true blah",
38                                              name,
39                                              G_APP_INFO_CREATE_NONE,
40                                              &error);
41   g_assert (error == NULL);
42
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");
49   
50   return info;
51 }
52
53 static void
54 test_delete (void)
55 {
56   GAppInfo *info;
57
58   const char *id;
59   char *filename;
60   gboolean res;
61
62   info = create_app_info ("Blah");
63  
64   id = g_app_info_get_id (info);
65   g_assert (id != NULL);
66
67   filename = g_build_filename (basedir, "applications", id, NULL);
68
69   res = g_file_test (filename, G_FILE_TEST_EXISTS);
70   g_assert (res);
71
72   res = g_app_info_can_delete (info);
73   g_assert (res);
74
75   res = g_app_info_delete (info);
76   g_assert (res);
77
78   res = g_file_test (filename, G_FILE_TEST_EXISTS);
79   g_assert (!res);
80
81   g_object_unref (info);
82
83   if (g_file_test ("/usr/share/applications/gedit.desktop", G_FILE_TEST_EXISTS))
84     {
85       info = (GAppInfo*)g_desktop_app_info_new ("gedit.desktop");
86       g_assert (info);
87      
88       res = g_app_info_can_delete (info);
89       g_assert (!res);
90  
91       res = g_app_info_delete (info);
92       g_assert (!res);
93     }
94 }
95
96 static void
97 test_default (void)
98 {
99   GAppInfo *info, *info1, *info2, *info3;
100   GList *list;
101   GError *error = NULL;  
102
103   info1 = create_app_info ("Blah1");
104   info2 = create_app_info ("Blah2");
105   info3 = create_app_info ("Blah3");
106
107   g_app_info_set_as_default_for_type (info1, "application/x-test", &error);
108   g_assert (error == NULL);
109
110   g_app_info_set_as_default_for_type (info2, "application/x-test", &error);
111   g_assert (error == NULL);
112
113   list = g_app_info_get_all_for_type ("application/x-test");
114   g_assert (g_list_length (list) == 2);
115   
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);
119
120   info = (GAppInfo *)list->next->data;
121   g_assert (g_strcmp0 (g_app_info_get_id (info), g_app_info_get_id (info1)) == 0);
122
123   g_list_foreach (list, (GFunc)g_object_unref, NULL);
124   g_list_free (list);
125
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);
129
130   list = g_app_info_get_all_for_type ("application/x-test");
131   g_assert (g_list_length (list) == 3);
132   
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);
136
137   info = (GAppInfo *)list->next->data;
138   g_assert (g_strcmp0 (g_app_info_get_id (info), g_app_info_get_id (info1)) == 0);
139
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);
142
143   g_list_foreach (list, (GFunc)g_object_unref, NULL);
144   g_list_free (list);
145
146   /* now remove info1 again */
147   g_app_info_remove_supports_type (info1, "application/x-test", &error);
148   g_assert (error == NULL);
149
150   list = g_app_info_get_all_for_type ("application/x-test");
151   g_assert (g_list_length (list) == 2);
152   
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);
156
157   info = (GAppInfo *)list->next->data;
158   g_assert (g_strcmp0 (g_app_info_get_id (info), g_app_info_get_id (info3)) == 0);
159
160   g_list_foreach (list, (GFunc)g_object_unref, NULL);
161   g_list_free (list);
162
163   /* now clean it all up */
164   g_app_info_reset_type_associations ("application/x-test");
165
166   list = g_app_info_get_all_for_type ("application/x-test");
167   g_assert (list == NULL);
168   
169   g_app_info_delete (info1);
170   g_app_info_delete (info2);
171   g_app_info_delete (info3);
172
173   g_object_unref (info1);
174   g_object_unref (info2);
175 }
176
177 static void
178 cleanup_dir_recurse (GFile *parent, GFile *root)
179 {
180   gboolean res;
181   GError *error;
182   GFileEnumerator *enumerator;
183   GFileInfo *info;
184   GFile *descend;
185   char *relative_path;
186
187   g_assert (root != NULL);
188
189   error = NULL;
190   enumerator =
191     g_file_enumerate_children (parent, "*",
192                                G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL,
193                                &error);
194   if (! enumerator)
195           return;
196   error = NULL;
197   info = g_file_enumerator_next_file (enumerator, NULL, &error);
198   while ((info) && (!error))
199     {
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);
204
205       if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
206           cleanup_dir_recurse (descend, root);
207
208       error = NULL;
209       res = g_file_delete (descend, NULL, &error);
210       g_assert_cmpint (res, ==, TRUE);
211
212       g_object_unref (descend);
213       error = NULL;
214       info = g_file_enumerator_next_file (enumerator, NULL, &error);
215     }
216   g_assert (error == NULL);
217
218   error = NULL;
219   res = g_file_enumerator_close (enumerator, NULL, &error);
220   g_assert_cmpint (res, ==, TRUE);
221   g_assert (error == NULL);
222 }
223
224 static void
225 cleanup_subdirs (const char *base_dir)
226 {
227   GFile *base, *file;
228  
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);
236 }
237
238 int
239 main (int   argc,
240       char *argv[])
241 {
242   gint result;
243
244   g_type_init ();
245   g_test_init (&argc, &argv, NULL);
246   
247   basedir = g_get_current_dir ();
248   g_setenv ("XDG_DATA_HOME", basedir, TRUE);
249   cleanup_subdirs (basedir);
250   
251   g_test_add_func ("/desktop-app-info/delete", test_delete);
252   g_test_add_func ("/desktop-app-info/default", test_default);
253
254   result = g_test_run ();
255
256   cleanup_subdirs (basedir);
257
258   return result;
259 }