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 #if defined(OS_IOS) |
17 #import <CoreBluetooth/CoreBluetooth.h> | 18 #import <CoreBluetooth/CoreBluetooth.h> |
18 #else // !defined(OS_IOS) | 19 #else // !defined(OS_IOS) |
19 #import <IOBluetooth/IOBluetooth.h> | 20 #import <IOBluetooth/IOBluetooth.h> |
20 #endif // defined(OS_IOS) | 21 #endif // defined(OS_IOS) |
21 | 22 |
(...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
144 stringWithUTF8String:kTestUUIDLinkLoss.c_str()]] | 145 stringWithUTF8String:kTestUUIDLinkLoss.c_str()]] |
145 ]; | 146 ]; |
146 NSDictionary* advertisement_data = CreateAdvertisementData(name, uuids); | 147 NSDictionary* advertisement_data = CreateAdvertisementData(name, uuids); |
147 [central_manager_delegate centralManager:central_manager | 148 [central_manager_delegate centralManager:central_manager |
148 didDiscoverPeripheral:peripheral | 149 didDiscoverPeripheral:peripheral |
149 advertisementData:advertisement_data | 150 advertisementData:advertisement_data |
150 RSSI:[NSNumber numberWithInt:0]]; | 151 RSSI:[NSNumber numberWithInt:0]]; |
151 break; | 152 break; |
152 } | 153 } |
153 case 3: { | 154 case 3: { |
154 CBPeripheral* peripheral = CreateMockPeripheral( | 155 NSString* uuid_string = |
155 [NSString stringWithUTF8String:kTestPeripheralUUID1.c_str()]); | 156 [NSString stringWithUTF8String:kTestPeripheralUUID1.c_str()]; |
| 157 NSUUID* identifier = [[NSUUID alloc] initWithUUIDString:uuid_string]; |
| 158 CBPeripheral* peripheral = (CBPeripheral*)[[MockCBPeripheral alloc] |
| 159 initWithIdentifier:identifier]; |
156 NSString* name = | 160 NSString* name = |
157 [NSString stringWithUTF8String:kTestDeviceNameEmpty.c_str()]; | 161 [NSString stringWithUTF8String:kTestDeviceNameEmpty.c_str()]; |
158 NSArray* uuids = nil; | 162 NSArray* uuids = nil; |
159 NSDictionary* advertisement_data = CreateAdvertisementData(name, uuids); | 163 NSDictionary* advertisement_data = CreateAdvertisementData(name, uuids); |
160 [central_manager_delegate centralManager:central_manager | 164 [central_manager_delegate centralManager:central_manager |
161 didDiscoverPeripheral:peripheral | 165 didDiscoverPeripheral:peripheral |
162 advertisementData:advertisement_data | 166 advertisementData:advertisement_data |
163 RSSI:[NSNumber numberWithInt:0]]; | 167 RSSI:[NSNumber numberWithInt:0]]; |
| 168 [peripheral release]; |
164 break; | 169 break; |
165 } | 170 } |
166 case 4: { | 171 case 4: { |
167 CBPeripheral* peripheral = CreateMockPeripheral( | 172 CBPeripheral* peripheral = CreateMockPeripheral( |
168 [NSString stringWithUTF8String:kTestPeripheralUUID2.c_str()]); | 173 [NSString stringWithUTF8String:kTestPeripheralUUID2.c_str()]); |
169 NSString* name = | 174 NSString* name = |
170 [NSString stringWithUTF8String:kTestDeviceNameEmpty.c_str()]; | 175 [NSString stringWithUTF8String:kTestDeviceNameEmpty.c_str()]; |
171 NSArray* uuids = nil; | 176 NSArray* uuids = nil; |
172 NSDictionary* advertisement_data = CreateAdvertisementData(name, uuids); | 177 NSDictionary* advertisement_data = CreateAdvertisementData(name, uuids); |
173 [central_manager_delegate centralManager:central_manager | 178 [central_manager_delegate centralManager:central_manager |
174 didDiscoverPeripheral:peripheral | 179 didDiscoverPeripheral:peripheral |
175 advertisementData:advertisement_data | 180 advertisementData:advertisement_data |
176 RSSI:[NSNumber numberWithInt:0]]; | 181 RSSI:[NSNumber numberWithInt:0]]; |
177 break; | 182 break; |
178 } | 183 } |
179 } | 184 } |
180 return observer.last_device(); | 185 return observer.last_device(); |
181 } | 186 } |
182 | 187 |
| 188 void BluetoothTestMac::SimulateGattConnection(BluetoothDevice* device) { |
| 189 BluetoothLowEnergyDeviceMac* lowEnergyDeviceMac = |
| 190 (BluetoothLowEnergyDeviceMac*)device; |
| 191 BluetoothAdapterMac* adapter = lowEnergyDeviceMac->GetBluetoothAdapterMac(); |
| 192 CBPeripheral* peripheral = lowEnergyDeviceMac->GetPeripheral(); |
| 193 MockCBPeripheral* mockPeripheral = (MockCBPeripheral*)peripheral; |
| 194 [mockPeripheral setStateForTesting:CBPeripheralStateConnected]; |
| 195 CBCentralManager* centralManager = adapter->GetCentralManagerForTesting(); |
| 196 [centralManager.delegate centralManager:centralManager |
| 197 didConnectPeripheral:peripheral]; |
| 198 } |
| 199 |
183 // Utility function for generating new (CBUUID, address) pairs where CBUUID | 200 // 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 | 201 // 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, | 202 // suite because CoreBluetooth peripherals have CBUUIDs in place of addresses, |
186 // and we construct fake addresses for them by hashing the CBUUID. By changing | 203 // and we construct fake addresses for them by hashing the CBUUID. By changing |
187 // |target| the user can generate sequentially numbered test addresses. | 204 // |target| the user can generate sequentially numbered test addresses. |
188 // | 205 // |
189 // std::string BluetoothTestMac::FindCBUUIDForHashTarget() { | 206 // std::string BluetoothTestMac::FindCBUUIDForHashTarget() { |
190 // // The desired first 6 digits of the hash. For example 0100000, 020000, | 207 // // The desired first 6 digits of the hash. For example 0100000, 020000, |
191 // // 030000, ... | 208 // // 030000, ... |
192 // const std::string target = "010000"; | 209 // const std::string target = "010000"; |
(...skipping 16 matching lines...) Expand all Loading... |
209 // crypto::SHA256HashString(input_str, raw, sizeof(raw)); | 226 // crypto::SHA256HashString(input_str, raw, sizeof(raw)); |
210 // if (base::HexEncode(raw, sizeof(raw)) == target) { | 227 // if (base::HexEncode(raw, sizeof(raw)) == target) { |
211 // return input_str; | 228 // return input_str; |
212 // } | 229 // } |
213 // ++input[0]; | 230 // ++input[0]; |
214 // } | 231 // } |
215 // return ""; | 232 // return ""; |
216 // } | 233 // } |
217 | 234 |
218 } // namespace device | 235 } // namespace device |
OLD | NEW |