1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/memory/scoped_vector.h"
6 #include "base/message_loop/message_loop.h"
7 #include "base/run_loop.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chromeos/dbus/dbus_thread_manager.h"
10 #include "chromeos/dbus/fake_bluetooth_adapter_client.h"
11 #include "chromeos/dbus/fake_bluetooth_agent_manager_client.h"
12 #include "chromeos/dbus/fake_bluetooth_device_client.h"
13 #include "chromeos/dbus/fake_bluetooth_gatt_service_client.h"
14 #include "chromeos/dbus/fake_bluetooth_input_client.h"
15 #include "dbus/object_path.h"
16 #include "device/bluetooth/bluetooth_adapter.h"
17 #include "device/bluetooth/bluetooth_adapter_chromeos.h"
18 #include "device/bluetooth/bluetooth_adapter_factory.h"
19 #include "device/bluetooth/bluetooth_device.h"
20 #include "device/bluetooth/bluetooth_device_chromeos.h"
21 #include "device/bluetooth/bluetooth_discovery_session.h"
22 #include "device/bluetooth/bluetooth_pairing_chromeos.h"
23 #include "testing/gtest/include/gtest/gtest.h"
24 #include "third_party/cros_system_api/dbus/service_constants.h"
26 using device::BluetoothAdapter
;
27 using device::BluetoothAdapterFactory
;
28 using device::BluetoothAudioSink
;
29 using device::BluetoothDevice
;
30 using device::BluetoothDiscoverySession
;
31 using device::BluetoothUUID
;
37 class TestObserver
: public BluetoothAdapter::Observer
{
39 TestObserver(scoped_refptr
<BluetoothAdapter
> adapter
)
40 : present_changed_count_(0),
41 powered_changed_count_(0),
42 discoverable_changed_count_(0),
43 discovering_changed_count_(0),
46 last_discovering_(false),
47 device_added_count_(0),
48 device_changed_count_(0),
49 device_removed_count_(0),
52 adapter_
->AddObserver(this);
55 ~TestObserver() override
{ adapter_
->RemoveObserver(this); }
57 void AdapterPresentChanged(BluetoothAdapter
* adapter
, bool present
) override
{
58 EXPECT_EQ(adapter_
.get(), adapter
);
60 ++present_changed_count_
;
61 last_present_
= present
;
64 void AdapterPoweredChanged(BluetoothAdapter
* adapter
, bool powered
) override
{
65 EXPECT_EQ(adapter_
.get(), adapter
);
67 ++powered_changed_count_
;
68 last_powered_
= powered
;
71 void AdapterDiscoverableChanged(BluetoothAdapter
* adapter
,
72 bool discoverable
) override
{
73 EXPECT_EQ(adapter_
.get(), adapter
);
75 ++discoverable_changed_count_
;
78 void AdapterDiscoveringChanged(BluetoothAdapter
* adapter
,
79 bool discovering
) override
{
80 EXPECT_EQ(adapter_
.get(), adapter
);
82 ++discovering_changed_count_
;
83 last_discovering_
= discovering
;
86 void DeviceAdded(BluetoothAdapter
* adapter
,
87 BluetoothDevice
* device
) override
{
88 EXPECT_EQ(adapter_
.get(), adapter
);
90 ++device_added_count_
;
91 last_device_
= device
;
92 last_device_address_
= device
->GetAddress();
97 void DeviceChanged(BluetoothAdapter
* adapter
,
98 BluetoothDevice
* device
) override
{
99 EXPECT_EQ(adapter_
.get(), adapter
);
101 ++device_changed_count_
;
102 last_device_
= device
;
103 last_device_address_
= device
->GetAddress();
108 void DeviceRemoved(BluetoothAdapter
* adapter
,
109 BluetoothDevice
* device
) override
{
110 EXPECT_EQ(adapter_
.get(), adapter
);
112 ++device_removed_count_
;
113 // Can't save device, it may be freed
114 last_device_address_
= device
->GetAddress();
119 int present_changed_count_
;
120 int powered_changed_count_
;
121 int discoverable_changed_count_
;
122 int discovering_changed_count_
;
125 bool last_discovering_
;
126 int device_added_count_
;
127 int device_changed_count_
;
128 int device_removed_count_
;
129 BluetoothDevice
* last_device_
;
130 std::string last_device_address_
;
133 // Some tests use a message loop since background processing is simulated;
134 // break out of those loops.
135 void QuitMessageLoop() {
136 if (base::MessageLoop::current() &&
137 base::MessageLoop::current()->is_running())
138 base::MessageLoop::current()->Quit();
141 scoped_refptr
<BluetoothAdapter
> adapter_
;
144 // Callback for BluetoothDevice::GetConnectionInfo() that simply saves the
145 // connection info to the bound argument.
146 void SaveConnectionInfo(BluetoothDevice::ConnectionInfo
* out
,
147 const BluetoothDevice::ConnectionInfo
& conn_info
) {
151 class FakeBluetoothProfileServiceProviderDelegate
152 : public chromeos::BluetoothProfileServiceProvider::Delegate
{
154 FakeBluetoothProfileServiceProviderDelegate() {}
156 // BluetoothProfileServiceProvider::Delegate:
157 void Released() override
{}
159 void NewConnection(const dbus::ObjectPath
&,
160 scoped_ptr
<dbus::FileDescriptor
>,
161 const BluetoothProfileServiceProvider::Delegate::Options
&,
162 const ConfirmationCallback
&) override
{}
164 void RequestDisconnection(const dbus::ObjectPath
&,
165 const ConfirmationCallback
&) override
{}
167 void Cancel() override
{}
172 class TestPairingDelegate
: public BluetoothDevice::PairingDelegate
{
174 TestPairingDelegate()
176 request_pincode_count_(0),
177 request_passkey_count_(0),
178 display_pincode_count_(0),
179 display_passkey_count_(0),
180 keys_entered_count_(0),
181 confirm_passkey_count_(0),
182 authorize_pairing_count_(0),
183 last_passkey_(9999999U),
184 last_entered_(999U) {}
185 ~TestPairingDelegate() override
{}
187 void RequestPinCode(BluetoothDevice
* device
) override
{
189 ++request_pincode_count_
;
193 void RequestPasskey(BluetoothDevice
* device
) override
{
195 ++request_passkey_count_
;
199 void DisplayPinCode(BluetoothDevice
* device
,
200 const std::string
& pincode
) override
{
202 ++display_pincode_count_
;
203 last_pincode_
= pincode
;
207 void DisplayPasskey(BluetoothDevice
* device
, uint32 passkey
) override
{
209 ++display_passkey_count_
;
210 last_passkey_
= passkey
;
214 void KeysEntered(BluetoothDevice
* device
, uint32 entered
) override
{
216 ++keys_entered_count_
;
217 last_entered_
= entered
;
221 void ConfirmPasskey(BluetoothDevice
* device
, uint32 passkey
) override
{
223 ++confirm_passkey_count_
;
224 last_passkey_
= passkey
;
228 void AuthorizePairing(BluetoothDevice
* device
) override
{
230 ++authorize_pairing_count_
;
235 int request_pincode_count_
;
236 int request_passkey_count_
;
237 int display_pincode_count_
;
238 int display_passkey_count_
;
239 int keys_entered_count_
;
240 int confirm_passkey_count_
;
241 int authorize_pairing_count_
;
242 uint32 last_passkey_
;
243 uint32 last_entered_
;
244 std::string last_pincode_
;
247 // Some tests use a message loop since background processing is simulated;
248 // break out of those loops.
249 void QuitMessageLoop() {
250 if (base::MessageLoop::current() &&
251 base::MessageLoop::current()->is_running())
252 base::MessageLoop::current()->Quit();
256 class BluetoothChromeOSTest
: public testing::Test
{
258 void SetUp() override
{
259 scoped_ptr
<DBusThreadManagerSetter
> dbus_setter
=
260 chromeos::DBusThreadManager::GetSetterForTesting();
261 // We need to initialize DBusThreadManager early to prevent
262 // Bluetooth*::Create() methods from picking the real instead of fake
264 fake_bluetooth_adapter_client_
= new FakeBluetoothAdapterClient
;
265 dbus_setter
->SetBluetoothAdapterClient(
266 scoped_ptr
<BluetoothAdapterClient
>(fake_bluetooth_adapter_client_
));
267 fake_bluetooth_device_client_
= new FakeBluetoothDeviceClient
;
268 dbus_setter
->SetBluetoothDeviceClient(
269 scoped_ptr
<BluetoothDeviceClient
>(fake_bluetooth_device_client_
));
270 dbus_setter
->SetBluetoothInputClient(
271 scoped_ptr
<BluetoothInputClient
>(new FakeBluetoothInputClient
));
272 dbus_setter
->SetBluetoothAgentManagerClient(
273 scoped_ptr
<BluetoothAgentManagerClient
>(
274 new FakeBluetoothAgentManagerClient
));
275 dbus_setter
->SetBluetoothGattServiceClient(
276 scoped_ptr
<BluetoothGattServiceClient
>(
277 new FakeBluetoothGattServiceClient
));
279 fake_bluetooth_adapter_client_
->SetSimulationIntervalMs(10);
282 error_callback_count_
= 0;
283 last_connect_error_
= BluetoothDevice::ERROR_UNKNOWN
;
284 last_client_error_
= "";
287 void TearDown() override
{
288 for (ScopedVector
<BluetoothDiscoverySession
>::iterator iter
=
289 discovery_sessions_
.begin();
290 iter
!= discovery_sessions_
.end();
292 BluetoothDiscoverySession
* session
= *iter
;
293 if (!session
->IsActive())
296 session
->Stop(GetCallback(), GetErrorCallback());
298 ASSERT_EQ(1, callback_count_
);
300 discovery_sessions_
.clear();
302 DBusThreadManager::Shutdown();
311 base::Closure
GetCallback() {
312 return base::Bind(&BluetoothChromeOSTest::Callback
, base::Unretained(this));
315 void DiscoverySessionCallback(
316 scoped_ptr
<BluetoothDiscoverySession
> discovery_session
) {
318 discovery_sessions_
.push_back(discovery_session
.release());
322 void AudioSinkAcquiredCallback(scoped_refptr
<BluetoothAudioSink
>) {
327 void ProfileRegisteredCallback(BluetoothAdapterProfileChromeOS
* profile
) {
328 adapter_profile_
= profile
;
333 void ErrorCallback() {
334 ++error_callback_count_
;
338 base::Closure
GetErrorCallback() {
339 return base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
340 base::Unretained(this));
343 void DBusErrorCallback(const std::string
& error_name
,
344 const std::string
& error_message
) {
345 ++error_callback_count_
;
346 last_client_error_
= error_name
;
350 void ConnectErrorCallback(BluetoothDevice::ConnectErrorCode error
) {
351 ++error_callback_count_
;
352 last_connect_error_
= error
;
355 void AudioSinkErrorCallback(BluetoothAudioSink::ErrorCode
) {
356 ++error_callback_count_
;
360 void ErrorCompletionCallback(const std::string
& error_message
) {
361 ++error_callback_count_
;
365 // Call to fill the adapter_ member with a BluetoothAdapter instance.
367 adapter_
= new BluetoothAdapterChromeOS();
368 ASSERT_TRUE(adapter_
.get() != NULL
);
369 ASSERT_TRUE(adapter_
->IsInitialized());
372 // Run a discovery phase until the named device is detected, or if the named
373 // device is not created, the discovery process ends without finding it.
375 // The correct behavior of discovery is tested by the "Discovery" test case
376 // without using this function.
377 void DiscoverDevice(const std::string
& address
) {
378 ASSERT_TRUE(adapter_
.get() != NULL
);
379 ASSERT_TRUE(base::MessageLoop::current() != NULL
);
380 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
382 TestObserver
observer(adapter_
);
384 adapter_
->SetPowered(true, GetCallback(), GetErrorCallback());
385 adapter_
->StartDiscoverySession(
386 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
387 base::Unretained(this)),
389 base::MessageLoop::current()->Run();
390 ASSERT_EQ(2, callback_count_
);
391 ASSERT_EQ(0, error_callback_count_
);
392 ASSERT_EQ((size_t)1, discovery_sessions_
.size());
393 ASSERT_TRUE(discovery_sessions_
[0]->IsActive());
396 ASSERT_TRUE(adapter_
->IsPowered());
397 ASSERT_TRUE(adapter_
->IsDiscovering());
399 while (!observer
.device_removed_count_
&&
400 observer
.last_device_address_
!= address
)
401 base::MessageLoop::current()->Run();
403 discovery_sessions_
[0]->Stop(GetCallback(), GetErrorCallback());
404 base::MessageLoop::current()->Run();
405 ASSERT_EQ(1, callback_count_
);
406 ASSERT_EQ(0, error_callback_count_
);
409 ASSERT_FALSE(adapter_
->IsDiscovering());
412 // Run a discovery phase so we have devices that can be paired with.
413 void DiscoverDevices() {
414 // Pass an invalid address for the device so that the discovery process
415 // completes with all devices.
416 DiscoverDevice("does not exist");
420 base::MessageLoop message_loop_
;
421 FakeBluetoothAdapterClient
* fake_bluetooth_adapter_client_
;
422 FakeBluetoothDeviceClient
* fake_bluetooth_device_client_
;
423 scoped_refptr
<BluetoothAdapter
> adapter_
;
426 int error_callback_count_
;
427 enum BluetoothDevice::ConnectErrorCode last_connect_error_
;
428 std::string last_client_error_
;
429 ScopedVector
<BluetoothDiscoverySession
> discovery_sessions_
;
430 BluetoothAdapterProfileChromeOS
* adapter_profile_
;
433 // Some tests use a message loop since background processing is simulated;
434 // break out of those loops.
435 void QuitMessageLoop() {
436 if (base::MessageLoop::current() &&
437 base::MessageLoop::current()->is_running())
438 base::MessageLoop::current()->Quit();
442 TEST_F(BluetoothChromeOSTest
, AlreadyPresent
) {
445 // This verifies that the class gets the list of adapters when created;
446 // and initializes with an existing adapter if there is one.
447 EXPECT_TRUE(adapter_
->IsPresent());
448 EXPECT_FALSE(adapter_
->IsPowered());
449 EXPECT_EQ(FakeBluetoothAdapterClient::kAdapterAddress
,
450 adapter_
->GetAddress());
451 EXPECT_FALSE(adapter_
->IsDiscovering());
453 // There should be a device
454 BluetoothAdapter::DeviceList devices
= adapter_
->GetDevices();
455 EXPECT_EQ(2U, devices
.size());
456 EXPECT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress
,
457 devices
[0]->GetAddress());
458 EXPECT_EQ(FakeBluetoothDeviceClient::kPairedUnconnectableDeviceAddress
,
459 devices
[1]->GetAddress());
462 TEST_F(BluetoothChromeOSTest
, BecomePresent
) {
463 fake_bluetooth_adapter_client_
->SetVisible(false);
465 ASSERT_FALSE(adapter_
->IsPresent());
467 // Install an observer; expect the AdapterPresentChanged to be called
468 // with true, and IsPresent() to return true.
469 TestObserver
observer(adapter_
);
471 fake_bluetooth_adapter_client_
->SetVisible(true);
473 EXPECT_EQ(1, observer
.present_changed_count_
);
474 EXPECT_TRUE(observer
.last_present_
);
476 EXPECT_TRUE(adapter_
->IsPresent());
478 // We should have had a device announced.
479 EXPECT_EQ(2, observer
.device_added_count_
);
480 EXPECT_EQ(FakeBluetoothDeviceClient::kPairedUnconnectableDeviceAddress
,
481 observer
.last_device_address_
);
483 // Other callbacks shouldn't be called if the values are false.
484 EXPECT_EQ(0, observer
.powered_changed_count_
);
485 EXPECT_EQ(0, observer
.discovering_changed_count_
);
486 EXPECT_FALSE(adapter_
->IsPowered());
487 EXPECT_FALSE(adapter_
->IsDiscovering());
490 TEST_F(BluetoothChromeOSTest
, BecomeNotPresent
) {
492 ASSERT_TRUE(adapter_
->IsPresent());
494 // Install an observer; expect the AdapterPresentChanged to be called
495 // with false, and IsPresent() to return false.
496 TestObserver
observer(adapter_
);
498 fake_bluetooth_adapter_client_
->SetVisible(false);
500 EXPECT_EQ(1, observer
.present_changed_count_
);
501 EXPECT_FALSE(observer
.last_present_
);
503 EXPECT_FALSE(adapter_
->IsPresent());
505 // We should have had a device removed.
506 EXPECT_EQ(2, observer
.device_removed_count_
);
507 EXPECT_EQ(FakeBluetoothDeviceClient::kPairedUnconnectableDeviceAddress
,
508 observer
.last_device_address_
);
510 // Other callbacks shouldn't be called since the values are false.
511 EXPECT_EQ(0, observer
.powered_changed_count_
);
512 EXPECT_EQ(0, observer
.discovering_changed_count_
);
513 EXPECT_FALSE(adapter_
->IsPowered());
514 EXPECT_FALSE(adapter_
->IsDiscovering());
517 TEST_F(BluetoothChromeOSTest
, SecondAdapter
) {
519 ASSERT_TRUE(adapter_
->IsPresent());
521 // Install an observer, then add a second adapter. Nothing should change,
522 // we ignore the second adapter.
523 TestObserver
observer(adapter_
);
525 fake_bluetooth_adapter_client_
->SetSecondVisible(true);
527 EXPECT_EQ(0, observer
.present_changed_count_
);
529 EXPECT_TRUE(adapter_
->IsPresent());
530 EXPECT_EQ(FakeBluetoothAdapterClient::kAdapterAddress
,
531 adapter_
->GetAddress());
533 // Try removing the first adapter, we should now act as if the adapter
534 // is no longer present rather than fall back to the second.
535 fake_bluetooth_adapter_client_
->SetVisible(false);
537 EXPECT_EQ(1, observer
.present_changed_count_
);
538 EXPECT_FALSE(observer
.last_present_
);
540 EXPECT_FALSE(adapter_
->IsPresent());
542 // We should have had a device removed.
543 EXPECT_EQ(2, observer
.device_removed_count_
);
544 EXPECT_EQ(FakeBluetoothDeviceClient::kPairedUnconnectableDeviceAddress
,
545 observer
.last_device_address_
);
547 // Other callbacks shouldn't be called since the values are false.
548 EXPECT_EQ(0, observer
.powered_changed_count_
);
549 EXPECT_EQ(0, observer
.discovering_changed_count_
);
550 EXPECT_FALSE(adapter_
->IsPowered());
551 EXPECT_FALSE(adapter_
->IsDiscovering());
553 observer
.device_removed_count_
= 0;
555 // Removing the second adapter shouldn't set anything either.
556 fake_bluetooth_adapter_client_
->SetSecondVisible(false);
558 EXPECT_EQ(0, observer
.device_removed_count_
);
559 EXPECT_EQ(0, observer
.powered_changed_count_
);
560 EXPECT_EQ(0, observer
.discovering_changed_count_
);
563 TEST_F(BluetoothChromeOSTest
, BecomePowered
) {
565 ASSERT_FALSE(adapter_
->IsPowered());
567 // Install an observer; expect the AdapterPoweredChanged to be called
568 // with true, and IsPowered() to return true.
569 TestObserver
observer(adapter_
);
571 adapter_
->SetPowered(true, GetCallback(), GetErrorCallback());
572 EXPECT_EQ(1, callback_count_
);
573 EXPECT_EQ(0, error_callback_count_
);
575 EXPECT_EQ(1, observer
.powered_changed_count_
);
576 EXPECT_TRUE(observer
.last_powered_
);
578 EXPECT_TRUE(adapter_
->IsPowered());
581 TEST_F(BluetoothChromeOSTest
, BecomeNotPowered
) {
583 adapter_
->SetPowered(true, GetCallback(), GetErrorCallback());
584 EXPECT_EQ(1, callback_count_
);
585 EXPECT_EQ(0, error_callback_count_
);
588 ASSERT_TRUE(adapter_
->IsPowered());
590 // Install an observer; expect the AdapterPoweredChanged to be called
591 // with false, and IsPowered() to return false.
592 TestObserver
observer(adapter_
);
594 adapter_
->SetPowered(false, GetCallback(), GetErrorCallback());
595 EXPECT_EQ(1, callback_count_
);
596 EXPECT_EQ(0, error_callback_count_
);
598 EXPECT_EQ(1, observer
.powered_changed_count_
);
599 EXPECT_FALSE(observer
.last_powered_
);
601 EXPECT_FALSE(adapter_
->IsPowered());
604 TEST_F(BluetoothChromeOSTest
, SetPoweredWhenNotPresent
) {
606 ASSERT_TRUE(adapter_
->IsPresent());
608 // Install an observer; expect the AdapterPresentChanged to be called
609 // with false, and IsPresent() to return false.
610 TestObserver
observer(adapter_
);
612 fake_bluetooth_adapter_client_
->SetVisible(false);
614 EXPECT_EQ(1, observer
.present_changed_count_
);
615 EXPECT_FALSE(observer
.last_present_
);
617 EXPECT_FALSE(adapter_
->IsPresent());
618 EXPECT_FALSE(adapter_
->IsPowered());
620 adapter_
->SetPowered(true, GetCallback(), GetErrorCallback());
621 EXPECT_EQ(0, callback_count_
);
622 EXPECT_EQ(1, error_callback_count_
);
624 EXPECT_EQ(0, observer
.powered_changed_count_
);
625 EXPECT_FALSE(observer
.last_powered_
);
627 EXPECT_FALSE(adapter_
->IsPowered());
630 TEST_F(BluetoothChromeOSTest
, ChangeAdapterName
) {
633 static const std::string
new_name(".__.");
635 adapter_
->SetName(new_name
, GetCallback(), GetErrorCallback());
636 EXPECT_EQ(1, callback_count_
);
637 EXPECT_EQ(0, error_callback_count_
);
639 EXPECT_EQ(new_name
, adapter_
->GetName());
642 TEST_F(BluetoothChromeOSTest
, ChangeAdapterNameWhenNotPresent
) {
644 ASSERT_TRUE(adapter_
->IsPresent());
646 // Install an observer; expect the AdapterPresentChanged to be called
647 // with false, and IsPresent() to return false.
648 TestObserver
observer(adapter_
);
650 fake_bluetooth_adapter_client_
->SetVisible(false);
652 EXPECT_EQ(1, observer
.present_changed_count_
);
653 EXPECT_FALSE(observer
.last_present_
);
655 EXPECT_FALSE(adapter_
->IsPresent());
656 EXPECT_FALSE(adapter_
->IsPowered());
658 adapter_
->SetName("^o^", GetCallback(), GetErrorCallback());
659 EXPECT_EQ(0, callback_count_
);
660 EXPECT_EQ(1, error_callback_count_
);
662 EXPECT_EQ("", adapter_
->GetName());
665 TEST_F(BluetoothChromeOSTest
, BecomeDiscoverable
) {
667 ASSERT_FALSE(adapter_
->IsDiscoverable());
669 // Install an observer; expect the AdapterDiscoverableChanged to be called
670 // with true, and IsDiscoverable() to return true.
671 TestObserver
observer(adapter_
);
673 adapter_
->SetDiscoverable(true, GetCallback(), GetErrorCallback());
674 EXPECT_EQ(1, callback_count_
);
675 EXPECT_EQ(0, error_callback_count_
);
677 EXPECT_EQ(1, observer
.discoverable_changed_count_
);
679 EXPECT_TRUE(adapter_
->IsDiscoverable());
682 TEST_F(BluetoothChromeOSTest
, BecomeNotDiscoverable
) {
684 adapter_
->SetDiscoverable(true, GetCallback(), GetErrorCallback());
685 EXPECT_EQ(1, callback_count_
);
686 EXPECT_EQ(0, error_callback_count_
);
689 ASSERT_TRUE(adapter_
->IsDiscoverable());
691 // Install an observer; expect the AdapterDiscoverableChanged to be called
692 // with false, and IsDiscoverable() to return false.
693 TestObserver
observer(adapter_
);
695 adapter_
->SetDiscoverable(false, GetCallback(), GetErrorCallback());
696 EXPECT_EQ(1, callback_count_
);
697 EXPECT_EQ(0, error_callback_count_
);
699 EXPECT_EQ(1, observer
.discoverable_changed_count_
);
701 EXPECT_FALSE(adapter_
->IsDiscoverable());
704 TEST_F(BluetoothChromeOSTest
, SetDiscoverableWhenNotPresent
) {
706 ASSERT_TRUE(adapter_
->IsPresent());
707 ASSERT_FALSE(adapter_
->IsDiscoverable());
709 // Install an observer; expect the AdapterDiscoverableChanged to be called
710 // with true, and IsDiscoverable() to return true.
711 TestObserver
observer(adapter_
);
713 fake_bluetooth_adapter_client_
->SetVisible(false);
715 EXPECT_EQ(1, observer
.present_changed_count_
);
716 EXPECT_FALSE(observer
.last_present_
);
718 EXPECT_FALSE(adapter_
->IsPresent());
719 EXPECT_FALSE(adapter_
->IsDiscoverable());
721 adapter_
->SetDiscoverable(true, GetCallback(), GetErrorCallback());
722 EXPECT_EQ(0, callback_count_
);
723 EXPECT_EQ(1, error_callback_count_
);
725 EXPECT_EQ(0, observer
.discoverable_changed_count_
);
727 EXPECT_FALSE(adapter_
->IsDiscoverable());
730 TEST_F(BluetoothChromeOSTest
, StopDiscovery
) {
733 adapter_
->SetPowered(true, GetCallback(), GetErrorCallback());
734 adapter_
->StartDiscoverySession(
735 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
736 base::Unretained(this)),
739 EXPECT_EQ(2, callback_count_
);
740 EXPECT_EQ(0, error_callback_count_
);
743 ASSERT_TRUE(adapter_
->IsPowered());
744 ASSERT_TRUE(adapter_
->IsDiscovering());
745 ASSERT_EQ((size_t)1, discovery_sessions_
.size());
746 ASSERT_TRUE(discovery_sessions_
[0]->IsActive());
748 // Install an observer; aside from the callback, expect the
749 // AdapterDiscoveringChanged method to be called and no longer to be
751 TestObserver
observer(adapter_
);
753 discovery_sessions_
[0]->Stop(GetCallback(), GetErrorCallback());
755 EXPECT_EQ(1, callback_count_
);
756 EXPECT_EQ(0, error_callback_count_
);
758 EXPECT_EQ(1, observer
.discovering_changed_count_
);
759 EXPECT_FALSE(observer
.last_discovering_
);
761 EXPECT_FALSE(adapter_
->IsDiscovering());
764 TEST_F(BluetoothChromeOSTest
, Discovery
) {
765 // Test a simulated discovery session.
766 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
769 TestObserver
observer(adapter_
);
771 adapter_
->SetPowered(true, GetCallback(), GetErrorCallback());
772 adapter_
->StartDiscoverySession(
773 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
774 base::Unretained(this)),
777 EXPECT_EQ(2, callback_count_
);
778 EXPECT_EQ(0, error_callback_count_
);
781 ASSERT_TRUE(adapter_
->IsPowered());
782 ASSERT_TRUE(adapter_
->IsDiscovering());
783 ASSERT_EQ((size_t)1, discovery_sessions_
.size());
784 ASSERT_TRUE(discovery_sessions_
[0]->IsActive());
786 // First two devices to appear.
789 EXPECT_EQ(2, observer
.device_added_count_
);
790 EXPECT_EQ(FakeBluetoothDeviceClient::kLowEnergyAddress
,
791 observer
.last_device_address_
);
793 // Next we should get another two devices...
795 EXPECT_EQ(4, observer
.device_added_count_
);
797 // Okay, let's run forward until a device is actually removed...
798 while (!observer
.device_removed_count_
)
801 EXPECT_EQ(1, observer
.device_removed_count_
);
802 EXPECT_EQ(FakeBluetoothDeviceClient::kVanishingDeviceAddress
,
803 observer
.last_device_address_
);
806 TEST_F(BluetoothChromeOSTest
, PoweredAndDiscovering
) {
808 adapter_
->SetPowered(true, GetCallback(), GetErrorCallback());
809 adapter_
->StartDiscoverySession(
810 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
811 base::Unretained(this)),
814 EXPECT_EQ(2, callback_count_
);
815 EXPECT_EQ(0, error_callback_count_
);
817 ASSERT_EQ((size_t)1, discovery_sessions_
.size());
818 ASSERT_TRUE(discovery_sessions_
[0]->IsActive());
820 // Stop the timers that the simulation uses
821 fake_bluetooth_device_client_
->EndDiscoverySimulation(
822 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
));
824 ASSERT_TRUE(adapter_
->IsPowered());
825 ASSERT_TRUE(adapter_
->IsDiscovering());
827 fake_bluetooth_adapter_client_
->SetVisible(false);
828 ASSERT_FALSE(adapter_
->IsPresent());
829 ASSERT_FALSE(discovery_sessions_
[0]->IsActive());
831 // Install an observer; expect the AdapterPresentChanged,
832 // AdapterPoweredChanged and AdapterDiscoveringChanged methods to be called
833 // with true, and IsPresent(), IsPowered() and IsDiscovering() to all
835 TestObserver
observer(adapter_
);
837 fake_bluetooth_adapter_client_
->SetVisible(true);
839 EXPECT_EQ(1, observer
.present_changed_count_
);
840 EXPECT_TRUE(observer
.last_present_
);
841 EXPECT_TRUE(adapter_
->IsPresent());
843 EXPECT_EQ(1, observer
.powered_changed_count_
);
844 EXPECT_TRUE(observer
.last_powered_
);
845 EXPECT_TRUE(adapter_
->IsPowered());
847 EXPECT_EQ(1, observer
.discovering_changed_count_
);
848 EXPECT_TRUE(observer
.last_discovering_
);
849 EXPECT_TRUE(adapter_
->IsDiscovering());
851 observer
.present_changed_count_
= 0;
852 observer
.powered_changed_count_
= 0;
853 observer
.discovering_changed_count_
= 0;
855 // Now mark the adapter not present again. Expect the methods to be called
856 // again, to reset the properties back to false
857 fake_bluetooth_adapter_client_
->SetVisible(false);
859 EXPECT_EQ(1, observer
.present_changed_count_
);
860 EXPECT_FALSE(observer
.last_present_
);
861 EXPECT_FALSE(adapter_
->IsPresent());
863 EXPECT_EQ(1, observer
.powered_changed_count_
);
864 EXPECT_FALSE(observer
.last_powered_
);
865 EXPECT_FALSE(adapter_
->IsPowered());
867 EXPECT_EQ(1, observer
.discovering_changed_count_
);
868 EXPECT_FALSE(observer
.last_discovering_
);
869 EXPECT_FALSE(adapter_
->IsDiscovering());
872 // This unit test asserts that the basic reference counting logic works
873 // correctly for discovery requests done via the BluetoothAdapter.
874 TEST_F(BluetoothChromeOSTest
, MultipleDiscoverySessions
) {
876 adapter_
->SetPowered(true, GetCallback(), GetErrorCallback());
877 EXPECT_EQ(1, callback_count_
);
878 EXPECT_EQ(0, error_callback_count_
);
879 EXPECT_TRUE(adapter_
->IsPowered());
882 TestObserver
observer(adapter_
);
884 EXPECT_EQ(0, observer
.discovering_changed_count_
);
885 EXPECT_FALSE(observer
.last_discovering_
);
886 EXPECT_FALSE(adapter_
->IsDiscovering());
888 // Request device discovery 3 times.
889 for (int i
= 0; i
< 3; i
++) {
890 adapter_
->StartDiscoverySession(
891 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
892 base::Unretained(this)),
895 // Run only once, as there should have been one D-Bus call.
898 // The observer should have received the discovering changed event exactly
899 // once, the success callback should have been called 3 times and the adapter
900 // should be discovering.
901 EXPECT_EQ(1, observer
.discovering_changed_count_
);
902 EXPECT_EQ(3, callback_count_
);
903 EXPECT_EQ(0, error_callback_count_
);
904 EXPECT_TRUE(observer
.last_discovering_
);
905 EXPECT_TRUE(adapter_
->IsDiscovering());
906 ASSERT_EQ((size_t)3, discovery_sessions_
.size());
908 // Request to stop discovery twice.
909 for (int i
= 0; i
< 2; i
++) {
910 discovery_sessions_
[i
]->Stop(GetCallback(), GetErrorCallback());
913 // The observer should have received no additional discovering changed events,
914 // the success callback should have been called 2 times and the adapter should
915 // still be discovering.
916 EXPECT_EQ(1, observer
.discovering_changed_count_
);
917 EXPECT_EQ(5, callback_count_
);
918 EXPECT_EQ(0, error_callback_count_
);
919 EXPECT_TRUE(observer
.last_discovering_
);
920 EXPECT_TRUE(adapter_
->IsDiscovering());
921 EXPECT_TRUE(adapter_
->IsDiscovering());
922 EXPECT_FALSE(discovery_sessions_
[0]->IsActive());
923 EXPECT_FALSE(discovery_sessions_
[1]->IsActive());
924 EXPECT_TRUE(discovery_sessions_
[2]->IsActive());
926 // Request device discovery 3 times.
927 for (int i
= 0; i
< 3; i
++) {
928 adapter_
->StartDiscoverySession(
929 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
930 base::Unretained(this)),
934 // The observer should have received no additional discovering changed events,
935 // the success callback should have been called 3 times and the adapter should
936 // still be discovering.
937 EXPECT_EQ(1, observer
.discovering_changed_count_
);
938 EXPECT_EQ(8, callback_count_
);
939 EXPECT_EQ(0, error_callback_count_
);
940 EXPECT_TRUE(observer
.last_discovering_
);
941 EXPECT_TRUE(adapter_
->IsDiscovering());
942 ASSERT_EQ((size_t)6, discovery_sessions_
.size());
944 // Request to stop discovery 4 times.
945 for (int i
= 2; i
< 6; i
++) {
946 discovery_sessions_
[i
]->Stop(GetCallback(), GetErrorCallback());
948 // Run only once, as there should have been one D-Bus call.
951 // The observer should have received the discovering changed event exactly
952 // once, the success callback should have been called 4 times and the adapter
953 // should no longer be discovering.
954 EXPECT_EQ(2, observer
.discovering_changed_count_
);
955 EXPECT_EQ(12, callback_count_
);
956 EXPECT_EQ(0, error_callback_count_
);
957 EXPECT_FALSE(observer
.last_discovering_
);
958 EXPECT_FALSE(adapter_
->IsDiscovering());
960 // All discovery sessions should be inactive.
961 for (int i
= 0; i
< 6; i
++)
962 EXPECT_FALSE(discovery_sessions_
[i
]->IsActive());
964 // Request to stop discovery on of the inactive sessions.
965 discovery_sessions_
[0]->Stop(GetCallback(), GetErrorCallback());
967 // The call should have failed.
968 EXPECT_EQ(2, observer
.discovering_changed_count_
);
969 EXPECT_EQ(12, callback_count_
);
970 EXPECT_EQ(1, error_callback_count_
);
971 EXPECT_FALSE(observer
.last_discovering_
);
972 EXPECT_FALSE(adapter_
->IsDiscovering());
975 // This unit test asserts that the reference counting logic works correctly in
976 // the cases when the adapter gets reset and D-Bus calls are made outside of
977 // the BluetoothAdapter.
978 TEST_F(BluetoothChromeOSTest
,
979 UnexpectedChangesDuringMultipleDiscoverySessions
) {
981 adapter_
->SetPowered(true, GetCallback(), GetErrorCallback());
982 EXPECT_EQ(1, callback_count_
);
983 EXPECT_EQ(0, error_callback_count_
);
984 EXPECT_TRUE(adapter_
->IsPowered());
987 TestObserver
observer(adapter_
);
989 EXPECT_EQ(0, observer
.discovering_changed_count_
);
990 EXPECT_FALSE(observer
.last_discovering_
);
991 EXPECT_FALSE(adapter_
->IsDiscovering());
993 // Request device discovery 3 times.
994 for (int i
= 0; i
< 3; i
++) {
995 adapter_
->StartDiscoverySession(
996 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
997 base::Unretained(this)),
1000 // Run only once, as there should have been one D-Bus call.
1001 message_loop_
.Run();
1003 // The observer should have received the discovering changed event exactly
1004 // once, the success callback should have been called 3 times and the adapter
1005 // should be discovering.
1006 EXPECT_EQ(1, observer
.discovering_changed_count_
);
1007 EXPECT_EQ(3, callback_count_
);
1008 EXPECT_EQ(0, error_callback_count_
);
1009 EXPECT_TRUE(observer
.last_discovering_
);
1010 EXPECT_TRUE(adapter_
->IsDiscovering());
1011 ASSERT_EQ((size_t)3, discovery_sessions_
.size());
1013 for (int i
= 0; i
< 3; i
++)
1014 EXPECT_TRUE(discovery_sessions_
[i
]->IsActive());
1016 // Stop the timers that the simulation uses
1017 fake_bluetooth_device_client_
->EndDiscoverySimulation(
1018 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
));
1020 ASSERT_TRUE(adapter_
->IsPowered());
1021 ASSERT_TRUE(adapter_
->IsDiscovering());
1023 // Stop device discovery behind the adapter. The adapter and the observer
1024 // should be notified of the change and the reference count should be reset.
1025 // Even though FakeBluetoothAdapterClient does its own reference counting and
1026 // we called 3 BluetoothAdapter::StartDiscoverySession 3 times, the
1027 // FakeBluetoothAdapterClient's count should be only 1 and a single call to
1028 // FakeBluetoothAdapterClient::StopDiscovery should work.
1029 fake_bluetooth_adapter_client_
->StopDiscovery(
1030 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
), GetCallback(),
1031 base::Bind(&BluetoothChromeOSTest::DBusErrorCallback
,
1032 base::Unretained(this)));
1033 message_loop_
.Run();
1034 EXPECT_EQ(2, observer
.discovering_changed_count_
);
1035 EXPECT_EQ(4, callback_count_
);
1036 EXPECT_EQ(0, error_callback_count_
);
1037 EXPECT_FALSE(observer
.last_discovering_
);
1038 EXPECT_FALSE(adapter_
->IsDiscovering());
1040 // All discovery session instances should have been updated.
1041 for (int i
= 0; i
< 3; i
++)
1042 EXPECT_FALSE(discovery_sessions_
[i
]->IsActive());
1043 discovery_sessions_
.clear();
1045 // It should be possible to successfully start discovery.
1046 for (int i
= 0; i
< 2; i
++) {
1047 adapter_
->StartDiscoverySession(
1048 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
1049 base::Unretained(this)),
1050 GetErrorCallback());
1052 // Run only once, as there should have been one D-Bus call.
1053 message_loop_
.Run();
1054 EXPECT_EQ(3, observer
.discovering_changed_count_
);
1055 EXPECT_EQ(6, callback_count_
);
1056 EXPECT_EQ(0, error_callback_count_
);
1057 EXPECT_TRUE(observer
.last_discovering_
);
1058 EXPECT_TRUE(adapter_
->IsDiscovering());
1059 ASSERT_EQ((size_t)2, discovery_sessions_
.size());
1061 for (int i
= 0; i
< 2; i
++)
1062 EXPECT_TRUE(discovery_sessions_
[i
]->IsActive());
1064 fake_bluetooth_device_client_
->EndDiscoverySimulation(
1065 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
));
1067 // Make the adapter disappear and appear. This will make it come back as
1068 // discovering. When this happens, the reference count should become and
1069 // remain 0 as no new request was made through the BluetoothAdapter.
1070 fake_bluetooth_adapter_client_
->SetVisible(false);
1071 ASSERT_FALSE(adapter_
->IsPresent());
1072 EXPECT_EQ(4, observer
.discovering_changed_count_
);
1073 EXPECT_EQ(6, callback_count_
);
1074 EXPECT_EQ(0, error_callback_count_
);
1075 EXPECT_FALSE(observer
.last_discovering_
);
1076 EXPECT_FALSE(adapter_
->IsDiscovering());
1078 for (int i
= 0; i
< 2; i
++)
1079 EXPECT_FALSE(discovery_sessions_
[i
]->IsActive());
1080 discovery_sessions_
.clear();
1082 fake_bluetooth_adapter_client_
->SetVisible(true);
1083 ASSERT_TRUE(adapter_
->IsPresent());
1084 EXPECT_EQ(5, observer
.discovering_changed_count_
);
1085 EXPECT_EQ(6, callback_count_
);
1086 EXPECT_EQ(0, error_callback_count_
);
1087 EXPECT_TRUE(observer
.last_discovering_
);
1088 EXPECT_TRUE(adapter_
->IsDiscovering());
1090 // Start and stop discovery. At this point, FakeBluetoothAdapterClient has
1091 // a reference count that is equal to 1. Pretend that this was done by an
1092 // application other than us. Starting and stopping discovery will succeed
1093 // but it won't cause the discovery state to change.
1094 adapter_
->StartDiscoverySession(
1095 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
1096 base::Unretained(this)),
1097 GetErrorCallback());
1098 message_loop_
.Run(); // Run the loop, as there should have been a D-Bus call.
1099 EXPECT_EQ(5, observer
.discovering_changed_count_
);
1100 EXPECT_EQ(7, callback_count_
);
1101 EXPECT_EQ(0, error_callback_count_
);
1102 EXPECT_TRUE(observer
.last_discovering_
);
1103 EXPECT_TRUE(adapter_
->IsDiscovering());
1104 ASSERT_EQ((size_t)1, discovery_sessions_
.size());
1105 EXPECT_TRUE(discovery_sessions_
[0]->IsActive());
1107 discovery_sessions_
[0]->Stop(GetCallback(), GetErrorCallback());
1108 message_loop_
.Run(); // Run the loop, as there should have been a D-Bus call.
1109 EXPECT_EQ(5, observer
.discovering_changed_count_
);
1110 EXPECT_EQ(8, callback_count_
);
1111 EXPECT_EQ(0, error_callback_count_
);
1112 EXPECT_TRUE(observer
.last_discovering_
);
1113 EXPECT_TRUE(adapter_
->IsDiscovering());
1114 EXPECT_FALSE(discovery_sessions_
[0]->IsActive());
1115 discovery_sessions_
.clear();
1117 // Start discovery again.
1118 adapter_
->StartDiscoverySession(
1119 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
1120 base::Unretained(this)),
1121 GetErrorCallback());
1122 message_loop_
.Run(); // Run the loop, as there should have been a D-Bus call.
1123 EXPECT_EQ(5, observer
.discovering_changed_count_
);
1124 EXPECT_EQ(9, callback_count_
);
1125 EXPECT_EQ(0, error_callback_count_
);
1126 EXPECT_TRUE(observer
.last_discovering_
);
1127 EXPECT_TRUE(adapter_
->IsDiscovering());
1128 ASSERT_EQ((size_t)1, discovery_sessions_
.size());
1129 EXPECT_TRUE(discovery_sessions_
[0]->IsActive());
1131 // Stop discovery via D-Bus. The fake client's reference count will drop but
1132 // the discovery state won't change since our BluetoothAdapter also just
1133 // requested it via D-Bus.
1134 fake_bluetooth_adapter_client_
->StopDiscovery(
1135 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
), GetCallback(),
1136 base::Bind(&BluetoothChromeOSTest::DBusErrorCallback
,
1137 base::Unretained(this)));
1138 message_loop_
.Run();
1139 EXPECT_EQ(5, observer
.discovering_changed_count_
);
1140 EXPECT_EQ(10, callback_count_
);
1141 EXPECT_EQ(0, error_callback_count_
);
1142 EXPECT_TRUE(observer
.last_discovering_
);
1143 EXPECT_TRUE(adapter_
->IsDiscovering());
1145 // Now end the discovery session. This should change the adapter's discovery
1147 discovery_sessions_
[0]->Stop(GetCallback(), GetErrorCallback());
1148 message_loop_
.Run();
1149 EXPECT_EQ(6, observer
.discovering_changed_count_
);
1150 EXPECT_EQ(11, callback_count_
);
1151 EXPECT_EQ(0, error_callback_count_
);
1152 EXPECT_FALSE(observer
.last_discovering_
);
1153 EXPECT_FALSE(adapter_
->IsDiscovering());
1154 EXPECT_FALSE(discovery_sessions_
[0]->IsActive());
1157 TEST_F(BluetoothChromeOSTest
, InvalidatedDiscoverySessions
) {
1159 adapter_
->SetPowered(true, GetCallback(), GetErrorCallback());
1160 EXPECT_EQ(1, callback_count_
);
1161 EXPECT_EQ(0, error_callback_count_
);
1162 EXPECT_TRUE(adapter_
->IsPowered());
1163 callback_count_
= 0;
1165 TestObserver
observer(adapter_
);
1167 EXPECT_EQ(0, observer
.discovering_changed_count_
);
1168 EXPECT_FALSE(observer
.last_discovering_
);
1169 EXPECT_FALSE(adapter_
->IsDiscovering());
1171 // Request device discovery 3 times.
1172 for (int i
= 0; i
< 3; i
++) {
1173 adapter_
->StartDiscoverySession(
1174 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
1175 base::Unretained(this)),
1176 GetErrorCallback());
1178 // Run only once, as there should have been one D-Bus call.
1179 message_loop_
.Run();
1181 // The observer should have received the discovering changed event exactly
1182 // once, the success callback should have been called 3 times and the adapter
1183 // should be discovering.
1184 EXPECT_EQ(1, observer
.discovering_changed_count_
);
1185 EXPECT_EQ(3, callback_count_
);
1186 EXPECT_EQ(0, error_callback_count_
);
1187 EXPECT_TRUE(observer
.last_discovering_
);
1188 EXPECT_TRUE(adapter_
->IsDiscovering());
1189 ASSERT_EQ((size_t)3, discovery_sessions_
.size());
1191 for (int i
= 0; i
< 3; i
++)
1192 EXPECT_TRUE(discovery_sessions_
[i
]->IsActive());
1194 // Stop the timers that the simulation uses
1195 fake_bluetooth_device_client_
->EndDiscoverySimulation(
1196 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
));
1198 ASSERT_TRUE(adapter_
->IsPowered());
1199 ASSERT_TRUE(adapter_
->IsDiscovering());
1201 // Delete all but one discovery session.
1202 discovery_sessions_
.pop_back();
1203 discovery_sessions_
.pop_back();
1204 ASSERT_EQ((size_t)1, discovery_sessions_
.size());
1205 EXPECT_TRUE(discovery_sessions_
[0]->IsActive());
1206 EXPECT_TRUE(adapter_
->IsDiscovering());
1208 // Stop device discovery behind the adapter. The one active discovery session
1209 // should become inactive, but more importantly, we shouldn't run into any
1210 // memory errors as the sessions that we explicitly deleted should get
1212 fake_bluetooth_adapter_client_
->StopDiscovery(
1213 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
), GetCallback(),
1214 base::Bind(&BluetoothChromeOSTest::DBusErrorCallback
,
1215 base::Unretained(this)));
1216 message_loop_
.Run();
1217 EXPECT_EQ(2, observer
.discovering_changed_count_
);
1218 EXPECT_EQ(4, callback_count_
);
1219 EXPECT_EQ(0, error_callback_count_
);
1220 EXPECT_FALSE(observer
.last_discovering_
);
1221 EXPECT_FALSE(adapter_
->IsDiscovering());
1222 EXPECT_FALSE(discovery_sessions_
[0]->IsActive());
1225 TEST_F(BluetoothChromeOSTest
, QueuedDiscoveryRequests
) {
1228 adapter_
->SetPowered(true, GetCallback(), GetErrorCallback());
1229 EXPECT_EQ(1, callback_count_
);
1230 EXPECT_EQ(0, error_callback_count_
);
1231 EXPECT_TRUE(adapter_
->IsPowered());
1232 callback_count_
= 0;
1234 TestObserver
observer(adapter_
);
1236 EXPECT_EQ(0, observer
.discovering_changed_count_
);
1237 EXPECT_FALSE(observer
.last_discovering_
);
1238 EXPECT_FALSE(adapter_
->IsDiscovering());
1240 // Request to start discovery. The call should be pending.
1241 adapter_
->StartDiscoverySession(
1242 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
1243 base::Unretained(this)),
1244 GetErrorCallback());
1245 EXPECT_EQ(0, callback_count_
);
1247 fake_bluetooth_device_client_
->EndDiscoverySimulation(
1248 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
));
1250 // The underlying adapter has started discovery, but our call hasn't returned
1252 EXPECT_EQ(1, observer
.discovering_changed_count_
);
1253 EXPECT_TRUE(observer
.last_discovering_
);
1254 EXPECT_TRUE(adapter_
->IsDiscovering());
1255 EXPECT_TRUE(discovery_sessions_
.empty());
1257 // Request to start discovery twice. These should get queued and there should
1258 // be no change in state.
1259 for (int i
= 0; i
< 2; i
++) {
1260 adapter_
->StartDiscoverySession(
1261 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
1262 base::Unretained(this)),
1263 GetErrorCallback());
1265 EXPECT_EQ(0, callback_count_
);
1266 EXPECT_EQ(0, error_callback_count_
);
1267 EXPECT_EQ(1, observer
.discovering_changed_count_
);
1268 EXPECT_TRUE(observer
.last_discovering_
);
1269 EXPECT_TRUE(adapter_
->IsDiscovering());
1270 EXPECT_TRUE(discovery_sessions_
.empty());
1272 // Process the pending call. The queued calls should execute and the discovery
1273 // session reference count should increase.
1274 message_loop_
.Run();
1275 EXPECT_EQ(3, callback_count_
);
1276 EXPECT_EQ(0, error_callback_count_
);
1277 EXPECT_EQ(1, observer
.discovering_changed_count_
);
1278 EXPECT_TRUE(observer
.last_discovering_
);
1279 EXPECT_TRUE(adapter_
->IsDiscovering());
1280 ASSERT_EQ((size_t)3, discovery_sessions_
.size());
1282 // Verify the reference count by removing sessions 3 times. The last request
1283 // should remain pending.
1284 for (int i
= 0; i
< 3; i
++) {
1285 discovery_sessions_
[i
]->Stop(GetCallback(), GetErrorCallback());
1287 EXPECT_EQ(5, callback_count_
);
1288 EXPECT_EQ(0, error_callback_count_
);
1289 EXPECT_EQ(2, observer
.discovering_changed_count_
);
1290 EXPECT_FALSE(observer
.last_discovering_
);
1291 EXPECT_FALSE(adapter_
->IsDiscovering());
1292 EXPECT_FALSE(discovery_sessions_
[0]->IsActive());
1293 EXPECT_FALSE(discovery_sessions_
[1]->IsActive());
1294 EXPECT_TRUE(discovery_sessions_
[2]->IsActive());
1296 // Request to stop the session whose call is pending should fail.
1297 discovery_sessions_
[2]->Stop(GetCallback(), GetErrorCallback());
1298 EXPECT_EQ(5, callback_count_
);
1299 EXPECT_EQ(1, error_callback_count_
);
1300 EXPECT_EQ(2, observer
.discovering_changed_count_
);
1301 EXPECT_FALSE(observer
.last_discovering_
);
1302 EXPECT_FALSE(adapter_
->IsDiscovering());
1303 EXPECT_TRUE(discovery_sessions_
[2]->IsActive());
1305 // Request to start should get queued.
1306 adapter_
->StartDiscoverySession(
1307 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
1308 base::Unretained(this)),
1309 GetErrorCallback());
1310 EXPECT_EQ(5, callback_count_
);
1311 EXPECT_EQ(1, error_callback_count_
);
1312 EXPECT_EQ(2, observer
.discovering_changed_count_
);
1313 EXPECT_FALSE(observer
.last_discovering_
);
1314 EXPECT_FALSE(adapter_
->IsDiscovering());
1315 ASSERT_EQ((size_t)3, discovery_sessions_
.size());
1317 // Run the pending request.
1318 message_loop_
.Run();
1319 EXPECT_EQ(6, callback_count_
);
1320 EXPECT_EQ(1, error_callback_count_
);
1321 EXPECT_EQ(3, observer
.discovering_changed_count_
);
1322 EXPECT_TRUE(observer
.last_discovering_
);
1323 EXPECT_TRUE(adapter_
->IsDiscovering());
1324 ASSERT_EQ((size_t)3, discovery_sessions_
.size());
1325 EXPECT_FALSE(discovery_sessions_
[2]->IsActive());
1327 // The queued request to start discovery should have been issued but is still
1328 // pending. Run the loop and verify.
1329 message_loop_
.Run();
1330 EXPECT_EQ(7, callback_count_
);
1331 EXPECT_EQ(1, error_callback_count_
);
1332 EXPECT_EQ(3, observer
.discovering_changed_count_
);
1333 EXPECT_TRUE(observer
.last_discovering_
);
1334 EXPECT_TRUE(adapter_
->IsDiscovering());
1335 ASSERT_EQ((size_t)4, discovery_sessions_
.size());
1336 EXPECT_TRUE(discovery_sessions_
[3]->IsActive());
1339 TEST_F(BluetoothChromeOSTest
, StartDiscoverySession
) {
1342 adapter_
->SetPowered(true, GetCallback(), GetErrorCallback());
1343 EXPECT_EQ(1, callback_count_
);
1344 EXPECT_EQ(0, error_callback_count_
);
1345 EXPECT_TRUE(adapter_
->IsPowered());
1346 callback_count_
= 0;
1348 TestObserver
observer(adapter_
);
1350 EXPECT_EQ(0, observer
.discovering_changed_count_
);
1351 EXPECT_FALSE(observer
.last_discovering_
);
1352 EXPECT_FALSE(adapter_
->IsDiscovering());
1353 EXPECT_TRUE(discovery_sessions_
.empty());
1355 // Request a new discovery session.
1356 adapter_
->StartDiscoverySession(
1357 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
1358 base::Unretained(this)),
1359 GetErrorCallback());
1360 message_loop_
.Run();
1361 EXPECT_EQ(1, observer
.discovering_changed_count_
);
1362 EXPECT_EQ(1, callback_count_
);
1363 EXPECT_EQ(0, error_callback_count_
);
1364 EXPECT_TRUE(observer
.last_discovering_
);
1365 EXPECT_TRUE(adapter_
->IsDiscovering());
1366 ASSERT_EQ((size_t)1, discovery_sessions_
.size());
1367 EXPECT_TRUE(discovery_sessions_
[0]->IsActive());
1369 // Start another session. A new one should be returned in the callback, which
1370 // in turn will destroy the previous session. Adapter should still be
1371 // discovering and the reference count should be 1.
1372 adapter_
->StartDiscoverySession(
1373 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
1374 base::Unretained(this)),
1375 GetErrorCallback());
1376 message_loop_
.Run();
1377 EXPECT_EQ(1, observer
.discovering_changed_count_
);
1378 EXPECT_EQ(2, callback_count_
);
1379 EXPECT_EQ(0, error_callback_count_
);
1380 EXPECT_TRUE(observer
.last_discovering_
);
1381 EXPECT_TRUE(adapter_
->IsDiscovering());
1382 ASSERT_EQ((size_t)2, discovery_sessions_
.size());
1383 EXPECT_TRUE(discovery_sessions_
[0]->IsActive());
1385 // Request a new session.
1386 adapter_
->StartDiscoverySession(
1387 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
1388 base::Unretained(this)),
1389 GetErrorCallback());
1390 message_loop_
.Run();
1391 EXPECT_EQ(1, observer
.discovering_changed_count_
);
1392 EXPECT_EQ(3, callback_count_
);
1393 EXPECT_EQ(0, error_callback_count_
);
1394 EXPECT_TRUE(observer
.last_discovering_
);
1395 EXPECT_TRUE(adapter_
->IsDiscovering());
1396 ASSERT_EQ((size_t)3, discovery_sessions_
.size());
1397 EXPECT_TRUE(discovery_sessions_
[1]->IsActive());
1398 EXPECT_NE(discovery_sessions_
[0], discovery_sessions_
[1]);
1400 // Stop the previous discovery session. The session should end but discovery
1402 discovery_sessions_
[0]->Stop(GetCallback(), GetErrorCallback());
1403 message_loop_
.Run();
1404 EXPECT_EQ(1, observer
.discovering_changed_count_
);
1405 EXPECT_EQ(4, callback_count_
);
1406 EXPECT_EQ(0, error_callback_count_
);
1407 EXPECT_TRUE(observer
.last_discovering_
);
1408 EXPECT_TRUE(adapter_
->IsDiscovering());
1409 ASSERT_EQ((size_t)3, discovery_sessions_
.size());
1410 EXPECT_FALSE(discovery_sessions_
[0]->IsActive());
1411 EXPECT_TRUE(discovery_sessions_
[1]->IsActive());
1413 // Delete the current active session. Discovery should eventually stop.
1414 discovery_sessions_
.clear();
1415 while (observer
.last_discovering_
)
1416 message_loop_
.RunUntilIdle();
1418 EXPECT_EQ(2, observer
.discovering_changed_count_
);
1419 EXPECT_EQ(4, callback_count_
);
1420 EXPECT_EQ(0, error_callback_count_
);
1421 EXPECT_FALSE(observer
.last_discovering_
);
1422 EXPECT_FALSE(adapter_
->IsDiscovering());
1425 TEST_F(BluetoothChromeOSTest
, DeviceProperties
) {
1428 BluetoothAdapter::DeviceList devices
= adapter_
->GetDevices();
1429 ASSERT_EQ(2U, devices
.size());
1430 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress
,
1431 devices
[0]->GetAddress());
1433 // Verify the other device properties.
1434 EXPECT_EQ(base::UTF8ToUTF16(FakeBluetoothDeviceClient::kPairedDeviceName
),
1435 devices
[0]->GetName());
1436 EXPECT_EQ(BluetoothDevice::DEVICE_COMPUTER
, devices
[0]->GetDeviceType());
1437 EXPECT_TRUE(devices
[0]->IsPaired());
1438 EXPECT_FALSE(devices
[0]->IsConnected());
1439 EXPECT_FALSE(devices
[0]->IsConnecting());
1441 // Non HID devices are always connectable.
1442 EXPECT_TRUE(devices
[0]->IsConnectable());
1444 BluetoothDevice::UUIDList uuids
= devices
[0]->GetUUIDs();
1445 ASSERT_EQ(2U, uuids
.size());
1446 EXPECT_EQ(uuids
[0], BluetoothUUID("1800"));
1447 EXPECT_EQ(uuids
[1], BluetoothUUID("1801"));
1449 EXPECT_EQ(BluetoothDevice::VENDOR_ID_USB
, devices
[0]->GetVendorIDSource());
1450 EXPECT_EQ(0x05ac, devices
[0]->GetVendorID());
1451 EXPECT_EQ(0x030d, devices
[0]->GetProductID());
1452 EXPECT_EQ(0x0306, devices
[0]->GetDeviceID());
1455 TEST_F(BluetoothChromeOSTest
, DeviceClassChanged
) {
1456 // Simulate a change of class of a device, as sometimes occurs
1457 // during discovery.
1460 BluetoothAdapter::DeviceList devices
= adapter_
->GetDevices();
1461 ASSERT_EQ(2U, devices
.size());
1462 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress
,
1463 devices
[0]->GetAddress());
1464 ASSERT_EQ(BluetoothDevice::DEVICE_COMPUTER
, devices
[0]->GetDeviceType());
1466 // Install an observer; expect the DeviceChanged method to be called when
1467 // we change the class of the device.
1468 TestObserver
observer(adapter_
);
1470 FakeBluetoothDeviceClient::Properties
* properties
=
1471 fake_bluetooth_device_client_
->GetProperties(
1472 dbus::ObjectPath(FakeBluetoothDeviceClient::kPairedDevicePath
));
1474 properties
->bluetooth_class
.ReplaceValue(0x002580);
1476 EXPECT_EQ(1, observer
.device_changed_count_
);
1477 EXPECT_EQ(devices
[0], observer
.last_device_
);
1479 EXPECT_EQ(BluetoothDevice::DEVICE_MOUSE
, devices
[0]->GetDeviceType());
1482 TEST_F(BluetoothChromeOSTest
, DeviceNameChanged
) {
1483 // Simulate a change of name of a device.
1486 BluetoothAdapter::DeviceList devices
= adapter_
->GetDevices();
1487 ASSERT_EQ(2U, devices
.size());
1488 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress
,
1489 devices
[0]->GetAddress());
1490 ASSERT_EQ(base::UTF8ToUTF16(FakeBluetoothDeviceClient::kPairedDeviceName
),
1491 devices
[0]->GetName());
1493 // Install an observer; expect the DeviceChanged method to be called when
1494 // we change the alias of the device.
1495 TestObserver
observer(adapter_
);
1497 FakeBluetoothDeviceClient::Properties
* properties
=
1498 fake_bluetooth_device_client_
->GetProperties(
1499 dbus::ObjectPath(FakeBluetoothDeviceClient::kPairedDevicePath
));
1501 static const std::string
new_name("New Device Name");
1502 properties
->alias
.ReplaceValue(new_name
);
1504 EXPECT_EQ(1, observer
.device_changed_count_
);
1505 EXPECT_EQ(devices
[0], observer
.last_device_
);
1507 EXPECT_EQ(base::UTF8ToUTF16(new_name
), devices
[0]->GetName());
1510 TEST_F(BluetoothChromeOSTest
, DeviceUuidsChanged
) {
1511 // Simulate a change of advertised services of a device.
1514 BluetoothAdapter::DeviceList devices
= adapter_
->GetDevices();
1515 ASSERT_EQ(2U, devices
.size());
1516 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress
,
1517 devices
[0]->GetAddress());
1519 BluetoothDevice::UUIDList uuids
= devices
[0]->GetUUIDs();
1520 ASSERT_EQ(2U, uuids
.size());
1521 ASSERT_EQ(uuids
[0], BluetoothUUID("1800"));
1522 ASSERT_EQ(uuids
[1], BluetoothUUID("1801"));
1524 // Install an observer; expect the DeviceChanged method to be called when
1525 // we change the class of the device.
1526 TestObserver
observer(adapter_
);
1528 FakeBluetoothDeviceClient::Properties
* properties
=
1529 fake_bluetooth_device_client_
->GetProperties(
1530 dbus::ObjectPath(FakeBluetoothDeviceClient::kPairedDevicePath
));
1532 std::vector
<std::string
> new_uuids
;
1533 new_uuids
.push_back(uuids
[0].canonical_value());
1534 new_uuids
.push_back(uuids
[1].canonical_value());
1535 new_uuids
.push_back("0000110c-0000-1000-8000-00805f9b34fb");
1536 new_uuids
.push_back("0000110e-0000-1000-8000-00805f9b34fb");
1537 new_uuids
.push_back("0000110a-0000-1000-8000-00805f9b34fb");
1539 properties
->uuids
.ReplaceValue(new_uuids
);
1541 EXPECT_EQ(1, observer
.device_changed_count_
);
1542 EXPECT_EQ(devices
[0], observer
.last_device_
);
1544 // Fetching the value should give the new one.
1545 uuids
= devices
[0]->GetUUIDs();
1546 ASSERT_EQ(5U, uuids
.size());
1547 EXPECT_EQ(uuids
[0], BluetoothUUID("1800"));
1548 EXPECT_EQ(uuids
[1], BluetoothUUID("1801"));
1549 EXPECT_EQ(uuids
[2], BluetoothUUID("110c"));
1550 EXPECT_EQ(uuids
[3], BluetoothUUID("110e"));
1551 EXPECT_EQ(uuids
[4], BluetoothUUID("110a"));
1554 TEST_F(BluetoothChromeOSTest
, ForgetDevice
) {
1557 BluetoothAdapter::DeviceList devices
= adapter_
->GetDevices();
1558 ASSERT_EQ(2U, devices
.size());
1559 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress
,
1560 devices
[0]->GetAddress());
1562 std::string address
= devices
[0]->GetAddress();
1564 // Install an observer; expect the DeviceRemoved method to be called
1565 // with the device we remove.
1566 TestObserver
observer(adapter_
);
1568 devices
[0]->Forget(GetErrorCallback());
1569 EXPECT_EQ(0, error_callback_count_
);
1571 EXPECT_EQ(1, observer
.device_removed_count_
);
1572 EXPECT_EQ(address
, observer
.last_device_address_
);
1574 // GetDevices shouldn't return the device either.
1575 devices
= adapter_
->GetDevices();
1576 ASSERT_EQ(1U, devices
.size());
1579 TEST_F(BluetoothChromeOSTest
, ForgetUnpairedDevice
) {
1583 BluetoothDevice
* device
= adapter_
->GetDevice(
1584 FakeBluetoothDeviceClient::kConnectUnpairableAddress
);
1585 ASSERT_TRUE(device
!= NULL
);
1586 ASSERT_FALSE(device
->IsPaired());
1588 // Connect the device so it becomes trusted and remembered.
1589 device
->Connect(NULL
, GetCallback(),
1590 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
1591 base::Unretained(this)));
1593 ASSERT_EQ(1, callback_count_
);
1594 ASSERT_EQ(0, error_callback_count_
);
1595 callback_count_
= 0;
1597 ASSERT_TRUE(device
->IsConnected());
1598 ASSERT_FALSE(device
->IsConnecting());
1600 // Make sure the trusted property has been set to true.
1601 FakeBluetoothDeviceClient::Properties
* properties
=
1602 fake_bluetooth_device_client_
->GetProperties(
1603 dbus::ObjectPath(FakeBluetoothDeviceClient::kConnectUnpairablePath
));
1604 ASSERT_TRUE(properties
->trusted
.value());
1606 // Install an observer; expect the DeviceRemoved method to be called
1607 // with the device we remove.
1608 TestObserver
observer(adapter_
);
1610 device
->Forget(GetErrorCallback());
1611 EXPECT_EQ(0, error_callback_count_
);
1613 EXPECT_EQ(1, observer
.device_removed_count_
);
1614 EXPECT_EQ(FakeBluetoothDeviceClient::kConnectUnpairableAddress
,
1615 observer
.last_device_address_
);
1617 // GetDevices shouldn't return the device either.
1618 device
= adapter_
->GetDevice(
1619 FakeBluetoothDeviceClient::kConnectUnpairableAddress
);
1620 EXPECT_FALSE(device
!= NULL
);
1623 TEST_F(BluetoothChromeOSTest
, ConnectPairedDevice
) {
1626 BluetoothDevice
* device
= adapter_
->GetDevice(
1627 FakeBluetoothDeviceClient::kPairedDeviceAddress
);
1628 ASSERT_TRUE(device
!= NULL
);
1629 ASSERT_TRUE(device
->IsPaired());
1631 TestObserver
observer(adapter_
);
1633 // Connect without a pairing delegate; since the device is already Paired
1634 // this should succeed and the device should become connected.
1635 device
->Connect(NULL
, GetCallback(),
1636 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
1637 base::Unretained(this)));
1639 EXPECT_EQ(1, callback_count_
);
1640 EXPECT_EQ(0, error_callback_count_
);
1642 // Two changes for connecting, one for connected and one for for trusted
1643 // after connecting.
1644 EXPECT_EQ(4, observer
.device_changed_count_
);
1645 EXPECT_EQ(device
, observer
.last_device_
);
1647 EXPECT_TRUE(device
->IsConnected());
1648 EXPECT_FALSE(device
->IsConnecting());
1651 TEST_F(BluetoothChromeOSTest
, ConnectUnpairableDevice
) {
1655 BluetoothDevice
* device
= adapter_
->GetDevice(
1656 FakeBluetoothDeviceClient::kConnectUnpairableAddress
);
1657 ASSERT_TRUE(device
!= NULL
);
1658 ASSERT_FALSE(device
->IsPaired());
1660 TestObserver
observer(adapter_
);
1662 // Connect without a pairing delegate; since the device does not require
1663 // pairing, this should succeed and the device should become connected.
1664 device
->Connect(NULL
, GetCallback(),
1665 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
1666 base::Unretained(this)));
1668 EXPECT_EQ(1, callback_count_
);
1669 EXPECT_EQ(0, error_callback_count_
);
1671 // Two changes for connecting, one for connected, one for for trusted after
1672 // connection, and one for the reconnect mode (IsConnectable).
1673 EXPECT_EQ(5, observer
.device_changed_count_
);
1674 EXPECT_EQ(device
, observer
.last_device_
);
1676 EXPECT_TRUE(device
->IsConnected());
1677 EXPECT_FALSE(device
->IsConnecting());
1679 // Make sure the trusted property has been set to true.
1680 FakeBluetoothDeviceClient::Properties
* properties
=
1681 fake_bluetooth_device_client_
->GetProperties(
1682 dbus::ObjectPath(FakeBluetoothDeviceClient::kConnectUnpairablePath
));
1683 EXPECT_TRUE(properties
->trusted
.value());
1685 // Verify is a HID device and is not connectable.
1686 BluetoothDevice::UUIDList uuids
= device
->GetUUIDs();
1687 ASSERT_EQ(1U, uuids
.size());
1688 EXPECT_EQ(uuids
[0], BluetoothUUID("1124"));
1689 EXPECT_FALSE(device
->IsConnectable());
1692 TEST_F(BluetoothChromeOSTest
, ConnectConnectedDevice
) {
1695 BluetoothDevice
* device
= adapter_
->GetDevice(
1696 FakeBluetoothDeviceClient::kPairedDeviceAddress
);
1697 ASSERT_TRUE(device
!= NULL
);
1698 ASSERT_TRUE(device
->IsPaired());
1700 device
->Connect(NULL
, GetCallback(),
1701 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
1702 base::Unretained(this)));
1704 ASSERT_EQ(1, callback_count_
);
1705 ASSERT_EQ(0, error_callback_count_
);
1706 callback_count_
= 0;
1708 ASSERT_TRUE(device
->IsConnected());
1710 // Connect again; since the device is already Connected, this shouldn't do
1711 // anything to initiate the connection.
1712 TestObserver
observer(adapter_
);
1714 device
->Connect(NULL
, GetCallback(),
1715 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
1716 base::Unretained(this)));
1718 EXPECT_EQ(1, callback_count_
);
1719 EXPECT_EQ(0, error_callback_count_
);
1721 // The observer will be called because Connecting will toggle true and false,
1722 // and the trusted property will be updated to true.
1723 EXPECT_EQ(3, observer
.device_changed_count_
);
1725 EXPECT_TRUE(device
->IsConnected());
1726 EXPECT_FALSE(device
->IsConnecting());
1729 TEST_F(BluetoothChromeOSTest
, ConnectDeviceFails
) {
1733 BluetoothDevice
* device
= adapter_
->GetDevice(
1734 FakeBluetoothDeviceClient::kLegacyAutopairAddress
);
1735 ASSERT_TRUE(device
!= NULL
);
1736 ASSERT_FALSE(device
->IsPaired());
1738 TestObserver
observer(adapter_
);
1740 // Connect without a pairing delegate; since the device requires pairing,
1741 // this should fail with an error.
1742 device
->Connect(NULL
, GetCallback(),
1743 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
1744 base::Unretained(this)));
1746 EXPECT_EQ(0, callback_count_
);
1747 EXPECT_EQ(1, error_callback_count_
);
1748 EXPECT_EQ(BluetoothDevice::ERROR_FAILED
, last_connect_error_
);
1750 EXPECT_EQ(2, observer
.device_changed_count_
);
1752 EXPECT_FALSE(device
->IsConnected());
1753 EXPECT_FALSE(device
->IsConnecting());
1756 TEST_F(BluetoothChromeOSTest
, DisconnectDevice
) {
1759 BluetoothDevice
* device
= adapter_
->GetDevice(
1760 FakeBluetoothDeviceClient::kPairedDeviceAddress
);
1761 ASSERT_TRUE(device
!= NULL
);
1762 ASSERT_TRUE(device
->IsPaired());
1764 device
->Connect(NULL
, GetCallback(),
1765 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
1766 base::Unretained(this)));
1768 ASSERT_EQ(1, callback_count_
);
1769 ASSERT_EQ(0, error_callback_count_
);
1770 callback_count_
= 0;
1772 ASSERT_TRUE(device
->IsConnected());
1773 ASSERT_FALSE(device
->IsConnecting());
1775 // Disconnect the device, we should see the observer method fire and the
1776 // device get dropped.
1777 TestObserver
observer(adapter_
);
1779 device
->Disconnect(GetCallback(), GetErrorCallback());
1781 EXPECT_EQ(1, callback_count_
);
1782 EXPECT_EQ(0, error_callback_count_
);
1784 EXPECT_EQ(1, observer
.device_changed_count_
);
1785 EXPECT_EQ(device
, observer
.last_device_
);
1787 EXPECT_FALSE(device
->IsConnected());
1790 TEST_F(BluetoothChromeOSTest
, DisconnectUnconnectedDevice
) {
1793 BluetoothDevice
* device
= adapter_
->GetDevice(
1794 FakeBluetoothDeviceClient::kPairedDeviceAddress
);
1795 ASSERT_TRUE(device
!= NULL
);
1796 ASSERT_TRUE(device
->IsPaired());
1797 ASSERT_FALSE(device
->IsConnected());
1799 // Disconnect the device, we should see the observer method fire and the
1800 // device get dropped.
1801 TestObserver
observer(adapter_
);
1803 device
->Disconnect(GetCallback(), GetErrorCallback());
1805 EXPECT_EQ(0, callback_count_
);
1806 EXPECT_EQ(1, error_callback_count_
);
1808 EXPECT_EQ(0, observer
.device_changed_count_
);
1810 EXPECT_FALSE(device
->IsConnected());
1813 TEST_F(BluetoothChromeOSTest
, PairLegacyAutopair
) {
1814 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
1819 // The Legacy Autopair device requires no PIN or Passkey to pair because
1820 // the daemon provides 0000 to the device for us.
1821 BluetoothDevice
* device
= adapter_
->GetDevice(
1822 FakeBluetoothDeviceClient::kLegacyAutopairAddress
);
1823 ASSERT_TRUE(device
!= NULL
);
1824 ASSERT_FALSE(device
->IsPaired());
1826 TestObserver
observer(adapter_
);
1828 TestPairingDelegate pairing_delegate
;
1829 device
->Connect(&pairing_delegate
, GetCallback(),
1830 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
1831 base::Unretained(this)));
1833 EXPECT_EQ(0, pairing_delegate
.call_count_
);
1834 EXPECT_TRUE(device
->IsConnecting());
1836 message_loop_
.Run();
1838 EXPECT_EQ(1, callback_count_
);
1839 EXPECT_EQ(0, error_callback_count_
);
1841 // Two changes for connecting, one change for connected, one for paired,
1842 // two for trusted (after pairing and connection), and one for the reconnect
1843 // mode (IsConnectable).
1844 EXPECT_EQ(7, observer
.device_changed_count_
);
1845 EXPECT_EQ(device
, observer
.last_device_
);
1847 EXPECT_TRUE(device
->IsConnected());
1848 EXPECT_FALSE(device
->IsConnecting());
1850 EXPECT_TRUE(device
->IsPaired());
1852 // Verify is a HID device and is connectable.
1853 BluetoothDevice::UUIDList uuids
= device
->GetUUIDs();
1854 ASSERT_EQ(1U, uuids
.size());
1855 EXPECT_EQ(uuids
[0], BluetoothUUID("1124"));
1856 EXPECT_TRUE(device
->IsConnectable());
1858 // Make sure the trusted property has been set to true.
1859 FakeBluetoothDeviceClient::Properties
* properties
=
1860 fake_bluetooth_device_client_
->GetProperties(
1861 dbus::ObjectPath(FakeBluetoothDeviceClient::kLegacyAutopairPath
));
1862 EXPECT_TRUE(properties
->trusted
.value());
1865 TEST_F(BluetoothChromeOSTest
, PairDisplayPinCode
) {
1866 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
1871 // Requires that we display a randomly generated PIN on the screen.
1872 BluetoothDevice
* device
= adapter_
->GetDevice(
1873 FakeBluetoothDeviceClient::kDisplayPinCodeAddress
);
1874 ASSERT_TRUE(device
!= NULL
);
1875 ASSERT_FALSE(device
->IsPaired());
1877 TestObserver
observer(adapter_
);
1879 TestPairingDelegate pairing_delegate
;
1880 device
->Connect(&pairing_delegate
, GetCallback(),
1881 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
1882 base::Unretained(this)));
1884 EXPECT_EQ(1, pairing_delegate
.call_count_
);
1885 EXPECT_EQ(1, pairing_delegate
.display_pincode_count_
);
1886 EXPECT_EQ("123456", pairing_delegate
.last_pincode_
);
1887 EXPECT_TRUE(device
->IsConnecting());
1889 message_loop_
.Run();
1891 EXPECT_EQ(1, callback_count_
);
1892 EXPECT_EQ(0, error_callback_count_
);
1894 // Two changes for connecting, one change for connected, one for paired,
1895 // two for trusted (after pairing and connection), and one for the reconnect
1896 // mode (IsConnectable).
1897 EXPECT_EQ(7, observer
.device_changed_count_
);
1898 EXPECT_EQ(device
, observer
.last_device_
);
1900 EXPECT_TRUE(device
->IsConnected());
1901 EXPECT_FALSE(device
->IsConnecting());
1903 EXPECT_TRUE(device
->IsPaired());
1905 // Verify is a HID device and is connectable.
1906 BluetoothDevice::UUIDList uuids
= device
->GetUUIDs();
1907 ASSERT_EQ(1U, uuids
.size());
1908 EXPECT_EQ(uuids
[0], BluetoothUUID("1124"));
1909 EXPECT_TRUE(device
->IsConnectable());
1911 // Make sure the trusted property has been set to true.
1912 FakeBluetoothDeviceClient::Properties
* properties
=
1913 fake_bluetooth_device_client_
->GetProperties(
1914 dbus::ObjectPath(FakeBluetoothDeviceClient::kDisplayPinCodePath
));
1915 EXPECT_TRUE(properties
->trusted
.value());
1918 TEST_F(BluetoothChromeOSTest
, PairDisplayPasskey
) {
1919 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
1924 // Requires that we display a randomly generated Passkey on the screen,
1925 // and notifies us as it's typed in.
1926 BluetoothDevice
* device
= adapter_
->GetDevice(
1927 FakeBluetoothDeviceClient::kDisplayPasskeyAddress
);
1928 ASSERT_TRUE(device
!= NULL
);
1929 ASSERT_FALSE(device
->IsPaired());
1931 TestObserver
observer(adapter_
);
1933 TestPairingDelegate pairing_delegate
;
1934 device
->Connect(&pairing_delegate
, GetCallback(),
1935 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
1936 base::Unretained(this)));
1938 // One call for DisplayPasskey() and one for KeysEntered().
1939 EXPECT_EQ(2, pairing_delegate
.call_count_
);
1940 EXPECT_EQ(1, pairing_delegate
.display_passkey_count_
);
1941 EXPECT_EQ(123456U, pairing_delegate
.last_passkey_
);
1942 EXPECT_EQ(1, pairing_delegate
.keys_entered_count_
);
1943 EXPECT_EQ(0U, pairing_delegate
.last_entered_
);
1945 EXPECT_TRUE(device
->IsConnecting());
1947 // One call to KeysEntered() for each key, including [enter].
1948 for(int i
= 1; i
<= 7; ++i
) {
1949 message_loop_
.Run();
1951 EXPECT_EQ(2 + i
, pairing_delegate
.call_count_
);
1952 EXPECT_EQ(1 + i
, pairing_delegate
.keys_entered_count_
);
1953 EXPECT_EQ(static_cast<uint32_t>(i
), pairing_delegate
.last_entered_
);
1956 message_loop_
.Run();
1958 // 8 KeysEntered notifications (0 to 7, inclusive) and one aditional call for
1959 // DisplayPasskey().
1960 EXPECT_EQ(9, pairing_delegate
.call_count_
);
1961 EXPECT_EQ(8, pairing_delegate
.keys_entered_count_
);
1962 EXPECT_EQ(7U, pairing_delegate
.last_entered_
);
1964 EXPECT_EQ(1, callback_count_
);
1965 EXPECT_EQ(0, error_callback_count_
);
1967 // Two changes for connecting, one change for connected, one for paired,
1968 // two for trusted (after pairing and connection), and one for the reconnect
1969 // mode (IsConnectable).
1970 EXPECT_EQ(7, observer
.device_changed_count_
);
1971 EXPECT_EQ(device
, observer
.last_device_
);
1973 EXPECT_TRUE(device
->IsConnected());
1974 EXPECT_FALSE(device
->IsConnecting());
1976 EXPECT_TRUE(device
->IsPaired());
1978 // Verify is a HID device.
1979 BluetoothDevice::UUIDList uuids
= device
->GetUUIDs();
1980 ASSERT_EQ(1U, uuids
.size());
1981 EXPECT_EQ(uuids
[0], BluetoothUUID("1124"));
1983 // And usually not connectable.
1984 EXPECT_FALSE(device
->IsConnectable());
1986 // Make sure the trusted property has been set to true.
1987 FakeBluetoothDeviceClient::Properties
* properties
=
1988 fake_bluetooth_device_client_
->GetProperties(
1989 dbus::ObjectPath(FakeBluetoothDeviceClient::kDisplayPasskeyPath
));
1990 EXPECT_TRUE(properties
->trusted
.value());
1993 TEST_F(BluetoothChromeOSTest
, PairRequestPinCode
) {
1994 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
1999 // Requires that the user enters a PIN for them.
2000 BluetoothDevice
* device
= adapter_
->GetDevice(
2001 FakeBluetoothDeviceClient::kRequestPinCodeAddress
);
2002 ASSERT_TRUE(device
!= NULL
);
2003 ASSERT_FALSE(device
->IsPaired());
2005 TestObserver
observer(adapter_
);
2007 TestPairingDelegate pairing_delegate
;
2008 device
->Connect(&pairing_delegate
, GetCallback(),
2009 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
2010 base::Unretained(this)));
2012 EXPECT_EQ(1, pairing_delegate
.call_count_
);
2013 EXPECT_EQ(1, pairing_delegate
.request_pincode_count_
);
2014 EXPECT_TRUE(device
->IsConnecting());
2017 device
->SetPinCode("1234");
2018 message_loop_
.Run();
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 and
2024 // two for trusted (after pairing and connection).
2025 EXPECT_EQ(6, observer
.device_changed_count_
);
2026 EXPECT_EQ(device
, observer
.last_device_
);
2028 EXPECT_TRUE(device
->IsConnected());
2029 EXPECT_FALSE(device
->IsConnecting());
2031 EXPECT_TRUE(device
->IsPaired());
2033 // Verify is not a HID device.
2034 BluetoothDevice::UUIDList uuids
= device
->GetUUIDs();
2035 ASSERT_EQ(0U, uuids
.size());
2037 // Non HID devices are always connectable.
2038 EXPECT_TRUE(device
->IsConnectable());
2040 // Make sure the trusted property has been set to true.
2041 FakeBluetoothDeviceClient::Properties
* properties
=
2042 fake_bluetooth_device_client_
->GetProperties(
2043 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath
));
2044 EXPECT_TRUE(properties
->trusted
.value());
2047 TEST_F(BluetoothChromeOSTest
, PairConfirmPasskey
) {
2048 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
2053 // Requests that we confirm a displayed passkey.
2054 BluetoothDevice
* device
= adapter_
->GetDevice(
2055 FakeBluetoothDeviceClient::kConfirmPasskeyAddress
);
2056 ASSERT_TRUE(device
!= NULL
);
2057 ASSERT_FALSE(device
->IsPaired());
2059 TestObserver
observer(adapter_
);
2061 TestPairingDelegate pairing_delegate
;
2062 device
->Connect(&pairing_delegate
, GetCallback(),
2063 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
2064 base::Unretained(this)));
2066 EXPECT_EQ(1, pairing_delegate
.call_count_
);
2067 EXPECT_EQ(1, pairing_delegate
.confirm_passkey_count_
);
2068 EXPECT_EQ(123456U, pairing_delegate
.last_passkey_
);
2069 EXPECT_TRUE(device
->IsConnecting());
2071 // Confirm the passkey.
2072 device
->ConfirmPairing();
2073 message_loop_
.Run();
2075 EXPECT_EQ(1, callback_count_
);
2076 EXPECT_EQ(0, error_callback_count_
);
2078 // Two changes for connecting, one change for connected, one for paired and
2079 // two for trusted (after pairing and connection).
2080 EXPECT_EQ(6, observer
.device_changed_count_
);
2081 EXPECT_EQ(device
, observer
.last_device_
);
2083 EXPECT_TRUE(device
->IsConnected());
2084 EXPECT_FALSE(device
->IsConnecting());
2086 EXPECT_TRUE(device
->IsPaired());
2088 // Non HID devices are always connectable.
2089 EXPECT_TRUE(device
->IsConnectable());
2091 // Make sure the trusted property has been set to true.
2092 FakeBluetoothDeviceClient::Properties
* properties
=
2093 fake_bluetooth_device_client_
->GetProperties(
2094 dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath
));
2095 EXPECT_TRUE(properties
->trusted
.value());
2098 TEST_F(BluetoothChromeOSTest
, PairRequestPasskey
) {
2099 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
2104 // Requires that the user enters a Passkey, this would be some kind of
2105 // device that has a display, but doesn't use "just works" - maybe a car?
2106 BluetoothDevice
* device
= adapter_
->GetDevice(
2107 FakeBluetoothDeviceClient::kRequestPasskeyAddress
);
2108 ASSERT_TRUE(device
!= NULL
);
2109 ASSERT_FALSE(device
->IsPaired());
2111 TestObserver
observer(adapter_
);
2113 TestPairingDelegate pairing_delegate
;
2114 device
->Connect(&pairing_delegate
, GetCallback(),
2115 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
2116 base::Unretained(this)));
2118 EXPECT_EQ(1, pairing_delegate
.call_count_
);
2119 EXPECT_EQ(1, pairing_delegate
.request_passkey_count_
);
2120 EXPECT_TRUE(device
->IsConnecting());
2123 device
->SetPasskey(1234);
2124 message_loop_
.Run();
2126 EXPECT_EQ(1, callback_count_
);
2127 EXPECT_EQ(0, error_callback_count_
);
2129 // Two changes for connecting, one change for connected, one for paired and
2130 // two for trusted (after pairing and connection).
2131 EXPECT_EQ(6, observer
.device_changed_count_
);
2132 EXPECT_EQ(device
, observer
.last_device_
);
2134 EXPECT_TRUE(device
->IsConnected());
2135 EXPECT_FALSE(device
->IsConnecting());
2137 EXPECT_TRUE(device
->IsPaired());
2139 // Non HID devices are always connectable.
2140 EXPECT_TRUE(device
->IsConnectable());
2142 // Make sure the trusted property has been set to true.
2143 FakeBluetoothDeviceClient::Properties
* properties
=
2144 fake_bluetooth_device_client_
->GetProperties(
2145 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath
));
2146 EXPECT_TRUE(properties
->trusted
.value());
2149 TEST_F(BluetoothChromeOSTest
, PairJustWorks
) {
2150 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
2155 // Uses just-works pairing, since this is an outgoing pairing, no delegate
2156 // interaction is required.
2157 BluetoothDevice
* device
= adapter_
->GetDevice(
2158 FakeBluetoothDeviceClient::kJustWorksAddress
);
2159 ASSERT_TRUE(device
!= NULL
);
2160 ASSERT_FALSE(device
->IsPaired());
2162 TestObserver
observer(adapter_
);
2164 TestPairingDelegate pairing_delegate
;
2165 device
->Connect(&pairing_delegate
, GetCallback(),
2166 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
2167 base::Unretained(this)));
2169 EXPECT_EQ(0, pairing_delegate
.call_count_
);
2171 message_loop_
.Run();
2173 EXPECT_EQ(1, callback_count_
);
2174 EXPECT_EQ(0, error_callback_count_
);
2176 // Two changes for connecting, one change for connected, one for paired and
2177 // two for trusted (after pairing and connection).
2178 EXPECT_EQ(6, observer
.device_changed_count_
);
2179 EXPECT_EQ(device
, observer
.last_device_
);
2181 EXPECT_TRUE(device
->IsConnected());
2182 EXPECT_FALSE(device
->IsConnecting());
2184 EXPECT_TRUE(device
->IsPaired());
2186 // Non HID devices are always connectable.
2187 EXPECT_TRUE(device
->IsConnectable());
2189 // Make sure the trusted property has been set to true.
2190 FakeBluetoothDeviceClient::Properties
* properties
=
2191 fake_bluetooth_device_client_
->GetProperties(
2192 dbus::ObjectPath(FakeBluetoothDeviceClient::kJustWorksPath
));
2193 EXPECT_TRUE(properties
->trusted
.value());
2196 TEST_F(BluetoothChromeOSTest
, PairUnpairableDeviceFails
) {
2197 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
2200 DiscoverDevice(FakeBluetoothDeviceClient::kUnconnectableDeviceAddress
);
2202 BluetoothDevice
* device
= adapter_
->GetDevice(
2203 FakeBluetoothDeviceClient::kUnpairableDeviceAddress
);
2204 ASSERT_TRUE(device
!= NULL
);
2205 ASSERT_FALSE(device
->IsPaired());
2207 TestObserver
observer(adapter_
);
2209 TestPairingDelegate pairing_delegate
;
2210 device
->Connect(&pairing_delegate
, GetCallback(),
2211 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
2212 base::Unretained(this)));
2214 EXPECT_EQ(0, pairing_delegate
.call_count_
);
2215 EXPECT_TRUE(device
->IsConnecting());
2217 // Run the loop to get the error..
2218 message_loop_
.Run();
2220 EXPECT_EQ(0, callback_count_
);
2221 EXPECT_EQ(1, error_callback_count_
);
2223 EXPECT_EQ(BluetoothDevice::ERROR_FAILED
, last_connect_error_
);
2225 EXPECT_FALSE(device
->IsConnected());
2226 EXPECT_FALSE(device
->IsConnecting());
2227 EXPECT_FALSE(device
->IsPaired());
2230 TEST_F(BluetoothChromeOSTest
, PairingFails
) {
2231 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
2234 DiscoverDevice(FakeBluetoothDeviceClient::kVanishingDeviceAddress
);
2236 // The vanishing device times out during pairing
2237 BluetoothDevice
* device
= adapter_
->GetDevice(
2238 FakeBluetoothDeviceClient::kVanishingDeviceAddress
);
2239 ASSERT_TRUE(device
!= NULL
);
2240 ASSERT_FALSE(device
->IsPaired());
2242 TestObserver
observer(adapter_
);
2244 TestPairingDelegate pairing_delegate
;
2245 device
->Connect(&pairing_delegate
, GetCallback(),
2246 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
2247 base::Unretained(this)));
2249 EXPECT_EQ(0, pairing_delegate
.call_count_
);
2250 EXPECT_TRUE(device
->IsConnecting());
2252 // Run the loop to get the error..
2253 message_loop_
.Run();
2255 EXPECT_EQ(0, callback_count_
);
2256 EXPECT_EQ(1, error_callback_count_
);
2258 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_TIMEOUT
, last_connect_error_
);
2260 EXPECT_FALSE(device
->IsConnected());
2261 EXPECT_FALSE(device
->IsConnecting());
2262 EXPECT_FALSE(device
->IsPaired());
2265 TEST_F(BluetoothChromeOSTest
, PairingFailsAtConnection
) {
2266 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
2271 // Everything seems to go according to plan with the unconnectable device;
2272 // it pairs, but then you can't make connections to it after.
2273 BluetoothDevice
* device
= adapter_
->GetDevice(
2274 FakeBluetoothDeviceClient::kUnconnectableDeviceAddress
);
2275 ASSERT_TRUE(device
!= NULL
);
2276 ASSERT_FALSE(device
->IsPaired());
2278 TestObserver
observer(adapter_
);
2280 TestPairingDelegate pairing_delegate
;
2281 device
->Connect(&pairing_delegate
, GetCallback(),
2282 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
2283 base::Unretained(this)));
2285 EXPECT_EQ(0, pairing_delegate
.call_count_
);
2286 EXPECT_TRUE(device
->IsConnecting());
2288 message_loop_
.Run();
2290 EXPECT_EQ(0, callback_count_
);
2291 EXPECT_EQ(1, error_callback_count_
);
2292 EXPECT_EQ(BluetoothDevice::ERROR_FAILED
, last_connect_error_
);
2294 // Two changes for connecting, one for paired and one for trusted after
2295 // pairing. The device should not be connected.
2296 EXPECT_EQ(4, observer
.device_changed_count_
);
2297 EXPECT_EQ(device
, observer
.last_device_
);
2299 EXPECT_FALSE(device
->IsConnected());
2300 EXPECT_FALSE(device
->IsConnecting());
2302 EXPECT_TRUE(device
->IsPaired());
2304 // Make sure the trusted property has been set to true still (since pairing
2306 FakeBluetoothDeviceClient::Properties
* properties
=
2307 fake_bluetooth_device_client_
->GetProperties(
2309 FakeBluetoothDeviceClient::kUnconnectableDevicePath
));
2310 EXPECT_TRUE(properties
->trusted
.value());
2313 TEST_F(BluetoothChromeOSTest
, PairingRejectedAtPinCode
) {
2314 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
2319 // Reject the pairing after we receive a request for the PIN code.
2320 BluetoothDevice
* device
= adapter_
->GetDevice(
2321 FakeBluetoothDeviceClient::kRequestPinCodeAddress
);
2322 ASSERT_TRUE(device
!= NULL
);
2323 ASSERT_FALSE(device
->IsPaired());
2325 TestObserver
observer(adapter_
);
2327 TestPairingDelegate pairing_delegate
;
2328 device
->Connect(&pairing_delegate
, GetCallback(),
2329 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
2330 base::Unretained(this)));
2332 EXPECT_EQ(1, pairing_delegate
.call_count_
);
2333 EXPECT_EQ(1, pairing_delegate
.request_pincode_count_
);
2334 EXPECT_TRUE(device
->IsConnecting());
2336 // Reject the pairing.
2337 device
->RejectPairing();
2338 message_loop_
.Run();
2340 EXPECT_EQ(0, callback_count_
);
2341 EXPECT_EQ(1, error_callback_count_
);
2342 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_REJECTED
, last_connect_error_
);
2344 // Should be no changes except connecting going true and false.
2345 EXPECT_EQ(2, observer
.device_changed_count_
);
2346 EXPECT_FALSE(device
->IsConnected());
2347 EXPECT_FALSE(device
->IsConnecting());
2348 EXPECT_FALSE(device
->IsPaired());
2351 TEST_F(BluetoothChromeOSTest
, PairingCancelledAtPinCode
) {
2352 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
2357 // Cancel the pairing after we receive a request for the PIN code.
2358 BluetoothDevice
* device
= adapter_
->GetDevice(
2359 FakeBluetoothDeviceClient::kRequestPinCodeAddress
);
2360 ASSERT_TRUE(device
!= NULL
);
2361 ASSERT_FALSE(device
->IsPaired());
2363 TestObserver
observer(adapter_
);
2365 TestPairingDelegate pairing_delegate
;
2366 device
->Connect(&pairing_delegate
, GetCallback(),
2367 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
2368 base::Unretained(this)));
2370 EXPECT_EQ(1, pairing_delegate
.call_count_
);
2371 EXPECT_EQ(1, pairing_delegate
.request_pincode_count_
);
2372 EXPECT_TRUE(device
->IsConnecting());
2374 // Cancel the pairing.
2375 device
->CancelPairing();
2376 message_loop_
.Run();
2378 EXPECT_EQ(0, callback_count_
);
2379 EXPECT_EQ(1, error_callback_count_
);
2380 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED
, last_connect_error_
);
2382 // Should be no changes except connecting going true and false.
2383 EXPECT_EQ(2, observer
.device_changed_count_
);
2384 EXPECT_FALSE(device
->IsConnected());
2385 EXPECT_FALSE(device
->IsConnecting());
2386 EXPECT_FALSE(device
->IsPaired());
2389 TEST_F(BluetoothChromeOSTest
, PairingRejectedAtPasskey
) {
2390 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
2395 // Reject the pairing after we receive a request for the passkey.
2396 BluetoothDevice
* device
= adapter_
->GetDevice(
2397 FakeBluetoothDeviceClient::kRequestPasskeyAddress
);
2398 ASSERT_TRUE(device
!= NULL
);
2399 ASSERT_FALSE(device
->IsPaired());
2401 TestObserver
observer(adapter_
);
2403 TestPairingDelegate pairing_delegate
;
2404 device
->Connect(&pairing_delegate
, GetCallback(),
2405 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
2406 base::Unretained(this)));
2408 EXPECT_EQ(1, pairing_delegate
.call_count_
);
2409 EXPECT_EQ(1, pairing_delegate
.request_passkey_count_
);
2410 EXPECT_TRUE(device
->IsConnecting());
2412 // Reject the pairing.
2413 device
->RejectPairing();
2414 message_loop_
.Run();
2416 EXPECT_EQ(0, callback_count_
);
2417 EXPECT_EQ(1, error_callback_count_
);
2418 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_REJECTED
, last_connect_error_
);
2420 // Should be no changes except connecting going true and false.
2421 EXPECT_EQ(2, observer
.device_changed_count_
);
2422 EXPECT_FALSE(device
->IsConnected());
2423 EXPECT_FALSE(device
->IsConnecting());
2424 EXPECT_FALSE(device
->IsPaired());
2427 TEST_F(BluetoothChromeOSTest
, PairingCancelledAtPasskey
) {
2428 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
2433 // Cancel the pairing after we receive a request for the passkey.
2434 BluetoothDevice
* device
= adapter_
->GetDevice(
2435 FakeBluetoothDeviceClient::kRequestPasskeyAddress
);
2436 ASSERT_TRUE(device
!= NULL
);
2437 ASSERT_FALSE(device
->IsPaired());
2439 TestObserver
observer(adapter_
);
2441 TestPairingDelegate pairing_delegate
;
2442 device
->Connect(&pairing_delegate
, GetCallback(),
2443 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
2444 base::Unretained(this)));
2446 EXPECT_EQ(1, pairing_delegate
.call_count_
);
2447 EXPECT_EQ(1, pairing_delegate
.request_passkey_count_
);
2448 EXPECT_TRUE(device
->IsConnecting());
2450 // Cancel the pairing.
2451 device
->CancelPairing();
2452 message_loop_
.Run();
2454 EXPECT_EQ(0, callback_count_
);
2455 EXPECT_EQ(1, error_callback_count_
);
2456 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED
, last_connect_error_
);
2458 // Should be no changes except connecting going true and false.
2459 EXPECT_EQ(2, observer
.device_changed_count_
);
2460 EXPECT_FALSE(device
->IsConnected());
2461 EXPECT_FALSE(device
->IsConnecting());
2462 EXPECT_FALSE(device
->IsPaired());
2465 TEST_F(BluetoothChromeOSTest
, PairingRejectedAtConfirmation
) {
2466 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
2471 // Reject the pairing after we receive a request for passkey confirmation.
2472 BluetoothDevice
* device
= adapter_
->GetDevice(
2473 FakeBluetoothDeviceClient::kConfirmPasskeyAddress
);
2474 ASSERT_TRUE(device
!= NULL
);
2475 ASSERT_FALSE(device
->IsPaired());
2477 TestObserver
observer(adapter_
);
2479 TestPairingDelegate pairing_delegate
;
2480 device
->Connect(&pairing_delegate
, GetCallback(),
2481 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
2482 base::Unretained(this)));
2484 EXPECT_EQ(1, pairing_delegate
.call_count_
);
2485 EXPECT_EQ(1, pairing_delegate
.confirm_passkey_count_
);
2486 EXPECT_TRUE(device
->IsConnecting());
2488 // Reject the pairing.
2489 device
->RejectPairing();
2490 message_loop_
.Run();
2492 EXPECT_EQ(0, callback_count_
);
2493 EXPECT_EQ(1, error_callback_count_
);
2494 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_REJECTED
, last_connect_error_
);
2496 // Should be no changes except connecting going true and false.
2497 EXPECT_EQ(2, observer
.device_changed_count_
);
2498 EXPECT_FALSE(device
->IsConnected());
2499 EXPECT_FALSE(device
->IsConnecting());
2500 EXPECT_FALSE(device
->IsPaired());
2503 TEST_F(BluetoothChromeOSTest
, PairingCancelledAtConfirmation
) {
2504 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
2509 // Cancel the pairing after we receive a request for the passkey.
2510 BluetoothDevice
* device
= adapter_
->GetDevice(
2511 FakeBluetoothDeviceClient::kConfirmPasskeyAddress
);
2512 ASSERT_TRUE(device
!= NULL
);
2513 ASSERT_FALSE(device
->IsPaired());
2515 TestObserver
observer(adapter_
);
2517 TestPairingDelegate pairing_delegate
;
2518 device
->Connect(&pairing_delegate
, GetCallback(),
2519 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
2520 base::Unretained(this)));
2522 EXPECT_EQ(1, pairing_delegate
.call_count_
);
2523 EXPECT_EQ(1, pairing_delegate
.confirm_passkey_count_
);
2524 EXPECT_TRUE(device
->IsConnecting());
2526 // Cancel the pairing.
2527 device
->CancelPairing();
2528 message_loop_
.Run();
2530 EXPECT_EQ(0, callback_count_
);
2531 EXPECT_EQ(1, error_callback_count_
);
2532 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED
, last_connect_error_
);
2534 // Should be no changes except connecting going true and false.
2535 EXPECT_EQ(2, observer
.device_changed_count_
);
2536 EXPECT_FALSE(device
->IsConnected());
2537 EXPECT_FALSE(device
->IsConnecting());
2538 EXPECT_FALSE(device
->IsPaired());
2541 TEST_F(BluetoothChromeOSTest
, PairingCancelledInFlight
) {
2542 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
2547 // Cancel the pairing while we're waiting for the remote host.
2548 BluetoothDevice
* device
= adapter_
->GetDevice(
2549 FakeBluetoothDeviceClient::kLegacyAutopairAddress
);
2550 ASSERT_TRUE(device
!= NULL
);
2551 ASSERT_FALSE(device
->IsPaired());
2553 TestObserver
observer(adapter_
);
2555 TestPairingDelegate pairing_delegate
;
2556 device
->Connect(&pairing_delegate
, GetCallback(),
2557 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
2558 base::Unretained(this)));
2560 EXPECT_EQ(0, pairing_delegate
.call_count_
);
2561 EXPECT_TRUE(device
->IsConnecting());
2563 // Cancel the pairing.
2564 device
->CancelPairing();
2565 message_loop_
.Run();
2567 EXPECT_EQ(0, callback_count_
);
2568 EXPECT_EQ(1, error_callback_count_
);
2569 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED
, last_connect_error_
);
2571 // Should be no changes except connecting going true and false.
2572 EXPECT_EQ(2, observer
.device_changed_count_
);
2573 EXPECT_FALSE(device
->IsConnected());
2574 EXPECT_FALSE(device
->IsConnecting());
2575 EXPECT_FALSE(device
->IsPaired());
2578 TEST_F(BluetoothChromeOSTest
, IncomingPairRequestPinCode
) {
2579 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
2583 TestPairingDelegate pairing_delegate
;
2584 adapter_
->AddPairingDelegate(
2586 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH
);
2588 // Requires that we provide a PIN code.
2589 fake_bluetooth_device_client_
->CreateDevice(
2590 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
),
2591 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath
));
2592 BluetoothDevice
* device
= adapter_
->GetDevice(
2593 FakeBluetoothDeviceClient::kRequestPinCodeAddress
);
2594 ASSERT_TRUE(device
!= NULL
);
2595 ASSERT_FALSE(device
->IsPaired());
2597 TestObserver
observer(adapter_
);
2599 fake_bluetooth_device_client_
->SimulatePairing(
2600 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath
), true,
2601 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback
,
2602 base::Unretained(this)));
2604 EXPECT_EQ(1, pairing_delegate
.call_count_
);
2605 EXPECT_EQ(1, pairing_delegate
.request_pincode_count_
);
2608 device
->SetPinCode("1234");
2609 message_loop_
.Run();
2611 EXPECT_EQ(1, callback_count_
);
2612 EXPECT_EQ(0, error_callback_count_
);
2614 // One change for paired, and one for trusted.
2615 EXPECT_EQ(2, observer
.device_changed_count_
);
2616 EXPECT_EQ(device
, observer
.last_device_
);
2618 EXPECT_TRUE(device
->IsPaired());
2620 // Make sure the trusted property has been set to true.
2621 FakeBluetoothDeviceClient::Properties
* properties
=
2622 fake_bluetooth_device_client_
->GetProperties(
2623 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath
));
2624 ASSERT_TRUE(properties
->trusted
.value());
2626 // No pairing context should remain on the device.
2627 BluetoothDeviceChromeOS
* device_chromeos
=
2628 static_cast<BluetoothDeviceChromeOS
*>(device
);
2629 EXPECT_TRUE(device_chromeos
->GetPairing() == NULL
);
2632 TEST_F(BluetoothChromeOSTest
, IncomingPairConfirmPasskey
) {
2633 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
2637 TestPairingDelegate pairing_delegate
;
2638 adapter_
->AddPairingDelegate(
2640 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH
);
2642 // Requests that we confirm a displayed passkey.
2643 fake_bluetooth_device_client_
->CreateDevice(
2644 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
),
2645 dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath
));
2646 BluetoothDevice
* device
= adapter_
->GetDevice(
2647 FakeBluetoothDeviceClient::kConfirmPasskeyAddress
);
2648 ASSERT_TRUE(device
!= NULL
);
2649 ASSERT_FALSE(device
->IsPaired());
2651 TestObserver
observer(adapter_
);
2653 fake_bluetooth_device_client_
->SimulatePairing(
2654 dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath
), true,
2655 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback
,
2656 base::Unretained(this)));
2658 EXPECT_EQ(1, pairing_delegate
.call_count_
);
2659 EXPECT_EQ(1, pairing_delegate
.confirm_passkey_count_
);
2660 EXPECT_EQ(123456U, pairing_delegate
.last_passkey_
);
2662 // Confirm the passkey.
2663 device
->ConfirmPairing();
2664 message_loop_
.Run();
2666 EXPECT_EQ(1, callback_count_
);
2667 EXPECT_EQ(0, error_callback_count_
);
2669 // One change for paired, and one for trusted.
2670 EXPECT_EQ(2, observer
.device_changed_count_
);
2671 EXPECT_EQ(device
, observer
.last_device_
);
2673 EXPECT_TRUE(device
->IsPaired());
2675 // Make sure the trusted property has been set to true.
2676 FakeBluetoothDeviceClient::Properties
* properties
=
2677 fake_bluetooth_device_client_
->GetProperties(
2678 dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath
));
2679 ASSERT_TRUE(properties
->trusted
.value());
2681 // No pairing context should remain on the device.
2682 BluetoothDeviceChromeOS
* device_chromeos
=
2683 static_cast<BluetoothDeviceChromeOS
*>(device
);
2684 EXPECT_TRUE(device_chromeos
->GetPairing() == NULL
);
2687 TEST_F(BluetoothChromeOSTest
, IncomingPairRequestPasskey
) {
2688 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
2692 TestPairingDelegate pairing_delegate
;
2693 adapter_
->AddPairingDelegate(
2695 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH
);
2697 // Requests that we provide a Passkey.
2698 fake_bluetooth_device_client_
->CreateDevice(
2699 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
),
2700 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath
));
2701 BluetoothDevice
* device
= adapter_
->GetDevice(
2702 FakeBluetoothDeviceClient::kRequestPasskeyAddress
);
2703 ASSERT_TRUE(device
!= NULL
);
2704 ASSERT_FALSE(device
->IsPaired());
2706 TestObserver
observer(adapter_
);
2708 fake_bluetooth_device_client_
->SimulatePairing(
2709 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath
), true,
2710 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback
,
2711 base::Unretained(this)));
2713 EXPECT_EQ(1, pairing_delegate
.call_count_
);
2714 EXPECT_EQ(1, pairing_delegate
.request_passkey_count_
);
2717 device
->SetPasskey(1234);
2718 message_loop_
.Run();
2720 EXPECT_EQ(1, callback_count_
);
2721 EXPECT_EQ(0, error_callback_count_
);
2723 // One change for paired, and one for trusted.
2724 EXPECT_EQ(2, observer
.device_changed_count_
);
2725 EXPECT_EQ(device
, observer
.last_device_
);
2727 EXPECT_TRUE(device
->IsPaired());
2729 // Make sure the trusted property has been set to true.
2730 FakeBluetoothDeviceClient::Properties
* properties
=
2731 fake_bluetooth_device_client_
->GetProperties(
2732 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath
));
2733 ASSERT_TRUE(properties
->trusted
.value());
2735 // No pairing context should remain on the device.
2736 BluetoothDeviceChromeOS
* device_chromeos
=
2737 static_cast<BluetoothDeviceChromeOS
*>(device
);
2738 EXPECT_TRUE(device_chromeos
->GetPairing() == NULL
);
2741 TEST_F(BluetoothChromeOSTest
, IncomingPairJustWorks
) {
2742 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
2746 TestPairingDelegate pairing_delegate
;
2747 adapter_
->AddPairingDelegate(
2749 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH
);
2751 // Uses just-works pairing so, sinec this an incoming pairing, require
2752 // authorization from the user.
2753 fake_bluetooth_device_client_
->CreateDevice(
2754 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
),
2755 dbus::ObjectPath(FakeBluetoothDeviceClient::kJustWorksPath
));
2756 BluetoothDevice
* device
= adapter_
->GetDevice(
2757 FakeBluetoothDeviceClient::kJustWorksAddress
);
2758 ASSERT_TRUE(device
!= NULL
);
2759 ASSERT_FALSE(device
->IsPaired());
2761 TestObserver
observer(adapter_
);
2763 fake_bluetooth_device_client_
->SimulatePairing(
2764 dbus::ObjectPath(FakeBluetoothDeviceClient::kJustWorksPath
), true,
2765 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback
,
2766 base::Unretained(this)));
2768 EXPECT_EQ(1, pairing_delegate
.call_count_
);
2769 EXPECT_EQ(1, pairing_delegate
.authorize_pairing_count_
);
2771 // Confirm the pairing.
2772 device
->ConfirmPairing();
2773 message_loop_
.Run();
2775 EXPECT_EQ(1, callback_count_
);
2776 EXPECT_EQ(0, error_callback_count_
);
2778 // One change for paired, and one for trusted.
2779 EXPECT_EQ(2, observer
.device_changed_count_
);
2780 EXPECT_EQ(device
, observer
.last_device_
);
2782 EXPECT_TRUE(device
->IsPaired());
2784 // Make sure the trusted property has been set to true.
2785 FakeBluetoothDeviceClient::Properties
* properties
=
2786 fake_bluetooth_device_client_
->GetProperties(
2787 dbus::ObjectPath(FakeBluetoothDeviceClient::kJustWorksPath
));
2788 ASSERT_TRUE(properties
->trusted
.value());
2790 // No pairing context should remain on the device.
2791 BluetoothDeviceChromeOS
* device_chromeos
=
2792 static_cast<BluetoothDeviceChromeOS
*>(device
);
2793 EXPECT_TRUE(device_chromeos
->GetPairing() == NULL
);
2796 TEST_F(BluetoothChromeOSTest
, IncomingPairRequestPinCodeWithoutDelegate
) {
2797 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
2801 // Requires that we provide a PIN Code, without a pairing delegate,
2802 // that will be rejected.
2803 fake_bluetooth_device_client_
->CreateDevice(
2804 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
),
2805 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath
));
2806 BluetoothDevice
* device
= adapter_
->GetDevice(
2807 FakeBluetoothDeviceClient::kRequestPinCodeAddress
);
2808 ASSERT_TRUE(device
!= NULL
);
2809 ASSERT_FALSE(device
->IsPaired());
2811 TestObserver
observer(adapter_
);
2813 fake_bluetooth_device_client_
->SimulatePairing(
2814 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath
), true,
2815 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback
,
2816 base::Unretained(this)));
2818 message_loop_
.Run();
2820 EXPECT_EQ(0, callback_count_
);
2821 EXPECT_EQ(1, error_callback_count_
);
2822 EXPECT_EQ(bluetooth_device::kErrorAuthenticationRejected
, last_client_error_
);
2824 // No changes should be observer.
2825 EXPECT_EQ(0, observer
.device_changed_count_
);
2827 EXPECT_FALSE(device
->IsPaired());
2829 // No pairing context should remain on the device.
2830 BluetoothDeviceChromeOS
* device_chromeos
=
2831 static_cast<BluetoothDeviceChromeOS
*>(device
);
2832 EXPECT_TRUE(device_chromeos
->GetPairing() == NULL
);
2835 TEST_F(BluetoothChromeOSTest
, IncomingPairConfirmPasskeyWithoutDelegate
) {
2836 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
2840 // Requests that we confirm a displayed passkey, without a pairing delegate,
2841 // that will be rejected.
2842 fake_bluetooth_device_client_
->CreateDevice(
2843 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
),
2844 dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath
));
2845 BluetoothDevice
* device
= adapter_
->GetDevice(
2846 FakeBluetoothDeviceClient::kConfirmPasskeyAddress
);
2847 ASSERT_TRUE(device
!= NULL
);
2848 ASSERT_FALSE(device
->IsPaired());
2850 TestObserver
observer(adapter_
);
2852 fake_bluetooth_device_client_
->SimulatePairing(
2853 dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath
), true,
2854 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback
,
2855 base::Unretained(this)));
2857 message_loop_
.Run();
2859 EXPECT_EQ(0, callback_count_
);
2860 EXPECT_EQ(1, error_callback_count_
);
2861 EXPECT_EQ(bluetooth_device::kErrorAuthenticationRejected
, last_client_error_
);
2863 // No changes should be observer.
2864 EXPECT_EQ(0, observer
.device_changed_count_
);
2866 EXPECT_FALSE(device
->IsPaired());
2868 // No pairing context should remain on the device.
2869 BluetoothDeviceChromeOS
* device_chromeos
=
2870 static_cast<BluetoothDeviceChromeOS
*>(device
);
2871 EXPECT_TRUE(device_chromeos
->GetPairing() == NULL
);
2874 TEST_F(BluetoothChromeOSTest
, IncomingPairRequestPasskeyWithoutDelegate
) {
2875 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
2879 // Requests that we provide a displayed passkey, without a pairing delegate,
2880 // that will be rejected.
2881 fake_bluetooth_device_client_
->CreateDevice(
2882 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
),
2883 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath
));
2884 BluetoothDevice
* device
= adapter_
->GetDevice(
2885 FakeBluetoothDeviceClient::kRequestPasskeyAddress
);
2886 ASSERT_TRUE(device
!= NULL
);
2887 ASSERT_FALSE(device
->IsPaired());
2889 TestObserver
observer(adapter_
);
2891 fake_bluetooth_device_client_
->SimulatePairing(
2892 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath
), true,
2893 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback
,
2894 base::Unretained(this)));
2896 message_loop_
.Run();
2898 EXPECT_EQ(0, callback_count_
);
2899 EXPECT_EQ(1, error_callback_count_
);
2900 EXPECT_EQ(bluetooth_device::kErrorAuthenticationRejected
, last_client_error_
);
2902 // No changes should be observer.
2903 EXPECT_EQ(0, observer
.device_changed_count_
);
2905 EXPECT_FALSE(device
->IsPaired());
2907 // No pairing context should remain on the device.
2908 BluetoothDeviceChromeOS
* device_chromeos
=
2909 static_cast<BluetoothDeviceChromeOS
*>(device
);
2910 EXPECT_TRUE(device_chromeos
->GetPairing() == NULL
);
2913 TEST_F(BluetoothChromeOSTest
, IncomingPairJustWorksWithoutDelegate
) {
2914 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
2918 // Uses just-works pairing and thus requires authorization for incoming
2919 // pairings, without a pairing delegate, that will be rejected.
2920 fake_bluetooth_device_client_
->CreateDevice(
2921 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
),
2922 dbus::ObjectPath(FakeBluetoothDeviceClient::kJustWorksPath
));
2923 BluetoothDevice
* device
= adapter_
->GetDevice(
2924 FakeBluetoothDeviceClient::kJustWorksAddress
);
2925 ASSERT_TRUE(device
!= NULL
);
2926 ASSERT_FALSE(device
->IsPaired());
2928 TestObserver
observer(adapter_
);
2930 fake_bluetooth_device_client_
->SimulatePairing(
2931 dbus::ObjectPath(FakeBluetoothDeviceClient::kJustWorksPath
), true,
2932 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback
,
2933 base::Unretained(this)));
2935 message_loop_
.Run();
2937 EXPECT_EQ(0, callback_count_
);
2938 EXPECT_EQ(1, error_callback_count_
);
2939 EXPECT_EQ(bluetooth_device::kErrorAuthenticationRejected
, last_client_error_
);
2941 // No changes should be observer.
2942 EXPECT_EQ(0, observer
.device_changed_count_
);
2944 EXPECT_FALSE(device
->IsPaired());
2946 // No pairing context should remain on the device.
2947 BluetoothDeviceChromeOS
* device_chromeos
=
2948 static_cast<BluetoothDeviceChromeOS
*>(device
);
2949 EXPECT_TRUE(device_chromeos
->GetPairing() == NULL
);
2952 TEST_F(BluetoothChromeOSTest
, RemovePairingDelegateDuringPairing
) {
2953 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
2957 TestPairingDelegate pairing_delegate
;
2958 adapter_
->AddPairingDelegate(
2960 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH
);
2962 // Requests that we provide a Passkey.
2963 fake_bluetooth_device_client_
->CreateDevice(
2964 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
),
2965 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath
));
2966 BluetoothDevice
* device
= adapter_
->GetDevice(
2967 FakeBluetoothDeviceClient::kRequestPasskeyAddress
);
2968 ASSERT_TRUE(device
!= NULL
);
2969 ASSERT_FALSE(device
->IsPaired());
2971 TestObserver
observer(adapter_
);
2973 fake_bluetooth_device_client_
->SimulatePairing(
2974 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath
), true,
2975 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback
,
2976 base::Unretained(this)));
2978 EXPECT_EQ(1, pairing_delegate
.call_count_
);
2979 EXPECT_EQ(1, pairing_delegate
.request_passkey_count_
);
2981 // A pairing context should now be set on the device.
2982 BluetoothDeviceChromeOS
* device_chromeos
=
2983 static_cast<BluetoothDeviceChromeOS
*>(device
);
2984 ASSERT_TRUE(device_chromeos
->GetPairing() != NULL
);
2986 // Removing the pairing delegate should remove that pairing context.
2987 adapter_
->RemovePairingDelegate(&pairing_delegate
);
2989 EXPECT_TRUE(device_chromeos
->GetPairing() == NULL
);
2991 // Set the Passkey, this should now have no effect since the pairing has
2992 // been, in-effect, cancelled
2993 device
->SetPasskey(1234);
2995 EXPECT_EQ(0, callback_count_
);
2996 EXPECT_EQ(0, error_callback_count_
);
2997 EXPECT_EQ(0, observer
.device_changed_count_
);
2999 EXPECT_FALSE(device
->IsPaired());
3002 TEST_F(BluetoothChromeOSTest
, DeviceId
) {
3005 // Use the built-in paired device for this test, grab its Properties
3006 // structure so we can adjust the underlying modalias property.
3007 BluetoothDevice
* device
= adapter_
->GetDevice(
3008 FakeBluetoothDeviceClient::kPairedDeviceAddress
);
3009 FakeBluetoothDeviceClient::Properties
* properties
=
3010 fake_bluetooth_device_client_
->GetProperties(
3011 dbus::ObjectPath(FakeBluetoothDeviceClient::kPairedDevicePath
));
3013 ASSERT_TRUE(device
!= NULL
);
3014 ASSERT_TRUE(properties
!= NULL
);
3016 // Valid USB IF-assigned identifier.
3017 ASSERT_EQ("usb:v05ACp030Dd0306", properties
->modalias
.value());
3019 EXPECT_EQ(BluetoothDevice::VENDOR_ID_USB
, device
->GetVendorIDSource());
3020 EXPECT_EQ(0x05ac, device
->GetVendorID());
3021 EXPECT_EQ(0x030d, device
->GetProductID());
3022 EXPECT_EQ(0x0306, device
->GetDeviceID());
3024 // Valid Bluetooth SIG-assigned identifier.
3025 properties
->modalias
.ReplaceValue("bluetooth:v00E0p2400d0400");
3027 EXPECT_EQ(BluetoothDevice::VENDOR_ID_BLUETOOTH
, device
->GetVendorIDSource());
3028 EXPECT_EQ(0x00e0, device
->GetVendorID());
3029 EXPECT_EQ(0x2400, device
->GetProductID());
3030 EXPECT_EQ(0x0400, device
->GetDeviceID());
3032 // Invalid USB IF-assigned identifier.
3033 properties
->modalias
.ReplaceValue("usb:x00E0p2400d0400");
3035 EXPECT_EQ(BluetoothDevice::VENDOR_ID_UNKNOWN
, device
->GetVendorIDSource());
3036 EXPECT_EQ(0, device
->GetVendorID());
3037 EXPECT_EQ(0, device
->GetProductID());
3038 EXPECT_EQ(0, device
->GetDeviceID());
3040 // Invalid Bluetooth SIG-assigned identifier.
3041 properties
->modalias
.ReplaceValue("bluetooth:x00E0p2400d0400");
3043 EXPECT_EQ(BluetoothDevice::VENDOR_ID_UNKNOWN
, device
->GetVendorIDSource());
3044 EXPECT_EQ(0, device
->GetVendorID());
3045 EXPECT_EQ(0, device
->GetProductID());
3046 EXPECT_EQ(0, device
->GetDeviceID());
3048 // Unknown vendor specification identifier.
3049 properties
->modalias
.ReplaceValue("chrome:v00E0p2400d0400");
3051 EXPECT_EQ(BluetoothDevice::VENDOR_ID_UNKNOWN
, device
->GetVendorIDSource());
3052 EXPECT_EQ(0, device
->GetVendorID());
3053 EXPECT_EQ(0, device
->GetProductID());
3054 EXPECT_EQ(0, device
->GetDeviceID());
3057 TEST_F(BluetoothChromeOSTest
, GetConnectionInfoForDisconnectedDevice
) {
3059 BluetoothDevice
* device
=
3060 adapter_
->GetDevice(FakeBluetoothDeviceClient::kPairedDeviceAddress
);
3062 // Calling GetConnectionInfo for an unconnected device should return a result
3063 // in which all fields are filled with BluetoothDevice::kUnknownPower.
3064 BluetoothDevice::ConnectionInfo
conn_info(0, 0, 0);
3065 device
->GetConnectionInfo(base::Bind(&SaveConnectionInfo
, &conn_info
));
3066 int unknown_power
= BluetoothDevice::kUnknownPower
;
3067 EXPECT_NE(0, unknown_power
);
3068 EXPECT_EQ(unknown_power
, conn_info
.rssi
);
3069 EXPECT_EQ(unknown_power
, conn_info
.transmit_power
);
3070 EXPECT_EQ(unknown_power
, conn_info
.max_transmit_power
);
3073 TEST_F(BluetoothChromeOSTest
, GetConnectionInfoForConnectedDevice
) {
3075 BluetoothDevice
* device
=
3076 adapter_
->GetDevice(FakeBluetoothDeviceClient::kPairedDeviceAddress
);
3078 device
->Connect(NULL
, GetCallback(),
3079 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
3080 base::Unretained(this)));
3081 EXPECT_TRUE(device
->IsConnected());
3083 // Calling GetConnectionInfo for a connected device should return valid
3085 fake_bluetooth_device_client_
->UpdateConnectionInfo(-10, 3, 4);
3086 BluetoothDevice::ConnectionInfo conn_info
;
3087 device
->GetConnectionInfo(base::Bind(&SaveConnectionInfo
, &conn_info
));
3088 EXPECT_EQ(-10, conn_info
.rssi
);
3089 EXPECT_EQ(3, conn_info
.transmit_power
);
3090 EXPECT_EQ(4, conn_info
.max_transmit_power
);
3093 // Verifies Shutdown shuts down the adapter as expected.
3094 TEST_F(BluetoothChromeOSTest
, Shutdown
) {
3095 // Set up adapter. Set powered & discoverable, start discovery.
3097 adapter_
->SetPowered(true, GetCallback(), GetErrorCallback());
3098 adapter_
->SetDiscoverable(true, GetCallback(), GetErrorCallback());
3099 adapter_
->StartDiscoverySession(
3100 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
3101 base::Unretained(this)),
3102 GetErrorCallback());
3103 base::MessageLoop::current()->Run();
3104 ASSERT_EQ(3, callback_count_
);
3105 ASSERT_EQ(0, error_callback_count_
);
3106 callback_count_
= 0;
3108 TestPairingDelegate pairing_delegate
;
3109 adapter_
->AddPairingDelegate(
3110 &pairing_delegate
, BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH
);
3112 // Validate running adapter state.
3113 EXPECT_NE("", adapter_
->GetAddress());
3114 EXPECT_NE("", adapter_
->GetName());
3115 EXPECT_TRUE(adapter_
->IsInitialized());
3116 EXPECT_TRUE(adapter_
->IsPresent());
3117 EXPECT_TRUE(adapter_
->IsPowered());
3118 EXPECT_TRUE(adapter_
->IsDiscoverable());
3119 EXPECT_TRUE(adapter_
->IsDiscovering());
3120 EXPECT_EQ(2U, adapter_
->GetDevices().size());
3121 EXPECT_NE(nullptr, adapter_
->GetDevice(
3122 FakeBluetoothDeviceClient::kPairedDeviceAddress
));
3123 EXPECT_NE(dbus::ObjectPath(""), static_cast<BluetoothAdapterChromeOS
*>(
3124 adapter_
.get())->object_path());
3127 adapter_
->Shutdown();
3129 // Validate post shutdown state by calling all BluetoothAdapterChromeOS
3130 // members, in declaration order:
3132 adapter_
->Shutdown();
3133 // DeleteOnCorrectThread omitted as we don't want to delete in this test.
3135 TestObserver
observer(adapter_
); // Calls AddObserver
3136 } // TestObserver::~TestObserver calls RemoveObserver.
3137 EXPECT_EQ("", adapter_
->GetAddress());
3138 EXPECT_EQ("", adapter_
->GetName());
3140 adapter_
->SetName("", GetCallback(), GetErrorCallback());
3141 EXPECT_EQ(0, callback_count_
);
3142 EXPECT_EQ(1, error_callback_count_
--) << "SetName error";
3144 EXPECT_TRUE(adapter_
->IsInitialized());
3145 EXPECT_FALSE(adapter_
->IsPresent());
3146 EXPECT_FALSE(adapter_
->IsPowered());
3148 adapter_
->SetPowered(true, GetCallback(), GetErrorCallback());
3149 EXPECT_EQ(0, callback_count_
);
3150 EXPECT_EQ(1, error_callback_count_
--) << "SetPowered error";
3152 EXPECT_FALSE(adapter_
->IsDiscoverable());
3154 adapter_
->SetDiscoverable(true, GetCallback(), GetErrorCallback());
3155 EXPECT_EQ(0, callback_count_
);
3156 EXPECT_EQ(1, error_callback_count_
--) << "SetDiscoverable error";
3158 EXPECT_FALSE(adapter_
->IsDiscovering());
3159 // CreateRfcommService will DCHECK after Shutdown().
3160 // CreateL2capService will DCHECK after Shutdown().
3162 BluetoothAudioSink::Options audio_sink_options
;
3163 adapter_
->RegisterAudioSink(
3165 base::Bind(&BluetoothChromeOSTest::AudioSinkAcquiredCallback
,
3166 base::Unretained(this)),
3167 base::Bind(&BluetoothChromeOSTest::AudioSinkErrorCallback
,
3168 base::Unretained(this)));
3169 EXPECT_EQ(0, callback_count_
);
3170 EXPECT_EQ(1, error_callback_count_
--) << "RegisterAudioSink error";
3172 BluetoothAdapterChromeOS
* adapter_chrome_os
=
3173 static_cast<BluetoothAdapterChromeOS
*>(adapter_
.get());
3174 EXPECT_EQ(NULL
, adapter_chrome_os
->GetDeviceWithPath(dbus::ObjectPath("")));
3176 // Notify methods presume objects exist that are owned by the adapter and
3177 // destroyed in Shutdown(). Mocks are not attempted here that won't exist,
3178 // as verified below by EXPECT_EQ(0U, adapter_->GetDevices().size());
3179 // NotifyDeviceChanged
3180 // NotifyGattServiceAdded
3181 // NotifyGattServiceRemoved
3182 // NotifyGattServiceChanged
3183 // NotifyGattDiscoveryComplete
3184 // NotifyGattCharacteristicAdded
3185 // NotifyGattCharacteristicRemoved
3186 // NotifyGattDescriptorAdded
3187 // NotifyGattDescriptorRemoved
3188 // NotifyGattCharacteristicValueChanged
3189 // NotifyGattDescriptorValueChanged
3191 EXPECT_EQ(dbus::ObjectPath(""), adapter_chrome_os
->object_path());
3193 adapter_profile_
= NULL
;
3195 FakeBluetoothProfileServiceProviderDelegate profile_delegate
;
3196 adapter_chrome_os
->UseProfile(
3197 BluetoothUUID(), dbus::ObjectPath(""),
3198 BluetoothProfileManagerClient::Options(), &profile_delegate
,
3199 base::Bind(&BluetoothChromeOSTest::ProfileRegisteredCallback
,
3200 base::Unretained(this)),
3201 base::Bind(&BluetoothChromeOSTest::ErrorCompletionCallback
,
3202 base::Unretained(this)));
3204 EXPECT_FALSE(adapter_profile_
) << "UseProfile error";
3205 EXPECT_EQ(0, callback_count_
) << "UseProfile error";
3206 EXPECT_EQ(1, error_callback_count_
--) << "UseProfile error";
3208 // Protected and private methods:
3210 adapter_chrome_os
->RemovePairingDelegateInternal(&pairing_delegate
);
3211 // AdapterAdded() invalid post Shutdown because it calls SetAdapter.
3212 adapter_chrome_os
->AdapterRemoved(dbus::ObjectPath("x"));
3213 adapter_chrome_os
->AdapterPropertyChanged(dbus::ObjectPath("x"), "");
3214 adapter_chrome_os
->DeviceAdded(dbus::ObjectPath(""));
3215 adapter_chrome_os
->DeviceRemoved(dbus::ObjectPath(""));
3216 adapter_chrome_os
->DevicePropertyChanged(dbus::ObjectPath(""), "");
3217 adapter_chrome_os
->InputPropertyChanged(dbus::ObjectPath(""), "");
3218 // BluetoothAgentServiceProvider::Delegate omitted, dbus will be shutdown,
3219 // with the exception of Released.
3220 adapter_chrome_os
->Released();
3222 adapter_chrome_os
->OnRegisterAgent();
3223 adapter_chrome_os
->OnRegisterAgentError("", "");
3224 adapter_chrome_os
->OnRequestDefaultAgent();
3225 adapter_chrome_os
->OnRequestDefaultAgentError("", "");
3227 adapter_chrome_os
->OnRegisterAudioSink(
3228 base::Bind(&BluetoothChromeOSTest::AudioSinkAcquiredCallback
,
3229 base::Unretained(this)),
3230 base::Bind(&BluetoothChromeOSTest::AudioSinkErrorCallback
,
3231 base::Unretained(this)),
3232 scoped_refptr
<device::BluetoothAudioSink
>());
3233 EXPECT_EQ(0, callback_count_
);
3234 EXPECT_EQ(1, error_callback_count_
--) << "RegisterAudioSink error";
3236 // GetPairing will DCHECK after Shutdown().
3237 // SetAdapter will DCHECK after Shutdown().
3238 // SetDefaultAdapterName will DCHECK after Shutdown().
3239 // RemoveAdapter will DCHECK after Shutdown().
3240 adapter_chrome_os
->PoweredChanged(false);
3241 adapter_chrome_os
->DiscoverableChanged(false);
3242 adapter_chrome_os
->DiscoveringChanged(false);
3243 adapter_chrome_os
->PresentChanged(false);
3245 adapter_chrome_os
->OnSetDiscoverable(GetCallback(), GetErrorCallback(), true);
3246 EXPECT_EQ(0, callback_count_
) << "OnSetDiscoverable error";
3247 EXPECT_EQ(1, error_callback_count_
--) << "OnSetDiscoverable error";
3249 adapter_chrome_os
->OnPropertyChangeCompleted(GetCallback(),
3250 GetErrorCallback(), true);
3251 EXPECT_EQ(0, callback_count_
) << "OnPropertyChangeCompleted error";
3252 EXPECT_EQ(1, error_callback_count_
--) << "OnPropertyChangeCompleted error";
3254 adapter_chrome_os
->AddDiscoverySession(GetCallback(), GetErrorCallback());
3255 EXPECT_EQ(0, callback_count_
) << "AddDiscoverySession error";
3256 EXPECT_EQ(1, error_callback_count_
--) << "AddDiscoverySession error";
3258 adapter_chrome_os
->RemoveDiscoverySession(GetCallback(), GetErrorCallback());
3259 EXPECT_EQ(0, callback_count_
) << "RemoveDiscoverySession error";
3260 EXPECT_EQ(1, error_callback_count_
--) << "RemoveDiscoverySession error";
3262 // OnStartDiscovery tested in Shutdown_OnStartDiscovery
3263 // OnStartDiscoveryError tested in Shutdown_OnStartDiscoveryError
3264 // OnStopDiscovery tested in Shutdown_OnStopDiscovery
3265 // OnStopDiscoveryError tested in Shutdown_OnStopDiscoveryError
3267 adapter_profile_
= NULL
;
3269 // OnRegisterProfile SetProfileDelegate, OnRegisterProfileError, require
3270 // UseProfile to be set first, do so again here just before calling them.
3271 adapter_chrome_os
->UseProfile(
3272 BluetoothUUID(), dbus::ObjectPath(""),
3273 BluetoothProfileManagerClient::Options(), &profile_delegate
,
3274 base::Bind(&BluetoothChromeOSTest::ProfileRegisteredCallback
,
3275 base::Unretained(this)),
3276 base::Bind(&BluetoothChromeOSTest::ErrorCompletionCallback
,
3277 base::Unretained(this)));
3279 EXPECT_FALSE(adapter_profile_
) << "UseProfile error";
3280 EXPECT_EQ(0, callback_count_
) << "UseProfile error";
3281 EXPECT_EQ(1, error_callback_count_
--) << "UseProfile error";
3283 adapter_chrome_os
->SetProfileDelegate(
3284 BluetoothUUID(), dbus::ObjectPath(""), &profile_delegate
,
3285 base::Bind(&BluetoothChromeOSTest::ProfileRegisteredCallback
,
3286 base::Unretained(this)),
3287 base::Bind(&BluetoothChromeOSTest::ErrorCompletionCallback
,
3288 base::Unretained(this)));
3289 EXPECT_EQ(0, callback_count_
) << "SetProfileDelegate error";
3290 EXPECT_EQ(1, error_callback_count_
--) << "SetProfileDelegate error";
3292 adapter_chrome_os
->OnRegisterProfileError(BluetoothUUID(), "", "");
3293 EXPECT_EQ(0, callback_count_
) << "OnRegisterProfileError error";
3294 EXPECT_EQ(0, error_callback_count_
) << "OnRegisterProfileError error";
3296 adapter_chrome_os
->ProcessQueuedDiscoveryRequests();
3298 // From BluetoothAdapater:
3300 adapter_
->StartDiscoverySession(
3301 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
3302 base::Unretained(this)),
3303 GetErrorCallback());
3304 EXPECT_EQ(0, callback_count_
) << "StartDiscoverySession error";
3305 EXPECT_EQ(1, error_callback_count_
--) << "StartDiscoverySession error";
3307 EXPECT_EQ(0U, adapter_
->GetDevices().size());
3308 EXPECT_EQ(nullptr, adapter_
->GetDevice(
3309 FakeBluetoothDeviceClient::kPairedDeviceAddress
));
3310 TestPairingDelegate pairing_delegate2
;
3311 adapter_
->AddPairingDelegate(
3312 &pairing_delegate2
, BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH
);
3313 adapter_
->RemovePairingDelegate(&pairing_delegate2
);
3316 // Verifies post-Shutdown of discovery sessions and OnStartDiscovery.
3317 TEST_F(BluetoothChromeOSTest
, Shutdown_OnStartDiscovery
) {
3318 const int kNumberOfDiscoverySessions
= 10;
3320 BluetoothAdapterChromeOS
* adapter_chrome_os
=
3321 static_cast<BluetoothAdapterChromeOS
*>(adapter_
.get());
3323 for (int i
= 0; i
< kNumberOfDiscoverySessions
; i
++) {
3324 adapter_chrome_os
->AddDiscoverySession(GetCallback(), GetErrorCallback());
3326 adapter_
->Shutdown();
3327 adapter_chrome_os
->OnStartDiscovery(GetCallback(), GetErrorCallback());
3329 EXPECT_EQ(0, callback_count_
);
3330 EXPECT_EQ(kNumberOfDiscoverySessions
, error_callback_count_
);
3333 // Verifies post-Shutdown of discovery sessions and OnStartDiscoveryError.
3334 TEST_F(BluetoothChromeOSTest
, Shutdown_OnStartDiscoveryError
) {
3335 const int kNumberOfDiscoverySessions
= 10;
3337 BluetoothAdapterChromeOS
* adapter_chrome_os
=
3338 static_cast<BluetoothAdapterChromeOS
*>(adapter_
.get());
3340 for (int i
= 0; i
< kNumberOfDiscoverySessions
; i
++) {
3341 adapter_chrome_os
->AddDiscoverySession(GetCallback(), GetErrorCallback());
3343 adapter_
->Shutdown();
3344 adapter_chrome_os
->OnStartDiscoveryError(GetCallback(), GetErrorCallback(),
3347 EXPECT_EQ(0, callback_count_
);
3348 EXPECT_EQ(kNumberOfDiscoverySessions
, error_callback_count_
);
3351 // Verifies post-Shutdown of discovery sessions and OnStartDiscovery.
3352 TEST_F(BluetoothChromeOSTest
, Shutdown_OnStopDiscovery
) {
3353 const int kNumberOfDiscoverySessions
= 10;
3355 BluetoothAdapterChromeOS
* adapter_chrome_os
=
3356 static_cast<BluetoothAdapterChromeOS
*>(adapter_
.get());
3358 // In order to queue up discovery sessions before an OnStopDiscovery call
3359 // RemoveDiscoverySession must be called, so Add, Start, and Remove:
3360 adapter_chrome_os
->AddDiscoverySession(GetCallback(), GetErrorCallback());
3361 adapter_chrome_os
->OnStartDiscovery(GetCallback(), GetErrorCallback());
3362 adapter_chrome_os
->RemoveDiscoverySession(GetCallback(), GetErrorCallback());
3363 callback_count_
= 0;
3364 error_callback_count_
= 0;
3365 // Can now queue discovery sessions while waiting for OnStopDiscovery.
3366 for (int i
= 0; i
< kNumberOfDiscoverySessions
; i
++) {
3367 adapter_chrome_os
->AddDiscoverySession(GetCallback(), GetErrorCallback());
3369 adapter_
->Shutdown();
3370 adapter_chrome_os
->OnStopDiscovery(GetCallback());
3372 // 1 successful stopped discovery from RemoveDiscoverySession, and
3373 // kNumberOfDiscoverySessions errors from AddDiscoverySession/OnStopDiscovery.
3374 EXPECT_EQ(1, callback_count_
);
3375 EXPECT_EQ(kNumberOfDiscoverySessions
, error_callback_count_
);
3378 // Verifies post-Shutdown of discovery sessions and OnStopDiscoveryError.
3379 TEST_F(BluetoothChromeOSTest
, Shutdown_OnStopDiscoveryError
) {
3380 const int kNumberOfDiscoverySessions
= 10;
3382 BluetoothAdapterChromeOS
* adapter_chrome_os
=
3383 static_cast<BluetoothAdapterChromeOS
*>(adapter_
.get());
3385 // In order to queue up discovery sessions before an OnStopDiscoveryError call
3386 // RemoveDiscoverySession must be called, so Add, Start, and Remove:
3387 adapter_chrome_os
->AddDiscoverySession(GetCallback(), GetErrorCallback());
3388 adapter_chrome_os
->OnStartDiscovery(GetCallback(), GetErrorCallback());
3389 adapter_chrome_os
->RemoveDiscoverySession(GetCallback(), GetErrorCallback());
3390 callback_count_
= 0;
3391 error_callback_count_
= 0;
3392 // Can now queue discovery sessions while waiting for OnStopDiscoveryError.
3393 for (int i
= 0; i
< kNumberOfDiscoverySessions
; i
++) {
3394 adapter_chrome_os
->AddDiscoverySession(GetCallback(), GetErrorCallback());
3396 adapter_
->Shutdown();
3397 adapter_chrome_os
->OnStopDiscoveryError(GetErrorCallback(), "", "");
3399 // 1 error reported to RemoveDiscoverySession because of OnStopDiscoveryError,
3400 // and kNumberOfDiscoverySessions errors queued with AddDiscoverySession.
3401 EXPECT_EQ(0, callback_count_
);
3402 EXPECT_EQ(1 + kNumberOfDiscoverySessions
, error_callback_count_
);
3405 } // namespace chromeos