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

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

Issue 1972733002: Delete geofencing implementation. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: mark histogram suffix as obsolete Created 4 years, 7 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 <stdint.h>
6
7 #include "base/callback.h"
8 #include "base/message_loop/message_loop.h"
9 #include "content/browser/geofencing/geofencing_manager.h"
10 #include "content/browser/geofencing/geofencing_service.h"
11 #include "content/browser/service_worker/embedded_worker_test_helper.h"
12 #include "content/browser/service_worker/service_worker_context_wrapper.h"
13 #include "content/public/test/test_browser_thread_bundle.h"
14 #include "content/public/test/test_utils.h"
15 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h"
17 #include "third_party/WebKit/public/platform/WebCircularGeofencingRegion.h"
18
19 using blink::WebCircularGeofencingRegion;
20 typedef std::map<std::string, WebCircularGeofencingRegion> RegionMap;
21
22 namespace {
23
24 static const char* kTestRegionId = "region-id";
25 static const int64_t kTestGeofencingRegistrationId = 42;
26 static const int64_t kTestGeofencingRegistrationId2 = 43;
27
28 bool RegionsMatch(const WebCircularGeofencingRegion& expected,
29 const WebCircularGeofencingRegion& arg) {
30 return testing::Matches(expected.latitude)(arg.latitude) &&
31 testing::Matches(expected.longitude)(arg.longitude) &&
32 testing::Matches(expected.radius)(arg.radius);
33 }
34 }
35
36 namespace content {
37
38 class TestGeofencingService : public GeofencingService {
39 public:
40 TestGeofencingService() : is_available_(false) {}
41
42 bool IsServiceAvailable() override { return is_available_; }
43
44 void SetIsServiceAvailable(bool is_available) {
45 is_available_ = is_available;
46 }
47
48 MOCK_METHOD2(RegisterRegion,
49 int64_t(const WebCircularGeofencingRegion& region,
50 GeofencingRegistrationDelegate* delegate));
51 MOCK_METHOD1(UnregisterRegion, void(int64_t geofencing_registration_id));
52
53 private:
54 bool is_available_;
55 };
56
57 ACTION_P(SaveDelegate, delegate) {
58 *delegate = arg1;
59 }
60
61 ACTION_P(QuitRunner, runner) {
62 runner->Quit();
63 }
64
65 MATCHER_P(WebCircularGeofencingRegionEq, expected, "") {
66 return RegionsMatch(expected, arg);
67 }
68
69 class StatusCatcher {
70 public:
71 StatusCatcher() : was_called_(false), runner_(new MessageLoopRunner()) {}
72
73 void Done(GeofencingStatus status) {
74 CHECK(!was_called_);
75 result_ = status;
76 was_called_ = true;
77 runner_->Quit();
78 }
79
80 GeofencingStatus Wait() {
81 runner_->Run();
82 CHECK(was_called_);
83 return result_;
84 }
85
86 private:
87 bool was_called_;
88 GeofencingStatus result_;
89 scoped_refptr<MessageLoopRunner> runner_;
90 };
91
92 void SaveResponseCallback(bool* called,
93 int64_t* store_registration_id,
94 ServiceWorkerStatusCode status,
95 const std::string& status_message,
96 int64_t registration_id) {
97 EXPECT_EQ(SERVICE_WORKER_OK, status) << ServiceWorkerStatusToString(status);
98 *called = true;
99 *store_registration_id = registration_id;
100 }
101
102 ServiceWorkerContextCore::RegistrationCallback MakeRegisteredCallback(
103 bool* called,
104 int64_t* store_registration_id) {
105 return base::Bind(&SaveResponseCallback, called, store_registration_id);
106 }
107
108 void CallCompletedCallback(bool* called, ServiceWorkerStatusCode) {
109 *called = true;
110 }
111
112 ServiceWorkerContextCore::UnregistrationCallback MakeUnregisteredCallback(
113 bool* called) {
114 return base::Bind(&CallCompletedCallback, called);
115 }
116
117 class GeofencingManagerTest : public testing::Test {
118 public:
119 GeofencingManagerTest() : service_(nullptr) {
120 test_region_.latitude = 37.421999;
121 test_region_.longitude = -122.084015;
122 test_region_.radius = 100;
123 expected_regions_[kTestRegionId] = test_region_;
124 }
125
126 void SetUp() override {
127 helper_.reset(new EmbeddedWorkerTestHelper(base::FilePath()));
128 service_ = new TestGeofencingService();
129 manager_ = new GeofencingManager(helper_->context_wrapper());
130 manager_->SetServiceForTesting(service_);
131 manager_->Init();
132
133 worker1_ = RegisterServiceWorker("1");
134 worker2_ = RegisterServiceWorker("2");
135 }
136
137 void TearDown() override {
138 worker1_ = nullptr;
139 worker2_ = nullptr;
140 manager_ = nullptr;
141 delete service_;
142 service_ = nullptr;
143 helper_.reset();
144 }
145
146 void SetHasProviderForTests() { service_->SetIsServiceAvailable(true); }
147
148 scoped_refptr<ServiceWorkerRegistration> RegisterServiceWorker(
149 const std::string& name) {
150 GURL pattern("http://www.example.com/" + name);
151 GURL script_url("http://www.example.com/service_worker.js");
152 int64_t registration_id = kInvalidServiceWorkerRegistrationId;
153 bool called = false;
154 helper_->context()->RegisterServiceWorker(
155 pattern, script_url, nullptr,
156 MakeRegisteredCallback(&called, &registration_id));
157
158 EXPECT_FALSE(called);
159 base::RunLoop().RunUntilIdle();
160 EXPECT_TRUE(called);
161 scoped_refptr<ServiceWorkerRegistration> worker(
162 new ServiceWorkerRegistration(pattern, registration_id,
163 helper_->context()->AsWeakPtr()));
164 // ServiceWorkerRegistration posts a notification task on construction.
165 base::RunLoop().RunUntilIdle();
166 return worker;
167 }
168
169 void UnregisterServiceWorker(
170 const scoped_refptr<ServiceWorkerRegistration>& registration) {
171 bool called = false;
172 helper_->context()->UnregisterServiceWorker(
173 registration->pattern(), MakeUnregisteredCallback(&called));
174
175 EXPECT_FALSE(called);
176 base::RunLoop().RunUntilIdle();
177 EXPECT_TRUE(called);
178 }
179
180 GeofencingStatus RegisterRegionSync(
181 int64_t service_worker_registration_id,
182 const std::string& id,
183 const WebCircularGeofencingRegion& region) {
184 StatusCatcher result;
185 manager_->RegisterRegion(
186 service_worker_registration_id,
187 id,
188 region,
189 base::Bind(&StatusCatcher::Done, base::Unretained(&result)));
190 return result.Wait();
191 }
192
193 GeofencingStatus RegisterRegionSyncWithServiceResult(
194 int64_t service_worker_registration_id,
195 const std::string& id,
196 const WebCircularGeofencingRegion& region,
197 GeofencingStatus service_status,
198 int64_t geofencing_registration_id) {
199 StatusCatcher result;
200 GeofencingRegistrationDelegate* delegate = 0;
201 EXPECT_CALL(
202 *service_,
203 RegisterRegion(WebCircularGeofencingRegionEq(region), testing::_))
204 .WillOnce(testing::DoAll(SaveDelegate(&delegate),
205 testing::Return(geofencing_registration_id)));
206 manager_->RegisterRegion(
207 service_worker_registration_id,
208 id,
209 region,
210 base::Bind(&StatusCatcher::Done, base::Unretained(&result)));
211 CHECK(delegate);
212 delegate->RegistrationFinished(geofencing_registration_id, service_status);
213 return result.Wait();
214 }
215
216 GeofencingStatus UnregisterRegionSync(
217 int64_t service_worker_registration_id,
218 const std::string& id,
219 bool should_call_service,
220 int64_t geofencing_registration_id = 0) {
221 StatusCatcher result;
222 if (should_call_service) {
223 EXPECT_CALL(*service_, UnregisterRegion(geofencing_registration_id));
224 }
225 manager_->UnregisterRegion(
226 service_worker_registration_id,
227 id,
228 base::Bind(&StatusCatcher::Done, base::Unretained(&result)));
229 return result.Wait();
230 }
231
232 void VerifyRegions(int64_t service_worker_registration_id,
233 const RegionMap& expected_regions) {
234 RegionMap regions;
235 EXPECT_EQ(GEOFENCING_STATUS_OK,
236 manager_->GetRegisteredRegions(service_worker_registration_id,
237 &regions));
238 EXPECT_EQ(expected_regions.size(), regions.size());
239 for (RegionMap::const_iterator it = expected_regions.begin();
240 it != expected_regions.end();
241 ++it) {
242 EXPECT_THAT(regions[it->first],
243 WebCircularGeofencingRegionEq(it->second));
244 }
245 }
246
247 protected:
248 TestBrowserThreadBundle threads_;
249 std::unique_ptr<EmbeddedWorkerTestHelper> helper_;
250 TestGeofencingService* service_;
251 scoped_refptr<GeofencingManager> manager_;
252
253 WebCircularGeofencingRegion test_region_;
254 RegionMap expected_regions_;
255
256 scoped_refptr<ServiceWorkerRegistration> worker1_;
257 scoped_refptr<ServiceWorkerRegistration> worker2_;
258 };
259
260 TEST_F(GeofencingManagerTest, RegisterRegion_NoService) {
261 EXPECT_EQ(GEOFENCING_STATUS_OPERATION_FAILED_SERVICE_NOT_AVAILABLE,
262 RegisterRegionSync(worker1_->id(), kTestRegionId, test_region_));
263 }
264
265 TEST_F(GeofencingManagerTest, UnregisterRegion_NoService) {
266 EXPECT_EQ(GEOFENCING_STATUS_OPERATION_FAILED_SERVICE_NOT_AVAILABLE,
267 UnregisterRegionSync(worker1_->id(), kTestRegionId, false));
268 }
269
270 TEST_F(GeofencingManagerTest, GetRegisteredRegions_NoService) {
271 RegionMap regions;
272 EXPECT_EQ(GEOFENCING_STATUS_OPERATION_FAILED_SERVICE_NOT_AVAILABLE,
273 manager_->GetRegisteredRegions(worker1_->id(), &regions));
274 EXPECT_TRUE(regions.empty());
275 }
276
277 TEST_F(GeofencingManagerTest, RegisterRegion_FailsInService) {
278 SetHasProviderForTests();
279 EXPECT_EQ(GEOFENCING_STATUS_ERROR,
280 RegisterRegionSyncWithServiceResult(worker1_->id(), kTestRegionId,
281 test_region_,
282 GEOFENCING_STATUS_ERROR, -1));
283 }
284
285 TEST_F(GeofencingManagerTest, RegisterRegion_SucceedsInService) {
286 SetHasProviderForTests();
287 EXPECT_EQ(GEOFENCING_STATUS_OK,
288 RegisterRegionSyncWithServiceResult(
289 worker1_->id(), kTestRegionId, test_region_,
290 GEOFENCING_STATUS_OK, kTestGeofencingRegistrationId));
291 VerifyRegions(worker1_->id(), expected_regions_);
292 }
293
294 TEST_F(GeofencingManagerTest, RegisterRegion_AlreadyRegistered) {
295 SetHasProviderForTests();
296 EXPECT_EQ(GEOFENCING_STATUS_OK,
297 RegisterRegionSyncWithServiceResult(
298 worker1_->id(), kTestRegionId, test_region_,
299 GEOFENCING_STATUS_OK, kTestGeofencingRegistrationId));
300 VerifyRegions(worker1_->id(), expected_regions_);
301
302 WebCircularGeofencingRegion region2;
303 region2.latitude = 43.2;
304 region2.longitude = 1.45;
305 region2.radius = 8.5;
306 EXPECT_EQ(GEOFENCING_STATUS_ERROR,
307 RegisterRegionSync(worker1_->id(), kTestRegionId, region2));
308 VerifyRegions(worker1_->id(), expected_regions_);
309 }
310
311 TEST_F(GeofencingManagerTest, UnregisterRegion_NotRegistered) {
312 SetHasProviderForTests();
313 EXPECT_EQ(GEOFENCING_STATUS_UNREGISTRATION_FAILED_NOT_REGISTERED,
314 UnregisterRegionSync(worker1_->id(), kTestRegionId, false));
315 }
316
317 TEST_F(GeofencingManagerTest, UnregisterRegion_Success) {
318 SetHasProviderForTests();
319
320 EXPECT_EQ(GEOFENCING_STATUS_OK,
321 RegisterRegionSyncWithServiceResult(
322 worker1_->id(), kTestRegionId, test_region_,
323 GEOFENCING_STATUS_OK, kTestGeofencingRegistrationId));
324
325 EXPECT_EQ(GEOFENCING_STATUS_OK,
326 UnregisterRegionSync(worker1_->id(), kTestRegionId, true,
327 kTestGeofencingRegistrationId));
328 VerifyRegions(worker1_->id(), RegionMap());
329 }
330
331 TEST_F(GeofencingManagerTest, GetRegisteredRegions_RegistrationInProgress) {
332 SetHasProviderForTests();
333 StatusCatcher result;
334 GeofencingRegistrationDelegate* delegate = nullptr;
335
336 EXPECT_CALL(
337 *service_,
338 RegisterRegion(WebCircularGeofencingRegionEq(test_region_), testing::_))
339 .WillOnce(testing::DoAll(SaveDelegate(&delegate),
340 testing::Return(kTestGeofencingRegistrationId)));
341 manager_->RegisterRegion(
342 worker1_->id(), kTestRegionId, test_region_,
343 base::Bind(&StatusCatcher::Done, base::Unretained(&result)));
344
345 // At this point the manager should have tried registering the region with
346 // the service, resulting in |delegate| being set. Until the callback is
347 // called the registration is not complete though.
348 EXPECT_NE(delegate, nullptr);
349 VerifyRegions(worker1_->id(), RegionMap());
350
351 // Now call the callback, and verify the registration completed succesfully.
352 delegate->RegistrationFinished(kTestGeofencingRegistrationId,
353 GEOFENCING_STATUS_OK);
354 EXPECT_EQ(GEOFENCING_STATUS_OK, result.Wait());
355 VerifyRegions(worker1_->id(), expected_regions_);
356 }
357
358 TEST_F(GeofencingManagerTest, UnregisterRegion_RegistrationInProgress) {
359 SetHasProviderForTests();
360 StatusCatcher result;
361 GeofencingRegistrationDelegate* delegate = nullptr;
362
363 EXPECT_CALL(
364 *service_,
365 RegisterRegion(WebCircularGeofencingRegionEq(test_region_), testing::_))
366 .WillOnce(testing::DoAll(SaveDelegate(&delegate),
367 testing::Return(kTestGeofencingRegistrationId)));
368 manager_->RegisterRegion(
369 worker1_->id(), kTestRegionId, test_region_,
370 base::Bind(&StatusCatcher::Done, base::Unretained(&result)));
371
372 // At this point the manager should have tried registering the region with
373 // the service, resulting in |delegate| being set. Until the callback is
374 // called the registration is not complete though.
375 EXPECT_NE(delegate, nullptr);
376
377 EXPECT_EQ(GEOFENCING_STATUS_UNREGISTRATION_FAILED_NOT_REGISTERED,
378 UnregisterRegionSync(worker1_->id(), kTestRegionId, false));
379 }
380
381 TEST_F(GeofencingManagerTest, GetRegisteredRegions_NoRegions) {
382 SetHasProviderForTests();
383 VerifyRegions(worker1_->id(), RegionMap());
384 }
385
386 TEST_F(GeofencingManagerTest, RegisterRegion_SeparateServiceWorkers) {
387 SetHasProviderForTests();
388
389 EXPECT_EQ(GEOFENCING_STATUS_OK,
390 RegisterRegionSyncWithServiceResult(
391 worker1_->id(), kTestRegionId, test_region_,
392 GEOFENCING_STATUS_OK, kTestGeofencingRegistrationId));
393
394 VerifyRegions(worker1_->id(), expected_regions_);
395 VerifyRegions(worker2_->id(), RegionMap());
396
397 EXPECT_EQ(GEOFENCING_STATUS_OK,
398 RegisterRegionSyncWithServiceResult(
399 worker2_->id(), kTestRegionId, test_region_,
400 GEOFENCING_STATUS_OK, kTestGeofencingRegistrationId2));
401
402 VerifyRegions(worker1_->id(), expected_regions_);
403 VerifyRegions(worker2_->id(), expected_regions_);
404 }
405
406 TEST_F(GeofencingManagerTest, UnregisterRegion_SeparateServiceWorkers) {
407 SetHasProviderForTests();
408
409 EXPECT_EQ(GEOFENCING_STATUS_OK,
410 RegisterRegionSyncWithServiceResult(
411 worker1_->id(), kTestRegionId, test_region_,
412 GEOFENCING_STATUS_OK, kTestGeofencingRegistrationId));
413 EXPECT_EQ(GEOFENCING_STATUS_OK,
414 RegisterRegionSyncWithServiceResult(
415 worker2_->id(), kTestRegionId, test_region_,
416 GEOFENCING_STATUS_OK, kTestGeofencingRegistrationId2));
417
418 EXPECT_EQ(GEOFENCING_STATUS_OK,
419 UnregisterRegionSync(worker1_->id(), kTestRegionId, true,
420 kTestGeofencingRegistrationId));
421
422 VerifyRegions(worker1_->id(), RegionMap());
423 VerifyRegions(worker2_->id(), expected_regions_);
424
425 EXPECT_EQ(GEOFENCING_STATUS_OK,
426 UnregisterRegionSync(worker2_->id(), kTestRegionId, true,
427 kTestGeofencingRegistrationId2));
428
429 VerifyRegions(worker1_->id(), RegionMap());
430 VerifyRegions(worker2_->id(), RegionMap());
431 }
432
433 TEST_F(GeofencingManagerTest, ShutdownCleansRegistrations) {
434 SetHasProviderForTests();
435 scoped_refptr<MessageLoopRunner> runner(new MessageLoopRunner());
436 EXPECT_EQ(GEOFENCING_STATUS_OK,
437 RegisterRegionSyncWithServiceResult(
438 worker1_->id(), kTestRegionId, test_region_,
439 GEOFENCING_STATUS_OK, kTestGeofencingRegistrationId));
440
441 EXPECT_CALL(*service_, UnregisterRegion(kTestGeofencingRegistrationId))
442 .WillOnce(QuitRunner(runner));
443 manager_->Shutdown();
444 runner->Run();
445 }
446
447 TEST_F(GeofencingManagerTest, OnRegistrationDeleted) {
448 SetHasProviderForTests();
449
450 EXPECT_EQ(GEOFENCING_STATUS_OK,
451 RegisterRegionSyncWithServiceResult(
452 worker1_->id(), kTestRegionId, test_region_,
453 GEOFENCING_STATUS_OK, kTestGeofencingRegistrationId));
454 EXPECT_EQ(GEOFENCING_STATUS_OK,
455 RegisterRegionSyncWithServiceResult(
456 worker2_->id(), kTestRegionId, test_region_,
457 GEOFENCING_STATUS_OK, kTestGeofencingRegistrationId2));
458
459 EXPECT_CALL(*service_, UnregisterRegion(kTestGeofencingRegistrationId));
460 UnregisterServiceWorker(worker1_);
461 VerifyRegions(worker1_->id(), RegionMap());
462 VerifyRegions(worker2_->id(), expected_regions_);
463
464 EXPECT_CALL(*service_, UnregisterRegion(kTestGeofencingRegistrationId2));
465 UnregisterServiceWorker(worker2_);
466 VerifyRegions(worker1_->id(), RegionMap());
467 VerifyRegions(worker2_->id(), RegionMap());
468 }
469
470 TEST_F(GeofencingManagerTest, RegisterRegion_MockedNoService) {
471 manager_->SetMockProvider(GeofencingMockState::SERVICE_UNAVAILABLE);
472
473 EXPECT_EQ(GEOFENCING_STATUS_OPERATION_FAILED_SERVICE_NOT_AVAILABLE,
474 RegisterRegionSync(worker1_->id(), kTestRegionId, test_region_));
475 }
476
477 TEST_F(GeofencingManagerTest, UnregisterRegion_MockedNoService) {
478 manager_->SetMockProvider(GeofencingMockState::SERVICE_UNAVAILABLE);
479
480 EXPECT_EQ(GEOFENCING_STATUS_OPERATION_FAILED_SERVICE_NOT_AVAILABLE,
481 UnregisterRegionSync(worker1_->id(), kTestRegionId, false));
482 }
483
484 TEST_F(GeofencingManagerTest, GetRegisteredRegions_MockedNoService) {
485 manager_->SetMockProvider(GeofencingMockState::SERVICE_UNAVAILABLE);
486
487 RegionMap regions;
488 EXPECT_EQ(GEOFENCING_STATUS_OPERATION_FAILED_SERVICE_NOT_AVAILABLE,
489 manager_->GetRegisteredRegions(worker1_->id(), &regions));
490 EXPECT_TRUE(regions.empty());
491 }
492
493 TEST_F(GeofencingManagerTest, RegisterRegion_MockedService) {
494 manager_->SetMockProvider(GeofencingMockState::SERVICE_AVAILABLE);
495
496 // Make sure real service doesn't get called.
497 EXPECT_CALL(*service_, RegisterRegion(testing::_, testing::_)).Times(0);
498
499 EXPECT_EQ(GEOFENCING_STATUS_OK,
500 RegisterRegionSync(worker1_->id(), kTestRegionId, test_region_));
501 VerifyRegions(worker1_->id(), expected_regions_);
502 }
503
504 TEST_F(GeofencingManagerTest, SetMockProviderClearsRegistrations) {
505 SetHasProviderForTests();
506 EXPECT_EQ(GEOFENCING_STATUS_OK,
507 RegisterRegionSyncWithServiceResult(
508 worker1_->id(), kTestRegionId, test_region_,
509 GEOFENCING_STATUS_OK, kTestGeofencingRegistrationId));
510 VerifyRegions(worker1_->id(), expected_regions_);
511
512 EXPECT_CALL(*service_, UnregisterRegion(kTestGeofencingRegistrationId));
513
514 manager_->SetMockProvider(GeofencingMockState::SERVICE_AVAILABLE);
515 VerifyRegions(worker1_->id(), RegionMap());
516 }
517
518 } // 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