1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276
|
from dbus_next.service import ServiceInterface, dbus_property, method
from dbus_next.aio import MessageBus
from dbus_next import Message, MessageType, PropertyAccess, ErrorType, Variant, DBusError
import pytest
import asyncio
class ExampleInterface(ServiceInterface):
def __init__(self, name):
super().__init__(name)
self._string_prop = 'hi'
self._readonly_prop = 100
self._disabled_prop = '1234'
self._container_prop = [['hello', 'world']]
self._renamed_prop = '65'
@dbus_property()
def string_prop(self) -> 's':
return self._string_prop
@string_prop.setter
def string_prop_setter(self, val: 's'):
self._string_prop = val
@dbus_property(PropertyAccess.READ)
def readonly_prop(self) -> 't':
return self._readonly_prop
@dbus_property()
def container_prop(self) -> 'a(ss)':
return self._container_prop
@container_prop.setter
def container_prop(self, val: 'a(ss)'):
self._container_prop = val
@dbus_property(name='renamed_prop')
def original_name(self) -> 's':
return self._renamed_prop
@original_name.setter
def original_name_setter(self, val: 's'):
self._renamed_prop = val
@dbus_property(disabled=True)
def disabled_prop(self) -> 's':
return self._disabled_prop
@disabled_prop.setter
def disabled_prop(self, val: 's'):
self._disabled_prop = val
@dbus_property(disabled=True)
def throws_error(self) -> 's':
raise DBusError('test.error', 'told you so')
@throws_error.setter
def throws_error(self, val: 's'):
raise DBusError('test.error', 'told you so')
@dbus_property(PropertyAccess.READ, disabled=True)
def returns_wrong_type(self) -> 's':
return 5
@method()
def do_emit_properties_changed(self):
changed = {'string_prop': 'asdf'}
invalidated = ['container_prop']
self.emit_properties_changed(changed, invalidated)
class AsyncInterface(ServiceInterface):
def __init__(self, name):
super().__init__(name)
self._string_prop = 'hi'
self._readonly_prop = 100
self._disabled_prop = '1234'
self._container_prop = [['hello', 'world']]
self._renamed_prop = '65'
@dbus_property()
async def string_prop(self) -> 's':
return self._string_prop
@string_prop.setter
async def string_prop_setter(self, val: 's'):
self._string_prop = val
@dbus_property(PropertyAccess.READ)
async def readonly_prop(self) -> 't':
return self._readonly_prop
@dbus_property()
async def container_prop(self) -> 'a(ss)':
return self._container_prop
@container_prop.setter
async def container_prop(self, val: 'a(ss)'):
self._container_prop = val
@dbus_property(name='renamed_prop')
async def original_name(self) -> 's':
return self._renamed_prop
@original_name.setter
async def original_name_setter(self, val: 's'):
self._renamed_prop = val
@dbus_property(disabled=True)
async def disabled_prop(self) -> 's':
return self._disabled_prop
@disabled_prop.setter
async def disabled_prop(self, val: 's'):
self._disabled_prop = val
@dbus_property(disabled=True)
async def throws_error(self) -> 's':
raise DBusError('test.error', 'told you so')
@throws_error.setter
async def throws_error(self, val: 's'):
raise DBusError('test.error', 'told you so')
@dbus_property(PropertyAccess.READ, disabled=True)
async def returns_wrong_type(self) -> 's':
return 5
@method()
def do_emit_properties_changed(self):
changed = {'string_prop': 'asdf'}
invalidated = ['container_prop']
self.emit_properties_changed(changed, invalidated)
@pytest.mark.parametrize('interface_class', [ExampleInterface, AsyncInterface])
@pytest.mark.asyncio
async def test_property_methods(interface_class):
bus1 = await MessageBus().connect()
bus2 = await MessageBus().connect()
interface = interface_class('test.interface')
export_path = '/test/path'
bus1.export(export_path, interface)
async def call_properties(member, signature, body):
return await bus2.call(
Message(destination=bus1.unique_name,
path=export_path,
interface='org.freedesktop.DBus.Properties',
member=member,
signature=signature,
body=body))
result = await call_properties('GetAll', 's', [interface.name])
assert result.message_type == MessageType.METHOD_RETURN, result.body[0]
assert result.signature == 'a{sv}'
assert result.body == [{
'string_prop': Variant('s', interface._string_prop),
'readonly_prop': Variant('t', interface._readonly_prop),
'container_prop': Variant('a(ss)', interface._container_prop),
'renamed_prop': Variant('s', interface._renamed_prop)
}]
result = await call_properties('Get', 'ss', [interface.name, 'string_prop'])
assert result.message_type == MessageType.METHOD_RETURN, result.body[0]
assert result.signature == 'v'
assert result.body == [Variant('s', 'hi')]
result = await call_properties(
'Set', 'ssv',
[interface.name, 'string_prop', Variant('s', 'ho')])
assert result.message_type == MessageType.METHOD_RETURN, result.body[0]
assert interface._string_prop == 'ho'
if interface_class is AsyncInterface:
assert 'ho', await interface.string_prop()
else:
assert 'ho', interface.string_prop
result = await call_properties(
'Set', 'ssv',
[interface.name, 'readonly_prop', Variant('t', 100)])
assert result.message_type == MessageType.ERROR, result.body[0]
assert result.error_name == ErrorType.PROPERTY_READ_ONLY.value, result.body[0]
result = await call_properties(
'Set', 'ssv',
[interface.name, 'disabled_prop', Variant('s', 'asdf')])
assert result.message_type == MessageType.ERROR, result.body[0]
assert result.error_name == ErrorType.UNKNOWN_PROPERTY.value
result = await call_properties(
'Set', 'ssv',
[interface.name, 'not_a_prop', Variant('s', 'asdf')])
assert result.message_type == MessageType.ERROR, result.body[0]
assert result.error_name == ErrorType.UNKNOWN_PROPERTY.value
# wrong type
result = await call_properties('Set', 'ssv', [interface.name, 'string_prop', Variant('t', 100)])
assert result.message_type == MessageType.ERROR
assert result.error_name == ErrorType.INVALID_SIGNATURE.value
# enable the erroring properties so we can test them
for prop in ServiceInterface._get_properties(interface):
if prop.name in ['throws_error', 'returns_wrong_type']:
prop.disabled = False
result = await call_properties('Get', 'ss', [interface.name, 'returns_wrong_type'])
assert result.message_type == MessageType.ERROR, result.body[0]
assert result.error_name == ErrorType.SERVICE_ERROR.value
result = await call_properties(
'Set', 'ssv',
[interface.name, 'throws_error', Variant('s', 'ho')])
assert result.message_type == MessageType.ERROR, result.body[0]
assert result.error_name == 'test.error'
assert result.body == ['told you so']
result = await call_properties('Get', 'ss', [interface.name, 'throws_error'])
assert result.message_type == MessageType.ERROR, result.body[0]
assert result.error_name == 'test.error'
assert result.body == ['told you so']
result = await call_properties('GetAll', 's', [interface.name])
assert result.message_type == MessageType.ERROR, result.body[0]
assert result.error_name == 'test.error'
assert result.body == ['told you so']
@pytest.mark.parametrize('interface_class', [ExampleInterface, AsyncInterface])
@pytest.mark.asyncio
async def test_property_changed_signal(interface_class):
bus1 = await MessageBus().connect()
bus2 = await MessageBus().connect()
await bus2.call(
Message(destination='org.freedesktop.DBus',
path='/org/freedesktop/DBus',
interface='org.freedesktop.DBus',
member='AddMatch',
signature='s',
body=[f'sender={bus1.unique_name}']))
interface = interface_class('test.interface')
export_path = '/test/path'
bus1.export(export_path, interface)
async def wait_for_message():
# TODO timeout
future = asyncio.get_event_loop().create_future()
def message_handler(signal):
if signal.interface == 'org.freedesktop.DBus.Properties':
bus2.remove_message_handler(message_handler)
future.set_result(signal)
bus2.add_message_handler(message_handler)
return await future
bus2.send(
Message(destination=bus1.unique_name,
interface=interface.name,
path=export_path,
member='do_emit_properties_changed'))
signal = await wait_for_message()
assert signal.interface == 'org.freedesktop.DBus.Properties'
assert signal.member == 'PropertiesChanged'
assert signal.signature == 'sa{sv}as'
assert signal.body == [
interface.name, {
'string_prop': Variant('s', 'asdf')
}, ['container_prop']
]
|