Chromium Code Reviews| Index: device/bluetooth/bluetooth_adapter_mac_unittest.mm |
| diff --git a/device/bluetooth/bluetooth_adapter_mac_unittest.mm b/device/bluetooth/bluetooth_adapter_mac_unittest.mm |
| index 3e7ab6832ede2641ba91284e7597cdc009d3560b..d68e6eff115a878afdf6379f776a997087a331f7 100644 |
| --- a/device/bluetooth/bluetooth_adapter_mac_unittest.mm |
| +++ b/device/bluetooth/bluetooth_adapter_mac_unittest.mm |
| @@ -2,13 +2,30 @@ |
| // Use of this source code is governed by a BSD-style license that can be |
| // found in the LICENSE file. |
| +#include "base/mac/sdk_forward_declarations.h" |
| #include "base/memory/ref_counted.h" |
| #include "base/test/test_simple_task_runner.h" |
| #include "device/bluetooth/bluetooth_adapter.h" |
| #include "device/bluetooth/bluetooth_adapter_mac.h" |
| #include "device/bluetooth/bluetooth_discovery_session.h" |
| +#include "device/bluetooth/bluetooth_low_energy_device_mac.h" |
| #include "device/bluetooth/test/mock_bluetooth_central_manager_mac.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| +#include "third_party/ocmock/OCMock/OCMock.h" |
| + |
| +#if defined(OS_IOS) |
| +#import <CoreBluetooth/CoreBluetooth.h> |
| +#else |
|
armansito
2015/07/07 19:07:21
#else // !defined(OS_IOS)
krstnmnlsn
2015/07/07 21:58:01
Done.
|
| +#import <IOBluetooth/IOBluetooth.h> |
| +#endif |
|
armansito
2015/07/07 19:07:21
#endif // defined(OS_IOS)
krstnmnlsn
2015/07/07 21:58:01
Done. Also fixed in bluetooth_low_energy_device.h
|
| + |
| +#import <Foundation/Foundation.h> |
| + |
| +namespace { |
| +// |kTestHashAddress| is the hash corresponding to identifier |kTestNSUUID|. |
| +NSString* const kTestNSUUID = @"00000000-1111-2222-3333-444444444444"; |
| +const std::string kTestHashAddress = "D1:6F:E3:22:FD:5B"; |
| +} // namespace |
| namespace device { |
| @@ -24,6 +41,63 @@ class BluetoothAdapterMacTest : public testing::Test { |
| } |
| // Helper methods for setup and access to BluetoothAdapterMacTest's members. |
| + void PollAdapter() { adapter_mac_->PollAdapter(); } |
| + |
| + CBPeripheral* CreateMockPeripheral(NSString* identifier) { |
| + Class aClass = NSClassFromString(@"CBPeripheral"); |
| + if (aClass == nil) { |
| + LOG(WARNING) << "CoreBluetooth not available, skipping unit test."; |
| + return nil; |
| + } |
| + id mock_peripheral = |
| + [[OCMockObject mockForClass:[CBPeripheral class]] retain]; |
| + // isConnected deprecated on OSX SDK >= 10.9 and so BluetoothLowEnergyDevice |
| + // sends the state message instead. We stub accordingly. |
| + if ([CBPeripheral instancesRespondToSelector:@selector(state)]) |
| + [((CBPeripheral*)[[mock_peripheral stub] |
| + andReturnValue:@(CBPeripheralStateDisconnected)])state]; |
| + else |
| + [[[mock_peripheral stub] andReturnValue:@NO] isConnected]; |
| + [[[mock_peripheral stub] andReturn:[NSString string]] name]; |
| + [[[mock_peripheral stub] |
| + andReturn:[[NSUUID UUID] initWithUUIDString:identifier]] identifier]; |
| + |
| + return mock_peripheral; |
| + } |
| + |
| + NSDictionary* CreateAdvertisementData() { |
| + NSDictionary* advertisement_data = [[NSDictionary |
| + dictionaryWithObjectsAndKeys:@YES, @"CBAdvertisementDataIsConnectable", |
| + [NSDictionary dictionary], |
| + @"CBAdvertisementDataServiceDataKey", |
| + nil] retain]; |
| + return advertisement_data; |
| + } |
| + |
| + std::string GetHashAddress(CBPeripheral* peripheral) { |
| + return BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral); |
| + } |
| + |
| + void SetDeviceTimeGreaterThanTimeout(BluetoothLowEnergyDeviceMac* device) { |
| + device->last_update_time_.reset([[NSDate |
| + dateWithTimeInterval:-(BluetoothAdapterMac::kDiscoveryTimeoutSec + 1) |
| + sinceDate:[NSDate date]] retain]); |
| + } |
| + |
| + void AddLowEnergyDevice(BluetoothLowEnergyDeviceMac* device) { |
| + adapter_mac_->devices_[device->GetAddress()] = device; |
| + } |
| + |
| + int NumDevices() { return adapter_mac_->devices_.size(); } |
| + |
| + bool DevicePresent(CBPeripheral* peripheral) { |
| + BluetoothDevice* device = adapter_mac_->GetDevice( |
| + BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral)); |
| + return (device != NULL); |
| + } |
| + |
| + void RemoveTimedOutDevices() { adapter_mac_->RemoveTimedOutDevices(); } |
| + |
| bool SetMockCentralManager() { |
| Class aClass = NSClassFromString(@"CBCentralManager"); |
| if (aClass == nil) { |
| @@ -71,6 +145,7 @@ class BluetoothAdapterMacTest : public testing::Test { |
| }; |
| TEST_F(BluetoothAdapterMacTest, Poll) { |
| + PollAdapter(); |
| EXPECT_FALSE(ui_task_runner_->GetPendingTasks().empty()); |
| } |
| @@ -160,4 +235,33 @@ TEST_F(BluetoothAdapterMacTest, RemoveDiscoverySessionWithLowEnergyFilterFail) { |
| EXPECT_EQ(0, [mock_central_manager_ stopScanCallCount]); |
| } |
| +TEST_F(BluetoothAdapterMacTest, CheckGetPeripheralHashAddress) { |
| + base::scoped_nsobject<id> mock_peripheral(CreateMockPeripheral(kTestNSUUID)); |
| + if (mock_peripheral.get() == nil) |
| + return; |
| + EXPECT_EQ(kTestHashAddress, GetHashAddress(mock_peripheral)); |
| +} |
| + |
| +TEST_F(BluetoothAdapterMacTest, UpdateDevicesRemovesLowEnergyDevice) { |
| + base::scoped_nsobject<id> mock_peripheral(CreateMockPeripheral(kTestNSUUID)); |
| + if (mock_peripheral.get() == nil) |
| + return; |
| + base::scoped_nsobject<NSDictionary> advertisement_data( |
| + CreateAdvertisementData()); |
| + |
| + BluetoothLowEnergyDeviceMac* device = |
| + new BluetoothLowEnergyDeviceMac(mock_peripheral, advertisement_data, 0); |
| + SetDeviceTimeGreaterThanTimeout(device); |
| + |
| + EXPECT_EQ(0, NumDevices()); |
| + AddLowEnergyDevice(device); |
| + EXPECT_EQ(1, NumDevices()); |
| + EXPECT_TRUE(DevicePresent(mock_peripheral)); |
| + |
| + // Check that object pointed to by |device| is deleted by the adapter. |
| + RemoveTimedOutDevices(); |
| + EXPECT_EQ(0, NumDevices()); |
| + EXPECT_FALSE(DevicePresent(mock_peripheral)); |
| +} |
| + |
| } // namespace device |