This repository has been archived by the owner on Apr 10, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
TestGoProManager.py
632 lines (536 loc) · 29 KB
/
TestGoProManager.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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
# Unit tests for GoProManager
import mock
from mock import call
from mock import Mock
from mock import patch
import os
from os import sys, path
import struct
import monotonic
import unittest
from pymavlink import mavutil
sys.path.append(os.path.realpath('..'))
import app_packet
import GoProManager
from GoProConstants import *
class TestStatusCallback(unittest.TestCase):
def setUp(self):
shotmgr = Mock()
self.v = Mock()
self.v.message_factory = Mock()
shotmgr.vehicle = self.v
self.mgr = GoProManager.GoProManager(shotmgr)
self.mgr.sendGoProRequest = Mock()
self.mgr.sendState = Mock()
def testStartup(self):
""" GoPro status on startup should be GOPRO_HEARTBEAT_STATUS_DISCONNECTED """
self.assertEqual( self.mgr.status, mavutil.mavlink.GOPRO_HEARTBEAT_STATUS_DISCONNECTED)
self.assertFalse( self.mgr.sendState.called )
def testConnected(self):
""" Test connected status """
message = Mock(spec=["status", "capture_mode", "flags"])
message.status = mavutil.mavlink.GOPRO_HEARTBEAT_STATUS_CONNECTED
message.capture_mode = mavutil.mavlink.GOPRO_CAPTURE_MODE_VIDEO
message.flags = 0
#message = (mavutil.mavlink.GOPRO_HEARTBEAT_STATUS_CONNECTED, mavutil.mavlink.GOPRO_CAPTURE_MODE_VIDEO, 0)
self.mgr.state_callback('vehicle','name', message)
self.assertEqual( self.mgr.status, mavutil.mavlink.GOPRO_HEARTBEAT_STATUS_CONNECTED)
self.mgr.sendState.assert_called_with()
# when we connect, we should fetch status info (check last request called)
self.mgr.sendGoProRequest.assert_called_with(mavutil.mavlink.GOPRO_COMMAND_PROTUNE_EXPOSURE)
class TestGetResponseCallback(unittest.TestCase):
def setUp(self):
shotmgr = Mock()
self.v = Mock()
shotmgr.vehicle = self.v
self.mgr = GoProManager.GoProManager(shotmgr)
self.mgr.processMsgQueue = Mock()
self.mgr.sendState = Mock()
def testCaptureResponse(self):
""" Test that capture response works """
message = (mavutil.mavlink.GOPRO_COMMAND_CAPTURE_MODE, mavutil.mavlink.GOPRO_REQUEST_SUCCESS, (CAPTURE_MODE_BURST, 0, 0, 0))
self.mgr.get_response_callback('vehicle','name', message)
self.assertEqual( self.mgr.captureMode, CAPTURE_MODE_BURST)
self.mgr.processMsgQueue.assert_called_with()
self.mgr.sendState.assert_called_with()
def testBatteryResponse(self):
""" Test that battery response works """
message = (mavutil.mavlink.GOPRO_COMMAND_BATTERY, mavutil.mavlink.GOPRO_REQUEST_SUCCESS, (72, 0, 0, 0))
self.mgr.get_response_callback('vehicle','name', message)
self.assertEqual( self.mgr.battery, 72)
self.mgr.processMsgQueue.assert_called_with()
def testModelResponse(self):
""" Test that model response works """
message = (mavutil.mavlink.GOPRO_COMMAND_MODEL, mavutil.mavlink.GOPRO_REQUEST_SUCCESS, (MODEL_HERO3PLUS_BLACK, 0, 0, 0))
self.mgr.get_response_callback('vehicle','name', message)
self.assertEqual( self.mgr.model, MODEL_HERO3PLUS_BLACK)
self.mgr.processMsgQueue.assert_called_with()
self.mgr.sendState.assert_called_with()
def testVideoSettingsResponse(self):
""" Test that the video settings response works """
message = (mavutil.mavlink.GOPRO_COMMAND_VIDEO_SETTINGS, mavutil.mavlink.GOPRO_REQUEST_SUCCESS, (mavutil.mavlink.GOPRO_RESOLUTION_1080p, mavutil.mavlink.GOPRO_FRAME_RATE_60, mavutil.mavlink.GOPRO_FIELD_OF_VIEW_WIDE, VIDEO_FORMAT_NTSC))
self.mgr.get_response_callback('vehicle','name', message)
self.assertEqual(self.mgr.videoResolution, mavutil.mavlink.GOPRO_RESOLUTION_1080p)
self.assertEqual(self.mgr.videoFrameRate, mavutil.mavlink.GOPRO_FRAME_RATE_60)
self.assertEqual(self.mgr.videoFieldOfView, mavutil.mavlink.GOPRO_FIELD_OF_VIEW_WIDE)
self.assertEqual(self.mgr.videoFormat, VIDEO_FORMAT_NTSC)
self.mgr.processMsgQueue.assert_called_with()
self.mgr.sendState.assert_called_with()
class TestSetResponseCallback(unittest.TestCase):
def setUp(self):
shotmgr = Mock()
self.v = Mock()
shotmgr.vehicle = self.v
self.mgr = GoProManager.GoProManager(shotmgr)
self.mgr.processMsgQueue = Mock()
def testPowerOnResponse(self):
""" Test that power on response works """
message = (mavutil.mavlink.GOPRO_COMMAND_POWER, mavutil.mavlink.GOPRO_REQUEST_SUCCESS)
self.mgr.set_response_callback('vehicle','name', message)
self.mgr.processMsgQueue.assert_called_with()
def testCaptureModeResponse(self):
""" Test that capture mode response works """
message = (mavutil.mavlink.GOPRO_COMMAND_CAPTURE_MODE, mavutil.mavlink.GOPRO_REQUEST_SUCCESS)
self.mgr.set_response_callback('vehicle','name', message)
self.mgr.processMsgQueue.assert_called_with()
def testShutterResponse(self):
""" Test that shutter response works """
message = (mavutil.mavlink.GOPRO_COMMAND_SHUTTER, mavutil.mavlink.GOPRO_REQUEST_SUCCESS)
self.mgr.set_response_callback('vehicle','name', message)
self.mgr.processMsgQueue.assert_called_with()
def testVideoSettingsResponse(self):
""" Test that video settings response works """
message = (mavutil.mavlink.GOPRO_COMMAND_VIDEO_SETTINGS, mavutil.mavlink.GOPRO_REQUEST_SUCCESS)
self.mgr.set_response_callback('vehicle','name', message)
self.mgr.processMsgQueue.assert_called_with()
def testVideoLowLightResponse(self):
""" Test that video protune response works """
message = (mavutil.mavlink.GOPRO_COMMAND_LOW_LIGHT, mavutil.mavlink.GOPRO_REQUEST_SUCCESS)
self.mgr.set_response_callback('vehicle','name', message)
self.mgr.processMsgQueue.assert_called_with()
def testPhotoResolutionResponse(self):
""" Test that photo resolution response works """
message = (mavutil.mavlink.GOPRO_COMMAND_PHOTO_RESOLUTION, mavutil.mavlink.GOPRO_REQUEST_SUCCESS)
self.mgr.set_response_callback('vehicle','name', message)
self.mgr.processMsgQueue.assert_called_with()
def testPhotoBurstRateResponse(self):
""" Test that photo burst rate response works """
message = (mavutil.mavlink.GOPRO_COMMAND_PHOTO_BURST_RATE, mavutil.mavlink.GOPRO_REQUEST_SUCCESS)
self.mgr.set_response_callback('vehicle','name', message)
self.mgr.processMsgQueue.assert_called_with()
def testVideoProtuneResponse(self):
""" Test that video protune response works """
message = (mavutil.mavlink.GOPRO_COMMAND_PROTUNE, mavutil.mavlink.GOPRO_REQUEST_SUCCESS)
self.mgr.set_response_callback('vehicle','name', message)
self.mgr.processMsgQueue.assert_called_with()
def testVideoProtuneWhiteBalanceResponse(self):
""" Test that video protune white balance response works """
message = (mavutil.mavlink.GOPRO_COMMAND_PROTUNE_WHITE_BALANCE, mavutil.mavlink.GOPRO_REQUEST_SUCCESS)
self.mgr.set_response_callback('vehicle','name', message)
self.mgr.processMsgQueue.assert_called_with()
def testVideoProtuneColorResponse(self):
""" Test that video protune color response works """
message = (mavutil.mavlink.GOPRO_COMMAND_PROTUNE_COLOUR, mavutil.mavlink.GOPRO_REQUEST_SUCCESS)
self.mgr.set_response_callback('vehicle','name', message)
self.mgr.processMsgQueue.assert_called_with()
def testVideoProtuneGainResponse(self):
""" Test that video protune gain response works """
message = (mavutil.mavlink.GOPRO_COMMAND_PROTUNE_GAIN, mavutil.mavlink.GOPRO_REQUEST_SUCCESS)
self.mgr.set_response_callback('vehicle','name', message)
self.mgr.processMsgQueue.assert_called_with()
def testVideoProtuneSharpnessResponse(self):
""" Test that video protune sharpness response works """
message = (mavutil.mavlink.GOPRO_COMMAND_PROTUNE_SHARPNESS, mavutil.mavlink.GOPRO_REQUEST_SUCCESS)
self.mgr.set_response_callback('vehicle','name', message)
self.mgr.processMsgQueue.assert_called_with()
def testVideoProtuneExposureResponse(self):
""" Test that video protune exposure response works """
message = (mavutil.mavlink.GOPRO_COMMAND_PROTUNE_EXPOSURE, mavutil.mavlink.GOPRO_REQUEST_SUCCESS)
self.mgr.set_response_callback('vehicle','name', message)
self.mgr.processMsgQueue.assert_called_with()
def testFailedResponse(self):
""" Test that failed request sends state back to client """
self.mgr.sendState = Mock()
message = (mavutil.mavlink.GOPRO_COMMAND_VIDEO_SETTINGS, mavutil.mavlink.GOPRO_REQUEST_FAILED)
self.mgr.set_response_callback('vehicle','name', message)
self.mgr.sendState.assert_called_with()
self.mgr.processMsgQueue.assert_called_with()
class TestSendGoProRequest(unittest.TestCase):
def setUp(self):
shotmgr = Mock()
self.v = Mock()
self.v.message_factory = Mock()
shotmgr.vehicle = self.v
self.mgr = GoProManager.GoProManager(shotmgr)
self.mgr.queueMsg = Mock()
def testSendBattery(self):
""" Test sending a request to the Gopro for its battery life """
self.v.message_factory.gopro_get_request_encode.return_value = 7
self.mgr.sendGoProRequest(mavutil.mavlink.GOPRO_COMMAND_BATTERY)
self.v.message_factory.gopro_get_request_encode.assert_called_with(0, mavutil.mavlink.MAV_COMP_ID_GIMBAL,
mavutil.mavlink.GOPRO_COMMAND_BATTERY)
self.mgr.queueMsg.assert_called_with(7)
def testInvalidRequest(self):
""" Test sending an invalid request """
self.mgr.sendGoProRequest(141)
self.assertFalse(self.v.message_factory.gopro_set_request_encode.called)
class TestSendGoProCommand(unittest.TestCase):
def setUp(self):
shotmgr = Mock()
self.v = Mock()
self.v.message_factory = Mock()
shotmgr.vehicle = self.v
self.mgr = GoProManager.GoProManager(shotmgr)
self.mgr.queueMsg = Mock()
def testSendModeChange(self):
""" If we send a mode change command, we need to follow it up with a mode request to make sure it worked """
self.mgr.sendGoProRequest = Mock()
self.v.message_factory.gopro_set_request_encode.return_value = 3
self.mgr.sendGoProCommand(mavutil.mavlink.GOPRO_COMMAND_CAPTURE_MODE, (1, 0, 0, 0))
self.v.message_factory.gopro_set_request_encode.assert_called_with(0, mavutil.mavlink.MAV_COMP_ID_GIMBAL,
mavutil.mavlink.GOPRO_COMMAND_CAPTURE_MODE, (1, 0, 0, 0))
self.mgr.queueMsg.assert_called_with(3)
def testSendShutterTo1(self):
""" Test sending a shutter command with value 1 """
self.v.message_factory.gopro_set_request_encode.return_value = 3
self.mgr.sendGoProCommand(mavutil.mavlink.GOPRO_COMMAND_SHUTTER, (1, 0, 0, 0))
self.v.message_factory.gopro_set_request_encode.assert_called_with(0, mavutil.mavlink.MAV_COMP_ID_GIMBAL,
mavutil.mavlink.GOPRO_COMMAND_SHUTTER, (1, 0, 0, 0))
self.mgr.queueMsg.assert_called_with(3)
def testSendVideoSettingsChange(self):
""" Test sending a video settings command """
self.mgr.sendGoProRequest = Mock()
self.v.message_factory.gopro_set_request_encode.return_value = 3
self.mgr.sendGoProCommand(mavutil.mavlink.GOPRO_COMMAND_VIDEO_SETTINGS, (0, 3, 7, 1))
self.v.message_factory.gopro_set_request_encode.assert_called_with(0, mavutil.mavlink.MAV_COMP_ID_GIMBAL,
mavutil.mavlink.GOPRO_COMMAND_VIDEO_SETTINGS, (0, 3, 7, 1))
self.mgr.queueMsg.assert_called_with(3)
self.mgr.sendGoProRequest.assert_called_with(mavutil.mavlink.GOPRO_COMMAND_VIDEO_SETTINGS)
def testSendVideoLowLight(self):
""" Test sending a video low light command """
self.mgr.sendGoProRequest = Mock()
self.v.message_factory.gopro_set_request_encode.return_value = 3
self.mgr.sendGoProCommand(mavutil.mavlink.GOPRO_COMMAND_LOW_LIGHT, (1, 0, 0, 0))
self.v.message_factory.gopro_set_request_encode.assert_called_with(0, mavutil.mavlink.MAV_COMP_ID_GIMBAL,
mavutil.mavlink.GOPRO_COMMAND_LOW_LIGHT, (1, 0, 0, 0))
self.mgr.queueMsg.assert_called_with(3)
self.mgr.sendGoProRequest.assert_called_with(mavutil.mavlink.GOPRO_COMMAND_LOW_LIGHT)
def testSendPhotoResolution(self):
""" Test sending a photo resolution command """
self.mgr.sendGoProRequest = Mock()
self.v.message_factory.gopro_set_request_encode.return_value = 3
self.mgr.sendGoProCommand(mavutil.mavlink.GOPRO_COMMAND_PHOTO_RESOLUTION, (1, 0, 0, 0))
self.v.message_factory.gopro_set_request_encode.assert_called_with(0, mavutil.mavlink.MAV_COMP_ID_GIMBAL,
mavutil.mavlink.GOPRO_COMMAND_PHOTO_RESOLUTION, (1, 0, 0, 0))
self.mgr.queueMsg.assert_called_with(3)
self.mgr.sendGoProRequest.assert_called_with(mavutil.mavlink.GOPRO_COMMAND_PHOTO_RESOLUTION)
def testSendPhotoBurstRate(self):
""" Test sending a photo burst rate command """
self.mgr.sendGoProRequest = Mock()
self.mgr.sendGoProRequest = Mock()
self.v.message_factory.gopro_set_request_encode.return_value = 3
self.mgr.sendGoProCommand(mavutil.mavlink.GOPRO_COMMAND_PHOTO_BURST_RATE, (1, 0, 0, 0))
self.v.message_factory.gopro_set_request_encode.assert_called_with(0, mavutil.mavlink.MAV_COMP_ID_GIMBAL,
mavutil.mavlink.GOPRO_COMMAND_PHOTO_BURST_RATE, (1, 0, 0, 0))
self.mgr.queueMsg.assert_called_with(3)
self.mgr.sendGoProRequest.assert_called_with(mavutil.mavlink.GOPRO_COMMAND_PHOTO_BURST_RATE)
def testSendVideoProtune(self):
""" Test sending a video protune command """
self.mgr.sendGoProRequest = Mock()
self.v.message_factory.gopro_set_request_encode.return_value = 3
self.mgr.sendGoProCommand(mavutil.mavlink.GOPRO_COMMAND_PROTUNE, (1, 0, 0, 0))
self.v.message_factory.gopro_set_request_encode.assert_called_with(0, mavutil.mavlink.MAV_COMP_ID_GIMBAL,
mavutil.mavlink.GOPRO_COMMAND_PROTUNE, (1, 0, 0, 0))
self.mgr.queueMsg.assert_called_with(3)
self.mgr.sendGoProRequest.assert_called_with(mavutil.mavlink.GOPRO_COMMAND_PROTUNE)
def testSendVideoProtuneWhiteBalance(self):
""" Test sending a video protune white balance command """
self.mgr.sendGoProRequest = Mock()
self.v.message_factory.gopro_set_request_encode.return_value = 3
self.mgr.sendGoProCommand(mavutil.mavlink.GOPRO_COMMAND_PROTUNE_WHITE_BALANCE, (1, 0, 0, 0))
self.v.message_factory.gopro_set_request_encode.assert_called_with(0, mavutil.mavlink.MAV_COMP_ID_GIMBAL,
mavutil.mavlink.GOPRO_COMMAND_PROTUNE_WHITE_BALANCE, (1, 0, 0, 0))
self.mgr.queueMsg.assert_called_with(3)
self.mgr.sendGoProRequest.assert_called_with(mavutil.mavlink.GOPRO_COMMAND_PROTUNE_WHITE_BALANCE)
def testSendVideoProtuneColor(self):
""" Test sending a video protune color command """
self.mgr.sendGoProRequest = Mock()
self.v.message_factory.gopro_set_request_encode.return_value = 3
self.mgr.sendGoProCommand(mavutil.mavlink.GOPRO_COMMAND_PROTUNE_COLOUR, (1, 0, 0, 0))
self.v.message_factory.gopro_set_request_encode.assert_called_with(0, mavutil.mavlink.MAV_COMP_ID_GIMBAL,
mavutil.mavlink.GOPRO_COMMAND_PROTUNE_COLOUR, (1, 0, 0, 0))
self.mgr.queueMsg.assert_called_with(3)
self.mgr.sendGoProRequest.assert_called_with(mavutil.mavlink.GOPRO_COMMAND_PROTUNE_COLOUR)
def testSendVideoProtuneGain(self):
""" Test sending a video protune gain command """
self.mgr.sendGoProRequest = Mock()
self.v.message_factory.gopro_set_request_encode.return_value = 3
self.mgr.sendGoProCommand(mavutil.mavlink.GOPRO_COMMAND_PROTUNE_GAIN, (1, 0, 0, 0))
self.v.message_factory.gopro_set_request_encode.assert_called_with(0, mavutil.mavlink.MAV_COMP_ID_GIMBAL,
mavutil.mavlink.GOPRO_COMMAND_PROTUNE_GAIN, (1, 0, 0, 0))
self.mgr.queueMsg.assert_called_with(3)
self.mgr.sendGoProRequest.assert_called_with(mavutil.mavlink.GOPRO_COMMAND_PROTUNE_GAIN)
def testSendVideoProtuneSharpness(self):
""" Test sending a video protune sharpness command """
self.mgr.sendGoProRequest = Mock()
self.v.message_factory.gopro_set_request_encode.return_value = 3
self.mgr.sendGoProCommand(mavutil.mavlink.GOPRO_COMMAND_PROTUNE_SHARPNESS, (1, 0, 0, 0))
self.v.message_factory.gopro_set_request_encode.assert_called_with(0, mavutil.mavlink.MAV_COMP_ID_GIMBAL,
mavutil.mavlink.GOPRO_COMMAND_PROTUNE_SHARPNESS, (1, 0, 0, 0))
self.mgr.queueMsg.assert_called_with(3)
self.mgr.sendGoProRequest.assert_called_with(mavutil.mavlink.GOPRO_COMMAND_PROTUNE_SHARPNESS)
def testSendVideoProtuneExposure(self):
""" Test sending a video protune exposure command """
self.mgr.sendGoProRequest = Mock()
self.v.message_factory.gopro_set_request_encode.return_value = 3
self.mgr.sendGoProCommand(mavutil.mavlink.GOPRO_COMMAND_PROTUNE_EXPOSURE, (1, 0, 0, 0))
self.v.message_factory.gopro_set_request_encode.assert_called_with(0, mavutil.mavlink.MAV_COMP_ID_GIMBAL,
mavutil.mavlink.GOPRO_COMMAND_PROTUNE_EXPOSURE, (1, 0, 0, 0))
self.mgr.queueMsg.assert_called_with(3)
self.mgr.sendGoProRequest.assert_called_with(mavutil.mavlink.GOPRO_COMMAND_PROTUNE_EXPOSURE)
def testInvalidCommand(self):
""" Test sending an invalid command """
self.mgr.sendGoProCommand(140, (1, 0, 0, 0))
self.assertFalse(self.v.message_factory.gopro_set_request_encode.called)
class TestHandleRecordCommand(unittest.TestCase):
def setUp(self):
shotmgr = Mock()
self.v = Mock()
self.v.message_factory = Mock()
shotmgr.vehicle = self.v
self.mgr = GoProManager.GoProManager(shotmgr)
self.mgr.status = mavutil.mavlink.GOPRO_HEARTBEAT_STATUS_CONNECTED
self.mgr.sendGoProCommand = Mock()
self.mgr.sendState = Mock()
def testStartRecord(self):
""" Test starting video recording """
self.mgr.handleRecordCommand( CAPTURE_MODE_VIDEO, RECORD_COMMAND_START )
self.mgr.sendGoProCommand.assert_called_with(mavutil.mavlink.GOPRO_COMMAND_SHUTTER, (1, 0, 0, 0))
def testStartRecordAlreadyRecording(self):
""" Test starting video recording while already recording """
self.mgr.isRecording = True
self.mgr.captureMode = CAPTURE_MODE_VIDEO
self.mgr.handleRecordCommand( CAPTURE_MODE_VIDEO, RECORD_COMMAND_START )
self.assertTrue(self.mgr.isRecording)
assert not self.mgr.sendGoProCommand.called
assert not self.mgr.sendState.called
def testSwitchToStillsAndTakeAStill(self):
""" Should switch modes to still and take a still """
self.mgr.captureMode = CAPTURE_MODE_VIDEO
self.mgr.handleRecordCommand( CAPTURE_MODE_PHOTO, RECORD_COMMAND_START )
self.assertFalse(self.mgr.isRecording)
call1 = call(mavutil.mavlink.GOPRO_COMMAND_CAPTURE_MODE, (CAPTURE_MODE_PHOTO, 0, 0, 0))
call2 = call(mavutil.mavlink.GOPRO_COMMAND_SHUTTER, (1, 0, 0, 0))
self.mgr.sendGoProCommand.assert_has_calls( [call1, call2] )
def testToggleStartRecord(self):
""" If we're not recording, start recording """
self.mgr.captureMode = CAPTURE_MODE_VIDEO
self.mgr.handleRecordCommand( CAPTURE_MODE_VIDEO, RECORD_COMMAND_TOGGLE )
self.mgr.sendGoProCommand.assert_called_with(mavutil.mavlink.GOPRO_COMMAND_SHUTTER, (1, 0, 0, 0))
def testToggleStopRecord(self):
""" If we're recording, stop recording """
self.mgr.captureMode = CAPTURE_MODE_VIDEO
self.mgr.isRecording = True
self.mgr.handleRecordCommand( CAPTURE_MODE_VIDEO, RECORD_COMMAND_TOGGLE )
self.mgr.sendGoProCommand.assert_called_with(mavutil.mavlink.GOPRO_COMMAND_SHUTTER, (0, 0, 0, 0))
def testStartBurstShot(self):
'''If GoPro is in burst shot mode then record button takes a burst shot'''
self.mgr.captureMode = CAPTURE_MODE_BURST
self.mgr.handleRecordCommand( CAPTURE_MODE_BURST, RECORD_COMMAND_START )
self.mgr.sendGoProCommand.assert_called_with(mavutil.mavlink.GOPRO_COMMAND_SHUTTER, (1, 0, 0, 0))
def testWrongMode(self):
""" Don't do anything if we're not in the right mode """
self.mgr.status = mavutil.mavlink.GOPRO_HEARTBEAT_STATUS_DISCONNECTED
self.mgr.handleRecordCommand( CAPTURE_MODE_VIDEO, RECORD_COMMAND_TOGGLE )
self.assertFalse(self.mgr.sendGoProCommand.called)
class TestQueueMsg(unittest.TestCase):
def setUp(self):
shotmgr = Mock()
self.v = Mock()
shotmgr.vehicle = self.v
self.mgr = GoProManager.GoProManager(shotmgr)
def testQueueMsg(self):
""" Test queuing up a message """
self.mgr.isGoproBusy = True
self.mgr.lastRequestSent = monotonic.monotonic()
self.mgr.queueMsg(4)
self.assertFalse( self.mgr.msgQueue.empty() )
self.assertTrue(self.mgr.isGoproBusy)
def testQueueMultiMsg(self):
""" Test queuing up multiple messages """
for i in range(10):
self.mgr.queueMsg(i)
self.assertEqual( self.mgr.msgQueue.qsize(), 9)
def testQueueSend(self):
""" if the queue is not busy, we start sending a message instead of queuing up """
self.mgr.queueMsg(37)
self.assertTrue( self.mgr.msgQueue.empty() )
self.v.send_mavlink.assert_called_with(37)
def testQueueFlushQueue(self):
""" Test queue is flushed with outstanding message sent more than 2 seconds ago """
self.mgr.sendState = Mock()
self.mgr.isGoproBusy = True
self.mgr.lastRequestSent = monotonic.monotonic()
self.mgr.queueMsg(1)
self.mgr.queueMsg(2)
self.assertEqual(self.mgr.msgQueue.qsize(), 2)
self.mgr.lastRequestSent = monotonic.monotonic() - 3.0
self.mgr.queueMsg(3)
self.assertTrue(self.mgr.msgQueue.empty)
self.mgr.sendState.assert_called_with()
class TestProcessMsgQueue(unittest.TestCase):
def setUp(self):
shotmgr = Mock()
self.v = Mock()
shotmgr.vehicle = self.v
self.mgr = GoProManager.GoProManager(shotmgr)
def testSendNextMessage(self):
""" send the next message in the queue """
self.mgr.isGoproBusy = True
self.mgr.lastRequestSent = monotonic.monotonic()
self.mgr.queueMsg(3)
self.mgr.queueMsg(2)
self.mgr.queueMsg(1)
self.mgr.processMsgQueue()
self.v.send_mavlink.assert_called_with(3)
self.assertEqual( self.mgr.msgQueue.qsize(), 2)
def testQueueisEmpty(self):
""" if our queue is empty, set ourselves to not busy """
self.mgr.isGoproBusy = True
self.mgr.processMsgQueue()
self.assertFalse( self.mgr.isGoproBusy )
class TestHandlePacket(unittest.TestCase):
def setUp(self):
shotmgr = Mock()
self.mgr = GoProManager.GoProManager(shotmgr)
def testEnableGoPro(self):
""" Should enable gopro commands """
pkt = struct.pack('<I', 1)
self.mgr.setGoProEnabled = Mock()
self.mgr.handlePacket(app_packet.GOPRO_SET_ENABLED, pkt)
self.mgr.setGoProEnabled.assert_called_with(True)
def testSetRequest(self):
""" Handle a set request """
self.mgr.sendGoProCommand = Mock()
value = struct.pack('<HH', 8, 22)
self.mgr.handlePacket(app_packet.GOPRO_SET_REQUEST, value)
self.mgr.sendGoProCommand.assert_called_with( 8, (22, 0, 0, 0) )
def testRecord(self):
""" Handle a record packet """
self.mgr.handleRecordCommand = Mock()
self.mgr.captureMode = CAPTURE_MODE_VIDEO
value = struct.pack('<I', 1)
self.mgr.handlePacket(app_packet.GOPRO_RECORD, value)
self.mgr.handleRecordCommand.assert_called_with( CAPTURE_MODE_VIDEO, 1 )
def testStateRequest(self):
""" Should send state on request """
pkt = struct.pack('<')
self.mgr.sendState = Mock()
self.mgr.handlePacket(app_packet.GOPRO_REQUEST_STATE, pkt)
self.mgr.sendState.assert_called_with()
def testSetExtendedRequest(self):
""" Handle extended payload settings request """
self.mgr.sendGoProCommand = Mock()
value = struct.pack('<HBBBB', 5, 0, 3, 7, 1)
self.mgr.handlePacket(app_packet.GOPRO_SET_EXTENDED_REQUEST, value)
self.mgr.sendGoProCommand.assert_called_with(5, (0, 3, 7, 1))
class TestSendState(unittest.TestCase):
def setUp(self):
shotmgr = Mock()
self.mgr = GoProManager.GoProManager(shotmgr)
def testSendState(self):
""" Send state sends the current gopro state to the app """
self.mgr.enabled = 1
self.mgr.model = MODEL_HERO3PLUS_BLACK
self.mgr.status = STATUS_GOPRO_CONNECTED
self.mgr.isRecording = False
self.mgr.captureMode = CAPTURE_MODE_VIDEO
self.mgr.videoFormat = VIDEO_FORMAT_NTSC
self.mgr.videoResolution = 3
self.mgr.videoFrameRate = 1
self.mgr.videoFieldOfView = 2
self.mgr.videoLowLight = True
self.mgr.photoResolution = 1
self.mgr.photoBurstRate = 2
self.mgr.videoProtune = True
self.mgr.videoProtuneWhiteBalance = 2
self.mgr.videoProtuneColor = 1
self.mgr.videoProtuneGain = 3
self.mgr.videoProtuneSharpness = 2
self.mgr.videoProtuneExposure = 1
# Send old spec version
# 2 unsigned shorts for a header, 26 unsigned bytes, then 5 unsigned shorts
pkt1 = struct.pack('<IIBBBBBBBBBBBBBBBBBBBBBBBBBBHHHHH', app_packet.GOPRO_V1_STATE, 36, \
GOPRO_V1_SPEC_VERSION,
self.mgr.model,
self.mgr.status,
self.mgr.isRecording,
self.mgr.captureMode,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0
)
# send new spec version
# 2 unsigned shorts for a header, 26 unsigned bytes, then 5 unsigned shorts
pkt2 = struct.pack('<IIBBBBBBBBBBBBBBBBBBBBBBBBBBHHHHH', app_packet.GOPRO_V2_STATE, 36, \
GOPRO_V2_SPEC_VERSION,
self.mgr.model,
self.mgr.status,
self.mgr.isRecording,
self.mgr.captureMode,
self.mgr.videoFormat,
self.mgr.videoResolution,
self.mgr.videoFrameRate,
self.mgr.videoFieldOfView,
self.mgr.videoLowLight,
self.mgr.photoResolution,
self.mgr.photoBurstRate,
self.mgr.videoProtune,
self.mgr.videoProtuneWhiteBalance,
self.mgr.videoProtuneColor,
self.mgr.videoProtuneGain,
self.mgr.videoProtuneSharpness,
self.mgr.videoProtuneExposure,
self.mgr.enabled,
0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0
)
self.mgr.sendState()
call1 = call(pkt1)
call2 = call(pkt2)
self.mgr.shotMgr.appMgr.sendPacket.assert_has_calls([call1, call2])
class TestSetGimbalEnabledParam(unittest.TestCase):
def setUp(self):
shotmgr = Mock()
self.mgr = GoProManager.GoProManager(shotmgr)
def testSetEnabled(self):
""" Tell gimbal to turn on gopro commands """
self.mgr.enabled = True
self.mgr.setGimbalEnabledParam()
self.mgr.shotMgr.vehicle.message_factory.param_set_encode.assert_called_with(0, mavutil.mavlink.MAV_COMP_ID_GIMBAL, # target system, target component
"GMB_GP_CTRL", 1.0, mavutil.mavlink.MAV_PARAM_TYPE_REAL32 )
def testSetDisabled(self):
""" Tell gimbal to turn off gopro commands """
self.mgr.enabled = False
self.mgr.setGimbalEnabledParam()
self.mgr.shotMgr.vehicle.message_factory.param_set_encode.assert_called_with(0, mavutil.mavlink.MAV_COMP_ID_GIMBAL, # target system, target component
"GMB_GP_CTRL", 0.0, mavutil.mavlink.MAV_PARAM_TYPE_REAL32 )
class TestSetGoProEnabled(unittest.TestCase):
def setUp(self):
shotmgr = Mock()
self.mgr = GoProManager.GoProManager(shotmgr)
mock = patch('settings.writeSetting')
self.addCleanup(mock.stop)
self.mockWrite = mock.start()
self.mgr.setGimbalEnabledParam = Mock()
def testSetEnabled(self):
""" Tell gimbal to turn on gopro commands """
self.mgr.setGoProEnabled(True)
self.assertTrue(self.mgr.enabled)
self.mockWrite.assert_called_with("GoProEnabled", "1")
self.mgr.setGimbalEnabledParam.assert_called_with()
def testSetDisabled(self):
""" Tell gimbal to turn off gopro commands """
self.mgr.setGoProEnabled(False)
self.assertFalse(self.mgr.enabled)
self.mockWrite.assert_called_with("GoProEnabled", "0")
self.mgr.setGimbalEnabledParam.assert_called_with()