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.h" | 9 #include "base/message_loop.h" |
10 #include "base/string16.h" | 10 #include "base/string16.h" |
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
60 public: | 60 public: |
61 // GeolocationObserver | 61 // GeolocationObserver |
62 MOCK_METHOD1(OnLocationUpdate, void(const Geoposition& position)); | 62 MOCK_METHOD1(OnLocationUpdate, void(const Geoposition& position)); |
63 }; | 63 }; |
64 | 64 |
65 class AsyncMockGeolocationObserver : public MockGeolocationObserver { | 65 class AsyncMockGeolocationObserver : public MockGeolocationObserver { |
66 public: | 66 public: |
67 // GeolocationObserver | 67 // GeolocationObserver |
68 virtual void OnLocationUpdate(const Geoposition& position) OVERRIDE { | 68 virtual void OnLocationUpdate(const Geoposition& position) OVERRIDE { |
69 MockGeolocationObserver::OnLocationUpdate(position); | 69 MockGeolocationObserver::OnLocationUpdate(position); |
70 MessageLoop::current()->Quit(); | 70 base::MessageLoop::current()->Quit(); |
71 } | 71 } |
72 }; | 72 }; |
73 | 73 |
74 class MockGeolocationCallbackWrapper { | 74 class MockGeolocationCallbackWrapper { |
75 public: | 75 public: |
76 MOCK_METHOD1(Callback, void(const Geoposition& position)); | 76 MOCK_METHOD1(Callback, void(const Geoposition& position)); |
77 }; | 77 }; |
78 | 78 |
79 class GeopositionEqMatcher | 79 class GeopositionEqMatcher |
80 : public MatcherInterface<const Geoposition&> { | 80 : public MatcherInterface<const Geoposition&> { |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
127 LocationProviderForTestArbitrator* provider() { return provider_.get(); } | 127 LocationProviderForTestArbitrator* provider() { return provider_.get(); } |
128 | 128 |
129 // Called on test thread. | 129 // Called on test thread. |
130 bool ProvidersStarted(); | 130 bool ProvidersStarted(); |
131 void SendMockLocation(const Geoposition& position); | 131 void SendMockLocation(const Geoposition& position); |
132 | 132 |
133 private: | 133 private: |
134 // Called on provider thread. | 134 // Called on provider thread. |
135 void GetProvidersStarted(bool* started); | 135 void GetProvidersStarted(bool* started); |
136 | 136 |
137 MessageLoop message_loop_; | 137 base::MessageLoop message_loop_; |
138 TestBrowserThread io_thread_; | 138 TestBrowserThread io_thread_; |
139 scoped_ptr<LocationProviderForTestArbitrator> provider_; | 139 scoped_ptr<LocationProviderForTestArbitrator> provider_; |
140 }; | 140 }; |
141 | 141 |
142 | 142 |
143 bool GeolocationProviderTest::ProvidersStarted() { | 143 bool GeolocationProviderTest::ProvidersStarted() { |
144 DCHECK(provider_->IsRunning()); | 144 DCHECK(provider_->IsRunning()); |
145 DCHECK(MessageLoop::current() == &message_loop_); | 145 DCHECK(base::MessageLoop::current() == &message_loop_); |
146 bool started; | 146 bool started; |
147 provider_->message_loop_proxy()->PostTaskAndReply( | 147 provider_->message_loop_proxy()->PostTaskAndReply( |
148 FROM_HERE, | 148 FROM_HERE, |
149 base::Bind(&GeolocationProviderTest::GetProvidersStarted, | 149 base::Bind(&GeolocationProviderTest::GetProvidersStarted, |
150 base::Unretained(this), | 150 base::Unretained(this), |
151 &started), | 151 &started), |
152 MessageLoop::QuitClosure()); | 152 base::MessageLoop::QuitClosure()); |
153 message_loop_.Run(); | 153 message_loop_.Run(); |
154 return started; | 154 return started; |
155 } | 155 } |
156 | 156 |
157 void GeolocationProviderTest::GetProvidersStarted(bool* started) { | 157 void GeolocationProviderTest::GetProvidersStarted(bool* started) { |
158 DCHECK(MessageLoop::current() == provider_->message_loop()); | 158 DCHECK(base::MessageLoop::current() == provider_->message_loop()); |
159 *started = provider_->mock_arbitrator()->providers_started(); | 159 *started = provider_->mock_arbitrator()->providers_started(); |
160 } | 160 } |
161 | 161 |
162 void GeolocationProviderTest::SendMockLocation(const Geoposition& position) { | 162 void GeolocationProviderTest::SendMockLocation(const Geoposition& position) { |
163 DCHECK(provider_->IsRunning()); | 163 DCHECK(provider_->IsRunning()); |
164 DCHECK(MessageLoop::current() == &message_loop_); | 164 DCHECK(base::MessageLoop::current() == &message_loop_); |
165 provider_->message_loop()->PostTask( | 165 provider_->message_loop() |
166 FROM_HERE, | 166 ->PostTask(FROM_HERE, |
167 base::Bind(&GeolocationProvider::OnLocationUpdate, | 167 base::Bind(&GeolocationProvider::OnLocationUpdate, |
168 base::Unretained(provider_.get()), | 168 base::Unretained(provider_.get()), |
169 position)); | 169 position)); |
170 } | 170 } |
171 | 171 |
172 // Regression test for http://crbug.com/59377 | 172 // Regression test for http://crbug.com/59377 |
173 TEST_F(GeolocationProviderTest, OnPermissionGrantedWithoutObservers) { | 173 TEST_F(GeolocationProviderTest, OnPermissionGrantedWithoutObservers) { |
174 EXPECT_FALSE(provider()->HasPermissionBeenGranted()); | 174 EXPECT_FALSE(provider()->HasPermissionBeenGranted()); |
175 provider()->OnPermissionGranted(); | 175 provider()->OnPermissionGranted(); |
176 EXPECT_TRUE(provider()->HasPermissionBeenGranted()); | 176 EXPECT_TRUE(provider()->HasPermissionBeenGranted()); |
177 } | 177 } |
178 | 178 |
179 TEST_F(GeolocationProviderTest, StartStop) { | 179 TEST_F(GeolocationProviderTest, StartStop) { |
(...skipping 14 matching lines...) Expand all Loading... |
194 first_position.latitude = 12; | 194 first_position.latitude = 12; |
195 first_position.longitude = 34; | 195 first_position.longitude = 34; |
196 first_position.accuracy = 56; | 196 first_position.accuracy = 56; |
197 first_position.timestamp = base::Time::Now(); | 197 first_position.timestamp = base::Time::Now(); |
198 | 198 |
199 AsyncMockGeolocationObserver first_observer; | 199 AsyncMockGeolocationObserver first_observer; |
200 GeolocationObserverOptions options; | 200 GeolocationObserverOptions options; |
201 EXPECT_CALL(first_observer, OnLocationUpdate(GeopositionEq(first_position))); | 201 EXPECT_CALL(first_observer, OnLocationUpdate(GeopositionEq(first_position))); |
202 provider()->AddObserver(&first_observer, options); | 202 provider()->AddObserver(&first_observer, options); |
203 SendMockLocation(first_position); | 203 SendMockLocation(first_position); |
204 MessageLoop::current()->Run(); | 204 base::MessageLoop::current()->Run(); |
205 | 205 |
206 provider()->RemoveObserver(&first_observer); | 206 provider()->RemoveObserver(&first_observer); |
207 | 207 |
208 Geoposition second_position; | 208 Geoposition second_position; |
209 second_position.latitude = 13; | 209 second_position.latitude = 13; |
210 second_position.longitude = 34; | 210 second_position.longitude = 34; |
211 second_position.accuracy = 56; | 211 second_position.accuracy = 56; |
212 second_position.timestamp = base::Time::Now(); | 212 second_position.timestamp = base::Time::Now(); |
213 | 213 |
214 AsyncMockGeolocationObserver second_observer; | 214 AsyncMockGeolocationObserver second_observer; |
215 | 215 |
216 // After adding a second observer, check that no unexpected position update | 216 // After adding a second observer, check that no unexpected position update |
217 // is sent. | 217 // is sent. |
218 EXPECT_CALL(second_observer, OnLocationUpdate(testing::_)).Times(0); | 218 EXPECT_CALL(second_observer, OnLocationUpdate(testing::_)).Times(0); |
219 provider()->AddObserver(&second_observer, options); | 219 provider()->AddObserver(&second_observer, options); |
220 MessageLoop::current()->RunUntilIdle(); | 220 base::MessageLoop::current()->RunUntilIdle(); |
221 | 221 |
222 // The second observer should receive the new position now. | 222 // The second observer should receive the new position now. |
223 EXPECT_CALL(second_observer, | 223 EXPECT_CALL(second_observer, |
224 OnLocationUpdate(GeopositionEq(second_position))); | 224 OnLocationUpdate(GeopositionEq(second_position))); |
225 SendMockLocation(second_position); | 225 SendMockLocation(second_position); |
226 MessageLoop::current()->Run(); | 226 base::MessageLoop::current()->Run(); |
227 | 227 |
228 provider()->RemoveObserver(&second_observer); | 228 provider()->RemoveObserver(&second_observer); |
229 EXPECT_FALSE(ProvidersStarted()); | 229 EXPECT_FALSE(ProvidersStarted()); |
230 } | 230 } |
231 | 231 |
232 TEST_F(GeolocationProviderTest, OverrideLocationForTesting) { | 232 TEST_F(GeolocationProviderTest, OverrideLocationForTesting) { |
233 Geoposition position; | 233 Geoposition position; |
234 position.error_code = Geoposition::ERROR_CODE_POSITION_UNAVAILABLE; | 234 position.error_code = Geoposition::ERROR_CODE_POSITION_UNAVAILABLE; |
235 provider()->OverrideLocationForTesting(position); | 235 provider()->OverrideLocationForTesting(position); |
236 // Adding an observer when the location is overridden should synchronously | 236 // Adding an observer when the location is overridden should synchronously |
(...skipping 16 matching lines...) Expand all Loading... |
253 position.longitude = 34; | 253 position.longitude = 34; |
254 position.accuracy = 56; | 254 position.accuracy = 56; |
255 position.timestamp = base::Time::Now(); | 255 position.timestamp = base::Time::Now(); |
256 EXPECT_CALL(callback_wrapper, Callback(GeopositionEq(position))); | 256 EXPECT_CALL(callback_wrapper, Callback(GeopositionEq(position))); |
257 provider()->OverrideLocationForTesting(position); | 257 provider()->OverrideLocationForTesting(position); |
258 // 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. |
259 EXPECT_FALSE(ProvidersStarted()); | 259 EXPECT_FALSE(ProvidersStarted()); |
260 } | 260 } |
261 | 261 |
262 } // namespace content | 262 } // namespace content |
OLD | NEW |