PlzNavigate: send history params at commit time to the renderer
[chromium-blink-merge.git] / device / bluetooth / bluetooth_chromeos_unittest.cc
blob7da63b789c4de98ae3c903bcf58f45763f4fbe95
1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/memory/scoped_vector.h"
6 #include "base/message_loop/message_loop.h"
7 #include "base/run_loop.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "chromeos/dbus/dbus_thread_manager.h"
10 #include "chromeos/dbus/fake_bluetooth_adapter_client.h"
11 #include "chromeos/dbus/fake_bluetooth_agent_manager_client.h"
12 #include "chromeos/dbus/fake_bluetooth_device_client.h"
13 #include "chromeos/dbus/fake_bluetooth_gatt_service_client.h"
14 #include "chromeos/dbus/fake_bluetooth_input_client.h"
15 #include "dbus/object_path.h"
16 #include "device/bluetooth/bluetooth_adapter.h"
17 #include "device/bluetooth/bluetooth_adapter_chromeos.h"
18 #include "device/bluetooth/bluetooth_adapter_factory.h"
19 #include "device/bluetooth/bluetooth_device.h"
20 #include "device/bluetooth/bluetooth_device_chromeos.h"
21 #include "device/bluetooth/bluetooth_discovery_session.h"
22 #include "device/bluetooth/bluetooth_pairing_chromeos.h"
23 #include "testing/gtest/include/gtest/gtest.h"
24 #include "third_party/cros_system_api/dbus/service_constants.h"
26 using device::BluetoothAdapter;
27 using device::BluetoothAdapterFactory;
28 using device::BluetoothAudioSink;
29 using device::BluetoothDevice;
30 using device::BluetoothDiscoverySession;
31 using device::BluetoothUUID;
33 namespace chromeos {
35 namespace {
37 class TestObserver : public BluetoothAdapter::Observer {
38 public:
39 TestObserver(scoped_refptr<BluetoothAdapter> adapter)
40 : present_changed_count_(0),
41 powered_changed_count_(0),
42 discoverable_changed_count_(0),
43 discovering_changed_count_(0),
44 last_present_(false),
45 last_powered_(false),
46 last_discovering_(false),
47 device_added_count_(0),
48 device_changed_count_(0),
49 device_removed_count_(0),
50 last_device_(NULL),
51 adapter_(adapter) {
52 adapter_->AddObserver(this);
55 ~TestObserver() override { adapter_->RemoveObserver(this); }
57 void AdapterPresentChanged(BluetoothAdapter* adapter, bool present) override {
58 EXPECT_EQ(adapter_.get(), adapter);
60 ++present_changed_count_;
61 last_present_ = present;
64 void AdapterPoweredChanged(BluetoothAdapter* adapter, bool powered) override {
65 EXPECT_EQ(adapter_.get(), adapter);
67 ++powered_changed_count_;
68 last_powered_ = powered;
71 void AdapterDiscoverableChanged(BluetoothAdapter* adapter,
72 bool discoverable) override {
73 EXPECT_EQ(adapter_.get(), adapter);
75 ++discoverable_changed_count_;
78 void AdapterDiscoveringChanged(BluetoothAdapter* adapter,
79 bool discovering) override {
80 EXPECT_EQ(adapter_.get(), adapter);
82 ++discovering_changed_count_;
83 last_discovering_ = discovering;
86 void DeviceAdded(BluetoothAdapter* adapter,
87 BluetoothDevice* device) override {
88 EXPECT_EQ(adapter_.get(), adapter);
90 ++device_added_count_;
91 last_device_ = device;
92 last_device_address_ = device->GetAddress();
94 QuitMessageLoop();
97 void DeviceChanged(BluetoothAdapter* adapter,
98 BluetoothDevice* device) override {
99 EXPECT_EQ(adapter_.get(), adapter);
101 ++device_changed_count_;
102 last_device_ = device;
103 last_device_address_ = device->GetAddress();
105 QuitMessageLoop();
108 void DeviceRemoved(BluetoothAdapter* adapter,
109 BluetoothDevice* device) override {
110 EXPECT_EQ(adapter_.get(), adapter);
112 ++device_removed_count_;
113 // Can't save device, it may be freed
114 last_device_address_ = device->GetAddress();
116 QuitMessageLoop();
119 int present_changed_count_;
120 int powered_changed_count_;
121 int discoverable_changed_count_;
122 int discovering_changed_count_;
123 bool last_present_;
124 bool last_powered_;
125 bool last_discovering_;
126 int device_added_count_;
127 int device_changed_count_;
128 int device_removed_count_;
129 BluetoothDevice* last_device_;
130 std::string last_device_address_;
132 private:
133 // Some tests use a message loop since background processing is simulated;
134 // break out of those loops.
135 void QuitMessageLoop() {
136 if (base::MessageLoop::current() &&
137 base::MessageLoop::current()->is_running())
138 base::MessageLoop::current()->Quit();
141 scoped_refptr<BluetoothAdapter> adapter_;
144 // Callback for BluetoothDevice::GetConnectionInfo() that simply saves the
145 // connection info to the bound argument.
146 void SaveConnectionInfo(BluetoothDevice::ConnectionInfo* out,
147 const BluetoothDevice::ConnectionInfo& conn_info) {
148 *out = conn_info;
151 class FakeBluetoothProfileServiceProviderDelegate
152 : public chromeos::BluetoothProfileServiceProvider::Delegate {
153 public:
154 FakeBluetoothProfileServiceProviderDelegate() {}
156 // BluetoothProfileServiceProvider::Delegate:
157 void Released() override {}
159 void NewConnection(const dbus::ObjectPath&,
160 scoped_ptr<dbus::FileDescriptor>,
161 const BluetoothProfileServiceProvider::Delegate::Options&,
162 const ConfirmationCallback&) override {}
164 void RequestDisconnection(const dbus::ObjectPath&,
165 const ConfirmationCallback&) override {}
167 void Cancel() override {}
170 } // namespace
172 class TestPairingDelegate : public BluetoothDevice::PairingDelegate {
173 public:
174 TestPairingDelegate()
175 : call_count_(0),
176 request_pincode_count_(0),
177 request_passkey_count_(0),
178 display_pincode_count_(0),
179 display_passkey_count_(0),
180 keys_entered_count_(0),
181 confirm_passkey_count_(0),
182 authorize_pairing_count_(0),
183 last_passkey_(9999999U),
184 last_entered_(999U) {}
185 ~TestPairingDelegate() override {}
187 void RequestPinCode(BluetoothDevice* device) override {
188 ++call_count_;
189 ++request_pincode_count_;
190 QuitMessageLoop();
193 void RequestPasskey(BluetoothDevice* device) override {
194 ++call_count_;
195 ++request_passkey_count_;
196 QuitMessageLoop();
199 void DisplayPinCode(BluetoothDevice* device,
200 const std::string& pincode) override {
201 ++call_count_;
202 ++display_pincode_count_;
203 last_pincode_ = pincode;
204 QuitMessageLoop();
207 void DisplayPasskey(BluetoothDevice* device, uint32 passkey) override {
208 ++call_count_;
209 ++display_passkey_count_;
210 last_passkey_ = passkey;
211 QuitMessageLoop();
214 void KeysEntered(BluetoothDevice* device, uint32 entered) override {
215 ++call_count_;
216 ++keys_entered_count_;
217 last_entered_ = entered;
218 QuitMessageLoop();
221 void ConfirmPasskey(BluetoothDevice* device, uint32 passkey) override {
222 ++call_count_;
223 ++confirm_passkey_count_;
224 last_passkey_ = passkey;
225 QuitMessageLoop();
228 void AuthorizePairing(BluetoothDevice* device) override {
229 ++call_count_;
230 ++authorize_pairing_count_;
231 QuitMessageLoop();
234 int call_count_;
235 int request_pincode_count_;
236 int request_passkey_count_;
237 int display_pincode_count_;
238 int display_passkey_count_;
239 int keys_entered_count_;
240 int confirm_passkey_count_;
241 int authorize_pairing_count_;
242 uint32 last_passkey_;
243 uint32 last_entered_;
244 std::string last_pincode_;
246 private:
247 // Some tests use a message loop since background processing is simulated;
248 // break out of those loops.
249 void QuitMessageLoop() {
250 if (base::MessageLoop::current() &&
251 base::MessageLoop::current()->is_running())
252 base::MessageLoop::current()->Quit();
256 class BluetoothChromeOSTest : public testing::Test {
257 public:
258 void SetUp() override {
259 scoped_ptr<DBusThreadManagerSetter> dbus_setter =
260 chromeos::DBusThreadManager::GetSetterForTesting();
261 // We need to initialize DBusThreadManager early to prevent
262 // Bluetooth*::Create() methods from picking the real instead of fake
263 // implementations.
264 fake_bluetooth_adapter_client_ = new FakeBluetoothAdapterClient;
265 dbus_setter->SetBluetoothAdapterClient(
266 scoped_ptr<BluetoothAdapterClient>(fake_bluetooth_adapter_client_));
267 fake_bluetooth_device_client_ = new FakeBluetoothDeviceClient;
268 dbus_setter->SetBluetoothDeviceClient(
269 scoped_ptr<BluetoothDeviceClient>(fake_bluetooth_device_client_));
270 dbus_setter->SetBluetoothInputClient(
271 scoped_ptr<BluetoothInputClient>(new FakeBluetoothInputClient));
272 dbus_setter->SetBluetoothAgentManagerClient(
273 scoped_ptr<BluetoothAgentManagerClient>(
274 new FakeBluetoothAgentManagerClient));
275 dbus_setter->SetBluetoothGattServiceClient(
276 scoped_ptr<BluetoothGattServiceClient>(
277 new FakeBluetoothGattServiceClient));
279 fake_bluetooth_adapter_client_->SetSimulationIntervalMs(10);
281 callback_count_ = 0;
282 error_callback_count_ = 0;
283 last_connect_error_ = BluetoothDevice::ERROR_UNKNOWN;
284 last_client_error_ = "";
287 void TearDown() override {
288 for (ScopedVector<BluetoothDiscoverySession>::iterator iter =
289 discovery_sessions_.begin();
290 iter != discovery_sessions_.end();
291 ++iter) {
292 BluetoothDiscoverySession* session = *iter;
293 if (!session->IsActive())
294 continue;
295 callback_count_ = 0;
296 session->Stop(GetCallback(), GetErrorCallback());
297 message_loop_.Run();
298 ASSERT_EQ(1, callback_count_);
300 discovery_sessions_.clear();
301 adapter_ = NULL;
302 DBusThreadManager::Shutdown();
305 // Generic callbacks
306 void Callback() {
307 ++callback_count_;
308 QuitMessageLoop();
311 base::Closure GetCallback() {
312 return base::Bind(&BluetoothChromeOSTest::Callback, base::Unretained(this));
315 void DiscoverySessionCallback(
316 scoped_ptr<BluetoothDiscoverySession> discovery_session) {
317 ++callback_count_;
318 discovery_sessions_.push_back(discovery_session.release());
319 QuitMessageLoop();
322 void AudioSinkAcquiredCallback(scoped_refptr<BluetoothAudioSink>) {
323 ++callback_count_;
324 QuitMessageLoop();
327 void ProfileRegisteredCallback(BluetoothAdapterProfileChromeOS*) {
328 ++callback_count_;
329 QuitMessageLoop();
332 void ErrorCallback() {
333 ++error_callback_count_;
334 QuitMessageLoop();
337 base::Closure GetErrorCallback() {
338 return base::Bind(&BluetoothChromeOSTest::ErrorCallback,
339 base::Unretained(this));
342 void DBusErrorCallback(const std::string& error_name,
343 const std::string& error_message) {
344 ++error_callback_count_;
345 last_client_error_ = error_name;
346 QuitMessageLoop();
349 void ConnectErrorCallback(BluetoothDevice::ConnectErrorCode error) {
350 ++error_callback_count_;
351 last_connect_error_ = error;
354 void AudioSinkErrorCallback(BluetoothAudioSink::ErrorCode) {
355 ++error_callback_count_;
356 QuitMessageLoop();
359 void ErrorCompletionCallback(const std::string& error_message) {
360 ++error_callback_count_;
361 QuitMessageLoop();
364 // Call to fill the adapter_ member with a BluetoothAdapter instance.
365 void GetAdapter() {
366 adapter_ = new BluetoothAdapterChromeOS();
367 ASSERT_TRUE(adapter_.get() != NULL);
368 ASSERT_TRUE(adapter_->IsInitialized());
371 // Run a discovery phase until the named device is detected, or if the named
372 // device is not created, the discovery process ends without finding it.
374 // The correct behavior of discovery is tested by the "Discovery" test case
375 // without using this function.
376 void DiscoverDevice(const std::string& address) {
377 ASSERT_TRUE(adapter_.get() != NULL);
378 ASSERT_TRUE(base::MessageLoop::current() != NULL);
379 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
381 TestObserver observer(adapter_);
383 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
384 adapter_->StartDiscoverySession(
385 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
386 base::Unretained(this)),
387 GetErrorCallback());
388 base::MessageLoop::current()->Run();
389 ASSERT_EQ(2, callback_count_);
390 ASSERT_EQ(0, error_callback_count_);
391 ASSERT_EQ((size_t)1, discovery_sessions_.size());
392 ASSERT_TRUE(discovery_sessions_[0]->IsActive());
393 callback_count_ = 0;
395 ASSERT_TRUE(adapter_->IsPowered());
396 ASSERT_TRUE(adapter_->IsDiscovering());
398 while (!observer.device_removed_count_ &&
399 observer.last_device_address_ != address)
400 base::MessageLoop::current()->Run();
402 discovery_sessions_[0]->Stop(GetCallback(), GetErrorCallback());
403 base::MessageLoop::current()->Run();
404 ASSERT_EQ(1, callback_count_);
405 ASSERT_EQ(0, error_callback_count_);
406 callback_count_ = 0;
408 ASSERT_FALSE(adapter_->IsDiscovering());
411 // Run a discovery phase so we have devices that can be paired with.
412 void DiscoverDevices() {
413 // Pass an invalid address for the device so that the discovery process
414 // completes with all devices.
415 DiscoverDevice("does not exist");
418 protected:
419 base::MessageLoop message_loop_;
420 FakeBluetoothAdapterClient* fake_bluetooth_adapter_client_;
421 FakeBluetoothDeviceClient* fake_bluetooth_device_client_;
422 scoped_refptr<BluetoothAdapter> adapter_;
424 int callback_count_;
425 int error_callback_count_;
426 enum BluetoothDevice::ConnectErrorCode last_connect_error_;
427 std::string last_client_error_;
428 ScopedVector<BluetoothDiscoverySession> discovery_sessions_;
430 private:
431 // Some tests use a message loop since background processing is simulated;
432 // break out of those loops.
433 void QuitMessageLoop() {
434 if (base::MessageLoop::current() &&
435 base::MessageLoop::current()->is_running())
436 base::MessageLoop::current()->Quit();
440 TEST_F(BluetoothChromeOSTest, AlreadyPresent) {
441 GetAdapter();
443 // This verifies that the class gets the list of adapters when created;
444 // and initializes with an existing adapter if there is one.
445 EXPECT_TRUE(adapter_->IsPresent());
446 EXPECT_FALSE(adapter_->IsPowered());
447 EXPECT_EQ(FakeBluetoothAdapterClient::kAdapterAddress,
448 adapter_->GetAddress());
449 EXPECT_FALSE(adapter_->IsDiscovering());
451 // There should be a device
452 BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
453 EXPECT_EQ(2U, devices.size());
454 EXPECT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
455 devices[0]->GetAddress());
456 EXPECT_EQ(FakeBluetoothDeviceClient::kPairedUnconnectableDeviceAddress,
457 devices[1]->GetAddress());
460 TEST_F(BluetoothChromeOSTest, BecomePresent) {
461 fake_bluetooth_adapter_client_->SetVisible(false);
462 GetAdapter();
463 ASSERT_FALSE(adapter_->IsPresent());
465 // Install an observer; expect the AdapterPresentChanged to be called
466 // with true, and IsPresent() to return true.
467 TestObserver observer(adapter_);
469 fake_bluetooth_adapter_client_->SetVisible(true);
471 EXPECT_EQ(1, observer.present_changed_count_);
472 EXPECT_TRUE(observer.last_present_);
474 EXPECT_TRUE(adapter_->IsPresent());
476 // We should have had a device announced.
477 EXPECT_EQ(2, observer.device_added_count_);
478 EXPECT_EQ(FakeBluetoothDeviceClient::kPairedUnconnectableDeviceAddress,
479 observer.last_device_address_);
481 // Other callbacks shouldn't be called if the values are false.
482 EXPECT_EQ(0, observer.powered_changed_count_);
483 EXPECT_EQ(0, observer.discovering_changed_count_);
484 EXPECT_FALSE(adapter_->IsPowered());
485 EXPECT_FALSE(adapter_->IsDiscovering());
488 TEST_F(BluetoothChromeOSTest, BecomeNotPresent) {
489 GetAdapter();
490 ASSERT_TRUE(adapter_->IsPresent());
492 // Install an observer; expect the AdapterPresentChanged to be called
493 // with false, and IsPresent() to return false.
494 TestObserver observer(adapter_);
496 fake_bluetooth_adapter_client_->SetVisible(false);
498 EXPECT_EQ(1, observer.present_changed_count_);
499 EXPECT_FALSE(observer.last_present_);
501 EXPECT_FALSE(adapter_->IsPresent());
503 // We should have had a device removed.
504 EXPECT_EQ(2, observer.device_removed_count_);
505 EXPECT_EQ(FakeBluetoothDeviceClient::kPairedUnconnectableDeviceAddress,
506 observer.last_device_address_);
508 // Other callbacks shouldn't be called since the values are false.
509 EXPECT_EQ(0, observer.powered_changed_count_);
510 EXPECT_EQ(0, observer.discovering_changed_count_);
511 EXPECT_FALSE(adapter_->IsPowered());
512 EXPECT_FALSE(adapter_->IsDiscovering());
515 TEST_F(BluetoothChromeOSTest, SecondAdapter) {
516 GetAdapter();
517 ASSERT_TRUE(adapter_->IsPresent());
519 // Install an observer, then add a second adapter. Nothing should change,
520 // we ignore the second adapter.
521 TestObserver observer(adapter_);
523 fake_bluetooth_adapter_client_->SetSecondVisible(true);
525 EXPECT_EQ(0, observer.present_changed_count_);
527 EXPECT_TRUE(adapter_->IsPresent());
528 EXPECT_EQ(FakeBluetoothAdapterClient::kAdapterAddress,
529 adapter_->GetAddress());
531 // Try removing the first adapter, we should now act as if the adapter
532 // is no longer present rather than fall back to the second.
533 fake_bluetooth_adapter_client_->SetVisible(false);
535 EXPECT_EQ(1, observer.present_changed_count_);
536 EXPECT_FALSE(observer.last_present_);
538 EXPECT_FALSE(adapter_->IsPresent());
540 // We should have had a device removed.
541 EXPECT_EQ(2, observer.device_removed_count_);
542 EXPECT_EQ(FakeBluetoothDeviceClient::kPairedUnconnectableDeviceAddress,
543 observer.last_device_address_);
545 // Other callbacks shouldn't be called since the values are false.
546 EXPECT_EQ(0, observer.powered_changed_count_);
547 EXPECT_EQ(0, observer.discovering_changed_count_);
548 EXPECT_FALSE(adapter_->IsPowered());
549 EXPECT_FALSE(adapter_->IsDiscovering());
551 observer.device_removed_count_ = 0;
553 // Removing the second adapter shouldn't set anything either.
554 fake_bluetooth_adapter_client_->SetSecondVisible(false);
556 EXPECT_EQ(0, observer.device_removed_count_);
557 EXPECT_EQ(0, observer.powered_changed_count_);
558 EXPECT_EQ(0, observer.discovering_changed_count_);
561 TEST_F(BluetoothChromeOSTest, BecomePowered) {
562 GetAdapter();
563 ASSERT_FALSE(adapter_->IsPowered());
565 // Install an observer; expect the AdapterPoweredChanged to be called
566 // with true, and IsPowered() to return true.
567 TestObserver observer(adapter_);
569 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
570 EXPECT_EQ(1, callback_count_);
571 EXPECT_EQ(0, error_callback_count_);
573 EXPECT_EQ(1, observer.powered_changed_count_);
574 EXPECT_TRUE(observer.last_powered_);
576 EXPECT_TRUE(adapter_->IsPowered());
579 TEST_F(BluetoothChromeOSTest, BecomeNotPowered) {
580 GetAdapter();
581 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
582 EXPECT_EQ(1, callback_count_);
583 EXPECT_EQ(0, error_callback_count_);
584 callback_count_ = 0;
586 ASSERT_TRUE(adapter_->IsPowered());
588 // Install an observer; expect the AdapterPoweredChanged to be called
589 // with false, and IsPowered() to return false.
590 TestObserver observer(adapter_);
592 adapter_->SetPowered(false, GetCallback(), GetErrorCallback());
593 EXPECT_EQ(1, callback_count_);
594 EXPECT_EQ(0, error_callback_count_);
596 EXPECT_EQ(1, observer.powered_changed_count_);
597 EXPECT_FALSE(observer.last_powered_);
599 EXPECT_FALSE(adapter_->IsPowered());
602 TEST_F(BluetoothChromeOSTest, SetPoweredWhenNotPresent) {
603 GetAdapter();
604 ASSERT_TRUE(adapter_->IsPresent());
606 // Install an observer; expect the AdapterPresentChanged to be called
607 // with false, and IsPresent() to return false.
608 TestObserver observer(adapter_);
610 fake_bluetooth_adapter_client_->SetVisible(false);
612 EXPECT_EQ(1, observer.present_changed_count_);
613 EXPECT_FALSE(observer.last_present_);
615 EXPECT_FALSE(adapter_->IsPresent());
616 EXPECT_FALSE(adapter_->IsPowered());
618 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
619 EXPECT_EQ(0, callback_count_);
620 EXPECT_EQ(1, error_callback_count_);
622 EXPECT_EQ(0, observer.powered_changed_count_);
623 EXPECT_FALSE(observer.last_powered_);
625 EXPECT_FALSE(adapter_->IsPowered());
628 TEST_F(BluetoothChromeOSTest, ChangeAdapterName) {
629 GetAdapter();
631 static const std::string new_name(".__.");
633 adapter_->SetName(new_name, GetCallback(), GetErrorCallback());
634 EXPECT_EQ(1, callback_count_);
635 EXPECT_EQ(0, error_callback_count_);
637 EXPECT_EQ(new_name, adapter_->GetName());
640 TEST_F(BluetoothChromeOSTest, ChangeAdapterNameWhenNotPresent) {
641 GetAdapter();
642 ASSERT_TRUE(adapter_->IsPresent());
644 // Install an observer; expect the AdapterPresentChanged to be called
645 // with false, and IsPresent() to return false.
646 TestObserver observer(adapter_);
648 fake_bluetooth_adapter_client_->SetVisible(false);
650 EXPECT_EQ(1, observer.present_changed_count_);
651 EXPECT_FALSE(observer.last_present_);
653 EXPECT_FALSE(adapter_->IsPresent());
654 EXPECT_FALSE(adapter_->IsPowered());
656 adapter_->SetName("^o^", GetCallback(), GetErrorCallback());
657 EXPECT_EQ(0, callback_count_);
658 EXPECT_EQ(1, error_callback_count_);
660 EXPECT_EQ("", adapter_->GetName());
663 TEST_F(BluetoothChromeOSTest, BecomeDiscoverable) {
664 GetAdapter();
665 ASSERT_FALSE(adapter_->IsDiscoverable());
667 // Install an observer; expect the AdapterDiscoverableChanged to be called
668 // with true, and IsDiscoverable() to return true.
669 TestObserver observer(adapter_);
671 adapter_->SetDiscoverable(true, GetCallback(), GetErrorCallback());
672 EXPECT_EQ(1, callback_count_);
673 EXPECT_EQ(0, error_callback_count_);
675 EXPECT_EQ(1, observer.discoverable_changed_count_);
677 EXPECT_TRUE(adapter_->IsDiscoverable());
680 TEST_F(BluetoothChromeOSTest, BecomeNotDiscoverable) {
681 GetAdapter();
682 adapter_->SetDiscoverable(true, GetCallback(), GetErrorCallback());
683 EXPECT_EQ(1, callback_count_);
684 EXPECT_EQ(0, error_callback_count_);
685 callback_count_ = 0;
687 ASSERT_TRUE(adapter_->IsDiscoverable());
689 // Install an observer; expect the AdapterDiscoverableChanged to be called
690 // with false, and IsDiscoverable() to return false.
691 TestObserver observer(adapter_);
693 adapter_->SetDiscoverable(false, GetCallback(), GetErrorCallback());
694 EXPECT_EQ(1, callback_count_);
695 EXPECT_EQ(0, error_callback_count_);
697 EXPECT_EQ(1, observer.discoverable_changed_count_);
699 EXPECT_FALSE(adapter_->IsDiscoverable());
702 TEST_F(BluetoothChromeOSTest, SetDiscoverableWhenNotPresent) {
703 GetAdapter();
704 ASSERT_TRUE(adapter_->IsPresent());
705 ASSERT_FALSE(adapter_->IsDiscoverable());
707 // Install an observer; expect the AdapterDiscoverableChanged to be called
708 // with true, and IsDiscoverable() to return true.
709 TestObserver observer(adapter_);
711 fake_bluetooth_adapter_client_->SetVisible(false);
713 EXPECT_EQ(1, observer.present_changed_count_);
714 EXPECT_FALSE(observer.last_present_);
716 EXPECT_FALSE(adapter_->IsPresent());
717 EXPECT_FALSE(adapter_->IsDiscoverable());
719 adapter_->SetDiscoverable(true, GetCallback(), GetErrorCallback());
720 EXPECT_EQ(0, callback_count_);
721 EXPECT_EQ(1, error_callback_count_);
723 EXPECT_EQ(0, observer.discoverable_changed_count_);
725 EXPECT_FALSE(adapter_->IsDiscoverable());
728 TEST_F(BluetoothChromeOSTest, StopDiscovery) {
729 GetAdapter();
731 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
732 adapter_->StartDiscoverySession(
733 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
734 base::Unretained(this)),
735 GetErrorCallback());
736 message_loop_.Run();
737 EXPECT_EQ(2, callback_count_);
738 EXPECT_EQ(0, error_callback_count_);
739 callback_count_ = 0;
741 ASSERT_TRUE(adapter_->IsPowered());
742 ASSERT_TRUE(adapter_->IsDiscovering());
743 ASSERT_EQ((size_t)1, discovery_sessions_.size());
744 ASSERT_TRUE(discovery_sessions_[0]->IsActive());
746 // Install an observer; aside from the callback, expect the
747 // AdapterDiscoveringChanged method to be called and no longer to be
748 // discovering,
749 TestObserver observer(adapter_);
751 discovery_sessions_[0]->Stop(GetCallback(), GetErrorCallback());
752 message_loop_.Run();
753 EXPECT_EQ(1, callback_count_);
754 EXPECT_EQ(0, error_callback_count_);
756 EXPECT_EQ(1, observer.discovering_changed_count_);
757 EXPECT_FALSE(observer.last_discovering_);
759 EXPECT_FALSE(adapter_->IsDiscovering());
762 TEST_F(BluetoothChromeOSTest, Discovery) {
763 // Test a simulated discovery session.
764 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
765 GetAdapter();
767 TestObserver observer(adapter_);
769 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
770 adapter_->StartDiscoverySession(
771 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
772 base::Unretained(this)),
773 GetErrorCallback());
774 message_loop_.Run();
775 EXPECT_EQ(2, callback_count_);
776 EXPECT_EQ(0, error_callback_count_);
777 callback_count_ = 0;
779 ASSERT_TRUE(adapter_->IsPowered());
780 ASSERT_TRUE(adapter_->IsDiscovering());
781 ASSERT_EQ((size_t)1, discovery_sessions_.size());
782 ASSERT_TRUE(discovery_sessions_[0]->IsActive());
784 // First two devices to appear.
785 message_loop_.Run();
787 EXPECT_EQ(2, observer.device_added_count_);
788 EXPECT_EQ(FakeBluetoothDeviceClient::kLowEnergyAddress,
789 observer.last_device_address_);
791 // Next we should get another two devices...
792 message_loop_.Run();
793 EXPECT_EQ(4, observer.device_added_count_);
795 // Okay, let's run forward until a device is actually removed...
796 while (!observer.device_removed_count_)
797 message_loop_.Run();
799 EXPECT_EQ(1, observer.device_removed_count_);
800 EXPECT_EQ(FakeBluetoothDeviceClient::kVanishingDeviceAddress,
801 observer.last_device_address_);
804 TEST_F(BluetoothChromeOSTest, PoweredAndDiscovering) {
805 GetAdapter();
806 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
807 adapter_->StartDiscoverySession(
808 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
809 base::Unretained(this)),
810 GetErrorCallback());
811 message_loop_.Run();
812 EXPECT_EQ(2, callback_count_);
813 EXPECT_EQ(0, error_callback_count_);
814 callback_count_ = 0;
815 ASSERT_EQ((size_t)1, discovery_sessions_.size());
816 ASSERT_TRUE(discovery_sessions_[0]->IsActive());
818 // Stop the timers that the simulation uses
819 fake_bluetooth_device_client_->EndDiscoverySimulation(
820 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath));
822 ASSERT_TRUE(adapter_->IsPowered());
823 ASSERT_TRUE(adapter_->IsDiscovering());
825 fake_bluetooth_adapter_client_->SetVisible(false);
826 ASSERT_FALSE(adapter_->IsPresent());
827 ASSERT_FALSE(discovery_sessions_[0]->IsActive());
829 // Install an observer; expect the AdapterPresentChanged,
830 // AdapterPoweredChanged and AdapterDiscoveringChanged methods to be called
831 // with true, and IsPresent(), IsPowered() and IsDiscovering() to all
832 // return true.
833 TestObserver observer(adapter_);
835 fake_bluetooth_adapter_client_->SetVisible(true);
837 EXPECT_EQ(1, observer.present_changed_count_);
838 EXPECT_TRUE(observer.last_present_);
839 EXPECT_TRUE(adapter_->IsPresent());
841 EXPECT_EQ(1, observer.powered_changed_count_);
842 EXPECT_TRUE(observer.last_powered_);
843 EXPECT_TRUE(adapter_->IsPowered());
845 EXPECT_EQ(1, observer.discovering_changed_count_);
846 EXPECT_TRUE(observer.last_discovering_);
847 EXPECT_TRUE(adapter_->IsDiscovering());
849 observer.present_changed_count_ = 0;
850 observer.powered_changed_count_ = 0;
851 observer.discovering_changed_count_ = 0;
853 // Now mark the adapter not present again. Expect the methods to be called
854 // again, to reset the properties back to false
855 fake_bluetooth_adapter_client_->SetVisible(false);
857 EXPECT_EQ(1, observer.present_changed_count_);
858 EXPECT_FALSE(observer.last_present_);
859 EXPECT_FALSE(adapter_->IsPresent());
861 EXPECT_EQ(1, observer.powered_changed_count_);
862 EXPECT_FALSE(observer.last_powered_);
863 EXPECT_FALSE(adapter_->IsPowered());
865 EXPECT_EQ(1, observer.discovering_changed_count_);
866 EXPECT_FALSE(observer.last_discovering_);
867 EXPECT_FALSE(adapter_->IsDiscovering());
870 // This unit test asserts that the basic reference counting logic works
871 // correctly for discovery requests done via the BluetoothAdapter.
872 TEST_F(BluetoothChromeOSTest, MultipleDiscoverySessions) {
873 GetAdapter();
874 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
875 EXPECT_EQ(1, callback_count_);
876 EXPECT_EQ(0, error_callback_count_);
877 EXPECT_TRUE(adapter_->IsPowered());
878 callback_count_ = 0;
880 TestObserver observer(adapter_);
882 EXPECT_EQ(0, observer.discovering_changed_count_);
883 EXPECT_FALSE(observer.last_discovering_);
884 EXPECT_FALSE(adapter_->IsDiscovering());
886 // Request device discovery 3 times.
887 for (int i = 0; i < 3; i++) {
888 adapter_->StartDiscoverySession(
889 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
890 base::Unretained(this)),
891 GetErrorCallback());
893 // Run only once, as there should have been one D-Bus call.
894 message_loop_.Run();
896 // The observer should have received the discovering changed event exactly
897 // once, the success callback should have been called 3 times and the adapter
898 // should be discovering.
899 EXPECT_EQ(1, observer.discovering_changed_count_);
900 EXPECT_EQ(3, callback_count_);
901 EXPECT_EQ(0, error_callback_count_);
902 EXPECT_TRUE(observer.last_discovering_);
903 EXPECT_TRUE(adapter_->IsDiscovering());
904 ASSERT_EQ((size_t)3, discovery_sessions_.size());
906 // Request to stop discovery twice.
907 for (int i = 0; i < 2; i++) {
908 discovery_sessions_[i]->Stop(GetCallback(), GetErrorCallback());
911 // The observer should have received no additional discovering changed events,
912 // the success callback should have been called 2 times and the adapter should
913 // still be discovering.
914 EXPECT_EQ(1, observer.discovering_changed_count_);
915 EXPECT_EQ(5, callback_count_);
916 EXPECT_EQ(0, error_callback_count_);
917 EXPECT_TRUE(observer.last_discovering_);
918 EXPECT_TRUE(adapter_->IsDiscovering());
919 EXPECT_TRUE(adapter_->IsDiscovering());
920 EXPECT_FALSE(discovery_sessions_[0]->IsActive());
921 EXPECT_FALSE(discovery_sessions_[1]->IsActive());
922 EXPECT_TRUE(discovery_sessions_[2]->IsActive());
924 // Request device discovery 3 times.
925 for (int i = 0; i < 3; i++) {
926 adapter_->StartDiscoverySession(
927 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
928 base::Unretained(this)),
929 GetErrorCallback());
932 // The observer should have received no additional discovering changed events,
933 // the success callback should have been called 3 times and the adapter should
934 // still be discovering.
935 EXPECT_EQ(1, observer.discovering_changed_count_);
936 EXPECT_EQ(8, callback_count_);
937 EXPECT_EQ(0, error_callback_count_);
938 EXPECT_TRUE(observer.last_discovering_);
939 EXPECT_TRUE(adapter_->IsDiscovering());
940 ASSERT_EQ((size_t)6, discovery_sessions_.size());
942 // Request to stop discovery 4 times.
943 for (int i = 2; i < 6; i++) {
944 discovery_sessions_[i]->Stop(GetCallback(), GetErrorCallback());
946 // Run only once, as there should have been one D-Bus call.
947 message_loop_.Run();
949 // The observer should have received the discovering changed event exactly
950 // once, the success callback should have been called 4 times and the adapter
951 // should no longer be discovering.
952 EXPECT_EQ(2, observer.discovering_changed_count_);
953 EXPECT_EQ(12, callback_count_);
954 EXPECT_EQ(0, error_callback_count_);
955 EXPECT_FALSE(observer.last_discovering_);
956 EXPECT_FALSE(adapter_->IsDiscovering());
958 // All discovery sessions should be inactive.
959 for (int i = 0; i < 6; i++)
960 EXPECT_FALSE(discovery_sessions_[i]->IsActive());
962 // Request to stop discovery on of the inactive sessions.
963 discovery_sessions_[0]->Stop(GetCallback(), GetErrorCallback());
965 // The call should have failed.
966 EXPECT_EQ(2, observer.discovering_changed_count_);
967 EXPECT_EQ(12, callback_count_);
968 EXPECT_EQ(1, error_callback_count_);
969 EXPECT_FALSE(observer.last_discovering_);
970 EXPECT_FALSE(adapter_->IsDiscovering());
973 // This unit test asserts that the reference counting logic works correctly in
974 // the cases when the adapter gets reset and D-Bus calls are made outside of
975 // the BluetoothAdapter.
976 TEST_F(BluetoothChromeOSTest,
977 UnexpectedChangesDuringMultipleDiscoverySessions) {
978 GetAdapter();
979 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
980 EXPECT_EQ(1, callback_count_);
981 EXPECT_EQ(0, error_callback_count_);
982 EXPECT_TRUE(adapter_->IsPowered());
983 callback_count_ = 0;
985 TestObserver observer(adapter_);
987 EXPECT_EQ(0, observer.discovering_changed_count_);
988 EXPECT_FALSE(observer.last_discovering_);
989 EXPECT_FALSE(adapter_->IsDiscovering());
991 // Request device discovery 3 times.
992 for (int i = 0; i < 3; i++) {
993 adapter_->StartDiscoverySession(
994 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
995 base::Unretained(this)),
996 GetErrorCallback());
998 // Run only once, as there should have been one D-Bus call.
999 message_loop_.Run();
1001 // The observer should have received the discovering changed event exactly
1002 // once, the success callback should have been called 3 times and the adapter
1003 // should be discovering.
1004 EXPECT_EQ(1, observer.discovering_changed_count_);
1005 EXPECT_EQ(3, callback_count_);
1006 EXPECT_EQ(0, error_callback_count_);
1007 EXPECT_TRUE(observer.last_discovering_);
1008 EXPECT_TRUE(adapter_->IsDiscovering());
1009 ASSERT_EQ((size_t)3, discovery_sessions_.size());
1011 for (int i = 0; i < 3; i++)
1012 EXPECT_TRUE(discovery_sessions_[i]->IsActive());
1014 // Stop the timers that the simulation uses
1015 fake_bluetooth_device_client_->EndDiscoverySimulation(
1016 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath));
1018 ASSERT_TRUE(adapter_->IsPowered());
1019 ASSERT_TRUE(adapter_->IsDiscovering());
1021 // Stop device discovery behind the adapter. The adapter and the observer
1022 // should be notified of the change and the reference count should be reset.
1023 // Even though FakeBluetoothAdapterClient does its own reference counting and
1024 // we called 3 BluetoothAdapter::StartDiscoverySession 3 times, the
1025 // FakeBluetoothAdapterClient's count should be only 1 and a single call to
1026 // FakeBluetoothAdapterClient::StopDiscovery should work.
1027 fake_bluetooth_adapter_client_->StopDiscovery(
1028 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath), GetCallback(),
1029 base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
1030 base::Unretained(this)));
1031 message_loop_.Run();
1032 EXPECT_EQ(2, observer.discovering_changed_count_);
1033 EXPECT_EQ(4, callback_count_);
1034 EXPECT_EQ(0, error_callback_count_);
1035 EXPECT_FALSE(observer.last_discovering_);
1036 EXPECT_FALSE(adapter_->IsDiscovering());
1038 // All discovery session instances should have been updated.
1039 for (int i = 0; i < 3; i++)
1040 EXPECT_FALSE(discovery_sessions_[i]->IsActive());
1041 discovery_sessions_.clear();
1043 // It should be possible to successfully start discovery.
1044 for (int i = 0; i < 2; i++) {
1045 adapter_->StartDiscoverySession(
1046 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1047 base::Unretained(this)),
1048 GetErrorCallback());
1050 // Run only once, as there should have been one D-Bus call.
1051 message_loop_.Run();
1052 EXPECT_EQ(3, observer.discovering_changed_count_);
1053 EXPECT_EQ(6, callback_count_);
1054 EXPECT_EQ(0, error_callback_count_);
1055 EXPECT_TRUE(observer.last_discovering_);
1056 EXPECT_TRUE(adapter_->IsDiscovering());
1057 ASSERT_EQ((size_t)2, discovery_sessions_.size());
1059 for (int i = 0; i < 2; i++)
1060 EXPECT_TRUE(discovery_sessions_[i]->IsActive());
1062 fake_bluetooth_device_client_->EndDiscoverySimulation(
1063 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath));
1065 // Make the adapter disappear and appear. This will make it come back as
1066 // discovering. When this happens, the reference count should become and
1067 // remain 0 as no new request was made through the BluetoothAdapter.
1068 fake_bluetooth_adapter_client_->SetVisible(false);
1069 ASSERT_FALSE(adapter_->IsPresent());
1070 EXPECT_EQ(4, observer.discovering_changed_count_);
1071 EXPECT_EQ(6, callback_count_);
1072 EXPECT_EQ(0, error_callback_count_);
1073 EXPECT_FALSE(observer.last_discovering_);
1074 EXPECT_FALSE(adapter_->IsDiscovering());
1076 for (int i = 0; i < 2; i++)
1077 EXPECT_FALSE(discovery_sessions_[i]->IsActive());
1078 discovery_sessions_.clear();
1080 fake_bluetooth_adapter_client_->SetVisible(true);
1081 ASSERT_TRUE(adapter_->IsPresent());
1082 EXPECT_EQ(5, observer.discovering_changed_count_);
1083 EXPECT_EQ(6, callback_count_);
1084 EXPECT_EQ(0, error_callback_count_);
1085 EXPECT_TRUE(observer.last_discovering_);
1086 EXPECT_TRUE(adapter_->IsDiscovering());
1088 // Start and stop discovery. At this point, FakeBluetoothAdapterClient has
1089 // a reference count that is equal to 1. Pretend that this was done by an
1090 // application other than us. Starting and stopping discovery will succeed
1091 // but it won't cause the discovery state to change.
1092 adapter_->StartDiscoverySession(
1093 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1094 base::Unretained(this)),
1095 GetErrorCallback());
1096 message_loop_.Run(); // Run the loop, as there should have been a D-Bus call.
1097 EXPECT_EQ(5, observer.discovering_changed_count_);
1098 EXPECT_EQ(7, callback_count_);
1099 EXPECT_EQ(0, error_callback_count_);
1100 EXPECT_TRUE(observer.last_discovering_);
1101 EXPECT_TRUE(adapter_->IsDiscovering());
1102 ASSERT_EQ((size_t)1, discovery_sessions_.size());
1103 EXPECT_TRUE(discovery_sessions_[0]->IsActive());
1105 discovery_sessions_[0]->Stop(GetCallback(), GetErrorCallback());
1106 message_loop_.Run(); // Run the loop, as there should have been a D-Bus call.
1107 EXPECT_EQ(5, observer.discovering_changed_count_);
1108 EXPECT_EQ(8, callback_count_);
1109 EXPECT_EQ(0, error_callback_count_);
1110 EXPECT_TRUE(observer.last_discovering_);
1111 EXPECT_TRUE(adapter_->IsDiscovering());
1112 EXPECT_FALSE(discovery_sessions_[0]->IsActive());
1113 discovery_sessions_.clear();
1115 // Start discovery again.
1116 adapter_->StartDiscoverySession(
1117 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1118 base::Unretained(this)),
1119 GetErrorCallback());
1120 message_loop_.Run(); // Run the loop, as there should have been a D-Bus call.
1121 EXPECT_EQ(5, observer.discovering_changed_count_);
1122 EXPECT_EQ(9, callback_count_);
1123 EXPECT_EQ(0, error_callback_count_);
1124 EXPECT_TRUE(observer.last_discovering_);
1125 EXPECT_TRUE(adapter_->IsDiscovering());
1126 ASSERT_EQ((size_t)1, discovery_sessions_.size());
1127 EXPECT_TRUE(discovery_sessions_[0]->IsActive());
1129 // Stop discovery via D-Bus. The fake client's reference count will drop but
1130 // the discovery state won't change since our BluetoothAdapter also just
1131 // requested it via D-Bus.
1132 fake_bluetooth_adapter_client_->StopDiscovery(
1133 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath), GetCallback(),
1134 base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
1135 base::Unretained(this)));
1136 message_loop_.Run();
1137 EXPECT_EQ(5, observer.discovering_changed_count_);
1138 EXPECT_EQ(10, callback_count_);
1139 EXPECT_EQ(0, error_callback_count_);
1140 EXPECT_TRUE(observer.last_discovering_);
1141 EXPECT_TRUE(adapter_->IsDiscovering());
1143 // Now end the discovery session. This should change the adapter's discovery
1144 // state.
1145 discovery_sessions_[0]->Stop(GetCallback(), GetErrorCallback());
1146 message_loop_.Run();
1147 EXPECT_EQ(6, observer.discovering_changed_count_);
1148 EXPECT_EQ(11, callback_count_);
1149 EXPECT_EQ(0, error_callback_count_);
1150 EXPECT_FALSE(observer.last_discovering_);
1151 EXPECT_FALSE(adapter_->IsDiscovering());
1152 EXPECT_FALSE(discovery_sessions_[0]->IsActive());
1155 TEST_F(BluetoothChromeOSTest, InvalidatedDiscoverySessions) {
1156 GetAdapter();
1157 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
1158 EXPECT_EQ(1, callback_count_);
1159 EXPECT_EQ(0, error_callback_count_);
1160 EXPECT_TRUE(adapter_->IsPowered());
1161 callback_count_ = 0;
1163 TestObserver observer(adapter_);
1165 EXPECT_EQ(0, observer.discovering_changed_count_);
1166 EXPECT_FALSE(observer.last_discovering_);
1167 EXPECT_FALSE(adapter_->IsDiscovering());
1169 // Request device discovery 3 times.
1170 for (int i = 0; i < 3; i++) {
1171 adapter_->StartDiscoverySession(
1172 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1173 base::Unretained(this)),
1174 GetErrorCallback());
1176 // Run only once, as there should have been one D-Bus call.
1177 message_loop_.Run();
1179 // The observer should have received the discovering changed event exactly
1180 // once, the success callback should have been called 3 times and the adapter
1181 // should be discovering.
1182 EXPECT_EQ(1, observer.discovering_changed_count_);
1183 EXPECT_EQ(3, callback_count_);
1184 EXPECT_EQ(0, error_callback_count_);
1185 EXPECT_TRUE(observer.last_discovering_);
1186 EXPECT_TRUE(adapter_->IsDiscovering());
1187 ASSERT_EQ((size_t)3, discovery_sessions_.size());
1189 for (int i = 0; i < 3; i++)
1190 EXPECT_TRUE(discovery_sessions_[i]->IsActive());
1192 // Stop the timers that the simulation uses
1193 fake_bluetooth_device_client_->EndDiscoverySimulation(
1194 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath));
1196 ASSERT_TRUE(adapter_->IsPowered());
1197 ASSERT_TRUE(adapter_->IsDiscovering());
1199 // Delete all but one discovery session.
1200 discovery_sessions_.pop_back();
1201 discovery_sessions_.pop_back();
1202 ASSERT_EQ((size_t)1, discovery_sessions_.size());
1203 EXPECT_TRUE(discovery_sessions_[0]->IsActive());
1204 EXPECT_TRUE(adapter_->IsDiscovering());
1206 // Stop device discovery behind the adapter. The one active discovery session
1207 // should become inactive, but more importantly, we shouldn't run into any
1208 // memory errors as the sessions that we explicitly deleted should get
1209 // cleaned up.
1210 fake_bluetooth_adapter_client_->StopDiscovery(
1211 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath), GetCallback(),
1212 base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
1213 base::Unretained(this)));
1214 message_loop_.Run();
1215 EXPECT_EQ(2, observer.discovering_changed_count_);
1216 EXPECT_EQ(4, callback_count_);
1217 EXPECT_EQ(0, error_callback_count_);
1218 EXPECT_FALSE(observer.last_discovering_);
1219 EXPECT_FALSE(adapter_->IsDiscovering());
1220 EXPECT_FALSE(discovery_sessions_[0]->IsActive());
1223 TEST_F(BluetoothChromeOSTest, QueuedDiscoveryRequests) {
1224 GetAdapter();
1226 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
1227 EXPECT_EQ(1, callback_count_);
1228 EXPECT_EQ(0, error_callback_count_);
1229 EXPECT_TRUE(adapter_->IsPowered());
1230 callback_count_ = 0;
1232 TestObserver observer(adapter_);
1234 EXPECT_EQ(0, observer.discovering_changed_count_);
1235 EXPECT_FALSE(observer.last_discovering_);
1236 EXPECT_FALSE(adapter_->IsDiscovering());
1238 // Request to start discovery. The call should be pending.
1239 adapter_->StartDiscoverySession(
1240 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1241 base::Unretained(this)),
1242 GetErrorCallback());
1243 EXPECT_EQ(0, callback_count_);
1245 fake_bluetooth_device_client_->EndDiscoverySimulation(
1246 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath));
1248 // The underlying adapter has started discovery, but our call hasn't returned
1249 // yet.
1250 EXPECT_EQ(1, observer.discovering_changed_count_);
1251 EXPECT_TRUE(observer.last_discovering_);
1252 EXPECT_TRUE(adapter_->IsDiscovering());
1253 EXPECT_TRUE(discovery_sessions_.empty());
1255 // Request to start discovery twice. These should get queued and there should
1256 // be no change in state.
1257 for (int i = 0; i < 2; i++) {
1258 adapter_->StartDiscoverySession(
1259 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1260 base::Unretained(this)),
1261 GetErrorCallback());
1263 EXPECT_EQ(0, callback_count_);
1264 EXPECT_EQ(0, error_callback_count_);
1265 EXPECT_EQ(1, observer.discovering_changed_count_);
1266 EXPECT_TRUE(observer.last_discovering_);
1267 EXPECT_TRUE(adapter_->IsDiscovering());
1268 EXPECT_TRUE(discovery_sessions_.empty());
1270 // Process the pending call. The queued calls should execute and the discovery
1271 // session reference count should increase.
1272 message_loop_.Run();
1273 EXPECT_EQ(3, callback_count_);
1274 EXPECT_EQ(0, error_callback_count_);
1275 EXPECT_EQ(1, observer.discovering_changed_count_);
1276 EXPECT_TRUE(observer.last_discovering_);
1277 EXPECT_TRUE(adapter_->IsDiscovering());
1278 ASSERT_EQ((size_t)3, discovery_sessions_.size());
1280 // Verify the reference count by removing sessions 3 times. The last request
1281 // should remain pending.
1282 for (int i = 0; i < 3; i++) {
1283 discovery_sessions_[i]->Stop(GetCallback(), GetErrorCallback());
1285 EXPECT_EQ(5, callback_count_);
1286 EXPECT_EQ(0, error_callback_count_);
1287 EXPECT_EQ(2, observer.discovering_changed_count_);
1288 EXPECT_FALSE(observer.last_discovering_);
1289 EXPECT_FALSE(adapter_->IsDiscovering());
1290 EXPECT_FALSE(discovery_sessions_[0]->IsActive());
1291 EXPECT_FALSE(discovery_sessions_[1]->IsActive());
1292 EXPECT_TRUE(discovery_sessions_[2]->IsActive());
1294 // Request to stop the session whose call is pending should fail.
1295 discovery_sessions_[2]->Stop(GetCallback(), GetErrorCallback());
1296 EXPECT_EQ(5, callback_count_);
1297 EXPECT_EQ(1, error_callback_count_);
1298 EXPECT_EQ(2, observer.discovering_changed_count_);
1299 EXPECT_FALSE(observer.last_discovering_);
1300 EXPECT_FALSE(adapter_->IsDiscovering());
1301 EXPECT_TRUE(discovery_sessions_[2]->IsActive());
1303 // Request to start should get queued.
1304 adapter_->StartDiscoverySession(
1305 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1306 base::Unretained(this)),
1307 GetErrorCallback());
1308 EXPECT_EQ(5, callback_count_);
1309 EXPECT_EQ(1, error_callback_count_);
1310 EXPECT_EQ(2, observer.discovering_changed_count_);
1311 EXPECT_FALSE(observer.last_discovering_);
1312 EXPECT_FALSE(adapter_->IsDiscovering());
1313 ASSERT_EQ((size_t)3, discovery_sessions_.size());
1315 // Run the pending request.
1316 message_loop_.Run();
1317 EXPECT_EQ(6, callback_count_);
1318 EXPECT_EQ(1, error_callback_count_);
1319 EXPECT_EQ(3, observer.discovering_changed_count_);
1320 EXPECT_TRUE(observer.last_discovering_);
1321 EXPECT_TRUE(adapter_->IsDiscovering());
1322 ASSERT_EQ((size_t)3, discovery_sessions_.size());
1323 EXPECT_FALSE(discovery_sessions_[2]->IsActive());
1325 // The queued request to start discovery should have been issued but is still
1326 // pending. Run the loop and verify.
1327 message_loop_.Run();
1328 EXPECT_EQ(7, callback_count_);
1329 EXPECT_EQ(1, error_callback_count_);
1330 EXPECT_EQ(3, observer.discovering_changed_count_);
1331 EXPECT_TRUE(observer.last_discovering_);
1332 EXPECT_TRUE(adapter_->IsDiscovering());
1333 ASSERT_EQ((size_t)4, discovery_sessions_.size());
1334 EXPECT_TRUE(discovery_sessions_[3]->IsActive());
1337 TEST_F(BluetoothChromeOSTest, StartDiscoverySession) {
1338 GetAdapter();
1340 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
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 GetErrorCallback());
1358 message_loop_.Run();
1359 EXPECT_EQ(1, observer.discovering_changed_count_);
1360 EXPECT_EQ(1, callback_count_);
1361 EXPECT_EQ(0, error_callback_count_);
1362 EXPECT_TRUE(observer.last_discovering_);
1363 EXPECT_TRUE(adapter_->IsDiscovering());
1364 ASSERT_EQ((size_t)1, discovery_sessions_.size());
1365 EXPECT_TRUE(discovery_sessions_[0]->IsActive());
1367 // Start another session. A new one should be returned in the callback, which
1368 // in turn will destroy the previous session. Adapter should still be
1369 // discovering and the reference count should be 1.
1370 adapter_->StartDiscoverySession(
1371 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1372 base::Unretained(this)),
1373 GetErrorCallback());
1374 message_loop_.Run();
1375 EXPECT_EQ(1, observer.discovering_changed_count_);
1376 EXPECT_EQ(2, callback_count_);
1377 EXPECT_EQ(0, error_callback_count_);
1378 EXPECT_TRUE(observer.last_discovering_);
1379 EXPECT_TRUE(adapter_->IsDiscovering());
1380 ASSERT_EQ((size_t)2, discovery_sessions_.size());
1381 EXPECT_TRUE(discovery_sessions_[0]->IsActive());
1383 // Request a new session.
1384 adapter_->StartDiscoverySession(
1385 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1386 base::Unretained(this)),
1387 GetErrorCallback());
1388 message_loop_.Run();
1389 EXPECT_EQ(1, observer.discovering_changed_count_);
1390 EXPECT_EQ(3, callback_count_);
1391 EXPECT_EQ(0, error_callback_count_);
1392 EXPECT_TRUE(observer.last_discovering_);
1393 EXPECT_TRUE(adapter_->IsDiscovering());
1394 ASSERT_EQ((size_t)3, discovery_sessions_.size());
1395 EXPECT_TRUE(discovery_sessions_[1]->IsActive());
1396 EXPECT_NE(discovery_sessions_[0], discovery_sessions_[1]);
1398 // Stop the previous discovery session. The session should end but discovery
1399 // should continue.
1400 discovery_sessions_[0]->Stop(GetCallback(), GetErrorCallback());
1401 message_loop_.Run();
1402 EXPECT_EQ(1, observer.discovering_changed_count_);
1403 EXPECT_EQ(4, callback_count_);
1404 EXPECT_EQ(0, error_callback_count_);
1405 EXPECT_TRUE(observer.last_discovering_);
1406 EXPECT_TRUE(adapter_->IsDiscovering());
1407 ASSERT_EQ((size_t)3, discovery_sessions_.size());
1408 EXPECT_FALSE(discovery_sessions_[0]->IsActive());
1409 EXPECT_TRUE(discovery_sessions_[1]->IsActive());
1411 // Delete the current active session. Discovery should eventually stop.
1412 discovery_sessions_.clear();
1413 while (observer.last_discovering_)
1414 message_loop_.RunUntilIdle();
1416 EXPECT_EQ(2, observer.discovering_changed_count_);
1417 EXPECT_EQ(4, callback_count_);
1418 EXPECT_EQ(0, error_callback_count_);
1419 EXPECT_FALSE(observer.last_discovering_);
1420 EXPECT_FALSE(adapter_->IsDiscovering());
1423 TEST_F(BluetoothChromeOSTest, DeviceProperties) {
1424 GetAdapter();
1426 BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
1427 ASSERT_EQ(2U, devices.size());
1428 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
1429 devices[0]->GetAddress());
1431 // Verify the other device properties.
1432 EXPECT_EQ(base::UTF8ToUTF16(FakeBluetoothDeviceClient::kPairedDeviceName),
1433 devices[0]->GetName());
1434 EXPECT_EQ(BluetoothDevice::DEVICE_COMPUTER, devices[0]->GetDeviceType());
1435 EXPECT_TRUE(devices[0]->IsPaired());
1436 EXPECT_FALSE(devices[0]->IsConnected());
1437 EXPECT_FALSE(devices[0]->IsConnecting());
1439 // Non HID devices are always connectable.
1440 EXPECT_TRUE(devices[0]->IsConnectable());
1442 BluetoothDevice::UUIDList uuids = devices[0]->GetUUIDs();
1443 ASSERT_EQ(2U, uuids.size());
1444 EXPECT_EQ(uuids[0], BluetoothUUID("1800"));
1445 EXPECT_EQ(uuids[1], BluetoothUUID("1801"));
1447 EXPECT_EQ(BluetoothDevice::VENDOR_ID_USB, devices[0]->GetVendorIDSource());
1448 EXPECT_EQ(0x05ac, devices[0]->GetVendorID());
1449 EXPECT_EQ(0x030d, devices[0]->GetProductID());
1450 EXPECT_EQ(0x0306, devices[0]->GetDeviceID());
1453 TEST_F(BluetoothChromeOSTest, DeviceClassChanged) {
1454 // Simulate a change of class of a device, as sometimes occurs
1455 // during discovery.
1456 GetAdapter();
1458 BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
1459 ASSERT_EQ(2U, devices.size());
1460 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
1461 devices[0]->GetAddress());
1462 ASSERT_EQ(BluetoothDevice::DEVICE_COMPUTER, devices[0]->GetDeviceType());
1464 // Install an observer; expect the DeviceChanged method to be called when
1465 // we change the class of the device.
1466 TestObserver observer(adapter_);
1468 FakeBluetoothDeviceClient::Properties* properties =
1469 fake_bluetooth_device_client_->GetProperties(
1470 dbus::ObjectPath(FakeBluetoothDeviceClient::kPairedDevicePath));
1472 properties->bluetooth_class.ReplaceValue(0x002580);
1474 EXPECT_EQ(1, observer.device_changed_count_);
1475 EXPECT_EQ(devices[0], observer.last_device_);
1477 EXPECT_EQ(BluetoothDevice::DEVICE_MOUSE, devices[0]->GetDeviceType());
1480 TEST_F(BluetoothChromeOSTest, DeviceNameChanged) {
1481 // Simulate a change of name of a device.
1482 GetAdapter();
1484 BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
1485 ASSERT_EQ(2U, devices.size());
1486 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
1487 devices[0]->GetAddress());
1488 ASSERT_EQ(base::UTF8ToUTF16(FakeBluetoothDeviceClient::kPairedDeviceName),
1489 devices[0]->GetName());
1491 // Install an observer; expect the DeviceChanged method to be called when
1492 // we change the alias of the device.
1493 TestObserver observer(adapter_);
1495 FakeBluetoothDeviceClient::Properties* properties =
1496 fake_bluetooth_device_client_->GetProperties(
1497 dbus::ObjectPath(FakeBluetoothDeviceClient::kPairedDevicePath));
1499 static const std::string new_name("New Device Name");
1500 properties->alias.ReplaceValue(new_name);
1502 EXPECT_EQ(1, observer.device_changed_count_);
1503 EXPECT_EQ(devices[0], observer.last_device_);
1505 EXPECT_EQ(base::UTF8ToUTF16(new_name), devices[0]->GetName());
1508 TEST_F(BluetoothChromeOSTest, DeviceUuidsChanged) {
1509 // Simulate a change of advertised services of a device.
1510 GetAdapter();
1512 BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
1513 ASSERT_EQ(2U, devices.size());
1514 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
1515 devices[0]->GetAddress());
1517 BluetoothDevice::UUIDList uuids = devices[0]->GetUUIDs();
1518 ASSERT_EQ(2U, uuids.size());
1519 ASSERT_EQ(uuids[0], BluetoothUUID("1800"));
1520 ASSERT_EQ(uuids[1], BluetoothUUID("1801"));
1522 // Install an observer; expect the DeviceChanged method to be called when
1523 // we change the class of the device.
1524 TestObserver observer(adapter_);
1526 FakeBluetoothDeviceClient::Properties* properties =
1527 fake_bluetooth_device_client_->GetProperties(
1528 dbus::ObjectPath(FakeBluetoothDeviceClient::kPairedDevicePath));
1530 std::vector<std::string> new_uuids;
1531 new_uuids.push_back(uuids[0].canonical_value());
1532 new_uuids.push_back(uuids[1].canonical_value());
1533 new_uuids.push_back("0000110c-0000-1000-8000-00805f9b34fb");
1534 new_uuids.push_back("0000110e-0000-1000-8000-00805f9b34fb");
1535 new_uuids.push_back("0000110a-0000-1000-8000-00805f9b34fb");
1537 properties->uuids.ReplaceValue(new_uuids);
1539 EXPECT_EQ(1, observer.device_changed_count_);
1540 EXPECT_EQ(devices[0], observer.last_device_);
1542 // Fetching the value should give the new one.
1543 uuids = devices[0]->GetUUIDs();
1544 ASSERT_EQ(5U, uuids.size());
1545 EXPECT_EQ(uuids[0], BluetoothUUID("1800"));
1546 EXPECT_EQ(uuids[1], BluetoothUUID("1801"));
1547 EXPECT_EQ(uuids[2], BluetoothUUID("110c"));
1548 EXPECT_EQ(uuids[3], BluetoothUUID("110e"));
1549 EXPECT_EQ(uuids[4], BluetoothUUID("110a"));
1552 TEST_F(BluetoothChromeOSTest, ForgetDevice) {
1553 GetAdapter();
1555 BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
1556 ASSERT_EQ(2U, devices.size());
1557 ASSERT_EQ(FakeBluetoothDeviceClient::kPairedDeviceAddress,
1558 devices[0]->GetAddress());
1560 std::string address = devices[0]->GetAddress();
1562 // Install an observer; expect the DeviceRemoved method to be called
1563 // with the device we remove.
1564 TestObserver observer(adapter_);
1566 devices[0]->Forget(GetErrorCallback());
1567 EXPECT_EQ(0, error_callback_count_);
1569 EXPECT_EQ(1, observer.device_removed_count_);
1570 EXPECT_EQ(address, observer.last_device_address_);
1572 // GetDevices shouldn't return the device either.
1573 devices = adapter_->GetDevices();
1574 ASSERT_EQ(1U, devices.size());
1577 TEST_F(BluetoothChromeOSTest, ForgetUnpairedDevice) {
1578 GetAdapter();
1579 DiscoverDevices();
1581 BluetoothDevice* device = adapter_->GetDevice(
1582 FakeBluetoothDeviceClient::kConnectUnpairableAddress);
1583 ASSERT_TRUE(device != NULL);
1584 ASSERT_FALSE(device->IsPaired());
1586 // Connect the device so it becomes trusted and remembered.
1587 device->Connect(NULL, GetCallback(),
1588 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
1589 base::Unretained(this)));
1591 ASSERT_EQ(1, callback_count_);
1592 ASSERT_EQ(0, error_callback_count_);
1593 callback_count_ = 0;
1595 ASSERT_TRUE(device->IsConnected());
1596 ASSERT_FALSE(device->IsConnecting());
1598 // Make sure the trusted property has been set to true.
1599 FakeBluetoothDeviceClient::Properties* properties =
1600 fake_bluetooth_device_client_->GetProperties(
1601 dbus::ObjectPath(FakeBluetoothDeviceClient::kConnectUnpairablePath));
1602 ASSERT_TRUE(properties->trusted.value());
1604 // Install an observer; expect the DeviceRemoved method to be called
1605 // with the device we remove.
1606 TestObserver observer(adapter_);
1608 device->Forget(GetErrorCallback());
1609 EXPECT_EQ(0, error_callback_count_);
1611 EXPECT_EQ(1, observer.device_removed_count_);
1612 EXPECT_EQ(FakeBluetoothDeviceClient::kConnectUnpairableAddress,
1613 observer.last_device_address_);
1615 // GetDevices shouldn't return the device either.
1616 device = adapter_->GetDevice(
1617 FakeBluetoothDeviceClient::kConnectUnpairableAddress);
1618 EXPECT_FALSE(device != NULL);
1621 TEST_F(BluetoothChromeOSTest, ConnectPairedDevice) {
1622 GetAdapter();
1624 BluetoothDevice* device = adapter_->GetDevice(
1625 FakeBluetoothDeviceClient::kPairedDeviceAddress);
1626 ASSERT_TRUE(device != NULL);
1627 ASSERT_TRUE(device->IsPaired());
1629 TestObserver observer(adapter_);
1631 // Connect without a pairing delegate; since the device is already Paired
1632 // this should succeed and the device should become connected.
1633 device->Connect(NULL, GetCallback(),
1634 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
1635 base::Unretained(this)));
1637 EXPECT_EQ(1, callback_count_);
1638 EXPECT_EQ(0, error_callback_count_);
1640 // Two changes for connecting, one for connected and one for for trusted
1641 // after connecting.
1642 EXPECT_EQ(4, observer.device_changed_count_);
1643 EXPECT_EQ(device, observer.last_device_);
1645 EXPECT_TRUE(device->IsConnected());
1646 EXPECT_FALSE(device->IsConnecting());
1649 TEST_F(BluetoothChromeOSTest, ConnectUnpairableDevice) {
1650 GetAdapter();
1651 DiscoverDevices();
1653 BluetoothDevice* device = adapter_->GetDevice(
1654 FakeBluetoothDeviceClient::kConnectUnpairableAddress);
1655 ASSERT_TRUE(device != NULL);
1656 ASSERT_FALSE(device->IsPaired());
1658 TestObserver observer(adapter_);
1660 // Connect without a pairing delegate; since the device does not require
1661 // pairing, this should succeed and the device should become connected.
1662 device->Connect(NULL, GetCallback(),
1663 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
1664 base::Unretained(this)));
1666 EXPECT_EQ(1, callback_count_);
1667 EXPECT_EQ(0, error_callback_count_);
1669 // Two changes for connecting, one for connected, one for for trusted after
1670 // connection, and one for the reconnect mode (IsConnectable).
1671 EXPECT_EQ(5, observer.device_changed_count_);
1672 EXPECT_EQ(device, observer.last_device_);
1674 EXPECT_TRUE(device->IsConnected());
1675 EXPECT_FALSE(device->IsConnecting());
1677 // Make sure the trusted property has been set to true.
1678 FakeBluetoothDeviceClient::Properties* properties =
1679 fake_bluetooth_device_client_->GetProperties(
1680 dbus::ObjectPath(FakeBluetoothDeviceClient::kConnectUnpairablePath));
1681 EXPECT_TRUE(properties->trusted.value());
1683 // Verify is a HID device and is not connectable.
1684 BluetoothDevice::UUIDList uuids = device->GetUUIDs();
1685 ASSERT_EQ(1U, uuids.size());
1686 EXPECT_EQ(uuids[0], BluetoothUUID("1124"));
1687 EXPECT_FALSE(device->IsConnectable());
1690 TEST_F(BluetoothChromeOSTest, ConnectConnectedDevice) {
1691 GetAdapter();
1693 BluetoothDevice* device = adapter_->GetDevice(
1694 FakeBluetoothDeviceClient::kPairedDeviceAddress);
1695 ASSERT_TRUE(device != NULL);
1696 ASSERT_TRUE(device->IsPaired());
1698 device->Connect(NULL, GetCallback(),
1699 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
1700 base::Unretained(this)));
1702 ASSERT_EQ(1, callback_count_);
1703 ASSERT_EQ(0, error_callback_count_);
1704 callback_count_ = 0;
1706 ASSERT_TRUE(device->IsConnected());
1708 // Connect again; since the device is already Connected, this shouldn't do
1709 // anything to initiate the connection.
1710 TestObserver observer(adapter_);
1712 device->Connect(NULL, GetCallback(),
1713 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
1714 base::Unretained(this)));
1716 EXPECT_EQ(1, callback_count_);
1717 EXPECT_EQ(0, error_callback_count_);
1719 // The observer will be called because Connecting will toggle true and false,
1720 // and the trusted property will be updated to true.
1721 EXPECT_EQ(3, observer.device_changed_count_);
1723 EXPECT_TRUE(device->IsConnected());
1724 EXPECT_FALSE(device->IsConnecting());
1727 TEST_F(BluetoothChromeOSTest, ConnectDeviceFails) {
1728 GetAdapter();
1729 DiscoverDevices();
1731 BluetoothDevice* device = adapter_->GetDevice(
1732 FakeBluetoothDeviceClient::kLegacyAutopairAddress);
1733 ASSERT_TRUE(device != NULL);
1734 ASSERT_FALSE(device->IsPaired());
1736 TestObserver observer(adapter_);
1738 // Connect without a pairing delegate; since the device requires pairing,
1739 // this should fail with an error.
1740 device->Connect(NULL, GetCallback(),
1741 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
1742 base::Unretained(this)));
1744 EXPECT_EQ(0, callback_count_);
1745 EXPECT_EQ(1, error_callback_count_);
1746 EXPECT_EQ(BluetoothDevice::ERROR_FAILED, last_connect_error_);
1748 EXPECT_EQ(2, observer.device_changed_count_);
1750 EXPECT_FALSE(device->IsConnected());
1751 EXPECT_FALSE(device->IsConnecting());
1754 TEST_F(BluetoothChromeOSTest, DisconnectDevice) {
1755 GetAdapter();
1757 BluetoothDevice* device = adapter_->GetDevice(
1758 FakeBluetoothDeviceClient::kPairedDeviceAddress);
1759 ASSERT_TRUE(device != NULL);
1760 ASSERT_TRUE(device->IsPaired());
1762 device->Connect(NULL, GetCallback(),
1763 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
1764 base::Unretained(this)));
1766 ASSERT_EQ(1, callback_count_);
1767 ASSERT_EQ(0, error_callback_count_);
1768 callback_count_ = 0;
1770 ASSERT_TRUE(device->IsConnected());
1771 ASSERT_FALSE(device->IsConnecting());
1773 // Disconnect the device, we should see the observer method fire and the
1774 // device get dropped.
1775 TestObserver observer(adapter_);
1777 device->Disconnect(GetCallback(), GetErrorCallback());
1779 EXPECT_EQ(1, callback_count_);
1780 EXPECT_EQ(0, error_callback_count_);
1782 EXPECT_EQ(1, observer.device_changed_count_);
1783 EXPECT_EQ(device, observer.last_device_);
1785 EXPECT_FALSE(device->IsConnected());
1788 TEST_F(BluetoothChromeOSTest, DisconnectUnconnectedDevice) {
1789 GetAdapter();
1791 BluetoothDevice* device = adapter_->GetDevice(
1792 FakeBluetoothDeviceClient::kPairedDeviceAddress);
1793 ASSERT_TRUE(device != NULL);
1794 ASSERT_TRUE(device->IsPaired());
1795 ASSERT_FALSE(device->IsConnected());
1797 // Disconnect the device, we should see the observer method fire and the
1798 // device get dropped.
1799 TestObserver observer(adapter_);
1801 device->Disconnect(GetCallback(), GetErrorCallback());
1803 EXPECT_EQ(0, callback_count_);
1804 EXPECT_EQ(1, error_callback_count_);
1806 EXPECT_EQ(0, observer.device_changed_count_);
1808 EXPECT_FALSE(device->IsConnected());
1811 TEST_F(BluetoothChromeOSTest, PairLegacyAutopair) {
1812 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
1814 GetAdapter();
1815 DiscoverDevices();
1817 // The Legacy Autopair device requires no PIN or Passkey to pair because
1818 // the daemon provides 0000 to the device for us.
1819 BluetoothDevice* device = adapter_->GetDevice(
1820 FakeBluetoothDeviceClient::kLegacyAutopairAddress);
1821 ASSERT_TRUE(device != NULL);
1822 ASSERT_FALSE(device->IsPaired());
1824 TestObserver observer(adapter_);
1826 TestPairingDelegate pairing_delegate;
1827 device->Connect(&pairing_delegate, GetCallback(),
1828 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
1829 base::Unretained(this)));
1831 EXPECT_EQ(0, pairing_delegate.call_count_);
1832 EXPECT_TRUE(device->IsConnecting());
1834 message_loop_.Run();
1836 EXPECT_EQ(1, callback_count_);
1837 EXPECT_EQ(0, error_callback_count_);
1839 // Two changes for connecting, one change for connected, one for paired,
1840 // two for trusted (after pairing and connection), and one for the reconnect
1841 // mode (IsConnectable).
1842 EXPECT_EQ(7, observer.device_changed_count_);
1843 EXPECT_EQ(device, observer.last_device_);
1845 EXPECT_TRUE(device->IsConnected());
1846 EXPECT_FALSE(device->IsConnecting());
1848 EXPECT_TRUE(device->IsPaired());
1850 // Verify is a HID device and is connectable.
1851 BluetoothDevice::UUIDList uuids = device->GetUUIDs();
1852 ASSERT_EQ(1U, uuids.size());
1853 EXPECT_EQ(uuids[0], BluetoothUUID("1124"));
1854 EXPECT_TRUE(device->IsConnectable());
1856 // Make sure the trusted property has been set to true.
1857 FakeBluetoothDeviceClient::Properties* properties =
1858 fake_bluetooth_device_client_->GetProperties(
1859 dbus::ObjectPath(FakeBluetoothDeviceClient::kLegacyAutopairPath));
1860 EXPECT_TRUE(properties->trusted.value());
1863 TEST_F(BluetoothChromeOSTest, PairDisplayPinCode) {
1864 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
1866 GetAdapter();
1867 DiscoverDevices();
1869 // Requires that we display a randomly generated PIN on the screen.
1870 BluetoothDevice* device = adapter_->GetDevice(
1871 FakeBluetoothDeviceClient::kDisplayPinCodeAddress);
1872 ASSERT_TRUE(device != NULL);
1873 ASSERT_FALSE(device->IsPaired());
1875 TestObserver observer(adapter_);
1877 TestPairingDelegate pairing_delegate;
1878 device->Connect(&pairing_delegate, GetCallback(),
1879 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
1880 base::Unretained(this)));
1882 EXPECT_EQ(1, pairing_delegate.call_count_);
1883 EXPECT_EQ(1, pairing_delegate.display_pincode_count_);
1884 EXPECT_EQ("123456", pairing_delegate.last_pincode_);
1885 EXPECT_TRUE(device->IsConnecting());
1887 message_loop_.Run();
1889 EXPECT_EQ(1, callback_count_);
1890 EXPECT_EQ(0, error_callback_count_);
1892 // Two changes for connecting, one change for connected, one for paired,
1893 // two for trusted (after pairing and connection), and one for the reconnect
1894 // mode (IsConnectable).
1895 EXPECT_EQ(7, observer.device_changed_count_);
1896 EXPECT_EQ(device, observer.last_device_);
1898 EXPECT_TRUE(device->IsConnected());
1899 EXPECT_FALSE(device->IsConnecting());
1901 EXPECT_TRUE(device->IsPaired());
1903 // Verify is a HID device and is connectable.
1904 BluetoothDevice::UUIDList uuids = device->GetUUIDs();
1905 ASSERT_EQ(1U, uuids.size());
1906 EXPECT_EQ(uuids[0], BluetoothUUID("1124"));
1907 EXPECT_TRUE(device->IsConnectable());
1909 // Make sure the trusted property has been set to true.
1910 FakeBluetoothDeviceClient::Properties* properties =
1911 fake_bluetooth_device_client_->GetProperties(
1912 dbus::ObjectPath(FakeBluetoothDeviceClient::kDisplayPinCodePath));
1913 EXPECT_TRUE(properties->trusted.value());
1916 TEST_F(BluetoothChromeOSTest, PairDisplayPasskey) {
1917 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
1919 GetAdapter();
1920 DiscoverDevices();
1922 // Requires that we display a randomly generated Passkey on the screen,
1923 // and notifies us as it's typed in.
1924 BluetoothDevice* device = adapter_->GetDevice(
1925 FakeBluetoothDeviceClient::kDisplayPasskeyAddress);
1926 ASSERT_TRUE(device != NULL);
1927 ASSERT_FALSE(device->IsPaired());
1929 TestObserver observer(adapter_);
1931 TestPairingDelegate pairing_delegate;
1932 device->Connect(&pairing_delegate, GetCallback(),
1933 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
1934 base::Unretained(this)));
1936 // One call for DisplayPasskey() and one for KeysEntered().
1937 EXPECT_EQ(2, pairing_delegate.call_count_);
1938 EXPECT_EQ(1, pairing_delegate.display_passkey_count_);
1939 EXPECT_EQ(123456U, pairing_delegate.last_passkey_);
1940 EXPECT_EQ(1, pairing_delegate.keys_entered_count_);
1941 EXPECT_EQ(0U, pairing_delegate.last_entered_);
1943 EXPECT_TRUE(device->IsConnecting());
1945 // One call to KeysEntered() for each key, including [enter].
1946 for(int i = 1; i <= 7; ++i) {
1947 message_loop_.Run();
1949 EXPECT_EQ(2 + i, pairing_delegate.call_count_);
1950 EXPECT_EQ(1 + i, pairing_delegate.keys_entered_count_);
1951 EXPECT_EQ(static_cast<uint32_t>(i), pairing_delegate.last_entered_);
1954 message_loop_.Run();
1956 // 8 KeysEntered notifications (0 to 7, inclusive) and one aditional call for
1957 // DisplayPasskey().
1958 EXPECT_EQ(9, pairing_delegate.call_count_);
1959 EXPECT_EQ(8, pairing_delegate.keys_entered_count_);
1960 EXPECT_EQ(7U, pairing_delegate.last_entered_);
1962 EXPECT_EQ(1, callback_count_);
1963 EXPECT_EQ(0, error_callback_count_);
1965 // Two changes for connecting, one change for connected, one for paired,
1966 // two for trusted (after pairing and connection), and one for the reconnect
1967 // mode (IsConnectable).
1968 EXPECT_EQ(7, observer.device_changed_count_);
1969 EXPECT_EQ(device, observer.last_device_);
1971 EXPECT_TRUE(device->IsConnected());
1972 EXPECT_FALSE(device->IsConnecting());
1974 EXPECT_TRUE(device->IsPaired());
1976 // Verify is a HID device.
1977 BluetoothDevice::UUIDList uuids = device->GetUUIDs();
1978 ASSERT_EQ(1U, uuids.size());
1979 EXPECT_EQ(uuids[0], BluetoothUUID("1124"));
1981 // And usually not connectable.
1982 EXPECT_FALSE(device->IsConnectable());
1984 // Make sure the trusted property has been set to true.
1985 FakeBluetoothDeviceClient::Properties* properties =
1986 fake_bluetooth_device_client_->GetProperties(
1987 dbus::ObjectPath(FakeBluetoothDeviceClient::kDisplayPasskeyPath));
1988 EXPECT_TRUE(properties->trusted.value());
1991 TEST_F(BluetoothChromeOSTest, PairRequestPinCode) {
1992 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
1994 GetAdapter();
1995 DiscoverDevices();
1997 // Requires that the user enters a PIN for them.
1998 BluetoothDevice* device = adapter_->GetDevice(
1999 FakeBluetoothDeviceClient::kRequestPinCodeAddress);
2000 ASSERT_TRUE(device != NULL);
2001 ASSERT_FALSE(device->IsPaired());
2003 TestObserver observer(adapter_);
2005 TestPairingDelegate pairing_delegate;
2006 device->Connect(&pairing_delegate, GetCallback(),
2007 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2008 base::Unretained(this)));
2010 EXPECT_EQ(1, pairing_delegate.call_count_);
2011 EXPECT_EQ(1, pairing_delegate.request_pincode_count_);
2012 EXPECT_TRUE(device->IsConnecting());
2014 // Set the PIN.
2015 device->SetPinCode("1234");
2016 message_loop_.Run();
2018 EXPECT_EQ(1, callback_count_);
2019 EXPECT_EQ(0, error_callback_count_);
2021 // Two changes for connecting, one change for connected, one for paired and
2022 // two for trusted (after pairing and connection).
2023 EXPECT_EQ(6, observer.device_changed_count_);
2024 EXPECT_EQ(device, observer.last_device_);
2026 EXPECT_TRUE(device->IsConnected());
2027 EXPECT_FALSE(device->IsConnecting());
2029 EXPECT_TRUE(device->IsPaired());
2031 // Verify is not a HID device.
2032 BluetoothDevice::UUIDList uuids = device->GetUUIDs();
2033 ASSERT_EQ(0U, uuids.size());
2035 // Non HID devices are always connectable.
2036 EXPECT_TRUE(device->IsConnectable());
2038 // Make sure the trusted property has been set to true.
2039 FakeBluetoothDeviceClient::Properties* properties =
2040 fake_bluetooth_device_client_->GetProperties(
2041 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath));
2042 EXPECT_TRUE(properties->trusted.value());
2045 TEST_F(BluetoothChromeOSTest, PairConfirmPasskey) {
2046 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2048 GetAdapter();
2049 DiscoverDevices();
2051 // Requests that we confirm a displayed passkey.
2052 BluetoothDevice* device = adapter_->GetDevice(
2053 FakeBluetoothDeviceClient::kConfirmPasskeyAddress);
2054 ASSERT_TRUE(device != NULL);
2055 ASSERT_FALSE(device->IsPaired());
2057 TestObserver observer(adapter_);
2059 TestPairingDelegate pairing_delegate;
2060 device->Connect(&pairing_delegate, GetCallback(),
2061 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2062 base::Unretained(this)));
2064 EXPECT_EQ(1, pairing_delegate.call_count_);
2065 EXPECT_EQ(1, pairing_delegate.confirm_passkey_count_);
2066 EXPECT_EQ(123456U, pairing_delegate.last_passkey_);
2067 EXPECT_TRUE(device->IsConnecting());
2069 // Confirm the passkey.
2070 device->ConfirmPairing();
2071 message_loop_.Run();
2073 EXPECT_EQ(1, callback_count_);
2074 EXPECT_EQ(0, error_callback_count_);
2076 // Two changes for connecting, one change for connected, one for paired and
2077 // two for trusted (after pairing and connection).
2078 EXPECT_EQ(6, observer.device_changed_count_);
2079 EXPECT_EQ(device, observer.last_device_);
2081 EXPECT_TRUE(device->IsConnected());
2082 EXPECT_FALSE(device->IsConnecting());
2084 EXPECT_TRUE(device->IsPaired());
2086 // Non HID devices are always connectable.
2087 EXPECT_TRUE(device->IsConnectable());
2089 // Make sure the trusted property has been set to true.
2090 FakeBluetoothDeviceClient::Properties* properties =
2091 fake_bluetooth_device_client_->GetProperties(
2092 dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath));
2093 EXPECT_TRUE(properties->trusted.value());
2096 TEST_F(BluetoothChromeOSTest, PairRequestPasskey) {
2097 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2099 GetAdapter();
2100 DiscoverDevices();
2102 // Requires that the user enters a Passkey, this would be some kind of
2103 // device that has a display, but doesn't use "just works" - maybe a car?
2104 BluetoothDevice* device = adapter_->GetDevice(
2105 FakeBluetoothDeviceClient::kRequestPasskeyAddress);
2106 ASSERT_TRUE(device != NULL);
2107 ASSERT_FALSE(device->IsPaired());
2109 TestObserver observer(adapter_);
2111 TestPairingDelegate pairing_delegate;
2112 device->Connect(&pairing_delegate, GetCallback(),
2113 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2114 base::Unretained(this)));
2116 EXPECT_EQ(1, pairing_delegate.call_count_);
2117 EXPECT_EQ(1, pairing_delegate.request_passkey_count_);
2118 EXPECT_TRUE(device->IsConnecting());
2120 // Set the Passkey.
2121 device->SetPasskey(1234);
2122 message_loop_.Run();
2124 EXPECT_EQ(1, callback_count_);
2125 EXPECT_EQ(0, error_callback_count_);
2127 // Two changes for connecting, one change for connected, one for paired and
2128 // two for trusted (after pairing and connection).
2129 EXPECT_EQ(6, observer.device_changed_count_);
2130 EXPECT_EQ(device, observer.last_device_);
2132 EXPECT_TRUE(device->IsConnected());
2133 EXPECT_FALSE(device->IsConnecting());
2135 EXPECT_TRUE(device->IsPaired());
2137 // Non HID devices are always connectable.
2138 EXPECT_TRUE(device->IsConnectable());
2140 // Make sure the trusted property has been set to true.
2141 FakeBluetoothDeviceClient::Properties* properties =
2142 fake_bluetooth_device_client_->GetProperties(
2143 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath));
2144 EXPECT_TRUE(properties->trusted.value());
2147 TEST_F(BluetoothChromeOSTest, PairJustWorks) {
2148 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2150 GetAdapter();
2151 DiscoverDevices();
2153 // Uses just-works pairing, since this is an outgoing pairing, no delegate
2154 // interaction is required.
2155 BluetoothDevice* device = adapter_->GetDevice(
2156 FakeBluetoothDeviceClient::kJustWorksAddress);
2157 ASSERT_TRUE(device != NULL);
2158 ASSERT_FALSE(device->IsPaired());
2160 TestObserver observer(adapter_);
2162 TestPairingDelegate pairing_delegate;
2163 device->Connect(&pairing_delegate, GetCallback(),
2164 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2165 base::Unretained(this)));
2167 EXPECT_EQ(0, pairing_delegate.call_count_);
2169 message_loop_.Run();
2171 EXPECT_EQ(1, callback_count_);
2172 EXPECT_EQ(0, error_callback_count_);
2174 // Two changes for connecting, one change for connected, one for paired and
2175 // two for trusted (after pairing and connection).
2176 EXPECT_EQ(6, observer.device_changed_count_);
2177 EXPECT_EQ(device, observer.last_device_);
2179 EXPECT_TRUE(device->IsConnected());
2180 EXPECT_FALSE(device->IsConnecting());
2182 EXPECT_TRUE(device->IsPaired());
2184 // Non HID devices are always connectable.
2185 EXPECT_TRUE(device->IsConnectable());
2187 // Make sure the trusted property has been set to true.
2188 FakeBluetoothDeviceClient::Properties* properties =
2189 fake_bluetooth_device_client_->GetProperties(
2190 dbus::ObjectPath(FakeBluetoothDeviceClient::kJustWorksPath));
2191 EXPECT_TRUE(properties->trusted.value());
2194 TEST_F(BluetoothChromeOSTest, PairUnpairableDeviceFails) {
2195 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2197 GetAdapter();
2198 DiscoverDevice(FakeBluetoothDeviceClient::kUnconnectableDeviceAddress);
2200 BluetoothDevice* device = adapter_->GetDevice(
2201 FakeBluetoothDeviceClient::kUnpairableDeviceAddress);
2202 ASSERT_TRUE(device != NULL);
2203 ASSERT_FALSE(device->IsPaired());
2205 TestObserver observer(adapter_);
2207 TestPairingDelegate pairing_delegate;
2208 device->Connect(&pairing_delegate, GetCallback(),
2209 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2210 base::Unretained(this)));
2212 EXPECT_EQ(0, pairing_delegate.call_count_);
2213 EXPECT_TRUE(device->IsConnecting());
2215 // Run the loop to get the error..
2216 message_loop_.Run();
2218 EXPECT_EQ(0, callback_count_);
2219 EXPECT_EQ(1, error_callback_count_);
2221 EXPECT_EQ(BluetoothDevice::ERROR_FAILED, last_connect_error_);
2223 EXPECT_FALSE(device->IsConnected());
2224 EXPECT_FALSE(device->IsConnecting());
2225 EXPECT_FALSE(device->IsPaired());
2228 TEST_F(BluetoothChromeOSTest, PairingFails) {
2229 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2231 GetAdapter();
2232 DiscoverDevice(FakeBluetoothDeviceClient::kVanishingDeviceAddress);
2234 // The vanishing device times out during pairing
2235 BluetoothDevice* device = adapter_->GetDevice(
2236 FakeBluetoothDeviceClient::kVanishingDeviceAddress);
2237 ASSERT_TRUE(device != NULL);
2238 ASSERT_FALSE(device->IsPaired());
2240 TestObserver observer(adapter_);
2242 TestPairingDelegate pairing_delegate;
2243 device->Connect(&pairing_delegate, GetCallback(),
2244 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2245 base::Unretained(this)));
2247 EXPECT_EQ(0, pairing_delegate.call_count_);
2248 EXPECT_TRUE(device->IsConnecting());
2250 // Run the loop to get the error..
2251 message_loop_.Run();
2253 EXPECT_EQ(0, callback_count_);
2254 EXPECT_EQ(1, error_callback_count_);
2256 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_TIMEOUT, last_connect_error_);
2258 EXPECT_FALSE(device->IsConnected());
2259 EXPECT_FALSE(device->IsConnecting());
2260 EXPECT_FALSE(device->IsPaired());
2263 TEST_F(BluetoothChromeOSTest, PairingFailsAtConnection) {
2264 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2266 GetAdapter();
2267 DiscoverDevices();
2269 // Everything seems to go according to plan with the unconnectable device;
2270 // it pairs, but then you can't make connections to it after.
2271 BluetoothDevice* device = adapter_->GetDevice(
2272 FakeBluetoothDeviceClient::kUnconnectableDeviceAddress);
2273 ASSERT_TRUE(device != NULL);
2274 ASSERT_FALSE(device->IsPaired());
2276 TestObserver observer(adapter_);
2278 TestPairingDelegate pairing_delegate;
2279 device->Connect(&pairing_delegate, GetCallback(),
2280 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2281 base::Unretained(this)));
2283 EXPECT_EQ(0, pairing_delegate.call_count_);
2284 EXPECT_TRUE(device->IsConnecting());
2286 message_loop_.Run();
2288 EXPECT_EQ(0, callback_count_);
2289 EXPECT_EQ(1, error_callback_count_);
2290 EXPECT_EQ(BluetoothDevice::ERROR_FAILED, last_connect_error_);
2292 // Two changes for connecting, one for paired and one for trusted after
2293 // pairing. The device should not be connected.
2294 EXPECT_EQ(4, observer.device_changed_count_);
2295 EXPECT_EQ(device, observer.last_device_);
2297 EXPECT_FALSE(device->IsConnected());
2298 EXPECT_FALSE(device->IsConnecting());
2300 EXPECT_TRUE(device->IsPaired());
2302 // Make sure the trusted property has been set to true still (since pairing
2303 // worked).
2304 FakeBluetoothDeviceClient::Properties* properties =
2305 fake_bluetooth_device_client_->GetProperties(
2306 dbus::ObjectPath(
2307 FakeBluetoothDeviceClient::kUnconnectableDevicePath));
2308 EXPECT_TRUE(properties->trusted.value());
2311 TEST_F(BluetoothChromeOSTest, PairingRejectedAtPinCode) {
2312 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2314 GetAdapter();
2315 DiscoverDevices();
2317 // Reject the pairing after we receive a request for the PIN code.
2318 BluetoothDevice* device = adapter_->GetDevice(
2319 FakeBluetoothDeviceClient::kRequestPinCodeAddress);
2320 ASSERT_TRUE(device != NULL);
2321 ASSERT_FALSE(device->IsPaired());
2323 TestObserver observer(adapter_);
2325 TestPairingDelegate pairing_delegate;
2326 device->Connect(&pairing_delegate, GetCallback(),
2327 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2328 base::Unretained(this)));
2330 EXPECT_EQ(1, pairing_delegate.call_count_);
2331 EXPECT_EQ(1, pairing_delegate.request_pincode_count_);
2332 EXPECT_TRUE(device->IsConnecting());
2334 // Reject the pairing.
2335 device->RejectPairing();
2336 message_loop_.Run();
2338 EXPECT_EQ(0, callback_count_);
2339 EXPECT_EQ(1, error_callback_count_);
2340 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_REJECTED, last_connect_error_);
2342 // Should be no changes except connecting going true and false.
2343 EXPECT_EQ(2, observer.device_changed_count_);
2344 EXPECT_FALSE(device->IsConnected());
2345 EXPECT_FALSE(device->IsConnecting());
2346 EXPECT_FALSE(device->IsPaired());
2349 TEST_F(BluetoothChromeOSTest, PairingCancelledAtPinCode) {
2350 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2352 GetAdapter();
2353 DiscoverDevices();
2355 // Cancel the pairing after we receive a request for the PIN code.
2356 BluetoothDevice* device = adapter_->GetDevice(
2357 FakeBluetoothDeviceClient::kRequestPinCodeAddress);
2358 ASSERT_TRUE(device != NULL);
2359 ASSERT_FALSE(device->IsPaired());
2361 TestObserver observer(adapter_);
2363 TestPairingDelegate pairing_delegate;
2364 device->Connect(&pairing_delegate, GetCallback(),
2365 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2366 base::Unretained(this)));
2368 EXPECT_EQ(1, pairing_delegate.call_count_);
2369 EXPECT_EQ(1, pairing_delegate.request_pincode_count_);
2370 EXPECT_TRUE(device->IsConnecting());
2372 // Cancel the pairing.
2373 device->CancelPairing();
2374 message_loop_.Run();
2376 EXPECT_EQ(0, callback_count_);
2377 EXPECT_EQ(1, error_callback_count_);
2378 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED, last_connect_error_);
2380 // Should be no changes except connecting going true and false.
2381 EXPECT_EQ(2, observer.device_changed_count_);
2382 EXPECT_FALSE(device->IsConnected());
2383 EXPECT_FALSE(device->IsConnecting());
2384 EXPECT_FALSE(device->IsPaired());
2387 TEST_F(BluetoothChromeOSTest, PairingRejectedAtPasskey) {
2388 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2390 GetAdapter();
2391 DiscoverDevices();
2393 // Reject the pairing after we receive a request for the passkey.
2394 BluetoothDevice* device = adapter_->GetDevice(
2395 FakeBluetoothDeviceClient::kRequestPasskeyAddress);
2396 ASSERT_TRUE(device != NULL);
2397 ASSERT_FALSE(device->IsPaired());
2399 TestObserver observer(adapter_);
2401 TestPairingDelegate pairing_delegate;
2402 device->Connect(&pairing_delegate, GetCallback(),
2403 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2404 base::Unretained(this)));
2406 EXPECT_EQ(1, pairing_delegate.call_count_);
2407 EXPECT_EQ(1, pairing_delegate.request_passkey_count_);
2408 EXPECT_TRUE(device->IsConnecting());
2410 // Reject the pairing.
2411 device->RejectPairing();
2412 message_loop_.Run();
2414 EXPECT_EQ(0, callback_count_);
2415 EXPECT_EQ(1, error_callback_count_);
2416 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_REJECTED, last_connect_error_);
2418 // Should be no changes except connecting going true and false.
2419 EXPECT_EQ(2, observer.device_changed_count_);
2420 EXPECT_FALSE(device->IsConnected());
2421 EXPECT_FALSE(device->IsConnecting());
2422 EXPECT_FALSE(device->IsPaired());
2425 TEST_F(BluetoothChromeOSTest, PairingCancelledAtPasskey) {
2426 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2428 GetAdapter();
2429 DiscoverDevices();
2431 // Cancel the pairing after we receive a request for the passkey.
2432 BluetoothDevice* device = adapter_->GetDevice(
2433 FakeBluetoothDeviceClient::kRequestPasskeyAddress);
2434 ASSERT_TRUE(device != NULL);
2435 ASSERT_FALSE(device->IsPaired());
2437 TestObserver observer(adapter_);
2439 TestPairingDelegate pairing_delegate;
2440 device->Connect(&pairing_delegate, GetCallback(),
2441 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2442 base::Unretained(this)));
2444 EXPECT_EQ(1, pairing_delegate.call_count_);
2445 EXPECT_EQ(1, pairing_delegate.request_passkey_count_);
2446 EXPECT_TRUE(device->IsConnecting());
2448 // Cancel the pairing.
2449 device->CancelPairing();
2450 message_loop_.Run();
2452 EXPECT_EQ(0, callback_count_);
2453 EXPECT_EQ(1, error_callback_count_);
2454 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED, last_connect_error_);
2456 // Should be no changes except connecting going true and false.
2457 EXPECT_EQ(2, observer.device_changed_count_);
2458 EXPECT_FALSE(device->IsConnected());
2459 EXPECT_FALSE(device->IsConnecting());
2460 EXPECT_FALSE(device->IsPaired());
2463 TEST_F(BluetoothChromeOSTest, PairingRejectedAtConfirmation) {
2464 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2466 GetAdapter();
2467 DiscoverDevices();
2469 // Reject the pairing after we receive a request for passkey confirmation.
2470 BluetoothDevice* device = adapter_->GetDevice(
2471 FakeBluetoothDeviceClient::kConfirmPasskeyAddress);
2472 ASSERT_TRUE(device != NULL);
2473 ASSERT_FALSE(device->IsPaired());
2475 TestObserver observer(adapter_);
2477 TestPairingDelegate pairing_delegate;
2478 device->Connect(&pairing_delegate, GetCallback(),
2479 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2480 base::Unretained(this)));
2482 EXPECT_EQ(1, pairing_delegate.call_count_);
2483 EXPECT_EQ(1, pairing_delegate.confirm_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, PairingCancelledAtConfirmation) {
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::kConfirmPasskeyAddress);
2510 ASSERT_TRUE(device != NULL);
2511 ASSERT_FALSE(device->IsPaired());
2513 TestObserver observer(adapter_);
2515 TestPairingDelegate pairing_delegate;
2516 device->Connect(&pairing_delegate, GetCallback(),
2517 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2518 base::Unretained(this)));
2520 EXPECT_EQ(1, pairing_delegate.call_count_);
2521 EXPECT_EQ(1, pairing_delegate.confirm_passkey_count_);
2522 EXPECT_TRUE(device->IsConnecting());
2524 // Cancel the pairing.
2525 device->CancelPairing();
2526 message_loop_.Run();
2528 EXPECT_EQ(0, callback_count_);
2529 EXPECT_EQ(1, error_callback_count_);
2530 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED, last_connect_error_);
2532 // Should be no changes except connecting going true and false.
2533 EXPECT_EQ(2, observer.device_changed_count_);
2534 EXPECT_FALSE(device->IsConnected());
2535 EXPECT_FALSE(device->IsConnecting());
2536 EXPECT_FALSE(device->IsPaired());
2539 TEST_F(BluetoothChromeOSTest, PairingCancelledInFlight) {
2540 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2542 GetAdapter();
2543 DiscoverDevices();
2545 // Cancel the pairing while we're waiting for the remote host.
2546 BluetoothDevice* device = adapter_->GetDevice(
2547 FakeBluetoothDeviceClient::kLegacyAutopairAddress);
2548 ASSERT_TRUE(device != NULL);
2549 ASSERT_FALSE(device->IsPaired());
2551 TestObserver observer(adapter_);
2553 TestPairingDelegate pairing_delegate;
2554 device->Connect(&pairing_delegate, GetCallback(),
2555 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2556 base::Unretained(this)));
2558 EXPECT_EQ(0, pairing_delegate.call_count_);
2559 EXPECT_TRUE(device->IsConnecting());
2561 // Cancel the pairing.
2562 device->CancelPairing();
2563 message_loop_.Run();
2565 EXPECT_EQ(0, callback_count_);
2566 EXPECT_EQ(1, error_callback_count_);
2567 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED, last_connect_error_);
2569 // Should be no changes except connecting going true and false.
2570 EXPECT_EQ(2, observer.device_changed_count_);
2571 EXPECT_FALSE(device->IsConnected());
2572 EXPECT_FALSE(device->IsConnecting());
2573 EXPECT_FALSE(device->IsPaired());
2576 TEST_F(BluetoothChromeOSTest, IncomingPairRequestPinCode) {
2577 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2579 GetAdapter();
2581 TestPairingDelegate pairing_delegate;
2582 adapter_->AddPairingDelegate(
2583 &pairing_delegate,
2584 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH);
2586 // Requires that we provide a PIN code.
2587 fake_bluetooth_device_client_->CreateDevice(
2588 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
2589 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath));
2590 BluetoothDevice* device = adapter_->GetDevice(
2591 FakeBluetoothDeviceClient::kRequestPinCodeAddress);
2592 ASSERT_TRUE(device != NULL);
2593 ASSERT_FALSE(device->IsPaired());
2595 TestObserver observer(adapter_);
2597 fake_bluetooth_device_client_->SimulatePairing(
2598 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath), true,
2599 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
2600 base::Unretained(this)));
2602 EXPECT_EQ(1, pairing_delegate.call_count_);
2603 EXPECT_EQ(1, pairing_delegate.request_pincode_count_);
2605 // Set the PIN.
2606 device->SetPinCode("1234");
2607 message_loop_.Run();
2609 EXPECT_EQ(1, callback_count_);
2610 EXPECT_EQ(0, error_callback_count_);
2612 // One change for paired, and one for trusted.
2613 EXPECT_EQ(2, observer.device_changed_count_);
2614 EXPECT_EQ(device, observer.last_device_);
2616 EXPECT_TRUE(device->IsPaired());
2618 // Make sure the trusted property has been set to true.
2619 FakeBluetoothDeviceClient::Properties* properties =
2620 fake_bluetooth_device_client_->GetProperties(
2621 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath));
2622 ASSERT_TRUE(properties->trusted.value());
2624 // No pairing context should remain on the device.
2625 BluetoothDeviceChromeOS* device_chromeos =
2626 static_cast<BluetoothDeviceChromeOS*>(device);
2627 EXPECT_TRUE(device_chromeos->GetPairing() == NULL);
2630 TEST_F(BluetoothChromeOSTest, IncomingPairConfirmPasskey) {
2631 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2633 GetAdapter();
2635 TestPairingDelegate pairing_delegate;
2636 adapter_->AddPairingDelegate(
2637 &pairing_delegate,
2638 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH);
2640 // Requests that we confirm a displayed passkey.
2641 fake_bluetooth_device_client_->CreateDevice(
2642 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
2643 dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath));
2644 BluetoothDevice* device = adapter_->GetDevice(
2645 FakeBluetoothDeviceClient::kConfirmPasskeyAddress);
2646 ASSERT_TRUE(device != NULL);
2647 ASSERT_FALSE(device->IsPaired());
2649 TestObserver observer(adapter_);
2651 fake_bluetooth_device_client_->SimulatePairing(
2652 dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath), true,
2653 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
2654 base::Unretained(this)));
2656 EXPECT_EQ(1, pairing_delegate.call_count_);
2657 EXPECT_EQ(1, pairing_delegate.confirm_passkey_count_);
2658 EXPECT_EQ(123456U, pairing_delegate.last_passkey_);
2660 // Confirm the passkey.
2661 device->ConfirmPairing();
2662 message_loop_.Run();
2664 EXPECT_EQ(1, callback_count_);
2665 EXPECT_EQ(0, error_callback_count_);
2667 // One change for paired, and one for trusted.
2668 EXPECT_EQ(2, observer.device_changed_count_);
2669 EXPECT_EQ(device, observer.last_device_);
2671 EXPECT_TRUE(device->IsPaired());
2673 // Make sure the trusted property has been set to true.
2674 FakeBluetoothDeviceClient::Properties* properties =
2675 fake_bluetooth_device_client_->GetProperties(
2676 dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath));
2677 ASSERT_TRUE(properties->trusted.value());
2679 // No pairing context should remain on the device.
2680 BluetoothDeviceChromeOS* device_chromeos =
2681 static_cast<BluetoothDeviceChromeOS*>(device);
2682 EXPECT_TRUE(device_chromeos->GetPairing() == NULL);
2685 TEST_F(BluetoothChromeOSTest, IncomingPairRequestPasskey) {
2686 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2688 GetAdapter();
2690 TestPairingDelegate pairing_delegate;
2691 adapter_->AddPairingDelegate(
2692 &pairing_delegate,
2693 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH);
2695 // Requests that we provide a Passkey.
2696 fake_bluetooth_device_client_->CreateDevice(
2697 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
2698 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath));
2699 BluetoothDevice* device = adapter_->GetDevice(
2700 FakeBluetoothDeviceClient::kRequestPasskeyAddress);
2701 ASSERT_TRUE(device != NULL);
2702 ASSERT_FALSE(device->IsPaired());
2704 TestObserver observer(adapter_);
2706 fake_bluetooth_device_client_->SimulatePairing(
2707 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath), true,
2708 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
2709 base::Unretained(this)));
2711 EXPECT_EQ(1, pairing_delegate.call_count_);
2712 EXPECT_EQ(1, pairing_delegate.request_passkey_count_);
2714 // Set the Passkey.
2715 device->SetPasskey(1234);
2716 message_loop_.Run();
2718 EXPECT_EQ(1, callback_count_);
2719 EXPECT_EQ(0, error_callback_count_);
2721 // One change for paired, and one for trusted.
2722 EXPECT_EQ(2, observer.device_changed_count_);
2723 EXPECT_EQ(device, observer.last_device_);
2725 EXPECT_TRUE(device->IsPaired());
2727 // Make sure the trusted property has been set to true.
2728 FakeBluetoothDeviceClient::Properties* properties =
2729 fake_bluetooth_device_client_->GetProperties(
2730 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath));
2731 ASSERT_TRUE(properties->trusted.value());
2733 // No pairing context should remain on the device.
2734 BluetoothDeviceChromeOS* device_chromeos =
2735 static_cast<BluetoothDeviceChromeOS*>(device);
2736 EXPECT_TRUE(device_chromeos->GetPairing() == NULL);
2739 TEST_F(BluetoothChromeOSTest, IncomingPairJustWorks) {
2740 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2742 GetAdapter();
2744 TestPairingDelegate pairing_delegate;
2745 adapter_->AddPairingDelegate(
2746 &pairing_delegate,
2747 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH);
2749 // Uses just-works pairing so, sinec this an incoming pairing, require
2750 // authorization from the user.
2751 fake_bluetooth_device_client_->CreateDevice(
2752 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
2753 dbus::ObjectPath(FakeBluetoothDeviceClient::kJustWorksPath));
2754 BluetoothDevice* device = adapter_->GetDevice(
2755 FakeBluetoothDeviceClient::kJustWorksAddress);
2756 ASSERT_TRUE(device != NULL);
2757 ASSERT_FALSE(device->IsPaired());
2759 TestObserver observer(adapter_);
2761 fake_bluetooth_device_client_->SimulatePairing(
2762 dbus::ObjectPath(FakeBluetoothDeviceClient::kJustWorksPath), true,
2763 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
2764 base::Unretained(this)));
2766 EXPECT_EQ(1, pairing_delegate.call_count_);
2767 EXPECT_EQ(1, pairing_delegate.authorize_pairing_count_);
2769 // Confirm the pairing.
2770 device->ConfirmPairing();
2771 message_loop_.Run();
2773 EXPECT_EQ(1, callback_count_);
2774 EXPECT_EQ(0, error_callback_count_);
2776 // One change for paired, and one for trusted.
2777 EXPECT_EQ(2, observer.device_changed_count_);
2778 EXPECT_EQ(device, observer.last_device_);
2780 EXPECT_TRUE(device->IsPaired());
2782 // Make sure the trusted property has been set to true.
2783 FakeBluetoothDeviceClient::Properties* properties =
2784 fake_bluetooth_device_client_->GetProperties(
2785 dbus::ObjectPath(FakeBluetoothDeviceClient::kJustWorksPath));
2786 ASSERT_TRUE(properties->trusted.value());
2788 // No pairing context should remain on the device.
2789 BluetoothDeviceChromeOS* device_chromeos =
2790 static_cast<BluetoothDeviceChromeOS*>(device);
2791 EXPECT_TRUE(device_chromeos->GetPairing() == NULL);
2794 TEST_F(BluetoothChromeOSTest, IncomingPairRequestPinCodeWithoutDelegate) {
2795 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2797 GetAdapter();
2799 // Requires that we provide a PIN Code, without a pairing delegate,
2800 // that will be rejected.
2801 fake_bluetooth_device_client_->CreateDevice(
2802 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
2803 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath));
2804 BluetoothDevice* device = adapter_->GetDevice(
2805 FakeBluetoothDeviceClient::kRequestPinCodeAddress);
2806 ASSERT_TRUE(device != NULL);
2807 ASSERT_FALSE(device->IsPaired());
2809 TestObserver observer(adapter_);
2811 fake_bluetooth_device_client_->SimulatePairing(
2812 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPinCodePath), true,
2813 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
2814 base::Unretained(this)));
2816 message_loop_.Run();
2818 EXPECT_EQ(0, callback_count_);
2819 EXPECT_EQ(1, error_callback_count_);
2820 EXPECT_EQ(bluetooth_device::kErrorAuthenticationRejected, last_client_error_);
2822 // No changes should be observer.
2823 EXPECT_EQ(0, observer.device_changed_count_);
2825 EXPECT_FALSE(device->IsPaired());
2827 // No pairing context should remain on the device.
2828 BluetoothDeviceChromeOS* device_chromeos =
2829 static_cast<BluetoothDeviceChromeOS*>(device);
2830 EXPECT_TRUE(device_chromeos->GetPairing() == NULL);
2833 TEST_F(BluetoothChromeOSTest, IncomingPairConfirmPasskeyWithoutDelegate) {
2834 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2836 GetAdapter();
2838 // Requests that we confirm a displayed passkey, without a pairing delegate,
2839 // that will be rejected.
2840 fake_bluetooth_device_client_->CreateDevice(
2841 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
2842 dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath));
2843 BluetoothDevice* device = adapter_->GetDevice(
2844 FakeBluetoothDeviceClient::kConfirmPasskeyAddress);
2845 ASSERT_TRUE(device != NULL);
2846 ASSERT_FALSE(device->IsPaired());
2848 TestObserver observer(adapter_);
2850 fake_bluetooth_device_client_->SimulatePairing(
2851 dbus::ObjectPath(FakeBluetoothDeviceClient::kConfirmPasskeyPath), true,
2852 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
2853 base::Unretained(this)));
2855 message_loop_.Run();
2857 EXPECT_EQ(0, callback_count_);
2858 EXPECT_EQ(1, error_callback_count_);
2859 EXPECT_EQ(bluetooth_device::kErrorAuthenticationRejected, last_client_error_);
2861 // No changes should be observer.
2862 EXPECT_EQ(0, observer.device_changed_count_);
2864 EXPECT_FALSE(device->IsPaired());
2866 // No pairing context should remain on the device.
2867 BluetoothDeviceChromeOS* device_chromeos =
2868 static_cast<BluetoothDeviceChromeOS*>(device);
2869 EXPECT_TRUE(device_chromeos->GetPairing() == NULL);
2872 TEST_F(BluetoothChromeOSTest, IncomingPairRequestPasskeyWithoutDelegate) {
2873 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2875 GetAdapter();
2877 // Requests that we provide a displayed passkey, without a pairing delegate,
2878 // that will be rejected.
2879 fake_bluetooth_device_client_->CreateDevice(
2880 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
2881 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath));
2882 BluetoothDevice* device = adapter_->GetDevice(
2883 FakeBluetoothDeviceClient::kRequestPasskeyAddress);
2884 ASSERT_TRUE(device != NULL);
2885 ASSERT_FALSE(device->IsPaired());
2887 TestObserver observer(adapter_);
2889 fake_bluetooth_device_client_->SimulatePairing(
2890 dbus::ObjectPath(FakeBluetoothDeviceClient::kRequestPasskeyPath), true,
2891 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
2892 base::Unretained(this)));
2894 message_loop_.Run();
2896 EXPECT_EQ(0, callback_count_);
2897 EXPECT_EQ(1, error_callback_count_);
2898 EXPECT_EQ(bluetooth_device::kErrorAuthenticationRejected, last_client_error_);
2900 // No changes should be observer.
2901 EXPECT_EQ(0, observer.device_changed_count_);
2903 EXPECT_FALSE(device->IsPaired());
2905 // No pairing context should remain on the device.
2906 BluetoothDeviceChromeOS* device_chromeos =
2907 static_cast<BluetoothDeviceChromeOS*>(device);
2908 EXPECT_TRUE(device_chromeos->GetPairing() == NULL);
2911 TEST_F(BluetoothChromeOSTest, IncomingPairJustWorksWithoutDelegate) {
2912 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2914 GetAdapter();
2916 // Uses just-works pairing and thus requires authorization for incoming
2917 // pairings, without a pairing delegate, that will be rejected.
2918 fake_bluetooth_device_client_->CreateDevice(
2919 dbus::ObjectPath(FakeBluetoothAdapterClient::kAdapterPath),
2920 dbus::ObjectPath(FakeBluetoothDeviceClient::kJustWorksPath));
2921 BluetoothDevice* device = adapter_->GetDevice(
2922 FakeBluetoothDeviceClient::kJustWorksAddress);
2923 ASSERT_TRUE(device != NULL);
2924 ASSERT_FALSE(device->IsPaired());
2926 TestObserver observer(adapter_);
2928 fake_bluetooth_device_client_->SimulatePairing(
2929 dbus::ObjectPath(FakeBluetoothDeviceClient::kJustWorksPath), true,
2930 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
2931 base::Unretained(this)));
2933 message_loop_.Run();
2935 EXPECT_EQ(0, callback_count_);
2936 EXPECT_EQ(1, error_callback_count_);
2937 EXPECT_EQ(bluetooth_device::kErrorAuthenticationRejected, last_client_error_);
2939 // No changes should be observer.
2940 EXPECT_EQ(0, observer.device_changed_count_);
2942 EXPECT_FALSE(device->IsPaired());
2944 // No pairing context should remain on the device.
2945 BluetoothDeviceChromeOS* device_chromeos =
2946 static_cast<BluetoothDeviceChromeOS*>(device);
2947 EXPECT_TRUE(device_chromeos->GetPairing() == NULL);
2950 TEST_F(BluetoothChromeOSTest, RemovePairingDelegateDuringPairing) {
2951 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2953 GetAdapter();
2955 TestPairingDelegate pairing_delegate;
2956 adapter_->AddPairingDelegate(
2957 &pairing_delegate,
2958 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH);
2960 // Requests that we provide a Passkey.
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), true,
2973 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
2974 base::Unretained(this)));
2976 EXPECT_EQ(1, pairing_delegate.call_count_);
2977 EXPECT_EQ(1, pairing_delegate.request_passkey_count_);
2979 // A pairing context should now be set on the device.
2980 BluetoothDeviceChromeOS* device_chromeos =
2981 static_cast<BluetoothDeviceChromeOS*>(device);
2982 ASSERT_TRUE(device_chromeos->GetPairing() != NULL);
2984 // Removing the pairing delegate should remove that pairing context.
2985 adapter_->RemovePairingDelegate(&pairing_delegate);
2987 EXPECT_TRUE(device_chromeos->GetPairing() == NULL);
2989 // Set the Passkey, this should now have no effect since the pairing has
2990 // been, in-effect, cancelled
2991 device->SetPasskey(1234);
2993 EXPECT_EQ(0, callback_count_);
2994 EXPECT_EQ(0, error_callback_count_);
2995 EXPECT_EQ(0, observer.device_changed_count_);
2997 EXPECT_FALSE(device->IsPaired());
3000 TEST_F(BluetoothChromeOSTest, DeviceId) {
3001 GetAdapter();
3003 // Use the built-in paired device for this test, grab its Properties
3004 // structure so we can adjust the underlying modalias property.
3005 BluetoothDevice* device = adapter_->GetDevice(
3006 FakeBluetoothDeviceClient::kPairedDeviceAddress);
3007 FakeBluetoothDeviceClient::Properties* properties =
3008 fake_bluetooth_device_client_->GetProperties(
3009 dbus::ObjectPath(FakeBluetoothDeviceClient::kPairedDevicePath));
3011 ASSERT_TRUE(device != NULL);
3012 ASSERT_TRUE(properties != NULL);
3014 // Valid USB IF-assigned identifier.
3015 ASSERT_EQ("usb:v05ACp030Dd0306", properties->modalias.value());
3017 EXPECT_EQ(BluetoothDevice::VENDOR_ID_USB, device->GetVendorIDSource());
3018 EXPECT_EQ(0x05ac, device->GetVendorID());
3019 EXPECT_EQ(0x030d, device->GetProductID());
3020 EXPECT_EQ(0x0306, device->GetDeviceID());
3022 // Valid Bluetooth SIG-assigned identifier.
3023 properties->modalias.ReplaceValue("bluetooth:v00E0p2400d0400");
3025 EXPECT_EQ(BluetoothDevice::VENDOR_ID_BLUETOOTH, device->GetVendorIDSource());
3026 EXPECT_EQ(0x00e0, device->GetVendorID());
3027 EXPECT_EQ(0x2400, device->GetProductID());
3028 EXPECT_EQ(0x0400, device->GetDeviceID());
3030 // Invalid USB IF-assigned identifier.
3031 properties->modalias.ReplaceValue("usb:x00E0p2400d0400");
3033 EXPECT_EQ(BluetoothDevice::VENDOR_ID_UNKNOWN, device->GetVendorIDSource());
3034 EXPECT_EQ(0, device->GetVendorID());
3035 EXPECT_EQ(0, device->GetProductID());
3036 EXPECT_EQ(0, device->GetDeviceID());
3038 // Invalid Bluetooth SIG-assigned identifier.
3039 properties->modalias.ReplaceValue("bluetooth:x00E0p2400d0400");
3041 EXPECT_EQ(BluetoothDevice::VENDOR_ID_UNKNOWN, device->GetVendorIDSource());
3042 EXPECT_EQ(0, device->GetVendorID());
3043 EXPECT_EQ(0, device->GetProductID());
3044 EXPECT_EQ(0, device->GetDeviceID());
3046 // Unknown vendor specification identifier.
3047 properties->modalias.ReplaceValue("chrome:v00E0p2400d0400");
3049 EXPECT_EQ(BluetoothDevice::VENDOR_ID_UNKNOWN, device->GetVendorIDSource());
3050 EXPECT_EQ(0, device->GetVendorID());
3051 EXPECT_EQ(0, device->GetProductID());
3052 EXPECT_EQ(0, device->GetDeviceID());
3055 TEST_F(BluetoothChromeOSTest, GetConnectionInfoForDisconnectedDevice) {
3056 GetAdapter();
3057 BluetoothDevice* device =
3058 adapter_->GetDevice(FakeBluetoothDeviceClient::kPairedDeviceAddress);
3060 // Calling GetConnectionInfo for an unconnected device should return a result
3061 // in which all fields are filled with BluetoothDevice::kUnknownPower.
3062 BluetoothDevice::ConnectionInfo conn_info(0, 0, 0);
3063 device->GetConnectionInfo(base::Bind(&SaveConnectionInfo, &conn_info));
3064 int unknown_power = BluetoothDevice::kUnknownPower;
3065 EXPECT_NE(0, unknown_power);
3066 EXPECT_EQ(unknown_power, conn_info.rssi);
3067 EXPECT_EQ(unknown_power, conn_info.transmit_power);
3068 EXPECT_EQ(unknown_power, conn_info.max_transmit_power);
3071 TEST_F(BluetoothChromeOSTest, GetConnectionInfoForConnectedDevice) {
3072 GetAdapter();
3073 BluetoothDevice* device =
3074 adapter_->GetDevice(FakeBluetoothDeviceClient::kPairedDeviceAddress);
3076 device->Connect(NULL, GetCallback(),
3077 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
3078 base::Unretained(this)));
3079 EXPECT_TRUE(device->IsConnected());
3081 // Calling GetConnectionInfo for a connected device should return valid
3082 // results.
3083 fake_bluetooth_device_client_->UpdateConnectionInfo(-10, 3, 4);
3084 BluetoothDevice::ConnectionInfo conn_info;
3085 device->GetConnectionInfo(base::Bind(&SaveConnectionInfo, &conn_info));
3086 EXPECT_EQ(-10, conn_info.rssi);
3087 EXPECT_EQ(3, conn_info.transmit_power);
3088 EXPECT_EQ(4, conn_info.max_transmit_power);
3091 // Verifies Shutdown shuts down the adapter as expected.
3092 TEST_F(BluetoothChromeOSTest, Shutdown) {
3093 // Set up adapter. Set powered & discoverable, start discovery.
3094 GetAdapter();
3095 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
3096 adapter_->SetDiscoverable(true, GetCallback(), GetErrorCallback());
3097 adapter_->StartDiscoverySession(
3098 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
3099 base::Unretained(this)),
3100 GetErrorCallback());
3101 base::MessageLoop::current()->Run();
3102 ASSERT_EQ(3, callback_count_);
3103 ASSERT_EQ(0, error_callback_count_);
3104 callback_count_ = 0;
3106 TestPairingDelegate pairing_delegate;
3107 adapter_->AddPairingDelegate(
3108 &pairing_delegate, BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH);
3110 // Validate running adapter state.
3111 EXPECT_NE("", adapter_->GetAddress());
3112 EXPECT_NE("", adapter_->GetName());
3113 EXPECT_TRUE(adapter_->IsInitialized());
3114 EXPECT_TRUE(adapter_->IsPresent());
3115 EXPECT_TRUE(adapter_->IsPowered());
3116 EXPECT_TRUE(adapter_->IsDiscoverable());
3117 EXPECT_TRUE(adapter_->IsDiscovering());
3118 EXPECT_EQ(2U, adapter_->GetDevices().size());
3119 EXPECT_NE(nullptr, adapter_->GetDevice(
3120 FakeBluetoothDeviceClient::kPairedDeviceAddress));
3121 EXPECT_NE(dbus::ObjectPath(""), static_cast<BluetoothAdapterChromeOS*>(
3122 adapter_.get())->object_path());
3124 // Shutdown
3125 adapter_->Shutdown();
3127 // Validate post shutdown state by calling all BluetoothAdapterChromeOS
3128 // members, in declaration order:
3130 adapter_->Shutdown();
3131 // DeleteOnCorrectThread omitted as we don't want to delete in this test.
3133 TestObserver observer(adapter_); // Calls AddObserver
3134 } // TestObserver::~TestObserver calls RemoveObserver.
3135 EXPECT_EQ("", adapter_->GetAddress());
3136 EXPECT_EQ("", adapter_->GetName());
3138 adapter_->SetName("", GetCallback(), GetErrorCallback());
3139 EXPECT_EQ(0, callback_count_);
3140 EXPECT_EQ(1, error_callback_count_--) << "SetName error";
3142 EXPECT_TRUE(adapter_->IsInitialized());
3143 EXPECT_FALSE(adapter_->IsPresent());
3144 EXPECT_FALSE(adapter_->IsPowered());
3146 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
3147 EXPECT_EQ(0, callback_count_);
3148 EXPECT_EQ(1, error_callback_count_--) << "SetPowered error";
3150 EXPECT_FALSE(adapter_->IsDiscoverable());
3152 adapter_->SetDiscoverable(true, GetCallback(), GetErrorCallback());
3153 EXPECT_EQ(0, callback_count_);
3154 EXPECT_EQ(1, error_callback_count_--) << "SetDiscoverable error";
3156 EXPECT_FALSE(adapter_->IsDiscovering());
3157 // CreateRfcommService will DCHECK after Shutdown().
3158 // CreateL2capService will DCHECK after Shutdown().
3160 BluetoothAudioSink::Options audio_sink_options;
3161 adapter_->RegisterAudioSink(
3162 audio_sink_options,
3163 base::Bind(&BluetoothChromeOSTest::AudioSinkAcquiredCallback,
3164 base::Unretained(this)),
3165 base::Bind(&BluetoothChromeOSTest::AudioSinkErrorCallback,
3166 base::Unretained(this)));
3167 EXPECT_EQ(0, callback_count_);
3168 EXPECT_EQ(1, error_callback_count_--) << "RegisterAudioSink error";
3170 BluetoothAdapterChromeOS* adapter_chrome_os =
3171 static_cast<BluetoothAdapterChromeOS*>(adapter_.get());
3172 EXPECT_EQ(NULL, adapter_chrome_os->GetDeviceWithPath(dbus::ObjectPath("")));
3174 // Notify methods presume objects exist that are owned by the adapter and
3175 // destroyed in Shutdown(). Mocks are not attempted here that won't exist,
3176 // as verified below by EXPECT_EQ(0U, adapter_->GetDevices().size());
3177 // NotifyDeviceChanged
3178 // NotifyGattServiceAdded
3179 // NotifyGattServiceRemoved
3180 // NotifyGattServiceChanged
3181 // NotifyGattDiscoveryComplete
3182 // NotifyGattCharacteristicAdded
3183 // NotifyGattCharacteristicRemoved
3184 // NotifyGattDescriptorAdded
3185 // NotifyGattDescriptorRemoved
3186 // NotifyGattCharacteristicValueChanged
3187 // NotifyGattDescriptorValueChanged
3189 EXPECT_EQ(dbus::ObjectPath(""), adapter_chrome_os->object_path());
3191 FakeBluetoothProfileServiceProviderDelegate profile_delegate;
3192 adapter_chrome_os->UseProfile(
3193 BluetoothUUID(), dbus::ObjectPath(""),
3194 BluetoothProfileManagerClient::Options(), &profile_delegate,
3195 base::Bind(&BluetoothChromeOSTest::ProfileRegisteredCallback,
3196 base::Unretained(this)),
3197 base::Bind(&BluetoothChromeOSTest::ErrorCompletionCallback,
3198 base::Unretained(this)));
3199 base::MessageLoop::current()->Run();
3200 EXPECT_EQ(1, callback_count_--) << "UseProfile error";
3201 EXPECT_EQ(0, error_callback_count_) << "UseProfile error";
3203 adapter_chrome_os->ReleaseProfile(BluetoothUUID());
3205 // Protected and private methods:
3207 adapter_chrome_os->RemovePairingDelegateInternal(&pairing_delegate);
3209 // BluetoothAdapterClient::Observer methods omitted, dbus will be shutdown.
3210 // BluetoothDeviceClient::Observer methods omitted, dbus will be shutdown.
3211 // BluetoothInputClient::Observer methods omitted, dbus will be shutdown.
3212 // BluetoothAgentServiceProvider::Delegate omitted, dbus will be shutdown,
3213 // with the exception of Released.
3214 adapter_chrome_os->Released();
3216 adapter_chrome_os->OnRegisterAgent();
3217 adapter_chrome_os->OnRegisterAgentError("", "");
3218 adapter_chrome_os->OnRequestDefaultAgent();
3219 adapter_chrome_os->OnRequestDefaultAgentError("", "");
3221 adapter_chrome_os->OnRegisterAudioSink(
3222 base::Bind(&BluetoothChromeOSTest::AudioSinkAcquiredCallback,
3223 base::Unretained(this)),
3224 base::Bind(&BluetoothChromeOSTest::AudioSinkErrorCallback,
3225 base::Unretained(this)),
3226 scoped_refptr<device::BluetoothAudioSink>());
3227 EXPECT_EQ(0, callback_count_);
3228 EXPECT_EQ(1, error_callback_count_--) << "RegisterAudioSink error";
3230 // GetPairing will DCHECK after Shutdown().
3231 // SetAdapter will DCHECK after Shutdown().
3232 // SetDefaultAdapterName will DCHECK after Shutdown().
3233 // RemoveAdapter will DCHECK after Shutdown().
3234 adapter_chrome_os->PoweredChanged(false);
3235 adapter_chrome_os->DiscoverableChanged(false);
3236 adapter_chrome_os->DiscoveringChanged(false);
3237 adapter_chrome_os->PresentChanged(false);
3239 adapter_chrome_os->OnSetDiscoverable(GetCallback(), GetErrorCallback(), true);
3240 EXPECT_EQ(0, callback_count_) << "OnSetDiscoverable error";
3241 EXPECT_EQ(1, error_callback_count_--) << "OnSetDiscoverable error";
3243 adapter_chrome_os->OnPropertyChangeCompleted(GetCallback(),
3244 GetErrorCallback(), true);
3245 EXPECT_EQ(0, callback_count_) << "OnPropertyChangeCompleted error";
3246 EXPECT_EQ(1, error_callback_count_--) << "OnPropertyChangeCompleted error";
3248 adapter_chrome_os->AddDiscoverySession(GetCallback(), GetErrorCallback());
3249 EXPECT_EQ(0, callback_count_) << "AddDiscoverySession error";
3250 EXPECT_EQ(1, error_callback_count_--) << "AddDiscoverySession error";
3252 adapter_chrome_os->RemoveDiscoverySession(GetCallback(), GetErrorCallback());
3253 EXPECT_EQ(0, callback_count_) << "RemoveDiscoverySession error";
3254 EXPECT_EQ(1, error_callback_count_--) << "RemoveDiscoverySession error";
3256 // OnStartDiscovery tested in Shutdown_OnStartDiscovery
3257 // OnStartDiscoveryError tested in Shutdown_OnStartDiscoveryError
3258 // OnStopDiscovery tested in Shutdown_OnStopDiscovery
3259 // OnStopDiscoveryError tested in Shutdown_OnStopDiscoveryError
3261 // OnRegisterProfile SetProfileDelegate, OnRegisterProfileError, require
3262 // UseProfile to be set first, do so again here just before calling them.
3263 adapter_chrome_os->UseProfile(
3264 BluetoothUUID(), dbus::ObjectPath(""),
3265 BluetoothProfileManagerClient::Options(), &profile_delegate,
3266 base::Bind(&BluetoothChromeOSTest::ProfileRegisteredCallback,
3267 base::Unretained(this)),
3268 base::Bind(&BluetoothChromeOSTest::ErrorCompletionCallback,
3269 base::Unretained(this)));
3271 adapter_chrome_os->OnRegisterProfile(
3272 BluetoothUUID(), dbus::ObjectPath(""), &profile_delegate,
3273 base::Bind(&BluetoothChromeOSTest::ProfileRegisteredCallback,
3274 base::Unretained(this)),
3275 base::Bind(&BluetoothChromeOSTest::ErrorCompletionCallback,
3276 base::Unretained(this)));
3277 EXPECT_EQ(1, callback_count_--) << "OnRegisterProfile error";
3278 EXPECT_EQ(1, error_callback_count_--) << "OnRegisterProfile error";
3280 adapter_chrome_os->SetProfileDelegate(
3281 BluetoothUUID(), dbus::ObjectPath(""), &profile_delegate,
3282 base::Bind(&BluetoothChromeOSTest::ProfileRegisteredCallback,
3283 base::Unretained(this)),
3284 base::Bind(&BluetoothChromeOSTest::ErrorCompletionCallback,
3285 base::Unretained(this)));
3286 EXPECT_EQ(0, callback_count_) << "SetProfileDelegate error";
3287 EXPECT_EQ(1, error_callback_count_--) << "SetProfileDelegate error";
3289 adapter_chrome_os->OnRegisterProfileError(
3290 BluetoothUUID(),
3291 base::Bind(&BluetoothChromeOSTest::ErrorCompletionCallback,
3292 base::Unretained(this)),
3293 "", "");
3294 EXPECT_EQ(0, callback_count_) << "OnRegisterProfileError error";
3295 EXPECT_EQ(1, error_callback_count_--) << "OnRegisterProfileError error";
3297 adapter_chrome_os->ProcessQueuedDiscoveryRequests();
3299 // From BluetoothAdapater:
3301 adapter_->StartDiscoverySession(
3302 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
3303 base::Unretained(this)),
3304 GetErrorCallback());
3305 EXPECT_EQ(0, callback_count_) << "StartDiscoverySession error";
3306 EXPECT_EQ(1, error_callback_count_--) << "StartDiscoverySession error";
3308 EXPECT_EQ(0U, adapter_->GetDevices().size());
3309 EXPECT_EQ(nullptr, adapter_->GetDevice(
3310 FakeBluetoothDeviceClient::kPairedDeviceAddress));
3311 TestPairingDelegate pairing_delegate2;
3312 adapter_->AddPairingDelegate(
3313 &pairing_delegate2, BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH);
3314 adapter_->RemovePairingDelegate(&pairing_delegate2);
3317 // Verifies post-Shutdown of discovery sessions and OnStartDiscovery.
3318 TEST_F(BluetoothChromeOSTest, Shutdown_OnStartDiscovery) {
3319 const int kNumberOfDiscoverySessions = 10;
3320 GetAdapter();
3321 BluetoothAdapterChromeOS* adapter_chrome_os =
3322 static_cast<BluetoothAdapterChromeOS*>(adapter_.get());
3324 for (int i = 0; i < kNumberOfDiscoverySessions; i++) {
3325 adapter_chrome_os->AddDiscoverySession(GetCallback(), GetErrorCallback());
3327 adapter_->Shutdown();
3328 adapter_chrome_os->OnStartDiscovery(GetCallback(), GetErrorCallback());
3330 EXPECT_EQ(0, callback_count_);
3331 EXPECT_EQ(kNumberOfDiscoverySessions, error_callback_count_);
3334 // Verifies post-Shutdown of discovery sessions and OnStartDiscoveryError.
3335 TEST_F(BluetoothChromeOSTest, Shutdown_OnStartDiscoveryError) {
3336 const int kNumberOfDiscoverySessions = 10;
3337 GetAdapter();
3338 BluetoothAdapterChromeOS* adapter_chrome_os =
3339 static_cast<BluetoothAdapterChromeOS*>(adapter_.get());
3341 for (int i = 0; i < kNumberOfDiscoverySessions; i++) {
3342 adapter_chrome_os->AddDiscoverySession(GetCallback(), GetErrorCallback());
3344 adapter_->Shutdown();
3345 adapter_chrome_os->OnStartDiscoveryError(GetCallback(), GetErrorCallback(),
3346 "", "");
3348 EXPECT_EQ(0, callback_count_);
3349 EXPECT_EQ(kNumberOfDiscoverySessions, error_callback_count_);
3352 // Verifies post-Shutdown of discovery sessions and OnStartDiscovery.
3353 TEST_F(BluetoothChromeOSTest, Shutdown_OnStopDiscovery) {
3354 const int kNumberOfDiscoverySessions = 10;
3355 GetAdapter();
3356 BluetoothAdapterChromeOS* adapter_chrome_os =
3357 static_cast<BluetoothAdapterChromeOS*>(adapter_.get());
3359 // In order to queue up discovery sessions before an OnStopDiscovery call
3360 // RemoveDiscoverySession must be called, so Add, Start, and Remove:
3361 adapter_chrome_os->AddDiscoverySession(GetCallback(), GetErrorCallback());
3362 adapter_chrome_os->OnStartDiscovery(GetCallback(), GetErrorCallback());
3363 adapter_chrome_os->RemoveDiscoverySession(GetCallback(), GetErrorCallback());
3364 callback_count_ = 0;
3365 error_callback_count_ = 0;
3366 // Can now queue discovery sessions while waiting for OnStopDiscovery.
3367 for (int i = 0; i < kNumberOfDiscoverySessions; i++) {
3368 adapter_chrome_os->AddDiscoverySession(GetCallback(), GetErrorCallback());
3370 adapter_->Shutdown();
3371 adapter_chrome_os->OnStopDiscovery(GetCallback());
3373 // 1 successful stopped discovery from RemoveDiscoverySession, and
3374 // kNumberOfDiscoverySessions errors from AddDiscoverySession/OnStopDiscovery.
3375 EXPECT_EQ(1, callback_count_);
3376 EXPECT_EQ(kNumberOfDiscoverySessions, error_callback_count_);
3379 // Verifies post-Shutdown of discovery sessions and OnStopDiscoveryError.
3380 TEST_F(BluetoothChromeOSTest, Shutdown_OnStopDiscoveryError) {
3381 const int kNumberOfDiscoverySessions = 10;
3382 GetAdapter();
3383 BluetoothAdapterChromeOS* adapter_chrome_os =
3384 static_cast<BluetoothAdapterChromeOS*>(adapter_.get());
3386 // In order to queue up discovery sessions before an OnStopDiscoveryError call
3387 // RemoveDiscoverySession must be called, so Add, Start, and Remove:
3388 adapter_chrome_os->AddDiscoverySession(GetCallback(), GetErrorCallback());
3389 adapter_chrome_os->OnStartDiscovery(GetCallback(), GetErrorCallback());
3390 adapter_chrome_os->RemoveDiscoverySession(GetCallback(), GetErrorCallback());
3391 callback_count_ = 0;
3392 error_callback_count_ = 0;
3393 // Can now queue discovery sessions while waiting for OnStopDiscoveryError.
3394 for (int i = 0; i < kNumberOfDiscoverySessions; i++) {
3395 adapter_chrome_os->AddDiscoverySession(GetCallback(), GetErrorCallback());
3397 adapter_->Shutdown();
3398 adapter_chrome_os->OnStopDiscoveryError(GetErrorCallback(), "", "");
3400 // 1 error reported to RemoveDiscoverySession because of OnStopDiscoveryError,
3401 // and kNumberOfDiscoverySessions errors queued with AddDiscoverySession.
3402 EXPECT_EQ(0, callback_count_);
3403 EXPECT_EQ(1 + kNumberOfDiscoverySessions, error_callback_count_);
3406 } // namespace chromeos