Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(282)

Side by Side Diff: device/bluetooth/bluetooth_chromeos_unittest.cc

Issue 1415573014: Reland "Add Linux support for the Bluetooth API" (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: build fix. Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
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 "dbus/object_path.h"
10 #include "device/bluetooth/bluetooth_adapter.h"
11 #include "device/bluetooth/bluetooth_adapter_chromeos.h"
12 #include "device/bluetooth/bluetooth_adapter_factory.h"
13 #include "device/bluetooth/bluetooth_device.h"
14 #include "device/bluetooth/bluetooth_device_chromeos.h"
15 #include "device/bluetooth/bluetooth_discovery_session.h"
16 #include "device/bluetooth/bluetooth_pairing_chromeos.h"
17 #include "device/bluetooth/dbus/bluez_dbus_manager.h"
18 #include "device/bluetooth/dbus/fake_bluetooth_adapter_client.h"
19 #include "device/bluetooth/dbus/fake_bluetooth_agent_manager_client.h"
20 #include "device/bluetooth/dbus/fake_bluetooth_device_client.h"
21 #include "device/bluetooth/dbus/fake_bluetooth_gatt_service_client.h"
22 #include "device/bluetooth/dbus/fake_bluetooth_input_client.h"
23 #include "device/bluetooth/test/test_bluetooth_adapter_observer.h"
24 #include "testing/gtest/include/gtest/gtest.h"
25 #include "third_party/cros_system_api/dbus/service_constants.h"
26
27 using device::BluetoothAdapter;
28 using device::BluetoothAdapterFactory;
29 using device::BluetoothAudioSink;
30 using device::BluetoothDevice;
31 using device::BluetoothDiscoveryFilter;
32 using device::BluetoothDiscoverySession;
33 using device::BluetoothUUID;
34 using device::TestBluetoothAdapterObserver;
35
36 namespace chromeos {
37
38 namespace {
39
40 // Callback for BluetoothDevice::GetConnectionInfo() that simply saves the
41 // connection info to the bound argument.
42 void SaveConnectionInfo(BluetoothDevice::ConnectionInfo* out,
43 const BluetoothDevice::ConnectionInfo& conn_info) {
44 *out = conn_info;
45 };
46
47 // Find |address| in |devices|, if found returns the index otherwise returns -1.
48 int GetDeviceIndexByAddress(BluetoothAdapter::DeviceList& devices,
49 const char* address) {
50 int idx = -1;
51 for (auto& device : devices) {
52 ++idx;
53 if (device->GetAddress().compare(address) == 0)
54 return idx;
55 }
56 return -1;
57 }
58
59 class FakeBluetoothProfileServiceProviderDelegate
60 : public bluez::BluetoothProfileServiceProvider::Delegate {
61 public:
62 FakeBluetoothProfileServiceProviderDelegate() {}
63
64 // bluez::BluetoothProfileServiceProvider::Delegate:
65 void Released() override {}
66
67 void NewConnection(
68 const dbus::ObjectPath&,
69 scoped_ptr<dbus::FileDescriptor>,
70 const bluez::BluetoothProfileServiceProvider::Delegate::Options&,
71 const ConfirmationCallback&) override {}
72
73 void RequestDisconnection(const dbus::ObjectPath&,
74 const ConfirmationCallback&) override {}
75
76 void Cancel() override {}
77 };
78
79 } // namespace
80
81 class TestPairingDelegate : public BluetoothDevice::PairingDelegate {
82 public:
83 TestPairingDelegate()
84 : call_count_(0),
85 request_pincode_count_(0),
86 request_passkey_count_(0),
87 display_pincode_count_(0),
88 display_passkey_count_(0),
89 keys_entered_count_(0),
90 confirm_passkey_count_(0),
91 authorize_pairing_count_(0),
92 last_passkey_(9999999U),
93 last_entered_(999U) {}
94 ~TestPairingDelegate() override {}
95
96 void RequestPinCode(BluetoothDevice* device) override {
97 ++call_count_;
98 ++request_pincode_count_;
99 QuitMessageLoop();
100 }
101
102 void RequestPasskey(BluetoothDevice* device) override {
103 ++call_count_;
104 ++request_passkey_count_;
105 QuitMessageLoop();
106 }
107
108 void DisplayPinCode(BluetoothDevice* device,
109 const std::string& pincode) override {
110 ++call_count_;
111 ++display_pincode_count_;
112 last_pincode_ = pincode;
113 QuitMessageLoop();
114 }
115
116 void DisplayPasskey(BluetoothDevice* device, uint32 passkey) override {
117 ++call_count_;
118 ++display_passkey_count_;
119 last_passkey_ = passkey;
120 QuitMessageLoop();
121 }
122
123 void KeysEntered(BluetoothDevice* device, uint32 entered) override {
124 ++call_count_;
125 ++keys_entered_count_;
126 last_entered_ = entered;
127 QuitMessageLoop();
128 }
129
130 void ConfirmPasskey(BluetoothDevice* device, uint32 passkey) override {
131 ++call_count_;
132 ++confirm_passkey_count_;
133 last_passkey_ = passkey;
134 QuitMessageLoop();
135 }
136
137 void AuthorizePairing(BluetoothDevice* device) override {
138 ++call_count_;
139 ++authorize_pairing_count_;
140 QuitMessageLoop();
141 }
142
143 int call_count_;
144 int request_pincode_count_;
145 int request_passkey_count_;
146 int display_pincode_count_;
147 int display_passkey_count_;
148 int keys_entered_count_;
149 int confirm_passkey_count_;
150 int authorize_pairing_count_;
151 uint32 last_passkey_;
152 uint32 last_entered_;
153 std::string last_pincode_;
154
155 private:
156 // Some tests use a message loop since background processing is simulated;
157 // break out of those loops.
158 void QuitMessageLoop() {
159 if (base::MessageLoop::current() &&
160 base::MessageLoop::current()->is_running()) {
161 base::MessageLoop::current()->QuitWhenIdle();
162 }
163 }
164 };
165
166 class BluetoothChromeOSTest : public testing::Test {
167 public:
168 void SetUp() override {
169 scoped_ptr<bluez::BluezDBusManagerSetter> dbus_setter =
170 bluez::BluezDBusManager::GetSetterForTesting();
171 // We need to initialize BluezDBusManager early to prevent
172 // Bluetooth*::Create() methods from picking the real instead of fake
173 // implementations.
174 fake_bluetooth_adapter_client_ = new bluez::FakeBluetoothAdapterClient;
175 dbus_setter->SetBluetoothAdapterClient(
176 scoped_ptr<bluez::BluetoothAdapterClient>(
177 fake_bluetooth_adapter_client_));
178 fake_bluetooth_device_client_ = new bluez::FakeBluetoothDeviceClient;
179 dbus_setter->SetBluetoothDeviceClient(
180 scoped_ptr<bluez::BluetoothDeviceClient>(
181 fake_bluetooth_device_client_));
182 dbus_setter->SetBluetoothInputClient(
183 scoped_ptr<bluez::BluetoothInputClient>(
184 new bluez::FakeBluetoothInputClient));
185 dbus_setter->SetBluetoothAgentManagerClient(
186 scoped_ptr<bluez::BluetoothAgentManagerClient>(
187 new bluez::FakeBluetoothAgentManagerClient));
188 dbus_setter->SetBluetoothGattServiceClient(
189 scoped_ptr<bluez::BluetoothGattServiceClient>(
190 new bluez::FakeBluetoothGattServiceClient));
191
192 fake_bluetooth_adapter_client_->SetSimulationIntervalMs(10);
193
194 callback_count_ = 0;
195 error_callback_count_ = 0;
196 last_connect_error_ = BluetoothDevice::ERROR_UNKNOWN;
197 last_client_error_ = "";
198 }
199
200 void TearDown() override {
201 for (ScopedVector<BluetoothDiscoverySession>::iterator iter =
202 discovery_sessions_.begin();
203 iter != discovery_sessions_.end();
204 ++iter) {
205 BluetoothDiscoverySession* session = *iter;
206 if (!session->IsActive())
207 continue;
208 callback_count_ = 0;
209 session->Stop(GetCallback(), GetErrorCallback());
210 message_loop_.Run();
211 ASSERT_EQ(1, callback_count_);
212 }
213 discovery_sessions_.clear();
214 adapter_ = nullptr;
215 bluez::BluezDBusManager::Shutdown();
216 }
217
218 // Generic callbacks
219 void Callback() {
220 ++callback_count_;
221 QuitMessageLoop();
222 }
223
224 base::Closure GetCallback() {
225 return base::Bind(&BluetoothChromeOSTest::Callback, base::Unretained(this));
226 }
227
228 void DiscoverySessionCallback(
229 scoped_ptr<BluetoothDiscoverySession> discovery_session) {
230 ++callback_count_;
231 discovery_sessions_.push_back(discovery_session.release());
232 QuitMessageLoop();
233 }
234
235 void AudioSinkAcquiredCallback(scoped_refptr<BluetoothAudioSink>) {
236 ++callback_count_;
237 QuitMessageLoop();
238 }
239
240 void ProfileRegisteredCallback(BluetoothAdapterProfileChromeOS* profile) {
241 adapter_profile_ = profile;
242 ++callback_count_;
243 QuitMessageLoop();
244 }
245
246 void ErrorCallback() {
247 ++error_callback_count_;
248 QuitMessageLoop();
249 }
250
251 void DiscoveryErrorCallback(device::UMABluetoothDiscoverySessionOutcome) {
252 ErrorCallback();
253 }
254
255 base::Closure GetErrorCallback() {
256 return base::Bind(&BluetoothChromeOSTest::ErrorCallback,
257 base::Unretained(this));
258 }
259
260 base::Callback<void(device::UMABluetoothDiscoverySessionOutcome)>
261 GetDiscoveryErrorCallback() {
262 return base::Bind(&BluetoothChromeOSTest::DiscoveryErrorCallback,
263 base::Unretained(this));
264 }
265
266 void DBusErrorCallback(const std::string& error_name,
267 const std::string& error_message) {
268 ++error_callback_count_;
269 last_client_error_ = error_name;
270 QuitMessageLoop();
271 }
272
273 void ConnectErrorCallback(BluetoothDevice::ConnectErrorCode error) {
274 ++error_callback_count_;
275 last_connect_error_ = error;
276 }
277
278 void AudioSinkErrorCallback(BluetoothAudioSink::ErrorCode) {
279 ++error_callback_count_;
280 QuitMessageLoop();
281 }
282
283 void ErrorCompletionCallback(const std::string& error_message) {
284 ++error_callback_count_;
285 QuitMessageLoop();
286 }
287
288 // Call to fill the adapter_ member with a BluetoothAdapter instance.
289 void GetAdapter() {
290 adapter_ = new BluetoothAdapterChromeOS();
291 ASSERT_TRUE(adapter_.get() != nullptr);
292 ASSERT_TRUE(adapter_->IsInitialized());
293 }
294
295 // Run a discovery phase until the named device is detected, or if the named
296 // device is not created, the discovery process ends without finding it.
297 //
298 // The correct behavior of discovery is tested by the "Discovery" test case
299 // without using this function.
300 void DiscoverDevice(const std::string& address) {
301 ASSERT_TRUE(adapter_.get() != nullptr);
302 ASSERT_TRUE(base::MessageLoop::current() != nullptr);
303 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
304
305 TestBluetoothAdapterObserver observer(adapter_);
306
307 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
308 adapter_->StartDiscoverySession(
309 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
310 base::Unretained(this)),
311 GetErrorCallback());
312 base::MessageLoop::current()->Run();
313 ASSERT_EQ(2, callback_count_);
314 ASSERT_EQ(0, error_callback_count_);
315 ASSERT_EQ((size_t)1, discovery_sessions_.size());
316 ASSERT_TRUE(discovery_sessions_[0]->IsActive());
317 callback_count_ = 0;
318
319 ASSERT_TRUE(adapter_->IsPowered());
320 ASSERT_TRUE(adapter_->IsDiscovering());
321
322 while (!observer.device_removed_count() &&
323 observer.last_device_address() != address)
324 base::MessageLoop::current()->Run();
325
326 discovery_sessions_[0]->Stop(GetCallback(), GetErrorCallback());
327 base::MessageLoop::current()->Run();
328 ASSERT_EQ(1, callback_count_);
329 ASSERT_EQ(0, error_callback_count_);
330 callback_count_ = 0;
331
332 ASSERT_FALSE(adapter_->IsDiscovering());
333 }
334
335 // Run a discovery phase so we have devices that can be paired with.
336 void DiscoverDevices() {
337 // Pass an invalid address for the device so that the discovery process
338 // completes with all devices.
339 DiscoverDevice("does not exist");
340 }
341
342 protected:
343 base::MessageLoop message_loop_;
344 bluez::FakeBluetoothAdapterClient* fake_bluetooth_adapter_client_;
345 bluez::FakeBluetoothDeviceClient* fake_bluetooth_device_client_;
346 scoped_refptr<BluetoothAdapter> adapter_;
347
348 int callback_count_;
349 int error_callback_count_;
350 enum BluetoothDevice::ConnectErrorCode last_connect_error_;
351 std::string last_client_error_;
352 ScopedVector<BluetoothDiscoverySession> discovery_sessions_;
353 BluetoothAdapterProfileChromeOS* adapter_profile_;
354
355 private:
356 // Some tests use a message loop since background processing is simulated;
357 // break out of those loops.
358 void QuitMessageLoop() {
359 if (base::MessageLoop::current() &&
360 base::MessageLoop::current()->is_running()) {
361 base::MessageLoop::current()->QuitWhenIdle();
362 }
363 }
364 };
365
366 TEST_F(BluetoothChromeOSTest, AlreadyPresent) {
367 GetAdapter();
368
369 // This verifies that the class gets the list of adapters when created;
370 // and initializes with an existing adapter if there is one.
371 EXPECT_TRUE(adapter_->IsPresent());
372 EXPECT_FALSE(adapter_->IsPowered());
373 EXPECT_EQ(bluez::FakeBluetoothAdapterClient::kAdapterAddress,
374 adapter_->GetAddress());
375 EXPECT_FALSE(adapter_->IsDiscovering());
376
377 // There should be 2 devices
378 BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
379 EXPECT_EQ(2U, devices.size());
380
381 // |devices| are not ordered, verify it contains the 2 device addresses.
382 EXPECT_NE(
383 -1, GetDeviceIndexByAddress(
384 devices, bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress));
385 EXPECT_NE(
386 -1,
387 GetDeviceIndexByAddress(
388 devices,
389 bluez::FakeBluetoothDeviceClient::kPairedUnconnectableDeviceAddress));
390 }
391
392 TEST_F(BluetoothChromeOSTest, BecomePresent) {
393 fake_bluetooth_adapter_client_->SetVisible(false);
394 GetAdapter();
395 ASSERT_FALSE(adapter_->IsPresent());
396
397 // Install an observer; expect the AdapterPresentChanged to be called
398 // with true, and IsPresent() to return true.
399 TestBluetoothAdapterObserver observer(adapter_);
400
401 fake_bluetooth_adapter_client_->SetVisible(true);
402
403 EXPECT_EQ(1, observer.present_changed_count());
404 EXPECT_TRUE(observer.last_present());
405
406 EXPECT_TRUE(adapter_->IsPresent());
407
408 // We should have had a device announced.
409 EXPECT_EQ(2, observer.device_added_count());
410 EXPECT_EQ(bluez::FakeBluetoothDeviceClient::kPairedUnconnectableDeviceAddress,
411 observer.last_device_address());
412
413 // Other callbacks shouldn't be called if the values are false.
414 EXPECT_EQ(0, observer.powered_changed_count());
415 EXPECT_EQ(0, observer.discovering_changed_count());
416 EXPECT_FALSE(adapter_->IsPowered());
417 EXPECT_FALSE(adapter_->IsDiscovering());
418 }
419
420 TEST_F(BluetoothChromeOSTest, BecomeNotPresent) {
421 GetAdapter();
422 ASSERT_TRUE(adapter_->IsPresent());
423
424 // Install an observer; expect the AdapterPresentChanged to be called
425 // with false, and IsPresent() to return false.
426 TestBluetoothAdapterObserver observer(adapter_);
427
428 fake_bluetooth_adapter_client_->SetVisible(false);
429
430 EXPECT_EQ(1, observer.present_changed_count());
431 EXPECT_FALSE(observer.last_present());
432
433 EXPECT_FALSE(adapter_->IsPresent());
434
435 // We should have had 2 devices removed.
436 EXPECT_EQ(2, observer.device_removed_count());
437 // 2 possibilities for the last device here.
438 std::string address = observer.last_device_address();
439 EXPECT_TRUE(address.compare(bluez::FakeBluetoothDeviceClient::
440 kPairedUnconnectableDeviceAddress) == 0 ||
441 address.compare(
442 bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress) == 0);
443
444 // Other callbacks shouldn't be called since the values are false.
445 EXPECT_EQ(0, observer.powered_changed_count());
446 EXPECT_EQ(0, observer.discovering_changed_count());
447 EXPECT_FALSE(adapter_->IsPowered());
448 EXPECT_FALSE(adapter_->IsDiscovering());
449 }
450
451 TEST_F(BluetoothChromeOSTest, SecondAdapter) {
452 GetAdapter();
453 ASSERT_TRUE(adapter_->IsPresent());
454
455 // Install an observer, then add a second adapter. Nothing should change,
456 // we ignore the second adapter.
457 TestBluetoothAdapterObserver observer(adapter_);
458
459 fake_bluetooth_adapter_client_->SetSecondVisible(true);
460
461 EXPECT_EQ(0, observer.present_changed_count());
462
463 EXPECT_TRUE(adapter_->IsPresent());
464 EXPECT_EQ(bluez::FakeBluetoothAdapterClient::kAdapterAddress,
465 adapter_->GetAddress());
466
467 // Try removing the first adapter, we should now act as if the adapter
468 // is no longer present rather than fall back to the second.
469 fake_bluetooth_adapter_client_->SetVisible(false);
470
471 EXPECT_EQ(1, observer.present_changed_count());
472 EXPECT_FALSE(observer.last_present());
473
474 EXPECT_FALSE(adapter_->IsPresent());
475
476 // We should have had 2 devices removed.
477 EXPECT_EQ(2, observer.device_removed_count());
478 // As BluetoothAdapter devices removal does not keep the order of adding them,
479 // 2 possibilities for the last device here.
480 std::string address = observer.last_device_address();
481 EXPECT_TRUE(address.compare(bluez::FakeBluetoothDeviceClient::
482 kPairedUnconnectableDeviceAddress) == 0 ||
483 address.compare(
484 bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress) == 0);
485
486 // Other callbacks shouldn't be called since the values are false.
487 EXPECT_EQ(0, observer.powered_changed_count());
488 EXPECT_EQ(0, observer.discovering_changed_count());
489 EXPECT_FALSE(adapter_->IsPowered());
490 EXPECT_FALSE(adapter_->IsDiscovering());
491
492 observer.Reset();
493
494 // Removing the second adapter shouldn't set anything either.
495 fake_bluetooth_adapter_client_->SetSecondVisible(false);
496
497 EXPECT_EQ(0, observer.device_removed_count());
498 EXPECT_EQ(0, observer.powered_changed_count());
499 EXPECT_EQ(0, observer.discovering_changed_count());
500 }
501
502 TEST_F(BluetoothChromeOSTest, BecomePowered) {
503 GetAdapter();
504 ASSERT_FALSE(adapter_->IsPowered());
505
506 // Install an observer; expect the AdapterPoweredChanged to be called
507 // with true, and IsPowered() to return true.
508 TestBluetoothAdapterObserver observer(adapter_);
509
510 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
511 EXPECT_EQ(1, callback_count_);
512 EXPECT_EQ(0, error_callback_count_);
513
514 EXPECT_EQ(1, observer.powered_changed_count());
515 EXPECT_TRUE(observer.last_powered());
516
517 EXPECT_TRUE(adapter_->IsPowered());
518 }
519
520 TEST_F(BluetoothChromeOSTest, BecomeNotPowered) {
521 GetAdapter();
522 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
523 EXPECT_EQ(1, callback_count_);
524 EXPECT_EQ(0, error_callback_count_);
525 callback_count_ = 0;
526
527 ASSERT_TRUE(adapter_->IsPowered());
528
529 // Install an observer; expect the AdapterPoweredChanged to be called
530 // with false, and IsPowered() to return false.
531 TestBluetoothAdapterObserver observer(adapter_);
532
533 adapter_->SetPowered(false, GetCallback(), GetErrorCallback());
534 EXPECT_EQ(1, callback_count_);
535 EXPECT_EQ(0, error_callback_count_);
536
537 EXPECT_EQ(1, observer.powered_changed_count());
538 EXPECT_FALSE(observer.last_powered());
539
540 EXPECT_FALSE(adapter_->IsPowered());
541 }
542
543 TEST_F(BluetoothChromeOSTest, SetPoweredWhenNotPresent) {
544 GetAdapter();
545 ASSERT_TRUE(adapter_->IsPresent());
546
547 // Install an observer; expect the AdapterPresentChanged to be called
548 // with false, and IsPresent() to return false.
549 TestBluetoothAdapterObserver observer(adapter_);
550
551 fake_bluetooth_adapter_client_->SetVisible(false);
552
553 EXPECT_EQ(1, observer.present_changed_count());
554 EXPECT_FALSE(observer.last_present());
555
556 EXPECT_FALSE(adapter_->IsPresent());
557 EXPECT_FALSE(adapter_->IsPowered());
558
559 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
560 EXPECT_EQ(0, callback_count_);
561 EXPECT_EQ(1, error_callback_count_);
562
563 EXPECT_EQ(0, observer.powered_changed_count());
564 EXPECT_FALSE(observer.last_powered());
565
566 EXPECT_FALSE(adapter_->IsPowered());
567 }
568
569 TEST_F(BluetoothChromeOSTest, ChangeAdapterName) {
570 GetAdapter();
571
572 static const std::string new_name(".__.");
573
574 adapter_->SetName(new_name, GetCallback(), GetErrorCallback());
575 EXPECT_EQ(1, callback_count_);
576 EXPECT_EQ(0, error_callback_count_);
577
578 EXPECT_EQ(new_name, adapter_->GetName());
579 }
580
581 TEST_F(BluetoothChromeOSTest, ChangeAdapterNameWhenNotPresent) {
582 GetAdapter();
583 ASSERT_TRUE(adapter_->IsPresent());
584
585 // Install an observer; expect the AdapterPresentChanged to be called
586 // with false, and IsPresent() to return false.
587 TestBluetoothAdapterObserver observer(adapter_);
588
589 fake_bluetooth_adapter_client_->SetVisible(false);
590
591 EXPECT_EQ(1, observer.present_changed_count());
592 EXPECT_FALSE(observer.last_present());
593
594 EXPECT_FALSE(adapter_->IsPresent());
595 EXPECT_FALSE(adapter_->IsPowered());
596
597 adapter_->SetName("^o^", GetCallback(), GetErrorCallback());
598 EXPECT_EQ(0, callback_count_);
599 EXPECT_EQ(1, error_callback_count_);
600
601 EXPECT_EQ("", adapter_->GetName());
602 }
603
604 TEST_F(BluetoothChromeOSTest, BecomeDiscoverable) {
605 GetAdapter();
606 ASSERT_FALSE(adapter_->IsDiscoverable());
607
608 // Install an observer; expect the AdapterDiscoverableChanged to be called
609 // with true, and IsDiscoverable() to return true.
610 TestBluetoothAdapterObserver observer(adapter_);
611
612 adapter_->SetDiscoverable(true, GetCallback(), GetErrorCallback());
613 EXPECT_EQ(1, callback_count_);
614 EXPECT_EQ(0, error_callback_count_);
615
616 EXPECT_EQ(1, observer.discoverable_changed_count());
617
618 EXPECT_TRUE(adapter_->IsDiscoverable());
619 }
620
621 TEST_F(BluetoothChromeOSTest, BecomeNotDiscoverable) {
622 GetAdapter();
623 adapter_->SetDiscoverable(true, GetCallback(), GetErrorCallback());
624 EXPECT_EQ(1, callback_count_);
625 EXPECT_EQ(0, error_callback_count_);
626 callback_count_ = 0;
627
628 ASSERT_TRUE(adapter_->IsDiscoverable());
629
630 // Install an observer; expect the AdapterDiscoverableChanged to be called
631 // with false, and IsDiscoverable() to return false.
632 TestBluetoothAdapterObserver observer(adapter_);
633
634 adapter_->SetDiscoverable(false, GetCallback(), GetErrorCallback());
635 EXPECT_EQ(1, callback_count_);
636 EXPECT_EQ(0, error_callback_count_);
637
638 EXPECT_EQ(1, observer.discoverable_changed_count());
639
640 EXPECT_FALSE(adapter_->IsDiscoverable());
641 }
642
643 TEST_F(BluetoothChromeOSTest, SetDiscoverableWhenNotPresent) {
644 GetAdapter();
645 ASSERT_TRUE(adapter_->IsPresent());
646 ASSERT_FALSE(adapter_->IsDiscoverable());
647
648 // Install an observer; expect the AdapterDiscoverableChanged to be called
649 // with true, and IsDiscoverable() to return true.
650 TestBluetoothAdapterObserver observer(adapter_);
651
652 fake_bluetooth_adapter_client_->SetVisible(false);
653
654 EXPECT_EQ(1, observer.present_changed_count());
655 EXPECT_FALSE(observer.last_present());
656
657 EXPECT_FALSE(adapter_->IsPresent());
658 EXPECT_FALSE(adapter_->IsDiscoverable());
659
660 adapter_->SetDiscoverable(true, GetCallback(), GetErrorCallback());
661 EXPECT_EQ(0, callback_count_);
662 EXPECT_EQ(1, error_callback_count_);
663
664 EXPECT_EQ(0, observer.discoverable_changed_count());
665
666 EXPECT_FALSE(adapter_->IsDiscoverable());
667 }
668
669 TEST_F(BluetoothChromeOSTest, StopDiscovery) {
670 GetAdapter();
671
672 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
673 adapter_->StartDiscoverySession(
674 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
675 base::Unretained(this)),
676 GetErrorCallback());
677 message_loop_.Run();
678 EXPECT_EQ(2, callback_count_);
679 EXPECT_EQ(0, error_callback_count_);
680 callback_count_ = 0;
681
682 ASSERT_TRUE(adapter_->IsPowered());
683 ASSERT_TRUE(adapter_->IsDiscovering());
684 ASSERT_EQ((size_t)1, discovery_sessions_.size());
685 ASSERT_TRUE(discovery_sessions_[0]->IsActive());
686
687 // Install an observer; aside from the callback, expect the
688 // AdapterDiscoveringChanged method to be called and no longer to be
689 // discovering,
690 TestBluetoothAdapterObserver observer(adapter_);
691
692 discovery_sessions_[0]->Stop(GetCallback(), GetErrorCallback());
693 message_loop_.Run();
694 EXPECT_EQ(1, callback_count_);
695 EXPECT_EQ(0, error_callback_count_);
696
697 EXPECT_EQ(1, observer.discovering_changed_count());
698 EXPECT_FALSE(observer.last_discovering());
699
700 EXPECT_FALSE(adapter_->IsDiscovering());
701 discovery_sessions_.clear();
702 callback_count_ = 0;
703
704 // Test that the Stop callbacks get called even if the
705 // BluetoothDiscoverySession objects gets deleted
706 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
707 adapter_->StartDiscoverySession(
708 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
709 base::Unretained(this)),
710 GetErrorCallback());
711 message_loop_.Run();
712 EXPECT_EQ(2, callback_count_);
713 EXPECT_EQ(0, error_callback_count_);
714 callback_count_ = 0;
715 ASSERT_TRUE(adapter_->IsPowered());
716 ASSERT_TRUE(adapter_->IsDiscovering());
717 ASSERT_EQ((size_t)1, discovery_sessions_.size());
718 ASSERT_TRUE(discovery_sessions_[0]->IsActive());
719
720 discovery_sessions_[0]->Stop(GetCallback(), GetErrorCallback());
721 discovery_sessions_.clear();
722
723 message_loop_.Run();
724 EXPECT_EQ(1, callback_count_);
725 EXPECT_EQ(0, error_callback_count_);
726 }
727
728 TEST_F(BluetoothChromeOSTest, Discovery) {
729 // Test a simulated discovery session.
730 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
731 GetAdapter();
732
733 TestBluetoothAdapterObserver observer(adapter_);
734
735 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
736 adapter_->StartDiscoverySession(
737 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
738 base::Unretained(this)),
739 GetErrorCallback());
740 message_loop_.Run();
741 EXPECT_EQ(2, callback_count_);
742 EXPECT_EQ(0, error_callback_count_);
743 callback_count_ = 0;
744
745 ASSERT_TRUE(adapter_->IsPowered());
746 ASSERT_TRUE(adapter_->IsDiscovering());
747 ASSERT_EQ((size_t)1, discovery_sessions_.size());
748 ASSERT_TRUE(discovery_sessions_[0]->IsActive());
749
750 // First two devices to appear.
751 message_loop_.Run();
752
753 EXPECT_EQ(2, observer.device_added_count());
754 EXPECT_EQ(bluez::FakeBluetoothDeviceClient::kLowEnergyAddress,
755 observer.last_device_address());
756
757 // Next we should get another two devices...
758 message_loop_.Run();
759 EXPECT_EQ(4, observer.device_added_count());
760
761 // Okay, let's run forward until a device is actually removed...
762 while (!observer.device_removed_count())
763 message_loop_.Run();
764
765 EXPECT_EQ(1, observer.device_removed_count());
766 EXPECT_EQ(bluez::FakeBluetoothDeviceClient::kVanishingDeviceAddress,
767 observer.last_device_address());
768 }
769
770 TEST_F(BluetoothChromeOSTest, PoweredAndDiscovering) {
771 GetAdapter();
772 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
773 adapter_->StartDiscoverySession(
774 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
775 base::Unretained(this)),
776 GetErrorCallback());
777 message_loop_.Run();
778 EXPECT_EQ(2, callback_count_);
779 EXPECT_EQ(0, error_callback_count_);
780 callback_count_ = 0;
781 ASSERT_EQ((size_t)1, discovery_sessions_.size());
782 ASSERT_TRUE(discovery_sessions_[0]->IsActive());
783
784 // Stop the timers that the simulation uses
785 fake_bluetooth_device_client_->EndDiscoverySimulation(
786 dbus::ObjectPath(bluez::FakeBluetoothAdapterClient::kAdapterPath));
787
788 ASSERT_TRUE(adapter_->IsPowered());
789 ASSERT_TRUE(adapter_->IsDiscovering());
790
791 fake_bluetooth_adapter_client_->SetVisible(false);
792 ASSERT_FALSE(adapter_->IsPresent());
793 ASSERT_FALSE(discovery_sessions_[0]->IsActive());
794
795 // Install an observer; expect the AdapterPresentChanged,
796 // AdapterPoweredChanged and AdapterDiscoveringChanged methods to be called
797 // with true, and IsPresent(), IsPowered() and IsDiscovering() to all
798 // return true.
799 TestBluetoothAdapterObserver observer(adapter_);
800
801 fake_bluetooth_adapter_client_->SetVisible(true);
802
803 EXPECT_EQ(1, observer.present_changed_count());
804 EXPECT_TRUE(observer.last_present());
805 EXPECT_TRUE(adapter_->IsPresent());
806
807 EXPECT_EQ(1, observer.powered_changed_count());
808 EXPECT_TRUE(observer.last_powered());
809 EXPECT_TRUE(adapter_->IsPowered());
810
811 EXPECT_EQ(1, observer.discovering_changed_count());
812 EXPECT_TRUE(observer.last_discovering());
813 EXPECT_TRUE(adapter_->IsDiscovering());
814
815 observer.Reset();
816
817 // Now mark the adapter not present again. Expect the methods to be called
818 // again, to reset the properties back to false
819 fake_bluetooth_adapter_client_->SetVisible(false);
820
821 EXPECT_EQ(1, observer.present_changed_count());
822 EXPECT_FALSE(observer.last_present());
823 EXPECT_FALSE(adapter_->IsPresent());
824
825 EXPECT_EQ(1, observer.powered_changed_count());
826 EXPECT_FALSE(observer.last_powered());
827 EXPECT_FALSE(adapter_->IsPowered());
828
829 EXPECT_EQ(1, observer.discovering_changed_count());
830 EXPECT_FALSE(observer.last_discovering());
831 EXPECT_FALSE(adapter_->IsDiscovering());
832 }
833
834 // This unit test asserts that the basic reference counting logic works
835 // correctly for discovery requests done via the BluetoothAdapter.
836 TEST_F(BluetoothChromeOSTest, MultipleDiscoverySessions) {
837 GetAdapter();
838 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
839 EXPECT_EQ(1, callback_count_);
840 EXPECT_EQ(0, error_callback_count_);
841 EXPECT_TRUE(adapter_->IsPowered());
842 callback_count_ = 0;
843
844 TestBluetoothAdapterObserver observer(adapter_);
845
846 EXPECT_EQ(0, observer.discovering_changed_count());
847 EXPECT_FALSE(observer.last_discovering());
848 EXPECT_FALSE(adapter_->IsDiscovering());
849
850 // Request device discovery 3 times.
851 for (int i = 0; i < 3; i++) {
852 adapter_->StartDiscoverySession(
853 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
854 base::Unretained(this)),
855 GetErrorCallback());
856 }
857 // Run only once, as there should have been one D-Bus call.
858 message_loop_.Run();
859
860 // The observer should have received the discovering changed event exactly
861 // once, the success callback should have been called 3 times and the adapter
862 // should be discovering.
863 EXPECT_EQ(1, observer.discovering_changed_count());
864 EXPECT_EQ(3, callback_count_);
865 EXPECT_EQ(0, error_callback_count_);
866 EXPECT_TRUE(observer.last_discovering());
867 EXPECT_TRUE(adapter_->IsDiscovering());
868 ASSERT_EQ((size_t)3, discovery_sessions_.size());
869
870 // Request to stop discovery twice.
871 for (int i = 0; i < 2; i++) {
872 discovery_sessions_[i]->Stop(GetCallback(), GetErrorCallback());
873 }
874
875 // The observer should have received no additional discovering changed events,
876 // the success callback should have been called 2 times and the adapter should
877 // still be discovering.
878 EXPECT_EQ(1, observer.discovering_changed_count());
879 EXPECT_EQ(5, callback_count_);
880 EXPECT_EQ(0, error_callback_count_);
881 EXPECT_TRUE(observer.last_discovering());
882 EXPECT_TRUE(adapter_->IsDiscovering());
883 EXPECT_TRUE(adapter_->IsDiscovering());
884 EXPECT_FALSE(discovery_sessions_[0]->IsActive());
885 EXPECT_FALSE(discovery_sessions_[1]->IsActive());
886 EXPECT_TRUE(discovery_sessions_[2]->IsActive());
887
888 // Request device discovery 3 times.
889 for (int i = 0; i < 3; i++) {
890 adapter_->StartDiscoverySession(
891 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
892 base::Unretained(this)),
893 GetErrorCallback());
894 }
895
896 // The observer should have received no additional discovering changed events,
897 // the success callback should have been called 3 times and the adapter should
898 // still be discovering.
899 EXPECT_EQ(1, observer.discovering_changed_count());
900 EXPECT_EQ(8, 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)6, discovery_sessions_.size());
905
906 // Request to stop discovery 4 times.
907 for (int i = 2; i < 6; i++) {
908 discovery_sessions_[i]->Stop(GetCallback(), GetErrorCallback());
909 }
910 // Run only once, as there should have been one D-Bus call.
911 message_loop_.Run();
912
913 // The observer should have received the discovering changed event exactly
914 // once, the success callback should have been called 4 times and the adapter
915 // should no longer be discovering.
916 EXPECT_EQ(2, observer.discovering_changed_count());
917 EXPECT_EQ(12, callback_count_);
918 EXPECT_EQ(0, error_callback_count_);
919 EXPECT_FALSE(observer.last_discovering());
920 EXPECT_FALSE(adapter_->IsDiscovering());
921
922 // All discovery sessions should be inactive.
923 for (int i = 0; i < 6; i++)
924 EXPECT_FALSE(discovery_sessions_[i]->IsActive());
925
926 // Request to stop discovery on of the inactive sessions.
927 discovery_sessions_[0]->Stop(GetCallback(), GetErrorCallback());
928
929 // The call should have failed.
930 EXPECT_EQ(2, observer.discovering_changed_count());
931 EXPECT_EQ(12, callback_count_);
932 EXPECT_EQ(1, error_callback_count_);
933 EXPECT_FALSE(observer.last_discovering());
934 EXPECT_FALSE(adapter_->IsDiscovering());
935 }
936
937 // This unit test asserts that the reference counting logic works correctly in
938 // the cases when the adapter gets reset and D-Bus calls are made outside of
939 // the BluetoothAdapter.
940 TEST_F(BluetoothChromeOSTest,
941 UnexpectedChangesDuringMultipleDiscoverySessions) {
942 GetAdapter();
943 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
944 EXPECT_EQ(1, callback_count_);
945 EXPECT_EQ(0, error_callback_count_);
946 EXPECT_TRUE(adapter_->IsPowered());
947 callback_count_ = 0;
948
949 TestBluetoothAdapterObserver observer(adapter_);
950
951 EXPECT_EQ(0, observer.discovering_changed_count());
952 EXPECT_FALSE(observer.last_discovering());
953 EXPECT_FALSE(adapter_->IsDiscovering());
954
955 // Request device discovery 3 times.
956 for (int i = 0; i < 3; i++) {
957 adapter_->StartDiscoverySession(
958 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
959 base::Unretained(this)),
960 GetErrorCallback());
961 }
962 // Run only once, as there should have been one D-Bus call.
963 message_loop_.Run();
964
965 // The observer should have received the discovering changed event exactly
966 // once, the success callback should have been called 3 times and the adapter
967 // should be discovering.
968 EXPECT_EQ(1, observer.discovering_changed_count());
969 EXPECT_EQ(3, callback_count_);
970 EXPECT_EQ(0, error_callback_count_);
971 EXPECT_TRUE(observer.last_discovering());
972 EXPECT_TRUE(adapter_->IsDiscovering());
973 ASSERT_EQ((size_t)3, discovery_sessions_.size());
974
975 for (int i = 0; i < 3; i++)
976 EXPECT_TRUE(discovery_sessions_[i]->IsActive());
977
978 // Stop the timers that the simulation uses
979 fake_bluetooth_device_client_->EndDiscoverySimulation(
980 dbus::ObjectPath(bluez::FakeBluetoothAdapterClient::kAdapterPath));
981
982 ASSERT_TRUE(adapter_->IsPowered());
983 ASSERT_TRUE(adapter_->IsDiscovering());
984
985 // Stop device discovery behind the adapter. The adapter and the observer
986 // should be notified of the change and the reference count should be reset.
987 // Even though bluez::FakeBluetoothAdapterClient does its own reference
988 // counting and
989 // we called 3 BluetoothAdapter::StartDiscoverySession 3 times, the
990 // bluez::FakeBluetoothAdapterClient's count should be only 1 and a single
991 // call to
992 // bluez::FakeBluetoothAdapterClient::StopDiscovery should work.
993 fake_bluetooth_adapter_client_->StopDiscovery(
994 dbus::ObjectPath(bluez::FakeBluetoothAdapterClient::kAdapterPath),
995 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
996 base::Unretained(this)));
997 message_loop_.Run();
998 EXPECT_EQ(2, observer.discovering_changed_count());
999 EXPECT_EQ(4, callback_count_);
1000 EXPECT_EQ(0, error_callback_count_);
1001 EXPECT_FALSE(observer.last_discovering());
1002 EXPECT_FALSE(adapter_->IsDiscovering());
1003
1004 // All discovery session instances should have been updated.
1005 for (int i = 0; i < 3; i++)
1006 EXPECT_FALSE(discovery_sessions_[i]->IsActive());
1007 discovery_sessions_.clear();
1008
1009 // It should be possible to successfully start discovery.
1010 for (int i = 0; i < 2; i++) {
1011 adapter_->StartDiscoverySession(
1012 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1013 base::Unretained(this)),
1014 GetErrorCallback());
1015 }
1016 // Run only once, as there should have been one D-Bus call.
1017 message_loop_.Run();
1018 EXPECT_EQ(3, observer.discovering_changed_count());
1019 EXPECT_EQ(6, callback_count_);
1020 EXPECT_EQ(0, error_callback_count_);
1021 EXPECT_TRUE(observer.last_discovering());
1022 EXPECT_TRUE(adapter_->IsDiscovering());
1023 ASSERT_EQ((size_t)2, discovery_sessions_.size());
1024
1025 for (int i = 0; i < 2; i++)
1026 EXPECT_TRUE(discovery_sessions_[i]->IsActive());
1027
1028 fake_bluetooth_device_client_->EndDiscoverySimulation(
1029 dbus::ObjectPath(bluez::FakeBluetoothAdapterClient::kAdapterPath));
1030
1031 // Make the adapter disappear and appear. This will make it come back as
1032 // discovering. When this happens, the reference count should become and
1033 // remain 0 as no new request was made through the BluetoothAdapter.
1034 fake_bluetooth_adapter_client_->SetVisible(false);
1035 ASSERT_FALSE(adapter_->IsPresent());
1036 EXPECT_EQ(4, observer.discovering_changed_count());
1037 EXPECT_EQ(6, callback_count_);
1038 EXPECT_EQ(0, error_callback_count_);
1039 EXPECT_FALSE(observer.last_discovering());
1040 EXPECT_FALSE(adapter_->IsDiscovering());
1041
1042 for (int i = 0; i < 2; i++)
1043 EXPECT_FALSE(discovery_sessions_[i]->IsActive());
1044 discovery_sessions_.clear();
1045
1046 fake_bluetooth_adapter_client_->SetVisible(true);
1047 ASSERT_TRUE(adapter_->IsPresent());
1048 EXPECT_EQ(5, observer.discovering_changed_count());
1049 EXPECT_EQ(6, callback_count_);
1050 EXPECT_EQ(0, error_callback_count_);
1051 EXPECT_TRUE(observer.last_discovering());
1052 EXPECT_TRUE(adapter_->IsDiscovering());
1053
1054 // Start and stop discovery. At this point, bluez::FakeBluetoothAdapterClient
1055 // has
1056 // a reference count that is equal to 1. Pretend that this was done by an
1057 // application other than us. Starting and stopping discovery will succeed
1058 // but it won't cause the discovery state to change.
1059 adapter_->StartDiscoverySession(
1060 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1061 base::Unretained(this)),
1062 GetErrorCallback());
1063 message_loop_.Run(); // Run the loop, as there should have been a D-Bus call.
1064 EXPECT_EQ(5, observer.discovering_changed_count());
1065 EXPECT_EQ(7, callback_count_);
1066 EXPECT_EQ(0, error_callback_count_);
1067 EXPECT_TRUE(observer.last_discovering());
1068 EXPECT_TRUE(adapter_->IsDiscovering());
1069 ASSERT_EQ((size_t)1, discovery_sessions_.size());
1070 EXPECT_TRUE(discovery_sessions_[0]->IsActive());
1071
1072 discovery_sessions_[0]->Stop(GetCallback(), GetErrorCallback());
1073 message_loop_.Run(); // Run the loop, as there should have been a D-Bus call.
1074 EXPECT_EQ(5, observer.discovering_changed_count());
1075 EXPECT_EQ(8, callback_count_);
1076 EXPECT_EQ(0, error_callback_count_);
1077 EXPECT_TRUE(observer.last_discovering());
1078 EXPECT_TRUE(adapter_->IsDiscovering());
1079 EXPECT_FALSE(discovery_sessions_[0]->IsActive());
1080 discovery_sessions_.clear();
1081
1082 // Start discovery again.
1083 adapter_->StartDiscoverySession(
1084 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1085 base::Unretained(this)),
1086 GetErrorCallback());
1087 message_loop_.Run(); // Run the loop, as there should have been a D-Bus call.
1088 EXPECT_EQ(5, observer.discovering_changed_count());
1089 EXPECT_EQ(9, callback_count_);
1090 EXPECT_EQ(0, error_callback_count_);
1091 EXPECT_TRUE(observer.last_discovering());
1092 EXPECT_TRUE(adapter_->IsDiscovering());
1093 ASSERT_EQ((size_t)1, discovery_sessions_.size());
1094 EXPECT_TRUE(discovery_sessions_[0]->IsActive());
1095
1096 // Stop discovery via D-Bus. The fake client's reference count will drop but
1097 // the discovery state won't change since our BluetoothAdapter also just
1098 // requested it via D-Bus.
1099 fake_bluetooth_adapter_client_->StopDiscovery(
1100 dbus::ObjectPath(bluez::FakeBluetoothAdapterClient::kAdapterPath),
1101 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
1102 base::Unretained(this)));
1103 message_loop_.Run();
1104 EXPECT_EQ(5, observer.discovering_changed_count());
1105 EXPECT_EQ(10, callback_count_);
1106 EXPECT_EQ(0, error_callback_count_);
1107 EXPECT_TRUE(observer.last_discovering());
1108 EXPECT_TRUE(adapter_->IsDiscovering());
1109
1110 // Now end the discovery session. This should change the adapter's discovery
1111 // state.
1112 discovery_sessions_[0]->Stop(GetCallback(), GetErrorCallback());
1113 message_loop_.Run();
1114 EXPECT_EQ(6, observer.discovering_changed_count());
1115 EXPECT_EQ(11, callback_count_);
1116 EXPECT_EQ(0, error_callback_count_);
1117 EXPECT_FALSE(observer.last_discovering());
1118 EXPECT_FALSE(adapter_->IsDiscovering());
1119 EXPECT_FALSE(discovery_sessions_[0]->IsActive());
1120 }
1121
1122 TEST_F(BluetoothChromeOSTest, InvalidatedDiscoverySessions) {
1123 GetAdapter();
1124 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
1125 EXPECT_EQ(1, callback_count_);
1126 EXPECT_EQ(0, error_callback_count_);
1127 EXPECT_TRUE(adapter_->IsPowered());
1128 callback_count_ = 0;
1129
1130 TestBluetoothAdapterObserver observer(adapter_);
1131
1132 EXPECT_EQ(0, observer.discovering_changed_count());
1133 EXPECT_FALSE(observer.last_discovering());
1134 EXPECT_FALSE(adapter_->IsDiscovering());
1135
1136 // Request device discovery 3 times.
1137 for (int i = 0; i < 3; i++) {
1138 adapter_->StartDiscoverySession(
1139 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1140 base::Unretained(this)),
1141 GetErrorCallback());
1142 }
1143 // Run only once, as there should have been one D-Bus call.
1144 message_loop_.Run();
1145
1146 // The observer should have received the discovering changed event exactly
1147 // once, the success callback should have been called 3 times and the adapter
1148 // should be discovering.
1149 EXPECT_EQ(1, observer.discovering_changed_count());
1150 EXPECT_EQ(3, callback_count_);
1151 EXPECT_EQ(0, error_callback_count_);
1152 EXPECT_TRUE(observer.last_discovering());
1153 EXPECT_TRUE(adapter_->IsDiscovering());
1154 ASSERT_EQ((size_t)3, discovery_sessions_.size());
1155
1156 for (int i = 0; i < 3; i++)
1157 EXPECT_TRUE(discovery_sessions_[i]->IsActive());
1158
1159 // Stop the timers that the simulation uses
1160 fake_bluetooth_device_client_->EndDiscoverySimulation(
1161 dbus::ObjectPath(bluez::FakeBluetoothAdapterClient::kAdapterPath));
1162
1163 ASSERT_TRUE(adapter_->IsPowered());
1164 ASSERT_TRUE(adapter_->IsDiscovering());
1165
1166 // Delete all but one discovery session.
1167 discovery_sessions_.pop_back();
1168 discovery_sessions_.pop_back();
1169 ASSERT_EQ((size_t)1, discovery_sessions_.size());
1170 EXPECT_TRUE(discovery_sessions_[0]->IsActive());
1171 EXPECT_TRUE(adapter_->IsDiscovering());
1172
1173 // Stop device discovery behind the adapter. The one active discovery session
1174 // should become inactive, but more importantly, we shouldn't run into any
1175 // memory errors as the sessions that we explicitly deleted should get
1176 // cleaned up.
1177 fake_bluetooth_adapter_client_->StopDiscovery(
1178 dbus::ObjectPath(bluez::FakeBluetoothAdapterClient::kAdapterPath),
1179 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
1180 base::Unretained(this)));
1181 message_loop_.Run();
1182 EXPECT_EQ(2, observer.discovering_changed_count());
1183 EXPECT_EQ(4, callback_count_);
1184 EXPECT_EQ(0, error_callback_count_);
1185 EXPECT_FALSE(observer.last_discovering());
1186 EXPECT_FALSE(adapter_->IsDiscovering());
1187 EXPECT_FALSE(discovery_sessions_[0]->IsActive());
1188 }
1189
1190 TEST_F(BluetoothChromeOSTest, QueuedDiscoveryRequests) {
1191 GetAdapter();
1192
1193 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
1194 EXPECT_EQ(1, callback_count_);
1195 EXPECT_EQ(0, error_callback_count_);
1196 EXPECT_TRUE(adapter_->IsPowered());
1197 callback_count_ = 0;
1198
1199 TestBluetoothAdapterObserver observer(adapter_);
1200
1201 EXPECT_EQ(0, observer.discovering_changed_count());
1202 EXPECT_FALSE(observer.last_discovering());
1203 EXPECT_FALSE(adapter_->IsDiscovering());
1204
1205 // Request to start discovery. The call should be pending.
1206 adapter_->StartDiscoverySession(
1207 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1208 base::Unretained(this)),
1209 GetErrorCallback());
1210 EXPECT_EQ(0, callback_count_);
1211
1212 fake_bluetooth_device_client_->EndDiscoverySimulation(
1213 dbus::ObjectPath(bluez::FakeBluetoothAdapterClient::kAdapterPath));
1214
1215 // The underlying adapter has started discovery, but our call hasn't returned
1216 // yet.
1217 EXPECT_EQ(1, observer.discovering_changed_count());
1218 EXPECT_TRUE(observer.last_discovering());
1219 EXPECT_TRUE(adapter_->IsDiscovering());
1220 EXPECT_TRUE(discovery_sessions_.empty());
1221
1222 // Request to start discovery twice. These should get queued and there should
1223 // be no change in state.
1224 for (int i = 0; i < 2; i++) {
1225 adapter_->StartDiscoverySession(
1226 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1227 base::Unretained(this)),
1228 GetErrorCallback());
1229 }
1230 EXPECT_EQ(0, callback_count_);
1231 EXPECT_EQ(0, error_callback_count_);
1232 EXPECT_EQ(1, observer.discovering_changed_count());
1233 EXPECT_TRUE(observer.last_discovering());
1234 EXPECT_TRUE(adapter_->IsDiscovering());
1235 EXPECT_TRUE(discovery_sessions_.empty());
1236
1237 // Process the pending call. The queued calls should execute and the discovery
1238 // session reference count should increase.
1239 message_loop_.Run();
1240 EXPECT_EQ(3, callback_count_);
1241 EXPECT_EQ(0, error_callback_count_);
1242 EXPECT_EQ(1, observer.discovering_changed_count());
1243 EXPECT_TRUE(observer.last_discovering());
1244 EXPECT_TRUE(adapter_->IsDiscovering());
1245 ASSERT_EQ((size_t)3, discovery_sessions_.size());
1246
1247 // Verify the reference count by removing sessions 3 times. The last request
1248 // should remain pending.
1249 for (int i = 0; i < 3; i++) {
1250 discovery_sessions_[i]->Stop(GetCallback(), GetErrorCallback());
1251 }
1252 EXPECT_EQ(5, callback_count_);
1253 EXPECT_EQ(0, error_callback_count_);
1254 EXPECT_EQ(2, observer.discovering_changed_count());
1255 EXPECT_FALSE(observer.last_discovering());
1256 EXPECT_FALSE(adapter_->IsDiscovering());
1257 EXPECT_FALSE(discovery_sessions_[0]->IsActive());
1258 EXPECT_FALSE(discovery_sessions_[1]->IsActive());
1259 EXPECT_TRUE(discovery_sessions_[2]->IsActive());
1260
1261 // Request to stop the session whose call is pending should fail.
1262 discovery_sessions_[2]->Stop(GetCallback(), GetErrorCallback());
1263 EXPECT_EQ(5, callback_count_);
1264 EXPECT_EQ(1, error_callback_count_);
1265 EXPECT_EQ(2, observer.discovering_changed_count());
1266 EXPECT_FALSE(observer.last_discovering());
1267 EXPECT_FALSE(adapter_->IsDiscovering());
1268 EXPECT_TRUE(discovery_sessions_[2]->IsActive());
1269
1270 // Request to start should get queued.
1271 adapter_->StartDiscoverySession(
1272 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1273 base::Unretained(this)),
1274 GetErrorCallback());
1275 EXPECT_EQ(5, callback_count_);
1276 EXPECT_EQ(1, error_callback_count_);
1277 EXPECT_EQ(2, observer.discovering_changed_count());
1278 EXPECT_FALSE(observer.last_discovering());
1279 EXPECT_FALSE(adapter_->IsDiscovering());
1280 ASSERT_EQ((size_t)3, discovery_sessions_.size());
1281
1282 // Run the pending request.
1283 message_loop_.Run();
1284 EXPECT_EQ(6, callback_count_);
1285 EXPECT_EQ(1, error_callback_count_);
1286 EXPECT_EQ(3, observer.discovering_changed_count());
1287 EXPECT_TRUE(observer.last_discovering());
1288 EXPECT_TRUE(adapter_->IsDiscovering());
1289 ASSERT_EQ((size_t)3, discovery_sessions_.size());
1290 EXPECT_FALSE(discovery_sessions_[2]->IsActive());
1291
1292 // The queued request to start discovery should have been issued but is still
1293 // pending. Run the loop and verify.
1294 message_loop_.Run();
1295 EXPECT_EQ(7, callback_count_);
1296 EXPECT_EQ(1, error_callback_count_);
1297 EXPECT_EQ(3, observer.discovering_changed_count());
1298 EXPECT_TRUE(observer.last_discovering());
1299 EXPECT_TRUE(adapter_->IsDiscovering());
1300 ASSERT_EQ((size_t)4, discovery_sessions_.size());
1301 EXPECT_TRUE(discovery_sessions_[3]->IsActive());
1302 }
1303
1304 TEST_F(BluetoothChromeOSTest, StartDiscoverySession) {
1305 GetAdapter();
1306
1307 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
1308 EXPECT_EQ(1, callback_count_);
1309 EXPECT_EQ(0, error_callback_count_);
1310 EXPECT_TRUE(adapter_->IsPowered());
1311 callback_count_ = 0;
1312
1313 TestBluetoothAdapterObserver observer(adapter_);
1314
1315 EXPECT_EQ(0, observer.discovering_changed_count());
1316 EXPECT_FALSE(observer.last_discovering());
1317 EXPECT_FALSE(adapter_->IsDiscovering());
1318 EXPECT_TRUE(discovery_sessions_.empty());
1319
1320 // Request a new discovery session.
1321 adapter_->StartDiscoverySession(
1322 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1323 base::Unretained(this)),
1324 GetErrorCallback());
1325 message_loop_.Run();
1326 EXPECT_EQ(1, observer.discovering_changed_count());
1327 EXPECT_EQ(1, callback_count_);
1328 EXPECT_EQ(0, error_callback_count_);
1329 EXPECT_TRUE(observer.last_discovering());
1330 EXPECT_TRUE(adapter_->IsDiscovering());
1331 ASSERT_EQ((size_t)1, discovery_sessions_.size());
1332 EXPECT_TRUE(discovery_sessions_[0]->IsActive());
1333
1334 // Start another session. A new one should be returned in the callback, which
1335 // in turn will destroy the previous session. Adapter should still be
1336 // discovering and the reference count should be 1.
1337 adapter_->StartDiscoverySession(
1338 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1339 base::Unretained(this)),
1340 GetErrorCallback());
1341 message_loop_.Run();
1342 EXPECT_EQ(1, observer.discovering_changed_count());
1343 EXPECT_EQ(2, callback_count_);
1344 EXPECT_EQ(0, error_callback_count_);
1345 EXPECT_TRUE(observer.last_discovering());
1346 EXPECT_TRUE(adapter_->IsDiscovering());
1347 ASSERT_EQ((size_t)2, discovery_sessions_.size());
1348 EXPECT_TRUE(discovery_sessions_[0]->IsActive());
1349
1350 // Request a new session.
1351 adapter_->StartDiscoverySession(
1352 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1353 base::Unretained(this)),
1354 GetErrorCallback());
1355 message_loop_.Run();
1356 EXPECT_EQ(1, observer.discovering_changed_count());
1357 EXPECT_EQ(3, callback_count_);
1358 EXPECT_EQ(0, error_callback_count_);
1359 EXPECT_TRUE(observer.last_discovering());
1360 EXPECT_TRUE(adapter_->IsDiscovering());
1361 ASSERT_EQ((size_t)3, discovery_sessions_.size());
1362 EXPECT_TRUE(discovery_sessions_[1]->IsActive());
1363 EXPECT_NE(discovery_sessions_[0], discovery_sessions_[1]);
1364
1365 // Stop the previous discovery session. The session should end but discovery
1366 // should continue.
1367 discovery_sessions_[0]->Stop(GetCallback(), GetErrorCallback());
1368 message_loop_.Run();
1369 EXPECT_EQ(1, observer.discovering_changed_count());
1370 EXPECT_EQ(4, callback_count_);
1371 EXPECT_EQ(0, error_callback_count_);
1372 EXPECT_TRUE(observer.last_discovering());
1373 EXPECT_TRUE(adapter_->IsDiscovering());
1374 ASSERT_EQ((size_t)3, discovery_sessions_.size());
1375 EXPECT_FALSE(discovery_sessions_[0]->IsActive());
1376 EXPECT_TRUE(discovery_sessions_[1]->IsActive());
1377
1378 // Delete the current active session. Discovery should eventually stop.
1379 discovery_sessions_.clear();
1380 while (observer.last_discovering())
1381 message_loop_.RunUntilIdle();
1382
1383 EXPECT_EQ(2, observer.discovering_changed_count());
1384 EXPECT_EQ(4, callback_count_);
1385 EXPECT_EQ(0, error_callback_count_);
1386 EXPECT_FALSE(observer.last_discovering());
1387 EXPECT_FALSE(adapter_->IsDiscovering());
1388 }
1389
1390 TEST_F(BluetoothChromeOSTest, SetDiscoveryFilterBeforeStartDiscovery) {
1391 // Test a simulated discovery session.
1392 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
1393 GetAdapter();
1394
1395 TestBluetoothAdapterObserver observer(adapter_);
1396
1397 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter(
1398 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
1399 df->SetRSSI(-60);
1400 df->AddUUID(BluetoothUUID("1000"));
1401 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter(df);
1402
1403 adapter_->SetPowered(true, base::Bind(&BluetoothChromeOSTest::Callback,
1404 base::Unretained(this)),
1405 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1406 base::Unretained(this)));
1407 adapter_->StartDiscoverySessionWithFilter(
1408 discovery_filter.Pass(),
1409 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1410 base::Unretained(this)),
1411 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1412 base::Unretained(this)));
1413 message_loop_.Run();
1414 EXPECT_EQ(2, callback_count_);
1415 EXPECT_EQ(0, error_callback_count_);
1416 callback_count_ = 0;
1417
1418 ASSERT_TRUE(adapter_->IsPowered());
1419 ASSERT_TRUE(adapter_->IsDiscovering());
1420 ASSERT_EQ((size_t)1, discovery_sessions_.size());
1421 ASSERT_TRUE(discovery_sessions_[0]->IsActive());
1422 ASSERT_TRUE(df->Equals(*discovery_sessions_[0]->GetDiscoveryFilter()));
1423
1424 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1425 EXPECT_NE(nullptr, filter);
1426 EXPECT_EQ("le", *filter->transport);
1427 EXPECT_EQ(-60, *filter->rssi);
1428 EXPECT_EQ(nullptr, filter->pathloss.get());
1429 std::vector<std::string> uuids = *filter->uuids;
1430 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1000"));
1431
1432 discovery_sessions_[0]->Stop(
1433 base::Bind(&BluetoothChromeOSTest::Callback, base::Unretained(this)),
1434 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1435 base::Unretained(this)));
1436
1437 message_loop_.Run();
1438
1439 EXPECT_EQ(1, callback_count_);
1440 EXPECT_EQ(0, error_callback_count_);
1441
1442 ASSERT_TRUE(adapter_->IsPowered());
1443 ASSERT_FALSE(adapter_->IsDiscovering());
1444 ASSERT_EQ((size_t)1, discovery_sessions_.size());
1445 ASSERT_FALSE(discovery_sessions_[0]->IsActive());
1446 ASSERT_EQ(discovery_sessions_[0]->GetDiscoveryFilter(),
1447 (BluetoothDiscoveryFilter*)nullptr);
1448
1449 filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1450 EXPECT_EQ(nullptr, filter);
1451 }
1452
1453 TEST_F(BluetoothChromeOSTest, SetDiscoveryFilterBeforeStartDiscoveryFail) {
1454 // Test a simulated discovery session.
1455 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
1456 GetAdapter();
1457
1458 TestBluetoothAdapterObserver observer(adapter_);
1459
1460 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter(
1461 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
1462 df->SetRSSI(-60);
1463 df->AddUUID(BluetoothUUID("1000"));
1464 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter(df);
1465
1466 adapter_->SetPowered(true, base::Bind(&BluetoothChromeOSTest::Callback,
1467 base::Unretained(this)),
1468 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1469 base::Unretained(this)));
1470 EXPECT_EQ(1, callback_count_);
1471 callback_count_ = 0;
1472
1473 fake_bluetooth_adapter_client_->MakeSetDiscoveryFilterFail();
1474
1475 adapter_->StartDiscoverySessionWithFilter(
1476 discovery_filter.Pass(),
1477 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1478 base::Unretained(this)),
1479 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1480 base::Unretained(this)));
1481
1482 message_loop_.Run();
1483
1484 EXPECT_EQ(1, error_callback_count_);
1485 error_callback_count_ = 0;
1486
1487 ASSERT_TRUE(adapter_->IsPowered());
1488 ASSERT_FALSE(adapter_->IsDiscovering());
1489 ASSERT_EQ((size_t)0, discovery_sessions_.size());
1490
1491 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1492 EXPECT_EQ(nullptr, filter);
1493 }
1494
1495 // This test queues two requests to StartDiscovery with pre set filter. This
1496 // should result in SetDiscoveryFilter, then StartDiscovery, and SetDiscovery
1497 // DBus calls
1498 TEST_F(BluetoothChromeOSTest, QueuedSetDiscoveryFilterBeforeStartDiscovery) {
1499 // Test a simulated discovery session.
1500 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
1501 GetAdapter();
1502
1503 TestBluetoothAdapterObserver observer(adapter_);
1504
1505 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter(
1506 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
1507 df->SetRSSI(-60);
1508 df->AddUUID(BluetoothUUID("1000"));
1509 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter(df);
1510
1511 BluetoothDiscoveryFilter* df2 = new BluetoothDiscoveryFilter(
1512 BluetoothDiscoveryFilter::Transport::TRANSPORT_CLASSIC);
1513 df2->SetRSSI(-65);
1514 df2->AddUUID(BluetoothUUID("1002"));
1515 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter2(df2);
1516
1517 adapter_->SetPowered(true, base::Bind(&BluetoothChromeOSTest::Callback,
1518 base::Unretained(this)),
1519 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1520 base::Unretained(this)));
1521
1522 EXPECT_EQ(1, callback_count_);
1523 EXPECT_EQ(0, error_callback_count_);
1524 callback_count_ = 0;
1525
1526 // Queue two requests to start discovery session with filter.
1527 adapter_->StartDiscoverySessionWithFilter(
1528 discovery_filter.Pass(),
1529 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1530 base::Unretained(this)),
1531 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1532 base::Unretained(this)));
1533
1534 adapter_->StartDiscoverySessionWithFilter(
1535 discovery_filter2.Pass(),
1536 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1537 base::Unretained(this)),
1538 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1539 base::Unretained(this)));
1540
1541 // Run requests, on DBus level there should be call SetDiscoveryFilter, then
1542 // StartDiscovery, then SetDiscoveryFilter again.
1543 message_loop_.Run();
1544 message_loop_.Run();
1545
1546 EXPECT_EQ(2, callback_count_);
1547 EXPECT_EQ(0, error_callback_count_);
1548 callback_count_ = 0;
1549
1550 ASSERT_TRUE(adapter_->IsPowered());
1551 ASSERT_TRUE(adapter_->IsDiscovering());
1552 ASSERT_EQ((size_t)2, discovery_sessions_.size());
1553 ASSERT_TRUE(discovery_sessions_[0]->IsActive());
1554 ASSERT_TRUE(df->Equals(*discovery_sessions_[0]->GetDiscoveryFilter()));
1555 ASSERT_TRUE(discovery_sessions_[1]->IsActive());
1556 ASSERT_TRUE(df2->Equals(*discovery_sessions_[1]->GetDiscoveryFilter()));
1557
1558 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1559 EXPECT_NE(nullptr, filter);
1560 EXPECT_EQ("auto", *filter->transport);
1561 EXPECT_EQ(-65, *filter->rssi);
1562 EXPECT_EQ(nullptr, filter->pathloss.get());
1563 auto uuids = *filter->uuids;
1564 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1000"));
1565 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1002"));
1566
1567 discovery_sessions_[0]->Stop(
1568 base::Bind(&BluetoothChromeOSTest::Callback, base::Unretained(this)),
1569 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1570 base::Unretained(this)));
1571
1572 discovery_sessions_[1]->Stop(
1573 base::Bind(&BluetoothChromeOSTest::Callback, base::Unretained(this)),
1574 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1575 base::Unretained(this)));
1576
1577 message_loop_.Run();
1578
1579 EXPECT_EQ(2, callback_count_);
1580 EXPECT_EQ(0, error_callback_count_);
1581
1582 ASSERT_TRUE(adapter_->IsPowered());
1583 ASSERT_FALSE(adapter_->IsDiscovering());
1584 ASSERT_FALSE(discovery_sessions_[0]->IsActive());
1585 ASSERT_EQ(discovery_sessions_[0]->GetDiscoveryFilter(),
1586 (BluetoothDiscoveryFilter*)nullptr);
1587 ASSERT_FALSE(discovery_sessions_[1]->IsActive());
1588 ASSERT_EQ(discovery_sessions_[1]->GetDiscoveryFilter(),
1589 (BluetoothDiscoveryFilter*)nullptr);
1590
1591 filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1592 EXPECT_EQ(nullptr, filter);
1593 }
1594
1595 // Call StartFilteredDiscovery twice (2nd time while 1st call is still pending).
1596 // Make the first SetDiscoveryFilter fail and the second one succeed. It should
1597 // end up with one active discovery session.
1598 TEST_F(BluetoothChromeOSTest,
1599 QueuedSetDiscoveryFilterBeforeStartDiscoveryFail) {
1600 // Test a simulated discovery session.
1601 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
1602 GetAdapter();
1603
1604 TestBluetoothAdapterObserver observer(adapter_);
1605
1606 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter(
1607 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
1608 df->SetRSSI(-60);
1609 df->AddUUID(BluetoothUUID("1000"));
1610 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter(df);
1611
1612 BluetoothDiscoveryFilter* df2 = new BluetoothDiscoveryFilter(
1613 BluetoothDiscoveryFilter::Transport::TRANSPORT_CLASSIC);
1614 df2->SetRSSI(-65);
1615 df2->AddUUID(BluetoothUUID("1002"));
1616 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter2(df2);
1617
1618 adapter_->SetPowered(true, base::Bind(&BluetoothChromeOSTest::Callback,
1619 base::Unretained(this)),
1620 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1621 base::Unretained(this)));
1622
1623 EXPECT_EQ(1, callback_count_);
1624 EXPECT_EQ(0, error_callback_count_);
1625 callback_count_ = 0;
1626
1627 fake_bluetooth_adapter_client_->MakeSetDiscoveryFilterFail();
1628
1629 // Queue two requests to start discovery session with filter.
1630 adapter_->StartDiscoverySessionWithFilter(
1631 discovery_filter.Pass(),
1632 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1633 base::Unretained(this)),
1634 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1635 base::Unretained(this)));
1636
1637 adapter_->StartDiscoverySessionWithFilter(
1638 discovery_filter2.Pass(),
1639 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1640 base::Unretained(this)),
1641 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1642 base::Unretained(this)));
1643
1644 message_loop_.Run();
1645
1646 // First request to SetDiscoveryFilter should fail, resulting in no session
1647 // being created.
1648 EXPECT_EQ(0, callback_count_);
1649 EXPECT_EQ(1, error_callback_count_);
1650 error_callback_count_ = 0;
1651
1652 ASSERT_TRUE(adapter_->IsPowered());
1653 ASSERT_FALSE(adapter_->IsDiscovering());
1654 ASSERT_EQ((size_t)0, discovery_sessions_.size());
1655
1656 message_loop_.Run();
1657
1658 // Second request should succeed
1659 EXPECT_EQ(1, callback_count_);
1660 EXPECT_EQ(0, error_callback_count_);
1661 callback_count_ = 0;
1662
1663 ASSERT_TRUE(adapter_->IsDiscovering());
1664 ASSERT_EQ((size_t)1, discovery_sessions_.size());
1665 ASSERT_TRUE(discovery_sessions_[0]->IsActive());
1666 ASSERT_TRUE(df2->Equals(*discovery_sessions_[0]->GetDiscoveryFilter()));
1667
1668 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1669 EXPECT_NE(nullptr, filter);
1670 EXPECT_EQ("bredr", *filter->transport);
1671 EXPECT_EQ(-65, *filter->rssi);
1672 EXPECT_EQ(nullptr, filter->pathloss.get());
1673 auto uuids = *filter->uuids;
1674 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1002"));
1675
1676 discovery_sessions_[0]->Stop(
1677 base::Bind(&BluetoothChromeOSTest::Callback, base::Unretained(this)),
1678 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1679 base::Unretained(this)));
1680
1681 message_loop_.Run();
1682
1683 EXPECT_EQ(1, callback_count_);
1684 EXPECT_EQ(0, error_callback_count_);
1685
1686 ASSERT_TRUE(adapter_->IsPowered());
1687 ASSERT_FALSE(adapter_->IsDiscovering());
1688 ASSERT_FALSE(discovery_sessions_[0]->IsActive());
1689 ASSERT_EQ(discovery_sessions_[0]->GetDiscoveryFilter(),
1690 (BluetoothDiscoveryFilter*)nullptr);
1691
1692 filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1693 EXPECT_EQ(nullptr, filter);
1694 }
1695
1696 TEST_F(BluetoothChromeOSTest, SetDiscoveryFilterAfterStartDiscovery) {
1697 // Test a simulated discovery session.
1698 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
1699 GetAdapter();
1700
1701 TestBluetoothAdapterObserver observer(adapter_);
1702
1703 adapter_->SetPowered(true, base::Bind(&BluetoothChromeOSTest::Callback,
1704 base::Unretained(this)),
1705 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1706 base::Unretained(this)));
1707 adapter_->StartDiscoverySession(
1708 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1709 base::Unretained(this)),
1710 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1711 base::Unretained(this)));
1712 message_loop_.Run();
1713 EXPECT_EQ(2, callback_count_);
1714 EXPECT_EQ(0, error_callback_count_);
1715 callback_count_ = 0;
1716
1717 ASSERT_TRUE(adapter_->IsPowered());
1718 ASSERT_TRUE(adapter_->IsDiscovering());
1719 ASSERT_EQ((size_t)1, discovery_sessions_.size());
1720 ASSERT_TRUE(discovery_sessions_[0]->IsActive());
1721 EXPECT_EQ(1, observer.discovering_changed_count());
1722 observer.Reset();
1723
1724 auto null_instance = scoped_ptr<BluetoothDiscoveryFilter>();
1725 null_instance.reset();
1726 ASSERT_EQ(discovery_sessions_[0]->GetDiscoveryFilter(), null_instance.get());
1727
1728 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1729 EXPECT_EQ(nullptr, filter);
1730
1731 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter(
1732 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
1733 df->SetRSSI(-60);
1734 df->AddUUID(BluetoothUUID("1000"));
1735 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter(df);
1736
1737 discovery_sessions_[0]->SetDiscoveryFilter(
1738 discovery_filter.Pass(),
1739 base::Bind(&BluetoothChromeOSTest::Callback, base::Unretained(this)),
1740 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1741 base::Unretained(this)));
1742
1743 message_loop_.Run();
1744 EXPECT_EQ(1, callback_count_);
1745 EXPECT_EQ(0, error_callback_count_);
1746 callback_count_ = 0;
1747
1748 ASSERT_TRUE(df->Equals(*discovery_sessions_[0]->GetDiscoveryFilter()));
1749
1750 filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1751 EXPECT_NE(nullptr, filter);
1752 EXPECT_EQ("le", *filter->transport);
1753 EXPECT_EQ(-60, *filter->rssi);
1754 EXPECT_EQ(nullptr, filter->pathloss.get());
1755 std::vector<std::string> uuids = *filter->uuids;
1756 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1000"));
1757
1758 discovery_sessions_[0]->Stop(
1759 base::Bind(&BluetoothChromeOSTest::Callback, base::Unretained(this)),
1760 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1761 base::Unretained(this)));
1762
1763 message_loop_.Run();
1764
1765 EXPECT_EQ(1, callback_count_);
1766 EXPECT_EQ(0, error_callback_count_);
1767
1768 ASSERT_TRUE(adapter_->IsPowered());
1769 ASSERT_FALSE(adapter_->IsDiscovering());
1770 ASSERT_EQ((size_t)1, discovery_sessions_.size());
1771 ASSERT_FALSE(discovery_sessions_[0]->IsActive());
1772 ASSERT_EQ(discovery_sessions_[0]->GetDiscoveryFilter(),
1773 (BluetoothDiscoveryFilter*)nullptr);
1774
1775 filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1776 EXPECT_EQ(nullptr, filter);
1777 }
1778
1779 // This unit test asserts that the basic reference counting, and filter merging
1780 // works correctly for discovery requests done via the BluetoothAdapter.
1781 TEST_F(BluetoothChromeOSTest, SetDiscoveryFilterBeforeStartDiscoveryMultiple) {
1782 GetAdapter();
1783 adapter_->SetPowered(true, base::Bind(&BluetoothChromeOSTest::Callback,
1784 base::Unretained(this)),
1785 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1786 base::Unretained(this)));
1787 EXPECT_EQ(1, callback_count_);
1788 EXPECT_EQ(0, error_callback_count_);
1789 EXPECT_TRUE(adapter_->IsPowered());
1790 callback_count_ = 0;
1791
1792 TestBluetoothAdapterObserver observer(adapter_);
1793
1794 // Request device discovery with pre-set filter 3 times.
1795 for (int i = 0; i < 3; i++) {
1796 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter;
1797 if (i == 0) {
1798 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter(
1799 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
1800 df->SetRSSI(-85);
1801 df->AddUUID(BluetoothUUID("1000"));
1802 discovery_filter.reset(df);
1803 } else if (i == 1) {
1804 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter(
1805 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
1806 df->SetRSSI(-60);
1807 df->AddUUID(BluetoothUUID("1020"));
1808 df->AddUUID(BluetoothUUID("1001"));
1809 discovery_filter.reset(df);
1810 } else if (i == 2) {
1811 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter(
1812 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
1813 df->SetRSSI(-65);
1814 df->AddUUID(BluetoothUUID("1020"));
1815 df->AddUUID(BluetoothUUID("1003"));
1816 discovery_filter.reset(df);
1817 }
1818
1819 adapter_->StartDiscoverySessionWithFilter(
1820 discovery_filter.Pass(),
1821 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1822 base::Unretained(this)),
1823 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1824 base::Unretained(this)));
1825
1826 message_loop_.Run();
1827
1828 if (i == 0) {
1829 EXPECT_EQ(1, observer.discovering_changed_count());
1830 observer.Reset();
1831
1832 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1833 EXPECT_EQ("le", *filter->transport);
1834 EXPECT_EQ(-85, *filter->rssi);
1835 EXPECT_EQ(nullptr, filter->pathloss.get());
1836 std::vector<std::string> uuids = *filter->uuids;
1837 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1000"));
1838 } else if (i == 1) {
1839 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1840 EXPECT_EQ("le", *filter->transport);
1841 EXPECT_EQ(-85, *filter->rssi);
1842 EXPECT_EQ(nullptr, filter->pathloss.get());
1843 std::vector<std::string> uuids = *filter->uuids;
1844 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1000"));
1845 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1001"));
1846 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1020"));
1847 } else if (i == 2) {
1848 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1849 EXPECT_EQ("le", *filter->transport);
1850 EXPECT_EQ(-85, *filter->rssi);
1851 EXPECT_EQ(nullptr, filter->pathloss.get());
1852 std::vector<std::string> uuids = *filter->uuids;
1853 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1000"));
1854 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1001"));
1855 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1003"));
1856 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1020"));
1857 }
1858 }
1859
1860 // the success callback should have been called 3 times and the adapter should
1861 // be discovering.
1862 EXPECT_EQ(3, callback_count_);
1863 EXPECT_EQ(0, error_callback_count_);
1864 EXPECT_TRUE(adapter_->IsDiscovering());
1865 ASSERT_EQ((size_t)3, discovery_sessions_.size());
1866
1867 callback_count_ = 0;
1868 // Request to stop discovery twice.
1869 for (int i = 0; i < 2; i++) {
1870 discovery_sessions_[i]->Stop(
1871 base::Bind(&BluetoothChromeOSTest::Callback, base::Unretained(this)),
1872 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1873 base::Unretained(this)));
1874 message_loop_.Run();
1875
1876 if (i == 0) {
1877 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1878 EXPECT_EQ("le", *filter->transport);
1879 EXPECT_EQ(-65, *filter->rssi);
1880 EXPECT_EQ(nullptr, filter->pathloss.get());
1881 std::vector<std::string> uuids = *filter->uuids;
1882 EXPECT_EQ(3UL, uuids.size());
1883 EXPECT_EQ(uuids.end(), std::find(uuids.begin(), uuids.end(), "1000"));
1884 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1001"));
1885 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1003"));
1886 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1020"));
1887 } else if (i == 1) {
1888 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1889 EXPECT_EQ("le", *filter->transport);
1890 EXPECT_EQ(-65, *filter->rssi);
1891 EXPECT_EQ(nullptr, filter->pathloss.get());
1892 std::vector<std::string> uuids = *filter->uuids;
1893 EXPECT_EQ(2UL, uuids.size());
1894 EXPECT_EQ(uuids.end(), std::find(uuids.begin(), uuids.end(), "1000"));
1895 EXPECT_EQ(uuids.end(), std::find(uuids.begin(), uuids.end(), "1001"));
1896 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1003"));
1897 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1020"));
1898 } else if (i == 2) {
1899 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1900 EXPECT_EQ("le", *filter->transport);
1901 EXPECT_EQ(-65, *filter->rssi);
1902 EXPECT_EQ(nullptr, filter->pathloss.get());
1903 std::vector<std::string> uuids = *filter->uuids;
1904 EXPECT_EQ(0UL, uuids.size());
1905 }
1906 }
1907
1908 // The success callback should have been called 2 times and the adapter should
1909 // still be discovering.
1910 EXPECT_EQ(2, callback_count_);
1911 EXPECT_EQ(0, error_callback_count_);
1912 EXPECT_TRUE(adapter_->IsDiscovering());
1913 EXPECT_FALSE(discovery_sessions_[0]->IsActive());
1914 EXPECT_FALSE(discovery_sessions_[1]->IsActive());
1915 EXPECT_TRUE(discovery_sessions_[2]->IsActive());
1916
1917 callback_count_ = 0;
1918
1919 // Request device discovery 3 times.
1920 for (int i = 0; i < 3; i++) {
1921 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter;
1922
1923 if (i == 0) {
1924 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter(
1925 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
1926 df->SetRSSI(-85);
1927 df->AddUUID(BluetoothUUID("1000"));
1928 discovery_filter.reset(df);
1929 } else if (i == 1) {
1930 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter(
1931 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
1932 df->SetRSSI(-60);
1933 df->AddUUID(BluetoothUUID("1020"));
1934 df->AddUUID(BluetoothUUID("1001"));
1935 discovery_filter.reset(df);
1936 } else if (i == 2) {
1937 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter(
1938 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
1939 df->SetRSSI(-65);
1940 df->AddUUID(BluetoothUUID("1020"));
1941 df->AddUUID(BluetoothUUID("1003"));
1942 discovery_filter.reset(df);
1943 }
1944
1945 adapter_->StartDiscoverySessionWithFilter(
1946 discovery_filter.Pass(),
1947 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
1948 base::Unretained(this)),
1949 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1950 base::Unretained(this)));
1951
1952 // each result in 1 requests.
1953 message_loop_.Run();
1954
1955 if (i == 0) {
1956 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1957 EXPECT_EQ("le", *filter->transport);
1958 EXPECT_EQ(-85, *filter->rssi);
1959 EXPECT_EQ(nullptr, filter->pathloss.get());
1960 std::vector<std::string> uuids = *filter->uuids;
1961 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1000"));
1962 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1003"));
1963 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1020"));
1964 } else if (i == 1 || i == 2) {
1965 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
1966 EXPECT_EQ("le", *filter->transport);
1967 EXPECT_EQ(-85, *filter->rssi);
1968 EXPECT_EQ(nullptr, filter->pathloss.get());
1969 std::vector<std::string> uuids = *filter->uuids;
1970 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1000"));
1971 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1001"));
1972 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1003"));
1973 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1020"));
1974 }
1975 }
1976
1977 // The success callback should have been called 3 times and the adapter should
1978 // still be discovering.
1979 EXPECT_EQ(3, callback_count_);
1980 EXPECT_EQ(0, error_callback_count_);
1981 EXPECT_TRUE(adapter_->IsDiscovering());
1982 ASSERT_EQ((size_t)6, discovery_sessions_.size());
1983
1984 callback_count_ = 0;
1985 // Request to stop discovery 4 times.
1986 for (int i = 2; i < 6; i++) {
1987 discovery_sessions_[i]->Stop(
1988 base::Bind(&BluetoothChromeOSTest::Callback, base::Unretained(this)),
1989 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
1990 base::Unretained(this)));
1991
1992 // filter no 2 is same as filter no 5, so removing it shouldn't cause any
1993 // filter update
1994 if (i != 2 && i != 5)
1995 message_loop_.Run();
1996 }
1997 // Run only once, as there should have been one D-Bus call.
1998 message_loop_.Run();
1999
2000 // The success callback should have been called 4 times and the adapter should
2001 // no longer be discovering.
2002 EXPECT_EQ(4, callback_count_);
2003 EXPECT_EQ(0, error_callback_count_);
2004 EXPECT_FALSE(adapter_->IsDiscovering());
2005 EXPECT_EQ(1, observer.discovering_changed_count());
2006
2007 // All discovery sessions should be inactive.
2008 for (int i = 0; i < 6; i++)
2009 EXPECT_FALSE(discovery_sessions_[i]->IsActive());
2010
2011 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
2012 EXPECT_EQ(nullptr, filter);
2013 }
2014
2015 // This unit test asserts that filter merging logic works correctly for filtered
2016 // discovery requests done via the BluetoothAdapter.
2017 TEST_F(BluetoothChromeOSTest, SetDiscoveryFilterMergingTest) {
2018 GetAdapter();
2019 adapter_->SetPowered(true, base::Bind(&BluetoothChromeOSTest::Callback,
2020 base::Unretained(this)),
2021 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
2022 base::Unretained(this)));
2023
2024 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter(
2025 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
2026 df->SetRSSI(-15);
2027 df->AddUUID(BluetoothUUID("1000"));
2028 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter(df);
2029
2030 adapter_->StartDiscoverySessionWithFilter(
2031 discovery_filter.Pass(),
2032 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
2033 base::Unretained(this)),
2034 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
2035 base::Unretained(this)));
2036
2037 message_loop_.Run();
2038
2039 auto filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
2040 EXPECT_EQ("le", *filter->transport);
2041 EXPECT_EQ(-15, *filter->rssi);
2042 EXPECT_EQ(nullptr, filter->pathloss.get());
2043 std::vector<std::string> uuids = *filter->uuids;
2044 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1000"));
2045
2046 df = new BluetoothDiscoveryFilter(
2047 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
2048 df->SetRSSI(-60);
2049 df->AddUUID(BluetoothUUID("1020"));
2050 df->AddUUID(BluetoothUUID("1001"));
2051 discovery_filter = scoped_ptr<BluetoothDiscoveryFilter>(df);
2052
2053 adapter_->StartDiscoverySessionWithFilter(
2054 discovery_filter.Pass(),
2055 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
2056 base::Unretained(this)),
2057 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
2058 base::Unretained(this)));
2059
2060 message_loop_.Run();
2061
2062 filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
2063 EXPECT_EQ("le", *filter->transport);
2064 EXPECT_EQ(-60, *filter->rssi);
2065 EXPECT_EQ(nullptr, filter->pathloss.get());
2066 uuids = *filter->uuids;
2067 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1000"));
2068 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1001"));
2069 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1020"));
2070
2071 BluetoothDiscoveryFilter* df3 = new BluetoothDiscoveryFilter(
2072 BluetoothDiscoveryFilter::Transport::TRANSPORT_CLASSIC);
2073 df3->SetRSSI(-65);
2074 df3->AddUUID(BluetoothUUID("1020"));
2075 df3->AddUUID(BluetoothUUID("1003"));
2076 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter3(df3);
2077
2078 adapter_->StartDiscoverySessionWithFilter(
2079 discovery_filter3.Pass(),
2080 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
2081 base::Unretained(this)),
2082 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
2083 base::Unretained(this)));
2084
2085 message_loop_.Run();
2086
2087 filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
2088 EXPECT_EQ("auto", *filter->transport);
2089 EXPECT_EQ(-65, *filter->rssi);
2090 EXPECT_EQ(nullptr, filter->pathloss.get());
2091 uuids = *filter->uuids;
2092 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1000"));
2093 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1001"));
2094 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1003"));
2095 EXPECT_NE(uuids.end(), std::find(uuids.begin(), uuids.end(), "1020"));
2096
2097 // start additionally classic scan
2098 adapter_->StartDiscoverySession(
2099 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
2100 base::Unretained(this)),
2101 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
2102 base::Unretained(this)));
2103
2104 message_loop_.Run();
2105
2106 filter = fake_bluetooth_adapter_client_->GetDiscoveryFilter();
2107 EXPECT_EQ("auto", *filter->transport);
2108 EXPECT_EQ(nullptr, filter->rssi.get());
2109 EXPECT_EQ(nullptr, filter->pathloss.get());
2110 EXPECT_EQ(nullptr, filter->uuids.get());
2111
2112 // Request to stop discovery 4 times.
2113 for (int i = 3; i >= 0; i--) {
2114 discovery_sessions_[i]->Stop(
2115 base::Bind(&BluetoothChromeOSTest::Callback, base::Unretained(this)),
2116 base::Bind(&BluetoothChromeOSTest::ErrorCallback,
2117 base::Unretained(this)));
2118
2119 // Every session stopping would trigger filter update
2120 message_loop_.Run();
2121 }
2122 }
2123
2124 TEST_F(BluetoothChromeOSTest, DeviceProperties) {
2125 GetAdapter();
2126
2127 BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
2128 ASSERT_EQ(2U, devices.size());
2129
2130 int idx = GetDeviceIndexByAddress(
2131 devices, bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress);
2132 ASSERT_NE(-1, idx);
2133 ASSERT_EQ(bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress,
2134 devices[idx]->GetAddress());
2135
2136 // Verify the other device properties.
2137 EXPECT_EQ(
2138 base::UTF8ToUTF16(bluez::FakeBluetoothDeviceClient::kPairedDeviceName),
2139 devices[idx]->GetName());
2140 EXPECT_EQ(BluetoothDevice::DEVICE_COMPUTER, devices[idx]->GetDeviceType());
2141 EXPECT_TRUE(devices[idx]->IsPaired());
2142 EXPECT_FALSE(devices[idx]->IsConnected());
2143 EXPECT_FALSE(devices[idx]->IsConnecting());
2144
2145 // Non HID devices are always connectable.
2146 EXPECT_TRUE(devices[idx]->IsConnectable());
2147
2148 BluetoothDevice::UUIDList uuids = devices[idx]->GetUUIDs();
2149 ASSERT_EQ(2U, uuids.size());
2150 EXPECT_EQ(uuids[0], BluetoothUUID("1800"));
2151 EXPECT_EQ(uuids[1], BluetoothUUID("1801"));
2152
2153 EXPECT_EQ(BluetoothDevice::VENDOR_ID_USB, devices[idx]->GetVendorIDSource());
2154 EXPECT_EQ(0x05ac, devices[idx]->GetVendorID());
2155 EXPECT_EQ(0x030d, devices[idx]->GetProductID());
2156 EXPECT_EQ(0x0306, devices[idx]->GetDeviceID());
2157 }
2158
2159 TEST_F(BluetoothChromeOSTest, DeviceClassChanged) {
2160 // Simulate a change of class of a device, as sometimes occurs
2161 // during discovery.
2162 GetAdapter();
2163
2164 BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
2165 ASSERT_EQ(2U, devices.size());
2166
2167 int idx = GetDeviceIndexByAddress(
2168 devices, bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress);
2169 ASSERT_NE(-1, idx);
2170 ASSERT_EQ(BluetoothDevice::DEVICE_COMPUTER, devices[idx]->GetDeviceType());
2171
2172 // Install an observer; expect the DeviceChanged method to be called when
2173 // we change the class of the device.
2174 TestBluetoothAdapterObserver observer(adapter_);
2175
2176 bluez::FakeBluetoothDeviceClient::Properties* properties =
2177 fake_bluetooth_device_client_->GetProperties(dbus::ObjectPath(
2178 bluez::FakeBluetoothDeviceClient::kPairedDevicePath));
2179
2180 properties->bluetooth_class.ReplaceValue(0x002580);
2181
2182 EXPECT_EQ(1, observer.device_changed_count());
2183 EXPECT_EQ(devices[idx], observer.last_device());
2184
2185 EXPECT_EQ(BluetoothDevice::DEVICE_MOUSE, devices[idx]->GetDeviceType());
2186 }
2187
2188 TEST_F(BluetoothChromeOSTest, DeviceNameChanged) {
2189 // Simulate a change of name of a device.
2190 GetAdapter();
2191
2192 BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
2193 ASSERT_EQ(2U, devices.size());
2194
2195 int idx = GetDeviceIndexByAddress(
2196 devices, bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress);
2197 ASSERT_NE(-1, idx);
2198 ASSERT_EQ(bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress,
2199 devices[idx]->GetAddress());
2200 ASSERT_EQ(
2201 base::UTF8ToUTF16(bluez::FakeBluetoothDeviceClient::kPairedDeviceName),
2202 devices[idx]->GetName());
2203
2204 // Install an observer; expect the DeviceChanged method to be called when
2205 // we change the alias of the device.
2206 TestBluetoothAdapterObserver observer(adapter_);
2207
2208 bluez::FakeBluetoothDeviceClient::Properties* properties =
2209 fake_bluetooth_device_client_->GetProperties(dbus::ObjectPath(
2210 bluez::FakeBluetoothDeviceClient::kPairedDevicePath));
2211
2212 static const std::string new_name("New Device Name");
2213 properties->alias.ReplaceValue(new_name);
2214
2215 EXPECT_EQ(1, observer.device_changed_count());
2216 EXPECT_EQ(devices[idx], observer.last_device());
2217
2218 EXPECT_EQ(base::UTF8ToUTF16(new_name), devices[idx]->GetName());
2219 }
2220
2221 TEST_F(BluetoothChromeOSTest, DeviceAddressChanged) {
2222 // Simulate a change of address of a device.
2223 GetAdapter();
2224
2225 BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
2226 ASSERT_EQ(2U, devices.size());
2227
2228 int idx = GetDeviceIndexByAddress(
2229 devices, bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress);
2230 ASSERT_NE(-1, idx);
2231 ASSERT_EQ(bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress,
2232 devices[idx]->GetAddress());
2233 ASSERT_EQ(
2234 base::UTF8ToUTF16(bluez::FakeBluetoothDeviceClient::kPairedDeviceName),
2235 devices[idx]->GetName());
2236
2237 // Install an observer; expect the DeviceAddressChanged method to be called
2238 // when we change the alias of the device.
2239 TestBluetoothAdapterObserver observer(adapter_);
2240
2241 bluez::FakeBluetoothDeviceClient::Properties* properties =
2242 fake_bluetooth_device_client_->GetProperties(dbus::ObjectPath(
2243 bluez::FakeBluetoothDeviceClient::kPairedDevicePath));
2244
2245 static const char* kNewAddress = "D9:1F:FC:11:22:33";
2246 properties->address.ReplaceValue(kNewAddress);
2247
2248 EXPECT_EQ(1, observer.device_address_changed_count());
2249 EXPECT_EQ(1, observer.device_changed_count());
2250 EXPECT_EQ(devices[idx], observer.last_device());
2251
2252 EXPECT_EQ(std::string(kNewAddress), devices[idx]->GetAddress());
2253 }
2254
2255 TEST_F(BluetoothChromeOSTest, DeviceUuidsChanged) {
2256 // Simulate a change of advertised services of a device.
2257 GetAdapter();
2258
2259 BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
2260 ASSERT_EQ(2U, devices.size());
2261
2262 int idx = GetDeviceIndexByAddress(
2263 devices, bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress);
2264 ASSERT_NE(-1, idx);
2265 ASSERT_EQ(bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress,
2266 devices[idx]->GetAddress());
2267
2268 BluetoothDevice::UUIDList uuids = devices[idx]->GetUUIDs();
2269 ASSERT_EQ(2U, uuids.size());
2270 ASSERT_EQ(uuids[0], BluetoothUUID("1800"));
2271 ASSERT_EQ(uuids[1], BluetoothUUID("1801"));
2272
2273 // Install an observer; expect the DeviceChanged method to be called when
2274 // we change the class of the device.
2275 TestBluetoothAdapterObserver observer(adapter_);
2276
2277 bluez::FakeBluetoothDeviceClient::Properties* properties =
2278 fake_bluetooth_device_client_->GetProperties(dbus::ObjectPath(
2279 bluez::FakeBluetoothDeviceClient::kPairedDevicePath));
2280
2281 std::vector<std::string> new_uuids;
2282 new_uuids.push_back(uuids[0].canonical_value());
2283 new_uuids.push_back(uuids[1].canonical_value());
2284 new_uuids.push_back("0000110c-0000-1000-8000-00805f9b34fb");
2285 new_uuids.push_back("0000110e-0000-1000-8000-00805f9b34fb");
2286 new_uuids.push_back("0000110a-0000-1000-8000-00805f9b34fb");
2287
2288 properties->uuids.ReplaceValue(new_uuids);
2289
2290 EXPECT_EQ(1, observer.device_changed_count());
2291 EXPECT_EQ(devices[idx], observer.last_device());
2292
2293 // Fetching the value should give the new one.
2294 uuids = devices[idx]->GetUUIDs();
2295 ASSERT_EQ(5U, uuids.size());
2296 EXPECT_EQ(uuids[0], BluetoothUUID("1800"));
2297 EXPECT_EQ(uuids[1], BluetoothUUID("1801"));
2298 EXPECT_EQ(uuids[2], BluetoothUUID("110c"));
2299 EXPECT_EQ(uuids[3], BluetoothUUID("110e"));
2300 EXPECT_EQ(uuids[4], BluetoothUUID("110a"));
2301 }
2302
2303 TEST_F(BluetoothChromeOSTest, DeviceInquiryRSSIInvalidated) {
2304 // Simulate invalidation of inquiry RSSI of a device, as it occurs
2305 // when discovery is finished.
2306 GetAdapter();
2307
2308 BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
2309 ASSERT_EQ(2U, devices.size());
2310
2311 int idx = GetDeviceIndexByAddress(
2312 devices, bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress);
2313 ASSERT_NE(-1, idx);
2314
2315 bluez::FakeBluetoothDeviceClient::Properties* properties =
2316 fake_bluetooth_device_client_->GetProperties(dbus::ObjectPath(
2317 bluez::FakeBluetoothDeviceClient::kPairedDevicePath));
2318
2319 // During discovery, rssi is a valid value (-75)
2320 properties->rssi.ReplaceValue(-75);
2321 properties->rssi.set_valid(true);
2322
2323 ASSERT_EQ(-75, devices[idx]->GetInquiryRSSI());
2324
2325 // Install an observer; expect the DeviceChanged method to be called when
2326 // we invalidate the RSSI of the device.
2327 TestBluetoothAdapterObserver observer(adapter_);
2328
2329 // When discovery is over, the value should be invalidated.
2330 properties->rssi.set_valid(false);
2331 properties->NotifyPropertyChanged(properties->rssi.name());
2332
2333 EXPECT_EQ(1, observer.device_changed_count());
2334 EXPECT_EQ(devices[idx], observer.last_device());
2335
2336 int unknown_power = BluetoothDevice::kUnknownPower;
2337 EXPECT_EQ(unknown_power, devices[idx]->GetInquiryRSSI());
2338 }
2339
2340 TEST_F(BluetoothChromeOSTest, DeviceInquiryTxPowerInvalidated) {
2341 // Simulate invalidation of inquiry TxPower of a device, as it occurs
2342 // when discovery is finished.
2343 GetAdapter();
2344
2345 BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
2346 ASSERT_EQ(2U, devices.size());
2347
2348 int idx = GetDeviceIndexByAddress(
2349 devices, bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress);
2350 ASSERT_NE(-1, idx);
2351
2352 bluez::FakeBluetoothDeviceClient::Properties* properties =
2353 fake_bluetooth_device_client_->GetProperties(dbus::ObjectPath(
2354 bluez::FakeBluetoothDeviceClient::kPairedDevicePath));
2355
2356 // During discovery, tx_power is a valid value (0)
2357 properties->tx_power.ReplaceValue(0);
2358 properties->tx_power.set_valid(true);
2359
2360 ASSERT_EQ(0, devices[idx]->GetInquiryTxPower());
2361
2362 // Install an observer; expect the DeviceChanged method to be called when
2363 // we invalidate the tx_power of the device.
2364 TestBluetoothAdapterObserver observer(adapter_);
2365
2366 // When discovery is over, the value should be invalidated.
2367 properties->tx_power.set_valid(false);
2368 properties->NotifyPropertyChanged(properties->tx_power.name());
2369
2370 EXPECT_EQ(1, observer.device_changed_count());
2371 EXPECT_EQ(devices[idx], observer.last_device());
2372
2373 int unknown_power = BluetoothDevice::kUnknownPower;
2374 EXPECT_EQ(unknown_power, devices[idx]->GetInquiryTxPower());
2375 }
2376
2377 TEST_F(BluetoothChromeOSTest, ForgetDevice) {
2378 GetAdapter();
2379
2380 BluetoothAdapter::DeviceList devices = adapter_->GetDevices();
2381 ASSERT_EQ(2U, devices.size());
2382
2383 int idx = GetDeviceIndexByAddress(
2384 devices, bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress);
2385 ASSERT_NE(-1, idx);
2386 ASSERT_EQ(bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress,
2387 devices[idx]->GetAddress());
2388
2389 std::string address = devices[idx]->GetAddress();
2390
2391 // Install an observer; expect the DeviceRemoved method to be called
2392 // with the device we remove.
2393 TestBluetoothAdapterObserver observer(adapter_);
2394
2395 devices[idx]->Forget(GetErrorCallback());
2396 EXPECT_EQ(0, error_callback_count_);
2397
2398 EXPECT_EQ(1, observer.device_removed_count());
2399 EXPECT_EQ(address, observer.last_device_address());
2400
2401 // GetDevices shouldn't return the device either.
2402 devices = adapter_->GetDevices();
2403 ASSERT_EQ(1U, devices.size());
2404 }
2405
2406 TEST_F(BluetoothChromeOSTest, ForgetUnpairedDevice) {
2407 GetAdapter();
2408 DiscoverDevices();
2409
2410 BluetoothDevice* device = adapter_->GetDevice(
2411 bluez::FakeBluetoothDeviceClient::kConnectUnpairableAddress);
2412 ASSERT_TRUE(device != nullptr);
2413 ASSERT_FALSE(device->IsPaired());
2414
2415 // Connect the device so it becomes trusted and remembered.
2416 device->Connect(nullptr, GetCallback(),
2417 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2418 base::Unretained(this)));
2419
2420 ASSERT_EQ(1, callback_count_);
2421 ASSERT_EQ(0, error_callback_count_);
2422 callback_count_ = 0;
2423
2424 ASSERT_TRUE(device->IsConnected());
2425 ASSERT_FALSE(device->IsConnecting());
2426
2427 // Make sure the trusted property has been set to true.
2428 bluez::FakeBluetoothDeviceClient::Properties* properties =
2429 fake_bluetooth_device_client_->GetProperties(dbus::ObjectPath(
2430 bluez::FakeBluetoothDeviceClient::kConnectUnpairablePath));
2431 ASSERT_TRUE(properties->trusted.value());
2432
2433 // Install an observer; expect the DeviceRemoved method to be called
2434 // with the device we remove.
2435 TestBluetoothAdapterObserver observer(adapter_);
2436
2437 device->Forget(GetErrorCallback());
2438 EXPECT_EQ(0, error_callback_count_);
2439
2440 EXPECT_EQ(1, observer.device_removed_count());
2441 EXPECT_EQ(bluez::FakeBluetoothDeviceClient::kConnectUnpairableAddress,
2442 observer.last_device_address());
2443
2444 // GetDevices shouldn't return the device either.
2445 device = adapter_->GetDevice(
2446 bluez::FakeBluetoothDeviceClient::kConnectUnpairableAddress);
2447 EXPECT_FALSE(device != nullptr);
2448 }
2449
2450 TEST_F(BluetoothChromeOSTest, ConnectPairedDevice) {
2451 GetAdapter();
2452
2453 BluetoothDevice* device = adapter_->GetDevice(
2454 bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress);
2455 ASSERT_TRUE(device != nullptr);
2456 ASSERT_TRUE(device->IsPaired());
2457
2458 TestBluetoothAdapterObserver observer(adapter_);
2459
2460 // Connect without a pairing delegate; since the device is already Paired
2461 // this should succeed and the device should become connected.
2462 device->Connect(nullptr, GetCallback(),
2463 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2464 base::Unretained(this)));
2465
2466 EXPECT_EQ(1, callback_count_);
2467 EXPECT_EQ(0, error_callback_count_);
2468
2469 // Two changes for connecting, one for connected and one for for trusted
2470 // after connecting.
2471 EXPECT_EQ(4, observer.device_changed_count());
2472 EXPECT_EQ(device, observer.last_device());
2473
2474 EXPECT_TRUE(device->IsConnected());
2475 EXPECT_FALSE(device->IsConnecting());
2476 }
2477
2478 TEST_F(BluetoothChromeOSTest, ConnectUnpairableDevice) {
2479 GetAdapter();
2480 DiscoverDevices();
2481
2482 BluetoothDevice* device = adapter_->GetDevice(
2483 bluez::FakeBluetoothDeviceClient::kConnectUnpairableAddress);
2484 ASSERT_TRUE(device != nullptr);
2485 ASSERT_FALSE(device->IsPaired());
2486
2487 TestBluetoothAdapterObserver observer(adapter_);
2488
2489 // Connect without a pairing delegate; since the device does not require
2490 // pairing, this should succeed and the device should become connected.
2491 device->Connect(nullptr, GetCallback(),
2492 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2493 base::Unretained(this)));
2494
2495 EXPECT_EQ(1, callback_count_);
2496 EXPECT_EQ(0, error_callback_count_);
2497
2498 // Two changes for connecting, one for connected, one for for trusted after
2499 // connection, and one for the reconnect mode (IsConnectable).
2500 EXPECT_EQ(5, observer.device_changed_count());
2501 EXPECT_EQ(device, observer.last_device());
2502
2503 EXPECT_TRUE(device->IsConnected());
2504 EXPECT_FALSE(device->IsConnecting());
2505
2506 // Make sure the trusted property has been set to true.
2507 bluez::FakeBluetoothDeviceClient::Properties* properties =
2508 fake_bluetooth_device_client_->GetProperties(dbus::ObjectPath(
2509 bluez::FakeBluetoothDeviceClient::kConnectUnpairablePath));
2510 EXPECT_TRUE(properties->trusted.value());
2511
2512 // Verify is a HID device and is not connectable.
2513 BluetoothDevice::UUIDList uuids = device->GetUUIDs();
2514 ASSERT_EQ(1U, uuids.size());
2515 EXPECT_EQ(uuids[0], BluetoothUUID("1124"));
2516 EXPECT_FALSE(device->IsConnectable());
2517 }
2518
2519 TEST_F(BluetoothChromeOSTest, ConnectConnectedDevice) {
2520 GetAdapter();
2521
2522 BluetoothDevice* device = adapter_->GetDevice(
2523 bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress);
2524 ASSERT_TRUE(device != nullptr);
2525 ASSERT_TRUE(device->IsPaired());
2526
2527 device->Connect(nullptr, GetCallback(),
2528 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2529 base::Unretained(this)));
2530
2531 ASSERT_EQ(1, callback_count_);
2532 ASSERT_EQ(0, error_callback_count_);
2533 callback_count_ = 0;
2534
2535 ASSERT_TRUE(device->IsConnected());
2536
2537 // Connect again; since the device is already Connected, this shouldn't do
2538 // anything to initiate the connection.
2539 TestBluetoothAdapterObserver observer(adapter_);
2540
2541 device->Connect(nullptr, GetCallback(),
2542 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2543 base::Unretained(this)));
2544
2545 EXPECT_EQ(1, callback_count_);
2546 EXPECT_EQ(0, error_callback_count_);
2547
2548 // The observer will be called because Connecting will toggle true and false,
2549 // and the trusted property will be updated to true.
2550 EXPECT_EQ(3, observer.device_changed_count());
2551
2552 EXPECT_TRUE(device->IsConnected());
2553 EXPECT_FALSE(device->IsConnecting());
2554 }
2555
2556 TEST_F(BluetoothChromeOSTest, ConnectDeviceFails) {
2557 GetAdapter();
2558 DiscoverDevices();
2559
2560 BluetoothDevice* device = adapter_->GetDevice(
2561 bluez::FakeBluetoothDeviceClient::kLegacyAutopairAddress);
2562 ASSERT_TRUE(device != nullptr);
2563 ASSERT_FALSE(device->IsPaired());
2564
2565 TestBluetoothAdapterObserver observer(adapter_);
2566
2567 // Connect without a pairing delegate; since the device requires pairing,
2568 // this should fail with an error.
2569 device->Connect(nullptr, GetCallback(),
2570 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2571 base::Unretained(this)));
2572
2573 EXPECT_EQ(0, callback_count_);
2574 EXPECT_EQ(1, error_callback_count_);
2575 EXPECT_EQ(BluetoothDevice::ERROR_FAILED, last_connect_error_);
2576
2577 EXPECT_EQ(2, observer.device_changed_count());
2578
2579 EXPECT_FALSE(device->IsConnected());
2580 EXPECT_FALSE(device->IsConnecting());
2581 }
2582
2583 TEST_F(BluetoothChromeOSTest, DisconnectDevice) {
2584 GetAdapter();
2585
2586 BluetoothDevice* device = adapter_->GetDevice(
2587 bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress);
2588 ASSERT_TRUE(device != nullptr);
2589 ASSERT_TRUE(device->IsPaired());
2590
2591 device->Connect(nullptr, GetCallback(),
2592 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2593 base::Unretained(this)));
2594
2595 ASSERT_EQ(1, callback_count_);
2596 ASSERT_EQ(0, error_callback_count_);
2597 callback_count_ = 0;
2598
2599 ASSERT_TRUE(device->IsConnected());
2600 ASSERT_FALSE(device->IsConnecting());
2601
2602 // Disconnect the device, we should see the observer method fire and the
2603 // device get dropped.
2604 TestBluetoothAdapterObserver observer(adapter_);
2605
2606 device->Disconnect(GetCallback(), GetErrorCallback());
2607
2608 EXPECT_EQ(1, callback_count_);
2609 EXPECT_EQ(0, error_callback_count_);
2610
2611 EXPECT_EQ(1, observer.device_changed_count());
2612 EXPECT_EQ(device, observer.last_device());
2613
2614 EXPECT_FALSE(device->IsConnected());
2615 }
2616
2617 TEST_F(BluetoothChromeOSTest, DisconnectUnconnectedDevice) {
2618 GetAdapter();
2619
2620 BluetoothDevice* device = adapter_->GetDevice(
2621 bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress);
2622 ASSERT_TRUE(device != nullptr);
2623 ASSERT_TRUE(device->IsPaired());
2624 ASSERT_FALSE(device->IsConnected());
2625
2626 // Disconnect the device, we should see the observer method fire and the
2627 // device get dropped.
2628 TestBluetoothAdapterObserver observer(adapter_);
2629
2630 device->Disconnect(GetCallback(), GetErrorCallback());
2631
2632 EXPECT_EQ(0, callback_count_);
2633 EXPECT_EQ(1, error_callback_count_);
2634
2635 EXPECT_EQ(0, observer.device_changed_count());
2636
2637 EXPECT_FALSE(device->IsConnected());
2638 }
2639
2640 TEST_F(BluetoothChromeOSTest, PairTrustedDevice) {
2641 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2642 GetAdapter();
2643
2644 fake_bluetooth_device_client_->CreateDevice(
2645 dbus::ObjectPath(bluez::FakeBluetoothAdapterClient::kAdapterPath),
2646 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::
2647 kConnectedTrustedNotPairedDevicePath));
2648 BluetoothDevice* device =
2649 adapter_->GetDevice(bluez::FakeBluetoothDeviceClient::
2650 kConnectedTrustedNotPairedDeviceAddress);
2651 ASSERT_TRUE(device != nullptr);
2652
2653 // On the DBus level the device is trusted but not paired. But the current
2654 // implementation of |BluetoothDevice::IsPaired()| returns true in this case.
2655 bluez::FakeBluetoothDeviceClient::Properties* properties =
2656 fake_bluetooth_device_client_->GetProperties(
2657 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::
2658 kConnectedTrustedNotPairedDevicePath));
2659 EXPECT_FALSE(properties->paired.value());
2660 EXPECT_TRUE(properties->trusted.value());
2661 ASSERT_TRUE(device->IsPaired());
2662
2663 // The |kConnectedTrustedNotPairedDevicePath| requests a passkey confirmation.
2664 // Obs.: This is the flow when CrOS triggers pairing with a iOS device.
2665 TestBluetoothAdapterObserver observer(adapter_);
2666 TestPairingDelegate pairing_delegate;
2667 adapter_->AddPairingDelegate(
2668 &pairing_delegate, BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH);
2669 device->Pair(&pairing_delegate, GetCallback(),
2670 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2671 base::Unretained(this)));
2672 EXPECT_EQ(1, pairing_delegate.call_count_);
2673 EXPECT_EQ(1, pairing_delegate.confirm_passkey_count_);
2674 EXPECT_EQ(123456U, pairing_delegate.last_passkey_);
2675
2676 // Confirm the passkey.
2677 device->ConfirmPairing();
2678 message_loop_.Run();
2679 EXPECT_EQ(1, callback_count_);
2680 EXPECT_EQ(0, error_callback_count_);
2681
2682 // Make sure the paired property has been set to true.
2683 properties = fake_bluetooth_device_client_->GetProperties(dbus::ObjectPath(
2684 bluez::FakeBluetoothDeviceClient::kConnectedTrustedNotPairedDevicePath));
2685 EXPECT_TRUE(properties->paired.value());
2686 }
2687
2688 TEST_F(BluetoothChromeOSTest, PairAlreadyPairedDevice) {
2689 GetAdapter();
2690
2691 fake_bluetooth_device_client_->CreateDevice(
2692 dbus::ObjectPath(bluez::FakeBluetoothAdapterClient::kAdapterPath),
2693 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kPairedDevicePath));
2694 BluetoothDevice* device = adapter_->GetDevice(
2695 bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress);
2696 ASSERT_TRUE(device != nullptr);
2697
2698 // On the DBus level a device can be trusted but not paired.
2699 bluez::FakeBluetoothDeviceClient::Properties* properties =
2700 fake_bluetooth_device_client_->GetProperties(dbus::ObjectPath(
2701 bluez::FakeBluetoothDeviceClient::kPairedDevicePath));
2702 EXPECT_TRUE(properties->paired.value());
2703 EXPECT_TRUE(properties->trusted.value());
2704 ASSERT_TRUE(device->IsPaired());
2705
2706 TestBluetoothAdapterObserver observer(adapter_);
2707 TestPairingDelegate pairing_delegate;
2708 adapter_->AddPairingDelegate(
2709 &pairing_delegate, BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH);
2710 device->Pair(&pairing_delegate, GetCallback(),
2711 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2712 base::Unretained(this)));
2713
2714 // For already paired devices a call to |Pair| should succeed without calling
2715 // the pairing delegate.
2716 EXPECT_EQ(0, pairing_delegate.call_count_);
2717 EXPECT_EQ(1, callback_count_);
2718 EXPECT_EQ(0, error_callback_count_);
2719 }
2720
2721 TEST_F(BluetoothChromeOSTest, PairLegacyAutopair) {
2722 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2723
2724 GetAdapter();
2725 DiscoverDevices();
2726
2727 // The Legacy Autopair device requires no PIN or Passkey to pair because
2728 // the daemon provides 0000 to the device for us.
2729 BluetoothDevice* device = adapter_->GetDevice(
2730 bluez::FakeBluetoothDeviceClient::kLegacyAutopairAddress);
2731 ASSERT_TRUE(device != nullptr);
2732 ASSERT_FALSE(device->IsPaired());
2733
2734 TestBluetoothAdapterObserver observer(adapter_);
2735
2736 TestPairingDelegate pairing_delegate;
2737 device->Connect(&pairing_delegate, GetCallback(),
2738 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2739 base::Unretained(this)));
2740
2741 EXPECT_EQ(0, pairing_delegate.call_count_);
2742 EXPECT_TRUE(device->IsConnecting());
2743
2744 message_loop_.Run();
2745
2746 EXPECT_EQ(1, callback_count_);
2747 EXPECT_EQ(0, error_callback_count_);
2748
2749 // Two changes for connecting, one change for connected, one for paired,
2750 // two for trusted (after pairing and connection), and one for the reconnect
2751 // mode (IsConnectable).
2752 EXPECT_EQ(7, observer.device_changed_count());
2753 EXPECT_EQ(device, observer.last_device());
2754
2755 EXPECT_TRUE(device->IsConnected());
2756 EXPECT_FALSE(device->IsConnecting());
2757
2758 EXPECT_TRUE(device->IsPaired());
2759
2760 // Verify is a HID device and is connectable.
2761 BluetoothDevice::UUIDList uuids = device->GetUUIDs();
2762 ASSERT_EQ(1U, uuids.size());
2763 EXPECT_EQ(uuids[0], BluetoothUUID("1124"));
2764 EXPECT_TRUE(device->IsConnectable());
2765
2766 // Make sure the trusted property has been set to true.
2767 bluez::FakeBluetoothDeviceClient::Properties* properties =
2768 fake_bluetooth_device_client_->GetProperties(dbus::ObjectPath(
2769 bluez::FakeBluetoothDeviceClient::kLegacyAutopairPath));
2770 EXPECT_TRUE(properties->trusted.value());
2771 }
2772
2773 TEST_F(BluetoothChromeOSTest, PairDisplayPinCode) {
2774 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2775
2776 GetAdapter();
2777 DiscoverDevices();
2778
2779 // Requires that we display a randomly generated PIN on the screen.
2780 BluetoothDevice* device = adapter_->GetDevice(
2781 bluez::FakeBluetoothDeviceClient::kDisplayPinCodeAddress);
2782 ASSERT_TRUE(device != nullptr);
2783 ASSERT_FALSE(device->IsPaired());
2784
2785 TestBluetoothAdapterObserver observer(adapter_);
2786
2787 TestPairingDelegate pairing_delegate;
2788 device->Connect(&pairing_delegate, GetCallback(),
2789 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2790 base::Unretained(this)));
2791
2792 EXPECT_EQ(1, pairing_delegate.call_count_);
2793 EXPECT_EQ(1, pairing_delegate.display_pincode_count_);
2794 EXPECT_EQ("123456", pairing_delegate.last_pincode_);
2795 EXPECT_TRUE(device->IsConnecting());
2796
2797 message_loop_.Run();
2798
2799 EXPECT_EQ(1, callback_count_);
2800 EXPECT_EQ(0, error_callback_count_);
2801
2802 // Two changes for connecting, one change for connected, one for paired,
2803 // two for trusted (after pairing and connection), and one for the reconnect
2804 // mode (IsConnectable).
2805 EXPECT_EQ(7, observer.device_changed_count());
2806 EXPECT_EQ(device, observer.last_device());
2807
2808 EXPECT_TRUE(device->IsConnected());
2809 EXPECT_FALSE(device->IsConnecting());
2810
2811 EXPECT_TRUE(device->IsPaired());
2812
2813 // Verify is a HID device and is connectable.
2814 BluetoothDevice::UUIDList uuids = device->GetUUIDs();
2815 ASSERT_EQ(1U, uuids.size());
2816 EXPECT_EQ(uuids[0], BluetoothUUID("1124"));
2817 EXPECT_TRUE(device->IsConnectable());
2818
2819 // Make sure the trusted property has been set to true.
2820 bluez::FakeBluetoothDeviceClient::Properties* properties =
2821 fake_bluetooth_device_client_->GetProperties(dbus::ObjectPath(
2822 bluez::FakeBluetoothDeviceClient::kDisplayPinCodePath));
2823 EXPECT_TRUE(properties->trusted.value());
2824 }
2825
2826 TEST_F(BluetoothChromeOSTest, PairDisplayPasskey) {
2827 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2828
2829 GetAdapter();
2830 DiscoverDevices();
2831
2832 // Requires that we display a randomly generated Passkey on the screen,
2833 // and notifies us as it's typed in.
2834 BluetoothDevice* device = adapter_->GetDevice(
2835 bluez::FakeBluetoothDeviceClient::kDisplayPasskeyAddress);
2836 ASSERT_TRUE(device != nullptr);
2837 ASSERT_FALSE(device->IsPaired());
2838
2839 TestBluetoothAdapterObserver observer(adapter_);
2840
2841 TestPairingDelegate pairing_delegate;
2842 device->Connect(&pairing_delegate, GetCallback(),
2843 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2844 base::Unretained(this)));
2845
2846 // One call for DisplayPasskey() and one for KeysEntered().
2847 EXPECT_EQ(2, pairing_delegate.call_count_);
2848 EXPECT_EQ(1, pairing_delegate.display_passkey_count_);
2849 EXPECT_EQ(123456U, pairing_delegate.last_passkey_);
2850 EXPECT_EQ(1, pairing_delegate.keys_entered_count_);
2851 EXPECT_EQ(0U, pairing_delegate.last_entered_);
2852
2853 EXPECT_TRUE(device->IsConnecting());
2854
2855 // One call to KeysEntered() for each key, including [enter].
2856 for (int i = 1; i <= 7; ++i) {
2857 message_loop_.Run();
2858
2859 EXPECT_EQ(2 + i, pairing_delegate.call_count_);
2860 EXPECT_EQ(1 + i, pairing_delegate.keys_entered_count_);
2861 EXPECT_EQ(static_cast<uint32_t>(i), pairing_delegate.last_entered_);
2862 }
2863
2864 message_loop_.Run();
2865
2866 // 8 KeysEntered notifications (0 to 7, inclusive) and one aditional call for
2867 // DisplayPasskey().
2868 EXPECT_EQ(9, pairing_delegate.call_count_);
2869 EXPECT_EQ(8, pairing_delegate.keys_entered_count_);
2870 EXPECT_EQ(7U, pairing_delegate.last_entered_);
2871
2872 EXPECT_EQ(1, callback_count_);
2873 EXPECT_EQ(0, error_callback_count_);
2874
2875 // Two changes for connecting, one change for connected, one for paired,
2876 // two for trusted (after pairing and connection), and one for the reconnect
2877 // mode (IsConnectable).
2878 EXPECT_EQ(7, observer.device_changed_count());
2879 EXPECT_EQ(device, observer.last_device());
2880
2881 EXPECT_TRUE(device->IsConnected());
2882 EXPECT_FALSE(device->IsConnecting());
2883
2884 EXPECT_TRUE(device->IsPaired());
2885
2886 // Verify is a HID device.
2887 BluetoothDevice::UUIDList uuids = device->GetUUIDs();
2888 ASSERT_EQ(1U, uuids.size());
2889 EXPECT_EQ(uuids[0], BluetoothUUID("1124"));
2890
2891 // And usually not connectable.
2892 EXPECT_FALSE(device->IsConnectable());
2893
2894 // Make sure the trusted property has been set to true.
2895 bluez::FakeBluetoothDeviceClient::Properties* properties =
2896 fake_bluetooth_device_client_->GetProperties(dbus::ObjectPath(
2897 bluez::FakeBluetoothDeviceClient::kDisplayPasskeyPath));
2898 EXPECT_TRUE(properties->trusted.value());
2899 }
2900
2901 TEST_F(BluetoothChromeOSTest, PairRequestPinCode) {
2902 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2903
2904 GetAdapter();
2905 DiscoverDevices();
2906
2907 // Requires that the user enters a PIN for them.
2908 BluetoothDevice* device = adapter_->GetDevice(
2909 bluez::FakeBluetoothDeviceClient::kRequestPinCodeAddress);
2910 ASSERT_TRUE(device != nullptr);
2911 ASSERT_FALSE(device->IsPaired());
2912
2913 TestBluetoothAdapterObserver observer(adapter_);
2914
2915 TestPairingDelegate pairing_delegate;
2916 device->Connect(&pairing_delegate, GetCallback(),
2917 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2918 base::Unretained(this)));
2919
2920 EXPECT_EQ(1, pairing_delegate.call_count_);
2921 EXPECT_EQ(1, pairing_delegate.request_pincode_count_);
2922 EXPECT_TRUE(device->IsConnecting());
2923
2924 // Set the PIN.
2925 device->SetPinCode("1234");
2926 message_loop_.Run();
2927
2928 EXPECT_EQ(1, callback_count_);
2929 EXPECT_EQ(0, error_callback_count_);
2930
2931 // Two changes for connecting, one change for connected, one for paired and
2932 // two for trusted (after pairing and connection).
2933 EXPECT_EQ(6, observer.device_changed_count());
2934 EXPECT_EQ(device, observer.last_device());
2935
2936 EXPECT_TRUE(device->IsConnected());
2937 EXPECT_FALSE(device->IsConnecting());
2938
2939 EXPECT_TRUE(device->IsPaired());
2940
2941 // Verify is not a HID device.
2942 BluetoothDevice::UUIDList uuids = device->GetUUIDs();
2943 ASSERT_EQ(0U, uuids.size());
2944
2945 // Non HID devices are always connectable.
2946 EXPECT_TRUE(device->IsConnectable());
2947
2948 // Make sure the trusted property has been set to true.
2949 bluez::FakeBluetoothDeviceClient::Properties* properties =
2950 fake_bluetooth_device_client_->GetProperties(dbus::ObjectPath(
2951 bluez::FakeBluetoothDeviceClient::kRequestPinCodePath));
2952 EXPECT_TRUE(properties->trusted.value());
2953 }
2954
2955 TEST_F(BluetoothChromeOSTest, PairConfirmPasskey) {
2956 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
2957
2958 GetAdapter();
2959 DiscoverDevices();
2960
2961 // Requests that we confirm a displayed passkey.
2962 BluetoothDevice* device = adapter_->GetDevice(
2963 bluez::FakeBluetoothDeviceClient::kConfirmPasskeyAddress);
2964 ASSERT_TRUE(device != nullptr);
2965 ASSERT_FALSE(device->IsPaired());
2966
2967 TestBluetoothAdapterObserver observer(adapter_);
2968
2969 TestPairingDelegate pairing_delegate;
2970 device->Connect(&pairing_delegate, GetCallback(),
2971 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
2972 base::Unretained(this)));
2973
2974 EXPECT_EQ(1, pairing_delegate.call_count_);
2975 EXPECT_EQ(1, pairing_delegate.confirm_passkey_count_);
2976 EXPECT_EQ(123456U, pairing_delegate.last_passkey_);
2977 EXPECT_TRUE(device->IsConnecting());
2978
2979 // Confirm the passkey.
2980 device->ConfirmPairing();
2981 message_loop_.Run();
2982
2983 EXPECT_EQ(1, callback_count_);
2984 EXPECT_EQ(0, error_callback_count_);
2985
2986 // Two changes for connecting, one change for connected, one for paired and
2987 // two for trusted (after pairing and connection).
2988 EXPECT_EQ(6, observer.device_changed_count());
2989 EXPECT_EQ(device, observer.last_device());
2990
2991 EXPECT_TRUE(device->IsConnected());
2992 EXPECT_FALSE(device->IsConnecting());
2993
2994 EXPECT_TRUE(device->IsPaired());
2995
2996 // Non HID devices are always connectable.
2997 EXPECT_TRUE(device->IsConnectable());
2998
2999 // Make sure the trusted property has been set to true.
3000 bluez::FakeBluetoothDeviceClient::Properties* properties =
3001 fake_bluetooth_device_client_->GetProperties(dbus::ObjectPath(
3002 bluez::FakeBluetoothDeviceClient::kConfirmPasskeyPath));
3003 EXPECT_TRUE(properties->trusted.value());
3004 }
3005
3006 TEST_F(BluetoothChromeOSTest, PairRequestPasskey) {
3007 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3008
3009 GetAdapter();
3010 DiscoverDevices();
3011
3012 // Requires that the user enters a Passkey, this would be some kind of
3013 // device that has a display, but doesn't use "just works" - maybe a car?
3014 BluetoothDevice* device = adapter_->GetDevice(
3015 bluez::FakeBluetoothDeviceClient::kRequestPasskeyAddress);
3016 ASSERT_TRUE(device != nullptr);
3017 ASSERT_FALSE(device->IsPaired());
3018
3019 TestBluetoothAdapterObserver observer(adapter_);
3020
3021 TestPairingDelegate pairing_delegate;
3022 device->Connect(&pairing_delegate, GetCallback(),
3023 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
3024 base::Unretained(this)));
3025
3026 EXPECT_EQ(1, pairing_delegate.call_count_);
3027 EXPECT_EQ(1, pairing_delegate.request_passkey_count_);
3028 EXPECT_TRUE(device->IsConnecting());
3029
3030 // Set the Passkey.
3031 device->SetPasskey(1234);
3032 message_loop_.Run();
3033
3034 EXPECT_EQ(1, callback_count_);
3035 EXPECT_EQ(0, error_callback_count_);
3036
3037 // Two changes for connecting, one change for connected, one for paired and
3038 // two for trusted (after pairing and connection).
3039 EXPECT_EQ(6, observer.device_changed_count());
3040 EXPECT_EQ(device, observer.last_device());
3041
3042 EXPECT_TRUE(device->IsConnected());
3043 EXPECT_FALSE(device->IsConnecting());
3044
3045 EXPECT_TRUE(device->IsPaired());
3046
3047 // Non HID devices are always connectable.
3048 EXPECT_TRUE(device->IsConnectable());
3049
3050 // Make sure the trusted property has been set to true.
3051 bluez::FakeBluetoothDeviceClient::Properties* properties =
3052 fake_bluetooth_device_client_->GetProperties(dbus::ObjectPath(
3053 bluez::FakeBluetoothDeviceClient::kRequestPasskeyPath));
3054 EXPECT_TRUE(properties->trusted.value());
3055 }
3056
3057 TEST_F(BluetoothChromeOSTest, PairJustWorks) {
3058 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3059
3060 GetAdapter();
3061 DiscoverDevices();
3062
3063 // Uses just-works pairing, since this is an outgoing pairing, no delegate
3064 // interaction is required.
3065 BluetoothDevice* device =
3066 adapter_->GetDevice(bluez::FakeBluetoothDeviceClient::kJustWorksAddress);
3067 ASSERT_TRUE(device != nullptr);
3068 ASSERT_FALSE(device->IsPaired());
3069
3070 TestBluetoothAdapterObserver observer(adapter_);
3071
3072 TestPairingDelegate pairing_delegate;
3073 device->Connect(&pairing_delegate, GetCallback(),
3074 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
3075 base::Unretained(this)));
3076
3077 EXPECT_EQ(0, pairing_delegate.call_count_);
3078
3079 message_loop_.Run();
3080
3081 EXPECT_EQ(1, callback_count_);
3082 EXPECT_EQ(0, error_callback_count_);
3083
3084 // Two changes for connecting, one change for connected, one for paired and
3085 // two for trusted (after pairing and connection).
3086 EXPECT_EQ(6, observer.device_changed_count());
3087 EXPECT_EQ(device, observer.last_device());
3088
3089 EXPECT_TRUE(device->IsConnected());
3090 EXPECT_FALSE(device->IsConnecting());
3091
3092 EXPECT_TRUE(device->IsPaired());
3093
3094 // Non HID devices are always connectable.
3095 EXPECT_TRUE(device->IsConnectable());
3096
3097 // Make sure the trusted property has been set to true.
3098 bluez::FakeBluetoothDeviceClient::Properties* properties =
3099 fake_bluetooth_device_client_->GetProperties(
3100 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kJustWorksPath));
3101 EXPECT_TRUE(properties->trusted.value());
3102 }
3103
3104 TEST_F(BluetoothChromeOSTest, PairUnpairableDeviceFails) {
3105 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3106
3107 GetAdapter();
3108 DiscoverDevice(bluez::FakeBluetoothDeviceClient::kUnconnectableDeviceAddress);
3109
3110 BluetoothDevice* device = adapter_->GetDevice(
3111 bluez::FakeBluetoothDeviceClient::kUnpairableDeviceAddress);
3112 ASSERT_TRUE(device != nullptr);
3113 ASSERT_FALSE(device->IsPaired());
3114
3115 TestBluetoothAdapterObserver observer(adapter_);
3116
3117 TestPairingDelegate pairing_delegate;
3118 device->Connect(&pairing_delegate, GetCallback(),
3119 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
3120 base::Unretained(this)));
3121
3122 EXPECT_EQ(0, pairing_delegate.call_count_);
3123 EXPECT_TRUE(device->IsConnecting());
3124
3125 // Run the loop to get the error..
3126 message_loop_.Run();
3127
3128 EXPECT_EQ(0, callback_count_);
3129 EXPECT_EQ(1, error_callback_count_);
3130
3131 EXPECT_EQ(BluetoothDevice::ERROR_FAILED, last_connect_error_);
3132
3133 EXPECT_FALSE(device->IsConnected());
3134 EXPECT_FALSE(device->IsConnecting());
3135 EXPECT_FALSE(device->IsPaired());
3136 }
3137
3138 TEST_F(BluetoothChromeOSTest, PairingFails) {
3139 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3140
3141 GetAdapter();
3142 DiscoverDevice(bluez::FakeBluetoothDeviceClient::kVanishingDeviceAddress);
3143
3144 // The vanishing device times out during pairing
3145 BluetoothDevice* device = adapter_->GetDevice(
3146 bluez::FakeBluetoothDeviceClient::kVanishingDeviceAddress);
3147 ASSERT_TRUE(device != nullptr);
3148 ASSERT_FALSE(device->IsPaired());
3149
3150 TestBluetoothAdapterObserver observer(adapter_);
3151
3152 TestPairingDelegate pairing_delegate;
3153 device->Connect(&pairing_delegate, GetCallback(),
3154 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
3155 base::Unretained(this)));
3156
3157 EXPECT_EQ(0, pairing_delegate.call_count_);
3158 EXPECT_TRUE(device->IsConnecting());
3159
3160 // Run the loop to get the error..
3161 message_loop_.Run();
3162
3163 EXPECT_EQ(0, callback_count_);
3164 EXPECT_EQ(1, error_callback_count_);
3165
3166 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_TIMEOUT, last_connect_error_);
3167
3168 EXPECT_FALSE(device->IsConnected());
3169 EXPECT_FALSE(device->IsConnecting());
3170 EXPECT_FALSE(device->IsPaired());
3171 }
3172
3173 TEST_F(BluetoothChromeOSTest, PairingFailsAtConnection) {
3174 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3175
3176 GetAdapter();
3177 DiscoverDevices();
3178
3179 // Everything seems to go according to plan with the unconnectable device;
3180 // it pairs, but then you can't make connections to it after.
3181 BluetoothDevice* device = adapter_->GetDevice(
3182 bluez::FakeBluetoothDeviceClient::kUnconnectableDeviceAddress);
3183 ASSERT_TRUE(device != nullptr);
3184 ASSERT_FALSE(device->IsPaired());
3185
3186 TestBluetoothAdapterObserver observer(adapter_);
3187
3188 TestPairingDelegate pairing_delegate;
3189 device->Connect(&pairing_delegate, GetCallback(),
3190 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
3191 base::Unretained(this)));
3192
3193 EXPECT_EQ(0, pairing_delegate.call_count_);
3194 EXPECT_TRUE(device->IsConnecting());
3195
3196 message_loop_.Run();
3197
3198 EXPECT_EQ(0, callback_count_);
3199 EXPECT_EQ(1, error_callback_count_);
3200 EXPECT_EQ(BluetoothDevice::ERROR_FAILED, last_connect_error_);
3201
3202 // Two changes for connecting, one for paired and one for trusted after
3203 // pairing. The device should not be connected.
3204 EXPECT_EQ(4, observer.device_changed_count());
3205 EXPECT_EQ(device, observer.last_device());
3206
3207 EXPECT_FALSE(device->IsConnected());
3208 EXPECT_FALSE(device->IsConnecting());
3209
3210 EXPECT_TRUE(device->IsPaired());
3211
3212 // Make sure the trusted property has been set to true still (since pairing
3213 // worked).
3214 bluez::FakeBluetoothDeviceClient::Properties* properties =
3215 fake_bluetooth_device_client_->GetProperties(dbus::ObjectPath(
3216 bluez::FakeBluetoothDeviceClient::kUnconnectableDevicePath));
3217 EXPECT_TRUE(properties->trusted.value());
3218 }
3219
3220 TEST_F(BluetoothChromeOSTest, PairingRejectedAtPinCode) {
3221 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3222
3223 GetAdapter();
3224 DiscoverDevices();
3225
3226 // Reject the pairing after we receive a request for the PIN code.
3227 BluetoothDevice* device = adapter_->GetDevice(
3228 bluez::FakeBluetoothDeviceClient::kRequestPinCodeAddress);
3229 ASSERT_TRUE(device != nullptr);
3230 ASSERT_FALSE(device->IsPaired());
3231
3232 TestBluetoothAdapterObserver observer(adapter_);
3233
3234 TestPairingDelegate pairing_delegate;
3235 device->Connect(&pairing_delegate, GetCallback(),
3236 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
3237 base::Unretained(this)));
3238
3239 EXPECT_EQ(1, pairing_delegate.call_count_);
3240 EXPECT_EQ(1, pairing_delegate.request_pincode_count_);
3241 EXPECT_TRUE(device->IsConnecting());
3242
3243 // Reject the pairing.
3244 device->RejectPairing();
3245 message_loop_.Run();
3246
3247 EXPECT_EQ(0, callback_count_);
3248 EXPECT_EQ(1, error_callback_count_);
3249 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_REJECTED, last_connect_error_);
3250
3251 // Should be no changes except connecting going true and false.
3252 EXPECT_EQ(2, observer.device_changed_count());
3253 EXPECT_FALSE(device->IsConnected());
3254 EXPECT_FALSE(device->IsConnecting());
3255 EXPECT_FALSE(device->IsPaired());
3256 }
3257
3258 TEST_F(BluetoothChromeOSTest, PairingCancelledAtPinCode) {
3259 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3260
3261 GetAdapter();
3262 DiscoverDevices();
3263
3264 // Cancel the pairing after we receive a request for the PIN code.
3265 BluetoothDevice* device = adapter_->GetDevice(
3266 bluez::FakeBluetoothDeviceClient::kRequestPinCodeAddress);
3267 ASSERT_TRUE(device != nullptr);
3268 ASSERT_FALSE(device->IsPaired());
3269
3270 TestBluetoothAdapterObserver observer(adapter_);
3271
3272 TestPairingDelegate pairing_delegate;
3273 device->Connect(&pairing_delegate, GetCallback(),
3274 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
3275 base::Unretained(this)));
3276
3277 EXPECT_EQ(1, pairing_delegate.call_count_);
3278 EXPECT_EQ(1, pairing_delegate.request_pincode_count_);
3279 EXPECT_TRUE(device->IsConnecting());
3280
3281 // Cancel the pairing.
3282 device->CancelPairing();
3283 message_loop_.Run();
3284
3285 EXPECT_EQ(0, callback_count_);
3286 EXPECT_EQ(1, error_callback_count_);
3287 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED, last_connect_error_);
3288
3289 // Should be no changes except connecting going true and false.
3290 EXPECT_EQ(2, observer.device_changed_count());
3291 EXPECT_FALSE(device->IsConnected());
3292 EXPECT_FALSE(device->IsConnecting());
3293 EXPECT_FALSE(device->IsPaired());
3294 }
3295
3296 TEST_F(BluetoothChromeOSTest, PairingRejectedAtPasskey) {
3297 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3298
3299 GetAdapter();
3300 DiscoverDevices();
3301
3302 // Reject the pairing after we receive a request for the passkey.
3303 BluetoothDevice* device = adapter_->GetDevice(
3304 bluez::FakeBluetoothDeviceClient::kRequestPasskeyAddress);
3305 ASSERT_TRUE(device != nullptr);
3306 ASSERT_FALSE(device->IsPaired());
3307
3308 TestBluetoothAdapterObserver observer(adapter_);
3309
3310 TestPairingDelegate pairing_delegate;
3311 device->Connect(&pairing_delegate, GetCallback(),
3312 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
3313 base::Unretained(this)));
3314
3315 EXPECT_EQ(1, pairing_delegate.call_count_);
3316 EXPECT_EQ(1, pairing_delegate.request_passkey_count_);
3317 EXPECT_TRUE(device->IsConnecting());
3318
3319 // Reject the pairing.
3320 device->RejectPairing();
3321 message_loop_.Run();
3322
3323 EXPECT_EQ(0, callback_count_);
3324 EXPECT_EQ(1, error_callback_count_);
3325 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_REJECTED, last_connect_error_);
3326
3327 // Should be no changes except connecting going true and false.
3328 EXPECT_EQ(2, observer.device_changed_count());
3329 EXPECT_FALSE(device->IsConnected());
3330 EXPECT_FALSE(device->IsConnecting());
3331 EXPECT_FALSE(device->IsPaired());
3332 }
3333
3334 TEST_F(BluetoothChromeOSTest, PairingCancelledAtPasskey) {
3335 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3336
3337 GetAdapter();
3338 DiscoverDevices();
3339
3340 // Cancel the pairing after we receive a request for the passkey.
3341 BluetoothDevice* device = adapter_->GetDevice(
3342 bluez::FakeBluetoothDeviceClient::kRequestPasskeyAddress);
3343 ASSERT_TRUE(device != nullptr);
3344 ASSERT_FALSE(device->IsPaired());
3345
3346 TestBluetoothAdapterObserver observer(adapter_);
3347
3348 TestPairingDelegate pairing_delegate;
3349 device->Connect(&pairing_delegate, GetCallback(),
3350 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
3351 base::Unretained(this)));
3352
3353 EXPECT_EQ(1, pairing_delegate.call_count_);
3354 EXPECT_EQ(1, pairing_delegate.request_passkey_count_);
3355 EXPECT_TRUE(device->IsConnecting());
3356
3357 // Cancel the pairing.
3358 device->CancelPairing();
3359 message_loop_.Run();
3360
3361 EXPECT_EQ(0, callback_count_);
3362 EXPECT_EQ(1, error_callback_count_);
3363 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED, last_connect_error_);
3364
3365 // Should be no changes except connecting going true and false.
3366 EXPECT_EQ(2, observer.device_changed_count());
3367 EXPECT_FALSE(device->IsConnected());
3368 EXPECT_FALSE(device->IsConnecting());
3369 EXPECT_FALSE(device->IsPaired());
3370 }
3371
3372 TEST_F(BluetoothChromeOSTest, PairingRejectedAtConfirmation) {
3373 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3374
3375 GetAdapter();
3376 DiscoverDevices();
3377
3378 // Reject the pairing after we receive a request for passkey confirmation.
3379 BluetoothDevice* device = adapter_->GetDevice(
3380 bluez::FakeBluetoothDeviceClient::kConfirmPasskeyAddress);
3381 ASSERT_TRUE(device != nullptr);
3382 ASSERT_FALSE(device->IsPaired());
3383
3384 TestBluetoothAdapterObserver observer(adapter_);
3385
3386 TestPairingDelegate pairing_delegate;
3387 device->Connect(&pairing_delegate, GetCallback(),
3388 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
3389 base::Unretained(this)));
3390
3391 EXPECT_EQ(1, pairing_delegate.call_count_);
3392 EXPECT_EQ(1, pairing_delegate.confirm_passkey_count_);
3393 EXPECT_TRUE(device->IsConnecting());
3394
3395 // Reject the pairing.
3396 device->RejectPairing();
3397 message_loop_.Run();
3398
3399 EXPECT_EQ(0, callback_count_);
3400 EXPECT_EQ(1, error_callback_count_);
3401 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_REJECTED, last_connect_error_);
3402
3403 // Should be no changes except connecting going true and false.
3404 EXPECT_EQ(2, observer.device_changed_count());
3405 EXPECT_FALSE(device->IsConnected());
3406 EXPECT_FALSE(device->IsConnecting());
3407 EXPECT_FALSE(device->IsPaired());
3408 }
3409
3410 TEST_F(BluetoothChromeOSTest, PairingCancelledAtConfirmation) {
3411 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3412
3413 GetAdapter();
3414 DiscoverDevices();
3415
3416 // Cancel the pairing after we receive a request for the passkey.
3417 BluetoothDevice* device = adapter_->GetDevice(
3418 bluez::FakeBluetoothDeviceClient::kConfirmPasskeyAddress);
3419 ASSERT_TRUE(device != nullptr);
3420 ASSERT_FALSE(device->IsPaired());
3421
3422 TestBluetoothAdapterObserver observer(adapter_);
3423
3424 TestPairingDelegate pairing_delegate;
3425 device->Connect(&pairing_delegate, GetCallback(),
3426 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
3427 base::Unretained(this)));
3428
3429 EXPECT_EQ(1, pairing_delegate.call_count_);
3430 EXPECT_EQ(1, pairing_delegate.confirm_passkey_count_);
3431 EXPECT_TRUE(device->IsConnecting());
3432
3433 // Cancel the pairing.
3434 device->CancelPairing();
3435 message_loop_.Run();
3436
3437 EXPECT_EQ(0, callback_count_);
3438 EXPECT_EQ(1, error_callback_count_);
3439 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED, last_connect_error_);
3440
3441 // Should be no changes except connecting going true and false.
3442 EXPECT_EQ(2, observer.device_changed_count());
3443 EXPECT_FALSE(device->IsConnected());
3444 EXPECT_FALSE(device->IsConnecting());
3445 EXPECT_FALSE(device->IsPaired());
3446 }
3447
3448 TEST_F(BluetoothChromeOSTest, PairingCancelledInFlight) {
3449 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3450
3451 GetAdapter();
3452 DiscoverDevices();
3453
3454 // Cancel the pairing while we're waiting for the remote host.
3455 BluetoothDevice* device = adapter_->GetDevice(
3456 bluez::FakeBluetoothDeviceClient::kLegacyAutopairAddress);
3457 ASSERT_TRUE(device != nullptr);
3458 ASSERT_FALSE(device->IsPaired());
3459
3460 TestBluetoothAdapterObserver observer(adapter_);
3461
3462 TestPairingDelegate pairing_delegate;
3463 device->Connect(&pairing_delegate, GetCallback(),
3464 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
3465 base::Unretained(this)));
3466
3467 EXPECT_EQ(0, pairing_delegate.call_count_);
3468 EXPECT_TRUE(device->IsConnecting());
3469
3470 // Cancel the pairing.
3471 device->CancelPairing();
3472 message_loop_.Run();
3473
3474 EXPECT_EQ(0, callback_count_);
3475 EXPECT_EQ(1, error_callback_count_);
3476 EXPECT_EQ(BluetoothDevice::ERROR_AUTH_CANCELED, last_connect_error_);
3477
3478 // Should be no changes except connecting going true and false.
3479 EXPECT_EQ(2, observer.device_changed_count());
3480 EXPECT_FALSE(device->IsConnected());
3481 EXPECT_FALSE(device->IsConnecting());
3482 EXPECT_FALSE(device->IsPaired());
3483 }
3484
3485 TEST_F(BluetoothChromeOSTest, IncomingPairRequestPinCode) {
3486 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3487
3488 GetAdapter();
3489
3490 TestPairingDelegate pairing_delegate;
3491 adapter_->AddPairingDelegate(
3492 &pairing_delegate,
3493 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH);
3494
3495 // Requires that we provide a PIN code.
3496 fake_bluetooth_device_client_->CreateDevice(
3497 dbus::ObjectPath(bluez::FakeBluetoothAdapterClient::kAdapterPath),
3498 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kRequestPinCodePath));
3499 BluetoothDevice* device = adapter_->GetDevice(
3500 bluez::FakeBluetoothDeviceClient::kRequestPinCodeAddress);
3501 ASSERT_TRUE(device != nullptr);
3502 ASSERT_FALSE(device->IsPaired());
3503
3504 TestBluetoothAdapterObserver observer(adapter_);
3505
3506 fake_bluetooth_device_client_->SimulatePairing(
3507 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kRequestPinCodePath),
3508 true, GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
3509 base::Unretained(this)));
3510
3511 EXPECT_EQ(1, pairing_delegate.call_count_);
3512 EXPECT_EQ(1, pairing_delegate.request_pincode_count_);
3513
3514 // Set the PIN.
3515 device->SetPinCode("1234");
3516 message_loop_.Run();
3517
3518 EXPECT_EQ(1, callback_count_);
3519 EXPECT_EQ(0, error_callback_count_);
3520
3521 // One change for paired, and one for trusted.
3522 EXPECT_EQ(2, observer.device_changed_count());
3523 EXPECT_EQ(device, observer.last_device());
3524
3525 EXPECT_TRUE(device->IsPaired());
3526
3527 // Make sure the trusted property has been set to true.
3528 bluez::FakeBluetoothDeviceClient::Properties* properties =
3529 fake_bluetooth_device_client_->GetProperties(dbus::ObjectPath(
3530 bluez::FakeBluetoothDeviceClient::kRequestPinCodePath));
3531 ASSERT_TRUE(properties->trusted.value());
3532
3533 // No pairing context should remain on the device.
3534 BluetoothDeviceChromeOS* device_chromeos =
3535 static_cast<BluetoothDeviceChromeOS*>(device);
3536 EXPECT_TRUE(device_chromeos->GetPairing() == nullptr);
3537 }
3538
3539 TEST_F(BluetoothChromeOSTest, IncomingPairConfirmPasskey) {
3540 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3541
3542 GetAdapter();
3543
3544 TestPairingDelegate pairing_delegate;
3545 adapter_->AddPairingDelegate(
3546 &pairing_delegate,
3547 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH);
3548
3549 // Requests that we confirm a displayed passkey.
3550 fake_bluetooth_device_client_->CreateDevice(
3551 dbus::ObjectPath(bluez::FakeBluetoothAdapterClient::kAdapterPath),
3552 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kConfirmPasskeyPath));
3553 BluetoothDevice* device = adapter_->GetDevice(
3554 bluez::FakeBluetoothDeviceClient::kConfirmPasskeyAddress);
3555 ASSERT_TRUE(device != nullptr);
3556 ASSERT_FALSE(device->IsPaired());
3557
3558 TestBluetoothAdapterObserver observer(adapter_);
3559
3560 fake_bluetooth_device_client_->SimulatePairing(
3561 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kConfirmPasskeyPath),
3562 true, GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
3563 base::Unretained(this)));
3564
3565 EXPECT_EQ(1, pairing_delegate.call_count_);
3566 EXPECT_EQ(1, pairing_delegate.confirm_passkey_count_);
3567 EXPECT_EQ(123456U, pairing_delegate.last_passkey_);
3568
3569 // Confirm the passkey.
3570 device->ConfirmPairing();
3571 message_loop_.Run();
3572
3573 EXPECT_EQ(1, callback_count_);
3574 EXPECT_EQ(0, error_callback_count_);
3575
3576 // One change for paired, and one for trusted.
3577 EXPECT_EQ(2, observer.device_changed_count());
3578 EXPECT_EQ(device, observer.last_device());
3579
3580 EXPECT_TRUE(device->IsPaired());
3581
3582 // Make sure the trusted property has been set to true.
3583 bluez::FakeBluetoothDeviceClient::Properties* properties =
3584 fake_bluetooth_device_client_->GetProperties(dbus::ObjectPath(
3585 bluez::FakeBluetoothDeviceClient::kConfirmPasskeyPath));
3586 ASSERT_TRUE(properties->trusted.value());
3587
3588 // No pairing context should remain on the device.
3589 BluetoothDeviceChromeOS* device_chromeos =
3590 static_cast<BluetoothDeviceChromeOS*>(device);
3591 EXPECT_TRUE(device_chromeos->GetPairing() == nullptr);
3592 }
3593
3594 TEST_F(BluetoothChromeOSTest, IncomingPairRequestPasskey) {
3595 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3596
3597 GetAdapter();
3598
3599 TestPairingDelegate pairing_delegate;
3600 adapter_->AddPairingDelegate(
3601 &pairing_delegate,
3602 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH);
3603
3604 // Requests that we provide a Passkey.
3605 fake_bluetooth_device_client_->CreateDevice(
3606 dbus::ObjectPath(bluez::FakeBluetoothAdapterClient::kAdapterPath),
3607 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kRequestPasskeyPath));
3608 BluetoothDevice* device = adapter_->GetDevice(
3609 bluez::FakeBluetoothDeviceClient::kRequestPasskeyAddress);
3610 ASSERT_TRUE(device != nullptr);
3611 ASSERT_FALSE(device->IsPaired());
3612
3613 TestBluetoothAdapterObserver observer(adapter_);
3614
3615 fake_bluetooth_device_client_->SimulatePairing(
3616 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kRequestPasskeyPath),
3617 true, GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
3618 base::Unretained(this)));
3619
3620 EXPECT_EQ(1, pairing_delegate.call_count_);
3621 EXPECT_EQ(1, pairing_delegate.request_passkey_count_);
3622
3623 // Set the Passkey.
3624 device->SetPasskey(1234);
3625 message_loop_.Run();
3626
3627 EXPECT_EQ(1, callback_count_);
3628 EXPECT_EQ(0, error_callback_count_);
3629
3630 // One change for paired, and one for trusted.
3631 EXPECT_EQ(2, observer.device_changed_count());
3632 EXPECT_EQ(device, observer.last_device());
3633
3634 EXPECT_TRUE(device->IsPaired());
3635
3636 // Make sure the trusted property has been set to true.
3637 bluez::FakeBluetoothDeviceClient::Properties* properties =
3638 fake_bluetooth_device_client_->GetProperties(dbus::ObjectPath(
3639 bluez::FakeBluetoothDeviceClient::kRequestPasskeyPath));
3640 ASSERT_TRUE(properties->trusted.value());
3641
3642 // No pairing context should remain on the device.
3643 BluetoothDeviceChromeOS* device_chromeos =
3644 static_cast<BluetoothDeviceChromeOS*>(device);
3645 EXPECT_TRUE(device_chromeos->GetPairing() == nullptr);
3646 }
3647
3648 TEST_F(BluetoothChromeOSTest, IncomingPairJustWorks) {
3649 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3650
3651 GetAdapter();
3652
3653 TestPairingDelegate pairing_delegate;
3654 adapter_->AddPairingDelegate(
3655 &pairing_delegate,
3656 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH);
3657
3658 // Uses just-works pairing so, sinec this an incoming pairing, require
3659 // authorization from the user.
3660 fake_bluetooth_device_client_->CreateDevice(
3661 dbus::ObjectPath(bluez::FakeBluetoothAdapterClient::kAdapterPath),
3662 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kJustWorksPath));
3663 BluetoothDevice* device =
3664 adapter_->GetDevice(bluez::FakeBluetoothDeviceClient::kJustWorksAddress);
3665 ASSERT_TRUE(device != nullptr);
3666 ASSERT_FALSE(device->IsPaired());
3667
3668 TestBluetoothAdapterObserver observer(adapter_);
3669
3670 fake_bluetooth_device_client_->SimulatePairing(
3671 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kJustWorksPath), true,
3672 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
3673 base::Unretained(this)));
3674
3675 EXPECT_EQ(1, pairing_delegate.call_count_);
3676 EXPECT_EQ(1, pairing_delegate.authorize_pairing_count_);
3677
3678 // Confirm the pairing.
3679 device->ConfirmPairing();
3680 message_loop_.Run();
3681
3682 EXPECT_EQ(1, callback_count_);
3683 EXPECT_EQ(0, error_callback_count_);
3684
3685 // One change for paired, and one for trusted.
3686 EXPECT_EQ(2, observer.device_changed_count());
3687 EXPECT_EQ(device, observer.last_device());
3688
3689 EXPECT_TRUE(device->IsPaired());
3690
3691 // Make sure the trusted property has been set to true.
3692 bluez::FakeBluetoothDeviceClient::Properties* properties =
3693 fake_bluetooth_device_client_->GetProperties(
3694 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kJustWorksPath));
3695 ASSERT_TRUE(properties->trusted.value());
3696
3697 // No pairing context should remain on the device.
3698 BluetoothDeviceChromeOS* device_chromeos =
3699 static_cast<BluetoothDeviceChromeOS*>(device);
3700 EXPECT_TRUE(device_chromeos->GetPairing() == nullptr);
3701 }
3702
3703 TEST_F(BluetoothChromeOSTest, IncomingPairRequestPinCodeWithoutDelegate) {
3704 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3705
3706 GetAdapter();
3707
3708 // Requires that we provide a PIN Code, without a pairing delegate,
3709 // that will be rejected.
3710 fake_bluetooth_device_client_->CreateDevice(
3711 dbus::ObjectPath(bluez::FakeBluetoothAdapterClient::kAdapterPath),
3712 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kRequestPinCodePath));
3713 BluetoothDevice* device = adapter_->GetDevice(
3714 bluez::FakeBluetoothDeviceClient::kRequestPinCodeAddress);
3715 ASSERT_TRUE(device != nullptr);
3716 ASSERT_FALSE(device->IsPaired());
3717
3718 TestBluetoothAdapterObserver observer(adapter_);
3719
3720 fake_bluetooth_device_client_->SimulatePairing(
3721 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kRequestPinCodePath),
3722 true, GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
3723 base::Unretained(this)));
3724
3725 message_loop_.Run();
3726
3727 EXPECT_EQ(0, callback_count_);
3728 EXPECT_EQ(1, error_callback_count_);
3729 EXPECT_EQ(bluetooth_device::kErrorAuthenticationRejected, last_client_error_);
3730
3731 // No changes should be observer.
3732 EXPECT_EQ(0, observer.device_changed_count());
3733
3734 EXPECT_FALSE(device->IsPaired());
3735
3736 // No pairing context should remain on the device.
3737 BluetoothDeviceChromeOS* device_chromeos =
3738 static_cast<BluetoothDeviceChromeOS*>(device);
3739 EXPECT_TRUE(device_chromeos->GetPairing() == nullptr);
3740 }
3741
3742 TEST_F(BluetoothChromeOSTest, IncomingPairConfirmPasskeyWithoutDelegate) {
3743 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3744
3745 GetAdapter();
3746
3747 // Requests that we confirm a displayed passkey, without a pairing delegate,
3748 // that will be rejected.
3749 fake_bluetooth_device_client_->CreateDevice(
3750 dbus::ObjectPath(bluez::FakeBluetoothAdapterClient::kAdapterPath),
3751 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kConfirmPasskeyPath));
3752 BluetoothDevice* device = adapter_->GetDevice(
3753 bluez::FakeBluetoothDeviceClient::kConfirmPasskeyAddress);
3754 ASSERT_TRUE(device != nullptr);
3755 ASSERT_FALSE(device->IsPaired());
3756
3757 TestBluetoothAdapterObserver observer(adapter_);
3758
3759 fake_bluetooth_device_client_->SimulatePairing(
3760 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kConfirmPasskeyPath),
3761 true, GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
3762 base::Unretained(this)));
3763
3764 message_loop_.Run();
3765
3766 EXPECT_EQ(0, callback_count_);
3767 EXPECT_EQ(1, error_callback_count_);
3768 EXPECT_EQ(bluetooth_device::kErrorAuthenticationRejected, last_client_error_);
3769
3770 // No changes should be observer.
3771 EXPECT_EQ(0, observer.device_changed_count());
3772
3773 EXPECT_FALSE(device->IsPaired());
3774
3775 // No pairing context should remain on the device.
3776 BluetoothDeviceChromeOS* device_chromeos =
3777 static_cast<BluetoothDeviceChromeOS*>(device);
3778 EXPECT_TRUE(device_chromeos->GetPairing() == nullptr);
3779 }
3780
3781 TEST_F(BluetoothChromeOSTest, IncomingPairRequestPasskeyWithoutDelegate) {
3782 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3783
3784 GetAdapter();
3785
3786 // Requests that we provide a displayed passkey, without a pairing delegate,
3787 // that will be rejected.
3788 fake_bluetooth_device_client_->CreateDevice(
3789 dbus::ObjectPath(bluez::FakeBluetoothAdapterClient::kAdapterPath),
3790 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kRequestPasskeyPath));
3791 BluetoothDevice* device = adapter_->GetDevice(
3792 bluez::FakeBluetoothDeviceClient::kRequestPasskeyAddress);
3793 ASSERT_TRUE(device != nullptr);
3794 ASSERT_FALSE(device->IsPaired());
3795
3796 TestBluetoothAdapterObserver observer(adapter_);
3797
3798 fake_bluetooth_device_client_->SimulatePairing(
3799 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kRequestPasskeyPath),
3800 true, GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
3801 base::Unretained(this)));
3802
3803 message_loop_.Run();
3804
3805 EXPECT_EQ(0, callback_count_);
3806 EXPECT_EQ(1, error_callback_count_);
3807 EXPECT_EQ(bluetooth_device::kErrorAuthenticationRejected, last_client_error_);
3808
3809 // No changes should be observer.
3810 EXPECT_EQ(0, observer.device_changed_count());
3811
3812 EXPECT_FALSE(device->IsPaired());
3813
3814 // No pairing context should remain on the device.
3815 BluetoothDeviceChromeOS* device_chromeos =
3816 static_cast<BluetoothDeviceChromeOS*>(device);
3817 EXPECT_TRUE(device_chromeos->GetPairing() == nullptr);
3818 }
3819
3820 TEST_F(BluetoothChromeOSTest, IncomingPairJustWorksWithoutDelegate) {
3821 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3822
3823 GetAdapter();
3824
3825 // Uses just-works pairing and thus requires authorization for incoming
3826 // pairings, without a pairing delegate, that will be rejected.
3827 fake_bluetooth_device_client_->CreateDevice(
3828 dbus::ObjectPath(bluez::FakeBluetoothAdapterClient::kAdapterPath),
3829 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kJustWorksPath));
3830 BluetoothDevice* device =
3831 adapter_->GetDevice(bluez::FakeBluetoothDeviceClient::kJustWorksAddress);
3832 ASSERT_TRUE(device != nullptr);
3833 ASSERT_FALSE(device->IsPaired());
3834
3835 TestBluetoothAdapterObserver observer(adapter_);
3836
3837 fake_bluetooth_device_client_->SimulatePairing(
3838 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kJustWorksPath), true,
3839 GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
3840 base::Unretained(this)));
3841
3842 message_loop_.Run();
3843
3844 EXPECT_EQ(0, callback_count_);
3845 EXPECT_EQ(1, error_callback_count_);
3846 EXPECT_EQ(bluetooth_device::kErrorAuthenticationRejected, last_client_error_);
3847
3848 // No changes should be observer.
3849 EXPECT_EQ(0, observer.device_changed_count());
3850
3851 EXPECT_FALSE(device->IsPaired());
3852
3853 // No pairing context should remain on the device.
3854 BluetoothDeviceChromeOS* device_chromeos =
3855 static_cast<BluetoothDeviceChromeOS*>(device);
3856 EXPECT_TRUE(device_chromeos->GetPairing() == nullptr);
3857 }
3858
3859 TEST_F(BluetoothChromeOSTest, RemovePairingDelegateDuringPairing) {
3860 fake_bluetooth_device_client_->SetSimulationIntervalMs(10);
3861
3862 GetAdapter();
3863
3864 TestPairingDelegate pairing_delegate;
3865 adapter_->AddPairingDelegate(
3866 &pairing_delegate,
3867 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH);
3868
3869 // Requests that we provide a Passkey.
3870 fake_bluetooth_device_client_->CreateDevice(
3871 dbus::ObjectPath(bluez::FakeBluetoothAdapterClient::kAdapterPath),
3872 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kRequestPasskeyPath));
3873 BluetoothDevice* device = adapter_->GetDevice(
3874 bluez::FakeBluetoothDeviceClient::kRequestPasskeyAddress);
3875 ASSERT_TRUE(device != nullptr);
3876 ASSERT_FALSE(device->IsPaired());
3877
3878 TestBluetoothAdapterObserver observer(adapter_);
3879
3880 fake_bluetooth_device_client_->SimulatePairing(
3881 dbus::ObjectPath(bluez::FakeBluetoothDeviceClient::kRequestPasskeyPath),
3882 true, GetCallback(), base::Bind(&BluetoothChromeOSTest::DBusErrorCallback,
3883 base::Unretained(this)));
3884
3885 EXPECT_EQ(1, pairing_delegate.call_count_);
3886 EXPECT_EQ(1, pairing_delegate.request_passkey_count_);
3887
3888 // A pairing context should now be set on the device.
3889 BluetoothDeviceChromeOS* device_chromeos =
3890 static_cast<BluetoothDeviceChromeOS*>(device);
3891 ASSERT_TRUE(device_chromeos->GetPairing() != nullptr);
3892
3893 // Removing the pairing delegate should remove that pairing context.
3894 adapter_->RemovePairingDelegate(&pairing_delegate);
3895
3896 EXPECT_TRUE(device_chromeos->GetPairing() == nullptr);
3897
3898 // Set the Passkey, this should now have no effect since the pairing has
3899 // been, in-effect, cancelled
3900 device->SetPasskey(1234);
3901
3902 EXPECT_EQ(0, callback_count_);
3903 EXPECT_EQ(0, error_callback_count_);
3904 EXPECT_EQ(0, observer.device_changed_count());
3905
3906 EXPECT_FALSE(device->IsPaired());
3907 }
3908
3909 TEST_F(BluetoothChromeOSTest, DeviceId) {
3910 GetAdapter();
3911
3912 // Use the built-in paired device for this test, grab its Properties
3913 // structure so we can adjust the underlying modalias property.
3914 BluetoothDevice* device = adapter_->GetDevice(
3915 bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress);
3916 bluez::FakeBluetoothDeviceClient::Properties* properties =
3917 fake_bluetooth_device_client_->GetProperties(dbus::ObjectPath(
3918 bluez::FakeBluetoothDeviceClient::kPairedDevicePath));
3919
3920 ASSERT_TRUE(device != nullptr);
3921 ASSERT_TRUE(properties != nullptr);
3922
3923 // Valid USB IF-assigned identifier.
3924 ASSERT_EQ("usb:v05ACp030Dd0306", properties->modalias.value());
3925
3926 EXPECT_EQ(BluetoothDevice::VENDOR_ID_USB, device->GetVendorIDSource());
3927 EXPECT_EQ(0x05ac, device->GetVendorID());
3928 EXPECT_EQ(0x030d, device->GetProductID());
3929 EXPECT_EQ(0x0306, device->GetDeviceID());
3930
3931 // Valid Bluetooth SIG-assigned identifier.
3932 properties->modalias.ReplaceValue("bluetooth:v00E0p2400d0400");
3933
3934 EXPECT_EQ(BluetoothDevice::VENDOR_ID_BLUETOOTH, device->GetVendorIDSource());
3935 EXPECT_EQ(0x00e0, device->GetVendorID());
3936 EXPECT_EQ(0x2400, device->GetProductID());
3937 EXPECT_EQ(0x0400, device->GetDeviceID());
3938
3939 // Invalid USB IF-assigned identifier.
3940 properties->modalias.ReplaceValue("usb:x00E0p2400d0400");
3941
3942 EXPECT_EQ(BluetoothDevice::VENDOR_ID_UNKNOWN, device->GetVendorIDSource());
3943 EXPECT_EQ(0, device->GetVendorID());
3944 EXPECT_EQ(0, device->GetProductID());
3945 EXPECT_EQ(0, device->GetDeviceID());
3946
3947 // Invalid Bluetooth SIG-assigned identifier.
3948 properties->modalias.ReplaceValue("bluetooth:x00E0p2400d0400");
3949
3950 EXPECT_EQ(BluetoothDevice::VENDOR_ID_UNKNOWN, device->GetVendorIDSource());
3951 EXPECT_EQ(0, device->GetVendorID());
3952 EXPECT_EQ(0, device->GetProductID());
3953 EXPECT_EQ(0, device->GetDeviceID());
3954
3955 // Unknown vendor specification identifier.
3956 properties->modalias.ReplaceValue("chrome:v00E0p2400d0400");
3957
3958 EXPECT_EQ(BluetoothDevice::VENDOR_ID_UNKNOWN, device->GetVendorIDSource());
3959 EXPECT_EQ(0, device->GetVendorID());
3960 EXPECT_EQ(0, device->GetProductID());
3961 EXPECT_EQ(0, device->GetDeviceID());
3962 }
3963
3964 TEST_F(BluetoothChromeOSTest, GetConnectionInfoForDisconnectedDevice) {
3965 GetAdapter();
3966 BluetoothDevice* device = adapter_->GetDevice(
3967 bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress);
3968
3969 // Calling GetConnectionInfo for an unconnected device should return a result
3970 // in which all fields are filled with BluetoothDevice::kUnknownPower.
3971 BluetoothDevice::ConnectionInfo conn_info(0, 0, 0);
3972 device->GetConnectionInfo(base::Bind(&SaveConnectionInfo, &conn_info));
3973 int unknown_power = BluetoothDevice::kUnknownPower;
3974 EXPECT_NE(0, unknown_power);
3975 EXPECT_EQ(unknown_power, conn_info.rssi);
3976 EXPECT_EQ(unknown_power, conn_info.transmit_power);
3977 EXPECT_EQ(unknown_power, conn_info.max_transmit_power);
3978 }
3979
3980 TEST_F(BluetoothChromeOSTest, GetConnectionInfoForConnectedDevice) {
3981 GetAdapter();
3982 BluetoothDevice* device = adapter_->GetDevice(
3983 bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress);
3984
3985 device->Connect(nullptr, GetCallback(),
3986 base::Bind(&BluetoothChromeOSTest::ConnectErrorCallback,
3987 base::Unretained(this)));
3988 EXPECT_TRUE(device->IsConnected());
3989
3990 // Calling GetConnectionInfo for a connected device should return valid
3991 // results.
3992 fake_bluetooth_device_client_->UpdateConnectionInfo(-10, 3, 4);
3993 BluetoothDevice::ConnectionInfo conn_info;
3994 device->GetConnectionInfo(base::Bind(&SaveConnectionInfo, &conn_info));
3995 EXPECT_EQ(-10, conn_info.rssi);
3996 EXPECT_EQ(3, conn_info.transmit_power);
3997 EXPECT_EQ(4, conn_info.max_transmit_power);
3998 }
3999
4000 // Verifies Shutdown shuts down the adapter as expected.
4001 TEST_F(BluetoothChromeOSTest, Shutdown) {
4002 // Set up adapter. Set powered & discoverable, start discovery.
4003 GetAdapter();
4004 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
4005 adapter_->SetDiscoverable(true, GetCallback(), GetErrorCallback());
4006 adapter_->StartDiscoverySession(
4007 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
4008 base::Unretained(this)),
4009 GetErrorCallback());
4010 base::MessageLoop::current()->Run();
4011 ASSERT_EQ(3, callback_count_);
4012 ASSERT_EQ(0, error_callback_count_);
4013 callback_count_ = 0;
4014
4015 TestPairingDelegate pairing_delegate;
4016 adapter_->AddPairingDelegate(
4017 &pairing_delegate, BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH);
4018
4019 // Validate running adapter state.
4020 EXPECT_NE("", adapter_->GetAddress());
4021 EXPECT_NE("", adapter_->GetName());
4022 EXPECT_TRUE(adapter_->IsInitialized());
4023 EXPECT_TRUE(adapter_->IsPresent());
4024 EXPECT_TRUE(adapter_->IsPowered());
4025 EXPECT_TRUE(adapter_->IsDiscoverable());
4026 EXPECT_TRUE(adapter_->IsDiscovering());
4027 EXPECT_EQ(2U, adapter_->GetDevices().size());
4028 EXPECT_NE(nullptr,
4029 adapter_->GetDevice(
4030 bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress));
4031 EXPECT_NE(dbus::ObjectPath(""), static_cast<BluetoothAdapterChromeOS*>(
4032 adapter_.get())->object_path());
4033
4034 // Shutdown
4035 adapter_->Shutdown();
4036
4037 // Validate post shutdown state by calling all BluetoothAdapterChromeOS
4038 // members, in declaration order:
4039
4040 adapter_->Shutdown();
4041 // DeleteOnCorrectThread omitted as we don't want to delete in this test.
4042 {
4043 TestBluetoothAdapterObserver observer(adapter_); // Calls AddObserver
4044 } // ~TestBluetoothAdapterObserver calls RemoveObserver.
4045 EXPECT_EQ("", adapter_->GetAddress());
4046 EXPECT_EQ("", adapter_->GetName());
4047
4048 adapter_->SetName("", GetCallback(), GetErrorCallback());
4049 EXPECT_EQ(0, callback_count_);
4050 EXPECT_EQ(1, error_callback_count_--) << "SetName error";
4051
4052 EXPECT_TRUE(adapter_->IsInitialized());
4053 EXPECT_FALSE(adapter_->IsPresent());
4054 EXPECT_FALSE(adapter_->IsPowered());
4055
4056 adapter_->SetPowered(true, GetCallback(), GetErrorCallback());
4057 EXPECT_EQ(0, callback_count_);
4058 EXPECT_EQ(1, error_callback_count_--) << "SetPowered error";
4059
4060 EXPECT_FALSE(adapter_->IsDiscoverable());
4061
4062 adapter_->SetDiscoverable(true, GetCallback(), GetErrorCallback());
4063 EXPECT_EQ(0, callback_count_);
4064 EXPECT_EQ(1, error_callback_count_--) << "SetDiscoverable error";
4065
4066 EXPECT_FALSE(adapter_->IsDiscovering());
4067 // CreateRfcommService will DCHECK after Shutdown().
4068 // CreateL2capService will DCHECK after Shutdown().
4069
4070 BluetoothAudioSink::Options audio_sink_options;
4071 adapter_->RegisterAudioSink(
4072 audio_sink_options,
4073 base::Bind(&BluetoothChromeOSTest::AudioSinkAcquiredCallback,
4074 base::Unretained(this)),
4075 base::Bind(&BluetoothChromeOSTest::AudioSinkErrorCallback,
4076 base::Unretained(this)));
4077 EXPECT_EQ(0, callback_count_);
4078 EXPECT_EQ(1, error_callback_count_--) << "RegisterAudioSink error";
4079
4080 BluetoothAdapterChromeOS* adapter_chrome_os =
4081 static_cast<BluetoothAdapterChromeOS*>(adapter_.get());
4082 EXPECT_EQ(nullptr,
4083 adapter_chrome_os->GetDeviceWithPath(dbus::ObjectPath("")));
4084
4085 // Notify methods presume objects exist that are owned by the adapter and
4086 // destroyed in Shutdown(). Mocks are not attempted here that won't exist,
4087 // as verified below by EXPECT_EQ(0U, adapter_->GetDevices().size());
4088 // NotifyDeviceChanged
4089 // NotifyGattServiceAdded
4090 // NotifyGattServiceRemoved
4091 // NotifyGattServiceChanged
4092 // NotifyGattDiscoveryComplete
4093 // NotifyGattCharacteristicAdded
4094 // NotifyGattCharacteristicRemoved
4095 // NotifyGattDescriptorAdded
4096 // NotifyGattDescriptorRemoved
4097 // NotifyGattCharacteristicValueChanged
4098 // NotifyGattDescriptorValueChanged
4099
4100 EXPECT_EQ(dbus::ObjectPath(""), adapter_chrome_os->object_path());
4101
4102 adapter_profile_ = nullptr;
4103
4104 FakeBluetoothProfileServiceProviderDelegate profile_delegate;
4105 adapter_chrome_os->UseProfile(
4106 BluetoothUUID(), dbus::ObjectPath(""),
4107 bluez::BluetoothProfileManagerClient::Options(), &profile_delegate,
4108 base::Bind(&BluetoothChromeOSTest::ProfileRegisteredCallback,
4109 base::Unretained(this)),
4110 base::Bind(&BluetoothChromeOSTest::ErrorCompletionCallback,
4111 base::Unretained(this)));
4112
4113 EXPECT_FALSE(adapter_profile_) << "UseProfile error";
4114 EXPECT_EQ(0, callback_count_) << "UseProfile error";
4115 EXPECT_EQ(1, error_callback_count_--) << "UseProfile error";
4116
4117 // Protected and private methods:
4118
4119 adapter_chrome_os->RemovePairingDelegateInternal(&pairing_delegate);
4120 // AdapterAdded() invalid post Shutdown because it calls SetAdapter.
4121 adapter_chrome_os->AdapterRemoved(dbus::ObjectPath("x"));
4122 adapter_chrome_os->AdapterPropertyChanged(dbus::ObjectPath("x"), "");
4123 adapter_chrome_os->DeviceAdded(dbus::ObjectPath(""));
4124 adapter_chrome_os->DeviceRemoved(dbus::ObjectPath(""));
4125 adapter_chrome_os->DevicePropertyChanged(dbus::ObjectPath(""), "");
4126 adapter_chrome_os->InputPropertyChanged(dbus::ObjectPath(""), "");
4127 // bluez::BluetoothAgentServiceProvider::Delegate omitted, dbus will be
4128 // shutdown,
4129 // with the exception of Released.
4130 adapter_chrome_os->Released();
4131
4132 adapter_chrome_os->OnRegisterAgent();
4133 adapter_chrome_os->OnRegisterAgentError("", "");
4134 adapter_chrome_os->OnRequestDefaultAgent();
4135 adapter_chrome_os->OnRequestDefaultAgentError("", "");
4136
4137 adapter_chrome_os->OnRegisterAudioSink(
4138 base::Bind(&BluetoothChromeOSTest::AudioSinkAcquiredCallback,
4139 base::Unretained(this)),
4140 base::Bind(&BluetoothChromeOSTest::AudioSinkErrorCallback,
4141 base::Unretained(this)),
4142 scoped_refptr<device::BluetoothAudioSink>());
4143 EXPECT_EQ(0, callback_count_);
4144 EXPECT_EQ(1, error_callback_count_--) << "RegisterAudioSink error";
4145
4146 // GetPairing will DCHECK after Shutdown().
4147 // SetAdapter will DCHECK after Shutdown().
4148 // SetDefaultAdapterName will DCHECK after Shutdown().
4149 // RemoveAdapter will DCHECK after Shutdown().
4150 adapter_chrome_os->PoweredChanged(false);
4151 adapter_chrome_os->DiscoverableChanged(false);
4152 adapter_chrome_os->DiscoveringChanged(false);
4153 adapter_chrome_os->PresentChanged(false);
4154
4155 adapter_chrome_os->OnSetDiscoverable(GetCallback(), GetErrorCallback(), true);
4156 EXPECT_EQ(0, callback_count_) << "OnSetDiscoverable error";
4157 EXPECT_EQ(1, error_callback_count_--) << "OnSetDiscoverable error";
4158
4159 adapter_chrome_os->OnPropertyChangeCompleted(GetCallback(),
4160 GetErrorCallback(), true);
4161 EXPECT_EQ(0, callback_count_) << "OnPropertyChangeCompleted error";
4162 EXPECT_EQ(1, error_callback_count_--) << "OnPropertyChangeCompleted error";
4163
4164 adapter_chrome_os->AddDiscoverySession(nullptr, GetCallback(),
4165 GetDiscoveryErrorCallback());
4166 EXPECT_EQ(0, callback_count_) << "AddDiscoverySession error";
4167 EXPECT_EQ(1, error_callback_count_--) << "AddDiscoverySession error";
4168
4169 adapter_chrome_os->RemoveDiscoverySession(nullptr, GetCallback(),
4170 GetDiscoveryErrorCallback());
4171 EXPECT_EQ(0, callback_count_) << "RemoveDiscoverySession error";
4172 EXPECT_EQ(1, error_callback_count_--) << "RemoveDiscoverySession error";
4173
4174 // OnStartDiscovery tested in Shutdown_OnStartDiscovery
4175 // OnStartDiscoveryError tested in Shutdown_OnStartDiscoveryError
4176 // OnStopDiscovery tested in Shutdown_OnStopDiscovery
4177 // OnStopDiscoveryError tested in Shutdown_OnStopDiscoveryError
4178
4179 adapter_profile_ = nullptr;
4180
4181 // OnRegisterProfile SetProfileDelegate, OnRegisterProfileError, require
4182 // UseProfile to be set first, do so again here just before calling them.
4183 adapter_chrome_os->UseProfile(
4184 BluetoothUUID(), dbus::ObjectPath(""),
4185 bluez::BluetoothProfileManagerClient::Options(), &profile_delegate,
4186 base::Bind(&BluetoothChromeOSTest::ProfileRegisteredCallback,
4187 base::Unretained(this)),
4188 base::Bind(&BluetoothChromeOSTest::ErrorCompletionCallback,
4189 base::Unretained(this)));
4190
4191 EXPECT_FALSE(adapter_profile_) << "UseProfile error";
4192 EXPECT_EQ(0, callback_count_) << "UseProfile error";
4193 EXPECT_EQ(1, error_callback_count_--) << "UseProfile error";
4194
4195 adapter_chrome_os->SetProfileDelegate(
4196 BluetoothUUID(), dbus::ObjectPath(""), &profile_delegate,
4197 base::Bind(&BluetoothChromeOSTest::ProfileRegisteredCallback,
4198 base::Unretained(this)),
4199 base::Bind(&BluetoothChromeOSTest::ErrorCompletionCallback,
4200 base::Unretained(this)));
4201 EXPECT_EQ(0, callback_count_) << "SetProfileDelegate error";
4202 EXPECT_EQ(1, error_callback_count_--) << "SetProfileDelegate error";
4203
4204 adapter_chrome_os->OnRegisterProfileError(BluetoothUUID(), "", "");
4205 EXPECT_EQ(0, callback_count_) << "OnRegisterProfileError error";
4206 EXPECT_EQ(0, error_callback_count_) << "OnRegisterProfileError error";
4207
4208 adapter_chrome_os->ProcessQueuedDiscoveryRequests();
4209
4210 // From BluetoothAdapater:
4211
4212 adapter_->StartDiscoverySession(
4213 base::Bind(&BluetoothChromeOSTest::DiscoverySessionCallback,
4214 base::Unretained(this)),
4215 GetErrorCallback());
4216 EXPECT_EQ(0, callback_count_) << "StartDiscoverySession error";
4217 EXPECT_EQ(1, error_callback_count_--) << "StartDiscoverySession error";
4218
4219 EXPECT_EQ(0U, adapter_->GetDevices().size());
4220 EXPECT_EQ(nullptr,
4221 adapter_->GetDevice(
4222 bluez::FakeBluetoothDeviceClient::kPairedDeviceAddress));
4223 TestPairingDelegate pairing_delegate2;
4224 adapter_->AddPairingDelegate(
4225 &pairing_delegate2, BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH);
4226 adapter_->RemovePairingDelegate(&pairing_delegate2);
4227 }
4228
4229 // Verifies post-Shutdown of discovery sessions and OnStartDiscovery.
4230 TEST_F(BluetoothChromeOSTest, Shutdown_OnStartDiscovery) {
4231 const int kNumberOfDiscoverySessions = 10;
4232 GetAdapter();
4233 BluetoothAdapterChromeOS* adapter_chrome_os =
4234 static_cast<BluetoothAdapterChromeOS*>(adapter_.get());
4235
4236 for (int i = 0; i < kNumberOfDiscoverySessions; i++) {
4237 adapter_chrome_os->AddDiscoverySession(nullptr, GetCallback(),
4238 GetDiscoveryErrorCallback());
4239 }
4240 adapter_->Shutdown();
4241 adapter_chrome_os->OnStartDiscovery(GetCallback(),
4242 GetDiscoveryErrorCallback());
4243
4244 EXPECT_EQ(0, callback_count_);
4245 EXPECT_EQ(kNumberOfDiscoverySessions, error_callback_count_);
4246 }
4247
4248 // Verifies post-Shutdown of discovery sessions and OnStartDiscoveryError.
4249 TEST_F(BluetoothChromeOSTest, Shutdown_OnStartDiscoveryError) {
4250 const int kNumberOfDiscoverySessions = 10;
4251 GetAdapter();
4252 BluetoothAdapterChromeOS* adapter_chrome_os =
4253 static_cast<BluetoothAdapterChromeOS*>(adapter_.get());
4254
4255 for (int i = 0; i < kNumberOfDiscoverySessions; i++) {
4256 adapter_chrome_os->AddDiscoverySession(nullptr, GetCallback(),
4257 GetDiscoveryErrorCallback());
4258 }
4259 adapter_->Shutdown();
4260 adapter_chrome_os->OnStartDiscoveryError(GetCallback(),
4261 GetDiscoveryErrorCallback(), "", "");
4262
4263 EXPECT_EQ(0, callback_count_);
4264 EXPECT_EQ(kNumberOfDiscoverySessions, error_callback_count_);
4265 }
4266
4267 // Verifies post-Shutdown of discovery sessions and OnStartDiscovery.
4268 TEST_F(BluetoothChromeOSTest, Shutdown_OnStopDiscovery) {
4269 const int kNumberOfDiscoverySessions = 10;
4270 GetAdapter();
4271 BluetoothAdapterChromeOS* adapter_chrome_os =
4272 static_cast<BluetoothAdapterChromeOS*>(adapter_.get());
4273
4274 // In order to queue up discovery sessions before an OnStopDiscovery call
4275 // RemoveDiscoverySession must be called, so Add, Start, and Remove:
4276 adapter_chrome_os->AddDiscoverySession(nullptr, GetCallback(),
4277 GetDiscoveryErrorCallback());
4278 adapter_chrome_os->OnStartDiscovery(GetCallback(),
4279 GetDiscoveryErrorCallback());
4280 adapter_chrome_os->RemoveDiscoverySession(nullptr, GetCallback(),
4281 GetDiscoveryErrorCallback());
4282 callback_count_ = 0;
4283 error_callback_count_ = 0;
4284 // Can now queue discovery sessions while waiting for OnStopDiscovery.
4285 for (int i = 0; i < kNumberOfDiscoverySessions; i++) {
4286 adapter_chrome_os->AddDiscoverySession(nullptr, GetCallback(),
4287 GetDiscoveryErrorCallback());
4288 }
4289 adapter_->Shutdown();
4290 adapter_chrome_os->OnStopDiscovery(GetCallback());
4291
4292 // 1 successful stopped discovery from RemoveDiscoverySession, and
4293 // kNumberOfDiscoverySessions errors from AddDiscoverySession/OnStopDiscovery.
4294 EXPECT_EQ(1, callback_count_);
4295 EXPECT_EQ(kNumberOfDiscoverySessions, error_callback_count_);
4296 }
4297
4298 // Verifies post-Shutdown of discovery sessions and OnStopDiscoveryError.
4299 TEST_F(BluetoothChromeOSTest, Shutdown_OnStopDiscoveryError) {
4300 const int kNumberOfDiscoverySessions = 10;
4301 GetAdapter();
4302 BluetoothAdapterChromeOS* adapter_chrome_os =
4303 static_cast<BluetoothAdapterChromeOS*>(adapter_.get());
4304
4305 // In order to queue up discovery sessions before an OnStopDiscoveryError call
4306 // RemoveDiscoverySession must be called, so Add, Start, and Remove:
4307 adapter_chrome_os->AddDiscoverySession(nullptr, GetCallback(),
4308 GetDiscoveryErrorCallback());
4309 adapter_chrome_os->OnStartDiscovery(GetCallback(),
4310 GetDiscoveryErrorCallback());
4311 adapter_chrome_os->RemoveDiscoverySession(nullptr, GetCallback(),
4312 GetDiscoveryErrorCallback());
4313 callback_count_ = 0;
4314 error_callback_count_ = 0;
4315 // Can now queue discovery sessions while waiting for OnStopDiscoveryError.
4316 for (int i = 0; i < kNumberOfDiscoverySessions; i++) {
4317 adapter_chrome_os->AddDiscoverySession(nullptr, GetCallback(),
4318 GetDiscoveryErrorCallback());
4319 }
4320 adapter_->Shutdown();
4321 adapter_chrome_os->OnStopDiscoveryError(GetDiscoveryErrorCallback(), "", "");
4322
4323 // 1 error reported to RemoveDiscoverySession because of OnStopDiscoveryError,
4324 // and kNumberOfDiscoverySessions errors queued with AddDiscoverySession.
4325 EXPECT_EQ(0, callback_count_);
4326 EXPECT_EQ(1 + kNumberOfDiscoverySessions, error_callback_count_);
4327 }
4328
4329 } // namespace chromeos
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698