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>
This commit is contained in:
parent
67e7140c25
commit
4fe743e03b
81
src/tests/dbusmock-templates/colord.py
Normal file
81
src/tests/dbusmock-templates/colord.py
Normal file
@ -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 = {}
|
@ -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)
|
||||
{
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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)
|
||||
|
Loading…
Reference in New Issue
Block a user