| 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_service_mac.h" | 13 #include "device/bluetooth/bluetooth_remote_gatt_service_mac.h" |
| 14 #include "device/bluetooth/test/mock_bluetooth_cbperipheral_mac.h" | 14 #include "device/bluetooth/test/mock_bluetooth_cbperipheral_mac.h" |
| 15 #include "device/bluetooth/test/mock_bluetooth_cbservice_mac.h" |
| 15 #include "device/bluetooth/test/mock_bluetooth_central_manager_mac.h" | 16 #include "device/bluetooth/test/mock_bluetooth_central_manager_mac.h" |
| 16 #include "device/bluetooth/test/test_bluetooth_adapter_observer.h" | 17 #include "device/bluetooth/test/test_bluetooth_adapter_observer.h" |
| 17 #include "third_party/ocmock/OCMock/OCMock.h" | 18 #include "third_party/ocmock/OCMock/OCMock.h" |
| 18 | 19 |
| 19 #import <CoreBluetooth/CoreBluetooth.h> | 20 #import <CoreBluetooth/CoreBluetooth.h> |
| 20 | 21 |
| 21 using base::mac::ObjCCast; | 22 using base::mac::ObjCCast; |
| 22 using base::scoped_nsobject; | 23 using base::scoped_nsobject; |
| 23 | 24 |
| 25 namespace { |
| 26 |
| 27 static void DidDiscoverServices(MockCBPeripheral* peripheral_mock) { |
| 28 [peripheral_mock didDiscoverServicesWithError:nil]; |
| 29 // BluetoothLowEnergyDeviceMac is expected to call |
| 30 // -[CBPeripheral discoverCharacteristics:forService:] for each services, |
| 31 // so -[<CBPeripheralDelegate peripheral:didDiscoverCharacteristicsForService: |
| 32 // error:] needs to be called |
| 33 [peripheral_mock didDiscoverCharactericsForAllServices]; |
| 34 } |
| 35 } // namespace |
| 36 |
| 24 namespace device { | 37 namespace device { |
| 25 | 38 |
| 26 // This class hides Objective-C from bluetooth_test_mac.h. | 39 // This class hides Objective-C from bluetooth_test_mac.h. |
| 27 class BluetoothTestMac::ScopedMockCentralManager { | 40 class BluetoothTestMac::ScopedMockCentralManager { |
| 28 public: | 41 public: |
| 29 explicit ScopedMockCentralManager(MockCentralManager* mock_central_manager) { | 42 explicit ScopedMockCentralManager(MockCentralManager* mock_central_manager) { |
| 30 mock_central_manager_.reset(mock_central_manager); | 43 mock_central_manager_.reset(mock_central_manager); |
| 31 } | 44 } |
| 32 | 45 |
| 33 // Returns MockCentralManager instance. | 46 // Returns MockCentralManager instance. |
| (...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 230 BluetoothLowEnergyDeviceMac* device_mac = | 243 BluetoothLowEnergyDeviceMac* device_mac = |
| 231 static_cast<BluetoothLowEnergyDeviceMac*>(device); | 244 static_cast<BluetoothLowEnergyDeviceMac*>(device); |
| 232 CBPeripheral* peripheral = device_mac->GetPeripheral(); | 245 CBPeripheral* peripheral = device_mac->GetPeripheral(); |
| 233 MockCBPeripheral* peripheral_mock = ObjCCast<MockCBPeripheral>(peripheral); | 246 MockCBPeripheral* peripheral_mock = ObjCCast<MockCBPeripheral>(peripheral); |
| 234 scoped_nsobject<NSMutableArray> services([[NSMutableArray alloc] init]); | 247 scoped_nsobject<NSMutableArray> services([[NSMutableArray alloc] init]); |
| 235 for (auto uuid : uuids) { | 248 for (auto uuid : uuids) { |
| 236 CBUUID* cb_service_uuid = [CBUUID UUIDWithString:@(uuid.c_str())]; | 249 CBUUID* cb_service_uuid = [CBUUID UUIDWithString:@(uuid.c_str())]; |
| 237 [services addObject:cb_service_uuid]; | 250 [services addObject:cb_service_uuid]; |
| 238 } | 251 } |
| 239 [peripheral_mock addServices:services]; | 252 [peripheral_mock addServices:services]; |
| 240 [peripheral_mock didDiscoverServicesWithError:nil]; | 253 DidDiscoverServices(peripheral_mock); |
| 241 } | 254 } |
| 242 | 255 |
| 243 void BluetoothTestMac::SimulateGattServiceRemoved( | 256 void BluetoothTestMac::SimulateGattServiceRemoved( |
| 244 BluetoothRemoteGattService* service) { | 257 BluetoothRemoteGattService* service) { |
| 245 BluetoothUUID bluetooth_service_uuid = service->GetUUID(); | 258 BluetoothUUID bluetooth_service_uuid = service->GetUUID(); |
| 246 std::string service_uuid_string = bluetooth_service_uuid.canonical_value(); | 259 std::string service_uuid_string = bluetooth_service_uuid.canonical_value(); |
| 247 BluetoothRemoteGattServiceMac* mac_gatt_service = | 260 BluetoothRemoteGattServiceMac* mac_gatt_service = |
| 248 static_cast<BluetoothRemoteGattServiceMac*>(service); | 261 static_cast<BluetoothRemoteGattServiceMac*>(service); |
| 249 BluetoothDevice* device = service->GetDevice(); | 262 BluetoothDevice* device = service->GetDevice(); |
| 250 BluetoothLowEnergyDeviceMac* device_mac = | 263 BluetoothLowEnergyDeviceMac* device_mac = |
| 251 static_cast<BluetoothLowEnergyDeviceMac*>(device); | 264 static_cast<BluetoothLowEnergyDeviceMac*>(device); |
| 252 CBPeripheral* peripheral = device_mac->GetPeripheral(); | 265 CBPeripheral* peripheral = device_mac->GetPeripheral(); |
| 253 MockCBPeripheral* peripheral_mock = ObjCCast<MockCBPeripheral>(peripheral); | 266 MockCBPeripheral* peripheral_mock = ObjCCast<MockCBPeripheral>(peripheral); |
| 254 [peripheral_mock removeService:mac_gatt_service->GetService()]; | 267 [peripheral_mock removeService:mac_gatt_service->GetService()]; |
| 255 [peripheral_mock didDiscoverServicesWithError:nil]; | 268 // After -[MockCBPeripheral removeService:], BluetoothLowEnergyDeviceMac is |
| 269 // expected to call -[CBPeripheral discoverServices:] |
| 270 DidDiscoverServices(peripheral_mock); |
| 271 } |
| 272 |
| 273 void BluetoothTestMac::SimulateGattCharacteristic( |
| 274 BluetoothRemoteGattService* service, |
| 275 const std::string& uuid, |
| 276 int properties) { |
| 277 BluetoothRemoteGattServiceMac* mac_gatt_service = |
| 278 static_cast<BluetoothRemoteGattServiceMac*>(service); |
| 279 CBService* cb_service = mac_gatt_service->GetService(); |
| 280 MockCBService* service_mock = ObjCCast<MockCBService>(cb_service); |
| 281 CBUUID* cb_uuid = [CBUUID UUIDWithString:@(uuid.c_str())]; |
| 282 [service_mock addCharacteristicWithUUID:cb_uuid properties:properties]; |
| 283 BluetoothDevice* device = service->GetDevice(); |
| 284 BluetoothLowEnergyDeviceMac* device_mac = |
| 285 static_cast<BluetoothLowEnergyDeviceMac*>(device); |
| 286 CBPeripheral* cb_peripheral = device_mac->GetPeripheral(); |
| 287 MockCBPeripheral* peripheral_mock = ObjCCast<MockCBPeripheral>(cb_peripheral); |
| 288 [peripheral_mock didModifyServices:@[]]; |
| 289 // After -[MockCBPeripheral didModifyServices:], BluetoothLowEnergyDeviceMac |
| 290 // is expected to call -[CBPeripheral discoverServices:] |
| 291 DidDiscoverServices(peripheral_mock); |
| 256 } | 292 } |
| 257 | 293 |
| 258 void BluetoothTestMac::OnFakeBluetoothDeviceConnectGattCalled() { | 294 void BluetoothTestMac::OnFakeBluetoothDeviceConnectGattCalled() { |
| 259 gatt_connection_attempts_++; | 295 gatt_connection_attempts_++; |
| 260 } | 296 } |
| 261 | 297 |
| 262 void BluetoothTestMac::OnFakeBluetoothGattDisconnect() { | 298 void BluetoothTestMac::OnFakeBluetoothGattDisconnect() { |
| 263 gatt_disconnection_attempts_++; | 299 gatt_disconnection_attempts_++; |
| 264 } | 300 } |
| 265 | 301 |
| (...skipping 30 matching lines...) Expand all Loading... |
| 296 // crypto::SHA256HashString(input_str, raw, sizeof(raw)); | 332 // crypto::SHA256HashString(input_str, raw, sizeof(raw)); |
| 297 // if (base::HexEncode(raw, sizeof(raw)) == target) { | 333 // if (base::HexEncode(raw, sizeof(raw)) == target) { |
| 298 // return input_str; | 334 // return input_str; |
| 299 // } | 335 // } |
| 300 // ++input[0]; | 336 // ++input[0]; |
| 301 // } | 337 // } |
| 302 // return ""; | 338 // return ""; |
| 303 // } | 339 // } |
| 304 | 340 |
| 305 } // namespace device | 341 } // namespace device |
| OLD | NEW |