Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / device / bluetooth / bluetooth_chromeos_unittest.cc
blob1a71c5bb237a68e170bcc6e358e0e2970a3afaec
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/memory/scoped_vector.h"
6 #include "base/message_loop/message_loop.h"
7 #include "base/run_loop.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chromeos/dbus/dbus_thread_manager.h"
10 #include "chromeos/dbus/fake_bluetooth_adapter_client.h"
11 #include "chromeos/dbus/fake_bluetooth_agent_manager_client.h"
12 #include "chromeos/dbus/fake_bluetooth_device_client.h"
13 #include "chromeos/dbus/fake_bluetooth_gatt_service_client.h"
14 #include "chromeos/dbus/fake_bluetooth_input_client.h"
15 #include "dbus/object_path.h"
16 #include "device/bluetooth/bluetooth_adapter.h"
17 #include "device/bluetooth/bluetooth_adapter_chromeos.h"
18 #include "device/bluetooth/bluetooth_adapter_factory.h"
19 #include "device/bluetooth/bluetooth_device.h"
20 #include "device/bluetooth/bluetooth_device_chromeos.h"
21 #include "device/bluetooth/bluetooth_discovery_session.h"
22 #include "device/bluetooth/bluetooth_pairing_chromeos.h"
23 #include "testing/gtest/include/gtest/gtest.h"
24 #include "third_party/cros_system_api/dbus/service_constants.h"
26 using device::BluetoothAdapter;
27 using device::BluetoothAdapterFactory;
28 using device::BluetoothAudioSink;
29 using device::BluetoothDevice;
30 using device::BluetoothDiscoveryFilter;
31 using device::BluetoothDiscoverySession;
32 using device::BluetoothUUID;
34 namespace chromeos {
36 namespace {
38 class TestObserver : public BluetoothAdapter::Observer {
39 public:
40 TestObserver(scoped_refptr<BluetoothAdapter> adapter)
41 : present_changed_count_(0),
42 powered_changed_count_(0),
43 discoverable_changed_count_(0),
44 discovering_changed_count_(0),
45 last_present_(false),
46 last_powered_(false),
47 last_discovering_(false),
48 device_added_count_(0),
49 device_changed_count_(0),
50 device_removed_count_(0),
51 last_device_(NULL),
52 adapter_(adapter) {
53 adapter_->AddObserver(this);
56 ~TestObserver() override { adapter_->RemoveObserver(this); }
58 void AdapterPresentChanged(BluetoothAdapter* adapter, bool present) override {
59 EXPECT_EQ(adapter_.get(), adapter);
61 ++present_changed_count_;
62 last_present_ = present;
65 void AdapterPoweredChanged(BluetoothAdapter* adapter, bool powered) override {
66 EXPECT_EQ(adapter_.get(), adapter);
68 ++powered_changed_count_;
69 last_powered_ = powered;
72 void AdapterDiscoverableChanged(BluetoothAdapter* adapter,
73 bool discoverable) override {
74 EXPECT_EQ(adapter_.get(), adapter);
76 ++discoverable_changed_count_;
79 void AdapterDiscoveringChanged(BluetoothAdapter* adapter,
80 bool discovering) override {
81 EXPECT_EQ(adapter_.get(), adapter);
83 ++discovering_changed_count_;
84 last_discovering_ = discovering;
87 void DeviceAdded(BluetoothAdapter* adapter,
88 BluetoothDevice* device) override {
89 EXPECT_EQ(adapter_.get(), adapter);
91 ++device_added_count_;
92 last_device_ = device;
93 last_device_address_ = device->GetAddress();
95 QuitMessageLoop();
98 void DeviceChanged(BluetoothAdapter* adapter,
99 BluetoothDevice* device) override {
100 EXPECT_EQ(adapter_.get(), adapter);
102 ++device_changed_count_;
103 last_device_ = device;
104 last_device_address_ = device->GetAddress();
106 QuitMessageLoop();
109 void DeviceRemoved(BluetoothAdapter* adapter,
110 BluetoothDevice* device) override {
111 EXPECT_EQ(adapter_.get(), adapter);
113 ++device_removed_count_;
114 // Can't save device, it may be freed
115 last_device_address_ = device->GetAddress();
117 QuitMessageLoop();
120 int present_changed_count_;
121 int powered_changed_count_;
122 int discoverable_changed_count_;
123 int discovering_changed_count_;
124 bool last_present_;
125 bool last_powered_;
126 bool last_discovering_;
127 int device_added_count_;
128 int device_changed_count_;
129 int device_removed_count_;
130 BluetoothDevice* last_device_;
131 std::string last_device_address_;
133 private:
134 // Some tests use a message loop since background processing is simulated;
135 // break out of those loops.
136 void QuitMessageLoop() {
137 if (base::MessageLoop::current() &&
138 base::MessageLoop::current()->is_running())
139 base::MessageLoop::current()->Quit();
142 scoped_refptr<BluetoothAdapter> adapter_;
145 // Callback for BluetoothDevice::GetConnectionInfo() that simply saves the
146 // connection info to the bound argument.
147 void SaveConnectionInfo(BluetoothDevice::ConnectionInfo* out,
148 const BluetoothDevice::ConnectionInfo& conn_info) {
149 *out = conn_info;
152 class FakeBluetoothProfileServiceProviderDelegate
153 : public chromeos::BluetoothProfileServiceProvider::Delegate {
154 public:
155 FakeBluetoothProfileServiceProviderDelegate() {}
157 // BluetoothProfileServiceProvider::Delegate:
158 void Released() override {}
160 void NewConnection(const dbus::ObjectPath&,
161 scoped_ptr<dbus::FileDescriptor>,
162 const BluetoothProfileServiceProvider::Delegate::Options&,
163 const ConfirmationCallback&) override {}
165 void RequestDisconnection(const dbus::ObjectPath&,
166 const ConfirmationCallback&) override {}
168 void Cancel() override {}
171 } // namespace
173 class TestPairingDelegate : public BluetoothDevice::PairingDelegate {
174 public:
175 TestPairingDelegate()
176 : call_count_(0),
177 request_pincode_count_(0),
178 request_passkey_count_(0),
179 display_pincode_count_(0),
180 display_passkey_count_(0),
181 keys_entered_count_(0),
182 confirm_passkey_count_(0),
183 authorize_pairing_count_(0),
184 last_passkey_(9999999U),
185 last_entered_(999U) {}
186 ~TestPairingDelegate() override {}
188 void RequestPinCode(BluetoothDevice* device) override {
189 ++call_count_;
190 ++request_pincode_count_;
191 QuitMessageLoop();
194 void RequestPasskey(BluetoothDevice* device) override {
195 ++call_count_;
196 ++request_passkey_count_;
197 QuitMessageLoop();
200 void DisplayPinCode(BluetoothDevice* device,
201 const std::string& pincode) override {
202 ++call_count_;
203 ++display_pincode_count_;
204 last_pincode_ = pincode;
205 QuitMessageLoop();
208 void DisplayPasskey(BluetoothDevice* device, uint32 passkey) override {
209 ++call_count_;
210 ++display_passkey_count_;
211 last_passkey_ = passkey;
212 QuitMessageLoop();
215 void KeysEntered(BluetoothDevice* device, uint32 entered) override {
216 ++call_count_;
217 ++keys_entered_count_;
218 last_entered_ = entered;
219 QuitMessageLoop();
222 void ConfirmPasskey(BluetoothDevice* device, uint32 passkey) override {
223 ++call_count_;
224 ++confirm_passkey_count_;
225 last_passkey_ = passkey;
226 QuitMessageLoop();
229 void AuthorizePairing(BluetoothDevice* device) override {
230 ++call_count_;
231 ++authorize_pairing_count_;
232 QuitMessageLoop();
235 int call_count_;
236 int request_pincode_count_;
237 int request_passkey_count_;
238 int display_pincode_count_;
239 int display_passkey_count_;
240 int keys_entered_count_;
241 int confirm_passkey_count_;
242 int authorize_pairing_count_;
243 uint32 last_passkey_;
244 uint32 last_entered_;
245 std::string last_pincode_;
247 private:
248 // Some tests use a message loop since background processing is simulated;
249 // break out of those loops.
250 void QuitMessageLoop() {
251 if (base::MessageLoop::current() &&
252 base::MessageLoop::current()->is_running())
253 base::MessageLoop::current()->Quit();
257 class BluetoothChromeOSTest : public testing::Test {
258 public:
259 void SetUp() override {
260 scoped_ptr<DBusThreadManagerSetter> dbus_setter =
261 chromeos::DBusThreadManager::GetSetterForTesting();
262 // We need to initialize DBusThreadManager early to prevent
263 // Bluetooth*::Create() methods from picking the real instead of fake
264 // implementations.
265 fake_bluetooth_adapter_client_ = new FakeBluetoothAdapterClient;
266 dbus_setter->SetBluetoothAdapterClient(
267 scoped_ptr<BluetoothAdapterClient>(fake_bluetooth_adapter_client_));
268 fake_bluetooth_device_client_ = new FakeBluetoothDeviceClient;
269 dbus_setter->SetBluetoothDeviceClient(
270 scoped_ptr<BluetoothDeviceClient>(fake_bluetooth_device_client_));
271 dbus_setter->SetBluetoothInputClient(
272 scoped_ptr<BluetoothInputClient>(new FakeBluetoothInputClient));
273 dbus_setter->SetBluetoothAgentManagerClient(
274 scoped_ptr<BluetoothAgentManagerClient>(
275 new FakeBluetoothAgentManagerClient));
276 dbus_setter->SetBluetoothGattServiceClient(
277 scoped_ptr<BluetoothGattServiceClient>(
278 new FakeBluetoothGattServiceClient));
280 fake_bluetooth_adapter_client_->SetSimulationIntervalMs(10);
282 callback_count_ = 0;
283 error_callback_count_ = 0;
284 last_connect_error_ = BluetoothDevice::ERROR_UNKNOWN;
285 last_client_error_ = "";
288 void TearDown() override {
289 for (ScopedVector<BluetoothDiscoverySession>::iterator iter =
290 discovery_sessions_.begin();
291 iter != discovery_sessions_.end();
292 ++iter) {
293 BluetoothDiscoverySession* session = *iter;
294 if (!session->IsActive())
295 continue;
296 callback_count_ = 0;
297 session->Stop(GetCallback(), GetErrorCallback());
298 message_loop_.Run();
299 ASSERT_EQ(1, callback_count_);
301 discovery_sessions_.clear();
302 adapter_ = NULL;
303 DBusThreadManager::Shutdown();
306 // Generic callbacks
307 void Callback() {
308 ++callback_count_;
309 QuitMessageLoop();
312 base::Closure GetCallback() {
313 return base::Bind(&BluetoothChromeOSTest::Callback, base::Unretained(this));
316 void DiscoverySessionCallback(
317 scoped_ptr<BluetoothDiscoverySession> discovery_session) {
318 ++callback_count_;
319 discovery_sessions_.push_back(discovery_session.release());
320 QuitMessageLoop();
323 void AudioSinkAcquiredCallback(scoped_refptr<BluetoothAudioSink>) {
324 ++callback_count_;
325 QuitMessageLoop();
328 void ProfileRegisteredCallback(BluetoothAdapterProfileChromeOS* profile) {
329 adapter_profile_ = profile;
330 ++callback_count_;
331 QuitMessageLoop();
334 void ErrorCallback() {
335 ++error_callback_count_;
336 QuitMessageLoop();
339 base::Closure GetErrorCallback() {
340 return base::Bind(&BluetoothChromeOSTest::ErrorCallback,
341 base::Unretained(this));
344 void DBusErrorCallback(const std::string& error_name,
345 const std::string& error_message) {
346 ++error_callback_count_;
347 last_client_error_ = error_name;
348 QuitMessageLoop();
351 void ConnectErrorCallback(BluetoothDevice::ConnectErrorCode error) {
352 ++error_callback_count_;
353 last_connect_error_ = error;
356 void AudioSinkErrorCallback(BluetoothAudioSink::ErrorCode) {
357 ++error_callback_count_;
358 QuitMessageLoop();
361 void ErrorCompletionCallback(const std::string& error_message) {
362 ++error_callback_count_;
363 QuitMessageLoop();
366 // Call to fill the adapter_ member with a BluetoothAdapter instance.
367 void GetAdapter() {
368 adapter_ = new BluetoothAdapterChromeOS();
369 ASSERT_TRUE(adapter_.get() != NULL);
370 ASSERT_TRUE(adapter_->IsInitialized());
373 // Run a discovery phase until the named device is detected, or if the named
374 // device is not created, the discovery process ends without finding it.
376 // The correct behavior of discovery is tested by the "Discovery" test case
377 // without using this function.
378 void DiscoverDevice(const std::string& address) {
379 ASSERT_TRUE(adapter_.get() != NULL);
380 ASSERT_TRUE(base::MessageLoop::current() != NULL);
381 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
383 TestObserver observer(adapter_);
385 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
386 adapter_->StartDiscoverySession(
387 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
388 base::Unretained(this)),
389 GetErrorCallback());
390 base::MessageLoop::current()->Run();
391 ASSERT_EQ(2, callback_count_);
392 ASSERT_EQ(0, error_callback_count_);
393 ASSERT_EQ((size_t)1, discovery_sessions_.size());
394 ASSERT_TRUE(discovery_sessions_[0]->IsActive());
395 callback_count_ = 0;
397 ASSERT_TRUE(adapter_->IsPowered());
398 ASSERT_TRUE(adapter_->IsDiscovering());
400 while (!observer.device_removed_count_ &&
401 observer.last_device_address_ != address)
402 base::MessageLoop::current()->Run();
404 discovery_sessions_[0]->Stop(GetCallback(), GetErrorCallback());
405 base::MessageLoop::current()->Run();
406 ASSERT_EQ(1, callback_count_);
407 ASSERT_EQ(0, error_callback_count_);
408 callback_count_ = 0;
410 ASSERT_FALSE(adapter_->IsDiscovering());
413 // Run a discovery phase so we have devices that can be paired with.
414 void DiscoverDevices() {
415 // Pass an invalid address for the device so that the discovery process
416 // completes with all devices.
417 DiscoverDevice("does not exist");
420 protected:
421 base::MessageLoop message_loop_;
422 FakeBluetoothAdapterClient* fake_bluetooth_adapter_client_;
423 FakeBluetoothDeviceClient* fake_bluetooth_device_client_;
424 scoped_refptr<BluetoothAdapter> adapter_;
426 int callback_count_;
427 int error_callback_count_;
428 enum BluetoothDevice::ConnectErrorCode last_connect_error_;
429 std::string last_client_error_;
430 ScopedVector<BluetoothDiscoverySession> discovery_sessions_;
431 BluetoothAdapterProfileChromeOS* adapter_profile_;
433 private:
434 // Some tests use a message loop since background processing is simulated;
435 // break out of those loops.
436 void QuitMessageLoop() {
437 if (base::MessageLoop::current() &&
438 base::MessageLoop::current()->is_running())
439 base::MessageLoop::current()->Quit();
443 TEST_F(BluetoothChromeOSTest, AlreadyPresent) {
444 GetAdapter();
446 // This verifies that the class gets the list of adapters when created;
447 // and initializes with an existing adapter if there is one.
448 EXPECT_TRUE(adapter_->IsPresent());
449 EXPECT_FALSE(adapter_->IsPowered());
450 EXPECT_EQ(FakeBluetoothAdapterClient::kAdapterAddress,
451 adapter_->GetAddress());
452 EXPECT_FALSE(adapter_->IsDiscovering());
454 // There should be a device
455 BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
456 EXPECT_EQ(2U, devices.size());
457 EXPECT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
458 devices[0]->GetAddress());
459 EXPECT_EQ(FakeBluetoothDeviceClient::kPairedUnconnectableDeviceAddress,
460 devices[1]->GetAddress());
463 TEST_F(BluetoothChromeOSTest, BecomePresent) {
464 fake_bluetooth_adapter_client_->SetVisible(false);
465 GetAdapter();
466 ASSERT_FALSE(adapter_->IsPresent());
468 // Install an observer; expect the AdapterPresentChanged to be called
469 // with true, and IsPresent() to return true.
470 TestObserver observer(adapter_);
472 fake_bluetooth_adapter_client_->SetVisible(true);
474 EXPECT_EQ(1, observer.present_changed_count_);
475 EXPECT_TRUE(observer.last_present_);
477 EXPECT_TRUE(adapter_->IsPresent());
479 // We should have had a device announced.
480 EXPECT_EQ(2, observer.device_added_count_);
481 EXPECT_EQ(FakeBluetoothDeviceClient::kPairedUnconnectableDeviceAddress,
482 observer.last_device_address_);
484 // Other callbacks shouldn't be called if the values are false.
485 EXPECT_EQ(0, observer.powered_changed_count_);
486 EXPECT_EQ(0, observer.discovering_changed_count_);
487 EXPECT_FALSE(adapter_->IsPowered());
488 EXPECT_FALSE(adapter_->IsDiscovering());
491 TEST_F(BluetoothChromeOSTest, BecomeNotPresent) {
492 GetAdapter();
493 ASSERT_TRUE(adapter_->IsPresent());
495 // Install an observer; expect the AdapterPresentChanged to be called
496 // with false, and IsPresent() to return false.
497 TestObserver observer(adapter_);
499 fake_bluetooth_adapter_client_->SetVisible(false);
501 EXPECT_EQ(1, observer.present_changed_count_);
502 EXPECT_FALSE(observer.last_present_);
504 EXPECT_FALSE(adapter_->IsPresent());
506 // We should have had a device removed.
507 EXPECT_EQ(2, observer.device_removed_count_);
508 EXPECT_EQ(FakeBluetoothDeviceClient::kPairedUnconnectableDeviceAddress,
509 observer.last_device_address_);
511 // Other callbacks shouldn't be called since the values are false.
512 EXPECT_EQ(0, observer.powered_changed_count_);
513 EXPECT_EQ(0, observer.discovering_changed_count_);
514 EXPECT_FALSE(adapter_->IsPowered());
515 EXPECT_FALSE(adapter_->IsDiscovering());
518 TEST_F(BluetoothChromeOSTest, SecondAdapter) {
519 GetAdapter();
520 ASSERT_TRUE(adapter_->IsPresent());
522 // Install an observer, then add a second adapter. Nothing should change,
523 // we ignore the second adapter.
524 TestObserver observer(adapter_);
526 fake_bluetooth_adapter_client_->SetSecondVisible(true);
528 EXPECT_EQ(0, observer.present_changed_count_);
530 EXPECT_TRUE(adapter_->IsPresent());
531 EXPECT_EQ(FakeBluetoothAdapterClient::kAdapterAddress,
532 adapter_->GetAddress());
534 // Try removing the first adapter, we should now act as if the adapter
535 // is no longer present rather than fall back to the second.
536 fake_bluetooth_adapter_client_->SetVisible(false);
538 EXPECT_EQ(1, observer.present_changed_count_);
539 EXPECT_FALSE(observer.last_present_);
541 EXPECT_FALSE(adapter_->IsPresent());
543 // We should have had a device removed.
544 EXPECT_EQ(2, observer.device_removed_count_);
545 EXPECT_EQ(FakeBluetoothDeviceClient::kPairedUnconnectableDeviceAddress,
546 observer.last_device_address_);
548 // Other callbacks shouldn't be called since the values are false.
549 EXPECT_EQ(0, observer.powered_changed_count_);
550 EXPECT_EQ(0, observer.discovering_changed_count_);
551 EXPECT_FALSE(adapter_->IsPowered());
552 EXPECT_FALSE(adapter_->IsDiscovering());
554 observer.device_removed_count_ = 0;
556 // Removing the second adapter shouldn't set anything either.
557 fake_bluetooth_adapter_client_->SetSecondVisible(false);
559 EXPECT_EQ(0, observer.device_removed_count_);
560 EXPECT_EQ(0, observer.powered_changed_count_);
561 EXPECT_EQ(0, observer.discovering_changed_count_);
564 TEST_F(BluetoothChromeOSTest, BecomePowered) {
565 GetAdapter();
566 ASSERT_FALSE(adapter_->IsPowered());
568 // Install an observer; expect the AdapterPoweredChanged to be called
569 // with true, and IsPowered() to return true.
570 TestObserver observer(adapter_);
572 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
573 EXPECT_EQ(1, callback_count_);
574 EXPECT_EQ(0, error_callback_count_);
576 EXPECT_EQ(1, observer.powered_changed_count_);
577 EXPECT_TRUE(observer.last_powered_);
579 EXPECT_TRUE(adapter_->IsPowered());
582 TEST_F(BluetoothChromeOSTest, BecomeNotPowered) {
583 GetAdapter();
584 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
585 EXPECT_EQ(1, callback_count_);
586 EXPECT_EQ(0, error_callback_count_);
587 callback_count_ = 0;
589 ASSERT_TRUE(adapter_->IsPowered());
591 // Install an observer; expect the AdapterPoweredChanged to be called
592 // with false, and IsPowered() to return false.
593 TestObserver observer(adapter_);
595 adapter_->SetPowered(false, GetCallback(), GetErrorCallback());
596 EXPECT_EQ(1, callback_count_);
597 EXPECT_EQ(0, error_callback_count_);
599 EXPECT_EQ(1, observer.powered_changed_count_);
600 EXPECT_FALSE(observer.last_powered_);
602 EXPECT_FALSE(adapter_->IsPowered());
605 TEST_F(BluetoothChromeOSTest, SetPoweredWhenNotPresent) {
606 GetAdapter();
607 ASSERT_TRUE(adapter_->IsPresent());
609 // Install an observer; expect the AdapterPresentChanged to be called
610 // with false, and IsPresent() to return false.
611 TestObserver observer(adapter_);
613 fake_bluetooth_adapter_client_->SetVisible(false);
615 EXPECT_EQ(1, observer.present_changed_count_);
616 EXPECT_FALSE(observer.last_present_);
618 EXPECT_FALSE(adapter_->IsPresent());
619 EXPECT_FALSE(adapter_->IsPowered());
621 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
622 EXPECT_EQ(0, callback_count_);
623 EXPECT_EQ(1, error_callback_count_);
625 EXPECT_EQ(0, observer.powered_changed_count_);
626 EXPECT_FALSE(observer.last_powered_);
628 EXPECT_FALSE(adapter_->IsPowered());
631 TEST_F(BluetoothChromeOSTest, ChangeAdapterName) {
632 GetAdapter();
634 static const std::string new_name(".__.");
636 adapter_->SetName(new_name, GetCallback(), GetErrorCallback());
637 EXPECT_EQ(1, callback_count_);
638 EXPECT_EQ(0, error_callback_count_);
640 EXPECT_EQ(new_name, adapter_->GetName());
643 TEST_F(BluetoothChromeOSTest, ChangeAdapterNameWhenNotPresent) {
644 GetAdapter();
645 ASSERT_TRUE(adapter_->IsPresent());
647 // Install an observer; expect the AdapterPresentChanged to be called
648 // with false, and IsPresent() to return false.
649 TestObserver observer(adapter_);
651 fake_bluetooth_adapter_client_->SetVisible(false);
653 EXPECT_EQ(1, observer.present_changed_count_);
654 EXPECT_FALSE(observer.last_present_);
656 EXPECT_FALSE(adapter_->IsPresent());
657 EXPECT_FALSE(adapter_->IsPowered());
659 adapter_->SetName("^o^", GetCallback(), GetErrorCallback());
660 EXPECT_EQ(0, callback_count_);
661 EXPECT_EQ(1, error_callback_count_);
663 EXPECT_EQ("", adapter_->GetName());
666 TEST_F(BluetoothChromeOSTest, BecomeDiscoverable) {
667 GetAdapter();
668 ASSERT_FALSE(adapter_->IsDiscoverable());
670 // Install an observer; expect the AdapterDiscoverableChanged to be called
671 // with true, and IsDiscoverable() to return true.
672 TestObserver observer(adapter_);
674 adapter_->SetDiscoverable(true, GetCallback(), GetErrorCallback());
675 EXPECT_EQ(1, callback_count_);
676 EXPECT_EQ(0, error_callback_count_);
678 EXPECT_EQ(1, observer.discoverable_changed_count_);
680 EXPECT_TRUE(adapter_->IsDiscoverable());
683 TEST_F(BluetoothChromeOSTest, BecomeNotDiscoverable) {
684 GetAdapter();
685 adapter_->SetDiscoverable(true, GetCallback(), GetErrorCallback());
686 EXPECT_EQ(1, callback_count_);
687 EXPECT_EQ(0, error_callback_count_);
688 callback_count_ = 0;
690 ASSERT_TRUE(adapter_->IsDiscoverable());
692 // Install an observer; expect the AdapterDiscoverableChanged to be called
693 // with false, and IsDiscoverable() to return false.
694 TestObserver observer(adapter_);
696 adapter_->SetDiscoverable(false, GetCallback(), GetErrorCallback());
697 EXPECT_EQ(1, callback_count_);
698 EXPECT_EQ(0, error_callback_count_);
700 EXPECT_EQ(1, observer.discoverable_changed_count_);
702 EXPECT_FALSE(adapter_->IsDiscoverable());
705 TEST_F(BluetoothChromeOSTest, SetDiscoverableWhenNotPresent) {
706 GetAdapter();
707 ASSERT_TRUE(adapter_->IsPresent());
708 ASSERT_FALSE(adapter_->IsDiscoverable());
710 // Install an observer; expect the AdapterDiscoverableChanged to be called
711 // with true, and IsDiscoverable() to return true.
712 TestObserver observer(adapter_);
714 fake_bluetooth_adapter_client_->SetVisible(false);
716 EXPECT_EQ(1, observer.present_changed_count_);
717 EXPECT_FALSE(observer.last_present_);
719 EXPECT_FALSE(adapter_->IsPresent());
720 EXPECT_FALSE(adapter_->IsDiscoverable());
722 adapter_->SetDiscoverable(true, GetCallback(), GetErrorCallback());
723 EXPECT_EQ(0, callback_count_);
724 EXPECT_EQ(1, error_callback_count_);
726 EXPECT_EQ(0, observer.discoverable_changed_count_);
728 EXPECT_FALSE(adapter_->IsDiscoverable());
731 TEST_F(BluetoothChromeOSTest, StopDiscovery) {
732 GetAdapter();
734 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
735 adapter_->StartDiscoverySession(
736 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
737 base::Unretained(this)),
738 GetErrorCallback());
739 message_loop_.Run();
740 EXPECT_EQ(2, callback_count_);
741 EXPECT_EQ(0, error_callback_count_);
742 callback_count_ = 0;
744 ASSERT_TRUE(adapter_->IsPowered());
745 ASSERT_TRUE(adapter_->IsDiscovering());
746 ASSERT_EQ((size_t)1, discovery_sessions_.size());
747 ASSERT_TRUE(discovery_sessions_[0]->IsActive());
749 // Install an observer; aside from the callback, expect the
750 // AdapterDiscoveringChanged method to be called and no longer to be
751 // discovering,
752 TestObserver observer(adapter_);
754 discovery_sessions_[0]->Stop(GetCallback(), GetErrorCallback());
755 message_loop_.Run();
756 EXPECT_EQ(1, callback_count_);
757 EXPECT_EQ(0, error_callback_count_);
759 EXPECT_EQ(1, observer.discovering_changed_count_);
760 EXPECT_FALSE(observer.last_discovering_);
762 EXPECT_FALSE(adapter_->IsDiscovering());
765 TEST_F(BluetoothChromeOSTest, Discovery) {
766 // Test a simulated discovery session.
767 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
768 GetAdapter();
770 TestObserver observer(adapter_);
772 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
773 adapter_->StartDiscoverySession(
774 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
775 base::Unretained(this)),
776 GetErrorCallback());
777 message_loop_.Run();
778 EXPECT_EQ(2, callback_count_);
779 EXPECT_EQ(0, error_callback_count_);
780 callback_count_ = 0;
782 ASSERT_TRUE(adapter_->IsPowered());
783 ASSERT_TRUE(adapter_->IsDiscovering());
784 ASSERT_EQ((size_t)1, discovery_sessions_.size());
785 ASSERT_TRUE(discovery_sessions_[0]->IsActive());
787 // First two devices to appear.
788 message_loop_.Run();
790 EXPECT_EQ(2, observer.device_added_count_);
791 EXPECT_EQ(FakeBluetoothDeviceClient::kLowEnergyAddress,
792 observer.last_device_address_);
794 // Next we should get another two devices...
795 message_loop_.Run();
796 EXPECT_EQ(4, observer.device_added_count_);
798 // Okay, let's run forward until a device is actually removed...
799 while (!observer.device_removed_count_)
800 message_loop_.Run();
802 EXPECT_EQ(1, observer.device_removed_count_);
803 EXPECT_EQ(FakeBluetoothDeviceClient::kVanishingDeviceAddress,
804 observer.last_device_address_);
807 TEST_F(BluetoothChromeOSTest, PoweredAndDiscovering) {
808 GetAdapter();
809 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
810 adapter_->StartDiscoverySession(
811 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
812 base::Unretained(this)),
813 GetErrorCallback());
814 message_loop_.Run();
815 EXPECT_EQ(2, callback_count_);
816 EXPECT_EQ(0, error_callback_count_);
817 callback_count_ = 0;
818 ASSERT_EQ((size_t)1, discovery_sessions_.size());
819 ASSERT_TRUE(discovery_sessions_[0]->IsActive());
821 // Stop the timers that the simulation uses
822 fake_bluetooth_device_client_->EndDiscoverySimulation(
823 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath));
825 ASSERT_TRUE(adapter_->IsPowered());
826 ASSERT_TRUE(adapter_->IsDiscovering());
828 fake_bluetooth_adapter_client_->SetVisible(false);
829 ASSERT_FALSE(adapter_->IsPresent());
830 ASSERT_FALSE(discovery_sessions_[0]->IsActive());
832 // Install an observer; expect the AdapterPresentChanged,
833 // AdapterPoweredChanged and AdapterDiscoveringChanged methods to be called
834 // with true, and IsPresent(), IsPowered() and IsDiscovering() to all
835 // return true.
836 TestObserver observer(adapter_);
838 fake_bluetooth_adapter_client_->SetVisible(true);
840 EXPECT_EQ(1, observer.present_changed_count_);
841 EXPECT_TRUE(observer.last_present_);
842 EXPECT_TRUE(adapter_->IsPresent());
844 EXPECT_EQ(1, observer.powered_changed_count_);
845 EXPECT_TRUE(observer.last_powered_);
846 EXPECT_TRUE(adapter_->IsPowered());
848 EXPECT_EQ(1, observer.discovering_changed_count_);
849 EXPECT_TRUE(observer.last_discovering_);
850 EXPECT_TRUE(adapter_->IsDiscovering());
852 observer.present_changed_count_ = 0;
853 observer.powered_changed_count_ = 0;
854 observer.discovering_changed_count_ = 0;
856 // Now mark the adapter not present again. Expect the methods to be called
857 // again, to reset the properties back to false
858 fake_bluetooth_adapter_client_->SetVisible(false);
860 EXPECT_EQ(1, observer.present_changed_count_);
861 EXPECT_FALSE(observer.last_present_);
862 EXPECT_FALSE(adapter_->IsPresent());
864 EXPECT_EQ(1, observer.powered_changed_count_);
865 EXPECT_FALSE(observer.last_powered_);
866 EXPECT_FALSE(adapter_->IsPowered());
868 EXPECT_EQ(1, observer.discovering_changed_count_);
869 EXPECT_FALSE(observer.last_discovering_);
870 EXPECT_FALSE(adapter_->IsDiscovering());
873 // This unit test asserts that the basic reference counting logic works
874 // correctly for discovery requests done via the BluetoothAdapter.
875 TEST_F(BluetoothChromeOSTest, MultipleDiscoverySessions) {
876 GetAdapter();
877 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
878 EXPECT_EQ(1, callback_count_);
879 EXPECT_EQ(0, error_callback_count_);
880 EXPECT_TRUE(adapter_->IsPowered());
881 callback_count_ = 0;
883 TestObserver observer(adapter_);
885 EXPECT_EQ(0, observer.discovering_changed_count_);
886 EXPECT_FALSE(observer.last_discovering_);
887 EXPECT_FALSE(adapter_->IsDiscovering());
889 // Request device discovery 3 times.
890 for (int i = 0; i < 3; i++) {
891 adapter_->StartDiscoverySession(
892 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
893 base::Unretained(this)),
894 GetErrorCallback());
896 // Run only once, as there should have been one D-Bus call.
897 message_loop_.Run();
899 // The observer should have received the discovering changed event exactly
900 // once, the success callback should have been called 3 times and the adapter
901 // should be discovering.
902 EXPECT_EQ(1, observer.discovering_changed_count_);
903 EXPECT_EQ(3, callback_count_);
904 EXPECT_EQ(0, error_callback_count_);
905 EXPECT_TRUE(observer.last_discovering_);
906 EXPECT_TRUE(adapter_->IsDiscovering());
907 ASSERT_EQ((size_t)3, discovery_sessions_.size());
909 // Request to stop discovery twice.
910 for (int i = 0; i < 2; i++) {
911 discovery_sessions_[i]->Stop(GetCallback(), GetErrorCallback());
914 // The observer should have received no additional discovering changed events,
915 // the success callback should have been called 2 times and the adapter should
916 // still be discovering.
917 EXPECT_EQ(1, observer.discovering_changed_count_);
918 EXPECT_EQ(5, callback_count_);
919 EXPECT_EQ(0, error_callback_count_);
920 EXPECT_TRUE(observer.last_discovering_);
921 EXPECT_TRUE(adapter_->IsDiscovering());
922 EXPECT_TRUE(adapter_->IsDiscovering());
923 EXPECT_FALSE(discovery_sessions_[0]->IsActive());
924 EXPECT_FALSE(discovery_sessions_[1]->IsActive());
925 EXPECT_TRUE(discovery_sessions_[2]->IsActive());
927 // Request device discovery 3 times.
928 for (int i = 0; i < 3; i++) {
929 adapter_->StartDiscoverySession(
930 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
931 base::Unretained(this)),
932 GetErrorCallback());
935 // The observer should have received no additional discovering changed events,
936 // the success callback should have been called 3 times and the adapter should
937 // still be discovering.
938 EXPECT_EQ(1, observer.discovering_changed_count_);
939 EXPECT_EQ(8, callback_count_);
940 EXPECT_EQ(0, error_callback_count_);
941 EXPECT_TRUE(observer.last_discovering_);
942 EXPECT_TRUE(adapter_->IsDiscovering());
943 ASSERT_EQ((size_t)6, discovery_sessions_.size());
945 // Request to stop discovery 4 times.
946 for (int i = 2; i < 6; i++) {
947 discovery_sessions_[i]->Stop(GetCallback(), GetErrorCallback());
949 // Run only once, as there should have been one D-Bus call.
950 message_loop_.Run();
952 // The observer should have received the discovering changed event exactly
953 // once, the success callback should have been called 4 times and the adapter
954 // should no longer be discovering.
955 EXPECT_EQ(2, observer.discovering_changed_count_);
956 EXPECT_EQ(12, callback_count_);
957 EXPECT_EQ(0, error_callback_count_);
958 EXPECT_FALSE(observer.last_discovering_);
959 EXPECT_FALSE(adapter_->IsDiscovering());
961 // All discovery sessions should be inactive.
962 for (int i = 0; i < 6; i++)
963 EXPECT_FALSE(discovery_sessions_[i]->IsActive());
965 // Request to stop discovery on of the inactive sessions.
966 discovery_sessions_[0]->Stop(GetCallback(), GetErrorCallback());
968 // The call should have failed.
969 EXPECT_EQ(2, observer.discovering_changed_count_);
970 EXPECT_EQ(12, callback_count_);
971 EXPECT_EQ(1, error_callback_count_);
972 EXPECT_FALSE(observer.last_discovering_);
973 EXPECT_FALSE(adapter_->IsDiscovering());
976 // This unit test asserts that the reference counting logic works correctly in
977 // the cases when the adapter gets reset and D-Bus calls are made outside of
978 // the BluetoothAdapter.
979 TEST_F(BluetoothChromeOSTest,
980 UnexpectedChangesDuringMultipleDiscoverySessions) {
981 GetAdapter();
982 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
983 EXPECT_EQ(1, callback_count_);
984 EXPECT_EQ(0, error_callback_count_);
985 EXPECT_TRUE(adapter_->IsPowered());
986 callback_count_ = 0;
988 TestObserver observer(adapter_);
990 EXPECT_EQ(0, observer.discovering_changed_count_);
991 EXPECT_FALSE(observer.last_discovering_);
992 EXPECT_FALSE(adapter_->IsDiscovering());
994 // Request device discovery 3 times.
995 for (int i = 0; i < 3; i++) {
996 adapter_->StartDiscoverySession(
997 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
998 base::Unretained(this)),
999 GetErrorCallback());
1001 // Run only once, as there should have been one D-Bus call.
1002 message_loop_.Run();
1004 // The observer should have received the discovering changed event exactly
1005 // once, the success callback should have been called 3 times and the adapter
1006 // should be discovering.
1007 EXPECT_EQ(1, observer.discovering_changed_count_);
1008 EXPECT_EQ(3, callback_count_);
1009 EXPECT_EQ(0, error_callback_count_);
1010 EXPECT_TRUE(observer.last_discovering_);
1011 EXPECT_TRUE(adapter_->IsDiscovering());
1012 ASSERT_EQ((size_t)3, discovery_sessions_.size());
1014 for (int i = 0; i < 3; i++)
1015 EXPECT_TRUE(discovery_sessions_[i]->IsActive());
1017 // Stop the timers that the simulation uses
1018 fake_bluetooth_device_client_->EndDiscoverySimulation(
1019 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath));
1021 ASSERT_TRUE(adapter_->IsPowered());
1022 ASSERT_TRUE(adapter_->IsDiscovering());
1024 // Stop device discovery behind the adapter. The adapter and the observer
1025 // should be notified of the change and the reference count should be reset.
1026 // Even though FakeBluetoothAdapterClient does its own reference counting and
1027 // we called 3 BluetoothAdapter::StartDiscoverySession 3 times, the
1028 // FakeBluetoothAdapterClient's count should be only 1 and a single call to
1029 // FakeBluetoothAdapterClient::StopDiscovery should work.
1030 fake_bluetooth_adapter_client_->StopDiscovery(
1031 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath), GetCallback(),
1032 base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
1033 base::Unretained(this)));
1034 message_loop_.Run();
1035 EXPECT_EQ(2, observer.discovering_changed_count_);
1036 EXPECT_EQ(4, callback_count_);
1037 EXPECT_EQ(0, error_callback_count_);
1038 EXPECT_FALSE(observer.last_discovering_);
1039 EXPECT_FALSE(adapter_->IsDiscovering());
1041 // All discovery session instances should have been updated.
1042 for (int i = 0; i < 3; i++)
1043 EXPECT_FALSE(discovery_sessions_[i]->IsActive());
1044 discovery_sessions_.clear();
1046 // It should be possible to successfully start discovery.
1047 for (int i = 0; i < 2; i++) {
1048 adapter_->StartDiscoverySession(
1049 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1050 base::Unretained(this)),
1051 GetErrorCallback());
1053 // Run only once, as there should have been one D-Bus call.
1054 message_loop_.Run();
1055 EXPECT_EQ(3, observer.discovering_changed_count_);
1056 EXPECT_EQ(6, callback_count_);
1057 EXPECT_EQ(0, error_callback_count_);
1058 EXPECT_TRUE(observer.last_discovering_);
1059 EXPECT_TRUE(adapter_->IsDiscovering());
1060 ASSERT_EQ((size_t)2, discovery_sessions_.size());
1062 for (int i = 0; i < 2; i++)
1063 EXPECT_TRUE(discovery_sessions_[i]->IsActive());
1065 fake_bluetooth_device_client_->EndDiscoverySimulation(
1066 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath));
1068 // Make the adapter disappear and appear. This will make it come back as
1069 // discovering. When this happens, the reference count should become and
1070 // remain 0 as no new request was made through the BluetoothAdapter.
1071 fake_bluetooth_adapter_client_->SetVisible(false);
1072 ASSERT_FALSE(adapter_->IsPresent());
1073 EXPECT_EQ(4, observer.discovering_changed_count_);
1074 EXPECT_EQ(6, callback_count_);
1075 EXPECT_EQ(0, error_callback_count_);
1076 EXPECT_FALSE(observer.last_discovering_);
1077 EXPECT_FALSE(adapter_->IsDiscovering());
1079 for (int i = 0; i < 2; i++)
1080 EXPECT_FALSE(discovery_sessions_[i]->IsActive());
1081 discovery_sessions_.clear();
1083 fake_bluetooth_adapter_client_->SetVisible(true);
1084 ASSERT_TRUE(adapter_->IsPresent());
1085 EXPECT_EQ(5, observer.discovering_changed_count_);
1086 EXPECT_EQ(6, callback_count_);
1087 EXPECT_EQ(0, error_callback_count_);
1088 EXPECT_TRUE(observer.last_discovering_);
1089 EXPECT_TRUE(adapter_->IsDiscovering());
1091 // Start and stop discovery. At this point, FakeBluetoothAdapterClient has
1092 // a reference count that is equal to 1. Pretend that this was done by an
1093 // application other than us. Starting and stopping discovery will succeed
1094 // but it won't cause the discovery state to change.
1095 adapter_->StartDiscoverySession(
1096 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1097 base::Unretained(this)),
1098 GetErrorCallback());
1099 message_loop_.Run(); // Run the loop, as there should have been a D-Bus call.
1100 EXPECT_EQ(5, observer.discovering_changed_count_);
1101 EXPECT_EQ(7, callback_count_);
1102 EXPECT_EQ(0, error_callback_count_);
1103 EXPECT_TRUE(observer.last_discovering_);
1104 EXPECT_TRUE(adapter_->IsDiscovering());
1105 ASSERT_EQ((size_t)1, discovery_sessions_.size());
1106 EXPECT_TRUE(discovery_sessions_[0]->IsActive());
1108 discovery_sessions_[0]->Stop(GetCallback(), GetErrorCallback());
1109 message_loop_.Run(); // Run the loop, as there should have been a D-Bus call.
1110 EXPECT_EQ(5, observer.discovering_changed_count_);
1111 EXPECT_EQ(8, callback_count_);
1112 EXPECT_EQ(0, error_callback_count_);
1113 EXPECT_TRUE(observer.last_discovering_);
1114 EXPECT_TRUE(adapter_->IsDiscovering());
1115 EXPECT_FALSE(discovery_sessions_[0]->IsActive());
1116 discovery_sessions_.clear();
1118 // Start discovery again.
1119 adapter_->StartDiscoverySession(
1120 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1121 base::Unretained(this)),
1122 GetErrorCallback());
1123 message_loop_.Run(); // Run the loop, as there should have been a D-Bus call.
1124 EXPECT_EQ(5, observer.discovering_changed_count_);
1125 EXPECT_EQ(9, callback_count_);
1126 EXPECT_EQ(0, error_callback_count_);
1127 EXPECT_TRUE(observer.last_discovering_);
1128 EXPECT_TRUE(adapter_->IsDiscovering());
1129 ASSERT_EQ((size_t)1, discovery_sessions_.size());
1130 EXPECT_TRUE(discovery_sessions_[0]->IsActive());
1132 // Stop discovery via D-Bus. The fake client's reference count will drop but
1133 // the discovery state won't change since our BluetoothAdapter also just
1134 // requested it via D-Bus.
1135 fake_bluetooth_adapter_client_->StopDiscovery(
1136 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath), GetCallback(),
1137 base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
1138 base::Unretained(this)));
1139 message_loop_.Run();
1140 EXPECT_EQ(5, observer.discovering_changed_count_);
1141 EXPECT_EQ(10, callback_count_);
1142 EXPECT_EQ(0, error_callback_count_);
1143 EXPECT_TRUE(observer.last_discovering_);
1144 EXPECT_TRUE(adapter_->IsDiscovering());
1146 // Now end the discovery session. This should change the adapter's discovery
1147 // state.
1148 discovery_sessions_[0]->Stop(GetCallback(), GetErrorCallback());
1149 message_loop_.Run();
1150 EXPECT_EQ(6, observer.discovering_changed_count_);
1151 EXPECT_EQ(11, callback_count_);
1152 EXPECT_EQ(0, error_callback_count_);
1153 EXPECT_FALSE(observer.last_discovering_);
1154 EXPECT_FALSE(adapter_->IsDiscovering());
1155 EXPECT_FALSE(discovery_sessions_[0]->IsActive());
1158 TEST_F(BluetoothChromeOSTest, InvalidatedDiscoverySessions) {
1159 GetAdapter();
1160 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
1161 EXPECT_EQ(1, callback_count_);
1162 EXPECT_EQ(0, error_callback_count_);
1163 EXPECT_TRUE(adapter_->IsPowered());
1164 callback_count_ = 0;
1166 TestObserver observer(adapter_);
1168 EXPECT_EQ(0, observer.discovering_changed_count_);
1169 EXPECT_FALSE(observer.last_discovering_);
1170 EXPECT_FALSE(adapter_->IsDiscovering());
1172 // Request device discovery 3 times.
1173 for (int i = 0; i < 3; i++) {
1174 adapter_->StartDiscoverySession(
1175 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1176 base::Unretained(this)),
1177 GetErrorCallback());
1179 // Run only once, as there should have been one D-Bus call.
1180 message_loop_.Run();
1182 // The observer should have received the discovering changed event exactly
1183 // once, the success callback should have been called 3 times and the adapter
1184 // should be discovering.
1185 EXPECT_EQ(1, observer.discovering_changed_count_);
1186 EXPECT_EQ(3, callback_count_);
1187 EXPECT_EQ(0, error_callback_count_);
1188 EXPECT_TRUE(observer.last_discovering_);
1189 EXPECT_TRUE(adapter_->IsDiscovering());
1190 ASSERT_EQ((size_t)3, discovery_sessions_.size());
1192 for (int i = 0; i < 3; i++)
1193 EXPECT_TRUE(discovery_sessions_[i]->IsActive());
1195 // Stop the timers that the simulation uses
1196 fake_bluetooth_device_client_->EndDiscoverySimulation(
1197 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath));
1199 ASSERT_TRUE(adapter_->IsPowered());
1200 ASSERT_TRUE(adapter_->IsDiscovering());
1202 // Delete all but one discovery session.
1203 discovery_sessions_.pop_back();
1204 discovery_sessions_.pop_back();
1205 ASSERT_EQ((size_t)1, discovery_sessions_.size());
1206 EXPECT_TRUE(discovery_sessions_[0]->IsActive());
1207 EXPECT_TRUE(adapter_->IsDiscovering());
1209 // Stop device discovery behind the adapter. The one active discovery session
1210 // should become inactive, but more importantly, we shouldn't run into any
1211 // memory errors as the sessions that we explicitly deleted should get
1212 // cleaned up.
1213 fake_bluetooth_adapter_client_->StopDiscovery(
1214 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath), GetCallback(),
1215 base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
1216 base::Unretained(this)));
1217 message_loop_.Run();
1218 EXPECT_EQ(2, observer.discovering_changed_count_);
1219 EXPECT_EQ(4, callback_count_);
1220 EXPECT_EQ(0, error_callback_count_);
1221 EXPECT_FALSE(observer.last_discovering_);
1222 EXPECT_FALSE(adapter_->IsDiscovering());
1223 EXPECT_FALSE(discovery_sessions_[0]->IsActive());
1226 TEST_F(BluetoothChromeOSTest, QueuedDiscoveryRequests) {
1227 GetAdapter();
1229 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
1230 EXPECT_EQ(1, callback_count_);
1231 EXPECT_EQ(0, error_callback_count_);
1232 EXPECT_TRUE(adapter_->IsPowered());
1233 callback_count_ = 0;
1235 TestObserver observer(adapter_);
1237 EXPECT_EQ(0, observer.discovering_changed_count_);
1238 EXPECT_FALSE(observer.last_discovering_);
1239 EXPECT_FALSE(adapter_->IsDiscovering());
1241 // Request to start discovery. The call should be pending.
1242 adapter_->StartDiscoverySession(
1243 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1244 base::Unretained(this)),
1245 GetErrorCallback());
1246 EXPECT_EQ(0, callback_count_);
1248 fake_bluetooth_device_client_->EndDiscoverySimulation(
1249 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath));
1251 // The underlying adapter has started discovery, but our call hasn't returned
1252 // yet.
1253 EXPECT_EQ(1, observer.discovering_changed_count_);
1254 EXPECT_TRUE(observer.last_discovering_);
1255 EXPECT_TRUE(adapter_->IsDiscovering());
1256 EXPECT_TRUE(discovery_sessions_.empty());
1258 // Request to start discovery twice. These should get queued and there should
1259 // be no change in state.
1260 for (int i = 0; i < 2; i++) {
1261 adapter_->StartDiscoverySession(
1262 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1263 base::Unretained(this)),
1264 GetErrorCallback());
1266 EXPECT_EQ(0, callback_count_);
1267 EXPECT_EQ(0, error_callback_count_);
1268 EXPECT_EQ(1, observer.discovering_changed_count_);
1269 EXPECT_TRUE(observer.last_discovering_);
1270 EXPECT_TRUE(adapter_->IsDiscovering());
1271 EXPECT_TRUE(discovery_sessions_.empty());
1273 // Process the pending call. The queued calls should execute and the discovery
1274 // session reference count should increase.
1275 message_loop_.Run();
1276 EXPECT_EQ(3, callback_count_);
1277 EXPECT_EQ(0, error_callback_count_);
1278 EXPECT_EQ(1, observer.discovering_changed_count_);
1279 EXPECT_TRUE(observer.last_discovering_);
1280 EXPECT_TRUE(adapter_->IsDiscovering());
1281 ASSERT_EQ((size_t)3, discovery_sessions_.size());
1283 // Verify the reference count by removing sessions 3 times. The last request
1284 // should remain pending.
1285 for (int i = 0; i < 3; i++) {
1286 discovery_sessions_[i]->Stop(GetCallback(), GetErrorCallback());
1288 EXPECT_EQ(5, callback_count_);
1289 EXPECT_EQ(0, error_callback_count_);
1290 EXPECT_EQ(2, observer.discovering_changed_count_);
1291 EXPECT_FALSE(observer.last_discovering_);
1292 EXPECT_FALSE(adapter_->IsDiscovering());
1293 EXPECT_FALSE(discovery_sessions_[0]->IsActive());
1294 EXPECT_FALSE(discovery_sessions_[1]->IsActive());
1295 EXPECT_TRUE(discovery_sessions_[2]->IsActive());
1297 // Request to stop the session whose call is pending should fail.
1298 discovery_sessions_[2]->Stop(GetCallback(), GetErrorCallback());
1299 EXPECT_EQ(5, callback_count_);
1300 EXPECT_EQ(1, error_callback_count_);
1301 EXPECT_EQ(2, observer.discovering_changed_count_);
1302 EXPECT_FALSE(observer.last_discovering_);
1303 EXPECT_FALSE(adapter_->IsDiscovering());
1304 EXPECT_TRUE(discovery_sessions_[2]->IsActive());
1306 // Request to start should get queued.
1307 adapter_->StartDiscoverySession(
1308 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1309 base::Unretained(this)),
1310 GetErrorCallback());
1311 EXPECT_EQ(5, callback_count_);
1312 EXPECT_EQ(1, error_callback_count_);
1313 EXPECT_EQ(2, observer.discovering_changed_count_);
1314 EXPECT_FALSE(observer.last_discovering_);
1315 EXPECT_FALSE(adapter_->IsDiscovering());
1316 ASSERT_EQ((size_t)3, discovery_sessions_.size());
1318 // Run the pending request.
1319 message_loop_.Run();
1320 EXPECT_EQ(6, callback_count_);
1321 EXPECT_EQ(1, error_callback_count_);
1322 EXPECT_EQ(3, observer.discovering_changed_count_);
1323 EXPECT_TRUE(observer.last_discovering_);
1324 EXPECT_TRUE(adapter_->IsDiscovering());
1325 ASSERT_EQ((size_t)3, discovery_sessions_.size());
1326 EXPECT_FALSE(discovery_sessions_[2]->IsActive());
1328 // The queued request to start discovery should have been issued but is still
1329 // pending. Run the loop and verify.
1330 message_loop_.Run();
1331 EXPECT_EQ(7, callback_count_);
1332 EXPECT_EQ(1, error_callback_count_);
1333 EXPECT_EQ(3, observer.discovering_changed_count_);
1334 EXPECT_TRUE(observer.last_discovering_);
1335 EXPECT_TRUE(adapter_->IsDiscovering());
1336 ASSERT_EQ((size_t)4, discovery_sessions_.size());
1337 EXPECT_TRUE(discovery_sessions_[3]->IsActive());
1340 TEST_F(BluetoothChromeOSTest, StartDiscoverySession) {
1341 GetAdapter();
1343 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
1344 EXPECT_EQ(1, callback_count_);
1345 EXPECT_EQ(0, error_callback_count_);
1346 EXPECT_TRUE(adapter_->IsPowered());
1347 callback_count_ = 0;
1349 TestObserver observer(adapter_);
1351 EXPECT_EQ(0, observer.discovering_changed_count_);
1352 EXPECT_FALSE(observer.last_discovering_);
1353 EXPECT_FALSE(adapter_->IsDiscovering());
1354 EXPECT_TRUE(discovery_sessions_.empty());
1356 // Request a new discovery session.
1357 adapter_->StartDiscoverySession(
1358 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1359 base::Unretained(this)),
1360 GetErrorCallback());
1361 message_loop_.Run();
1362 EXPECT_EQ(1, observer.discovering_changed_count_);
1363 EXPECT_EQ(1, callback_count_);
1364 EXPECT_EQ(0, error_callback_count_);
1365 EXPECT_TRUE(observer.last_discovering_);
1366 EXPECT_TRUE(adapter_->IsDiscovering());
1367 ASSERT_EQ((size_t)1, discovery_sessions_.size());
1368 EXPECT_TRUE(discovery_sessions_[0]->IsActive());
1370 // Start another session. A new one should be returned in the callback, which
1371 // in turn will destroy the previous session. Adapter should still be
1372 // discovering and the reference count should be 1.
1373 adapter_->StartDiscoverySession(
1374 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1375 base::Unretained(this)),
1376 GetErrorCallback());
1377 message_loop_.Run();
1378 EXPECT_EQ(1, observer.discovering_changed_count_);
1379 EXPECT_EQ(2, callback_count_);
1380 EXPECT_EQ(0, error_callback_count_);
1381 EXPECT_TRUE(observer.last_discovering_);
1382 EXPECT_TRUE(adapter_->IsDiscovering());
1383 ASSERT_EQ((size_t)2, discovery_sessions_.size());
1384 EXPECT_TRUE(discovery_sessions_[0]->IsActive());
1386 // Request a new session.
1387 adapter_->StartDiscoverySession(
1388 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1389 base::Unretained(this)),
1390 GetErrorCallback());
1391 message_loop_.Run();
1392 EXPECT_EQ(1, observer.discovering_changed_count_);
1393 EXPECT_EQ(3, callback_count_);
1394 EXPECT_EQ(0, error_callback_count_);
1395 EXPECT_TRUE(observer.last_discovering_);
1396 EXPECT_TRUE(adapter_->IsDiscovering());
1397 ASSERT_EQ((size_t)3, discovery_sessions_.size());
1398 EXPECT_TRUE(discovery_sessions_[1]->IsActive());
1399 EXPECT_NE(discovery_sessions_[0], discovery_sessions_[1]);
1401 // Stop the previous discovery session. The session should end but discovery
1402 // should continue.
1403 discovery_sessions_[0]->Stop(GetCallback(), GetErrorCallback());
1404 message_loop_.Run();
1405 EXPECT_EQ(1, observer.discovering_changed_count_);
1406 EXPECT_EQ(4, callback_count_);
1407 EXPECT_EQ(0, error_callback_count_);
1408 EXPECT_TRUE(observer.last_discovering_);
1409 EXPECT_TRUE(adapter_->IsDiscovering());
1410 ASSERT_EQ((size_t)3, discovery_sessions_.size());
1411 EXPECT_FALSE(discovery_sessions_[0]->IsActive());
1412 EXPECT_TRUE(discovery_sessions_[1]->IsActive());
1414 // Delete the current active session. Discovery should eventually stop.
1415 discovery_sessions_.clear();
1416 while (observer.last_discovering_)
1417 message_loop_.RunUntilIdle();
1419 EXPECT_EQ(2, observer.discovering_changed_count_);
1420 EXPECT_EQ(4, callback_count_);
1421 EXPECT_EQ(0, error_callback_count_);
1422 EXPECT_FALSE(observer.last_discovering_);
1423 EXPECT_FALSE(adapter_->IsDiscovering());
1426 TEST_F(BluetoothChromeOSTest, SetDiscoveryFilterBeforeStartDiscovery) {
1427 // Test a simulated discovery session.
1428 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
1429 GetAdapter();
1431 TestObserver observer(adapter_);
1433 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter(
1434 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
1435 df->SetRSSI(-60);
1436 df->AddUUID(BluetoothUUID("1000"));
1437 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter(df);
1439 adapter_->SetPowered(true, base::Bind(&BluetoothChromeOSTest::Callback,
1440 base::Unretained(this)),
1441 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1442 base::Unretained(this)));
1443 adapter_->StartDiscoverySessionWithFilter(
1444 discovery_filter.Pass(),
1445 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1446 base::Unretained(this)),
1447 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1448 base::Unretained(this)));
1449 message_loop_.Run();
1450 EXPECT_EQ(2, callback_count_);
1451 EXPECT_EQ(0, error_callback_count_);
1452 callback_count_ = 0;
1454 ASSERT_TRUE(adapter_->IsPowered());
1455 ASSERT_TRUE(adapter_->IsDiscovering());
1456 ASSERT_EQ((size_t)1, discovery_sessions_.size());
1457 ASSERT_TRUE(discovery_sessions_[0]->IsActive());
1458 ASSERT_TRUE(df->Equals(*discovery_sessions_[0]->GetDiscoveryFilter()));
1460 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1461 EXPECT_NE(nullptr, filter);
1462 EXPECT_EQ("le", *filter->transport);
1463 EXPECT_EQ(-60, *filter->rssi);
1464 EXPECT_EQ(nullptr, filter->pathloss.get());
1465 std::vector<std::string> uuids = *filter->uuids;
1466 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1000"));
1468 discovery_sessions_[0]->Stop(
1469 base::Bind(&BluetoothChromeOSTest::Callback, base::Unretained(this)),
1470 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1471 base::Unretained(this)));
1473 message_loop_.Run();
1475 EXPECT_EQ(1, callback_count_);
1476 EXPECT_EQ(0, error_callback_count_);
1478 ASSERT_TRUE(adapter_->IsPowered());
1479 ASSERT_FALSE(adapter_->IsDiscovering());
1480 ASSERT_EQ((size_t)1, discovery_sessions_.size());
1481 ASSERT_FALSE(discovery_sessions_[0]->IsActive());
1482 ASSERT_EQ(discovery_sessions_[0]->GetDiscoveryFilter(),
1483 (BluetoothDiscoveryFilter*)NULL);
1485 filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1486 EXPECT_EQ(nullptr, filter);
1489 TEST_F(BluetoothChromeOSTest, SetDiscoveryFilterBeforeStartDiscoveryFail) {
1490 // Test a simulated discovery session.
1491 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
1492 GetAdapter();
1494 TestObserver observer(adapter_);
1496 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter(
1497 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
1498 df->SetRSSI(-60);
1499 df->AddUUID(BluetoothUUID("1000"));
1500 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter(df);
1502 adapter_->SetPowered(true, base::Bind(&BluetoothChromeOSTest::Callback,
1503 base::Unretained(this)),
1504 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1505 base::Unretained(this)));
1506 EXPECT_EQ(1, callback_count_);
1507 callback_count_ = 0;
1509 fake_bluetooth_adapter_client_->MakeSetDiscoveryFilterFail();
1511 adapter_->StartDiscoverySessionWithFilter(
1512 discovery_filter.Pass(),
1513 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1514 base::Unretained(this)),
1515 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1516 base::Unretained(this)));
1518 message_loop_.Run();
1520 EXPECT_EQ(1, error_callback_count_);
1521 error_callback_count_ = 0;
1523 ASSERT_TRUE(adapter_->IsPowered());
1524 ASSERT_FALSE(adapter_->IsDiscovering());
1525 ASSERT_EQ((size_t)0, discovery_sessions_.size());
1527 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1528 EXPECT_EQ(nullptr, filter);
1531 // This test queues two requests to StartDiscovery with pre set filter. This
1532 // should result in SetDiscoveryFilter, then StartDiscovery, and SetDiscovery
1533 // DBus calls
1534 TEST_F(BluetoothChromeOSTest, QueuedSetDiscoveryFilterBeforeStartDiscovery) {
1535 // Test a simulated discovery session.
1536 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
1537 GetAdapter();
1539 TestObserver observer(adapter_);
1541 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter(
1542 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
1543 df->SetRSSI(-60);
1544 df->AddUUID(BluetoothUUID("1000"));
1545 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter(df);
1547 BluetoothDiscoveryFilter* df2 = new BluetoothDiscoveryFilter(
1548 BluetoothDiscoveryFilter::Transport::TRANSPORT_CLASSIC);
1549 df2->SetRSSI(-65);
1550 df2->AddUUID(BluetoothUUID("1002"));
1551 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter2(df2);
1553 adapter_->SetPowered(true, base::Bind(&BluetoothChromeOSTest::Callback,
1554 base::Unretained(this)),
1555 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1556 base::Unretained(this)));
1558 EXPECT_EQ(1, callback_count_);
1559 EXPECT_EQ(0, error_callback_count_);
1560 callback_count_ = 0;
1562 // Queue two requests to start discovery session with filter.
1563 adapter_->StartDiscoverySessionWithFilter(
1564 discovery_filter.Pass(),
1565 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1566 base::Unretained(this)),
1567 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1568 base::Unretained(this)));
1570 adapter_->StartDiscoverySessionWithFilter(
1571 discovery_filter2.Pass(),
1572 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1573 base::Unretained(this)),
1574 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1575 base::Unretained(this)));
1577 // Run requests, on DBus level there should be call SetDiscoveryFilter, then
1578 // StartDiscovery, then SetDiscoveryFilter again.
1579 message_loop_.Run();
1580 message_loop_.Run();
1582 EXPECT_EQ(2, callback_count_);
1583 EXPECT_EQ(0, error_callback_count_);
1584 callback_count_ = 0;
1586 ASSERT_TRUE(adapter_->IsPowered());
1587 ASSERT_TRUE(adapter_->IsDiscovering());
1588 ASSERT_EQ((size_t)2, discovery_sessions_.size());
1589 ASSERT_TRUE(discovery_sessions_[0]->IsActive());
1590 ASSERT_TRUE(df->Equals(*discovery_sessions_[0]->GetDiscoveryFilter()));
1591 ASSERT_TRUE(discovery_sessions_[1]->IsActive());
1592 ASSERT_TRUE(df2->Equals(*discovery_sessions_[1]->GetDiscoveryFilter()));
1594 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1595 EXPECT_NE(nullptr, filter);
1596 EXPECT_EQ("auto", *filter->transport);
1597 EXPECT_EQ(-65, *filter->rssi);
1598 EXPECT_EQ(nullptr, filter->pathloss.get());
1599 auto uuids = *filter->uuids;
1600 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1000"));
1601 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1002"));
1603 discovery_sessions_[0]->Stop(
1604 base::Bind(&BluetoothChromeOSTest::Callback, base::Unretained(this)),
1605 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1606 base::Unretained(this)));
1608 discovery_sessions_[1]->Stop(
1609 base::Bind(&BluetoothChromeOSTest::Callback, base::Unretained(this)),
1610 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1611 base::Unretained(this)));
1613 message_loop_.Run();
1615 EXPECT_EQ(2, callback_count_);
1616 EXPECT_EQ(0, error_callback_count_);
1618 ASSERT_TRUE(adapter_->IsPowered());
1619 ASSERT_FALSE(adapter_->IsDiscovering());
1620 ASSERT_FALSE(discovery_sessions_[0]->IsActive());
1621 ASSERT_EQ(discovery_sessions_[0]->GetDiscoveryFilter(),
1622 (BluetoothDiscoveryFilter*)NULL);
1623 ASSERT_FALSE(discovery_sessions_[1]->IsActive());
1624 ASSERT_EQ(discovery_sessions_[1]->GetDiscoveryFilter(),
1625 (BluetoothDiscoveryFilter*)NULL);
1627 filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1628 EXPECT_EQ(nullptr, filter);
1631 // Call StartFilteredDiscovery twice (2nd time while 1st call is still pending).
1632 // Make the first SetDiscoveryFilter fail and the second one succeed. It should
1633 // end up with one active discovery session.
1634 TEST_F(BluetoothChromeOSTest,
1635 QueuedSetDiscoveryFilterBeforeStartDiscoveryFail) {
1636 // Test a simulated discovery session.
1637 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
1638 GetAdapter();
1640 TestObserver observer(adapter_);
1642 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter(
1643 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
1644 df->SetRSSI(-60);
1645 df->AddUUID(BluetoothUUID("1000"));
1646 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter(df);
1648 BluetoothDiscoveryFilter* df2 = new BluetoothDiscoveryFilter(
1649 BluetoothDiscoveryFilter::Transport::TRANSPORT_CLASSIC);
1650 df2->SetRSSI(-65);
1651 df2->AddUUID(BluetoothUUID("1002"));
1652 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter2(df2);
1654 adapter_->SetPowered(true, base::Bind(&BluetoothChromeOSTest::Callback,
1655 base::Unretained(this)),
1656 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1657 base::Unretained(this)));
1659 EXPECT_EQ(1, callback_count_);
1660 EXPECT_EQ(0, error_callback_count_);
1661 callback_count_ = 0;
1663 fake_bluetooth_adapter_client_->MakeSetDiscoveryFilterFail();
1665 // Queue two requests to start discovery session with filter.
1666 adapter_->StartDiscoverySessionWithFilter(
1667 discovery_filter.Pass(),
1668 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1669 base::Unretained(this)),
1670 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1671 base::Unretained(this)));
1673 adapter_->StartDiscoverySessionWithFilter(
1674 discovery_filter2.Pass(),
1675 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1676 base::Unretained(this)),
1677 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1678 base::Unretained(this)));
1680 message_loop_.Run();
1682 // First request to SetDiscoveryFilter should fail, resulting in no session
1683 // being created.
1684 EXPECT_EQ(0, callback_count_);
1685 EXPECT_EQ(1, error_callback_count_);
1686 error_callback_count_ = 0;
1688 ASSERT_TRUE(adapter_->IsPowered());
1689 ASSERT_FALSE(adapter_->IsDiscovering());
1690 ASSERT_EQ((size_t)0, discovery_sessions_.size());
1692 message_loop_.Run();
1694 // Second request should succeed
1695 EXPECT_EQ(1, callback_count_);
1696 EXPECT_EQ(0, error_callback_count_);
1697 callback_count_ = 0;
1699 ASSERT_TRUE(adapter_->IsDiscovering());
1700 ASSERT_EQ((size_t)1, discovery_sessions_.size());
1701 ASSERT_TRUE(discovery_sessions_[0]->IsActive());
1702 ASSERT_TRUE(df2->Equals(*discovery_sessions_[0]->GetDiscoveryFilter()));
1704 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1705 EXPECT_NE(nullptr, filter);
1706 EXPECT_EQ("bredr", *filter->transport);
1707 EXPECT_EQ(-65, *filter->rssi);
1708 EXPECT_EQ(nullptr, filter->pathloss.get());
1709 auto uuids = *filter->uuids;
1710 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1002"));
1712 discovery_sessions_[0]->Stop(
1713 base::Bind(&BluetoothChromeOSTest::Callback, base::Unretained(this)),
1714 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1715 base::Unretained(this)));
1717 message_loop_.Run();
1719 EXPECT_EQ(1, callback_count_);
1720 EXPECT_EQ(0, error_callback_count_);
1722 ASSERT_TRUE(adapter_->IsPowered());
1723 ASSERT_FALSE(adapter_->IsDiscovering());
1724 ASSERT_FALSE(discovery_sessions_[0]->IsActive());
1725 ASSERT_EQ(discovery_sessions_[0]->GetDiscoveryFilter(),
1726 (BluetoothDiscoveryFilter*)NULL);
1728 filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1729 EXPECT_EQ(nullptr, filter);
1732 TEST_F(BluetoothChromeOSTest, SetDiscoveryFilterAfterStartDiscovery) {
1733 // Test a simulated discovery session.
1734 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
1735 GetAdapter();
1737 TestObserver observer(adapter_);
1739 adapter_->SetPowered(true, base::Bind(&BluetoothChromeOSTest::Callback,
1740 base::Unretained(this)),
1741 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1742 base::Unretained(this)));
1743 adapter_->StartDiscoverySession(
1744 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1745 base::Unretained(this)),
1746 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1747 base::Unretained(this)));
1748 message_loop_.Run();
1749 EXPECT_EQ(2, callback_count_);
1750 EXPECT_EQ(0, error_callback_count_);
1751 callback_count_ = 0;
1753 ASSERT_TRUE(adapter_->IsPowered());
1754 ASSERT_TRUE(adapter_->IsDiscovering());
1755 ASSERT_EQ((size_t)1, discovery_sessions_.size());
1756 ASSERT_TRUE(discovery_sessions_[0]->IsActive());
1757 EXPECT_EQ(1, observer.discovering_changed_count_);
1758 observer.discovering_changed_count_ = 0;
1760 auto nullInstance = scoped_ptr<BluetoothDiscoveryFilter>();
1761 nullInstance.reset();
1762 ASSERT_EQ(discovery_sessions_[0]->GetDiscoveryFilter(), nullInstance.get());
1764 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1765 EXPECT_EQ(nullptr, filter);
1767 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter(
1768 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
1769 df->SetRSSI(-60);
1770 df->AddUUID(BluetoothUUID("1000"));
1771 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter(df);
1773 discovery_sessions_[0]->SetDiscoveryFilter(
1774 discovery_filter.Pass(),
1775 base::Bind(&BluetoothChromeOSTest::Callback, base::Unretained(this)),
1776 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1777 base::Unretained(this)));
1779 message_loop_.Run();
1780 EXPECT_EQ(1, callback_count_);
1781 EXPECT_EQ(0, error_callback_count_);
1782 callback_count_ = 0;
1784 ASSERT_TRUE(df->Equals(*discovery_sessions_[0]->GetDiscoveryFilter()));
1786 filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1787 EXPECT_NE(nullptr, filter);
1788 EXPECT_EQ("le", *filter->transport);
1789 EXPECT_EQ(-60, *filter->rssi);
1790 EXPECT_EQ(nullptr, filter->pathloss.get());
1791 std::vector<std::string> uuids = *filter->uuids;
1792 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1000"));
1794 discovery_sessions_[0]->Stop(
1795 base::Bind(&BluetoothChromeOSTest::Callback, base::Unretained(this)),
1796 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1797 base::Unretained(this)));
1799 message_loop_.Run();
1801 EXPECT_EQ(1, callback_count_);
1802 EXPECT_EQ(0, error_callback_count_);
1804 ASSERT_TRUE(adapter_->IsPowered());
1805 ASSERT_FALSE(adapter_->IsDiscovering());
1806 ASSERT_EQ((size_t)1, discovery_sessions_.size());
1807 ASSERT_FALSE(discovery_sessions_[0]->IsActive());
1808 ASSERT_EQ(discovery_sessions_[0]->GetDiscoveryFilter(),
1809 (BluetoothDiscoveryFilter*)NULL);
1811 filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1812 EXPECT_EQ(nullptr, filter);
1815 // This unit test asserts that the basic reference counting, and filter merging
1816 // works correctly for discovery requests done via the BluetoothAdapter.
1817 TEST_F(BluetoothChromeOSTest, SetDiscoveryFilterBeforeStartDiscoveryMultiple) {
1818 GetAdapter();
1819 adapter_->SetPowered(true, base::Bind(&BluetoothChromeOSTest::Callback,
1820 base::Unretained(this)),
1821 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1822 base::Unretained(this)));
1823 EXPECT_EQ(1, callback_count_);
1824 EXPECT_EQ(0, error_callback_count_);
1825 EXPECT_TRUE(adapter_->IsPowered());
1826 callback_count_ = 0;
1828 TestObserver observer(adapter_);
1830 // Request device discovery with pre-set filter 3 times.
1831 for (int i = 0; i < 3; i++) {
1832 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter;
1833 if (i == 0) {
1834 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter(
1835 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
1836 df->SetRSSI(-85);
1837 df->AddUUID(BluetoothUUID("1000"));
1838 discovery_filter.reset(df);
1839 } else if (i == 1) {
1840 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter(
1841 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
1842 df->SetRSSI(-60);
1843 df->AddUUID(BluetoothUUID("1020"));
1844 df->AddUUID(BluetoothUUID("1001"));
1845 discovery_filter.reset(df);
1846 } else if (i == 2) {
1847 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter(
1848 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
1849 df->SetRSSI(-65);
1850 df->AddUUID(BluetoothUUID("1020"));
1851 df->AddUUID(BluetoothUUID("1003"));
1852 discovery_filter.reset(df);
1855 adapter_->StartDiscoverySessionWithFilter(
1856 discovery_filter.Pass(),
1857 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1858 base::Unretained(this)),
1859 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1860 base::Unretained(this)));
1862 message_loop_.Run();
1864 if (i == 0) {
1865 EXPECT_EQ(1, observer.discovering_changed_count_);
1866 observer.discovering_changed_count_ = 0;
1868 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1869 EXPECT_EQ("le", *filter->transport);
1870 EXPECT_EQ(-85, *filter->rssi);
1871 EXPECT_EQ(nullptr, filter->pathloss.get());
1872 std::vector<std::string> uuids = *filter->uuids;
1873 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1000"));
1874 } else if (i == 1) {
1875 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1876 EXPECT_EQ("le", *filter->transport);
1877 EXPECT_EQ(-85, *filter->rssi);
1878 EXPECT_EQ(nullptr, filter->pathloss.get());
1879 std::vector<std::string> uuids = *filter->uuids;
1880 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1000"));
1881 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1001"));
1882 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1020"));
1883 } else if (i == 2) {
1884 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1885 EXPECT_EQ("le", *filter->transport);
1886 EXPECT_EQ(-85, *filter->rssi);
1887 EXPECT_EQ(nullptr, filter->pathloss.get());
1888 std::vector<std::string> uuids = *filter->uuids;
1889 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1000"));
1890 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1001"));
1891 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1003"));
1892 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1020"));
1896 // the success callback should have been called 3 times and the adapter should
1897 // be discovering.
1898 EXPECT_EQ(3, callback_count_);
1899 EXPECT_EQ(0, error_callback_count_);
1900 EXPECT_TRUE(adapter_->IsDiscovering());
1901 ASSERT_EQ((size_t)3, discovery_sessions_.size());
1903 callback_count_ = 0;
1904 // Request to stop discovery twice.
1905 for (int i = 0; i < 2; i++) {
1906 discovery_sessions_[i]->Stop(
1907 base::Bind(&BluetoothChromeOSTest::Callback, base::Unretained(this)),
1908 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1909 base::Unretained(this)));
1910 message_loop_.Run();
1912 if (i == 0) {
1913 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1914 EXPECT_EQ("le", *filter->transport);
1915 EXPECT_EQ(-65, *filter->rssi);
1916 EXPECT_EQ(nullptr, filter->pathloss.get());
1917 std::vector<std::string> uuids = *filter->uuids;
1918 EXPECT_EQ(3UL, uuids.size());
1919 EXPECT_EQ(uuids.end(), std::find(uuids.begin(), uuids.end(), "1000"));
1920 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1001"));
1921 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1003"));
1922 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1020"));
1923 } else if (i == 1) {
1924 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1925 EXPECT_EQ("le", *filter->transport);
1926 EXPECT_EQ(-65, *filter->rssi);
1927 EXPECT_EQ(nullptr, filter->pathloss.get());
1928 std::vector<std::string> uuids = *filter->uuids;
1929 EXPECT_EQ(2UL, uuids.size());
1930 EXPECT_EQ(uuids.end(), std::find(uuids.begin(), uuids.end(), "1000"));
1931 EXPECT_EQ(uuids.end(), std::find(uuids.begin(), uuids.end(), "1001"));
1932 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1003"));
1933 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1020"));
1934 } else if (i == 2) {
1935 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1936 EXPECT_EQ("le", *filter->transport);
1937 EXPECT_EQ(-65, *filter->rssi);
1938 EXPECT_EQ(nullptr, filter->pathloss.get());
1939 std::vector<std::string> uuids = *filter->uuids;
1940 EXPECT_EQ(0UL, uuids.size());
1944 // The success callback should have been called 2 times and the adapter should
1945 // still be discovering.
1946 EXPECT_EQ(2, callback_count_);
1947 EXPECT_EQ(0, error_callback_count_);
1948 EXPECT_TRUE(adapter_->IsDiscovering());
1949 EXPECT_FALSE(discovery_sessions_[0]->IsActive());
1950 EXPECT_FALSE(discovery_sessions_[1]->IsActive());
1951 EXPECT_TRUE(discovery_sessions_[2]->IsActive());
1953 callback_count_ = 0;
1955 // Request device discovery 3 times.
1956 for (int i = 0; i < 3; i++) {
1957 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter;
1959 if (i == 0) {
1960 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter(
1961 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
1962 df->SetRSSI(-85);
1963 df->AddUUID(BluetoothUUID("1000"));
1964 discovery_filter.reset(df);
1965 } else if (i == 1) {
1966 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter(
1967 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
1968 df->SetRSSI(-60);
1969 df->AddUUID(BluetoothUUID("1020"));
1970 df->AddUUID(BluetoothUUID("1001"));
1971 discovery_filter.reset(df);
1972 } else if (i == 2) {
1973 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter(
1974 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
1975 df->SetRSSI(-65);
1976 df->AddUUID(BluetoothUUID("1020"));
1977 df->AddUUID(BluetoothUUID("1003"));
1978 discovery_filter.reset(df);
1981 adapter_->StartDiscoverySessionWithFilter(
1982 discovery_filter.Pass(),
1983 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1984 base::Unretained(this)),
1985 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1986 base::Unretained(this)));
1988 // each result in 1 requests.
1989 message_loop_.Run();
1991 if (i == 0) {
1992 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1993 EXPECT_EQ("le", *filter->transport);
1994 EXPECT_EQ(-85, *filter->rssi);
1995 EXPECT_EQ(nullptr, filter->pathloss.get());
1996 std::vector<std::string> uuids = *filter->uuids;
1997 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1000"));
1998 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1003"));
1999 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1020"));
2000 } else if (i == 1 || i == 2) {
2001 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
2002 EXPECT_EQ("le", *filter->transport);
2003 EXPECT_EQ(-85, *filter->rssi);
2004 EXPECT_EQ(nullptr, filter->pathloss.get());
2005 std::vector<std::string> uuids = *filter->uuids;
2006 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1000"));
2007 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1001"));
2008 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1003"));
2009 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1020"));
2013 // The success callback should have been called 3 times and the adapter should
2014 // still be discovering.
2015 EXPECT_EQ(3, callback_count_);
2016 EXPECT_EQ(0, error_callback_count_);
2017 EXPECT_TRUE(adapter_->IsDiscovering());
2018 ASSERT_EQ((size_t)6, discovery_sessions_.size());
2020 callback_count_ = 0;
2021 // Request to stop discovery 4 times.
2022 for (int i = 2; i < 6; i++) {
2023 discovery_sessions_[i]->Stop(
2024 base::Bind(&BluetoothChromeOSTest::Callback, base::Unretained(this)),
2025 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
2026 base::Unretained(this)));
2028 // filter no 2 is same as filter no 5, so removing it shouldn't cause any
2029 // filter update
2030 if (i != 2 && i != 5)
2031 message_loop_.Run();
2033 // Run only once, as there should have been one D-Bus call.
2034 message_loop_.Run();
2036 // The success callback should have been called 4 times and the adapter should
2037 // no longer be discovering.
2038 EXPECT_EQ(4, callback_count_);
2039 EXPECT_EQ(0, error_callback_count_);
2040 EXPECT_FALSE(adapter_->IsDiscovering());
2041 EXPECT_EQ(1, observer.discovering_changed_count_);
2042 observer.discovering_changed_count_ = 0;
2044 // All discovery sessions should be inactive.
2045 for (int i = 0; i < 6; i++)
2046 EXPECT_FALSE(discovery_sessions_[i]->IsActive());
2048 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
2049 EXPECT_EQ(nullptr, filter);
2052 // This unit test asserts that filter merging logic works correctly for filtered
2053 // discovery requests done via the BluetoothAdapter.
2054 TEST_F(BluetoothChromeOSTest, SetDiscoveryFilterMergingTest) {
2055 GetAdapter();
2056 adapter_->SetPowered(true, base::Bind(&BluetoothChromeOSTest::Callback,
2057 base::Unretained(this)),
2058 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
2059 base::Unretained(this)));
2061 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter(
2062 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
2063 df->SetRSSI(-15);
2064 df->AddUUID(BluetoothUUID("1000"));
2065 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter(df);
2067 adapter_->StartDiscoverySessionWithFilter(
2068 discovery_filter.Pass(),
2069 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
2070 base::Unretained(this)),
2071 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
2072 base::Unretained(this)));
2074 message_loop_.Run();
2076 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
2077 EXPECT_EQ("le", *filter->transport);
2078 EXPECT_EQ(-15, *filter->rssi);
2079 EXPECT_EQ(nullptr, filter->pathloss.get());
2080 std::vector<std::string> uuids = *filter->uuids;
2081 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1000"));
2083 df = new BluetoothDiscoveryFilter(
2084 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
2085 df->SetRSSI(-60);
2086 df->AddUUID(BluetoothUUID("1020"));
2087 df->AddUUID(BluetoothUUID("1001"));
2088 discovery_filter = scoped_ptr<BluetoothDiscoveryFilter>(df);
2090 adapter_->StartDiscoverySessionWithFilter(
2091 discovery_filter.Pass(),
2092 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
2093 base::Unretained(this)),
2094 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
2095 base::Unretained(this)));
2097 message_loop_.Run();
2099 filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
2100 EXPECT_EQ("le", *filter->transport);
2101 EXPECT_EQ(-60, *filter->rssi);
2102 EXPECT_EQ(nullptr, filter->pathloss.get());
2103 uuids = *filter->uuids;
2104 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1000"));
2105 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1001"));
2106 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1020"));
2108 BluetoothDiscoveryFilter* df3 = new BluetoothDiscoveryFilter(
2109 BluetoothDiscoveryFilter::Transport::TRANSPORT_CLASSIC);
2110 df3->SetRSSI(-65);
2111 df3->AddUUID(BluetoothUUID("1020"));
2112 df3->AddUUID(BluetoothUUID("1003"));
2113 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter3(df3);
2115 adapter_->StartDiscoverySessionWithFilter(
2116 discovery_filter3.Pass(),
2117 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
2118 base::Unretained(this)),
2119 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
2120 base::Unretained(this)));
2122 message_loop_.Run();
2124 filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
2125 EXPECT_EQ("auto", *filter->transport);
2126 EXPECT_EQ(-65, *filter->rssi);
2127 EXPECT_EQ(nullptr, filter->pathloss.get());
2128 uuids = *filter->uuids;
2129 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1000"));
2130 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1001"));
2131 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1003"));
2132 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1020"));
2134 // start additionally classic scan
2135 adapter_->StartDiscoverySession(
2136 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
2137 base::Unretained(this)),
2138 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
2139 base::Unretained(this)));
2141 message_loop_.Run();
2143 filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
2144 EXPECT_EQ("auto", *filter->transport);
2145 EXPECT_EQ(nullptr, filter->rssi.get());
2146 EXPECT_EQ(nullptr, filter->pathloss.get());
2147 EXPECT_EQ(nullptr, filter->uuids.get());
2149 // Request to stop discovery 4 times.
2150 for (int i = 3; i >= 0; i--) {
2151 discovery_sessions_[i]->Stop(
2152 base::Bind(&BluetoothChromeOSTest::Callback, base::Unretained(this)),
2153 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
2154 base::Unretained(this)));
2156 // Every session stopping would trigger filter update
2157 message_loop_.Run();
2161 TEST_F(BluetoothChromeOSTest, DeviceProperties) {
2162 GetAdapter();
2164 BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
2165 ASSERT_EQ(2U, devices.size());
2166 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
2167 devices[0]->GetAddress());
2169 // Verify the other device properties.
2170 EXPECT_EQ(base::UTF8ToUTF16(FakeBluetoothDeviceClient::kPairedDeviceName),
2171 devices[0]->GetName());
2172 EXPECT_EQ(BluetoothDevice::DEVICE_COMPUTER, devices[0]->GetDeviceType());
2173 EXPECT_TRUE(devices[0]->IsPaired());
2174 EXPECT_FALSE(devices[0]->IsConnected());
2175 EXPECT_FALSE(devices[0]->IsConnecting());
2177 // Non HID devices are always connectable.
2178 EXPECT_TRUE(devices[0]->IsConnectable());
2180 BluetoothDevice::UUIDList uuids = devices[0]->GetUUIDs();
2181 ASSERT_EQ(2U, uuids.size());
2182 EXPECT_EQ(uuids[0], BluetoothUUID("1800"));
2183 EXPECT_EQ(uuids[1], BluetoothUUID("1801"));
2185 EXPECT_EQ(BluetoothDevice::VENDOR_ID_USB, devices[0]->GetVendorIDSource());
2186 EXPECT_EQ(0x05ac, devices[0]->GetVendorID());
2187 EXPECT_EQ(0x030d, devices[0]->GetProductID());
2188 EXPECT_EQ(0x0306, devices[0]->GetDeviceID());
2191 TEST_F(BluetoothChromeOSTest, DeviceClassChanged) {
2192 // Simulate a change of class of a device, as sometimes occurs
2193 // during discovery.
2194 GetAdapter();
2196 BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
2197 ASSERT_EQ(2U, devices.size());
2198 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
2199 devices[0]->GetAddress());
2200 ASSERT_EQ(BluetoothDevice::DEVICE_COMPUTER, devices[0]->GetDeviceType());
2202 // Install an observer; expect the DeviceChanged method to be called when
2203 // we change the class of the device.
2204 TestObserver observer(adapter_);
2206 FakeBluetoothDeviceClient::Properties* properties =
2207 fake_bluetooth_device_client_->GetProperties(
2208 dbus::ObjectPath(FakeBluetoothDeviceClient::kPairedDevicePath));
2210 properties->bluetooth_class.ReplaceValue(0x002580);
2212 EXPECT_EQ(1, observer.device_changed_count_);
2213 EXPECT_EQ(devices[0], observer.last_device_);
2215 EXPECT_EQ(BluetoothDevice::DEVICE_MOUSE, devices[0]->GetDeviceType());
2218 TEST_F(BluetoothChromeOSTest, DeviceNameChanged) {
2219 // Simulate a change of name of a device.
2220 GetAdapter();
2222 BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
2223 ASSERT_EQ(2U, devices.size());
2224 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
2225 devices[0]->GetAddress());
2226 ASSERT_EQ(base::UTF8ToUTF16(FakeBluetoothDeviceClient::kPairedDeviceName),
2227 devices[0]->GetName());
2229 // Install an observer; expect the DeviceChanged method to be called when
2230 // we change the alias of the device.
2231 TestObserver observer(adapter_);
2233 FakeBluetoothDeviceClient::Properties* properties =
2234 fake_bluetooth_device_client_->GetProperties(
2235 dbus::ObjectPath(FakeBluetoothDeviceClient::kPairedDevicePath));
2237 static const std::string new_name("New Device Name");
2238 properties->alias.ReplaceValue(new_name);
2240 EXPECT_EQ(1, observer.device_changed_count_);
2241 EXPECT_EQ(devices[0], observer.last_device_);
2243 EXPECT_EQ(base::UTF8ToUTF16(new_name), devices[0]->GetName());
2246 TEST_F(BluetoothChromeOSTest, DeviceUuidsChanged) {
2247 // Simulate a change of advertised services of a device.
2248 GetAdapter();
2250 BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
2251 ASSERT_EQ(2U, devices.size());
2252 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
2253 devices[0]->GetAddress());
2255 BluetoothDevice::UUIDList uuids = devices[0]->GetUUIDs();
2256 ASSERT_EQ(2U, uuids.size());
2257 ASSERT_EQ(uuids[0], BluetoothUUID("1800"));
2258 ASSERT_EQ(uuids[1], BluetoothUUID("1801"));
2260 // Install an observer; expect the DeviceChanged method to be called when
2261 // we change the class of the device.
2262 TestObserver observer(adapter_);
2264 FakeBluetoothDeviceClient::Properties* properties =
2265 fake_bluetooth_device_client_->GetProperties(
2266 dbus::ObjectPath(FakeBluetoothDeviceClient::kPairedDevicePath));
2268 std::vector<std::string> new_uuids;
2269 new_uuids.push_back(uuids[0].canonical_value());
2270 new_uuids.push_back(uuids[1].canonical_value());
2271 new_uuids.push_back("0000110c-0000-1000-8000-00805f9b34fb");
2272 new_uuids.push_back("0000110e-0000-1000-8000-00805f9b34fb");
2273 new_uuids.push_back("0000110a-0000-1000-8000-00805f9b34fb");
2275 properties->uuids.ReplaceValue(new_uuids);
2277 EXPECT_EQ(1, observer.device_changed_count_);
2278 EXPECT_EQ(devices[0], observer.last_device_);
2280 // Fetching the value should give the new one.
2281 uuids = devices[0]->GetUUIDs();
2282 ASSERT_EQ(5U, uuids.size());
2283 EXPECT_EQ(uuids[0], BluetoothUUID("1800"));
2284 EXPECT_EQ(uuids[1], BluetoothUUID("1801"));
2285 EXPECT_EQ(uuids[2], BluetoothUUID("110c"));
2286 EXPECT_EQ(uuids[3], BluetoothUUID("110e"));
2287 EXPECT_EQ(uuids[4], BluetoothUUID("110a"));
2290 TEST_F(BluetoothChromeOSTest, ForgetDevice) {
2291 GetAdapter();
2293 BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
2294 ASSERT_EQ(2U, devices.size());
2295 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
2296 devices[0]->GetAddress());
2298 std::string address = devices[0]->GetAddress();
2300 // Install an observer; expect the DeviceRemoved method to be called
2301 // with the device we remove.
2302 TestObserver observer(adapter_);
2304 devices[0]->Forget(GetErrorCallback());
2305 EXPECT_EQ(0, error_callback_count_);
2307 EXPECT_EQ(1, observer.device_removed_count_);
2308 EXPECT_EQ(address, observer.last_device_address_);
2310 // GetDevices shouldn't return the device either.
2311 devices = adapter_->GetDevices();
2312 ASSERT_EQ(1U, devices.size());
2315 TEST_F(BluetoothChromeOSTest, ForgetUnpairedDevice) {
2316 GetAdapter();
2317 DiscoverDevices();
2319 BluetoothDevice* device = adapter_->GetDevice(
2320 FakeBluetoothDeviceClient::kConnectUnpairableAddress);
2321 ASSERT_TRUE(device != NULL);
2322 ASSERT_FALSE(device->IsPaired());
2324 // Connect the device so it becomes trusted and remembered.
2325 device->Connect(NULL, GetCallback(),
2326 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2327 base::Unretained(this)));
2329 ASSERT_EQ(1, callback_count_);
2330 ASSERT_EQ(0, error_callback_count_);
2331 callback_count_ = 0;
2333 ASSERT_TRUE(device->IsConnected());
2334 ASSERT_FALSE(device->IsConnecting());
2336 // Make sure the trusted property has been set to true.
2337 FakeBluetoothDeviceClient::Properties* properties =
2338 fake_bluetooth_device_client_->GetProperties(
2339 dbus::ObjectPath(FakeBluetoothDeviceClient::kConnectUnpairablePath));
2340 ASSERT_TRUE(properties->trusted.value());
2342 // Install an observer; expect the DeviceRemoved method to be called
2343 // with the device we remove.
2344 TestObserver observer(adapter_);
2346 device->Forget(GetErrorCallback());
2347 EXPECT_EQ(0, error_callback_count_);
2349 EXPECT_EQ(1, observer.device_removed_count_);
2350 EXPECT_EQ(FakeBluetoothDeviceClient::kConnectUnpairableAddress,
2351 observer.last_device_address_);
2353 // GetDevices shouldn't return the device either.
2354 device = adapter_->GetDevice(
2355 FakeBluetoothDeviceClient::kConnectUnpairableAddress);
2356 EXPECT_FALSE(device != NULL);
2359 TEST_F(BluetoothChromeOSTest, ConnectPairedDevice) {
2360 GetAdapter();
2362 BluetoothDevice* device = adapter_->GetDevice(
2363 FakeBluetoothDeviceClient::kPairedDeviceAddress);
2364 ASSERT_TRUE(device != NULL);
2365 ASSERT_TRUE(device->IsPaired());
2367 TestObserver observer(adapter_);
2369 // Connect without a pairing delegate; since the device is already Paired
2370 // this should succeed and the device should become connected.
2371 device->Connect(NULL, GetCallback(),
2372 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2373 base::Unretained(this)));
2375 EXPECT_EQ(1, callback_count_);
2376 EXPECT_EQ(0, error_callback_count_);
2378 // Two changes for connecting, one for connected and one for for trusted
2379 // after connecting.
2380 EXPECT_EQ(4, observer.device_changed_count_);
2381 EXPECT_EQ(device, observer.last_device_);
2383 EXPECT_TRUE(device->IsConnected());
2384 EXPECT_FALSE(device->IsConnecting());
2387 TEST_F(BluetoothChromeOSTest, ConnectUnpairableDevice) {
2388 GetAdapter();
2389 DiscoverDevices();
2391 BluetoothDevice* device = adapter_->GetDevice(
2392 FakeBluetoothDeviceClient::kConnectUnpairableAddress);
2393 ASSERT_TRUE(device != NULL);
2394 ASSERT_FALSE(device->IsPaired());
2396 TestObserver observer(adapter_);
2398 // Connect without a pairing delegate; since the device does not require
2399 // pairing, this should succeed and the device should become connected.
2400 device->Connect(NULL, GetCallback(),
2401 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2402 base::Unretained(this)));
2404 EXPECT_EQ(1, callback_count_);
2405 EXPECT_EQ(0, error_callback_count_);
2407 // Two changes for connecting, one for connected, one for for trusted after
2408 // connection, and one for the reconnect mode (IsConnectable).
2409 EXPECT_EQ(5, observer.device_changed_count_);
2410 EXPECT_EQ(device, observer.last_device_);
2412 EXPECT_TRUE(device->IsConnected());
2413 EXPECT_FALSE(device->IsConnecting());
2415 // Make sure the trusted property has been set to true.
2416 FakeBluetoothDeviceClient::Properties* properties =
2417 fake_bluetooth_device_client_->GetProperties(
2418 dbus::ObjectPath(FakeBluetoothDeviceClient::kConnectUnpairablePath));
2419 EXPECT_TRUE(properties->trusted.value());
2421 // Verify is a HID device and is not connectable.
2422 BluetoothDevice::UUIDList uuids = device->GetUUIDs();
2423 ASSERT_EQ(1U, uuids.size());
2424 EXPECT_EQ(uuids[0], BluetoothUUID("1124"));
2425 EXPECT_FALSE(device->IsConnectable());
2428 TEST_F(BluetoothChromeOSTest, ConnectConnectedDevice) {
2429 GetAdapter();
2431 BluetoothDevice* device = adapter_->GetDevice(
2432 FakeBluetoothDeviceClient::kPairedDeviceAddress);
2433 ASSERT_TRUE(device != NULL);
2434 ASSERT_TRUE(device->IsPaired());
2436 device->Connect(NULL, GetCallback(),
2437 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2438 base::Unretained(this)));
2440 ASSERT_EQ(1, callback_count_);
2441 ASSERT_EQ(0, error_callback_count_);
2442 callback_count_ = 0;
2444 ASSERT_TRUE(device->IsConnected());
2446 // Connect again; since the device is already Connected, this shouldn't do
2447 // anything to initiate the connection.
2448 TestObserver observer(adapter_);
2450 device->Connect(NULL, GetCallback(),
2451 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2452 base::Unretained(this)));
2454 EXPECT_EQ(1, callback_count_);
2455 EXPECT_EQ(0, error_callback_count_);
2457 // The observer will be called because Connecting will toggle true and false,
2458 // and the trusted property will be updated to true.
2459 EXPECT_EQ(3, observer.device_changed_count_);
2461 EXPECT_TRUE(device->IsConnected());
2462 EXPECT_FALSE(device->IsConnecting());
2465 TEST_F(BluetoothChromeOSTest, ConnectDeviceFails) {
2466 GetAdapter();
2467 DiscoverDevices();
2469 BluetoothDevice* device = adapter_->GetDevice(
2470 FakeBluetoothDeviceClient::kLegacyAutopairAddress);
2471 ASSERT_TRUE(device != NULL);
2472 ASSERT_FALSE(device->IsPaired());
2474 TestObserver observer(adapter_);
2476 // Connect without a pairing delegate; since the device requires pairing,
2477 // this should fail with an error.
2478 device->Connect(NULL, GetCallback(),
2479 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2480 base::Unretained(this)));
2482 EXPECT_EQ(0, callback_count_);
2483 EXPECT_EQ(1, error_callback_count_);
2484 EXPECT_EQ(BluetoothDevice::ERROR_FAILED, last_connect_error_);
2486 EXPECT_EQ(2, observer.device_changed_count_);
2488 EXPECT_FALSE(device->IsConnected());
2489 EXPECT_FALSE(device->IsConnecting());
2492 TEST_F(BluetoothChromeOSTest, DisconnectDevice) {
2493 GetAdapter();
2495 BluetoothDevice* device = adapter_->GetDevice(
2496 FakeBluetoothDeviceClient::kPairedDeviceAddress);
2497 ASSERT_TRUE(device != NULL);
2498 ASSERT_TRUE(device->IsPaired());
2500 device->Connect(NULL, GetCallback(),
2501 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2502 base::Unretained(this)));
2504 ASSERT_EQ(1, callback_count_);
2505 ASSERT_EQ(0, error_callback_count_);
2506 callback_count_ = 0;
2508 ASSERT_TRUE(device->IsConnected());
2509 ASSERT_FALSE(device->IsConnecting());
2511 // Disconnect the device, we should see the observer method fire and the
2512 // device get dropped.
2513 TestObserver observer(adapter_);
2515 device->Disconnect(GetCallback(), GetErrorCallback());
2517 EXPECT_EQ(1, callback_count_);
2518 EXPECT_EQ(0, error_callback_count_);
2520 EXPECT_EQ(1, observer.device_changed_count_);
2521 EXPECT_EQ(device, observer.last_device_);
2523 EXPECT_FALSE(device->IsConnected());
2526 TEST_F(BluetoothChromeOSTest, DisconnectUnconnectedDevice) {
2527 GetAdapter();
2529 BluetoothDevice* device = adapter_->GetDevice(
2530 FakeBluetoothDeviceClient::kPairedDeviceAddress);
2531 ASSERT_TRUE(device != NULL);
2532 ASSERT_TRUE(device->IsPaired());
2533 ASSERT_FALSE(device->IsConnected());
2535 // Disconnect the device, we should see the observer method fire and the
2536 // device get dropped.
2537 TestObserver observer(adapter_);
2539 device->Disconnect(GetCallback(), GetErrorCallback());
2541 EXPECT_EQ(0, callback_count_);
2542 EXPECT_EQ(1, error_callback_count_);
2544 EXPECT_EQ(0, observer.device_changed_count_);
2546 EXPECT_FALSE(device->IsConnected());
2549 TEST_F(BluetoothChromeOSTest, PairLegacyAutopair) {
2550 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2552 GetAdapter();
2553 DiscoverDevices();
2555 // The Legacy Autopair device requires no PIN or Passkey to pair because
2556 // the daemon provides 0000 to the device for us.
2557 BluetoothDevice* device = adapter_->GetDevice(
2558 FakeBluetoothDeviceClient::kLegacyAutopairAddress);
2559 ASSERT_TRUE(device != NULL);
2560 ASSERT_FALSE(device->IsPaired());
2562 TestObserver observer(adapter_);
2564 TestPairingDelegate pairing_delegate;
2565 device->Connect(&pairing_delegate, GetCallback(),
2566 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2567 base::Unretained(this)));
2569 EXPECT_EQ(0, pairing_delegate.call_count_);
2570 EXPECT_TRUE(device->IsConnecting());
2572 message_loop_.Run();
2574 EXPECT_EQ(1, callback_count_);
2575 EXPECT_EQ(0, error_callback_count_);
2577 // Two changes for connecting, one change for connected, one for paired,
2578 // two for trusted (after pairing and connection), and one for the reconnect
2579 // mode (IsConnectable).
2580 EXPECT_EQ(7, observer.device_changed_count_);
2581 EXPECT_EQ(device, observer.last_device_);
2583 EXPECT_TRUE(device->IsConnected());
2584 EXPECT_FALSE(device->IsConnecting());
2586 EXPECT_TRUE(device->IsPaired());
2588 // Verify is a HID device and is connectable.
2589 BluetoothDevice::UUIDList uuids = device->GetUUIDs();
2590 ASSERT_EQ(1U, uuids.size());
2591 EXPECT_EQ(uuids[0], BluetoothUUID("1124"));
2592 EXPECT_TRUE(device->IsConnectable());
2594 // Make sure the trusted property has been set to true.
2595 FakeBluetoothDeviceClient::Properties* properties =
2596 fake_bluetooth_device_client_->GetProperties(
2597 dbus::ObjectPath(FakeBluetoothDeviceClient::kLegacyAutopairPath));
2598 EXPECT_TRUE(properties->trusted.value());
2601 TEST_F(BluetoothChromeOSTest, PairDisplayPinCode) {
2602 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2604 GetAdapter();
2605 DiscoverDevices();
2607 // Requires that we display a randomly generated PIN on the screen.
2608 BluetoothDevice* device = adapter_->GetDevice(
2609 FakeBluetoothDeviceClient::kDisplayPinCodeAddress);
2610 ASSERT_TRUE(device != NULL);
2611 ASSERT_FALSE(device->IsPaired());
2613 TestObserver observer(adapter_);
2615 TestPairingDelegate pairing_delegate;
2616 device->Connect(&pairing_delegate, GetCallback(),
2617 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2618 base::Unretained(this)));
2620 EXPECT_EQ(1, pairing_delegate.call_count_);
2621 EXPECT_EQ(1, pairing_delegate.display_pincode_count_);
2622 EXPECT_EQ("123456", pairing_delegate.last_pincode_);
2623 EXPECT_TRUE(device->IsConnecting());
2625 message_loop_.Run();
2627 EXPECT_EQ(1, callback_count_);
2628 EXPECT_EQ(0, error_callback_count_);
2630 // Two changes for connecting, one change for connected, one for paired,
2631 // two for trusted (after pairing and connection), and one for the reconnect
2632 // mode (IsConnectable).
2633 EXPECT_EQ(7, observer.device_changed_count_);
2634 EXPECT_EQ(device, observer.last_device_);
2636 EXPECT_TRUE(device->IsConnected());
2637 EXPECT_FALSE(device->IsConnecting());
2639 EXPECT_TRUE(device->IsPaired());
2641 // Verify is a HID device and is connectable.
2642 BluetoothDevice::UUIDList uuids = device->GetUUIDs();
2643 ASSERT_EQ(1U, uuids.size());
2644 EXPECT_EQ(uuids[0], BluetoothUUID("1124"));
2645 EXPECT_TRUE(device->IsConnectable());
2647 // Make sure the trusted property has been set to true.
2648 FakeBluetoothDeviceClient::Properties* properties =
2649 fake_bluetooth_device_client_->GetProperties(
2650 dbus::ObjectPath(FakeBluetoothDeviceClient::kDisplayPinCodePath));
2651 EXPECT_TRUE(properties->trusted.value());
2654 TEST_F(BluetoothChromeOSTest, PairDisplayPasskey) {
2655 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2657 GetAdapter();
2658 DiscoverDevices();
2660 // Requires that we display a randomly generated Passkey on the screen,
2661 // and notifies us as it's typed in.
2662 BluetoothDevice* device = adapter_->GetDevice(
2663 FakeBluetoothDeviceClient::kDisplayPasskeyAddress);
2664 ASSERT_TRUE(device != NULL);
2665 ASSERT_FALSE(device->IsPaired());
2667 TestObserver observer(adapter_);
2669 TestPairingDelegate pairing_delegate;
2670 device->Connect(&pairing_delegate, GetCallback(),
2671 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2672 base::Unretained(this)));
2674 // One call for DisplayPasskey() and one for KeysEntered().
2675 EXPECT_EQ(2, pairing_delegate.call_count_);
2676 EXPECT_EQ(1, pairing_delegate.display_passkey_count_);
2677 EXPECT_EQ(123456U, pairing_delegate.last_passkey_);
2678 EXPECT_EQ(1, pairing_delegate.keys_entered_count_);
2679 EXPECT_EQ(0U, pairing_delegate.last_entered_);
2681 EXPECT_TRUE(device->IsConnecting());
2683 // One call to KeysEntered() for each key, including [enter].
2684 for(int i = 1; i <= 7; ++i) {
2685 message_loop_.Run();
2687 EXPECT_EQ(2 + i, pairing_delegate.call_count_);
2688 EXPECT_EQ(1 + i, pairing_delegate.keys_entered_count_);
2689 EXPECT_EQ(static_cast<uint32_t>(i), pairing_delegate.last_entered_);
2692 message_loop_.Run();
2694 // 8 KeysEntered notifications (0 to 7, inclusive) and one aditional call for
2695 // DisplayPasskey().
2696 EXPECT_EQ(9, pairing_delegate.call_count_);
2697 EXPECT_EQ(8, pairing_delegate.keys_entered_count_);
2698 EXPECT_EQ(7U, pairing_delegate.last_entered_);
2700 EXPECT_EQ(1, callback_count_);
2701 EXPECT_EQ(0, error_callback_count_);
2703 // Two changes for connecting, one change for connected, one for paired,
2704 // two for trusted (after pairing and connection), and one for the reconnect
2705 // mode (IsConnectable).
2706 EXPECT_EQ(7, observer.device_changed_count_);
2707 EXPECT_EQ(device, observer.last_device_);
2709 EXPECT_TRUE(device->IsConnected());
2710 EXPECT_FALSE(device->IsConnecting());
2712 EXPECT_TRUE(device->IsPaired());
2714 // Verify is a HID device.
2715 BluetoothDevice::UUIDList uuids = device->GetUUIDs();
2716 ASSERT_EQ(1U, uuids.size());
2717 EXPECT_EQ(uuids[0], BluetoothUUID("1124"));
2719 // And usually not connectable.
2720 EXPECT_FALSE(device->IsConnectable());
2722 // Make sure the trusted property has been set to true.
2723 FakeBluetoothDeviceClient::Properties* properties =
2724 fake_bluetooth_device_client_->GetProperties(
2725 dbus::ObjectPath(FakeBluetoothDeviceClient::kDisplayPasskeyPath));
2726 EXPECT_TRUE(properties->trusted.value());
2729 TEST_F(BluetoothChromeOSTest, PairRequestPinCode) {
2730 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2732 GetAdapter();
2733 DiscoverDevices();
2735 // Requires that the user enters a PIN for them.
2736 BluetoothDevice* device = adapter_->GetDevice(
2737 FakeBluetoothDeviceClient::kRequestPinCodeAddress);
2738 ASSERT_TRUE(device != NULL);
2739 ASSERT_FALSE(device->IsPaired());
2741 TestObserver observer(adapter_);
2743 TestPairingDelegate pairing_delegate;
2744 device->Connect(&pairing_delegate, GetCallback(),
2745 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2746 base::Unretained(this)));
2748 EXPECT_EQ(1, pairing_delegate.call_count_);
2749 EXPECT_EQ(1, pairing_delegate.request_pincode_count_);
2750 EXPECT_TRUE(device->IsConnecting());
2752 // Set the PIN.
2753 device->SetPinCode("1234");
2754 message_loop_.Run();
2756 EXPECT_EQ(1, callback_count_);
2757 EXPECT_EQ(0, error_callback_count_);
2759 // Two changes for connecting, one change for connected, one for paired and
2760 // two for trusted (after pairing and connection).
2761 EXPECT_EQ(6, observer.device_changed_count_);
2762 EXPECT_EQ(device, observer.last_device_);
2764 EXPECT_TRUE(device->IsConnected());
2765 EXPECT_FALSE(device->IsConnecting());
2767 EXPECT_TRUE(device->IsPaired());
2769 // Verify is not a HID device.
2770 BluetoothDevice::UUIDList uuids = device->GetUUIDs();
2771 ASSERT_EQ(0U, uuids.size());
2773 // Non HID devices are always connectable.
2774 EXPECT_TRUE(device->IsConnectable());
2776 // Make sure the trusted property has been set to true.
2777 FakeBluetoothDeviceClient::Properties* properties =
2778 fake_bluetooth_device_client_->GetProperties(
2779 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath));
2780 EXPECT_TRUE(properties->trusted.value());
2783 TEST_F(BluetoothChromeOSTest, PairConfirmPasskey) {
2784 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2786 GetAdapter();
2787 DiscoverDevices();
2789 // Requests that we confirm a displayed passkey.
2790 BluetoothDevice* device = adapter_->GetDevice(
2791 FakeBluetoothDeviceClient::kConfirmPasskeyAddress);
2792 ASSERT_TRUE(device != NULL);
2793 ASSERT_FALSE(device->IsPaired());
2795 TestObserver observer(adapter_);
2797 TestPairingDelegate pairing_delegate;
2798 device->Connect(&pairing_delegate, GetCallback(),
2799 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2800 base::Unretained(this)));
2802 EXPECT_EQ(1, pairing_delegate.call_count_);
2803 EXPECT_EQ(1, pairing_delegate.confirm_passkey_count_);
2804 EXPECT_EQ(123456U, pairing_delegate.last_passkey_);
2805 EXPECT_TRUE(device->IsConnecting());
2807 // Confirm the passkey.
2808 device->ConfirmPairing();
2809 message_loop_.Run();
2811 EXPECT_EQ(1, callback_count_);
2812 EXPECT_EQ(0, error_callback_count_);
2814 // Two changes for connecting, one change for connected, one for paired and
2815 // two for trusted (after pairing and connection).
2816 EXPECT_EQ(6, observer.device_changed_count_);
2817 EXPECT_EQ(device, observer.last_device_);
2819 EXPECT_TRUE(device->IsConnected());
2820 EXPECT_FALSE(device->IsConnecting());
2822 EXPECT_TRUE(device->IsPaired());
2824 // Non HID devices are always connectable.
2825 EXPECT_TRUE(device->IsConnectable());
2827 // Make sure the trusted property has been set to true.
2828 FakeBluetoothDeviceClient::Properties* properties =
2829 fake_bluetooth_device_client_->GetProperties(
2830 dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath));
2831 EXPECT_TRUE(properties->trusted.value());
2834 TEST_F(BluetoothChromeOSTest, PairRequestPasskey) {
2835 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2837 GetAdapter();
2838 DiscoverDevices();
2840 // Requires that the user enters a Passkey, this would be some kind of
2841 // device that has a display, but doesn't use "just works" - maybe a car?
2842 BluetoothDevice* device = adapter_->GetDevice(
2843 FakeBluetoothDeviceClient::kRequestPasskeyAddress);
2844 ASSERT_TRUE(device != NULL);
2845 ASSERT_FALSE(device->IsPaired());
2847 TestObserver observer(adapter_);
2849 TestPairingDelegate pairing_delegate;
2850 device->Connect(&pairing_delegate, GetCallback(),
2851 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2852 base::Unretained(this)));
2854 EXPECT_EQ(1, pairing_delegate.call_count_);
2855 EXPECT_EQ(1, pairing_delegate.request_passkey_count_);
2856 EXPECT_TRUE(device->IsConnecting());
2858 // Set the Passkey.
2859 device->SetPasskey(1234);
2860 message_loop_.Run();
2862 EXPECT_EQ(1, callback_count_);
2863 EXPECT_EQ(0, error_callback_count_);
2865 // Two changes for connecting, one change for connected, one for paired and
2866 // two for trusted (after pairing and connection).
2867 EXPECT_EQ(6, observer.device_changed_count_);
2868 EXPECT_EQ(device, observer.last_device_);
2870 EXPECT_TRUE(device->IsConnected());
2871 EXPECT_FALSE(device->IsConnecting());
2873 EXPECT_TRUE(device->IsPaired());
2875 // Non HID devices are always connectable.
2876 EXPECT_TRUE(device->IsConnectable());
2878 // Make sure the trusted property has been set to true.
2879 FakeBluetoothDeviceClient::Properties* properties =
2880 fake_bluetooth_device_client_->GetProperties(
2881 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath));
2882 EXPECT_TRUE(properties->trusted.value());
2885 TEST_F(BluetoothChromeOSTest, PairJustWorks) {
2886 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2888 GetAdapter();
2889 DiscoverDevices();
2891 // Uses just-works pairing, since this is an outgoing pairing, no delegate
2892 // interaction is required.
2893 BluetoothDevice* device = adapter_->GetDevice(
2894 FakeBluetoothDeviceClient::kJustWorksAddress);
2895 ASSERT_TRUE(device != NULL);
2896 ASSERT_FALSE(device->IsPaired());
2898 TestObserver observer(adapter_);
2900 TestPairingDelegate pairing_delegate;
2901 device->Connect(&pairing_delegate, GetCallback(),
2902 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2903 base::Unretained(this)));
2905 EXPECT_EQ(0, pairing_delegate.call_count_);
2907 message_loop_.Run();
2909 EXPECT_EQ(1, callback_count_);
2910 EXPECT_EQ(0, error_callback_count_);
2912 // Two changes for connecting, one change for connected, one for paired and
2913 // two for trusted (after pairing and connection).
2914 EXPECT_EQ(6, observer.device_changed_count_);
2915 EXPECT_EQ(device, observer.last_device_);
2917 EXPECT_TRUE(device->IsConnected());
2918 EXPECT_FALSE(device->IsConnecting());
2920 EXPECT_TRUE(device->IsPaired());
2922 // Non HID devices are always connectable.
2923 EXPECT_TRUE(device->IsConnectable());
2925 // Make sure the trusted property has been set to true.
2926 FakeBluetoothDeviceClient::Properties* properties =
2927 fake_bluetooth_device_client_->GetProperties(
2928 dbus::ObjectPath(FakeBluetoothDeviceClient::kJustWorksPath));
2929 EXPECT_TRUE(properties->trusted.value());
2932 TEST_F(BluetoothChromeOSTest, PairUnpairableDeviceFails) {
2933 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2935 GetAdapter();
2936 DiscoverDevice(FakeBluetoothDeviceClient::kUnconnectableDeviceAddress);
2938 BluetoothDevice* device = adapter_->GetDevice(
2939 FakeBluetoothDeviceClient::kUnpairableDeviceAddress);
2940 ASSERT_TRUE(device != NULL);
2941 ASSERT_FALSE(device->IsPaired());
2943 TestObserver observer(adapter_);
2945 TestPairingDelegate pairing_delegate;
2946 device->Connect(&pairing_delegate, GetCallback(),
2947 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2948 base::Unretained(this)));
2950 EXPECT_EQ(0, pairing_delegate.call_count_);
2951 EXPECT_TRUE(device->IsConnecting());
2953 // Run the loop to get the error..
2954 message_loop_.Run();
2956 EXPECT_EQ(0, callback_count_);
2957 EXPECT_EQ(1, error_callback_count_);
2959 EXPECT_EQ(BluetoothDevice::ERROR_FAILED, last_connect_error_);
2961 EXPECT_FALSE(device->IsConnected());
2962 EXPECT_FALSE(device->IsConnecting());
2963 EXPECT_FALSE(device->IsPaired());
2966 TEST_F(BluetoothChromeOSTest, PairingFails) {
2967 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2969 GetAdapter();
2970 DiscoverDevice(FakeBluetoothDeviceClient::kVanishingDeviceAddress);
2972 // The vanishing device times out during pairing
2973 BluetoothDevice* device = adapter_->GetDevice(
2974 FakeBluetoothDeviceClient::kVanishingDeviceAddress);
2975 ASSERT_TRUE(device != NULL);
2976 ASSERT_FALSE(device->IsPaired());
2978 TestObserver observer(adapter_);
2980 TestPairingDelegate pairing_delegate;
2981 device->Connect(&pairing_delegate, GetCallback(),
2982 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2983 base::Unretained(this)));
2985 EXPECT_EQ(0, pairing_delegate.call_count_);
2986 EXPECT_TRUE(device->IsConnecting());
2988 // Run the loop to get the error..
2989 message_loop_.Run();
2991 EXPECT_EQ(0, callback_count_);
2992 EXPECT_EQ(1, error_callback_count_);
2994 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_TIMEOUT, last_connect_error_);
2996 EXPECT_FALSE(device->IsConnected());
2997 EXPECT_FALSE(device->IsConnecting());
2998 EXPECT_FALSE(device->IsPaired());
3001 TEST_F(BluetoothChromeOSTest, PairingFailsAtConnection) {
3002 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3004 GetAdapter();
3005 DiscoverDevices();
3007 // Everything seems to go according to plan with the unconnectable device;
3008 // it pairs, but then you can't make connections to it after.
3009 BluetoothDevice* device = adapter_->GetDevice(
3010 FakeBluetoothDeviceClient::kUnconnectableDeviceAddress);
3011 ASSERT_TRUE(device != NULL);
3012 ASSERT_FALSE(device->IsPaired());
3014 TestObserver observer(adapter_);
3016 TestPairingDelegate pairing_delegate;
3017 device->Connect(&pairing_delegate, GetCallback(),
3018 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
3019 base::Unretained(this)));
3021 EXPECT_EQ(0, pairing_delegate.call_count_);
3022 EXPECT_TRUE(device->IsConnecting());
3024 message_loop_.Run();
3026 EXPECT_EQ(0, callback_count_);
3027 EXPECT_EQ(1, error_callback_count_);
3028 EXPECT_EQ(BluetoothDevice::ERROR_FAILED, last_connect_error_);
3030 // Two changes for connecting, one for paired and one for trusted after
3031 // pairing. The device should not be connected.
3032 EXPECT_EQ(4, observer.device_changed_count_);
3033 EXPECT_EQ(device, observer.last_device_);
3035 EXPECT_FALSE(device->IsConnected());
3036 EXPECT_FALSE(device->IsConnecting());
3038 EXPECT_TRUE(device->IsPaired());
3040 // Make sure the trusted property has been set to true still (since pairing
3041 // worked).
3042 FakeBluetoothDeviceClient::Properties* properties =
3043 fake_bluetooth_device_client_->GetProperties(
3044 dbus::ObjectPath(
3045 FakeBluetoothDeviceClient::kUnconnectableDevicePath));
3046 EXPECT_TRUE(properties->trusted.value());
3049 TEST_F(BluetoothChromeOSTest, PairingRejectedAtPinCode) {
3050 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3052 GetAdapter();
3053 DiscoverDevices();
3055 // Reject the pairing after we receive a request for the PIN code.
3056 BluetoothDevice* device = adapter_->GetDevice(
3057 FakeBluetoothDeviceClient::kRequestPinCodeAddress);
3058 ASSERT_TRUE(device != NULL);
3059 ASSERT_FALSE(device->IsPaired());
3061 TestObserver observer(adapter_);
3063 TestPairingDelegate pairing_delegate;
3064 device->Connect(&pairing_delegate, GetCallback(),
3065 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
3066 base::Unretained(this)));
3068 EXPECT_EQ(1, pairing_delegate.call_count_);
3069 EXPECT_EQ(1, pairing_delegate.request_pincode_count_);
3070 EXPECT_TRUE(device->IsConnecting());
3072 // Reject the pairing.
3073 device->RejectPairing();
3074 message_loop_.Run();
3076 EXPECT_EQ(0, callback_count_);
3077 EXPECT_EQ(1, error_callback_count_);
3078 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_REJECTED, last_connect_error_);
3080 // Should be no changes except connecting going true and false.
3081 EXPECT_EQ(2, observer.device_changed_count_);
3082 EXPECT_FALSE(device->IsConnected());
3083 EXPECT_FALSE(device->IsConnecting());
3084 EXPECT_FALSE(device->IsPaired());
3087 TEST_F(BluetoothChromeOSTest, PairingCancelledAtPinCode) {
3088 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3090 GetAdapter();
3091 DiscoverDevices();
3093 // Cancel the pairing after we receive a request for the PIN code.
3094 BluetoothDevice* device = adapter_->GetDevice(
3095 FakeBluetoothDeviceClient::kRequestPinCodeAddress);
3096 ASSERT_TRUE(device != NULL);
3097 ASSERT_FALSE(device->IsPaired());
3099 TestObserver observer(adapter_);
3101 TestPairingDelegate pairing_delegate;
3102 device->Connect(&pairing_delegate, GetCallback(),
3103 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
3104 base::Unretained(this)));
3106 EXPECT_EQ(1, pairing_delegate.call_count_);
3107 EXPECT_EQ(1, pairing_delegate.request_pincode_count_);
3108 EXPECT_TRUE(device->IsConnecting());
3110 // Cancel the pairing.
3111 device->CancelPairing();
3112 message_loop_.Run();
3114 EXPECT_EQ(0, callback_count_);
3115 EXPECT_EQ(1, error_callback_count_);
3116 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED, last_connect_error_);
3118 // Should be no changes except connecting going true and false.
3119 EXPECT_EQ(2, observer.device_changed_count_);
3120 EXPECT_FALSE(device->IsConnected());
3121 EXPECT_FALSE(device->IsConnecting());
3122 EXPECT_FALSE(device->IsPaired());
3125 TEST_F(BluetoothChromeOSTest, PairingRejectedAtPasskey) {
3126 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3128 GetAdapter();
3129 DiscoverDevices();
3131 // Reject the pairing after we receive a request for the passkey.
3132 BluetoothDevice* device = adapter_->GetDevice(
3133 FakeBluetoothDeviceClient::kRequestPasskeyAddress);
3134 ASSERT_TRUE(device != NULL);
3135 ASSERT_FALSE(device->IsPaired());
3137 TestObserver observer(adapter_);
3139 TestPairingDelegate pairing_delegate;
3140 device->Connect(&pairing_delegate, GetCallback(),
3141 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
3142 base::Unretained(this)));
3144 EXPECT_EQ(1, pairing_delegate.call_count_);
3145 EXPECT_EQ(1, pairing_delegate.request_passkey_count_);
3146 EXPECT_TRUE(device->IsConnecting());
3148 // Reject the pairing.
3149 device->RejectPairing();
3150 message_loop_.Run();
3152 EXPECT_EQ(0, callback_count_);
3153 EXPECT_EQ(1, error_callback_count_);
3154 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_REJECTED, last_connect_error_);
3156 // Should be no changes except connecting going true and false.
3157 EXPECT_EQ(2, observer.device_changed_count_);
3158 EXPECT_FALSE(device->IsConnected());
3159 EXPECT_FALSE(device->IsConnecting());
3160 EXPECT_FALSE(device->IsPaired());
3163 TEST_F(BluetoothChromeOSTest, PairingCancelledAtPasskey) {
3164 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3166 GetAdapter();
3167 DiscoverDevices();
3169 // Cancel the pairing after we receive a request for the passkey.
3170 BluetoothDevice* device = adapter_->GetDevice(
3171 FakeBluetoothDeviceClient::kRequestPasskeyAddress);
3172 ASSERT_TRUE(device != NULL);
3173 ASSERT_FALSE(device->IsPaired());
3175 TestObserver observer(adapter_);
3177 TestPairingDelegate pairing_delegate;
3178 device->Connect(&pairing_delegate, GetCallback(),
3179 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
3180 base::Unretained(this)));
3182 EXPECT_EQ(1, pairing_delegate.call_count_);
3183 EXPECT_EQ(1, pairing_delegate.request_passkey_count_);
3184 EXPECT_TRUE(device->IsConnecting());
3186 // Cancel the pairing.
3187 device->CancelPairing();
3188 message_loop_.Run();
3190 EXPECT_EQ(0, callback_count_);
3191 EXPECT_EQ(1, error_callback_count_);
3192 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED, last_connect_error_);
3194 // Should be no changes except connecting going true and false.
3195 EXPECT_EQ(2, observer.device_changed_count_);
3196 EXPECT_FALSE(device->IsConnected());
3197 EXPECT_FALSE(device->IsConnecting());
3198 EXPECT_FALSE(device->IsPaired());
3201 TEST_F(BluetoothChromeOSTest, PairingRejectedAtConfirmation) {
3202 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3204 GetAdapter();
3205 DiscoverDevices();
3207 // Reject the pairing after we receive a request for passkey confirmation.
3208 BluetoothDevice* device = adapter_->GetDevice(
3209 FakeBluetoothDeviceClient::kConfirmPasskeyAddress);
3210 ASSERT_TRUE(device != NULL);
3211 ASSERT_FALSE(device->IsPaired());
3213 TestObserver observer(adapter_);
3215 TestPairingDelegate pairing_delegate;
3216 device->Connect(&pairing_delegate, GetCallback(),
3217 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
3218 base::Unretained(this)));
3220 EXPECT_EQ(1, pairing_delegate.call_count_);
3221 EXPECT_EQ(1, pairing_delegate.confirm_passkey_count_);
3222 EXPECT_TRUE(device->IsConnecting());
3224 // Reject the pairing.
3225 device->RejectPairing();
3226 message_loop_.Run();
3228 EXPECT_EQ(0, callback_count_);
3229 EXPECT_EQ(1, error_callback_count_);
3230 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_REJECTED, last_connect_error_);
3232 // Should be no changes except connecting going true and false.
3233 EXPECT_EQ(2, observer.device_changed_count_);
3234 EXPECT_FALSE(device->IsConnected());
3235 EXPECT_FALSE(device->IsConnecting());
3236 EXPECT_FALSE(device->IsPaired());
3239 TEST_F(BluetoothChromeOSTest, PairingCancelledAtConfirmation) {
3240 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3242 GetAdapter();
3243 DiscoverDevices();
3245 // Cancel the pairing after we receive a request for the passkey.
3246 BluetoothDevice* device = adapter_->GetDevice(
3247 FakeBluetoothDeviceClient::kConfirmPasskeyAddress);
3248 ASSERT_TRUE(device != NULL);
3249 ASSERT_FALSE(device->IsPaired());
3251 TestObserver observer(adapter_);
3253 TestPairingDelegate pairing_delegate;
3254 device->Connect(&pairing_delegate, GetCallback(),
3255 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
3256 base::Unretained(this)));
3258 EXPECT_EQ(1, pairing_delegate.call_count_);
3259 EXPECT_EQ(1, pairing_delegate.confirm_passkey_count_);
3260 EXPECT_TRUE(device->IsConnecting());
3262 // Cancel the pairing.
3263 device->CancelPairing();
3264 message_loop_.Run();
3266 EXPECT_EQ(0, callback_count_);
3267 EXPECT_EQ(1, error_callback_count_);
3268 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED, last_connect_error_);
3270 // Should be no changes except connecting going true and false.
3271 EXPECT_EQ(2, observer.device_changed_count_);
3272 EXPECT_FALSE(device->IsConnected());
3273 EXPECT_FALSE(device->IsConnecting());
3274 EXPECT_FALSE(device->IsPaired());
3277 TEST_F(BluetoothChromeOSTest, PairingCancelledInFlight) {
3278 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3280 GetAdapter();
3281 DiscoverDevices();
3283 // Cancel the pairing while we're waiting for the remote host.
3284 BluetoothDevice* device = adapter_->GetDevice(
3285 FakeBluetoothDeviceClient::kLegacyAutopairAddress);
3286 ASSERT_TRUE(device != NULL);
3287 ASSERT_FALSE(device->IsPaired());
3289 TestObserver observer(adapter_);
3291 TestPairingDelegate pairing_delegate;
3292 device->Connect(&pairing_delegate, GetCallback(),
3293 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
3294 base::Unretained(this)));
3296 EXPECT_EQ(0, pairing_delegate.call_count_);
3297 EXPECT_TRUE(device->IsConnecting());
3299 // Cancel the pairing.
3300 device->CancelPairing();
3301 message_loop_.Run();
3303 EXPECT_EQ(0, callback_count_);
3304 EXPECT_EQ(1, error_callback_count_);
3305 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED, last_connect_error_);
3307 // Should be no changes except connecting going true and false.
3308 EXPECT_EQ(2, observer.device_changed_count_);
3309 EXPECT_FALSE(device->IsConnected());
3310 EXPECT_FALSE(device->IsConnecting());
3311 EXPECT_FALSE(device->IsPaired());
3314 TEST_F(BluetoothChromeOSTest, IncomingPairRequestPinCode) {
3315 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3317 GetAdapter();
3319 TestPairingDelegate pairing_delegate;
3320 adapter_->AddPairingDelegate(
3321 &pairing_delegate,
3322 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH);
3324 // Requires that we provide a PIN code.
3325 fake_bluetooth_device_client_->CreateDevice(
3326 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
3327 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath));
3328 BluetoothDevice* device = adapter_->GetDevice(
3329 FakeBluetoothDeviceClient::kRequestPinCodeAddress);
3330 ASSERT_TRUE(device != NULL);
3331 ASSERT_FALSE(device->IsPaired());
3333 TestObserver observer(adapter_);
3335 fake_bluetooth_device_client_->SimulatePairing(
3336 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath), true,
3337 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
3338 base::Unretained(this)));
3340 EXPECT_EQ(1, pairing_delegate.call_count_);
3341 EXPECT_EQ(1, pairing_delegate.request_pincode_count_);
3343 // Set the PIN.
3344 device->SetPinCode("1234");
3345 message_loop_.Run();
3347 EXPECT_EQ(1, callback_count_);
3348 EXPECT_EQ(0, error_callback_count_);
3350 // One change for paired, and one for trusted.
3351 EXPECT_EQ(2, observer.device_changed_count_);
3352 EXPECT_EQ(device, observer.last_device_);
3354 EXPECT_TRUE(device->IsPaired());
3356 // Make sure the trusted property has been set to true.
3357 FakeBluetoothDeviceClient::Properties* properties =
3358 fake_bluetooth_device_client_->GetProperties(
3359 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath));
3360 ASSERT_TRUE(properties->trusted.value());
3362 // No pairing context should remain on the device.
3363 BluetoothDeviceChromeOS* device_chromeos =
3364 static_cast<BluetoothDeviceChromeOS*>(device);
3365 EXPECT_TRUE(device_chromeos->GetPairing() == NULL);
3368 TEST_F(BluetoothChromeOSTest, IncomingPairConfirmPasskey) {
3369 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3371 GetAdapter();
3373 TestPairingDelegate pairing_delegate;
3374 adapter_->AddPairingDelegate(
3375 &pairing_delegate,
3376 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH);
3378 // Requests that we confirm a displayed passkey.
3379 fake_bluetooth_device_client_->CreateDevice(
3380 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
3381 dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath));
3382 BluetoothDevice* device = adapter_->GetDevice(
3383 FakeBluetoothDeviceClient::kConfirmPasskeyAddress);
3384 ASSERT_TRUE(device != NULL);
3385 ASSERT_FALSE(device->IsPaired());
3387 TestObserver observer(adapter_);
3389 fake_bluetooth_device_client_->SimulatePairing(
3390 dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath), true,
3391 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
3392 base::Unretained(this)));
3394 EXPECT_EQ(1, pairing_delegate.call_count_);
3395 EXPECT_EQ(1, pairing_delegate.confirm_passkey_count_);
3396 EXPECT_EQ(123456U, pairing_delegate.last_passkey_);
3398 // Confirm the passkey.
3399 device->ConfirmPairing();
3400 message_loop_.Run();
3402 EXPECT_EQ(1, callback_count_);
3403 EXPECT_EQ(0, error_callback_count_);
3405 // One change for paired, and one for trusted.
3406 EXPECT_EQ(2, observer.device_changed_count_);
3407 EXPECT_EQ(device, observer.last_device_);
3409 EXPECT_TRUE(device->IsPaired());
3411 // Make sure the trusted property has been set to true.
3412 FakeBluetoothDeviceClient::Properties* properties =
3413 fake_bluetooth_device_client_->GetProperties(
3414 dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath));
3415 ASSERT_TRUE(properties->trusted.value());
3417 // No pairing context should remain on the device.
3418 BluetoothDeviceChromeOS* device_chromeos =
3419 static_cast<BluetoothDeviceChromeOS*>(device);
3420 EXPECT_TRUE(device_chromeos->GetPairing() == NULL);
3423 TEST_F(BluetoothChromeOSTest, IncomingPairRequestPasskey) {
3424 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3426 GetAdapter();
3428 TestPairingDelegate pairing_delegate;
3429 adapter_->AddPairingDelegate(
3430 &pairing_delegate,
3431 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH);
3433 // Requests that we provide a Passkey.
3434 fake_bluetooth_device_client_->CreateDevice(
3435 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
3436 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath));
3437 BluetoothDevice* device = adapter_->GetDevice(
3438 FakeBluetoothDeviceClient::kRequestPasskeyAddress);
3439 ASSERT_TRUE(device != NULL);
3440 ASSERT_FALSE(device->IsPaired());
3442 TestObserver observer(adapter_);
3444 fake_bluetooth_device_client_->SimulatePairing(
3445 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath), true,
3446 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
3447 base::Unretained(this)));
3449 EXPECT_EQ(1, pairing_delegate.call_count_);
3450 EXPECT_EQ(1, pairing_delegate.request_passkey_count_);
3452 // Set the Passkey.
3453 device->SetPasskey(1234);
3454 message_loop_.Run();
3456 EXPECT_EQ(1, callback_count_);
3457 EXPECT_EQ(0, error_callback_count_);
3459 // One change for paired, and one for trusted.
3460 EXPECT_EQ(2, observer.device_changed_count_);
3461 EXPECT_EQ(device, observer.last_device_);
3463 EXPECT_TRUE(device->IsPaired());
3465 // Make sure the trusted property has been set to true.
3466 FakeBluetoothDeviceClient::Properties* properties =
3467 fake_bluetooth_device_client_->GetProperties(
3468 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath));
3469 ASSERT_TRUE(properties->trusted.value());
3471 // No pairing context should remain on the device.
3472 BluetoothDeviceChromeOS* device_chromeos =
3473 static_cast<BluetoothDeviceChromeOS*>(device);
3474 EXPECT_TRUE(device_chromeos->GetPairing() == NULL);
3477 TEST_F(BluetoothChromeOSTest, IncomingPairJustWorks) {
3478 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3480 GetAdapter();
3482 TestPairingDelegate pairing_delegate;
3483 adapter_->AddPairingDelegate(
3484 &pairing_delegate,
3485 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH);
3487 // Uses just-works pairing so, sinec this an incoming pairing, require
3488 // authorization from the user.
3489 fake_bluetooth_device_client_->CreateDevice(
3490 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
3491 dbus::ObjectPath(FakeBluetoothDeviceClient::kJustWorksPath));
3492 BluetoothDevice* device = adapter_->GetDevice(
3493 FakeBluetoothDeviceClient::kJustWorksAddress);
3494 ASSERT_TRUE(device != NULL);
3495 ASSERT_FALSE(device->IsPaired());
3497 TestObserver observer(adapter_);
3499 fake_bluetooth_device_client_->SimulatePairing(
3500 dbus::ObjectPath(FakeBluetoothDeviceClient::kJustWorksPath), true,
3501 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
3502 base::Unretained(this)));
3504 EXPECT_EQ(1, pairing_delegate.call_count_);
3505 EXPECT_EQ(1, pairing_delegate.authorize_pairing_count_);
3507 // Confirm the pairing.
3508 device->ConfirmPairing();
3509 message_loop_.Run();
3511 EXPECT_EQ(1, callback_count_);
3512 EXPECT_EQ(0, error_callback_count_);
3514 // One change for paired, and one for trusted.
3515 EXPECT_EQ(2, observer.device_changed_count_);
3516 EXPECT_EQ(device, observer.last_device_);
3518 EXPECT_TRUE(device->IsPaired());
3520 // Make sure the trusted property has been set to true.
3521 FakeBluetoothDeviceClient::Properties* properties =
3522 fake_bluetooth_device_client_->GetProperties(
3523 dbus::ObjectPath(FakeBluetoothDeviceClient::kJustWorksPath));
3524 ASSERT_TRUE(properties->trusted.value());
3526 // No pairing context should remain on the device.
3527 BluetoothDeviceChromeOS* device_chromeos =
3528 static_cast<BluetoothDeviceChromeOS*>(device);
3529 EXPECT_TRUE(device_chromeos->GetPairing() == NULL);
3532 TEST_F(BluetoothChromeOSTest, IncomingPairRequestPinCodeWithoutDelegate) {
3533 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3535 GetAdapter();
3537 // Requires that we provide a PIN Code, without a pairing delegate,
3538 // that will be rejected.
3539 fake_bluetooth_device_client_->CreateDevice(
3540 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
3541 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath));
3542 BluetoothDevice* device = adapter_->GetDevice(
3543 FakeBluetoothDeviceClient::kRequestPinCodeAddress);
3544 ASSERT_TRUE(device != NULL);
3545 ASSERT_FALSE(device->IsPaired());
3547 TestObserver observer(adapter_);
3549 fake_bluetooth_device_client_->SimulatePairing(
3550 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath), true,
3551 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
3552 base::Unretained(this)));
3554 message_loop_.Run();
3556 EXPECT_EQ(0, callback_count_);
3557 EXPECT_EQ(1, error_callback_count_);
3558 EXPECT_EQ(bluetooth_device::kErrorAuthenticationRejected, last_client_error_);
3560 // No changes should be observer.
3561 EXPECT_EQ(0, observer.device_changed_count_);
3563 EXPECT_FALSE(device->IsPaired());
3565 // No pairing context should remain on the device.
3566 BluetoothDeviceChromeOS* device_chromeos =
3567 static_cast<BluetoothDeviceChromeOS*>(device);
3568 EXPECT_TRUE(device_chromeos->GetPairing() == NULL);
3571 TEST_F(BluetoothChromeOSTest, IncomingPairConfirmPasskeyWithoutDelegate) {
3572 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3574 GetAdapter();
3576 // Requests that we confirm a displayed passkey, without a pairing delegate,
3577 // that will be rejected.
3578 fake_bluetooth_device_client_->CreateDevice(
3579 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
3580 dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath));
3581 BluetoothDevice* device = adapter_->GetDevice(
3582 FakeBluetoothDeviceClient::kConfirmPasskeyAddress);
3583 ASSERT_TRUE(device != NULL);
3584 ASSERT_FALSE(device->IsPaired());
3586 TestObserver observer(adapter_);
3588 fake_bluetooth_device_client_->SimulatePairing(
3589 dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath), true,
3590 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
3591 base::Unretained(this)));
3593 message_loop_.Run();
3595 EXPECT_EQ(0, callback_count_);
3596 EXPECT_EQ(1, error_callback_count_);
3597 EXPECT_EQ(bluetooth_device::kErrorAuthenticationRejected, last_client_error_);
3599 // No changes should be observer.
3600 EXPECT_EQ(0, observer.device_changed_count_);
3602 EXPECT_FALSE(device->IsPaired());
3604 // No pairing context should remain on the device.
3605 BluetoothDeviceChromeOS* device_chromeos =
3606 static_cast<BluetoothDeviceChromeOS*>(device);
3607 EXPECT_TRUE(device_chromeos->GetPairing() == NULL);
3610 TEST_F(BluetoothChromeOSTest, IncomingPairRequestPasskeyWithoutDelegate) {
3611 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3613 GetAdapter();
3615 // Requests that we provide a displayed passkey, without a pairing delegate,
3616 // that will be rejected.
3617 fake_bluetooth_device_client_->CreateDevice(
3618 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
3619 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath));
3620 BluetoothDevice* device = adapter_->GetDevice(
3621 FakeBluetoothDeviceClient::kRequestPasskeyAddress);
3622 ASSERT_TRUE(device != NULL);
3623 ASSERT_FALSE(device->IsPaired());
3625 TestObserver observer(adapter_);
3627 fake_bluetooth_device_client_->SimulatePairing(
3628 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath), true,
3629 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
3630 base::Unretained(this)));
3632 message_loop_.Run();
3634 EXPECT_EQ(0, callback_count_);
3635 EXPECT_EQ(1, error_callback_count_);
3636 EXPECT_EQ(bluetooth_device::kErrorAuthenticationRejected, last_client_error_);
3638 // No changes should be observer.
3639 EXPECT_EQ(0, observer.device_changed_count_);
3641 EXPECT_FALSE(device->IsPaired());
3643 // No pairing context should remain on the device.
3644 BluetoothDeviceChromeOS* device_chromeos =
3645 static_cast<BluetoothDeviceChromeOS*>(device);
3646 EXPECT_TRUE(device_chromeos->GetPairing() == NULL);
3649 TEST_F(BluetoothChromeOSTest, IncomingPairJustWorksWithoutDelegate) {
3650 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3652 GetAdapter();
3654 // Uses just-works pairing and thus requires authorization for incoming
3655 // pairings, without a pairing delegate, that will be rejected.
3656 fake_bluetooth_device_client_->CreateDevice(
3657 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
3658 dbus::ObjectPath(FakeBluetoothDeviceClient::kJustWorksPath));
3659 BluetoothDevice* device = adapter_->GetDevice(
3660 FakeBluetoothDeviceClient::kJustWorksAddress);
3661 ASSERT_TRUE(device != NULL);
3662 ASSERT_FALSE(device->IsPaired());
3664 TestObserver observer(adapter_);
3666 fake_bluetooth_device_client_->SimulatePairing(
3667 dbus::ObjectPath(FakeBluetoothDeviceClient::kJustWorksPath), true,
3668 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
3669 base::Unretained(this)));
3671 message_loop_.Run();
3673 EXPECT_EQ(0, callback_count_);
3674 EXPECT_EQ(1, error_callback_count_);
3675 EXPECT_EQ(bluetooth_device::kErrorAuthenticationRejected, last_client_error_);
3677 // No changes should be observer.
3678 EXPECT_EQ(0, observer.device_changed_count_);
3680 EXPECT_FALSE(device->IsPaired());
3682 // No pairing context should remain on the device.
3683 BluetoothDeviceChromeOS* device_chromeos =
3684 static_cast<BluetoothDeviceChromeOS*>(device);
3685 EXPECT_TRUE(device_chromeos->GetPairing() == NULL);
3688 TEST_F(BluetoothChromeOSTest, RemovePairingDelegateDuringPairing) {
3689 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3691 GetAdapter();
3693 TestPairingDelegate pairing_delegate;
3694 adapter_->AddPairingDelegate(
3695 &pairing_delegate,
3696 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH);
3698 // Requests that we provide a Passkey.
3699 fake_bluetooth_device_client_->CreateDevice(
3700 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
3701 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath));
3702 BluetoothDevice* device = adapter_->GetDevice(
3703 FakeBluetoothDeviceClient::kRequestPasskeyAddress);
3704 ASSERT_TRUE(device != NULL);
3705 ASSERT_FALSE(device->IsPaired());
3707 TestObserver observer(adapter_);
3709 fake_bluetooth_device_client_->SimulatePairing(
3710 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath), true,
3711 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
3712 base::Unretained(this)));
3714 EXPECT_EQ(1, pairing_delegate.call_count_);
3715 EXPECT_EQ(1, pairing_delegate.request_passkey_count_);
3717 // A pairing context should now be set on the device.
3718 BluetoothDeviceChromeOS* device_chromeos =
3719 static_cast<BluetoothDeviceChromeOS*>(device);
3720 ASSERT_TRUE(device_chromeos->GetPairing() != NULL);
3722 // Removing the pairing delegate should remove that pairing context.
3723 adapter_->RemovePairingDelegate(&pairing_delegate);
3725 EXPECT_TRUE(device_chromeos->GetPairing() == NULL);
3727 // Set the Passkey, this should now have no effect since the pairing has
3728 // been, in-effect, cancelled
3729 device->SetPasskey(1234);
3731 EXPECT_EQ(0, callback_count_);
3732 EXPECT_EQ(0, error_callback_count_);
3733 EXPECT_EQ(0, observer.device_changed_count_);
3735 EXPECT_FALSE(device->IsPaired());
3738 TEST_F(BluetoothChromeOSTest, DeviceId) {
3739 GetAdapter();
3741 // Use the built-in paired device for this test, grab its Properties
3742 // structure so we can adjust the underlying modalias property.
3743 BluetoothDevice* device = adapter_->GetDevice(
3744 FakeBluetoothDeviceClient::kPairedDeviceAddress);
3745 FakeBluetoothDeviceClient::Properties* properties =
3746 fake_bluetooth_device_client_->GetProperties(
3747 dbus::ObjectPath(FakeBluetoothDeviceClient::kPairedDevicePath));
3749 ASSERT_TRUE(device != NULL);
3750 ASSERT_TRUE(properties != NULL);
3752 // Valid USB IF-assigned identifier.
3753 ASSERT_EQ("usb:v05ACp030Dd0306", properties->modalias.value());
3755 EXPECT_EQ(BluetoothDevice::VENDOR_ID_USB, device->GetVendorIDSource());
3756 EXPECT_EQ(0x05ac, device->GetVendorID());
3757 EXPECT_EQ(0x030d, device->GetProductID());
3758 EXPECT_EQ(0x0306, device->GetDeviceID());
3760 // Valid Bluetooth SIG-assigned identifier.
3761 properties->modalias.ReplaceValue("bluetooth:v00E0p2400d0400");
3763 EXPECT_EQ(BluetoothDevice::VENDOR_ID_BLUETOOTH, device->GetVendorIDSource());
3764 EXPECT_EQ(0x00e0, device->GetVendorID());
3765 EXPECT_EQ(0x2400, device->GetProductID());
3766 EXPECT_EQ(0x0400, device->GetDeviceID());
3768 // Invalid USB IF-assigned identifier.
3769 properties->modalias.ReplaceValue("usb:x00E0p2400d0400");
3771 EXPECT_EQ(BluetoothDevice::VENDOR_ID_UNKNOWN, device->GetVendorIDSource());
3772 EXPECT_EQ(0, device->GetVendorID());
3773 EXPECT_EQ(0, device->GetProductID());
3774 EXPECT_EQ(0, device->GetDeviceID());
3776 // Invalid Bluetooth SIG-assigned identifier.
3777 properties->modalias.ReplaceValue("bluetooth:x00E0p2400d0400");
3779 EXPECT_EQ(BluetoothDevice::VENDOR_ID_UNKNOWN, device->GetVendorIDSource());
3780 EXPECT_EQ(0, device->GetVendorID());
3781 EXPECT_EQ(0, device->GetProductID());
3782 EXPECT_EQ(0, device->GetDeviceID());
3784 // Unknown vendor specification identifier.
3785 properties->modalias.ReplaceValue("chrome:v00E0p2400d0400");
3787 EXPECT_EQ(BluetoothDevice::VENDOR_ID_UNKNOWN, device->GetVendorIDSource());
3788 EXPECT_EQ(0, device->GetVendorID());
3789 EXPECT_EQ(0, device->GetProductID());
3790 EXPECT_EQ(0, device->GetDeviceID());
3793 TEST_F(BluetoothChromeOSTest, GetConnectionInfoForDisconnectedDevice) {
3794 GetAdapter();
3795 BluetoothDevice* device =
3796 adapter_->GetDevice(FakeBluetoothDeviceClient::kPairedDeviceAddress);
3798 // Calling GetConnectionInfo for an unconnected device should return a result
3799 // in which all fields are filled with BluetoothDevice::kUnknownPower.
3800 BluetoothDevice::ConnectionInfo conn_info(0, 0, 0);
3801 device->GetConnectionInfo(base::Bind(&SaveConnectionInfo, &conn_info));
3802 int unknown_power = BluetoothDevice::kUnknownPower;
3803 EXPECT_NE(0, unknown_power);
3804 EXPECT_EQ(unknown_power, conn_info.rssi);
3805 EXPECT_EQ(unknown_power, conn_info.transmit_power);
3806 EXPECT_EQ(unknown_power, conn_info.max_transmit_power);
3809 TEST_F(BluetoothChromeOSTest, GetConnectionInfoForConnectedDevice) {
3810 GetAdapter();
3811 BluetoothDevice* device =
3812 adapter_->GetDevice(FakeBluetoothDeviceClient::kPairedDeviceAddress);
3814 device->Connect(NULL, GetCallback(),
3815 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
3816 base::Unretained(this)));
3817 EXPECT_TRUE(device->IsConnected());
3819 // Calling GetConnectionInfo for a connected device should return valid
3820 // results.
3821 fake_bluetooth_device_client_->UpdateConnectionInfo(-10, 3, 4);
3822 BluetoothDevice::ConnectionInfo conn_info;
3823 device->GetConnectionInfo(base::Bind(&SaveConnectionInfo, &conn_info));
3824 EXPECT_EQ(-10, conn_info.rssi);
3825 EXPECT_EQ(3, conn_info.transmit_power);
3826 EXPECT_EQ(4, conn_info.max_transmit_power);
3829 // Verifies Shutdown shuts down the adapter as expected.
3830 TEST_F(BluetoothChromeOSTest, Shutdown) {
3831 // Set up adapter. Set powered & discoverable, start discovery.
3832 GetAdapter();
3833 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
3834 adapter_->SetDiscoverable(true, GetCallback(), GetErrorCallback());
3835 adapter_->StartDiscoverySession(
3836 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
3837 base::Unretained(this)),
3838 GetErrorCallback());
3839 base::MessageLoop::current()->Run();
3840 ASSERT_EQ(3, callback_count_);
3841 ASSERT_EQ(0, error_callback_count_);
3842 callback_count_ = 0;
3844 TestPairingDelegate pairing_delegate;
3845 adapter_->AddPairingDelegate(
3846 &pairing_delegate, BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH);
3848 // Validate running adapter state.
3849 EXPECT_NE("", adapter_->GetAddress());
3850 EXPECT_NE("", adapter_->GetName());
3851 EXPECT_TRUE(adapter_->IsInitialized());
3852 EXPECT_TRUE(adapter_->IsPresent());
3853 EXPECT_TRUE(adapter_->IsPowered());
3854 EXPECT_TRUE(adapter_->IsDiscoverable());
3855 EXPECT_TRUE(adapter_->IsDiscovering());
3856 EXPECT_EQ(2U, adapter_->GetDevices().size());
3857 EXPECT_NE(nullptr, adapter_->GetDevice(
3858 FakeBluetoothDeviceClient::kPairedDeviceAddress));
3859 EXPECT_NE(dbus::ObjectPath(""), static_cast<BluetoothAdapterChromeOS*>(
3860 adapter_.get())->object_path());
3862 // Shutdown
3863 adapter_->Shutdown();
3865 // Validate post shutdown state by calling all BluetoothAdapterChromeOS
3866 // members, in declaration order:
3868 adapter_->Shutdown();
3869 // DeleteOnCorrectThread omitted as we don't want to delete in this test.
3871 TestObserver observer(adapter_); // Calls AddObserver
3872 } // TestObserver::~TestObserver calls RemoveObserver.
3873 EXPECT_EQ("", adapter_->GetAddress());
3874 EXPECT_EQ("", adapter_->GetName());
3876 adapter_->SetName("", GetCallback(), GetErrorCallback());
3877 EXPECT_EQ(0, callback_count_);
3878 EXPECT_EQ(1, error_callback_count_--) << "SetName error";
3880 EXPECT_TRUE(adapter_->IsInitialized());
3881 EXPECT_FALSE(adapter_->IsPresent());
3882 EXPECT_FALSE(adapter_->IsPowered());
3884 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
3885 EXPECT_EQ(0, callback_count_);
3886 EXPECT_EQ(1, error_callback_count_--) << "SetPowered error";
3888 EXPECT_FALSE(adapter_->IsDiscoverable());
3890 adapter_->SetDiscoverable(true, GetCallback(), GetErrorCallback());
3891 EXPECT_EQ(0, callback_count_);
3892 EXPECT_EQ(1, error_callback_count_--) << "SetDiscoverable error";
3894 EXPECT_FALSE(adapter_->IsDiscovering());
3895 // CreateRfcommService will DCHECK after Shutdown().
3896 // CreateL2capService will DCHECK after Shutdown().
3898 BluetoothAudioSink::Options audio_sink_options;
3899 adapter_->RegisterAudioSink(
3900 audio_sink_options,
3901 base::Bind(&BluetoothChromeOSTest::AudioSinkAcquiredCallback,
3902 base::Unretained(this)),
3903 base::Bind(&BluetoothChromeOSTest::AudioSinkErrorCallback,
3904 base::Unretained(this)));
3905 EXPECT_EQ(0, callback_count_);
3906 EXPECT_EQ(1, error_callback_count_--) << "RegisterAudioSink error";
3908 BluetoothAdapterChromeOS* adapter_chrome_os =
3909 static_cast<BluetoothAdapterChromeOS*>(adapter_.get());
3910 EXPECT_EQ(NULL, adapter_chrome_os->GetDeviceWithPath(dbus::ObjectPath("")));
3912 // Notify methods presume objects exist that are owned by the adapter and
3913 // destroyed in Shutdown(). Mocks are not attempted here that won't exist,
3914 // as verified below by EXPECT_EQ(0U, adapter_->GetDevices().size());
3915 // NotifyDeviceChanged
3916 // NotifyGattServiceAdded
3917 // NotifyGattServiceRemoved
3918 // NotifyGattServiceChanged
3919 // NotifyGattDiscoveryComplete
3920 // NotifyGattCharacteristicAdded
3921 // NotifyGattCharacteristicRemoved
3922 // NotifyGattDescriptorAdded
3923 // NotifyGattDescriptorRemoved
3924 // NotifyGattCharacteristicValueChanged
3925 // NotifyGattDescriptorValueChanged
3927 EXPECT_EQ(dbus::ObjectPath(""), adapter_chrome_os->object_path());
3929 adapter_profile_ = NULL;
3931 FakeBluetoothProfileServiceProviderDelegate profile_delegate;
3932 adapter_chrome_os->UseProfile(
3933 BluetoothUUID(), dbus::ObjectPath(""),
3934 BluetoothProfileManagerClient::Options(), &profile_delegate,
3935 base::Bind(&BluetoothChromeOSTest::ProfileRegisteredCallback,
3936 base::Unretained(this)),
3937 base::Bind(&BluetoothChromeOSTest::ErrorCompletionCallback,
3938 base::Unretained(this)));
3940 EXPECT_FALSE(adapter_profile_) << "UseProfile error";
3941 EXPECT_EQ(0, callback_count_) << "UseProfile error";
3942 EXPECT_EQ(1, error_callback_count_--) << "UseProfile error";
3944 // Protected and private methods:
3946 adapter_chrome_os->RemovePairingDelegateInternal(&pairing_delegate);
3947 // AdapterAdded() invalid post Shutdown because it calls SetAdapter.
3948 adapter_chrome_os->AdapterRemoved(dbus::ObjectPath("x"));
3949 adapter_chrome_os->AdapterPropertyChanged(dbus::ObjectPath("x"), "");
3950 adapter_chrome_os->DeviceAdded(dbus::ObjectPath(""));
3951 adapter_chrome_os->DeviceRemoved(dbus::ObjectPath(""));
3952 adapter_chrome_os->DevicePropertyChanged(dbus::ObjectPath(""), "");
3953 adapter_chrome_os->InputPropertyChanged(dbus::ObjectPath(""), "");
3954 // BluetoothAgentServiceProvider::Delegate omitted, dbus will be shutdown,
3955 // with the exception of Released.
3956 adapter_chrome_os->Released();
3958 adapter_chrome_os->OnRegisterAgent();
3959 adapter_chrome_os->OnRegisterAgentError("", "");
3960 adapter_chrome_os->OnRequestDefaultAgent();
3961 adapter_chrome_os->OnRequestDefaultAgentError("", "");
3963 adapter_chrome_os->OnRegisterAudioSink(
3964 base::Bind(&BluetoothChromeOSTest::AudioSinkAcquiredCallback,
3965 base::Unretained(this)),
3966 base::Bind(&BluetoothChromeOSTest::AudioSinkErrorCallback,
3967 base::Unretained(this)),
3968 scoped_refptr<device::BluetoothAudioSink>());
3969 EXPECT_EQ(0, callback_count_);
3970 EXPECT_EQ(1, error_callback_count_--) << "RegisterAudioSink error";
3972 // GetPairing will DCHECK after Shutdown().
3973 // SetAdapter will DCHECK after Shutdown().
3974 // SetDefaultAdapterName will DCHECK after Shutdown().
3975 // RemoveAdapter will DCHECK after Shutdown().
3976 adapter_chrome_os->PoweredChanged(false);
3977 adapter_chrome_os->DiscoverableChanged(false);
3978 adapter_chrome_os->DiscoveringChanged(false);
3979 adapter_chrome_os->PresentChanged(false);
3981 adapter_chrome_os->OnSetDiscoverable(GetCallback(), GetErrorCallback(), true);
3982 EXPECT_EQ(0, callback_count_) << "OnSetDiscoverable error";
3983 EXPECT_EQ(1, error_callback_count_--) << "OnSetDiscoverable error";
3985 adapter_chrome_os->OnPropertyChangeCompleted(GetCallback(),
3986 GetErrorCallback(), true);
3987 EXPECT_EQ(0, callback_count_) << "OnPropertyChangeCompleted error";
3988 EXPECT_EQ(1, error_callback_count_--) << "OnPropertyChangeCompleted error";
3990 adapter_chrome_os->AddDiscoverySession(nullptr, GetCallback(),
3991 GetErrorCallback());
3992 EXPECT_EQ(0, callback_count_) << "AddDiscoverySession error";
3993 EXPECT_EQ(1, error_callback_count_--) << "AddDiscoverySession error";
3995 adapter_chrome_os->RemoveDiscoverySession(nullptr, GetCallback(),
3996 GetErrorCallback());
3997 EXPECT_EQ(0, callback_count_) << "RemoveDiscoverySession error";
3998 EXPECT_EQ(1, error_callback_count_--) << "RemoveDiscoverySession error";
4000 // OnStartDiscovery tested in Shutdown_OnStartDiscovery
4001 // OnStartDiscoveryError tested in Shutdown_OnStartDiscoveryError
4002 // OnStopDiscovery tested in Shutdown_OnStopDiscovery
4003 // OnStopDiscoveryError tested in Shutdown_OnStopDiscoveryError
4005 adapter_profile_ = NULL;
4007 // OnRegisterProfile SetProfileDelegate, OnRegisterProfileError, require
4008 // UseProfile to be set first, do so again here just before calling them.
4009 adapter_chrome_os->UseProfile(
4010 BluetoothUUID(), dbus::ObjectPath(""),
4011 BluetoothProfileManagerClient::Options(), &profile_delegate,
4012 base::Bind(&BluetoothChromeOSTest::ProfileRegisteredCallback,
4013 base::Unretained(this)),
4014 base::Bind(&BluetoothChromeOSTest::ErrorCompletionCallback,
4015 base::Unretained(this)));
4017 EXPECT_FALSE(adapter_profile_) << "UseProfile error";
4018 EXPECT_EQ(0, callback_count_) << "UseProfile error";
4019 EXPECT_EQ(1, error_callback_count_--) << "UseProfile error";
4021 adapter_chrome_os->SetProfileDelegate(
4022 BluetoothUUID(), dbus::ObjectPath(""), &profile_delegate,
4023 base::Bind(&BluetoothChromeOSTest::ProfileRegisteredCallback,
4024 base::Unretained(this)),
4025 base::Bind(&BluetoothChromeOSTest::ErrorCompletionCallback,
4026 base::Unretained(this)));
4027 EXPECT_EQ(0, callback_count_) << "SetProfileDelegate error";
4028 EXPECT_EQ(1, error_callback_count_--) << "SetProfileDelegate error";
4030 adapter_chrome_os->OnRegisterProfileError(BluetoothUUID(), "", "");
4031 EXPECT_EQ(0, callback_count_) << "OnRegisterProfileError error";
4032 EXPECT_EQ(0, error_callback_count_) << "OnRegisterProfileError error";
4034 adapter_chrome_os->ProcessQueuedDiscoveryRequests();
4036 // From BluetoothAdapater:
4038 adapter_->StartDiscoverySession(
4039 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
4040 base::Unretained(this)),
4041 GetErrorCallback());
4042 EXPECT_EQ(0, callback_count_) << "StartDiscoverySession error";
4043 EXPECT_EQ(1, error_callback_count_--) << "StartDiscoverySession error";
4045 EXPECT_EQ(0U, adapter_->GetDevices().size());
4046 EXPECT_EQ(nullptr, adapter_->GetDevice(
4047 FakeBluetoothDeviceClient::kPairedDeviceAddress));
4048 TestPairingDelegate pairing_delegate2;
4049 adapter_->AddPairingDelegate(
4050 &pairing_delegate2, BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH);
4051 adapter_->RemovePairingDelegate(&pairing_delegate2);
4054 // Verifies post-Shutdown of discovery sessions and OnStartDiscovery.
4055 TEST_F(BluetoothChromeOSTest, Shutdown_OnStartDiscovery) {
4056 const int kNumberOfDiscoverySessions = 10;
4057 GetAdapter();
4058 BluetoothAdapterChromeOS* adapter_chrome_os =
4059 static_cast<BluetoothAdapterChromeOS*>(adapter_.get());
4061 for (int i = 0; i < kNumberOfDiscoverySessions; i++) {
4062 adapter_chrome_os->AddDiscoverySession(nullptr, GetCallback(),
4063 GetErrorCallback());
4065 adapter_->Shutdown();
4066 adapter_chrome_os->OnStartDiscovery(GetCallback(), GetErrorCallback());
4068 EXPECT_EQ(0, callback_count_);
4069 EXPECT_EQ(kNumberOfDiscoverySessions, error_callback_count_);
4072 // Verifies post-Shutdown of discovery sessions and OnStartDiscoveryError.
4073 TEST_F(BluetoothChromeOSTest, Shutdown_OnStartDiscoveryError) {
4074 const int kNumberOfDiscoverySessions = 10;
4075 GetAdapter();
4076 BluetoothAdapterChromeOS* adapter_chrome_os =
4077 static_cast<BluetoothAdapterChromeOS*>(adapter_.get());
4079 for (int i = 0; i < kNumberOfDiscoverySessions; i++) {
4080 adapter_chrome_os->AddDiscoverySession(nullptr, GetCallback(),
4081 GetErrorCallback());
4083 adapter_->Shutdown();
4084 adapter_chrome_os->OnStartDiscoveryError(GetCallback(), GetErrorCallback(),
4085 "", "");
4087 EXPECT_EQ(0, callback_count_);
4088 EXPECT_EQ(kNumberOfDiscoverySessions, error_callback_count_);
4091 // Verifies post-Shutdown of discovery sessions and OnStartDiscovery.
4092 TEST_F(BluetoothChromeOSTest, Shutdown_OnStopDiscovery) {
4093 const int kNumberOfDiscoverySessions = 10;
4094 GetAdapter();
4095 BluetoothAdapterChromeOS* adapter_chrome_os =
4096 static_cast<BluetoothAdapterChromeOS*>(adapter_.get());
4098 // In order to queue up discovery sessions before an OnStopDiscovery call
4099 // RemoveDiscoverySession must be called, so Add, Start, and Remove:
4100 adapter_chrome_os->AddDiscoverySession(nullptr, GetCallback(),
4101 GetErrorCallback());
4102 adapter_chrome_os->OnStartDiscovery(GetCallback(), GetErrorCallback());
4103 adapter_chrome_os->RemoveDiscoverySession(nullptr, GetCallback(),
4104 GetErrorCallback());
4105 callback_count_ = 0;
4106 error_callback_count_ = 0;
4107 // Can now queue discovery sessions while waiting for OnStopDiscovery.
4108 for (int i = 0; i < kNumberOfDiscoverySessions; i++) {
4109 adapter_chrome_os->AddDiscoverySession(nullptr, GetCallback(),
4110 GetErrorCallback());
4112 adapter_->Shutdown();
4113 adapter_chrome_os->OnStopDiscovery(GetCallback());
4115 // 1 successful stopped discovery from RemoveDiscoverySession, and
4116 // kNumberOfDiscoverySessions errors from AddDiscoverySession/OnStopDiscovery.
4117 EXPECT_EQ(1, callback_count_);
4118 EXPECT_EQ(kNumberOfDiscoverySessions, error_callback_count_);
4121 // Verifies post-Shutdown of discovery sessions and OnStopDiscoveryError.
4122 TEST_F(BluetoothChromeOSTest, Shutdown_OnStopDiscoveryError) {
4123 const int kNumberOfDiscoverySessions = 10;
4124 GetAdapter();
4125 BluetoothAdapterChromeOS* adapter_chrome_os =
4126 static_cast<BluetoothAdapterChromeOS*>(adapter_.get());
4128 // In order to queue up discovery sessions before an OnStopDiscoveryError call
4129 // RemoveDiscoverySession must be called, so Add, Start, and Remove:
4130 adapter_chrome_os->AddDiscoverySession(nullptr, GetCallback(),
4131 GetErrorCallback());
4132 adapter_chrome_os->OnStartDiscovery(GetCallback(), GetErrorCallback());
4133 adapter_chrome_os->RemoveDiscoverySession(nullptr, GetCallback(),
4134 GetErrorCallback());
4135 callback_count_ = 0;
4136 error_callback_count_ = 0;
4137 // Can now queue discovery sessions while waiting for OnStopDiscoveryError.
4138 for (int i = 0; i < kNumberOfDiscoverySessions; i++) {
4139 adapter_chrome_os->AddDiscoverySession(nullptr, GetCallback(),
4140 GetErrorCallback());
4142 adapter_->Shutdown();
4143 adapter_chrome_os->OnStopDiscoveryError(GetErrorCallback(), "", "");
4145 // 1 error reported to RemoveDiscoverySession because of OnStopDiscoveryError,
4146 // and kNumberOfDiscoverySessions errors queued with AddDiscoverySession.
4147 EXPECT_EQ(0, callback_count_);
4148 EXPECT_EQ(1 + kNumberOfDiscoverySessions, error_callback_count_);
4151 } // namespace chromeos