OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "components/proximity_auth/bluetooth_connection_finder.h" | 5 #include "components/proximity_auth/bluetooth_connection_finder.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
13 #include "base/memory/ref_counted.h" | 13 #include "base/memory/ref_counted.h" |
14 #include "base/message_loop/message_loop.h" | 14 #include "base/message_loop/message_loop.h" |
15 #include "base/run_loop.h" | 15 #include "base/run_loop.h" |
16 #include "base/time/time.h" | 16 #include "base/time/time.h" |
| 17 #include "components/cryptauth/cryptauth_test_util.h" |
17 #include "components/cryptauth/remote_device.h" | 18 #include "components/cryptauth/remote_device.h" |
18 #include "components/proximity_auth/proximity_auth_test_util.h" | 19 #include "components/cryptauth/wire_message.h" |
19 #include "components/proximity_auth/wire_message.h" | |
20 #include "device/bluetooth/bluetooth_adapter_factory.h" | 20 #include "device/bluetooth/bluetooth_adapter_factory.h" |
21 #include "device/bluetooth/bluetooth_uuid.h" | 21 #include "device/bluetooth/bluetooth_uuid.h" |
22 #include "device/bluetooth/test/mock_bluetooth_adapter.h" | 22 #include "device/bluetooth/test/mock_bluetooth_adapter.h" |
23 #include "device/bluetooth/test/mock_bluetooth_device.h" | 23 #include "device/bluetooth/test/mock_bluetooth_device.h" |
24 #include "testing/gmock/include/gmock/gmock.h" | 24 #include "testing/gmock/include/gmock/gmock.h" |
25 #include "testing/gtest/include/gtest/gtest.h" | 25 #include "testing/gtest/include/gtest/gtest.h" |
26 | 26 |
27 using testing::_; | 27 using testing::_; |
28 using testing::NiceMock; | 28 using testing::NiceMock; |
29 using testing::Return; | 29 using testing::Return; |
30 using testing::StrictMock; | 30 using testing::StrictMock; |
31 | 31 |
32 namespace proximity_auth { | 32 namespace proximity_auth { |
33 namespace { | 33 namespace { |
34 | 34 |
35 const char kUuid[] = "DEADBEEF-CAFE-FEED-FOOD-D15EA5EBEEF"; | 35 const char kUuid[] = "DEADBEEF-CAFE-FEED-FOOD-D15EA5EBEEF"; |
36 | 36 |
37 class MockConnection : public Connection { | 37 class MockConnection : public cryptauth::Connection { |
38 public: | 38 public: |
39 MockConnection() | 39 MockConnection() |
40 : Connection(CreateClassicRemoteDeviceForTest()), | 40 : Connection(cryptauth::CreateClassicRemoteDeviceForTest()), |
41 do_not_destroy_(false) {} | 41 do_not_destroy_(false) {} |
42 ~MockConnection() override { EXPECT_FALSE(do_not_destroy_); } | 42 ~MockConnection() override { EXPECT_FALSE(do_not_destroy_); } |
43 | 43 |
44 MOCK_METHOD0(Connect, void()); | 44 MOCK_METHOD0(Connect, void()); |
45 | 45 |
46 void SetStatus(Connection::Status status) { | 46 void SetStatus(cryptauth::Connection::Status status) { |
47 // This object should not be destroyed after setting the status and calling | 47 // This object should not be destroyed after setting the status and calling |
48 // observers. | 48 // observers. |
49 do_not_destroy_ = true; | 49 do_not_destroy_ = true; |
50 Connection::SetStatus(status); | 50 cryptauth::Connection::SetStatus(status); |
51 do_not_destroy_ = false; | 51 do_not_destroy_ = false; |
52 } | 52 } |
53 | 53 |
54 private: | 54 private: |
55 void Disconnect() override {} | 55 void Disconnect() override {} |
56 void SendMessageImpl(std::unique_ptr<WireMessage> message) override {} | 56 void SendMessageImpl( |
| 57 std::unique_ptr<cryptauth::WireMessage> message) override {} |
57 | 58 |
58 // If true, we do not expect |this| object to be destroyed until this value is | 59 // If true, we do not expect |this| object to be destroyed until this value is |
59 // toggled back to false. | 60 // toggled back to false. |
60 bool do_not_destroy_; | 61 bool do_not_destroy_; |
61 | 62 |
62 DISALLOW_COPY_AND_ASSIGN(MockConnection); | 63 DISALLOW_COPY_AND_ASSIGN(MockConnection); |
63 }; | 64 }; |
64 | 65 |
65 class MockBluetoothConnectionFinder : public BluetoothConnectionFinder { | 66 class MockBluetoothConnectionFinder : public BluetoothConnectionFinder { |
66 public: | 67 public: |
67 MockBluetoothConnectionFinder() | 68 MockBluetoothConnectionFinder() |
68 : BluetoothConnectionFinder(CreateClassicRemoteDeviceForTest(), | 69 : BluetoothConnectionFinder(cryptauth::CreateClassicRemoteDeviceForTest(), |
69 device::BluetoothUUID(kUuid), | 70 device::BluetoothUUID(kUuid), |
70 base::TimeDelta()) {} | 71 base::TimeDelta()) {} |
71 ~MockBluetoothConnectionFinder() override {} | 72 ~MockBluetoothConnectionFinder() override {} |
72 | 73 |
73 MOCK_METHOD0(CreateConnectionProxy, Connection*()); | 74 MOCK_METHOD0(CreateConnectionProxy, cryptauth::Connection*()); |
74 | 75 |
75 // Creates a mock connection and sets an expectation that the mock connection | 76 // Creates a mock connection and sets an expectation that the mock connection |
76 // finder's CreateConnection() method will be called and will return the | 77 // finder's CreateConnection() method will be called and will return the |
77 // created connection. Returns a reference to the created connection. | 78 // created connection. Returns a reference to the created connection. |
78 // NOTE: The returned connection's lifetime is managed by the connection | 79 // NOTE: The returned connection's lifetime is managed by the connection |
79 // finder. | 80 // finder. |
80 MockConnection* ExpectCreateConnection() { | 81 MockConnection* ExpectCreateConnection() { |
81 std::unique_ptr<MockConnection> connection(new NiceMock<MockConnection>()); | 82 std::unique_ptr<MockConnection> connection(new NiceMock<MockConnection>()); |
82 MockConnection* connection_alias = connection.get(); | 83 MockConnection* connection_alias = connection.get(); |
83 EXPECT_CALL(*this, CreateConnectionProxy()) | 84 EXPECT_CALL(*this, CreateConnectionProxy()) |
84 .WillOnce(Return(connection.release())); | 85 .WillOnce(Return(connection.release())); |
85 return connection_alias; | 86 return connection_alias; |
86 } | 87 } |
87 | 88 |
88 using BluetoothConnectionFinder::AdapterPresentChanged; | 89 using BluetoothConnectionFinder::AdapterPresentChanged; |
89 using BluetoothConnectionFinder::AdapterPoweredChanged; | 90 using BluetoothConnectionFinder::AdapterPoweredChanged; |
90 | 91 |
91 void ClearSeekCallbacks() { | 92 void ClearSeekCallbacks() { |
92 seek_callback_ = base::Closure(); | 93 seek_callback_ = base::Closure(); |
93 seek_error_callback_ = bluetooth_util::ErrorCallback(); | 94 seek_error_callback_ = bluetooth_util::ErrorCallback(); |
94 } | 95 } |
95 | 96 |
96 const base::Closure& seek_callback() { return seek_callback_; } | 97 const base::Closure& seek_callback() { return seek_callback_; } |
97 const bluetooth_util::ErrorCallback& seek_error_callback() { | 98 const bluetooth_util::ErrorCallback& seek_error_callback() { |
98 return seek_error_callback_; | 99 return seek_error_callback_; |
99 } | 100 } |
100 | 101 |
101 protected: | 102 protected: |
102 // BluetoothConnectionFinder: | 103 // BluetoothConnectionFinder: |
103 std::unique_ptr<Connection> CreateConnection() override { | 104 std::unique_ptr<cryptauth::Connection> CreateConnection() override { |
104 return base::WrapUnique(CreateConnectionProxy()); | 105 return base::WrapUnique(CreateConnectionProxy()); |
105 } | 106 } |
106 | 107 |
107 void SeekDeviceByAddress( | 108 void SeekDeviceByAddress( |
108 const std::string& bluetooth_address, | 109 const std::string& bluetooth_address, |
109 const base::Closure& callback, | 110 const base::Closure& callback, |
110 const bluetooth_util::ErrorCallback& error_callback) override { | 111 const bluetooth_util::ErrorCallback& error_callback) override { |
111 EXPECT_EQ(kTestRemoteDeviceBluetoothAddress, bluetooth_address); | 112 EXPECT_EQ(cryptauth::kTestRemoteDeviceBluetoothAddress, bluetooth_address); |
112 seek_callback_ = callback; | 113 seek_callback_ = callback; |
113 seek_error_callback_ = error_callback; | 114 seek_error_callback_ = error_callback; |
114 } | 115 } |
115 | 116 |
116 private: | 117 private: |
117 base::Closure seek_callback_; | 118 base::Closure seek_callback_; |
118 bluetooth_util::ErrorCallback seek_error_callback_; | 119 bluetooth_util::ErrorCallback seek_error_callback_; |
119 | 120 |
120 DISALLOW_COPY_AND_ASSIGN(MockBluetoothConnectionFinder); | 121 DISALLOW_COPY_AND_ASSIGN(MockBluetoothConnectionFinder); |
121 }; | 122 }; |
122 | 123 |
123 } // namespace | 124 } // namespace |
124 | 125 |
125 class ProximityAuthBluetoothConnectionFinderTest : public testing::Test { | 126 class ProximityAuthBluetoothConnectionFinderTest : public testing::Test { |
126 protected: | 127 protected: |
127 ProximityAuthBluetoothConnectionFinderTest() | 128 ProximityAuthBluetoothConnectionFinderTest() |
128 : adapter_(new NiceMock<device::MockBluetoothAdapter>), | 129 : adapter_(new NiceMock<device::MockBluetoothAdapter>), |
129 bluetooth_device_(new NiceMock<device::MockBluetoothDevice>( | 130 bluetooth_device_(new NiceMock<device::MockBluetoothDevice>( |
130 adapter_.get(), | 131 adapter_.get(), |
131 static_cast<uint32_t>(device::BluetoothDeviceType::PHONE), | 132 static_cast<uint32_t>(device::BluetoothDeviceType::PHONE), |
132 kTestRemoteDeviceName, | 133 cryptauth::kTestRemoteDeviceName, |
133 kTestRemoteDeviceBluetoothAddress, | 134 cryptauth::kTestRemoteDeviceBluetoothAddress, |
134 true, | 135 true, |
135 false)), | 136 false)), |
136 connection_callback_(base::Bind( | 137 connection_callback_(base::Bind( |
137 &ProximityAuthBluetoothConnectionFinderTest::OnConnectionFound, | 138 &ProximityAuthBluetoothConnectionFinderTest::OnConnectionFound, |
138 base::Unretained(this))) { | 139 base::Unretained(this))) { |
139 device::BluetoothAdapterFactory::SetAdapterForTesting(adapter_); | 140 device::BluetoothAdapterFactory::SetAdapterForTesting(adapter_); |
140 | 141 |
141 // By default, configure the environment to allow polling. Individual tests | 142 // By default, configure the environment to allow polling. Individual tests |
142 // can override this as needed. | 143 // can override this as needed. |
143 ON_CALL(*adapter_, IsPresent()).WillByDefault(Return(true)); | 144 ON_CALL(*adapter_, IsPresent()).WillByDefault(Return(true)); |
144 ON_CALL(*adapter_, IsPowered()).WillByDefault(Return(true)); | 145 ON_CALL(*adapter_, IsPowered()).WillByDefault(Return(true)); |
145 | 146 |
146 // By default, the remote device is known to |adapter_| so | 147 // By default, the remote device is known to |adapter_| so |
147 // |SeekDeviceByAddress()| will not be called. | 148 // |SeekDeviceByAddress()| will not be called. |
148 ON_CALL(*adapter_, GetDevice(kTestRemoteDeviceBluetoothAddress)) | 149 ON_CALL(*adapter_, GetDevice(cryptauth::kTestRemoteDeviceBluetoothAddress)) |
149 .WillByDefault(Return(bluetooth_device_.get())); | 150 .WillByDefault(Return(bluetooth_device_.get())); |
150 } | 151 } |
151 | 152 |
152 MOCK_METHOD1(OnConnectionFoundProxy, void(Connection* connection)); | 153 MOCK_METHOD1(OnConnectionFoundProxy, void(cryptauth::Connection* connection)); |
153 void OnConnectionFound(std::unique_ptr<Connection> connection) { | 154 void OnConnectionFound(std::unique_ptr<cryptauth::Connection> connection) { |
154 OnConnectionFoundProxy(connection.get()); | 155 OnConnectionFoundProxy(connection.get()); |
155 last_found_connection_ = std::move(connection); | 156 last_found_connection_ = std::move(connection); |
156 } | 157 } |
157 | 158 |
158 // Starts |connection_finder_|. If |expect_connection| is true, then we set an | 159 // Starts |connection_finder_|. If |expect_connection| is true, then we set an |
159 // expectation that an in-progress connection will be created and returned. | 160 // expectation that an in-progress connection will be created and returned. |
160 MockConnection* StartConnectionFinder(bool expect_connection) { | 161 MockConnection* StartConnectionFinder(bool expect_connection) { |
161 MockConnection* connection = nullptr; | 162 MockConnection* connection = nullptr; |
162 if (expect_connection) | 163 if (expect_connection) |
163 connection = connection_finder_.ExpectCreateConnection(); | 164 connection = connection_finder_.ExpectCreateConnection(); |
164 connection_finder_.Find(connection_callback_); | 165 connection_finder_.Find(connection_callback_); |
165 return connection; | 166 return connection; |
166 } | 167 } |
167 | 168 |
168 // Given an in-progress |connection| returned by |StartConnectionFinder()|, | 169 // Given an in-progress |connection| returned by |StartConnectionFinder()|, |
169 // simulate it transitioning to the CONNECTED state. | 170 // simulate it transitioning to the CONNECTED state. |
170 void SimulateDeviceConnection(MockConnection* connection) { | 171 void SimulateDeviceConnection(MockConnection* connection) { |
171 connection->SetStatus(Connection::IN_PROGRESS); | 172 connection->SetStatus(cryptauth::Connection::IN_PROGRESS); |
172 base::RunLoop run_loop; | 173 base::RunLoop run_loop; |
173 EXPECT_CALL(*this, OnConnectionFoundProxy(_)); | 174 EXPECT_CALL(*this, OnConnectionFoundProxy(_)); |
174 connection->SetStatus(Connection::CONNECTED); | 175 connection->SetStatus(cryptauth::Connection::CONNECTED); |
175 run_loop.RunUntilIdle(); | 176 run_loop.RunUntilIdle(); |
176 } | 177 } |
177 | 178 |
178 scoped_refptr<device::MockBluetoothAdapter> adapter_; | 179 scoped_refptr<device::MockBluetoothAdapter> adapter_; |
179 StrictMock<MockBluetoothConnectionFinder> connection_finder_; | 180 StrictMock<MockBluetoothConnectionFinder> connection_finder_; |
180 std::unique_ptr<device::MockBluetoothDevice> bluetooth_device_; | 181 std::unique_ptr<device::MockBluetoothDevice> bluetooth_device_; |
181 ConnectionFinder::ConnectionCallback connection_callback_; | 182 cryptauth::ConnectionFinder::ConnectionCallback connection_callback_; |
182 | 183 |
183 private: | 184 private: |
184 // Save a pointer to the last found connection, to extend its lifetime. | 185 // Save a pointer to the last found connection, to extend its lifetime. |
185 std::unique_ptr<Connection> last_found_connection_; | 186 std::unique_ptr<cryptauth::Connection> last_found_connection_; |
186 | 187 |
187 base::MessageLoop message_loop_; | 188 base::MessageLoop message_loop_; |
188 }; | 189 }; |
189 | 190 |
190 TEST_F(ProximityAuthBluetoothConnectionFinderTest, | 191 TEST_F(ProximityAuthBluetoothConnectionFinderTest, |
191 ConstructAndDestroyDoesntCrash) { | 192 ConstructAndDestroyDoesntCrash) { |
192 // Destroying a BluetoothConnectionFinder for which Find() has not been called | 193 // Destroying a BluetoothConnectionFinder for which Find() has not been called |
193 // should not crash. | 194 // should not crash. |
194 BluetoothConnectionFinder connection_finder( | 195 BluetoothConnectionFinder connection_finder( |
195 CreateClassicRemoteDeviceForTest(), device::BluetoothUUID(kUuid), | 196 cryptauth::CreateClassicRemoteDeviceForTest(), |
196 base::TimeDelta::FromMilliseconds(1)); | 197 device::BluetoothUUID(kUuid), base::TimeDelta::FromMilliseconds(1)); |
197 } | 198 } |
198 | 199 |
199 TEST_F(ProximityAuthBluetoothConnectionFinderTest, Find_NoBluetoothAdapter) { | 200 TEST_F(ProximityAuthBluetoothConnectionFinderTest, Find_NoBluetoothAdapter) { |
200 // Some platforms do not support Bluetooth. This test is only meaningful on | 201 // Some platforms do not support Bluetooth. This test is only meaningful on |
201 // those platforms. | 202 // those platforms. |
202 adapter_ = NULL; | 203 adapter_ = NULL; |
203 if (device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) | 204 if (device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) |
204 return; | 205 return; |
205 | 206 |
206 // The StrictMock will verify that no connection is created. | 207 // The StrictMock will verify that no connection is created. |
(...skipping 21 matching lines...) Expand all Loading... |
228 | 229 |
229 TEST_F(ProximityAuthBluetoothConnectionFinderTest, | 230 TEST_F(ProximityAuthBluetoothConnectionFinderTest, |
230 Find_ConnectionSucceeds_UnregistersAsObserver) { | 231 Find_ConnectionSucceeds_UnregistersAsObserver) { |
231 MockConnection* connection = StartConnectionFinder(true); | 232 MockConnection* connection = StartConnectionFinder(true); |
232 SimulateDeviceConnection(connection); | 233 SimulateDeviceConnection(connection); |
233 | 234 |
234 // If for some reason the connection sends more status updates, they should | 235 // If for some reason the connection sends more status updates, they should |
235 // be ignored. | 236 // be ignored. |
236 base::RunLoop run_loop; | 237 base::RunLoop run_loop; |
237 EXPECT_CALL(*this, OnConnectionFoundProxy(_)).Times(0); | 238 EXPECT_CALL(*this, OnConnectionFoundProxy(_)).Times(0); |
238 connection->SetStatus(Connection::IN_PROGRESS); | 239 connection->SetStatus(cryptauth::Connection::IN_PROGRESS); |
239 connection->SetStatus(Connection::CONNECTED); | 240 connection->SetStatus(cryptauth::Connection::CONNECTED); |
240 run_loop.RunUntilIdle(); | 241 run_loop.RunUntilIdle(); |
241 } | 242 } |
242 | 243 |
243 TEST_F(ProximityAuthBluetoothConnectionFinderTest, | 244 TEST_F(ProximityAuthBluetoothConnectionFinderTest, |
244 Find_ConnectionFails_PostsTaskToPollAgain) { | 245 Find_ConnectionFails_PostsTaskToPollAgain) { |
245 MockConnection* connection = StartConnectionFinder(true); | 246 MockConnection* connection = StartConnectionFinder(true); |
246 | 247 |
247 // Simulate a connection that fails to connect. | 248 // Simulate a connection that fails to connect. |
248 connection->SetStatus(Connection::IN_PROGRESS); | 249 connection->SetStatus(cryptauth::Connection::IN_PROGRESS); |
249 connection->SetStatus(Connection::DISCONNECTED); | 250 connection->SetStatus(cryptauth::Connection::DISCONNECTED); |
250 | 251 |
251 // A task should have been posted to poll again. | 252 // A task should have been posted to poll again. |
252 base::RunLoop run_loop; | 253 base::RunLoop run_loop; |
253 connection_finder_.ExpectCreateConnection(); | 254 connection_finder_.ExpectCreateConnection(); |
254 run_loop.RunUntilIdle(); | 255 run_loop.RunUntilIdle(); |
255 } | 256 } |
256 | 257 |
257 TEST_F(ProximityAuthBluetoothConnectionFinderTest, Find_PollsOnAdapterPresent) { | 258 TEST_F(ProximityAuthBluetoothConnectionFinderTest, Find_PollsOnAdapterPresent) { |
258 ON_CALL(*adapter_, IsPresent()).WillByDefault(Return(false)); | 259 ON_CALL(*adapter_, IsPresent()).WillByDefault(Return(false)); |
259 EXPECT_CALL(connection_finder_, CreateConnectionProxy()).Times(0); | 260 EXPECT_CALL(connection_finder_, CreateConnectionProxy()).Times(0); |
(...skipping 11 matching lines...) Expand all Loading... |
271 | 272 |
272 ON_CALL(*adapter_, IsPowered()).WillByDefault(Return(true)); | 273 ON_CALL(*adapter_, IsPowered()).WillByDefault(Return(true)); |
273 connection_finder_.ExpectCreateConnection(); | 274 connection_finder_.ExpectCreateConnection(); |
274 connection_finder_.AdapterPoweredChanged(adapter_.get(), true); | 275 connection_finder_.AdapterPoweredChanged(adapter_.get(), true); |
275 } | 276 } |
276 | 277 |
277 TEST_F(ProximityAuthBluetoothConnectionFinderTest, | 278 TEST_F(ProximityAuthBluetoothConnectionFinderTest, |
278 Find_DoesNotPollIfConnectionPending) { | 279 Find_DoesNotPollIfConnectionPending) { |
279 MockConnection* connection = StartConnectionFinder(true); | 280 MockConnection* connection = StartConnectionFinder(true); |
280 | 281 |
281 connection->SetStatus(Connection::IN_PROGRESS); | 282 connection->SetStatus(cryptauth::Connection::IN_PROGRESS); |
282 | 283 |
283 // At this point, there is a pending connection in progress. Hence, an event | 284 // At this point, there is a pending connection in progress. Hence, an event |
284 // that would normally trigger a new polling iteration should not do so now, | 285 // that would normally trigger a new polling iteration should not do so now, |
285 // because the delay interval between successive polling attempts has not yet | 286 // because the delay interval between successive polling attempts has not yet |
286 // expired. | 287 // expired. |
287 EXPECT_CALL(connection_finder_, CreateConnectionProxy()).Times(0); | 288 EXPECT_CALL(connection_finder_, CreateConnectionProxy()).Times(0); |
288 connection_finder_.AdapterPresentChanged(adapter_.get(), true); | 289 connection_finder_.AdapterPresentChanged(adapter_.get(), true); |
289 } | 290 } |
290 | 291 |
291 TEST_F(ProximityAuthBluetoothConnectionFinderTest, | 292 TEST_F(ProximityAuthBluetoothConnectionFinderTest, |
292 Find_ConnectionFails_PostsTaskToPollAgain_PollWaitsForTask) { | 293 Find_ConnectionFails_PostsTaskToPollAgain_PollWaitsForTask) { |
293 MockConnection* connection = StartConnectionFinder(true); | 294 MockConnection* connection = StartConnectionFinder(true); |
294 | 295 |
295 connection->SetStatus(Connection::IN_PROGRESS); | 296 connection->SetStatus(cryptauth::Connection::IN_PROGRESS); |
296 connection->SetStatus(Connection::DISCONNECTED); | 297 connection->SetStatus(cryptauth::Connection::DISCONNECTED); |
297 | 298 |
298 // At this point, there is a pending poll scheduled. Hence, an event that | 299 // At this point, there is a pending poll scheduled. Hence, an event that |
299 // would normally trigger a new polling iteration should not do so now, | 300 // would normally trigger a new polling iteration should not do so now, |
300 // because the delay interval between successive polling attempts has not yet | 301 // because the delay interval between successive polling attempts has not yet |
301 // expired. | 302 // expired. |
302 EXPECT_CALL(connection_finder_, CreateConnectionProxy()).Times(0); | 303 EXPECT_CALL(connection_finder_, CreateConnectionProxy()).Times(0); |
303 connection_finder_.AdapterPresentChanged(adapter_.get(), true); | 304 connection_finder_.AdapterPresentChanged(adapter_.get(), true); |
304 | 305 |
305 // Now, allow the pending task to run, but fail early, so that no new task is | 306 // Now, allow the pending task to run, but fail early, so that no new task is |
306 // posted. | 307 // posted. |
307 ON_CALL(*adapter_, IsPresent()).WillByDefault(Return(false)); | 308 ON_CALL(*adapter_, IsPresent()).WillByDefault(Return(false)); |
308 base::RunLoop run_loop; | 309 base::RunLoop run_loop; |
309 run_loop.RunUntilIdle(); | 310 run_loop.RunUntilIdle(); |
310 ON_CALL(*adapter_, IsPresent()).WillByDefault(Return(true)); | 311 ON_CALL(*adapter_, IsPresent()).WillByDefault(Return(true)); |
311 | 312 |
312 // Now that there is no pending task, events should once again trigger new | 313 // Now that there is no pending task, events should once again trigger new |
313 // polling iterations. | 314 // polling iterations. |
314 connection_finder_.ExpectCreateConnection(); | 315 connection_finder_.ExpectCreateConnection(); |
315 connection_finder_.AdapterPresentChanged(adapter_.get(), true); | 316 connection_finder_.AdapterPresentChanged(adapter_.get(), true); |
316 } | 317 } |
317 | 318 |
318 TEST_F(ProximityAuthBluetoothConnectionFinderTest, | 319 TEST_F(ProximityAuthBluetoothConnectionFinderTest, |
319 Find_DeviceNotKnown_SeekDeviceSucceeds) { | 320 Find_DeviceNotKnown_SeekDeviceSucceeds) { |
320 // If the BluetoothDevice is not known by the adapter, |connection_finder| | 321 // If the BluetoothDevice is not known by the adapter, |connection_finder| |
321 // will call SeekDeviceByAddress() first to make it known. | 322 // will call SeekDeviceByAddress() first to make it known. |
322 ON_CALL(*adapter_, GetDevice(kTestRemoteDeviceBluetoothAddress)) | 323 ON_CALL(*adapter_, GetDevice(cryptauth::kTestRemoteDeviceBluetoothAddress)) |
323 .WillByDefault(Return(nullptr)); | 324 .WillByDefault(Return(nullptr)); |
324 connection_finder_.Find(connection_callback_); | 325 connection_finder_.Find(connection_callback_); |
325 ASSERT_FALSE(connection_finder_.seek_callback().is_null()); | 326 ASSERT_FALSE(connection_finder_.seek_callback().is_null()); |
326 EXPECT_FALSE(connection_finder_.seek_error_callback().is_null()); | 327 EXPECT_FALSE(connection_finder_.seek_error_callback().is_null()); |
327 | 328 |
328 // After seeking is successful, the normal flow should resume. | 329 // After seeking is successful, the normal flow should resume. |
329 ON_CALL(*adapter_, GetDevice(kTestRemoteDeviceBluetoothAddress)) | 330 ON_CALL(*adapter_, GetDevice(cryptauth::kTestRemoteDeviceBluetoothAddress)) |
330 .WillByDefault(Return(bluetooth_device_.get())); | 331 .WillByDefault(Return(bluetooth_device_.get())); |
331 MockConnection* connection = connection_finder_.ExpectCreateConnection(); | 332 MockConnection* connection = connection_finder_.ExpectCreateConnection(); |
332 connection_finder_.seek_callback().Run(); | 333 connection_finder_.seek_callback().Run(); |
333 SimulateDeviceConnection(connection); | 334 SimulateDeviceConnection(connection); |
334 } | 335 } |
335 | 336 |
336 TEST_F(ProximityAuthBluetoothConnectionFinderTest, | 337 TEST_F(ProximityAuthBluetoothConnectionFinderTest, |
337 Find_DeviceNotKnown_SeekDeviceFailThenSucceeds) { | 338 Find_DeviceNotKnown_SeekDeviceFailThenSucceeds) { |
338 // If the BluetoothDevice is not known by the adapter, |connection_finder| | 339 // If the BluetoothDevice is not known by the adapter, |connection_finder| |
339 // will call SeekDeviceByAddress() first to make it known. | 340 // will call SeekDeviceByAddress() first to make it known. |
340 ON_CALL(*adapter_, GetDevice(kTestRemoteDeviceBluetoothAddress)) | 341 ON_CALL(*adapter_, GetDevice(cryptauth::kTestRemoteDeviceBluetoothAddress)) |
341 .WillByDefault(Return(nullptr)); | 342 .WillByDefault(Return(nullptr)); |
342 connection_finder_.Find(connection_callback_); | 343 connection_finder_.Find(connection_callback_); |
343 EXPECT_FALSE(connection_finder_.seek_callback().is_null()); | 344 EXPECT_FALSE(connection_finder_.seek_callback().is_null()); |
344 ASSERT_FALSE(connection_finder_.seek_error_callback().is_null()); | 345 ASSERT_FALSE(connection_finder_.seek_error_callback().is_null()); |
345 | 346 |
346 // If the seek fails, then |connection_finder| will post a delayed poll to | 347 // If the seek fails, then |connection_finder| will post a delayed poll to |
347 // reattempt the seek. | 348 // reattempt the seek. |
348 connection_finder_.seek_error_callback().Run("Seek failed for test."); | 349 connection_finder_.seek_error_callback().Run("Seek failed for test."); |
349 connection_finder_.ClearSeekCallbacks(); | 350 connection_finder_.ClearSeekCallbacks(); |
350 EXPECT_TRUE(connection_finder_.seek_callback().is_null()); | 351 EXPECT_TRUE(connection_finder_.seek_callback().is_null()); |
351 EXPECT_TRUE(connection_finder_.seek_error_callback().is_null()); | 352 EXPECT_TRUE(connection_finder_.seek_error_callback().is_null()); |
352 | 353 |
353 // Check that seek is reattempted. | 354 // Check that seek is reattempted. |
354 base::RunLoop run_loop; | 355 base::RunLoop run_loop; |
355 run_loop.RunUntilIdle(); | 356 run_loop.RunUntilIdle(); |
356 ASSERT_FALSE(connection_finder_.seek_callback().is_null()); | 357 ASSERT_FALSE(connection_finder_.seek_callback().is_null()); |
357 EXPECT_FALSE(connection_finder_.seek_error_callback().is_null()); | 358 EXPECT_FALSE(connection_finder_.seek_error_callback().is_null()); |
358 | 359 |
359 // Successfully connect to the Bluetooth device. | 360 // Successfully connect to the Bluetooth device. |
360 ON_CALL(*adapter_, GetDevice(kTestRemoteDeviceBluetoothAddress)) | 361 ON_CALL(*adapter_, GetDevice(cryptauth::kTestRemoteDeviceBluetoothAddress)) |
361 .WillByDefault(Return(bluetooth_device_.get())); | 362 .WillByDefault(Return(bluetooth_device_.get())); |
362 MockConnection* connection = connection_finder_.ExpectCreateConnection(); | 363 MockConnection* connection = connection_finder_.ExpectCreateConnection(); |
363 connection_finder_.seek_callback().Run(); | 364 connection_finder_.seek_callback().Run(); |
364 SimulateDeviceConnection(connection); | 365 SimulateDeviceConnection(connection); |
365 } | 366 } |
366 | 367 |
367 } // namespace proximity_auth | 368 } // namespace proximity_auth |
OLD | NEW |