Chromium Code Reviews| 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/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| 10 #include "build/build_config.h" | 11 #include "build/build_config.h" |
| 11 #include "device/bluetooth/bluetooth_adapter_mac.h" | 12 #include "device/bluetooth/bluetooth_adapter_mac.h" |
| 13 #include "device/bluetooth/test/mock_bluetooth_cbperipheral_mac.h" | |
| 12 #include "device/bluetooth/test/mock_bluetooth_central_manager_mac.h" | 14 #include "device/bluetooth/test/mock_bluetooth_central_manager_mac.h" |
| 13 #include "device/bluetooth/test/test_bluetooth_adapter_observer.h" | 15 #include "device/bluetooth/test/test_bluetooth_adapter_observer.h" |
| 14 #include "third_party/ocmock/OCMock/OCMock.h" | 16 #include "third_party/ocmock/OCMock/OCMock.h" |
| 15 | 17 |
| 16 #if defined(OS_IOS) | |
| 17 #import <CoreBluetooth/CoreBluetooth.h> | 18 #import <CoreBluetooth/CoreBluetooth.h> |
| 18 #else // !defined(OS_IOS) | 19 |
| 19 #import <IOBluetooth/IOBluetooth.h> | 20 using base::mac::ObjCCast; |
| 20 #endif // defined(OS_IOS) | 21 using base::scoped_nsobject; |
| 21 | 22 |
| 22 namespace device { | 23 namespace device { |
| 23 | 24 |
| 25 // This class hides Objective-C from bluetooth_test_mac.h. | |
| 26 class BluetoothTestMac::ScopedMockCentralManager { | |
| 27 public: | |
| 28 explicit ScopedMockCentralManager(MockCentralManager* mock_central_manager) { | |
| 29 mock_central_manager_.reset(mock_central_manager); | |
| 30 } | |
| 31 | |
| 32 // Returns MockCentralManager instance. | |
| 33 MockCentralManager* central_manager() { return mock_central_manager_.get(); }; | |
|
scheib
2016/03/02 16:28:42
'get' would also be an OK name and shorter.
| |
| 34 | |
| 35 private: | |
| 36 scoped_nsobject<MockCentralManager> mock_central_manager_; | |
| 37 | |
| 38 DISALLOW_COPY_AND_ASSIGN(ScopedMockCentralManager); | |
| 39 }; | |
| 40 | |
| 24 namespace { | 41 namespace { |
| 25 | 42 |
| 26 CBPeripheral* CreateMockPeripheral(NSString* peripheral_identifier) { | 43 CBPeripheral* CreateMockPeripheral(NSString* peripheral_identifier) { |
| 27 Class peripheral_class = NSClassFromString(@"CBPeripheral"); | 44 Class peripheral_class = NSClassFromString(@"CBPeripheral"); |
| 28 id mock_peripheral = [OCMockObject mockForClass:[peripheral_class class]]; | 45 id mock_peripheral = [OCMockObject mockForClass:[peripheral_class class]]; |
| 29 [[[mock_peripheral stub] andReturnValue:@(CBPeripheralStateDisconnected)] | 46 [[[mock_peripheral stub] andReturnValue:@(CBPeripheralStateDisconnected)] |
| 30 performSelector:@selector(state)]; | 47 performSelector:@selector(state)]; |
| 31 Class uuid_class = NSClassFromString(@"NSUUID"); | 48 Class uuid_class = NSClassFromString(@"NSUUID"); |
| 32 [[[mock_peripheral stub] | 49 [[[mock_peripheral stub] |
| 33 andReturn:[[uuid_class performSelector:@selector(UUID)] | 50 andReturn:[[uuid_class performSelector:@selector(UUID)] |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 76 adapter_ = adapter_mac_; | 93 adapter_ = adapter_mac_; |
| 77 } | 94 } |
| 78 | 95 |
| 79 void BluetoothTestMac::InitWithoutDefaultAdapter() { | 96 void BluetoothTestMac::InitWithoutDefaultAdapter() { |
| 80 adapter_mac_ = BluetoothAdapterMac::CreateAdapterForTest( | 97 adapter_mac_ = BluetoothAdapterMac::CreateAdapterForTest( |
| 81 "", "", message_loop_.task_runner()) | 98 "", "", message_loop_.task_runner()) |
| 82 .get(); | 99 .get(); |
| 83 adapter_ = adapter_mac_; | 100 adapter_ = adapter_mac_; |
| 84 | 101 |
| 85 if (BluetoothAdapterMac::IsLowEnergyAvailable()) { | 102 if (BluetoothAdapterMac::IsLowEnergyAvailable()) { |
| 86 id low_energy_central_manager = [[MockCentralManager alloc] init]; | 103 mock_central_manager_.reset( |
| 87 [low_energy_central_manager setState:CBCentralManagerStateUnsupported]; | 104 new ScopedMockCentralManager([[MockCentralManager alloc] init])); |
| 88 adapter_mac_->SetCentralManagerForTesting(low_energy_central_manager); | 105 [mock_central_manager_->central_manager() setBluetoothTestMac:this]; |
| 106 [mock_central_manager_->central_manager() | |
| 107 setState:CBCentralManagerStateUnsupported]; | |
| 108 adapter_mac_->SetCentralManagerForTesting( | |
| 109 (id)mock_central_manager_->central_manager()); | |
| 89 } | 110 } |
| 90 } | 111 } |
| 91 | 112 |
| 92 void BluetoothTestMac::InitWithFakeAdapter() { | 113 void BluetoothTestMac::InitWithFakeAdapter() { |
| 93 adapter_mac_ = | 114 adapter_mac_ = |
| 94 BluetoothAdapterMac::CreateAdapterForTest( | 115 BluetoothAdapterMac::CreateAdapterForTest( |
| 95 kTestAdapterName, kTestAdapterAddress, message_loop_.task_runner()) | 116 kTestAdapterName, kTestAdapterAddress, message_loop_.task_runner()) |
| 96 .get(); | 117 .get(); |
| 97 adapter_ = adapter_mac_; | 118 adapter_ = adapter_mac_; |
| 98 | 119 |
| 99 if (BluetoothAdapterMac::IsLowEnergyAvailable()) { | 120 if (BluetoothAdapterMac::IsLowEnergyAvailable()) { |
| 100 id low_energy_central_manager = [[MockCentralManager alloc] init]; | 121 mock_central_manager_.reset( |
| 101 [low_energy_central_manager setState:CBCentralManagerStatePoweredOn]; | 122 new ScopedMockCentralManager([[MockCentralManager alloc] init])); |
| 102 adapter_mac_->SetCentralManagerForTesting(low_energy_central_manager); | 123 mock_central_manager_->central_manager().bluetoothTestMac = this; |
| 124 [mock_central_manager_->central_manager() | |
| 125 setState:CBCentralManagerStatePoweredOn]; | |
| 126 adapter_mac_->SetCentralManagerForTesting( | |
| 127 (id)mock_central_manager_->central_manager()); | |
| 103 } | 128 } |
| 104 } | 129 } |
| 105 | 130 |
| 106 BluetoothDevice* BluetoothTestMac::DiscoverLowEnergyDevice(int device_ordinal) { | 131 BluetoothDevice* BluetoothTestMac::DiscoverLowEnergyDevice(int device_ordinal) { |
| 107 TestBluetoothAdapterObserver observer(adapter_); | 132 TestBluetoothAdapterObserver observer(adapter_); |
| 108 CBCentralManager* central_manager = adapter_mac_->low_energy_central_manager_; | 133 CBCentralManager* central_manager = adapter_mac_->low_energy_central_manager_; |
| 109 BluetoothLowEnergyCentralManagerDelegate* central_manager_delegate = | 134 BluetoothLowEnergyCentralManagerDelegate* central_manager_delegate = |
| 110 adapter_mac_->low_energy_central_manager_delegate_; | 135 adapter_mac_->low_energy_central_manager_delegate_; |
| 111 Class cbuuid_class = NSClassFromString(@"CBUUID"); | 136 Class cbuuid_class = NSClassFromString(@"CBUUID"); |
| 112 switch (device_ordinal) { | 137 switch (device_ordinal) { |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 144 stringWithUTF8String:kTestUUIDLinkLoss.c_str()]] | 169 stringWithUTF8String:kTestUUIDLinkLoss.c_str()]] |
| 145 ]; | 170 ]; |
| 146 NSDictionary* advertisement_data = CreateAdvertisementData(name, uuids); | 171 NSDictionary* advertisement_data = CreateAdvertisementData(name, uuids); |
| 147 [central_manager_delegate centralManager:central_manager | 172 [central_manager_delegate centralManager:central_manager |
| 148 didDiscoverPeripheral:peripheral | 173 didDiscoverPeripheral:peripheral |
| 149 advertisementData:advertisement_data | 174 advertisementData:advertisement_data |
| 150 RSSI:@(0)]; | 175 RSSI:@(0)]; |
| 151 break; | 176 break; |
| 152 } | 177 } |
| 153 case 3: { | 178 case 3: { |
| 154 CBPeripheral* peripheral = CreateMockPeripheral( | 179 scoped_nsobject<NSString> uuid_string( |
| 155 [NSString stringWithUTF8String:kTestPeripheralUUID1.c_str()]); | 180 [[NSString alloc] initWithUTF8String:kTestPeripheralUUID1.c_str()]); |
| 156 NSString* name = | 181 scoped_nsobject<NSUUID> identifier( |
| 157 [NSString stringWithUTF8String:kTestDeviceNameEmpty.c_str()]; | 182 [[NSUUID alloc] initWithUUIDString:uuid_string]); |
| 158 NSArray* uuids = nil; | 183 scoped_nsobject<CBPeripheral> peripheral; |
| 159 NSDictionary* advertisement_data = CreateAdvertisementData(name, uuids); | 184 peripheral.reset(static_cast<CBPeripheral*>( |
| 185 [[MockCBPeripheral alloc] initWithIdentifier:identifier])); | |
| 186 scoped_nsobject<NSString> name( | |
| 187 [[NSString alloc] initWithUTF8String:kTestDeviceNameEmpty.c_str()]); | |
| 188 scoped_nsobject<NSDictionary> advertisement_data( | |
| 189 [CreateAdvertisementData(name, nil) retain]); | |
| 160 [central_manager_delegate centralManager:central_manager | 190 [central_manager_delegate centralManager:central_manager |
| 161 didDiscoverPeripheral:peripheral | 191 didDiscoverPeripheral:peripheral |
| 162 advertisementData:advertisement_data | 192 advertisementData:advertisement_data |
| 163 RSSI:@(0)]; | 193 RSSI:@(0)]; |
| 164 break; | 194 break; |
| 165 } | 195 } |
| 166 case 4: { | 196 case 4: { |
| 167 CBPeripheral* peripheral = CreateMockPeripheral( | 197 CBPeripheral* peripheral = CreateMockPeripheral( |
| 168 [NSString stringWithUTF8String:kTestPeripheralUUID2.c_str()]); | 198 [NSString stringWithUTF8String:kTestPeripheralUUID2.c_str()]); |
| 169 NSString* name = | 199 NSString* name = |
| 170 [NSString stringWithUTF8String:kTestDeviceNameEmpty.c_str()]; | 200 [NSString stringWithUTF8String:kTestDeviceNameEmpty.c_str()]; |
| 171 NSArray* uuids = nil; | 201 NSArray* uuids = nil; |
| 172 NSDictionary* advertisement_data = CreateAdvertisementData(name, uuids); | 202 NSDictionary* advertisement_data = CreateAdvertisementData(name, uuids); |
| 173 [central_manager_delegate centralManager:central_manager | 203 [central_manager_delegate centralManager:central_manager |
| 174 didDiscoverPeripheral:peripheral | 204 didDiscoverPeripheral:peripheral |
| 175 advertisementData:advertisement_data | 205 advertisementData:advertisement_data |
| 176 RSSI:@(0)]; | 206 RSSI:@(0)]; |
| 177 break; | 207 break; |
| 178 } | 208 } |
| 179 } | 209 } |
| 180 return observer.last_device(); | 210 return observer.last_device(); |
| 181 } | 211 } |
| 182 | 212 |
| 213 void BluetoothTestMac::SimulateGattConnection(BluetoothDevice* device) { | |
| 214 BluetoothLowEnergyDeviceMac* lowEnergyDeviceMac = | |
| 215 static_cast<BluetoothLowEnergyDeviceMac*>(device); | |
| 216 CBPeripheral* peripheral = lowEnergyDeviceMac->GetPeripheral(); | |
| 217 MockCBPeripheral* mockPeripheral = (MockCBPeripheral*)peripheral; | |
| 218 [mockPeripheral setState:CBPeripheralStateConnected]; | |
| 219 CBCentralManager* centralManager = | |
| 220 ObjCCast<CBCentralManager>(mock_central_manager_->central_manager()); | |
| 221 [centralManager.delegate centralManager:centralManager | |
| 222 didConnectPeripheral:peripheral]; | |
| 223 } | |
| 224 | |
| 225 void BluetoothTestMac::SimulateGattDisconnection(BluetoothDevice* device) { | |
| 226 BluetoothLowEnergyDeviceMac* lowEnergyDeviceMac = | |
| 227 static_cast<BluetoothLowEnergyDeviceMac*>(device); | |
| 228 CBPeripheral* peripheral = lowEnergyDeviceMac->GetPeripheral(); | |
| 229 MockCBPeripheral* mockPeripheral = (MockCBPeripheral*)peripheral; | |
| 230 [mockPeripheral setState:CBPeripheralStateDisconnected]; | |
| 231 CBCentralManager* centralManager = | |
| 232 ObjCCast<CBCentralManager>(mock_central_manager_->central_manager()); | |
| 233 [centralManager.delegate centralManager:centralManager | |
| 234 didDisconnectPeripheral:peripheral | |
| 235 error:nil]; | |
| 236 } | |
| 237 | |
| 238 void BluetoothTestMac::SimulateGattConnectionError( | |
| 239 BluetoothDevice* device, | |
| 240 BluetoothDevice::ConnectErrorCode errorCode) { | |
| 241 BluetoothLowEnergyDeviceMac* lowEnergyDeviceMac = | |
| 242 static_cast<BluetoothLowEnergyDeviceMac*>(device); | |
| 243 CBPeripheral* peripheral = lowEnergyDeviceMac->GetPeripheral(); | |
| 244 MockCBPeripheral* mockPeripheral = (MockCBPeripheral*)peripheral; | |
| 245 [mockPeripheral setState:CBPeripheralStateDisconnected]; | |
| 246 CBCentralManager* centralManager = | |
| 247 ObjCCast<CBCentralManager>(mock_central_manager_->central_manager()); | |
| 248 // TODO(http://crbug.com/585894): Need to convert the connect error code into | |
| 249 // NSError | |
| 250 NSError* error = [NSError errorWithDomain:@"BluetoothDevice::ConnectErrorCode" | |
| 251 code:-1 | |
| 252 userInfo:nil]; | |
| 253 [centralManager.delegate centralManager:centralManager | |
| 254 didFailToConnectPeripheral:peripheral | |
| 255 error:error]; | |
| 256 } | |
| 257 | |
| 258 void BluetoothTestMac::OnFakeBluetoothDeviceConnectGattCalled() { | |
| 259 gatt_connection_attempts_++; | |
| 260 } | |
| 261 | |
| 262 void BluetoothTestMac::OnFakeBluetoothGattDisconnect() { | |
| 263 gatt_disconnection_attempts_++; | |
| 264 } | |
| 265 | |
| 183 // Utility function for generating new (CBUUID, address) pairs where CBUUID | 266 // Utility function for generating new (CBUUID, address) pairs where CBUUID |
| 184 // hashes to address. For use when adding a new device address to the testing | 267 // hashes to address. For use when adding a new device address to the testing |
| 185 // suite because CoreBluetooth peripherals have CBUUIDs in place of addresses, | 268 // suite because CoreBluetooth peripherals have CBUUIDs in place of addresses, |
| 186 // and we construct fake addresses for them by hashing the CBUUID. By changing | 269 // and we construct fake addresses for them by hashing the CBUUID. By changing |
| 187 // |target| the user can generate sequentially numbered test addresses. | 270 // |target| the user can generate sequentially numbered test addresses. |
| 188 // | 271 // |
| 189 // std::string BluetoothTestMac::FindCBUUIDForHashTarget() { | 272 // std::string BluetoothTestMac::FindCBUUIDForHashTarget() { |
| 190 // // The desired first 6 digits of the hash. For example 0100000, 020000, | 273 // // The desired first 6 digits of the hash. For example 0100000, 020000, |
| 191 // // 030000, ... | 274 // // 030000, ... |
| 192 // const std::string target = "010000"; | 275 // const std::string target = "010000"; |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 209 // crypto::SHA256HashString(input_str, raw, sizeof(raw)); | 292 // crypto::SHA256HashString(input_str, raw, sizeof(raw)); |
| 210 // if (base::HexEncode(raw, sizeof(raw)) == target) { | 293 // if (base::HexEncode(raw, sizeof(raw)) == target) { |
| 211 // return input_str; | 294 // return input_str; |
| 212 // } | 295 // } |
| 213 // ++input[0]; | 296 // ++input[0]; |
| 214 // } | 297 // } |
| 215 // return ""; | 298 // return ""; |
| 216 // } | 299 // } |
| 217 | 300 |
| 218 } // namespace device | 301 } // namespace device |
| OLD | NEW |