Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 "content/browser/geolocation/location_arbitrator_impl.h" | 5 #include "content/browser/geolocation/location_arbitrator_impl.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "content/browser/geolocation/fake_access_token_store.h" | 10 #include "content/browser/geolocation/fake_access_token_store.h" |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 65 namespace { | 65 namespace { |
| 66 | 66 |
| 67 class TestingLocationArbitrator : public LocationArbitratorImpl { | 67 class TestingLocationArbitrator : public LocationArbitratorImpl { |
| 68 public: | 68 public: |
| 69 TestingLocationArbitrator( | 69 TestingLocationArbitrator( |
| 70 const LocationArbitratorImpl::LocationUpdateCallback& callback, | 70 const LocationArbitratorImpl::LocationUpdateCallback& callback, |
| 71 AccessTokenStore* access_token_store) | 71 AccessTokenStore* access_token_store) |
| 72 : LocationArbitratorImpl(callback), | 72 : LocationArbitratorImpl(callback), |
| 73 cell_(NULL), | 73 cell_(NULL), |
| 74 gps_(NULL), | 74 gps_(NULL), |
| 75 override_(NULL), | |
|
Kevin M
2016/06/08 20:47:25
nit: use nullptr instead of NULL now, and you can
CJ
2016/06/09 01:02:18
Done.
| |
| 75 access_token_store_(access_token_store) { | 76 access_token_store_(access_token_store) { |
| 76 } | 77 } |
| 77 | 78 |
| 78 base::Time GetTimeNow() const override { return GetTimeNowForTest(); } | 79 base::Time GetTimeNow() const override { return GetTimeNowForTest(); } |
| 79 | 80 |
| 80 AccessTokenStore* NewAccessTokenStore() override { | 81 AccessTokenStore* NewAccessTokenStore() override { |
| 81 return access_token_store_.get(); | 82 return access_token_store_.get(); |
| 82 } | 83 } |
| 83 | 84 |
| 84 LocationProvider* NewNetworkLocationProvider( | 85 LocationProvider* NewNetworkLocationProvider( |
| 85 AccessTokenStore* access_token_store, | 86 AccessTokenStore* access_token_store, |
| 86 net::URLRequestContextGetter* context, | 87 net::URLRequestContextGetter* context, |
| 87 const GURL& url, | 88 const GURL& url, |
| 88 const base::string16& access_token) override { | 89 const base::string16& access_token) override { |
| 89 return new MockLocationProvider(&cell_); | 90 return new MockLocationProvider(&cell_); |
| 90 } | 91 } |
| 91 | 92 |
| 92 LocationProvider* NewSystemLocationProvider() override { | 93 LocationProvider* NewSystemLocationProvider() override { |
| 93 return new MockLocationProvider(&gps_); | 94 return new MockLocationProvider(&gps_); |
| 94 } | 95 } |
| 95 | 96 |
| 96 // Two location providers, with nice short names to make the tests more | 97 LocationProvider* GetOverrideSystemLocationProvider() override { |
| 98 if (use_override_) { | |
| 99 return new MockLocationProvider(&override_); | |
| 100 } | |
| 101 return nullptr; | |
| 102 } | |
| 103 | |
| 104 // Three location providers, with nice short names to make the tests more | |
| 97 // readable. Note |gps_| will only be set when there is a high accuracy | 105 // readable. Note |gps_| will only be set when there is a high accuracy |
| 98 // observer registered (and |cell_| when there's at least one observer of any | 106 // observer registered (and |cell_| when there's at least one observer of any |
| 99 // type). | 107 // type). |override_| will be included only if use_network is false. |
| 100 MockLocationProvider* cell_; | 108 MockLocationProvider* cell_; |
| 101 MockLocationProvider* gps_; | 109 MockLocationProvider* gps_; |
| 110 MockLocationProvider* override_; | |
| 111 bool use_override_ = false; | |
| 102 scoped_refptr<AccessTokenStore> access_token_store_; | 112 scoped_refptr<AccessTokenStore> access_token_store_; |
| 103 }; | 113 }; |
| 104 | 114 |
| 105 } // namespace | 115 } // namespace |
| 106 | 116 |
| 107 class GeolocationLocationArbitratorTest : public testing::Test { | 117 class GeolocationLocationArbitratorTest : public testing::Test { |
| 108 protected: | 118 protected: |
| 109 // testing::Test | 119 // testing::Test |
| 110 void SetUp() override { | 120 void SetUp() override { |
| 111 access_token_store_ = new NiceMock<FakeAccessTokenStore>; | 121 access_token_store_ = new NiceMock<FakeAccessTokenStore>; |
| (...skipping 25 matching lines...) Expand all Loading... | |
| 137 } | 147 } |
| 138 | 148 |
| 139 MockLocationProvider* cell() { | 149 MockLocationProvider* cell() { |
| 140 return arbitrator_->cell_; | 150 return arbitrator_->cell_; |
| 141 } | 151 } |
| 142 | 152 |
| 143 MockLocationProvider* gps() { | 153 MockLocationProvider* gps() { |
| 144 return arbitrator_->gps_; | 154 return arbitrator_->gps_; |
| 145 } | 155 } |
| 146 | 156 |
| 157 MockLocationProvider* overrider() { | |
|
Kevin M
2016/06/08 20:47:25
"override"?
CJ
2016/06/09 01:02:18
Not sure if I was allowed to use it since override
| |
| 158 return arbitrator_->override_; | |
| 159 } | |
| 160 | |
| 147 scoped_refptr<FakeAccessTokenStore> access_token_store_; | 161 scoped_refptr<FakeAccessTokenStore> access_token_store_; |
| 148 std::unique_ptr<MockLocationObserver> observer_; | 162 std::unique_ptr<MockLocationObserver> observer_; |
| 149 std::unique_ptr<TestingLocationArbitrator> arbitrator_; | 163 std::unique_ptr<TestingLocationArbitrator> arbitrator_; |
| 150 base::MessageLoop loop_; | 164 base::MessageLoop loop_; |
| 151 }; | 165 }; |
| 152 | 166 |
| 153 TEST_F(GeolocationLocationArbitratorTest, CreateDestroy) { | 167 TEST_F(GeolocationLocationArbitratorTest, CreateDestroy) { |
| 154 EXPECT_TRUE(access_token_store_.get()); | 168 EXPECT_TRUE(access_token_store_.get()); |
| 155 EXPECT_TRUE(arbitrator_ != NULL); | 169 EXPECT_TRUE(arbitrator_ != NULL); |
| 156 arbitrator_.reset(); | 170 arbitrator_.reset(); |
| 157 SUCCEED(); | 171 SUCCEED(); |
| 158 } | 172 } |
| 159 | 173 |
| 160 TEST_F(GeolocationLocationArbitratorTest, OnPermissionGranted) { | 174 TEST_F(GeolocationLocationArbitratorTest, OnPermissionGranted) { |
| 161 EXPECT_FALSE(arbitrator_->HasPermissionBeenGranted()); | 175 EXPECT_FALSE(arbitrator_->HasPermissionBeenGranted()); |
| 162 arbitrator_->OnPermissionGranted(); | 176 arbitrator_->OnPermissionGranted(); |
| 163 EXPECT_TRUE(arbitrator_->HasPermissionBeenGranted()); | 177 EXPECT_TRUE(arbitrator_->HasPermissionBeenGranted()); |
| 164 // Can't check the provider has been notified without going through the | 178 // Can't check the provider has been notified without going through the |
| 165 // motions to create the provider (see next test). | 179 // motions to create the provider (see next test). |
| 166 EXPECT_FALSE(cell()); | 180 EXPECT_FALSE(cell()); |
| 167 EXPECT_FALSE(gps()); | 181 EXPECT_FALSE(gps()); |
| 168 } | 182 } |
| 169 | 183 |
| 170 TEST_F(GeolocationLocationArbitratorTest, NormalUsage) { | 184 TEST_F(GeolocationLocationArbitratorTest, NormalUsage) { |
| 171 ASSERT_TRUE(access_token_store_.get()); | 185 ASSERT_TRUE(access_token_store_.get()); |
| 172 ASSERT_TRUE(arbitrator_ != NULL); | 186 ASSERT_TRUE(arbitrator_ != NULL); |
| 173 | 187 |
| 174 EXPECT_FALSE(cell()); | 188 EXPECT_FALSE(cell()); |
| 175 EXPECT_FALSE(gps()); | 189 EXPECT_FALSE(gps()); |
| 176 arbitrator_->StartProviders(false); | 190 arbitrator_->StartProviders(false, true); |
| 177 | 191 |
| 178 EXPECT_TRUE(access_token_store_->access_token_map_.empty()); | 192 EXPECT_TRUE(access_token_store_->access_token_map_.empty()); |
| 179 EXPECT_TRUE(access_token_store_->access_token_map_.empty()); | 193 EXPECT_TRUE(access_token_store_->access_token_map_.empty()); |
| 180 | 194 |
| 181 access_token_store_->NotifyDelegateTokensLoaded(); | 195 access_token_store_->NotifyDelegateTokensLoaded(); |
| 182 ASSERT_TRUE(cell()); | 196 ASSERT_TRUE(cell()); |
| 183 EXPECT_TRUE(gps()); | 197 EXPECT_TRUE(gps()); |
| 184 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, cell()->state_); | 198 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, cell()->state_); |
| 185 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, gps()->state_); | 199 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, gps()->state_); |
| 186 EXPECT_FALSE(observer_->last_position_.Validate()); | 200 EXPECT_FALSE(observer_->last_position_.Validate()); |
| 187 EXPECT_EQ(Geoposition::ERROR_CODE_NONE, | 201 EXPECT_EQ(Geoposition::ERROR_CODE_NONE, |
| 188 observer_->last_position_.error_code); | 202 observer_->last_position_.error_code); |
| 189 | 203 |
| 190 SetReferencePosition(cell()); | 204 SetReferencePosition(cell()); |
| 191 | 205 |
| 192 EXPECT_TRUE(observer_->last_position_.Validate() || | 206 EXPECT_TRUE(observer_->last_position_.Validate() || |
| 193 observer_->last_position_.error_code != | 207 observer_->last_position_.error_code != |
| 194 Geoposition::ERROR_CODE_NONE); | 208 Geoposition::ERROR_CODE_NONE); |
| 195 EXPECT_EQ(cell()->position_.latitude, | 209 EXPECT_EQ(cell()->position_.latitude, |
| 196 observer_->last_position_.latitude); | 210 observer_->last_position_.latitude); |
| 197 | 211 |
| 198 EXPECT_FALSE(cell()->is_permission_granted_); | 212 EXPECT_FALSE(cell()->is_permission_granted_); |
| 199 EXPECT_FALSE(arbitrator_->HasPermissionBeenGranted()); | 213 EXPECT_FALSE(arbitrator_->HasPermissionBeenGranted()); |
| 200 arbitrator_->OnPermissionGranted(); | 214 arbitrator_->OnPermissionGranted(); |
| 201 EXPECT_TRUE(arbitrator_->HasPermissionBeenGranted()); | 215 EXPECT_TRUE(arbitrator_->HasPermissionBeenGranted()); |
| 202 EXPECT_TRUE(cell()->is_permission_granted_); | 216 EXPECT_TRUE(cell()->is_permission_granted_); |
| 203 } | 217 } |
| 204 | 218 |
| 205 TEST_F(GeolocationLocationArbitratorTest, SetObserverOptions) { | 219 TEST_F(GeolocationLocationArbitratorTest, SetObserverOptions) { |
| 206 arbitrator_->StartProviders(false); | 220 arbitrator_->StartProviders(false, true); |
| 207 access_token_store_->NotifyDelegateTokensLoaded(); | 221 access_token_store_->NotifyDelegateTokensLoaded(); |
| 208 ASSERT_TRUE(cell()); | 222 ASSERT_TRUE(cell()); |
| 209 ASSERT_TRUE(gps()); | 223 ASSERT_TRUE(gps()); |
| 210 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, cell()->state_); | 224 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, cell()->state_); |
| 211 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, gps()->state_); | 225 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, gps()->state_); |
| 212 SetReferencePosition(cell()); | 226 SetReferencePosition(cell()); |
| 213 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, cell()->state_); | 227 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, cell()->state_); |
| 214 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, gps()->state_); | 228 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, gps()->state_); |
| 215 arbitrator_->StartProviders(true); | 229 arbitrator_->StartProviders(true, true); |
| 216 EXPECT_EQ(MockLocationProvider::HIGH_ACCURACY, cell()->state_); | 230 EXPECT_EQ(MockLocationProvider::HIGH_ACCURACY, cell()->state_); |
| 217 EXPECT_EQ(MockLocationProvider::HIGH_ACCURACY, gps()->state_); | 231 EXPECT_EQ(MockLocationProvider::HIGH_ACCURACY, gps()->state_); |
| 218 } | 232 } |
| 219 | 233 |
| 220 TEST_F(GeolocationLocationArbitratorTest, Arbitration) { | 234 TEST_F(GeolocationLocationArbitratorTest, Arbitration) { |
| 221 arbitrator_->StartProviders(false); | 235 arbitrator_->StartProviders(false, true); |
| 222 access_token_store_->NotifyDelegateTokensLoaded(); | 236 access_token_store_->NotifyDelegateTokensLoaded(); |
| 223 ASSERT_TRUE(cell()); | 237 ASSERT_TRUE(cell()); |
| 224 ASSERT_TRUE(gps()); | 238 ASSERT_TRUE(gps()); |
| 225 | 239 |
| 226 SetPositionFix(cell(), 1, 2, 150); | 240 SetPositionFix(cell(), 1, 2, 150); |
| 227 | 241 |
| 228 // First position available | 242 // First position available |
| 229 EXPECT_TRUE(observer_->last_position_.Validate()); | 243 EXPECT_TRUE(observer_->last_position_.Validate()); |
| 230 CheckLastPositionInfo(1, 2, 150); | 244 CheckLastPositionInfo(1, 2, 150); |
| 231 | 245 |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 287 CheckLastPositionInfo(3.5657104, 139.690341, 300); | 301 CheckLastPositionInfo(3.5657104, 139.690341, 300); |
| 288 | 302 |
| 289 // 2 minutes later | 303 // 2 minutes later |
| 290 AdvanceTimeNow(base::TimeDelta::FromMinutes(2)); | 304 AdvanceTimeNow(base::TimeDelta::FromMinutes(2)); |
| 291 // Arrive in station. Cell moves but GPS is stale. Switch to fresher cell. | 305 // Arrive in station. Cell moves but GPS is stale. Switch to fresher cell. |
| 292 SetPositionFix(cell(), 3.5658700, 139.069979, 1000); | 306 SetPositionFix(cell(), 3.5658700, 139.069979, 1000); |
| 293 CheckLastPositionInfo(3.5658700, 139.069979, 1000); | 307 CheckLastPositionInfo(3.5658700, 139.069979, 1000); |
| 294 } | 308 } |
| 295 | 309 |
| 296 TEST_F(GeolocationLocationArbitratorTest, TwoOneShotsIsNewPositionBetter) { | 310 TEST_F(GeolocationLocationArbitratorTest, TwoOneShotsIsNewPositionBetter) { |
| 297 arbitrator_->StartProviders(false); | 311 arbitrator_->StartProviders(false, true); |
| 298 access_token_store_->NotifyDelegateTokensLoaded(); | 312 access_token_store_->NotifyDelegateTokensLoaded(); |
| 299 ASSERT_TRUE(cell()); | 313 ASSERT_TRUE(cell()); |
| 300 ASSERT_TRUE(gps()); | 314 ASSERT_TRUE(gps()); |
| 301 | 315 |
| 302 // Set the initial position. | 316 // Set the initial position. |
| 303 SetPositionFix(cell(), 3, 139, 100); | 317 SetPositionFix(cell(), 3, 139, 100); |
| 304 CheckLastPositionInfo(3, 139, 100); | 318 CheckLastPositionInfo(3, 139, 100); |
| 305 | 319 |
| 306 // Restart providers to simulate a one-shot request. | 320 // Restart providers to simulate a one-shot request. |
| 307 arbitrator_->StopProviders(); | 321 arbitrator_->StopProviders(); |
| 308 | 322 |
| 309 // To test 240956, perform a throwaway alloc. | 323 // To test 240956, perform a throwaway alloc. |
| 310 // This convinces the allocator to put the providers in a new memory location. | 324 // This convinces the allocator to put the providers in a new memory location. |
| 311 MockLocationProvider* fakeMockProvider = NULL; | 325 MockLocationProvider* fakeMockProvider = NULL; |
| 312 LocationProvider* fakeProvider = | 326 LocationProvider* fakeProvider = |
| 313 new MockLocationProvider(&fakeMockProvider); | 327 new MockLocationProvider(&fakeMockProvider); |
| 314 | 328 |
| 315 arbitrator_->StartProviders(false); | 329 arbitrator_->StartProviders(false, true); |
| 316 access_token_store_->NotifyDelegateTokensLoaded(); | 330 access_token_store_->NotifyDelegateTokensLoaded(); |
| 317 | 331 |
| 318 // Advance the time a short while to simulate successive calls. | 332 // Advance the time a short while to simulate successive calls. |
| 319 AdvanceTimeNow(base::TimeDelta::FromMilliseconds(5)); | 333 AdvanceTimeNow(base::TimeDelta::FromMilliseconds(5)); |
| 320 | 334 |
| 321 // Update with a less accurate position to verify 240956. | 335 // Update with a less accurate position to verify 240956. |
| 322 SetPositionFix(cell(), 3, 139, 150); | 336 SetPositionFix(cell(), 3, 139, 150); |
| 323 CheckLastPositionInfo(3, 139, 150); | 337 CheckLastPositionInfo(3, 139, 150); |
| 324 | 338 |
| 325 // No delete required for fakeMockProvider. It points to fakeProvider. | 339 // No delete required for fakeMockProvider. It points to fakeProvider. |
| 326 delete fakeProvider; | 340 delete fakeProvider; |
| 327 } | 341 } |
| 328 | 342 |
| 329 } // namespace content | 343 } // namespace content |
| OLD | NEW |