Add OWNERS to content/browser/quota
[chromium-blink-merge.git] / device / bluetooth / bluetooth_chromeos_unittest.cc
blobfbdfbb1e0fbfbaf5b1a2101b0ce3757ae79ec6a5
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.
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 "chromeos/dbus/dbus_thread_manager.h"
10 #include "chromeos/dbus/fake_bluetooth_adapter_client.h"
11 #include "chromeos/dbus/fake_bluetooth_agent_manager_client.h"
12 #include "chromeos/dbus/fake_bluetooth_device_client.h"
13 #include "chromeos/dbus/fake_bluetooth_gatt_service_client.h"
14 #include "chromeos/dbus/fake_bluetooth_input_client.h"
15 #include "dbus/object_path.h"
16 #include "device/bluetooth/bluetooth_adapter.h"
17 #include "device/bluetooth/bluetooth_adapter_chromeos.h"
18 #include "device/bluetooth/bluetooth_adapter_factory.h"
19 #include "device/bluetooth/bluetooth_device.h"
20 #include "device/bluetooth/bluetooth_device_chromeos.h"
21 #include "device/bluetooth/bluetooth_discovery_session.h"
22 #include "device/bluetooth/bluetooth_pairing_chromeos.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"
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;
36 namespace chromeos {
38 namespace {
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;
47 class FakeBluetoothProfileServiceProviderDelegate
48 : public chromeos::BluetoothProfileServiceProvider::Delegate {
49 public:
50 FakeBluetoothProfileServiceProviderDelegate() {}
52 // BluetoothProfileServiceProvider::Delegate:
53 void Released() override {}
55 void NewConnection(const dbus::ObjectPath&,
56 scoped_ptr<dbus::FileDescriptor>,
57 const BluetoothProfileServiceProvider::Delegate::Options&,
58 const ConfirmationCallback&) override {}
60 void RequestDisconnection(const dbus::ObjectPath&,
61 const ConfirmationCallback&) override {}
63 void Cancel() override {}
66 } // namespace
68 class TestPairingDelegate : public BluetoothDevice::PairingDelegate {
69 public:
70 TestPairingDelegate()
71 : call_count_(0),
72 request_pincode_count_(0),
73 request_passkey_count_(0),
74 display_pincode_count_(0),
75 display_passkey_count_(0),
76 keys_entered_count_(0),
77 confirm_passkey_count_(0),
78 authorize_pairing_count_(0),
79 last_passkey_(9999999U),
80 last_entered_(999U) {}
81 ~TestPairingDelegate() override {}
83 void RequestPinCode(BluetoothDevice* device) override {
84 ++call_count_;
85 ++request_pincode_count_;
86 QuitMessageLoop();
89 void RequestPasskey(BluetoothDevice* device) override {
90 ++call_count_;
91 ++request_passkey_count_;
92 QuitMessageLoop();
95 void DisplayPinCode(BluetoothDevice* device,
96 const std::string& pincode) override {
97 ++call_count_;
98 ++display_pincode_count_;
99 last_pincode_ = pincode;
100 QuitMessageLoop();
103 void DisplayPasskey(BluetoothDevice* device, uint32 passkey) override {
104 ++call_count_;
105 ++display_passkey_count_;
106 last_passkey_ = passkey;
107 QuitMessageLoop();
110 void KeysEntered(BluetoothDevice* device, uint32 entered) override {
111 ++call_count_;
112 ++keys_entered_count_;
113 last_entered_ = entered;
114 QuitMessageLoop();
117 void ConfirmPasskey(BluetoothDevice* device, uint32 passkey) override {
118 ++call_count_;
119 ++confirm_passkey_count_;
120 last_passkey_ = passkey;
121 QuitMessageLoop();
124 void AuthorizePairing(BluetoothDevice* device) override {
125 ++call_count_;
126 ++authorize_pairing_count_;
127 QuitMessageLoop();
130 int call_count_;
131 int request_pincode_count_;
132 int request_passkey_count_;
133 int display_pincode_count_;
134 int display_passkey_count_;
135 int keys_entered_count_;
136 int confirm_passkey_count_;
137 int authorize_pairing_count_;
138 uint32 last_passkey_;
139 uint32 last_entered_;
140 std::string last_pincode_;
142 private:
143 // Some tests use a message loop since background processing is simulated;
144 // break out of those loops.
145 void QuitMessageLoop() {
146 if (base::MessageLoop::current() &&
147 base::MessageLoop::current()->is_running()) {
148 base::MessageLoop::current()->Quit();
153 class BluetoothChromeOSTest : public testing::Test {
154 public:
155 void SetUp() override {
156 scoped_ptr<DBusThreadManagerSetter> dbus_setter =
157 chromeos::DBusThreadManager::GetSetterForTesting();
158 // We need to initialize DBusThreadManager early to prevent
159 // Bluetooth*::Create() methods from picking the real instead of fake
160 // implementations.
161 fake_bluetooth_adapter_client_ = new FakeBluetoothAdapterClient;
162 dbus_setter->SetBluetoothAdapterClient(
163 scoped_ptr<BluetoothAdapterClient>(fake_bluetooth_adapter_client_));
164 fake_bluetooth_device_client_ = new FakeBluetoothDeviceClient;
165 dbus_setter->SetBluetoothDeviceClient(
166 scoped_ptr<BluetoothDeviceClient>(fake_bluetooth_device_client_));
167 dbus_setter->SetBluetoothInputClient(
168 scoped_ptr<BluetoothInputClient>(new FakeBluetoothInputClient));
169 dbus_setter->SetBluetoothAgentManagerClient(
170 scoped_ptr<BluetoothAgentManagerClient>(
171 new FakeBluetoothAgentManagerClient));
172 dbus_setter->SetBluetoothGattServiceClient(
173 scoped_ptr<BluetoothGattServiceClient>(
174 new FakeBluetoothGattServiceClient));
176 fake_bluetooth_adapter_client_->SetSimulationIntervalMs(10);
178 callback_count_ = 0;
179 error_callback_count_ = 0;
180 last_connect_error_ = BluetoothDevice::ERROR_UNKNOWN;
181 last_client_error_ = "";
184 void TearDown() override {
185 for (ScopedVector<BluetoothDiscoverySession>::iterator iter =
186 discovery_sessions_.begin();
187 iter != discovery_sessions_.end();
188 ++iter) {
189 BluetoothDiscoverySession* session = *iter;
190 if (!session->IsActive())
191 continue;
192 callback_count_ = 0;
193 session->Stop(GetCallback(), GetErrorCallback());
194 message_loop_.Run();
195 ASSERT_EQ(1, callback_count_);
197 discovery_sessions_.clear();
198 adapter_ = nullptr;
199 DBusThreadManager::Shutdown();
202 // Generic callbacks
203 void Callback() {
204 ++callback_count_;
205 QuitMessageLoop();
208 base::Closure GetCallback() {
209 return base::Bind(&BluetoothChromeOSTest::Callback, base::Unretained(this));
212 void DiscoverySessionCallback(
213 scoped_ptr<BluetoothDiscoverySession> discovery_session) {
214 ++callback_count_;
215 discovery_sessions_.push_back(discovery_session.release());
216 QuitMessageLoop();
219 void AudioSinkAcquiredCallback(scoped_refptr<BluetoothAudioSink>) {
220 ++callback_count_;
221 QuitMessageLoop();
224 void ProfileRegisteredCallback(BluetoothAdapterProfileChromeOS* profile) {
225 adapter_profile_ = profile;
226 ++callback_count_;
227 QuitMessageLoop();
230 void ErrorCallback() {
231 ++error_callback_count_;
232 QuitMessageLoop();
235 void DiscoveryErrorCallback(device::UMABluetoothDiscoverySessionOutcome) {
236 ErrorCallback();
239 base::Closure GetErrorCallback() {
240 return base::Bind(&BluetoothChromeOSTest::ErrorCallback,
241 base::Unretained(this));
244 base::Callback<void(device::UMABluetoothDiscoverySessionOutcome)>
245 GetDiscoveryErrorCallback() {
246 return base::Bind(&BluetoothChromeOSTest::DiscoveryErrorCallback,
247 base::Unretained(this));
250 void DBusErrorCallback(const std::string& error_name,
251 const std::string& error_message) {
252 ++error_callback_count_;
253 last_client_error_ = error_name;
254 QuitMessageLoop();
257 void ConnectErrorCallback(BluetoothDevice::ConnectErrorCode error) {
258 ++error_callback_count_;
259 last_connect_error_ = error;
262 void AudioSinkErrorCallback(BluetoothAudioSink::ErrorCode) {
263 ++error_callback_count_;
264 QuitMessageLoop();
267 void ErrorCompletionCallback(const std::string& error_message) {
268 ++error_callback_count_;
269 QuitMessageLoop();
272 // Call to fill the adapter_ member with a BluetoothAdapter instance.
273 void GetAdapter() {
274 adapter_ = new BluetoothAdapterChromeOS();
275 ASSERT_TRUE(adapter_.get() != nullptr);
276 ASSERT_TRUE(adapter_->IsInitialized());
279 // Run a discovery phase until the named device is detected, or if the named
280 // device is not created, the discovery process ends without finding it.
282 // The correct behavior of discovery is tested by the "Discovery" test case
283 // without using this function.
284 void DiscoverDevice(const std::string& address) {
285 ASSERT_TRUE(adapter_.get() != nullptr);
286 ASSERT_TRUE(base::MessageLoop::current() != nullptr);
287 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
289 TestBluetoothAdapterObserver observer(adapter_);
291 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
292 adapter_->StartDiscoverySession(
293 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
294 base::Unretained(this)),
295 GetErrorCallback());
296 base::MessageLoop::current()->Run();
297 ASSERT_EQ(2, callback_count_);
298 ASSERT_EQ(0, error_callback_count_);
299 ASSERT_EQ((size_t)1, discovery_sessions_.size());
300 ASSERT_TRUE(discovery_sessions_[0]->IsActive());
301 callback_count_ = 0;
303 ASSERT_TRUE(adapter_->IsPowered());
304 ASSERT_TRUE(adapter_->IsDiscovering());
306 while (!observer.device_removed_count() &&
307 observer.last_device_address() != address)
308 base::MessageLoop::current()->Run();
310 discovery_sessions_[0]->Stop(GetCallback(), GetErrorCallback());
311 base::MessageLoop::current()->Run();
312 ASSERT_EQ(1, callback_count_);
313 ASSERT_EQ(0, error_callback_count_);
314 callback_count_ = 0;
316 ASSERT_FALSE(adapter_->IsDiscovering());
319 // Run a discovery phase so we have devices that can be paired with.
320 void DiscoverDevices() {
321 // Pass an invalid address for the device so that the discovery process
322 // completes with all devices.
323 DiscoverDevice("does not exist");
326 protected:
327 base::MessageLoop message_loop_;
328 FakeBluetoothAdapterClient* fake_bluetooth_adapter_client_;
329 FakeBluetoothDeviceClient* fake_bluetooth_device_client_;
330 scoped_refptr<BluetoothAdapter> adapter_;
332 int callback_count_;
333 int error_callback_count_;
334 enum BluetoothDevice::ConnectErrorCode last_connect_error_;
335 std::string last_client_error_;
336 ScopedVector<BluetoothDiscoverySession> discovery_sessions_;
337 BluetoothAdapterProfileChromeOS* adapter_profile_;
339 private:
340 // Some tests use a message loop since background processing is simulated;
341 // break out of those loops.
342 void QuitMessageLoop() {
343 if (base::MessageLoop::current() &&
344 base::MessageLoop::current()->is_running()) {
345 base::MessageLoop::current()->Quit();
350 TEST_F(BluetoothChromeOSTest, AlreadyPresent) {
351 GetAdapter();
353 // This verifies that the class gets the list of adapters when created;
354 // and initializes with an existing adapter if there is one.
355 EXPECT_TRUE(adapter_->IsPresent());
356 EXPECT_FALSE(adapter_->IsPowered());
357 EXPECT_EQ(FakeBluetoothAdapterClient::kAdapterAddress,
358 adapter_->GetAddress());
359 EXPECT_FALSE(adapter_->IsDiscovering());
361 // There should be a device
362 BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
363 EXPECT_EQ(2U, devices.size());
364 EXPECT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
365 devices[0]->GetAddress());
366 EXPECT_EQ(FakeBluetoothDeviceClient::kPairedUnconnectableDeviceAddress,
367 devices[1]->GetAddress());
370 TEST_F(BluetoothChromeOSTest, BecomePresent) {
371 fake_bluetooth_adapter_client_->SetVisible(false);
372 GetAdapter();
373 ASSERT_FALSE(adapter_->IsPresent());
375 // Install an observer; expect the AdapterPresentChanged to be called
376 // with true, and IsPresent() to return true.
377 TestBluetoothAdapterObserver observer(adapter_);
379 fake_bluetooth_adapter_client_->SetVisible(true);
381 EXPECT_EQ(1, observer.present_changed_count());
382 EXPECT_TRUE(observer.last_present());
384 EXPECT_TRUE(adapter_->IsPresent());
386 // We should have had a device announced.
387 EXPECT_EQ(2, observer.device_added_count());
388 EXPECT_EQ(FakeBluetoothDeviceClient::kPairedUnconnectableDeviceAddress,
389 observer.last_device_address());
391 // Other callbacks shouldn't be called if the values are false.
392 EXPECT_EQ(0, observer.powered_changed_count());
393 EXPECT_EQ(0, observer.discovering_changed_count());
394 EXPECT_FALSE(adapter_->IsPowered());
395 EXPECT_FALSE(adapter_->IsDiscovering());
398 TEST_F(BluetoothChromeOSTest, BecomeNotPresent) {
399 GetAdapter();
400 ASSERT_TRUE(adapter_->IsPresent());
402 // Install an observer; expect the AdapterPresentChanged to be called
403 // with false, and IsPresent() to return false.
404 TestBluetoothAdapterObserver observer(adapter_);
406 fake_bluetooth_adapter_client_->SetVisible(false);
408 EXPECT_EQ(1, observer.present_changed_count());
409 EXPECT_FALSE(observer.last_present());
411 EXPECT_FALSE(adapter_->IsPresent());
413 // We should have had a device removed.
414 EXPECT_EQ(2, observer.device_removed_count());
415 EXPECT_EQ(FakeBluetoothDeviceClient::kPairedUnconnectableDeviceAddress,
416 observer.last_device_address());
418 // Other callbacks shouldn't be called since the values are false.
419 EXPECT_EQ(0, observer.powered_changed_count());
420 EXPECT_EQ(0, observer.discovering_changed_count());
421 EXPECT_FALSE(adapter_->IsPowered());
422 EXPECT_FALSE(adapter_->IsDiscovering());
425 TEST_F(BluetoothChromeOSTest, SecondAdapter) {
426 GetAdapter();
427 ASSERT_TRUE(adapter_->IsPresent());
429 // Install an observer, then add a second adapter. Nothing should change,
430 // we ignore the second adapter.
431 TestBluetoothAdapterObserver observer(adapter_);
433 fake_bluetooth_adapter_client_->SetSecondVisible(true);
435 EXPECT_EQ(0, observer.present_changed_count());
437 EXPECT_TRUE(adapter_->IsPresent());
438 EXPECT_EQ(FakeBluetoothAdapterClient::kAdapterAddress,
439 adapter_->GetAddress());
441 // Try removing the first adapter, we should now act as if the adapter
442 // is no longer present rather than fall back to the second.
443 fake_bluetooth_adapter_client_->SetVisible(false);
445 EXPECT_EQ(1, observer.present_changed_count());
446 EXPECT_FALSE(observer.last_present());
448 EXPECT_FALSE(adapter_->IsPresent());
450 // We should have had a device removed.
451 EXPECT_EQ(2, observer.device_removed_count());
452 EXPECT_EQ(FakeBluetoothDeviceClient::kPairedUnconnectableDeviceAddress,
453 observer.last_device_address());
455 // Other callbacks shouldn't be called since the values are false.
456 EXPECT_EQ(0, observer.powered_changed_count());
457 EXPECT_EQ(0, observer.discovering_changed_count());
458 EXPECT_FALSE(adapter_->IsPowered());
459 EXPECT_FALSE(adapter_->IsDiscovering());
461 observer.Reset();
463 // Removing the second adapter shouldn't set anything either.
464 fake_bluetooth_adapter_client_->SetSecondVisible(false);
466 EXPECT_EQ(0, observer.device_removed_count());
467 EXPECT_EQ(0, observer.powered_changed_count());
468 EXPECT_EQ(0, observer.discovering_changed_count());
471 TEST_F(BluetoothChromeOSTest, BecomePowered) {
472 GetAdapter();
473 ASSERT_FALSE(adapter_->IsPowered());
475 // Install an observer; expect the AdapterPoweredChanged to be called
476 // with true, and IsPowered() to return true.
477 TestBluetoothAdapterObserver observer(adapter_);
479 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
480 EXPECT_EQ(1, callback_count_);
481 EXPECT_EQ(0, error_callback_count_);
483 EXPECT_EQ(1, observer.powered_changed_count());
484 EXPECT_TRUE(observer.last_powered());
486 EXPECT_TRUE(adapter_->IsPowered());
489 TEST_F(BluetoothChromeOSTest, BecomeNotPowered) {
490 GetAdapter();
491 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
492 EXPECT_EQ(1, callback_count_);
493 EXPECT_EQ(0, error_callback_count_);
494 callback_count_ = 0;
496 ASSERT_TRUE(adapter_->IsPowered());
498 // Install an observer; expect the AdapterPoweredChanged to be called
499 // with false, and IsPowered() to return false.
500 TestBluetoothAdapterObserver observer(adapter_);
502 adapter_->SetPowered(false, GetCallback(), GetErrorCallback());
503 EXPECT_EQ(1, callback_count_);
504 EXPECT_EQ(0, error_callback_count_);
506 EXPECT_EQ(1, observer.powered_changed_count());
507 EXPECT_FALSE(observer.last_powered());
509 EXPECT_FALSE(adapter_->IsPowered());
512 TEST_F(BluetoothChromeOSTest, SetPoweredWhenNotPresent) {
513 GetAdapter();
514 ASSERT_TRUE(adapter_->IsPresent());
516 // Install an observer; expect the AdapterPresentChanged to be called
517 // with false, and IsPresent() to return false.
518 TestBluetoothAdapterObserver observer(adapter_);
520 fake_bluetooth_adapter_client_->SetVisible(false);
522 EXPECT_EQ(1, observer.present_changed_count());
523 EXPECT_FALSE(observer.last_present());
525 EXPECT_FALSE(adapter_->IsPresent());
526 EXPECT_FALSE(adapter_->IsPowered());
528 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
529 EXPECT_EQ(0, callback_count_);
530 EXPECT_EQ(1, error_callback_count_);
532 EXPECT_EQ(0, observer.powered_changed_count());
533 EXPECT_FALSE(observer.last_powered());
535 EXPECT_FALSE(adapter_->IsPowered());
538 TEST_F(BluetoothChromeOSTest, ChangeAdapterName) {
539 GetAdapter();
541 static const std::string new_name(".__.");
543 adapter_->SetName(new_name, GetCallback(), GetErrorCallback());
544 EXPECT_EQ(1, callback_count_);
545 EXPECT_EQ(0, error_callback_count_);
547 EXPECT_EQ(new_name, adapter_->GetName());
550 TEST_F(BluetoothChromeOSTest, ChangeAdapterNameWhenNotPresent) {
551 GetAdapter();
552 ASSERT_TRUE(adapter_->IsPresent());
554 // Install an observer; expect the AdapterPresentChanged to be called
555 // with false, and IsPresent() to return false.
556 TestBluetoothAdapterObserver observer(adapter_);
558 fake_bluetooth_adapter_client_->SetVisible(false);
560 EXPECT_EQ(1, observer.present_changed_count());
561 EXPECT_FALSE(observer.last_present());
563 EXPECT_FALSE(adapter_->IsPresent());
564 EXPECT_FALSE(adapter_->IsPowered());
566 adapter_->SetName("^o^", GetCallback(), GetErrorCallback());
567 EXPECT_EQ(0, callback_count_);
568 EXPECT_EQ(1, error_callback_count_);
570 EXPECT_EQ("", adapter_->GetName());
573 TEST_F(BluetoothChromeOSTest, BecomeDiscoverable) {
574 GetAdapter();
575 ASSERT_FALSE(adapter_->IsDiscoverable());
577 // Install an observer; expect the AdapterDiscoverableChanged to be called
578 // with true, and IsDiscoverable() to return true.
579 TestBluetoothAdapterObserver observer(adapter_);
581 adapter_->SetDiscoverable(true, GetCallback(), GetErrorCallback());
582 EXPECT_EQ(1, callback_count_);
583 EXPECT_EQ(0, error_callback_count_);
585 EXPECT_EQ(1, observer.discoverable_changed_count());
587 EXPECT_TRUE(adapter_->IsDiscoverable());
590 TEST_F(BluetoothChromeOSTest, BecomeNotDiscoverable) {
591 GetAdapter();
592 adapter_->SetDiscoverable(true, GetCallback(), GetErrorCallback());
593 EXPECT_EQ(1, callback_count_);
594 EXPECT_EQ(0, error_callback_count_);
595 callback_count_ = 0;
597 ASSERT_TRUE(adapter_->IsDiscoverable());
599 // Install an observer; expect the AdapterDiscoverableChanged to be called
600 // with false, and IsDiscoverable() to return false.
601 TestBluetoothAdapterObserver observer(adapter_);
603 adapter_->SetDiscoverable(false, GetCallback(), GetErrorCallback());
604 EXPECT_EQ(1, callback_count_);
605 EXPECT_EQ(0, error_callback_count_);
607 EXPECT_EQ(1, observer.discoverable_changed_count());
609 EXPECT_FALSE(adapter_->IsDiscoverable());
612 TEST_F(BluetoothChromeOSTest, SetDiscoverableWhenNotPresent) {
613 GetAdapter();
614 ASSERT_TRUE(adapter_->IsPresent());
615 ASSERT_FALSE(adapter_->IsDiscoverable());
617 // Install an observer; expect the AdapterDiscoverableChanged to be called
618 // with true, and IsDiscoverable() to return true.
619 TestBluetoothAdapterObserver observer(adapter_);
621 fake_bluetooth_adapter_client_->SetVisible(false);
623 EXPECT_EQ(1, observer.present_changed_count());
624 EXPECT_FALSE(observer.last_present());
626 EXPECT_FALSE(adapter_->IsPresent());
627 EXPECT_FALSE(adapter_->IsDiscoverable());
629 adapter_->SetDiscoverable(true, GetCallback(), GetErrorCallback());
630 EXPECT_EQ(0, callback_count_);
631 EXPECT_EQ(1, error_callback_count_);
633 EXPECT_EQ(0, observer.discoverable_changed_count());
635 EXPECT_FALSE(adapter_->IsDiscoverable());
638 TEST_F(BluetoothChromeOSTest, StopDiscovery) {
639 GetAdapter();
641 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
642 adapter_->StartDiscoverySession(
643 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
644 base::Unretained(this)),
645 GetErrorCallback());
646 message_loop_.Run();
647 EXPECT_EQ(2, callback_count_);
648 EXPECT_EQ(0, error_callback_count_);
649 callback_count_ = 0;
651 ASSERT_TRUE(adapter_->IsPowered());
652 ASSERT_TRUE(adapter_->IsDiscovering());
653 ASSERT_EQ((size_t)1, discovery_sessions_.size());
654 ASSERT_TRUE(discovery_sessions_[0]->IsActive());
656 // Install an observer; aside from the callback, expect the
657 // AdapterDiscoveringChanged method to be called and no longer to be
658 // discovering,
659 TestBluetoothAdapterObserver observer(adapter_);
661 discovery_sessions_[0]->Stop(GetCallback(), GetErrorCallback());
662 message_loop_.Run();
663 EXPECT_EQ(1, callback_count_);
664 EXPECT_EQ(0, error_callback_count_);
666 EXPECT_EQ(1, observer.discovering_changed_count());
667 EXPECT_FALSE(observer.last_discovering());
669 EXPECT_FALSE(adapter_->IsDiscovering());
670 discovery_sessions_.clear();
671 callback_count_ = 0;
673 // Test that the Stop callbacks get called even if the
674 // BluetoothDiscoverySession objects gets deleted
675 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
676 adapter_->StartDiscoverySession(
677 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
678 base::Unretained(this)),
679 GetErrorCallback());
680 message_loop_.Run();
681 EXPECT_EQ(2, callback_count_);
682 EXPECT_EQ(0, error_callback_count_);
683 callback_count_ = 0;
684 ASSERT_TRUE(adapter_->IsPowered());
685 ASSERT_TRUE(adapter_->IsDiscovering());
686 ASSERT_EQ((size_t)1, discovery_sessions_.size());
687 ASSERT_TRUE(discovery_sessions_[0]->IsActive());
689 discovery_sessions_[0]->Stop(GetCallback(), GetErrorCallback());
690 discovery_sessions_.clear();
692 message_loop_.Run();
693 EXPECT_EQ(1, callback_count_);
694 EXPECT_EQ(0, error_callback_count_);
697 TEST_F(BluetoothChromeOSTest, Discovery) {
698 // Test a simulated discovery session.
699 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
700 GetAdapter();
702 TestBluetoothAdapterObserver observer(adapter_);
704 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
705 adapter_->StartDiscoverySession(
706 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
707 base::Unretained(this)),
708 GetErrorCallback());
709 message_loop_.Run();
710 EXPECT_EQ(2, callback_count_);
711 EXPECT_EQ(0, error_callback_count_);
712 callback_count_ = 0;
714 ASSERT_TRUE(adapter_->IsPowered());
715 ASSERT_TRUE(adapter_->IsDiscovering());
716 ASSERT_EQ((size_t)1, discovery_sessions_.size());
717 ASSERT_TRUE(discovery_sessions_[0]->IsActive());
719 // First two devices to appear.
720 message_loop_.Run();
722 EXPECT_EQ(2, observer.device_added_count());
723 EXPECT_EQ(FakeBluetoothDeviceClient::kLowEnergyAddress,
724 observer.last_device_address());
726 // Next we should get another two devices...
727 message_loop_.Run();
728 EXPECT_EQ(4, observer.device_added_count());
730 // Okay, let's run forward until a device is actually removed...
731 while (!observer.device_removed_count())
732 message_loop_.Run();
734 EXPECT_EQ(1, observer.device_removed_count());
735 EXPECT_EQ(FakeBluetoothDeviceClient::kVanishingDeviceAddress,
736 observer.last_device_address());
739 TEST_F(BluetoothChromeOSTest, PoweredAndDiscovering) {
740 GetAdapter();
741 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
742 adapter_->StartDiscoverySession(
743 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
744 base::Unretained(this)),
745 GetErrorCallback());
746 message_loop_.Run();
747 EXPECT_EQ(2, callback_count_);
748 EXPECT_EQ(0, error_callback_count_);
749 callback_count_ = 0;
750 ASSERT_EQ((size_t)1, discovery_sessions_.size());
751 ASSERT_TRUE(discovery_sessions_[0]->IsActive());
753 // Stop the timers that the simulation uses
754 fake_bluetooth_device_client_->EndDiscoverySimulation(
755 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath));
757 ASSERT_TRUE(adapter_->IsPowered());
758 ASSERT_TRUE(adapter_->IsDiscovering());
760 fake_bluetooth_adapter_client_->SetVisible(false);
761 ASSERT_FALSE(adapter_->IsPresent());
762 ASSERT_FALSE(discovery_sessions_[0]->IsActive());
764 // Install an observer; expect the AdapterPresentChanged,
765 // AdapterPoweredChanged and AdapterDiscoveringChanged methods to be called
766 // with true, and IsPresent(), IsPowered() and IsDiscovering() to all
767 // return true.
768 TestBluetoothAdapterObserver observer(adapter_);
770 fake_bluetooth_adapter_client_->SetVisible(true);
772 EXPECT_EQ(1, observer.present_changed_count());
773 EXPECT_TRUE(observer.last_present());
774 EXPECT_TRUE(adapter_->IsPresent());
776 EXPECT_EQ(1, observer.powered_changed_count());
777 EXPECT_TRUE(observer.last_powered());
778 EXPECT_TRUE(adapter_->IsPowered());
780 EXPECT_EQ(1, observer.discovering_changed_count());
781 EXPECT_TRUE(observer.last_discovering());
782 EXPECT_TRUE(adapter_->IsDiscovering());
784 observer.Reset();
786 // Now mark the adapter not present again. Expect the methods to be called
787 // again, to reset the properties back to false
788 fake_bluetooth_adapter_client_->SetVisible(false);
790 EXPECT_EQ(1, observer.present_changed_count());
791 EXPECT_FALSE(observer.last_present());
792 EXPECT_FALSE(adapter_->IsPresent());
794 EXPECT_EQ(1, observer.powered_changed_count());
795 EXPECT_FALSE(observer.last_powered());
796 EXPECT_FALSE(adapter_->IsPowered());
798 EXPECT_EQ(1, observer.discovering_changed_count());
799 EXPECT_FALSE(observer.last_discovering());
800 EXPECT_FALSE(adapter_->IsDiscovering());
803 // This unit test asserts that the basic reference counting logic works
804 // correctly for discovery requests done via the BluetoothAdapter.
805 TEST_F(BluetoothChromeOSTest, MultipleDiscoverySessions) {
806 GetAdapter();
807 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
808 EXPECT_EQ(1, callback_count_);
809 EXPECT_EQ(0, error_callback_count_);
810 EXPECT_TRUE(adapter_->IsPowered());
811 callback_count_ = 0;
813 TestBluetoothAdapterObserver observer(adapter_);
815 EXPECT_EQ(0, observer.discovering_changed_count());
816 EXPECT_FALSE(observer.last_discovering());
817 EXPECT_FALSE(adapter_->IsDiscovering());
819 // Request device discovery 3 times.
820 for (int i = 0; i < 3; i++) {
821 adapter_->StartDiscoverySession(
822 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
823 base::Unretained(this)),
824 GetErrorCallback());
826 // Run only once, as there should have been one D-Bus call.
827 message_loop_.Run();
829 // The observer should have received the discovering changed event exactly
830 // once, the success callback should have been called 3 times and the adapter
831 // should be discovering.
832 EXPECT_EQ(1, observer.discovering_changed_count());
833 EXPECT_EQ(3, callback_count_);
834 EXPECT_EQ(0, error_callback_count_);
835 EXPECT_TRUE(observer.last_discovering());
836 EXPECT_TRUE(adapter_->IsDiscovering());
837 ASSERT_EQ((size_t)3, discovery_sessions_.size());
839 // Request to stop discovery twice.
840 for (int i = 0; i < 2; i++) {
841 discovery_sessions_[i]->Stop(GetCallback(), GetErrorCallback());
844 // The observer should have received no additional discovering changed events,
845 // the success callback should have been called 2 times and the adapter should
846 // still be discovering.
847 EXPECT_EQ(1, observer.discovering_changed_count());
848 EXPECT_EQ(5, callback_count_);
849 EXPECT_EQ(0, error_callback_count_);
850 EXPECT_TRUE(observer.last_discovering());
851 EXPECT_TRUE(adapter_->IsDiscovering());
852 EXPECT_TRUE(adapter_->IsDiscovering());
853 EXPECT_FALSE(discovery_sessions_[0]->IsActive());
854 EXPECT_FALSE(discovery_sessions_[1]->IsActive());
855 EXPECT_TRUE(discovery_sessions_[2]->IsActive());
857 // Request device discovery 3 times.
858 for (int i = 0; i < 3; i++) {
859 adapter_->StartDiscoverySession(
860 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
861 base::Unretained(this)),
862 GetErrorCallback());
865 // The observer should have received no additional discovering changed events,
866 // the success callback should have been called 3 times and the adapter should
867 // still be discovering.
868 EXPECT_EQ(1, observer.discovering_changed_count());
869 EXPECT_EQ(8, callback_count_);
870 EXPECT_EQ(0, error_callback_count_);
871 EXPECT_TRUE(observer.last_discovering());
872 EXPECT_TRUE(adapter_->IsDiscovering());
873 ASSERT_EQ((size_t)6, discovery_sessions_.size());
875 // Request to stop discovery 4 times.
876 for (int i = 2; i < 6; i++) {
877 discovery_sessions_[i]->Stop(GetCallback(), GetErrorCallback());
879 // Run only once, as there should have been one D-Bus call.
880 message_loop_.Run();
882 // The observer should have received the discovering changed event exactly
883 // once, the success callback should have been called 4 times and the adapter
884 // should no longer be discovering.
885 EXPECT_EQ(2, observer.discovering_changed_count());
886 EXPECT_EQ(12, callback_count_);
887 EXPECT_EQ(0, error_callback_count_);
888 EXPECT_FALSE(observer.last_discovering());
889 EXPECT_FALSE(adapter_->IsDiscovering());
891 // All discovery sessions should be inactive.
892 for (int i = 0; i < 6; i++)
893 EXPECT_FALSE(discovery_sessions_[i]->IsActive());
895 // Request to stop discovery on of the inactive sessions.
896 discovery_sessions_[0]->Stop(GetCallback(), GetErrorCallback());
898 // The call should have failed.
899 EXPECT_EQ(2, observer.discovering_changed_count());
900 EXPECT_EQ(12, callback_count_);
901 EXPECT_EQ(1, error_callback_count_);
902 EXPECT_FALSE(observer.last_discovering());
903 EXPECT_FALSE(adapter_->IsDiscovering());
906 // This unit test asserts that the reference counting logic works correctly in
907 // the cases when the adapter gets reset and D-Bus calls are made outside of
908 // the BluetoothAdapter.
909 TEST_F(BluetoothChromeOSTest,
910 UnexpectedChangesDuringMultipleDiscoverySessions) {
911 GetAdapter();
912 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
913 EXPECT_EQ(1, callback_count_);
914 EXPECT_EQ(0, error_callback_count_);
915 EXPECT_TRUE(adapter_->IsPowered());
916 callback_count_ = 0;
918 TestBluetoothAdapterObserver observer(adapter_);
920 EXPECT_EQ(0, observer.discovering_changed_count());
921 EXPECT_FALSE(observer.last_discovering());
922 EXPECT_FALSE(adapter_->IsDiscovering());
924 // Request device discovery 3 times.
925 for (int i = 0; i < 3; i++) {
926 adapter_->StartDiscoverySession(
927 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
928 base::Unretained(this)),
929 GetErrorCallback());
931 // Run only once, as there should have been one D-Bus call.
932 message_loop_.Run();
934 // The observer should have received the discovering changed event exactly
935 // once, the success callback should have been called 3 times and the adapter
936 // should be discovering.
937 EXPECT_EQ(1, observer.discovering_changed_count());
938 EXPECT_EQ(3, callback_count_);
939 EXPECT_EQ(0, error_callback_count_);
940 EXPECT_TRUE(observer.last_discovering());
941 EXPECT_TRUE(adapter_->IsDiscovering());
942 ASSERT_EQ((size_t)3, discovery_sessions_.size());
944 for (int i = 0; i < 3; i++)
945 EXPECT_TRUE(discovery_sessions_[i]->IsActive());
947 // Stop the timers that the simulation uses
948 fake_bluetooth_device_client_->EndDiscoverySimulation(
949 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath));
951 ASSERT_TRUE(adapter_->IsPowered());
952 ASSERT_TRUE(adapter_->IsDiscovering());
954 // Stop device discovery behind the adapter. The adapter and the observer
955 // should be notified of the change and the reference count should be reset.
956 // Even though FakeBluetoothAdapterClient does its own reference counting and
957 // we called 3 BluetoothAdapter::StartDiscoverySession 3 times, the
958 // FakeBluetoothAdapterClient's count should be only 1 and a single call to
959 // FakeBluetoothAdapterClient::StopDiscovery should work.
960 fake_bluetooth_adapter_client_->StopDiscovery(
961 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath), GetCallback(),
962 base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
963 base::Unretained(this)));
964 message_loop_.Run();
965 EXPECT_EQ(2, observer.discovering_changed_count());
966 EXPECT_EQ(4, callback_count_);
967 EXPECT_EQ(0, error_callback_count_);
968 EXPECT_FALSE(observer.last_discovering());
969 EXPECT_FALSE(adapter_->IsDiscovering());
971 // All discovery session instances should have been updated.
972 for (int i = 0; i < 3; i++)
973 EXPECT_FALSE(discovery_sessions_[i]->IsActive());
974 discovery_sessions_.clear();
976 // It should be possible to successfully start discovery.
977 for (int i = 0; i < 2; i++) {
978 adapter_->StartDiscoverySession(
979 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
980 base::Unretained(this)),
981 GetErrorCallback());
983 // Run only once, as there should have been one D-Bus call.
984 message_loop_.Run();
985 EXPECT_EQ(3, observer.discovering_changed_count());
986 EXPECT_EQ(6, callback_count_);
987 EXPECT_EQ(0, error_callback_count_);
988 EXPECT_TRUE(observer.last_discovering());
989 EXPECT_TRUE(adapter_->IsDiscovering());
990 ASSERT_EQ((size_t)2, discovery_sessions_.size());
992 for (int i = 0; i < 2; i++)
993 EXPECT_TRUE(discovery_sessions_[i]->IsActive());
995 fake_bluetooth_device_client_->EndDiscoverySimulation(
996 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath));
998 // Make the adapter disappear and appear. This will make it come back as
999 // discovering. When this happens, the reference count should become and
1000 // remain 0 as no new request was made through the BluetoothAdapter.
1001 fake_bluetooth_adapter_client_->SetVisible(false);
1002 ASSERT_FALSE(adapter_->IsPresent());
1003 EXPECT_EQ(4, observer.discovering_changed_count());
1004 EXPECT_EQ(6, callback_count_);
1005 EXPECT_EQ(0, error_callback_count_);
1006 EXPECT_FALSE(observer.last_discovering());
1007 EXPECT_FALSE(adapter_->IsDiscovering());
1009 for (int i = 0; i < 2; i++)
1010 EXPECT_FALSE(discovery_sessions_[i]->IsActive());
1011 discovery_sessions_.clear();
1013 fake_bluetooth_adapter_client_->SetVisible(true);
1014 ASSERT_TRUE(adapter_->IsPresent());
1015 EXPECT_EQ(5, observer.discovering_changed_count());
1016 EXPECT_EQ(6, callback_count_);
1017 EXPECT_EQ(0, error_callback_count_);
1018 EXPECT_TRUE(observer.last_discovering());
1019 EXPECT_TRUE(adapter_->IsDiscovering());
1021 // Start and stop discovery. At this point, FakeBluetoothAdapterClient has
1022 // a reference count that is equal to 1. Pretend that this was done by an
1023 // application other than us. Starting and stopping discovery will succeed
1024 // but it won't cause the discovery state to change.
1025 adapter_->StartDiscoverySession(
1026 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1027 base::Unretained(this)),
1028 GetErrorCallback());
1029 message_loop_.Run(); // Run the loop, as there should have been a D-Bus call.
1030 EXPECT_EQ(5, observer.discovering_changed_count());
1031 EXPECT_EQ(7, callback_count_);
1032 EXPECT_EQ(0, error_callback_count_);
1033 EXPECT_TRUE(observer.last_discovering());
1034 EXPECT_TRUE(adapter_->IsDiscovering());
1035 ASSERT_EQ((size_t)1, discovery_sessions_.size());
1036 EXPECT_TRUE(discovery_sessions_[0]->IsActive());
1038 discovery_sessions_[0]->Stop(GetCallback(), GetErrorCallback());
1039 message_loop_.Run(); // Run the loop, as there should have been a D-Bus call.
1040 EXPECT_EQ(5, observer.discovering_changed_count());
1041 EXPECT_EQ(8, callback_count_);
1042 EXPECT_EQ(0, error_callback_count_);
1043 EXPECT_TRUE(observer.last_discovering());
1044 EXPECT_TRUE(adapter_->IsDiscovering());
1045 EXPECT_FALSE(discovery_sessions_[0]->IsActive());
1046 discovery_sessions_.clear();
1048 // Start discovery again.
1049 adapter_->StartDiscoverySession(
1050 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1051 base::Unretained(this)),
1052 GetErrorCallback());
1053 message_loop_.Run(); // Run the loop, as there should have been a D-Bus call.
1054 EXPECT_EQ(5, observer.discovering_changed_count());
1055 EXPECT_EQ(9, callback_count_);
1056 EXPECT_EQ(0, error_callback_count_);
1057 EXPECT_TRUE(observer.last_discovering());
1058 EXPECT_TRUE(adapter_->IsDiscovering());
1059 ASSERT_EQ((size_t)1, discovery_sessions_.size());
1060 EXPECT_TRUE(discovery_sessions_[0]->IsActive());
1062 // Stop discovery via D-Bus. The fake client's reference count will drop but
1063 // the discovery state won't change since our BluetoothAdapter also just
1064 // requested it via D-Bus.
1065 fake_bluetooth_adapter_client_->StopDiscovery(
1066 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath), GetCallback(),
1067 base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
1068 base::Unretained(this)));
1069 message_loop_.Run();
1070 EXPECT_EQ(5, observer.discovering_changed_count());
1071 EXPECT_EQ(10, callback_count_);
1072 EXPECT_EQ(0, error_callback_count_);
1073 EXPECT_TRUE(observer.last_discovering());
1074 EXPECT_TRUE(adapter_->IsDiscovering());
1076 // Now end the discovery session. This should change the adapter's discovery
1077 // state.
1078 discovery_sessions_[0]->Stop(GetCallback(), GetErrorCallback());
1079 message_loop_.Run();
1080 EXPECT_EQ(6, observer.discovering_changed_count());
1081 EXPECT_EQ(11, callback_count_);
1082 EXPECT_EQ(0, error_callback_count_);
1083 EXPECT_FALSE(observer.last_discovering());
1084 EXPECT_FALSE(adapter_->IsDiscovering());
1085 EXPECT_FALSE(discovery_sessions_[0]->IsActive());
1088 TEST_F(BluetoothChromeOSTest, InvalidatedDiscoverySessions) {
1089 GetAdapter();
1090 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
1091 EXPECT_EQ(1, callback_count_);
1092 EXPECT_EQ(0, error_callback_count_);
1093 EXPECT_TRUE(adapter_->IsPowered());
1094 callback_count_ = 0;
1096 TestBluetoothAdapterObserver observer(adapter_);
1098 EXPECT_EQ(0, observer.discovering_changed_count());
1099 EXPECT_FALSE(observer.last_discovering());
1100 EXPECT_FALSE(adapter_->IsDiscovering());
1102 // Request device discovery 3 times.
1103 for (int i = 0; i < 3; i++) {
1104 adapter_->StartDiscoverySession(
1105 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1106 base::Unretained(this)),
1107 GetErrorCallback());
1109 // Run only once, as there should have been one D-Bus call.
1110 message_loop_.Run();
1112 // The observer should have received the discovering changed event exactly
1113 // once, the success callback should have been called 3 times and the adapter
1114 // should be discovering.
1115 EXPECT_EQ(1, observer.discovering_changed_count());
1116 EXPECT_EQ(3, callback_count_);
1117 EXPECT_EQ(0, error_callback_count_);
1118 EXPECT_TRUE(observer.last_discovering());
1119 EXPECT_TRUE(adapter_->IsDiscovering());
1120 ASSERT_EQ((size_t)3, discovery_sessions_.size());
1122 for (int i = 0; i < 3; i++)
1123 EXPECT_TRUE(discovery_sessions_[i]->IsActive());
1125 // Stop the timers that the simulation uses
1126 fake_bluetooth_device_client_->EndDiscoverySimulation(
1127 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath));
1129 ASSERT_TRUE(adapter_->IsPowered());
1130 ASSERT_TRUE(adapter_->IsDiscovering());
1132 // Delete all but one discovery session.
1133 discovery_sessions_.pop_back();
1134 discovery_sessions_.pop_back();
1135 ASSERT_EQ((size_t)1, discovery_sessions_.size());
1136 EXPECT_TRUE(discovery_sessions_[0]->IsActive());
1137 EXPECT_TRUE(adapter_->IsDiscovering());
1139 // Stop device discovery behind the adapter. The one active discovery session
1140 // should become inactive, but more importantly, we shouldn't run into any
1141 // memory errors as the sessions that we explicitly deleted should get
1142 // cleaned up.
1143 fake_bluetooth_adapter_client_->StopDiscovery(
1144 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath), GetCallback(),
1145 base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
1146 base::Unretained(this)));
1147 message_loop_.Run();
1148 EXPECT_EQ(2, observer.discovering_changed_count());
1149 EXPECT_EQ(4, callback_count_);
1150 EXPECT_EQ(0, error_callback_count_);
1151 EXPECT_FALSE(observer.last_discovering());
1152 EXPECT_FALSE(adapter_->IsDiscovering());
1153 EXPECT_FALSE(discovery_sessions_[0]->IsActive());
1156 TEST_F(BluetoothChromeOSTest, QueuedDiscoveryRequests) {
1157 GetAdapter();
1159 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
1160 EXPECT_EQ(1, callback_count_);
1161 EXPECT_EQ(0, error_callback_count_);
1162 EXPECT_TRUE(adapter_->IsPowered());
1163 callback_count_ = 0;
1165 TestBluetoothAdapterObserver observer(adapter_);
1167 EXPECT_EQ(0, observer.discovering_changed_count());
1168 EXPECT_FALSE(observer.last_discovering());
1169 EXPECT_FALSE(adapter_->IsDiscovering());
1171 // Request to start discovery. The call should be pending.
1172 adapter_->StartDiscoverySession(
1173 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1174 base::Unretained(this)),
1175 GetErrorCallback());
1176 EXPECT_EQ(0, callback_count_);
1178 fake_bluetooth_device_client_->EndDiscoverySimulation(
1179 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath));
1181 // The underlying adapter has started discovery, but our call hasn't returned
1182 // yet.
1183 EXPECT_EQ(1, observer.discovering_changed_count());
1184 EXPECT_TRUE(observer.last_discovering());
1185 EXPECT_TRUE(adapter_->IsDiscovering());
1186 EXPECT_TRUE(discovery_sessions_.empty());
1188 // Request to start discovery twice. These should get queued and there should
1189 // be no change in state.
1190 for (int i = 0; i < 2; i++) {
1191 adapter_->StartDiscoverySession(
1192 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1193 base::Unretained(this)),
1194 GetErrorCallback());
1196 EXPECT_EQ(0, callback_count_);
1197 EXPECT_EQ(0, error_callback_count_);
1198 EXPECT_EQ(1, observer.discovering_changed_count());
1199 EXPECT_TRUE(observer.last_discovering());
1200 EXPECT_TRUE(adapter_->IsDiscovering());
1201 EXPECT_TRUE(discovery_sessions_.empty());
1203 // Process the pending call. The queued calls should execute and the discovery
1204 // session reference count should increase.
1205 message_loop_.Run();
1206 EXPECT_EQ(3, callback_count_);
1207 EXPECT_EQ(0, error_callback_count_);
1208 EXPECT_EQ(1, observer.discovering_changed_count());
1209 EXPECT_TRUE(observer.last_discovering());
1210 EXPECT_TRUE(adapter_->IsDiscovering());
1211 ASSERT_EQ((size_t)3, discovery_sessions_.size());
1213 // Verify the reference count by removing sessions 3 times. The last request
1214 // should remain pending.
1215 for (int i = 0; i < 3; i++) {
1216 discovery_sessions_[i]->Stop(GetCallback(), GetErrorCallback());
1218 EXPECT_EQ(5, callback_count_);
1219 EXPECT_EQ(0, error_callback_count_);
1220 EXPECT_EQ(2, observer.discovering_changed_count());
1221 EXPECT_FALSE(observer.last_discovering());
1222 EXPECT_FALSE(adapter_->IsDiscovering());
1223 EXPECT_FALSE(discovery_sessions_[0]->IsActive());
1224 EXPECT_FALSE(discovery_sessions_[1]->IsActive());
1225 EXPECT_TRUE(discovery_sessions_[2]->IsActive());
1227 // Request to stop the session whose call is pending should fail.
1228 discovery_sessions_[2]->Stop(GetCallback(), GetErrorCallback());
1229 EXPECT_EQ(5, callback_count_);
1230 EXPECT_EQ(1, error_callback_count_);
1231 EXPECT_EQ(2, observer.discovering_changed_count());
1232 EXPECT_FALSE(observer.last_discovering());
1233 EXPECT_FALSE(adapter_->IsDiscovering());
1234 EXPECT_TRUE(discovery_sessions_[2]->IsActive());
1236 // Request to start should get queued.
1237 adapter_->StartDiscoverySession(
1238 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1239 base::Unretained(this)),
1240 GetErrorCallback());
1241 EXPECT_EQ(5, callback_count_);
1242 EXPECT_EQ(1, error_callback_count_);
1243 EXPECT_EQ(2, observer.discovering_changed_count());
1244 EXPECT_FALSE(observer.last_discovering());
1245 EXPECT_FALSE(adapter_->IsDiscovering());
1246 ASSERT_EQ((size_t)3, discovery_sessions_.size());
1248 // Run the pending request.
1249 message_loop_.Run();
1250 EXPECT_EQ(6, callback_count_);
1251 EXPECT_EQ(1, error_callback_count_);
1252 EXPECT_EQ(3, observer.discovering_changed_count());
1253 EXPECT_TRUE(observer.last_discovering());
1254 EXPECT_TRUE(adapter_->IsDiscovering());
1255 ASSERT_EQ((size_t)3, discovery_sessions_.size());
1256 EXPECT_FALSE(discovery_sessions_[2]->IsActive());
1258 // The queued request to start discovery should have been issued but is still
1259 // pending. Run the loop and verify.
1260 message_loop_.Run();
1261 EXPECT_EQ(7, callback_count_);
1262 EXPECT_EQ(1, error_callback_count_);
1263 EXPECT_EQ(3, observer.discovering_changed_count());
1264 EXPECT_TRUE(observer.last_discovering());
1265 EXPECT_TRUE(adapter_->IsDiscovering());
1266 ASSERT_EQ((size_t)4, discovery_sessions_.size());
1267 EXPECT_TRUE(discovery_sessions_[3]->IsActive());
1270 TEST_F(BluetoothChromeOSTest, StartDiscoverySession) {
1271 GetAdapter();
1273 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
1274 EXPECT_EQ(1, callback_count_);
1275 EXPECT_EQ(0, error_callback_count_);
1276 EXPECT_TRUE(adapter_->IsPowered());
1277 callback_count_ = 0;
1279 TestBluetoothAdapterObserver observer(adapter_);
1281 EXPECT_EQ(0, observer.discovering_changed_count());
1282 EXPECT_FALSE(observer.last_discovering());
1283 EXPECT_FALSE(adapter_->IsDiscovering());
1284 EXPECT_TRUE(discovery_sessions_.empty());
1286 // Request a new discovery session.
1287 adapter_->StartDiscoverySession(
1288 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1289 base::Unretained(this)),
1290 GetErrorCallback());
1291 message_loop_.Run();
1292 EXPECT_EQ(1, observer.discovering_changed_count());
1293 EXPECT_EQ(1, callback_count_);
1294 EXPECT_EQ(0, error_callback_count_);
1295 EXPECT_TRUE(observer.last_discovering());
1296 EXPECT_TRUE(adapter_->IsDiscovering());
1297 ASSERT_EQ((size_t)1, discovery_sessions_.size());
1298 EXPECT_TRUE(discovery_sessions_[0]->IsActive());
1300 // Start another session. A new one should be returned in the callback, which
1301 // in turn will destroy the previous session. Adapter should still be
1302 // discovering and the reference count should be 1.
1303 adapter_->StartDiscoverySession(
1304 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1305 base::Unretained(this)),
1306 GetErrorCallback());
1307 message_loop_.Run();
1308 EXPECT_EQ(1, observer.discovering_changed_count());
1309 EXPECT_EQ(2, callback_count_);
1310 EXPECT_EQ(0, error_callback_count_);
1311 EXPECT_TRUE(observer.last_discovering());
1312 EXPECT_TRUE(adapter_->IsDiscovering());
1313 ASSERT_EQ((size_t)2, discovery_sessions_.size());
1314 EXPECT_TRUE(discovery_sessions_[0]->IsActive());
1316 // Request a new session.
1317 adapter_->StartDiscoverySession(
1318 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1319 base::Unretained(this)),
1320 GetErrorCallback());
1321 message_loop_.Run();
1322 EXPECT_EQ(1, observer.discovering_changed_count());
1323 EXPECT_EQ(3, callback_count_);
1324 EXPECT_EQ(0, error_callback_count_);
1325 EXPECT_TRUE(observer.last_discovering());
1326 EXPECT_TRUE(adapter_->IsDiscovering());
1327 ASSERT_EQ((size_t)3, discovery_sessions_.size());
1328 EXPECT_TRUE(discovery_sessions_[1]->IsActive());
1329 EXPECT_NE(discovery_sessions_[0], discovery_sessions_[1]);
1331 // Stop the previous discovery session. The session should end but discovery
1332 // should continue.
1333 discovery_sessions_[0]->Stop(GetCallback(), GetErrorCallback());
1334 message_loop_.Run();
1335 EXPECT_EQ(1, observer.discovering_changed_count());
1336 EXPECT_EQ(4, callback_count_);
1337 EXPECT_EQ(0, error_callback_count_);
1338 EXPECT_TRUE(observer.last_discovering());
1339 EXPECT_TRUE(adapter_->IsDiscovering());
1340 ASSERT_EQ((size_t)3, discovery_sessions_.size());
1341 EXPECT_FALSE(discovery_sessions_[0]->IsActive());
1342 EXPECT_TRUE(discovery_sessions_[1]->IsActive());
1344 // Delete the current active session. Discovery should eventually stop.
1345 discovery_sessions_.clear();
1346 while (observer.last_discovering())
1347 message_loop_.RunUntilIdle();
1349 EXPECT_EQ(2, observer.discovering_changed_count());
1350 EXPECT_EQ(4, callback_count_);
1351 EXPECT_EQ(0, error_callback_count_);
1352 EXPECT_FALSE(observer.last_discovering());
1353 EXPECT_FALSE(adapter_->IsDiscovering());
1356 TEST_F(BluetoothChromeOSTest, SetDiscoveryFilterBeforeStartDiscovery) {
1357 // Test a simulated discovery session.
1358 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
1359 GetAdapter();
1361 TestBluetoothAdapterObserver observer(adapter_);
1363 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter(
1364 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
1365 df->SetRSSI(-60);
1366 df->AddUUID(BluetoothUUID("1000"));
1367 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter(df);
1369 adapter_->SetPowered(true, base::Bind(&BluetoothChromeOSTest::Callback,
1370 base::Unretained(this)),
1371 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1372 base::Unretained(this)));
1373 adapter_->StartDiscoverySessionWithFilter(
1374 discovery_filter.Pass(),
1375 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1376 base::Unretained(this)),
1377 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1378 base::Unretained(this)));
1379 message_loop_.Run();
1380 EXPECT_EQ(2, callback_count_);
1381 EXPECT_EQ(0, error_callback_count_);
1382 callback_count_ = 0;
1384 ASSERT_TRUE(adapter_->IsPowered());
1385 ASSERT_TRUE(adapter_->IsDiscovering());
1386 ASSERT_EQ((size_t)1, discovery_sessions_.size());
1387 ASSERT_TRUE(discovery_sessions_[0]->IsActive());
1388 ASSERT_TRUE(df->Equals(*discovery_sessions_[0]->GetDiscoveryFilter()));
1390 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1391 EXPECT_NE(nullptr, filter);
1392 EXPECT_EQ("le", *filter->transport);
1393 EXPECT_EQ(-60, *filter->rssi);
1394 EXPECT_EQ(nullptr, filter->pathloss.get());
1395 std::vector<std::string> uuids = *filter->uuids;
1396 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1000"));
1398 discovery_sessions_[0]->Stop(
1399 base::Bind(&BluetoothChromeOSTest::Callback, base::Unretained(this)),
1400 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1401 base::Unretained(this)));
1403 message_loop_.Run();
1405 EXPECT_EQ(1, callback_count_);
1406 EXPECT_EQ(0, error_callback_count_);
1408 ASSERT_TRUE(adapter_->IsPowered());
1409 ASSERT_FALSE(adapter_->IsDiscovering());
1410 ASSERT_EQ((size_t)1, discovery_sessions_.size());
1411 ASSERT_FALSE(discovery_sessions_[0]->IsActive());
1412 ASSERT_EQ(discovery_sessions_[0]->GetDiscoveryFilter(),
1413 (BluetoothDiscoveryFilter*)nullptr);
1415 filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1416 EXPECT_EQ(nullptr, filter);
1419 TEST_F(BluetoothChromeOSTest, SetDiscoveryFilterBeforeStartDiscoveryFail) {
1420 // Test a simulated discovery session.
1421 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
1422 GetAdapter();
1424 TestBluetoothAdapterObserver observer(adapter_);
1426 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter(
1427 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
1428 df->SetRSSI(-60);
1429 df->AddUUID(BluetoothUUID("1000"));
1430 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter(df);
1432 adapter_->SetPowered(true, base::Bind(&BluetoothChromeOSTest::Callback,
1433 base::Unretained(this)),
1434 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1435 base::Unretained(this)));
1436 EXPECT_EQ(1, callback_count_);
1437 callback_count_ = 0;
1439 fake_bluetooth_adapter_client_->MakeSetDiscoveryFilterFail();
1441 adapter_->StartDiscoverySessionWithFilter(
1442 discovery_filter.Pass(),
1443 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1444 base::Unretained(this)),
1445 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1446 base::Unretained(this)));
1448 message_loop_.Run();
1450 EXPECT_EQ(1, error_callback_count_);
1451 error_callback_count_ = 0;
1453 ASSERT_TRUE(adapter_->IsPowered());
1454 ASSERT_FALSE(adapter_->IsDiscovering());
1455 ASSERT_EQ((size_t)0, discovery_sessions_.size());
1457 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1458 EXPECT_EQ(nullptr, filter);
1461 // This test queues two requests to StartDiscovery with pre set filter. This
1462 // should result in SetDiscoveryFilter, then StartDiscovery, and SetDiscovery
1463 // DBus calls
1464 TEST_F(BluetoothChromeOSTest, QueuedSetDiscoveryFilterBeforeStartDiscovery) {
1465 // Test a simulated discovery session.
1466 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
1467 GetAdapter();
1469 TestBluetoothAdapterObserver observer(adapter_);
1471 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter(
1472 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
1473 df->SetRSSI(-60);
1474 df->AddUUID(BluetoothUUID("1000"));
1475 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter(df);
1477 BluetoothDiscoveryFilter* df2 = new BluetoothDiscoveryFilter(
1478 BluetoothDiscoveryFilter::Transport::TRANSPORT_CLASSIC);
1479 df2->SetRSSI(-65);
1480 df2->AddUUID(BluetoothUUID("1002"));
1481 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter2(df2);
1483 adapter_->SetPowered(true, base::Bind(&BluetoothChromeOSTest::Callback,
1484 base::Unretained(this)),
1485 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1486 base::Unretained(this)));
1488 EXPECT_EQ(1, callback_count_);
1489 EXPECT_EQ(0, error_callback_count_);
1490 callback_count_ = 0;
1492 // Queue two requests to start discovery session with filter.
1493 adapter_->StartDiscoverySessionWithFilter(
1494 discovery_filter.Pass(),
1495 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1496 base::Unretained(this)),
1497 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1498 base::Unretained(this)));
1500 adapter_->StartDiscoverySessionWithFilter(
1501 discovery_filter2.Pass(),
1502 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1503 base::Unretained(this)),
1504 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1505 base::Unretained(this)));
1507 // Run requests, on DBus level there should be call SetDiscoveryFilter, then
1508 // StartDiscovery, then SetDiscoveryFilter again.
1509 message_loop_.Run();
1510 message_loop_.Run();
1512 EXPECT_EQ(2, callback_count_);
1513 EXPECT_EQ(0, error_callback_count_);
1514 callback_count_ = 0;
1516 ASSERT_TRUE(adapter_->IsPowered());
1517 ASSERT_TRUE(adapter_->IsDiscovering());
1518 ASSERT_EQ((size_t)2, discovery_sessions_.size());
1519 ASSERT_TRUE(discovery_sessions_[0]->IsActive());
1520 ASSERT_TRUE(df->Equals(*discovery_sessions_[0]->GetDiscoveryFilter()));
1521 ASSERT_TRUE(discovery_sessions_[1]->IsActive());
1522 ASSERT_TRUE(df2->Equals(*discovery_sessions_[1]->GetDiscoveryFilter()));
1524 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1525 EXPECT_NE(nullptr, filter);
1526 EXPECT_EQ("auto", *filter->transport);
1527 EXPECT_EQ(-65, *filter->rssi);
1528 EXPECT_EQ(nullptr, filter->pathloss.get());
1529 auto uuids = *filter->uuids;
1530 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1000"));
1531 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1002"));
1533 discovery_sessions_[0]->Stop(
1534 base::Bind(&BluetoothChromeOSTest::Callback, base::Unretained(this)),
1535 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1536 base::Unretained(this)));
1538 discovery_sessions_[1]->Stop(
1539 base::Bind(&BluetoothChromeOSTest::Callback, base::Unretained(this)),
1540 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1541 base::Unretained(this)));
1543 message_loop_.Run();
1545 EXPECT_EQ(2, callback_count_);
1546 EXPECT_EQ(0, error_callback_count_);
1548 ASSERT_TRUE(adapter_->IsPowered());
1549 ASSERT_FALSE(adapter_->IsDiscovering());
1550 ASSERT_FALSE(discovery_sessions_[0]->IsActive());
1551 ASSERT_EQ(discovery_sessions_[0]->GetDiscoveryFilter(),
1552 (BluetoothDiscoveryFilter*)nullptr);
1553 ASSERT_FALSE(discovery_sessions_[1]->IsActive());
1554 ASSERT_EQ(discovery_sessions_[1]->GetDiscoveryFilter(),
1555 (BluetoothDiscoveryFilter*)nullptr);
1557 filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1558 EXPECT_EQ(nullptr, filter);
1561 // Call StartFilteredDiscovery twice (2nd time while 1st call is still pending).
1562 // Make the first SetDiscoveryFilter fail and the second one succeed. It should
1563 // end up with one active discovery session.
1564 TEST_F(BluetoothChromeOSTest,
1565 QueuedSetDiscoveryFilterBeforeStartDiscoveryFail) {
1566 // Test a simulated discovery session.
1567 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
1568 GetAdapter();
1570 TestBluetoothAdapterObserver observer(adapter_);
1572 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter(
1573 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
1574 df->SetRSSI(-60);
1575 df->AddUUID(BluetoothUUID("1000"));
1576 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter(df);
1578 BluetoothDiscoveryFilter* df2 = new BluetoothDiscoveryFilter(
1579 BluetoothDiscoveryFilter::Transport::TRANSPORT_CLASSIC);
1580 df2->SetRSSI(-65);
1581 df2->AddUUID(BluetoothUUID("1002"));
1582 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter2(df2);
1584 adapter_->SetPowered(true, base::Bind(&BluetoothChromeOSTest::Callback,
1585 base::Unretained(this)),
1586 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1587 base::Unretained(this)));
1589 EXPECT_EQ(1, callback_count_);
1590 EXPECT_EQ(0, error_callback_count_);
1591 callback_count_ = 0;
1593 fake_bluetooth_adapter_client_->MakeSetDiscoveryFilterFail();
1595 // Queue two requests to start discovery session with filter.
1596 adapter_->StartDiscoverySessionWithFilter(
1597 discovery_filter.Pass(),
1598 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1599 base::Unretained(this)),
1600 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1601 base::Unretained(this)));
1603 adapter_->StartDiscoverySessionWithFilter(
1604 discovery_filter2.Pass(),
1605 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1606 base::Unretained(this)),
1607 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1608 base::Unretained(this)));
1610 message_loop_.Run();
1612 // First request to SetDiscoveryFilter should fail, resulting in no session
1613 // being created.
1614 EXPECT_EQ(0, callback_count_);
1615 EXPECT_EQ(1, error_callback_count_);
1616 error_callback_count_ = 0;
1618 ASSERT_TRUE(adapter_->IsPowered());
1619 ASSERT_FALSE(adapter_->IsDiscovering());
1620 ASSERT_EQ((size_t)0, discovery_sessions_.size());
1622 message_loop_.Run();
1624 // Second request should succeed
1625 EXPECT_EQ(1, callback_count_);
1626 EXPECT_EQ(0, error_callback_count_);
1627 callback_count_ = 0;
1629 ASSERT_TRUE(adapter_->IsDiscovering());
1630 ASSERT_EQ((size_t)1, discovery_sessions_.size());
1631 ASSERT_TRUE(discovery_sessions_[0]->IsActive());
1632 ASSERT_TRUE(df2->Equals(*discovery_sessions_[0]->GetDiscoveryFilter()));
1634 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1635 EXPECT_NE(nullptr, filter);
1636 EXPECT_EQ("bredr", *filter->transport);
1637 EXPECT_EQ(-65, *filter->rssi);
1638 EXPECT_EQ(nullptr, filter->pathloss.get());
1639 auto uuids = *filter->uuids;
1640 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1002"));
1642 discovery_sessions_[0]->Stop(
1643 base::Bind(&BluetoothChromeOSTest::Callback, base::Unretained(this)),
1644 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1645 base::Unretained(this)));
1647 message_loop_.Run();
1649 EXPECT_EQ(1, callback_count_);
1650 EXPECT_EQ(0, error_callback_count_);
1652 ASSERT_TRUE(adapter_->IsPowered());
1653 ASSERT_FALSE(adapter_->IsDiscovering());
1654 ASSERT_FALSE(discovery_sessions_[0]->IsActive());
1655 ASSERT_EQ(discovery_sessions_[0]->GetDiscoveryFilter(),
1656 (BluetoothDiscoveryFilter*)nullptr);
1658 filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1659 EXPECT_EQ(nullptr, filter);
1662 TEST_F(BluetoothChromeOSTest, SetDiscoveryFilterAfterStartDiscovery) {
1663 // Test a simulated discovery session.
1664 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
1665 GetAdapter();
1667 TestBluetoothAdapterObserver observer(adapter_);
1669 adapter_->SetPowered(true, base::Bind(&BluetoothChromeOSTest::Callback,
1670 base::Unretained(this)),
1671 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1672 base::Unretained(this)));
1673 adapter_->StartDiscoverySession(
1674 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1675 base::Unretained(this)),
1676 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1677 base::Unretained(this)));
1678 message_loop_.Run();
1679 EXPECT_EQ(2, callback_count_);
1680 EXPECT_EQ(0, error_callback_count_);
1681 callback_count_ = 0;
1683 ASSERT_TRUE(adapter_->IsPowered());
1684 ASSERT_TRUE(adapter_->IsDiscovering());
1685 ASSERT_EQ((size_t)1, discovery_sessions_.size());
1686 ASSERT_TRUE(discovery_sessions_[0]->IsActive());
1687 EXPECT_EQ(1, observer.discovering_changed_count());
1688 observer.Reset();
1690 auto null_instance = scoped_ptr<BluetoothDiscoveryFilter>();
1691 null_instance.reset();
1692 ASSERT_EQ(discovery_sessions_[0]->GetDiscoveryFilter(), null_instance.get());
1694 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1695 EXPECT_EQ(nullptr, filter);
1697 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter(
1698 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
1699 df->SetRSSI(-60);
1700 df->AddUUID(BluetoothUUID("1000"));
1701 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter(df);
1703 discovery_sessions_[0]->SetDiscoveryFilter(
1704 discovery_filter.Pass(),
1705 base::Bind(&BluetoothChromeOSTest::Callback, base::Unretained(this)),
1706 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1707 base::Unretained(this)));
1709 message_loop_.Run();
1710 EXPECT_EQ(1, callback_count_);
1711 EXPECT_EQ(0, error_callback_count_);
1712 callback_count_ = 0;
1714 ASSERT_TRUE(df->Equals(*discovery_sessions_[0]->GetDiscoveryFilter()));
1716 filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1717 EXPECT_NE(nullptr, filter);
1718 EXPECT_EQ("le", *filter->transport);
1719 EXPECT_EQ(-60, *filter->rssi);
1720 EXPECT_EQ(nullptr, filter->pathloss.get());
1721 std::vector<std::string> uuids = *filter->uuids;
1722 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1000"));
1724 discovery_sessions_[0]->Stop(
1725 base::Bind(&BluetoothChromeOSTest::Callback, base::Unretained(this)),
1726 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1727 base::Unretained(this)));
1729 message_loop_.Run();
1731 EXPECT_EQ(1, callback_count_);
1732 EXPECT_EQ(0, error_callback_count_);
1734 ASSERT_TRUE(adapter_->IsPowered());
1735 ASSERT_FALSE(adapter_->IsDiscovering());
1736 ASSERT_EQ((size_t)1, discovery_sessions_.size());
1737 ASSERT_FALSE(discovery_sessions_[0]->IsActive());
1738 ASSERT_EQ(discovery_sessions_[0]->GetDiscoveryFilter(),
1739 (BluetoothDiscoveryFilter*)nullptr);
1741 filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1742 EXPECT_EQ(nullptr, filter);
1745 // This unit test asserts that the basic reference counting, and filter merging
1746 // works correctly for discovery requests done via the BluetoothAdapter.
1747 TEST_F(BluetoothChromeOSTest, SetDiscoveryFilterBeforeStartDiscoveryMultiple) {
1748 GetAdapter();
1749 adapter_->SetPowered(true, base::Bind(&BluetoothChromeOSTest::Callback,
1750 base::Unretained(this)),
1751 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1752 base::Unretained(this)));
1753 EXPECT_EQ(1, callback_count_);
1754 EXPECT_EQ(0, error_callback_count_);
1755 EXPECT_TRUE(adapter_->IsPowered());
1756 callback_count_ = 0;
1758 TestBluetoothAdapterObserver observer(adapter_);
1760 // Request device discovery with pre-set filter 3 times.
1761 for (int i = 0; i < 3; i++) {
1762 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter;
1763 if (i == 0) {
1764 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter(
1765 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
1766 df->SetRSSI(-85);
1767 df->AddUUID(BluetoothUUID("1000"));
1768 discovery_filter.reset(df);
1769 } else if (i == 1) {
1770 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter(
1771 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
1772 df->SetRSSI(-60);
1773 df->AddUUID(BluetoothUUID("1020"));
1774 df->AddUUID(BluetoothUUID("1001"));
1775 discovery_filter.reset(df);
1776 } else if (i == 2) {
1777 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter(
1778 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
1779 df->SetRSSI(-65);
1780 df->AddUUID(BluetoothUUID("1020"));
1781 df->AddUUID(BluetoothUUID("1003"));
1782 discovery_filter.reset(df);
1785 adapter_->StartDiscoverySessionWithFilter(
1786 discovery_filter.Pass(),
1787 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1788 base::Unretained(this)),
1789 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1790 base::Unretained(this)));
1792 message_loop_.Run();
1794 if (i == 0) {
1795 EXPECT_EQ(1, observer.discovering_changed_count());
1796 observer.Reset();
1798 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1799 EXPECT_EQ("le", *filter->transport);
1800 EXPECT_EQ(-85, *filter->rssi);
1801 EXPECT_EQ(nullptr, filter->pathloss.get());
1802 std::vector<std::string> uuids = *filter->uuids;
1803 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1000"));
1804 } else if (i == 1) {
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 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1001"));
1812 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1020"));
1813 } else if (i == 2) {
1814 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1815 EXPECT_EQ("le", *filter->transport);
1816 EXPECT_EQ(-85, *filter->rssi);
1817 EXPECT_EQ(nullptr, filter->pathloss.get());
1818 std::vector<std::string> uuids = *filter->uuids;
1819 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1000"));
1820 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1001"));
1821 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1003"));
1822 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1020"));
1826 // the success callback should have been called 3 times and the adapter should
1827 // be discovering.
1828 EXPECT_EQ(3, callback_count_);
1829 EXPECT_EQ(0, error_callback_count_);
1830 EXPECT_TRUE(adapter_->IsDiscovering());
1831 ASSERT_EQ((size_t)3, discovery_sessions_.size());
1833 callback_count_ = 0;
1834 // Request to stop discovery twice.
1835 for (int i = 0; i < 2; i++) {
1836 discovery_sessions_[i]->Stop(
1837 base::Bind(&BluetoothChromeOSTest::Callback, base::Unretained(this)),
1838 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1839 base::Unretained(this)));
1840 message_loop_.Run();
1842 if (i == 0) {
1843 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1844 EXPECT_EQ("le", *filter->transport);
1845 EXPECT_EQ(-65, *filter->rssi);
1846 EXPECT_EQ(nullptr, filter->pathloss.get());
1847 std::vector<std::string> uuids = *filter->uuids;
1848 EXPECT_EQ(3UL, uuids.size());
1849 EXPECT_EQ(uuids.end(), std::find(uuids.begin(), uuids.end(), "1000"));
1850 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1001"));
1851 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1003"));
1852 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1020"));
1853 } else if (i == 1) {
1854 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1855 EXPECT_EQ("le", *filter->transport);
1856 EXPECT_EQ(-65, *filter->rssi);
1857 EXPECT_EQ(nullptr, filter->pathloss.get());
1858 std::vector<std::string> uuids = *filter->uuids;
1859 EXPECT_EQ(2UL, uuids.size());
1860 EXPECT_EQ(uuids.end(), std::find(uuids.begin(), uuids.end(), "1000"));
1861 EXPECT_EQ(uuids.end(), std::find(uuids.begin(), uuids.end(), "1001"));
1862 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1003"));
1863 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1020"));
1864 } else if (i == 2) {
1865 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1866 EXPECT_EQ("le", *filter->transport);
1867 EXPECT_EQ(-65, *filter->rssi);
1868 EXPECT_EQ(nullptr, filter->pathloss.get());
1869 std::vector<std::string> uuids = *filter->uuids;
1870 EXPECT_EQ(0UL, uuids.size());
1874 // The success callback should have been called 2 times and the adapter should
1875 // still be discovering.
1876 EXPECT_EQ(2, callback_count_);
1877 EXPECT_EQ(0, error_callback_count_);
1878 EXPECT_TRUE(adapter_->IsDiscovering());
1879 EXPECT_FALSE(discovery_sessions_[0]->IsActive());
1880 EXPECT_FALSE(discovery_sessions_[1]->IsActive());
1881 EXPECT_TRUE(discovery_sessions_[2]->IsActive());
1883 callback_count_ = 0;
1885 // Request device discovery 3 times.
1886 for (int i = 0; i < 3; i++) {
1887 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter;
1889 if (i == 0) {
1890 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter(
1891 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
1892 df->SetRSSI(-85);
1893 df->AddUUID(BluetoothUUID("1000"));
1894 discovery_filter.reset(df);
1895 } else if (i == 1) {
1896 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter(
1897 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
1898 df->SetRSSI(-60);
1899 df->AddUUID(BluetoothUUID("1020"));
1900 df->AddUUID(BluetoothUUID("1001"));
1901 discovery_filter.reset(df);
1902 } else if (i == 2) {
1903 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter(
1904 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
1905 df->SetRSSI(-65);
1906 df->AddUUID(BluetoothUUID("1020"));
1907 df->AddUUID(BluetoothUUID("1003"));
1908 discovery_filter.reset(df);
1911 adapter_->StartDiscoverySessionWithFilter(
1912 discovery_filter.Pass(),
1913 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1914 base::Unretained(this)),
1915 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1916 base::Unretained(this)));
1918 // each result in 1 requests.
1919 message_loop_.Run();
1921 if (i == 0) {
1922 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1923 EXPECT_EQ("le", *filter->transport);
1924 EXPECT_EQ(-85, *filter->rssi);
1925 EXPECT_EQ(nullptr, filter->pathloss.get());
1926 std::vector<std::string> uuids = *filter->uuids;
1927 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1000"));
1928 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1003"));
1929 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1020"));
1930 } else if (i == 1 || i == 2) {
1931 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1932 EXPECT_EQ("le", *filter->transport);
1933 EXPECT_EQ(-85, *filter->rssi);
1934 EXPECT_EQ(nullptr, filter->pathloss.get());
1935 std::vector<std::string> uuids = *filter->uuids;
1936 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1000"));
1937 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1001"));
1938 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1003"));
1939 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1020"));
1943 // The success callback should have been called 3 times and the adapter should
1944 // still be discovering.
1945 EXPECT_EQ(3, callback_count_);
1946 EXPECT_EQ(0, error_callback_count_);
1947 EXPECT_TRUE(adapter_->IsDiscovering());
1948 ASSERT_EQ((size_t)6, discovery_sessions_.size());
1950 callback_count_ = 0;
1951 // Request to stop discovery 4 times.
1952 for (int i = 2; i < 6; i++) {
1953 discovery_sessions_[i]->Stop(
1954 base::Bind(&BluetoothChromeOSTest::Callback, base::Unretained(this)),
1955 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1956 base::Unretained(this)));
1958 // filter no 2 is same as filter no 5, so removing it shouldn't cause any
1959 // filter update
1960 if (i != 2 && i != 5)
1961 message_loop_.Run();
1963 // Run only once, as there should have been one D-Bus call.
1964 message_loop_.Run();
1966 // The success callback should have been called 4 times and the adapter should
1967 // no longer be discovering.
1968 EXPECT_EQ(4, callback_count_);
1969 EXPECT_EQ(0, error_callback_count_);
1970 EXPECT_FALSE(adapter_->IsDiscovering());
1971 EXPECT_EQ(1, observer.discovering_changed_count());
1973 // All discovery sessions should be inactive.
1974 for (int i = 0; i < 6; i++)
1975 EXPECT_FALSE(discovery_sessions_[i]->IsActive());
1977 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1978 EXPECT_EQ(nullptr, filter);
1981 // This unit test asserts that filter merging logic works correctly for filtered
1982 // discovery requests done via the BluetoothAdapter.
1983 TEST_F(BluetoothChromeOSTest, SetDiscoveryFilterMergingTest) {
1984 GetAdapter();
1985 adapter_->SetPowered(true, base::Bind(&BluetoothChromeOSTest::Callback,
1986 base::Unretained(this)),
1987 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1988 base::Unretained(this)));
1990 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter(
1991 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
1992 df->SetRSSI(-15);
1993 df->AddUUID(BluetoothUUID("1000"));
1994 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter(df);
1996 adapter_->StartDiscoverySessionWithFilter(
1997 discovery_filter.Pass(),
1998 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1999 base::Unretained(this)),
2000 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
2001 base::Unretained(this)));
2003 message_loop_.Run();
2005 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
2006 EXPECT_EQ("le", *filter->transport);
2007 EXPECT_EQ(-15, *filter->rssi);
2008 EXPECT_EQ(nullptr, filter->pathloss.get());
2009 std::vector<std::string> uuids = *filter->uuids;
2010 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1000"));
2012 df = new BluetoothDiscoveryFilter(
2013 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
2014 df->SetRSSI(-60);
2015 df->AddUUID(BluetoothUUID("1020"));
2016 df->AddUUID(BluetoothUUID("1001"));
2017 discovery_filter = scoped_ptr<BluetoothDiscoveryFilter>(df);
2019 adapter_->StartDiscoverySessionWithFilter(
2020 discovery_filter.Pass(),
2021 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
2022 base::Unretained(this)),
2023 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
2024 base::Unretained(this)));
2026 message_loop_.Run();
2028 filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
2029 EXPECT_EQ("le", *filter->transport);
2030 EXPECT_EQ(-60, *filter->rssi);
2031 EXPECT_EQ(nullptr, filter->pathloss.get());
2032 uuids = *filter->uuids;
2033 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1000"));
2034 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1001"));
2035 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1020"));
2037 BluetoothDiscoveryFilter* df3 = new BluetoothDiscoveryFilter(
2038 BluetoothDiscoveryFilter::Transport::TRANSPORT_CLASSIC);
2039 df3->SetRSSI(-65);
2040 df3->AddUUID(BluetoothUUID("1020"));
2041 df3->AddUUID(BluetoothUUID("1003"));
2042 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter3(df3);
2044 adapter_->StartDiscoverySessionWithFilter(
2045 discovery_filter3.Pass(),
2046 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
2047 base::Unretained(this)),
2048 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
2049 base::Unretained(this)));
2051 message_loop_.Run();
2053 filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
2054 EXPECT_EQ("auto", *filter->transport);
2055 EXPECT_EQ(-65, *filter->rssi);
2056 EXPECT_EQ(nullptr, filter->pathloss.get());
2057 uuids = *filter->uuids;
2058 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1000"));
2059 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1001"));
2060 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1003"));
2061 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1020"));
2063 // start additionally classic scan
2064 adapter_->StartDiscoverySession(
2065 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
2066 base::Unretained(this)),
2067 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
2068 base::Unretained(this)));
2070 message_loop_.Run();
2072 filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
2073 EXPECT_EQ("auto", *filter->transport);
2074 EXPECT_EQ(nullptr, filter->rssi.get());
2075 EXPECT_EQ(nullptr, filter->pathloss.get());
2076 EXPECT_EQ(nullptr, filter->uuids.get());
2078 // Request to stop discovery 4 times.
2079 for (int i = 3; i >= 0; i--) {
2080 discovery_sessions_[i]->Stop(
2081 base::Bind(&BluetoothChromeOSTest::Callback, base::Unretained(this)),
2082 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
2083 base::Unretained(this)));
2085 // Every session stopping would trigger filter update
2086 message_loop_.Run();
2090 TEST_F(BluetoothChromeOSTest, DeviceProperties) {
2091 GetAdapter();
2093 BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
2094 ASSERT_EQ(2U, devices.size());
2095 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
2096 devices[0]->GetAddress());
2098 // Verify the other device properties.
2099 EXPECT_EQ(base::UTF8ToUTF16(FakeBluetoothDeviceClient::kPairedDeviceName),
2100 devices[0]->GetName());
2101 EXPECT_EQ(BluetoothDevice::DEVICE_COMPUTER, devices[0]->GetDeviceType());
2102 EXPECT_TRUE(devices[0]->IsPaired());
2103 EXPECT_FALSE(devices[0]->IsConnected());
2104 EXPECT_FALSE(devices[0]->IsConnecting());
2106 // Non HID devices are always connectable.
2107 EXPECT_TRUE(devices[0]->IsConnectable());
2109 BluetoothDevice::UUIDList uuids = devices[0]->GetUUIDs();
2110 ASSERT_EQ(2U, uuids.size());
2111 EXPECT_EQ(uuids[0], BluetoothUUID("1800"));
2112 EXPECT_EQ(uuids[1], BluetoothUUID("1801"));
2114 EXPECT_EQ(BluetoothDevice::VENDOR_ID_USB, devices[0]->GetVendorIDSource());
2115 EXPECT_EQ(0x05ac, devices[0]->GetVendorID());
2116 EXPECT_EQ(0x030d, devices[0]->GetProductID());
2117 EXPECT_EQ(0x0306, devices[0]->GetDeviceID());
2120 TEST_F(BluetoothChromeOSTest, DeviceClassChanged) {
2121 // Simulate a change of class of a device, as sometimes occurs
2122 // during discovery.
2123 GetAdapter();
2125 BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
2126 ASSERT_EQ(2U, devices.size());
2127 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
2128 devices[0]->GetAddress());
2129 ASSERT_EQ(BluetoothDevice::DEVICE_COMPUTER, devices[0]->GetDeviceType());
2131 // Install an observer; expect the DeviceChanged method to be called when
2132 // we change the class of the device.
2133 TestBluetoothAdapterObserver observer(adapter_);
2135 FakeBluetoothDeviceClient::Properties* properties =
2136 fake_bluetooth_device_client_->GetProperties(
2137 dbus::ObjectPath(FakeBluetoothDeviceClient::kPairedDevicePath));
2139 properties->bluetooth_class.ReplaceValue(0x002580);
2141 EXPECT_EQ(1, observer.device_changed_count());
2142 EXPECT_EQ(devices[0], observer.last_device());
2144 EXPECT_EQ(BluetoothDevice::DEVICE_MOUSE, devices[0]->GetDeviceType());
2147 TEST_F(BluetoothChromeOSTest, DeviceNameChanged) {
2148 // Simulate a change of name of a device.
2149 GetAdapter();
2151 BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
2152 ASSERT_EQ(2U, devices.size());
2153 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
2154 devices[0]->GetAddress());
2155 ASSERT_EQ(base::UTF8ToUTF16(FakeBluetoothDeviceClient::kPairedDeviceName),
2156 devices[0]->GetName());
2158 // Install an observer; expect the DeviceChanged method to be called when
2159 // we change the alias of the device.
2160 TestBluetoothAdapterObserver observer(adapter_);
2162 FakeBluetoothDeviceClient::Properties* properties =
2163 fake_bluetooth_device_client_->GetProperties(
2164 dbus::ObjectPath(FakeBluetoothDeviceClient::kPairedDevicePath));
2166 static const std::string new_name("New Device Name");
2167 properties->alias.ReplaceValue(new_name);
2169 EXPECT_EQ(1, observer.device_changed_count());
2170 EXPECT_EQ(devices[0], observer.last_device());
2172 EXPECT_EQ(base::UTF8ToUTF16(new_name), devices[0]->GetName());
2175 TEST_F(BluetoothChromeOSTest, DeviceUuidsChanged) {
2176 // Simulate a change of advertised services of a device.
2177 GetAdapter();
2179 BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
2180 ASSERT_EQ(2U, devices.size());
2181 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
2182 devices[0]->GetAddress());
2184 BluetoothDevice::UUIDList uuids = devices[0]->GetUUIDs();
2185 ASSERT_EQ(2U, uuids.size());
2186 ASSERT_EQ(uuids[0], BluetoothUUID("1800"));
2187 ASSERT_EQ(uuids[1], BluetoothUUID("1801"));
2189 // Install an observer; expect the DeviceChanged method to be called when
2190 // we change the class of the device.
2191 TestBluetoothAdapterObserver observer(adapter_);
2193 FakeBluetoothDeviceClient::Properties* properties =
2194 fake_bluetooth_device_client_->GetProperties(
2195 dbus::ObjectPath(FakeBluetoothDeviceClient::kPairedDevicePath));
2197 std::vector<std::string> new_uuids;
2198 new_uuids.push_back(uuids[0].canonical_value());
2199 new_uuids.push_back(uuids[1].canonical_value());
2200 new_uuids.push_back("0000110c-0000-1000-8000-00805f9b34fb");
2201 new_uuids.push_back("0000110e-0000-1000-8000-00805f9b34fb");
2202 new_uuids.push_back("0000110a-0000-1000-8000-00805f9b34fb");
2204 properties->uuids.ReplaceValue(new_uuids);
2206 EXPECT_EQ(1, observer.device_changed_count());
2207 EXPECT_EQ(devices[0], observer.last_device());
2209 // Fetching the value should give the new one.
2210 uuids = devices[0]->GetUUIDs();
2211 ASSERT_EQ(5U, uuids.size());
2212 EXPECT_EQ(uuids[0], BluetoothUUID("1800"));
2213 EXPECT_EQ(uuids[1], BluetoothUUID("1801"));
2214 EXPECT_EQ(uuids[2], BluetoothUUID("110c"));
2215 EXPECT_EQ(uuids[3], BluetoothUUID("110e"));
2216 EXPECT_EQ(uuids[4], BluetoothUUID("110a"));
2219 TEST_F(BluetoothChromeOSTest, DeviceInquiryRSSIInvalidated) {
2220 // Simulate invalidation of inquiry RSSI of a device, as it occurs
2221 // when discovery is finished.
2222 GetAdapter();
2224 BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
2225 ASSERT_EQ(2U, devices.size());
2226 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
2227 devices[0]->GetAddress());
2229 FakeBluetoothDeviceClient::Properties* properties =
2230 fake_bluetooth_device_client_->GetProperties(
2231 dbus::ObjectPath(FakeBluetoothDeviceClient::kPairedDevicePath));
2233 // During discovery, rssi is a valid value (-75)
2234 properties->rssi.ReplaceValue(-75);
2235 properties->rssi.set_valid(true);
2237 ASSERT_EQ(-75, devices[0]->GetInquiryRSSI());
2239 // Install an observer; expect the DeviceChanged method to be called when
2240 // we invalidate the RSSI of the device.
2241 TestBluetoothAdapterObserver observer(adapter_);
2243 // When discovery is over, the value should be invalidated.
2244 properties->rssi.set_valid(false);
2245 properties->NotifyPropertyChanged(properties->rssi.name());
2247 EXPECT_EQ(1, observer.device_changed_count());
2248 EXPECT_EQ(devices[0], observer.last_device());
2250 int unknown_power = BluetoothDevice::kUnknownPower;
2251 EXPECT_EQ(unknown_power, devices[0]->GetInquiryRSSI());
2254 TEST_F(BluetoothChromeOSTest, DeviceInquiryTxPowerInvalidated) {
2255 // Simulate invalidation of inquiry TxPower of a device, as it occurs
2256 // when discovery is finished.
2257 GetAdapter();
2259 BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
2260 ASSERT_EQ(2U, devices.size());
2261 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
2262 devices[0]->GetAddress());
2264 FakeBluetoothDeviceClient::Properties* properties =
2265 fake_bluetooth_device_client_->GetProperties(
2266 dbus::ObjectPath(FakeBluetoothDeviceClient::kPairedDevicePath));
2268 // During discovery, tx_power is a valid value (0)
2269 properties->tx_power.ReplaceValue(0);
2270 properties->tx_power.set_valid(true);
2272 ASSERT_EQ(0, devices[0]->GetInquiryTxPower());
2274 // Install an observer; expect the DeviceChanged method to be called when
2275 // we invalidate the tx_power of the device.
2276 TestBluetoothAdapterObserver observer(adapter_);
2278 // When discovery is over, the value should be invalidated.
2279 properties->tx_power.set_valid(false);
2280 properties->NotifyPropertyChanged(properties->tx_power.name());
2282 EXPECT_EQ(1, observer.device_changed_count());
2283 EXPECT_EQ(devices[0], observer.last_device());
2285 int unknown_power = BluetoothDevice::kUnknownPower;
2286 EXPECT_EQ(unknown_power, devices[0]->GetInquiryTxPower());
2289 TEST_F(BluetoothChromeOSTest, ForgetDevice) {
2290 GetAdapter();
2292 BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
2293 ASSERT_EQ(2U, devices.size());
2294 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
2295 devices[0]->GetAddress());
2297 std::string address = devices[0]->GetAddress();
2299 // Install an observer; expect the DeviceRemoved method to be called
2300 // with the device we remove.
2301 TestBluetoothAdapterObserver observer(adapter_);
2303 devices[0]->Forget(GetErrorCallback());
2304 EXPECT_EQ(0, error_callback_count_);
2306 EXPECT_EQ(1, observer.device_removed_count());
2307 EXPECT_EQ(address, observer.last_device_address());
2309 // GetDevices shouldn't return the device either.
2310 devices = adapter_->GetDevices();
2311 ASSERT_EQ(1U, devices.size());
2314 TEST_F(BluetoothChromeOSTest, ForgetUnpairedDevice) {
2315 GetAdapter();
2316 DiscoverDevices();
2318 BluetoothDevice* device = adapter_->GetDevice(
2319 FakeBluetoothDeviceClient::kConnectUnpairableAddress);
2320 ASSERT_TRUE(device != nullptr);
2321 ASSERT_FALSE(device->IsPaired());
2323 // Connect the device so it becomes trusted and remembered.
2324 device->Connect(nullptr, GetCallback(),
2325 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2326 base::Unretained(this)));
2328 ASSERT_EQ(1, callback_count_);
2329 ASSERT_EQ(0, error_callback_count_);
2330 callback_count_ = 0;
2332 ASSERT_TRUE(device->IsConnected());
2333 ASSERT_FALSE(device->IsConnecting());
2335 // Make sure the trusted property has been set to true.
2336 FakeBluetoothDeviceClient::Properties* properties =
2337 fake_bluetooth_device_client_->GetProperties(
2338 dbus::ObjectPath(FakeBluetoothDeviceClient::kConnectUnpairablePath));
2339 ASSERT_TRUE(properties->trusted.value());
2341 // Install an observer; expect the DeviceRemoved method to be called
2342 // with the device we remove.
2343 TestBluetoothAdapterObserver observer(adapter_);
2345 device->Forget(GetErrorCallback());
2346 EXPECT_EQ(0, error_callback_count_);
2348 EXPECT_EQ(1, observer.device_removed_count());
2349 EXPECT_EQ(FakeBluetoothDeviceClient::kConnectUnpairableAddress,
2350 observer.last_device_address());
2352 // GetDevices shouldn't return the device either.
2353 device = adapter_->GetDevice(
2354 FakeBluetoothDeviceClient::kConnectUnpairableAddress);
2355 EXPECT_FALSE(device != nullptr);
2358 TEST_F(BluetoothChromeOSTest, ConnectPairedDevice) {
2359 GetAdapter();
2361 BluetoothDevice* device = adapter_->GetDevice(
2362 FakeBluetoothDeviceClient::kPairedDeviceAddress);
2363 ASSERT_TRUE(device != nullptr);
2364 ASSERT_TRUE(device->IsPaired());
2366 TestBluetoothAdapterObserver observer(adapter_);
2368 // Connect without a pairing delegate; since the device is already Paired
2369 // this should succeed and the device should become connected.
2370 device->Connect(nullptr, GetCallback(),
2371 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2372 base::Unretained(this)));
2374 EXPECT_EQ(1, callback_count_);
2375 EXPECT_EQ(0, error_callback_count_);
2377 // Two changes for connecting, one for connected and one for for trusted
2378 // after connecting.
2379 EXPECT_EQ(4, observer.device_changed_count());
2380 EXPECT_EQ(device, observer.last_device());
2382 EXPECT_TRUE(device->IsConnected());
2383 EXPECT_FALSE(device->IsConnecting());
2386 TEST_F(BluetoothChromeOSTest, ConnectUnpairableDevice) {
2387 GetAdapter();
2388 DiscoverDevices();
2390 BluetoothDevice* device = adapter_->GetDevice(
2391 FakeBluetoothDeviceClient::kConnectUnpairableAddress);
2392 ASSERT_TRUE(device != nullptr);
2393 ASSERT_FALSE(device->IsPaired());
2395 TestBluetoothAdapterObserver observer(adapter_);
2397 // Connect without a pairing delegate; since the device does not require
2398 // pairing, this should succeed and the device should become connected.
2399 device->Connect(nullptr, GetCallback(),
2400 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2401 base::Unretained(this)));
2403 EXPECT_EQ(1, callback_count_);
2404 EXPECT_EQ(0, error_callback_count_);
2406 // Two changes for connecting, one for connected, one for for trusted after
2407 // connection, and one for the reconnect mode (IsConnectable).
2408 EXPECT_EQ(5, observer.device_changed_count());
2409 EXPECT_EQ(device, observer.last_device());
2411 EXPECT_TRUE(device->IsConnected());
2412 EXPECT_FALSE(device->IsConnecting());
2414 // Make sure the trusted property has been set to true.
2415 FakeBluetoothDeviceClient::Properties* properties =
2416 fake_bluetooth_device_client_->GetProperties(
2417 dbus::ObjectPath(FakeBluetoothDeviceClient::kConnectUnpairablePath));
2418 EXPECT_TRUE(properties->trusted.value());
2420 // Verify is a HID device and is not connectable.
2421 BluetoothDevice::UUIDList uuids = device->GetUUIDs();
2422 ASSERT_EQ(1U, uuids.size());
2423 EXPECT_EQ(uuids[0], BluetoothUUID("1124"));
2424 EXPECT_FALSE(device->IsConnectable());
2427 TEST_F(BluetoothChromeOSTest, ConnectConnectedDevice) {
2428 GetAdapter();
2430 BluetoothDevice* device = adapter_->GetDevice(
2431 FakeBluetoothDeviceClient::kPairedDeviceAddress);
2432 ASSERT_TRUE(device != nullptr);
2433 ASSERT_TRUE(device->IsPaired());
2435 device->Connect(nullptr, GetCallback(),
2436 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2437 base::Unretained(this)));
2439 ASSERT_EQ(1, callback_count_);
2440 ASSERT_EQ(0, error_callback_count_);
2441 callback_count_ = 0;
2443 ASSERT_TRUE(device->IsConnected());
2445 // Connect again; since the device is already Connected, this shouldn't do
2446 // anything to initiate the connection.
2447 TestBluetoothAdapterObserver observer(adapter_);
2449 device->Connect(nullptr, GetCallback(),
2450 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2451 base::Unretained(this)));
2453 EXPECT_EQ(1, callback_count_);
2454 EXPECT_EQ(0, error_callback_count_);
2456 // The observer will be called because Connecting will toggle true and false,
2457 // and the trusted property will be updated to true.
2458 EXPECT_EQ(3, observer.device_changed_count());
2460 EXPECT_TRUE(device->IsConnected());
2461 EXPECT_FALSE(device->IsConnecting());
2464 TEST_F(BluetoothChromeOSTest, ConnectDeviceFails) {
2465 GetAdapter();
2466 DiscoverDevices();
2468 BluetoothDevice* device = adapter_->GetDevice(
2469 FakeBluetoothDeviceClient::kLegacyAutopairAddress);
2470 ASSERT_TRUE(device != nullptr);
2471 ASSERT_FALSE(device->IsPaired());
2473 TestBluetoothAdapterObserver observer(adapter_);
2475 // Connect without a pairing delegate; since the device requires pairing,
2476 // this should fail with an error.
2477 device->Connect(nullptr, GetCallback(),
2478 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2479 base::Unretained(this)));
2481 EXPECT_EQ(0, callback_count_);
2482 EXPECT_EQ(1, error_callback_count_);
2483 EXPECT_EQ(BluetoothDevice::ERROR_FAILED, last_connect_error_);
2485 EXPECT_EQ(2, observer.device_changed_count());
2487 EXPECT_FALSE(device->IsConnected());
2488 EXPECT_FALSE(device->IsConnecting());
2491 TEST_F(BluetoothChromeOSTest, DisconnectDevice) {
2492 GetAdapter();
2494 BluetoothDevice* device = adapter_->GetDevice(
2495 FakeBluetoothDeviceClient::kPairedDeviceAddress);
2496 ASSERT_TRUE(device != nullptr);
2497 ASSERT_TRUE(device->IsPaired());
2499 device->Connect(nullptr, GetCallback(),
2500 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2501 base::Unretained(this)));
2503 ASSERT_EQ(1, callback_count_);
2504 ASSERT_EQ(0, error_callback_count_);
2505 callback_count_ = 0;
2507 ASSERT_TRUE(device->IsConnected());
2508 ASSERT_FALSE(device->IsConnecting());
2510 // Disconnect the device, we should see the observer method fire and the
2511 // device get dropped.
2512 TestBluetoothAdapterObserver observer(adapter_);
2514 device->Disconnect(GetCallback(), GetErrorCallback());
2516 EXPECT_EQ(1, callback_count_);
2517 EXPECT_EQ(0, error_callback_count_);
2519 EXPECT_EQ(1, observer.device_changed_count());
2520 EXPECT_EQ(device, observer.last_device());
2522 EXPECT_FALSE(device->IsConnected());
2525 TEST_F(BluetoothChromeOSTest, DisconnectUnconnectedDevice) {
2526 GetAdapter();
2528 BluetoothDevice* device = adapter_->GetDevice(
2529 FakeBluetoothDeviceClient::kPairedDeviceAddress);
2530 ASSERT_TRUE(device != nullptr);
2531 ASSERT_TRUE(device->IsPaired());
2532 ASSERT_FALSE(device->IsConnected());
2534 // Disconnect the device, we should see the observer method fire and the
2535 // device get dropped.
2536 TestBluetoothAdapterObserver observer(adapter_);
2538 device->Disconnect(GetCallback(), GetErrorCallback());
2540 EXPECT_EQ(0, callback_count_);
2541 EXPECT_EQ(1, error_callback_count_);
2543 EXPECT_EQ(0, observer.device_changed_count());
2545 EXPECT_FALSE(device->IsConnected());
2548 TEST_F(BluetoothChromeOSTest, PairLegacyAutopair) {
2549 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2551 GetAdapter();
2552 DiscoverDevices();
2554 // The Legacy Autopair device requires no PIN or Passkey to pair because
2555 // the daemon provides 0000 to the device for us.
2556 BluetoothDevice* device = adapter_->GetDevice(
2557 FakeBluetoothDeviceClient::kLegacyAutopairAddress);
2558 ASSERT_TRUE(device != nullptr);
2559 ASSERT_FALSE(device->IsPaired());
2561 TestBluetoothAdapterObserver observer(adapter_);
2563 TestPairingDelegate pairing_delegate;
2564 device->Connect(&pairing_delegate, GetCallback(),
2565 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2566 base::Unretained(this)));
2568 EXPECT_EQ(0, pairing_delegate.call_count_);
2569 EXPECT_TRUE(device->IsConnecting());
2571 message_loop_.Run();
2573 EXPECT_EQ(1, callback_count_);
2574 EXPECT_EQ(0, error_callback_count_);
2576 // Two changes for connecting, one change for connected, one for paired,
2577 // two for trusted (after pairing and connection), and one for the reconnect
2578 // mode (IsConnectable).
2579 EXPECT_EQ(7, observer.device_changed_count());
2580 EXPECT_EQ(device, observer.last_device());
2582 EXPECT_TRUE(device->IsConnected());
2583 EXPECT_FALSE(device->IsConnecting());
2585 EXPECT_TRUE(device->IsPaired());
2587 // Verify is a HID device and is connectable.
2588 BluetoothDevice::UUIDList uuids = device->GetUUIDs();
2589 ASSERT_EQ(1U, uuids.size());
2590 EXPECT_EQ(uuids[0], BluetoothUUID("1124"));
2591 EXPECT_TRUE(device->IsConnectable());
2593 // Make sure the trusted property has been set to true.
2594 FakeBluetoothDeviceClient::Properties* properties =
2595 fake_bluetooth_device_client_->GetProperties(
2596 dbus::ObjectPath(FakeBluetoothDeviceClient::kLegacyAutopairPath));
2597 EXPECT_TRUE(properties->trusted.value());
2600 TEST_F(BluetoothChromeOSTest, PairDisplayPinCode) {
2601 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2603 GetAdapter();
2604 DiscoverDevices();
2606 // Requires that we display a randomly generated PIN on the screen.
2607 BluetoothDevice* device = adapter_->GetDevice(
2608 FakeBluetoothDeviceClient::kDisplayPinCodeAddress);
2609 ASSERT_TRUE(device != nullptr);
2610 ASSERT_FALSE(device->IsPaired());
2612 TestBluetoothAdapterObserver observer(adapter_);
2614 TestPairingDelegate pairing_delegate;
2615 device->Connect(&pairing_delegate, GetCallback(),
2616 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2617 base::Unretained(this)));
2619 EXPECT_EQ(1, pairing_delegate.call_count_);
2620 EXPECT_EQ(1, pairing_delegate.display_pincode_count_);
2621 EXPECT_EQ("123456", pairing_delegate.last_pincode_);
2622 EXPECT_TRUE(device->IsConnecting());
2624 message_loop_.Run();
2626 EXPECT_EQ(1, callback_count_);
2627 EXPECT_EQ(0, error_callback_count_);
2629 // Two changes for connecting, one change for connected, one for paired,
2630 // two for trusted (after pairing and connection), and one for the reconnect
2631 // mode (IsConnectable).
2632 EXPECT_EQ(7, observer.device_changed_count());
2633 EXPECT_EQ(device, observer.last_device());
2635 EXPECT_TRUE(device->IsConnected());
2636 EXPECT_FALSE(device->IsConnecting());
2638 EXPECT_TRUE(device->IsPaired());
2640 // Verify is a HID device and is connectable.
2641 BluetoothDevice::UUIDList uuids = device->GetUUIDs();
2642 ASSERT_EQ(1U, uuids.size());
2643 EXPECT_EQ(uuids[0], BluetoothUUID("1124"));
2644 EXPECT_TRUE(device->IsConnectable());
2646 // Make sure the trusted property has been set to true.
2647 FakeBluetoothDeviceClient::Properties* properties =
2648 fake_bluetooth_device_client_->GetProperties(
2649 dbus::ObjectPath(FakeBluetoothDeviceClient::kDisplayPinCodePath));
2650 EXPECT_TRUE(properties->trusted.value());
2653 TEST_F(BluetoothChromeOSTest, PairDisplayPasskey) {
2654 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2656 GetAdapter();
2657 DiscoverDevices();
2659 // Requires that we display a randomly generated Passkey on the screen,
2660 // and notifies us as it's typed in.
2661 BluetoothDevice* device = adapter_->GetDevice(
2662 FakeBluetoothDeviceClient::kDisplayPasskeyAddress);
2663 ASSERT_TRUE(device != nullptr);
2664 ASSERT_FALSE(device->IsPaired());
2666 TestBluetoothAdapterObserver observer(adapter_);
2668 TestPairingDelegate pairing_delegate;
2669 device->Connect(&pairing_delegate, GetCallback(),
2670 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2671 base::Unretained(this)));
2673 // One call for DisplayPasskey() and one for KeysEntered().
2674 EXPECT_EQ(2, pairing_delegate.call_count_);
2675 EXPECT_EQ(1, pairing_delegate.display_passkey_count_);
2676 EXPECT_EQ(123456U, pairing_delegate.last_passkey_);
2677 EXPECT_EQ(1, pairing_delegate.keys_entered_count_);
2678 EXPECT_EQ(0U, pairing_delegate.last_entered_);
2680 EXPECT_TRUE(device->IsConnecting());
2682 // One call to KeysEntered() for each key, including [enter].
2683 for (int i = 1; i <= 7; ++i) {
2684 message_loop_.Run();
2686 EXPECT_EQ(2 + i, pairing_delegate.call_count_);
2687 EXPECT_EQ(1 + i, pairing_delegate.keys_entered_count_);
2688 EXPECT_EQ(static_cast<uint32_t>(i), pairing_delegate.last_entered_);
2691 message_loop_.Run();
2693 // 8 KeysEntered notifications (0 to 7, inclusive) and one aditional call for
2694 // DisplayPasskey().
2695 EXPECT_EQ(9, pairing_delegate.call_count_);
2696 EXPECT_EQ(8, pairing_delegate.keys_entered_count_);
2697 EXPECT_EQ(7U, pairing_delegate.last_entered_);
2699 EXPECT_EQ(1, callback_count_);
2700 EXPECT_EQ(0, error_callback_count_);
2702 // Two changes for connecting, one change for connected, one for paired,
2703 // two for trusted (after pairing and connection), and one for the reconnect
2704 // mode (IsConnectable).
2705 EXPECT_EQ(7, observer.device_changed_count());
2706 EXPECT_EQ(device, observer.last_device());
2708 EXPECT_TRUE(device->IsConnected());
2709 EXPECT_FALSE(device->IsConnecting());
2711 EXPECT_TRUE(device->IsPaired());
2713 // Verify is a HID device.
2714 BluetoothDevice::UUIDList uuids = device->GetUUIDs();
2715 ASSERT_EQ(1U, uuids.size());
2716 EXPECT_EQ(uuids[0], BluetoothUUID("1124"));
2718 // And usually not connectable.
2719 EXPECT_FALSE(device->IsConnectable());
2721 // Make sure the trusted property has been set to true.
2722 FakeBluetoothDeviceClient::Properties* properties =
2723 fake_bluetooth_device_client_->GetProperties(
2724 dbus::ObjectPath(FakeBluetoothDeviceClient::kDisplayPasskeyPath));
2725 EXPECT_TRUE(properties->trusted.value());
2728 TEST_F(BluetoothChromeOSTest, PairRequestPinCode) {
2729 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2731 GetAdapter();
2732 DiscoverDevices();
2734 // Requires that the user enters a PIN for them.
2735 BluetoothDevice* device = adapter_->GetDevice(
2736 FakeBluetoothDeviceClient::kRequestPinCodeAddress);
2737 ASSERT_TRUE(device != nullptr);
2738 ASSERT_FALSE(device->IsPaired());
2740 TestBluetoothAdapterObserver observer(adapter_);
2742 TestPairingDelegate pairing_delegate;
2743 device->Connect(&pairing_delegate, GetCallback(),
2744 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2745 base::Unretained(this)));
2747 EXPECT_EQ(1, pairing_delegate.call_count_);
2748 EXPECT_EQ(1, pairing_delegate.request_pincode_count_);
2749 EXPECT_TRUE(device->IsConnecting());
2751 // Set the PIN.
2752 device->SetPinCode("1234");
2753 message_loop_.Run();
2755 EXPECT_EQ(1, callback_count_);
2756 EXPECT_EQ(0, error_callback_count_);
2758 // Two changes for connecting, one change for connected, one for paired and
2759 // two for trusted (after pairing and connection).
2760 EXPECT_EQ(6, observer.device_changed_count());
2761 EXPECT_EQ(device, observer.last_device());
2763 EXPECT_TRUE(device->IsConnected());
2764 EXPECT_FALSE(device->IsConnecting());
2766 EXPECT_TRUE(device->IsPaired());
2768 // Verify is not a HID device.
2769 BluetoothDevice::UUIDList uuids = device->GetUUIDs();
2770 ASSERT_EQ(0U, uuids.size());
2772 // Non HID devices are always connectable.
2773 EXPECT_TRUE(device->IsConnectable());
2775 // Make sure the trusted property has been set to true.
2776 FakeBluetoothDeviceClient::Properties* properties =
2777 fake_bluetooth_device_client_->GetProperties(
2778 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath));
2779 EXPECT_TRUE(properties->trusted.value());
2782 TEST_F(BluetoothChromeOSTest, PairConfirmPasskey) {
2783 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2785 GetAdapter();
2786 DiscoverDevices();
2788 // Requests that we confirm a displayed passkey.
2789 BluetoothDevice* device = adapter_->GetDevice(
2790 FakeBluetoothDeviceClient::kConfirmPasskeyAddress);
2791 ASSERT_TRUE(device != nullptr);
2792 ASSERT_FALSE(device->IsPaired());
2794 TestBluetoothAdapterObserver observer(adapter_);
2796 TestPairingDelegate pairing_delegate;
2797 device->Connect(&pairing_delegate, GetCallback(),
2798 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2799 base::Unretained(this)));
2801 EXPECT_EQ(1, pairing_delegate.call_count_);
2802 EXPECT_EQ(1, pairing_delegate.confirm_passkey_count_);
2803 EXPECT_EQ(123456U, pairing_delegate.last_passkey_);
2804 EXPECT_TRUE(device->IsConnecting());
2806 // Confirm the passkey.
2807 device->ConfirmPairing();
2808 message_loop_.Run();
2810 EXPECT_EQ(1, callback_count_);
2811 EXPECT_EQ(0, error_callback_count_);
2813 // Two changes for connecting, one change for connected, one for paired and
2814 // two for trusted (after pairing and connection).
2815 EXPECT_EQ(6, observer.device_changed_count());
2816 EXPECT_EQ(device, observer.last_device());
2818 EXPECT_TRUE(device->IsConnected());
2819 EXPECT_FALSE(device->IsConnecting());
2821 EXPECT_TRUE(device->IsPaired());
2823 // Non HID devices are always connectable.
2824 EXPECT_TRUE(device->IsConnectable());
2826 // Make sure the trusted property has been set to true.
2827 FakeBluetoothDeviceClient::Properties* properties =
2828 fake_bluetooth_device_client_->GetProperties(
2829 dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath));
2830 EXPECT_TRUE(properties->trusted.value());
2833 TEST_F(BluetoothChromeOSTest, PairRequestPasskey) {
2834 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2836 GetAdapter();
2837 DiscoverDevices();
2839 // Requires that the user enters a Passkey, this would be some kind of
2840 // device that has a display, but doesn't use "just works" - maybe a car?
2841 BluetoothDevice* device = adapter_->GetDevice(
2842 FakeBluetoothDeviceClient::kRequestPasskeyAddress);
2843 ASSERT_TRUE(device != nullptr);
2844 ASSERT_FALSE(device->IsPaired());
2846 TestBluetoothAdapterObserver observer(adapter_);
2848 TestPairingDelegate pairing_delegate;
2849 device->Connect(&pairing_delegate, GetCallback(),
2850 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2851 base::Unretained(this)));
2853 EXPECT_EQ(1, pairing_delegate.call_count_);
2854 EXPECT_EQ(1, pairing_delegate.request_passkey_count_);
2855 EXPECT_TRUE(device->IsConnecting());
2857 // Set the Passkey.
2858 device->SetPasskey(1234);
2859 message_loop_.Run();
2861 EXPECT_EQ(1, callback_count_);
2862 EXPECT_EQ(0, error_callback_count_);
2864 // Two changes for connecting, one change for connected, one for paired and
2865 // two for trusted (after pairing and connection).
2866 EXPECT_EQ(6, observer.device_changed_count());
2867 EXPECT_EQ(device, observer.last_device());
2869 EXPECT_TRUE(device->IsConnected());
2870 EXPECT_FALSE(device->IsConnecting());
2872 EXPECT_TRUE(device->IsPaired());
2874 // Non HID devices are always connectable.
2875 EXPECT_TRUE(device->IsConnectable());
2877 // Make sure the trusted property has been set to true.
2878 FakeBluetoothDeviceClient::Properties* properties =
2879 fake_bluetooth_device_client_->GetProperties(
2880 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath));
2881 EXPECT_TRUE(properties->trusted.value());
2884 TEST_F(BluetoothChromeOSTest, PairJustWorks) {
2885 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2887 GetAdapter();
2888 DiscoverDevices();
2890 // Uses just-works pairing, since this is an outgoing pairing, no delegate
2891 // interaction is required.
2892 BluetoothDevice* device = adapter_->GetDevice(
2893 FakeBluetoothDeviceClient::kJustWorksAddress);
2894 ASSERT_TRUE(device != nullptr);
2895 ASSERT_FALSE(device->IsPaired());
2897 TestBluetoothAdapterObserver observer(adapter_);
2899 TestPairingDelegate pairing_delegate;
2900 device->Connect(&pairing_delegate, GetCallback(),
2901 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2902 base::Unretained(this)));
2904 EXPECT_EQ(0, pairing_delegate.call_count_);
2906 message_loop_.Run();
2908 EXPECT_EQ(1, callback_count_);
2909 EXPECT_EQ(0, error_callback_count_);
2911 // Two changes for connecting, one change for connected, one for paired and
2912 // two for trusted (after pairing and connection).
2913 EXPECT_EQ(6, observer.device_changed_count());
2914 EXPECT_EQ(device, observer.last_device());
2916 EXPECT_TRUE(device->IsConnected());
2917 EXPECT_FALSE(device->IsConnecting());
2919 EXPECT_TRUE(device->IsPaired());
2921 // Non HID devices are always connectable.
2922 EXPECT_TRUE(device->IsConnectable());
2924 // Make sure the trusted property has been set to true.
2925 FakeBluetoothDeviceClient::Properties* properties =
2926 fake_bluetooth_device_client_->GetProperties(
2927 dbus::ObjectPath(FakeBluetoothDeviceClient::kJustWorksPath));
2928 EXPECT_TRUE(properties->trusted.value());
2931 TEST_F(BluetoothChromeOSTest, PairUnpairableDeviceFails) {
2932 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2934 GetAdapter();
2935 DiscoverDevice(FakeBluetoothDeviceClient::kUnconnectableDeviceAddress);
2937 BluetoothDevice* device = adapter_->GetDevice(
2938 FakeBluetoothDeviceClient::kUnpairableDeviceAddress);
2939 ASSERT_TRUE(device != nullptr);
2940 ASSERT_FALSE(device->IsPaired());
2942 TestBluetoothAdapterObserver observer(adapter_);
2944 TestPairingDelegate pairing_delegate;
2945 device->Connect(&pairing_delegate, GetCallback(),
2946 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2947 base::Unretained(this)));
2949 EXPECT_EQ(0, pairing_delegate.call_count_);
2950 EXPECT_TRUE(device->IsConnecting());
2952 // Run the loop to get the error..
2953 message_loop_.Run();
2955 EXPECT_EQ(0, callback_count_);
2956 EXPECT_EQ(1, error_callback_count_);
2958 EXPECT_EQ(BluetoothDevice::ERROR_FAILED, last_connect_error_);
2960 EXPECT_FALSE(device->IsConnected());
2961 EXPECT_FALSE(device->IsConnecting());
2962 EXPECT_FALSE(device->IsPaired());
2965 TEST_F(BluetoothChromeOSTest, PairingFails) {
2966 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2968 GetAdapter();
2969 DiscoverDevice(FakeBluetoothDeviceClient::kVanishingDeviceAddress);
2971 // The vanishing device times out during pairing
2972 BluetoothDevice* device = adapter_->GetDevice(
2973 FakeBluetoothDeviceClient::kVanishingDeviceAddress);
2974 ASSERT_TRUE(device != nullptr);
2975 ASSERT_FALSE(device->IsPaired());
2977 TestBluetoothAdapterObserver observer(adapter_);
2979 TestPairingDelegate pairing_delegate;
2980 device->Connect(&pairing_delegate, GetCallback(),
2981 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2982 base::Unretained(this)));
2984 EXPECT_EQ(0, pairing_delegate.call_count_);
2985 EXPECT_TRUE(device->IsConnecting());
2987 // Run the loop to get the error..
2988 message_loop_.Run();
2990 EXPECT_EQ(0, callback_count_);
2991 EXPECT_EQ(1, error_callback_count_);
2993 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_TIMEOUT, last_connect_error_);
2995 EXPECT_FALSE(device->IsConnected());
2996 EXPECT_FALSE(device->IsConnecting());
2997 EXPECT_FALSE(device->IsPaired());
3000 TEST_F(BluetoothChromeOSTest, PairingFailsAtConnection) {
3001 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3003 GetAdapter();
3004 DiscoverDevices();
3006 // Everything seems to go according to plan with the unconnectable device;
3007 // it pairs, but then you can't make connections to it after.
3008 BluetoothDevice* device = adapter_->GetDevice(
3009 FakeBluetoothDeviceClient::kUnconnectableDeviceAddress);
3010 ASSERT_TRUE(device != nullptr);
3011 ASSERT_FALSE(device->IsPaired());
3013 TestBluetoothAdapterObserver observer(adapter_);
3015 TestPairingDelegate pairing_delegate;
3016 device->Connect(&pairing_delegate, GetCallback(),
3017 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
3018 base::Unretained(this)));
3020 EXPECT_EQ(0, pairing_delegate.call_count_);
3021 EXPECT_TRUE(device->IsConnecting());
3023 message_loop_.Run();
3025 EXPECT_EQ(0, callback_count_);
3026 EXPECT_EQ(1, error_callback_count_);
3027 EXPECT_EQ(BluetoothDevice::ERROR_FAILED, last_connect_error_);
3029 // Two changes for connecting, one for paired and one for trusted after
3030 // pairing. The device should not be connected.
3031 EXPECT_EQ(4, observer.device_changed_count());
3032 EXPECT_EQ(device, observer.last_device());
3034 EXPECT_FALSE(device->IsConnected());
3035 EXPECT_FALSE(device->IsConnecting());
3037 EXPECT_TRUE(device->IsPaired());
3039 // Make sure the trusted property has been set to true still (since pairing
3040 // worked).
3041 FakeBluetoothDeviceClient::Properties* properties =
3042 fake_bluetooth_device_client_->GetProperties(
3043 dbus::ObjectPath(
3044 FakeBluetoothDeviceClient::kUnconnectableDevicePath));
3045 EXPECT_TRUE(properties->trusted.value());
3048 TEST_F(BluetoothChromeOSTest, PairingRejectedAtPinCode) {
3049 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3051 GetAdapter();
3052 DiscoverDevices();
3054 // Reject the pairing after we receive a request for the PIN code.
3055 BluetoothDevice* device = adapter_->GetDevice(
3056 FakeBluetoothDeviceClient::kRequestPinCodeAddress);
3057 ASSERT_TRUE(device != nullptr);
3058 ASSERT_FALSE(device->IsPaired());
3060 TestBluetoothAdapterObserver observer(adapter_);
3062 TestPairingDelegate pairing_delegate;
3063 device->Connect(&pairing_delegate, GetCallback(),
3064 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
3065 base::Unretained(this)));
3067 EXPECT_EQ(1, pairing_delegate.call_count_);
3068 EXPECT_EQ(1, pairing_delegate.request_pincode_count_);
3069 EXPECT_TRUE(device->IsConnecting());
3071 // Reject the pairing.
3072 device->RejectPairing();
3073 message_loop_.Run();
3075 EXPECT_EQ(0, callback_count_);
3076 EXPECT_EQ(1, error_callback_count_);
3077 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_REJECTED, last_connect_error_);
3079 // Should be no changes except connecting going true and false.
3080 EXPECT_EQ(2, observer.device_changed_count());
3081 EXPECT_FALSE(device->IsConnected());
3082 EXPECT_FALSE(device->IsConnecting());
3083 EXPECT_FALSE(device->IsPaired());
3086 TEST_F(BluetoothChromeOSTest, PairingCancelledAtPinCode) {
3087 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3089 GetAdapter();
3090 DiscoverDevices();
3092 // Cancel the pairing after we receive a request for the PIN code.
3093 BluetoothDevice* device = adapter_->GetDevice(
3094 FakeBluetoothDeviceClient::kRequestPinCodeAddress);
3095 ASSERT_TRUE(device != nullptr);
3096 ASSERT_FALSE(device->IsPaired());
3098 TestBluetoothAdapterObserver observer(adapter_);
3100 TestPairingDelegate pairing_delegate;
3101 device->Connect(&pairing_delegate, GetCallback(),
3102 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
3103 base::Unretained(this)));
3105 EXPECT_EQ(1, pairing_delegate.call_count_);
3106 EXPECT_EQ(1, pairing_delegate.request_pincode_count_);
3107 EXPECT_TRUE(device->IsConnecting());
3109 // Cancel the pairing.
3110 device->CancelPairing();
3111 message_loop_.Run();
3113 EXPECT_EQ(0, callback_count_);
3114 EXPECT_EQ(1, error_callback_count_);
3115 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED, last_connect_error_);
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());
3124 TEST_F(BluetoothChromeOSTest, PairingRejectedAtPasskey) {
3125 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3127 GetAdapter();
3128 DiscoverDevices();
3130 // Reject the pairing after we receive a request for the passkey.
3131 BluetoothDevice* device = adapter_->GetDevice(
3132 FakeBluetoothDeviceClient::kRequestPasskeyAddress);
3133 ASSERT_TRUE(device != nullptr);
3134 ASSERT_FALSE(device->IsPaired());
3136 TestBluetoothAdapterObserver observer(adapter_);
3138 TestPairingDelegate pairing_delegate;
3139 device->Connect(&pairing_delegate, GetCallback(),
3140 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
3141 base::Unretained(this)));
3143 EXPECT_EQ(1, pairing_delegate.call_count_);
3144 EXPECT_EQ(1, pairing_delegate.request_passkey_count_);
3145 EXPECT_TRUE(device->IsConnecting());
3147 // Reject the pairing.
3148 device->RejectPairing();
3149 message_loop_.Run();
3151 EXPECT_EQ(0, callback_count_);
3152 EXPECT_EQ(1, error_callback_count_);
3153 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_REJECTED, last_connect_error_);
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());
3162 TEST_F(BluetoothChromeOSTest, PairingCancelledAtPasskey) {
3163 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3165 GetAdapter();
3166 DiscoverDevices();
3168 // Cancel the pairing after we receive a request for the passkey.
3169 BluetoothDevice* device = adapter_->GetDevice(
3170 FakeBluetoothDeviceClient::kRequestPasskeyAddress);
3171 ASSERT_TRUE(device != nullptr);
3172 ASSERT_FALSE(device->IsPaired());
3174 TestBluetoothAdapterObserver observer(adapter_);
3176 TestPairingDelegate pairing_delegate;
3177 device->Connect(&pairing_delegate, GetCallback(),
3178 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
3179 base::Unretained(this)));
3181 EXPECT_EQ(1, pairing_delegate.call_count_);
3182 EXPECT_EQ(1, pairing_delegate.request_passkey_count_);
3183 EXPECT_TRUE(device->IsConnecting());
3185 // Cancel the pairing.
3186 device->CancelPairing();
3187 message_loop_.Run();
3189 EXPECT_EQ(0, callback_count_);
3190 EXPECT_EQ(1, error_callback_count_);
3191 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED, last_connect_error_);
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());
3200 TEST_F(BluetoothChromeOSTest, PairingRejectedAtConfirmation) {
3201 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3203 GetAdapter();
3204 DiscoverDevices();
3206 // Reject the pairing after we receive a request for passkey confirmation.
3207 BluetoothDevice* device = adapter_->GetDevice(
3208 FakeBluetoothDeviceClient::kConfirmPasskeyAddress);
3209 ASSERT_TRUE(device != nullptr);
3210 ASSERT_FALSE(device->IsPaired());
3212 TestBluetoothAdapterObserver observer(adapter_);
3214 TestPairingDelegate pairing_delegate;
3215 device->Connect(&pairing_delegate, GetCallback(),
3216 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
3217 base::Unretained(this)));
3219 EXPECT_EQ(1, pairing_delegate.call_count_);
3220 EXPECT_EQ(1, pairing_delegate.confirm_passkey_count_);
3221 EXPECT_TRUE(device->IsConnecting());
3223 // Reject the pairing.
3224 device->RejectPairing();
3225 message_loop_.Run();
3227 EXPECT_EQ(0, callback_count_);
3228 EXPECT_EQ(1, error_callback_count_);
3229 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_REJECTED, last_connect_error_);
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());
3238 TEST_F(BluetoothChromeOSTest, PairingCancelledAtConfirmation) {
3239 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3241 GetAdapter();
3242 DiscoverDevices();
3244 // Cancel the pairing after we receive a request for the passkey.
3245 BluetoothDevice* device = adapter_->GetDevice(
3246 FakeBluetoothDeviceClient::kConfirmPasskeyAddress);
3247 ASSERT_TRUE(device != nullptr);
3248 ASSERT_FALSE(device->IsPaired());
3250 TestBluetoothAdapterObserver observer(adapter_);
3252 TestPairingDelegate pairing_delegate;
3253 device->Connect(&pairing_delegate, GetCallback(),
3254 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
3255 base::Unretained(this)));
3257 EXPECT_EQ(1, pairing_delegate.call_count_);
3258 EXPECT_EQ(1, pairing_delegate.confirm_passkey_count_);
3259 EXPECT_TRUE(device->IsConnecting());
3261 // Cancel the pairing.
3262 device->CancelPairing();
3263 message_loop_.Run();
3265 EXPECT_EQ(0, callback_count_);
3266 EXPECT_EQ(1, error_callback_count_);
3267 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED, last_connect_error_);
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());
3276 TEST_F(BluetoothChromeOSTest, PairingCancelledInFlight) {
3277 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3279 GetAdapter();
3280 DiscoverDevices();
3282 // Cancel the pairing while we're waiting for the remote host.
3283 BluetoothDevice* device = adapter_->GetDevice(
3284 FakeBluetoothDeviceClient::kLegacyAutopairAddress);
3285 ASSERT_TRUE(device != nullptr);
3286 ASSERT_FALSE(device->IsPaired());
3288 TestBluetoothAdapterObserver observer(adapter_);
3290 TestPairingDelegate pairing_delegate;
3291 device->Connect(&pairing_delegate, GetCallback(),
3292 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
3293 base::Unretained(this)));
3295 EXPECT_EQ(0, pairing_delegate.call_count_);
3296 EXPECT_TRUE(device->IsConnecting());
3298 // Cancel the pairing.
3299 device->CancelPairing();
3300 message_loop_.Run();
3302 EXPECT_EQ(0, callback_count_);
3303 EXPECT_EQ(1, error_callback_count_);
3304 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED, last_connect_error_);
3306 // Should be no changes except connecting going true and false.
3307 EXPECT_EQ(2, observer.device_changed_count());
3308 EXPECT_FALSE(device->IsConnected());
3309 EXPECT_FALSE(device->IsConnecting());
3310 EXPECT_FALSE(device->IsPaired());
3313 TEST_F(BluetoothChromeOSTest, IncomingPairRequestPinCode) {
3314 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3316 GetAdapter();
3318 TestPairingDelegate pairing_delegate;
3319 adapter_->AddPairingDelegate(
3320 &pairing_delegate,
3321 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH);
3323 // Requires that we provide a PIN code.
3324 fake_bluetooth_device_client_->CreateDevice(
3325 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
3326 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath));
3327 BluetoothDevice* device = adapter_->GetDevice(
3328 FakeBluetoothDeviceClient::kRequestPinCodeAddress);
3329 ASSERT_TRUE(device != nullptr);
3330 ASSERT_FALSE(device->IsPaired());
3332 TestBluetoothAdapterObserver observer(adapter_);
3334 fake_bluetooth_device_client_->SimulatePairing(
3335 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath), true,
3336 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
3337 base::Unretained(this)));
3339 EXPECT_EQ(1, pairing_delegate.call_count_);
3340 EXPECT_EQ(1, pairing_delegate.request_pincode_count_);
3342 // Set the PIN.
3343 device->SetPinCode("1234");
3344 message_loop_.Run();
3346 EXPECT_EQ(1, callback_count_);
3347 EXPECT_EQ(0, error_callback_count_);
3349 // One change for paired, and one for trusted.
3350 EXPECT_EQ(2, observer.device_changed_count());
3351 EXPECT_EQ(device, observer.last_device());
3353 EXPECT_TRUE(device->IsPaired());
3355 // Make sure the trusted property has been set to true.
3356 FakeBluetoothDeviceClient::Properties* properties =
3357 fake_bluetooth_device_client_->GetProperties(
3358 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath));
3359 ASSERT_TRUE(properties->trusted.value());
3361 // No pairing context should remain on the device.
3362 BluetoothDeviceChromeOS* device_chromeos =
3363 static_cast<BluetoothDeviceChromeOS*>(device);
3364 EXPECT_TRUE(device_chromeos->GetPairing() == nullptr);
3367 TEST_F(BluetoothChromeOSTest, IncomingPairConfirmPasskey) {
3368 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3370 GetAdapter();
3372 TestPairingDelegate pairing_delegate;
3373 adapter_->AddPairingDelegate(
3374 &pairing_delegate,
3375 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH);
3377 // Requests that we confirm a displayed passkey.
3378 fake_bluetooth_device_client_->CreateDevice(
3379 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
3380 dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath));
3381 BluetoothDevice* device = adapter_->GetDevice(
3382 FakeBluetoothDeviceClient::kConfirmPasskeyAddress);
3383 ASSERT_TRUE(device != nullptr);
3384 ASSERT_FALSE(device->IsPaired());
3386 TestBluetoothAdapterObserver observer(adapter_);
3388 fake_bluetooth_device_client_->SimulatePairing(
3389 dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath), true,
3390 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
3391 base::Unretained(this)));
3393 EXPECT_EQ(1, pairing_delegate.call_count_);
3394 EXPECT_EQ(1, pairing_delegate.confirm_passkey_count_);
3395 EXPECT_EQ(123456U, pairing_delegate.last_passkey_);
3397 // Confirm the passkey.
3398 device->ConfirmPairing();
3399 message_loop_.Run();
3401 EXPECT_EQ(1, callback_count_);
3402 EXPECT_EQ(0, error_callback_count_);
3404 // One change for paired, and one for trusted.
3405 EXPECT_EQ(2, observer.device_changed_count());
3406 EXPECT_EQ(device, observer.last_device());
3408 EXPECT_TRUE(device->IsPaired());
3410 // Make sure the trusted property has been set to true.
3411 FakeBluetoothDeviceClient::Properties* properties =
3412 fake_bluetooth_device_client_->GetProperties(
3413 dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath));
3414 ASSERT_TRUE(properties->trusted.value());
3416 // No pairing context should remain on the device.
3417 BluetoothDeviceChromeOS* device_chromeos =
3418 static_cast<BluetoothDeviceChromeOS*>(device);
3419 EXPECT_TRUE(device_chromeos->GetPairing() == nullptr);
3422 TEST_F(BluetoothChromeOSTest, IncomingPairRequestPasskey) {
3423 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3425 GetAdapter();
3427 TestPairingDelegate pairing_delegate;
3428 adapter_->AddPairingDelegate(
3429 &pairing_delegate,
3430 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH);
3432 // Requests that we provide a Passkey.
3433 fake_bluetooth_device_client_->CreateDevice(
3434 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
3435 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath));
3436 BluetoothDevice* device = adapter_->GetDevice(
3437 FakeBluetoothDeviceClient::kRequestPasskeyAddress);
3438 ASSERT_TRUE(device != nullptr);
3439 ASSERT_FALSE(device->IsPaired());
3441 TestBluetoothAdapterObserver observer(adapter_);
3443 fake_bluetooth_device_client_->SimulatePairing(
3444 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath), true,
3445 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
3446 base::Unretained(this)));
3448 EXPECT_EQ(1, pairing_delegate.call_count_);
3449 EXPECT_EQ(1, pairing_delegate.request_passkey_count_);
3451 // Set the Passkey.
3452 device->SetPasskey(1234);
3453 message_loop_.Run();
3455 EXPECT_EQ(1, callback_count_);
3456 EXPECT_EQ(0, error_callback_count_);
3458 // One change for paired, and one for trusted.
3459 EXPECT_EQ(2, observer.device_changed_count());
3460 EXPECT_EQ(device, observer.last_device());
3462 EXPECT_TRUE(device->IsPaired());
3464 // Make sure the trusted property has been set to true.
3465 FakeBluetoothDeviceClient::Properties* properties =
3466 fake_bluetooth_device_client_->GetProperties(
3467 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath));
3468 ASSERT_TRUE(properties->trusted.value());
3470 // No pairing context should remain on the device.
3471 BluetoothDeviceChromeOS* device_chromeos =
3472 static_cast<BluetoothDeviceChromeOS*>(device);
3473 EXPECT_TRUE(device_chromeos->GetPairing() == nullptr);
3476 TEST_F(BluetoothChromeOSTest, IncomingPairJustWorks) {
3477 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3479 GetAdapter();
3481 TestPairingDelegate pairing_delegate;
3482 adapter_->AddPairingDelegate(
3483 &pairing_delegate,
3484 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH);
3486 // Uses just-works pairing so, sinec this an incoming pairing, require
3487 // authorization from the user.
3488 fake_bluetooth_device_client_->CreateDevice(
3489 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
3490 dbus::ObjectPath(FakeBluetoothDeviceClient::kJustWorksPath));
3491 BluetoothDevice* device = adapter_->GetDevice(
3492 FakeBluetoothDeviceClient::kJustWorksAddress);
3493 ASSERT_TRUE(device != nullptr);
3494 ASSERT_FALSE(device->IsPaired());
3496 TestBluetoothAdapterObserver observer(adapter_);
3498 fake_bluetooth_device_client_->SimulatePairing(
3499 dbus::ObjectPath(FakeBluetoothDeviceClient::kJustWorksPath), true,
3500 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
3501 base::Unretained(this)));
3503 EXPECT_EQ(1, pairing_delegate.call_count_);
3504 EXPECT_EQ(1, pairing_delegate.authorize_pairing_count_);
3506 // Confirm the pairing.
3507 device->ConfirmPairing();
3508 message_loop_.Run();
3510 EXPECT_EQ(1, callback_count_);
3511 EXPECT_EQ(0, error_callback_count_);
3513 // One change for paired, and one for trusted.
3514 EXPECT_EQ(2, observer.device_changed_count());
3515 EXPECT_EQ(device, observer.last_device());
3517 EXPECT_TRUE(device->IsPaired());
3519 // Make sure the trusted property has been set to true.
3520 FakeBluetoothDeviceClient::Properties* properties =
3521 fake_bluetooth_device_client_->GetProperties(
3522 dbus::ObjectPath(FakeBluetoothDeviceClient::kJustWorksPath));
3523 ASSERT_TRUE(properties->trusted.value());
3525 // No pairing context should remain on the device.
3526 BluetoothDeviceChromeOS* device_chromeos =
3527 static_cast<BluetoothDeviceChromeOS*>(device);
3528 EXPECT_TRUE(device_chromeos->GetPairing() == nullptr);
3531 TEST_F(BluetoothChromeOSTest, IncomingPairRequestPinCodeWithoutDelegate) {
3532 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3534 GetAdapter();
3536 // Requires that we provide a PIN Code, without a pairing delegate,
3537 // that will be rejected.
3538 fake_bluetooth_device_client_->CreateDevice(
3539 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
3540 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath));
3541 BluetoothDevice* device = adapter_->GetDevice(
3542 FakeBluetoothDeviceClient::kRequestPinCodeAddress);
3543 ASSERT_TRUE(device != nullptr);
3544 ASSERT_FALSE(device->IsPaired());
3546 TestBluetoothAdapterObserver observer(adapter_);
3548 fake_bluetooth_device_client_->SimulatePairing(
3549 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath), true,
3550 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
3551 base::Unretained(this)));
3553 message_loop_.Run();
3555 EXPECT_EQ(0, callback_count_);
3556 EXPECT_EQ(1, error_callback_count_);
3557 EXPECT_EQ(bluetooth_device::kErrorAuthenticationRejected, last_client_error_);
3559 // No changes should be observer.
3560 EXPECT_EQ(0, observer.device_changed_count());
3562 EXPECT_FALSE(device->IsPaired());
3564 // No pairing context should remain on the device.
3565 BluetoothDeviceChromeOS* device_chromeos =
3566 static_cast<BluetoothDeviceChromeOS*>(device);
3567 EXPECT_TRUE(device_chromeos->GetPairing() == nullptr);
3570 TEST_F(BluetoothChromeOSTest, IncomingPairConfirmPasskeyWithoutDelegate) {
3571 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3573 GetAdapter();
3575 // Requests that we confirm a displayed passkey, without a pairing delegate,
3576 // that will be rejected.
3577 fake_bluetooth_device_client_->CreateDevice(
3578 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
3579 dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath));
3580 BluetoothDevice* device = adapter_->GetDevice(
3581 FakeBluetoothDeviceClient::kConfirmPasskeyAddress);
3582 ASSERT_TRUE(device != nullptr);
3583 ASSERT_FALSE(device->IsPaired());
3585 TestBluetoothAdapterObserver observer(adapter_);
3587 fake_bluetooth_device_client_->SimulatePairing(
3588 dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath), true,
3589 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
3590 base::Unretained(this)));
3592 message_loop_.Run();
3594 EXPECT_EQ(0, callback_count_);
3595 EXPECT_EQ(1, error_callback_count_);
3596 EXPECT_EQ(bluetooth_device::kErrorAuthenticationRejected, last_client_error_);
3598 // No changes should be observer.
3599 EXPECT_EQ(0, observer.device_changed_count());
3601 EXPECT_FALSE(device->IsPaired());
3603 // No pairing context should remain on the device.
3604 BluetoothDeviceChromeOS* device_chromeos =
3605 static_cast<BluetoothDeviceChromeOS*>(device);
3606 EXPECT_TRUE(device_chromeos->GetPairing() == nullptr);
3609 TEST_F(BluetoothChromeOSTest, IncomingPairRequestPasskeyWithoutDelegate) {
3610 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3612 GetAdapter();
3614 // Requests that we provide a displayed passkey, without a pairing delegate,
3615 // that will be rejected.
3616 fake_bluetooth_device_client_->CreateDevice(
3617 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
3618 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath));
3619 BluetoothDevice* device = adapter_->GetDevice(
3620 FakeBluetoothDeviceClient::kRequestPasskeyAddress);
3621 ASSERT_TRUE(device != nullptr);
3622 ASSERT_FALSE(device->IsPaired());
3624 TestBluetoothAdapterObserver observer(adapter_);
3626 fake_bluetooth_device_client_->SimulatePairing(
3627 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath), true,
3628 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
3629 base::Unretained(this)));
3631 message_loop_.Run();
3633 EXPECT_EQ(0, callback_count_);
3634 EXPECT_EQ(1, error_callback_count_);
3635 EXPECT_EQ(bluetooth_device::kErrorAuthenticationRejected, last_client_error_);
3637 // No changes should be observer.
3638 EXPECT_EQ(0, observer.device_changed_count());
3640 EXPECT_FALSE(device->IsPaired());
3642 // No pairing context should remain on the device.
3643 BluetoothDeviceChromeOS* device_chromeos =
3644 static_cast<BluetoothDeviceChromeOS*>(device);
3645 EXPECT_TRUE(device_chromeos->GetPairing() == nullptr);
3648 TEST_F(BluetoothChromeOSTest, IncomingPairJustWorksWithoutDelegate) {
3649 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3651 GetAdapter();
3653 // Uses just-works pairing and thus requires authorization for incoming
3654 // pairings, without a pairing delegate, that will be rejected.
3655 fake_bluetooth_device_client_->CreateDevice(
3656 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
3657 dbus::ObjectPath(FakeBluetoothDeviceClient::kJustWorksPath));
3658 BluetoothDevice* device = adapter_->GetDevice(
3659 FakeBluetoothDeviceClient::kJustWorksAddress);
3660 ASSERT_TRUE(device != nullptr);
3661 ASSERT_FALSE(device->IsPaired());
3663 TestBluetoothAdapterObserver observer(adapter_);
3665 fake_bluetooth_device_client_->SimulatePairing(
3666 dbus::ObjectPath(FakeBluetoothDeviceClient::kJustWorksPath), true,
3667 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
3668 base::Unretained(this)));
3670 message_loop_.Run();
3672 EXPECT_EQ(0, callback_count_);
3673 EXPECT_EQ(1, error_callback_count_);
3674 EXPECT_EQ(bluetooth_device::kErrorAuthenticationRejected, last_client_error_);
3676 // No changes should be observer.
3677 EXPECT_EQ(0, observer.device_changed_count());
3679 EXPECT_FALSE(device->IsPaired());
3681 // No pairing context should remain on the device.
3682 BluetoothDeviceChromeOS* device_chromeos =
3683 static_cast<BluetoothDeviceChromeOS*>(device);
3684 EXPECT_TRUE(device_chromeos->GetPairing() == nullptr);
3687 TEST_F(BluetoothChromeOSTest, RemovePairingDelegateDuringPairing) {
3688 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3690 GetAdapter();
3692 TestPairingDelegate pairing_delegate;
3693 adapter_->AddPairingDelegate(
3694 &pairing_delegate,
3695 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH);
3697 // Requests that we provide a Passkey.
3698 fake_bluetooth_device_client_->CreateDevice(
3699 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
3700 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath));
3701 BluetoothDevice* device = adapter_->GetDevice(
3702 FakeBluetoothDeviceClient::kRequestPasskeyAddress);
3703 ASSERT_TRUE(device != nullptr);
3704 ASSERT_FALSE(device->IsPaired());
3706 TestBluetoothAdapterObserver observer(adapter_);
3708 fake_bluetooth_device_client_->SimulatePairing(
3709 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath), true,
3710 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
3711 base::Unretained(this)));
3713 EXPECT_EQ(1, pairing_delegate.call_count_);
3714 EXPECT_EQ(1, pairing_delegate.request_passkey_count_);
3716 // A pairing context should now be set on the device.
3717 BluetoothDeviceChromeOS* device_chromeos =
3718 static_cast<BluetoothDeviceChromeOS*>(device);
3719 ASSERT_TRUE(device_chromeos->GetPairing() != nullptr);
3721 // Removing the pairing delegate should remove that pairing context.
3722 adapter_->RemovePairingDelegate(&pairing_delegate);
3724 EXPECT_TRUE(device_chromeos->GetPairing() == nullptr);
3726 // Set the Passkey, this should now have no effect since the pairing has
3727 // been, in-effect, cancelled
3728 device->SetPasskey(1234);
3730 EXPECT_EQ(0, callback_count_);
3731 EXPECT_EQ(0, error_callback_count_);
3732 EXPECT_EQ(0, observer.device_changed_count());
3734 EXPECT_FALSE(device->IsPaired());
3737 TEST_F(BluetoothChromeOSTest, DeviceId) {
3738 GetAdapter();
3740 // Use the built-in paired device for this test, grab its Properties
3741 // structure so we can adjust the underlying modalias property.
3742 BluetoothDevice* device = adapter_->GetDevice(
3743 FakeBluetoothDeviceClient::kPairedDeviceAddress);
3744 FakeBluetoothDeviceClient::Properties* properties =
3745 fake_bluetooth_device_client_->GetProperties(
3746 dbus::ObjectPath(FakeBluetoothDeviceClient::kPairedDevicePath));
3748 ASSERT_TRUE(device != nullptr);
3749 ASSERT_TRUE(properties != nullptr);
3751 // Valid USB IF-assigned identifier.
3752 ASSERT_EQ("usb:v05ACp030Dd0306", properties->modalias.value());
3754 EXPECT_EQ(BluetoothDevice::VENDOR_ID_USB, device->GetVendorIDSource());
3755 EXPECT_EQ(0x05ac, device->GetVendorID());
3756 EXPECT_EQ(0x030d, device->GetProductID());
3757 EXPECT_EQ(0x0306, device->GetDeviceID());
3759 // Valid Bluetooth SIG-assigned identifier.
3760 properties->modalias.ReplaceValue("bluetooth:v00E0p2400d0400");
3762 EXPECT_EQ(BluetoothDevice::VENDOR_ID_BLUETOOTH, device->GetVendorIDSource());
3763 EXPECT_EQ(0x00e0, device->GetVendorID());
3764 EXPECT_EQ(0x2400, device->GetProductID());
3765 EXPECT_EQ(0x0400, device->GetDeviceID());
3767 // Invalid USB IF-assigned identifier.
3768 properties->modalias.ReplaceValue("usb:x00E0p2400d0400");
3770 EXPECT_EQ(BluetoothDevice::VENDOR_ID_UNKNOWN, device->GetVendorIDSource());
3771 EXPECT_EQ(0, device->GetVendorID());
3772 EXPECT_EQ(0, device->GetProductID());
3773 EXPECT_EQ(0, device->GetDeviceID());
3775 // Invalid Bluetooth SIG-assigned identifier.
3776 properties->modalias.ReplaceValue("bluetooth:x00E0p2400d0400");
3778 EXPECT_EQ(BluetoothDevice::VENDOR_ID_UNKNOWN, device->GetVendorIDSource());
3779 EXPECT_EQ(0, device->GetVendorID());
3780 EXPECT_EQ(0, device->GetProductID());
3781 EXPECT_EQ(0, device->GetDeviceID());
3783 // Unknown vendor specification identifier.
3784 properties->modalias.ReplaceValue("chrome:v00E0p2400d0400");
3786 EXPECT_EQ(BluetoothDevice::VENDOR_ID_UNKNOWN, device->GetVendorIDSource());
3787 EXPECT_EQ(0, device->GetVendorID());
3788 EXPECT_EQ(0, device->GetProductID());
3789 EXPECT_EQ(0, device->GetDeviceID());
3792 TEST_F(BluetoothChromeOSTest, GetConnectionInfoForDisconnectedDevice) {
3793 GetAdapter();
3794 BluetoothDevice* device =
3795 adapter_->GetDevice(FakeBluetoothDeviceClient::kPairedDeviceAddress);
3797 // Calling GetConnectionInfo for an unconnected device should return a result
3798 // in which all fields are filled with BluetoothDevice::kUnknownPower.
3799 BluetoothDevice::ConnectionInfo conn_info(0, 0, 0);
3800 device->GetConnectionInfo(base::Bind(&SaveConnectionInfo, &conn_info));
3801 int unknown_power = BluetoothDevice::kUnknownPower;
3802 EXPECT_NE(0, unknown_power);
3803 EXPECT_EQ(unknown_power, conn_info.rssi);
3804 EXPECT_EQ(unknown_power, conn_info.transmit_power);
3805 EXPECT_EQ(unknown_power, conn_info.max_transmit_power);
3808 TEST_F(BluetoothChromeOSTest, GetConnectionInfoForConnectedDevice) {
3809 GetAdapter();
3810 BluetoothDevice* device =
3811 adapter_->GetDevice(FakeBluetoothDeviceClient::kPairedDeviceAddress);
3813 device->Connect(nullptr, GetCallback(),
3814 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
3815 base::Unretained(this)));
3816 EXPECT_TRUE(device->IsConnected());
3818 // Calling GetConnectionInfo for a connected device should return valid
3819 // results.
3820 fake_bluetooth_device_client_->UpdateConnectionInfo(-10, 3, 4);
3821 BluetoothDevice::ConnectionInfo conn_info;
3822 device->GetConnectionInfo(base::Bind(&SaveConnectionInfo, &conn_info));
3823 EXPECT_EQ(-10, conn_info.rssi);
3824 EXPECT_EQ(3, conn_info.transmit_power);
3825 EXPECT_EQ(4, conn_info.max_transmit_power);
3828 // Verifies Shutdown shuts down the adapter as expected.
3829 TEST_F(BluetoothChromeOSTest, Shutdown) {
3830 // Set up adapter. Set powered & discoverable, start discovery.
3831 GetAdapter();
3832 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
3833 adapter_->SetDiscoverable(true, GetCallback(), GetErrorCallback());
3834 adapter_->StartDiscoverySession(
3835 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
3836 base::Unretained(this)),
3837 GetErrorCallback());
3838 base::MessageLoop::current()->Run();
3839 ASSERT_EQ(3, callback_count_);
3840 ASSERT_EQ(0, error_callback_count_);
3841 callback_count_ = 0;
3843 TestPairingDelegate pairing_delegate;
3844 adapter_->AddPairingDelegate(
3845 &pairing_delegate, BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH);
3847 // Validate running adapter state.
3848 EXPECT_NE("", adapter_->GetAddress());
3849 EXPECT_NE("", adapter_->GetName());
3850 EXPECT_TRUE(adapter_->IsInitialized());
3851 EXPECT_TRUE(adapter_->IsPresent());
3852 EXPECT_TRUE(adapter_->IsPowered());
3853 EXPECT_TRUE(adapter_->IsDiscoverable());
3854 EXPECT_TRUE(adapter_->IsDiscovering());
3855 EXPECT_EQ(2U, adapter_->GetDevices().size());
3856 EXPECT_NE(nullptr, adapter_->GetDevice(
3857 FakeBluetoothDeviceClient::kPairedDeviceAddress));
3858 EXPECT_NE(dbus::ObjectPath(""), static_cast<BluetoothAdapterChromeOS*>(
3859 adapter_.get())->object_path());
3861 // Shutdown
3862 adapter_->Shutdown();
3864 // Validate post shutdown state by calling all BluetoothAdapterChromeOS
3865 // members, in declaration order:
3867 adapter_->Shutdown();
3868 // DeleteOnCorrectThread omitted as we don't want to delete in this test.
3870 TestBluetoothAdapterObserver observer(adapter_); // Calls AddObserver
3871 } // ~TestBluetoothAdapterObserver calls RemoveObserver.
3872 EXPECT_EQ("", adapter_->GetAddress());
3873 EXPECT_EQ("", adapter_->GetName());
3875 adapter_->SetName("", GetCallback(), GetErrorCallback());
3876 EXPECT_EQ(0, callback_count_);
3877 EXPECT_EQ(1, error_callback_count_--) << "SetName error";
3879 EXPECT_TRUE(adapter_->IsInitialized());
3880 EXPECT_FALSE(adapter_->IsPresent());
3881 EXPECT_FALSE(adapter_->IsPowered());
3883 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
3884 EXPECT_EQ(0, callback_count_);
3885 EXPECT_EQ(1, error_callback_count_--) << "SetPowered error";
3887 EXPECT_FALSE(adapter_->IsDiscoverable());
3889 adapter_->SetDiscoverable(true, GetCallback(), GetErrorCallback());
3890 EXPECT_EQ(0, callback_count_);
3891 EXPECT_EQ(1, error_callback_count_--) << "SetDiscoverable error";
3893 EXPECT_FALSE(adapter_->IsDiscovering());
3894 // CreateRfcommService will DCHECK after Shutdown().
3895 // CreateL2capService will DCHECK after Shutdown().
3897 BluetoothAudioSink::Options audio_sink_options;
3898 adapter_->RegisterAudioSink(
3899 audio_sink_options,
3900 base::Bind(&BluetoothChromeOSTest::AudioSinkAcquiredCallback,
3901 base::Unretained(this)),
3902 base::Bind(&BluetoothChromeOSTest::AudioSinkErrorCallback,
3903 base::Unretained(this)));
3904 EXPECT_EQ(0, callback_count_);
3905 EXPECT_EQ(1, error_callback_count_--) << "RegisterAudioSink error";
3907 BluetoothAdapterChromeOS* adapter_chrome_os =
3908 static_cast<BluetoothAdapterChromeOS*>(adapter_.get());
3909 EXPECT_EQ(nullptr,
3910 adapter_chrome_os->GetDeviceWithPath(dbus::ObjectPath("")));
3912 // Notify methods presume objects exist that are owned by the adapter and
3913 // destroyed in Shutdown(). Mocks are not attempted here that won't exist,
3914 // as verified below by EXPECT_EQ(0U, adapter_->GetDevices().size());
3915 // NotifyDeviceChanged
3916 // NotifyGattServiceAdded
3917 // NotifyGattServiceRemoved
3918 // NotifyGattServiceChanged
3919 // NotifyGattDiscoveryComplete
3920 // NotifyGattCharacteristicAdded
3921 // NotifyGattCharacteristicRemoved
3922 // NotifyGattDescriptorAdded
3923 // NotifyGattDescriptorRemoved
3924 // NotifyGattCharacteristicValueChanged
3925 // NotifyGattDescriptorValueChanged
3927 EXPECT_EQ(dbus::ObjectPath(""), adapter_chrome_os->object_path());
3929 adapter_profile_ = nullptr;
3931 FakeBluetoothProfileServiceProviderDelegate profile_delegate;
3932 adapter_chrome_os->UseProfile(
3933 BluetoothUUID(), dbus::ObjectPath(""),
3934 BluetoothProfileManagerClient::Options(), &profile_delegate,
3935 base::Bind(&BluetoothChromeOSTest::ProfileRegisteredCallback,
3936 base::Unretained(this)),
3937 base::Bind(&BluetoothChromeOSTest::ErrorCompletionCallback,
3938 base::Unretained(this)));
3940 EXPECT_FALSE(adapter_profile_) << "UseProfile error";
3941 EXPECT_EQ(0, callback_count_) << "UseProfile error";
3942 EXPECT_EQ(1, error_callback_count_--) << "UseProfile error";
3944 // Protected and private methods:
3946 adapter_chrome_os->RemovePairingDelegateInternal(&pairing_delegate);
3947 // AdapterAdded() invalid post Shutdown because it calls SetAdapter.
3948 adapter_chrome_os->AdapterRemoved(dbus::ObjectPath("x"));
3949 adapter_chrome_os->AdapterPropertyChanged(dbus::ObjectPath("x"), "");
3950 adapter_chrome_os->DeviceAdded(dbus::ObjectPath(""));
3951 adapter_chrome_os->DeviceRemoved(dbus::ObjectPath(""));
3952 adapter_chrome_os->DevicePropertyChanged(dbus::ObjectPath(""), "");
3953 adapter_chrome_os->InputPropertyChanged(dbus::ObjectPath(""), "");
3954 // BluetoothAgentServiceProvider::Delegate omitted, dbus will be shutdown,
3955 // with the exception of Released.
3956 adapter_chrome_os->Released();
3958 adapter_chrome_os->OnRegisterAgent();
3959 adapter_chrome_os->OnRegisterAgentError("", "");
3960 adapter_chrome_os->OnRequestDefaultAgent();
3961 adapter_chrome_os->OnRequestDefaultAgentError("", "");
3963 adapter_chrome_os->OnRegisterAudioSink(
3964 base::Bind(&BluetoothChromeOSTest::AudioSinkAcquiredCallback,
3965 base::Unretained(this)),
3966 base::Bind(&BluetoothChromeOSTest::AudioSinkErrorCallback,
3967 base::Unretained(this)),
3968 scoped_refptr<device::BluetoothAudioSink>());
3969 EXPECT_EQ(0, callback_count_);
3970 EXPECT_EQ(1, error_callback_count_--) << "RegisterAudioSink error";
3972 // GetPairing will DCHECK after Shutdown().
3973 // SetAdapter will DCHECK after Shutdown().
3974 // SetDefaultAdapterName will DCHECK after Shutdown().
3975 // RemoveAdapter will DCHECK after Shutdown().
3976 adapter_chrome_os->PoweredChanged(false);
3977 adapter_chrome_os->DiscoverableChanged(false);
3978 adapter_chrome_os->DiscoveringChanged(false);
3979 adapter_chrome_os->PresentChanged(false);
3981 adapter_chrome_os->OnSetDiscoverable(GetCallback(), GetErrorCallback(), true);
3982 EXPECT_EQ(0, callback_count_) << "OnSetDiscoverable error";
3983 EXPECT_EQ(1, error_callback_count_--) << "OnSetDiscoverable error";
3985 adapter_chrome_os->OnPropertyChangeCompleted(GetCallback(),
3986 GetErrorCallback(), true);
3987 EXPECT_EQ(0, callback_count_) << "OnPropertyChangeCompleted error";
3988 EXPECT_EQ(1, error_callback_count_--) << "OnPropertyChangeCompleted error";
3990 adapter_chrome_os->AddDiscoverySession(nullptr, GetCallback(),
3991 GetDiscoveryErrorCallback());
3992 EXPECT_EQ(0, callback_count_) << "AddDiscoverySession error";
3993 EXPECT_EQ(1, error_callback_count_--) << "AddDiscoverySession error";
3995 adapter_chrome_os->RemoveDiscoverySession(nullptr, GetCallback(),
3996 GetDiscoveryErrorCallback());
3997 EXPECT_EQ(0, callback_count_) << "RemoveDiscoverySession error";
3998 EXPECT_EQ(1, error_callback_count_--) << "RemoveDiscoverySession error";
4000 // OnStartDiscovery tested in Shutdown_OnStartDiscovery
4001 // OnStartDiscoveryError tested in Shutdown_OnStartDiscoveryError
4002 // OnStopDiscovery tested in Shutdown_OnStopDiscovery
4003 // OnStopDiscoveryError tested in Shutdown_OnStopDiscoveryError
4005 adapter_profile_ = nullptr;
4007 // OnRegisterProfile SetProfileDelegate, OnRegisterProfileError, require
4008 // UseProfile to be set first, do so again here just before calling them.
4009 adapter_chrome_os->UseProfile(
4010 BluetoothUUID(), dbus::ObjectPath(""),
4011 BluetoothProfileManagerClient::Options(), &profile_delegate,
4012 base::Bind(&BluetoothChromeOSTest::ProfileRegisteredCallback,
4013 base::Unretained(this)),
4014 base::Bind(&BluetoothChromeOSTest::ErrorCompletionCallback,
4015 base::Unretained(this)));
4017 EXPECT_FALSE(adapter_profile_) << "UseProfile error";
4018 EXPECT_EQ(0, callback_count_) << "UseProfile error";
4019 EXPECT_EQ(1, error_callback_count_--) << "UseProfile error";
4021 adapter_chrome_os->SetProfileDelegate(
4022 BluetoothUUID(), dbus::ObjectPath(""), &profile_delegate,
4023 base::Bind(&BluetoothChromeOSTest::ProfileRegisteredCallback,
4024 base::Unretained(this)),
4025 base::Bind(&BluetoothChromeOSTest::ErrorCompletionCallback,
4026 base::Unretained(this)));
4027 EXPECT_EQ(0, callback_count_) << "SetProfileDelegate error";
4028 EXPECT_EQ(1, error_callback_count_--) << "SetProfileDelegate error";
4030 adapter_chrome_os->OnRegisterProfileError(BluetoothUUID(), "", "");
4031 EXPECT_EQ(0, callback_count_) << "OnRegisterProfileError error";
4032 EXPECT_EQ(0, error_callback_count_) << "OnRegisterProfileError error";
4034 adapter_chrome_os->ProcessQueuedDiscoveryRequests();
4036 // From BluetoothAdapater:
4038 adapter_->StartDiscoverySession(
4039 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
4040 base::Unretained(this)),
4041 GetErrorCallback());
4042 EXPECT_EQ(0, callback_count_) << "StartDiscoverySession error";
4043 EXPECT_EQ(1, error_callback_count_--) << "StartDiscoverySession error";
4045 EXPECT_EQ(0U, adapter_->GetDevices().size());
4046 EXPECT_EQ(nullptr, adapter_->GetDevice(
4047 FakeBluetoothDeviceClient::kPairedDeviceAddress));
4048 TestPairingDelegate pairing_delegate2;
4049 adapter_->AddPairingDelegate(
4050 &pairing_delegate2, BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH);
4051 adapter_->RemovePairingDelegate(&pairing_delegate2);
4054 // Verifies post-Shutdown of discovery sessions and OnStartDiscovery.
4055 TEST_F(BluetoothChromeOSTest, Shutdown_OnStartDiscovery) {
4056 const int kNumberOfDiscoverySessions = 10;
4057 GetAdapter();
4058 BluetoothAdapterChromeOS* adapter_chrome_os =
4059 static_cast<BluetoothAdapterChromeOS*>(adapter_.get());
4061 for (int i = 0; i < kNumberOfDiscoverySessions; i++) {
4062 adapter_chrome_os->AddDiscoverySession(nullptr, GetCallback(),
4063 GetDiscoveryErrorCallback());
4065 adapter_->Shutdown();
4066 adapter_chrome_os->OnStartDiscovery(GetCallback(),
4067 GetDiscoveryErrorCallback());
4069 EXPECT_EQ(0, callback_count_);
4070 EXPECT_EQ(kNumberOfDiscoverySessions, error_callback_count_);
4073 // Verifies post-Shutdown of discovery sessions and OnStartDiscoveryError.
4074 TEST_F(BluetoothChromeOSTest, Shutdown_OnStartDiscoveryError) {
4075 const int kNumberOfDiscoverySessions = 10;
4076 GetAdapter();
4077 BluetoothAdapterChromeOS* adapter_chrome_os =
4078 static_cast<BluetoothAdapterChromeOS*>(adapter_.get());
4080 for (int i = 0; i < kNumberOfDiscoverySessions; i++) {
4081 adapter_chrome_os->AddDiscoverySession(nullptr, GetCallback(),
4082 GetDiscoveryErrorCallback());
4084 adapter_->Shutdown();
4085 adapter_chrome_os->OnStartDiscoveryError(GetCallback(),
4086 GetDiscoveryErrorCallback(), "", "");
4088 EXPECT_EQ(0, callback_count_);
4089 EXPECT_EQ(kNumberOfDiscoverySessions, error_callback_count_);
4092 // Verifies post-Shutdown of discovery sessions and OnStartDiscovery.
4093 TEST_F(BluetoothChromeOSTest, Shutdown_OnStopDiscovery) {
4094 const int kNumberOfDiscoverySessions = 10;
4095 GetAdapter();
4096 BluetoothAdapterChromeOS* adapter_chrome_os =
4097 static_cast<BluetoothAdapterChromeOS*>(adapter_.get());
4099 // In order to queue up discovery sessions before an OnStopDiscovery call
4100 // RemoveDiscoverySession must be called, so Add, Start, and Remove:
4101 adapter_chrome_os->AddDiscoverySession(nullptr, GetCallback(),
4102 GetDiscoveryErrorCallback());
4103 adapter_chrome_os->OnStartDiscovery(GetCallback(),
4104 GetDiscoveryErrorCallback());
4105 adapter_chrome_os->RemoveDiscoverySession(nullptr, GetCallback(),
4106 GetDiscoveryErrorCallback());
4107 callback_count_ = 0;
4108 error_callback_count_ = 0;
4109 // Can now queue discovery sessions while waiting for OnStopDiscovery.
4110 for (int i = 0; i < kNumberOfDiscoverySessions; i++) {
4111 adapter_chrome_os->AddDiscoverySession(nullptr, GetCallback(),
4112 GetDiscoveryErrorCallback());
4114 adapter_->Shutdown();
4115 adapter_chrome_os->OnStopDiscovery(GetCallback());
4117 // 1 successful stopped discovery from RemoveDiscoverySession, and
4118 // kNumberOfDiscoverySessions errors from AddDiscoverySession/OnStopDiscovery.
4119 EXPECT_EQ(1, callback_count_);
4120 EXPECT_EQ(kNumberOfDiscoverySessions, error_callback_count_);
4123 // Verifies post-Shutdown of discovery sessions and OnStopDiscoveryError.
4124 TEST_F(BluetoothChromeOSTest, Shutdown_OnStopDiscoveryError) {
4125 const int kNumberOfDiscoverySessions = 10;
4126 GetAdapter();
4127 BluetoothAdapterChromeOS* adapter_chrome_os =
4128 static_cast<BluetoothAdapterChromeOS*>(adapter_.get());
4130 // In order to queue up discovery sessions before an OnStopDiscoveryError call
4131 // RemoveDiscoverySession must be called, so Add, Start, and Remove:
4132 adapter_chrome_os->AddDiscoverySession(nullptr, GetCallback(),
4133 GetDiscoveryErrorCallback());
4134 adapter_chrome_os->OnStartDiscovery(GetCallback(),
4135 GetDiscoveryErrorCallback());
4136 adapter_chrome_os->RemoveDiscoverySession(nullptr, GetCallback(),
4137 GetDiscoveryErrorCallback());
4138 callback_count_ = 0;
4139 error_callback_count_ = 0;
4140 // Can now queue discovery sessions while waiting for OnStopDiscoveryError.
4141 for (int i = 0; i < kNumberOfDiscoverySessions; i++) {
4142 adapter_chrome_os->AddDiscoverySession(nullptr, GetCallback(),
4143 GetDiscoveryErrorCallback());
4145 adapter_->Shutdown();
4146 adapter_chrome_os->OnStopDiscoveryError(GetDiscoveryErrorCallback(), "", "");
4148 // 1 error reported to RemoveDiscoverySession because of OnStopDiscoveryError,
4149 // and kNumberOfDiscoverySessions errors queued with AddDiscoverySession.
4150 EXPECT_EQ(0, callback_count_);
4151 EXPECT_EQ(1 + kNumberOfDiscoverySessions, error_callback_count_);
4154 } // namespace chromeos