Commit af1c15b1 authored by Ovidio Henriquez's avatar Ovidio Henriquez Committed by Commit Bot

bluetooth: Use DeviceUUID in FakeBluetooth

This change refactors the Web Bluetooth test API to use the
DeviceUUIDs helper class defined in BluetoothDevice. Additionally,
this change finishes the implementation of SimulateGATTServicesChanged,
and as a result, tests are updated to set the next discovery response
before calling this interface.

BUG=719826

Change-Id: I0f986eb7afe6fbf7ebaa80ac4b633d46a027b80d
Reviewed-on: https://chromium-review.googlesource.com/939984
Commit-Queue: Ovidio Henriquez <odejesush@chromium.org>
Reviewed-by: default avatarGiovanni Ortuño Urquidi <ortuno@chromium.org>
Cr-Commit-Position: refs/heads/master@{#541577}
parent e2d74157
......@@ -580,7 +580,7 @@ class DEVICE_BLUETOOTH_EXPORT BluetoothDevice {
// Helper class to easily update the sets of UUIDs and keep them in sync with
// the set of all the device's UUIDs.
class DeviceUUIDs {
class DEVICE_BLUETOOTH_EXPORT DeviceUUIDs {
public:
DeviceUUIDs();
~DeviceUUIDs();
......
......@@ -162,6 +162,7 @@ void FakeCentral::SimulateGATTServicesChanged(
std::move(callback).Run(false);
return;
}
fake_peripheral->SimulateGATTServicesChanged();
std::move(callback).Run(true);
}
......
......@@ -8,6 +8,7 @@
#include "base/memory/ptr_util.h"
#include "base/memory/weak_ptr.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/stringprintf.h"
#include "device/bluetooth/bluetooth_uuid.h"
#include "device/bluetooth/test/fake_remote_gatt_service.h"
......@@ -35,7 +36,20 @@ void FakePeripheral::SetSystemConnected(bool connected) {
}
void FakePeripheral::SetServiceUUIDs(UUIDSet service_uuids) {
service_uuids_ = std::move(service_uuids);
device::BluetoothDevice::GattServiceMap services_map;
bool inserted;
// Create a temporary map of services, because ReplaceServiceUUIDs expects a
// GattServiceMap even though it only uses the UUIDs.
int count = 0;
for (auto uuid : service_uuids) {
std::string id = base::IntToString(count++);
std::tie(std::ignore, inserted) =
services_map.emplace(id, std::make_unique<FakeRemoteGattService>(
id, uuid, true /* is_primary */, this));
DCHECK(inserted);
}
device_uuids_.ReplaceServiceUUIDs(services_map);
}
void FakePeripheral::SetNextGATTConnectionResponse(uint16_t code) {
......@@ -66,6 +80,7 @@ void FakePeripheral::SimulateGATTDisconnection() {
// for more details.
system_connected_ = false;
gatt_connected_ = false;
device_uuids_.ClearServiceUUIDs();
SetGattServicesDiscoveryComplete(false);
DidDisconnectGatt();
}
......@@ -98,6 +113,12 @@ bool FakePeripheral::RemoveFakeService(const std::string& identifier) {
return true;
}
void FakePeripheral::SimulateGATTServicesChanged() {
device_uuids_.ClearServiceUUIDs();
SetGattServicesDiscoveryComplete(false);
GetAdapter()->NotifyDeviceChanged(this);
}
uint32_t FakePeripheral::GetBluetoothClass() const {
NOTREACHED();
return 0;
......@@ -180,10 +201,6 @@ bool FakePeripheral::IsConnecting() const {
return false;
}
device::BluetoothDevice::UUIDSet FakePeripheral::GetUUIDs() const {
return service_uuids_;
}
bool FakePeripheral::ExpectingPinCode() const {
NOTREACHED();
return false;
......@@ -328,6 +345,7 @@ void FakePeripheral::DispatchDiscoveryResponse() {
pending_gatt_discovery_ = false;
if (code == mojom::kHCISuccess) {
device_uuids_.ReplaceServiceUUIDs(gatt_services_);
SetGattServicesDiscoveryComplete(true);
GetAdapter()->NotifyGattServicesDiscovered(this);
} else {
......
......@@ -62,6 +62,9 @@ class FakePeripheral : public device::BluetoothDevice {
// Remove a fake service with |identifier| from this peripheral.
bool RemoveFakeService(const std::string& identifier);
// Simulates a GATT services changed from the peripheral.
void SimulateGATTServicesChanged();
// BluetoothDevice overrides:
uint32_t GetBluetoothClass() const override;
#if defined(OS_CHROMEOS) || defined(OS_LINUX)
......@@ -81,7 +84,6 @@ class FakePeripheral : public device::BluetoothDevice {
bool IsGattConnected() const override;
bool IsConnectable() const override;
bool IsConnecting() const override;
UUIDSet GetUUIDs() const override;
bool ExpectingPinCode() const override;
bool ExpectingPasskey() const override;
bool ExpectingConfirmation() const override;
......
......@@ -17,6 +17,7 @@ bluetooth_test(() => getMeasurementIntervalCharacteristic()
.then(() => characteristic.getDescriptor(user_description.name))
.then(() => null, (e) => assert_unreached('Caught error unexpectedly.', e))
.then(() => fake_characteristic.remove())
.then(() => fake_peripheral.setNextGATTDiscoveryResponse({HCI_SUCCESS}))
.then(() => fake_peripheral.simulateGATTServicesChanged())
.then(() => assert_promise_rejects_with_message(
characteristic.getDescriptor(user_description.name), expected)),
......
......@@ -17,6 +17,7 @@ let characteristic, fake_peripheral, fake_service;
bluetooth_test(() => getMeasurementIntervalCharacteristic()
.then(_ => ({characteristic, fake_peripheral, fake_service} = _))
.then(() => fake_service.remove())
.then(() => fake_peripheral.setNextGATTDiscoveryResponse({HCI_SUCCESS}))
.then(() => fake_peripheral.simulateGATTServicesChanged())
.then(() => assert_promise_rejects_with_message(
characteristic.getDescriptor(user_description.name),
......
......@@ -17,6 +17,7 @@ bluetooth_test(() => getMeasurementIntervalCharacteristic()
.then(() => characteristic.getDescriptor(user_description.name))
.then(() => null, (e) => assert_unreached('Caught error unexpectedly.', e))
.then(() => fake_characteristic.remove())
.then(() => fake_peripheral.setNextGATTDiscoveryResponse({HCI_SUCCESS}))
.then(() => fake_peripheral.simulateGATTServicesChanged())
.then(() => assert_promise_rejects_with_message(
characteristic.getDescriptors(user_description.name), expected)),
......
......@@ -17,6 +17,7 @@ bluetooth_test(() => getMeasurementIntervalCharacteristic()
.then(() => characteristic.getDescriptor(user_description.name))
.then(() => null, (e) => assert_unreached('Caught error unexpectedly.', e))
.then(() => fake_characteristic.remove())
.then(() => fake_peripheral.setNextGATTDiscoveryResponse({HCI_SUCCESS}))
.then(() => fake_peripheral.simulateGATTServicesChanged())
.then(() => assert_promise_rejects_with_message(
characteristic.getDescriptors(), expected)),
......
......@@ -17,6 +17,7 @@ let characteristic, fake_peripheral, fake_service;
bluetooth_test(() => getMeasurementIntervalCharacteristic()
.then(_ => ({characteristic, fake_peripheral, fake_service} = _))
.then(() => fake_service.remove())
.then(() => fake_peripheral.setNextGATTDiscoveryResponse({HCI_SUCCESS}))
.then(() => fake_peripheral.simulateGATTServicesChanged())
.then(() => assert_promise_rejects_with_message(
characteristic.getDescriptors(user_description.uuid),
......
......@@ -17,6 +17,7 @@ let characteristic, fake_peripheral, fake_service;
bluetooth_test(() => getMeasurementIntervalCharacteristic()
.then(_ => ({characteristic, fake_peripheral, fake_service} = _))
.then(() => fake_service.remove())
.then(() => fake_peripheral.setNextGATTDiscoveryResponse({HCI_SUCCESS}))
.then(() => fake_peripheral.simulateGATTServicesChanged())
.then(() => assert_promise_rejects_with_message(
characteristic.getDescriptors(user_description.name),
......
......@@ -14,6 +14,7 @@ let characteristic, fake_peripheral, fake_service;
bluetooth_test(() => getMeasurementIntervalCharacteristic()
.then(_ => ({characteristic, fake_peripheral, fake_service} = _))
.then(() => fake_service.remove())
.then(() => fake_peripheral.setNextGATTDiscoveryResponse({HCI_SUCCESS}))
.then(() => fake_peripheral.simulateGATTServicesChanged())
.then(() => assert_promise_rejects_with_message(
characteristic.startNotifications(),
......
......@@ -17,6 +17,7 @@ bluetooth_test(() => getMeasurementIntervalCharacteristic()
.then(() => characteristic.getDescriptor(user_description.name))
.then(() => null, (e) => assert_unreached('Caught error unexpectedly.', e))
.then(() => fake_characteristic.remove())
.then(() => fake_peripheral.setNextGATTDiscoveryResponse({HCI_SUCCESS}))
.then(() => fake_peripheral.simulateGATTServicesChanged())
.then(() => assert_promise_rejects_with_message(
characteristic.readValue(), expected)),
......
......@@ -14,6 +14,7 @@ let characteristic, fake_peripheral, fake_service;
bluetooth_test(() => getMeasurementIntervalCharacteristic()
.then(_ => ({characteristic, fake_peripheral, fake_service} = _))
.then(() => fake_service.remove())
.then(() => fake_peripheral.setNextGATTDiscoveryResponse({HCI_SUCCESS}))
.then(() => fake_peripheral.simulateGATTServicesChanged())
.then(() => assert_promise_rejects_with_message(
characteristic.readValue(),
......
......@@ -17,6 +17,7 @@ bluetooth_test(() => getMeasurementIntervalCharacteristic()
.then(() => characteristic.getDescriptor(user_description.name))
.then(() => null, (e) => assert_unreached('Caught error unexpectedly.', e))
.then(() => fake_characteristic.remove())
.then(() => fake_peripheral.setNextGATTDiscoveryResponse({HCI_SUCCESS}))
.then(() => fake_peripheral.simulateGATTServicesChanged())
.then(() => assert_promise_rejects_with_message(
characteristic.startNotifications(), expected)),
......
......@@ -17,6 +17,7 @@ bluetooth_test(() => getMeasurementIntervalCharacteristic()
.then(() => characteristic.getDescriptor(user_description.name))
.then(() => null, (e) => assert_unreached('Caught error unexpectedly.', e))
.then(() => fake_characteristic.remove())
.then(() => fake_peripheral.setNextGATTDiscoveryResponse({HCI_SUCCESS}))
.then(() => fake_peripheral.simulateGATTServicesChanged())
.then(() => assert_promise_rejects_with_message(
characteristic.writeValue(new Uint8Array(1)), expected)),
......
......@@ -14,6 +14,7 @@ let characteristic, fake_peripheral, fake_service;
bluetooth_test(() => getMeasurementIntervalCharacteristic()
.then(_ => ({characteristic, fake_peripheral, fake_service} = _))
.then(() => fake_service.remove())
.then(() => fake_peripheral.setNextGATTDiscoveryResponse({HCI_SUCCESS}))
.then(() => fake_peripheral.simulateGATTServicesChanged())
.then(() => assert_promise_rejects_with_message(
characteristic.writeValue(new ArrayBuffer(1 /* length */)),
......
......@@ -15,6 +15,7 @@ let descriptor, fake_peripheral, fake_service;
bluetooth_test(() => getUserDescriptionDescriptor()
.then(_ => ({descriptor, fake_peripheral, fake_service} = _))
.then(() => fake_service.remove())
.then(() => fake_peripheral.setNextGATTDiscoveryResponse({HCI_SUCCESS}))
.then(() => fake_peripheral.simulateGATTServicesChanged())
.then(() => assert_promise_rejects_with_message(
descriptor.readValue(),
......
......@@ -15,6 +15,7 @@ let descriptor, fake_peripheral, fake_service;
bluetooth_test(() => getUserDescriptionDescriptor()
.then(_ => ({descriptor, fake_peripheral, fake_service} = _))
.then(() => fake_service.remove())
.then(() => fake_peripheral.setNextGATTDiscoveryResponse({HCI_SUCCESS}))
.then(() => fake_peripheral.simulateGATTServicesChanged())
.then(() => assert_promise_rejects_with_message(
descriptor.writeValue(new ArrayBuffer(1 /* length */)),
......
......@@ -9,6 +9,7 @@ bluetooth_test(() => getMeasurementIntervalCharacteristic()
.then(() => characteristic.getDescriptor(user_description.name))
.then(() => null, (e) => assert_unreached('Caught error unexpectedly.', e))
.then(() => fake_characteristic.remove())
.then(() => fake_peripheral.setNextGATTDiscoveryResponse({HCI_SUCCESS}))
.then(() => fake_peripheral.simulateGATTServicesChanged())
.then(() => assert_promise_rejects_with_message(
characteristic.CALLS([
......
......@@ -9,6 +9,7 @@ let characteristic, fake_peripheral, fake_service;
bluetooth_test(() => getMeasurementIntervalCharacteristic()
.then(_ => ({characteristic, fake_peripheral, fake_service} = _))
.then(() => fake_service.remove())
.then(() => fake_peripheral.setNextGATTDiscoveryResponse({HCI_SUCCESS}))
.then(() => fake_peripheral.simulateGATTServicesChanged())
.then(() => assert_promise_rejects_with_message(
characteristic.CALLS([
......
......@@ -7,6 +7,7 @@ let descriptor, fake_peripheral, fake_service;
bluetooth_test(() => getUserDescriptionDescriptor()
.then(_ => ({descriptor, fake_peripheral, fake_service} = _))
.then(() => fake_service.remove())
.then(() => fake_peripheral.setNextGATTDiscoveryResponse({HCI_SUCCESS}))
.then(() => fake_peripheral.simulateGATTServicesChanged())
.then(() => assert_promise_rejects_with_message(
descriptor.CALLS([
......
......@@ -8,6 +8,7 @@ let service, fake_service, fake_peripheral;
bluetooth_test(() => getHealthThermometerService()
.then(_ => ({service, fake_service, fake_peripheral} = _))
.then(() => fake_service.remove())
.then(() => fake_peripheral.setNextGATTDiscoveryResponse({HCI_SUCCESS}))
.then(() => fake_peripheral.simulateGATTServicesChanged())
.then(() => assert_promise_rejects_with_message(
service.CALLS([
......
......@@ -16,6 +16,7 @@ let service, fake_service, fake_peripheral;
bluetooth_test(() => getHealthThermometerService()
.then(_ => ({service, fake_service, fake_peripheral} = _))
.then(() => fake_service.remove())
.then(() => fake_peripheral.setNextGATTDiscoveryResponse({HCI_SUCCESS}))
.then(() => fake_peripheral.simulateGATTServicesChanged())
.then(() => assert_promise_rejects_with_message(
service.getCharacteristic('measurement_interval'),
......
......@@ -16,6 +16,7 @@ let service, fake_service, fake_peripheral;
bluetooth_test(() => getHealthThermometerService()
.then(_ => ({service, fake_service, fake_peripheral} = _))
.then(() => fake_service.remove())
.then(() => fake_peripheral.setNextGATTDiscoveryResponse({HCI_SUCCESS}))
.then(() => fake_peripheral.simulateGATTServicesChanged())
.then(() => assert_promise_rejects_with_message(
service.getCharacteristics('measurement_interval'),
......
......@@ -16,6 +16,7 @@ let service, fake_service, fake_peripheral;
bluetooth_test(() => getHealthThermometerService()
.then(_ => ({service, fake_service, fake_peripheral} = _))
.then(() => fake_service.remove())
.then(() => fake_peripheral.setNextGATTDiscoveryResponse({HCI_SUCCESS}))
.then(() => fake_peripheral.simulateGATTServicesChanged())
.then(() => assert_promise_rejects_with_message(
service.getCharacteristics(),
......
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