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_device_mac.h" |
13 #include "device/bluetooth/bluetooth_remote_gatt_characteristic_mac.h" | 14 #include "device/bluetooth/bluetooth_remote_gatt_characteristic_mac.h" |
14 #include "device/bluetooth/bluetooth_remote_gatt_service_mac.h" | 15 #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_cbcharacteristic_mac.h" |
16 #include "device/bluetooth/test/mock_bluetooth_cbperipheral_mac.h" | 17 #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_cbservice_mac.h" |
18 #include "device/bluetooth/test/mock_bluetooth_central_manager_mac.h" | 19 #include "device/bluetooth/test/mock_bluetooth_central_manager_mac.h" |
19 #include "device/bluetooth/test/test_bluetooth_adapter_observer.h" | 20 #include "device/bluetooth/test/test_bluetooth_adapter_observer.h" |
20 #include "third_party/ocmock/OCMock/OCMock.h" | 21 #include "third_party/ocmock/OCMock/OCMock.h" |
21 | 22 |
22 #import <CoreBluetooth/CoreBluetooth.h> | 23 #import <CoreBluetooth/CoreBluetooth.h> |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 } | 74 } |
74 | 75 |
75 } // namespace | 76 } // namespace |
76 | 77 |
77 // UUID1 hashes to kTestDeviceAddress1, and UUID2 to kTestDeviceAddress2. | 78 // UUID1 hashes to kTestDeviceAddress1, and UUID2 to kTestDeviceAddress2. |
78 const std::string BluetoothTestMac::kTestPeripheralUUID1 = | 79 const std::string BluetoothTestMac::kTestPeripheralUUID1 = |
79 "34045B00-0000-0000-0000-000000000000"; | 80 "34045B00-0000-0000-0000-000000000000"; |
80 const std::string BluetoothTestMac::kTestPeripheralUUID2 = | 81 const std::string BluetoothTestMac::kTestPeripheralUUID2 = |
81 "EC1B8F00-0000-0000-0000-000000000000"; | 82 "EC1B8F00-0000-0000-0000-000000000000"; |
82 | 83 |
83 BluetoothTestMac::BluetoothTestMac() {} | 84 BluetoothTestMac::BluetoothTestMac() : BluetoothTestBase() {} |
84 | 85 |
85 BluetoothTestMac::~BluetoothTestMac() {} | 86 BluetoothTestMac::~BluetoothTestMac() {} |
86 | 87 |
87 void BluetoothTestMac::SetUp() {} | 88 void BluetoothTestMac::SetUp() {} |
88 | 89 |
89 bool BluetoothTestMac::PlatformSupportsLowEnergy() { | 90 bool BluetoothTestMac::PlatformSupportsLowEnergy() { |
90 return BluetoothAdapterMac::IsLowEnergyAvailable(); | 91 return BluetoothAdapterMac::IsLowEnergyAvailable(); |
91 } | 92 } |
92 | 93 |
93 void BluetoothTestMac::InitWithDefaultAdapter() { | 94 void BluetoothTestMac::InitWithDefaultAdapter() { |
(...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
282 MockCBService* service_mock = ObjCCast<MockCBService>(cb_service); | 283 MockCBService* service_mock = ObjCCast<MockCBService>(cb_service); |
283 CBUUID* cb_uuid = [CBUUID UUIDWithString:@(uuid.c_str())]; | 284 CBUUID* cb_uuid = [CBUUID UUIDWithString:@(uuid.c_str())]; |
284 [service_mock addCharacteristicWithUUID:cb_uuid properties:properties]; | 285 [service_mock addCharacteristicWithUUID:cb_uuid properties:properties]; |
285 MockCBPeripheral* peripheral_mock = GetMockCBPeripheral(service); | 286 MockCBPeripheral* peripheral_mock = GetMockCBPeripheral(service); |
286 [peripheral_mock didModifyServices:@[]]; | 287 [peripheral_mock didModifyServices:@[]]; |
287 // After -[MockCBPeripheral didModifyServices:], BluetoothLowEnergyDeviceMac | 288 // After -[MockCBPeripheral didModifyServices:], BluetoothLowEnergyDeviceMac |
288 // is expected to call -[CBPeripheral discoverServices:] | 289 // is expected to call -[CBPeripheral discoverServices:] |
289 DidDiscoverServices(peripheral_mock); | 290 DidDiscoverServices(peripheral_mock); |
290 } | 291 } |
291 | 292 |
| 293 void BluetoothTestMac::SimulateGattCharacteristicRead( |
| 294 BluetoothRemoteGattCharacteristic* characteristic, |
| 295 const std::vector<uint8_t>& value) { |
| 296 MockCBCharacteristic* characteristic_mock = |
| 297 GetCBMockCharacteristic(characteristic); |
| 298 scoped_nsobject<NSData> data( |
| 299 [[NSData alloc] initWithBytes:value.data() length:value.size()]); |
| 300 [characteristic_mock simulateReadWithValue:data error:nil]; |
| 301 } |
| 302 |
| 303 void BluetoothTestMac::SimulateGattCharacteristicReadError( |
| 304 BluetoothRemoteGattCharacteristic* characteristic, |
| 305 BluetoothRemoteGattService::GattErrorCode error_code) { |
| 306 MockCBCharacteristic* characteristic_mock = |
| 307 GetCBMockCharacteristic(characteristic); |
| 308 NSError* error = BluetoothDeviceMac::GetNSErrorFromGattErrorCode(error_code); |
| 309 [characteristic_mock simulateReadWithValue:nil error:error]; |
| 310 } |
| 311 |
292 void BluetoothTestMac::SimulateGattCharacteristicRemoved( | 312 void BluetoothTestMac::SimulateGattCharacteristicRemoved( |
293 BluetoothRemoteGattService* service, | 313 BluetoothRemoteGattService* service, |
294 BluetoothRemoteGattCharacteristic* characteristic) { | 314 BluetoothRemoteGattCharacteristic* characteristic) { |
295 MockCBPeripheral* peripheral_mock = GetMockCBPeripheral(service); | 315 MockCBPeripheral* peripheral_mock = GetMockCBPeripheral(service); |
296 BluetoothRemoteGattServiceMac* mac_gatt_service = | 316 BluetoothRemoteGattServiceMac* mac_gatt_service = |
297 static_cast<BluetoothRemoteGattServiceMac*>(service); | 317 static_cast<BluetoothRemoteGattServiceMac*>(service); |
298 CBService* cb_service = mac_gatt_service->GetService(); | 318 CBService* cb_service = mac_gatt_service->GetService(); |
299 MockCBService* service_mock = ObjCCast<MockCBService>(cb_service); | 319 MockCBService* service_mock = ObjCCast<MockCBService>(cb_service); |
300 BluetoothRemoteGattCharacteristicMac* characteristic_mac = | 320 BluetoothRemoteGattCharacteristicMac* characteristic_mac = |
301 static_cast<BluetoothRemoteGattCharacteristicMac*>(characteristic); | 321 static_cast<BluetoothRemoteGattCharacteristicMac*>(characteristic); |
(...skipping 10 matching lines...) Expand all Loading... |
312 } | 332 } |
313 | 333 |
314 void BluetoothTestMac::OnFakeBluetoothGattDisconnect() { | 334 void BluetoothTestMac::OnFakeBluetoothGattDisconnect() { |
315 gatt_disconnection_attempts_++; | 335 gatt_disconnection_attempts_++; |
316 } | 336 } |
317 | 337 |
318 void BluetoothTestMac::OnFakeBluetoothServiceDiscovery() { | 338 void BluetoothTestMac::OnFakeBluetoothServiceDiscovery() { |
319 gatt_discovery_attempts_++; | 339 gatt_discovery_attempts_++; |
320 } | 340 } |
321 | 341 |
| 342 void BluetoothTest::OnFakeBluetoothCharacteristicReadValue() { |
| 343 gatt_read_characteristic_attempts_++; |
| 344 } |
| 345 |
322 MockCBPeripheral* BluetoothTestMac::GetMockCBPeripheral( | 346 MockCBPeripheral* BluetoothTestMac::GetMockCBPeripheral( |
323 BluetoothRemoteGattService* service) const { | 347 BluetoothRemoteGattService* service) const { |
324 BluetoothDevice* device = service->GetDevice(); | 348 BluetoothDevice* device = service->GetDevice(); |
325 BluetoothLowEnergyDeviceMac* device_mac = | 349 BluetoothLowEnergyDeviceMac* device_mac = |
326 static_cast<BluetoothLowEnergyDeviceMac*>(device); | 350 static_cast<BluetoothLowEnergyDeviceMac*>(device); |
327 CBPeripheral* cb_peripheral = device_mac->GetPeripheral(); | 351 CBPeripheral* cb_peripheral = device_mac->GetPeripheral(); |
328 return ObjCCast<MockCBPeripheral>(cb_peripheral); | 352 return ObjCCast<MockCBPeripheral>(cb_peripheral); |
329 } | 353 } |
330 | 354 |
| 355 MockCBCharacteristic* BluetoothTest::GetCBMockCharacteristic( |
| 356 device::BluetoothRemoteGattCharacteristic* characteristic) const { |
| 357 device::BluetoothRemoteGattCharacteristicMac* mac_gatt_characteristic = |
| 358 static_cast<device::BluetoothRemoteGattCharacteristicMac*>( |
| 359 characteristic); |
| 360 CBCharacteristic* cb_characteristic = |
| 361 mac_gatt_characteristic->GetCBCharacteristic(); |
| 362 return ObjCCast<MockCBCharacteristic>(cb_characteristic); |
| 363 } |
| 364 |
331 // Utility function for generating new (CBUUID, address) pairs where CBUUID | 365 // 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 | 366 // 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, | 367 // suite because CoreBluetooth peripherals have CBUUIDs in place of addresses, |
334 // and we construct fake addresses for them by hashing the CBUUID. By changing | 368 // and we construct fake addresses for them by hashing the CBUUID. By changing |
335 // |target| the user can generate sequentially numbered test addresses. | 369 // |target| the user can generate sequentially numbered test addresses. |
336 // | 370 // |
337 // std::string BluetoothTestMac::FindCBUUIDForHashTarget() { | 371 // std::string BluetoothTestMac::FindCBUUIDForHashTarget() { |
338 // // The desired first 6 digits of the hash. For example 0100000, 020000, | 372 // // The desired first 6 digits of the hash. For example 0100000, 020000, |
339 // // 030000, ... | 373 // // 030000, ... |
340 // const std::string target = "010000"; | 374 // const std::string target = "010000"; |
(...skipping 16 matching lines...) Expand all Loading... |
357 // crypto::SHA256HashString(input_str, raw, sizeof(raw)); | 391 // crypto::SHA256HashString(input_str, raw, sizeof(raw)); |
358 // if (base::HexEncode(raw, sizeof(raw)) == target) { | 392 // if (base::HexEncode(raw, sizeof(raw)) == target) { |
359 // return input_str; | 393 // return input_str; |
360 // } | 394 // } |
361 // ++input[0]; | 395 // ++input[0]; |
362 // } | 396 // } |
363 // return ""; | 397 // return ""; |
364 // } | 398 // } |
365 | 399 |
366 } // namespace device | 400 } // namespace device |
OLD | NEW |