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

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: shortening a signature.. 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/mac/sdk_forward_declarations.h"
5 #include "base/memory/ref_counted.h" 6 #include "base/memory/ref_counted.h"
6 #include "base/test/test_simple_task_runner.h" 7 #include "base/test/test_simple_task_runner.h"
7 #include "device/bluetooth/bluetooth_adapter.h" 8 #include "device/bluetooth/bluetooth_adapter.h"
8 #include "device/bluetooth/bluetooth_adapter_mac.h" 9 #include "device/bluetooth/bluetooth_adapter_mac.h"
9 #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"
10 #include "device/bluetooth/test/mock_bluetooth_central_manager_mac.h" 12 #include "device/bluetooth/test/mock_bluetooth_central_manager_mac.h"
11 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "third_party/ocmock/OCMock/OCMock.h"
15
16 #if defined(OS_IOS)
17 #import <CoreBluetooth/CoreBluetooth.h>
18 #else
armansito 2015/07/07 19:07:21 #else // !defined(OS_IOS)
krstnmnlsn 2015/07/07 21:58:01 Done.
19 #import <IOBluetooth/IOBluetooth.h>
20 #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
21
22 #import <Foundation/Foundation.h>
23
24 namespace {
25 // |kTestHashAddress| is the hash corresponding to identifier |kTestNSUUID|.
26 NSString* const kTestNSUUID = @"00000000-1111-2222-3333-444444444444";
27 const std::string kTestHashAddress = "D1:6F:E3:22:FD:5B";
28 } // namespace
12 29
13 namespace device { 30 namespace device {
14 31
15 class BluetoothAdapterMacTest : public testing::Test { 32 class BluetoothAdapterMacTest : public testing::Test {
16 public: 33 public:
17 BluetoothAdapterMacTest() 34 BluetoothAdapterMacTest()
18 : ui_task_runner_(new base::TestSimpleTaskRunner()), 35 : ui_task_runner_(new base::TestSimpleTaskRunner()),
19 adapter_(new BluetoothAdapterMac()), 36 adapter_(new BluetoothAdapterMac()),
20 adapter_mac_(static_cast<BluetoothAdapterMac*>(adapter_.get())), 37 adapter_mac_(static_cast<BluetoothAdapterMac*>(adapter_.get())),
21 callback_count_(0), 38 callback_count_(0),
22 error_callback_count_(0) { 39 error_callback_count_(0) {
23 adapter_mac_->InitForTest(ui_task_runner_); 40 adapter_mac_->InitForTest(ui_task_runner_);
24 } 41 }
25 42
26 // Helper methods for setup and access to BluetoothAdapterMacTest's members. 43 // Helper methods for setup and access to BluetoothAdapterMacTest's members.
44 void PollAdapter() { adapter_mac_->PollAdapter(); }
45
46 CBPeripheral* CreateMockPeripheral(NSString* identifier) {
47 Class aClass = NSClassFromString(@"CBPeripheral");
48 if (aClass == nil) {
49 LOG(WARNING) << "CoreBluetooth not available, skipping unit test.";
50 return nil;
51 }
52 id mock_peripheral =
53 [[OCMockObject mockForClass:[CBPeripheral class]] retain];
54 // isConnected deprecated on OSX SDK >= 10.9 and so BluetoothLowEnergyDevice
55 // sends the state message instead. We stub accordingly.
56 if ([CBPeripheral instancesRespondToSelector:@selector(state)])
57 [((CBPeripheral*)[[mock_peripheral stub]
58 andReturnValue:@(CBPeripheralStateDisconnected)])state];
59 else
60 [[[mock_peripheral stub] andReturnValue:@NO] isConnected];
61 [[[mock_peripheral stub] andReturn:[NSString string]] name];
62 [[[mock_peripheral stub]
63 andReturn:[[NSUUID UUID] initWithUUIDString:identifier]] identifier];
64
65 return mock_peripheral;
66 }
67
68 NSDictionary* CreateAdvertisementData() {
69 NSDictionary* advertisement_data = [[NSDictionary
70 dictionaryWithObjectsAndKeys:@YES, @"CBAdvertisementDataIsConnectable",
71 [NSDictionary dictionary],
72 @"CBAdvertisementDataServiceDataKey",
73 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 Class aClass = NSClassFromString(@"CBCentralManager"); 102 Class aClass = NSClassFromString(@"CBCentralManager");
29 if (aClass == nil) { 103 if (aClass == nil) {
30 LOG(WARNING) << "CoreBluetooth not available, skipping unit test."; 104 LOG(WARNING) << "CoreBluetooth not available, skipping unit test.";
31 return false; 105 return false;
32 } 106 }
33 mock_central_manager_ = [[MockCentralManager alloc] init]; 107 mock_central_manager_ = [[MockCentralManager alloc] init];
34 adapter_mac_->low_energy_discovery_manager_->SetManagerForTesting( 108 adapter_mac_->low_energy_discovery_manager_->SetManagerForTesting(
35 mock_central_manager_); 109 mock_central_manager_);
36 return true; 110 return true;
(...skipping 27 matching lines...) Expand all
64 BluetoothAdapterMac* adapter_mac_; 138 BluetoothAdapterMac* adapter_mac_;
65 139
66 // Owned by |low_energy_discovery_manager_| on |adapter_mac_|. 140 // Owned by |low_energy_discovery_manager_| on |adapter_mac_|.
67 id mock_central_manager_ = NULL; 141 id mock_central_manager_ = NULL;
68 142
69 int callback_count_; 143 int callback_count_;
70 int error_callback_count_; 144 int error_callback_count_;
71 }; 145 };
72 146
73 TEST_F(BluetoothAdapterMacTest, Poll) { 147 TEST_F(BluetoothAdapterMacTest, Poll) {
148 PollAdapter();
74 EXPECT_FALSE(ui_task_runner_->GetPendingTasks().empty()); 149 EXPECT_FALSE(ui_task_runner_->GetPendingTasks().empty());
75 } 150 }
76 151
77 TEST_F(BluetoothAdapterMacTest, AddDiscoverySessionWithLowEnergyFilter) { 152 TEST_F(BluetoothAdapterMacTest, AddDiscoverySessionWithLowEnergyFilter) {
78 if (!SetMockCentralManager()) 153 if (!SetMockCentralManager())
79 return; 154 return;
80 EXPECT_EQ(0, [mock_central_manager_ scanForPeripheralsCallCount]); 155 EXPECT_EQ(0, [mock_central_manager_ scanForPeripheralsCallCount]);
81 EXPECT_EQ(0, NumDiscoverySessions()); 156 EXPECT_EQ(0, NumDiscoverySessions());
82 157
83 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter( 158 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter(
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
153 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE)); 228 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE));
154 RemoveDiscoverySession(discovery_filter.get()); 229 RemoveDiscoverySession(discovery_filter.get());
155 EXPECT_EQ(0, callback_count_); 230 EXPECT_EQ(0, callback_count_);
156 EXPECT_EQ(1, error_callback_count_); 231 EXPECT_EQ(1, error_callback_count_);
157 EXPECT_EQ(0, NumDiscoverySessions()); 232 EXPECT_EQ(0, NumDiscoverySessions());
158 233
159 // Check that stopScan was not called. 234 // Check that stopScan was not called.
160 EXPECT_EQ(0, [mock_central_manager_ stopScanCallCount]); 235 EXPECT_EQ(0, [mock_central_manager_ stopScanCallCount]);
161 } 236 }
162 237
238 TEST_F(BluetoothAdapterMacTest, CheckGetPeripheralHashAddress) {
239 base::scoped_nsobject<id> mock_peripheral(CreateMockPeripheral(kTestNSUUID));
240 if (mock_peripheral.get() == nil)
241 return;
242 EXPECT_EQ(kTestHashAddress, GetHashAddress(mock_peripheral));
243 }
244
245 TEST_F(BluetoothAdapterMacTest, UpdateDevicesRemovesLowEnergyDevice) {
246 base::scoped_nsobject<id> mock_peripheral(CreateMockPeripheral(kTestNSUUID));
247 if (mock_peripheral.get() == nil)
248 return;
249 base::scoped_nsobject<NSDictionary> advertisement_data(
250 CreateAdvertisementData());
251
252 BluetoothLowEnergyDeviceMac* device =
253 new BluetoothLowEnergyDeviceMac(mock_peripheral, advertisement_data, 0);
254 SetDeviceTimeGreaterThanTimeout(device);
255
256 EXPECT_EQ(0, NumDevices());
257 AddLowEnergyDevice(device);
258 EXPECT_EQ(1, NumDevices());
259 EXPECT_TRUE(DevicePresent(mock_peripheral));
260
261 // Check that object pointed to by |device| is deleted by the adapter.
262 RemoveTimedOutDevices();
263 EXPECT_EQ(0, NumDevices());
264 EXPECT_FALSE(DevicePresent(mock_peripheral));
265 }
266
163 } // namespace device 267 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698