| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "device/bluetooth/test/bluetooth_test_mac.h" | 5 #include "device/bluetooth/test/bluetooth_test_mac.h" |
| 6 | 6 |
| 7 #include <stdint.h> | 7 #include <stdint.h> |
| 8 | 8 |
| 9 #include "base/mac/foundation_util.h" | 9 #include "base/mac/foundation_util.h" |
| 10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| 11 #include "build/build_config.h" | 11 #include "build/build_config.h" |
| 12 #include "device/bluetooth/bluetooth_adapter_mac.h" | 12 #include "device/bluetooth/bluetooth_adapter_mac.h" |
| 13 #include "device/bluetooth/bluetooth_remote_gatt_characteristic_mac.h" | |
| 14 #include "device/bluetooth/bluetooth_remote_gatt_service_mac.h" | 13 #include "device/bluetooth/bluetooth_remote_gatt_service_mac.h" |
| 15 #include "device/bluetooth/test/mock_bluetooth_cbcharacteristic_mac.h" | |
| 16 #include "device/bluetooth/test/mock_bluetooth_cbperipheral_mac.h" | 14 #include "device/bluetooth/test/mock_bluetooth_cbperipheral_mac.h" |
| 17 #include "device/bluetooth/test/mock_bluetooth_cbservice_mac.h" | |
| 18 #include "device/bluetooth/test/mock_bluetooth_central_manager_mac.h" | 15 #include "device/bluetooth/test/mock_bluetooth_central_manager_mac.h" |
| 19 #include "device/bluetooth/test/test_bluetooth_adapter_observer.h" | 16 #include "device/bluetooth/test/test_bluetooth_adapter_observer.h" |
| 20 #include "third_party/ocmock/OCMock/OCMock.h" | 17 #include "third_party/ocmock/OCMock/OCMock.h" |
| 21 | 18 |
| 22 #import <CoreBluetooth/CoreBluetooth.h> | 19 #import <CoreBluetooth/CoreBluetooth.h> |
| 23 | 20 |
| 24 using base::mac::ObjCCast; | 21 using base::mac::ObjCCast; |
| 25 using base::scoped_nsobject; | 22 using base::scoped_nsobject; |
| 26 | 23 |
| 27 namespace { | |
| 28 | |
| 29 void DidDiscoverServices(MockCBPeripheral* peripheral_mock) { | |
| 30 [peripheral_mock didDiscoverServicesWithError:nil]; | |
| 31 // BluetoothLowEnergyDeviceMac is expected to call | |
| 32 // -[CBPeripheral discoverCharacteristics:forService:] for each services, | |
| 33 // so -[<CBPeripheralDelegate peripheral:didDiscoverCharacteristicsForService: | |
| 34 // error:] needs to be called | |
| 35 [peripheral_mock didDiscoverCharactericsForAllServices]; | |
| 36 } | |
| 37 } // namespace | |
| 38 | |
| 39 namespace device { | 24 namespace device { |
| 40 | 25 |
| 41 // This class hides Objective-C from bluetooth_test_mac.h. | 26 // This class hides Objective-C from bluetooth_test_mac.h. |
| 42 class BluetoothTestMac::ScopedMockCentralManager { | 27 class BluetoothTestMac::ScopedMockCentralManager { |
| 43 public: | 28 public: |
| 44 explicit ScopedMockCentralManager(MockCentralManager* mock_central_manager) { | 29 explicit ScopedMockCentralManager(MockCentralManager* mock_central_manager) { |
| 45 mock_central_manager_.reset(mock_central_manager); | 30 mock_central_manager_.reset(mock_central_manager); |
| 46 } | 31 } |
| 47 | 32 |
| 48 // Returns MockCentralManager instance. | 33 // Returns MockCentralManager instance. |
| (...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 245 BluetoothLowEnergyDeviceMac* device_mac = | 230 BluetoothLowEnergyDeviceMac* device_mac = |
| 246 static_cast<BluetoothLowEnergyDeviceMac*>(device); | 231 static_cast<BluetoothLowEnergyDeviceMac*>(device); |
| 247 CBPeripheral* peripheral = device_mac->GetPeripheral(); | 232 CBPeripheral* peripheral = device_mac->GetPeripheral(); |
| 248 MockCBPeripheral* peripheral_mock = ObjCCast<MockCBPeripheral>(peripheral); | 233 MockCBPeripheral* peripheral_mock = ObjCCast<MockCBPeripheral>(peripheral); |
| 249 scoped_nsobject<NSMutableArray> services([[NSMutableArray alloc] init]); | 234 scoped_nsobject<NSMutableArray> services([[NSMutableArray alloc] init]); |
| 250 for (auto uuid : uuids) { | 235 for (auto uuid : uuids) { |
| 251 CBUUID* cb_service_uuid = [CBUUID UUIDWithString:@(uuid.c_str())]; | 236 CBUUID* cb_service_uuid = [CBUUID UUIDWithString:@(uuid.c_str())]; |
| 252 [services addObject:cb_service_uuid]; | 237 [services addObject:cb_service_uuid]; |
| 253 } | 238 } |
| 254 [peripheral_mock addServices:services]; | 239 [peripheral_mock addServices:services]; |
| 255 DidDiscoverServices(peripheral_mock); | 240 [peripheral_mock didDiscoverServicesWithError:nil]; |
| 256 } | 241 } |
| 257 | 242 |
| 258 void BluetoothTestMac::SimulateGattServiceRemoved( | 243 void BluetoothTestMac::SimulateGattServiceRemoved( |
| 259 BluetoothRemoteGattService* service) { | 244 BluetoothRemoteGattService* service) { |
| 260 BluetoothUUID bluetooth_service_uuid = service->GetUUID(); | 245 BluetoothUUID bluetooth_service_uuid = service->GetUUID(); |
| 261 std::string service_uuid_string = bluetooth_service_uuid.canonical_value(); | 246 std::string service_uuid_string = bluetooth_service_uuid.canonical_value(); |
| 262 BluetoothRemoteGattServiceMac* mac_gatt_service = | 247 BluetoothRemoteGattServiceMac* mac_gatt_service = |
| 263 static_cast<BluetoothRemoteGattServiceMac*>(service); | 248 static_cast<BluetoothRemoteGattServiceMac*>(service); |
| 264 BluetoothDevice* device = service->GetDevice(); | 249 BluetoothDevice* device = service->GetDevice(); |
| 265 BluetoothLowEnergyDeviceMac* device_mac = | 250 BluetoothLowEnergyDeviceMac* device_mac = |
| 266 static_cast<BluetoothLowEnergyDeviceMac*>(device); | 251 static_cast<BluetoothLowEnergyDeviceMac*>(device); |
| 267 CBPeripheral* peripheral = device_mac->GetPeripheral(); | 252 CBPeripheral* peripheral = device_mac->GetPeripheral(); |
| 268 MockCBPeripheral* peripheral_mock = ObjCCast<MockCBPeripheral>(peripheral); | 253 MockCBPeripheral* peripheral_mock = ObjCCast<MockCBPeripheral>(peripheral); |
| 269 [peripheral_mock removeService:mac_gatt_service->GetService()]; | 254 [peripheral_mock removeService:mac_gatt_service->GetService()]; |
| 270 // After -[MockCBPeripheral removeService:], BluetoothLowEnergyDeviceMac is | 255 [peripheral_mock didDiscoverServicesWithError:nil]; |
| 271 // expected to call -[CBPeripheral discoverServices:] | |
| 272 DidDiscoverServices(peripheral_mock); | |
| 273 } | |
| 274 | |
| 275 void BluetoothTestMac::SimulateGattCharacteristic( | |
| 276 BluetoothRemoteGattService* service, | |
| 277 const std::string& uuid, | |
| 278 int properties) { | |
| 279 BluetoothRemoteGattServiceMac* mac_gatt_service = | |
| 280 static_cast<BluetoothRemoteGattServiceMac*>(service); | |
| 281 CBService* cb_service = mac_gatt_service->GetService(); | |
| 282 MockCBService* service_mock = ObjCCast<MockCBService>(cb_service); | |
| 283 CBUUID* cb_uuid = [CBUUID UUIDWithString:@(uuid.c_str())]; | |
| 284 [service_mock addCharacteristicWithUUID:cb_uuid properties:properties]; | |
| 285 MockCBPeripheral* peripheral_mock = GetMockCBPeripheral(service); | |
| 286 [peripheral_mock didModifyServices:@[]]; | |
| 287 // After -[MockCBPeripheral didModifyServices:], BluetoothLowEnergyDeviceMac | |
| 288 // is expected to call -[CBPeripheral discoverServices:] | |
| 289 DidDiscoverServices(peripheral_mock); | |
| 290 } | |
| 291 | |
| 292 void BluetoothTestMac::SimulateGattCharacteristicRemoved( | |
| 293 BluetoothRemoteGattService* service, | |
| 294 BluetoothRemoteGattCharacteristic* characteristic) { | |
| 295 MockCBPeripheral* peripheral_mock = GetMockCBPeripheral(service); | |
| 296 BluetoothRemoteGattServiceMac* mac_gatt_service = | |
| 297 static_cast<BluetoothRemoteGattServiceMac*>(service); | |
| 298 CBService* cb_service = mac_gatt_service->GetService(); | |
| 299 MockCBService* service_mock = ObjCCast<MockCBService>(cb_service); | |
| 300 BluetoothRemoteGattCharacteristicMac* characteristic_mac = | |
| 301 static_cast<BluetoothRemoteGattCharacteristicMac*>(characteristic); | |
| 302 CBCharacteristic* cb_characteristic = | |
| 303 characteristic_mac->GetCBCharacteristic(); | |
| 304 MockCBCharacteristic* characteristic_mock = | |
| 305 ObjCCast<MockCBCharacteristic>(cb_characteristic); | |
| 306 [service_mock removeCharacteristicMock:characteristic_mock]; | |
| 307 DidDiscoverServices(peripheral_mock); | |
| 308 } | 256 } |
| 309 | 257 |
| 310 void BluetoothTestMac::OnFakeBluetoothDeviceConnectGattCalled() { | 258 void BluetoothTestMac::OnFakeBluetoothDeviceConnectGattCalled() { |
| 311 gatt_connection_attempts_++; | 259 gatt_connection_attempts_++; |
| 312 } | 260 } |
| 313 | 261 |
| 314 void BluetoothTestMac::OnFakeBluetoothGattDisconnect() { | 262 void BluetoothTestMac::OnFakeBluetoothGattDisconnect() { |
| 315 gatt_disconnection_attempts_++; | 263 gatt_disconnection_attempts_++; |
| 316 } | 264 } |
| 317 | 265 |
| 318 void BluetoothTestMac::OnFakeBluetoothServiceDiscovery() { | 266 void BluetoothTestMac::OnFakeBluetoothServiceDiscovery() { |
| 319 gatt_discovery_attempts_++; | 267 gatt_discovery_attempts_++; |
| 320 } | 268 } |
| 321 | 269 |
| 322 MockCBPeripheral* BluetoothTestMac::GetMockCBPeripheral( | |
| 323 BluetoothRemoteGattService* service) const { | |
| 324 BluetoothDevice* device = service->GetDevice(); | |
| 325 BluetoothLowEnergyDeviceMac* device_mac = | |
| 326 static_cast<BluetoothLowEnergyDeviceMac*>(device); | |
| 327 CBPeripheral* cb_peripheral = device_mac->GetPeripheral(); | |
| 328 return ObjCCast<MockCBPeripheral>(cb_peripheral); | |
| 329 } | |
| 330 | |
| 331 // Utility function for generating new (CBUUID, address) pairs where CBUUID | 270 // Utility function for generating new (CBUUID, address) pairs where CBUUID |
| 332 // hashes to address. For use when adding a new device address to the testing | 271 // hashes to address. For use when adding a new device address to the testing |
| 333 // suite because CoreBluetooth peripherals have CBUUIDs in place of addresses, | 272 // suite because CoreBluetooth peripherals have CBUUIDs in place of addresses, |
| 334 // and we construct fake addresses for them by hashing the CBUUID. By changing | 273 // and we construct fake addresses for them by hashing the CBUUID. By changing |
| 335 // |target| the user can generate sequentially numbered test addresses. | 274 // |target| the user can generate sequentially numbered test addresses. |
| 336 // | 275 // |
| 337 // std::string BluetoothTestMac::FindCBUUIDForHashTarget() { | 276 // std::string BluetoothTestMac::FindCBUUIDForHashTarget() { |
| 338 // // The desired first 6 digits of the hash. For example 0100000, 020000, | 277 // // The desired first 6 digits of the hash. For example 0100000, 020000, |
| 339 // // 030000, ... | 278 // // 030000, ... |
| 340 // const std::string target = "010000"; | 279 // const std::string target = "010000"; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 357 // crypto::SHA256HashString(input_str, raw, sizeof(raw)); | 296 // crypto::SHA256HashString(input_str, raw, sizeof(raw)); |
| 358 // if (base::HexEncode(raw, sizeof(raw)) == target) { | 297 // if (base::HexEncode(raw, sizeof(raw)) == target) { |
| 359 // return input_str; | 298 // return input_str; |
| 360 // } | 299 // } |
| 361 // ++input[0]; | 300 // ++input[0]; |
| 362 // } | 301 // } |
| 363 // return ""; | 302 // return ""; |
| 364 // } | 303 // } |
| 365 | 304 |
| 366 } // namespace device | 305 } // namespace device |
| OLD | NEW |