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" |
13 #include "device/bluetooth/bluetooth_remote_gatt_service_mac.h" | 14 #include "device/bluetooth/bluetooth_remote_gatt_service_mac.h" |
| 15 #include "device/bluetooth/test/mock_bluetooth_cbcharacteristic_mac.h" |
14 #include "device/bluetooth/test/mock_bluetooth_cbperipheral_mac.h" | 16 #include "device/bluetooth/test/mock_bluetooth_cbperipheral_mac.h" |
| 17 #include "device/bluetooth/test/mock_bluetooth_cbservice_mac.h" |
15 #include "device/bluetooth/test/mock_bluetooth_central_manager_mac.h" | 18 #include "device/bluetooth/test/mock_bluetooth_central_manager_mac.h" |
16 #include "device/bluetooth/test/test_bluetooth_adapter_observer.h" | 19 #include "device/bluetooth/test/test_bluetooth_adapter_observer.h" |
17 #include "third_party/ocmock/OCMock/OCMock.h" | 20 #include "third_party/ocmock/OCMock/OCMock.h" |
18 | 21 |
19 #import <CoreBluetooth/CoreBluetooth.h> | 22 #import <CoreBluetooth/CoreBluetooth.h> |
20 | 23 |
21 using base::mac::ObjCCast; | 24 using base::mac::ObjCCast; |
22 using base::scoped_nsobject; | 25 using base::scoped_nsobject; |
23 | 26 |
| 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 |
24 namespace device { | 39 namespace device { |
25 | 40 |
26 // This class hides Objective-C from bluetooth_test_mac.h. | 41 // This class hides Objective-C from bluetooth_test_mac.h. |
27 class BluetoothTestMac::ScopedMockCentralManager { | 42 class BluetoothTestMac::ScopedMockCentralManager { |
28 public: | 43 public: |
29 explicit ScopedMockCentralManager(MockCentralManager* mock_central_manager) { | 44 explicit ScopedMockCentralManager(MockCentralManager* mock_central_manager) { |
30 mock_central_manager_.reset(mock_central_manager); | 45 mock_central_manager_.reset(mock_central_manager); |
31 } | 46 } |
32 | 47 |
33 // Returns MockCentralManager instance. | 48 // Returns MockCentralManager instance. |
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
230 BluetoothLowEnergyDeviceMac* device_mac = | 245 BluetoothLowEnergyDeviceMac* device_mac = |
231 static_cast<BluetoothLowEnergyDeviceMac*>(device); | 246 static_cast<BluetoothLowEnergyDeviceMac*>(device); |
232 CBPeripheral* peripheral = device_mac->GetPeripheral(); | 247 CBPeripheral* peripheral = device_mac->GetPeripheral(); |
233 MockCBPeripheral* peripheral_mock = ObjCCast<MockCBPeripheral>(peripheral); | 248 MockCBPeripheral* peripheral_mock = ObjCCast<MockCBPeripheral>(peripheral); |
234 scoped_nsobject<NSMutableArray> services([[NSMutableArray alloc] init]); | 249 scoped_nsobject<NSMutableArray> services([[NSMutableArray alloc] init]); |
235 for (auto uuid : uuids) { | 250 for (auto uuid : uuids) { |
236 CBUUID* cb_service_uuid = [CBUUID UUIDWithString:@(uuid.c_str())]; | 251 CBUUID* cb_service_uuid = [CBUUID UUIDWithString:@(uuid.c_str())]; |
237 [services addObject:cb_service_uuid]; | 252 [services addObject:cb_service_uuid]; |
238 } | 253 } |
239 [peripheral_mock addServices:services]; | 254 [peripheral_mock addServices:services]; |
240 [peripheral_mock didDiscoverServicesWithError:nil]; | 255 DidDiscoverServices(peripheral_mock); |
241 } | 256 } |
242 | 257 |
243 void BluetoothTestMac::SimulateGattServiceRemoved( | 258 void BluetoothTestMac::SimulateGattServiceRemoved( |
244 BluetoothRemoteGattService* service) { | 259 BluetoothRemoteGattService* service) { |
245 BluetoothUUID bluetooth_service_uuid = service->GetUUID(); | 260 BluetoothUUID bluetooth_service_uuid = service->GetUUID(); |
246 std::string service_uuid_string = bluetooth_service_uuid.canonical_value(); | 261 std::string service_uuid_string = bluetooth_service_uuid.canonical_value(); |
247 BluetoothRemoteGattServiceMac* mac_gatt_service = | 262 BluetoothRemoteGattServiceMac* mac_gatt_service = |
248 static_cast<BluetoothRemoteGattServiceMac*>(service); | 263 static_cast<BluetoothRemoteGattServiceMac*>(service); |
249 BluetoothDevice* device = service->GetDevice(); | 264 BluetoothDevice* device = service->GetDevice(); |
250 BluetoothLowEnergyDeviceMac* device_mac = | 265 BluetoothLowEnergyDeviceMac* device_mac = |
251 static_cast<BluetoothLowEnergyDeviceMac*>(device); | 266 static_cast<BluetoothLowEnergyDeviceMac*>(device); |
252 CBPeripheral* peripheral = device_mac->GetPeripheral(); | 267 CBPeripheral* peripheral = device_mac->GetPeripheral(); |
253 MockCBPeripheral* peripheral_mock = ObjCCast<MockCBPeripheral>(peripheral); | 268 MockCBPeripheral* peripheral_mock = ObjCCast<MockCBPeripheral>(peripheral); |
254 [peripheral_mock removeService:mac_gatt_service->GetService()]; | 269 [peripheral_mock removeService:mac_gatt_service->GetService()]; |
255 [peripheral_mock didDiscoverServicesWithError:nil]; | 270 // After -[MockCBPeripheral removeService:], BluetoothLowEnergyDeviceMac is |
| 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); |
256 } | 308 } |
257 | 309 |
258 void BluetoothTestMac::OnFakeBluetoothDeviceConnectGattCalled() { | 310 void BluetoothTestMac::OnFakeBluetoothDeviceConnectGattCalled() { |
259 gatt_connection_attempts_++; | 311 gatt_connection_attempts_++; |
260 } | 312 } |
261 | 313 |
262 void BluetoothTestMac::OnFakeBluetoothGattDisconnect() { | 314 void BluetoothTestMac::OnFakeBluetoothGattDisconnect() { |
263 gatt_disconnection_attempts_++; | 315 gatt_disconnection_attempts_++; |
264 } | 316 } |
265 | 317 |
266 void BluetoothTestMac::OnFakeBluetoothServiceDiscovery() { | 318 void BluetoothTestMac::OnFakeBluetoothServiceDiscovery() { |
267 gatt_discovery_attempts_++; | 319 gatt_discovery_attempts_++; |
268 } | 320 } |
269 | 321 |
| 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 |
270 // Utility function for generating new (CBUUID, address) pairs where CBUUID | 331 // Utility function for generating new (CBUUID, address) pairs where CBUUID |
271 // hashes to address. For use when adding a new device address to the testing | 332 // hashes to address. For use when adding a new device address to the testing |
272 // suite because CoreBluetooth peripherals have CBUUIDs in place of addresses, | 333 // suite because CoreBluetooth peripherals have CBUUIDs in place of addresses, |
273 // and we construct fake addresses for them by hashing the CBUUID. By changing | 334 // and we construct fake addresses for them by hashing the CBUUID. By changing |
274 // |target| the user can generate sequentially numbered test addresses. | 335 // |target| the user can generate sequentially numbered test addresses. |
275 // | 336 // |
276 // std::string BluetoothTestMac::FindCBUUIDForHashTarget() { | 337 // std::string BluetoothTestMac::FindCBUUIDForHashTarget() { |
277 // // The desired first 6 digits of the hash. For example 0100000, 020000, | 338 // // The desired first 6 digits of the hash. For example 0100000, 020000, |
278 // // 030000, ... | 339 // // 030000, ... |
279 // const std::string target = "010000"; | 340 // const std::string target = "010000"; |
(...skipping 16 matching lines...) Expand all Loading... |
296 // crypto::SHA256HashString(input_str, raw, sizeof(raw)); | 357 // crypto::SHA256HashString(input_str, raw, sizeof(raw)); |
297 // if (base::HexEncode(raw, sizeof(raw)) == target) { | 358 // if (base::HexEncode(raw, sizeof(raw)) == target) { |
298 // return input_str; | 359 // return input_str; |
299 // } | 360 // } |
300 // ++input[0]; | 361 // ++input[0]; |
301 // } | 362 // } |
302 // return ""; | 363 // return ""; |
303 // } | 364 // } |
304 | 365 |
305 } // namespace device | 366 } // namespace device |
OLD | NEW |