class TestCanvasMarker(unittest.TestCase):
    def setUp(self):
        self.app = QApplication(sys.argv)

        self.config = Config()
        self.config.load()
        self.config.csettings = self.config.settings

        self.canvas = QgsMapCanvas()
        self.canvas.setObjectName("canvas")

        self.default_color_auv = Qt.darkGreen

        self.marker = CanvasMarker(self.canvas,
                                   self.default_color_auv,
                                   None,
                                   marker_mode=True,
                                   config=self.config)

    def test_buttons(self):
        self.trackwidget_auv = TrackWidget()
        self.trackwidget_auv.init("AUV track", self.canvas,
                                  self.default_color_auv,
                                  QgsWkbTypes.LineGeometry, self.marker)
        for i in range(0, 10):
            self.trackwidget_auv.track_update_canvas(
                QgsPointXY(i * 10, i * -10), 0)

        QTest.mouseClick(self.trackwidget_auv.centerButton, Qt.LeftButton)
        QTest.mouseClick(self.trackwidget_auv.clearTrackButton, Qt.LeftButton)
class TestVehicleData(unittest.TestCase):
    def setUp(self):
        self.app = QApplication(sys.argv)
        #sparus2 default
        self.config = Config()
        self.config.load()
        self.config.csettings = self.config.settings

        vinfo = VehicleInfo(self.config)

        self.vd = VehicleData(self.config, vinfo)

    def test_subscribed(self):
        self.assertEqual(self.vd.is_subscribed(), False)

    def test_gets(self):
        #empty none gets
        self.assertEqual(self.vd.get_total_time(), None)
        self.assertEqual(self.vd.get_watchdog(), None)
        self.assertEqual(self.vd.get_goto_status(), None)
        self.assertEqual(self.vd.get_thrusters_status(), None)
        self.assertEqual(self.vd.get_active_controller(), None)
        self.assertEqual(self.vd.get_captain_state(), None)
        self.assertEqual(self.vd.get_battery_charge(), None)
        self.assertEqual(self.vd.get_cpu_usage(), None)
        self.assertEqual(self.vd.get_ram_usage(), None)
        self.assertEqual(self.vd.get_thruster_setpoints(), None)
        self.assertEqual(self.vd.get_mission_active(), None)
        self.assertEqual(self.vd.get_total_steps(), None)
        self.assertEqual(self.vd.get_status_code(), None)
        self.assertEqual(self.vd.get_error_code(), None)
        self.assertEqual(self.vd.get_recovery_action(), None)
        self.assertEqual(self.vd.get_rosout(), None)

        self.assertEqual(self.vd.get_calibrate_magnetometer_service(),
                         "/imu_angle_estimator/calibrate_magnetometer")
        self.assertEqual(self.vd.get_stop_magnetometer_calibration_service(),
                         "/imu_angle_estimator/stop_magnetometer_calibration")
        self.assertEqual(self.vd.get_keep_position_service(),
                         "/captain/enable_keep_position_non_holonomic")
        self.assertEqual(self.vd.get_disable_keep_position_service(),
                         "/captain/disable_keep_position")
        self.assertEqual(self.vd.get_disable_all_keep_positions_service(),
                         "/captain/disable_all_keep_positions")
        self.assertEqual(self.vd.get_reset_timeout_service(),
                         "/cola2_watchdog/reset_timeout")
        self.assertEqual(self.vd.get_goto_service(), "/captain/enable_goto")
        self.assertEqual(self.vd.get_disable_goto_service(),
                         "/captain/disable_goto")
        self.assertEqual(self.vd.get_enable_thrusters_service(),
                         "/controller/enable_thrusters")
        self.assertEqual(self.vd.get_disable_thrusters_service(),
                         "/controller/disable_thrusters")
        self.assertEqual(self.vd.get_enable_mission_service(),
                         "/captain/enable_default_mission_non_block")
        self.assertEqual(self.vd.get_disable_mission_service(),
                         "/captain/disable_mission")
        self.assertEqual(
            self.vd.get_teleoperation_launch(),
            "roslaunch cola2_sparus2 sparus2_teleoperation.launch")
