[glib: 2/5] gio/tests: add a fake implementation of the document portal



commit 27db702ceb9e3b4c612eaa7eb6502bb1e8b8fee0
Author: James Henstridge <james jamesh id au>
Date:   Mon Sep 16 14:04:30 2019 +0200

    gio/tests: add a fake implementation of the document portal

 gio/tests/fake-document-portal.c                   | 136 +++++++++++++++++++++
 gio/tests/gdbus-sessionbus.c                       |  13 ++
 gio/tests/gdbus-test-fixture.c                     |  12 +-
 gio/tests/meson.build                              |  21 +++-
 gio/tests/services/meson.build                     |  30 +++++
 .../org.freedesktop.portal.Documents.service.in    |   3 +
 6 files changed, 209 insertions(+), 6 deletions(-)
---
diff --git a/gio/tests/fake-document-portal.c b/gio/tests/fake-document-portal.c
new file mode 100644
index 000000000..bb2b4e95d
--- /dev/null
+++ b/gio/tests/fake-document-portal.c
@@ -0,0 +1,136 @@
+/*
+ * Copyright (C) 2019 Canonical Limited
+ *
+ * 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 <http://www.gnu.org/licenses/>.
+ *
+ * Authors: James Henstridge <james henstridge canonical com>
+ */
+
+/* A stub implementation of xdg-document-portal covering enough to
+ * support g_document_portal_add_documents */
+
+#include "fake-document-portal-generated.h"
+
+static gboolean
+on_handle_get_mount_point (FakeDocuments         *object,
+                           GDBusMethodInvocation *invocation,
+                           gpointer               user_data)
+{
+  fake_documents_complete_get_mount_point (object,
+                                           invocation,
+                                           "/document-portal");
+  return TRUE;
+}
+
+static gboolean
+on_handle_add_full (FakeDocuments         *object,
+                    GDBusMethodInvocation *invocation,
+                    GVariant              *o_path_fds,
+                    guint                  flags,
+                    const gchar           *app_id,
+                    const gchar           *permissions,
+                    gpointer               user_data)
+{
+  const gchar **doc_ids = NULL;
+  GVariant *extra_out = NULL;
+  gsize length, i;
+
+  length = g_variant_get_size (o_path_fds);
+  doc_ids = g_new0 (const gchar *, length);
+  for (i = 0; i < length; i++)
+    {
+      doc_ids[i] = "document-id";
+    }
+  extra_out = g_variant_new_array (G_VARIANT_TYPE ("{sv}"), NULL, 0);
+
+  fake_documents_complete_add_full (object,
+                                    invocation,
+                                    NULL,
+                                    doc_ids,
+                                    extra_out);
+
+  g_free (doc_ids);
+
+  return TRUE;
+}
+
+static void
+on_bus_acquired (GDBusConnection *connection,
+                 const gchar     *name,
+                 gpointer         user_data)
+{
+  FakeDocuments *interface;
+  GError *error = NULL;
+
+  g_test_message ("Acquired a message bus connection");
+
+  interface = fake_documents_skeleton_new ();
+  g_signal_connect (interface,
+                    "handle-get-mount-point",
+                    G_CALLBACK (on_handle_get_mount_point),
+                    NULL);
+  g_signal_connect (interface,
+                    "handle-add-full",
+                    G_CALLBACK (on_handle_add_full),
+                    NULL);
+
+  g_dbus_interface_skeleton_export (G_DBUS_INTERFACE_SKELETON (interface),
+                                    connection,
+                                    "/org/freedesktop/portal/documents",
+                                    &error);
+  g_assert_no_error (error);
+}
+
+static void
+on_name_acquired (GDBusConnection *connection,
+                  const gchar     *name,
+                  gpointer         user_data)
+{
+  g_test_message ("Acquired the name %s", name);
+}
+
+static void
+on_name_lost (GDBusConnection *connection,
+              const gchar     *name,
+              gpointer         user_data)
+{
+  g_test_message ("Lost the name %s", name);
+}
+
+
+gint
+main (gint argc, gchar *argv[])
+{
+  GMainLoop *loop;
+  guint id;
+
+  loop = g_main_loop_new (NULL, FALSE);
+
+  id = g_bus_own_name (G_BUS_TYPE_SESSION,
+                       "org.freedesktop.portal.Documents",
+                       G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT |
+                       G_BUS_NAME_OWNER_FLAGS_REPLACE,
+                       on_bus_acquired,
+                       on_name_acquired,
+                       on_name_lost,
+                       loop,
+                       NULL);
+
+  g_main_loop_run (loop);
+
+  g_bus_unown_name (id);
+  g_main_loop_unref (loop);
+
+  return 0;
+}
diff --git a/gio/tests/gdbus-sessionbus.c b/gio/tests/gdbus-sessionbus.c
index e90096973..29f05d414 100644
--- a/gio/tests/gdbus-sessionbus.c
+++ b/gio/tests/gdbus-sessionbus.c
@@ -25,8 +25,21 @@ static GTestDBus *singleton = NULL;
 void
 session_bus_up (void)
 {
+  gchar *relative, *servicesdir;
   g_assert (singleton == NULL);
   singleton = g_test_dbus_new (G_TEST_DBUS_NONE);
+
+  /* We ignore deprecations here so that gdbus-test-codegen-old can
+   * build successfully despite these two functions not being
+   * available in GLib 2.36 */
+  G_GNUC_BEGIN_IGNORE_DEPRECATIONS
+  relative = g_test_build_filename (G_TEST_BUILT, "services", NULL);
+  servicesdir = g_canonicalize_filename (relative, NULL);
+  G_GNUC_END_IGNORE_DEPRECATIONS
+  g_free (relative);
+
+  g_test_dbus_add_service_dir (singleton, servicesdir);
+  g_free (servicesdir);
   g_test_dbus_up (singleton);
 }
 
