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 194 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
228 BluetoothLowEnergyDeviceMac* device_mac = | 241 BluetoothLowEnergyDeviceMac* device_mac = |
229 static_cast<BluetoothLowEnergyDeviceMac*>(device); | 242 static_cast<BluetoothLowEnergyDeviceMac*>(device); |
230 CBPeripheral* peripheral = device_mac->GetPeripheral(); | 243 CBPeripheral* peripheral = device_mac->GetPeripheral(); |
231 MockCBPeripheral* peripheral_mock = ObjCCast<MockCBPeripheral>(peripheral); | 244 MockCBPeripheral* peripheral_mock = ObjCCast<MockCBPeripheral>(peripheral); |
232 scoped_nsobject<NSMutableArray> services = [[NSMutableArray alloc] init]; | 245 scoped_nsobject<NSMutableArray> services = [[NSMutableArray alloc] init]; |
233 for (auto uuid : uuids) { | 246 for (auto uuid : uuids) { |
234 CBUUID* cb_service_uuid = [CBUUID UUIDWithString:@(uuid.c_str())]; | 247 CBUUID* cb_service_uuid = [CBUUID UUIDWithString:@(uuid.c_str())]; |
235 [services addObject:cb_service_uuid]; | 248 [services addObject:cb_service_uuid]; |
236 } | 249 } |
237 [peripheral_mock addServices:services]; | 250 [peripheral_mock addServices:services]; |
238 [peripheral_mock didDiscoverServicesWithError:nil]; | 251 DidDiscoverServices(peripheral_mock); |
239 } | 252 } |
240 | 253 |
241 void BluetoothTestMac::SimulateGattServiceRemoved( | 254 void BluetoothTestMac::SimulateGattServiceRemoved( |
242 BluetoothRemoteGattService* service) { | 255 BluetoothRemoteGattService* service) { |
243 BluetoothUUID bluetooth_service_uuid = service->GetUUID(); | 256 BluetoothUUID bluetooth_service_uuid = service->GetUUID(); |
244 std::string service_uuid_string = bluetooth_service_uuid.canonical_value(); | 257 std::string service_uuid_string = bluetooth_service_uuid.canonical_value(); |
245 BluetoothRemoteGattServiceMac* mac_gatt_service = | 258 BluetoothRemoteGattServiceMac* mac_gatt_service = |
246 static_cast<BluetoothRemoteGattServiceMac*>(service); | 259 static_cast<BluetoothRemoteGattServiceMac*>(service); |
247 BluetoothDevice* device = service->GetDevice(); | 260 BluetoothDevice* device = service->GetDevice(); |
248 BluetoothLowEnergyDeviceMac* device_mac = | 261 BluetoothLowEnergyDeviceMac* device_mac = |
249 static_cast<BluetoothLowEnergyDeviceMac*>(device); | 262 static_cast<BluetoothLowEnergyDeviceMac*>(device); |
250 CBPeripheral* peripheral = device_mac->GetPeripheral(); | 263 CBPeripheral* peripheral = device_mac->GetPeripheral(); |
251 MockCBPeripheral* peripheral_mock = ObjCCast<MockCBPeripheral>(peripheral); | 264 MockCBPeripheral* peripheral_mock = ObjCCast<MockCBPeripheral>(peripheral); |
252 [peripheral_mock removeService:mac_gatt_service->GetService()]; | 265 [peripheral_mock removeService:mac_gatt_service->GetService()]; |
253 [peripheral_mock didDiscoverServicesWithError:nil]; | 266 // After -[MockCBPeripheral removeService:], BluetoothLowEnergyDeviceMac is |
| 267 // expected to call -[CBPeripheral discoverServices:] |
| 268 DidDiscoverServices(peripheral_mock); |
| 269 } |
| 270 |
| 271 void BluetoothTestMac::SimulateGattCharacteristic( |
| 272 BluetoothRemoteGattService* service, |
| 273 const std::string& uuid, |
| 274 int properties) { |
| 275 BluetoothRemoteGattServiceMac* mac_gatt_service = |
| 276 static_cast<BluetoothRemoteGattServiceMac*>(service); |
| 277 CBService* cb_service = mac_gatt_service->GetService(); |
| 278 MockCBService* service_mock = ObjCCast<MockCBService>(cb_service); |
| 279 CBUUID* cb_uuid = [CBUUID UUIDWithString:@(uuid.c_str())]; |
| 280 [service_mock addCharacteristicWithUUID:cb_uuid properties:properties]; |
| 281 BluetoothDevice* device = service->GetDevice(); |
| 282 BluetoothLowEnergyDeviceMac* device_mac = |
| 283 static_cast<BluetoothLowEnergyDeviceMac*>(device); |
| 284 CBPeripheral* cb_peripheral = device_mac->GetPeripheral(); |
| 285 MockCBPeripheral* peripheral_mock = ObjCCast<MockCBPeripheral>(cb_peripheral); |
| 286 [peripheral_mock didModifyServices:@[]]; |
| 287 // After -[MockCBPeripheral didModifyServices:], BluetoothLowEnergyDeviceMac |
| 288 // is expected to call -[CBPeripheral discoverServices:] |
| 289 DidDiscoverServices(peripheral_mock); |
254 } | 290 } |
255 | 291 |
256 void BluetoothTestMac::OnFakeBluetoothDeviceConnectGattCalled() { | 292 void BluetoothTestMac::OnFakeBluetoothDeviceConnectGattCalled() { |
257 gatt_connection_attempts_++; | 293 gatt_connection_attempts_++; |
258 } | 294 } |
259 | 295 |
260 void BluetoothTestMac::OnFakeBluetoothGattDisconnect() { | 296 void BluetoothTestMac::OnFakeBluetoothGattDisconnect() { |
261 gatt_disconnection_attempts_++; | 297 gatt_disconnection_attempts_++; |
262 } | 298 } |
263 | 299 |
(...skipping 30 matching lines...) Expand all Loading... |
294 // crypto::SHA256HashString(input_str, raw, sizeof(raw)); | 330 // crypto::SHA256HashString(input_str, raw, sizeof(raw)); |
295 // if (base::HexEncode(raw, sizeof(raw)) == target) { | 331 // if (base::HexEncode(raw, sizeof(raw)) == target) { |
296 // return input_str; | 332 // return input_str; |
297 // } | 333 // } |
298 // ++input[0]; | 334 // ++input[0]; |
299 // } | 335 // } |
300 // return ""; | 336 // return ""; |
301 // } | 337 // } |
302 | 338 |
303 } // namespace device | 339 } // namespace device |
OLD | NEW |