[tracker-miners/sam/test-removable-devices: 2/4] Rename and add DBus setup
- From: Sam Thursfield <sthursfield src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [tracker-miners/sam/test-removable-devices: 2/4] Rename and add DBus setup
- Date: Tue, 17 Aug 2021 17:31:58 +0000 (UTC)
commit 197c6fc271a1c4abfe2ea722438e634545d1b5b8
Author: Sam Thursfield <sam afuera me uk>
Date: Tue Aug 17 18:05:39 2021 +0200
Rename and add DBus setup
tests/functional-tests/meson.build | 2 +
.../miner-removable-media/test-volume-monitor.c | 34 -----
.../miner-removable-media/test-volume-monitor.h | 9 --
.../meson.build | 2 +-
.../mockvolumemonitor/mock-volume-monitor.c | 142 +++++++++++++++++++++
.../mockvolumemonitor/mock-volume-monitor.h | 9 ++
6 files changed, 154 insertions(+), 44 deletions(-)
---
diff --git a/tests/functional-tests/meson.build b/tests/functional-tests/meson.build
index 9ed3be7b5..4838dcdaa 100644
--- a/tests/functional-tests/meson.build
+++ b/tests/functional-tests/meson.build
@@ -1,5 +1,7 @@
python = find_program('python3')
+subdir('mockvolumemonitor')
+
# Configure functional tests to run completely from source tree.
testconf = configuration_data()
diff --git a/tests/functional-tests/miner-removable-media/meson.build
b/tests/functional-tests/mockvolumemonitor/meson.build
similarity index 52%
rename from tests/functional-tests/miner-removable-media/meson.build
rename to tests/functional-tests/mockvolumemonitor/meson.build
index d037d240d..2fc734e60 100644
--- a/tests/functional-tests/miner-removable-media/meson.build
+++ b/tests/functional-tests/mockvolumemonitor/meson.build
@@ -1,3 +1,3 @@
shared_module('mockvolumemonitor',
- 'test-volume-monitor.c', 'test-volume-monitor.h',
+ 'mock-volume-monitor.c', 'mock-volume-monitor.h',
dependencies: [gio])
diff --git a/tests/functional-tests/mockvolumemonitor/mock-volume-monitor.c
b/tests/functional-tests/mockvolumemonitor/mock-volume-monitor.c
new file mode 100644
index 000000000..165b63795
--- /dev/null
+++ b/tests/functional-tests/mockvolumemonitor/mock-volume-monitor.c
@@ -0,0 +1,142 @@
+#include "mock-volume-monitor.h"
+
+struct _MockVolumeMonitor {
+ GNativeVolumeMonitor parent;
+
+ GDBusNodeInfo *node_info;
+ guint bus_token;
+ guint object_token;
+};
+
+G_DEFINE_TYPE (MockVolumeMonitor, mock_volume_monitor, G_TYPE_NATIVE_VOLUME_MONITOR);
+
+/* DBus interface
+ * --------------
+ *
+ * Used by tests to control mounts.
+ */
+
+#define BUS_NAME "org.freedesktop.Tracker.MockVolumeMonitor"
+#define BUS_PATH "/org/freedesktop/Tracker/MockVolumeMonitor"
+
+static const gchar dbus_xml[] =
+ "<node>"
+ " <interface name='org.freedesktop.Tracker.MockVolumeMonitor'>"
+ " <method name='AddMount'>"
+ " <arg type='s' name='path' direction='in' />"
+ " </method>"
+ " <method name='RemoveMount'>"
+ " <arg type='s' name='path' direction='in' />"
+ " </method>"
+ " </interface>"
+ "</node>";
+
+
+static void
+on_dbus_method_call (GDBusConnection *connection,
+ const gchar *sender,
+ const gchar *object_path,
+ const gchar *interface_name,
+ const gchar *method_name,
+ GVariant *parameters,
+ GDBusMethodInvocation *invocation,
+ gpointer user_data)
+{
+ MockVolumeMonitor *self = MOCK_VOLUME_MONITOR (user_data);
+
+ if (g_strcmp0 (method_name, "AddMount") == 0) {
+ g_message ("AddMount");
+ } else if (g_strcmp0 (method_name, "RemoveMount") == 0) {
+ g_message ("RemoveMount");
+ } else {
+ g_dbus_method_invocation_return_error (invocation,
+ G_DBUS_ERROR,
+ G_DBUS_ERROR_UNKNOWN_METHOD,
+ "Unknown method on DBus interface: '%s'", method_name);
+ }
+
+}
+
+static void
+on_bus_acquired (GDBusConnection *connection, const gchar *name, gpointer user_data)
+{
+ MockVolumeMonitor *self = MOCK_VOLUME_MONITOR (user_data);
+ GError *error = NULL;
+
+ g_message ("Publishing DBus object at " BUS_PATH);
+
+ self->node_info= g_dbus_node_info_new_for_xml (dbus_xml, &error);
+ g_assert_no_error (error);
+
+ self->object_token =
+ g_dbus_connection_register_object (connection,
+ BUS_PATH,
+ self->node_info->interfaces[0],
+ &(GDBusInterfaceVTable) { on_dbus_method_call, NULL, NULL
},
+ self,
+ NULL,
+ &error);
+}
+
+static void
+on_bus_name_acquired (GDBusConnection *connection,
+ const gchar *name,
+ gpointer user_data)
+{
+ g_message ("Acquired name: %s", name);
+}
+
+static void
+on_bus_name_lost (GDBusConnection *connection,
+ const gchar *name,
+ gpointer user_data)
+{
+ MockVolumeMonitor *self = MOCK_VOLUME_MONITOR (user_data);
+ g_message ("Lost name: %s", name);
+
+ g_dbus_connection_unregister_object (connection, self->object_token);
+ self->object_token = 0;
+}
+
+/* GVolumeMonitor implementation
+ * -----------------------------
+ *
+ * We inject fake mounts into current process by replacing the usual
+ * GNativeVolumeMonitor implementation with this.
+ */
+
+static gboolean
+is_supported (void)
+{
+ g_debug (__func__);
+ return TRUE;
+}
+
+static void mock_volume_monitor_init (MockVolumeMonitor *self) {
+ self->bus_token = g_bus_own_name (G_BUS_TYPE_SESSION, BUS_NAME, G_BUS_NAME_OWNER_FLAGS_NONE,
+ on_bus_acquired,
+ on_bus_name_acquired,
+ on_bus_name_lost,
+ g_object_ref (self),
+ g_object_unref);
+}
+
+static void mock_volume_monitor_class_init (MockVolumeMonitorClass *klass) {
+ GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
+ GVolumeMonitorClass *monitor_class = G_VOLUME_MONITOR_CLASS (klass);
+
+ monitor_class->is_supported = is_supported;
+}
+
+void g_io_module_load (GIOModule *module) {
+ g_debug (__func__);
+
+ g_type_module_use (G_TYPE_MODULE (module));
+
+ g_io_extension_point_implement (
+ G_NATIVE_VOLUME_MONITOR_EXTENSION_POINT_NAME,
+ MOCK_TYPE_VOLUME_MONITOR, "mockvolumemonitor", 0);
+}
+
+void g_io_module_unload (GIOModule *module) {
+}
diff --git a/tests/functional-tests/mockvolumemonitor/mock-volume-monitor.h
b/tests/functional-tests/mockvolumemonitor/mock-volume-monitor.h
new file mode 100644
index 000000000..563080c28
--- /dev/null
+++ b/tests/functional-tests/mockvolumemonitor/mock-volume-monitor.h
@@ -0,0 +1,9 @@
+#ifndef __MOCK_VOLUME_MONITOR_H__
+#define __MOCK_VOLUME_MONITOR_H__
+
+#include <gio/gio.h>
+
+#define MOCK_TYPE_VOLUME_MONITOR mock_volume_monitor_get_type()
+G_DECLARE_FINAL_TYPE (MockVolumeMonitor, mock_volume_monitor, MOCK, VOLUME_MONITOR, GNativeVolumeMonitor)
+
+#endif
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]