1 // Copyright 2015 The Chromium Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #ifndef DEVICE_BLUETOOTH_TEST_BLUETOOTH_TEST_H_ 6 #define DEVICE_BLUETOOTH_TEST_BLUETOOTH_TEST_H_ 7 8 #include <stdint.h> 9 10 #include <memory> 11 #include <string> 12 #include <vector> 13 14 #include "base/memory/ref_counted.h" 15 #include "base/memory/weak_ptr.h" 16 #include "base/optional.h" 17 #include "base/test/task_environment.h" 18 #include "device/bluetooth/bluetooth_adapter.h" 19 #include "device/bluetooth/bluetooth_advertisement.h" 20 #include "device/bluetooth/bluetooth_device.h" 21 #include "device/bluetooth/bluetooth_discovery_session.h" 22 #include "device/bluetooth/bluetooth_gatt_connection.h" 23 #include "device/bluetooth/bluetooth_gatt_notify_session.h" 24 #include "device/bluetooth/bluetooth_local_gatt_service.h" 25 #include "device/bluetooth/bluetooth_remote_gatt_characteristic.h" 26 #include "device/bluetooth/bluetooth_remote_gatt_descriptor.h" 27 #include "device/bluetooth/bluetooth_remote_gatt_service.h" 28 #include "testing/gtest/include/gtest/gtest.h" 29 30 namespace device { 31 32 class BluetoothAdapter; 33 class BluetoothDevice; 34 class BluetoothLocalGattCharacteristic; 35 class BluetoothLocalGattDescriptor; 36 37 // A test fixture for Bluetooth that abstracts platform specifics for creating 38 // and controlling fake low level objects. 39 // 40 // Subclasses on each platform implement this, and are then typedef-ed to 41 // BluetoothTest. 42 class BluetoothTestBase : public testing::Test { 43 public: 44 enum class Call { EXPECTED, NOT_EXPECTED }; 45 46 // List of devices that can be simulated with 47 // SimulateConnectedLowEnergyDevice(). 48 // GENERIC_DEVICE: 49 // - Name: kTestDeviceName 50 // - Address: kTestPeripheralUUID1 51 // - Services: [ kTestUUIDGenericAccess ] 52 // HEART_RATE_DEVICE: 53 // - Name: kTestDeviceName 54 // - Address: kTestPeripheralUUID2 55 // - Services: [ kTestUUIDGenericAccess, kTestUUIDHeartRate] 56 enum class ConnectedDeviceType { 57 GENERIC_DEVICE, 58 HEART_RATE_DEVICE, 59 }; 60 61 enum class NotifyValueState { 62 NONE, 63 NOTIFY, 64 INDICATE, 65 }; 66 67 // Utility struct to simplify simulating a low energy device. 68 struct LowEnergyDeviceData { 69 LowEnergyDeviceData(); 70 LowEnergyDeviceData(LowEnergyDeviceData&& data); 71 ~LowEnergyDeviceData(); 72 73 base::Optional<std::string> name; 74 std::string address; 75 int8_t rssi = 0; 76 base::Optional<uint8_t> flags; 77 BluetoothDevice::UUIDList advertised_uuids; 78 base::Optional<int8_t> tx_power; 79 BluetoothDevice::ServiceDataMap service_data; 80 BluetoothDevice::ManufacturerDataMap manufacturer_data; 81 BluetoothTransport transport = BLUETOOTH_TRANSPORT_LE; 82 83 DISALLOW_COPY_AND_ASSIGN(LowEnergyDeviceData); 84 }; 85 86 static const char kTestAdapterName[]; 87 static const char kTestAdapterAddress[]; 88 89 static const char kTestDeviceName[]; 90 static const char kTestDeviceNameEmpty[]; 91 static const char kTestDeviceNameU2f[]; 92 static const char kTestDeviceNameCable[]; 93 94 static const char kTestDeviceAddress1[]; 95 static const char kTestDeviceAddress2[]; 96 static const char kTestDeviceAddress3[]; 97 98 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.device.bluetooth.test 99 enum class TestRSSI { 100 LOWEST = -81, 101 LOWER = -61, 102 LOW = -41, 103 MEDIUM = -21, 104 HIGH = -1, 105 }; 106 107 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.device.bluetooth.test 108 enum class TestTxPower { 109 LOWEST = -40, 110 LOWER = -20, 111 }; 112 113 // Services 114 static const char kTestUUIDGenericAccess[]; 115 static const char kTestUUIDGenericAttribute[]; 116 static const char kTestUUIDImmediateAlert[]; 117 static const char kTestUUIDLinkLoss[]; 118 static const char kTestUUIDHeartRate[]; 119 static const char kTestUUIDU2f[]; 120 // Characteristics 121 // The following three characteristics are for kTestUUIDGenericAccess. 122 static const char kTestUUIDDeviceName[]; 123 static const char kTestUUIDAppearance[]; 124 static const char kTestUUIDReconnectionAddress[]; 125 // This characteristic is for kTestUUIDHeartRate. 126 static const char kTestUUIDHeartRateMeasurement[]; 127 // This characteristic is for kTestUUIDU2f. 128 static const char kTestUUIDU2fControlPointLength[]; 129 // Descriptors 130 static const char kTestUUIDCharacteristicUserDescription[]; 131 static const char kTestUUIDClientCharacteristicConfiguration[]; 132 static const char kTestUUIDServerCharacteristicConfiguration[]; 133 static const char kTestUUIDCharacteristicPresentationFormat[]; 134 static const char kTestUUIDCableAdvertisement[]; 135 // Manufacturer data 136 static const uint16_t kTestManufacturerId; 137 // Test ephemeral ID for BLE devices that support cloud-assisted BLE protocol. 138 static const uint8_t kTestCableEid[]; 139 static const char kTestUuidFormattedClientEid[]; 140 141 BluetoothTestBase(); 142 ~BluetoothTestBase() override; 143 144 // Checks that no unexpected calls have been made to callbacks. 145 // Overrides of this method should always call the parent's class method. 146 void TearDown() override; 147 148 // Calls adapter_->StartDiscoverySessionWithFilter with Low Energy transport, 149 // and this fixture's callbacks expecting success. 150 // Then RunLoop().RunUntilIdle(). 151 virtual void StartLowEnergyDiscoverySession(); 152 153 // Calls adapter_->StartDiscoverySessionWithFilter with Low Energy transport, 154 // and this fixture's callbacks expecting error. 155 // Then RunLoop().RunUntilIdle(). 156 void StartLowEnergyDiscoverySessionExpectedToFail(); 157 158 // Check if Low Energy is available. 159 virtual bool PlatformSupportsLowEnergy() = 0; 160 161 // Initializes the BluetoothAdapter |adapter_| with the system adapter. InitWithDefaultAdapter()162 virtual void InitWithDefaultAdapter() {} 163 164 // Initializes the BluetoothAdapter |adapter_| with the system adapter forced 165 // to be ignored as if it did not exist. This enables tests for when an 166 // adapter is not present on the system. InitWithoutDefaultAdapter()167 virtual void InitWithoutDefaultAdapter() {} 168 169 // Initializes the BluetoothAdapter |adapter_| with a fake adapter that can be 170 // controlled by this test fixture. InitWithFakeAdapter()171 virtual void InitWithFakeAdapter() {} 172 173 // Similar to InitWithFakeAdapter(), but simulates a state where we fail to 174 // get access to the underlying radio. InitFakeAdapterWithoutRadio()175 virtual void InitFakeAdapterWithoutRadio() {} 176 177 // Configures the fake adapter to lack the necessary permissions to scan for 178 // devices. Returns false if the current platform always has permission. 179 virtual bool DenyPermission(); 180 181 // Simulates a failure during a pending BluetoothAdapter::SetPowered() 182 // operation. SimulateAdapterPowerFailure()183 virtual void SimulateAdapterPowerFailure() {} 184 185 // Simulates the Adapter being switched on. SimulateAdapterPoweredOn()186 virtual void SimulateAdapterPoweredOn() {} 187 188 // Simulates the Adapter being switched off. SimulateAdapterPoweredOff()189 virtual void SimulateAdapterPoweredOff() {} 190 191 // Create a fake Low Energy device and discover it. 192 // |device_ordinal| with the same device address stands for the same fake 193 // device with different properties. 194 // For example: 195 // SimulateLowEnergyDevice(2); << First call will create a device with address 196 // kTestDeviceAddress1 197 // SimulateLowEnergyDevice(3); << Second call will update changes to the 198 // device of address kTestDeviceAddress1. 199 // 200 // |device_ordinal| selects between multiple fake device data sets to produce: 201 // 1: Name: kTestDeviceName 202 // Address: kTestDeviceAddress1 203 // RSSI: TestRSSI::LOWEST 204 // Flags: 0x04 205 // Advertised UUIDs: {kTestUUIDGenericAccess, kTestUUIDGenericAttribute} 206 // Service Data: {kTestUUIDHeartRate: [1]} 207 // ManufacturerData: {kTestManufacturerId: [1, 2, 3, 4]} 208 // Tx Power: TestTxPower::LOWEST 209 // 2: Name: kTestDeviceName 210 // Address: kTestDeviceAddress1 211 // RSSI: TestRSSI::LOWER 212 // Flags: 0x05 213 // Advertised UUIDs: {kTestUUIDImmediateAlert, kTestUUIDLinkLoss} 214 // Service Data: {kTestUUIDHeartRate: [], 215 // kTestUUIDImmediateAlert: [0, 2]} 216 // ManufacturerData: {kTestManufacturerId: []} 217 // Tx Power: TestTxPower::LOWER 218 // 3: Name: kTestDeviceNameEmpty 219 // Address: kTestDeviceAddress1 220 // RSSI: TestRSSI::LOW 221 // No Flags 222 // No Advertised UUIDs 223 // No Service Data 224 // No Manufacturer Data 225 // No Tx Power 226 // 4: Name: kTestDeviceNameEmpty 227 // Address: kTestDeviceAddress2 228 // RSSI: TestRSSI::MEDIUM 229 // No Flags 230 // No Advertised UUIDs 231 // No Service Data 232 // No Manufacturer Data 233 // No Tx Power 234 // 5: No name device 235 // Address: kTestDeviceAddress1 236 // RSSI: TestRSSI::HIGH 237 // Flags: 0x06 238 // No Advertised UUIDs 239 // No Service Data 240 // No Tx Power 241 // 6: Name: kTestDeviceName 242 // Address: kTestDeviceAddress2 243 // Flags: 0x18 244 // RSSI: TestRSSI::LOWEST 245 // No Advertised UUIDs 246 // No Service Data 247 // No Manufacturer Data 248 // No Tx Power 249 // Supports BR/EDR and LE. 250 // 7: Name: kTestDeviceNameU2f 251 // Address: kTestDeviceAddress1 252 // Flags: 0x07 253 // RSSI: TestRSSI::LOWEST 254 // Advertised UUIDs: {kTestUUIDU2f} 255 // Service Data: {kTestUUIDU2fControlPointLength: [0, 20]} 256 // No Manufacturer Data 257 // No Tx Power 258 // 8: Name: kTestDeviceNameCable; 259 // Address: kTestDeviceAddress1; 260 // Flags: 0x07; 261 // RSSI: static_cast<int>(TestRSSI::LOWEST); 262 // Advertised UUIDs: {BluetoothUUID(kTestUUIDU2f)}; 263 // Service Data: { 264 // {BluetoothUUID(kTestUUIDCableAdvertisement128), 265 // std::vector<uint8_t>(std::begin(kTestCableEid), 266 // std::end(kTestCableEid))}}; 267 // 9: Name: kTestDeviceNameCable; 268 // Address: kTestDeviceAddress2; 269 // Flags: = 0x07; 270 // RSSI: static_cast<int>(TestRSSI::LOWEST); 271 // Advertised UUIDs: { 272 // BluetoothUUID(kTestUUIDCableAdvertisement16), 273 // BluetoothUUID(kTestUuidFormattedClientEid)}; 274 275 virtual BluetoothDevice* SimulateLowEnergyDevice(int device_ordinal); 276 277 // Simulates a signal by the OS that an ongoing discovery aborted because of 278 // some unexpected error. 279 virtual void SimulateLowEnergyDiscoveryFailure(); 280 281 // Simulates a connected low energy device. Used before starting a low energy 282 // discovey session. SimulateConnectedLowEnergyDevice(ConnectedDeviceType device_ordinal)283 virtual void SimulateConnectedLowEnergyDevice( 284 ConnectedDeviceType device_ordinal) {} 285 286 // Create a fake classic device and discover it. The device will have 287 // name kTestDeviceName, no advertised UUIDs and address kTestDeviceAddress3. 288 virtual BluetoothDevice* SimulateClassicDevice(); 289 290 // Simulates a change in |device|'s pairing state. SimulateDevicePaired(BluetoothDevice * device,bool is_paired)291 virtual void SimulateDevicePaired(BluetoothDevice* device, bool is_paired) {} 292 293 // Sets |device|'s pairing code to |pin_code|. SimulatePairingPinCode(BluetoothDevice * device,std::string pin_code)294 virtual void SimulatePairingPinCode(BluetoothDevice* device, 295 std::string pin_code) {} 296 297 // Simulates a successful registration of |advertisement|. SimulateAdvertisementStarted(BluetoothAdvertisement * advertisement)298 virtual void SimulateAdvertisementStarted( 299 BluetoothAdvertisement* advertisement) {} 300 301 // Simulates a successful unregistration of |advertisement|. SimulateAdvertisementStopped(BluetoothAdvertisement * advertisement)302 virtual void SimulateAdvertisementStopped( 303 BluetoothAdvertisement* advertisement) {} 304 305 // Simulates a failure of either registering or unregistering |advertisement| 306 // with error code |error_code|. SimulateAdvertisementError(BluetoothAdvertisement * advertisement,BluetoothAdvertisement::ErrorCode error_code)307 virtual void SimulateAdvertisementError( 308 BluetoothAdvertisement* advertisement, 309 BluetoothAdvertisement::ErrorCode error_code) {} 310 311 // Remembers |device|'s platform specific object to be used in a 312 // subsequent call to methods such as SimulateGattServicesDiscovered that 313 // accept a nullptr value to select this remembered characteristic. This 314 // enables tests where the platform attempts to reference device 315 // objects after the Chrome objects have been deleted, e.g. with DeleteDevice. RememberDeviceForSubsequentAction(BluetoothDevice * device)316 virtual void RememberDeviceForSubsequentAction(BluetoothDevice* device) {} 317 318 // Performs a GATT connection to the given device and returns whether it was 319 // successful. The |service_uuid| is passed to 320 // |BluetoothDevice::CreateGattConnection|; see the documentation for it 321 // there. The callback is called to complete the GATT connection. If not 322 // given, |SimulateGattConnection| is called but the callback argument lets 323 // one override that. 324 bool ConnectGatt(BluetoothDevice* device, 325 base::Optional<BluetoothUUID> service_uuid = base::nullopt, 326 base::Optional<base::OnceCallback<void(BluetoothDevice*)>> = 327 base::nullopt); 328 329 // GetTargetGattService returns the specific GATT service, if any, that was 330 // targeted for discovery, i.e. via the |service_uuid| argument to 331 // |CreateGattConnection|. 332 virtual base::Optional<BluetoothUUID> GetTargetGattService( 333 BluetoothDevice* device); 334 335 // Simulates success of implementation details of CreateGattConnection. SimulateGattConnection(BluetoothDevice * device)336 virtual void SimulateGattConnection(BluetoothDevice* device) {} 337 338 // Simulates failure of CreateGattConnection with the given error code. SimulateGattConnectionError(BluetoothDevice * device,BluetoothDevice::ConnectErrorCode)339 virtual void SimulateGattConnectionError(BluetoothDevice* device, 340 BluetoothDevice::ConnectErrorCode) {} 341 342 // Simulates GattConnection disconnecting. SimulateGattDisconnection(BluetoothDevice * device)343 virtual void SimulateGattDisconnection(BluetoothDevice* device) {} 344 345 // Simulates Error in GattConnection disconnecting. SimulateGattDisconnectionError(BluetoothDevice * device)346 virtual void SimulateGattDisconnectionError(BluetoothDevice* device) {} 347 348 // Simulates an event where the OS breaks the Gatt connection. Defaults to 349 // SimulateGattDisconnection(device). 350 virtual void SimulateDeviceBreaksConnection(BluetoothDevice* device); 351 352 // Simulates a device changing its name property while a GATT connection is 353 // open. SimulateGattNameChange(BluetoothDevice * device,const std::string & new_name)354 virtual void SimulateGattNameChange(BluetoothDevice* device, 355 const std::string& new_name) {} 356 357 // Simulates a connection status change to disconnect. SimulateStatusChangeToDisconnect(BluetoothDevice * device)358 virtual void SimulateStatusChangeToDisconnect(BluetoothDevice* device) {} 359 360 // Simulates success of discovering services. |uuids| and |blocked_uuids| are 361 // used to create a service for each UUID string. Multiple UUIDs with the same 362 // value produce multiple service instances. UUIDs in the |blocked_uuids| list 363 // create services which cannot be accessed (WinRT-only). 364 virtual void SimulateGattServicesDiscovered( 365 BluetoothDevice* device, 366 const std::vector<std::string>& uuids, 367 const std::vector<std::string>& blocked_uuids = {}) {} 368 369 // Simulates a GATT Services changed event. SimulateGattServicesChanged(BluetoothDevice * device)370 virtual void SimulateGattServicesChanged(BluetoothDevice* device) {} 371 372 // Simulates remove of a |service|. SimulateGattServiceRemoved(BluetoothRemoteGattService * service)373 virtual void SimulateGattServiceRemoved(BluetoothRemoteGattService* service) { 374 } 375 376 // Simulates failure to discover services. SimulateGattServicesDiscoveryError(BluetoothDevice * device)377 virtual void SimulateGattServicesDiscoveryError(BluetoothDevice* device) {} 378 379 // Simulates a Characteristic on a service. SimulateGattCharacteristic(BluetoothRemoteGattService * service,const std::string & uuid,int properties)380 virtual void SimulateGattCharacteristic(BluetoothRemoteGattService* service, 381 const std::string& uuid, 382 int properties) {} 383 384 // Simulates remove of a |characteristic| from |service|. SimulateGattCharacteristicRemoved(BluetoothRemoteGattService * service,BluetoothRemoteGattCharacteristic * characteristic)385 virtual void SimulateGattCharacteristicRemoved( 386 BluetoothRemoteGattService* service, 387 BluetoothRemoteGattCharacteristic* characteristic) {} 388 389 // Remembers |characteristic|'s platform specific object to be used in a 390 // subsequent call to methods such as SimulateGattCharacteristicRead that 391 // accept a nullptr value to select this remembered characteristic. This 392 // enables tests where the platform attempts to reference characteristic 393 // objects after the Chrome objects have been deleted, e.g. with DeleteDevice. RememberCharacteristicForSubsequentAction(BluetoothRemoteGattCharacteristic * characteristic)394 virtual void RememberCharacteristicForSubsequentAction( 395 BluetoothRemoteGattCharacteristic* characteristic) {} 396 397 // Remembers |characteristic|'s Client Characteristic Configuration (CCC) 398 // descriptor's platform specific object to be used in a subsequent call to 399 // methods such as SimulateGattNotifySessionStarted. This enables tests where 400 // the platform attempts to reference descriptor objects after the Chrome 401 // objects have been deleted, e.g. with DeleteDevice. RememberCCCDescriptorForSubsequentAction(BluetoothRemoteGattCharacteristic * characteristic)402 virtual void RememberCCCDescriptorForSubsequentAction( 403 BluetoothRemoteGattCharacteristic* characteristic) {} 404 405 // Simulates a Characteristic Set Notify success. 406 // If |characteristic| is null, acts upon the characteristic & CCC 407 // descriptor provided to RememberCharacteristicForSubsequentAction & 408 // RememberCCCDescriptorForSubsequentAction. SimulateGattNotifySessionStarted(BluetoothRemoteGattCharacteristic * characteristic)409 virtual void SimulateGattNotifySessionStarted( 410 BluetoothRemoteGattCharacteristic* characteristic) {} 411 412 // Simulates a Characteristic Set Notify error. 413 // If |characteristic| is null, acts upon the characteristic & CCC 414 // descriptor provided to RememberCharacteristicForSubsequentAction & 415 // RememberCCCDescriptorForSubsequentAction. SimulateGattNotifySessionStartError(BluetoothRemoteGattCharacteristic * characteristic,BluetoothRemoteGattService::GattErrorCode error_code)416 virtual void SimulateGattNotifySessionStartError( 417 BluetoothRemoteGattCharacteristic* characteristic, 418 BluetoothRemoteGattService::GattErrorCode error_code) {} 419 420 // Simulates a Characteristic Stop Notify completed. 421 // If |characteristic| is null, acts upon the characteristic & CCC 422 // descriptor provided to RememberCharacteristicForSubsequentAction & 423 // RememberCCCDescriptorForSubsequentAction. SimulateGattNotifySessionStopped(BluetoothRemoteGattCharacteristic * characteristic)424 virtual void SimulateGattNotifySessionStopped( 425 BluetoothRemoteGattCharacteristic* characteristic) {} 426 427 // Simulates a Characteristic Stop Notify error. 428 // If |characteristic| is null, acts upon the characteristic & CCC 429 // descriptor provided to RememberCharacteristicForSubsequentAction & 430 // RememberCCCDescriptorForSubsequentAction. SimulateGattNotifySessionStopError(BluetoothRemoteGattCharacteristic * characteristic,BluetoothRemoteGattService::GattErrorCode error_code)431 virtual void SimulateGattNotifySessionStopError( 432 BluetoothRemoteGattCharacteristic* characteristic, 433 BluetoothRemoteGattService::GattErrorCode error_code) {} 434 435 // Simulates a Characteristic Set Notify operation failing synchronously once 436 // for an unknown reason. SimulateGattCharacteristicSetNotifyWillFailSynchronouslyOnce(BluetoothRemoteGattCharacteristic * characteristic)437 virtual void SimulateGattCharacteristicSetNotifyWillFailSynchronouslyOnce( 438 BluetoothRemoteGattCharacteristic* characteristic) {} 439 440 // Simulates a Characteristic Changed operation with updated |value|. SimulateGattCharacteristicChanged(BluetoothRemoteGattCharacteristic * characteristic,const std::vector<uint8_t> & value)441 virtual void SimulateGattCharacteristicChanged( 442 BluetoothRemoteGattCharacteristic* characteristic, 443 const std::vector<uint8_t>& value) {} 444 445 // Simulates a Characteristic Read operation succeeding, returning |value|. 446 // If |characteristic| is null, acts upon the characteristic provided to 447 // RememberCharacteristicForSubsequentAction. SimulateGattCharacteristicRead(BluetoothRemoteGattCharacteristic * characteristic,const std::vector<uint8_t> & value)448 virtual void SimulateGattCharacteristicRead( 449 BluetoothRemoteGattCharacteristic* characteristic, 450 const std::vector<uint8_t>& value) {} 451 452 // Simulates a Characteristic Read operation failing with a GattErrorCode. SimulateGattCharacteristicReadError(BluetoothRemoteGattCharacteristic * characteristic,BluetoothRemoteGattService::GattErrorCode)453 virtual void SimulateGattCharacteristicReadError( 454 BluetoothRemoteGattCharacteristic* characteristic, 455 BluetoothRemoteGattService::GattErrorCode) {} 456 457 // Simulates a Characteristic Read operation failing synchronously once for an 458 // unknown reason. SimulateGattCharacteristicReadWillFailSynchronouslyOnce(BluetoothRemoteGattCharacteristic * characteristic)459 virtual void SimulateGattCharacteristicReadWillFailSynchronouslyOnce( 460 BluetoothRemoteGattCharacteristic* characteristic) {} 461 462 // Simulates a Characteristic Write operation succeeding, returning |value|. 463 // If |characteristic| is null, acts upon the characteristic provided to 464 // RememberCharacteristicForSubsequentAction. SimulateGattCharacteristicWrite(BluetoothRemoteGattCharacteristic * characteristic)465 virtual void SimulateGattCharacteristicWrite( 466 BluetoothRemoteGattCharacteristic* characteristic) {} 467 468 // Simulates a Characteristic Write operation failing with a GattErrorCode. SimulateGattCharacteristicWriteError(BluetoothRemoteGattCharacteristic * characteristic,BluetoothRemoteGattService::GattErrorCode)469 virtual void SimulateGattCharacteristicWriteError( 470 BluetoothRemoteGattCharacteristic* characteristic, 471 BluetoothRemoteGattService::GattErrorCode) {} 472 473 // Simulates a Characteristic Write operation failing synchronously once for 474 // an unknown reason. SimulateGattCharacteristicWriteWillFailSynchronouslyOnce(BluetoothRemoteGattCharacteristic * characteristic)475 virtual void SimulateGattCharacteristicWriteWillFailSynchronouslyOnce( 476 BluetoothRemoteGattCharacteristic* characteristic) {} 477 478 // Simulates a Descriptor on a service. SimulateGattDescriptor(BluetoothRemoteGattCharacteristic * characteristic,const std::string & uuid)479 virtual void SimulateGattDescriptor( 480 BluetoothRemoteGattCharacteristic* characteristic, 481 const std::string& uuid) {} 482 483 // Simulates reading a value from a locally hosted GATT characteristic by a 484 // remote central device. Returns the value that was read from the local 485 // GATT characteristic in the value callback. SimulateLocalGattCharacteristicValueReadRequest(BluetoothDevice * from_device,BluetoothLocalGattCharacteristic * characteristic,BluetoothLocalGattService::Delegate::ValueCallback value_callback,base::OnceClosure error_callback)486 virtual void SimulateLocalGattCharacteristicValueReadRequest( 487 BluetoothDevice* from_device, 488 BluetoothLocalGattCharacteristic* characteristic, 489 BluetoothLocalGattService::Delegate::ValueCallback value_callback, 490 base::OnceClosure error_callback) {} 491 492 // Simulates write a value to a locally hosted GATT characteristic by a 493 // remote central device. SimulateLocalGattCharacteristicValueWriteRequest(BluetoothDevice * from_device,BluetoothLocalGattCharacteristic * characteristic,const std::vector<uint8_t> & value_to_write,base::OnceClosure success_callback,base::OnceClosure error_callback)494 virtual void SimulateLocalGattCharacteristicValueWriteRequest( 495 BluetoothDevice* from_device, 496 BluetoothLocalGattCharacteristic* characteristic, 497 const std::vector<uint8_t>& value_to_write, 498 base::OnceClosure success_callback, 499 base::OnceClosure error_callback) {} 500 501 // Simulates prepare write a value to a locally hosted GATT characteristic by 502 // a remote central device. SimulateLocalGattCharacteristicValuePrepareWriteRequest(BluetoothDevice * from_device,BluetoothLocalGattCharacteristic * characteristic,const std::vector<uint8_t> & value_to_write,int offset,bool has_subsequent_write,base::OnceClosure success_callback,base::OnceClosure error_callback)503 virtual void SimulateLocalGattCharacteristicValuePrepareWriteRequest( 504 BluetoothDevice* from_device, 505 BluetoothLocalGattCharacteristic* characteristic, 506 const std::vector<uint8_t>& value_to_write, 507 int offset, 508 bool has_subsequent_write, 509 base::OnceClosure success_callback, 510 base::OnceClosure error_callback) {} 511 512 // Simulates reading a value from a locally hosted GATT descriptor by a 513 // remote central device. Returns the value that was read from the local 514 // GATT descriptor in the value callback. SimulateLocalGattDescriptorValueReadRequest(BluetoothDevice * from_device,BluetoothLocalGattDescriptor * descriptor,BluetoothLocalGattService::Delegate::ValueCallback value_callback,base::OnceClosure error_callback)515 virtual void SimulateLocalGattDescriptorValueReadRequest( 516 BluetoothDevice* from_device, 517 BluetoothLocalGattDescriptor* descriptor, 518 BluetoothLocalGattService::Delegate::ValueCallback value_callback, 519 base::OnceClosure error_callback) {} 520 521 // Simulates write a value to a locally hosted GATT descriptor by a 522 // remote central device. SimulateLocalGattDescriptorValueWriteRequest(BluetoothDevice * from_device,BluetoothLocalGattDescriptor * descriptor,const std::vector<uint8_t> & value_to_write,base::OnceClosure success_callback,base::OnceClosure error_callback)523 virtual void SimulateLocalGattDescriptorValueWriteRequest( 524 BluetoothDevice* from_device, 525 BluetoothLocalGattDescriptor* descriptor, 526 const std::vector<uint8_t>& value_to_write, 527 base::OnceClosure success_callback, 528 base::OnceClosure error_callback) {} 529 530 // Simulates starting or stopping a notification session for a locally 531 // hosted GATT characteristic by a remote device. Returns false if we were 532 // not able to start or stop notifications. 533 virtual bool SimulateLocalGattCharacteristicNotificationsRequest( 534 BluetoothLocalGattCharacteristic* characteristic, 535 bool start); 536 537 // Returns the value for the last notification that was sent on this 538 // characteristic. 539 virtual std::vector<uint8_t> LastNotifactionValueForCharacteristic( 540 BluetoothLocalGattCharacteristic* characteristic); 541 542 // Remembers |descriptor|'s platform specific object to be used in a 543 // subsequent call to methods such as SimulateGattDescriptorRead that 544 // accept a nullptr value to select this remembered descriptor. This 545 // enables tests where the platform attempts to reference descriptor 546 // objects after the Chrome objects have been deleted, e.g. with DeleteDevice. RememberDescriptorForSubsequentAction(BluetoothRemoteGattDescriptor * descriptor)547 virtual void RememberDescriptorForSubsequentAction( 548 BluetoothRemoteGattDescriptor* descriptor) {} 549 550 // Simulates a Descriptor Read operation succeeding, returning |value|. 551 // If |descriptor| is null, acts upon the descriptor provided to 552 // RememberDescriptorForSubsequentAction. SimulateGattDescriptorRead(BluetoothRemoteGattDescriptor * descriptor,const std::vector<uint8_t> & value)553 virtual void SimulateGattDescriptorRead( 554 BluetoothRemoteGattDescriptor* descriptor, 555 const std::vector<uint8_t>& value) {} 556 557 // Simulates a Descriptor Read operation failing with a GattErrorCode. SimulateGattDescriptorReadError(BluetoothRemoteGattDescriptor * descriptor,BluetoothRemoteGattService::GattErrorCode)558 virtual void SimulateGattDescriptorReadError( 559 BluetoothRemoteGattDescriptor* descriptor, 560 BluetoothRemoteGattService::GattErrorCode) {} 561 562 // Simulates a Descriptor Read operation failing synchronously once for an 563 // unknown reason. SimulateGattDescriptorReadWillFailSynchronouslyOnce(BluetoothRemoteGattDescriptor * descriptor)564 virtual void SimulateGattDescriptorReadWillFailSynchronouslyOnce( 565 BluetoothRemoteGattDescriptor* descriptor) {} 566 567 // Simulates a Descriptor Write operation succeeding, returning |value|. 568 // If |descriptor| is null, acts upon the descriptor provided to 569 // RememberDescriptorForSubsequentAction. SimulateGattDescriptorWrite(BluetoothRemoteGattDescriptor * descriptor)570 virtual void SimulateGattDescriptorWrite( 571 BluetoothRemoteGattDescriptor* descriptor) {} 572 573 // Simulates a Descriptor Write operation failing with a GattErrorCode. SimulateGattDescriptorWriteError(BluetoothRemoteGattDescriptor * descriptor,BluetoothRemoteGattService::GattErrorCode)574 virtual void SimulateGattDescriptorWriteError( 575 BluetoothRemoteGattDescriptor* descriptor, 576 BluetoothRemoteGattService::GattErrorCode) {} 577 578 // Simulates a Descriptor Update operation failing with a GattErrorCode. SimulateGattDescriptorUpdateError(BluetoothRemoteGattDescriptor * descriptor,BluetoothRemoteGattService::GattErrorCode)579 virtual void SimulateGattDescriptorUpdateError( 580 BluetoothRemoteGattDescriptor* descriptor, 581 BluetoothRemoteGattService::GattErrorCode) {} 582 583 // Simulates a Descriptor Write operation failing synchronously once for 584 // an unknown reason. SimulateGattDescriptorWriteWillFailSynchronouslyOnce(BluetoothRemoteGattDescriptor * descriptor)585 virtual void SimulateGattDescriptorWriteWillFailSynchronouslyOnce( 586 BluetoothRemoteGattDescriptor* descriptor) {} 587 588 // Tests that functions to change the notify value have been called |attempts| 589 // times. 590 virtual void ExpectedChangeNotifyValueAttempts(int attempts); 591 592 // Tests that the notify value is |expected_value_state|. The default 593 // implementation checks that the correct value has been written to the CCC 594 // Descriptor. 595 virtual void ExpectedNotifyValue(NotifyValueState expected_value_state); 596 597 // Returns a list of local GATT services registered with the adapter. 598 virtual std::vector<BluetoothLocalGattService*> RegisteredGattServices(); 599 600 // Removes the device from the adapter and deletes it. 601 virtual void DeleteDevice(BluetoothDevice* device); 602 603 // Callbacks that increment |callback_count_|, |error_callback_count_|: 604 void Callback(Call expected); 605 void CreateAdvertisementCallback(Call expected, 606 scoped_refptr<BluetoothAdvertisement>); 607 void DiscoverySessionCallback(Call expected, 608 std::unique_ptr<BluetoothDiscoverySession>); 609 void GattConnectionCallback(Call expected, 610 std::unique_ptr<BluetoothGattConnection>); 611 void NotifyCallback(Call expected, 612 std::unique_ptr<BluetoothGattNotifySession>); 613 void NotifyCheckForPrecedingCalls( 614 int num_of_preceding_calls, 615 std::unique_ptr<BluetoothGattNotifySession>); 616 void StopNotifyCallback(Call expected); 617 void StopNotifyCheckForPrecedingCalls(int num_of_preceding_calls); 618 void ReadValueCallback(Call expected, const std::vector<uint8_t>& value); 619 void ErrorCallback(Call expected); 620 void AdvertisementErrorCallback(Call expected, 621 BluetoothAdvertisement::ErrorCode error_code); 622 void ConnectErrorCallback(Call expected, 623 enum BluetoothDevice::ConnectErrorCode); 624 void GattErrorCallback(Call expected, 625 BluetoothRemoteGattService::GattErrorCode); 626 void ReentrantStartNotifySessionSuccessCallback( 627 Call expected, 628 BluetoothRemoteGattCharacteristic* characteristic, 629 std::unique_ptr<BluetoothGattNotifySession> notify_session); 630 void ReentrantStartNotifySessionErrorCallback( 631 Call expected, 632 BluetoothRemoteGattCharacteristic* characteristic, 633 bool error_in_reentrant, 634 BluetoothGattService::GattErrorCode error_code); 635 636 // Accessors to get callbacks bound to this fixture: 637 base::OnceClosure GetCallback(Call expected); 638 BluetoothAdapter::CreateAdvertisementCallback GetCreateAdvertisementCallback( 639 Call expected); 640 BluetoothAdapter::DiscoverySessionCallback GetDiscoverySessionCallback( 641 Call expected); 642 BluetoothDevice::GattConnectionCallback GetGattConnectionCallback( 643 Call expected); 644 BluetoothRemoteGattCharacteristic::NotifySessionCallback GetNotifyCallback( 645 Call expected); 646 BluetoothRemoteGattCharacteristic::NotifySessionCallback 647 GetNotifyCheckForPrecedingCalls(int num_of_preceding_calls); 648 base::OnceClosure GetStopNotifyCallback(Call expected); 649 base::OnceClosure GetStopNotifyCheckForPrecedingCalls( 650 int num_of_preceding_calls); 651 BluetoothRemoteGattCharacteristic::ValueCallback GetReadValueCallback( 652 Call expected); 653 BluetoothAdapter::ErrorCallback GetErrorCallback(Call expected); 654 BluetoothAdapter::AdvertisementErrorCallback GetAdvertisementErrorCallback( 655 Call expected); 656 BluetoothDevice::ConnectErrorCallback GetConnectErrorCallback(Call expected); 657 base::OnceCallback<void(BluetoothRemoteGattService::GattErrorCode)> 658 GetGattErrorCallback(Call expected); 659 BluetoothRemoteGattCharacteristic::NotifySessionCallback 660 GetReentrantStartNotifySessionSuccessCallback( 661 Call expected, 662 BluetoothRemoteGattCharacteristic* characteristic); 663 base::OnceCallback<void(BluetoothGattService::GattErrorCode)> 664 GetReentrantStartNotifySessionErrorCallback( 665 Call expected, 666 BluetoothRemoteGattCharacteristic* characteristic, 667 bool error_in_reentrant); 668 669 // Reset all event count members to 0. 670 virtual void ResetEventCounts(); 671 672 void RemoveTimedOutDevices(); 673 674 protected: 675 // Utility method to simplify creading a low energy device of a given 676 // |device_ordinal|. 677 LowEnergyDeviceData GetLowEnergyDeviceData(int device_ordinal) const; 678 679 // A TaskEnvironment is required by some implementations that will 680 // PostTasks and by base::RunLoop().RunUntilIdle() use in this fixture. 681 base::test::TaskEnvironment task_environment_; 682 683 scoped_refptr<BluetoothAdapter> adapter_; 684 std::vector<scoped_refptr<BluetoothAdvertisement>> advertisements_; 685 std::vector<std::unique_ptr<BluetoothDiscoverySession>> discovery_sessions_; 686 std::vector<std::unique_ptr<BluetoothGattConnection>> gatt_connections_; 687 BluetoothAdvertisement::ErrorCode last_advertisement_error_code_ = 688 BluetoothAdvertisement::INVALID_ADVERTISEMENT_ERROR_CODE; 689 enum BluetoothDevice::ConnectErrorCode last_connect_error_code_ = 690 BluetoothDevice::ERROR_UNKNOWN; 691 std::vector<std::unique_ptr<BluetoothGattNotifySession>> notify_sessions_; 692 std::vector<uint8_t> last_read_value_; 693 std::vector<uint8_t> last_write_value_; 694 BluetoothRemoteGattService::GattErrorCode last_gatt_error_code_; 695 696 int callback_count_ = 0; 697 int error_callback_count_ = 0; 698 int gatt_connection_attempts_ = 0; 699 int gatt_disconnection_attempts_ = 0; 700 int gatt_discovery_attempts_ = 0; 701 int gatt_notify_characteristic_attempts_ = 0; 702 int gatt_read_characteristic_attempts_ = 0; 703 int gatt_write_characteristic_attempts_ = 0; 704 int gatt_read_descriptor_attempts_ = 0; 705 int gatt_write_descriptor_attempts_ = 0; 706 707 // The following values are used to make sure the correct callbacks 708 // have been called. They are not reset when calling ResetEventCounts(). 709 int expected_success_callback_calls_ = 0; 710 int expected_error_callback_calls_ = 0; 711 int actual_success_callback_calls_ = 0; 712 int actual_error_callback_calls_ = 0; 713 bool unexpected_success_callback_ = false; 714 bool unexpected_error_callback_ = false; 715 716 base::WeakPtrFactory<BluetoothTestBase> weak_factory_{this}; 717 }; 718 719 } // namespace device 720 721 #endif // DEVICE_BLUETOOTH_TEST_BLUETOOTH_TEST_H_ 722