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