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();
152 class BluetoothChromeOSTest
: public testing::Test
{
154 void SetUp() override
{
155 scoped_ptr
<DBusThreadManagerSetter
> dbus_setter
=
156 chromeos::DBusThreadManager::GetSetterForTesting();
157 // We need to initialize DBusThreadManager early to prevent
158 // Bluetooth*::Create() methods from picking the real instead of fake
160 fake_bluetooth_adapter_client_
= new FakeBluetoothAdapterClient
;
161 dbus_setter
->SetBluetoothAdapterClient(
162 scoped_ptr
<BluetoothAdapterClient
>(fake_bluetooth_adapter_client_
));
163 fake_bluetooth_device_client_
= new FakeBluetoothDeviceClient
;
164 dbus_setter
->SetBluetoothDeviceClient(
165 scoped_ptr
<BluetoothDeviceClient
>(fake_bluetooth_device_client_
));
166 dbus_setter
->SetBluetoothInputClient(
167 scoped_ptr
<BluetoothInputClient
>(new FakeBluetoothInputClient
));
168 dbus_setter
->SetBluetoothAgentManagerClient(
169 scoped_ptr
<BluetoothAgentManagerClient
>(
170 new FakeBluetoothAgentManagerClient
));
171 dbus_setter
->SetBluetoothGattServiceClient(
172 scoped_ptr
<BluetoothGattServiceClient
>(
173 new FakeBluetoothGattServiceClient
));
175 fake_bluetooth_adapter_client_
->SetSimulationIntervalMs(10);
178 error_callback_count_
= 0;
179 last_connect_error_
= BluetoothDevice::ERROR_UNKNOWN
;
180 last_client_error_
= "";
183 void TearDown() override
{
184 for (ScopedVector
<BluetoothDiscoverySession
>::iterator iter
=
185 discovery_sessions_
.begin();
186 iter
!= discovery_sessions_
.end();
188 BluetoothDiscoverySession
* session
= *iter
;
189 if (!session
->IsActive())
192 session
->Stop(GetCallback(), GetErrorCallback());
194 ASSERT_EQ(1, callback_count_
);
196 discovery_sessions_
.clear();
198 DBusThreadManager::Shutdown();
207 base::Closure
GetCallback() {
208 return base::Bind(&BluetoothChromeOSTest::Callback
, base::Unretained(this));
211 void DiscoverySessionCallback(
212 scoped_ptr
<BluetoothDiscoverySession
> discovery_session
) {
214 discovery_sessions_
.push_back(discovery_session
.release());
218 void AudioSinkAcquiredCallback(scoped_refptr
<BluetoothAudioSink
>) {
223 void ProfileRegisteredCallback(BluetoothAdapterProfileChromeOS
* profile
) {
224 adapter_profile_
= profile
;
229 void ErrorCallback() {
230 ++error_callback_count_
;
234 base::Closure
GetErrorCallback() {
235 return base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
236 base::Unretained(this));
239 void DBusErrorCallback(const std::string
& error_name
,
240 const std::string
& error_message
) {
241 ++error_callback_count_
;
242 last_client_error_
= error_name
;
246 void ConnectErrorCallback(BluetoothDevice::ConnectErrorCode error
) {
247 ++error_callback_count_
;
248 last_connect_error_
= error
;
251 void AudioSinkErrorCallback(BluetoothAudioSink::ErrorCode
) {
252 ++error_callback_count_
;
256 void ErrorCompletionCallback(const std::string
& error_message
) {
257 ++error_callback_count_
;
261 // Call to fill the adapter_ member with a BluetoothAdapter instance.
263 adapter_
= new BluetoothAdapterChromeOS();
264 ASSERT_TRUE(adapter_
.get() != NULL
);
265 ASSERT_TRUE(adapter_
->IsInitialized());
268 // Run a discovery phase until the named device is detected, or if the named
269 // device is not created, the discovery process ends without finding it.
271 // The correct behavior of discovery is tested by the "Discovery" test case
272 // without using this function.
273 void DiscoverDevice(const std::string
& address
) {
274 ASSERT_TRUE(adapter_
.get() != NULL
);
275 ASSERT_TRUE(base::MessageLoop::current() != NULL
);
276 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
278 TestBluetoothAdapterObserver
observer(adapter_
);
280 adapter_
->SetPowered(true, GetCallback(), GetErrorCallback());
281 adapter_
->StartDiscoverySession(
282 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
283 base::Unretained(this)),
285 base::MessageLoop::current()->Run();
286 ASSERT_EQ(2, callback_count_
);
287 ASSERT_EQ(0, error_callback_count_
);
288 ASSERT_EQ((size_t)1, discovery_sessions_
.size());
289 ASSERT_TRUE(discovery_sessions_
[0]->IsActive());
292 ASSERT_TRUE(adapter_
->IsPowered());
293 ASSERT_TRUE(adapter_
->IsDiscovering());
295 while (!observer
.device_removed_count() &&
296 observer
.last_device_address() != address
)
297 base::MessageLoop::current()->Run();
299 discovery_sessions_
[0]->Stop(GetCallback(), GetErrorCallback());
300 base::MessageLoop::current()->Run();
301 ASSERT_EQ(1, callback_count_
);
302 ASSERT_EQ(0, error_callback_count_
);
305 ASSERT_FALSE(adapter_
->IsDiscovering());
308 // Run a discovery phase so we have devices that can be paired with.
309 void DiscoverDevices() {
310 // Pass an invalid address for the device so that the discovery process
311 // completes with all devices.
312 DiscoverDevice("does not exist");
316 base::MessageLoop message_loop_
;
317 FakeBluetoothAdapterClient
* fake_bluetooth_adapter_client_
;
318 FakeBluetoothDeviceClient
* fake_bluetooth_device_client_
;
319 scoped_refptr
<BluetoothAdapter
> adapter_
;
322 int error_callback_count_
;
323 enum BluetoothDevice::ConnectErrorCode last_connect_error_
;
324 std::string last_client_error_
;
325 ScopedVector
<BluetoothDiscoverySession
> discovery_sessions_
;
326 BluetoothAdapterProfileChromeOS
* adapter_profile_
;
329 // Some tests use a message loop since background processing is simulated;
330 // break out of those loops.
331 void QuitMessageLoop() {
332 if (base::MessageLoop::current() &&
333 base::MessageLoop::current()->is_running())
334 base::MessageLoop::current()->Quit();
338 TEST_F(BluetoothChromeOSTest
, AlreadyPresent
) {
341 // This verifies that the class gets the list of adapters when created;
342 // and initializes with an existing adapter if there is one.
343 EXPECT_TRUE(adapter_
->IsPresent());
344 EXPECT_FALSE(adapter_
->IsPowered());
345 EXPECT_EQ(FakeBluetoothAdapterClient::kAdapterAddress
,
346 adapter_
->GetAddress());
347 EXPECT_FALSE(adapter_
->IsDiscovering());
349 // There should be a device
350 BluetoothAdapter::DeviceList devices
= adapter_
->GetDevices();
351 EXPECT_EQ(2U, devices
.size());
352 EXPECT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress
,
353 devices
[0]->GetAddress());
354 EXPECT_EQ(FakeBluetoothDeviceClient::kPairedUnconnectableDeviceAddress
,
355 devices
[1]->GetAddress());
358 TEST_F(BluetoothChromeOSTest
, BecomePresent
) {
359 fake_bluetooth_adapter_client_
->SetVisible(false);
361 ASSERT_FALSE(adapter_
->IsPresent());
363 // Install an observer; expect the AdapterPresentChanged to be called
364 // with true, and IsPresent() to return true.
365 TestBluetoothAdapterObserver
observer(adapter_
);
367 fake_bluetooth_adapter_client_
->SetVisible(true);
369 EXPECT_EQ(1, observer
.present_changed_count());
370 EXPECT_TRUE(observer
.last_present());
372 EXPECT_TRUE(adapter_
->IsPresent());
374 // We should have had a device announced.
375 EXPECT_EQ(2, observer
.device_added_count());
376 EXPECT_EQ(FakeBluetoothDeviceClient::kPairedUnconnectableDeviceAddress
,
377 observer
.last_device_address());
379 // Other callbacks shouldn't be called if the values are false.
380 EXPECT_EQ(0, observer
.powered_changed_count());
381 EXPECT_EQ(0, observer
.discovering_changed_count());
382 EXPECT_FALSE(adapter_
->IsPowered());
383 EXPECT_FALSE(adapter_
->IsDiscovering());
386 TEST_F(BluetoothChromeOSTest
, BecomeNotPresent
) {
388 ASSERT_TRUE(adapter_
->IsPresent());
390 // Install an observer; expect the AdapterPresentChanged to be called
391 // with false, and IsPresent() to return false.
392 TestBluetoothAdapterObserver
observer(adapter_
);
394 fake_bluetooth_adapter_client_
->SetVisible(false);
396 EXPECT_EQ(1, observer
.present_changed_count());
397 EXPECT_FALSE(observer
.last_present());
399 EXPECT_FALSE(adapter_
->IsPresent());
401 // We should have had a device removed.
402 EXPECT_EQ(2, observer
.device_removed_count());
403 EXPECT_EQ(FakeBluetoothDeviceClient::kPairedUnconnectableDeviceAddress
,
404 observer
.last_device_address());
406 // Other callbacks shouldn't be called since the values are false.
407 EXPECT_EQ(0, observer
.powered_changed_count());
408 EXPECT_EQ(0, observer
.discovering_changed_count());
409 EXPECT_FALSE(adapter_
->IsPowered());
410 EXPECT_FALSE(adapter_
->IsDiscovering());
413 TEST_F(BluetoothChromeOSTest
, SecondAdapter
) {
415 ASSERT_TRUE(adapter_
->IsPresent());
417 // Install an observer, then add a second adapter. Nothing should change,
418 // we ignore the second adapter.
419 TestBluetoothAdapterObserver
observer(adapter_
);
421 fake_bluetooth_adapter_client_
->SetSecondVisible(true);
423 EXPECT_EQ(0, observer
.present_changed_count());
425 EXPECT_TRUE(adapter_
->IsPresent());
426 EXPECT_EQ(FakeBluetoothAdapterClient::kAdapterAddress
,
427 adapter_
->GetAddress());
429 // Try removing the first adapter, we should now act as if the adapter
430 // is no longer present rather than fall back to the second.
431 fake_bluetooth_adapter_client_
->SetVisible(false);
433 EXPECT_EQ(1, observer
.present_changed_count());
434 EXPECT_FALSE(observer
.last_present());
436 EXPECT_FALSE(adapter_
->IsPresent());
438 // We should have had a device removed.
439 EXPECT_EQ(2, observer
.device_removed_count());
440 EXPECT_EQ(FakeBluetoothDeviceClient::kPairedUnconnectableDeviceAddress
,
441 observer
.last_device_address());
443 // Other callbacks shouldn't be called since the values are false.
444 EXPECT_EQ(0, observer
.powered_changed_count());
445 EXPECT_EQ(0, observer
.discovering_changed_count());
446 EXPECT_FALSE(adapter_
->IsPowered());
447 EXPECT_FALSE(adapter_
->IsDiscovering());
451 // Removing the second adapter shouldn't set anything either.
452 fake_bluetooth_adapter_client_
->SetSecondVisible(false);
454 EXPECT_EQ(0, observer
.device_removed_count());
455 EXPECT_EQ(0, observer
.powered_changed_count());
456 EXPECT_EQ(0, observer
.discovering_changed_count());
459 TEST_F(BluetoothChromeOSTest
, BecomePowered
) {
461 ASSERT_FALSE(adapter_
->IsPowered());
463 // Install an observer; expect the AdapterPoweredChanged to be called
464 // with true, and IsPowered() to return true.
465 TestBluetoothAdapterObserver
observer(adapter_
);
467 adapter_
->SetPowered(true, GetCallback(), GetErrorCallback());
468 EXPECT_EQ(1, callback_count_
);
469 EXPECT_EQ(0, error_callback_count_
);
471 EXPECT_EQ(1, observer
.powered_changed_count());
472 EXPECT_TRUE(observer
.last_powered());
474 EXPECT_TRUE(adapter_
->IsPowered());
477 TEST_F(BluetoothChromeOSTest
, BecomeNotPowered
) {
479 adapter_
->SetPowered(true, GetCallback(), GetErrorCallback());
480 EXPECT_EQ(1, callback_count_
);
481 EXPECT_EQ(0, error_callback_count_
);
484 ASSERT_TRUE(adapter_
->IsPowered());
486 // Install an observer; expect the AdapterPoweredChanged to be called
487 // with false, and IsPowered() to return false.
488 TestBluetoothAdapterObserver
observer(adapter_
);
490 adapter_
->SetPowered(false, GetCallback(), GetErrorCallback());
491 EXPECT_EQ(1, callback_count_
);
492 EXPECT_EQ(0, error_callback_count_
);
494 EXPECT_EQ(1, observer
.powered_changed_count());
495 EXPECT_FALSE(observer
.last_powered());
497 EXPECT_FALSE(adapter_
->IsPowered());
500 TEST_F(BluetoothChromeOSTest
, SetPoweredWhenNotPresent
) {
502 ASSERT_TRUE(adapter_
->IsPresent());
504 // Install an observer; expect the AdapterPresentChanged to be called
505 // with false, and IsPresent() to return false.
506 TestBluetoothAdapterObserver
observer(adapter_
);
508 fake_bluetooth_adapter_client_
->SetVisible(false);
510 EXPECT_EQ(1, observer
.present_changed_count());
511 EXPECT_FALSE(observer
.last_present());
513 EXPECT_FALSE(adapter_
->IsPresent());
514 EXPECT_FALSE(adapter_
->IsPowered());
516 adapter_
->SetPowered(true, GetCallback(), GetErrorCallback());
517 EXPECT_EQ(0, callback_count_
);
518 EXPECT_EQ(1, error_callback_count_
);
520 EXPECT_EQ(0, observer
.powered_changed_count());
521 EXPECT_FALSE(observer
.last_powered());
523 EXPECT_FALSE(adapter_
->IsPowered());
526 TEST_F(BluetoothChromeOSTest
, ChangeAdapterName
) {
529 static const std::string
new_name(".__.");
531 adapter_
->SetName(new_name
, GetCallback(), GetErrorCallback());
532 EXPECT_EQ(1, callback_count_
);
533 EXPECT_EQ(0, error_callback_count_
);
535 EXPECT_EQ(new_name
, adapter_
->GetName());
538 TEST_F(BluetoothChromeOSTest
, ChangeAdapterNameWhenNotPresent
) {
540 ASSERT_TRUE(adapter_
->IsPresent());
542 // Install an observer; expect the AdapterPresentChanged to be called
543 // with false, and IsPresent() to return false.
544 TestBluetoothAdapterObserver
observer(adapter_
);
546 fake_bluetooth_adapter_client_
->SetVisible(false);
548 EXPECT_EQ(1, observer
.present_changed_count());
549 EXPECT_FALSE(observer
.last_present());
551 EXPECT_FALSE(adapter_
->IsPresent());
552 EXPECT_FALSE(adapter_
->IsPowered());
554 adapter_
->SetName("^o^", GetCallback(), GetErrorCallback());
555 EXPECT_EQ(0, callback_count_
);
556 EXPECT_EQ(1, error_callback_count_
);
558 EXPECT_EQ("", adapter_
->GetName());
561 TEST_F(BluetoothChromeOSTest
, BecomeDiscoverable
) {
563 ASSERT_FALSE(adapter_
->IsDiscoverable());
565 // Install an observer; expect the AdapterDiscoverableChanged to be called
566 // with true, and IsDiscoverable() to return true.
567 TestBluetoothAdapterObserver
observer(adapter_
);
569 adapter_
->SetDiscoverable(true, GetCallback(), GetErrorCallback());
570 EXPECT_EQ(1, callback_count_
);
571 EXPECT_EQ(0, error_callback_count_
);
573 EXPECT_EQ(1, observer
.discoverable_changed_count());
575 EXPECT_TRUE(adapter_
->IsDiscoverable());
578 TEST_F(BluetoothChromeOSTest
, BecomeNotDiscoverable
) {
580 adapter_
->SetDiscoverable(true, GetCallback(), GetErrorCallback());
581 EXPECT_EQ(1, callback_count_
);
582 EXPECT_EQ(0, error_callback_count_
);
585 ASSERT_TRUE(adapter_
->IsDiscoverable());
587 // Install an observer; expect the AdapterDiscoverableChanged to be called
588 // with false, and IsDiscoverable() to return false.
589 TestBluetoothAdapterObserver
observer(adapter_
);
591 adapter_
->SetDiscoverable(false, GetCallback(), GetErrorCallback());
592 EXPECT_EQ(1, callback_count_
);
593 EXPECT_EQ(0, error_callback_count_
);
595 EXPECT_EQ(1, observer
.discoverable_changed_count());
597 EXPECT_FALSE(adapter_
->IsDiscoverable());
600 TEST_F(BluetoothChromeOSTest
, SetDiscoverableWhenNotPresent
) {
602 ASSERT_TRUE(adapter_
->IsPresent());
603 ASSERT_FALSE(adapter_
->IsDiscoverable());
605 // Install an observer; expect the AdapterDiscoverableChanged to be called
606 // with true, and IsDiscoverable() to return true.
607 TestBluetoothAdapterObserver
observer(adapter_
);
609 fake_bluetooth_adapter_client_
->SetVisible(false);
611 EXPECT_EQ(1, observer
.present_changed_count());
612 EXPECT_FALSE(observer
.last_present());
614 EXPECT_FALSE(adapter_
->IsPresent());
615 EXPECT_FALSE(adapter_
->IsDiscoverable());
617 adapter_
->SetDiscoverable(true, GetCallback(), GetErrorCallback());
618 EXPECT_EQ(0, callback_count_
);
619 EXPECT_EQ(1, error_callback_count_
);
621 EXPECT_EQ(0, observer
.discoverable_changed_count());
623 EXPECT_FALSE(adapter_
->IsDiscoverable());
626 TEST_F(BluetoothChromeOSTest
, StopDiscovery
) {
629 adapter_
->SetPowered(true, GetCallback(), GetErrorCallback());
630 adapter_
->StartDiscoverySession(
631 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
632 base::Unretained(this)),
635 EXPECT_EQ(2, callback_count_
);
636 EXPECT_EQ(0, error_callback_count_
);
639 ASSERT_TRUE(adapter_
->IsPowered());
640 ASSERT_TRUE(adapter_
->IsDiscovering());
641 ASSERT_EQ((size_t)1, discovery_sessions_
.size());
642 ASSERT_TRUE(discovery_sessions_
[0]->IsActive());
644 // Install an observer; aside from the callback, expect the
645 // AdapterDiscoveringChanged method to be called and no longer to be
647 TestBluetoothAdapterObserver
observer(adapter_
);
649 discovery_sessions_
[0]->Stop(GetCallback(), GetErrorCallback());
651 EXPECT_EQ(1, callback_count_
);
652 EXPECT_EQ(0, error_callback_count_
);
654 EXPECT_EQ(1, observer
.discovering_changed_count());
655 EXPECT_FALSE(observer
.last_discovering());
657 EXPECT_FALSE(adapter_
->IsDiscovering());
660 TEST_F(BluetoothChromeOSTest
, Discovery
) {
661 // Test a simulated discovery session.
662 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
665 TestBluetoothAdapterObserver
observer(adapter_
);
667 adapter_
->SetPowered(true, GetCallback(), GetErrorCallback());
668 adapter_
->StartDiscoverySession(
669 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
670 base::Unretained(this)),
673 EXPECT_EQ(2, callback_count_
);
674 EXPECT_EQ(0, error_callback_count_
);
677 ASSERT_TRUE(adapter_
->IsPowered());
678 ASSERT_TRUE(adapter_
->IsDiscovering());
679 ASSERT_EQ((size_t)1, discovery_sessions_
.size());
680 ASSERT_TRUE(discovery_sessions_
[0]->IsActive());
682 // First two devices to appear.
685 EXPECT_EQ(2, observer
.device_added_count());
686 EXPECT_EQ(FakeBluetoothDeviceClient::kLowEnergyAddress
,
687 observer
.last_device_address());
689 // Next we should get another two devices...
691 EXPECT_EQ(4, observer
.device_added_count());
693 // Okay, let's run forward until a device is actually removed...
694 while (!observer
.device_removed_count())
697 EXPECT_EQ(1, observer
.device_removed_count());
698 EXPECT_EQ(FakeBluetoothDeviceClient::kVanishingDeviceAddress
,
699 observer
.last_device_address());
702 TEST_F(BluetoothChromeOSTest
, PoweredAndDiscovering
) {
704 adapter_
->SetPowered(true, GetCallback(), GetErrorCallback());
705 adapter_
->StartDiscoverySession(
706 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
707 base::Unretained(this)),
710 EXPECT_EQ(2, callback_count_
);
711 EXPECT_EQ(0, error_callback_count_
);
713 ASSERT_EQ((size_t)1, discovery_sessions_
.size());
714 ASSERT_TRUE(discovery_sessions_
[0]->IsActive());
716 // Stop the timers that the simulation uses
717 fake_bluetooth_device_client_
->EndDiscoverySimulation(
718 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
));
720 ASSERT_TRUE(adapter_
->IsPowered());
721 ASSERT_TRUE(adapter_
->IsDiscovering());
723 fake_bluetooth_adapter_client_
->SetVisible(false);
724 ASSERT_FALSE(adapter_
->IsPresent());
725 ASSERT_FALSE(discovery_sessions_
[0]->IsActive());
727 // Install an observer; expect the AdapterPresentChanged,
728 // AdapterPoweredChanged and AdapterDiscoveringChanged methods to be called
729 // with true, and IsPresent(), IsPowered() and IsDiscovering() to all
731 TestBluetoothAdapterObserver
observer(adapter_
);
733 fake_bluetooth_adapter_client_
->SetVisible(true);
735 EXPECT_EQ(1, observer
.present_changed_count());
736 EXPECT_TRUE(observer
.last_present());
737 EXPECT_TRUE(adapter_
->IsPresent());
739 EXPECT_EQ(1, observer
.powered_changed_count());
740 EXPECT_TRUE(observer
.last_powered());
741 EXPECT_TRUE(adapter_
->IsPowered());
743 EXPECT_EQ(1, observer
.discovering_changed_count());
744 EXPECT_TRUE(observer
.last_discovering());
745 EXPECT_TRUE(adapter_
->IsDiscovering());
749 // Now mark the adapter not present again. Expect the methods to be called
750 // again, to reset the properties back to false
751 fake_bluetooth_adapter_client_
->SetVisible(false);
753 EXPECT_EQ(1, observer
.present_changed_count());
754 EXPECT_FALSE(observer
.last_present());
755 EXPECT_FALSE(adapter_
->IsPresent());
757 EXPECT_EQ(1, observer
.powered_changed_count());
758 EXPECT_FALSE(observer
.last_powered());
759 EXPECT_FALSE(adapter_
->IsPowered());
761 EXPECT_EQ(1, observer
.discovering_changed_count());
762 EXPECT_FALSE(observer
.last_discovering());
763 EXPECT_FALSE(adapter_
->IsDiscovering());
766 // This unit test asserts that the basic reference counting logic works
767 // correctly for discovery requests done via the BluetoothAdapter.
768 TEST_F(BluetoothChromeOSTest
, MultipleDiscoverySessions
) {
770 adapter_
->SetPowered(true, GetCallback(), GetErrorCallback());
771 EXPECT_EQ(1, callback_count_
);
772 EXPECT_EQ(0, error_callback_count_
);
773 EXPECT_TRUE(adapter_
->IsPowered());
776 TestBluetoothAdapterObserver
observer(adapter_
);
778 EXPECT_EQ(0, observer
.discovering_changed_count());
779 EXPECT_FALSE(observer
.last_discovering());
780 EXPECT_FALSE(adapter_
->IsDiscovering());
782 // Request device discovery 3 times.
783 for (int i
= 0; i
< 3; i
++) {
784 adapter_
->StartDiscoverySession(
785 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
786 base::Unretained(this)),
789 // Run only once, as there should have been one D-Bus call.
792 // The observer should have received the discovering changed event exactly
793 // once, the success callback should have been called 3 times and the adapter
794 // should be discovering.
795 EXPECT_EQ(1, observer
.discovering_changed_count());
796 EXPECT_EQ(3, callback_count_
);
797 EXPECT_EQ(0, error_callback_count_
);
798 EXPECT_TRUE(observer
.last_discovering());
799 EXPECT_TRUE(adapter_
->IsDiscovering());
800 ASSERT_EQ((size_t)3, discovery_sessions_
.size());
802 // Request to stop discovery twice.
803 for (int i
= 0; i
< 2; i
++) {
804 discovery_sessions_
[i
]->Stop(GetCallback(), GetErrorCallback());
807 // The observer should have received no additional discovering changed events,
808 // the success callback should have been called 2 times and the adapter should
809 // still be discovering.
810 EXPECT_EQ(1, observer
.discovering_changed_count());
811 EXPECT_EQ(5, callback_count_
);
812 EXPECT_EQ(0, error_callback_count_
);
813 EXPECT_TRUE(observer
.last_discovering());
814 EXPECT_TRUE(adapter_
->IsDiscovering());
815 EXPECT_TRUE(adapter_
->IsDiscovering());
816 EXPECT_FALSE(discovery_sessions_
[0]->IsActive());
817 EXPECT_FALSE(discovery_sessions_
[1]->IsActive());
818 EXPECT_TRUE(discovery_sessions_
[2]->IsActive());
820 // Request device discovery 3 times.
821 for (int i
= 0; i
< 3; i
++) {
822 adapter_
->StartDiscoverySession(
823 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
824 base::Unretained(this)),
828 // The observer should have received no additional discovering changed events,
829 // the success callback should have been called 3 times and the adapter should
830 // still be discovering.
831 EXPECT_EQ(1, observer
.discovering_changed_count());
832 EXPECT_EQ(8, callback_count_
);
833 EXPECT_EQ(0, error_callback_count_
);
834 EXPECT_TRUE(observer
.last_discovering());
835 EXPECT_TRUE(adapter_
->IsDiscovering());
836 ASSERT_EQ((size_t)6, discovery_sessions_
.size());
838 // Request to stop discovery 4 times.
839 for (int i
= 2; i
< 6; i
++) {
840 discovery_sessions_
[i
]->Stop(GetCallback(), GetErrorCallback());
842 // Run only once, as there should have been one D-Bus call.
845 // The observer should have received the discovering changed event exactly
846 // once, the success callback should have been called 4 times and the adapter
847 // should no longer be discovering.
848 EXPECT_EQ(2, observer
.discovering_changed_count());
849 EXPECT_EQ(12, callback_count_
);
850 EXPECT_EQ(0, error_callback_count_
);
851 EXPECT_FALSE(observer
.last_discovering());
852 EXPECT_FALSE(adapter_
->IsDiscovering());
854 // All discovery sessions should be inactive.
855 for (int i
= 0; i
< 6; i
++)
856 EXPECT_FALSE(discovery_sessions_
[i
]->IsActive());
858 // Request to stop discovery on of the inactive sessions.
859 discovery_sessions_
[0]->Stop(GetCallback(), GetErrorCallback());
861 // The call should have failed.
862 EXPECT_EQ(2, observer
.discovering_changed_count());
863 EXPECT_EQ(12, callback_count_
);
864 EXPECT_EQ(1, error_callback_count_
);
865 EXPECT_FALSE(observer
.last_discovering());
866 EXPECT_FALSE(adapter_
->IsDiscovering());
869 // This unit test asserts that the reference counting logic works correctly in
870 // the cases when the adapter gets reset and D-Bus calls are made outside of
871 // the BluetoothAdapter.
872 TEST_F(BluetoothChromeOSTest
,
873 UnexpectedChangesDuringMultipleDiscoverySessions
) {
875 adapter_
->SetPowered(true, GetCallback(), GetErrorCallback());
876 EXPECT_EQ(1, callback_count_
);
877 EXPECT_EQ(0, error_callback_count_
);
878 EXPECT_TRUE(adapter_
->IsPowered());
881 TestBluetoothAdapterObserver
observer(adapter_
);
883 EXPECT_EQ(0, observer
.discovering_changed_count());
884 EXPECT_FALSE(observer
.last_discovering());
885 EXPECT_FALSE(adapter_
->IsDiscovering());
887 // Request device discovery 3 times.
888 for (int i
= 0; i
< 3; i
++) {
889 adapter_
->StartDiscoverySession(
890 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
891 base::Unretained(this)),
894 // Run only once, as there should have been one D-Bus call.
897 // The observer should have received the discovering changed event exactly
898 // once, the success callback should have been called 3 times and the adapter
899 // should be discovering.
900 EXPECT_EQ(1, observer
.discovering_changed_count());
901 EXPECT_EQ(3, callback_count_
);
902 EXPECT_EQ(0, error_callback_count_
);
903 EXPECT_TRUE(observer
.last_discovering());
904 EXPECT_TRUE(adapter_
->IsDiscovering());
905 ASSERT_EQ((size_t)3, discovery_sessions_
.size());
907 for (int i
= 0; i
< 3; i
++)
908 EXPECT_TRUE(discovery_sessions_
[i
]->IsActive());
910 // Stop the timers that the simulation uses
911 fake_bluetooth_device_client_
->EndDiscoverySimulation(
912 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
));
914 ASSERT_TRUE(adapter_
->IsPowered());
915 ASSERT_TRUE(adapter_
->IsDiscovering());
917 // Stop device discovery behind the adapter. The adapter and the observer
918 // should be notified of the change and the reference count should be reset.
919 // Even though FakeBluetoothAdapterClient does its own reference counting and
920 // we called 3 BluetoothAdapter::StartDiscoverySession 3 times, the
921 // FakeBluetoothAdapterClient's count should be only 1 and a single call to
922 // FakeBluetoothAdapterClient::StopDiscovery should work.
923 fake_bluetooth_adapter_client_
->StopDiscovery(
924 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
), GetCallback(),
925 base::Bind(&BluetoothChromeOSTest::DBusErrorCallback
,
926 base::Unretained(this)));
928 EXPECT_EQ(2, observer
.discovering_changed_count());
929 EXPECT_EQ(4, callback_count_
);
930 EXPECT_EQ(0, error_callback_count_
);
931 EXPECT_FALSE(observer
.last_discovering());
932 EXPECT_FALSE(adapter_
->IsDiscovering());
934 // All discovery session instances should have been updated.
935 for (int i
= 0; i
< 3; i
++)
936 EXPECT_FALSE(discovery_sessions_
[i
]->IsActive());
937 discovery_sessions_
.clear();
939 // It should be possible to successfully start discovery.
940 for (int i
= 0; i
< 2; i
++) {
941 adapter_
->StartDiscoverySession(
942 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
943 base::Unretained(this)),
946 // Run only once, as there should have been one D-Bus call.
948 EXPECT_EQ(3, observer
.discovering_changed_count());
949 EXPECT_EQ(6, callback_count_
);
950 EXPECT_EQ(0, error_callback_count_
);
951 EXPECT_TRUE(observer
.last_discovering());
952 EXPECT_TRUE(adapter_
->IsDiscovering());
953 ASSERT_EQ((size_t)2, discovery_sessions_
.size());
955 for (int i
= 0; i
< 2; i
++)
956 EXPECT_TRUE(discovery_sessions_
[i
]->IsActive());
958 fake_bluetooth_device_client_
->EndDiscoverySimulation(
959 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
));
961 // Make the adapter disappear and appear. This will make it come back as
962 // discovering. When this happens, the reference count should become and
963 // remain 0 as no new request was made through the BluetoothAdapter.
964 fake_bluetooth_adapter_client_
->SetVisible(false);
965 ASSERT_FALSE(adapter_
->IsPresent());
966 EXPECT_EQ(4, observer
.discovering_changed_count());
967 EXPECT_EQ(6, callback_count_
);
968 EXPECT_EQ(0, error_callback_count_
);
969 EXPECT_FALSE(observer
.last_discovering());
970 EXPECT_FALSE(adapter_
->IsDiscovering());
972 for (int i
= 0; i
< 2; i
++)
973 EXPECT_FALSE(discovery_sessions_
[i
]->IsActive());
974 discovery_sessions_
.clear();
976 fake_bluetooth_adapter_client_
->SetVisible(true);
977 ASSERT_TRUE(adapter_
->IsPresent());
978 EXPECT_EQ(5, observer
.discovering_changed_count());
979 EXPECT_EQ(6, callback_count_
);
980 EXPECT_EQ(0, error_callback_count_
);
981 EXPECT_TRUE(observer
.last_discovering());
982 EXPECT_TRUE(adapter_
->IsDiscovering());
984 // Start and stop discovery. At this point, FakeBluetoothAdapterClient has
985 // a reference count that is equal to 1. Pretend that this was done by an
986 // application other than us. Starting and stopping discovery will succeed
987 // but it won't cause the discovery state to change.
988 adapter_
->StartDiscoverySession(
989 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
990 base::Unretained(this)),
992 message_loop_
.Run(); // Run the loop, as there should have been a D-Bus call.
993 EXPECT_EQ(5, observer
.discovering_changed_count());
994 EXPECT_EQ(7, callback_count_
);
995 EXPECT_EQ(0, error_callback_count_
);
996 EXPECT_TRUE(observer
.last_discovering());
997 EXPECT_TRUE(adapter_
->IsDiscovering());
998 ASSERT_EQ((size_t)1, discovery_sessions_
.size());
999 EXPECT_TRUE(discovery_sessions_
[0]->IsActive());
1001 discovery_sessions_
[0]->Stop(GetCallback(), GetErrorCallback());
1002 message_loop_
.Run(); // Run the loop, as there should have been a D-Bus call.
1003 EXPECT_EQ(5, observer
.discovering_changed_count());
1004 EXPECT_EQ(8, callback_count_
);
1005 EXPECT_EQ(0, error_callback_count_
);
1006 EXPECT_TRUE(observer
.last_discovering());
1007 EXPECT_TRUE(adapter_
->IsDiscovering());
1008 EXPECT_FALSE(discovery_sessions_
[0]->IsActive());
1009 discovery_sessions_
.clear();
1011 // Start discovery again.
1012 adapter_
->StartDiscoverySession(
1013 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
1014 base::Unretained(this)),
1015 GetErrorCallback());
1016 message_loop_
.Run(); // Run the loop, as there should have been a D-Bus call.
1017 EXPECT_EQ(5, observer
.discovering_changed_count());
1018 EXPECT_EQ(9, callback_count_
);
1019 EXPECT_EQ(0, error_callback_count_
);
1020 EXPECT_TRUE(observer
.last_discovering());
1021 EXPECT_TRUE(adapter_
->IsDiscovering());
1022 ASSERT_EQ((size_t)1, discovery_sessions_
.size());
1023 EXPECT_TRUE(discovery_sessions_
[0]->IsActive());
1025 // Stop discovery via D-Bus. The fake client's reference count will drop but
1026 // the discovery state won't change since our BluetoothAdapter also just
1027 // requested it via D-Bus.
1028 fake_bluetooth_adapter_client_
->StopDiscovery(
1029 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
), GetCallback(),
1030 base::Bind(&BluetoothChromeOSTest::DBusErrorCallback
,
1031 base::Unretained(this)));
1032 message_loop_
.Run();
1033 EXPECT_EQ(5, observer
.discovering_changed_count());
1034 EXPECT_EQ(10, callback_count_
);
1035 EXPECT_EQ(0, error_callback_count_
);
1036 EXPECT_TRUE(observer
.last_discovering());
1037 EXPECT_TRUE(adapter_
->IsDiscovering());
1039 // Now end the discovery session. This should change the adapter's discovery
1041 discovery_sessions_
[0]->Stop(GetCallback(), GetErrorCallback());
1042 message_loop_
.Run();
1043 EXPECT_EQ(6, observer
.discovering_changed_count());
1044 EXPECT_EQ(11, callback_count_
);
1045 EXPECT_EQ(0, error_callback_count_
);
1046 EXPECT_FALSE(observer
.last_discovering());
1047 EXPECT_FALSE(adapter_
->IsDiscovering());
1048 EXPECT_FALSE(discovery_sessions_
[0]->IsActive());
1051 TEST_F(BluetoothChromeOSTest
, InvalidatedDiscoverySessions
) {
1053 adapter_
->SetPowered(true, GetCallback(), GetErrorCallback());
1054 EXPECT_EQ(1, callback_count_
);
1055 EXPECT_EQ(0, error_callback_count_
);
1056 EXPECT_TRUE(adapter_
->IsPowered());
1057 callback_count_
= 0;
1059 TestBluetoothAdapterObserver
observer(adapter_
);
1061 EXPECT_EQ(0, observer
.discovering_changed_count());
1062 EXPECT_FALSE(observer
.last_discovering());
1063 EXPECT_FALSE(adapter_
->IsDiscovering());
1065 // Request device discovery 3 times.
1066 for (int i
= 0; i
< 3; i
++) {
1067 adapter_
->StartDiscoverySession(
1068 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
1069 base::Unretained(this)),
1070 GetErrorCallback());
1072 // Run only once, as there should have been one D-Bus call.
1073 message_loop_
.Run();
1075 // The observer should have received the discovering changed event exactly
1076 // once, the success callback should have been called 3 times and the adapter
1077 // should be discovering.
1078 EXPECT_EQ(1, observer
.discovering_changed_count());
1079 EXPECT_EQ(3, callback_count_
);
1080 EXPECT_EQ(0, error_callback_count_
);
1081 EXPECT_TRUE(observer
.last_discovering());
1082 EXPECT_TRUE(adapter_
->IsDiscovering());
1083 ASSERT_EQ((size_t)3, discovery_sessions_
.size());
1085 for (int i
= 0; i
< 3; i
++)
1086 EXPECT_TRUE(discovery_sessions_
[i
]->IsActive());
1088 // Stop the timers that the simulation uses
1089 fake_bluetooth_device_client_
->EndDiscoverySimulation(
1090 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
));
1092 ASSERT_TRUE(adapter_
->IsPowered());
1093 ASSERT_TRUE(adapter_
->IsDiscovering());
1095 // Delete all but one discovery session.
1096 discovery_sessions_
.pop_back();
1097 discovery_sessions_
.pop_back();
1098 ASSERT_EQ((size_t)1, discovery_sessions_
.size());
1099 EXPECT_TRUE(discovery_sessions_
[0]->IsActive());
1100 EXPECT_TRUE(adapter_
->IsDiscovering());
1102 // Stop device discovery behind the adapter. The one active discovery session
1103 // should become inactive, but more importantly, we shouldn't run into any
1104 // memory errors as the sessions that we explicitly deleted should get
1106 fake_bluetooth_adapter_client_
->StopDiscovery(
1107 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
), GetCallback(),
1108 base::Bind(&BluetoothChromeOSTest::DBusErrorCallback
,
1109 base::Unretained(this)));
1110 message_loop_
.Run();
1111 EXPECT_EQ(2, observer
.discovering_changed_count());
1112 EXPECT_EQ(4, callback_count_
);
1113 EXPECT_EQ(0, error_callback_count_
);
1114 EXPECT_FALSE(observer
.last_discovering());
1115 EXPECT_FALSE(adapter_
->IsDiscovering());
1116 EXPECT_FALSE(discovery_sessions_
[0]->IsActive());
1119 TEST_F(BluetoothChromeOSTest
, QueuedDiscoveryRequests
) {
1122 adapter_
->SetPowered(true, GetCallback(), GetErrorCallback());
1123 EXPECT_EQ(1, callback_count_
);
1124 EXPECT_EQ(0, error_callback_count_
);
1125 EXPECT_TRUE(adapter_
->IsPowered());
1126 callback_count_
= 0;
1128 TestBluetoothAdapterObserver
observer(adapter_
);
1130 EXPECT_EQ(0, observer
.discovering_changed_count());
1131 EXPECT_FALSE(observer
.last_discovering());
1132 EXPECT_FALSE(adapter_
->IsDiscovering());
1134 // Request to start discovery. The call should be pending.
1135 adapter_
->StartDiscoverySession(
1136 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
1137 base::Unretained(this)),
1138 GetErrorCallback());
1139 EXPECT_EQ(0, callback_count_
);
1141 fake_bluetooth_device_client_
->EndDiscoverySimulation(
1142 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
));
1144 // The underlying adapter has started discovery, but our call hasn't returned
1146 EXPECT_EQ(1, observer
.discovering_changed_count());
1147 EXPECT_TRUE(observer
.last_discovering());
1148 EXPECT_TRUE(adapter_
->IsDiscovering());
1149 EXPECT_TRUE(discovery_sessions_
.empty());
1151 // Request to start discovery twice. These should get queued and there should
1152 // be no change in state.
1153 for (int i
= 0; i
< 2; i
++) {
1154 adapter_
->StartDiscoverySession(
1155 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
1156 base::Unretained(this)),
1157 GetErrorCallback());
1159 EXPECT_EQ(0, callback_count_
);
1160 EXPECT_EQ(0, error_callback_count_
);
1161 EXPECT_EQ(1, observer
.discovering_changed_count());
1162 EXPECT_TRUE(observer
.last_discovering());
1163 EXPECT_TRUE(adapter_
->IsDiscovering());
1164 EXPECT_TRUE(discovery_sessions_
.empty());
1166 // Process the pending call. The queued calls should execute and the discovery
1167 // session reference count should increase.
1168 message_loop_
.Run();
1169 EXPECT_EQ(3, callback_count_
);
1170 EXPECT_EQ(0, error_callback_count_
);
1171 EXPECT_EQ(1, observer
.discovering_changed_count());
1172 EXPECT_TRUE(observer
.last_discovering());
1173 EXPECT_TRUE(adapter_
->IsDiscovering());
1174 ASSERT_EQ((size_t)3, discovery_sessions_
.size());
1176 // Verify the reference count by removing sessions 3 times. The last request
1177 // should remain pending.
1178 for (int i
= 0; i
< 3; i
++) {
1179 discovery_sessions_
[i
]->Stop(GetCallback(), GetErrorCallback());
1181 EXPECT_EQ(5, callback_count_
);
1182 EXPECT_EQ(0, error_callback_count_
);
1183 EXPECT_EQ(2, observer
.discovering_changed_count());
1184 EXPECT_FALSE(observer
.last_discovering());
1185 EXPECT_FALSE(adapter_
->IsDiscovering());
1186 EXPECT_FALSE(discovery_sessions_
[0]->IsActive());
1187 EXPECT_FALSE(discovery_sessions_
[1]->IsActive());
1188 EXPECT_TRUE(discovery_sessions_
[2]->IsActive());
1190 // Request to stop the session whose call is pending should fail.
1191 discovery_sessions_
[2]->Stop(GetCallback(), GetErrorCallback());
1192 EXPECT_EQ(5, callback_count_
);
1193 EXPECT_EQ(1, error_callback_count_
);
1194 EXPECT_EQ(2, observer
.discovering_changed_count());
1195 EXPECT_FALSE(observer
.last_discovering());
1196 EXPECT_FALSE(adapter_
->IsDiscovering());
1197 EXPECT_TRUE(discovery_sessions_
[2]->IsActive());
1199 // Request to start should get queued.
1200 adapter_
->StartDiscoverySession(
1201 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
1202 base::Unretained(this)),
1203 GetErrorCallback());
1204 EXPECT_EQ(5, callback_count_
);
1205 EXPECT_EQ(1, error_callback_count_
);
1206 EXPECT_EQ(2, observer
.discovering_changed_count());
1207 EXPECT_FALSE(observer
.last_discovering());
1208 EXPECT_FALSE(adapter_
->IsDiscovering());
1209 ASSERT_EQ((size_t)3, discovery_sessions_
.size());
1211 // Run the pending request.
1212 message_loop_
.Run();
1213 EXPECT_EQ(6, callback_count_
);
1214 EXPECT_EQ(1, error_callback_count_
);
1215 EXPECT_EQ(3, observer
.discovering_changed_count());
1216 EXPECT_TRUE(observer
.last_discovering());
1217 EXPECT_TRUE(adapter_
->IsDiscovering());
1218 ASSERT_EQ((size_t)3, discovery_sessions_
.size());
1219 EXPECT_FALSE(discovery_sessions_
[2]->IsActive());
1221 // The queued request to start discovery should have been issued but is still
1222 // pending. Run the loop and verify.
1223 message_loop_
.Run();
1224 EXPECT_EQ(7, callback_count_
);
1225 EXPECT_EQ(1, error_callback_count_
);
1226 EXPECT_EQ(3, observer
.discovering_changed_count());
1227 EXPECT_TRUE(observer
.last_discovering());
1228 EXPECT_TRUE(adapter_
->IsDiscovering());
1229 ASSERT_EQ((size_t)4, discovery_sessions_
.size());
1230 EXPECT_TRUE(discovery_sessions_
[3]->IsActive());
1233 TEST_F(BluetoothChromeOSTest
, StartDiscoverySession
) {
1236 adapter_
->SetPowered(true, GetCallback(), GetErrorCallback());
1237 EXPECT_EQ(1, callback_count_
);
1238 EXPECT_EQ(0, error_callback_count_
);
1239 EXPECT_TRUE(adapter_
->IsPowered());
1240 callback_count_
= 0;
1242 TestBluetoothAdapterObserver
observer(adapter_
);
1244 EXPECT_EQ(0, observer
.discovering_changed_count());
1245 EXPECT_FALSE(observer
.last_discovering());
1246 EXPECT_FALSE(adapter_
->IsDiscovering());
1247 EXPECT_TRUE(discovery_sessions_
.empty());
1249 // Request a new discovery session.
1250 adapter_
->StartDiscoverySession(
1251 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
1252 base::Unretained(this)),
1253 GetErrorCallback());
1254 message_loop_
.Run();
1255 EXPECT_EQ(1, observer
.discovering_changed_count());
1256 EXPECT_EQ(1, callback_count_
);
1257 EXPECT_EQ(0, error_callback_count_
);
1258 EXPECT_TRUE(observer
.last_discovering());
1259 EXPECT_TRUE(adapter_
->IsDiscovering());
1260 ASSERT_EQ((size_t)1, discovery_sessions_
.size());
1261 EXPECT_TRUE(discovery_sessions_
[0]->IsActive());
1263 // Start another session. A new one should be returned in the callback, which
1264 // in turn will destroy the previous session. Adapter should still be
1265 // discovering and the reference count should be 1.
1266 adapter_
->StartDiscoverySession(
1267 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
1268 base::Unretained(this)),
1269 GetErrorCallback());
1270 message_loop_
.Run();
1271 EXPECT_EQ(1, observer
.discovering_changed_count());
1272 EXPECT_EQ(2, callback_count_
);
1273 EXPECT_EQ(0, error_callback_count_
);
1274 EXPECT_TRUE(observer
.last_discovering());
1275 EXPECT_TRUE(adapter_
->IsDiscovering());
1276 ASSERT_EQ((size_t)2, discovery_sessions_
.size());
1277 EXPECT_TRUE(discovery_sessions_
[0]->IsActive());
1279 // Request a new session.
1280 adapter_
->StartDiscoverySession(
1281 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
1282 base::Unretained(this)),
1283 GetErrorCallback());
1284 message_loop_
.Run();
1285 EXPECT_EQ(1, observer
.discovering_changed_count());
1286 EXPECT_EQ(3, callback_count_
);
1287 EXPECT_EQ(0, error_callback_count_
);
1288 EXPECT_TRUE(observer
.last_discovering());
1289 EXPECT_TRUE(adapter_
->IsDiscovering());
1290 ASSERT_EQ((size_t)3, discovery_sessions_
.size());
1291 EXPECT_TRUE(discovery_sessions_
[1]->IsActive());
1292 EXPECT_NE(discovery_sessions_
[0], discovery_sessions_
[1]);
1294 // Stop the previous discovery session. The session should end but discovery
1296 discovery_sessions_
[0]->Stop(GetCallback(), GetErrorCallback());
1297 message_loop_
.Run();
1298 EXPECT_EQ(1, observer
.discovering_changed_count());
1299 EXPECT_EQ(4, 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)3, discovery_sessions_
.size());
1304 EXPECT_FALSE(discovery_sessions_
[0]->IsActive());
1305 EXPECT_TRUE(discovery_sessions_
[1]->IsActive());
1307 // Delete the current active session. Discovery should eventually stop.
1308 discovery_sessions_
.clear();
1309 while (observer
.last_discovering())
1310 message_loop_
.RunUntilIdle();
1312 EXPECT_EQ(2, observer
.discovering_changed_count());
1313 EXPECT_EQ(4, callback_count_
);
1314 EXPECT_EQ(0, error_callback_count_
);
1315 EXPECT_FALSE(observer
.last_discovering());
1316 EXPECT_FALSE(adapter_
->IsDiscovering());
1319 TEST_F(BluetoothChromeOSTest
, SetDiscoveryFilterBeforeStartDiscovery
) {
1320 // Test a simulated discovery session.
1321 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
1324 TestBluetoothAdapterObserver
observer(adapter_
);
1326 BluetoothDiscoveryFilter
* df
= new BluetoothDiscoveryFilter(
1327 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE
);
1329 df
->AddUUID(BluetoothUUID("1000"));
1330 scoped_ptr
<BluetoothDiscoveryFilter
> discovery_filter(df
);
1332 adapter_
->SetPowered(true, base::Bind(&BluetoothChromeOSTest::Callback
,
1333 base::Unretained(this)),
1334 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1335 base::Unretained(this)));
1336 adapter_
->StartDiscoverySessionWithFilter(
1337 discovery_filter
.Pass(),
1338 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
1339 base::Unretained(this)),
1340 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1341 base::Unretained(this)));
1342 message_loop_
.Run();
1343 EXPECT_EQ(2, callback_count_
);
1344 EXPECT_EQ(0, error_callback_count_
);
1345 callback_count_
= 0;
1347 ASSERT_TRUE(adapter_
->IsPowered());
1348 ASSERT_TRUE(adapter_
->IsDiscovering());
1349 ASSERT_EQ((size_t)1, discovery_sessions_
.size());
1350 ASSERT_TRUE(discovery_sessions_
[0]->IsActive());
1351 ASSERT_TRUE(df
->Equals(*discovery_sessions_
[0]->GetDiscoveryFilter()));
1353 auto filter
= fake_bluetooth_adapter_client_
->GetDiscoveryFilter();
1354 EXPECT_NE(nullptr, filter
);
1355 EXPECT_EQ("le", *filter
->transport
);
1356 EXPECT_EQ(-60, *filter
->rssi
);
1357 EXPECT_EQ(nullptr, filter
->pathloss
.get());
1358 std::vector
<std::string
> uuids
= *filter
->uuids
;
1359 EXPECT_NE(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1000"));
1361 discovery_sessions_
[0]->Stop(
1362 base::Bind(&BluetoothChromeOSTest::Callback
, base::Unretained(this)),
1363 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1364 base::Unretained(this)));
1366 message_loop_
.Run();
1368 EXPECT_EQ(1, callback_count_
);
1369 EXPECT_EQ(0, error_callback_count_
);
1371 ASSERT_TRUE(adapter_
->IsPowered());
1372 ASSERT_FALSE(adapter_
->IsDiscovering());
1373 ASSERT_EQ((size_t)1, discovery_sessions_
.size());
1374 ASSERT_FALSE(discovery_sessions_
[0]->IsActive());
1375 ASSERT_EQ(discovery_sessions_
[0]->GetDiscoveryFilter(),
1376 (BluetoothDiscoveryFilter
*)NULL
);
1378 filter
= fake_bluetooth_adapter_client_
->GetDiscoveryFilter();
1379 EXPECT_EQ(nullptr, filter
);
1382 TEST_F(BluetoothChromeOSTest
, SetDiscoveryFilterBeforeStartDiscoveryFail
) {
1383 // Test a simulated discovery session.
1384 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
1387 TestBluetoothAdapterObserver
observer(adapter_
);
1389 BluetoothDiscoveryFilter
* df
= new BluetoothDiscoveryFilter(
1390 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE
);
1392 df
->AddUUID(BluetoothUUID("1000"));
1393 scoped_ptr
<BluetoothDiscoveryFilter
> discovery_filter(df
);
1395 adapter_
->SetPowered(true, base::Bind(&BluetoothChromeOSTest::Callback
,
1396 base::Unretained(this)),
1397 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1398 base::Unretained(this)));
1399 EXPECT_EQ(1, callback_count_
);
1400 callback_count_
= 0;
1402 fake_bluetooth_adapter_client_
->MakeSetDiscoveryFilterFail();
1404 adapter_
->StartDiscoverySessionWithFilter(
1405 discovery_filter
.Pass(),
1406 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
1407 base::Unretained(this)),
1408 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1409 base::Unretained(this)));
1411 message_loop_
.Run();
1413 EXPECT_EQ(1, error_callback_count_
);
1414 error_callback_count_
= 0;
1416 ASSERT_TRUE(adapter_
->IsPowered());
1417 ASSERT_FALSE(adapter_
->IsDiscovering());
1418 ASSERT_EQ((size_t)0, discovery_sessions_
.size());
1420 auto filter
= fake_bluetooth_adapter_client_
->GetDiscoveryFilter();
1421 EXPECT_EQ(nullptr, filter
);
1424 // This test queues two requests to StartDiscovery with pre set filter. This
1425 // should result in SetDiscoveryFilter, then StartDiscovery, and SetDiscovery
1427 TEST_F(BluetoothChromeOSTest
, QueuedSetDiscoveryFilterBeforeStartDiscovery
) {
1428 // Test a simulated discovery session.
1429 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
1432 TestBluetoothAdapterObserver
observer(adapter_
);
1434 BluetoothDiscoveryFilter
* df
= new BluetoothDiscoveryFilter(
1435 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE
);
1437 df
->AddUUID(BluetoothUUID("1000"));
1438 scoped_ptr
<BluetoothDiscoveryFilter
> discovery_filter(df
);
1440 BluetoothDiscoveryFilter
* df2
= new BluetoothDiscoveryFilter(
1441 BluetoothDiscoveryFilter::Transport::TRANSPORT_CLASSIC
);
1443 df2
->AddUUID(BluetoothUUID("1002"));
1444 scoped_ptr
<BluetoothDiscoveryFilter
> discovery_filter2(df2
);
1446 adapter_
->SetPowered(true, base::Bind(&BluetoothChromeOSTest::Callback
,
1447 base::Unretained(this)),
1448 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1449 base::Unretained(this)));
1451 EXPECT_EQ(1, callback_count_
);
1452 EXPECT_EQ(0, error_callback_count_
);
1453 callback_count_
= 0;
1455 // Queue two requests to start discovery session with filter.
1456 adapter_
->StartDiscoverySessionWithFilter(
1457 discovery_filter
.Pass(),
1458 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
1459 base::Unretained(this)),
1460 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1461 base::Unretained(this)));
1463 adapter_
->StartDiscoverySessionWithFilter(
1464 discovery_filter2
.Pass(),
1465 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
1466 base::Unretained(this)),
1467 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1468 base::Unretained(this)));
1470 // Run requests, on DBus level there should be call SetDiscoveryFilter, then
1471 // StartDiscovery, then SetDiscoveryFilter again.
1472 message_loop_
.Run();
1473 message_loop_
.Run();
1475 EXPECT_EQ(2, callback_count_
);
1476 EXPECT_EQ(0, error_callback_count_
);
1477 callback_count_
= 0;
1479 ASSERT_TRUE(adapter_
->IsPowered());
1480 ASSERT_TRUE(adapter_
->IsDiscovering());
1481 ASSERT_EQ((size_t)2, discovery_sessions_
.size());
1482 ASSERT_TRUE(discovery_sessions_
[0]->IsActive());
1483 ASSERT_TRUE(df
->Equals(*discovery_sessions_
[0]->GetDiscoveryFilter()));
1484 ASSERT_TRUE(discovery_sessions_
[1]->IsActive());
1485 ASSERT_TRUE(df2
->Equals(*discovery_sessions_
[1]->GetDiscoveryFilter()));
1487 auto filter
= fake_bluetooth_adapter_client_
->GetDiscoveryFilter();
1488 EXPECT_NE(nullptr, filter
);
1489 EXPECT_EQ("auto", *filter
->transport
);
1490 EXPECT_EQ(-65, *filter
->rssi
);
1491 EXPECT_EQ(nullptr, filter
->pathloss
.get());
1492 auto uuids
= *filter
->uuids
;
1493 EXPECT_NE(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1000"));
1494 EXPECT_NE(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1002"));
1496 discovery_sessions_
[0]->Stop(
1497 base::Bind(&BluetoothChromeOSTest::Callback
, base::Unretained(this)),
1498 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1499 base::Unretained(this)));
1501 discovery_sessions_
[1]->Stop(
1502 base::Bind(&BluetoothChromeOSTest::Callback
, base::Unretained(this)),
1503 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1504 base::Unretained(this)));
1506 message_loop_
.Run();
1508 EXPECT_EQ(2, callback_count_
);
1509 EXPECT_EQ(0, error_callback_count_
);
1511 ASSERT_TRUE(adapter_
->IsPowered());
1512 ASSERT_FALSE(adapter_
->IsDiscovering());
1513 ASSERT_FALSE(discovery_sessions_
[0]->IsActive());
1514 ASSERT_EQ(discovery_sessions_
[0]->GetDiscoveryFilter(),
1515 (BluetoothDiscoveryFilter
*)NULL
);
1516 ASSERT_FALSE(discovery_sessions_
[1]->IsActive());
1517 ASSERT_EQ(discovery_sessions_
[1]->GetDiscoveryFilter(),
1518 (BluetoothDiscoveryFilter
*)NULL
);
1520 filter
= fake_bluetooth_adapter_client_
->GetDiscoveryFilter();
1521 EXPECT_EQ(nullptr, filter
);
1524 // Call StartFilteredDiscovery twice (2nd time while 1st call is still pending).
1525 // Make the first SetDiscoveryFilter fail and the second one succeed. It should
1526 // end up with one active discovery session.
1527 TEST_F(BluetoothChromeOSTest
,
1528 QueuedSetDiscoveryFilterBeforeStartDiscoveryFail
) {
1529 // Test a simulated discovery session.
1530 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
1533 TestBluetoothAdapterObserver
observer(adapter_
);
1535 BluetoothDiscoveryFilter
* df
= new BluetoothDiscoveryFilter(
1536 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE
);
1538 df
->AddUUID(BluetoothUUID("1000"));
1539 scoped_ptr
<BluetoothDiscoveryFilter
> discovery_filter(df
);
1541 BluetoothDiscoveryFilter
* df2
= new BluetoothDiscoveryFilter(
1542 BluetoothDiscoveryFilter::Transport::TRANSPORT_CLASSIC
);
1544 df2
->AddUUID(BluetoothUUID("1002"));
1545 scoped_ptr
<BluetoothDiscoveryFilter
> discovery_filter2(df2
);
1547 adapter_
->SetPowered(true, base::Bind(&BluetoothChromeOSTest::Callback
,
1548 base::Unretained(this)),
1549 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1550 base::Unretained(this)));
1552 EXPECT_EQ(1, callback_count_
);
1553 EXPECT_EQ(0, error_callback_count_
);
1554 callback_count_
= 0;
1556 fake_bluetooth_adapter_client_
->MakeSetDiscoveryFilterFail();
1558 // Queue two requests to start discovery session with filter.
1559 adapter_
->StartDiscoverySessionWithFilter(
1560 discovery_filter
.Pass(),
1561 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
1562 base::Unretained(this)),
1563 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1564 base::Unretained(this)));
1566 adapter_
->StartDiscoverySessionWithFilter(
1567 discovery_filter2
.Pass(),
1568 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
1569 base::Unretained(this)),
1570 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1571 base::Unretained(this)));
1573 message_loop_
.Run();
1575 // First request to SetDiscoveryFilter should fail, resulting in no session
1577 EXPECT_EQ(0, callback_count_
);
1578 EXPECT_EQ(1, error_callback_count_
);
1579 error_callback_count_
= 0;
1581 ASSERT_TRUE(adapter_
->IsPowered());
1582 ASSERT_FALSE(adapter_
->IsDiscovering());
1583 ASSERT_EQ((size_t)0, discovery_sessions_
.size());
1585 message_loop_
.Run();
1587 // Second request should succeed
1588 EXPECT_EQ(1, callback_count_
);
1589 EXPECT_EQ(0, error_callback_count_
);
1590 callback_count_
= 0;
1592 ASSERT_TRUE(adapter_
->IsDiscovering());
1593 ASSERT_EQ((size_t)1, discovery_sessions_
.size());
1594 ASSERT_TRUE(discovery_sessions_
[0]->IsActive());
1595 ASSERT_TRUE(df2
->Equals(*discovery_sessions_
[0]->GetDiscoveryFilter()));
1597 auto filter
= fake_bluetooth_adapter_client_
->GetDiscoveryFilter();
1598 EXPECT_NE(nullptr, filter
);
1599 EXPECT_EQ("bredr", *filter
->transport
);
1600 EXPECT_EQ(-65, *filter
->rssi
);
1601 EXPECT_EQ(nullptr, filter
->pathloss
.get());
1602 auto uuids
= *filter
->uuids
;
1603 EXPECT_NE(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1002"));
1605 discovery_sessions_
[0]->Stop(
1606 base::Bind(&BluetoothChromeOSTest::Callback
, base::Unretained(this)),
1607 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1608 base::Unretained(this)));
1610 message_loop_
.Run();
1612 EXPECT_EQ(1, callback_count_
);
1613 EXPECT_EQ(0, error_callback_count_
);
1615 ASSERT_TRUE(adapter_
->IsPowered());
1616 ASSERT_FALSE(adapter_
->IsDiscovering());
1617 ASSERT_FALSE(discovery_sessions_
[0]->IsActive());
1618 ASSERT_EQ(discovery_sessions_
[0]->GetDiscoveryFilter(),
1619 (BluetoothDiscoveryFilter
*)NULL
);
1621 filter
= fake_bluetooth_adapter_client_
->GetDiscoveryFilter();
1622 EXPECT_EQ(nullptr, filter
);
1625 TEST_F(BluetoothChromeOSTest
, SetDiscoveryFilterAfterStartDiscovery
) {
1626 // Test a simulated discovery session.
1627 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
1630 TestBluetoothAdapterObserver
observer(adapter_
);
1632 adapter_
->SetPowered(true, base::Bind(&BluetoothChromeOSTest::Callback
,
1633 base::Unretained(this)),
1634 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1635 base::Unretained(this)));
1636 adapter_
->StartDiscoverySession(
1637 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
1638 base::Unretained(this)),
1639 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1640 base::Unretained(this)));
1641 message_loop_
.Run();
1642 EXPECT_EQ(2, callback_count_
);
1643 EXPECT_EQ(0, error_callback_count_
);
1644 callback_count_
= 0;
1646 ASSERT_TRUE(adapter_
->IsPowered());
1647 ASSERT_TRUE(adapter_
->IsDiscovering());
1648 ASSERT_EQ((size_t)1, discovery_sessions_
.size());
1649 ASSERT_TRUE(discovery_sessions_
[0]->IsActive());
1650 EXPECT_EQ(1, observer
.discovering_changed_count());
1653 auto nullInstance
= scoped_ptr
<BluetoothDiscoveryFilter
>();
1654 nullInstance
.reset();
1655 ASSERT_EQ(discovery_sessions_
[0]->GetDiscoveryFilter(), nullInstance
.get());
1657 auto filter
= fake_bluetooth_adapter_client_
->GetDiscoveryFilter();
1658 EXPECT_EQ(nullptr, filter
);
1660 BluetoothDiscoveryFilter
* df
= new BluetoothDiscoveryFilter(
1661 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE
);
1663 df
->AddUUID(BluetoothUUID("1000"));
1664 scoped_ptr
<BluetoothDiscoveryFilter
> discovery_filter(df
);
1666 discovery_sessions_
[0]->SetDiscoveryFilter(
1667 discovery_filter
.Pass(),
1668 base::Bind(&BluetoothChromeOSTest::Callback
, base::Unretained(this)),
1669 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1670 base::Unretained(this)));
1672 message_loop_
.Run();
1673 EXPECT_EQ(1, callback_count_
);
1674 EXPECT_EQ(0, error_callback_count_
);
1675 callback_count_
= 0;
1677 ASSERT_TRUE(df
->Equals(*discovery_sessions_
[0]->GetDiscoveryFilter()));
1679 filter
= fake_bluetooth_adapter_client_
->GetDiscoveryFilter();
1680 EXPECT_NE(nullptr, filter
);
1681 EXPECT_EQ("le", *filter
->transport
);
1682 EXPECT_EQ(-60, *filter
->rssi
);
1683 EXPECT_EQ(nullptr, filter
->pathloss
.get());
1684 std::vector
<std::string
> uuids
= *filter
->uuids
;
1685 EXPECT_NE(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1000"));
1687 discovery_sessions_
[0]->Stop(
1688 base::Bind(&BluetoothChromeOSTest::Callback
, base::Unretained(this)),
1689 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1690 base::Unretained(this)));
1692 message_loop_
.Run();
1694 EXPECT_EQ(1, callback_count_
);
1695 EXPECT_EQ(0, error_callback_count_
);
1697 ASSERT_TRUE(adapter_
->IsPowered());
1698 ASSERT_FALSE(adapter_
->IsDiscovering());
1699 ASSERT_EQ((size_t)1, discovery_sessions_
.size());
1700 ASSERT_FALSE(discovery_sessions_
[0]->IsActive());
1701 ASSERT_EQ(discovery_sessions_
[0]->GetDiscoveryFilter(),
1702 (BluetoothDiscoveryFilter
*)NULL
);
1704 filter
= fake_bluetooth_adapter_client_
->GetDiscoveryFilter();
1705 EXPECT_EQ(nullptr, filter
);
1708 // This unit test asserts that the basic reference counting, and filter merging
1709 // works correctly for discovery requests done via the BluetoothAdapter.
1710 TEST_F(BluetoothChromeOSTest
, SetDiscoveryFilterBeforeStartDiscoveryMultiple
) {
1712 adapter_
->SetPowered(true, base::Bind(&BluetoothChromeOSTest::Callback
,
1713 base::Unretained(this)),
1714 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1715 base::Unretained(this)));
1716 EXPECT_EQ(1, callback_count_
);
1717 EXPECT_EQ(0, error_callback_count_
);
1718 EXPECT_TRUE(adapter_
->IsPowered());
1719 callback_count_
= 0;
1721 TestBluetoothAdapterObserver
observer(adapter_
);
1723 // Request device discovery with pre-set filter 3 times.
1724 for (int i
= 0; i
< 3; i
++) {
1725 scoped_ptr
<BluetoothDiscoveryFilter
> discovery_filter
;
1727 BluetoothDiscoveryFilter
* df
= new BluetoothDiscoveryFilter(
1728 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE
);
1730 df
->AddUUID(BluetoothUUID("1000"));
1731 discovery_filter
.reset(df
);
1732 } else if (i
== 1) {
1733 BluetoothDiscoveryFilter
* df
= new BluetoothDiscoveryFilter(
1734 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE
);
1736 df
->AddUUID(BluetoothUUID("1020"));
1737 df
->AddUUID(BluetoothUUID("1001"));
1738 discovery_filter
.reset(df
);
1739 } else if (i
== 2) {
1740 BluetoothDiscoveryFilter
* df
= new BluetoothDiscoveryFilter(
1741 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE
);
1743 df
->AddUUID(BluetoothUUID("1020"));
1744 df
->AddUUID(BluetoothUUID("1003"));
1745 discovery_filter
.reset(df
);
1748 adapter_
->StartDiscoverySessionWithFilter(
1749 discovery_filter
.Pass(),
1750 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
1751 base::Unretained(this)),
1752 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1753 base::Unretained(this)));
1755 message_loop_
.Run();
1758 EXPECT_EQ(1, observer
.discovering_changed_count());
1761 auto filter
= fake_bluetooth_adapter_client_
->GetDiscoveryFilter();
1762 EXPECT_EQ("le", *filter
->transport
);
1763 EXPECT_EQ(-85, *filter
->rssi
);
1764 EXPECT_EQ(nullptr, filter
->pathloss
.get());
1765 std::vector
<std::string
> uuids
= *filter
->uuids
;
1766 EXPECT_NE(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1000"));
1767 } else if (i
== 1) {
1768 auto filter
= fake_bluetooth_adapter_client_
->GetDiscoveryFilter();
1769 EXPECT_EQ("le", *filter
->transport
);
1770 EXPECT_EQ(-85, *filter
->rssi
);
1771 EXPECT_EQ(nullptr, filter
->pathloss
.get());
1772 std::vector
<std::string
> uuids
= *filter
->uuids
;
1773 EXPECT_NE(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1000"));
1774 EXPECT_NE(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1001"));
1775 EXPECT_NE(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1020"));
1776 } else if (i
== 2) {
1777 auto filter
= fake_bluetooth_adapter_client_
->GetDiscoveryFilter();
1778 EXPECT_EQ("le", *filter
->transport
);
1779 EXPECT_EQ(-85, *filter
->rssi
);
1780 EXPECT_EQ(nullptr, filter
->pathloss
.get());
1781 std::vector
<std::string
> uuids
= *filter
->uuids
;
1782 EXPECT_NE(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1000"));
1783 EXPECT_NE(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1001"));
1784 EXPECT_NE(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1003"));
1785 EXPECT_NE(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1020"));
1789 // the success callback should have been called 3 times and the adapter should
1791 EXPECT_EQ(3, callback_count_
);
1792 EXPECT_EQ(0, error_callback_count_
);
1793 EXPECT_TRUE(adapter_
->IsDiscovering());
1794 ASSERT_EQ((size_t)3, discovery_sessions_
.size());
1796 callback_count_
= 0;
1797 // Request to stop discovery twice.
1798 for (int i
= 0; i
< 2; i
++) {
1799 discovery_sessions_
[i
]->Stop(
1800 base::Bind(&BluetoothChromeOSTest::Callback
, base::Unretained(this)),
1801 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1802 base::Unretained(this)));
1803 message_loop_
.Run();
1806 auto filter
= fake_bluetooth_adapter_client_
->GetDiscoveryFilter();
1807 EXPECT_EQ("le", *filter
->transport
);
1808 EXPECT_EQ(-65, *filter
->rssi
);
1809 EXPECT_EQ(nullptr, filter
->pathloss
.get());
1810 std::vector
<std::string
> uuids
= *filter
->uuids
;
1811 EXPECT_EQ(3UL, uuids
.size());
1812 EXPECT_EQ(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1000"));
1813 EXPECT_NE(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1001"));
1814 EXPECT_NE(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1003"));
1815 EXPECT_NE(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1020"));
1816 } else if (i
== 1) {
1817 auto filter
= fake_bluetooth_adapter_client_
->GetDiscoveryFilter();
1818 EXPECT_EQ("le", *filter
->transport
);
1819 EXPECT_EQ(-65, *filter
->rssi
);
1820 EXPECT_EQ(nullptr, filter
->pathloss
.get());
1821 std::vector
<std::string
> uuids
= *filter
->uuids
;
1822 EXPECT_EQ(2UL, uuids
.size());
1823 EXPECT_EQ(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1000"));
1824 EXPECT_EQ(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1001"));
1825 EXPECT_NE(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1003"));
1826 EXPECT_NE(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1020"));
1827 } else if (i
== 2) {
1828 auto filter
= fake_bluetooth_adapter_client_
->GetDiscoveryFilter();
1829 EXPECT_EQ("le", *filter
->transport
);
1830 EXPECT_EQ(-65, *filter
->rssi
);
1831 EXPECT_EQ(nullptr, filter
->pathloss
.get());
1832 std::vector
<std::string
> uuids
= *filter
->uuids
;
1833 EXPECT_EQ(0UL, uuids
.size());
1837 // The success callback should have been called 2 times and the adapter should
1838 // still be discovering.
1839 EXPECT_EQ(2, callback_count_
);
1840 EXPECT_EQ(0, error_callback_count_
);
1841 EXPECT_TRUE(adapter_
->IsDiscovering());
1842 EXPECT_FALSE(discovery_sessions_
[0]->IsActive());
1843 EXPECT_FALSE(discovery_sessions_
[1]->IsActive());
1844 EXPECT_TRUE(discovery_sessions_
[2]->IsActive());
1846 callback_count_
= 0;
1848 // Request device discovery 3 times.
1849 for (int i
= 0; i
< 3; i
++) {
1850 scoped_ptr
<BluetoothDiscoveryFilter
> discovery_filter
;
1853 BluetoothDiscoveryFilter
* df
= new BluetoothDiscoveryFilter(
1854 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE
);
1856 df
->AddUUID(BluetoothUUID("1000"));
1857 discovery_filter
.reset(df
);
1858 } else if (i
== 1) {
1859 BluetoothDiscoveryFilter
* df
= new BluetoothDiscoveryFilter(
1860 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE
);
1862 df
->AddUUID(BluetoothUUID("1020"));
1863 df
->AddUUID(BluetoothUUID("1001"));
1864 discovery_filter
.reset(df
);
1865 } else if (i
== 2) {
1866 BluetoothDiscoveryFilter
* df
= new BluetoothDiscoveryFilter(
1867 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE
);
1869 df
->AddUUID(BluetoothUUID("1020"));
1870 df
->AddUUID(BluetoothUUID("1003"));
1871 discovery_filter
.reset(df
);
1874 adapter_
->StartDiscoverySessionWithFilter(
1875 discovery_filter
.Pass(),
1876 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
1877 base::Unretained(this)),
1878 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1879 base::Unretained(this)));
1881 // each result in 1 requests.
1882 message_loop_
.Run();
1885 auto filter
= fake_bluetooth_adapter_client_
->GetDiscoveryFilter();
1886 EXPECT_EQ("le", *filter
->transport
);
1887 EXPECT_EQ(-85, *filter
->rssi
);
1888 EXPECT_EQ(nullptr, filter
->pathloss
.get());
1889 std::vector
<std::string
> uuids
= *filter
->uuids
;
1890 EXPECT_NE(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1000"));
1891 EXPECT_NE(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1003"));
1892 EXPECT_NE(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1020"));
1893 } else if (i
== 1 || i
== 2) {
1894 auto filter
= fake_bluetooth_adapter_client_
->GetDiscoveryFilter();
1895 EXPECT_EQ("le", *filter
->transport
);
1896 EXPECT_EQ(-85, *filter
->rssi
);
1897 EXPECT_EQ(nullptr, filter
->pathloss
.get());
1898 std::vector
<std::string
> uuids
= *filter
->uuids
;
1899 EXPECT_NE(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1000"));
1900 EXPECT_NE(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1001"));
1901 EXPECT_NE(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1003"));
1902 EXPECT_NE(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1020"));
1906 // The success callback should have been called 3 times and the adapter should
1907 // still be discovering.
1908 EXPECT_EQ(3, callback_count_
);
1909 EXPECT_EQ(0, error_callback_count_
);
1910 EXPECT_TRUE(adapter_
->IsDiscovering());
1911 ASSERT_EQ((size_t)6, discovery_sessions_
.size());
1913 callback_count_
= 0;
1914 // Request to stop discovery 4 times.
1915 for (int i
= 2; i
< 6; i
++) {
1916 discovery_sessions_
[i
]->Stop(
1917 base::Bind(&BluetoothChromeOSTest::Callback
, base::Unretained(this)),
1918 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1919 base::Unretained(this)));
1921 // filter no 2 is same as filter no 5, so removing it shouldn't cause any
1923 if (i
!= 2 && i
!= 5)
1924 message_loop_
.Run();
1926 // Run only once, as there should have been one D-Bus call.
1927 message_loop_
.Run();
1929 // The success callback should have been called 4 times and the adapter should
1930 // no longer be discovering.
1931 EXPECT_EQ(4, callback_count_
);
1932 EXPECT_EQ(0, error_callback_count_
);
1933 EXPECT_FALSE(adapter_
->IsDiscovering());
1934 EXPECT_EQ(1, observer
.discovering_changed_count());
1936 // All discovery sessions should be inactive.
1937 for (int i
= 0; i
< 6; i
++)
1938 EXPECT_FALSE(discovery_sessions_
[i
]->IsActive());
1940 auto filter
= fake_bluetooth_adapter_client_
->GetDiscoveryFilter();
1941 EXPECT_EQ(nullptr, filter
);
1944 // This unit test asserts that filter merging logic works correctly for filtered
1945 // discovery requests done via the BluetoothAdapter.
1946 TEST_F(BluetoothChromeOSTest
, SetDiscoveryFilterMergingTest
) {
1948 adapter_
->SetPowered(true, base::Bind(&BluetoothChromeOSTest::Callback
,
1949 base::Unretained(this)),
1950 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1951 base::Unretained(this)));
1953 BluetoothDiscoveryFilter
* df
= new BluetoothDiscoveryFilter(
1954 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE
);
1956 df
->AddUUID(BluetoothUUID("1000"));
1957 scoped_ptr
<BluetoothDiscoveryFilter
> discovery_filter(df
);
1959 adapter_
->StartDiscoverySessionWithFilter(
1960 discovery_filter
.Pass(),
1961 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
1962 base::Unretained(this)),
1963 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1964 base::Unretained(this)));
1966 message_loop_
.Run();
1968 auto filter
= fake_bluetooth_adapter_client_
->GetDiscoveryFilter();
1969 EXPECT_EQ("le", *filter
->transport
);
1970 EXPECT_EQ(-15, *filter
->rssi
);
1971 EXPECT_EQ(nullptr, filter
->pathloss
.get());
1972 std::vector
<std::string
> uuids
= *filter
->uuids
;
1973 EXPECT_NE(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1000"));
1975 df
= new BluetoothDiscoveryFilter(
1976 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE
);
1978 df
->AddUUID(BluetoothUUID("1020"));
1979 df
->AddUUID(BluetoothUUID("1001"));
1980 discovery_filter
= scoped_ptr
<BluetoothDiscoveryFilter
>(df
);
1982 adapter_
->StartDiscoverySessionWithFilter(
1983 discovery_filter
.Pass(),
1984 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
1985 base::Unretained(this)),
1986 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
1987 base::Unretained(this)));
1989 message_loop_
.Run();
1991 filter
= fake_bluetooth_adapter_client_
->GetDiscoveryFilter();
1992 EXPECT_EQ("le", *filter
->transport
);
1993 EXPECT_EQ(-60, *filter
->rssi
);
1994 EXPECT_EQ(nullptr, filter
->pathloss
.get());
1995 uuids
= *filter
->uuids
;
1996 EXPECT_NE(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1000"));
1997 EXPECT_NE(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1001"));
1998 EXPECT_NE(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1020"));
2000 BluetoothDiscoveryFilter
* df3
= new BluetoothDiscoveryFilter(
2001 BluetoothDiscoveryFilter::Transport::TRANSPORT_CLASSIC
);
2003 df3
->AddUUID(BluetoothUUID("1020"));
2004 df3
->AddUUID(BluetoothUUID("1003"));
2005 scoped_ptr
<BluetoothDiscoveryFilter
> discovery_filter3(df3
);
2007 adapter_
->StartDiscoverySessionWithFilter(
2008 discovery_filter3
.Pass(),
2009 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
2010 base::Unretained(this)),
2011 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
2012 base::Unretained(this)));
2014 message_loop_
.Run();
2016 filter
= fake_bluetooth_adapter_client_
->GetDiscoveryFilter();
2017 EXPECT_EQ("auto", *filter
->transport
);
2018 EXPECT_EQ(-65, *filter
->rssi
);
2019 EXPECT_EQ(nullptr, filter
->pathloss
.get());
2020 uuids
= *filter
->uuids
;
2021 EXPECT_NE(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1000"));
2022 EXPECT_NE(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1001"));
2023 EXPECT_NE(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1003"));
2024 EXPECT_NE(uuids
.end(), std::find(uuids
.begin(), uuids
.end(), "1020"));
2026 // start additionally classic scan
2027 adapter_
->StartDiscoverySession(
2028 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
2029 base::Unretained(this)),
2030 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
2031 base::Unretained(this)));
2033 message_loop_
.Run();
2035 filter
= fake_bluetooth_adapter_client_
->GetDiscoveryFilter();
2036 EXPECT_EQ("auto", *filter
->transport
);
2037 EXPECT_EQ(nullptr, filter
->rssi
.get());
2038 EXPECT_EQ(nullptr, filter
->pathloss
.get());
2039 EXPECT_EQ(nullptr, filter
->uuids
.get());
2041 // Request to stop discovery 4 times.
2042 for (int i
= 3; i
>= 0; i
--) {
2043 discovery_sessions_
[i
]->Stop(
2044 base::Bind(&BluetoothChromeOSTest::Callback
, base::Unretained(this)),
2045 base::Bind(&BluetoothChromeOSTest::ErrorCallback
,
2046 base::Unretained(this)));
2048 // Every session stopping would trigger filter update
2049 message_loop_
.Run();
2053 TEST_F(BluetoothChromeOSTest
, DeviceProperties
) {
2056 BluetoothAdapter::DeviceList devices
= adapter_
->GetDevices();
2057 ASSERT_EQ(2U, devices
.size());
2058 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress
,
2059 devices
[0]->GetAddress());
2061 // Verify the other device properties.
2062 EXPECT_EQ(base::UTF8ToUTF16(FakeBluetoothDeviceClient::kPairedDeviceName
),
2063 devices
[0]->GetName());
2064 EXPECT_EQ(BluetoothDevice::DEVICE_COMPUTER
, devices
[0]->GetDeviceType());
2065 EXPECT_TRUE(devices
[0]->IsPaired());
2066 EXPECT_FALSE(devices
[0]->IsConnected());
2067 EXPECT_FALSE(devices
[0]->IsConnecting());
2069 // Non HID devices are always connectable.
2070 EXPECT_TRUE(devices
[0]->IsConnectable());
2072 BluetoothDevice::UUIDList uuids
= devices
[0]->GetUUIDs();
2073 ASSERT_EQ(2U, uuids
.size());
2074 EXPECT_EQ(uuids
[0], BluetoothUUID("1800"));
2075 EXPECT_EQ(uuids
[1], BluetoothUUID("1801"));
2077 EXPECT_EQ(BluetoothDevice::VENDOR_ID_USB
, devices
[0]->GetVendorIDSource());
2078 EXPECT_EQ(0x05ac, devices
[0]->GetVendorID());
2079 EXPECT_EQ(0x030d, devices
[0]->GetProductID());
2080 EXPECT_EQ(0x0306, devices
[0]->GetDeviceID());
2083 TEST_F(BluetoothChromeOSTest
, DeviceClassChanged
) {
2084 // Simulate a change of class of a device, as sometimes occurs
2085 // during discovery.
2088 BluetoothAdapter::DeviceList devices
= adapter_
->GetDevices();
2089 ASSERT_EQ(2U, devices
.size());
2090 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress
,
2091 devices
[0]->GetAddress());
2092 ASSERT_EQ(BluetoothDevice::DEVICE_COMPUTER
, devices
[0]->GetDeviceType());
2094 // Install an observer; expect the DeviceChanged method to be called when
2095 // we change the class of the device.
2096 TestBluetoothAdapterObserver
observer(adapter_
);
2098 FakeBluetoothDeviceClient::Properties
* properties
=
2099 fake_bluetooth_device_client_
->GetProperties(
2100 dbus::ObjectPath(FakeBluetoothDeviceClient::kPairedDevicePath
));
2102 properties
->bluetooth_class
.ReplaceValue(0x002580);
2104 EXPECT_EQ(1, observer
.device_changed_count());
2105 EXPECT_EQ(devices
[0], observer
.last_device());
2107 EXPECT_EQ(BluetoothDevice::DEVICE_MOUSE
, devices
[0]->GetDeviceType());
2110 TEST_F(BluetoothChromeOSTest
, DeviceNameChanged
) {
2111 // Simulate a change of name of a device.
2114 BluetoothAdapter::DeviceList devices
= adapter_
->GetDevices();
2115 ASSERT_EQ(2U, devices
.size());
2116 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress
,
2117 devices
[0]->GetAddress());
2118 ASSERT_EQ(base::UTF8ToUTF16(FakeBluetoothDeviceClient::kPairedDeviceName
),
2119 devices
[0]->GetName());
2121 // Install an observer; expect the DeviceChanged method to be called when
2122 // we change the alias of the device.
2123 TestBluetoothAdapterObserver
observer(adapter_
);
2125 FakeBluetoothDeviceClient::Properties
* properties
=
2126 fake_bluetooth_device_client_
->GetProperties(
2127 dbus::ObjectPath(FakeBluetoothDeviceClient::kPairedDevicePath
));
2129 static const std::string
new_name("New Device Name");
2130 properties
->alias
.ReplaceValue(new_name
);
2132 EXPECT_EQ(1, observer
.device_changed_count());
2133 EXPECT_EQ(devices
[0], observer
.last_device());
2135 EXPECT_EQ(base::UTF8ToUTF16(new_name
), devices
[0]->GetName());
2138 TEST_F(BluetoothChromeOSTest
, DeviceUuidsChanged
) {
2139 // Simulate a change of advertised services of a device.
2142 BluetoothAdapter::DeviceList devices
= adapter_
->GetDevices();
2143 ASSERT_EQ(2U, devices
.size());
2144 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress
,
2145 devices
[0]->GetAddress());
2147 BluetoothDevice::UUIDList uuids
= devices
[0]->GetUUIDs();
2148 ASSERT_EQ(2U, uuids
.size());
2149 ASSERT_EQ(uuids
[0], BluetoothUUID("1800"));
2150 ASSERT_EQ(uuids
[1], BluetoothUUID("1801"));
2152 // Install an observer; expect the DeviceChanged method to be called when
2153 // we change the class of the device.
2154 TestBluetoothAdapterObserver
observer(adapter_
);
2156 FakeBluetoothDeviceClient::Properties
* properties
=
2157 fake_bluetooth_device_client_
->GetProperties(
2158 dbus::ObjectPath(FakeBluetoothDeviceClient::kPairedDevicePath
));
2160 std::vector
<std::string
> new_uuids
;
2161 new_uuids
.push_back(uuids
[0].canonical_value());
2162 new_uuids
.push_back(uuids
[1].canonical_value());
2163 new_uuids
.push_back("0000110c-0000-1000-8000-00805f9b34fb");
2164 new_uuids
.push_back("0000110e-0000-1000-8000-00805f9b34fb");
2165 new_uuids
.push_back("0000110a-0000-1000-8000-00805f9b34fb");
2167 properties
->uuids
.ReplaceValue(new_uuids
);
2169 EXPECT_EQ(1, observer
.device_changed_count());
2170 EXPECT_EQ(devices
[0], observer
.last_device());
2172 // Fetching the value should give the new one.
2173 uuids
= devices
[0]->GetUUIDs();
2174 ASSERT_EQ(5U, uuids
.size());
2175 EXPECT_EQ(uuids
[0], BluetoothUUID("1800"));
2176 EXPECT_EQ(uuids
[1], BluetoothUUID("1801"));
2177 EXPECT_EQ(uuids
[2], BluetoothUUID("110c"));
2178 EXPECT_EQ(uuids
[3], BluetoothUUID("110e"));
2179 EXPECT_EQ(uuids
[4], BluetoothUUID("110a"));
2182 TEST_F(BluetoothChromeOSTest
, ForgetDevice
) {
2185 BluetoothAdapter::DeviceList devices
= adapter_
->GetDevices();
2186 ASSERT_EQ(2U, devices
.size());
2187 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress
,
2188 devices
[0]->GetAddress());
2190 std::string address
= devices
[0]->GetAddress();
2192 // Install an observer; expect the DeviceRemoved method to be called
2193 // with the device we remove.
2194 TestBluetoothAdapterObserver
observer(adapter_
);
2196 devices
[0]->Forget(GetErrorCallback());
2197 EXPECT_EQ(0, error_callback_count_
);
2199 EXPECT_EQ(1, observer
.device_removed_count());
2200 EXPECT_EQ(address
, observer
.last_device_address());
2202 // GetDevices shouldn't return the device either.
2203 devices
= adapter_
->GetDevices();
2204 ASSERT_EQ(1U, devices
.size());
2207 TEST_F(BluetoothChromeOSTest
, ForgetUnpairedDevice
) {
2211 BluetoothDevice
* device
= adapter_
->GetDevice(
2212 FakeBluetoothDeviceClient::kConnectUnpairableAddress
);
2213 ASSERT_TRUE(device
!= NULL
);
2214 ASSERT_FALSE(device
->IsPaired());
2216 // Connect the device so it becomes trusted and remembered.
2217 device
->Connect(NULL
, GetCallback(),
2218 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
2219 base::Unretained(this)));
2221 ASSERT_EQ(1, callback_count_
);
2222 ASSERT_EQ(0, error_callback_count_
);
2223 callback_count_
= 0;
2225 ASSERT_TRUE(device
->IsConnected());
2226 ASSERT_FALSE(device
->IsConnecting());
2228 // Make sure the trusted property has been set to true.
2229 FakeBluetoothDeviceClient::Properties
* properties
=
2230 fake_bluetooth_device_client_
->GetProperties(
2231 dbus::ObjectPath(FakeBluetoothDeviceClient::kConnectUnpairablePath
));
2232 ASSERT_TRUE(properties
->trusted
.value());
2234 // Install an observer; expect the DeviceRemoved method to be called
2235 // with the device we remove.
2236 TestBluetoothAdapterObserver
observer(adapter_
);
2238 device
->Forget(GetErrorCallback());
2239 EXPECT_EQ(0, error_callback_count_
);
2241 EXPECT_EQ(1, observer
.device_removed_count());
2242 EXPECT_EQ(FakeBluetoothDeviceClient::kConnectUnpairableAddress
,
2243 observer
.last_device_address());
2245 // GetDevices shouldn't return the device either.
2246 device
= adapter_
->GetDevice(
2247 FakeBluetoothDeviceClient::kConnectUnpairableAddress
);
2248 EXPECT_FALSE(device
!= NULL
);
2251 TEST_F(BluetoothChromeOSTest
, ConnectPairedDevice
) {
2254 BluetoothDevice
* device
= adapter_
->GetDevice(
2255 FakeBluetoothDeviceClient::kPairedDeviceAddress
);
2256 ASSERT_TRUE(device
!= NULL
);
2257 ASSERT_TRUE(device
->IsPaired());
2259 TestBluetoothAdapterObserver
observer(adapter_
);
2261 // Connect without a pairing delegate; since the device is already Paired
2262 // this should succeed and the device should become connected.
2263 device
->Connect(NULL
, GetCallback(),
2264 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
2265 base::Unretained(this)));
2267 EXPECT_EQ(1, callback_count_
);
2268 EXPECT_EQ(0, error_callback_count_
);
2270 // Two changes for connecting, one for connected and one for for trusted
2271 // after connecting.
2272 EXPECT_EQ(4, observer
.device_changed_count());
2273 EXPECT_EQ(device
, observer
.last_device());
2275 EXPECT_TRUE(device
->IsConnected());
2276 EXPECT_FALSE(device
->IsConnecting());
2279 TEST_F(BluetoothChromeOSTest
, ConnectUnpairableDevice
) {
2283 BluetoothDevice
* device
= adapter_
->GetDevice(
2284 FakeBluetoothDeviceClient::kConnectUnpairableAddress
);
2285 ASSERT_TRUE(device
!= NULL
);
2286 ASSERT_FALSE(device
->IsPaired());
2288 TestBluetoothAdapterObserver
observer(adapter_
);
2290 // Connect without a pairing delegate; since the device does not require
2291 // pairing, this should succeed and the device should become connected.
2292 device
->Connect(NULL
, GetCallback(),
2293 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
2294 base::Unretained(this)));
2296 EXPECT_EQ(1, callback_count_
);
2297 EXPECT_EQ(0, error_callback_count_
);
2299 // Two changes for connecting, one for connected, one for for trusted after
2300 // connection, and one for the reconnect mode (IsConnectable).
2301 EXPECT_EQ(5, observer
.device_changed_count());
2302 EXPECT_EQ(device
, observer
.last_device());
2304 EXPECT_TRUE(device
->IsConnected());
2305 EXPECT_FALSE(device
->IsConnecting());
2307 // Make sure the trusted property has been set to true.
2308 FakeBluetoothDeviceClient::Properties
* properties
=
2309 fake_bluetooth_device_client_
->GetProperties(
2310 dbus::ObjectPath(FakeBluetoothDeviceClient::kConnectUnpairablePath
));
2311 EXPECT_TRUE(properties
->trusted
.value());
2313 // Verify is a HID device and is not connectable.
2314 BluetoothDevice::UUIDList uuids
= device
->GetUUIDs();
2315 ASSERT_EQ(1U, uuids
.size());
2316 EXPECT_EQ(uuids
[0], BluetoothUUID("1124"));
2317 EXPECT_FALSE(device
->IsConnectable());
2320 TEST_F(BluetoothChromeOSTest
, ConnectConnectedDevice
) {
2323 BluetoothDevice
* device
= adapter_
->GetDevice(
2324 FakeBluetoothDeviceClient::kPairedDeviceAddress
);
2325 ASSERT_TRUE(device
!= NULL
);
2326 ASSERT_TRUE(device
->IsPaired());
2328 device
->Connect(NULL
, GetCallback(),
2329 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
2330 base::Unretained(this)));
2332 ASSERT_EQ(1, callback_count_
);
2333 ASSERT_EQ(0, error_callback_count_
);
2334 callback_count_
= 0;
2336 ASSERT_TRUE(device
->IsConnected());
2338 // Connect again; since the device is already Connected, this shouldn't do
2339 // anything to initiate the connection.
2340 TestBluetoothAdapterObserver
observer(adapter_
);
2342 device
->Connect(NULL
, GetCallback(),
2343 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
2344 base::Unretained(this)));
2346 EXPECT_EQ(1, callback_count_
);
2347 EXPECT_EQ(0, error_callback_count_
);
2349 // The observer will be called because Connecting will toggle true and false,
2350 // and the trusted property will be updated to true.
2351 EXPECT_EQ(3, observer
.device_changed_count());
2353 EXPECT_TRUE(device
->IsConnected());
2354 EXPECT_FALSE(device
->IsConnecting());
2357 TEST_F(BluetoothChromeOSTest
, ConnectDeviceFails
) {
2361 BluetoothDevice
* device
= adapter_
->GetDevice(
2362 FakeBluetoothDeviceClient::kLegacyAutopairAddress
);
2363 ASSERT_TRUE(device
!= NULL
);
2364 ASSERT_FALSE(device
->IsPaired());
2366 TestBluetoothAdapterObserver
observer(adapter_
);
2368 // Connect without a pairing delegate; since the device requires pairing,
2369 // this should fail with an error.
2370 device
->Connect(NULL
, GetCallback(),
2371 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
2372 base::Unretained(this)));
2374 EXPECT_EQ(0, callback_count_
);
2375 EXPECT_EQ(1, error_callback_count_
);
2376 EXPECT_EQ(BluetoothDevice::ERROR_FAILED
, last_connect_error_
);
2378 EXPECT_EQ(2, observer
.device_changed_count());
2380 EXPECT_FALSE(device
->IsConnected());
2381 EXPECT_FALSE(device
->IsConnecting());
2384 TEST_F(BluetoothChromeOSTest
, DisconnectDevice
) {
2387 BluetoothDevice
* device
= adapter_
->GetDevice(
2388 FakeBluetoothDeviceClient::kPairedDeviceAddress
);
2389 ASSERT_TRUE(device
!= NULL
);
2390 ASSERT_TRUE(device
->IsPaired());
2392 device
->Connect(NULL
, GetCallback(),
2393 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
2394 base::Unretained(this)));
2396 ASSERT_EQ(1, callback_count_
);
2397 ASSERT_EQ(0, error_callback_count_
);
2398 callback_count_
= 0;
2400 ASSERT_TRUE(device
->IsConnected());
2401 ASSERT_FALSE(device
->IsConnecting());
2403 // Disconnect the device, we should see the observer method fire and the
2404 // device get dropped.
2405 TestBluetoothAdapterObserver
observer(adapter_
);
2407 device
->Disconnect(GetCallback(), GetErrorCallback());
2409 EXPECT_EQ(1, callback_count_
);
2410 EXPECT_EQ(0, error_callback_count_
);
2412 EXPECT_EQ(1, observer
.device_changed_count());
2413 EXPECT_EQ(device
, observer
.last_device());
2415 EXPECT_FALSE(device
->IsConnected());
2418 TEST_F(BluetoothChromeOSTest
, DisconnectUnconnectedDevice
) {
2421 BluetoothDevice
* device
= adapter_
->GetDevice(
2422 FakeBluetoothDeviceClient::kPairedDeviceAddress
);
2423 ASSERT_TRUE(device
!= NULL
);
2424 ASSERT_TRUE(device
->IsPaired());
2425 ASSERT_FALSE(device
->IsConnected());
2427 // Disconnect the device, we should see the observer method fire and the
2428 // device get dropped.
2429 TestBluetoothAdapterObserver
observer(adapter_
);
2431 device
->Disconnect(GetCallback(), GetErrorCallback());
2433 EXPECT_EQ(0, callback_count_
);
2434 EXPECT_EQ(1, error_callback_count_
);
2436 EXPECT_EQ(0, observer
.device_changed_count());
2438 EXPECT_FALSE(device
->IsConnected());
2441 TEST_F(BluetoothChromeOSTest
, PairLegacyAutopair
) {
2442 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
2447 // The Legacy Autopair device requires no PIN or Passkey to pair because
2448 // the daemon provides 0000 to the device for us.
2449 BluetoothDevice
* device
= adapter_
->GetDevice(
2450 FakeBluetoothDeviceClient::kLegacyAutopairAddress
);
2451 ASSERT_TRUE(device
!= NULL
);
2452 ASSERT_FALSE(device
->IsPaired());
2454 TestBluetoothAdapterObserver
observer(adapter_
);
2456 TestPairingDelegate pairing_delegate
;
2457 device
->Connect(&pairing_delegate
, GetCallback(),
2458 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
2459 base::Unretained(this)));
2461 EXPECT_EQ(0, pairing_delegate
.call_count_
);
2462 EXPECT_TRUE(device
->IsConnecting());
2464 message_loop_
.Run();
2466 EXPECT_EQ(1, callback_count_
);
2467 EXPECT_EQ(0, error_callback_count_
);
2469 // Two changes for connecting, one change for connected, one for paired,
2470 // two for trusted (after pairing and connection), and one for the reconnect
2471 // mode (IsConnectable).
2472 EXPECT_EQ(7, observer
.device_changed_count());
2473 EXPECT_EQ(device
, observer
.last_device());
2475 EXPECT_TRUE(device
->IsConnected());
2476 EXPECT_FALSE(device
->IsConnecting());
2478 EXPECT_TRUE(device
->IsPaired());
2480 // Verify is a HID device and is connectable.
2481 BluetoothDevice::UUIDList uuids
= device
->GetUUIDs();
2482 ASSERT_EQ(1U, uuids
.size());
2483 EXPECT_EQ(uuids
[0], BluetoothUUID("1124"));
2484 EXPECT_TRUE(device
->IsConnectable());
2486 // Make sure the trusted property has been set to true.
2487 FakeBluetoothDeviceClient::Properties
* properties
=
2488 fake_bluetooth_device_client_
->GetProperties(
2489 dbus::ObjectPath(FakeBluetoothDeviceClient::kLegacyAutopairPath
));
2490 EXPECT_TRUE(properties
->trusted
.value());
2493 TEST_F(BluetoothChromeOSTest
, PairDisplayPinCode
) {
2494 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
2499 // Requires that we display a randomly generated PIN on the screen.
2500 BluetoothDevice
* device
= adapter_
->GetDevice(
2501 FakeBluetoothDeviceClient::kDisplayPinCodeAddress
);
2502 ASSERT_TRUE(device
!= NULL
);
2503 ASSERT_FALSE(device
->IsPaired());
2505 TestBluetoothAdapterObserver
observer(adapter_
);
2507 TestPairingDelegate pairing_delegate
;
2508 device
->Connect(&pairing_delegate
, GetCallback(),
2509 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
2510 base::Unretained(this)));
2512 EXPECT_EQ(1, pairing_delegate
.call_count_
);
2513 EXPECT_EQ(1, pairing_delegate
.display_pincode_count_
);
2514 EXPECT_EQ("123456", pairing_delegate
.last_pincode_
);
2515 EXPECT_TRUE(device
->IsConnecting());
2517 message_loop_
.Run();
2519 EXPECT_EQ(1, callback_count_
);
2520 EXPECT_EQ(0, error_callback_count_
);
2522 // Two changes for connecting, one change for connected, one for paired,
2523 // two for trusted (after pairing and connection), and one for the reconnect
2524 // mode (IsConnectable).
2525 EXPECT_EQ(7, observer
.device_changed_count());
2526 EXPECT_EQ(device
, observer
.last_device());
2528 EXPECT_TRUE(device
->IsConnected());
2529 EXPECT_FALSE(device
->IsConnecting());
2531 EXPECT_TRUE(device
->IsPaired());
2533 // Verify is a HID device and is connectable.
2534 BluetoothDevice::UUIDList uuids
= device
->GetUUIDs();
2535 ASSERT_EQ(1U, uuids
.size());
2536 EXPECT_EQ(uuids
[0], BluetoothUUID("1124"));
2537 EXPECT_TRUE(device
->IsConnectable());
2539 // Make sure the trusted property has been set to true.
2540 FakeBluetoothDeviceClient::Properties
* properties
=
2541 fake_bluetooth_device_client_
->GetProperties(
2542 dbus::ObjectPath(FakeBluetoothDeviceClient::kDisplayPinCodePath
));
2543 EXPECT_TRUE(properties
->trusted
.value());
2546 TEST_F(BluetoothChromeOSTest
, PairDisplayPasskey
) {
2547 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
2552 // Requires that we display a randomly generated Passkey on the screen,
2553 // and notifies us as it's typed in.
2554 BluetoothDevice
* device
= adapter_
->GetDevice(
2555 FakeBluetoothDeviceClient::kDisplayPasskeyAddress
);
2556 ASSERT_TRUE(device
!= NULL
);
2557 ASSERT_FALSE(device
->IsPaired());
2559 TestBluetoothAdapterObserver
observer(adapter_
);
2561 TestPairingDelegate pairing_delegate
;
2562 device
->Connect(&pairing_delegate
, GetCallback(),
2563 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
2564 base::Unretained(this)));
2566 // One call for DisplayPasskey() and one for KeysEntered().
2567 EXPECT_EQ(2, pairing_delegate
.call_count_
);
2568 EXPECT_EQ(1, pairing_delegate
.display_passkey_count_
);
2569 EXPECT_EQ(123456U, pairing_delegate
.last_passkey_
);
2570 EXPECT_EQ(1, pairing_delegate
.keys_entered_count_
);
2571 EXPECT_EQ(0U, pairing_delegate
.last_entered_
);
2573 EXPECT_TRUE(device
->IsConnecting());
2575 // One call to KeysEntered() for each key, including [enter].
2576 for(int i
= 1; i
<= 7; ++i
) {
2577 message_loop_
.Run();
2579 EXPECT_EQ(2 + i
, pairing_delegate
.call_count_
);
2580 EXPECT_EQ(1 + i
, pairing_delegate
.keys_entered_count_
);
2581 EXPECT_EQ(static_cast<uint32_t>(i
), pairing_delegate
.last_entered_
);
2584 message_loop_
.Run();
2586 // 8 KeysEntered notifications (0 to 7, inclusive) and one aditional call for
2587 // DisplayPasskey().
2588 EXPECT_EQ(9, pairing_delegate
.call_count_
);
2589 EXPECT_EQ(8, pairing_delegate
.keys_entered_count_
);
2590 EXPECT_EQ(7U, pairing_delegate
.last_entered_
);
2592 EXPECT_EQ(1, callback_count_
);
2593 EXPECT_EQ(0, error_callback_count_
);
2595 // Two changes for connecting, one change for connected, one for paired,
2596 // two for trusted (after pairing and connection), and one for the reconnect
2597 // mode (IsConnectable).
2598 EXPECT_EQ(7, observer
.device_changed_count());
2599 EXPECT_EQ(device
, observer
.last_device());
2601 EXPECT_TRUE(device
->IsConnected());
2602 EXPECT_FALSE(device
->IsConnecting());
2604 EXPECT_TRUE(device
->IsPaired());
2606 // Verify is a HID device.
2607 BluetoothDevice::UUIDList uuids
= device
->GetUUIDs();
2608 ASSERT_EQ(1U, uuids
.size());
2609 EXPECT_EQ(uuids
[0], BluetoothUUID("1124"));
2611 // And usually not connectable.
2612 EXPECT_FALSE(device
->IsConnectable());
2614 // Make sure the trusted property has been set to true.
2615 FakeBluetoothDeviceClient::Properties
* properties
=
2616 fake_bluetooth_device_client_
->GetProperties(
2617 dbus::ObjectPath(FakeBluetoothDeviceClient::kDisplayPasskeyPath
));
2618 EXPECT_TRUE(properties
->trusted
.value());
2621 TEST_F(BluetoothChromeOSTest
, PairRequestPinCode
) {
2622 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
2627 // Requires that the user enters a PIN for them.
2628 BluetoothDevice
* device
= adapter_
->GetDevice(
2629 FakeBluetoothDeviceClient::kRequestPinCodeAddress
);
2630 ASSERT_TRUE(device
!= NULL
);
2631 ASSERT_FALSE(device
->IsPaired());
2633 TestBluetoothAdapterObserver
observer(adapter_
);
2635 TestPairingDelegate pairing_delegate
;
2636 device
->Connect(&pairing_delegate
, GetCallback(),
2637 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
2638 base::Unretained(this)));
2640 EXPECT_EQ(1, pairing_delegate
.call_count_
);
2641 EXPECT_EQ(1, pairing_delegate
.request_pincode_count_
);
2642 EXPECT_TRUE(device
->IsConnecting());
2645 device
->SetPinCode("1234");
2646 message_loop_
.Run();
2648 EXPECT_EQ(1, callback_count_
);
2649 EXPECT_EQ(0, error_callback_count_
);
2651 // Two changes for connecting, one change for connected, one for paired and
2652 // two for trusted (after pairing and connection).
2653 EXPECT_EQ(6, observer
.device_changed_count());
2654 EXPECT_EQ(device
, observer
.last_device());
2656 EXPECT_TRUE(device
->IsConnected());
2657 EXPECT_FALSE(device
->IsConnecting());
2659 EXPECT_TRUE(device
->IsPaired());
2661 // Verify is not a HID device.
2662 BluetoothDevice::UUIDList uuids
= device
->GetUUIDs();
2663 ASSERT_EQ(0U, uuids
.size());
2665 // Non HID devices are always connectable.
2666 EXPECT_TRUE(device
->IsConnectable());
2668 // Make sure the trusted property has been set to true.
2669 FakeBluetoothDeviceClient::Properties
* properties
=
2670 fake_bluetooth_device_client_
->GetProperties(
2671 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath
));
2672 EXPECT_TRUE(properties
->trusted
.value());
2675 TEST_F(BluetoothChromeOSTest
, PairConfirmPasskey
) {
2676 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
2681 // Requests that we confirm a displayed passkey.
2682 BluetoothDevice
* device
= adapter_
->GetDevice(
2683 FakeBluetoothDeviceClient::kConfirmPasskeyAddress
);
2684 ASSERT_TRUE(device
!= NULL
);
2685 ASSERT_FALSE(device
->IsPaired());
2687 TestBluetoothAdapterObserver
observer(adapter_
);
2689 TestPairingDelegate pairing_delegate
;
2690 device
->Connect(&pairing_delegate
, GetCallback(),
2691 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
2692 base::Unretained(this)));
2694 EXPECT_EQ(1, pairing_delegate
.call_count_
);
2695 EXPECT_EQ(1, pairing_delegate
.confirm_passkey_count_
);
2696 EXPECT_EQ(123456U, pairing_delegate
.last_passkey_
);
2697 EXPECT_TRUE(device
->IsConnecting());
2699 // Confirm the passkey.
2700 device
->ConfirmPairing();
2701 message_loop_
.Run();
2703 EXPECT_EQ(1, callback_count_
);
2704 EXPECT_EQ(0, error_callback_count_
);
2706 // Two changes for connecting, one change for connected, one for paired and
2707 // two for trusted (after pairing and connection).
2708 EXPECT_EQ(6, observer
.device_changed_count());
2709 EXPECT_EQ(device
, observer
.last_device());
2711 EXPECT_TRUE(device
->IsConnected());
2712 EXPECT_FALSE(device
->IsConnecting());
2714 EXPECT_TRUE(device
->IsPaired());
2716 // Non HID devices are always connectable.
2717 EXPECT_TRUE(device
->IsConnectable());
2719 // Make sure the trusted property has been set to true.
2720 FakeBluetoothDeviceClient::Properties
* properties
=
2721 fake_bluetooth_device_client_
->GetProperties(
2722 dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath
));
2723 EXPECT_TRUE(properties
->trusted
.value());
2726 TEST_F(BluetoothChromeOSTest
, PairRequestPasskey
) {
2727 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
2732 // Requires that the user enters a Passkey, this would be some kind of
2733 // device that has a display, but doesn't use "just works" - maybe a car?
2734 BluetoothDevice
* device
= adapter_
->GetDevice(
2735 FakeBluetoothDeviceClient::kRequestPasskeyAddress
);
2736 ASSERT_TRUE(device
!= NULL
);
2737 ASSERT_FALSE(device
->IsPaired());
2739 TestBluetoothAdapterObserver
observer(adapter_
);
2741 TestPairingDelegate pairing_delegate
;
2742 device
->Connect(&pairing_delegate
, GetCallback(),
2743 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
2744 base::Unretained(this)));
2746 EXPECT_EQ(1, pairing_delegate
.call_count_
);
2747 EXPECT_EQ(1, pairing_delegate
.request_passkey_count_
);
2748 EXPECT_TRUE(device
->IsConnecting());
2751 device
->SetPasskey(1234);
2752 message_loop_
.Run();
2754 EXPECT_EQ(1, callback_count_
);
2755 EXPECT_EQ(0, error_callback_count_
);
2757 // Two changes for connecting, one change for connected, one for paired and
2758 // two for trusted (after pairing and connection).
2759 EXPECT_EQ(6, observer
.device_changed_count());
2760 EXPECT_EQ(device
, observer
.last_device());
2762 EXPECT_TRUE(device
->IsConnected());
2763 EXPECT_FALSE(device
->IsConnecting());
2765 EXPECT_TRUE(device
->IsPaired());
2767 // Non HID devices are always connectable.
2768 EXPECT_TRUE(device
->IsConnectable());
2770 // Make sure the trusted property has been set to true.
2771 FakeBluetoothDeviceClient::Properties
* properties
=
2772 fake_bluetooth_device_client_
->GetProperties(
2773 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath
));
2774 EXPECT_TRUE(properties
->trusted
.value());
2777 TEST_F(BluetoothChromeOSTest
, PairJustWorks
) {
2778 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
2783 // Uses just-works pairing, since this is an outgoing pairing, no delegate
2784 // interaction is required.
2785 BluetoothDevice
* device
= adapter_
->GetDevice(
2786 FakeBluetoothDeviceClient::kJustWorksAddress
);
2787 ASSERT_TRUE(device
!= NULL
);
2788 ASSERT_FALSE(device
->IsPaired());
2790 TestBluetoothAdapterObserver
observer(adapter_
);
2792 TestPairingDelegate pairing_delegate
;
2793 device
->Connect(&pairing_delegate
, GetCallback(),
2794 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
2795 base::Unretained(this)));
2797 EXPECT_EQ(0, pairing_delegate
.call_count_
);
2799 message_loop_
.Run();
2801 EXPECT_EQ(1, callback_count_
);
2802 EXPECT_EQ(0, error_callback_count_
);
2804 // Two changes for connecting, one change for connected, one for paired and
2805 // two for trusted (after pairing and connection).
2806 EXPECT_EQ(6, observer
.device_changed_count());
2807 EXPECT_EQ(device
, observer
.last_device());
2809 EXPECT_TRUE(device
->IsConnected());
2810 EXPECT_FALSE(device
->IsConnecting());
2812 EXPECT_TRUE(device
->IsPaired());
2814 // Non HID devices are always connectable.
2815 EXPECT_TRUE(device
->IsConnectable());
2817 // Make sure the trusted property has been set to true.
2818 FakeBluetoothDeviceClient::Properties
* properties
=
2819 fake_bluetooth_device_client_
->GetProperties(
2820 dbus::ObjectPath(FakeBluetoothDeviceClient::kJustWorksPath
));
2821 EXPECT_TRUE(properties
->trusted
.value());
2824 TEST_F(BluetoothChromeOSTest
, PairUnpairableDeviceFails
) {
2825 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
2828 DiscoverDevice(FakeBluetoothDeviceClient::kUnconnectableDeviceAddress
);
2830 BluetoothDevice
* device
= adapter_
->GetDevice(
2831 FakeBluetoothDeviceClient::kUnpairableDeviceAddress
);
2832 ASSERT_TRUE(device
!= NULL
);
2833 ASSERT_FALSE(device
->IsPaired());
2835 TestBluetoothAdapterObserver
observer(adapter_
);
2837 TestPairingDelegate pairing_delegate
;
2838 device
->Connect(&pairing_delegate
, GetCallback(),
2839 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
2840 base::Unretained(this)));
2842 EXPECT_EQ(0, pairing_delegate
.call_count_
);
2843 EXPECT_TRUE(device
->IsConnecting());
2845 // Run the loop to get the error..
2846 message_loop_
.Run();
2848 EXPECT_EQ(0, callback_count_
);
2849 EXPECT_EQ(1, error_callback_count_
);
2851 EXPECT_EQ(BluetoothDevice::ERROR_FAILED
, last_connect_error_
);
2853 EXPECT_FALSE(device
->IsConnected());
2854 EXPECT_FALSE(device
->IsConnecting());
2855 EXPECT_FALSE(device
->IsPaired());
2858 TEST_F(BluetoothChromeOSTest
, PairingFails
) {
2859 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
2862 DiscoverDevice(FakeBluetoothDeviceClient::kVanishingDeviceAddress
);
2864 // The vanishing device times out during pairing
2865 BluetoothDevice
* device
= adapter_
->GetDevice(
2866 FakeBluetoothDeviceClient::kVanishingDeviceAddress
);
2867 ASSERT_TRUE(device
!= NULL
);
2868 ASSERT_FALSE(device
->IsPaired());
2870 TestBluetoothAdapterObserver
observer(adapter_
);
2872 TestPairingDelegate pairing_delegate
;
2873 device
->Connect(&pairing_delegate
, GetCallback(),
2874 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
2875 base::Unretained(this)));
2877 EXPECT_EQ(0, pairing_delegate
.call_count_
);
2878 EXPECT_TRUE(device
->IsConnecting());
2880 // Run the loop to get the error..
2881 message_loop_
.Run();
2883 EXPECT_EQ(0, callback_count_
);
2884 EXPECT_EQ(1, error_callback_count_
);
2886 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_TIMEOUT
, last_connect_error_
);
2888 EXPECT_FALSE(device
->IsConnected());
2889 EXPECT_FALSE(device
->IsConnecting());
2890 EXPECT_FALSE(device
->IsPaired());
2893 TEST_F(BluetoothChromeOSTest
, PairingFailsAtConnection
) {
2894 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
2899 // Everything seems to go according to plan with the unconnectable device;
2900 // it pairs, but then you can't make connections to it after.
2901 BluetoothDevice
* device
= adapter_
->GetDevice(
2902 FakeBluetoothDeviceClient::kUnconnectableDeviceAddress
);
2903 ASSERT_TRUE(device
!= NULL
);
2904 ASSERT_FALSE(device
->IsPaired());
2906 TestBluetoothAdapterObserver
observer(adapter_
);
2908 TestPairingDelegate pairing_delegate
;
2909 device
->Connect(&pairing_delegate
, GetCallback(),
2910 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
2911 base::Unretained(this)));
2913 EXPECT_EQ(0, pairing_delegate
.call_count_
);
2914 EXPECT_TRUE(device
->IsConnecting());
2916 message_loop_
.Run();
2918 EXPECT_EQ(0, callback_count_
);
2919 EXPECT_EQ(1, error_callback_count_
);
2920 EXPECT_EQ(BluetoothDevice::ERROR_FAILED
, last_connect_error_
);
2922 // Two changes for connecting, one for paired and one for trusted after
2923 // pairing. The device should not be connected.
2924 EXPECT_EQ(4, observer
.device_changed_count());
2925 EXPECT_EQ(device
, observer
.last_device());
2927 EXPECT_FALSE(device
->IsConnected());
2928 EXPECT_FALSE(device
->IsConnecting());
2930 EXPECT_TRUE(device
->IsPaired());
2932 // Make sure the trusted property has been set to true still (since pairing
2934 FakeBluetoothDeviceClient::Properties
* properties
=
2935 fake_bluetooth_device_client_
->GetProperties(
2937 FakeBluetoothDeviceClient::kUnconnectableDevicePath
));
2938 EXPECT_TRUE(properties
->trusted
.value());
2941 TEST_F(BluetoothChromeOSTest
, PairingRejectedAtPinCode
) {
2942 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
2947 // Reject the pairing after we receive a request for the PIN code.
2948 BluetoothDevice
* device
= adapter_
->GetDevice(
2949 FakeBluetoothDeviceClient::kRequestPinCodeAddress
);
2950 ASSERT_TRUE(device
!= NULL
);
2951 ASSERT_FALSE(device
->IsPaired());
2953 TestBluetoothAdapterObserver
observer(adapter_
);
2955 TestPairingDelegate pairing_delegate
;
2956 device
->Connect(&pairing_delegate
, GetCallback(),
2957 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
2958 base::Unretained(this)));
2960 EXPECT_EQ(1, pairing_delegate
.call_count_
);
2961 EXPECT_EQ(1, pairing_delegate
.request_pincode_count_
);
2962 EXPECT_TRUE(device
->IsConnecting());
2964 // Reject the pairing.
2965 device
->RejectPairing();
2966 message_loop_
.Run();
2968 EXPECT_EQ(0, callback_count_
);
2969 EXPECT_EQ(1, error_callback_count_
);
2970 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_REJECTED
, last_connect_error_
);
2972 // Should be no changes except connecting going true and false.
2973 EXPECT_EQ(2, observer
.device_changed_count());
2974 EXPECT_FALSE(device
->IsConnected());
2975 EXPECT_FALSE(device
->IsConnecting());
2976 EXPECT_FALSE(device
->IsPaired());
2979 TEST_F(BluetoothChromeOSTest
, PairingCancelledAtPinCode
) {
2980 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
2985 // Cancel the pairing after we receive a request for the PIN code.
2986 BluetoothDevice
* device
= adapter_
->GetDevice(
2987 FakeBluetoothDeviceClient::kRequestPinCodeAddress
);
2988 ASSERT_TRUE(device
!= NULL
);
2989 ASSERT_FALSE(device
->IsPaired());
2991 TestBluetoothAdapterObserver
observer(adapter_
);
2993 TestPairingDelegate pairing_delegate
;
2994 device
->Connect(&pairing_delegate
, GetCallback(),
2995 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
2996 base::Unretained(this)));
2998 EXPECT_EQ(1, pairing_delegate
.call_count_
);
2999 EXPECT_EQ(1, pairing_delegate
.request_pincode_count_
);
3000 EXPECT_TRUE(device
->IsConnecting());
3002 // Cancel the pairing.
3003 device
->CancelPairing();
3004 message_loop_
.Run();
3006 EXPECT_EQ(0, callback_count_
);
3007 EXPECT_EQ(1, error_callback_count_
);
3008 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED
, last_connect_error_
);
3010 // Should be no changes except connecting going true and false.
3011 EXPECT_EQ(2, observer
.device_changed_count());
3012 EXPECT_FALSE(device
->IsConnected());
3013 EXPECT_FALSE(device
->IsConnecting());
3014 EXPECT_FALSE(device
->IsPaired());
3017 TEST_F(BluetoothChromeOSTest
, PairingRejectedAtPasskey
) {
3018 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
3023 // Reject the pairing after we receive a request for the passkey.
3024 BluetoothDevice
* device
= adapter_
->GetDevice(
3025 FakeBluetoothDeviceClient::kRequestPasskeyAddress
);
3026 ASSERT_TRUE(device
!= NULL
);
3027 ASSERT_FALSE(device
->IsPaired());
3029 TestBluetoothAdapterObserver
observer(adapter_
);
3031 TestPairingDelegate pairing_delegate
;
3032 device
->Connect(&pairing_delegate
, GetCallback(),
3033 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
3034 base::Unretained(this)));
3036 EXPECT_EQ(1, pairing_delegate
.call_count_
);
3037 EXPECT_EQ(1, pairing_delegate
.request_passkey_count_
);
3038 EXPECT_TRUE(device
->IsConnecting());
3040 // Reject the pairing.
3041 device
->RejectPairing();
3042 message_loop_
.Run();
3044 EXPECT_EQ(0, callback_count_
);
3045 EXPECT_EQ(1, error_callback_count_
);
3046 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_REJECTED
, last_connect_error_
);
3048 // Should be no changes except connecting going true and false.
3049 EXPECT_EQ(2, observer
.device_changed_count());
3050 EXPECT_FALSE(device
->IsConnected());
3051 EXPECT_FALSE(device
->IsConnecting());
3052 EXPECT_FALSE(device
->IsPaired());
3055 TEST_F(BluetoothChromeOSTest
, PairingCancelledAtPasskey
) {
3056 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
3061 // Cancel the pairing after we receive a request for the passkey.
3062 BluetoothDevice
* device
= adapter_
->GetDevice(
3063 FakeBluetoothDeviceClient::kRequestPasskeyAddress
);
3064 ASSERT_TRUE(device
!= NULL
);
3065 ASSERT_FALSE(device
->IsPaired());
3067 TestBluetoothAdapterObserver
observer(adapter_
);
3069 TestPairingDelegate pairing_delegate
;
3070 device
->Connect(&pairing_delegate
, GetCallback(),
3071 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
3072 base::Unretained(this)));
3074 EXPECT_EQ(1, pairing_delegate
.call_count_
);
3075 EXPECT_EQ(1, pairing_delegate
.request_passkey_count_
);
3076 EXPECT_TRUE(device
->IsConnecting());
3078 // Cancel the pairing.
3079 device
->CancelPairing();
3080 message_loop_
.Run();
3082 EXPECT_EQ(0, callback_count_
);
3083 EXPECT_EQ(1, error_callback_count_
);
3084 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED
, last_connect_error_
);
3086 // Should be no changes except connecting going true and false.
3087 EXPECT_EQ(2, observer
.device_changed_count());
3088 EXPECT_FALSE(device
->IsConnected());
3089 EXPECT_FALSE(device
->IsConnecting());
3090 EXPECT_FALSE(device
->IsPaired());
3093 TEST_F(BluetoothChromeOSTest
, PairingRejectedAtConfirmation
) {
3094 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
3099 // Reject the pairing after we receive a request for passkey confirmation.
3100 BluetoothDevice
* device
= adapter_
->GetDevice(
3101 FakeBluetoothDeviceClient::kConfirmPasskeyAddress
);
3102 ASSERT_TRUE(device
!= NULL
);
3103 ASSERT_FALSE(device
->IsPaired());
3105 TestBluetoothAdapterObserver
observer(adapter_
);
3107 TestPairingDelegate pairing_delegate
;
3108 device
->Connect(&pairing_delegate
, GetCallback(),
3109 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
3110 base::Unretained(this)));
3112 EXPECT_EQ(1, pairing_delegate
.call_count_
);
3113 EXPECT_EQ(1, pairing_delegate
.confirm_passkey_count_
);
3114 EXPECT_TRUE(device
->IsConnecting());
3116 // Reject the pairing.
3117 device
->RejectPairing();
3118 message_loop_
.Run();
3120 EXPECT_EQ(0, callback_count_
);
3121 EXPECT_EQ(1, error_callback_count_
);
3122 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_REJECTED
, last_connect_error_
);
3124 // Should be no changes except connecting going true and false.
3125 EXPECT_EQ(2, observer
.device_changed_count());
3126 EXPECT_FALSE(device
->IsConnected());
3127 EXPECT_FALSE(device
->IsConnecting());
3128 EXPECT_FALSE(device
->IsPaired());
3131 TEST_F(BluetoothChromeOSTest
, PairingCancelledAtConfirmation
) {
3132 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
3137 // Cancel the pairing after we receive a request for the passkey.
3138 BluetoothDevice
* device
= adapter_
->GetDevice(
3139 FakeBluetoothDeviceClient::kConfirmPasskeyAddress
);
3140 ASSERT_TRUE(device
!= NULL
);
3141 ASSERT_FALSE(device
->IsPaired());
3143 TestBluetoothAdapterObserver
observer(adapter_
);
3145 TestPairingDelegate pairing_delegate
;
3146 device
->Connect(&pairing_delegate
, GetCallback(),
3147 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
3148 base::Unretained(this)));
3150 EXPECT_EQ(1, pairing_delegate
.call_count_
);
3151 EXPECT_EQ(1, pairing_delegate
.confirm_passkey_count_
);
3152 EXPECT_TRUE(device
->IsConnecting());
3154 // Cancel the pairing.
3155 device
->CancelPairing();
3156 message_loop_
.Run();
3158 EXPECT_EQ(0, callback_count_
);
3159 EXPECT_EQ(1, error_callback_count_
);
3160 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED
, last_connect_error_
);
3162 // Should be no changes except connecting going true and false.
3163 EXPECT_EQ(2, observer
.device_changed_count());
3164 EXPECT_FALSE(device
->IsConnected());
3165 EXPECT_FALSE(device
->IsConnecting());
3166 EXPECT_FALSE(device
->IsPaired());
3169 TEST_F(BluetoothChromeOSTest
, PairingCancelledInFlight
) {
3170 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
3175 // Cancel the pairing while we're waiting for the remote host.
3176 BluetoothDevice
* device
= adapter_
->GetDevice(
3177 FakeBluetoothDeviceClient::kLegacyAutopairAddress
);
3178 ASSERT_TRUE(device
!= NULL
);
3179 ASSERT_FALSE(device
->IsPaired());
3181 TestBluetoothAdapterObserver
observer(adapter_
);
3183 TestPairingDelegate pairing_delegate
;
3184 device
->Connect(&pairing_delegate
, GetCallback(),
3185 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
3186 base::Unretained(this)));
3188 EXPECT_EQ(0, pairing_delegate
.call_count_
);
3189 EXPECT_TRUE(device
->IsConnecting());
3191 // Cancel the pairing.
3192 device
->CancelPairing();
3193 message_loop_
.Run();
3195 EXPECT_EQ(0, callback_count_
);
3196 EXPECT_EQ(1, error_callback_count_
);
3197 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED
, last_connect_error_
);
3199 // Should be no changes except connecting going true and false.
3200 EXPECT_EQ(2, observer
.device_changed_count());
3201 EXPECT_FALSE(device
->IsConnected());
3202 EXPECT_FALSE(device
->IsConnecting());
3203 EXPECT_FALSE(device
->IsPaired());
3206 TEST_F(BluetoothChromeOSTest
, IncomingPairRequestPinCode
) {
3207 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
3211 TestPairingDelegate pairing_delegate
;
3212 adapter_
->AddPairingDelegate(
3214 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH
);
3216 // Requires that we provide a PIN code.
3217 fake_bluetooth_device_client_
->CreateDevice(
3218 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
),
3219 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath
));
3220 BluetoothDevice
* device
= adapter_
->GetDevice(
3221 FakeBluetoothDeviceClient::kRequestPinCodeAddress
);
3222 ASSERT_TRUE(device
!= NULL
);
3223 ASSERT_FALSE(device
->IsPaired());
3225 TestBluetoothAdapterObserver
observer(adapter_
);
3227 fake_bluetooth_device_client_
->SimulatePairing(
3228 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath
), true,
3229 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback
,
3230 base::Unretained(this)));
3232 EXPECT_EQ(1, pairing_delegate
.call_count_
);
3233 EXPECT_EQ(1, pairing_delegate
.request_pincode_count_
);
3236 device
->SetPinCode("1234");
3237 message_loop_
.Run();
3239 EXPECT_EQ(1, callback_count_
);
3240 EXPECT_EQ(0, error_callback_count_
);
3242 // One change for paired, and one for trusted.
3243 EXPECT_EQ(2, observer
.device_changed_count());
3244 EXPECT_EQ(device
, observer
.last_device());
3246 EXPECT_TRUE(device
->IsPaired());
3248 // Make sure the trusted property has been set to true.
3249 FakeBluetoothDeviceClient::Properties
* properties
=
3250 fake_bluetooth_device_client_
->GetProperties(
3251 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath
));
3252 ASSERT_TRUE(properties
->trusted
.value());
3254 // No pairing context should remain on the device.
3255 BluetoothDeviceChromeOS
* device_chromeos
=
3256 static_cast<BluetoothDeviceChromeOS
*>(device
);
3257 EXPECT_TRUE(device_chromeos
->GetPairing() == NULL
);
3260 TEST_F(BluetoothChromeOSTest
, IncomingPairConfirmPasskey
) {
3261 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
3265 TestPairingDelegate pairing_delegate
;
3266 adapter_
->AddPairingDelegate(
3268 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH
);
3270 // Requests that we confirm a displayed passkey.
3271 fake_bluetooth_device_client_
->CreateDevice(
3272 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
),
3273 dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath
));
3274 BluetoothDevice
* device
= adapter_
->GetDevice(
3275 FakeBluetoothDeviceClient::kConfirmPasskeyAddress
);
3276 ASSERT_TRUE(device
!= NULL
);
3277 ASSERT_FALSE(device
->IsPaired());
3279 TestBluetoothAdapterObserver
observer(adapter_
);
3281 fake_bluetooth_device_client_
->SimulatePairing(
3282 dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath
), true,
3283 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback
,
3284 base::Unretained(this)));
3286 EXPECT_EQ(1, pairing_delegate
.call_count_
);
3287 EXPECT_EQ(1, pairing_delegate
.confirm_passkey_count_
);
3288 EXPECT_EQ(123456U, pairing_delegate
.last_passkey_
);
3290 // Confirm the passkey.
3291 device
->ConfirmPairing();
3292 message_loop_
.Run();
3294 EXPECT_EQ(1, callback_count_
);
3295 EXPECT_EQ(0, error_callback_count_
);
3297 // One change for paired, and one for trusted.
3298 EXPECT_EQ(2, observer
.device_changed_count());
3299 EXPECT_EQ(device
, observer
.last_device());
3301 EXPECT_TRUE(device
->IsPaired());
3303 // Make sure the trusted property has been set to true.
3304 FakeBluetoothDeviceClient::Properties
* properties
=
3305 fake_bluetooth_device_client_
->GetProperties(
3306 dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath
));
3307 ASSERT_TRUE(properties
->trusted
.value());
3309 // No pairing context should remain on the device.
3310 BluetoothDeviceChromeOS
* device_chromeos
=
3311 static_cast<BluetoothDeviceChromeOS
*>(device
);
3312 EXPECT_TRUE(device_chromeos
->GetPairing() == NULL
);
3315 TEST_F(BluetoothChromeOSTest
, IncomingPairRequestPasskey
) {
3316 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
3320 TestPairingDelegate pairing_delegate
;
3321 adapter_
->AddPairingDelegate(
3323 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH
);
3325 // Requests that we provide a Passkey.
3326 fake_bluetooth_device_client_
->CreateDevice(
3327 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
),
3328 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath
));
3329 BluetoothDevice
* device
= adapter_
->GetDevice(
3330 FakeBluetoothDeviceClient::kRequestPasskeyAddress
);
3331 ASSERT_TRUE(device
!= NULL
);
3332 ASSERT_FALSE(device
->IsPaired());
3334 TestBluetoothAdapterObserver
observer(adapter_
);
3336 fake_bluetooth_device_client_
->SimulatePairing(
3337 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath
), true,
3338 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback
,
3339 base::Unretained(this)));
3341 EXPECT_EQ(1, pairing_delegate
.call_count_
);
3342 EXPECT_EQ(1, pairing_delegate
.request_passkey_count_
);
3345 device
->SetPasskey(1234);
3346 message_loop_
.Run();
3348 EXPECT_EQ(1, callback_count_
);
3349 EXPECT_EQ(0, error_callback_count_
);
3351 // One change for paired, and one for trusted.
3352 EXPECT_EQ(2, observer
.device_changed_count());
3353 EXPECT_EQ(device
, observer
.last_device());
3355 EXPECT_TRUE(device
->IsPaired());
3357 // Make sure the trusted property has been set to true.
3358 FakeBluetoothDeviceClient::Properties
* properties
=
3359 fake_bluetooth_device_client_
->GetProperties(
3360 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath
));
3361 ASSERT_TRUE(properties
->trusted
.value());
3363 // No pairing context should remain on the device.
3364 BluetoothDeviceChromeOS
* device_chromeos
=
3365 static_cast<BluetoothDeviceChromeOS
*>(device
);
3366 EXPECT_TRUE(device_chromeos
->GetPairing() == NULL
);
3369 TEST_F(BluetoothChromeOSTest
, IncomingPairJustWorks
) {
3370 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
3374 TestPairingDelegate pairing_delegate
;
3375 adapter_
->AddPairingDelegate(
3377 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH
);
3379 // Uses just-works pairing so, sinec this an incoming pairing, require
3380 // authorization from the user.
3381 fake_bluetooth_device_client_
->CreateDevice(
3382 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
),
3383 dbus::ObjectPath(FakeBluetoothDeviceClient::kJustWorksPath
));
3384 BluetoothDevice
* device
= adapter_
->GetDevice(
3385 FakeBluetoothDeviceClient::kJustWorksAddress
);
3386 ASSERT_TRUE(device
!= NULL
);
3387 ASSERT_FALSE(device
->IsPaired());
3389 TestBluetoothAdapterObserver
observer(adapter_
);
3391 fake_bluetooth_device_client_
->SimulatePairing(
3392 dbus::ObjectPath(FakeBluetoothDeviceClient::kJustWorksPath
), true,
3393 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback
,
3394 base::Unretained(this)));
3396 EXPECT_EQ(1, pairing_delegate
.call_count_
);
3397 EXPECT_EQ(1, pairing_delegate
.authorize_pairing_count_
);
3399 // Confirm the pairing.
3400 device
->ConfirmPairing();
3401 message_loop_
.Run();
3403 EXPECT_EQ(1, callback_count_
);
3404 EXPECT_EQ(0, error_callback_count_
);
3406 // One change for paired, and one for trusted.
3407 EXPECT_EQ(2, observer
.device_changed_count());
3408 EXPECT_EQ(device
, observer
.last_device());
3410 EXPECT_TRUE(device
->IsPaired());
3412 // Make sure the trusted property has been set to true.
3413 FakeBluetoothDeviceClient::Properties
* properties
=
3414 fake_bluetooth_device_client_
->GetProperties(
3415 dbus::ObjectPath(FakeBluetoothDeviceClient::kJustWorksPath
));
3416 ASSERT_TRUE(properties
->trusted
.value());
3418 // No pairing context should remain on the device.
3419 BluetoothDeviceChromeOS
* device_chromeos
=
3420 static_cast<BluetoothDeviceChromeOS
*>(device
);
3421 EXPECT_TRUE(device_chromeos
->GetPairing() == NULL
);
3424 TEST_F(BluetoothChromeOSTest
, IncomingPairRequestPinCodeWithoutDelegate
) {
3425 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
3429 // Requires that we provide a PIN Code, without a pairing delegate,
3430 // that will be rejected.
3431 fake_bluetooth_device_client_
->CreateDevice(
3432 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
),
3433 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath
));
3434 BluetoothDevice
* device
= adapter_
->GetDevice(
3435 FakeBluetoothDeviceClient::kRequestPinCodeAddress
);
3436 ASSERT_TRUE(device
!= NULL
);
3437 ASSERT_FALSE(device
->IsPaired());
3439 TestBluetoothAdapterObserver
observer(adapter_
);
3441 fake_bluetooth_device_client_
->SimulatePairing(
3442 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath
), true,
3443 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback
,
3444 base::Unretained(this)));
3446 message_loop_
.Run();
3448 EXPECT_EQ(0, callback_count_
);
3449 EXPECT_EQ(1, error_callback_count_
);
3450 EXPECT_EQ(bluetooth_device::kErrorAuthenticationRejected
, last_client_error_
);
3452 // No changes should be observer.
3453 EXPECT_EQ(0, observer
.device_changed_count());
3455 EXPECT_FALSE(device
->IsPaired());
3457 // No pairing context should remain on the device.
3458 BluetoothDeviceChromeOS
* device_chromeos
=
3459 static_cast<BluetoothDeviceChromeOS
*>(device
);
3460 EXPECT_TRUE(device_chromeos
->GetPairing() == NULL
);
3463 TEST_F(BluetoothChromeOSTest
, IncomingPairConfirmPasskeyWithoutDelegate
) {
3464 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
3468 // Requests that we confirm a displayed passkey, without a pairing delegate,
3469 // that will be rejected.
3470 fake_bluetooth_device_client_
->CreateDevice(
3471 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
),
3472 dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath
));
3473 BluetoothDevice
* device
= adapter_
->GetDevice(
3474 FakeBluetoothDeviceClient::kConfirmPasskeyAddress
);
3475 ASSERT_TRUE(device
!= NULL
);
3476 ASSERT_FALSE(device
->IsPaired());
3478 TestBluetoothAdapterObserver
observer(adapter_
);
3480 fake_bluetooth_device_client_
->SimulatePairing(
3481 dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath
), true,
3482 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback
,
3483 base::Unretained(this)));
3485 message_loop_
.Run();
3487 EXPECT_EQ(0, callback_count_
);
3488 EXPECT_EQ(1, error_callback_count_
);
3489 EXPECT_EQ(bluetooth_device::kErrorAuthenticationRejected
, last_client_error_
);
3491 // No changes should be observer.
3492 EXPECT_EQ(0, observer
.device_changed_count());
3494 EXPECT_FALSE(device
->IsPaired());
3496 // No pairing context should remain on the device.
3497 BluetoothDeviceChromeOS
* device_chromeos
=
3498 static_cast<BluetoothDeviceChromeOS
*>(device
);
3499 EXPECT_TRUE(device_chromeos
->GetPairing() == NULL
);
3502 TEST_F(BluetoothChromeOSTest
, IncomingPairRequestPasskeyWithoutDelegate
) {
3503 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
3507 // Requests that we provide a displayed passkey, without a pairing delegate,
3508 // that will be rejected.
3509 fake_bluetooth_device_client_
->CreateDevice(
3510 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
),
3511 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath
));
3512 BluetoothDevice
* device
= adapter_
->GetDevice(
3513 FakeBluetoothDeviceClient::kRequestPasskeyAddress
);
3514 ASSERT_TRUE(device
!= NULL
);
3515 ASSERT_FALSE(device
->IsPaired());
3517 TestBluetoothAdapterObserver
observer(adapter_
);
3519 fake_bluetooth_device_client_
->SimulatePairing(
3520 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath
), true,
3521 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback
,
3522 base::Unretained(this)));
3524 message_loop_
.Run();
3526 EXPECT_EQ(0, callback_count_
);
3527 EXPECT_EQ(1, error_callback_count_
);
3528 EXPECT_EQ(bluetooth_device::kErrorAuthenticationRejected
, last_client_error_
);
3530 // No changes should be observer.
3531 EXPECT_EQ(0, observer
.device_changed_count());
3533 EXPECT_FALSE(device
->IsPaired());
3535 // No pairing context should remain on the device.
3536 BluetoothDeviceChromeOS
* device_chromeos
=
3537 static_cast<BluetoothDeviceChromeOS
*>(device
);
3538 EXPECT_TRUE(device_chromeos
->GetPairing() == NULL
);
3541 TEST_F(BluetoothChromeOSTest
, IncomingPairJustWorksWithoutDelegate
) {
3542 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
3546 // Uses just-works pairing and thus requires authorization for incoming
3547 // pairings, without a pairing delegate, that will be rejected.
3548 fake_bluetooth_device_client_
->CreateDevice(
3549 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
),
3550 dbus::ObjectPath(FakeBluetoothDeviceClient::kJustWorksPath
));
3551 BluetoothDevice
* device
= adapter_
->GetDevice(
3552 FakeBluetoothDeviceClient::kJustWorksAddress
);
3553 ASSERT_TRUE(device
!= NULL
);
3554 ASSERT_FALSE(device
->IsPaired());
3556 TestBluetoothAdapterObserver
observer(adapter_
);
3558 fake_bluetooth_device_client_
->SimulatePairing(
3559 dbus::ObjectPath(FakeBluetoothDeviceClient::kJustWorksPath
), true,
3560 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback
,
3561 base::Unretained(this)));
3563 message_loop_
.Run();
3565 EXPECT_EQ(0, callback_count_
);
3566 EXPECT_EQ(1, error_callback_count_
);
3567 EXPECT_EQ(bluetooth_device::kErrorAuthenticationRejected
, last_client_error_
);
3569 // No changes should be observer.
3570 EXPECT_EQ(0, observer
.device_changed_count());
3572 EXPECT_FALSE(device
->IsPaired());
3574 // No pairing context should remain on the device.
3575 BluetoothDeviceChromeOS
* device_chromeos
=
3576 static_cast<BluetoothDeviceChromeOS
*>(device
);
3577 EXPECT_TRUE(device_chromeos
->GetPairing() == NULL
);
3580 TEST_F(BluetoothChromeOSTest
, RemovePairingDelegateDuringPairing
) {
3581 fake_bluetooth_device_client_
->SetSimulationIntervalMs(10);
3585 TestPairingDelegate pairing_delegate
;
3586 adapter_
->AddPairingDelegate(
3588 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH
);
3590 // Requests that we provide a Passkey.
3591 fake_bluetooth_device_client_
->CreateDevice(
3592 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath
),
3593 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath
));
3594 BluetoothDevice
* device
= adapter_
->GetDevice(
3595 FakeBluetoothDeviceClient::kRequestPasskeyAddress
);
3596 ASSERT_TRUE(device
!= NULL
);
3597 ASSERT_FALSE(device
->IsPaired());
3599 TestBluetoothAdapterObserver
observer(adapter_
);
3601 fake_bluetooth_device_client_
->SimulatePairing(
3602 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath
), true,
3603 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback
,
3604 base::Unretained(this)));
3606 EXPECT_EQ(1, pairing_delegate
.call_count_
);
3607 EXPECT_EQ(1, pairing_delegate
.request_passkey_count_
);
3609 // A pairing context should now be set on the device.
3610 BluetoothDeviceChromeOS
* device_chromeos
=
3611 static_cast<BluetoothDeviceChromeOS
*>(device
);
3612 ASSERT_TRUE(device_chromeos
->GetPairing() != NULL
);
3614 // Removing the pairing delegate should remove that pairing context.
3615 adapter_
->RemovePairingDelegate(&pairing_delegate
);
3617 EXPECT_TRUE(device_chromeos
->GetPairing() == NULL
);
3619 // Set the Passkey, this should now have no effect since the pairing has
3620 // been, in-effect, cancelled
3621 device
->SetPasskey(1234);
3623 EXPECT_EQ(0, callback_count_
);
3624 EXPECT_EQ(0, error_callback_count_
);
3625 EXPECT_EQ(0, observer
.device_changed_count());
3627 EXPECT_FALSE(device
->IsPaired());
3630 TEST_F(BluetoothChromeOSTest
, DeviceId
) {
3633 // Use the built-in paired device for this test, grab its Properties
3634 // structure so we can adjust the underlying modalias property.
3635 BluetoothDevice
* device
= adapter_
->GetDevice(
3636 FakeBluetoothDeviceClient::kPairedDeviceAddress
);
3637 FakeBluetoothDeviceClient::Properties
* properties
=
3638 fake_bluetooth_device_client_
->GetProperties(
3639 dbus::ObjectPath(FakeBluetoothDeviceClient::kPairedDevicePath
));
3641 ASSERT_TRUE(device
!= NULL
);
3642 ASSERT_TRUE(properties
!= NULL
);
3644 // Valid USB IF-assigned identifier.
3645 ASSERT_EQ("usb:v05ACp030Dd0306", properties
->modalias
.value());
3647 EXPECT_EQ(BluetoothDevice::VENDOR_ID_USB
, device
->GetVendorIDSource());
3648 EXPECT_EQ(0x05ac, device
->GetVendorID());
3649 EXPECT_EQ(0x030d, device
->GetProductID());
3650 EXPECT_EQ(0x0306, device
->GetDeviceID());
3652 // Valid Bluetooth SIG-assigned identifier.
3653 properties
->modalias
.ReplaceValue("bluetooth:v00E0p2400d0400");
3655 EXPECT_EQ(BluetoothDevice::VENDOR_ID_BLUETOOTH
, device
->GetVendorIDSource());
3656 EXPECT_EQ(0x00e0, device
->GetVendorID());
3657 EXPECT_EQ(0x2400, device
->GetProductID());
3658 EXPECT_EQ(0x0400, device
->GetDeviceID());
3660 // Invalid USB IF-assigned identifier.
3661 properties
->modalias
.ReplaceValue("usb:x00E0p2400d0400");
3663 EXPECT_EQ(BluetoothDevice::VENDOR_ID_UNKNOWN
, device
->GetVendorIDSource());
3664 EXPECT_EQ(0, device
->GetVendorID());
3665 EXPECT_EQ(0, device
->GetProductID());
3666 EXPECT_EQ(0, device
->GetDeviceID());
3668 // Invalid Bluetooth SIG-assigned identifier.
3669 properties
->modalias
.ReplaceValue("bluetooth:x00E0p2400d0400");
3671 EXPECT_EQ(BluetoothDevice::VENDOR_ID_UNKNOWN
, device
->GetVendorIDSource());
3672 EXPECT_EQ(0, device
->GetVendorID());
3673 EXPECT_EQ(0, device
->GetProductID());
3674 EXPECT_EQ(0, device
->GetDeviceID());
3676 // Unknown vendor specification identifier.
3677 properties
->modalias
.ReplaceValue("chrome:v00E0p2400d0400");
3679 EXPECT_EQ(BluetoothDevice::VENDOR_ID_UNKNOWN
, device
->GetVendorIDSource());
3680 EXPECT_EQ(0, device
->GetVendorID());
3681 EXPECT_EQ(0, device
->GetProductID());
3682 EXPECT_EQ(0, device
->GetDeviceID());
3685 TEST_F(BluetoothChromeOSTest
, GetConnectionInfoForDisconnectedDevice
) {
3687 BluetoothDevice
* device
=
3688 adapter_
->GetDevice(FakeBluetoothDeviceClient::kPairedDeviceAddress
);
3690 // Calling GetConnectionInfo for an unconnected device should return a result
3691 // in which all fields are filled with BluetoothDevice::kUnknownPower.
3692 BluetoothDevice::ConnectionInfo
conn_info(0, 0, 0);
3693 device
->GetConnectionInfo(base::Bind(&SaveConnectionInfo
, &conn_info
));
3694 int unknown_power
= BluetoothDevice::kUnknownPower
;
3695 EXPECT_NE(0, unknown_power
);
3696 EXPECT_EQ(unknown_power
, conn_info
.rssi
);
3697 EXPECT_EQ(unknown_power
, conn_info
.transmit_power
);
3698 EXPECT_EQ(unknown_power
, conn_info
.max_transmit_power
);
3701 TEST_F(BluetoothChromeOSTest
, GetConnectionInfoForConnectedDevice
) {
3703 BluetoothDevice
* device
=
3704 adapter_
->GetDevice(FakeBluetoothDeviceClient::kPairedDeviceAddress
);
3706 device
->Connect(NULL
, GetCallback(),
3707 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback
,
3708 base::Unretained(this)));
3709 EXPECT_TRUE(device
->IsConnected());
3711 // Calling GetConnectionInfo for a connected device should return valid
3713 fake_bluetooth_device_client_
->UpdateConnectionInfo(-10, 3, 4);
3714 BluetoothDevice::ConnectionInfo conn_info
;
3715 device
->GetConnectionInfo(base::Bind(&SaveConnectionInfo
, &conn_info
));
3716 EXPECT_EQ(-10, conn_info
.rssi
);
3717 EXPECT_EQ(3, conn_info
.transmit_power
);
3718 EXPECT_EQ(4, conn_info
.max_transmit_power
);
3721 // Verifies Shutdown shuts down the adapter as expected.
3722 TEST_F(BluetoothChromeOSTest
, Shutdown
) {
3723 // Set up adapter. Set powered & discoverable, start discovery.
3725 adapter_
->SetPowered(true, GetCallback(), GetErrorCallback());
3726 adapter_
->SetDiscoverable(true, GetCallback(), GetErrorCallback());
3727 adapter_
->StartDiscoverySession(
3728 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
3729 base::Unretained(this)),
3730 GetErrorCallback());
3731 base::MessageLoop::current()->Run();
3732 ASSERT_EQ(3, callback_count_
);
3733 ASSERT_EQ(0, error_callback_count_
);
3734 callback_count_
= 0;
3736 TestPairingDelegate pairing_delegate
;
3737 adapter_
->AddPairingDelegate(
3738 &pairing_delegate
, BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH
);
3740 // Validate running adapter state.
3741 EXPECT_NE("", adapter_
->GetAddress());
3742 EXPECT_NE("", adapter_
->GetName());
3743 EXPECT_TRUE(adapter_
->IsInitialized());
3744 EXPECT_TRUE(adapter_
->IsPresent());
3745 EXPECT_TRUE(adapter_
->IsPowered());
3746 EXPECT_TRUE(adapter_
->IsDiscoverable());
3747 EXPECT_TRUE(adapter_
->IsDiscovering());
3748 EXPECT_EQ(2U, adapter_
->GetDevices().size());
3749 EXPECT_NE(nullptr, adapter_
->GetDevice(
3750 FakeBluetoothDeviceClient::kPairedDeviceAddress
));
3751 EXPECT_NE(dbus::ObjectPath(""), static_cast<BluetoothAdapterChromeOS
*>(
3752 adapter_
.get())->object_path());
3755 adapter_
->Shutdown();
3757 // Validate post shutdown state by calling all BluetoothAdapterChromeOS
3758 // members, in declaration order:
3760 adapter_
->Shutdown();
3761 // DeleteOnCorrectThread omitted as we don't want to delete in this test.
3763 TestBluetoothAdapterObserver
observer(adapter_
); // Calls AddObserver
3764 } // ~TestBluetoothAdapterObserver calls RemoveObserver.
3765 EXPECT_EQ("", adapter_
->GetAddress());
3766 EXPECT_EQ("", adapter_
->GetName());
3768 adapter_
->SetName("", GetCallback(), GetErrorCallback());
3769 EXPECT_EQ(0, callback_count_
);
3770 EXPECT_EQ(1, error_callback_count_
--) << "SetName error";
3772 EXPECT_TRUE(adapter_
->IsInitialized());
3773 EXPECT_FALSE(adapter_
->IsPresent());
3774 EXPECT_FALSE(adapter_
->IsPowered());
3776 adapter_
->SetPowered(true, GetCallback(), GetErrorCallback());
3777 EXPECT_EQ(0, callback_count_
);
3778 EXPECT_EQ(1, error_callback_count_
--) << "SetPowered error";
3780 EXPECT_FALSE(adapter_
->IsDiscoverable());
3782 adapter_
->SetDiscoverable(true, GetCallback(), GetErrorCallback());
3783 EXPECT_EQ(0, callback_count_
);
3784 EXPECT_EQ(1, error_callback_count_
--) << "SetDiscoverable error";
3786 EXPECT_FALSE(adapter_
->IsDiscovering());
3787 // CreateRfcommService will DCHECK after Shutdown().
3788 // CreateL2capService will DCHECK after Shutdown().
3790 BluetoothAudioSink::Options audio_sink_options
;
3791 adapter_
->RegisterAudioSink(
3793 base::Bind(&BluetoothChromeOSTest::AudioSinkAcquiredCallback
,
3794 base::Unretained(this)),
3795 base::Bind(&BluetoothChromeOSTest::AudioSinkErrorCallback
,
3796 base::Unretained(this)));
3797 EXPECT_EQ(0, callback_count_
);
3798 EXPECT_EQ(1, error_callback_count_
--) << "RegisterAudioSink error";
3800 BluetoothAdapterChromeOS
* adapter_chrome_os
=
3801 static_cast<BluetoothAdapterChromeOS
*>(adapter_
.get());
3802 EXPECT_EQ(NULL
, adapter_chrome_os
->GetDeviceWithPath(dbus::ObjectPath("")));
3804 // Notify methods presume objects exist that are owned by the adapter and
3805 // destroyed in Shutdown(). Mocks are not attempted here that won't exist,
3806 // as verified below by EXPECT_EQ(0U, adapter_->GetDevices().size());
3807 // NotifyDeviceChanged
3808 // NotifyGattServiceAdded
3809 // NotifyGattServiceRemoved
3810 // NotifyGattServiceChanged
3811 // NotifyGattDiscoveryComplete
3812 // NotifyGattCharacteristicAdded
3813 // NotifyGattCharacteristicRemoved
3814 // NotifyGattDescriptorAdded
3815 // NotifyGattDescriptorRemoved
3816 // NotifyGattCharacteristicValueChanged
3817 // NotifyGattDescriptorValueChanged
3819 EXPECT_EQ(dbus::ObjectPath(""), adapter_chrome_os
->object_path());
3821 adapter_profile_
= NULL
;
3823 FakeBluetoothProfileServiceProviderDelegate profile_delegate
;
3824 adapter_chrome_os
->UseProfile(
3825 BluetoothUUID(), dbus::ObjectPath(""),
3826 BluetoothProfileManagerClient::Options(), &profile_delegate
,
3827 base::Bind(&BluetoothChromeOSTest::ProfileRegisteredCallback
,
3828 base::Unretained(this)),
3829 base::Bind(&BluetoothChromeOSTest::ErrorCompletionCallback
,
3830 base::Unretained(this)));
3832 EXPECT_FALSE(adapter_profile_
) << "UseProfile error";
3833 EXPECT_EQ(0, callback_count_
) << "UseProfile error";
3834 EXPECT_EQ(1, error_callback_count_
--) << "UseProfile error";
3836 // Protected and private methods:
3838 adapter_chrome_os
->RemovePairingDelegateInternal(&pairing_delegate
);
3839 // AdapterAdded() invalid post Shutdown because it calls SetAdapter.
3840 adapter_chrome_os
->AdapterRemoved(dbus::ObjectPath("x"));
3841 adapter_chrome_os
->AdapterPropertyChanged(dbus::ObjectPath("x"), "");
3842 adapter_chrome_os
->DeviceAdded(dbus::ObjectPath(""));
3843 adapter_chrome_os
->DeviceRemoved(dbus::ObjectPath(""));
3844 adapter_chrome_os
->DevicePropertyChanged(dbus::ObjectPath(""), "");
3845 adapter_chrome_os
->InputPropertyChanged(dbus::ObjectPath(""), "");
3846 // BluetoothAgentServiceProvider::Delegate omitted, dbus will be shutdown,
3847 // with the exception of Released.
3848 adapter_chrome_os
->Released();
3850 adapter_chrome_os
->OnRegisterAgent();
3851 adapter_chrome_os
->OnRegisterAgentError("", "");
3852 adapter_chrome_os
->OnRequestDefaultAgent();
3853 adapter_chrome_os
->OnRequestDefaultAgentError("", "");
3855 adapter_chrome_os
->OnRegisterAudioSink(
3856 base::Bind(&BluetoothChromeOSTest::AudioSinkAcquiredCallback
,
3857 base::Unretained(this)),
3858 base::Bind(&BluetoothChromeOSTest::AudioSinkErrorCallback
,
3859 base::Unretained(this)),
3860 scoped_refptr
<device::BluetoothAudioSink
>());
3861 EXPECT_EQ(0, callback_count_
);
3862 EXPECT_EQ(1, error_callback_count_
--) << "RegisterAudioSink error";
3864 // GetPairing will DCHECK after Shutdown().
3865 // SetAdapter will DCHECK after Shutdown().
3866 // SetDefaultAdapterName will DCHECK after Shutdown().
3867 // RemoveAdapter will DCHECK after Shutdown().
3868 adapter_chrome_os
->PoweredChanged(false);
3869 adapter_chrome_os
->DiscoverableChanged(false);
3870 adapter_chrome_os
->DiscoveringChanged(false);
3871 adapter_chrome_os
->PresentChanged(false);
3873 adapter_chrome_os
->OnSetDiscoverable(GetCallback(), GetErrorCallback(), true);
3874 EXPECT_EQ(0, callback_count_
) << "OnSetDiscoverable error";
3875 EXPECT_EQ(1, error_callback_count_
--) << "OnSetDiscoverable error";
3877 adapter_chrome_os
->OnPropertyChangeCompleted(GetCallback(),
3878 GetErrorCallback(), true);
3879 EXPECT_EQ(0, callback_count_
) << "OnPropertyChangeCompleted error";
3880 EXPECT_EQ(1, error_callback_count_
--) << "OnPropertyChangeCompleted error";
3882 adapter_chrome_os
->AddDiscoverySession(nullptr, GetCallback(),
3883 GetErrorCallback());
3884 EXPECT_EQ(0, callback_count_
) << "AddDiscoverySession error";
3885 EXPECT_EQ(1, error_callback_count_
--) << "AddDiscoverySession error";
3887 adapter_chrome_os
->RemoveDiscoverySession(nullptr, GetCallback(),
3888 GetErrorCallback());
3889 EXPECT_EQ(0, callback_count_
) << "RemoveDiscoverySession error";
3890 EXPECT_EQ(1, error_callback_count_
--) << "RemoveDiscoverySession error";
3892 // OnStartDiscovery tested in Shutdown_OnStartDiscovery
3893 // OnStartDiscoveryError tested in Shutdown_OnStartDiscoveryError
3894 // OnStopDiscovery tested in Shutdown_OnStopDiscovery
3895 // OnStopDiscoveryError tested in Shutdown_OnStopDiscoveryError
3897 adapter_profile_
= NULL
;
3899 // OnRegisterProfile SetProfileDelegate, OnRegisterProfileError, require
3900 // UseProfile to be set first, do so again here just before calling them.
3901 adapter_chrome_os
->UseProfile(
3902 BluetoothUUID(), dbus::ObjectPath(""),
3903 BluetoothProfileManagerClient::Options(), &profile_delegate
,
3904 base::Bind(&BluetoothChromeOSTest::ProfileRegisteredCallback
,
3905 base::Unretained(this)),
3906 base::Bind(&BluetoothChromeOSTest::ErrorCompletionCallback
,
3907 base::Unretained(this)));
3909 EXPECT_FALSE(adapter_profile_
) << "UseProfile error";
3910 EXPECT_EQ(0, callback_count_
) << "UseProfile error";
3911 EXPECT_EQ(1, error_callback_count_
--) << "UseProfile error";
3913 adapter_chrome_os
->SetProfileDelegate(
3914 BluetoothUUID(), dbus::ObjectPath(""), &profile_delegate
,
3915 base::Bind(&BluetoothChromeOSTest::ProfileRegisteredCallback
,
3916 base::Unretained(this)),
3917 base::Bind(&BluetoothChromeOSTest::ErrorCompletionCallback
,
3918 base::Unretained(this)));
3919 EXPECT_EQ(0, callback_count_
) << "SetProfileDelegate error";
3920 EXPECT_EQ(1, error_callback_count_
--) << "SetProfileDelegate error";
3922 adapter_chrome_os
->OnRegisterProfileError(BluetoothUUID(), "", "");
3923 EXPECT_EQ(0, callback_count_
) << "OnRegisterProfileError error";
3924 EXPECT_EQ(0, error_callback_count_
) << "OnRegisterProfileError error";
3926 adapter_chrome_os
->ProcessQueuedDiscoveryRequests();
3928 // From BluetoothAdapater:
3930 adapter_
->StartDiscoverySession(
3931 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback
,
3932 base::Unretained(this)),
3933 GetErrorCallback());
3934 EXPECT_EQ(0, callback_count_
) << "StartDiscoverySession error";
3935 EXPECT_EQ(1, error_callback_count_
--) << "StartDiscoverySession error";
3937 EXPECT_EQ(0U, adapter_
->GetDevices().size());
3938 EXPECT_EQ(nullptr, adapter_
->GetDevice(
3939 FakeBluetoothDeviceClient::kPairedDeviceAddress
));
3940 TestPairingDelegate pairing_delegate2
;
3941 adapter_
->AddPairingDelegate(
3942 &pairing_delegate2
, BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH
);
3943 adapter_
->RemovePairingDelegate(&pairing_delegate2
);
3946 // Verifies post-Shutdown of discovery sessions and OnStartDiscovery.
3947 TEST_F(BluetoothChromeOSTest
, Shutdown_OnStartDiscovery
) {
3948 const int kNumberOfDiscoverySessions
= 10;
3950 BluetoothAdapterChromeOS
* adapter_chrome_os
=
3951 static_cast<BluetoothAdapterChromeOS
*>(adapter_
.get());
3953 for (int i
= 0; i
< kNumberOfDiscoverySessions
; i
++) {
3954 adapter_chrome_os
->AddDiscoverySession(nullptr, GetCallback(),
3955 GetErrorCallback());
3957 adapter_
->Shutdown();
3958 adapter_chrome_os
->OnStartDiscovery(GetCallback(), GetErrorCallback());
3960 EXPECT_EQ(0, callback_count_
);
3961 EXPECT_EQ(kNumberOfDiscoverySessions
, error_callback_count_
);
3964 // Verifies post-Shutdown of discovery sessions and OnStartDiscoveryError.
3965 TEST_F(BluetoothChromeOSTest
, Shutdown_OnStartDiscoveryError
) {
3966 const int kNumberOfDiscoverySessions
= 10;
3968 BluetoothAdapterChromeOS
* adapter_chrome_os
=
3969 static_cast<BluetoothAdapterChromeOS
*>(adapter_
.get());
3971 for (int i
= 0; i
< kNumberOfDiscoverySessions
; i
++) {
3972 adapter_chrome_os
->AddDiscoverySession(nullptr, GetCallback(),
3973 GetErrorCallback());
3975 adapter_
->Shutdown();
3976 adapter_chrome_os
->OnStartDiscoveryError(GetCallback(), GetErrorCallback(),
3979 EXPECT_EQ(0, callback_count_
);
3980 EXPECT_EQ(kNumberOfDiscoverySessions
, error_callback_count_
);
3983 // Verifies post-Shutdown of discovery sessions and OnStartDiscovery.
3984 TEST_F(BluetoothChromeOSTest
, Shutdown_OnStopDiscovery
) {
3985 const int kNumberOfDiscoverySessions
= 10;
3987 BluetoothAdapterChromeOS
* adapter_chrome_os
=
3988 static_cast<BluetoothAdapterChromeOS
*>(adapter_
.get());
3990 // In order to queue up discovery sessions before an OnStopDiscovery call
3991 // RemoveDiscoverySession must be called, so Add, Start, and Remove:
3992 adapter_chrome_os
->AddDiscoverySession(nullptr, GetCallback(),
3993 GetErrorCallback());
3994 adapter_chrome_os
->OnStartDiscovery(GetCallback(), GetErrorCallback());
3995 adapter_chrome_os
->RemoveDiscoverySession(nullptr, GetCallback(),
3996 GetErrorCallback());
3997 callback_count_
= 0;
3998 error_callback_count_
= 0;
3999 // Can now queue discovery sessions while waiting for OnStopDiscovery.
4000 for (int i
= 0; i
< kNumberOfDiscoverySessions
; i
++) {
4001 adapter_chrome_os
->AddDiscoverySession(nullptr, GetCallback(),
4002 GetErrorCallback());
4004 adapter_
->Shutdown();
4005 adapter_chrome_os
->OnStopDiscovery(GetCallback());
4007 // 1 successful stopped discovery from RemoveDiscoverySession, and
4008 // kNumberOfDiscoverySessions errors from AddDiscoverySession/OnStopDiscovery.
4009 EXPECT_EQ(1, callback_count_
);
4010 EXPECT_EQ(kNumberOfDiscoverySessions
, error_callback_count_
);
4013 // Verifies post-Shutdown of discovery sessions and OnStopDiscoveryError.
4014 TEST_F(BluetoothChromeOSTest
, Shutdown_OnStopDiscoveryError
) {
4015 const int kNumberOfDiscoverySessions
= 10;
4017 BluetoothAdapterChromeOS
* adapter_chrome_os
=
4018 static_cast<BluetoothAdapterChromeOS
*>(adapter_
.get());
4020 // In order to queue up discovery sessions before an OnStopDiscoveryError call
4021 // RemoveDiscoverySession must be called, so Add, Start, and Remove:
4022 adapter_chrome_os
->AddDiscoverySession(nullptr, GetCallback(),
4023 GetErrorCallback());
4024 adapter_chrome_os
->OnStartDiscovery(GetCallback(), GetErrorCallback());
4025 adapter_chrome_os
->RemoveDiscoverySession(nullptr, GetCallback(),
4026 GetErrorCallback());
4027 callback_count_
= 0;
4028 error_callback_count_
= 0;
4029 // Can now queue discovery sessions while waiting for OnStopDiscoveryError.
4030 for (int i
= 0; i
< kNumberOfDiscoverySessions
; i
++) {
4031 adapter_chrome_os
->AddDiscoverySession(nullptr, GetCallback(),
4032 GetErrorCallback());
4034 adapter_
->Shutdown();
4035 adapter_chrome_os
->OnStopDiscoveryError(GetErrorCallback(), "", "");
4037 // 1 error reported to RemoveDiscoverySession because of OnStopDiscoveryError,
4038 // and kNumberOfDiscoverySessions errors queued with AddDiscoverySession.
4039 EXPECT_EQ(0, callback_count_
);
4040 EXPECT_EQ(1 + kNumberOfDiscoverySessions
, error_callback_count_
);
4043 } // namespace chromeos