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