1 // Copyright 2015 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 "components/proximity_auth/ble/bluetooth_low_energy_connection_finder.h"
10 #include "base/memory/ref_counted.h"
11 #include "base/memory/scoped_ptr.h"
12 #include "base/message_loop/message_loop.h"
13 #include "base/run_loop.h"
14 #include "base/time/time.h"
15 #include "components/proximity_auth/ble/bluetooth_low_energy_device_whitelist.h"
16 #include "components/proximity_auth/connection.h"
17 #include "components/proximity_auth/remote_device.h"
18 #include "components/proximity_auth/wire_message.h"
19 #include "device/bluetooth/bluetooth_adapter_factory.h"
20 #include "device/bluetooth/bluetooth_uuid.h"
21 #include "device/bluetooth/test/mock_bluetooth_adapter.h"
22 #include "device/bluetooth/test/mock_bluetooth_device.h"
23 #include "device/bluetooth/test/mock_bluetooth_discovery_session.h"
24 #include "device/bluetooth/test/mock_bluetooth_gatt_connection.h"
25 #include "testing/gmock/include/gmock/gmock.h"
26 #include "testing/gtest/include/gtest/gtest.h"
29 using testing::AtLeast
;
30 using testing::NiceMock
;
31 using testing::Return
;
32 using testing::StrictMock
;
33 using testing::SaveArg
;
35 namespace proximity_auth
{
38 const char kDeviceName
[] = "Device name";
39 const char kBluetoothAddress
[] = "11:22:33:44:55:66";
40 const RemoteDevice kRemoteDevice
= {kDeviceName
, kBluetoothAddress
};
42 const char kServiceUUID
[] = "DEADBEEF-CAFE-FEED-FOOD-D15EA5EBEEEF";
43 const char kToPeripheralCharUUID
[] = "FBAE09F2-0482-11E5-8418-1697F925EC7B";
44 const char kFromPeripheralCharUUID
[] = "5539ED10-0483-11E5-8418-1697F925EC7B";
46 const char kOtherUUID
[] = "AAAAAAAA-AAAA-AAAA-AAAA-D15EA5EBEEEF";
47 const char kOtherBluetoothAddress
[] = "00:00:00:00:00:00";
49 const int kMaxNumberOfAttempts
= 2;
51 class MockConnection
: public Connection
{
53 MockConnection() : Connection(kRemoteDevice
) {}
54 ~MockConnection() override
{}
56 MOCK_METHOD0(Connect
, void());
58 using Connection::SetStatus
;
61 void Disconnect() override
{}
62 void SendMessageImpl(scoped_ptr
<WireMessage
> message
) override
{}
64 DISALLOW_COPY_AND_ASSIGN(MockConnection
);
67 class MockBluetoothLowEnergyDeviceWhitelist
68 : public BluetoothLowEnergyDeviceWhitelist
{
70 MockBluetoothLowEnergyDeviceWhitelist()
71 : BluetoothLowEnergyDeviceWhitelist(nullptr) {}
72 ~MockBluetoothLowEnergyDeviceWhitelist() override
{}
74 MOCK_CONST_METHOD1(HasDeviceWithAddress
, bool(const std::string
&));
77 class MockBluetoothLowEnergyConnectionFinder
78 : public BluetoothLowEnergyConnectionFinder
{
80 MockBluetoothLowEnergyConnectionFinder(
81 const BluetoothLowEnergyDeviceWhitelist
* device_whitelist
)
82 : BluetoothLowEnergyConnectionFinder(kServiceUUID
,
83 kToPeripheralCharUUID
,
84 kFromPeripheralCharUUID
,
86 kMaxNumberOfAttempts
) {
87 SetDelayForTesting(base::TimeDelta());
90 ~MockBluetoothLowEnergyConnectionFinder() override
{}
92 // Mock methods don't support return type scoped_ptr<>. This is a possible
93 // workaround: mock a proxy method to be called by the target overrided method
94 // (CreateConnection).
95 MOCK_METHOD0(CreateConnectionProxy
, Connection
*());
97 // Creates a mock connection and sets an expectation that the mock connection
98 // finder's CreateConnection() method will be called and will return the
99 // created connection. Returns a reference to the created connection.
100 // NOTE: The returned connection's lifetime is managed by the connection
102 MockConnection
* ExpectCreateConnection() {
103 scoped_ptr
<MockConnection
> connection(new NiceMock
<MockConnection
>());
104 MockConnection
* connection_alias
= connection
.get();
105 EXPECT_CALL(*this, CreateConnectionProxy())
106 .WillOnce(Return(connection
.release()));
107 return connection_alias
;
110 MOCK_METHOD0(CloseGattConnectionProxy
, void(void));
113 scoped_ptr
<Connection
> CreateConnection(
114 scoped_ptr
<device::BluetoothGattConnection
> gatt_connection
) override
{
115 return make_scoped_ptr(CreateConnectionProxy());
118 void CloseGattConnection(
119 scoped_ptr
<device::BluetoothGattConnection
> gatt_connection
) override
{
120 BluetoothLowEnergyConnectionFinder::CloseGattConnection(
121 gatt_connection
.Pass());
122 CloseGattConnectionProxy();
126 DISALLOW_COPY_AND_ASSIGN(MockBluetoothLowEnergyConnectionFinder
);
131 class ProximityAuthBluetoothLowEnergyConnectionFinderTest
132 : public testing::Test
{
134 ProximityAuthBluetoothLowEnergyConnectionFinderTest()
135 : adapter_(new NiceMock
<device::MockBluetoothAdapter
>),
136 connection_callback_(
137 base::Bind(&ProximityAuthBluetoothLowEnergyConnectionFinderTest::
139 base::Unretained(this))),
140 device_(new NiceMock
<device::MockBluetoothDevice
>(adapter_
.get(),
146 device_whitelist_(new MockBluetoothLowEnergyDeviceWhitelist()),
147 last_discovery_session_alias_(nullptr) {
148 device::BluetoothAdapterFactory::SetAdapterForTesting(adapter_
);
150 std::vector
<const device::BluetoothDevice
*> devices
;
151 ON_CALL(*adapter_
, GetDevices()).WillByDefault(Return(devices
));
153 ON_CALL(*adapter_
, IsPresent()).WillByDefault(Return(true));
154 ON_CALL(*adapter_
, IsPowered()).WillByDefault(Return(true));
156 ON_CALL(*device_whitelist_
, HasDeviceWithAddress(_
))
157 .WillByDefault(Return(false));
160 void OnConnectionFound(scoped_ptr
<Connection
> connection
) {
161 last_found_connection_
= connection
.Pass();
164 void FindAndExpectStartDiscovery(
165 BluetoothLowEnergyConnectionFinder
& connection_finder
) {
166 device::BluetoothAdapter::DiscoverySessionCallback discovery_callback
;
167 scoped_ptr
<device::MockBluetoothDiscoverySession
> discovery_session(
168 new NiceMock
<device::MockBluetoothDiscoverySession
>());
169 last_discovery_session_alias_
= discovery_session
.get();
171 // Starting a discovery session. StartDiscoveryWithFilterRaw is a proxy for
172 // StartDiscoveryWithFilter.
173 EXPECT_CALL(*adapter_
, StartDiscoverySessionWithFilterRaw(_
, _
, _
))
174 .WillOnce(SaveArg
<1>(&discovery_callback
));
175 EXPECT_CALL(*adapter_
, AddObserver(_
));
176 ON_CALL(*last_discovery_session_alias_
, IsActive())
177 .WillByDefault(Return(true));
178 connection_finder
.Find(connection_callback_
);
179 ASSERT_FALSE(discovery_callback
.is_null());
180 discovery_callback
.Run(discovery_session
.Pass());
183 void ExpectStopDiscoveryAndRemoveObserver() {
184 EXPECT_CALL(*last_discovery_session_alias_
, Stop(_
, _
)).Times(AtLeast(1));
185 EXPECT_CALL(*adapter_
, RemoveObserver(_
)).Times(AtLeast(1));
188 // Prepare |device_| with |uuid|.
189 void PrepareDevice(const std::string
& uuid
) {
190 std::vector
<device::BluetoothUUID
> uuids
;
191 uuids
.push_back(device::BluetoothUUID(uuid
));
192 ON_CALL(*device_
, GetUUIDs()).WillByDefault(Return(uuids
));
195 // Prepare expectations to add/change a right device.
196 void PrepareForNewRightDevice(
197 const std::string
& uuid
,
198 device::BluetoothDevice::GattConnectionCallback
& callback
) {
200 ON_CALL(*device_
, IsPaired()).WillByDefault(Return(true));
201 EXPECT_CALL(*device_
, CreateGattConnection(_
, _
))
202 .WillOnce(SaveArg
<0>(&callback
));
205 // Prepare expectations to add/change a wrong device.
206 void PrepareForNewWrongDevice(const std::string
& uuid
) {
208 ON_CALL(*device_
, IsPaired()).WillByDefault(Return(true));
209 EXPECT_CALL(*device_
, CreateGattConnection(_
, _
)).Times(0);
212 scoped_refptr
<device::MockBluetoothAdapter
> adapter_
;
213 ConnectionFinder::ConnectionCallback connection_callback_
;
214 scoped_ptr
<device::MockBluetoothDevice
> device_
;
215 scoped_ptr
<Connection
> last_found_connection_
;
216 scoped_ptr
<MockBluetoothLowEnergyDeviceWhitelist
> device_whitelist_
;
217 device::MockBluetoothDiscoverySession
* last_discovery_session_alias_
;
220 base::MessageLoop message_loop_
;
223 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest
,
224 ConstructAndDestroyDoesntCrash
) {
225 // Destroying a BluetoothConnectionFinder for which Find() has not been called
227 BluetoothLowEnergyConnectionFinder
connection_finder(
228 kServiceUUID
, kToPeripheralCharUUID
, kFromPeripheralCharUUID
,
229 device_whitelist_
.get(), kMaxNumberOfAttempts
);
232 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest
,
233 Find_StartsDiscoverySession
) {
234 BluetoothLowEnergyConnectionFinder
connection_finder(
235 kServiceUUID
, kToPeripheralCharUUID
, kFromPeripheralCharUUID
,
236 device_whitelist_
.get(), kMaxNumberOfAttempts
);
238 EXPECT_CALL(*adapter_
, StartDiscoverySessionWithFilterRaw(_
, _
, _
));
239 EXPECT_CALL(*adapter_
, AddObserver(_
));
240 connection_finder
.Find(connection_callback_
);
243 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest
,
244 Find_StopsDiscoverySessionBeforeDestroying
) {
245 BluetoothLowEnergyConnectionFinder
connection_finder(
246 kServiceUUID
, kToPeripheralCharUUID
, kFromPeripheralCharUUID
,
247 device_whitelist_
.get(), kMaxNumberOfAttempts
);
249 device::BluetoothAdapter::DiscoverySessionCallback discovery_callback
;
250 scoped_ptr
<device::MockBluetoothDiscoverySession
> discovery_session(
251 new NiceMock
<device::MockBluetoothDiscoverySession
>());
252 device::MockBluetoothDiscoverySession
* discovery_session_alias
=
253 discovery_session
.get();
255 EXPECT_CALL(*adapter_
, StartDiscoverySessionWithFilterRaw(_
, _
, _
))
256 .WillOnce(SaveArg
<1>(&discovery_callback
));
257 ON_CALL(*discovery_session_alias
, IsActive()).WillByDefault(Return(true));
258 EXPECT_CALL(*adapter_
, AddObserver(_
));
259 connection_finder
.Find(connection_callback_
);
261 EXPECT_CALL(*discovery_session_alias
, Stop(_
, _
));
262 ASSERT_FALSE(discovery_callback
.is_null());
263 discovery_callback
.Run(discovery_session
.Pass());
265 EXPECT_CALL(*adapter_
, RemoveObserver(_
));
268 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest
,
269 Find_CreatesGattConnectionWhenWhitelistedDeviceIsAdded
) {
270 BluetoothLowEnergyConnectionFinder
connection_finder(
271 kServiceUUID
, kToPeripheralCharUUID
, kFromPeripheralCharUUID
,
272 device_whitelist_
.get(), kMaxNumberOfAttempts
);
273 device::BluetoothDevice::GattConnectionCallback gatt_connection_callback
;
274 FindAndExpectStartDiscovery(connection_finder
);
275 ExpectStopDiscoveryAndRemoveObserver();
277 std::vector
<device::BluetoothUUID
> uuids
;
278 ON_CALL(*device_
, GetUUIDs()).WillByDefault(Return(uuids
));
279 ON_CALL(*device_
, IsPaired()).WillByDefault(Return(true));
280 ON_CALL(*device_whitelist_
, HasDeviceWithAddress(_
))
281 .WillByDefault(Return(true));
283 EXPECT_CALL(*device_
, CreateGattConnection(_
, _
))
284 .WillOnce(SaveArg
<0>(&gatt_connection_callback
));
285 EXPECT_TRUE(gatt_connection_callback
.is_null());
286 connection_finder
.DeviceAdded(adapter_
.get(), device_
.get());
287 EXPECT_FALSE(gatt_connection_callback
.is_null());
290 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest
,
291 Find_CreatesGattConnectionWhenRightDeviceIsAdded
) {
292 BluetoothLowEnergyConnectionFinder
connection_finder(
293 kServiceUUID
, kToPeripheralCharUUID
, kFromPeripheralCharUUID
,
294 device_whitelist_
.get(), kMaxNumberOfAttempts
);
295 device::BluetoothDevice::GattConnectionCallback gatt_connection_callback
;
296 FindAndExpectStartDiscovery(connection_finder
);
297 ExpectStopDiscoveryAndRemoveObserver();
299 PrepareForNewRightDevice(kServiceUUID
, gatt_connection_callback
);
300 connection_finder
.DeviceAdded(adapter_
.get(), device_
.get());
301 ASSERT_FALSE(gatt_connection_callback
.is_null());
304 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest
,
305 Find_DoesntCreateGattConnectionWhenWrongDeviceIsAdded
) {
306 BluetoothLowEnergyConnectionFinder
connection_finder(
307 kServiceUUID
, kToPeripheralCharUUID
, kFromPeripheralCharUUID
,
308 device_whitelist_
.get(), kMaxNumberOfAttempts
);
309 FindAndExpectStartDiscovery(connection_finder
);
310 ExpectStopDiscoveryAndRemoveObserver();
312 PrepareForNewWrongDevice(kOtherUUID
);
313 connection_finder
.DeviceAdded(adapter_
.get(), device_
.get());
316 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest
,
317 Find_CreatesGattConnectionWhenRightDeviceIsChanged
) {
318 BluetoothLowEnergyConnectionFinder
connection_finder(
319 kServiceUUID
, kToPeripheralCharUUID
, kFromPeripheralCharUUID
,
320 device_whitelist_
.get(), kMaxNumberOfAttempts
);
321 device::BluetoothDevice::GattConnectionCallback gatt_connection_callback
;
322 FindAndExpectStartDiscovery(connection_finder
);
323 ExpectStopDiscoveryAndRemoveObserver();
325 PrepareForNewRightDevice(kServiceUUID
, gatt_connection_callback
);
326 connection_finder
.DeviceChanged(adapter_
.get(), device_
.get());
327 ASSERT_FALSE(gatt_connection_callback
.is_null());
330 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest
,
331 Find_DoesntCreateGattConnectionWhenWrongDeviceIsChanged
) {
332 BluetoothLowEnergyConnectionFinder
connection_finder(
333 kServiceUUID
, kToPeripheralCharUUID
, kFromPeripheralCharUUID
,
334 device_whitelist_
.get(), kMaxNumberOfAttempts
);
335 FindAndExpectStartDiscovery(connection_finder
);
336 ExpectStopDiscoveryAndRemoveObserver();
338 PrepareForNewWrongDevice(kOtherUUID
);
339 connection_finder
.DeviceChanged(adapter_
.get(), device_
.get());
342 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest
,
343 Find_CreatesTwoGattConnections
) {
344 StrictMock
<MockBluetoothLowEnergyConnectionFinder
> connection_finder(
345 device_whitelist_
.get());
346 FindAndExpectStartDiscovery(connection_finder
);
347 ExpectStopDiscoveryAndRemoveObserver();
348 connection_finder
.ExpectCreateConnection();
350 // Prepare to add |device_|.
351 device::BluetoothDevice::GattConnectionCallback gatt_connection_callback
;
352 PrepareForNewRightDevice(kServiceUUID
, gatt_connection_callback
);
354 // Prepare to add |other_device|.
355 device::BluetoothDevice::GattConnectionCallback
356 other_gatt_connection_callback
;
357 NiceMock
<device::MockBluetoothDevice
> other_device(
358 adapter_
.get(), 0, kDeviceName
, kOtherBluetoothAddress
, false, false);
359 std::vector
<device::BluetoothUUID
> uuids
;
360 uuids
.push_back(device::BluetoothUUID(kServiceUUID
));
361 ON_CALL(other_device
, IsPaired()).WillByDefault(Return(true));
362 ON_CALL(other_device
, GetUUIDs()).WillByDefault((Return(uuids
)));
363 EXPECT_CALL(other_device
, CreateGattConnection(_
, _
))
364 .WillOnce(SaveArg
<0>(&other_gatt_connection_callback
));
367 connection_finder
.DeviceAdded(adapter_
.get(), device_
.get());
368 connection_finder
.DeviceAdded(adapter_
.get(), &other_device
);
370 ASSERT_FALSE(gatt_connection_callback
.is_null());
371 ASSERT_FALSE(other_gatt_connection_callback
.is_null());
373 base::RunLoop run_loop
;
374 gatt_connection_callback
.Run(make_scoped_ptr(
375 new NiceMock
<device::MockBluetoothGattConnection
>(kBluetoothAddress
)));
376 run_loop
.RunUntilIdle();
378 // The second device should be forgetten.
379 EXPECT_CALL(connection_finder
, CloseGattConnectionProxy());
380 other_gatt_connection_callback
.Run(
381 make_scoped_ptr(new NiceMock
<device::MockBluetoothGattConnection
>(
382 kOtherBluetoothAddress
)));
385 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest
,
386 Find_ConnectionSucceeds
) {
387 StrictMock
<MockBluetoothLowEnergyConnectionFinder
> connection_finder(
388 device_whitelist_
.get());
390 // Starting discovery.
391 FindAndExpectStartDiscovery(connection_finder
);
392 ExpectStopDiscoveryAndRemoveObserver();
394 // Finding and creating a GATT connection to the right device.
395 device::BluetoothDevice::GattConnectionCallback gatt_connection_callback
;
396 PrepareForNewRightDevice(kServiceUUID
, gatt_connection_callback
);
397 connection_finder
.DeviceAdded(adapter_
.get(), device_
.get());
399 // Creating a connection.
400 MockConnection
* connection
= connection_finder
.ExpectCreateConnection();
401 ASSERT_FALSE(gatt_connection_callback
.is_null());
402 base::RunLoop run_loop
;
403 gatt_connection_callback
.Run(make_scoped_ptr(
404 new NiceMock
<device::MockBluetoothGattConnection
>(kBluetoothAddress
)));
405 run_loop
.RunUntilIdle();
406 EXPECT_FALSE(last_found_connection_
);
407 connection
->SetStatus(Connection::IN_PROGRESS
);
408 connection
->SetStatus(Connection::CONNECTED
);
409 EXPECT_TRUE(last_found_connection_
);
412 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest
,
413 Find_ConnectionFails_RestartDiscoveryAndConnectionSucceeds
) {
414 StrictMock
<MockBluetoothLowEnergyConnectionFinder
> connection_finder(
415 device_whitelist_
.get());
417 // Starting discovery.
418 FindAndExpectStartDiscovery(connection_finder
);
419 base::Closure stop_discovery_session_callback
;
420 EXPECT_CALL(*last_discovery_session_alias_
, Stop(_
, _
))
421 .WillOnce(SaveArg
<0>(&stop_discovery_session_callback
));
423 // Preparing to create a GATT connection to the right device.
424 device::BluetoothDevice::GattConnectionCallback gatt_connection_callback
;
425 PrepareForNewRightDevice(kServiceUUID
, gatt_connection_callback
);
426 MockConnection
* connection
= connection_finder
.ExpectCreateConnection();
428 // Trying to create a connection.
429 connection_finder
.DeviceAdded(adapter_
.get(), device_
.get());
430 ASSERT_FALSE(gatt_connection_callback
.is_null());
431 base::RunLoop run_loop
;
432 gatt_connection_callback
.Run(make_scoped_ptr(
433 new NiceMock
<device::MockBluetoothGattConnection
>(kBluetoothAddress
)));
434 run_loop
.RunUntilIdle();
435 ASSERT_FALSE(last_found_connection_
);
436 connection
->SetStatus(Connection::IN_PROGRESS
);
438 // Stopping the discovery session.
439 ASSERT_FALSE(stop_discovery_session_callback
.is_null());
440 stop_discovery_session_callback
.Run();
442 // Preparing to restart the discovery session.
443 device::BluetoothAdapter::DiscoverySessionCallback discovery_callback
;
444 std::vector
<const device::BluetoothDevice
*> devices
;
445 ON_CALL(*adapter_
, GetDevices()).WillByDefault(Return(devices
));
446 EXPECT_CALL(*adapter_
, StartDiscoverySessionWithFilterRaw(_
, _
, _
))
447 .WillOnce(SaveArg
<1>(&discovery_callback
));
450 connection
->SetStatus(Connection::DISCONNECTED
);
452 // Restarting the discovery session.
453 scoped_ptr
<device::MockBluetoothDiscoverySession
> discovery_session(
454 new NiceMock
<device::MockBluetoothDiscoverySession
>());
455 last_discovery_session_alias_
= discovery_session
.get();
456 ON_CALL(*last_discovery_session_alias_
, IsActive())
457 .WillByDefault(Return(true));
458 ASSERT_FALSE(discovery_callback
.is_null());
459 discovery_callback
.Run(discovery_session
.Pass());
461 // Preparing to create a GATT connection to the right device.
462 PrepareForNewRightDevice(kServiceUUID
, gatt_connection_callback
);
463 connection
= connection_finder
.ExpectCreateConnection();
465 // Trying to create a connection.
466 connection_finder
.DeviceAdded(adapter_
.get(), device_
.get());
467 EXPECT_CALL(*last_discovery_session_alias_
, Stop(_
, _
)).Times(AtLeast(1));
468 ASSERT_FALSE(gatt_connection_callback
.is_null());
469 base::RunLoop other_run_loop
;
470 gatt_connection_callback
.Run(make_scoped_ptr(
471 new NiceMock
<device::MockBluetoothGattConnection
>(kBluetoothAddress
)));
472 other_run_loop
.RunUntilIdle();
474 // Completing the connection.
475 EXPECT_FALSE(last_found_connection_
);
476 connection
->SetStatus(Connection::IN_PROGRESS
);
477 connection
->SetStatus(Connection::CONNECTED
);
478 EXPECT_TRUE(last_found_connection_
);
481 TEST_F(ProximityAuthBluetoothLowEnergyConnectionFinderTest
,
482 Find_AdapterRemoved_RestartDiscoveryAndConnectionSucceeds
) {
483 StrictMock
<MockBluetoothLowEnergyConnectionFinder
> connection_finder(
484 device_whitelist_
.get());
486 // Starting discovery.
487 FindAndExpectStartDiscovery(connection_finder
);
489 // Removing the adapter.
490 ON_CALL(*adapter_
, IsPresent()).WillByDefault(Return(false));
491 ON_CALL(*adapter_
, IsPowered()).WillByDefault(Return(false));
492 ON_CALL(*last_discovery_session_alias_
, IsActive())
493 .WillByDefault(Return(false));
494 connection_finder
.AdapterPoweredChanged(adapter_
.get(), false);
495 connection_finder
.AdapterPresentChanged(adapter_
.get(), false);
497 // Adding the adapter.
498 ON_CALL(*adapter_
, IsPresent()).WillByDefault(Return(true));
499 ON_CALL(*adapter_
, IsPowered()).WillByDefault(Return(true));
501 device::BluetoothAdapter::DiscoverySessionCallback discovery_callback
;
502 scoped_ptr
<device::MockBluetoothDiscoverySession
> discovery_session(
503 new NiceMock
<device::MockBluetoothDiscoverySession
>());
504 last_discovery_session_alias_
= discovery_session
.get();
506 // Restarting the discovery session.
507 EXPECT_CALL(*adapter_
, StartDiscoverySessionWithFilterRaw(_
, _
, _
))
508 .WillOnce(SaveArg
<1>(&discovery_callback
));
509 connection_finder
.AdapterPresentChanged(adapter_
.get(), true);
510 connection_finder
.AdapterPoweredChanged(adapter_
.get(), true);
511 ON_CALL(*last_discovery_session_alias_
, IsActive())
512 .WillByDefault(Return(true));
514 ASSERT_FALSE(discovery_callback
.is_null());
515 discovery_callback
.Run(discovery_session
.Pass());
517 // Preparing to create a GATT connection to the right device.
518 device::BluetoothDevice::GattConnectionCallback gatt_connection_callback
;
519 PrepareForNewRightDevice(kServiceUUID
, gatt_connection_callback
);
520 MockConnection
* connection
= connection_finder
.ExpectCreateConnection();
522 // Trying to create a connection.
523 connection_finder
.DeviceAdded(adapter_
.get(), device_
.get());
524 EXPECT_CALL(*last_discovery_session_alias_
, Stop(_
, _
)).Times(AtLeast(1));
525 ASSERT_FALSE(gatt_connection_callback
.is_null());
526 base::RunLoop run_loop
;
527 gatt_connection_callback
.Run(make_scoped_ptr(
528 new NiceMock
<device::MockBluetoothGattConnection
>(kBluetoothAddress
)));
529 run_loop
.RunUntilIdle();
531 // Completing the connection.
532 ASSERT_FALSE(last_found_connection_
);
533 connection
->SetStatus(Connection::IN_PROGRESS
);
534 connection
->SetStatus(Connection::CONNECTED
);
535 EXPECT_TRUE(last_found_connection_
);
538 } // namespace proximity_auth