Add BluetoothDevice::VendorIDSource()
[chromium-blink-merge.git] / device / bluetooth / bluetooth_chromeos_unittest.cc
blobd24ad946dcc531282fb71c7c2c7684c30ba4fc07
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/memory/scoped_vector.h"
6 #include "base/message_loop/message_loop.h"
7 #include "base/strings/utf_string_conversions.h"
8 #include "chromeos/dbus/fake_bluetooth_adapter_client.h"
9 #include "chromeos/dbus/fake_bluetooth_agent_manager_client.h"
10 #include "chromeos/dbus/fake_bluetooth_device_client.h"
11 #include "chromeos/dbus/fake_bluetooth_input_client.h"
12 #include "chromeos/dbus/fake_dbus_thread_manager.h"
13 #include "dbus/object_path.h"
14 #include "device/bluetooth/bluetooth_adapter.h"
15 #include "device/bluetooth/bluetooth_adapter_chromeos.h"
16 #include "device/bluetooth/bluetooth_adapter_factory.h"
17 #include "device/bluetooth/bluetooth_device.h"
18 #include "device/bluetooth/bluetooth_device_chromeos.h"
19 #include "device/bluetooth/bluetooth_discovery_session.h"
20 #include "device/bluetooth/bluetooth_pairing_chromeos.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22 #include "third_party/cros_system_api/dbus/service_constants.h"
24 using device::BluetoothAdapter;
25 using device::BluetoothAdapterFactory;
26 using device::BluetoothDevice;
27 using device::BluetoothDiscoverySession;
29 namespace chromeos {
31 class TestObserver : public BluetoothAdapter::Observer {
32 public:
33 TestObserver(scoped_refptr<BluetoothAdapter> adapter)
34 : present_changed_count_(0),
35 powered_changed_count_(0),
36 discoverable_changed_count_(0),
37 discovering_changed_count_(0),
38 last_present_(false),
39 last_powered_(false),
40 last_discovering_(false),
41 device_added_count_(0),
42 device_changed_count_(0),
43 device_removed_count_(0),
44 last_device_(NULL),
45 adapter_(adapter) {
46 adapter_->AddObserver(this);
49 virtual ~TestObserver() {
50 adapter_->RemoveObserver(this);
53 virtual void AdapterPresentChanged(BluetoothAdapter* adapter,
54 bool present) OVERRIDE {
55 EXPECT_EQ(adapter_, adapter);
57 ++present_changed_count_;
58 last_present_ = present;
61 virtual void AdapterPoweredChanged(BluetoothAdapter* adapter,
62 bool powered) OVERRIDE {
63 EXPECT_EQ(adapter_, adapter);
65 ++powered_changed_count_;
66 last_powered_ = powered;
69 virtual void AdapterDiscoverableChanged(BluetoothAdapter* adapter,
70 bool discoverable) OVERRIDE {
71 EXPECT_EQ(adapter_, adapter);
73 ++discoverable_changed_count_;
76 virtual void AdapterDiscoveringChanged(BluetoothAdapter* adapter,
77 bool discovering) OVERRIDE {
78 EXPECT_EQ(adapter_, adapter);
80 ++discovering_changed_count_;
81 last_discovering_ = discovering;
84 virtual void DeviceAdded(BluetoothAdapter* adapter,
85 BluetoothDevice* device) OVERRIDE {
86 EXPECT_EQ(adapter_, adapter);
88 ++device_added_count_;
89 last_device_ = device;
90 last_device_address_ = device->GetAddress();
92 QuitMessageLoop();
95 virtual void DeviceChanged(BluetoothAdapter* adapter,
96 BluetoothDevice* device) OVERRIDE {
97 EXPECT_EQ(adapter_, adapter);
99 ++device_changed_count_;
100 last_device_ = device;
101 last_device_address_ = device->GetAddress();
103 QuitMessageLoop();
106 virtual void DeviceRemoved(BluetoothAdapter* adapter,
107 BluetoothDevice* device) OVERRIDE {
108 EXPECT_EQ(adapter_, adapter);
110 ++device_removed_count_;
111 // Can't save device, it may be freed
112 last_device_address_ = device->GetAddress();
114 QuitMessageLoop();
117 int present_changed_count_;
118 int powered_changed_count_;
119 int discoverable_changed_count_;
120 int discovering_changed_count_;
121 bool last_present_;
122 bool last_powered_;
123 bool last_discovering_;
124 int device_added_count_;
125 int device_changed_count_;
126 int device_removed_count_;
127 BluetoothDevice* last_device_;
128 std::string last_device_address_;
130 private:
131 // Some tests use a message loop since background processing is simulated;
132 // break out of those loops.
133 void QuitMessageLoop() {
134 if (base::MessageLoop::current() &&
135 base::MessageLoop::current()->is_running())
136 base::MessageLoop::current()->Quit();
139 scoped_refptr<BluetoothAdapter> adapter_;
142 class TestPairingDelegate : public BluetoothDevice::PairingDelegate {
143 public:
144 TestPairingDelegate()
145 : call_count_(0),
146 request_pincode_count_(0),
147 request_passkey_count_(0),
148 display_pincode_count_(0),
149 display_passkey_count_(0),
150 keys_entered_count_(0),
151 confirm_passkey_count_(0),
152 authorize_pairing_count_(0),
153 last_passkey_(9999999U),
154 last_entered_(999U) {}
155 virtual ~TestPairingDelegate() {}
157 virtual void RequestPinCode(BluetoothDevice* device) OVERRIDE {
158 ++call_count_;
159 ++request_pincode_count_;
160 QuitMessageLoop();
163 virtual void RequestPasskey(BluetoothDevice* device) OVERRIDE {
164 ++call_count_;
165 ++request_passkey_count_;
166 QuitMessageLoop();
169 virtual void DisplayPinCode(BluetoothDevice* device,
170 const std::string& pincode) OVERRIDE {
171 ++call_count_;
172 ++display_pincode_count_;
173 last_pincode_ = pincode;
174 QuitMessageLoop();
177 virtual void DisplayPasskey(BluetoothDevice* device,
178 uint32 passkey) OVERRIDE {
179 ++call_count_;
180 ++display_passkey_count_;
181 last_passkey_ = passkey;
182 QuitMessageLoop();
185 virtual void KeysEntered(BluetoothDevice* device, uint32 entered) OVERRIDE {
186 ++call_count_;
187 ++keys_entered_count_;
188 last_entered_ = entered;
189 QuitMessageLoop();
192 virtual void ConfirmPasskey(BluetoothDevice* device,
193 uint32 passkey) OVERRIDE {
194 ++call_count_;
195 ++confirm_passkey_count_;
196 last_passkey_ = passkey;
197 QuitMessageLoop();
200 virtual void AuthorizePairing(BluetoothDevice* device) OVERRIDE {
201 ++call_count_;
202 ++authorize_pairing_count_;
203 QuitMessageLoop();
206 int call_count_;
207 int request_pincode_count_;
208 int request_passkey_count_;
209 int display_pincode_count_;
210 int display_passkey_count_;
211 int keys_entered_count_;
212 int confirm_passkey_count_;
213 int authorize_pairing_count_;
214 uint32 last_passkey_;
215 uint32 last_entered_;
216 std::string last_pincode_;
218 private:
219 // Some tests use a message loop since background processing is simulated;
220 // break out of those loops.
221 void QuitMessageLoop() {
222 if (base::MessageLoop::current() &&
223 base::MessageLoop::current()->is_running())
224 base::MessageLoop::current()->Quit();
228 class BluetoothChromeOSTest : public testing::Test {
229 public:
230 virtual void SetUp() {
231 FakeDBusThreadManager* fake_dbus_thread_manager = new FakeDBusThreadManager;
232 fake_bluetooth_adapter_client_ = new FakeBluetoothAdapterClient;
233 fake_dbus_thread_manager->SetBluetoothAdapterClient(
234 scoped_ptr<BluetoothAdapterClient>(fake_bluetooth_adapter_client_));
235 fake_bluetooth_device_client_ = new FakeBluetoothDeviceClient;
236 fake_dbus_thread_manager->SetBluetoothDeviceClient(
237 scoped_ptr<BluetoothDeviceClient>(fake_bluetooth_device_client_));
238 fake_dbus_thread_manager->SetBluetoothInputClient(
239 scoped_ptr<BluetoothInputClient>(new FakeBluetoothInputClient));
240 fake_dbus_thread_manager->SetBluetoothAgentManagerClient(
241 scoped_ptr<BluetoothAgentManagerClient>(
242 new FakeBluetoothAgentManagerClient));
243 DBusThreadManager::InitializeForTesting(fake_dbus_thread_manager);
245 fake_bluetooth_adapter_client_->SetSimulationIntervalMs(10);
247 callback_count_ = 0;
248 error_callback_count_ = 0;
249 last_connect_error_ = BluetoothDevice::ERROR_UNKNOWN;
250 last_client_error_ = "";
253 virtual void TearDown() {
254 for (ScopedVector<BluetoothDiscoverySession>::iterator iter =
255 discovery_sessions_.begin();
256 iter != discovery_sessions_.end();
257 ++iter) {
258 BluetoothDiscoverySession* session = *iter;
259 if (!session->IsActive())
260 continue;
261 callback_count_ = 0;
262 session->Stop(
263 base::Bind(&BluetoothChromeOSTest::Callback,
264 base::Unretained(this)),
265 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
266 base::Unretained(this)));
267 message_loop_.Run();
268 ASSERT_EQ(1, callback_count_);
270 discovery_sessions_.clear();
271 adapter_ = NULL;
272 DBusThreadManager::Shutdown();
275 // Generic callbacks
276 void Callback() {
277 ++callback_count_;
278 QuitMessageLoop();
281 void DiscoverySessionCallback(
282 scoped_ptr<BluetoothDiscoverySession> discovery_session) {
283 ++callback_count_;
284 discovery_sessions_.push_back(discovery_session.release());
285 QuitMessageLoop();
288 void ErrorCallback() {
289 ++error_callback_count_;
290 QuitMessageLoop();
293 void DBusErrorCallback(const std::string& error_name,
294 const std::string& error_message) {
295 ++error_callback_count_;
296 last_client_error_ = error_name;
297 QuitMessageLoop();
300 void ConnectErrorCallback(BluetoothDevice::ConnectErrorCode error) {
301 ++error_callback_count_;
302 last_connect_error_ = error;
305 // Call to fill the adapter_ member with a BluetoothAdapter instance.
306 void GetAdapter() {
307 adapter_ = new BluetoothAdapterChromeOS();
308 ASSERT_TRUE(adapter_.get() != NULL);
309 ASSERT_TRUE(adapter_->IsInitialized());
312 // Run a discovery phase until the named device is detected, or if the named
313 // device is not created, the discovery process ends without finding it.
315 // The correct behavior of discovery is tested by the "Discovery" test case
316 // without using this function.
317 void DiscoverDevice(const std::string& address) {
318 ASSERT_TRUE(adapter_.get() != NULL);
319 ASSERT_TRUE(base::MessageLoop::current() != NULL);
320 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
322 TestObserver observer(adapter_);
324 adapter_->SetPowered(
325 true,
326 base::Bind(&BluetoothChromeOSTest::Callback,
327 base::Unretained(this)),
328 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
329 base::Unretained(this)));
330 adapter_->StartDiscoverySession(
331 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
332 base::Unretained(this)),
333 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
334 base::Unretained(this)));
335 base::MessageLoop::current()->Run();
336 ASSERT_EQ(2, callback_count_);
337 ASSERT_EQ(0, error_callback_count_);
338 ASSERT_EQ((size_t)1, discovery_sessions_.size());
339 ASSERT_TRUE(discovery_sessions_[0]->IsActive());
340 callback_count_ = 0;
342 ASSERT_TRUE(adapter_->IsPowered());
343 ASSERT_TRUE(adapter_->IsDiscovering());
345 while (!observer.device_removed_count_ &&
346 observer.last_device_address_ != address)
347 base::MessageLoop::current()->Run();
349 discovery_sessions_[0]->Stop(
350 base::Bind(&BluetoothChromeOSTest::Callback,
351 base::Unretained(this)),
352 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
353 base::Unretained(this)));
354 base::MessageLoop::current()->Run();
355 ASSERT_EQ(1, callback_count_);
356 ASSERT_EQ(0, error_callback_count_);
357 callback_count_ = 0;
359 ASSERT_FALSE(adapter_->IsDiscovering());
362 // Run a discovery phase so we have devices that can be paired with.
363 void DiscoverDevices() {
364 // Pass an invalid address for the device so that the discovery process
365 // completes with all devices.
366 DiscoverDevice("does not exist");
369 protected:
370 base::MessageLoop message_loop_;
371 FakeBluetoothAdapterClient* fake_bluetooth_adapter_client_;
372 FakeBluetoothDeviceClient* fake_bluetooth_device_client_;
373 scoped_refptr<BluetoothAdapter> adapter_;
375 int callback_count_;
376 int error_callback_count_;
377 enum BluetoothDevice::ConnectErrorCode last_connect_error_;
378 std::string last_client_error_;
379 ScopedVector<BluetoothDiscoverySession> discovery_sessions_;
381 private:
382 // Some tests use a message loop since background processing is simulated;
383 // break out of those loops.
384 void QuitMessageLoop() {
385 if (base::MessageLoop::current() &&
386 base::MessageLoop::current()->is_running())
387 base::MessageLoop::current()->Quit();
391 TEST_F(BluetoothChromeOSTest, AlreadyPresent) {
392 GetAdapter();
394 // This verifies that the class gets the list of adapters when created;
395 // and initializes with an existing adapter if there is one.
396 EXPECT_TRUE(adapter_->IsPresent());
397 EXPECT_FALSE(adapter_->IsPowered());
398 EXPECT_EQ(FakeBluetoothAdapterClient::kAdapterAddress,
399 adapter_->GetAddress());
400 EXPECT_FALSE(adapter_->IsDiscovering());
402 // There should be a device
403 BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
404 EXPECT_EQ(1U, devices.size());
405 EXPECT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
406 devices[0]->GetAddress());
409 TEST_F(BluetoothChromeOSTest, BecomePresent) {
410 fake_bluetooth_adapter_client_->SetVisible(false);
411 GetAdapter();
412 ASSERT_FALSE(adapter_->IsPresent());
414 // Install an observer; expect the AdapterPresentChanged to be called
415 // with true, and IsPresent() to return true.
416 TestObserver observer(adapter_);
418 fake_bluetooth_adapter_client_->SetVisible(true);
420 EXPECT_EQ(1, observer.present_changed_count_);
421 EXPECT_TRUE(observer.last_present_);
423 EXPECT_TRUE(adapter_->IsPresent());
425 // We should have had a device announced.
426 EXPECT_EQ(1, observer.device_added_count_);
427 EXPECT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
428 observer.last_device_address_);
430 // Other callbacks shouldn't be called if the values are false.
431 EXPECT_EQ(0, observer.powered_changed_count_);
432 EXPECT_EQ(0, observer.discovering_changed_count_);
433 EXPECT_FALSE(adapter_->IsPowered());
434 EXPECT_FALSE(adapter_->IsDiscovering());
437 TEST_F(BluetoothChromeOSTest, BecomeNotPresent) {
438 GetAdapter();
439 ASSERT_TRUE(adapter_->IsPresent());
441 // Install an observer; expect the AdapterPresentChanged to be called
442 // with false, and IsPresent() to return false.
443 TestObserver observer(adapter_);
445 fake_bluetooth_adapter_client_->SetVisible(false);
447 EXPECT_EQ(1, observer.present_changed_count_);
448 EXPECT_FALSE(observer.last_present_);
450 EXPECT_FALSE(adapter_->IsPresent());
452 // We should have had a device removed.
453 EXPECT_EQ(1, observer.device_removed_count_);
454 EXPECT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
455 observer.last_device_address_);
457 // Other callbacks shouldn't be called since the values are false.
458 EXPECT_EQ(0, observer.powered_changed_count_);
459 EXPECT_EQ(0, observer.discovering_changed_count_);
460 EXPECT_FALSE(adapter_->IsPowered());
461 EXPECT_FALSE(adapter_->IsDiscovering());
464 TEST_F(BluetoothChromeOSTest, SecondAdapter) {
465 GetAdapter();
466 ASSERT_TRUE(adapter_->IsPresent());
468 // Install an observer, then add a second adapter. Nothing should change,
469 // we ignore the second adapter.
470 TestObserver observer(adapter_);
472 fake_bluetooth_adapter_client_->SetSecondVisible(true);
474 EXPECT_EQ(0, observer.present_changed_count_);
476 EXPECT_TRUE(adapter_->IsPresent());
477 EXPECT_EQ(FakeBluetoothAdapterClient::kAdapterAddress,
478 adapter_->GetAddress());
480 // Try removing the first adapter, we should now act as if the adapter
481 // is no longer present rather than fall back to the second.
482 fake_bluetooth_adapter_client_->SetVisible(false);
484 EXPECT_EQ(1, observer.present_changed_count_);
485 EXPECT_FALSE(observer.last_present_);
487 EXPECT_FALSE(adapter_->IsPresent());
489 // We should have had a device removed.
490 EXPECT_EQ(1, observer.device_removed_count_);
491 EXPECT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
492 observer.last_device_address_);
494 // Other callbacks shouldn't be called since the values are false.
495 EXPECT_EQ(0, observer.powered_changed_count_);
496 EXPECT_EQ(0, observer.discovering_changed_count_);
497 EXPECT_FALSE(adapter_->IsPowered());
498 EXPECT_FALSE(adapter_->IsDiscovering());
500 observer.device_removed_count_ = 0;
502 // Removing the second adapter shouldn't set anything either.
503 fake_bluetooth_adapter_client_->SetSecondVisible(false);
505 EXPECT_EQ(0, observer.device_removed_count_);
506 EXPECT_EQ(0, observer.powered_changed_count_);
507 EXPECT_EQ(0, observer.discovering_changed_count_);
510 TEST_F(BluetoothChromeOSTest, BecomePowered) {
511 GetAdapter();
512 ASSERT_FALSE(adapter_->IsPowered());
514 // Install an observer; expect the AdapterPoweredChanged to be called
515 // with true, and IsPowered() to return true.
516 TestObserver observer(adapter_);
518 adapter_->SetPowered(
519 true,
520 base::Bind(&BluetoothChromeOSTest::Callback,
521 base::Unretained(this)),
522 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
523 base::Unretained(this)));
524 EXPECT_EQ(1, callback_count_);
525 EXPECT_EQ(0, error_callback_count_);
527 EXPECT_EQ(1, observer.powered_changed_count_);
528 EXPECT_TRUE(observer.last_powered_);
530 EXPECT_TRUE(adapter_->IsPowered());
533 TEST_F(BluetoothChromeOSTest, BecomeNotPowered) {
534 GetAdapter();
535 adapter_->SetPowered(
536 true,
537 base::Bind(&BluetoothChromeOSTest::Callback,
538 base::Unretained(this)),
539 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
540 base::Unretained(this)));
541 EXPECT_EQ(1, callback_count_);
542 EXPECT_EQ(0, error_callback_count_);
543 callback_count_ = 0;
545 ASSERT_TRUE(adapter_->IsPowered());
547 // Install an observer; expect the AdapterPoweredChanged to be called
548 // with false, and IsPowered() to return false.
549 TestObserver observer(adapter_);
551 adapter_->SetPowered(
552 false,
553 base::Bind(&BluetoothChromeOSTest::Callback,
554 base::Unretained(this)),
555 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
556 base::Unretained(this)));
557 EXPECT_EQ(1, callback_count_);
558 EXPECT_EQ(0, error_callback_count_);
560 EXPECT_EQ(1, observer.powered_changed_count_);
561 EXPECT_FALSE(observer.last_powered_);
563 EXPECT_FALSE(adapter_->IsPowered());
566 TEST_F(BluetoothChromeOSTest, ChangeAdapterName) {
567 GetAdapter();
569 static const std::string new_name(".__.");
571 adapter_->SetName(
572 new_name,
573 base::Bind(&BluetoothChromeOSTest::Callback,
574 base::Unretained(this)),
575 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
576 base::Unretained(this)));
577 EXPECT_EQ(1, callback_count_);
578 EXPECT_EQ(0, error_callback_count_);
580 EXPECT_EQ(new_name, adapter_->GetName());
583 TEST_F(BluetoothChromeOSTest, BecomeDiscoverable) {
584 GetAdapter();
585 ASSERT_FALSE(adapter_->IsDiscoverable());
587 // Install an observer; expect the AdapterDiscoverableChanged to be called
588 // with true, and IsDiscoverable() to return true.
589 TestObserver observer(adapter_);
591 adapter_->SetDiscoverable(
592 true,
593 base::Bind(&BluetoothChromeOSTest::Callback,
594 base::Unretained(this)),
595 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
596 base::Unretained(this)));
597 EXPECT_EQ(1, callback_count_);
598 EXPECT_EQ(0, error_callback_count_);
600 EXPECT_EQ(1, observer.discoverable_changed_count_);
602 EXPECT_TRUE(adapter_->IsDiscoverable());
605 TEST_F(BluetoothChromeOSTest, BecomeNotDiscoverable) {
606 GetAdapter();
607 adapter_->SetDiscoverable(
608 true,
609 base::Bind(&BluetoothChromeOSTest::Callback,
610 base::Unretained(this)),
611 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
612 base::Unretained(this)));
613 EXPECT_EQ(1, callback_count_);
614 EXPECT_EQ(0, error_callback_count_);
615 callback_count_ = 0;
617 ASSERT_TRUE(adapter_->IsDiscoverable());
619 // Install an observer; expect the AdapterDiscoverableChanged to be called
620 // with false, and IsDiscoverable() to return false.
621 TestObserver observer(adapter_);
623 adapter_->SetDiscoverable(
624 false,
625 base::Bind(&BluetoothChromeOSTest::Callback,
626 base::Unretained(this)),
627 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
628 base::Unretained(this)));
629 EXPECT_EQ(1, callback_count_);
630 EXPECT_EQ(0, error_callback_count_);
632 EXPECT_EQ(1, observer.discoverable_changed_count_);
634 EXPECT_FALSE(adapter_->IsDiscoverable());
637 TEST_F(BluetoothChromeOSTest, StopDiscovery) {
638 GetAdapter();
640 adapter_->SetPowered(
641 true,
642 base::Bind(&BluetoothChromeOSTest::Callback,
643 base::Unretained(this)),
644 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
645 base::Unretained(this)));
646 adapter_->StartDiscoverySession(
647 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
648 base::Unretained(this)),
649 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
650 base::Unretained(this)));
651 message_loop_.Run();
652 EXPECT_EQ(2, callback_count_);
653 EXPECT_EQ(0, error_callback_count_);
654 callback_count_ = 0;
656 ASSERT_TRUE(adapter_->IsPowered());
657 ASSERT_TRUE(adapter_->IsDiscovering());
658 ASSERT_EQ((size_t)1, discovery_sessions_.size());
659 ASSERT_TRUE(discovery_sessions_[0]->IsActive());
661 // Install an observer; aside from the callback, expect the
662 // AdapterDiscoveringChanged method to be called and no longer to be
663 // discovering,
664 TestObserver observer(adapter_);
666 discovery_sessions_[0]->Stop(
667 base::Bind(&BluetoothChromeOSTest::Callback,
668 base::Unretained(this)),
669 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
670 base::Unretained(this)));
671 message_loop_.Run();
672 EXPECT_EQ(1, callback_count_);
673 EXPECT_EQ(0, error_callback_count_);
675 EXPECT_EQ(1, observer.discovering_changed_count_);
676 EXPECT_FALSE(observer.last_discovering_);
678 EXPECT_FALSE(adapter_->IsDiscovering());
681 TEST_F(BluetoothChromeOSTest, Discovery) {
682 // Test a simulated discovery session.
683 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
684 GetAdapter();
686 TestObserver observer(adapter_);
688 adapter_->SetPowered(
689 true,
690 base::Bind(&BluetoothChromeOSTest::Callback,
691 base::Unretained(this)),
692 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
693 base::Unretained(this)));
694 adapter_->StartDiscoverySession(
695 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
696 base::Unretained(this)),
697 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
698 base::Unretained(this)));
699 message_loop_.Run();
700 EXPECT_EQ(2, callback_count_);
701 EXPECT_EQ(0, error_callback_count_);
702 callback_count_ = 0;
704 ASSERT_TRUE(adapter_->IsPowered());
705 ASSERT_TRUE(adapter_->IsDiscovering());
706 ASSERT_EQ((size_t)1, discovery_sessions_.size());
707 ASSERT_TRUE(discovery_sessions_[0]->IsActive());
709 // First device to appear.
710 message_loop_.Run();
712 EXPECT_EQ(1, observer.device_added_count_);
713 EXPECT_EQ(FakeBluetoothDeviceClient::kLegacyAutopairAddress,
714 observer.last_device_address_);
716 // Next we should get another two devices...
717 message_loop_.Run();
718 EXPECT_EQ(3, observer.device_added_count_);
720 // Okay, let's run forward until a device is actually removed...
721 while (!observer.device_removed_count_)
722 message_loop_.Run();
724 EXPECT_EQ(1, observer.device_removed_count_);
725 EXPECT_EQ(FakeBluetoothDeviceClient::kVanishingDeviceAddress,
726 observer.last_device_address_);
729 TEST_F(BluetoothChromeOSTest, PoweredAndDiscovering) {
730 GetAdapter();
731 adapter_->SetPowered(
732 true,
733 base::Bind(&BluetoothChromeOSTest::Callback,
734 base::Unretained(this)),
735 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
736 base::Unretained(this)));
737 adapter_->StartDiscoverySession(
738 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
739 base::Unretained(this)),
740 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
741 base::Unretained(this)));
742 message_loop_.Run();
743 EXPECT_EQ(2, callback_count_);
744 EXPECT_EQ(0, error_callback_count_);
745 callback_count_ = 0;
746 ASSERT_EQ((size_t)1, discovery_sessions_.size());
747 ASSERT_TRUE(discovery_sessions_[0]->IsActive());
749 // Stop the timers that the simulation uses
750 fake_bluetooth_device_client_->EndDiscoverySimulation(
751 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath));
753 ASSERT_TRUE(adapter_->IsPowered());
754 ASSERT_TRUE(adapter_->IsDiscovering());
756 fake_bluetooth_adapter_client_->SetVisible(false);
757 ASSERT_FALSE(adapter_->IsPresent());
758 ASSERT_FALSE(discovery_sessions_[0]->IsActive());
760 // Install an observer; expect the AdapterPresentChanged,
761 // AdapterPoweredChanged and AdapterDiscoveringChanged methods to be called
762 // with true, and IsPresent(), IsPowered() and IsDiscovering() to all
763 // return true.
764 TestObserver observer(adapter_);
766 fake_bluetooth_adapter_client_->SetVisible(true);
768 EXPECT_EQ(1, observer.present_changed_count_);
769 EXPECT_TRUE(observer.last_present_);
770 EXPECT_TRUE(adapter_->IsPresent());
772 EXPECT_EQ(1, observer.powered_changed_count_);
773 EXPECT_TRUE(observer.last_powered_);
774 EXPECT_TRUE(adapter_->IsPowered());
776 EXPECT_EQ(1, observer.discovering_changed_count_);
777 EXPECT_TRUE(observer.last_discovering_);
778 EXPECT_TRUE(adapter_->IsDiscovering());
780 observer.present_changed_count_ = 0;
781 observer.powered_changed_count_ = 0;
782 observer.discovering_changed_count_ = 0;
784 // Now mark the adapter not present again. Expect the methods to be called
785 // again, to reset the properties back to false
786 fake_bluetooth_adapter_client_->SetVisible(false);
788 EXPECT_EQ(1, observer.present_changed_count_);
789 EXPECT_FALSE(observer.last_present_);
790 EXPECT_FALSE(adapter_->IsPresent());
792 EXPECT_EQ(1, observer.powered_changed_count_);
793 EXPECT_FALSE(observer.last_powered_);
794 EXPECT_FALSE(adapter_->IsPowered());
796 EXPECT_EQ(1, observer.discovering_changed_count_);
797 EXPECT_FALSE(observer.last_discovering_);
798 EXPECT_FALSE(adapter_->IsDiscovering());
801 // This unit test asserts that the basic reference counting logic works
802 // correctly for discovery requests done via the BluetoothAdapter.
803 TEST_F(BluetoothChromeOSTest, MultipleDiscoverySessions) {
804 GetAdapter();
805 adapter_->SetPowered(
806 true,
807 base::Bind(&BluetoothChromeOSTest::Callback,
808 base::Unretained(this)),
809 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
810 base::Unretained(this)));
811 EXPECT_EQ(1, callback_count_);
812 EXPECT_EQ(0, error_callback_count_);
813 EXPECT_TRUE(adapter_->IsPowered());
814 callback_count_ = 0;
816 TestObserver observer(adapter_);
818 EXPECT_EQ(0, observer.discovering_changed_count_);
819 EXPECT_FALSE(observer.last_discovering_);
820 EXPECT_FALSE(adapter_->IsDiscovering());
822 // Request device discovery 3 times.
823 for (int i = 0; i < 3; i++) {
824 adapter_->StartDiscoverySession(
825 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
826 base::Unretained(this)),
827 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
828 base::Unretained(this)));
830 // Run only once, as there should have been one D-Bus call.
831 message_loop_.Run();
833 // The observer should have received the discovering changed event exactly
834 // once, the success callback should have been called 3 times and the adapter
835 // should be discovering.
836 EXPECT_EQ(1, observer.discovering_changed_count_);
837 EXPECT_EQ(3, callback_count_);
838 EXPECT_EQ(0, error_callback_count_);
839 EXPECT_TRUE(observer.last_discovering_);
840 EXPECT_TRUE(adapter_->IsDiscovering());
841 ASSERT_EQ((size_t)3, discovery_sessions_.size());
843 // Request to stop discovery twice.
844 for (int i = 0; i < 2; i++) {
845 discovery_sessions_[i]->Stop(
846 base::Bind(&BluetoothChromeOSTest::Callback,
847 base::Unretained(this)),
848 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
849 base::Unretained(this)));
852 // The observer should have received no additional discovering changed events,
853 // the success callback should have been called 2 times and the adapter should
854 // still be discovering.
855 EXPECT_EQ(1, observer.discovering_changed_count_);
856 EXPECT_EQ(5, callback_count_);
857 EXPECT_EQ(0, error_callback_count_);
858 EXPECT_TRUE(observer.last_discovering_);
859 EXPECT_TRUE(adapter_->IsDiscovering());
860 EXPECT_TRUE(adapter_->IsDiscovering());
861 EXPECT_FALSE(discovery_sessions_[0]->IsActive());
862 EXPECT_FALSE(discovery_sessions_[1]->IsActive());
863 EXPECT_TRUE(discovery_sessions_[2]->IsActive());
865 // Request device discovery 3 times.
866 for (int i = 0; i < 3; i++) {
867 adapter_->StartDiscoverySession(
868 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
869 base::Unretained(this)),
870 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
871 base::Unretained(this)));
874 // The observer should have received no additional discovering changed events,
875 // the success callback should have been called 3 times and the adapter should
876 // still be discovering.
877 EXPECT_EQ(1, observer.discovering_changed_count_);
878 EXPECT_EQ(8, callback_count_);
879 EXPECT_EQ(0, error_callback_count_);
880 EXPECT_TRUE(observer.last_discovering_);
881 EXPECT_TRUE(adapter_->IsDiscovering());
882 ASSERT_EQ((size_t)6, discovery_sessions_.size());
884 // Request to stop discovery 4 times.
885 for (int i = 2; i < 6; i++) {
886 discovery_sessions_[i]->Stop(
887 base::Bind(&BluetoothChromeOSTest::Callback,
888 base::Unretained(this)),
889 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
890 base::Unretained(this)));
892 // Run only once, as there should have been one D-Bus call.
893 message_loop_.Run();
895 // The observer should have received the discovering changed event exactly
896 // once, the success callback should have been called 4 times and the adapter
897 // should no longer be discovering.
898 EXPECT_EQ(2, observer.discovering_changed_count_);
899 EXPECT_EQ(12, callback_count_);
900 EXPECT_EQ(0, error_callback_count_);
901 EXPECT_FALSE(observer.last_discovering_);
902 EXPECT_FALSE(adapter_->IsDiscovering());
904 // All discovery sessions should be inactive.
905 for (int i = 0; i < 6; i++)
906 EXPECT_FALSE(discovery_sessions_[i]->IsActive());
908 // Request to stop discovery on of the inactive sessions.
909 discovery_sessions_[0]->Stop(
910 base::Bind(&BluetoothChromeOSTest::Callback,
911 base::Unretained(this)),
912 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
913 base::Unretained(this)));
915 // The call should have failed.
916 EXPECT_EQ(2, observer.discovering_changed_count_);
917 EXPECT_EQ(12, callback_count_);
918 EXPECT_EQ(1, error_callback_count_);
919 EXPECT_FALSE(observer.last_discovering_);
920 EXPECT_FALSE(adapter_->IsDiscovering());
923 // This unit test asserts that the reference counting logic works correctly in
924 // the cases when the adapter gets reset and D-Bus calls are made outside of
925 // the BluetoothAdapter.
926 TEST_F(BluetoothChromeOSTest,
927 UnexpectedChangesDuringMultipleDiscoverySessions) {
928 GetAdapter();
929 adapter_->SetPowered(
930 true,
931 base::Bind(&BluetoothChromeOSTest::Callback,
932 base::Unretained(this)),
933 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
934 base::Unretained(this)));
935 EXPECT_EQ(1, callback_count_);
936 EXPECT_EQ(0, error_callback_count_);
937 EXPECT_TRUE(adapter_->IsPowered());
938 callback_count_ = 0;
940 TestObserver observer(adapter_);
942 EXPECT_EQ(0, observer.discovering_changed_count_);
943 EXPECT_FALSE(observer.last_discovering_);
944 EXPECT_FALSE(adapter_->IsDiscovering());
946 // Request device discovery 3 times.
947 for (int i = 0; i < 3; i++) {
948 adapter_->StartDiscoverySession(
949 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
950 base::Unretained(this)),
951 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
952 base::Unretained(this)));
954 // Run only once, as there should have been one D-Bus call.
955 message_loop_.Run();
957 // The observer should have received the discovering changed event exactly
958 // once, the success callback should have been called 3 times and the adapter
959 // should be discovering.
960 EXPECT_EQ(1, observer.discovering_changed_count_);
961 EXPECT_EQ(3, callback_count_);
962 EXPECT_EQ(0, error_callback_count_);
963 EXPECT_TRUE(observer.last_discovering_);
964 EXPECT_TRUE(adapter_->IsDiscovering());
965 ASSERT_EQ((size_t)3, discovery_sessions_.size());
967 for (int i = 0; i < 3; i++)
968 EXPECT_TRUE(discovery_sessions_[i]->IsActive());
970 // Stop the timers that the simulation uses
971 fake_bluetooth_device_client_->EndDiscoverySimulation(
972 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath));
974 ASSERT_TRUE(adapter_->IsPowered());
975 ASSERT_TRUE(adapter_->IsDiscovering());
977 // Stop device discovery behind the adapter. The adapter and the observer
978 // should be notified of the change and the reference count should be reset.
979 // Even though FakeBluetoothAdapterClient does its own reference counting and
980 // we called 3 BluetoothAdapter::StartDiscoverySession 3 times, the
981 // FakeBluetoothAdapterClient's count should be only 1 and a single call to
982 // FakeBluetoothAdapterClient::StopDiscovery should work.
983 fake_bluetooth_adapter_client_->StopDiscovery(
984 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
985 base::Bind(&BluetoothChromeOSTest::Callback,
986 base::Unretained(this)),
987 base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
988 base::Unretained(this)));
989 message_loop_.Run();
990 EXPECT_EQ(2, observer.discovering_changed_count_);
991 EXPECT_EQ(4, callback_count_);
992 EXPECT_EQ(0, error_callback_count_);
993 EXPECT_FALSE(observer.last_discovering_);
994 EXPECT_FALSE(adapter_->IsDiscovering());
996 // All discovery session instances should have been updated.
997 for (int i = 0; i < 3; i++)
998 EXPECT_FALSE(discovery_sessions_[i]->IsActive());
999 discovery_sessions_.clear();
1001 // It should be possible to successfully start discovery.
1002 for (int i = 0; i < 2; i++) {
1003 adapter_->StartDiscoverySession(
1004 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1005 base::Unretained(this)),
1006 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1007 base::Unretained(this)));
1009 // Run only once, as there should have been one D-Bus call.
1010 message_loop_.Run();
1011 EXPECT_EQ(3, observer.discovering_changed_count_);
1012 EXPECT_EQ(6, callback_count_);
1013 EXPECT_EQ(0, error_callback_count_);
1014 EXPECT_TRUE(observer.last_discovering_);
1015 EXPECT_TRUE(adapter_->IsDiscovering());
1016 ASSERT_EQ((size_t)2, discovery_sessions_.size());
1018 for (int i = 0; i < 2; i++)
1019 EXPECT_TRUE(discovery_sessions_[i]->IsActive());
1021 fake_bluetooth_device_client_->EndDiscoverySimulation(
1022 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath));
1024 // Make the adapter disappear and appear. This will make it come back as
1025 // discovering. When this happens, the reference count should become and
1026 // remain 0 as no new request was made through the BluetoothAdapter.
1027 fake_bluetooth_adapter_client_->SetVisible(false);
1028 ASSERT_FALSE(adapter_->IsPresent());
1029 EXPECT_EQ(4, observer.discovering_changed_count_);
1030 EXPECT_EQ(6, callback_count_);
1031 EXPECT_EQ(0, error_callback_count_);
1032 EXPECT_FALSE(observer.last_discovering_);
1033 EXPECT_FALSE(adapter_->IsDiscovering());
1035 for (int i = 0; i < 2; i++)
1036 EXPECT_FALSE(discovery_sessions_[i]->IsActive());
1037 discovery_sessions_.clear();
1039 fake_bluetooth_adapter_client_->SetVisible(true);
1040 ASSERT_TRUE(adapter_->IsPresent());
1041 EXPECT_EQ(5, observer.discovering_changed_count_);
1042 EXPECT_EQ(6, callback_count_);
1043 EXPECT_EQ(0, error_callback_count_);
1044 EXPECT_TRUE(observer.last_discovering_);
1045 EXPECT_TRUE(adapter_->IsDiscovering());
1047 // Start and stop discovery. At this point, FakeBluetoothAdapterClient has
1048 // a reference count that is equal to 1. Pretend that this was done by an
1049 // application other than us. Starting and stopping discovery will succeed
1050 // but it won't cause the discovery state to change.
1051 adapter_->StartDiscoverySession(
1052 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1053 base::Unretained(this)),
1054 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1055 base::Unretained(this)));
1056 message_loop_.Run(); // Run the loop, as there should have been a D-Bus call.
1057 EXPECT_EQ(5, observer.discovering_changed_count_);
1058 EXPECT_EQ(7, callback_count_);
1059 EXPECT_EQ(0, error_callback_count_);
1060 EXPECT_TRUE(observer.last_discovering_);
1061 EXPECT_TRUE(adapter_->IsDiscovering());
1062 ASSERT_EQ((size_t)1, discovery_sessions_.size());
1063 EXPECT_TRUE(discovery_sessions_[0]->IsActive());
1065 discovery_sessions_[0]->Stop(
1066 base::Bind(&BluetoothChromeOSTest::Callback,
1067 base::Unretained(this)),
1068 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1069 base::Unretained(this)));
1070 message_loop_.Run(); // Run the loop, as there should have been a D-Bus call.
1071 EXPECT_EQ(5, observer.discovering_changed_count_);
1072 EXPECT_EQ(8, callback_count_);
1073 EXPECT_EQ(0, error_callback_count_);
1074 EXPECT_TRUE(observer.last_discovering_);
1075 EXPECT_TRUE(adapter_->IsDiscovering());
1076 EXPECT_FALSE(discovery_sessions_[0]->IsActive());
1077 discovery_sessions_.clear();
1079 // Start discovery again.
1080 adapter_->StartDiscoverySession(
1081 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1082 base::Unretained(this)),
1083 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1084 base::Unretained(this)));
1085 message_loop_.Run(); // Run the loop, as there should have been a D-Bus call.
1086 EXPECT_EQ(5, observer.discovering_changed_count_);
1087 EXPECT_EQ(9, callback_count_);
1088 EXPECT_EQ(0, error_callback_count_);
1089 EXPECT_TRUE(observer.last_discovering_);
1090 EXPECT_TRUE(adapter_->IsDiscovering());
1091 ASSERT_EQ((size_t)1, discovery_sessions_.size());
1092 EXPECT_TRUE(discovery_sessions_[0]->IsActive());
1094 // Stop discovery via D-Bus. The fake client's reference count will drop but
1095 // the discovery state won't change since our BluetoothAdapter also just
1096 // requested it via D-Bus.
1097 fake_bluetooth_adapter_client_->StopDiscovery(
1098 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
1099 base::Bind(&BluetoothChromeOSTest::Callback,
1100 base::Unretained(this)),
1101 base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
1102 base::Unretained(this)));
1103 message_loop_.Run();
1104 EXPECT_EQ(5, observer.discovering_changed_count_);
1105 EXPECT_EQ(10, callback_count_);
1106 EXPECT_EQ(0, error_callback_count_);
1107 EXPECT_TRUE(observer.last_discovering_);
1108 EXPECT_TRUE(adapter_->IsDiscovering());
1110 // Now end the discovery session. This should change the adapter's discovery
1111 // state.
1112 discovery_sessions_[0]->Stop(
1113 base::Bind(&BluetoothChromeOSTest::Callback,
1114 base::Unretained(this)),
1115 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1116 base::Unretained(this)));
1117 message_loop_.Run();
1118 EXPECT_EQ(6, observer.discovering_changed_count_);
1119 EXPECT_EQ(11, callback_count_);
1120 EXPECT_EQ(0, error_callback_count_);
1121 EXPECT_FALSE(observer.last_discovering_);
1122 EXPECT_FALSE(adapter_->IsDiscovering());
1123 EXPECT_FALSE(discovery_sessions_[0]->IsActive());
1126 TEST_F(BluetoothChromeOSTest, InvalidatedDiscoverySessions) {
1127 GetAdapter();
1128 adapter_->SetPowered(
1129 true,
1130 base::Bind(&BluetoothChromeOSTest::Callback,
1131 base::Unretained(this)),
1132 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1133 base::Unretained(this)));
1134 EXPECT_EQ(1, callback_count_);
1135 EXPECT_EQ(0, error_callback_count_);
1136 EXPECT_TRUE(adapter_->IsPowered());
1137 callback_count_ = 0;
1139 TestObserver observer(adapter_);
1141 EXPECT_EQ(0, observer.discovering_changed_count_);
1142 EXPECT_FALSE(observer.last_discovering_);
1143 EXPECT_FALSE(adapter_->IsDiscovering());
1145 // Request device discovery 3 times.
1146 for (int i = 0; i < 3; i++) {
1147 adapter_->StartDiscoverySession(
1148 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1149 base::Unretained(this)),
1150 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1151 base::Unretained(this)));
1153 // Run only once, as there should have been one D-Bus call.
1154 message_loop_.Run();
1156 // The observer should have received the discovering changed event exactly
1157 // once, the success callback should have been called 3 times and the adapter
1158 // should be discovering.
1159 EXPECT_EQ(1, observer.discovering_changed_count_);
1160 EXPECT_EQ(3, callback_count_);
1161 EXPECT_EQ(0, error_callback_count_);
1162 EXPECT_TRUE(observer.last_discovering_);
1163 EXPECT_TRUE(adapter_->IsDiscovering());
1164 ASSERT_EQ((size_t)3, discovery_sessions_.size());
1166 for (int i = 0; i < 3; i++)
1167 EXPECT_TRUE(discovery_sessions_[i]->IsActive());
1169 // Stop the timers that the simulation uses
1170 fake_bluetooth_device_client_->EndDiscoverySimulation(
1171 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath));
1173 ASSERT_TRUE(adapter_->IsPowered());
1174 ASSERT_TRUE(adapter_->IsDiscovering());
1176 // Delete all but one discovery session.
1177 discovery_sessions_.pop_back();
1178 discovery_sessions_.pop_back();
1179 ASSERT_EQ((size_t)1, discovery_sessions_.size());
1180 EXPECT_TRUE(discovery_sessions_[0]->IsActive());
1181 EXPECT_TRUE(adapter_->IsDiscovering());
1183 // Stop device discovery behind the adapter. The one active discovery session
1184 // should become inactive, but more importantly, we shouldn't run into any
1185 // memory errors as the sessions that we explicitly deleted should get
1186 // cleaned up.
1187 fake_bluetooth_adapter_client_->StopDiscovery(
1188 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
1189 base::Bind(&BluetoothChromeOSTest::Callback,
1190 base::Unretained(this)),
1191 base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
1192 base::Unretained(this)));
1193 message_loop_.Run();
1194 EXPECT_EQ(2, observer.discovering_changed_count_);
1195 EXPECT_EQ(4, callback_count_);
1196 EXPECT_EQ(0, error_callback_count_);
1197 EXPECT_FALSE(observer.last_discovering_);
1198 EXPECT_FALSE(adapter_->IsDiscovering());
1199 EXPECT_FALSE(discovery_sessions_[0]->IsActive());
1202 TEST_F(BluetoothChromeOSTest, QueuedDiscoveryRequests) {
1203 GetAdapter();
1205 adapter_->SetPowered(
1206 true,
1207 base::Bind(&BluetoothChromeOSTest::Callback,
1208 base::Unretained(this)),
1209 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1210 base::Unretained(this)));
1211 EXPECT_EQ(1, callback_count_);
1212 EXPECT_EQ(0, error_callback_count_);
1213 EXPECT_TRUE(adapter_->IsPowered());
1214 callback_count_ = 0;
1216 TestObserver observer(adapter_);
1218 EXPECT_EQ(0, observer.discovering_changed_count_);
1219 EXPECT_FALSE(observer.last_discovering_);
1220 EXPECT_FALSE(adapter_->IsDiscovering());
1222 // Request to start discovery. The call should be pending.
1223 adapter_->StartDiscoverySession(
1224 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1225 base::Unretained(this)),
1226 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1227 base::Unretained(this)));
1228 EXPECT_EQ(0, callback_count_);
1230 fake_bluetooth_device_client_->EndDiscoverySimulation(
1231 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath));
1233 // The underlying adapter has started discovery, but our call hasn't returned
1234 // yet.
1235 EXPECT_EQ(1, observer.discovering_changed_count_);
1236 EXPECT_TRUE(observer.last_discovering_);
1237 EXPECT_TRUE(adapter_->IsDiscovering());
1238 EXPECT_TRUE(discovery_sessions_.empty());
1240 // Request to start discovery twice. These should get queued and there should
1241 // be no change in state.
1242 for (int i = 0; i < 2; i++) {
1243 adapter_->StartDiscoverySession(
1244 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1245 base::Unretained(this)),
1246 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1247 base::Unretained(this)));
1249 EXPECT_EQ(0, callback_count_);
1250 EXPECT_EQ(0, error_callback_count_);
1251 EXPECT_EQ(1, observer.discovering_changed_count_);
1252 EXPECT_TRUE(observer.last_discovering_);
1253 EXPECT_TRUE(adapter_->IsDiscovering());
1254 EXPECT_TRUE(discovery_sessions_.empty());
1256 // Process the pending call. The queued calls should execute and the discovery
1257 // session reference count should increase.
1258 message_loop_.Run();
1259 EXPECT_EQ(3, callback_count_);
1260 EXPECT_EQ(0, error_callback_count_);
1261 EXPECT_EQ(1, observer.discovering_changed_count_);
1262 EXPECT_TRUE(observer.last_discovering_);
1263 EXPECT_TRUE(adapter_->IsDiscovering());
1264 ASSERT_EQ((size_t)3, discovery_sessions_.size());
1266 // Verify the reference count by removing sessions 3 times. The last request
1267 // should remain pending.
1268 for (int i = 0; i < 3; i++) {
1269 discovery_sessions_[i]->Stop(
1270 base::Bind(&BluetoothChromeOSTest::Callback,
1271 base::Unretained(this)),
1272 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1273 base::Unretained(this)));
1275 EXPECT_EQ(5, callback_count_);
1276 EXPECT_EQ(0, error_callback_count_);
1277 EXPECT_EQ(2, observer.discovering_changed_count_);
1278 EXPECT_FALSE(observer.last_discovering_);
1279 EXPECT_FALSE(adapter_->IsDiscovering());
1280 EXPECT_FALSE(discovery_sessions_[0]->IsActive());
1281 EXPECT_FALSE(discovery_sessions_[1]->IsActive());
1282 EXPECT_TRUE(discovery_sessions_[2]->IsActive());
1284 // Request to stop the session whose call is pending should fail.
1285 discovery_sessions_[2]->Stop(
1286 base::Bind(&BluetoothChromeOSTest::Callback,
1287 base::Unretained(this)),
1288 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1289 base::Unretained(this)));
1290 EXPECT_EQ(5, callback_count_);
1291 EXPECT_EQ(1, error_callback_count_);
1292 EXPECT_EQ(2, observer.discovering_changed_count_);
1293 EXPECT_FALSE(observer.last_discovering_);
1294 EXPECT_FALSE(adapter_->IsDiscovering());
1295 EXPECT_TRUE(discovery_sessions_[2]->IsActive());
1297 // Request to start should get queued.
1298 adapter_->StartDiscoverySession(
1299 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1300 base::Unretained(this)),
1301 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1302 base::Unretained(this)));
1303 EXPECT_EQ(5, callback_count_);
1304 EXPECT_EQ(1, error_callback_count_);
1305 EXPECT_EQ(2, observer.discovering_changed_count_);
1306 EXPECT_FALSE(observer.last_discovering_);
1307 EXPECT_FALSE(adapter_->IsDiscovering());
1308 ASSERT_EQ((size_t)3, discovery_sessions_.size());
1310 // Run the pending request.
1311 message_loop_.Run();
1312 EXPECT_EQ(6, callback_count_);
1313 EXPECT_EQ(1, error_callback_count_);
1314 EXPECT_EQ(3, observer.discovering_changed_count_);
1315 EXPECT_TRUE(observer.last_discovering_);
1316 EXPECT_TRUE(adapter_->IsDiscovering());
1317 ASSERT_EQ((size_t)3, discovery_sessions_.size());
1318 EXPECT_FALSE(discovery_sessions_[2]->IsActive());
1320 // The queued request to start discovery should have been issued but is still
1321 // pending. Run the loop and verify.
1322 message_loop_.Run();
1323 EXPECT_EQ(7, callback_count_);
1324 EXPECT_EQ(1, error_callback_count_);
1325 EXPECT_EQ(3, observer.discovering_changed_count_);
1326 EXPECT_TRUE(observer.last_discovering_);
1327 EXPECT_TRUE(adapter_->IsDiscovering());
1328 ASSERT_EQ((size_t)4, discovery_sessions_.size());
1329 EXPECT_TRUE(discovery_sessions_[3]->IsActive());
1332 TEST_F(BluetoothChromeOSTest, StartDiscoverySession) {
1333 GetAdapter();
1335 adapter_->SetPowered(
1336 true,
1337 base::Bind(&BluetoothChromeOSTest::Callback,
1338 base::Unretained(this)),
1339 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1340 base::Unretained(this)));
1341 EXPECT_EQ(1, callback_count_);
1342 EXPECT_EQ(0, error_callback_count_);
1343 EXPECT_TRUE(adapter_->IsPowered());
1344 callback_count_ = 0;
1346 TestObserver observer(adapter_);
1348 EXPECT_EQ(0, observer.discovering_changed_count_);
1349 EXPECT_FALSE(observer.last_discovering_);
1350 EXPECT_FALSE(adapter_->IsDiscovering());
1351 EXPECT_TRUE(discovery_sessions_.empty());
1353 // Request a new discovery session.
1354 adapter_->StartDiscoverySession(
1355 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1356 base::Unretained(this)),
1357 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1358 base::Unretained(this)));
1359 message_loop_.Run();
1360 EXPECT_EQ(1, observer.discovering_changed_count_);
1361 EXPECT_EQ(1, callback_count_);
1362 EXPECT_EQ(0, error_callback_count_);
1363 EXPECT_TRUE(observer.last_discovering_);
1364 EXPECT_TRUE(adapter_->IsDiscovering());
1365 ASSERT_EQ((size_t)1, discovery_sessions_.size());
1366 EXPECT_TRUE(discovery_sessions_[0]->IsActive());
1368 // Start another session. A new one should be returned in the callback, which
1369 // in turn will destroy the previous session. Adapter should still be
1370 // discovering and the reference count should be 1.
1371 adapter_->StartDiscoverySession(
1372 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1373 base::Unretained(this)),
1374 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1375 base::Unretained(this)));
1376 message_loop_.Run();
1377 EXPECT_EQ(1, observer.discovering_changed_count_);
1378 EXPECT_EQ(2, callback_count_);
1379 EXPECT_EQ(0, error_callback_count_);
1380 EXPECT_TRUE(observer.last_discovering_);
1381 EXPECT_TRUE(adapter_->IsDiscovering());
1382 ASSERT_EQ((size_t)2, discovery_sessions_.size());
1383 EXPECT_TRUE(discovery_sessions_[0]->IsActive());
1385 // Request a new session.
1386 adapter_->StartDiscoverySession(
1387 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1388 base::Unretained(this)),
1389 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1390 base::Unretained(this)));
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(
1404 base::Bind(&BluetoothChromeOSTest::Callback,
1405 base::Unretained(this)),
1406 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1407 base::Unretained(this)));
1408 message_loop_.Run();
1409 EXPECT_EQ(1, observer.discovering_changed_count_);
1410 EXPECT_EQ(4, callback_count_);
1411 EXPECT_EQ(0, error_callback_count_);
1412 EXPECT_TRUE(observer.last_discovering_);
1413 EXPECT_TRUE(adapter_->IsDiscovering());
1414 ASSERT_EQ((size_t)3, discovery_sessions_.size());
1415 EXPECT_FALSE(discovery_sessions_[0]->IsActive());
1416 EXPECT_TRUE(discovery_sessions_[1]->IsActive());
1418 // Delete the current active session. Discovery should eventually stop.
1419 discovery_sessions_.clear();
1420 while (observer.last_discovering_)
1421 message_loop_.RunUntilIdle();
1423 EXPECT_EQ(2, observer.discovering_changed_count_);
1424 EXPECT_EQ(4, callback_count_);
1425 EXPECT_EQ(0, error_callback_count_);
1426 EXPECT_FALSE(observer.last_discovering_);
1427 EXPECT_FALSE(adapter_->IsDiscovering());
1430 TEST_F(BluetoothChromeOSTest, DeviceProperties) {
1431 GetAdapter();
1433 BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
1434 ASSERT_EQ(1U, devices.size());
1435 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
1436 devices[0]->GetAddress());
1438 // Verify the other device properties.
1439 EXPECT_EQ(base::UTF8ToUTF16(FakeBluetoothDeviceClient::kPairedDeviceName),
1440 devices[0]->GetName());
1441 EXPECT_EQ(BluetoothDevice::DEVICE_COMPUTER, devices[0]->GetDeviceType());
1442 EXPECT_TRUE(devices[0]->IsPaired());
1443 EXPECT_FALSE(devices[0]->IsConnected());
1444 EXPECT_FALSE(devices[0]->IsConnecting());
1446 // Non HID devices are always connectable.
1447 EXPECT_TRUE(devices[0]->IsConnectable());
1449 BluetoothDevice::ServiceList uuids = devices[0]->GetServices();
1450 ASSERT_EQ(2U, uuids.size());
1451 EXPECT_EQ(uuids[0], "00001800-0000-1000-8000-00805f9b34fb");
1452 EXPECT_EQ(uuids[1], "00001801-0000-1000-8000-00805f9b34fb");
1454 EXPECT_EQ(BluetoothDevice::VENDOR_ID_USB, devices[0]->GetVendorIDSource());
1455 EXPECT_EQ(0x05ac, devices[0]->GetVendorID());
1456 EXPECT_EQ(0x030d, devices[0]->GetProductID());
1457 EXPECT_EQ(0x0306, devices[0]->GetDeviceID());
1460 TEST_F(BluetoothChromeOSTest, DeviceClassChanged) {
1461 // Simulate a change of class of a device, as sometimes occurs
1462 // during discovery.
1463 GetAdapter();
1465 BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
1466 ASSERT_EQ(1U, devices.size());
1467 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
1468 devices[0]->GetAddress());
1469 ASSERT_EQ(BluetoothDevice::DEVICE_COMPUTER, devices[0]->GetDeviceType());
1471 // Install an observer; expect the DeviceChanged method to be called when
1472 // we change the class of the device.
1473 TestObserver observer(adapter_);
1475 FakeBluetoothDeviceClient::Properties* properties =
1476 fake_bluetooth_device_client_->GetProperties(
1477 dbus::ObjectPath(FakeBluetoothDeviceClient::kPairedDevicePath));
1479 properties->bluetooth_class.ReplaceValue(0x002580);
1481 EXPECT_EQ(1, observer.device_changed_count_);
1482 EXPECT_EQ(devices[0], observer.last_device_);
1484 EXPECT_EQ(BluetoothDevice::DEVICE_MOUSE, devices[0]->GetDeviceType());
1487 TEST_F(BluetoothChromeOSTest, DeviceNameChanged) {
1488 // Simulate a change of name of a device.
1489 GetAdapter();
1491 BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
1492 ASSERT_EQ(1U, devices.size());
1493 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
1494 devices[0]->GetAddress());
1495 ASSERT_EQ(base::UTF8ToUTF16(FakeBluetoothDeviceClient::kPairedDeviceName),
1496 devices[0]->GetName());
1498 // Install an observer; expect the DeviceChanged method to be called when
1499 // we change the alias of the device.
1500 TestObserver observer(adapter_);
1502 FakeBluetoothDeviceClient::Properties* properties =
1503 fake_bluetooth_device_client_->GetProperties(
1504 dbus::ObjectPath(FakeBluetoothDeviceClient::kPairedDevicePath));
1506 static const std::string new_name("New Device Name");
1507 properties->alias.ReplaceValue(new_name);
1509 EXPECT_EQ(1, observer.device_changed_count_);
1510 EXPECT_EQ(devices[0], observer.last_device_);
1512 EXPECT_EQ(base::UTF8ToUTF16(new_name), devices[0]->GetName());
1515 TEST_F(BluetoothChromeOSTest, DeviceUuidsChanged) {
1516 // Simulate a change of advertised services of a device.
1517 GetAdapter();
1519 BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
1520 ASSERT_EQ(1U, devices.size());
1521 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
1522 devices[0]->GetAddress());
1524 BluetoothDevice::ServiceList uuids = devices[0]->GetServices();
1525 ASSERT_EQ(2U, uuids.size());
1526 ASSERT_EQ(uuids[0], "00001800-0000-1000-8000-00805f9b34fb");
1527 ASSERT_EQ(uuids[1], "00001801-0000-1000-8000-00805f9b34fb");
1529 // Install an observer; expect the DeviceChanged method to be called when
1530 // we change the class of the device.
1531 TestObserver observer(adapter_);
1533 FakeBluetoothDeviceClient::Properties* properties =
1534 fake_bluetooth_device_client_->GetProperties(
1535 dbus::ObjectPath(FakeBluetoothDeviceClient::kPairedDevicePath));
1537 uuids.push_back("0000110c-0000-1000-8000-00805f9b34fb");
1538 uuids.push_back("0000110e-0000-1000-8000-00805f9b34fb");
1539 uuids.push_back("0000110a-0000-1000-8000-00805f9b34fb");
1541 properties->uuids.ReplaceValue(uuids);
1543 EXPECT_EQ(1, observer.device_changed_count_);
1544 EXPECT_EQ(devices[0], observer.last_device_);
1546 // Fetching the value should give the new one.
1547 uuids = devices[0]->GetServices();
1548 ASSERT_EQ(5U, uuids.size());
1549 EXPECT_EQ(uuids[0], "00001800-0000-1000-8000-00805f9b34fb");
1550 EXPECT_EQ(uuids[1], "00001801-0000-1000-8000-00805f9b34fb");
1551 EXPECT_EQ(uuids[2], "0000110c-0000-1000-8000-00805f9b34fb");
1552 EXPECT_EQ(uuids[3], "0000110e-0000-1000-8000-00805f9b34fb");
1553 EXPECT_EQ(uuids[4], "0000110a-0000-1000-8000-00805f9b34fb");
1556 TEST_F(BluetoothChromeOSTest, ForgetDevice) {
1557 GetAdapter();
1559 BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
1560 ASSERT_EQ(1U, devices.size());
1561 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
1562 devices[0]->GetAddress());
1564 std::string address = devices[0]->GetAddress();
1566 // Install an observer; expect the DeviceRemoved method to be called
1567 // with the device we remove.
1568 TestObserver observer(adapter_);
1570 devices[0]->Forget(
1571 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1572 base::Unretained(this)));
1573 EXPECT_EQ(0, error_callback_count_);
1575 EXPECT_EQ(1, observer.device_removed_count_);
1576 EXPECT_EQ(address, observer.last_device_address_);
1578 // GetDevices shouldn't return the device either.
1579 devices = adapter_->GetDevices();
1580 ASSERT_EQ(0U, devices.size());
1583 TEST_F(BluetoothChromeOSTest, ForgetUnpairedDevice) {
1584 GetAdapter();
1585 DiscoverDevices();
1587 BluetoothDevice* device = adapter_->GetDevice(
1588 FakeBluetoothDeviceClient::kConnectUnpairableAddress);
1589 ASSERT_TRUE(device != NULL);
1590 ASSERT_FALSE(device->IsPaired());
1592 // Connect the device so it becomes trusted and remembered.
1593 device->Connect(
1594 NULL,
1595 base::Bind(&BluetoothChromeOSTest::Callback,
1596 base::Unretained(this)),
1597 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
1598 base::Unretained(this)));
1600 ASSERT_EQ(1, callback_count_);
1601 ASSERT_EQ(0, error_callback_count_);
1602 callback_count_ = 0;
1604 ASSERT_TRUE(device->IsConnected());
1605 ASSERT_FALSE(device->IsConnecting());
1607 // Make sure the trusted property has been set to true.
1608 FakeBluetoothDeviceClient::Properties* properties =
1609 fake_bluetooth_device_client_->GetProperties(
1610 dbus::ObjectPath(FakeBluetoothDeviceClient::kConnectUnpairablePath));
1611 ASSERT_TRUE(properties->trusted.value());
1613 // Install an observer; expect the DeviceRemoved method to be called
1614 // with the device we remove.
1615 TestObserver observer(adapter_);
1617 device->Forget(
1618 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1619 base::Unretained(this)));
1620 EXPECT_EQ(0, error_callback_count_);
1622 EXPECT_EQ(1, observer.device_removed_count_);
1623 EXPECT_EQ(FakeBluetoothDeviceClient::kConnectUnpairableAddress,
1624 observer.last_device_address_);
1626 // GetDevices shouldn't return the device either.
1627 device = adapter_->GetDevice(
1628 FakeBluetoothDeviceClient::kConnectUnpairableAddress);
1629 EXPECT_FALSE(device != NULL);
1632 TEST_F(BluetoothChromeOSTest, ConnectPairedDevice) {
1633 GetAdapter();
1635 BluetoothDevice* device = adapter_->GetDevice(
1636 FakeBluetoothDeviceClient::kPairedDeviceAddress);
1637 ASSERT_TRUE(device != NULL);
1638 ASSERT_TRUE(device->IsPaired());
1640 TestObserver observer(adapter_);
1642 // Connect without a pairing delegate; since the device is already Paired
1643 // this should succeed and the device should become connected.
1644 device->Connect(
1645 NULL,
1646 base::Bind(&BluetoothChromeOSTest::Callback,
1647 base::Unretained(this)),
1648 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
1649 base::Unretained(this)));
1651 EXPECT_EQ(1, callback_count_);
1652 EXPECT_EQ(0, error_callback_count_);
1654 // Two changes for connecting, one for connected and one for for trusted
1655 // after connecting.
1656 EXPECT_EQ(4, observer.device_changed_count_);
1657 EXPECT_EQ(device, observer.last_device_);
1659 EXPECT_TRUE(device->IsConnected());
1660 EXPECT_FALSE(device->IsConnecting());
1663 TEST_F(BluetoothChromeOSTest, ConnectUnpairableDevice) {
1664 GetAdapter();
1665 DiscoverDevices();
1667 BluetoothDevice* device = adapter_->GetDevice(
1668 FakeBluetoothDeviceClient::kConnectUnpairableAddress);
1669 ASSERT_TRUE(device != NULL);
1670 ASSERT_FALSE(device->IsPaired());
1672 TestObserver observer(adapter_);
1674 // Connect without a pairing delegate; since the device does not require
1675 // pairing, this should succeed and the device should become connected.
1676 device->Connect(
1677 NULL,
1678 base::Bind(&BluetoothChromeOSTest::Callback,
1679 base::Unretained(this)),
1680 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
1681 base::Unretained(this)));
1683 EXPECT_EQ(1, callback_count_);
1684 EXPECT_EQ(0, error_callback_count_);
1686 // Two changes for connecting, one for connected, one for for trusted after
1687 // connection, and one for the reconnect mode (IsConnectable).
1688 EXPECT_EQ(5, observer.device_changed_count_);
1689 EXPECT_EQ(device, observer.last_device_);
1691 EXPECT_TRUE(device->IsConnected());
1692 EXPECT_FALSE(device->IsConnecting());
1694 // Make sure the trusted property has been set to true.
1695 FakeBluetoothDeviceClient::Properties* properties =
1696 fake_bluetooth_device_client_->GetProperties(
1697 dbus::ObjectPath(FakeBluetoothDeviceClient::kConnectUnpairablePath));
1698 EXPECT_TRUE(properties->trusted.value());
1700 // Verify is a HID device and is not connectable.
1701 BluetoothDevice::ServiceList uuids = device->GetServices();
1702 ASSERT_EQ(1U, uuids.size());
1703 EXPECT_EQ(uuids[0], "00001124-0000-1000-8000-00805f9b34fb");
1704 EXPECT_FALSE(device->IsConnectable());
1707 TEST_F(BluetoothChromeOSTest, ConnectConnectedDevice) {
1708 GetAdapter();
1710 BluetoothDevice* device = adapter_->GetDevice(
1711 FakeBluetoothDeviceClient::kPairedDeviceAddress);
1712 ASSERT_TRUE(device != NULL);
1713 ASSERT_TRUE(device->IsPaired());
1715 device->Connect(
1716 NULL,
1717 base::Bind(&BluetoothChromeOSTest::Callback,
1718 base::Unretained(this)),
1719 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
1720 base::Unretained(this)));
1722 ASSERT_EQ(1, callback_count_);
1723 ASSERT_EQ(0, error_callback_count_);
1724 callback_count_ = 0;
1726 ASSERT_TRUE(device->IsConnected());
1728 // Connect again; since the device is already Connected, this shouldn't do
1729 // anything to initiate the connection.
1730 TestObserver observer(adapter_);
1732 device->Connect(
1733 NULL,
1734 base::Bind(&BluetoothChromeOSTest::Callback,
1735 base::Unretained(this)),
1736 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
1737 base::Unretained(this)));
1739 EXPECT_EQ(1, callback_count_);
1740 EXPECT_EQ(0, error_callback_count_);
1742 // The observer will be called because Connecting will toggle true and false,
1743 // and the trusted property will be updated to true.
1744 EXPECT_EQ(3, observer.device_changed_count_);
1746 EXPECT_TRUE(device->IsConnected());
1747 EXPECT_FALSE(device->IsConnecting());
1750 TEST_F(BluetoothChromeOSTest, ConnectDeviceFails) {
1751 GetAdapter();
1752 DiscoverDevices();
1754 BluetoothDevice* device = adapter_->GetDevice(
1755 FakeBluetoothDeviceClient::kLegacyAutopairAddress);
1756 ASSERT_TRUE(device != NULL);
1757 ASSERT_FALSE(device->IsPaired());
1759 TestObserver observer(adapter_);
1761 // Connect without a pairing delegate; since the device requires pairing,
1762 // this should fail with an error.
1763 device->Connect(
1764 NULL,
1765 base::Bind(&BluetoothChromeOSTest::Callback,
1766 base::Unretained(this)),
1767 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
1768 base::Unretained(this)));
1770 EXPECT_EQ(0, callback_count_);
1771 EXPECT_EQ(1, error_callback_count_);
1772 EXPECT_EQ(BluetoothDevice::ERROR_FAILED, last_connect_error_);
1774 EXPECT_EQ(2, observer.device_changed_count_);
1776 EXPECT_FALSE(device->IsConnected());
1777 EXPECT_FALSE(device->IsConnecting());
1780 TEST_F(BluetoothChromeOSTest, DisconnectDevice) {
1781 GetAdapter();
1783 BluetoothDevice* device = adapter_->GetDevice(
1784 FakeBluetoothDeviceClient::kPairedDeviceAddress);
1785 ASSERT_TRUE(device != NULL);
1786 ASSERT_TRUE(device->IsPaired());
1788 device->Connect(
1789 NULL,
1790 base::Bind(&BluetoothChromeOSTest::Callback,
1791 base::Unretained(this)),
1792 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
1793 base::Unretained(this)));
1795 ASSERT_EQ(1, callback_count_);
1796 ASSERT_EQ(0, error_callback_count_);
1797 callback_count_ = 0;
1799 ASSERT_TRUE(device->IsConnected());
1800 ASSERT_FALSE(device->IsConnecting());
1802 // Disconnect the device, we should see the observer method fire and the
1803 // device get dropped.
1804 TestObserver observer(adapter_);
1806 device->Disconnect(
1807 base::Bind(&BluetoothChromeOSTest::Callback,
1808 base::Unretained(this)),
1809 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1810 base::Unretained(this)));
1812 EXPECT_EQ(1, callback_count_);
1813 EXPECT_EQ(0, error_callback_count_);
1815 EXPECT_EQ(1, observer.device_changed_count_);
1816 EXPECT_EQ(device, observer.last_device_);
1818 EXPECT_FALSE(device->IsConnected());
1821 TEST_F(BluetoothChromeOSTest, DisconnectUnconnectedDevice) {
1822 GetAdapter();
1824 BluetoothDevice* device = adapter_->GetDevice(
1825 FakeBluetoothDeviceClient::kPairedDeviceAddress);
1826 ASSERT_TRUE(device != NULL);
1827 ASSERT_TRUE(device->IsPaired());
1828 ASSERT_FALSE(device->IsConnected());
1830 // Disconnect the device, we should see the observer method fire and the
1831 // device get dropped.
1832 TestObserver observer(adapter_);
1834 device->Disconnect(
1835 base::Bind(&BluetoothChromeOSTest::Callback,
1836 base::Unretained(this)),
1837 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1838 base::Unretained(this)));
1840 EXPECT_EQ(0, callback_count_);
1841 EXPECT_EQ(1, error_callback_count_);
1843 EXPECT_EQ(0, observer.device_changed_count_);
1845 EXPECT_FALSE(device->IsConnected());
1848 TEST_F(BluetoothChromeOSTest, PairLegacyAutopair) {
1849 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
1851 GetAdapter();
1852 DiscoverDevices();
1854 // The Legacy Autopair device requires no PIN or Passkey to pair because
1855 // the daemon provides 0000 to the device for us.
1856 BluetoothDevice* device = adapter_->GetDevice(
1857 FakeBluetoothDeviceClient::kLegacyAutopairAddress);
1858 ASSERT_TRUE(device != NULL);
1859 ASSERT_FALSE(device->IsPaired());
1861 TestObserver observer(adapter_);
1863 TestPairingDelegate pairing_delegate;
1864 device->Connect(
1865 &pairing_delegate,
1866 base::Bind(&BluetoothChromeOSTest::Callback,
1867 base::Unretained(this)),
1868 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
1869 base::Unretained(this)));
1871 EXPECT_EQ(0, pairing_delegate.call_count_);
1872 EXPECT_TRUE(device->IsConnecting());
1874 message_loop_.Run();
1876 EXPECT_EQ(1, callback_count_);
1877 EXPECT_EQ(0, error_callback_count_);
1879 // Two changes for connecting, one change for connected, one for paired,
1880 // two for trusted (after pairing and connection), and one for the reconnect
1881 // mode (IsConnectable).
1882 EXPECT_EQ(7, observer.device_changed_count_);
1883 EXPECT_EQ(device, observer.last_device_);
1885 EXPECT_TRUE(device->IsConnected());
1886 EXPECT_FALSE(device->IsConnecting());
1888 EXPECT_TRUE(device->IsPaired());
1890 // Verify is a HID device and is connectable.
1891 BluetoothDevice::ServiceList uuids = device->GetServices();
1892 ASSERT_EQ(1U, uuids.size());
1893 EXPECT_EQ(uuids[0], "00001124-0000-1000-8000-00805f9b34fb");
1894 EXPECT_TRUE(device->IsConnectable());
1896 // Make sure the trusted property has been set to true.
1897 FakeBluetoothDeviceClient::Properties* properties =
1898 fake_bluetooth_device_client_->GetProperties(
1899 dbus::ObjectPath(FakeBluetoothDeviceClient::kLegacyAutopairPath));
1900 EXPECT_TRUE(properties->trusted.value());
1903 TEST_F(BluetoothChromeOSTest, PairDisplayPinCode) {
1904 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
1906 GetAdapter();
1907 DiscoverDevices();
1909 // Requires that we display a randomly generated PIN on the screen.
1910 BluetoothDevice* device = adapter_->GetDevice(
1911 FakeBluetoothDeviceClient::kDisplayPinCodeAddress);
1912 ASSERT_TRUE(device != NULL);
1913 ASSERT_FALSE(device->IsPaired());
1915 TestObserver observer(adapter_);
1917 TestPairingDelegate pairing_delegate;
1918 device->Connect(
1919 &pairing_delegate,
1920 base::Bind(&BluetoothChromeOSTest::Callback,
1921 base::Unretained(this)),
1922 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
1923 base::Unretained(this)));
1925 EXPECT_EQ(1, pairing_delegate.call_count_);
1926 EXPECT_EQ(1, pairing_delegate.display_pincode_count_);
1927 EXPECT_EQ("123456", pairing_delegate.last_pincode_);
1928 EXPECT_TRUE(device->IsConnecting());
1930 message_loop_.Run();
1932 EXPECT_EQ(1, callback_count_);
1933 EXPECT_EQ(0, error_callback_count_);
1935 // Two changes for connecting, one change for connected, one for paired,
1936 // two for trusted (after pairing and connection), and one for the reconnect
1937 // mode (IsConnectable).
1938 EXPECT_EQ(7, observer.device_changed_count_);
1939 EXPECT_EQ(device, observer.last_device_);
1941 EXPECT_TRUE(device->IsConnected());
1942 EXPECT_FALSE(device->IsConnecting());
1944 EXPECT_TRUE(device->IsPaired());
1946 // Verify is a HID device and is connectable.
1947 BluetoothDevice::ServiceList uuids = device->GetServices();
1948 ASSERT_EQ(1U, uuids.size());
1949 EXPECT_EQ(uuids[0], "00001124-0000-1000-8000-00805f9b34fb");
1950 EXPECT_TRUE(device->IsConnectable());
1952 // Make sure the trusted property has been set to true.
1953 FakeBluetoothDeviceClient::Properties* properties =
1954 fake_bluetooth_device_client_->GetProperties(
1955 dbus::ObjectPath(FakeBluetoothDeviceClient::kDisplayPinCodePath));
1956 EXPECT_TRUE(properties->trusted.value());
1959 TEST_F(BluetoothChromeOSTest, PairDisplayPasskey) {
1960 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
1962 GetAdapter();
1963 DiscoverDevices();
1965 // Requires that we display a randomly generated Passkey on the screen,
1966 // and notifies us as it's typed in.
1967 BluetoothDevice* device = adapter_->GetDevice(
1968 FakeBluetoothDeviceClient::kDisplayPasskeyAddress);
1969 ASSERT_TRUE(device != NULL);
1970 ASSERT_FALSE(device->IsPaired());
1972 TestObserver observer(adapter_);
1974 TestPairingDelegate pairing_delegate;
1975 device->Connect(
1976 &pairing_delegate,
1977 base::Bind(&BluetoothChromeOSTest::Callback,
1978 base::Unretained(this)),
1979 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
1980 base::Unretained(this)));
1982 // One call for DisplayPasskey() and one for KeysEntered().
1983 EXPECT_EQ(2, pairing_delegate.call_count_);
1984 EXPECT_EQ(1, pairing_delegate.display_passkey_count_);
1985 EXPECT_EQ(123456U, pairing_delegate.last_passkey_);
1986 EXPECT_EQ(1, pairing_delegate.keys_entered_count_);
1987 EXPECT_EQ(0U, pairing_delegate.last_entered_);
1989 EXPECT_TRUE(device->IsConnecting());
1991 // One call to KeysEntered() for each key, including [enter].
1992 for(int i = 1; i <= 7; ++i) {
1993 message_loop_.Run();
1995 EXPECT_EQ(2 + i, pairing_delegate.call_count_);
1996 EXPECT_EQ(1 + i, pairing_delegate.keys_entered_count_);
1997 EXPECT_EQ(static_cast<uint32_t>(i), pairing_delegate.last_entered_);
2000 message_loop_.Run();
2002 // 8 KeysEntered notifications (0 to 7, inclusive) and one aditional call for
2003 // DisplayPasskey().
2004 EXPECT_EQ(9, pairing_delegate.call_count_);
2005 EXPECT_EQ(8, pairing_delegate.keys_entered_count_);
2006 EXPECT_EQ(7U, pairing_delegate.last_entered_);
2008 EXPECT_EQ(1, callback_count_);
2009 EXPECT_EQ(0, error_callback_count_);
2011 // Two changes for connecting, one change for connected, one for paired,
2012 // two for trusted (after pairing and connection), and one for the reconnect
2013 // mode (IsConnectable).
2014 EXPECT_EQ(7, observer.device_changed_count_);
2015 EXPECT_EQ(device, observer.last_device_);
2017 EXPECT_TRUE(device->IsConnected());
2018 EXPECT_FALSE(device->IsConnecting());
2020 EXPECT_TRUE(device->IsPaired());
2022 // Verify is a HID device.
2023 BluetoothDevice::ServiceList uuids = device->GetServices();
2024 ASSERT_EQ(1U, uuids.size());
2025 EXPECT_EQ(uuids[0], "00001124-0000-1000-8000-00805f9b34fb");
2027 // And usually not connectable.
2028 EXPECT_FALSE(device->IsConnectable());
2030 // Make sure the trusted property has been set to true.
2031 FakeBluetoothDeviceClient::Properties* properties =
2032 fake_bluetooth_device_client_->GetProperties(
2033 dbus::ObjectPath(FakeBluetoothDeviceClient::kDisplayPasskeyPath));
2034 EXPECT_TRUE(properties->trusted.value());
2037 TEST_F(BluetoothChromeOSTest, PairRequestPinCode) {
2038 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2040 GetAdapter();
2041 DiscoverDevices();
2043 // Requires that the user enters a PIN for them.
2044 BluetoothDevice* device = adapter_->GetDevice(
2045 FakeBluetoothDeviceClient::kRequestPinCodeAddress);
2046 ASSERT_TRUE(device != NULL);
2047 ASSERT_FALSE(device->IsPaired());
2049 TestObserver observer(adapter_);
2051 TestPairingDelegate pairing_delegate;
2052 device->Connect(
2053 &pairing_delegate,
2054 base::Bind(&BluetoothChromeOSTest::Callback,
2055 base::Unretained(this)),
2056 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2057 base::Unretained(this)));
2059 EXPECT_EQ(1, pairing_delegate.call_count_);
2060 EXPECT_EQ(1, pairing_delegate.request_pincode_count_);
2061 EXPECT_TRUE(device->IsConnecting());
2063 // Set the PIN.
2064 device->SetPinCode("1234");
2065 message_loop_.Run();
2067 EXPECT_EQ(1, callback_count_);
2068 EXPECT_EQ(0, error_callback_count_);
2070 // Two changes for connecting, one change for connected, one for paired and
2071 // two for trusted (after pairing and connection).
2072 EXPECT_EQ(6, observer.device_changed_count_);
2073 EXPECT_EQ(device, observer.last_device_);
2075 EXPECT_TRUE(device->IsConnected());
2076 EXPECT_FALSE(device->IsConnecting());
2078 EXPECT_TRUE(device->IsPaired());
2080 // Verify is not a HID device.
2081 BluetoothDevice::ServiceList uuids = device->GetServices();
2082 ASSERT_EQ(0U, uuids.size());
2084 // Non HID devices are always connectable.
2085 EXPECT_TRUE(device->IsConnectable());
2087 // Make sure the trusted property has been set to true.
2088 FakeBluetoothDeviceClient::Properties* properties =
2089 fake_bluetooth_device_client_->GetProperties(
2090 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath));
2091 EXPECT_TRUE(properties->trusted.value());
2094 TEST_F(BluetoothChromeOSTest, PairConfirmPasskey) {
2095 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2097 GetAdapter();
2098 DiscoverDevices();
2100 // Requests that we confirm a displayed passkey.
2101 BluetoothDevice* device = adapter_->GetDevice(
2102 FakeBluetoothDeviceClient::kConfirmPasskeyAddress);
2103 ASSERT_TRUE(device != NULL);
2104 ASSERT_FALSE(device->IsPaired());
2106 TestObserver observer(adapter_);
2108 TestPairingDelegate pairing_delegate;
2109 device->Connect(
2110 &pairing_delegate,
2111 base::Bind(&BluetoothChromeOSTest::Callback,
2112 base::Unretained(this)),
2113 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2114 base::Unretained(this)));
2116 EXPECT_EQ(1, pairing_delegate.call_count_);
2117 EXPECT_EQ(1, pairing_delegate.confirm_passkey_count_);
2118 EXPECT_EQ(123456U, pairing_delegate.last_passkey_);
2119 EXPECT_TRUE(device->IsConnecting());
2121 // Confirm the passkey.
2122 device->ConfirmPairing();
2123 message_loop_.Run();
2125 EXPECT_EQ(1, callback_count_);
2126 EXPECT_EQ(0, error_callback_count_);
2128 // Two changes for connecting, one change for connected, one for paired and
2129 // two for trusted (after pairing and connection).
2130 EXPECT_EQ(6, observer.device_changed_count_);
2131 EXPECT_EQ(device, observer.last_device_);
2133 EXPECT_TRUE(device->IsConnected());
2134 EXPECT_FALSE(device->IsConnecting());
2136 EXPECT_TRUE(device->IsPaired());
2138 // Non HID devices are always connectable.
2139 EXPECT_TRUE(device->IsConnectable());
2141 // Make sure the trusted property has been set to true.
2142 FakeBluetoothDeviceClient::Properties* properties =
2143 fake_bluetooth_device_client_->GetProperties(
2144 dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath));
2145 EXPECT_TRUE(properties->trusted.value());
2148 TEST_F(BluetoothChromeOSTest, PairRequestPasskey) {
2149 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2151 GetAdapter();
2152 DiscoverDevices();
2154 // Requires that the user enters a Passkey, this would be some kind of
2155 // device that has a display, but doesn't use "just works" - maybe a car?
2156 BluetoothDevice* device = adapter_->GetDevice(
2157 FakeBluetoothDeviceClient::kRequestPasskeyAddress);
2158 ASSERT_TRUE(device != NULL);
2159 ASSERT_FALSE(device->IsPaired());
2161 TestObserver observer(adapter_);
2163 TestPairingDelegate pairing_delegate;
2164 device->Connect(
2165 &pairing_delegate,
2166 base::Bind(&BluetoothChromeOSTest::Callback,
2167 base::Unretained(this)),
2168 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2169 base::Unretained(this)));
2171 EXPECT_EQ(1, pairing_delegate.call_count_);
2172 EXPECT_EQ(1, pairing_delegate.request_passkey_count_);
2173 EXPECT_TRUE(device->IsConnecting());
2175 // Set the Passkey.
2176 device->SetPasskey(1234);
2177 message_loop_.Run();
2179 EXPECT_EQ(1, callback_count_);
2180 EXPECT_EQ(0, error_callback_count_);
2182 // Two changes for connecting, one change for connected, one for paired and
2183 // two for trusted (after pairing and connection).
2184 EXPECT_EQ(6, observer.device_changed_count_);
2185 EXPECT_EQ(device, observer.last_device_);
2187 EXPECT_TRUE(device->IsConnected());
2188 EXPECT_FALSE(device->IsConnecting());
2190 EXPECT_TRUE(device->IsPaired());
2192 // Non HID devices are always connectable.
2193 EXPECT_TRUE(device->IsConnectable());
2195 // Make sure the trusted property has been set to true.
2196 FakeBluetoothDeviceClient::Properties* properties =
2197 fake_bluetooth_device_client_->GetProperties(
2198 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath));
2199 EXPECT_TRUE(properties->trusted.value());
2202 TEST_F(BluetoothChromeOSTest, PairJustWorks) {
2203 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2205 GetAdapter();
2206 DiscoverDevices();
2208 // Uses just-works pairing, since this is an outgoing pairing, no delegate
2209 // interaction is required.
2210 BluetoothDevice* device = adapter_->GetDevice(
2211 FakeBluetoothDeviceClient::kJustWorksAddress);
2212 ASSERT_TRUE(device != NULL);
2213 ASSERT_FALSE(device->IsPaired());
2215 TestObserver observer(adapter_);
2217 TestPairingDelegate pairing_delegate;
2218 device->Connect(
2219 &pairing_delegate,
2220 base::Bind(&BluetoothChromeOSTest::Callback,
2221 base::Unretained(this)),
2222 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2223 base::Unretained(this)));
2225 EXPECT_EQ(0, pairing_delegate.call_count_);
2227 message_loop_.Run();
2229 EXPECT_EQ(1, callback_count_);
2230 EXPECT_EQ(0, error_callback_count_);
2232 // Two changes for connecting, one change for connected, one for paired and
2233 // two for trusted (after pairing and connection).
2234 EXPECT_EQ(6, observer.device_changed_count_);
2235 EXPECT_EQ(device, observer.last_device_);
2237 EXPECT_TRUE(device->IsConnected());
2238 EXPECT_FALSE(device->IsConnecting());
2240 EXPECT_TRUE(device->IsPaired());
2242 // Non HID devices are always connectable.
2243 EXPECT_TRUE(device->IsConnectable());
2245 // Make sure the trusted property has been set to true.
2246 FakeBluetoothDeviceClient::Properties* properties =
2247 fake_bluetooth_device_client_->GetProperties(
2248 dbus::ObjectPath(FakeBluetoothDeviceClient::kJustWorksPath));
2249 EXPECT_TRUE(properties->trusted.value());
2252 TEST_F(BluetoothChromeOSTest, PairUnpairableDeviceFails) {
2253 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2255 GetAdapter();
2256 DiscoverDevice(FakeBluetoothDeviceClient::kUnconnectableDeviceAddress);
2258 BluetoothDevice* device = adapter_->GetDevice(
2259 FakeBluetoothDeviceClient::kUnpairableDeviceAddress);
2260 ASSERT_TRUE(device != NULL);
2261 ASSERT_FALSE(device->IsPaired());
2263 TestObserver observer(adapter_);
2265 TestPairingDelegate pairing_delegate;
2266 device->Connect(
2267 &pairing_delegate,
2268 base::Bind(&BluetoothChromeOSTest::Callback,
2269 base::Unretained(this)),
2270 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2271 base::Unretained(this)));
2273 EXPECT_EQ(0, pairing_delegate.call_count_);
2274 EXPECT_TRUE(device->IsConnecting());
2276 // Run the loop to get the error..
2277 message_loop_.Run();
2279 EXPECT_EQ(0, callback_count_);
2280 EXPECT_EQ(1, error_callback_count_);
2282 EXPECT_EQ(BluetoothDevice::ERROR_FAILED, last_connect_error_);
2284 EXPECT_FALSE(device->IsConnected());
2285 EXPECT_FALSE(device->IsConnecting());
2286 EXPECT_FALSE(device->IsPaired());
2289 TEST_F(BluetoothChromeOSTest, PairingFails) {
2290 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2292 GetAdapter();
2293 DiscoverDevice(FakeBluetoothDeviceClient::kVanishingDeviceAddress);
2295 // The vanishing device times out during pairing
2296 BluetoothDevice* device = adapter_->GetDevice(
2297 FakeBluetoothDeviceClient::kVanishingDeviceAddress);
2298 ASSERT_TRUE(device != NULL);
2299 ASSERT_FALSE(device->IsPaired());
2301 TestObserver observer(adapter_);
2303 TestPairingDelegate pairing_delegate;
2304 device->Connect(
2305 &pairing_delegate,
2306 base::Bind(&BluetoothChromeOSTest::Callback,
2307 base::Unretained(this)),
2308 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2309 base::Unretained(this)));
2311 EXPECT_EQ(0, pairing_delegate.call_count_);
2312 EXPECT_TRUE(device->IsConnecting());
2314 // Run the loop to get the error..
2315 message_loop_.Run();
2317 EXPECT_EQ(0, callback_count_);
2318 EXPECT_EQ(1, error_callback_count_);
2320 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_TIMEOUT, last_connect_error_);
2322 EXPECT_FALSE(device->IsConnected());
2323 EXPECT_FALSE(device->IsConnecting());
2324 EXPECT_FALSE(device->IsPaired());
2327 TEST_F(BluetoothChromeOSTest, PairingFailsAtConnection) {
2328 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2330 GetAdapter();
2331 DiscoverDevices();
2333 // Everything seems to go according to plan with the unconnectable device;
2334 // it pairs, but then you can't make connections to it after.
2335 BluetoothDevice* device = adapter_->GetDevice(
2336 FakeBluetoothDeviceClient::kUnconnectableDeviceAddress);
2337 ASSERT_TRUE(device != NULL);
2338 ASSERT_FALSE(device->IsPaired());
2340 TestObserver observer(adapter_);
2342 TestPairingDelegate pairing_delegate;
2343 device->Connect(
2344 &pairing_delegate,
2345 base::Bind(&BluetoothChromeOSTest::Callback,
2346 base::Unretained(this)),
2347 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2348 base::Unretained(this)));
2350 EXPECT_EQ(0, pairing_delegate.call_count_);
2351 EXPECT_TRUE(device->IsConnecting());
2353 message_loop_.Run();
2355 EXPECT_EQ(0, callback_count_);
2356 EXPECT_EQ(1, error_callback_count_);
2357 EXPECT_EQ(BluetoothDevice::ERROR_FAILED, last_connect_error_);
2359 // Two changes for connecting, one for paired and one for trusted after
2360 // pairing. The device should not be connected.
2361 EXPECT_EQ(4, observer.device_changed_count_);
2362 EXPECT_EQ(device, observer.last_device_);
2364 EXPECT_FALSE(device->IsConnected());
2365 EXPECT_FALSE(device->IsConnecting());
2367 EXPECT_TRUE(device->IsPaired());
2369 // Make sure the trusted property has been set to true still (since pairing
2370 // worked).
2371 FakeBluetoothDeviceClient::Properties* properties =
2372 fake_bluetooth_device_client_->GetProperties(
2373 dbus::ObjectPath(
2374 FakeBluetoothDeviceClient::kUnconnectableDevicePath));
2375 EXPECT_TRUE(properties->trusted.value());
2378 TEST_F(BluetoothChromeOSTest, PairingRejectedAtPinCode) {
2379 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2381 GetAdapter();
2382 DiscoverDevices();
2384 // Reject the pairing after we receive a request for the PIN code.
2385 BluetoothDevice* device = adapter_->GetDevice(
2386 FakeBluetoothDeviceClient::kRequestPinCodeAddress);
2387 ASSERT_TRUE(device != NULL);
2388 ASSERT_FALSE(device->IsPaired());
2390 TestObserver observer(adapter_);
2392 TestPairingDelegate pairing_delegate;
2393 device->Connect(
2394 &pairing_delegate,
2395 base::Bind(&BluetoothChromeOSTest::Callback,
2396 base::Unretained(this)),
2397 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2398 base::Unretained(this)));
2400 EXPECT_EQ(1, pairing_delegate.call_count_);
2401 EXPECT_EQ(1, pairing_delegate.request_pincode_count_);
2402 EXPECT_TRUE(device->IsConnecting());
2404 // Reject the pairing.
2405 device->RejectPairing();
2406 message_loop_.Run();
2408 EXPECT_EQ(0, callback_count_);
2409 EXPECT_EQ(1, error_callback_count_);
2410 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_REJECTED, last_connect_error_);
2412 // Should be no changes except connecting going true and false.
2413 EXPECT_EQ(2, observer.device_changed_count_);
2414 EXPECT_FALSE(device->IsConnected());
2415 EXPECT_FALSE(device->IsConnecting());
2416 EXPECT_FALSE(device->IsPaired());
2419 TEST_F(BluetoothChromeOSTest, PairingCancelledAtPinCode) {
2420 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2422 GetAdapter();
2423 DiscoverDevices();
2425 // Cancel the pairing after we receive a request for the PIN code.
2426 BluetoothDevice* device = adapter_->GetDevice(
2427 FakeBluetoothDeviceClient::kRequestPinCodeAddress);
2428 ASSERT_TRUE(device != NULL);
2429 ASSERT_FALSE(device->IsPaired());
2431 TestObserver observer(adapter_);
2433 TestPairingDelegate pairing_delegate;
2434 device->Connect(
2435 &pairing_delegate,
2436 base::Bind(&BluetoothChromeOSTest::Callback,
2437 base::Unretained(this)),
2438 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2439 base::Unretained(this)));
2441 EXPECT_EQ(1, pairing_delegate.call_count_);
2442 EXPECT_EQ(1, pairing_delegate.request_pincode_count_);
2443 EXPECT_TRUE(device->IsConnecting());
2445 // Cancel the pairing.
2446 device->CancelPairing();
2447 message_loop_.Run();
2449 EXPECT_EQ(0, callback_count_);
2450 EXPECT_EQ(1, error_callback_count_);
2451 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED, last_connect_error_);
2453 // Should be no changes except connecting going true and false.
2454 EXPECT_EQ(2, observer.device_changed_count_);
2455 EXPECT_FALSE(device->IsConnected());
2456 EXPECT_FALSE(device->IsConnecting());
2457 EXPECT_FALSE(device->IsPaired());
2460 TEST_F(BluetoothChromeOSTest, PairingRejectedAtPasskey) {
2461 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2463 GetAdapter();
2464 DiscoverDevices();
2466 // Reject the pairing after we receive a request for the passkey.
2467 BluetoothDevice* device = adapter_->GetDevice(
2468 FakeBluetoothDeviceClient::kRequestPasskeyAddress);
2469 ASSERT_TRUE(device != NULL);
2470 ASSERT_FALSE(device->IsPaired());
2472 TestObserver observer(adapter_);
2474 TestPairingDelegate pairing_delegate;
2475 device->Connect(
2476 &pairing_delegate,
2477 base::Bind(&BluetoothChromeOSTest::Callback,
2478 base::Unretained(this)),
2479 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2480 base::Unretained(this)));
2482 EXPECT_EQ(1, pairing_delegate.call_count_);
2483 EXPECT_EQ(1, pairing_delegate.request_passkey_count_);
2484 EXPECT_TRUE(device->IsConnecting());
2486 // Reject the pairing.
2487 device->RejectPairing();
2488 message_loop_.Run();
2490 EXPECT_EQ(0, callback_count_);
2491 EXPECT_EQ(1, error_callback_count_);
2492 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_REJECTED, last_connect_error_);
2494 // Should be no changes except connecting going true and false.
2495 EXPECT_EQ(2, observer.device_changed_count_);
2496 EXPECT_FALSE(device->IsConnected());
2497 EXPECT_FALSE(device->IsConnecting());
2498 EXPECT_FALSE(device->IsPaired());
2501 TEST_F(BluetoothChromeOSTest, PairingCancelledAtPasskey) {
2502 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2504 GetAdapter();
2505 DiscoverDevices();
2507 // Cancel the pairing after we receive a request for the passkey.
2508 BluetoothDevice* device = adapter_->GetDevice(
2509 FakeBluetoothDeviceClient::kRequestPasskeyAddress);
2510 ASSERT_TRUE(device != NULL);
2511 ASSERT_FALSE(device->IsPaired());
2513 TestObserver observer(adapter_);
2515 TestPairingDelegate pairing_delegate;
2516 device->Connect(
2517 &pairing_delegate,
2518 base::Bind(&BluetoothChromeOSTest::Callback,
2519 base::Unretained(this)),
2520 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2521 base::Unretained(this)));
2523 EXPECT_EQ(1, pairing_delegate.call_count_);
2524 EXPECT_EQ(1, pairing_delegate.request_passkey_count_);
2525 EXPECT_TRUE(device->IsConnecting());
2527 // Cancel the pairing.
2528 device->CancelPairing();
2529 message_loop_.Run();
2531 EXPECT_EQ(0, callback_count_);
2532 EXPECT_EQ(1, error_callback_count_);
2533 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED, last_connect_error_);
2535 // Should be no changes except connecting going true and false.
2536 EXPECT_EQ(2, observer.device_changed_count_);
2537 EXPECT_FALSE(device->IsConnected());
2538 EXPECT_FALSE(device->IsConnecting());
2539 EXPECT_FALSE(device->IsPaired());
2542 TEST_F(BluetoothChromeOSTest, PairingRejectedAtConfirmation) {
2543 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2545 GetAdapter();
2546 DiscoverDevices();
2548 // Reject the pairing after we receive a request for passkey confirmation.
2549 BluetoothDevice* device = adapter_->GetDevice(
2550 FakeBluetoothDeviceClient::kConfirmPasskeyAddress);
2551 ASSERT_TRUE(device != NULL);
2552 ASSERT_FALSE(device->IsPaired());
2554 TestObserver observer(adapter_);
2556 TestPairingDelegate pairing_delegate;
2557 device->Connect(
2558 &pairing_delegate,
2559 base::Bind(&BluetoothChromeOSTest::Callback,
2560 base::Unretained(this)),
2561 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2562 base::Unretained(this)));
2564 EXPECT_EQ(1, pairing_delegate.call_count_);
2565 EXPECT_EQ(1, pairing_delegate.confirm_passkey_count_);
2566 EXPECT_TRUE(device->IsConnecting());
2568 // Reject the pairing.
2569 device->RejectPairing();
2570 message_loop_.Run();
2572 EXPECT_EQ(0, callback_count_);
2573 EXPECT_EQ(1, error_callback_count_);
2574 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_REJECTED, last_connect_error_);
2576 // Should be no changes except connecting going true and false.
2577 EXPECT_EQ(2, observer.device_changed_count_);
2578 EXPECT_FALSE(device->IsConnected());
2579 EXPECT_FALSE(device->IsConnecting());
2580 EXPECT_FALSE(device->IsPaired());
2583 TEST_F(BluetoothChromeOSTest, PairingCancelledAtConfirmation) {
2584 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2586 GetAdapter();
2587 DiscoverDevices();
2589 // Cancel the pairing after we receive a request for the passkey.
2590 BluetoothDevice* device = adapter_->GetDevice(
2591 FakeBluetoothDeviceClient::kConfirmPasskeyAddress);
2592 ASSERT_TRUE(device != NULL);
2593 ASSERT_FALSE(device->IsPaired());
2595 TestObserver observer(adapter_);
2597 TestPairingDelegate pairing_delegate;
2598 device->Connect(
2599 &pairing_delegate,
2600 base::Bind(&BluetoothChromeOSTest::Callback,
2601 base::Unretained(this)),
2602 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2603 base::Unretained(this)));
2605 EXPECT_EQ(1, pairing_delegate.call_count_);
2606 EXPECT_EQ(1, pairing_delegate.confirm_passkey_count_);
2607 EXPECT_TRUE(device->IsConnecting());
2609 // Cancel the pairing.
2610 device->CancelPairing();
2611 message_loop_.Run();
2613 EXPECT_EQ(0, callback_count_);
2614 EXPECT_EQ(1, error_callback_count_);
2615 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED, last_connect_error_);
2617 // Should be no changes except connecting going true and false.
2618 EXPECT_EQ(2, observer.device_changed_count_);
2619 EXPECT_FALSE(device->IsConnected());
2620 EXPECT_FALSE(device->IsConnecting());
2621 EXPECT_FALSE(device->IsPaired());
2624 TEST_F(BluetoothChromeOSTest, PairingCancelledInFlight) {
2625 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2627 GetAdapter();
2628 DiscoverDevices();
2630 // Cancel the pairing while we're waiting for the remote host.
2631 BluetoothDevice* device = adapter_->GetDevice(
2632 FakeBluetoothDeviceClient::kLegacyAutopairAddress);
2633 ASSERT_TRUE(device != NULL);
2634 ASSERT_FALSE(device->IsPaired());
2636 TestObserver observer(adapter_);
2638 TestPairingDelegate pairing_delegate;
2639 device->Connect(
2640 &pairing_delegate,
2641 base::Bind(&BluetoothChromeOSTest::Callback,
2642 base::Unretained(this)),
2643 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2644 base::Unretained(this)));
2646 EXPECT_EQ(0, pairing_delegate.call_count_);
2647 EXPECT_TRUE(device->IsConnecting());
2649 // Cancel the pairing.
2650 device->CancelPairing();
2651 message_loop_.Run();
2653 EXPECT_EQ(0, callback_count_);
2654 EXPECT_EQ(1, error_callback_count_);
2655 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED, last_connect_error_);
2657 // Should be no changes except connecting going true and false.
2658 EXPECT_EQ(2, observer.device_changed_count_);
2659 EXPECT_FALSE(device->IsConnected());
2660 EXPECT_FALSE(device->IsConnecting());
2661 EXPECT_FALSE(device->IsPaired());
2664 TEST_F(BluetoothChromeOSTest, IncomingPairRequestPinCode) {
2665 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2667 GetAdapter();
2669 TestPairingDelegate pairing_delegate;
2670 adapter_->AddPairingDelegate(
2671 &pairing_delegate,
2672 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH);
2674 // Requires that we provide a PIN code.
2675 fake_bluetooth_device_client_->CreateDevice(
2676 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
2677 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath));
2678 BluetoothDevice* device = adapter_->GetDevice(
2679 FakeBluetoothDeviceClient::kRequestPinCodeAddress);
2680 ASSERT_TRUE(device != NULL);
2681 ASSERT_FALSE(device->IsPaired());
2683 TestObserver observer(adapter_);
2685 fake_bluetooth_device_client_->SimulatePairing(
2686 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath),
2687 true,
2688 base::Bind(&BluetoothChromeOSTest::Callback,
2689 base::Unretained(this)),
2690 base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
2691 base::Unretained(this)));
2693 EXPECT_EQ(1, pairing_delegate.call_count_);
2694 EXPECT_EQ(1, pairing_delegate.request_pincode_count_);
2696 // Set the PIN.
2697 device->SetPinCode("1234");
2698 message_loop_.Run();
2700 EXPECT_EQ(1, callback_count_);
2701 EXPECT_EQ(0, error_callback_count_);
2703 // One for paired.
2704 EXPECT_EQ(1, observer.device_changed_count_);
2705 EXPECT_EQ(device, observer.last_device_);
2707 EXPECT_TRUE(device->IsPaired());
2709 // No pairing context should remain on the device.
2710 BluetoothDeviceChromeOS* device_chromeos =
2711 static_cast<BluetoothDeviceChromeOS*>(device);
2712 EXPECT_TRUE(device_chromeos->GetPairing() == NULL);
2715 TEST_F(BluetoothChromeOSTest, IncomingPairConfirmPasskey) {
2716 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2718 GetAdapter();
2720 TestPairingDelegate pairing_delegate;
2721 adapter_->AddPairingDelegate(
2722 &pairing_delegate,
2723 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH);
2725 // Requests that we confirm a displayed passkey.
2726 fake_bluetooth_device_client_->CreateDevice(
2727 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
2728 dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath));
2729 BluetoothDevice* device = adapter_->GetDevice(
2730 FakeBluetoothDeviceClient::kConfirmPasskeyAddress);
2731 ASSERT_TRUE(device != NULL);
2732 ASSERT_FALSE(device->IsPaired());
2734 TestObserver observer(adapter_);
2736 fake_bluetooth_device_client_->SimulatePairing(
2737 dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath),
2738 true,
2739 base::Bind(&BluetoothChromeOSTest::Callback,
2740 base::Unretained(this)),
2741 base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
2742 base::Unretained(this)));
2744 EXPECT_EQ(1, pairing_delegate.call_count_);
2745 EXPECT_EQ(1, pairing_delegate.confirm_passkey_count_);
2746 EXPECT_EQ(123456U, pairing_delegate.last_passkey_);
2748 // Confirm the passkey.
2749 device->ConfirmPairing();
2750 message_loop_.Run();
2752 EXPECT_EQ(1, callback_count_);
2753 EXPECT_EQ(0, error_callback_count_);
2755 // One for paired.
2756 EXPECT_EQ(1, observer.device_changed_count_);
2757 EXPECT_EQ(device, observer.last_device_);
2759 EXPECT_TRUE(device->IsPaired());
2761 // No pairing context should remain on the device.
2762 BluetoothDeviceChromeOS* device_chromeos =
2763 static_cast<BluetoothDeviceChromeOS*>(device);
2764 EXPECT_TRUE(device_chromeos->GetPairing() == NULL);
2767 TEST_F(BluetoothChromeOSTest, IncomingPairRequestPasskey) {
2768 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2770 GetAdapter();
2772 TestPairingDelegate pairing_delegate;
2773 adapter_->AddPairingDelegate(
2774 &pairing_delegate,
2775 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH);
2777 // Requests that we provide a Passkey.
2778 fake_bluetooth_device_client_->CreateDevice(
2779 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
2780 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath));
2781 BluetoothDevice* device = adapter_->GetDevice(
2782 FakeBluetoothDeviceClient::kRequestPasskeyAddress);
2783 ASSERT_TRUE(device != NULL);
2784 ASSERT_FALSE(device->IsPaired());
2786 TestObserver observer(adapter_);
2788 fake_bluetooth_device_client_->SimulatePairing(
2789 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath),
2790 true,
2791 base::Bind(&BluetoothChromeOSTest::Callback,
2792 base::Unretained(this)),
2793 base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
2794 base::Unretained(this)));
2796 EXPECT_EQ(1, pairing_delegate.call_count_);
2797 EXPECT_EQ(1, pairing_delegate.request_passkey_count_);
2799 // Set the Passkey.
2800 device->SetPasskey(1234);
2801 message_loop_.Run();
2803 EXPECT_EQ(1, callback_count_);
2804 EXPECT_EQ(0, error_callback_count_);
2806 // One for paired.
2807 EXPECT_EQ(1, observer.device_changed_count_);
2808 EXPECT_EQ(device, observer.last_device_);
2810 EXPECT_TRUE(device->IsPaired());
2812 // No pairing context should remain on the device.
2813 BluetoothDeviceChromeOS* device_chromeos =
2814 static_cast<BluetoothDeviceChromeOS*>(device);
2815 EXPECT_TRUE(device_chromeos->GetPairing() == NULL);
2818 TEST_F(BluetoothChromeOSTest, IncomingPairJustWorks) {
2819 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2821 GetAdapter();
2823 TestPairingDelegate pairing_delegate;
2824 adapter_->AddPairingDelegate(
2825 &pairing_delegate,
2826 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH);
2828 // Uses just-works pairing so, sinec this an incoming pairing, require
2829 // authorization from the user.
2830 fake_bluetooth_device_client_->CreateDevice(
2831 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
2832 dbus::ObjectPath(FakeBluetoothDeviceClient::kJustWorksPath));
2833 BluetoothDevice* device = adapter_->GetDevice(
2834 FakeBluetoothDeviceClient::kJustWorksAddress);
2835 ASSERT_TRUE(device != NULL);
2836 ASSERT_FALSE(device->IsPaired());
2838 TestObserver observer(adapter_);
2840 fake_bluetooth_device_client_->SimulatePairing(
2841 dbus::ObjectPath(FakeBluetoothDeviceClient::kJustWorksPath),
2842 true,
2843 base::Bind(&BluetoothChromeOSTest::Callback,
2844 base::Unretained(this)),
2845 base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
2846 base::Unretained(this)));
2848 EXPECT_EQ(1, pairing_delegate.call_count_);
2849 EXPECT_EQ(1, pairing_delegate.authorize_pairing_count_);
2851 // Confirm the pairing.
2852 device->ConfirmPairing();
2853 message_loop_.Run();
2855 EXPECT_EQ(1, callback_count_);
2856 EXPECT_EQ(0, error_callback_count_);
2858 // One for paired.
2859 EXPECT_EQ(1, observer.device_changed_count_);
2860 EXPECT_EQ(device, observer.last_device_);
2862 EXPECT_TRUE(device->IsPaired());
2864 // No pairing context should remain on the device.
2865 BluetoothDeviceChromeOS* device_chromeos =
2866 static_cast<BluetoothDeviceChromeOS*>(device);
2867 EXPECT_TRUE(device_chromeos->GetPairing() == NULL);
2870 TEST_F(BluetoothChromeOSTest, IncomingPairRequestPinCodeWithoutDelegate) {
2871 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2873 GetAdapter();
2875 // Requires that we provide a PIN Code, without a pairing delegate,
2876 // that will be rejected.
2877 fake_bluetooth_device_client_->CreateDevice(
2878 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
2879 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath));
2880 BluetoothDevice* device = adapter_->GetDevice(
2881 FakeBluetoothDeviceClient::kRequestPinCodeAddress);
2882 ASSERT_TRUE(device != NULL);
2883 ASSERT_FALSE(device->IsPaired());
2885 TestObserver observer(adapter_);
2887 fake_bluetooth_device_client_->SimulatePairing(
2888 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath),
2889 true,
2890 base::Bind(&BluetoothChromeOSTest::Callback,
2891 base::Unretained(this)),
2892 base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
2893 base::Unretained(this)));
2895 message_loop_.Run();
2897 EXPECT_EQ(0, callback_count_);
2898 EXPECT_EQ(1, error_callback_count_);
2899 EXPECT_EQ(bluetooth_device::kErrorAuthenticationRejected, last_client_error_);
2901 // No changes should be observer.
2902 EXPECT_EQ(0, observer.device_changed_count_);
2904 EXPECT_FALSE(device->IsPaired());
2906 // No pairing context should remain on the device.
2907 BluetoothDeviceChromeOS* device_chromeos =
2908 static_cast<BluetoothDeviceChromeOS*>(device);
2909 EXPECT_TRUE(device_chromeos->GetPairing() == NULL);
2912 TEST_F(BluetoothChromeOSTest, IncomingPairConfirmPasskeyWithoutDelegate) {
2913 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2915 GetAdapter();
2917 // Requests that we confirm a displayed passkey, without a pairing delegate,
2918 // that will be rejected.
2919 fake_bluetooth_device_client_->CreateDevice(
2920 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
2921 dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath));
2922 BluetoothDevice* device = adapter_->GetDevice(
2923 FakeBluetoothDeviceClient::kConfirmPasskeyAddress);
2924 ASSERT_TRUE(device != NULL);
2925 ASSERT_FALSE(device->IsPaired());
2927 TestObserver observer(adapter_);
2929 fake_bluetooth_device_client_->SimulatePairing(
2930 dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath),
2931 true,
2932 base::Bind(&BluetoothChromeOSTest::Callback,
2933 base::Unretained(this)),
2934 base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
2935 base::Unretained(this)));
2937 message_loop_.Run();
2939 EXPECT_EQ(0, callback_count_);
2940 EXPECT_EQ(1, error_callback_count_);
2941 EXPECT_EQ(bluetooth_device::kErrorAuthenticationRejected, last_client_error_);
2943 // No changes should be observer.
2944 EXPECT_EQ(0, observer.device_changed_count_);
2946 EXPECT_FALSE(device->IsPaired());
2948 // No pairing context should remain on the device.
2949 BluetoothDeviceChromeOS* device_chromeos =
2950 static_cast<BluetoothDeviceChromeOS*>(device);
2951 EXPECT_TRUE(device_chromeos->GetPairing() == NULL);
2954 TEST_F(BluetoothChromeOSTest, IncomingPairRequestPasskeyWithoutDelegate) {
2955 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2957 GetAdapter();
2959 // Requests that we provide a displayed passkey, without a pairing delegate,
2960 // that will be rejected.
2961 fake_bluetooth_device_client_->CreateDevice(
2962 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
2963 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath));
2964 BluetoothDevice* device = adapter_->GetDevice(
2965 FakeBluetoothDeviceClient::kRequestPasskeyAddress);
2966 ASSERT_TRUE(device != NULL);
2967 ASSERT_FALSE(device->IsPaired());
2969 TestObserver observer(adapter_);
2971 fake_bluetooth_device_client_->SimulatePairing(
2972 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath),
2973 true,
2974 base::Bind(&BluetoothChromeOSTest::Callback,
2975 base::Unretained(this)),
2976 base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
2977 base::Unretained(this)));
2979 message_loop_.Run();
2981 EXPECT_EQ(0, callback_count_);
2982 EXPECT_EQ(1, error_callback_count_);
2983 EXPECT_EQ(bluetooth_device::kErrorAuthenticationRejected, last_client_error_);
2985 // No changes should be observer.
2986 EXPECT_EQ(0, observer.device_changed_count_);
2988 EXPECT_FALSE(device->IsPaired());
2990 // No pairing context should remain on the device.
2991 BluetoothDeviceChromeOS* device_chromeos =
2992 static_cast<BluetoothDeviceChromeOS*>(device);
2993 EXPECT_TRUE(device_chromeos->GetPairing() == NULL);
2996 TEST_F(BluetoothChromeOSTest, IncomingPairJustWorksWithoutDelegate) {
2997 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2999 GetAdapter();
3001 // Uses just-works pairing and thus requires authorization for incoming
3002 // pairings, without a pairing delegate, that will be rejected.
3003 fake_bluetooth_device_client_->CreateDevice(
3004 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
3005 dbus::ObjectPath(FakeBluetoothDeviceClient::kJustWorksPath));
3006 BluetoothDevice* device = adapter_->GetDevice(
3007 FakeBluetoothDeviceClient::kJustWorksAddress);
3008 ASSERT_TRUE(device != NULL);
3009 ASSERT_FALSE(device->IsPaired());
3011 TestObserver observer(adapter_);
3013 fake_bluetooth_device_client_->SimulatePairing(
3014 dbus::ObjectPath(FakeBluetoothDeviceClient::kJustWorksPath),
3015 true,
3016 base::Bind(&BluetoothChromeOSTest::Callback,
3017 base::Unretained(this)),
3018 base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
3019 base::Unretained(this)));
3021 message_loop_.Run();
3023 EXPECT_EQ(0, callback_count_);
3024 EXPECT_EQ(1, error_callback_count_);
3025 EXPECT_EQ(bluetooth_device::kErrorAuthenticationRejected, last_client_error_);
3027 // No changes should be observer.
3028 EXPECT_EQ(0, observer.device_changed_count_);
3030 EXPECT_FALSE(device->IsPaired());
3032 // No pairing context should remain on the device.
3033 BluetoothDeviceChromeOS* device_chromeos =
3034 static_cast<BluetoothDeviceChromeOS*>(device);
3035 EXPECT_TRUE(device_chromeos->GetPairing() == NULL);
3038 TEST_F(BluetoothChromeOSTest, RemovePairingDelegateDuringPairing) {
3039 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3041 GetAdapter();
3043 TestPairingDelegate pairing_delegate;
3044 adapter_->AddPairingDelegate(
3045 &pairing_delegate,
3046 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH);
3048 // Requests that we provide a Passkey.
3049 fake_bluetooth_device_client_->CreateDevice(
3050 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
3051 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath));
3052 BluetoothDevice* device = adapter_->GetDevice(
3053 FakeBluetoothDeviceClient::kRequestPasskeyAddress);
3054 ASSERT_TRUE(device != NULL);
3055 ASSERT_FALSE(device->IsPaired());
3057 TestObserver observer(adapter_);
3059 fake_bluetooth_device_client_->SimulatePairing(
3060 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath),
3061 true,
3062 base::Bind(&BluetoothChromeOSTest::Callback,
3063 base::Unretained(this)),
3064 base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
3065 base::Unretained(this)));
3067 EXPECT_EQ(1, pairing_delegate.call_count_);
3068 EXPECT_EQ(1, pairing_delegate.request_passkey_count_);
3070 // A pairing context should now be set on the device.
3071 BluetoothDeviceChromeOS* device_chromeos =
3072 static_cast<BluetoothDeviceChromeOS*>(device);
3073 ASSERT_TRUE(device_chromeos->GetPairing() != NULL);
3075 // Removing the pairing delegate should remove that pairing context.
3076 adapter_->RemovePairingDelegate(&pairing_delegate);
3078 EXPECT_TRUE(device_chromeos->GetPairing() == NULL);
3080 // Set the Passkey, this should now have no effect since the pairing has
3081 // been, in-effect, cancelled
3082 device->SetPasskey(1234);
3084 EXPECT_EQ(0, callback_count_);
3085 EXPECT_EQ(0, error_callback_count_);
3086 EXPECT_EQ(0, observer.device_changed_count_);
3088 EXPECT_FALSE(device->IsPaired());
3091 TEST_F(BluetoothChromeOSTest, DeviceId) {
3092 GetAdapter();
3094 // Use the built-in paired device for this test, grab its Properties
3095 // structure so we can adjust the underlying modalias property.
3096 BluetoothDevice* device = adapter_->GetDevice(
3097 FakeBluetoothDeviceClient::kPairedDeviceAddress);
3098 FakeBluetoothDeviceClient::Properties* properties =
3099 fake_bluetooth_device_client_->GetProperties(
3100 dbus::ObjectPath(FakeBluetoothDeviceClient::kPairedDevicePath));
3102 ASSERT_TRUE(device != NULL);
3103 ASSERT_TRUE(properties != NULL);
3105 // Valid USB IF-assigned identifier.
3106 ASSERT_EQ("usb:v05ACp030Dd0306", properties->modalias.value());
3108 EXPECT_EQ(BluetoothDevice::VENDOR_ID_USB, device->GetVendorIDSource());
3109 EXPECT_EQ(0x05ac, device->GetVendorID());
3110 EXPECT_EQ(0x030d, device->GetProductID());
3111 EXPECT_EQ(0x0306, device->GetDeviceID());
3113 // Valid Bluetooth SIG-assigned identifier.
3114 properties->modalias.ReplaceValue("bluetooth:v00E0p2400d0400");
3116 EXPECT_EQ(BluetoothDevice::VENDOR_ID_BLUETOOTH, device->GetVendorIDSource());
3117 EXPECT_EQ(0x00e0, device->GetVendorID());
3118 EXPECT_EQ(0x2400, device->GetProductID());
3119 EXPECT_EQ(0x0400, device->GetDeviceID());
3121 // Invalid USB IF-assigned identifier.
3122 properties->modalias.ReplaceValue("usb:x00E0p2400d0400");
3124 EXPECT_EQ(BluetoothDevice::VENDOR_ID_UNKNOWN, device->GetVendorIDSource());
3125 EXPECT_EQ(0, device->GetVendorID());
3126 EXPECT_EQ(0, device->GetProductID());
3127 EXPECT_EQ(0, device->GetDeviceID());
3129 // Invalid Bluetooth SIG-assigned identifier.
3130 properties->modalias.ReplaceValue("bluetooth:x00E0p2400d0400");
3132 EXPECT_EQ(BluetoothDevice::VENDOR_ID_UNKNOWN, device->GetVendorIDSource());
3133 EXPECT_EQ(0, device->GetVendorID());
3134 EXPECT_EQ(0, device->GetProductID());
3135 EXPECT_EQ(0, device->GetDeviceID());
3137 // Unknown vendor specification identifier.
3138 properties->modalias.ReplaceValue("chrome:v00E0p2400d0400");
3140 EXPECT_EQ(BluetoothDevice::VENDOR_ID_UNKNOWN, device->GetVendorIDSource());
3141 EXPECT_EQ(0, device->GetVendorID());
3142 EXPECT_EQ(0, device->GetProductID());
3143 EXPECT_EQ(0, device->GetDeviceID());
3146 } // namespace chromeos