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" |
11 #include "base/synchronization/waitable_event.h" | |
12 #include "base/time.h" | 11 #include "base/time.h" |
13 #include "content/browser/geolocation/geolocation_provider.h" | 12 #include "content/browser/geolocation/geolocation_provider.h" |
14 #include "content/browser/geolocation/location_arbitrator.h" | |
15 #include "content/browser/geolocation/location_provider.h" | 13 #include "content/browser/geolocation/location_provider.h" |
16 #include "content/browser/geolocation/mock_location_provider.h" | 14 #include "content/browser/geolocation/mock_location_arbitrator.h" |
17 #include "content/public/browser/access_token_store.h" | 15 #include "content/public/browser/access_token_store.h" |
18 #include "content/public/browser/browser_thread.h" | 16 #include "content/public/browser/browser_thread.h" |
19 #include "content/public/test/test_browser_thread.h" | 17 #include "content/public/test/test_browser_thread.h" |
20 #include "googleurl/src/gurl.h" | 18 #include "googleurl/src/gurl.h" |
21 #include "testing/gmock/include/gmock/gmock.h" | 19 #include "testing/gmock/include/gmock/gmock.h" |
22 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
23 | 21 |
24 using testing::MakeMatcher; | 22 using testing::MakeMatcher; |
25 using testing::Matcher; | 23 using testing::Matcher; |
26 using testing::MatcherInterface; | 24 using testing::MatcherInterface; |
27 using testing::MatchResultListener; | 25 using testing::MatchResultListener; |
28 | 26 |
29 namespace content { | 27 namespace content { |
30 | 28 |
31 class LocationProviderForTestArbitrator : public GeolocationProvider { | 29 class LocationProviderForTestArbitrator : public GeolocationProvider { |
32 public: | 30 public: |
33 explicit LocationProviderForTestArbitrator(base::WaitableEvent* event) | 31 LocationProviderForTestArbitrator() : mock_arbitrator_(NULL) {} |
34 : event_(event) { | 32 virtual ~LocationProviderForTestArbitrator() {} |
| 33 |
| 34 // Only valid for use on the geolocation thread. |
| 35 MockGeolocationArbitrator* mock_arbitrator() const { |
| 36 return mock_arbitrator_; |
35 } | 37 } |
36 | 38 |
37 virtual ~LocationProviderForTestArbitrator() {} | |
38 | |
39 protected: | 39 protected: |
40 // GeolocationProvider implementation: | 40 // GeolocationProvider implementation: |
41 virtual GeolocationArbitrator* CreateArbitrator() OVERRIDE; | 41 virtual GeolocationArbitrator* CreateArbitrator() OVERRIDE; |
42 | 42 |
43 private: | 43 private: |
44 base::WaitableEvent* event_; | 44 MockGeolocationArbitrator* mock_arbitrator_; |
45 }; | |
46 | |
47 class StartStopMockLocationProvider : public MockLocationProvider { | |
48 public: | |
49 StartStopMockLocationProvider(base::WaitableEvent* event) : | |
50 MockLocationProvider(&instance_), | |
51 event_(event) { | |
52 } | |
53 | |
54 virtual ~StartStopMockLocationProvider() { | |
55 event_->Signal(); | |
56 } | |
57 | |
58 private: | |
59 base::WaitableEvent* event_; | |
60 }; | |
61 | |
62 // The AccessTokenStore will be accessed from the geolocation helper thread. The | |
63 // existing FakeAccessTokenStore class cannot be used here because it is based | |
64 // on gmock and gmock is not thread-safe on Windows. | |
65 // See: http://code.google.com/p/googlemock/issues/detail?id=156 | |
66 class TestingAccessTokenStore : public AccessTokenStore { | |
67 public: | |
68 TestingAccessTokenStore(base::WaitableEvent* event) : event_(event) {} | |
69 | |
70 virtual void LoadAccessTokens(const LoadAccessTokensCallbackType& callback) | |
71 OVERRIDE { | |
72 callback.Run(AccessTokenSet(), NULL); | |
73 event_->Signal(); | |
74 } | |
75 | |
76 virtual void SaveAccessToken(const GURL& server_url, | |
77 const string16& access_token) OVERRIDE {} | |
78 | |
79 protected: | |
80 virtual ~TestingAccessTokenStore() {} | |
81 | |
82 private: | |
83 base::WaitableEvent* event_; | |
84 }; | |
85 | |
86 class TestGeolocationArbitrator : public GeolocationArbitrator { | |
87 public: | |
88 TestGeolocationArbitrator(GeolocationObserver* observer, | |
89 base::WaitableEvent* event) | |
90 : GeolocationArbitrator(observer), | |
91 event_(event) { | |
92 } | |
93 | |
94 virtual AccessTokenStore* NewAccessTokenStore() OVERRIDE { | |
95 return new TestingAccessTokenStore(event_); | |
96 } | |
97 | |
98 virtual LocationProviderBase* NewNetworkLocationProvider( | |
99 AccessTokenStore* access_token_store, | |
100 net::URLRequestContextGetter* context, | |
101 const GURL& url, | |
102 const string16& access_token) OVERRIDE { | |
103 return new StartStopMockLocationProvider(event_); | |
104 } | |
105 | |
106 virtual LocationProviderBase* NewSystemLocationProvider() OVERRIDE { | |
107 return NULL; | |
108 } | |
109 | |
110 private: | |
111 base::WaitableEvent* event_; | |
112 }; | 45 }; |
113 | 46 |
114 GeolocationArbitrator* LocationProviderForTestArbitrator::CreateArbitrator() { | 47 GeolocationArbitrator* LocationProviderForTestArbitrator::CreateArbitrator() { |
115 return new TestGeolocationArbitrator(this, event_); | 48 DCHECK(mock_arbitrator_ == NULL); |
| 49 mock_arbitrator_ = new MockGeolocationArbitrator; |
| 50 return mock_arbitrator_; |
116 } | 51 } |
117 | 52 |
118 class NullGeolocationObserver : public GeolocationObserver { | 53 class NullGeolocationObserver : public GeolocationObserver { |
119 public: | 54 public: |
120 // GeolocationObserver | 55 // GeolocationObserver |
121 virtual void OnLocationUpdate(const Geoposition& position) {} | 56 virtual void OnLocationUpdate(const Geoposition& position) {} |
122 }; | 57 }; |
123 | 58 |
124 class MockGeolocationObserver : public GeolocationObserver { | 59 class MockGeolocationObserver : public GeolocationObserver { |
125 public: | 60 public: |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
168 | 103 |
169 Matcher<const Geoposition&> GeopositionEq(const Geoposition& expected) { | 104 Matcher<const Geoposition&> GeopositionEq(const Geoposition& expected) { |
170 return MakeMatcher(new GeopositionEqMatcher(expected)); | 105 return MakeMatcher(new GeopositionEqMatcher(expected)); |
171 } | 106 } |
172 | 107 |
173 class GeolocationProviderTest : public testing::Test { | 108 class GeolocationProviderTest : public testing::Test { |
174 protected: | 109 protected: |
175 GeolocationProviderTest() | 110 GeolocationProviderTest() |
176 : message_loop_(), | 111 : message_loop_(), |
177 io_thread_(BrowserThread::IO, &message_loop_), | 112 io_thread_(BrowserThread::IO, &message_loop_), |
178 event_(false, false), | 113 provider_(new LocationProviderForTestArbitrator) { |
179 provider_(new LocationProviderForTestArbitrator(&event_)) { | |
180 } | 114 } |
181 | 115 |
182 ~GeolocationProviderTest() { | 116 ~GeolocationProviderTest() {} |
183 } | |
184 | 117 |
185 void WaitAndReset() { | 118 LocationProviderForTestArbitrator* provider() { return provider_.get(); } |
186 event_.Wait(); | 119 |
187 event_.Reset(); | 120 // Called on test thread. |
188 } | 121 bool ProvidersStarted(); |
| 122 |
| 123 private: |
| 124 // Called on provider thread. |
| 125 void GetProvidersStarted(bool* started); |
189 | 126 |
190 MessageLoop message_loop_; | 127 MessageLoop message_loop_; |
191 TestBrowserThread io_thread_; | 128 TestBrowserThread io_thread_; |
192 | |
193 base::WaitableEvent event_; | |
194 scoped_ptr<LocationProviderForTestArbitrator> provider_; | 129 scoped_ptr<LocationProviderForTestArbitrator> provider_; |
195 }; | 130 }; |
196 | 131 |
| 132 |
| 133 bool GeolocationProviderTest::ProvidersStarted() { |
| 134 DCHECK(provider_->IsRunning()); |
| 135 DCHECK(MessageLoop::current() == &message_loop_); |
| 136 bool started; |
| 137 provider_->message_loop_proxy()->PostTaskAndReply( |
| 138 FROM_HERE, |
| 139 base::Bind(&GeolocationProviderTest::GetProvidersStarted, |
| 140 base::Unretained(this), |
| 141 &started), |
| 142 MessageLoop::QuitClosure()); |
| 143 message_loop_.Run(); |
| 144 return started; |
| 145 } |
| 146 |
| 147 void GeolocationProviderTest::GetProvidersStarted(bool* started) { |
| 148 DCHECK(MessageLoop::current() == provider_->message_loop()); |
| 149 *started = provider_->mock_arbitrator()->providers_started(); |
| 150 } |
| 151 |
| 152 |
197 // Regression test for http://crbug.com/59377 | 153 // Regression test for http://crbug.com/59377 |
198 TEST_F(GeolocationProviderTest, OnPermissionGrantedWithoutObservers) { | 154 TEST_F(GeolocationProviderTest, OnPermissionGrantedWithoutObservers) { |
199 EXPECT_FALSE(provider_->HasPermissionBeenGranted()); | 155 EXPECT_FALSE(provider()->HasPermissionBeenGranted()); |
200 provider_->OnPermissionGranted(); | 156 provider()->OnPermissionGranted(); |
201 EXPECT_TRUE(provider_->HasPermissionBeenGranted()); | 157 EXPECT_TRUE(provider()->HasPermissionBeenGranted()); |
202 } | 158 } |
203 | 159 |
204 TEST_F(GeolocationProviderTest, StartStop) { | 160 TEST_F(GeolocationProviderTest, StartStop) { |
205 EXPECT_FALSE(provider_->IsRunning()); | 161 EXPECT_FALSE(provider()->IsRunning()); |
206 NullGeolocationObserver null_observer; | 162 NullGeolocationObserver null_observer; |
207 GeolocationObserverOptions options; | 163 GeolocationObserverOptions options; |
208 provider_->AddObserver(&null_observer, options); | 164 provider()->AddObserver(&null_observer, options); |
209 EXPECT_TRUE(provider_->IsRunning()); | 165 EXPECT_TRUE(provider()->IsRunning()); |
210 // Wait for token load request from the arbitrator to come through. | 166 EXPECT_TRUE(ProvidersStarted()); |
211 WaitAndReset(); | |
212 | 167 |
213 EXPECT_EQ(MockLocationProvider::instance_->state_, | 168 provider()->RemoveObserver(&null_observer); |
214 MockLocationProvider::LOW_ACCURACY); | 169 EXPECT_FALSE(ProvidersStarted()); |
215 provider_->RemoveObserver(&null_observer); | 170 EXPECT_TRUE(provider()->IsRunning()); |
216 // Wait for the providers to be stopped now that all clients are gone. | |
217 WaitAndReset(); | |
218 EXPECT_TRUE(provider_->IsRunning()); | |
219 } | 171 } |
220 | 172 |
221 TEST_F(GeolocationProviderTest, OverrideLocationForTesting) { | 173 TEST_F(GeolocationProviderTest, OverrideLocationForTesting) { |
222 Geoposition position; | 174 Geoposition position; |
223 position.error_code = Geoposition::ERROR_CODE_POSITION_UNAVAILABLE; | 175 position.error_code = Geoposition::ERROR_CODE_POSITION_UNAVAILABLE; |
224 provider_->OverrideLocationForTesting(position); | 176 provider()->OverrideLocationForTesting(position); |
225 // Adding an observer when the location is overridden should synchronously | 177 // Adding an observer when the location is overridden should synchronously |
226 // update the observer with our overridden position. | 178 // update the observer with our overridden position. |
227 MockGeolocationObserver mock_observer; | 179 MockGeolocationObserver mock_observer; |
228 EXPECT_CALL(mock_observer, OnLocationUpdate(GeopositionEq(position))); | 180 EXPECT_CALL(mock_observer, OnLocationUpdate(GeopositionEq(position))); |
229 provider_->AddObserver(&mock_observer, GeolocationObserverOptions()); | 181 provider()->AddObserver(&mock_observer, GeolocationObserverOptions()); |
230 // Wait for token load request from the arbitrator to come through. | 182 provider()->RemoveObserver(&mock_observer); |
231 WaitAndReset(); | |
232 | |
233 provider_->RemoveObserver(&mock_observer); | |
234 // Wait for the providers to be stopped now that all clients are gone. | 183 // Wait for the providers to be stopped now that all clients are gone. |
235 WaitAndReset(); | 184 EXPECT_FALSE(ProvidersStarted()); |
236 } | 185 } |
237 | 186 |
238 TEST_F(GeolocationProviderTest, Callback) { | 187 TEST_F(GeolocationProviderTest, Callback) { |
239 MockGeolocationCallbackWrapper callback_wrapper; | 188 MockGeolocationCallbackWrapper callback_wrapper; |
240 provider_->RequestCallback( | 189 provider()->RequestCallback( |
241 base::Bind(&MockGeolocationCallbackWrapper::Callback, | 190 base::Bind(&MockGeolocationCallbackWrapper::Callback, |
242 base::Unretained(&callback_wrapper))); | 191 base::Unretained(&callback_wrapper))); |
243 // Wait for token load request from the arbitrator to come through. | |
244 WaitAndReset(); | |
245 | |
246 Geoposition position; | 192 Geoposition position; |
247 position.latitude = 12; | 193 position.latitude = 12; |
248 position.longitude = 34; | 194 position.longitude = 34; |
249 position.accuracy = 56; | 195 position.accuracy = 56; |
250 position.timestamp = base::Time::Now(); | 196 position.timestamp = base::Time::Now(); |
251 EXPECT_CALL(callback_wrapper, Callback(GeopositionEq(position))); | 197 EXPECT_CALL(callback_wrapper, Callback(GeopositionEq(position))); |
252 provider_->OverrideLocationForTesting(position); | 198 provider()->OverrideLocationForTesting(position); |
253 // Wait for the providers to be stopped now that all clients are gone. | 199 // Wait for the providers to be stopped now that all clients are gone. |
254 WaitAndReset(); | 200 EXPECT_FALSE(ProvidersStarted()); |
255 } | 201 } |
256 | 202 |
257 } // namespace content | 203 } // namespace content |
OLD | NEW |