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

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

Powered by Google App Engine
This is Rietveld 408576698