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..de467a084ab72e87486d9539b9090d9272c8fc29 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 |
| +#import <IOBluetooth/IOBluetooth.h> |
| +#endif |
| + |
| +#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,61 @@ 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]]; |
| + // TODO(krstnmnlsn): Remove check once we move to OSX SDK >= 10.9. |
|
scheib
2015/07/01 17:29:44
Shift this comment into explaining that the mock i
krstnmnlsn
2015/07/02 00:25:30
Done.
|
| + 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 = @{ |
| + @"CBAdvertisementDataIsConnectable" : @YES, |
| + @"CBAdvertisementDataServiceDataKey" : [NSDictionary dictionary], |
| + }; |
| + return advertisement_data; |
| + } |
| + |
| + std::string GetHashAddress(CBPeripheral* peripheral) { |
| + return BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral); |
| + } |
| + |
| + void SetDeviceTimeGreaterThanTimeout(BluetoothLowEnergyDeviceMac* device) { |
| + device->last_update_time = [NSDate |
| + dateWithTimeInterval:-(BluetoothAdapterMac::kDiscoveryTimeoutSec + 1) |
| + sinceDate:[NSDate date]]; |
| + } |
| + |
| + void AddLowEnergyDevice(std::string address, |
| + BluetoothLowEnergyDeviceMac* device) { |
| + adapter_mac_->devices_[address] = 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 +143,7 @@ class BluetoothAdapterMacTest : public testing::Test { |
| }; |
| TEST_F(BluetoothAdapterMacTest, Poll) { |
| + PollAdapter(); |
| EXPECT_FALSE(ui_task_runner_->GetPendingTasks().empty()); |
| } |
| @@ -160,4 +233,31 @@ TEST_F(BluetoothAdapterMacTest, RemoveDiscoverySessionWithLowEnergyFilterFail) { |
| EXPECT_EQ(0, [mock_central_manager_ stopScanCallCount]); |
| } |
| +TEST_F(BluetoothAdapterMacTest, CheckGetPeripheralHashAddress) { |
| + id mock_peripheral = CreateMockPeripheral(kTestNSUUID); |
| + if (mock_peripheral == nil) |
| + return; |
| + EXPECT_EQ(kTestHashAddress, GetHashAddress(mock_peripheral)); |
| +} |
| + |
| +TEST_F(BluetoothAdapterMacTest, UpdateDevicesRemovesLowEnergyDevice) { |
| + id mock_peripheral = CreateMockPeripheral(kTestNSUUID); |
| + if (mock_peripheral == nil) |
| + return; |
| + NSDictionary* advertisement_data = CreateAdvertisementData(); |
| + BluetoothLowEnergyDeviceMac* device = |
| + new BluetoothLowEnergyDeviceMac(mock_peripheral, advertisement_data, 0); |
| + SetDeviceTimeGreaterThanTimeout(device); |
| + |
| + EXPECT_EQ(0, NumDevices()); |
| + AddLowEnergyDevice(GetHashAddress(mock_peripheral), device); |
| + EXPECT_EQ(1, NumDevices()); |
| + EXPECT_EQ(true, DevicePresent(mock_peripheral)); |
| + |
| + // Check that object pointed to by |device| is deleted by the adapter. |
| + RemoveTimedOutDevices(); |
| + EXPECT_EQ(0, NumDevices()); |
| + EXPECT_EQ(false, DevicePresent(mock_peripheral)); |
| +} |
| + |
| } // namespace device |