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_)) { | 114 { |
bulach
2012/11/28 19:35:59
nit: { on the previous line
John Knottenbelt
2012/11/29 17:31:01
Done.
| |
180 } | 115 } |
181 | 116 |
182 ~GeolocationProviderTest() { | 117 ~GeolocationProviderTest() {} |
183 } | |
184 | 118 |
185 void WaitAndReset() { | 119 LocationProviderForTestArbitrator* provider() { return provider_.get(); } |
186 event_.Wait(); | 120 |
187 event_.Reset(); | 121 // Called on test thread. |
188 } | 122 bool ProvidersStarted(); |
123 | |
124 private: | |
125 // Called on provider thread. | |
126 void GetProvidersStarted(bool* started); | |
189 | 127 |
190 MessageLoop message_loop_; | 128 MessageLoop message_loop_; |
191 TestBrowserThread io_thread_; | 129 TestBrowserThread io_thread_; |
192 | |
193 base::WaitableEvent event_; | |
194 scoped_ptr<LocationProviderForTestArbitrator> provider_; | 130 scoped_ptr<LocationProviderForTestArbitrator> provider_; |
195 }; | 131 }; |
196 | 132 |
133 | |
134 bool GeolocationProviderTest::ProvidersStarted() { | |
135 DCHECK(provider_->IsRunning()); | |
136 DCHECK(MessageLoop::current() == &message_loop_); | |
137 bool started; | |
138 provider_->message_loop_proxy()->PostTaskAndReply( | |
139 FROM_HERE, | |
140 base::Bind(&GeolocationProviderTest::GetProvidersStarted, | |
141 base::Unretained(this), | |
142 &started), | |
143 MessageLoop::QuitClosure()); | |
144 message_loop_.Run(); | |
145 return started; | |
146 } | |
147 | |
148 void GeolocationProviderTest::GetProvidersStarted(bool* started) { | |
149 DCHECK(MessageLoop::current() == provider_->message_loop()); | |
150 *started = provider_->mock_arbitrator()->providers_started(); | |
151 } | |
152 | |
153 | |
197 // Regression test for http://crbug.com/59377 | 154 // Regression test for http://crbug.com/59377 |
198 TEST_F(GeolocationProviderTest, OnPermissionGrantedWithoutObservers) { | 155 TEST_F(GeolocationProviderTest, OnPermissionGrantedWithoutObservers) { |
199 EXPECT_FALSE(provider_->HasPermissionBeenGranted()); | 156 EXPECT_FALSE(provider()->HasPermissionBeenGranted()); |
200 provider_->OnPermissionGranted(); | 157 provider()->OnPermissionGranted(); |
201 EXPECT_TRUE(provider_->HasPermissionBeenGranted()); | 158 EXPECT_TRUE(provider()->HasPermissionBeenGranted()); |
202 } | 159 } |
203 | 160 |
204 TEST_F(GeolocationProviderTest, StartStop) { | 161 TEST_F(GeolocationProviderTest, StartStop) { |
205 EXPECT_FALSE(provider_->IsRunning()); | 162 EXPECT_FALSE(provider()->IsRunning()); |
206 NullGeolocationObserver null_observer; | 163 NullGeolocationObserver null_observer; |
207 GeolocationObserverOptions options; | 164 GeolocationObserverOptions options; |
208 provider_->AddObserver(&null_observer, options); | 165 provider()->AddObserver(&null_observer, options); |
209 EXPECT_TRUE(provider_->IsRunning()); | 166 EXPECT_TRUE(provider()->IsRunning()); |
210 // Wait for token load request from the arbitrator to come through. | 167 EXPECT_TRUE(ProvidersStarted()); |
211 WaitAndReset(); | |
212 | 168 |
213 EXPECT_EQ(MockLocationProvider::instance_->state_, | 169 provider()->RemoveObserver(&null_observer); |
214 MockLocationProvider::LOW_ACCURACY); | 170 EXPECT_FALSE(ProvidersStarted()); |
215 provider_->RemoveObserver(&null_observer); | 171 EXPECT_TRUE(provider()->IsRunning()); |
bulach
2012/11/28 19:35:59
shouldn't the provider be stopped at this stage?
John Knottenbelt
2012/11/29 17:31:01
This may be something we want to look into for the
| |
216 // Wait for the providers to be stopped now that all clients are gone. | |
217 WaitAndReset(); | |
218 EXPECT_TRUE(provider_->IsRunning()); | |
219 } | 172 } |
220 | 173 |
221 TEST_F(GeolocationProviderTest, OverrideLocationForTesting) { | 174 TEST_F(GeolocationProviderTest, OverrideLocationForTesting) { |
222 Geoposition position; | 175 Geoposition position; |
223 position.error_code = Geoposition::ERROR_CODE_POSITION_UNAVAILABLE; | 176 position.error_code = Geoposition::ERROR_CODE_POSITION_UNAVAILABLE; |
224 provider_->OverrideLocationForTesting(position); | 177 provider()->OverrideLocationForTesting(position); |
225 // Adding an observer when the location is overridden should synchronously | 178 // Adding an observer when the location is overridden should synchronously |
226 // update the observer with our overridden position. | 179 // update the observer with our overridden position. |
227 MockGeolocationObserver mock_observer; | 180 MockGeolocationObserver mock_observer; |
228 EXPECT_CALL(mock_observer, OnLocationUpdate(GeopositionEq(position))); | 181 EXPECT_CALL(mock_observer, OnLocationUpdate(GeopositionEq(position))); |
229 provider_->AddObserver(&mock_observer, GeolocationObserverOptions()); | 182 provider()->AddObserver(&mock_observer, GeolocationObserverOptions()); |
230 // Wait for token load request from the arbitrator to come through. | 183 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. | 184 // Wait for the providers to be stopped now that all clients are gone. |
235 WaitAndReset(); | 185 EXPECT_FALSE(ProvidersStarted()); |
236 } | 186 } |
237 | 187 |
238 TEST_F(GeolocationProviderTest, Callback) { | 188 TEST_F(GeolocationProviderTest, Callback) { |
239 MockGeolocationCallbackWrapper callback_wrapper; | 189 MockGeolocationCallbackWrapper callback_wrapper; |
240 provider_->RequestCallback( | 190 provider()->RequestCallback( |
241 base::Bind(&MockGeolocationCallbackWrapper::Callback, | 191 base::Bind(&MockGeolocationCallbackWrapper::Callback, |
242 base::Unretained(&callback_wrapper))); | 192 base::Unretained(&callback_wrapper))); |
243 // Wait for token load request from the arbitrator to come through. | |
244 WaitAndReset(); | |
245 | |
246 Geoposition position; | 193 Geoposition position; |
247 position.latitude = 12; | 194 position.latitude = 12; |
248 position.longitude = 34; | 195 position.longitude = 34; |
249 position.accuracy = 56; | 196 position.accuracy = 56; |
250 position.timestamp = base::Time::Now(); | 197 position.timestamp = base::Time::Now(); |
251 EXPECT_CALL(callback_wrapper, Callback(GeopositionEq(position))); | 198 EXPECT_CALL(callback_wrapper, Callback(GeopositionEq(position))); |
252 provider_->OverrideLocationForTesting(position); | 199 provider()->OverrideLocationForTesting(position); |
253 // Wait for the providers to be stopped now that all clients are gone. | 200 // Wait for the providers to be stopped now that all clients are gone. |
254 WaitAndReset(); | 201 EXPECT_FALSE(ProvidersStarted()); |
255 } | 202 } |
256 | 203 |
257 } // namespace content | 204 } // namespace content |
OLD | NEW |