QUIC - cleanup changes to sync chromium tree with internal source.
[chromium-blink-merge.git] / device / bluetooth / bluetooth_adapter_unittest.cc
blobc4a1513824478b8d822e1b8c1bfe6846328ac77e
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/bind.h"
6 #include "base/memory/ref_counted.h"
7 #include "base/run_loop.h"
8 #include "device/bluetooth/bluetooth_adapter.h"
9 #include "device/bluetooth/bluetooth_device.h"
10 #include "device/bluetooth/bluetooth_discovery_session.h"
11 #include "device/bluetooth/test/bluetooth_test.h"
12 #include "device/bluetooth/test/test_bluetooth_adapter_observer.h"
13 #include "testing/gtest/include/gtest/gtest.h"
15 #if defined(OS_ANDROID)
16 #include "device/bluetooth/test/bluetooth_test_android.h"
17 #elif defined(OS_MACOSX)
18 #include "device/bluetooth/test/bluetooth_test_mac.h"
19 #endif
21 using device::BluetoothDevice;
23 namespace device {
25 class TestBluetoothAdapter : public BluetoothAdapter {
26 public:
27 TestBluetoothAdapter() {
30 std::string GetAddress() const override { return ""; }
32 std::string GetName() const override { return ""; }
34 void SetName(const std::string& name,
35 const base::Closure& callback,
36 const ErrorCallback& error_callback) override {}
38 bool IsInitialized() const override { return false; }
40 bool IsPresent() const override { return false; }
42 bool IsPowered() const override { return false; }
44 void SetPowered(bool powered,
45 const base::Closure& callback,
46 const ErrorCallback& error_callback) override {}
48 bool IsDiscoverable() const override { return false; }
50 void SetDiscoverable(bool discoverable,
51 const base::Closure& callback,
52 const ErrorCallback& error_callback) override {}
54 bool IsDiscovering() const override { return false; }
56 void StartDiscoverySessionWithFilter(
57 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter,
58 const DiscoverySessionCallback& callback,
59 const ErrorCallback& error_callback) override {
60 OnStartDiscoverySession(discovery_filter.Pass(), callback);
63 void StartDiscoverySession(const DiscoverySessionCallback& callback,
64 const ErrorCallback& error_callback) override {}
65 void CreateRfcommService(
66 const BluetoothUUID& uuid,
67 const ServiceOptions& options,
68 const CreateServiceCallback& callback,
69 const CreateServiceErrorCallback& error_callback) override {}
71 void CreateL2capService(
72 const BluetoothUUID& uuid,
73 const ServiceOptions& options,
74 const CreateServiceCallback& callback,
75 const CreateServiceErrorCallback& error_callback) override {}
77 void RegisterAudioSink(
78 const BluetoothAudioSink::Options& options,
79 const AcquiredCallback& callback,
80 const BluetoothAudioSink::ErrorCallback& error_callback) override {}
82 void RegisterAdvertisement(
83 scoped_ptr<BluetoothAdvertisement::Data> advertisement_data,
84 const CreateAdvertisementCallback& callback,
85 const CreateAdvertisementErrorCallback& error_callback) override {}
87 void TestErrorCallback() {}
89 ScopedVector<BluetoothDiscoverySession> discovery_sessions_;
91 void TestOnStartDiscoverySession(
92 scoped_ptr<device::BluetoothDiscoverySession> discovery_session) {
93 discovery_sessions_.push_back(discovery_session.Pass());
96 void CleanupSessions() { discovery_sessions_.clear(); }
98 void InjectFilteredSession(
99 scoped_ptr<device::BluetoothDiscoveryFilter> discovery_filter) {
100 StartDiscoverySessionWithFilter(
101 discovery_filter.Pass(),
102 base::Bind(&TestBluetoothAdapter::TestOnStartDiscoverySession,
103 base::Unretained(this)),
104 base::Bind(&TestBluetoothAdapter::TestErrorCallback,
105 base::Unretained(this)));
108 protected:
109 ~TestBluetoothAdapter() override {}
111 void AddDiscoverySession(
112 BluetoothDiscoveryFilter* discovery_filter,
113 const base::Closure& callback,
114 const DiscoverySessionErrorCallback& error_callback) override {}
116 void RemoveDiscoverySession(
117 BluetoothDiscoveryFilter* discovery_filter,
118 const base::Closure& callback,
119 const DiscoverySessionErrorCallback& error_callback) override {}
121 void SetDiscoveryFilter(
122 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter,
123 const base::Closure& callback,
124 const DiscoverySessionErrorCallback& error_callback) override {}
126 void RemovePairingDelegateInternal(
127 BluetoothDevice::PairingDelegate* pairing_delegate) override {}
130 class TestPairingDelegate : public BluetoothDevice::PairingDelegate {
131 public:
132 void RequestPinCode(BluetoothDevice* device) override {}
133 void RequestPasskey(BluetoothDevice* device) override {}
134 void DisplayPinCode(BluetoothDevice* device,
135 const std::string& pincode) override {}
136 void DisplayPasskey(BluetoothDevice* device, uint32 passkey) override {}
137 void KeysEntered(BluetoothDevice* device, uint32 entered) override {}
138 void ConfirmPasskey(BluetoothDevice* device, uint32 passkey) override {}
139 void AuthorizePairing(BluetoothDevice* device) override {}
143 TEST(BluetoothAdapterTest, NoDefaultPairingDelegate) {
144 scoped_refptr<BluetoothAdapter> adapter = new TestBluetoothAdapter();
146 // Verify that when there is no registered pairing delegate, NULL is returned.
147 EXPECT_TRUE(adapter->DefaultPairingDelegate() == NULL);
150 TEST(BluetoothAdapterTest, OneDefaultPairingDelegate) {
151 scoped_refptr<BluetoothAdapter> adapter = new TestBluetoothAdapter();
153 // Verify that when there is one registered pairing delegate, it is returned.
154 TestPairingDelegate delegate;
156 adapter->AddPairingDelegate(&delegate,
157 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_LOW);
159 EXPECT_EQ(&delegate, adapter->DefaultPairingDelegate());
162 TEST(BluetoothAdapterTest, SamePriorityDelegates) {
163 scoped_refptr<BluetoothAdapter> adapter = new TestBluetoothAdapter();
165 // Verify that when there are two registered pairing delegates of the same
166 // priority, the first one registered is returned.
167 TestPairingDelegate delegate1, delegate2;
169 adapter->AddPairingDelegate(&delegate1,
170 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_LOW);
171 adapter->AddPairingDelegate(&delegate2,
172 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_LOW);
174 EXPECT_EQ(&delegate1, adapter->DefaultPairingDelegate());
176 // After unregistering the first, the second can be returned.
177 adapter->RemovePairingDelegate(&delegate1);
179 EXPECT_EQ(&delegate2, adapter->DefaultPairingDelegate());
182 TEST(BluetoothAdapterTest, HighestPriorityDelegate) {
183 scoped_refptr<BluetoothAdapter> adapter = new TestBluetoothAdapter();
185 // Verify that when there are two registered pairing delegates, the one with
186 // the highest priority is returned.
187 TestPairingDelegate delegate1, delegate2;
189 adapter->AddPairingDelegate(&delegate1,
190 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_LOW);
191 adapter->AddPairingDelegate(&delegate2,
192 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH);
194 EXPECT_EQ(&delegate2, adapter->DefaultPairingDelegate());
197 TEST(BluetoothAdapterTest, UnregisterDelegate) {
198 scoped_refptr<BluetoothAdapter> adapter = new TestBluetoothAdapter();
200 // Verify that after unregistering a delegate, NULL is returned.
201 TestPairingDelegate delegate;
203 adapter->AddPairingDelegate(&delegate,
204 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_LOW);
205 adapter->RemovePairingDelegate(&delegate);
207 EXPECT_TRUE(adapter->DefaultPairingDelegate() == NULL);
210 TEST(BluetoothAdapterTest, GetMergedDiscoveryFilterEmpty) {
211 scoped_refptr<BluetoothAdapter> adapter = new TestBluetoothAdapter();
212 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter;
214 discovery_filter = adapter->GetMergedDiscoveryFilter();
215 EXPECT_TRUE(discovery_filter.get() == nullptr);
217 discovery_filter = adapter->GetMergedDiscoveryFilterMasked(nullptr);
218 EXPECT_TRUE(discovery_filter.get() == nullptr);
221 TEST(BluetoothAdapterTest, GetMergedDiscoveryFilterRegular) {
222 scoped_refptr<TestBluetoothAdapter> adapter = new TestBluetoothAdapter();
223 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter;
225 // make sure adapter have one session wihout filtering.
226 adapter->InjectFilteredSession(discovery_filter.Pass());
228 // having one reglar session should result in no filter
229 scoped_ptr<BluetoothDiscoveryFilter> resulting_filter =
230 adapter->GetMergedDiscoveryFilter();
231 EXPECT_TRUE(resulting_filter.get() == nullptr);
233 // omiting no filter when having one reglar session should result in no filter
234 resulting_filter = adapter->GetMergedDiscoveryFilterMasked(nullptr);
235 EXPECT_TRUE(resulting_filter.get() == nullptr);
237 adapter->CleanupSessions();
240 TEST(BluetoothAdapterTest, GetMergedDiscoveryFilterRssi) {
241 scoped_refptr<TestBluetoothAdapter> adapter = new TestBluetoothAdapter();
242 int16_t resulting_rssi;
243 uint16_t resulting_pathloss;
244 scoped_ptr<BluetoothDiscoveryFilter> resulting_filter;
246 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter(
247 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
248 df->SetRSSI(-30);
249 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter(df);
251 BluetoothDiscoveryFilter* df2 = new BluetoothDiscoveryFilter(
252 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
253 df2->SetRSSI(-65);
254 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter2(df2);
256 // make sure adapter have one session wihout filtering.
257 adapter->InjectFilteredSession(discovery_filter.Pass());
259 // DO_NOTHING should have no impact
260 resulting_filter = adapter->GetMergedDiscoveryFilter();
261 resulting_filter->GetRSSI(&resulting_rssi);
262 EXPECT_EQ(-30, resulting_rssi);
264 // should not use df2 at all, as it's not associated with adapter yet
265 resulting_filter = adapter->GetMergedDiscoveryFilterMasked(df2);
266 resulting_filter->GetRSSI(&resulting_rssi);
267 EXPECT_EQ(-30, resulting_rssi);
269 adapter->InjectFilteredSession(discovery_filter2.Pass());
271 // result of merging two rssi values should be lower one
272 resulting_filter = adapter->GetMergedDiscoveryFilter();
273 resulting_filter->GetRSSI(&resulting_rssi);
274 EXPECT_EQ(-65, resulting_rssi);
276 // ommit bigger value, result should stay same
277 resulting_filter = adapter->GetMergedDiscoveryFilterMasked(df);
278 resulting_filter->GetRSSI(&resulting_rssi);
279 EXPECT_EQ(-65, resulting_rssi);
281 // ommit lower value, result should change
282 resulting_filter = adapter->GetMergedDiscoveryFilterMasked(df2);
283 resulting_filter->GetRSSI(&resulting_rssi);
284 EXPECT_EQ(-30, resulting_rssi);
286 BluetoothDiscoveryFilter* df3 = new BluetoothDiscoveryFilter(
287 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
288 df3->SetPathloss(60);
289 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter3(df3);
291 // when rssi and pathloss are merged, both should be cleared, becuase there is
292 // no way to tell which filter will be more generic
293 adapter->InjectFilteredSession(discovery_filter3.Pass());
294 resulting_filter = adapter->GetMergedDiscoveryFilter();
295 EXPECT_FALSE(resulting_filter->GetRSSI(&resulting_rssi));
296 EXPECT_FALSE(resulting_filter->GetPathloss(&resulting_pathloss));
298 adapter->CleanupSessions();
301 TEST(BluetoothAdapterTest, GetMergedDiscoveryFilterTransport) {
302 scoped_refptr<TestBluetoothAdapter> adapter = new TestBluetoothAdapter();
303 scoped_ptr<BluetoothDiscoveryFilter> resulting_filter;
305 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter(
306 BluetoothDiscoveryFilter::Transport::TRANSPORT_CLASSIC);
307 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter(df);
309 BluetoothDiscoveryFilter* df2 = new BluetoothDiscoveryFilter(
310 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
311 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter2(df2);
313 adapter->InjectFilteredSession(discovery_filter.Pass());
315 // Just one filter, make sure transport was properly rewritten
316 resulting_filter = adapter->GetMergedDiscoveryFilter();
317 EXPECT_EQ(BluetoothDiscoveryFilter::Transport::TRANSPORT_CLASSIC,
318 resulting_filter->GetTransport());
320 adapter->InjectFilteredSession(discovery_filter2.Pass());
322 // Two filters, should have OR of both transport's
323 resulting_filter = adapter->GetMergedDiscoveryFilter();
324 EXPECT_EQ(BluetoothDiscoveryFilter::Transport::TRANSPORT_DUAL,
325 resulting_filter->GetTransport());
327 // When 1st filter is masked, 2nd filter transport should be returned.
328 resulting_filter = adapter->GetMergedDiscoveryFilterMasked(df);
329 EXPECT_EQ(BluetoothDiscoveryFilter::Transport::TRANSPORT_LE,
330 resulting_filter->GetTransport());
332 // When 2nd filter is masked, 1st filter transport should be returned.
333 resulting_filter = adapter->GetMergedDiscoveryFilterMasked(df2);
334 EXPECT_EQ(BluetoothDiscoveryFilter::Transport::TRANSPORT_CLASSIC,
335 resulting_filter->GetTransport());
337 BluetoothDiscoveryFilter* df3 = new BluetoothDiscoveryFilter(
338 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
339 df3->CopyFrom(BluetoothDiscoveryFilter(
340 BluetoothDiscoveryFilter::Transport::TRANSPORT_DUAL));
341 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter3(df3);
343 // Merging empty filter in should result in empty filter
344 adapter->InjectFilteredSession(discovery_filter3.Pass());
345 resulting_filter = adapter->GetMergedDiscoveryFilter();
346 EXPECT_TRUE(resulting_filter->IsDefault());
348 adapter->CleanupSessions();
351 TEST(BluetoothAdapterTest, GetMergedDiscoveryFilterAllFields) {
352 scoped_refptr<TestBluetoothAdapter> adapter = new TestBluetoothAdapter();
353 int16_t resulting_rssi;
354 std::set<device::BluetoothUUID> resulting_uuids;
356 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter(
357 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
358 df->SetRSSI(-60);
359 df->AddUUID(device::BluetoothUUID("1000"));
360 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter(df);
362 BluetoothDiscoveryFilter* df2 = new BluetoothDiscoveryFilter(
363 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
364 df2->SetRSSI(-85);
365 df2->SetTransport(BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
366 df2->AddUUID(device::BluetoothUUID("1020"));
367 df2->AddUUID(device::BluetoothUUID("1001"));
368 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter2(df2);
370 BluetoothDiscoveryFilter* df3 = new BluetoothDiscoveryFilter(
371 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
372 df3->SetRSSI(-65);
373 df3->SetTransport(BluetoothDiscoveryFilter::Transport::TRANSPORT_CLASSIC);
374 df3->AddUUID(device::BluetoothUUID("1020"));
375 df3->AddUUID(device::BluetoothUUID("1003"));
376 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter3(df3);
378 // make sure adapter have one session wihout filtering.
379 adapter->InjectFilteredSession(discovery_filter.Pass());
380 adapter->InjectFilteredSession(discovery_filter2.Pass());
381 adapter->InjectFilteredSession(discovery_filter3.Pass());
383 scoped_ptr<BluetoothDiscoveryFilter> resulting_filter =
384 adapter->GetMergedDiscoveryFilter();
385 resulting_filter->GetRSSI(&resulting_rssi);
386 resulting_filter->GetUUIDs(resulting_uuids);
387 EXPECT_TRUE(resulting_filter->GetTransport());
388 EXPECT_EQ(BluetoothDiscoveryFilter::Transport::TRANSPORT_DUAL,
389 resulting_filter->GetTransport());
390 EXPECT_EQ(-85, resulting_rssi);
391 EXPECT_EQ(4UL, resulting_uuids.size());
392 EXPECT_TRUE(resulting_uuids.find(device::BluetoothUUID("1000")) !=
393 resulting_uuids.end());
394 EXPECT_TRUE(resulting_uuids.find(device::BluetoothUUID("1001")) !=
395 resulting_uuids.end());
396 EXPECT_TRUE(resulting_uuids.find(device::BluetoothUUID("1003")) !=
397 resulting_uuids.end());
398 EXPECT_TRUE(resulting_uuids.find(device::BluetoothUUID("1020")) !=
399 resulting_uuids.end());
401 resulting_filter = adapter->GetMergedDiscoveryFilterMasked(df);
402 EXPECT_EQ(BluetoothDiscoveryFilter::Transport::TRANSPORT_DUAL,
403 resulting_filter->GetTransport());
405 adapter->CleanupSessions();
408 // TODO(scheib): Enable BluetoothTest fixture tests on all platforms.
409 #if defined(OS_ANDROID) || defined(OS_MACOSX)
410 TEST_F(BluetoothTest, ConstructDefaultAdapter) {
411 InitWithDefaultAdapter();
412 if (!adapter_->IsPresent()) {
413 LOG(WARNING) << "Bluetooth adapter not present; skipping unit test.";
414 return;
416 EXPECT_GT(adapter_->GetAddress().length(), 0u);
417 EXPECT_GT(adapter_->GetName().length(), 0u);
418 EXPECT_TRUE(adapter_->IsPresent());
419 // Don't know on test machines if adapter will be powered or not, but
420 // the call should be safe to make and consistent.
421 EXPECT_EQ(adapter_->IsPowered(), adapter_->IsPowered());
422 EXPECT_FALSE(adapter_->IsDiscoverable());
423 EXPECT_FALSE(adapter_->IsDiscovering());
425 #endif // defined(OS_ANDROID) || defined(OS_MACOSX)
427 // TODO(scheib): Enable BluetoothTest fixture tests on all platforms.
428 #if defined(OS_ANDROID) || defined(OS_MACOSX)
429 TEST_F(BluetoothTest, ConstructWithoutDefaultAdapter) {
430 InitWithoutDefaultAdapter();
431 EXPECT_EQ(adapter_->GetAddress(), "");
432 EXPECT_EQ(adapter_->GetName(), "");
433 EXPECT_FALSE(adapter_->IsPresent());
434 EXPECT_FALSE(adapter_->IsPowered());
435 EXPECT_FALSE(adapter_->IsDiscoverable());
436 EXPECT_FALSE(adapter_->IsDiscovering());
438 #endif // defined(OS_ANDROID) || defined(OS_MACOSX)
440 // TODO(scheib): Enable BluetoothTest fixture tests on all platforms.
441 #if defined(OS_ANDROID) || defined(OS_MACOSX)
442 TEST_F(BluetoothTest, ConstructFakeAdapter) {
443 InitWithFakeAdapter();
444 EXPECT_EQ(adapter_->GetAddress(), kTestAdapterAddress);
445 EXPECT_EQ(adapter_->GetName(), kTestAdapterName);
446 EXPECT_TRUE(adapter_->IsPresent());
447 EXPECT_TRUE(adapter_->IsPowered());
448 EXPECT_FALSE(adapter_->IsDiscoverable());
449 EXPECT_FALSE(adapter_->IsDiscovering());
451 #endif // defined(OS_ANDROID) || defined(OS_MACOSX)
453 // TODO(scheib): Enable BluetoothTest fixture tests on all platforms.
454 #if defined(OS_ANDROID)
455 // Starts and Stops a discovery session.
456 TEST_F(BluetoothTest, DiscoverySession) {
457 InitWithFakeAdapter();
458 EXPECT_FALSE(adapter_->IsDiscovering());
460 adapter_->StartDiscoverySession(GetDiscoverySessionCallback(),
461 GetErrorCallback());
462 base::RunLoop().RunUntilIdle();
463 EXPECT_EQ(1, callback_count_--);
464 EXPECT_EQ(0, error_callback_count_);
465 EXPECT_TRUE(adapter_->IsDiscovering());
466 ASSERT_EQ((size_t)1, discovery_sessions_.size());
467 EXPECT_TRUE(discovery_sessions_[0]->IsActive());
469 discovery_sessions_[0]->Stop(GetCallback(), GetErrorCallback());
470 base::RunLoop().RunUntilIdle();
471 EXPECT_EQ(1, callback_count_--);
472 EXPECT_EQ(0, error_callback_count_);
473 EXPECT_FALSE(adapter_->IsDiscovering());
474 EXPECT_FALSE(discovery_sessions_[0]->IsActive());
476 #endif // defined(OS_ANDROID)
478 #if defined(OS_ANDROID) || defined(OS_MACOSX)
479 // Discovers a device.
480 TEST_F(BluetoothTest, DiscoverLowEnergyDevice) {
481 if (!PlatformSupportsLowEnergy()) {
482 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
483 return;
485 InitWithFakeAdapter();
486 TestBluetoothAdapterObserver observer(adapter_);
488 // Start discovery and find a device.
489 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter(
490 new BluetoothDiscoveryFilter(
491 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE));
492 adapter_->StartDiscoverySessionWithFilter(discovery_filter.Pass(),
493 GetDiscoverySessionCallback(),
494 GetErrorCallback());
495 base::RunLoop().RunUntilIdle();
496 DiscoverLowEnergyDevice(1);
497 base::RunLoop().RunUntilIdle();
498 EXPECT_EQ(1, observer.device_added_count());
499 BluetoothDevice* device = adapter_->GetDevice(observer.last_device_address());
500 EXPECT_TRUE(device);
502 #endif // defined(OS_ANDROID) || defined(OS_MACOSX)
504 #if defined(OS_ANDROID) || defined(OS_MACOSX)
505 // Discovers the same device multiple times.
506 TEST_F(BluetoothTest, DiscoverLowEnergyDeviceTwice) {
507 if (!PlatformSupportsLowEnergy()) {
508 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
509 return;
511 InitWithFakeAdapter();
512 TestBluetoothAdapterObserver observer(adapter_);
514 // Start discovery and find a device.
515 adapter_->StartDiscoverySession(GetDiscoverySessionCallback(),
516 GetErrorCallback());
517 base::RunLoop().RunUntilIdle();
518 DiscoverLowEnergyDevice(1);
519 base::RunLoop().RunUntilIdle();
520 EXPECT_EQ(1, observer.device_added_count());
521 BluetoothDevice* device = adapter_->GetDevice(observer.last_device_address());
522 EXPECT_TRUE(device);
524 // Find the same device again. This should not create a new device object.
525 observer.Reset();
526 DiscoverLowEnergyDevice(1);
527 base::RunLoop().RunUntilIdle();
528 EXPECT_EQ(0, observer.device_added_count());
529 EXPECT_EQ(1u, adapter_->GetDevices().size());
531 #endif // defined(OS_ANDROID) || defined(OS_MACOSX)
533 #if defined(OS_ANDROID) || defined(OS_MACOSX)
534 // Discovers a device, and then again with new Service UUIDs.
535 TEST_F(BluetoothTest, DiscoverLowEnergyDeviceWithUpdatedUUIDs) {
536 if (!PlatformSupportsLowEnergy()) {
537 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
538 return;
540 InitWithFakeAdapter();
541 TestBluetoothAdapterObserver observer(adapter_);
543 // Start discovery and find a device.
544 adapter_->StartDiscoverySession(GetDiscoverySessionCallback(),
545 GetErrorCallback());
546 base::RunLoop().RunUntilIdle();
547 DiscoverLowEnergyDevice(1);
548 base::RunLoop().RunUntilIdle();
549 BluetoothDevice* device = observer.last_device();
551 // Check the initial UUIDs:
552 EXPECT_TRUE(
553 ContainsValue(device->GetUUIDs(), BluetoothUUID(kTestUUIDGenericAccess)));
554 EXPECT_FALSE(ContainsValue(device->GetUUIDs(),
555 BluetoothUUID(kTestUUIDImmediateAlert)));
557 // Discover same device again with updated UUIDs:
558 observer.Reset();
559 DiscoverLowEnergyDevice(2);
560 base::RunLoop().RunUntilIdle();
561 EXPECT_EQ(0, observer.device_added_count());
562 EXPECT_EQ(1, observer.device_changed_count());
563 EXPECT_EQ(1u, adapter_->GetDevices().size());
564 EXPECT_EQ(device, observer.last_device());
566 // Expect new UUIDs:
567 EXPECT_FALSE(
568 ContainsValue(device->GetUUIDs(), BluetoothUUID(kTestUUIDGenericAccess)));
569 EXPECT_TRUE(ContainsValue(device->GetUUIDs(),
570 BluetoothUUID(kTestUUIDImmediateAlert)));
572 // Discover same device again with empty UUIDs:
573 observer.Reset();
574 DiscoverLowEnergyDevice(3);
575 base::RunLoop().RunUntilIdle();
576 EXPECT_EQ(0, observer.device_added_count());
577 EXPECT_EQ(1, observer.device_changed_count());
578 EXPECT_EQ(1u, adapter_->GetDevices().size());
579 EXPECT_EQ(device, observer.last_device());
581 // Expect empty UUIDs:
582 EXPECT_EQ(0u, device->GetUUIDs().size());
584 #endif // defined(OS_ANDROID) || defined(OS_MACOSX)
586 #if defined(OS_ANDROID) || defined(OS_MACOSX)
587 // Discovers multiple devices when addresses vary.
588 TEST_F(BluetoothTest, DiscoverMultipleLowEnergyDevices) {
589 if (!PlatformSupportsLowEnergy()) {
590 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
591 return;
593 InitWithFakeAdapter();
594 TestBluetoothAdapterObserver observer(adapter_);
596 // Start discovery and find a device.
597 adapter_->StartDiscoverySession(GetDiscoverySessionCallback(),
598 GetErrorCallback());
599 base::RunLoop().RunUntilIdle();
600 DiscoverLowEnergyDevice(1);
601 DiscoverLowEnergyDevice(4);
602 base::RunLoop().RunUntilIdle();
603 EXPECT_EQ(2, observer.device_added_count());
604 EXPECT_EQ(2u, adapter_->GetDevices().size());
606 #endif // defined(OS_ANDROID) || defined(OS_MACOSX)
608 } // namespace device