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

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

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

Powered by Google App Engine
This is Rietveld 408576698