Remove unnecessary message_loop_proxy.h includes
[chromium-blink-merge.git] / device / bluetooth / bluetooth_chromeos_unittest.cc
blobad19d8b5c57e3bb7fc4b77a70e1fd95c1b3ebb4a
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();
152 class BluetoothChromeOSTest : public testing::Test {
153 public:
154 void SetUp() override {
155 scoped_ptr<DBusThreadManagerSetter> dbus_setter =
156 chromeos::DBusThreadManager::GetSetterForTesting();
157 // We need to initialize DBusThreadManager early to prevent
158 // Bluetooth*::Create() methods from picking the real instead of fake
159 // implementations.
160 fake_bluetooth_adapter_client_ = new FakeBluetoothAdapterClient;
161 dbus_setter->SetBluetoothAdapterClient(
162 scoped_ptr<BluetoothAdapterClient>(fake_bluetooth_adapter_client_));
163 fake_bluetooth_device_client_ = new FakeBluetoothDeviceClient;
164 dbus_setter->SetBluetoothDeviceClient(
165 scoped_ptr<BluetoothDeviceClient>(fake_bluetooth_device_client_));
166 dbus_setter->SetBluetoothInputClient(
167 scoped_ptr<BluetoothInputClient>(new FakeBluetoothInputClient));
168 dbus_setter->SetBluetoothAgentManagerClient(
169 scoped_ptr<BluetoothAgentManagerClient>(
170 new FakeBluetoothAgentManagerClient));
171 dbus_setter->SetBluetoothGattServiceClient(
172 scoped_ptr<BluetoothGattServiceClient>(
173 new FakeBluetoothGattServiceClient));
175 fake_bluetooth_adapter_client_->SetSimulationIntervalMs(10);
177 callback_count_ = 0;
178 error_callback_count_ = 0;
179 last_connect_error_ = BluetoothDevice::ERROR_UNKNOWN;
180 last_client_error_ = "";
183 void TearDown() override {
184 for (ScopedVector<BluetoothDiscoverySession>::iterator iter =
185 discovery_sessions_.begin();
186 iter != discovery_sessions_.end();
187 ++iter) {
188 BluetoothDiscoverySession* session = *iter;
189 if (!session->IsActive())
190 continue;
191 callback_count_ = 0;
192 session->Stop(GetCallback(), GetErrorCallback());
193 message_loop_.Run();
194 ASSERT_EQ(1, callback_count_);
196 discovery_sessions_.clear();
197 adapter_ = NULL;
198 DBusThreadManager::Shutdown();
201 // Generic callbacks
202 void Callback() {
203 ++callback_count_;
204 QuitMessageLoop();
207 base::Closure GetCallback() {
208 return base::Bind(&BluetoothChromeOSTest::Callback, base::Unretained(this));
211 void DiscoverySessionCallback(
212 scoped_ptr<BluetoothDiscoverySession> discovery_session) {
213 ++callback_count_;
214 discovery_sessions_.push_back(discovery_session.release());
215 QuitMessageLoop();
218 void AudioSinkAcquiredCallback(scoped_refptr<BluetoothAudioSink>) {
219 ++callback_count_;
220 QuitMessageLoop();
223 void ProfileRegisteredCallback(BluetoothAdapterProfileChromeOS* profile) {
224 adapter_profile_ = profile;
225 ++callback_count_;
226 QuitMessageLoop();
229 void ErrorCallback() {
230 ++error_callback_count_;
231 QuitMessageLoop();
234 base::Closure GetErrorCallback() {
235 return base::Bind(&BluetoothChromeOSTest::ErrorCallback,
236 base::Unretained(this));
239 void DBusErrorCallback(const std::string& error_name,
240 const std::string& error_message) {
241 ++error_callback_count_;
242 last_client_error_ = error_name;
243 QuitMessageLoop();
246 void ConnectErrorCallback(BluetoothDevice::ConnectErrorCode error) {
247 ++error_callback_count_;
248 last_connect_error_ = error;
251 void AudioSinkErrorCallback(BluetoothAudioSink::ErrorCode) {
252 ++error_callback_count_;
253 QuitMessageLoop();
256 void ErrorCompletionCallback(const std::string& error_message) {
257 ++error_callback_count_;
258 QuitMessageLoop();
261 // Call to fill the adapter_ member with a BluetoothAdapter instance.
262 void GetAdapter() {
263 adapter_ = new BluetoothAdapterChromeOS();
264 ASSERT_TRUE(adapter_.get() != NULL);
265 ASSERT_TRUE(adapter_->IsInitialized());
268 // Run a discovery phase until the named device is detected, or if the named
269 // device is not created, the discovery process ends without finding it.
271 // The correct behavior of discovery is tested by the "Discovery" test case
272 // without using this function.
273 void DiscoverDevice(const std::string& address) {
274 ASSERT_TRUE(adapter_.get() != NULL);
275 ASSERT_TRUE(base::MessageLoop::current() != NULL);
276 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
278 TestBluetoothAdapterObserver observer(adapter_);
280 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
281 adapter_->StartDiscoverySession(
282 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
283 base::Unretained(this)),
284 GetErrorCallback());
285 base::MessageLoop::current()->Run();
286 ASSERT_EQ(2, callback_count_);
287 ASSERT_EQ(0, error_callback_count_);
288 ASSERT_EQ((size_t)1, discovery_sessions_.size());
289 ASSERT_TRUE(discovery_sessions_[0]->IsActive());
290 callback_count_ = 0;
292 ASSERT_TRUE(adapter_->IsPowered());
293 ASSERT_TRUE(adapter_->IsDiscovering());
295 while (!observer.device_removed_count() &&
296 observer.last_device_address() != address)
297 base::MessageLoop::current()->Run();
299 discovery_sessions_[0]->Stop(GetCallback(), GetErrorCallback());
300 base::MessageLoop::current()->Run();
301 ASSERT_EQ(1, callback_count_);
302 ASSERT_EQ(0, error_callback_count_);
303 callback_count_ = 0;
305 ASSERT_FALSE(adapter_->IsDiscovering());
308 // Run a discovery phase so we have devices that can be paired with.
309 void DiscoverDevices() {
310 // Pass an invalid address for the device so that the discovery process
311 // completes with all devices.
312 DiscoverDevice("does not exist");
315 protected:
316 base::MessageLoop message_loop_;
317 FakeBluetoothAdapterClient* fake_bluetooth_adapter_client_;
318 FakeBluetoothDeviceClient* fake_bluetooth_device_client_;
319 scoped_refptr<BluetoothAdapter> adapter_;
321 int callback_count_;
322 int error_callback_count_;
323 enum BluetoothDevice::ConnectErrorCode last_connect_error_;
324 std::string last_client_error_;
325 ScopedVector<BluetoothDiscoverySession> discovery_sessions_;
326 BluetoothAdapterProfileChromeOS* adapter_profile_;
328 private:
329 // Some tests use a message loop since background processing is simulated;
330 // break out of those loops.
331 void QuitMessageLoop() {
332 if (base::MessageLoop::current() &&
333 base::MessageLoop::current()->is_running())
334 base::MessageLoop::current()->Quit();
338 TEST_F(BluetoothChromeOSTest, AlreadyPresent) {
339 GetAdapter();
341 // This verifies that the class gets the list of adapters when created;
342 // and initializes with an existing adapter if there is one.
343 EXPECT_TRUE(adapter_->IsPresent());
344 EXPECT_FALSE(adapter_->IsPowered());
345 EXPECT_EQ(FakeBluetoothAdapterClient::kAdapterAddress,
346 adapter_->GetAddress());
347 EXPECT_FALSE(adapter_->IsDiscovering());
349 // There should be a device
350 BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
351 EXPECT_EQ(2U, devices.size());
352 EXPECT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
353 devices[0]->GetAddress());
354 EXPECT_EQ(FakeBluetoothDeviceClient::kPairedUnconnectableDeviceAddress,
355 devices[1]->GetAddress());
358 TEST_F(BluetoothChromeOSTest, BecomePresent) {
359 fake_bluetooth_adapter_client_->SetVisible(false);
360 GetAdapter();
361 ASSERT_FALSE(adapter_->IsPresent());
363 // Install an observer; expect the AdapterPresentChanged to be called
364 // with true, and IsPresent() to return true.
365 TestBluetoothAdapterObserver observer(adapter_);
367 fake_bluetooth_adapter_client_->SetVisible(true);
369 EXPECT_EQ(1, observer.present_changed_count());
370 EXPECT_TRUE(observer.last_present());
372 EXPECT_TRUE(adapter_->IsPresent());
374 // We should have had a device announced.
375 EXPECT_EQ(2, observer.device_added_count());
376 EXPECT_EQ(FakeBluetoothDeviceClient::kPairedUnconnectableDeviceAddress,
377 observer.last_device_address());
379 // Other callbacks shouldn't be called if the values are false.
380 EXPECT_EQ(0, observer.powered_changed_count());
381 EXPECT_EQ(0, observer.discovering_changed_count());
382 EXPECT_FALSE(adapter_->IsPowered());
383 EXPECT_FALSE(adapter_->IsDiscovering());
386 TEST_F(BluetoothChromeOSTest, BecomeNotPresent) {
387 GetAdapter();
388 ASSERT_TRUE(adapter_->IsPresent());
390 // Install an observer; expect the AdapterPresentChanged to be called
391 // with false, and IsPresent() to return false.
392 TestBluetoothAdapterObserver observer(adapter_);
394 fake_bluetooth_adapter_client_->SetVisible(false);
396 EXPECT_EQ(1, observer.present_changed_count());
397 EXPECT_FALSE(observer.last_present());
399 EXPECT_FALSE(adapter_->IsPresent());
401 // We should have had a device removed.
402 EXPECT_EQ(2, observer.device_removed_count());
403 EXPECT_EQ(FakeBluetoothDeviceClient::kPairedUnconnectableDeviceAddress,
404 observer.last_device_address());
406 // Other callbacks shouldn't be called since the values are false.
407 EXPECT_EQ(0, observer.powered_changed_count());
408 EXPECT_EQ(0, observer.discovering_changed_count());
409 EXPECT_FALSE(adapter_->IsPowered());
410 EXPECT_FALSE(adapter_->IsDiscovering());
413 TEST_F(BluetoothChromeOSTest, SecondAdapter) {
414 GetAdapter();
415 ASSERT_TRUE(adapter_->IsPresent());
417 // Install an observer, then add a second adapter. Nothing should change,
418 // we ignore the second adapter.
419 TestBluetoothAdapterObserver observer(adapter_);
421 fake_bluetooth_adapter_client_->SetSecondVisible(true);
423 EXPECT_EQ(0, observer.present_changed_count());
425 EXPECT_TRUE(adapter_->IsPresent());
426 EXPECT_EQ(FakeBluetoothAdapterClient::kAdapterAddress,
427 adapter_->GetAddress());
429 // Try removing the first adapter, we should now act as if the adapter
430 // is no longer present rather than fall back to the second.
431 fake_bluetooth_adapter_client_->SetVisible(false);
433 EXPECT_EQ(1, observer.present_changed_count());
434 EXPECT_FALSE(observer.last_present());
436 EXPECT_FALSE(adapter_->IsPresent());
438 // We should have had a device removed.
439 EXPECT_EQ(2, observer.device_removed_count());
440 EXPECT_EQ(FakeBluetoothDeviceClient::kPairedUnconnectableDeviceAddress,
441 observer.last_device_address());
443 // Other callbacks shouldn't be called since the values are false.
444 EXPECT_EQ(0, observer.powered_changed_count());
445 EXPECT_EQ(0, observer.discovering_changed_count());
446 EXPECT_FALSE(adapter_->IsPowered());
447 EXPECT_FALSE(adapter_->IsDiscovering());
449 observer.Reset();
451 // Removing the second adapter shouldn't set anything either.
452 fake_bluetooth_adapter_client_->SetSecondVisible(false);
454 EXPECT_EQ(0, observer.device_removed_count());
455 EXPECT_EQ(0, observer.powered_changed_count());
456 EXPECT_EQ(0, observer.discovering_changed_count());
459 TEST_F(BluetoothChromeOSTest, BecomePowered) {
460 GetAdapter();
461 ASSERT_FALSE(adapter_->IsPowered());
463 // Install an observer; expect the AdapterPoweredChanged to be called
464 // with true, and IsPowered() to return true.
465 TestBluetoothAdapterObserver observer(adapter_);
467 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
468 EXPECT_EQ(1, callback_count_);
469 EXPECT_EQ(0, error_callback_count_);
471 EXPECT_EQ(1, observer.powered_changed_count());
472 EXPECT_TRUE(observer.last_powered());
474 EXPECT_TRUE(adapter_->IsPowered());
477 TEST_F(BluetoothChromeOSTest, BecomeNotPowered) {
478 GetAdapter();
479 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
480 EXPECT_EQ(1, callback_count_);
481 EXPECT_EQ(0, error_callback_count_);
482 callback_count_ = 0;
484 ASSERT_TRUE(adapter_->IsPowered());
486 // Install an observer; expect the AdapterPoweredChanged to be called
487 // with false, and IsPowered() to return false.
488 TestBluetoothAdapterObserver observer(adapter_);
490 adapter_->SetPowered(false, GetCallback(), GetErrorCallback());
491 EXPECT_EQ(1, callback_count_);
492 EXPECT_EQ(0, error_callback_count_);
494 EXPECT_EQ(1, observer.powered_changed_count());
495 EXPECT_FALSE(observer.last_powered());
497 EXPECT_FALSE(adapter_->IsPowered());
500 TEST_F(BluetoothChromeOSTest, SetPoweredWhenNotPresent) {
501 GetAdapter();
502 ASSERT_TRUE(adapter_->IsPresent());
504 // Install an observer; expect the AdapterPresentChanged to be called
505 // with false, and IsPresent() to return false.
506 TestBluetoothAdapterObserver observer(adapter_);
508 fake_bluetooth_adapter_client_->SetVisible(false);
510 EXPECT_EQ(1, observer.present_changed_count());
511 EXPECT_FALSE(observer.last_present());
513 EXPECT_FALSE(adapter_->IsPresent());
514 EXPECT_FALSE(adapter_->IsPowered());
516 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
517 EXPECT_EQ(0, callback_count_);
518 EXPECT_EQ(1, error_callback_count_);
520 EXPECT_EQ(0, observer.powered_changed_count());
521 EXPECT_FALSE(observer.last_powered());
523 EXPECT_FALSE(adapter_->IsPowered());
526 TEST_F(BluetoothChromeOSTest, ChangeAdapterName) {
527 GetAdapter();
529 static const std::string new_name(".__.");
531 adapter_->SetName(new_name, GetCallback(), GetErrorCallback());
532 EXPECT_EQ(1, callback_count_);
533 EXPECT_EQ(0, error_callback_count_);
535 EXPECT_EQ(new_name, adapter_->GetName());
538 TEST_F(BluetoothChromeOSTest, ChangeAdapterNameWhenNotPresent) {
539 GetAdapter();
540 ASSERT_TRUE(adapter_->IsPresent());
542 // Install an observer; expect the AdapterPresentChanged to be called
543 // with false, and IsPresent() to return false.
544 TestBluetoothAdapterObserver observer(adapter_);
546 fake_bluetooth_adapter_client_->SetVisible(false);
548 EXPECT_EQ(1, observer.present_changed_count());
549 EXPECT_FALSE(observer.last_present());
551 EXPECT_FALSE(adapter_->IsPresent());
552 EXPECT_FALSE(adapter_->IsPowered());
554 adapter_->SetName("^o^", GetCallback(), GetErrorCallback());
555 EXPECT_EQ(0, callback_count_);
556 EXPECT_EQ(1, error_callback_count_);
558 EXPECT_EQ("", adapter_->GetName());
561 TEST_F(BluetoothChromeOSTest, BecomeDiscoverable) {
562 GetAdapter();
563 ASSERT_FALSE(adapter_->IsDiscoverable());
565 // Install an observer; expect the AdapterDiscoverableChanged to be called
566 // with true, and IsDiscoverable() to return true.
567 TestBluetoothAdapterObserver observer(adapter_);
569 adapter_->SetDiscoverable(true, GetCallback(), GetErrorCallback());
570 EXPECT_EQ(1, callback_count_);
571 EXPECT_EQ(0, error_callback_count_);
573 EXPECT_EQ(1, observer.discoverable_changed_count());
575 EXPECT_TRUE(adapter_->IsDiscoverable());
578 TEST_F(BluetoothChromeOSTest, BecomeNotDiscoverable) {
579 GetAdapter();
580 adapter_->SetDiscoverable(true, GetCallback(), GetErrorCallback());
581 EXPECT_EQ(1, callback_count_);
582 EXPECT_EQ(0, error_callback_count_);
583 callback_count_ = 0;
585 ASSERT_TRUE(adapter_->IsDiscoverable());
587 // Install an observer; expect the AdapterDiscoverableChanged to be called
588 // with false, and IsDiscoverable() to return false.
589 TestBluetoothAdapterObserver observer(adapter_);
591 adapter_->SetDiscoverable(false, GetCallback(), GetErrorCallback());
592 EXPECT_EQ(1, callback_count_);
593 EXPECT_EQ(0, error_callback_count_);
595 EXPECT_EQ(1, observer.discoverable_changed_count());
597 EXPECT_FALSE(adapter_->IsDiscoverable());
600 TEST_F(BluetoothChromeOSTest, SetDiscoverableWhenNotPresent) {
601 GetAdapter();
602 ASSERT_TRUE(adapter_->IsPresent());
603 ASSERT_FALSE(adapter_->IsDiscoverable());
605 // Install an observer; expect the AdapterDiscoverableChanged to be called
606 // with true, and IsDiscoverable() to return true.
607 TestBluetoothAdapterObserver observer(adapter_);
609 fake_bluetooth_adapter_client_->SetVisible(false);
611 EXPECT_EQ(1, observer.present_changed_count());
612 EXPECT_FALSE(observer.last_present());
614 EXPECT_FALSE(adapter_->IsPresent());
615 EXPECT_FALSE(adapter_->IsDiscoverable());
617 adapter_->SetDiscoverable(true, GetCallback(), GetErrorCallback());
618 EXPECT_EQ(0, callback_count_);
619 EXPECT_EQ(1, error_callback_count_);
621 EXPECT_EQ(0, observer.discoverable_changed_count());
623 EXPECT_FALSE(adapter_->IsDiscoverable());
626 TEST_F(BluetoothChromeOSTest, StopDiscovery) {
627 GetAdapter();
629 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
630 adapter_->StartDiscoverySession(
631 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
632 base::Unretained(this)),
633 GetErrorCallback());
634 message_loop_.Run();
635 EXPECT_EQ(2, callback_count_);
636 EXPECT_EQ(0, error_callback_count_);
637 callback_count_ = 0;
639 ASSERT_TRUE(adapter_->IsPowered());
640 ASSERT_TRUE(adapter_->IsDiscovering());
641 ASSERT_EQ((size_t)1, discovery_sessions_.size());
642 ASSERT_TRUE(discovery_sessions_[0]->IsActive());
644 // Install an observer; aside from the callback, expect the
645 // AdapterDiscoveringChanged method to be called and no longer to be
646 // discovering,
647 TestBluetoothAdapterObserver observer(adapter_);
649 discovery_sessions_[0]->Stop(GetCallback(), GetErrorCallback());
650 message_loop_.Run();
651 EXPECT_EQ(1, callback_count_);
652 EXPECT_EQ(0, error_callback_count_);
654 EXPECT_EQ(1, observer.discovering_changed_count());
655 EXPECT_FALSE(observer.last_discovering());
657 EXPECT_FALSE(adapter_->IsDiscovering());
658 discovery_sessions_.clear();
659 callback_count_ = 0;
661 // Test that the Stop callbacks get called even if the
662 // BluetoothDiscoverySession objects gets deleted
663 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
664 adapter_->StartDiscoverySession(
665 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
666 base::Unretained(this)),
667 GetErrorCallback());
668 message_loop_.Run();
669 EXPECT_EQ(2, callback_count_);
670 EXPECT_EQ(0, error_callback_count_);
671 callback_count_ = 0;
672 ASSERT_TRUE(adapter_->IsPowered());
673 ASSERT_TRUE(adapter_->IsDiscovering());
674 ASSERT_EQ((size_t)1, discovery_sessions_.size());
675 ASSERT_TRUE(discovery_sessions_[0]->IsActive());
677 discovery_sessions_[0]->Stop(GetCallback(), GetErrorCallback());
678 discovery_sessions_.clear();
680 message_loop_.Run();
681 EXPECT_EQ(1, callback_count_);
682 EXPECT_EQ(0, error_callback_count_);
685 TEST_F(BluetoothChromeOSTest, Discovery) {
686 // Test a simulated discovery session.
687 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
688 GetAdapter();
690 TestBluetoothAdapterObserver observer(adapter_);
692 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
693 adapter_->StartDiscoverySession(
694 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
695 base::Unretained(this)),
696 GetErrorCallback());
697 message_loop_.Run();
698 EXPECT_EQ(2, callback_count_);
699 EXPECT_EQ(0, error_callback_count_);
700 callback_count_ = 0;
702 ASSERT_TRUE(adapter_->IsPowered());
703 ASSERT_TRUE(adapter_->IsDiscovering());
704 ASSERT_EQ((size_t)1, discovery_sessions_.size());
705 ASSERT_TRUE(discovery_sessions_[0]->IsActive());
707 // First two devices to appear.
708 message_loop_.Run();
710 EXPECT_EQ(2, observer.device_added_count());
711 EXPECT_EQ(FakeBluetoothDeviceClient::kLowEnergyAddress,
712 observer.last_device_address());
714 // Next we should get another two devices...
715 message_loop_.Run();
716 EXPECT_EQ(4, observer.device_added_count());
718 // Okay, let's run forward until a device is actually removed...
719 while (!observer.device_removed_count())
720 message_loop_.Run();
722 EXPECT_EQ(1, observer.device_removed_count());
723 EXPECT_EQ(FakeBluetoothDeviceClient::kVanishingDeviceAddress,
724 observer.last_device_address());
727 TEST_F(BluetoothChromeOSTest, PoweredAndDiscovering) {
728 GetAdapter();
729 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
730 adapter_->StartDiscoverySession(
731 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
732 base::Unretained(this)),
733 GetErrorCallback());
734 message_loop_.Run();
735 EXPECT_EQ(2, callback_count_);
736 EXPECT_EQ(0, error_callback_count_);
737 callback_count_ = 0;
738 ASSERT_EQ((size_t)1, discovery_sessions_.size());
739 ASSERT_TRUE(discovery_sessions_[0]->IsActive());
741 // Stop the timers that the simulation uses
742 fake_bluetooth_device_client_->EndDiscoverySimulation(
743 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath));
745 ASSERT_TRUE(adapter_->IsPowered());
746 ASSERT_TRUE(adapter_->IsDiscovering());
748 fake_bluetooth_adapter_client_->SetVisible(false);
749 ASSERT_FALSE(adapter_->IsPresent());
750 ASSERT_FALSE(discovery_sessions_[0]->IsActive());
752 // Install an observer; expect the AdapterPresentChanged,
753 // AdapterPoweredChanged and AdapterDiscoveringChanged methods to be called
754 // with true, and IsPresent(), IsPowered() and IsDiscovering() to all
755 // return true.
756 TestBluetoothAdapterObserver observer(adapter_);
758 fake_bluetooth_adapter_client_->SetVisible(true);
760 EXPECT_EQ(1, observer.present_changed_count());
761 EXPECT_TRUE(observer.last_present());
762 EXPECT_TRUE(adapter_->IsPresent());
764 EXPECT_EQ(1, observer.powered_changed_count());
765 EXPECT_TRUE(observer.last_powered());
766 EXPECT_TRUE(adapter_->IsPowered());
768 EXPECT_EQ(1, observer.discovering_changed_count());
769 EXPECT_TRUE(observer.last_discovering());
770 EXPECT_TRUE(adapter_->IsDiscovering());
772 observer.Reset();
774 // Now mark the adapter not present again. Expect the methods to be called
775 // again, to reset the properties back to false
776 fake_bluetooth_adapter_client_->SetVisible(false);
778 EXPECT_EQ(1, observer.present_changed_count());
779 EXPECT_FALSE(observer.last_present());
780 EXPECT_FALSE(adapter_->IsPresent());
782 EXPECT_EQ(1, observer.powered_changed_count());
783 EXPECT_FALSE(observer.last_powered());
784 EXPECT_FALSE(adapter_->IsPowered());
786 EXPECT_EQ(1, observer.discovering_changed_count());
787 EXPECT_FALSE(observer.last_discovering());
788 EXPECT_FALSE(adapter_->IsDiscovering());
791 // This unit test asserts that the basic reference counting logic works
792 // correctly for discovery requests done via the BluetoothAdapter.
793 TEST_F(BluetoothChromeOSTest, MultipleDiscoverySessions) {
794 GetAdapter();
795 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
796 EXPECT_EQ(1, callback_count_);
797 EXPECT_EQ(0, error_callback_count_);
798 EXPECT_TRUE(adapter_->IsPowered());
799 callback_count_ = 0;
801 TestBluetoothAdapterObserver observer(adapter_);
803 EXPECT_EQ(0, observer.discovering_changed_count());
804 EXPECT_FALSE(observer.last_discovering());
805 EXPECT_FALSE(adapter_->IsDiscovering());
807 // Request device discovery 3 times.
808 for (int i = 0; i < 3; i++) {
809 adapter_->StartDiscoverySession(
810 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
811 base::Unretained(this)),
812 GetErrorCallback());
814 // Run only once, as there should have been one D-Bus call.
815 message_loop_.Run();
817 // The observer should have received the discovering changed event exactly
818 // once, the success callback should have been called 3 times and the adapter
819 // should be discovering.
820 EXPECT_EQ(1, observer.discovering_changed_count());
821 EXPECT_EQ(3, callback_count_);
822 EXPECT_EQ(0, error_callback_count_);
823 EXPECT_TRUE(observer.last_discovering());
824 EXPECT_TRUE(adapter_->IsDiscovering());
825 ASSERT_EQ((size_t)3, discovery_sessions_.size());
827 // Request to stop discovery twice.
828 for (int i = 0; i < 2; i++) {
829 discovery_sessions_[i]->Stop(GetCallback(), GetErrorCallback());
832 // The observer should have received no additional discovering changed events,
833 // the success callback should have been called 2 times and the adapter should
834 // still be discovering.
835 EXPECT_EQ(1, observer.discovering_changed_count());
836 EXPECT_EQ(5, callback_count_);
837 EXPECT_EQ(0, error_callback_count_);
838 EXPECT_TRUE(observer.last_discovering());
839 EXPECT_TRUE(adapter_->IsDiscovering());
840 EXPECT_TRUE(adapter_->IsDiscovering());
841 EXPECT_FALSE(discovery_sessions_[0]->IsActive());
842 EXPECT_FALSE(discovery_sessions_[1]->IsActive());
843 EXPECT_TRUE(discovery_sessions_[2]->IsActive());
845 // Request device discovery 3 times.
846 for (int i = 0; i < 3; i++) {
847 adapter_->StartDiscoverySession(
848 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
849 base::Unretained(this)),
850 GetErrorCallback());
853 // The observer should have received no additional discovering changed events,
854 // the success callback should have been called 3 times and the adapter should
855 // still be discovering.
856 EXPECT_EQ(1, observer.discovering_changed_count());
857 EXPECT_EQ(8, callback_count_);
858 EXPECT_EQ(0, error_callback_count_);
859 EXPECT_TRUE(observer.last_discovering());
860 EXPECT_TRUE(adapter_->IsDiscovering());
861 ASSERT_EQ((size_t)6, discovery_sessions_.size());
863 // Request to stop discovery 4 times.
864 for (int i = 2; i < 6; i++) {
865 discovery_sessions_[i]->Stop(GetCallback(), GetErrorCallback());
867 // Run only once, as there should have been one D-Bus call.
868 message_loop_.Run();
870 // The observer should have received the discovering changed event exactly
871 // once, the success callback should have been called 4 times and the adapter
872 // should no longer be discovering.
873 EXPECT_EQ(2, observer.discovering_changed_count());
874 EXPECT_EQ(12, callback_count_);
875 EXPECT_EQ(0, error_callback_count_);
876 EXPECT_FALSE(observer.last_discovering());
877 EXPECT_FALSE(adapter_->IsDiscovering());
879 // All discovery sessions should be inactive.
880 for (int i = 0; i < 6; i++)
881 EXPECT_FALSE(discovery_sessions_[i]->IsActive());
883 // Request to stop discovery on of the inactive sessions.
884 discovery_sessions_[0]->Stop(GetCallback(), GetErrorCallback());
886 // The call should have failed.
887 EXPECT_EQ(2, observer.discovering_changed_count());
888 EXPECT_EQ(12, callback_count_);
889 EXPECT_EQ(1, error_callback_count_);
890 EXPECT_FALSE(observer.last_discovering());
891 EXPECT_FALSE(adapter_->IsDiscovering());
894 // This unit test asserts that the reference counting logic works correctly in
895 // the cases when the adapter gets reset and D-Bus calls are made outside of
896 // the BluetoothAdapter.
897 TEST_F(BluetoothChromeOSTest,
898 UnexpectedChangesDuringMultipleDiscoverySessions) {
899 GetAdapter();
900 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
901 EXPECT_EQ(1, callback_count_);
902 EXPECT_EQ(0, error_callback_count_);
903 EXPECT_TRUE(adapter_->IsPowered());
904 callback_count_ = 0;
906 TestBluetoothAdapterObserver observer(adapter_);
908 EXPECT_EQ(0, observer.discovering_changed_count());
909 EXPECT_FALSE(observer.last_discovering());
910 EXPECT_FALSE(adapter_->IsDiscovering());
912 // Request device discovery 3 times.
913 for (int i = 0; i < 3; i++) {
914 adapter_->StartDiscoverySession(
915 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
916 base::Unretained(this)),
917 GetErrorCallback());
919 // Run only once, as there should have been one D-Bus call.
920 message_loop_.Run();
922 // The observer should have received the discovering changed event exactly
923 // once, the success callback should have been called 3 times and the adapter
924 // should be discovering.
925 EXPECT_EQ(1, observer.discovering_changed_count());
926 EXPECT_EQ(3, callback_count_);
927 EXPECT_EQ(0, error_callback_count_);
928 EXPECT_TRUE(observer.last_discovering());
929 EXPECT_TRUE(adapter_->IsDiscovering());
930 ASSERT_EQ((size_t)3, discovery_sessions_.size());
932 for (int i = 0; i < 3; i++)
933 EXPECT_TRUE(discovery_sessions_[i]->IsActive());
935 // Stop the timers that the simulation uses
936 fake_bluetooth_device_client_->EndDiscoverySimulation(
937 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath));
939 ASSERT_TRUE(adapter_->IsPowered());
940 ASSERT_TRUE(adapter_->IsDiscovering());
942 // Stop device discovery behind the adapter. The adapter and the observer
943 // should be notified of the change and the reference count should be reset.
944 // Even though FakeBluetoothAdapterClient does its own reference counting and
945 // we called 3 BluetoothAdapter::StartDiscoverySession 3 times, the
946 // FakeBluetoothAdapterClient's count should be only 1 and a single call to
947 // FakeBluetoothAdapterClient::StopDiscovery should work.
948 fake_bluetooth_adapter_client_->StopDiscovery(
949 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath), GetCallback(),
950 base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
951 base::Unretained(this)));
952 message_loop_.Run();
953 EXPECT_EQ(2, observer.discovering_changed_count());
954 EXPECT_EQ(4, callback_count_);
955 EXPECT_EQ(0, error_callback_count_);
956 EXPECT_FALSE(observer.last_discovering());
957 EXPECT_FALSE(adapter_->IsDiscovering());
959 // All discovery session instances should have been updated.
960 for (int i = 0; i < 3; i++)
961 EXPECT_FALSE(discovery_sessions_[i]->IsActive());
962 discovery_sessions_.clear();
964 // It should be possible to successfully start discovery.
965 for (int i = 0; i < 2; i++) {
966 adapter_->StartDiscoverySession(
967 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
968 base::Unretained(this)),
969 GetErrorCallback());
971 // Run only once, as there should have been one D-Bus call.
972 message_loop_.Run();
973 EXPECT_EQ(3, observer.discovering_changed_count());
974 EXPECT_EQ(6, callback_count_);
975 EXPECT_EQ(0, error_callback_count_);
976 EXPECT_TRUE(observer.last_discovering());
977 EXPECT_TRUE(adapter_->IsDiscovering());
978 ASSERT_EQ((size_t)2, discovery_sessions_.size());
980 for (int i = 0; i < 2; i++)
981 EXPECT_TRUE(discovery_sessions_[i]->IsActive());
983 fake_bluetooth_device_client_->EndDiscoverySimulation(
984 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath));
986 // Make the adapter disappear and appear. This will make it come back as
987 // discovering. When this happens, the reference count should become and
988 // remain 0 as no new request was made through the BluetoothAdapter.
989 fake_bluetooth_adapter_client_->SetVisible(false);
990 ASSERT_FALSE(adapter_->IsPresent());
991 EXPECT_EQ(4, observer.discovering_changed_count());
992 EXPECT_EQ(6, callback_count_);
993 EXPECT_EQ(0, error_callback_count_);
994 EXPECT_FALSE(observer.last_discovering());
995 EXPECT_FALSE(adapter_->IsDiscovering());
997 for (int i = 0; i < 2; i++)
998 EXPECT_FALSE(discovery_sessions_[i]->IsActive());
999 discovery_sessions_.clear();
1001 fake_bluetooth_adapter_client_->SetVisible(true);
1002 ASSERT_TRUE(adapter_->IsPresent());
1003 EXPECT_EQ(5, observer.discovering_changed_count());
1004 EXPECT_EQ(6, callback_count_);
1005 EXPECT_EQ(0, error_callback_count_);
1006 EXPECT_TRUE(observer.last_discovering());
1007 EXPECT_TRUE(adapter_->IsDiscovering());
1009 // Start and stop discovery. At this point, FakeBluetoothAdapterClient has
1010 // a reference count that is equal to 1. Pretend that this was done by an
1011 // application other than us. Starting and stopping discovery will succeed
1012 // but it won't cause the discovery state to change.
1013 adapter_->StartDiscoverySession(
1014 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1015 base::Unretained(this)),
1016 GetErrorCallback());
1017 message_loop_.Run(); // Run the loop, as there should have been a D-Bus call.
1018 EXPECT_EQ(5, observer.discovering_changed_count());
1019 EXPECT_EQ(7, callback_count_);
1020 EXPECT_EQ(0, error_callback_count_);
1021 EXPECT_TRUE(observer.last_discovering());
1022 EXPECT_TRUE(adapter_->IsDiscovering());
1023 ASSERT_EQ((size_t)1, discovery_sessions_.size());
1024 EXPECT_TRUE(discovery_sessions_[0]->IsActive());
1026 discovery_sessions_[0]->Stop(GetCallback(), GetErrorCallback());
1027 message_loop_.Run(); // Run the loop, as there should have been a D-Bus call.
1028 EXPECT_EQ(5, observer.discovering_changed_count());
1029 EXPECT_EQ(8, callback_count_);
1030 EXPECT_EQ(0, error_callback_count_);
1031 EXPECT_TRUE(observer.last_discovering());
1032 EXPECT_TRUE(adapter_->IsDiscovering());
1033 EXPECT_FALSE(discovery_sessions_[0]->IsActive());
1034 discovery_sessions_.clear();
1036 // Start discovery again.
1037 adapter_->StartDiscoverySession(
1038 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1039 base::Unretained(this)),
1040 GetErrorCallback());
1041 message_loop_.Run(); // Run the loop, as there should have been a D-Bus call.
1042 EXPECT_EQ(5, observer.discovering_changed_count());
1043 EXPECT_EQ(9, callback_count_);
1044 EXPECT_EQ(0, error_callback_count_);
1045 EXPECT_TRUE(observer.last_discovering());
1046 EXPECT_TRUE(adapter_->IsDiscovering());
1047 ASSERT_EQ((size_t)1, discovery_sessions_.size());
1048 EXPECT_TRUE(discovery_sessions_[0]->IsActive());
1050 // Stop discovery via D-Bus. The fake client's reference count will drop but
1051 // the discovery state won't change since our BluetoothAdapter also just
1052 // requested it via D-Bus.
1053 fake_bluetooth_adapter_client_->StopDiscovery(
1054 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath), GetCallback(),
1055 base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
1056 base::Unretained(this)));
1057 message_loop_.Run();
1058 EXPECT_EQ(5, observer.discovering_changed_count());
1059 EXPECT_EQ(10, callback_count_);
1060 EXPECT_EQ(0, error_callback_count_);
1061 EXPECT_TRUE(observer.last_discovering());
1062 EXPECT_TRUE(adapter_->IsDiscovering());
1064 // Now end the discovery session. This should change the adapter's discovery
1065 // state.
1066 discovery_sessions_[0]->Stop(GetCallback(), GetErrorCallback());
1067 message_loop_.Run();
1068 EXPECT_EQ(6, observer.discovering_changed_count());
1069 EXPECT_EQ(11, callback_count_);
1070 EXPECT_EQ(0, error_callback_count_);
1071 EXPECT_FALSE(observer.last_discovering());
1072 EXPECT_FALSE(adapter_->IsDiscovering());
1073 EXPECT_FALSE(discovery_sessions_[0]->IsActive());
1076 TEST_F(BluetoothChromeOSTest, InvalidatedDiscoverySessions) {
1077 GetAdapter();
1078 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
1079 EXPECT_EQ(1, callback_count_);
1080 EXPECT_EQ(0, error_callback_count_);
1081 EXPECT_TRUE(adapter_->IsPowered());
1082 callback_count_ = 0;
1084 TestBluetoothAdapterObserver observer(adapter_);
1086 EXPECT_EQ(0, observer.discovering_changed_count());
1087 EXPECT_FALSE(observer.last_discovering());
1088 EXPECT_FALSE(adapter_->IsDiscovering());
1090 // Request device discovery 3 times.
1091 for (int i = 0; i < 3; i++) {
1092 adapter_->StartDiscoverySession(
1093 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1094 base::Unretained(this)),
1095 GetErrorCallback());
1097 // Run only once, as there should have been one D-Bus call.
1098 message_loop_.Run();
1100 // The observer should have received the discovering changed event exactly
1101 // once, the success callback should have been called 3 times and the adapter
1102 // should be discovering.
1103 EXPECT_EQ(1, observer.discovering_changed_count());
1104 EXPECT_EQ(3, callback_count_);
1105 EXPECT_EQ(0, error_callback_count_);
1106 EXPECT_TRUE(observer.last_discovering());
1107 EXPECT_TRUE(adapter_->IsDiscovering());
1108 ASSERT_EQ((size_t)3, discovery_sessions_.size());
1110 for (int i = 0; i < 3; i++)
1111 EXPECT_TRUE(discovery_sessions_[i]->IsActive());
1113 // Stop the timers that the simulation uses
1114 fake_bluetooth_device_client_->EndDiscoverySimulation(
1115 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath));
1117 ASSERT_TRUE(adapter_->IsPowered());
1118 ASSERT_TRUE(adapter_->IsDiscovering());
1120 // Delete all but one discovery session.
1121 discovery_sessions_.pop_back();
1122 discovery_sessions_.pop_back();
1123 ASSERT_EQ((size_t)1, discovery_sessions_.size());
1124 EXPECT_TRUE(discovery_sessions_[0]->IsActive());
1125 EXPECT_TRUE(adapter_->IsDiscovering());
1127 // Stop device discovery behind the adapter. The one active discovery session
1128 // should become inactive, but more importantly, we shouldn't run into any
1129 // memory errors as the sessions that we explicitly deleted should get
1130 // cleaned up.
1131 fake_bluetooth_adapter_client_->StopDiscovery(
1132 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath), GetCallback(),
1133 base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
1134 base::Unretained(this)));
1135 message_loop_.Run();
1136 EXPECT_EQ(2, observer.discovering_changed_count());
1137 EXPECT_EQ(4, callback_count_);
1138 EXPECT_EQ(0, error_callback_count_);
1139 EXPECT_FALSE(observer.last_discovering());
1140 EXPECT_FALSE(adapter_->IsDiscovering());
1141 EXPECT_FALSE(discovery_sessions_[0]->IsActive());
1144 TEST_F(BluetoothChromeOSTest, QueuedDiscoveryRequests) {
1145 GetAdapter();
1147 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
1148 EXPECT_EQ(1, callback_count_);
1149 EXPECT_EQ(0, error_callback_count_);
1150 EXPECT_TRUE(adapter_->IsPowered());
1151 callback_count_ = 0;
1153 TestBluetoothAdapterObserver observer(adapter_);
1155 EXPECT_EQ(0, observer.discovering_changed_count());
1156 EXPECT_FALSE(observer.last_discovering());
1157 EXPECT_FALSE(adapter_->IsDiscovering());
1159 // Request to start discovery. The call should be pending.
1160 adapter_->StartDiscoverySession(
1161 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1162 base::Unretained(this)),
1163 GetErrorCallback());
1164 EXPECT_EQ(0, callback_count_);
1166 fake_bluetooth_device_client_->EndDiscoverySimulation(
1167 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath));
1169 // The underlying adapter has started discovery, but our call hasn't returned
1170 // yet.
1171 EXPECT_EQ(1, observer.discovering_changed_count());
1172 EXPECT_TRUE(observer.last_discovering());
1173 EXPECT_TRUE(adapter_->IsDiscovering());
1174 EXPECT_TRUE(discovery_sessions_.empty());
1176 // Request to start discovery twice. These should get queued and there should
1177 // be no change in state.
1178 for (int i = 0; i < 2; i++) {
1179 adapter_->StartDiscoverySession(
1180 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1181 base::Unretained(this)),
1182 GetErrorCallback());
1184 EXPECT_EQ(0, callback_count_);
1185 EXPECT_EQ(0, error_callback_count_);
1186 EXPECT_EQ(1, observer.discovering_changed_count());
1187 EXPECT_TRUE(observer.last_discovering());
1188 EXPECT_TRUE(adapter_->IsDiscovering());
1189 EXPECT_TRUE(discovery_sessions_.empty());
1191 // Process the pending call. The queued calls should execute and the discovery
1192 // session reference count should increase.
1193 message_loop_.Run();
1194 EXPECT_EQ(3, callback_count_);
1195 EXPECT_EQ(0, error_callback_count_);
1196 EXPECT_EQ(1, observer.discovering_changed_count());
1197 EXPECT_TRUE(observer.last_discovering());
1198 EXPECT_TRUE(adapter_->IsDiscovering());
1199 ASSERT_EQ((size_t)3, discovery_sessions_.size());
1201 // Verify the reference count by removing sessions 3 times. The last request
1202 // should remain pending.
1203 for (int i = 0; i < 3; i++) {
1204 discovery_sessions_[i]->Stop(GetCallback(), GetErrorCallback());
1206 EXPECT_EQ(5, callback_count_);
1207 EXPECT_EQ(0, error_callback_count_);
1208 EXPECT_EQ(2, observer.discovering_changed_count());
1209 EXPECT_FALSE(observer.last_discovering());
1210 EXPECT_FALSE(adapter_->IsDiscovering());
1211 EXPECT_FALSE(discovery_sessions_[0]->IsActive());
1212 EXPECT_FALSE(discovery_sessions_[1]->IsActive());
1213 EXPECT_TRUE(discovery_sessions_[2]->IsActive());
1215 // Request to stop the session whose call is pending should fail.
1216 discovery_sessions_[2]->Stop(GetCallback(), GetErrorCallback());
1217 EXPECT_EQ(5, callback_count_);
1218 EXPECT_EQ(1, error_callback_count_);
1219 EXPECT_EQ(2, observer.discovering_changed_count());
1220 EXPECT_FALSE(observer.last_discovering());
1221 EXPECT_FALSE(adapter_->IsDiscovering());
1222 EXPECT_TRUE(discovery_sessions_[2]->IsActive());
1224 // Request to start should get queued.
1225 adapter_->StartDiscoverySession(
1226 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1227 base::Unretained(this)),
1228 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 ASSERT_EQ((size_t)3, discovery_sessions_.size());
1236 // Run the pending request.
1237 message_loop_.Run();
1238 EXPECT_EQ(6, callback_count_);
1239 EXPECT_EQ(1, error_callback_count_);
1240 EXPECT_EQ(3, observer.discovering_changed_count());
1241 EXPECT_TRUE(observer.last_discovering());
1242 EXPECT_TRUE(adapter_->IsDiscovering());
1243 ASSERT_EQ((size_t)3, discovery_sessions_.size());
1244 EXPECT_FALSE(discovery_sessions_[2]->IsActive());
1246 // The queued request to start discovery should have been issued but is still
1247 // pending. Run the loop and verify.
1248 message_loop_.Run();
1249 EXPECT_EQ(7, callback_count_);
1250 EXPECT_EQ(1, error_callback_count_);
1251 EXPECT_EQ(3, observer.discovering_changed_count());
1252 EXPECT_TRUE(observer.last_discovering());
1253 EXPECT_TRUE(adapter_->IsDiscovering());
1254 ASSERT_EQ((size_t)4, discovery_sessions_.size());
1255 EXPECT_TRUE(discovery_sessions_[3]->IsActive());
1258 TEST_F(BluetoothChromeOSTest, StartDiscoverySession) {
1259 GetAdapter();
1261 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
1262 EXPECT_EQ(1, callback_count_);
1263 EXPECT_EQ(0, error_callback_count_);
1264 EXPECT_TRUE(adapter_->IsPowered());
1265 callback_count_ = 0;
1267 TestBluetoothAdapterObserver observer(adapter_);
1269 EXPECT_EQ(0, observer.discovering_changed_count());
1270 EXPECT_FALSE(observer.last_discovering());
1271 EXPECT_FALSE(adapter_->IsDiscovering());
1272 EXPECT_TRUE(discovery_sessions_.empty());
1274 // Request a new discovery session.
1275 adapter_->StartDiscoverySession(
1276 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1277 base::Unretained(this)),
1278 GetErrorCallback());
1279 message_loop_.Run();
1280 EXPECT_EQ(1, observer.discovering_changed_count());
1281 EXPECT_EQ(1, callback_count_);
1282 EXPECT_EQ(0, error_callback_count_);
1283 EXPECT_TRUE(observer.last_discovering());
1284 EXPECT_TRUE(adapter_->IsDiscovering());
1285 ASSERT_EQ((size_t)1, discovery_sessions_.size());
1286 EXPECT_TRUE(discovery_sessions_[0]->IsActive());
1288 // Start another session. A new one should be returned in the callback, which
1289 // in turn will destroy the previous session. Adapter should still be
1290 // discovering and the reference count should be 1.
1291 adapter_->StartDiscoverySession(
1292 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1293 base::Unretained(this)),
1294 GetErrorCallback());
1295 message_loop_.Run();
1296 EXPECT_EQ(1, observer.discovering_changed_count());
1297 EXPECT_EQ(2, callback_count_);
1298 EXPECT_EQ(0, error_callback_count_);
1299 EXPECT_TRUE(observer.last_discovering());
1300 EXPECT_TRUE(adapter_->IsDiscovering());
1301 ASSERT_EQ((size_t)2, discovery_sessions_.size());
1302 EXPECT_TRUE(discovery_sessions_[0]->IsActive());
1304 // Request a new session.
1305 adapter_->StartDiscoverySession(
1306 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1307 base::Unretained(this)),
1308 GetErrorCallback());
1309 message_loop_.Run();
1310 EXPECT_EQ(1, observer.discovering_changed_count());
1311 EXPECT_EQ(3, callback_count_);
1312 EXPECT_EQ(0, error_callback_count_);
1313 EXPECT_TRUE(observer.last_discovering());
1314 EXPECT_TRUE(adapter_->IsDiscovering());
1315 ASSERT_EQ((size_t)3, discovery_sessions_.size());
1316 EXPECT_TRUE(discovery_sessions_[1]->IsActive());
1317 EXPECT_NE(discovery_sessions_[0], discovery_sessions_[1]);
1319 // Stop the previous discovery session. The session should end but discovery
1320 // should continue.
1321 discovery_sessions_[0]->Stop(GetCallback(), GetErrorCallback());
1322 message_loop_.Run();
1323 EXPECT_EQ(1, observer.discovering_changed_count());
1324 EXPECT_EQ(4, callback_count_);
1325 EXPECT_EQ(0, error_callback_count_);
1326 EXPECT_TRUE(observer.last_discovering());
1327 EXPECT_TRUE(adapter_->IsDiscovering());
1328 ASSERT_EQ((size_t)3, discovery_sessions_.size());
1329 EXPECT_FALSE(discovery_sessions_[0]->IsActive());
1330 EXPECT_TRUE(discovery_sessions_[1]->IsActive());
1332 // Delete the current active session. Discovery should eventually stop.
1333 discovery_sessions_.clear();
1334 while (observer.last_discovering())
1335 message_loop_.RunUntilIdle();
1337 EXPECT_EQ(2, observer.discovering_changed_count());
1338 EXPECT_EQ(4, callback_count_);
1339 EXPECT_EQ(0, error_callback_count_);
1340 EXPECT_FALSE(observer.last_discovering());
1341 EXPECT_FALSE(adapter_->IsDiscovering());
1344 TEST_F(BluetoothChromeOSTest, SetDiscoveryFilterBeforeStartDiscovery) {
1345 // Test a simulated discovery session.
1346 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
1347 GetAdapter();
1349 TestBluetoothAdapterObserver observer(adapter_);
1351 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter(
1352 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
1353 df->SetRSSI(-60);
1354 df->AddUUID(BluetoothUUID("1000"));
1355 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter(df);
1357 adapter_->SetPowered(true, base::Bind(&BluetoothChromeOSTest::Callback,
1358 base::Unretained(this)),
1359 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1360 base::Unretained(this)));
1361 adapter_->StartDiscoverySessionWithFilter(
1362 discovery_filter.Pass(),
1363 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1364 base::Unretained(this)),
1365 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1366 base::Unretained(this)));
1367 message_loop_.Run();
1368 EXPECT_EQ(2, callback_count_);
1369 EXPECT_EQ(0, error_callback_count_);
1370 callback_count_ = 0;
1372 ASSERT_TRUE(adapter_->IsPowered());
1373 ASSERT_TRUE(adapter_->IsDiscovering());
1374 ASSERT_EQ((size_t)1, discovery_sessions_.size());
1375 ASSERT_TRUE(discovery_sessions_[0]->IsActive());
1376 ASSERT_TRUE(df->Equals(*discovery_sessions_[0]->GetDiscoveryFilter()));
1378 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1379 EXPECT_NE(nullptr, filter);
1380 EXPECT_EQ("le", *filter->transport);
1381 EXPECT_EQ(-60, *filter->rssi);
1382 EXPECT_EQ(nullptr, filter->pathloss.get());
1383 std::vector<std::string> uuids = *filter->uuids;
1384 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1000"));
1386 discovery_sessions_[0]->Stop(
1387 base::Bind(&BluetoothChromeOSTest::Callback, base::Unretained(this)),
1388 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1389 base::Unretained(this)));
1391 message_loop_.Run();
1393 EXPECT_EQ(1, callback_count_);
1394 EXPECT_EQ(0, error_callback_count_);
1396 ASSERT_TRUE(adapter_->IsPowered());
1397 ASSERT_FALSE(adapter_->IsDiscovering());
1398 ASSERT_EQ((size_t)1, discovery_sessions_.size());
1399 ASSERT_FALSE(discovery_sessions_[0]->IsActive());
1400 ASSERT_EQ(discovery_sessions_[0]->GetDiscoveryFilter(),
1401 (BluetoothDiscoveryFilter*)NULL);
1403 filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1404 EXPECT_EQ(nullptr, filter);
1407 TEST_F(BluetoothChromeOSTest, SetDiscoveryFilterBeforeStartDiscoveryFail) {
1408 // Test a simulated discovery session.
1409 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
1410 GetAdapter();
1412 TestBluetoothAdapterObserver observer(adapter_);
1414 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter(
1415 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
1416 df->SetRSSI(-60);
1417 df->AddUUID(BluetoothUUID("1000"));
1418 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter(df);
1420 adapter_->SetPowered(true, base::Bind(&BluetoothChromeOSTest::Callback,
1421 base::Unretained(this)),
1422 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1423 base::Unretained(this)));
1424 EXPECT_EQ(1, callback_count_);
1425 callback_count_ = 0;
1427 fake_bluetooth_adapter_client_->MakeSetDiscoveryFilterFail();
1429 adapter_->StartDiscoverySessionWithFilter(
1430 discovery_filter.Pass(),
1431 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1432 base::Unretained(this)),
1433 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1434 base::Unretained(this)));
1436 message_loop_.Run();
1438 EXPECT_EQ(1, error_callback_count_);
1439 error_callback_count_ = 0;
1441 ASSERT_TRUE(adapter_->IsPowered());
1442 ASSERT_FALSE(adapter_->IsDiscovering());
1443 ASSERT_EQ((size_t)0, discovery_sessions_.size());
1445 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1446 EXPECT_EQ(nullptr, filter);
1449 // This test queues two requests to StartDiscovery with pre set filter. This
1450 // should result in SetDiscoveryFilter, then StartDiscovery, and SetDiscovery
1451 // DBus calls
1452 TEST_F(BluetoothChromeOSTest, QueuedSetDiscoveryFilterBeforeStartDiscovery) {
1453 // Test a simulated discovery session.
1454 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
1455 GetAdapter();
1457 TestBluetoothAdapterObserver observer(adapter_);
1459 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter(
1460 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
1461 df->SetRSSI(-60);
1462 df->AddUUID(BluetoothUUID("1000"));
1463 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter(df);
1465 BluetoothDiscoveryFilter* df2 = new BluetoothDiscoveryFilter(
1466 BluetoothDiscoveryFilter::Transport::TRANSPORT_CLASSIC);
1467 df2->SetRSSI(-65);
1468 df2->AddUUID(BluetoothUUID("1002"));
1469 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter2(df2);
1471 adapter_->SetPowered(true, base::Bind(&BluetoothChromeOSTest::Callback,
1472 base::Unretained(this)),
1473 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1474 base::Unretained(this)));
1476 EXPECT_EQ(1, callback_count_);
1477 EXPECT_EQ(0, error_callback_count_);
1478 callback_count_ = 0;
1480 // Queue two requests to start discovery session with filter.
1481 adapter_->StartDiscoverySessionWithFilter(
1482 discovery_filter.Pass(),
1483 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1484 base::Unretained(this)),
1485 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1486 base::Unretained(this)));
1488 adapter_->StartDiscoverySessionWithFilter(
1489 discovery_filter2.Pass(),
1490 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1491 base::Unretained(this)),
1492 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1493 base::Unretained(this)));
1495 // Run requests, on DBus level there should be call SetDiscoveryFilter, then
1496 // StartDiscovery, then SetDiscoveryFilter again.
1497 message_loop_.Run();
1498 message_loop_.Run();
1500 EXPECT_EQ(2, callback_count_);
1501 EXPECT_EQ(0, error_callback_count_);
1502 callback_count_ = 0;
1504 ASSERT_TRUE(adapter_->IsPowered());
1505 ASSERT_TRUE(adapter_->IsDiscovering());
1506 ASSERT_EQ((size_t)2, discovery_sessions_.size());
1507 ASSERT_TRUE(discovery_sessions_[0]->IsActive());
1508 ASSERT_TRUE(df->Equals(*discovery_sessions_[0]->GetDiscoveryFilter()));
1509 ASSERT_TRUE(discovery_sessions_[1]->IsActive());
1510 ASSERT_TRUE(df2->Equals(*discovery_sessions_[1]->GetDiscoveryFilter()));
1512 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1513 EXPECT_NE(nullptr, filter);
1514 EXPECT_EQ("auto", *filter->transport);
1515 EXPECT_EQ(-65, *filter->rssi);
1516 EXPECT_EQ(nullptr, filter->pathloss.get());
1517 auto uuids = *filter->uuids;
1518 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1000"));
1519 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1002"));
1521 discovery_sessions_[0]->Stop(
1522 base::Bind(&BluetoothChromeOSTest::Callback, base::Unretained(this)),
1523 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1524 base::Unretained(this)));
1526 discovery_sessions_[1]->Stop(
1527 base::Bind(&BluetoothChromeOSTest::Callback, base::Unretained(this)),
1528 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1529 base::Unretained(this)));
1531 message_loop_.Run();
1533 EXPECT_EQ(2, callback_count_);
1534 EXPECT_EQ(0, error_callback_count_);
1536 ASSERT_TRUE(adapter_->IsPowered());
1537 ASSERT_FALSE(adapter_->IsDiscovering());
1538 ASSERT_FALSE(discovery_sessions_[0]->IsActive());
1539 ASSERT_EQ(discovery_sessions_[0]->GetDiscoveryFilter(),
1540 (BluetoothDiscoveryFilter*)NULL);
1541 ASSERT_FALSE(discovery_sessions_[1]->IsActive());
1542 ASSERT_EQ(discovery_sessions_[1]->GetDiscoveryFilter(),
1543 (BluetoothDiscoveryFilter*)NULL);
1545 filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1546 EXPECT_EQ(nullptr, filter);
1549 // Call StartFilteredDiscovery twice (2nd time while 1st call is still pending).
1550 // Make the first SetDiscoveryFilter fail and the second one succeed. It should
1551 // end up with one active discovery session.
1552 TEST_F(BluetoothChromeOSTest,
1553 QueuedSetDiscoveryFilterBeforeStartDiscoveryFail) {
1554 // Test a simulated discovery session.
1555 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
1556 GetAdapter();
1558 TestBluetoothAdapterObserver observer(adapter_);
1560 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter(
1561 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
1562 df->SetRSSI(-60);
1563 df->AddUUID(BluetoothUUID("1000"));
1564 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter(df);
1566 BluetoothDiscoveryFilter* df2 = new BluetoothDiscoveryFilter(
1567 BluetoothDiscoveryFilter::Transport::TRANSPORT_CLASSIC);
1568 df2->SetRSSI(-65);
1569 df2->AddUUID(BluetoothUUID("1002"));
1570 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter2(df2);
1572 adapter_->SetPowered(true, base::Bind(&BluetoothChromeOSTest::Callback,
1573 base::Unretained(this)),
1574 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1575 base::Unretained(this)));
1577 EXPECT_EQ(1, callback_count_);
1578 EXPECT_EQ(0, error_callback_count_);
1579 callback_count_ = 0;
1581 fake_bluetooth_adapter_client_->MakeSetDiscoveryFilterFail();
1583 // Queue two requests to start discovery session with filter.
1584 adapter_->StartDiscoverySessionWithFilter(
1585 discovery_filter.Pass(),
1586 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1587 base::Unretained(this)),
1588 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1589 base::Unretained(this)));
1591 adapter_->StartDiscoverySessionWithFilter(
1592 discovery_filter2.Pass(),
1593 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1594 base::Unretained(this)),
1595 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1596 base::Unretained(this)));
1598 message_loop_.Run();
1600 // First request to SetDiscoveryFilter should fail, resulting in no session
1601 // being created.
1602 EXPECT_EQ(0, callback_count_);
1603 EXPECT_EQ(1, error_callback_count_);
1604 error_callback_count_ = 0;
1606 ASSERT_TRUE(adapter_->IsPowered());
1607 ASSERT_FALSE(adapter_->IsDiscovering());
1608 ASSERT_EQ((size_t)0, discovery_sessions_.size());
1610 message_loop_.Run();
1612 // Second request should succeed
1613 EXPECT_EQ(1, callback_count_);
1614 EXPECT_EQ(0, error_callback_count_);
1615 callback_count_ = 0;
1617 ASSERT_TRUE(adapter_->IsDiscovering());
1618 ASSERT_EQ((size_t)1, discovery_sessions_.size());
1619 ASSERT_TRUE(discovery_sessions_[0]->IsActive());
1620 ASSERT_TRUE(df2->Equals(*discovery_sessions_[0]->GetDiscoveryFilter()));
1622 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1623 EXPECT_NE(nullptr, filter);
1624 EXPECT_EQ("bredr", *filter->transport);
1625 EXPECT_EQ(-65, *filter->rssi);
1626 EXPECT_EQ(nullptr, filter->pathloss.get());
1627 auto uuids = *filter->uuids;
1628 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1002"));
1630 discovery_sessions_[0]->Stop(
1631 base::Bind(&BluetoothChromeOSTest::Callback, base::Unretained(this)),
1632 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1633 base::Unretained(this)));
1635 message_loop_.Run();
1637 EXPECT_EQ(1, callback_count_);
1638 EXPECT_EQ(0, error_callback_count_);
1640 ASSERT_TRUE(adapter_->IsPowered());
1641 ASSERT_FALSE(adapter_->IsDiscovering());
1642 ASSERT_FALSE(discovery_sessions_[0]->IsActive());
1643 ASSERT_EQ(discovery_sessions_[0]->GetDiscoveryFilter(),
1644 (BluetoothDiscoveryFilter*)NULL);
1646 filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1647 EXPECT_EQ(nullptr, filter);
1650 TEST_F(BluetoothChromeOSTest, SetDiscoveryFilterAfterStartDiscovery) {
1651 // Test a simulated discovery session.
1652 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
1653 GetAdapter();
1655 TestBluetoothAdapterObserver observer(adapter_);
1657 adapter_->SetPowered(true, base::Bind(&BluetoothChromeOSTest::Callback,
1658 base::Unretained(this)),
1659 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1660 base::Unretained(this)));
1661 adapter_->StartDiscoverySession(
1662 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1663 base::Unretained(this)),
1664 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1665 base::Unretained(this)));
1666 message_loop_.Run();
1667 EXPECT_EQ(2, callback_count_);
1668 EXPECT_EQ(0, error_callback_count_);
1669 callback_count_ = 0;
1671 ASSERT_TRUE(adapter_->IsPowered());
1672 ASSERT_TRUE(adapter_->IsDiscovering());
1673 ASSERT_EQ((size_t)1, discovery_sessions_.size());
1674 ASSERT_TRUE(discovery_sessions_[0]->IsActive());
1675 EXPECT_EQ(1, observer.discovering_changed_count());
1676 observer.Reset();
1678 auto nullInstance = scoped_ptr<BluetoothDiscoveryFilter>();
1679 nullInstance.reset();
1680 ASSERT_EQ(discovery_sessions_[0]->GetDiscoveryFilter(), nullInstance.get());
1682 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1683 EXPECT_EQ(nullptr, filter);
1685 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter(
1686 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
1687 df->SetRSSI(-60);
1688 df->AddUUID(BluetoothUUID("1000"));
1689 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter(df);
1691 discovery_sessions_[0]->SetDiscoveryFilter(
1692 discovery_filter.Pass(),
1693 base::Bind(&BluetoothChromeOSTest::Callback, base::Unretained(this)),
1694 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1695 base::Unretained(this)));
1697 message_loop_.Run();
1698 EXPECT_EQ(1, callback_count_);
1699 EXPECT_EQ(0, error_callback_count_);
1700 callback_count_ = 0;
1702 ASSERT_TRUE(df->Equals(*discovery_sessions_[0]->GetDiscoveryFilter()));
1704 filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1705 EXPECT_NE(nullptr, filter);
1706 EXPECT_EQ("le", *filter->transport);
1707 EXPECT_EQ(-60, *filter->rssi);
1708 EXPECT_EQ(nullptr, filter->pathloss.get());
1709 std::vector<std::string> uuids = *filter->uuids;
1710 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1000"));
1712 discovery_sessions_[0]->Stop(
1713 base::Bind(&BluetoothChromeOSTest::Callback, base::Unretained(this)),
1714 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1715 base::Unretained(this)));
1717 message_loop_.Run();
1719 EXPECT_EQ(1, callback_count_);
1720 EXPECT_EQ(0, error_callback_count_);
1722 ASSERT_TRUE(adapter_->IsPowered());
1723 ASSERT_FALSE(adapter_->IsDiscovering());
1724 ASSERT_EQ((size_t)1, discovery_sessions_.size());
1725 ASSERT_FALSE(discovery_sessions_[0]->IsActive());
1726 ASSERT_EQ(discovery_sessions_[0]->GetDiscoveryFilter(),
1727 (BluetoothDiscoveryFilter*)NULL);
1729 filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1730 EXPECT_EQ(nullptr, filter);
1733 // This unit test asserts that the basic reference counting, and filter merging
1734 // works correctly for discovery requests done via the BluetoothAdapter.
1735 TEST_F(BluetoothChromeOSTest, SetDiscoveryFilterBeforeStartDiscoveryMultiple) {
1736 GetAdapter();
1737 adapter_->SetPowered(true, base::Bind(&BluetoothChromeOSTest::Callback,
1738 base::Unretained(this)),
1739 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1740 base::Unretained(this)));
1741 EXPECT_EQ(1, callback_count_);
1742 EXPECT_EQ(0, error_callback_count_);
1743 EXPECT_TRUE(adapter_->IsPowered());
1744 callback_count_ = 0;
1746 TestBluetoothAdapterObserver observer(adapter_);
1748 // Request device discovery with pre-set filter 3 times.
1749 for (int i = 0; i < 3; i++) {
1750 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter;
1751 if (i == 0) {
1752 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter(
1753 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
1754 df->SetRSSI(-85);
1755 df->AddUUID(BluetoothUUID("1000"));
1756 discovery_filter.reset(df);
1757 } else if (i == 1) {
1758 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter(
1759 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
1760 df->SetRSSI(-60);
1761 df->AddUUID(BluetoothUUID("1020"));
1762 df->AddUUID(BluetoothUUID("1001"));
1763 discovery_filter.reset(df);
1764 } else if (i == 2) {
1765 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter(
1766 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
1767 df->SetRSSI(-65);
1768 df->AddUUID(BluetoothUUID("1020"));
1769 df->AddUUID(BluetoothUUID("1003"));
1770 discovery_filter.reset(df);
1773 adapter_->StartDiscoverySessionWithFilter(
1774 discovery_filter.Pass(),
1775 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1776 base::Unretained(this)),
1777 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1778 base::Unretained(this)));
1780 message_loop_.Run();
1782 if (i == 0) {
1783 EXPECT_EQ(1, observer.discovering_changed_count());
1784 observer.Reset();
1786 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1787 EXPECT_EQ("le", *filter->transport);
1788 EXPECT_EQ(-85, *filter->rssi);
1789 EXPECT_EQ(nullptr, filter->pathloss.get());
1790 std::vector<std::string> uuids = *filter->uuids;
1791 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1000"));
1792 } else if (i == 1) {
1793 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1794 EXPECT_EQ("le", *filter->transport);
1795 EXPECT_EQ(-85, *filter->rssi);
1796 EXPECT_EQ(nullptr, filter->pathloss.get());
1797 std::vector<std::string> uuids = *filter->uuids;
1798 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1000"));
1799 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1001"));
1800 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1020"));
1801 } else if (i == 2) {
1802 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1803 EXPECT_EQ("le", *filter->transport);
1804 EXPECT_EQ(-85, *filter->rssi);
1805 EXPECT_EQ(nullptr, filter->pathloss.get());
1806 std::vector<std::string> uuids = *filter->uuids;
1807 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1000"));
1808 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1001"));
1809 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1003"));
1810 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1020"));
1814 // the success callback should have been called 3 times and the adapter should
1815 // be discovering.
1816 EXPECT_EQ(3, callback_count_);
1817 EXPECT_EQ(0, error_callback_count_);
1818 EXPECT_TRUE(adapter_->IsDiscovering());
1819 ASSERT_EQ((size_t)3, discovery_sessions_.size());
1821 callback_count_ = 0;
1822 // Request to stop discovery twice.
1823 for (int i = 0; i < 2; i++) {
1824 discovery_sessions_[i]->Stop(
1825 base::Bind(&BluetoothChromeOSTest::Callback, base::Unretained(this)),
1826 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1827 base::Unretained(this)));
1828 message_loop_.Run();
1830 if (i == 0) {
1831 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1832 EXPECT_EQ("le", *filter->transport);
1833 EXPECT_EQ(-65, *filter->rssi);
1834 EXPECT_EQ(nullptr, filter->pathloss.get());
1835 std::vector<std::string> uuids = *filter->uuids;
1836 EXPECT_EQ(3UL, uuids.size());
1837 EXPECT_EQ(uuids.end(), std::find(uuids.begin(), uuids.end(), "1000"));
1838 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1001"));
1839 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1003"));
1840 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1020"));
1841 } else if (i == 1) {
1842 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1843 EXPECT_EQ("le", *filter->transport);
1844 EXPECT_EQ(-65, *filter->rssi);
1845 EXPECT_EQ(nullptr, filter->pathloss.get());
1846 std::vector<std::string> uuids = *filter->uuids;
1847 EXPECT_EQ(2UL, uuids.size());
1848 EXPECT_EQ(uuids.end(), std::find(uuids.begin(), uuids.end(), "1000"));
1849 EXPECT_EQ(uuids.end(), std::find(uuids.begin(), uuids.end(), "1001"));
1850 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1003"));
1851 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1020"));
1852 } else if (i == 2) {
1853 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1854 EXPECT_EQ("le", *filter->transport);
1855 EXPECT_EQ(-65, *filter->rssi);
1856 EXPECT_EQ(nullptr, filter->pathloss.get());
1857 std::vector<std::string> uuids = *filter->uuids;
1858 EXPECT_EQ(0UL, uuids.size());
1862 // The success callback should have been called 2 times and the adapter should
1863 // still be discovering.
1864 EXPECT_EQ(2, callback_count_);
1865 EXPECT_EQ(0, error_callback_count_);
1866 EXPECT_TRUE(adapter_->IsDiscovering());
1867 EXPECT_FALSE(discovery_sessions_[0]->IsActive());
1868 EXPECT_FALSE(discovery_sessions_[1]->IsActive());
1869 EXPECT_TRUE(discovery_sessions_[2]->IsActive());
1871 callback_count_ = 0;
1873 // Request device discovery 3 times.
1874 for (int i = 0; i < 3; i++) {
1875 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter;
1877 if (i == 0) {
1878 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter(
1879 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
1880 df->SetRSSI(-85);
1881 df->AddUUID(BluetoothUUID("1000"));
1882 discovery_filter.reset(df);
1883 } else if (i == 1) {
1884 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter(
1885 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
1886 df->SetRSSI(-60);
1887 df->AddUUID(BluetoothUUID("1020"));
1888 df->AddUUID(BluetoothUUID("1001"));
1889 discovery_filter.reset(df);
1890 } else if (i == 2) {
1891 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter(
1892 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
1893 df->SetRSSI(-65);
1894 df->AddUUID(BluetoothUUID("1020"));
1895 df->AddUUID(BluetoothUUID("1003"));
1896 discovery_filter.reset(df);
1899 adapter_->StartDiscoverySessionWithFilter(
1900 discovery_filter.Pass(),
1901 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1902 base::Unretained(this)),
1903 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1904 base::Unretained(this)));
1906 // each result in 1 requests.
1907 message_loop_.Run();
1909 if (i == 0) {
1910 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1911 EXPECT_EQ("le", *filter->transport);
1912 EXPECT_EQ(-85, *filter->rssi);
1913 EXPECT_EQ(nullptr, filter->pathloss.get());
1914 std::vector<std::string> uuids = *filter->uuids;
1915 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1000"));
1916 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1003"));
1917 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1020"));
1918 } else if (i == 1 || i == 2) {
1919 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1920 EXPECT_EQ("le", *filter->transport);
1921 EXPECT_EQ(-85, *filter->rssi);
1922 EXPECT_EQ(nullptr, filter->pathloss.get());
1923 std::vector<std::string> uuids = *filter->uuids;
1924 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1000"));
1925 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1001"));
1926 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1003"));
1927 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1020"));
1931 // The success callback should have been called 3 times and the adapter should
1932 // still be discovering.
1933 EXPECT_EQ(3, callback_count_);
1934 EXPECT_EQ(0, error_callback_count_);
1935 EXPECT_TRUE(adapter_->IsDiscovering());
1936 ASSERT_EQ((size_t)6, discovery_sessions_.size());
1938 callback_count_ = 0;
1939 // Request to stop discovery 4 times.
1940 for (int i = 2; i < 6; i++) {
1941 discovery_sessions_[i]->Stop(
1942 base::Bind(&BluetoothChromeOSTest::Callback, base::Unretained(this)),
1943 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1944 base::Unretained(this)));
1946 // filter no 2 is same as filter no 5, so removing it shouldn't cause any
1947 // filter update
1948 if (i != 2 && i != 5)
1949 message_loop_.Run();
1951 // Run only once, as there should have been one D-Bus call.
1952 message_loop_.Run();
1954 // The success callback should have been called 4 times and the adapter should
1955 // no longer be discovering.
1956 EXPECT_EQ(4, callback_count_);
1957 EXPECT_EQ(0, error_callback_count_);
1958 EXPECT_FALSE(adapter_->IsDiscovering());
1959 EXPECT_EQ(1, observer.discovering_changed_count());
1961 // All discovery sessions should be inactive.
1962 for (int i = 0; i < 6; i++)
1963 EXPECT_FALSE(discovery_sessions_[i]->IsActive());
1965 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1966 EXPECT_EQ(nullptr, filter);
1969 // This unit test asserts that filter merging logic works correctly for filtered
1970 // discovery requests done via the BluetoothAdapter.
1971 TEST_F(BluetoothChromeOSTest, SetDiscoveryFilterMergingTest) {
1972 GetAdapter();
1973 adapter_->SetPowered(true, base::Bind(&BluetoothChromeOSTest::Callback,
1974 base::Unretained(this)),
1975 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1976 base::Unretained(this)));
1978 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter(
1979 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
1980 df->SetRSSI(-15);
1981 df->AddUUID(BluetoothUUID("1000"));
1982 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter(df);
1984 adapter_->StartDiscoverySessionWithFilter(
1985 discovery_filter.Pass(),
1986 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1987 base::Unretained(this)),
1988 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1989 base::Unretained(this)));
1991 message_loop_.Run();
1993 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1994 EXPECT_EQ("le", *filter->transport);
1995 EXPECT_EQ(-15, *filter->rssi);
1996 EXPECT_EQ(nullptr, filter->pathloss.get());
1997 std::vector<std::string> uuids = *filter->uuids;
1998 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1000"));
2000 df = new BluetoothDiscoveryFilter(
2001 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
2002 df->SetRSSI(-60);
2003 df->AddUUID(BluetoothUUID("1020"));
2004 df->AddUUID(BluetoothUUID("1001"));
2005 discovery_filter = scoped_ptr<BluetoothDiscoveryFilter>(df);
2007 adapter_->StartDiscoverySessionWithFilter(
2008 discovery_filter.Pass(),
2009 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
2010 base::Unretained(this)),
2011 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
2012 base::Unretained(this)));
2014 message_loop_.Run();
2016 filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
2017 EXPECT_EQ("le", *filter->transport);
2018 EXPECT_EQ(-60, *filter->rssi);
2019 EXPECT_EQ(nullptr, filter->pathloss.get());
2020 uuids = *filter->uuids;
2021 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1000"));
2022 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1001"));
2023 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1020"));
2025 BluetoothDiscoveryFilter* df3 = new BluetoothDiscoveryFilter(
2026 BluetoothDiscoveryFilter::Transport::TRANSPORT_CLASSIC);
2027 df3->SetRSSI(-65);
2028 df3->AddUUID(BluetoothUUID("1020"));
2029 df3->AddUUID(BluetoothUUID("1003"));
2030 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter3(df3);
2032 adapter_->StartDiscoverySessionWithFilter(
2033 discovery_filter3.Pass(),
2034 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
2035 base::Unretained(this)),
2036 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
2037 base::Unretained(this)));
2039 message_loop_.Run();
2041 filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
2042 EXPECT_EQ("auto", *filter->transport);
2043 EXPECT_EQ(-65, *filter->rssi);
2044 EXPECT_EQ(nullptr, filter->pathloss.get());
2045 uuids = *filter->uuids;
2046 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1000"));
2047 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1001"));
2048 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1003"));
2049 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1020"));
2051 // start additionally classic scan
2052 adapter_->StartDiscoverySession(
2053 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
2054 base::Unretained(this)),
2055 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
2056 base::Unretained(this)));
2058 message_loop_.Run();
2060 filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
2061 EXPECT_EQ("auto", *filter->transport);
2062 EXPECT_EQ(nullptr, filter->rssi.get());
2063 EXPECT_EQ(nullptr, filter->pathloss.get());
2064 EXPECT_EQ(nullptr, filter->uuids.get());
2066 // Request to stop discovery 4 times.
2067 for (int i = 3; i >= 0; i--) {
2068 discovery_sessions_[i]->Stop(
2069 base::Bind(&BluetoothChromeOSTest::Callback, base::Unretained(this)),
2070 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
2071 base::Unretained(this)));
2073 // Every session stopping would trigger filter update
2074 message_loop_.Run();
2078 TEST_F(BluetoothChromeOSTest, DeviceProperties) {
2079 GetAdapter();
2081 BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
2082 ASSERT_EQ(2U, devices.size());
2083 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
2084 devices[0]->GetAddress());
2086 // Verify the other device properties.
2087 EXPECT_EQ(base::UTF8ToUTF16(FakeBluetoothDeviceClient::kPairedDeviceName),
2088 devices[0]->GetName());
2089 EXPECT_EQ(BluetoothDevice::DEVICE_COMPUTER, devices[0]->GetDeviceType());
2090 EXPECT_TRUE(devices[0]->IsPaired());
2091 EXPECT_FALSE(devices[0]->IsConnected());
2092 EXPECT_FALSE(devices[0]->IsConnecting());
2094 // Non HID devices are always connectable.
2095 EXPECT_TRUE(devices[0]->IsConnectable());
2097 BluetoothDevice::UUIDList uuids = devices[0]->GetUUIDs();
2098 ASSERT_EQ(2U, uuids.size());
2099 EXPECT_EQ(uuids[0], BluetoothUUID("1800"));
2100 EXPECT_EQ(uuids[1], BluetoothUUID("1801"));
2102 EXPECT_EQ(BluetoothDevice::VENDOR_ID_USB, devices[0]->GetVendorIDSource());
2103 EXPECT_EQ(0x05ac, devices[0]->GetVendorID());
2104 EXPECT_EQ(0x030d, devices[0]->GetProductID());
2105 EXPECT_EQ(0x0306, devices[0]->GetDeviceID());
2108 TEST_F(BluetoothChromeOSTest, DeviceClassChanged) {
2109 // Simulate a change of class of a device, as sometimes occurs
2110 // during discovery.
2111 GetAdapter();
2113 BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
2114 ASSERT_EQ(2U, devices.size());
2115 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
2116 devices[0]->GetAddress());
2117 ASSERT_EQ(BluetoothDevice::DEVICE_COMPUTER, devices[0]->GetDeviceType());
2119 // Install an observer; expect the DeviceChanged method to be called when
2120 // we change the class of the device.
2121 TestBluetoothAdapterObserver observer(adapter_);
2123 FakeBluetoothDeviceClient::Properties* properties =
2124 fake_bluetooth_device_client_->GetProperties(
2125 dbus::ObjectPath(FakeBluetoothDeviceClient::kPairedDevicePath));
2127 properties->bluetooth_class.ReplaceValue(0x002580);
2129 EXPECT_EQ(1, observer.device_changed_count());
2130 EXPECT_EQ(devices[0], observer.last_device());
2132 EXPECT_EQ(BluetoothDevice::DEVICE_MOUSE, devices[0]->GetDeviceType());
2135 TEST_F(BluetoothChromeOSTest, DeviceNameChanged) {
2136 // Simulate a change of name of a device.
2137 GetAdapter();
2139 BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
2140 ASSERT_EQ(2U, devices.size());
2141 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
2142 devices[0]->GetAddress());
2143 ASSERT_EQ(base::UTF8ToUTF16(FakeBluetoothDeviceClient::kPairedDeviceName),
2144 devices[0]->GetName());
2146 // Install an observer; expect the DeviceChanged method to be called when
2147 // we change the alias of the device.
2148 TestBluetoothAdapterObserver observer(adapter_);
2150 FakeBluetoothDeviceClient::Properties* properties =
2151 fake_bluetooth_device_client_->GetProperties(
2152 dbus::ObjectPath(FakeBluetoothDeviceClient::kPairedDevicePath));
2154 static const std::string new_name("New Device Name");
2155 properties->alias.ReplaceValue(new_name);
2157 EXPECT_EQ(1, observer.device_changed_count());
2158 EXPECT_EQ(devices[0], observer.last_device());
2160 EXPECT_EQ(base::UTF8ToUTF16(new_name), devices[0]->GetName());
2163 TEST_F(BluetoothChromeOSTest, DeviceUuidsChanged) {
2164 // Simulate a change of advertised services of a device.
2165 GetAdapter();
2167 BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
2168 ASSERT_EQ(2U, devices.size());
2169 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
2170 devices[0]->GetAddress());
2172 BluetoothDevice::UUIDList uuids = devices[0]->GetUUIDs();
2173 ASSERT_EQ(2U, uuids.size());
2174 ASSERT_EQ(uuids[0], BluetoothUUID("1800"));
2175 ASSERT_EQ(uuids[1], BluetoothUUID("1801"));
2177 // Install an observer; expect the DeviceChanged method to be called when
2178 // we change the class of the device.
2179 TestBluetoothAdapterObserver observer(adapter_);
2181 FakeBluetoothDeviceClient::Properties* properties =
2182 fake_bluetooth_device_client_->GetProperties(
2183 dbus::ObjectPath(FakeBluetoothDeviceClient::kPairedDevicePath));
2185 std::vector<std::string> new_uuids;
2186 new_uuids.push_back(uuids[0].canonical_value());
2187 new_uuids.push_back(uuids[1].canonical_value());
2188 new_uuids.push_back("0000110c-0000-1000-8000-00805f9b34fb");
2189 new_uuids.push_back("0000110e-0000-1000-8000-00805f9b34fb");
2190 new_uuids.push_back("0000110a-0000-1000-8000-00805f9b34fb");
2192 properties->uuids.ReplaceValue(new_uuids);
2194 EXPECT_EQ(1, observer.device_changed_count());
2195 EXPECT_EQ(devices[0], observer.last_device());
2197 // Fetching the value should give the new one.
2198 uuids = devices[0]->GetUUIDs();
2199 ASSERT_EQ(5U, uuids.size());
2200 EXPECT_EQ(uuids[0], BluetoothUUID("1800"));
2201 EXPECT_EQ(uuids[1], BluetoothUUID("1801"));
2202 EXPECT_EQ(uuids[2], BluetoothUUID("110c"));
2203 EXPECT_EQ(uuids[3], BluetoothUUID("110e"));
2204 EXPECT_EQ(uuids[4], BluetoothUUID("110a"));
2207 TEST_F(BluetoothChromeOSTest, DeviceInquiryRSSIInvalidated) {
2208 // Simulate invalidation of inquiry RSSI of a device, as it occurs
2209 // when discovery is finished.
2210 GetAdapter();
2212 BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
2213 ASSERT_EQ(2U, devices.size());
2214 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
2215 devices[0]->GetAddress());
2217 FakeBluetoothDeviceClient::Properties* properties =
2218 fake_bluetooth_device_client_->GetProperties(
2219 dbus::ObjectPath(FakeBluetoothDeviceClient::kPairedDevicePath));
2221 // During discovery, rssi is a valid value (-75)
2222 properties->rssi.ReplaceValue(-75);
2223 properties->rssi.set_valid(true);
2225 ASSERT_EQ(-75, devices[0]->GetInquiryRSSI());
2227 // Install an observer; expect the DeviceChanged method to be called when
2228 // we invalidate the RSSI of the device.
2229 TestBluetoothAdapterObserver observer(adapter_);
2231 // When discovery is over, the value should be invalidated.
2232 properties->rssi.set_valid(false);
2233 properties->NotifyPropertyChanged(properties->rssi.name());
2235 EXPECT_EQ(1, observer.device_changed_count());
2236 EXPECT_EQ(devices[0], observer.last_device());
2238 int unknown_power = BluetoothDevice::kUnknownPower;
2239 EXPECT_EQ(unknown_power, devices[0]->GetInquiryRSSI());
2242 TEST_F(BluetoothChromeOSTest, DeviceInquiryTxPowerInvalidated) {
2243 // Simulate invalidation of inquiry TxPower of a device, as it occurs
2244 // when discovery is finished.
2245 GetAdapter();
2247 BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
2248 ASSERT_EQ(2U, devices.size());
2249 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
2250 devices[0]->GetAddress());
2252 FakeBluetoothDeviceClient::Properties* properties =
2253 fake_bluetooth_device_client_->GetProperties(
2254 dbus::ObjectPath(FakeBluetoothDeviceClient::kPairedDevicePath));
2256 // During discovery, tx_power is a valid value (0)
2257 properties->tx_power.ReplaceValue(0);
2258 properties->tx_power.set_valid(true);
2260 ASSERT_EQ(0, devices[0]->GetInquiryTxPower());
2262 // Install an observer; expect the DeviceChanged method to be called when
2263 // we invalidate the tx_power of the device.
2264 TestBluetoothAdapterObserver observer(adapter_);
2266 // When discovery is over, the value should be invalidated.
2267 properties->tx_power.set_valid(false);
2268 properties->NotifyPropertyChanged(properties->tx_power.name());
2270 EXPECT_EQ(1, observer.device_changed_count());
2271 EXPECT_EQ(devices[0], observer.last_device());
2273 int unknown_power = BluetoothDevice::kUnknownPower;
2274 EXPECT_EQ(unknown_power, devices[0]->GetInquiryTxPower());
2277 TEST_F(BluetoothChromeOSTest, ForgetDevice) {
2278 GetAdapter();
2280 BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
2281 ASSERT_EQ(2U, devices.size());
2282 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
2283 devices[0]->GetAddress());
2285 std::string address = devices[0]->GetAddress();
2287 // Install an observer; expect the DeviceRemoved method to be called
2288 // with the device we remove.
2289 TestBluetoothAdapterObserver observer(adapter_);
2291 devices[0]->Forget(GetErrorCallback());
2292 EXPECT_EQ(0, error_callback_count_);
2294 EXPECT_EQ(1, observer.device_removed_count());
2295 EXPECT_EQ(address, observer.last_device_address());
2297 // GetDevices shouldn't return the device either.
2298 devices = adapter_->GetDevices();
2299 ASSERT_EQ(1U, devices.size());
2302 TEST_F(BluetoothChromeOSTest, ForgetUnpairedDevice) {
2303 GetAdapter();
2304 DiscoverDevices();
2306 BluetoothDevice* device = adapter_->GetDevice(
2307 FakeBluetoothDeviceClient::kConnectUnpairableAddress);
2308 ASSERT_TRUE(device != NULL);
2309 ASSERT_FALSE(device->IsPaired());
2311 // Connect the device so it becomes trusted and remembered.
2312 device->Connect(NULL, GetCallback(),
2313 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2314 base::Unretained(this)));
2316 ASSERT_EQ(1, callback_count_);
2317 ASSERT_EQ(0, error_callback_count_);
2318 callback_count_ = 0;
2320 ASSERT_TRUE(device->IsConnected());
2321 ASSERT_FALSE(device->IsConnecting());
2323 // Make sure the trusted property has been set to true.
2324 FakeBluetoothDeviceClient::Properties* properties =
2325 fake_bluetooth_device_client_->GetProperties(
2326 dbus::ObjectPath(FakeBluetoothDeviceClient::kConnectUnpairablePath));
2327 ASSERT_TRUE(properties->trusted.value());
2329 // Install an observer; expect the DeviceRemoved method to be called
2330 // with the device we remove.
2331 TestBluetoothAdapterObserver observer(adapter_);
2333 device->Forget(GetErrorCallback());
2334 EXPECT_EQ(0, error_callback_count_);
2336 EXPECT_EQ(1, observer.device_removed_count());
2337 EXPECT_EQ(FakeBluetoothDeviceClient::kConnectUnpairableAddress,
2338 observer.last_device_address());
2340 // GetDevices shouldn't return the device either.
2341 device = adapter_->GetDevice(
2342 FakeBluetoothDeviceClient::kConnectUnpairableAddress);
2343 EXPECT_FALSE(device != NULL);
2346 TEST_F(BluetoothChromeOSTest, ConnectPairedDevice) {
2347 GetAdapter();
2349 BluetoothDevice* device = adapter_->GetDevice(
2350 FakeBluetoothDeviceClient::kPairedDeviceAddress);
2351 ASSERT_TRUE(device != NULL);
2352 ASSERT_TRUE(device->IsPaired());
2354 TestBluetoothAdapterObserver observer(adapter_);
2356 // Connect without a pairing delegate; since the device is already Paired
2357 // this should succeed and the device should become connected.
2358 device->Connect(NULL, GetCallback(),
2359 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2360 base::Unretained(this)));
2362 EXPECT_EQ(1, callback_count_);
2363 EXPECT_EQ(0, error_callback_count_);
2365 // Two changes for connecting, one for connected and one for for trusted
2366 // after connecting.
2367 EXPECT_EQ(4, observer.device_changed_count());
2368 EXPECT_EQ(device, observer.last_device());
2370 EXPECT_TRUE(device->IsConnected());
2371 EXPECT_FALSE(device->IsConnecting());
2374 TEST_F(BluetoothChromeOSTest, ConnectUnpairableDevice) {
2375 GetAdapter();
2376 DiscoverDevices();
2378 BluetoothDevice* device = adapter_->GetDevice(
2379 FakeBluetoothDeviceClient::kConnectUnpairableAddress);
2380 ASSERT_TRUE(device != NULL);
2381 ASSERT_FALSE(device->IsPaired());
2383 TestBluetoothAdapterObserver observer(adapter_);
2385 // Connect without a pairing delegate; since the device does not require
2386 // pairing, this should succeed and the device should become connected.
2387 device->Connect(NULL, GetCallback(),
2388 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2389 base::Unretained(this)));
2391 EXPECT_EQ(1, callback_count_);
2392 EXPECT_EQ(0, error_callback_count_);
2394 // Two changes for connecting, one for connected, one for for trusted after
2395 // connection, and one for the reconnect mode (IsConnectable).
2396 EXPECT_EQ(5, observer.device_changed_count());
2397 EXPECT_EQ(device, observer.last_device());
2399 EXPECT_TRUE(device->IsConnected());
2400 EXPECT_FALSE(device->IsConnecting());
2402 // Make sure the trusted property has been set to true.
2403 FakeBluetoothDeviceClient::Properties* properties =
2404 fake_bluetooth_device_client_->GetProperties(
2405 dbus::ObjectPath(FakeBluetoothDeviceClient::kConnectUnpairablePath));
2406 EXPECT_TRUE(properties->trusted.value());
2408 // Verify is a HID device and is not connectable.
2409 BluetoothDevice::UUIDList uuids = device->GetUUIDs();
2410 ASSERT_EQ(1U, uuids.size());
2411 EXPECT_EQ(uuids[0], BluetoothUUID("1124"));
2412 EXPECT_FALSE(device->IsConnectable());
2415 TEST_F(BluetoothChromeOSTest, ConnectConnectedDevice) {
2416 GetAdapter();
2418 BluetoothDevice* device = adapter_->GetDevice(
2419 FakeBluetoothDeviceClient::kPairedDeviceAddress);
2420 ASSERT_TRUE(device != NULL);
2421 ASSERT_TRUE(device->IsPaired());
2423 device->Connect(NULL, GetCallback(),
2424 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2425 base::Unretained(this)));
2427 ASSERT_EQ(1, callback_count_);
2428 ASSERT_EQ(0, error_callback_count_);
2429 callback_count_ = 0;
2431 ASSERT_TRUE(device->IsConnected());
2433 // Connect again; since the device is already Connected, this shouldn't do
2434 // anything to initiate the connection.
2435 TestBluetoothAdapterObserver observer(adapter_);
2437 device->Connect(NULL, GetCallback(),
2438 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2439 base::Unretained(this)));
2441 EXPECT_EQ(1, callback_count_);
2442 EXPECT_EQ(0, error_callback_count_);
2444 // The observer will be called because Connecting will toggle true and false,
2445 // and the trusted property will be updated to true.
2446 EXPECT_EQ(3, observer.device_changed_count());
2448 EXPECT_TRUE(device->IsConnected());
2449 EXPECT_FALSE(device->IsConnecting());
2452 TEST_F(BluetoothChromeOSTest, ConnectDeviceFails) {
2453 GetAdapter();
2454 DiscoverDevices();
2456 BluetoothDevice* device = adapter_->GetDevice(
2457 FakeBluetoothDeviceClient::kLegacyAutopairAddress);
2458 ASSERT_TRUE(device != NULL);
2459 ASSERT_FALSE(device->IsPaired());
2461 TestBluetoothAdapterObserver observer(adapter_);
2463 // Connect without a pairing delegate; since the device requires pairing,
2464 // this should fail with an error.
2465 device->Connect(NULL, GetCallback(),
2466 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2467 base::Unretained(this)));
2469 EXPECT_EQ(0, callback_count_);
2470 EXPECT_EQ(1, error_callback_count_);
2471 EXPECT_EQ(BluetoothDevice::ERROR_FAILED, last_connect_error_);
2473 EXPECT_EQ(2, observer.device_changed_count());
2475 EXPECT_FALSE(device->IsConnected());
2476 EXPECT_FALSE(device->IsConnecting());
2479 TEST_F(BluetoothChromeOSTest, DisconnectDevice) {
2480 GetAdapter();
2482 BluetoothDevice* device = adapter_->GetDevice(
2483 FakeBluetoothDeviceClient::kPairedDeviceAddress);
2484 ASSERT_TRUE(device != NULL);
2485 ASSERT_TRUE(device->IsPaired());
2487 device->Connect(NULL, GetCallback(),
2488 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2489 base::Unretained(this)));
2491 ASSERT_EQ(1, callback_count_);
2492 ASSERT_EQ(0, error_callback_count_);
2493 callback_count_ = 0;
2495 ASSERT_TRUE(device->IsConnected());
2496 ASSERT_FALSE(device->IsConnecting());
2498 // Disconnect the device, we should see the observer method fire and the
2499 // device get dropped.
2500 TestBluetoothAdapterObserver observer(adapter_);
2502 device->Disconnect(GetCallback(), GetErrorCallback());
2504 EXPECT_EQ(1, callback_count_);
2505 EXPECT_EQ(0, error_callback_count_);
2507 EXPECT_EQ(1, observer.device_changed_count());
2508 EXPECT_EQ(device, observer.last_device());
2510 EXPECT_FALSE(device->IsConnected());
2513 TEST_F(BluetoothChromeOSTest, DisconnectUnconnectedDevice) {
2514 GetAdapter();
2516 BluetoothDevice* device = adapter_->GetDevice(
2517 FakeBluetoothDeviceClient::kPairedDeviceAddress);
2518 ASSERT_TRUE(device != NULL);
2519 ASSERT_TRUE(device->IsPaired());
2520 ASSERT_FALSE(device->IsConnected());
2522 // Disconnect the device, we should see the observer method fire and the
2523 // device get dropped.
2524 TestBluetoothAdapterObserver observer(adapter_);
2526 device->Disconnect(GetCallback(), GetErrorCallback());
2528 EXPECT_EQ(0, callback_count_);
2529 EXPECT_EQ(1, error_callback_count_);
2531 EXPECT_EQ(0, observer.device_changed_count());
2533 EXPECT_FALSE(device->IsConnected());
2536 TEST_F(BluetoothChromeOSTest, PairLegacyAutopair) {
2537 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2539 GetAdapter();
2540 DiscoverDevices();
2542 // The Legacy Autopair device requires no PIN or Passkey to pair because
2543 // the daemon provides 0000 to the device for us.
2544 BluetoothDevice* device = adapter_->GetDevice(
2545 FakeBluetoothDeviceClient::kLegacyAutopairAddress);
2546 ASSERT_TRUE(device != NULL);
2547 ASSERT_FALSE(device->IsPaired());
2549 TestBluetoothAdapterObserver observer(adapter_);
2551 TestPairingDelegate pairing_delegate;
2552 device->Connect(&pairing_delegate, GetCallback(),
2553 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2554 base::Unretained(this)));
2556 EXPECT_EQ(0, pairing_delegate.call_count_);
2557 EXPECT_TRUE(device->IsConnecting());
2559 message_loop_.Run();
2561 EXPECT_EQ(1, callback_count_);
2562 EXPECT_EQ(0, error_callback_count_);
2564 // Two changes for connecting, one change for connected, one for paired,
2565 // two for trusted (after pairing and connection), and one for the reconnect
2566 // mode (IsConnectable).
2567 EXPECT_EQ(7, observer.device_changed_count());
2568 EXPECT_EQ(device, observer.last_device());
2570 EXPECT_TRUE(device->IsConnected());
2571 EXPECT_FALSE(device->IsConnecting());
2573 EXPECT_TRUE(device->IsPaired());
2575 // Verify is a HID device and is connectable.
2576 BluetoothDevice::UUIDList uuids = device->GetUUIDs();
2577 ASSERT_EQ(1U, uuids.size());
2578 EXPECT_EQ(uuids[0], BluetoothUUID("1124"));
2579 EXPECT_TRUE(device->IsConnectable());
2581 // Make sure the trusted property has been set to true.
2582 FakeBluetoothDeviceClient::Properties* properties =
2583 fake_bluetooth_device_client_->GetProperties(
2584 dbus::ObjectPath(FakeBluetoothDeviceClient::kLegacyAutopairPath));
2585 EXPECT_TRUE(properties->trusted.value());
2588 TEST_F(BluetoothChromeOSTest, PairDisplayPinCode) {
2589 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2591 GetAdapter();
2592 DiscoverDevices();
2594 // Requires that we display a randomly generated PIN on the screen.
2595 BluetoothDevice* device = adapter_->GetDevice(
2596 FakeBluetoothDeviceClient::kDisplayPinCodeAddress);
2597 ASSERT_TRUE(device != NULL);
2598 ASSERT_FALSE(device->IsPaired());
2600 TestBluetoothAdapterObserver observer(adapter_);
2602 TestPairingDelegate pairing_delegate;
2603 device->Connect(&pairing_delegate, GetCallback(),
2604 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2605 base::Unretained(this)));
2607 EXPECT_EQ(1, pairing_delegate.call_count_);
2608 EXPECT_EQ(1, pairing_delegate.display_pincode_count_);
2609 EXPECT_EQ("123456", pairing_delegate.last_pincode_);
2610 EXPECT_TRUE(device->IsConnecting());
2612 message_loop_.Run();
2614 EXPECT_EQ(1, callback_count_);
2615 EXPECT_EQ(0, error_callback_count_);
2617 // Two changes for connecting, one change for connected, one for paired,
2618 // two for trusted (after pairing and connection), and one for the reconnect
2619 // mode (IsConnectable).
2620 EXPECT_EQ(7, observer.device_changed_count());
2621 EXPECT_EQ(device, observer.last_device());
2623 EXPECT_TRUE(device->IsConnected());
2624 EXPECT_FALSE(device->IsConnecting());
2626 EXPECT_TRUE(device->IsPaired());
2628 // Verify is a HID device and is connectable.
2629 BluetoothDevice::UUIDList uuids = device->GetUUIDs();
2630 ASSERT_EQ(1U, uuids.size());
2631 EXPECT_EQ(uuids[0], BluetoothUUID("1124"));
2632 EXPECT_TRUE(device->IsConnectable());
2634 // Make sure the trusted property has been set to true.
2635 FakeBluetoothDeviceClient::Properties* properties =
2636 fake_bluetooth_device_client_->GetProperties(
2637 dbus::ObjectPath(FakeBluetoothDeviceClient::kDisplayPinCodePath));
2638 EXPECT_TRUE(properties->trusted.value());
2641 TEST_F(BluetoothChromeOSTest, PairDisplayPasskey) {
2642 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2644 GetAdapter();
2645 DiscoverDevices();
2647 // Requires that we display a randomly generated Passkey on the screen,
2648 // and notifies us as it's typed in.
2649 BluetoothDevice* device = adapter_->GetDevice(
2650 FakeBluetoothDeviceClient::kDisplayPasskeyAddress);
2651 ASSERT_TRUE(device != NULL);
2652 ASSERT_FALSE(device->IsPaired());
2654 TestBluetoothAdapterObserver observer(adapter_);
2656 TestPairingDelegate pairing_delegate;
2657 device->Connect(&pairing_delegate, GetCallback(),
2658 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2659 base::Unretained(this)));
2661 // One call for DisplayPasskey() and one for KeysEntered().
2662 EXPECT_EQ(2, pairing_delegate.call_count_);
2663 EXPECT_EQ(1, pairing_delegate.display_passkey_count_);
2664 EXPECT_EQ(123456U, pairing_delegate.last_passkey_);
2665 EXPECT_EQ(1, pairing_delegate.keys_entered_count_);
2666 EXPECT_EQ(0U, pairing_delegate.last_entered_);
2668 EXPECT_TRUE(device->IsConnecting());
2670 // One call to KeysEntered() for each key, including [enter].
2671 for(int i = 1; i <= 7; ++i) {
2672 message_loop_.Run();
2674 EXPECT_EQ(2 + i, pairing_delegate.call_count_);
2675 EXPECT_EQ(1 + i, pairing_delegate.keys_entered_count_);
2676 EXPECT_EQ(static_cast<uint32_t>(i), pairing_delegate.last_entered_);
2679 message_loop_.Run();
2681 // 8 KeysEntered notifications (0 to 7, inclusive) and one aditional call for
2682 // DisplayPasskey().
2683 EXPECT_EQ(9, pairing_delegate.call_count_);
2684 EXPECT_EQ(8, pairing_delegate.keys_entered_count_);
2685 EXPECT_EQ(7U, pairing_delegate.last_entered_);
2687 EXPECT_EQ(1, callback_count_);
2688 EXPECT_EQ(0, error_callback_count_);
2690 // Two changes for connecting, one change for connected, one for paired,
2691 // two for trusted (after pairing and connection), and one for the reconnect
2692 // mode (IsConnectable).
2693 EXPECT_EQ(7, observer.device_changed_count());
2694 EXPECT_EQ(device, observer.last_device());
2696 EXPECT_TRUE(device->IsConnected());
2697 EXPECT_FALSE(device->IsConnecting());
2699 EXPECT_TRUE(device->IsPaired());
2701 // Verify is a HID device.
2702 BluetoothDevice::UUIDList uuids = device->GetUUIDs();
2703 ASSERT_EQ(1U, uuids.size());
2704 EXPECT_EQ(uuids[0], BluetoothUUID("1124"));
2706 // And usually not connectable.
2707 EXPECT_FALSE(device->IsConnectable());
2709 // Make sure the trusted property has been set to true.
2710 FakeBluetoothDeviceClient::Properties* properties =
2711 fake_bluetooth_device_client_->GetProperties(
2712 dbus::ObjectPath(FakeBluetoothDeviceClient::kDisplayPasskeyPath));
2713 EXPECT_TRUE(properties->trusted.value());
2716 TEST_F(BluetoothChromeOSTest, PairRequestPinCode) {
2717 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2719 GetAdapter();
2720 DiscoverDevices();
2722 // Requires that the user enters a PIN for them.
2723 BluetoothDevice* device = adapter_->GetDevice(
2724 FakeBluetoothDeviceClient::kRequestPinCodeAddress);
2725 ASSERT_TRUE(device != NULL);
2726 ASSERT_FALSE(device->IsPaired());
2728 TestBluetoothAdapterObserver observer(adapter_);
2730 TestPairingDelegate pairing_delegate;
2731 device->Connect(&pairing_delegate, GetCallback(),
2732 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2733 base::Unretained(this)));
2735 EXPECT_EQ(1, pairing_delegate.call_count_);
2736 EXPECT_EQ(1, pairing_delegate.request_pincode_count_);
2737 EXPECT_TRUE(device->IsConnecting());
2739 // Set the PIN.
2740 device->SetPinCode("1234");
2741 message_loop_.Run();
2743 EXPECT_EQ(1, callback_count_);
2744 EXPECT_EQ(0, error_callback_count_);
2746 // Two changes for connecting, one change for connected, one for paired and
2747 // two for trusted (after pairing and connection).
2748 EXPECT_EQ(6, observer.device_changed_count());
2749 EXPECT_EQ(device, observer.last_device());
2751 EXPECT_TRUE(device->IsConnected());
2752 EXPECT_FALSE(device->IsConnecting());
2754 EXPECT_TRUE(device->IsPaired());
2756 // Verify is not a HID device.
2757 BluetoothDevice::UUIDList uuids = device->GetUUIDs();
2758 ASSERT_EQ(0U, uuids.size());
2760 // Non HID devices are always connectable.
2761 EXPECT_TRUE(device->IsConnectable());
2763 // Make sure the trusted property has been set to true.
2764 FakeBluetoothDeviceClient::Properties* properties =
2765 fake_bluetooth_device_client_->GetProperties(
2766 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath));
2767 EXPECT_TRUE(properties->trusted.value());
2770 TEST_F(BluetoothChromeOSTest, PairConfirmPasskey) {
2771 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2773 GetAdapter();
2774 DiscoverDevices();
2776 // Requests that we confirm a displayed passkey.
2777 BluetoothDevice* device = adapter_->GetDevice(
2778 FakeBluetoothDeviceClient::kConfirmPasskeyAddress);
2779 ASSERT_TRUE(device != NULL);
2780 ASSERT_FALSE(device->IsPaired());
2782 TestBluetoothAdapterObserver observer(adapter_);
2784 TestPairingDelegate pairing_delegate;
2785 device->Connect(&pairing_delegate, GetCallback(),
2786 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2787 base::Unretained(this)));
2789 EXPECT_EQ(1, pairing_delegate.call_count_);
2790 EXPECT_EQ(1, pairing_delegate.confirm_passkey_count_);
2791 EXPECT_EQ(123456U, pairing_delegate.last_passkey_);
2792 EXPECT_TRUE(device->IsConnecting());
2794 // Confirm the passkey.
2795 device->ConfirmPairing();
2796 message_loop_.Run();
2798 EXPECT_EQ(1, callback_count_);
2799 EXPECT_EQ(0, error_callback_count_);
2801 // Two changes for connecting, one change for connected, one for paired and
2802 // two for trusted (after pairing and connection).
2803 EXPECT_EQ(6, observer.device_changed_count());
2804 EXPECT_EQ(device, observer.last_device());
2806 EXPECT_TRUE(device->IsConnected());
2807 EXPECT_FALSE(device->IsConnecting());
2809 EXPECT_TRUE(device->IsPaired());
2811 // Non HID devices are always connectable.
2812 EXPECT_TRUE(device->IsConnectable());
2814 // Make sure the trusted property has been set to true.
2815 FakeBluetoothDeviceClient::Properties* properties =
2816 fake_bluetooth_device_client_->GetProperties(
2817 dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath));
2818 EXPECT_TRUE(properties->trusted.value());
2821 TEST_F(BluetoothChromeOSTest, PairRequestPasskey) {
2822 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2824 GetAdapter();
2825 DiscoverDevices();
2827 // Requires that the user enters a Passkey, this would be some kind of
2828 // device that has a display, but doesn't use "just works" - maybe a car?
2829 BluetoothDevice* device = adapter_->GetDevice(
2830 FakeBluetoothDeviceClient::kRequestPasskeyAddress);
2831 ASSERT_TRUE(device != NULL);
2832 ASSERT_FALSE(device->IsPaired());
2834 TestBluetoothAdapterObserver observer(adapter_);
2836 TestPairingDelegate pairing_delegate;
2837 device->Connect(&pairing_delegate, GetCallback(),
2838 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2839 base::Unretained(this)));
2841 EXPECT_EQ(1, pairing_delegate.call_count_);
2842 EXPECT_EQ(1, pairing_delegate.request_passkey_count_);
2843 EXPECT_TRUE(device->IsConnecting());
2845 // Set the Passkey.
2846 device->SetPasskey(1234);
2847 message_loop_.Run();
2849 EXPECT_EQ(1, callback_count_);
2850 EXPECT_EQ(0, error_callback_count_);
2852 // Two changes for connecting, one change for connected, one for paired and
2853 // two for trusted (after pairing and connection).
2854 EXPECT_EQ(6, observer.device_changed_count());
2855 EXPECT_EQ(device, observer.last_device());
2857 EXPECT_TRUE(device->IsConnected());
2858 EXPECT_FALSE(device->IsConnecting());
2860 EXPECT_TRUE(device->IsPaired());
2862 // Non HID devices are always connectable.
2863 EXPECT_TRUE(device->IsConnectable());
2865 // Make sure the trusted property has been set to true.
2866 FakeBluetoothDeviceClient::Properties* properties =
2867 fake_bluetooth_device_client_->GetProperties(
2868 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath));
2869 EXPECT_TRUE(properties->trusted.value());
2872 TEST_F(BluetoothChromeOSTest, PairJustWorks) {
2873 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2875 GetAdapter();
2876 DiscoverDevices();
2878 // Uses just-works pairing, since this is an outgoing pairing, no delegate
2879 // interaction is required.
2880 BluetoothDevice* device = adapter_->GetDevice(
2881 FakeBluetoothDeviceClient::kJustWorksAddress);
2882 ASSERT_TRUE(device != NULL);
2883 ASSERT_FALSE(device->IsPaired());
2885 TestBluetoothAdapterObserver observer(adapter_);
2887 TestPairingDelegate pairing_delegate;
2888 device->Connect(&pairing_delegate, GetCallback(),
2889 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2890 base::Unretained(this)));
2892 EXPECT_EQ(0, pairing_delegate.call_count_);
2894 message_loop_.Run();
2896 EXPECT_EQ(1, callback_count_);
2897 EXPECT_EQ(0, error_callback_count_);
2899 // Two changes for connecting, one change for connected, one for paired and
2900 // two for trusted (after pairing and connection).
2901 EXPECT_EQ(6, observer.device_changed_count());
2902 EXPECT_EQ(device, observer.last_device());
2904 EXPECT_TRUE(device->IsConnected());
2905 EXPECT_FALSE(device->IsConnecting());
2907 EXPECT_TRUE(device->IsPaired());
2909 // Non HID devices are always connectable.
2910 EXPECT_TRUE(device->IsConnectable());
2912 // Make sure the trusted property has been set to true.
2913 FakeBluetoothDeviceClient::Properties* properties =
2914 fake_bluetooth_device_client_->GetProperties(
2915 dbus::ObjectPath(FakeBluetoothDeviceClient::kJustWorksPath));
2916 EXPECT_TRUE(properties->trusted.value());
2919 TEST_F(BluetoothChromeOSTest, PairUnpairableDeviceFails) {
2920 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2922 GetAdapter();
2923 DiscoverDevice(FakeBluetoothDeviceClient::kUnconnectableDeviceAddress);
2925 BluetoothDevice* device = adapter_->GetDevice(
2926 FakeBluetoothDeviceClient::kUnpairableDeviceAddress);
2927 ASSERT_TRUE(device != NULL);
2928 ASSERT_FALSE(device->IsPaired());
2930 TestBluetoothAdapterObserver observer(adapter_);
2932 TestPairingDelegate pairing_delegate;
2933 device->Connect(&pairing_delegate, GetCallback(),
2934 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2935 base::Unretained(this)));
2937 EXPECT_EQ(0, pairing_delegate.call_count_);
2938 EXPECT_TRUE(device->IsConnecting());
2940 // Run the loop to get the error..
2941 message_loop_.Run();
2943 EXPECT_EQ(0, callback_count_);
2944 EXPECT_EQ(1, error_callback_count_);
2946 EXPECT_EQ(BluetoothDevice::ERROR_FAILED, last_connect_error_);
2948 EXPECT_FALSE(device->IsConnected());
2949 EXPECT_FALSE(device->IsConnecting());
2950 EXPECT_FALSE(device->IsPaired());
2953 TEST_F(BluetoothChromeOSTest, PairingFails) {
2954 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2956 GetAdapter();
2957 DiscoverDevice(FakeBluetoothDeviceClient::kVanishingDeviceAddress);
2959 // The vanishing device times out during pairing
2960 BluetoothDevice* device = adapter_->GetDevice(
2961 FakeBluetoothDeviceClient::kVanishingDeviceAddress);
2962 ASSERT_TRUE(device != NULL);
2963 ASSERT_FALSE(device->IsPaired());
2965 TestBluetoothAdapterObserver observer(adapter_);
2967 TestPairingDelegate pairing_delegate;
2968 device->Connect(&pairing_delegate, GetCallback(),
2969 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2970 base::Unretained(this)));
2972 EXPECT_EQ(0, pairing_delegate.call_count_);
2973 EXPECT_TRUE(device->IsConnecting());
2975 // Run the loop to get the error..
2976 message_loop_.Run();
2978 EXPECT_EQ(0, callback_count_);
2979 EXPECT_EQ(1, error_callback_count_);
2981 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_TIMEOUT, last_connect_error_);
2983 EXPECT_FALSE(device->IsConnected());
2984 EXPECT_FALSE(device->IsConnecting());
2985 EXPECT_FALSE(device->IsPaired());
2988 TEST_F(BluetoothChromeOSTest, PairingFailsAtConnection) {
2989 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2991 GetAdapter();
2992 DiscoverDevices();
2994 // Everything seems to go according to plan with the unconnectable device;
2995 // it pairs, but then you can't make connections to it after.
2996 BluetoothDevice* device = adapter_->GetDevice(
2997 FakeBluetoothDeviceClient::kUnconnectableDeviceAddress);
2998 ASSERT_TRUE(device != NULL);
2999 ASSERT_FALSE(device->IsPaired());
3001 TestBluetoothAdapterObserver observer(adapter_);
3003 TestPairingDelegate pairing_delegate;
3004 device->Connect(&pairing_delegate, GetCallback(),
3005 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
3006 base::Unretained(this)));
3008 EXPECT_EQ(0, pairing_delegate.call_count_);
3009 EXPECT_TRUE(device->IsConnecting());
3011 message_loop_.Run();
3013 EXPECT_EQ(0, callback_count_);
3014 EXPECT_EQ(1, error_callback_count_);
3015 EXPECT_EQ(BluetoothDevice::ERROR_FAILED, last_connect_error_);
3017 // Two changes for connecting, one for paired and one for trusted after
3018 // pairing. The device should not be connected.
3019 EXPECT_EQ(4, observer.device_changed_count());
3020 EXPECT_EQ(device, observer.last_device());
3022 EXPECT_FALSE(device->IsConnected());
3023 EXPECT_FALSE(device->IsConnecting());
3025 EXPECT_TRUE(device->IsPaired());
3027 // Make sure the trusted property has been set to true still (since pairing
3028 // worked).
3029 FakeBluetoothDeviceClient::Properties* properties =
3030 fake_bluetooth_device_client_->GetProperties(
3031 dbus::ObjectPath(
3032 FakeBluetoothDeviceClient::kUnconnectableDevicePath));
3033 EXPECT_TRUE(properties->trusted.value());
3036 TEST_F(BluetoothChromeOSTest, PairingRejectedAtPinCode) {
3037 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3039 GetAdapter();
3040 DiscoverDevices();
3042 // Reject the pairing after we receive a request for the PIN code.
3043 BluetoothDevice* device = adapter_->GetDevice(
3044 FakeBluetoothDeviceClient::kRequestPinCodeAddress);
3045 ASSERT_TRUE(device != NULL);
3046 ASSERT_FALSE(device->IsPaired());
3048 TestBluetoothAdapterObserver observer(adapter_);
3050 TestPairingDelegate pairing_delegate;
3051 device->Connect(&pairing_delegate, GetCallback(),
3052 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
3053 base::Unretained(this)));
3055 EXPECT_EQ(1, pairing_delegate.call_count_);
3056 EXPECT_EQ(1, pairing_delegate.request_pincode_count_);
3057 EXPECT_TRUE(device->IsConnecting());
3059 // Reject the pairing.
3060 device->RejectPairing();
3061 message_loop_.Run();
3063 EXPECT_EQ(0, callback_count_);
3064 EXPECT_EQ(1, error_callback_count_);
3065 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_REJECTED, last_connect_error_);
3067 // Should be no changes except connecting going true and false.
3068 EXPECT_EQ(2, observer.device_changed_count());
3069 EXPECT_FALSE(device->IsConnected());
3070 EXPECT_FALSE(device->IsConnecting());
3071 EXPECT_FALSE(device->IsPaired());
3074 TEST_F(BluetoothChromeOSTest, PairingCancelledAtPinCode) {
3075 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3077 GetAdapter();
3078 DiscoverDevices();
3080 // Cancel the pairing after we receive a request for the PIN code.
3081 BluetoothDevice* device = adapter_->GetDevice(
3082 FakeBluetoothDeviceClient::kRequestPinCodeAddress);
3083 ASSERT_TRUE(device != NULL);
3084 ASSERT_FALSE(device->IsPaired());
3086 TestBluetoothAdapterObserver observer(adapter_);
3088 TestPairingDelegate pairing_delegate;
3089 device->Connect(&pairing_delegate, GetCallback(),
3090 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
3091 base::Unretained(this)));
3093 EXPECT_EQ(1, pairing_delegate.call_count_);
3094 EXPECT_EQ(1, pairing_delegate.request_pincode_count_);
3095 EXPECT_TRUE(device->IsConnecting());
3097 // Cancel the pairing.
3098 device->CancelPairing();
3099 message_loop_.Run();
3101 EXPECT_EQ(0, callback_count_);
3102 EXPECT_EQ(1, error_callback_count_);
3103 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED, last_connect_error_);
3105 // Should be no changes except connecting going true and false.
3106 EXPECT_EQ(2, observer.device_changed_count());
3107 EXPECT_FALSE(device->IsConnected());
3108 EXPECT_FALSE(device->IsConnecting());
3109 EXPECT_FALSE(device->IsPaired());
3112 TEST_F(BluetoothChromeOSTest, PairingRejectedAtPasskey) {
3113 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3115 GetAdapter();
3116 DiscoverDevices();
3118 // Reject the pairing after we receive a request for the passkey.
3119 BluetoothDevice* device = adapter_->GetDevice(
3120 FakeBluetoothDeviceClient::kRequestPasskeyAddress);
3121 ASSERT_TRUE(device != NULL);
3122 ASSERT_FALSE(device->IsPaired());
3124 TestBluetoothAdapterObserver observer(adapter_);
3126 TestPairingDelegate pairing_delegate;
3127 device->Connect(&pairing_delegate, GetCallback(),
3128 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
3129 base::Unretained(this)));
3131 EXPECT_EQ(1, pairing_delegate.call_count_);
3132 EXPECT_EQ(1, pairing_delegate.request_passkey_count_);
3133 EXPECT_TRUE(device->IsConnecting());
3135 // Reject the pairing.
3136 device->RejectPairing();
3137 message_loop_.Run();
3139 EXPECT_EQ(0, callback_count_);
3140 EXPECT_EQ(1, error_callback_count_);
3141 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_REJECTED, last_connect_error_);
3143 // Should be no changes except connecting going true and false.
3144 EXPECT_EQ(2, observer.device_changed_count());
3145 EXPECT_FALSE(device->IsConnected());
3146 EXPECT_FALSE(device->IsConnecting());
3147 EXPECT_FALSE(device->IsPaired());
3150 TEST_F(BluetoothChromeOSTest, PairingCancelledAtPasskey) {
3151 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3153 GetAdapter();
3154 DiscoverDevices();
3156 // Cancel the pairing after we receive a request for the passkey.
3157 BluetoothDevice* device = adapter_->GetDevice(
3158 FakeBluetoothDeviceClient::kRequestPasskeyAddress);
3159 ASSERT_TRUE(device != NULL);
3160 ASSERT_FALSE(device->IsPaired());
3162 TestBluetoothAdapterObserver observer(adapter_);
3164 TestPairingDelegate pairing_delegate;
3165 device->Connect(&pairing_delegate, GetCallback(),
3166 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
3167 base::Unretained(this)));
3169 EXPECT_EQ(1, pairing_delegate.call_count_);
3170 EXPECT_EQ(1, pairing_delegate.request_passkey_count_);
3171 EXPECT_TRUE(device->IsConnecting());
3173 // Cancel the pairing.
3174 device->CancelPairing();
3175 message_loop_.Run();
3177 EXPECT_EQ(0, callback_count_);
3178 EXPECT_EQ(1, error_callback_count_);
3179 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED, last_connect_error_);
3181 // Should be no changes except connecting going true and false.
3182 EXPECT_EQ(2, observer.device_changed_count());
3183 EXPECT_FALSE(device->IsConnected());
3184 EXPECT_FALSE(device->IsConnecting());
3185 EXPECT_FALSE(device->IsPaired());
3188 TEST_F(BluetoothChromeOSTest, PairingRejectedAtConfirmation) {
3189 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3191 GetAdapter();
3192 DiscoverDevices();
3194 // Reject the pairing after we receive a request for passkey confirmation.
3195 BluetoothDevice* device = adapter_->GetDevice(
3196 FakeBluetoothDeviceClient::kConfirmPasskeyAddress);
3197 ASSERT_TRUE(device != NULL);
3198 ASSERT_FALSE(device->IsPaired());
3200 TestBluetoothAdapterObserver observer(adapter_);
3202 TestPairingDelegate pairing_delegate;
3203 device->Connect(&pairing_delegate, GetCallback(),
3204 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
3205 base::Unretained(this)));
3207 EXPECT_EQ(1, pairing_delegate.call_count_);
3208 EXPECT_EQ(1, pairing_delegate.confirm_passkey_count_);
3209 EXPECT_TRUE(device->IsConnecting());
3211 // Reject the pairing.
3212 device->RejectPairing();
3213 message_loop_.Run();
3215 EXPECT_EQ(0, callback_count_);
3216 EXPECT_EQ(1, error_callback_count_);
3217 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_REJECTED, last_connect_error_);
3219 // Should be no changes except connecting going true and false.
3220 EXPECT_EQ(2, observer.device_changed_count());
3221 EXPECT_FALSE(device->IsConnected());
3222 EXPECT_FALSE(device->IsConnecting());
3223 EXPECT_FALSE(device->IsPaired());
3226 TEST_F(BluetoothChromeOSTest, PairingCancelledAtConfirmation) {
3227 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3229 GetAdapter();
3230 DiscoverDevices();
3232 // Cancel the pairing after we receive a request for the passkey.
3233 BluetoothDevice* device = adapter_->GetDevice(
3234 FakeBluetoothDeviceClient::kConfirmPasskeyAddress);
3235 ASSERT_TRUE(device != NULL);
3236 ASSERT_FALSE(device->IsPaired());
3238 TestBluetoothAdapterObserver observer(adapter_);
3240 TestPairingDelegate pairing_delegate;
3241 device->Connect(&pairing_delegate, GetCallback(),
3242 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
3243 base::Unretained(this)));
3245 EXPECT_EQ(1, pairing_delegate.call_count_);
3246 EXPECT_EQ(1, pairing_delegate.confirm_passkey_count_);
3247 EXPECT_TRUE(device->IsConnecting());
3249 // Cancel the pairing.
3250 device->CancelPairing();
3251 message_loop_.Run();
3253 EXPECT_EQ(0, callback_count_);
3254 EXPECT_EQ(1, error_callback_count_);
3255 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED, last_connect_error_);
3257 // Should be no changes except connecting going true and false.
3258 EXPECT_EQ(2, observer.device_changed_count());
3259 EXPECT_FALSE(device->IsConnected());
3260 EXPECT_FALSE(device->IsConnecting());
3261 EXPECT_FALSE(device->IsPaired());
3264 TEST_F(BluetoothChromeOSTest, PairingCancelledInFlight) {
3265 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3267 GetAdapter();
3268 DiscoverDevices();
3270 // Cancel the pairing while we're waiting for the remote host.
3271 BluetoothDevice* device = adapter_->GetDevice(
3272 FakeBluetoothDeviceClient::kLegacyAutopairAddress);
3273 ASSERT_TRUE(device != NULL);
3274 ASSERT_FALSE(device->IsPaired());
3276 TestBluetoothAdapterObserver observer(adapter_);
3278 TestPairingDelegate pairing_delegate;
3279 device->Connect(&pairing_delegate, GetCallback(),
3280 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
3281 base::Unretained(this)));
3283 EXPECT_EQ(0, pairing_delegate.call_count_);
3284 EXPECT_TRUE(device->IsConnecting());
3286 // Cancel the pairing.
3287 device->CancelPairing();
3288 message_loop_.Run();
3290 EXPECT_EQ(0, callback_count_);
3291 EXPECT_EQ(1, error_callback_count_);
3292 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED, last_connect_error_);
3294 // Should be no changes except connecting going true and false.
3295 EXPECT_EQ(2, observer.device_changed_count());
3296 EXPECT_FALSE(device->IsConnected());
3297 EXPECT_FALSE(device->IsConnecting());
3298 EXPECT_FALSE(device->IsPaired());
3301 TEST_F(BluetoothChromeOSTest, IncomingPairRequestPinCode) {
3302 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3304 GetAdapter();
3306 TestPairingDelegate pairing_delegate;
3307 adapter_->AddPairingDelegate(
3308 &pairing_delegate,
3309 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH);
3311 // Requires that we provide a PIN code.
3312 fake_bluetooth_device_client_->CreateDevice(
3313 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
3314 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath));
3315 BluetoothDevice* device = adapter_->GetDevice(
3316 FakeBluetoothDeviceClient::kRequestPinCodeAddress);
3317 ASSERT_TRUE(device != NULL);
3318 ASSERT_FALSE(device->IsPaired());
3320 TestBluetoothAdapterObserver observer(adapter_);
3322 fake_bluetooth_device_client_->SimulatePairing(
3323 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath), true,
3324 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
3325 base::Unretained(this)));
3327 EXPECT_EQ(1, pairing_delegate.call_count_);
3328 EXPECT_EQ(1, pairing_delegate.request_pincode_count_);
3330 // Set the PIN.
3331 device->SetPinCode("1234");
3332 message_loop_.Run();
3334 EXPECT_EQ(1, callback_count_);
3335 EXPECT_EQ(0, error_callback_count_);
3337 // One change for paired, and one for trusted.
3338 EXPECT_EQ(2, observer.device_changed_count());
3339 EXPECT_EQ(device, observer.last_device());
3341 EXPECT_TRUE(device->IsPaired());
3343 // Make sure the trusted property has been set to true.
3344 FakeBluetoothDeviceClient::Properties* properties =
3345 fake_bluetooth_device_client_->GetProperties(
3346 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath));
3347 ASSERT_TRUE(properties->trusted.value());
3349 // No pairing context should remain on the device.
3350 BluetoothDeviceChromeOS* device_chromeos =
3351 static_cast<BluetoothDeviceChromeOS*>(device);
3352 EXPECT_TRUE(device_chromeos->GetPairing() == NULL);
3355 TEST_F(BluetoothChromeOSTest, IncomingPairConfirmPasskey) {
3356 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3358 GetAdapter();
3360 TestPairingDelegate pairing_delegate;
3361 adapter_->AddPairingDelegate(
3362 &pairing_delegate,
3363 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH);
3365 // Requests that we confirm a displayed passkey.
3366 fake_bluetooth_device_client_->CreateDevice(
3367 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
3368 dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath));
3369 BluetoothDevice* device = adapter_->GetDevice(
3370 FakeBluetoothDeviceClient::kConfirmPasskeyAddress);
3371 ASSERT_TRUE(device != NULL);
3372 ASSERT_FALSE(device->IsPaired());
3374 TestBluetoothAdapterObserver observer(adapter_);
3376 fake_bluetooth_device_client_->SimulatePairing(
3377 dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath), true,
3378 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
3379 base::Unretained(this)));
3381 EXPECT_EQ(1, pairing_delegate.call_count_);
3382 EXPECT_EQ(1, pairing_delegate.confirm_passkey_count_);
3383 EXPECT_EQ(123456U, pairing_delegate.last_passkey_);
3385 // Confirm the passkey.
3386 device->ConfirmPairing();
3387 message_loop_.Run();
3389 EXPECT_EQ(1, callback_count_);
3390 EXPECT_EQ(0, error_callback_count_);
3392 // One change for paired, and one for trusted.
3393 EXPECT_EQ(2, observer.device_changed_count());
3394 EXPECT_EQ(device, observer.last_device());
3396 EXPECT_TRUE(device->IsPaired());
3398 // Make sure the trusted property has been set to true.
3399 FakeBluetoothDeviceClient::Properties* properties =
3400 fake_bluetooth_device_client_->GetProperties(
3401 dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath));
3402 ASSERT_TRUE(properties->trusted.value());
3404 // No pairing context should remain on the device.
3405 BluetoothDeviceChromeOS* device_chromeos =
3406 static_cast<BluetoothDeviceChromeOS*>(device);
3407 EXPECT_TRUE(device_chromeos->GetPairing() == NULL);
3410 TEST_F(BluetoothChromeOSTest, IncomingPairRequestPasskey) {
3411 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3413 GetAdapter();
3415 TestPairingDelegate pairing_delegate;
3416 adapter_->AddPairingDelegate(
3417 &pairing_delegate,
3418 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH);
3420 // Requests that we provide a Passkey.
3421 fake_bluetooth_device_client_->CreateDevice(
3422 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
3423 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath));
3424 BluetoothDevice* device = adapter_->GetDevice(
3425 FakeBluetoothDeviceClient::kRequestPasskeyAddress);
3426 ASSERT_TRUE(device != NULL);
3427 ASSERT_FALSE(device->IsPaired());
3429 TestBluetoothAdapterObserver observer(adapter_);
3431 fake_bluetooth_device_client_->SimulatePairing(
3432 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath), true,
3433 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
3434 base::Unretained(this)));
3436 EXPECT_EQ(1, pairing_delegate.call_count_);
3437 EXPECT_EQ(1, pairing_delegate.request_passkey_count_);
3439 // Set the Passkey.
3440 device->SetPasskey(1234);
3441 message_loop_.Run();
3443 EXPECT_EQ(1, callback_count_);
3444 EXPECT_EQ(0, error_callback_count_);
3446 // One change for paired, and one for trusted.
3447 EXPECT_EQ(2, observer.device_changed_count());
3448 EXPECT_EQ(device, observer.last_device());
3450 EXPECT_TRUE(device->IsPaired());
3452 // Make sure the trusted property has been set to true.
3453 FakeBluetoothDeviceClient::Properties* properties =
3454 fake_bluetooth_device_client_->GetProperties(
3455 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath));
3456 ASSERT_TRUE(properties->trusted.value());
3458 // No pairing context should remain on the device.
3459 BluetoothDeviceChromeOS* device_chromeos =
3460 static_cast<BluetoothDeviceChromeOS*>(device);
3461 EXPECT_TRUE(device_chromeos->GetPairing() == NULL);
3464 TEST_F(BluetoothChromeOSTest, IncomingPairJustWorks) {
3465 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3467 GetAdapter();
3469 TestPairingDelegate pairing_delegate;
3470 adapter_->AddPairingDelegate(
3471 &pairing_delegate,
3472 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH);
3474 // Uses just-works pairing so, sinec this an incoming pairing, require
3475 // authorization from the user.
3476 fake_bluetooth_device_client_->CreateDevice(
3477 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
3478 dbus::ObjectPath(FakeBluetoothDeviceClient::kJustWorksPath));
3479 BluetoothDevice* device = adapter_->GetDevice(
3480 FakeBluetoothDeviceClient::kJustWorksAddress);
3481 ASSERT_TRUE(device != NULL);
3482 ASSERT_FALSE(device->IsPaired());
3484 TestBluetoothAdapterObserver observer(adapter_);
3486 fake_bluetooth_device_client_->SimulatePairing(
3487 dbus::ObjectPath(FakeBluetoothDeviceClient::kJustWorksPath), true,
3488 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
3489 base::Unretained(this)));
3491 EXPECT_EQ(1, pairing_delegate.call_count_);
3492 EXPECT_EQ(1, pairing_delegate.authorize_pairing_count_);
3494 // Confirm the pairing.
3495 device->ConfirmPairing();
3496 message_loop_.Run();
3498 EXPECT_EQ(1, callback_count_);
3499 EXPECT_EQ(0, error_callback_count_);
3501 // One change for paired, and one for trusted.
3502 EXPECT_EQ(2, observer.device_changed_count());
3503 EXPECT_EQ(device, observer.last_device());
3505 EXPECT_TRUE(device->IsPaired());
3507 // Make sure the trusted property has been set to true.
3508 FakeBluetoothDeviceClient::Properties* properties =
3509 fake_bluetooth_device_client_->GetProperties(
3510 dbus::ObjectPath(FakeBluetoothDeviceClient::kJustWorksPath));
3511 ASSERT_TRUE(properties->trusted.value());
3513 // No pairing context should remain on the device.
3514 BluetoothDeviceChromeOS* device_chromeos =
3515 static_cast<BluetoothDeviceChromeOS*>(device);
3516 EXPECT_TRUE(device_chromeos->GetPairing() == NULL);
3519 TEST_F(BluetoothChromeOSTest, IncomingPairRequestPinCodeWithoutDelegate) {
3520 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3522 GetAdapter();
3524 // Requires that we provide a PIN Code, without a pairing delegate,
3525 // that will be rejected.
3526 fake_bluetooth_device_client_->CreateDevice(
3527 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
3528 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath));
3529 BluetoothDevice* device = adapter_->GetDevice(
3530 FakeBluetoothDeviceClient::kRequestPinCodeAddress);
3531 ASSERT_TRUE(device != NULL);
3532 ASSERT_FALSE(device->IsPaired());
3534 TestBluetoothAdapterObserver observer(adapter_);
3536 fake_bluetooth_device_client_->SimulatePairing(
3537 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath), true,
3538 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
3539 base::Unretained(this)));
3541 message_loop_.Run();
3543 EXPECT_EQ(0, callback_count_);
3544 EXPECT_EQ(1, error_callback_count_);
3545 EXPECT_EQ(bluetooth_device::kErrorAuthenticationRejected, last_client_error_);
3547 // No changes should be observer.
3548 EXPECT_EQ(0, observer.device_changed_count());
3550 EXPECT_FALSE(device->IsPaired());
3552 // No pairing context should remain on the device.
3553 BluetoothDeviceChromeOS* device_chromeos =
3554 static_cast<BluetoothDeviceChromeOS*>(device);
3555 EXPECT_TRUE(device_chromeos->GetPairing() == NULL);
3558 TEST_F(BluetoothChromeOSTest, IncomingPairConfirmPasskeyWithoutDelegate) {
3559 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3561 GetAdapter();
3563 // Requests that we confirm a displayed passkey, without a pairing delegate,
3564 // that will be rejected.
3565 fake_bluetooth_device_client_->CreateDevice(
3566 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
3567 dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath));
3568 BluetoothDevice* device = adapter_->GetDevice(
3569 FakeBluetoothDeviceClient::kConfirmPasskeyAddress);
3570 ASSERT_TRUE(device != NULL);
3571 ASSERT_FALSE(device->IsPaired());
3573 TestBluetoothAdapterObserver observer(adapter_);
3575 fake_bluetooth_device_client_->SimulatePairing(
3576 dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath), true,
3577 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
3578 base::Unretained(this)));
3580 message_loop_.Run();
3582 EXPECT_EQ(0, callback_count_);
3583 EXPECT_EQ(1, error_callback_count_);
3584 EXPECT_EQ(bluetooth_device::kErrorAuthenticationRejected, last_client_error_);
3586 // No changes should be observer.
3587 EXPECT_EQ(0, observer.device_changed_count());
3589 EXPECT_FALSE(device->IsPaired());
3591 // No pairing context should remain on the device.
3592 BluetoothDeviceChromeOS* device_chromeos =
3593 static_cast<BluetoothDeviceChromeOS*>(device);
3594 EXPECT_TRUE(device_chromeos->GetPairing() == NULL);
3597 TEST_F(BluetoothChromeOSTest, IncomingPairRequestPasskeyWithoutDelegate) {
3598 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3600 GetAdapter();
3602 // Requests that we provide a displayed passkey, without a pairing delegate,
3603 // that will be rejected.
3604 fake_bluetooth_device_client_->CreateDevice(
3605 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
3606 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath));
3607 BluetoothDevice* device = adapter_->GetDevice(
3608 FakeBluetoothDeviceClient::kRequestPasskeyAddress);
3609 ASSERT_TRUE(device != NULL);
3610 ASSERT_FALSE(device->IsPaired());
3612 TestBluetoothAdapterObserver observer(adapter_);
3614 fake_bluetooth_device_client_->SimulatePairing(
3615 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath), true,
3616 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
3617 base::Unretained(this)));
3619 message_loop_.Run();
3621 EXPECT_EQ(0, callback_count_);
3622 EXPECT_EQ(1, error_callback_count_);
3623 EXPECT_EQ(bluetooth_device::kErrorAuthenticationRejected, last_client_error_);
3625 // No changes should be observer.
3626 EXPECT_EQ(0, observer.device_changed_count());
3628 EXPECT_FALSE(device->IsPaired());
3630 // No pairing context should remain on the device.
3631 BluetoothDeviceChromeOS* device_chromeos =
3632 static_cast<BluetoothDeviceChromeOS*>(device);
3633 EXPECT_TRUE(device_chromeos->GetPairing() == NULL);
3636 TEST_F(BluetoothChromeOSTest, IncomingPairJustWorksWithoutDelegate) {
3637 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3639 GetAdapter();
3641 // Uses just-works pairing and thus requires authorization for incoming
3642 // pairings, without a pairing delegate, that will be rejected.
3643 fake_bluetooth_device_client_->CreateDevice(
3644 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
3645 dbus::ObjectPath(FakeBluetoothDeviceClient::kJustWorksPath));
3646 BluetoothDevice* device = adapter_->GetDevice(
3647 FakeBluetoothDeviceClient::kJustWorksAddress);
3648 ASSERT_TRUE(device != NULL);
3649 ASSERT_FALSE(device->IsPaired());
3651 TestBluetoothAdapterObserver observer(adapter_);
3653 fake_bluetooth_device_client_->SimulatePairing(
3654 dbus::ObjectPath(FakeBluetoothDeviceClient::kJustWorksPath), true,
3655 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
3656 base::Unretained(this)));
3658 message_loop_.Run();
3660 EXPECT_EQ(0, callback_count_);
3661 EXPECT_EQ(1, error_callback_count_);
3662 EXPECT_EQ(bluetooth_device::kErrorAuthenticationRejected, last_client_error_);
3664 // No changes should be observer.
3665 EXPECT_EQ(0, observer.device_changed_count());
3667 EXPECT_FALSE(device->IsPaired());
3669 // No pairing context should remain on the device.
3670 BluetoothDeviceChromeOS* device_chromeos =
3671 static_cast<BluetoothDeviceChromeOS*>(device);
3672 EXPECT_TRUE(device_chromeos->GetPairing() == NULL);
3675 TEST_F(BluetoothChromeOSTest, RemovePairingDelegateDuringPairing) {
3676 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3678 GetAdapter();
3680 TestPairingDelegate pairing_delegate;
3681 adapter_->AddPairingDelegate(
3682 &pairing_delegate,
3683 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH);
3685 // Requests that we provide a Passkey.
3686 fake_bluetooth_device_client_->CreateDevice(
3687 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
3688 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath));
3689 BluetoothDevice* device = adapter_->GetDevice(
3690 FakeBluetoothDeviceClient::kRequestPasskeyAddress);
3691 ASSERT_TRUE(device != NULL);
3692 ASSERT_FALSE(device->IsPaired());
3694 TestBluetoothAdapterObserver observer(adapter_);
3696 fake_bluetooth_device_client_->SimulatePairing(
3697 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath), true,
3698 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
3699 base::Unretained(this)));
3701 EXPECT_EQ(1, pairing_delegate.call_count_);
3702 EXPECT_EQ(1, pairing_delegate.request_passkey_count_);
3704 // A pairing context should now be set on the device.
3705 BluetoothDeviceChromeOS* device_chromeos =
3706 static_cast<BluetoothDeviceChromeOS*>(device);
3707 ASSERT_TRUE(device_chromeos->GetPairing() != NULL);
3709 // Removing the pairing delegate should remove that pairing context.
3710 adapter_->RemovePairingDelegate(&pairing_delegate);
3712 EXPECT_TRUE(device_chromeos->GetPairing() == NULL);
3714 // Set the Passkey, this should now have no effect since the pairing has
3715 // been, in-effect, cancelled
3716 device->SetPasskey(1234);
3718 EXPECT_EQ(0, callback_count_);
3719 EXPECT_EQ(0, error_callback_count_);
3720 EXPECT_EQ(0, observer.device_changed_count());
3722 EXPECT_FALSE(device->IsPaired());
3725 TEST_F(BluetoothChromeOSTest, DeviceId) {
3726 GetAdapter();
3728 // Use the built-in paired device for this test, grab its Properties
3729 // structure so we can adjust the underlying modalias property.
3730 BluetoothDevice* device = adapter_->GetDevice(
3731 FakeBluetoothDeviceClient::kPairedDeviceAddress);
3732 FakeBluetoothDeviceClient::Properties* properties =
3733 fake_bluetooth_device_client_->GetProperties(
3734 dbus::ObjectPath(FakeBluetoothDeviceClient::kPairedDevicePath));
3736 ASSERT_TRUE(device != NULL);
3737 ASSERT_TRUE(properties != NULL);
3739 // Valid USB IF-assigned identifier.
3740 ASSERT_EQ("usb:v05ACp030Dd0306", properties->modalias.value());
3742 EXPECT_EQ(BluetoothDevice::VENDOR_ID_USB, device->GetVendorIDSource());
3743 EXPECT_EQ(0x05ac, device->GetVendorID());
3744 EXPECT_EQ(0x030d, device->GetProductID());
3745 EXPECT_EQ(0x0306, device->GetDeviceID());
3747 // Valid Bluetooth SIG-assigned identifier.
3748 properties->modalias.ReplaceValue("bluetooth:v00E0p2400d0400");
3750 EXPECT_EQ(BluetoothDevice::VENDOR_ID_BLUETOOTH, device->GetVendorIDSource());
3751 EXPECT_EQ(0x00e0, device->GetVendorID());
3752 EXPECT_EQ(0x2400, device->GetProductID());
3753 EXPECT_EQ(0x0400, device->GetDeviceID());
3755 // Invalid USB IF-assigned identifier.
3756 properties->modalias.ReplaceValue("usb:x00E0p2400d0400");
3758 EXPECT_EQ(BluetoothDevice::VENDOR_ID_UNKNOWN, device->GetVendorIDSource());
3759 EXPECT_EQ(0, device->GetVendorID());
3760 EXPECT_EQ(0, device->GetProductID());
3761 EXPECT_EQ(0, device->GetDeviceID());
3763 // Invalid Bluetooth SIG-assigned identifier.
3764 properties->modalias.ReplaceValue("bluetooth:x00E0p2400d0400");
3766 EXPECT_EQ(BluetoothDevice::VENDOR_ID_UNKNOWN, device->GetVendorIDSource());
3767 EXPECT_EQ(0, device->GetVendorID());
3768 EXPECT_EQ(0, device->GetProductID());
3769 EXPECT_EQ(0, device->GetDeviceID());
3771 // Unknown vendor specification identifier.
3772 properties->modalias.ReplaceValue("chrome:v00E0p2400d0400");
3774 EXPECT_EQ(BluetoothDevice::VENDOR_ID_UNKNOWN, device->GetVendorIDSource());
3775 EXPECT_EQ(0, device->GetVendorID());
3776 EXPECT_EQ(0, device->GetProductID());
3777 EXPECT_EQ(0, device->GetDeviceID());
3780 TEST_F(BluetoothChromeOSTest, GetConnectionInfoForDisconnectedDevice) {
3781 GetAdapter();
3782 BluetoothDevice* device =
3783 adapter_->GetDevice(FakeBluetoothDeviceClient::kPairedDeviceAddress);
3785 // Calling GetConnectionInfo for an unconnected device should return a result
3786 // in which all fields are filled with BluetoothDevice::kUnknownPower.
3787 BluetoothDevice::ConnectionInfo conn_info(0, 0, 0);
3788 device->GetConnectionInfo(base::Bind(&SaveConnectionInfo, &conn_info));
3789 int unknown_power = BluetoothDevice::kUnknownPower;
3790 EXPECT_NE(0, unknown_power);
3791 EXPECT_EQ(unknown_power, conn_info.rssi);
3792 EXPECT_EQ(unknown_power, conn_info.transmit_power);
3793 EXPECT_EQ(unknown_power, conn_info.max_transmit_power);
3796 TEST_F(BluetoothChromeOSTest, GetConnectionInfoForConnectedDevice) {
3797 GetAdapter();
3798 BluetoothDevice* device =
3799 adapter_->GetDevice(FakeBluetoothDeviceClient::kPairedDeviceAddress);
3801 device->Connect(NULL, GetCallback(),
3802 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
3803 base::Unretained(this)));
3804 EXPECT_TRUE(device->IsConnected());
3806 // Calling GetConnectionInfo for a connected device should return valid
3807 // results.
3808 fake_bluetooth_device_client_->UpdateConnectionInfo(-10, 3, 4);
3809 BluetoothDevice::ConnectionInfo conn_info;
3810 device->GetConnectionInfo(base::Bind(&SaveConnectionInfo, &conn_info));
3811 EXPECT_EQ(-10, conn_info.rssi);
3812 EXPECT_EQ(3, conn_info.transmit_power);
3813 EXPECT_EQ(4, conn_info.max_transmit_power);
3816 // Verifies Shutdown shuts down the adapter as expected.
3817 TEST_F(BluetoothChromeOSTest, Shutdown) {
3818 // Set up adapter. Set powered & discoverable, start discovery.
3819 GetAdapter();
3820 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
3821 adapter_->SetDiscoverable(true, GetCallback(), GetErrorCallback());
3822 adapter_->StartDiscoverySession(
3823 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
3824 base::Unretained(this)),
3825 GetErrorCallback());
3826 base::MessageLoop::current()->Run();
3827 ASSERT_EQ(3, callback_count_);
3828 ASSERT_EQ(0, error_callback_count_);
3829 callback_count_ = 0;
3831 TestPairingDelegate pairing_delegate;
3832 adapter_->AddPairingDelegate(
3833 &pairing_delegate, BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH);
3835 // Validate running adapter state.
3836 EXPECT_NE("", adapter_->GetAddress());
3837 EXPECT_NE("", adapter_->GetName());
3838 EXPECT_TRUE(adapter_->IsInitialized());
3839 EXPECT_TRUE(adapter_->IsPresent());
3840 EXPECT_TRUE(adapter_->IsPowered());
3841 EXPECT_TRUE(adapter_->IsDiscoverable());
3842 EXPECT_TRUE(adapter_->IsDiscovering());
3843 EXPECT_EQ(2U, adapter_->GetDevices().size());
3844 EXPECT_NE(nullptr, adapter_->GetDevice(
3845 FakeBluetoothDeviceClient::kPairedDeviceAddress));
3846 EXPECT_NE(dbus::ObjectPath(""), static_cast<BluetoothAdapterChromeOS*>(
3847 adapter_.get())->object_path());
3849 // Shutdown
3850 adapter_->Shutdown();
3852 // Validate post shutdown state by calling all BluetoothAdapterChromeOS
3853 // members, in declaration order:
3855 adapter_->Shutdown();
3856 // DeleteOnCorrectThread omitted as we don't want to delete in this test.
3858 TestBluetoothAdapterObserver observer(adapter_); // Calls AddObserver
3859 } // ~TestBluetoothAdapterObserver calls RemoveObserver.
3860 EXPECT_EQ("", adapter_->GetAddress());
3861 EXPECT_EQ("", adapter_->GetName());
3863 adapter_->SetName("", GetCallback(), GetErrorCallback());
3864 EXPECT_EQ(0, callback_count_);
3865 EXPECT_EQ(1, error_callback_count_--) << "SetName error";
3867 EXPECT_TRUE(adapter_->IsInitialized());
3868 EXPECT_FALSE(adapter_->IsPresent());
3869 EXPECT_FALSE(adapter_->IsPowered());
3871 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
3872 EXPECT_EQ(0, callback_count_);
3873 EXPECT_EQ(1, error_callback_count_--) << "SetPowered error";
3875 EXPECT_FALSE(adapter_->IsDiscoverable());
3877 adapter_->SetDiscoverable(true, GetCallback(), GetErrorCallback());
3878 EXPECT_EQ(0, callback_count_);
3879 EXPECT_EQ(1, error_callback_count_--) << "SetDiscoverable error";
3881 EXPECT_FALSE(adapter_->IsDiscovering());
3882 // CreateRfcommService will DCHECK after Shutdown().
3883 // CreateL2capService will DCHECK after Shutdown().
3885 BluetoothAudioSink::Options audio_sink_options;
3886 adapter_->RegisterAudioSink(
3887 audio_sink_options,
3888 base::Bind(&BluetoothChromeOSTest::AudioSinkAcquiredCallback,
3889 base::Unretained(this)),
3890 base::Bind(&BluetoothChromeOSTest::AudioSinkErrorCallback,
3891 base::Unretained(this)));
3892 EXPECT_EQ(0, callback_count_);
3893 EXPECT_EQ(1, error_callback_count_--) << "RegisterAudioSink error";
3895 BluetoothAdapterChromeOS* adapter_chrome_os =
3896 static_cast<BluetoothAdapterChromeOS*>(adapter_.get());
3897 EXPECT_EQ(NULL, adapter_chrome_os->GetDeviceWithPath(dbus::ObjectPath("")));
3899 // Notify methods presume objects exist that are owned by the adapter and
3900 // destroyed in Shutdown(). Mocks are not attempted here that won't exist,
3901 // as verified below by EXPECT_EQ(0U, adapter_->GetDevices().size());
3902 // NotifyDeviceChanged
3903 // NotifyGattServiceAdded
3904 // NotifyGattServiceRemoved
3905 // NotifyGattServiceChanged
3906 // NotifyGattDiscoveryComplete
3907 // NotifyGattCharacteristicAdded
3908 // NotifyGattCharacteristicRemoved
3909 // NotifyGattDescriptorAdded
3910 // NotifyGattDescriptorRemoved
3911 // NotifyGattCharacteristicValueChanged
3912 // NotifyGattDescriptorValueChanged
3914 EXPECT_EQ(dbus::ObjectPath(""), adapter_chrome_os->object_path());
3916 adapter_profile_ = NULL;
3918 FakeBluetoothProfileServiceProviderDelegate profile_delegate;
3919 adapter_chrome_os->UseProfile(
3920 BluetoothUUID(), dbus::ObjectPath(""),
3921 BluetoothProfileManagerClient::Options(), &profile_delegate,
3922 base::Bind(&BluetoothChromeOSTest::ProfileRegisteredCallback,
3923 base::Unretained(this)),
3924 base::Bind(&BluetoothChromeOSTest::ErrorCompletionCallback,
3925 base::Unretained(this)));
3927 EXPECT_FALSE(adapter_profile_) << "UseProfile error";
3928 EXPECT_EQ(0, callback_count_) << "UseProfile error";
3929 EXPECT_EQ(1, error_callback_count_--) << "UseProfile error";
3931 // Protected and private methods:
3933 adapter_chrome_os->RemovePairingDelegateInternal(&pairing_delegate);
3934 // AdapterAdded() invalid post Shutdown because it calls SetAdapter.
3935 adapter_chrome_os->AdapterRemoved(dbus::ObjectPath("x"));
3936 adapter_chrome_os->AdapterPropertyChanged(dbus::ObjectPath("x"), "");
3937 adapter_chrome_os->DeviceAdded(dbus::ObjectPath(""));
3938 adapter_chrome_os->DeviceRemoved(dbus::ObjectPath(""));
3939 adapter_chrome_os->DevicePropertyChanged(dbus::ObjectPath(""), "");
3940 adapter_chrome_os->InputPropertyChanged(dbus::ObjectPath(""), "");
3941 // BluetoothAgentServiceProvider::Delegate omitted, dbus will be shutdown,
3942 // with the exception of Released.
3943 adapter_chrome_os->Released();
3945 adapter_chrome_os->OnRegisterAgent();
3946 adapter_chrome_os->OnRegisterAgentError("", "");
3947 adapter_chrome_os->OnRequestDefaultAgent();
3948 adapter_chrome_os->OnRequestDefaultAgentError("", "");
3950 adapter_chrome_os->OnRegisterAudioSink(
3951 base::Bind(&BluetoothChromeOSTest::AudioSinkAcquiredCallback,
3952 base::Unretained(this)),
3953 base::Bind(&BluetoothChromeOSTest::AudioSinkErrorCallback,
3954 base::Unretained(this)),
3955 scoped_refptr<device::BluetoothAudioSink>());
3956 EXPECT_EQ(0, callback_count_);
3957 EXPECT_EQ(1, error_callback_count_--) << "RegisterAudioSink error";
3959 // GetPairing will DCHECK after Shutdown().
3960 // SetAdapter will DCHECK after Shutdown().
3961 // SetDefaultAdapterName will DCHECK after Shutdown().
3962 // RemoveAdapter will DCHECK after Shutdown().
3963 adapter_chrome_os->PoweredChanged(false);
3964 adapter_chrome_os->DiscoverableChanged(false);
3965 adapter_chrome_os->DiscoveringChanged(false);
3966 adapter_chrome_os->PresentChanged(false);
3968 adapter_chrome_os->OnSetDiscoverable(GetCallback(), GetErrorCallback(), true);
3969 EXPECT_EQ(0, callback_count_) << "OnSetDiscoverable error";
3970 EXPECT_EQ(1, error_callback_count_--) << "OnSetDiscoverable error";
3972 adapter_chrome_os->OnPropertyChangeCompleted(GetCallback(),
3973 GetErrorCallback(), true);
3974 EXPECT_EQ(0, callback_count_) << "OnPropertyChangeCompleted error";
3975 EXPECT_EQ(1, error_callback_count_--) << "OnPropertyChangeCompleted error";
3977 adapter_chrome_os->AddDiscoverySession(nullptr, GetCallback(),
3978 GetErrorCallback());
3979 EXPECT_EQ(0, callback_count_) << "AddDiscoverySession error";
3980 EXPECT_EQ(1, error_callback_count_--) << "AddDiscoverySession error";
3982 adapter_chrome_os->RemoveDiscoverySession(nullptr, GetCallback(),
3983 GetErrorCallback());
3984 EXPECT_EQ(0, callback_count_) << "RemoveDiscoverySession error";
3985 EXPECT_EQ(1, error_callback_count_--) << "RemoveDiscoverySession error";
3987 // OnStartDiscovery tested in Shutdown_OnStartDiscovery
3988 // OnStartDiscoveryError tested in Shutdown_OnStartDiscoveryError
3989 // OnStopDiscovery tested in Shutdown_OnStopDiscovery
3990 // OnStopDiscoveryError tested in Shutdown_OnStopDiscoveryError
3992 adapter_profile_ = NULL;
3994 // OnRegisterProfile SetProfileDelegate, OnRegisterProfileError, require
3995 // UseProfile to be set first, do so again here just before calling them.
3996 adapter_chrome_os->UseProfile(
3997 BluetoothUUID(), dbus::ObjectPath(""),
3998 BluetoothProfileManagerClient::Options(), &profile_delegate,
3999 base::Bind(&BluetoothChromeOSTest::ProfileRegisteredCallback,
4000 base::Unretained(this)),
4001 base::Bind(&BluetoothChromeOSTest::ErrorCompletionCallback,
4002 base::Unretained(this)));
4004 EXPECT_FALSE(adapter_profile_) << "UseProfile error";
4005 EXPECT_EQ(0, callback_count_) << "UseProfile error";
4006 EXPECT_EQ(1, error_callback_count_--) << "UseProfile error";
4008 adapter_chrome_os->SetProfileDelegate(
4009 BluetoothUUID(), dbus::ObjectPath(""), &profile_delegate,
4010 base::Bind(&BluetoothChromeOSTest::ProfileRegisteredCallback,
4011 base::Unretained(this)),
4012 base::Bind(&BluetoothChromeOSTest::ErrorCompletionCallback,
4013 base::Unretained(this)));
4014 EXPECT_EQ(0, callback_count_) << "SetProfileDelegate error";
4015 EXPECT_EQ(1, error_callback_count_--) << "SetProfileDelegate error";
4017 adapter_chrome_os->OnRegisterProfileError(BluetoothUUID(), "", "");
4018 EXPECT_EQ(0, callback_count_) << "OnRegisterProfileError error";
4019 EXPECT_EQ(0, error_callback_count_) << "OnRegisterProfileError error";
4021 adapter_chrome_os->ProcessQueuedDiscoveryRequests();
4023 // From BluetoothAdapater:
4025 adapter_->StartDiscoverySession(
4026 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
4027 base::Unretained(this)),
4028 GetErrorCallback());
4029 EXPECT_EQ(0, callback_count_) << "StartDiscoverySession error";
4030 EXPECT_EQ(1, error_callback_count_--) << "StartDiscoverySession error";
4032 EXPECT_EQ(0U, adapter_->GetDevices().size());
4033 EXPECT_EQ(nullptr, adapter_->GetDevice(
4034 FakeBluetoothDeviceClient::kPairedDeviceAddress));
4035 TestPairingDelegate pairing_delegate2;
4036 adapter_->AddPairingDelegate(
4037 &pairing_delegate2, BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH);
4038 adapter_->RemovePairingDelegate(&pairing_delegate2);
4041 // Verifies post-Shutdown of discovery sessions and OnStartDiscovery.
4042 TEST_F(BluetoothChromeOSTest, Shutdown_OnStartDiscovery) {
4043 const int kNumberOfDiscoverySessions = 10;
4044 GetAdapter();
4045 BluetoothAdapterChromeOS* adapter_chrome_os =
4046 static_cast<BluetoothAdapterChromeOS*>(adapter_.get());
4048 for (int i = 0; i < kNumberOfDiscoverySessions; i++) {
4049 adapter_chrome_os->AddDiscoverySession(nullptr, GetCallback(),
4050 GetErrorCallback());
4052 adapter_->Shutdown();
4053 adapter_chrome_os->OnStartDiscovery(GetCallback(), GetErrorCallback());
4055 EXPECT_EQ(0, callback_count_);
4056 EXPECT_EQ(kNumberOfDiscoverySessions, error_callback_count_);
4059 // Verifies post-Shutdown of discovery sessions and OnStartDiscoveryError.
4060 TEST_F(BluetoothChromeOSTest, Shutdown_OnStartDiscoveryError) {
4061 const int kNumberOfDiscoverySessions = 10;
4062 GetAdapter();
4063 BluetoothAdapterChromeOS* adapter_chrome_os =
4064 static_cast<BluetoothAdapterChromeOS*>(adapter_.get());
4066 for (int i = 0; i < kNumberOfDiscoverySessions; i++) {
4067 adapter_chrome_os->AddDiscoverySession(nullptr, GetCallback(),
4068 GetErrorCallback());
4070 adapter_->Shutdown();
4071 adapter_chrome_os->OnStartDiscoveryError(GetCallback(), GetErrorCallback(),
4072 "", "");
4074 EXPECT_EQ(0, callback_count_);
4075 EXPECT_EQ(kNumberOfDiscoverySessions, error_callback_count_);
4078 // Verifies post-Shutdown of discovery sessions and OnStartDiscovery.
4079 TEST_F(BluetoothChromeOSTest, Shutdown_OnStopDiscovery) {
4080 const int kNumberOfDiscoverySessions = 10;
4081 GetAdapter();
4082 BluetoothAdapterChromeOS* adapter_chrome_os =
4083 static_cast<BluetoothAdapterChromeOS*>(adapter_.get());
4085 // In order to queue up discovery sessions before an OnStopDiscovery call
4086 // RemoveDiscoverySession must be called, so Add, Start, and Remove:
4087 adapter_chrome_os->AddDiscoverySession(nullptr, GetCallback(),
4088 GetErrorCallback());
4089 adapter_chrome_os->OnStartDiscovery(GetCallback(), GetErrorCallback());
4090 adapter_chrome_os->RemoveDiscoverySession(nullptr, GetCallback(),
4091 GetErrorCallback());
4092 callback_count_ = 0;
4093 error_callback_count_ = 0;
4094 // Can now queue discovery sessions while waiting for OnStopDiscovery.
4095 for (int i = 0; i < kNumberOfDiscoverySessions; i++) {
4096 adapter_chrome_os->AddDiscoverySession(nullptr, GetCallback(),
4097 GetErrorCallback());
4099 adapter_->Shutdown();
4100 adapter_chrome_os->OnStopDiscovery(GetCallback());
4102 // 1 successful stopped discovery from RemoveDiscoverySession, and
4103 // kNumberOfDiscoverySessions errors from AddDiscoverySession/OnStopDiscovery.
4104 EXPECT_EQ(1, callback_count_);
4105 EXPECT_EQ(kNumberOfDiscoverySessions, error_callback_count_);
4108 // Verifies post-Shutdown of discovery sessions and OnStopDiscoveryError.
4109 TEST_F(BluetoothChromeOSTest, Shutdown_OnStopDiscoveryError) {
4110 const int kNumberOfDiscoverySessions = 10;
4111 GetAdapter();
4112 BluetoothAdapterChromeOS* adapter_chrome_os =
4113 static_cast<BluetoothAdapterChromeOS*>(adapter_.get());
4115 // In order to queue up discovery sessions before an OnStopDiscoveryError call
4116 // RemoveDiscoverySession must be called, so Add, Start, and Remove:
4117 adapter_chrome_os->AddDiscoverySession(nullptr, GetCallback(),
4118 GetErrorCallback());
4119 adapter_chrome_os->OnStartDiscovery(GetCallback(), GetErrorCallback());
4120 adapter_chrome_os->RemoveDiscoverySession(nullptr, GetCallback(),
4121 GetErrorCallback());
4122 callback_count_ = 0;
4123 error_callback_count_ = 0;
4124 // Can now queue discovery sessions while waiting for OnStopDiscoveryError.
4125 for (int i = 0; i < kNumberOfDiscoverySessions; i++) {
4126 adapter_chrome_os->AddDiscoverySession(nullptr, GetCallback(),
4127 GetErrorCallback());
4129 adapter_->Shutdown();
4130 adapter_chrome_os->OnStopDiscoveryError(GetErrorCallback(), "", "");
4132 // 1 error reported to RemoveDiscoverySession because of OnStopDiscoveryError,
4133 // and kNumberOfDiscoverySessions errors queued with AddDiscoverySession.
4134 EXPECT_EQ(0, callback_count_);
4135 EXPECT_EQ(1 + kNumberOfDiscoverySessions, error_callback_count_);
4138 } // namespace chromeos