class TestVesselPosSystem(unittest.TestCase):

    def setUp(self):
        self.app = QApplication(sys.argv)

        self.config = Config()
        self.config.load()
        self.config.csettings = self.config.settings

        self.vps = VesselPositionSystem(self.config)

    def test_spinbox(self):
        self.assertEqual(type(self.vps.vessel_width_doubleSpinBox.value()), type(0.0))
        self.assertEqual(type(self.vps.vessel_length_doubleSpinBox.value()), type(0.0))
        self.assertEqual(type(self.vps.gps_x_offset_doubleSpinBox.value()), type(0.0))
        self.assertEqual(type(self.vps.gps_y_offset_doubleSpinBox.value()), type(0.0))
        self.assertEqual(type(self.vps.usbl_x_offset_doubleSpinBox.value()), type(0.0))
        self.assertEqual(type(self.vps.usbl_y_offset_doubleSpinBox.value()), type(0.0))
        self.assertEqual(type(self.vps.usbl_z_offset_doubleSpinBox.value()), type(0.0))

    def test_buttons(self):

        okwidget = self.vps.buttonBox.button(self.vps.buttonBox.Ok)
        cancelwidget = self.vps.buttonBox.button(self.vps.buttonBox.Ok)

        QTest.mouseClick(okwidget, Qt.LeftButton)
        QTest.mouseClick(cancelwidget, Qt.LeftButton)
    def setUp(self):
        self.app = QApplication(sys.argv)
        #sparus2 default
        self.config = Config()
        self.config.load()
        self.config.csettings = self.config.settings

        self.vehicle_info = VehicleInfo(self.config)

        self.proj = QgsProject.instance()
        self.proj.setFileName("")

        self.canvas = QgsMapCanvas()
        self.view = None
        self.wp_dock = None
        self.templates_dock = None
        self.minfo_dock = None
        self.msg_log = None
        self.MISSION_NAME = "temp_mission"

        # self.mt = MissionTrack(mission_filename="mission_test.xml")
        self.mission_ctrl = MissionController(self.config, self.vehicle_info,
                                              self.proj, self.canvas,
                                              self.view, self.wp_dock,
                                              self.templates_dock,
                                              self.minfo_dock, self.msg_log)
    def setUp(self):
        self.app = QApplication(sys.argv)

        self.config = Config()
        self.config.load()
        self.config.csettings = self.config.settings

        self.vps = VesselPositionSystem(self.config)
    def setUp(self):
        self.app = QApplication(sys.argv)
        #sparus2 default
        self.config = Config()
        self.config.load()
        self.config.csettings = self.config.settings

        vinfo = VehicleInfo(self.config)

        self.vd = VehicleData(self.config, vinfo)
    def setUp(self):
        self.app = QApplication(sys.argv)

        self.config = Config()
        self.config.load()
        self.config.csettings = self.config.settings

        self.canvas = QgsMapCanvas()
        self.canvas.setObjectName("canvas")

        self.default_color_auv = Qt.darkGreen

        self.marker = CanvasMarker(self.canvas,
                                   self.default_color_auv,
                                   None,
                                   marker_mode=True,
                                   config=self.config)
