Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(165)

Side by Side Diff: content/browser/geofencing/geofencing_manager_unittest.cc

Issue 586163003: Basic implementation of GeofencingManager class. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@geofencing5
Patch Set: minor cleanup Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "base/callback.h"
6 #include "base/message_loop/message_loop.h"
7 #include "content/browser/geofencing/geofencing_manager.h"
8 #include "content/browser/geofencing/geofencing_provider.h"
9 #include "content/public/browser/browser_thread.h"
10 #include "content/public/test/test_browser_thread.h"
11 #include "content/public/test/test_utils.h"
12 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include "third_party/WebKit/public/platform/WebCircularGeofencingRegion.h"
15 #include "url/gurl.h"
16
17 using blink::WebCircularGeofencingRegion;
18 typedef std::map<std::string, WebCircularGeofencingRegion> RegionMap;
19
20 namespace {
21
22 static const char* kTestRegionId = "region-id";
23 static const int64 kTestServiceWorkerRegistrationId = 123;
24 static const int64 kTestServiceWorkerRegistrationId2 = 456;
25
26 bool RegionsMatch(const WebCircularGeofencingRegion& expected,
27 const WebCircularGeofencingRegion& arg) {
28 return testing::Matches(expected.latitude)(arg.latitude) &&
29 testing::Matches(expected.longitude)(arg.longitude) &&
30 testing::Matches(expected.radius)(arg.radius);
31 }
32 }
33
34 namespace content {
35
36 class TestGeofencingProvider : public GeofencingProvider {
37 public:
38 MOCK_METHOD2(RegisterRegion,
39 void(const WebCircularGeofencingRegion& region,
40 const RegisterCallback& callback));
41 MOCK_METHOD1(UnregisterRegion, void(int registration_id));
42 };
43
44 ACTION_P2(CallRegisterCallback, status, id) {
45 arg1.Run(status, id);
46 }
47
48 ACTION_P(SaveRegisterCallback, callback) {
49 *callback = arg1;
50 }
51
52 MATCHER_P(WebCircularGeofencingRegionEq, expected, "") {
53 return RegionsMatch(expected, arg);
54 }
55
56 class StatusCatcher {
57 public:
58 StatusCatcher() : was_called_(false), runner_(new MessageLoopRunner()) {}
59
60 void Done(GeofencingStatus status) {
61 CHECK(!was_called_);
62 result_ = status;
63 was_called_ = true;
64 runner_->Quit();
65 }
66
67 GeofencingStatus Wait() {
68 runner_->Run();
69 CHECK(was_called_);
70 return result_;
71 }
72
73 private:
74 bool was_called_;
75 GeofencingStatus result_;
76 scoped_refptr<MessageLoopRunner> runner_;
77 };
78
79 class GeofencingManagerTest : public testing::Test {
80 public:
81 GeofencingManagerTest()
82 : message_loop_(),
83 io_thread_(BrowserThread::IO, &message_loop_),
84 provider_(0),
85 manager_(0),
86 test_origin_("https://example.com/") {
87 test_region_.latitude = 37.421999;
88 test_region_.longitude = -122.084015;
89 test_region_.radius = 100;
90 expected_regions_[kTestRegionId] = test_region_;
91 }
92
93 virtual void SetUp() { manager_ = new GeofencingManager(); }
94
95 virtual void TearDown() { delete manager_; }
96
97 void SetProviderForTests() {
98 provider_ = new TestGeofencingProvider();
99 manager_->SetProviderForTests(scoped_ptr<GeofencingProvider>(provider_));
100 }
101
102 GeofencingStatus RegisterRegionSync(
103 int64 service_worker_registration_id,
104 const std::string& id,
105 const WebCircularGeofencingRegion& region) {
106 StatusCatcher result;
107 manager_->RegisterRegion(
108 nullptr, /* browser_context */
109 service_worker_registration_id,
110 test_origin_,
111 id,
112 region,
113 base::Bind(&StatusCatcher::Done, base::Unretained(&result)));
114 return result.Wait();
115 }
116
117 GeofencingStatus RegisterRegionSyncWithProviderResult(
118 int64 service_worker_registration_id,
119 const std::string& id,
120 const WebCircularGeofencingRegion& region,
121 GeofencingStatus provider_status,
122 int provider_result) {
123 StatusCatcher result;
124 EXPECT_CALL(
125 *provider_,
126 RegisterRegion(WebCircularGeofencingRegionEq(region), testing::_))
127 .WillOnce(CallRegisterCallback(provider_status, provider_result));
128 manager_->RegisterRegion(
129 nullptr, /* browser_context */
130 service_worker_registration_id,
131 test_origin_,
132 id,
133 region,
134 base::Bind(&StatusCatcher::Done, base::Unretained(&result)));
135 return result.Wait();
136 }
137
138 GeofencingStatus UnregisterRegionSync(int64 service_worker_registration_id,
139 const std::string& id,
140 bool should_call_provider,
141 int provider_id = 0) {
142 StatusCatcher result;
143 if (should_call_provider) {
144 EXPECT_CALL(*provider_, UnregisterRegion(provider_id));
145 }
146 manager_->UnregisterRegion(
147 nullptr, /* browser_context */
148 service_worker_registration_id,
149 test_origin_,
150 id,
151 base::Bind(&StatusCatcher::Done, base::Unretained(&result)));
152 return result.Wait();
153 }
154
155 void VerifyRegions(int64 service_worker_registration_id,
156 const RegionMap& expected_regions) {
157 RegionMap regions;
158 EXPECT_EQ(GeofencingStatus::GEOFENCING_STATUS_OK,
159 manager_->GetRegisteredRegions(nullptr, /* browser_context */
160 service_worker_registration_id,
161 test_origin_,
162 &regions));
163 EXPECT_EQ(expected_regions.size(), regions.size());
164 for (RegionMap::const_iterator it = expected_regions.begin();
165 it != expected_regions.end();
166 ++it) {
167 EXPECT_THAT(regions[it->first],
168 WebCircularGeofencingRegionEq(it->second));
169 }
170 }
171
172 protected:
173 base::MessageLoop message_loop_;
174 TestBrowserThread io_thread_;
175 TestGeofencingProvider* provider_;
176 GeofencingManager* manager_;
177
178 WebCircularGeofencingRegion test_region_;
179 RegionMap expected_regions_;
180 GURL test_origin_;
181 };
182
183 TEST_F(GeofencingManagerTest, RegisterRegion_NoProvider) {
184 EXPECT_EQ(GeofencingStatus::
185 GEOFENCING_STATUS_OPERATION_FAILED_SERVICE_NOT_AVAILABLE,
186 RegisterRegionSync(
187 kTestServiceWorkerRegistrationId, kTestRegionId, test_region_));
188 }
189
190 TEST_F(GeofencingManagerTest, UnregisterRegion_NoProvider) {
191 EXPECT_EQ(GeofencingStatus::
192 GEOFENCING_STATUS_OPERATION_FAILED_SERVICE_NOT_AVAILABLE,
193 UnregisterRegionSync(
194 kTestServiceWorkerRegistrationId, kTestRegionId, false));
195 }
196
197 TEST_F(GeofencingManagerTest, GetRegisteredRegions_NoProvider) {
198 RegionMap regions;
199 EXPECT_EQ(GeofencingStatus::
200 GEOFENCING_STATUS_OPERATION_FAILED_SERVICE_NOT_AVAILABLE,
201 manager_->GetRegisteredRegions(nullptr, /* browser_context */
202 kTestServiceWorkerRegistrationId,
203 test_origin_,
204 &regions));
205 EXPECT_TRUE(regions.empty());
206 }
207
208 TEST_F(GeofencingManagerTest, RegisterRegion_FailsInProvider) {
209 SetProviderForTests();
210 EXPECT_EQ(
211 GeofencingStatus::GEOFENCING_STATUS_ERROR,
212 RegisterRegionSyncWithProviderResult(kTestServiceWorkerRegistrationId,
213 kTestRegionId,
214 test_region_,
215 GEOFENCING_STATUS_ERROR,
216 -1));
217 }
218
219 TEST_F(GeofencingManagerTest, RegisterRegion_SucceedsInProvider) {
220 SetProviderForTests();
221 EXPECT_EQ(
222 GeofencingStatus::GEOFENCING_STATUS_OK,
223 RegisterRegionSyncWithProviderResult(kTestServiceWorkerRegistrationId,
224 kTestRegionId,
225 test_region_,
226 GEOFENCING_STATUS_OK,
227 0));
228 VerifyRegions(kTestServiceWorkerRegistrationId, expected_regions_);
229 }
230
231 TEST_F(GeofencingManagerTest, RegisterRegion_AlreadyRegistered) {
232 SetProviderForTests();
233 EXPECT_EQ(
234 GeofencingStatus::GEOFENCING_STATUS_OK,
235 RegisterRegionSyncWithProviderResult(kTestServiceWorkerRegistrationId,
236 kTestRegionId,
237 test_region_,
238 GEOFENCING_STATUS_OK,
239 0));
240 VerifyRegions(kTestServiceWorkerRegistrationId, expected_regions_);
241
242 WebCircularGeofencingRegion region2;
243 region2.latitude = 43.2;
244 region2.longitude = 1.45;
245 region2.radius = 8.5;
246 EXPECT_EQ(GeofencingStatus::GEOFENCING_STATUS_ERROR,
247 RegisterRegionSync(
248 kTestServiceWorkerRegistrationId, kTestRegionId, region2));
249 VerifyRegions(kTestServiceWorkerRegistrationId, expected_regions_);
250 }
251
252 TEST_F(GeofencingManagerTest, UnregisterRegion_NotRegistered) {
253 SetProviderForTests();
254 EXPECT_EQ(GeofencingStatus::GEOFENCING_STATUS_ERROR,
255 UnregisterRegionSync(
256 kTestServiceWorkerRegistrationId, kTestRegionId, false));
257 }
258
259 TEST_F(GeofencingManagerTest, UnregisterRegion_Success) {
260 SetProviderForTests();
261 int provider_id = 123;
262
263 EXPECT_EQ(
264 GeofencingStatus::GEOFENCING_STATUS_OK,
265 RegisterRegionSyncWithProviderResult(kTestServiceWorkerRegistrationId,
266 kTestRegionId,
267 test_region_,
268 GEOFENCING_STATUS_OK,
269 provider_id));
270
271 EXPECT_EQ(
272 GeofencingStatus::GEOFENCING_STATUS_OK,
273 UnregisterRegionSync(
274 kTestServiceWorkerRegistrationId, kTestRegionId, true, provider_id));
275 VerifyRegions(kTestServiceWorkerRegistrationId, RegionMap());
276 }
277
278 TEST_F(GeofencingManagerTest, GetRegisteredRegions_RegistrationInProgress) {
279 SetProviderForTests();
280 StatusCatcher result;
281 GeofencingProvider::RegisterCallback callback;
282
283 EXPECT_CALL(
284 *provider_,
285 RegisterRegion(WebCircularGeofencingRegionEq(test_region_), testing::_))
286 .WillOnce(SaveRegisterCallback(&callback));
287 manager_->RegisterRegion(
288 nullptr, /* browser_context */
289 kTestServiceWorkerRegistrationId,
290 test_origin_,
291 kTestRegionId,
292 test_region_,
293 base::Bind(&StatusCatcher::Done, base::Unretained(&result)));
294
295 // At this point the manager should have tried registering the region with
296 // the provider, resulting in |callback| being set. Until the callback is
297 // called the registration is not complete though.
298 EXPECT_FALSE(callback.is_null());
299 VerifyRegions(kTestServiceWorkerRegistrationId, RegionMap());
300
301 // Now call the callback, and verify the registration completed succesfully.
302 callback.Run(GEOFENCING_STATUS_OK, 123);
303 EXPECT_EQ(GeofencingStatus::GEOFENCING_STATUS_OK, result.Wait());
304 VerifyRegions(kTestServiceWorkerRegistrationId, expected_regions_);
305 }
306
307 TEST_F(GeofencingManagerTest, UnregisterRegion_RegistrationInProgress) {
308 SetProviderForTests();
309 StatusCatcher result;
310 GeofencingProvider::RegisterCallback callback;
311
312 EXPECT_CALL(
313 *provider_,
314 RegisterRegion(WebCircularGeofencingRegionEq(test_region_), testing::_))
315 .WillOnce(SaveRegisterCallback(&callback));
316 manager_->RegisterRegion(
317 nullptr, /* browser_context */
318 kTestServiceWorkerRegistrationId,
319 test_origin_,
320 kTestRegionId,
321 test_region_,
322 base::Bind(&StatusCatcher::Done, base::Unretained(&result)));
323
324 // At this point the manager should have tried registering the region with
325 // the provider, resulting in |callback| being set. Until the callback is
326 // called the registration is not complete though.
327 EXPECT_FALSE(callback.is_null());
328
329 EXPECT_EQ(GeofencingStatus::GEOFENCING_STATUS_ERROR,
330 UnregisterRegionSync(
331 kTestServiceWorkerRegistrationId, kTestRegionId, false));
332 }
333
334 TEST_F(GeofencingManagerTest, GetRegisteredRegions_NoRegions) {
335 SetProviderForTests();
336 VerifyRegions(kTestServiceWorkerRegistrationId, RegionMap());
337 }
338
339 TEST_F(GeofencingManagerTest, RegisterRegion_SeparateServiceWorkers) {
340 SetProviderForTests();
341 int provider_id1 = 12;
342 int provider_id2 = 34;
343
344 EXPECT_EQ(
345 GeofencingStatus::GEOFENCING_STATUS_OK,
346 RegisterRegionSyncWithProviderResult(kTestServiceWorkerRegistrationId,
347 kTestRegionId,
348 test_region_,
349 GEOFENCING_STATUS_OK,
350 provider_id1));
351
352 VerifyRegions(kTestServiceWorkerRegistrationId, expected_regions_);
353 VerifyRegions(kTestServiceWorkerRegistrationId2, RegionMap());
354
355 EXPECT_EQ(
356 GeofencingStatus::GEOFENCING_STATUS_OK,
357 RegisterRegionSyncWithProviderResult(kTestServiceWorkerRegistrationId2,
358 kTestRegionId,
359 test_region_,
360 GEOFENCING_STATUS_OK,
361 provider_id2));
362
363 VerifyRegions(kTestServiceWorkerRegistrationId, expected_regions_);
364 VerifyRegions(kTestServiceWorkerRegistrationId2, expected_regions_);
365 }
366
367 TEST_F(GeofencingManagerTest, UnregisterRegion_SeparateServiceWorkers) {
368 SetProviderForTests();
369 int provider_id1 = 12;
370 int provider_id2 = 34;
371
372 EXPECT_EQ(
373 GeofencingStatus::GEOFENCING_STATUS_OK,
374 RegisterRegionSyncWithProviderResult(kTestServiceWorkerRegistrationId,
375 kTestRegionId,
376 test_region_,
377 GEOFENCING_STATUS_OK,
378 provider_id1));
379 EXPECT_EQ(
380 GeofencingStatus::GEOFENCING_STATUS_OK,
381 RegisterRegionSyncWithProviderResult(kTestServiceWorkerRegistrationId2,
382 kTestRegionId,
383 test_region_,
384 GEOFENCING_STATUS_OK,
385 provider_id2));
386
387 EXPECT_EQ(
388 GeofencingStatus::GEOFENCING_STATUS_OK,
389 UnregisterRegionSync(
390 kTestServiceWorkerRegistrationId, kTestRegionId, true, provider_id1));
391
392 VerifyRegions(kTestServiceWorkerRegistrationId, RegionMap());
393 VerifyRegions(kTestServiceWorkerRegistrationId2, expected_regions_);
394
395 EXPECT_EQ(GeofencingStatus::GEOFENCING_STATUS_OK,
396 UnregisterRegionSync(kTestServiceWorkerRegistrationId2,
397 kTestRegionId,
398 true,
399 provider_id2));
400
401 VerifyRegions(kTestServiceWorkerRegistrationId, RegionMap());
402 VerifyRegions(kTestServiceWorkerRegistrationId2, RegionMap());
403 }
404
405 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/geofencing/geofencing_manager.cc ('k') | content/browser/geofencing/geofencing_provider.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698