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

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: 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
19 #import <IOBluetooth/IOBluetooth.h>
20 #endif
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 = [OCMockObject mockForClass:[CBPeripheral class]];
53 // 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.
54 if ([CBPeripheral instancesRespondToSelector:@selector(state)])
55 [((CBPeripheral*)[[mock_peripheral stub]
56 andReturnValue:@(CBPeripheralStateDisconnected)])state];
57 else
58 [[[mock_peripheral stub] andReturnValue:@NO] isConnected];
59 [[[mock_peripheral stub] andReturn:[NSString string]] name];
60 [[[mock_peripheral stub]
61 andReturn:[[NSUUID UUID] initWithUUIDString:identifier]] identifier];
62
63 return mock_peripheral;
64 }
65
66 NSDictionary* CreateAdvertisementData() {
67 NSDictionary* advertisement_data = @{
68 @"CBAdvertisementDataIsConnectable" : @YES,
69 @"CBAdvertisementDataServiceDataKey" : [NSDictionary dictionary],
70 };
71 return advertisement_data;
72 }
73
74 std::string GetHashAddress(CBPeripheral* peripheral) {
75 return BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral);
76 }
77
78 void SetDeviceTimeGreaterThanTimeout(BluetoothLowEnergyDeviceMac* device) {
79 device->last_update_time = [NSDate
80 dateWithTimeInterval:-(BluetoothAdapterMac::kDiscoveryTimeoutSec + 1)
81 sinceDate:[NSDate date]];
82 }
83
84 void AddLowEnergyDevice(std::string address,
85 BluetoothLowEnergyDeviceMac* device) {
86 adapter_mac_->devices_[address] = device;
87 }
88
89 int NumDevices() { return adapter_mac_->devices_.size(); }
90
91 bool DevicePresent(CBPeripheral* peripheral) {
92 BluetoothDevice* device = adapter_mac_->GetDevice(
93 BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral));
94 return (device != NULL);
95 }
96
97 void RemoveTimedOutDevices() { adapter_mac_->RemoveTimedOutDevices(); }
98
27 bool SetMockCentralManager() { 99 bool SetMockCentralManager() {
28 Class aClass = NSClassFromString(@"CBCentralManager"); 100 Class aClass = NSClassFromString(@"CBCentralManager");
29 if (aClass == nil) { 101 if (aClass == nil) {
30 LOG(WARNING) << "CoreBluetooth not available, skipping unit test."; 102 LOG(WARNING) << "CoreBluetooth not available, skipping unit test.";
31 return false; 103 return false;
32 } 104 }
33 mock_central_manager_ = [[MockCentralManager alloc] init]; 105 mock_central_manager_ = [[MockCentralManager alloc] init];
34 adapter_mac_->low_energy_discovery_manager_->SetManagerForTesting( 106 adapter_mac_->low_energy_discovery_manager_->SetManagerForTesting(
35 mock_central_manager_); 107 mock_central_manager_);
36 return true; 108 return true;
(...skipping 27 matching lines...) Expand all
64 BluetoothAdapterMac* adapter_mac_; 136 BluetoothAdapterMac* adapter_mac_;
65 137
66 // Owned by |low_energy_discovery_manager_| on |adapter_mac_|. 138 // Owned by |low_energy_discovery_manager_| on |adapter_mac_|.
67 id mock_central_manager_ = NULL; 139 id mock_central_manager_ = NULL;
68 140
69 int callback_count_; 141 int callback_count_;
70 int error_callback_count_; 142 int error_callback_count_;
71 }; 143 };
72 144
73 TEST_F(BluetoothAdapterMacTest, Poll) { 145 TEST_F(BluetoothAdapterMacTest, Poll) {
146 PollAdapter();
74 EXPECT_FALSE(ui_task_runner_->GetPendingTasks().empty()); 147 EXPECT_FALSE(ui_task_runner_->GetPendingTasks().empty());
75 } 148 }
76 149
77 TEST_F(BluetoothAdapterMacTest, AddDiscoverySessionWithLowEnergyFilter) { 150 TEST_F(BluetoothAdapterMacTest, AddDiscoverySessionWithLowEnergyFilter) {
78 if (!SetMockCentralManager()) 151 if (!SetMockCentralManager())
79 return; 152 return;
80 EXPECT_EQ(0, [mock_central_manager_ scanForPeripheralsCallCount]); 153 EXPECT_EQ(0, [mock_central_manager_ scanForPeripheralsCallCount]);
81 EXPECT_EQ(0, NumDiscoverySessions()); 154 EXPECT_EQ(0, NumDiscoverySessions());
82 155
83 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter( 156 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter(
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
153 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE)); 226 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE));
154 RemoveDiscoverySession(discovery_filter.get()); 227 RemoveDiscoverySession(discovery_filter.get());
155 EXPECT_EQ(0, callback_count_); 228 EXPECT_EQ(0, callback_count_);
156 EXPECT_EQ(1, error_callback_count_); 229 EXPECT_EQ(1, error_callback_count_);
157 EXPECT_EQ(0, NumDiscoverySessions()); 230 EXPECT_EQ(0, NumDiscoverySessions());
158 231
159 // Check that stopScan was not called. 232 // Check that stopScan was not called.
160 EXPECT_EQ(0, [mock_central_manager_ stopScanCallCount]); 233 EXPECT_EQ(0, [mock_central_manager_ stopScanCallCount]);
161 } 234 }
162 235
236 TEST_F(BluetoothAdapterMacTest, CheckGetPeripheralHashAddress) {
237 id mock_peripheral = CreateMockPeripheral(kTestNSUUID);
238 if (mock_peripheral == nil)
239 return;
240 EXPECT_EQ(kTestHashAddress, GetHashAddress(mock_peripheral));
241 }
242
243 TEST_F(BluetoothAdapterMacTest, UpdateDevicesRemovesLowEnergyDevice) {
244 id mock_peripheral = CreateMockPeripheral(kTestNSUUID);
245 if (mock_peripheral == nil)
246 return;
247 NSDictionary* advertisement_data = CreateAdvertisementData();
248 BluetoothLowEnergyDeviceMac* device =
249 new BluetoothLowEnergyDeviceMac(mock_peripheral, advertisement_data, 0);
250 SetDeviceTimeGreaterThanTimeout(device);
251
252 EXPECT_EQ(0, NumDevices());
253 AddLowEnergyDevice(GetHashAddress(mock_peripheral), device);
254 EXPECT_EQ(1, NumDevices());
255 EXPECT_EQ(true, DevicePresent(mock_peripheral));
256
257 // Check that object pointed to by |device| is deleted by the adapter.
258 RemoveTimedOutDevices();
259 EXPECT_EQ(0, NumDevices());
260 EXPECT_EQ(false, DevicePresent(mock_peripheral));
261 }
262
163 } // namespace device 263 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698