OLD | NEW |
| (Empty) |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 #include "base/memory/scoped_vector.h" | |
6 #include "base/message_loop/message_loop.h" | |
7 #include "base/run_loop.h" | |
8 #include "base/strings/utf_string_conversions.h" | |
9 #include "dbus/object_path.h" | |
10 #include "device/bluetooth/bluetooth_adapter.h" | |
11 #include "device/bluetooth/bluetooth_adapter_chromeos.h" | |
12 #include "device/bluetooth/bluetooth_adapter_factory.h" | |
13 #include "device/bluetooth/bluetooth_device.h" | |
14 #include "device/bluetooth/bluetooth_device_chromeos.h" | |
15 #include "device/bluetooth/bluetooth_discovery_session.h" | |
16 #include "device/bluetooth/bluetooth_pairing_chromeos.h" | |
17 #include "device/bluetooth/dbus/bluez_dbus_manager.h" | |
18 #include "device/bluetooth/dbus/fake_bluetooth_adapter_client.h" | |
19 #include "device/bluetooth/dbus/fake_bluetooth_agent_manager_client.h" | |
20 #include "device/bluetooth/dbus/fake_bluetooth_device_client.h" | |
21 #include "device/bluetooth/dbus/fake_bluetooth_gatt_service_client.h" | |
22 #include "device/bluetooth/dbus/fake_bluetooth_input_client.h" | |
23 #include "device/bluetooth/test/test_bluetooth_adapter_observer.h" | |
24 #include "testing/gtest/include/gtest/gtest.h" | |
25 #include "third_party/cros_system_api/dbus/service_constants.h" | |
26 | |
27 using device::BluetoothAdapter; | |
28 using device::BluetoothAdapterFactory; | |
29 using device::BluetoothAudioSink; | |
30 using device::BluetoothDevice; | |
31 using device::BluetoothDiscoveryFilter; | |
32 using device::BluetoothDiscoverySession; | |
33 using device::BluetoothUUID; | |
34 using device::TestBluetoothAdapterObserver; | |
35 | |
36 namespace chromeos { | |
37 | |
38 namespace { | |
39 | |
40 // Callback for BluetoothDevice::GetConnectionInfo() that simply saves the | |
41 // connection info to the bound argument. | |
42 void SaveConnectionInfo(BluetoothDevice::ConnectionInfo* out, | |
43 const BluetoothDevice::ConnectionInfo& conn_info) { | |
44 *out = conn_info; | |
45 }; | |
46 | |
47 class FakeBluetoothProfileServiceProviderDelegate | |
48 : public bluez::BluetoothProfileServiceProvider::Delegate { | |
49 public: | |
50 FakeBluetoothProfileServiceProviderDelegate() {} | |
51 | |
52 // bluez::BluetoothProfileServiceProvider::Delegate: | |
53 void Released() override {} | |
54 | |
55 void NewConnection( | |
56 const dbus::ObjectPath&, | |
57 scoped_ptr<dbus::FileDescriptor>, | |
58 const bluez::BluetoothProfileServiceProvider::Delegate::Options&, | |
59 const ConfirmationCallback&) override {} | |
60 | |
61 void RequestDisconnection(const dbus::ObjectPath&, | |
62 const ConfirmationCallback&) override {} | |
63 | |
64 void Cancel() override {} | |
65 }; | |
66 | |
67 } // namespace | |
68 | |
69 class TestPairingDelegate : public BluetoothDevice::PairingDelegate { | |
70 public: | |
71 TestPairingDelegate() | |
72 : call_count_(0), | |
73 request_pincode_count_(0), | |
74 request_passkey_count_(0), | |
75 display_pincode_count_(0), | |
76 display_passkey_count_(0), | |
77 keys_entered_count_(0), | |
78 confirm_passkey_count_(0), | |
79 authorize_pairing_count_(0), | |
80 last_passkey_(9999999U), | |
81 last_entered_(999U) {} | |
82 ~TestPairingDelegate() override {} | |
83 | |
84 void RequestPinCode(BluetoothDevice* device) override { | |
85 ++call_count_; | |
86 ++request_pincode_count_; | |
87 QuitMessageLoop(); | |
88 } | |
89 | |
90 void RequestPasskey(BluetoothDevice* device) override { | |
91 ++call_count_; | |
92 ++request_passkey_count_; | |
93 QuitMessageLoop(); | |
94 } | |
95 | |
96 void DisplayPinCode(BluetoothDevice* device, | |
97 const std::string& pincode) override { | |
98 ++call_count_; | |
99 ++display_pincode_count_; | |
100 last_pincode_ = pincode; | |
101 QuitMessageLoop(); | |
102 } | |
103 | |
104 void DisplayPasskey(BluetoothDevice* device, uint32 passkey) override { | |
105 ++call_count_; | |
106 ++display_passkey_count_; | |
107 last_passkey_ = passkey; | |
108 QuitMessageLoop(); | |
109 } | |
110 | |
111 void KeysEntered(BluetoothDevice* device, uint32 entered) override { | |
112 ++call_count_; | |
113 ++keys_entered_count_; | |
114 last_entered_ = entered; | |
115 QuitMessageLoop(); | |
116 } | |
117 | |
118 void ConfirmPasskey(BluetoothDevice* device, uint32 passkey) override { | |
119 ++call_count_; | |
120 ++confirm_passkey_count_; | |
121 last_passkey_ = passkey; | |
122 QuitMessageLoop(); | |
123 } | |
124 | |
125 void AuthorizePairing(BluetoothDevice* device) override { | |
126 ++call_count_; | |
127 ++authorize_pairing_count_; | |
128 QuitMessageLoop(); | |
129 } | |
130 | |
131 int call_count_; | |
132 int request_pincode_count_; | |
133 int request_passkey_count_; | |
134 int display_pincode_count_; | |
135 int display_passkey_count_; | |
136 int keys_entered_count_; | |
137 int confirm_passkey_count_; | |
138 int authorize_pairing_count_; | |
139 uint32 last_passkey_; | |
140 uint32 last_entered_; | |
141 std::string last_pincode_; | |
142 | |
143 private: | |
144 // Some tests use a message loop since background processing is simulated; | |
145 // break out of those loops. | |
146 void QuitMessageLoop() { | |
147 if (base::MessageLoop::current() && | |
148 base::MessageLoop::current()->is_running()) { | |
149 base::MessageLoop::current()->Quit(); | |
150 } | |
151 } | |
152 }; | |
153 | |
154 class BluetoothChromeOSTest : public testing::Test { | |
155 public: | |
156 void SetUp() override { | |
157 scoped_ptr<bluez::BluezDBusManagerSetter> dbus_setter = | |
158 bluez::BluezDBusManager::GetSetterForTesting(); | |
159 // We need to initialize BluezDBusManager early to prevent | |
160 // Bluetooth*::Create() methods from picking the real instead of fake | |
161 // implementations. | |
162 fake_bluetooth_adapter_client_ = new bluez::FakeBluetoothAdapterClient; | |
163 dbus_setter->SetBluetoothAdapterClient( | |
164 scoped_ptr<bluez::BluetoothAdapterClient>( | |
165 fake_bluetooth_adapter_client_)); | |
166 fake_bluetooth_device_client_ = new bluez::FakeBluetoothDeviceClient; | |
167 dbus_setter->SetBluetoothDeviceClient( | |
168 scoped_ptr<bluez::BluetoothDeviceClient>( | |
169 fake_bluetooth_device_client_)); | |
170 dbus_setter->SetBluetoothInputClient( | |
171 scoped_ptr<bluez::BluetoothInputClient>( | |
172 new bluez::FakeBluetoothInputClient)); | |
173 dbus_setter->SetBluetoothAgentManagerClient( | |
174 scoped_ptr<bluez::BluetoothAgentManagerClient>( | |
175 new bluez::FakeBluetoothAgentManagerClient)); | |
176 dbus_setter->SetBluetoothGattServiceClient( | |
177 scoped_ptr<bluez::BluetoothGattServiceClient>( | |
178 new bluez::FakeBluetoothGattServiceClient)); | |
179 | |
180 fake_bluetooth_adapter_client_->SetSimulationIntervalMs(10); | |
181 | |
182 callback_count_ = 0; | |
183 error_callback_count_ = 0; | |
184 last_connect_error_ = BluetoothDevice::ERROR_UNKNOWN; | |
185 last_client_error_ = ""; | |
186 } | |
187 | |
188 void TearDown() override { | |
189 for (ScopedVector<BluetoothDiscoverySession>::iterator iter = | |
190 discovery_sessions_.begin(); | |
191 iter != discovery_sessions_.end(); | |
192 ++iter) { | |
193 BluetoothDiscoverySession* session = *iter; | |
194 if (!session->IsActive()) | |
195 continue; | |
196 callback_count_ = 0; | |
197 session->Stop(GetCallback(), GetErrorCallback()); | |
198 message_loop_.Run(); | |
199 ASSERT_EQ(1, callback_count_); | |
200 } | |
201 discovery_sessions_.clear(); | |
202 adapter_ = nullptr; | |
203 bluez::BluezDBusManager::Shutdown(); | |
204 } | |
205 | |
206 // Generic callbacks | |
207 void Callback() { | |
208 ++callback_count_; | |
209 QuitMessageLoop(); | |
210 } | |
211 | |
212 base::Closure GetCallback() { | |
213 return base::Bind(&BluetoothChromeOSTest::Callback, base::Unretained(this)); | |
214 } | |
215 | |
216 void DiscoverySessionCallback( | |
217 scoped_ptr<BluetoothDiscoverySession> discovery_session) { | |
218 ++callback_count_; | |
219 discovery_sessions_.push_back(discovery_session.release()); | |
220 QuitMessageLoop(); | |
221 } | |
222 | |
223 void AudioSinkAcquiredCallback(scoped_refptr<BluetoothAudioSink>) { | |
224 ++callback_count_; | |
225 QuitMessageLoop(); | |
226 } | |
227 | |
228 void ProfileRegisteredCallback(BluetoothAdapterProfileChromeOS* profile) { | |
229 adapter_profile_ = profile; | |
230 ++callback_count_; | |
231 QuitMessageLoop(); | |
232 } | |
233 | |
234 void ErrorCallback() { | |
235 ++error_callback_count_; | |
236 QuitMessageLoop(); | |
237 } | |
238 | |
239 void DiscoveryErrorCallback(device::UMABluetoothDiscoverySessionOutcome) { | |
240 ErrorCallback(); | |
241 } | |
242 | |
243 base::Closure GetErrorCallback() { | |
244 return base::Bind(&BluetoothChromeOSTest::ErrorCallback, | |
245 base::Unretained(this)); | |
246 } | |
247 | |
248 base::Callback<void(device::UMABluetoothDiscoverySessionOutcome)> | |
249 GetDiscoveryErrorCallback() { | |
250 return base::Bind(&BluetoothChromeOSTest::DiscoveryErrorCallback, | |
251 base::Unretained(this)); | |
252 } | |
253 | |
254 void DBusErrorCallback(const std::string& error_name, | |
255 const std::string& error_message) { | |
256 ++error_callback_count_; | |
257 last_client_error_ = error_name; | |
258 QuitMessageLoop(); | |
259 } | |
260 | |
261 void ConnectErrorCallback(BluetoothDevice::ConnectErrorCode error) { | |
262 ++error_callback_count_; | |
263 last_connect_error_ = error; | |
264 } | |
265 | |
266 void AudioSinkErrorCallback(BluetoothAudioSink::ErrorCode) { | |
267 ++error_callback_count_; | |
268 QuitMessageLoop(); | |
269 } | |
270 | |
271 void ErrorCompletionCallback(const std::string& error_message) { | |
272 ++error_callback_count_; | |
273 QuitMessageLoop(); | |
274 } | |
275 | |
276 // Call to fill the adapter_ member with a BluetoothAdapter instance. | |
277 void GetAdapter() { | |
278 adapter_ = new BluetoothAdapterChromeOS(); | |
279 ASSERT_TRUE(adapter_.get() != nullptr); | |
280 ASSERT_TRUE(adapter_->IsInitialized()); | |
281 } | |
282 | |
283 // Run a discovery phase until the named device is detected, or if the named | |
284 // device is not created, the discovery process ends without finding it. | |
285 // | |
286 // The correct behavior of discovery is tested by the "Discovery" test case | |
287 // without using this function. | |
288 void DiscoverDevice(const std::string& address) { | |
289 ASSERT_TRUE(adapter_.get() != nullptr); | |
290 ASSERT_TRUE(base::MessageLoop::current() != nullptr); | |
291 fake_bluetooth_device_client_->SetSimulationIntervalMs(10); | |
292 | |
293 TestBluetoothAdapterObserver observer(adapter_); | |
294 | |
295 adapter_->SetPowered(true, GetCallback(), GetErrorCallback()); | |
296 adapter_->StartDiscoverySession( | |
297 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback, | |
298 base::Unretained(this)), | |
299 GetErrorCallback()); | |
300 base::MessageLoop::current()->Run(); | |
301 ASSERT_EQ(2, callback_count_); | |
302 ASSERT_EQ(0, error_callback_count_); | |
303 ASSERT_EQ((size_t)1, discovery_sessions_.size()); | |
304 ASSERT_TRUE(discovery_sessions_[0]->IsActive()); | |
305 callback_count_ = 0; | |
306 | |
307 ASSERT_TRUE(adapter_->IsPowered()); | |
308 ASSERT_TRUE(adapter_->IsDiscovering()); | |
309 | |
310 while (!observer.device_removed_count() && | |
311 observer.last_device_address() != address) | |
312 base::MessageLoop::current()->Run(); | |
313 | |
314 discovery_sessions_[0]->Stop(GetCallback(), GetErrorCallback()); | |
315 base::MessageLoop::current()->Run(); | |
316 ASSERT_EQ(1, callback_count_); | |
317 ASSERT_EQ(0, error_callback_count_); | |
318 callback_count_ = 0; | |
319 | |
320 ASSERT_FALSE(adapter_->IsDiscovering()); | |
321 } | |
322 | |
323 // Run a discovery phase so we have devices that can be paired with. | |
324 void DiscoverDevices() { | |
325 // Pass an invalid address for the device so that the discovery process | |
326 // completes with all devices. | |
327 DiscoverDevice("does not exist"); | |
328 } | |
329 | |
330 protected: | |
331 base::MessageLoop message_loop_; | |
332 bluez::FakeBluetoothAdapterClient* fake_bluetooth_adapter_client_; | |
333 bluez::FakeBluetoothDeviceClient* fake_bluetooth_device_client_; | |
334 scoped_refptr<BluetoothAdapter> adapter_; | |
335 | |
336 int callback_count_; | |
337 int error_callback_count_; | |
338 enum BluetoothDevice::ConnectErrorCode last_connect_error_; | |
339 std::string last_client_error_; | |
340 ScopedVector<BluetoothDiscoverySession> discovery_sessions_; | |
341 BluetoothAdapterProfileChromeOS* adapter_profile_; | |
342 | |
343 private: | |
344 // Some tests use a message loop since background processing is simulated; | |
345 // break out of those loops. | |
346 void QuitMessageLoop() { | |
347 if (base::MessageLoop::current() && | |
348 base::MessageLoop::current()->is_running()) { | |
349 base::MessageLoop::current()->Quit(); | |
350 } | |
351 } | |
352 }; | |
353 | |
354 TEST_F(BluetoothChromeOSTest, AlreadyPresent) { | |
355 GetAdapter(); | |
356 | |
357 // This verifies that the class gets the list of adapters when created; | |
358 // and initializes with an existing adapter if there is one. | |
359 EXPECT_TRUE(adapter_->IsPresent()); | |
360 EXPECT_FALSE(adapter_->IsPowered()); | |
361 EXPECT_EQ(bluez::FakeBluetoothAdapterClient::kAdapterAddress, | |
362 adapter_->GetAddress()); | |
363 EXPECT_FALSE(adapter_->IsDiscovering()); | |
364 | |
365 // There should be a device | |
366 BluetoothAdapter::DeviceList devices = adapter_->GetDevices(); | |
367 EXPECT_EQ(2U, devices.size()); | |
368 EXPECT_EQ(bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress, | |
369 devices[0]->GetAddress()); | |
370 EXPECT_EQ(bluez::FakeBluetoothDeviceClient::kPairedUnconnectableDeviceAddress, | |
371 devices[1]->GetAddress()); | |
372 } | |
373 | |
374 TEST_F(BluetoothChromeOSTest, BecomePresent) { | |
375 fake_bluetooth_adapter_client_->SetVisible(false); | |
376 GetAdapter(); | |
377 ASSERT_FALSE(adapter_->IsPresent()); | |
378 | |
379 // Install an observer; expect the AdapterPresentChanged to be called | |
380 // with true, and IsPresent() to return true. | |
381 TestBluetoothAdapterObserver observer(adapter_); | |
382 | |
383 fake_bluetooth_adapter_client_->SetVisible(true); | |
384 | |
385 EXPECT_EQ(1, observer.present_changed_count()); | |
386 EXPECT_TRUE(observer.last_present()); | |
387 | |
388 EXPECT_TRUE(adapter_->IsPresent()); | |
389 | |
390 // We should have had a device announced. | |
391 EXPECT_EQ(2, observer.device_added_count()); | |
392 EXPECT_EQ(bluez::FakeBluetoothDeviceClient::kPairedUnconnectableDeviceAddress, | |
393 observer.last_device_address()); | |
394 | |
395 // Other callbacks shouldn't be called if the values are false. | |
396 EXPECT_EQ(0, observer.powered_changed_count()); | |
397 EXPECT_EQ(0, observer.discovering_changed_count()); | |
398 EXPECT_FALSE(adapter_->IsPowered()); | |
399 EXPECT_FALSE(adapter_->IsDiscovering()); | |
400 } | |
401 | |
402 TEST_F(BluetoothChromeOSTest, BecomeNotPresent) { | |
403 GetAdapter(); | |
404 ASSERT_TRUE(adapter_->IsPresent()); | |
405 | |
406 // Install an observer; expect the AdapterPresentChanged to be called | |
407 // with false, and IsPresent() to return false. | |
408 TestBluetoothAdapterObserver observer(adapter_); | |
409 | |
410 fake_bluetooth_adapter_client_->SetVisible(false); | |
411 | |
412 EXPECT_EQ(1, observer.present_changed_count()); | |
413 EXPECT_FALSE(observer.last_present()); | |
414 | |
415 EXPECT_FALSE(adapter_->IsPresent()); | |
416 | |
417 // We should have had a device removed. | |
418 EXPECT_EQ(2, observer.device_removed_count()); | |
419 EXPECT_EQ(bluez::FakeBluetoothDeviceClient::kPairedUnconnectableDeviceAddress, | |
420 observer.last_device_address()); | |
421 | |
422 // Other callbacks shouldn't be called since the values are false. | |
423 EXPECT_EQ(0, observer.powered_changed_count()); | |
424 EXPECT_EQ(0, observer.discovering_changed_count()); | |
425 EXPECT_FALSE(adapter_->IsPowered()); | |
426 EXPECT_FALSE(adapter_->IsDiscovering()); | |
427 } | |
428 | |
429 TEST_F(BluetoothChromeOSTest, SecondAdapter) { | |
430 GetAdapter(); | |
431 ASSERT_TRUE(adapter_->IsPresent()); | |
432 | |
433 // Install an observer, then add a second adapter. Nothing should change, | |
434 // we ignore the second adapter. | |
435 TestBluetoothAdapterObserver observer(adapter_); | |
436 | |
437 fake_bluetooth_adapter_client_->SetSecondVisible(true); | |
438 | |
439 EXPECT_EQ(0, observer.present_changed_count()); | |
440 | |
441 EXPECT_TRUE(adapter_->IsPresent()); | |
442 EXPECT_EQ(bluez::FakeBluetoothAdapterClient::kAdapterAddress, | |
443 adapter_->GetAddress()); | |
444 | |
445 // Try removing the first adapter, we should now act as if the adapter | |
446 // is no longer present rather than fall back to the second. | |
447 fake_bluetooth_adapter_client_->SetVisible(false); | |
448 | |
449 EXPECT_EQ(1, observer.present_changed_count()); | |
450 EXPECT_FALSE(observer.last_present()); | |
451 | |
452 EXPECT_FALSE(adapter_->IsPresent()); | |
453 | |
454 // We should have had a device removed. | |
455 EXPECT_EQ(2, observer.device_removed_count()); | |
456 EXPECT_EQ(bluez::FakeBluetoothDeviceClient::kPairedUnconnectableDeviceAddress, | |
457 observer.last_device_address()); | |
458 | |
459 // Other callbacks shouldn't be called since the values are false. | |
460 EXPECT_EQ(0, observer.powered_changed_count()); | |
461 EXPECT_EQ(0, observer.discovering_changed_count()); | |
462 EXPECT_FALSE(adapter_->IsPowered()); | |
463 EXPECT_FALSE(adapter_->IsDiscovering()); | |
464 | |
465 observer.Reset(); | |
466 | |
467 // Removing the second adapter shouldn't set anything either. | |
468 fake_bluetooth_adapter_client_->SetSecondVisible(false); | |
469 | |
470 EXPECT_EQ(0, observer.device_removed_count()); | |
471 EXPECT_EQ(0, observer.powered_changed_count()); | |
472 EXPECT_EQ(0, observer.discovering_changed_count()); | |
473 } | |
474 | |
475 TEST_F(BluetoothChromeOSTest, BecomePowered) { | |
476 GetAdapter(); | |
477 ASSERT_FALSE(adapter_->IsPowered()); | |
478 | |
479 // Install an observer; expect the AdapterPoweredChanged to be called | |
480 // with true, and IsPowered() to return true. | |
481 TestBluetoothAdapterObserver observer(adapter_); | |
482 | |
483 adapter_->SetPowered(true, GetCallback(), GetErrorCallback()); | |
484 EXPECT_EQ(1, callback_count_); | |
485 EXPECT_EQ(0, error_callback_count_); | |
486 | |
487 EXPECT_EQ(1, observer.powered_changed_count()); | |
488 EXPECT_TRUE(observer.last_powered()); | |
489 | |
490 EXPECT_TRUE(adapter_->IsPowered()); | |
491 } | |
492 | |
493 TEST_F(BluetoothChromeOSTest, BecomeNotPowered) { | |
494 GetAdapter(); | |
495 adapter_->SetPowered(true, GetCallback(), GetErrorCallback()); | |
496 EXPECT_EQ(1, callback_count_); | |
497 EXPECT_EQ(0, error_callback_count_); | |
498 callback_count_ = 0; | |
499 | |
500 ASSERT_TRUE(adapter_->IsPowered()); | |
501 | |
502 // Install an observer; expect the AdapterPoweredChanged to be called | |
503 // with false, and IsPowered() to return false. | |
504 TestBluetoothAdapterObserver observer(adapter_); | |
505 | |
506 adapter_->SetPowered(false, GetCallback(), GetErrorCallback()); | |
507 EXPECT_EQ(1, callback_count_); | |
508 EXPECT_EQ(0, error_callback_count_); | |
509 | |
510 EXPECT_EQ(1, observer.powered_changed_count()); | |
511 EXPECT_FALSE(observer.last_powered()); | |
512 | |
513 EXPECT_FALSE(adapter_->IsPowered()); | |
514 } | |
515 | |
516 TEST_F(BluetoothChromeOSTest, SetPoweredWhenNotPresent) { | |
517 GetAdapter(); | |
518 ASSERT_TRUE(adapter_->IsPresent()); | |
519 | |
520 // Install an observer; expect the AdapterPresentChanged to be called | |
521 // with false, and IsPresent() to return false. | |
522 TestBluetoothAdapterObserver observer(adapter_); | |
523 | |
524 fake_bluetooth_adapter_client_->SetVisible(false); | |
525 | |
526 EXPECT_EQ(1, observer.present_changed_count()); | |
527 EXPECT_FALSE(observer.last_present()); | |
528 | |
529 EXPECT_FALSE(adapter_->IsPresent()); | |
530 EXPECT_FALSE(adapter_->IsPowered()); | |
531 | |
532 adapter_->SetPowered(true, GetCallback(), GetErrorCallback()); | |
533 EXPECT_EQ(0, callback_count_); | |
534 EXPECT_EQ(1, error_callback_count_); | |
535 | |
536 EXPECT_EQ(0, observer.powered_changed_count()); | |
537 EXPECT_FALSE(observer.last_powered()); | |
538 | |
539 EXPECT_FALSE(adapter_->IsPowered()); | |
540 } | |
541 | |
542 TEST_F(BluetoothChromeOSTest, ChangeAdapterName) { | |
543 GetAdapter(); | |
544 | |
545 static const std::string new_name(".__."); | |
546 | |
547 adapter_->SetName(new_name, GetCallback(), GetErrorCallback()); | |
548 EXPECT_EQ(1, callback_count_); | |
549 EXPECT_EQ(0, error_callback_count_); | |
550 | |
551 EXPECT_EQ(new_name, adapter_->GetName()); | |
552 } | |
553 | |
554 TEST_F(BluetoothChromeOSTest, ChangeAdapterNameWhenNotPresent) { | |
555 GetAdapter(); | |
556 ASSERT_TRUE(adapter_->IsPresent()); | |
557 | |
558 // Install an observer; expect the AdapterPresentChanged to be called | |
559 // with false, and IsPresent() to return false. | |
560 TestBluetoothAdapterObserver observer(adapter_); | |
561 | |
562 fake_bluetooth_adapter_client_->SetVisible(false); | |
563 | |
564 EXPECT_EQ(1, observer.present_changed_count()); | |
565 EXPECT_FALSE(observer.last_present()); | |
566 | |
567 EXPECT_FALSE(adapter_->IsPresent()); | |
568 EXPECT_FALSE(adapter_->IsPowered()); | |
569 | |
570 adapter_->SetName("^o^", GetCallback(), GetErrorCallback()); | |
571 EXPECT_EQ(0, callback_count_); | |
572 EXPECT_EQ(1, error_callback_count_); | |
573 | |
574 EXPECT_EQ("", adapter_->GetName()); | |
575 } | |
576 | |
577 TEST_F(BluetoothChromeOSTest, BecomeDiscoverable) { | |
578 GetAdapter(); | |
579 ASSERT_FALSE(adapter_->IsDiscoverable()); | |
580 | |
581 // Install an observer; expect the AdapterDiscoverableChanged to be called | |
582 // with true, and IsDiscoverable() to return true. | |
583 TestBluetoothAdapterObserver observer(adapter_); | |
584 | |
585 adapter_->SetDiscoverable(true, GetCallback(), GetErrorCallback()); | |
586 EXPECT_EQ(1, callback_count_); | |
587 EXPECT_EQ(0, error_callback_count_); | |
588 | |
589 EXPECT_EQ(1, observer.discoverable_changed_count()); | |
590 | |
591 EXPECT_TRUE(adapter_->IsDiscoverable()); | |
592 } | |
593 | |
594 TEST_F(BluetoothChromeOSTest, BecomeNotDiscoverable) { | |
595 GetAdapter(); | |
596 adapter_->SetDiscoverable(true, GetCallback(), GetErrorCallback()); | |
597 EXPECT_EQ(1, callback_count_); | |
598 EXPECT_EQ(0, error_callback_count_); | |
599 callback_count_ = 0; | |
600 | |
601 ASSERT_TRUE(adapter_->IsDiscoverable()); | |
602 | |
603 // Install an observer; expect the AdapterDiscoverableChanged to be called | |
604 // with false, and IsDiscoverable() to return false. | |
605 TestBluetoothAdapterObserver observer(adapter_); | |
606 | |
607 adapter_->SetDiscoverable(false, GetCallback(), GetErrorCallback()); | |
608 EXPECT_EQ(1, callback_count_); | |
609 EXPECT_EQ(0, error_callback_count_); | |
610 | |
611 EXPECT_EQ(1, observer.discoverable_changed_count()); | |
612 | |
613 EXPECT_FALSE(adapter_->IsDiscoverable()); | |
614 } | |
615 | |
616 TEST_F(BluetoothChromeOSTest, SetDiscoverableWhenNotPresent) { | |
617 GetAdapter(); | |
618 ASSERT_TRUE(adapter_->IsPresent()); | |
619 ASSERT_FALSE(adapter_->IsDiscoverable()); | |
620 | |
621 // Install an observer; expect the AdapterDiscoverableChanged to be called | |
622 // with true, and IsDiscoverable() to return true. | |
623 TestBluetoothAdapterObserver observer(adapter_); | |
624 | |
625 fake_bluetooth_adapter_client_->SetVisible(false); | |
626 | |
627 EXPECT_EQ(1, observer.present_changed_count()); | |
628 EXPECT_FALSE(observer.last_present()); | |
629 | |
630 EXPECT_FALSE(adapter_->IsPresent()); | |
631 EXPECT_FALSE(adapter_->IsDiscoverable()); | |
632 | |
633 adapter_->SetDiscoverable(true, GetCallback(), GetErrorCallback()); | |
634 EXPECT_EQ(0, callback_count_); | |
635 EXPECT_EQ(1, error_callback_count_); | |
636 | |
637 EXPECT_EQ(0, observer.discoverable_changed_count()); | |
638 | |
639 EXPECT_FALSE(adapter_->IsDiscoverable()); | |
640 } | |
641 | |
642 TEST_F(BluetoothChromeOSTest, StopDiscovery) { | |
643 GetAdapter(); | |
644 | |
645 adapter_->SetPowered(true, GetCallback(), GetErrorCallback()); | |
646 adapter_->StartDiscoverySession( | |
647 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback, | |
648 base::Unretained(this)), | |
649 GetErrorCallback()); | |
650 message_loop_.Run(); | |
651 EXPECT_EQ(2, callback_count_); | |
652 EXPECT_EQ(0, error_callback_count_); | |
653 callback_count_ = 0; | |
654 | |
655 ASSERT_TRUE(adapter_->IsPowered()); | |
656 ASSERT_TRUE(adapter_->IsDiscovering()); | |
657 ASSERT_EQ((size_t)1, discovery_sessions_.size()); | |
658 ASSERT_TRUE(discovery_sessions_[0]->IsActive()); | |
659 | |
660 // Install an observer; aside from the callback, expect the | |
661 // AdapterDiscoveringChanged method to be called and no longer to be | |
662 // discovering, | |
663 TestBluetoothAdapterObserver observer(adapter_); | |
664 | |
665 discovery_sessions_[0]->Stop(GetCallback(), GetErrorCallback()); | |
666 message_loop_.Run(); | |
667 EXPECT_EQ(1, callback_count_); | |
668 EXPECT_EQ(0, error_callback_count_); | |
669 | |
670 EXPECT_EQ(1, observer.discovering_changed_count()); | |
671 EXPECT_FALSE(observer.last_discovering()); | |
672 | |
673 EXPECT_FALSE(adapter_->IsDiscovering()); | |
674 discovery_sessions_.clear(); | |
675 callback_count_ = 0; | |
676 | |
677 // Test that the Stop callbacks get called even if the | |
678 // BluetoothDiscoverySession objects gets deleted | |
679 adapter_->SetPowered(true, GetCallback(), GetErrorCallback()); | |
680 adapter_->StartDiscoverySession( | |
681 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback, | |
682 base::Unretained(this)), | |
683 GetErrorCallback()); | |
684 message_loop_.Run(); | |
685 EXPECT_EQ(2, callback_count_); | |
686 EXPECT_EQ(0, error_callback_count_); | |
687 callback_count_ = 0; | |
688 ASSERT_TRUE(adapter_->IsPowered()); | |
689 ASSERT_TRUE(adapter_->IsDiscovering()); | |
690 ASSERT_EQ((size_t)1, discovery_sessions_.size()); | |
691 ASSERT_TRUE(discovery_sessions_[0]->IsActive()); | |
692 | |
693 discovery_sessions_[0]->Stop(GetCallback(), GetErrorCallback()); | |
694 discovery_sessions_.clear(); | |
695 | |
696 message_loop_.Run(); | |
697 EXPECT_EQ(1, callback_count_); | |
698 EXPECT_EQ(0, error_callback_count_); | |
699 } | |
700 | |
701 TEST_F(BluetoothChromeOSTest, Discovery) { | |
702 // Test a simulated discovery session. | |
703 fake_bluetooth_device_client_->SetSimulationIntervalMs(10); | |
704 GetAdapter(); | |
705 | |
706 TestBluetoothAdapterObserver observer(adapter_); | |
707 | |
708 adapter_->SetPowered(true, GetCallback(), GetErrorCallback()); | |
709 adapter_->StartDiscoverySession( | |
710 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback, | |
711 base::Unretained(this)), | |
712 GetErrorCallback()); | |
713 message_loop_.Run(); | |
714 EXPECT_EQ(2, callback_count_); | |
715 EXPECT_EQ(0, error_callback_count_); | |
716 callback_count_ = 0; | |
717 | |
718 ASSERT_TRUE(adapter_->IsPowered()); | |
719 ASSERT_TRUE(adapter_->IsDiscovering()); | |
720 ASSERT_EQ((size_t)1, discovery_sessions_.size()); | |
721 ASSERT_TRUE(discovery_sessions_[0]->IsActive()); | |
722 | |
723 // First two devices to appear. | |
724 message_loop_.Run(); | |
725 | |
726 EXPECT_EQ(2, observer.device_added_count()); | |
727 EXPECT_EQ(bluez::FakeBluetoothDeviceClient::kLowEnergyAddress, | |
728 observer.last_device_address()); | |
729 | |
730 // Next we should get another two devices... | |
731 message_loop_.Run(); | |
732 EXPECT_EQ(4, observer.device_added_count()); | |
733 | |
734 // Okay, let's run forward until a device is actually removed... | |
735 while (!observer.device_removed_count()) | |
736 message_loop_.Run(); | |
737 | |
738 EXPECT_EQ(1, observer.device_removed_count()); | |
739 EXPECT_EQ(bluez::FakeBluetoothDeviceClient::kVanishingDeviceAddress, | |
740 observer.last_device_address()); | |
741 } | |
742 | |
743 TEST_F(BluetoothChromeOSTest, PoweredAndDiscovering) { | |
744 GetAdapter(); | |
745 adapter_->SetPowered(true, GetCallback(), GetErrorCallback()); | |
746 adapter_->StartDiscoverySession( | |
747 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback, | |
748 base::Unretained(this)), | |
749 GetErrorCallback()); | |
750 message_loop_.Run(); | |
751 EXPECT_EQ(2, callback_count_); | |
752 EXPECT_EQ(0, error_callback_count_); | |
753 callback_count_ = 0; | |
754 ASSERT_EQ((size_t)1, discovery_sessions_.size()); | |
755 ASSERT_TRUE(discovery_sessions_[0]->IsActive()); | |
756 | |
757 // Stop the timers that the simulation uses | |
758 fake_bluetooth_device_client_->EndDiscoverySimulation( | |
759 dbus::ObjectPath(bluez::FakeBluetoothAdapterClient::kAdapterPath)); | |
760 | |
761 ASSERT_TRUE(adapter_->IsPowered()); | |
762 ASSERT_TRUE(adapter_->IsDiscovering()); | |
763 | |
764 fake_bluetooth_adapter_client_->SetVisible(false); | |
765 ASSERT_FALSE(adapter_->IsPresent()); | |
766 ASSERT_FALSE(discovery_sessions_[0]->IsActive()); | |
767 | |
768 // Install an observer; expect the AdapterPresentChanged, | |
769 // AdapterPoweredChanged and AdapterDiscoveringChanged methods to be called | |
770 // with true, and IsPresent(), IsPowered() and IsDiscovering() to all | |
771 // return true. | |
772 TestBluetoothAdapterObserver observer(adapter_); | |
773 | |
774 fake_bluetooth_adapter_client_->SetVisible(true); | |
775 | |
776 EXPECT_EQ(1, observer.present_changed_count()); | |
777 EXPECT_TRUE(observer.last_present()); | |
778 EXPECT_TRUE(adapter_->IsPresent()); | |
779 | |
780 EXPECT_EQ(1, observer.powered_changed_count()); | |
781 EXPECT_TRUE(observer.last_powered()); | |
782 EXPECT_TRUE(adapter_->IsPowered()); | |
783 | |
784 EXPECT_EQ(1, observer.discovering_changed_count()); | |
785 EXPECT_TRUE(observer.last_discovering()); | |
786 EXPECT_TRUE(adapter_->IsDiscovering()); | |
787 | |
788 observer.Reset(); | |
789 | |
790 // Now mark the adapter not present again. Expect the methods to be called | |
791 // again, to reset the properties back to false | |
792 fake_bluetooth_adapter_client_->SetVisible(false); | |
793 | |
794 EXPECT_EQ(1, observer.present_changed_count()); | |
795 EXPECT_FALSE(observer.last_present()); | |
796 EXPECT_FALSE(adapter_->IsPresent()); | |
797 | |
798 EXPECT_EQ(1, observer.powered_changed_count()); | |
799 EXPECT_FALSE(observer.last_powered()); | |
800 EXPECT_FALSE(adapter_->IsPowered()); | |
801 | |
802 EXPECT_EQ(1, observer.discovering_changed_count()); | |
803 EXPECT_FALSE(observer.last_discovering()); | |
804 EXPECT_FALSE(adapter_->IsDiscovering()); | |
805 } | |
806 | |
807 // This unit test asserts that the basic reference counting logic works | |
808 // correctly for discovery requests done via the BluetoothAdapter. | |
809 TEST_F(BluetoothChromeOSTest, MultipleDiscoverySessions) { | |
810 GetAdapter(); | |
811 adapter_->SetPowered(true, GetCallback(), GetErrorCallback()); | |
812 EXPECT_EQ(1, callback_count_); | |
813 EXPECT_EQ(0, error_callback_count_); | |
814 EXPECT_TRUE(adapter_->IsPowered()); | |
815 callback_count_ = 0; | |
816 | |
817 TestBluetoothAdapterObserver observer(adapter_); | |
818 | |
819 EXPECT_EQ(0, observer.discovering_changed_count()); | |
820 EXPECT_FALSE(observer.last_discovering()); | |
821 EXPECT_FALSE(adapter_->IsDiscovering()); | |
822 | |
823 // Request device discovery 3 times. | |
824 for (int i = 0; i < 3; i++) { | |
825 adapter_->StartDiscoverySession( | |
826 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback, | |
827 base::Unretained(this)), | |
828 GetErrorCallback()); | |
829 } | |
830 // Run only once, as there should have been one D-Bus call. | |
831 message_loop_.Run(); | |
832 | |
833 // The observer should have received the discovering changed event exactly | |
834 // once, the success callback should have been called 3 times and the adapter | |
835 // should be discovering. | |
836 EXPECT_EQ(1, observer.discovering_changed_count()); | |
837 EXPECT_EQ(3, callback_count_); | |
838 EXPECT_EQ(0, error_callback_count_); | |
839 EXPECT_TRUE(observer.last_discovering()); | |
840 EXPECT_TRUE(adapter_->IsDiscovering()); | |
841 ASSERT_EQ((size_t)3, discovery_sessions_.size()); | |
842 | |
843 // Request to stop discovery twice. | |
844 for (int i = 0; i < 2; i++) { | |
845 discovery_sessions_[i]->Stop(GetCallback(), GetErrorCallback()); | |
846 } | |
847 | |
848 // The observer should have received no additional discovering changed events, | |
849 // the success callback should have been called 2 times and the adapter should | |
850 // still be discovering. | |
851 EXPECT_EQ(1, observer.discovering_changed_count()); | |
852 EXPECT_EQ(5, callback_count_); | |
853 EXPECT_EQ(0, error_callback_count_); | |
854 EXPECT_TRUE(observer.last_discovering()); | |
855 EXPECT_TRUE(adapter_->IsDiscovering()); | |
856 EXPECT_TRUE(adapter_->IsDiscovering()); | |
857 EXPECT_FALSE(discovery_sessions_[0]->IsActive()); | |
858 EXPECT_FALSE(discovery_sessions_[1]->IsActive()); | |
859 EXPECT_TRUE(discovery_sessions_[2]->IsActive()); | |
860 | |
861 // Request device discovery 3 times. | |
862 for (int i = 0; i < 3; i++) { | |
863 adapter_->StartDiscoverySession( | |
864 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback, | |
865 base::Unretained(this)), | |
866 GetErrorCallback()); | |
867 } | |
868 | |
869 // The observer should have received no additional discovering changed events, | |
870 // the success callback should have been called 3 times and the adapter should | |
871 // still be discovering. | |
872 EXPECT_EQ(1, observer.discovering_changed_count()); | |
873 EXPECT_EQ(8, callback_count_); | |
874 EXPECT_EQ(0, error_callback_count_); | |
875 EXPECT_TRUE(observer.last_discovering()); | |
876 EXPECT_TRUE(adapter_->IsDiscovering()); | |
877 ASSERT_EQ((size_t)6, discovery_sessions_.size()); | |
878 | |
879 // Request to stop discovery 4 times. | |
880 for (int i = 2; i < 6; i++) { | |
881 discovery_sessions_[i]->Stop(GetCallback(), GetErrorCallback()); | |
882 } | |
883 // Run only once, as there should have been one D-Bus call. | |
884 message_loop_.Run(); | |
885 | |
886 // The observer should have received the discovering changed event exactly | |
887 // once, the success callback should have been called 4 times and the adapter | |
888 // should no longer be discovering. | |
889 EXPECT_EQ(2, observer.discovering_changed_count()); | |
890 EXPECT_EQ(12, callback_count_); | |
891 EXPECT_EQ(0, error_callback_count_); | |
892 EXPECT_FALSE(observer.last_discovering()); | |
893 EXPECT_FALSE(adapter_->IsDiscovering()); | |
894 | |
895 // All discovery sessions should be inactive. | |
896 for (int i = 0; i < 6; i++) | |
897 EXPECT_FALSE(discovery_sessions_[i]->IsActive()); | |
898 | |
899 // Request to stop discovery on of the inactive sessions. | |
900 discovery_sessions_[0]->Stop(GetCallback(), GetErrorCallback()); | |
901 | |
902 // The call should have failed. | |
903 EXPECT_EQ(2, observer.discovering_changed_count()); | |
904 EXPECT_EQ(12, callback_count_); | |
905 EXPECT_EQ(1, error_callback_count_); | |
906 EXPECT_FALSE(observer.last_discovering()); | |
907 EXPECT_FALSE(adapter_->IsDiscovering()); | |
908 } | |
909 | |
910 // This unit test asserts that the reference counting logic works correctly in | |
911 // the cases when the adapter gets reset and D-Bus calls are made outside of | |
912 // the BluetoothAdapter. | |
913 TEST_F(BluetoothChromeOSTest, | |
914 UnexpectedChangesDuringMultipleDiscoverySessions) { | |
915 GetAdapter(); | |
916 adapter_->SetPowered(true, GetCallback(), GetErrorCallback()); | |
917 EXPECT_EQ(1, callback_count_); | |
918 EXPECT_EQ(0, error_callback_count_); | |
919 EXPECT_TRUE(adapter_->IsPowered()); | |
920 callback_count_ = 0; | |
921 | |
922 TestBluetoothAdapterObserver observer(adapter_); | |
923 | |
924 EXPECT_EQ(0, observer.discovering_changed_count()); | |
925 EXPECT_FALSE(observer.last_discovering()); | |
926 EXPECT_FALSE(adapter_->IsDiscovering()); | |
927 | |
928 // Request device discovery 3 times. | |
929 for (int i = 0; i < 3; i++) { | |
930 adapter_->StartDiscoverySession( | |
931 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback, | |
932 base::Unretained(this)), | |
933 GetErrorCallback()); | |
934 } | |
935 // Run only once, as there should have been one D-Bus call. | |
936 message_loop_.Run(); | |
937 | |
938 // The observer should have received the discovering changed event exactly | |
939 // once, the success callback should have been called 3 times and the adapter | |
940 // should be discovering. | |
941 EXPECT_EQ(1, observer.discovering_changed_count()); | |
942 EXPECT_EQ(3, callback_count_); | |
943 EXPECT_EQ(0, error_callback_count_); | |
944 EXPECT_TRUE(observer.last_discovering()); | |
945 EXPECT_TRUE(adapter_->IsDiscovering()); | |
946 ASSERT_EQ((size_t)3, discovery_sessions_.size()); | |
947 | |
948 for (int i = 0; i < 3; i++) | |
949 EXPECT_TRUE(discovery_sessions_[i]->IsActive()); | |
950 | |
951 // Stop the timers that the simulation uses | |
952 fake_bluetooth_device_client_->EndDiscoverySimulation( | |
953 dbus::ObjectPath(bluez::FakeBluetoothAdapterClient::kAdapterPath)); | |
954 | |
955 ASSERT_TRUE(adapter_->IsPowered()); | |
956 ASSERT_TRUE(adapter_->IsDiscovering()); | |
957 | |
958 // Stop device discovery behind the adapter. The adapter and the observer | |
959 // should be notified of the change and the reference count should be reset. | |
960 // Even though bluez::FakeBluetoothAdapterClient does its own reference | |
961 // counting and | |
962 // we called 3 BluetoothAdapter::StartDiscoverySession 3 times, the | |
963 // bluez::FakeBluetoothAdapterClient's count should be only 1 and a single | |
964 // call to | |
965 // bluez::FakeBluetoothAdapterClient::StopDiscovery should work. | |
966 fake_bluetooth_adapter_client_->StopDiscovery( | |
967 dbus::ObjectPath(bluez::FakeBluetoothAdapterClient::kAdapterPath), | |
968 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback, | |
969 base::Unretained(this))); | |
970 message_loop_.Run(); | |
971 EXPECT_EQ(2, observer.discovering_changed_count()); | |
972 EXPECT_EQ(4, callback_count_); | |
973 EXPECT_EQ(0, error_callback_count_); | |
974 EXPECT_FALSE(observer.last_discovering()); | |
975 EXPECT_FALSE(adapter_->IsDiscovering()); | |
976 | |
977 // All discovery session instances should have been updated. | |
978 for (int i = 0; i < 3; i++) | |
979 EXPECT_FALSE(discovery_sessions_[i]->IsActive()); | |
980 discovery_sessions_.clear(); | |
981 | |
982 // It should be possible to successfully start discovery. | |
983 for (int i = 0; i < 2; i++) { | |
984 adapter_->StartDiscoverySession( | |
985 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback, | |
986 base::Unretained(this)), | |
987 GetErrorCallback()); | |
988 } | |
989 // Run only once, as there should have been one D-Bus call. | |
990 message_loop_.Run(); | |
991 EXPECT_EQ(3, observer.discovering_changed_count()); | |
992 EXPECT_EQ(6, callback_count_); | |
993 EXPECT_EQ(0, error_callback_count_); | |
994 EXPECT_TRUE(observer.last_discovering()); | |
995 EXPECT_TRUE(adapter_->IsDiscovering()); | |
996 ASSERT_EQ((size_t)2, discovery_sessions_.size()); | |
997 | |
998 for (int i = 0; i < 2; i++) | |
999 EXPECT_TRUE(discovery_sessions_[i]->IsActive()); | |
1000 | |
1001 fake_bluetooth_device_client_->EndDiscoverySimulation( | |
1002 dbus::ObjectPath(bluez::FakeBluetoothAdapterClient::kAdapterPath)); | |
1003 | |
1004 // Make the adapter disappear and appear. This will make it come back as | |
1005 // discovering. When this happens, the reference count should become and | |
1006 // remain 0 as no new request was made through the BluetoothAdapter. | |
1007 fake_bluetooth_adapter_client_->SetVisible(false); | |
1008 ASSERT_FALSE(adapter_->IsPresent()); | |
1009 EXPECT_EQ(4, observer.discovering_changed_count()); | |
1010 EXPECT_EQ(6, callback_count_); | |
1011 EXPECT_EQ(0, error_callback_count_); | |
1012 EXPECT_FALSE(observer.last_discovering()); | |
1013 EXPECT_FALSE(adapter_->IsDiscovering()); | |
1014 | |
1015 for (int i = 0; i < 2; i++) | |
1016 EXPECT_FALSE(discovery_sessions_[i]->IsActive()); | |
1017 discovery_sessions_.clear(); | |
1018 | |
1019 fake_bluetooth_adapter_client_->SetVisible(true); | |
1020 ASSERT_TRUE(adapter_->IsPresent()); | |
1021 EXPECT_EQ(5, observer.discovering_changed_count()); | |
1022 EXPECT_EQ(6, callback_count_); | |
1023 EXPECT_EQ(0, error_callback_count_); | |
1024 EXPECT_TRUE(observer.last_discovering()); | |
1025 EXPECT_TRUE(adapter_->IsDiscovering()); | |
1026 | |
1027 // Start and stop discovery. At this point, bluez::FakeBluetoothAdapterClient | |
1028 // has | |
1029 // a reference count that is equal to 1. Pretend that this was done by an | |
1030 // application other than us. Starting and stopping discovery will succeed | |
1031 // but it won't cause the discovery state to change. | |
1032 adapter_->StartDiscoverySession( | |
1033 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback, | |
1034 base::Unretained(this)), | |
1035 GetErrorCallback()); | |
1036 message_loop_.Run(); // Run the loop, as there should have been a D-Bus call. | |
1037 EXPECT_EQ(5, observer.discovering_changed_count()); | |
1038 EXPECT_EQ(7, callback_count_); | |
1039 EXPECT_EQ(0, error_callback_count_); | |
1040 EXPECT_TRUE(observer.last_discovering()); | |
1041 EXPECT_TRUE(adapter_->IsDiscovering()); | |
1042 ASSERT_EQ((size_t)1, discovery_sessions_.size()); | |
1043 EXPECT_TRUE(discovery_sessions_[0]->IsActive()); | |
1044 | |
1045 discovery_sessions_[0]->Stop(GetCallback(), GetErrorCallback()); | |
1046 message_loop_.Run(); // Run the loop, as there should have been a D-Bus call. | |
1047 EXPECT_EQ(5, observer.discovering_changed_count()); | |
1048 EXPECT_EQ(8, callback_count_); | |
1049 EXPECT_EQ(0, error_callback_count_); | |
1050 EXPECT_TRUE(observer.last_discovering()); | |
1051 EXPECT_TRUE(adapter_->IsDiscovering()); | |
1052 EXPECT_FALSE(discovery_sessions_[0]->IsActive()); | |
1053 discovery_sessions_.clear(); | |
1054 | |
1055 // Start discovery again. | |
1056 adapter_->StartDiscoverySession( | |
1057 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback, | |
1058 base::Unretained(this)), | |
1059 GetErrorCallback()); | |
1060 message_loop_.Run(); // Run the loop, as there should have been a D-Bus call. | |
1061 EXPECT_EQ(5, observer.discovering_changed_count()); | |
1062 EXPECT_EQ(9, callback_count_); | |
1063 EXPECT_EQ(0, error_callback_count_); | |
1064 EXPECT_TRUE(observer.last_discovering()); | |
1065 EXPECT_TRUE(adapter_->IsDiscovering()); | |
1066 ASSERT_EQ((size_t)1, discovery_sessions_.size()); | |
1067 EXPECT_TRUE(discovery_sessions_[0]->IsActive()); | |
1068 | |
1069 // Stop discovery via D-Bus. The fake client's reference count will drop but | |
1070 // the discovery state won't change since our BluetoothAdapter also just | |
1071 // requested it via D-Bus. | |
1072 fake_bluetooth_adapter_client_->StopDiscovery( | |
1073 dbus::ObjectPath(bluez::FakeBluetoothAdapterClient::kAdapterPath), | |
1074 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback, | |
1075 base::Unretained(this))); | |
1076 message_loop_.Run(); | |
1077 EXPECT_EQ(5, observer.discovering_changed_count()); | |
1078 EXPECT_EQ(10, callback_count_); | |
1079 EXPECT_EQ(0, error_callback_count_); | |
1080 EXPECT_TRUE(observer.last_discovering()); | |
1081 EXPECT_TRUE(adapter_->IsDiscovering()); | |
1082 | |
1083 // Now end the discovery session. This should change the adapter's discovery | |
1084 // state. | |
1085 discovery_sessions_[0]->Stop(GetCallback(), GetErrorCallback()); | |
1086 message_loop_.Run(); | |
1087 EXPECT_EQ(6, observer.discovering_changed_count()); | |
1088 EXPECT_EQ(11, callback_count_); | |
1089 EXPECT_EQ(0, error_callback_count_); | |
1090 EXPECT_FALSE(observer.last_discovering()); | |
1091 EXPECT_FALSE(adapter_->IsDiscovering()); | |
1092 EXPECT_FALSE(discovery_sessions_[0]->IsActive()); | |
1093 } | |
1094 | |
1095 TEST_F(BluetoothChromeOSTest, InvalidatedDiscoverySessions) { | |
1096 GetAdapter(); | |
1097 adapter_->SetPowered(true, GetCallback(), GetErrorCallback()); | |
1098 EXPECT_EQ(1, callback_count_); | |
1099 EXPECT_EQ(0, error_callback_count_); | |
1100 EXPECT_TRUE(adapter_->IsPowered()); | |
1101 callback_count_ = 0; | |
1102 | |
1103 TestBluetoothAdapterObserver observer(adapter_); | |
1104 | |
1105 EXPECT_EQ(0, observer.discovering_changed_count()); | |
1106 EXPECT_FALSE(observer.last_discovering()); | |
1107 EXPECT_FALSE(adapter_->IsDiscovering()); | |
1108 | |
1109 // Request device discovery 3 times. | |
1110 for (int i = 0; i < 3; i++) { | |
1111 adapter_->StartDiscoverySession( | |
1112 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback, | |
1113 base::Unretained(this)), | |
1114 GetErrorCallback()); | |
1115 } | |
1116 // Run only once, as there should have been one D-Bus call. | |
1117 message_loop_.Run(); | |
1118 | |
1119 // The observer should have received the discovering changed event exactly | |
1120 // once, the success callback should have been called 3 times and the adapter | |
1121 // should be discovering. | |
1122 EXPECT_EQ(1, observer.discovering_changed_count()); | |
1123 EXPECT_EQ(3, callback_count_); | |
1124 EXPECT_EQ(0, error_callback_count_); | |
1125 EXPECT_TRUE(observer.last_discovering()); | |
1126 EXPECT_TRUE(adapter_->IsDiscovering()); | |
1127 ASSERT_EQ((size_t)3, discovery_sessions_.size()); | |
1128 | |
1129 for (int i = 0; i < 3; i++) | |
1130 EXPECT_TRUE(discovery_sessions_[i]->IsActive()); | |
1131 | |
1132 // Stop the timers that the simulation uses | |
1133 fake_bluetooth_device_client_->EndDiscoverySimulation( | |
1134 dbus::ObjectPath(bluez::FakeBluetoothAdapterClient::kAdapterPath)); | |
1135 | |
1136 ASSERT_TRUE(adapter_->IsPowered()); | |
1137 ASSERT_TRUE(adapter_->IsDiscovering()); | |
1138 | |
1139 // Delete all but one discovery session. | |
1140 discovery_sessions_.pop_back(); | |
1141 discovery_sessions_.pop_back(); | |
1142 ASSERT_EQ((size_t)1, discovery_sessions_.size()); | |
1143 EXPECT_TRUE(discovery_sessions_[0]->IsActive()); | |
1144 EXPECT_TRUE(adapter_->IsDiscovering()); | |
1145 | |
1146 // Stop device discovery behind the adapter. The one active discovery session | |
1147 // should become inactive, but more importantly, we shouldn't run into any | |
1148 // memory errors as the sessions that we explicitly deleted should get | |
1149 // cleaned up. | |
1150 fake_bluetooth_adapter_client_->StopDiscovery( | |
1151 dbus::ObjectPath(bluez::FakeBluetoothAdapterClient::kAdapterPath), | |
1152 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback, | |
1153 base::Unretained(this))); | |
1154 message_loop_.Run(); | |
1155 EXPECT_EQ(2, observer.discovering_changed_count()); | |
1156 EXPECT_EQ(4, callback_count_); | |
1157 EXPECT_EQ(0, error_callback_count_); | |
1158 EXPECT_FALSE(observer.last_discovering()); | |
1159 EXPECT_FALSE(adapter_->IsDiscovering()); | |
1160 EXPECT_FALSE(discovery_sessions_[0]->IsActive()); | |
1161 } | |
1162 | |
1163 TEST_F(BluetoothChromeOSTest, QueuedDiscoveryRequests) { | |
1164 GetAdapter(); | |
1165 | |
1166 adapter_->SetPowered(true, GetCallback(), GetErrorCallback()); | |
1167 EXPECT_EQ(1, callback_count_); | |
1168 EXPECT_EQ(0, error_callback_count_); | |
1169 EXPECT_TRUE(adapter_->IsPowered()); | |
1170 callback_count_ = 0; | |
1171 | |
1172 TestBluetoothAdapterObserver observer(adapter_); | |
1173 | |
1174 EXPECT_EQ(0, observer.discovering_changed_count()); | |
1175 EXPECT_FALSE(observer.last_discovering()); | |
1176 EXPECT_FALSE(adapter_->IsDiscovering()); | |
1177 | |
1178 // Request to start discovery. The call should be pending. | |
1179 adapter_->StartDiscoverySession( | |
1180 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback, | |
1181 base::Unretained(this)), | |
1182 GetErrorCallback()); | |
1183 EXPECT_EQ(0, callback_count_); | |
1184 | |
1185 fake_bluetooth_device_client_->EndDiscoverySimulation( | |
1186 dbus::ObjectPath(bluez::FakeBluetoothAdapterClient::kAdapterPath)); | |
1187 | |
1188 // The underlying adapter has started discovery, but our call hasn't returned | |
1189 // yet. | |
1190 EXPECT_EQ(1, observer.discovering_changed_count()); | |
1191 EXPECT_TRUE(observer.last_discovering()); | |
1192 EXPECT_TRUE(adapter_->IsDiscovering()); | |
1193 EXPECT_TRUE(discovery_sessions_.empty()); | |
1194 | |
1195 // Request to start discovery twice. These should get queued and there should | |
1196 // be no change in state. | |
1197 for (int i = 0; i < 2; i++) { | |
1198 adapter_->StartDiscoverySession( | |
1199 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback, | |
1200 base::Unretained(this)), | |
1201 GetErrorCallback()); | |
1202 } | |
1203 EXPECT_EQ(0, callback_count_); | |
1204 EXPECT_EQ(0, error_callback_count_); | |
1205 EXPECT_EQ(1, observer.discovering_changed_count()); | |
1206 EXPECT_TRUE(observer.last_discovering()); | |
1207 EXPECT_TRUE(adapter_->IsDiscovering()); | |
1208 EXPECT_TRUE(discovery_sessions_.empty()); | |
1209 | |
1210 // Process the pending call. The queued calls should execute and the discovery | |
1211 // session reference count should increase. | |
1212 message_loop_.Run(); | |
1213 EXPECT_EQ(3, callback_count_); | |
1214 EXPECT_EQ(0, error_callback_count_); | |
1215 EXPECT_EQ(1, observer.discovering_changed_count()); | |
1216 EXPECT_TRUE(observer.last_discovering()); | |
1217 EXPECT_TRUE(adapter_->IsDiscovering()); | |
1218 ASSERT_EQ((size_t)3, discovery_sessions_.size()); | |
1219 | |
1220 // Verify the reference count by removing sessions 3 times. The last request | |
1221 // should remain pending. | |
1222 for (int i = 0; i < 3; i++) { | |
1223 discovery_sessions_[i]->Stop(GetCallback(), GetErrorCallback()); | |
1224 } | |
1225 EXPECT_EQ(5, callback_count_); | |
1226 EXPECT_EQ(0, error_callback_count_); | |
1227 EXPECT_EQ(2, observer.discovering_changed_count()); | |
1228 EXPECT_FALSE(observer.last_discovering()); | |
1229 EXPECT_FALSE(adapter_->IsDiscovering()); | |
1230 EXPECT_FALSE(discovery_sessions_[0]->IsActive()); | |
1231 EXPECT_FALSE(discovery_sessions_[1]->IsActive()); | |
1232 EXPECT_TRUE(discovery_sessions_[2]->IsActive()); | |
1233 | |
1234 // Request to stop the session whose call is pending should fail. | |
1235 discovery_sessions_[2]->Stop(GetCallback(), GetErrorCallback()); | |
1236 EXPECT_EQ(5, callback_count_); | |
1237 EXPECT_EQ(1, error_callback_count_); | |
1238 EXPECT_EQ(2, observer.discovering_changed_count()); | |
1239 EXPECT_FALSE(observer.last_discovering()); | |
1240 EXPECT_FALSE(adapter_->IsDiscovering()); | |
1241 EXPECT_TRUE(discovery_sessions_[2]->IsActive()); | |
1242 | |
1243 // Request to start should get queued. | |
1244 adapter_->StartDiscoverySession( | |
1245 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback, | |
1246 base::Unretained(this)), | |
1247 GetErrorCallback()); | |
1248 EXPECT_EQ(5, callback_count_); | |
1249 EXPECT_EQ(1, error_callback_count_); | |
1250 EXPECT_EQ(2, observer.discovering_changed_count()); | |
1251 EXPECT_FALSE(observer.last_discovering()); | |
1252 EXPECT_FALSE(adapter_->IsDiscovering()); | |
1253 ASSERT_EQ((size_t)3, discovery_sessions_.size()); | |
1254 | |
1255 // Run the pending request. | |
1256 message_loop_.Run(); | |
1257 EXPECT_EQ(6, callback_count_); | |
1258 EXPECT_EQ(1, error_callback_count_); | |
1259 EXPECT_EQ(3, observer.discovering_changed_count()); | |
1260 EXPECT_TRUE(observer.last_discovering()); | |
1261 EXPECT_TRUE(adapter_->IsDiscovering()); | |
1262 ASSERT_EQ((size_t)3, discovery_sessions_.size()); | |
1263 EXPECT_FALSE(discovery_sessions_[2]->IsActive()); | |
1264 | |
1265 // The queued request to start discovery should have been issued but is still | |
1266 // pending. Run the loop and verify. | |
1267 message_loop_.Run(); | |
1268 EXPECT_EQ(7, callback_count_); | |
1269 EXPECT_EQ(1, error_callback_count_); | |
1270 EXPECT_EQ(3, observer.discovering_changed_count()); | |
1271 EXPECT_TRUE(observer.last_discovering()); | |
1272 EXPECT_TRUE(adapter_->IsDiscovering()); | |
1273 ASSERT_EQ((size_t)4, discovery_sessions_.size()); | |
1274 EXPECT_TRUE(discovery_sessions_[3]->IsActive()); | |
1275 } | |
1276 | |
1277 TEST_F(BluetoothChromeOSTest, StartDiscoverySession) { | |
1278 GetAdapter(); | |
1279 | |
1280 adapter_->SetPowered(true, GetCallback(), GetErrorCallback()); | |
1281 EXPECT_EQ(1, callback_count_); | |
1282 EXPECT_EQ(0, error_callback_count_); | |
1283 EXPECT_TRUE(adapter_->IsPowered()); | |
1284 callback_count_ = 0; | |
1285 | |
1286 TestBluetoothAdapterObserver observer(adapter_); | |
1287 | |
1288 EXPECT_EQ(0, observer.discovering_changed_count()); | |
1289 EXPECT_FALSE(observer.last_discovering()); | |
1290 EXPECT_FALSE(adapter_->IsDiscovering()); | |
1291 EXPECT_TRUE(discovery_sessions_.empty()); | |
1292 | |
1293 // Request a new discovery session. | |
1294 adapter_->StartDiscoverySession( | |
1295 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback, | |
1296 base::Unretained(this)), | |
1297 GetErrorCallback()); | |
1298 message_loop_.Run(); | |
1299 EXPECT_EQ(1, observer.discovering_changed_count()); | |
1300 EXPECT_EQ(1, callback_count_); | |
1301 EXPECT_EQ(0, error_callback_count_); | |
1302 EXPECT_TRUE(observer.last_discovering()); | |
1303 EXPECT_TRUE(adapter_->IsDiscovering()); | |
1304 ASSERT_EQ((size_t)1, discovery_sessions_.size()); | |
1305 EXPECT_TRUE(discovery_sessions_[0]->IsActive()); | |
1306 | |
1307 // Start another session. A new one should be returned in the callback, which | |
1308 // in turn will destroy the previous session. Adapter should still be | |
1309 // discovering and the reference count should be 1. | |
1310 adapter_->StartDiscoverySession( | |
1311 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback, | |
1312 base::Unretained(this)), | |
1313 GetErrorCallback()); | |
1314 message_loop_.Run(); | |
1315 EXPECT_EQ(1, observer.discovering_changed_count()); | |
1316 EXPECT_EQ(2, callback_count_); | |
1317 EXPECT_EQ(0, error_callback_count_); | |
1318 EXPECT_TRUE(observer.last_discovering()); | |
1319 EXPECT_TRUE(adapter_->IsDiscovering()); | |
1320 ASSERT_EQ((size_t)2, discovery_sessions_.size()); | |
1321 EXPECT_TRUE(discovery_sessions_[0]->IsActive()); | |
1322 | |
1323 // Request a new session. | |
1324 adapter_->StartDiscoverySession( | |
1325 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback, | |
1326 base::Unretained(this)), | |
1327 GetErrorCallback()); | |
1328 message_loop_.Run(); | |
1329 EXPECT_EQ(1, observer.discovering_changed_count()); | |
1330 EXPECT_EQ(3, callback_count_); | |
1331 EXPECT_EQ(0, error_callback_count_); | |
1332 EXPECT_TRUE(observer.last_discovering()); | |
1333 EXPECT_TRUE(adapter_->IsDiscovering()); | |
1334 ASSERT_EQ((size_t)3, discovery_sessions_.size()); | |
1335 EXPECT_TRUE(discovery_sessions_[1]->IsActive()); | |
1336 EXPECT_NE(discovery_sessions_[0], discovery_sessions_[1]); | |
1337 | |
1338 // Stop the previous discovery session. The session should end but discovery | |
1339 // should continue. | |
1340 discovery_sessions_[0]->Stop(GetCallback(), GetErrorCallback()); | |
1341 message_loop_.Run(); | |
1342 EXPECT_EQ(1, observer.discovering_changed_count()); | |
1343 EXPECT_EQ(4, callback_count_); | |
1344 EXPECT_EQ(0, error_callback_count_); | |
1345 EXPECT_TRUE(observer.last_discovering()); | |
1346 EXPECT_TRUE(adapter_->IsDiscovering()); | |
1347 ASSERT_EQ((size_t)3, discovery_sessions_.size()); | |
1348 EXPECT_FALSE(discovery_sessions_[0]->IsActive()); | |
1349 EXPECT_TRUE(discovery_sessions_[1]->IsActive()); | |
1350 | |
1351 // Delete the current active session. Discovery should eventually stop. | |
1352 discovery_sessions_.clear(); | |
1353 while (observer.last_discovering()) | |
1354 message_loop_.RunUntilIdle(); | |
1355 | |
1356 EXPECT_EQ(2, observer.discovering_changed_count()); | |
1357 EXPECT_EQ(4, callback_count_); | |
1358 EXPECT_EQ(0, error_callback_count_); | |
1359 EXPECT_FALSE(observer.last_discovering()); | |
1360 EXPECT_FALSE(adapter_->IsDiscovering()); | |
1361 } | |
1362 | |
1363 TEST_F(BluetoothChromeOSTest, SetDiscoveryFilterBeforeStartDiscovery) { | |
1364 // Test a simulated discovery session. | |
1365 fake_bluetooth_device_client_->SetSimulationIntervalMs(10); | |
1366 GetAdapter(); | |
1367 | |
1368 TestBluetoothAdapterObserver observer(adapter_); | |
1369 | |
1370 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter( | |
1371 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE); | |
1372 df->SetRSSI(-60); | |
1373 df->AddUUID(BluetoothUUID("1000")); | |
1374 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter(df); | |
1375 | |
1376 adapter_->SetPowered(true, base::Bind(&BluetoothChromeOSTest::Callback, | |
1377 base::Unretained(this)), | |
1378 base::Bind(&BluetoothChromeOSTest::ErrorCallback, | |
1379 base::Unretained(this))); | |
1380 adapter_->StartDiscoverySessionWithFilter( | |
1381 discovery_filter.Pass(), | |
1382 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback, | |
1383 base::Unretained(this)), | |
1384 base::Bind(&BluetoothChromeOSTest::ErrorCallback, | |
1385 base::Unretained(this))); | |
1386 message_loop_.Run(); | |
1387 EXPECT_EQ(2, callback_count_); | |
1388 EXPECT_EQ(0, error_callback_count_); | |
1389 callback_count_ = 0; | |
1390 | |
1391 ASSERT_TRUE(adapter_->IsPowered()); | |
1392 ASSERT_TRUE(adapter_->IsDiscovering()); | |
1393 ASSERT_EQ((size_t)1, discovery_sessions_.size()); | |
1394 ASSERT_TRUE(discovery_sessions_[0]->IsActive()); | |
1395 ASSERT_TRUE(df->Equals(*discovery_sessions_[0]->GetDiscoveryFilter())); | |
1396 | |
1397 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter(); | |
1398 EXPECT_NE(nullptr, filter); | |
1399 EXPECT_EQ("le", *filter->transport); | |
1400 EXPECT_EQ(-60, *filter->rssi); | |
1401 EXPECT_EQ(nullptr, filter->pathloss.get()); | |
1402 std::vector<std::string> uuids = *filter->uuids; | |
1403 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1000")); | |
1404 | |
1405 discovery_sessions_[0]->Stop( | |
1406 base::Bind(&BluetoothChromeOSTest::Callback, base::Unretained(this)), | |
1407 base::Bind(&BluetoothChromeOSTest::ErrorCallback, | |
1408 base::Unretained(this))); | |
1409 | |
1410 message_loop_.Run(); | |
1411 | |
1412 EXPECT_EQ(1, callback_count_); | |
1413 EXPECT_EQ(0, error_callback_count_); | |
1414 | |
1415 ASSERT_TRUE(adapter_->IsPowered()); | |
1416 ASSERT_FALSE(adapter_->IsDiscovering()); | |
1417 ASSERT_EQ((size_t)1, discovery_sessions_.size()); | |
1418 ASSERT_FALSE(discovery_sessions_[0]->IsActive()); | |
1419 ASSERT_EQ(discovery_sessions_[0]->GetDiscoveryFilter(), | |
1420 (BluetoothDiscoveryFilter*)nullptr); | |
1421 | |
1422 filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter(); | |
1423 EXPECT_EQ(nullptr, filter); | |
1424 } | |
1425 | |
1426 TEST_F(BluetoothChromeOSTest, SetDiscoveryFilterBeforeStartDiscoveryFail) { | |
1427 // Test a simulated discovery session. | |
1428 fake_bluetooth_device_client_->SetSimulationIntervalMs(10); | |
1429 GetAdapter(); | |
1430 | |
1431 TestBluetoothAdapterObserver observer(adapter_); | |
1432 | |
1433 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter( | |
1434 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE); | |
1435 df->SetRSSI(-60); | |
1436 df->AddUUID(BluetoothUUID("1000")); | |
1437 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter(df); | |
1438 | |
1439 adapter_->SetPowered(true, base::Bind(&BluetoothChromeOSTest::Callback, | |
1440 base::Unretained(this)), | |
1441 base::Bind(&BluetoothChromeOSTest::ErrorCallback, | |
1442 base::Unretained(this))); | |
1443 EXPECT_EQ(1, callback_count_); | |
1444 callback_count_ = 0; | |
1445 | |
1446 fake_bluetooth_adapter_client_->MakeSetDiscoveryFilterFail(); | |
1447 | |
1448 adapter_->StartDiscoverySessionWithFilter( | |
1449 discovery_filter.Pass(), | |
1450 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback, | |
1451 base::Unretained(this)), | |
1452 base::Bind(&BluetoothChromeOSTest::ErrorCallback, | |
1453 base::Unretained(this))); | |
1454 | |
1455 message_loop_.Run(); | |
1456 | |
1457 EXPECT_EQ(1, error_callback_count_); | |
1458 error_callback_count_ = 0; | |
1459 | |
1460 ASSERT_TRUE(adapter_->IsPowered()); | |
1461 ASSERT_FALSE(adapter_->IsDiscovering()); | |
1462 ASSERT_EQ((size_t)0, discovery_sessions_.size()); | |
1463 | |
1464 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter(); | |
1465 EXPECT_EQ(nullptr, filter); | |
1466 } | |
1467 | |
1468 // This test queues two requests to StartDiscovery with pre set filter. This | |
1469 // should result in SetDiscoveryFilter, then StartDiscovery, and SetDiscovery | |
1470 // DBus calls | |
1471 TEST_F(BluetoothChromeOSTest, QueuedSetDiscoveryFilterBeforeStartDiscovery) { | |
1472 // Test a simulated discovery session. | |
1473 fake_bluetooth_device_client_->SetSimulationIntervalMs(10); | |
1474 GetAdapter(); | |
1475 | |
1476 TestBluetoothAdapterObserver observer(adapter_); | |
1477 | |
1478 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter( | |
1479 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE); | |
1480 df->SetRSSI(-60); | |
1481 df->AddUUID(BluetoothUUID("1000")); | |
1482 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter(df); | |
1483 | |
1484 BluetoothDiscoveryFilter* df2 = new BluetoothDiscoveryFilter( | |
1485 BluetoothDiscoveryFilter::Transport::TRANSPORT_CLASSIC); | |
1486 df2->SetRSSI(-65); | |
1487 df2->AddUUID(BluetoothUUID("1002")); | |
1488 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter2(df2); | |
1489 | |
1490 adapter_->SetPowered(true, base::Bind(&BluetoothChromeOSTest::Callback, | |
1491 base::Unretained(this)), | |
1492 base::Bind(&BluetoothChromeOSTest::ErrorCallback, | |
1493 base::Unretained(this))); | |
1494 | |
1495 EXPECT_EQ(1, callback_count_); | |
1496 EXPECT_EQ(0, error_callback_count_); | |
1497 callback_count_ = 0; | |
1498 | |
1499 // Queue two requests to start discovery session with filter. | |
1500 adapter_->StartDiscoverySessionWithFilter( | |
1501 discovery_filter.Pass(), | |
1502 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback, | |
1503 base::Unretained(this)), | |
1504 base::Bind(&BluetoothChromeOSTest::ErrorCallback, | |
1505 base::Unretained(this))); | |
1506 | |
1507 adapter_->StartDiscoverySessionWithFilter( | |
1508 discovery_filter2.Pass(), | |
1509 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback, | |
1510 base::Unretained(this)), | |
1511 base::Bind(&BluetoothChromeOSTest::ErrorCallback, | |
1512 base::Unretained(this))); | |
1513 | |
1514 // Run requests, on DBus level there should be call SetDiscoveryFilter, then | |
1515 // StartDiscovery, then SetDiscoveryFilter again. | |
1516 message_loop_.Run(); | |
1517 message_loop_.Run(); | |
1518 | |
1519 EXPECT_EQ(2, callback_count_); | |
1520 EXPECT_EQ(0, error_callback_count_); | |
1521 callback_count_ = 0; | |
1522 | |
1523 ASSERT_TRUE(adapter_->IsPowered()); | |
1524 ASSERT_TRUE(adapter_->IsDiscovering()); | |
1525 ASSERT_EQ((size_t)2, discovery_sessions_.size()); | |
1526 ASSERT_TRUE(discovery_sessions_[0]->IsActive()); | |
1527 ASSERT_TRUE(df->Equals(*discovery_sessions_[0]->GetDiscoveryFilter())); | |
1528 ASSERT_TRUE(discovery_sessions_[1]->IsActive()); | |
1529 ASSERT_TRUE(df2->Equals(*discovery_sessions_[1]->GetDiscoveryFilter())); | |
1530 | |
1531 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter(); | |
1532 EXPECT_NE(nullptr, filter); | |
1533 EXPECT_EQ("auto", *filter->transport); | |
1534 EXPECT_EQ(-65, *filter->rssi); | |
1535 EXPECT_EQ(nullptr, filter->pathloss.get()); | |
1536 auto uuids = *filter->uuids; | |
1537 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1000")); | |
1538 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1002")); | |
1539 | |
1540 discovery_sessions_[0]->Stop( | |
1541 base::Bind(&BluetoothChromeOSTest::Callback, base::Unretained(this)), | |
1542 base::Bind(&BluetoothChromeOSTest::ErrorCallback, | |
1543 base::Unretained(this))); | |
1544 | |
1545 discovery_sessions_[1]->Stop( | |
1546 base::Bind(&BluetoothChromeOSTest::Callback, base::Unretained(this)), | |
1547 base::Bind(&BluetoothChromeOSTest::ErrorCallback, | |
1548 base::Unretained(this))); | |
1549 | |
1550 message_loop_.Run(); | |
1551 | |
1552 EXPECT_EQ(2, callback_count_); | |
1553 EXPECT_EQ(0, error_callback_count_); | |
1554 | |
1555 ASSERT_TRUE(adapter_->IsPowered()); | |
1556 ASSERT_FALSE(adapter_->IsDiscovering()); | |
1557 ASSERT_FALSE(discovery_sessions_[0]->IsActive()); | |
1558 ASSERT_EQ(discovery_sessions_[0]->GetDiscoveryFilter(), | |
1559 (BluetoothDiscoveryFilter*)nullptr); | |
1560 ASSERT_FALSE(discovery_sessions_[1]->IsActive()); | |
1561 ASSERT_EQ(discovery_sessions_[1]->GetDiscoveryFilter(), | |
1562 (BluetoothDiscoveryFilter*)nullptr); | |
1563 | |
1564 filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter(); | |
1565 EXPECT_EQ(nullptr, filter); | |
1566 } | |
1567 | |
1568 // Call StartFilteredDiscovery twice (2nd time while 1st call is still pending). | |
1569 // Make the first SetDiscoveryFilter fail and the second one succeed. It should | |
1570 // end up with one active discovery session. | |
1571 TEST_F(BluetoothChromeOSTest, | |
1572 QueuedSetDiscoveryFilterBeforeStartDiscoveryFail) { | |
1573 // Test a simulated discovery session. | |
1574 fake_bluetooth_device_client_->SetSimulationIntervalMs(10); | |
1575 GetAdapter(); | |
1576 | |
1577 TestBluetoothAdapterObserver observer(adapter_); | |
1578 | |
1579 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter( | |
1580 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE); | |
1581 df->SetRSSI(-60); | |
1582 df->AddUUID(BluetoothUUID("1000")); | |
1583 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter(df); | |
1584 | |
1585 BluetoothDiscoveryFilter* df2 = new BluetoothDiscoveryFilter( | |
1586 BluetoothDiscoveryFilter::Transport::TRANSPORT_CLASSIC); | |
1587 df2->SetRSSI(-65); | |
1588 df2->AddUUID(BluetoothUUID("1002")); | |
1589 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter2(df2); | |
1590 | |
1591 adapter_->SetPowered(true, base::Bind(&BluetoothChromeOSTest::Callback, | |
1592 base::Unretained(this)), | |
1593 base::Bind(&BluetoothChromeOSTest::ErrorCallback, | |
1594 base::Unretained(this))); | |
1595 | |
1596 EXPECT_EQ(1, callback_count_); | |
1597 EXPECT_EQ(0, error_callback_count_); | |
1598 callback_count_ = 0; | |
1599 | |
1600 fake_bluetooth_adapter_client_->MakeSetDiscoveryFilterFail(); | |
1601 | |
1602 // Queue two requests to start discovery session with filter. | |
1603 adapter_->StartDiscoverySessionWithFilter( | |
1604 discovery_filter.Pass(), | |
1605 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback, | |
1606 base::Unretained(this)), | |
1607 base::Bind(&BluetoothChromeOSTest::ErrorCallback, | |
1608 base::Unretained(this))); | |
1609 | |
1610 adapter_->StartDiscoverySessionWithFilter( | |
1611 discovery_filter2.Pass(), | |
1612 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback, | |
1613 base::Unretained(this)), | |
1614 base::Bind(&BluetoothChromeOSTest::ErrorCallback, | |
1615 base::Unretained(this))); | |
1616 | |
1617 message_loop_.Run(); | |
1618 | |
1619 // First request to SetDiscoveryFilter should fail, resulting in no session | |
1620 // being created. | |
1621 EXPECT_EQ(0, callback_count_); | |
1622 EXPECT_EQ(1, error_callback_count_); | |
1623 error_callback_count_ = 0; | |
1624 | |
1625 ASSERT_TRUE(adapter_->IsPowered()); | |
1626 ASSERT_FALSE(adapter_->IsDiscovering()); | |
1627 ASSERT_EQ((size_t)0, discovery_sessions_.size()); | |
1628 | |
1629 message_loop_.Run(); | |
1630 | |
1631 // Second request should succeed | |
1632 EXPECT_EQ(1, callback_count_); | |
1633 EXPECT_EQ(0, error_callback_count_); | |
1634 callback_count_ = 0; | |
1635 | |
1636 ASSERT_TRUE(adapter_->IsDiscovering()); | |
1637 ASSERT_EQ((size_t)1, discovery_sessions_.size()); | |
1638 ASSERT_TRUE(discovery_sessions_[0]->IsActive()); | |
1639 ASSERT_TRUE(df2->Equals(*discovery_sessions_[0]->GetDiscoveryFilter())); | |
1640 | |
1641 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter(); | |
1642 EXPECT_NE(nullptr, filter); | |
1643 EXPECT_EQ("bredr", *filter->transport); | |
1644 EXPECT_EQ(-65, *filter->rssi); | |
1645 EXPECT_EQ(nullptr, filter->pathloss.get()); | |
1646 auto uuids = *filter->uuids; | |
1647 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1002")); | |
1648 | |
1649 discovery_sessions_[0]->Stop( | |
1650 base::Bind(&BluetoothChromeOSTest::Callback, base::Unretained(this)), | |
1651 base::Bind(&BluetoothChromeOSTest::ErrorCallback, | |
1652 base::Unretained(this))); | |
1653 | |
1654 message_loop_.Run(); | |
1655 | |
1656 EXPECT_EQ(1, callback_count_); | |
1657 EXPECT_EQ(0, error_callback_count_); | |
1658 | |
1659 ASSERT_TRUE(adapter_->IsPowered()); | |
1660 ASSERT_FALSE(adapter_->IsDiscovering()); | |
1661 ASSERT_FALSE(discovery_sessions_[0]->IsActive()); | |
1662 ASSERT_EQ(discovery_sessions_[0]->GetDiscoveryFilter(), | |
1663 (BluetoothDiscoveryFilter*)nullptr); | |
1664 | |
1665 filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter(); | |
1666 EXPECT_EQ(nullptr, filter); | |
1667 } | |
1668 | |
1669 TEST_F(BluetoothChromeOSTest, SetDiscoveryFilterAfterStartDiscovery) { | |
1670 // Test a simulated discovery session. | |
1671 fake_bluetooth_device_client_->SetSimulationIntervalMs(10); | |
1672 GetAdapter(); | |
1673 | |
1674 TestBluetoothAdapterObserver observer(adapter_); | |
1675 | |
1676 adapter_->SetPowered(true, base::Bind(&BluetoothChromeOSTest::Callback, | |
1677 base::Unretained(this)), | |
1678 base::Bind(&BluetoothChromeOSTest::ErrorCallback, | |
1679 base::Unretained(this))); | |
1680 adapter_->StartDiscoverySession( | |
1681 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback, | |
1682 base::Unretained(this)), | |
1683 base::Bind(&BluetoothChromeOSTest::ErrorCallback, | |
1684 base::Unretained(this))); | |
1685 message_loop_.Run(); | |
1686 EXPECT_EQ(2, callback_count_); | |
1687 EXPECT_EQ(0, error_callback_count_); | |
1688 callback_count_ = 0; | |
1689 | |
1690 ASSERT_TRUE(adapter_->IsPowered()); | |
1691 ASSERT_TRUE(adapter_->IsDiscovering()); | |
1692 ASSERT_EQ((size_t)1, discovery_sessions_.size()); | |
1693 ASSERT_TRUE(discovery_sessions_[0]->IsActive()); | |
1694 EXPECT_EQ(1, observer.discovering_changed_count()); | |
1695 observer.Reset(); | |
1696 | |
1697 auto null_instance = scoped_ptr<BluetoothDiscoveryFilter>(); | |
1698 null_instance.reset(); | |
1699 ASSERT_EQ(discovery_sessions_[0]->GetDiscoveryFilter(), null_instance.get()); | |
1700 | |
1701 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter(); | |
1702 EXPECT_EQ(nullptr, filter); | |
1703 | |
1704 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter( | |
1705 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE); | |
1706 df->SetRSSI(-60); | |
1707 df->AddUUID(BluetoothUUID("1000")); | |
1708 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter(df); | |
1709 | |
1710 discovery_sessions_[0]->SetDiscoveryFilter( | |
1711 discovery_filter.Pass(), | |
1712 base::Bind(&BluetoothChromeOSTest::Callback, base::Unretained(this)), | |
1713 base::Bind(&BluetoothChromeOSTest::ErrorCallback, | |
1714 base::Unretained(this))); | |
1715 | |
1716 message_loop_.Run(); | |
1717 EXPECT_EQ(1, callback_count_); | |
1718 EXPECT_EQ(0, error_callback_count_); | |
1719 callback_count_ = 0; | |
1720 | |
1721 ASSERT_TRUE(df->Equals(*discovery_sessions_[0]->GetDiscoveryFilter())); | |
1722 | |
1723 filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter(); | |
1724 EXPECT_NE(nullptr, filter); | |
1725 EXPECT_EQ("le", *filter->transport); | |
1726 EXPECT_EQ(-60, *filter->rssi); | |
1727 EXPECT_EQ(nullptr, filter->pathloss.get()); | |
1728 std::vector<std::string> uuids = *filter->uuids; | |
1729 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1000")); | |
1730 | |
1731 discovery_sessions_[0]->Stop( | |
1732 base::Bind(&BluetoothChromeOSTest::Callback, base::Unretained(this)), | |
1733 base::Bind(&BluetoothChromeOSTest::ErrorCallback, | |
1734 base::Unretained(this))); | |
1735 | |
1736 message_loop_.Run(); | |
1737 | |
1738 EXPECT_EQ(1, callback_count_); | |
1739 EXPECT_EQ(0, error_callback_count_); | |
1740 | |
1741 ASSERT_TRUE(adapter_->IsPowered()); | |
1742 ASSERT_FALSE(adapter_->IsDiscovering()); | |
1743 ASSERT_EQ((size_t)1, discovery_sessions_.size()); | |
1744 ASSERT_FALSE(discovery_sessions_[0]->IsActive()); | |
1745 ASSERT_EQ(discovery_sessions_[0]->GetDiscoveryFilter(), | |
1746 (BluetoothDiscoveryFilter*)nullptr); | |
1747 | |
1748 filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter(); | |
1749 EXPECT_EQ(nullptr, filter); | |
1750 } | |
1751 | |
1752 // This unit test asserts that the basic reference counting, and filter merging | |
1753 // works correctly for discovery requests done via the BluetoothAdapter. | |
1754 TEST_F(BluetoothChromeOSTest, SetDiscoveryFilterBeforeStartDiscoveryMultiple) { | |
1755 GetAdapter(); | |
1756 adapter_->SetPowered(true, base::Bind(&BluetoothChromeOSTest::Callback, | |
1757 base::Unretained(this)), | |
1758 base::Bind(&BluetoothChromeOSTest::ErrorCallback, | |
1759 base::Unretained(this))); | |
1760 EXPECT_EQ(1, callback_count_); | |
1761 EXPECT_EQ(0, error_callback_count_); | |
1762 EXPECT_TRUE(adapter_->IsPowered()); | |
1763 callback_count_ = 0; | |
1764 | |
1765 TestBluetoothAdapterObserver observer(adapter_); | |
1766 | |
1767 // Request device discovery with pre-set filter 3 times. | |
1768 for (int i = 0; i < 3; i++) { | |
1769 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter; | |
1770 if (i == 0) { | |
1771 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter( | |
1772 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE); | |
1773 df->SetRSSI(-85); | |
1774 df->AddUUID(BluetoothUUID("1000")); | |
1775 discovery_filter.reset(df); | |
1776 } else if (i == 1) { | |
1777 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter( | |
1778 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE); | |
1779 df->SetRSSI(-60); | |
1780 df->AddUUID(BluetoothUUID("1020")); | |
1781 df->AddUUID(BluetoothUUID("1001")); | |
1782 discovery_filter.reset(df); | |
1783 } else if (i == 2) { | |
1784 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter( | |
1785 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE); | |
1786 df->SetRSSI(-65); | |
1787 df->AddUUID(BluetoothUUID("1020")); | |
1788 df->AddUUID(BluetoothUUID("1003")); | |
1789 discovery_filter.reset(df); | |
1790 } | |
1791 | |
1792 adapter_->StartDiscoverySessionWithFilter( | |
1793 discovery_filter.Pass(), | |
1794 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback, | |
1795 base::Unretained(this)), | |
1796 base::Bind(&BluetoothChromeOSTest::ErrorCallback, | |
1797 base::Unretained(this))); | |
1798 | |
1799 message_loop_.Run(); | |
1800 | |
1801 if (i == 0) { | |
1802 EXPECT_EQ(1, observer.discovering_changed_count()); | |
1803 observer.Reset(); | |
1804 | |
1805 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter(); | |
1806 EXPECT_EQ("le", *filter->transport); | |
1807 EXPECT_EQ(-85, *filter->rssi); | |
1808 EXPECT_EQ(nullptr, filter->pathloss.get()); | |
1809 std::vector<std::string> uuids = *filter->uuids; | |
1810 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1000")); | |
1811 } else if (i == 1) { | |
1812 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter(); | |
1813 EXPECT_EQ("le", *filter->transport); | |
1814 EXPECT_EQ(-85, *filter->rssi); | |
1815 EXPECT_EQ(nullptr, filter->pathloss.get()); | |
1816 std::vector<std::string> uuids = *filter->uuids; | |
1817 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1000")); | |
1818 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1001")); | |
1819 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1020")); | |
1820 } else if (i == 2) { | |
1821 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter(); | |
1822 EXPECT_EQ("le", *filter->transport); | |
1823 EXPECT_EQ(-85, *filter->rssi); | |
1824 EXPECT_EQ(nullptr, filter->pathloss.get()); | |
1825 std::vector<std::string> uuids = *filter->uuids; | |
1826 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1000")); | |
1827 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1001")); | |
1828 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1003")); | |
1829 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1020")); | |
1830 } | |
1831 } | |
1832 | |
1833 // the success callback should have been called 3 times and the adapter should | |
1834 // be discovering. | |
1835 EXPECT_EQ(3, callback_count_); | |
1836 EXPECT_EQ(0, error_callback_count_); | |
1837 EXPECT_TRUE(adapter_->IsDiscovering()); | |
1838 ASSERT_EQ((size_t)3, discovery_sessions_.size()); | |
1839 | |
1840 callback_count_ = 0; | |
1841 // Request to stop discovery twice. | |
1842 for (int i = 0; i < 2; i++) { | |
1843 discovery_sessions_[i]->Stop( | |
1844 base::Bind(&BluetoothChromeOSTest::Callback, base::Unretained(this)), | |
1845 base::Bind(&BluetoothChromeOSTest::ErrorCallback, | |
1846 base::Unretained(this))); | |
1847 message_loop_.Run(); | |
1848 | |
1849 if (i == 0) { | |
1850 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter(); | |
1851 EXPECT_EQ("le", *filter->transport); | |
1852 EXPECT_EQ(-65, *filter->rssi); | |
1853 EXPECT_EQ(nullptr, filter->pathloss.get()); | |
1854 std::vector<std::string> uuids = *filter->uuids; | |
1855 EXPECT_EQ(3UL, uuids.size()); | |
1856 EXPECT_EQ(uuids.end(), std::find(uuids.begin(), uuids.end(), "1000")); | |
1857 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1001")); | |
1858 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1003")); | |
1859 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1020")); | |
1860 } else if (i == 1) { | |
1861 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter(); | |
1862 EXPECT_EQ("le", *filter->transport); | |
1863 EXPECT_EQ(-65, *filter->rssi); | |
1864 EXPECT_EQ(nullptr, filter->pathloss.get()); | |
1865 std::vector<std::string> uuids = *filter->uuids; | |
1866 EXPECT_EQ(2UL, uuids.size()); | |
1867 EXPECT_EQ(uuids.end(), std::find(uuids.begin(), uuids.end(), "1000")); | |
1868 EXPECT_EQ(uuids.end(), std::find(uuids.begin(), uuids.end(), "1001")); | |
1869 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1003")); | |
1870 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1020")); | |
1871 } else if (i == 2) { | |
1872 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter(); | |
1873 EXPECT_EQ("le", *filter->transport); | |
1874 EXPECT_EQ(-65, *filter->rssi); | |
1875 EXPECT_EQ(nullptr, filter->pathloss.get()); | |
1876 std::vector<std::string> uuids = *filter->uuids; | |
1877 EXPECT_EQ(0UL, uuids.size()); | |
1878 } | |
1879 } | |
1880 | |
1881 // The success callback should have been called 2 times and the adapter should | |
1882 // still be discovering. | |
1883 EXPECT_EQ(2, callback_count_); | |
1884 EXPECT_EQ(0, error_callback_count_); | |
1885 EXPECT_TRUE(adapter_->IsDiscovering()); | |
1886 EXPECT_FALSE(discovery_sessions_[0]->IsActive()); | |
1887 EXPECT_FALSE(discovery_sessions_[1]->IsActive()); | |
1888 EXPECT_TRUE(discovery_sessions_[2]->IsActive()); | |
1889 | |
1890 callback_count_ = 0; | |
1891 | |
1892 // Request device discovery 3 times. | |
1893 for (int i = 0; i < 3; i++) { | |
1894 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter; | |
1895 | |
1896 if (i == 0) { | |
1897 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter( | |
1898 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE); | |
1899 df->SetRSSI(-85); | |
1900 df->AddUUID(BluetoothUUID("1000")); | |
1901 discovery_filter.reset(df); | |
1902 } else if (i == 1) { | |
1903 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter( | |
1904 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE); | |
1905 df->SetRSSI(-60); | |
1906 df->AddUUID(BluetoothUUID("1020")); | |
1907 df->AddUUID(BluetoothUUID("1001")); | |
1908 discovery_filter.reset(df); | |
1909 } else if (i == 2) { | |
1910 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter( | |
1911 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE); | |
1912 df->SetRSSI(-65); | |
1913 df->AddUUID(BluetoothUUID("1020")); | |
1914 df->AddUUID(BluetoothUUID("1003")); | |
1915 discovery_filter.reset(df); | |
1916 } | |
1917 | |
1918 adapter_->StartDiscoverySessionWithFilter( | |
1919 discovery_filter.Pass(), | |
1920 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback, | |
1921 base::Unretained(this)), | |
1922 base::Bind(&BluetoothChromeOSTest::ErrorCallback, | |
1923 base::Unretained(this))); | |
1924 | |
1925 // each result in 1 requests. | |
1926 message_loop_.Run(); | |
1927 | |
1928 if (i == 0) { | |
1929 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter(); | |
1930 EXPECT_EQ("le", *filter->transport); | |
1931 EXPECT_EQ(-85, *filter->rssi); | |
1932 EXPECT_EQ(nullptr, filter->pathloss.get()); | |
1933 std::vector<std::string> uuids = *filter->uuids; | |
1934 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1000")); | |
1935 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1003")); | |
1936 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1020")); | |
1937 } else if (i == 1 || i == 2) { | |
1938 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter(); | |
1939 EXPECT_EQ("le", *filter->transport); | |
1940 EXPECT_EQ(-85, *filter->rssi); | |
1941 EXPECT_EQ(nullptr, filter->pathloss.get()); | |
1942 std::vector<std::string> uuids = *filter->uuids; | |
1943 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1000")); | |
1944 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1001")); | |
1945 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1003")); | |
1946 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1020")); | |
1947 } | |
1948 } | |
1949 | |
1950 // The success callback should have been called 3 times and the adapter should | |
1951 // still be discovering. | |
1952 EXPECT_EQ(3, callback_count_); | |
1953 EXPECT_EQ(0, error_callback_count_); | |
1954 EXPECT_TRUE(adapter_->IsDiscovering()); | |
1955 ASSERT_EQ((size_t)6, discovery_sessions_.size()); | |
1956 | |
1957 callback_count_ = 0; | |
1958 // Request to stop discovery 4 times. | |
1959 for (int i = 2; i < 6; i++) { | |
1960 discovery_sessions_[i]->Stop( | |
1961 base::Bind(&BluetoothChromeOSTest::Callback, base::Unretained(this)), | |
1962 base::Bind(&BluetoothChromeOSTest::ErrorCallback, | |
1963 base::Unretained(this))); | |
1964 | |
1965 // filter no 2 is same as filter no 5, so removing it shouldn't cause any | |
1966 // filter update | |
1967 if (i != 2 && i != 5) | |
1968 message_loop_.Run(); | |
1969 } | |
1970 // Run only once, as there should have been one D-Bus call. | |
1971 message_loop_.Run(); | |
1972 | |
1973 // The success callback should have been called 4 times and the adapter should | |
1974 // no longer be discovering. | |
1975 EXPECT_EQ(4, callback_count_); | |
1976 EXPECT_EQ(0, error_callback_count_); | |
1977 EXPECT_FALSE(adapter_->IsDiscovering()); | |
1978 EXPECT_EQ(1, observer.discovering_changed_count()); | |
1979 | |
1980 // All discovery sessions should be inactive. | |
1981 for (int i = 0; i < 6; i++) | |
1982 EXPECT_FALSE(discovery_sessions_[i]->IsActive()); | |
1983 | |
1984 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter(); | |
1985 EXPECT_EQ(nullptr, filter); | |
1986 } | |
1987 | |
1988 // This unit test asserts that filter merging logic works correctly for filtered | |
1989 // discovery requests done via the BluetoothAdapter. | |
1990 TEST_F(BluetoothChromeOSTest, SetDiscoveryFilterMergingTest) { | |
1991 GetAdapter(); | |
1992 adapter_->SetPowered(true, base::Bind(&BluetoothChromeOSTest::Callback, | |
1993 base::Unretained(this)), | |
1994 base::Bind(&BluetoothChromeOSTest::ErrorCallback, | |
1995 base::Unretained(this))); | |
1996 | |
1997 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter( | |
1998 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE); | |
1999 df->SetRSSI(-15); | |
2000 df->AddUUID(BluetoothUUID("1000")); | |
2001 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter(df); | |
2002 | |
2003 adapter_->StartDiscoverySessionWithFilter( | |
2004 discovery_filter.Pass(), | |
2005 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback, | |
2006 base::Unretained(this)), | |
2007 base::Bind(&BluetoothChromeOSTest::ErrorCallback, | |
2008 base::Unretained(this))); | |
2009 | |
2010 message_loop_.Run(); | |
2011 | |
2012 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter(); | |
2013 EXPECT_EQ("le", *filter->transport); | |
2014 EXPECT_EQ(-15, *filter->rssi); | |
2015 EXPECT_EQ(nullptr, filter->pathloss.get()); | |
2016 std::vector<std::string> uuids = *filter->uuids; | |
2017 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1000")); | |
2018 | |
2019 df = new BluetoothDiscoveryFilter( | |
2020 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE); | |
2021 df->SetRSSI(-60); | |
2022 df->AddUUID(BluetoothUUID("1020")); | |
2023 df->AddUUID(BluetoothUUID("1001")); | |
2024 discovery_filter = scoped_ptr<BluetoothDiscoveryFilter>(df); | |
2025 | |
2026 adapter_->StartDiscoverySessionWithFilter( | |
2027 discovery_filter.Pass(), | |
2028 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback, | |
2029 base::Unretained(this)), | |
2030 base::Bind(&BluetoothChromeOSTest::ErrorCallback, | |
2031 base::Unretained(this))); | |
2032 | |
2033 message_loop_.Run(); | |
2034 | |
2035 filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter(); | |
2036 EXPECT_EQ("le", *filter->transport); | |
2037 EXPECT_EQ(-60, *filter->rssi); | |
2038 EXPECT_EQ(nullptr, filter->pathloss.get()); | |
2039 uuids = *filter->uuids; | |
2040 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1000")); | |
2041 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1001")); | |
2042 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1020")); | |
2043 | |
2044 BluetoothDiscoveryFilter* df3 = new BluetoothDiscoveryFilter( | |
2045 BluetoothDiscoveryFilter::Transport::TRANSPORT_CLASSIC); | |
2046 df3->SetRSSI(-65); | |
2047 df3->AddUUID(BluetoothUUID("1020")); | |
2048 df3->AddUUID(BluetoothUUID("1003")); | |
2049 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter3(df3); | |
2050 | |
2051 adapter_->StartDiscoverySessionWithFilter( | |
2052 discovery_filter3.Pass(), | |
2053 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback, | |
2054 base::Unretained(this)), | |
2055 base::Bind(&BluetoothChromeOSTest::ErrorCallback, | |
2056 base::Unretained(this))); | |
2057 | |
2058 message_loop_.Run(); | |
2059 | |
2060 filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter(); | |
2061 EXPECT_EQ("auto", *filter->transport); | |
2062 EXPECT_EQ(-65, *filter->rssi); | |
2063 EXPECT_EQ(nullptr, filter->pathloss.get()); | |
2064 uuids = *filter->uuids; | |
2065 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1000")); | |
2066 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1001")); | |
2067 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1003")); | |
2068 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1020")); | |
2069 | |
2070 // start additionally classic scan | |
2071 adapter_->StartDiscoverySession( | |
2072 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback, | |
2073 base::Unretained(this)), | |
2074 base::Bind(&BluetoothChromeOSTest::ErrorCallback, | |
2075 base::Unretained(this))); | |
2076 | |
2077 message_loop_.Run(); | |
2078 | |
2079 filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter(); | |
2080 EXPECT_EQ("auto", *filter->transport); | |
2081 EXPECT_EQ(nullptr, filter->rssi.get()); | |
2082 EXPECT_EQ(nullptr, filter->pathloss.get()); | |
2083 EXPECT_EQ(nullptr, filter->uuids.get()); | |
2084 | |
2085 // Request to stop discovery 4 times. | |
2086 for (int i = 3; i >= 0; i--) { | |
2087 discovery_sessions_[i]->Stop( | |
2088 base::Bind(&BluetoothChromeOSTest::Callback, base::Unretained(this)), | |
2089 base::Bind(&BluetoothChromeOSTest::ErrorCallback, | |
2090 base::Unretained(this))); | |
2091 | |
2092 // Every session stopping would trigger filter update | |
2093 message_loop_.Run(); | |
2094 } | |
2095 } | |
2096 | |
2097 TEST_F(BluetoothChromeOSTest, DeviceProperties) { | |
2098 GetAdapter(); | |
2099 | |
2100 BluetoothAdapter::DeviceList devices = adapter_->GetDevices(); | |
2101 ASSERT_EQ(2U, devices.size()); | |
2102 ASSERT_EQ(bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress, | |
2103 devices[0]->GetAddress()); | |
2104 | |
2105 // Verify the other device properties. | |
2106 EXPECT_EQ( | |
2107 base::UTF8ToUTF16(bluez::FakeBluetoothDeviceClient::kPairedDeviceName), | |
2108 devices[0]->GetName()); | |
2109 EXPECT_EQ(BluetoothDevice::DEVICE_COMPUTER, devices[0]->GetDeviceType()); | |
2110 EXPECT_TRUE(devices[0]->IsPaired()); | |
2111 EXPECT_FALSE(devices[0]->IsConnected()); | |
2112 EXPECT_FALSE(devices[0]->IsConnecting()); | |
2113 | |
2114 // Non HID devices are always connectable. | |
2115 EXPECT_TRUE(devices[0]->IsConnectable()); | |
2116 | |
2117 BluetoothDevice::UUIDList uuids = devices[0]->GetUUIDs(); | |
2118 ASSERT_EQ(2U, uuids.size()); | |
2119 EXPECT_EQ(uuids[0], BluetoothUUID("1800")); | |
2120 EXPECT_EQ(uuids[1], BluetoothUUID("1801")); | |
2121 | |
2122 EXPECT_EQ(BluetoothDevice::VENDOR_ID_USB, devices[0]->GetVendorIDSource()); | |
2123 EXPECT_EQ(0x05ac, devices[0]->GetVendorID()); | |
2124 EXPECT_EQ(0x030d, devices[0]->GetProductID()); | |
2125 EXPECT_EQ(0x0306, devices[0]->GetDeviceID()); | |
2126 } | |
2127 | |
2128 TEST_F(BluetoothChromeOSTest, DeviceClassChanged) { | |
2129 // Simulate a change of class of a device, as sometimes occurs | |
2130 // during discovery. | |
2131 GetAdapter(); | |
2132 | |
2133 BluetoothAdapter::DeviceList devices = adapter_->GetDevices(); | |
2134 ASSERT_EQ(2U, devices.size()); | |
2135 ASSERT_EQ(bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress, | |
2136 devices[0]->GetAddress()); | |
2137 ASSERT_EQ(BluetoothDevice::DEVICE_COMPUTER, devices[0]->GetDeviceType()); | |
2138 | |
2139 // Install an observer; expect the DeviceChanged method to be called when | |
2140 // we change the class of the device. | |
2141 TestBluetoothAdapterObserver observer(adapter_); | |
2142 | |
2143 bluez::FakeBluetoothDeviceClient::Properties* properties = | |
2144 fake_bluetooth_device_client_->GetProperties(dbus::ObjectPath( | |
2145 bluez::FakeBluetoothDeviceClient::kPairedDevicePath)); | |
2146 | |
2147 properties->bluetooth_class.ReplaceValue(0x002580); | |
2148 | |
2149 EXPECT_EQ(1, observer.device_changed_count()); | |
2150 EXPECT_EQ(devices[0], observer.last_device()); | |
2151 | |
2152 EXPECT_EQ(BluetoothDevice::DEVICE_MOUSE, devices[0]->GetDeviceType()); | |
2153 } | |
2154 | |
2155 TEST_F(BluetoothChromeOSTest, DeviceNameChanged) { | |
2156 // Simulate a change of name of a device. | |
2157 GetAdapter(); | |
2158 | |
2159 BluetoothAdapter::DeviceList devices = adapter_->GetDevices(); | |
2160 ASSERT_EQ(2U, devices.size()); | |
2161 ASSERT_EQ(bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress, | |
2162 devices[0]->GetAddress()); | |
2163 ASSERT_EQ( | |
2164 base::UTF8ToUTF16(bluez::FakeBluetoothDeviceClient::kPairedDeviceName), | |
2165 devices[0]->GetName()); | |
2166 | |
2167 // Install an observer; expect the DeviceChanged method to be called when | |
2168 // we change the alias of the device. | |
2169 TestBluetoothAdapterObserver observer(adapter_); | |
2170 | |
2171 bluez::FakeBluetoothDeviceClient::Properties* properties = | |
2172 fake_bluetooth_device_client_->GetProperties(dbus::ObjectPath( | |
2173 bluez::FakeBluetoothDeviceClient::kPairedDevicePath)); | |
2174 | |
2175 static const std::string new_name("New Device Name"); | |
2176 properties->alias.ReplaceValue(new_name); | |
2177 | |
2178 EXPECT_EQ(1, observer.device_changed_count()); | |
2179 EXPECT_EQ(devices[0], observer.last_device()); | |
2180 | |
2181 EXPECT_EQ(base::UTF8ToUTF16(new_name), devices[0]->GetName()); | |
2182 } | |
2183 | |
2184 TEST_F(BluetoothChromeOSTest, DeviceAddressChanged) { | |
2185 // Simulate a change of address of a device. | |
2186 GetAdapter(); | |
2187 | |
2188 BluetoothAdapter::DeviceList devices = adapter_->GetDevices(); | |
2189 ASSERT_EQ(2U, devices.size()); | |
2190 ASSERT_EQ(bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress, | |
2191 devices[0]->GetAddress()); | |
2192 ASSERT_EQ( | |
2193 base::UTF8ToUTF16(bluez::FakeBluetoothDeviceClient::kPairedDeviceName), | |
2194 devices[0]->GetName()); | |
2195 | |
2196 // Install an observer; expect the DeviceAddressChanged method to be called | |
2197 // when we change the alias of the device. | |
2198 TestBluetoothAdapterObserver observer(adapter_); | |
2199 | |
2200 bluez::FakeBluetoothDeviceClient::Properties* properties = | |
2201 fake_bluetooth_device_client_->GetProperties(dbus::ObjectPath( | |
2202 bluez::FakeBluetoothDeviceClient::kPairedDevicePath)); | |
2203 | |
2204 static const char* kNewAddress = "D9:1F:FC:11:22:33"; | |
2205 properties->address.ReplaceValue(kNewAddress); | |
2206 | |
2207 EXPECT_EQ(1, observer.device_address_changed_count()); | |
2208 EXPECT_EQ(1, observer.device_changed_count()); | |
2209 EXPECT_EQ(devices[0], observer.last_device()); | |
2210 | |
2211 EXPECT_EQ(std::string(kNewAddress), devices[0]->GetAddress()); | |
2212 } | |
2213 | |
2214 TEST_F(BluetoothChromeOSTest, DeviceUuidsChanged) { | |
2215 // Simulate a change of advertised services of a device. | |
2216 GetAdapter(); | |
2217 | |
2218 BluetoothAdapter::DeviceList devices = adapter_->GetDevices(); | |
2219 ASSERT_EQ(2U, devices.size()); | |
2220 ASSERT_EQ(bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress, | |
2221 devices[0]->GetAddress()); | |
2222 | |
2223 BluetoothDevice::UUIDList uuids = devices[0]->GetUUIDs(); | |
2224 ASSERT_EQ(2U, uuids.size()); | |
2225 ASSERT_EQ(uuids[0], BluetoothUUID("1800")); | |
2226 ASSERT_EQ(uuids[1], BluetoothUUID("1801")); | |
2227 | |
2228 // Install an observer; expect the DeviceChanged method to be called when | |
2229 // we change the class of the device. | |
2230 TestBluetoothAdapterObserver observer(adapter_); | |
2231 | |
2232 bluez::FakeBluetoothDeviceClient::Properties* properties = | |
2233 fake_bluetooth_device_client_->GetProperties(dbus::ObjectPath( | |
2234 bluez::FakeBluetoothDeviceClient::kPairedDevicePath)); | |
2235 | |
2236 std::vector<std::string> new_uuids; | |
2237 new_uuids.push_back(uuids[0].canonical_value()); | |
2238 new_uuids.push_back(uuids[1].canonical_value()); | |
2239 new_uuids.push_back("0000110c-0000-1000-8000-00805f9b34fb"); | |
2240 new_uuids.push_back("0000110e-0000-1000-8000-00805f9b34fb"); | |
2241 new_uuids.push_back("0000110a-0000-1000-8000-00805f9b34fb"); | |
2242 | |
2243 properties->uuids.ReplaceValue(new_uuids); | |
2244 | |
2245 EXPECT_EQ(1, observer.device_changed_count()); | |
2246 EXPECT_EQ(devices[0], observer.last_device()); | |
2247 | |
2248 // Fetching the value should give the new one. | |
2249 uuids = devices[0]->GetUUIDs(); | |
2250 ASSERT_EQ(5U, uuids.size()); | |
2251 EXPECT_EQ(uuids[0], BluetoothUUID("1800")); | |
2252 EXPECT_EQ(uuids[1], BluetoothUUID("1801")); | |
2253 EXPECT_EQ(uuids[2], BluetoothUUID("110c")); | |
2254 EXPECT_EQ(uuids[3], BluetoothUUID("110e")); | |
2255 EXPECT_EQ(uuids[4], BluetoothUUID("110a")); | |
2256 } | |
2257 | |
2258 TEST_F(BluetoothChromeOSTest, DeviceInquiryRSSIInvalidated) { | |
2259 // Simulate invalidation of inquiry RSSI of a device, as it occurs | |
2260 // when discovery is finished. | |
2261 GetAdapter(); | |
2262 | |
2263 BluetoothAdapter::DeviceList devices = adapter_->GetDevices(); | |
2264 ASSERT_EQ(2U, devices.size()); | |
2265 ASSERT_EQ(bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress, | |
2266 devices[0]->GetAddress()); | |
2267 | |
2268 bluez::FakeBluetoothDeviceClient::Properties* properties = | |
2269 fake_bluetooth_device_client_->GetProperties(dbus::ObjectPath( | |
2270 bluez::FakeBluetoothDeviceClient::kPairedDevicePath)); | |
2271 | |
2272 // During discovery, rssi is a valid value (-75) | |
2273 properties->rssi.ReplaceValue(-75); | |
2274 properties->rssi.set_valid(true); | |
2275 | |
2276 ASSERT_EQ(-75, devices[0]->GetInquiryRSSI()); | |
2277 | |
2278 // Install an observer; expect the DeviceChanged method to be called when | |
2279 // we invalidate the RSSI of the device. | |
2280 TestBluetoothAdapterObserver observer(adapter_); | |
2281 | |
2282 // When discovery is over, the value should be invalidated. | |
2283 properties->rssi.set_valid(false); | |
2284 properties->NotifyPropertyChanged(properties->rssi.name()); | |
2285 | |
2286 EXPECT_EQ(1, observer.device_changed_count()); | |
2287 EXPECT_EQ(devices[0], observer.last_device()); | |
2288 | |
2289 int unknown_power = BluetoothDevice::kUnknownPower; | |
2290 EXPECT_EQ(unknown_power, devices[0]->GetInquiryRSSI()); | |
2291 } | |
2292 | |
2293 TEST_F(BluetoothChromeOSTest, DeviceInquiryTxPowerInvalidated) { | |
2294 // Simulate invalidation of inquiry TxPower of a device, as it occurs | |
2295 // when discovery is finished. | |
2296 GetAdapter(); | |
2297 | |
2298 BluetoothAdapter::DeviceList devices = adapter_->GetDevices(); | |
2299 ASSERT_EQ(2U, devices.size()); | |
2300 ASSERT_EQ(bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress, | |
2301 devices[0]->GetAddress()); | |
2302 | |
2303 bluez::FakeBluetoothDeviceClient::Properties* properties = | |
2304 fake_bluetooth_device_client_->GetProperties(dbus::ObjectPath( | |
2305 bluez::FakeBluetoothDeviceClient::kPairedDevicePath)); | |
2306 | |
2307 // During discovery, tx_power is a valid value (0) | |
2308 properties->tx_power.ReplaceValue(0); | |
2309 properties->tx_power.set_valid(true); | |
2310 | |
2311 ASSERT_EQ(0, devices[0]->GetInquiryTxPower()); | |
2312 | |
2313 // Install an observer; expect the DeviceChanged method to be called when | |
2314 // we invalidate the tx_power of the device. | |
2315 TestBluetoothAdapterObserver observer(adapter_); | |
2316 | |
2317 // When discovery is over, the value should be invalidated. | |
2318 properties->tx_power.set_valid(false); | |
2319 properties->NotifyPropertyChanged(properties->tx_power.name()); | |
2320 | |
2321 EXPECT_EQ(1, observer.device_changed_count()); | |
2322 EXPECT_EQ(devices[0], observer.last_device()); | |
2323 | |
2324 int unknown_power = BluetoothDevice::kUnknownPower; | |
2325 EXPECT_EQ(unknown_power, devices[0]->GetInquiryTxPower()); | |
2326 } | |
2327 | |
2328 TEST_F(BluetoothChromeOSTest, ForgetDevice) { | |
2329 GetAdapter(); | |
2330 | |
2331 BluetoothAdapter::DeviceList devices = adapter_->GetDevices(); | |
2332 ASSERT_EQ(2U, devices.size()); | |
2333 ASSERT_EQ(bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress, | |
2334 devices[0]->GetAddress()); | |
2335 | |
2336 std::string address = devices[0]->GetAddress(); | |
2337 | |
2338 // Install an observer; expect the DeviceRemoved method to be called | |
2339 // with the device we remove. | |
2340 TestBluetoothAdapterObserver observer(adapter_); | |
2341 | |
2342 devices[0]->Forget(GetErrorCallback()); | |
2343 EXPECT_EQ(0, error_callback_count_); | |
2344 | |
2345 EXPECT_EQ(1, observer.device_removed_count()); | |
2346 EXPECT_EQ(address, observer.last_device_address()); | |
2347 | |
2348 // GetDevices shouldn't return the device either. | |
2349 devices = adapter_->GetDevices(); | |
2350 ASSERT_EQ(1U, devices.size()); | |
2351 } | |
2352 | |
2353 TEST_F(BluetoothChromeOSTest, ForgetUnpairedDevice) { | |
2354 GetAdapter(); | |
2355 DiscoverDevices(); | |
2356 | |
2357 BluetoothDevice* device = adapter_->GetDevice( | |
2358 bluez::FakeBluetoothDeviceClient::kConnectUnpairableAddress); | |
2359 ASSERT_TRUE(device != nullptr); | |
2360 ASSERT_FALSE(device->IsPaired()); | |
2361 | |
2362 // Connect the device so it becomes trusted and remembered. | |
2363 device->Connect(nullptr, GetCallback(), | |
2364 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback, | |
2365 base::Unretained(this))); | |
2366 | |
2367 ASSERT_EQ(1, callback_count_); | |
2368 ASSERT_EQ(0, error_callback_count_); | |
2369 callback_count_ = 0; | |
2370 | |
2371 ASSERT_TRUE(device->IsConnected()); | |
2372 ASSERT_FALSE(device->IsConnecting()); | |
2373 | |
2374 // Make sure the trusted property has been set to true. | |
2375 bluez::FakeBluetoothDeviceClient::Properties* properties = | |
2376 fake_bluetooth_device_client_->GetProperties(dbus::ObjectPath( | |
2377 bluez::FakeBluetoothDeviceClient::kConnectUnpairablePath)); | |
2378 ASSERT_TRUE(properties->trusted.value()); | |
2379 | |
2380 // Install an observer; expect the DeviceRemoved method to be called | |
2381 // with the device we remove. | |
2382 TestBluetoothAdapterObserver observer(adapter_); | |
2383 | |
2384 device->Forget(GetErrorCallback()); | |
2385 EXPECT_EQ(0, error_callback_count_); | |
2386 | |
2387 EXPECT_EQ(1, observer.device_removed_count()); | |
2388 EXPECT_EQ(bluez::FakeBluetoothDeviceClient::kConnectUnpairableAddress, | |
2389 observer.last_device_address()); | |
2390 | |
2391 // GetDevices shouldn't return the device either. | |
2392 device = adapter_->GetDevice( | |
2393 bluez::FakeBluetoothDeviceClient::kConnectUnpairableAddress); | |
2394 EXPECT_FALSE(device != nullptr); | |
2395 } | |
2396 | |
2397 TEST_F(BluetoothChromeOSTest, ConnectPairedDevice) { | |
2398 GetAdapter(); | |
2399 | |
2400 BluetoothDevice* device = adapter_->GetDevice( | |
2401 bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress); | |
2402 ASSERT_TRUE(device != nullptr); | |
2403 ASSERT_TRUE(device->IsPaired()); | |
2404 | |
2405 TestBluetoothAdapterObserver observer(adapter_); | |
2406 | |
2407 // Connect without a pairing delegate; since the device is already Paired | |
2408 // this should succeed and the device should become connected. | |
2409 device->Connect(nullptr, GetCallback(), | |
2410 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback, | |
2411 base::Unretained(this))); | |
2412 | |
2413 EXPECT_EQ(1, callback_count_); | |
2414 EXPECT_EQ(0, error_callback_count_); | |
2415 | |
2416 // Two changes for connecting, one for connected and one for for trusted | |
2417 // after connecting. | |
2418 EXPECT_EQ(4, observer.device_changed_count()); | |
2419 EXPECT_EQ(device, observer.last_device()); | |
2420 | |
2421 EXPECT_TRUE(device->IsConnected()); | |
2422 EXPECT_FALSE(device->IsConnecting()); | |
2423 } | |
2424 | |
2425 TEST_F(BluetoothChromeOSTest, ConnectUnpairableDevice) { | |
2426 GetAdapter(); | |
2427 DiscoverDevices(); | |
2428 | |
2429 BluetoothDevice* device = adapter_->GetDevice( | |
2430 bluez::FakeBluetoothDeviceClient::kConnectUnpairableAddress); | |
2431 ASSERT_TRUE(device != nullptr); | |
2432 ASSERT_FALSE(device->IsPaired()); | |
2433 | |
2434 TestBluetoothAdapterObserver observer(adapter_); | |
2435 | |
2436 // Connect without a pairing delegate; since the device does not require | |
2437 // pairing, this should succeed and the device should become connected. | |
2438 device->Connect(nullptr, GetCallback(), | |
2439 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback, | |
2440 base::Unretained(this))); | |
2441 | |
2442 EXPECT_EQ(1, callback_count_); | |
2443 EXPECT_EQ(0, error_callback_count_); | |
2444 | |
2445 // Two changes for connecting, one for connected, one for for trusted after | |
2446 // connection, and one for the reconnect mode (IsConnectable). | |
2447 EXPECT_EQ(5, observer.device_changed_count()); | |
2448 EXPECT_EQ(device, observer.last_device()); | |
2449 | |
2450 EXPECT_TRUE(device->IsConnected()); | |
2451 EXPECT_FALSE(device->IsConnecting()); | |
2452 | |
2453 // Make sure the trusted property has been set to true. | |
2454 bluez::FakeBluetoothDeviceClient::Properties* properties = | |
2455 fake_bluetooth_device_client_->GetProperties(dbus::ObjectPath( | |
2456 bluez::FakeBluetoothDeviceClient::kConnectUnpairablePath)); | |
2457 EXPECT_TRUE(properties->trusted.value()); | |
2458 | |
2459 // Verify is a HID device and is not connectable. | |
2460 BluetoothDevice::UUIDList uuids = device->GetUUIDs(); | |
2461 ASSERT_EQ(1U, uuids.size()); | |
2462 EXPECT_EQ(uuids[0], BluetoothUUID("1124")); | |
2463 EXPECT_FALSE(device->IsConnectable()); | |
2464 } | |
2465 | |
2466 TEST_F(BluetoothChromeOSTest, ConnectConnectedDevice) { | |
2467 GetAdapter(); | |
2468 | |
2469 BluetoothDevice* device = adapter_->GetDevice( | |
2470 bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress); | |
2471 ASSERT_TRUE(device != nullptr); | |
2472 ASSERT_TRUE(device->IsPaired()); | |
2473 | |
2474 device->Connect(nullptr, GetCallback(), | |
2475 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback, | |
2476 base::Unretained(this))); | |
2477 | |
2478 ASSERT_EQ(1, callback_count_); | |
2479 ASSERT_EQ(0, error_callback_count_); | |
2480 callback_count_ = 0; | |
2481 | |
2482 ASSERT_TRUE(device->IsConnected()); | |
2483 | |
2484 // Connect again; since the device is already Connected, this shouldn't do | |
2485 // anything to initiate the connection. | |
2486 TestBluetoothAdapterObserver observer(adapter_); | |
2487 | |
2488 device->Connect(nullptr, GetCallback(), | |
2489 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback, | |
2490 base::Unretained(this))); | |
2491 | |
2492 EXPECT_EQ(1, callback_count_); | |
2493 EXPECT_EQ(0, error_callback_count_); | |
2494 | |
2495 // The observer will be called because Connecting will toggle true and false, | |
2496 // and the trusted property will be updated to true. | |
2497 EXPECT_EQ(3, observer.device_changed_count()); | |
2498 | |
2499 EXPECT_TRUE(device->IsConnected()); | |
2500 EXPECT_FALSE(device->IsConnecting()); | |
2501 } | |
2502 | |
2503 TEST_F(BluetoothChromeOSTest, ConnectDeviceFails) { | |
2504 GetAdapter(); | |
2505 DiscoverDevices(); | |
2506 | |
2507 BluetoothDevice* device = adapter_->GetDevice( | |
2508 bluez::FakeBluetoothDeviceClient::kLegacyAutopairAddress); | |
2509 ASSERT_TRUE(device != nullptr); | |
2510 ASSERT_FALSE(device->IsPaired()); | |
2511 | |
2512 TestBluetoothAdapterObserver observer(adapter_); | |
2513 | |
2514 // Connect without a pairing delegate; since the device requires pairing, | |
2515 // this should fail with an error. | |
2516 device->Connect(nullptr, GetCallback(), | |
2517 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback, | |
2518 base::Unretained(this))); | |
2519 | |
2520 EXPECT_EQ(0, callback_count_); | |
2521 EXPECT_EQ(1, error_callback_count_); | |
2522 EXPECT_EQ(BluetoothDevice::ERROR_FAILED, last_connect_error_); | |
2523 | |
2524 EXPECT_EQ(2, observer.device_changed_count()); | |
2525 | |
2526 EXPECT_FALSE(device->IsConnected()); | |
2527 EXPECT_FALSE(device->IsConnecting()); | |
2528 } | |
2529 | |
2530 TEST_F(BluetoothChromeOSTest, DisconnectDevice) { | |
2531 GetAdapter(); | |
2532 | |
2533 BluetoothDevice* device = adapter_->GetDevice( | |
2534 bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress); | |
2535 ASSERT_TRUE(device != nullptr); | |
2536 ASSERT_TRUE(device->IsPaired()); | |
2537 | |
2538 device->Connect(nullptr, GetCallback(), | |
2539 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback, | |
2540 base::Unretained(this))); | |
2541 | |
2542 ASSERT_EQ(1, callback_count_); | |
2543 ASSERT_EQ(0, error_callback_count_); | |
2544 callback_count_ = 0; | |
2545 | |
2546 ASSERT_TRUE(device->IsConnected()); | |
2547 ASSERT_FALSE(device->IsConnecting()); | |
2548 | |
2549 // Disconnect the device, we should see the observer method fire and the | |
2550 // device get dropped. | |
2551 TestBluetoothAdapterObserver observer(adapter_); | |
2552 | |
2553 device->Disconnect(GetCallback(), GetErrorCallback()); | |
2554 | |
2555 EXPECT_EQ(1, callback_count_); | |
2556 EXPECT_EQ(0, error_callback_count_); | |
2557 | |
2558 EXPECT_EQ(1, observer.device_changed_count()); | |
2559 EXPECT_EQ(device, observer.last_device()); | |
2560 | |
2561 EXPECT_FALSE(device->IsConnected()); | |
2562 } | |
2563 | |
2564 TEST_F(BluetoothChromeOSTest, DisconnectUnconnectedDevice) { | |
2565 GetAdapter(); | |
2566 | |
2567 BluetoothDevice* device = adapter_->GetDevice( | |
2568 bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress); | |
2569 ASSERT_TRUE(device != nullptr); | |
2570 ASSERT_TRUE(device->IsPaired()); | |
2571 ASSERT_FALSE(device->IsConnected()); | |
2572 | |
2573 // Disconnect the device, we should see the observer method fire and the | |
2574 // device get dropped. | |
2575 TestBluetoothAdapterObserver observer(adapter_); | |
2576 | |
2577 device->Disconnect(GetCallback(), GetErrorCallback()); | |
2578 | |
2579 EXPECT_EQ(0, callback_count_); | |
2580 EXPECT_EQ(1, error_callback_count_); | |
2581 | |
2582 EXPECT_EQ(0, observer.device_changed_count()); | |
2583 | |
2584 EXPECT_FALSE(device->IsConnected()); | |
2585 } | |
2586 | |
2587 TEST_F(BluetoothChromeOSTest, PairLegacyAutopair) { | |
2588 fake_bluetooth_device_client_->SetSimulationIntervalMs(10); | |
2589 | |
2590 GetAdapter(); | |
2591 DiscoverDevices(); | |
2592 | |
2593 // The Legacy Autopair device requires no PIN or Passkey to pair because | |
2594 // the daemon provides 0000 to the device for us. | |
2595 BluetoothDevice* device = adapter_->GetDevice( | |
2596 bluez::FakeBluetoothDeviceClient::kLegacyAutopairAddress); | |
2597 ASSERT_TRUE(device != nullptr); | |
2598 ASSERT_FALSE(device->IsPaired()); | |
2599 | |
2600 TestBluetoothAdapterObserver observer(adapter_); | |
2601 | |
2602 TestPairingDelegate pairing_delegate; | |
2603 device->Connect(&pairing_delegate, GetCallback(), | |
2604 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback, | |
2605 base::Unretained(this))); | |
2606 | |
2607 EXPECT_EQ(0, pairing_delegate.call_count_); | |
2608 EXPECT_TRUE(device->IsConnecting()); | |
2609 | |
2610 message_loop_.Run(); | |
2611 | |
2612 EXPECT_EQ(1, callback_count_); | |
2613 EXPECT_EQ(0, error_callback_count_); | |
2614 | |
2615 // Two changes for connecting, one change for connected, one for paired, | |
2616 // two for trusted (after pairing and connection), and one for the reconnect | |
2617 // mode (IsConnectable). | |
2618 EXPECT_EQ(7, observer.device_changed_count()); | |
2619 EXPECT_EQ(device, observer.last_device()); | |
2620 | |
2621 EXPECT_TRUE(device->IsConnected()); | |
2622 EXPECT_FALSE(device->IsConnecting()); | |
2623 | |
2624 EXPECT_TRUE(device->IsPaired()); | |
2625 | |
2626 // Verify is a HID device and is connectable. | |
2627 BluetoothDevice::UUIDList uuids = device->GetUUIDs(); | |
2628 ASSERT_EQ(1U, uuids.size()); | |
2629 EXPECT_EQ(uuids[0], BluetoothUUID("1124")); | |
2630 EXPECT_TRUE(device->IsConnectable()); | |
2631 | |
2632 // Make sure the trusted property has been set to true. | |
2633 bluez::FakeBluetoothDeviceClient::Properties* properties = | |
2634 fake_bluetooth_device_client_->GetProperties(dbus::ObjectPath( | |
2635 bluez::FakeBluetoothDeviceClient::kLegacyAutopairPath)); | |
2636 EXPECT_TRUE(properties->trusted.value()); | |
2637 } | |
2638 | |
2639 TEST_F(BluetoothChromeOSTest, PairDisplayPinCode) { | |
2640 fake_bluetooth_device_client_->SetSimulationIntervalMs(10); | |
2641 | |
2642 GetAdapter(); | |
2643 DiscoverDevices(); | |
2644 | |
2645 // Requires that we display a randomly generated PIN on the screen. | |
2646 BluetoothDevice* device = adapter_->GetDevice( | |
2647 bluez::FakeBluetoothDeviceClient::kDisplayPinCodeAddress); | |
2648 ASSERT_TRUE(device != nullptr); | |
2649 ASSERT_FALSE(device->IsPaired()); | |
2650 | |
2651 TestBluetoothAdapterObserver observer(adapter_); | |
2652 | |
2653 TestPairingDelegate pairing_delegate; | |
2654 device->Connect(&pairing_delegate, GetCallback(), | |
2655 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback, | |
2656 base::Unretained(this))); | |
2657 | |
2658 EXPECT_EQ(1, pairing_delegate.call_count_); | |
2659 EXPECT_EQ(1, pairing_delegate.display_pincode_count_); | |
2660 EXPECT_EQ("123456", pairing_delegate.last_pincode_); | |
2661 EXPECT_TRUE(device->IsConnecting()); | |
2662 | |
2663 message_loop_.Run(); | |
2664 | |
2665 EXPECT_EQ(1, callback_count_); | |
2666 EXPECT_EQ(0, error_callback_count_); | |
2667 | |
2668 // Two changes for connecting, one change for connected, one for paired, | |
2669 // two for trusted (after pairing and connection), and one for the reconnect | |
2670 // mode (IsConnectable). | |
2671 EXPECT_EQ(7, observer.device_changed_count()); | |
2672 EXPECT_EQ(device, observer.last_device()); | |
2673 | |
2674 EXPECT_TRUE(device->IsConnected()); | |
2675 EXPECT_FALSE(device->IsConnecting()); | |
2676 | |
2677 EXPECT_TRUE(device->IsPaired()); | |
2678 | |
2679 // Verify is a HID device and is connectable. | |
2680 BluetoothDevice::UUIDList uuids = device->GetUUIDs(); | |
2681 ASSERT_EQ(1U, uuids.size()); | |
2682 EXPECT_EQ(uuids[0], BluetoothUUID("1124")); | |
2683 EXPECT_TRUE(device->IsConnectable()); | |
2684 | |
2685 // Make sure the trusted property has been set to true. | |
2686 bluez::FakeBluetoothDeviceClient::Properties* properties = | |
2687 fake_bluetooth_device_client_->GetProperties(dbus::ObjectPath( | |
2688 bluez::FakeBluetoothDeviceClient::kDisplayPinCodePath)); | |
2689 EXPECT_TRUE(properties->trusted.value()); | |
2690 } | |
2691 | |
2692 TEST_F(BluetoothChromeOSTest, PairDisplayPasskey) { | |
2693 fake_bluetooth_device_client_->SetSimulationIntervalMs(10); | |
2694 | |
2695 GetAdapter(); | |
2696 DiscoverDevices(); | |
2697 | |
2698 // Requires that we display a randomly generated Passkey on the screen, | |
2699 // and notifies us as it's typed in. | |
2700 BluetoothDevice* device = adapter_->GetDevice( | |
2701 bluez::FakeBluetoothDeviceClient::kDisplayPasskeyAddress); | |
2702 ASSERT_TRUE(device != nullptr); | |
2703 ASSERT_FALSE(device->IsPaired()); | |
2704 | |
2705 TestBluetoothAdapterObserver observer(adapter_); | |
2706 | |
2707 TestPairingDelegate pairing_delegate; | |
2708 device->Connect(&pairing_delegate, GetCallback(), | |
2709 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback, | |
2710 base::Unretained(this))); | |
2711 | |
2712 // One call for DisplayPasskey() and one for KeysEntered(). | |
2713 EXPECT_EQ(2, pairing_delegate.call_count_); | |
2714 EXPECT_EQ(1, pairing_delegate.display_passkey_count_); | |
2715 EXPECT_EQ(123456U, pairing_delegate.last_passkey_); | |
2716 EXPECT_EQ(1, pairing_delegate.keys_entered_count_); | |
2717 EXPECT_EQ(0U, pairing_delegate.last_entered_); | |
2718 | |
2719 EXPECT_TRUE(device->IsConnecting()); | |
2720 | |
2721 // One call to KeysEntered() for each key, including [enter]. | |
2722 for (int i = 1; i <= 7; ++i) { | |
2723 message_loop_.Run(); | |
2724 | |
2725 EXPECT_EQ(2 + i, pairing_delegate.call_count_); | |
2726 EXPECT_EQ(1 + i, pairing_delegate.keys_entered_count_); | |
2727 EXPECT_EQ(static_cast<uint32_t>(i), pairing_delegate.last_entered_); | |
2728 } | |
2729 | |
2730 message_loop_.Run(); | |
2731 | |
2732 // 8 KeysEntered notifications (0 to 7, inclusive) and one aditional call for | |
2733 // DisplayPasskey(). | |
2734 EXPECT_EQ(9, pairing_delegate.call_count_); | |
2735 EXPECT_EQ(8, pairing_delegate.keys_entered_count_); | |
2736 EXPECT_EQ(7U, pairing_delegate.last_entered_); | |
2737 | |
2738 EXPECT_EQ(1, callback_count_); | |
2739 EXPECT_EQ(0, error_callback_count_); | |
2740 | |
2741 // Two changes for connecting, one change for connected, one for paired, | |
2742 // two for trusted (after pairing and connection), and one for the reconnect | |
2743 // mode (IsConnectable). | |
2744 EXPECT_EQ(7, observer.device_changed_count()); | |
2745 EXPECT_EQ(device, observer.last_device()); | |
2746 | |
2747 EXPECT_TRUE(device->IsConnected()); | |
2748 EXPECT_FALSE(device->IsConnecting()); | |
2749 | |
2750 EXPECT_TRUE(device->IsPaired()); | |
2751 | |
2752 // Verify is a HID device. | |
2753 BluetoothDevice::UUIDList uuids = device->GetUUIDs(); | |
2754 ASSERT_EQ(1U, uuids.size()); | |
2755 EXPECT_EQ(uuids[0], BluetoothUUID("1124")); | |
2756 | |
2757 // And usually not connectable. | |
2758 EXPECT_FALSE(device->IsConnectable()); | |
2759 | |
2760 // Make sure the trusted property has been set to true. | |
2761 bluez::FakeBluetoothDeviceClient::Properties* properties = | |
2762 fake_bluetooth_device_client_->GetProperties(dbus::ObjectPath( | |
2763 bluez::FakeBluetoothDeviceClient::kDisplayPasskeyPath)); | |
2764 EXPECT_TRUE(properties->trusted.value()); | |
2765 } | |
2766 | |
2767 TEST_F(BluetoothChromeOSTest, PairRequestPinCode) { | |
2768 fake_bluetooth_device_client_->SetSimulationIntervalMs(10); | |
2769 | |
2770 GetAdapter(); | |
2771 DiscoverDevices(); | |
2772 | |
2773 // Requires that the user enters a PIN for them. | |
2774 BluetoothDevice* device = adapter_->GetDevice( | |
2775 bluez::FakeBluetoothDeviceClient::kRequestPinCodeAddress); | |
2776 ASSERT_TRUE(device != nullptr); | |
2777 ASSERT_FALSE(device->IsPaired()); | |
2778 | |
2779 TestBluetoothAdapterObserver observer(adapter_); | |
2780 | |
2781 TestPairingDelegate pairing_delegate; | |
2782 device->Connect(&pairing_delegate, GetCallback(), | |
2783 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback, | |
2784 base::Unretained(this))); | |
2785 | |
2786 EXPECT_EQ(1, pairing_delegate.call_count_); | |
2787 EXPECT_EQ(1, pairing_delegate.request_pincode_count_); | |
2788 EXPECT_TRUE(device->IsConnecting()); | |
2789 | |
2790 // Set the PIN. | |
2791 device->SetPinCode("1234"); | |
2792 message_loop_.Run(); | |
2793 | |
2794 EXPECT_EQ(1, callback_count_); | |
2795 EXPECT_EQ(0, error_callback_count_); | |
2796 | |
2797 // Two changes for connecting, one change for connected, one for paired and | |
2798 // two for trusted (after pairing and connection). | |
2799 EXPECT_EQ(6, observer.device_changed_count()); | |
2800 EXPECT_EQ(device, observer.last_device()); | |
2801 | |
2802 EXPECT_TRUE(device->IsConnected()); | |
2803 EXPECT_FALSE(device->IsConnecting()); | |
2804 | |
2805 EXPECT_TRUE(device->IsPaired()); | |
2806 | |
2807 // Verify is not a HID device. | |
2808 BluetoothDevice::UUIDList uuids = device->GetUUIDs(); | |
2809 ASSERT_EQ(0U, uuids.size()); | |
2810 | |
2811 // Non HID devices are always connectable. | |
2812 EXPECT_TRUE(device->IsConnectable()); | |
2813 | |
2814 // Make sure the trusted property has been set to true. | |
2815 bluez::FakeBluetoothDeviceClient::Properties* properties = | |
2816 fake_bluetooth_device_client_->GetProperties(dbus::ObjectPath( | |
2817 bluez::FakeBluetoothDeviceClient::kRequestPinCodePath)); | |
2818 EXPECT_TRUE(properties->trusted.value()); | |
2819 } | |
2820 | |
2821 TEST_F(BluetoothChromeOSTest, PairConfirmPasskey) { | |
2822 fake_bluetooth_device_client_->SetSimulationIntervalMs(10); | |
2823 | |
2824 GetAdapter(); | |
2825 DiscoverDevices(); | |
2826 | |
2827 // Requests that we confirm a displayed passkey. | |
2828 BluetoothDevice* device = adapter_->GetDevice( | |
2829 bluez::FakeBluetoothDeviceClient::kConfirmPasskeyAddress); | |
2830 ASSERT_TRUE(device != nullptr); | |
2831 ASSERT_FALSE(device->IsPaired()); | |
2832 | |
2833 TestBluetoothAdapterObserver observer(adapter_); | |
2834 | |
2835 TestPairingDelegate pairing_delegate; | |
2836 device->Connect(&pairing_delegate, GetCallback(), | |
2837 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback, | |
2838 base::Unretained(this))); | |
2839 | |
2840 EXPECT_EQ(1, pairing_delegate.call_count_); | |
2841 EXPECT_EQ(1, pairing_delegate.confirm_passkey_count_); | |
2842 EXPECT_EQ(123456U, pairing_delegate.last_passkey_); | |
2843 EXPECT_TRUE(device->IsConnecting()); | |
2844 | |
2845 // Confirm the passkey. | |
2846 device->ConfirmPairing(); | |
2847 message_loop_.Run(); | |
2848 | |
2849 EXPECT_EQ(1, callback_count_); | |
2850 EXPECT_EQ(0, error_callback_count_); | |
2851 | |
2852 // Two changes for connecting, one change for connected, one for paired and | |
2853 // two for trusted (after pairing and connection). | |
2854 EXPECT_EQ(6, observer.device_changed_count()); | |
2855 EXPECT_EQ(device, observer.last_device()); | |
2856 | |
2857 EXPECT_TRUE(device->IsConnected()); | |
2858 EXPECT_FALSE(device->IsConnecting()); | |
2859 | |
2860 EXPECT_TRUE(device->IsPaired()); | |
2861 | |
2862 // Non HID devices are always connectable. | |
2863 EXPECT_TRUE(device->IsConnectable()); | |
2864 | |
2865 // Make sure the trusted property has been set to true. | |
2866 bluez::FakeBluetoothDeviceClient::Properties* properties = | |
2867 fake_bluetooth_device_client_->GetProperties(dbus::ObjectPath( | |
2868 bluez::FakeBluetoothDeviceClient::kConfirmPasskeyPath)); | |
2869 EXPECT_TRUE(properties->trusted.value()); | |
2870 } | |
2871 | |
2872 TEST_F(BluetoothChromeOSTest, PairRequestPasskey) { | |
2873 fake_bluetooth_device_client_->SetSimulationIntervalMs(10); | |
2874 | |
2875 GetAdapter(); | |
2876 DiscoverDevices(); | |
2877 | |
2878 // Requires that the user enters a Passkey, this would be some kind of | |
2879 // device that has a display, but doesn't use "just works" - maybe a car? | |
2880 BluetoothDevice* device = adapter_->GetDevice( | |
2881 bluez::FakeBluetoothDeviceClient::kRequestPasskeyAddress); | |
2882 ASSERT_TRUE(device != nullptr); | |
2883 ASSERT_FALSE(device->IsPaired()); | |
2884 | |
2885 TestBluetoothAdapterObserver observer(adapter_); | |
2886 | |
2887 TestPairingDelegate pairing_delegate; | |
2888 device->Connect(&pairing_delegate, GetCallback(), | |
2889 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback, | |
2890 base::Unretained(this))); | |
2891 | |
2892 EXPECT_EQ(1, pairing_delegate.call_count_); | |
2893 EXPECT_EQ(1, pairing_delegate.request_passkey_count_); | |
2894 EXPECT_TRUE(device->IsConnecting()); | |
2895 | |
2896 // Set the Passkey. | |
2897 device->SetPasskey(1234); | |
2898 message_loop_.Run(); | |
2899 | |
2900 EXPECT_EQ(1, callback_count_); | |
2901 EXPECT_EQ(0, error_callback_count_); | |
2902 | |
2903 // Two changes for connecting, one change for connected, one for paired and | |
2904 // two for trusted (after pairing and connection). | |
2905 EXPECT_EQ(6, observer.device_changed_count()); | |
2906 EXPECT_EQ(device, observer.last_device()); | |
2907 | |
2908 EXPECT_TRUE(device->IsConnected()); | |
2909 EXPECT_FALSE(device->IsConnecting()); | |
2910 | |
2911 EXPECT_TRUE(device->IsPaired()); | |
2912 | |
2913 // Non HID devices are always connectable. | |
2914 EXPECT_TRUE(device->IsConnectable()); | |
2915 | |
2916 // Make sure the trusted property has been set to true. | |
2917 bluez::FakeBluetoothDeviceClient::Properties* properties = | |
2918 fake_bluetooth_device_client_->GetProperties(dbus::ObjectPath( | |
2919 bluez::FakeBluetoothDeviceClient::kRequestPasskeyPath)); | |
2920 EXPECT_TRUE(properties->trusted.value()); | |
2921 } | |
2922 | |
2923 TEST_F(BluetoothChromeOSTest, PairJustWorks) { | |
2924 fake_bluetooth_device_client_->SetSimulationIntervalMs(10); | |
2925 | |
2926 GetAdapter(); | |
2927 DiscoverDevices(); | |
2928 | |
2929 // Uses just-works pairing, since this is an outgoing pairing, no delegate | |
2930 // interaction is required. | |
2931 BluetoothDevice* device = | |
2932 adapter_->GetDevice(bluez::FakeBluetoothDeviceClient::kJustWorksAddress); | |
2933 ASSERT_TRUE(device != nullptr); | |
2934 ASSERT_FALSE(device->IsPaired()); | |
2935 | |
2936 TestBluetoothAdapterObserver observer(adapter_); | |
2937 | |
2938 TestPairingDelegate pairing_delegate; | |
2939 device->Connect(&pairing_delegate, GetCallback(), | |
2940 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback, | |
2941 base::Unretained(this))); | |
2942 | |
2943 EXPECT_EQ(0, pairing_delegate.call_count_); | |
2944 | |
2945 message_loop_.Run(); | |
2946 | |
2947 EXPECT_EQ(1, callback_count_); | |
2948 EXPECT_EQ(0, error_callback_count_); | |
2949 | |
2950 // Two changes for connecting, one change for connected, one for paired and | |
2951 // two for trusted (after pairing and connection). | |
2952 EXPECT_EQ(6, observer.device_changed_count()); | |
2953 EXPECT_EQ(device, observer.last_device()); | |
2954 | |
2955 EXPECT_TRUE(device->IsConnected()); | |
2956 EXPECT_FALSE(device->IsConnecting()); | |
2957 | |
2958 EXPECT_TRUE(device->IsPaired()); | |
2959 | |
2960 // Non HID devices are always connectable. | |
2961 EXPECT_TRUE(device->IsConnectable()); | |
2962 | |
2963 // Make sure the trusted property has been set to true. | |
2964 bluez::FakeBluetoothDeviceClient::Properties* properties = | |
2965 fake_bluetooth_device_client_->GetProperties( | |
2966 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kJustWorksPath)); | |
2967 EXPECT_TRUE(properties->trusted.value()); | |
2968 } | |
2969 | |
2970 TEST_F(BluetoothChromeOSTest, PairUnpairableDeviceFails) { | |
2971 fake_bluetooth_device_client_->SetSimulationIntervalMs(10); | |
2972 | |
2973 GetAdapter(); | |
2974 DiscoverDevice(bluez::FakeBluetoothDeviceClient::kUnconnectableDeviceAddress); | |
2975 | |
2976 BluetoothDevice* device = adapter_->GetDevice( | |
2977 bluez::FakeBluetoothDeviceClient::kUnpairableDeviceAddress); | |
2978 ASSERT_TRUE(device != nullptr); | |
2979 ASSERT_FALSE(device->IsPaired()); | |
2980 | |
2981 TestBluetoothAdapterObserver observer(adapter_); | |
2982 | |
2983 TestPairingDelegate pairing_delegate; | |
2984 device->Connect(&pairing_delegate, GetCallback(), | |
2985 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback, | |
2986 base::Unretained(this))); | |
2987 | |
2988 EXPECT_EQ(0, pairing_delegate.call_count_); | |
2989 EXPECT_TRUE(device->IsConnecting()); | |
2990 | |
2991 // Run the loop to get the error.. | |
2992 message_loop_.Run(); | |
2993 | |
2994 EXPECT_EQ(0, callback_count_); | |
2995 EXPECT_EQ(1, error_callback_count_); | |
2996 | |
2997 EXPECT_EQ(BluetoothDevice::ERROR_FAILED, last_connect_error_); | |
2998 | |
2999 EXPECT_FALSE(device->IsConnected()); | |
3000 EXPECT_FALSE(device->IsConnecting()); | |
3001 EXPECT_FALSE(device->IsPaired()); | |
3002 } | |
3003 | |
3004 TEST_F(BluetoothChromeOSTest, PairingFails) { | |
3005 fake_bluetooth_device_client_->SetSimulationIntervalMs(10); | |
3006 | |
3007 GetAdapter(); | |
3008 DiscoverDevice(bluez::FakeBluetoothDeviceClient::kVanishingDeviceAddress); | |
3009 | |
3010 // The vanishing device times out during pairing | |
3011 BluetoothDevice* device = adapter_->GetDevice( | |
3012 bluez::FakeBluetoothDeviceClient::kVanishingDeviceAddress); | |
3013 ASSERT_TRUE(device != nullptr); | |
3014 ASSERT_FALSE(device->IsPaired()); | |
3015 | |
3016 TestBluetoothAdapterObserver observer(adapter_); | |
3017 | |
3018 TestPairingDelegate pairing_delegate; | |
3019 device->Connect(&pairing_delegate, GetCallback(), | |
3020 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback, | |
3021 base::Unretained(this))); | |
3022 | |
3023 EXPECT_EQ(0, pairing_delegate.call_count_); | |
3024 EXPECT_TRUE(device->IsConnecting()); | |
3025 | |
3026 // Run the loop to get the error.. | |
3027 message_loop_.Run(); | |
3028 | |
3029 EXPECT_EQ(0, callback_count_); | |
3030 EXPECT_EQ(1, error_callback_count_); | |
3031 | |
3032 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_TIMEOUT, last_connect_error_); | |
3033 | |
3034 EXPECT_FALSE(device->IsConnected()); | |
3035 EXPECT_FALSE(device->IsConnecting()); | |
3036 EXPECT_FALSE(device->IsPaired()); | |
3037 } | |
3038 | |
3039 TEST_F(BluetoothChromeOSTest, PairingFailsAtConnection) { | |
3040 fake_bluetooth_device_client_->SetSimulationIntervalMs(10); | |
3041 | |
3042 GetAdapter(); | |
3043 DiscoverDevices(); | |
3044 | |
3045 // Everything seems to go according to plan with the unconnectable device; | |
3046 // it pairs, but then you can't make connections to it after. | |
3047 BluetoothDevice* device = adapter_->GetDevice( | |
3048 bluez::FakeBluetoothDeviceClient::kUnconnectableDeviceAddress); | |
3049 ASSERT_TRUE(device != nullptr); | |
3050 ASSERT_FALSE(device->IsPaired()); | |
3051 | |
3052 TestBluetoothAdapterObserver observer(adapter_); | |
3053 | |
3054 TestPairingDelegate pairing_delegate; | |
3055 device->Connect(&pairing_delegate, GetCallback(), | |
3056 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback, | |
3057 base::Unretained(this))); | |
3058 | |
3059 EXPECT_EQ(0, pairing_delegate.call_count_); | |
3060 EXPECT_TRUE(device->IsConnecting()); | |
3061 | |
3062 message_loop_.Run(); | |
3063 | |
3064 EXPECT_EQ(0, callback_count_); | |
3065 EXPECT_EQ(1, error_callback_count_); | |
3066 EXPECT_EQ(BluetoothDevice::ERROR_FAILED, last_connect_error_); | |
3067 | |
3068 // Two changes for connecting, one for paired and one for trusted after | |
3069 // pairing. The device should not be connected. | |
3070 EXPECT_EQ(4, observer.device_changed_count()); | |
3071 EXPECT_EQ(device, observer.last_device()); | |
3072 | |
3073 EXPECT_FALSE(device->IsConnected()); | |
3074 EXPECT_FALSE(device->IsConnecting()); | |
3075 | |
3076 EXPECT_TRUE(device->IsPaired()); | |
3077 | |
3078 // Make sure the trusted property has been set to true still (since pairing | |
3079 // worked). | |
3080 bluez::FakeBluetoothDeviceClient::Properties* properties = | |
3081 fake_bluetooth_device_client_->GetProperties(dbus::ObjectPath( | |
3082 bluez::FakeBluetoothDeviceClient::kUnconnectableDevicePath)); | |
3083 EXPECT_TRUE(properties->trusted.value()); | |
3084 } | |
3085 | |
3086 TEST_F(BluetoothChromeOSTest, PairingRejectedAtPinCode) { | |
3087 fake_bluetooth_device_client_->SetSimulationIntervalMs(10); | |
3088 | |
3089 GetAdapter(); | |
3090 DiscoverDevices(); | |
3091 | |
3092 // Reject the pairing after we receive a request for the PIN code. | |
3093 BluetoothDevice* device = adapter_->GetDevice( | |
3094 bluez::FakeBluetoothDeviceClient::kRequestPinCodeAddress); | |
3095 ASSERT_TRUE(device != nullptr); | |
3096 ASSERT_FALSE(device->IsPaired()); | |
3097 | |
3098 TestBluetoothAdapterObserver observer(adapter_); | |
3099 | |
3100 TestPairingDelegate pairing_delegate; | |
3101 device->Connect(&pairing_delegate, GetCallback(), | |
3102 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback, | |
3103 base::Unretained(this))); | |
3104 | |
3105 EXPECT_EQ(1, pairing_delegate.call_count_); | |
3106 EXPECT_EQ(1, pairing_delegate.request_pincode_count_); | |
3107 EXPECT_TRUE(device->IsConnecting()); | |
3108 | |
3109 // Reject the pairing. | |
3110 device->RejectPairing(); | |
3111 message_loop_.Run(); | |
3112 | |
3113 EXPECT_EQ(0, callback_count_); | |
3114 EXPECT_EQ(1, error_callback_count_); | |
3115 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_REJECTED, last_connect_error_); | |
3116 | |
3117 // Should be no changes except connecting going true and false. | |
3118 EXPECT_EQ(2, observer.device_changed_count()); | |
3119 EXPECT_FALSE(device->IsConnected()); | |
3120 EXPECT_FALSE(device->IsConnecting()); | |
3121 EXPECT_FALSE(device->IsPaired()); | |
3122 } | |
3123 | |
3124 TEST_F(BluetoothChromeOSTest, PairingCancelledAtPinCode) { | |
3125 fake_bluetooth_device_client_->SetSimulationIntervalMs(10); | |
3126 | |
3127 GetAdapter(); | |
3128 DiscoverDevices(); | |
3129 | |
3130 // Cancel the pairing after we receive a request for the PIN code. | |
3131 BluetoothDevice* device = adapter_->GetDevice( | |
3132 bluez::FakeBluetoothDeviceClient::kRequestPinCodeAddress); | |
3133 ASSERT_TRUE(device != nullptr); | |
3134 ASSERT_FALSE(device->IsPaired()); | |
3135 | |
3136 TestBluetoothAdapterObserver observer(adapter_); | |
3137 | |
3138 TestPairingDelegate pairing_delegate; | |
3139 device->Connect(&pairing_delegate, GetCallback(), | |
3140 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback, | |
3141 base::Unretained(this))); | |
3142 | |
3143 EXPECT_EQ(1, pairing_delegate.call_count_); | |
3144 EXPECT_EQ(1, pairing_delegate.request_pincode_count_); | |
3145 EXPECT_TRUE(device->IsConnecting()); | |
3146 | |
3147 // Cancel the pairing. | |
3148 device->CancelPairing(); | |
3149 message_loop_.Run(); | |
3150 | |
3151 EXPECT_EQ(0, callback_count_); | |
3152 EXPECT_EQ(1, error_callback_count_); | |
3153 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED, last_connect_error_); | |
3154 | |
3155 // Should be no changes except connecting going true and false. | |
3156 EXPECT_EQ(2, observer.device_changed_count()); | |
3157 EXPECT_FALSE(device->IsConnected()); | |
3158 EXPECT_FALSE(device->IsConnecting()); | |
3159 EXPECT_FALSE(device->IsPaired()); | |
3160 } | |
3161 | |
3162 TEST_F(BluetoothChromeOSTest, PairingRejectedAtPasskey) { | |
3163 fake_bluetooth_device_client_->SetSimulationIntervalMs(10); | |
3164 | |
3165 GetAdapter(); | |
3166 DiscoverDevices(); | |
3167 | |
3168 // Reject the pairing after we receive a request for the passkey. | |
3169 BluetoothDevice* device = adapter_->GetDevice( | |
3170 bluez::FakeBluetoothDeviceClient::kRequestPasskeyAddress); | |
3171 ASSERT_TRUE(device != nullptr); | |
3172 ASSERT_FALSE(device->IsPaired()); | |
3173 | |
3174 TestBluetoothAdapterObserver observer(adapter_); | |
3175 | |
3176 TestPairingDelegate pairing_delegate; | |
3177 device->Connect(&pairing_delegate, GetCallback(), | |
3178 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback, | |
3179 base::Unretained(this))); | |
3180 | |
3181 EXPECT_EQ(1, pairing_delegate.call_count_); | |
3182 EXPECT_EQ(1, pairing_delegate.request_passkey_count_); | |
3183 EXPECT_TRUE(device->IsConnecting()); | |
3184 | |
3185 // Reject the pairing. | |
3186 device->RejectPairing(); | |
3187 message_loop_.Run(); | |
3188 | |
3189 EXPECT_EQ(0, callback_count_); | |
3190 EXPECT_EQ(1, error_callback_count_); | |
3191 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_REJECTED, last_connect_error_); | |
3192 | |
3193 // Should be no changes except connecting going true and false. | |
3194 EXPECT_EQ(2, observer.device_changed_count()); | |
3195 EXPECT_FALSE(device->IsConnected()); | |
3196 EXPECT_FALSE(device->IsConnecting()); | |
3197 EXPECT_FALSE(device->IsPaired()); | |
3198 } | |
3199 | |
3200 TEST_F(BluetoothChromeOSTest, PairingCancelledAtPasskey) { | |
3201 fake_bluetooth_device_client_->SetSimulationIntervalMs(10); | |
3202 | |
3203 GetAdapter(); | |
3204 DiscoverDevices(); | |
3205 | |
3206 // Cancel the pairing after we receive a request for the passkey. | |
3207 BluetoothDevice* device = adapter_->GetDevice( | |
3208 bluez::FakeBluetoothDeviceClient::kRequestPasskeyAddress); | |
3209 ASSERT_TRUE(device != nullptr); | |
3210 ASSERT_FALSE(device->IsPaired()); | |
3211 | |
3212 TestBluetoothAdapterObserver observer(adapter_); | |
3213 | |
3214 TestPairingDelegate pairing_delegate; | |
3215 device->Connect(&pairing_delegate, GetCallback(), | |
3216 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback, | |
3217 base::Unretained(this))); | |
3218 | |
3219 EXPECT_EQ(1, pairing_delegate.call_count_); | |
3220 EXPECT_EQ(1, pairing_delegate.request_passkey_count_); | |
3221 EXPECT_TRUE(device->IsConnecting()); | |
3222 | |
3223 // Cancel the pairing. | |
3224 device->CancelPairing(); | |
3225 message_loop_.Run(); | |
3226 | |
3227 EXPECT_EQ(0, callback_count_); | |
3228 EXPECT_EQ(1, error_callback_count_); | |
3229 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED, last_connect_error_); | |
3230 | |
3231 // Should be no changes except connecting going true and false. | |
3232 EXPECT_EQ(2, observer.device_changed_count()); | |
3233 EXPECT_FALSE(device->IsConnected()); | |
3234 EXPECT_FALSE(device->IsConnecting()); | |
3235 EXPECT_FALSE(device->IsPaired()); | |
3236 } | |
3237 | |
3238 TEST_F(BluetoothChromeOSTest, PairingRejectedAtConfirmation) { | |
3239 fake_bluetooth_device_client_->SetSimulationIntervalMs(10); | |
3240 | |
3241 GetAdapter(); | |
3242 DiscoverDevices(); | |
3243 | |
3244 // Reject the pairing after we receive a request for passkey confirmation. | |
3245 BluetoothDevice* device = adapter_->GetDevice( | |
3246 bluez::FakeBluetoothDeviceClient::kConfirmPasskeyAddress); | |
3247 ASSERT_TRUE(device != nullptr); | |
3248 ASSERT_FALSE(device->IsPaired()); | |
3249 | |
3250 TestBluetoothAdapterObserver observer(adapter_); | |
3251 | |
3252 TestPairingDelegate pairing_delegate; | |
3253 device->Connect(&pairing_delegate, GetCallback(), | |
3254 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback, | |
3255 base::Unretained(this))); | |
3256 | |
3257 EXPECT_EQ(1, pairing_delegate.call_count_); | |
3258 EXPECT_EQ(1, pairing_delegate.confirm_passkey_count_); | |
3259 EXPECT_TRUE(device->IsConnecting()); | |
3260 | |
3261 // Reject the pairing. | |
3262 device->RejectPairing(); | |
3263 message_loop_.Run(); | |
3264 | |
3265 EXPECT_EQ(0, callback_count_); | |
3266 EXPECT_EQ(1, error_callback_count_); | |
3267 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_REJECTED, last_connect_error_); | |
3268 | |
3269 // Should be no changes except connecting going true and false. | |
3270 EXPECT_EQ(2, observer.device_changed_count()); | |
3271 EXPECT_FALSE(device->IsConnected()); | |
3272 EXPECT_FALSE(device->IsConnecting()); | |
3273 EXPECT_FALSE(device->IsPaired()); | |
3274 } | |
3275 | |
3276 TEST_F(BluetoothChromeOSTest, PairingCancelledAtConfirmation) { | |
3277 fake_bluetooth_device_client_->SetSimulationIntervalMs(10); | |
3278 | |
3279 GetAdapter(); | |
3280 DiscoverDevices(); | |
3281 | |
3282 // Cancel the pairing after we receive a request for the passkey. | |
3283 BluetoothDevice* device = adapter_->GetDevice( | |
3284 bluez::FakeBluetoothDeviceClient::kConfirmPasskeyAddress); | |
3285 ASSERT_TRUE(device != nullptr); | |
3286 ASSERT_FALSE(device->IsPaired()); | |
3287 | |
3288 TestBluetoothAdapterObserver observer(adapter_); | |
3289 | |
3290 TestPairingDelegate pairing_delegate; | |
3291 device->Connect(&pairing_delegate, GetCallback(), | |
3292 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback, | |
3293 base::Unretained(this))); | |
3294 | |
3295 EXPECT_EQ(1, pairing_delegate.call_count_); | |
3296 EXPECT_EQ(1, pairing_delegate.confirm_passkey_count_); | |
3297 EXPECT_TRUE(device->IsConnecting()); | |
3298 | |
3299 // Cancel the pairing. | |
3300 device->CancelPairing(); | |
3301 message_loop_.Run(); | |
3302 | |
3303 EXPECT_EQ(0, callback_count_); | |
3304 EXPECT_EQ(1, error_callback_count_); | |
3305 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED, last_connect_error_); | |
3306 | |
3307 // Should be no changes except connecting going true and false. | |
3308 EXPECT_EQ(2, observer.device_changed_count()); | |
3309 EXPECT_FALSE(device->IsConnected()); | |
3310 EXPECT_FALSE(device->IsConnecting()); | |
3311 EXPECT_FALSE(device->IsPaired()); | |
3312 } | |
3313 | |
3314 TEST_F(BluetoothChromeOSTest, PairingCancelledInFlight) { | |
3315 fake_bluetooth_device_client_->SetSimulationIntervalMs(10); | |
3316 | |
3317 GetAdapter(); | |
3318 DiscoverDevices(); | |
3319 | |
3320 // Cancel the pairing while we're waiting for the remote host. | |
3321 BluetoothDevice* device = adapter_->GetDevice( | |
3322 bluez::FakeBluetoothDeviceClient::kLegacyAutopairAddress); | |
3323 ASSERT_TRUE(device != nullptr); | |
3324 ASSERT_FALSE(device->IsPaired()); | |
3325 | |
3326 TestBluetoothAdapterObserver observer(adapter_); | |
3327 | |
3328 TestPairingDelegate pairing_delegate; | |
3329 device->Connect(&pairing_delegate, GetCallback(), | |
3330 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback, | |
3331 base::Unretained(this))); | |
3332 | |
3333 EXPECT_EQ(0, pairing_delegate.call_count_); | |
3334 EXPECT_TRUE(device->IsConnecting()); | |
3335 | |
3336 // Cancel the pairing. | |
3337 device->CancelPairing(); | |
3338 message_loop_.Run(); | |
3339 | |
3340 EXPECT_EQ(0, callback_count_); | |
3341 EXPECT_EQ(1, error_callback_count_); | |
3342 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED, last_connect_error_); | |
3343 | |
3344 // Should be no changes except connecting going true and false. | |
3345 EXPECT_EQ(2, observer.device_changed_count()); | |
3346 EXPECT_FALSE(device->IsConnected()); | |
3347 EXPECT_FALSE(device->IsConnecting()); | |
3348 EXPECT_FALSE(device->IsPaired()); | |
3349 } | |
3350 | |
3351 TEST_F(BluetoothChromeOSTest, IncomingPairRequestPinCode) { | |
3352 fake_bluetooth_device_client_->SetSimulationIntervalMs(10); | |
3353 | |
3354 GetAdapter(); | |
3355 | |
3356 TestPairingDelegate pairing_delegate; | |
3357 adapter_->AddPairingDelegate( | |
3358 &pairing_delegate, | |
3359 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH); | |
3360 | |
3361 // Requires that we provide a PIN code. | |
3362 fake_bluetooth_device_client_->CreateDevice( | |
3363 dbus::ObjectPath(bluez::FakeBluetoothAdapterClient::kAdapterPath), | |
3364 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kRequestPinCodePath)); | |
3365 BluetoothDevice* device = adapter_->GetDevice( | |
3366 bluez::FakeBluetoothDeviceClient::kRequestPinCodeAddress); | |
3367 ASSERT_TRUE(device != nullptr); | |
3368 ASSERT_FALSE(device->IsPaired()); | |
3369 | |
3370 TestBluetoothAdapterObserver observer(adapter_); | |
3371 | |
3372 fake_bluetooth_device_client_->SimulatePairing( | |
3373 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kRequestPinCodePath), | |
3374 true, GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback, | |
3375 base::Unretained(this))); | |
3376 | |
3377 EXPECT_EQ(1, pairing_delegate.call_count_); | |
3378 EXPECT_EQ(1, pairing_delegate.request_pincode_count_); | |
3379 | |
3380 // Set the PIN. | |
3381 device->SetPinCode("1234"); | |
3382 message_loop_.Run(); | |
3383 | |
3384 EXPECT_EQ(1, callback_count_); | |
3385 EXPECT_EQ(0, error_callback_count_); | |
3386 | |
3387 // One change for paired, and one for trusted. | |
3388 EXPECT_EQ(2, observer.device_changed_count()); | |
3389 EXPECT_EQ(device, observer.last_device()); | |
3390 | |
3391 EXPECT_TRUE(device->IsPaired()); | |
3392 | |
3393 // Make sure the trusted property has been set to true. | |
3394 bluez::FakeBluetoothDeviceClient::Properties* properties = | |
3395 fake_bluetooth_device_client_->GetProperties(dbus::ObjectPath( | |
3396 bluez::FakeBluetoothDeviceClient::kRequestPinCodePath)); | |
3397 ASSERT_TRUE(properties->trusted.value()); | |
3398 | |
3399 // No pairing context should remain on the device. | |
3400 BluetoothDeviceChromeOS* device_chromeos = | |
3401 static_cast<BluetoothDeviceChromeOS*>(device); | |
3402 EXPECT_TRUE(device_chromeos->GetPairing() == nullptr); | |
3403 } | |
3404 | |
3405 TEST_F(BluetoothChromeOSTest, IncomingPairConfirmPasskey) { | |
3406 fake_bluetooth_device_client_->SetSimulationIntervalMs(10); | |
3407 | |
3408 GetAdapter(); | |
3409 | |
3410 TestPairingDelegate pairing_delegate; | |
3411 adapter_->AddPairingDelegate( | |
3412 &pairing_delegate, | |
3413 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH); | |
3414 | |
3415 // Requests that we confirm a displayed passkey. | |
3416 fake_bluetooth_device_client_->CreateDevice( | |
3417 dbus::ObjectPath(bluez::FakeBluetoothAdapterClient::kAdapterPath), | |
3418 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kConfirmPasskeyPath)); | |
3419 BluetoothDevice* device = adapter_->GetDevice( | |
3420 bluez::FakeBluetoothDeviceClient::kConfirmPasskeyAddress); | |
3421 ASSERT_TRUE(device != nullptr); | |
3422 ASSERT_FALSE(device->IsPaired()); | |
3423 | |
3424 TestBluetoothAdapterObserver observer(adapter_); | |
3425 | |
3426 fake_bluetooth_device_client_->SimulatePairing( | |
3427 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kConfirmPasskeyPath), | |
3428 true, GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback, | |
3429 base::Unretained(this))); | |
3430 | |
3431 EXPECT_EQ(1, pairing_delegate.call_count_); | |
3432 EXPECT_EQ(1, pairing_delegate.confirm_passkey_count_); | |
3433 EXPECT_EQ(123456U, pairing_delegate.last_passkey_); | |
3434 | |
3435 // Confirm the passkey. | |
3436 device->ConfirmPairing(); | |
3437 message_loop_.Run(); | |
3438 | |
3439 EXPECT_EQ(1, callback_count_); | |
3440 EXPECT_EQ(0, error_callback_count_); | |
3441 | |
3442 // One change for paired, and one for trusted. | |
3443 EXPECT_EQ(2, observer.device_changed_count()); | |
3444 EXPECT_EQ(device, observer.last_device()); | |
3445 | |
3446 EXPECT_TRUE(device->IsPaired()); | |
3447 | |
3448 // Make sure the trusted property has been set to true. | |
3449 bluez::FakeBluetoothDeviceClient::Properties* properties = | |
3450 fake_bluetooth_device_client_->GetProperties(dbus::ObjectPath( | |
3451 bluez::FakeBluetoothDeviceClient::kConfirmPasskeyPath)); | |
3452 ASSERT_TRUE(properties->trusted.value()); | |
3453 | |
3454 // No pairing context should remain on the device. | |
3455 BluetoothDeviceChromeOS* device_chromeos = | |
3456 static_cast<BluetoothDeviceChromeOS*>(device); | |
3457 EXPECT_TRUE(device_chromeos->GetPairing() == nullptr); | |
3458 } | |
3459 | |
3460 TEST_F(BluetoothChromeOSTest, IncomingPairRequestPasskey) { | |
3461 fake_bluetooth_device_client_->SetSimulationIntervalMs(10); | |
3462 | |
3463 GetAdapter(); | |
3464 | |
3465 TestPairingDelegate pairing_delegate; | |
3466 adapter_->AddPairingDelegate( | |
3467 &pairing_delegate, | |
3468 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH); | |
3469 | |
3470 // Requests that we provide a Passkey. | |
3471 fake_bluetooth_device_client_->CreateDevice( | |
3472 dbus::ObjectPath(bluez::FakeBluetoothAdapterClient::kAdapterPath), | |
3473 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kRequestPasskeyPath)); | |
3474 BluetoothDevice* device = adapter_->GetDevice( | |
3475 bluez::FakeBluetoothDeviceClient::kRequestPasskeyAddress); | |
3476 ASSERT_TRUE(device != nullptr); | |
3477 ASSERT_FALSE(device->IsPaired()); | |
3478 | |
3479 TestBluetoothAdapterObserver observer(adapter_); | |
3480 | |
3481 fake_bluetooth_device_client_->SimulatePairing( | |
3482 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kRequestPasskeyPath), | |
3483 true, GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback, | |
3484 base::Unretained(this))); | |
3485 | |
3486 EXPECT_EQ(1, pairing_delegate.call_count_); | |
3487 EXPECT_EQ(1, pairing_delegate.request_passkey_count_); | |
3488 | |
3489 // Set the Passkey. | |
3490 device->SetPasskey(1234); | |
3491 message_loop_.Run(); | |
3492 | |
3493 EXPECT_EQ(1, callback_count_); | |
3494 EXPECT_EQ(0, error_callback_count_); | |
3495 | |
3496 // One change for paired, and one for trusted. | |
3497 EXPECT_EQ(2, observer.device_changed_count()); | |
3498 EXPECT_EQ(device, observer.last_device()); | |
3499 | |
3500 EXPECT_TRUE(device->IsPaired()); | |
3501 | |
3502 // Make sure the trusted property has been set to true. | |
3503 bluez::FakeBluetoothDeviceClient::Properties* properties = | |
3504 fake_bluetooth_device_client_->GetProperties(dbus::ObjectPath( | |
3505 bluez::FakeBluetoothDeviceClient::kRequestPasskeyPath)); | |
3506 ASSERT_TRUE(properties->trusted.value()); | |
3507 | |
3508 // No pairing context should remain on the device. | |
3509 BluetoothDeviceChromeOS* device_chromeos = | |
3510 static_cast<BluetoothDeviceChromeOS*>(device); | |
3511 EXPECT_TRUE(device_chromeos->GetPairing() == nullptr); | |
3512 } | |
3513 | |
3514 TEST_F(BluetoothChromeOSTest, IncomingPairJustWorks) { | |
3515 fake_bluetooth_device_client_->SetSimulationIntervalMs(10); | |
3516 | |
3517 GetAdapter(); | |
3518 | |
3519 TestPairingDelegate pairing_delegate; | |
3520 adapter_->AddPairingDelegate( | |
3521 &pairing_delegate, | |
3522 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH); | |
3523 | |
3524 // Uses just-works pairing so, sinec this an incoming pairing, require | |
3525 // authorization from the user. | |
3526 fake_bluetooth_device_client_->CreateDevice( | |
3527 dbus::ObjectPath(bluez::FakeBluetoothAdapterClient::kAdapterPath), | |
3528 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kJustWorksPath)); | |
3529 BluetoothDevice* device = | |
3530 adapter_->GetDevice(bluez::FakeBluetoothDeviceClient::kJustWorksAddress); | |
3531 ASSERT_TRUE(device != nullptr); | |
3532 ASSERT_FALSE(device->IsPaired()); | |
3533 | |
3534 TestBluetoothAdapterObserver observer(adapter_); | |
3535 | |
3536 fake_bluetooth_device_client_->SimulatePairing( | |
3537 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kJustWorksPath), true, | |
3538 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback, | |
3539 base::Unretained(this))); | |
3540 | |
3541 EXPECT_EQ(1, pairing_delegate.call_count_); | |
3542 EXPECT_EQ(1, pairing_delegate.authorize_pairing_count_); | |
3543 | |
3544 // Confirm the pairing. | |
3545 device->ConfirmPairing(); | |
3546 message_loop_.Run(); | |
3547 | |
3548 EXPECT_EQ(1, callback_count_); | |
3549 EXPECT_EQ(0, error_callback_count_); | |
3550 | |
3551 // One change for paired, and one for trusted. | |
3552 EXPECT_EQ(2, observer.device_changed_count()); | |
3553 EXPECT_EQ(device, observer.last_device()); | |
3554 | |
3555 EXPECT_TRUE(device->IsPaired()); | |
3556 | |
3557 // Make sure the trusted property has been set to true. | |
3558 bluez::FakeBluetoothDeviceClient::Properties* properties = | |
3559 fake_bluetooth_device_client_->GetProperties( | |
3560 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kJustWorksPath)); | |
3561 ASSERT_TRUE(properties->trusted.value()); | |
3562 | |
3563 // No pairing context should remain on the device. | |
3564 BluetoothDeviceChromeOS* device_chromeos = | |
3565 static_cast<BluetoothDeviceChromeOS*>(device); | |
3566 EXPECT_TRUE(device_chromeos->GetPairing() == nullptr); | |
3567 } | |
3568 | |
3569 TEST_F(BluetoothChromeOSTest, IncomingPairRequestPinCodeWithoutDelegate) { | |
3570 fake_bluetooth_device_client_->SetSimulationIntervalMs(10); | |
3571 | |
3572 GetAdapter(); | |
3573 | |
3574 // Requires that we provide a PIN Code, without a pairing delegate, | |
3575 // that will be rejected. | |
3576 fake_bluetooth_device_client_->CreateDevice( | |
3577 dbus::ObjectPath(bluez::FakeBluetoothAdapterClient::kAdapterPath), | |
3578 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kRequestPinCodePath)); | |
3579 BluetoothDevice* device = adapter_->GetDevice( | |
3580 bluez::FakeBluetoothDeviceClient::kRequestPinCodeAddress); | |
3581 ASSERT_TRUE(device != nullptr); | |
3582 ASSERT_FALSE(device->IsPaired()); | |
3583 | |
3584 TestBluetoothAdapterObserver observer(adapter_); | |
3585 | |
3586 fake_bluetooth_device_client_->SimulatePairing( | |
3587 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kRequestPinCodePath), | |
3588 true, GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback, | |
3589 base::Unretained(this))); | |
3590 | |
3591 message_loop_.Run(); | |
3592 | |
3593 EXPECT_EQ(0, callback_count_); | |
3594 EXPECT_EQ(1, error_callback_count_); | |
3595 EXPECT_EQ(bluetooth_device::kErrorAuthenticationRejected, last_client_error_); | |
3596 | |
3597 // No changes should be observer. | |
3598 EXPECT_EQ(0, observer.device_changed_count()); | |
3599 | |
3600 EXPECT_FALSE(device->IsPaired()); | |
3601 | |
3602 // No pairing context should remain on the device. | |
3603 BluetoothDeviceChromeOS* device_chromeos = | |
3604 static_cast<BluetoothDeviceChromeOS*>(device); | |
3605 EXPECT_TRUE(device_chromeos->GetPairing() == nullptr); | |
3606 } | |
3607 | |
3608 TEST_F(BluetoothChromeOSTest, IncomingPairConfirmPasskeyWithoutDelegate) { | |
3609 fake_bluetooth_device_client_->SetSimulationIntervalMs(10); | |
3610 | |
3611 GetAdapter(); | |
3612 | |
3613 // Requests that we confirm a displayed passkey, without a pairing delegate, | |
3614 // that will be rejected. | |
3615 fake_bluetooth_device_client_->CreateDevice( | |
3616 dbus::ObjectPath(bluez::FakeBluetoothAdapterClient::kAdapterPath), | |
3617 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kConfirmPasskeyPath)); | |
3618 BluetoothDevice* device = adapter_->GetDevice( | |
3619 bluez::FakeBluetoothDeviceClient::kConfirmPasskeyAddress); | |
3620 ASSERT_TRUE(device != nullptr); | |
3621 ASSERT_FALSE(device->IsPaired()); | |
3622 | |
3623 TestBluetoothAdapterObserver observer(adapter_); | |
3624 | |
3625 fake_bluetooth_device_client_->SimulatePairing( | |
3626 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kConfirmPasskeyPath), | |
3627 true, GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback, | |
3628 base::Unretained(this))); | |
3629 | |
3630 message_loop_.Run(); | |
3631 | |
3632 EXPECT_EQ(0, callback_count_); | |
3633 EXPECT_EQ(1, error_callback_count_); | |
3634 EXPECT_EQ(bluetooth_device::kErrorAuthenticationRejected, last_client_error_); | |
3635 | |
3636 // No changes should be observer. | |
3637 EXPECT_EQ(0, observer.device_changed_count()); | |
3638 | |
3639 EXPECT_FALSE(device->IsPaired()); | |
3640 | |
3641 // No pairing context should remain on the device. | |
3642 BluetoothDeviceChromeOS* device_chromeos = | |
3643 static_cast<BluetoothDeviceChromeOS*>(device); | |
3644 EXPECT_TRUE(device_chromeos->GetPairing() == nullptr); | |
3645 } | |
3646 | |
3647 TEST_F(BluetoothChromeOSTest, IncomingPairRequestPasskeyWithoutDelegate) { | |
3648 fake_bluetooth_device_client_->SetSimulationIntervalMs(10); | |
3649 | |
3650 GetAdapter(); | |
3651 | |
3652 // Requests that we provide a displayed passkey, without a pairing delegate, | |
3653 // that will be rejected. | |
3654 fake_bluetooth_device_client_->CreateDevice( | |
3655 dbus::ObjectPath(bluez::FakeBluetoothAdapterClient::kAdapterPath), | |
3656 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kRequestPasskeyPath)); | |
3657 BluetoothDevice* device = adapter_->GetDevice( | |
3658 bluez::FakeBluetoothDeviceClient::kRequestPasskeyAddress); | |
3659 ASSERT_TRUE(device != nullptr); | |
3660 ASSERT_FALSE(device->IsPaired()); | |
3661 | |
3662 TestBluetoothAdapterObserver observer(adapter_); | |
3663 | |
3664 fake_bluetooth_device_client_->SimulatePairing( | |
3665 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kRequestPasskeyPath), | |
3666 true, GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback, | |
3667 base::Unretained(this))); | |
3668 | |
3669 message_loop_.Run(); | |
3670 | |
3671 EXPECT_EQ(0, callback_count_); | |
3672 EXPECT_EQ(1, error_callback_count_); | |
3673 EXPECT_EQ(bluetooth_device::kErrorAuthenticationRejected, last_client_error_); | |
3674 | |
3675 // No changes should be observer. | |
3676 EXPECT_EQ(0, observer.device_changed_count()); | |
3677 | |
3678 EXPECT_FALSE(device->IsPaired()); | |
3679 | |
3680 // No pairing context should remain on the device. | |
3681 BluetoothDeviceChromeOS* device_chromeos = | |
3682 static_cast<BluetoothDeviceChromeOS*>(device); | |
3683 EXPECT_TRUE(device_chromeos->GetPairing() == nullptr); | |
3684 } | |
3685 | |
3686 TEST_F(BluetoothChromeOSTest, IncomingPairJustWorksWithoutDelegate) { | |
3687 fake_bluetooth_device_client_->SetSimulationIntervalMs(10); | |
3688 | |
3689 GetAdapter(); | |
3690 | |
3691 // Uses just-works pairing and thus requires authorization for incoming | |
3692 // pairings, without a pairing delegate, that will be rejected. | |
3693 fake_bluetooth_device_client_->CreateDevice( | |
3694 dbus::ObjectPath(bluez::FakeBluetoothAdapterClient::kAdapterPath), | |
3695 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kJustWorksPath)); | |
3696 BluetoothDevice* device = | |
3697 adapter_->GetDevice(bluez::FakeBluetoothDeviceClient::kJustWorksAddress); | |
3698 ASSERT_TRUE(device != nullptr); | |
3699 ASSERT_FALSE(device->IsPaired()); | |
3700 | |
3701 TestBluetoothAdapterObserver observer(adapter_); | |
3702 | |
3703 fake_bluetooth_device_client_->SimulatePairing( | |
3704 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kJustWorksPath), true, | |
3705 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback, | |
3706 base::Unretained(this))); | |
3707 | |
3708 message_loop_.Run(); | |
3709 | |
3710 EXPECT_EQ(0, callback_count_); | |
3711 EXPECT_EQ(1, error_callback_count_); | |
3712 EXPECT_EQ(bluetooth_device::kErrorAuthenticationRejected, last_client_error_); | |
3713 | |
3714 // No changes should be observer. | |
3715 EXPECT_EQ(0, observer.device_changed_count()); | |
3716 | |
3717 EXPECT_FALSE(device->IsPaired()); | |
3718 | |
3719 // No pairing context should remain on the device. | |
3720 BluetoothDeviceChromeOS* device_chromeos = | |
3721 static_cast<BluetoothDeviceChromeOS*>(device); | |
3722 EXPECT_TRUE(device_chromeos->GetPairing() == nullptr); | |
3723 } | |
3724 | |
3725 TEST_F(BluetoothChromeOSTest, RemovePairingDelegateDuringPairing) { | |
3726 fake_bluetooth_device_client_->SetSimulationIntervalMs(10); | |
3727 | |
3728 GetAdapter(); | |
3729 | |
3730 TestPairingDelegate pairing_delegate; | |
3731 adapter_->AddPairingDelegate( | |
3732 &pairing_delegate, | |
3733 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH); | |
3734 | |
3735 // Requests that we provide a Passkey. | |
3736 fake_bluetooth_device_client_->CreateDevice( | |
3737 dbus::ObjectPath(bluez::FakeBluetoothAdapterClient::kAdapterPath), | |
3738 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kRequestPasskeyPath)); | |
3739 BluetoothDevice* device = adapter_->GetDevice( | |
3740 bluez::FakeBluetoothDeviceClient::kRequestPasskeyAddress); | |
3741 ASSERT_TRUE(device != nullptr); | |
3742 ASSERT_FALSE(device->IsPaired()); | |
3743 | |
3744 TestBluetoothAdapterObserver observer(adapter_); | |
3745 | |
3746 fake_bluetooth_device_client_->SimulatePairing( | |
3747 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kRequestPasskeyPath), | |
3748 true, GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback, | |
3749 base::Unretained(this))); | |
3750 | |
3751 EXPECT_EQ(1, pairing_delegate.call_count_); | |
3752 EXPECT_EQ(1, pairing_delegate.request_passkey_count_); | |
3753 | |
3754 // A pairing context should now be set on the device. | |
3755 BluetoothDeviceChromeOS* device_chromeos = | |
3756 static_cast<BluetoothDeviceChromeOS*>(device); | |
3757 ASSERT_TRUE(device_chromeos->GetPairing() != nullptr); | |
3758 | |
3759 // Removing the pairing delegate should remove that pairing context. | |
3760 adapter_->RemovePairingDelegate(&pairing_delegate); | |
3761 | |
3762 EXPECT_TRUE(device_chromeos->GetPairing() == nullptr); | |
3763 | |
3764 // Set the Passkey, this should now have no effect since the pairing has | |
3765 // been, in-effect, cancelled | |
3766 device->SetPasskey(1234); | |
3767 | |
3768 EXPECT_EQ(0, callback_count_); | |
3769 EXPECT_EQ(0, error_callback_count_); | |
3770 EXPECT_EQ(0, observer.device_changed_count()); | |
3771 | |
3772 EXPECT_FALSE(device->IsPaired()); | |
3773 } | |
3774 | |
3775 TEST_F(BluetoothChromeOSTest, DeviceId) { | |
3776 GetAdapter(); | |
3777 | |
3778 // Use the built-in paired device for this test, grab its Properties | |
3779 // structure so we can adjust the underlying modalias property. | |
3780 BluetoothDevice* device = adapter_->GetDevice( | |
3781 bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress); | |
3782 bluez::FakeBluetoothDeviceClient::Properties* properties = | |
3783 fake_bluetooth_device_client_->GetProperties(dbus::ObjectPath( | |
3784 bluez::FakeBluetoothDeviceClient::kPairedDevicePath)); | |
3785 | |
3786 ASSERT_TRUE(device != nullptr); | |
3787 ASSERT_TRUE(properties != nullptr); | |
3788 | |
3789 // Valid USB IF-assigned identifier. | |
3790 ASSERT_EQ("usb:v05ACp030Dd0306", properties->modalias.value()); | |
3791 | |
3792 EXPECT_EQ(BluetoothDevice::VENDOR_ID_USB, device->GetVendorIDSource()); | |
3793 EXPECT_EQ(0x05ac, device->GetVendorID()); | |
3794 EXPECT_EQ(0x030d, device->GetProductID()); | |
3795 EXPECT_EQ(0x0306, device->GetDeviceID()); | |
3796 | |
3797 // Valid Bluetooth SIG-assigned identifier. | |
3798 properties->modalias.ReplaceValue("bluetooth:v00E0p2400d0400"); | |
3799 | |
3800 EXPECT_EQ(BluetoothDevice::VENDOR_ID_BLUETOOTH, device->GetVendorIDSource()); | |
3801 EXPECT_EQ(0x00e0, device->GetVendorID()); | |
3802 EXPECT_EQ(0x2400, device->GetProductID()); | |
3803 EXPECT_EQ(0x0400, device->GetDeviceID()); | |
3804 | |
3805 // Invalid USB IF-assigned identifier. | |
3806 properties->modalias.ReplaceValue("usb:x00E0p2400d0400"); | |
3807 | |
3808 EXPECT_EQ(BluetoothDevice::VENDOR_ID_UNKNOWN, device->GetVendorIDSource()); | |
3809 EXPECT_EQ(0, device->GetVendorID()); | |
3810 EXPECT_EQ(0, device->GetProductID()); | |
3811 EXPECT_EQ(0, device->GetDeviceID()); | |
3812 | |
3813 // Invalid Bluetooth SIG-assigned identifier. | |
3814 properties->modalias.ReplaceValue("bluetooth:x00E0p2400d0400"); | |
3815 | |
3816 EXPECT_EQ(BluetoothDevice::VENDOR_ID_UNKNOWN, device->GetVendorIDSource()); | |
3817 EXPECT_EQ(0, device->GetVendorID()); | |
3818 EXPECT_EQ(0, device->GetProductID()); | |
3819 EXPECT_EQ(0, device->GetDeviceID()); | |
3820 | |
3821 // Unknown vendor specification identifier. | |
3822 properties->modalias.ReplaceValue("chrome:v00E0p2400d0400"); | |
3823 | |
3824 EXPECT_EQ(BluetoothDevice::VENDOR_ID_UNKNOWN, device->GetVendorIDSource()); | |
3825 EXPECT_EQ(0, device->GetVendorID()); | |
3826 EXPECT_EQ(0, device->GetProductID()); | |
3827 EXPECT_EQ(0, device->GetDeviceID()); | |
3828 } | |
3829 | |
3830 TEST_F(BluetoothChromeOSTest, GetConnectionInfoForDisconnectedDevice) { | |
3831 GetAdapter(); | |
3832 BluetoothDevice* device = adapter_->GetDevice( | |
3833 bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress); | |
3834 | |
3835 // Calling GetConnectionInfo for an unconnected device should return a result | |
3836 // in which all fields are filled with BluetoothDevice::kUnknownPower. | |
3837 BluetoothDevice::ConnectionInfo conn_info(0, 0, 0); | |
3838 device->GetConnectionInfo(base::Bind(&SaveConnectionInfo, &conn_info)); | |
3839 int unknown_power = BluetoothDevice::kUnknownPower; | |
3840 EXPECT_NE(0, unknown_power); | |
3841 EXPECT_EQ(unknown_power, conn_info.rssi); | |
3842 EXPECT_EQ(unknown_power, conn_info.transmit_power); | |
3843 EXPECT_EQ(unknown_power, conn_info.max_transmit_power); | |
3844 } | |
3845 | |
3846 TEST_F(BluetoothChromeOSTest, GetConnectionInfoForConnectedDevice) { | |
3847 GetAdapter(); | |
3848 BluetoothDevice* device = adapter_->GetDevice( | |
3849 bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress); | |
3850 | |
3851 device->Connect(nullptr, GetCallback(), | |
3852 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback, | |
3853 base::Unretained(this))); | |
3854 EXPECT_TRUE(device->IsConnected()); | |
3855 | |
3856 // Calling GetConnectionInfo for a connected device should return valid | |
3857 // results. | |
3858 fake_bluetooth_device_client_->UpdateConnectionInfo(-10, 3, 4); | |
3859 BluetoothDevice::ConnectionInfo conn_info; | |
3860 device->GetConnectionInfo(base::Bind(&SaveConnectionInfo, &conn_info)); | |
3861 EXPECT_EQ(-10, conn_info.rssi); | |
3862 EXPECT_EQ(3, conn_info.transmit_power); | |
3863 EXPECT_EQ(4, conn_info.max_transmit_power); | |
3864 } | |
3865 | |
3866 // Verifies Shutdown shuts down the adapter as expected. | |
3867 TEST_F(BluetoothChromeOSTest, Shutdown) { | |
3868 // Set up adapter. Set powered & discoverable, start discovery. | |
3869 GetAdapter(); | |
3870 adapter_->SetPowered(true, GetCallback(), GetErrorCallback()); | |
3871 adapter_->SetDiscoverable(true, GetCallback(), GetErrorCallback()); | |
3872 adapter_->StartDiscoverySession( | |
3873 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback, | |
3874 base::Unretained(this)), | |
3875 GetErrorCallback()); | |
3876 base::MessageLoop::current()->Run(); | |
3877 ASSERT_EQ(3, callback_count_); | |
3878 ASSERT_EQ(0, error_callback_count_); | |
3879 callback_count_ = 0; | |
3880 | |
3881 TestPairingDelegate pairing_delegate; | |
3882 adapter_->AddPairingDelegate( | |
3883 &pairing_delegate, BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH); | |
3884 | |
3885 // Validate running adapter state. | |
3886 EXPECT_NE("", adapter_->GetAddress()); | |
3887 EXPECT_NE("", adapter_->GetName()); | |
3888 EXPECT_TRUE(adapter_->IsInitialized()); | |
3889 EXPECT_TRUE(adapter_->IsPresent()); | |
3890 EXPECT_TRUE(adapter_->IsPowered()); | |
3891 EXPECT_TRUE(adapter_->IsDiscoverable()); | |
3892 EXPECT_TRUE(adapter_->IsDiscovering()); | |
3893 EXPECT_EQ(2U, adapter_->GetDevices().size()); | |
3894 EXPECT_NE(nullptr, | |
3895 adapter_->GetDevice( | |
3896 bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress)); | |
3897 EXPECT_NE(dbus::ObjectPath(""), static_cast<BluetoothAdapterChromeOS*>( | |
3898 adapter_.get())->object_path()); | |
3899 | |
3900 // Shutdown | |
3901 adapter_->Shutdown(); | |
3902 | |
3903 // Validate post shutdown state by calling all BluetoothAdapterChromeOS | |
3904 // members, in declaration order: | |
3905 | |
3906 adapter_->Shutdown(); | |
3907 // DeleteOnCorrectThread omitted as we don't want to delete in this test. | |
3908 { | |
3909 TestBluetoothAdapterObserver observer(adapter_); // Calls AddObserver | |
3910 } // ~TestBluetoothAdapterObserver calls RemoveObserver. | |
3911 EXPECT_EQ("", adapter_->GetAddress()); | |
3912 EXPECT_EQ("", adapter_->GetName()); | |
3913 | |
3914 adapter_->SetName("", GetCallback(), GetErrorCallback()); | |
3915 EXPECT_EQ(0, callback_count_); | |
3916 EXPECT_EQ(1, error_callback_count_--) << "SetName error"; | |
3917 | |
3918 EXPECT_TRUE(adapter_->IsInitialized()); | |
3919 EXPECT_FALSE(adapter_->IsPresent()); | |
3920 EXPECT_FALSE(adapter_->IsPowered()); | |
3921 | |
3922 adapter_->SetPowered(true, GetCallback(), GetErrorCallback()); | |
3923 EXPECT_EQ(0, callback_count_); | |
3924 EXPECT_EQ(1, error_callback_count_--) << "SetPowered error"; | |
3925 | |
3926 EXPECT_FALSE(adapter_->IsDiscoverable()); | |
3927 | |
3928 adapter_->SetDiscoverable(true, GetCallback(), GetErrorCallback()); | |
3929 EXPECT_EQ(0, callback_count_); | |
3930 EXPECT_EQ(1, error_callback_count_--) << "SetDiscoverable error"; | |
3931 | |
3932 EXPECT_FALSE(adapter_->IsDiscovering()); | |
3933 // CreateRfcommService will DCHECK after Shutdown(). | |
3934 // CreateL2capService will DCHECK after Shutdown(). | |
3935 | |
3936 BluetoothAudioSink::Options audio_sink_options; | |
3937 adapter_->RegisterAudioSink( | |
3938 audio_sink_options, | |
3939 base::Bind(&BluetoothChromeOSTest::AudioSinkAcquiredCallback, | |
3940 base::Unretained(this)), | |
3941 base::Bind(&BluetoothChromeOSTest::AudioSinkErrorCallback, | |
3942 base::Unretained(this))); | |
3943 EXPECT_EQ(0, callback_count_); | |
3944 EXPECT_EQ(1, error_callback_count_--) << "RegisterAudioSink error"; | |
3945 | |
3946 BluetoothAdapterChromeOS* adapter_chrome_os = | |
3947 static_cast<BluetoothAdapterChromeOS*>(adapter_.get()); | |
3948 EXPECT_EQ(nullptr, | |
3949 adapter_chrome_os->GetDeviceWithPath(dbus::ObjectPath(""))); | |
3950 | |
3951 // Notify methods presume objects exist that are owned by the adapter and | |
3952 // destroyed in Shutdown(). Mocks are not attempted here that won't exist, | |
3953 // as verified below by EXPECT_EQ(0U, adapter_->GetDevices().size()); | |
3954 // NotifyDeviceChanged | |
3955 // NotifyGattServiceAdded | |
3956 // NotifyGattServiceRemoved | |
3957 // NotifyGattServiceChanged | |
3958 // NotifyGattDiscoveryComplete | |
3959 // NotifyGattCharacteristicAdded | |
3960 // NotifyGattCharacteristicRemoved | |
3961 // NotifyGattDescriptorAdded | |
3962 // NotifyGattDescriptorRemoved | |
3963 // NotifyGattCharacteristicValueChanged | |
3964 // NotifyGattDescriptorValueChanged | |
3965 | |
3966 EXPECT_EQ(dbus::ObjectPath(""), adapter_chrome_os->object_path()); | |
3967 | |
3968 adapter_profile_ = nullptr; | |
3969 | |
3970 FakeBluetoothProfileServiceProviderDelegate profile_delegate; | |
3971 adapter_chrome_os->UseProfile( | |
3972 BluetoothUUID(), dbus::ObjectPath(""), | |
3973 bluez::BluetoothProfileManagerClient::Options(), &profile_delegate, | |
3974 base::Bind(&BluetoothChromeOSTest::ProfileRegisteredCallback, | |
3975 base::Unretained(this)), | |
3976 base::Bind(&BluetoothChromeOSTest::ErrorCompletionCallback, | |
3977 base::Unretained(this))); | |
3978 | |
3979 EXPECT_FALSE(adapter_profile_) << "UseProfile error"; | |
3980 EXPECT_EQ(0, callback_count_) << "UseProfile error"; | |
3981 EXPECT_EQ(1, error_callback_count_--) << "UseProfile error"; | |
3982 | |
3983 // Protected and private methods: | |
3984 | |
3985 adapter_chrome_os->RemovePairingDelegateInternal(&pairing_delegate); | |
3986 // AdapterAdded() invalid post Shutdown because it calls SetAdapter. | |
3987 adapter_chrome_os->AdapterRemoved(dbus::ObjectPath("x")); | |
3988 adapter_chrome_os->AdapterPropertyChanged(dbus::ObjectPath("x"), ""); | |
3989 adapter_chrome_os->DeviceAdded(dbus::ObjectPath("")); | |
3990 adapter_chrome_os->DeviceRemoved(dbus::ObjectPath("")); | |
3991 adapter_chrome_os->DevicePropertyChanged(dbus::ObjectPath(""), ""); | |
3992 adapter_chrome_os->InputPropertyChanged(dbus::ObjectPath(""), ""); | |
3993 // bluez::BluetoothAgentServiceProvider::Delegate omitted, dbus will be | |
3994 // shutdown, | |
3995 // with the exception of Released. | |
3996 adapter_chrome_os->Released(); | |
3997 | |
3998 adapter_chrome_os->OnRegisterAgent(); | |
3999 adapter_chrome_os->OnRegisterAgentError("", ""); | |
4000 adapter_chrome_os->OnRequestDefaultAgent(); | |
4001 adapter_chrome_os->OnRequestDefaultAgentError("", ""); | |
4002 | |
4003 adapter_chrome_os->OnRegisterAudioSink( | |
4004 base::Bind(&BluetoothChromeOSTest::AudioSinkAcquiredCallback, | |
4005 base::Unretained(this)), | |
4006 base::Bind(&BluetoothChromeOSTest::AudioSinkErrorCallback, | |
4007 base::Unretained(this)), | |
4008 scoped_refptr<device::BluetoothAudioSink>()); | |
4009 EXPECT_EQ(0, callback_count_); | |
4010 EXPECT_EQ(1, error_callback_count_--) << "RegisterAudioSink error"; | |
4011 | |
4012 // GetPairing will DCHECK after Shutdown(). | |
4013 // SetAdapter will DCHECK after Shutdown(). | |
4014 // SetDefaultAdapterName will DCHECK after Shutdown(). | |
4015 // RemoveAdapter will DCHECK after Shutdown(). | |
4016 adapter_chrome_os->PoweredChanged(false); | |
4017 adapter_chrome_os->DiscoverableChanged(false); | |
4018 adapter_chrome_os->DiscoveringChanged(false); | |
4019 adapter_chrome_os->PresentChanged(false); | |
4020 | |
4021 adapter_chrome_os->OnSetDiscoverable(GetCallback(), GetErrorCallback(), true); | |
4022 EXPECT_EQ(0, callback_count_) << "OnSetDiscoverable error"; | |
4023 EXPECT_EQ(1, error_callback_count_--) << "OnSetDiscoverable error"; | |
4024 | |
4025 adapter_chrome_os->OnPropertyChangeCompleted(GetCallback(), | |
4026 GetErrorCallback(), true); | |
4027 EXPECT_EQ(0, callback_count_) << "OnPropertyChangeCompleted error"; | |
4028 EXPECT_EQ(1, error_callback_count_--) << "OnPropertyChangeCompleted error"; | |
4029 | |
4030 adapter_chrome_os->AddDiscoverySession(nullptr, GetCallback(), | |
4031 GetDiscoveryErrorCallback()); | |
4032 EXPECT_EQ(0, callback_count_) << "AddDiscoverySession error"; | |
4033 EXPECT_EQ(1, error_callback_count_--) << "AddDiscoverySession error"; | |
4034 | |
4035 adapter_chrome_os->RemoveDiscoverySession(nullptr, GetCallback(), | |
4036 GetDiscoveryErrorCallback()); | |
4037 EXPECT_EQ(0, callback_count_) << "RemoveDiscoverySession error"; | |
4038 EXPECT_EQ(1, error_callback_count_--) << "RemoveDiscoverySession error"; | |
4039 | |
4040 // OnStartDiscovery tested in Shutdown_OnStartDiscovery | |
4041 // OnStartDiscoveryError tested in Shutdown_OnStartDiscoveryError | |
4042 // OnStopDiscovery tested in Shutdown_OnStopDiscovery | |
4043 // OnStopDiscoveryError tested in Shutdown_OnStopDiscoveryError | |
4044 | |
4045 adapter_profile_ = nullptr; | |
4046 | |
4047 // OnRegisterProfile SetProfileDelegate, OnRegisterProfileError, require | |
4048 // UseProfile to be set first, do so again here just before calling them. | |
4049 adapter_chrome_os->UseProfile( | |
4050 BluetoothUUID(), dbus::ObjectPath(""), | |
4051 bluez::BluetoothProfileManagerClient::Options(), &profile_delegate, | |
4052 base::Bind(&BluetoothChromeOSTest::ProfileRegisteredCallback, | |
4053 base::Unretained(this)), | |
4054 base::Bind(&BluetoothChromeOSTest::ErrorCompletionCallback, | |
4055 base::Unretained(this))); | |
4056 | |
4057 EXPECT_FALSE(adapter_profile_) << "UseProfile error"; | |
4058 EXPECT_EQ(0, callback_count_) << "UseProfile error"; | |
4059 EXPECT_EQ(1, error_callback_count_--) << "UseProfile error"; | |
4060 | |
4061 adapter_chrome_os->SetProfileDelegate( | |
4062 BluetoothUUID(), dbus::ObjectPath(""), &profile_delegate, | |
4063 base::Bind(&BluetoothChromeOSTest::ProfileRegisteredCallback, | |
4064 base::Unretained(this)), | |
4065 base::Bind(&BluetoothChromeOSTest::ErrorCompletionCallback, | |
4066 base::Unretained(this))); | |
4067 EXPECT_EQ(0, callback_count_) << "SetProfileDelegate error"; | |
4068 EXPECT_EQ(1, error_callback_count_--) << "SetProfileDelegate error"; | |
4069 | |
4070 adapter_chrome_os->OnRegisterProfileError(BluetoothUUID(), "", ""); | |
4071 EXPECT_EQ(0, callback_count_) << "OnRegisterProfileError error"; | |
4072 EXPECT_EQ(0, error_callback_count_) << "OnRegisterProfileError error"; | |
4073 | |
4074 adapter_chrome_os->ProcessQueuedDiscoveryRequests(); | |
4075 | |
4076 // From BluetoothAdapater: | |
4077 | |
4078 adapter_->StartDiscoverySession( | |
4079 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback, | |
4080 base::Unretained(this)), | |
4081 GetErrorCallback()); | |
4082 EXPECT_EQ(0, callback_count_) << "StartDiscoverySession error"; | |
4083 EXPECT_EQ(1, error_callback_count_--) << "StartDiscoverySession error"; | |
4084 | |
4085 EXPECT_EQ(0U, adapter_->GetDevices().size()); | |
4086 EXPECT_EQ(nullptr, | |
4087 adapter_->GetDevice( | |
4088 bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress)); | |
4089 TestPairingDelegate pairing_delegate2; | |
4090 adapter_->AddPairingDelegate( | |
4091 &pairing_delegate2, BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH); | |
4092 adapter_->RemovePairingDelegate(&pairing_delegate2); | |
4093 } | |
4094 | |
4095 // Verifies post-Shutdown of discovery sessions and OnStartDiscovery. | |
4096 TEST_F(BluetoothChromeOSTest, Shutdown_OnStartDiscovery) { | |
4097 const int kNumberOfDiscoverySessions = 10; | |
4098 GetAdapter(); | |
4099 BluetoothAdapterChromeOS* adapter_chrome_os = | |
4100 static_cast<BluetoothAdapterChromeOS*>(adapter_.get()); | |
4101 | |
4102 for (int i = 0; i < kNumberOfDiscoverySessions; i++) { | |
4103 adapter_chrome_os->AddDiscoverySession(nullptr, GetCallback(), | |
4104 GetDiscoveryErrorCallback()); | |
4105 } | |
4106 adapter_->Shutdown(); | |
4107 adapter_chrome_os->OnStartDiscovery(GetCallback(), | |
4108 GetDiscoveryErrorCallback()); | |
4109 | |
4110 EXPECT_EQ(0, callback_count_); | |
4111 EXPECT_EQ(kNumberOfDiscoverySessions, error_callback_count_); | |
4112 } | |
4113 | |
4114 // Verifies post-Shutdown of discovery sessions and OnStartDiscoveryError. | |
4115 TEST_F(BluetoothChromeOSTest, Shutdown_OnStartDiscoveryError) { | |
4116 const int kNumberOfDiscoverySessions = 10; | |
4117 GetAdapter(); | |
4118 BluetoothAdapterChromeOS* adapter_chrome_os = | |
4119 static_cast<BluetoothAdapterChromeOS*>(adapter_.get()); | |
4120 | |
4121 for (int i = 0; i < kNumberOfDiscoverySessions; i++) { | |
4122 adapter_chrome_os->AddDiscoverySession(nullptr, GetCallback(), | |
4123 GetDiscoveryErrorCallback()); | |
4124 } | |
4125 adapter_->Shutdown(); | |
4126 adapter_chrome_os->OnStartDiscoveryError(GetCallback(), | |
4127 GetDiscoveryErrorCallback(), "", ""); | |
4128 | |
4129 EXPECT_EQ(0, callback_count_); | |
4130 EXPECT_EQ(kNumberOfDiscoverySessions, error_callback_count_); | |
4131 } | |
4132 | |
4133 // Verifies post-Shutdown of discovery sessions and OnStartDiscovery. | |
4134 TEST_F(BluetoothChromeOSTest, Shutdown_OnStopDiscovery) { | |
4135 const int kNumberOfDiscoverySessions = 10; | |
4136 GetAdapter(); | |
4137 BluetoothAdapterChromeOS* adapter_chrome_os = | |
4138 static_cast<BluetoothAdapterChromeOS*>(adapter_.get()); | |
4139 | |
4140 // In order to queue up discovery sessions before an OnStopDiscovery call | |
4141 // RemoveDiscoverySession must be called, so Add, Start, and Remove: | |
4142 adapter_chrome_os->AddDiscoverySession(nullptr, GetCallback(), | |
4143 GetDiscoveryErrorCallback()); | |
4144 adapter_chrome_os->OnStartDiscovery(GetCallback(), | |
4145 GetDiscoveryErrorCallback()); | |
4146 adapter_chrome_os->RemoveDiscoverySession(nullptr, GetCallback(), | |
4147 GetDiscoveryErrorCallback()); | |
4148 callback_count_ = 0; | |
4149 error_callback_count_ = 0; | |
4150 // Can now queue discovery sessions while waiting for OnStopDiscovery. | |
4151 for (int i = 0; i < kNumberOfDiscoverySessions; i++) { | |
4152 adapter_chrome_os->AddDiscoverySession(nullptr, GetCallback(), | |
4153 GetDiscoveryErrorCallback()); | |
4154 } | |
4155 adapter_->Shutdown(); | |
4156 adapter_chrome_os->OnStopDiscovery(GetCallback()); | |
4157 | |
4158 // 1 successful stopped discovery from RemoveDiscoverySession, and | |
4159 // kNumberOfDiscoverySessions errors from AddDiscoverySession/OnStopDiscovery. | |
4160 EXPECT_EQ(1, callback_count_); | |
4161 EXPECT_EQ(kNumberOfDiscoverySessions, error_callback_count_); | |
4162 } | |
4163 | |
4164 // Verifies post-Shutdown of discovery sessions and OnStopDiscoveryError. | |
4165 TEST_F(BluetoothChromeOSTest, Shutdown_OnStopDiscoveryError) { | |
4166 const int kNumberOfDiscoverySessions = 10; | |
4167 GetAdapter(); | |
4168 BluetoothAdapterChromeOS* adapter_chrome_os = | |
4169 static_cast<BluetoothAdapterChromeOS*>(adapter_.get()); | |
4170 | |
4171 // In order to queue up discovery sessions before an OnStopDiscoveryError call | |
4172 // RemoveDiscoverySession must be called, so Add, Start, and Remove: | |
4173 adapter_chrome_os->AddDiscoverySession(nullptr, GetCallback(), | |
4174 GetDiscoveryErrorCallback()); | |
4175 adapter_chrome_os->OnStartDiscovery(GetCallback(), | |
4176 GetDiscoveryErrorCallback()); | |
4177 adapter_chrome_os->RemoveDiscoverySession(nullptr, GetCallback(), | |
4178 GetDiscoveryErrorCallback()); | |
4179 callback_count_ = 0; | |
4180 error_callback_count_ = 0; | |
4181 // Can now queue discovery sessions while waiting for OnStopDiscoveryError. | |
4182 for (int i = 0; i < kNumberOfDiscoverySessions; i++) { | |
4183 adapter_chrome_os->AddDiscoverySession(nullptr, GetCallback(), | |
4184 GetDiscoveryErrorCallback()); | |
4185 } | |
4186 adapter_->Shutdown(); | |
4187 adapter_chrome_os->OnStopDiscoveryError(GetDiscoveryErrorCallback(), "", ""); | |
4188 | |
4189 // 1 error reported to RemoveDiscoverySession because of OnStopDiscoveryError, | |
4190 // and kNumberOfDiscoverySessions errors queued with AddDiscoverySession. | |
4191 EXPECT_EQ(0, callback_count_); | |
4192 EXPECT_EQ(1 + kNumberOfDiscoverySessions, error_callback_count_); | |
4193 } | |
4194 | |
4195 } // namespace chromeos | |
OLD | NEW |