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

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

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

Powered by Google App Engine
This is Rietveld 408576698