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 [static_cast<MockCentralManager*>(mock_central_manager_) release]; | |
| 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 = |
|
msarda
2016/02/22 14:01:06
Do not use static cast for Objective-C objects. Us
jlebel
2016/02/24 16:54:54
Done.
| |
| 88 adapter_mac_->SetCentralManagerForTesting(low_energy_central_manager); | 87 static_cast<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 static_cast<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 base::scoped_nsobject<NSString> uuid_string( |
| 155 [NSString stringWithUTF8String:kTestPeripheralUUID1.c_str()]); | 160 [[NSString alloc] initWithUTF8String:kTestPeripheralUUID1.c_str()]); |
| 156 NSString* name = | 161 base::scoped_nsobject<NSUUID> identifier( |
| 157 [NSString stringWithUTF8String:kTestDeviceNameEmpty.c_str()]; | 162 [[NSUUID alloc] initWithUUIDString:uuid_string]); |
| 158 NSArray* uuids = nil; | 163 base::scoped_nsobject<CBPeripheral> peripheral; |
| 159 NSDictionary* advertisement_data = CreateAdvertisementData(name, uuids); | 164 peripheral.reset(static_cast<CBPeripheral*>( |
| 165 [[MockCBPeripheral alloc] initWithIdentifier:identifier])); | |
| 166 base::scoped_nsobject<NSString> name( | |
| 167 [[NSString alloc] initWithUTF8String:kTestDeviceNameEmpty.c_str()]); | |
| 168 base::scoped_nsobject<NSDictionary> advertisement_data( | |
| 169 [CreateAdvertisementData(name, nil) retain]); | |
| 160 [central_manager_delegate centralManager:central_manager | 170 [central_manager_delegate centralManager:central_manager |
| 161 didDiscoverPeripheral:peripheral | 171 didDiscoverPeripheral:peripheral |
| 162 advertisementData:advertisement_data | 172 advertisementData:advertisement_data |
| 163 RSSI:[NSNumber numberWithInt:0]]; | 173 RSSI:[NSNumber numberWithInt:0]]; |
| 164 break; | 174 break; |
| 165 } | 175 } |
| 166 case 4: { | 176 case 4: { |
| 167 CBPeripheral* peripheral = CreateMockPeripheral( | 177 CBPeripheral* peripheral = CreateMockPeripheral( |
| 168 [NSString stringWithUTF8String:kTestPeripheralUUID2.c_str()]); | 178 [NSString stringWithUTF8String:kTestPeripheralUUID2.c_str()]); |
| 169 NSString* name = | 179 NSString* name = |
| 170 [NSString stringWithUTF8String:kTestDeviceNameEmpty.c_str()]; | 180 [NSString stringWithUTF8String:kTestDeviceNameEmpty.c_str()]; |
| 171 NSArray* uuids = nil; | 181 NSArray* uuids = nil; |
| 172 NSDictionary* advertisement_data = CreateAdvertisementData(name, uuids); | 182 NSDictionary* advertisement_data = CreateAdvertisementData(name, uuids); |
| 173 [central_manager_delegate centralManager:central_manager | 183 [central_manager_delegate centralManager:central_manager |
| 174 didDiscoverPeripheral:peripheral | 184 didDiscoverPeripheral:peripheral |
| 175 advertisementData:advertisement_data | 185 advertisementData:advertisement_data |
| 176 RSSI:[NSNumber numberWithInt:0]]; | 186 RSSI:[NSNumber numberWithInt:0]]; |
| 177 break; | 187 break; |
| 178 } | 188 } |
| 179 } | 189 } |
| 180 return observer.last_device(); | 190 return observer.last_device(); |
| 181 } | 191 } |
| 182 | 192 |
| 193 void BluetoothTestMac::SimulateGattConnection(BluetoothDevice* device) { | |
| 194 BluetoothLowEnergyDeviceMac* lowEnergyDeviceMac = | |
| 195 static_cast<BluetoothLowEnergyDeviceMac*>(device); | |
| 196 CBPeripheral* peripheral = lowEnergyDeviceMac->GetPeripheral(); | |
| 197 MockCBPeripheral* mockPeripheral = static_cast<MockCBPeripheral*>(peripheral); | |
|
msarda
2016/02/22 14:01:06
https://code.google.com/p/chromium/codesearch#chro
jlebel
2016/02/24 16:54:54
Done.
| |
| 198 [mockPeripheral setState:CBPeripheralStateConnected]; | |
| 199 CBCentralManager* centralManager = | |
| 200 static_cast<CBCentralManager*>(mock_central_manager_); | |
| 201 [centralManager.delegate centralManager:centralManager | |
| 202 didConnectPeripheral:peripheral]; | |
| 203 } | |
| 204 | |
| 205 void BluetoothTestMac::SimulateGattDisconnection(BluetoothDevice* device) { | |
| 206 BluetoothLowEnergyDeviceMac* lowEnergyDeviceMac = | |
| 207 static_cast<BluetoothLowEnergyDeviceMac*>(device); | |
| 208 CBPeripheral* peripheral = lowEnergyDeviceMac->GetPeripheral(); | |
| 209 MockCBPeripheral* mockPeripheral = static_cast<MockCBPeripheral*>(peripheral); | |
|
msarda
2016/02/22 14:01:06
Do not use static_cast for Objective-C objects. Us
jlebel
2016/02/24 16:54:54
Done.
| |
| 210 [mockPeripheral setState:CBPeripheralStateDisconnected]; | |
| 211 CBCentralManager* centralManager = | |
| 212 static_cast<CBCentralManager*>(mock_central_manager_); | |
| 213 [centralManager.delegate centralManager:centralManager | |
| 214 didDisconnectPeripheral:peripheral | |
| 215 error:nil]; | |
| 216 } | |
| 217 | |
| 218 void BluetoothTestMac::SimulateGattConnectionError( | |
| 219 BluetoothDevice* device, | |
| 220 BluetoothDevice::ConnectErrorCode errorCode) { | |
| 221 BluetoothLowEnergyDeviceMac* lowEnergyDeviceMac = | |
| 222 static_cast<BluetoothLowEnergyDeviceMac*>(device); | |
| 223 CBPeripheral* peripheral = lowEnergyDeviceMac->GetPeripheral(); | |
| 224 MockCBPeripheral* mockPeripheral = static_cast<MockCBPeripheral*>(peripheral); | |
| 225 [mockPeripheral setState:CBPeripheralStateDisconnected]; | |
| 226 CBCentralManager* centralManager = | |
| 227 static_cast<CBCentralManager*>(mock_central_manager_); | |
| 228 // TODO(http://crbug.com/585894): Need to convert the connect error code into | |
| 229 // NSError | |
| 230 NSError* error = [NSError errorWithDomain:@"BluetoothDevice::ConnectErrorCode" | |
| 231 code:-1 | |
| 232 userInfo:nil]; | |
| 233 [centralManager.delegate centralManager:centralManager | |
| 234 didFailToConnectPeripheral:peripheral | |
| 235 error:error]; | |
| 236 } | |
| 237 | |
| 238 void BluetoothTestMac::OnFakeBluetoothDeviceConnectGattCalled() { | |
| 239 gatt_connection_attempts_++; | |
| 240 } | |
| 241 | |
| 242 void BluetoothTestMac::OnFakeBluetoothGattDisconnect() { | |
| 243 gatt_disconnection_attempts_++; | |
| 244 } | |
| 245 | |
| 183 // Utility function for generating new (CBUUID, address) pairs where CBUUID | 246 // 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 | 247 // 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, | 248 // suite because CoreBluetooth peripherals have CBUUIDs in place of addresses, |
| 186 // and we construct fake addresses for them by hashing the CBUUID. By changing | 249 // and we construct fake addresses for them by hashing the CBUUID. By changing |
| 187 // |target| the user can generate sequentially numbered test addresses. | 250 // |target| the user can generate sequentially numbered test addresses. |
| 188 // | 251 // |
| 189 // std::string BluetoothTestMac::FindCBUUIDForHashTarget() { | 252 // std::string BluetoothTestMac::FindCBUUIDForHashTarget() { |
| 190 // // The desired first 6 digits of the hash. For example 0100000, 020000, | 253 // // The desired first 6 digits of the hash. For example 0100000, 020000, |
| 191 // // 030000, ... | 254 // // 030000, ... |
| 192 // const std::string target = "010000"; | 255 // const std::string target = "010000"; |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 209 // crypto::SHA256HashString(input_str, raw, sizeof(raw)); | 272 // crypto::SHA256HashString(input_str, raw, sizeof(raw)); |
| 210 // if (base::HexEncode(raw, sizeof(raw)) == target) { | 273 // if (base::HexEncode(raw, sizeof(raw)) == target) { |
| 211 // return input_str; | 274 // return input_str; |
| 212 // } | 275 // } |
| 213 // ++input[0]; | 276 // ++input[0]; |
| 214 // } | 277 // } |
| 215 // return ""; | 278 // return ""; |
| 216 // } | 279 // } |
| 217 | 280 |
| 218 } // namespace device | 281 } // namespace device |
| OLD | NEW |