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..b52ead4357982bdde38377dfdbf4c082b15e001e 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(base::WrapUnique(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,25 @@ class GeolocationProviderTest : public testing::Test { |
// Called on provider thread. |
void GetProvidersStarted(bool* started); |
+ // |at_exit| must be initialized before all other variables so that it is |
+ // available to register with Singletons and can handle tear down when the |
+ // test completes. |
+ base::AtExitManager at_exit_; |
+ |
+ // |message_loop_| must come next to include all other variable construction |
Kevin M
2016/08/15 19:12:46
Relative statements like "come next to" is confusi
CJ
2016/08/15 20:11:51
Done.
|
+ // on the correct thread. |
base::MessageLoopForUI message_loop_; |
- std::unique_ptr<LocationProviderForTestArbitrator> provider_; |
+ |
+ // Owned by the GeolocationProviderImpl class. |
+ MockLocationProvider* arbitrator_; |
Kevin M
2016/08/15 19:12:46
DISALLOW_COPY_AND_ASSIGN
CJ
2016/08/15 20:11:51
Done.
|
}; |
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 +144,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 = |