diff --git a/gio/tests/gdbus-test-fixture.c b/gio/tests/gdbus-test-fixture.c
index f8b9b93b6..2071bbad9 100644
--- a/gio/tests/gdbus-test-fixture.c
+++ b/gio/tests/gdbus-test-fixture.c
@@ -15,16 +15,20 @@ static void
 fixture_setup (TestFixture *fixture, gconstpointer unused)
 {
   GError *error = NULL;
+  gchar *relative, *servicesdir;
 
   /* Create the global dbus-daemon for this test suite
    */
   fixture->dbus = g_test_dbus_new (G_TEST_DBUS_NONE);
 
-  /* Add the private directory with our in-tree service files, 
-   * TEST_SERVICES is defined by the build system to point
-   * to the right directory.
+  /* Add the private directory with our in-tree service files.
    */
-  g_test_dbus_add_service_dir (fixture->dbus, TEST_SERVICES);
+  relative = g_test_build_filename (G_TEST_BUILT, "services", NULL);
+  servicesdir = g_canonicalize_filename (relative, NULL);
+  g_free (relative);
+
+  g_test_dbus_add_service_dir (fixture->dbus, servicesdir);
+  g_free (servicesdir);
 
   /* Start the private D-Bus daemon
    */
diff --git a/gio/tests/meson.build b/gio/tests/meson.build
index 890f7b74d..7a664d493 100644
--- a/gio/tests/meson.build
+++ b/gio/tests/meson.build
@@ -7,7 +7,6 @@ common_gio_tests_deps = [
 
 test_c_args = [
   '-DG_LOG_DOMAIN="GLib-GIO"',
-  '-DTEST_SERVICES="@0@/gio/tests/services"'.format(meson.build_root()),
   '-DGLIB_MKENUMS="@0@"'.format(glib_mkenums),
   '-DGLIB_COMPILE_SCHEMAS="@0@"'.format(glib_compile_schemas.full_path()),
   '-UG_DISABLE_ASSERT',
@@ -311,6 +310,24 @@ if host_machine.system() != 'windows'
         },
       }
     endif
+
+    fake_document_portal_generated = custom_target('fake-document-portal-generated',
+        input :   ['../org.freedesktop.portal.Documents.xml'],
+        output :  ['fake-document-portal-generated.h',
+                   'fake-document-portal-generated.c'],
+        depend_files : gdbus_codegen_built_files,
+        command : [python, gdbus_codegen,
+                   '--interface-prefix', 'org.freedesktop.portal.',
+                   '--output-directory', '@OUTDIR@',
+                   '--generate-c-code', 'fake-document-portal-generated',
+                   '--c-namespace', 'Fake',
+                   '@INPUT@'])
+
+    test_extra_programs += {
+      'fake-document-portal' : {
+        'extra_sources': fake_document_portal_generated,
+      },
+    }
   endif # have_dbus_daemon
 
   # This test is currently unreliable
@@ -781,5 +798,5 @@ if installed_tests_enabled
   )
 endif
 
-# FIXME: subdir('services')
+subdir('services')
 subdir('modules')
diff --git a/gio/tests/services/meson.build b/gio/tests/services/meson.build
new file mode 100644
index 000000000..059eeb667
--- /dev/null
+++ b/gio/tests/services/meson.build
@@ -0,0 +1,30 @@
+dbus_service_files = [
+  'org.freedesktop.portal.Documents.service',
+]
+
+srcdir_cdata = configuration_data()
+srcdir_cdata.set('installed_tests_dir', meson.current_build_dir() / '..')
+
+installed_cdata = configuration_data()
+installed_cdata.set('installed_tests_dir', installed_tests_execdir)
+
+foreach service_file : dbus_service_files
+  configure_file(
+    input: service_file + '.in',
+    output: service_file,
+    configuration: srcdir_cdata,
+  )
+  if installed_tests_enabled
+    # Build a second copy of the service file for the installed
+    # version of the tests.
+    configure_file(
+      input: service_file + '.in',
+      output: service_file + '.to-install',
+      configuration: installed_cdata,
+    )
+    install_data(meson.current_build_dir() / service_file + '.to-install',
+      install_dir: installed_tests_execdir / 'services',
+      rename: [service_file],
+    )
+  endif
+endforeach
diff --git a/gio/tests/services/org.freedesktop.portal.Documents.service.in 
b/gio/tests/services/org.freedesktop.portal.Documents.service.in
new file mode 100644
index 000000000..2769ff7b6
--- /dev/null
+++ b/gio/tests/services/org.freedesktop.portal.Documents.service.in
@@ -0,0 +1,3 @@
+[D-BUS Service]
+Name=org.freedesktop.portal.Documents
+Exec=@installed_tests_dir@/fake-document-portal


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