Update CrOS OOBE throbber to MD throbber; delete old asset
[chromium-blink-merge.git] / device / bluetooth / bluetooth_adapter_unittest.cc
blob7f380ab384a835bc0beee72eb16d931c90a45d32
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(BluetoothDiscoveryFilter* discovery_filter,
112 const base::Closure& callback,
113 const ErrorCallback& error_callback) override {}
115 void RemoveDiscoverySession(BluetoothDiscoveryFilter* discovery_filter,
116 const base::Closure& callback,
117 const ErrorCallback& error_callback) override {}
119 void SetDiscoveryFilter(scoped_ptr<BluetoothDiscoveryFilter> discovery_filter,
120 const base::Closure& callback,
121 const ErrorCallback& error_callback) override {}
123 void RemovePairingDelegateInternal(
124 BluetoothDevice::PairingDelegate* pairing_delegate) override {}
127 class TestPairingDelegate : public BluetoothDevice::PairingDelegate {
128 public:
129 void RequestPinCode(BluetoothDevice* device) override {}
130 void RequestPasskey(BluetoothDevice* device) override {}
131 void DisplayPinCode(BluetoothDevice* device,
132 const std::string& pincode) override {}
133 void DisplayPasskey(BluetoothDevice* device, uint32 passkey) override {}
134 void KeysEntered(BluetoothDevice* device, uint32 entered) override {}
135 void ConfirmPasskey(BluetoothDevice* device, uint32 passkey) override {}
136 void AuthorizePairing(BluetoothDevice* device) override {}
140 TEST(BluetoothAdapterTest, NoDefaultPairingDelegate) {
141 scoped_refptr<BluetoothAdapter> adapter = new TestBluetoothAdapter();
143 // Verify that when there is no registered pairing delegate, NULL is returned.
144 EXPECT_TRUE(adapter->DefaultPairingDelegate() == NULL);
147 TEST(BluetoothAdapterTest, OneDefaultPairingDelegate) {
148 scoped_refptr<BluetoothAdapter> adapter = new TestBluetoothAdapter();
150 // Verify that when there is one registered pairing delegate, it is returned.
151 TestPairingDelegate delegate;
153 adapter->AddPairingDelegate(&delegate,
154 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_LOW);
156 EXPECT_EQ(&delegate, adapter->DefaultPairingDelegate());
159 TEST(BluetoothAdapterTest, SamePriorityDelegates) {
160 scoped_refptr<BluetoothAdapter> adapter = new TestBluetoothAdapter();
162 // Verify that when there are two registered pairing delegates of the same
163 // priority, the first one registered is returned.
164 TestPairingDelegate delegate1, delegate2;
166 adapter->AddPairingDelegate(&delegate1,
167 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_LOW);
168 adapter->AddPairingDelegate(&delegate2,
169 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_LOW);
171 EXPECT_EQ(&delegate1, adapter->DefaultPairingDelegate());
173 // After unregistering the first, the second can be returned.
174 adapter->RemovePairingDelegate(&delegate1);
176 EXPECT_EQ(&delegate2, adapter->DefaultPairingDelegate());
179 TEST(BluetoothAdapterTest, HighestPriorityDelegate) {
180 scoped_refptr<BluetoothAdapter> adapter = new TestBluetoothAdapter();
182 // Verify that when there are two registered pairing delegates, the one with
183 // the highest priority is returned.
184 TestPairingDelegate delegate1, delegate2;
186 adapter->AddPairingDelegate(&delegate1,
187 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_LOW);
188 adapter->AddPairingDelegate(&delegate2,
189 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_HIGH);
191 EXPECT_EQ(&delegate2, adapter->DefaultPairingDelegate());
194 TEST(BluetoothAdapterTest, UnregisterDelegate) {
195 scoped_refptr<BluetoothAdapter> adapter = new TestBluetoothAdapter();
197 // Verify that after unregistering a delegate, NULL is returned.
198 TestPairingDelegate delegate;
200 adapter->AddPairingDelegate(&delegate,
201 BluetoothAdapter::PAIRING_DELEGATE_PRIORITY_LOW);
202 adapter->RemovePairingDelegate(&delegate);
204 EXPECT_TRUE(adapter->DefaultPairingDelegate() == NULL);
207 TEST(BluetoothAdapterTest, GetMergedDiscoveryFilterEmpty) {
208 scoped_refptr<BluetoothAdapter> adapter = new TestBluetoothAdapter();
209 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter;
211 discovery_filter = adapter->GetMergedDiscoveryFilter();
212 EXPECT_TRUE(discovery_filter.get() == nullptr);
214 discovery_filter = adapter->GetMergedDiscoveryFilterMasked(nullptr);
215 EXPECT_TRUE(discovery_filter.get() == nullptr);
218 TEST(BluetoothAdapterTest, GetMergedDiscoveryFilterRegular) {
219 scoped_refptr<TestBluetoothAdapter> adapter = new TestBluetoothAdapter();
220 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter;
222 // make sure adapter have one session wihout filtering.
223 adapter->InjectFilteredSession(discovery_filter.Pass());
225 // having one reglar session should result in no filter
226 scoped_ptr<BluetoothDiscoveryFilter> resulting_filter =
227 adapter->GetMergedDiscoveryFilter();
228 EXPECT_TRUE(resulting_filter.get() == nullptr);
230 // omiting no filter when having one reglar session should result in no filter
231 resulting_filter = adapter->GetMergedDiscoveryFilterMasked(nullptr);
232 EXPECT_TRUE(resulting_filter.get() == nullptr);
234 adapter->CleanupSessions();
237 TEST(BluetoothAdapterTest, GetMergedDiscoveryFilterRssi) {
238 scoped_refptr<TestBluetoothAdapter> adapter = new TestBluetoothAdapter();
239 int16_t resulting_rssi;
240 uint16_t resulting_pathloss;
241 scoped_ptr<BluetoothDiscoveryFilter> resulting_filter;
243 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter(
244 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
245 df->SetRSSI(-30);
246 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter(df);
248 BluetoothDiscoveryFilter* df2 = new BluetoothDiscoveryFilter(
249 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
250 df2->SetRSSI(-65);
251 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter2(df2);
253 // make sure adapter have one session wihout filtering.
254 adapter->InjectFilteredSession(discovery_filter.Pass());
256 // DO_NOTHING should have no impact
257 resulting_filter = adapter->GetMergedDiscoveryFilter();
258 resulting_filter->GetRSSI(&resulting_rssi);
259 EXPECT_EQ(-30, resulting_rssi);
261 // should not use df2 at all, as it's not associated with adapter yet
262 resulting_filter = adapter->GetMergedDiscoveryFilterMasked(df2);
263 resulting_filter->GetRSSI(&resulting_rssi);
264 EXPECT_EQ(-30, resulting_rssi);
266 adapter->InjectFilteredSession(discovery_filter2.Pass());
268 // result of merging two rssi values should be lower one
269 resulting_filter = adapter->GetMergedDiscoveryFilter();
270 resulting_filter->GetRSSI(&resulting_rssi);
271 EXPECT_EQ(-65, resulting_rssi);
273 // ommit bigger value, result should stay same
274 resulting_filter = adapter->GetMergedDiscoveryFilterMasked(df);
275 resulting_filter->GetRSSI(&resulting_rssi);
276 EXPECT_EQ(-65, resulting_rssi);
278 // ommit lower value, result should change
279 resulting_filter = adapter->GetMergedDiscoveryFilterMasked(df2);
280 resulting_filter->GetRSSI(&resulting_rssi);
281 EXPECT_EQ(-30, resulting_rssi);
283 BluetoothDiscoveryFilter* df3 = new BluetoothDiscoveryFilter(
284 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
285 df3->SetPathloss(60);
286 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter3(df3);
288 // when rssi and pathloss are merged, both should be cleared, becuase there is
289 // no way to tell which filter will be more generic
290 adapter->InjectFilteredSession(discovery_filter3.Pass());
291 resulting_filter = adapter->GetMergedDiscoveryFilter();
292 EXPECT_FALSE(resulting_filter->GetRSSI(&resulting_rssi));
293 EXPECT_FALSE(resulting_filter->GetPathloss(&resulting_pathloss));
295 adapter->CleanupSessions();
298 TEST(BluetoothAdapterTest, GetMergedDiscoveryFilterTransport) {
299 scoped_refptr<TestBluetoothAdapter> adapter = new TestBluetoothAdapter();
300 scoped_ptr<BluetoothDiscoveryFilter> resulting_filter;
302 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter(
303 BluetoothDiscoveryFilter::Transport::TRANSPORT_CLASSIC);
304 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter(df);
306 BluetoothDiscoveryFilter* df2 = new BluetoothDiscoveryFilter(
307 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
308 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter2(df2);
310 adapter->InjectFilteredSession(discovery_filter.Pass());
312 // Just one filter, make sure transport was properly rewritten
313 resulting_filter = adapter->GetMergedDiscoveryFilter();
314 EXPECT_EQ(BluetoothDiscoveryFilter::Transport::TRANSPORT_CLASSIC,
315 resulting_filter->GetTransport());
317 adapter->InjectFilteredSession(discovery_filter2.Pass());
319 // Two filters, should have OR of both transport's
320 resulting_filter = adapter->GetMergedDiscoveryFilter();
321 EXPECT_EQ(BluetoothDiscoveryFilter::Transport::TRANSPORT_DUAL,
322 resulting_filter->GetTransport());
324 // When 1st filter is masked, 2nd filter transport should be returned.
325 resulting_filter = adapter->GetMergedDiscoveryFilterMasked(df);
326 EXPECT_EQ(BluetoothDiscoveryFilter::Transport::TRANSPORT_LE,
327 resulting_filter->GetTransport());
329 // When 2nd filter is masked, 1st filter transport should be returned.
330 resulting_filter = adapter->GetMergedDiscoveryFilterMasked(df2);
331 EXPECT_EQ(BluetoothDiscoveryFilter::Transport::TRANSPORT_CLASSIC,
332 resulting_filter->GetTransport());
334 BluetoothDiscoveryFilter* df3 = new BluetoothDiscoveryFilter(
335 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
336 df3->CopyFrom(BluetoothDiscoveryFilter(
337 BluetoothDiscoveryFilter::Transport::TRANSPORT_DUAL));
338 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter3(df3);
340 // Merging empty filter in should result in empty filter
341 adapter->InjectFilteredSession(discovery_filter3.Pass());
342 resulting_filter = adapter->GetMergedDiscoveryFilter();
343 EXPECT_TRUE(resulting_filter->IsDefault());
345 adapter->CleanupSessions();
348 TEST(BluetoothAdapterTest, GetMergedDiscoveryFilterAllFields) {
349 scoped_refptr<TestBluetoothAdapter> adapter = new TestBluetoothAdapter();
350 int16_t resulting_rssi;
351 std::set<device::BluetoothUUID> resulting_uuids;
353 BluetoothDiscoveryFilter* df = new BluetoothDiscoveryFilter(
354 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
355 df->SetRSSI(-60);
356 df->AddUUID(device::BluetoothUUID("1000"));
357 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter(df);
359 BluetoothDiscoveryFilter* df2 = new BluetoothDiscoveryFilter(
360 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
361 df2->SetRSSI(-85);
362 df2->SetTransport(BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
363 df2->AddUUID(device::BluetoothUUID("1020"));
364 df2->AddUUID(device::BluetoothUUID("1001"));
365 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter2(df2);
367 BluetoothDiscoveryFilter* df3 = new BluetoothDiscoveryFilter(
368 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE);
369 df3->SetRSSI(-65);
370 df3->SetTransport(BluetoothDiscoveryFilter::Transport::TRANSPORT_CLASSIC);
371 df3->AddUUID(device::BluetoothUUID("1020"));
372 df3->AddUUID(device::BluetoothUUID("1003"));
373 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter3(df3);
375 // make sure adapter have one session wihout filtering.
376 adapter->InjectFilteredSession(discovery_filter.Pass());
377 adapter->InjectFilteredSession(discovery_filter2.Pass());
378 adapter->InjectFilteredSession(discovery_filter3.Pass());
380 scoped_ptr<BluetoothDiscoveryFilter> resulting_filter =
381 adapter->GetMergedDiscoveryFilter();
382 resulting_filter->GetRSSI(&resulting_rssi);
383 resulting_filter->GetUUIDs(resulting_uuids);
384 EXPECT_TRUE(resulting_filter->GetTransport());
385 EXPECT_EQ(BluetoothDiscoveryFilter::Transport::TRANSPORT_DUAL,
386 resulting_filter->GetTransport());
387 EXPECT_EQ(-85, resulting_rssi);
388 EXPECT_EQ(4UL, resulting_uuids.size());
389 EXPECT_TRUE(resulting_uuids.find(device::BluetoothUUID("1000")) !=
390 resulting_uuids.end());
391 EXPECT_TRUE(resulting_uuids.find(device::BluetoothUUID("1001")) !=
392 resulting_uuids.end());
393 EXPECT_TRUE(resulting_uuids.find(device::BluetoothUUID("1003")) !=
394 resulting_uuids.end());
395 EXPECT_TRUE(resulting_uuids.find(device::BluetoothUUID("1020")) !=
396 resulting_uuids.end());
398 resulting_filter = adapter->GetMergedDiscoveryFilterMasked(df);
399 EXPECT_EQ(BluetoothDiscoveryFilter::Transport::TRANSPORT_DUAL,
400 resulting_filter->GetTransport());
402 adapter->CleanupSessions();
405 // TODO(scheib): Enable BluetoothTest fixture tests on all platforms.
406 #if defined(OS_ANDROID) || defined(OS_MACOSX)
407 TEST_F(BluetoothTest, ConstructDefaultAdapter) {
408 InitWithDefaultAdapter();
409 if (!adapter_->IsPresent()) {
410 LOG(WARNING) << "Bluetooth adapter not present; skipping unit test.";
411 return;
413 EXPECT_GT(adapter_->GetAddress().length(), 0u);
414 EXPECT_GT(adapter_->GetName().length(), 0u);
415 EXPECT_TRUE(adapter_->IsPresent());
416 // Don't know on test machines if adapter will be powered or not, but
417 // the call should be safe to make and consistent.
418 EXPECT_EQ(adapter_->IsPowered(), adapter_->IsPowered());
419 EXPECT_FALSE(adapter_->IsDiscoverable());
420 EXPECT_FALSE(adapter_->IsDiscovering());
422 #endif // defined(OS_ANDROID) || defined(OS_MACOSX)
424 // TODO(scheib): Enable BluetoothTest fixture tests on all platforms.
425 #if defined(OS_ANDROID) || defined(OS_MACOSX)
426 TEST_F(BluetoothTest, ConstructWithoutDefaultAdapter) {
427 InitWithoutDefaultAdapter();
428 EXPECT_EQ(adapter_->GetAddress(), "");
429 EXPECT_EQ(adapter_->GetName(), "");
430 EXPECT_FALSE(adapter_->IsPresent());
431 EXPECT_FALSE(adapter_->IsPowered());
432 EXPECT_FALSE(adapter_->IsDiscoverable());
433 EXPECT_FALSE(adapter_->IsDiscovering());
435 #endif // defined(OS_ANDROID) || defined(OS_MACOSX)
437 // TODO(scheib): Enable BluetoothTest fixture tests on all platforms.
438 #if defined(OS_ANDROID) || defined(OS_MACOSX)
439 TEST_F(BluetoothTest, ConstructFakeAdapter) {
440 InitWithFakeAdapter();
441 EXPECT_EQ(adapter_->GetAddress(), kTestAdapterAddress);
442 EXPECT_EQ(adapter_->GetName(), kTestAdapterName);
443 EXPECT_TRUE(adapter_->IsPresent());
444 EXPECT_TRUE(adapter_->IsPowered());
445 EXPECT_FALSE(adapter_->IsDiscoverable());
446 EXPECT_FALSE(adapter_->IsDiscovering());
448 #endif // defined(OS_ANDROID) || defined(OS_MACOSX)
450 // TODO(scheib): Enable BluetoothTest fixture tests on all platforms.
451 #if defined(OS_ANDROID)
452 // Starts and Stops a discovery session.
453 TEST_F(BluetoothTest, DiscoverySession) {
454 InitWithFakeAdapter();
455 EXPECT_FALSE(adapter_->IsDiscovering());
457 adapter_->StartDiscoverySession(GetDiscoverySessionCallback(),
458 GetErrorCallback());
459 base::RunLoop().RunUntilIdle();
460 EXPECT_EQ(1, callback_count_--);
461 EXPECT_EQ(0, error_callback_count_);
462 EXPECT_TRUE(adapter_->IsDiscovering());
463 ASSERT_EQ((size_t)1, discovery_sessions_.size());
464 EXPECT_TRUE(discovery_sessions_[0]->IsActive());
466 discovery_sessions_[0]->Stop(GetCallback(), GetErrorCallback());
467 base::RunLoop().RunUntilIdle();
468 EXPECT_EQ(1, callback_count_--);
469 EXPECT_EQ(0, error_callback_count_);
470 EXPECT_FALSE(adapter_->IsDiscovering());
471 EXPECT_FALSE(discovery_sessions_[0]->IsActive());
473 #endif // defined(OS_ANDROID)
475 #if defined(OS_ANDROID) || defined(OS_MACOSX)
476 // Discovers a device.
477 TEST_F(BluetoothTest, DiscoverLowEnergyDevice) {
478 if (!PlatformSupportsLowEnergy()) {
479 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
480 return;
482 InitWithFakeAdapter();
483 TestBluetoothAdapterObserver observer(adapter_);
485 // Start discovery and find a device.
486 scoped_ptr<BluetoothDiscoveryFilter> discovery_filter(
487 new BluetoothDiscoveryFilter(
488 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE));
489 adapter_->StartDiscoverySessionWithFilter(discovery_filter.Pass(),
490 GetDiscoverySessionCallback(),
491 GetErrorCallback());
492 base::RunLoop().RunUntilIdle();
493 DiscoverLowEnergyDevice(1);
494 base::RunLoop().RunUntilIdle();
495 EXPECT_EQ(1, observer.device_added_count());
496 BluetoothDevice* device = adapter_->GetDevice(observer.last_device_address());
497 EXPECT_TRUE(device);
499 #endif // defined(OS_ANDROID) || defined(OS_MACOSX)
501 #if defined(OS_ANDROID) || defined(OS_MACOSX)
502 // Discovers the same device multiple times.
503 TEST_F(BluetoothTest, DiscoverLowEnergyDeviceTwice) {
504 if (!PlatformSupportsLowEnergy()) {
505 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
506 return;
508 InitWithFakeAdapter();
509 TestBluetoothAdapterObserver observer(adapter_);
511 // Start discovery and find a device.
512 adapter_->StartDiscoverySession(GetDiscoverySessionCallback(),
513 GetErrorCallback());
514 base::RunLoop().RunUntilIdle();
515 DiscoverLowEnergyDevice(1);
516 base::RunLoop().RunUntilIdle();
517 EXPECT_EQ(1, observer.device_added_count());
518 BluetoothDevice* device = adapter_->GetDevice(observer.last_device_address());
519 EXPECT_TRUE(device);
521 // Find the same device again. This should not create a new device object.
522 observer.Reset();
523 DiscoverLowEnergyDevice(1);
524 base::RunLoop().RunUntilIdle();
525 EXPECT_EQ(0, observer.device_added_count());
526 EXPECT_EQ(1u, adapter_->GetDevices().size());
528 #endif // defined(OS_ANDROID) || defined(OS_MACOSX)
530 #if defined(OS_ANDROID) || defined(OS_MACOSX)
531 // Discovers a device, and then again with new Service UUIDs.
532 TEST_F(BluetoothTest, DiscoverLowEnergyDeviceWithUpdatedUUIDs) {
533 if (!PlatformSupportsLowEnergy()) {
534 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
535 return;
537 InitWithFakeAdapter();
538 TestBluetoothAdapterObserver observer(adapter_);
540 // Start discovery and find a device.
541 adapter_->StartDiscoverySession(GetDiscoverySessionCallback(),
542 GetErrorCallback());
543 base::RunLoop().RunUntilIdle();
544 DiscoverLowEnergyDevice(1);
545 base::RunLoop().RunUntilIdle();
546 BluetoothDevice* device = observer.last_device();
548 // Check the initial UUIDs:
549 EXPECT_TRUE(
550 ContainsValue(device->GetUUIDs(), BluetoothUUID(kTestUUIDGenericAccess)));
551 EXPECT_FALSE(ContainsValue(device->GetUUIDs(),
552 BluetoothUUID(kTestUUIDImmediateAlert)));
554 // Discover same device again with updated UUIDs:
555 observer.Reset();
556 DiscoverLowEnergyDevice(2);
557 base::RunLoop().RunUntilIdle();
558 EXPECT_EQ(0, observer.device_added_count());
559 EXPECT_EQ(1, observer.device_changed_count());
560 EXPECT_EQ(1u, adapter_->GetDevices().size());
561 EXPECT_EQ(device, observer.last_device());
563 // Expect new UUIDs:
564 EXPECT_FALSE(
565 ContainsValue(device->GetUUIDs(), BluetoothUUID(kTestUUIDGenericAccess)));
566 EXPECT_TRUE(ContainsValue(device->GetUUIDs(),
567 BluetoothUUID(kTestUUIDImmediateAlert)));
569 // Discover same device again with empty UUIDs:
570 observer.Reset();
571 DiscoverLowEnergyDevice(3);
572 base::RunLoop().RunUntilIdle();
573 EXPECT_EQ(0, observer.device_added_count());
574 EXPECT_EQ(1, observer.device_changed_count());
575 EXPECT_EQ(1u, adapter_->GetDevices().size());
576 EXPECT_EQ(device, observer.last_device());
578 // Expect empty UUIDs:
579 EXPECT_EQ(0u, device->GetUUIDs().size());
581 #endif // defined(OS_ANDROID) || defined(OS_MACOSX)
583 #if defined(OS_ANDROID) || defined(OS_MACOSX)
584 // Discovers multiple devices when addresses vary.
585 TEST_F(BluetoothTest, DiscoverMultipleLowEnergyDevices) {
586 if (!PlatformSupportsLowEnergy()) {
587 LOG(WARNING) << "Low Energy Bluetooth unavailable, skipping unit test.";
588 return;
590 InitWithFakeAdapter();
591 TestBluetoothAdapterObserver observer(adapter_);
593 // Start discovery and find a device.
594 adapter_->StartDiscoverySession(GetDiscoverySessionCallback(),
595 GetErrorCallback());
596 base::RunLoop().RunUntilIdle();
597 DiscoverLowEnergyDevice(1);
598 DiscoverLowEnergyDevice(4);
599 base::RunLoop().RunUntilIdle();
600 EXPECT_EQ(2, observer.device_added_count());
601 EXPECT_EQ(2u, adapter_->GetDevices().size());
603 #endif // defined(OS_ANDROID) || defined(OS_MACOSX)
605 } // namespace device