| 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/mac/foundation_util.h" |
| 10 #include "base/strings/string_number_conversions.h" | 10 #include "base/strings/string_number_conversions.h" |
| 11 #include "build/build_config.h" | 11 #include "build/build_config.h" |
| 12 #include "device/bluetooth/bluetooth_adapter_mac.h" | 12 #include "device/bluetooth/bluetooth_adapter_mac.h" |
| 13 #include "device/bluetooth/bluetooth_remote_gatt_service_mac.h" |
| 13 #include "device/bluetooth/test/mock_bluetooth_cbperipheral_mac.h" | 14 #include "device/bluetooth/test/mock_bluetooth_cbperipheral_mac.h" |
| 14 #include "device/bluetooth/test/mock_bluetooth_central_manager_mac.h" | 15 #include "device/bluetooth/test/mock_bluetooth_central_manager_mac.h" |
| 15 #include "device/bluetooth/test/test_bluetooth_adapter_observer.h" | 16 #include "device/bluetooth/test/test_bluetooth_adapter_observer.h" |
| 16 #include "third_party/ocmock/OCMock/OCMock.h" | 17 #include "third_party/ocmock/OCMock/OCMock.h" |
| 17 | 18 |
| 18 #import <CoreBluetooth/CoreBluetooth.h> | 19 #import <CoreBluetooth/CoreBluetooth.h> |
| 19 | 20 |
| 20 using base::mac::ObjCCast; | 21 using base::mac::ObjCCast; |
| 21 using base::scoped_nsobject; | 22 using base::scoped_nsobject; |
| 22 | 23 |
| (...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 BluetoothDevice* BluetoothTestMac::DiscoverLowEnergyDevice(int device_ordinal) { | 112 BluetoothDevice* BluetoothTestMac::DiscoverLowEnergyDevice(int device_ordinal) { |
| 112 TestBluetoothAdapterObserver observer(adapter_); | 113 TestBluetoothAdapterObserver observer(adapter_); |
| 113 CBCentralManager* central_manager = adapter_mac_->low_energy_central_manager_; | 114 CBCentralManager* central_manager = adapter_mac_->low_energy_central_manager_; |
| 114 BluetoothLowEnergyCentralManagerDelegate* central_manager_delegate = | 115 BluetoothLowEnergyCentralManagerDelegate* central_manager_delegate = |
| 115 adapter_mac_->low_energy_central_manager_delegate_; | 116 adapter_mac_->low_energy_central_manager_delegate_; |
| 116 switch (device_ordinal) { | 117 switch (device_ordinal) { |
| 117 case 1: { | 118 case 1: { |
| 118 scoped_nsobject<MockCBPeripheral> mock_peripheral( | 119 scoped_nsobject<MockCBPeripheral> mock_peripheral( |
| 119 [[MockCBPeripheral alloc] | 120 [[MockCBPeripheral alloc] |
| 120 initWithUTF8StringIdentifier:kTestPeripheralUUID1.c_str()]); | 121 initWithUTF8StringIdentifier:kTestPeripheralUUID1.c_str()]); |
| 122 mock_peripheral.get().bluetoothTestMac = this; |
| 121 NSArray* uuids = @[ | 123 NSArray* uuids = @[ |
| 122 [CBUUID UUIDWithString:@(kTestUUIDGenericAccess.c_str())], | 124 [CBUUID UUIDWithString:@(kTestUUIDGenericAccess.c_str())], |
| 123 [CBUUID UUIDWithString:@(kTestUUIDGenericAttribute.c_str())] | 125 [CBUUID UUIDWithString:@(kTestUUIDGenericAttribute.c_str())] |
| 124 ]; | 126 ]; |
| 125 scoped_nsobject<NSDictionary> advertisement_data = | 127 scoped_nsobject<NSDictionary> advertisement_data = |
| 126 CreateAdvertisementData(@(kTestDeviceName.c_str()), uuids); | 128 CreateAdvertisementData(@(kTestDeviceName.c_str()), uuids); |
| 127 [central_manager_delegate centralManager:central_manager | 129 [central_manager_delegate centralManager:central_manager |
| 128 didDiscoverPeripheral:mock_peripheral.get().peripheral | 130 didDiscoverPeripheral:mock_peripheral.get().peripheral |
| 129 advertisementData:advertisement_data | 131 advertisementData:advertisement_data |
| 130 RSSI:@(0)]; | 132 RSSI:@(0)]; |
| 131 break; | 133 break; |
| 132 } | 134 } |
| 133 case 2: { | 135 case 2: { |
| 134 scoped_nsobject<MockCBPeripheral> mock_peripheral( | 136 scoped_nsobject<MockCBPeripheral> mock_peripheral( |
| 135 [[MockCBPeripheral alloc] | 137 [[MockCBPeripheral alloc] |
| 136 initWithUTF8StringIdentifier:kTestPeripheralUUID1.c_str()]); | 138 initWithUTF8StringIdentifier:kTestPeripheralUUID1.c_str()]); |
| 139 mock_peripheral.get().bluetoothTestMac = this; |
| 137 NSArray* uuids = @[ | 140 NSArray* uuids = @[ |
| 138 [CBUUID UUIDWithString:@(kTestUUIDImmediateAlert.c_str())], | 141 [CBUUID UUIDWithString:@(kTestUUIDImmediateAlert.c_str())], |
| 139 [CBUUID UUIDWithString:@(kTestUUIDLinkLoss.c_str())] | 142 [CBUUID UUIDWithString:@(kTestUUIDLinkLoss.c_str())] |
| 140 ]; | 143 ]; |
| 141 scoped_nsobject<NSDictionary> advertisement_data = | 144 scoped_nsobject<NSDictionary> advertisement_data = |
| 142 CreateAdvertisementData(@(kTestDeviceName.c_str()), uuids); | 145 CreateAdvertisementData(@(kTestDeviceName.c_str()), uuids); |
| 143 [central_manager_delegate centralManager:central_manager | 146 [central_manager_delegate centralManager:central_manager |
| 144 didDiscoverPeripheral:mock_peripheral.get().peripheral | 147 didDiscoverPeripheral:mock_peripheral.get().peripheral |
| 145 advertisementData:advertisement_data | 148 advertisementData:advertisement_data |
| 146 RSSI:@(0)]; | 149 RSSI:@(0)]; |
| 147 break; | 150 break; |
| 148 } | 151 } |
| 149 case 3: { | 152 case 3: { |
| 150 scoped_nsobject<MockCBPeripheral> mock_peripheral( | 153 scoped_nsobject<MockCBPeripheral> mock_peripheral( |
| 151 [[MockCBPeripheral alloc] | 154 [[MockCBPeripheral alloc] |
| 152 initWithUTF8StringIdentifier:kTestPeripheralUUID1.c_str()]); | 155 initWithUTF8StringIdentifier:kTestPeripheralUUID1.c_str()]); |
| 156 mock_peripheral.get().bluetoothTestMac = this; |
| 153 scoped_nsobject<NSDictionary> advertisement_data( | 157 scoped_nsobject<NSDictionary> advertisement_data( |
| 154 CreateAdvertisementData(@(kTestDeviceNameEmpty.c_str()), nil)); | 158 CreateAdvertisementData(@(kTestDeviceNameEmpty.c_str()), nil)); |
| 155 [central_manager_delegate centralManager:central_manager | 159 [central_manager_delegate centralManager:central_manager |
| 156 didDiscoverPeripheral:mock_peripheral.get().peripheral | 160 didDiscoverPeripheral:mock_peripheral.get().peripheral |
| 157 advertisementData:advertisement_data | 161 advertisementData:advertisement_data |
| 158 RSSI:@(0)]; | 162 RSSI:@(0)]; |
| 159 break; | 163 break; |
| 160 } | 164 } |
| 161 case 4: { | 165 case 4: { |
| 162 scoped_nsobject<MockCBPeripheral> mock_peripheral( | 166 scoped_nsobject<MockCBPeripheral> mock_peripheral( |
| 163 [[MockCBPeripheral alloc] | 167 [[MockCBPeripheral alloc] |
| 164 initWithUTF8StringIdentifier:kTestPeripheralUUID2.c_str()]); | 168 initWithUTF8StringIdentifier:kTestPeripheralUUID2.c_str()]); |
| 169 mock_peripheral.get().bluetoothTestMac = this; |
| 165 NSArray* uuids = nil; | 170 NSArray* uuids = nil; |
| 166 scoped_nsobject<NSDictionary> advertisement_data = | 171 scoped_nsobject<NSDictionary> advertisement_data = |
| 167 CreateAdvertisementData(@(kTestDeviceNameEmpty.c_str()), uuids); | 172 CreateAdvertisementData(@(kTestDeviceNameEmpty.c_str()), uuids); |
| 168 [central_manager_delegate centralManager:central_manager | 173 [central_manager_delegate centralManager:central_manager |
| 169 didDiscoverPeripheral:mock_peripheral.get().peripheral | 174 didDiscoverPeripheral:mock_peripheral.get().peripheral |
| 170 advertisementData:advertisement_data | 175 advertisementData:advertisement_data |
| 171 RSSI:@(0)]; | 176 RSSI:@(0)]; |
| 172 break; | 177 break; |
| 173 } | 178 } |
| 174 } | 179 } |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 210 CBPeripheral* peripheral = lowEnergyDeviceMac->GetPeripheral(); | 215 CBPeripheral* peripheral = lowEnergyDeviceMac->GetPeripheral(); |
| 211 MockCBPeripheral* mockPeripheral = (MockCBPeripheral*)peripheral; | 216 MockCBPeripheral* mockPeripheral = (MockCBPeripheral*)peripheral; |
| 212 [mockPeripheral setState:CBPeripheralStateDisconnected]; | 217 [mockPeripheral setState:CBPeripheralStateDisconnected]; |
| 213 CBCentralManager* centralManager = | 218 CBCentralManager* centralManager = |
| 214 ObjCCast<CBCentralManager>(mock_central_manager_->get()); | 219 ObjCCast<CBCentralManager>(mock_central_manager_->get()); |
| 215 [centralManager.delegate centralManager:centralManager | 220 [centralManager.delegate centralManager:centralManager |
| 216 didDisconnectPeripheral:peripheral | 221 didDisconnectPeripheral:peripheral |
| 217 error:nil]; | 222 error:nil]; |
| 218 } | 223 } |
| 219 | 224 |
| 225 void BluetoothTestMac::SimulateGattServicesDiscovered( |
| 226 BluetoothDevice* device, |
| 227 const std::vector<std::string>& uuids) { |
| 228 BluetoothLowEnergyDeviceMac* device_mac = |
| 229 static_cast<BluetoothLowEnergyDeviceMac*>(device); |
| 230 CBPeripheral* peripheral = device_mac->GetPeripheral(); |
| 231 MockCBPeripheral* peripheral_mock = ObjCCast<MockCBPeripheral>(peripheral); |
| 232 scoped_nsobject<NSMutableArray> services = [[NSMutableArray alloc] init]; |
| 233 for (auto uuid : uuids) { |
| 234 CBUUID* cb_service_uuid = [CBUUID UUIDWithString:@(uuid.c_str())]; |
| 235 [services addObject:cb_service_uuid]; |
| 236 } |
| 237 [peripheral_mock removeAllServices]; |
| 238 [peripheral_mock addServices:services]; |
| 239 [peripheral_mock didDiscoverWithError:nil]; |
| 240 } |
| 241 |
| 242 void BluetoothTestMac::SimulateGattServiceRemoved( |
| 243 BluetoothRemoteGattService* service) { |
| 244 BluetoothUUID bluetooth_service_uuid = service->GetUUID(); |
| 245 std::string service_uuid_string = bluetooth_service_uuid.canonical_value(); |
| 246 BluetoothRemoteGattServiceMac* mac_gatt_service = |
| 247 static_cast<BluetoothRemoteGattServiceMac*>(service); |
| 248 BluetoothDevice* device = service->GetDevice(); |
| 249 BluetoothLowEnergyDeviceMac* device_mac = |
| 250 static_cast<BluetoothLowEnergyDeviceMac*>(device); |
| 251 CBPeripheral* peripheral = device_mac->GetPeripheral(); |
| 252 MockCBPeripheral* peripheral_mock = ObjCCast<MockCBPeripheral>(peripheral); |
| 253 [peripheral_mock removeService:mac_gatt_service->GetService()]; |
| 254 [peripheral_mock didDiscoverWithError:nil]; |
| 255 } |
| 256 |
| 220 void BluetoothTestMac::OnFakeBluetoothDeviceConnectGattCalled() { | 257 void BluetoothTestMac::OnFakeBluetoothDeviceConnectGattCalled() { |
| 221 gatt_connection_attempts_++; | 258 gatt_connection_attempts_++; |
| 222 } | 259 } |
| 223 | 260 |
| 224 void BluetoothTestMac::OnFakeBluetoothGattDisconnect() { | 261 void BluetoothTestMac::OnFakeBluetoothGattDisconnect() { |
| 225 gatt_disconnection_attempts_++; | 262 gatt_disconnection_attempts_++; |
| 226 } | 263 } |
| 227 | 264 |
| 265 void BluetoothTestMac::OnFakeBluetoothServiceDiscovery() { |
| 266 gatt_discovery_attempts_++; |
| 267 } |
| 268 |
| 228 // Utility function for generating new (CBUUID, address) pairs where CBUUID | 269 // Utility function for generating new (CBUUID, address) pairs where CBUUID |
| 229 // hashes to address. For use when adding a new device address to the testing | 270 // hashes to address. For use when adding a new device address to the testing |
| 230 // suite because CoreBluetooth peripherals have CBUUIDs in place of addresses, | 271 // suite because CoreBluetooth peripherals have CBUUIDs in place of addresses, |
| 231 // and we construct fake addresses for them by hashing the CBUUID. By changing | 272 // and we construct fake addresses for them by hashing the CBUUID. By changing |
| 232 // |target| the user can generate sequentially numbered test addresses. | 273 // |target| the user can generate sequentially numbered test addresses. |
| 233 // | 274 // |
| 234 // std::string BluetoothTestMac::FindCBUUIDForHashTarget() { | 275 // std::string BluetoothTestMac::FindCBUUIDForHashTarget() { |
| 235 // // The desired first 6 digits of the hash. For example 0100000, 020000, | 276 // // The desired first 6 digits of the hash. For example 0100000, 020000, |
| 236 // // 030000, ... | 277 // // 030000, ... |
| 237 // const std::string target = "010000"; | 278 // const std::string target = "010000"; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 254 // crypto::SHA256HashString(input_str, raw, sizeof(raw)); | 295 // crypto::SHA256HashString(input_str, raw, sizeof(raw)); |
| 255 // if (base::HexEncode(raw, sizeof(raw)) == target) { | 296 // if (base::HexEncode(raw, sizeof(raw)) == target) { |
| 256 // return input_str; | 297 // return input_str; |
| 257 // } | 298 // } |
| 258 // ++input[0]; | 299 // ++input[0]; |
| 259 // } | 300 // } |
| 260 // return ""; | 301 // return ""; |
| 261 // } | 302 // } |
| 262 | 303 |
| 263 } // namespace device | 304 } // namespace device |
| OLD | NEW |