def test_cleanup_after_session(self, mocked_Hamlib):
        """ Makes sure that the driver correctly cleans up after the session using it ends. """

        # Initialize a test driver
        test_cp = MagicMock()
        test_device = icom_910.ICOM_910(self.standard_icom_config, test_cp)

        # Create a mock pipeline that returns mock services
        test_pipeline = MagicMock()
        mock_tnc_state_service = MagicMock()
        mock_tracker_service = MagicMock()

        def mock_load_service(service_id):
            if service_id == "tnc_state":
                return mock_tnc_state_service
            else:
                return mock_tracker_service

        test_pipeline.load_service = mock_load_service

        # Prepare for the session
        results = test_device.prepare_for_session(test_pipeline)
        self.assertEqual(test_device._tracker_service, mock_tracker_service)
        self.assertEqual(test_device._tnc_state_service, mock_tnc_state_service)

        # Clean up after the session and make sure it had the intended effect
        test_device.cleanup_after_session()
        mocked_Hamlib().close.assert_called_once_with()
        self.assertEqual(test_device._tracker_service, None)
        self.assertEqual(test_device._tnc_state_service, None)
    def test_prepare_for_session(self, mocked_Hamlib):
        """ Makes sure that the driver can prepare for a new useage session. """

        # Initialize a test driver
        test_cp = MagicMock()
        test_device = icom_910.ICOM_910(self.standard_icom_config, test_cp)

        # Create a mock pipeline that returns mock services
        test_pipeline = MagicMock()
        mock_tnc_state_service = MagicMock()
        mock_tracker_service = MagicMock()

        def mock_load_service(service_id):
            if service_id == "tnc_state":
                return mock_tnc_state_service
            else:
                return mock_tracker_service

        test_pipeline.load_service = mock_load_service

        # Prepare for the session and verify results
        results = test_device.prepare_for_session(test_pipeline)
        self.assertTrue(results)
        self.assertEqual(test_device._session_pipeline, test_pipeline)
        self.assertEqual(test_device._tracker_service, mock_tracker_service)
        self.assertEqual(test_device._tnc_state_service, mock_tnc_state_service)
        test_device._tracker_service.register_position_receiver.assert_called_once_with(
            test_device.process_new_doppler_correction
        )
        self.assertEqual(Hamlib.rig_set_debug.call_count, 1)
        mocked_Hamlib().set_conf.assert_has_calls(
            [mock.call("rig_pathname", self.standard_icom_config["icom_device_path"]), mock.call("retry", "5")]
        )
        mocked_Hamlib().open.assert_called_once_with()
  def test_cleanup_after_session(self):
    """ Tests that the antenna controller takes the correct actions once a session has ended. """

    def mock_parse_command(command_request, **keywords):
      self.assertEqual(command_request['command'], "calibrate_and_park")
      self.assertEqual(keywords['user_id'], None)
      self.assertEqual(keywords['kernel_mode'], True)

      return defer.succeed(True)

    # Create a driver instance to test with
    test_pipeline = MagicMock()
    test_pipeline.id = "test_pipeline"
    test_cp = MagicMock()
    test_cp.parse_command = mock_parse_command
    test_device = mxl_antenna_controller.MXL_Antenna_Controller(self.standard_device_configuration, test_cp)
    test_device._session_pipeline = test_pipeline

    # Create a mock 'tracker' service
    test_pipeline.load_service = MagicMock()

    # Run prepare_for_session and check results
    test_deferred = test_device.cleanup_after_session()
    result = yield test_deferred

    # Make sure the tracker state was reset
    self.assertEqual(test_device._current_position, None)
    self.assertEqual(test_device._tracker_service, None)
    self.assertEqual(test_device._session_pipeline, None)
    self.assertEqual(test_device._controller_state['timestamp'], None)
    self.assertEqual(test_device._controller_state['azimuth'], 0)
    self.assertEqual(test_device._controller_state['elevation'], 0)
    self.assertEqual(test_device._controller_state['state'], "inactive")
  def test_prepare_for_session_no_tracker_service(self):
    """ Verifies that the antenna controller takes the correct actions if can't find a 'tracker' service during the 
    prepare_for_session() phase.
    """

    def mock_load_service(service_id):
      raise pipeline.ServiceTypeNotFound("Test error yo.")

    # Create driver and pipeline instances to test with
    test_pipeline = MagicMock()
    test_pipeline.load_service = mock_load_service
    test_device = mxl_antenna_controller.MXL_Antenna_Controller(self.standard_device_configuration, MagicMock())
    test_device._update_state = MagicMock()

    # Run prepare_for_session and check results
    test_results = test_device.prepare_for_session(test_pipeline)
    self.assertTrue(not test_results)
  def test_prepare_for_session_update_loop_error(self):
    """ This test verifies that the antenna controller prepare_for_session deferred. """

    def mock_update_state(service_id):
      raise TypeError("Test error yo.")

    # Create a driver instance to test with
    test_pipeline = MagicMock()
    test_device = mxl_antenna_controller.MXL_Antenna_Controller(self.standard_device_configuration, MagicMock())
    test_device._update_state = mock_update_state

    # Create a mock 'tracker' service
    tracker_service = MagicMock()
    test_pipeline.load_service = lambda service_id : tracker_service

    # Run prepare_for_session
    test_deferred = test_device.prepare_for_session(test_pipeline)
    result = yield test_deferred

    # Verify the results
    self.assertTrue(not test_device._state_update_loop.running)
    self.assertEqual(result, False)
    def test_prepare_for_session_missing_service(self, mocked_Hamlib):
        """ Verifies that the driver still prepares itself for new sessions if it can't find any services that it is capable
    of using. """

        # Initialize a test driver
        test_cp = MagicMock()
        test_device = icom_910.ICOM_910(self.standard_icom_config, test_cp)

        # Create a mock pipeline that doesn't return any services
        test_pipeline = MagicMock()
        test_pipeline.load_service = MagicMock(side_effect=pipeline.ServiceTypeNotFound)

        # Prepare for the session
        results = test_device.prepare_for_session(test_pipeline)
        self.assertEqual(test_device._tracker_service, None)
        self.assertEqual(test_device._tnc_state_service, None)

        # Make sure the radio was still setup (shouldn't depend on services)
        self.assertEqual(Hamlib.rig_set_debug.call_count, 1)
        mocked_Hamlib().set_conf.assert_has_calls(
            [mock.call("rig_pathname", self.standard_icom_config["icom_device_path"]), mock.call("retry", "5")]
        )
        mocked_Hamlib().open.assert_called_once_with()
  def test_prepare_for_session(self):
    """ Verifies that the antenna controller can set itself up correctly in preparation for a new session given that 
    the required service is available.
    """

    # Create a driver instance to test with
    test_pipeline = MagicMock()
    test_device = mxl_antenna_controller.MXL_Antenna_Controller(self.standard_device_configuration, MagicMock())
    test_device._update_state = MagicMock()

    # Create a mock 'tracker' service
    tracker_service = MagicMock()
    test_pipeline.load_service = lambda service_id : tracker_service

    # Run prepare_for_session and check results
    test_deferred = test_device.prepare_for_session(test_pipeline)
    tracker_service.register_position_receiver.assert_called_once_with(test_device.process_new_position)
    self.assertTrue(test_device._state_update_loop.running)

    # Stop the LoopingCall
    test_device._state_update_loop.stop()

    return test_deferred