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 "base/bind.h" | 7 #include "base/bind.h" |
8 #include "base/memory/ref_counted.h" | 8 #include "base/memory/ref_counted.h" |
9 #include "base/memory/scoped_ptr.h" | 9 #include "base/memory/scoped_ptr.h" |
10 #include "base/message_loop/message_loop.h" | 10 #include "base/message_loop/message_loop.h" |
11 #include "base/run_loop.h" | 11 #include "base/run_loop.h" |
12 #include "base/time/time.h" | 12 #include "base/time/time.h" |
13 #include "components/proximity_auth/remote_device.h" | 13 #include "components/proximity_auth/remote_device.h" |
14 #include "components/proximity_auth/wire_message.h" | 14 #include "components/proximity_auth/wire_message.h" |
15 #include "device/bluetooth/bluetooth_adapter_factory.h" | 15 #include "device/bluetooth/bluetooth_adapter_factory.h" |
16 #include "device/bluetooth/bluetooth_uuid.h" | 16 #include "device/bluetooth/bluetooth_uuid.h" |
17 #include "device/bluetooth/test/mock_bluetooth_adapter.h" | 17 #include "device/bluetooth/test/mock_bluetooth_adapter.h" |
| 18 #include "device/bluetooth/test/mock_bluetooth_device.h" |
18 #include "testing/gmock/include/gmock/gmock.h" | 19 #include "testing/gmock/include/gmock/gmock.h" |
19 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
20 | 21 |
21 using testing::_; | 22 using testing::_; |
22 using testing::NiceMock; | 23 using testing::NiceMock; |
23 using testing::Return; | 24 using testing::Return; |
24 using testing::StrictMock; | 25 using testing::StrictMock; |
25 | 26 |
26 namespace proximity_auth { | 27 namespace proximity_auth { |
27 namespace { | 28 namespace { |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
73 scoped_ptr<MockConnection> connection(new NiceMock<MockConnection>()); | 74 scoped_ptr<MockConnection> connection(new NiceMock<MockConnection>()); |
74 MockConnection* connection_alias = connection.get(); | 75 MockConnection* connection_alias = connection.get(); |
75 EXPECT_CALL(*this, CreateConnectionProxy()) | 76 EXPECT_CALL(*this, CreateConnectionProxy()) |
76 .WillOnce(Return(connection.release())); | 77 .WillOnce(Return(connection.release())); |
77 return connection_alias; | 78 return connection_alias; |
78 } | 79 } |
79 | 80 |
80 using BluetoothConnectionFinder::AdapterPresentChanged; | 81 using BluetoothConnectionFinder::AdapterPresentChanged; |
81 using BluetoothConnectionFinder::AdapterPoweredChanged; | 82 using BluetoothConnectionFinder::AdapterPoweredChanged; |
82 | 83 |
| 84 void ClearSeekCallbacks() { |
| 85 seek_callback_ = base::Closure(); |
| 86 seek_error_callback_ = bluetooth_util::ErrorCallback(); |
| 87 } |
| 88 |
| 89 const base::Closure& seek_callback() { return seek_callback_; } |
| 90 const bluetooth_util::ErrorCallback& seek_error_callback() { |
| 91 return seek_error_callback_; |
| 92 } |
| 93 |
83 protected: | 94 protected: |
| 95 // BluetoothConnectionFinder: |
84 scoped_ptr<Connection> CreateConnection() override { | 96 scoped_ptr<Connection> CreateConnection() override { |
85 return make_scoped_ptr(CreateConnectionProxy()); | 97 return make_scoped_ptr(CreateConnectionProxy()); |
86 } | 98 } |
87 | 99 |
| 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; |
| 107 } |
| 108 |
88 private: | 109 private: |
| 110 base::Closure seek_callback_; |
| 111 bluetooth_util::ErrorCallback seek_error_callback_; |
| 112 |
89 DISALLOW_COPY_AND_ASSIGN(MockBluetoothConnectionFinder); | 113 DISALLOW_COPY_AND_ASSIGN(MockBluetoothConnectionFinder); |
90 }; | 114 }; |
91 | 115 |
92 } // namespace | 116 } // namespace |
93 | 117 |
94 class ProximityAuthBluetoothConnectionFinderTest : public testing::Test { | 118 class ProximityAuthBluetoothConnectionFinderTest : public testing::Test { |
95 protected: | 119 protected: |
96 ProximityAuthBluetoothConnectionFinderTest() | 120 ProximityAuthBluetoothConnectionFinderTest() |
97 : adapter_(new NiceMock<device::MockBluetoothAdapter>), | 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)), |
98 connection_callback_(base::Bind( | 129 connection_callback_(base::Bind( |
99 &ProximityAuthBluetoothConnectionFinderTest::OnConnectionFound, | 130 &ProximityAuthBluetoothConnectionFinderTest::OnConnectionFound, |
100 base::Unretained(this))) { | 131 base::Unretained(this))) { |
101 device::BluetoothAdapterFactory::SetAdapterForTesting(adapter_); | 132 device::BluetoothAdapterFactory::SetAdapterForTesting(adapter_); |
102 | 133 |
103 // By default, configure the environment to allow polling. Individual tests | 134 // By default, configure the environment to allow polling. Individual tests |
104 // can override this as needed. | 135 // can override this as needed. |
105 ON_CALL(*adapter_, IsPresent()).WillByDefault(Return(true)); | 136 ON_CALL(*adapter_, IsPresent()).WillByDefault(Return(true)); |
106 ON_CALL(*adapter_, IsPowered()).WillByDefault(Return(true)); | 137 ON_CALL(*adapter_, IsPowered()).WillByDefault(Return(true)); |
| 138 |
| 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())); |
107 } | 143 } |
108 | 144 |
109 MOCK_METHOD1(OnConnectionFoundProxy, void(Connection* connection)); | 145 MOCK_METHOD1(OnConnectionFoundProxy, void(Connection* connection)); |
110 void OnConnectionFound(scoped_ptr<Connection> connection) { | 146 void OnConnectionFound(scoped_ptr<Connection> connection) { |
111 OnConnectionFoundProxy(connection.get()); | 147 OnConnectionFoundProxy(connection.get()); |
112 last_found_connection_ = connection.Pass(); | 148 last_found_connection_ = connection.Pass(); |
113 } | 149 } |
114 | 150 |
| 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; |
| 159 } |
| 160 |
| 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(); |
| 169 } |
| 170 |
115 scoped_refptr<device::MockBluetoothAdapter> adapter_; | 171 scoped_refptr<device::MockBluetoothAdapter> adapter_; |
| 172 StrictMock<MockBluetoothConnectionFinder> connection_finder_; |
| 173 scoped_ptr<device::MockBluetoothDevice> bluetooth_device_; |
116 ConnectionFinder::ConnectionCallback connection_callback_; | 174 ConnectionFinder::ConnectionCallback connection_callback_; |
117 | 175 |
118 private: | 176 private: |
119 // Save a pointer to the last found connection, to extend its lifetime. | 177 // Save a pointer to the last found connection, to extend its lifetime. |
120 scoped_ptr<Connection> last_found_connection_; | 178 scoped_ptr<Connection> last_found_connection_; |
121 | 179 |
122 base::MessageLoop message_loop_; | 180 base::MessageLoop message_loop_; |
123 }; | 181 }; |
124 | 182 |
125 TEST_F(ProximityAuthBluetoothConnectionFinderTest, | 183 TEST_F(ProximityAuthBluetoothConnectionFinderTest, |
126 ConstructAndDestroyDoesntCrash) { | 184 ConstructAndDestroyDoesntCrash) { |
127 // Destroying a BluetoothConnectionFinder for which Find() has not been called | 185 // Destroying a BluetoothConnectionFinder for which Find() has not been called |
128 // should not crash. | 186 // should not crash. |
129 BluetoothConnectionFinder connection_finder( | 187 BluetoothConnectionFinder connection_finder( |
130 CreateRemoteDevice(), device::BluetoothUUID(kUuid), | 188 CreateRemoteDevice(), device::BluetoothUUID(kUuid), |
131 base::TimeDelta::FromMilliseconds(1)); | 189 base::TimeDelta::FromMilliseconds(1)); |
132 } | 190 } |
133 | 191 |
134 TEST_F(ProximityAuthBluetoothConnectionFinderTest, Find_NoBluetoothAdapter) { | 192 TEST_F(ProximityAuthBluetoothConnectionFinderTest, Find_NoBluetoothAdapter) { |
135 // Some platforms do not support Bluetooth. This test is only meaningful on | 193 // Some platforms do not support Bluetooth. This test is only meaningful on |
136 // those platforms. | 194 // those platforms. |
137 adapter_ = NULL; | 195 adapter_ = NULL; |
138 if (device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) | 196 if (device::BluetoothAdapterFactory::IsBluetoothAdapterAvailable()) |
139 return; | 197 return; |
140 | 198 |
141 // The StrictMock will verify that no connection is created. | 199 // The StrictMock will verify that no connection is created. |
142 StrictMock<MockBluetoothConnectionFinder> connection_finder; | 200 StartConnectionFinder(false); |
143 connection_finder.Find(connection_callback_); | |
144 } | 201 } |
145 | 202 |
146 TEST_F(ProximityAuthBluetoothConnectionFinderTest, | 203 TEST_F(ProximityAuthBluetoothConnectionFinderTest, |
147 Find_BluetoothAdapterNotPresent) { | 204 Find_BluetoothAdapterNotPresent) { |
148 // The StrictMock will verify that no connection is created. | 205 // The StrictMock will verify that no connection is created. |
149 StrictMock<MockBluetoothConnectionFinder> connection_finder; | |
150 ON_CALL(*adapter_, IsPresent()).WillByDefault(Return(false)); | 206 ON_CALL(*adapter_, IsPresent()).WillByDefault(Return(false)); |
151 connection_finder.Find(connection_callback_); | 207 StartConnectionFinder(false); |
152 } | 208 } |
153 | 209 |
154 TEST_F(ProximityAuthBluetoothConnectionFinderTest, | 210 TEST_F(ProximityAuthBluetoothConnectionFinderTest, |
155 Find_BluetoothAdapterNotPowered) { | 211 Find_BluetoothAdapterNotPowered) { |
| 212 ON_CALL(*adapter_, IsPowered()).WillByDefault(Return(false)); |
156 // The StrictMock will verify that no connection is created. | 213 // The StrictMock will verify that no connection is created. |
157 StrictMock<MockBluetoothConnectionFinder> connection_finder; | 214 StartConnectionFinder(false); |
158 ON_CALL(*adapter_, IsPowered()).WillByDefault(Return(false)); | |
159 connection_finder.Find(connection_callback_); | |
160 } | 215 } |
161 | 216 |
162 TEST_F(ProximityAuthBluetoothConnectionFinderTest, Find_ConnectionSucceeds) { | 217 TEST_F(ProximityAuthBluetoothConnectionFinderTest, Find_ConnectionSucceeds) { |
163 StrictMock<MockBluetoothConnectionFinder> connection_finder; | 218 MockConnection* connection = StartConnectionFinder(true); |
164 | 219 SimulateDeviceConnection(connection); |
165 MockConnection* connection = connection_finder.ExpectCreateConnection(); | |
166 connection_finder.Find(connection_callback_); | |
167 | |
168 connection->SetStatus(Connection::IN_PROGRESS); | |
169 | |
170 EXPECT_CALL(*this, OnConnectionFoundProxy(_)); | |
171 connection->SetStatus(Connection::CONNECTED); | |
172 } | 220 } |
173 | 221 |
174 TEST_F(ProximityAuthBluetoothConnectionFinderTest, | 222 TEST_F(ProximityAuthBluetoothConnectionFinderTest, |
175 Find_ConnectionSucceeds_UnregistersAsObserver) { | 223 Find_ConnectionSucceeds_UnregistersAsObserver) { |
176 StrictMock<MockBluetoothConnectionFinder> connection_finder; | 224 MockConnection* connection = StartConnectionFinder(true); |
| 225 SimulateDeviceConnection(connection); |
177 | 226 |
178 MockConnection* connection = connection_finder.ExpectCreateConnection(); | 227 // If for some reason the connection sends more status updates, they should |
179 connection_finder.Find(connection_callback_); | 228 // be ignored. |
180 | 229 base::RunLoop run_loop; |
181 connection->SetStatus(Connection::IN_PROGRESS); | |
182 | |
183 EXPECT_CALL(*this, OnConnectionFoundProxy(_)); | |
184 EXPECT_CALL(*adapter_, RemoveObserver(&connection_finder)); | |
185 connection->SetStatus(Connection::CONNECTED); | |
186 | |
187 // If for some reason the connection sends more status updates, they should be | |
188 // ignored. | |
189 EXPECT_CALL(*this, OnConnectionFoundProxy(_)).Times(0); | 230 EXPECT_CALL(*this, OnConnectionFoundProxy(_)).Times(0); |
190 connection->SetStatus(Connection::IN_PROGRESS); | 231 connection->SetStatus(Connection::IN_PROGRESS); |
191 connection->SetStatus(Connection::CONNECTED); | 232 connection->SetStatus(Connection::CONNECTED); |
| 233 run_loop.RunUntilIdle(); |
192 } | 234 } |
193 | 235 |
194 TEST_F(ProximityAuthBluetoothConnectionFinderTest, | 236 TEST_F(ProximityAuthBluetoothConnectionFinderTest, |
195 Find_ConnectionFails_PostsTaskToPollAgain) { | 237 Find_ConnectionFails_PostsTaskToPollAgain) { |
196 StrictMock<MockBluetoothConnectionFinder> connection_finder; | 238 MockConnection* connection = StartConnectionFinder(true); |
197 | |
198 MockConnection* connection = connection_finder.ExpectCreateConnection(); | |
199 connection_finder.Find(connection_callback_); | |
200 | 239 |
201 // Simulate a connection that fails to connect. | 240 // Simulate a connection that fails to connect. |
202 connection->SetStatus(Connection::IN_PROGRESS); | 241 connection->SetStatus(Connection::IN_PROGRESS); |
203 connection->SetStatus(Connection::DISCONNECTED); | 242 connection->SetStatus(Connection::DISCONNECTED); |
204 | 243 |
205 // A task should have been posted to poll again. | 244 // A task should have been posted to poll again. |
206 base::RunLoop run_loop; | 245 base::RunLoop run_loop; |
207 connection_finder.ExpectCreateConnection(); | 246 connection_finder_.ExpectCreateConnection(); |
208 run_loop.RunUntilIdle(); | 247 run_loop.RunUntilIdle(); |
209 } | 248 } |
210 | 249 |
211 TEST_F(ProximityAuthBluetoothConnectionFinderTest, Find_PollsOnAdapterPresent) { | 250 TEST_F(ProximityAuthBluetoothConnectionFinderTest, Find_PollsOnAdapterPresent) { |
212 StrictMock<MockBluetoothConnectionFinder> connection_finder; | |
213 | |
214 ON_CALL(*adapter_, IsPresent()).WillByDefault(Return(false)); | 251 ON_CALL(*adapter_, IsPresent()).WillByDefault(Return(false)); |
215 EXPECT_CALL(connection_finder, CreateConnectionProxy()).Times(0); | 252 EXPECT_CALL(connection_finder_, CreateConnectionProxy()).Times(0); |
216 connection_finder.Find(connection_callback_); | 253 connection_finder_.Find(connection_callback_); |
217 | 254 |
218 ON_CALL(*adapter_, IsPresent()).WillByDefault(Return(true)); | 255 ON_CALL(*adapter_, IsPresent()).WillByDefault(Return(true)); |
219 connection_finder.ExpectCreateConnection(); | 256 connection_finder_.ExpectCreateConnection(); |
220 connection_finder.AdapterPresentChanged(adapter_.get(), true); | 257 connection_finder_.AdapterPresentChanged(adapter_.get(), true); |
221 } | 258 } |
222 | 259 |
223 TEST_F(ProximityAuthBluetoothConnectionFinderTest, Find_PollsOnAdapterPowered) { | 260 TEST_F(ProximityAuthBluetoothConnectionFinderTest, Find_PollsOnAdapterPowered) { |
224 StrictMock<MockBluetoothConnectionFinder> connection_finder; | |
225 | |
226 ON_CALL(*adapter_, IsPowered()).WillByDefault(Return(false)); | 261 ON_CALL(*adapter_, IsPowered()).WillByDefault(Return(false)); |
227 EXPECT_CALL(connection_finder, CreateConnectionProxy()).Times(0); | 262 EXPECT_CALL(connection_finder_, CreateConnectionProxy()).Times(0); |
228 connection_finder.Find(connection_callback_); | 263 connection_finder_.Find(connection_callback_); |
229 | 264 |
230 ON_CALL(*adapter_, IsPowered()).WillByDefault(Return(true)); | 265 ON_CALL(*adapter_, IsPowered()).WillByDefault(Return(true)); |
231 connection_finder.ExpectCreateConnection(); | 266 connection_finder_.ExpectCreateConnection(); |
232 connection_finder.AdapterPoweredChanged(adapter_.get(), true); | 267 connection_finder_.AdapterPoweredChanged(adapter_.get(), true); |
233 } | 268 } |
234 | 269 |
235 TEST_F(ProximityAuthBluetoothConnectionFinderTest, | 270 TEST_F(ProximityAuthBluetoothConnectionFinderTest, |
236 Find_DoesNotPollIfConnectionPending) { | 271 Find_DoesNotPollIfConnectionPending) { |
237 StrictMock<MockBluetoothConnectionFinder> connection_finder; | 272 MockConnection* connection = StartConnectionFinder(true); |
238 | |
239 MockConnection* connection = connection_finder.ExpectCreateConnection(); | |
240 connection_finder.Find(connection_callback_); | |
241 | 273 |
242 connection->SetStatus(Connection::IN_PROGRESS); | 274 connection->SetStatus(Connection::IN_PROGRESS); |
243 | 275 |
244 // At this point, there is a pending connection in progress. Hence, an event | 276 // At this point, there is a pending connection in progress. Hence, an event |
245 // that would normally trigger a new polling iteration should not do so now, | 277 // that would normally trigger a new polling iteration should not do so now, |
246 // because the delay interval between successive polling attempts has not yet | 278 // because the delay interval between successive polling attempts has not yet |
247 // expired. | 279 // expired. |
248 EXPECT_CALL(connection_finder, CreateConnectionProxy()).Times(0); | 280 EXPECT_CALL(connection_finder_, CreateConnectionProxy()).Times(0); |
249 connection_finder.AdapterPresentChanged(adapter_.get(), true); | 281 connection_finder_.AdapterPresentChanged(adapter_.get(), true); |
250 } | 282 } |
251 | 283 |
252 TEST_F(ProximityAuthBluetoothConnectionFinderTest, | 284 TEST_F(ProximityAuthBluetoothConnectionFinderTest, |
253 Find_ConnectionFails_PostsTaskToPollAgain_PollWaitsForTask) { | 285 Find_ConnectionFails_PostsTaskToPollAgain_PollWaitsForTask) { |
254 StrictMock<MockBluetoothConnectionFinder> connection_finder; | 286 MockConnection* connection = StartConnectionFinder(true); |
255 | |
256 MockConnection* connection = connection_finder.ExpectCreateConnection(); | |
257 connection_finder.Find(connection_callback_); | |
258 | 287 |
259 connection->SetStatus(Connection::IN_PROGRESS); | 288 connection->SetStatus(Connection::IN_PROGRESS); |
260 connection->SetStatus(Connection::DISCONNECTED); | 289 connection->SetStatus(Connection::DISCONNECTED); |
261 | 290 |
262 // At this point, there is a pending poll scheduled. Hence, an event that | 291 // At this point, there is a pending poll scheduled. Hence, an event that |
263 // would normally trigger a new polling iteration should not do so now, | 292 // would normally trigger a new polling iteration should not do so now, |
264 // because the delay interval between successive polling attempts has not yet | 293 // because the delay interval between successive polling attempts has not yet |
265 // expired. | 294 // expired. |
266 EXPECT_CALL(connection_finder, CreateConnectionProxy()).Times(0); | 295 EXPECT_CALL(connection_finder_, CreateConnectionProxy()).Times(0); |
267 connection_finder.AdapterPresentChanged(adapter_.get(), true); | 296 connection_finder_.AdapterPresentChanged(adapter_.get(), true); |
268 | 297 |
269 // Now, allow the pending task to run, but fail early, so that no new task is | 298 // Now, allow the pending task to run, but fail early, so that no new task is |
270 // posted. | 299 // posted. |
271 ON_CALL(*adapter_, IsPresent()).WillByDefault(Return(false)); | 300 ON_CALL(*adapter_, IsPresent()).WillByDefault(Return(false)); |
272 base::RunLoop run_loop; | 301 base::RunLoop run_loop; |
273 run_loop.RunUntilIdle(); | 302 run_loop.RunUntilIdle(); |
274 ON_CALL(*adapter_, IsPresent()).WillByDefault(Return(true)); | 303 ON_CALL(*adapter_, IsPresent()).WillByDefault(Return(true)); |
275 | 304 |
276 // Now that there is no pending task, events should once again trigger new | 305 // Now that there is no pending task, events should once again trigger new |
277 // polling iterations. | 306 // polling iterations. |
278 connection_finder.ExpectCreateConnection(); | 307 connection_finder_.ExpectCreateConnection(); |
279 connection_finder.AdapterPresentChanged(adapter_.get(), true); | 308 connection_finder_.AdapterPresentChanged(adapter_.get(), true); |
| 309 } |
| 310 |
| 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()); |
| 320 |
| 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); |
| 327 } |
| 328 |
| 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()); |
| 338 |
| 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()); |
| 345 |
| 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()); |
| 351 |
| 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); |
280 } | 358 } |
281 | 359 |
282 } // namespace proximity_auth | 360 } // namespace proximity_auth |
OLD | NEW |