[glib: 3/5] gio/tests: add a test for document portal use when launching flatpaks



commit 926ff8c6a7a51db7a2e12a2eea2a56a19e43e0d0
Author: James Henstridge <james jamesh id au>
Date:   Mon Sep 16 16:30:10 2019 +0200

    gio/tests: add a test for document portal use when launching flatpaks
    
    We can't use session_bus_down() in the test since gdocumentportal.c
    holds a reference to the session bus connection, preventing it from
    being finalised.

 gio/tests/dbus-appinfo.c                           | 95 +++++++++++++++++++++-
 gio/tests/meson.build                              |  1 +
 gio/tests/org.gtk.test.dbusappinfo.flatpak.desktop |  5 ++
 3 files changed, 99 insertions(+), 2 deletions(-)
---
diff --git a/gio/tests/dbus-appinfo.c b/gio/tests/dbus-appinfo.c
index 8961a5495..54ee42602 100644
--- a/gio/tests/dbus-appinfo.c
+++ b/gio/tests/dbus-appinfo.c
@@ -276,12 +276,103 @@ test_dbus_appinfo (void)
   g_object_unref (app);
 }
 
+static void
+on_flatpak_launch_uris_finish (GObject *object,
+                               GAsyncResult *result,
+                               gpointer user_data)
+{
+  GApplication *app = user_data;
+  GError *error = NULL;
+
+  g_app_info_launch_uris_finish (G_APP_INFO (object), result, &error);
+  g_assert_no_error (error);
+
+  g_application_release (app);
+}
+
+static void
+on_flatpak_activate (GApplication *app,
+                     gpointer user_data)
+{
+  GDesktopAppInfo *flatpak_appinfo = user_data;
+  char *uri;
+  GList *uris;
+
+  /* The app will be released in on_flatpak_launch_uris_finish */
+  g_application_hold (app);
+
+  uri = g_filename_to_uri (g_desktop_app_info_get_filename (flatpak_appinfo), NULL, NULL);
+  g_assert_nonnull (uri);
+  uris = g_list_prepend (NULL, uri);
+  g_app_info_launch_uris_async (G_APP_INFO (flatpak_appinfo), uris, NULL,
+                                NULL, on_flatpak_launch_uris_finish, app);
+  g_list_free (uris);
+  g_free (uri);
+}
+
+static void
+on_flatpak_open (GApplication  *app,
+                 GFile        **files,
+                 gint           n_files,
+                 const char    *hint)
+{
+  GFile *f;
+
+  g_assert_cmpint (n_files, ==, 1);
+  g_test_message ("on_flatpak_open received file '%s'", g_file_peek_path (files[0]));
+
+  /* The file has been exported via the document portal */
+  f = g_file_new_for_uri ("file:///document-portal/document-id/org.gtk.test.dbusappinfo.flatpak.desktop");
+  g_assert_true (g_file_equal (files[0], f));
+  g_object_unref (f);
+}
+
+static void
+test_flatpak_doc_export (void)
+{
+  const gchar *argv[] = { "myapp", NULL };
+  gchar *desktop_file = NULL;
+  GDesktopAppInfo *flatpak_appinfo;
+  GApplication *app;
+  int status;
+
+  g_test_summary ("Test that files launched via Flatpak apps are made available via the document portal.");
+
+  desktop_file = g_test_build_filename (G_TEST_DIST,
+                                        "org.gtk.test.dbusappinfo.flatpak.desktop",
+                                        NULL);
+  flatpak_appinfo = g_desktop_app_info_new_from_filename (desktop_file);
+  g_assert_nonnull (flatpak_appinfo);
+  g_free (desktop_file);
+
+  app = g_application_new ("org.gtk.test.dbusappinfo.flatpak",
+                           G_APPLICATION_HANDLES_OPEN);
+  g_signal_connect (app, "activate", G_CALLBACK (on_flatpak_activate),
+                    flatpak_appinfo);
+  g_signal_connect (app, "open", G_CALLBACK (on_flatpak_open), NULL);
+
+  status = g_application_run (app, 1, (gchar **) argv);
+  g_assert_cmpint (status, ==, 0);
+
+  g_object_unref (app);
+  g_object_unref (flatpak_appinfo);
+}
+
 int
 main (int argc, char **argv)
 {
+  int ret;
+
   g_test_init (&argc, &argv, NULL);
 
   g_test_add_func ("/appinfo/dbusappinfo", test_dbus_appinfo);
-
-  return session_bus_run ();
+  g_test_add_func ("/appinfo/flatpak-doc-export", test_flatpak_doc_export);
+
+  session_bus_up ();
+  ret = g_test_run ();
+  /* gdocumentportal.c holds a reference to the session bus
+   * connection, which prevents session_bus_down from completing
+   * successfully. */
+  session_bus_stop ();
+  return ret;
 }
diff --git a/gio/tests/meson.build b/gio/tests/meson.build
index 7a664d493..224301e24 100644
--- a/gio/tests/meson.build
+++ b/gio/tests/meson.build
@@ -474,6 +474,7 @@ if installed_tests_enabled
     'appinfo-test-static.desktop',
     'file.c',
     'org.gtk.test.dbusappinfo.desktop',
+    'org.gtk.test.dbusappinfo.flatpak.desktop',
     'test1.overlay',
     install_dir : installed_tests_execdir,
   )
diff --git a/gio/tests/org.gtk.test.dbusappinfo.flatpak.desktop 
b/gio/tests/org.gtk.test.dbusappinfo.flatpak.desktop
new file mode 100644
index 000000000..9ef248a5f
--- /dev/null
+++ b/gio/tests/org.gtk.test.dbusappinfo.flatpak.desktop
@@ -0,0 +1,5 @@
+[Desktop Entry]
+Type=Application
+Name=Test
+DBusActivatable=true
+X-Flatpak=org.gtk.test.dbusappinfo.flatpak


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]