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

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

Issue 2249283003: Hooks together Geolocation Feature in the Client and Engine. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@lai
Patch Set: In response to Wez's #48 comments. Created 4 years, 3 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
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "device/geolocation/location_arbitrator_impl.h"
6
7 #include <memory>
8 #include <utility>
9
10 #include "base/bind.h"
11 #include "base/memory/ptr_util.h"
12 #include "device/geolocation/fake_access_token_store.h"
13 #include "device/geolocation/fake_location_provider.h"
14 #include "device/geolocation/geolocation_delegate.h"
15 #include "device/geolocation/geoposition.h"
16 #include "testing/gmock/include/gmock/gmock.h"
17 #include "testing/gtest/include/gtest/gtest.h"
18
19 using ::testing::NiceMock;
20
21 namespace device {
22
23 class MockLocationObserver {
24 public:
25 // Need a vtable for GMock.
26 virtual ~MockLocationObserver() {}
27 void InvalidateLastPosition() {
28 last_position_.latitude = 100;
29 last_position_.error_code = Geoposition::ERROR_CODE_NONE;
30 ASSERT_FALSE(last_position_.Validate());
31 }
32 // Delegate
33 void OnLocationUpdate(const LocationProvider*, const Geoposition& position) {
34 last_position_ = position;
35 }
36
37 Geoposition last_position_;
38 };
39
40 double g_fake_time_now_secs = 1;
41
42 base::Time GetTimeNowForTest() {
43 return base::Time::FromDoubleT(g_fake_time_now_secs);
44 }
45
46 void AdvanceTimeNow(const base::TimeDelta& delta) {
47 g_fake_time_now_secs += delta.InSecondsF();
48 }
49
50 void SetPositionFix(FakeLocationProvider* provider,
51 double latitude,
52 double longitude,
53 double accuracy) {
54 Geoposition position;
55 position.error_code = Geoposition::ERROR_CODE_NONE;
56 position.latitude = latitude;
57 position.longitude = longitude;
58 position.accuracy = accuracy;
59 position.timestamp = GetTimeNowForTest();
60 ASSERT_TRUE(position.Validate());
61 provider->HandlePositionChanged(position);
62 }
63
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) {
67 SetPositionFix(provider, 51.0, -0.1, 400);
68 }
69
70 namespace {
71
72 class FakeGeolocationDelegate : public GeolocationDelegate {
73 public:
74 FakeGeolocationDelegate() = default;
75
76 bool UseNetworkLocationProviders() override { return use_network_; }
77 void set_use_network(bool use_network) { use_network_ = use_network; }
78
79 std::unique_ptr<LocationProvider> OverrideSystemLocationProvider() override {
80 DCHECK(!mock_location_provider_);
81 mock_location_provider_ = new FakeLocationProvider;
82 return base::WrapUnique(mock_location_provider_);
83 }
84
85 FakeLocationProvider* mock_location_provider() const {
86 return mock_location_provider_;
87 }
88
89 private:
90 bool use_network_ = true;
91 FakeLocationProvider* mock_location_provider_ = nullptr;
92
93 DISALLOW_COPY_AND_ASSIGN(FakeGeolocationDelegate);
94 };
95
96 } // namespace
97
98 class TestingLocationArbitrator : public LocationArbitratorImpl {
99 public:
100 TestingLocationArbitrator(
101 const LocationProviderUpdateCallback& callback,
102 const scoped_refptr<AccessTokenStore>& access_token_store,
103 GeolocationDelegate* delegate)
104 : LocationArbitratorImpl(delegate),
105 cell_(nullptr),
106 gps_(nullptr),
107 access_token_store_(access_token_store) {
108 SetUpdateCallback(callback);
109 }
110
111 base::Time GetTimeNow() const override { return GetTimeNowForTest(); }
112
113 scoped_refptr<AccessTokenStore> NewAccessTokenStore() override {
114 return access_token_store_;
115 }
116
117 std::unique_ptr<LocationProvider> NewNetworkLocationProvider(
118 const scoped_refptr<AccessTokenStore>& access_token_store,
119 const scoped_refptr<net::URLRequestContextGetter>& context,
120 const GURL& url,
121 const base::string16& access_token) override {
122 cell_ = new FakeLocationProvider;
123 return base::WrapUnique(cell_);
124 }
125
126 std::unique_ptr<LocationProvider> NewSystemLocationProvider() override {
127 gps_ = new FakeLocationProvider;
128 return base::WrapUnique(gps_);
129 }
130
131 // Two location providers, with nice short names to make the tests more
132 // readable. Note |gps_| will only be set when there is a high accuracy
133 // observer registered (and |cell_| when there's at least one observer of any
134 // type).
135 // TODO(mvanouwerkerk): rename |cell_| to |network_location_provider_| and
136 // |gps_| to |gps_location_provider_|
137 FakeLocationProvider* cell_;
138 FakeLocationProvider* gps_;
139 const scoped_refptr<AccessTokenStore> access_token_store_;
140 };
141
142 class GeolocationLocationArbitratorTest : public testing::Test {
143 protected:
144 GeolocationLocationArbitratorTest()
145 : access_token_store_(new NiceMock<FakeAccessTokenStore>),
146 observer_(new MockLocationObserver),
147 delegate_(new GeolocationDelegate) {}
148
149 // Initializes |arbitrator_|, with the possibility of injecting a specific
150 // |delegate|, otherwise a default, no-op GeolocationDelegate is used.
151 void InitializeArbitrator(std::unique_ptr<GeolocationDelegate> delegate) {
152 if (delegate)
153 delegate_ = std::move(delegate);
154 const LocationProvider::LocationProviderUpdateCallback callback =
155 base::Bind(&MockLocationObserver::OnLocationUpdate,
156 base::Unretained(observer_.get()));
157 arbitrator_.reset(new TestingLocationArbitrator(
158 callback, access_token_store_, delegate_.get()));
159 }
160
161 // testing::Test
162 void TearDown() override {}
163
164 void CheckLastPositionInfo(double latitude,
165 double longitude,
166 double accuracy) {
167 Geoposition geoposition = observer_->last_position_;
168 EXPECT_TRUE(geoposition.Validate());
169 EXPECT_DOUBLE_EQ(latitude, geoposition.latitude);
170 EXPECT_DOUBLE_EQ(longitude, geoposition.longitude);
171 EXPECT_DOUBLE_EQ(accuracy, geoposition.accuracy);
172 }
173
174 base::TimeDelta SwitchOnFreshnessCliff() {
175 // Add 1, to ensure it meets any greater-than test.
176 return base::TimeDelta::FromMilliseconds(
177 LocationArbitratorImpl::kFixStaleTimeoutMilliseconds + 1);
178 }
179
180 FakeLocationProvider* cell() { return arbitrator_->cell_; }
181
182 FakeLocationProvider* gps() { return arbitrator_->gps_; }
183
184 const scoped_refptr<FakeAccessTokenStore> access_token_store_;
185 const std::unique_ptr<MockLocationObserver> observer_;
186 std::unique_ptr<TestingLocationArbitrator> arbitrator_;
187 std::unique_ptr<GeolocationDelegate> delegate_;
188 const base::MessageLoop loop_;
189 };
190
191 TEST_F(GeolocationLocationArbitratorTest, CreateDestroy) {
192 EXPECT_TRUE(access_token_store_.get());
193 InitializeArbitrator(nullptr);
194 EXPECT_TRUE(arbitrator_);
195 arbitrator_.reset();
196 SUCCEED();
197 }
198
199 TEST_F(GeolocationLocationArbitratorTest, OnPermissionGranted) {
200 InitializeArbitrator(nullptr);
201 EXPECT_FALSE(arbitrator_->HasPermissionBeenGrantedForTest());
202 arbitrator_->OnPermissionGranted();
203 EXPECT_TRUE(arbitrator_->HasPermissionBeenGrantedForTest());
204 // Can't check the provider has been notified without going through the
205 // motions to create the provider (see next test).
206 EXPECT_FALSE(cell());
207 EXPECT_FALSE(gps());
208 }
209
210 TEST_F(GeolocationLocationArbitratorTest, NormalUsage) {
211 InitializeArbitrator(nullptr);
212 ASSERT_TRUE(access_token_store_.get());
213 ASSERT_TRUE(arbitrator_);
214
215 EXPECT_FALSE(cell());
216 EXPECT_FALSE(gps());
217 arbitrator_->StartProvider(false);
218
219 EXPECT_TRUE(access_token_store_->access_token_map_.empty());
220
221 access_token_store_->NotifyDelegateTokensLoaded();
222 ASSERT_TRUE(cell());
223 EXPECT_TRUE(gps());
224 EXPECT_EQ(FakeLocationProvider::LOW_ACCURACY, cell()->state_);
225 EXPECT_EQ(FakeLocationProvider::LOW_ACCURACY, gps()->state_);
226 EXPECT_FALSE(observer_->last_position_.Validate());
227 EXPECT_EQ(Geoposition::ERROR_CODE_NONE, observer_->last_position_.error_code);
228
229 SetReferencePosition(cell());
230
231 EXPECT_TRUE(observer_->last_position_.Validate() ||
232 observer_->last_position_.error_code !=
233 Geoposition::ERROR_CODE_NONE);
234 EXPECT_EQ(cell()->GetPosition().latitude, observer_->last_position_.latitude);
235
236 EXPECT_FALSE(cell()->is_permission_granted());
237 EXPECT_FALSE(arbitrator_->HasPermissionBeenGrantedForTest());
238 arbitrator_->OnPermissionGranted();
239 EXPECT_TRUE(arbitrator_->HasPermissionBeenGrantedForTest());
240 EXPECT_TRUE(cell()->is_permission_granted());
241 }
242
243 TEST_F(GeolocationLocationArbitratorTest, CustomSystemProviderOnly) {
244 FakeGeolocationDelegate* fake_delegate = new FakeGeolocationDelegate;
245 fake_delegate->set_use_network(false);
246
247 std::unique_ptr<GeolocationDelegate> delegate(fake_delegate);
248 InitializeArbitrator(std::move(delegate));
249 ASSERT_TRUE(arbitrator_);
250
251 EXPECT_FALSE(cell());
252 EXPECT_FALSE(gps());
253 arbitrator_->StartProvider(false);
254
255 ASSERT_FALSE(cell());
256 EXPECT_FALSE(gps());
257 ASSERT_TRUE(fake_delegate->mock_location_provider());
258 EXPECT_EQ(FakeLocationProvider::LOW_ACCURACY,
259 fake_delegate->mock_location_provider()->state_);
260 EXPECT_FALSE(observer_->last_position_.Validate());
261 EXPECT_EQ(Geoposition::ERROR_CODE_NONE, observer_->last_position_.error_code);
262
263 SetReferencePosition(fake_delegate->mock_location_provider());
264
265 EXPECT_TRUE(observer_->last_position_.Validate() ||
266 observer_->last_position_.error_code !=
267 Geoposition::ERROR_CODE_NONE);
268 EXPECT_EQ(fake_delegate->mock_location_provider()->GetPosition().latitude,
269 observer_->last_position_.latitude);
270
271 EXPECT_FALSE(
272 fake_delegate->mock_location_provider()->is_permission_granted());
273 EXPECT_FALSE(arbitrator_->HasPermissionBeenGrantedForTest());
274 arbitrator_->OnPermissionGranted();
275 EXPECT_TRUE(arbitrator_->HasPermissionBeenGrantedForTest());
276 EXPECT_TRUE(fake_delegate->mock_location_provider()->is_permission_granted());
277 }
278
279 TEST_F(GeolocationLocationArbitratorTest,
280 CustomSystemAndDefaultNetworkProviders) {
281 FakeGeolocationDelegate* fake_delegate = new FakeGeolocationDelegate;
282 fake_delegate->set_use_network(true);
283
284 std::unique_ptr<GeolocationDelegate> delegate(fake_delegate);
285 InitializeArbitrator(std::move(delegate));
286 ASSERT_TRUE(arbitrator_);
287
288 EXPECT_FALSE(cell());
289 EXPECT_FALSE(gps());
290 arbitrator_->StartProvider(false);
291
292 EXPECT_TRUE(access_token_store_->access_token_map_.empty());
293
294 access_token_store_->NotifyDelegateTokensLoaded();
295
296 ASSERT_TRUE(cell());
297 EXPECT_FALSE(gps());
298 ASSERT_TRUE(fake_delegate->mock_location_provider());
299 EXPECT_EQ(FakeLocationProvider::LOW_ACCURACY,
300 fake_delegate->mock_location_provider()->state_);
301 EXPECT_EQ(FakeLocationProvider::LOW_ACCURACY, cell()->state_);
302 EXPECT_FALSE(observer_->last_position_.Validate());
303 EXPECT_EQ(Geoposition::ERROR_CODE_NONE, observer_->last_position_.error_code);
304
305 SetReferencePosition(cell());
306
307 EXPECT_TRUE(observer_->last_position_.Validate() ||
308 observer_->last_position_.error_code !=
309 Geoposition::ERROR_CODE_NONE);
310 EXPECT_EQ(cell()->GetPosition().latitude, observer_->last_position_.latitude);
311
312 EXPECT_FALSE(cell()->is_permission_granted());
313 EXPECT_FALSE(arbitrator_->HasPermissionBeenGrantedForTest());
314 arbitrator_->OnPermissionGranted();
315 EXPECT_TRUE(arbitrator_->HasPermissionBeenGrantedForTest());
316 EXPECT_TRUE(cell()->is_permission_granted());
317 }
318
319 TEST_F(GeolocationLocationArbitratorTest, SetObserverOptions) {
320 InitializeArbitrator(nullptr);
321 arbitrator_->StartProvider(false);
322 access_token_store_->NotifyDelegateTokensLoaded();
323 ASSERT_TRUE(cell());
324 ASSERT_TRUE(gps());
325 EXPECT_EQ(FakeLocationProvider::LOW_ACCURACY, cell()->state_);
326 EXPECT_EQ(FakeLocationProvider::LOW_ACCURACY, gps()->state_);
327 SetReferencePosition(cell());
328 EXPECT_EQ(FakeLocationProvider::LOW_ACCURACY, cell()->state_);
329 EXPECT_EQ(FakeLocationProvider::LOW_ACCURACY, gps()->state_);
330 arbitrator_->StartProvider(true);
331 EXPECT_EQ(FakeLocationProvider::HIGH_ACCURACY, cell()->state_);
332 EXPECT_EQ(FakeLocationProvider::HIGH_ACCURACY, gps()->state_);
333 }
334
335 TEST_F(GeolocationLocationArbitratorTest, Arbitration) {
336 InitializeArbitrator(nullptr);
337 arbitrator_->StartProvider(false);
338 access_token_store_->NotifyDelegateTokensLoaded();
339 ASSERT_TRUE(cell());
340 ASSERT_TRUE(gps());
341
342 SetPositionFix(cell(), 1, 2, 150);
343
344 // First position available
345 EXPECT_TRUE(observer_->last_position_.Validate());
346 CheckLastPositionInfo(1, 2, 150);
347
348 SetPositionFix(gps(), 3, 4, 50);
349
350 // More accurate fix available
351 CheckLastPositionInfo(3, 4, 50);
352
353 SetPositionFix(cell(), 5, 6, 150);
354
355 // New fix is available but it's less accurate, older fix should be kept.
356 CheckLastPositionInfo(3, 4, 50);
357
358 // Advance time, and notify once again
359 AdvanceTimeNow(SwitchOnFreshnessCliff());
360 cell()->HandlePositionChanged(cell()->GetPosition());
361
362 // New fix is available, less accurate but fresher
363 CheckLastPositionInfo(5, 6, 150);
364
365 // Advance time, and set a low accuracy position
366 AdvanceTimeNow(SwitchOnFreshnessCliff());
367 SetPositionFix(cell(), 5.676731, 139.629385, 1000);
368 CheckLastPositionInfo(5.676731, 139.629385, 1000);
369
370 // 15 secs later, step outside. Switches to gps signal.
371 AdvanceTimeNow(base::TimeDelta::FromSeconds(15));
372 SetPositionFix(gps(), 3.5676457, 139.629198, 50);
373 CheckLastPositionInfo(3.5676457, 139.629198, 50);
374
375 // 5 mins later switch cells while walking. Stay on gps.
376 AdvanceTimeNow(base::TimeDelta::FromMinutes(5));
377 SetPositionFix(cell(), 3.567832, 139.634648, 300);
378 SetPositionFix(gps(), 3.5677675, 139.632314, 50);
379 CheckLastPositionInfo(3.5677675, 139.632314, 50);
380
381 // Ride train and gps signal degrades slightly. Stay on fresher gps
382 AdvanceTimeNow(base::TimeDelta::FromMinutes(5));
383 SetPositionFix(gps(), 3.5679026, 139.634777, 300);
384 CheckLastPositionInfo(3.5679026, 139.634777, 300);
385
386 // 14 minutes later
387 AdvanceTimeNow(base::TimeDelta::FromMinutes(14));
388
389 // GPS reading misses a beat, but don't switch to cell yet to avoid
390 // oscillating.
391 SetPositionFix(gps(), 3.5659005, 139.682579, 300);
392
393 AdvanceTimeNow(base::TimeDelta::FromSeconds(7));
394 SetPositionFix(cell(), 3.5689579, 139.691420, 1000);
395 CheckLastPositionInfo(3.5659005, 139.682579, 300);
396
397 // 1 minute later
398 AdvanceTimeNow(base::TimeDelta::FromMinutes(1));
399
400 // Enter tunnel. Stay on fresher gps for a moment.
401 SetPositionFix(cell(), 3.5657078, 139.68922, 300);
402 SetPositionFix(gps(), 3.5657104, 139.690341, 300);
403 CheckLastPositionInfo(3.5657104, 139.690341, 300);
404
405 // 2 minutes later
406 AdvanceTimeNow(base::TimeDelta::FromMinutes(2));
407 // Arrive in station. Cell moves but GPS is stale. Switch to fresher cell.
408 SetPositionFix(cell(), 3.5658700, 139.069979, 1000);
409 CheckLastPositionInfo(3.5658700, 139.069979, 1000);
410 }
411
412 TEST_F(GeolocationLocationArbitratorTest, TwoOneShotsIsNewPositionBetter) {
413 InitializeArbitrator(nullptr);
414 arbitrator_->StartProvider(false);
415 access_token_store_->NotifyDelegateTokensLoaded();
416 ASSERT_TRUE(cell());
417 ASSERT_TRUE(gps());
418
419 // Set the initial position.
420 SetPositionFix(cell(), 3, 139, 100);
421 CheckLastPositionInfo(3, 139, 100);
422
423 // Restart providers to simulate a one-shot request.
424 arbitrator_->StopProvider();
425
426 // To test 240956, perform a throwaway alloc.
427 // This convinces the allocator to put the providers in a new memory location.
428 std::unique_ptr<FakeLocationProvider> dummy_provider(
429 new FakeLocationProvider);
430
431 arbitrator_->StartProvider(false);
432 access_token_store_->NotifyDelegateTokensLoaded();
433
434 // Advance the time a short while to simulate successive calls.
435 AdvanceTimeNow(base::TimeDelta::FromMilliseconds(5));
436
437 // Update with a less accurate position to verify 240956.
438 SetPositionFix(cell(), 3, 139, 150);
439 CheckLastPositionInfo(3, 139, 150);
440 }
441
442 } // namespace device
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698