| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2010 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 "base/scoped_ptr.h" | |
| 6 #include "chrome/common/geoposition.h" | |
| 7 #include "content/browser/geolocation/arbitrator_dependency_factory.h" | |
| 8 #include "content/browser/geolocation/fake_access_token_store.h" | |
| 9 #include "content/browser/geolocation/geolocation_observer.h" | |
| 10 #include "content/browser/geolocation/location_arbitrator.h" | |
| 11 #include "content/browser/geolocation/location_provider.h" | |
| 12 #include "content/browser/geolocation/mock_location_provider.h" | |
| 13 #include "testing/gtest/include/gtest/gtest.h" | |
| 14 | |
| 15 namespace { | |
| 16 | |
| 17 class MockLocationObserver : public GeolocationObserver { | |
| 18 public: | |
| 19 void InvalidateLastPosition() { | |
| 20 last_position_.latitude = 100; | |
| 21 last_position_.error_code = Geoposition::ERROR_CODE_NONE; | |
| 22 ASSERT_FALSE(last_position_.IsInitialized()); | |
| 23 } | |
| 24 // Delegate | |
| 25 virtual void OnLocationUpdate(const Geoposition& position) { | |
| 26 last_position_ = position; | |
| 27 } | |
| 28 | |
| 29 Geoposition last_position_; | |
| 30 }; | |
| 31 | |
| 32 double g_fake_time_now_secs = 1; | |
| 33 | |
| 34 base::Time GetTimeNowForTest() { | |
| 35 return base::Time::FromDoubleT(g_fake_time_now_secs); | |
| 36 } | |
| 37 | |
| 38 void AdvanceTimeNow(const base::TimeDelta& delta) { | |
| 39 g_fake_time_now_secs += delta.InSecondsF(); | |
| 40 } | |
| 41 | |
| 42 void SetPositionFix(MockLocationProvider* provider, | |
| 43 double latitude, | |
| 44 double longitude, | |
| 45 double accuracy) { | |
| 46 Geoposition position; | |
| 47 position.error_code = Geoposition::ERROR_CODE_NONE; | |
| 48 position.latitude = latitude; | |
| 49 position.longitude = longitude; | |
| 50 position.accuracy = accuracy; | |
| 51 position.timestamp = GetTimeNowForTest(); | |
| 52 ASSERT_TRUE(position.IsInitialized()); | |
| 53 ASSERT_TRUE(position.IsValidFix()); | |
| 54 provider->HandlePositionChanged(position); | |
| 55 } | |
| 56 | |
| 57 void SetReferencePosition(MockLocationProvider* provider) { | |
| 58 SetPositionFix(provider, 51.0, -0.1, 400); | |
| 59 } | |
| 60 | |
| 61 class MockDependencyFactory : public GeolocationArbitratorDependencyFactory { | |
| 62 public: | |
| 63 explicit MockDependencyFactory(AccessTokenStore* access_token_store) | |
| 64 : cell_(NULL), | |
| 65 gps_(NULL), | |
| 66 access_token_store_(access_token_store) { | |
| 67 } | |
| 68 virtual GeolocationArbitrator::GetTimeNow GetTimeFunction() { | |
| 69 return GetTimeNowForTest; | |
| 70 } | |
| 71 virtual URLRequestContextGetter* GetContextGetter() { | |
| 72 return NULL; | |
| 73 } | |
| 74 virtual AccessTokenStore* NewAccessTokenStore() { | |
| 75 return access_token_store_.get(); | |
| 76 } | |
| 77 virtual LocationProviderBase* NewNetworkLocationProvider( | |
| 78 AccessTokenStore* access_token_store, | |
| 79 URLRequestContextGetter* context, | |
| 80 const GURL& url, | |
| 81 const string16& access_token) { | |
| 82 return new MockLocationProvider(&cell_); | |
| 83 } | |
| 84 virtual LocationProviderBase* NewSystemLocationProvider() { | |
| 85 return new MockLocationProvider(&gps_); | |
| 86 } | |
| 87 | |
| 88 // Two location providers, with nice short names to make the tests more | |
| 89 // readable. Note |gps_| will only be set when there is a high accuracy | |
| 90 // observer registered (and |cell_| when there's at least one observer of any | |
| 91 // type). | |
| 92 MockLocationProvider* cell_; | |
| 93 MockLocationProvider* gps_; | |
| 94 | |
| 95 scoped_refptr<AccessTokenStore> access_token_store_; | |
| 96 }; | |
| 97 | |
| 98 class GeolocationLocationArbitratorTest : public testing::Test { | |
| 99 protected: | |
| 100 // testing::Test | |
| 101 virtual void SetUp() { | |
| 102 access_token_store_ = new FakeAccessTokenStore; | |
| 103 observer_.reset(new MockLocationObserver); | |
| 104 dependency_factory_ = new MockDependencyFactory(access_token_store_); | |
| 105 GeolocationArbitrator::SetDependencyFactoryForTest( | |
| 106 dependency_factory_.get()); | |
| 107 arbitrator_.reset(GeolocationArbitrator::Create(observer_.get())); | |
| 108 } | |
| 109 | |
| 110 // testing::Test | |
| 111 virtual void TearDown() { | |
| 112 GeolocationArbitrator::SetDependencyFactoryForTest(NULL); | |
| 113 dependency_factory_ = NULL; | |
| 114 } | |
| 115 | |
| 116 void CheckLastPositionInfo(double latitude, | |
| 117 double longitude, | |
| 118 double accuracy) { | |
| 119 Geoposition geoposition = observer_->last_position_; | |
| 120 EXPECT_TRUE(geoposition.IsValidFix()); | |
| 121 EXPECT_DOUBLE_EQ(latitude, geoposition.latitude); | |
| 122 EXPECT_DOUBLE_EQ(longitude, geoposition.longitude); | |
| 123 EXPECT_DOUBLE_EQ(accuracy, geoposition.accuracy); | |
| 124 } | |
| 125 | |
| 126 base::TimeDelta SwitchOnFreshnessCliff() { | |
| 127 // Add 1, to ensure it meets any greater-than test. | |
| 128 return base::TimeDelta::FromMilliseconds( | |
| 129 GeolocationArbitrator::kFixStaleTimeoutMilliseconds + 1); | |
| 130 } | |
| 131 | |
| 132 MockLocationProvider* cell() { | |
| 133 return dependency_factory_->cell_; | |
| 134 } | |
| 135 | |
| 136 MockLocationProvider* gps() { | |
| 137 return dependency_factory_->gps_; | |
| 138 } | |
| 139 | |
| 140 scoped_refptr<FakeAccessTokenStore> access_token_store_; | |
| 141 scoped_refptr<MockDependencyFactory> dependency_factory_; | |
| 142 scoped_ptr<MockLocationObserver> observer_; | |
| 143 scoped_ptr<GeolocationArbitrator> arbitrator_; | |
| 144 MessageLoop loop_; | |
| 145 }; | |
| 146 | |
| 147 TEST_F(GeolocationLocationArbitratorTest, CreateDestroy) { | |
| 148 EXPECT_TRUE(access_token_store_); | |
| 149 EXPECT_TRUE(arbitrator_ != NULL); | |
| 150 arbitrator_.reset(); | |
| 151 SUCCEED(); | |
| 152 } | |
| 153 | |
| 154 TEST_F(GeolocationLocationArbitratorTest, OnPermissionGranted) { | |
| 155 EXPECT_FALSE(arbitrator_->HasPermissionBeenGranted()); | |
| 156 arbitrator_->OnPermissionGranted(GURL("http://frame.test")); | |
| 157 EXPECT_TRUE(arbitrator_->HasPermissionBeenGranted()); | |
| 158 // Can't check the provider has been notified without going through the | |
| 159 // motions to create the provider (see next test). | |
| 160 EXPECT_FALSE(cell()); | |
| 161 EXPECT_FALSE(gps()); | |
| 162 } | |
| 163 | |
| 164 TEST_F(GeolocationLocationArbitratorTest, NormalUsage) { | |
| 165 ASSERT_TRUE(access_token_store_); | |
| 166 ASSERT_TRUE(arbitrator_ != NULL); | |
| 167 | |
| 168 EXPECT_TRUE(access_token_store_->access_token_set_.empty()); | |
| 169 EXPECT_TRUE(access_token_store_->request_); | |
| 170 EXPECT_TRUE(access_token_store_->access_token_set_.empty()); | |
| 171 ASSERT_TRUE(access_token_store_->request_); | |
| 172 | |
| 173 EXPECT_FALSE(cell()); | |
| 174 EXPECT_FALSE(gps()); | |
| 175 access_token_store_->NotifyDelegateTokensLoaded(); | |
| 176 ASSERT_TRUE(cell()); | |
| 177 EXPECT_TRUE(gps()); | |
| 178 EXPECT_TRUE(cell()->has_listeners()); | |
| 179 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, cell()->state_); | |
| 180 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, gps()->state_); | |
| 181 EXPECT_FALSE(observer_->last_position_.IsInitialized()); | |
| 182 | |
| 183 SetReferencePosition(cell()); | |
| 184 | |
| 185 EXPECT_TRUE(observer_->last_position_.IsInitialized()); | |
| 186 EXPECT_EQ(cell()->position_.latitude, | |
| 187 observer_->last_position_.latitude); | |
| 188 | |
| 189 EXPECT_FALSE( | |
| 190 cell()->permission_granted_url_.is_valid()); | |
| 191 EXPECT_FALSE(arbitrator_->HasPermissionBeenGranted()); | |
| 192 GURL frame_url("http://frame.test"); | |
| 193 arbitrator_->OnPermissionGranted(frame_url); | |
| 194 EXPECT_TRUE(arbitrator_->HasPermissionBeenGranted()); | |
| 195 EXPECT_TRUE( | |
| 196 cell()->permission_granted_url_.is_valid()); | |
| 197 EXPECT_EQ(frame_url, | |
| 198 cell()->permission_granted_url_); | |
| 199 } | |
| 200 | |
| 201 TEST_F(GeolocationLocationArbitratorTest, SetObserverOptions) { | |
| 202 access_token_store_->NotifyDelegateTokensLoaded(); | |
| 203 ASSERT_TRUE(cell()); | |
| 204 ASSERT_TRUE(gps()); | |
| 205 arbitrator_->StartProviders(GeolocationObserverOptions(true)); | |
| 206 EXPECT_EQ(MockLocationProvider::HIGH_ACCURACY, cell()->state_); | |
| 207 EXPECT_EQ(MockLocationProvider::HIGH_ACCURACY, gps()->state_); | |
| 208 arbitrator_->StartProviders(GeolocationObserverOptions(false)); | |
| 209 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, cell()->state_); | |
| 210 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, gps()->state_); | |
| 211 } | |
| 212 | |
| 213 TEST_F(GeolocationLocationArbitratorTest, StartProvidersAfterFixArrives) { | |
| 214 access_token_store_->NotifyDelegateTokensLoaded(); | |
| 215 ASSERT_TRUE(cell()); | |
| 216 ASSERT_TRUE(gps()); | |
| 217 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, cell()->state_); | |
| 218 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, gps()->state_); | |
| 219 SetReferencePosition(cell()); | |
| 220 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, cell()->state_); | |
| 221 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, gps()->state_); | |
| 222 arbitrator_->StartProviders(GeolocationObserverOptions(true)); | |
| 223 EXPECT_EQ(MockLocationProvider::HIGH_ACCURACY, cell()->state_); | |
| 224 EXPECT_EQ(MockLocationProvider::HIGH_ACCURACY, gps()->state_); | |
| 225 } | |
| 226 | |
| 227 TEST_F(GeolocationLocationArbitratorTest, Arbitration) { | |
| 228 access_token_store_->NotifyDelegateTokensLoaded(); | |
| 229 ASSERT_TRUE(cell()); | |
| 230 ASSERT_TRUE(gps()); | |
| 231 | |
| 232 SetPositionFix(cell(), 1, 2, 150); | |
| 233 | |
| 234 // First position available | |
| 235 EXPECT_TRUE(observer_->last_position_.IsValidFix()); | |
| 236 CheckLastPositionInfo(1, 2, 150); | |
| 237 | |
| 238 SetPositionFix(gps(), 3, 4, 50); | |
| 239 | |
| 240 // More accurate fix available | |
| 241 CheckLastPositionInfo(3, 4, 50); | |
| 242 | |
| 243 SetPositionFix(cell(), 5, 6, 150); | |
| 244 | |
| 245 // New fix is available but it's less accurate, older fix should be kept. | |
| 246 CheckLastPositionInfo(3, 4, 50); | |
| 247 | |
| 248 // Advance time, and notify once again | |
| 249 AdvanceTimeNow(SwitchOnFreshnessCliff()); | |
| 250 cell()->HandlePositionChanged(cell()->position_); | |
| 251 | |
| 252 // New fix is available, less accurate but fresher | |
| 253 CheckLastPositionInfo(5, 6, 150); | |
| 254 | |
| 255 // Advance time, and set a low accuracy position | |
| 256 AdvanceTimeNow(SwitchOnFreshnessCliff()); | |
| 257 SetPositionFix(cell(), 5.676731, 139.629385, 1000); | |
| 258 CheckLastPositionInfo(5.676731, 139.629385, 1000); | |
| 259 | |
| 260 // 15 secs later, step outside. Switches to gps signal. | |
| 261 AdvanceTimeNow(base::TimeDelta::FromSeconds(15)); | |
| 262 SetPositionFix(gps(), 3.5676457, 139.629198, 50); | |
| 263 CheckLastPositionInfo(3.5676457, 139.629198, 50); | |
| 264 | |
| 265 // 5 mins later switch cells while walking. Stay on gps. | |
| 266 AdvanceTimeNow(base::TimeDelta::FromMinutes(5)); | |
| 267 SetPositionFix(cell(), 3.567832, 139.634648, 300); | |
| 268 SetPositionFix(gps(), 3.5677675, 139.632314, 50); | |
| 269 CheckLastPositionInfo(3.5677675, 139.632314, 50); | |
| 270 | |
| 271 // Ride train and gps signal degrades slightly. Stay on fresher gps | |
| 272 AdvanceTimeNow(base::TimeDelta::FromMinutes(5)); | |
| 273 SetPositionFix(gps(), 3.5679026, 139.634777, 300); | |
| 274 CheckLastPositionInfo(3.5679026, 139.634777, 300); | |
| 275 | |
| 276 // 14 minutes later | |
| 277 AdvanceTimeNow(base::TimeDelta::FromMinutes(14)); | |
| 278 | |
| 279 // GPS reading misses a beat, but don't switch to cell yet to avoid | |
| 280 // oscillating. | |
| 281 SetPositionFix(gps(), 3.5659005, 139.682579, 300); | |
| 282 | |
| 283 AdvanceTimeNow(base::TimeDelta::FromSeconds(7)); | |
| 284 SetPositionFix(cell(), 3.5689579, 139.691420, 1000); | |
| 285 CheckLastPositionInfo(3.5659005, 139.682579, 300); | |
| 286 | |
| 287 // 1 minute later | |
| 288 AdvanceTimeNow(base::TimeDelta::FromMinutes(1)); | |
| 289 | |
| 290 // Enter tunnel. Stay on fresher gps for a moment. | |
| 291 SetPositionFix(cell(), 3.5657078, 139.68922, 300); | |
| 292 SetPositionFix(gps(), 3.5657104, 139.690341, 300); | |
| 293 CheckLastPositionInfo(3.5657104, 139.690341, 300); | |
| 294 | |
| 295 // 2 minutes later | |
| 296 AdvanceTimeNow(base::TimeDelta::FromMinutes(2)); | |
| 297 // Arrive in station. Cell moves but GPS is stale. Switch to fresher cell. | |
| 298 SetPositionFix(cell(), 3.5658700, 139.069979, 1000); | |
| 299 CheckLastPositionInfo(3.5658700, 139.069979, 1000); | |
| 300 } | |
| 301 | |
| 302 } // namespace | |
| OLD | NEW |