OLD | NEW |
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" |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 int NumDevices() { return adapter_mac_->devices_.size(); } | 100 int NumDevices() { return adapter_mac_->devices_.size(); } |
101 | 101 |
102 bool DevicePresent(CBPeripheral* peripheral) { | 102 bool DevicePresent(CBPeripheral* peripheral) { |
103 BluetoothDevice* device = adapter_mac_->GetDevice( | 103 BluetoothDevice* device = adapter_mac_->GetDevice( |
104 BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral)); | 104 BluetoothLowEnergyDeviceMac::GetPeripheralHashAddress(peripheral)); |
105 return (device != NULL); | 105 return (device != NULL); |
106 } | 106 } |
107 | 107 |
108 void RemoveTimedOutDevices() { adapter_mac_->RemoveTimedOutDevices(); } | 108 void RemoveTimedOutDevices() { adapter_mac_->RemoveTimedOutDevices(); } |
109 | 109 |
110 bool SetMockCentralManager() { | 110 bool SetMockCentralManager(CBCentralManagerState desired_state) { |
111 if (!BluetoothAdapterMac::IsLowEnergyAvailable()) { | 111 if (!BluetoothAdapterMac::IsLowEnergyAvailable()) { |
112 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; | 112 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test."; |
113 return false; | 113 return false; |
114 } | 114 } |
115 mock_central_manager_ = [[MockCentralManager alloc] init]; | 115 mock_central_manager_ = [[MockCentralManager alloc] init]; |
| 116 [mock_central_manager_ setState:desired_state]; |
116 adapter_mac_->SetCentralManagerForTesting(mock_central_manager_); | 117 adapter_mac_->SetCentralManagerForTesting(mock_central_manager_); |
117 return true; | 118 return true; |
118 } | 119 } |
119 | 120 |
120 void AddDiscoverySession(BluetoothDiscoveryFilter* discovery_filter) { | 121 void AddDiscoverySession(BluetoothDiscoveryFilter* discovery_filter) { |
121 adapter_mac_->AddDiscoverySession( | 122 adapter_mac_->AddDiscoverySession( |
122 discovery_filter, | 123 discovery_filter, |
123 base::Bind(&BluetoothAdapterMacTest::Callback, base::Unretained(this)), | 124 base::Bind(&BluetoothAdapterMacTest::Callback, base::Unretained(this)), |
124 base::Bind(&BluetoothAdapterMacTest::ErrorCallback, | 125 base::Bind(&BluetoothAdapterMacTest::ErrorCallback, |
125 base::Unretained(this))); | 126 base::Unretained(this))); |
(...skipping 24 matching lines...) Expand all Loading... |
150 int callback_count_; | 151 int callback_count_; |
151 int error_callback_count_; | 152 int error_callback_count_; |
152 }; | 153 }; |
153 | 154 |
154 TEST_F(BluetoothAdapterMacTest, Poll) { | 155 TEST_F(BluetoothAdapterMacTest, Poll) { |
155 PollAdapter(); | 156 PollAdapter(); |
156 EXPECT_FALSE(ui_task_runner_->GetPendingTasks().empty()); | 157 EXPECT_FALSE(ui_task_runner_->GetPendingTasks().empty()); |
157 } | 158 } |
158 | 159 |
159 TEST_F(BluetoothAdapterMacTest, AddDiscoverySessionWithLowEnergyFilter) { | 160 TEST_F(BluetoothAdapterMacTest, AddDiscoverySessionWithLowEnergyFilter) { |
160 if (!SetMockCentralManager()) | 161 if (!SetMockCentralManager(CBCentralManagerStatePoweredOn)) |
161 return; | 162 return; |
162 EXPECT_EQ(0, [mock_central_manager_ scanForPeripheralsCallCount]); | 163 EXPECT_EQ(0, [mock_central_manager_ scanForPeripheralsCallCount]); |
163 EXPECT_EQ(0, NumDiscoverySessions()); | 164 EXPECT_EQ(0, NumDiscoverySessions()); |
164 | 165 |
165 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter( | 166 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter( |
166 new BluetoothDiscoveryFilter( | 167 new BluetoothDiscoveryFilter( |
167 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE)); | 168 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE)); |
168 AddDiscoverySession(discovery_filter.get()); | 169 AddDiscoverySession(discovery_filter.get()); |
169 EXPECT_EQ(1, callback_count_); | 170 EXPECT_EQ(1, callback_count_); |
170 EXPECT_EQ(0, error_callback_count_); | 171 EXPECT_EQ(0, error_callback_count_); |
171 EXPECT_EQ(1, NumDiscoverySessions()); | 172 EXPECT_EQ(1, NumDiscoverySessions()); |
172 | 173 |
173 // Check that adding a discovery session resulted in | 174 // Check that adding a discovery session resulted in |
174 // scanForPeripheralsWithServices being called on the Central Manager. | 175 // scanForPeripheralsWithServices being called on the Central Manager. |
175 EXPECT_EQ(1, [mock_central_manager_ scanForPeripheralsCallCount]); | 176 EXPECT_EQ(1, [mock_central_manager_ scanForPeripheralsCallCount]); |
176 } | 177 } |
177 | 178 |
178 // TODO(krstnmnlsn): Test changing the filter when adding the second discovery | 179 // TODO(krstnmnlsn): Test changing the filter when adding the second discovery |
179 // session (once we have that ability). | 180 // session (once we have that ability). |
180 TEST_F(BluetoothAdapterMacTest, AddSecondDiscoverySessionWithLowEnergyFilter) { | 181 TEST_F(BluetoothAdapterMacTest, AddSecondDiscoverySessionWithLowEnergyFilter) { |
181 if (!SetMockCentralManager()) | 182 if (!SetMockCentralManager(CBCentralManagerStatePoweredOn)) |
182 return; | 183 return; |
183 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter( | 184 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter( |
184 new BluetoothDiscoveryFilter( | 185 new BluetoothDiscoveryFilter( |
185 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE)); | 186 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE)); |
186 AddDiscoverySession(discovery_filter.get()); | 187 AddDiscoverySession(discovery_filter.get()); |
187 EXPECT_EQ(1, callback_count_); | 188 EXPECT_EQ(1, callback_count_); |
188 EXPECT_EQ(0, error_callback_count_); | 189 EXPECT_EQ(0, error_callback_count_); |
189 EXPECT_EQ(1, NumDiscoverySessions()); | 190 EXPECT_EQ(1, NumDiscoverySessions()); |
190 | 191 |
191 // We replaced the success callback handed to AddDiscoverySession, so | 192 // We replaced the success callback handed to AddDiscoverySession, so |
192 // |adapter_mac_| should remain in a discovering state indefinitely. | 193 // |adapter_mac_| should remain in a discovering state indefinitely. |
193 EXPECT_TRUE(adapter_mac_->IsDiscovering()); | 194 EXPECT_TRUE(adapter_mac_->IsDiscovering()); |
194 | 195 |
195 AddDiscoverySession(discovery_filter.get()); | 196 AddDiscoverySession(discovery_filter.get()); |
196 EXPECT_EQ(2, [mock_central_manager_ scanForPeripheralsCallCount]); | 197 EXPECT_EQ(2, [mock_central_manager_ scanForPeripheralsCallCount]); |
197 EXPECT_EQ(2, callback_count_); | 198 EXPECT_EQ(2, callback_count_); |
198 EXPECT_EQ(0, error_callback_count_); | 199 EXPECT_EQ(0, error_callback_count_); |
199 EXPECT_EQ(2, NumDiscoverySessions()); | 200 EXPECT_EQ(2, NumDiscoverySessions()); |
200 } | 201 } |
201 | 202 |
202 TEST_F(BluetoothAdapterMacTest, RemoveDiscoverySessionWithLowEnergyFilter) { | 203 TEST_F(BluetoothAdapterMacTest, RemoveDiscoverySessionWithLowEnergyFilter) { |
203 if (!SetMockCentralManager()) | 204 if (!SetMockCentralManager(CBCentralManagerStatePoweredOn)) |
204 return; | 205 return; |
205 EXPECT_EQ(0, [mock_central_manager_ scanForPeripheralsCallCount]); | 206 EXPECT_EQ(0, [mock_central_manager_ scanForPeripheralsCallCount]); |
206 | 207 |
207 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter( | 208 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter( |
208 new BluetoothDiscoveryFilter( | 209 new BluetoothDiscoveryFilter( |
209 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE)); | 210 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE)); |
210 AddDiscoverySession(discovery_filter.get()); | 211 AddDiscoverySession(discovery_filter.get()); |
211 EXPECT_EQ(1, callback_count_); | 212 EXPECT_EQ(1, callback_count_); |
212 EXPECT_EQ(0, error_callback_count_); | 213 EXPECT_EQ(0, error_callback_count_); |
213 EXPECT_EQ(1, NumDiscoverySessions()); | 214 EXPECT_EQ(1, NumDiscoverySessions()); |
214 | 215 |
215 EXPECT_EQ(0, [mock_central_manager_ stopScanCallCount]); | 216 EXPECT_EQ(0, [mock_central_manager_ stopScanCallCount]); |
216 RemoveDiscoverySession(discovery_filter.get()); | 217 RemoveDiscoverySession(discovery_filter.get()); |
217 EXPECT_EQ(2, callback_count_); | 218 EXPECT_EQ(2, callback_count_); |
218 EXPECT_EQ(0, error_callback_count_); | 219 EXPECT_EQ(0, error_callback_count_); |
219 EXPECT_EQ(0, NumDiscoverySessions()); | 220 EXPECT_EQ(0, NumDiscoverySessions()); |
220 | 221 |
221 // Check that removing the discovery session resulted in stopScan being called | 222 // Check that removing the discovery session resulted in stopScan being called |
222 // on the Central Manager. | 223 // on the Central Manager. |
223 EXPECT_EQ(1, [mock_central_manager_ stopScanCallCount]); | 224 EXPECT_EQ(1, [mock_central_manager_ stopScanCallCount]); |
224 } | 225 } |
225 | 226 |
226 TEST_F(BluetoothAdapterMacTest, RemoveDiscoverySessionWithLowEnergyFilterFail) { | 227 TEST_F(BluetoothAdapterMacTest, RemoveDiscoverySessionWithLowEnergyFilterFail) { |
227 if (!SetMockCentralManager()) | 228 if (!SetMockCentralManager(CBCentralManagerStatePoweredOn)) |
228 return; | 229 return; |
229 EXPECT_EQ(0, [mock_central_manager_ scanForPeripheralsCallCount]); | 230 EXPECT_EQ(0, [mock_central_manager_ scanForPeripheralsCallCount]); |
230 EXPECT_EQ(0, [mock_central_manager_ stopScanCallCount]); | 231 EXPECT_EQ(0, [mock_central_manager_ stopScanCallCount]); |
231 EXPECT_EQ(0, NumDiscoverySessions()); | 232 EXPECT_EQ(0, NumDiscoverySessions()); |
232 | 233 |
233 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter( | 234 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter( |
234 new BluetoothDiscoveryFilter( | 235 new BluetoothDiscoveryFilter( |
235 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE)); | 236 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE)); |
236 RemoveDiscoverySession(discovery_filter.get()); | 237 RemoveDiscoverySession(discovery_filter.get()); |
237 EXPECT_EQ(0, callback_count_); | 238 EXPECT_EQ(0, callback_count_); |
(...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
309 EXPECT_EQ(1, NumDevices()); | 310 EXPECT_EQ(1, NumDevices()); |
310 EXPECT_TRUE(DevicePresent(mock_peripheral)); | 311 EXPECT_TRUE(DevicePresent(mock_peripheral)); |
311 | 312 |
312 // 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. |
313 RemoveTimedOutDevices(); | 314 RemoveTimedOutDevices(); |
314 EXPECT_EQ(0, NumDevices()); | 315 EXPECT_EQ(0, NumDevices()); |
315 EXPECT_FALSE(DevicePresent(mock_peripheral)); | 316 EXPECT_FALSE(DevicePresent(mock_peripheral)); |
316 } | 317 } |
317 | 318 |
318 } // namespace device | 319 } // namespace device |
OLD | NEW |