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

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

Issue 1842223003: Remove outdated devices from Android device chooser (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fixed unit test for ItemChooserDialog Created 4 years, 8 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/bind.h" 5 #include "base/bind.h"
6 #include "base/memory/ref_counted.h" 6 #include "base/memory/ref_counted.h"
7 #include "base/test/test_simple_task_runner.h" 7 #include "base/test/test_simple_task_runner.h"
8 #include "build/build_config.h" 8 #include "build/build_config.h"
9 #include "device/bluetooth/bluetooth_adapter.h" 9 #include "device/bluetooth/bluetooth_adapter.h"
10 #include "device/bluetooth/bluetooth_adapter_mac.h" 10 #include "device/bluetooth/bluetooth_adapter_mac.h"
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
72 CBAdvertisementDataIsConnectable : @(YES), 72 CBAdvertisementDataIsConnectable : @(YES),
73 CBAdvertisementDataServiceDataKey : [NSDictionary dictionary], 73 CBAdvertisementDataServiceDataKey : [NSDictionary dictionary],
74 }; 74 };
75 return [advertisement_data retain]; 75 return [advertisement_data retain];
76 } 76 }
77 77
78 std::string GetHashAddress(CBPeripheral* peripheral) { 78 std::string GetHashAddress(CBPeripheral* peripheral) {
79 return BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral); 79 return BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral);
80 } 80 }
81 81
82 void SetDeviceTimeGreaterThanTimeout(BluetoothLowEnergyDeviceMac* device) {
83 device->last_update_time_.reset([[NSDate
84 dateWithTimeInterval:-(BluetoothAdapterMac::kDiscoveryTimeoutSec + 1)
85 sinceDate:[NSDate date]] retain]);
86 }
87
88 void AddLowEnergyDevice(BluetoothLowEnergyDeviceMac* device) { 82 void AddLowEnergyDevice(BluetoothLowEnergyDeviceMac* device) {
89 adapter_mac_->devices_.set(device->GetAddress(), 83 adapter_mac_->devices_.set(device->GetAddress(),
90 scoped_ptr<BluetoothDevice>(device)); 84 scoped_ptr<BluetoothDevice>(device));
91 } 85 }
92 86
93 int NumDevices() { return adapter_mac_->devices_.size(); } 87 int NumDevices() { return adapter_mac_->devices_.size(); }
94 88
95 bool DevicePresent(CBPeripheral* peripheral) { 89 bool DevicePresent(CBPeripheral* peripheral) {
96 BluetoothDevice* device = adapter_mac_->GetDevice( 90 BluetoothDevice* device = adapter_mac_->GetDevice(
97 BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral)); 91 BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral));
98 return (device != NULL); 92 return (device != NULL);
99 } 93 }
100 94
101 void RemoveTimedOutDevices() { adapter_mac_->RemoveTimedOutDevices(); }
102
103 bool SetMockCentralManager(CBCentralManagerState desired_state) { 95 bool SetMockCentralManager(CBCentralManagerState desired_state) {
104 if (!BluetoothAdapterMac::IsLowEnergyAvailable()) { 96 if (!BluetoothAdapterMac::IsLowEnergyAvailable()) {
105 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; 97 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
106 return false; 98 return false;
107 } 99 }
108 mock_central_manager_.reset([[MockCentralManager alloc] init]); 100 mock_central_manager_.reset([[MockCentralManager alloc] init]);
109 [mock_central_manager_ setState:desired_state]; 101 [mock_central_manager_ setState:desired_state];
110 CBCentralManager* centralManager = 102 CBCentralManager* centralManager =
111 (CBCentralManager*)mock_central_manager_.get(); 103 (CBCentralManager*)mock_central_manager_.get();
112 adapter_mac_->SetCentralManagerForTesting(centralManager); 104 adapter_mac_->SetCentralManagerForTesting(centralManager);
(...skipping 148 matching lines...) Expand 10 before | Expand all | Expand 10 after
261 base::scoped_nsobject<NSDictionary> advertisement_data( 253 base::scoped_nsobject<NSDictionary> advertisement_data(
262 CreateAdvertisementData()); 254 CreateAdvertisementData());
263 255
264 EXPECT_EQ(0, NumDevices()); 256 EXPECT_EQ(0, NumDevices());
265 EXPECT_FALSE(DevicePresent(mock_peripheral)); 257 EXPECT_FALSE(DevicePresent(mock_peripheral));
266 LowEnergyDeviceUpdated(mock_peripheral, advertisement_data, kTestRssi); 258 LowEnergyDeviceUpdated(mock_peripheral, advertisement_data, kTestRssi);
267 EXPECT_EQ(1, NumDevices()); 259 EXPECT_EQ(1, NumDevices());
268 EXPECT_TRUE(DevicePresent(mock_peripheral)); 260 EXPECT_TRUE(DevicePresent(mock_peripheral));
269 } 261 }
270 262
271 TEST_F(BluetoothAdapterMacTest, LowEnergyDeviceUpdatedOldDevice) {
272 if (!SetMockCentralManager(CBCentralManagerStatePoweredOn))
273 return;
274 base::scoped_nsobject<CBPeripheral> mock_peripheral(
275 CreateMockPeripheral(kTestNSUUID));
276 if (mock_peripheral.get() == nil)
277 return;
278 base::scoped_nsobject<NSDictionary> advertisement_data(
279 CreateAdvertisementData());
280
281 // Update the device for the first time and check it was correctly added to
282 // |devices_|.
283 EXPECT_EQ(0, NumDevices());
284 EXPECT_FALSE(DevicePresent(mock_peripheral));
285 LowEnergyDeviceUpdated(mock_peripheral, advertisement_data, kTestRssi);
286 EXPECT_EQ(1, NumDevices());
287 EXPECT_TRUE(DevicePresent(mock_peripheral));
288 // Search for the device by the address corresponding to |kTestNSUUID|.
289 BluetoothDeviceMac* device =
290 static_cast<BluetoothDeviceMac*>(GetDevice(kTestHashAddress));
291 base::scoped_nsobject<NSDate> first_update_time(
292 [device->GetLastUpdateTime() retain]);
293
294 // Update the device a second time. The device should be updated in
295 // |devices_| so check the time returned by GetLastUpdateTime() has increased.
296 LowEnergyDeviceUpdated(mock_peripheral, advertisement_data, kTestRssi);
297 EXPECT_EQ(1, NumDevices());
298 EXPECT_TRUE(DevicePresent(mock_peripheral));
299 device = static_cast<BluetoothDeviceMac*>(GetDevice(kTestHashAddress));
300 EXPECT_TRUE([device->GetLastUpdateTime() compare:first_update_time] ==
301 NSOrderedDescending);
302 }
303
304 TEST_F(BluetoothAdapterMacTest, UpdateDevicesRemovesLowEnergyDevice) {
305 if (!SetMockCentralManager(CBCentralManagerStatePoweredOn))
306 return;
307 base::scoped_nsobject<CBPeripheral> mock_peripheral(
308 CreateMockPeripheral(kTestNSUUID));
309 if (mock_peripheral.get() == nil)
310 return;
311 base::scoped_nsobject<NSDictionary> advertisement_data(
312 CreateAdvertisementData());
313
314 BluetoothLowEnergyDeviceMac* device = new BluetoothLowEnergyDeviceMac(
315 adapter_mac_, mock_peripheral, advertisement_data, kTestRssi);
316 SetDeviceTimeGreaterThanTimeout(device);
317
318 EXPECT_EQ(0, NumDevices());
319 AddLowEnergyDevice(device);
320 EXPECT_EQ(1, NumDevices());
321 EXPECT_TRUE(DevicePresent(mock_peripheral));
322
323 // Check that object pointed to by |device| is deleted by the adapter.
324 RemoveTimedOutDevices();
325 EXPECT_EQ(0, NumDevices());
326 EXPECT_FALSE(DevicePresent(mock_peripheral));
327 }
328
329 } // namespace device 263 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698