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/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "build/build_config.h" | 10 #include "build/build_config.h" |
| 11 #include "device/bluetooth/bluetooth_adapter_mac.h" | 11 #include "device/bluetooth/bluetooth_adapter_mac.h" |
| 12 #include "device/bluetooth/test/mock_bluetooth_cbperipheral_mac.h" | |
| 12 #include "device/bluetooth/test/mock_bluetooth_central_manager_mac.h" | 13 #include "device/bluetooth/test/mock_bluetooth_central_manager_mac.h" |
| 13 #include "device/bluetooth/test/test_bluetooth_adapter_observer.h" | 14 #include "device/bluetooth/test/test_bluetooth_adapter_observer.h" |
| 14 #include "third_party/ocmock/OCMock/OCMock.h" | 15 #include "third_party/ocmock/OCMock/OCMock.h" |
| 15 | 16 |
| 16 #if defined(OS_IOS) | |
| 17 #import <CoreBluetooth/CoreBluetooth.h> | 17 #import <CoreBluetooth/CoreBluetooth.h> |
| 18 #else // !defined(OS_IOS) | |
| 19 #import <IOBluetooth/IOBluetooth.h> | |
| 20 #endif // defined(OS_IOS) | |
| 21 | 18 |
| 22 namespace device { | 19 namespace device { |
| 23 | 20 |
| 24 namespace { | 21 namespace { |
| 25 | 22 |
| 26 CBPeripheral* CreateMockPeripheral(NSString* peripheral_identifier) { | 23 CBPeripheral* CreateMockPeripheral(NSString* peripheral_identifier) { |
| 27 Class peripheral_class = NSClassFromString(@"CBPeripheral"); | 24 Class peripheral_class = NSClassFromString(@"CBPeripheral"); |
| 28 id mock_peripheral = [OCMockObject mockForClass:[peripheral_class class]]; | 25 id mock_peripheral = [OCMockObject mockForClass:[peripheral_class class]]; |
| 29 [[[mock_peripheral stub] andReturnValue:@(CBPeripheralStateDisconnected)] | 26 [[[mock_peripheral stub] andReturnValue:@(CBPeripheralStateDisconnected)] |
| 30 performSelector:@selector(state)]; | 27 performSelector:@selector(state)]; |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 56 } // namespace | 53 } // namespace |
| 57 | 54 |
| 58 // UUID1 hashes to kTestDeviceAddress1, and UUID2 to kTestDeviceAddress2. | 55 // UUID1 hashes to kTestDeviceAddress1, and UUID2 to kTestDeviceAddress2. |
| 59 const std::string BluetoothTestMac::kTestPeripheralUUID1 = | 56 const std::string BluetoothTestMac::kTestPeripheralUUID1 = |
| 60 "34045B00-0000-0000-0000-000000000000"; | 57 "34045B00-0000-0000-0000-000000000000"; |
| 61 const std::string BluetoothTestMac::kTestPeripheralUUID2 = | 58 const std::string BluetoothTestMac::kTestPeripheralUUID2 = |
| 62 "EC1B8F00-0000-0000-0000-000000000000"; | 59 "EC1B8F00-0000-0000-0000-000000000000"; |
| 63 | 60 |
| 64 BluetoothTestMac::BluetoothTestMac() {} | 61 BluetoothTestMac::BluetoothTestMac() {} |
| 65 | 62 |
| 66 BluetoothTestMac::~BluetoothTestMac() {} | 63 BluetoothTestMac::~BluetoothTestMac() { |
| 64 [(MockCentralManager*)mock_central_manager_ release]; | |
|
scheib
2016/02/10 18:59:46
Use static_cast instead of C-Style casts. https://
jlebel
2016/02/10 21:20:08
Done.
| |
| 65 } | |
| 67 | 66 |
| 68 void BluetoothTestMac::SetUp() {} | 67 void BluetoothTestMac::SetUp() {} |
| 69 | 68 |
| 70 bool BluetoothTestMac::PlatformSupportsLowEnergy() { | 69 bool BluetoothTestMac::PlatformSupportsLowEnergy() { |
| 71 return BluetoothAdapterMac::IsLowEnergyAvailable(); | 70 return BluetoothAdapterMac::IsLowEnergyAvailable(); |
| 72 } | 71 } |
| 73 | 72 |
| 74 void BluetoothTestMac::InitWithDefaultAdapter() { | 73 void BluetoothTestMac::InitWithDefaultAdapter() { |
| 75 adapter_mac_ = BluetoothAdapterMac::CreateAdapter().get(); | 74 adapter_mac_ = BluetoothAdapterMac::CreateAdapter().get(); |
| 76 adapter_ = adapter_mac_; | 75 adapter_ = adapter_mac_; |
| 77 } | 76 } |
| 78 | 77 |
| 79 void BluetoothTestMac::InitWithoutDefaultAdapter() { | 78 void BluetoothTestMac::InitWithoutDefaultAdapter() { |
| 80 adapter_mac_ = BluetoothAdapterMac::CreateAdapterForTest( | 79 adapter_mac_ = BluetoothAdapterMac::CreateAdapterForTest( |
| 81 "", "", message_loop_.task_runner()) | 80 "", "", message_loop_.task_runner()) |
| 82 .get(); | 81 .get(); |
| 83 adapter_ = adapter_mac_; | 82 adapter_ = adapter_mac_; |
| 84 | 83 |
| 85 if (BluetoothAdapterMac::IsLowEnergyAvailable()) { | 84 if (BluetoothAdapterMac::IsLowEnergyAvailable()) { |
| 86 id low_energy_central_manager = [[MockCentralManager alloc] init]; | 85 mock_central_manager_ = [[MockCentralManager alloc] init]; |
| 87 [low_energy_central_manager setState:CBCentralManagerStateUnsupported]; | 86 MockCentralManager* mock_central_manager = |
| 88 adapter_mac_->SetCentralManagerForTesting(low_energy_central_manager); | 87 (MockCentralManager*)mock_central_manager_; |
| 88 [mock_central_manager setBluetoothTestMac:this]; | |
| 89 [mock_central_manager setState:CBCentralManagerStateUnsupported]; | |
| 90 adapter_mac_->SetCentralManagerForTesting((id)mock_central_manager); | |
| 89 } | 91 } |
| 90 } | 92 } |
| 91 | 93 |
| 92 void BluetoothTestMac::InitWithFakeAdapter() { | 94 void BluetoothTestMac::InitWithFakeAdapter() { |
| 93 adapter_mac_ = | 95 adapter_mac_ = |
| 94 BluetoothAdapterMac::CreateAdapterForTest( | 96 BluetoothAdapterMac::CreateAdapterForTest( |
| 95 kTestAdapterName, kTestAdapterAddress, message_loop_.task_runner()) | 97 kTestAdapterName, kTestAdapterAddress, message_loop_.task_runner()) |
| 96 .get(); | 98 .get(); |
| 97 adapter_ = adapter_mac_; | 99 adapter_ = adapter_mac_; |
| 98 | 100 |
| 99 if (BluetoothAdapterMac::IsLowEnergyAvailable()) { | 101 if (BluetoothAdapterMac::IsLowEnergyAvailable()) { |
| 100 id low_energy_central_manager = [[MockCentralManager alloc] init]; | 102 mock_central_manager_ = [[MockCentralManager alloc] init]; |
| 101 [low_energy_central_manager setState:CBCentralManagerStatePoweredOn]; | 103 MockCentralManager* mock_central_manager = |
| 102 adapter_mac_->SetCentralManagerForTesting(low_energy_central_manager); | 104 (MockCentralManager*)mock_central_manager_; |
| 105 mock_central_manager.bluetoothTestMac = this; | |
| 106 [mock_central_manager setState:CBCentralManagerStatePoweredOn]; | |
| 107 adapter_mac_->SetCentralManagerForTesting((id)mock_central_manager); | |
| 103 } | 108 } |
| 104 } | 109 } |
| 105 | 110 |
| 106 BluetoothDevice* BluetoothTestMac::DiscoverLowEnergyDevice(int device_ordinal) { | 111 BluetoothDevice* BluetoothTestMac::DiscoverLowEnergyDevice(int device_ordinal) { |
| 107 TestBluetoothAdapterObserver observer(adapter_); | 112 TestBluetoothAdapterObserver observer(adapter_); |
| 108 CBCentralManager* central_manager = adapter_mac_->low_energy_central_manager_; | 113 CBCentralManager* central_manager = adapter_mac_->low_energy_central_manager_; |
| 109 BluetoothLowEnergyCentralManagerDelegate* central_manager_delegate = | 114 BluetoothLowEnergyCentralManagerDelegate* central_manager_delegate = |
| 110 adapter_mac_->low_energy_central_manager_delegate_; | 115 adapter_mac_->low_energy_central_manager_delegate_; |
| 111 Class cbuuid_class = NSClassFromString(@"CBUUID"); | 116 Class cbuuid_class = NSClassFromString(@"CBUUID"); |
| 112 switch (device_ordinal) { | 117 switch (device_ordinal) { |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 144 stringWithUTF8String:kTestUUIDLinkLoss.c_str()]] | 149 stringWithUTF8String:kTestUUIDLinkLoss.c_str()]] |
| 145 ]; | 150 ]; |
| 146 NSDictionary* advertisement_data = CreateAdvertisementData(name, uuids); | 151 NSDictionary* advertisement_data = CreateAdvertisementData(name, uuids); |
| 147 [central_manager_delegate centralManager:central_manager | 152 [central_manager_delegate centralManager:central_manager |
| 148 didDiscoverPeripheral:peripheral | 153 didDiscoverPeripheral:peripheral |
| 149 advertisementData:advertisement_data | 154 advertisementData:advertisement_data |
| 150 RSSI:[NSNumber numberWithInt:0]]; | 155 RSSI:[NSNumber numberWithInt:0]]; |
| 151 break; | 156 break; |
| 152 } | 157 } |
| 153 case 3: { | 158 case 3: { |
| 154 CBPeripheral* peripheral = CreateMockPeripheral( | 159 NSString* uuid_string = |
| 155 [NSString stringWithUTF8String:kTestPeripheralUUID1.c_str()]); | 160 [NSString stringWithUTF8String:kTestPeripheralUUID1.c_str()]; |
| 161 NSUUID* identifier = [[NSUUID alloc] initWithUUIDString:uuid_string]; | |
| 162 CBPeripheral* peripheral = (CBPeripheral*)[[MockCBPeripheral alloc] | |
|
scheib
2016/02/10 18:59:46
scoped object?
jlebel
2016/02/10 21:20:08
Done.
| |
| 163 initWithIdentifier:identifier]; | |
| 156 NSString* name = | 164 NSString* name = |
| 157 [NSString stringWithUTF8String:kTestDeviceNameEmpty.c_str()]; | 165 [NSString stringWithUTF8String:kTestDeviceNameEmpty.c_str()]; |
| 158 NSArray* uuids = nil; | 166 NSArray* uuids = nil; |
| 159 NSDictionary* advertisement_data = CreateAdvertisementData(name, uuids); | 167 NSDictionary* advertisement_data = CreateAdvertisementData(name, uuids); |
| 160 [central_manager_delegate centralManager:central_manager | 168 [central_manager_delegate centralManager:central_manager |
| 161 didDiscoverPeripheral:peripheral | 169 didDiscoverPeripheral:peripheral |
| 162 advertisementData:advertisement_data | 170 advertisementData:advertisement_data |
| 163 RSSI:[NSNumber numberWithInt:0]]; | 171 RSSI:[NSNumber numberWithInt:0]]; |
| 172 [peripheral release]; | |
| 164 break; | 173 break; |
| 165 } | 174 } |
| 166 case 4: { | 175 case 4: { |
| 167 CBPeripheral* peripheral = CreateMockPeripheral( | 176 CBPeripheral* peripheral = CreateMockPeripheral( |
| 168 [NSString stringWithUTF8String:kTestPeripheralUUID2.c_str()]); | 177 [NSString stringWithUTF8String:kTestPeripheralUUID2.c_str()]); |
| 169 NSString* name = | 178 NSString* name = |
| 170 [NSString stringWithUTF8String:kTestDeviceNameEmpty.c_str()]; | 179 [NSString stringWithUTF8String:kTestDeviceNameEmpty.c_str()]; |
| 171 NSArray* uuids = nil; | 180 NSArray* uuids = nil; |
| 172 NSDictionary* advertisement_data = CreateAdvertisementData(name, uuids); | 181 NSDictionary* advertisement_data = CreateAdvertisementData(name, uuids); |
| 173 [central_manager_delegate centralManager:central_manager | 182 [central_manager_delegate centralManager:central_manager |
| 174 didDiscoverPeripheral:peripheral | 183 didDiscoverPeripheral:peripheral |
| 175 advertisementData:advertisement_data | 184 advertisementData:advertisement_data |
| 176 RSSI:[NSNumber numberWithInt:0]]; | 185 RSSI:[NSNumber numberWithInt:0]]; |
| 177 break; | 186 break; |
| 178 } | 187 } |
| 179 } | 188 } |
| 180 return observer.last_device(); | 189 return observer.last_device(); |
| 181 } | 190 } |
| 182 | 191 |
| 192 void BluetoothTestMac::SimulateGattConnection(BluetoothDevice* device) { | |
| 193 BluetoothLowEnergyDeviceMac* lowEnergyDeviceMac = | |
| 194 (BluetoothLowEnergyDeviceMac*)device; | |
| 195 CBPeripheral* peripheral = lowEnergyDeviceMac->GetPeripheral(); | |
| 196 MockCBPeripheral* mockPeripheral = (MockCBPeripheral*)peripheral; | |
| 197 [mockPeripheral setStateForTesting:CBPeripheralStateConnected]; | |
| 198 CBCentralManager* centralManager = (CBCentralManager*)mock_central_manager_; | |
| 199 [centralManager.delegate centralManager:centralManager | |
| 200 didConnectPeripheral:peripheral]; | |
| 201 } | |
| 202 | |
| 203 void BluetoothTestMac::SimulateGattDisconnection(BluetoothDevice* device) { | |
| 204 BluetoothLowEnergyDeviceMac* lowEnergyDeviceMac = | |
| 205 (BluetoothLowEnergyDeviceMac*)device; | |
| 206 CBPeripheral* peripheral = lowEnergyDeviceMac->GetPeripheral(); | |
| 207 MockCBPeripheral* mockPeripheral = (MockCBPeripheral*)peripheral; | |
| 208 [mockPeripheral setStateForTesting:CBPeripheralStateDisconnected]; | |
| 209 CBCentralManager* centralManager = (CBCentralManager*)mock_central_manager_; | |
| 210 [centralManager.delegate centralManager:centralManager | |
| 211 didDisconnectPeripheral:peripheral | |
| 212 error:nil]; | |
| 213 } | |
| 214 | |
| 215 void BluetoothTestMac::SimulateGattConnectionError( | |
| 216 BluetoothDevice* device, | |
| 217 BluetoothDevice::ConnectErrorCode errorCode) { | |
| 218 BluetoothLowEnergyDeviceMac* lowEnergyDeviceMac = | |
| 219 (BluetoothLowEnergyDeviceMac*)device; | |
| 220 CBPeripheral* peripheral = lowEnergyDeviceMac->GetPeripheral(); | |
| 221 MockCBPeripheral* mockPeripheral = (MockCBPeripheral*)peripheral; | |
| 222 [mockPeripheral setStateForTesting:CBPeripheralStateDisconnected]; | |
| 223 CBCentralManager* centralManager = (CBCentralManager*)mock_central_manager_; | |
| 224 // TODO(jlebel): Need to convert the connect error code into NSError | |
|
scheib
2016/02/10 18:59:46
ditto: crbug.com
jlebel
2016/02/10 21:20:08
Done.
| |
| 225 NSError* error = [NSError errorWithDomain:@"BluetoothDevice::ConnectErrorCode" | |
| 226 code:-1 | |
| 227 userInfo:nil]; | |
| 228 [centralManager.delegate centralManager:centralManager | |
| 229 didFailToConnectPeripheral:peripheral | |
| 230 error:error]; | |
| 231 } | |
| 232 | |
| 233 void BluetoothTestMac::OnFakeBluetoothDeviceConnectGattCalled() { | |
| 234 gatt_connection_attempts_++; | |
| 235 } | |
| 236 | |
| 237 void BluetoothTestMac::OnFakeBluetoothGattDisconnect() { | |
| 238 gatt_disconnection_attempts_++; | |
| 239 } | |
| 240 | |
| 183 // Utility function for generating new (CBUUID, address) pairs where CBUUID | 241 // 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 | 242 // 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, | 243 // suite because CoreBluetooth peripherals have CBUUIDs in place of addresses, |
| 186 // and we construct fake addresses for them by hashing the CBUUID. By changing | 244 // and we construct fake addresses for them by hashing the CBUUID. By changing |
| 187 // |target| the user can generate sequentially numbered test addresses. | 245 // |target| the user can generate sequentially numbered test addresses. |
| 188 // | 246 // |
| 189 // std::string BluetoothTestMac::FindCBUUIDForHashTarget() { | 247 // std::string BluetoothTestMac::FindCBUUIDForHashTarget() { |
| 190 // // The desired first 6 digits of the hash. For example 0100000, 020000, | 248 // // The desired first 6 digits of the hash. For example 0100000, 020000, |
| 191 // // 030000, ... | 249 // // 030000, ... |
| 192 // const std::string target = "010000"; | 250 // const std::string target = "010000"; |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 209 // crypto::SHA256HashString(input_str, raw, sizeof(raw)); | 267 // crypto::SHA256HashString(input_str, raw, sizeof(raw)); |
| 210 // if (base::HexEncode(raw, sizeof(raw)) == target) { | 268 // if (base::HexEncode(raw, sizeof(raw)) == target) { |
| 211 // return input_str; | 269 // return input_str; |
| 212 // } | 270 // } |
| 213 // ++input[0]; | 271 // ++input[0]; |
| 214 // } | 272 // } |
| 215 // return ""; | 273 // return ""; |
| 216 // } | 274 // } |
| 217 | 275 |
| 218 } // namespace device | 276 } // namespace device |
| OLD | NEW |