-
Notifications
You must be signed in to change notification settings - Fork 0
/
testBluez.py
405 lines (349 loc) · 16.1 KB
/
testBluez.py
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#!/usr/bin/python3
# Copyright (c) 2017 Johannes Leupolz
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
# of the Software, and to permit persons to whom the Software is furnished to do
# so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import asyncio, gbulb
from pydbus import SessionBus
from pydbus import SystemBus
import unittest
from pydbus.generic import signal
from gi.repository import GLib, Gio
from core import BluetoothAudioBridge
class TestFakeObjectManager():
# org.freedesktop.DBus.ObjectManager.GetManagedObjects (out DICT<OBJPATH,DICT<STRING,DICT<STRING,VARIANT>>> objpath_interfaces_and_properties);
# org.freedesktop.DBus.ObjectManager.InterfacesAdded (OBJPATH object_path, DICT<STRING,DICT<STRING,VARIANT>> interfaces_and_properties);
# org.freedesktop.DBus.ObjectManager.InterfacesRemoved (OBJPATH object_path, ARRAY<STRING> interfaces);
dbus="""
<node>
<interface name='org.freedesktop.DBus.ObjectManager'>
<method name='GetManagedObjects'>
<arg type='a{oa{sa{sv}}}' name='objpath_interfaces_and_properties' direction='out'/>
</method>
<signal name="InterfacesAdded">
<arg direction="out" type="o" name="object_path"/>
<arg direction="out" type="a{sa{sv}}" name="interfaces_and_properties"/>
</signal>
<signal name="InterfacesRemoved">
<arg direction="out" type="o" name="object_path"/>
<arg direction="out" type="as" name="interfaces"/>
</signal>
</interface>
</node>"""
def GetManagedObjects(self):
"""get managed objects"""
print("get managed objects")
result = {}
for path,obj in self.objects.items():
resObj = {}
node_info = type(obj).dbus
node_info = Gio.DBusNodeInfo.new_for_xml(node_info)
interfaces = node_info.interfaces
for interface in interfaces:
resInterface={}
for p in interface.properties:
pvalue = getattr(obj,p.name)
pvalueVariant=GLib.Variant(p.signature,pvalue)
if pvalueVariant==None:
print("could not convert value "+str(pvalue)+" of "+p.name+"("+ str(type(pvalue)) +")to a variant")
else:
resInterface[p.name]=pvalueVariant
resObj[interface.name]=resInterface
result[path]=resObj
return result
def __init__(self,bus):
self.bus=bus
self.objects = {}
self.registrations = {}
def export(self,path,obj):
newRegistration=self.bus.register_object(path,obj,None)
self.objects[path]=obj
self.registrations[path]=newRegistration
return newRegistration
def unexport(self,path):
self.registrations[path].unregister()
self.registrations.pop(path)
self.objects.pop(path)
PropertiesChanged = signal()
InterfacesAdded = signal()
InterfacesRemoved = signal()
class TestFakeDbusBluezAdapter():
dbus="""
<node>
<interface name='BluetoothAudioBridge.FakeDbusObject.Adapter1'>
<method name='StartDiscovery'>
</method>
<method name='StopDiscovery'>
</method>
<property name="Address" type="s" access="read">
</property>
</interface>
</node>"""
def StartDiscovery(self):
"""start discovery"""
print("startDiscovery")
self.fakes.TestResult=self.fakes.TestResult+1
def StopDiscovery(self):
"""stop discovery"""
print("stopDiscovery")
self.fakes.TestResult=self.fakes.TestResult+2
def __init__(self,fakes):
self._address = "initial value"
self.fakes=fakes
@property
def Address(self):
return self._address
@Address.setter
def Address(self, value):
self._address = value
self.PropertiesChanged("BluetoothAudioBridge.FakeDbusObject.Adapter1", {"Address": self._address}, [])
PropertiesChanged = signal()
class TestFakeDbusBluezDevice():
dbus="""
<node>
<interface name='BluetoothAudioBridge.FakeDbusObject.Device1'>
<method name='Connect'>
</method>
<method name='Disconnect'>
</method>
<method name='Pair'>
</method>
<property name="Connected" type="b" access="read">
</property>
<property name="Trusted" type="b" access="readwrite">
</property>
<property name="UUIDs" type="as" access="read">
</property>
</interface>
</node>"""
def Connect(self):
"""connect"""
print("connect")
self.fakes.TestResult=self.fakes.TestResult+1
def Disconnect(self):
"""disconnect"""
print("disconnect")
self.fakes.TestResult=self.fakes.TestResult+2
def Pair(self):
"""pair"""
print("pair")
self.fakes.TestResult=self.fakes.TestResult+4
def __init__(self,fakes):
self._address = "initial value"
self._connected = False
self._trusted = False
self._uuids = ['0000110b-0000-1000-8000-00805f9b34fb']
self.fakes=fakes
@property
def Address(self):
return self._address
@Address.setter
def Address(self, value):
self._address = value
self.PropertiesChanged("BluetoothAudioBridge.FakeDbusObject.Device1", {"Address": self._address}, [])
@property
def Connected(self):
return self._connected
@Connected.setter
def Connected(self, value):
print("dummy-connection: "+str(value))
self._connected = value
self.PropertiesChanged("BluetoothAudioBridge.FakeDbusObject.Device1", {"Connected": self._connected}, [])
@property
def Trusted(self):
#self.fakes.TestResult=self.fakes.TestResult+8
return self._trusted
@Trusted.setter
def Trusted(self, value):
self._trusted = value
self.PropertiesChanged("BluetoothAudioBridge.FakeDbusObject.Device1", {"Trusted": self._trusted}, [])
@property
def UUIDs(self):
return self._uuids
@UUIDs.setter
def UUIDs(self, value):
self._uuids = value
self.PropertiesChanged("BluetoothAudioBridge.FakeDbusObject.Device1", {"UUIDs": self._uuids}, [])
PropertiesChanged = signal()
class TestFakeMethods():
def __init__(self,bluetoothAudioBridge):
self.TestResult = 0
self.bluetoothAudioBridge=bluetoothAudioBridge
self.bluetoothAudioBridge.DbusBluezBusName = "BluetoothAudioBridge.FakeDbusObject"
self.bluetoothAudioBridge.DbusBluezObjectPath = "/BluetoothAudioBridge/FakeDbusObject/hci0"
self.bluetoothAudioBridge.PollingCycle = 1
self.fakeDbusDevices = []
self.fakeDbusAdapter = None
self.fakeDbusAdapterRegistration = None
self.fakeDbusObjectManager = None
self.fakeDbusObjectManagerRegistration = None
self.bus = None
self.bluetoothAudioBridge.DbusBluezOnSystemBus=False
self.busName = None
def callerWithOneParameterWasCalled(self):
def methodCall(parameter):
print("parameter "+parameter)
#print(self.TestResult)
self.TestResult=self.TestResult+1
return methodCall
def callerWithOneParameterWasCalledAsync(self):
async def methodCall(parameter):
print("parameter "+parameter)
self.TestResult=self.TestResult+1
return methodCall
async def unexportAllDevices(self):
if self.fakeDbusObjectManager:
for name,obj in self.fakeDbusDevices:
self.fakeDbusObjectManager.unexport(name)
self.fakeDbusDevices=[]
#if self.fakeDbusDevice:
# self.fakeDbusDevice.unregister()
#if self.fakeDbusObjectManager:
# self.fakeDbusObjectManager.unregister()
async def unexportDevice(self,path):
self.fakeDbusObjectManager.unexport(path)
self.fakeDbusDevices.remove(path)
async def startTestFakeDbusBluez(self):
if not self.bus:
self.bus = SessionBus()
await self.unexportAllDevices()
if self.busName:
busName.unown()
if self.fakeDbusAdapterRegistration:
self.fakeDbusAdapterRegistration.unregister()
self.fakeDbusAdapterRegistration=None
if self.fakeDbusObjectManagerRegistration:
self.fakeDbusObjectManagerRegistration.unregister()
self.fakeDbusObjectManagerRegistration=None
await asyncio.sleep(0.5)
prefix = "/"+ self.bluetoothAudioBridge.DbusBluezBusName.replace(".","/")
self.fakeDbusObjectManager = TestFakeObjectManager(self.bus)
self.fakeDbusAdapter = TestFakeDbusBluezAdapter(self)
self.fakeDbusObjectManagerRegistration=self.bus.register_object("/",self.fakeDbusObjectManager,None)
self.fakeDbusAdapterRegistration=self.fakeDbusObjectManager.export(prefix+"/hci0",self.fakeDbusAdapter)
self.busName=self.bus.request_name(self.bluetoothAudioBridge.DbusBluezBusName)
async def exportNewDevice(self,name):
prefix = "/"+ self.bluetoothAudioBridge.DbusBluezBusName.replace(".","/")
self.fakeDbusDevice = TestFakeDbusBluezDevice(self)
result = (prefix+"/hci0/dev_"+name,self.fakeDbusDevice)
self.fakeDbusObjectManager.export(result[0],result[1])
self.fakeDbusDevices.append(result)
return result
async def stopTestFakeDbusBluez(self):
await self.unexportAllDevices()
if (self.fakeDbusObjectManagerRegistration):
self.fakeDbusObjectManagerRegistration.unregister()
if (self.fakeDbusAdapterRegistration):
self.fakeDbusAdapterRegistration.unregister()
if self.busName:
self.busName.unown()
self.busName = None
self.fakeDbusDevices = []
self.fakeDbusObject = None
self.fakeDbusObjectManager = None
self.fakeDbusObjectManagerRegistration=None
self.fakeDbusAdapter = None
self.fakeDbusAdapterRegistration=None
self.bus = None
await asyncio.sleep(0.5)
async def cancelIn2Seconds(self):
await asyncio.sleep(2)
self.bluetoothAudioBridge.CancellationToken.set_result(True)
async def setResultInXSecondsCancelable(self,time):
(finished,result) = await self.bluetoothAudioBridge.awaitOrStop(asyncio.sleep(time))
if finished:
print("set Result to true")
self.TestResult=1
class TestBridge(unittest.TestCase):
def setUp(self):
gbulb.install()
self.loop=asyncio.get_event_loop()
self.bluetoothAudioBridge=BluetoothAudioBridge(self.loop)
self.fakes=TestFakeMethods(self.bluetoothAudioBridge)
def atest_startFakeObjectManager(self):
self.loop.run_until_complete(self.fakes.startTestFakeDbusBluez())
self.loop.run_until_complete(self.fakes.exportNewDevice("dev_aa_12_00_41_aa_01"))
self.loop.run_until_complete(asyncio.sleep(30))
self.loop.run_until_complete(self.fakes.stopTestFakeDbusBluez())
def test_detectMockedBluetoothDevice(self):
self.bluetoothAudioBridge.dbusBtDeviceDetected=self.fakes.callerWithOneParameterWasCalledAsync()
self.loop.run_until_complete(self.fakes.startTestFakeDbusBluez())
self.loop.run_until_complete(self.bluetoothAudioBridge.registerDbus())
self.loop.run_until_complete(asyncio.sleep(2))
self.loop.run_until_complete(self.fakes.exportNewDevice("aa_12_00_41_aa_01"))
self.loop.run_until_complete(asyncio.sleep(2))
self.loop.run_until_complete(self.bluetoothAudioBridge.unregister())
self.loop.run_until_complete(self.fakes.stopTestFakeDbusBluez())
self.assertEqual(self.fakes.TestResult,1)
def test_removeMockedBluetoothDevice(self):
self.bluetoothAudioBridge.dbusBtDeviceRemoved=self.fakes.callerWithOneParameterWasCalledAsync()
self.loop.run_until_complete(self.fakes.startTestFakeDbusBluez())
self.loop.run_until_complete(self.bluetoothAudioBridge.registerDbus())
self.loop.run_until_complete(asyncio.sleep(2))
self.loop.run_until_complete(self.fakes.exportNewDevice("aa_12_00_41_aa_01"))
self.loop.run_until_complete(asyncio.sleep(2))
self.loop.run_until_complete(self.fakes.unexportAllDevices())
self.loop.run_until_complete(asyncio.sleep(2))
self.loop.run_until_complete(self.bluetoothAudioBridge.unregister())
self.loop.run_until_complete(self.fakes.stopTestFakeDbusBluez())
self.assertEqual(self.fakes.TestResult,1)
def test_detectMockedBluetoothDeviceConnection(self):
self.bluetoothAudioBridge.dbusBtDeviceConnected=self.fakes.callerWithOneParameterWasCalledAsync()
self.loop.run_until_complete(self.fakes.startTestFakeDbusBluez())
self.loop.run_until_complete(self.bluetoothAudioBridge.registerDbus())
self.loop.run_until_complete(asyncio.sleep(2))
self.loop.run_until_complete(self.fakes.exportNewDevice("aa_12_00_41_aa_01"))
devicepath,deviceobj=self.fakes.fakeDbusDevices[0]
deviceobj.Connected=True
self.loop.run_until_complete(asyncio.sleep(2))
self.loop.run_until_complete(self.bluetoothAudioBridge.unregister())
self.loop.run_until_complete(self.fakes.stopTestFakeDbusBluez())
self.assertEqual(self.fakes.TestResult,1)
def test_detectMockedBluetoothDeviceDisconnection(self):
self.bluetoothAudioBridge.dbusBtDeviceDisconnected=self.fakes.callerWithOneParameterWasCalledAsync()
self.loop.run_until_complete(self.fakes.startTestFakeDbusBluez())
self.loop.run_until_complete(self.bluetoothAudioBridge.registerDbus())
self.loop.run_until_complete(asyncio.sleep(2))
self.loop.run_until_complete(self.fakes.exportNewDevice("aa_12_00_41_aa_01"))
devicepath,deviceobj=self.fakes.fakeDbusDevices[0]
deviceobj.Connected=True
self.loop.run_until_complete(asyncio.sleep(2))
deviceobj.Connected=False
self.loop.run_until_complete(asyncio.sleep(2))
self.loop.run_until_complete(self.bluetoothAudioBridge.unregister())
self.loop.run_until_complete(self.fakes.stopTestFakeDbusBluez())
self.assertEqual(self.fakes.TestResult,1)
def atest_listMockedDbusEntriesOnScanMessage(self):
self.loop.run_until_complete(self.fakes.startFakeBroker())
self.loop.run_until_complete(self.bluetoothAudioBridge.registerMqtt())
self.loop.run_until_complete(asyncio.sleep(2))
self.loop.run_until_complete(self.bluetoothAudioBridge.unregister())
self.loop.run_until_complete(self.fakes.stopFakeBroker())
#self.assertTrue(self.fakes.BridgeWorks)
def atest_awaitOrStop1(self):
asyncio.ensure_future(self.fakes.cancelIn2Seconds())
asyncio.ensure_future(self.fakes.setResultInXSecondsCancelable(3))
self.loop.run_until_complete(asyncio.sleep(4))
self.assertEqual(self.fakes.TestResult,0)
def atest_awaitOrStop2(self):
asyncio.ensure_future(self.fakes.cancelIn2Seconds())
asyncio.ensure_future(self.fakes.setResultInXSecondsCancelable(1))
self.loop.run_until_complete(asyncio.sleep(4))
self.assertEqual(self.fakes.TestResult,1)
if __name__ == '__main__':
unittest.main()