Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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 "device/geolocation/geolocation_provider_impl.h" | 5 #include "device/geolocation/geolocation_provider_impl.h" |
| 6 | 6 |
| 7 #include <memory> | 7 #include <memory> |
| 8 | 8 |
| 9 #include "base/at_exit.h" | 9 #include "base/at_exit.h" |
| 10 #include "base/bind.h" | 10 #include "base/bind.h" |
| 11 #include "base/bind_helpers.h" | 11 #include "base/bind_helpers.h" |
| 12 #include "base/location.h" | 12 #include "base/location.h" |
| 13 #include "base/macros.h" | 13 #include "base/macros.h" |
| 14 #include "base/memory/ptr_util.h" | 14 #include "base/memory/ptr_util.h" |
| 15 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
| 16 #include "base/run_loop.h" | 16 #include "base/run_loop.h" |
| 17 #include "base/single_thread_task_runner.h" | 17 #include "base/single_thread_task_runner.h" |
| 18 #include "base/strings/string16.h" | 18 #include "base/strings/string16.h" |
| 19 #include "base/time/time.h" | 19 #include "base/time/time.h" |
| 20 #include "device/geolocation/access_token_store.h" | 20 #include "device/geolocation/access_token_store.h" |
| 21 #include "device/geolocation/mock_location_provider.h" | 21 #include "device/geolocation/fake_location_provider.h" |
| 22 #include "testing/gmock/include/gmock/gmock.h" | 22 #include "testing/gmock/include/gmock/gmock.h" |
| 23 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
| 24 | 24 |
| 25 using testing::MakeMatcher; | 25 using testing::MakeMatcher; |
| 26 using testing::Matcher; | 26 using testing::Matcher; |
| 27 using testing::MatcherInterface; | 27 using testing::MatcherInterface; |
| 28 using testing::MatchResultListener; | 28 using testing::MatchResultListener; |
| 29 | 29 |
| 30 namespace device { | 30 namespace device { |
| 31 namespace { | 31 namespace { |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 91 return MakeMatcher(new GeopositionEqMatcher(expected)); | 91 return MakeMatcher(new GeopositionEqMatcher(expected)); |
| 92 } | 92 } |
| 93 | 93 |
| 94 void DummyFunction(const LocationProvider* provider, | 94 void DummyFunction(const LocationProvider* provider, |
| 95 const Geoposition& position) {} | 95 const Geoposition& position) {} |
| 96 | 96 |
| 97 } // namespace | 97 } // namespace |
| 98 | 98 |
| 99 class GeolocationProviderTest : public testing::Test { | 99 class GeolocationProviderTest : public testing::Test { |
| 100 protected: | 100 protected: |
| 101 GeolocationProviderTest() : arbitrator_(new MockLocationProvider) { | 101 GeolocationProviderTest() : arbitrator_(new FakeLocationProvider) { |
| 102 provider()->SetArbitratorForTesting(base::WrapUnique(arbitrator_)); | 102 provider()->SetArbitratorForTesting(base::WrapUnique(arbitrator_)); |
| 103 } | 103 } |
| 104 | 104 |
| 105 ~GeolocationProviderTest() override {} | 105 ~GeolocationProviderTest() override {} |
| 106 | 106 |
| 107 GeolocationProviderImpl* provider() { | 107 GeolocationProviderImpl* provider() { |
| 108 return GeolocationProviderImpl::GetInstance(); | 108 return GeolocationProviderImpl::GetInstance(); |
| 109 } | 109 } |
| 110 | 110 |
| 111 MockLocationProvider* arbitrator() { return arbitrator_; } | 111 FakeLocationProvider* arbitrator() { return arbitrator_; } |
| 112 | 112 |
| 113 // Called on test thread. | 113 // Called on test thread. |
| 114 bool ProvidersStarted(); | 114 bool ProvidersStarted(); |
| 115 void SendMockLocation(const Geoposition& position); | 115 void SendMockLocation(const Geoposition& position); |
| 116 | 116 |
| 117 private: | 117 private: |
| 118 // Called on provider thread. | 118 // Called on provider thread. |
| 119 void GetProvidersStarted(bool* started); | 119 void GetProvidersStarted(); |
| 120 | 120 |
| 121 // |at_exit| must be initialized before all other variables so that it is | 121 // |at_exit| must be initialized before all other variables so that it is |
| 122 // available to register with Singletons and can handle tear down when the | 122 // available to register with Singletons and can handle tear down when the |
| 123 // test completes. | 123 // test completes. |
| 124 base::ShadowingAtExitManager at_exit_; | 124 base::ShadowingAtExitManager at_exit_; |
| 125 | 125 |
| 126 base::MessageLoopForUI message_loop_; | 126 base::MessageLoopForUI message_loop_; |
| 127 | 127 |
| 128 // Owned by the GeolocationProviderImpl class. | 128 // Owned by the GeolocationProviderImpl class. |
| 129 MockLocationProvider* arbitrator_; | 129 FakeLocationProvider* arbitrator_; |
| 130 | |
| 131 // True if |arbitrator_| is started. | |
| 132 bool started_; | |
|
Wez
2016/08/24 04:25:50
nit: Prefer is_started_ in cases like this.
CJ
2016/08/24 20:37:10
Done.
| |
| 130 | 133 |
| 131 DISALLOW_COPY_AND_ASSIGN(GeolocationProviderTest); | 134 DISALLOW_COPY_AND_ASSIGN(GeolocationProviderTest); |
| 132 }; | 135 }; |
| 133 | 136 |
| 134 bool GeolocationProviderTest::ProvidersStarted() { | 137 bool GeolocationProviderTest::ProvidersStarted() { |
| 135 DCHECK(provider()->IsRunning()); | 138 DCHECK(provider()->IsRunning()); |
| 136 DCHECK(base::MessageLoop::current() == &message_loop_); | 139 DCHECK(base::MessageLoop::current() == &message_loop_); |
| 137 | 140 |
| 138 bool started; | |
| 139 provider()->task_runner()->PostTaskAndReply( | 141 provider()->task_runner()->PostTaskAndReply( |
| 140 FROM_HERE, base::Bind(&GeolocationProviderTest::GetProvidersStarted, | 142 FROM_HERE, base::Bind(&GeolocationProviderTest::GetProvidersStarted, |
| 141 base::Unretained(this), &started), | 143 base::Unretained(this)), |
| 142 base::MessageLoop::QuitWhenIdleClosure()); | 144 base::MessageLoop::QuitWhenIdleClosure()); |
| 143 base::RunLoop().Run(); | 145 base::RunLoop().Run(); |
| 144 return started; | 146 return started_; |
| 145 } | 147 } |
| 146 | 148 |
| 147 void GeolocationProviderTest::GetProvidersStarted(bool* started) { | 149 void GeolocationProviderTest::GetProvidersStarted() { |
| 148 DCHECK(provider()->task_runner()->BelongsToCurrentThread()); | 150 DCHECK(provider()->task_runner()->BelongsToCurrentThread()); |
| 149 *started = arbitrator()->is_started(); | 151 started_ = arbitrator()->state() != FakeLocationProvider::STOPPED; |
| 150 } | 152 } |
| 151 | 153 |
| 152 void GeolocationProviderTest::SendMockLocation(const Geoposition& position) { | 154 void GeolocationProviderTest::SendMockLocation(const Geoposition& position) { |
| 153 DCHECK(provider()->IsRunning()); | 155 DCHECK(provider()->IsRunning()); |
| 154 DCHECK(base::MessageLoop::current() == &message_loop_); | 156 DCHECK(base::MessageLoop::current() == &message_loop_); |
| 155 provider()->task_runner()->PostTask( | 157 provider()->task_runner()->PostTask( |
| 156 FROM_HERE, | 158 FROM_HERE, |
| 157 base::Bind(&GeolocationProviderImpl::OnLocationUpdate, | 159 base::Bind(&GeolocationProviderImpl::OnLocationUpdate, |
| 158 base::Unretained(provider()), arbitrator_, position)); | 160 base::Unretained(provider()), arbitrator_, position)); |
| 159 } | 161 } |
| 160 | 162 |
| 161 // Regression test for http://crbug.com/59377 | 163 // Regression test for http://crbug.com/59377 |
| 162 TEST_F(GeolocationProviderTest, OnPermissionGrantedWithoutObservers) { | 164 TEST_F(GeolocationProviderTest, OnPermissionGrantedWithoutObservers) { |
| 163 // Clear |provider|'s arbitrator so the default arbitrator can be used. | 165 // Clear |provider|'s arbitrator so the default arbitrator can be used. |
| 164 provider()->SetArbitratorForTesting(nullptr); | 166 provider()->SetArbitratorForTesting(nullptr); |
| 165 EXPECT_FALSE(provider()->user_did_opt_into_location_services_for_testing()); | 167 EXPECT_FALSE(provider()->user_did_opt_into_location_services_for_testing()); |
| 166 provider()->UserDidOptIntoLocationServices(); | 168 provider()->UserDidOptIntoLocationServices(); |
| 167 EXPECT_TRUE(provider()->user_did_opt_into_location_services_for_testing()); | 169 EXPECT_TRUE(provider()->user_did_opt_into_location_services_for_testing()); |
| 168 } | 170 } |
| 169 | 171 |
| 170 TEST_F(GeolocationProviderTest, StartStop) { | 172 TEST_F(GeolocationProviderTest, StartStop) { |
| 171 EXPECT_FALSE(provider()->IsRunning()); | 173 EXPECT_FALSE(provider()->IsRunning()); |
| 172 LocationProvider::LocationProviderUpdateCallback callback = | 174 LocationProvider::LocationProviderUpdateCallback callback = |
| 173 base::Bind(&DummyFunction); | 175 base::Bind(&DummyFunction); |
|
Wez
2016/08/24 04:25:50
No need for this line; just pass &DummyFunction di
CJ
2016/08/24 20:37:10
Done.
| |
| 174 std::unique_ptr<GeolocationProvider::Subscription> subscription = | 176 std::unique_ptr<GeolocationProvider::Subscription> subscription = |
| 175 provider()->AddLocationUpdateCallback(base::Bind(callback, arbitrator()), | 177 provider()->AddLocationUpdateCallback(base::Bind(callback, arbitrator()), |
| 176 false); | 178 false); |
| 177 EXPECT_TRUE(provider()->IsRunning()); | 179 EXPECT_TRUE(provider()->IsRunning()); |
| 178 EXPECT_TRUE(ProvidersStarted()); | 180 EXPECT_TRUE(ProvidersStarted()); |
| 179 | 181 |
| 180 subscription.reset(); | 182 subscription.reset(); |
| 181 | 183 |
| 182 EXPECT_FALSE(ProvidersStarted()); | 184 EXPECT_FALSE(ProvidersStarted()); |
| 183 EXPECT_TRUE(provider()->IsRunning()); | 185 EXPECT_TRUE(provider()->IsRunning()); |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 242 base::Bind(&MockGeolocationObserver::OnLocationUpdate, | 244 base::Bind(&MockGeolocationObserver::OnLocationUpdate, |
| 243 base::Unretained(&mock_observer)); | 245 base::Unretained(&mock_observer)); |
| 244 std::unique_ptr<GeolocationProvider::Subscription> subscription = | 246 std::unique_ptr<GeolocationProvider::Subscription> subscription = |
| 245 provider()->AddLocationUpdateCallback(callback, false); | 247 provider()->AddLocationUpdateCallback(callback, false); |
| 246 subscription.reset(); | 248 subscription.reset(); |
| 247 // Wait for the providers to be stopped now that all clients are gone. | 249 // Wait for the providers to be stopped now that all clients are gone. |
| 248 EXPECT_FALSE(ProvidersStarted()); | 250 EXPECT_FALSE(ProvidersStarted()); |
| 249 } | 251 } |
| 250 | 252 |
| 251 } // namespace device | 253 } // namespace device |
| OLD | NEW |