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.
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"
21 using device::BluetoothDevice
;
25 class TestBluetoothAdapter
: public BluetoothAdapter
{
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)));
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
{
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
);
246 scoped_ptr
<BluetoothDiscoveryFilter
> discovery_filter(df
);
248 BluetoothDiscoveryFilter
* df2
= new BluetoothDiscoveryFilter(
249 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE
);
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
);
356 df
->AddUUID(device::BluetoothUUID("1000"));
357 scoped_ptr
<BluetoothDiscoveryFilter
> discovery_filter(df
);
359 BluetoothDiscoveryFilter
* df2
= new BluetoothDiscoveryFilter(
360 BluetoothDiscoveryFilter::Transport::TRANSPORT_LE
);
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
);
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.";
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(),
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)
476 // Discovers a device.
477 TEST_F(BluetoothTest
, DiscoverDevice
) {
478 InitWithFakeAdapter();
479 TestBluetoothAdapterObserver
observer(adapter_
);
481 // Start discovery and find a device.
482 adapter_
->StartDiscoverySession(GetDiscoverySessionCallback(),
484 base::RunLoop().RunUntilIdle();
485 DiscoverLowEnergyDevice(1);
486 base::RunLoop().RunUntilIdle();
487 EXPECT_EQ(1, observer
.device_added_count());
488 BluetoothDevice
* device
= adapter_
->GetDevice(observer
.last_device_address());
491 #endif // defined(OS_ANDROID)
493 #if defined(OS_ANDROID)
494 // Discovers the same device multiple times.
495 TEST_F(BluetoothTest
, DiscoverDeviceTwice
) {
496 InitWithFakeAdapter();
497 TestBluetoothAdapterObserver
observer(adapter_
);
499 // Start discovery and find a device.
500 adapter_
->StartDiscoverySession(GetDiscoverySessionCallback(),
502 base::RunLoop().RunUntilIdle();
503 DiscoverLowEnergyDevice(1);
504 base::RunLoop().RunUntilIdle();
505 EXPECT_EQ(1, observer
.device_added_count());
506 BluetoothDevice
* device
= adapter_
->GetDevice(observer
.last_device_address());
509 // Find the same device again. This should not create a new device object.
511 DiscoverLowEnergyDevice(1);
512 base::RunLoop().RunUntilIdle();
513 EXPECT_EQ(0, observer
.device_added_count());
514 EXPECT_EQ(0, observer
.device_changed_count());
515 EXPECT_EQ(1u, adapter_
->GetDevices().size());
517 #endif // defined(OS_ANDROID)
519 #if defined(OS_ANDROID)
520 // Discovers a device, and then again with new Service UUIDs.
521 TEST_F(BluetoothTest
, DiscoverDeviceWithUpdatedUUIDs
) {
522 InitWithFakeAdapter();
523 TestBluetoothAdapterObserver
observer(adapter_
);
525 // Start discovery and find a device.
526 adapter_
->StartDiscoverySession(GetDiscoverySessionCallback(),
528 base::RunLoop().RunUntilIdle();
529 DiscoverLowEnergyDevice(1);
530 base::RunLoop().RunUntilIdle();
531 BluetoothDevice
* device
= observer
.last_device();
533 // Check the initial UUIDs:
534 EXPECT_TRUE(ContainsValue(device
->GetUUIDs(), BluetoothUUID("1800")));
535 EXPECT_FALSE(ContainsValue(device
->GetUUIDs(), BluetoothUUID("1802")));
537 // Discover same device again with updated UUIDs:
539 DiscoverLowEnergyDevice(2);
540 base::RunLoop().RunUntilIdle();
541 EXPECT_EQ(0, observer
.device_added_count());
542 EXPECT_EQ(1, observer
.device_changed_count());
543 EXPECT_EQ(1u, adapter_
->GetDevices().size());
544 EXPECT_EQ(device
, observer
.last_device());
547 EXPECT_FALSE(ContainsValue(device
->GetUUIDs(), BluetoothUUID("1800")));
548 EXPECT_TRUE(ContainsValue(device
->GetUUIDs(), BluetoothUUID("1802")));
550 // Discover same device again with empty UUIDs:
552 DiscoverLowEnergyDevice(3);
553 base::RunLoop().RunUntilIdle();
554 EXPECT_EQ(0, observer
.device_added_count());
555 EXPECT_EQ(1, observer
.device_changed_count());
556 EXPECT_EQ(1u, adapter_
->GetDevices().size());
557 EXPECT_EQ(device
, observer
.last_device());
559 // Expect empty UUIDs:
560 EXPECT_EQ(0u, device
->GetUUIDs().size());
562 #endif // defined(OS_ANDROID)
564 #if defined(OS_ANDROID)
565 // Discovers multiple devices when addresses vary.
566 TEST_F(BluetoothTest
, DiscoverMultipleDevices
) {
567 InitWithFakeAdapter();
568 TestBluetoothAdapterObserver
observer(adapter_
);
570 // Start discovery and find a device.
571 adapter_
->StartDiscoverySession(GetDiscoverySessionCallback(),
573 base::RunLoop().RunUntilIdle();
574 DiscoverLowEnergyDevice(1);
575 DiscoverLowEnergyDevice(4);
576 base::RunLoop().RunUntilIdle();
577 EXPECT_EQ(2, observer
.device_added_count());
578 EXPECT_EQ(2u, adapter_
->GetDevices().size());
580 #endif // defined(OS_ANDROID)
582 } // namespace device