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 93b1645b65aed37fc912f56497b2177aa805be65..4caccae5eb6f2d95287d4118f906462eb945aa86 100644 |
| --- a/content/browser/geolocation/location_arbitrator_impl_unittest.cc |
| +++ b/content/browser/geolocation/location_arbitrator_impl_unittest.cc |
| @@ -10,6 +10,7 @@ |
| #include "content/browser/geolocation/fake_access_token_store.h" |
| #include "content/browser/geolocation/mock_location_provider.h" |
| #include "content/public/common/geoposition.h" |
| +#include "content/test/test_content_browser_client.h" |
| #include "testing/gmock/include/gmock/gmock.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| @@ -64,15 +65,35 @@ void SetReferencePosition(MockLocationProvider* provider) { |
| namespace { |
| +class OverrideContentBrowserClient : public TestContentBrowserClient { |
|
Michael van Ouwerkerk
2016/06/09 13:03:11
The name of this class trips me up a bit, as I rea
CJ
2016/06/09 20:31:50
Done.
|
| + public: |
| + void SetUseNetwork(bool use_network) { |
| + use_network_ = use_network; |
| + } |
| + |
| + LocationProvider* OverrideSystemLocationProvider() override { |
| + provider_ = new MockLocationProvider; |
| + return provider_; |
| + } |
| + |
| + bool UseNetworkLocationProviders() override { |
| + return use_network_; |
| + } |
| + |
| + MockLocationProvider* provider_ = nullptr; |
| + private: |
|
Michael van Ouwerkerk
2016/06/09 13:03:10
nit: I would expect to see a blank line before "pr
CJ
2016/06/09 20:31:50
Done.
|
| + bool use_network_ = true; |
| +}; |
| + |
| class TestingLocationArbitrator : public LocationArbitratorImpl { |
| public: |
| TestingLocationArbitrator( |
| const LocationArbitratorImpl::LocationUpdateCallback& callback, |
| AccessTokenStore* access_token_store) |
| : LocationArbitratorImpl(callback), |
| - cell_(NULL), |
| - gps_(NULL), |
| access_token_store_(access_token_store) { |
| + cell_ = nullptr; |
|
Michael van Ouwerkerk
2016/06/09 13:03:10
Why move these inside the constructor body? Genera
CJ
2016/06/09 20:31:50
Done.
|
| + gps_ = nullptr; |
| } |
| base::Time GetTimeNow() const override { return GetTimeNowForTest(); } |
| @@ -86,17 +107,20 @@ class TestingLocationArbitrator : public LocationArbitratorImpl { |
| net::URLRequestContextGetter* context, |
| const GURL& url, |
| const base::string16& access_token) override { |
| - return new MockLocationProvider(&cell_); |
| + cell_ = new MockLocationProvider; |
| + return cell_; |
| } |
| LocationProvider* NewSystemLocationProvider() override { |
| - return new MockLocationProvider(&gps_); |
| + gps_ = new MockLocationProvider; |
| + return gps_; |
| } |
| - // Two location providers, with nice short names to make the tests more |
| + // Three location providers, with nice short names to make the tests more |
|
Michael van Ouwerkerk
2016/06/09 13:03:11
Just two again.
CJ
2016/06/09 20:31:50
Done.
|
| // 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 |
| - // type). |
| + // type). |override_| will be included only if the ContentBrowser specifies |
|
Michael van Ouwerkerk
2016/06/09 13:03:10
|override_| is gone again
CJ
2016/06/09 20:31:50
Done.
|
| + // for it. |
| MockLocationProvider* cell_; |
| MockLocationProvider* gps_; |
| scoped_refptr<AccessTokenStore> access_token_store_; |
| @@ -115,6 +139,7 @@ class GeolocationLocationArbitratorTest : public testing::Test { |
| base::Unretained(observer_.get())); |
| arbitrator_.reset(new TestingLocationArbitrator( |
| callback, access_token_store_.get())); |
| + override_content_browser_client_.reset(new OverrideContentBrowserClient()); |
| } |
| // testing::Test |
| @@ -144,10 +169,16 @@ class GeolocationLocationArbitratorTest : public testing::Test { |
| return arbitrator_->gps_; |
| } |
| + MockLocationProvider* overrider() { |
|
Michael van Ouwerkerk
2016/06/09 13:03:11
Two things here:
1) This is not a simple member ge
CJ
2016/06/09 20:31:50
Done.
|
| + return override_content_browser_client_->provider_; |
| + } |
| + |
| scoped_refptr<FakeAccessTokenStore> access_token_store_; |
| std::unique_ptr<MockLocationObserver> observer_; |
| std::unique_ptr<TestingLocationArbitrator> arbitrator_; |
| base::MessageLoop loop_; |
| + std::unique_ptr<OverrideContentBrowserClient> |
| + override_content_browser_client_; |
| }; |
| TEST_F(GeolocationLocationArbitratorTest, CreateDestroy) { |
| @@ -165,6 +196,7 @@ TEST_F(GeolocationLocationArbitratorTest, OnPermissionGranted) { |
| // motions to create the provider (see next test). |
| EXPECT_FALSE(cell()); |
| EXPECT_FALSE(gps()); |
| + EXPECT_FALSE(overrider()); |
| } |
| TEST_F(GeolocationLocationArbitratorTest, NormalUsage) { |
| @@ -173,6 +205,7 @@ TEST_F(GeolocationLocationArbitratorTest, NormalUsage) { |
| EXPECT_FALSE(cell()); |
| EXPECT_FALSE(gps()); |
| + EXPECT_FALSE(overrider()); |
| arbitrator_->StartProviders(false); |
| EXPECT_TRUE(access_token_store_->access_token_map_.empty()); |
| @@ -181,6 +214,7 @@ TEST_F(GeolocationLocationArbitratorTest, NormalUsage) { |
| access_token_store_->NotifyDelegateTokensLoaded(); |
| ASSERT_TRUE(cell()); |
| EXPECT_TRUE(gps()); |
| + EXPECT_FALSE(overrider()); |
| EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, cell()->state_); |
| EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, gps()->state_); |
| EXPECT_FALSE(observer_->last_position_.Validate()); |
| @@ -202,11 +236,84 @@ TEST_F(GeolocationLocationArbitratorTest, NormalUsage) { |
| EXPECT_TRUE(cell()->is_permission_granted_); |
| } |
| +TEST_F(GeolocationLocationArbitratorTest, OverrideNoNetworkUsage) { |
| + override_content_browser_client_->SetUseNetwork(false); |
| + content::SetBrowserClientForTesting(override_content_browser_client_.get()); |
|
Michael van Ouwerkerk
2016/06/09 13:03:11
nit: I think there's no need for "content::" as we
CJ
2016/06/09 20:31:50
Done.
|
| + ASSERT_TRUE(arbitrator_ != NULL); |
|
Michael van Ouwerkerk
2016/06/09 13:03:11
nit: use nullptr in new code, but even simpler to
CJ
2016/06/09 20:31:50
Done.
|
| + |
| + EXPECT_FALSE(cell()); |
| + EXPECT_FALSE(gps()); |
| + EXPECT_FALSE(overrider()); |
| + arbitrator_->StartProviders(false); |
| + |
| + ASSERT_FALSE(cell()); |
| + EXPECT_FALSE(gps()); |
| + EXPECT_TRUE(overrider()); |
| + EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, overrider()->state_); |
| + EXPECT_FALSE(observer_->last_position_.Validate()); |
| + EXPECT_EQ(Geoposition::ERROR_CODE_NONE, |
| + observer_->last_position_.error_code); |
| + |
| + SetReferencePosition(overrider()); |
| + |
| + EXPECT_TRUE(observer_->last_position_.Validate() || |
| + observer_->last_position_.error_code != |
| + Geoposition::ERROR_CODE_NONE); |
| + EXPECT_EQ(overrider()->position_.latitude, |
| + observer_->last_position_.latitude); |
| + |
| + EXPECT_FALSE(overrider()->is_permission_granted_); |
| + EXPECT_FALSE(arbitrator_->HasPermissionBeenGranted()); |
| + arbitrator_->OnPermissionGranted(); |
| + EXPECT_TRUE(arbitrator_->HasPermissionBeenGranted()); |
| + EXPECT_TRUE(overrider()->is_permission_granted_); |
| +} |
| + |
| +TEST_F(GeolocationLocationArbitratorTest, OverrideAndNetworkUsage) { |
| + override_content_browser_client_->SetUseNetwork(true); |
| + content::SetBrowserClientForTesting(override_content_browser_client_.get()); |
|
Michael van Ouwerkerk
2016/06/09 13:03:11
nit: I think there's no need for "content::" as we
CJ
2016/06/09 20:31:50
Done.
|
| + ASSERT_TRUE(arbitrator_ != NULL); |
|
Michael van Ouwerkerk
2016/06/09 13:03:10
nit: use nullptr in new code, but even simpler to
CJ
2016/06/09 20:31:50
Done.
|
| + |
| + EXPECT_FALSE(cell()); |
| + EXPECT_FALSE(gps()); |
| + EXPECT_FALSE(overrider()); |
| + arbitrator_->StartProviders(false); |
| + |
| + EXPECT_TRUE(access_token_store_->access_token_map_.empty()); |
| + EXPECT_TRUE(access_token_store_->access_token_map_.empty()); |
|
Michael van Ouwerkerk
2016/06/09 13:03:11
delete this duplicate line
CJ
2016/06/09 20:31:50
Was wondering about it since it was in the origina
|
| + |
| + access_token_store_->NotifyDelegateTokensLoaded(); |
| + |
| + ASSERT_TRUE(cell()); |
| + EXPECT_FALSE(gps()); |
| + EXPECT_TRUE(overrider()); |
| + EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, overrider()->state_); |
| + EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, cell()->state_); |
| + EXPECT_FALSE(observer_->last_position_.Validate()); |
| + EXPECT_EQ(Geoposition::ERROR_CODE_NONE, |
| + observer_->last_position_.error_code); |
| + |
| + SetReferencePosition(cell()); |
| + |
| + EXPECT_TRUE(observer_->last_position_.Validate() || |
| + observer_->last_position_.error_code != |
| + Geoposition::ERROR_CODE_NONE); |
| + EXPECT_EQ(cell()->position_.latitude, |
| + observer_->last_position_.latitude); |
| + |
| + EXPECT_FALSE(cell()->is_permission_granted_); |
| + EXPECT_FALSE(arbitrator_->HasPermissionBeenGranted()); |
| + arbitrator_->OnPermissionGranted(); |
| + EXPECT_TRUE(arbitrator_->HasPermissionBeenGranted()); |
| + EXPECT_TRUE(cell()->is_permission_granted_); |
| +} |
| + |
| TEST_F(GeolocationLocationArbitratorTest, SetObserverOptions) { |
| arbitrator_->StartProviders(false); |
| access_token_store_->NotifyDelegateTokensLoaded(); |
| ASSERT_TRUE(cell()); |
| ASSERT_TRUE(gps()); |
| + EXPECT_FALSE(overrider()); |
| EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, cell()->state_); |
| EXPECT_EQ(MockLocationProvider::LOW_ACCURACY, gps()->state_); |
| SetReferencePosition(cell()); |
| @@ -215,6 +322,7 @@ TEST_F(GeolocationLocationArbitratorTest, SetObserverOptions) { |
| arbitrator_->StartProviders(true); |
| EXPECT_EQ(MockLocationProvider::HIGH_ACCURACY, cell()->state_); |
| EXPECT_EQ(MockLocationProvider::HIGH_ACCURACY, gps()->state_); |
| + EXPECT_FALSE(overrider()); |
| } |
| TEST_F(GeolocationLocationArbitratorTest, Arbitration) { |
| @@ -222,6 +330,7 @@ TEST_F(GeolocationLocationArbitratorTest, Arbitration) { |
| access_token_store_->NotifyDelegateTokensLoaded(); |
| ASSERT_TRUE(cell()); |
| ASSERT_TRUE(gps()); |
| + EXPECT_FALSE(overrider()); |
| SetPositionFix(cell(), 1, 2, 150); |
| @@ -266,9 +375,7 @@ TEST_F(GeolocationLocationArbitratorTest, Arbitration) { |
| AdvanceTimeNow(base::TimeDelta::FromMinutes(5)); |
| SetPositionFix(gps(), 3.5679026, 139.634777, 300); |
| CheckLastPositionInfo(3.5679026, 139.634777, 300); |
| - |
| - // 14 minutes later |
| - AdvanceTimeNow(base::TimeDelta::FromMinutes(14)); |
| + // 14 minutes later AdvanceTimeNow(base::TimeDelta::FromMinutes(14)); |
|
Michael van Ouwerkerk
2016/06/09 13:03:10
Did you intend to comment out this line?
CJ
2016/06/09 20:31:50
Done.
|
| // GPS reading misses a beat, but don't switch to cell yet to avoid |
| // oscillating. |
| @@ -298,6 +405,7 @@ TEST_F(GeolocationLocationArbitratorTest, TwoOneShotsIsNewPositionBetter) { |
| access_token_store_->NotifyDelegateTokensLoaded(); |
| ASSERT_TRUE(cell()); |
| ASSERT_TRUE(gps()); |
| + EXPECT_FALSE(overrider()); |
| // Set the initial position. |
| SetPositionFix(cell(), 3, 139, 100); |
| @@ -308,9 +416,8 @@ TEST_F(GeolocationLocationArbitratorTest, TwoOneShotsIsNewPositionBetter) { |
| // To test 240956, perform a throwaway alloc. |
| // This convinces the allocator to put the providers in a new memory location. |
| - MockLocationProvider* fakeMockProvider = NULL; |
| - LocationProvider* fakeProvider = |
| - new MockLocationProvider(&fakeMockProvider); |
| + std::unique_ptr<MockLocationProvider> fakeMockProvider( |
| + new MockLocationProvider); |
| arbitrator_->StartProviders(false); |
| access_token_store_->NotifyDelegateTokensLoaded(); |
| @@ -321,9 +428,6 @@ TEST_F(GeolocationLocationArbitratorTest, TwoOneShotsIsNewPositionBetter) { |
| // Update with a less accurate position to verify 240956. |
| SetPositionFix(cell(), 3, 139, 150); |
| CheckLastPositionInfo(3, 139, 150); |
| - |
| - // No delete required for fakeMockProvider. It points to fakeProvider. |
| - delete fakeProvider; |
| } |
| } // namespace content |