class TestMissionTrack(unittest.TestCase):
    def setUp(self):
        self.app = QApplication(sys.argv)
        #sparus2 default
        self.config = Config()
        self.config.load()
        self.config.csettings = self.config.settings

        self.vehicle_info = VehicleInfo(self.config)

        self.proj = QgsProject.instance()
        self.proj.setFileName("")

        self.canvas = QgsMapCanvas()
        self.view = None
        self.wp_dock = None
        self.templates_dock = None
        self.minfo_dock = None
        self.msg_log = None
        self.MISSION_NAME = "temp_mission"

        # self.mt = MissionTrack(mission_filename="mission_test.xml")
        self.mission_ctrl = MissionController(self.config, self.vehicle_info,
                                              self.proj, self.canvas,
                                              self.view, self.wp_dock,
                                              self.templates_dock,
                                              self.minfo_dock, self.msg_log)

    def tearDown(self):
        os.remove("temp_mission.xml")

    def test_structure(self):
        self.write_temp_mission_xml()
        self.mission_ctrl.load_mission(self.MISSION_NAME + ".xml")
        self.mission_track = self.mission_ctrl.mission_list[0]

        self.assertNotEqual(self.mission_track, None)
        self.assertEqual(self.mission_track.get_mission_length(), 3)
        self.assertEqual(self.mission_track.mission_layer.geometryType(),
                         QgsWkbTypes.LineGeometry)
        self.assertEqual(self.mission_track.get_mission_name(),
                         self.MISSION_NAME)

    def test_remove_step(self):
        self.write_temp_mission_xml()
        self.mission_ctrl.load_mission(self.MISSION_NAME + ".xml")
        self.mission_track = self.mission_ctrl.mission_list[0]

        self.mission_track.remove_step(2)
        self.assertEqual(self.mission_track.get_mission_length(), 2)
        self.assertEqual(self.mission_track.mission_layer.geometryType(),
                         QgsWkbTypes.LineGeometry)

        self.mission_track.remove_step(1)
        self.assertEqual(self.mission_track.get_mission_length(), 1)
        self.assertEqual(self.mission_track.mission_layer.geometryType(),
                         QgsWkbTypes.PointGeometry)

        self.mission_track.remove_step(0)
        self.assertEqual(self.mission_track.get_mission_length(), 0)
        self.assertEqual(self.mission_track.mission_layer.geometryType(),
                         QgsWkbTypes.LineGeometry)

        self.assertEqual(self.mission_track.is_modified(), True)

    def test_add_step(self):
        self.write_temp_mission_xml()
        self.mission_ctrl.load_mission(self.MISSION_NAME + ".xml")
        self.mission_track = self.mission_ctrl.mission_list[0]

        point = QgsPointXY(40.0032123, 3.0594338)
        point2 = QgsPointXY(40.0032127, 3.0594331)
        point3 = QgsPointXY(40.0032151, 3.0594340)
        self.mission_track.add_step(3, point)
        self.mission_track.add_step(0, point2)
        self.mission_track.add_step(2, point3)

        self.assertEqual(self.mission_track.get_mission_length(), 6)
        self.assertEqual(self.mission_track.mission_layer.geometryType(),
                         QgsWkbTypes.LineGeometry)

        self.assertEqual(self.mission_track.is_modified(), True)

    def test_change_position(self):
        self.write_temp_mission_xml()
        self.mission_ctrl.load_mission(self.MISSION_NAME + ".xml")
        self.mission_track = self.mission_ctrl.mission_list[0]

        point = QgsPointXY(40.001, 3.002)
        self.mission_track.change_position(0, point)

        position = self.mission_track.get_step(0).get_maneuver().get_position()
        lat = position.get_latitude()
        lon = position.get_longitude()
        self.assertEqual(point.x(), lon)
        self.assertEqual(point.y(), lat)

        self.assertEqual(self.mission_track.is_modified(), True)

    def test_empty_mission(self):
        self.write_temp_mission_xml_empty()
        self.mission_ctrl.load_mission(self.MISSION_NAME + '.xml')
        self.mission_track = self.mission_ctrl.mission_list[0]

        self.assertNotEqual(self.mission_track, None)
        self.assertEqual(self.mission_track.get_mission_length(), 0)
        self.assertEqual(self.mission_track.mission_layer.geometryType(),
                         QgsWkbTypes.LineGeometry)

        point = QgsPointXY(40.0032123, 3.0594338)
        point2 = QgsPointXY(40.0032127, 3.0594331)

        self.mission_track.add_step(0, point)
        self.assertEqual(self.mission_track.get_mission_length(), 1)
        self.assertEqual(self.mission_track.mission_layer.geometryType(),
                         QgsWkbTypes.PointGeometry)

        self.mission_track.add_step(1, point2)
        self.assertEqual(self.mission_track.get_mission_length(), 2)
        self.assertEqual(self.mission_track.mission_layer.geometryType(),
                         QgsWkbTypes.LineGeometry)

        self.mission_track.remove_step(0)
        self.assertEqual(self.mission_track.get_mission_length(), 1)
        self.assertEqual(self.mission_track.mission_layer.geometryType(),
                         QgsWkbTypes.PointGeometry)

        self.mission_track.remove_step(0)
        self.assertEqual(self.mission_track.get_mission_length(), 0)
        self.assertEqual(self.mission_track.mission_layer.geometryType(),
                         QgsWkbTypes.LineGeometry)

    def write_temp_mission_xml(self):
        mission = Mission()
        mission_step = MissionStep()
        param = Parameter("abcd")
        param_2 = Parameter("2")
        parameters = list()
        parameters.append(param)
        parameters.append(param_2)
        action = MissionAction("action1", parameters)
        mission_step.add_action(action)
        wp = MissionWaypoint(MissionPosition(41.777, 3.030, 15.0, False), 0.5,
                             MissionTolerance(2.0, 2.0, 1.0))
        mission_step.add_maneuver(wp)
        mission.add_step(mission_step)
        mission_step2 = MissionStep()
        sec = MissionSection(MissionPosition(41.777, 3.030, 15.0, False),
                             MissionPosition(41.787, 3.034, 15.0, False), 0.5,
                             MissionTolerance(2.0, 2.0, 1.0))
        mission_step2.add_maneuver(sec)
        mission.add_step(mission_step2)
        mission_step3 = MissionStep()
        park = MissionPark(MissionPosition(41.777, 3.030, 15.0, False), 0.5,
                           120, MissionTolerance(2.0, 2.0, 1.0))
        mission_step3.add_maneuver(park)
        mission.add_step(mission_step3)
        mission.write_mission(self.MISSION_NAME + '.xml')

    def write_temp_mission_xml_empty(self):
        mission = Mission()
        mission.write_mission(self.MISSION_NAME + '.xml')