# Unit tests for SelfieShot import math import mock from mock import call from mock import Mock from mock import MagicMock import os from os import sys, path from pymavlink import mavutil import struct import unittest from dronekit import LocationGlobalRelative, Vehicle sys.path.append(os.path.realpath('..')) import location_helpers import selfie from selfie import SelfieShot from shotManager import ShotManager from rcManager import rcManager from shotManagerConstants import * import shots import app_packet # on host systems these files are located here from sololink import btn_msg ERROR = 0.1 class Attitude(): yaw = 0.0 class TestSelfieHandleRCsNotStarted(unittest.TestCase): def setUp(self): mgr = mock.create_autospec(ShotManager) mgr.buttonManager = Mock() mgr.currentShot = shots.APP_SHOT_SELFIE mgr.rcMgr = Mock(specs = ['remapper']) v = mock.create_autospec(Vehicle) v.location.global_relative_frame = LocationGlobalRelative(30.13241, 10.112135, 0.0) self.controller = SelfieShot(v, mgr) self.controller.altLimit = 0 def testHandleRCsNotStarted(self): """ Without points, handleRCs shouldn't do anything """ channels = [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0] self.controller.handleRCs(channels) class TestSelfieHandleRCs(unittest.TestCase): def setUp(self): mgr = mock.create_autospec(ShotManager) mgr.buttonManager = Mock() mgr.currentShot = shots.APP_SHOT_SELFIE mgr.rcMgr = Mock(specs = ['remapper']) mgr.appMgr = Mock() v = mock.create_autospec(Vehicle) v.location.global_relative_frame = LocationGlobalRelative(30.13241, 10.112135, 0.0) self.controller = SelfieShot(v, mgr) self.controller.altLimit = 0 self.controller.addLocation( LocationGlobalRelative(-14.654861, 108.4645, 32.6545) ) self.controller.addLocation( LocationGlobalRelative(23.654504, 40.4645, 17.6545) ) self.controller.addLocation( LocationGlobalRelative(23.661, 10.4645, 2.6545) ) def testRCsMax(self): """ Test RCs Max """ channels = [1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0] self.controller.handleRCs(channels) def testRCsMin(self): """ Test RCs Min """ channels = [-1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0] self.controller.handleRCs(channels) def testRCsZero(self): """ Test RCs Max """ channels = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0] self.controller.handleRCs(channels) def testRCs2(self): """ Test RCs Max """ channels = [0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2] self.controller.handleRCs(channels) class TestSetButtonMappings(unittest.TestCase): def setUp(self): mgr = mock.create_autospec(ShotManager) mgr.buttonManager = Mock() mgr.currentShot = shots.APP_SHOT_SELFIE mgr.buttonManager = Mock() mgr.rcMgr = Mock(specs = ['remapper']) v = mock.create_autospec(Vehicle) v.location.global_relative_frame = LocationGlobalRelative(30.13241, 10.112135, 0.0) self.controller = SelfieShot(v, mgr) self.controller.altLimit = 0 def testSetButtonMappingsNoROI(self): """ Testing setting button mappings when we haven't locked on yet """ self.controller.setButtonMappings() calls = [call(btn_msg.ButtonA, shots.APP_SHOT_SELFIE, 0, "\0"), call(btn_msg.ButtonB, shots.APP_SHOT_SELFIE, 0, "\0")] self.controller.shotmgr.buttonManager.setArtooButton.assert_has_calls(calls, any_order = False) def testSetButtonMappings(self): """ Testing setting button mappings when we have started """ self.controller.addLocation( LocationGlobalRelative(-14.654861, 108.4645, 32.6545) ) self.controller.addLocation( LocationGlobalRelative(23.654504, 40.4645, 17.6545) ) self.controller.addLocation( LocationGlobalRelative(23.661, 10.4645, 2.6545) ) self.controller.setButtonMappings() calls = [call(btn_msg.ButtonA, shots.APP_SHOT_SELFIE, 0, "\0"), call(btn_msg.ButtonB, shots.APP_SHOT_SELFIE, 0, "\0")] self.controller.shotmgr.buttonManager.setArtooButton.assert_has_calls(calls) class TestHandlePacket(unittest.TestCase): def setUp(self): mgr = mock.create_autospec(ShotManager) mgr.buttonManager = Mock() mgr.currentShot = shots.APP_SHOT_SELFIE mgr.rcMgr = Mock(specs = ['remapper']) v = mock.create_autospec(Vehicle) v.location.global_relative_frame = LocationGlobalRelative(30.13241, 10.112135, 0.0) self.controller = SelfieShot(v, mgr) self.controller.pathHandler = Mock(specs=['isPaused']) self.controller.altLimit = 0 def testCruiseSpeedSet0(self): options = struct.pack('