Chromium Code Reviews| Index: content/browser/geolocation/location_arbitrator_impl_unittest.cc |
| diff --git a/content/browser/geolocation/location_arbitrator_impl_unittest.cc b/content/browser/geolocation/location_arbitrator_impl_unittest.cc |
| index b5429c86d47a650add0b6250771865b3b026ee70..1f8cf27cd6b1aa4da889889e7a487fcfb46c0326 100644 |
| --- a/content/browser/geolocation/location_arbitrator_impl_unittest.cc |
| +++ b/content/browser/geolocation/location_arbitrator_impl_unittest.cc |
| @@ -66,37 +66,30 @@ void SetReferencePosition(MockLocationProvider* provider) { |
| namespace { |
| -class GeolocationContentBrowserClient : public TestContentBrowserClient { |
| +class MockServiceOverrides : public GeolocationProvider::ServiceOverrides { |
|
Michael van Ouwerkerk
2016/06/24 13:57:19
This is not really a mock (using gmock), but a fak
mcasas
2016/06/24 19:25:52
Done.
|
| public: |
| - GeolocationContentBrowserClient() {} |
| - |
| + bool UseNetworkLocationProviders() override { return use_network_; } |
| void set_use_network(bool use_network) { use_network_ = use_network; } |
| LocationProvider* OverrideSystemLocationProvider() override { |
| - provider_ = new MockLocationProvider; |
| - return provider_; |
| + if (!location_provider_) |
| + location_provider_ = base::WrapUnique(new MockLocationProvider); |
| + return location_provider_.get(); |
| } |
| - bool UseNetworkLocationProviders() override { return use_network_; } |
| - |
| - // This provider does not own the object. It is returned by |
| - // GeolocationLocationAribtratorTest::GetSystemLocationProviderOverride(). |
| - // The caller takes ownership. This is just a reference we can use for |
| - // mocking purposes. |
| - MockLocationProvider* provider_ = nullptr; |
| - |
| private: |
| bool use_network_ = true; |
| + std::unique_ptr<LocationProvider> location_provider_; |
| - DISALLOW_COPY_AND_ASSIGN(GeolocationContentBrowserClient); |
|
Michael van Ouwerkerk
2016/06/24 13:57:19
Why delete this?
mcasas
2016/06/24 19:25:52
Unintended, restated.
|
| }; |
| class TestingLocationArbitrator : public LocationArbitratorImpl { |
| public: |
| TestingLocationArbitrator( |
| const LocationArbitratorImpl::LocationUpdateCallback& callback, |
| - AccessTokenStore* access_token_store) |
| - : LocationArbitratorImpl(callback), |
| + AccessTokenStore* access_token_store, |
| + GeolocationProvider::ServiceOverrides* service_overrides) |
| + : LocationArbitratorImpl(callback, service_overrides), |
| cell_(nullptr), |
| gps_(nullptr), |
| access_token_store_(access_token_store) {} |
| @@ -121,6 +114,14 @@ class TestingLocationArbitrator : public LocationArbitratorImpl { |
| return base::WrapUnique(gps_); |
| } |
| + MockServiceOverrides* GetServiceOverrides() { |
| + return static_cast<MockServiceOverrides*>(GetServiceOverridesForTesting()); |
| + } |
| + |
| + LocationProvider* GetLocationProvider() { |
| + return GetServiceOverrides()->OverrideSystemLocationProvider(); |
| + } |
| + |
| // Two location providers, with nice short names to make the tests more |
| // readable. Note |gps_| will only be set when there is a high accuracy |
| // observer registered (and |cell_| when there's at least one observer of any |
| @@ -141,12 +142,20 @@ class GeolocationLocationArbitratorTest : public testing::Test { |
| void SetUp() override { |
| access_token_store_ = new NiceMock<FakeAccessTokenStore>; |
| observer_.reset(new MockLocationObserver); |
| - LocationArbitratorImpl::LocationUpdateCallback callback = |
| + } |
| + |
| + // There are two types of test cases: those using MockServiceOverrides and the |
| + // ones exercising whatever the embedder provides. Test cases call this method |
| + // to choose the appropriate one. |
| + void InitializeArbitrator(bool use_mock_service_overrides) { |
| + GeolocationProvider::ServiceOverrides* const service_override = |
| + use_mock_service_overrides ? new MockServiceOverrides |
| + : new GeolocationProvider::ServiceOverrides; |
| + const LocationArbitratorImpl::LocationUpdateCallback callback = |
| base::Bind(&MockLocationObserver::OnLocationUpdate, |
| base::Unretained(observer_.get())); |
| arbitrator_.reset(new TestingLocationArbitrator( |
| - callback, access_token_store_.get())); |
| - override_content_browser_client_.reset(new GeolocationContentBrowserClient); |
| + callback, access_token_store_.get(), service_override)); |
| } |
| // testing::Test |
| @@ -177,25 +186,26 @@ class GeolocationLocationArbitratorTest : public testing::Test { |
| } |
| MockLocationProvider* GetSystemLocationProviderOverride() { |
| - return override_content_browser_client_->provider_; |
| + return static_cast<MockLocationProvider*>( |
| + arbitrator_->GetLocationProvider()); |
| } |
| scoped_refptr<FakeAccessTokenStore> access_token_store_; |
| std::unique_ptr<MockLocationObserver> observer_; |
| std::unique_ptr<TestingLocationArbitrator> arbitrator_; |
| base::MessageLoop loop_; |
| - std::unique_ptr<GeolocationContentBrowserClient> |
| - override_content_browser_client_; |
| }; |
| TEST_F(GeolocationLocationArbitratorTest, CreateDestroy) { |
| EXPECT_TRUE(access_token_store_.get()); |
| + InitializeArbitrator(true /* use_mock_service_overrides */); |
| EXPECT_TRUE(arbitrator_); |
| arbitrator_.reset(); |
| SUCCEED(); |
| } |
| TEST_F(GeolocationLocationArbitratorTest, OnPermissionGranted) { |
| + InitializeArbitrator(false /* use_mock_service_overrides */); |
| EXPECT_FALSE(arbitrator_->HasPermissionBeenGranted()); |
| arbitrator_->OnPermissionGranted(); |
| EXPECT_TRUE(arbitrator_->HasPermissionBeenGranted()); |
| @@ -207,6 +217,7 @@ TEST_F(GeolocationLocationArbitratorTest, OnPermissionGranted) { |
| } |
| TEST_F(GeolocationLocationArbitratorTest, NormalUsage) { |
| + InitializeArbitrator(false /* use_mock_service_overrides */); |
| ASSERT_TRUE(access_token_store_.get()); |
| ASSERT_TRUE(arbitrator_); |
| @@ -243,13 +254,12 @@ TEST_F(GeolocationLocationArbitratorTest, NormalUsage) { |
| } |
| TEST_F(GeolocationLocationArbitratorTest, CustomSystemProviderOnly) { |
| - override_content_browser_client_->set_use_network(false); |
| - SetBrowserClientForTesting(override_content_browser_client_.get()); |
| + InitializeArbitrator(true /* use_mock_service_overrides */); |
| + arbitrator_->GetServiceOverrides()->set_use_network(false); |
| ASSERT_TRUE(arbitrator_); |
| EXPECT_FALSE(cell()); |
| EXPECT_FALSE(gps()); |
| - EXPECT_FALSE(GetSystemLocationProviderOverride()); |
| arbitrator_->StartProviders(false); |
| ASSERT_FALSE(cell()); |
| @@ -277,13 +287,12 @@ TEST_F(GeolocationLocationArbitratorTest, CustomSystemProviderOnly) { |
| TEST_F(GeolocationLocationArbitratorTest, |
| CustomSystemAndDefaultNetworkProviders) { |
| - override_content_browser_client_->set_use_network(true); |
| - content::SetBrowserClientForTesting(override_content_browser_client_.get()); |
| + InitializeArbitrator(true /* use_mock_service_overrides */); |
| + arbitrator_->GetServiceOverrides()->set_use_network(true); |
| ASSERT_TRUE(arbitrator_); |
| EXPECT_FALSE(cell()); |
| EXPECT_FALSE(gps()); |
| - EXPECT_FALSE(GetSystemLocationProviderOverride()); |
| arbitrator_->StartProviders(false); |
| EXPECT_TRUE(access_token_store_->access_token_map_.empty()); |
| @@ -314,6 +323,7 @@ TEST_F(GeolocationLocationArbitratorTest, |
| } |
| TEST_F(GeolocationLocationArbitratorTest, SetObserverOptions) { |
| + InitializeArbitrator(false /* use_mock_service_overrides */); |
| arbitrator_->StartProviders(false); |
| access_token_store_->NotifyDelegateTokensLoaded(); |
| ASSERT_TRUE(cell()); |
| @@ -331,6 +341,7 @@ TEST_F(GeolocationLocationArbitratorTest, SetObserverOptions) { |
| } |
| TEST_F(GeolocationLocationArbitratorTest, Arbitration) { |
| + InitializeArbitrator(false /* use_mock_service_overrides */); |
| arbitrator_->StartProviders(false); |
| access_token_store_->NotifyDelegateTokensLoaded(); |
| ASSERT_TRUE(cell()); |
| @@ -408,6 +419,7 @@ TEST_F(GeolocationLocationArbitratorTest, Arbitration) { |
| } |
| TEST_F(GeolocationLocationArbitratorTest, TwoOneShotsIsNewPositionBetter) { |
| + InitializeArbitrator(false /* use_mock_service_overrides */); |
| arbitrator_->StartProviders(false); |
| access_token_store_->NotifyDelegateTokensLoaded(); |
| ASSERT_TRUE(cell()); |