# Unit tests for buttonManager import mock from mock import call from mock import Mock from mock import patch import os from os import sys, path import unittest sys.path.append(os.path.realpath('..')) import buttonManager import GeoFenceManager import shots from dronekit import Vehicle # on host systems these files are located here from sololink import btn_msg import shotManager import struct import app_packet class TestHandleButtons(unittest.TestCase): def setUp(self): self.mgr = shotManager.ShotManager() self.mgr.rcMgr = Mock() self.mgr.buttonManager = buttonManager.buttonManager(self.mgr) self.mgr.lastMode = "LOITER" self.mgr.appMgr = Mock() self.mgr.vehicle = mock.create_autospec(Vehicle) self.mgr.vehicle.system_status = 'ACTIVE' self.mgr.sendPacket = Mock() self.mgr.goproManager = Mock() self.mgr.geoFenceManager = GeoFenceManager.GeoFenceManager(self.mgr) def tearDown(self): del self.mgr def testHandleButtonsNoEvent(self): """ Testing handle Buttons with no event """ self.mgr.buttonManager.handleButtons(None) def testHandleButtonsStartShot(self): """ Test starting a shot via button press """ whichShots = [shots.APP_SHOT_ORBIT, shots.APP_SHOT_CABLECAM] self.mgr.client = 5 self.mgr.vehicle.armed = True self.mgr.last_ekf_ok = True for i in whichShots: self.mgr.buttonManager.getFreeButtonMapping = Mock(return_value = (i, -1)) self.mgr.currentShot = shots.APP_SHOT_NONE self.mgr.buttonManager.handleButtons((btn_msg.ButtonA, btn_msg.Press)) self.assertEqual(self.mgr.currentShot, i) def testDisarmedStartShot(self): """ if we're not armed, we should not be able to start the shot """ whichShots = [shots.APP_SHOT_ORBIT, shots.APP_SHOT_CABLECAM] self.mgr.client = 5 self.mgr.vehicle.armed = False self.mgr.last_ekf_ok = True for i in whichShots: self.mgr.buttonManager.getFreeButtonMapping = Mock(return_value = (i, -1)) self.mgr.currentShot = shots.APP_SHOT_NONE self.mgr.buttonManager.handleButtons((btn_msg.ButtonA, btn_msg.Press)) self.assertEqual(self.mgr.currentShot, shots.APP_SHOT_NONE) packetDisallow = struct.pack('