| 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 "content/browser/geolocation/geolocation_provider_impl.h" |
| 6 |
| 7 #include <memory> |
| 8 |
| 5 #include "base/bind.h" | 9 #include "base/bind.h" |
| 6 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| 7 #include "base/location.h" | 11 #include "base/location.h" |
| 8 #include "base/macros.h" | 12 #include "base/macros.h" |
| 13 #include "base/memory/ptr_util.h" |
| 9 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 10 #include "base/memory/scoped_ptr.h" | |
| 11 #include "base/single_thread_task_runner.h" | 15 #include "base/single_thread_task_runner.h" |
| 12 #include "base/strings/string16.h" | 16 #include "base/strings/string16.h" |
| 13 #include "base/time/time.h" | 17 #include "base/time/time.h" |
| 14 #include "content/browser/geolocation/geolocation_provider_impl.h" | |
| 15 #include "content/browser/geolocation/mock_location_arbitrator.h" | 18 #include "content/browser/geolocation/mock_location_arbitrator.h" |
| 16 #include "content/public/browser/access_token_store.h" | 19 #include "content/public/browser/access_token_store.h" |
| 17 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
| 18 #include "content/public/test/test_browser_thread.h" | 21 #include "content/public/test/test_browser_thread.h" |
| 19 #include "testing/gmock/include/gmock/gmock.h" | 22 #include "testing/gmock/include/gmock/gmock.h" |
| 20 #include "testing/gtest/include/gtest/gtest.h" | 23 #include "testing/gtest/include/gtest/gtest.h" |
| 21 | 24 |
| 22 using testing::MakeMatcher; | 25 using testing::MakeMatcher; |
| 23 using testing::Matcher; | 26 using testing::Matcher; |
| 24 using testing::MatcherInterface; | 27 using testing::MatcherInterface; |
| 25 using testing::MatchResultListener; | 28 using testing::MatchResultListener; |
| 26 | 29 |
| 27 namespace content { | 30 namespace content { |
| 28 | 31 |
| 29 class LocationProviderForTestArbitrator : public GeolocationProviderImpl { | 32 class LocationProviderForTestArbitrator : public GeolocationProviderImpl { |
| 30 public: | 33 public: |
| 31 LocationProviderForTestArbitrator() : mock_arbitrator_(NULL) {} | 34 LocationProviderForTestArbitrator() : mock_arbitrator_(NULL) {} |
| 32 ~LocationProviderForTestArbitrator() override {} | 35 ~LocationProviderForTestArbitrator() override {} |
| 33 | 36 |
| 34 // Only valid for use on the geolocation thread. | 37 // Only valid for use on the geolocation thread. |
| 35 MockLocationArbitrator* mock_arbitrator() const { | 38 MockLocationArbitrator* mock_arbitrator() const { |
| 36 return mock_arbitrator_; | 39 return mock_arbitrator_; |
| 37 } | 40 } |
| 38 | 41 |
| 39 protected: | 42 protected: |
| 40 // GeolocationProviderImpl implementation: | 43 // GeolocationProviderImpl implementation: |
| 41 scoped_ptr<LocationArbitrator> CreateArbitrator() override; | 44 std::unique_ptr<LocationArbitrator> CreateArbitrator() override; |
| 42 | 45 |
| 43 private: | 46 private: |
| 44 // An alias to the arbitrator stored in the super class, where it is owned. | 47 // An alias to the arbitrator stored in the super class, where it is owned. |
| 45 MockLocationArbitrator* mock_arbitrator_; | 48 MockLocationArbitrator* mock_arbitrator_; |
| 46 }; | 49 }; |
| 47 | 50 |
| 48 scoped_ptr<LocationArbitrator> | 51 std::unique_ptr<LocationArbitrator> |
| 49 LocationProviderForTestArbitrator::CreateArbitrator() { | 52 LocationProviderForTestArbitrator::CreateArbitrator() { |
| 50 DCHECK(mock_arbitrator_ == NULL); | 53 DCHECK(mock_arbitrator_ == NULL); |
| 51 mock_arbitrator_ = new MockLocationArbitrator; | 54 mock_arbitrator_ = new MockLocationArbitrator; |
| 52 return make_scoped_ptr(mock_arbitrator_); | 55 return base::WrapUnique(mock_arbitrator_); |
| 53 } | 56 } |
| 54 | 57 |
| 55 class GeolocationObserver { | 58 class GeolocationObserver { |
| 56 public: | 59 public: |
| 57 virtual ~GeolocationObserver() {} | 60 virtual ~GeolocationObserver() {} |
| 58 virtual void OnLocationUpdate(const Geoposition& position) = 0; | 61 virtual void OnLocationUpdate(const Geoposition& position) = 0; |
| 59 }; | 62 }; |
| 60 | 63 |
| 61 class MockGeolocationObserver : public GeolocationObserver { | 64 class MockGeolocationObserver : public GeolocationObserver { |
| 62 public: | 65 public: |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 129 // Called on test thread. | 132 // Called on test thread. |
| 130 bool ProvidersStarted(); | 133 bool ProvidersStarted(); |
| 131 void SendMockLocation(const Geoposition& position); | 134 void SendMockLocation(const Geoposition& position); |
| 132 | 135 |
| 133 private: | 136 private: |
| 134 // Called on provider thread. | 137 // Called on provider thread. |
| 135 void GetProvidersStarted(bool* started); | 138 void GetProvidersStarted(bool* started); |
| 136 | 139 |
| 137 base::MessageLoop message_loop_; | 140 base::MessageLoop message_loop_; |
| 138 TestBrowserThread ui_thread_; | 141 TestBrowserThread ui_thread_; |
| 139 scoped_ptr<LocationProviderForTestArbitrator> provider_; | 142 std::unique_ptr<LocationProviderForTestArbitrator> provider_; |
| 140 }; | 143 }; |
| 141 | 144 |
| 142 | 145 |
| 143 bool GeolocationProviderTest::ProvidersStarted() { | 146 bool GeolocationProviderTest::ProvidersStarted() { |
| 144 DCHECK(provider_->IsRunning()); | 147 DCHECK(provider_->IsRunning()); |
| 145 DCHECK(base::MessageLoop::current() == &message_loop_); | 148 DCHECK(base::MessageLoop::current() == &message_loop_); |
| 146 bool started; | 149 bool started; |
| 147 provider_->task_runner()->PostTaskAndReply( | 150 provider_->task_runner()->PostTaskAndReply( |
| 148 FROM_HERE, base::Bind(&GeolocationProviderTest::GetProvidersStarted, | 151 FROM_HERE, base::Bind(&GeolocationProviderTest::GetProvidersStarted, |
| 149 base::Unretained(this), &started), | 152 base::Unretained(this), &started), |
| (...skipping 22 matching lines...) Expand all Loading... |
| 172 EXPECT_TRUE(provider()->user_did_opt_into_location_services_for_testing()); | 175 EXPECT_TRUE(provider()->user_did_opt_into_location_services_for_testing()); |
| 173 } | 176 } |
| 174 | 177 |
| 175 void DummyFunction(const Geoposition& position) { | 178 void DummyFunction(const Geoposition& position) { |
| 176 } | 179 } |
| 177 | 180 |
| 178 TEST_F(GeolocationProviderTest, StartStop) { | 181 TEST_F(GeolocationProviderTest, StartStop) { |
| 179 EXPECT_FALSE(provider()->IsRunning()); | 182 EXPECT_FALSE(provider()->IsRunning()); |
| 180 GeolocationProviderImpl::LocationUpdateCallback callback = | 183 GeolocationProviderImpl::LocationUpdateCallback callback = |
| 181 base::Bind(&DummyFunction); | 184 base::Bind(&DummyFunction); |
| 182 scoped_ptr<content::GeolocationProvider::Subscription> subscription = | 185 std::unique_ptr<content::GeolocationProvider::Subscription> subscription = |
| 183 provider()->AddLocationUpdateCallback(callback, false); | 186 provider()->AddLocationUpdateCallback(callback, false); |
| 184 EXPECT_TRUE(provider()->IsRunning()); | 187 EXPECT_TRUE(provider()->IsRunning()); |
| 185 EXPECT_TRUE(ProvidersStarted()); | 188 EXPECT_TRUE(ProvidersStarted()); |
| 186 | 189 |
| 187 subscription.reset(); | 190 subscription.reset(); |
| 188 | 191 |
| 189 EXPECT_FALSE(ProvidersStarted()); | 192 EXPECT_FALSE(ProvidersStarted()); |
| 190 EXPECT_TRUE(provider()->IsRunning()); | 193 EXPECT_TRUE(provider()->IsRunning()); |
| 191 } | 194 } |
| 192 | 195 |
| 193 TEST_F(GeolocationProviderTest, StalePositionNotSent) { | 196 TEST_F(GeolocationProviderTest, StalePositionNotSent) { |
| 194 Geoposition first_position; | 197 Geoposition first_position; |
| 195 first_position.latitude = 12; | 198 first_position.latitude = 12; |
| 196 first_position.longitude = 34; | 199 first_position.longitude = 34; |
| 197 first_position.accuracy = 56; | 200 first_position.accuracy = 56; |
| 198 first_position.timestamp = base::Time::Now(); | 201 first_position.timestamp = base::Time::Now(); |
| 199 | 202 |
| 200 AsyncMockGeolocationObserver first_observer; | 203 AsyncMockGeolocationObserver first_observer; |
| 201 GeolocationProviderImpl::LocationUpdateCallback first_callback = base::Bind( | 204 GeolocationProviderImpl::LocationUpdateCallback first_callback = base::Bind( |
| 202 &MockGeolocationObserver::OnLocationUpdate, | 205 &MockGeolocationObserver::OnLocationUpdate, |
| 203 base::Unretained(&first_observer)); | 206 base::Unretained(&first_observer)); |
| 204 EXPECT_CALL(first_observer, OnLocationUpdate(GeopositionEq(first_position))); | 207 EXPECT_CALL(first_observer, OnLocationUpdate(GeopositionEq(first_position))); |
| 205 scoped_ptr<content::GeolocationProvider::Subscription> subscription = | 208 std::unique_ptr<content::GeolocationProvider::Subscription> subscription = |
| 206 provider()->AddLocationUpdateCallback(first_callback, false); | 209 provider()->AddLocationUpdateCallback(first_callback, false); |
| 207 SendMockLocation(first_position); | 210 SendMockLocation(first_position); |
| 208 base::MessageLoop::current()->Run(); | 211 base::MessageLoop::current()->Run(); |
| 209 | 212 |
| 210 subscription.reset(); | 213 subscription.reset(); |
| 211 | 214 |
| 212 Geoposition second_position; | 215 Geoposition second_position; |
| 213 second_position.latitude = 13; | 216 second_position.latitude = 13; |
| 214 second_position.longitude = 34; | 217 second_position.longitude = 34; |
| 215 second_position.accuracy = 56; | 218 second_position.accuracy = 56; |
| 216 second_position.timestamp = base::Time::Now(); | 219 second_position.timestamp = base::Time::Now(); |
| 217 | 220 |
| 218 AsyncMockGeolocationObserver second_observer; | 221 AsyncMockGeolocationObserver second_observer; |
| 219 | 222 |
| 220 // After adding a second observer, check that no unexpected position update | 223 // After adding a second observer, check that no unexpected position update |
| 221 // is sent. | 224 // is sent. |
| 222 EXPECT_CALL(second_observer, OnLocationUpdate(testing::_)).Times(0); | 225 EXPECT_CALL(second_observer, OnLocationUpdate(testing::_)).Times(0); |
| 223 GeolocationProviderImpl::LocationUpdateCallback second_callback = base::Bind( | 226 GeolocationProviderImpl::LocationUpdateCallback second_callback = base::Bind( |
| 224 &MockGeolocationObserver::OnLocationUpdate, | 227 &MockGeolocationObserver::OnLocationUpdate, |
| 225 base::Unretained(&second_observer)); | 228 base::Unretained(&second_observer)); |
| 226 scoped_ptr<content::GeolocationProvider::Subscription> subscription2 = | 229 std::unique_ptr<content::GeolocationProvider::Subscription> subscription2 = |
| 227 provider()->AddLocationUpdateCallback(second_callback, false); | 230 provider()->AddLocationUpdateCallback(second_callback, false); |
| 228 base::MessageLoop::current()->RunUntilIdle(); | 231 base::MessageLoop::current()->RunUntilIdle(); |
| 229 | 232 |
| 230 // The second observer should receive the new position now. | 233 // The second observer should receive the new position now. |
| 231 EXPECT_CALL(second_observer, | 234 EXPECT_CALL(second_observer, |
| 232 OnLocationUpdate(GeopositionEq(second_position))); | 235 OnLocationUpdate(GeopositionEq(second_position))); |
| 233 SendMockLocation(second_position); | 236 SendMockLocation(second_position); |
| 234 base::MessageLoop::current()->Run(); | 237 base::MessageLoop::current()->Run(); |
| 235 | 238 |
| 236 subscription2.reset(); | 239 subscription2.reset(); |
| 237 EXPECT_FALSE(ProvidersStarted()); | 240 EXPECT_FALSE(ProvidersStarted()); |
| 238 } | 241 } |
| 239 | 242 |
| 240 TEST_F(GeolocationProviderTest, OverrideLocationForTesting) { | 243 TEST_F(GeolocationProviderTest, OverrideLocationForTesting) { |
| 241 Geoposition position; | 244 Geoposition position; |
| 242 position.error_code = Geoposition::ERROR_CODE_POSITION_UNAVAILABLE; | 245 position.error_code = Geoposition::ERROR_CODE_POSITION_UNAVAILABLE; |
| 243 provider()->OverrideLocationForTesting(position); | 246 provider()->OverrideLocationForTesting(position); |
| 244 // Adding an observer when the location is overridden should synchronously | 247 // Adding an observer when the location is overridden should synchronously |
| 245 // update the observer with our overridden position. | 248 // update the observer with our overridden position. |
| 246 MockGeolocationObserver mock_observer; | 249 MockGeolocationObserver mock_observer; |
| 247 EXPECT_CALL(mock_observer, OnLocationUpdate(GeopositionEq(position))); | 250 EXPECT_CALL(mock_observer, OnLocationUpdate(GeopositionEq(position))); |
| 248 GeolocationProviderImpl::LocationUpdateCallback callback = base::Bind( | 251 GeolocationProviderImpl::LocationUpdateCallback callback = base::Bind( |
| 249 &MockGeolocationObserver::OnLocationUpdate, | 252 &MockGeolocationObserver::OnLocationUpdate, |
| 250 base::Unretained(&mock_observer)); | 253 base::Unretained(&mock_observer)); |
| 251 scoped_ptr<content::GeolocationProvider::Subscription> subscription = | 254 std::unique_ptr<content::GeolocationProvider::Subscription> subscription = |
| 252 provider()->AddLocationUpdateCallback(callback, false); | 255 provider()->AddLocationUpdateCallback(callback, false); |
| 253 subscription.reset(); | 256 subscription.reset(); |
| 254 // Wait for the providers to be stopped now that all clients are gone. | 257 // Wait for the providers to be stopped now that all clients are gone. |
| 255 EXPECT_FALSE(ProvidersStarted()); | 258 EXPECT_FALSE(ProvidersStarted()); |
| 256 } | 259 } |
| 257 | 260 |
| 258 } // namespace content | 261 } // namespace content |
| OLD | NEW |