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>
32 create_app_info (const char *name
)
38 info
= g_app_info_create_from_commandline ("true blah",
40 G_APP_INFO_CREATE_NONE
,
42 g_assert (error
== NULL
);
44 /* this is necessary to ensure that the info is saved */
45 g_app_info_set_as_default_for_type (info
, "application/x-blah", &error
);
46 g_assert (error
== NULL
);
47 g_app_info_remove_supports_type (info
, "application/x-blah", &error
);
48 g_assert (error
== NULL
);
49 g_app_info_reset_type_associations ("application/x-blah");
63 info
= create_app_info ("Blah");
65 id
= g_app_info_get_id (info
);
66 g_assert (id
!= NULL
);
68 filename
= g_build_filename (basedir
, "applications", id
, NULL
);
70 res
= g_file_test (filename
, G_FILE_TEST_EXISTS
);
73 res
= g_app_info_can_delete (info
);
76 res
= g_app_info_delete (info
);
79 res
= g_file_test (filename
, G_FILE_TEST_EXISTS
);
82 g_object_unref (info
);
84 if (g_file_test ("/usr/share/applications/gedit.desktop", G_FILE_TEST_EXISTS
))
86 info
= (GAppInfo
*)g_desktop_app_info_new_from_filename ("/usr/share/applications/gedit.desktop");
89 res
= g_app_info_can_delete (info
);
92 res
= g_app_info_delete (info
);
100 GAppInfo
*info
, *info1
, *info2
, *info3
;
102 GError
*error
= NULL
;
104 info1
= create_app_info ("Blah1");
105 info2
= create_app_info ("Blah2");
106 info3
= create_app_info ("Blah3");
108 g_app_info_set_as_default_for_type (info1
, "application/x-test", &error
);
109 g_assert (error
== NULL
);
111 g_app_info_set_as_default_for_type (info2
, "application/x-test", &error
);
112 g_assert (error
== NULL
);
114 list
= g_app_info_get_all_for_type ("application/x-test");
115 g_assert (g_list_length (list
) == 2);
117 /* check that both are in the list, info2 before info1 */
118 info
= (GAppInfo
*)list
->data
;
119 g_assert (g_strcmp0 (g_app_info_get_id (info
), g_app_info_get_id (info2
)) == 0);
121 info
= (GAppInfo
*)list
->next
->data
;
122 g_assert (g_strcmp0 (g_app_info_get_id (info
), g_app_info_get_id (info1
)) == 0);
124 g_list_free_full (list
, g_object_unref
);
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_free_full (list
, g_object_unref
);
145 /* now remove info1 again */
146 g_app_info_remove_supports_type (info1
, "application/x-test", &error
);
147 g_assert (error
== NULL
);
149 list
= g_app_info_get_all_for_type ("application/x-test");
150 g_assert (g_list_length (list
) == 2);
152 /* check that both are in the list, info2 before info3 */
153 info
= (GAppInfo
*)list
->data
;
154 g_assert (g_strcmp0 (g_app_info_get_id (info
), g_app_info_get_id (info2
)) == 0);
156 info
= (GAppInfo
*)list
->next
->data
;
157 g_assert (g_strcmp0 (g_app_info_get_id (info
), g_app_info_get_id (info3
)) == 0);
159 g_list_free_full (list
, g_object_unref
);
161 /* now clean it all up */
162 g_app_info_reset_type_associations ("application/x-test");
164 list
= g_app_info_get_all_for_type ("application/x-test");
165 g_assert (list
== NULL
);
167 g_app_info_delete (info1
);
168 g_app_info_delete (info2
);
169 g_app_info_delete (info3
);
171 g_object_unref (info1
);
172 g_object_unref (info2
);
178 GAppInfo
*info1
, *info2
, *app
;
179 GList
*apps
, *recomm
, *fallback
, *list
, *l
, *m
;
180 GError
*error
= NULL
;
183 info1
= create_app_info ("Test1");
184 info2
= create_app_info ("Test2");
186 g_assert (g_content_type_is_a ("text/x-python", "text/plain"));
188 apps
= g_app_info_get_all_for_type ("text/x-python");
189 old_length
= g_list_length (apps
);
190 g_list_free_full (apps
, g_object_unref
);
192 g_app_info_set_as_default_for_type (info1
, "text/x-python", &error
);
193 g_assert (error
== NULL
);
195 g_app_info_set_as_default_for_type (info2
, "text/plain", &error
);
196 g_assert (error
== NULL
);
198 /* check that both apps are registered */
199 apps
= g_app_info_get_all_for_type ("text/x-python");
200 g_assert (g_list_length (apps
) == old_length
+ 2);
202 /* check the ordering */
203 app
= g_list_nth_data (apps
, 0);
204 g_assert (g_app_info_equal (info1
, app
));
206 /* check that Test1 is the first recommended app */
207 recomm
= g_app_info_get_recommended_for_type ("text/x-python");
208 g_assert (recomm
!= NULL
);
209 app
= g_list_nth_data (recomm
, 0);
210 g_assert (g_app_info_equal (info1
, app
));
212 /* and that Test2 is the first fallback */
213 fallback
= g_app_info_get_fallback_for_type ("text/x-python");
214 g_assert (fallback
!= NULL
);
215 app
= g_list_nth_data (fallback
, 0);
216 g_assert (g_app_info_equal (info2
, app
));
218 /* check that recomm + fallback = all applications */
219 list
= g_list_concat (g_list_copy (recomm
), g_list_copy (fallback
));
220 g_assert (g_list_length (list
) == g_list_length (apps
));
222 for (l
= list
, m
= apps
; l
!= NULL
&& m
!= NULL
; l
= l
->next
, m
= m
->next
)
224 g_assert (g_app_info_equal (l
->data
, m
->data
));
229 g_list_free_full (apps
, g_object_unref
);
230 g_list_free_full (recomm
, g_object_unref
);
231 g_list_free_full (fallback
, g_object_unref
);
233 g_app_info_reset_type_associations ("text/x-python");
234 g_app_info_reset_type_associations ("text/plain");
236 g_app_info_delete (info1
);
237 g_app_info_delete (info2
);
239 g_object_unref (info1
);
240 g_object_unref (info2
);
244 test_last_used (void)
247 GAppInfo
*info1
, *info2
, *default_app
;
248 GError
*error
= NULL
;
250 info1
= create_app_info ("Test1");
251 info2
= create_app_info ("Test2");
253 g_app_info_set_as_default_for_type (info1
, "application/x-test", &error
);
254 g_assert (error
== NULL
);
256 g_app_info_add_supports_type (info2
, "application/x-test", &error
);
257 g_assert (error
== NULL
);
259 applications
= g_app_info_get_recommended_for_type ("application/x-test");
260 g_assert (g_list_length (applications
) == 2);
262 /* the first should be the default app now */
263 g_assert (g_app_info_equal (g_list_nth_data (applications
, 0), info1
));
264 g_assert (g_app_info_equal (g_list_nth_data (applications
, 1), info2
));
266 g_list_free_full (applications
, g_object_unref
);
268 g_app_info_set_as_last_used_for_type (info2
, "application/x-test", &error
);
269 g_assert (error
== NULL
);
271 applications
= g_app_info_get_recommended_for_type ("application/x-test");
272 g_assert (g_list_length (applications
) == 2);
274 default_app
= g_app_info_get_default_for_type ("application/x-test", FALSE
);
275 g_assert (g_app_info_equal (default_app
, info1
));
277 /* the first should be the other app now */
278 g_assert (g_app_info_equal (g_list_nth_data (applications
, 0), info2
));
279 g_assert (g_app_info_equal (g_list_nth_data (applications
, 1), info1
));
281 g_list_free_full (applications
, g_object_unref
);
283 g_app_info_reset_type_associations ("application/x-test");
285 g_app_info_delete (info1
);
286 g_app_info_delete (info2
);
288 g_object_unref (info1
);
289 g_object_unref (info2
);
290 g_object_unref (default_app
);
294 cleanup_dir_recurse (GFile
*parent
, GFile
*root
)
298 GFileEnumerator
*enumerator
;
303 g_assert (root
!= NULL
);
307 g_file_enumerate_children (parent
, "*",
308 G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS
, NULL
,
313 info
= g_file_enumerator_next_file (enumerator
, NULL
, &error
);
314 while ((info
) && (!error
))
316 descend
= g_file_get_child (parent
, g_file_info_get_name (info
));
317 g_assert (descend
!= NULL
);
318 relative_path
= g_file_get_relative_path (root
, descend
);
319 g_assert (relative_path
!= NULL
);
321 if (g_file_info_get_file_type (info
) == G_FILE_TYPE_DIRECTORY
)
322 cleanup_dir_recurse (descend
, root
);
325 res
= g_file_delete (descend
, NULL
, &error
);
326 g_assert_cmpint (res
, ==, TRUE
);
328 g_object_unref (descend
);
330 info
= g_file_enumerator_next_file (enumerator
, NULL
, &error
);
332 g_assert (error
== NULL
);
335 res
= g_file_enumerator_close (enumerator
, NULL
, &error
);
336 g_assert_cmpint (res
, ==, TRUE
);
337 g_assert (error
== NULL
);
341 cleanup_subdirs (const char *base_dir
)
345 base
= g_file_new_for_path (base_dir
);
346 file
= g_file_get_child (base
, "applications");
347 cleanup_dir_recurse (file
, file
);
348 g_object_unref (file
);
349 file
= g_file_get_child (base
, "mime");
350 cleanup_dir_recurse (file
, file
);
351 g_object_unref (file
);
355 test_extra_getters (void)
357 GDesktopAppInfo
*appinfo
;
361 appinfo
= g_desktop_app_info_new_from_filename (g_test_get_filename (G_TEST_DIST
, "appinfo-test.desktop", NULL
));
362 g_assert (appinfo
!= NULL
);
364 g_assert (g_desktop_app_info_has_key (appinfo
, "Terminal"));
365 g_assert (!g_desktop_app_info_has_key (appinfo
, "Bratwurst"));
367 s
= g_desktop_app_info_get_string (appinfo
, "StartupWMClass");
368 g_assert_cmpstr (s
, ==, "appinfo-class");
371 b
= g_desktop_app_info_get_boolean (appinfo
, "Terminal");
374 g_object_unref (appinfo
);
378 wait_for_file (const gchar
*want_this
,
379 const gchar
*but_not_this
,
380 const gchar
*or_this
)
384 /* I hate time-based conditions in tests, but this will wait up to one
385 * whole minute for "touch file" to finish running. I think it should
388 * 600 * 100ms = 60 seconds.
390 while (access (want_this
, F_OK
) != 0)
392 g_usleep (100000); /* 100ms */
397 g_assert (access (but_not_this
, F_OK
) != 0);
398 g_assert (access (or_this
, F_OK
) != 0);
401 unlink (but_not_this
);
408 const gchar
* const *actions
;
409 GDesktopAppInfo
*appinfo
;
412 appinfo
= g_desktop_app_info_new_from_filename (g_test_get_filename (G_TEST_DIST
, "appinfo-test-actions.desktop", NULL
));
413 g_assert (appinfo
!= NULL
);
415 actions
= g_desktop_app_info_list_actions (appinfo
);
416 g_assert_cmpstr (actions
[0], ==, "frob");
417 g_assert_cmpstr (actions
[1], ==, "tweak");
418 g_assert_cmpstr (actions
[2], ==, "twiddle");
419 g_assert_cmpstr (actions
[3], ==, "broken");
420 g_assert_cmpstr (actions
[4], ==, NULL
);
422 name
= g_desktop_app_info_get_action_name (appinfo
, "frob");
423 g_assert_cmpstr (name
, ==, "Frobnicate");
426 name
= g_desktop_app_info_get_action_name (appinfo
, "tweak");
427 g_assert_cmpstr (name
, ==, "Tweak");
430 name
= g_desktop_app_info_get_action_name (appinfo
, "twiddle");
431 g_assert_cmpstr (name
, ==, "Twiddle");
434 name
= g_desktop_app_info_get_action_name (appinfo
, "broken");
435 g_assert (name
!= NULL
);
436 g_assert (g_utf8_validate (name
, -1, NULL
));
439 unlink ("frob"); unlink ("tweak"); unlink ("twiddle");
441 g_desktop_app_info_launch_action (appinfo
, "frob", NULL
);
442 wait_for_file ("frob", "tweak", "twiddle");
444 g_desktop_app_info_launch_action (appinfo
, "tweak", NULL
);
445 wait_for_file ("tweak", "frob", "twiddle");
447 g_desktop_app_info_launch_action (appinfo
, "twiddle", NULL
);
448 wait_for_file ("twiddle", "frob", "tweak");
450 g_object_unref (appinfo
);
459 g_test_init (&argc
, &argv
, NULL
);
461 basedir
= g_get_current_dir ();
462 g_setenv ("XDG_DATA_HOME", basedir
, TRUE
);
463 cleanup_subdirs (basedir
);
465 g_test_add_func ("/desktop-app-info/delete", test_delete
);
466 g_test_add_func ("/desktop-app-info/default", test_default
);
467 g_test_add_func ("/desktop-app-info/fallback", test_fallback
);
468 g_test_add_func ("/desktop-app-info/lastused", test_last_used
);
469 g_test_add_func ("/desktop-app-info/extra-getters", test_extra_getters
);
470 g_test_add_func ("/desktop-app-info/actions", test_actions
);
472 result
= g_test_run ();
474 cleanup_subdirs (basedir
);