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

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

Issue 1229473005: BluetoothAdapterMac::LowEnergyDeviceUpdated() Implemented (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@hash
Patch Set: s/advertisementData/advertisement_data 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
« no previous file with comments | « device/bluetooth/bluetooth_adapter_mac.mm ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/bluetooth_low_energy_device_mac.h"
11 #include "device/bluetooth/test/mock_bluetooth_central_manager_mac.h" 11 #include "device/bluetooth/test/mock_bluetooth_central_manager_mac.h"
12 #include "testing/gtest/include/gtest/gtest.h" 12 #include "testing/gtest/include/gtest/gtest.h"
13 #include "third_party/ocmock/OCMock/OCMock.h" 13 #include "third_party/ocmock/OCMock/OCMock.h"
14 14
15 #if defined(OS_IOS) 15 #if defined(OS_IOS)
16 #import <CoreBluetooth/CoreBluetooth.h> 16 #import <CoreBluetooth/CoreBluetooth.h>
17 #else // !defined(OS_IOS) 17 #else // !defined(OS_IOS)
18 #import <IOBluetooth/IOBluetooth.h> 18 #import <IOBluetooth/IOBluetooth.h>
19 #endif // defined(OS_IOS) 19 #endif // defined(OS_IOS)
20 20
21 #import <Foundation/Foundation.h> 21 #import <Foundation/Foundation.h>
22 22
23 namespace { 23 namespace {
24 // |kTestHashAddress| is the hash corresponding to identifier |kTestNSUUID|. 24 // |kTestHashAddress| is the hash corresponding to identifier |kTestNSUUID|.
25 NSString* const kTestNSUUID = @"00000000-1111-2222-3333-444444444444"; 25 NSString* const kTestNSUUID = @"00000000-1111-2222-3333-444444444444";
26 const std::string kTestHashAddress = "D1:6F:E3:22:FD:5B"; 26 const std::string kTestHashAddress = "D1:6F:E3:22:FD:5B";
27 const int kTestRssi = 0;
27 } // namespace 28 } // namespace
28 29
29 namespace device { 30 namespace device {
30 31
31 class BluetoothAdapterMacTest : public testing::Test { 32 class BluetoothAdapterMacTest : public testing::Test {
32 public: 33 public:
33 BluetoothAdapterMacTest() 34 BluetoothAdapterMacTest()
34 : ui_task_runner_(new base::TestSimpleTaskRunner()), 35 : ui_task_runner_(new base::TestSimpleTaskRunner()),
35 adapter_(new BluetoothAdapterMac()), 36 adapter_(new BluetoothAdapterMac()),
36 adapter_mac_(static_cast<BluetoothAdapterMac*>(adapter_.get())), 37 adapter_mac_(static_cast<BluetoothAdapterMac*>(adapter_.get())),
37 callback_count_(0), 38 callback_count_(0),
38 error_callback_count_(0) { 39 error_callback_count_(0) {
39 adapter_mac_->InitForTest(ui_task_runner_); 40 adapter_mac_->InitForTest(ui_task_runner_);
40 } 41 }
41 42
42 // Helper methods for setup and access to BluetoothAdapterMacTest's members. 43 // Helper methods for setup and access to BluetoothAdapterMacTest's members.
43 void PollAdapter() { adapter_mac_->PollAdapter(); } 44 void PollAdapter() { adapter_mac_->PollAdapter(); }
44 45
46 void LowEnergyDeviceUpdated(CBPeripheral* peripheral,
47 NSDictionary* advertisement_data,
48 int rssi) {
49 adapter_mac_->LowEnergyDeviceUpdated(peripheral, advertisement_data, rssi);
50 }
51
52 BluetoothDevice* GetDevice(const std::string& address) {
53 return adapter_->GetDevice(address);
54 }
55
45 CBPeripheral* CreateMockPeripheral(NSString* identifier) { 56 CBPeripheral* CreateMockPeripheral(NSString* identifier) {
46 if (!BluetoothAdapterMac::IsLowEnergyAvailable()) { 57 if (!BluetoothAdapterMac::IsLowEnergyAvailable()) {
47 // For stability we only use CoreBluetooth on OS X >= 10.10. Thus on 58 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
48 // previous OS X versions the code cannot be tested.
49 LOG(WARNING) << "OS X version < 10.10, skipping unit test.";
50 return nil; 59 return nil;
51 } 60 }
52 Class peripheral_class = NSClassFromString(@"CBPeripheral"); 61 Class peripheral_class = NSClassFromString(@"CBPeripheral");
53 id mock_peripheral = 62 id mock_peripheral =
54 [[OCMockObject mockForClass:[peripheral_class class]] retain]; 63 [[OCMockObject mockForClass:[peripheral_class class]] retain];
55 [static_cast<CBPeripheral*>([[mock_peripheral stub] 64 [static_cast<CBPeripheral*>([[mock_peripheral stub]
56 andReturnValue:@(CBPeripheralStateDisconnected)]) 65 andReturnValue:@(CBPeripheralStateDisconnected)])
57 performSelector:@selector(state)]; 66 performSelector:@selector(state)];
58 [[[mock_peripheral stub] andReturn:[NSString string]] name]; 67 [[[mock_peripheral stub] andReturn:[NSString string]] name];
59 Class uuid_class = NSClassFromString(@"NSUUID"); 68 Class uuid_class = NSClassFromString(@"NSUUID");
(...skipping 174 matching lines...) Expand 10 before | Expand all | Expand 10 after
234 EXPECT_EQ(0, [mock_central_manager_ stopScanCallCount]); 243 EXPECT_EQ(0, [mock_central_manager_ stopScanCallCount]);
235 } 244 }
236 245
237 TEST_F(BluetoothAdapterMacTest, CheckGetPeripheralHashAddress) { 246 TEST_F(BluetoothAdapterMacTest, CheckGetPeripheralHashAddress) {
238 base::scoped_nsobject<id> mock_peripheral(CreateMockPeripheral(kTestNSUUID)); 247 base::scoped_nsobject<id> mock_peripheral(CreateMockPeripheral(kTestNSUUID));
239 if (mock_peripheral.get() == nil) 248 if (mock_peripheral.get() == nil)
240 return; 249 return;
241 EXPECT_EQ(kTestHashAddress, GetHashAddress(mock_peripheral)); 250 EXPECT_EQ(kTestHashAddress, GetHashAddress(mock_peripheral));
242 } 251 }
243 252
253 TEST_F(BluetoothAdapterMacTest, LowEnergyDeviceUpdatedNewDevice) {
254 base::scoped_nsobject<id> mock_peripheral(CreateMockPeripheral(kTestNSUUID));
255 if (mock_peripheral.get() == nil)
256 return;
257 base::scoped_nsobject<NSDictionary> advertisement_data(
258 CreateAdvertisementData());
259
260 EXPECT_EQ(0, NumDevices());
261 EXPECT_FALSE(DevicePresent(mock_peripheral));
262 LowEnergyDeviceUpdated(mock_peripheral, advertisement_data, kTestRssi);
263 EXPECT_EQ(1, NumDevices());
264 EXPECT_TRUE(DevicePresent(mock_peripheral));
265 }
266
267 TEST_F(BluetoothAdapterMacTest, LowEnergyDeviceUpdatedOldDevice) {
268 base::scoped_nsobject<id> mock_peripheral(CreateMockPeripheral(kTestNSUUID));
269 if (mock_peripheral.get() == nil)
270 return;
271 base::scoped_nsobject<NSDictionary> advertisement_data(
272 CreateAdvertisementData());
273
274 // Update the device for the first time and check it was correctly added to
275 // |devices_|.
276 EXPECT_EQ(0, NumDevices());
277 EXPECT_FALSE(DevicePresent(mock_peripheral));
278 LowEnergyDeviceUpdated(mock_peripheral, advertisement_data, kTestRssi);
279 EXPECT_EQ(1, NumDevices());
280 EXPECT_TRUE(DevicePresent(mock_peripheral));
281 // Search for the device by the address corresponding to |kTestNSUUID|.
282 BluetoothDeviceMac* device =
283 static_cast<BluetoothDeviceMac*>(GetDevice(kTestHashAddress));
284 base::scoped_nsobject<NSDate> first_update_time(
285 [device->GetLastUpdateTime() retain]);
286
287 // Update the device a second time. The device should be updated in
288 // |devices_| so check the time returned by GetLastUpdateTime() has increased.
289 LowEnergyDeviceUpdated(mock_peripheral, advertisement_data, kTestRssi);
290 EXPECT_EQ(1, NumDevices());
291 EXPECT_TRUE(DevicePresent(mock_peripheral));
292 device = static_cast<BluetoothDeviceMac*>(GetDevice(kTestHashAddress));
293 EXPECT_TRUE([device->GetLastUpdateTime() compare:first_update_time] ==
294 NSOrderedDescending);
295 }
296
244 TEST_F(BluetoothAdapterMacTest, UpdateDevicesRemovesLowEnergyDevice) { 297 TEST_F(BluetoothAdapterMacTest, UpdateDevicesRemovesLowEnergyDevice) {
245 base::scoped_nsobject<id> mock_peripheral(CreateMockPeripheral(kTestNSUUID)); 298 base::scoped_nsobject<id> mock_peripheral(CreateMockPeripheral(kTestNSUUID));
246 if (mock_peripheral.get() == nil) 299 if (mock_peripheral.get() == nil)
247 return; 300 return;
248 base::scoped_nsobject<NSDictionary> advertisement_data( 301 base::scoped_nsobject<NSDictionary> advertisement_data(
249 CreateAdvertisementData()); 302 CreateAdvertisementData());
250 303
251 BluetoothLowEnergyDeviceMac* device = 304 BluetoothLowEnergyDeviceMac* device = new BluetoothLowEnergyDeviceMac(
252 new BluetoothLowEnergyDeviceMac(mock_peripheral, advertisement_data, 0); 305 mock_peripheral, advertisement_data, kTestRssi);
253 SetDeviceTimeGreaterThanTimeout(device); 306 SetDeviceTimeGreaterThanTimeout(device);
254 307
255 EXPECT_EQ(0, NumDevices()); 308 EXPECT_EQ(0, NumDevices());
256 AddLowEnergyDevice(device); 309 AddLowEnergyDevice(device);
257 EXPECT_EQ(1, NumDevices()); 310 EXPECT_EQ(1, NumDevices());
258 EXPECT_TRUE(DevicePresent(mock_peripheral)); 311 EXPECT_TRUE(DevicePresent(mock_peripheral));
259 312
260 // Check that object pointed to by |device| is deleted by the adapter. 313 // Check that object pointed to by |device| is deleted by the adapter.
261 RemoveTimedOutDevices(); 314 RemoveTimedOutDevices();
262 EXPECT_EQ(0, NumDevices()); 315 EXPECT_EQ(0, NumDevices());
263 EXPECT_FALSE(DevicePresent(mock_peripheral)); 316 EXPECT_FALSE(DevicePresent(mock_peripheral));
264 } 317 }
265 318
266 } // namespace device 319 } // namespace device
OLDNEW
« no previous file with comments | « device/bluetooth/bluetooth_adapter_mac.mm ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698