[android_webview] Disable AwSettingsTest broken by Blink roll.
[chromium-blink-merge.git] / device / bluetooth / bluetooth_experimental_chromeos_unittest.cc
blob0dc3ad5e81ca0839cf7729e008bdb3459747bed2
1 // Copyright (c) 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/command_line.h"
6 #include "base/message_loop.h"
7 #include "base/utf_string_conversions.h"
8 #include "chromeos/chromeos_switches.h"
9 #include "chromeos/dbus/fake_bluetooth_adapter_client.h"
10 #include "chromeos/dbus/fake_bluetooth_device_client.h"
11 #include "chromeos/dbus/mock_dbus_thread_manager_without_gmock.h"
12 #include "dbus/object_path.h"
13 #include "device/bluetooth/bluetooth_adapter.h"
14 #include "device/bluetooth/bluetooth_adapter_experimental_chromeos.h"
15 #include "device/bluetooth/bluetooth_adapter_factory.h"
16 #include "device/bluetooth/bluetooth_device.h"
17 #include "device/bluetooth/bluetooth_device_experimental_chromeos.h"
18 #include "testing/gtest/include/gtest/gtest.h"
20 using device::BluetoothAdapter;
21 using device::BluetoothAdapterFactory;
22 using device::BluetoothDevice;
24 namespace chromeos {
26 class TestObserver : public BluetoothAdapter::Observer {
27 public:
28 TestObserver(scoped_refptr<BluetoothAdapter> adapter)
29 : present_changed_count_(0),
30 powered_changed_count_(0),
31 discovering_changed_count_(0),
32 last_present_(false),
33 last_powered_(false),
34 last_discovering_(false),
35 device_added_count_(0),
36 device_changed_count_(0),
37 device_removed_count_(0),
38 last_device_(NULL),
39 adapter_(adapter) {
41 virtual ~TestObserver() {}
43 virtual void AdapterPresentChanged(BluetoothAdapter* adapter,
44 bool present) OVERRIDE {
45 EXPECT_EQ(adapter_, adapter);
47 ++present_changed_count_;
48 last_present_ = present;
51 virtual void AdapterPoweredChanged(BluetoothAdapter* adapter,
52 bool powered) OVERRIDE {
53 EXPECT_EQ(adapter_, adapter);
55 ++powered_changed_count_;
56 last_powered_ = powered;
59 virtual void AdapterDiscoveringChanged(BluetoothAdapter* adapter,
60 bool discovering) OVERRIDE {
61 EXPECT_EQ(adapter_, adapter);
63 ++discovering_changed_count_;
64 last_discovering_ = discovering;
67 virtual void DeviceAdded(BluetoothAdapter* adapter,
68 BluetoothDevice* device) OVERRIDE {
69 EXPECT_EQ(adapter_, adapter);
71 ++device_added_count_;
72 last_device_ = device;
73 last_device_address_ = device->GetAddress();
75 QuitMessageLoop();
78 virtual void DeviceChanged(BluetoothAdapter* adapter,
79 BluetoothDevice* device) OVERRIDE {
80 EXPECT_EQ(adapter_, adapter);
82 ++device_changed_count_;
83 last_device_ = device;
84 last_device_address_ = device->GetAddress();
86 QuitMessageLoop();
89 virtual void DeviceRemoved(BluetoothAdapter* adapter,
90 BluetoothDevice* device) OVERRIDE {
91 EXPECT_EQ(adapter_, adapter);
93 ++device_removed_count_;
94 // Can't save device, it may be freed
95 last_device_address_ = device->GetAddress();
97 QuitMessageLoop();
100 int present_changed_count_;
101 int powered_changed_count_;
102 int discovering_changed_count_;
103 bool last_present_;
104 bool last_powered_;
105 bool last_discovering_;
106 int device_added_count_;
107 int device_changed_count_;
108 int device_removed_count_;
109 BluetoothDevice* last_device_;
110 std::string last_device_address_;
112 private:
113 // Some tests use a message loop since background processing is simulated;
114 // break out of those loops.
115 void QuitMessageLoop() {
116 if (base::MessageLoop::current() &&
117 base::MessageLoop::current()->is_running())
118 base::MessageLoop::current()->Quit();
121 scoped_refptr<BluetoothAdapter> adapter_;
124 class TestPairingDelegate : public BluetoothDevice::PairingDelegate {
125 public:
126 TestPairingDelegate()
127 : call_count_(0),
128 request_pincode_count_(0),
129 request_passkey_count_(0),
130 display_pincode_count_(0),
131 display_passkey_count_(0),
132 keys_entered_count_(0),
133 confirm_passkey_count_(0),
134 dismiss_count_(0),
135 last_passkey_(9999999U),
136 last_entered_(999U) {}
137 virtual ~TestPairingDelegate() {}
139 virtual void RequestPinCode(BluetoothDevice* device) OVERRIDE {
140 ++call_count_;
141 ++request_pincode_count_;
142 QuitMessageLoop();
145 virtual void RequestPasskey(BluetoothDevice* device) OVERRIDE {
146 ++call_count_;
147 ++request_passkey_count_;
148 QuitMessageLoop();
151 virtual void DisplayPinCode(BluetoothDevice* device,
152 const std::string& pincode) OVERRIDE {
153 ++call_count_;
154 ++display_pincode_count_;
155 last_pincode_ = pincode;
156 QuitMessageLoop();
159 virtual void DisplayPasskey(BluetoothDevice* device,
160 uint32 passkey) OVERRIDE {
161 ++call_count_;
162 ++display_passkey_count_;
163 last_passkey_ = passkey;
164 QuitMessageLoop();
167 virtual void KeysEntered(BluetoothDevice* device, uint32 entered) OVERRIDE {
168 ++call_count_;
169 ++keys_entered_count_;
170 last_entered_ = entered;
171 QuitMessageLoop();
174 virtual void ConfirmPasskey(BluetoothDevice* device,
175 uint32 passkey) OVERRIDE {
176 ++call_count_;
177 ++confirm_passkey_count_;
178 last_passkey_ = passkey;
179 QuitMessageLoop();
182 virtual void DismissDisplayOrConfirm() OVERRIDE {
183 ++call_count_;
184 ++dismiss_count_;
185 QuitMessageLoop();
188 int call_count_;
189 int request_pincode_count_;
190 int request_passkey_count_;
191 int display_pincode_count_;
192 int display_passkey_count_;
193 int keys_entered_count_;
194 int confirm_passkey_count_;
195 int dismiss_count_;
196 uint32 last_passkey_;
197 uint32 last_entered_;
198 std::string last_pincode_;
200 private:
201 // Some tests use a message loop since background processing is simulated;
202 // break out of those loops.
203 void QuitMessageLoop() {
204 if (base::MessageLoop::current() &&
205 base::MessageLoop::current()->is_running())
206 base::MessageLoop::current()->Quit();
210 class BluetoothExperimentalChromeOSTest : public testing::Test {
211 public:
212 virtual void SetUp() {
213 if (!CommandLine::ForCurrentProcess()->HasSwitch(
214 chromeos::switches::kEnableExperimentalBluetooth))
215 CommandLine::ForCurrentProcess()->AppendSwitch(
216 chromeos::switches::kEnableExperimentalBluetooth);
218 mock_dbus_thread_manager_ =
219 new MockDBusThreadManagerWithoutGMock();
220 DBusThreadManager::InitializeForTesting(mock_dbus_thread_manager_);
222 fake_bluetooth_adapter_client_ =
223 mock_dbus_thread_manager_->fake_bluetooth_adapter_client();
224 fake_bluetooth_device_client_ =
225 mock_dbus_thread_manager_->fake_bluetooth_device_client();
227 callback_count_ = 0;
228 error_callback_count_ = 0;
229 last_connect_error_ = BluetoothDevice::ERROR_UNKNOWN;
232 virtual void TearDown() {
233 adapter_ = NULL;
234 DBusThreadManager::Shutdown();
237 // Generic callbacks
238 void Callback() {
239 ++callback_count_;
242 void ErrorCallback() {
243 ++error_callback_count_;
246 void ConnectErrorCallback(enum BluetoothDevice::ConnectErrorCode error) {
247 ++error_callback_count_;
248 last_connect_error_ = error;
251 // Call to fill the adapter_ member with a BluetoothAdapter instance.
252 void GetAdapter() {
253 adapter_ = new BluetoothAdapterExperimentalChromeOS();
254 ASSERT_TRUE(adapter_ != NULL);
255 ASSERT_TRUE(adapter_->IsInitialized());
258 // Run a discovery phase until the named device is detected, or if the named
259 // device is not created, the discovery process ends without finding it.
261 // The correct behavior of discovery is tested by the "Discovery" test case
262 // without using this function.
263 void DiscoverDevice(const std::string& address) {
264 ASSERT_TRUE(adapter_ != NULL);
266 if (base::MessageLoop::current() == NULL) {
267 base::MessageLoop message_loop(base::MessageLoop::TYPE_DEFAULT);
268 DiscoverDevices();
269 return;
272 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
274 TestObserver observer(adapter_);
275 adapter_->AddObserver(&observer);
277 adapter_->SetPowered(
278 true,
279 base::Bind(&BluetoothExperimentalChromeOSTest::Callback,
280 base::Unretained(this)),
281 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback,
282 base::Unretained(this)));
283 adapter_->StartDiscovering(
284 base::Bind(&BluetoothExperimentalChromeOSTest::Callback,
285 base::Unretained(this)),
286 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback,
287 base::Unretained(this)));
288 ASSERT_EQ(2, callback_count_);
289 ASSERT_EQ(0, error_callback_count_);
290 callback_count_ = 0;
292 ASSERT_TRUE(adapter_->IsPowered());
293 ASSERT_TRUE(adapter_->IsDiscovering());
295 while (!observer.device_removed_count_ &&
296 observer.last_device_address_ != address)
297 base::MessageLoop::current()->Run();
299 adapter_->StopDiscovering(
300 base::Bind(&BluetoothExperimentalChromeOSTest::Callback,
301 base::Unretained(this)),
302 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback,
303 base::Unretained(this)));
304 ASSERT_EQ(1, callback_count_);
305 ASSERT_EQ(0, error_callback_count_);
306 callback_count_ = 0;
308 ASSERT_FALSE(adapter_->IsDiscovering());
310 adapter_->RemoveObserver(&observer);
313 // Run a discovery phase so we have devices that can be paired with.
314 void DiscoverDevices() {
315 // Pass an invalid address for the device so that the discovery process
316 // completes with all devices.
317 DiscoverDevice("does not exist");
320 protected:
321 FakeBluetoothAdapterClient* fake_bluetooth_adapter_client_;
322 FakeBluetoothDeviceClient* fake_bluetooth_device_client_;
323 MockDBusThreadManagerWithoutGMock* mock_dbus_thread_manager_;
324 scoped_refptr<BluetoothAdapter> adapter_;
326 int callback_count_;
327 int error_callback_count_;
328 enum BluetoothDevice::ConnectErrorCode last_connect_error_;
331 TEST_F(BluetoothExperimentalChromeOSTest, AlreadyPresent) {
332 GetAdapter();
334 // This verifies that the class gets the list of adapters when created;
335 // and initializes with an existing adapter if there is one.
336 EXPECT_TRUE(adapter_->IsPresent());
337 EXPECT_FALSE(adapter_->IsPowered());
338 EXPECT_EQ(FakeBluetoothAdapterClient::kAdapterAddress,
339 adapter_->GetAddress());
340 EXPECT_FALSE(adapter_->IsDiscovering());
342 // There should be a device
343 BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
344 EXPECT_EQ(1U, devices.size());
345 EXPECT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
346 devices[0]->GetAddress());
349 TEST_F(BluetoothExperimentalChromeOSTest, BecomePresent) {
350 fake_bluetooth_adapter_client_->SetVisible(false);
351 GetAdapter();
352 ASSERT_FALSE(adapter_->IsPresent());
354 // Install an observer; expect the AdapterPresentChanged to be called
355 // with true, and IsPresent() to return true.
356 TestObserver observer(adapter_);
357 adapter_->AddObserver(&observer);
359 fake_bluetooth_adapter_client_->SetVisible(true);
361 EXPECT_EQ(1, observer.present_changed_count_);
362 EXPECT_TRUE(observer.last_present_);
364 EXPECT_TRUE(adapter_->IsPresent());
366 // We should have had a device announced.
367 EXPECT_EQ(1, observer.device_added_count_);
368 EXPECT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
369 observer.last_device_address_);
371 // Other callbacks shouldn't be called if the values are false.
372 EXPECT_EQ(0, observer.powered_changed_count_);
373 EXPECT_EQ(0, observer.discovering_changed_count_);
374 EXPECT_FALSE(adapter_->IsPowered());
375 EXPECT_FALSE(adapter_->IsDiscovering());
378 TEST_F(BluetoothExperimentalChromeOSTest, BecomeNotPresent) {
379 GetAdapter();
380 ASSERT_TRUE(adapter_->IsPresent());
382 // Install an observer; expect the AdapterPresentChanged to be called
383 // with false, and IsPresent() to return false.
384 TestObserver observer(adapter_);
385 adapter_->AddObserver(&observer);
387 fake_bluetooth_adapter_client_->SetVisible(false);
389 EXPECT_EQ(1, observer.present_changed_count_);
390 EXPECT_FALSE(observer.last_present_);
392 EXPECT_FALSE(adapter_->IsPresent());
394 // We should have had a device removed.
395 EXPECT_EQ(1, observer.device_removed_count_);
396 EXPECT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
397 observer.last_device_address_);
399 // Other callbacks shouldn't be called since the values are false.
400 EXPECT_EQ(0, observer.powered_changed_count_);
401 EXPECT_EQ(0, observer.discovering_changed_count_);
402 EXPECT_FALSE(adapter_->IsPowered());
403 EXPECT_FALSE(adapter_->IsDiscovering());
406 TEST_F(BluetoothExperimentalChromeOSTest, SecondAdapter) {
407 GetAdapter();
408 ASSERT_TRUE(adapter_->IsPresent());
410 // Install an observer, then add a second adapter. Nothing should change,
411 // we ignore the second adapter.
412 TestObserver observer(adapter_);
413 adapter_->AddObserver(&observer);
415 fake_bluetooth_adapter_client_->SetSecondVisible(true);
417 EXPECT_EQ(0, observer.present_changed_count_);
419 EXPECT_TRUE(adapter_->IsPresent());
420 EXPECT_EQ(FakeBluetoothAdapterClient::kAdapterAddress,
421 adapter_->GetAddress());
423 // Try removing the first adapter, we should now act as if the adapter
424 // is no longer present rather than fall back to the second.
425 fake_bluetooth_adapter_client_->SetVisible(false);
427 EXPECT_EQ(1, observer.present_changed_count_);
428 EXPECT_FALSE(observer.last_present_);
430 EXPECT_FALSE(adapter_->IsPresent());
432 // We should have had a device removed.
433 EXPECT_EQ(1, observer.device_removed_count_);
434 EXPECT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
435 observer.last_device_address_);
437 // Other callbacks shouldn't be called since the values are false.
438 EXPECT_EQ(0, observer.powered_changed_count_);
439 EXPECT_EQ(0, observer.discovering_changed_count_);
440 EXPECT_FALSE(adapter_->IsPowered());
441 EXPECT_FALSE(adapter_->IsDiscovering());
443 observer.device_removed_count_ = 0;
445 // Removing the second adapter shouldn't set anything either.
446 fake_bluetooth_adapter_client_->SetSecondVisible(false);
448 EXPECT_EQ(0, observer.device_removed_count_);
449 EXPECT_EQ(0, observer.powered_changed_count_);
450 EXPECT_EQ(0, observer.discovering_changed_count_);
453 TEST_F(BluetoothExperimentalChromeOSTest, BecomePowered) {
454 GetAdapter();
455 ASSERT_FALSE(adapter_->IsPowered());
457 // Install an observer; expect the AdapterPoweredChanged to be called
458 // with true, and IsPowered() to return true.
459 TestObserver observer(adapter_);
460 adapter_->AddObserver(&observer);
462 adapter_->SetPowered(
463 true,
464 base::Bind(&BluetoothExperimentalChromeOSTest::Callback,
465 base::Unretained(this)),
466 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback,
467 base::Unretained(this)));
468 EXPECT_EQ(1, callback_count_);
469 EXPECT_EQ(0, error_callback_count_);
471 EXPECT_EQ(1, observer.powered_changed_count_);
472 EXPECT_TRUE(observer.last_powered_);
474 EXPECT_TRUE(adapter_->IsPowered());
477 TEST_F(BluetoothExperimentalChromeOSTest, BecomeNotPowered) {
478 GetAdapter();
479 adapter_->SetPowered(
480 true,
481 base::Bind(&BluetoothExperimentalChromeOSTest::Callback,
482 base::Unretained(this)),
483 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback,
484 base::Unretained(this)));
485 EXPECT_EQ(1, callback_count_);
486 EXPECT_EQ(0, error_callback_count_);
487 callback_count_ = 0;
489 ASSERT_TRUE(adapter_->IsPowered());
491 // Install an observer; expect the AdapterPoweredChanged to be called
492 // with false, and IsPowered() to return false.
493 TestObserver observer(adapter_);
494 adapter_->AddObserver(&observer);
496 adapter_->SetPowered(
497 false,
498 base::Bind(&BluetoothExperimentalChromeOSTest::Callback,
499 base::Unretained(this)),
500 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback,
501 base::Unretained(this)));
502 EXPECT_EQ(1, callback_count_);
503 EXPECT_EQ(0, error_callback_count_);
505 EXPECT_EQ(1, observer.powered_changed_count_);
506 EXPECT_FALSE(observer.last_powered_);
508 EXPECT_FALSE(adapter_->IsPowered());
511 TEST_F(BluetoothExperimentalChromeOSTest, StopDiscovery) {
512 base::MessageLoop message_loop(base::MessageLoop::TYPE_DEFAULT);
514 GetAdapter();
516 adapter_->SetPowered(
517 true,
518 base::Bind(&BluetoothExperimentalChromeOSTest::Callback,
519 base::Unretained(this)),
520 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback,
521 base::Unretained(this)));
522 adapter_->StartDiscovering(
523 base::Bind(&BluetoothExperimentalChromeOSTest::Callback,
524 base::Unretained(this)),
525 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback,
526 base::Unretained(this)));
527 EXPECT_EQ(2, callback_count_);
528 EXPECT_EQ(0, error_callback_count_);
529 callback_count_ = 0;
531 ASSERT_TRUE(adapter_->IsPowered());
532 ASSERT_TRUE(adapter_->IsDiscovering());
534 // Install an observer; aside from the callback, expect the
535 // AdapterDiscoveringChanged method to be called and no longer to be
536 // discovering,
537 TestObserver observer(adapter_);
538 adapter_->AddObserver(&observer);
540 adapter_->StopDiscovering(
541 base::Bind(&BluetoothExperimentalChromeOSTest::Callback,
542 base::Unretained(this)),
543 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback,
544 base::Unretained(this)));
545 EXPECT_EQ(1, callback_count_);
546 EXPECT_EQ(0, error_callback_count_);
548 EXPECT_EQ(1, observer.discovering_changed_count_);
549 EXPECT_FALSE(observer.last_discovering_);
551 EXPECT_FALSE(adapter_->IsDiscovering());
554 TEST_F(BluetoothExperimentalChromeOSTest, StopDiscoveryAfterTwoStarts) {
555 base::MessageLoop message_loop(base::MessageLoop::TYPE_DEFAULT);
557 GetAdapter();
559 adapter_->SetPowered(
560 true,
561 base::Bind(&BluetoothExperimentalChromeOSTest::Callback,
562 base::Unretained(this)),
563 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback,
564 base::Unretained(this)));
565 adapter_->StartDiscovering(
566 base::Bind(&BluetoothExperimentalChromeOSTest::Callback,
567 base::Unretained(this)),
568 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback,
569 base::Unretained(this)));
570 EXPECT_EQ(2, callback_count_);
571 EXPECT_EQ(0, error_callback_count_);
572 callback_count_ = 0;
574 ASSERT_TRUE(adapter_->IsPowered());
575 ASSERT_TRUE(adapter_->IsDiscovering());
577 // Install an observer and start discovering again; only the callback
578 // should be called since we were already discovering to begin with.
579 TestObserver observer(adapter_);
580 adapter_->AddObserver(&observer);
582 adapter_->StartDiscovering(
583 base::Bind(&BluetoothExperimentalChromeOSTest::Callback,
584 base::Unretained(this)),
585 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback,
586 base::Unretained(this)));
587 EXPECT_EQ(1, callback_count_);
588 EXPECT_EQ(0, error_callback_count_);
589 callback_count_ = 0;
591 EXPECT_EQ(0, observer.discovering_changed_count_);
593 // Stop discovering; only the callback should be called since we're still
594 // discovering. The adapter should be still discovering.
595 adapter_->StopDiscovering(
596 base::Bind(&BluetoothExperimentalChromeOSTest::Callback,
597 base::Unretained(this)),
598 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback,
599 base::Unretained(this)));
600 EXPECT_EQ(1, callback_count_);
601 EXPECT_EQ(0, error_callback_count_);
602 callback_count_ = 0;
604 EXPECT_EQ(0, observer.discovering_changed_count_);
606 EXPECT_TRUE(adapter_->IsDiscovering());
608 // Stop discovering one more time; aside from the callback, expect the
609 // AdapterDiscoveringChanged method to be called and no longer to be
610 // discovering,
611 adapter_->StopDiscovering(
612 base::Bind(&BluetoothExperimentalChromeOSTest::Callback,
613 base::Unretained(this)),
614 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback,
615 base::Unretained(this)));
616 EXPECT_EQ(1, callback_count_);
617 EXPECT_EQ(0, error_callback_count_);
619 EXPECT_EQ(1, observer.discovering_changed_count_);
620 EXPECT_FALSE(observer.last_discovering_);
622 EXPECT_FALSE(adapter_->IsDiscovering());
625 TEST_F(BluetoothExperimentalChromeOSTest, Discovery) {
626 // Test a simulated discovery session.
627 base::MessageLoop message_loop(base::MessageLoop::TYPE_DEFAULT);
629 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
630 GetAdapter();
632 TestObserver observer(adapter_);
633 adapter_->AddObserver(&observer);
635 adapter_->SetPowered(
636 true,
637 base::Bind(&BluetoothExperimentalChromeOSTest::Callback,
638 base::Unretained(this)),
639 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback,
640 base::Unretained(this)));
641 adapter_->StartDiscovering(
642 base::Bind(&BluetoothExperimentalChromeOSTest::Callback,
643 base::Unretained(this)),
644 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback,
645 base::Unretained(this)));
646 EXPECT_EQ(2, callback_count_);
647 EXPECT_EQ(0, error_callback_count_);
648 callback_count_ = 0;
650 ASSERT_TRUE(adapter_->IsPowered());
651 ASSERT_TRUE(adapter_->IsDiscovering());
653 // First device to appear should be an Apple Mouse.
654 message_loop.Run();
656 EXPECT_EQ(1, observer.device_added_count_);
657 EXPECT_EQ(FakeBluetoothDeviceClient::kAppleMouseAddress,
658 observer.last_device_address_);
660 // Next we should get another two devices...
661 message_loop.Run();
662 EXPECT_EQ(3, observer.device_added_count_);
664 // Okay, let's run forward until a device is actually removed...
665 while (!observer.device_removed_count_)
666 message_loop.Run();
668 EXPECT_EQ(1, observer.device_removed_count_);
669 EXPECT_EQ(FakeBluetoothDeviceClient::kVanishingDeviceAddress,
670 observer.last_device_address_);
673 TEST_F(BluetoothExperimentalChromeOSTest, PoweredAndDiscovering) {
674 base::MessageLoop message_loop(base::MessageLoop::TYPE_DEFAULT);
676 GetAdapter();
677 adapter_->SetPowered(
678 true,
679 base::Bind(&BluetoothExperimentalChromeOSTest::Callback,
680 base::Unretained(this)),
681 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback,
682 base::Unretained(this)));
683 adapter_->StartDiscovering(
684 base::Bind(&BluetoothExperimentalChromeOSTest::Callback,
685 base::Unretained(this)),
686 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback,
687 base::Unretained(this)));
688 EXPECT_EQ(2, callback_count_);
689 EXPECT_EQ(0, error_callback_count_);
690 callback_count_ = 0;
692 // Stop the timers that the simulation uses
693 fake_bluetooth_device_client_->EndDiscoverySimulation(
694 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath));
696 ASSERT_TRUE(adapter_->IsPowered());
697 ASSERT_TRUE(adapter_->IsDiscovering());
699 fake_bluetooth_adapter_client_->SetVisible(false);
700 ASSERT_FALSE(adapter_->IsPresent());
702 // Install an observer; expect the AdapterPresentChanged,
703 // AdapterPoweredChanged and AdapterDiscoveringChanged methods to be called
704 // with true, and IsPresent(), IsPowered() and IsDiscovering() to all
705 // return true.
706 TestObserver observer(adapter_);
707 adapter_->AddObserver(&observer);
709 fake_bluetooth_adapter_client_->SetVisible(true);
711 EXPECT_EQ(1, observer.present_changed_count_);
712 EXPECT_TRUE(observer.last_present_);
713 EXPECT_TRUE(adapter_->IsPresent());
715 EXPECT_EQ(1, observer.powered_changed_count_);
716 EXPECT_TRUE(observer.last_powered_);
717 EXPECT_TRUE(adapter_->IsPowered());
719 EXPECT_EQ(1, observer.discovering_changed_count_);
720 EXPECT_TRUE(observer.last_discovering_);
721 EXPECT_TRUE(adapter_->IsDiscovering());
723 observer.present_changed_count_ = 0;
724 observer.powered_changed_count_ = 0;
725 observer.discovering_changed_count_ = 0;
727 // Now mark the adapter not present again. Expect the methods to be called
728 // again, to reset the properties back to false
729 fake_bluetooth_adapter_client_->SetVisible(false);
731 EXPECT_EQ(1, observer.present_changed_count_);
732 EXPECT_FALSE(observer.last_present_);
733 EXPECT_FALSE(adapter_->IsPresent());
735 EXPECT_EQ(1, observer.powered_changed_count_);
736 EXPECT_FALSE(observer.last_powered_);
737 EXPECT_FALSE(adapter_->IsPowered());
739 EXPECT_EQ(1, observer.discovering_changed_count_);
740 EXPECT_FALSE(observer.last_discovering_);
741 EXPECT_FALSE(adapter_->IsDiscovering());
744 TEST_F(BluetoothExperimentalChromeOSTest, DeviceProperties) {
745 GetAdapter();
747 BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
748 ASSERT_EQ(1U, devices.size());
749 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
750 devices[0]->GetAddress());
752 // Verify the other device properties.
753 EXPECT_EQ(UTF8ToUTF16(FakeBluetoothDeviceClient::kPairedDeviceName),
754 devices[0]->GetName());
755 EXPECT_EQ(BluetoothDevice::DEVICE_COMPUTER, devices[0]->GetDeviceType());
756 EXPECT_TRUE(devices[0]->IsPaired());
757 EXPECT_FALSE(devices[0]->IsConnected());
758 EXPECT_FALSE(devices[0]->IsConnecting());
760 // Non HID devices are always connectable.
761 EXPECT_TRUE(devices[0]->IsConnectable());
763 BluetoothDevice::ServiceList uuids = devices[0]->GetServices();
764 ASSERT_EQ(2U, uuids.size());
765 EXPECT_EQ(uuids[0], "00001800-0000-1000-8000-00805f9b34fb");
766 EXPECT_EQ(uuids[1], "00001801-0000-1000-8000-00805f9b34fb");
768 EXPECT_EQ(0x05ac, devices[0]->GetVendorID());
769 EXPECT_EQ(0x030d, devices[0]->GetProductID());
770 EXPECT_EQ(0x0306, devices[0]->GetDeviceID());
773 TEST_F(BluetoothExperimentalChromeOSTest, DeviceClassChanged) {
774 // Simulate a change of class of a device, as sometimes occurs
775 // during discovery.
776 GetAdapter();
778 BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
779 ASSERT_EQ(1U, devices.size());
780 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
781 devices[0]->GetAddress());
782 ASSERT_EQ(BluetoothDevice::DEVICE_COMPUTER, devices[0]->GetDeviceType());
784 // Install an observer; expect the DeviceChanged method to be called when
785 // we change the class of the device.
786 TestObserver observer(adapter_);
787 adapter_->AddObserver(&observer);
789 FakeBluetoothDeviceClient::Properties* properties =
790 fake_bluetooth_device_client_->GetProperties(
791 dbus::ObjectPath(FakeBluetoothDeviceClient::kPairedDevicePath));
793 properties->bluetooth_class.ReplaceValue(0x002580);
795 EXPECT_EQ(1, observer.device_changed_count_);
796 EXPECT_EQ(devices[0], observer.last_device_);
798 EXPECT_EQ(BluetoothDevice::DEVICE_MOUSE, devices[0]->GetDeviceType());
801 TEST_F(BluetoothExperimentalChromeOSTest, DeviceNameChanged) {
802 // Simulate a change of name of a device.
803 GetAdapter();
805 BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
806 ASSERT_EQ(1U, devices.size());
807 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
808 devices[0]->GetAddress());
809 ASSERT_EQ(UTF8ToUTF16(FakeBluetoothDeviceClient::kPairedDeviceName),
810 devices[0]->GetName());
812 // Install an observer; expect the DeviceChanged method to be called when
813 // we change the alias of the device.
814 TestObserver observer(adapter_);
815 adapter_->AddObserver(&observer);
817 FakeBluetoothDeviceClient::Properties* properties =
818 fake_bluetooth_device_client_->GetProperties(
819 dbus::ObjectPath(FakeBluetoothDeviceClient::kPairedDevicePath));
821 static const std::string new_name("New Device Name");
822 properties->alias.ReplaceValue(new_name);
824 EXPECT_EQ(1, observer.device_changed_count_);
825 EXPECT_EQ(devices[0], observer.last_device_);
827 EXPECT_EQ(UTF8ToUTF16(new_name), devices[0]->GetName());
830 TEST_F(BluetoothExperimentalChromeOSTest, DeviceUuidsChanged) {
831 // Simulate a change of advertised services of a device.
832 GetAdapter();
834 BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
835 ASSERT_EQ(1U, devices.size());
836 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
837 devices[0]->GetAddress());
839 BluetoothDevice::ServiceList uuids = devices[0]->GetServices();
840 ASSERT_EQ(2U, uuids.size());
841 ASSERT_EQ(uuids[0], "00001800-0000-1000-8000-00805f9b34fb");
842 ASSERT_EQ(uuids[1], "00001801-0000-1000-8000-00805f9b34fb");
844 // Install an observer; expect the DeviceChanged method to be called when
845 // we change the class of the device.
846 TestObserver observer(adapter_);
847 adapter_->AddObserver(&observer);
849 FakeBluetoothDeviceClient::Properties* properties =
850 fake_bluetooth_device_client_->GetProperties(
851 dbus::ObjectPath(FakeBluetoothDeviceClient::kPairedDevicePath));
853 uuids.push_back("0000110c-0000-1000-8000-00805f9b34fb");
854 uuids.push_back("0000110e-0000-1000-8000-00805f9b34fb");
855 uuids.push_back("0000110a-0000-1000-8000-00805f9b34fb");
857 properties->uuids.ReplaceValue(uuids);
859 EXPECT_EQ(1, observer.device_changed_count_);
860 EXPECT_EQ(devices[0], observer.last_device_);
862 // Fetching the value should give the new one.
863 uuids = devices[0]->GetServices();
864 ASSERT_EQ(5U, uuids.size());
865 EXPECT_EQ(uuids[0], "00001800-0000-1000-8000-00805f9b34fb");
866 EXPECT_EQ(uuids[1], "00001801-0000-1000-8000-00805f9b34fb");
867 EXPECT_EQ(uuids[2], "0000110c-0000-1000-8000-00805f9b34fb");
868 EXPECT_EQ(uuids[3], "0000110e-0000-1000-8000-00805f9b34fb");
869 EXPECT_EQ(uuids[4], "0000110a-0000-1000-8000-00805f9b34fb");
872 TEST_F(BluetoothExperimentalChromeOSTest, ForgetDevice) {
873 GetAdapter();
875 BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
876 ASSERT_EQ(1U, devices.size());
877 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
878 devices[0]->GetAddress());
880 std::string address = devices[0]->GetAddress();
882 // Install an observer; expect the DeviceRemoved method to be called
883 // with the device we remove.
884 TestObserver observer(adapter_);
885 adapter_->AddObserver(&observer);
887 devices[0]->Forget(
888 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback,
889 base::Unretained(this)));
890 EXPECT_EQ(0, error_callback_count_);
892 EXPECT_EQ(1, observer.device_removed_count_);
893 EXPECT_EQ(address, observer.last_device_address_);
895 // GetDevices shouldn't return the device either.
896 devices = adapter_->GetDevices();
897 ASSERT_EQ(0U, devices.size());
900 TEST_F(BluetoothExperimentalChromeOSTest, ForgetUnpairedDevice) {
901 GetAdapter();
902 DiscoverDevices();
904 BluetoothDevice* device = adapter_->GetDevice(
905 FakeBluetoothDeviceClient::kMicrosoftMouseAddress);
906 ASSERT_TRUE(device != NULL);
907 ASSERT_FALSE(device->IsPaired());
909 // Connect the device so it becomes trusted and remembered.
910 device->Connect(
911 NULL,
912 base::Bind(&BluetoothExperimentalChromeOSTest::Callback,
913 base::Unretained(this)),
914 base::Bind(&BluetoothExperimentalChromeOSTest::ConnectErrorCallback,
915 base::Unretained(this)));
917 ASSERT_EQ(1, callback_count_);
918 ASSERT_EQ(0, error_callback_count_);
919 callback_count_ = 0;
921 ASSERT_TRUE(device->IsConnected());
922 ASSERT_FALSE(device->IsConnecting());
924 // Make sure the trusted property has been set to true.
925 FakeBluetoothDeviceClient::Properties* properties =
926 fake_bluetooth_device_client_->GetProperties(
927 dbus::ObjectPath(FakeBluetoothDeviceClient::kMicrosoftMousePath));
928 ASSERT_TRUE(properties->trusted.value());
930 // Install an observer; expect the DeviceRemoved method to be called
931 // with the device we remove.
932 TestObserver observer(adapter_);
933 adapter_->AddObserver(&observer);
935 device->Forget(
936 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback,
937 base::Unretained(this)));
938 EXPECT_EQ(0, error_callback_count_);
940 EXPECT_EQ(1, observer.device_removed_count_);
941 EXPECT_EQ(FakeBluetoothDeviceClient::kMicrosoftMouseAddress,
942 observer.last_device_address_);
944 // GetDevices shouldn't return the device either.
945 device = adapter_->GetDevice(
946 FakeBluetoothDeviceClient::kMicrosoftMouseAddress);
947 EXPECT_FALSE(device != NULL);
950 TEST_F(BluetoothExperimentalChromeOSTest, ConnectPairedDevice) {
951 GetAdapter();
953 BluetoothDevice* device = adapter_->GetDevice(
954 FakeBluetoothDeviceClient::kPairedDeviceAddress);
955 ASSERT_TRUE(device != NULL);
956 ASSERT_TRUE(device->IsPaired());
958 TestObserver observer(adapter_);
959 adapter_->AddObserver(&observer);
961 // Connect without a pairing delegate; since the device is already Paired
962 // this should succeed and the device should become connected.
963 device->Connect(
964 NULL,
965 base::Bind(&BluetoothExperimentalChromeOSTest::Callback,
966 base::Unretained(this)),
967 base::Bind(&BluetoothExperimentalChromeOSTest::ConnectErrorCallback,
968 base::Unretained(this)));
970 EXPECT_EQ(1, callback_count_);
971 EXPECT_EQ(0, error_callback_count_);
973 // Two changes for connecting, one for connected and one for for trusted
974 // after connecting.
975 EXPECT_EQ(4, observer.device_changed_count_);
976 EXPECT_EQ(device, observer.last_device_);
978 EXPECT_TRUE(device->IsConnected());
979 EXPECT_FALSE(device->IsConnecting());
982 TEST_F(BluetoothExperimentalChromeOSTest, ConnectUnpairableDevice) {
983 GetAdapter();
984 DiscoverDevices();
986 BluetoothDevice* device = adapter_->GetDevice(
987 FakeBluetoothDeviceClient::kMicrosoftMouseAddress);
988 ASSERT_TRUE(device != NULL);
989 ASSERT_FALSE(device->IsPaired());
991 TestObserver observer(adapter_);
992 adapter_->AddObserver(&observer);
994 // Connect without a pairing delegate; since the device does not require
995 // pairing, this should succeed and the device should become connected.
996 device->Connect(
997 NULL,
998 base::Bind(&BluetoothExperimentalChromeOSTest::Callback,
999 base::Unretained(this)),
1000 base::Bind(&BluetoothExperimentalChromeOSTest::ConnectErrorCallback,
1001 base::Unretained(this)));
1003 EXPECT_EQ(1, callback_count_);
1004 EXPECT_EQ(0, error_callback_count_);
1006 // Two changes for connecting, one for connected, one for for trusted after
1007 // connection, and one for the reconnect mode (IsConnectable).
1008 EXPECT_EQ(5, observer.device_changed_count_);
1009 EXPECT_EQ(device, observer.last_device_);
1011 EXPECT_TRUE(device->IsConnected());
1012 EXPECT_FALSE(device->IsConnecting());
1014 // Make sure the trusted property has been set to true.
1015 FakeBluetoothDeviceClient::Properties* properties =
1016 fake_bluetooth_device_client_->GetProperties(
1017 dbus::ObjectPath(FakeBluetoothDeviceClient::kMicrosoftMousePath));
1018 EXPECT_TRUE(properties->trusted.value());
1020 // Verify is a HID device and is not connectable.
1021 BluetoothDevice::ServiceList uuids = device->GetServices();
1022 ASSERT_EQ(1U, uuids.size());
1023 EXPECT_EQ(uuids[0], "00001124-0000-1000-8000-00805f9b34fb");
1024 EXPECT_FALSE(device->IsConnectable());
1027 TEST_F(BluetoothExperimentalChromeOSTest, ConnectConnectedDevice) {
1028 GetAdapter();
1030 BluetoothDevice* device = adapter_->GetDevice(
1031 FakeBluetoothDeviceClient::kPairedDeviceAddress);
1032 ASSERT_TRUE(device != NULL);
1033 ASSERT_TRUE(device->IsPaired());
1035 device->Connect(
1036 NULL,
1037 base::Bind(&BluetoothExperimentalChromeOSTest::Callback,
1038 base::Unretained(this)),
1039 base::Bind(&BluetoothExperimentalChromeOSTest::ConnectErrorCallback,
1040 base::Unretained(this)));
1042 ASSERT_EQ(1, callback_count_);
1043 ASSERT_EQ(0, error_callback_count_);
1044 callback_count_ = 0;
1046 ASSERT_TRUE(device->IsConnected());
1048 // Connect again; since the device is already Connected, this shouldn't do
1049 // anything to initiate the connection.
1050 TestObserver observer(adapter_);
1051 adapter_->AddObserver(&observer);
1053 device->Connect(
1054 NULL,
1055 base::Bind(&BluetoothExperimentalChromeOSTest::Callback,
1056 base::Unretained(this)),
1057 base::Bind(&BluetoothExperimentalChromeOSTest::ConnectErrorCallback,
1058 base::Unretained(this)));
1060 EXPECT_EQ(1, callback_count_);
1061 EXPECT_EQ(0, error_callback_count_);
1063 // The observer will be called because Connecting will toggle true and false,
1064 // and the trusted property will be updated to true.
1065 EXPECT_EQ(3, observer.device_changed_count_);
1067 EXPECT_TRUE(device->IsConnected());
1068 EXPECT_FALSE(device->IsConnecting());
1071 TEST_F(BluetoothExperimentalChromeOSTest, ConnectDeviceFails) {
1072 GetAdapter();
1073 DiscoverDevices();
1075 BluetoothDevice* device = adapter_->GetDevice(
1076 FakeBluetoothDeviceClient::kAppleMouseAddress);
1077 ASSERT_TRUE(device != NULL);
1078 ASSERT_FALSE(device->IsPaired());
1080 TestObserver observer(adapter_);
1081 adapter_->AddObserver(&observer);
1083 // Connect without a pairing delegate; since the device requires pairing,
1084 // this should fail with an error.
1085 device->Connect(
1086 NULL,
1087 base::Bind(&BluetoothExperimentalChromeOSTest::Callback,
1088 base::Unretained(this)),
1089 base::Bind(&BluetoothExperimentalChromeOSTest::ConnectErrorCallback,
1090 base::Unretained(this)));
1092 EXPECT_EQ(0, callback_count_);
1093 EXPECT_EQ(1, error_callback_count_);
1094 EXPECT_EQ(BluetoothDevice::ERROR_FAILED, last_connect_error_);
1096 EXPECT_EQ(2, observer.device_changed_count_);
1098 EXPECT_FALSE(device->IsConnected());
1099 EXPECT_FALSE(device->IsConnecting());
1102 TEST_F(BluetoothExperimentalChromeOSTest, DisconnectDevice) {
1103 GetAdapter();
1105 BluetoothDevice* device = adapter_->GetDevice(
1106 FakeBluetoothDeviceClient::kPairedDeviceAddress);
1107 ASSERT_TRUE(device != NULL);
1108 ASSERT_TRUE(device->IsPaired());
1110 device->Connect(
1111 NULL,
1112 base::Bind(&BluetoothExperimentalChromeOSTest::Callback,
1113 base::Unretained(this)),
1114 base::Bind(&BluetoothExperimentalChromeOSTest::ConnectErrorCallback,
1115 base::Unretained(this)));
1117 ASSERT_EQ(1, callback_count_);
1118 ASSERT_EQ(0, error_callback_count_);
1119 callback_count_ = 0;
1121 ASSERT_TRUE(device->IsConnected());
1122 ASSERT_FALSE(device->IsConnecting());
1124 // Disconnect the device, we should see the observer method fire and the
1125 // device get dropped.
1126 TestObserver observer(adapter_);
1127 adapter_->AddObserver(&observer);
1129 device->Disconnect(
1130 base::Bind(&BluetoothExperimentalChromeOSTest::Callback,
1131 base::Unretained(this)),
1132 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback,
1133 base::Unretained(this)));
1135 EXPECT_EQ(1, callback_count_);
1136 EXPECT_EQ(0, error_callback_count_);
1138 EXPECT_EQ(1, observer.device_changed_count_);
1139 EXPECT_EQ(device, observer.last_device_);
1141 EXPECT_FALSE(device->IsConnected());
1144 TEST_F(BluetoothExperimentalChromeOSTest, DisconnectUnconnectedDevice) {
1145 GetAdapter();
1147 BluetoothDevice* device = adapter_->GetDevice(
1148 FakeBluetoothDeviceClient::kPairedDeviceAddress);
1149 ASSERT_TRUE(device != NULL);
1150 ASSERT_TRUE(device->IsPaired());
1151 ASSERT_FALSE(device->IsConnected());
1153 // Disconnect the device, we should see the observer method fire and the
1154 // device get dropped.
1155 TestObserver observer(adapter_);
1156 adapter_->AddObserver(&observer);
1158 device->Disconnect(
1159 base::Bind(&BluetoothExperimentalChromeOSTest::Callback,
1160 base::Unretained(this)),
1161 base::Bind(&BluetoothExperimentalChromeOSTest::ErrorCallback,
1162 base::Unretained(this)));
1164 EXPECT_EQ(0, callback_count_);
1165 EXPECT_EQ(1, error_callback_count_);
1167 EXPECT_EQ(0, observer.device_changed_count_);
1169 EXPECT_FALSE(device->IsConnected());
1172 TEST_F(BluetoothExperimentalChromeOSTest, PairAppleMouse) {
1173 base::MessageLoop message_loop(base::MessageLoop::TYPE_DEFAULT);
1174 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
1176 GetAdapter();
1177 DiscoverDevices();
1179 // The Apple Mouse requires no PIN or Passkey to pair; this is equivalent
1180 // to Simple Secure Pairing or a device with a fixed 0000 PIN.
1181 BluetoothDevice* device = adapter_->GetDevice(
1182 FakeBluetoothDeviceClient::kAppleMouseAddress);
1183 ASSERT_TRUE(device != NULL);
1184 ASSERT_FALSE(device->IsPaired());
1186 TestObserver observer(adapter_);
1187 adapter_->AddObserver(&observer);
1189 TestPairingDelegate pairing_delegate;
1190 device->Connect(
1191 &pairing_delegate,
1192 base::Bind(&BluetoothExperimentalChromeOSTest::Callback,
1193 base::Unretained(this)),
1194 base::Bind(&BluetoothExperimentalChromeOSTest::ConnectErrorCallback,
1195 base::Unretained(this)));
1197 EXPECT_EQ(0, pairing_delegate.call_count_);
1198 EXPECT_TRUE(device->IsConnecting());
1200 message_loop.Run();
1202 EXPECT_EQ(1, callback_count_);
1203 EXPECT_EQ(0, error_callback_count_);
1205 // Two changes for connecting, one change for connected, one for paired,
1206 // two for trusted (after pairing and connection), and one for the reconnect
1207 // mode (IsConnectable).
1208 EXPECT_EQ(7, observer.device_changed_count_);
1209 EXPECT_EQ(device, observer.last_device_);
1211 EXPECT_TRUE(device->IsConnected());
1212 EXPECT_FALSE(device->IsConnecting());
1214 EXPECT_TRUE(device->IsPaired());
1216 // Verify is a HID device and is connectable.
1217 BluetoothDevice::ServiceList uuids = device->GetServices();
1218 ASSERT_EQ(1U, uuids.size());
1219 EXPECT_EQ(uuids[0], "00001124-0000-1000-8000-00805f9b34fb");
1220 EXPECT_TRUE(device->IsConnectable());
1222 // Pairing dialog should be dismissed
1223 EXPECT_EQ(1, pairing_delegate.call_count_);
1224 EXPECT_EQ(1, pairing_delegate.dismiss_count_);
1226 // Make sure the trusted property has been set to true.
1227 FakeBluetoothDeviceClient::Properties* properties =
1228 fake_bluetooth_device_client_->GetProperties(
1229 dbus::ObjectPath(FakeBluetoothDeviceClient::kAppleMousePath));
1230 EXPECT_TRUE(properties->trusted.value());
1233 TEST_F(BluetoothExperimentalChromeOSTest, PairAppleKeyboard) {
1234 base::MessageLoop message_loop(base::MessageLoop::TYPE_DEFAULT);
1235 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
1237 GetAdapter();
1238 DiscoverDevices();
1240 // The Apple Keyboard requires that we display a randomly generated
1241 // PIN on the screen.
1242 BluetoothDevice* device = adapter_->GetDevice(
1243 FakeBluetoothDeviceClient::kAppleKeyboardAddress);
1244 ASSERT_TRUE(device != NULL);
1245 ASSERT_FALSE(device->IsPaired());
1247 TestObserver observer(adapter_);
1248 adapter_->AddObserver(&observer);
1250 TestPairingDelegate pairing_delegate;
1251 device->Connect(
1252 &pairing_delegate,
1253 base::Bind(&BluetoothExperimentalChromeOSTest::Callback,
1254 base::Unretained(this)),
1255 base::Bind(&BluetoothExperimentalChromeOSTest::ConnectErrorCallback,
1256 base::Unretained(this)));
1258 EXPECT_EQ(1, pairing_delegate.call_count_);
1259 EXPECT_EQ(1, pairing_delegate.display_pincode_count_);
1260 EXPECT_EQ("123456", pairing_delegate.last_pincode_);
1261 EXPECT_TRUE(device->IsConnecting());
1263 message_loop.Run();
1265 EXPECT_EQ(1, callback_count_);
1266 EXPECT_EQ(0, error_callback_count_);
1268 // Two changes for connecting, one change for connected, one for paired,
1269 // two for trusted (after pairing and connection), and one for the reconnect
1270 // mode (IsConnectable).
1271 EXPECT_EQ(7, observer.device_changed_count_);
1272 EXPECT_EQ(device, observer.last_device_);
1274 EXPECT_TRUE(device->IsConnected());
1275 EXPECT_FALSE(device->IsConnecting());
1277 EXPECT_TRUE(device->IsPaired());
1279 // Verify is a HID device and is connectable.
1280 BluetoothDevice::ServiceList uuids = device->GetServices();
1281 ASSERT_EQ(1U, uuids.size());
1282 EXPECT_EQ(uuids[0], "00001124-0000-1000-8000-00805f9b34fb");
1283 EXPECT_TRUE(device->IsConnectable());
1285 // Pairing dialog should be dismissed
1286 EXPECT_EQ(1, pairing_delegate.dismiss_count_);
1288 // Make sure the trusted property has been set to true.
1289 FakeBluetoothDeviceClient::Properties* properties =
1290 fake_bluetooth_device_client_->GetProperties(
1291 dbus::ObjectPath(FakeBluetoothDeviceClient::kAppleKeyboardPath));
1292 EXPECT_TRUE(properties->trusted.value());
1295 TEST_F(BluetoothExperimentalChromeOSTest, PairMotorolaKeyboard) {
1296 base::MessageLoop message_loop(base::MessageLoop::TYPE_DEFAULT);
1297 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
1299 GetAdapter();
1300 DiscoverDevices();
1302 // The Motorola Keyboard requires that we display a randomly generated
1303 // Passkey on the screen, and notifies us as it's typed in.
1304 BluetoothDevice* device = adapter_->GetDevice(
1305 FakeBluetoothDeviceClient::kMotorolaKeyboardAddress);
1306 ASSERT_TRUE(device != NULL);
1307 ASSERT_FALSE(device->IsPaired());
1309 TestObserver observer(adapter_);
1310 adapter_->AddObserver(&observer);
1312 TestPairingDelegate pairing_delegate;
1313 device->Connect(
1314 &pairing_delegate,
1315 base::Bind(&BluetoothExperimentalChromeOSTest::Callback,
1316 base::Unretained(this)),
1317 base::Bind(&BluetoothExperimentalChromeOSTest::ConnectErrorCallback,
1318 base::Unretained(this)));
1320 // One call for DisplayPasskey() and one for KeysEntered().
1321 EXPECT_EQ(2, pairing_delegate.call_count_);
1322 EXPECT_EQ(1, pairing_delegate.display_passkey_count_);
1323 EXPECT_EQ(123456U, pairing_delegate.last_passkey_);
1324 EXPECT_EQ(1, pairing_delegate.keys_entered_count_);
1325 EXPECT_EQ(0U, pairing_delegate.last_entered_);
1327 EXPECT_TRUE(device->IsConnecting());
1329 // One call to KeysEntered() for each key, including [enter].
1330 for(int i = 1; i <= 7; ++i) {
1331 message_loop.Run();
1333 EXPECT_EQ(2 + i, pairing_delegate.call_count_);
1334 EXPECT_EQ(1 + i, pairing_delegate.keys_entered_count_);
1335 EXPECT_EQ(static_cast<uint32_t>(i), pairing_delegate.last_entered_);
1338 message_loop.Run();
1340 // 8 KeysEntered notifications (0 to 7, inclusive). Two aditional calls for
1341 // DisplayPasskey() and DismissDisplayOrConfirm().
1342 EXPECT_EQ(10, pairing_delegate.call_count_);
1343 EXPECT_EQ(8, pairing_delegate.keys_entered_count_);
1344 EXPECT_EQ(7U, pairing_delegate.last_entered_);
1346 EXPECT_EQ(1, callback_count_);
1347 EXPECT_EQ(0, error_callback_count_);
1349 // Two changes for connecting, one change for connected, one for paired,
1350 // two for trusted (after pairing and connection), and one for the reconnect
1351 // mode (IsConnectable).
1352 EXPECT_EQ(7, observer.device_changed_count_);
1353 EXPECT_EQ(device, observer.last_device_);
1355 EXPECT_TRUE(device->IsConnected());
1356 EXPECT_FALSE(device->IsConnecting());
1358 EXPECT_TRUE(device->IsPaired());
1360 // Verify is a HID device.
1361 BluetoothDevice::ServiceList uuids = device->GetServices();
1362 ASSERT_EQ(1U, uuids.size());
1363 EXPECT_EQ(uuids[0], "00001124-0000-1000-8000-00805f9b34fb");
1365 // Fake MotorolaKeyboard is not connectable.
1366 EXPECT_FALSE(device->IsConnectable());
1368 // Pairing dialog should be dismissed
1369 EXPECT_EQ(1, pairing_delegate.dismiss_count_);
1371 // Make sure the trusted property has been set to true.
1372 FakeBluetoothDeviceClient::Properties* properties =
1373 fake_bluetooth_device_client_->GetProperties(
1374 dbus::ObjectPath(FakeBluetoothDeviceClient::kMotorolaKeyboardPath));
1375 EXPECT_TRUE(properties->trusted.value());
1378 TEST_F(BluetoothExperimentalChromeOSTest, PairSonyHeadphones) {
1379 base::MessageLoop message_loop(base::MessageLoop::TYPE_DEFAULT);
1380 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
1382 GetAdapter();
1383 DiscoverDevices();
1385 // The Sony Headphones fake requires that the user enters a PIN for them.
1386 BluetoothDevice* device = adapter_->GetDevice(
1387 FakeBluetoothDeviceClient::kSonyHeadphonesAddress);
1388 ASSERT_TRUE(device != NULL);
1389 ASSERT_FALSE(device->IsPaired());
1391 TestObserver observer(adapter_);
1392 adapter_->AddObserver(&observer);
1394 TestPairingDelegate pairing_delegate;
1395 device->Connect(
1396 &pairing_delegate,
1397 base::Bind(&BluetoothExperimentalChromeOSTest::Callback,
1398 base::Unretained(this)),
1399 base::Bind(&BluetoothExperimentalChromeOSTest::ConnectErrorCallback,
1400 base::Unretained(this)));
1402 EXPECT_EQ(1, pairing_delegate.call_count_);
1403 EXPECT_EQ(1, pairing_delegate.request_pincode_count_);
1404 EXPECT_TRUE(device->IsConnecting());
1406 // Set the PIN.
1407 device->SetPinCode("1234");
1408 message_loop.Run();
1410 EXPECT_EQ(1, callback_count_);
1411 EXPECT_EQ(0, error_callback_count_);
1413 // Two changes for connecting, one change for connected, one for paired and
1414 // two for trusted (after pairing and connection).
1415 EXPECT_EQ(6, observer.device_changed_count_);
1416 EXPECT_EQ(device, observer.last_device_);
1418 EXPECT_TRUE(device->IsConnected());
1419 EXPECT_FALSE(device->IsConnecting());
1421 EXPECT_TRUE(device->IsPaired());
1423 // Verify is not a HID device.
1424 BluetoothDevice::ServiceList uuids = device->GetServices();
1425 ASSERT_EQ(0U, uuids.size());
1427 // Non HID devices are always connectable.
1428 EXPECT_TRUE(device->IsConnectable());
1430 // Pairing dialog should be dismissed
1431 EXPECT_EQ(2, pairing_delegate.call_count_);
1432 EXPECT_EQ(1, pairing_delegate.dismiss_count_);
1434 // Make sure the trusted property has been set to true.
1435 FakeBluetoothDeviceClient::Properties* properties =
1436 fake_bluetooth_device_client_->GetProperties(
1437 dbus::ObjectPath(FakeBluetoothDeviceClient::kSonyHeadphonesPath));
1438 EXPECT_TRUE(properties->trusted.value());
1441 TEST_F(BluetoothExperimentalChromeOSTest, PairPhone) {
1442 base::MessageLoop message_loop(base::MessageLoop::TYPE_DEFAULT);
1443 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
1445 GetAdapter();
1446 DiscoverDevices();
1448 // The fake phone requests that we confirm a displayed passkey.
1449 BluetoothDevice* device = adapter_->GetDevice(
1450 FakeBluetoothDeviceClient::kPhoneAddress);
1451 ASSERT_TRUE(device != NULL);
1452 ASSERT_FALSE(device->IsPaired());
1454 TestObserver observer(adapter_);
1455 adapter_->AddObserver(&observer);
1457 TestPairingDelegate pairing_delegate;
1458 device->Connect(
1459 &pairing_delegate,
1460 base::Bind(&BluetoothExperimentalChromeOSTest::Callback,
1461 base::Unretained(this)),
1462 base::Bind(&BluetoothExperimentalChromeOSTest::ConnectErrorCallback,
1463 base::Unretained(this)));
1465 EXPECT_EQ(1, pairing_delegate.call_count_);
1466 EXPECT_EQ(1, pairing_delegate.confirm_passkey_count_);
1467 EXPECT_EQ(123456U, pairing_delegate.last_passkey_);
1468 EXPECT_TRUE(device->IsConnecting());
1470 // Confirm the passkey.
1471 device->ConfirmPairing();
1472 message_loop.Run();
1474 EXPECT_EQ(1, callback_count_);
1475 EXPECT_EQ(0, error_callback_count_);
1477 // Two changes for connecting, one change for connected, one for paired and
1478 // two for trusted (after pairing and connection).
1479 EXPECT_EQ(6, observer.device_changed_count_);
1480 EXPECT_EQ(device, observer.last_device_);
1482 EXPECT_TRUE(device->IsConnected());
1483 EXPECT_FALSE(device->IsConnecting());
1485 EXPECT_TRUE(device->IsPaired());
1487 // Non HID devices are always connectable.
1488 EXPECT_TRUE(device->IsConnectable());
1490 // Pairing dialog should be dismissed
1491 EXPECT_EQ(2, pairing_delegate.call_count_);
1492 EXPECT_EQ(1, pairing_delegate.dismiss_count_);
1494 // Make sure the trusted property has been set to true.
1495 FakeBluetoothDeviceClient::Properties* properties =
1496 fake_bluetooth_device_client_->GetProperties(
1497 dbus::ObjectPath(FakeBluetoothDeviceClient::kPhonePath));
1498 EXPECT_TRUE(properties->trusted.value());
1501 TEST_F(BluetoothExperimentalChromeOSTest, PairWeirdDevice) {
1502 base::MessageLoop message_loop(base::MessageLoop::TYPE_DEFAULT);
1503 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
1505 GetAdapter();
1506 DiscoverDevices();
1508 // Use the "weird device" fake that requires that the user enters a Passkey,
1509 // this would be some kind of device that has a display, but doesn't use
1510 // "just works" - maybe a car?
1511 BluetoothDevice* device = adapter_->GetDevice(
1512 FakeBluetoothDeviceClient::kWeirdDeviceAddress);
1513 ASSERT_TRUE(device != NULL);
1514 ASSERT_FALSE(device->IsPaired());
1516 TestObserver observer(adapter_);
1517 adapter_->AddObserver(&observer);
1519 TestPairingDelegate pairing_delegate;
1520 device->Connect(
1521 &pairing_delegate,
1522 base::Bind(&BluetoothExperimentalChromeOSTest::Callback,
1523 base::Unretained(this)),
1524 base::Bind(&BluetoothExperimentalChromeOSTest::ConnectErrorCallback,
1525 base::Unretained(this)));
1527 EXPECT_EQ(1, pairing_delegate.call_count_);
1528 EXPECT_EQ(1, pairing_delegate.request_passkey_count_);
1529 EXPECT_TRUE(device->IsConnecting());
1531 // Set the Passkey.
1532 device->SetPasskey(1234);
1533 message_loop.Run();
1535 EXPECT_EQ(1, callback_count_);
1536 EXPECT_EQ(0, error_callback_count_);
1538 // Two changes for connecting, one change for connected, one for paired and
1539 // two for trusted (after pairing and connection).
1540 EXPECT_EQ(6, observer.device_changed_count_);
1541 EXPECT_EQ(device, observer.last_device_);
1543 EXPECT_TRUE(device->IsConnected());
1544 EXPECT_FALSE(device->IsConnecting());
1546 EXPECT_TRUE(device->IsPaired());
1548 // Non HID devices are always connectable.
1549 EXPECT_TRUE(device->IsConnectable());
1551 // Pairing dialog should be dismissed
1552 EXPECT_EQ(2, pairing_delegate.call_count_);
1553 EXPECT_EQ(1, pairing_delegate.dismiss_count_);
1555 // Make sure the trusted property has been set to true.
1556 FakeBluetoothDeviceClient::Properties* properties =
1557 fake_bluetooth_device_client_->GetProperties(
1558 dbus::ObjectPath(FakeBluetoothDeviceClient::kWeirdDevicePath));
1559 EXPECT_TRUE(properties->trusted.value());
1562 TEST_F(BluetoothExperimentalChromeOSTest, PairUnpairableDeviceFails) {
1563 base::MessageLoop message_loop(base::MessageLoop::TYPE_DEFAULT);
1564 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
1566 GetAdapter();
1567 DiscoverDevice(FakeBluetoothDeviceClient::kUnconnectableDeviceAddress);
1569 BluetoothDevice* device = adapter_->GetDevice(
1570 FakeBluetoothDeviceClient::kUnpairableDeviceAddress);
1571 ASSERT_TRUE(device != NULL);
1572 ASSERT_FALSE(device->IsPaired());
1574 TestObserver observer(adapter_);
1575 adapter_->AddObserver(&observer);
1577 TestPairingDelegate pairing_delegate;
1578 device->Connect(
1579 &pairing_delegate,
1580 base::Bind(&BluetoothExperimentalChromeOSTest::Callback,
1581 base::Unretained(this)),
1582 base::Bind(&BluetoothExperimentalChromeOSTest::ConnectErrorCallback,
1583 base::Unretained(this)));
1585 EXPECT_EQ(0, pairing_delegate.call_count_);
1586 EXPECT_TRUE(device->IsConnecting());
1588 // Run the loop to get the error..
1589 message_loop.Run();
1591 EXPECT_EQ(0, callback_count_);
1592 EXPECT_EQ(1, error_callback_count_);
1594 EXPECT_EQ(BluetoothDevice::ERROR_FAILED, last_connect_error_);
1596 EXPECT_FALSE(device->IsConnected());
1597 EXPECT_FALSE(device->IsConnecting());
1598 EXPECT_FALSE(device->IsPaired());
1600 // Pairing dialog should be dismissed
1601 EXPECT_EQ(1, pairing_delegate.call_count_);
1602 EXPECT_EQ(1, pairing_delegate.dismiss_count_);
1605 TEST_F(BluetoothExperimentalChromeOSTest, PairingFails) {
1606 base::MessageLoop message_loop(base::MessageLoop::TYPE_DEFAULT);
1607 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
1609 GetAdapter();
1610 DiscoverDevice(FakeBluetoothDeviceClient::kVanishingDeviceAddress);
1612 // The vanishing device times out during pairing
1613 BluetoothDevice* device = adapter_->GetDevice(
1614 FakeBluetoothDeviceClient::kVanishingDeviceAddress);
1615 ASSERT_TRUE(device != NULL);
1616 ASSERT_FALSE(device->IsPaired());
1618 TestObserver observer(adapter_);
1619 adapter_->AddObserver(&observer);
1621 TestPairingDelegate pairing_delegate;
1622 device->Connect(
1623 &pairing_delegate,
1624 base::Bind(&BluetoothExperimentalChromeOSTest::Callback,
1625 base::Unretained(this)),
1626 base::Bind(&BluetoothExperimentalChromeOSTest::ConnectErrorCallback,
1627 base::Unretained(this)));
1629 EXPECT_EQ(0, pairing_delegate.call_count_);
1630 EXPECT_TRUE(device->IsConnecting());
1632 // Run the loop to get the error..
1633 message_loop.Run();
1635 EXPECT_EQ(0, callback_count_);
1636 EXPECT_EQ(1, error_callback_count_);
1638 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_TIMEOUT, last_connect_error_);
1640 EXPECT_FALSE(device->IsConnected());
1641 EXPECT_FALSE(device->IsConnecting());
1642 EXPECT_FALSE(device->IsPaired());
1644 // Pairing dialog should be dismissed
1645 EXPECT_EQ(1, pairing_delegate.call_count_);
1646 EXPECT_EQ(1, pairing_delegate.dismiss_count_);
1649 TEST_F(BluetoothExperimentalChromeOSTest, PairingFailsAtConnection) {
1650 base::MessageLoop message_loop(base::MessageLoop::TYPE_DEFAULT);
1651 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
1653 GetAdapter();
1654 DiscoverDevices();
1656 // Everything seems to go according to plan with the unconnectable device;
1657 // it pairs, but then you can't make connections to it after.
1658 BluetoothDevice* device = adapter_->GetDevice(
1659 FakeBluetoothDeviceClient::kUnconnectableDeviceAddress);
1660 ASSERT_TRUE(device != NULL);
1661 ASSERT_FALSE(device->IsPaired());
1663 TestObserver observer(adapter_);
1664 adapter_->AddObserver(&observer);
1666 TestPairingDelegate pairing_delegate;
1667 device->Connect(
1668 &pairing_delegate,
1669 base::Bind(&BluetoothExperimentalChromeOSTest::Callback,
1670 base::Unretained(this)),
1671 base::Bind(&BluetoothExperimentalChromeOSTest::ConnectErrorCallback,
1672 base::Unretained(this)));
1674 EXPECT_EQ(0, pairing_delegate.call_count_);
1675 EXPECT_TRUE(device->IsConnecting());
1677 message_loop.Run();
1679 EXPECT_EQ(0, callback_count_);
1680 EXPECT_EQ(1, error_callback_count_);
1681 EXPECT_EQ(BluetoothDevice::ERROR_FAILED, last_connect_error_);
1683 // Two changes for connecting, one for paired and one for trusted after
1684 // pairing. The device should not be connected.
1685 EXPECT_EQ(4, observer.device_changed_count_);
1686 EXPECT_EQ(device, observer.last_device_);
1688 EXPECT_FALSE(device->IsConnected());
1689 EXPECT_FALSE(device->IsConnecting());
1691 EXPECT_TRUE(device->IsPaired());
1693 // Pairing dialog should be dismissed
1694 EXPECT_EQ(1, pairing_delegate.call_count_);
1695 EXPECT_EQ(1, pairing_delegate.dismiss_count_);
1697 // Make sure the trusted property has been set to true still (since pairing
1698 // worked).
1699 FakeBluetoothDeviceClient::Properties* properties =
1700 fake_bluetooth_device_client_->GetProperties(
1701 dbus::ObjectPath(
1702 FakeBluetoothDeviceClient::kUnconnectableDevicePath));
1703 EXPECT_TRUE(properties->trusted.value());
1706 TEST_F(BluetoothExperimentalChromeOSTest, PairingRejectedAtPinCode) {
1707 base::MessageLoop message_loop(base::MessageLoop::TYPE_DEFAULT);
1708 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
1710 GetAdapter();
1711 DiscoverDevices();
1713 // Reject the pairing after we receive a request for the PIN code.
1714 BluetoothDevice* device = adapter_->GetDevice(
1715 FakeBluetoothDeviceClient::kSonyHeadphonesAddress);
1716 ASSERT_TRUE(device != NULL);
1717 ASSERT_FALSE(device->IsPaired());
1719 TestObserver observer(adapter_);
1720 adapter_->AddObserver(&observer);
1722 TestPairingDelegate pairing_delegate;
1723 device->Connect(
1724 &pairing_delegate,
1725 base::Bind(&BluetoothExperimentalChromeOSTest::Callback,
1726 base::Unretained(this)),
1727 base::Bind(&BluetoothExperimentalChromeOSTest::ConnectErrorCallback,
1728 base::Unretained(this)));
1730 EXPECT_EQ(1, pairing_delegate.call_count_);
1731 EXPECT_EQ(1, pairing_delegate.request_pincode_count_);
1732 EXPECT_TRUE(device->IsConnecting());
1734 // Reject the pairing.
1735 device->RejectPairing();
1736 message_loop.Run();
1738 EXPECT_EQ(0, callback_count_);
1739 EXPECT_EQ(1, error_callback_count_);
1740 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_REJECTED, last_connect_error_);
1742 // Should be no changes except connecting going true and false.
1743 EXPECT_EQ(2, observer.device_changed_count_);
1744 EXPECT_FALSE(device->IsConnected());
1745 EXPECT_FALSE(device->IsConnecting());
1746 EXPECT_FALSE(device->IsPaired());
1748 // Pairing dialog should be dismissed
1749 EXPECT_EQ(2, pairing_delegate.call_count_);
1750 EXPECT_EQ(1, pairing_delegate.dismiss_count_);
1753 TEST_F(BluetoothExperimentalChromeOSTest, PairingCancelledAtPinCode) {
1754 base::MessageLoop message_loop(base::MessageLoop::TYPE_DEFAULT);
1755 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
1757 GetAdapter();
1758 DiscoverDevices();
1760 // Cancel the pairing after we receive a request for the PIN code.
1761 BluetoothDevice* device = adapter_->GetDevice(
1762 FakeBluetoothDeviceClient::kSonyHeadphonesAddress);
1763 ASSERT_TRUE(device != NULL);
1764 ASSERT_FALSE(device->IsPaired());
1766 TestObserver observer(adapter_);
1767 adapter_->AddObserver(&observer);
1769 TestPairingDelegate pairing_delegate;
1770 device->Connect(
1771 &pairing_delegate,
1772 base::Bind(&BluetoothExperimentalChromeOSTest::Callback,
1773 base::Unretained(this)),
1774 base::Bind(&BluetoothExperimentalChromeOSTest::ConnectErrorCallback,
1775 base::Unretained(this)));
1777 EXPECT_EQ(1, pairing_delegate.call_count_);
1778 EXPECT_EQ(1, pairing_delegate.request_pincode_count_);
1779 EXPECT_TRUE(device->IsConnecting());
1781 // Cancel the pairing.
1782 device->CancelPairing();
1783 message_loop.Run();
1785 EXPECT_EQ(0, callback_count_);
1786 EXPECT_EQ(1, error_callback_count_);
1787 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED, last_connect_error_);
1789 // Should be no changes except connecting going true and false.
1790 EXPECT_EQ(2, observer.device_changed_count_);
1791 EXPECT_FALSE(device->IsConnected());
1792 EXPECT_FALSE(device->IsConnecting());
1793 EXPECT_FALSE(device->IsPaired());
1795 // Pairing dialog should be dismissed
1796 EXPECT_EQ(2, pairing_delegate.call_count_);
1797 EXPECT_EQ(1, pairing_delegate.dismiss_count_);
1800 TEST_F(BluetoothExperimentalChromeOSTest, PairingRejectedAtPasskey) {
1801 base::MessageLoop message_loop(base::MessageLoop::TYPE_DEFAULT);
1802 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
1804 GetAdapter();
1805 DiscoverDevices();
1807 // Reject the pairing after we receive a request for the passkey.
1808 BluetoothDevice* device = adapter_->GetDevice(
1809 FakeBluetoothDeviceClient::kWeirdDeviceAddress);
1810 ASSERT_TRUE(device != NULL);
1811 ASSERT_FALSE(device->IsPaired());
1813 TestObserver observer(adapter_);
1814 adapter_->AddObserver(&observer);
1816 TestPairingDelegate pairing_delegate;
1817 device->Connect(
1818 &pairing_delegate,
1819 base::Bind(&BluetoothExperimentalChromeOSTest::Callback,
1820 base::Unretained(this)),
1821 base::Bind(&BluetoothExperimentalChromeOSTest::ConnectErrorCallback,
1822 base::Unretained(this)));
1824 EXPECT_EQ(1, pairing_delegate.call_count_);
1825 EXPECT_EQ(1, pairing_delegate.request_passkey_count_);
1826 EXPECT_TRUE(device->IsConnecting());
1828 // Reject the pairing.
1829 device->RejectPairing();
1830 message_loop.Run();
1832 EXPECT_EQ(0, callback_count_);
1833 EXPECT_EQ(1, error_callback_count_);
1834 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_REJECTED, last_connect_error_);
1836 // Should be no changes except connecting going true and false.
1837 EXPECT_EQ(2, observer.device_changed_count_);
1838 EXPECT_FALSE(device->IsConnected());
1839 EXPECT_FALSE(device->IsConnecting());
1840 EXPECT_FALSE(device->IsPaired());
1842 // Pairing dialog should be dismissed
1843 EXPECT_EQ(2, pairing_delegate.call_count_);
1844 EXPECT_EQ(1, pairing_delegate.dismiss_count_);
1847 TEST_F(BluetoothExperimentalChromeOSTest, PairingCancelledAtPasskey) {
1848 base::MessageLoop message_loop(base::MessageLoop::TYPE_DEFAULT);
1849 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
1851 GetAdapter();
1852 DiscoverDevices();
1854 // Cancel the pairing after we receive a request for the passkey.
1855 BluetoothDevice* device = adapter_->GetDevice(
1856 FakeBluetoothDeviceClient::kWeirdDeviceAddress);
1857 ASSERT_TRUE(device != NULL);
1858 ASSERT_FALSE(device->IsPaired());
1860 TestObserver observer(adapter_);
1861 adapter_->AddObserver(&observer);
1863 TestPairingDelegate pairing_delegate;
1864 device->Connect(
1865 &pairing_delegate,
1866 base::Bind(&BluetoothExperimentalChromeOSTest::Callback,
1867 base::Unretained(this)),
1868 base::Bind(&BluetoothExperimentalChromeOSTest::ConnectErrorCallback,
1869 base::Unretained(this)));
1871 EXPECT_EQ(1, pairing_delegate.call_count_);
1872 EXPECT_EQ(1, pairing_delegate.request_passkey_count_);
1873 EXPECT_TRUE(device->IsConnecting());
1875 // Cancel the pairing.
1876 device->CancelPairing();
1877 message_loop.Run();
1879 EXPECT_EQ(0, callback_count_);
1880 EXPECT_EQ(1, error_callback_count_);
1881 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED, last_connect_error_);
1883 // Should be no changes except connecting going true and false.
1884 EXPECT_EQ(2, observer.device_changed_count_);
1885 EXPECT_FALSE(device->IsConnected());
1886 EXPECT_FALSE(device->IsConnecting());
1887 EXPECT_FALSE(device->IsPaired());
1889 // Pairing dialog should be dismissed
1890 EXPECT_EQ(2, pairing_delegate.call_count_);
1891 EXPECT_EQ(1, pairing_delegate.dismiss_count_);
1894 TEST_F(BluetoothExperimentalChromeOSTest, PairingRejectedAtConfirmation) {
1895 base::MessageLoop message_loop(base::MessageLoop::TYPE_DEFAULT);
1896 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
1898 GetAdapter();
1899 DiscoverDevices();
1901 // Reject the pairing after we receive a request for passkey confirmation.
1902 BluetoothDevice* device = adapter_->GetDevice(
1903 FakeBluetoothDeviceClient::kPhoneAddress);
1904 ASSERT_TRUE(device != NULL);
1905 ASSERT_FALSE(device->IsPaired());
1907 TestObserver observer(adapter_);
1908 adapter_->AddObserver(&observer);
1910 TestPairingDelegate pairing_delegate;
1911 device->Connect(
1912 &pairing_delegate,
1913 base::Bind(&BluetoothExperimentalChromeOSTest::Callback,
1914 base::Unretained(this)),
1915 base::Bind(&BluetoothExperimentalChromeOSTest::ConnectErrorCallback,
1916 base::Unretained(this)));
1918 EXPECT_EQ(1, pairing_delegate.call_count_);
1919 EXPECT_EQ(1, pairing_delegate.confirm_passkey_count_);
1920 EXPECT_TRUE(device->IsConnecting());
1922 // Reject the pairing.
1923 device->RejectPairing();
1924 message_loop.Run();
1926 EXPECT_EQ(0, callback_count_);
1927 EXPECT_EQ(1, error_callback_count_);
1928 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_REJECTED, last_connect_error_);
1930 // Should be no changes except connecting going true and false.
1931 EXPECT_EQ(2, observer.device_changed_count_);
1932 EXPECT_FALSE(device->IsConnected());
1933 EXPECT_FALSE(device->IsConnecting());
1934 EXPECT_FALSE(device->IsPaired());
1936 // Pairing dialog should be dismissed
1937 EXPECT_EQ(2, pairing_delegate.call_count_);
1938 EXPECT_EQ(1, pairing_delegate.dismiss_count_);
1941 TEST_F(BluetoothExperimentalChromeOSTest, PairingCancelledAtConfirmation) {
1942 base::MessageLoop message_loop(base::MessageLoop::TYPE_DEFAULT);
1943 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
1945 GetAdapter();
1946 DiscoverDevices();
1948 // Cancel the pairing after we receive a request for the passkey.
1949 BluetoothDevice* device = adapter_->GetDevice(
1950 FakeBluetoothDeviceClient::kPhoneAddress);
1951 ASSERT_TRUE(device != NULL);
1952 ASSERT_FALSE(device->IsPaired());
1954 TestObserver observer(adapter_);
1955 adapter_->AddObserver(&observer);
1957 TestPairingDelegate pairing_delegate;
1958 device->Connect(
1959 &pairing_delegate,
1960 base::Bind(&BluetoothExperimentalChromeOSTest::Callback,
1961 base::Unretained(this)),
1962 base::Bind(&BluetoothExperimentalChromeOSTest::ConnectErrorCallback,
1963 base::Unretained(this)));
1965 EXPECT_EQ(1, pairing_delegate.call_count_);
1966 EXPECT_EQ(1, pairing_delegate.confirm_passkey_count_);
1967 EXPECT_TRUE(device->IsConnecting());
1969 // Cancel the pairing.
1970 device->CancelPairing();
1971 message_loop.Run();
1973 EXPECT_EQ(0, callback_count_);
1974 EXPECT_EQ(1, error_callback_count_);
1975 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED, last_connect_error_);
1977 // Should be no changes except connecting going true and false.
1978 EXPECT_EQ(2, observer.device_changed_count_);
1979 EXPECT_FALSE(device->IsConnected());
1980 EXPECT_FALSE(device->IsConnecting());
1981 EXPECT_FALSE(device->IsPaired());
1983 // Pairing dialog should be dismissed
1984 EXPECT_EQ(2, pairing_delegate.call_count_);
1985 EXPECT_EQ(1, pairing_delegate.dismiss_count_);
1988 TEST_F(BluetoothExperimentalChromeOSTest, PairingCancelledInFlight) {
1989 base::MessageLoop message_loop(base::MessageLoop::TYPE_DEFAULT);
1990 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
1992 GetAdapter();
1993 DiscoverDevices();
1995 // Cancel the pairing while we're waiting for the remote host.
1996 BluetoothDevice* device = adapter_->GetDevice(
1997 FakeBluetoothDeviceClient::kAppleMouseAddress);
1998 ASSERT_TRUE(device != NULL);
1999 ASSERT_FALSE(device->IsPaired());
2001 TestObserver observer(adapter_);
2002 adapter_->AddObserver(&observer);
2004 TestPairingDelegate pairing_delegate;
2005 device->Connect(
2006 &pairing_delegate,
2007 base::Bind(&BluetoothExperimentalChromeOSTest::Callback,
2008 base::Unretained(this)),
2009 base::Bind(&BluetoothExperimentalChromeOSTest::ConnectErrorCallback,
2010 base::Unretained(this)));
2012 EXPECT_EQ(0, pairing_delegate.call_count_);
2013 EXPECT_TRUE(device->IsConnecting());
2015 // Cancel the pairing.
2016 device->CancelPairing();
2017 message_loop.Run();
2019 EXPECT_EQ(0, callback_count_);
2020 EXPECT_EQ(1, error_callback_count_);
2021 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED, last_connect_error_);
2023 // Should be no changes except connecting going true and false.
2024 EXPECT_EQ(2, observer.device_changed_count_);
2025 EXPECT_FALSE(device->IsConnected());
2026 EXPECT_FALSE(device->IsConnecting());
2027 EXPECT_FALSE(device->IsPaired());
2029 // Pairing dialog should be dismissed
2030 EXPECT_EQ(1, pairing_delegate.call_count_);
2031 EXPECT_EQ(1, pairing_delegate.dismiss_count_);
2034 } // namespace chromeos