[mutter] dbus-runner: Add colord mocking



commit 4fe743e03bae2fc1687758809fc437b83675a3f9
Author: Jonas Ådahl <jadahl gmail com>
Date:   Thu Oct 28 15:35:43 2021 +0200

    dbus-runner: Add colord mocking
    
    This will be needed for adding colord integration without breaking
    testing.
    
    The test context is altered to make sure any left over color devices are
    cleaned up before starting. This means it becomes possible to run a test
    case multiple times without having to restart meta-dbus-runner.py.
    
    Note: Don't use os.getlogin() to get the current username; as that
    requires a controlling terminal.
    
    Part-of: <https://gitlab.gnome.org/GNOME/mutter/-/merge_requests/2141>

 src/tests/dbusmock-templates/colord.py | 81 ++++++++++++++++++++++++++++++++++
 src/tests/meta-backend-test.c          | 10 ++---
 src/tests/meta-context-test.c          | 26 +++++++++++
 src/tests/meta-dbus-runner.py          |  1 +
 4 files changed, 113 insertions(+), 5 deletions(-)
---
diff --git a/src/tests/dbusmock-templates/colord.py b/src/tests/dbusmock-templates/colord.py
new file mode 100644
index 0000000000..c3901dbbd3
--- /dev/null
+++ b/src/tests/dbusmock-templates/colord.py
@@ -0,0 +1,81 @@
+'''colord proxy mock template
+'''
+
+# This program 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 3 of the License, or (at your option) any
+# later version.  See http://www.gnu.org/copyleft/lgpl.html for the full text
+# of the license.
+
+__author__ = 'Jonas Ådahl'
+__copyright__ = '(c) 2021 Red Hat Inc.'
+
+import dbus
+import os
+import pwd
+from dbusmock import MOCK_IFACE
+
+
+BUS_PREFIX = 'org.freedesktop.ColorManager'
+PATH_PREFIX = '/org/freedesktop/ColorManager'
+
+BUS_NAME = BUS_PREFIX
+MAIN_OBJ = PATH_PREFIX
+MAIN_IFACE = BUS_NAME
+DEVICE_IFACE = BUS_PREFIX + '.Device'
+SYSTEM_BUS = True
+
+
+def load(mock, parameters=None):
+    mock.devices = {}
+
+def escape_unit_name(name):
+    for s in ['.', '-', '\'', ' ']:
+        name = name.replace(s, '_')
+    return name
+
+def get_username(uid):
+    return pwd.getpwuid(uid).pw_name
+
+def device_id_from_path(mock, path):
+    for device_id in mock.devices:
+        device_path = mock.devices[device_id]
+        if device_path == path:
+            return device_id
+    return None
+
+@dbus.service.method(MAIN_IFACE, in_signature='ssa{sv}', out_signature='o')
+def CreateDevice(self, device_id, scope, props):
+    uid = os.getuid()
+    username = get_username(uid)
+    device_path = PATH_PREFIX + '/devices/' + \
+        escape_unit_name(device_id) + \
+        '_' + username + '_' + str(uid)
+    self.devices[device_id] = device_path
+    self.AddObject(device_path,
+                   DEVICE_IFACE,
+                   {
+                     'DeviceId': device_id,
+                   },
+                   [])
+    self.EmitSignal(MAIN_IFACE, 'DeviceAdded', 'o', [device_path])
+    return device_path
+
+@dbus.service.method(MAIN_IFACE, in_signature='o')
+def DeleteDevice(self, device_path):
+    self.RemoveObject(device_path)
+    device_id = device_id_from_path(self, device_path)
+    del self.devices[device_id]
+    self.EmitSignal(MAIN_IFACE, 'DeviceRemoved', 'o', [device_path])
+
+
+@dbus.service.method(MAIN_IFACE, in_signature='s', out_signature='o')
+def FindDeviceById(self, device_id):
+    return self.devices[device_id]
+
+
+@dbus.service.method(MOCK_IFACE)
+def ClearDevices(self):
+    for device_path in self.devices.values():
+        self.RemoveObject(device_path)
+    self.devices = {}
diff --git a/src/tests/meta-backend-test.c b/src/tests/meta-backend-test.c
index 5769903864..6916994e4b 100644
--- a/src/tests/meta-backend-test.c
+++ b/src/tests/meta-backend-test.c
@@ -67,11 +67,6 @@ meta_backend_test_init_gpus (MetaBackendX11Nested *backend_x11_nested)
   meta_backend_add_gpu (META_BACKEND (backend_test), backend_test->gpu);
 }
 
-static void
-meta_backend_test_init (MetaBackendTest *backend_test)
-{
-}
-
 static MetaMonitorManager *
 meta_backend_test_create_monitor_manager (MetaBackend *backend,
                                           GError     **error)
@@ -175,6 +170,11 @@ meta_backend_test_remove_device (MetaBackendTest    *backend_test,
   clutter_event_free (event);
 }
 
+static void
+meta_backend_test_init (MetaBackendTest *backend_test)
+{
+}
+
 static void
 meta_backend_test_class_init (MetaBackendTestClass *klass)
 {
diff --git a/src/tests/meta-context-test.c b/src/tests/meta-context-test.c
index 5a4e48876b..f30070789f 100644
--- a/src/tests/meta-context-test.c
+++ b/src/tests/meta-context-test.c
@@ -368,4 +368,30 @@ meta_context_test_class_init (MetaContextTestClass *klass)
 static void
 meta_context_test_init (MetaContextTest *context_test)
 {
+  GDBusProxy *proxy;
+  g_autoptr (GError) error = NULL;
+  g_autoptr (GVariant) ret = NULL;
+
+  proxy =
+    g_dbus_proxy_new_for_bus_sync (G_BUS_TYPE_SYSTEM,
+                                   G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START |
+                                   G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS,
+                                   NULL,
+                                   "org.freedesktop.ColorManager",
+                                   "/org/freedesktop/ColorManager",
+                                   "org.freedesktop.DBus.Mock",
+                                   NULL, &error);
+  if (!proxy)
+    {
+      g_warning ("Failed to find mocked color manager system service, %s",
+                 error->message);
+      return;
+    }
+
+  if (!g_dbus_proxy_call_sync (proxy,
+                               "ClearDevices",
+                               NULL,
+                               G_DBUS_CALL_FLAGS_NO_AUTO_START, -1, NULL,
+                               &error))
+    g_warning ("Failed to clear mocked color devices: %s", error->message);
 }
diff --git a/src/tests/meta-dbus-runner.py b/src/tests/meta-dbus-runner.py
index 512328c352..41dcdab2dd 100755
--- a/src/tests/meta-dbus-runner.py
+++ b/src/tests/meta-dbus-runner.py
@@ -42,6 +42,7 @@ class MutterDBusTestCase(DBusTestCase):
             'meta-mocks-manager', {'templates-dir': get_templates_dir()})
 
         klass.start_from_local_template('localed')
+        klass.start_from_local_template('colord')
 
         klass.system_bus_con = klass.get_dbus(system_bus=True)
         klass.session_bus_con = klass.get_dbus(system_bus=False)


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