Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(451)

Side by Side Diff: device/geolocation/location_arbitrator_impl_unittest.cc

Issue 2226143002: Gets rid of the LocationArbitrator interface, in preference for LocationProvider. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Addresses kmarshall's #41 comments Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "device/geolocation/location_arbitrator_impl.h" 5 #include "device/geolocation/location_arbitrator_impl.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <utility>
8 9
9 #include "base/bind.h" 10 #include "base/bind.h"
10 #include "base/memory/ptr_util.h" 11 #include "base/memory/ptr_util.h"
11 #include "device/geolocation/fake_access_token_store.h" 12 #include "device/geolocation/fake_access_token_store.h"
12 #include "device/geolocation/geolocation_delegate.h" 13 #include "device/geolocation/geolocation_delegate.h"
13 #include "device/geolocation/geoposition.h" 14 #include "device/geolocation/geoposition.h"
14 #include "device/geolocation/mock_location_provider.h" 15 #include "device/geolocation/mock_location_provider.h"
15 #include "testing/gmock/include/gmock/gmock.h" 16 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h" 17 #include "testing/gtest/include/gtest/gtest.h"
17 18
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
107 108
108 scoped_refptr<AccessTokenStore> NewAccessTokenStore() override { 109 scoped_refptr<AccessTokenStore> NewAccessTokenStore() override {
109 return access_token_store_; 110 return access_token_store_;
110 } 111 }
111 112
112 std::unique_ptr<LocationProvider> NewNetworkLocationProvider( 113 std::unique_ptr<LocationProvider> NewNetworkLocationProvider(
113 const scoped_refptr<AccessTokenStore>& access_token_store, 114 const scoped_refptr<AccessTokenStore>& access_token_store,
114 const scoped_refptr<net::URLRequestContextGetter>& context, 115 const scoped_refptr<net::URLRequestContextGetter>& context,
115 const GURL& url, 116 const GURL& url,
116 const base::string16& access_token) override { 117 const base::string16& access_token) override {
117 cell_ = new MockLocationProvider; 118 cell_ = new MockLocationProvider;
Kevin M 2016/08/15 21:34:54 StrictMock here and elsewhere in this test that mo
CJ 2016/08/15 23:09:52 To be noted, these are not actually mock objects,
Kevin M 2016/08/17 17:44:37 Ooh, right. No, StrictMock doesn't do anything for
CJ 2016/08/17 23:07:10 Acknowledged.
118 return base::WrapUnique(cell_); 119 return base::WrapUnique(cell_);
119 } 120 }
120 121
121 std::unique_ptr<LocationProvider> NewSystemLocationProvider() override { 122 std::unique_ptr<LocationProvider> NewSystemLocationProvider() override {
122 gps_ = new MockLocationProvider; 123 gps_ = new MockLocationProvider;
123 return base::WrapUnique(gps_); 124 return base::WrapUnique(gps_);
124 } 125 }
125 126
126 // Two location providers, with nice short names to make the tests more 127 // Two location providers, with nice short names to make the tests more
127 // readable. Note |gps_| will only be set when there is a high accuracy 128 // readable. Note |gps_| will only be set when there is a high accuracy
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
186 TEST_F(GeolocationLocationArbitratorTest, CreateDestroy) { 187 TEST_F(GeolocationLocationArbitratorTest, CreateDestroy) {
187 EXPECT_TRUE(access_token_store_.get()); 188 EXPECT_TRUE(access_token_store_.get());
188 InitializeArbitrator(nullptr); 189 InitializeArbitrator(nullptr);
189 EXPECT_TRUE(arbitrator_); 190 EXPECT_TRUE(arbitrator_);
190 arbitrator_.reset(); 191 arbitrator_.reset();
191 SUCCEED(); 192 SUCCEED();
192 } 193 }
193 194
194 TEST_F(GeolocationLocationArbitratorTest, OnPermissionGranted) { 195 TEST_F(GeolocationLocationArbitratorTest, OnPermissionGranted) {
195 InitializeArbitrator(nullptr); 196 InitializeArbitrator(nullptr);
196 EXPECT_FALSE(arbitrator_->HasPermissionBeenGranted()); 197 EXPECT_FALSE(arbitrator_->HasPermissionBeenGrantedForTest());
197 arbitrator_->OnPermissionGranted(); 198 arbitrator_->OnPermissionGranted();
198 EXPECT_TRUE(arbitrator_->HasPermissionBeenGranted()); 199 EXPECT_TRUE(arbitrator_->HasPermissionBeenGrantedForTest());
199 // Can't check the provider has been notified without going through the 200 // Can't check the provider has been notified without going through the
200 // motions to create the provider (see next test). 201 // motions to create the provider (see next test).
201 EXPECT_FALSE(cell()); 202 EXPECT_FALSE(cell());
202 EXPECT_FALSE(gps()); 203 EXPECT_FALSE(gps());
203 } 204 }
204 205
205 TEST_F(GeolocationLocationArbitratorTest, NormalUsage) { 206 TEST_F(GeolocationLocationArbitratorTest, NormalUsage) {
206 InitializeArbitrator(nullptr); 207 InitializeArbitrator(nullptr);
207 ASSERT_TRUE(access_token_store_.get()); 208 ASSERT_TRUE(access_token_store_.get());
208 ASSERT_TRUE(arbitrator_); 209 ASSERT_TRUE(arbitrator_);
209 210
210 EXPECT_FALSE(cell()); 211 EXPECT_FALSE(cell());
211 EXPECT_FALSE(gps()); 212 EXPECT_FALSE(gps());
212 arbitrator_->StartProviders(false); 213 arbitrator_->StartProvider(false);
213 214
214 EXPECT_TRUE(access_token_store_->access_token_map_.empty()); 215 EXPECT_TRUE(access_token_store_->access_token_map_.empty());
215 216
216 access_token_store_->NotifyDelegateTokensLoaded(); 217 access_token_store_->NotifyDelegateTokensLoaded();
217 ASSERT_TRUE(cell()); 218 ASSERT_TRUE(cell());
218 EXPECT_TRUE(gps()); 219 EXPECT_TRUE(gps());
219 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, cell()->state_); 220 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, cell()->state_);
220 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, gps()->state_); 221 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, gps()->state_);
221 EXPECT_FALSE(observer_->last_position_.Validate()); 222 EXPECT_FALSE(observer_->last_position_.Validate());
222 EXPECT_EQ(Geoposition::ERROR_CODE_NONE, observer_->last_position_.error_code); 223 EXPECT_EQ(Geoposition::ERROR_CODE_NONE, observer_->last_position_.error_code);
223 224
224 SetReferencePosition(cell()); 225 SetReferencePosition(cell());
225 226
226 EXPECT_TRUE(observer_->last_position_.Validate() || 227 EXPECT_TRUE(observer_->last_position_.Validate() ||
227 observer_->last_position_.error_code != 228 observer_->last_position_.error_code !=
228 Geoposition::ERROR_CODE_NONE); 229 Geoposition::ERROR_CODE_NONE);
229 EXPECT_EQ(cell()->position_.latitude, observer_->last_position_.latitude); 230 EXPECT_EQ(cell()->position_.latitude, observer_->last_position_.latitude);
230 231
231 EXPECT_FALSE(cell()->is_permission_granted_); 232 EXPECT_FALSE(cell()->is_permission_granted_);
232 EXPECT_FALSE(arbitrator_->HasPermissionBeenGranted()); 233 EXPECT_FALSE(arbitrator_->HasPermissionBeenGrantedForTest());
233 arbitrator_->OnPermissionGranted(); 234 arbitrator_->OnPermissionGranted();
234 EXPECT_TRUE(arbitrator_->HasPermissionBeenGranted()); 235 EXPECT_TRUE(arbitrator_->HasPermissionBeenGrantedForTest());
235 EXPECT_TRUE(cell()->is_permission_granted_); 236 EXPECT_TRUE(cell()->is_permission_granted_);
236 } 237 }
237 238
238 TEST_F(GeolocationLocationArbitratorTest, CustomSystemProviderOnly) { 239 TEST_F(GeolocationLocationArbitratorTest, CustomSystemProviderOnly) {
239 FakeGeolocationDelegate* fake_delegate = new FakeGeolocationDelegate; 240 FakeGeolocationDelegate* fake_delegate = new FakeGeolocationDelegate;
240 fake_delegate->set_use_network(false); 241 fake_delegate->set_use_network(false);
241 242
242 std::unique_ptr<GeolocationDelegate> delegate(fake_delegate); 243 std::unique_ptr<GeolocationDelegate> delegate(fake_delegate);
243 InitializeArbitrator(std::move(delegate)); 244 InitializeArbitrator(std::move(delegate));
244 ASSERT_TRUE(arbitrator_); 245 ASSERT_TRUE(arbitrator_);
245 246
246 EXPECT_FALSE(cell()); 247 EXPECT_FALSE(cell());
247 EXPECT_FALSE(gps()); 248 EXPECT_FALSE(gps());
248 arbitrator_->StartProviders(false); 249 arbitrator_->StartProvider(false);
249 250
250 ASSERT_FALSE(cell()); 251 ASSERT_FALSE(cell());
251 EXPECT_FALSE(gps()); 252 EXPECT_FALSE(gps());
252 ASSERT_TRUE(fake_delegate->mock_location_provider()); 253 ASSERT_TRUE(fake_delegate->mock_location_provider());
253 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, 254 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY,
254 fake_delegate->mock_location_provider()->state_); 255 fake_delegate->mock_location_provider()->state_);
255 EXPECT_FALSE(observer_->last_position_.Validate()); 256 EXPECT_FALSE(observer_->last_position_.Validate());
256 EXPECT_EQ(Geoposition::ERROR_CODE_NONE, observer_->last_position_.error_code); 257 EXPECT_EQ(Geoposition::ERROR_CODE_NONE, observer_->last_position_.error_code);
257 258
258 SetReferencePosition(fake_delegate->mock_location_provider()); 259 SetReferencePosition(fake_delegate->mock_location_provider());
259 260
260 EXPECT_TRUE(observer_->last_position_.Validate() || 261 EXPECT_TRUE(observer_->last_position_.Validate() ||
261 observer_->last_position_.error_code != 262 observer_->last_position_.error_code !=
262 Geoposition::ERROR_CODE_NONE); 263 Geoposition::ERROR_CODE_NONE);
263 EXPECT_EQ(fake_delegate->mock_location_provider()->position_.latitude, 264 EXPECT_EQ(fake_delegate->mock_location_provider()->position_.latitude,
264 observer_->last_position_.latitude); 265 observer_->last_position_.latitude);
265 266
266 EXPECT_FALSE(fake_delegate->mock_location_provider()->is_permission_granted_); 267 EXPECT_FALSE(fake_delegate->mock_location_provider()->is_permission_granted_);
267 EXPECT_FALSE(arbitrator_->HasPermissionBeenGranted()); 268 EXPECT_FALSE(arbitrator_->HasPermissionBeenGrantedForTest());
268 arbitrator_->OnPermissionGranted(); 269 arbitrator_->OnPermissionGranted();
269 EXPECT_TRUE(arbitrator_->HasPermissionBeenGranted()); 270 EXPECT_TRUE(arbitrator_->HasPermissionBeenGrantedForTest());
270 EXPECT_TRUE(fake_delegate->mock_location_provider()->is_permission_granted_); 271 EXPECT_TRUE(fake_delegate->mock_location_provider()->is_permission_granted_);
271 } 272 }
272 273
273 TEST_F(GeolocationLocationArbitratorTest, 274 TEST_F(GeolocationLocationArbitratorTest,
274 CustomSystemAndDefaultNetworkProviders) { 275 CustomSystemAndDefaultNetworkProviders) {
275 FakeGeolocationDelegate* fake_delegate = new FakeGeolocationDelegate; 276 FakeGeolocationDelegate* fake_delegate = new FakeGeolocationDelegate;
276 fake_delegate->set_use_network(true); 277 fake_delegate->set_use_network(true);
277 278
278 std::unique_ptr<GeolocationDelegate> delegate(fake_delegate); 279 std::unique_ptr<GeolocationDelegate> delegate(fake_delegate);
279 InitializeArbitrator(std::move(delegate)); 280 InitializeArbitrator(std::move(delegate));
280 ASSERT_TRUE(arbitrator_); 281 ASSERT_TRUE(arbitrator_);
281 282
282 EXPECT_FALSE(cell()); 283 EXPECT_FALSE(cell());
283 EXPECT_FALSE(gps()); 284 EXPECT_FALSE(gps());
284 arbitrator_->StartProviders(false); 285 arbitrator_->StartProvider(false);
285 286
286 EXPECT_TRUE(access_token_store_->access_token_map_.empty()); 287 EXPECT_TRUE(access_token_store_->access_token_map_.empty());
287 288
288 access_token_store_->NotifyDelegateTokensLoaded(); 289 access_token_store_->NotifyDelegateTokensLoaded();
289 290
290 ASSERT_TRUE(cell()); 291 ASSERT_TRUE(cell());
291 EXPECT_FALSE(gps()); 292 EXPECT_FALSE(gps());
292 ASSERT_TRUE(fake_delegate->mock_location_provider()); 293 ASSERT_TRUE(fake_delegate->mock_location_provider());
293 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, 294 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY,
294 fake_delegate->mock_location_provider()->state_); 295 fake_delegate->mock_location_provider()->state_);
295 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, cell()->state_); 296 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, cell()->state_);
296 EXPECT_FALSE(observer_->last_position_.Validate()); 297 EXPECT_FALSE(observer_->last_position_.Validate());
297 EXPECT_EQ(Geoposition::ERROR_CODE_NONE, observer_->last_position_.error_code); 298 EXPECT_EQ(Geoposition::ERROR_CODE_NONE, observer_->last_position_.error_code);
298 299
299 SetReferencePosition(cell()); 300 SetReferencePosition(cell());
300 301
301 EXPECT_TRUE(observer_->last_position_.Validate() || 302 EXPECT_TRUE(observer_->last_position_.Validate() ||
302 observer_->last_position_.error_code != 303 observer_->last_position_.error_code !=
303 Geoposition::ERROR_CODE_NONE); 304 Geoposition::ERROR_CODE_NONE);
304 EXPECT_EQ(cell()->position_.latitude, observer_->last_position_.latitude); 305 EXPECT_EQ(cell()->position_.latitude, observer_->last_position_.latitude);
305 306
306 EXPECT_FALSE(cell()->is_permission_granted_); 307 EXPECT_FALSE(cell()->is_permission_granted_);
307 EXPECT_FALSE(arbitrator_->HasPermissionBeenGranted()); 308 EXPECT_FALSE(arbitrator_->HasPermissionBeenGrantedForTest());
308 arbitrator_->OnPermissionGranted(); 309 arbitrator_->OnPermissionGranted();
309 EXPECT_TRUE(arbitrator_->HasPermissionBeenGranted()); 310 EXPECT_TRUE(arbitrator_->HasPermissionBeenGrantedForTest());
310 EXPECT_TRUE(cell()->is_permission_granted_); 311 EXPECT_TRUE(cell()->is_permission_granted_);
311 } 312 }
312 313
313 TEST_F(GeolocationLocationArbitratorTest, SetObserverOptions) { 314 TEST_F(GeolocationLocationArbitratorTest, SetObserverOptions) {
314 InitializeArbitrator(nullptr); 315 InitializeArbitrator(nullptr);
315 arbitrator_->StartProviders(false); 316 arbitrator_->StartProvider(false);
316 access_token_store_->NotifyDelegateTokensLoaded(); 317 access_token_store_->NotifyDelegateTokensLoaded();
317 ASSERT_TRUE(cell()); 318 ASSERT_TRUE(cell());
318 ASSERT_TRUE(gps()); 319 ASSERT_TRUE(gps());
319 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, cell()->state_); 320 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, cell()->state_);
320 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, gps()->state_); 321 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, gps()->state_);
321 SetReferencePosition(cell()); 322 SetReferencePosition(cell());
322 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, cell()->state_); 323 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, cell()->state_);
323 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, gps()->state_); 324 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, gps()->state_);
324 arbitrator_->StartProviders(true); 325 arbitrator_->StartProvider(true);
325 EXPECT_EQ(MockLocationProvider::HIGH_ACCURACY, cell()->state_); 326 EXPECT_EQ(MockLocationProvider::HIGH_ACCURACY, cell()->state_);
326 EXPECT_EQ(MockLocationProvider::HIGH_ACCURACY, gps()->state_); 327 EXPECT_EQ(MockLocationProvider::HIGH_ACCURACY, gps()->state_);
327 } 328 }
328 329
329 TEST_F(GeolocationLocationArbitratorTest, Arbitration) { 330 TEST_F(GeolocationLocationArbitratorTest, Arbitration) {
330 InitializeArbitrator(nullptr); 331 InitializeArbitrator(nullptr);
331 arbitrator_->StartProviders(false); 332 arbitrator_->StartProvider(false);
332 access_token_store_->NotifyDelegateTokensLoaded(); 333 access_token_store_->NotifyDelegateTokensLoaded();
333 ASSERT_TRUE(cell()); 334 ASSERT_TRUE(cell());
334 ASSERT_TRUE(gps()); 335 ASSERT_TRUE(gps());
335 336
336 SetPositionFix(cell(), 1, 2, 150); 337 SetPositionFix(cell(), 1, 2, 150);
337 338
338 // First position available 339 // First position available
339 EXPECT_TRUE(observer_->last_position_.Validate()); 340 EXPECT_TRUE(observer_->last_position_.Validate());
340 CheckLastPositionInfo(1, 2, 150); 341 CheckLastPositionInfo(1, 2, 150);
341 342
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
398 399
399 // 2 minutes later 400 // 2 minutes later
400 AdvanceTimeNow(base::TimeDelta::FromMinutes(2)); 401 AdvanceTimeNow(base::TimeDelta::FromMinutes(2));
401 // Arrive in station. Cell moves but GPS is stale. Switch to fresher cell. 402 // Arrive in station. Cell moves but GPS is stale. Switch to fresher cell.
402 SetPositionFix(cell(), 3.5658700, 139.069979, 1000); 403 SetPositionFix(cell(), 3.5658700, 139.069979, 1000);
403 CheckLastPositionInfo(3.5658700, 139.069979, 1000); 404 CheckLastPositionInfo(3.5658700, 139.069979, 1000);
404 } 405 }
405 406
406 TEST_F(GeolocationLocationArbitratorTest, TwoOneShotsIsNewPositionBetter) { 407 TEST_F(GeolocationLocationArbitratorTest, TwoOneShotsIsNewPositionBetter) {
407 InitializeArbitrator(nullptr); 408 InitializeArbitrator(nullptr);
408 arbitrator_->StartProviders(false); 409 arbitrator_->StartProvider(false);
409 access_token_store_->NotifyDelegateTokensLoaded(); 410 access_token_store_->NotifyDelegateTokensLoaded();
410 ASSERT_TRUE(cell()); 411 ASSERT_TRUE(cell());
411 ASSERT_TRUE(gps()); 412 ASSERT_TRUE(gps());
412 413
413 // Set the initial position. 414 // Set the initial position.
414 SetPositionFix(cell(), 3, 139, 100); 415 SetPositionFix(cell(), 3, 139, 100);
415 CheckLastPositionInfo(3, 139, 100); 416 CheckLastPositionInfo(3, 139, 100);
416 417
417 // Restart providers to simulate a one-shot request. 418 // Restart providers to simulate a one-shot request.
418 arbitrator_->StopProviders(); 419 arbitrator_->StopProvider();
419 420
420 // To test 240956, perform a throwaway alloc. 421 // To test 240956, perform a throwaway alloc.
421 // This convinces the allocator to put the providers in a new memory location. 422 // This convinces the allocator to put the providers in a new memory location.
422 std::unique_ptr<MockLocationProvider> dummy_provider( 423 std::unique_ptr<MockLocationProvider> dummy_provider(
423 new MockLocationProvider); 424 new MockLocationProvider);
424 425
425 arbitrator_->StartProviders(false); 426 arbitrator_->StartProvider(false);
426 access_token_store_->NotifyDelegateTokensLoaded(); 427 access_token_store_->NotifyDelegateTokensLoaded();
427 428
428 // Advance the time a short while to simulate successive calls. 429 // Advance the time a short while to simulate successive calls.
429 AdvanceTimeNow(base::TimeDelta::FromMilliseconds(5)); 430 AdvanceTimeNow(base::TimeDelta::FromMilliseconds(5));
430 431
431 // Update with a less accurate position to verify 240956. 432 // Update with a less accurate position to verify 240956.
432 SetPositionFix(cell(), 3, 139, 150); 433 SetPositionFix(cell(), 3, 139, 150);
433 CheckLastPositionInfo(3, 139, 150); 434 CheckLastPositionInfo(3, 139, 150);
434 } 435 }
435 436
436 } // namespace device 437 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698