Tizen 2.1 base
[platform/upstream/glib2.0.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_from_filename ("/usr/share/applications/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_free_full (list, g_object_unref);
124
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);
128
129   list = g_app_info_get_all_for_type ("application/x-test");
130   g_assert (g_list_length (list) == 3);
131   
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);
135
136   info = (GAppInfo *)list->next->data;
137   g_assert (g_strcmp0 (g_app_info_get_id (info), g_app_info_get_id (info1)) == 0);
138
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);
141
142   g_list_free_full (list, g_object_unref);
143
144   /* now remove info1 again */
145   g_app_info_remove_supports_type (info1, "application/x-test", &error);
146   g_assert (error == NULL);
147
148   list = g_app_info_get_all_for_type ("application/x-test");
149   g_assert (g_list_length (list) == 2);
150   
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);
154
155   info = (GAppInfo *)list->next->data;
156   g_assert (g_strcmp0 (g_app_info_get_id (info), g_app_info_get_id (info3)) == 0);
157
158   g_list_free_full (list, g_object_unref);
159
160   /* now clean it all up */
161   g_app_info_reset_type_associations ("application/x-test");
162
163   list = g_app_info_get_all_for_type ("application/x-test");
164   g_assert (list == NULL);
165   
166   g_app_info_delete (info1);
167   g_app_info_delete (info2);
168   g_app_info_delete (info3);
169
170   g_object_unref (info1);
171   g_object_unref (info2);
172 }
173
174 static void
175 test_fallback (void)
176 {
177   GAppInfo *info1, *info2, *app;
178   GList *apps, *recomm, *fallback, *list, *l, *m;
179   GError *error = NULL;
180   gint old_length;
181
182   info1 = create_app_info ("Test1");
183   info2 = create_app_info ("Test2");
184
185   g_assert (g_content_type_is_a ("text/x-python", "text/plain"));
186
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);
190
191   g_app_info_set_as_default_for_type (info1, "text/x-python", &error);
192   g_assert (error == NULL);
193
194   g_app_info_set_as_default_for_type (info2, "text/plain", &error);
195   g_assert (error == NULL);
196
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);
200
201   /* check the ordering */
202   app = g_list_nth_data (apps, 0);
203   g_assert (g_app_info_equal (info1, app));
204
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));
210
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));
216
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));
220
221   for (l = list, m = apps; l != NULL && m != NULL; l = l->next, m = m->next)
222     {
223       g_assert (g_app_info_equal (l->data, m->data));
224     }
225
226   g_list_free (list);
227
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);
231
232   g_app_info_reset_type_associations ("text/x-python");
233   g_app_info_reset_type_associations ("text/plain");
234
235   g_app_info_delete (info1);
236   g_app_info_delete (info2);
237
238   g_object_unref (info1);
239   g_object_unref (info2);
240 }
241
242 static void
243 test_last_used (void)
244 {
245   GList *applications;
246   GAppInfo *info1, *info2, *default_app;
247   GError *error = NULL;
248
249   info1 = create_app_info ("Test1");
250   info2 = create_app_info ("Test2");
251
252   g_app_info_set_as_default_for_type (info1, "application/x-test", &error);
253   g_assert (error == NULL);
254
255   g_app_info_add_supports_type (info2, "application/x-test", &error);
256   g_assert (error == NULL);
257
258   applications = g_app_info_get_recommended_for_type ("application/x-test");
259   g_assert (g_list_length (applications) == 2);
260
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));
264
265   g_list_free_full (applications, g_object_unref);
266
267   g_app_info_set_as_last_used_for_type (info2, "application/x-test", &error);
268   g_assert (error == NULL);
269
270   applications = g_app_info_get_recommended_for_type ("application/x-test");
271   g_assert (g_list_length (applications) == 2);
272
273   default_app = g_app_info_get_default_for_type ("application/x-test", FALSE);
274   g_assert (g_app_info_equal (default_app, info1));
275
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));
279
280   g_list_free_full (applications, g_object_unref);
281
282   g_app_info_reset_type_associations ("application/x-test");
283
284   g_app_info_delete (info1);
285   g_app_info_delete (info2);
286
287   g_object_unref (info1);
288   g_object_unref (info2);
289   g_object_unref (default_app);
290 }
291
292 static void
293 cleanup_dir_recurse (GFile *parent, GFile *root)
294 {
295   gboolean res;
296   GError *error;
297   GFileEnumerator *enumerator;
298   GFileInfo *info;
299   GFile *descend;
300   char *relative_path;
301
302   g_assert (root != NULL);
303
304   error = NULL;
305   enumerator =
306     g_file_enumerate_children (parent, "*",
307                                G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, NULL,
308                                &error);
309   if (! enumerator)
310           return;
311   error = NULL;
312   info = g_file_enumerator_next_file (enumerator, NULL, &error);
313   while ((info) && (!error))
314     {
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);
319
320       if (g_file_info_get_file_type (info) == G_FILE_TYPE_DIRECTORY)
321           cleanup_dir_recurse (descend, root);
322
323       error = NULL;
324       res = g_file_delete (descend, NULL, &error);
325       g_assert_cmpint (res, ==, TRUE);
326
327       g_object_unref (descend);
328       error = NULL;
329       info = g_file_enumerator_next_file (enumerator, NULL, &error);
330     }
331   g_assert (error == NULL);
332
333   error = NULL;
334   res = g_file_enumerator_close (enumerator, NULL, &error);
335   g_assert_cmpint (res, ==, TRUE);
336   g_assert (error == NULL);
337 }
338
339 static void
340 cleanup_subdirs (const char *base_dir)
341 {
342   GFile *base, *file;
343  
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);
351 }
352
353 int
354 main (int   argc,
355       char *argv[])
356 {
357   gint result;
358
359   g_type_init ();
360   g_test_init (&argc, &argv, NULL);
361   
362   basedir = g_get_current_dir ();
363   g_setenv ("XDG_DATA_HOME", basedir, TRUE);
364   cleanup_subdirs (basedir);
365   
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);
370
371   result = g_test_run ();
372
373   cleanup_subdirs (basedir);
374
375   return result;
376 }