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

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

Issue 2161223003: Adds GeolocationFeature for Blimp Geolocation project. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@engine_feature_prep
Patch Set: Addresses kmarshall's #66 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"
13 #include "device/geolocation/fake_location_provider.h"
12 #include "device/geolocation/geolocation_delegate.h" 14 #include "device/geolocation/geolocation_delegate.h"
13 #include "device/geolocation/geoposition.h" 15 #include "device/geolocation/geoposition.h"
14 #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
18 using ::testing::NiceMock; 19 using ::testing::NiceMock;
19 20
20 namespace device { 21 namespace device {
21 22
22 class MockLocationObserver { 23 class MockLocationObserver {
23 public: 24 public:
24 // Need a vtable for GMock. 25 // Need a vtable for GMock.
(...skipping 14 matching lines...) Expand all
39 double g_fake_time_now_secs = 1; 40 double g_fake_time_now_secs = 1;
40 41
41 base::Time GetTimeNowForTest() { 42 base::Time GetTimeNowForTest() {
42 return base::Time::FromDoubleT(g_fake_time_now_secs); 43 return base::Time::FromDoubleT(g_fake_time_now_secs);
43 } 44 }
44 45
45 void AdvanceTimeNow(const base::TimeDelta& delta) { 46 void AdvanceTimeNow(const base::TimeDelta& delta) {
46 g_fake_time_now_secs += delta.InSecondsF(); 47 g_fake_time_now_secs += delta.InSecondsF();
47 } 48 }
48 49
49 void SetPositionFix(MockLocationProvider* provider, 50 void SetPositionFix(FakeLocationProvider* provider,
50 double latitude, 51 double latitude,
51 double longitude, 52 double longitude,
52 double accuracy) { 53 double accuracy) {
53 Geoposition position; 54 Geoposition position;
54 position.error_code = Geoposition::ERROR_CODE_NONE; 55 position.error_code = Geoposition::ERROR_CODE_NONE;
55 position.latitude = latitude; 56 position.latitude = latitude;
56 position.longitude = longitude; 57 position.longitude = longitude;
57 position.accuracy = accuracy; 58 position.accuracy = accuracy;
58 position.timestamp = GetTimeNowForTest(); 59 position.timestamp = GetTimeNowForTest();
59 ASSERT_TRUE(position.Validate()); 60 ASSERT_TRUE(position.Validate());
60 provider->HandlePositionChanged(position); 61 provider->HandlePositionChanged(position);
61 } 62 }
62 63
63 void SetReferencePosition(MockLocationProvider* provider) { 64 // TODO(lethalantidote): Populate a Geoposition in the class from kConstants
65 // and then just copy that with "=" versus using a helper function.
66 void SetReferencePosition(FakeLocationProvider* provider) {
64 SetPositionFix(provider, 51.0, -0.1, 400); 67 SetPositionFix(provider, 51.0, -0.1, 400);
65 } 68 }
66 69
67 namespace { 70 namespace {
68 71
69 class FakeGeolocationDelegate : public GeolocationDelegate { 72 class FakeGeolocationDelegate : public GeolocationDelegate {
70 public: 73 public:
71 FakeGeolocationDelegate() = default; 74 FakeGeolocationDelegate() = default;
72 75
73 bool UseNetworkLocationProviders() override { return use_network_; } 76 bool UseNetworkLocationProviders() override { return use_network_; }
74 void set_use_network(bool use_network) { use_network_ = use_network; } 77 void set_use_network(bool use_network) { use_network_ = use_network; }
75 78
76 std::unique_ptr<LocationProvider> OverrideSystemLocationProvider() override { 79 std::unique_ptr<LocationProvider> OverrideSystemLocationProvider() override {
77 DCHECK(!mock_location_provider_); 80 DCHECK(!mock_location_provider_);
78 mock_location_provider_ = new MockLocationProvider; 81 mock_location_provider_ = new FakeLocationProvider;
79 return base::WrapUnique(mock_location_provider_); 82 return base::WrapUnique(mock_location_provider_);
80 } 83 }
81 84
82 MockLocationProvider* mock_location_provider() const { 85 FakeLocationProvider* mock_location_provider() const {
83 return mock_location_provider_; 86 return mock_location_provider_;
84 } 87 }
85 88
86 private: 89 private:
87 bool use_network_ = true; 90 bool use_network_ = true;
88 MockLocationProvider* mock_location_provider_ = nullptr; 91 FakeLocationProvider* mock_location_provider_ = nullptr;
89 92
90 DISALLOW_COPY_AND_ASSIGN(FakeGeolocationDelegate); 93 DISALLOW_COPY_AND_ASSIGN(FakeGeolocationDelegate);
91 }; 94 };
92 95
93 } // namespace 96 } // namespace
94 97
95 class TestingLocationArbitrator : public LocationArbitratorImpl { 98 class TestingLocationArbitrator : public LocationArbitratorImpl {
96 public: 99 public:
97 TestingLocationArbitrator( 100 TestingLocationArbitrator(
98 const LocationArbitratorImpl::LocationUpdateCallback& callback, 101 const LocationArbitratorImpl::LocationUpdateCallback& callback,
99 const scoped_refptr<AccessTokenStore>& access_token_store, 102 const scoped_refptr<AccessTokenStore>& access_token_store,
100 GeolocationDelegate* delegate) 103 GeolocationDelegate* delegate)
101 : LocationArbitratorImpl(callback, delegate), 104 : LocationArbitratorImpl(callback, delegate),
102 cell_(nullptr), 105 cell_(nullptr),
103 gps_(nullptr), 106 gps_(nullptr),
104 access_token_store_(access_token_store) {} 107 access_token_store_(access_token_store) {}
105 108
106 base::Time GetTimeNow() const override { return GetTimeNowForTest(); } 109 base::Time GetTimeNow() const override { return GetTimeNowForTest(); }
107 110
108 scoped_refptr<AccessTokenStore> NewAccessTokenStore() override { 111 scoped_refptr<AccessTokenStore> NewAccessTokenStore() override {
109 return access_token_store_; 112 return access_token_store_;
110 } 113 }
111 114
112 std::unique_ptr<LocationProvider> NewNetworkLocationProvider( 115 std::unique_ptr<LocationProvider> NewNetworkLocationProvider(
113 const scoped_refptr<AccessTokenStore>& access_token_store, 116 const scoped_refptr<AccessTokenStore>& access_token_store,
114 const scoped_refptr<net::URLRequestContextGetter>& context, 117 const scoped_refptr<net::URLRequestContextGetter>& context,
115 const GURL& url, 118 const GURL& url,
116 const base::string16& access_token) override { 119 const base::string16& access_token) override {
117 cell_ = new MockLocationProvider; 120 cell_ = new FakeLocationProvider;
118 return base::WrapUnique(cell_); 121 return base::WrapUnique(cell_);
119 } 122 }
120 123
121 std::unique_ptr<LocationProvider> NewSystemLocationProvider() override { 124 std::unique_ptr<LocationProvider> NewSystemLocationProvider() override {
122 gps_ = new MockLocationProvider; 125 gps_ = new FakeLocationProvider;
123 return base::WrapUnique(gps_); 126 return base::WrapUnique(gps_);
124 } 127 }
125 128
126 // Two location providers, with nice short names to make the tests more 129 // 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 130 // readable. Note |gps_| will only be set when there is a high accuracy
128 // observer registered (and |cell_| when there's at least one observer of any 131 // observer registered (and |cell_| when there's at least one observer of any
129 // type). 132 // type).
130 // TODO(mvanouwerkerk): rename |cell_| to |network_location_provider_| and 133 // TODO(mvanouwerkerk): rename |cell_| to |network_location_provider_| and
131 // |gps_| to |gps_location_provider_| 134 // |gps_| to |gps_location_provider_|
132 MockLocationProvider* cell_; 135 FakeLocationProvider* cell_;
133 MockLocationProvider* gps_; 136 FakeLocationProvider* gps_;
134 const scoped_refptr<AccessTokenStore> access_token_store_; 137 const scoped_refptr<AccessTokenStore> access_token_store_;
135 }; 138 };
136 139
137 class GeolocationLocationArbitratorTest : public testing::Test { 140 class GeolocationLocationArbitratorTest : public testing::Test {
138 protected: 141 protected:
139 GeolocationLocationArbitratorTest() 142 GeolocationLocationArbitratorTest()
140 : access_token_store_(new NiceMock<FakeAccessTokenStore>), 143 : access_token_store_(new NiceMock<FakeAccessTokenStore>),
141 observer_(new MockLocationObserver), 144 observer_(new MockLocationObserver),
142 delegate_(new GeolocationDelegate) {} 145 delegate_(new GeolocationDelegate) {}
143 146
(...skipping 21 matching lines...) Expand all
165 EXPECT_DOUBLE_EQ(longitude, geoposition.longitude); 168 EXPECT_DOUBLE_EQ(longitude, geoposition.longitude);
166 EXPECT_DOUBLE_EQ(accuracy, geoposition.accuracy); 169 EXPECT_DOUBLE_EQ(accuracy, geoposition.accuracy);
167 } 170 }
168 171
169 base::TimeDelta SwitchOnFreshnessCliff() { 172 base::TimeDelta SwitchOnFreshnessCliff() {
170 // Add 1, to ensure it meets any greater-than test. 173 // Add 1, to ensure it meets any greater-than test.
171 return base::TimeDelta::FromMilliseconds( 174 return base::TimeDelta::FromMilliseconds(
172 LocationArbitratorImpl::kFixStaleTimeoutMilliseconds + 1); 175 LocationArbitratorImpl::kFixStaleTimeoutMilliseconds + 1);
173 } 176 }
174 177
175 MockLocationProvider* cell() { return arbitrator_->cell_; } 178 FakeLocationProvider* cell() { return arbitrator_->cell_; }
176 179
177 MockLocationProvider* gps() { return arbitrator_->gps_; } 180 FakeLocationProvider* gps() { return arbitrator_->gps_; }
178 181
179 const scoped_refptr<FakeAccessTokenStore> access_token_store_; 182 const scoped_refptr<FakeAccessTokenStore> access_token_store_;
180 const std::unique_ptr<MockLocationObserver> observer_; 183 const std::unique_ptr<MockLocationObserver> observer_;
181 std::unique_ptr<TestingLocationArbitrator> arbitrator_; 184 std::unique_ptr<TestingLocationArbitrator> arbitrator_;
182 std::unique_ptr<GeolocationDelegate> delegate_; 185 std::unique_ptr<GeolocationDelegate> delegate_;
183 const base::MessageLoop loop_; 186 const base::MessageLoop loop_;
184 }; 187 };
185 188
186 TEST_F(GeolocationLocationArbitratorTest, CreateDestroy) { 189 TEST_F(GeolocationLocationArbitratorTest, CreateDestroy) {
187 EXPECT_TRUE(access_token_store_.get()); 190 EXPECT_TRUE(access_token_store_.get());
(...skipping 21 matching lines...) Expand all
209 212
210 EXPECT_FALSE(cell()); 213 EXPECT_FALSE(cell());
211 EXPECT_FALSE(gps()); 214 EXPECT_FALSE(gps());
212 arbitrator_->StartProviders(false); 215 arbitrator_->StartProviders(false);
213 216
214 EXPECT_TRUE(access_token_store_->access_token_map_.empty()); 217 EXPECT_TRUE(access_token_store_->access_token_map_.empty());
215 218
216 access_token_store_->NotifyDelegateTokensLoaded(); 219 access_token_store_->NotifyDelegateTokensLoaded();
217 ASSERT_TRUE(cell()); 220 ASSERT_TRUE(cell());
218 EXPECT_TRUE(gps()); 221 EXPECT_TRUE(gps());
219 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, cell()->state_); 222 EXPECT_EQ(FakeLocationProvider::LOW_ACCURACY, cell()->state_);
220 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, gps()->state_); 223 EXPECT_EQ(FakeLocationProvider::LOW_ACCURACY, gps()->state_);
221 EXPECT_FALSE(observer_->last_position_.Validate()); 224 EXPECT_FALSE(observer_->last_position_.Validate());
222 EXPECT_EQ(Geoposition::ERROR_CODE_NONE, observer_->last_position_.error_code); 225 EXPECT_EQ(Geoposition::ERROR_CODE_NONE, observer_->last_position_.error_code);
223 226
224 SetReferencePosition(cell()); 227 SetReferencePosition(cell());
225 228
226 EXPECT_TRUE(observer_->last_position_.Validate() || 229 EXPECT_TRUE(observer_->last_position_.Validate() ||
227 observer_->last_position_.error_code != 230 observer_->last_position_.error_code !=
228 Geoposition::ERROR_CODE_NONE); 231 Geoposition::ERROR_CODE_NONE);
229 EXPECT_EQ(cell()->position_.latitude, observer_->last_position_.latitude); 232 EXPECT_EQ(cell()->position_.latitude, observer_->last_position_.latitude);
230 233
(...skipping 12 matching lines...) Expand all
243 InitializeArbitrator(std::move(delegate)); 246 InitializeArbitrator(std::move(delegate));
244 ASSERT_TRUE(arbitrator_); 247 ASSERT_TRUE(arbitrator_);
245 248
246 EXPECT_FALSE(cell()); 249 EXPECT_FALSE(cell());
247 EXPECT_FALSE(gps()); 250 EXPECT_FALSE(gps());
248 arbitrator_->StartProviders(false); 251 arbitrator_->StartProviders(false);
249 252
250 ASSERT_FALSE(cell()); 253 ASSERT_FALSE(cell());
251 EXPECT_FALSE(gps()); 254 EXPECT_FALSE(gps());
252 ASSERT_TRUE(fake_delegate->mock_location_provider()); 255 ASSERT_TRUE(fake_delegate->mock_location_provider());
253 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, 256 EXPECT_EQ(FakeLocationProvider::LOW_ACCURACY,
254 fake_delegate->mock_location_provider()->state_); 257 fake_delegate->mock_location_provider()->state_);
255 EXPECT_FALSE(observer_->last_position_.Validate()); 258 EXPECT_FALSE(observer_->last_position_.Validate());
256 EXPECT_EQ(Geoposition::ERROR_CODE_NONE, observer_->last_position_.error_code); 259 EXPECT_EQ(Geoposition::ERROR_CODE_NONE, observer_->last_position_.error_code);
257 260
258 SetReferencePosition(fake_delegate->mock_location_provider()); 261 SetReferencePosition(fake_delegate->mock_location_provider());
259 262
260 EXPECT_TRUE(observer_->last_position_.Validate() || 263 EXPECT_TRUE(observer_->last_position_.Validate() ||
261 observer_->last_position_.error_code != 264 observer_->last_position_.error_code !=
262 Geoposition::ERROR_CODE_NONE); 265 Geoposition::ERROR_CODE_NONE);
263 EXPECT_EQ(fake_delegate->mock_location_provider()->position_.latitude, 266 EXPECT_EQ(fake_delegate->mock_location_provider()->position_.latitude,
(...skipping 19 matching lines...) Expand all
283 EXPECT_FALSE(gps()); 286 EXPECT_FALSE(gps());
284 arbitrator_->StartProviders(false); 287 arbitrator_->StartProviders(false);
285 288
286 EXPECT_TRUE(access_token_store_->access_token_map_.empty()); 289 EXPECT_TRUE(access_token_store_->access_token_map_.empty());
287 290
288 access_token_store_->NotifyDelegateTokensLoaded(); 291 access_token_store_->NotifyDelegateTokensLoaded();
289 292
290 ASSERT_TRUE(cell()); 293 ASSERT_TRUE(cell());
291 EXPECT_FALSE(gps()); 294 EXPECT_FALSE(gps());
292 ASSERT_TRUE(fake_delegate->mock_location_provider()); 295 ASSERT_TRUE(fake_delegate->mock_location_provider());
293 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, 296 EXPECT_EQ(FakeLocationProvider::LOW_ACCURACY,
294 fake_delegate->mock_location_provider()->state_); 297 fake_delegate->mock_location_provider()->state_);
295 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, cell()->state_); 298 EXPECT_EQ(FakeLocationProvider::LOW_ACCURACY, cell()->state_);
296 EXPECT_FALSE(observer_->last_position_.Validate()); 299 EXPECT_FALSE(observer_->last_position_.Validate());
297 EXPECT_EQ(Geoposition::ERROR_CODE_NONE, observer_->last_position_.error_code); 300 EXPECT_EQ(Geoposition::ERROR_CODE_NONE, observer_->last_position_.error_code);
298 301
299 SetReferencePosition(cell()); 302 SetReferencePosition(cell());
300 303
301 EXPECT_TRUE(observer_->last_position_.Validate() || 304 EXPECT_TRUE(observer_->last_position_.Validate() ||
302 observer_->last_position_.error_code != 305 observer_->last_position_.error_code !=
303 Geoposition::ERROR_CODE_NONE); 306 Geoposition::ERROR_CODE_NONE);
304 EXPECT_EQ(cell()->position_.latitude, observer_->last_position_.latitude); 307 EXPECT_EQ(cell()->position_.latitude, observer_->last_position_.latitude);
305 308
306 EXPECT_FALSE(cell()->is_permission_granted_); 309 EXPECT_FALSE(cell()->is_permission_granted_);
307 EXPECT_FALSE(arbitrator_->HasPermissionBeenGranted()); 310 EXPECT_FALSE(arbitrator_->HasPermissionBeenGranted());
308 arbitrator_->OnPermissionGranted(); 311 arbitrator_->OnPermissionGranted();
309 EXPECT_TRUE(arbitrator_->HasPermissionBeenGranted()); 312 EXPECT_TRUE(arbitrator_->HasPermissionBeenGranted());
310 EXPECT_TRUE(cell()->is_permission_granted_); 313 EXPECT_TRUE(cell()->is_permission_granted_);
311 } 314 }
312 315
313 TEST_F(GeolocationLocationArbitratorTest, SetObserverOptions) { 316 TEST_F(GeolocationLocationArbitratorTest, SetObserverOptions) {
314 InitializeArbitrator(nullptr); 317 InitializeArbitrator(nullptr);
315 arbitrator_->StartProviders(false); 318 arbitrator_->StartProviders(false);
316 access_token_store_->NotifyDelegateTokensLoaded(); 319 access_token_store_->NotifyDelegateTokensLoaded();
317 ASSERT_TRUE(cell()); 320 ASSERT_TRUE(cell());
318 ASSERT_TRUE(gps()); 321 ASSERT_TRUE(gps());
319 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, cell()->state_); 322 EXPECT_EQ(FakeLocationProvider::LOW_ACCURACY, cell()->state_);
320 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, gps()->state_); 323 EXPECT_EQ(FakeLocationProvider::LOW_ACCURACY, gps()->state_);
321 SetReferencePosition(cell()); 324 SetReferencePosition(cell());
322 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, cell()->state_); 325 EXPECT_EQ(FakeLocationProvider::LOW_ACCURACY, cell()->state_);
323 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, gps()->state_); 326 EXPECT_EQ(FakeLocationProvider::LOW_ACCURACY, gps()->state_);
324 arbitrator_->StartProviders(true); 327 arbitrator_->StartProviders(true);
325 EXPECT_EQ(MockLocationProvider::HIGH_ACCURACY, cell()->state_); 328 EXPECT_EQ(FakeLocationProvider::HIGH_ACCURACY, cell()->state_);
326 EXPECT_EQ(MockLocationProvider::HIGH_ACCURACY, gps()->state_); 329 EXPECT_EQ(FakeLocationProvider::HIGH_ACCURACY, gps()->state_);
327 } 330 }
328 331
329 TEST_F(GeolocationLocationArbitratorTest, Arbitration) { 332 TEST_F(GeolocationLocationArbitratorTest, Arbitration) {
330 InitializeArbitrator(nullptr); 333 InitializeArbitrator(nullptr);
331 arbitrator_->StartProviders(false); 334 arbitrator_->StartProviders(false);
332 access_token_store_->NotifyDelegateTokensLoaded(); 335 access_token_store_->NotifyDelegateTokensLoaded();
333 ASSERT_TRUE(cell()); 336 ASSERT_TRUE(cell());
334 ASSERT_TRUE(gps()); 337 ASSERT_TRUE(gps());
335 338
336 SetPositionFix(cell(), 1, 2, 150); 339 SetPositionFix(cell(), 1, 2, 150);
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
412 415
413 // Set the initial position. 416 // Set the initial position.
414 SetPositionFix(cell(), 3, 139, 100); 417 SetPositionFix(cell(), 3, 139, 100);
415 CheckLastPositionInfo(3, 139, 100); 418 CheckLastPositionInfo(3, 139, 100);
416 419
417 // Restart providers to simulate a one-shot request. 420 // Restart providers to simulate a one-shot request.
418 arbitrator_->StopProviders(); 421 arbitrator_->StopProviders();
419 422
420 // To test 240956, perform a throwaway alloc. 423 // To test 240956, perform a throwaway alloc.
421 // This convinces the allocator to put the providers in a new memory location. 424 // This convinces the allocator to put the providers in a new memory location.
422 std::unique_ptr<MockLocationProvider> dummy_provider( 425 std::unique_ptr<FakeLocationProvider> dummy_provider(
423 new MockLocationProvider); 426 new FakeLocationProvider);
424 427
425 arbitrator_->StartProviders(false); 428 arbitrator_->StartProviders(false);
426 access_token_store_->NotifyDelegateTokensLoaded(); 429 access_token_store_->NotifyDelegateTokensLoaded();
427 430
428 // Advance the time a short while to simulate successive calls. 431 // Advance the time a short while to simulate successive calls.
429 AdvanceTimeNow(base::TimeDelta::FromMilliseconds(5)); 432 AdvanceTimeNow(base::TimeDelta::FromMilliseconds(5));
430 433
431 // Update with a less accurate position to verify 240956. 434 // Update with a less accurate position to verify 240956.
432 SetPositionFix(cell(), 3, 139, 150); 435 SetPositionFix(cell(), 3, 139, 150);
433 CheckLastPositionInfo(3, 139, 150); 436 CheckLastPositionInfo(3, 139, 150);
434 } 437 }
435 438
436 } // namespace device 439 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698