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