From fac8a8c8d848101ee6f07c9a6744b372c379134a Mon Sep 17 00:00:00 2001 From: Arjan Molenaar Date: Wed, 24 Jul 2024 16:35:48 +0200 Subject: [PATCH] macos: Add test for async launcher The test brings a Finder window to the front. It's not ideal, but I have no better idea at the moment. It would be cool if we can make the test case register itself as handler for a particular uri scheme, but I have no idea how to do that. --- gio/tests/meson.build | 6 ++++ gio/tests/osx-appinfo.c | 74 +++++++++++++++++++++++++++++++++++++++++ 2 files changed, 80 insertions(+) create mode 100644 gio/tests/osx-appinfo.c diff --git a/gio/tests/meson.build b/gio/tests/meson.build index 95da6002c..f2c11e360 100644 --- a/gio/tests/meson.build +++ b/gio/tests/meson.build @@ -153,6 +153,12 @@ gio_tests = { 'win32-appinfo' : {}, } +if glib_have_cocoa + gio_tests += { + 'osx-appinfo' : {} + } +endif + if have_cxx gio_tests += { 'cxx' : { diff --git a/gio/tests/osx-appinfo.c b/gio/tests/osx-appinfo.c new file mode 100644 index 000000000..53717ac24 --- /dev/null +++ b/gio/tests/osx-appinfo.c @@ -0,0 +1,74 @@ +/* + * Copyright (C) 2024 GNOME Foundation + * Copyright (C) 2024 Arjan Molenaar + * + * SPDX-License-Identifier: LGPL-2.1-or-later + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General + * Public License along with this library; if not, see . + */ + +#include +#include + + +static void +async_result_cb (GObject *source_object, + GAsyncResult *result, + gpointer user_data) +{ + GAsyncResult **result_out = user_data; + + g_assert (*result_out == NULL); + *result_out = g_object_ref (result); + g_main_context_wakeup (g_main_context_get_thread_default ()); +} + +static void +test_launch_async (void) +{ + GAppInfo *app_info; + GAppLaunchContext *context; + GAsyncResult *result = NULL; + GError *error = NULL; + + app_info = g_app_info_get_default_for_uri_scheme("file"); + g_assert_nonnull (app_info); + g_assert_true (G_IS_OSX_APP_INFO (app_info)); + + context = g_app_launch_context_new (); + + g_app_info_launch_uris_async (G_APP_INFO (app_info), NULL, context, NULL, async_result_cb, &result); + + while (result == NULL) + g_main_context_iteration (NULL, TRUE); + + g_assert_true (g_app_info_launch_uris_finish (G_APP_INFO (app_info), result, &error)); + g_assert_no_error (error); + + g_clear_error (&error); + g_clear_object (&result); + g_clear_object (&context); + g_clear_object (&app_info); +} + +int +main (int argc, + char *argv[]) +{ + g_test_init (&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL); + + g_test_add_func ("/osx-app-info/launch-async", test_launch_async); + + return g_test_run (); +}