| 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 "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 | 67 |
| 68 namespace { | 68 namespace { |
| 69 | 69 |
| 70 class FakeGeolocationDelegate : public GeolocationDelegate { | 70 class FakeGeolocationDelegate : public GeolocationDelegate { |
| 71 public: | 71 public: |
| 72 FakeGeolocationDelegate() = default; | 72 FakeGeolocationDelegate() = default; |
| 73 | 73 |
| 74 bool UseNetworkLocationProviders() override { return use_network_; } | 74 bool UseNetworkLocationProviders() override { return use_network_; } |
| 75 void set_use_network(bool use_network) { use_network_ = use_network; } | 75 void set_use_network(bool use_network) { use_network_ = use_network; } |
| 76 | 76 |
| 77 LocationProvider* OverrideSystemLocationProvider() override { | 77 std::unique_ptr<LocationProvider> OverrideSystemLocationProvider() override { |
| 78 if (!system_location_provider_) | 78 DCHECK(!mock_location_provider_); |
| 79 system_location_provider_ = base::WrapUnique(new MockLocationProvider); | 79 mock_location_provider_ = new MockLocationProvider; |
| 80 return system_location_provider_.get(); | 80 return base::WrapUnique(mock_location_provider_); |
| 81 } | 81 } |
| 82 | 82 |
| 83 LocationProvider* system_location_provider() const { | 83 MockLocationProvider* mock_location_provider() const { |
| 84 return system_location_provider_.get(); | 84 return mock_location_provider_; |
| 85 } | 85 } |
| 86 | 86 |
| 87 private: | 87 private: |
| 88 bool use_network_ = true; | 88 bool use_network_ = true; |
| 89 std::unique_ptr<LocationProvider> system_location_provider_; | 89 MockLocationProvider* mock_location_provider_ = nullptr; |
| 90 | 90 |
| 91 DISALLOW_COPY_AND_ASSIGN(FakeGeolocationDelegate); | 91 DISALLOW_COPY_AND_ASSIGN(FakeGeolocationDelegate); |
| 92 }; | 92 }; |
| 93 | 93 |
| 94 } // namespace |
| 95 |
| 94 class TestingLocationArbitrator : public LocationArbitratorImpl { | 96 class TestingLocationArbitrator : public LocationArbitratorImpl { |
| 95 public: | 97 public: |
| 96 TestingLocationArbitrator( | 98 TestingLocationArbitrator( |
| 97 const LocationArbitratorImpl::LocationUpdateCallback& callback, | 99 const LocationArbitratorImpl::LocationUpdateCallback& callback, |
| 98 AccessTokenStore* access_token_store, | 100 AccessTokenStore* access_token_store, |
| 99 GeolocationDelegate* delegate, | 101 GeolocationDelegate* delegate) |
| 100 bool is_fake_delegate) | |
| 101 : LocationArbitratorImpl(callback, delegate), | 102 : LocationArbitratorImpl(callback, delegate), |
| 102 is_fake_delegate_(is_fake_delegate), | |
| 103 cell_(nullptr), | 103 cell_(nullptr), |
| 104 gps_(nullptr), | 104 gps_(nullptr), |
| 105 access_token_store_(access_token_store) {} | 105 access_token_store_(access_token_store) {} |
| 106 | 106 |
| 107 base::Time GetTimeNow() const override { return GetTimeNowForTest(); } | 107 base::Time GetTimeNow() const override { return GetTimeNowForTest(); } |
| 108 | 108 |
| 109 AccessTokenStore* NewAccessTokenStore() override { | 109 AccessTokenStore* NewAccessTokenStore() override { |
| 110 return access_token_store_.get(); | 110 return access_token_store_.get(); |
| 111 } | 111 } |
| 112 | 112 |
| 113 std::unique_ptr<LocationProvider> NewNetworkLocationProvider( | 113 std::unique_ptr<LocationProvider> NewNetworkLocationProvider( |
| 114 AccessTokenStore* access_token_store, | 114 AccessTokenStore* access_token_store, |
| 115 net::URLRequestContextGetter* context, | 115 net::URLRequestContextGetter* context, |
| 116 const GURL& url, | 116 const GURL& url, |
| 117 const base::string16& access_token) override { | 117 const base::string16& access_token) override { |
| 118 cell_ = new MockLocationProvider; | 118 cell_ = new MockLocationProvider; |
| 119 return base::WrapUnique(cell_); | 119 return base::WrapUnique(cell_); |
| 120 } | 120 } |
| 121 | 121 |
| 122 std::unique_ptr<LocationProvider> NewSystemLocationProvider() override { | 122 std::unique_ptr<LocationProvider> NewSystemLocationProvider() override { |
| 123 gps_ = new MockLocationProvider; | 123 gps_ = new MockLocationProvider; |
| 124 return base::WrapUnique(gps_); | 124 return base::WrapUnique(gps_); |
| 125 } | 125 } |
| 126 | 126 |
| 127 FakeGeolocationDelegate* GetFakeGeolocationDelegate() { | |
| 128 DCHECK(is_fake_delegate_); | |
| 129 return static_cast<FakeGeolocationDelegate*>(GetDelegateForTesting()); | |
| 130 } | |
| 131 | |
| 132 LocationProvider* GetLocationProvider() { | |
| 133 if (is_fake_delegate_) | |
| 134 return GetFakeGeolocationDelegate()->system_location_provider(); | |
| 135 return GetDelegateForTesting()->OverrideSystemLocationProvider(); | |
| 136 } | |
| 137 | |
| 138 const bool is_fake_delegate_; | |
| 139 | |
| 140 // Two location providers, with nice short names to make the tests more | 127 // Two location providers, with nice short names to make the tests more |
| 141 // readable. Note |gps_| will only be set when there is a high accuracy | 128 // readable. Note |gps_| will only be set when there is a high accuracy |
| 142 // observer registered (and |cell_| when there's at least one observer of any | 129 // observer registered (and |cell_| when there's at least one observer of any |
| 143 // type). | 130 // type). |
| 144 | |
| 145 // TODO(mvanouwerkerk): rename |cell_| to |network_location_provider_| and | 131 // TODO(mvanouwerkerk): rename |cell_| to |network_location_provider_| and |
| 146 // |gps_| to |system_location_provider_| | 132 // |gps_| to |gps_location_provider_| |
| 147 MockLocationProvider* cell_; | 133 MockLocationProvider* cell_; |
| 148 MockLocationProvider* gps_; | 134 MockLocationProvider* gps_; |
| 135 std::unique_ptr<LocationProvider> system_location_provider_; |
| 149 scoped_refptr<AccessTokenStore> access_token_store_; | 136 scoped_refptr<AccessTokenStore> access_token_store_; |
| 150 }; | 137 }; |
| 151 | 138 |
| 152 } // namespace | |
| 153 | |
| 154 class GeolocationLocationArbitratorTest : public testing::Test { | 139 class GeolocationLocationArbitratorTest : public testing::Test { |
| 155 protected: | 140 protected: |
| 156 // testing::Test | 141 // testing::Test |
| 157 void SetUp() override { | 142 void SetUp() override { |
| 158 access_token_store_ = new NiceMock<FakeAccessTokenStore>; | 143 access_token_store_ = new NiceMock<FakeAccessTokenStore>; |
| 159 observer_.reset(new MockLocationObserver); | 144 observer_.reset(new MockLocationObserver); |
| 145 delegate_.reset(new GeolocationDelegate); |
| 160 } | 146 } |
| 161 | 147 |
| 162 // There are two types of test cases: those using FakeGeolocationDelegate and | 148 // Initializes |arbitrator_|, with the possibility of injecting a specific |
| 163 // the ones exercising whatever the embedder provides. Test cases call this | 149 // |delegate|, otherwise a default, no-op GeolocationDelegate is used. |
| 164 // method to choose the appropriate one. | 150 void InitializeArbitrator(std::unique_ptr<GeolocationDelegate> delegate) { |
| 165 void InitializeArbitrator(bool use_fake_delegate) { | 151 if (delegate) |
| 166 delegate_.reset(use_fake_delegate ? new FakeGeolocationDelegate() | 152 delegate_ = std::move(delegate); |
| 167 : new GeolocationDelegate); | |
| 168 const LocationArbitratorImpl::LocationUpdateCallback callback = | 153 const LocationArbitratorImpl::LocationUpdateCallback callback = |
| 169 base::Bind(&MockLocationObserver::OnLocationUpdate, | 154 base::Bind(&MockLocationObserver::OnLocationUpdate, |
| 170 base::Unretained(observer_.get())); | 155 base::Unretained(observer_.get())); |
| 171 arbitrator_.reset( | 156 arbitrator_.reset(new TestingLocationArbitrator( |
| 172 new TestingLocationArbitrator(callback, access_token_store_.get(), | 157 callback, access_token_store_.get(), delegate_.get())); |
| 173 delegate_.get(), use_fake_delegate)); | |
| 174 } | 158 } |
| 175 | 159 |
| 176 // testing::Test | 160 // testing::Test |
| 177 void TearDown() override {} | 161 void TearDown() override {} |
| 178 | 162 |
| 179 void CheckLastPositionInfo(double latitude, | 163 void CheckLastPositionInfo(double latitude, |
| 180 double longitude, | 164 double longitude, |
| 181 double accuracy) { | 165 double accuracy) { |
| 182 Geoposition geoposition = observer_->last_position_; | 166 Geoposition geoposition = observer_->last_position_; |
| 183 EXPECT_TRUE(geoposition.Validate()); | 167 EXPECT_TRUE(geoposition.Validate()); |
| 184 EXPECT_DOUBLE_EQ(latitude, geoposition.latitude); | 168 EXPECT_DOUBLE_EQ(latitude, geoposition.latitude); |
| 185 EXPECT_DOUBLE_EQ(longitude, geoposition.longitude); | 169 EXPECT_DOUBLE_EQ(longitude, geoposition.longitude); |
| 186 EXPECT_DOUBLE_EQ(accuracy, geoposition.accuracy); | 170 EXPECT_DOUBLE_EQ(accuracy, geoposition.accuracy); |
| 187 } | 171 } |
| 188 | 172 |
| 189 base::TimeDelta SwitchOnFreshnessCliff() { | 173 base::TimeDelta SwitchOnFreshnessCliff() { |
| 190 // Add 1, to ensure it meets any greater-than test. | 174 // Add 1, to ensure it meets any greater-than test. |
| 191 return base::TimeDelta::FromMilliseconds( | 175 return base::TimeDelta::FromMilliseconds( |
| 192 LocationArbitratorImpl::kFixStaleTimeoutMilliseconds + 1); | 176 LocationArbitratorImpl::kFixStaleTimeoutMilliseconds + 1); |
| 193 } | 177 } |
| 194 | 178 |
| 195 MockLocationProvider* cell() { | 179 MockLocationProvider* cell() { |
| 196 return arbitrator_->cell_; | 180 return arbitrator_->cell_; |
| 197 } | 181 } |
| 198 | 182 |
| 199 MockLocationProvider* gps() { | 183 MockLocationProvider* gps() { |
| 200 return arbitrator_->gps_; | 184 return arbitrator_->gps_; |
| 201 } | 185 } |
| 202 | 186 |
| 203 MockLocationProvider* GetSystemLocationProviderOverride() { | |
| 204 return static_cast<MockLocationProvider*>( | |
| 205 arbitrator_->GetLocationProvider()); | |
| 206 } | |
| 207 | |
| 208 scoped_refptr<FakeAccessTokenStore> access_token_store_; | 187 scoped_refptr<FakeAccessTokenStore> access_token_store_; |
| 209 std::unique_ptr<MockLocationObserver> observer_; | 188 std::unique_ptr<MockLocationObserver> observer_; |
| 210 std::unique_ptr<TestingLocationArbitrator> arbitrator_; | 189 std::unique_ptr<TestingLocationArbitrator> arbitrator_; |
| 211 std::unique_ptr<GeolocationDelegate> delegate_; | 190 std::unique_ptr<GeolocationDelegate> delegate_; |
| 212 base::MessageLoop loop_; | 191 base::MessageLoop loop_; |
| 213 }; | 192 }; |
| 214 | 193 |
| 215 TEST_F(GeolocationLocationArbitratorTest, CreateDestroy) { | 194 TEST_F(GeolocationLocationArbitratorTest, CreateDestroy) { |
| 216 EXPECT_TRUE(access_token_store_.get()); | 195 EXPECT_TRUE(access_token_store_.get()); |
| 217 InitializeArbitrator(true /* use_fake_delegate */); | 196 InitializeArbitrator(nullptr); |
| 218 EXPECT_TRUE(arbitrator_); | 197 EXPECT_TRUE(arbitrator_); |
| 219 arbitrator_.reset(); | 198 arbitrator_.reset(); |
| 220 SUCCEED(); | 199 SUCCEED(); |
| 221 } | 200 } |
| 222 | 201 |
| 223 TEST_F(GeolocationLocationArbitratorTest, OnPermissionGranted) { | 202 TEST_F(GeolocationLocationArbitratorTest, OnPermissionGranted) { |
| 224 InitializeArbitrator(false /* use_fake_delegate */); | 203 InitializeArbitrator(nullptr); |
| 225 EXPECT_FALSE(arbitrator_->HasPermissionBeenGranted()); | 204 EXPECT_FALSE(arbitrator_->HasPermissionBeenGranted()); |
| 226 arbitrator_->OnPermissionGranted(); | 205 arbitrator_->OnPermissionGranted(); |
| 227 EXPECT_TRUE(arbitrator_->HasPermissionBeenGranted()); | 206 EXPECT_TRUE(arbitrator_->HasPermissionBeenGranted()); |
| 228 // Can't check the provider has been notified without going through the | 207 // Can't check the provider has been notified without going through the |
| 229 // motions to create the provider (see next test). | 208 // motions to create the provider (see next test). |
| 230 EXPECT_FALSE(cell()); | 209 EXPECT_FALSE(cell()); |
| 231 EXPECT_FALSE(gps()); | 210 EXPECT_FALSE(gps()); |
| 232 EXPECT_FALSE(GetSystemLocationProviderOverride()); | |
| 233 } | 211 } |
| 234 | 212 |
| 235 TEST_F(GeolocationLocationArbitratorTest, NormalUsage) { | 213 TEST_F(GeolocationLocationArbitratorTest, NormalUsage) { |
| 236 InitializeArbitrator(false /* use_fake_delegate */); | 214 InitializeArbitrator(nullptr); |
| 237 ASSERT_TRUE(access_token_store_.get()); | 215 ASSERT_TRUE(access_token_store_.get()); |
| 238 ASSERT_TRUE(arbitrator_); | 216 ASSERT_TRUE(arbitrator_); |
| 239 | 217 |
| 240 EXPECT_FALSE(cell()); | 218 EXPECT_FALSE(cell()); |
| 241 EXPECT_FALSE(gps()); | 219 EXPECT_FALSE(gps()); |
| 242 EXPECT_FALSE(GetSystemLocationProviderOverride()); | |
| 243 arbitrator_->StartProviders(false); | 220 arbitrator_->StartProviders(false); |
| 244 | 221 |
| 245 EXPECT_TRUE(access_token_store_->access_token_map_.empty()); | 222 EXPECT_TRUE(access_token_store_->access_token_map_.empty()); |
| 246 | 223 |
| 247 access_token_store_->NotifyDelegateTokensLoaded(); | 224 access_token_store_->NotifyDelegateTokensLoaded(); |
| 248 ASSERT_TRUE(cell()); | 225 ASSERT_TRUE(cell()); |
| 249 EXPECT_TRUE(gps()); | 226 EXPECT_TRUE(gps()); |
| 250 EXPECT_FALSE(GetSystemLocationProviderOverride()); | |
| 251 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, cell()->state_); | 227 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, cell()->state_); |
| 252 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, gps()->state_); | 228 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, gps()->state_); |
| 253 EXPECT_FALSE(observer_->last_position_.Validate()); | 229 EXPECT_FALSE(observer_->last_position_.Validate()); |
| 254 EXPECT_EQ(Geoposition::ERROR_CODE_NONE, | 230 EXPECT_EQ(Geoposition::ERROR_CODE_NONE, |
| 255 observer_->last_position_.error_code); | 231 observer_->last_position_.error_code); |
| 256 | 232 |
| 257 SetReferencePosition(cell()); | 233 SetReferencePosition(cell()); |
| 258 | 234 |
| 259 EXPECT_TRUE(observer_->last_position_.Validate() || | 235 EXPECT_TRUE(observer_->last_position_.Validate() || |
| 260 observer_->last_position_.error_code != | 236 observer_->last_position_.error_code != |
| 261 Geoposition::ERROR_CODE_NONE); | 237 Geoposition::ERROR_CODE_NONE); |
| 262 EXPECT_EQ(cell()->position_.latitude, | 238 EXPECT_EQ(cell()->position_.latitude, |
| 263 observer_->last_position_.latitude); | 239 observer_->last_position_.latitude); |
| 264 | 240 |
| 265 EXPECT_FALSE(cell()->is_permission_granted_); | 241 EXPECT_FALSE(cell()->is_permission_granted_); |
| 266 EXPECT_FALSE(arbitrator_->HasPermissionBeenGranted()); | 242 EXPECT_FALSE(arbitrator_->HasPermissionBeenGranted()); |
| 267 arbitrator_->OnPermissionGranted(); | 243 arbitrator_->OnPermissionGranted(); |
| 268 EXPECT_TRUE(arbitrator_->HasPermissionBeenGranted()); | 244 EXPECT_TRUE(arbitrator_->HasPermissionBeenGranted()); |
| 269 EXPECT_TRUE(cell()->is_permission_granted_); | 245 EXPECT_TRUE(cell()->is_permission_granted_); |
| 270 } | 246 } |
| 271 | 247 |
| 272 TEST_F(GeolocationLocationArbitratorTest, CustomSystemProviderOnly) { | 248 TEST_F(GeolocationLocationArbitratorTest, CustomSystemProviderOnly) { |
| 273 InitializeArbitrator(true /* use_fake_delegate */); | 249 FakeGeolocationDelegate* fake_delegate = new FakeGeolocationDelegate; |
| 274 arbitrator_->GetFakeGeolocationDelegate()->set_use_network(false); | 250 fake_delegate->set_use_network(false); |
| 251 |
| 252 std::unique_ptr<GeolocationDelegate> delegate(fake_delegate); |
| 253 InitializeArbitrator(std::move(delegate)); |
| 275 ASSERT_TRUE(arbitrator_); | 254 ASSERT_TRUE(arbitrator_); |
| 276 | 255 |
| 277 EXPECT_FALSE(cell()); | 256 EXPECT_FALSE(cell()); |
| 278 EXPECT_FALSE(gps()); | 257 EXPECT_FALSE(gps()); |
| 279 arbitrator_->StartProviders(false); | 258 arbitrator_->StartProviders(false); |
| 280 | 259 |
| 281 ASSERT_FALSE(cell()); | 260 ASSERT_FALSE(cell()); |
| 282 EXPECT_FALSE(gps()); | 261 EXPECT_FALSE(gps()); |
| 283 ASSERT_TRUE(GetSystemLocationProviderOverride()); | 262 ASSERT_TRUE(fake_delegate->mock_location_provider()); |
| 284 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, | 263 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, |
| 285 GetSystemLocationProviderOverride()->state_); | 264 fake_delegate->mock_location_provider()->state_); |
| 286 EXPECT_FALSE(observer_->last_position_.Validate()); | 265 EXPECT_FALSE(observer_->last_position_.Validate()); |
| 287 EXPECT_EQ(Geoposition::ERROR_CODE_NONE, observer_->last_position_.error_code); | 266 EXPECT_EQ(Geoposition::ERROR_CODE_NONE, observer_->last_position_.error_code); |
| 288 | 267 |
| 289 SetReferencePosition(GetSystemLocationProviderOverride()); | 268 SetReferencePosition(fake_delegate->mock_location_provider()); |
| 290 | 269 |
| 291 EXPECT_TRUE(observer_->last_position_.Validate() || | 270 EXPECT_TRUE(observer_->last_position_.Validate() || |
| 292 observer_->last_position_.error_code != | 271 observer_->last_position_.error_code != |
| 293 Geoposition::ERROR_CODE_NONE); | 272 Geoposition::ERROR_CODE_NONE); |
| 294 EXPECT_EQ(GetSystemLocationProviderOverride()->position_.latitude, | 273 EXPECT_EQ(fake_delegate->mock_location_provider()->position_.latitude, |
| 295 observer_->last_position_.latitude); | 274 observer_->last_position_.latitude); |
| 296 | 275 |
| 297 EXPECT_FALSE(GetSystemLocationProviderOverride()->is_permission_granted_); | 276 EXPECT_FALSE(fake_delegate->mock_location_provider()->is_permission_granted_); |
| 298 EXPECT_FALSE(arbitrator_->HasPermissionBeenGranted()); | 277 EXPECT_FALSE(arbitrator_->HasPermissionBeenGranted()); |
| 299 arbitrator_->OnPermissionGranted(); | 278 arbitrator_->OnPermissionGranted(); |
| 300 EXPECT_TRUE(arbitrator_->HasPermissionBeenGranted()); | 279 EXPECT_TRUE(arbitrator_->HasPermissionBeenGranted()); |
| 301 EXPECT_TRUE(GetSystemLocationProviderOverride()->is_permission_granted_); | 280 EXPECT_TRUE(fake_delegate->mock_location_provider()->is_permission_granted_); |
| 302 } | 281 } |
| 303 | 282 |
| 304 TEST_F(GeolocationLocationArbitratorTest, | 283 TEST_F(GeolocationLocationArbitratorTest, |
| 305 CustomSystemAndDefaultNetworkProviders) { | 284 CustomSystemAndDefaultNetworkProviders) { |
| 306 InitializeArbitrator(true /* use_fake_delegate */); | 285 FakeGeolocationDelegate* fake_delegate = new FakeGeolocationDelegate; |
| 307 arbitrator_->GetFakeGeolocationDelegate()->set_use_network(true); | 286 fake_delegate->set_use_network(true); |
| 287 |
| 288 std::unique_ptr<GeolocationDelegate> delegate(fake_delegate); |
| 289 InitializeArbitrator(std::move(delegate)); |
| 308 ASSERT_TRUE(arbitrator_); | 290 ASSERT_TRUE(arbitrator_); |
| 309 | 291 |
| 310 EXPECT_FALSE(cell()); | 292 EXPECT_FALSE(cell()); |
| 311 EXPECT_FALSE(gps()); | 293 EXPECT_FALSE(gps()); |
| 312 arbitrator_->StartProviders(false); | 294 arbitrator_->StartProviders(false); |
| 313 | 295 |
| 314 EXPECT_TRUE(access_token_store_->access_token_map_.empty()); | 296 EXPECT_TRUE(access_token_store_->access_token_map_.empty()); |
| 315 | 297 |
| 316 access_token_store_->NotifyDelegateTokensLoaded(); | 298 access_token_store_->NotifyDelegateTokensLoaded(); |
| 317 | 299 |
| 318 ASSERT_TRUE(cell()); | 300 ASSERT_TRUE(cell()); |
| 319 EXPECT_FALSE(gps()); | 301 EXPECT_FALSE(gps()); |
| 320 ASSERT_TRUE(GetSystemLocationProviderOverride()); | 302 ASSERT_TRUE(fake_delegate->mock_location_provider()); |
| 321 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, | 303 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, |
| 322 GetSystemLocationProviderOverride()->state_); | 304 fake_delegate->mock_location_provider()->state_); |
| 323 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, cell()->state_); | 305 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, cell()->state_); |
| 324 EXPECT_FALSE(observer_->last_position_.Validate()); | 306 EXPECT_FALSE(observer_->last_position_.Validate()); |
| 325 EXPECT_EQ(Geoposition::ERROR_CODE_NONE, observer_->last_position_.error_code); | 307 EXPECT_EQ(Geoposition::ERROR_CODE_NONE, observer_->last_position_.error_code); |
| 326 | 308 |
| 327 SetReferencePosition(cell()); | 309 SetReferencePosition(cell()); |
| 328 | 310 |
| 329 EXPECT_TRUE(observer_->last_position_.Validate() || | 311 EXPECT_TRUE(observer_->last_position_.Validate() || |
| 330 observer_->last_position_.error_code != | 312 observer_->last_position_.error_code != |
| 331 Geoposition::ERROR_CODE_NONE); | 313 Geoposition::ERROR_CODE_NONE); |
| 332 EXPECT_EQ(cell()->position_.latitude, observer_->last_position_.latitude); | 314 EXPECT_EQ(cell()->position_.latitude, observer_->last_position_.latitude); |
| 333 | 315 |
| 334 EXPECT_FALSE(cell()->is_permission_granted_); | 316 EXPECT_FALSE(cell()->is_permission_granted_); |
| 335 EXPECT_FALSE(arbitrator_->HasPermissionBeenGranted()); | 317 EXPECT_FALSE(arbitrator_->HasPermissionBeenGranted()); |
| 336 arbitrator_->OnPermissionGranted(); | 318 arbitrator_->OnPermissionGranted(); |
| 337 EXPECT_TRUE(arbitrator_->HasPermissionBeenGranted()); | 319 EXPECT_TRUE(arbitrator_->HasPermissionBeenGranted()); |
| 338 EXPECT_TRUE(cell()->is_permission_granted_); | 320 EXPECT_TRUE(cell()->is_permission_granted_); |
| 339 } | 321 } |
| 340 | 322 |
| 341 TEST_F(GeolocationLocationArbitratorTest, SetObserverOptions) { | 323 TEST_F(GeolocationLocationArbitratorTest, SetObserverOptions) { |
| 342 InitializeArbitrator(false /* use_fake_delegate */); | 324 InitializeArbitrator(nullptr); |
| 343 arbitrator_->StartProviders(false); | 325 arbitrator_->StartProviders(false); |
| 344 access_token_store_->NotifyDelegateTokensLoaded(); | 326 access_token_store_->NotifyDelegateTokensLoaded(); |
| 345 ASSERT_TRUE(cell()); | 327 ASSERT_TRUE(cell()); |
| 346 ASSERT_TRUE(gps()); | 328 ASSERT_TRUE(gps()); |
| 347 EXPECT_FALSE(GetSystemLocationProviderOverride()); | |
| 348 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, cell()->state_); | 329 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, cell()->state_); |
| 349 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, gps()->state_); | 330 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, gps()->state_); |
| 350 SetReferencePosition(cell()); | 331 SetReferencePosition(cell()); |
| 351 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, cell()->state_); | 332 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, cell()->state_); |
| 352 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, gps()->state_); | 333 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, gps()->state_); |
| 353 arbitrator_->StartProviders(true); | 334 arbitrator_->StartProviders(true); |
| 354 EXPECT_EQ(MockLocationProvider::HIGH_ACCURACY, cell()->state_); | 335 EXPECT_EQ(MockLocationProvider::HIGH_ACCURACY, cell()->state_); |
| 355 EXPECT_EQ(MockLocationProvider::HIGH_ACCURACY, gps()->state_); | 336 EXPECT_EQ(MockLocationProvider::HIGH_ACCURACY, gps()->state_); |
| 356 EXPECT_FALSE(GetSystemLocationProviderOverride()); | |
| 357 } | 337 } |
| 358 | 338 |
| 359 TEST_F(GeolocationLocationArbitratorTest, Arbitration) { | 339 TEST_F(GeolocationLocationArbitratorTest, Arbitration) { |
| 360 InitializeArbitrator(false /* use_fake_delegate */); | 340 InitializeArbitrator(nullptr); |
| 361 arbitrator_->StartProviders(false); | 341 arbitrator_->StartProviders(false); |
| 362 access_token_store_->NotifyDelegateTokensLoaded(); | 342 access_token_store_->NotifyDelegateTokensLoaded(); |
| 363 ASSERT_TRUE(cell()); | 343 ASSERT_TRUE(cell()); |
| 364 ASSERT_TRUE(gps()); | 344 ASSERT_TRUE(gps()); |
| 365 EXPECT_FALSE(GetSystemLocationProviderOverride()); | |
| 366 | 345 |
| 367 SetPositionFix(cell(), 1, 2, 150); | 346 SetPositionFix(cell(), 1, 2, 150); |
| 368 | 347 |
| 369 // First position available | 348 // First position available |
| 370 EXPECT_TRUE(observer_->last_position_.Validate()); | 349 EXPECT_TRUE(observer_->last_position_.Validate()); |
| 371 CheckLastPositionInfo(1, 2, 150); | 350 CheckLastPositionInfo(1, 2, 150); |
| 372 | 351 |
| 373 SetPositionFix(gps(), 3, 4, 50); | 352 SetPositionFix(gps(), 3, 4, 50); |
| 374 | 353 |
| 375 // More accurate fix available | 354 // More accurate fix available |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 428 CheckLastPositionInfo(3.5657104, 139.690341, 300); | 407 CheckLastPositionInfo(3.5657104, 139.690341, 300); |
| 429 | 408 |
| 430 // 2 minutes later | 409 // 2 minutes later |
| 431 AdvanceTimeNow(base::TimeDelta::FromMinutes(2)); | 410 AdvanceTimeNow(base::TimeDelta::FromMinutes(2)); |
| 432 // Arrive in station. Cell moves but GPS is stale. Switch to fresher cell. | 411 // Arrive in station. Cell moves but GPS is stale. Switch to fresher cell. |
| 433 SetPositionFix(cell(), 3.5658700, 139.069979, 1000); | 412 SetPositionFix(cell(), 3.5658700, 139.069979, 1000); |
| 434 CheckLastPositionInfo(3.5658700, 139.069979, 1000); | 413 CheckLastPositionInfo(3.5658700, 139.069979, 1000); |
| 435 } | 414 } |
| 436 | 415 |
| 437 TEST_F(GeolocationLocationArbitratorTest, TwoOneShotsIsNewPositionBetter) { | 416 TEST_F(GeolocationLocationArbitratorTest, TwoOneShotsIsNewPositionBetter) { |
| 438 InitializeArbitrator(false /* use_fake_delegate */); | 417 InitializeArbitrator(nullptr); |
| 439 arbitrator_->StartProviders(false); | 418 arbitrator_->StartProviders(false); |
| 440 access_token_store_->NotifyDelegateTokensLoaded(); | 419 access_token_store_->NotifyDelegateTokensLoaded(); |
| 441 ASSERT_TRUE(cell()); | 420 ASSERT_TRUE(cell()); |
| 442 ASSERT_TRUE(gps()); | 421 ASSERT_TRUE(gps()); |
| 443 EXPECT_FALSE(GetSystemLocationProviderOverride()); | |
| 444 | 422 |
| 445 // Set the initial position. | 423 // Set the initial position. |
| 446 SetPositionFix(cell(), 3, 139, 100); | 424 SetPositionFix(cell(), 3, 139, 100); |
| 447 CheckLastPositionInfo(3, 139, 100); | 425 CheckLastPositionInfo(3, 139, 100); |
| 448 | 426 |
| 449 // Restart providers to simulate a one-shot request. | 427 // Restart providers to simulate a one-shot request. |
| 450 arbitrator_->StopProviders(); | 428 arbitrator_->StopProviders(); |
| 451 | 429 |
| 452 // To test 240956, perform a throwaway alloc. | 430 // To test 240956, perform a throwaway alloc. |
| 453 // This convinces the allocator to put the providers in a new memory location. | 431 // This convinces the allocator to put the providers in a new memory location. |
| 454 std::unique_ptr<MockLocationProvider> dummy_provider( | 432 std::unique_ptr<MockLocationProvider> dummy_provider( |
| 455 new MockLocationProvider); | 433 new MockLocationProvider); |
| 456 | 434 |
| 457 arbitrator_->StartProviders(false); | 435 arbitrator_->StartProviders(false); |
| 458 access_token_store_->NotifyDelegateTokensLoaded(); | 436 access_token_store_->NotifyDelegateTokensLoaded(); |
| 459 | 437 |
| 460 // Advance the time a short while to simulate successive calls. | 438 // Advance the time a short while to simulate successive calls. |
| 461 AdvanceTimeNow(base::TimeDelta::FromMilliseconds(5)); | 439 AdvanceTimeNow(base::TimeDelta::FromMilliseconds(5)); |
| 462 | 440 |
| 463 // Update with a less accurate position to verify 240956. | 441 // Update with a less accurate position to verify 240956. |
| 464 SetPositionFix(cell(), 3, 139, 150); | 442 SetPositionFix(cell(), 3, 139, 150); |
| 465 CheckLastPositionInfo(3, 139, 150); | 443 CheckLastPositionInfo(3, 139, 150); |
| 466 } | 444 } |
| 467 | 445 |
| 468 } // namespace content | 446 } // namespace content |
| OLD | NEW |