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/strings/utf_string_conversions.h"
8 #include "chromeos/dbus/fake_bluetooth_adapter_client.h"
9 #include "chromeos/dbus/fake_bluetooth_agent_manager_client.h"
10 #include "chromeos/dbus/fake_bluetooth_device_client.h"
11 #include "chromeos/dbus/fake_bluetooth_gatt_service_client.h"
12 #include "chromeos/dbus/fake_bluetooth_input_client.h"
13 #include "chromeos/dbus/fake_dbus_thread_manager.h"
14 #include "dbus/object_path.h"
15 #include "device/bluetooth/bluetooth_adapter.h"
16 #include "device/bluetooth/bluetooth_adapter_chromeos.h"
17 #include "device/bluetooth/bluetooth_adapter_factory.h"
18 #include "device/bluetooth/bluetooth_device.h"
19 #include "device/bluetooth/bluetooth_device_chromeos.h"
20 #include "device/bluetooth/bluetooth_discovery_session.h"
21 #include "device/bluetooth/bluetooth_pairing_chromeos.h"
22 #include "testing/gtest/include/gtest/gtest.h"
23 #include "third_party/cros_system_api/dbus/service_constants.h"
25 using device::BluetoothAdapter
;
26 using device::BluetoothAdapterFactory
;
27 using device::BluetoothDevice
;
28 using device::BluetoothDiscoverySession
;
29 using device::BluetoothUUID
;
35 class TestObserver
: public BluetoothAdapter::Observer
{
37 TestObserver(scoped_refptr
<BluetoothAdapter
> adapter
)
38 : present_changed_count_(0),
39 powered_changed_count_(0),
40 discoverable_changed_count_(0),
41 discovering_changed_count_(0),
44 last_discovering_(false),
45 device_added_count_(0),
46 device_changed_count_(0),
47 device_removed_count_(0),
50 adapter_
->AddObserver(this);
53 virtual ~TestObserver() {
54 adapter_
->RemoveObserver(this);
57 virtual void AdapterPresentChanged(BluetoothAdapter
* adapter
,
58 bool present
) OVERRIDE
{
59 EXPECT_EQ(adapter_
, adapter
);
61 ++present_changed_count_
;
62 last_present_
= present
;
65 virtual void AdapterPoweredChanged(BluetoothAdapter
* adapter
,
66 bool powered
) OVERRIDE
{
67 EXPECT_EQ(adapter_
, adapter
);
69 ++powered_changed_count_
;
70 last_powered_
= powered
;
73 virtual void AdapterDiscoverableChanged(BluetoothAdapter
* adapter
,
74 bool discoverable
) OVERRIDE
{
75 EXPECT_EQ(adapter_
, adapter
);
77 ++discoverable_changed_count_
;
80 virtual void AdapterDiscoveringChanged(BluetoothAdapter
* adapter
,
81 bool discovering
) OVERRIDE
{
82 EXPECT_EQ(adapter_
, adapter
);
84 ++discovering_changed_count_
;
85 last_discovering_
= discovering
;
88 virtual void DeviceAdded(BluetoothAdapter
* adapter
,
89 BluetoothDevice
* device
) OVERRIDE
{
90 EXPECT_EQ(adapter_
, adapter
);
92 ++device_added_count_
;
93 last_device_
= device
;
94 last_device_address_
= device
->GetAddress();
99 virtual void DeviceChanged(BluetoothAdapter
* adapter
,
100 BluetoothDevice
* device
) OVERRIDE
{
101 EXPECT_EQ(adapter_
, adapter
);
103 ++device_changed_count_
;
104 last_device_
= device
;
105 last_device_address_
= device
->GetAddress();
110 virtual void DeviceRemoved(BluetoothAdapter
* adapter
,
111 BluetoothDevice
* device
) OVERRIDE
{
112 EXPECT_EQ(adapter_
, adapter
);
114 ++device_removed_count_
;
115 // Can't save device, it may be freed
116 last_device_address_
= device
->GetAddress();
121 int present_changed_count_
;
122 int powered_changed_count_
;
123 int discoverable_changed_count_
;
124 int discovering_changed_count_
;
127 bool last_discovering_
;
128 int device_added_count_
;
129 int device_changed_count_
;
130 int device_removed_count_
;
131 BluetoothDevice
* last_device_
;
132 std::string last_device_address_
;
135 // Some tests use a message loop since background processing is simulated;
136 // break out of those loops.
137 void QuitMessageLoop() {
138 if (base::MessageLoop::current() &&
139 base::MessageLoop::current()->is_running())
140 base::MessageLoop::current()->Quit();
143 scoped_refptr
<BluetoothAdapter
> adapter_
;
148 class TestPairingDelegate
: public BluetoothDevice::PairingDelegate
{
150 TestPairingDelegate()
152 request_pincode_count_(0),
153 request_passkey_count_(0),
154 display_pincode_count_(0),
155 display_passkey_count_(0),
156 keys_entered_count_(0),
157 confirm_passkey_count_(0),
158 authorize_pairing_count_(0),
159 last_passkey_(9999999U),
160 last_entered_(999U) {}
161 virtual ~TestPairingDelegate() {}
163 virtual void RequestPinCode(BluetoothDevice
* device
) OVERRIDE
{
165 ++request_pincode_count_
;
169 virtual void RequestPasskey(BluetoothDevice
* device
) OVERRIDE
{
171 ++request_passkey_count_
;
175 virtual void DisplayPinCode(BluetoothDevice
* device
,
176 const std::string
& pincode
) OVERRIDE
{
178 ++display_pincode_count_
;
179 last_pincode_
= pincode
;
183 virtual void DisplayPasskey(BluetoothDevice
* device
,
184 uint32 passkey
) OVERRIDE
{
186 ++display_passkey_count_
;
187 last_passkey_
= passkey
;
191 virtual void KeysEntered(BluetoothDevice
* device
, uint32 entered
) OVERRIDE
{
193 ++keys_entered_count_
;
194 last_entered_
= entered
;
198 virtual void ConfirmPasskey(BluetoothDevice
* device
,
199 uint32 passkey
) OVERRIDE
{
201 ++confirm_passkey_count_
;
202 last_passkey_
= passkey
;
206 virtual void AuthorizePairing(BluetoothDevice
* device
) OVERRIDE
{
208 ++authorize_pairing_count_
;
213 int request_pincode_count_
;
214 int request_passkey_count_
;
215 int display_pincode_count_
;
216 int display_passkey_count_
;
217 int keys_entered_count_
;
218 int confirm_passkey_count_
;
219 int authorize_pairing_count_
;
220 uint32 last_passkey_
;
221 uint32 last_entered_
;
222 std::string last_pincode_
;
225 // Some tests use a message loop since background processing is simulated;
226 // break out of those loops.
227 void QuitMessageLoop() {
228 if (base::MessageLoop::current() &&
229 base::MessageLoop::current()->is_running())
230 base::MessageLoop::current()->Quit();
234 class BluetoothChromeOSTest
: public testing::Test
{
236 virtual void SetUp() {
237 FakeDBusThreadManager
* fake_dbus_thread_manager
= new FakeDBusThreadManager
;
238 fake_bluetooth_adapter_client_
= new FakeBluetoothAdapterClient
;
239 fake_dbus_thread_manager
->SetBluetoothAdapterClient(
240 scoped_ptr
<BluetoothAdapterClient
>(fake_bluetooth_adapter_client_
));
241 fake_bluetooth_device_client_
= new FakeBluetoothDeviceClient
;
242 fake_dbus_thread_manager
->SetBluetoothDeviceClient(
243 scoped_ptr
<BluetoothDeviceClient
>(fake_bluetooth_device_client_
));
244 fake_dbus_thread_manager
->SetBluetoothInputClient(
245 scoped_ptr
<BluetoothInputClient
>(new FakeBluetoothInputClient
));
246 fake_dbus_thread_manager
->SetBluetoothAgentManagerClient(
247 scoped_ptr
<BluetoothAgentManagerClient
>(
248 new FakeBluetoothAgentManagerClient
));
249 fake_dbus_thread_manager
->SetBluetoothGattServiceClient(
250 scoped_ptr
<BluetoothGattServiceClient
>(
251 new FakeBluetoothGattServiceClient
));
252 DBusThreadManager::InitializeForTesting(fake_dbus_thread_manager
);
254 fake_bluetooth_adapter_client_
->SetSimulationIntervalMs(10);
257 error_callback_count_
= 0;
258 last_connect_error_
= BluetoothDevice::ERROR_UNKNOWN
;
259 last_client_error_
= "";
262 virtual void TearDown() {
263 for (ScopedVector
<BluetoothDiscoverySession
>::iterator iter
=
264 discovery_sessions_
.begin();
265 iter
!= discovery_sessions_
.end();
267 BluetoothDiscoverySession
* session
= *iter
;
268 if (!session
->IsActive())
272 base::Bind(&BluetoothChromeOSTest::Callback
,
273 base::Unretained(this)),
274 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
275 base::Unretained(this)));
277 ASSERT_EQ(1, callback_count_
);
279 discovery_sessions_
.clear();
281 DBusThreadManager::Shutdown();
290 void DiscoverySessionCallback(
291 scoped_ptr
<BluetoothDiscoverySession
> discovery_session
) {
293 discovery_sessions_
.push_back(discovery_session
.release());
297 void ErrorCallback() {
298 ++error_callback_count_
;
302 void DBusErrorCallback(const std::string
& error_name
,
303 const std::string
& error_message
) {
304 ++error_callback_count_
;
305 last_client_error_
= error_name
;
309 void ConnectErrorCallback(BluetoothDevice::ConnectErrorCode error
) {
310 ++error_callback_count_
;
311 last_connect_error_
= error
;
314 // Call to fill the adapter_ member with a BluetoothAdapter instance.
316 adapter_
= new BluetoothAdapterChromeOS();
317 ASSERT_TRUE(adapter_
.get() != NULL
);
318 ASSERT_TRUE(adapter_
->IsInitialized());
321 // Run a discovery phase until the named device is detected, or if the named
322 // device is not created, the discovery process ends without finding it.
324 // The correct behavior of discovery is tested by the "Discovery" test case
325 // without using this function.
326 void DiscoverDevice(const std::string
& address
) {
327 ASSERT_TRUE(adapter_
.get() != NULL
);
328 ASSERT_TRUE(base::MessageLoop::current() != NULL
);
329 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
331 TestObserver
observer(adapter_
);
333 adapter_
->SetPowered(
335 base::Bind(&BluetoothChromeOSTest::Callback
,
336 base::Unretained(this)),
337 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
338 base::Unretained(this)));
339 adapter_
->StartDiscoverySession(
340 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
341 base::Unretained(this)),
342 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
343 base::Unretained(this)));
344 base::MessageLoop::current()->Run();
345 ASSERT_EQ(2, callback_count_
);
346 ASSERT_EQ(0, error_callback_count_
);
347 ASSERT_EQ((size_t)1, discovery_sessions_
.size());
348 ASSERT_TRUE(discovery_sessions_
[0]->IsActive());
351 ASSERT_TRUE(adapter_
->IsPowered());
352 ASSERT_TRUE(adapter_
->IsDiscovering());
354 while (!observer
.device_removed_count_
&&
355 observer
.last_device_address_
!= address
)
356 base::MessageLoop::current()->Run();
358 discovery_sessions_
[0]->Stop(
359 base::Bind(&BluetoothChromeOSTest::Callback
,
360 base::Unretained(this)),
361 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
362 base::Unretained(this)));
363 base::MessageLoop::current()->Run();
364 ASSERT_EQ(1, callback_count_
);
365 ASSERT_EQ(0, error_callback_count_
);
368 ASSERT_FALSE(adapter_
->IsDiscovering());
371 // Run a discovery phase so we have devices that can be paired with.
372 void DiscoverDevices() {
373 // Pass an invalid address for the device so that the discovery process
374 // completes with all devices.
375 DiscoverDevice("does not exist");
379 base::MessageLoop message_loop_
;
380 FakeBluetoothAdapterClient
* fake_bluetooth_adapter_client_
;
381 FakeBluetoothDeviceClient
* fake_bluetooth_device_client_
;
382 scoped_refptr
<BluetoothAdapter
> adapter_
;
385 int error_callback_count_
;
386 enum BluetoothDevice::ConnectErrorCode last_connect_error_
;
387 std::string last_client_error_
;
388 ScopedVector
<BluetoothDiscoverySession
> discovery_sessions_
;
391 // Some tests use a message loop since background processing is simulated;
392 // break out of those loops.
393 void QuitMessageLoop() {
394 if (base::MessageLoop::current() &&
395 base::MessageLoop::current()->is_running())
396 base::MessageLoop::current()->Quit();
400 TEST_F(BluetoothChromeOSTest
, AlreadyPresent
) {
403 // This verifies that the class gets the list of adapters when created;
404 // and initializes with an existing adapter if there is one.
405 EXPECT_TRUE(adapter_
->IsPresent());
406 EXPECT_FALSE(adapter_
->IsPowered());
407 EXPECT_EQ(FakeBluetoothAdapterClient::kAdapterAddress
,
408 adapter_
->GetAddress());
409 EXPECT_FALSE(adapter_
->IsDiscovering());
411 // There should be a device
412 BluetoothAdapter::DeviceList devices
= adapter_
->GetDevices();
413 EXPECT_EQ(1U, devices
.size());
414 EXPECT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress
,
415 devices
[0]->GetAddress());
418 TEST_F(BluetoothChromeOSTest
, BecomePresent
) {
419 fake_bluetooth_adapter_client_
->SetVisible(false);
421 ASSERT_FALSE(adapter_
->IsPresent());
423 // Install an observer; expect the AdapterPresentChanged to be called
424 // with true, and IsPresent() to return true.
425 TestObserver
observer(adapter_
);
427 fake_bluetooth_adapter_client_
->SetVisible(true);
429 EXPECT_EQ(1, observer
.present_changed_count_
);
430 EXPECT_TRUE(observer
.last_present_
);
432 EXPECT_TRUE(adapter_
->IsPresent());
434 // We should have had a device announced.
435 EXPECT_EQ(1, observer
.device_added_count_
);
436 EXPECT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress
,
437 observer
.last_device_address_
);
439 // Other callbacks shouldn't be called if the values are false.
440 EXPECT_EQ(0, observer
.powered_changed_count_
);
441 EXPECT_EQ(0, observer
.discovering_changed_count_
);
442 EXPECT_FALSE(adapter_
->IsPowered());
443 EXPECT_FALSE(adapter_
->IsDiscovering());
446 TEST_F(BluetoothChromeOSTest
, BecomeNotPresent
) {
448 ASSERT_TRUE(adapter_
->IsPresent());
450 // Install an observer; expect the AdapterPresentChanged to be called
451 // with false, and IsPresent() to return false.
452 TestObserver
observer(adapter_
);
454 fake_bluetooth_adapter_client_
->SetVisible(false);
456 EXPECT_EQ(1, observer
.present_changed_count_
);
457 EXPECT_FALSE(observer
.last_present_
);
459 EXPECT_FALSE(adapter_
->IsPresent());
461 // We should have had a device removed.
462 EXPECT_EQ(1, observer
.device_removed_count_
);
463 EXPECT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress
,
464 observer
.last_device_address_
);
466 // Other callbacks shouldn't be called since the values are false.
467 EXPECT_EQ(0, observer
.powered_changed_count_
);
468 EXPECT_EQ(0, observer
.discovering_changed_count_
);
469 EXPECT_FALSE(adapter_
->IsPowered());
470 EXPECT_FALSE(adapter_
->IsDiscovering());
473 TEST_F(BluetoothChromeOSTest
, SecondAdapter
) {
475 ASSERT_TRUE(adapter_
->IsPresent());
477 // Install an observer, then add a second adapter. Nothing should change,
478 // we ignore the second adapter.
479 TestObserver
observer(adapter_
);
481 fake_bluetooth_adapter_client_
->SetSecondVisible(true);
483 EXPECT_EQ(0, observer
.present_changed_count_
);
485 EXPECT_TRUE(adapter_
->IsPresent());
486 EXPECT_EQ(FakeBluetoothAdapterClient::kAdapterAddress
,
487 adapter_
->GetAddress());
489 // Try removing the first adapter, we should now act as if the adapter
490 // is no longer present rather than fall back to the second.
491 fake_bluetooth_adapter_client_
->SetVisible(false);
493 EXPECT_EQ(1, observer
.present_changed_count_
);
494 EXPECT_FALSE(observer
.last_present_
);
496 EXPECT_FALSE(adapter_
->IsPresent());
498 // We should have had a device removed.
499 EXPECT_EQ(1, observer
.device_removed_count_
);
500 EXPECT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress
,
501 observer
.last_device_address_
);
503 // Other callbacks shouldn't be called since the values are false.
504 EXPECT_EQ(0, observer
.powered_changed_count_
);
505 EXPECT_EQ(0, observer
.discovering_changed_count_
);
506 EXPECT_FALSE(adapter_
->IsPowered());
507 EXPECT_FALSE(adapter_
->IsDiscovering());
509 observer
.device_removed_count_
= 0;
511 // Removing the second adapter shouldn't set anything either.
512 fake_bluetooth_adapter_client_
->SetSecondVisible(false);
514 EXPECT_EQ(0, observer
.device_removed_count_
);
515 EXPECT_EQ(0, observer
.powered_changed_count_
);
516 EXPECT_EQ(0, observer
.discovering_changed_count_
);
519 TEST_F(BluetoothChromeOSTest
, BecomePowered
) {
521 ASSERT_FALSE(adapter_
->IsPowered());
523 // Install an observer; expect the AdapterPoweredChanged to be called
524 // with true, and IsPowered() to return true.
525 TestObserver
observer(adapter_
);
527 adapter_
->SetPowered(
529 base::Bind(&BluetoothChromeOSTest::Callback
,
530 base::Unretained(this)),
531 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
532 base::Unretained(this)));
533 EXPECT_EQ(1, callback_count_
);
534 EXPECT_EQ(0, error_callback_count_
);
536 EXPECT_EQ(1, observer
.powered_changed_count_
);
537 EXPECT_TRUE(observer
.last_powered_
);
539 EXPECT_TRUE(adapter_
->IsPowered());
542 TEST_F(BluetoothChromeOSTest
, BecomeNotPowered
) {
544 adapter_
->SetPowered(
546 base::Bind(&BluetoothChromeOSTest::Callback
,
547 base::Unretained(this)),
548 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
549 base::Unretained(this)));
550 EXPECT_EQ(1, callback_count_
);
551 EXPECT_EQ(0, error_callback_count_
);
554 ASSERT_TRUE(adapter_
->IsPowered());
556 // Install an observer; expect the AdapterPoweredChanged to be called
557 // with false, and IsPowered() to return false.
558 TestObserver
observer(adapter_
);
560 adapter_
->SetPowered(
562 base::Bind(&BluetoothChromeOSTest::Callback
,
563 base::Unretained(this)),
564 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
565 base::Unretained(this)));
566 EXPECT_EQ(1, callback_count_
);
567 EXPECT_EQ(0, error_callback_count_
);
569 EXPECT_EQ(1, observer
.powered_changed_count_
);
570 EXPECT_FALSE(observer
.last_powered_
);
572 EXPECT_FALSE(adapter_
->IsPowered());
575 TEST_F(BluetoothChromeOSTest
, ChangeAdapterName
) {
578 static const std::string
new_name(".__.");
582 base::Bind(&BluetoothChromeOSTest::Callback
,
583 base::Unretained(this)),
584 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
585 base::Unretained(this)));
586 EXPECT_EQ(1, callback_count_
);
587 EXPECT_EQ(0, error_callback_count_
);
589 EXPECT_EQ(new_name
, adapter_
->GetName());
592 TEST_F(BluetoothChromeOSTest
, BecomeDiscoverable
) {
594 ASSERT_FALSE(adapter_
->IsDiscoverable());
596 // Install an observer; expect the AdapterDiscoverableChanged to be called
597 // with true, and IsDiscoverable() to return true.
598 TestObserver
observer(adapter_
);
600 adapter_
->SetDiscoverable(
602 base::Bind(&BluetoothChromeOSTest::Callback
,
603 base::Unretained(this)),
604 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
605 base::Unretained(this)));
606 EXPECT_EQ(1, callback_count_
);
607 EXPECT_EQ(0, error_callback_count_
);
609 EXPECT_EQ(1, observer
.discoverable_changed_count_
);
611 EXPECT_TRUE(adapter_
->IsDiscoverable());
614 TEST_F(BluetoothChromeOSTest
, BecomeNotDiscoverable
) {
616 adapter_
->SetDiscoverable(
618 base::Bind(&BluetoothChromeOSTest::Callback
,
619 base::Unretained(this)),
620 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
621 base::Unretained(this)));
622 EXPECT_EQ(1, callback_count_
);
623 EXPECT_EQ(0, error_callback_count_
);
626 ASSERT_TRUE(adapter_
->IsDiscoverable());
628 // Install an observer; expect the AdapterDiscoverableChanged to be called
629 // with false, and IsDiscoverable() to return false.
630 TestObserver
observer(adapter_
);
632 adapter_
->SetDiscoverable(
634 base::Bind(&BluetoothChromeOSTest::Callback
,
635 base::Unretained(this)),
636 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
637 base::Unretained(this)));
638 EXPECT_EQ(1, callback_count_
);
639 EXPECT_EQ(0, error_callback_count_
);
641 EXPECT_EQ(1, observer
.discoverable_changed_count_
);
643 EXPECT_FALSE(adapter_
->IsDiscoverable());
646 TEST_F(BluetoothChromeOSTest
, StopDiscovery
) {
649 adapter_
->SetPowered(
651 base::Bind(&BluetoothChromeOSTest::Callback
,
652 base::Unretained(this)),
653 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
654 base::Unretained(this)));
655 adapter_
->StartDiscoverySession(
656 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
657 base::Unretained(this)),
658 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
659 base::Unretained(this)));
661 EXPECT_EQ(2, callback_count_
);
662 EXPECT_EQ(0, error_callback_count_
);
665 ASSERT_TRUE(adapter_
->IsPowered());
666 ASSERT_TRUE(adapter_
->IsDiscovering());
667 ASSERT_EQ((size_t)1, discovery_sessions_
.size());
668 ASSERT_TRUE(discovery_sessions_
[0]->IsActive());
670 // Install an observer; aside from the callback, expect the
671 // AdapterDiscoveringChanged method to be called and no longer to be
673 TestObserver
observer(adapter_
);
675 discovery_sessions_
[0]->Stop(
676 base::Bind(&BluetoothChromeOSTest::Callback
,
677 base::Unretained(this)),
678 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
679 base::Unretained(this)));
681 EXPECT_EQ(1, callback_count_
);
682 EXPECT_EQ(0, error_callback_count_
);
684 EXPECT_EQ(1, observer
.discovering_changed_count_
);
685 EXPECT_FALSE(observer
.last_discovering_
);
687 EXPECT_FALSE(adapter_
->IsDiscovering());
690 TEST_F(BluetoothChromeOSTest
, Discovery
) {
691 // Test a simulated discovery session.
692 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
695 TestObserver
observer(adapter_
);
697 adapter_
->SetPowered(
699 base::Bind(&BluetoothChromeOSTest::Callback
,
700 base::Unretained(this)),
701 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
702 base::Unretained(this)));
703 adapter_
->StartDiscoverySession(
704 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
705 base::Unretained(this)),
706 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
707 base::Unretained(this)));
709 EXPECT_EQ(2, callback_count_
);
710 EXPECT_EQ(0, error_callback_count_
);
713 ASSERT_TRUE(adapter_
->IsPowered());
714 ASSERT_TRUE(adapter_
->IsDiscovering());
715 ASSERT_EQ((size_t)1, discovery_sessions_
.size());
716 ASSERT_TRUE(discovery_sessions_
[0]->IsActive());
718 // First two devices to appear.
721 EXPECT_EQ(2, observer
.device_added_count_
);
722 EXPECT_EQ(FakeBluetoothDeviceClient::kLowEnergyAddress
,
723 observer
.last_device_address_
);
725 // Next we should get another two devices...
727 EXPECT_EQ(4, observer
.device_added_count_
);
729 // Okay, let's run forward until a device is actually removed...
730 while (!observer
.device_removed_count_
)
733 EXPECT_EQ(1, observer
.device_removed_count_
);
734 EXPECT_EQ(FakeBluetoothDeviceClient::kVanishingDeviceAddress
,
735 observer
.last_device_address_
);
738 TEST_F(BluetoothChromeOSTest
, PoweredAndDiscovering
) {
740 adapter_
->SetPowered(
742 base::Bind(&BluetoothChromeOSTest::Callback
,
743 base::Unretained(this)),
744 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
745 base::Unretained(this)));
746 adapter_
->StartDiscoverySession(
747 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
748 base::Unretained(this)),
749 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
750 base::Unretained(this)));
752 EXPECT_EQ(2, callback_count_
);
753 EXPECT_EQ(0, error_callback_count_
);
755 ASSERT_EQ((size_t)1, discovery_sessions_
.size());
756 ASSERT_TRUE(discovery_sessions_
[0]->IsActive());
758 // Stop the timers that the simulation uses
759 fake_bluetooth_device_client_
->EndDiscoverySimulation(
760 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
));
762 ASSERT_TRUE(adapter_
->IsPowered());
763 ASSERT_TRUE(adapter_
->IsDiscovering());
765 fake_bluetooth_adapter_client_
->SetVisible(false);
766 ASSERT_FALSE(adapter_
->IsPresent());
767 ASSERT_FALSE(discovery_sessions_
[0]->IsActive());
769 // Install an observer; expect the AdapterPresentChanged,
770 // AdapterPoweredChanged and AdapterDiscoveringChanged methods to be called
771 // with true, and IsPresent(), IsPowered() and IsDiscovering() to all
773 TestObserver
observer(adapter_
);
775 fake_bluetooth_adapter_client_
->SetVisible(true);
777 EXPECT_EQ(1, observer
.present_changed_count_
);
778 EXPECT_TRUE(observer
.last_present_
);
779 EXPECT_TRUE(adapter_
->IsPresent());
781 EXPECT_EQ(1, observer
.powered_changed_count_
);
782 EXPECT_TRUE(observer
.last_powered_
);
783 EXPECT_TRUE(adapter_
->IsPowered());
785 EXPECT_EQ(1, observer
.discovering_changed_count_
);
786 EXPECT_TRUE(observer
.last_discovering_
);
787 EXPECT_TRUE(adapter_
->IsDiscovering());
789 observer
.present_changed_count_
= 0;
790 observer
.powered_changed_count_
= 0;
791 observer
.discovering_changed_count_
= 0;
793 // Now mark the adapter not present again. Expect the methods to be called
794 // again, to reset the properties back to false
795 fake_bluetooth_adapter_client_
->SetVisible(false);
797 EXPECT_EQ(1, observer
.present_changed_count_
);
798 EXPECT_FALSE(observer
.last_present_
);
799 EXPECT_FALSE(adapter_
->IsPresent());
801 EXPECT_EQ(1, observer
.powered_changed_count_
);
802 EXPECT_FALSE(observer
.last_powered_
);
803 EXPECT_FALSE(adapter_
->IsPowered());
805 EXPECT_EQ(1, observer
.discovering_changed_count_
);
806 EXPECT_FALSE(observer
.last_discovering_
);
807 EXPECT_FALSE(adapter_
->IsDiscovering());
810 // This unit test asserts that the basic reference counting logic works
811 // correctly for discovery requests done via the BluetoothAdapter.
812 TEST_F(BluetoothChromeOSTest
, MultipleDiscoverySessions
) {
814 adapter_
->SetPowered(
816 base::Bind(&BluetoothChromeOSTest::Callback
,
817 base::Unretained(this)),
818 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
819 base::Unretained(this)));
820 EXPECT_EQ(1, callback_count_
);
821 EXPECT_EQ(0, error_callback_count_
);
822 EXPECT_TRUE(adapter_
->IsPowered());
825 TestObserver
observer(adapter_
);
827 EXPECT_EQ(0, observer
.discovering_changed_count_
);
828 EXPECT_FALSE(observer
.last_discovering_
);
829 EXPECT_FALSE(adapter_
->IsDiscovering());
831 // Request device discovery 3 times.
832 for (int i
= 0; i
< 3; i
++) {
833 adapter_
->StartDiscoverySession(
834 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
835 base::Unretained(this)),
836 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
837 base::Unretained(this)));
839 // Run only once, as there should have been one D-Bus call.
842 // The observer should have received the discovering changed event exactly
843 // once, the success callback should have been called 3 times and the adapter
844 // should be discovering.
845 EXPECT_EQ(1, observer
.discovering_changed_count_
);
846 EXPECT_EQ(3, callback_count_
);
847 EXPECT_EQ(0, error_callback_count_
);
848 EXPECT_TRUE(observer
.last_discovering_
);
849 EXPECT_TRUE(adapter_
->IsDiscovering());
850 ASSERT_EQ((size_t)3, discovery_sessions_
.size());
852 // Request to stop discovery twice.
853 for (int i
= 0; i
< 2; i
++) {
854 discovery_sessions_
[i
]->Stop(
855 base::Bind(&BluetoothChromeOSTest::Callback
,
856 base::Unretained(this)),
857 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
858 base::Unretained(this)));
861 // The observer should have received no additional discovering changed events,
862 // the success callback should have been called 2 times and the adapter should
863 // still be discovering.
864 EXPECT_EQ(1, observer
.discovering_changed_count_
);
865 EXPECT_EQ(5, callback_count_
);
866 EXPECT_EQ(0, error_callback_count_
);
867 EXPECT_TRUE(observer
.last_discovering_
);
868 EXPECT_TRUE(adapter_
->IsDiscovering());
869 EXPECT_TRUE(adapter_
->IsDiscovering());
870 EXPECT_FALSE(discovery_sessions_
[0]->IsActive());
871 EXPECT_FALSE(discovery_sessions_
[1]->IsActive());
872 EXPECT_TRUE(discovery_sessions_
[2]->IsActive());
874 // Request device discovery 3 times.
875 for (int i
= 0; i
< 3; i
++) {
876 adapter_
->StartDiscoverySession(
877 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
878 base::Unretained(this)),
879 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
880 base::Unretained(this)));
883 // The observer should have received no additional discovering changed events,
884 // the success callback should have been called 3 times and the adapter should
885 // still be discovering.
886 EXPECT_EQ(1, observer
.discovering_changed_count_
);
887 EXPECT_EQ(8, callback_count_
);
888 EXPECT_EQ(0, error_callback_count_
);
889 EXPECT_TRUE(observer
.last_discovering_
);
890 EXPECT_TRUE(adapter_
->IsDiscovering());
891 ASSERT_EQ((size_t)6, discovery_sessions_
.size());
893 // Request to stop discovery 4 times.
894 for (int i
= 2; i
< 6; i
++) {
895 discovery_sessions_
[i
]->Stop(
896 base::Bind(&BluetoothChromeOSTest::Callback
,
897 base::Unretained(this)),
898 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
899 base::Unretained(this)));
901 // Run only once, as there should have been one D-Bus call.
904 // The observer should have received the discovering changed event exactly
905 // once, the success callback should have been called 4 times and the adapter
906 // should no longer be discovering.
907 EXPECT_EQ(2, observer
.discovering_changed_count_
);
908 EXPECT_EQ(12, callback_count_
);
909 EXPECT_EQ(0, error_callback_count_
);
910 EXPECT_FALSE(observer
.last_discovering_
);
911 EXPECT_FALSE(adapter_
->IsDiscovering());
913 // All discovery sessions should be inactive.
914 for (int i
= 0; i
< 6; i
++)
915 EXPECT_FALSE(discovery_sessions_
[i
]->IsActive());
917 // Request to stop discovery on of the inactive sessions.
918 discovery_sessions_
[0]->Stop(
919 base::Bind(&BluetoothChromeOSTest::Callback
,
920 base::Unretained(this)),
921 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
922 base::Unretained(this)));
924 // The call should have failed.
925 EXPECT_EQ(2, observer
.discovering_changed_count_
);
926 EXPECT_EQ(12, callback_count_
);
927 EXPECT_EQ(1, error_callback_count_
);
928 EXPECT_FALSE(observer
.last_discovering_
);
929 EXPECT_FALSE(adapter_
->IsDiscovering());
932 // This unit test asserts that the reference counting logic works correctly in
933 // the cases when the adapter gets reset and D-Bus calls are made outside of
934 // the BluetoothAdapter.
935 TEST_F(BluetoothChromeOSTest
,
936 UnexpectedChangesDuringMultipleDiscoverySessions
) {
938 adapter_
->SetPowered(
940 base::Bind(&BluetoothChromeOSTest::Callback
,
941 base::Unretained(this)),
942 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
943 base::Unretained(this)));
944 EXPECT_EQ(1, callback_count_
);
945 EXPECT_EQ(0, error_callback_count_
);
946 EXPECT_TRUE(adapter_
->IsPowered());
949 TestObserver
observer(adapter_
);
951 EXPECT_EQ(0, observer
.discovering_changed_count_
);
952 EXPECT_FALSE(observer
.last_discovering_
);
953 EXPECT_FALSE(adapter_
->IsDiscovering());
955 // Request device discovery 3 times.
956 for (int i
= 0; i
< 3; i
++) {
957 adapter_
->StartDiscoverySession(
958 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
959 base::Unretained(this)),
960 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
961 base::Unretained(this)));
963 // Run only once, as there should have been one D-Bus call.
966 // The observer should have received the discovering changed event exactly
967 // once, the success callback should have been called 3 times and the adapter
968 // should be discovering.
969 EXPECT_EQ(1, observer
.discovering_changed_count_
);
970 EXPECT_EQ(3, callback_count_
);
971 EXPECT_EQ(0, error_callback_count_
);
972 EXPECT_TRUE(observer
.last_discovering_
);
973 EXPECT_TRUE(adapter_
->IsDiscovering());
974 ASSERT_EQ((size_t)3, discovery_sessions_
.size());
976 for (int i
= 0; i
< 3; i
++)
977 EXPECT_TRUE(discovery_sessions_
[i
]->IsActive());
979 // Stop the timers that the simulation uses
980 fake_bluetooth_device_client_
->EndDiscoverySimulation(
981 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
));
983 ASSERT_TRUE(adapter_
->IsPowered());
984 ASSERT_TRUE(adapter_
->IsDiscovering());
986 // Stop device discovery behind the adapter. The adapter and the observer
987 // should be notified of the change and the reference count should be reset.
988 // Even though FakeBluetoothAdapterClient does its own reference counting and
989 // we called 3 BluetoothAdapter::StartDiscoverySession 3 times, the
990 // FakeBluetoothAdapterClient's count should be only 1 and a single call to
991 // FakeBluetoothAdapterClient::StopDiscovery should work.
992 fake_bluetooth_adapter_client_
->StopDiscovery(
993 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
),
994 base::Bind(&BluetoothChromeOSTest::Callback
,
995 base::Unretained(this)),
996 base::Bind(&BluetoothChromeOSTest::DBusErrorCallback
,
997 base::Unretained(this)));
999 EXPECT_EQ(2, observer
.discovering_changed_count_
);
1000 EXPECT_EQ(4, callback_count_
);
1001 EXPECT_EQ(0, error_callback_count_
);
1002 EXPECT_FALSE(observer
.last_discovering_
);
1003 EXPECT_FALSE(adapter_
->IsDiscovering());
1005 // All discovery session instances should have been updated.
1006 for (int i
= 0; i
< 3; i
++)
1007 EXPECT_FALSE(discovery_sessions_
[i
]->IsActive());
1008 discovery_sessions_
.clear();
1010 // It should be possible to successfully start discovery.
1011 for (int i
= 0; i
< 2; i
++) {
1012 adapter_
->StartDiscoverySession(
1013 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
1014 base::Unretained(this)),
1015 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1016 base::Unretained(this)));
1018 // Run only once, as there should have been one D-Bus call.
1019 message_loop_
.Run();
1020 EXPECT_EQ(3, observer
.discovering_changed_count_
);
1021 EXPECT_EQ(6, callback_count_
);
1022 EXPECT_EQ(0, error_callback_count_
);
1023 EXPECT_TRUE(observer
.last_discovering_
);
1024 EXPECT_TRUE(adapter_
->IsDiscovering());
1025 ASSERT_EQ((size_t)2, discovery_sessions_
.size());
1027 for (int i
= 0; i
< 2; i
++)
1028 EXPECT_TRUE(discovery_sessions_
[i
]->IsActive());
1030 fake_bluetooth_device_client_
->EndDiscoverySimulation(
1031 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
));
1033 // Make the adapter disappear and appear. This will make it come back as
1034 // discovering. When this happens, the reference count should become and
1035 // remain 0 as no new request was made through the BluetoothAdapter.
1036 fake_bluetooth_adapter_client_
->SetVisible(false);
1037 ASSERT_FALSE(adapter_
->IsPresent());
1038 EXPECT_EQ(4, observer
.discovering_changed_count_
);
1039 EXPECT_EQ(6, callback_count_
);
1040 EXPECT_EQ(0, error_callback_count_
);
1041 EXPECT_FALSE(observer
.last_discovering_
);
1042 EXPECT_FALSE(adapter_
->IsDiscovering());
1044 for (int i
= 0; i
< 2; i
++)
1045 EXPECT_FALSE(discovery_sessions_
[i
]->IsActive());
1046 discovery_sessions_
.clear();
1048 fake_bluetooth_adapter_client_
->SetVisible(true);
1049 ASSERT_TRUE(adapter_
->IsPresent());
1050 EXPECT_EQ(5, observer
.discovering_changed_count_
);
1051 EXPECT_EQ(6, callback_count_
);
1052 EXPECT_EQ(0, error_callback_count_
);
1053 EXPECT_TRUE(observer
.last_discovering_
);
1054 EXPECT_TRUE(adapter_
->IsDiscovering());
1056 // Start and stop discovery. At this point, FakeBluetoothAdapterClient has
1057 // a reference count that is equal to 1. Pretend that this was done by an
1058 // application other than us. Starting and stopping discovery will succeed
1059 // but it won't cause the discovery state to change.
1060 adapter_
->StartDiscoverySession(
1061 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
1062 base::Unretained(this)),
1063 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1064 base::Unretained(this)));
1065 message_loop_
.Run(); // Run the loop, as there should have been a D-Bus call.
1066 EXPECT_EQ(5, observer
.discovering_changed_count_
);
1067 EXPECT_EQ(7, callback_count_
);
1068 EXPECT_EQ(0, error_callback_count_
);
1069 EXPECT_TRUE(observer
.last_discovering_
);
1070 EXPECT_TRUE(adapter_
->IsDiscovering());
1071 ASSERT_EQ((size_t)1, discovery_sessions_
.size());
1072 EXPECT_TRUE(discovery_sessions_
[0]->IsActive());
1074 discovery_sessions_
[0]->Stop(
1075 base::Bind(&BluetoothChromeOSTest::Callback
,
1076 base::Unretained(this)),
1077 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1078 base::Unretained(this)));
1079 message_loop_
.Run(); // Run the loop, as there should have been a D-Bus call.
1080 EXPECT_EQ(5, observer
.discovering_changed_count_
);
1081 EXPECT_EQ(8, callback_count_
);
1082 EXPECT_EQ(0, error_callback_count_
);
1083 EXPECT_TRUE(observer
.last_discovering_
);
1084 EXPECT_TRUE(adapter_
->IsDiscovering());
1085 EXPECT_FALSE(discovery_sessions_
[0]->IsActive());
1086 discovery_sessions_
.clear();
1088 // Start discovery again.
1089 adapter_
->StartDiscoverySession(
1090 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
1091 base::Unretained(this)),
1092 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1093 base::Unretained(this)));
1094 message_loop_
.Run(); // Run the loop, as there should have been a D-Bus call.
1095 EXPECT_EQ(5, observer
.discovering_changed_count_
);
1096 EXPECT_EQ(9, callback_count_
);
1097 EXPECT_EQ(0, error_callback_count_
);
1098 EXPECT_TRUE(observer
.last_discovering_
);
1099 EXPECT_TRUE(adapter_
->IsDiscovering());
1100 ASSERT_EQ((size_t)1, discovery_sessions_
.size());
1101 EXPECT_TRUE(discovery_sessions_
[0]->IsActive());
1103 // Stop discovery via D-Bus. The fake client's reference count will drop but
1104 // the discovery state won't change since our BluetoothAdapter also just
1105 // requested it via D-Bus.
1106 fake_bluetooth_adapter_client_
->StopDiscovery(
1107 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
),
1108 base::Bind(&BluetoothChromeOSTest::Callback
,
1109 base::Unretained(this)),
1110 base::Bind(&BluetoothChromeOSTest::DBusErrorCallback
,
1111 base::Unretained(this)));
1112 message_loop_
.Run();
1113 EXPECT_EQ(5, observer
.discovering_changed_count_
);
1114 EXPECT_EQ(10, callback_count_
);
1115 EXPECT_EQ(0, error_callback_count_
);
1116 EXPECT_TRUE(observer
.last_discovering_
);
1117 EXPECT_TRUE(adapter_
->IsDiscovering());
1119 // Now end the discovery session. This should change the adapter's discovery
1121 discovery_sessions_
[0]->Stop(
1122 base::Bind(&BluetoothChromeOSTest::Callback
,
1123 base::Unretained(this)),
1124 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1125 base::Unretained(this)));
1126 message_loop_
.Run();
1127 EXPECT_EQ(6, observer
.discovering_changed_count_
);
1128 EXPECT_EQ(11, callback_count_
);
1129 EXPECT_EQ(0, error_callback_count_
);
1130 EXPECT_FALSE(observer
.last_discovering_
);
1131 EXPECT_FALSE(adapter_
->IsDiscovering());
1132 EXPECT_FALSE(discovery_sessions_
[0]->IsActive());
1135 TEST_F(BluetoothChromeOSTest
, InvalidatedDiscoverySessions
) {
1137 adapter_
->SetPowered(
1139 base::Bind(&BluetoothChromeOSTest::Callback
,
1140 base::Unretained(this)),
1141 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1142 base::Unretained(this)));
1143 EXPECT_EQ(1, callback_count_
);
1144 EXPECT_EQ(0, error_callback_count_
);
1145 EXPECT_TRUE(adapter_
->IsPowered());
1146 callback_count_
= 0;
1148 TestObserver
observer(adapter_
);
1150 EXPECT_EQ(0, observer
.discovering_changed_count_
);
1151 EXPECT_FALSE(observer
.last_discovering_
);
1152 EXPECT_FALSE(adapter_
->IsDiscovering());
1154 // Request device discovery 3 times.
1155 for (int i
= 0; i
< 3; i
++) {
1156 adapter_
->StartDiscoverySession(
1157 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
1158 base::Unretained(this)),
1159 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1160 base::Unretained(this)));
1162 // Run only once, as there should have been one D-Bus call.
1163 message_loop_
.Run();
1165 // The observer should have received the discovering changed event exactly
1166 // once, the success callback should have been called 3 times and the adapter
1167 // should be discovering.
1168 EXPECT_EQ(1, observer
.discovering_changed_count_
);
1169 EXPECT_EQ(3, callback_count_
);
1170 EXPECT_EQ(0, error_callback_count_
);
1171 EXPECT_TRUE(observer
.last_discovering_
);
1172 EXPECT_TRUE(adapter_
->IsDiscovering());
1173 ASSERT_EQ((size_t)3, discovery_sessions_
.size());
1175 for (int i
= 0; i
< 3; i
++)
1176 EXPECT_TRUE(discovery_sessions_
[i
]->IsActive());
1178 // Stop the timers that the simulation uses
1179 fake_bluetooth_device_client_
->EndDiscoverySimulation(
1180 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
));
1182 ASSERT_TRUE(adapter_
->IsPowered());
1183 ASSERT_TRUE(adapter_
->IsDiscovering());
1185 // Delete all but one discovery session.
1186 discovery_sessions_
.pop_back();
1187 discovery_sessions_
.pop_back();
1188 ASSERT_EQ((size_t)1, discovery_sessions_
.size());
1189 EXPECT_TRUE(discovery_sessions_
[0]->IsActive());
1190 EXPECT_TRUE(adapter_
->IsDiscovering());
1192 // Stop device discovery behind the adapter. The one active discovery session
1193 // should become inactive, but more importantly, we shouldn't run into any
1194 // memory errors as the sessions that we explicitly deleted should get
1196 fake_bluetooth_adapter_client_
->StopDiscovery(
1197 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
),
1198 base::Bind(&BluetoothChromeOSTest::Callback
,
1199 base::Unretained(this)),
1200 base::Bind(&BluetoothChromeOSTest::DBusErrorCallback
,
1201 base::Unretained(this)));
1202 message_loop_
.Run();
1203 EXPECT_EQ(2, observer
.discovering_changed_count_
);
1204 EXPECT_EQ(4, callback_count_
);
1205 EXPECT_EQ(0, error_callback_count_
);
1206 EXPECT_FALSE(observer
.last_discovering_
);
1207 EXPECT_FALSE(adapter_
->IsDiscovering());
1208 EXPECT_FALSE(discovery_sessions_
[0]->IsActive());
1211 TEST_F(BluetoothChromeOSTest
, QueuedDiscoveryRequests
) {
1214 adapter_
->SetPowered(
1216 base::Bind(&BluetoothChromeOSTest::Callback
,
1217 base::Unretained(this)),
1218 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1219 base::Unretained(this)));
1220 EXPECT_EQ(1, callback_count_
);
1221 EXPECT_EQ(0, error_callback_count_
);
1222 EXPECT_TRUE(adapter_
->IsPowered());
1223 callback_count_
= 0;
1225 TestObserver
observer(adapter_
);
1227 EXPECT_EQ(0, observer
.discovering_changed_count_
);
1228 EXPECT_FALSE(observer
.last_discovering_
);
1229 EXPECT_FALSE(adapter_
->IsDiscovering());
1231 // Request to start discovery. The call should be pending.
1232 adapter_
->StartDiscoverySession(
1233 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
1234 base::Unretained(this)),
1235 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1236 base::Unretained(this)));
1237 EXPECT_EQ(0, callback_count_
);
1239 fake_bluetooth_device_client_
->EndDiscoverySimulation(
1240 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
));
1242 // The underlying adapter has started discovery, but our call hasn't returned
1244 EXPECT_EQ(1, observer
.discovering_changed_count_
);
1245 EXPECT_TRUE(observer
.last_discovering_
);
1246 EXPECT_TRUE(adapter_
->IsDiscovering());
1247 EXPECT_TRUE(discovery_sessions_
.empty());
1249 // Request to start discovery twice. These should get queued and there should
1250 // be no change in state.
1251 for (int i
= 0; i
< 2; i
++) {
1252 adapter_
->StartDiscoverySession(
1253 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
1254 base::Unretained(this)),
1255 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1256 base::Unretained(this)));
1258 EXPECT_EQ(0, callback_count_
);
1259 EXPECT_EQ(0, error_callback_count_
);
1260 EXPECT_EQ(1, observer
.discovering_changed_count_
);
1261 EXPECT_TRUE(observer
.last_discovering_
);
1262 EXPECT_TRUE(adapter_
->IsDiscovering());
1263 EXPECT_TRUE(discovery_sessions_
.empty());
1265 // Process the pending call. The queued calls should execute and the discovery
1266 // session reference count should increase.
1267 message_loop_
.Run();
1268 EXPECT_EQ(3, callback_count_
);
1269 EXPECT_EQ(0, error_callback_count_
);
1270 EXPECT_EQ(1, observer
.discovering_changed_count_
);
1271 EXPECT_TRUE(observer
.last_discovering_
);
1272 EXPECT_TRUE(adapter_
->IsDiscovering());
1273 ASSERT_EQ((size_t)3, discovery_sessions_
.size());
1275 // Verify the reference count by removing sessions 3 times. The last request
1276 // should remain pending.
1277 for (int i
= 0; i
< 3; i
++) {
1278 discovery_sessions_
[i
]->Stop(
1279 base::Bind(&BluetoothChromeOSTest::Callback
,
1280 base::Unretained(this)),
1281 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1282 base::Unretained(this)));
1284 EXPECT_EQ(5, callback_count_
);
1285 EXPECT_EQ(0, error_callback_count_
);
1286 EXPECT_EQ(2, observer
.discovering_changed_count_
);
1287 EXPECT_FALSE(observer
.last_discovering_
);
1288 EXPECT_FALSE(adapter_
->IsDiscovering());
1289 EXPECT_FALSE(discovery_sessions_
[0]->IsActive());
1290 EXPECT_FALSE(discovery_sessions_
[1]->IsActive());
1291 EXPECT_TRUE(discovery_sessions_
[2]->IsActive());
1293 // Request to stop the session whose call is pending should fail.
1294 discovery_sessions_
[2]->Stop(
1295 base::Bind(&BluetoothChromeOSTest::Callback
,
1296 base::Unretained(this)),
1297 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1298 base::Unretained(this)));
1299 EXPECT_EQ(5, callback_count_
);
1300 EXPECT_EQ(1, error_callback_count_
);
1301 EXPECT_EQ(2, observer
.discovering_changed_count_
);
1302 EXPECT_FALSE(observer
.last_discovering_
);
1303 EXPECT_FALSE(adapter_
->IsDiscovering());
1304 EXPECT_TRUE(discovery_sessions_
[2]->IsActive());
1306 // Request to start should get queued.
1307 adapter_
->StartDiscoverySession(
1308 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
1309 base::Unretained(this)),
1310 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1311 base::Unretained(this)));
1312 EXPECT_EQ(5, callback_count_
);
1313 EXPECT_EQ(1, error_callback_count_
);
1314 EXPECT_EQ(2, observer
.discovering_changed_count_
);
1315 EXPECT_FALSE(observer
.last_discovering_
);
1316 EXPECT_FALSE(adapter_
->IsDiscovering());
1317 ASSERT_EQ((size_t)3, discovery_sessions_
.size());
1319 // Run the pending request.
1320 message_loop_
.Run();
1321 EXPECT_EQ(6, callback_count_
);
1322 EXPECT_EQ(1, error_callback_count_
);
1323 EXPECT_EQ(3, observer
.discovering_changed_count_
);
1324 EXPECT_TRUE(observer
.last_discovering_
);
1325 EXPECT_TRUE(adapter_
->IsDiscovering());
1326 ASSERT_EQ((size_t)3, discovery_sessions_
.size());
1327 EXPECT_FALSE(discovery_sessions_
[2]->IsActive());
1329 // The queued request to start discovery should have been issued but is still
1330 // pending. Run the loop and verify.
1331 message_loop_
.Run();
1332 EXPECT_EQ(7, callback_count_
);
1333 EXPECT_EQ(1, error_callback_count_
);
1334 EXPECT_EQ(3, observer
.discovering_changed_count_
);
1335 EXPECT_TRUE(observer
.last_discovering_
);
1336 EXPECT_TRUE(adapter_
->IsDiscovering());
1337 ASSERT_EQ((size_t)4, discovery_sessions_
.size());
1338 EXPECT_TRUE(discovery_sessions_
[3]->IsActive());
1341 TEST_F(BluetoothChromeOSTest
, StartDiscoverySession
) {
1344 adapter_
->SetPowered(
1346 base::Bind(&BluetoothChromeOSTest::Callback
,
1347 base::Unretained(this)),
1348 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1349 base::Unretained(this)));
1350 EXPECT_EQ(1, callback_count_
);
1351 EXPECT_EQ(0, error_callback_count_
);
1352 EXPECT_TRUE(adapter_
->IsPowered());
1353 callback_count_
= 0;
1355 TestObserver
observer(adapter_
);
1357 EXPECT_EQ(0, observer
.discovering_changed_count_
);
1358 EXPECT_FALSE(observer
.last_discovering_
);
1359 EXPECT_FALSE(adapter_
->IsDiscovering());
1360 EXPECT_TRUE(discovery_sessions_
.empty());
1362 // Request a new discovery session.
1363 adapter_
->StartDiscoverySession(
1364 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
1365 base::Unretained(this)),
1366 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1367 base::Unretained(this)));
1368 message_loop_
.Run();
1369 EXPECT_EQ(1, observer
.discovering_changed_count_
);
1370 EXPECT_EQ(1, callback_count_
);
1371 EXPECT_EQ(0, error_callback_count_
);
1372 EXPECT_TRUE(observer
.last_discovering_
);
1373 EXPECT_TRUE(adapter_
->IsDiscovering());
1374 ASSERT_EQ((size_t)1, discovery_sessions_
.size());
1375 EXPECT_TRUE(discovery_sessions_
[0]->IsActive());
1377 // Start another session. A new one should be returned in the callback, which
1378 // in turn will destroy the previous session. Adapter should still be
1379 // discovering and the reference count should be 1.
1380 adapter_
->StartDiscoverySession(
1381 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
1382 base::Unretained(this)),
1383 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1384 base::Unretained(this)));
1385 message_loop_
.Run();
1386 EXPECT_EQ(1, observer
.discovering_changed_count_
);
1387 EXPECT_EQ(2, callback_count_
);
1388 EXPECT_EQ(0, error_callback_count_
);
1389 EXPECT_TRUE(observer
.last_discovering_
);
1390 EXPECT_TRUE(adapter_
->IsDiscovering());
1391 ASSERT_EQ((size_t)2, discovery_sessions_
.size());
1392 EXPECT_TRUE(discovery_sessions_
[0]->IsActive());
1394 // Request a new session.
1395 adapter_
->StartDiscoverySession(
1396 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
1397 base::Unretained(this)),
1398 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1399 base::Unretained(this)));
1400 message_loop_
.Run();
1401 EXPECT_EQ(1, observer
.discovering_changed_count_
);
1402 EXPECT_EQ(3, callback_count_
);
1403 EXPECT_EQ(0, error_callback_count_
);
1404 EXPECT_TRUE(observer
.last_discovering_
);
1405 EXPECT_TRUE(adapter_
->IsDiscovering());
1406 ASSERT_EQ((size_t)3, discovery_sessions_
.size());
1407 EXPECT_TRUE(discovery_sessions_
[1]->IsActive());
1408 EXPECT_NE(discovery_sessions_
[0], discovery_sessions_
[1]);
1410 // Stop the previous discovery session. The session should end but discovery
1412 discovery_sessions_
[0]->Stop(
1413 base::Bind(&BluetoothChromeOSTest::Callback
,
1414 base::Unretained(this)),
1415 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1416 base::Unretained(this)));
1417 message_loop_
.Run();
1418 EXPECT_EQ(1, observer
.discovering_changed_count_
);
1419 EXPECT_EQ(4, callback_count_
);
1420 EXPECT_EQ(0, error_callback_count_
);
1421 EXPECT_TRUE(observer
.last_discovering_
);
1422 EXPECT_TRUE(adapter_
->IsDiscovering());
1423 ASSERT_EQ((size_t)3, discovery_sessions_
.size());
1424 EXPECT_FALSE(discovery_sessions_
[0]->IsActive());
1425 EXPECT_TRUE(discovery_sessions_
[1]->IsActive());
1427 // Delete the current active session. Discovery should eventually stop.
1428 discovery_sessions_
.clear();
1429 while (observer
.last_discovering_
)
1430 message_loop_
.RunUntilIdle();
1432 EXPECT_EQ(2, observer
.discovering_changed_count_
);
1433 EXPECT_EQ(4, callback_count_
);
1434 EXPECT_EQ(0, error_callback_count_
);
1435 EXPECT_FALSE(observer
.last_discovering_
);
1436 EXPECT_FALSE(adapter_
->IsDiscovering());
1439 TEST_F(BluetoothChromeOSTest
, DeviceProperties
) {
1442 BluetoothAdapter::DeviceList devices
= adapter_
->GetDevices();
1443 ASSERT_EQ(1U, devices
.size());
1444 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress
,
1445 devices
[0]->GetAddress());
1447 // Verify the other device properties.
1448 EXPECT_EQ(base::UTF8ToUTF16(FakeBluetoothDeviceClient::kPairedDeviceName
),
1449 devices
[0]->GetName());
1450 EXPECT_EQ(BluetoothDevice::DEVICE_COMPUTER
, devices
[0]->GetDeviceType());
1451 EXPECT_TRUE(devices
[0]->IsPaired());
1452 EXPECT_FALSE(devices
[0]->IsConnected());
1453 EXPECT_FALSE(devices
[0]->IsConnecting());
1455 // Non HID devices are always connectable.
1456 EXPECT_TRUE(devices
[0]->IsConnectable());
1458 BluetoothDevice::UUIDList uuids
= devices
[0]->GetUUIDs();
1459 ASSERT_EQ(2U, uuids
.size());
1460 EXPECT_EQ(uuids
[0], BluetoothUUID("1800"));
1461 EXPECT_EQ(uuids
[1], BluetoothUUID("1801"));
1463 EXPECT_EQ(BluetoothDevice::VENDOR_ID_USB
, devices
[0]->GetVendorIDSource());
1464 EXPECT_EQ(0x05ac, devices
[0]->GetVendorID());
1465 EXPECT_EQ(0x030d, devices
[0]->GetProductID());
1466 EXPECT_EQ(0x0306, devices
[0]->GetDeviceID());
1469 TEST_F(BluetoothChromeOSTest
, DeviceClassChanged
) {
1470 // Simulate a change of class of a device, as sometimes occurs
1471 // during discovery.
1474 BluetoothAdapter::DeviceList devices
= adapter_
->GetDevices();
1475 ASSERT_EQ(1U, devices
.size());
1476 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress
,
1477 devices
[0]->GetAddress());
1478 ASSERT_EQ(BluetoothDevice::DEVICE_COMPUTER
, devices
[0]->GetDeviceType());
1480 // Install an observer; expect the DeviceChanged method to be called when
1481 // we change the class of the device.
1482 TestObserver
observer(adapter_
);
1484 FakeBluetoothDeviceClient::Properties
* properties
=
1485 fake_bluetooth_device_client_
->GetProperties(
1486 dbus::ObjectPath(FakeBluetoothDeviceClient::kPairedDevicePath
));
1488 properties
->bluetooth_class
.ReplaceValue(0x002580);
1490 EXPECT_EQ(1, observer
.device_changed_count_
);
1491 EXPECT_EQ(devices
[0], observer
.last_device_
);
1493 EXPECT_EQ(BluetoothDevice::DEVICE_MOUSE
, devices
[0]->GetDeviceType());
1496 TEST_F(BluetoothChromeOSTest
, DeviceNameChanged
) {
1497 // Simulate a change of name of a device.
1500 BluetoothAdapter::DeviceList devices
= adapter_
->GetDevices();
1501 ASSERT_EQ(1U, devices
.size());
1502 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress
,
1503 devices
[0]->GetAddress());
1504 ASSERT_EQ(base::UTF8ToUTF16(FakeBluetoothDeviceClient::kPairedDeviceName
),
1505 devices
[0]->GetName());
1507 // Install an observer; expect the DeviceChanged method to be called when
1508 // we change the alias of the device.
1509 TestObserver
observer(adapter_
);
1511 FakeBluetoothDeviceClient::Properties
* properties
=
1512 fake_bluetooth_device_client_
->GetProperties(
1513 dbus::ObjectPath(FakeBluetoothDeviceClient::kPairedDevicePath
));
1515 static const std::string
new_name("New Device Name");
1516 properties
->alias
.ReplaceValue(new_name
);
1518 EXPECT_EQ(1, observer
.device_changed_count_
);
1519 EXPECT_EQ(devices
[0], observer
.last_device_
);
1521 EXPECT_EQ(base::UTF8ToUTF16(new_name
), devices
[0]->GetName());
1524 TEST_F(BluetoothChromeOSTest
, DeviceUuidsChanged
) {
1525 // Simulate a change of advertised services of a device.
1528 BluetoothAdapter::DeviceList devices
= adapter_
->GetDevices();
1529 ASSERT_EQ(1U, devices
.size());
1530 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress
,
1531 devices
[0]->GetAddress());
1533 BluetoothDevice::UUIDList uuids
= devices
[0]->GetUUIDs();
1534 ASSERT_EQ(2U, uuids
.size());
1535 ASSERT_EQ(uuids
[0], BluetoothUUID("1800"));
1536 ASSERT_EQ(uuids
[1], BluetoothUUID("1801"));
1538 // Install an observer; expect the DeviceChanged method to be called when
1539 // we change the class of the device.
1540 TestObserver
observer(adapter_
);
1542 FakeBluetoothDeviceClient::Properties
* properties
=
1543 fake_bluetooth_device_client_
->GetProperties(
1544 dbus::ObjectPath(FakeBluetoothDeviceClient::kPairedDevicePath
));
1546 std::vector
<std::string
> new_uuids
;
1547 new_uuids
.push_back(uuids
[0].canonical_value());
1548 new_uuids
.push_back(uuids
[1].canonical_value());
1549 new_uuids
.push_back("0000110c-0000-1000-8000-00805f9b34fb");
1550 new_uuids
.push_back("0000110e-0000-1000-8000-00805f9b34fb");
1551 new_uuids
.push_back("0000110a-0000-1000-8000-00805f9b34fb");
1553 properties
->uuids
.ReplaceValue(new_uuids
);
1555 EXPECT_EQ(1, observer
.device_changed_count_
);
1556 EXPECT_EQ(devices
[0], observer
.last_device_
);
1558 // Fetching the value should give the new one.
1559 uuids
= devices
[0]->GetUUIDs();
1560 ASSERT_EQ(5U, uuids
.size());
1561 EXPECT_EQ(uuids
[0], BluetoothUUID("1800"));
1562 EXPECT_EQ(uuids
[1], BluetoothUUID("1801"));
1563 EXPECT_EQ(uuids
[2], BluetoothUUID("110c"));
1564 EXPECT_EQ(uuids
[3], BluetoothUUID("110e"));
1565 EXPECT_EQ(uuids
[4], BluetoothUUID("110a"));
1568 TEST_F(BluetoothChromeOSTest
, ForgetDevice
) {
1571 BluetoothAdapter::DeviceList devices
= adapter_
->GetDevices();
1572 ASSERT_EQ(1U, devices
.size());
1573 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress
,
1574 devices
[0]->GetAddress());
1576 std::string address
= devices
[0]->GetAddress();
1578 // Install an observer; expect the DeviceRemoved method to be called
1579 // with the device we remove.
1580 TestObserver
observer(adapter_
);
1583 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1584 base::Unretained(this)));
1585 EXPECT_EQ(0, error_callback_count_
);
1587 EXPECT_EQ(1, observer
.device_removed_count_
);
1588 EXPECT_EQ(address
, observer
.last_device_address_
);
1590 // GetDevices shouldn't return the device either.
1591 devices
= adapter_
->GetDevices();
1592 ASSERT_EQ(0U, devices
.size());
1595 TEST_F(BluetoothChromeOSTest
, ForgetUnpairedDevice
) {
1599 BluetoothDevice
* device
= adapter_
->GetDevice(
1600 FakeBluetoothDeviceClient::kConnectUnpairableAddress
);
1601 ASSERT_TRUE(device
!= NULL
);
1602 ASSERT_FALSE(device
->IsPaired());
1604 // Connect the device so it becomes trusted and remembered.
1607 base::Bind(&BluetoothChromeOSTest::Callback
,
1608 base::Unretained(this)),
1609 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
1610 base::Unretained(this)));
1612 ASSERT_EQ(1, callback_count_
);
1613 ASSERT_EQ(0, error_callback_count_
);
1614 callback_count_
= 0;
1616 ASSERT_TRUE(device
->IsConnected());
1617 ASSERT_FALSE(device
->IsConnecting());
1619 // Make sure the trusted property has been set to true.
1620 FakeBluetoothDeviceClient::Properties
* properties
=
1621 fake_bluetooth_device_client_
->GetProperties(
1622 dbus::ObjectPath(FakeBluetoothDeviceClient::kConnectUnpairablePath
));
1623 ASSERT_TRUE(properties
->trusted
.value());
1625 // Install an observer; expect the DeviceRemoved method to be called
1626 // with the device we remove.
1627 TestObserver
observer(adapter_
);
1630 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1631 base::Unretained(this)));
1632 EXPECT_EQ(0, error_callback_count_
);
1634 EXPECT_EQ(1, observer
.device_removed_count_
);
1635 EXPECT_EQ(FakeBluetoothDeviceClient::kConnectUnpairableAddress
,
1636 observer
.last_device_address_
);
1638 // GetDevices shouldn't return the device either.
1639 device
= adapter_
->GetDevice(
1640 FakeBluetoothDeviceClient::kConnectUnpairableAddress
);
1641 EXPECT_FALSE(device
!= NULL
);
1644 TEST_F(BluetoothChromeOSTest
, ConnectPairedDevice
) {
1647 BluetoothDevice
* device
= adapter_
->GetDevice(
1648 FakeBluetoothDeviceClient::kPairedDeviceAddress
);
1649 ASSERT_TRUE(device
!= NULL
);
1650 ASSERT_TRUE(device
->IsPaired());
1652 TestObserver
observer(adapter_
);
1654 // Connect without a pairing delegate; since the device is already Paired
1655 // this should succeed and the device should become connected.
1658 base::Bind(&BluetoothChromeOSTest::Callback
,
1659 base::Unretained(this)),
1660 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
1661 base::Unretained(this)));
1663 EXPECT_EQ(1, callback_count_
);
1664 EXPECT_EQ(0, error_callback_count_
);
1666 // Two changes for connecting, one for connected and one for for trusted
1667 // after connecting.
1668 EXPECT_EQ(4, observer
.device_changed_count_
);
1669 EXPECT_EQ(device
, observer
.last_device_
);
1671 EXPECT_TRUE(device
->IsConnected());
1672 EXPECT_FALSE(device
->IsConnecting());
1675 TEST_F(BluetoothChromeOSTest
, ConnectUnpairableDevice
) {
1679 BluetoothDevice
* device
= adapter_
->GetDevice(
1680 FakeBluetoothDeviceClient::kConnectUnpairableAddress
);
1681 ASSERT_TRUE(device
!= NULL
);
1682 ASSERT_FALSE(device
->IsPaired());
1684 TestObserver
observer(adapter_
);
1686 // Connect without a pairing delegate; since the device does not require
1687 // pairing, this should succeed and the device should become connected.
1690 base::Bind(&BluetoothChromeOSTest::Callback
,
1691 base::Unretained(this)),
1692 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
1693 base::Unretained(this)));
1695 EXPECT_EQ(1, callback_count_
);
1696 EXPECT_EQ(0, error_callback_count_
);
1698 // Two changes for connecting, one for connected, one for for trusted after
1699 // connection, and one for the reconnect mode (IsConnectable).
1700 EXPECT_EQ(5, observer
.device_changed_count_
);
1701 EXPECT_EQ(device
, observer
.last_device_
);
1703 EXPECT_TRUE(device
->IsConnected());
1704 EXPECT_FALSE(device
->IsConnecting());
1706 // Make sure the trusted property has been set to true.
1707 FakeBluetoothDeviceClient::Properties
* properties
=
1708 fake_bluetooth_device_client_
->GetProperties(
1709 dbus::ObjectPath(FakeBluetoothDeviceClient::kConnectUnpairablePath
));
1710 EXPECT_TRUE(properties
->trusted
.value());
1712 // Verify is a HID device and is not connectable.
1713 BluetoothDevice::UUIDList uuids
= device
->GetUUIDs();
1714 ASSERT_EQ(1U, uuids
.size());
1715 EXPECT_EQ(uuids
[0], BluetoothUUID("1124"));
1716 EXPECT_FALSE(device
->IsConnectable());
1719 TEST_F(BluetoothChromeOSTest
, ConnectConnectedDevice
) {
1722 BluetoothDevice
* device
= adapter_
->GetDevice(
1723 FakeBluetoothDeviceClient::kPairedDeviceAddress
);
1724 ASSERT_TRUE(device
!= NULL
);
1725 ASSERT_TRUE(device
->IsPaired());
1729 base::Bind(&BluetoothChromeOSTest::Callback
,
1730 base::Unretained(this)),
1731 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
1732 base::Unretained(this)));
1734 ASSERT_EQ(1, callback_count_
);
1735 ASSERT_EQ(0, error_callback_count_
);
1736 callback_count_
= 0;
1738 ASSERT_TRUE(device
->IsConnected());
1740 // Connect again; since the device is already Connected, this shouldn't do
1741 // anything to initiate the connection.
1742 TestObserver
observer(adapter_
);
1746 base::Bind(&BluetoothChromeOSTest::Callback
,
1747 base::Unretained(this)),
1748 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
1749 base::Unretained(this)));
1751 EXPECT_EQ(1, callback_count_
);
1752 EXPECT_EQ(0, error_callback_count_
);
1754 // The observer will be called because Connecting will toggle true and false,
1755 // and the trusted property will be updated to true.
1756 EXPECT_EQ(3, observer
.device_changed_count_
);
1758 EXPECT_TRUE(device
->IsConnected());
1759 EXPECT_FALSE(device
->IsConnecting());
1762 TEST_F(BluetoothChromeOSTest
, ConnectDeviceFails
) {
1766 BluetoothDevice
* device
= adapter_
->GetDevice(
1767 FakeBluetoothDeviceClient::kLegacyAutopairAddress
);
1768 ASSERT_TRUE(device
!= NULL
);
1769 ASSERT_FALSE(device
->IsPaired());
1771 TestObserver
observer(adapter_
);
1773 // Connect without a pairing delegate; since the device requires pairing,
1774 // this should fail with an error.
1777 base::Bind(&BluetoothChromeOSTest::Callback
,
1778 base::Unretained(this)),
1779 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
1780 base::Unretained(this)));
1782 EXPECT_EQ(0, callback_count_
);
1783 EXPECT_EQ(1, error_callback_count_
);
1784 EXPECT_EQ(BluetoothDevice::ERROR_FAILED
, last_connect_error_
);
1786 EXPECT_EQ(2, observer
.device_changed_count_
);
1788 EXPECT_FALSE(device
->IsConnected());
1789 EXPECT_FALSE(device
->IsConnecting());
1792 TEST_F(BluetoothChromeOSTest
, DisconnectDevice
) {
1795 BluetoothDevice
* device
= adapter_
->GetDevice(
1796 FakeBluetoothDeviceClient::kPairedDeviceAddress
);
1797 ASSERT_TRUE(device
!= NULL
);
1798 ASSERT_TRUE(device
->IsPaired());
1802 base::Bind(&BluetoothChromeOSTest::Callback
,
1803 base::Unretained(this)),
1804 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
1805 base::Unretained(this)));
1807 ASSERT_EQ(1, callback_count_
);
1808 ASSERT_EQ(0, error_callback_count_
);
1809 callback_count_
= 0;
1811 ASSERT_TRUE(device
->IsConnected());
1812 ASSERT_FALSE(device
->IsConnecting());
1814 // Disconnect the device, we should see the observer method fire and the
1815 // device get dropped.
1816 TestObserver
observer(adapter_
);
1819 base::Bind(&BluetoothChromeOSTest::Callback
,
1820 base::Unretained(this)),
1821 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1822 base::Unretained(this)));
1824 EXPECT_EQ(1, callback_count_
);
1825 EXPECT_EQ(0, error_callback_count_
);
1827 EXPECT_EQ(1, observer
.device_changed_count_
);
1828 EXPECT_EQ(device
, observer
.last_device_
);
1830 EXPECT_FALSE(device
->IsConnected());
1833 TEST_F(BluetoothChromeOSTest
, DisconnectUnconnectedDevice
) {
1836 BluetoothDevice
* device
= adapter_
->GetDevice(
1837 FakeBluetoothDeviceClient::kPairedDeviceAddress
);
1838 ASSERT_TRUE(device
!= NULL
);
1839 ASSERT_TRUE(device
->IsPaired());
1840 ASSERT_FALSE(device
->IsConnected());
1842 // Disconnect the device, we should see the observer method fire and the
1843 // device get dropped.
1844 TestObserver
observer(adapter_
);
1847 base::Bind(&BluetoothChromeOSTest::Callback
,
1848 base::Unretained(this)),
1849 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1850 base::Unretained(this)));
1852 EXPECT_EQ(0, callback_count_
);
1853 EXPECT_EQ(1, error_callback_count_
);
1855 EXPECT_EQ(0, observer
.device_changed_count_
);
1857 EXPECT_FALSE(device
->IsConnected());
1860 TEST_F(BluetoothChromeOSTest
, PairLegacyAutopair
) {
1861 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
1866 // The Legacy Autopair device requires no PIN or Passkey to pair because
1867 // the daemon provides 0000 to the device for us.
1868 BluetoothDevice
* device
= adapter_
->GetDevice(
1869 FakeBluetoothDeviceClient::kLegacyAutopairAddress
);
1870 ASSERT_TRUE(device
!= NULL
);
1871 ASSERT_FALSE(device
->IsPaired());
1873 TestObserver
observer(adapter_
);
1875 TestPairingDelegate pairing_delegate
;
1878 base::Bind(&BluetoothChromeOSTest::Callback
,
1879 base::Unretained(this)),
1880 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
1881 base::Unretained(this)));
1883 EXPECT_EQ(0, pairing_delegate
.call_count_
);
1884 EXPECT_TRUE(device
->IsConnecting());
1886 message_loop_
.Run();
1888 EXPECT_EQ(1, callback_count_
);
1889 EXPECT_EQ(0, error_callback_count_
);
1891 // Two changes for connecting, one change for connected, one for paired,
1892 // two for trusted (after pairing and connection), and one for the reconnect
1893 // mode (IsConnectable).
1894 EXPECT_EQ(7, observer
.device_changed_count_
);
1895 EXPECT_EQ(device
, observer
.last_device_
);
1897 EXPECT_TRUE(device
->IsConnected());
1898 EXPECT_FALSE(device
->IsConnecting());
1900 EXPECT_TRUE(device
->IsPaired());
1902 // Verify is a HID device and is connectable.
1903 BluetoothDevice::UUIDList uuids
= device
->GetUUIDs();
1904 ASSERT_EQ(1U, uuids
.size());
1905 EXPECT_EQ(uuids
[0], BluetoothUUID("1124"));
1906 EXPECT_TRUE(device
->IsConnectable());
1908 // Make sure the trusted property has been set to true.
1909 FakeBluetoothDeviceClient::Properties
* properties
=
1910 fake_bluetooth_device_client_
->GetProperties(
1911 dbus::ObjectPath(FakeBluetoothDeviceClient::kLegacyAutopairPath
));
1912 EXPECT_TRUE(properties
->trusted
.value());
1915 TEST_F(BluetoothChromeOSTest
, PairDisplayPinCode
) {
1916 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
1921 // Requires that we display a randomly generated PIN on the screen.
1922 BluetoothDevice
* device
= adapter_
->GetDevice(
1923 FakeBluetoothDeviceClient::kDisplayPinCodeAddress
);
1924 ASSERT_TRUE(device
!= NULL
);
1925 ASSERT_FALSE(device
->IsPaired());
1927 TestObserver
observer(adapter_
);
1929 TestPairingDelegate pairing_delegate
;
1932 base::Bind(&BluetoothChromeOSTest::Callback
,
1933 base::Unretained(this)),
1934 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
1935 base::Unretained(this)));
1937 EXPECT_EQ(1, pairing_delegate
.call_count_
);
1938 EXPECT_EQ(1, pairing_delegate
.display_pincode_count_
);
1939 EXPECT_EQ("123456", pairing_delegate
.last_pincode_
);
1940 EXPECT_TRUE(device
->IsConnecting());
1942 message_loop_
.Run();
1944 EXPECT_EQ(1, callback_count_
);
1945 EXPECT_EQ(0, error_callback_count_
);
1947 // Two changes for connecting, one change for connected, one for paired,
1948 // two for trusted (after pairing and connection), and one for the reconnect
1949 // mode (IsConnectable).
1950 EXPECT_EQ(7, observer
.device_changed_count_
);
1951 EXPECT_EQ(device
, observer
.last_device_
);
1953 EXPECT_TRUE(device
->IsConnected());
1954 EXPECT_FALSE(device
->IsConnecting());
1956 EXPECT_TRUE(device
->IsPaired());
1958 // Verify is a HID device and is connectable.
1959 BluetoothDevice::UUIDList uuids
= device
->GetUUIDs();
1960 ASSERT_EQ(1U, uuids
.size());
1961 EXPECT_EQ(uuids
[0], BluetoothUUID("1124"));
1962 EXPECT_TRUE(device
->IsConnectable());
1964 // Make sure the trusted property has been set to true.
1965 FakeBluetoothDeviceClient::Properties
* properties
=
1966 fake_bluetooth_device_client_
->GetProperties(
1967 dbus::ObjectPath(FakeBluetoothDeviceClient::kDisplayPinCodePath
));
1968 EXPECT_TRUE(properties
->trusted
.value());
1971 TEST_F(BluetoothChromeOSTest
, PairDisplayPasskey
) {
1972 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
1977 // Requires that we display a randomly generated Passkey on the screen,
1978 // and notifies us as it's typed in.
1979 BluetoothDevice
* device
= adapter_
->GetDevice(
1980 FakeBluetoothDeviceClient::kDisplayPasskeyAddress
);
1981 ASSERT_TRUE(device
!= NULL
);
1982 ASSERT_FALSE(device
->IsPaired());
1984 TestObserver
observer(adapter_
);
1986 TestPairingDelegate pairing_delegate
;
1989 base::Bind(&BluetoothChromeOSTest::Callback
,
1990 base::Unretained(this)),
1991 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
1992 base::Unretained(this)));
1994 // One call for DisplayPasskey() and one for KeysEntered().
1995 EXPECT_EQ(2, pairing_delegate
.call_count_
);
1996 EXPECT_EQ(1, pairing_delegate
.display_passkey_count_
);
1997 EXPECT_EQ(123456U, pairing_delegate
.last_passkey_
);
1998 EXPECT_EQ(1, pairing_delegate
.keys_entered_count_
);
1999 EXPECT_EQ(0U, pairing_delegate
.last_entered_
);
2001 EXPECT_TRUE(device
->IsConnecting());
2003 // One call to KeysEntered() for each key, including [enter].
2004 for(int i
= 1; i
<= 7; ++i
) {
2005 message_loop_
.Run();
2007 EXPECT_EQ(2 + i
, pairing_delegate
.call_count_
);
2008 EXPECT_EQ(1 + i
, pairing_delegate
.keys_entered_count_
);
2009 EXPECT_EQ(static_cast<uint32_t>(i
), pairing_delegate
.last_entered_
);
2012 message_loop_
.Run();
2014 // 8 KeysEntered notifications (0 to 7, inclusive) and one aditional call for
2015 // DisplayPasskey().
2016 EXPECT_EQ(9, pairing_delegate
.call_count_
);
2017 EXPECT_EQ(8, pairing_delegate
.keys_entered_count_
);
2018 EXPECT_EQ(7U, pairing_delegate
.last_entered_
);
2020 EXPECT_EQ(1, callback_count_
);
2021 EXPECT_EQ(0, error_callback_count_
);
2023 // Two changes for connecting, one change for connected, one for paired,
2024 // two for trusted (after pairing and connection), and one for the reconnect
2025 // mode (IsConnectable).
2026 EXPECT_EQ(7, observer
.device_changed_count_
);
2027 EXPECT_EQ(device
, observer
.last_device_
);
2029 EXPECT_TRUE(device
->IsConnected());
2030 EXPECT_FALSE(device
->IsConnecting());
2032 EXPECT_TRUE(device
->IsPaired());
2034 // Verify is a HID device.
2035 BluetoothDevice::UUIDList uuids
= device
->GetUUIDs();
2036 ASSERT_EQ(1U, uuids
.size());
2037 EXPECT_EQ(uuids
[0], BluetoothUUID("1124"));
2039 // And usually not connectable.
2040 EXPECT_FALSE(device
->IsConnectable());
2042 // Make sure the trusted property has been set to true.
2043 FakeBluetoothDeviceClient::Properties
* properties
=
2044 fake_bluetooth_device_client_
->GetProperties(
2045 dbus::ObjectPath(FakeBluetoothDeviceClient::kDisplayPasskeyPath
));
2046 EXPECT_TRUE(properties
->trusted
.value());
2049 TEST_F(BluetoothChromeOSTest
, PairRequestPinCode
) {
2050 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
2055 // Requires that the user enters a PIN for them.
2056 BluetoothDevice
* device
= adapter_
->GetDevice(
2057 FakeBluetoothDeviceClient::kRequestPinCodeAddress
);
2058 ASSERT_TRUE(device
!= NULL
);
2059 ASSERT_FALSE(device
->IsPaired());
2061 TestObserver
observer(adapter_
);
2063 TestPairingDelegate pairing_delegate
;
2066 base::Bind(&BluetoothChromeOSTest::Callback
,
2067 base::Unretained(this)),
2068 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
2069 base::Unretained(this)));
2071 EXPECT_EQ(1, pairing_delegate
.call_count_
);
2072 EXPECT_EQ(1, pairing_delegate
.request_pincode_count_
);
2073 EXPECT_TRUE(device
->IsConnecting());
2076 device
->SetPinCode("1234");
2077 message_loop_
.Run();
2079 EXPECT_EQ(1, callback_count_
);
2080 EXPECT_EQ(0, error_callback_count_
);
2082 // Two changes for connecting, one change for connected, one for paired and
2083 // two for trusted (after pairing and connection).
2084 EXPECT_EQ(6, observer
.device_changed_count_
);
2085 EXPECT_EQ(device
, observer
.last_device_
);
2087 EXPECT_TRUE(device
->IsConnected());
2088 EXPECT_FALSE(device
->IsConnecting());
2090 EXPECT_TRUE(device
->IsPaired());
2092 // Verify is not a HID device.
2093 BluetoothDevice::UUIDList uuids
= device
->GetUUIDs();
2094 ASSERT_EQ(0U, uuids
.size());
2096 // Non HID devices are always connectable.
2097 EXPECT_TRUE(device
->IsConnectable());
2099 // Make sure the trusted property has been set to true.
2100 FakeBluetoothDeviceClient::Properties
* properties
=
2101 fake_bluetooth_device_client_
->GetProperties(
2102 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath
));
2103 EXPECT_TRUE(properties
->trusted
.value());
2106 TEST_F(BluetoothChromeOSTest
, PairConfirmPasskey
) {
2107 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
2112 // Requests that we confirm a displayed passkey.
2113 BluetoothDevice
* device
= adapter_
->GetDevice(
2114 FakeBluetoothDeviceClient::kConfirmPasskeyAddress
);
2115 ASSERT_TRUE(device
!= NULL
);
2116 ASSERT_FALSE(device
->IsPaired());
2118 TestObserver
observer(adapter_
);
2120 TestPairingDelegate pairing_delegate
;
2123 base::Bind(&BluetoothChromeOSTest::Callback
,
2124 base::Unretained(this)),
2125 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
2126 base::Unretained(this)));
2128 EXPECT_EQ(1, pairing_delegate
.call_count_
);
2129 EXPECT_EQ(1, pairing_delegate
.confirm_passkey_count_
);
2130 EXPECT_EQ(123456U, pairing_delegate
.last_passkey_
);
2131 EXPECT_TRUE(device
->IsConnecting());
2133 // Confirm the passkey.
2134 device
->ConfirmPairing();
2135 message_loop_
.Run();
2137 EXPECT_EQ(1, callback_count_
);
2138 EXPECT_EQ(0, error_callback_count_
);
2140 // Two changes for connecting, one change for connected, one for paired and
2141 // two for trusted (after pairing and connection).
2142 EXPECT_EQ(6, observer
.device_changed_count_
);
2143 EXPECT_EQ(device
, observer
.last_device_
);
2145 EXPECT_TRUE(device
->IsConnected());
2146 EXPECT_FALSE(device
->IsConnecting());
2148 EXPECT_TRUE(device
->IsPaired());
2150 // Non HID devices are always connectable.
2151 EXPECT_TRUE(device
->IsConnectable());
2153 // Make sure the trusted property has been set to true.
2154 FakeBluetoothDeviceClient::Properties
* properties
=
2155 fake_bluetooth_device_client_
->GetProperties(
2156 dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath
));
2157 EXPECT_TRUE(properties
->trusted
.value());
2160 TEST_F(BluetoothChromeOSTest
, PairRequestPasskey
) {
2161 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
2166 // Requires that the user enters a Passkey, this would be some kind of
2167 // device that has a display, but doesn't use "just works" - maybe a car?
2168 BluetoothDevice
* device
= adapter_
->GetDevice(
2169 FakeBluetoothDeviceClient::kRequestPasskeyAddress
);
2170 ASSERT_TRUE(device
!= NULL
);
2171 ASSERT_FALSE(device
->IsPaired());
2173 TestObserver
observer(adapter_
);
2175 TestPairingDelegate pairing_delegate
;
2178 base::Bind(&BluetoothChromeOSTest::Callback
,
2179 base::Unretained(this)),
2180 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
2181 base::Unretained(this)));
2183 EXPECT_EQ(1, pairing_delegate
.call_count_
);
2184 EXPECT_EQ(1, pairing_delegate
.request_passkey_count_
);
2185 EXPECT_TRUE(device
->IsConnecting());
2188 device
->SetPasskey(1234);
2189 message_loop_
.Run();
2191 EXPECT_EQ(1, callback_count_
);
2192 EXPECT_EQ(0, error_callback_count_
);
2194 // Two changes for connecting, one change for connected, one for paired and
2195 // two for trusted (after pairing and connection).
2196 EXPECT_EQ(6, observer
.device_changed_count_
);
2197 EXPECT_EQ(device
, observer
.last_device_
);
2199 EXPECT_TRUE(device
->IsConnected());
2200 EXPECT_FALSE(device
->IsConnecting());
2202 EXPECT_TRUE(device
->IsPaired());
2204 // Non HID devices are always connectable.
2205 EXPECT_TRUE(device
->IsConnectable());
2207 // Make sure the trusted property has been set to true.
2208 FakeBluetoothDeviceClient::Properties
* properties
=
2209 fake_bluetooth_device_client_
->GetProperties(
2210 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath
));
2211 EXPECT_TRUE(properties
->trusted
.value());
2214 TEST_F(BluetoothChromeOSTest
, PairJustWorks
) {
2215 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
2220 // Uses just-works pairing, since this is an outgoing pairing, no delegate
2221 // interaction is required.
2222 BluetoothDevice
* device
= adapter_
->GetDevice(
2223 FakeBluetoothDeviceClient::kJustWorksAddress
);
2224 ASSERT_TRUE(device
!= NULL
);
2225 ASSERT_FALSE(device
->IsPaired());
2227 TestObserver
observer(adapter_
);
2229 TestPairingDelegate pairing_delegate
;
2232 base::Bind(&BluetoothChromeOSTest::Callback
,
2233 base::Unretained(this)),
2234 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
2235 base::Unretained(this)));
2237 EXPECT_EQ(0, pairing_delegate
.call_count_
);
2239 message_loop_
.Run();
2241 EXPECT_EQ(1, callback_count_
);
2242 EXPECT_EQ(0, error_callback_count_
);
2244 // Two changes for connecting, one change for connected, one for paired and
2245 // two for trusted (after pairing and connection).
2246 EXPECT_EQ(6, observer
.device_changed_count_
);
2247 EXPECT_EQ(device
, observer
.last_device_
);
2249 EXPECT_TRUE(device
->IsConnected());
2250 EXPECT_FALSE(device
->IsConnecting());
2252 EXPECT_TRUE(device
->IsPaired());
2254 // Non HID devices are always connectable.
2255 EXPECT_TRUE(device
->IsConnectable());
2257 // Make sure the trusted property has been set to true.
2258 FakeBluetoothDeviceClient::Properties
* properties
=
2259 fake_bluetooth_device_client_
->GetProperties(
2260 dbus::ObjectPath(FakeBluetoothDeviceClient::kJustWorksPath
));
2261 EXPECT_TRUE(properties
->trusted
.value());
2264 TEST_F(BluetoothChromeOSTest
, PairUnpairableDeviceFails
) {
2265 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
2268 DiscoverDevice(FakeBluetoothDeviceClient::kUnconnectableDeviceAddress
);
2270 BluetoothDevice
* device
= adapter_
->GetDevice(
2271 FakeBluetoothDeviceClient::kUnpairableDeviceAddress
);
2272 ASSERT_TRUE(device
!= NULL
);
2273 ASSERT_FALSE(device
->IsPaired());
2275 TestObserver
observer(adapter_
);
2277 TestPairingDelegate pairing_delegate
;
2280 base::Bind(&BluetoothChromeOSTest::Callback
,
2281 base::Unretained(this)),
2282 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
2283 base::Unretained(this)));
2285 EXPECT_EQ(0, pairing_delegate
.call_count_
);
2286 EXPECT_TRUE(device
->IsConnecting());
2288 // Run the loop to get the error..
2289 message_loop_
.Run();
2291 EXPECT_EQ(0, callback_count_
);
2292 EXPECT_EQ(1, error_callback_count_
);
2294 EXPECT_EQ(BluetoothDevice::ERROR_FAILED
, last_connect_error_
);
2296 EXPECT_FALSE(device
->IsConnected());
2297 EXPECT_FALSE(device
->IsConnecting());
2298 EXPECT_FALSE(device
->IsPaired());
2301 TEST_F(BluetoothChromeOSTest
, PairingFails
) {
2302 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
2305 DiscoverDevice(FakeBluetoothDeviceClient::kVanishingDeviceAddress
);
2307 // The vanishing device times out during pairing
2308 BluetoothDevice
* device
= adapter_
->GetDevice(
2309 FakeBluetoothDeviceClient::kVanishingDeviceAddress
);
2310 ASSERT_TRUE(device
!= NULL
);
2311 ASSERT_FALSE(device
->IsPaired());
2313 TestObserver
observer(adapter_
);
2315 TestPairingDelegate pairing_delegate
;
2318 base::Bind(&BluetoothChromeOSTest::Callback
,
2319 base::Unretained(this)),
2320 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
2321 base::Unretained(this)));
2323 EXPECT_EQ(0, pairing_delegate
.call_count_
);
2324 EXPECT_TRUE(device
->IsConnecting());
2326 // Run the loop to get the error..
2327 message_loop_
.Run();
2329 EXPECT_EQ(0, callback_count_
);
2330 EXPECT_EQ(1, error_callback_count_
);
2332 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_TIMEOUT
, last_connect_error_
);
2334 EXPECT_FALSE(device
->IsConnected());
2335 EXPECT_FALSE(device
->IsConnecting());
2336 EXPECT_FALSE(device
->IsPaired());
2339 TEST_F(BluetoothChromeOSTest
, PairingFailsAtConnection
) {
2340 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
2345 // Everything seems to go according to plan with the unconnectable device;
2346 // it pairs, but then you can't make connections to it after.
2347 BluetoothDevice
* device
= adapter_
->GetDevice(
2348 FakeBluetoothDeviceClient::kUnconnectableDeviceAddress
);
2349 ASSERT_TRUE(device
!= NULL
);
2350 ASSERT_FALSE(device
->IsPaired());
2352 TestObserver
observer(adapter_
);
2354 TestPairingDelegate pairing_delegate
;
2357 base::Bind(&BluetoothChromeOSTest::Callback
,
2358 base::Unretained(this)),
2359 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
2360 base::Unretained(this)));
2362 EXPECT_EQ(0, pairing_delegate
.call_count_
);
2363 EXPECT_TRUE(device
->IsConnecting());
2365 message_loop_
.Run();
2367 EXPECT_EQ(0, callback_count_
);
2368 EXPECT_EQ(1, error_callback_count_
);
2369 EXPECT_EQ(BluetoothDevice::ERROR_FAILED
, last_connect_error_
);
2371 // Two changes for connecting, one for paired and one for trusted after
2372 // pairing. The device should not be connected.
2373 EXPECT_EQ(4, observer
.device_changed_count_
);
2374 EXPECT_EQ(device
, observer
.last_device_
);
2376 EXPECT_FALSE(device
->IsConnected());
2377 EXPECT_FALSE(device
->IsConnecting());
2379 EXPECT_TRUE(device
->IsPaired());
2381 // Make sure the trusted property has been set to true still (since pairing
2383 FakeBluetoothDeviceClient::Properties
* properties
=
2384 fake_bluetooth_device_client_
->GetProperties(
2386 FakeBluetoothDeviceClient::kUnconnectableDevicePath
));
2387 EXPECT_TRUE(properties
->trusted
.value());
2390 TEST_F(BluetoothChromeOSTest
, PairingRejectedAtPinCode
) {
2391 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
2396 // Reject the pairing after we receive a request for the PIN code.
2397 BluetoothDevice
* device
= adapter_
->GetDevice(
2398 FakeBluetoothDeviceClient::kRequestPinCodeAddress
);
2399 ASSERT_TRUE(device
!= NULL
);
2400 ASSERT_FALSE(device
->IsPaired());
2402 TestObserver
observer(adapter_
);
2404 TestPairingDelegate pairing_delegate
;
2407 base::Bind(&BluetoothChromeOSTest::Callback
,
2408 base::Unretained(this)),
2409 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
2410 base::Unretained(this)));
2412 EXPECT_EQ(1, pairing_delegate
.call_count_
);
2413 EXPECT_EQ(1, pairing_delegate
.request_pincode_count_
);
2414 EXPECT_TRUE(device
->IsConnecting());
2416 // Reject the pairing.
2417 device
->RejectPairing();
2418 message_loop_
.Run();
2420 EXPECT_EQ(0, callback_count_
);
2421 EXPECT_EQ(1, error_callback_count_
);
2422 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_REJECTED
, last_connect_error_
);
2424 // Should be no changes except connecting going true and false.
2425 EXPECT_EQ(2, observer
.device_changed_count_
);
2426 EXPECT_FALSE(device
->IsConnected());
2427 EXPECT_FALSE(device
->IsConnecting());
2428 EXPECT_FALSE(device
->IsPaired());
2431 TEST_F(BluetoothChromeOSTest
, PairingCancelledAtPinCode
) {
2432 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
2437 // Cancel the pairing after we receive a request for the PIN code.
2438 BluetoothDevice
* device
= adapter_
->GetDevice(
2439 FakeBluetoothDeviceClient::kRequestPinCodeAddress
);
2440 ASSERT_TRUE(device
!= NULL
);
2441 ASSERT_FALSE(device
->IsPaired());
2443 TestObserver
observer(adapter_
);
2445 TestPairingDelegate pairing_delegate
;
2448 base::Bind(&BluetoothChromeOSTest::Callback
,
2449 base::Unretained(this)),
2450 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
2451 base::Unretained(this)));
2453 EXPECT_EQ(1, pairing_delegate
.call_count_
);
2454 EXPECT_EQ(1, pairing_delegate
.request_pincode_count_
);
2455 EXPECT_TRUE(device
->IsConnecting());
2457 // Cancel the pairing.
2458 device
->CancelPairing();
2459 message_loop_
.Run();
2461 EXPECT_EQ(0, callback_count_
);
2462 EXPECT_EQ(1, error_callback_count_
);
2463 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED
, last_connect_error_
);
2465 // Should be no changes except connecting going true and false.
2466 EXPECT_EQ(2, observer
.device_changed_count_
);
2467 EXPECT_FALSE(device
->IsConnected());
2468 EXPECT_FALSE(device
->IsConnecting());
2469 EXPECT_FALSE(device
->IsPaired());
2472 TEST_F(BluetoothChromeOSTest
, PairingRejectedAtPasskey
) {
2473 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
2478 // Reject the pairing after we receive a request for the passkey.
2479 BluetoothDevice
* device
= adapter_
->GetDevice(
2480 FakeBluetoothDeviceClient::kRequestPasskeyAddress
);
2481 ASSERT_TRUE(device
!= NULL
);
2482 ASSERT_FALSE(device
->IsPaired());
2484 TestObserver
observer(adapter_
);
2486 TestPairingDelegate pairing_delegate
;
2489 base::Bind(&BluetoothChromeOSTest::Callback
,
2490 base::Unretained(this)),
2491 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
2492 base::Unretained(this)));
2494 EXPECT_EQ(1, pairing_delegate
.call_count_
);
2495 EXPECT_EQ(1, pairing_delegate
.request_passkey_count_
);
2496 EXPECT_TRUE(device
->IsConnecting());
2498 // Reject the pairing.
2499 device
->RejectPairing();
2500 message_loop_
.Run();
2502 EXPECT_EQ(0, callback_count_
);
2503 EXPECT_EQ(1, error_callback_count_
);
2504 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_REJECTED
, last_connect_error_
);
2506 // Should be no changes except connecting going true and false.
2507 EXPECT_EQ(2, observer
.device_changed_count_
);
2508 EXPECT_FALSE(device
->IsConnected());
2509 EXPECT_FALSE(device
->IsConnecting());
2510 EXPECT_FALSE(device
->IsPaired());
2513 TEST_F(BluetoothChromeOSTest
, PairingCancelledAtPasskey
) {
2514 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
2519 // Cancel the pairing after we receive a request for the passkey.
2520 BluetoothDevice
* device
= adapter_
->GetDevice(
2521 FakeBluetoothDeviceClient::kRequestPasskeyAddress
);
2522 ASSERT_TRUE(device
!= NULL
);
2523 ASSERT_FALSE(device
->IsPaired());
2525 TestObserver
observer(adapter_
);
2527 TestPairingDelegate pairing_delegate
;
2530 base::Bind(&BluetoothChromeOSTest::Callback
,
2531 base::Unretained(this)),
2532 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
2533 base::Unretained(this)));
2535 EXPECT_EQ(1, pairing_delegate
.call_count_
);
2536 EXPECT_EQ(1, pairing_delegate
.request_passkey_count_
);
2537 EXPECT_TRUE(device
->IsConnecting());
2539 // Cancel the pairing.
2540 device
->CancelPairing();
2541 message_loop_
.Run();
2543 EXPECT_EQ(0, callback_count_
);
2544 EXPECT_EQ(1, error_callback_count_
);
2545 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED
, last_connect_error_
);
2547 // Should be no changes except connecting going true and false.
2548 EXPECT_EQ(2, observer
.device_changed_count_
);
2549 EXPECT_FALSE(device
->IsConnected());
2550 EXPECT_FALSE(device
->IsConnecting());
2551 EXPECT_FALSE(device
->IsPaired());
2554 TEST_F(BluetoothChromeOSTest
, PairingRejectedAtConfirmation
) {
2555 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
2560 // Reject the pairing after we receive a request for passkey confirmation.
2561 BluetoothDevice
* device
= adapter_
->GetDevice(
2562 FakeBluetoothDeviceClient::kConfirmPasskeyAddress
);
2563 ASSERT_TRUE(device
!= NULL
);
2564 ASSERT_FALSE(device
->IsPaired());
2566 TestObserver
observer(adapter_
);
2568 TestPairingDelegate pairing_delegate
;
2571 base::Bind(&BluetoothChromeOSTest::Callback
,
2572 base::Unretained(this)),
2573 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
2574 base::Unretained(this)));
2576 EXPECT_EQ(1, pairing_delegate
.call_count_
);
2577 EXPECT_EQ(1, pairing_delegate
.confirm_passkey_count_
);
2578 EXPECT_TRUE(device
->IsConnecting());
2580 // Reject the pairing.
2581 device
->RejectPairing();
2582 message_loop_
.Run();
2584 EXPECT_EQ(0, callback_count_
);
2585 EXPECT_EQ(1, error_callback_count_
);
2586 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_REJECTED
, last_connect_error_
);
2588 // Should be no changes except connecting going true and false.
2589 EXPECT_EQ(2, observer
.device_changed_count_
);
2590 EXPECT_FALSE(device
->IsConnected());
2591 EXPECT_FALSE(device
->IsConnecting());
2592 EXPECT_FALSE(device
->IsPaired());
2595 TEST_F(BluetoothChromeOSTest
, PairingCancelledAtConfirmation
) {
2596 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
2601 // Cancel the pairing after we receive a request for the passkey.
2602 BluetoothDevice
* device
= adapter_
->GetDevice(
2603 FakeBluetoothDeviceClient::kConfirmPasskeyAddress
);
2604 ASSERT_TRUE(device
!= NULL
);
2605 ASSERT_FALSE(device
->IsPaired());
2607 TestObserver
observer(adapter_
);
2609 TestPairingDelegate pairing_delegate
;
2612 base::Bind(&BluetoothChromeOSTest::Callback
,
2613 base::Unretained(this)),
2614 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
2615 base::Unretained(this)));
2617 EXPECT_EQ(1, pairing_delegate
.call_count_
);
2618 EXPECT_EQ(1, pairing_delegate
.confirm_passkey_count_
);
2619 EXPECT_TRUE(device
->IsConnecting());
2621 // Cancel the pairing.
2622 device
->CancelPairing();
2623 message_loop_
.Run();
2625 EXPECT_EQ(0, callback_count_
);
2626 EXPECT_EQ(1, error_callback_count_
);
2627 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED
, last_connect_error_
);
2629 // Should be no changes except connecting going true and false.
2630 EXPECT_EQ(2, observer
.device_changed_count_
);
2631 EXPECT_FALSE(device
->IsConnected());
2632 EXPECT_FALSE(device
->IsConnecting());
2633 EXPECT_FALSE(device
->IsPaired());
2636 TEST_F(BluetoothChromeOSTest
, PairingCancelledInFlight
) {
2637 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
2642 // Cancel the pairing while we're waiting for the remote host.
2643 BluetoothDevice
* device
= adapter_
->GetDevice(
2644 FakeBluetoothDeviceClient::kLegacyAutopairAddress
);
2645 ASSERT_TRUE(device
!= NULL
);
2646 ASSERT_FALSE(device
->IsPaired());
2648 TestObserver
observer(adapter_
);
2650 TestPairingDelegate pairing_delegate
;
2653 base::Bind(&BluetoothChromeOSTest::Callback
,
2654 base::Unretained(this)),
2655 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
2656 base::Unretained(this)));
2658 EXPECT_EQ(0, pairing_delegate
.call_count_
);
2659 EXPECT_TRUE(device
->IsConnecting());
2661 // Cancel the pairing.
2662 device
->CancelPairing();
2663 message_loop_
.Run();
2665 EXPECT_EQ(0, callback_count_
);
2666 EXPECT_EQ(1, error_callback_count_
);
2667 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED
, last_connect_error_
);
2669 // Should be no changes except connecting going true and false.
2670 EXPECT_EQ(2, observer
.device_changed_count_
);
2671 EXPECT_FALSE(device
->IsConnected());
2672 EXPECT_FALSE(device
->IsConnecting());
2673 EXPECT_FALSE(device
->IsPaired());
2676 TEST_F(BluetoothChromeOSTest
, IncomingPairRequestPinCode
) {
2677 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
2681 TestPairingDelegate pairing_delegate
;
2682 adapter_
->AddPairingDelegate(
2684 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH
);
2686 // Requires that we provide a PIN code.
2687 fake_bluetooth_device_client_
->CreateDevice(
2688 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
),
2689 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath
));
2690 BluetoothDevice
* device
= adapter_
->GetDevice(
2691 FakeBluetoothDeviceClient::kRequestPinCodeAddress
);
2692 ASSERT_TRUE(device
!= NULL
);
2693 ASSERT_FALSE(device
->IsPaired());
2695 TestObserver
observer(adapter_
);
2697 fake_bluetooth_device_client_
->SimulatePairing(
2698 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath
),
2700 base::Bind(&BluetoothChromeOSTest::Callback
,
2701 base::Unretained(this)),
2702 base::Bind(&BluetoothChromeOSTest::DBusErrorCallback
,
2703 base::Unretained(this)));
2705 EXPECT_EQ(1, pairing_delegate
.call_count_
);
2706 EXPECT_EQ(1, pairing_delegate
.request_pincode_count_
);
2709 device
->SetPinCode("1234");
2710 message_loop_
.Run();
2712 EXPECT_EQ(1, callback_count_
);
2713 EXPECT_EQ(0, error_callback_count_
);
2715 // One change for paired, and one for trusted.
2716 EXPECT_EQ(2, observer
.device_changed_count_
);
2717 EXPECT_EQ(device
, observer
.last_device_
);
2719 EXPECT_TRUE(device
->IsPaired());
2721 // Make sure the trusted property has been set to true.
2722 FakeBluetoothDeviceClient::Properties
* properties
=
2723 fake_bluetooth_device_client_
->GetProperties(
2724 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath
));
2725 ASSERT_TRUE(properties
->trusted
.value());
2727 // No pairing context should remain on the device.
2728 BluetoothDeviceChromeOS
* device_chromeos
=
2729 static_cast<BluetoothDeviceChromeOS
*>(device
);
2730 EXPECT_TRUE(device_chromeos
->GetPairing() == NULL
);
2733 TEST_F(BluetoothChromeOSTest
, IncomingPairConfirmPasskey
) {
2734 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
2738 TestPairingDelegate pairing_delegate
;
2739 adapter_
->AddPairingDelegate(
2741 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH
);
2743 // Requests that we confirm a displayed passkey.
2744 fake_bluetooth_device_client_
->CreateDevice(
2745 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
),
2746 dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath
));
2747 BluetoothDevice
* device
= adapter_
->GetDevice(
2748 FakeBluetoothDeviceClient::kConfirmPasskeyAddress
);
2749 ASSERT_TRUE(device
!= NULL
);
2750 ASSERT_FALSE(device
->IsPaired());
2752 TestObserver
observer(adapter_
);
2754 fake_bluetooth_device_client_
->SimulatePairing(
2755 dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath
),
2757 base::Bind(&BluetoothChromeOSTest::Callback
,
2758 base::Unretained(this)),
2759 base::Bind(&BluetoothChromeOSTest::DBusErrorCallback
,
2760 base::Unretained(this)));
2762 EXPECT_EQ(1, pairing_delegate
.call_count_
);
2763 EXPECT_EQ(1, pairing_delegate
.confirm_passkey_count_
);
2764 EXPECT_EQ(123456U, pairing_delegate
.last_passkey_
);
2766 // Confirm the passkey.
2767 device
->ConfirmPairing();
2768 message_loop_
.Run();
2770 EXPECT_EQ(1, callback_count_
);
2771 EXPECT_EQ(0, error_callback_count_
);
2773 // One change for paired, and one for trusted.
2774 EXPECT_EQ(2, observer
.device_changed_count_
);
2775 EXPECT_EQ(device
, observer
.last_device_
);
2777 EXPECT_TRUE(device
->IsPaired());
2779 // Make sure the trusted property has been set to true.
2780 FakeBluetoothDeviceClient::Properties
* properties
=
2781 fake_bluetooth_device_client_
->GetProperties(
2782 dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath
));
2783 ASSERT_TRUE(properties
->trusted
.value());
2785 // No pairing context should remain on the device.
2786 BluetoothDeviceChromeOS
* device_chromeos
=
2787 static_cast<BluetoothDeviceChromeOS
*>(device
);
2788 EXPECT_TRUE(device_chromeos
->GetPairing() == NULL
);
2791 TEST_F(BluetoothChromeOSTest
, IncomingPairRequestPasskey
) {
2792 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
2796 TestPairingDelegate pairing_delegate
;
2797 adapter_
->AddPairingDelegate(
2799 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH
);
2801 // Requests that we provide a Passkey.
2802 fake_bluetooth_device_client_
->CreateDevice(
2803 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
),
2804 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath
));
2805 BluetoothDevice
* device
= adapter_
->GetDevice(
2806 FakeBluetoothDeviceClient::kRequestPasskeyAddress
);
2807 ASSERT_TRUE(device
!= NULL
);
2808 ASSERT_FALSE(device
->IsPaired());
2810 TestObserver
observer(adapter_
);
2812 fake_bluetooth_device_client_
->SimulatePairing(
2813 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath
),
2815 base::Bind(&BluetoothChromeOSTest::Callback
,
2816 base::Unretained(this)),
2817 base::Bind(&BluetoothChromeOSTest::DBusErrorCallback
,
2818 base::Unretained(this)));
2820 EXPECT_EQ(1, pairing_delegate
.call_count_
);
2821 EXPECT_EQ(1, pairing_delegate
.request_passkey_count_
);
2824 device
->SetPasskey(1234);
2825 message_loop_
.Run();
2827 EXPECT_EQ(1, callback_count_
);
2828 EXPECT_EQ(0, error_callback_count_
);
2830 // One change for paired, and one for trusted.
2831 EXPECT_EQ(2, observer
.device_changed_count_
);
2832 EXPECT_EQ(device
, observer
.last_device_
);
2834 EXPECT_TRUE(device
->IsPaired());
2836 // Make sure the trusted property has been set to true.
2837 FakeBluetoothDeviceClient::Properties
* properties
=
2838 fake_bluetooth_device_client_
->GetProperties(
2839 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath
));
2840 ASSERT_TRUE(properties
->trusted
.value());
2842 // No pairing context should remain on the device.
2843 BluetoothDeviceChromeOS
* device_chromeos
=
2844 static_cast<BluetoothDeviceChromeOS
*>(device
);
2845 EXPECT_TRUE(device_chromeos
->GetPairing() == NULL
);
2848 TEST_F(BluetoothChromeOSTest
, IncomingPairJustWorks
) {
2849 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
2853 TestPairingDelegate pairing_delegate
;
2854 adapter_
->AddPairingDelegate(
2856 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH
);
2858 // Uses just-works pairing so, sinec this an incoming pairing, require
2859 // authorization from the user.
2860 fake_bluetooth_device_client_
->CreateDevice(
2861 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
),
2862 dbus::ObjectPath(FakeBluetoothDeviceClient::kJustWorksPath
));
2863 BluetoothDevice
* device
= adapter_
->GetDevice(
2864 FakeBluetoothDeviceClient::kJustWorksAddress
);
2865 ASSERT_TRUE(device
!= NULL
);
2866 ASSERT_FALSE(device
->IsPaired());
2868 TestObserver
observer(adapter_
);
2870 fake_bluetooth_device_client_
->SimulatePairing(
2871 dbus::ObjectPath(FakeBluetoothDeviceClient::kJustWorksPath
),
2873 base::Bind(&BluetoothChromeOSTest::Callback
,
2874 base::Unretained(this)),
2875 base::Bind(&BluetoothChromeOSTest::DBusErrorCallback
,
2876 base::Unretained(this)));
2878 EXPECT_EQ(1, pairing_delegate
.call_count_
);
2879 EXPECT_EQ(1, pairing_delegate
.authorize_pairing_count_
);
2881 // Confirm the pairing.
2882 device
->ConfirmPairing();
2883 message_loop_
.Run();
2885 EXPECT_EQ(1, callback_count_
);
2886 EXPECT_EQ(0, error_callback_count_
);
2888 // One change for paired, and one for trusted.
2889 EXPECT_EQ(2, observer
.device_changed_count_
);
2890 EXPECT_EQ(device
, observer
.last_device_
);
2892 EXPECT_TRUE(device
->IsPaired());
2894 // Make sure the trusted property has been set to true.
2895 FakeBluetoothDeviceClient::Properties
* properties
=
2896 fake_bluetooth_device_client_
->GetProperties(
2897 dbus::ObjectPath(FakeBluetoothDeviceClient::kJustWorksPath
));
2898 ASSERT_TRUE(properties
->trusted
.value());
2900 // No pairing context should remain on the device.
2901 BluetoothDeviceChromeOS
* device_chromeos
=
2902 static_cast<BluetoothDeviceChromeOS
*>(device
);
2903 EXPECT_TRUE(device_chromeos
->GetPairing() == NULL
);
2906 TEST_F(BluetoothChromeOSTest
, IncomingPairRequestPinCodeWithoutDelegate
) {
2907 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
2911 // Requires that we provide a PIN Code, without a pairing delegate,
2912 // that will be rejected.
2913 fake_bluetooth_device_client_
->CreateDevice(
2914 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
),
2915 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath
));
2916 BluetoothDevice
* device
= adapter_
->GetDevice(
2917 FakeBluetoothDeviceClient::kRequestPinCodeAddress
);
2918 ASSERT_TRUE(device
!= NULL
);
2919 ASSERT_FALSE(device
->IsPaired());
2921 TestObserver
observer(adapter_
);
2923 fake_bluetooth_device_client_
->SimulatePairing(
2924 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath
),
2926 base::Bind(&BluetoothChromeOSTest::Callback
,
2927 base::Unretained(this)),
2928 base::Bind(&BluetoothChromeOSTest::DBusErrorCallback
,
2929 base::Unretained(this)));
2931 message_loop_
.Run();
2933 EXPECT_EQ(0, callback_count_
);
2934 EXPECT_EQ(1, error_callback_count_
);
2935 EXPECT_EQ(bluetooth_device::kErrorAuthenticationRejected
, last_client_error_
);
2937 // No changes should be observer.
2938 EXPECT_EQ(0, observer
.device_changed_count_
);
2940 EXPECT_FALSE(device
->IsPaired());
2942 // No pairing context should remain on the device.
2943 BluetoothDeviceChromeOS
* device_chromeos
=
2944 static_cast<BluetoothDeviceChromeOS
*>(device
);
2945 EXPECT_TRUE(device_chromeos
->GetPairing() == NULL
);
2948 TEST_F(BluetoothChromeOSTest
, IncomingPairConfirmPasskeyWithoutDelegate
) {
2949 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
2953 // Requests that we confirm a displayed passkey, without a pairing delegate,
2954 // that will be rejected.
2955 fake_bluetooth_device_client_
->CreateDevice(
2956 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
),
2957 dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath
));
2958 BluetoothDevice
* device
= adapter_
->GetDevice(
2959 FakeBluetoothDeviceClient::kConfirmPasskeyAddress
);
2960 ASSERT_TRUE(device
!= NULL
);
2961 ASSERT_FALSE(device
->IsPaired());
2963 TestObserver
observer(adapter_
);
2965 fake_bluetooth_device_client_
->SimulatePairing(
2966 dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath
),
2968 base::Bind(&BluetoothChromeOSTest::Callback
,
2969 base::Unretained(this)),
2970 base::Bind(&BluetoothChromeOSTest::DBusErrorCallback
,
2971 base::Unretained(this)));
2973 message_loop_
.Run();
2975 EXPECT_EQ(0, callback_count_
);
2976 EXPECT_EQ(1, error_callback_count_
);
2977 EXPECT_EQ(bluetooth_device::kErrorAuthenticationRejected
, last_client_error_
);
2979 // No changes should be observer.
2980 EXPECT_EQ(0, observer
.device_changed_count_
);
2982 EXPECT_FALSE(device
->IsPaired());
2984 // No pairing context should remain on the device.
2985 BluetoothDeviceChromeOS
* device_chromeos
=
2986 static_cast<BluetoothDeviceChromeOS
*>(device
);
2987 EXPECT_TRUE(device_chromeos
->GetPairing() == NULL
);
2990 TEST_F(BluetoothChromeOSTest
, IncomingPairRequestPasskeyWithoutDelegate
) {
2991 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
2995 // Requests that we provide a displayed passkey, without a pairing delegate,
2996 // that will be rejected.
2997 fake_bluetooth_device_client_
->CreateDevice(
2998 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
),
2999 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath
));
3000 BluetoothDevice
* device
= adapter_
->GetDevice(
3001 FakeBluetoothDeviceClient::kRequestPasskeyAddress
);
3002 ASSERT_TRUE(device
!= NULL
);
3003 ASSERT_FALSE(device
->IsPaired());
3005 TestObserver
observer(adapter_
);
3007 fake_bluetooth_device_client_
->SimulatePairing(
3008 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath
),
3010 base::Bind(&BluetoothChromeOSTest::Callback
,
3011 base::Unretained(this)),
3012 base::Bind(&BluetoothChromeOSTest::DBusErrorCallback
,
3013 base::Unretained(this)));
3015 message_loop_
.Run();
3017 EXPECT_EQ(0, callback_count_
);
3018 EXPECT_EQ(1, error_callback_count_
);
3019 EXPECT_EQ(bluetooth_device::kErrorAuthenticationRejected
, last_client_error_
);
3021 // No changes should be observer.
3022 EXPECT_EQ(0, observer
.device_changed_count_
);
3024 EXPECT_FALSE(device
->IsPaired());
3026 // No pairing context should remain on the device.
3027 BluetoothDeviceChromeOS
* device_chromeos
=
3028 static_cast<BluetoothDeviceChromeOS
*>(device
);
3029 EXPECT_TRUE(device_chromeos
->GetPairing() == NULL
);
3032 TEST_F(BluetoothChromeOSTest
, IncomingPairJustWorksWithoutDelegate
) {
3033 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
3037 // Uses just-works pairing and thus requires authorization for incoming
3038 // pairings, without a pairing delegate, that will be rejected.
3039 fake_bluetooth_device_client_
->CreateDevice(
3040 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
),
3041 dbus::ObjectPath(FakeBluetoothDeviceClient::kJustWorksPath
));
3042 BluetoothDevice
* device
= adapter_
->GetDevice(
3043 FakeBluetoothDeviceClient::kJustWorksAddress
);
3044 ASSERT_TRUE(device
!= NULL
);
3045 ASSERT_FALSE(device
->IsPaired());
3047 TestObserver
observer(adapter_
);
3049 fake_bluetooth_device_client_
->SimulatePairing(
3050 dbus::ObjectPath(FakeBluetoothDeviceClient::kJustWorksPath
),
3052 base::Bind(&BluetoothChromeOSTest::Callback
,
3053 base::Unretained(this)),
3054 base::Bind(&BluetoothChromeOSTest::DBusErrorCallback
,
3055 base::Unretained(this)));
3057 message_loop_
.Run();
3059 EXPECT_EQ(0, callback_count_
);
3060 EXPECT_EQ(1, error_callback_count_
);
3061 EXPECT_EQ(bluetooth_device::kErrorAuthenticationRejected
, last_client_error_
);
3063 // No changes should be observer.
3064 EXPECT_EQ(0, observer
.device_changed_count_
);
3066 EXPECT_FALSE(device
->IsPaired());
3068 // No pairing context should remain on the device.
3069 BluetoothDeviceChromeOS
* device_chromeos
=
3070 static_cast<BluetoothDeviceChromeOS
*>(device
);
3071 EXPECT_TRUE(device_chromeos
->GetPairing() == NULL
);
3074 TEST_F(BluetoothChromeOSTest
, RemovePairingDelegateDuringPairing
) {
3075 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
3079 TestPairingDelegate pairing_delegate
;
3080 adapter_
->AddPairingDelegate(
3082 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH
);
3084 // Requests that we provide a Passkey.
3085 fake_bluetooth_device_client_
->CreateDevice(
3086 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
),
3087 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath
));
3088 BluetoothDevice
* device
= adapter_
->GetDevice(
3089 FakeBluetoothDeviceClient::kRequestPasskeyAddress
);
3090 ASSERT_TRUE(device
!= NULL
);
3091 ASSERT_FALSE(device
->IsPaired());
3093 TestObserver
observer(adapter_
);
3095 fake_bluetooth_device_client_
->SimulatePairing(
3096 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath
),
3098 base::Bind(&BluetoothChromeOSTest::Callback
,
3099 base::Unretained(this)),
3100 base::Bind(&BluetoothChromeOSTest::DBusErrorCallback
,
3101 base::Unretained(this)));
3103 EXPECT_EQ(1, pairing_delegate
.call_count_
);
3104 EXPECT_EQ(1, pairing_delegate
.request_passkey_count_
);
3106 // A pairing context should now be set on the device.
3107 BluetoothDeviceChromeOS
* device_chromeos
=
3108 static_cast<BluetoothDeviceChromeOS
*>(device
);
3109 ASSERT_TRUE(device_chromeos
->GetPairing() != NULL
);
3111 // Removing the pairing delegate should remove that pairing context.
3112 adapter_
->RemovePairingDelegate(&pairing_delegate
);
3114 EXPECT_TRUE(device_chromeos
->GetPairing() == NULL
);
3116 // Set the Passkey, this should now have no effect since the pairing has
3117 // been, in-effect, cancelled
3118 device
->SetPasskey(1234);
3120 EXPECT_EQ(0, callback_count_
);
3121 EXPECT_EQ(0, error_callback_count_
);
3122 EXPECT_EQ(0, observer
.device_changed_count_
);
3124 EXPECT_FALSE(device
->IsPaired());
3127 TEST_F(BluetoothChromeOSTest
, DeviceId
) {
3130 // Use the built-in paired device for this test, grab its Properties
3131 // structure so we can adjust the underlying modalias property.
3132 BluetoothDevice
* device
= adapter_
->GetDevice(
3133 FakeBluetoothDeviceClient::kPairedDeviceAddress
);
3134 FakeBluetoothDeviceClient::Properties
* properties
=
3135 fake_bluetooth_device_client_
->GetProperties(
3136 dbus::ObjectPath(FakeBluetoothDeviceClient::kPairedDevicePath
));
3138 ASSERT_TRUE(device
!= NULL
);
3139 ASSERT_TRUE(properties
!= NULL
);
3141 // Valid USB IF-assigned identifier.
3142 ASSERT_EQ("usb:v05ACp030Dd0306", properties
->modalias
.value());
3144 EXPECT_EQ(BluetoothDevice::VENDOR_ID_USB
, device
->GetVendorIDSource());
3145 EXPECT_EQ(0x05ac, device
->GetVendorID());
3146 EXPECT_EQ(0x030d, device
->GetProductID());
3147 EXPECT_EQ(0x0306, device
->GetDeviceID());
3149 // Valid Bluetooth SIG-assigned identifier.
3150 properties
->modalias
.ReplaceValue("bluetooth:v00E0p2400d0400");
3152 EXPECT_EQ(BluetoothDevice::VENDOR_ID_BLUETOOTH
, device
->GetVendorIDSource());
3153 EXPECT_EQ(0x00e0, device
->GetVendorID());
3154 EXPECT_EQ(0x2400, device
->GetProductID());
3155 EXPECT_EQ(0x0400, device
->GetDeviceID());
3157 // Invalid USB IF-assigned identifier.
3158 properties
->modalias
.ReplaceValue("usb:x00E0p2400d0400");
3160 EXPECT_EQ(BluetoothDevice::VENDOR_ID_UNKNOWN
, device
->GetVendorIDSource());
3161 EXPECT_EQ(0, device
->GetVendorID());
3162 EXPECT_EQ(0, device
->GetProductID());
3163 EXPECT_EQ(0, device
->GetDeviceID());
3165 // Invalid Bluetooth SIG-assigned identifier.
3166 properties
->modalias
.ReplaceValue("bluetooth:x00E0p2400d0400");
3168 EXPECT_EQ(BluetoothDevice::VENDOR_ID_UNKNOWN
, device
->GetVendorIDSource());
3169 EXPECT_EQ(0, device
->GetVendorID());
3170 EXPECT_EQ(0, device
->GetProductID());
3171 EXPECT_EQ(0, device
->GetDeviceID());
3173 // Unknown vendor specification identifier.
3174 properties
->modalias
.ReplaceValue("chrome:v00E0p2400d0400");
3176 EXPECT_EQ(BluetoothDevice::VENDOR_ID_UNKNOWN
, device
->GetVendorIDSource());
3177 EXPECT_EQ(0, device
->GetVendorID());
3178 EXPECT_EQ(0, device
->GetProductID());
3179 EXPECT_EQ(0, device
->GetDeviceID());
3182 } // namespace chromeos