| 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/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| (...skipping 16 matching lines...) Expand all Loading... |
| 27 using testing::MatchResultListener; | 27 using testing::MatchResultListener; |
| 28 | 28 |
| 29 namespace device { | 29 namespace device { |
| 30 | 30 |
| 31 class LocationProviderForTestArbitrator : public GeolocationProviderImpl { | 31 class LocationProviderForTestArbitrator : public GeolocationProviderImpl { |
| 32 public: | 32 public: |
| 33 LocationProviderForTestArbitrator() : mock_arbitrator_(NULL) {} | 33 LocationProviderForTestArbitrator() : mock_arbitrator_(NULL) {} |
| 34 ~LocationProviderForTestArbitrator() override {} | 34 ~LocationProviderForTestArbitrator() override {} |
| 35 | 35 |
| 36 // Only valid for use on the geolocation thread. | 36 // Only valid for use on the geolocation thread. |
| 37 MockLocationArbitrator* mock_arbitrator() const { | 37 MockLocationArbitrator* mock_arbitrator() const { return mock_arbitrator_; } |
| 38 return mock_arbitrator_; | |
| 39 } | |
| 40 | 38 |
| 41 protected: | 39 protected: |
| 42 // GeolocationProviderImpl implementation: | 40 // GeolocationProviderImpl implementation: |
| 43 std::unique_ptr<LocationArbitrator> CreateArbitrator() override; | 41 std::unique_ptr<LocationArbitrator> CreateArbitrator() override; |
| 44 | 42 |
| 45 private: | 43 private: |
| 46 // An alias to the arbitrator stored in the super class, where it is owned. | 44 // An alias to the arbitrator stored in the super class, where it is owned. |
| 47 MockLocationArbitrator* mock_arbitrator_; | 45 MockLocationArbitrator* mock_arbitrator_; |
| 48 }; | 46 }; |
| 49 | 47 |
| (...skipping 21 matching lines...) Expand all Loading... |
| 71 MockGeolocationObserver::OnLocationUpdate(position); | 69 MockGeolocationObserver::OnLocationUpdate(position); |
| 72 base::MessageLoop::current()->QuitWhenIdle(); | 70 base::MessageLoop::current()->QuitWhenIdle(); |
| 73 } | 71 } |
| 74 }; | 72 }; |
| 75 | 73 |
| 76 class MockGeolocationCallbackWrapper { | 74 class MockGeolocationCallbackWrapper { |
| 77 public: | 75 public: |
| 78 MOCK_METHOD1(Callback, void(const Geoposition& position)); | 76 MOCK_METHOD1(Callback, void(const Geoposition& position)); |
| 79 }; | 77 }; |
| 80 | 78 |
| 81 class GeopositionEqMatcher | 79 class GeopositionEqMatcher : public MatcherInterface<const Geoposition&> { |
| 82 : public MatcherInterface<const Geoposition&> { | |
| 83 public: | 80 public: |
| 84 explicit GeopositionEqMatcher(const Geoposition& expected) | 81 explicit GeopositionEqMatcher(const Geoposition& expected) |
| 85 : expected_(expected) {} | 82 : expected_(expected) {} |
| 86 | 83 |
| 87 bool MatchAndExplain(const Geoposition& actual, | 84 bool MatchAndExplain(const Geoposition& actual, |
| 88 MatchResultListener* listener) const override { | 85 MatchResultListener* listener) const override { |
| 89 return actual.latitude == expected_.latitude && | 86 return actual.latitude == expected_.latitude && |
| 90 actual.longitude == expected_.longitude && | 87 actual.longitude == expected_.longitude && |
| 91 actual.altitude == expected_.altitude && | 88 actual.altitude == expected_.altitude && |
| 92 actual.accuracy == expected_.accuracy && | 89 actual.accuracy == expected_.accuracy && |
| (...skipping 19 matching lines...) Expand all Loading... |
| 112 DISALLOW_COPY_AND_ASSIGN(GeopositionEqMatcher); | 109 DISALLOW_COPY_AND_ASSIGN(GeopositionEqMatcher); |
| 113 }; | 110 }; |
| 114 | 111 |
| 115 Matcher<const Geoposition&> GeopositionEq(const Geoposition& expected) { | 112 Matcher<const Geoposition&> GeopositionEq(const Geoposition& expected) { |
| 116 return MakeMatcher(new GeopositionEqMatcher(expected)); | 113 return MakeMatcher(new GeopositionEqMatcher(expected)); |
| 117 } | 114 } |
| 118 | 115 |
| 119 class GeolocationProviderTest : public testing::Test { | 116 class GeolocationProviderTest : public testing::Test { |
| 120 protected: | 117 protected: |
| 121 GeolocationProviderTest() | 118 GeolocationProviderTest() |
| 122 : message_loop_(), | 119 : message_loop_(), provider_(new LocationProviderForTestArbitrator) {} |
| 123 provider_(new LocationProviderForTestArbitrator) { | |
| 124 } | |
| 125 | 120 |
| 126 ~GeolocationProviderTest() override {} | 121 ~GeolocationProviderTest() override {} |
| 127 | 122 |
| 128 LocationProviderForTestArbitrator* provider() { return provider_.get(); } | 123 LocationProviderForTestArbitrator* provider() { return provider_.get(); } |
| 129 | 124 |
| 130 // Called on test thread. | 125 // Called on test thread. |
| 131 bool ProvidersStarted(); | 126 bool ProvidersStarted(); |
| 132 void SendMockLocation(const Geoposition& position); | 127 void SendMockLocation(const Geoposition& position); |
| 133 | 128 |
| 134 private: | 129 private: |
| 135 // Called on provider thread. | 130 // Called on provider thread. |
| 136 void GetProvidersStarted(bool* started); | 131 void GetProvidersStarted(bool* started); |
| 137 | 132 |
| 138 base::MessageLoopForUI message_loop_; | 133 base::MessageLoopForUI message_loop_; |
| 139 std::unique_ptr<LocationProviderForTestArbitrator> provider_; | 134 std::unique_ptr<LocationProviderForTestArbitrator> provider_; |
| 140 }; | 135 }; |
| 141 | 136 |
| 142 | |
| 143 bool GeolocationProviderTest::ProvidersStarted() { | 137 bool GeolocationProviderTest::ProvidersStarted() { |
| 144 DCHECK(provider_->IsRunning()); | 138 DCHECK(provider_->IsRunning()); |
| 145 DCHECK(base::MessageLoop::current() == &message_loop_); | 139 DCHECK(base::MessageLoop::current() == &message_loop_); |
| 146 bool started; | 140 bool started; |
| 147 provider_->task_runner()->PostTaskAndReply( | 141 provider_->task_runner()->PostTaskAndReply( |
| 148 FROM_HERE, base::Bind(&GeolocationProviderTest::GetProvidersStarted, | 142 FROM_HERE, base::Bind(&GeolocationProviderTest::GetProvidersStarted, |
| 149 base::Unretained(this), &started), | 143 base::Unretained(this), &started), |
| 150 base::MessageLoop::QuitWhenIdleClosure()); | 144 base::MessageLoop::QuitWhenIdleClosure()); |
| 151 base::RunLoop().Run(); | 145 base::RunLoop().Run(); |
| 152 return started; | 146 return started; |
| (...skipping 12 matching lines...) Expand all Loading... |
| 165 base::Unretained(provider_.get()), position)); | 159 base::Unretained(provider_.get()), position)); |
| 166 } | 160 } |
| 167 | 161 |
| 168 // Regression test for http://crbug.com/59377 | 162 // Regression test for http://crbug.com/59377 |
| 169 TEST_F(GeolocationProviderTest, OnPermissionGrantedWithoutObservers) { | 163 TEST_F(GeolocationProviderTest, OnPermissionGrantedWithoutObservers) { |
| 170 EXPECT_FALSE(provider()->user_did_opt_into_location_services_for_testing()); | 164 EXPECT_FALSE(provider()->user_did_opt_into_location_services_for_testing()); |
| 171 provider()->UserDidOptIntoLocationServices(); | 165 provider()->UserDidOptIntoLocationServices(); |
| 172 EXPECT_TRUE(provider()->user_did_opt_into_location_services_for_testing()); | 166 EXPECT_TRUE(provider()->user_did_opt_into_location_services_for_testing()); |
| 173 } | 167 } |
| 174 | 168 |
| 175 void DummyFunction(const Geoposition& position) { | 169 void DummyFunction(const Geoposition& position) {} |
| 176 } | |
| 177 | 170 |
| 178 TEST_F(GeolocationProviderTest, StartStop) { | 171 TEST_F(GeolocationProviderTest, StartStop) { |
| 179 EXPECT_FALSE(provider()->IsRunning()); | 172 EXPECT_FALSE(provider()->IsRunning()); |
| 180 GeolocationProviderImpl::LocationUpdateCallback callback = | 173 GeolocationProviderImpl::LocationUpdateCallback callback = |
| 181 base::Bind(&DummyFunction); | 174 base::Bind(&DummyFunction); |
| 182 std::unique_ptr<GeolocationProvider::Subscription> subscription = | 175 std::unique_ptr<GeolocationProvider::Subscription> subscription = |
| 183 provider()->AddLocationUpdateCallback(callback, false); | 176 provider()->AddLocationUpdateCallback(callback, false); |
| 184 EXPECT_TRUE(provider()->IsRunning()); | 177 EXPECT_TRUE(provider()->IsRunning()); |
| 185 EXPECT_TRUE(ProvidersStarted()); | 178 EXPECT_TRUE(ProvidersStarted()); |
| 186 | 179 |
| 187 subscription.reset(); | 180 subscription.reset(); |
| 188 | 181 |
| 189 EXPECT_FALSE(ProvidersStarted()); | 182 EXPECT_FALSE(ProvidersStarted()); |
| 190 EXPECT_TRUE(provider()->IsRunning()); | 183 EXPECT_TRUE(provider()->IsRunning()); |
| 191 } | 184 } |
| 192 | 185 |
| 193 TEST_F(GeolocationProviderTest, StalePositionNotSent) { | 186 TEST_F(GeolocationProviderTest, StalePositionNotSent) { |
| 194 Geoposition first_position; | 187 Geoposition first_position; |
| 195 first_position.latitude = 12; | 188 first_position.latitude = 12; |
| 196 first_position.longitude = 34; | 189 first_position.longitude = 34; |
| 197 first_position.accuracy = 56; | 190 first_position.accuracy = 56; |
| 198 first_position.timestamp = base::Time::Now(); | 191 first_position.timestamp = base::Time::Now(); |
| 199 | 192 |
| 200 AsyncMockGeolocationObserver first_observer; | 193 AsyncMockGeolocationObserver first_observer; |
| 201 GeolocationProviderImpl::LocationUpdateCallback first_callback = base::Bind( | 194 GeolocationProviderImpl::LocationUpdateCallback first_callback = |
| 202 &MockGeolocationObserver::OnLocationUpdate, | 195 base::Bind(&MockGeolocationObserver::OnLocationUpdate, |
| 203 base::Unretained(&first_observer)); | 196 base::Unretained(&first_observer)); |
| 204 EXPECT_CALL(first_observer, OnLocationUpdate(GeopositionEq(first_position))); | 197 EXPECT_CALL(first_observer, OnLocationUpdate(GeopositionEq(first_position))); |
| 205 std::unique_ptr<GeolocationProvider::Subscription> subscription = | 198 std::unique_ptr<GeolocationProvider::Subscription> subscription = |
| 206 provider()->AddLocationUpdateCallback(first_callback, false); | 199 provider()->AddLocationUpdateCallback(first_callback, false); |
| 207 SendMockLocation(first_position); | 200 SendMockLocation(first_position); |
| 208 base::RunLoop().Run(); | 201 base::RunLoop().Run(); |
| 209 | 202 |
| 210 subscription.reset(); | 203 subscription.reset(); |
| 211 | 204 |
| 212 Geoposition second_position; | 205 Geoposition second_position; |
| 213 second_position.latitude = 13; | 206 second_position.latitude = 13; |
| 214 second_position.longitude = 34; | 207 second_position.longitude = 34; |
| 215 second_position.accuracy = 56; | 208 second_position.accuracy = 56; |
| 216 second_position.timestamp = base::Time::Now(); | 209 second_position.timestamp = base::Time::Now(); |
| 217 | 210 |
| 218 AsyncMockGeolocationObserver second_observer; | 211 AsyncMockGeolocationObserver second_observer; |
| 219 | 212 |
| 220 // After adding a second observer, check that no unexpected position update | 213 // After adding a second observer, check that no unexpected position update |
| 221 // is sent. | 214 // is sent. |
| 222 EXPECT_CALL(second_observer, OnLocationUpdate(testing::_)).Times(0); | 215 EXPECT_CALL(second_observer, OnLocationUpdate(testing::_)).Times(0); |
| 223 GeolocationProviderImpl::LocationUpdateCallback second_callback = base::Bind( | 216 GeolocationProviderImpl::LocationUpdateCallback second_callback = |
| 224 &MockGeolocationObserver::OnLocationUpdate, | 217 base::Bind(&MockGeolocationObserver::OnLocationUpdate, |
| 225 base::Unretained(&second_observer)); | 218 base::Unretained(&second_observer)); |
| 226 std::unique_ptr<GeolocationProvider::Subscription> subscription2 = | 219 std::unique_ptr<GeolocationProvider::Subscription> subscription2 = |
| 227 provider()->AddLocationUpdateCallback(second_callback, false); | 220 provider()->AddLocationUpdateCallback(second_callback, false); |
| 228 base::RunLoop().RunUntilIdle(); | 221 base::RunLoop().RunUntilIdle(); |
| 229 | 222 |
| 230 // The second observer should receive the new position now. | 223 // The second observer should receive the new position now. |
| 231 EXPECT_CALL(second_observer, | 224 EXPECT_CALL(second_observer, |
| 232 OnLocationUpdate(GeopositionEq(second_position))); | 225 OnLocationUpdate(GeopositionEq(second_position))); |
| 233 SendMockLocation(second_position); | 226 SendMockLocation(second_position); |
| 234 base::RunLoop().Run(); | 227 base::RunLoop().Run(); |
| 235 | 228 |
| 236 subscription2.reset(); | 229 subscription2.reset(); |
| 237 EXPECT_FALSE(ProvidersStarted()); | 230 EXPECT_FALSE(ProvidersStarted()); |
| 238 } | 231 } |
| 239 | 232 |
| 240 TEST_F(GeolocationProviderTest, OverrideLocationForTesting) { | 233 TEST_F(GeolocationProviderTest, OverrideLocationForTesting) { |
| 241 Geoposition position; | 234 Geoposition position; |
| 242 position.error_code = Geoposition::ERROR_CODE_POSITION_UNAVAILABLE; | 235 position.error_code = Geoposition::ERROR_CODE_POSITION_UNAVAILABLE; |
| 243 provider()->OverrideLocationForTesting(position); | 236 provider()->OverrideLocationForTesting(position); |
| 244 // Adding an observer when the location is overridden should synchronously | 237 // Adding an observer when the location is overridden should synchronously |
| 245 // update the observer with our overridden position. | 238 // update the observer with our overridden position. |
| 246 MockGeolocationObserver mock_observer; | 239 MockGeolocationObserver mock_observer; |
| 247 EXPECT_CALL(mock_observer, OnLocationUpdate(GeopositionEq(position))); | 240 EXPECT_CALL(mock_observer, OnLocationUpdate(GeopositionEq(position))); |
| 248 GeolocationProviderImpl::LocationUpdateCallback callback = base::Bind( | 241 GeolocationProviderImpl::LocationUpdateCallback callback = |
| 249 &MockGeolocationObserver::OnLocationUpdate, | 242 base::Bind(&MockGeolocationObserver::OnLocationUpdate, |
| 250 base::Unretained(&mock_observer)); | 243 base::Unretained(&mock_observer)); |
| 251 std::unique_ptr<GeolocationProvider::Subscription> subscription = | 244 std::unique_ptr<GeolocationProvider::Subscription> subscription = |
| 252 provider()->AddLocationUpdateCallback(callback, false); | 245 provider()->AddLocationUpdateCallback(callback, false); |
| 253 subscription.reset(); | 246 subscription.reset(); |
| 254 // Wait for the providers to be stopped now that all clients are gone. | 247 // Wait for the providers to be stopped now that all clients are gone. |
| 255 EXPECT_FALSE(ProvidersStarted()); | 248 EXPECT_FALSE(ProvidersStarted()); |
| 256 } | 249 } |
| 257 | 250 |
| 258 } // namespace device | 251 } // namespace device |
| OLD | NEW |