Chromium Code Reviews| Index: device/geolocation/geolocation_provider_impl_unittest.cc |
| diff --git a/device/geolocation/geolocation_provider_impl_unittest.cc b/device/geolocation/geolocation_provider_impl_unittest.cc |
| index 5eb1af9d34ede9a699fdc386c768015e4d035216..d99787f3a06752e53f711b96899a3195e8bdc7be 100644 |
| --- a/device/geolocation/geolocation_provider_impl_unittest.cc |
| +++ b/device/geolocation/geolocation_provider_impl_unittest.cc |
| @@ -6,6 +6,7 @@ |
| #include <memory> |
| +#include "base/at_exit.h" |
| #include "base/bind.h" |
| #include "base/bind_helpers.h" |
| #include "base/location.h" |
| @@ -17,7 +18,7 @@ |
| #include "base/strings/string16.h" |
| #include "base/time/time.h" |
| #include "device/geolocation/access_token_store.h" |
| -#include "device/geolocation/mock_location_arbitrator.h" |
| +#include "device/geolocation/mock_location_provider.h" |
| #include "testing/gmock/include/gmock/gmock.h" |
| #include "testing/gtest/include/gtest/gtest.h" |
| @@ -27,30 +28,7 @@ using testing::MatcherInterface; |
| using testing::MatchResultListener; |
| namespace device { |
| - |
| -class LocationProviderForTestArbitrator : public GeolocationProviderImpl { |
| - public: |
| - LocationProviderForTestArbitrator() : mock_arbitrator_(NULL) {} |
| - ~LocationProviderForTestArbitrator() override {} |
| - |
| - // Only valid for use on the geolocation thread. |
| - MockLocationArbitrator* mock_arbitrator() const { return mock_arbitrator_; } |
| - |
| - protected: |
| - // GeolocationProviderImpl implementation: |
| - std::unique_ptr<LocationArbitrator> CreateArbitrator() override; |
| - |
| - private: |
| - // An alias to the arbitrator stored in the super class, where it is owned. |
| - MockLocationArbitrator* mock_arbitrator_; |
| -}; |
| - |
| -std::unique_ptr<LocationArbitrator> |
| -LocationProviderForTestArbitrator::CreateArbitrator() { |
| - DCHECK(mock_arbitrator_ == NULL); |
| - mock_arbitrator_ = new MockLocationArbitrator; |
| - return base::WrapUnique(mock_arbitrator_); |
| -} |
| +namespace { |
| class GeolocationObserver { |
| public: |
| @@ -113,14 +91,23 @@ Matcher<const Geoposition&> GeopositionEq(const Geoposition& expected) { |
| return MakeMatcher(new GeopositionEqMatcher(expected)); |
| } |
| +void DummyFunction(const Geoposition& position) {} |
| + |
| +} // namespace |
| + |
| class GeolocationProviderTest : public testing::Test { |
| protected: |
| - GeolocationProviderTest() |
| - : message_loop_(), provider_(new LocationProviderForTestArbitrator) {} |
| + GeolocationProviderTest() : arbitrator_(new MockLocationProvider) { |
| + provider()->SetArbitratorForTesting(arbitrator_); |
| + } |
| ~GeolocationProviderTest() override {} |
| - LocationProviderForTestArbitrator* provider() { return provider_.get(); } |
| + GeolocationProviderImpl* provider() { |
| + return GeolocationProviderImpl::GetInstance(); |
| + } |
| + |
| + MockLocationProvider* arbitrator() { return arbitrator_; } |
| // Called on test thread. |
| bool ProvidersStarted(); |
| @@ -130,15 +117,23 @@ class GeolocationProviderTest : public testing::Test { |
| // Called on provider thread. |
| void GetProvidersStarted(bool* started); |
| + // |at_exit| must be initialized before all other variables to handle |
| + // correctly reinitializing GeolocationProviderImpl (Singleton) |
|
Wez
2016/08/12 00:33:44
It needs to be before all other variables so that
CJ
2016/08/12 20:22:46
Done.
|
| + // |message_loop_| must come next to include all other variable construction |
| + // on the correct thread. |
|
Wez
2016/08/12 00:33:44
nit: Move this comment down to the message_loop_ m
CJ
2016/08/12 20:22:46
Done.
|
| + base::AtExitManager at_exit_; |
| base::MessageLoopForUI message_loop_; |
| - std::unique_ptr<LocationProviderForTestArbitrator> provider_; |
| + |
| + // Owned by the GeolocationProviderImpl class. |
| + MockLocationProvider* arbitrator_; |
| }; |
| bool GeolocationProviderTest::ProvidersStarted() { |
| - DCHECK(provider_->IsRunning()); |
| + DCHECK(provider()->IsRunning()); |
| DCHECK(base::MessageLoop::current() == &message_loop_); |
| + |
| bool started; |
| - provider_->task_runner()->PostTaskAndReply( |
| + provider()->task_runner()->PostTaskAndReply( |
| FROM_HERE, base::Bind(&GeolocationProviderTest::GetProvidersStarted, |
| base::Unretained(this), &started), |
| base::MessageLoop::QuitWhenIdleClosure()); |
| @@ -147,27 +142,26 @@ bool GeolocationProviderTest::ProvidersStarted() { |
| } |
| void GeolocationProviderTest::GetProvidersStarted(bool* started) { |
| - DCHECK(provider_->task_runner()->BelongsToCurrentThread()); |
| - *started = provider_->mock_arbitrator()->providers_started(); |
| + DCHECK(provider()->task_runner()->BelongsToCurrentThread()); |
| + *started = arbitrator()->providers_started(); |
| } |
| void GeolocationProviderTest::SendMockLocation(const Geoposition& position) { |
| - DCHECK(provider_->IsRunning()); |
| + DCHECK(provider()->IsRunning()); |
| DCHECK(base::MessageLoop::current() == &message_loop_); |
| - provider_->task_runner()->PostTask( |
| + provider()->task_runner()->PostTask( |
| FROM_HERE, base::Bind(&GeolocationProviderImpl::OnLocationUpdate, |
| - base::Unretained(provider_.get()), position)); |
| + base::Unretained(provider()), position)); |
| } |
| // Regression test for http://crbug.com/59377 |
| TEST_F(GeolocationProviderTest, OnPermissionGrantedWithoutObservers) { |
| + provider()->SetArbitratorForTesting(nullptr); |
| EXPECT_FALSE(provider()->user_did_opt_into_location_services_for_testing()); |
| provider()->UserDidOptIntoLocationServices(); |
| EXPECT_TRUE(provider()->user_did_opt_into_location_services_for_testing()); |
| } |
| -void DummyFunction(const Geoposition& position) {} |
| - |
| TEST_F(GeolocationProviderTest, StartStop) { |
| EXPECT_FALSE(provider()->IsRunning()); |
| GeolocationProviderImpl::LocationUpdateCallback callback = |