def test_should_register_BLE_battery_service_and_write_initial_value(self): service = BatteryService(self.mockBLE) expected_services = _create_expected_services() service.register_services() self.assertTrue( self.mockBLE.has_been_called_with('gatts_register_services', (expected_services, ), times=1)) self.assertTrue( self.mockBLE.has_been_called(method='gatts_write', times=1))
def test_should_remove_connection_handle_on_central_disconnected(self): data = (124, 'A_type', 'address') service = BatteryService(self.mockBLE) self.assertEqual(service.connected_centrals(), 0) # triggering 'private' method !! service._irq_handler(_IRQ_CENTRAL_CONNECT, data) self.assertEqual(service.connected_centrals(), 1) service._irq_handler(_IRQ_CENTRAL_DISCONNECT, data) self.assertEqual(service.connected_centrals(), 0)
def test_should_notify_connect_centrals_on_battery_level_change(self): self.mockBLE.when('gatts_read', (MOCK_BATTERY_LEVEL_HANDLE, ), return_value=b'\x0A') connect_data = (124, 'A_type', 'address') service = BatteryService(self.mockBLE) service.register_services() service.start() self.assertFalse(self.mockBLE.has_been_called(method='gatts_notify')) # triggering 'private' method !! service._irq_handler(_IRQ_CENTRAL_CONNECT, connect_data) service.set_battery_level_percentage(23) self.assertTrue( self.mockBLE.has_been_called(method='gatts_notify', times=1))
def test_should_restart_advertising_after_connect(self): connect_data = (124, 'A_type', 'address') service = BatteryService(self.mockBLE) # testing 'private' method !! self.assertTrue(self.mockBLE.has_been_called(method='irq', times=1)) service._irq_handler(_IRQ_CENTRAL_CONNECT, connect_data) self.assertTrue( self.mockBLE.has_been_called_with( 'gap_advertise', { 'interval_us': 500000, 'adv_data': _create_expected_advertising_payload() }, times=1))
def test_should_stop_advertising(self): service = BatteryService(self.mockBLE) service.register_services() service.start() service.stop() self.assertTrue( self.mockBLE.has_been_called_with('gap_advertise', (None, ), times=1))
def test_should_set_battery_level(self): service = BatteryService(self.mockBLE) service.register_services() service.set_battery_level_percentage(10) self.assertTrue( self.mockBLE.has_been_called_with( 'gatts_write', (MOCK_BATTERY_LEVEL_HANDLE, b'\x0A'), times=1))
def test_should_advertise_services(self): service = BatteryService(self.mockBLE) service.register_services() service.start() self.assertTrue( self.mockBLE.has_been_called_with( 'gap_advertise', { 'interval_us': 500000, 'adv_data': _create_expected_advertising_payload() }, times=1))
def test_should_create_battery_service_with_BLE_on(self): service = BatteryService(self.mockBLE) self.assertIsNotNone(service) self.assertTrue( self.mockBLE.has_been_called_with('active', (True, ), times=1))
def test_should_read_battery_level_percentage(self): self.mockBLE.when('gatts_read', (MOCK_BATTERY_LEVEL_HANDLE, ), return_value=b'\x0A') service = BatteryService(self.mockBLE) service.register_services() self.assertEqual(service.read_battery_level_percentage(), 10)
def test_should_round_values_to_nearest_integer(self): service = BatteryService(self.mockBLE) service.register_services() service.set_battery_level_percentage(0.21) self.assertTrue( self.mockBLE.has_been_called_with( 'gatts_write', (MOCK_BATTERY_LEVEL_HANDLE, b'\x00'), times=1)) service.set_battery_level_percentage(0.87) self.assertTrue( self.mockBLE.has_been_called_with( 'gatts_write', (MOCK_BATTERY_LEVEL_HANDLE, b'\x01'), times=1)) service.set_battery_level_percentage(99.8) self.assertTrue( self.mockBLE.has_been_called_with( 'gatts_write', (MOCK_BATTERY_LEVEL_HANDLE, b'\x64'), times=1)) service.set_battery_level_percentage(99.1) self.assertTrue( self.mockBLE.has_been_called_with( 'gatts_write', (MOCK_BATTERY_LEVEL_HANDLE, b'\x63'), times=1))
def create_battery_service(): service = BatteryService(BLE()) service.register_services() service.start() return service