Commit 2fe7f2b6 authored by Ovidio Henriquez's avatar Ovidio Henriquez Committed by Commit Bot

Add RemoveFakeService to BT test interface

This change allows the removal of a service to be simulated by the
Bluetooth test interface. To test the new function the script for
generating service is removed tests is also updated to make use of the
new function.

BUG=569709

Change-Id: Ibb1c1dcfe3e7928c6628402d4a434847faf69e4e
Reviewed-on: https://chromium-review.googlesource.com/825946
Commit-Queue: Ovidio Henriquez <odejesush@chromium.org>
Reviewed-by: default avatarReilly Grant <reillyg@chromium.org>
Reviewed-by: default avatarDaniel Cheng <dcheng@chromium.org>
Reviewed-by: default avatarConley Owens <cco3@chromium.org>
Cr-Commit-Position: refs/heads/master@{#530582}
parent 8908693a
...@@ -163,6 +163,11 @@ interface FakeCentral { ...@@ -163,6 +163,11 @@ interface FakeCentral {
AddFakeService(string peripheral_address, UUID service_uuid) AddFakeService(string peripheral_address, UUID service_uuid)
=> (string? service_id); => (string? service_id);
// Removes a fake GATT Services with |service_id| from the fake peripheral
// with |peripheral_address|.
RemoveFakeService(string service_id, string peripheral_address)
=> (bool success);
// Adds a fake GATT Characteristic with |characteristic_uuid| and |properties| // Adds a fake GATT Characteristic with |characteristic_uuid| and |properties|
// to the fake service with |service_id| in |peripheral_address| peripheral. // to the fake service with |service_id| in |peripheral_address| peripheral.
// The characteristic will be found when discovering the peripheral's GATT // The characteristic will be found when discovering the peripheral's GATT
......
...@@ -118,6 +118,17 @@ void FakeCentral::AddFakeService(const std::string& peripheral_address, ...@@ -118,6 +118,17 @@ void FakeCentral::AddFakeService(const std::string& peripheral_address,
std::move(callback).Run(fake_peripheral->AddFakeService(service_uuid)); std::move(callback).Run(fake_peripheral->AddFakeService(service_uuid));
} }
void FakeCentral::RemoveFakeService(const std::string& identifier,
const std::string& peripheral_address,
RemoveFakeServiceCallback callback) {
FakePeripheral* fake_peripheral = GetFakePeripheral(peripheral_address);
if (!fake_peripheral) {
std::move(callback).Run(false);
return;
}
std::move(callback).Run(fake_peripheral->RemoveFakeService(identifier));
}
void FakeCentral::AddFakeCharacteristic( void FakeCentral::AddFakeCharacteristic(
const device::BluetoothUUID& characteristic_uuid, const device::BluetoothUUID& characteristic_uuid,
mojom::CharacteristicPropertiesPtr properties, mojom::CharacteristicPropertiesPtr properties,
......
...@@ -54,6 +54,9 @@ class FakeCentral : public mojom::FakeCentral, public device::BluetoothAdapter { ...@@ -54,6 +54,9 @@ class FakeCentral : public mojom::FakeCentral, public device::BluetoothAdapter {
void AddFakeService(const std::string& peripheral_address, void AddFakeService(const std::string& peripheral_address,
const device::BluetoothUUID& service_uuid, const device::BluetoothUUID& service_uuid,
AddFakeServiceCallback callback) override; AddFakeServiceCallback callback) override;
void RemoveFakeService(const std::string& identifier,
const std::string& peripheral_address,
RemoveFakeServiceCallback callback) override;
void AddFakeCharacteristic(const device::BluetoothUUID& characteristic_uuid, void AddFakeCharacteristic(const device::BluetoothUUID& characteristic_uuid,
mojom::CharacteristicPropertiesPtr properties, mojom::CharacteristicPropertiesPtr properties,
const std::string& service_id, const std::string& service_id,
......
...@@ -88,6 +88,16 @@ std::string FakePeripheral::AddFakeService( ...@@ -88,6 +88,16 @@ std::string FakePeripheral::AddFakeService(
return it->second->GetIdentifier(); return it->second->GetIdentifier();
} }
bool FakePeripheral::RemoveFakeService(const std::string& identifier) {
auto it = gatt_services_.find(identifier);
if (it == gatt_services_.end()) {
return false;
}
gatt_services_.erase(it);
return true;
}
uint32_t FakePeripheral::GetBluetoothClass() const { uint32_t FakePeripheral::GetBluetoothClass() const {
NOTREACHED(); NOTREACHED();
return 0; return 0;
......
...@@ -59,6 +59,9 @@ class FakePeripheral : public device::BluetoothDevice { ...@@ -59,6 +59,9 @@ class FakePeripheral : public device::BluetoothDevice {
// Returns the service's Id. // Returns the service's Id.
std::string AddFakeService(const device::BluetoothUUID& service_uuid); std::string AddFakeService(const device::BluetoothUUID& service_uuid);
// Remove a fake service with |identifier| from this peripheral.
bool RemoveFakeService(const std::string& identifier);
// BluetoothDevice overrides: // BluetoothDevice overrides:
uint32_t GetBluetoothClass() const override; uint32_t GetBluetoothClass() const override;
#if defined(OS_CHROMEOS) || defined(OS_LINUX) #if defined(OS_CHROMEOS) || defined(OS_LINUX)
......
'use strict'; 'use strict';
bluetooth_test(() => { const test_desc = 'Service is removed before FUNCTION_NAME call. ' +
return setBluetoothFakeAdapter('HeartRateAdapter') 'Reject with InvalidStateError.';
.then(() => requestDeviceWithTrustedClick({ const expected = new DOMException('GATT Service no longer exists.',
filters: [{services: ['heart_rate']}], 'InvalidStateError');
optionalServices: ['generic_access']})) let service, fake_service, fake_peripheral;
.then(device => device.gatt.connect())
.then(gatt => gatt.getPrimaryService('generic_access')) bluetooth_test(() => getHealthThermometerService()
.then(service => { .then(_ => ({service, fake_service, fake_peripheral} = _))
return setBluetoothFakeAdapter('MissingServiceHeartRateAdapter') .then(() => fake_service.remove())
.then(() => assert_promise_rejects_with_message( .then(() => fake_peripheral.simulateGATTServicesChanged())
service.CALLS([ .then(() => assert_promise_rejects_with_message(
getCharacteristic('gap.device_name')| service.CALLS([
getCharacteristics()| getCharacteristic('measurement_interval')|
getCharacteristics('gap.device_name')[UUID] getCharacteristics()|
]), getCharacteristics('measurement_interval')[UUID]
new DOMException('GATT Service no longer exists.', ]),
'InvalidStateError'), expected,
'Service got removed.')); 'Service got removed.')),
}); test_desc);
}, 'Service is removed before FUNCTION_NAME call. Reject with InvalidStateError.');
...@@ -7,21 +7,20 @@ ...@@ -7,21 +7,20 @@
<script src="../../../external/wpt/bluetooth/resources/bluetooth-helpers.js"></script> <script src="../../../external/wpt/bluetooth/resources/bluetooth-helpers.js"></script>
<script> <script>
'use strict'; 'use strict';
bluetooth_test(() => { const test_desc = 'Service is removed before getCharacteristic call. ' +
return setBluetoothFakeAdapter('HeartRateAdapter') 'Reject with InvalidStateError.';
.then(() => requestDeviceWithTrustedClick({ const expected = new DOMException('GATT Service no longer exists.',
filters: [{services: ['heart_rate']}], 'InvalidStateError');
optionalServices: ['generic_access']})) let service, fake_service, fake_peripheral;
.then(device => device.gatt.connect())
.then(gatt => gatt.getPrimaryService('generic_access')) bluetooth_test(() => getHealthThermometerService()
.then(service => { .then(_ => ({service, fake_service, fake_peripheral} = _))
return setBluetoothFakeAdapter('MissingServiceHeartRateAdapter') .then(() => fake_service.remove())
.then(() => assert_promise_rejects_with_message( .then(() => fake_peripheral.simulateGATTServicesChanged())
service.getCharacteristic('gap.device_name'), .then(() => assert_promise_rejects_with_message(
new DOMException('GATT Service no longer exists.', service.getCharacteristic('measurement_interval'),
'InvalidStateError'), expected,
'Service got removed.')); 'Service got removed.')),
}); test_desc);
}, 'Service is removed before getCharacteristic call. Reject with InvalidStateError.');
</script> </script>
...@@ -7,21 +7,20 @@ ...@@ -7,21 +7,20 @@
<script src="../../../external/wpt/bluetooth/resources/bluetooth-helpers.js"></script> <script src="../../../external/wpt/bluetooth/resources/bluetooth-helpers.js"></script>
<script> <script>
'use strict'; 'use strict';
bluetooth_test(() => { const test_desc = 'Service is removed before getCharacteristics call. ' +
return setBluetoothFakeAdapter('HeartRateAdapter') 'Reject with InvalidStateError.';
.then(() => requestDeviceWithTrustedClick({ const expected = new DOMException('GATT Service no longer exists.',
filters: [{services: ['heart_rate']}], 'InvalidStateError');
optionalServices: ['generic_access']})) let service, fake_service, fake_peripheral;
.then(device => device.gatt.connect())
.then(gatt => gatt.getPrimaryService('generic_access')) bluetooth_test(() => getHealthThermometerService()
.then(service => { .then(_ => ({service, fake_service, fake_peripheral} = _))
return setBluetoothFakeAdapter('MissingServiceHeartRateAdapter') .then(() => fake_service.remove())
.then(() => assert_promise_rejects_with_message( .then(() => fake_peripheral.simulateGATTServicesChanged())
service.getCharacteristics('gap.device_name'), .then(() => assert_promise_rejects_with_message(
new DOMException('GATT Service no longer exists.', service.getCharacteristics('measurement_interval'),
'InvalidStateError'), expected,
'Service got removed.')); 'Service got removed.')),
}); test_desc);
}, 'Service is removed before getCharacteristics call. Reject with InvalidStateError.');
</script> </script>
...@@ -7,21 +7,20 @@ ...@@ -7,21 +7,20 @@
<script src="../../../external/wpt/bluetooth/resources/bluetooth-helpers.js"></script> <script src="../../../external/wpt/bluetooth/resources/bluetooth-helpers.js"></script>
<script> <script>
'use strict'; 'use strict';
bluetooth_test(() => { const test_desc = 'Service is removed before getCharacteristics call. ' +
return setBluetoothFakeAdapter('HeartRateAdapter') 'Reject with InvalidStateError.';
.then(() => requestDeviceWithTrustedClick({ const expected = new DOMException('GATT Service no longer exists.',
filters: [{services: ['heart_rate']}], 'InvalidStateError');
optionalServices: ['generic_access']})) let service, fake_service, fake_peripheral;
.then(device => device.gatt.connect())
.then(gatt => gatt.getPrimaryService('generic_access')) bluetooth_test(() => getHealthThermometerService()
.then(service => { .then(_ => ({service, fake_service, fake_peripheral} = _))
return setBluetoothFakeAdapter('MissingServiceHeartRateAdapter') .then(() => fake_service.remove())
.then(() => assert_promise_rejects_with_message( .then(() => fake_peripheral.simulateGATTServicesChanged())
service.getCharacteristics(), .then(() => assert_promise_rejects_with_message(
new DOMException('GATT Service no longer exists.', service.getCharacteristics(),
'InvalidStateError'), expected,
'Service got removed.')); 'Service got removed.')),
}); test_desc);
}, 'Service is removed before getCharacteristics call. Reject with InvalidStateError.');
</script> </script>
...@@ -236,6 +236,16 @@ class FakeRemoteGATTService { ...@@ -236,6 +236,16 @@ class FakeRemoteGATTService {
characteristic_id, this.service_id_, characteristic_id, this.service_id_,
this.peripheral_address_, this.fake_central_ptr_); this.peripheral_address_, this.fake_central_ptr_);
} }
// Removes the fake GATT service from its fake peripheral.
async remove() {
let {success} =
await this.fake_central_ptr_.removeFakeService(
this.service_id_,
this.peripheral_address_);
if (!success) throw 'remove failed';
}
} }
class FakeRemoteGATTCharacteristic { class FakeRemoteGATTCharacteristic {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment