OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/bind_helpers.h" | 6 #include "base/bind_helpers.h" |
7 #include "base/memory/ref_counted.h" | 7 #include "base/memory/ref_counted.h" |
8 #include "base/memory/scoped_ptr.h" | 8 #include "base/memory/scoped_ptr.h" |
9 #include "base/message_loop/message_loop.h" | 9 #include "base/message_loop/message_loop.h" |
10 #include "base/strings/string16.h" | 10 #include "base/strings/string16.h" |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
107 }; | 107 }; |
108 | 108 |
109 Matcher<const Geoposition&> GeopositionEq(const Geoposition& expected) { | 109 Matcher<const Geoposition&> GeopositionEq(const Geoposition& expected) { |
110 return MakeMatcher(new GeopositionEqMatcher(expected)); | 110 return MakeMatcher(new GeopositionEqMatcher(expected)); |
111 } | 111 } |
112 | 112 |
113 class GeolocationProviderTest : public testing::Test { | 113 class GeolocationProviderTest : public testing::Test { |
114 protected: | 114 protected: |
115 GeolocationProviderTest() | 115 GeolocationProviderTest() |
116 : message_loop_(), | 116 : message_loop_(), |
117 io_thread_(BrowserThread::IO, &message_loop_), | 117 ui_thread_(BrowserThread::UI, &message_loop_), |
118 provider_(new LocationProviderForTestArbitrator) { | 118 provider_(new LocationProviderForTestArbitrator) { |
119 } | 119 } |
120 | 120 |
121 virtual ~GeolocationProviderTest() {} | 121 virtual ~GeolocationProviderTest() {} |
122 | 122 |
123 LocationProviderForTestArbitrator* provider() { return provider_.get(); } | 123 LocationProviderForTestArbitrator* provider() { return provider_.get(); } |
124 | 124 |
125 // Called on test thread. | 125 // Called on test thread. |
126 bool ProvidersStarted(); | 126 bool ProvidersStarted(); |
127 void SendMockLocation(const Geoposition& position); | 127 void SendMockLocation(const Geoposition& position); |
128 | 128 |
129 private: | 129 private: |
130 // Called on provider thread. | 130 // Called on provider thread. |
131 void GetProvidersStarted(bool* started); | 131 void GetProvidersStarted(bool* started); |
132 | 132 |
133 base::MessageLoop message_loop_; | 133 base::MessageLoop message_loop_; |
134 TestBrowserThread io_thread_; | 134 TestBrowserThread ui_thread_; |
135 scoped_ptr<LocationProviderForTestArbitrator> provider_; | 135 scoped_ptr<LocationProviderForTestArbitrator> provider_; |
136 }; | 136 }; |
137 | 137 |
138 | 138 |
139 bool GeolocationProviderTest::ProvidersStarted() { | 139 bool GeolocationProviderTest::ProvidersStarted() { |
140 DCHECK(provider_->IsRunning()); | 140 DCHECK(provider_->IsRunning()); |
141 DCHECK(base::MessageLoop::current() == &message_loop_); | 141 DCHECK(base::MessageLoop::current() == &message_loop_); |
142 bool started; | 142 bool started; |
143 provider_->message_loop_proxy()->PostTaskAndReply( | 143 provider_->message_loop_proxy()->PostTaskAndReply( |
144 FROM_HERE, | 144 FROM_HERE, |
(...skipping 15 matching lines...) Expand all Loading... |
160 DCHECK(base::MessageLoop::current() == &message_loop_); | 160 DCHECK(base::MessageLoop::current() == &message_loop_); |
161 provider_->message_loop() | 161 provider_->message_loop() |
162 ->PostTask(FROM_HERE, | 162 ->PostTask(FROM_HERE, |
163 base::Bind(&GeolocationProviderImpl::OnLocationUpdate, | 163 base::Bind(&GeolocationProviderImpl::OnLocationUpdate, |
164 base::Unretained(provider_.get()), | 164 base::Unretained(provider_.get()), |
165 position)); | 165 position)); |
166 } | 166 } |
167 | 167 |
168 // Regression test for http://crbug.com/59377 | 168 // Regression test for http://crbug.com/59377 |
169 TEST_F(GeolocationProviderTest, OnPermissionGrantedWithoutObservers) { | 169 TEST_F(GeolocationProviderTest, OnPermissionGrantedWithoutObservers) { |
170 EXPECT_FALSE(provider()->LocationServicesOptedIn()); | 170 EXPECT_FALSE(provider()->user_did_opt_into_location_services_for_testing()); |
171 provider()->UserDidOptIntoLocationServices(); | 171 provider()->UserDidOptIntoLocationServices(); |
172 EXPECT_TRUE(provider()->LocationServicesOptedIn()); | 172 EXPECT_TRUE(provider()->user_did_opt_into_location_services_for_testing()); |
| 173 } |
| 174 |
| 175 void DummyFunction(const Geoposition& position) { |
173 } | 176 } |
174 | 177 |
175 TEST_F(GeolocationProviderTest, StartStop) { | 178 TEST_F(GeolocationProviderTest, StartStop) { |
176 EXPECT_FALSE(provider()->IsRunning()); | 179 EXPECT_FALSE(provider()->IsRunning()); |
177 GeolocationProviderImpl::LocationUpdateCallback null_callback; | 180 GeolocationProviderImpl::LocationUpdateCallback callback = |
178 provider()->AddLocationUpdateCallback(null_callback, false); | 181 base::Bind(&DummyFunction); |
| 182 scoped_ptr<content::GeolocationProvider::Subscription> subscription = |
| 183 provider()->AddLocationUpdateCallback(callback, false); |
179 EXPECT_TRUE(provider()->IsRunning()); | 184 EXPECT_TRUE(provider()->IsRunning()); |
180 EXPECT_TRUE(ProvidersStarted()); | 185 EXPECT_TRUE(ProvidersStarted()); |
181 | 186 |
182 provider()->RemoveLocationUpdateCallback(null_callback); | 187 subscription.reset(); |
| 188 |
183 EXPECT_FALSE(ProvidersStarted()); | 189 EXPECT_FALSE(ProvidersStarted()); |
184 EXPECT_TRUE(provider()->IsRunning()); | 190 EXPECT_TRUE(provider()->IsRunning()); |
185 } | 191 } |
186 | 192 |
187 TEST_F(GeolocationProviderTest, StalePositionNotSent) { | 193 TEST_F(GeolocationProviderTest, StalePositionNotSent) { |
188 Geoposition first_position; | 194 Geoposition first_position; |
189 first_position.latitude = 12; | 195 first_position.latitude = 12; |
190 first_position.longitude = 34; | 196 first_position.longitude = 34; |
191 first_position.accuracy = 56; | 197 first_position.accuracy = 56; |
192 first_position.timestamp = base::Time::Now(); | 198 first_position.timestamp = base::Time::Now(); |
193 | 199 |
194 AsyncMockGeolocationObserver first_observer; | 200 AsyncMockGeolocationObserver first_observer; |
195 GeolocationProviderImpl::LocationUpdateCallback first_callback = base::Bind( | 201 GeolocationProviderImpl::LocationUpdateCallback first_callback = base::Bind( |
196 &MockGeolocationObserver::OnLocationUpdate, | 202 &MockGeolocationObserver::OnLocationUpdate, |
197 base::Unretained(&first_observer)); | 203 base::Unretained(&first_observer)); |
198 EXPECT_CALL(first_observer, OnLocationUpdate(GeopositionEq(first_position))); | 204 EXPECT_CALL(first_observer, OnLocationUpdate(GeopositionEq(first_position))); |
199 provider()->AddLocationUpdateCallback(first_callback, false); | 205 scoped_ptr<content::GeolocationProvider::Subscription> subscription = |
| 206 provider()->AddLocationUpdateCallback(first_callback, false); |
200 SendMockLocation(first_position); | 207 SendMockLocation(first_position); |
201 base::MessageLoop::current()->Run(); | 208 base::MessageLoop::current()->Run(); |
202 | 209 |
203 provider()->RemoveLocationUpdateCallback(first_callback); | 210 subscription.reset(); |
204 | 211 |
205 Geoposition second_position; | 212 Geoposition second_position; |
206 second_position.latitude = 13; | 213 second_position.latitude = 13; |
207 second_position.longitude = 34; | 214 second_position.longitude = 34; |
208 second_position.accuracy = 56; | 215 second_position.accuracy = 56; |
209 second_position.timestamp = base::Time::Now(); | 216 second_position.timestamp = base::Time::Now(); |
210 | 217 |
211 AsyncMockGeolocationObserver second_observer; | 218 AsyncMockGeolocationObserver second_observer; |
212 | 219 |
213 // After adding a second observer, check that no unexpected position update | 220 // After adding a second observer, check that no unexpected position update |
214 // is sent. | 221 // is sent. |
215 EXPECT_CALL(second_observer, OnLocationUpdate(testing::_)).Times(0); | 222 EXPECT_CALL(second_observer, OnLocationUpdate(testing::_)).Times(0); |
216 GeolocationProviderImpl::LocationUpdateCallback second_callback = base::Bind( | 223 GeolocationProviderImpl::LocationUpdateCallback second_callback = base::Bind( |
217 &MockGeolocationObserver::OnLocationUpdate, | 224 &MockGeolocationObserver::OnLocationUpdate, |
218 base::Unretained(&second_observer)); | 225 base::Unretained(&second_observer)); |
219 provider()->AddLocationUpdateCallback(second_callback, false); | 226 scoped_ptr<content::GeolocationProvider::Subscription> subscription2 = |
| 227 provider()->AddLocationUpdateCallback(second_callback, false); |
220 base::MessageLoop::current()->RunUntilIdle(); | 228 base::MessageLoop::current()->RunUntilIdle(); |
221 | 229 |
222 // The second observer should receive the new position now. | 230 // The second observer should receive the new position now. |
223 EXPECT_CALL(second_observer, | 231 EXPECT_CALL(second_observer, |
224 OnLocationUpdate(GeopositionEq(second_position))); | 232 OnLocationUpdate(GeopositionEq(second_position))); |
225 SendMockLocation(second_position); | 233 SendMockLocation(second_position); |
226 base::MessageLoop::current()->Run(); | 234 base::MessageLoop::current()->Run(); |
227 | 235 |
228 provider()->RemoveLocationUpdateCallback(second_callback); | 236 subscription2.reset(); |
229 EXPECT_FALSE(ProvidersStarted()); | 237 EXPECT_FALSE(ProvidersStarted()); |
230 } | 238 } |
231 | 239 |
232 TEST_F(GeolocationProviderTest, OverrideLocationForTesting) { | 240 TEST_F(GeolocationProviderTest, OverrideLocationForTesting) { |
233 Geoposition position; | 241 Geoposition position; |
234 position.error_code = Geoposition::ERROR_CODE_POSITION_UNAVAILABLE; | 242 position.error_code = Geoposition::ERROR_CODE_POSITION_UNAVAILABLE; |
235 provider()->OverrideLocationForTesting(position); | 243 provider()->OverrideLocationForTesting(position); |
236 // Adding an observer when the location is overridden should synchronously | 244 // Adding an observer when the location is overridden should synchronously |
237 // update the observer with our overridden position. | 245 // update the observer with our overridden position. |
238 MockGeolocationObserver mock_observer; | 246 MockGeolocationObserver mock_observer; |
239 EXPECT_CALL(mock_observer, OnLocationUpdate(GeopositionEq(position))); | 247 EXPECT_CALL(mock_observer, OnLocationUpdate(GeopositionEq(position))); |
240 GeolocationProviderImpl::LocationUpdateCallback callback = base::Bind( | 248 GeolocationProviderImpl::LocationUpdateCallback callback = base::Bind( |
241 &MockGeolocationObserver::OnLocationUpdate, | 249 &MockGeolocationObserver::OnLocationUpdate, |
242 base::Unretained(&mock_observer)); | 250 base::Unretained(&mock_observer)); |
243 provider()->AddLocationUpdateCallback(callback, false); | 251 scoped_ptr<content::GeolocationProvider::Subscription> subscription = |
244 provider()->RemoveLocationUpdateCallback(callback); | 252 provider()->AddLocationUpdateCallback(callback, false); |
| 253 subscription.reset(); |
245 // 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. |
246 EXPECT_FALSE(ProvidersStarted()); | 255 EXPECT_FALSE(ProvidersStarted()); |
247 } | 256 } |
248 | 257 |
249 } // namespace content | 258 } // namespace content |
OLD | NEW |