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.
6 #include "base/memory/scoped_ptr.h"
7 #include "content/browser/geolocation/fake_access_token_store.h"
8 #include "content/browser/geolocation/location_arbitrator_impl.h"
9 #include "content/browser/geolocation/mock_location_provider.h"
10 #include "content/public/common/geoposition.h"
11 #include "testing/gmock/include/gmock/gmock.h"
12 #include "testing/gtest/include/gtest/gtest.h"
14 using ::testing::NiceMock
;
18 class MockLocationObserver
{
20 // Need a vtable for GMock.
21 virtual ~MockLocationObserver() {}
22 void InvalidateLastPosition() {
23 last_position_
.latitude
= 100;
24 last_position_
.error_code
= Geoposition::ERROR_CODE_NONE
;
25 ASSERT_FALSE(last_position_
.Validate());
28 void OnLocationUpdate(const Geoposition
& position
) {
29 last_position_
= position
;
32 Geoposition last_position_
;
35 double g_fake_time_now_secs
= 1;
37 base::Time
GetTimeNowForTest() {
38 return base::Time::FromDoubleT(g_fake_time_now_secs
);
41 void AdvanceTimeNow(const base::TimeDelta
& delta
) {
42 g_fake_time_now_secs
+= delta
.InSecondsF();
45 void SetPositionFix(MockLocationProvider
* provider
,
50 position
.error_code
= Geoposition::ERROR_CODE_NONE
;
51 position
.latitude
= latitude
;
52 position
.longitude
= longitude
;
53 position
.accuracy
= accuracy
;
54 position
.timestamp
= GetTimeNowForTest();
55 ASSERT_TRUE(position
.Validate());
56 provider
->HandlePositionChanged(position
);
59 void SetReferencePosition(MockLocationProvider
* provider
) {
60 SetPositionFix(provider
, 51.0, -0.1, 400);
65 class TestingLocationArbitrator
: public LocationArbitratorImpl
{
67 TestingLocationArbitrator(
68 const LocationArbitratorImpl::LocationUpdateCallback
& callback
,
69 AccessTokenStore
* access_token_store
)
70 : LocationArbitratorImpl(callback
),
73 access_token_store_(access_token_store
) {
76 base::Time
GetTimeNow() const override
{ return GetTimeNowForTest(); }
78 AccessTokenStore
* NewAccessTokenStore() override
{
79 return access_token_store_
.get();
82 LocationProvider
* NewNetworkLocationProvider(
83 AccessTokenStore
* access_token_store
,
84 net::URLRequestContextGetter
* context
,
86 const base::string16
& access_token
) override
{
87 return new MockLocationProvider(&cell_
);
90 LocationProvider
* NewSystemLocationProvider() override
{
91 return new MockLocationProvider(&gps_
);
94 // Two location providers, with nice short names to make the tests more
95 // readable. Note |gps_| will only be set when there is a high accuracy
96 // observer registered (and |cell_| when there's at least one observer of any
98 MockLocationProvider
* cell_
;
99 MockLocationProvider
* gps_
;
100 scoped_refptr
<AccessTokenStore
> access_token_store_
;
105 class GeolocationLocationArbitratorTest
: public testing::Test
{
108 virtual void SetUp() {
109 access_token_store_
= new NiceMock
<FakeAccessTokenStore
>;
110 observer_
.reset(new MockLocationObserver
);
111 LocationArbitratorImpl::LocationUpdateCallback callback
=
112 base::Bind(&MockLocationObserver::OnLocationUpdate
,
113 base::Unretained(observer_
.get()));
114 arbitrator_
.reset(new TestingLocationArbitrator(
115 callback
, access_token_store_
.get()));
119 virtual void TearDown() {
122 void CheckLastPositionInfo(double latitude
,
125 Geoposition geoposition
= observer_
->last_position_
;
126 EXPECT_TRUE(geoposition
.Validate());
127 EXPECT_DOUBLE_EQ(latitude
, geoposition
.latitude
);
128 EXPECT_DOUBLE_EQ(longitude
, geoposition
.longitude
);
129 EXPECT_DOUBLE_EQ(accuracy
, geoposition
.accuracy
);
132 base::TimeDelta
SwitchOnFreshnessCliff() {
133 // Add 1, to ensure it meets any greater-than test.
134 return base::TimeDelta::FromMilliseconds(
135 LocationArbitratorImpl::kFixStaleTimeoutMilliseconds
+ 1);
138 MockLocationProvider
* cell() {
139 return arbitrator_
->cell_
;
142 MockLocationProvider
* gps() {
143 return arbitrator_
->gps_
;
146 scoped_refptr
<FakeAccessTokenStore
> access_token_store_
;
147 scoped_ptr
<MockLocationObserver
> observer_
;
148 scoped_ptr
<TestingLocationArbitrator
> arbitrator_
;
149 base::MessageLoop loop_
;
152 TEST_F(GeolocationLocationArbitratorTest
, CreateDestroy
) {
153 EXPECT_TRUE(access_token_store_
.get());
154 EXPECT_TRUE(arbitrator_
!= NULL
);
159 TEST_F(GeolocationLocationArbitratorTest
, OnPermissionGranted
) {
160 EXPECT_FALSE(arbitrator_
->HasPermissionBeenGranted());
161 arbitrator_
->OnPermissionGranted();
162 EXPECT_TRUE(arbitrator_
->HasPermissionBeenGranted());
163 // Can't check the provider has been notified without going through the
164 // motions to create the provider (see next test).
165 EXPECT_FALSE(cell());
169 TEST_F(GeolocationLocationArbitratorTest
, NormalUsage
) {
170 ASSERT_TRUE(access_token_store_
.get());
171 ASSERT_TRUE(arbitrator_
!= NULL
);
173 EXPECT_FALSE(cell());
175 arbitrator_
->StartProviders(false);
177 EXPECT_TRUE(access_token_store_
->access_token_set_
.empty());
178 EXPECT_TRUE(access_token_store_
->access_token_set_
.empty());
180 access_token_store_
->NotifyDelegateTokensLoaded();
183 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY
, cell()->state_
);
184 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY
, gps()->state_
);
185 EXPECT_FALSE(observer_
->last_position_
.Validate());
186 EXPECT_EQ(Geoposition::ERROR_CODE_NONE
,
187 observer_
->last_position_
.error_code
);
189 SetReferencePosition(cell());
191 EXPECT_TRUE(observer_
->last_position_
.Validate() ||
192 observer_
->last_position_
.error_code
!=
193 Geoposition::ERROR_CODE_NONE
);
194 EXPECT_EQ(cell()->position_
.latitude
,
195 observer_
->last_position_
.latitude
);
197 EXPECT_FALSE(cell()->is_permission_granted_
);
198 EXPECT_FALSE(arbitrator_
->HasPermissionBeenGranted());
199 arbitrator_
->OnPermissionGranted();
200 EXPECT_TRUE(arbitrator_
->HasPermissionBeenGranted());
201 EXPECT_TRUE(cell()->is_permission_granted_
);
204 TEST_F(GeolocationLocationArbitratorTest
, SetObserverOptions
) {
205 arbitrator_
->StartProviders(false);
206 access_token_store_
->NotifyDelegateTokensLoaded();
209 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY
, cell()->state_
);
210 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY
, gps()->state_
);
211 SetReferencePosition(cell());
212 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY
, cell()->state_
);
213 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY
, gps()->state_
);
214 arbitrator_
->StartProviders(true);
215 EXPECT_EQ(MockLocationProvider::HIGH_ACCURACY
, cell()->state_
);
216 EXPECT_EQ(MockLocationProvider::HIGH_ACCURACY
, gps()->state_
);
219 TEST_F(GeolocationLocationArbitratorTest
, Arbitration
) {
220 arbitrator_
->StartProviders(false);
221 access_token_store_
->NotifyDelegateTokensLoaded();
225 SetPositionFix(cell(), 1, 2, 150);
227 // First position available
228 EXPECT_TRUE(observer_
->last_position_
.Validate());
229 CheckLastPositionInfo(1, 2, 150);
231 SetPositionFix(gps(), 3, 4, 50);
233 // More accurate fix available
234 CheckLastPositionInfo(3, 4, 50);
236 SetPositionFix(cell(), 5, 6, 150);
238 // New fix is available but it's less accurate, older fix should be kept.
239 CheckLastPositionInfo(3, 4, 50);
241 // Advance time, and notify once again
242 AdvanceTimeNow(SwitchOnFreshnessCliff());
243 cell()->HandlePositionChanged(cell()->position_
);
245 // New fix is available, less accurate but fresher
246 CheckLastPositionInfo(5, 6, 150);
248 // Advance time, and set a low accuracy position
249 AdvanceTimeNow(SwitchOnFreshnessCliff());
250 SetPositionFix(cell(), 5.676731, 139.629385, 1000);
251 CheckLastPositionInfo(5.676731, 139.629385, 1000);
253 // 15 secs later, step outside. Switches to gps signal.
254 AdvanceTimeNow(base::TimeDelta::FromSeconds(15));
255 SetPositionFix(gps(), 3.5676457, 139.629198, 50);
256 CheckLastPositionInfo(3.5676457, 139.629198, 50);
258 // 5 mins later switch cells while walking. Stay on gps.
259 AdvanceTimeNow(base::TimeDelta::FromMinutes(5));
260 SetPositionFix(cell(), 3.567832, 139.634648, 300);
261 SetPositionFix(gps(), 3.5677675, 139.632314, 50);
262 CheckLastPositionInfo(3.5677675, 139.632314, 50);
264 // Ride train and gps signal degrades slightly. Stay on fresher gps
265 AdvanceTimeNow(base::TimeDelta::FromMinutes(5));
266 SetPositionFix(gps(), 3.5679026, 139.634777, 300);
267 CheckLastPositionInfo(3.5679026, 139.634777, 300);
270 AdvanceTimeNow(base::TimeDelta::FromMinutes(14));
272 // GPS reading misses a beat, but don't switch to cell yet to avoid
274 SetPositionFix(gps(), 3.5659005, 139.682579, 300);
276 AdvanceTimeNow(base::TimeDelta::FromSeconds(7));
277 SetPositionFix(cell(), 3.5689579, 139.691420, 1000);
278 CheckLastPositionInfo(3.5659005, 139.682579, 300);
281 AdvanceTimeNow(base::TimeDelta::FromMinutes(1));
283 // Enter tunnel. Stay on fresher gps for a moment.
284 SetPositionFix(cell(), 3.5657078, 139.68922, 300);
285 SetPositionFix(gps(), 3.5657104, 139.690341, 300);
286 CheckLastPositionInfo(3.5657104, 139.690341, 300);
289 AdvanceTimeNow(base::TimeDelta::FromMinutes(2));
290 // Arrive in station. Cell moves but GPS is stale. Switch to fresher cell.
291 SetPositionFix(cell(), 3.5658700, 139.069979, 1000);
292 CheckLastPositionInfo(3.5658700, 139.069979, 1000);
295 TEST_F(GeolocationLocationArbitratorTest
, TwoOneShotsIsNewPositionBetter
) {
296 arbitrator_
->StartProviders(false);
297 access_token_store_
->NotifyDelegateTokensLoaded();
301 // Set the initial position.
302 SetPositionFix(cell(), 3, 139, 100);
303 CheckLastPositionInfo(3, 139, 100);
305 // Restart providers to simulate a one-shot request.
306 arbitrator_
->StopProviders();
308 // To test 240956, perform a throwaway alloc.
309 // This convinces the allocator to put the providers in a new memory location.
310 MockLocationProvider
* fakeMockProvider
= NULL
;
311 LocationProvider
* fakeProvider
=
312 new MockLocationProvider(&fakeMockProvider
);
314 arbitrator_
->StartProviders(false);
315 access_token_store_
->NotifyDelegateTokensLoaded();
317 // Advance the time a short while to simulate successive calls.
318 AdvanceTimeNow(base::TimeDelta::FromMilliseconds(5));
320 // Update with a less accurate position to verify 240956.
321 SetPositionFix(cell(), 3, 139, 150);
322 CheckLastPositionInfo(3, 139, 150);
324 // No delete required for fakeMockProvider. It points to fakeProvider.
328 } // namespace content