Branch data Line data Source code
1 : : /*
2 : : * Copyright (C) 2008 Red Hat, Inc
3 : : *
4 : : * SPDX-License-Identifier: LGPL-2.1-or-later
5 : : *
6 : : * This library is free software; you can redistribute it and/or
7 : : * modify it under the terms of the GNU Lesser General Public
8 : : * License as published by the Free Software Foundation; either
9 : : * version 2.1 of the License, or (at your option) any later version.
10 : : *
11 : : * This library is distributed in the hope that it will be useful,
12 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 : : * Lesser General Public License for more details.
15 : : *
16 : : * You should have received a copy of the GNU Lesser General
17 : : * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 : : *
19 : : * Author: Matthias Clasen
20 : : */
21 : :
22 : : #include <locale.h>
23 : :
24 : : #include <glib/glib.h>
25 : : #include <glib/gstdio.h>
26 : : #include <gio/gio.h>
27 : : #include <gio/gdesktopappinfo.h>
28 : : #include <gio/gunixinputstream.h>
29 : : #include <glib-unix.h>
30 : : #include <stdlib.h>
31 : : #include <string.h>
32 : : #include <unistd.h>
33 : : #include <sys/types.h>
34 : : #include <sys/stat.h>
35 : :
36 : 51 : G_DECLARE_FINAL_TYPE (TestLaunchContext, test_launch_context, TEST,
37 : : LAUNCH_CONTEXT, GAppLaunchContext);
38 : :
39 : : struct _TestLaunchContext {
40 : : GAppLaunchContext parent;
41 : :
42 : : char *overriden_startup_notify_id;
43 : : };
44 : :
45 : : struct _TestLaunchContextClass {
46 : : GAppLaunchContextClass parent;
47 : : };
48 : :
49 : 92 : G_DEFINE_FINAL_TYPE (TestLaunchContext, test_launch_context,
50 : : G_TYPE_APP_LAUNCH_CONTEXT);
51 : :
52 : : static void
53 : 39 : test_launch_context_init (TestLaunchContext *test_context)
54 : : {
55 : 39 : }
56 : :
57 : : static char *
58 : 8 : test_launch_context_get_startup_notify_id (GAppLaunchContext *context,
59 : : GAppInfo *app_info,
60 : : GList *files)
61 : : {
62 : 8 : TestLaunchContext *test_context = TEST_LAUNCH_CONTEXT (context);
63 : :
64 : 8 : if (test_context->overriden_startup_notify_id)
65 : 0 : return g_strdup (test_context->overriden_startup_notify_id);
66 : :
67 : 8 : if (g_app_info_get_id (app_info))
68 : 12 : return g_strdup (g_app_info_get_id (app_info));
69 : :
70 : 2 : if (g_app_info_get_display_name (app_info))
71 : 4 : return g_strdup (g_app_info_get_display_name (app_info));
72 : :
73 : 0 : return g_strdup (g_app_info_get_commandline (app_info));
74 : : }
75 : :
76 : : static void
77 : 39 : test_launch_context_get_startup_notify_dispose (GObject *object)
78 : : {
79 : 39 : TestLaunchContext *test_context = TEST_LAUNCH_CONTEXT (object);
80 : :
81 : 39 : g_clear_pointer (&test_context->overriden_startup_notify_id, g_free);
82 : 39 : G_OBJECT_CLASS (test_launch_context_parent_class)->dispose (object);
83 : 39 : }
84 : :
85 : : static void
86 : 1 : test_launch_context_class_init (TestLaunchContextClass *klass)
87 : : {
88 : 1 : G_APP_LAUNCH_CONTEXT_CLASS (klass)->get_startup_notify_id = test_launch_context_get_startup_notify_id;
89 : 1 : G_OBJECT_CLASS (klass)->dispose = test_launch_context_get_startup_notify_dispose;
90 : 1 : }
91 : :
92 : : static GAppInfo *
93 : 13 : create_command_line_app_info (const char *name,
94 : : const char *command_line,
95 : : const char *default_for_type)
96 : : {
97 : : GAppInfo *info;
98 : 13 : GError *error = NULL;
99 : :
100 : 13 : info = g_app_info_create_from_commandline (command_line,
101 : : name,
102 : : G_APP_INFO_CREATE_NONE,
103 : : &error);
104 : 13 : g_assert_no_error (error);
105 : :
106 : 13 : g_app_info_set_as_default_for_type (info, default_for_type, &error);
107 : 13 : g_assert_no_error (error);
108 : :
109 : 13 : return g_steal_pointer (&info);
110 : : }
111 : :
112 : : static GAppInfo *
113 : 11 : create_app_info (const char *name)
114 : : {
115 : 11 : GError *error = NULL;
116 : : GAppInfo *info;
117 : :
118 : 11 : info = create_command_line_app_info (name, "true blah", "application/x-blah");
119 : :
120 : : /* this is necessary to ensure that the info is saved */
121 : 11 : g_app_info_remove_supports_type (info, "application/x-blah", &error);
122 : 11 : g_assert_no_error (error);
123 : 11 : g_app_info_reset_type_associations ("application/x-blah");
124 : :
125 : 11 : return info;
126 : : }
127 : :
128 : : static gboolean
129 : 7 : skip_missing_update_desktop_database (void)
130 : : {
131 : 7 : gchar *path = g_find_program_in_path ("update-desktop-database");
132 : :
133 : 7 : if (path == NULL)
134 : : {
135 : 0 : g_test_skip ("update-desktop-database is required to run this test");
136 : 0 : return TRUE;
137 : : }
138 : 7 : g_free (path);
139 : 7 : return FALSE;
140 : : }
141 : :
142 : : static void
143 : 1 : test_delete (void)
144 : : {
145 : : GAppInfo *info;
146 : :
147 : : const char *id;
148 : : char *filename;
149 : : gboolean res;
150 : :
151 : 1 : if (skip_missing_update_desktop_database ())
152 : 0 : return;
153 : :
154 : 1 : info = create_app_info ("Blah");
155 : :
156 : 1 : id = g_app_info_get_id (info);
157 : 1 : g_assert_nonnull (id);
158 : :
159 : 1 : filename = g_build_filename (g_get_user_data_dir (), "applications", id, NULL);
160 : :
161 : 1 : res = g_file_test (filename, G_FILE_TEST_EXISTS);
162 : 1 : g_assert_true (res);
163 : :
164 : 1 : res = g_app_info_can_delete (info);
165 : 1 : g_assert_true (res);
166 : :
167 : 1 : res = g_app_info_delete (info);
168 : 1 : g_assert_true (res);
169 : :
170 : 1 : res = g_file_test (filename, G_FILE_TEST_EXISTS);
171 : 1 : g_assert_false (res);
172 : :
173 : 1 : g_object_unref (info);
174 : :
175 : 1 : if (g_file_test ("/usr/share/applications/gedit.desktop", G_FILE_TEST_EXISTS))
176 : : {
177 : 0 : info = (GAppInfo*)g_desktop_app_info_new_from_filename ("/usr/share/applications/gedit.desktop");
178 : 0 : g_assert_nonnull (info);
179 : :
180 : 0 : res = g_app_info_can_delete (info);
181 : 0 : g_assert_false (res);
182 : :
183 : 0 : res = g_app_info_delete (info);
184 : 0 : g_assert_false (res);
185 : : }
186 : :
187 : 1 : g_free (filename);
188 : : }
189 : :
190 : : static void
191 : 1 : test_default (void)
192 : : {
193 : : GAppInfo *info, *info1, *info2, *info3;
194 : : GList *list;
195 : 1 : GError *error = NULL;
196 : :
197 : 1 : if (skip_missing_update_desktop_database ())
198 : 0 : return;
199 : :
200 : 1 : info1 = create_app_info ("Blah1");
201 : 1 : info2 = create_app_info ("Blah2");
202 : 1 : info3 = create_app_info ("Blah3");
203 : :
204 : 1 : g_app_info_set_as_default_for_type (info1, "application/x-test", &error);
205 : 1 : g_assert_no_error (error);
206 : :
207 : 1 : g_app_info_set_as_default_for_type (info2, "application/x-test", &error);
208 : 1 : g_assert_no_error (error);
209 : :
210 : 1 : info = g_app_info_get_default_for_type ("application/x-test", FALSE);
211 : 1 : g_assert_nonnull (info);
212 : 1 : g_assert_cmpstr (g_app_info_get_id (info), ==, g_app_info_get_id (info2));
213 : 1 : g_object_unref (info);
214 : :
215 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
216 : : "*assertion*uri_scheme*failed*");
217 : 1 : g_assert_null (g_app_info_get_default_for_uri_scheme (NULL));
218 : 1 : g_test_assert_expected_messages ();
219 : :
220 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
221 : : "*assertion*uri_scheme*failed*");
222 : 1 : g_assert_null (g_app_info_get_default_for_uri_scheme (""));
223 : 1 : g_test_assert_expected_messages ();
224 : :
225 : 1 : g_app_info_set_as_default_for_type (info3, "x-scheme-handler/glib", &error);
226 : 1 : g_assert_no_error (error);
227 : 1 : info = g_app_info_get_default_for_uri_scheme ("glib");
228 : 1 : g_assert_nonnull (info);
229 : 1 : g_assert_true (g_app_info_equal (info, info3));
230 : 1 : g_object_unref (info);
231 : :
232 : : /* now try adding something, but not setting as default */
233 : 1 : g_app_info_add_supports_type (info3, "application/x-test", &error);
234 : 1 : g_assert_no_error (error);
235 : :
236 : : /* check that info2 is still default */
237 : 1 : info = g_app_info_get_default_for_type ("application/x-test", FALSE);
238 : 1 : g_assert_nonnull (info);
239 : 1 : g_assert_cmpstr (g_app_info_get_id (info), ==, g_app_info_get_id (info2));
240 : 1 : g_object_unref (info);
241 : :
242 : : /* now remove info1 again */
243 : 1 : g_app_info_remove_supports_type (info1, "application/x-test", &error);
244 : 1 : g_assert_no_error (error);
245 : :
246 : : /* and make sure info2 is still default */
247 : 1 : info = g_app_info_get_default_for_type ("application/x-test", FALSE);
248 : 1 : g_assert_nonnull (info);
249 : 1 : g_assert_cmpstr (g_app_info_get_id (info), ==, g_app_info_get_id (info2));
250 : 1 : g_object_unref (info);
251 : :
252 : : /* now clean it all up */
253 : 1 : g_app_info_reset_type_associations ("application/x-test");
254 : 1 : g_app_info_reset_type_associations ("x-scheme-handler/glib");
255 : :
256 : 1 : list = g_app_info_get_all_for_type ("application/x-test");
257 : 1 : g_assert_null (list);
258 : :
259 : 1 : list = g_app_info_get_all_for_type ("x-scheme-handler/glib");
260 : 1 : g_assert_null (list);
261 : :
262 : 1 : g_app_info_delete (info1);
263 : 1 : g_app_info_delete (info2);
264 : 1 : g_app_info_delete (info3);
265 : :
266 : 1 : g_object_unref (info1);
267 : 1 : g_object_unref (info2);
268 : 1 : g_object_unref (info3);
269 : : }
270 : :
271 : : typedef struct
272 : : {
273 : : GAppInfo *expected_info;
274 : : GMainLoop *loop;
275 : : } DefaultForTypeData;
276 : :
277 : : static void
278 : 6 : ensure_default_type_result (GAppInfo *info,
279 : : DefaultForTypeData *data,
280 : : GError *error)
281 : : {
282 : 6 : if (data->expected_info)
283 : : {
284 : 4 : g_assert_nonnull (info);
285 : 4 : g_assert_no_error (error);
286 : 4 : g_assert_true (g_app_info_equal (info, data->expected_info));
287 : : }
288 : : else
289 : : {
290 : 2 : g_assert_null (info);
291 : 2 : g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND);
292 : : }
293 : :
294 : 6 : g_main_loop_quit (data->loop);
295 : 6 : g_clear_object (&info);
296 : 6 : g_clear_error (&error);
297 : 6 : }
298 : :
299 : : static void
300 : 4 : on_default_for_type_cb (GObject *object,
301 : : GAsyncResult *result,
302 : : gpointer user_data)
303 : : {
304 : : GAppInfo *info;
305 : 4 : GError *error = NULL;
306 : 4 : DefaultForTypeData *data = user_data;
307 : :
308 : 4 : g_assert_null (object);
309 : :
310 : 4 : info = g_app_info_get_default_for_type_finish (result, &error);
311 : :
312 : 4 : ensure_default_type_result (info, data, error);
313 : 4 : }
314 : :
315 : : static void
316 : 2 : on_default_for_uri_cb (GObject *object,
317 : : GAsyncResult *result,
318 : : gpointer user_data)
319 : : {
320 : : GAppInfo *info;
321 : 2 : GError *error = NULL;
322 : 2 : DefaultForTypeData *data = user_data;
323 : :
324 : 2 : g_assert_null (object);
325 : :
326 : 2 : info = g_app_info_get_default_for_uri_scheme_finish (result, &error);
327 : :
328 : 2 : ensure_default_type_result (info, data, error);
329 : 2 : }
330 : :
331 : : static void
332 : 1 : test_default_async (void)
333 : : {
334 : : DefaultForTypeData data;
335 : : GAppInfo *info1, *info2, *info3;
336 : : GList *list;
337 : 1 : GError *error = NULL;
338 : :
339 : 1 : if (skip_missing_update_desktop_database ())
340 : 0 : return;
341 : :
342 : 1 : data.loop = g_main_loop_new (NULL, TRUE);
343 : :
344 : 1 : info1 = create_app_info ("Blah1");
345 : 1 : info2 = create_app_info ("Blah2");
346 : 1 : info3 = create_app_info ("Blah3");
347 : :
348 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
349 : : "*assertion*content_type*failed*");
350 : 1 : g_app_info_get_default_for_type_async (NULL, FALSE, NULL, NULL, NULL);
351 : 1 : g_test_assert_expected_messages ();
352 : :
353 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
354 : : "*assertion*content_type*failed*");
355 : 1 : g_app_info_get_default_for_type_async ("", FALSE, NULL, NULL, NULL);
356 : 1 : g_test_assert_expected_messages ();
357 : :
358 : 1 : g_app_info_set_as_default_for_type (info1, "application/x-test", &error);
359 : 1 : g_assert_no_error (error);
360 : :
361 : 1 : g_app_info_set_as_default_for_type (info2, "application/x-test", &error);
362 : 1 : g_assert_no_error (error);
363 : :
364 : 1 : data.expected_info = info2;
365 : 1 : g_app_info_get_default_for_type_async ("application/x-test", FALSE,
366 : : NULL, on_default_for_type_cb, &data);
367 : 1 : g_main_loop_run (data.loop);
368 : :
369 : : /* now try adding something, but not setting as default */
370 : 1 : g_app_info_add_supports_type (info3, "application/x-test", &error);
371 : 1 : g_assert_no_error (error);
372 : :
373 : : /* check that info2 is still default */
374 : 1 : data.expected_info = info2;
375 : 1 : g_app_info_get_default_for_type_async ("application/x-test", FALSE,
376 : : NULL, on_default_for_type_cb, &data);
377 : 1 : g_main_loop_run (data.loop);
378 : :
379 : : /* now remove info1 again */
380 : 1 : g_app_info_remove_supports_type (info1, "application/x-test", &error);
381 : 1 : g_assert_no_error (error);
382 : :
383 : : /* and make sure info2 is still default */
384 : 1 : data.expected_info = info2;
385 : 1 : g_app_info_get_default_for_type_async ("application/x-test", FALSE,
386 : : NULL, on_default_for_type_cb, &data);
387 : 1 : g_main_loop_run (data.loop);
388 : :
389 : 1 : g_app_info_set_as_default_for_type (info3, "x-scheme-handler/glib-async", &error);
390 : 1 : g_assert_no_error (error);
391 : :
392 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
393 : : "*assertion*uri_scheme*failed*");
394 : 1 : g_assert_null (g_app_info_get_default_for_uri_scheme (NULL));
395 : 1 : g_test_assert_expected_messages ();
396 : :
397 : 1 : g_test_expect_message (G_LOG_DOMAIN, G_LOG_LEVEL_CRITICAL,
398 : : "*assertion*uri_scheme*failed*");
399 : 1 : g_assert_null (g_app_info_get_default_for_uri_scheme (""));
400 : 1 : g_test_assert_expected_messages ();
401 : :
402 : 1 : data.expected_info = info3;
403 : 1 : g_app_info_get_default_for_uri_scheme_async ("glib-async", NULL,
404 : : on_default_for_uri_cb, &data);
405 : 1 : g_main_loop_run (data.loop);
406 : :
407 : : /* now clean it all up */
408 : 1 : g_app_info_reset_type_associations ("application/x-test");
409 : :
410 : 1 : data.expected_info = NULL;
411 : 1 : g_app_info_get_default_for_type_async ("application/x-test", FALSE,
412 : : NULL, on_default_for_type_cb, &data);
413 : 1 : g_main_loop_run (data.loop);
414 : :
415 : 1 : g_app_info_reset_type_associations ("x-scheme-handler/glib-async");
416 : :
417 : 1 : data.expected_info = NULL;
418 : 1 : g_app_info_get_default_for_uri_scheme_async ("glib-async", NULL,
419 : : on_default_for_uri_cb, &data);
420 : 1 : g_main_loop_run (data.loop);
421 : :
422 : 1 : list = g_app_info_get_all_for_type ("application/x-test");
423 : 1 : g_assert_null (list);
424 : :
425 : 1 : g_app_info_delete (info1);
426 : 1 : g_app_info_delete (info2);
427 : 1 : g_app_info_delete (info3);
428 : :
429 : 1 : g_object_unref (info1);
430 : 1 : g_object_unref (info2);
431 : 1 : g_object_unref (info3);
432 : :
433 : 1 : g_main_loop_unref (data.loop);
434 : : }
435 : :
436 : : static void
437 : 1 : test_fallback (void)
438 : : {
439 : 1 : GAppInfo *info1, *info2, *app = NULL;
440 : : GList *apps, *recomm, *fallback, *list, *l, *m;
441 : 1 : GError *error = NULL;
442 : : gint old_length;
443 : :
444 : 1 : if (skip_missing_update_desktop_database ())
445 : 0 : return;
446 : :
447 : 1 : info1 = create_app_info ("Test1");
448 : 1 : info2 = create_app_info ("Test2");
449 : :
450 : 1 : g_assert_true (g_content_type_is_a ("text/x-python", "text/plain"));
451 : :
452 : 1 : apps = g_app_info_get_all_for_type ("text/x-python");
453 : 1 : old_length = g_list_length (apps);
454 : 1 : g_list_free_full (apps, g_object_unref);
455 : :
456 : 1 : g_app_info_add_supports_type (info1, "text/x-python", &error);
457 : 1 : g_assert_no_error (error);
458 : :
459 : 1 : g_app_info_add_supports_type (info2, "text/plain", &error);
460 : 1 : g_assert_no_error (error);
461 : :
462 : : /* check that both apps are registered */
463 : 1 : apps = g_app_info_get_all_for_type ("text/x-python");
464 : 1 : g_assert_cmpint (g_list_length (apps), ==, old_length + 2);
465 : :
466 : : /* check that Test1 is among the recommended apps */
467 : 1 : recomm = g_app_info_get_recommended_for_type ("text/x-python");
468 : 1 : g_assert_nonnull (recomm);
469 : 1 : for (l = recomm; l; l = l->next)
470 : : {
471 : 1 : app = l->data;
472 : 1 : if (g_app_info_equal (info1, app))
473 : 1 : break;
474 : : }
475 : 1 : g_assert_nonnull (app);
476 : 1 : g_assert_true (g_app_info_equal (info1, app));
477 : :
478 : : /* and that Test2 is among the fallback apps */
479 : 1 : fallback = g_app_info_get_fallback_for_type ("text/x-python");
480 : 1 : g_assert_nonnull (fallback);
481 : 1 : for (l = fallback; l; l = l->next)
482 : : {
483 : 1 : app = l->data;
484 : 1 : if (g_app_info_equal (info2, app))
485 : 1 : break;
486 : : }
487 : 1 : g_assert_cmpstr (g_app_info_get_name (app), ==, "Test2");
488 : :
489 : : /* check that recomm + fallback = all applications */
490 : 1 : list = g_list_concat (g_list_copy (recomm), g_list_copy (fallback));
491 : 1 : g_assert_cmpuint (g_list_length (list), ==, g_list_length (apps));
492 : :
493 : 3 : for (l = list, m = apps; l != NULL && m != NULL; l = l->next, m = m->next)
494 : : {
495 : 2 : g_assert_true (g_app_info_equal (l->data, m->data));
496 : : }
497 : :
498 : 1 : g_list_free (list);
499 : :
500 : 1 : g_list_free_full (apps, g_object_unref);
501 : 1 : g_list_free_full (recomm, g_object_unref);
502 : 1 : g_list_free_full (fallback, g_object_unref);
503 : :
504 : 1 : g_app_info_reset_type_associations ("text/x-python");
505 : 1 : g_app_info_reset_type_associations ("text/plain");
506 : :
507 : 1 : g_app_info_delete (info1);
508 : 1 : g_app_info_delete (info2);
509 : :
510 : 1 : g_object_unref (info1);
511 : 1 : g_object_unref (info2);
512 : : }
513 : :
514 : : static void
515 : 1 : test_last_used (void)
516 : : {
517 : : GList *applications;
518 : : GAppInfo *info1, *info2, *default_app;
519 : 1 : GError *error = NULL;
520 : :
521 : 1 : if (skip_missing_update_desktop_database ())
522 : 0 : return;
523 : :
524 : 1 : info1 = create_app_info ("Test1");
525 : 1 : info2 = create_app_info ("Test2");
526 : :
527 : 1 : g_app_info_set_as_default_for_type (info1, "application/x-test", &error);
528 : 1 : g_assert_no_error (error);
529 : :
530 : 1 : g_app_info_add_supports_type (info2, "application/x-test", &error);
531 : 1 : g_assert_no_error (error);
532 : :
533 : 1 : applications = g_app_info_get_recommended_for_type ("application/x-test");
534 : 1 : g_assert_cmpuint (g_list_length (applications), ==, 2);
535 : :
536 : : /* the first should be the default app now */
537 : 1 : g_assert_true (g_app_info_equal (g_list_nth_data (applications, 0), info1));
538 : 1 : g_assert_true (g_app_info_equal (g_list_nth_data (applications, 1), info2));
539 : :
540 : 1 : g_list_free_full (applications, g_object_unref);
541 : :
542 : 1 : g_app_info_set_as_last_used_for_type (info2, "application/x-test", &error);
543 : 1 : g_assert_no_error (error);
544 : :
545 : 1 : applications = g_app_info_get_recommended_for_type ("application/x-test");
546 : 1 : g_assert_cmpuint (g_list_length (applications), ==, 2);
547 : :
548 : 1 : default_app = g_app_info_get_default_for_type ("application/x-test", FALSE);
549 : 1 : g_assert_true (g_app_info_equal (default_app, info1));
550 : :
551 : : /* the first should be the other app now */
552 : 1 : g_assert_true (g_app_info_equal (g_list_nth_data (applications, 0), info2));
553 : 1 : g_assert_true (g_app_info_equal (g_list_nth_data (applications, 1), info1));
554 : :
555 : 1 : g_list_free_full (applications, g_object_unref);
556 : :
557 : 1 : g_app_info_reset_type_associations ("application/x-test");
558 : :
559 : 1 : g_app_info_delete (info1);
560 : 1 : g_app_info_delete (info2);
561 : :
562 : 1 : g_object_unref (info1);
563 : 1 : g_object_unref (info2);
564 : 1 : g_object_unref (default_app);
565 : : }
566 : :
567 : : static void
568 : 1 : test_extra_getters (void)
569 : : {
570 : : GDesktopAppInfo *appinfo;
571 : : const gchar *lang;
572 : : gchar *s;
573 : : gboolean b;
574 : :
575 : 1 : lang = setlocale (LC_ALL, NULL);
576 : 1 : g_setenv ("LANGUAGE", "de_DE.UTF8", TRUE);
577 : 1 : setlocale (LC_ALL, "");
578 : :
579 : 1 : appinfo = g_desktop_app_info_new_from_filename (g_test_get_filename (G_TEST_DIST, "appinfo-test-static.desktop", NULL));
580 : 1 : g_assert_nonnull (appinfo);
581 : :
582 : 1 : g_assert_true (g_desktop_app_info_has_key (appinfo, "Terminal"));
583 : 1 : g_assert_false (g_desktop_app_info_has_key (appinfo, "Bratwurst"));
584 : :
585 : 1 : s = g_desktop_app_info_get_string (appinfo, "StartupWMClass");
586 : 1 : g_assert_cmpstr (s, ==, "appinfo-class");
587 : 1 : g_free (s);
588 : :
589 : 1 : s = g_desktop_app_info_get_locale_string (appinfo, "X-JunkFood");
590 : 1 : g_assert_cmpstr (s, ==, "Bratwurst");
591 : 1 : g_free (s);
592 : :
593 : 1 : g_setenv ("LANGUAGE", "sv_SE.UTF8", TRUE);
594 : 1 : setlocale (LC_ALL, "");
595 : :
596 : 1 : s = g_desktop_app_info_get_locale_string (appinfo, "X-JunkFood");
597 : 1 : g_assert_cmpstr (s, ==, "Burger"); /* fallback */
598 : 1 : g_free (s);
599 : :
600 : 1 : b = g_desktop_app_info_get_boolean (appinfo, "Terminal");
601 : 1 : g_assert_true (b);
602 : :
603 : 1 : g_object_unref (appinfo);
604 : :
605 : 1 : g_setenv ("LANGUAGE", lang, TRUE);
606 : 1 : setlocale (LC_ALL, "");
607 : 1 : }
608 : :
609 : : static void
610 : 3 : wait_for_file (const gchar *want_this,
611 : : const gchar *but_not_this,
612 : : const gchar *or_this)
613 : : {
614 : 6 : while (access (want_this, F_OK) != 0)
615 : 3 : g_usleep (100000); /* 100ms */
616 : :
617 : 3 : g_assert_cmpuint (access (but_not_this, F_OK), !=, 0);
618 : 3 : g_assert_cmpuint (access (or_this, F_OK), !=, 0);
619 : :
620 : 3 : unlink (want_this);
621 : 3 : unlink (but_not_this);
622 : 3 : unlink (or_this);
623 : 3 : }
624 : :
625 : : static gboolean
626 : 2 : skip_missing_dbus_daemon (void)
627 : : {
628 : 2 : gchar *path = g_find_program_in_path ("dbus-daemon");
629 : 2 : if (path == NULL)
630 : : {
631 : 0 : g_test_skip ("dbus-daemon is required to run this test");
632 : 0 : return TRUE;
633 : : }
634 : 2 : g_free (path);
635 : 2 : return FALSE;
636 : : }
637 : :
638 : : static void
639 : 1 : test_actions (void)
640 : : {
641 : 1 : GTestDBus *bus = NULL;
642 : 1 : const char *expected[] = { "frob", "tweak", "twiddle", "broken", NULL };
643 : : const gchar * const *actions;
644 : : GDesktopAppInfo *appinfo;
645 : : const gchar *tmpdir;
646 : : gchar *name;
647 : : gchar *frob_path;
648 : : gchar *tweak_path;
649 : : gchar *twiddle_path;
650 : :
651 : 1 : if (skip_missing_dbus_daemon ())
652 : 0 : return;
653 : :
654 : : /* Set up a test session bus to keep D-Bus traffic off the real session bus. */
655 : 1 : bus = g_test_dbus_new (G_TEST_DBUS_NONE);
656 : 1 : g_test_dbus_up (bus);
657 : :
658 : 1 : appinfo = g_desktop_app_info_new_from_filename (g_test_get_filename (G_TEST_DIST, "appinfo-test-actions.desktop", NULL));
659 : 1 : g_assert_nonnull (appinfo);
660 : :
661 : 1 : actions = g_desktop_app_info_list_actions (appinfo);
662 : 5 : g_assert_cmpstrv (actions, expected);
663 : :
664 : 1 : name = g_desktop_app_info_get_action_name (appinfo, "frob");
665 : 1 : g_assert_cmpstr (name, ==, "Frobnicate");
666 : 1 : g_free (name);
667 : :
668 : 1 : name = g_desktop_app_info_get_action_name (appinfo, "tweak");
669 : 1 : g_assert_cmpstr (name, ==, "Tweak");
670 : 1 : g_free (name);
671 : :
672 : 1 : name = g_desktop_app_info_get_action_name (appinfo, "twiddle");
673 : 1 : g_assert_cmpstr (name, ==, "Twiddle");
674 : 1 : g_free (name);
675 : :
676 : 1 : name = g_desktop_app_info_get_action_name (appinfo, "broken");
677 : 1 : g_assert_nonnull (name);
678 : 1 : g_assert_true (g_utf8_validate (name, -1, NULL));
679 : 1 : g_free (name);
680 : :
681 : 1 : tmpdir = g_getenv ("G_TEST_TMPDIR");
682 : 1 : g_assert_nonnull (tmpdir);
683 : 1 : frob_path = g_build_filename (tmpdir, "frob", NULL);
684 : 1 : tweak_path = g_build_filename (tmpdir, "tweak", NULL);
685 : 1 : twiddle_path = g_build_filename (tmpdir, "twiddle", NULL);
686 : :
687 : 1 : g_assert_false (g_file_test (frob_path, G_FILE_TEST_EXISTS));
688 : 1 : g_assert_false (g_file_test (tweak_path, G_FILE_TEST_EXISTS));
689 : 1 : g_assert_false (g_file_test (twiddle_path, G_FILE_TEST_EXISTS));
690 : :
691 : 1 : g_desktop_app_info_launch_action (appinfo, "frob", NULL);
692 : 1 : wait_for_file (frob_path, tweak_path, twiddle_path);
693 : :
694 : 1 : g_desktop_app_info_launch_action (appinfo, "tweak", NULL);
695 : 1 : wait_for_file (tweak_path, frob_path, twiddle_path);
696 : :
697 : 1 : g_desktop_app_info_launch_action (appinfo, "twiddle", NULL);
698 : 1 : wait_for_file (twiddle_path, frob_path, tweak_path);
699 : :
700 : 1 : g_free (frob_path);
701 : 1 : g_free (tweak_path);
702 : 1 : g_free (twiddle_path);
703 : 1 : g_object_unref (appinfo);
704 : :
705 : 1 : g_test_dbus_down (bus);
706 : 1 : g_clear_object (&bus);
707 : : }
708 : :
709 : : static gchar *
710 : 50 : run_apps (const gchar *command,
711 : : const gchar *arg,
712 : : gboolean with_usr,
713 : : gboolean with_home,
714 : : const gchar *locale_name,
715 : : const gchar *language,
716 : : const gchar *xdg_current_desktop)
717 : : {
718 : : gboolean success;
719 : : gchar **envp;
720 : : gchar **argv;
721 : : gint status;
722 : : gchar *out;
723 : 50 : gchar *argv_str = NULL;
724 : :
725 : 50 : argv = g_new (gchar *, 4);
726 : 50 : argv[0] = g_test_build_filename (G_TEST_BUILT, "apps", NULL);
727 : 50 : argv[1] = g_strdup (command);
728 : 50 : argv[2] = g_strdup (arg);
729 : 50 : argv[3] = NULL;
730 : :
731 : 50 : g_assert_true (g_file_test (argv[0], G_FILE_TEST_IS_EXECUTABLE));
732 : 50 : envp = g_get_environ ();
733 : :
734 : 50 : if (with_usr)
735 : : {
736 : 41 : gchar *tmp = g_test_build_filename (G_TEST_DIST, "desktop-files", "usr", NULL);
737 : 41 : envp = g_environ_setenv (envp, "XDG_DATA_DIRS", tmp, TRUE);
738 : 41 : g_free (tmp);
739 : : }
740 : : else
741 : 9 : envp = g_environ_setenv (envp, "XDG_DATA_DIRS", "/does-not-exist", TRUE);
742 : :
743 : 50 : if (with_home)
744 : : {
745 : 34 : gchar *tmp = g_test_build_filename (G_TEST_DIST, "desktop-files", "home", NULL);
746 : 34 : envp = g_environ_setenv (envp, "XDG_DATA_HOME", tmp, TRUE);
747 : 34 : g_free (tmp);
748 : : }
749 : : else
750 : 16 : envp = g_environ_setenv (envp, "XDG_DATA_HOME", "/does-not-exist", TRUE);
751 : :
752 : 50 : if (locale_name)
753 : 2 : envp = g_environ_setenv (envp, "LC_ALL", locale_name, TRUE);
754 : : else
755 : 48 : envp = g_environ_setenv (envp, "LC_ALL", "C", TRUE);
756 : :
757 : 50 : if (language)
758 : 2 : envp = g_environ_setenv (envp, "LANGUAGE", language, TRUE);
759 : : else
760 : 48 : envp = g_environ_unsetenv (envp, "LANGUAGE");
761 : :
762 : 50 : if (xdg_current_desktop)
763 : 10 : envp = g_environ_setenv (envp, "XDG_CURRENT_DESKTOP", xdg_current_desktop, TRUE);
764 : : else
765 : 40 : envp = g_environ_unsetenv (envp, "XDG_CURRENT_DESKTOP");
766 : :
767 : 50 : envp = g_environ_setenv (envp, "G_MESSAGES_DEBUG", "", TRUE);
768 : :
769 : 50 : success = g_spawn_sync (NULL, argv, envp, 0, NULL, NULL, &out, NULL, &status, NULL);
770 : 50 : g_assert_true (success);
771 : 50 : g_assert_cmpuint (status, ==, 0);
772 : :
773 : 50 : argv_str = g_strjoinv (" ", argv);
774 : 50 : g_test_message ("%s: `%s` returned: %s", G_STRFUNC, argv_str, out);
775 : 50 : g_free (argv_str);
776 : :
777 : 50 : g_strfreev (envp);
778 : 50 : g_strfreev (argv);
779 : :
780 : 50 : return out;
781 : : }
782 : :
783 : : static void
784 : 59 : assert_strings_equivalent (const gchar *expected,
785 : : const gchar *result)
786 : 101 : {
787 : : gchar **expected_words;
788 : : gchar **result_words;
789 : : gint i, j;
790 : :
791 : 59 : expected_words = g_strsplit (expected, " ", 0);
792 : 59 : result_words = g_strsplit_set (result, " \n", 0);
793 : :
794 : 160 : for (i = 0; expected_words[i]; i++)
795 : : {
796 : 1007 : for (j = 0; result_words[j]; j++)
797 : 1007 : if (g_str_equal (expected_words[i], result_words[j]))
798 : 101 : goto got_it;
799 : :
800 : 0 : g_test_fail_printf ("Unable to find expected string '%s' in result '%s'", expected_words[i], result);
801 : :
802 : 101 : got_it:
803 : 101 : continue;
804 : : }
805 : :
806 : 59 : g_assert_cmpint (g_strv_length (expected_words), ==, g_strv_length (result_words));
807 : 59 : g_strfreev (expected_words);
808 : 59 : g_strfreev (result_words);
809 : 59 : }
810 : :
811 : : static void
812 : 4 : assert_list (const gchar *expected,
813 : : gboolean with_usr,
814 : : gboolean with_home,
815 : : const gchar *locale_name,
816 : : const gchar *language)
817 : : {
818 : : gchar *result;
819 : :
820 : 4 : result = run_apps ("list", NULL, with_usr, with_home, locale_name, language, NULL);
821 : 4 : g_strchomp (result);
822 : 4 : assert_strings_equivalent (expected, result);
823 : 4 : g_free (result);
824 : 4 : }
825 : :
826 : : static void
827 : 2 : assert_info (const gchar *desktop_id,
828 : : const gchar *expected,
829 : : gboolean with_usr,
830 : : gboolean with_home,
831 : : const gchar *locale_name,
832 : : const gchar *language)
833 : : {
834 : : gchar *result;
835 : :
836 : 2 : result = run_apps ("show-info", desktop_id, with_usr, with_home, locale_name, language, NULL);
837 : 2 : g_assert_cmpstr (result, ==, expected);
838 : 2 : g_free (result);
839 : 2 : }
840 : :
841 : : static void
842 : 28 : assert_search (const gchar *search_string,
843 : : const gchar *expected,
844 : : gboolean with_usr,
845 : : gboolean with_home,
846 : : const gchar *locale_name,
847 : : const gchar *language)
848 : : {
849 : : gchar **expected_lines;
850 : : gchar **result_lines;
851 : : gchar *result;
852 : : gint i;
853 : :
854 : 28 : expected_lines = g_strsplit (expected, "\n", -1);
855 : 28 : result = run_apps ("search", search_string, with_usr, with_home, locale_name, language, NULL);
856 : 28 : result_lines = g_strsplit (result, "\n", -1);
857 : 28 : g_assert_cmpint (g_strv_length (expected_lines), ==, g_strv_length (result_lines));
858 : 79 : for (i = 0; expected_lines[i]; i++)
859 : 51 : assert_strings_equivalent (expected_lines[i], result_lines[i]);
860 : 28 : g_strfreev (expected_lines);
861 : 28 : g_strfreev (result_lines);
862 : 28 : g_free (result);
863 : 28 : }
864 : :
865 : : static void
866 : 4 : assert_implementations (const gchar *interface,
867 : : const gchar *expected,
868 : : gboolean with_usr,
869 : : gboolean with_home)
870 : : {
871 : : gchar *result;
872 : :
873 : 4 : result = run_apps ("implementations", interface, with_usr, with_home, NULL, NULL, NULL);
874 : 4 : g_strchomp (result);
875 : 4 : assert_strings_equivalent (expected, result);
876 : 4 : g_free (result);
877 : 4 : }
878 : :
879 : : #define ALL_USR_APPS "evince-previewer.desktop nautilus-classic.desktop gnome-font-viewer.desktop " \
880 : : "baobab.desktop yelp.desktop eog.desktop cheese.desktop org.gnome.clocks.desktop " \
881 : : "gnome-contacts.desktop kde4-kate.desktop gcr-prompter.desktop totem.desktop " \
882 : : "gnome-terminal.desktop nautilus-autorun-software.desktop gcr-viewer.desktop " \
883 : : "nautilus-connect-server.desktop kde4-dolphin.desktop gnome-music.desktop " \
884 : : "kde4-konqbrowser.desktop gucharmap.desktop kde4-okular.desktop nautilus.desktop " \
885 : : "gedit.desktop evince.desktop file-roller.desktop dconf-editor.desktop glade.desktop " \
886 : : "invalid-desktop.desktop org.gnome.Calculator.desktop libreoffice-calc.desktop"
887 : : #define HOME_APPS "epiphany-weather-for-toronto-island-9c6a4e022b17686306243dada811d550d25eb1fb.desktop"
888 : : #define ALL_HOME_APPS HOME_APPS " eog.desktop"
889 : :
890 : : static void
891 : 1 : test_search (void)
892 : : {
893 : 1 : assert_list ("", FALSE, FALSE, NULL, NULL);
894 : 1 : assert_list (ALL_USR_APPS, TRUE, FALSE, NULL, NULL);
895 : 1 : assert_list (ALL_HOME_APPS, FALSE, TRUE, NULL, NULL);
896 : 1 : assert_list (ALL_USR_APPS " " HOME_APPS, TRUE, TRUE, NULL, NULL);
897 : :
898 : : /* The user has "installed" their own version of eog.desktop which
899 : : * calls it "Eye of GNOME". Do some testing based on that.
900 : : *
901 : : * We should always find "Pictures" keyword no matter where we look.
902 : : */
903 : 1 : assert_search ("Picture", "eog.desktop\n", TRUE, TRUE, NULL, NULL);
904 : 1 : assert_search ("Picture", "eog.desktop\n", TRUE, FALSE, NULL, NULL);
905 : 1 : assert_search ("Picture", "eog.desktop\n", FALSE, TRUE, NULL, NULL);
906 : 1 : assert_search ("Picture", "", FALSE, FALSE, NULL, NULL);
907 : :
908 : : /* We should only find it called "eye of gnome" when using the user's
909 : : * directory.
910 : : */
911 : 1 : assert_search ("eye gnome", "", TRUE, FALSE, NULL, NULL);
912 : 1 : assert_search ("eye gnome", "eog.desktop\n", FALSE, TRUE, NULL, NULL);
913 : 1 : assert_search ("eye gnome", "eog.desktop\n", TRUE, TRUE, NULL, NULL);
914 : :
915 : : /* We should only find it called "image viewer" when _not_ using the
916 : : * user's directory.
917 : : */
918 : 1 : assert_search ("image viewer", "eog.desktop\n", TRUE, FALSE, NULL, NULL);
919 : 1 : assert_search ("image viewer", "", FALSE, TRUE, NULL, NULL);
920 : 1 : assert_search ("image viewer", "", TRUE, TRUE, NULL, NULL);
921 : :
922 : : /* There're "flatpak" apps (clocks) installed as well - they should *not*
923 : : * match the prefix command ("/bin/sh") in the Exec= line though. Then with
924 : : * substring matching, Image Viewer (eog) should be in next group because it
925 : : * contains "Slideshow" in its keywords.
926 : : *
927 : : * Finally we have LibreOffice Calc, which contains "OpenDocument Spreadsheet".
928 : : * It is sorted last because its match ("sh" in "Spreadsheet") occurs in a
929 : : * later token.
930 : : */
931 : 1 : assert_search ("sh", "gnome-terminal.desktop\n"
932 : : "eog.desktop\n"
933 : : "libreoffice-calc.desktop\n", TRUE, FALSE, NULL, NULL);
934 : :
935 : : /* "frobnicator.desktop" is ignored by get_all() because the binary is
936 : : * missing, but search should still find it (to avoid either stale results
937 : : * from the cache or expensive stat() calls for each potential result)
938 : : */
939 : 1 : assert_search ("frobni", "frobnicator.desktop\n", TRUE, FALSE, NULL, NULL);
940 : :
941 : : /* Obvious multi-word search */
942 : 1 : assert_search ("doc hel", "yelp.desktop\n", TRUE, TRUE, NULL, NULL);
943 : :
944 : : /* Repeated search terms should do nothing... */
945 : 1 : assert_search ("files file fil fi f", "nautilus.desktop\n", TRUE, TRUE, NULL, NULL);
946 : :
947 : : /* "con" will match "connect" and "contacts" on name with prefix match in
948 : : * first group, then second group is a Keyword prefix match for "configuration" in dconf-editor.desktop
949 : : * and third group is a substring match for "Desktop Icons" in Name of nautilus-classic.desktop.
950 : : */
951 : 1 : assert_search ("con", "gnome-contacts.desktop nautilus-connect-server.desktop\n"
952 : : "dconf-editor.desktop\n"
953 : : "nautilus-classic.desktop\n", TRUE, TRUE, NULL, NULL);
954 : :
955 : : /* We prefer matches of tokens that come earlier in a string. In this case
956 : : * "LibreOffice Calc" and "Calculator" both have a name that contains a prefix
957 : : * match "cal", but the one in Calculator occurs in the first token.
958 : : */
959 : 1 : assert_search ("cal", "org.gnome.Calculator.desktop\nlibreoffice-calc.desktop\n", TRUE, TRUE, NULL, NULL);
960 : :
961 : : /* Same as above, but ensure that substring matches are sorted after prefix matches */
962 : 1 : assert_search ("ca", "org.gnome.Calculator.desktop\n"
963 : : "libreoffice-calc.desktop\n"
964 : : "frobnicator.desktop\n"
965 : : "cheese.desktop\n", TRUE, TRUE, NULL, NULL);
966 : :
967 : : /* "gnome" will match "eye of gnome" from the user's directory, plus
968 : : * matching "GNOME Clocks" X-GNOME-FullName.
969 : : */
970 : 1 : assert_search ("gnome", "eog.desktop\n"
971 : : "org.gnome.clocks.desktop\n", TRUE, TRUE, NULL, NULL);
972 : :
973 : : /* eog has exec name 'false' in usr only */
974 : 1 : assert_search ("false", "eog.desktop\n", TRUE, FALSE, NULL, NULL);
975 : 1 : assert_search ("false", "", FALSE, TRUE, NULL, NULL);
976 : 1 : assert_search ("false", "", TRUE, TRUE, NULL, NULL);
977 : 1 : assert_search ("false", "", FALSE, FALSE, NULL, NULL);
978 : :
979 : : /* make sure we only search the first component */
980 : 1 : assert_search ("nonsearchable", "", TRUE, FALSE, NULL, NULL);
981 : :
982 : : /* "gnome con" will match only gnome contacts; via the name for
983 : : * "contacts" and keywords for "friend"
984 : : */
985 : 1 : assert_search ("friend con", "gnome-contacts.desktop\n", TRUE, TRUE, NULL, NULL);
986 : :
987 : : /* make sure we get the correct kde4- prefix on the application IDs
988 : : * from subdirectories
989 : : */
990 : 1 : assert_search ("konq", "kde4-konqbrowser.desktop\n", TRUE, TRUE, NULL, NULL);
991 : 1 : assert_search ("kate", "kde4-kate.desktop\n", TRUE, TRUE, NULL, NULL);
992 : :
993 : : /* make sure we can look up apps by name properly */
994 : 1 : assert_info ("kde4-kate.desktop",
995 : : "kde4-kate.desktop\n"
996 : : "Kate\n"
997 : : "Kate\n"
998 : : "nil\n", TRUE, TRUE, NULL, NULL);
999 : :
1000 : 1 : assert_info ("nautilus.desktop",
1001 : : "nautilus.desktop\n"
1002 : : "Files\n"
1003 : : "Files\n"
1004 : : "Access and organize files\n", TRUE, TRUE, NULL, NULL);
1005 : :
1006 : : /* make sure localised searching works properly */
1007 : 1 : assert_search ("foliumi", "nautilus.desktop\n"
1008 : : "kde4-konqbrowser.desktop\n", TRUE, FALSE, "en_US.UTF-8", "eo");
1009 : : /* the user's eog.desktop has no translations... */
1010 : 1 : assert_search ("foliumi", "nautilus.desktop\n"
1011 : : "kde4-konqbrowser.desktop\n", TRUE, TRUE, "en_US.UTF-8", "eo");
1012 : 1 : }
1013 : :
1014 : : static void
1015 : 1 : test_implements (void)
1016 : : {
1017 : : /* Make sure we can find our search providers... */
1018 : 1 : assert_implementations ("org.gnome.Shell.SearchProvider2",
1019 : : "gnome-music.desktop gnome-contacts.desktop eog.desktop",
1020 : : TRUE, FALSE);
1021 : :
1022 : : /* And our image acquisition possibilities... */
1023 : 1 : assert_implementations ("org.freedesktop.ImageProvider",
1024 : : "cheese.desktop",
1025 : : TRUE, FALSE);
1026 : :
1027 : : /* Make sure the user's eog is properly masking the system one */
1028 : 1 : assert_implementations ("org.gnome.Shell.SearchProvider2",
1029 : : "gnome-music.desktop gnome-contacts.desktop",
1030 : : TRUE, TRUE);
1031 : :
1032 : : /* Make sure we get nothing if we have nothing */
1033 : 1 : assert_implementations ("org.gnome.Shell.SearchProvider2", "", FALSE, FALSE);
1034 : 1 : }
1035 : :
1036 : : static void
1037 : 11 : assert_shown (const gchar *desktop_id,
1038 : : gboolean expected,
1039 : : const gchar *xdg_current_desktop)
1040 : : {
1041 : : gchar *result;
1042 : :
1043 : 11 : result = run_apps ("should-show", desktop_id, TRUE, TRUE, NULL, NULL, xdg_current_desktop);
1044 : 11 : g_assert_cmpstr (result, ==, expected ? "true\n" : "false\n");
1045 : 11 : g_free (result);
1046 : 11 : }
1047 : :
1048 : : static void
1049 : 1 : test_show_in (void)
1050 : : {
1051 : 1 : assert_shown ("gcr-prompter.desktop", FALSE, NULL);
1052 : 1 : assert_shown ("gcr-prompter.desktop", FALSE, "GNOME");
1053 : 1 : assert_shown ("gcr-prompter.desktop", FALSE, "KDE");
1054 : 1 : assert_shown ("gcr-prompter.desktop", FALSE, "GNOME:GNOME-Classic");
1055 : 1 : assert_shown ("gcr-prompter.desktop", TRUE, "GNOME-Classic:GNOME");
1056 : 1 : assert_shown ("gcr-prompter.desktop", TRUE, "GNOME-Classic");
1057 : 1 : assert_shown ("gcr-prompter.desktop", TRUE, "GNOME-Classic:KDE");
1058 : 1 : assert_shown ("gcr-prompter.desktop", TRUE, "KDE:GNOME-Classic");
1059 : 1 : assert_shown ("invalid-desktop.desktop", TRUE, "GNOME");
1060 : 1 : assert_shown ("invalid-desktop.desktop", FALSE, "../invalid/desktop");
1061 : 1 : assert_shown ("invalid-desktop.desktop", FALSE, "../invalid/desktop:../invalid/desktop");
1062 : 1 : }
1063 : :
1064 : : static void
1065 : 4 : on_launch_started (GAppLaunchContext *context, GAppInfo *info, GVariant *platform_data, gpointer data)
1066 : : {
1067 : 4 : gboolean *invoked = data;
1068 : :
1069 : 4 : g_assert_true (G_IS_APP_LAUNCH_CONTEXT (context));
1070 : :
1071 : 4 : if (TEST_IS_LAUNCH_CONTEXT (context))
1072 : : {
1073 : : GVariantDict dict;
1074 : : const char *sni;
1075 : : char *expected_sni;
1076 : :
1077 : 4 : g_assert_nonnull (platform_data);
1078 : 4 : g_variant_dict_init (&dict, platform_data);
1079 : 4 : g_assert_true (
1080 : : g_variant_dict_lookup (&dict, "startup-notification-id", "&s", &sni));
1081 : 4 : expected_sni = g_app_launch_context_get_startup_notify_id (context, info, NULL);
1082 : 4 : g_assert_cmpstr (sni, ==, expected_sni);
1083 : :
1084 : 4 : g_free (expected_sni);
1085 : 4 : g_variant_dict_clear (&dict);
1086 : : }
1087 : : else
1088 : : {
1089 : : /* Our default context doesn't fill in any platform data */
1090 : 0 : g_assert_null (platform_data);
1091 : : }
1092 : :
1093 : 4 : g_assert_false (*invoked);
1094 : 4 : *invoked = TRUE;
1095 : 4 : }
1096 : :
1097 : : static void
1098 : 2 : on_launched (GAppLaunchContext *context, GAppInfo *info, GVariant *platform_data, gpointer data)
1099 : : {
1100 : 2 : gboolean *launched = data;
1101 : : GVariantDict dict;
1102 : : int pid;
1103 : :
1104 : 2 : g_assert_true (G_IS_APP_LAUNCH_CONTEXT (context));
1105 : 2 : g_assert_true (G_IS_APP_INFO (info));
1106 : 2 : g_assert_nonnull (platform_data);
1107 : 2 : g_variant_dict_init (&dict, platform_data);
1108 : 2 : g_assert_true (g_variant_dict_lookup (&dict, "pid", "i", &pid, NULL));
1109 : 2 : g_assert_cmpint (pid, >, 1);
1110 : :
1111 : 2 : g_assert_false (*launched);
1112 : 2 : *launched = TRUE;
1113 : :
1114 : 2 : g_variant_dict_clear (&dict);
1115 : 2 : }
1116 : :
1117 : : static void
1118 : 2 : on_launch_failed (GAppLaunchContext *context, const char *startup_notify_id, gpointer data)
1119 : : {
1120 : 2 : gboolean *invoked = data;
1121 : :
1122 : 2 : g_assert_true (G_IS_APP_LAUNCH_CONTEXT (context));
1123 : 2 : g_assert_nonnull (startup_notify_id);
1124 : 2 : g_test_message ("Application launch failed: %s", startup_notify_id);
1125 : :
1126 : 2 : g_assert_false (*invoked);
1127 : 2 : *invoked = TRUE;
1128 : 2 : }
1129 : :
1130 : : /* Test g_desktop_app_info_launch_uris_as_manager() and
1131 : : * g_desktop_app_info_launch_uris_as_manager_with_fds()
1132 : : */
1133 : : static void
1134 : 1 : test_launch_as_manager (void)
1135 : : {
1136 : : GDesktopAppInfo *appinfo;
1137 : 1 : GError *error = NULL;
1138 : : gboolean retval;
1139 : : const gchar *path;
1140 : 1 : gboolean invoked = FALSE;
1141 : 1 : gboolean launched = FALSE;
1142 : 1 : gboolean failed = FALSE;
1143 : : GAppLaunchContext *context;
1144 : :
1145 : 1 : path = g_test_get_filename (G_TEST_BUILT, "appinfo-test.desktop", NULL);
1146 : 1 : appinfo = g_desktop_app_info_new_from_filename (path);
1147 : 1 : g_assert_true (G_IS_APP_INFO (appinfo));
1148 : :
1149 : 1 : context = g_object_new (test_launch_context_get_type (), NULL);
1150 : 1 : g_signal_connect (context, "launch-started",
1151 : : G_CALLBACK (on_launch_started),
1152 : : &invoked);
1153 : 1 : g_signal_connect (context, "launched",
1154 : : G_CALLBACK (on_launched),
1155 : : &launched);
1156 : 1 : g_signal_connect (context, "launch-failed",
1157 : : G_CALLBACK (on_launch_failed),
1158 : : &failed);
1159 : 1 : retval = g_desktop_app_info_launch_uris_as_manager (appinfo, NULL, context, 0,
1160 : : NULL, NULL,
1161 : : NULL, NULL,
1162 : : &error);
1163 : 1 : g_assert_no_error (error);
1164 : 1 : g_assert_true (retval);
1165 : 1 : g_assert_true (invoked);
1166 : 1 : g_assert_true (launched);
1167 : 1 : g_assert_false (failed);
1168 : :
1169 : 1 : invoked = FALSE;
1170 : 1 : launched = FALSE;
1171 : 1 : failed = FALSE;
1172 : 1 : retval = g_desktop_app_info_launch_uris_as_manager_with_fds (appinfo,
1173 : : NULL, context, 0,
1174 : : NULL, NULL,
1175 : : NULL, NULL,
1176 : : -1, -1, -1,
1177 : : &error);
1178 : 1 : g_assert_no_error (error);
1179 : 1 : g_assert_true (retval);
1180 : 1 : g_assert_true (invoked);
1181 : 1 : g_assert_true (launched);
1182 : 1 : g_assert_false (failed);
1183 : :
1184 : 1 : g_object_unref (appinfo);
1185 : 1 : g_assert_finalize_object (context);
1186 : 1 : }
1187 : :
1188 : : static void
1189 : 1 : test_launch_as_manager_fail (void)
1190 : : {
1191 : : GAppLaunchContext *context;
1192 : : GDesktopAppInfo *appinfo;
1193 : 1 : GError *error = NULL;
1194 : : gboolean retval;
1195 : : const gchar *path;
1196 : 1 : gboolean launch_started = FALSE;
1197 : 1 : gboolean launched = FALSE;
1198 : 1 : gboolean failed = FALSE;
1199 : :
1200 : 1 : g_test_summary ("Tests that launch-errors are properly handled, we force " \
1201 : : "this by using invalid FD's values when launching as manager");
1202 : :
1203 : 1 : path = g_test_get_filename (G_TEST_BUILT, "appinfo-test.desktop", NULL);
1204 : 1 : appinfo = g_desktop_app_info_new_from_filename (path);
1205 : 1 : g_assert_true (G_IS_APP_INFO (appinfo));
1206 : :
1207 : 1 : context = g_object_new (test_launch_context_get_type (), NULL);
1208 : 1 : g_signal_connect (context, "launch-started",
1209 : : G_CALLBACK (on_launch_started),
1210 : : &launch_started);
1211 : 1 : g_signal_connect (context, "launched",
1212 : : G_CALLBACK (on_launched),
1213 : : &launched);
1214 : 1 : g_signal_connect (context, "launch-failed",
1215 : : G_CALLBACK (on_launch_failed),
1216 : : &failed);
1217 : :
1218 : 1 : retval = g_desktop_app_info_launch_uris_as_manager_with_fds (appinfo,
1219 : : NULL, context, 0,
1220 : : NULL, NULL,
1221 : : NULL, NULL,
1222 : : 3000, 3001, 3002,
1223 : : &error);
1224 : 1 : g_assert_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_FAILED);
1225 : 1 : g_assert_false (retval);
1226 : 1 : g_assert_true (launch_started);
1227 : 1 : g_assert_false (launched);
1228 : 1 : g_assert_true (failed);
1229 : :
1230 : 1 : g_clear_error (&error);
1231 : 1 : g_object_unref (appinfo);
1232 : 1 : g_assert_finalize_object (context);
1233 : 1 : }
1234 : :
1235 : : static GAppInfo *
1236 : 2 : create_app_info_toucher (const char *name,
1237 : : const char *touched_file_name,
1238 : : const char *handled_type,
1239 : : char **out_file_path)
1240 : : {
1241 : 2 : GError *error = NULL;
1242 : : GAppInfo *info;
1243 : : gchar *command_line;
1244 : : gchar *file_path;
1245 : : gchar *tmpdir;
1246 : :
1247 : 2 : g_assert_nonnull (out_file_path);
1248 : :
1249 : 2 : tmpdir = g_dir_make_tmp ("desktop-app-info-launch-XXXXXX", &error);
1250 : 2 : g_assert_no_error (error);
1251 : :
1252 : 2 : file_path = g_build_filename (tmpdir, touched_file_name, NULL);
1253 : 2 : command_line = g_strdup_printf ("touch %s", file_path);
1254 : :
1255 : 2 : info = create_command_line_app_info (name, command_line, handled_type);
1256 : 2 : *out_file_path = g_steal_pointer (&file_path);
1257 : :
1258 : 2 : g_free (tmpdir);
1259 : 2 : g_free (command_line);
1260 : :
1261 : 2 : return info;
1262 : : }
1263 : :
1264 : : static void
1265 : 1 : test_default_uri_handler (void)
1266 : : {
1267 : 1 : GError *error = NULL;
1268 : 1 : gchar *file_path = NULL;
1269 : : GAppInfo *info;
1270 : :
1271 : 1 : if (skip_missing_update_desktop_database ())
1272 : 0 : return;
1273 : :
1274 : 1 : info = create_app_info_toucher ("Touch Handled", "handled",
1275 : : "x-scheme-handler/glib-touch",
1276 : : &file_path);
1277 : 1 : g_assert_true (G_IS_APP_INFO (info));
1278 : 1 : g_assert_nonnull (file_path);
1279 : :
1280 : 1 : g_assert_true (g_app_info_launch_default_for_uri ("glib-touch://touch-me",
1281 : : NULL, &error));
1282 : 1 : g_assert_no_error (error);
1283 : :
1284 : 99 : while (!g_file_test (file_path, G_FILE_TEST_IS_REGULAR));
1285 : 1 : g_assert_true (g_file_test (file_path, G_FILE_TEST_IS_REGULAR));
1286 : :
1287 : 1 : g_assert_false (g_app_info_launch_default_for_uri ("glib-INVALID-touch://touch-me",
1288 : : NULL, &error));
1289 : 1 : g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
1290 : 1 : g_clear_error (&error);
1291 : :
1292 : 1 : g_object_unref (info);
1293 : 1 : g_free (file_path);
1294 : : }
1295 : :
1296 : : static void
1297 : 1 : on_launch_default_for_uri_success_cb (GObject *object,
1298 : : GAsyncResult *result,
1299 : : gpointer user_data)
1300 : : {
1301 : 1 : GError *error = NULL;
1302 : 1 : gboolean *called = user_data;
1303 : :
1304 : 1 : g_assert_true (g_app_info_launch_default_for_uri_finish (result, &error));
1305 : 1 : g_assert_no_error (error);
1306 : :
1307 : 1 : *called = TRUE;
1308 : 1 : }
1309 : :
1310 : : static void
1311 : 1 : on_launch_default_for_uri_not_found_cb (GObject *object,
1312 : : GAsyncResult *result,
1313 : : gpointer user_data)
1314 : : {
1315 : 1 : GError *error = NULL;
1316 : 1 : GMainLoop *loop = user_data;
1317 : :
1318 : 1 : g_assert_false (g_app_info_launch_default_for_uri_finish (result, &error));
1319 : 1 : g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_SUPPORTED);
1320 : 1 : g_clear_error (&error);
1321 : :
1322 : 1 : g_main_loop_quit (loop);
1323 : 1 : }
1324 : :
1325 : : static void
1326 : 1 : on_launch_default_for_uri_cancelled_cb (GObject *object,
1327 : : GAsyncResult *result,
1328 : : gpointer user_data)
1329 : : {
1330 : 1 : GError *error = NULL;
1331 : 1 : GMainLoop *loop = user_data;
1332 : :
1333 : 1 : g_assert_false (g_app_info_launch_default_for_uri_finish (result, &error));
1334 : 1 : g_assert_error (error, G_IO_ERROR, G_IO_ERROR_CANCELLED);
1335 : 1 : g_clear_error (&error);
1336 : :
1337 : 1 : g_main_loop_quit (loop);
1338 : 1 : }
1339 : :
1340 : : static void
1341 : 1 : test_default_uri_handler_async (void)
1342 : : {
1343 : : GCancellable *cancellable;
1344 : 1 : gchar *file_path = NULL;
1345 : : GAppInfo *info;
1346 : : GMainLoop *loop;
1347 : 1 : gboolean called = FALSE;
1348 : : gint64 start_time, touch_time;
1349 : :
1350 : 1 : if (skip_missing_update_desktop_database ())
1351 : 0 : return;
1352 : :
1353 : 1 : loop = g_main_loop_new (NULL, FALSE);
1354 : 1 : info = create_app_info_toucher ("Touch Handled", "handled-async",
1355 : : "x-scheme-handler/glib-async-touch",
1356 : : &file_path);
1357 : 1 : g_assert_true (G_IS_APP_INFO (info));
1358 : 1 : g_assert_nonnull (file_path);
1359 : :
1360 : 1 : start_time = g_get_real_time ();
1361 : 1 : g_app_info_launch_default_for_uri_async ("glib-async-touch://touch-me", NULL,
1362 : : NULL,
1363 : : on_launch_default_for_uri_success_cb,
1364 : : &called);
1365 : :
1366 : 197 : while (!g_file_test (file_path, G_FILE_TEST_IS_REGULAR) || !called)
1367 : 196 : g_main_context_iteration (NULL, FALSE);
1368 : :
1369 : 1 : touch_time = g_get_real_time () - start_time;
1370 : 1 : g_assert_true (called);
1371 : 1 : g_assert_true (g_file_test (file_path, G_FILE_TEST_IS_REGULAR));
1372 : :
1373 : 1 : g_unlink (file_path);
1374 : 1 : g_assert_false (g_file_test (file_path, G_FILE_TEST_IS_REGULAR));
1375 : :
1376 : 1 : g_app_info_launch_default_for_uri_async ("glib-async-INVALID-touch://touch-me",
1377 : : NULL, NULL,
1378 : : on_launch_default_for_uri_not_found_cb,
1379 : : loop);
1380 : 1 : g_main_loop_run (loop);
1381 : :
1382 : 1 : cancellable = g_cancellable_new ();
1383 : 1 : g_app_info_launch_default_for_uri_async ("glib-async-touch://touch-me", NULL,
1384 : : cancellable,
1385 : : on_launch_default_for_uri_cancelled_cb,
1386 : : loop);
1387 : 1 : g_cancellable_cancel (cancellable);
1388 : 1 : g_main_loop_run (loop);
1389 : :
1390 : : /* If started, our touch app would take some time to actually write the
1391 : : * file to disk, so let's wait a bit here to ensure that the file isn't
1392 : : * inadvertently getting created when a launch operation is canceled up
1393 : : * front. Give it 3× as long as the successful case took, to allow for
1394 : : * some variance.
1395 : : */
1396 : 1 : g_usleep (touch_time * 3);
1397 : 1 : g_assert_false (g_file_test (file_path, G_FILE_TEST_IS_REGULAR));
1398 : :
1399 : 1 : g_object_unref (info);
1400 : 1 : g_main_loop_unref (loop);
1401 : 1 : g_free (file_path);
1402 : : }
1403 : :
1404 : : /* Test if Desktop-File Id is correctly formed */
1405 : : static void
1406 : 1 : test_id (void)
1407 : : {
1408 : : gchar *result;
1409 : :
1410 : 1 : result = run_apps ("default-for-type", "application/vnd.kde.okular-archive",
1411 : : TRUE, FALSE, NULL, NULL, NULL);
1412 : 1 : g_assert_cmpstr (result, ==, "kde4-okular.desktop\n");
1413 : 1 : g_free (result);
1414 : 1 : }
1415 : :
1416 : : static const char *
1417 : 102 : get_terminal_divider (const char *terminal_name)
1418 : : {
1419 : 102 : if (g_str_equal (terminal_name, "xdg-terminal-exec"))
1420 : 3 : return NULL;
1421 : 99 : if (g_str_equal (terminal_name, "kgx"))
1422 : 9 : return "-e";
1423 : 90 : if (g_str_equal (terminal_name, "gnome-terminal"))
1424 : 9 : return "--";
1425 : 81 : if (g_str_equal (terminal_name, "tilix"))
1426 : 9 : return "-e";
1427 : 72 : if (g_str_equal (terminal_name, "konsole"))
1428 : 9 : return "-e";
1429 : 63 : if (g_str_equal (terminal_name, "nxterm"))
1430 : 9 : return "-e";
1431 : 54 : if (g_str_equal (terminal_name, "color-xterm"))
1432 : 9 : return "-e";
1433 : 45 : if (g_str_equal (terminal_name, "rxvt"))
1434 : 9 : return "-e";
1435 : 36 : if (g_str_equal (terminal_name, "dtterm"))
1436 : 9 : return "-e";
1437 : 27 : if (g_str_equal (terminal_name, "xterm"))
1438 : 9 : return "-e";
1439 : 18 : if (g_str_equal (terminal_name, "mate-terminal"))
1440 : 9 : return "-x";
1441 : 9 : if (g_str_equal (terminal_name, "xfce4-terminal"))
1442 : 9 : return "-x";
1443 : :
1444 : : g_return_val_if_reached (NULL);
1445 : : }
1446 : :
1447 : : typedef enum {
1448 : : TERMINAL_LAUNCH_TYPE_COMMAND_LINE_WITH_PATH_OVERRIDE,
1449 : : TERMINAL_LAUNCH_TYPE_COMMAND_LINE_WITH_CONTEXT,
1450 : : TERMINAL_LAUNCH_TYPE_KEY_FILE_WITH_PATH,
1451 : : } TerminalLaunchType;
1452 : :
1453 : : typedef struct {
1454 : : const char *exec;
1455 : : TerminalLaunchType type;
1456 : : } TerminalLaunchData;
1457 : :
1458 : : static TerminalLaunchData *
1459 : 36 : terminal_launch_data_new (const char *exec, TerminalLaunchType type)
1460 : : {
1461 : 36 : TerminalLaunchData *d = NULL;
1462 : :
1463 : 36 : d = g_new0 (TerminalLaunchData, 1);
1464 : 36 : d->exec = exec;
1465 : 36 : d->type = type;
1466 : :
1467 : 36 : return d;
1468 : : }
1469 : :
1470 : : static void
1471 : 36 : test_launch_uris_with_terminal (gconstpointer data)
1472 : : {
1473 : : int fd;
1474 : : int ret;
1475 : : int flags;
1476 : : int terminal_divider_arg_length;
1477 : 36 : const TerminalLaunchData *launch_data = data;
1478 : 36 : const char *terminal_exec = launch_data->exec;
1479 : 36 : char *old_path = NULL;
1480 : : char *command_line;
1481 : : char *bin_path;
1482 : : char *terminal_path;
1483 : : char *output_fd_path;
1484 : : char *script_contents;
1485 : 36 : char *output_contents = NULL;
1486 : : char *sh;
1487 : : GAppInfo *app_info;
1488 : : GList *uris;
1489 : : GList *paths;
1490 : : GStrv output_args;
1491 : 36 : GError *error = NULL;
1492 : : GInputStream *input_stream;
1493 : : GDataInputStream *data_input_stream;
1494 : : GAppLaunchContext *launch_context;
1495 : :
1496 : 36 : sh = g_find_program_in_path ("sh");
1497 : 36 : g_assert_nonnull (sh);
1498 : :
1499 : 36 : bin_path = g_dir_make_tmp ("bin-path-XXXXXX", &error);
1500 : 36 : g_assert_no_error (error);
1501 : :
1502 : 36 : launch_context = g_object_new (test_launch_context_get_type (), NULL);
1503 : :
1504 : 36 : switch (launch_data->type)
1505 : : {
1506 : 12 : case TERMINAL_LAUNCH_TYPE_COMMAND_LINE_WITH_PATH_OVERRIDE:
1507 : 12 : old_path = g_strdup (g_getenv ("PATH"));
1508 : 12 : g_assert_true (g_setenv ("PATH", bin_path, TRUE));
1509 : 12 : break;
1510 : :
1511 : 12 : case TERMINAL_LAUNCH_TYPE_COMMAND_LINE_WITH_CONTEXT:
1512 : 12 : g_app_launch_context_setenv (launch_context, "PATH", bin_path);
1513 : 12 : break;
1514 : :
1515 : 12 : case TERMINAL_LAUNCH_TYPE_KEY_FILE_WITH_PATH:
1516 : 12 : g_app_launch_context_setenv (launch_context, "PATH", "/not/valid");
1517 : 12 : break;
1518 : :
1519 : 0 : default:
1520 : : g_assert_not_reached ();
1521 : : }
1522 : :
1523 : 36 : terminal_path = g_build_filename (bin_path, terminal_exec, NULL);
1524 : 36 : output_fd_path = g_build_filename (bin_path, "fifo", NULL);
1525 : :
1526 : 36 : ret = mkfifo (output_fd_path, 0600);
1527 : 36 : g_assert_cmpint (ret, ==, 0);
1528 : :
1529 : 36 : fd = g_open (output_fd_path, O_RDONLY | O_CLOEXEC | O_NONBLOCK, 0);
1530 : 36 : g_assert_cmpint (fd, >=, 0);
1531 : :
1532 : 36 : flags = fcntl (fd, F_GETFL);
1533 : 36 : g_assert_cmpint (flags, >=, 0);
1534 : :
1535 : 36 : ret = fcntl (fd, F_SETFL, flags & ~O_NONBLOCK);
1536 : 36 : g_assert_cmpint (ret, ==, 0);
1537 : :
1538 : 36 : input_stream = g_unix_input_stream_new (fd, TRUE);
1539 : 36 : data_input_stream = g_data_input_stream_new (input_stream);
1540 : 36 : script_contents = g_strdup_printf ("#!%s\n" \
1541 : : "out='%s'\n"
1542 : : "printf '%%s\\n' \"$*\" > \"$out\"\n",
1543 : : sh,
1544 : : output_fd_path);
1545 : 36 : g_file_set_contents (terminal_path, script_contents, -1, &error);
1546 : 36 : g_assert_no_error (error);
1547 : 36 : g_assert_cmpint (g_chmod (terminal_path, 0500), ==, 0);
1548 : :
1549 : 36 : g_test_message ("Fake '%s' terminal created as: %s", terminal_exec, terminal_path);
1550 : :
1551 : 36 : command_line = g_strdup_printf ("true %s-argument", terminal_exec);
1552 : :
1553 : 36 : if (launch_data->type == TERMINAL_LAUNCH_TYPE_KEY_FILE_WITH_PATH)
1554 : : {
1555 : : GKeyFile *key_file;
1556 : : char *key_file_contents;
1557 : 12 : const char base_file[] =
1558 : : "[Desktop Entry]\n"
1559 : : "Type=Application\n"
1560 : : "Name=terminal launched app\n"
1561 : : "Terminal=true\n"
1562 : : "Path=%s\n"
1563 : : "Exec=%s\n";
1564 : :
1565 : 12 : key_file = g_key_file_new ();
1566 : 12 : key_file_contents = g_strdup_printf (base_file, bin_path, command_line);
1567 : :
1568 : 12 : g_assert_true (
1569 : : g_key_file_load_from_data (key_file, key_file_contents, -1,
1570 : : G_KEY_FILE_NONE, NULL));
1571 : :
1572 : 12 : app_info = (GAppInfo*) g_desktop_app_info_new_from_keyfile (key_file);
1573 : 12 : g_assert_true (G_IS_DESKTOP_APP_INFO (app_info));
1574 : 12 : g_assert_true (
1575 : : g_desktop_app_info_get_boolean (G_DESKTOP_APP_INFO (app_info), "Terminal"));
1576 : :
1577 : 12 : g_key_file_unref (key_file);
1578 : 12 : g_free (key_file_contents);
1579 : : }
1580 : : else
1581 : : {
1582 : 24 : app_info = g_app_info_create_from_commandline (command_line,
1583 : : "Test App on Terminal",
1584 : : G_APP_INFO_CREATE_NEEDS_TERMINAL |
1585 : : G_APP_INFO_CREATE_SUPPORTS_URIS,
1586 : : &error);
1587 : 24 : g_assert_no_error (error);
1588 : : }
1589 : :
1590 : 36 : paths = g_list_prepend (NULL, bin_path);
1591 : 36 : uris = g_list_prepend (NULL, g_filename_to_uri (bin_path, NULL, &error));
1592 : 36 : g_assert_no_error (error);
1593 : :
1594 : 36 : paths = g_list_prepend (paths, (gpointer) g_get_user_data_dir ());
1595 : 36 : uris = g_list_append (uris, g_filename_to_uri (g_get_user_data_dir (), NULL, &error));
1596 : 36 : g_assert_no_error (error);
1597 : :
1598 : 36 : g_assert_cmpint (g_list_length (paths), ==, 2);
1599 : 36 : g_app_info_launch_uris (app_info, uris, launch_context, &error);
1600 : 36 : g_assert_no_error (error);
1601 : :
1602 : 108 : while (output_contents == NULL)
1603 : : {
1604 : 36 : output_contents =
1605 : 36 : g_data_input_stream_read_upto (data_input_stream, "\n", 1, NULL, NULL, &error);
1606 : 36 : g_assert_no_error (error);
1607 : :
1608 : 36 : if (output_contents == NULL)
1609 : 0 : g_usleep (G_USEC_PER_SEC / 10);
1610 : : }
1611 : 36 : g_test_message ("'%s' called with arguments: '%s'", terminal_exec, output_contents);
1612 : :
1613 : 36 : g_data_input_stream_read_byte (data_input_stream, NULL, &error);
1614 : 36 : g_assert_no_error (error);
1615 : :
1616 : 36 : output_args = g_strsplit (output_contents, " ", -1);
1617 : 36 : g_clear_pointer (&output_contents, g_free);
1618 : :
1619 : 36 : terminal_divider_arg_length = (get_terminal_divider (terminal_exec) != NULL) ? 1 : 0;
1620 : 36 : g_assert_cmpuint (g_strv_length (output_args), ==, 3 + terminal_divider_arg_length);
1621 : 36 : if (terminal_divider_arg_length == 1)
1622 : : {
1623 : 33 : g_assert_cmpstr (output_args[0], ==, get_terminal_divider (terminal_exec));
1624 : 33 : g_assert_cmpstr (output_args[1], ==, "true");
1625 : 33 : g_assert_cmpstr (output_args[2], ==, command_line + 5);
1626 : : }
1627 : : else
1628 : : {
1629 : 3 : g_assert_cmpstr (output_args[0], ==, "true");
1630 : 3 : g_assert_cmpstr (output_args[1], ==, command_line + 5);
1631 : : }
1632 : 36 : paths = g_list_delete_link (paths,
1633 : 36 : g_list_find_custom (paths, output_args[2 + terminal_divider_arg_length], g_str_equal));
1634 : 36 : g_assert_cmpint (g_list_length (paths), ==, 1);
1635 : 36 : g_clear_pointer (&output_args, g_strfreev);
1636 : :
1637 : 144 : while (output_contents == NULL)
1638 : : {
1639 : 72 : output_contents =
1640 : 72 : g_data_input_stream_read_upto (data_input_stream, "\n", 1, NULL, NULL, &error);
1641 : 72 : g_assert_no_error (error);
1642 : :
1643 : 72 : if (output_contents == NULL)
1644 : 36 : g_usleep (G_USEC_PER_SEC / 10);
1645 : : }
1646 : 36 : g_test_message ("'%s' called with arguments: '%s'", terminal_exec, output_contents);
1647 : :
1648 : 36 : g_data_input_stream_read_byte (data_input_stream, NULL, &error);
1649 : 36 : g_assert_no_error (error);
1650 : :
1651 : 36 : output_args = g_strsplit (output_contents, " ", -1);
1652 : 36 : g_clear_pointer (&output_contents, g_free);
1653 : 36 : g_assert_cmpuint (g_strv_length (output_args), ==, 3 + terminal_divider_arg_length);
1654 : 36 : if (terminal_divider_arg_length > 0)
1655 : : {
1656 : 33 : g_assert_cmpstr (output_args[0], ==, get_terminal_divider (terminal_exec));
1657 : 33 : g_assert_cmpstr (output_args[1], ==, "true");
1658 : 33 : g_assert_cmpstr (output_args[2], ==, command_line + 5);
1659 : : }
1660 : : else
1661 : : {
1662 : 3 : g_assert_cmpstr (output_args[0], ==, "true");
1663 : 3 : g_assert_cmpstr (output_args[1], ==, command_line + 5);
1664 : : }
1665 : 36 : paths = g_list_delete_link (paths,
1666 : 36 : g_list_find_custom (paths, output_args[2 + terminal_divider_arg_length], g_str_equal));
1667 : 36 : g_assert_cmpint (g_list_length (paths), ==, 0);
1668 : 36 : g_clear_pointer (&output_args, g_strfreev);
1669 : :
1670 : 36 : g_assert_null (paths);
1671 : :
1672 : 36 : if (launch_data->type == TERMINAL_LAUNCH_TYPE_COMMAND_LINE_WITH_PATH_OVERRIDE)
1673 : 12 : g_assert_true (g_setenv ("PATH", old_path, TRUE));
1674 : :
1675 : 36 : g_close (fd, &error);
1676 : 36 : g_assert_no_error (error);
1677 : :
1678 : 36 : g_free (sh);
1679 : 36 : g_free (command_line);
1680 : 36 : g_free (bin_path);
1681 : 36 : g_free (terminal_path);
1682 : 36 : g_free (output_fd_path);
1683 : 36 : g_free (script_contents);
1684 : 36 : g_free (old_path);
1685 : 36 : g_clear_pointer (&output_args, g_strfreev);
1686 : 36 : g_clear_pointer (&output_contents, g_free);
1687 : 36 : g_clear_object (&data_input_stream);
1688 : 36 : g_clear_object (&input_stream);
1689 : 36 : g_clear_object (&app_info);
1690 : 36 : g_clear_object (&launch_context);
1691 : 36 : g_clear_error (&error);
1692 : 36 : g_clear_list (&paths, NULL);
1693 : 36 : g_clear_list (&uris, g_free);
1694 : 36 : }
1695 : :
1696 : : static void
1697 : 1 : test_launch_uris_with_invalid_terminal (void)
1698 : : {
1699 : : char *old_path;
1700 : : char *bin_path;
1701 : : GAppInfo *app_info;
1702 : 1 : GError *error = NULL;
1703 : :
1704 : 1 : bin_path = g_dir_make_tmp ("bin-path-XXXXXX", &error);
1705 : 1 : g_assert_no_error (error);
1706 : :
1707 : 1 : old_path = g_strdup (g_getenv ("PATH"));
1708 : 1 : g_assert_true (g_setenv ("PATH", bin_path, TRUE));
1709 : :
1710 : 1 : app_info = g_app_info_create_from_commandline ("true invalid-glib-terminal",
1711 : : "Test App on Invalid Terminal",
1712 : : G_APP_INFO_CREATE_NEEDS_TERMINAL |
1713 : : G_APP_INFO_CREATE_SUPPORTS_URIS,
1714 : : &error);
1715 : 1 : g_assert_no_error (error);
1716 : :
1717 : 1 : g_app_info_launch_uris (app_info, NULL, NULL, &error);
1718 : 1 : g_assert_error (error, G_IO_ERROR, G_IO_ERROR_FAILED);
1719 : 1 : g_clear_error (&error);
1720 : :
1721 : 1 : g_assert_true (g_setenv ("PATH", old_path, TRUE));
1722 : :
1723 : 1 : g_clear_object (&app_info);
1724 : 1 : g_clear_error (&error);
1725 : 1 : g_free (bin_path);
1726 : 1 : g_free (old_path);
1727 : 1 : }
1728 : :
1729 : : static void
1730 : 1 : test_app_path (void)
1731 : : {
1732 : : GDesktopAppInfo *appinfo;
1733 : : const char *desktop_path;
1734 : :
1735 : 1 : desktop_path = g_test_get_filename (G_TEST_BUILT, "appinfo-test-path.desktop", NULL);
1736 : 1 : appinfo = g_desktop_app_info_new_from_filename (desktop_path);
1737 : :
1738 : 1 : g_assert_true (G_IS_DESKTOP_APP_INFO (appinfo));
1739 : :
1740 : 1 : g_clear_object (&appinfo);
1741 : 1 : }
1742 : :
1743 : : static void
1744 : 1 : test_app_path_wrong (void)
1745 : : {
1746 : : GKeyFile *key_file;
1747 : : GDesktopAppInfo *appinfo;
1748 : 1 : const gchar bad_try_exec_file_contents[] =
1749 : : "[Desktop Entry]\n"
1750 : : "Type=Application\n"
1751 : : "Name=appinfo-test\n"
1752 : : "TryExec=appinfo-test\n"
1753 : : "Path=this-must-not-exist‼\n"
1754 : : "Exec=true\n";
1755 : 1 : const gchar bad_exec_file_contents[] =
1756 : : "[Desktop Entry]\n"
1757 : : "Type=Application\n"
1758 : : "Name=appinfo-test\n"
1759 : : "TryExec=true\n"
1760 : : "Path=this-must-not-exist‼\n"
1761 : : "Exec=appinfo-test\n";
1762 : :
1763 : 1 : g_assert_true (
1764 : : g_file_test (g_test_get_filename (G_TEST_BUILT, "appinfo-test", NULL),
1765 : : G_FILE_TEST_IS_REGULAR | G_FILE_TEST_IS_EXECUTABLE));
1766 : :
1767 : 1 : key_file = g_key_file_new ();
1768 : :
1769 : 1 : g_assert_true (
1770 : : g_key_file_load_from_data (key_file, bad_try_exec_file_contents, -1,
1771 : : G_KEY_FILE_NONE, NULL));
1772 : :
1773 : 1 : appinfo = g_desktop_app_info_new_from_keyfile (key_file);
1774 : 1 : g_assert_false (G_IS_DESKTOP_APP_INFO (appinfo));
1775 : :
1776 : 1 : g_assert_true (
1777 : : g_key_file_load_from_data (key_file, bad_exec_file_contents, -1,
1778 : : G_KEY_FILE_NONE, NULL));
1779 : :
1780 : 1 : appinfo = g_desktop_app_info_new_from_keyfile (key_file);
1781 : 1 : g_assert_false (G_IS_DESKTOP_APP_INFO (appinfo));
1782 : :
1783 : 1 : g_clear_pointer (&key_file, g_key_file_unref);
1784 : 1 : g_clear_object (&appinfo);
1785 : 1 : }
1786 : :
1787 : : static void
1788 : 1 : test_launch_startup_notify_fail (void)
1789 : : {
1790 : : GAppInfo *app_info;
1791 : : GAppLaunchContext *context;
1792 : 1 : GError *error = NULL;
1793 : : gboolean launch_started;
1794 : : gboolean launch_failed;
1795 : : gboolean launched;
1796 : : GList *uris;
1797 : :
1798 : 1 : app_info = g_app_info_create_from_commandline ("this-must-not-exist‼",
1799 : : "failing app",
1800 : : G_APP_INFO_CREATE_NONE |
1801 : : G_APP_INFO_CREATE_SUPPORTS_STARTUP_NOTIFICATION,
1802 : : &error);
1803 : 1 : g_assert_no_error (error);
1804 : :
1805 : 1 : context = g_object_new (test_launch_context_get_type (), NULL);
1806 : 1 : g_signal_connect (context, "launch-started",
1807 : : G_CALLBACK (on_launch_started),
1808 : : &launch_started);
1809 : 1 : g_signal_connect (context, "launched",
1810 : : G_CALLBACK (on_launch_started),
1811 : : &launched);
1812 : 1 : g_signal_connect (context, "launch-failed",
1813 : : G_CALLBACK (on_launch_failed),
1814 : : &launch_failed);
1815 : :
1816 : 1 : launch_started = FALSE;
1817 : 1 : launch_failed = FALSE;
1818 : 1 : launched = FALSE;
1819 : 1 : uris = g_list_prepend (NULL, g_file_new_for_uri ("foo://bar"));
1820 : 1 : uris = g_list_prepend (uris, g_file_new_for_uri ("bar://foo"));
1821 : 1 : g_assert_false (g_app_info_launch (app_info, uris, context, &error));
1822 : 1 : g_assert_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_NOENT);
1823 : 1 : g_assert_true (launch_started);
1824 : 1 : g_assert_true (launch_failed);
1825 : 1 : g_assert_false (launched);
1826 : :
1827 : 1 : g_clear_error (&error);
1828 : 1 : g_clear_object (&app_info);
1829 : 1 : g_clear_object (&context);
1830 : 1 : g_clear_list (&uris, g_object_unref);
1831 : 1 : }
1832 : :
1833 : : static void
1834 : 1 : test_launch_fail (void)
1835 : : {
1836 : : GAppInfo *app_info;
1837 : 1 : GError *error = NULL;
1838 : :
1839 : 1 : app_info = g_app_info_create_from_commandline ("this-must-not-exist‼",
1840 : : "failing app",
1841 : : G_APP_INFO_CREATE_NONE,
1842 : : &error);
1843 : 1 : g_assert_no_error (error);
1844 : :
1845 : 1 : g_assert_false (g_app_info_launch (app_info, NULL, NULL, &error));
1846 : 1 : g_assert_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_NOENT);
1847 : :
1848 : 1 : g_clear_error (&error);
1849 : 1 : g_clear_object (&app_info);
1850 : 1 : }
1851 : :
1852 : : static void
1853 : 1 : test_launch_fail_absolute_path (void)
1854 : : {
1855 : : GAppInfo *app_info;
1856 : 1 : GError *error = NULL;
1857 : :
1858 : 1 : app_info = g_app_info_create_from_commandline ("/nothing/of/this-must-exist‼",
1859 : : NULL,
1860 : : G_APP_INFO_CREATE_NONE,
1861 : : &error);
1862 : 1 : g_assert_no_error (error);
1863 : :
1864 : 1 : g_assert_false (g_app_info_launch (app_info, NULL, NULL, &error));
1865 : 1 : g_assert_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_NOENT);
1866 : :
1867 : 1 : g_clear_error (&error);
1868 : 1 : g_clear_object (&app_info);
1869 : :
1870 : 1 : app_info = g_app_info_create_from_commandline ("/",
1871 : : NULL,
1872 : : G_APP_INFO_CREATE_NONE,
1873 : : &error);
1874 : 1 : g_assert_no_error (error);
1875 : :
1876 : 1 : g_assert_false (g_app_info_launch (app_info, NULL, NULL, &error));
1877 : 1 : g_assert_error (error, G_SPAWN_ERROR, G_SPAWN_ERROR_NOENT);
1878 : :
1879 : 1 : g_clear_error (&error);
1880 : 1 : g_clear_object (&app_info);
1881 : 1 : }
1882 : :
1883 : : static void
1884 : 1 : async_result_cb (GObject *source_object,
1885 : : GAsyncResult *result,
1886 : : gpointer user_data)
1887 : : {
1888 : 1 : GAsyncResult **result_out = user_data;
1889 : :
1890 : 1 : g_assert (*result_out == NULL);
1891 : 1 : *result_out = g_object_ref (result);
1892 : 1 : g_main_context_wakeup (g_main_context_get_thread_default ());
1893 : 1 : }
1894 : :
1895 : : static void
1896 : 1 : test_launch_fail_dbus (void)
1897 : : {
1898 : 1 : GTestDBus *bus = NULL;
1899 : 1 : GDesktopAppInfo *app_info = NULL;
1900 : 1 : GAppLaunchContext *context = NULL;
1901 : 1 : GAsyncResult *result = NULL;
1902 : 1 : GError *error = NULL;
1903 : :
1904 : 1 : if (skip_missing_dbus_daemon ())
1905 : 0 : return;
1906 : :
1907 : : /* Set up a test session bus to ensure that launching the app happens using
1908 : : * D-Bus rather than spawning. */
1909 : 1 : bus = g_test_dbus_new (G_TEST_DBUS_NONE);
1910 : 1 : g_test_dbus_up (bus);
1911 : :
1912 : 1 : app_info = g_desktop_app_info_new_from_filename (g_test_get_filename (G_TEST_DIST, "org.gtk.test.dbusappinfo.desktop", NULL));
1913 : 1 : g_assert_nonnull (app_info);
1914 : :
1915 : 1 : g_assert_true (g_desktop_app_info_has_key (app_info, "DBusActivatable"));
1916 : :
1917 : 1 : context = g_app_launch_context_new ();
1918 : :
1919 : 1 : g_app_info_launch_uris_async (G_APP_INFO (app_info), NULL, context, NULL, async_result_cb, &result);
1920 : :
1921 : 4 : while (result == NULL)
1922 : 3 : g_main_context_iteration (NULL, TRUE);
1923 : :
1924 : 1 : g_assert_false (g_app_info_launch_uris_finish (G_APP_INFO (app_info), result, &error));
1925 : 1 : g_assert_error (error, G_DBUS_ERROR, G_DBUS_ERROR_SERVICE_UNKNOWN);
1926 : :
1927 : 1 : g_test_dbus_down (bus);
1928 : 1 : g_clear_object (&bus);
1929 : :
1930 : 1 : g_clear_error (&error);
1931 : 1 : g_clear_object (&result);
1932 : 1 : g_clear_object (&context);
1933 : 1 : g_clear_object (&app_info);
1934 : : }
1935 : :
1936 : : int
1937 : 1 : main (int argc,
1938 : : char *argv[])
1939 : : {
1940 : : guint i;
1941 : 1 : const gchar *supported_terminals[] = {
1942 : : "xdg-terminal-exec",
1943 : : "kgx",
1944 : : "gnome-terminal",
1945 : : "mate-terminal",
1946 : : "xfce4-terminal",
1947 : : "tilix",
1948 : : "konsole",
1949 : : "nxterm",
1950 : : "color-xterm",
1951 : : "rxvt",
1952 : : "dtterm",
1953 : : "xterm",
1954 : : };
1955 : :
1956 : : /* While we use %G_TEST_OPTION_ISOLATE_DIRS to create temporary directories
1957 : : * for each of the tests, we want to use the system MIME registry, assuming
1958 : : * that it exists and correctly has shared-mime-info installed. */
1959 : 1 : g_content_type_set_mime_dirs (NULL);
1960 : :
1961 : 1 : g_test_init (&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL);
1962 : :
1963 : 1 : g_test_add_func ("/desktop-app-info/delete", test_delete);
1964 : 1 : g_test_add_func ("/desktop-app-info/default", test_default);
1965 : 1 : g_test_add_func ("/desktop-app-info/default-async", test_default_async);
1966 : 1 : g_test_add_func ("/desktop-app-info/fallback", test_fallback);
1967 : 1 : g_test_add_func ("/desktop-app-info/lastused", test_last_used);
1968 : 1 : g_test_add_func ("/desktop-app-info/extra-getters", test_extra_getters);
1969 : 1 : g_test_add_func ("/desktop-app-info/actions", test_actions);
1970 : 1 : g_test_add_func ("/desktop-app-info/search", test_search);
1971 : 1 : g_test_add_func ("/desktop-app-info/implements", test_implements);
1972 : 1 : g_test_add_func ("/desktop-app-info/show-in", test_show_in);
1973 : 1 : g_test_add_func ("/desktop-app-info/app-path", test_app_path);
1974 : 1 : g_test_add_func ("/desktop-app-info/app-path/wrong", test_app_path_wrong);
1975 : 1 : g_test_add_func ("/desktop-app-info/launch/fail", test_launch_fail);
1976 : 1 : g_test_add_func ("/desktop-app-info/launch/fail-absolute-path", test_launch_fail_absolute_path);
1977 : 1 : g_test_add_func ("/desktop-app-info/launch/fail-startup-notify", test_launch_startup_notify_fail);
1978 : 1 : g_test_add_func ("/desktop-app-info/launch/fail-dbus", test_launch_fail_dbus);
1979 : 1 : g_test_add_func ("/desktop-app-info/launch-as-manager", test_launch_as_manager);
1980 : 1 : g_test_add_func ("/desktop-app-info/launch-as-manager/fail", test_launch_as_manager_fail);
1981 : 1 : g_test_add_func ("/desktop-app-info/launch-default-uri-handler", test_default_uri_handler);
1982 : 1 : g_test_add_func ("/desktop-app-info/launch-default-uri-handler-async", test_default_uri_handler_async);
1983 : 1 : g_test_add_func ("/desktop-app-info/id", test_id);
1984 : :
1985 : 13 : for (i = 0; i < G_N_ELEMENTS (supported_terminals); i++)
1986 : : {
1987 : : char *path;
1988 : :
1989 : 12 : path = g_strdup_printf ("/desktop-app-info/launch-uris-with-terminal/with-path/%s",
1990 : : supported_terminals[i]);
1991 : 12 : g_test_add_data_func_full (path,
1992 : 12 : terminal_launch_data_new (supported_terminals[i],
1993 : : TERMINAL_LAUNCH_TYPE_COMMAND_LINE_WITH_PATH_OVERRIDE),
1994 : : test_launch_uris_with_terminal, g_free);
1995 : 12 : g_clear_pointer (&path, g_free);
1996 : :
1997 : 12 : path = g_strdup_printf ("/desktop-app-info/launch-uris-with-terminal/with-context/%s",
1998 : : supported_terminals[i]);
1999 : 12 : g_test_add_data_func_full (path,
2000 : 12 : terminal_launch_data_new (supported_terminals[i],
2001 : : TERMINAL_LAUNCH_TYPE_COMMAND_LINE_WITH_CONTEXT),
2002 : : test_launch_uris_with_terminal, g_free);
2003 : 12 : g_clear_pointer (&path, g_free);
2004 : :
2005 : 12 : path = g_strdup_printf ("/desktop-app-info/launch-uris-with-terminal/with-desktop-path/%s",
2006 : : supported_terminals[i]);
2007 : 12 : g_test_add_data_func_full (path,
2008 : 12 : terminal_launch_data_new (supported_terminals[i],
2009 : : TERMINAL_LAUNCH_TYPE_KEY_FILE_WITH_PATH),
2010 : : test_launch_uris_with_terminal, g_free);
2011 : 12 : g_clear_pointer (&path, g_free);
2012 : : }
2013 : :
2014 : 1 : g_test_add_func ("/desktop-app-info/launch-uris-with-terminal/invalid-glib-terminal",
2015 : : test_launch_uris_with_invalid_terminal);
2016 : :
2017 : 1 : return g_test_run ();
2018 : : }
|