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