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