Allow intent picker for external schemes
[chromium-blink-merge.git] / components / proximity_auth / bluetooth_connection_finder_unittest.cc
blob83d930ccdd812d4c909511ad643f055fc6ac932a
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 "components/proximity_auth/bluetooth_connection_finder.h"
7 #include "base/bind.h"
8 #include "base/memory/ref_counted.h"
9 #include "base/memory/scoped_ptr.h"
10 #include "base/message_loop/message_loop.h"
11 #include "base/run_loop.h"
12 #include "base/time/time.h"
13 #include "components/proximity_auth/remote_device.h"
14 #include "components/proximity_auth/wire_message.h"
15 #include "device/bluetooth/bluetooth_adapter_factory.h"
16 #include "device/bluetooth/bluetooth_uuid.h"
17 #include "device/bluetooth/test/mock_bluetooth_adapter.h"
18 #include "device/bluetooth/test/mock_bluetooth_device.h"
19 #include "testing/gmock/include/gmock/gmock.h"
20 #include "testing/gtest/include/gtest/gtest.h"
22 using testing::_;
23 using testing::NiceMock;
24 using testing::Return;
25 using testing::StrictMock;
27 namespace proximity_auth {
28 namespace {
30 const char kDeviceName[] = "Device name";
31 const char kPublicKey[] = "Public key";
32 const char kBluetoothAddress[] = "11:22:33:44:55:66";
33 const char kPersistentSymmetricKey[] = "PSK";
35 const char kUuid[] = "DEADBEEF-CAFE-FEED-FOOD-D15EA5EBEEF";
37 RemoteDevice CreateRemoteDevice() {
38 return RemoteDevice(kDeviceName, kPublicKey, kBluetoothAddress,
39 kPersistentSymmetricKey);
42 class MockConnection : public Connection {
43 public:
44 MockConnection() : Connection(CreateRemoteDevice()) {}
45 ~MockConnection() override {}
47 MOCK_METHOD0(Connect, void());
49 using Connection::SetStatus;
51 private:
52 void Disconnect() override {}
53 void SendMessageImpl(scoped_ptr<WireMessage> message) override {}
55 DISALLOW_COPY_AND_ASSIGN(MockConnection);
58 class MockBluetoothConnectionFinder : public BluetoothConnectionFinder {
59 public:
60 MockBluetoothConnectionFinder()
61 : BluetoothConnectionFinder(CreateRemoteDevice(),
62 device::BluetoothUUID(kUuid),
63 base::TimeDelta()) {}
64 ~MockBluetoothConnectionFinder() override {}
66 MOCK_METHOD0(CreateConnectionProxy, Connection*());
68 // Creates a mock connection and sets an expectation that the mock connection
69 // finder's CreateConnection() method will be called and will return the
70 // created connection. Returns a reference to the created connection.
71 // NOTE: The returned connection's lifetime is managed by the connection
72 // finder.
73 MockConnection* ExpectCreateConnection() {
74 scoped_ptr<MockConnection> connection(new NiceMock<MockConnection>());
75 MockConnection* connection_alias = connection.get();
76 EXPECT_CALL(*this, CreateConnectionProxy())
77 .WillOnce(Return(connection.release()));
78 return connection_alias;
81 using BluetoothConnectionFinder::AdapterPresentChanged;
82 using BluetoothConnectionFinder::AdapterPoweredChanged;
84 void ClearSeekCallbacks() {
85 seek_callback_ = base::Closure();
86 seek_error_callback_ = bluetooth_util::ErrorCallback();
89 const base::Closure& seek_callback() { return seek_callback_; }
90 const bluetooth_util::ErrorCallback& seek_error_callback() {
91 return seek_error_callback_;
94 protected:
95 // BluetoothConnectionFinder:
96 scoped_ptr<Connection> CreateConnection() override {
97 return make_scoped_ptr(CreateConnectionProxy());
100 void SeekDeviceByAddress(
101 const std::string& bluetooth_address,
102 const base::Closure& callback,
103 const bluetooth_util::ErrorCallback& error_callback) override {
104 EXPECT_EQ(kBluetoothAddress, bluetooth_address);
105 seek_callback_ = callback;
106 seek_error_callback_ = error_callback;
109 private:
110 base::Closure seek_callback_;
111 bluetooth_util::ErrorCallback seek_error_callback_;
113 DISALLOW_COPY_AND_ASSIGN(MockBluetoothConnectionFinder);
116 } // namespace
118 class ProximityAuthBluetoothConnectionFinderTest : public testing::Test {
119 protected:
120 ProximityAuthBluetoothConnectionFinderTest()
121 : adapter_(new NiceMock<device::MockBluetoothAdapter>),
122 bluetooth_device_(new NiceMock<device::MockBluetoothDevice>(
123 adapter_.get(),
124 device::BluetoothDevice::DEVICE_PHONE,
125 kDeviceName,
126 kBluetoothAddress,
127 true,
128 false)),
129 connection_callback_(base::Bind(
130 &ProximityAuthBluetoothConnectionFinderTest::OnConnectionFound,
131 base::Unretained(this))) {
132 device::BluetoothAdapterFactory::SetAdapterForTesting(adapter_);
134 // By default, configure the environment to allow polling. Individual tests
135 // can override this as needed.
136 ON_CALL(*adapter_, IsPresent()).WillByDefault(Return(true));
137 ON_CALL(*adapter_, IsPowered()).WillByDefault(Return(true));
139 // By default, the remote device is known to |adapter_| so
140 // |SeekDeviceByAddress()| will not be called.
141 ON_CALL(*adapter_, GetDevice(kBluetoothAddress))
142 .WillByDefault(Return(bluetooth_device_.get()));
145 MOCK_METHOD1(OnConnectionFoundProxy, void(Connection* connection));
146 void OnConnectionFound(scoped_ptr<Connection> connection) {
147 OnConnectionFoundProxy(connection.get());
148 last_found_connection_ = connection.Pass();
151 // Starts |connection_finder_|. If |expect_connection| is true, then we set an
152 // expectation that an in-progress connection will be created and returned.
153 MockConnection* StartConnectionFinder(bool expect_connection) {
154 MockConnection* connection = nullptr;
155 if (expect_connection)
156 connection = connection_finder_.ExpectCreateConnection();
157 connection_finder_.Find(connection_callback_);
158 return connection;
161 // Given an in-progress |connection| returned by |StartConnectionFinder()|,
162 // simulate it transitioning to the CONNECTED state.
163 void SimulateDeviceConnection(MockConnection* connection) {
164 connection->SetStatus(Connection::IN_PROGRESS);
165 base::RunLoop run_loop;
166 EXPECT_CALL(*this, OnConnectionFoundProxy(_));
167 connection->SetStatus(Connection::CONNECTED);
168 run_loop.RunUntilIdle();
171 scoped_refptr<device::MockBluetoothAdapter> adapter_;
172 StrictMock<MockBluetoothConnectionFinder> connection_finder_;
173 scoped_ptr<device::MockBluetoothDevice> bluetooth_device_;
174 ConnectionFinder::ConnectionCallback connection_callback_;
176 private:
177 // Save a pointer to the last found connection, to extend its lifetime.
178 scoped_ptr<Connection> last_found_connection_;
180 base::MessageLoop message_loop_;
183 TEST_F(ProximityAuthBluetoothConnectionFinderTest,
184 ConstructAndDestroyDoesntCrash) {
185 // Destroying a BluetoothConnectionFinder for which Find() has not been called
186 // should not crash.
187 BluetoothConnectionFinder connection_finder(
188 CreateRemoteDevice(), device::BluetoothUUID(kUuid),
189 base::TimeDelta::FromMilliseconds(1));
192 TEST_F(ProximityAuthBluetoothConnectionFinderTest, Find_NoBluetoothAdapter) {
193 // Some platforms do not support Bluetooth. This test is only meaningful on
194 // those platforms.
195 adapter_ = NULL;
196 if (device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable())
197 return;
199 // The StrictMock will verify that no connection is created.
200 StartConnectionFinder(false);
203 TEST_F(ProximityAuthBluetoothConnectionFinderTest,
204 Find_BluetoothAdapterNotPresent) {
205 // The StrictMock will verify that no connection is created.
206 ON_CALL(*adapter_, IsPresent()).WillByDefault(Return(false));
207 StartConnectionFinder(false);
210 TEST_F(ProximityAuthBluetoothConnectionFinderTest,
211 Find_BluetoothAdapterNotPowered) {
212 ON_CALL(*adapter_, IsPowered()).WillByDefault(Return(false));
213 // The StrictMock will verify that no connection is created.
214 StartConnectionFinder(false);
217 TEST_F(ProximityAuthBluetoothConnectionFinderTest, Find_ConnectionSucceeds) {
218 MockConnection* connection = StartConnectionFinder(true);
219 SimulateDeviceConnection(connection);
222 TEST_F(ProximityAuthBluetoothConnectionFinderTest,
223 Find_ConnectionSucceeds_UnregistersAsObserver) {
224 MockConnection* connection = StartConnectionFinder(true);
225 SimulateDeviceConnection(connection);
227 // If for some reason the connection sends more status updates, they should
228 // be ignored.
229 base::RunLoop run_loop;
230 EXPECT_CALL(*this, OnConnectionFoundProxy(_)).Times(0);
231 connection->SetStatus(Connection::IN_PROGRESS);
232 connection->SetStatus(Connection::CONNECTED);
233 run_loop.RunUntilIdle();
236 TEST_F(ProximityAuthBluetoothConnectionFinderTest,
237 Find_ConnectionFails_PostsTaskToPollAgain) {
238 MockConnection* connection = StartConnectionFinder(true);
240 // Simulate a connection that fails to connect.
241 connection->SetStatus(Connection::IN_PROGRESS);
242 connection->SetStatus(Connection::DISCONNECTED);
244 // A task should have been posted to poll again.
245 base::RunLoop run_loop;
246 connection_finder_.ExpectCreateConnection();
247 run_loop.RunUntilIdle();
250 TEST_F(ProximityAuthBluetoothConnectionFinderTest, Find_PollsOnAdapterPresent) {
251 ON_CALL(*adapter_, IsPresent()).WillByDefault(Return(false));
252 EXPECT_CALL(connection_finder_, CreateConnectionProxy()).Times(0);
253 connection_finder_.Find(connection_callback_);
255 ON_CALL(*adapter_, IsPresent()).WillByDefault(Return(true));
256 connection_finder_.ExpectCreateConnection();
257 connection_finder_.AdapterPresentChanged(adapter_.get(), true);
260 TEST_F(ProximityAuthBluetoothConnectionFinderTest, Find_PollsOnAdapterPowered) {
261 ON_CALL(*adapter_, IsPowered()).WillByDefault(Return(false));
262 EXPECT_CALL(connection_finder_, CreateConnectionProxy()).Times(0);
263 connection_finder_.Find(connection_callback_);
265 ON_CALL(*adapter_, IsPowered()).WillByDefault(Return(true));
266 connection_finder_.ExpectCreateConnection();
267 connection_finder_.AdapterPoweredChanged(adapter_.get(), true);
270 TEST_F(ProximityAuthBluetoothConnectionFinderTest,
271 Find_DoesNotPollIfConnectionPending) {
272 MockConnection* connection = StartConnectionFinder(true);
274 connection->SetStatus(Connection::IN_PROGRESS);
276 // At this point, there is a pending connection in progress. Hence, an event
277 // that would normally trigger a new polling iteration should not do so now,
278 // because the delay interval between successive polling attempts has not yet
279 // expired.
280 EXPECT_CALL(connection_finder_, CreateConnectionProxy()).Times(0);
281 connection_finder_.AdapterPresentChanged(adapter_.get(), true);
284 TEST_F(ProximityAuthBluetoothConnectionFinderTest,
285 Find_ConnectionFails_PostsTaskToPollAgain_PollWaitsForTask) {
286 MockConnection* connection = StartConnectionFinder(true);
288 connection->SetStatus(Connection::IN_PROGRESS);
289 connection->SetStatus(Connection::DISCONNECTED);
291 // At this point, there is a pending poll scheduled. Hence, an event that
292 // would normally trigger a new polling iteration should not do so now,
293 // because the delay interval between successive polling attempts has not yet
294 // expired.
295 EXPECT_CALL(connection_finder_, CreateConnectionProxy()).Times(0);
296 connection_finder_.AdapterPresentChanged(adapter_.get(), true);
298 // Now, allow the pending task to run, but fail early, so that no new task is
299 // posted.
300 ON_CALL(*adapter_, IsPresent()).WillByDefault(Return(false));
301 base::RunLoop run_loop;
302 run_loop.RunUntilIdle();
303 ON_CALL(*adapter_, IsPresent()).WillByDefault(Return(true));
305 // Now that there is no pending task, events should once again trigger new
306 // polling iterations.
307 connection_finder_.ExpectCreateConnection();
308 connection_finder_.AdapterPresentChanged(adapter_.get(), true);
311 TEST_F(ProximityAuthBluetoothConnectionFinderTest,
312 Find_DeviceNotKnown_SeekDeviceSucceeds) {
313 // If the BluetoothDevice is not known by the adapter, |connection_finder|
314 // will call SeekDeviceByAddress() first to make it known.
315 ON_CALL(*adapter_, GetDevice(kBluetoothAddress))
316 .WillByDefault(Return(nullptr));
317 connection_finder_.Find(connection_callback_);
318 ASSERT_FALSE(connection_finder_.seek_callback().is_null());
319 EXPECT_FALSE(connection_finder_.seek_error_callback().is_null());
321 // After seeking is successful, the normal flow should resume.
322 ON_CALL(*adapter_, GetDevice(kBluetoothAddress))
323 .WillByDefault(Return(bluetooth_device_.get()));
324 MockConnection* connection = connection_finder_.ExpectCreateConnection();
325 connection_finder_.seek_callback().Run();
326 SimulateDeviceConnection(connection);
329 TEST_F(ProximityAuthBluetoothConnectionFinderTest,
330 Find_DeviceNotKnown_SeekDeviceFailThenSucceeds) {
331 // If the BluetoothDevice is not known by the adapter, |connection_finder|
332 // will call SeekDeviceByAddress() first to make it known.
333 ON_CALL(*adapter_, GetDevice(kBluetoothAddress))
334 .WillByDefault(Return(nullptr));
335 connection_finder_.Find(connection_callback_);
336 EXPECT_FALSE(connection_finder_.seek_callback().is_null());
337 ASSERT_FALSE(connection_finder_.seek_error_callback().is_null());
339 // If the seek fails, then |connection_finder| will post a delayed poll to
340 // reattempt the seek.
341 connection_finder_.seek_error_callback().Run("Seek failed for test.");
342 connection_finder_.ClearSeekCallbacks();
343 EXPECT_TRUE(connection_finder_.seek_callback().is_null());
344 EXPECT_TRUE(connection_finder_.seek_error_callback().is_null());
346 // Check that seek is reattempted.
347 base::RunLoop run_loop;
348 run_loop.RunUntilIdle();
349 ASSERT_FALSE(connection_finder_.seek_callback().is_null());
350 EXPECT_FALSE(connection_finder_.seek_error_callback().is_null());
352 // Successfully connect to the Bluetooth device.
353 ON_CALL(*adapter_, GetDevice(kBluetoothAddress))
354 .WillByDefault(Return(bluetooth_device_.get()));
355 MockConnection* connection = connection_finder_.ExpectCreateConnection();
356 connection_finder_.seek_callback().Run();
357 SimulateDeviceConnection(connection);
360 } // namespace proximity_auth