forked from timvideos/gst-switch
-
Notifications
You must be signed in to change notification settings - Fork 1
/
test_controller.py
360 lines (268 loc) · 11.5 KB
/
test_controller.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
import sys
import os
sys.path.insert(0, os.path.abspath(os.path.join(__file__, "../../../")))
from gstswitch.controller import Controller
from gstswitch.exception import ConnectionReturnError
import pytest
from mock import Mock, patch
from gstswitch.connection import Connection
from gi.repository import GLib
class TestAddress(object):
def test_address_null(self):
address = ['', None, [], {}]
for x in address:
with pytest.raises(ValueError):
Controller(address=x)
def test_address_colon(self):
address = 'abcdefghijk'
with pytest.raises(ValueError):
Controller(address=address)
def test_address_normal(self):
address = ['unix:abstract=gstswitch', 'unix:temp=/tmp/abcd/xyz']
for x in address:
conn = Controller(address=x)
assert conn.address == x
class TestBusName(object):
def test_normal(self):
names = ['', 'abcd', 12345]
for bus in names:
conn = Controller(bus_name=bus)
assert conn.bus_name == str(bus)
def test_normal_none(self):
name = None
conn = Controller(bus_name=name)
assert conn.bus_name == name
class TestObjectPath(object):
def test_object_path_blank(self):
paths = [None, '', {}, []]
for object_path in paths:
with pytest.raises(ValueError):
Controller(object_path=object_path)
def test_object_path_slash(self):
object_path = 'a/////////'
with pytest.raises(ValueError):
Controller(object_path=object_path)
def test_object_path_normal(self):
object_path = "/info/duzy/gst/switch/SwitchController"
conn = Controller(object_path=object_path)
assert conn.object_path == object_path
class TestInterface(object):
def test_interface_none(self):
default_interface = [None, '', [], {}]
for x in default_interface:
with pytest.raises(ValueError):
Controller(default_interface=x)
def test_interface_dot(self):
default_interface = ['.', 'info.', 'info']
for x in default_interface:
with pytest.raises(ValueError):
Controller(default_interface=x)
def test_interface_normal(self):
default_interface = "info.duzy.gst.switch.SwitchControllerInterface"
conn = Controller(default_interface=default_interface)
assert default_interface == conn.default_interface
class TestEstablishConnection(object):
def test_normal(self, monkeypatch):
monkeypatch.setattr(Connection, 'connect_dbus', Mock())
controller = Controller(address='unix:abstract=abcd')
controller.establish_connection()
assert controller.connection is not None
class MockConnection(object):
def __init__(self, mode):
self.mode = mode
def get_compose_port(self):
if self.mode is False:
return GLib.Variant('(i)', (3001,))
else:
return (0,)
def get_encode_port(self):
if self.mode is False:
return GLib.Variant('(i)', (3002,))
else:
return (0,)
def get_audio_port(self):
if self.mode is False:
return GLib.Variant('(i)', (4000,))
else:
return (0,)
def get_preview_ports(self):
if self.mode is False:
return GLib.Variant('(s)', ('[(3002, 1, 7), (3003, 1, 8)]',))
else:
return (0,)
def set_composite_mode(self, m):
if self.mode is False:
return GLib.Variant('(b)', (True,))
else:
return (False,)
def set_encode_mode(self, m):
if self.mode is False:
return GLib.Variant('(b)', (True,))
else:
return (True,)
def new_record(self):
if self.mode is False:
return GLib.Variant('(b)', (True,))
else:
return (True,)
def adjust_pip(self, xpos, ypos, width, height):
if self.mode is False:
return GLib.Variant('(u)', (1,))
else:
return (1,)
def switch(self, channel, port):
if self.mode is False:
return GLib.Variant('(b)', (True,))
else:
return (True,)
def click_video(self, xpos, ypos, width, height):
if self.mode is False:
return GLib.Variant('(b)', (True,))
else:
return (True,)
def mark_face(self, face):
pass
def mark_tracking(self, face):
pass
class TestGetComposePort(object):
def test_unpack(self):
controller = Controller(address='unix:abstract=abcdefghijk')
controller.connection = MockConnection(True)
with pytest.raises(ConnectionReturnError):
controller.get_compose_port()
def test_normal_unpack(self):
controller = Controller(address='unix:abstract=abcdef')
controller.connection = MockConnection(False)
assert controller.get_compose_port() == 3001
class TestGetEncodePort(object):
def test_unpack(self):
controller = Controller(address='unix:abstract=abcdefghijk')
controller.connection = MockConnection(True)
with pytest.raises(ConnectionReturnError):
controller.get_encode_port()
def test_normal_unpack(self):
controller = Controller(address='unix:abstract=abcdef')
controller.connection = MockConnection(False)
assert controller.get_encode_port() == 3002
class TestGetAudioPort(object):
def test_unpack(self):
controller = Controller(address='unix:abstract=abcdefghijk')
controller.connection = MockConnection(True)
with pytest.raises(ConnectionReturnError):
controller.get_audio_port()
def test_normal_unpack(self):
controller = Controller(address='unix:abstract=abcdef')
controller.connection = MockConnection(False)
assert controller.get_audio_port() == 4000
class TestGetPreviewPorts(object):
def test_unpack(self):
controller = Controller(address='unix:abstract=abcdefghijk')
controller.connection = MockConnection(True)
with pytest.raises(ConnectionReturnError):
controller.get_preview_ports()
def test_normal_unpack(self):
controller = Controller(address='unix:abstract=abcdef')
controller.connection = MockConnection(False)
controller._parse_preview_ports = Mock(return_value=[3001, 3002])
assert controller.get_preview_ports() == [3001, 3002]
class TestSetCompositeMode(object):
def test_unpack(self):
controller = Controller(address='unix:abstract=abcdefghijk')
controller.establish_connection = Mock(return_value=None)
controller.connection = MockConnection(True)
with pytest.raises(ConnectionReturnError):
controller.set_composite_mode(1)
def test_normal_unpack(self):
controller = Controller(address='unix:abstract=abcdef')
controller.establish_connection = Mock(return_value=None)
controller.connection = MockConnection(False)
assert controller.set_composite_mode(1) == True
class TestSetEncodeMode(object):
def test_unpack(self):
controller = Controller(address='unix:abstract=abcde')
controller.establish_connection = Mock(return_value=None)
controller.connection = MockConnection(True)
with pytest.raises(ConnectionReturnError):
controller.set_encode_mode(1)
def test_normal_unpack(self):
controller = Controller(address='unix:abstract=abcdef')
controller.establish_connection = Mock(return_value=None)
controller.connection = MockConnection(False)
assert controller.set_encode_mode(1) == True
class TestNewRecord(object):
def test_unpack(self):
controller = Controller(address='unix:abstract=abcde')
controller.establish_connection = Mock(return_value=None)
controller.connection = MockConnection(True)
with pytest.raises(ConnectionReturnError):
controller.new_record()
def test_normal_unpack(self):
controller = Controller(address='unix:abstract=abcdef')
controller.establish_connection = Mock(return_value=None)
controller.connection = MockConnection(False)
assert controller.new_record() == True
class TestAdjustPIP(object):
def test_unpack(self):
controller = Controller(address='unix:abstract=abcde')
controller.establish_connection = Mock(return_value=None)
controller.connection = MockConnection(True)
with pytest.raises(ConnectionReturnError):
controller.adjust_pip(1,2,3,4)
def test_normal_unpack(self):
controller = Controller(address='unix:abstract=abcdef')
controller.establish_connection = Mock(return_value=None)
controller.connection = MockConnection(False)
assert controller.adjust_pip(1,2,3,4) == 1
class TestSwitch(object):
def test_unpack(self):
controller = Controller(address='unix:abstract=abcde')
controller.establish_connection = Mock(return_value=None)
controller.connection = MockConnection(True)
with pytest.raises(ConnectionReturnError):
controller.switch(1,2)
def test_normal_unpack(self):
controller = Controller(address='unix:abstract=abcdef')
controller.establish_connection = Mock(return_value=None)
controller.connection = MockConnection(False)
assert controller.switch(1,2) == True
class TestClickVideo(object):
def test_unpack(self):
controller = Controller(address='unix:abstract=abcde')
controller.establish_connection = Mock(return_value=None)
controller.connection = MockConnection(True)
with pytest.raises(ConnectionReturnError):
controller.click_video(1,2,3,4)
def test_normal_unpack(self):
controller = Controller(address='unix:abstract=abcdef')
controller.establish_connection = Mock(return_value=None)
controller.connection = MockConnection(False)
assert controller.click_video(1,2,3,4) == True
class TestMarkFaces(object):
def test_normal(self):
controller = Controller(address='unix:abstract=abcde')
controller.establish_connection = Mock(return_value=None)
controller.connection = MockConnection(True)
face = [(1, 2, 3, 4), (1, 1, 1, 1)]
controller.mark_face(face)
class TestMarkTracking(object):
def test_normal(self):
controller = Controller(address='unix:abstract=abcde')
controller.establish_connection = Mock(return_value=None)
controller.connection = MockConnection(True)
face = [(1, 2, 3, 4), (1, 1, 1, 1)]
controller.mark_tracking(face)
class TestParsePreviewPorts(object):
def test_value_error(self):
controller = Controller(address='unix:abstract=abcde')
test = 1234
with pytest.raises(ConnectionReturnError):
controller._parse_preview_ports(test)
def test_syntax_error(self):
controller = Controller(address='unix:abstract=abcde')
test = '{}[]'
with pytest.raises(ConnectionReturnError):
controller._parse_preview_ports(test)
def test_normal(self):
controller = Controller(address='unix:abstract=abcde')
test = '[(1, 2, 3), (2, 2, 2)]'
assert controller._parse_preview_ports(test) == [1, 2]