| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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 "base/memory/ref_counted.h" | 5 #include "base/memory/ref_counted.h" |
| 6 #include "base/test/test_simple_task_runner.h" | 6 #include "base/test/test_simple_task_runner.h" |
| 7 #include "device/bluetooth/bluetooth_adapter.h" | 7 #include "device/bluetooth/bluetooth_adapter.h" |
| 8 #include "device/bluetooth/bluetooth_adapter_mac.h" | 8 #include "device/bluetooth/bluetooth_adapter_mac.h" |
| 9 #include "device/bluetooth/bluetooth_discovery_session.h" | 9 #include "device/bluetooth/bluetooth_discovery_session.h" |
| 10 #include "device/bluetooth/bluetooth_low_energy_device_mac.h" | 10 #include "device/bluetooth/bluetooth_low_energy_device_mac.h" |
| 11 #include "device/bluetooth/test/mock_bluetooth_central_manager_mac.h" | 11 #include "device/bluetooth/test/mock_bluetooth_central_manager_mac.h" |
| 12 #include "testing/gtest/include/gtest/gtest.h" | 12 #include "testing/gtest/include/gtest/gtest.h" |
| 13 #include "third_party/ocmock/OCMock/OCMock.h" | 13 #include "third_party/ocmock/OCMock/OCMock.h" |
| 14 | 14 |
| 15 #if defined(OS_IOS) | 15 #if defined(OS_IOS) |
| 16 #import <CoreBluetooth/CoreBluetooth.h> | 16 #import <CoreBluetooth/CoreBluetooth.h> |
| 17 #else // !defined(OS_IOS) | 17 #else // !defined(OS_IOS) |
| 18 #import <IOBluetooth/IOBluetooth.h> | 18 #import <IOBluetooth/IOBluetooth.h> |
| 19 #endif // defined(OS_IOS) | 19 #endif // defined(OS_IOS) |
| 20 | 20 |
| 21 #import <Foundation/Foundation.h> | 21 #import <Foundation/Foundation.h> |
| 22 | 22 |
| 23 namespace { | 23 namespace { |
| 24 // |kTestHashAddress| is the hash corresponding to identifier |kTestNSUUID|. | 24 // |kTestHashAddress| is the hash corresponding to identifier |kTestNSUUID|. |
| 25 NSString* const kTestNSUUID = @"00000000-1111-2222-3333-444444444444"; | 25 NSString* const kTestNSUUID = @"00000000-1111-2222-3333-444444444444"; |
| 26 const std::string kTestHashAddress = "D1:6F:E3:22:FD:5B"; | 26 const std::string kTestHashAddress = "D1:6F:E3:22:FD:5B"; |
| 27 const int kTestRssi = 0; |
| 27 } // namespace | 28 } // namespace |
| 28 | 29 |
| 29 namespace device { | 30 namespace device { |
| 30 | 31 |
| 31 class BluetoothAdapterMacTest : public testing::Test { | 32 class BluetoothAdapterMacTest : public testing::Test { |
| 32 public: | 33 public: |
| 33 BluetoothAdapterMacTest() | 34 BluetoothAdapterMacTest() |
| 34 : ui_task_runner_(new base::TestSimpleTaskRunner()), | 35 : ui_task_runner_(new base::TestSimpleTaskRunner()), |
| 35 adapter_(new BluetoothAdapterMac()), | 36 adapter_(new BluetoothAdapterMac()), |
| 36 adapter_mac_(static_cast<BluetoothAdapterMac*>(adapter_.get())), | 37 adapter_mac_(static_cast<BluetoothAdapterMac*>(adapter_.get())), |
| 37 callback_count_(0), | 38 callback_count_(0), |
| 38 error_callback_count_(0) { | 39 error_callback_count_(0) { |
| 39 adapter_mac_->InitForTest(ui_task_runner_); | 40 adapter_mac_->InitForTest(ui_task_runner_); |
| 40 } | 41 } |
| 41 | 42 |
| 42 // Helper methods for setup and access to BluetoothAdapterMacTest's members. | 43 // Helper methods for setup and access to BluetoothAdapterMacTest's members. |
| 43 void PollAdapter() { adapter_mac_->PollAdapter(); } | 44 void PollAdapter() { adapter_mac_->PollAdapter(); } |
| 44 | 45 |
| 46 void LowEnergyDeviceUpdated(CBPeripheral* peripheral, |
| 47 NSDictionary* advertisementData, |
| 48 int rssi) { |
| 49 adapter_mac_->LowEnergyDeviceUpdated(peripheral, advertisementData, rssi); |
| 50 } |
| 51 |
| 52 BluetoothDevice* GetDevice(const std::string& address) { |
| 53 return adapter_->GetDevice(address); |
| 54 } |
| 55 |
| 45 CBPeripheral* CreateMockPeripheral(NSString* identifier) { | 56 CBPeripheral* CreateMockPeripheral(NSString* identifier) { |
| 46 if (!BluetoothAdapterMac::IsLowEnergyAvailable()) { | 57 if (!BluetoothAdapterMac::IsLowEnergyAvailable()) { |
| 47 // For stability we only use CoreBluetooth on OS X >= 10.10. Thus on | 58 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
| 48 // previous OS X versions the code cannot be tested. | |
| 49 LOG(WARNING) << "OS X version < 10.10, skipping unit test."; | |
| 50 return nil; | 59 return nil; |
| 51 } | 60 } |
| 52 id mock_peripheral = | 61 id mock_peripheral = |
| 53 [[OCMockObject mockForClass:[CBPeripheral class]] retain]; | 62 [[OCMockObject mockForClass:[CBPeripheral class]] retain]; |
| 54 [((CBPeripheral*)[[mock_peripheral stub] | 63 [((CBPeripheral*)[[mock_peripheral stub] |
| 55 andReturnValue:@(CBPeripheralStateDisconnected)]) | 64 andReturnValue:@(CBPeripheralStateDisconnected)]) |
| 56 performSelector:@selector(state)]; | 65 performSelector:@selector(state)]; |
| 57 [[[mock_peripheral stub] andReturn:[NSString string]] name]; | 66 [[[mock_peripheral stub] andReturn:[NSString string]] name]; |
| 58 [[[mock_peripheral stub] | 67 [[[mock_peripheral stub] |
| 59 andReturn:[[NSUUID performSelector:@selector(UUID)] | 68 andReturn:[[NSUUID performSelector:@selector(UUID)] |
| (...skipping 172 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 232 EXPECT_EQ(0, [mock_central_manager_ stopScanCallCount]); | 241 EXPECT_EQ(0, [mock_central_manager_ stopScanCallCount]); |
| 233 } | 242 } |
| 234 | 243 |
| 235 TEST_F(BluetoothAdapterMacTest, CheckGetPeripheralHashAddress) { | 244 TEST_F(BluetoothAdapterMacTest, CheckGetPeripheralHashAddress) { |
| 236 base::scoped_nsobject<id> mock_peripheral(CreateMockPeripheral(kTestNSUUID)); | 245 base::scoped_nsobject<id> mock_peripheral(CreateMockPeripheral(kTestNSUUID)); |
| 237 if (mock_peripheral.get() == nil) | 246 if (mock_peripheral.get() == nil) |
| 238 return; | 247 return; |
| 239 EXPECT_EQ(kTestHashAddress, GetHashAddress(mock_peripheral)); | 248 EXPECT_EQ(kTestHashAddress, GetHashAddress(mock_peripheral)); |
| 240 } | 249 } |
| 241 | 250 |
| 251 TEST_F(BluetoothAdapterMacTest, LowEnergyDeviceUpdatedNewDevice) { |
| 252 base::scoped_nsobject<id> mock_peripheral(CreateMockPeripheral(kTestNSUUID)); |
| 253 if (mock_peripheral.get() == nil) |
| 254 return; |
| 255 base::scoped_nsobject<NSDictionary> advertisement_data( |
| 256 CreateAdvertisementData()); |
| 257 |
| 258 EXPECT_EQ(0, NumDevices()); |
| 259 EXPECT_FALSE(DevicePresent(mock_peripheral)); |
| 260 LowEnergyDeviceUpdated(mock_peripheral, advertisement_data, kTestRssi); |
| 261 EXPECT_EQ(1, NumDevices()); |
| 262 EXPECT_TRUE(DevicePresent(mock_peripheral)); |
| 263 } |
| 264 |
| 265 TEST_F(BluetoothAdapterMacTest, LowEnergyDeviceUpdatedOldDevice) { |
| 266 base::scoped_nsobject<id> mock_peripheral(CreateMockPeripheral(kTestNSUUID)); |
| 267 if (mock_peripheral.get() == nil) |
| 268 return; |
| 269 base::scoped_nsobject<NSDictionary> advertisement_data( |
| 270 CreateAdvertisementData()); |
| 271 |
| 272 // Update the device for the first time and check it was correctly added to |
| 273 // |devices_|. |
| 274 EXPECT_EQ(0, NumDevices()); |
| 275 EXPECT_FALSE(DevicePresent(mock_peripheral)); |
| 276 LowEnergyDeviceUpdated(mock_peripheral, advertisement_data, kTestRssi); |
| 277 EXPECT_EQ(1, NumDevices()); |
| 278 EXPECT_TRUE(DevicePresent(mock_peripheral)); |
| 279 // Search for the device by the address corresponding to |kTestNSUUID|. |
| 280 BluetoothDeviceMac* device = |
| 281 static_cast<BluetoothDeviceMac*>(GetDevice(kTestHashAddress)); |
| 282 base::scoped_nsobject<NSDate> first_update_time( |
| 283 [device->GetLastUpdateTime() retain]); |
| 284 |
| 285 // Update the device a second time. The device should be updated in |
| 286 // |devices_| so check the time returned by GetLastUpdateTime() has increased. |
| 287 LowEnergyDeviceUpdated(mock_peripheral, advertisement_data, kTestRssi); |
| 288 EXPECT_EQ(1, NumDevices()); |
| 289 EXPECT_TRUE(DevicePresent(mock_peripheral)); |
| 290 device = static_cast<BluetoothDeviceMac*>(GetDevice(kTestHashAddress)); |
| 291 EXPECT_TRUE([device->GetLastUpdateTime() compare:first_update_time] == |
| 292 NSOrderedDescending); |
| 293 } |
| 294 |
| 242 TEST_F(BluetoothAdapterMacTest, UpdateDevicesRemovesLowEnergyDevice) { | 295 TEST_F(BluetoothAdapterMacTest, UpdateDevicesRemovesLowEnergyDevice) { |
| 243 base::scoped_nsobject<id> mock_peripheral(CreateMockPeripheral(kTestNSUUID)); | 296 base::scoped_nsobject<id> mock_peripheral(CreateMockPeripheral(kTestNSUUID)); |
| 244 if (mock_peripheral.get() == nil) | 297 if (mock_peripheral.get() == nil) |
| 245 return; | 298 return; |
| 246 base::scoped_nsobject<NSDictionary> advertisement_data( | 299 base::scoped_nsobject<NSDictionary> advertisement_data( |
| 247 CreateAdvertisementData()); | 300 CreateAdvertisementData()); |
| 248 | 301 |
| 249 BluetoothLowEnergyDeviceMac* device = | 302 BluetoothLowEnergyDeviceMac* device = new BluetoothLowEnergyDeviceMac( |
| 250 new BluetoothLowEnergyDeviceMac(mock_peripheral, advertisement_data, 0); | 303 mock_peripheral, advertisement_data, kTestRssi); |
| 251 SetDeviceTimeGreaterThanTimeout(device); | 304 SetDeviceTimeGreaterThanTimeout(device); |
| 252 | 305 |
| 253 EXPECT_EQ(0, NumDevices()); | 306 EXPECT_EQ(0, NumDevices()); |
| 254 AddLowEnergyDevice(device); | 307 AddLowEnergyDevice(device); |
| 255 EXPECT_EQ(1, NumDevices()); | 308 EXPECT_EQ(1, NumDevices()); |
| 256 EXPECT_TRUE(DevicePresent(mock_peripheral)); | 309 EXPECT_TRUE(DevicePresent(mock_peripheral)); |
| 257 | 310 |
| 258 // Check that object pointed to by |device| is deleted by the adapter. | 311 // Check that object pointed to by |device| is deleted by the adapter. |
| 259 RemoveTimedOutDevices(); | 312 RemoveTimedOutDevices(); |
| 260 EXPECT_EQ(0, NumDevices()); | 313 EXPECT_EQ(0, NumDevices()); |
| 261 EXPECT_FALSE(DevicePresent(mock_peripheral)); | 314 EXPECT_FALSE(DevicePresent(mock_peripheral)); |
| 262 } | 315 } |
| 263 | 316 |
| 264 } // namespace device | 317 } // namespace device |
| OLD | NEW |