| 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" |
| 11 #include "content/browser/geolocation/fake_access_token_store.h" | 11 #include "content/browser/geolocation/fake_access_token_store.h" |
| 12 #include "content/browser/geolocation/mock_location_provider.h" | 12 #include "content/browser/geolocation/mock_location_provider.h" |
| 13 #include "content/public/browser/geolocation_delegate.h" |
| 13 #include "content/public/common/geoposition.h" | 14 #include "content/public/common/geoposition.h" |
| 14 #include "content/test/test_content_browser_client.h" | 15 #include "content/test/test_content_browser_client.h" |
| 15 #include "testing/gmock/include/gmock/gmock.h" | 16 #include "testing/gmock/include/gmock/gmock.h" |
| 16 #include "testing/gtest/include/gtest/gtest.h" | 17 #include "testing/gtest/include/gtest/gtest.h" |
| 17 | 18 |
| 18 using ::testing::NiceMock; | 19 using ::testing::NiceMock; |
| 19 | 20 |
| 20 namespace content { | 21 namespace content { |
| 21 | 22 |
| 22 class MockLocationObserver { | 23 class MockLocationObserver { |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 ASSERT_TRUE(position.Validate()); | 60 ASSERT_TRUE(position.Validate()); |
| 60 provider->HandlePositionChanged(position); | 61 provider->HandlePositionChanged(position); |
| 61 } | 62 } |
| 62 | 63 |
| 63 void SetReferencePosition(MockLocationProvider* provider) { | 64 void SetReferencePosition(MockLocationProvider* provider) { |
| 64 SetPositionFix(provider, 51.0, -0.1, 400); | 65 SetPositionFix(provider, 51.0, -0.1, 400); |
| 65 } | 66 } |
| 66 | 67 |
| 67 namespace { | 68 namespace { |
| 68 | 69 |
| 69 class FakeDelegate : public GeolocationProvider::Delegate { | 70 class FakeGeolocationDelegate : public GeolocationDelegate { |
| 70 public: | 71 public: |
| 71 FakeDelegate() = default; | 72 FakeGeolocationDelegate() = default; |
| 72 | 73 |
| 73 bool UseNetworkLocationProviders() override { return use_network_; } | 74 bool UseNetworkLocationProviders() override { return use_network_; } |
| 74 void set_use_network(bool use_network) { use_network_ = use_network; } | 75 void set_use_network(bool use_network) { use_network_ = use_network; } |
| 75 | 76 |
| 76 LocationProvider* OverrideSystemLocationProvider() override { | 77 LocationProvider* OverrideSystemLocationProvider() override { |
| 77 if (!system_location_provider_) | 78 if (!system_location_provider_) |
| 78 system_location_provider_ = base::WrapUnique(new MockLocationProvider); | 79 system_location_provider_ = base::WrapUnique(new MockLocationProvider); |
| 79 return system_location_provider_.get(); | 80 return system_location_provider_.get(); |
| 80 } | 81 } |
| 81 | 82 |
| 82 LocationProvider* system_location_provider() const { | 83 LocationProvider* system_location_provider() const { |
| 83 return system_location_provider_.get(); | 84 return system_location_provider_.get(); |
| 84 } | 85 } |
| 85 | 86 |
| 86 private: | 87 private: |
| 87 bool use_network_ = true; | 88 bool use_network_ = true; |
| 88 std::unique_ptr<LocationProvider> system_location_provider_; | 89 std::unique_ptr<LocationProvider> system_location_provider_; |
| 89 | 90 |
| 90 DISALLOW_COPY_AND_ASSIGN(FakeDelegate); | 91 DISALLOW_COPY_AND_ASSIGN(FakeGeolocationDelegate); |
| 91 }; | 92 }; |
| 92 | 93 |
| 93 class TestingLocationArbitrator : public LocationArbitratorImpl { | 94 class TestingLocationArbitrator : public LocationArbitratorImpl { |
| 94 public: | 95 public: |
| 95 TestingLocationArbitrator( | 96 TestingLocationArbitrator( |
| 96 const LocationArbitratorImpl::LocationUpdateCallback& callback, | 97 const LocationArbitratorImpl::LocationUpdateCallback& callback, |
| 97 AccessTokenStore* access_token_store, | 98 AccessTokenStore* access_token_store, |
| 98 GeolocationProvider::Delegate* delegate, | 99 GeolocationDelegate* delegate, |
| 99 bool is_fake_delegate) | 100 bool is_fake_delegate) |
| 100 : LocationArbitratorImpl(callback, delegate), | 101 : LocationArbitratorImpl(callback, delegate), |
| 101 is_fake_delegate_(is_fake_delegate), | 102 is_fake_delegate_(is_fake_delegate), |
| 102 cell_(nullptr), | 103 cell_(nullptr), |
| 103 gps_(nullptr), | 104 gps_(nullptr), |
| 104 access_token_store_(access_token_store) {} | 105 access_token_store_(access_token_store) {} |
| 105 | 106 |
| 106 base::Time GetTimeNow() const override { return GetTimeNowForTest(); } | 107 base::Time GetTimeNow() const override { return GetTimeNowForTest(); } |
| 107 | 108 |
| 108 AccessTokenStore* NewAccessTokenStore() override { | 109 AccessTokenStore* NewAccessTokenStore() override { |
| 109 return access_token_store_.get(); | 110 return access_token_store_.get(); |
| 110 } | 111 } |
| 111 | 112 |
| 112 std::unique_ptr<LocationProvider> NewNetworkLocationProvider( | 113 std::unique_ptr<LocationProvider> NewNetworkLocationProvider( |
| 113 AccessTokenStore* access_token_store, | 114 AccessTokenStore* access_token_store, |
| 114 net::URLRequestContextGetter* context, | 115 net::URLRequestContextGetter* context, |
| 115 const GURL& url, | 116 const GURL& url, |
| 116 const base::string16& access_token) override { | 117 const base::string16& access_token) override { |
| 117 cell_ = new MockLocationProvider; | 118 cell_ = new MockLocationProvider; |
| 118 return base::WrapUnique(cell_); | 119 return base::WrapUnique(cell_); |
| 119 } | 120 } |
| 120 | 121 |
| 121 std::unique_ptr<LocationProvider> NewSystemLocationProvider() override { | 122 std::unique_ptr<LocationProvider> NewSystemLocationProvider() override { |
| 122 gps_ = new MockLocationProvider; | 123 gps_ = new MockLocationProvider; |
| 123 return base::WrapUnique(gps_); | 124 return base::WrapUnique(gps_); |
| 124 } | 125 } |
| 125 | 126 |
| 126 FakeDelegate* GetFakeDelegate() { | 127 FakeGeolocationDelegate* GetFakeGeolocationDelegate() { |
| 127 DCHECK(is_fake_delegate_); | 128 DCHECK(is_fake_delegate_); |
| 128 return static_cast<FakeDelegate*>(GetDelegateForTesting()); | 129 return static_cast<FakeGeolocationDelegate*>(GetDelegateForTesting()); |
| 129 } | 130 } |
| 130 | 131 |
| 131 LocationProvider* GetLocationProvider() { | 132 LocationProvider* GetLocationProvider() { |
| 132 if (is_fake_delegate_) | 133 if (is_fake_delegate_) |
| 133 return GetFakeDelegate()->system_location_provider(); | 134 return GetFakeGeolocationDelegate()->system_location_provider(); |
| 134 return GetDelegateForTesting()->OverrideSystemLocationProvider(); | 135 return GetDelegateForTesting()->OverrideSystemLocationProvider(); |
| 135 } | 136 } |
| 136 | 137 |
| 137 const bool is_fake_delegate_; | 138 const bool is_fake_delegate_; |
| 138 | 139 |
| 139 // Two location providers, with nice short names to make the tests more | 140 // Two location providers, with nice short names to make the tests more |
| 140 // readable. Note |gps_| will only be set when there is a high accuracy | 141 // readable. Note |gps_| will only be set when there is a high accuracy |
| 141 // observer registered (and |cell_| when there's at least one observer of any | 142 // observer registered (and |cell_| when there's at least one observer of any |
| 142 // type). | 143 // type). |
| 143 | 144 |
| 144 // TODO(mvanouwerkerk): rename |cell_| to |network_location_provider_| and | 145 // TODO(mvanouwerkerk): rename |cell_| to |network_location_provider_| and |
| 145 // |gps_| to |system_location_provider_| | 146 // |gps_| to |system_location_provider_| |
| 146 MockLocationProvider* cell_; | 147 MockLocationProvider* cell_; |
| 147 MockLocationProvider* gps_; | 148 MockLocationProvider* gps_; |
| 148 scoped_refptr<AccessTokenStore> access_token_store_; | 149 scoped_refptr<AccessTokenStore> access_token_store_; |
| 149 }; | 150 }; |
| 150 | 151 |
| 151 } // namespace | 152 } // namespace |
| 152 | 153 |
| 153 class GeolocationLocationArbitratorTest : public testing::Test { | 154 class GeolocationLocationArbitratorTest : public testing::Test { |
| 154 protected: | 155 protected: |
| 155 // testing::Test | 156 // testing::Test |
| 156 void SetUp() override { | 157 void SetUp() override { |
| 157 access_token_store_ = new NiceMock<FakeAccessTokenStore>; | 158 access_token_store_ = new NiceMock<FakeAccessTokenStore>; |
| 158 observer_.reset(new MockLocationObserver); | 159 observer_.reset(new MockLocationObserver); |
| 159 } | 160 } |
| 160 | 161 |
| 161 // There are two types of test cases: those using FakeDelegate and the ones | 162 // There are two types of test cases: those using FakeGeolocationDelegate and |
| 162 // exercising whatever the embedder provides. Test cases call this method to | 163 // the ones exercising whatever the embedder provides. Test cases call this |
| 163 // choose the appropriate one. | 164 // method to choose the appropriate one. |
| 164 void InitializeArbitrator(bool use_fake_delegate) { | 165 void InitializeArbitrator(bool use_fake_delegate) { |
| 165 delegate_.reset(use_fake_delegate ? new FakeDelegate() | 166 delegate_.reset(use_fake_delegate ? new FakeGeolocationDelegate() |
| 166 : new GeolocationProvider::Delegate); | 167 : new GeolocationDelegate); |
| 167 const LocationArbitratorImpl::LocationUpdateCallback callback = | 168 const LocationArbitratorImpl::LocationUpdateCallback callback = |
| 168 base::Bind(&MockLocationObserver::OnLocationUpdate, | 169 base::Bind(&MockLocationObserver::OnLocationUpdate, |
| 169 base::Unretained(observer_.get())); | 170 base::Unretained(observer_.get())); |
| 170 arbitrator_.reset( | 171 arbitrator_.reset( |
| 171 new TestingLocationArbitrator(callback, access_token_store_.get(), | 172 new TestingLocationArbitrator(callback, access_token_store_.get(), |
| 172 delegate_.get(), use_fake_delegate)); | 173 delegate_.get(), use_fake_delegate)); |
| 173 } | 174 } |
| 174 | 175 |
| 175 // testing::Test | 176 // testing::Test |
| 176 void TearDown() override {} | 177 void TearDown() override {} |
| (...skipping 23 matching lines...) Expand all Loading... |
| 200 } | 201 } |
| 201 | 202 |
| 202 MockLocationProvider* GetSystemLocationProviderOverride() { | 203 MockLocationProvider* GetSystemLocationProviderOverride() { |
| 203 return static_cast<MockLocationProvider*>( | 204 return static_cast<MockLocationProvider*>( |
| 204 arbitrator_->GetLocationProvider()); | 205 arbitrator_->GetLocationProvider()); |
| 205 } | 206 } |
| 206 | 207 |
| 207 scoped_refptr<FakeAccessTokenStore> access_token_store_; | 208 scoped_refptr<FakeAccessTokenStore> access_token_store_; |
| 208 std::unique_ptr<MockLocationObserver> observer_; | 209 std::unique_ptr<MockLocationObserver> observer_; |
| 209 std::unique_ptr<TestingLocationArbitrator> arbitrator_; | 210 std::unique_ptr<TestingLocationArbitrator> arbitrator_; |
| 210 std::unique_ptr<GeolocationProvider::Delegate> delegate_; | 211 std::unique_ptr<GeolocationDelegate> delegate_; |
| 211 base::MessageLoop loop_; | 212 base::MessageLoop loop_; |
| 212 }; | 213 }; |
| 213 | 214 |
| 214 TEST_F(GeolocationLocationArbitratorTest, CreateDestroy) { | 215 TEST_F(GeolocationLocationArbitratorTest, CreateDestroy) { |
| 215 EXPECT_TRUE(access_token_store_.get()); | 216 EXPECT_TRUE(access_token_store_.get()); |
| 216 InitializeArbitrator(true /* use_fake_delegate */); | 217 InitializeArbitrator(true /* use_fake_delegate */); |
| 217 EXPECT_TRUE(arbitrator_); | 218 EXPECT_TRUE(arbitrator_); |
| 218 arbitrator_.reset(); | 219 arbitrator_.reset(); |
| 219 SUCCEED(); | 220 SUCCEED(); |
| 220 } | 221 } |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 263 | 264 |
| 264 EXPECT_FALSE(cell()->is_permission_granted_); | 265 EXPECT_FALSE(cell()->is_permission_granted_); |
| 265 EXPECT_FALSE(arbitrator_->HasPermissionBeenGranted()); | 266 EXPECT_FALSE(arbitrator_->HasPermissionBeenGranted()); |
| 266 arbitrator_->OnPermissionGranted(); | 267 arbitrator_->OnPermissionGranted(); |
| 267 EXPECT_TRUE(arbitrator_->HasPermissionBeenGranted()); | 268 EXPECT_TRUE(arbitrator_->HasPermissionBeenGranted()); |
| 268 EXPECT_TRUE(cell()->is_permission_granted_); | 269 EXPECT_TRUE(cell()->is_permission_granted_); |
| 269 } | 270 } |
| 270 | 271 |
| 271 TEST_F(GeolocationLocationArbitratorTest, CustomSystemProviderOnly) { | 272 TEST_F(GeolocationLocationArbitratorTest, CustomSystemProviderOnly) { |
| 272 InitializeArbitrator(true /* use_fake_delegate */); | 273 InitializeArbitrator(true /* use_fake_delegate */); |
| 273 arbitrator_->GetFakeDelegate()->set_use_network(false); | 274 arbitrator_->GetFakeGeolocationDelegate()->set_use_network(false); |
| 274 ASSERT_TRUE(arbitrator_); | 275 ASSERT_TRUE(arbitrator_); |
| 275 | 276 |
| 276 EXPECT_FALSE(cell()); | 277 EXPECT_FALSE(cell()); |
| 277 EXPECT_FALSE(gps()); | 278 EXPECT_FALSE(gps()); |
| 278 arbitrator_->StartProviders(false); | 279 arbitrator_->StartProviders(false); |
| 279 | 280 |
| 280 ASSERT_FALSE(cell()); | 281 ASSERT_FALSE(cell()); |
| 281 EXPECT_FALSE(gps()); | 282 EXPECT_FALSE(gps()); |
| 282 ASSERT_TRUE(GetSystemLocationProviderOverride()); | 283 ASSERT_TRUE(GetSystemLocationProviderOverride()); |
| 283 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, | 284 EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, |
| (...skipping 12 matching lines...) Expand all Loading... |
| 296 EXPECT_FALSE(GetSystemLocationProviderOverride()->is_permission_granted_); | 297 EXPECT_FALSE(GetSystemLocationProviderOverride()->is_permission_granted_); |
| 297 EXPECT_FALSE(arbitrator_->HasPermissionBeenGranted()); | 298 EXPECT_FALSE(arbitrator_->HasPermissionBeenGranted()); |
| 298 arbitrator_->OnPermissionGranted(); | 299 arbitrator_->OnPermissionGranted(); |
| 299 EXPECT_TRUE(arbitrator_->HasPermissionBeenGranted()); | 300 EXPECT_TRUE(arbitrator_->HasPermissionBeenGranted()); |
| 300 EXPECT_TRUE(GetSystemLocationProviderOverride()->is_permission_granted_); | 301 EXPECT_TRUE(GetSystemLocationProviderOverride()->is_permission_granted_); |
| 301 } | 302 } |
| 302 | 303 |
| 303 TEST_F(GeolocationLocationArbitratorTest, | 304 TEST_F(GeolocationLocationArbitratorTest, |
| 304 CustomSystemAndDefaultNetworkProviders) { | 305 CustomSystemAndDefaultNetworkProviders) { |
| 305 InitializeArbitrator(true /* use_fake_delegate */); | 306 InitializeArbitrator(true /* use_fake_delegate */); |
| 306 arbitrator_->GetFakeDelegate()->set_use_network(true); | 307 arbitrator_->GetFakeGeolocationDelegate()->set_use_network(true); |
| 307 ASSERT_TRUE(arbitrator_); | 308 ASSERT_TRUE(arbitrator_); |
| 308 | 309 |
| 309 EXPECT_FALSE(cell()); | 310 EXPECT_FALSE(cell()); |
| 310 EXPECT_FALSE(gps()); | 311 EXPECT_FALSE(gps()); |
| 311 arbitrator_->StartProviders(false); | 312 arbitrator_->StartProviders(false); |
| 312 | 313 |
| 313 EXPECT_TRUE(access_token_store_->access_token_map_.empty()); | 314 EXPECT_TRUE(access_token_store_->access_token_map_.empty()); |
| 314 | 315 |
| 315 access_token_store_->NotifyDelegateTokensLoaded(); | 316 access_token_store_->NotifyDelegateTokensLoaded(); |
| 316 | 317 |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 458 | 459 |
| 459 // Advance the time a short while to simulate successive calls. | 460 // Advance the time a short while to simulate successive calls. |
| 460 AdvanceTimeNow(base::TimeDelta::FromMilliseconds(5)); | 461 AdvanceTimeNow(base::TimeDelta::FromMilliseconds(5)); |
| 461 | 462 |
| 462 // Update with a less accurate position to verify 240956. | 463 // Update with a less accurate position to verify 240956. |
| 463 SetPositionFix(cell(), 3, 139, 150); | 464 SetPositionFix(cell(), 3, 139, 150); |
| 464 CheckLastPositionInfo(3, 139, 150); | 465 CheckLastPositionInfo(3, 139, 150); |
| 465 } | 466 } |
| 466 | 467 |
| 467 } // namespace content | 468 } // namespace content |
| OLD | NEW |