1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/memory/scoped_vector.h"
6 #include "base/message_loop/message_loop.h"
7 #include "base/run_loop.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chromeos/dbus/dbus_thread_manager.h"
10 #include "chromeos/dbus/fake_bluetooth_adapter_client.h"
11 #include "chromeos/dbus/fake_bluetooth_agent_manager_client.h"
12 #include "chromeos/dbus/fake_bluetooth_device_client.h"
13 #include "chromeos/dbus/fake_bluetooth_gatt_service_client.h"
14 #include "chromeos/dbus/fake_bluetooth_input_client.h"
15 #include "dbus/object_path.h"
16 #include "device/bluetooth/bluetooth_adapter.h"
17 #include "device/bluetooth/bluetooth_adapter_chromeos.h"
18 #include "device/bluetooth/bluetooth_adapter_factory.h"
19 #include "device/bluetooth/bluetooth_device.h"
20 #include "device/bluetooth/bluetooth_device_chromeos.h"
21 #include "device/bluetooth/bluetooth_discovery_session.h"
22 #include "device/bluetooth/bluetooth_pairing_chromeos.h"
23 #include "device/bluetooth/test/test_bluetooth_adapter_observer.h"
24 #include "testing/gtest/include/gtest/gtest.h"
25 #include "third_party/cros_system_api/dbus/service_constants.h"
27 using device::BluetoothAdapter
;
28 using device::BluetoothAdapterFactory
;
29 using device::BluetoothAudioSink
;
30 using device::BluetoothDevice
;
31 using device::BluetoothDiscoveryFilter
;
32 using device::BluetoothDiscoverySession
;
33 using device::BluetoothUUID
;
34 using device::TestBluetoothAdapterObserver
;
40 // Callback for BluetoothDevice::GetConnectionInfo() that simply saves the
41 // connection info to the bound argument.
42 void SaveConnectionInfo(BluetoothDevice::ConnectionInfo
* out
,
43 const BluetoothDevice::ConnectionInfo
& conn_info
) {
47 class FakeBluetoothProfileServiceProviderDelegate
48 : public chromeos::BluetoothProfileServiceProvider::Delegate
{
50 FakeBluetoothProfileServiceProviderDelegate() {}
52 // BluetoothProfileServiceProvider::Delegate:
53 void Released() override
{}
55 void NewConnection(const dbus::ObjectPath
&,
56 scoped_ptr
<dbus::FileDescriptor
>,
57 const BluetoothProfileServiceProvider::Delegate::Options
&,
58 const ConfirmationCallback
&) override
{}
60 void RequestDisconnection(const dbus::ObjectPath
&,
61 const ConfirmationCallback
&) override
{}
63 void Cancel() override
{}
68 class TestPairingDelegate
: public BluetoothDevice::PairingDelegate
{
72 request_pincode_count_(0),
73 request_passkey_count_(0),
74 display_pincode_count_(0),
75 display_passkey_count_(0),
76 keys_entered_count_(0),
77 confirm_passkey_count_(0),
78 authorize_pairing_count_(0),
79 last_passkey_(9999999U),
80 last_entered_(999U) {}
81 ~TestPairingDelegate() override
{}
83 void RequestPinCode(BluetoothDevice
* device
) override
{
85 ++request_pincode_count_
;
89 void RequestPasskey(BluetoothDevice
* device
) override
{
91 ++request_passkey_count_
;
95 void DisplayPinCode(BluetoothDevice
* device
,
96 const std::string
& pincode
) override
{
98 ++display_pincode_count_
;
99 last_pincode_
= pincode
;
103 void DisplayPasskey(BluetoothDevice
* device
, uint32 passkey
) override
{
105 ++display_passkey_count_
;
106 last_passkey_
= passkey
;
110 void KeysEntered(BluetoothDevice
* device
, uint32 entered
) override
{
112 ++keys_entered_count_
;
113 last_entered_
= entered
;
117 void ConfirmPasskey(BluetoothDevice
* device
, uint32 passkey
) override
{
119 ++confirm_passkey_count_
;
120 last_passkey_
= passkey
;
124 void AuthorizePairing(BluetoothDevice
* device
) override
{
126 ++authorize_pairing_count_
;
131 int request_pincode_count_
;
132 int request_passkey_count_
;
133 int display_pincode_count_
;
134 int display_passkey_count_
;
135 int keys_entered_count_
;
136 int confirm_passkey_count_
;
137 int authorize_pairing_count_
;
138 uint32 last_passkey_
;
139 uint32 last_entered_
;
140 std::string last_pincode_
;
143 // Some tests use a message loop since background processing is simulated;
144 // break out of those loops.
145 void QuitMessageLoop() {
146 if (base::MessageLoop::current() &&
147 base::MessageLoop::current()->is_running()) {
148 base::MessageLoop::current()->Quit();
153 class BluetoothChromeOSTest
: public testing::Test
{
155 void SetUp() override
{
156 scoped_ptr
<DBusThreadManagerSetter
> dbus_setter
=
157 chromeos::DBusThreadManager::GetSetterForTesting();
158 // We need to initialize DBusThreadManager early to prevent
159 // Bluetooth*::Create() methods from picking the real instead of fake
161 fake_bluetooth_adapter_client_
= new FakeBluetoothAdapterClient
;
162 dbus_setter
->SetBluetoothAdapterClient(
163 scoped_ptr
<BluetoothAdapterClient
>(fake_bluetooth_adapter_client_
));
164 fake_bluetooth_device_client_
= new FakeBluetoothDeviceClient
;
165 dbus_setter
->SetBluetoothDeviceClient(
166 scoped_ptr
<BluetoothDeviceClient
>(fake_bluetooth_device_client_
));
167 dbus_setter
->SetBluetoothInputClient(
168 scoped_ptr
<BluetoothInputClient
>(new FakeBluetoothInputClient
));
169 dbus_setter
->SetBluetoothAgentManagerClient(
170 scoped_ptr
<BluetoothAgentManagerClient
>(
171 new FakeBluetoothAgentManagerClient
));
172 dbus_setter
->SetBluetoothGattServiceClient(
173 scoped_ptr
<BluetoothGattServiceClient
>(
174 new FakeBluetoothGattServiceClient
));
176 fake_bluetooth_adapter_client_
->SetSimulationIntervalMs(10);
179 error_callback_count_
= 0;
180 last_connect_error_
= BluetoothDevice::ERROR_UNKNOWN
;
181 last_client_error_
= "";
184 void TearDown() override
{
185 for (ScopedVector
<BluetoothDiscoverySession
>::iterator iter
=
186 discovery_sessions_
.begin();
187 iter
!= discovery_sessions_
.end();
189 BluetoothDiscoverySession
* session
= *iter
;
190 if (!session
->IsActive())
193 session
->Stop(GetCallback(), GetErrorCallback());
195 ASSERT_EQ(1, callback_count_
);
197 discovery_sessions_
.clear();
199 DBusThreadManager::Shutdown();
208 base::Closure
GetCallback() {
209 return base::Bind(&BluetoothChromeOSTest::Callback
, base::Unretained(this));
212 void DiscoverySessionCallback(
213 scoped_ptr
<BluetoothDiscoverySession
> discovery_session
) {
215 discovery_sessions_
.push_back(discovery_session
.release());
219 void AudioSinkAcquiredCallback(scoped_refptr
<BluetoothAudioSink
>) {
224 void ProfileRegisteredCallback(BluetoothAdapterProfileChromeOS
* profile
) {
225 adapter_profile_
= profile
;
230 void ErrorCallback() {
231 ++error_callback_count_
;
235 base::Closure
GetErrorCallback() {
236 return base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
237 base::Unretained(this));
240 void DBusErrorCallback(const std::string
& error_name
,
241 const std::string
& error_message
) {
242 ++error_callback_count_
;
243 last_client_error_
= error_name
;
247 void ConnectErrorCallback(BluetoothDevice::ConnectErrorCode error
) {
248 ++error_callback_count_
;
249 last_connect_error_
= error
;
252 void AudioSinkErrorCallback(BluetoothAudioSink::ErrorCode
) {
253 ++error_callback_count_
;
257 void ErrorCompletionCallback(const std::string
& error_message
) {
258 ++error_callback_count_
;
262 // Call to fill the adapter_ member with a BluetoothAdapter instance.
264 adapter_
= new BluetoothAdapterChromeOS();
265 ASSERT_TRUE(adapter_
.get() != nullptr);
266 ASSERT_TRUE(adapter_
->IsInitialized());
269 // Run a discovery phase until the named device is detected, or if the named
270 // device is not created, the discovery process ends without finding it.
272 // The correct behavior of discovery is tested by the "Discovery" test case
273 // without using this function.
274 void DiscoverDevice(const std::string
& address
) {
275 ASSERT_TRUE(adapter_
.get() != nullptr);
276 ASSERT_TRUE(base::MessageLoop::current() != nullptr);
277 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
279 TestBluetoothAdapterObserver
observer(adapter_
);
281 adapter_
->SetPowered(true, GetCallback(), GetErrorCallback());
282 adapter_
->StartDiscoverySession(
283 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
284 base::Unretained(this)),
286 base::MessageLoop::current()->Run();
287 ASSERT_EQ(2, callback_count_
);
288 ASSERT_EQ(0, error_callback_count_
);
289 ASSERT_EQ((size_t)1, discovery_sessions_
.size());
290 ASSERT_TRUE(discovery_sessions_
[0]->IsActive());
293 ASSERT_TRUE(adapter_
->IsPowered());
294 ASSERT_TRUE(adapter_
->IsDiscovering());
296 while (!observer
.device_removed_count() &&
297 observer
.last_device_address() != address
)
298 base::MessageLoop::current()->Run();
300 discovery_sessions_
[0]->Stop(GetCallback(), GetErrorCallback());
301 base::MessageLoop::current()->Run();
302 ASSERT_EQ(1, callback_count_
);
303 ASSERT_EQ(0, error_callback_count_
);
306 ASSERT_FALSE(adapter_
->IsDiscovering());
309 // Run a discovery phase so we have devices that can be paired with.
310 void DiscoverDevices() {
311 // Pass an invalid address for the device so that the discovery process
312 // completes with all devices.
313 DiscoverDevice("does not exist");
317 base::MessageLoop message_loop_
;
318 FakeBluetoothAdapterClient
* fake_bluetooth_adapter_client_
;
319 FakeBluetoothDeviceClient
* fake_bluetooth_device_client_
;
320 scoped_refptr
<BluetoothAdapter
> adapter_
;
323 int error_callback_count_
;
324 enum BluetoothDevice::ConnectErrorCode last_connect_error_
;
325 std::string last_client_error_
;
326 ScopedVector
<BluetoothDiscoverySession
> discovery_sessions_
;
327 BluetoothAdapterProfileChromeOS
* adapter_profile_
;
330 // Some tests use a message loop since background processing is simulated;
331 // break out of those loops.
332 void QuitMessageLoop() {
333 if (base::MessageLoop::current() &&
334 base::MessageLoop::current()->is_running()) {
335 base::MessageLoop::current()->Quit();
340 TEST_F(BluetoothChromeOSTest
, AlreadyPresent
) {
343 // This verifies that the class gets the list of adapters when created;
344 // and initializes with an existing adapter if there is one.
345 EXPECT_TRUE(adapter_
->IsPresent());
346 EXPECT_FALSE(adapter_
->IsPowered());
347 EXPECT_EQ(FakeBluetoothAdapterClient::kAdapterAddress
,
348 adapter_
->GetAddress());
349 EXPECT_FALSE(adapter_
->IsDiscovering());
351 // There should be a device
352 BluetoothAdapter::DeviceList devices
= adapter_
->GetDevices();
353 EXPECT_EQ(2U, devices
.size());
354 EXPECT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress
,
355 devices
[0]->GetAddress());
356 EXPECT_EQ(FakeBluetoothDeviceClient::kPairedUnconnectableDeviceAddress
,
357 devices
[1]->GetAddress());
360 TEST_F(BluetoothChromeOSTest
, BecomePresent
) {
361 fake_bluetooth_adapter_client_
->SetVisible(false);
363 ASSERT_FALSE(adapter_
->IsPresent());
365 // Install an observer; expect the AdapterPresentChanged to be called
366 // with true, and IsPresent() to return true.
367 TestBluetoothAdapterObserver
observer(adapter_
);
369 fake_bluetooth_adapter_client_
->SetVisible(true);
371 EXPECT_EQ(1, observer
.present_changed_count());
372 EXPECT_TRUE(observer
.last_present());
374 EXPECT_TRUE(adapter_
->IsPresent());
376 // We should have had a device announced.
377 EXPECT_EQ(2, observer
.device_added_count());
378 EXPECT_EQ(FakeBluetoothDeviceClient::kPairedUnconnectableDeviceAddress
,
379 observer
.last_device_address());
381 // Other callbacks shouldn't be called if the values are false.
382 EXPECT_EQ(0, observer
.powered_changed_count());
383 EXPECT_EQ(0, observer
.discovering_changed_count());
384 EXPECT_FALSE(adapter_
->IsPowered());
385 EXPECT_FALSE(adapter_
->IsDiscovering());
388 TEST_F(BluetoothChromeOSTest
, BecomeNotPresent
) {
390 ASSERT_TRUE(adapter_
->IsPresent());
392 // Install an observer; expect the AdapterPresentChanged to be called
393 // with false, and IsPresent() to return false.
394 TestBluetoothAdapterObserver
observer(adapter_
);
396 fake_bluetooth_adapter_client_
->SetVisible(false);
398 EXPECT_EQ(1, observer
.present_changed_count());
399 EXPECT_FALSE(observer
.last_present());
401 EXPECT_FALSE(adapter_
->IsPresent());
403 // We should have had a device removed.
404 EXPECT_EQ(2, observer
.device_removed_count());
405 EXPECT_EQ(FakeBluetoothDeviceClient::kPairedUnconnectableDeviceAddress
,
406 observer
.last_device_address());
408 // Other callbacks shouldn't be called since the values are false.
409 EXPECT_EQ(0, observer
.powered_changed_count());
410 EXPECT_EQ(0, observer
.discovering_changed_count());
411 EXPECT_FALSE(adapter_
->IsPowered());
412 EXPECT_FALSE(adapter_
->IsDiscovering());
415 TEST_F(BluetoothChromeOSTest
, SecondAdapter
) {
417 ASSERT_TRUE(adapter_
->IsPresent());
419 // Install an observer, then add a second adapter. Nothing should change,
420 // we ignore the second adapter.
421 TestBluetoothAdapterObserver
observer(adapter_
);
423 fake_bluetooth_adapter_client_
->SetSecondVisible(true);
425 EXPECT_EQ(0, observer
.present_changed_count());
427 EXPECT_TRUE(adapter_
->IsPresent());
428 EXPECT_EQ(FakeBluetoothAdapterClient::kAdapterAddress
,
429 adapter_
->GetAddress());
431 // Try removing the first adapter, we should now act as if the adapter
432 // is no longer present rather than fall back to the second.
433 fake_bluetooth_adapter_client_
->SetVisible(false);
435 EXPECT_EQ(1, observer
.present_changed_count());
436 EXPECT_FALSE(observer
.last_present());
438 EXPECT_FALSE(adapter_
->IsPresent());
440 // We should have had a device removed.
441 EXPECT_EQ(2, observer
.device_removed_count());
442 EXPECT_EQ(FakeBluetoothDeviceClient::kPairedUnconnectableDeviceAddress
,
443 observer
.last_device_address());
445 // Other callbacks shouldn't be called since the values are false.
446 EXPECT_EQ(0, observer
.powered_changed_count());
447 EXPECT_EQ(0, observer
.discovering_changed_count());
448 EXPECT_FALSE(adapter_
->IsPowered());
449 EXPECT_FALSE(adapter_
->IsDiscovering());
453 // Removing the second adapter shouldn't set anything either.
454 fake_bluetooth_adapter_client_
->SetSecondVisible(false);
456 EXPECT_EQ(0, observer
.device_removed_count());
457 EXPECT_EQ(0, observer
.powered_changed_count());
458 EXPECT_EQ(0, observer
.discovering_changed_count());
461 TEST_F(BluetoothChromeOSTest
, BecomePowered
) {
463 ASSERT_FALSE(adapter_
->IsPowered());
465 // Install an observer; expect the AdapterPoweredChanged to be called
466 // with true, and IsPowered() to return true.
467 TestBluetoothAdapterObserver
observer(adapter_
);
469 adapter_
->SetPowered(true, GetCallback(), GetErrorCallback());
470 EXPECT_EQ(1, callback_count_
);
471 EXPECT_EQ(0, error_callback_count_
);
473 EXPECT_EQ(1, observer
.powered_changed_count());
474 EXPECT_TRUE(observer
.last_powered());
476 EXPECT_TRUE(adapter_
->IsPowered());
479 TEST_F(BluetoothChromeOSTest
, BecomeNotPowered
) {
481 adapter_
->SetPowered(true, GetCallback(), GetErrorCallback());
482 EXPECT_EQ(1, callback_count_
);
483 EXPECT_EQ(0, error_callback_count_
);
486 ASSERT_TRUE(adapter_
->IsPowered());
488 // Install an observer; expect the AdapterPoweredChanged to be called
489 // with false, and IsPowered() to return false.
490 TestBluetoothAdapterObserver
observer(adapter_
);
492 adapter_
->SetPowered(false, GetCallback(), GetErrorCallback());
493 EXPECT_EQ(1, callback_count_
);
494 EXPECT_EQ(0, error_callback_count_
);
496 EXPECT_EQ(1, observer
.powered_changed_count());
497 EXPECT_FALSE(observer
.last_powered());
499 EXPECT_FALSE(adapter_
->IsPowered());
502 TEST_F(BluetoothChromeOSTest
, SetPoweredWhenNotPresent
) {
504 ASSERT_TRUE(adapter_
->IsPresent());
506 // Install an observer; expect the AdapterPresentChanged to be called
507 // with false, and IsPresent() to return false.
508 TestBluetoothAdapterObserver
observer(adapter_
);
510 fake_bluetooth_adapter_client_
->SetVisible(false);
512 EXPECT_EQ(1, observer
.present_changed_count());
513 EXPECT_FALSE(observer
.last_present());
515 EXPECT_FALSE(adapter_
->IsPresent());
516 EXPECT_FALSE(adapter_
->IsPowered());
518 adapter_
->SetPowered(true, GetCallback(), GetErrorCallback());
519 EXPECT_EQ(0, callback_count_
);
520 EXPECT_EQ(1, error_callback_count_
);
522 EXPECT_EQ(0, observer
.powered_changed_count());
523 EXPECT_FALSE(observer
.last_powered());
525 EXPECT_FALSE(adapter_
->IsPowered());
528 TEST_F(BluetoothChromeOSTest
, ChangeAdapterName
) {
531 static const std::string
new_name(".__.");
533 adapter_
->SetName(new_name
, GetCallback(), GetErrorCallback());
534 EXPECT_EQ(1, callback_count_
);
535 EXPECT_EQ(0, error_callback_count_
);
537 EXPECT_EQ(new_name
, adapter_
->GetName());
540 TEST_F(BluetoothChromeOSTest
, ChangeAdapterNameWhenNotPresent
) {
542 ASSERT_TRUE(adapter_
->IsPresent());
544 // Install an observer; expect the AdapterPresentChanged to be called
545 // with false, and IsPresent() to return false.
546 TestBluetoothAdapterObserver
observer(adapter_
);
548 fake_bluetooth_adapter_client_
->SetVisible(false);
550 EXPECT_EQ(1, observer
.present_changed_count());
551 EXPECT_FALSE(observer
.last_present());
553 EXPECT_FALSE(adapter_
->IsPresent());
554 EXPECT_FALSE(adapter_
->IsPowered());
556 adapter_
->SetName("^o^", GetCallback(), GetErrorCallback());
557 EXPECT_EQ(0, callback_count_
);
558 EXPECT_EQ(1, error_callback_count_
);
560 EXPECT_EQ("", adapter_
->GetName());
563 TEST_F(BluetoothChromeOSTest
, BecomeDiscoverable
) {
565 ASSERT_FALSE(adapter_
->IsDiscoverable());
567 // Install an observer; expect the AdapterDiscoverableChanged to be called
568 // with true, and IsDiscoverable() to return true.
569 TestBluetoothAdapterObserver
observer(adapter_
);
571 adapter_
->SetDiscoverable(true, GetCallback(), GetErrorCallback());
572 EXPECT_EQ(1, callback_count_
);
573 EXPECT_EQ(0, error_callback_count_
);
575 EXPECT_EQ(1, observer
.discoverable_changed_count());
577 EXPECT_TRUE(adapter_
->IsDiscoverable());
580 TEST_F(BluetoothChromeOSTest
, BecomeNotDiscoverable
) {
582 adapter_
->SetDiscoverable(true, GetCallback(), GetErrorCallback());
583 EXPECT_EQ(1, callback_count_
);
584 EXPECT_EQ(0, error_callback_count_
);
587 ASSERT_TRUE(adapter_
->IsDiscoverable());
589 // Install an observer; expect the AdapterDiscoverableChanged to be called
590 // with false, and IsDiscoverable() to return false.
591 TestBluetoothAdapterObserver
observer(adapter_
);
593 adapter_
->SetDiscoverable(false, GetCallback(), GetErrorCallback());
594 EXPECT_EQ(1, callback_count_
);
595 EXPECT_EQ(0, error_callback_count_
);
597 EXPECT_EQ(1, observer
.discoverable_changed_count());
599 EXPECT_FALSE(adapter_
->IsDiscoverable());
602 TEST_F(BluetoothChromeOSTest
, SetDiscoverableWhenNotPresent
) {
604 ASSERT_TRUE(adapter_
->IsPresent());
605 ASSERT_FALSE(adapter_
->IsDiscoverable());
607 // Install an observer; expect the AdapterDiscoverableChanged to be called
608 // with true, and IsDiscoverable() to return true.
609 TestBluetoothAdapterObserver
observer(adapter_
);
611 fake_bluetooth_adapter_client_
->SetVisible(false);
613 EXPECT_EQ(1, observer
.present_changed_count());
614 EXPECT_FALSE(observer
.last_present());
616 EXPECT_FALSE(adapter_
->IsPresent());
617 EXPECT_FALSE(adapter_
->IsDiscoverable());
619 adapter_
->SetDiscoverable(true, GetCallback(), GetErrorCallback());
620 EXPECT_EQ(0, callback_count_
);
621 EXPECT_EQ(1, error_callback_count_
);
623 EXPECT_EQ(0, observer
.discoverable_changed_count());
625 EXPECT_FALSE(adapter_
->IsDiscoverable());
628 TEST_F(BluetoothChromeOSTest
, StopDiscovery
) {
631 adapter_
->SetPowered(true, GetCallback(), GetErrorCallback());
632 adapter_
->StartDiscoverySession(
633 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
634 base::Unretained(this)),
637 EXPECT_EQ(2, callback_count_
);
638 EXPECT_EQ(0, error_callback_count_
);
641 ASSERT_TRUE(adapter_
->IsPowered());
642 ASSERT_TRUE(adapter_
->IsDiscovering());
643 ASSERT_EQ((size_t)1, discovery_sessions_
.size());
644 ASSERT_TRUE(discovery_sessions_
[0]->IsActive());
646 // Install an observer; aside from the callback, expect the
647 // AdapterDiscoveringChanged method to be called and no longer to be
649 TestBluetoothAdapterObserver
observer(adapter_
);
651 discovery_sessions_
[0]->Stop(GetCallback(), GetErrorCallback());
653 EXPECT_EQ(1, callback_count_
);
654 EXPECT_EQ(0, error_callback_count_
);
656 EXPECT_EQ(1, observer
.discovering_changed_count());
657 EXPECT_FALSE(observer
.last_discovering());
659 EXPECT_FALSE(adapter_
->IsDiscovering());
660 discovery_sessions_
.clear();
663 // Test that the Stop callbacks get called even if the
664 // BluetoothDiscoverySession objects gets deleted
665 adapter_
->SetPowered(true, GetCallback(), GetErrorCallback());
666 adapter_
->StartDiscoverySession(
667 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
668 base::Unretained(this)),
671 EXPECT_EQ(2, callback_count_
);
672 EXPECT_EQ(0, error_callback_count_
);
674 ASSERT_TRUE(adapter_
->IsPowered());
675 ASSERT_TRUE(adapter_
->IsDiscovering());
676 ASSERT_EQ((size_t)1, discovery_sessions_
.size());
677 ASSERT_TRUE(discovery_sessions_
[0]->IsActive());
679 discovery_sessions_
[0]->Stop(GetCallback(), GetErrorCallback());
680 discovery_sessions_
.clear();
683 EXPECT_EQ(1, callback_count_
);
684 EXPECT_EQ(0, error_callback_count_
);
687 TEST_F(BluetoothChromeOSTest
, Discovery
) {
688 // Test a simulated discovery session.
689 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
692 TestBluetoothAdapterObserver
observer(adapter_
);
694 adapter_
->SetPowered(true, GetCallback(), GetErrorCallback());
695 adapter_
->StartDiscoverySession(
696 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
697 base::Unretained(this)),
700 EXPECT_EQ(2, callback_count_
);
701 EXPECT_EQ(0, error_callback_count_
);
704 ASSERT_TRUE(adapter_
->IsPowered());
705 ASSERT_TRUE(adapter_
->IsDiscovering());
706 ASSERT_EQ((size_t)1, discovery_sessions_
.size());
707 ASSERT_TRUE(discovery_sessions_
[0]->IsActive());
709 // First two devices to appear.
712 EXPECT_EQ(2, observer
.device_added_count());
713 EXPECT_EQ(FakeBluetoothDeviceClient::kLowEnergyAddress
,
714 observer
.last_device_address());
716 // Next we should get another two devices...
718 EXPECT_EQ(4, observer
.device_added_count());
720 // Okay, let's run forward until a device is actually removed...
721 while (!observer
.device_removed_count())
724 EXPECT_EQ(1, observer
.device_removed_count());
725 EXPECT_EQ(FakeBluetoothDeviceClient::kVanishingDeviceAddress
,
726 observer
.last_device_address());
729 TEST_F(BluetoothChromeOSTest
, PoweredAndDiscovering
) {
731 adapter_
->SetPowered(true, GetCallback(), GetErrorCallback());
732 adapter_
->StartDiscoverySession(
733 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
734 base::Unretained(this)),
737 EXPECT_EQ(2, callback_count_
);
738 EXPECT_EQ(0, error_callback_count_
);
740 ASSERT_EQ((size_t)1, discovery_sessions_
.size());
741 ASSERT_TRUE(discovery_sessions_
[0]->IsActive());
743 // Stop the timers that the simulation uses
744 fake_bluetooth_device_client_
->EndDiscoverySimulation(
745 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
));
747 ASSERT_TRUE(adapter_
->IsPowered());
748 ASSERT_TRUE(adapter_
->IsDiscovering());
750 fake_bluetooth_adapter_client_
->SetVisible(false);
751 ASSERT_FALSE(adapter_
->IsPresent());
752 ASSERT_FALSE(discovery_sessions_
[0]->IsActive());
754 // Install an observer; expect the AdapterPresentChanged,
755 // AdapterPoweredChanged and AdapterDiscoveringChanged methods to be called
756 // with true, and IsPresent(), IsPowered() and IsDiscovering() to all
758 TestBluetoothAdapterObserver
observer(adapter_
);
760 fake_bluetooth_adapter_client_
->SetVisible(true);
762 EXPECT_EQ(1, observer
.present_changed_count());
763 EXPECT_TRUE(observer
.last_present());
764 EXPECT_TRUE(adapter_
->IsPresent());
766 EXPECT_EQ(1, observer
.powered_changed_count());
767 EXPECT_TRUE(observer
.last_powered());
768 EXPECT_TRUE(adapter_
->IsPowered());
770 EXPECT_EQ(1, observer
.discovering_changed_count());
771 EXPECT_TRUE(observer
.last_discovering());
772 EXPECT_TRUE(adapter_
->IsDiscovering());
776 // Now mark the adapter not present again. Expect the methods to be called
777 // again, to reset the properties back to false
778 fake_bluetooth_adapter_client_
->SetVisible(false);
780 EXPECT_EQ(1, observer
.present_changed_count());
781 EXPECT_FALSE(observer
.last_present());
782 EXPECT_FALSE(adapter_
->IsPresent());
784 EXPECT_EQ(1, observer
.powered_changed_count());
785 EXPECT_FALSE(observer
.last_powered());
786 EXPECT_FALSE(adapter_
->IsPowered());
788 EXPECT_EQ(1, observer
.discovering_changed_count());
789 EXPECT_FALSE(observer
.last_discovering());
790 EXPECT_FALSE(adapter_
->IsDiscovering());
793 // This unit test asserts that the basic reference counting logic works
794 // correctly for discovery requests done via the BluetoothAdapter.
795 TEST_F(BluetoothChromeOSTest
, MultipleDiscoverySessions
) {
797 adapter_
->SetPowered(true, GetCallback(), GetErrorCallback());
798 EXPECT_EQ(1, callback_count_
);
799 EXPECT_EQ(0, error_callback_count_
);
800 EXPECT_TRUE(adapter_
->IsPowered());
803 TestBluetoothAdapterObserver
observer(adapter_
);
805 EXPECT_EQ(0, observer
.discovering_changed_count());
806 EXPECT_FALSE(observer
.last_discovering());
807 EXPECT_FALSE(adapter_
->IsDiscovering());
809 // Request device discovery 3 times.
810 for (int i
= 0; i
< 3; i
++) {
811 adapter_
->StartDiscoverySession(
812 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
813 base::Unretained(this)),
816 // Run only once, as there should have been one D-Bus call.
819 // The observer should have received the discovering changed event exactly
820 // once, the success callback should have been called 3 times and the adapter
821 // should be discovering.
822 EXPECT_EQ(1, observer
.discovering_changed_count());
823 EXPECT_EQ(3, callback_count_
);
824 EXPECT_EQ(0, error_callback_count_
);
825 EXPECT_TRUE(observer
.last_discovering());
826 EXPECT_TRUE(adapter_
->IsDiscovering());
827 ASSERT_EQ((size_t)3, discovery_sessions_
.size());
829 // Request to stop discovery twice.
830 for (int i
= 0; i
< 2; i
++) {
831 discovery_sessions_
[i
]->Stop(GetCallback(), GetErrorCallback());
834 // The observer should have received no additional discovering changed events,
835 // the success callback should have been called 2 times and the adapter should
836 // still be discovering.
837 EXPECT_EQ(1, observer
.discovering_changed_count());
838 EXPECT_EQ(5, callback_count_
);
839 EXPECT_EQ(0, error_callback_count_
);
840 EXPECT_TRUE(observer
.last_discovering());
841 EXPECT_TRUE(adapter_
->IsDiscovering());
842 EXPECT_TRUE(adapter_
->IsDiscovering());
843 EXPECT_FALSE(discovery_sessions_
[0]->IsActive());
844 EXPECT_FALSE(discovery_sessions_
[1]->IsActive());
845 EXPECT_TRUE(discovery_sessions_
[2]->IsActive());
847 // Request device discovery 3 times.
848 for (int i
= 0; i
< 3; i
++) {
849 adapter_
->StartDiscoverySession(
850 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
851 base::Unretained(this)),
855 // The observer should have received no additional discovering changed events,
856 // the success callback should have been called 3 times and the adapter should
857 // still be discovering.
858 EXPECT_EQ(1, observer
.discovering_changed_count());
859 EXPECT_EQ(8, callback_count_
);
860 EXPECT_EQ(0, error_callback_count_
);
861 EXPECT_TRUE(observer
.last_discovering());
862 EXPECT_TRUE(adapter_
->IsDiscovering());
863 ASSERT_EQ((size_t)6, discovery_sessions_
.size());
865 // Request to stop discovery 4 times.
866 for (int i
= 2; i
< 6; i
++) {
867 discovery_sessions_
[i
]->Stop(GetCallback(), GetErrorCallback());
869 // Run only once, as there should have been one D-Bus call.
872 // The observer should have received the discovering changed event exactly
873 // once, the success callback should have been called 4 times and the adapter
874 // should no longer be discovering.
875 EXPECT_EQ(2, observer
.discovering_changed_count());
876 EXPECT_EQ(12, callback_count_
);
877 EXPECT_EQ(0, error_callback_count_
);
878 EXPECT_FALSE(observer
.last_discovering());
879 EXPECT_FALSE(adapter_
->IsDiscovering());
881 // All discovery sessions should be inactive.
882 for (int i
= 0; i
< 6; i
++)
883 EXPECT_FALSE(discovery_sessions_
[i
]->IsActive());
885 // Request to stop discovery on of the inactive sessions.
886 discovery_sessions_
[0]->Stop(GetCallback(), GetErrorCallback());
888 // The call should have failed.
889 EXPECT_EQ(2, observer
.discovering_changed_count());
890 EXPECT_EQ(12, callback_count_
);
891 EXPECT_EQ(1, error_callback_count_
);
892 EXPECT_FALSE(observer
.last_discovering());
893 EXPECT_FALSE(adapter_
->IsDiscovering());
896 // This unit test asserts that the reference counting logic works correctly in
897 // the cases when the adapter gets reset and D-Bus calls are made outside of
898 // the BluetoothAdapter.
899 TEST_F(BluetoothChromeOSTest
,
900 UnexpectedChangesDuringMultipleDiscoverySessions
) {
902 adapter_
->SetPowered(true, GetCallback(), GetErrorCallback());
903 EXPECT_EQ(1, callback_count_
);
904 EXPECT_EQ(0, error_callback_count_
);
905 EXPECT_TRUE(adapter_
->IsPowered());
908 TestBluetoothAdapterObserver
observer(adapter_
);
910 EXPECT_EQ(0, observer
.discovering_changed_count());
911 EXPECT_FALSE(observer
.last_discovering());
912 EXPECT_FALSE(adapter_
->IsDiscovering());
914 // Request device discovery 3 times.
915 for (int i
= 0; i
< 3; i
++) {
916 adapter_
->StartDiscoverySession(
917 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
918 base::Unretained(this)),
921 // Run only once, as there should have been one D-Bus call.
924 // The observer should have received the discovering changed event exactly
925 // once, the success callback should have been called 3 times and the adapter
926 // should be discovering.
927 EXPECT_EQ(1, observer
.discovering_changed_count());
928 EXPECT_EQ(3, callback_count_
);
929 EXPECT_EQ(0, error_callback_count_
);
930 EXPECT_TRUE(observer
.last_discovering());
931 EXPECT_TRUE(adapter_
->IsDiscovering());
932 ASSERT_EQ((size_t)3, discovery_sessions_
.size());
934 for (int i
= 0; i
< 3; i
++)
935 EXPECT_TRUE(discovery_sessions_
[i
]->IsActive());
937 // Stop the timers that the simulation uses
938 fake_bluetooth_device_client_
->EndDiscoverySimulation(
939 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
));
941 ASSERT_TRUE(adapter_
->IsPowered());
942 ASSERT_TRUE(adapter_
->IsDiscovering());
944 // Stop device discovery behind the adapter. The adapter and the observer
945 // should be notified of the change and the reference count should be reset.
946 // Even though FakeBluetoothAdapterClient does its own reference counting and
947 // we called 3 BluetoothAdapter::StartDiscoverySession 3 times, the
948 // FakeBluetoothAdapterClient's count should be only 1 and a single call to
949 // FakeBluetoothAdapterClient::StopDiscovery should work.
950 fake_bluetooth_adapter_client_
->StopDiscovery(
951 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
), GetCallback(),
952 base::Bind(&BluetoothChromeOSTest::DBusErrorCallback
,
953 base::Unretained(this)));
955 EXPECT_EQ(2, observer
.discovering_changed_count());
956 EXPECT_EQ(4, callback_count_
);
957 EXPECT_EQ(0, error_callback_count_
);
958 EXPECT_FALSE(observer
.last_discovering());
959 EXPECT_FALSE(adapter_
->IsDiscovering());
961 // All discovery session instances should have been updated.
962 for (int i
= 0; i
< 3; i
++)
963 EXPECT_FALSE(discovery_sessions_
[i
]->IsActive());
964 discovery_sessions_
.clear();
966 // It should be possible to successfully start discovery.
967 for (int i
= 0; i
< 2; i
++) {
968 adapter_
->StartDiscoverySession(
969 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
970 base::Unretained(this)),
973 // Run only once, as there should have been one D-Bus call.
975 EXPECT_EQ(3, observer
.discovering_changed_count());
976 EXPECT_EQ(6, callback_count_
);
977 EXPECT_EQ(0, error_callback_count_
);
978 EXPECT_TRUE(observer
.last_discovering());
979 EXPECT_TRUE(adapter_
->IsDiscovering());
980 ASSERT_EQ((size_t)2, discovery_sessions_
.size());
982 for (int i
= 0; i
< 2; i
++)
983 EXPECT_TRUE(discovery_sessions_
[i
]->IsActive());
985 fake_bluetooth_device_client_
->EndDiscoverySimulation(
986 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
));
988 // Make the adapter disappear and appear. This will make it come back as
989 // discovering. When this happens, the reference count should become and
990 // remain 0 as no new request was made through the BluetoothAdapter.
991 fake_bluetooth_adapter_client_
->SetVisible(false);
992 ASSERT_FALSE(adapter_
->IsPresent());
993 EXPECT_EQ(4, observer
.discovering_changed_count());
994 EXPECT_EQ(6, callback_count_
);
995 EXPECT_EQ(0, error_callback_count_
);
996 EXPECT_FALSE(observer
.last_discovering());
997 EXPECT_FALSE(adapter_
->IsDiscovering());
999 for (int i
= 0; i
< 2; i
++)
1000 EXPECT_FALSE(discovery_sessions_
[i
]->IsActive());
1001 discovery_sessions_
.clear();
1003 fake_bluetooth_adapter_client_
->SetVisible(true);
1004 ASSERT_TRUE(adapter_
->IsPresent());
1005 EXPECT_EQ(5, observer
.discovering_changed_count());
1006 EXPECT_EQ(6, callback_count_
);
1007 EXPECT_EQ(0, error_callback_count_
);
1008 EXPECT_TRUE(observer
.last_discovering());
1009 EXPECT_TRUE(adapter_
->IsDiscovering());
1011 // Start and stop discovery. At this point, FakeBluetoothAdapterClient has
1012 // a reference count that is equal to 1. Pretend that this was done by an
1013 // application other than us. Starting and stopping discovery will succeed
1014 // but it won't cause the discovery state to change.
1015 adapter_
->StartDiscoverySession(
1016 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
1017 base::Unretained(this)),
1018 GetErrorCallback());
1019 message_loop_
.Run(); // Run the loop, as there should have been a D-Bus call.
1020 EXPECT_EQ(5, observer
.discovering_changed_count());
1021 EXPECT_EQ(7, callback_count_
);
1022 EXPECT_EQ(0, error_callback_count_
);
1023 EXPECT_TRUE(observer
.last_discovering());
1024 EXPECT_TRUE(adapter_
->IsDiscovering());
1025 ASSERT_EQ((size_t)1, discovery_sessions_
.size());
1026 EXPECT_TRUE(discovery_sessions_
[0]->IsActive());
1028 discovery_sessions_
[0]->Stop(GetCallback(), GetErrorCallback());
1029 message_loop_
.Run(); // Run the loop, as there should have been a D-Bus call.
1030 EXPECT_EQ(5, observer
.discovering_changed_count());
1031 EXPECT_EQ(8, callback_count_
);
1032 EXPECT_EQ(0, error_callback_count_
);
1033 EXPECT_TRUE(observer
.last_discovering());
1034 EXPECT_TRUE(adapter_
->IsDiscovering());
1035 EXPECT_FALSE(discovery_sessions_
[0]->IsActive());
1036 discovery_sessions_
.clear();
1038 // Start discovery again.
1039 adapter_
->StartDiscoverySession(
1040 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
1041 base::Unretained(this)),
1042 GetErrorCallback());
1043 message_loop_
.Run(); // Run the loop, as there should have been a D-Bus call.
1044 EXPECT_EQ(5, observer
.discovering_changed_count());
1045 EXPECT_EQ(9, callback_count_
);
1046 EXPECT_EQ(0, error_callback_count_
);
1047 EXPECT_TRUE(observer
.last_discovering());
1048 EXPECT_TRUE(adapter_
->IsDiscovering());
1049 ASSERT_EQ((size_t)1, discovery_sessions_
.size());
1050 EXPECT_TRUE(discovery_sessions_
[0]->IsActive());
1052 // Stop discovery via D-Bus. The fake client's reference count will drop but
1053 // the discovery state won't change since our BluetoothAdapter also just
1054 // requested it via D-Bus.
1055 fake_bluetooth_adapter_client_
->StopDiscovery(
1056 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
), GetCallback(),
1057 base::Bind(&BluetoothChromeOSTest::DBusErrorCallback
,
1058 base::Unretained(this)));
1059 message_loop_
.Run();
1060 EXPECT_EQ(5, observer
.discovering_changed_count());
1061 EXPECT_EQ(10, callback_count_
);
1062 EXPECT_EQ(0, error_callback_count_
);
1063 EXPECT_TRUE(observer
.last_discovering());
1064 EXPECT_TRUE(adapter_
->IsDiscovering());
1066 // Now end the discovery session. This should change the adapter's discovery
1068 discovery_sessions_
[0]->Stop(GetCallback(), GetErrorCallback());
1069 message_loop_
.Run();
1070 EXPECT_EQ(6, observer
.discovering_changed_count());
1071 EXPECT_EQ(11, callback_count_
);
1072 EXPECT_EQ(0, error_callback_count_
);
1073 EXPECT_FALSE(observer
.last_discovering());
1074 EXPECT_FALSE(adapter_
->IsDiscovering());
1075 EXPECT_FALSE(discovery_sessions_
[0]->IsActive());
1078 TEST_F(BluetoothChromeOSTest
, InvalidatedDiscoverySessions
) {
1080 adapter_
->SetPowered(true, GetCallback(), GetErrorCallback());
1081 EXPECT_EQ(1, callback_count_
);
1082 EXPECT_EQ(0, error_callback_count_
);
1083 EXPECT_TRUE(adapter_
->IsPowered());
1084 callback_count_
= 0;
1086 TestBluetoothAdapterObserver
observer(adapter_
);
1088 EXPECT_EQ(0, observer
.discovering_changed_count());
1089 EXPECT_FALSE(observer
.last_discovering());
1090 EXPECT_FALSE(adapter_
->IsDiscovering());
1092 // Request device discovery 3 times.
1093 for (int i
= 0; i
< 3; i
++) {
1094 adapter_
->StartDiscoverySession(
1095 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
1096 base::Unretained(this)),
1097 GetErrorCallback());
1099 // Run only once, as there should have been one D-Bus call.
1100 message_loop_
.Run();
1102 // The observer should have received the discovering changed event exactly
1103 // once, the success callback should have been called 3 times and the adapter
1104 // should be discovering.
1105 EXPECT_EQ(1, observer
.discovering_changed_count());
1106 EXPECT_EQ(3, callback_count_
);
1107 EXPECT_EQ(0, error_callback_count_
);
1108 EXPECT_TRUE(observer
.last_discovering());
1109 EXPECT_TRUE(adapter_
->IsDiscovering());
1110 ASSERT_EQ((size_t)3, discovery_sessions_
.size());
1112 for (int i
= 0; i
< 3; i
++)
1113 EXPECT_TRUE(discovery_sessions_
[i
]->IsActive());
1115 // Stop the timers that the simulation uses
1116 fake_bluetooth_device_client_
->EndDiscoverySimulation(
1117 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
));
1119 ASSERT_TRUE(adapter_
->IsPowered());
1120 ASSERT_TRUE(adapter_
->IsDiscovering());
1122 // Delete all but one discovery session.
1123 discovery_sessions_
.pop_back();
1124 discovery_sessions_
.pop_back();
1125 ASSERT_EQ((size_t)1, discovery_sessions_
.size());
1126 EXPECT_TRUE(discovery_sessions_
[0]->IsActive());
1127 EXPECT_TRUE(adapter_
->IsDiscovering());
1129 // Stop device discovery behind the adapter. The one active discovery session
1130 // should become inactive, but more importantly, we shouldn't run into any
1131 // memory errors as the sessions that we explicitly deleted should get
1133 fake_bluetooth_adapter_client_
->StopDiscovery(
1134 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
), GetCallback(),
1135 base::Bind(&BluetoothChromeOSTest::DBusErrorCallback
,
1136 base::Unretained(this)));
1137 message_loop_
.Run();
1138 EXPECT_EQ(2, observer
.discovering_changed_count());
1139 EXPECT_EQ(4, callback_count_
);
1140 EXPECT_EQ(0, error_callback_count_
);
1141 EXPECT_FALSE(observer
.last_discovering());
1142 EXPECT_FALSE(adapter_
->IsDiscovering());
1143 EXPECT_FALSE(discovery_sessions_
[0]->IsActive());
1146 TEST_F(BluetoothChromeOSTest
, QueuedDiscoveryRequests
) {
1149 adapter_
->SetPowered(true, GetCallback(), GetErrorCallback());
1150 EXPECT_EQ(1, callback_count_
);
1151 EXPECT_EQ(0, error_callback_count_
);
1152 EXPECT_TRUE(adapter_
->IsPowered());
1153 callback_count_
= 0;
1155 TestBluetoothAdapterObserver
observer(adapter_
);
1157 EXPECT_EQ(0, observer
.discovering_changed_count());
1158 EXPECT_FALSE(observer
.last_discovering());
1159 EXPECT_FALSE(adapter_
->IsDiscovering());
1161 // Request to start discovery. The call should be pending.
1162 adapter_
->StartDiscoverySession(
1163 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
1164 base::Unretained(this)),
1165 GetErrorCallback());
1166 EXPECT_EQ(0, callback_count_
);
1168 fake_bluetooth_device_client_
->EndDiscoverySimulation(
1169 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
));
1171 // The underlying adapter has started discovery, but our call hasn't returned
1173 EXPECT_EQ(1, observer
.discovering_changed_count());
1174 EXPECT_TRUE(observer
.last_discovering());
1175 EXPECT_TRUE(adapter_
->IsDiscovering());
1176 EXPECT_TRUE(discovery_sessions_
.empty());
1178 // Request to start discovery twice. These should get queued and there should
1179 // be no change in state.
1180 for (int i
= 0; i
< 2; i
++) {
1181 adapter_
->StartDiscoverySession(
1182 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
1183 base::Unretained(this)),
1184 GetErrorCallback());
1186 EXPECT_EQ(0, callback_count_
);
1187 EXPECT_EQ(0, error_callback_count_
);
1188 EXPECT_EQ(1, observer
.discovering_changed_count());
1189 EXPECT_TRUE(observer
.last_discovering());
1190 EXPECT_TRUE(adapter_
->IsDiscovering());
1191 EXPECT_TRUE(discovery_sessions_
.empty());
1193 // Process the pending call. The queued calls should execute and the discovery
1194 // session reference count should increase.
1195 message_loop_
.Run();
1196 EXPECT_EQ(3, callback_count_
);
1197 EXPECT_EQ(0, error_callback_count_
);
1198 EXPECT_EQ(1, observer
.discovering_changed_count());
1199 EXPECT_TRUE(observer
.last_discovering());
1200 EXPECT_TRUE(adapter_
->IsDiscovering());
1201 ASSERT_EQ((size_t)3, discovery_sessions_
.size());
1203 // Verify the reference count by removing sessions 3 times. The last request
1204 // should remain pending.
1205 for (int i
= 0; i
< 3; i
++) {
1206 discovery_sessions_
[i
]->Stop(GetCallback(), GetErrorCallback());
1208 EXPECT_EQ(5, callback_count_
);
1209 EXPECT_EQ(0, error_callback_count_
);
1210 EXPECT_EQ(2, observer
.discovering_changed_count());
1211 EXPECT_FALSE(observer
.last_discovering());
1212 EXPECT_FALSE(adapter_
->IsDiscovering());
1213 EXPECT_FALSE(discovery_sessions_
[0]->IsActive());
1214 EXPECT_FALSE(discovery_sessions_
[1]->IsActive());
1215 EXPECT_TRUE(discovery_sessions_
[2]->IsActive());
1217 // Request to stop the session whose call is pending should fail.
1218 discovery_sessions_
[2]->Stop(GetCallback(), GetErrorCallback());
1219 EXPECT_EQ(5, callback_count_
);
1220 EXPECT_EQ(1, error_callback_count_
);
1221 EXPECT_EQ(2, observer
.discovering_changed_count());
1222 EXPECT_FALSE(observer
.last_discovering());
1223 EXPECT_FALSE(adapter_
->IsDiscovering());
1224 EXPECT_TRUE(discovery_sessions_
[2]->IsActive());
1226 // Request to start should get queued.
1227 adapter_
->StartDiscoverySession(
1228 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
1229 base::Unretained(this)),
1230 GetErrorCallback());
1231 EXPECT_EQ(5, callback_count_
);
1232 EXPECT_EQ(1, error_callback_count_
);
1233 EXPECT_EQ(2, observer
.discovering_changed_count());
1234 EXPECT_FALSE(observer
.last_discovering());
1235 EXPECT_FALSE(adapter_
->IsDiscovering());
1236 ASSERT_EQ((size_t)3, discovery_sessions_
.size());
1238 // Run the pending request.
1239 message_loop_
.Run();
1240 EXPECT_EQ(6, callback_count_
);
1241 EXPECT_EQ(1, error_callback_count_
);
1242 EXPECT_EQ(3, observer
.discovering_changed_count());
1243 EXPECT_TRUE(observer
.last_discovering());
1244 EXPECT_TRUE(adapter_
->IsDiscovering());
1245 ASSERT_EQ((size_t)3, discovery_sessions_
.size());
1246 EXPECT_FALSE(discovery_sessions_
[2]->IsActive());
1248 // The queued request to start discovery should have been issued but is still
1249 // pending. Run the loop and verify.
1250 message_loop_
.Run();
1251 EXPECT_EQ(7, callback_count_
);
1252 EXPECT_EQ(1, error_callback_count_
);
1253 EXPECT_EQ(3, observer
.discovering_changed_count());
1254 EXPECT_TRUE(observer
.last_discovering());
1255 EXPECT_TRUE(adapter_
->IsDiscovering());
1256 ASSERT_EQ((size_t)4, discovery_sessions_
.size());
1257 EXPECT_TRUE(discovery_sessions_
[3]->IsActive());
1260 TEST_F(BluetoothChromeOSTest
, StartDiscoverySession
) {
1263 adapter_
->SetPowered(true, GetCallback(), GetErrorCallback());
1264 EXPECT_EQ(1, callback_count_
);
1265 EXPECT_EQ(0, error_callback_count_
);
1266 EXPECT_TRUE(adapter_
->IsPowered());
1267 callback_count_
= 0;
1269 TestBluetoothAdapterObserver
observer(adapter_
);
1271 EXPECT_EQ(0, observer
.discovering_changed_count());
1272 EXPECT_FALSE(observer
.last_discovering());
1273 EXPECT_FALSE(adapter_
->IsDiscovering());
1274 EXPECT_TRUE(discovery_sessions_
.empty());
1276 // Request a new discovery session.
1277 adapter_
->StartDiscoverySession(
1278 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
1279 base::Unretained(this)),
1280 GetErrorCallback());
1281 message_loop_
.Run();
1282 EXPECT_EQ(1, observer
.discovering_changed_count());
1283 EXPECT_EQ(1, callback_count_
);
1284 EXPECT_EQ(0, error_callback_count_
);
1285 EXPECT_TRUE(observer
.last_discovering());
1286 EXPECT_TRUE(adapter_
->IsDiscovering());
1287 ASSERT_EQ((size_t)1, discovery_sessions_
.size());
1288 EXPECT_TRUE(discovery_sessions_
[0]->IsActive());
1290 // Start another session. A new one should be returned in the callback, which
1291 // in turn will destroy the previous session. Adapter should still be
1292 // discovering and the reference count should be 1.
1293 adapter_
->StartDiscoverySession(
1294 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
1295 base::Unretained(this)),
1296 GetErrorCallback());
1297 message_loop_
.Run();
1298 EXPECT_EQ(1, observer
.discovering_changed_count());
1299 EXPECT_EQ(2, callback_count_
);
1300 EXPECT_EQ(0, error_callback_count_
);
1301 EXPECT_TRUE(observer
.last_discovering());
1302 EXPECT_TRUE(adapter_
->IsDiscovering());
1303 ASSERT_EQ((size_t)2, discovery_sessions_
.size());
1304 EXPECT_TRUE(discovery_sessions_
[0]->IsActive());
1306 // Request a new session.
1307 adapter_
->StartDiscoverySession(
1308 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
1309 base::Unretained(this)),
1310 GetErrorCallback());
1311 message_loop_
.Run();
1312 EXPECT_EQ(1, observer
.discovering_changed_count());
1313 EXPECT_EQ(3, callback_count_
);
1314 EXPECT_EQ(0, error_callback_count_
);
1315 EXPECT_TRUE(observer
.last_discovering());
1316 EXPECT_TRUE(adapter_
->IsDiscovering());
1317 ASSERT_EQ((size_t)3, discovery_sessions_
.size());
1318 EXPECT_TRUE(discovery_sessions_
[1]->IsActive());
1319 EXPECT_NE(discovery_sessions_
[0], discovery_sessions_
[1]);
1321 // Stop the previous discovery session. The session should end but discovery
1323 discovery_sessions_
[0]->Stop(GetCallback(), GetErrorCallback());
1324 message_loop_
.Run();
1325 EXPECT_EQ(1, observer
.discovering_changed_count());
1326 EXPECT_EQ(4, callback_count_
);
1327 EXPECT_EQ(0, error_callback_count_
);
1328 EXPECT_TRUE(observer
.last_discovering());
1329 EXPECT_TRUE(adapter_
->IsDiscovering());
1330 ASSERT_EQ((size_t)3, discovery_sessions_
.size());
1331 EXPECT_FALSE(discovery_sessions_
[0]->IsActive());
1332 EXPECT_TRUE(discovery_sessions_
[1]->IsActive());
1334 // Delete the current active session. Discovery should eventually stop.
1335 discovery_sessions_
.clear();
1336 while (observer
.last_discovering())
1337 message_loop_
.RunUntilIdle();
1339 EXPECT_EQ(2, observer
.discovering_changed_count());
1340 EXPECT_EQ(4, callback_count_
);
1341 EXPECT_EQ(0, error_callback_count_
);
1342 EXPECT_FALSE(observer
.last_discovering());
1343 EXPECT_FALSE(adapter_
->IsDiscovering());
1346 TEST_F(BluetoothChromeOSTest
, SetDiscoveryFilterBeforeStartDiscovery
) {
1347 // Test a simulated discovery session.
1348 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
1351 TestBluetoothAdapterObserver
observer(adapter_
);
1353 BluetoothDiscoveryFilter
* df
= new BluetoothDiscoveryFilter(
1354 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE
);
1356 df
->AddUUID(BluetoothUUID("1000"));
1357 scoped_ptr
<BluetoothDiscoveryFilter
> discovery_filter(df
);
1359 adapter_
->SetPowered(true, base::Bind(&BluetoothChromeOSTest::Callback
,
1360 base::Unretained(this)),
1361 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1362 base::Unretained(this)));
1363 adapter_
->StartDiscoverySessionWithFilter(
1364 discovery_filter
.Pass(),
1365 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
1366 base::Unretained(this)),
1367 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1368 base::Unretained(this)));
1369 message_loop_
.Run();
1370 EXPECT_EQ(2, callback_count_
);
1371 EXPECT_EQ(0, error_callback_count_
);
1372 callback_count_
= 0;
1374 ASSERT_TRUE(adapter_
->IsPowered());
1375 ASSERT_TRUE(adapter_
->IsDiscovering());
1376 ASSERT_EQ((size_t)1, discovery_sessions_
.size());
1377 ASSERT_TRUE(discovery_sessions_
[0]->IsActive());
1378 ASSERT_TRUE(df
->Equals(*discovery_sessions_
[0]->GetDiscoveryFilter()));
1380 auto filter
= fake_bluetooth_adapter_client_
->GetDiscoveryFilter();
1381 EXPECT_NE(nullptr, filter
);
1382 EXPECT_EQ("le", *filter
->transport
);
1383 EXPECT_EQ(-60, *filter
->rssi
);
1384 EXPECT_EQ(nullptr, filter
->pathloss
.get());
1385 std::vector
<std::string
> uuids
= *filter
->uuids
;
1386 EXPECT_NE(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1000"));
1388 discovery_sessions_
[0]->Stop(
1389 base::Bind(&BluetoothChromeOSTest::Callback
, base::Unretained(this)),
1390 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1391 base::Unretained(this)));
1393 message_loop_
.Run();
1395 EXPECT_EQ(1, callback_count_
);
1396 EXPECT_EQ(0, error_callback_count_
);
1398 ASSERT_TRUE(adapter_
->IsPowered());
1399 ASSERT_FALSE(adapter_
->IsDiscovering());
1400 ASSERT_EQ((size_t)1, discovery_sessions_
.size());
1401 ASSERT_FALSE(discovery_sessions_
[0]->IsActive());
1402 ASSERT_EQ(discovery_sessions_
[0]->GetDiscoveryFilter(),
1403 (BluetoothDiscoveryFilter
*)nullptr);
1405 filter
= fake_bluetooth_adapter_client_
->GetDiscoveryFilter();
1406 EXPECT_EQ(nullptr, filter
);
1409 TEST_F(BluetoothChromeOSTest
, SetDiscoveryFilterBeforeStartDiscoveryFail
) {
1410 // Test a simulated discovery session.
1411 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
1414 TestBluetoothAdapterObserver
observer(adapter_
);
1416 BluetoothDiscoveryFilter
* df
= new BluetoothDiscoveryFilter(
1417 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE
);
1419 df
->AddUUID(BluetoothUUID("1000"));
1420 scoped_ptr
<BluetoothDiscoveryFilter
> discovery_filter(df
);
1422 adapter_
->SetPowered(true, base::Bind(&BluetoothChromeOSTest::Callback
,
1423 base::Unretained(this)),
1424 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1425 base::Unretained(this)));
1426 EXPECT_EQ(1, callback_count_
);
1427 callback_count_
= 0;
1429 fake_bluetooth_adapter_client_
->MakeSetDiscoveryFilterFail();
1431 adapter_
->StartDiscoverySessionWithFilter(
1432 discovery_filter
.Pass(),
1433 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
1434 base::Unretained(this)),
1435 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1436 base::Unretained(this)));
1438 message_loop_
.Run();
1440 EXPECT_EQ(1, error_callback_count_
);
1441 error_callback_count_
= 0;
1443 ASSERT_TRUE(adapter_
->IsPowered());
1444 ASSERT_FALSE(adapter_
->IsDiscovering());
1445 ASSERT_EQ((size_t)0, discovery_sessions_
.size());
1447 auto filter
= fake_bluetooth_adapter_client_
->GetDiscoveryFilter();
1448 EXPECT_EQ(nullptr, filter
);
1451 // This test queues two requests to StartDiscovery with pre set filter. This
1452 // should result in SetDiscoveryFilter, then StartDiscovery, and SetDiscovery
1454 TEST_F(BluetoothChromeOSTest
, QueuedSetDiscoveryFilterBeforeStartDiscovery
) {
1455 // Test a simulated discovery session.
1456 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
1459 TestBluetoothAdapterObserver
observer(adapter_
);
1461 BluetoothDiscoveryFilter
* df
= new BluetoothDiscoveryFilter(
1462 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE
);
1464 df
->AddUUID(BluetoothUUID("1000"));
1465 scoped_ptr
<BluetoothDiscoveryFilter
> discovery_filter(df
);
1467 BluetoothDiscoveryFilter
* df2
= new BluetoothDiscoveryFilter(
1468 BluetoothDiscoveryFilter::Transport::TRANSPORT_CLASSIC
);
1470 df2
->AddUUID(BluetoothUUID("1002"));
1471 scoped_ptr
<BluetoothDiscoveryFilter
> discovery_filter2(df2
);
1473 adapter_
->SetPowered(true, base::Bind(&BluetoothChromeOSTest::Callback
,
1474 base::Unretained(this)),
1475 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1476 base::Unretained(this)));
1478 EXPECT_EQ(1, callback_count_
);
1479 EXPECT_EQ(0, error_callback_count_
);
1480 callback_count_
= 0;
1482 // Queue two requests to start discovery session with filter.
1483 adapter_
->StartDiscoverySessionWithFilter(
1484 discovery_filter
.Pass(),
1485 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
1486 base::Unretained(this)),
1487 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1488 base::Unretained(this)));
1490 adapter_
->StartDiscoverySessionWithFilter(
1491 discovery_filter2
.Pass(),
1492 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
1493 base::Unretained(this)),
1494 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1495 base::Unretained(this)));
1497 // Run requests, on DBus level there should be call SetDiscoveryFilter, then
1498 // StartDiscovery, then SetDiscoveryFilter again.
1499 message_loop_
.Run();
1500 message_loop_
.Run();
1502 EXPECT_EQ(2, callback_count_
);
1503 EXPECT_EQ(0, error_callback_count_
);
1504 callback_count_
= 0;
1506 ASSERT_TRUE(adapter_
->IsPowered());
1507 ASSERT_TRUE(adapter_
->IsDiscovering());
1508 ASSERT_EQ((size_t)2, discovery_sessions_
.size());
1509 ASSERT_TRUE(discovery_sessions_
[0]->IsActive());
1510 ASSERT_TRUE(df
->Equals(*discovery_sessions_
[0]->GetDiscoveryFilter()));
1511 ASSERT_TRUE(discovery_sessions_
[1]->IsActive());
1512 ASSERT_TRUE(df2
->Equals(*discovery_sessions_
[1]->GetDiscoveryFilter()));
1514 auto filter
= fake_bluetooth_adapter_client_
->GetDiscoveryFilter();
1515 EXPECT_NE(nullptr, filter
);
1516 EXPECT_EQ("auto", *filter
->transport
);
1517 EXPECT_EQ(-65, *filter
->rssi
);
1518 EXPECT_EQ(nullptr, filter
->pathloss
.get());
1519 auto uuids
= *filter
->uuids
;
1520 EXPECT_NE(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1000"));
1521 EXPECT_NE(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1002"));
1523 discovery_sessions_
[0]->Stop(
1524 base::Bind(&BluetoothChromeOSTest::Callback
, base::Unretained(this)),
1525 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1526 base::Unretained(this)));
1528 discovery_sessions_
[1]->Stop(
1529 base::Bind(&BluetoothChromeOSTest::Callback
, base::Unretained(this)),
1530 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1531 base::Unretained(this)));
1533 message_loop_
.Run();
1535 EXPECT_EQ(2, callback_count_
);
1536 EXPECT_EQ(0, error_callback_count_
);
1538 ASSERT_TRUE(adapter_
->IsPowered());
1539 ASSERT_FALSE(adapter_
->IsDiscovering());
1540 ASSERT_FALSE(discovery_sessions_
[0]->IsActive());
1541 ASSERT_EQ(discovery_sessions_
[0]->GetDiscoveryFilter(),
1542 (BluetoothDiscoveryFilter
*)nullptr);
1543 ASSERT_FALSE(discovery_sessions_
[1]->IsActive());
1544 ASSERT_EQ(discovery_sessions_
[1]->GetDiscoveryFilter(),
1545 (BluetoothDiscoveryFilter
*)nullptr);
1547 filter
= fake_bluetooth_adapter_client_
->GetDiscoveryFilter();
1548 EXPECT_EQ(nullptr, filter
);
1551 // Call StartFilteredDiscovery twice (2nd time while 1st call is still pending).
1552 // Make the first SetDiscoveryFilter fail and the second one succeed. It should
1553 // end up with one active discovery session.
1554 TEST_F(BluetoothChromeOSTest
,
1555 QueuedSetDiscoveryFilterBeforeStartDiscoveryFail
) {
1556 // Test a simulated discovery session.
1557 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
1560 TestBluetoothAdapterObserver
observer(adapter_
);
1562 BluetoothDiscoveryFilter
* df
= new BluetoothDiscoveryFilter(
1563 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE
);
1565 df
->AddUUID(BluetoothUUID("1000"));
1566 scoped_ptr
<BluetoothDiscoveryFilter
> discovery_filter(df
);
1568 BluetoothDiscoveryFilter
* df2
= new BluetoothDiscoveryFilter(
1569 BluetoothDiscoveryFilter::Transport::TRANSPORT_CLASSIC
);
1571 df2
->AddUUID(BluetoothUUID("1002"));
1572 scoped_ptr
<BluetoothDiscoveryFilter
> discovery_filter2(df2
);
1574 adapter_
->SetPowered(true, base::Bind(&BluetoothChromeOSTest::Callback
,
1575 base::Unretained(this)),
1576 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1577 base::Unretained(this)));
1579 EXPECT_EQ(1, callback_count_
);
1580 EXPECT_EQ(0, error_callback_count_
);
1581 callback_count_
= 0;
1583 fake_bluetooth_adapter_client_
->MakeSetDiscoveryFilterFail();
1585 // Queue two requests to start discovery session with filter.
1586 adapter_
->StartDiscoverySessionWithFilter(
1587 discovery_filter
.Pass(),
1588 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
1589 base::Unretained(this)),
1590 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1591 base::Unretained(this)));
1593 adapter_
->StartDiscoverySessionWithFilter(
1594 discovery_filter2
.Pass(),
1595 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
1596 base::Unretained(this)),
1597 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1598 base::Unretained(this)));
1600 message_loop_
.Run();
1602 // First request to SetDiscoveryFilter should fail, resulting in no session
1604 EXPECT_EQ(0, callback_count_
);
1605 EXPECT_EQ(1, error_callback_count_
);
1606 error_callback_count_
= 0;
1608 ASSERT_TRUE(adapter_
->IsPowered());
1609 ASSERT_FALSE(adapter_
->IsDiscovering());
1610 ASSERT_EQ((size_t)0, discovery_sessions_
.size());
1612 message_loop_
.Run();
1614 // Second request should succeed
1615 EXPECT_EQ(1, callback_count_
);
1616 EXPECT_EQ(0, error_callback_count_
);
1617 callback_count_
= 0;
1619 ASSERT_TRUE(adapter_
->IsDiscovering());
1620 ASSERT_EQ((size_t)1, discovery_sessions_
.size());
1621 ASSERT_TRUE(discovery_sessions_
[0]->IsActive());
1622 ASSERT_TRUE(df2
->Equals(*discovery_sessions_
[0]->GetDiscoveryFilter()));
1624 auto filter
= fake_bluetooth_adapter_client_
->GetDiscoveryFilter();
1625 EXPECT_NE(nullptr, filter
);
1626 EXPECT_EQ("bredr", *filter
->transport
);
1627 EXPECT_EQ(-65, *filter
->rssi
);
1628 EXPECT_EQ(nullptr, filter
->pathloss
.get());
1629 auto uuids
= *filter
->uuids
;
1630 EXPECT_NE(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1002"));
1632 discovery_sessions_
[0]->Stop(
1633 base::Bind(&BluetoothChromeOSTest::Callback
, base::Unretained(this)),
1634 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1635 base::Unretained(this)));
1637 message_loop_
.Run();
1639 EXPECT_EQ(1, callback_count_
);
1640 EXPECT_EQ(0, error_callback_count_
);
1642 ASSERT_TRUE(adapter_
->IsPowered());
1643 ASSERT_FALSE(adapter_
->IsDiscovering());
1644 ASSERT_FALSE(discovery_sessions_
[0]->IsActive());
1645 ASSERT_EQ(discovery_sessions_
[0]->GetDiscoveryFilter(),
1646 (BluetoothDiscoveryFilter
*)nullptr);
1648 filter
= fake_bluetooth_adapter_client_
->GetDiscoveryFilter();
1649 EXPECT_EQ(nullptr, filter
);
1652 TEST_F(BluetoothChromeOSTest
, SetDiscoveryFilterAfterStartDiscovery
) {
1653 // Test a simulated discovery session.
1654 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
1657 TestBluetoothAdapterObserver
observer(adapter_
);
1659 adapter_
->SetPowered(true, base::Bind(&BluetoothChromeOSTest::Callback
,
1660 base::Unretained(this)),
1661 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1662 base::Unretained(this)));
1663 adapter_
->StartDiscoverySession(
1664 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
1665 base::Unretained(this)),
1666 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1667 base::Unretained(this)));
1668 message_loop_
.Run();
1669 EXPECT_EQ(2, callback_count_
);
1670 EXPECT_EQ(0, error_callback_count_
);
1671 callback_count_
= 0;
1673 ASSERT_TRUE(adapter_
->IsPowered());
1674 ASSERT_TRUE(adapter_
->IsDiscovering());
1675 ASSERT_EQ((size_t)1, discovery_sessions_
.size());
1676 ASSERT_TRUE(discovery_sessions_
[0]->IsActive());
1677 EXPECT_EQ(1, observer
.discovering_changed_count());
1680 auto null_instance
= scoped_ptr
<BluetoothDiscoveryFilter
>();
1681 null_instance
.reset();
1682 ASSERT_EQ(discovery_sessions_
[0]->GetDiscoveryFilter(), null_instance
.get());
1684 auto filter
= fake_bluetooth_adapter_client_
->GetDiscoveryFilter();
1685 EXPECT_EQ(nullptr, filter
);
1687 BluetoothDiscoveryFilter
* df
= new BluetoothDiscoveryFilter(
1688 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE
);
1690 df
->AddUUID(BluetoothUUID("1000"));
1691 scoped_ptr
<BluetoothDiscoveryFilter
> discovery_filter(df
);
1693 discovery_sessions_
[0]->SetDiscoveryFilter(
1694 discovery_filter
.Pass(),
1695 base::Bind(&BluetoothChromeOSTest::Callback
, base::Unretained(this)),
1696 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1697 base::Unretained(this)));
1699 message_loop_
.Run();
1700 EXPECT_EQ(1, callback_count_
);
1701 EXPECT_EQ(0, error_callback_count_
);
1702 callback_count_
= 0;
1704 ASSERT_TRUE(df
->Equals(*discovery_sessions_
[0]->GetDiscoveryFilter()));
1706 filter
= fake_bluetooth_adapter_client_
->GetDiscoveryFilter();
1707 EXPECT_NE(nullptr, filter
);
1708 EXPECT_EQ("le", *filter
->transport
);
1709 EXPECT_EQ(-60, *filter
->rssi
);
1710 EXPECT_EQ(nullptr, filter
->pathloss
.get());
1711 std::vector
<std::string
> uuids
= *filter
->uuids
;
1712 EXPECT_NE(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1000"));
1714 discovery_sessions_
[0]->Stop(
1715 base::Bind(&BluetoothChromeOSTest::Callback
, base::Unretained(this)),
1716 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1717 base::Unretained(this)));
1719 message_loop_
.Run();
1721 EXPECT_EQ(1, callback_count_
);
1722 EXPECT_EQ(0, error_callback_count_
);
1724 ASSERT_TRUE(adapter_
->IsPowered());
1725 ASSERT_FALSE(adapter_
->IsDiscovering());
1726 ASSERT_EQ((size_t)1, discovery_sessions_
.size());
1727 ASSERT_FALSE(discovery_sessions_
[0]->IsActive());
1728 ASSERT_EQ(discovery_sessions_
[0]->GetDiscoveryFilter(),
1729 (BluetoothDiscoveryFilter
*)nullptr);
1731 filter
= fake_bluetooth_adapter_client_
->GetDiscoveryFilter();
1732 EXPECT_EQ(nullptr, filter
);
1735 // This unit test asserts that the basic reference counting, and filter merging
1736 // works correctly for discovery requests done via the BluetoothAdapter.
1737 TEST_F(BluetoothChromeOSTest
, SetDiscoveryFilterBeforeStartDiscoveryMultiple
) {
1739 adapter_
->SetPowered(true, base::Bind(&BluetoothChromeOSTest::Callback
,
1740 base::Unretained(this)),
1741 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1742 base::Unretained(this)));
1743 EXPECT_EQ(1, callback_count_
);
1744 EXPECT_EQ(0, error_callback_count_
);
1745 EXPECT_TRUE(adapter_
->IsPowered());
1746 callback_count_
= 0;
1748 TestBluetoothAdapterObserver
observer(adapter_
);
1750 // Request device discovery with pre-set filter 3 times.
1751 for (int i
= 0; i
< 3; i
++) {
1752 scoped_ptr
<BluetoothDiscoveryFilter
> discovery_filter
;
1754 BluetoothDiscoveryFilter
* df
= new BluetoothDiscoveryFilter(
1755 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE
);
1757 df
->AddUUID(BluetoothUUID("1000"));
1758 discovery_filter
.reset(df
);
1759 } else if (i
== 1) {
1760 BluetoothDiscoveryFilter
* df
= new BluetoothDiscoveryFilter(
1761 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE
);
1763 df
->AddUUID(BluetoothUUID("1020"));
1764 df
->AddUUID(BluetoothUUID("1001"));
1765 discovery_filter
.reset(df
);
1766 } else if (i
== 2) {
1767 BluetoothDiscoveryFilter
* df
= new BluetoothDiscoveryFilter(
1768 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE
);
1770 df
->AddUUID(BluetoothUUID("1020"));
1771 df
->AddUUID(BluetoothUUID("1003"));
1772 discovery_filter
.reset(df
);
1775 adapter_
->StartDiscoverySessionWithFilter(
1776 discovery_filter
.Pass(),
1777 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
1778 base::Unretained(this)),
1779 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1780 base::Unretained(this)));
1782 message_loop_
.Run();
1785 EXPECT_EQ(1, observer
.discovering_changed_count());
1788 auto filter
= fake_bluetooth_adapter_client_
->GetDiscoveryFilter();
1789 EXPECT_EQ("le", *filter
->transport
);
1790 EXPECT_EQ(-85, *filter
->rssi
);
1791 EXPECT_EQ(nullptr, filter
->pathloss
.get());
1792 std::vector
<std::string
> uuids
= *filter
->uuids
;
1793 EXPECT_NE(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1000"));
1794 } else if (i
== 1) {
1795 auto filter
= fake_bluetooth_adapter_client_
->GetDiscoveryFilter();
1796 EXPECT_EQ("le", *filter
->transport
);
1797 EXPECT_EQ(-85, *filter
->rssi
);
1798 EXPECT_EQ(nullptr, filter
->pathloss
.get());
1799 std::vector
<std::string
> uuids
= *filter
->uuids
;
1800 EXPECT_NE(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1000"));
1801 EXPECT_NE(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1001"));
1802 EXPECT_NE(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1020"));
1803 } else if (i
== 2) {
1804 auto filter
= fake_bluetooth_adapter_client_
->GetDiscoveryFilter();
1805 EXPECT_EQ("le", *filter
->transport
);
1806 EXPECT_EQ(-85, *filter
->rssi
);
1807 EXPECT_EQ(nullptr, filter
->pathloss
.get());
1808 std::vector
<std::string
> uuids
= *filter
->uuids
;
1809 EXPECT_NE(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1000"));
1810 EXPECT_NE(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1001"));
1811 EXPECT_NE(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1003"));
1812 EXPECT_NE(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1020"));
1816 // the success callback should have been called 3 times and the adapter should
1818 EXPECT_EQ(3, callback_count_
);
1819 EXPECT_EQ(0, error_callback_count_
);
1820 EXPECT_TRUE(adapter_
->IsDiscovering());
1821 ASSERT_EQ((size_t)3, discovery_sessions_
.size());
1823 callback_count_
= 0;
1824 // Request to stop discovery twice.
1825 for (int i
= 0; i
< 2; i
++) {
1826 discovery_sessions_
[i
]->Stop(
1827 base::Bind(&BluetoothChromeOSTest::Callback
, base::Unretained(this)),
1828 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1829 base::Unretained(this)));
1830 message_loop_
.Run();
1833 auto filter
= fake_bluetooth_adapter_client_
->GetDiscoveryFilter();
1834 EXPECT_EQ("le", *filter
->transport
);
1835 EXPECT_EQ(-65, *filter
->rssi
);
1836 EXPECT_EQ(nullptr, filter
->pathloss
.get());
1837 std::vector
<std::string
> uuids
= *filter
->uuids
;
1838 EXPECT_EQ(3UL, uuids
.size());
1839 EXPECT_EQ(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1000"));
1840 EXPECT_NE(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1001"));
1841 EXPECT_NE(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1003"));
1842 EXPECT_NE(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1020"));
1843 } else if (i
== 1) {
1844 auto filter
= fake_bluetooth_adapter_client_
->GetDiscoveryFilter();
1845 EXPECT_EQ("le", *filter
->transport
);
1846 EXPECT_EQ(-65, *filter
->rssi
);
1847 EXPECT_EQ(nullptr, filter
->pathloss
.get());
1848 std::vector
<std::string
> uuids
= *filter
->uuids
;
1849 EXPECT_EQ(2UL, uuids
.size());
1850 EXPECT_EQ(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1000"));
1851 EXPECT_EQ(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1001"));
1852 EXPECT_NE(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1003"));
1853 EXPECT_NE(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1020"));
1854 } else if (i
== 2) {
1855 auto filter
= fake_bluetooth_adapter_client_
->GetDiscoveryFilter();
1856 EXPECT_EQ("le", *filter
->transport
);
1857 EXPECT_EQ(-65, *filter
->rssi
);
1858 EXPECT_EQ(nullptr, filter
->pathloss
.get());
1859 std::vector
<std::string
> uuids
= *filter
->uuids
;
1860 EXPECT_EQ(0UL, uuids
.size());
1864 // The success callback should have been called 2 times and the adapter should
1865 // still be discovering.
1866 EXPECT_EQ(2, callback_count_
);
1867 EXPECT_EQ(0, error_callback_count_
);
1868 EXPECT_TRUE(adapter_
->IsDiscovering());
1869 EXPECT_FALSE(discovery_sessions_
[0]->IsActive());
1870 EXPECT_FALSE(discovery_sessions_
[1]->IsActive());
1871 EXPECT_TRUE(discovery_sessions_
[2]->IsActive());
1873 callback_count_
= 0;
1875 // Request device discovery 3 times.
1876 for (int i
= 0; i
< 3; i
++) {
1877 scoped_ptr
<BluetoothDiscoveryFilter
> discovery_filter
;
1880 BluetoothDiscoveryFilter
* df
= new BluetoothDiscoveryFilter(
1881 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE
);
1883 df
->AddUUID(BluetoothUUID("1000"));
1884 discovery_filter
.reset(df
);
1885 } else if (i
== 1) {
1886 BluetoothDiscoveryFilter
* df
= new BluetoothDiscoveryFilter(
1887 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE
);
1889 df
->AddUUID(BluetoothUUID("1020"));
1890 df
->AddUUID(BluetoothUUID("1001"));
1891 discovery_filter
.reset(df
);
1892 } else if (i
== 2) {
1893 BluetoothDiscoveryFilter
* df
= new BluetoothDiscoveryFilter(
1894 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE
);
1896 df
->AddUUID(BluetoothUUID("1020"));
1897 df
->AddUUID(BluetoothUUID("1003"));
1898 discovery_filter
.reset(df
);
1901 adapter_
->StartDiscoverySessionWithFilter(
1902 discovery_filter
.Pass(),
1903 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
1904 base::Unretained(this)),
1905 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1906 base::Unretained(this)));
1908 // each result in 1 requests.
1909 message_loop_
.Run();
1912 auto filter
= fake_bluetooth_adapter_client_
->GetDiscoveryFilter();
1913 EXPECT_EQ("le", *filter
->transport
);
1914 EXPECT_EQ(-85, *filter
->rssi
);
1915 EXPECT_EQ(nullptr, filter
->pathloss
.get());
1916 std::vector
<std::string
> uuids
= *filter
->uuids
;
1917 EXPECT_NE(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1000"));
1918 EXPECT_NE(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1003"));
1919 EXPECT_NE(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1020"));
1920 } else if (i
== 1 || i
== 2) {
1921 auto filter
= fake_bluetooth_adapter_client_
->GetDiscoveryFilter();
1922 EXPECT_EQ("le", *filter
->transport
);
1923 EXPECT_EQ(-85, *filter
->rssi
);
1924 EXPECT_EQ(nullptr, filter
->pathloss
.get());
1925 std::vector
<std::string
> uuids
= *filter
->uuids
;
1926 EXPECT_NE(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1000"));
1927 EXPECT_NE(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1001"));
1928 EXPECT_NE(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1003"));
1929 EXPECT_NE(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1020"));
1933 // The success callback should have been called 3 times and the adapter should
1934 // still be discovering.
1935 EXPECT_EQ(3, callback_count_
);
1936 EXPECT_EQ(0, error_callback_count_
);
1937 EXPECT_TRUE(adapter_
->IsDiscovering());
1938 ASSERT_EQ((size_t)6, discovery_sessions_
.size());
1940 callback_count_
= 0;
1941 // Request to stop discovery 4 times.
1942 for (int i
= 2; i
< 6; i
++) {
1943 discovery_sessions_
[i
]->Stop(
1944 base::Bind(&BluetoothChromeOSTest::Callback
, base::Unretained(this)),
1945 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1946 base::Unretained(this)));
1948 // filter no 2 is same as filter no 5, so removing it shouldn't cause any
1950 if (i
!= 2 && i
!= 5)
1951 message_loop_
.Run();
1953 // Run only once, as there should have been one D-Bus call.
1954 message_loop_
.Run();
1956 // The success callback should have been called 4 times and the adapter should
1957 // no longer be discovering.
1958 EXPECT_EQ(4, callback_count_
);
1959 EXPECT_EQ(0, error_callback_count_
);
1960 EXPECT_FALSE(adapter_
->IsDiscovering());
1961 EXPECT_EQ(1, observer
.discovering_changed_count());
1963 // All discovery sessions should be inactive.
1964 for (int i
= 0; i
< 6; i
++)
1965 EXPECT_FALSE(discovery_sessions_
[i
]->IsActive());
1967 auto filter
= fake_bluetooth_adapter_client_
->GetDiscoveryFilter();
1968 EXPECT_EQ(nullptr, filter
);
1971 // This unit test asserts that filter merging logic works correctly for filtered
1972 // discovery requests done via the BluetoothAdapter.
1973 TEST_F(BluetoothChromeOSTest
, SetDiscoveryFilterMergingTest
) {
1975 adapter_
->SetPowered(true, base::Bind(&BluetoothChromeOSTest::Callback
,
1976 base::Unretained(this)),
1977 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1978 base::Unretained(this)));
1980 BluetoothDiscoveryFilter
* df
= new BluetoothDiscoveryFilter(
1981 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE
);
1983 df
->AddUUID(BluetoothUUID("1000"));
1984 scoped_ptr
<BluetoothDiscoveryFilter
> discovery_filter(df
);
1986 adapter_
->StartDiscoverySessionWithFilter(
1987 discovery_filter
.Pass(),
1988 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
1989 base::Unretained(this)),
1990 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1991 base::Unretained(this)));
1993 message_loop_
.Run();
1995 auto filter
= fake_bluetooth_adapter_client_
->GetDiscoveryFilter();
1996 EXPECT_EQ("le", *filter
->transport
);
1997 EXPECT_EQ(-15, *filter
->rssi
);
1998 EXPECT_EQ(nullptr, filter
->pathloss
.get());
1999 std::vector
<std::string
> uuids
= *filter
->uuids
;
2000 EXPECT_NE(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1000"));
2002 df
= new BluetoothDiscoveryFilter(
2003 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE
);
2005 df
->AddUUID(BluetoothUUID("1020"));
2006 df
->AddUUID(BluetoothUUID("1001"));
2007 discovery_filter
= scoped_ptr
<BluetoothDiscoveryFilter
>(df
);
2009 adapter_
->StartDiscoverySessionWithFilter(
2010 discovery_filter
.Pass(),
2011 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
2012 base::Unretained(this)),
2013 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
2014 base::Unretained(this)));
2016 message_loop_
.Run();
2018 filter
= fake_bluetooth_adapter_client_
->GetDiscoveryFilter();
2019 EXPECT_EQ("le", *filter
->transport
);
2020 EXPECT_EQ(-60, *filter
->rssi
);
2021 EXPECT_EQ(nullptr, filter
->pathloss
.get());
2022 uuids
= *filter
->uuids
;
2023 EXPECT_NE(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1000"));
2024 EXPECT_NE(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1001"));
2025 EXPECT_NE(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1020"));
2027 BluetoothDiscoveryFilter
* df3
= new BluetoothDiscoveryFilter(
2028 BluetoothDiscoveryFilter::Transport::TRANSPORT_CLASSIC
);
2030 df3
->AddUUID(BluetoothUUID("1020"));
2031 df3
->AddUUID(BluetoothUUID("1003"));
2032 scoped_ptr
<BluetoothDiscoveryFilter
> discovery_filter3(df3
);
2034 adapter_
->StartDiscoverySessionWithFilter(
2035 discovery_filter3
.Pass(),
2036 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
2037 base::Unretained(this)),
2038 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
2039 base::Unretained(this)));
2041 message_loop_
.Run();
2043 filter
= fake_bluetooth_adapter_client_
->GetDiscoveryFilter();
2044 EXPECT_EQ("auto", *filter
->transport
);
2045 EXPECT_EQ(-65, *filter
->rssi
);
2046 EXPECT_EQ(nullptr, filter
->pathloss
.get());
2047 uuids
= *filter
->uuids
;
2048 EXPECT_NE(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1000"));
2049 EXPECT_NE(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1001"));
2050 EXPECT_NE(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1003"));
2051 EXPECT_NE(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1020"));
2053 // start additionally classic scan
2054 adapter_
->StartDiscoverySession(
2055 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
2056 base::Unretained(this)),
2057 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
2058 base::Unretained(this)));
2060 message_loop_
.Run();
2062 filter
= fake_bluetooth_adapter_client_
->GetDiscoveryFilter();
2063 EXPECT_EQ("auto", *filter
->transport
);
2064 EXPECT_EQ(nullptr, filter
->rssi
.get());
2065 EXPECT_EQ(nullptr, filter
->pathloss
.get());
2066 EXPECT_EQ(nullptr, filter
->uuids
.get());
2068 // Request to stop discovery 4 times.
2069 for (int i
= 3; i
>= 0; i
--) {
2070 discovery_sessions_
[i
]->Stop(
2071 base::Bind(&BluetoothChromeOSTest::Callback
, base::Unretained(this)),
2072 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
2073 base::Unretained(this)));
2075 // Every session stopping would trigger filter update
2076 message_loop_
.Run();
2080 TEST_F(BluetoothChromeOSTest
, DeviceProperties
) {
2083 BluetoothAdapter::DeviceList devices
= adapter_
->GetDevices();
2084 ASSERT_EQ(2U, devices
.size());
2085 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress
,
2086 devices
[0]->GetAddress());
2088 // Verify the other device properties.
2089 EXPECT_EQ(base::UTF8ToUTF16(FakeBluetoothDeviceClient::kPairedDeviceName
),
2090 devices
[0]->GetName());
2091 EXPECT_EQ(BluetoothDevice::DEVICE_COMPUTER
, devices
[0]->GetDeviceType());
2092 EXPECT_TRUE(devices
[0]->IsPaired());
2093 EXPECT_FALSE(devices
[0]->IsConnected());
2094 EXPECT_FALSE(devices
[0]->IsConnecting());
2096 // Non HID devices are always connectable.
2097 EXPECT_TRUE(devices
[0]->IsConnectable());
2099 BluetoothDevice::UUIDList uuids
= devices
[0]->GetUUIDs();
2100 ASSERT_EQ(2U, uuids
.size());
2101 EXPECT_EQ(uuids
[0], BluetoothUUID("1800"));
2102 EXPECT_EQ(uuids
[1], BluetoothUUID("1801"));
2104 EXPECT_EQ(BluetoothDevice::VENDOR_ID_USB
, devices
[0]->GetVendorIDSource());
2105 EXPECT_EQ(0x05ac, devices
[0]->GetVendorID());
2106 EXPECT_EQ(0x030d, devices
[0]->GetProductID());
2107 EXPECT_EQ(0x0306, devices
[0]->GetDeviceID());
2110 TEST_F(BluetoothChromeOSTest
, DeviceClassChanged
) {
2111 // Simulate a change of class of a device, as sometimes occurs
2112 // during discovery.
2115 BluetoothAdapter::DeviceList devices
= adapter_
->GetDevices();
2116 ASSERT_EQ(2U, devices
.size());
2117 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress
,
2118 devices
[0]->GetAddress());
2119 ASSERT_EQ(BluetoothDevice::DEVICE_COMPUTER
, devices
[0]->GetDeviceType());
2121 // Install an observer; expect the DeviceChanged method to be called when
2122 // we change the class of the device.
2123 TestBluetoothAdapterObserver
observer(adapter_
);
2125 FakeBluetoothDeviceClient::Properties
* properties
=
2126 fake_bluetooth_device_client_
->GetProperties(
2127 dbus::ObjectPath(FakeBluetoothDeviceClient::kPairedDevicePath
));
2129 properties
->bluetooth_class
.ReplaceValue(0x002580);
2131 EXPECT_EQ(1, observer
.device_changed_count());
2132 EXPECT_EQ(devices
[0], observer
.last_device());
2134 EXPECT_EQ(BluetoothDevice::DEVICE_MOUSE
, devices
[0]->GetDeviceType());
2137 TEST_F(BluetoothChromeOSTest
, DeviceNameChanged
) {
2138 // Simulate a change of name of a device.
2141 BluetoothAdapter::DeviceList devices
= adapter_
->GetDevices();
2142 ASSERT_EQ(2U, devices
.size());
2143 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress
,
2144 devices
[0]->GetAddress());
2145 ASSERT_EQ(base::UTF8ToUTF16(FakeBluetoothDeviceClient::kPairedDeviceName
),
2146 devices
[0]->GetName());
2148 // Install an observer; expect the DeviceChanged method to be called when
2149 // we change the alias of the device.
2150 TestBluetoothAdapterObserver
observer(adapter_
);
2152 FakeBluetoothDeviceClient::Properties
* properties
=
2153 fake_bluetooth_device_client_
->GetProperties(
2154 dbus::ObjectPath(FakeBluetoothDeviceClient::kPairedDevicePath
));
2156 static const std::string
new_name("New Device Name");
2157 properties
->alias
.ReplaceValue(new_name
);
2159 EXPECT_EQ(1, observer
.device_changed_count());
2160 EXPECT_EQ(devices
[0], observer
.last_device());
2162 EXPECT_EQ(base::UTF8ToUTF16(new_name
), devices
[0]->GetName());
2165 TEST_F(BluetoothChromeOSTest
, DeviceUuidsChanged
) {
2166 // Simulate a change of advertised services of a device.
2169 BluetoothAdapter::DeviceList devices
= adapter_
->GetDevices();
2170 ASSERT_EQ(2U, devices
.size());
2171 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress
,
2172 devices
[0]->GetAddress());
2174 BluetoothDevice::UUIDList uuids
= devices
[0]->GetUUIDs();
2175 ASSERT_EQ(2U, uuids
.size());
2176 ASSERT_EQ(uuids
[0], BluetoothUUID("1800"));
2177 ASSERT_EQ(uuids
[1], BluetoothUUID("1801"));
2179 // Install an observer; expect the DeviceChanged method to be called when
2180 // we change the class of the device.
2181 TestBluetoothAdapterObserver
observer(adapter_
);
2183 FakeBluetoothDeviceClient::Properties
* properties
=
2184 fake_bluetooth_device_client_
->GetProperties(
2185 dbus::ObjectPath(FakeBluetoothDeviceClient::kPairedDevicePath
));
2187 std::vector
<std::string
> new_uuids
;
2188 new_uuids
.push_back(uuids
[0].canonical_value());
2189 new_uuids
.push_back(uuids
[1].canonical_value());
2190 new_uuids
.push_back("0000110c-0000-1000-8000-00805f9b34fb");
2191 new_uuids
.push_back("0000110e-0000-1000-8000-00805f9b34fb");
2192 new_uuids
.push_back("0000110a-0000-1000-8000-00805f9b34fb");
2194 properties
->uuids
.ReplaceValue(new_uuids
);
2196 EXPECT_EQ(1, observer
.device_changed_count());
2197 EXPECT_EQ(devices
[0], observer
.last_device());
2199 // Fetching the value should give the new one.
2200 uuids
= devices
[0]->GetUUIDs();
2201 ASSERT_EQ(5U, uuids
.size());
2202 EXPECT_EQ(uuids
[0], BluetoothUUID("1800"));
2203 EXPECT_EQ(uuids
[1], BluetoothUUID("1801"));
2204 EXPECT_EQ(uuids
[2], BluetoothUUID("110c"));
2205 EXPECT_EQ(uuids
[3], BluetoothUUID("110e"));
2206 EXPECT_EQ(uuids
[4], BluetoothUUID("110a"));
2209 TEST_F(BluetoothChromeOSTest
, DeviceInquiryRSSIInvalidated
) {
2210 // Simulate invalidation of inquiry RSSI of a device, as it occurs
2211 // when discovery is finished.
2214 BluetoothAdapter::DeviceList devices
= adapter_
->GetDevices();
2215 ASSERT_EQ(2U, devices
.size());
2216 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress
,
2217 devices
[0]->GetAddress());
2219 FakeBluetoothDeviceClient::Properties
* properties
=
2220 fake_bluetooth_device_client_
->GetProperties(
2221 dbus::ObjectPath(FakeBluetoothDeviceClient::kPairedDevicePath
));
2223 // During discovery, rssi is a valid value (-75)
2224 properties
->rssi
.ReplaceValue(-75);
2225 properties
->rssi
.set_valid(true);
2227 ASSERT_EQ(-75, devices
[0]->GetInquiryRSSI());
2229 // Install an observer; expect the DeviceChanged method to be called when
2230 // we invalidate the RSSI of the device.
2231 TestBluetoothAdapterObserver
observer(adapter_
);
2233 // When discovery is over, the value should be invalidated.
2234 properties
->rssi
.set_valid(false);
2235 properties
->NotifyPropertyChanged(properties
->rssi
.name());
2237 EXPECT_EQ(1, observer
.device_changed_count());
2238 EXPECT_EQ(devices
[0], observer
.last_device());
2240 int unknown_power
= BluetoothDevice::kUnknownPower
;
2241 EXPECT_EQ(unknown_power
, devices
[0]->GetInquiryRSSI());
2244 TEST_F(BluetoothChromeOSTest
, DeviceInquiryTxPowerInvalidated
) {
2245 // Simulate invalidation of inquiry TxPower of a device, as it occurs
2246 // when discovery is finished.
2249 BluetoothAdapter::DeviceList devices
= adapter_
->GetDevices();
2250 ASSERT_EQ(2U, devices
.size());
2251 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress
,
2252 devices
[0]->GetAddress());
2254 FakeBluetoothDeviceClient::Properties
* properties
=
2255 fake_bluetooth_device_client_
->GetProperties(
2256 dbus::ObjectPath(FakeBluetoothDeviceClient::kPairedDevicePath
));
2258 // During discovery, tx_power is a valid value (0)
2259 properties
->tx_power
.ReplaceValue(0);
2260 properties
->tx_power
.set_valid(true);
2262 ASSERT_EQ(0, devices
[0]->GetInquiryTxPower());
2264 // Install an observer; expect the DeviceChanged method to be called when
2265 // we invalidate the tx_power of the device.
2266 TestBluetoothAdapterObserver
observer(adapter_
);
2268 // When discovery is over, the value should be invalidated.
2269 properties
->tx_power
.set_valid(false);
2270 properties
->NotifyPropertyChanged(properties
->tx_power
.name());
2272 EXPECT_EQ(1, observer
.device_changed_count());
2273 EXPECT_EQ(devices
[0], observer
.last_device());
2275 int unknown_power
= BluetoothDevice::kUnknownPower
;
2276 EXPECT_EQ(unknown_power
, devices
[0]->GetInquiryTxPower());
2279 TEST_F(BluetoothChromeOSTest
, ForgetDevice
) {
2282 BluetoothAdapter::DeviceList devices
= adapter_
->GetDevices();
2283 ASSERT_EQ(2U, devices
.size());
2284 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress
,
2285 devices
[0]->GetAddress());
2287 std::string address
= devices
[0]->GetAddress();
2289 // Install an observer; expect the DeviceRemoved method to be called
2290 // with the device we remove.
2291 TestBluetoothAdapterObserver
observer(adapter_
);
2293 devices
[0]->Forget(GetErrorCallback());
2294 EXPECT_EQ(0, error_callback_count_
);
2296 EXPECT_EQ(1, observer
.device_removed_count());
2297 EXPECT_EQ(address
, observer
.last_device_address());
2299 // GetDevices shouldn't return the device either.
2300 devices
= adapter_
->GetDevices();
2301 ASSERT_EQ(1U, devices
.size());
2304 TEST_F(BluetoothChromeOSTest
, ForgetUnpairedDevice
) {
2308 BluetoothDevice
* device
= adapter_
->GetDevice(
2309 FakeBluetoothDeviceClient::kConnectUnpairableAddress
);
2310 ASSERT_TRUE(device
!= nullptr);
2311 ASSERT_FALSE(device
->IsPaired());
2313 // Connect the device so it becomes trusted and remembered.
2314 device
->Connect(nullptr, GetCallback(),
2315 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
2316 base::Unretained(this)));
2318 ASSERT_EQ(1, callback_count_
);
2319 ASSERT_EQ(0, error_callback_count_
);
2320 callback_count_
= 0;
2322 ASSERT_TRUE(device
->IsConnected());
2323 ASSERT_FALSE(device
->IsConnecting());
2325 // Make sure the trusted property has been set to true.
2326 FakeBluetoothDeviceClient::Properties
* properties
=
2327 fake_bluetooth_device_client_
->GetProperties(
2328 dbus::ObjectPath(FakeBluetoothDeviceClient::kConnectUnpairablePath
));
2329 ASSERT_TRUE(properties
->trusted
.value());
2331 // Install an observer; expect the DeviceRemoved method to be called
2332 // with the device we remove.
2333 TestBluetoothAdapterObserver
observer(adapter_
);
2335 device
->Forget(GetErrorCallback());
2336 EXPECT_EQ(0, error_callback_count_
);
2338 EXPECT_EQ(1, observer
.device_removed_count());
2339 EXPECT_EQ(FakeBluetoothDeviceClient::kConnectUnpairableAddress
,
2340 observer
.last_device_address());
2342 // GetDevices shouldn't return the device either.
2343 device
= adapter_
->GetDevice(
2344 FakeBluetoothDeviceClient::kConnectUnpairableAddress
);
2345 EXPECT_FALSE(device
!= nullptr);
2348 TEST_F(BluetoothChromeOSTest
, ConnectPairedDevice
) {
2351 BluetoothDevice
* device
= adapter_
->GetDevice(
2352 FakeBluetoothDeviceClient::kPairedDeviceAddress
);
2353 ASSERT_TRUE(device
!= nullptr);
2354 ASSERT_TRUE(device
->IsPaired());
2356 TestBluetoothAdapterObserver
observer(adapter_
);
2358 // Connect without a pairing delegate; since the device is already Paired
2359 // this should succeed and the device should become connected.
2360 device
->Connect(nullptr, GetCallback(),
2361 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
2362 base::Unretained(this)));
2364 EXPECT_EQ(1, callback_count_
);
2365 EXPECT_EQ(0, error_callback_count_
);
2367 // Two changes for connecting, one for connected and one for for trusted
2368 // after connecting.
2369 EXPECT_EQ(4, observer
.device_changed_count());
2370 EXPECT_EQ(device
, observer
.last_device());
2372 EXPECT_TRUE(device
->IsConnected());
2373 EXPECT_FALSE(device
->IsConnecting());
2376 TEST_F(BluetoothChromeOSTest
, ConnectUnpairableDevice
) {
2380 BluetoothDevice
* device
= adapter_
->GetDevice(
2381 FakeBluetoothDeviceClient::kConnectUnpairableAddress
);
2382 ASSERT_TRUE(device
!= nullptr);
2383 ASSERT_FALSE(device
->IsPaired());
2385 TestBluetoothAdapterObserver
observer(adapter_
);
2387 // Connect without a pairing delegate; since the device does not require
2388 // pairing, this should succeed and the device should become connected.
2389 device
->Connect(nullptr, GetCallback(),
2390 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
2391 base::Unretained(this)));
2393 EXPECT_EQ(1, callback_count_
);
2394 EXPECT_EQ(0, error_callback_count_
);
2396 // Two changes for connecting, one for connected, one for for trusted after
2397 // connection, and one for the reconnect mode (IsConnectable).
2398 EXPECT_EQ(5, observer
.device_changed_count());
2399 EXPECT_EQ(device
, observer
.last_device());
2401 EXPECT_TRUE(device
->IsConnected());
2402 EXPECT_FALSE(device
->IsConnecting());
2404 // Make sure the trusted property has been set to true.
2405 FakeBluetoothDeviceClient::Properties
* properties
=
2406 fake_bluetooth_device_client_
->GetProperties(
2407 dbus::ObjectPath(FakeBluetoothDeviceClient::kConnectUnpairablePath
));
2408 EXPECT_TRUE(properties
->trusted
.value());
2410 // Verify is a HID device and is not connectable.
2411 BluetoothDevice::UUIDList uuids
= device
->GetUUIDs();
2412 ASSERT_EQ(1U, uuids
.size());
2413 EXPECT_EQ(uuids
[0], BluetoothUUID("1124"));
2414 EXPECT_FALSE(device
->IsConnectable());
2417 TEST_F(BluetoothChromeOSTest
, ConnectConnectedDevice
) {
2420 BluetoothDevice
* device
= adapter_
->GetDevice(
2421 FakeBluetoothDeviceClient::kPairedDeviceAddress
);
2422 ASSERT_TRUE(device
!= nullptr);
2423 ASSERT_TRUE(device
->IsPaired());
2425 device
->Connect(nullptr, GetCallback(),
2426 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
2427 base::Unretained(this)));
2429 ASSERT_EQ(1, callback_count_
);
2430 ASSERT_EQ(0, error_callback_count_
);
2431 callback_count_
= 0;
2433 ASSERT_TRUE(device
->IsConnected());
2435 // Connect again; since the device is already Connected, this shouldn't do
2436 // anything to initiate the connection.
2437 TestBluetoothAdapterObserver
observer(adapter_
);
2439 device
->Connect(nullptr, GetCallback(),
2440 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
2441 base::Unretained(this)));
2443 EXPECT_EQ(1, callback_count_
);
2444 EXPECT_EQ(0, error_callback_count_
);
2446 // The observer will be called because Connecting will toggle true and false,
2447 // and the trusted property will be updated to true.
2448 EXPECT_EQ(3, observer
.device_changed_count());
2450 EXPECT_TRUE(device
->IsConnected());
2451 EXPECT_FALSE(device
->IsConnecting());
2454 TEST_F(BluetoothChromeOSTest
, ConnectDeviceFails
) {
2458 BluetoothDevice
* device
= adapter_
->GetDevice(
2459 FakeBluetoothDeviceClient::kLegacyAutopairAddress
);
2460 ASSERT_TRUE(device
!= nullptr);
2461 ASSERT_FALSE(device
->IsPaired());
2463 TestBluetoothAdapterObserver
observer(adapter_
);
2465 // Connect without a pairing delegate; since the device requires pairing,
2466 // this should fail with an error.
2467 device
->Connect(nullptr, GetCallback(),
2468 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
2469 base::Unretained(this)));
2471 EXPECT_EQ(0, callback_count_
);
2472 EXPECT_EQ(1, error_callback_count_
);
2473 EXPECT_EQ(BluetoothDevice::ERROR_FAILED
, last_connect_error_
);
2475 EXPECT_EQ(2, observer
.device_changed_count());
2477 EXPECT_FALSE(device
->IsConnected());
2478 EXPECT_FALSE(device
->IsConnecting());
2481 TEST_F(BluetoothChromeOSTest
, DisconnectDevice
) {
2484 BluetoothDevice
* device
= adapter_
->GetDevice(
2485 FakeBluetoothDeviceClient::kPairedDeviceAddress
);
2486 ASSERT_TRUE(device
!= nullptr);
2487 ASSERT_TRUE(device
->IsPaired());
2489 device
->Connect(nullptr, GetCallback(),
2490 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
2491 base::Unretained(this)));
2493 ASSERT_EQ(1, callback_count_
);
2494 ASSERT_EQ(0, error_callback_count_
);
2495 callback_count_
= 0;
2497 ASSERT_TRUE(device
->IsConnected());
2498 ASSERT_FALSE(device
->IsConnecting());
2500 // Disconnect the device, we should see the observer method fire and the
2501 // device get dropped.
2502 TestBluetoothAdapterObserver
observer(adapter_
);
2504 device
->Disconnect(GetCallback(), GetErrorCallback());
2506 EXPECT_EQ(1, callback_count_
);
2507 EXPECT_EQ(0, error_callback_count_
);
2509 EXPECT_EQ(1, observer
.device_changed_count());
2510 EXPECT_EQ(device
, observer
.last_device());
2512 EXPECT_FALSE(device
->IsConnected());
2515 TEST_F(BluetoothChromeOSTest
, DisconnectUnconnectedDevice
) {
2518 BluetoothDevice
* device
= adapter_
->GetDevice(
2519 FakeBluetoothDeviceClient::kPairedDeviceAddress
);
2520 ASSERT_TRUE(device
!= nullptr);
2521 ASSERT_TRUE(device
->IsPaired());
2522 ASSERT_FALSE(device
->IsConnected());
2524 // Disconnect the device, we should see the observer method fire and the
2525 // device get dropped.
2526 TestBluetoothAdapterObserver
observer(adapter_
);
2528 device
->Disconnect(GetCallback(), GetErrorCallback());
2530 EXPECT_EQ(0, callback_count_
);
2531 EXPECT_EQ(1, error_callback_count_
);
2533 EXPECT_EQ(0, observer
.device_changed_count());
2535 EXPECT_FALSE(device
->IsConnected());
2538 TEST_F(BluetoothChromeOSTest
, PairLegacyAutopair
) {
2539 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
2544 // The Legacy Autopair device requires no PIN or Passkey to pair because
2545 // the daemon provides 0000 to the device for us.
2546 BluetoothDevice
* device
= adapter_
->GetDevice(
2547 FakeBluetoothDeviceClient::kLegacyAutopairAddress
);
2548 ASSERT_TRUE(device
!= nullptr);
2549 ASSERT_FALSE(device
->IsPaired());
2551 TestBluetoothAdapterObserver
observer(adapter_
);
2553 TestPairingDelegate pairing_delegate
;
2554 device
->Connect(&pairing_delegate
, GetCallback(),
2555 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
2556 base::Unretained(this)));
2558 EXPECT_EQ(0, pairing_delegate
.call_count_
);
2559 EXPECT_TRUE(device
->IsConnecting());
2561 message_loop_
.Run();
2563 EXPECT_EQ(1, callback_count_
);
2564 EXPECT_EQ(0, error_callback_count_
);
2566 // Two changes for connecting, one change for connected, one for paired,
2567 // two for trusted (after pairing and connection), and one for the reconnect
2568 // mode (IsConnectable).
2569 EXPECT_EQ(7, observer
.device_changed_count());
2570 EXPECT_EQ(device
, observer
.last_device());
2572 EXPECT_TRUE(device
->IsConnected());
2573 EXPECT_FALSE(device
->IsConnecting());
2575 EXPECT_TRUE(device
->IsPaired());
2577 // Verify is a HID device and is connectable.
2578 BluetoothDevice::UUIDList uuids
= device
->GetUUIDs();
2579 ASSERT_EQ(1U, uuids
.size());
2580 EXPECT_EQ(uuids
[0], BluetoothUUID("1124"));
2581 EXPECT_TRUE(device
->IsConnectable());
2583 // Make sure the trusted property has been set to true.
2584 FakeBluetoothDeviceClient::Properties
* properties
=
2585 fake_bluetooth_device_client_
->GetProperties(
2586 dbus::ObjectPath(FakeBluetoothDeviceClient::kLegacyAutopairPath
));
2587 EXPECT_TRUE(properties
->trusted
.value());
2590 TEST_F(BluetoothChromeOSTest
, PairDisplayPinCode
) {
2591 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
2596 // Requires that we display a randomly generated PIN on the screen.
2597 BluetoothDevice
* device
= adapter_
->GetDevice(
2598 FakeBluetoothDeviceClient::kDisplayPinCodeAddress
);
2599 ASSERT_TRUE(device
!= nullptr);
2600 ASSERT_FALSE(device
->IsPaired());
2602 TestBluetoothAdapterObserver
observer(adapter_
);
2604 TestPairingDelegate pairing_delegate
;
2605 device
->Connect(&pairing_delegate
, GetCallback(),
2606 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
2607 base::Unretained(this)));
2609 EXPECT_EQ(1, pairing_delegate
.call_count_
);
2610 EXPECT_EQ(1, pairing_delegate
.display_pincode_count_
);
2611 EXPECT_EQ("123456", pairing_delegate
.last_pincode_
);
2612 EXPECT_TRUE(device
->IsConnecting());
2614 message_loop_
.Run();
2616 EXPECT_EQ(1, callback_count_
);
2617 EXPECT_EQ(0, error_callback_count_
);
2619 // Two changes for connecting, one change for connected, one for paired,
2620 // two for trusted (after pairing and connection), and one for the reconnect
2621 // mode (IsConnectable).
2622 EXPECT_EQ(7, observer
.device_changed_count());
2623 EXPECT_EQ(device
, observer
.last_device());
2625 EXPECT_TRUE(device
->IsConnected());
2626 EXPECT_FALSE(device
->IsConnecting());
2628 EXPECT_TRUE(device
->IsPaired());
2630 // Verify is a HID device and is connectable.
2631 BluetoothDevice::UUIDList uuids
= device
->GetUUIDs();
2632 ASSERT_EQ(1U, uuids
.size());
2633 EXPECT_EQ(uuids
[0], BluetoothUUID("1124"));
2634 EXPECT_TRUE(device
->IsConnectable());
2636 // Make sure the trusted property has been set to true.
2637 FakeBluetoothDeviceClient::Properties
* properties
=
2638 fake_bluetooth_device_client_
->GetProperties(
2639 dbus::ObjectPath(FakeBluetoothDeviceClient::kDisplayPinCodePath
));
2640 EXPECT_TRUE(properties
->trusted
.value());
2643 TEST_F(BluetoothChromeOSTest
, PairDisplayPasskey
) {
2644 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
2649 // Requires that we display a randomly generated Passkey on the screen,
2650 // and notifies us as it's typed in.
2651 BluetoothDevice
* device
= adapter_
->GetDevice(
2652 FakeBluetoothDeviceClient::kDisplayPasskeyAddress
);
2653 ASSERT_TRUE(device
!= nullptr);
2654 ASSERT_FALSE(device
->IsPaired());
2656 TestBluetoothAdapterObserver
observer(adapter_
);
2658 TestPairingDelegate pairing_delegate
;
2659 device
->Connect(&pairing_delegate
, GetCallback(),
2660 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
2661 base::Unretained(this)));
2663 // One call for DisplayPasskey() and one for KeysEntered().
2664 EXPECT_EQ(2, pairing_delegate
.call_count_
);
2665 EXPECT_EQ(1, pairing_delegate
.display_passkey_count_
);
2666 EXPECT_EQ(123456U, pairing_delegate
.last_passkey_
);
2667 EXPECT_EQ(1, pairing_delegate
.keys_entered_count_
);
2668 EXPECT_EQ(0U, pairing_delegate
.last_entered_
);
2670 EXPECT_TRUE(device
->IsConnecting());
2672 // One call to KeysEntered() for each key, including [enter].
2673 for (int i
= 1; i
<= 7; ++i
) {
2674 message_loop_
.Run();
2676 EXPECT_EQ(2 + i
, pairing_delegate
.call_count_
);
2677 EXPECT_EQ(1 + i
, pairing_delegate
.keys_entered_count_
);
2678 EXPECT_EQ(static_cast<uint32_t>(i
), pairing_delegate
.last_entered_
);
2681 message_loop_
.Run();
2683 // 8 KeysEntered notifications (0 to 7, inclusive) and one aditional call for
2684 // DisplayPasskey().
2685 EXPECT_EQ(9, pairing_delegate
.call_count_
);
2686 EXPECT_EQ(8, pairing_delegate
.keys_entered_count_
);
2687 EXPECT_EQ(7U, pairing_delegate
.last_entered_
);
2689 EXPECT_EQ(1, callback_count_
);
2690 EXPECT_EQ(0, error_callback_count_
);
2692 // Two changes for connecting, one change for connected, one for paired,
2693 // two for trusted (after pairing and connection), and one for the reconnect
2694 // mode (IsConnectable).
2695 EXPECT_EQ(7, observer
.device_changed_count());
2696 EXPECT_EQ(device
, observer
.last_device());
2698 EXPECT_TRUE(device
->IsConnected());
2699 EXPECT_FALSE(device
->IsConnecting());
2701 EXPECT_TRUE(device
->IsPaired());
2703 // Verify is a HID device.
2704 BluetoothDevice::UUIDList uuids
= device
->GetUUIDs();
2705 ASSERT_EQ(1U, uuids
.size());
2706 EXPECT_EQ(uuids
[0], BluetoothUUID("1124"));
2708 // And usually not connectable.
2709 EXPECT_FALSE(device
->IsConnectable());
2711 // Make sure the trusted property has been set to true.
2712 FakeBluetoothDeviceClient::Properties
* properties
=
2713 fake_bluetooth_device_client_
->GetProperties(
2714 dbus::ObjectPath(FakeBluetoothDeviceClient::kDisplayPasskeyPath
));
2715 EXPECT_TRUE(properties
->trusted
.value());
2718 TEST_F(BluetoothChromeOSTest
, PairRequestPinCode
) {
2719 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
2724 // Requires that the user enters a PIN for them.
2725 BluetoothDevice
* device
= adapter_
->GetDevice(
2726 FakeBluetoothDeviceClient::kRequestPinCodeAddress
);
2727 ASSERT_TRUE(device
!= nullptr);
2728 ASSERT_FALSE(device
->IsPaired());
2730 TestBluetoothAdapterObserver
observer(adapter_
);
2732 TestPairingDelegate pairing_delegate
;
2733 device
->Connect(&pairing_delegate
, GetCallback(),
2734 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
2735 base::Unretained(this)));
2737 EXPECT_EQ(1, pairing_delegate
.call_count_
);
2738 EXPECT_EQ(1, pairing_delegate
.request_pincode_count_
);
2739 EXPECT_TRUE(device
->IsConnecting());
2742 device
->SetPinCode("1234");
2743 message_loop_
.Run();
2745 EXPECT_EQ(1, callback_count_
);
2746 EXPECT_EQ(0, error_callback_count_
);
2748 // Two changes for connecting, one change for connected, one for paired and
2749 // two for trusted (after pairing and connection).
2750 EXPECT_EQ(6, observer
.device_changed_count());
2751 EXPECT_EQ(device
, observer
.last_device());
2753 EXPECT_TRUE(device
->IsConnected());
2754 EXPECT_FALSE(device
->IsConnecting());
2756 EXPECT_TRUE(device
->IsPaired());
2758 // Verify is not a HID device.
2759 BluetoothDevice::UUIDList uuids
= device
->GetUUIDs();
2760 ASSERT_EQ(0U, uuids
.size());
2762 // Non HID devices are always connectable.
2763 EXPECT_TRUE(device
->IsConnectable());
2765 // Make sure the trusted property has been set to true.
2766 FakeBluetoothDeviceClient::Properties
* properties
=
2767 fake_bluetooth_device_client_
->GetProperties(
2768 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath
));
2769 EXPECT_TRUE(properties
->trusted
.value());
2772 TEST_F(BluetoothChromeOSTest
, PairConfirmPasskey
) {
2773 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
2778 // Requests that we confirm a displayed passkey.
2779 BluetoothDevice
* device
= adapter_
->GetDevice(
2780 FakeBluetoothDeviceClient::kConfirmPasskeyAddress
);
2781 ASSERT_TRUE(device
!= nullptr);
2782 ASSERT_FALSE(device
->IsPaired());
2784 TestBluetoothAdapterObserver
observer(adapter_
);
2786 TestPairingDelegate pairing_delegate
;
2787 device
->Connect(&pairing_delegate
, GetCallback(),
2788 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
2789 base::Unretained(this)));
2791 EXPECT_EQ(1, pairing_delegate
.call_count_
);
2792 EXPECT_EQ(1, pairing_delegate
.confirm_passkey_count_
);
2793 EXPECT_EQ(123456U, pairing_delegate
.last_passkey_
);
2794 EXPECT_TRUE(device
->IsConnecting());
2796 // Confirm the passkey.
2797 device
->ConfirmPairing();
2798 message_loop_
.Run();
2800 EXPECT_EQ(1, callback_count_
);
2801 EXPECT_EQ(0, error_callback_count_
);
2803 // Two changes for connecting, one change for connected, one for paired and
2804 // two for trusted (after pairing and connection).
2805 EXPECT_EQ(6, observer
.device_changed_count());
2806 EXPECT_EQ(device
, observer
.last_device());
2808 EXPECT_TRUE(device
->IsConnected());
2809 EXPECT_FALSE(device
->IsConnecting());
2811 EXPECT_TRUE(device
->IsPaired());
2813 // Non HID devices are always connectable.
2814 EXPECT_TRUE(device
->IsConnectable());
2816 // Make sure the trusted property has been set to true.
2817 FakeBluetoothDeviceClient::Properties
* properties
=
2818 fake_bluetooth_device_client_
->GetProperties(
2819 dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath
));
2820 EXPECT_TRUE(properties
->trusted
.value());
2823 TEST_F(BluetoothChromeOSTest
, PairRequestPasskey
) {
2824 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
2829 // Requires that the user enters a Passkey, this would be some kind of
2830 // device that has a display, but doesn't use "just works" - maybe a car?
2831 BluetoothDevice
* device
= adapter_
->GetDevice(
2832 FakeBluetoothDeviceClient::kRequestPasskeyAddress
);
2833 ASSERT_TRUE(device
!= nullptr);
2834 ASSERT_FALSE(device
->IsPaired());
2836 TestBluetoothAdapterObserver
observer(adapter_
);
2838 TestPairingDelegate pairing_delegate
;
2839 device
->Connect(&pairing_delegate
, GetCallback(),
2840 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
2841 base::Unretained(this)));
2843 EXPECT_EQ(1, pairing_delegate
.call_count_
);
2844 EXPECT_EQ(1, pairing_delegate
.request_passkey_count_
);
2845 EXPECT_TRUE(device
->IsConnecting());
2848 device
->SetPasskey(1234);
2849 message_loop_
.Run();
2851 EXPECT_EQ(1, callback_count_
);
2852 EXPECT_EQ(0, error_callback_count_
);
2854 // Two changes for connecting, one change for connected, one for paired and
2855 // two for trusted (after pairing and connection).
2856 EXPECT_EQ(6, observer
.device_changed_count());
2857 EXPECT_EQ(device
, observer
.last_device());
2859 EXPECT_TRUE(device
->IsConnected());
2860 EXPECT_FALSE(device
->IsConnecting());
2862 EXPECT_TRUE(device
->IsPaired());
2864 // Non HID devices are always connectable.
2865 EXPECT_TRUE(device
->IsConnectable());
2867 // Make sure the trusted property has been set to true.
2868 FakeBluetoothDeviceClient::Properties
* properties
=
2869 fake_bluetooth_device_client_
->GetProperties(
2870 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath
));
2871 EXPECT_TRUE(properties
->trusted
.value());
2874 TEST_F(BluetoothChromeOSTest
, PairJustWorks
) {
2875 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
2880 // Uses just-works pairing, since this is an outgoing pairing, no delegate
2881 // interaction is required.
2882 BluetoothDevice
* device
= adapter_
->GetDevice(
2883 FakeBluetoothDeviceClient::kJustWorksAddress
);
2884 ASSERT_TRUE(device
!= nullptr);
2885 ASSERT_FALSE(device
->IsPaired());
2887 TestBluetoothAdapterObserver
observer(adapter_
);
2889 TestPairingDelegate pairing_delegate
;
2890 device
->Connect(&pairing_delegate
, GetCallback(),
2891 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
2892 base::Unretained(this)));
2894 EXPECT_EQ(0, pairing_delegate
.call_count_
);
2896 message_loop_
.Run();
2898 EXPECT_EQ(1, callback_count_
);
2899 EXPECT_EQ(0, error_callback_count_
);
2901 // Two changes for connecting, one change for connected, one for paired and
2902 // two for trusted (after pairing and connection).
2903 EXPECT_EQ(6, observer
.device_changed_count());
2904 EXPECT_EQ(device
, observer
.last_device());
2906 EXPECT_TRUE(device
->IsConnected());
2907 EXPECT_FALSE(device
->IsConnecting());
2909 EXPECT_TRUE(device
->IsPaired());
2911 // Non HID devices are always connectable.
2912 EXPECT_TRUE(device
->IsConnectable());
2914 // Make sure the trusted property has been set to true.
2915 FakeBluetoothDeviceClient::Properties
* properties
=
2916 fake_bluetooth_device_client_
->GetProperties(
2917 dbus::ObjectPath(FakeBluetoothDeviceClient::kJustWorksPath
));
2918 EXPECT_TRUE(properties
->trusted
.value());
2921 TEST_F(BluetoothChromeOSTest
, PairUnpairableDeviceFails
) {
2922 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
2925 DiscoverDevice(FakeBluetoothDeviceClient::kUnconnectableDeviceAddress
);
2927 BluetoothDevice
* device
= adapter_
->GetDevice(
2928 FakeBluetoothDeviceClient::kUnpairableDeviceAddress
);
2929 ASSERT_TRUE(device
!= nullptr);
2930 ASSERT_FALSE(device
->IsPaired());
2932 TestBluetoothAdapterObserver
observer(adapter_
);
2934 TestPairingDelegate pairing_delegate
;
2935 device
->Connect(&pairing_delegate
, GetCallback(),
2936 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
2937 base::Unretained(this)));
2939 EXPECT_EQ(0, pairing_delegate
.call_count_
);
2940 EXPECT_TRUE(device
->IsConnecting());
2942 // Run the loop to get the error..
2943 message_loop_
.Run();
2945 EXPECT_EQ(0, callback_count_
);
2946 EXPECT_EQ(1, error_callback_count_
);
2948 EXPECT_EQ(BluetoothDevice::ERROR_FAILED
, last_connect_error_
);
2950 EXPECT_FALSE(device
->IsConnected());
2951 EXPECT_FALSE(device
->IsConnecting());
2952 EXPECT_FALSE(device
->IsPaired());
2955 TEST_F(BluetoothChromeOSTest
, PairingFails
) {
2956 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
2959 DiscoverDevice(FakeBluetoothDeviceClient::kVanishingDeviceAddress
);
2961 // The vanishing device times out during pairing
2962 BluetoothDevice
* device
= adapter_
->GetDevice(
2963 FakeBluetoothDeviceClient::kVanishingDeviceAddress
);
2964 ASSERT_TRUE(device
!= nullptr);
2965 ASSERT_FALSE(device
->IsPaired());
2967 TestBluetoothAdapterObserver
observer(adapter_
);
2969 TestPairingDelegate pairing_delegate
;
2970 device
->Connect(&pairing_delegate
, GetCallback(),
2971 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
2972 base::Unretained(this)));
2974 EXPECT_EQ(0, pairing_delegate
.call_count_
);
2975 EXPECT_TRUE(device
->IsConnecting());
2977 // Run the loop to get the error..
2978 message_loop_
.Run();
2980 EXPECT_EQ(0, callback_count_
);
2981 EXPECT_EQ(1, error_callback_count_
);
2983 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_TIMEOUT
, last_connect_error_
);
2985 EXPECT_FALSE(device
->IsConnected());
2986 EXPECT_FALSE(device
->IsConnecting());
2987 EXPECT_FALSE(device
->IsPaired());
2990 TEST_F(BluetoothChromeOSTest
, PairingFailsAtConnection
) {
2991 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
2996 // Everything seems to go according to plan with the unconnectable device;
2997 // it pairs, but then you can't make connections to it after.
2998 BluetoothDevice
* device
= adapter_
->GetDevice(
2999 FakeBluetoothDeviceClient::kUnconnectableDeviceAddress
);
3000 ASSERT_TRUE(device
!= nullptr);
3001 ASSERT_FALSE(device
->IsPaired());
3003 TestBluetoothAdapterObserver
observer(adapter_
);
3005 TestPairingDelegate pairing_delegate
;
3006 device
->Connect(&pairing_delegate
, GetCallback(),
3007 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
3008 base::Unretained(this)));
3010 EXPECT_EQ(0, pairing_delegate
.call_count_
);
3011 EXPECT_TRUE(device
->IsConnecting());
3013 message_loop_
.Run();
3015 EXPECT_EQ(0, callback_count_
);
3016 EXPECT_EQ(1, error_callback_count_
);
3017 EXPECT_EQ(BluetoothDevice::ERROR_FAILED
, last_connect_error_
);
3019 // Two changes for connecting, one for paired and one for trusted after
3020 // pairing. The device should not be connected.
3021 EXPECT_EQ(4, observer
.device_changed_count());
3022 EXPECT_EQ(device
, observer
.last_device());
3024 EXPECT_FALSE(device
->IsConnected());
3025 EXPECT_FALSE(device
->IsConnecting());
3027 EXPECT_TRUE(device
->IsPaired());
3029 // Make sure the trusted property has been set to true still (since pairing
3031 FakeBluetoothDeviceClient::Properties
* properties
=
3032 fake_bluetooth_device_client_
->GetProperties(
3034 FakeBluetoothDeviceClient::kUnconnectableDevicePath
));
3035 EXPECT_TRUE(properties
->trusted
.value());
3038 TEST_F(BluetoothChromeOSTest
, PairingRejectedAtPinCode
) {
3039 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
3044 // Reject the pairing after we receive a request for the PIN code.
3045 BluetoothDevice
* device
= adapter_
->GetDevice(
3046 FakeBluetoothDeviceClient::kRequestPinCodeAddress
);
3047 ASSERT_TRUE(device
!= nullptr);
3048 ASSERT_FALSE(device
->IsPaired());
3050 TestBluetoothAdapterObserver
observer(adapter_
);
3052 TestPairingDelegate pairing_delegate
;
3053 device
->Connect(&pairing_delegate
, GetCallback(),
3054 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
3055 base::Unretained(this)));
3057 EXPECT_EQ(1, pairing_delegate
.call_count_
);
3058 EXPECT_EQ(1, pairing_delegate
.request_pincode_count_
);
3059 EXPECT_TRUE(device
->IsConnecting());
3061 // Reject the pairing.
3062 device
->RejectPairing();
3063 message_loop_
.Run();
3065 EXPECT_EQ(0, callback_count_
);
3066 EXPECT_EQ(1, error_callback_count_
);
3067 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_REJECTED
, last_connect_error_
);
3069 // Should be no changes except connecting going true and false.
3070 EXPECT_EQ(2, observer
.device_changed_count());
3071 EXPECT_FALSE(device
->IsConnected());
3072 EXPECT_FALSE(device
->IsConnecting());
3073 EXPECT_FALSE(device
->IsPaired());
3076 TEST_F(BluetoothChromeOSTest
, PairingCancelledAtPinCode
) {
3077 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
3082 // Cancel the pairing after we receive a request for the PIN code.
3083 BluetoothDevice
* device
= adapter_
->GetDevice(
3084 FakeBluetoothDeviceClient::kRequestPinCodeAddress
);
3085 ASSERT_TRUE(device
!= nullptr);
3086 ASSERT_FALSE(device
->IsPaired());
3088 TestBluetoothAdapterObserver
observer(adapter_
);
3090 TestPairingDelegate pairing_delegate
;
3091 device
->Connect(&pairing_delegate
, GetCallback(),
3092 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
3093 base::Unretained(this)));
3095 EXPECT_EQ(1, pairing_delegate
.call_count_
);
3096 EXPECT_EQ(1, pairing_delegate
.request_pincode_count_
);
3097 EXPECT_TRUE(device
->IsConnecting());
3099 // Cancel the pairing.
3100 device
->CancelPairing();
3101 message_loop_
.Run();
3103 EXPECT_EQ(0, callback_count_
);
3104 EXPECT_EQ(1, error_callback_count_
);
3105 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED
, last_connect_error_
);
3107 // Should be no changes except connecting going true and false.
3108 EXPECT_EQ(2, observer
.device_changed_count());
3109 EXPECT_FALSE(device
->IsConnected());
3110 EXPECT_FALSE(device
->IsConnecting());
3111 EXPECT_FALSE(device
->IsPaired());
3114 TEST_F(BluetoothChromeOSTest
, PairingRejectedAtPasskey
) {
3115 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
3120 // Reject the pairing after we receive a request for the passkey.
3121 BluetoothDevice
* device
= adapter_
->GetDevice(
3122 FakeBluetoothDeviceClient::kRequestPasskeyAddress
);
3123 ASSERT_TRUE(device
!= nullptr);
3124 ASSERT_FALSE(device
->IsPaired());
3126 TestBluetoothAdapterObserver
observer(adapter_
);
3128 TestPairingDelegate pairing_delegate
;
3129 device
->Connect(&pairing_delegate
, GetCallback(),
3130 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
3131 base::Unretained(this)));
3133 EXPECT_EQ(1, pairing_delegate
.call_count_
);
3134 EXPECT_EQ(1, pairing_delegate
.request_passkey_count_
);
3135 EXPECT_TRUE(device
->IsConnecting());
3137 // Reject the pairing.
3138 device
->RejectPairing();
3139 message_loop_
.Run();
3141 EXPECT_EQ(0, callback_count_
);
3142 EXPECT_EQ(1, error_callback_count_
);
3143 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_REJECTED
, last_connect_error_
);
3145 // Should be no changes except connecting going true and false.
3146 EXPECT_EQ(2, observer
.device_changed_count());
3147 EXPECT_FALSE(device
->IsConnected());
3148 EXPECT_FALSE(device
->IsConnecting());
3149 EXPECT_FALSE(device
->IsPaired());
3152 TEST_F(BluetoothChromeOSTest
, PairingCancelledAtPasskey
) {
3153 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
3158 // Cancel the pairing after we receive a request for the passkey.
3159 BluetoothDevice
* device
= adapter_
->GetDevice(
3160 FakeBluetoothDeviceClient::kRequestPasskeyAddress
);
3161 ASSERT_TRUE(device
!= nullptr);
3162 ASSERT_FALSE(device
->IsPaired());
3164 TestBluetoothAdapterObserver
observer(adapter_
);
3166 TestPairingDelegate pairing_delegate
;
3167 device
->Connect(&pairing_delegate
, GetCallback(),
3168 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
3169 base::Unretained(this)));
3171 EXPECT_EQ(1, pairing_delegate
.call_count_
);
3172 EXPECT_EQ(1, pairing_delegate
.request_passkey_count_
);
3173 EXPECT_TRUE(device
->IsConnecting());
3175 // Cancel the pairing.
3176 device
->CancelPairing();
3177 message_loop_
.Run();
3179 EXPECT_EQ(0, callback_count_
);
3180 EXPECT_EQ(1, error_callback_count_
);
3181 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED
, last_connect_error_
);
3183 // Should be no changes except connecting going true and false.
3184 EXPECT_EQ(2, observer
.device_changed_count());
3185 EXPECT_FALSE(device
->IsConnected());
3186 EXPECT_FALSE(device
->IsConnecting());
3187 EXPECT_FALSE(device
->IsPaired());
3190 TEST_F(BluetoothChromeOSTest
, PairingRejectedAtConfirmation
) {
3191 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
3196 // Reject the pairing after we receive a request for passkey confirmation.
3197 BluetoothDevice
* device
= adapter_
->GetDevice(
3198 FakeBluetoothDeviceClient::kConfirmPasskeyAddress
);
3199 ASSERT_TRUE(device
!= nullptr);
3200 ASSERT_FALSE(device
->IsPaired());
3202 TestBluetoothAdapterObserver
observer(adapter_
);
3204 TestPairingDelegate pairing_delegate
;
3205 device
->Connect(&pairing_delegate
, GetCallback(),
3206 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
3207 base::Unretained(this)));
3209 EXPECT_EQ(1, pairing_delegate
.call_count_
);
3210 EXPECT_EQ(1, pairing_delegate
.confirm_passkey_count_
);
3211 EXPECT_TRUE(device
->IsConnecting());
3213 // Reject the pairing.
3214 device
->RejectPairing();
3215 message_loop_
.Run();
3217 EXPECT_EQ(0, callback_count_
);
3218 EXPECT_EQ(1, error_callback_count_
);
3219 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_REJECTED
, last_connect_error_
);
3221 // Should be no changes except connecting going true and false.
3222 EXPECT_EQ(2, observer
.device_changed_count());
3223 EXPECT_FALSE(device
->IsConnected());
3224 EXPECT_FALSE(device
->IsConnecting());
3225 EXPECT_FALSE(device
->IsPaired());
3228 TEST_F(BluetoothChromeOSTest
, PairingCancelledAtConfirmation
) {
3229 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
3234 // Cancel the pairing after we receive a request for the passkey.
3235 BluetoothDevice
* device
= adapter_
->GetDevice(
3236 FakeBluetoothDeviceClient::kConfirmPasskeyAddress
);
3237 ASSERT_TRUE(device
!= nullptr);
3238 ASSERT_FALSE(device
->IsPaired());
3240 TestBluetoothAdapterObserver
observer(adapter_
);
3242 TestPairingDelegate pairing_delegate
;
3243 device
->Connect(&pairing_delegate
, GetCallback(),
3244 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
3245 base::Unretained(this)));
3247 EXPECT_EQ(1, pairing_delegate
.call_count_
);
3248 EXPECT_EQ(1, pairing_delegate
.confirm_passkey_count_
);
3249 EXPECT_TRUE(device
->IsConnecting());
3251 // Cancel the pairing.
3252 device
->CancelPairing();
3253 message_loop_
.Run();
3255 EXPECT_EQ(0, callback_count_
);
3256 EXPECT_EQ(1, error_callback_count_
);
3257 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED
, last_connect_error_
);
3259 // Should be no changes except connecting going true and false.
3260 EXPECT_EQ(2, observer
.device_changed_count());
3261 EXPECT_FALSE(device
->IsConnected());
3262 EXPECT_FALSE(device
->IsConnecting());
3263 EXPECT_FALSE(device
->IsPaired());
3266 TEST_F(BluetoothChromeOSTest
, PairingCancelledInFlight
) {
3267 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
3272 // Cancel the pairing while we're waiting for the remote host.
3273 BluetoothDevice
* device
= adapter_
->GetDevice(
3274 FakeBluetoothDeviceClient::kLegacyAutopairAddress
);
3275 ASSERT_TRUE(device
!= nullptr);
3276 ASSERT_FALSE(device
->IsPaired());
3278 TestBluetoothAdapterObserver
observer(adapter_
);
3280 TestPairingDelegate pairing_delegate
;
3281 device
->Connect(&pairing_delegate
, GetCallback(),
3282 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
3283 base::Unretained(this)));
3285 EXPECT_EQ(0, pairing_delegate
.call_count_
);
3286 EXPECT_TRUE(device
->IsConnecting());
3288 // Cancel the pairing.
3289 device
->CancelPairing();
3290 message_loop_
.Run();
3292 EXPECT_EQ(0, callback_count_
);
3293 EXPECT_EQ(1, error_callback_count_
);
3294 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED
, last_connect_error_
);
3296 // Should be no changes except connecting going true and false.
3297 EXPECT_EQ(2, observer
.device_changed_count());
3298 EXPECT_FALSE(device
->IsConnected());
3299 EXPECT_FALSE(device
->IsConnecting());
3300 EXPECT_FALSE(device
->IsPaired());
3303 TEST_F(BluetoothChromeOSTest
, IncomingPairRequestPinCode
) {
3304 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
3308 TestPairingDelegate pairing_delegate
;
3309 adapter_
->AddPairingDelegate(
3311 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH
);
3313 // Requires that we provide a PIN code.
3314 fake_bluetooth_device_client_
->CreateDevice(
3315 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
),
3316 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath
));
3317 BluetoothDevice
* device
= adapter_
->GetDevice(
3318 FakeBluetoothDeviceClient::kRequestPinCodeAddress
);
3319 ASSERT_TRUE(device
!= nullptr);
3320 ASSERT_FALSE(device
->IsPaired());
3322 TestBluetoothAdapterObserver
observer(adapter_
);
3324 fake_bluetooth_device_client_
->SimulatePairing(
3325 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath
), true,
3326 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback
,
3327 base::Unretained(this)));
3329 EXPECT_EQ(1, pairing_delegate
.call_count_
);
3330 EXPECT_EQ(1, pairing_delegate
.request_pincode_count_
);
3333 device
->SetPinCode("1234");
3334 message_loop_
.Run();
3336 EXPECT_EQ(1, callback_count_
);
3337 EXPECT_EQ(0, error_callback_count_
);
3339 // One change for paired, and one for trusted.
3340 EXPECT_EQ(2, observer
.device_changed_count());
3341 EXPECT_EQ(device
, observer
.last_device());
3343 EXPECT_TRUE(device
->IsPaired());
3345 // Make sure the trusted property has been set to true.
3346 FakeBluetoothDeviceClient::Properties
* properties
=
3347 fake_bluetooth_device_client_
->GetProperties(
3348 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath
));
3349 ASSERT_TRUE(properties
->trusted
.value());
3351 // No pairing context should remain on the device.
3352 BluetoothDeviceChromeOS
* device_chromeos
=
3353 static_cast<BluetoothDeviceChromeOS
*>(device
);
3354 EXPECT_TRUE(device_chromeos
->GetPairing() == nullptr);
3357 TEST_F(BluetoothChromeOSTest
, IncomingPairConfirmPasskey
) {
3358 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
3362 TestPairingDelegate pairing_delegate
;
3363 adapter_
->AddPairingDelegate(
3365 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH
);
3367 // Requests that we confirm a displayed passkey.
3368 fake_bluetooth_device_client_
->CreateDevice(
3369 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
),
3370 dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath
));
3371 BluetoothDevice
* device
= adapter_
->GetDevice(
3372 FakeBluetoothDeviceClient::kConfirmPasskeyAddress
);
3373 ASSERT_TRUE(device
!= nullptr);
3374 ASSERT_FALSE(device
->IsPaired());
3376 TestBluetoothAdapterObserver
observer(adapter_
);
3378 fake_bluetooth_device_client_
->SimulatePairing(
3379 dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath
), true,
3380 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback
,
3381 base::Unretained(this)));
3383 EXPECT_EQ(1, pairing_delegate
.call_count_
);
3384 EXPECT_EQ(1, pairing_delegate
.confirm_passkey_count_
);
3385 EXPECT_EQ(123456U, pairing_delegate
.last_passkey_
);
3387 // Confirm the passkey.
3388 device
->ConfirmPairing();
3389 message_loop_
.Run();
3391 EXPECT_EQ(1, callback_count_
);
3392 EXPECT_EQ(0, error_callback_count_
);
3394 // One change for paired, and one for trusted.
3395 EXPECT_EQ(2, observer
.device_changed_count());
3396 EXPECT_EQ(device
, observer
.last_device());
3398 EXPECT_TRUE(device
->IsPaired());
3400 // Make sure the trusted property has been set to true.
3401 FakeBluetoothDeviceClient::Properties
* properties
=
3402 fake_bluetooth_device_client_
->GetProperties(
3403 dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath
));
3404 ASSERT_TRUE(properties
->trusted
.value());
3406 // No pairing context should remain on the device.
3407 BluetoothDeviceChromeOS
* device_chromeos
=
3408 static_cast<BluetoothDeviceChromeOS
*>(device
);
3409 EXPECT_TRUE(device_chromeos
->GetPairing() == nullptr);
3412 TEST_F(BluetoothChromeOSTest
, IncomingPairRequestPasskey
) {
3413 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
3417 TestPairingDelegate pairing_delegate
;
3418 adapter_
->AddPairingDelegate(
3420 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH
);
3422 // Requests that we provide a Passkey.
3423 fake_bluetooth_device_client_
->CreateDevice(
3424 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
),
3425 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath
));
3426 BluetoothDevice
* device
= adapter_
->GetDevice(
3427 FakeBluetoothDeviceClient::kRequestPasskeyAddress
);
3428 ASSERT_TRUE(device
!= nullptr);
3429 ASSERT_FALSE(device
->IsPaired());
3431 TestBluetoothAdapterObserver
observer(adapter_
);
3433 fake_bluetooth_device_client_
->SimulatePairing(
3434 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath
), true,
3435 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback
,
3436 base::Unretained(this)));
3438 EXPECT_EQ(1, pairing_delegate
.call_count_
);
3439 EXPECT_EQ(1, pairing_delegate
.request_passkey_count_
);
3442 device
->SetPasskey(1234);
3443 message_loop_
.Run();
3445 EXPECT_EQ(1, callback_count_
);
3446 EXPECT_EQ(0, error_callback_count_
);
3448 // One change for paired, and one for trusted.
3449 EXPECT_EQ(2, observer
.device_changed_count());
3450 EXPECT_EQ(device
, observer
.last_device());
3452 EXPECT_TRUE(device
->IsPaired());
3454 // Make sure the trusted property has been set to true.
3455 FakeBluetoothDeviceClient::Properties
* properties
=
3456 fake_bluetooth_device_client_
->GetProperties(
3457 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath
));
3458 ASSERT_TRUE(properties
->trusted
.value());
3460 // No pairing context should remain on the device.
3461 BluetoothDeviceChromeOS
* device_chromeos
=
3462 static_cast<BluetoothDeviceChromeOS
*>(device
);
3463 EXPECT_TRUE(device_chromeos
->GetPairing() == nullptr);
3466 TEST_F(BluetoothChromeOSTest
, IncomingPairJustWorks
) {
3467 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
3471 TestPairingDelegate pairing_delegate
;
3472 adapter_
->AddPairingDelegate(
3474 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH
);
3476 // Uses just-works pairing so, sinec this an incoming pairing, require
3477 // authorization from the user.
3478 fake_bluetooth_device_client_
->CreateDevice(
3479 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
),
3480 dbus::ObjectPath(FakeBluetoothDeviceClient::kJustWorksPath
));
3481 BluetoothDevice
* device
= adapter_
->GetDevice(
3482 FakeBluetoothDeviceClient::kJustWorksAddress
);
3483 ASSERT_TRUE(device
!= nullptr);
3484 ASSERT_FALSE(device
->IsPaired());
3486 TestBluetoothAdapterObserver
observer(adapter_
);
3488 fake_bluetooth_device_client_
->SimulatePairing(
3489 dbus::ObjectPath(FakeBluetoothDeviceClient::kJustWorksPath
), true,
3490 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback
,
3491 base::Unretained(this)));
3493 EXPECT_EQ(1, pairing_delegate
.call_count_
);
3494 EXPECT_EQ(1, pairing_delegate
.authorize_pairing_count_
);
3496 // Confirm the pairing.
3497 device
->ConfirmPairing();
3498 message_loop_
.Run();
3500 EXPECT_EQ(1, callback_count_
);
3501 EXPECT_EQ(0, error_callback_count_
);
3503 // One change for paired, and one for trusted.
3504 EXPECT_EQ(2, observer
.device_changed_count());
3505 EXPECT_EQ(device
, observer
.last_device());
3507 EXPECT_TRUE(device
->IsPaired());
3509 // Make sure the trusted property has been set to true.
3510 FakeBluetoothDeviceClient::Properties
* properties
=
3511 fake_bluetooth_device_client_
->GetProperties(
3512 dbus::ObjectPath(FakeBluetoothDeviceClient::kJustWorksPath
));
3513 ASSERT_TRUE(properties
->trusted
.value());
3515 // No pairing context should remain on the device.
3516 BluetoothDeviceChromeOS
* device_chromeos
=
3517 static_cast<BluetoothDeviceChromeOS
*>(device
);
3518 EXPECT_TRUE(device_chromeos
->GetPairing() == nullptr);
3521 TEST_F(BluetoothChromeOSTest
, IncomingPairRequestPinCodeWithoutDelegate
) {
3522 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
3526 // Requires that we provide a PIN Code, without a pairing delegate,
3527 // that will be rejected.
3528 fake_bluetooth_device_client_
->CreateDevice(
3529 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
),
3530 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath
));
3531 BluetoothDevice
* device
= adapter_
->GetDevice(
3532 FakeBluetoothDeviceClient::kRequestPinCodeAddress
);
3533 ASSERT_TRUE(device
!= nullptr);
3534 ASSERT_FALSE(device
->IsPaired());
3536 TestBluetoothAdapterObserver
observer(adapter_
);
3538 fake_bluetooth_device_client_
->SimulatePairing(
3539 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath
), true,
3540 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback
,
3541 base::Unretained(this)));
3543 message_loop_
.Run();
3545 EXPECT_EQ(0, callback_count_
);
3546 EXPECT_EQ(1, error_callback_count_
);
3547 EXPECT_EQ(bluetooth_device::kErrorAuthenticationRejected
, last_client_error_
);
3549 // No changes should be observer.
3550 EXPECT_EQ(0, observer
.device_changed_count());
3552 EXPECT_FALSE(device
->IsPaired());
3554 // No pairing context should remain on the device.
3555 BluetoothDeviceChromeOS
* device_chromeos
=
3556 static_cast<BluetoothDeviceChromeOS
*>(device
);
3557 EXPECT_TRUE(device_chromeos
->GetPairing() == nullptr);
3560 TEST_F(BluetoothChromeOSTest
, IncomingPairConfirmPasskeyWithoutDelegate
) {
3561 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
3565 // Requests that we confirm a displayed passkey, without a pairing delegate,
3566 // that will be rejected.
3567 fake_bluetooth_device_client_
->CreateDevice(
3568 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
),
3569 dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath
));
3570 BluetoothDevice
* device
= adapter_
->GetDevice(
3571 FakeBluetoothDeviceClient::kConfirmPasskeyAddress
);
3572 ASSERT_TRUE(device
!= nullptr);
3573 ASSERT_FALSE(device
->IsPaired());
3575 TestBluetoothAdapterObserver
observer(adapter_
);
3577 fake_bluetooth_device_client_
->SimulatePairing(
3578 dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath
), true,
3579 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback
,
3580 base::Unretained(this)));
3582 message_loop_
.Run();
3584 EXPECT_EQ(0, callback_count_
);
3585 EXPECT_EQ(1, error_callback_count_
);
3586 EXPECT_EQ(bluetooth_device::kErrorAuthenticationRejected
, last_client_error_
);
3588 // No changes should be observer.
3589 EXPECT_EQ(0, observer
.device_changed_count());
3591 EXPECT_FALSE(device
->IsPaired());
3593 // No pairing context should remain on the device.
3594 BluetoothDeviceChromeOS
* device_chromeos
=
3595 static_cast<BluetoothDeviceChromeOS
*>(device
);
3596 EXPECT_TRUE(device_chromeos
->GetPairing() == nullptr);
3599 TEST_F(BluetoothChromeOSTest
, IncomingPairRequestPasskeyWithoutDelegate
) {
3600 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
3604 // Requests that we provide a displayed passkey, without a pairing delegate,
3605 // that will be rejected.
3606 fake_bluetooth_device_client_
->CreateDevice(
3607 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
),
3608 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath
));
3609 BluetoothDevice
* device
= adapter_
->GetDevice(
3610 FakeBluetoothDeviceClient::kRequestPasskeyAddress
);
3611 ASSERT_TRUE(device
!= nullptr);
3612 ASSERT_FALSE(device
->IsPaired());
3614 TestBluetoothAdapterObserver
observer(adapter_
);
3616 fake_bluetooth_device_client_
->SimulatePairing(
3617 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath
), true,
3618 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback
,
3619 base::Unretained(this)));
3621 message_loop_
.Run();
3623 EXPECT_EQ(0, callback_count_
);
3624 EXPECT_EQ(1, error_callback_count_
);
3625 EXPECT_EQ(bluetooth_device::kErrorAuthenticationRejected
, last_client_error_
);
3627 // No changes should be observer.
3628 EXPECT_EQ(0, observer
.device_changed_count());
3630 EXPECT_FALSE(device
->IsPaired());
3632 // No pairing context should remain on the device.
3633 BluetoothDeviceChromeOS
* device_chromeos
=
3634 static_cast<BluetoothDeviceChromeOS
*>(device
);
3635 EXPECT_TRUE(device_chromeos
->GetPairing() == nullptr);
3638 TEST_F(BluetoothChromeOSTest
, IncomingPairJustWorksWithoutDelegate
) {
3639 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
3643 // Uses just-works pairing and thus requires authorization for incoming
3644 // pairings, without a pairing delegate, that will be rejected.
3645 fake_bluetooth_device_client_
->CreateDevice(
3646 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
),
3647 dbus::ObjectPath(FakeBluetoothDeviceClient::kJustWorksPath
));
3648 BluetoothDevice
* device
= adapter_
->GetDevice(
3649 FakeBluetoothDeviceClient::kJustWorksAddress
);
3650 ASSERT_TRUE(device
!= nullptr);
3651 ASSERT_FALSE(device
->IsPaired());
3653 TestBluetoothAdapterObserver
observer(adapter_
);
3655 fake_bluetooth_device_client_
->SimulatePairing(
3656 dbus::ObjectPath(FakeBluetoothDeviceClient::kJustWorksPath
), true,
3657 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback
,
3658 base::Unretained(this)));
3660 message_loop_
.Run();
3662 EXPECT_EQ(0, callback_count_
);
3663 EXPECT_EQ(1, error_callback_count_
);
3664 EXPECT_EQ(bluetooth_device::kErrorAuthenticationRejected
, last_client_error_
);
3666 // No changes should be observer.
3667 EXPECT_EQ(0, observer
.device_changed_count());
3669 EXPECT_FALSE(device
->IsPaired());
3671 // No pairing context should remain on the device.
3672 BluetoothDeviceChromeOS
* device_chromeos
=
3673 static_cast<BluetoothDeviceChromeOS
*>(device
);
3674 EXPECT_TRUE(device_chromeos
->GetPairing() == nullptr);
3677 TEST_F(BluetoothChromeOSTest
, RemovePairingDelegateDuringPairing
) {
3678 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
3682 TestPairingDelegate pairing_delegate
;
3683 adapter_
->AddPairingDelegate(
3685 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH
);
3687 // Requests that we provide a Passkey.
3688 fake_bluetooth_device_client_
->CreateDevice(
3689 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
),
3690 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath
));
3691 BluetoothDevice
* device
= adapter_
->GetDevice(
3692 FakeBluetoothDeviceClient::kRequestPasskeyAddress
);
3693 ASSERT_TRUE(device
!= nullptr);
3694 ASSERT_FALSE(device
->IsPaired());
3696 TestBluetoothAdapterObserver
observer(adapter_
);
3698 fake_bluetooth_device_client_
->SimulatePairing(
3699 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath
), true,
3700 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback
,
3701 base::Unretained(this)));
3703 EXPECT_EQ(1, pairing_delegate
.call_count_
);
3704 EXPECT_EQ(1, pairing_delegate
.request_passkey_count_
);
3706 // A pairing context should now be set on the device.
3707 BluetoothDeviceChromeOS
* device_chromeos
=
3708 static_cast<BluetoothDeviceChromeOS
*>(device
);
3709 ASSERT_TRUE(device_chromeos
->GetPairing() != nullptr);
3711 // Removing the pairing delegate should remove that pairing context.
3712 adapter_
->RemovePairingDelegate(&pairing_delegate
);
3714 EXPECT_TRUE(device_chromeos
->GetPairing() == nullptr);
3716 // Set the Passkey, this should now have no effect since the pairing has
3717 // been, in-effect, cancelled
3718 device
->SetPasskey(1234);
3720 EXPECT_EQ(0, callback_count_
);
3721 EXPECT_EQ(0, error_callback_count_
);
3722 EXPECT_EQ(0, observer
.device_changed_count());
3724 EXPECT_FALSE(device
->IsPaired());
3727 TEST_F(BluetoothChromeOSTest
, DeviceId
) {
3730 // Use the built-in paired device for this test, grab its Properties
3731 // structure so we can adjust the underlying modalias property.
3732 BluetoothDevice
* device
= adapter_
->GetDevice(
3733 FakeBluetoothDeviceClient::kPairedDeviceAddress
);
3734 FakeBluetoothDeviceClient::Properties
* properties
=
3735 fake_bluetooth_device_client_
->GetProperties(
3736 dbus::ObjectPath(FakeBluetoothDeviceClient::kPairedDevicePath
));
3738 ASSERT_TRUE(device
!= nullptr);
3739 ASSERT_TRUE(properties
!= nullptr);
3741 // Valid USB IF-assigned identifier.
3742 ASSERT_EQ("usb:v05ACp030Dd0306", properties
->modalias
.value());
3744 EXPECT_EQ(BluetoothDevice::VENDOR_ID_USB
, device
->GetVendorIDSource());
3745 EXPECT_EQ(0x05ac, device
->GetVendorID());
3746 EXPECT_EQ(0x030d, device
->GetProductID());
3747 EXPECT_EQ(0x0306, device
->GetDeviceID());
3749 // Valid Bluetooth SIG-assigned identifier.
3750 properties
->modalias
.ReplaceValue("bluetooth:v00E0p2400d0400");
3752 EXPECT_EQ(BluetoothDevice::VENDOR_ID_BLUETOOTH
, device
->GetVendorIDSource());
3753 EXPECT_EQ(0x00e0, device
->GetVendorID());
3754 EXPECT_EQ(0x2400, device
->GetProductID());
3755 EXPECT_EQ(0x0400, device
->GetDeviceID());
3757 // Invalid USB IF-assigned identifier.
3758 properties
->modalias
.ReplaceValue("usb:x00E0p2400d0400");
3760 EXPECT_EQ(BluetoothDevice::VENDOR_ID_UNKNOWN
, device
->GetVendorIDSource());
3761 EXPECT_EQ(0, device
->GetVendorID());
3762 EXPECT_EQ(0, device
->GetProductID());
3763 EXPECT_EQ(0, device
->GetDeviceID());
3765 // Invalid Bluetooth SIG-assigned identifier.
3766 properties
->modalias
.ReplaceValue("bluetooth:x00E0p2400d0400");
3768 EXPECT_EQ(BluetoothDevice::VENDOR_ID_UNKNOWN
, device
->GetVendorIDSource());
3769 EXPECT_EQ(0, device
->GetVendorID());
3770 EXPECT_EQ(0, device
->GetProductID());
3771 EXPECT_EQ(0, device
->GetDeviceID());
3773 // Unknown vendor specification identifier.
3774 properties
->modalias
.ReplaceValue("chrome:v00E0p2400d0400");
3776 EXPECT_EQ(BluetoothDevice::VENDOR_ID_UNKNOWN
, device
->GetVendorIDSource());
3777 EXPECT_EQ(0, device
->GetVendorID());
3778 EXPECT_EQ(0, device
->GetProductID());
3779 EXPECT_EQ(0, device
->GetDeviceID());
3782 TEST_F(BluetoothChromeOSTest
, GetConnectionInfoForDisconnectedDevice
) {
3784 BluetoothDevice
* device
=
3785 adapter_
->GetDevice(FakeBluetoothDeviceClient::kPairedDeviceAddress
);
3787 // Calling GetConnectionInfo for an unconnected device should return a result
3788 // in which all fields are filled with BluetoothDevice::kUnknownPower.
3789 BluetoothDevice::ConnectionInfo
conn_info(0, 0, 0);
3790 device
->GetConnectionInfo(base::Bind(&SaveConnectionInfo
, &conn_info
));
3791 int unknown_power
= BluetoothDevice::kUnknownPower
;
3792 EXPECT_NE(0, unknown_power
);
3793 EXPECT_EQ(unknown_power
, conn_info
.rssi
);
3794 EXPECT_EQ(unknown_power
, conn_info
.transmit_power
);
3795 EXPECT_EQ(unknown_power
, conn_info
.max_transmit_power
);
3798 TEST_F(BluetoothChromeOSTest
, GetConnectionInfoForConnectedDevice
) {
3800 BluetoothDevice
* device
=
3801 adapter_
->GetDevice(FakeBluetoothDeviceClient::kPairedDeviceAddress
);
3803 device
->Connect(nullptr, GetCallback(),
3804 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
3805 base::Unretained(this)));
3806 EXPECT_TRUE(device
->IsConnected());
3808 // Calling GetConnectionInfo for a connected device should return valid
3810 fake_bluetooth_device_client_
->UpdateConnectionInfo(-10, 3, 4);
3811 BluetoothDevice::ConnectionInfo conn_info
;
3812 device
->GetConnectionInfo(base::Bind(&SaveConnectionInfo
, &conn_info
));
3813 EXPECT_EQ(-10, conn_info
.rssi
);
3814 EXPECT_EQ(3, conn_info
.transmit_power
);
3815 EXPECT_EQ(4, conn_info
.max_transmit_power
);
3818 // Verifies Shutdown shuts down the adapter as expected.
3819 TEST_F(BluetoothChromeOSTest
, Shutdown
) {
3820 // Set up adapter. Set powered & discoverable, start discovery.
3822 adapter_
->SetPowered(true, GetCallback(), GetErrorCallback());
3823 adapter_
->SetDiscoverable(true, GetCallback(), GetErrorCallback());
3824 adapter_
->StartDiscoverySession(
3825 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
3826 base::Unretained(this)),
3827 GetErrorCallback());
3828 base::MessageLoop::current()->Run();
3829 ASSERT_EQ(3, callback_count_
);
3830 ASSERT_EQ(0, error_callback_count_
);
3831 callback_count_
= 0;
3833 TestPairingDelegate pairing_delegate
;
3834 adapter_
->AddPairingDelegate(
3835 &pairing_delegate
, BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH
);
3837 // Validate running adapter state.
3838 EXPECT_NE("", adapter_
->GetAddress());
3839 EXPECT_NE("", adapter_
->GetName());
3840 EXPECT_TRUE(adapter_
->IsInitialized());
3841 EXPECT_TRUE(adapter_
->IsPresent());
3842 EXPECT_TRUE(adapter_
->IsPowered());
3843 EXPECT_TRUE(adapter_
->IsDiscoverable());
3844 EXPECT_TRUE(adapter_
->IsDiscovering());
3845 EXPECT_EQ(2U, adapter_
->GetDevices().size());
3846 EXPECT_NE(nullptr, adapter_
->GetDevice(
3847 FakeBluetoothDeviceClient::kPairedDeviceAddress
));
3848 EXPECT_NE(dbus::ObjectPath(""), static_cast<BluetoothAdapterChromeOS
*>(
3849 adapter_
.get())->object_path());
3852 adapter_
->Shutdown();
3854 // Validate post shutdown state by calling all BluetoothAdapterChromeOS
3855 // members, in declaration order:
3857 adapter_
->Shutdown();
3858 // DeleteOnCorrectThread omitted as we don't want to delete in this test.
3860 TestBluetoothAdapterObserver
observer(adapter_
); // Calls AddObserver
3861 } // ~TestBluetoothAdapterObserver calls RemoveObserver.
3862 EXPECT_EQ("", adapter_
->GetAddress());
3863 EXPECT_EQ("", adapter_
->GetName());
3865 adapter_
->SetName("", GetCallback(), GetErrorCallback());
3866 EXPECT_EQ(0, callback_count_
);
3867 EXPECT_EQ(1, error_callback_count_
--) << "SetName error";
3869 EXPECT_TRUE(adapter_
->IsInitialized());
3870 EXPECT_FALSE(adapter_
->IsPresent());
3871 EXPECT_FALSE(adapter_
->IsPowered());
3873 adapter_
->SetPowered(true, GetCallback(), GetErrorCallback());
3874 EXPECT_EQ(0, callback_count_
);
3875 EXPECT_EQ(1, error_callback_count_
--) << "SetPowered error";
3877 EXPECT_FALSE(adapter_
->IsDiscoverable());
3879 adapter_
->SetDiscoverable(true, GetCallback(), GetErrorCallback());
3880 EXPECT_EQ(0, callback_count_
);
3881 EXPECT_EQ(1, error_callback_count_
--) << "SetDiscoverable error";
3883 EXPECT_FALSE(adapter_
->IsDiscovering());
3884 // CreateRfcommService will DCHECK after Shutdown().
3885 // CreateL2capService will DCHECK after Shutdown().
3887 BluetoothAudioSink::Options audio_sink_options
;
3888 adapter_
->RegisterAudioSink(
3890 base::Bind(&BluetoothChromeOSTest::AudioSinkAcquiredCallback
,
3891 base::Unretained(this)),
3892 base::Bind(&BluetoothChromeOSTest::AudioSinkErrorCallback
,
3893 base::Unretained(this)));
3894 EXPECT_EQ(0, callback_count_
);
3895 EXPECT_EQ(1, error_callback_count_
--) << "RegisterAudioSink error";
3897 BluetoothAdapterChromeOS
* adapter_chrome_os
=
3898 static_cast<BluetoothAdapterChromeOS
*>(adapter_
.get());
3900 adapter_chrome_os
->GetDeviceWithPath(dbus::ObjectPath("")));
3902 // Notify methods presume objects exist that are owned by the adapter and
3903 // destroyed in Shutdown(). Mocks are not attempted here that won't exist,
3904 // as verified below by EXPECT_EQ(0U, adapter_->GetDevices().size());
3905 // NotifyDeviceChanged
3906 // NotifyGattServiceAdded
3907 // NotifyGattServiceRemoved
3908 // NotifyGattServiceChanged
3909 // NotifyGattDiscoveryComplete
3910 // NotifyGattCharacteristicAdded
3911 // NotifyGattCharacteristicRemoved
3912 // NotifyGattDescriptorAdded
3913 // NotifyGattDescriptorRemoved
3914 // NotifyGattCharacteristicValueChanged
3915 // NotifyGattDescriptorValueChanged
3917 EXPECT_EQ(dbus::ObjectPath(""), adapter_chrome_os
->object_path());
3919 adapter_profile_
= nullptr;
3921 FakeBluetoothProfileServiceProviderDelegate profile_delegate
;
3922 adapter_chrome_os
->UseProfile(
3923 BluetoothUUID(), dbus::ObjectPath(""),
3924 BluetoothProfileManagerClient::Options(), &profile_delegate
,
3925 base::Bind(&BluetoothChromeOSTest::ProfileRegisteredCallback
,
3926 base::Unretained(this)),
3927 base::Bind(&BluetoothChromeOSTest::ErrorCompletionCallback
,
3928 base::Unretained(this)));
3930 EXPECT_FALSE(adapter_profile_
) << "UseProfile error";
3931 EXPECT_EQ(0, callback_count_
) << "UseProfile error";
3932 EXPECT_EQ(1, error_callback_count_
--) << "UseProfile error";
3934 // Protected and private methods:
3936 adapter_chrome_os
->RemovePairingDelegateInternal(&pairing_delegate
);
3937 // AdapterAdded() invalid post Shutdown because it calls SetAdapter.
3938 adapter_chrome_os
->AdapterRemoved(dbus::ObjectPath("x"));
3939 adapter_chrome_os
->AdapterPropertyChanged(dbus::ObjectPath("x"), "");
3940 adapter_chrome_os
->DeviceAdded(dbus::ObjectPath(""));
3941 adapter_chrome_os
->DeviceRemoved(dbus::ObjectPath(""));
3942 adapter_chrome_os
->DevicePropertyChanged(dbus::ObjectPath(""), "");
3943 adapter_chrome_os
->InputPropertyChanged(dbus::ObjectPath(""), "");
3944 // BluetoothAgentServiceProvider::Delegate omitted, dbus will be shutdown,
3945 // with the exception of Released.
3946 adapter_chrome_os
->Released();
3948 adapter_chrome_os
->OnRegisterAgent();
3949 adapter_chrome_os
->OnRegisterAgentError("", "");
3950 adapter_chrome_os
->OnRequestDefaultAgent();
3951 adapter_chrome_os
->OnRequestDefaultAgentError("", "");
3953 adapter_chrome_os
->OnRegisterAudioSink(
3954 base::Bind(&BluetoothChromeOSTest::AudioSinkAcquiredCallback
,
3955 base::Unretained(this)),
3956 base::Bind(&BluetoothChromeOSTest::AudioSinkErrorCallback
,
3957 base::Unretained(this)),
3958 scoped_refptr
<device::BluetoothAudioSink
>());
3959 EXPECT_EQ(0, callback_count_
);
3960 EXPECT_EQ(1, error_callback_count_
--) << "RegisterAudioSink error";
3962 // GetPairing will DCHECK after Shutdown().
3963 // SetAdapter will DCHECK after Shutdown().
3964 // SetDefaultAdapterName will DCHECK after Shutdown().
3965 // RemoveAdapter will DCHECK after Shutdown().
3966 adapter_chrome_os
->PoweredChanged(false);
3967 adapter_chrome_os
->DiscoverableChanged(false);
3968 adapter_chrome_os
->DiscoveringChanged(false);
3969 adapter_chrome_os
->PresentChanged(false);
3971 adapter_chrome_os
->OnSetDiscoverable(GetCallback(), GetErrorCallback(), true);
3972 EXPECT_EQ(0, callback_count_
) << "OnSetDiscoverable error";
3973 EXPECT_EQ(1, error_callback_count_
--) << "OnSetDiscoverable error";
3975 adapter_chrome_os
->OnPropertyChangeCompleted(GetCallback(),
3976 GetErrorCallback(), true);
3977 EXPECT_EQ(0, callback_count_
) << "OnPropertyChangeCompleted error";
3978 EXPECT_EQ(1, error_callback_count_
--) << "OnPropertyChangeCompleted error";
3980 adapter_chrome_os
->AddDiscoverySession(nullptr, GetCallback(),
3981 GetErrorCallback());
3982 EXPECT_EQ(0, callback_count_
) << "AddDiscoverySession error";
3983 EXPECT_EQ(1, error_callback_count_
--) << "AddDiscoverySession error";
3985 adapter_chrome_os
->RemoveDiscoverySession(nullptr, GetCallback(),
3986 GetErrorCallback());
3987 EXPECT_EQ(0, callback_count_
) << "RemoveDiscoverySession error";
3988 EXPECT_EQ(1, error_callback_count_
--) << "RemoveDiscoverySession error";
3990 // OnStartDiscovery tested in Shutdown_OnStartDiscovery
3991 // OnStartDiscoveryError tested in Shutdown_OnStartDiscoveryError
3992 // OnStopDiscovery tested in Shutdown_OnStopDiscovery
3993 // OnStopDiscoveryError tested in Shutdown_OnStopDiscoveryError
3995 adapter_profile_
= nullptr;
3997 // OnRegisterProfile SetProfileDelegate, OnRegisterProfileError, require
3998 // UseProfile to be set first, do so again here just before calling them.
3999 adapter_chrome_os
->UseProfile(
4000 BluetoothUUID(), dbus::ObjectPath(""),
4001 BluetoothProfileManagerClient::Options(), &profile_delegate
,
4002 base::Bind(&BluetoothChromeOSTest::ProfileRegisteredCallback
,
4003 base::Unretained(this)),
4004 base::Bind(&BluetoothChromeOSTest::ErrorCompletionCallback
,
4005 base::Unretained(this)));
4007 EXPECT_FALSE(adapter_profile_
) << "UseProfile error";
4008 EXPECT_EQ(0, callback_count_
) << "UseProfile error";
4009 EXPECT_EQ(1, error_callback_count_
--) << "UseProfile error";
4011 adapter_chrome_os
->SetProfileDelegate(
4012 BluetoothUUID(), dbus::ObjectPath(""), &profile_delegate
,
4013 base::Bind(&BluetoothChromeOSTest::ProfileRegisteredCallback
,
4014 base::Unretained(this)),
4015 base::Bind(&BluetoothChromeOSTest::ErrorCompletionCallback
,
4016 base::Unretained(this)));
4017 EXPECT_EQ(0, callback_count_
) << "SetProfileDelegate error";
4018 EXPECT_EQ(1, error_callback_count_
--) << "SetProfileDelegate error";
4020 adapter_chrome_os
->OnRegisterProfileError(BluetoothUUID(), "", "");
4021 EXPECT_EQ(0, callback_count_
) << "OnRegisterProfileError error";
4022 EXPECT_EQ(0, error_callback_count_
) << "OnRegisterProfileError error";
4024 adapter_chrome_os
->ProcessQueuedDiscoveryRequests();
4026 // From BluetoothAdapater:
4028 adapter_
->StartDiscoverySession(
4029 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
4030 base::Unretained(this)),
4031 GetErrorCallback());
4032 EXPECT_EQ(0, callback_count_
) << "StartDiscoverySession error";
4033 EXPECT_EQ(1, error_callback_count_
--) << "StartDiscoverySession error";
4035 EXPECT_EQ(0U, adapter_
->GetDevices().size());
4036 EXPECT_EQ(nullptr, adapter_
->GetDevice(
4037 FakeBluetoothDeviceClient::kPairedDeviceAddress
));
4038 TestPairingDelegate pairing_delegate2
;
4039 adapter_
->AddPairingDelegate(
4040 &pairing_delegate2
, BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH
);
4041 adapter_
->RemovePairingDelegate(&pairing_delegate2
);
4044 // Verifies post-Shutdown of discovery sessions and OnStartDiscovery.
4045 TEST_F(BluetoothChromeOSTest
, Shutdown_OnStartDiscovery
) {
4046 const int kNumberOfDiscoverySessions
= 10;
4048 BluetoothAdapterChromeOS
* adapter_chrome_os
=
4049 static_cast<BluetoothAdapterChromeOS
*>(adapter_
.get());
4051 for (int i
= 0; i
< kNumberOfDiscoverySessions
; i
++) {
4052 adapter_chrome_os
->AddDiscoverySession(nullptr, GetCallback(),
4053 GetErrorCallback());
4055 adapter_
->Shutdown();
4056 adapter_chrome_os
->OnStartDiscovery(GetCallback(), GetErrorCallback());
4058 EXPECT_EQ(0, callback_count_
);
4059 EXPECT_EQ(kNumberOfDiscoverySessions
, error_callback_count_
);
4062 // Verifies post-Shutdown of discovery sessions and OnStartDiscoveryError.
4063 TEST_F(BluetoothChromeOSTest
, Shutdown_OnStartDiscoveryError
) {
4064 const int kNumberOfDiscoverySessions
= 10;
4066 BluetoothAdapterChromeOS
* adapter_chrome_os
=
4067 static_cast<BluetoothAdapterChromeOS
*>(adapter_
.get());
4069 for (int i
= 0; i
< kNumberOfDiscoverySessions
; i
++) {
4070 adapter_chrome_os
->AddDiscoverySession(nullptr, GetCallback(),
4071 GetErrorCallback());
4073 adapter_
->Shutdown();
4074 adapter_chrome_os
->OnStartDiscoveryError(GetCallback(), GetErrorCallback(),
4077 EXPECT_EQ(0, callback_count_
);
4078 EXPECT_EQ(kNumberOfDiscoverySessions
, error_callback_count_
);
4081 // Verifies post-Shutdown of discovery sessions and OnStartDiscovery.
4082 TEST_F(BluetoothChromeOSTest
, Shutdown_OnStopDiscovery
) {
4083 const int kNumberOfDiscoverySessions
= 10;
4085 BluetoothAdapterChromeOS
* adapter_chrome_os
=
4086 static_cast<BluetoothAdapterChromeOS
*>(adapter_
.get());
4088 // In order to queue up discovery sessions before an OnStopDiscovery call
4089 // RemoveDiscoverySession must be called, so Add, Start, and Remove:
4090 adapter_chrome_os
->AddDiscoverySession(nullptr, GetCallback(),
4091 GetErrorCallback());
4092 adapter_chrome_os
->OnStartDiscovery(GetCallback(), GetErrorCallback());
4093 adapter_chrome_os
->RemoveDiscoverySession(nullptr, GetCallback(),
4094 GetErrorCallback());
4095 callback_count_
= 0;
4096 error_callback_count_
= 0;
4097 // Can now queue discovery sessions while waiting for OnStopDiscovery.
4098 for (int i
= 0; i
< kNumberOfDiscoverySessions
; i
++) {
4099 adapter_chrome_os
->AddDiscoverySession(nullptr, GetCallback(),
4100 GetErrorCallback());
4102 adapter_
->Shutdown();
4103 adapter_chrome_os
->OnStopDiscovery(GetCallback());
4105 // 1 successful stopped discovery from RemoveDiscoverySession, and
4106 // kNumberOfDiscoverySessions errors from AddDiscoverySession/OnStopDiscovery.
4107 EXPECT_EQ(1, callback_count_
);
4108 EXPECT_EQ(kNumberOfDiscoverySessions
, error_callback_count_
);
4111 // Verifies post-Shutdown of discovery sessions and OnStopDiscoveryError.
4112 TEST_F(BluetoothChromeOSTest
, Shutdown_OnStopDiscoveryError
) {
4113 const int kNumberOfDiscoverySessions
= 10;
4115 BluetoothAdapterChromeOS
* adapter_chrome_os
=
4116 static_cast<BluetoothAdapterChromeOS
*>(adapter_
.get());
4118 // In order to queue up discovery sessions before an OnStopDiscoveryError call
4119 // RemoveDiscoverySession must be called, so Add, Start, and Remove:
4120 adapter_chrome_os
->AddDiscoverySession(nullptr, GetCallback(),
4121 GetErrorCallback());
4122 adapter_chrome_os
->OnStartDiscovery(GetCallback(), GetErrorCallback());
4123 adapter_chrome_os
->RemoveDiscoverySession(nullptr, GetCallback(),
4124 GetErrorCallback());
4125 callback_count_
= 0;
4126 error_callback_count_
= 0;
4127 // Can now queue discovery sessions while waiting for OnStopDiscoveryError.
4128 for (int i
= 0; i
< kNumberOfDiscoverySessions
; i
++) {
4129 adapter_chrome_os
->AddDiscoverySession(nullptr, GetCallback(),
4130 GetErrorCallback());
4132 adapter_
->Shutdown();
4133 adapter_chrome_os
->OnStopDiscoveryError(GetErrorCallback(), "", "");
4135 // 1 error reported to RemoveDiscoverySession because of OnStopDiscoveryError,
4136 // and kNumberOfDiscoverySessions errors queued with AddDiscoverySession.
4137 EXPECT_EQ(0, callback_count_
);
4138 EXPECT_EQ(1 + kNumberOfDiscoverySessions
, error_callback_count_
);
4141 } // namespace chromeos