Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(819)

Side by Side Diff: device/bluetooth/bluetooth_adapter_mac_unittest.mm

Issue 1216583003: Adding Hashed Address to BluetoothLowEnergyDeviceMac (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@timeinfo
Patch Set: compiles on OS X 10.6 Created 5 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/test/mock_bluetooth_central_manager_mac.h" 11 #include "device/bluetooth/test/mock_bluetooth_central_manager_mac.h"
11 #include "testing/gtest/include/gtest/gtest.h" 12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "third_party/ocmock/OCMock/OCMock.h"
14
15 #if defined(OS_IOS)
16 #import <CoreBluetooth/CoreBluetooth.h>
17 #else // !defined(OS_IOS)
18 #import <IOBluetooth/IOBluetooth.h>
19 #endif // defined(OS_IOS)
20
21 #import <Foundation/Foundation.h>
22
23 namespace {
24 // |kTestHashAddress| is the hash corresponding to identifier |kTestNSUUID|.
25 NSString* const kTestNSUUID = @"00000000-1111-2222-3333-444444444444";
26 const std::string kTestHashAddress = "D1:6F:E3:22:FD:5B";
27 } // namespace
12 28
13 namespace device { 29 namespace device {
14 30
15 class BluetoothAdapterMacTest : public testing::Test { 31 class BluetoothAdapterMacTest : public testing::Test {
16 public: 32 public:
17 BluetoothAdapterMacTest() 33 BluetoothAdapterMacTest()
18 : ui_task_runner_(new base::TestSimpleTaskRunner()), 34 : ui_task_runner_(new base::TestSimpleTaskRunner()),
19 adapter_(new BluetoothAdapterMac()), 35 adapter_(new BluetoothAdapterMac()),
20 adapter_mac_(static_cast<BluetoothAdapterMac*>(adapter_.get())), 36 adapter_mac_(static_cast<BluetoothAdapterMac*>(adapter_.get())),
21 callback_count_(0), 37 callback_count_(0),
22 error_callback_count_(0) { 38 error_callback_count_(0) {
23 adapter_mac_->InitForTest(ui_task_runner_); 39 adapter_mac_->InitForTest(ui_task_runner_);
24 } 40 }
25 41
26 // Helper methods for setup and access to BluetoothAdapterMacTest's members. 42 // Helper methods for setup and access to BluetoothAdapterMacTest's members.
43 void PollAdapter() { adapter_mac_->PollAdapter(); }
44
45 CBPeripheral* CreateMockPeripheral(NSString* identifier) {
46 if (!BluetoothAdapterMac::IsLowEnergyAvailable()) {
47 // For stability we only use CoreBluetooth on OS X >= 10.10. Thus on
48 // previous OS X versions the code cannot be tested.
49 LOG(WARNING) << "OS X version < 10.10, skipping unit test.";
50 return nil;
51 }
52 Class peripheral_class = NSClassFromString(@"CBPeripheral");
53 id mock_peripheral =
54 [[OCMockObject mockForClass:[peripheral_class class]] retain];
55 [((CBPeripheral*)[[mock_peripheral stub]
Robert Sesek 2015/07/16 16:54:04 C-style casts are banned.
krstnmnlsn 2015/07/16 17:13:33 Done.
56 andReturnValue:@(CBPeripheralStateDisconnected)])
57 performSelector:@selector(state)];
58 [[[mock_peripheral stub] andReturn:[NSString string]] name];
59 Class uuid_class = NSClassFromString(@"NSUUID");
60 [[[mock_peripheral stub]
61 andReturn:[[uuid_class performSelector:@selector(UUID)]
62 performSelector:@selector(initWithUUIDString:)
63 withObject:identifier]] identifier];
64
65 return mock_peripheral;
66 }
67
68 NSDictionary* CreateAdvertisementData() {
69 NSDictionary* advertisement_data =
70 [[NSDictionary dictionaryWithObjectsAndKeys:
Robert Sesek 2015/07/16 16:54:04 Use @{} syntax here?
krstnmnlsn 2015/07/16 17:13:33 Sure
71 @(YES), @"CBAdvertisementDataIsConnectable",
72 [NSDictionary dictionary],
73 @"CBAdvertisementDataServiceDataKey", nil] retain];
74 return advertisement_data;
75 }
76
77 std::string GetHashAddress(CBPeripheral* peripheral) {
78 return BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral);
79 }
80
81 void SetDeviceTimeGreaterThanTimeout(BluetoothLowEnergyDeviceMac* device) {
82 device->last_update_time_.reset([[NSDate
83 dateWithTimeInterval:-(BluetoothAdapterMac::kDiscoveryTimeoutSec + 1)
84 sinceDate:[NSDate date]] retain]);
85 }
86
87 void AddLowEnergyDevice(BluetoothLowEnergyDeviceMac* device) {
88 adapter_mac_->devices_[device->GetAddress()] = device;
89 }
90
91 int NumDevices() { return adapter_mac_->devices_.size(); }
92
93 bool DevicePresent(CBPeripheral* peripheral) {
94 BluetoothDevice* device = adapter_mac_->GetDevice(
95 BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral));
96 return (device != NULL);
97 }
98
99 void RemoveTimedOutDevices() { adapter_mac_->RemoveTimedOutDevices(); }
100
27 bool SetMockCentralManager() { 101 bool SetMockCentralManager() {
28 if (!BluetoothAdapterMac::IsLowEnergyAvailable()) { 102 if (!BluetoothAdapterMac::IsLowEnergyAvailable()) {
29 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; 103 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
30 return false; 104 return false;
31 } 105 }
32 mock_central_manager_ = [[MockCentralManager alloc] init]; 106 mock_central_manager_ = [[MockCentralManager alloc] init];
33 adapter_mac_->low_energy_discovery_manager_->SetManagerForTesting( 107 adapter_mac_->low_energy_discovery_manager_->SetManagerForTesting(
34 mock_central_manager_); 108 mock_central_manager_);
35 return true; 109 return true;
36 } 110 }
(...skipping 26 matching lines...) Expand all
63 BluetoothAdapterMac* adapter_mac_; 137 BluetoothAdapterMac* adapter_mac_;
64 138
65 // Owned by |low_energy_discovery_manager_| on |adapter_mac_|. 139 // Owned by |low_energy_discovery_manager_| on |adapter_mac_|.
66 id mock_central_manager_ = NULL; 140 id mock_central_manager_ = NULL;
67 141
68 int callback_count_; 142 int callback_count_;
69 int error_callback_count_; 143 int error_callback_count_;
70 }; 144 };
71 145
72 TEST_F(BluetoothAdapterMacTest, Poll) { 146 TEST_F(BluetoothAdapterMacTest, Poll) {
147 PollAdapter();
73 EXPECT_FALSE(ui_task_runner_->GetPendingTasks().empty()); 148 EXPECT_FALSE(ui_task_runner_->GetPendingTasks().empty());
74 } 149 }
75 150
76 TEST_F(BluetoothAdapterMacTest, AddDiscoverySessionWithLowEnergyFilter) { 151 TEST_F(BluetoothAdapterMacTest, AddDiscoverySessionWithLowEnergyFilter) {
77 if (!SetMockCentralManager()) 152 if (!SetMockCentralManager())
78 return; 153 return;
79 EXPECT_EQ(0, [mock_central_manager_ scanForPeripheralsCallCount]); 154 EXPECT_EQ(0, [mock_central_manager_ scanForPeripheralsCallCount]);
80 EXPECT_EQ(0, NumDiscoverySessions()); 155 EXPECT_EQ(0, NumDiscoverySessions());
81 156
82 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter( 157 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter(
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
152 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE)); 227 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE));
153 RemoveDiscoverySession(discovery_filter.get()); 228 RemoveDiscoverySession(discovery_filter.get());
154 EXPECT_EQ(0, callback_count_); 229 EXPECT_EQ(0, callback_count_);
155 EXPECT_EQ(1, error_callback_count_); 230 EXPECT_EQ(1, error_callback_count_);
156 EXPECT_EQ(0, NumDiscoverySessions()); 231 EXPECT_EQ(0, NumDiscoverySessions());
157 232
158 // Check that stopScan was not called. 233 // Check that stopScan was not called.
159 EXPECT_EQ(0, [mock_central_manager_ stopScanCallCount]); 234 EXPECT_EQ(0, [mock_central_manager_ stopScanCallCount]);
160 } 235 }
161 236
237 TEST_F(BluetoothAdapterMacTest, CheckGetPeripheralHashAddress) {
238 base::scoped_nsobject<id> mock_peripheral(CreateMockPeripheral(kTestNSUUID));
239 if (mock_peripheral.get() == nil)
240 return;
241 EXPECT_EQ(kTestHashAddress, GetHashAddress(mock_peripheral));
242 }
243
244 TEST_F(BluetoothAdapterMacTest, UpdateDevicesRemovesLowEnergyDevice) {
245 base::scoped_nsobject<id> mock_peripheral(CreateMockPeripheral(kTestNSUUID));
246 if (mock_peripheral.get() == nil)
247 return;
248 base::scoped_nsobject<NSDictionary> advertisement_data(
249 CreateAdvertisementData());
250
251 BluetoothLowEnergyDeviceMac* device =
252 new BluetoothLowEnergyDeviceMac(mock_peripheral, advertisement_data, 0);
253 SetDeviceTimeGreaterThanTimeout(device);
254
255 EXPECT_EQ(0, NumDevices());
256 AddLowEnergyDevice(device);
257 EXPECT_EQ(1, NumDevices());
258 EXPECT_TRUE(DevicePresent(mock_peripheral));
259
260 // Check that object pointed to by |device| is deleted by the adapter.
261 RemoveTimedOutDevices();
262 EXPECT_EQ(0, NumDevices());
263 EXPECT_FALSE(DevicePresent(mock_peripheral));
264 }
265
162 } // namespace device 266 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698