Chromium Code Reviews| Index: device/bluetooth/test/bluetooth_test_mac.mm |
| diff --git a/device/bluetooth/test/bluetooth_test_mac.mm b/device/bluetooth/test/bluetooth_test_mac.mm |
| index d64b5f79787622c89b85492f6684c06843f3c52b..654afaf723165d3b8c9acbcf56cce62ab41dd0da 100644 |
| --- a/device/bluetooth/test/bluetooth_test_mac.mm |
| +++ b/device/bluetooth/test/bluetooth_test_mac.mm |
| @@ -6,21 +6,38 @@ |
| #include <stdint.h> |
| +#include "base/mac/foundation_util.h" |
| #include "base/strings/string_number_conversions.h" |
| #include "build/build_config.h" |
| #include "device/bluetooth/bluetooth_adapter_mac.h" |
| +#include "device/bluetooth/test/mock_bluetooth_cbperipheral_mac.h" |
| #include "device/bluetooth/test/mock_bluetooth_central_manager_mac.h" |
| #include "device/bluetooth/test/test_bluetooth_adapter_observer.h" |
| #include "third_party/ocmock/OCMock/OCMock.h" |
| -#if defined(OS_IOS) |
| #import <CoreBluetooth/CoreBluetooth.h> |
| -#else // !defined(OS_IOS) |
| -#import <IOBluetooth/IOBluetooth.h> |
| -#endif // defined(OS_IOS) |
| + |
| +using base::mac::ObjCCast; |
| +using base::scoped_nsobject; |
| namespace device { |
| +// This class hides Objective-C from bluetooth_test_mac.h. |
| +class BluetoothTestMac::ScopedMockCentralManager { |
| + public: |
| + explicit ScopedMockCentralManager(MockCentralManager* mock_central_manager) { |
| + mock_central_manager_.reset(mock_central_manager); |
| + } |
| + |
| + // Returns MockCentralManager instance. |
| + MockCentralManager* central_manager() { return mock_central_manager_.get(); }; |
|
scheib
2016/03/02 16:28:42
'get' would also be an OK name and shorter.
|
| + |
| + private: |
| + scoped_nsobject<MockCentralManager> mock_central_manager_; |
| + |
| + DISALLOW_COPY_AND_ASSIGN(ScopedMockCentralManager); |
| +}; |
| + |
| namespace { |
| CBPeripheral* CreateMockPeripheral(NSString* peripheral_identifier) { |
| @@ -83,9 +100,13 @@ void BluetoothTestMac::InitWithoutDefaultAdapter() { |
| adapter_ = adapter_mac_; |
| if (BluetoothAdapterMac::IsLowEnergyAvailable()) { |
| - id low_energy_central_manager = [[MockCentralManager alloc] init]; |
| - [low_energy_central_manager setState:CBCentralManagerStateUnsupported]; |
| - adapter_mac_->SetCentralManagerForTesting(low_energy_central_manager); |
| + mock_central_manager_.reset( |
| + new ScopedMockCentralManager([[MockCentralManager alloc] init])); |
| + [mock_central_manager_->central_manager() setBluetoothTestMac:this]; |
| + [mock_central_manager_->central_manager() |
| + setState:CBCentralManagerStateUnsupported]; |
| + adapter_mac_->SetCentralManagerForTesting( |
| + (id)mock_central_manager_->central_manager()); |
| } |
| } |
| @@ -97,9 +118,13 @@ void BluetoothTestMac::InitWithFakeAdapter() { |
| adapter_ = adapter_mac_; |
| if (BluetoothAdapterMac::IsLowEnergyAvailable()) { |
| - id low_energy_central_manager = [[MockCentralManager alloc] init]; |
| - [low_energy_central_manager setState:CBCentralManagerStatePoweredOn]; |
| - adapter_mac_->SetCentralManagerForTesting(low_energy_central_manager); |
| + mock_central_manager_.reset( |
| + new ScopedMockCentralManager([[MockCentralManager alloc] init])); |
| + mock_central_manager_->central_manager().bluetoothTestMac = this; |
| + [mock_central_manager_->central_manager() |
| + setState:CBCentralManagerStatePoweredOn]; |
| + adapter_mac_->SetCentralManagerForTesting( |
| + (id)mock_central_manager_->central_manager()); |
| } |
| } |
| @@ -151,12 +176,17 @@ BluetoothDevice* BluetoothTestMac::DiscoverLowEnergyDevice(int device_ordinal) { |
| break; |
| } |
| case 3: { |
| - CBPeripheral* peripheral = CreateMockPeripheral( |
| - [NSString stringWithUTF8String:kTestPeripheralUUID1.c_str()]); |
| - NSString* name = |
| - [NSString stringWithUTF8String:kTestDeviceNameEmpty.c_str()]; |
| - NSArray* uuids = nil; |
| - NSDictionary* advertisement_data = CreateAdvertisementData(name, uuids); |
| + scoped_nsobject<NSString> uuid_string( |
| + [[NSString alloc] initWithUTF8String:kTestPeripheralUUID1.c_str()]); |
| + scoped_nsobject<NSUUID> identifier( |
| + [[NSUUID alloc] initWithUUIDString:uuid_string]); |
| + scoped_nsobject<CBPeripheral> peripheral; |
| + peripheral.reset(static_cast<CBPeripheral*>( |
| + [[MockCBPeripheral alloc] initWithIdentifier:identifier])); |
| + scoped_nsobject<NSString> name( |
| + [[NSString alloc] initWithUTF8String:kTestDeviceNameEmpty.c_str()]); |
| + scoped_nsobject<NSDictionary> advertisement_data( |
| + [CreateAdvertisementData(name, nil) retain]); |
| [central_manager_delegate centralManager:central_manager |
| didDiscoverPeripheral:peripheral |
| advertisementData:advertisement_data |
| @@ -180,6 +210,59 @@ BluetoothDevice* BluetoothTestMac::DiscoverLowEnergyDevice(int device_ordinal) { |
| return observer.last_device(); |
| } |
| +void BluetoothTestMac::SimulateGattConnection(BluetoothDevice* device) { |
| + BluetoothLowEnergyDeviceMac* lowEnergyDeviceMac = |
| + static_cast<BluetoothLowEnergyDeviceMac*>(device); |
| + CBPeripheral* peripheral = lowEnergyDeviceMac->GetPeripheral(); |
| + MockCBPeripheral* mockPeripheral = (MockCBPeripheral*)peripheral; |
| + [mockPeripheral setState:CBPeripheralStateConnected]; |
| + CBCentralManager* centralManager = |
| + ObjCCast<CBCentralManager>(mock_central_manager_->central_manager()); |
| + [centralManager.delegate centralManager:centralManager |
| + didConnectPeripheral:peripheral]; |
| +} |
| + |
| +void BluetoothTestMac::SimulateGattDisconnection(BluetoothDevice* device) { |
| + BluetoothLowEnergyDeviceMac* lowEnergyDeviceMac = |
| + static_cast<BluetoothLowEnergyDeviceMac*>(device); |
| + CBPeripheral* peripheral = lowEnergyDeviceMac->GetPeripheral(); |
| + MockCBPeripheral* mockPeripheral = (MockCBPeripheral*)peripheral; |
| + [mockPeripheral setState:CBPeripheralStateDisconnected]; |
| + CBCentralManager* centralManager = |
| + ObjCCast<CBCentralManager>(mock_central_manager_->central_manager()); |
| + [centralManager.delegate centralManager:centralManager |
| + didDisconnectPeripheral:peripheral |
| + error:nil]; |
| +} |
| + |
| +void BluetoothTestMac::SimulateGattConnectionError( |
| + BluetoothDevice* device, |
| + BluetoothDevice::ConnectErrorCode errorCode) { |
| + BluetoothLowEnergyDeviceMac* lowEnergyDeviceMac = |
| + static_cast<BluetoothLowEnergyDeviceMac*>(device); |
| + CBPeripheral* peripheral = lowEnergyDeviceMac->GetPeripheral(); |
| + MockCBPeripheral* mockPeripheral = (MockCBPeripheral*)peripheral; |
| + [mockPeripheral setState:CBPeripheralStateDisconnected]; |
| + CBCentralManager* centralManager = |
| + ObjCCast<CBCentralManager>(mock_central_manager_->central_manager()); |
| + // TODO(http://crbug.com/585894): Need to convert the connect error code into |
| + // NSError |
| + NSError* error = [NSError errorWithDomain:@"BluetoothDevice::ConnectErrorCode" |
| + code:-1 |
| + userInfo:nil]; |
| + [centralManager.delegate centralManager:centralManager |
| + didFailToConnectPeripheral:peripheral |
| + error:error]; |
| +} |
| + |
| +void BluetoothTestMac::OnFakeBluetoothDeviceConnectGattCalled() { |
| + gatt_connection_attempts_++; |
| +} |
| + |
| +void BluetoothTestMac::OnFakeBluetoothGattDisconnect() { |
| + gatt_disconnection_attempts_++; |
| +} |
| + |
| // Utility function for generating new (CBUUID, address) pairs where CBUUID |
| // hashes to address. For use when adding a new device address to the testing |
| // suite because CoreBluetooth peripherals have CBUUIDs in place of addresses, |