Simplify ChildProcessLauncher
[chromium-blink-merge.git] / content / browser / service_worker / service_worker_context_unittest.cc
blob60ef24dcc855a682dec6c11bbc05bba5ddb98ed4
1 // Copyright 2013 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.
5 #include "content/public/browser/service_worker_context.h"
7 #include "base/files/scoped_temp_dir.h"
8 #include "base/logging.h"
9 #include "base/message_loop/message_loop.h"
10 #include "content/browser/browser_thread_impl.h"
11 #include "content/browser/service_worker/embedded_worker_registry.h"
12 #include "content/browser/service_worker/embedded_worker_test_helper.h"
13 #include "content/browser/service_worker/service_worker_context_core.h"
14 #include "content/browser/service_worker/service_worker_context_observer.h"
15 #include "content/browser/service_worker/service_worker_context_wrapper.h"
16 #include "content/browser/service_worker/service_worker_provider_host.h"
17 #include "content/browser/service_worker/service_worker_registration.h"
18 #include "content/browser/service_worker/service_worker_storage.h"
19 #include "content/common/service_worker/embedded_worker_messages.h"
20 #include "content/common/service_worker/service_worker_messages.h"
21 #include "content/public/test/test_browser_thread_bundle.h"
22 #include "content/public/test/test_utils.h"
23 #include "testing/gtest/include/gtest/gtest.h"
25 namespace content {
27 namespace {
29 void SaveResponseCallback(bool* called,
30 int64* store_registration_id,
31 ServiceWorkerStatusCode status,
32 const std::string& status_message,
33 int64 registration_id) {
34 EXPECT_EQ(SERVICE_WORKER_OK, status) << ServiceWorkerStatusToString(status);
35 *called = true;
36 *store_registration_id = registration_id;
39 ServiceWorkerContextCore::RegistrationCallback MakeRegisteredCallback(
40 bool* called,
41 int64* store_registration_id) {
42 return base::Bind(&SaveResponseCallback, called, store_registration_id);
45 void CallCompletedCallback(bool* called, ServiceWorkerStatusCode) {
46 *called = true;
49 ServiceWorkerContextCore::UnregistrationCallback MakeUnregisteredCallback(
50 bool* called) {
51 return base::Bind(&CallCompletedCallback, called);
54 void ExpectRegisteredWorkers(
55 ServiceWorkerStatusCode expect_status,
56 bool expect_waiting,
57 bool expect_active,
58 ServiceWorkerStatusCode status,
59 const scoped_refptr<ServiceWorkerRegistration>& registration) {
60 ASSERT_EQ(expect_status, status);
61 if (status != SERVICE_WORKER_OK) {
62 EXPECT_FALSE(registration.get());
63 return;
66 if (expect_waiting) {
67 EXPECT_TRUE(registration->waiting_version());
68 } else {
69 EXPECT_FALSE(registration->waiting_version());
72 if (expect_active) {
73 EXPECT_TRUE(registration->active_version());
74 } else {
75 EXPECT_FALSE(registration->active_version());
79 class RejectInstallTestHelper : public EmbeddedWorkerTestHelper {
80 public:
81 explicit RejectInstallTestHelper(int mock_render_process_id)
82 : EmbeddedWorkerTestHelper(base::FilePath(), mock_render_process_id) {}
84 void OnInstallEvent(int embedded_worker_id,
85 int request_id) override {
86 SimulateSend(
87 new ServiceWorkerHostMsg_InstallEventFinished(
88 embedded_worker_id, request_id,
89 blink::WebServiceWorkerEventResultRejected));
93 class RejectActivateTestHelper : public EmbeddedWorkerTestHelper {
94 public:
95 explicit RejectActivateTestHelper(int mock_render_process_id)
96 : EmbeddedWorkerTestHelper(base::FilePath(), mock_render_process_id) {}
98 void OnActivateEvent(int embedded_worker_id, int request_id) override {
99 SimulateSend(
100 new ServiceWorkerHostMsg_ActivateEventFinished(
101 embedded_worker_id, request_id,
102 blink::WebServiceWorkerEventResultRejected));
106 enum NotificationType {
107 REGISTRATION_STORED,
108 REGISTRATION_DELETED,
109 STORAGE_RECOVERED,
112 struct NotificationLog {
113 NotificationType type;
114 GURL pattern;
115 int64 registration_id;
118 } // namespace
120 class ServiceWorkerContextTest : public ServiceWorkerContextObserver,
121 public testing::Test {
122 public:
123 ServiceWorkerContextTest()
124 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP),
125 render_process_id_(99) {}
127 void SetUp() override {
128 helper_.reset(
129 new EmbeddedWorkerTestHelper(base::FilePath(), render_process_id_));
130 helper_->context_wrapper()->AddObserver(this);
133 void TearDown() override { helper_.reset(); }
135 // ServiceWorkerContextObserver overrides.
136 void OnRegistrationStored(int64 registration_id,
137 const GURL& pattern) override {
138 NotificationLog log;
139 log.type = REGISTRATION_STORED;
140 log.pattern = pattern;
141 log.registration_id = registration_id;
142 notifications_.push_back(log);
144 void OnRegistrationDeleted(int64 registration_id,
145 const GURL& pattern) override {
146 NotificationLog log;
147 log.type = REGISTRATION_DELETED;
148 log.pattern = pattern;
149 log.registration_id = registration_id;
150 notifications_.push_back(log);
152 void OnStorageWiped() override {
153 NotificationLog log;
154 log.type = STORAGE_RECOVERED;
155 notifications_.push_back(log);
158 ServiceWorkerContextCore* context() { return helper_->context(); }
160 protected:
161 TestBrowserThreadBundle browser_thread_bundle_;
162 scoped_ptr<EmbeddedWorkerTestHelper> helper_;
163 const int render_process_id_;
164 std::vector<NotificationLog> notifications_;
167 // Make sure basic registration is working.
168 TEST_F(ServiceWorkerContextTest, Register) {
169 GURL pattern("http://www.example.com/");
170 GURL script_url("http://www.example.com/service_worker.js");
172 int64 registration_id = kInvalidServiceWorkerRegistrationId;
173 bool called = false;
174 context()->RegisterServiceWorker(
175 pattern,
176 script_url,
177 NULL,
178 MakeRegisteredCallback(&called, &registration_id));
180 ASSERT_FALSE(called);
181 base::RunLoop().RunUntilIdle();
182 EXPECT_TRUE(called);
184 EXPECT_EQ(4UL, helper_->ipc_sink()->message_count());
185 EXPECT_TRUE(helper_->ipc_sink()->GetUniqueMessageMatching(
186 EmbeddedWorkerMsg_StartWorker::ID));
187 EXPECT_TRUE(helper_->inner_ipc_sink()->GetUniqueMessageMatching(
188 ServiceWorkerMsg_InstallEvent::ID));
189 EXPECT_TRUE(helper_->inner_ipc_sink()->GetUniqueMessageMatching(
190 ServiceWorkerMsg_ActivateEvent::ID));
191 EXPECT_TRUE(helper_->ipc_sink()->GetUniqueMessageMatching(
192 EmbeddedWorkerMsg_StopWorker::ID));
193 EXPECT_NE(kInvalidServiceWorkerRegistrationId, registration_id);
195 context()->storage()->FindRegistrationForId(
196 registration_id,
197 pattern.GetOrigin(),
198 base::Bind(&ExpectRegisteredWorkers,
199 SERVICE_WORKER_OK,
200 false /* expect_waiting */,
201 true /* expect_active */));
202 base::RunLoop().RunUntilIdle();
204 ASSERT_EQ(1u, notifications_.size());
205 EXPECT_EQ(REGISTRATION_STORED, notifications_[0].type);
206 EXPECT_EQ(pattern, notifications_[0].pattern);
207 EXPECT_EQ(registration_id, notifications_[0].registration_id);
210 // Test registration when the service worker rejects the install event. The
211 // registration callback should indicate success, but there should be no waiting
212 // or active worker in the registration.
213 TEST_F(ServiceWorkerContextTest, Register_RejectInstall) {
214 GURL pattern("http://www.example.com/");
215 GURL script_url("http://www.example.com/service_worker.js");
217 helper_.reset(); // Make sure the process lookups stay overridden.
218 helper_.reset(new RejectInstallTestHelper(render_process_id_));
219 int64 registration_id = kInvalidServiceWorkerRegistrationId;
220 bool called = false;
221 context()->RegisterServiceWorker(
222 pattern,
223 script_url,
224 NULL,
225 MakeRegisteredCallback(&called, &registration_id));
227 ASSERT_FALSE(called);
228 base::RunLoop().RunUntilIdle();
229 EXPECT_TRUE(called);
231 EXPECT_EQ(3UL, helper_->ipc_sink()->message_count());
232 EXPECT_TRUE(helper_->ipc_sink()->GetUniqueMessageMatching(
233 EmbeddedWorkerMsg_StartWorker::ID));
234 EXPECT_TRUE(helper_->inner_ipc_sink()->GetUniqueMessageMatching(
235 ServiceWorkerMsg_InstallEvent::ID));
236 EXPECT_FALSE(helper_->inner_ipc_sink()->GetUniqueMessageMatching(
237 ServiceWorkerMsg_ActivateEvent::ID));
238 EXPECT_TRUE(helper_->ipc_sink()->GetUniqueMessageMatching(
239 EmbeddedWorkerMsg_StopWorker::ID));
240 EXPECT_NE(kInvalidServiceWorkerRegistrationId, registration_id);
242 context()->storage()->FindRegistrationForId(
243 registration_id,
244 pattern.GetOrigin(),
245 base::Bind(&ExpectRegisteredWorkers,
246 SERVICE_WORKER_ERROR_NOT_FOUND,
247 false /* expect_waiting */,
248 false /* expect_active */));
249 base::RunLoop().RunUntilIdle();
251 EXPECT_TRUE(notifications_.empty());
254 // Test registration when the service worker rejects the activate event. The
255 // registration callback should indicate success, but there should be no waiting
256 // or active worker in the registration.
257 TEST_F(ServiceWorkerContextTest, Register_RejectActivate) {
258 helper_.reset(); // Make sure the process lookups stay overridden.
259 helper_.reset(new RejectActivateTestHelper(render_process_id_));
260 int64 registration_id = kInvalidServiceWorkerRegistrationId;
261 bool called = false;
262 context()->RegisterServiceWorker(
263 GURL("http://www.example.com/"),
264 GURL("http://www.example.com/service_worker.js"),
265 NULL,
266 MakeRegisteredCallback(&called, &registration_id));
268 ASSERT_FALSE(called);
269 base::RunLoop().RunUntilIdle();
270 EXPECT_TRUE(called);
272 EXPECT_EQ(4UL, helper_->ipc_sink()->message_count());
273 EXPECT_TRUE(helper_->ipc_sink()->GetUniqueMessageMatching(
274 EmbeddedWorkerMsg_StartWorker::ID));
275 EXPECT_TRUE(helper_->inner_ipc_sink()->GetUniqueMessageMatching(
276 ServiceWorkerMsg_InstallEvent::ID));
277 EXPECT_TRUE(helper_->inner_ipc_sink()->GetUniqueMessageMatching(
278 ServiceWorkerMsg_ActivateEvent::ID));
279 EXPECT_TRUE(helper_->ipc_sink()->GetUniqueMessageMatching(
280 EmbeddedWorkerMsg_StopWorker::ID));
281 EXPECT_NE(kInvalidServiceWorkerRegistrationId, registration_id);
283 context()->storage()->FindRegistrationForId(
284 registration_id,
285 GURL("http://www.example.com"),
286 base::Bind(&ExpectRegisteredWorkers,
287 SERVICE_WORKER_ERROR_NOT_FOUND,
288 false /* expect_waiting */,
289 false /* expect_active */));
290 base::RunLoop().RunUntilIdle();
292 EXPECT_TRUE(notifications_.empty());
295 // Make sure registrations are cleaned up when they are unregistered.
296 TEST_F(ServiceWorkerContextTest, Unregister) {
297 GURL pattern("http://www.example.com/");
299 bool called = false;
300 int64 registration_id = kInvalidServiceWorkerRegistrationId;
301 context()->RegisterServiceWorker(
302 pattern,
303 GURL("http://www.example.com/service_worker.js"),
304 NULL,
305 MakeRegisteredCallback(&called, &registration_id));
307 ASSERT_FALSE(called);
308 base::RunLoop().RunUntilIdle();
309 ASSERT_TRUE(called);
310 EXPECT_NE(kInvalidServiceWorkerRegistrationId, registration_id);
312 called = false;
313 context()->UnregisterServiceWorker(pattern,
314 MakeUnregisteredCallback(&called));
316 ASSERT_FALSE(called);
317 base::RunLoop().RunUntilIdle();
318 ASSERT_TRUE(called);
320 context()->storage()->FindRegistrationForId(
321 registration_id,
322 pattern.GetOrigin(),
323 base::Bind(&ExpectRegisteredWorkers,
324 SERVICE_WORKER_ERROR_NOT_FOUND,
325 false /* expect_waiting */,
326 false /* expect_active */));
327 base::RunLoop().RunUntilIdle();
329 ASSERT_EQ(2u, notifications_.size());
330 EXPECT_EQ(REGISTRATION_STORED, notifications_[0].type);
331 EXPECT_EQ(pattern, notifications_[0].pattern);
332 EXPECT_EQ(registration_id, notifications_[0].registration_id);
333 EXPECT_EQ(REGISTRATION_DELETED, notifications_[1].type);
334 EXPECT_EQ(pattern, notifications_[1].pattern);
335 EXPECT_EQ(registration_id, notifications_[1].registration_id);
338 // Make sure registrations are cleaned up when they are unregistered in bulk.
339 TEST_F(ServiceWorkerContextTest, UnregisterMultiple) {
340 GURL origin1_p1("http://www.example.com/test");
341 GURL origin1_p2("http://www.example.com/hello");
342 GURL origin2_p1("http://www.example.com:8080/again");
343 GURL origin3_p1("http://www.other.com/");
345 bool called = false;
346 int64 registration_id1 = kInvalidServiceWorkerRegistrationId;
347 int64 registration_id2 = kInvalidServiceWorkerRegistrationId;
348 int64 registration_id3 = kInvalidServiceWorkerRegistrationId;
349 int64 registration_id4 = kInvalidServiceWorkerRegistrationId;
350 context()->RegisterServiceWorker(
351 origin1_p1,
352 GURL("http://www.example.com/service_worker.js"),
353 NULL,
354 MakeRegisteredCallback(&called, &registration_id1));
355 context()->RegisterServiceWorker(
356 origin1_p2,
357 GURL("http://www.example.com/service_worker2.js"),
358 NULL,
359 MakeRegisteredCallback(&called, &registration_id2));
360 context()->RegisterServiceWorker(
361 origin2_p1,
362 GURL("http://www.example.com:8080/service_worker3.js"),
363 NULL,
364 MakeRegisteredCallback(&called, &registration_id3));
365 context()->RegisterServiceWorker(
366 origin3_p1,
367 GURL("http://www.other.com/service_worker4.js"),
368 NULL,
369 MakeRegisteredCallback(&called, &registration_id4));
371 ASSERT_FALSE(called);
372 base::RunLoop().RunUntilIdle();
373 ASSERT_TRUE(called);
375 EXPECT_NE(kInvalidServiceWorkerRegistrationId, registration_id1);
376 EXPECT_NE(kInvalidServiceWorkerRegistrationId, registration_id2);
377 EXPECT_NE(kInvalidServiceWorkerRegistrationId, registration_id3);
378 EXPECT_NE(kInvalidServiceWorkerRegistrationId, registration_id4);
380 called = false;
381 context()->UnregisterServiceWorkers(origin1_p1.GetOrigin(),
382 MakeUnregisteredCallback(&called));
384 ASSERT_FALSE(called);
385 base::RunLoop().RunUntilIdle();
386 ASSERT_TRUE(called);
388 context()->storage()->FindRegistrationForId(
389 registration_id1,
390 origin1_p1.GetOrigin(),
391 base::Bind(&ExpectRegisteredWorkers,
392 SERVICE_WORKER_ERROR_NOT_FOUND,
393 false /* expect_waiting */,
394 false /* expect_active */));
395 context()->storage()->FindRegistrationForId(
396 registration_id2,
397 origin1_p2.GetOrigin(),
398 base::Bind(&ExpectRegisteredWorkers,
399 SERVICE_WORKER_ERROR_NOT_FOUND,
400 false /* expect_waiting */,
401 false /* expect_active */));
402 context()->storage()->FindRegistrationForId(
403 registration_id3,
404 origin2_p1.GetOrigin(),
405 base::Bind(&ExpectRegisteredWorkers,
406 SERVICE_WORKER_OK,
407 false /* expect_waiting */,
408 true /* expect_active */));
410 context()->storage()->FindRegistrationForId(
411 registration_id4,
412 origin3_p1.GetOrigin(),
413 base::Bind(&ExpectRegisteredWorkers,
414 SERVICE_WORKER_OK,
415 false /* expect_waiting */,
416 true /* expect_active */));
418 base::RunLoop().RunUntilIdle();
420 ASSERT_EQ(6u, notifications_.size());
421 EXPECT_EQ(REGISTRATION_STORED, notifications_[0].type);
422 EXPECT_EQ(registration_id1, notifications_[0].registration_id);
423 EXPECT_EQ(origin1_p1, notifications_[0].pattern);
424 EXPECT_EQ(REGISTRATION_STORED, notifications_[1].type);
425 EXPECT_EQ(origin1_p2, notifications_[1].pattern);
426 EXPECT_EQ(registration_id2, notifications_[1].registration_id);
427 EXPECT_EQ(REGISTRATION_STORED, notifications_[2].type);
428 EXPECT_EQ(origin2_p1, notifications_[2].pattern);
429 EXPECT_EQ(registration_id3, notifications_[2].registration_id);
430 EXPECT_EQ(REGISTRATION_STORED, notifications_[3].type);
431 EXPECT_EQ(origin3_p1, notifications_[3].pattern);
432 EXPECT_EQ(registration_id4, notifications_[3].registration_id);
433 EXPECT_EQ(REGISTRATION_DELETED, notifications_[4].type);
434 EXPECT_EQ(origin1_p2, notifications_[4].pattern);
435 EXPECT_EQ(registration_id2, notifications_[4].registration_id);
436 EXPECT_EQ(REGISTRATION_DELETED, notifications_[5].type);
437 EXPECT_EQ(origin1_p1, notifications_[5].pattern);
438 EXPECT_EQ(registration_id1, notifications_[5].registration_id);
441 // Make sure registering a new script shares an existing registration.
442 TEST_F(ServiceWorkerContextTest, RegisterNewScript) {
443 GURL pattern("http://www.example.com/");
445 bool called = false;
446 int64 old_registration_id = kInvalidServiceWorkerRegistrationId;
447 context()->RegisterServiceWorker(
448 pattern,
449 GURL("http://www.example.com/service_worker.js"),
450 NULL,
451 MakeRegisteredCallback(&called, &old_registration_id));
453 ASSERT_FALSE(called);
454 base::RunLoop().RunUntilIdle();
455 ASSERT_TRUE(called);
456 EXPECT_NE(kInvalidServiceWorkerRegistrationId, old_registration_id);
458 called = false;
459 int64 new_registration_id = kInvalidServiceWorkerRegistrationId;
460 context()->RegisterServiceWorker(
461 pattern,
462 GURL("http://www.example.com/service_worker_new.js"),
463 NULL,
464 MakeRegisteredCallback(&called, &new_registration_id));
466 ASSERT_FALSE(called);
467 base::RunLoop().RunUntilIdle();
468 ASSERT_TRUE(called);
470 EXPECT_NE(kInvalidServiceWorkerRegistrationId, new_registration_id);
471 EXPECT_EQ(old_registration_id, new_registration_id);
473 ASSERT_EQ(2u, notifications_.size());
474 EXPECT_EQ(REGISTRATION_STORED, notifications_[0].type);
475 EXPECT_EQ(pattern, notifications_[0].pattern);
476 EXPECT_EQ(old_registration_id, notifications_[0].registration_id);
477 EXPECT_EQ(REGISTRATION_STORED, notifications_[1].type);
478 EXPECT_EQ(pattern, notifications_[1].pattern);
479 EXPECT_EQ(new_registration_id, notifications_[1].registration_id);
482 // Make sure that when registering a duplicate pattern+script_url
483 // combination, that the same registration is used.
484 TEST_F(ServiceWorkerContextTest, RegisterDuplicateScript) {
485 GURL pattern("http://www.example.com/");
486 GURL script_url("http://www.example.com/service_worker.js");
488 bool called = false;
489 int64 old_registration_id = kInvalidServiceWorkerRegistrationId;
490 context()->RegisterServiceWorker(
491 pattern,
492 script_url,
493 NULL,
494 MakeRegisteredCallback(&called, &old_registration_id));
496 ASSERT_FALSE(called);
497 base::RunLoop().RunUntilIdle();
498 ASSERT_TRUE(called);
499 EXPECT_NE(kInvalidServiceWorkerRegistrationId, old_registration_id);
501 called = false;
502 int64 new_registration_id = kInvalidServiceWorkerRegistrationId;
503 context()->RegisterServiceWorker(
504 pattern,
505 script_url,
506 NULL,
507 MakeRegisteredCallback(&called, &new_registration_id));
509 ASSERT_FALSE(called);
510 base::RunLoop().RunUntilIdle();
511 ASSERT_TRUE(called);
512 EXPECT_EQ(old_registration_id, new_registration_id);
514 ASSERT_EQ(2u, notifications_.size());
515 EXPECT_EQ(REGISTRATION_STORED, notifications_[0].type);
516 EXPECT_EQ(pattern, notifications_[0].pattern);
517 EXPECT_EQ(old_registration_id, notifications_[0].registration_id);
518 EXPECT_EQ(REGISTRATION_STORED, notifications_[1].type);
519 EXPECT_EQ(pattern, notifications_[1].pattern);
520 EXPECT_EQ(old_registration_id, notifications_[1].registration_id);
523 TEST_F(ServiceWorkerContextTest, DeleteAndStartOver) {
524 GURL pattern("http://www.example.com/");
525 GURL script_url("http://www.example.com/service_worker.js");
527 // Reinitialize the helper to test on-disk storage.
528 base::ScopedTempDir user_data_directory;
529 ASSERT_TRUE(user_data_directory.CreateUniqueTempDir());
530 helper_.reset(new EmbeddedWorkerTestHelper(user_data_directory.path(),
531 render_process_id_));
532 helper_->context_wrapper()->AddObserver(this);
534 int64 registration_id = kInvalidServiceWorkerRegistrationId;
535 bool called = false;
536 context()->RegisterServiceWorker(
537 pattern,
538 script_url,
539 NULL,
540 MakeRegisteredCallback(&called, &registration_id));
542 ASSERT_FALSE(called);
543 base::RunLoop().RunUntilIdle();
544 EXPECT_TRUE(called);
546 context()->storage()->FindRegistrationForId(
547 registration_id,
548 pattern.GetOrigin(),
549 base::Bind(&ExpectRegisteredWorkers,
550 SERVICE_WORKER_OK,
551 false /* expect_waiting */,
552 true /* expect_active */));
553 base::RunLoop().RunUntilIdle();
555 // Next handle ids should be 0 (the next call should return 1).
556 EXPECT_EQ(0, context()->GetNewServiceWorkerHandleId());
557 EXPECT_EQ(0, context()->GetNewRegistrationHandleId());
559 context()->ScheduleDeleteAndStartOver();
561 // The storage is disabled while the recovery process is running, so the
562 // operation should be failed.
563 context()->storage()->FindRegistrationForId(
564 registration_id,
565 pattern.GetOrigin(),
566 base::Bind(&ExpectRegisteredWorkers,
567 SERVICE_WORKER_ERROR_FAILED,
568 false /* expect_waiting */,
569 true /* expect_active */));
570 base::RunLoop().RunUntilIdle();
572 // The context started over and the storage was re-initialized, so the
573 // registration should not be found.
574 context()->storage()->FindRegistrationForId(
575 registration_id,
576 pattern.GetOrigin(),
577 base::Bind(&ExpectRegisteredWorkers,
578 SERVICE_WORKER_ERROR_NOT_FOUND,
579 false /* expect_waiting */,
580 true /* expect_active */));
581 base::RunLoop().RunUntilIdle();
583 called = false;
584 context()->RegisterServiceWorker(
585 pattern,
586 script_url,
587 NULL,
588 MakeRegisteredCallback(&called, &registration_id));
590 ASSERT_FALSE(called);
591 base::RunLoop().RunUntilIdle();
592 EXPECT_TRUE(called);
594 context()->storage()->FindRegistrationForId(
595 registration_id,
596 pattern.GetOrigin(),
597 base::Bind(&ExpectRegisteredWorkers,
598 SERVICE_WORKER_OK,
599 false /* expect_waiting */,
600 true /* expect_active */));
601 base::RunLoop().RunUntilIdle();
603 // The new context should take over next handle ids.
604 EXPECT_EQ(1, context()->GetNewServiceWorkerHandleId());
605 EXPECT_EQ(1, context()->GetNewRegistrationHandleId());
607 ASSERT_EQ(3u, notifications_.size());
608 EXPECT_EQ(REGISTRATION_STORED, notifications_[0].type);
609 EXPECT_EQ(pattern, notifications_[0].pattern);
610 EXPECT_EQ(registration_id, notifications_[0].registration_id);
611 EXPECT_EQ(STORAGE_RECOVERED, notifications_[1].type);
612 EXPECT_EQ(REGISTRATION_STORED, notifications_[2].type);
613 EXPECT_EQ(pattern, notifications_[2].pattern);
614 EXPECT_EQ(registration_id, notifications_[2].registration_id);
617 TEST_F(ServiceWorkerContextTest, ProviderHostIterator) {
618 const int kRenderProcessId1 = 1;
619 const int kRenderProcessId2 = 2;
620 const GURL kOrigin1 = GURL("http://www.example.com/");
621 const GURL kOrigin2 = GURL("https://www.example.com/");
622 int provider_id = 1;
624 // Host1 (provider_id=1): process_id=1, origin1.
625 ServiceWorkerProviderHost* host1(new ServiceWorkerProviderHost(
626 kRenderProcessId1, MSG_ROUTING_NONE, provider_id++,
627 SERVICE_WORKER_PROVIDER_FOR_CONTROLLEE, context()->AsWeakPtr(), nullptr));
628 host1->SetDocumentUrl(kOrigin1);
630 // Host2 (provider_id=2): process_id=2, origin2.
631 ServiceWorkerProviderHost* host2(new ServiceWorkerProviderHost(
632 kRenderProcessId2, MSG_ROUTING_NONE, provider_id++,
633 SERVICE_WORKER_PROVIDER_FOR_CONTROLLEE, context()->AsWeakPtr(), nullptr));
634 host2->SetDocumentUrl(kOrigin2);
636 // Host3 (provider_id=3): process_id=2, origin1.
637 ServiceWorkerProviderHost* host3(new ServiceWorkerProviderHost(
638 kRenderProcessId2, MSG_ROUTING_NONE, provider_id++,
639 SERVICE_WORKER_PROVIDER_FOR_CONTROLLEE, context()->AsWeakPtr(), nullptr));
640 host3->SetDocumentUrl(kOrigin1);
642 // Host4 (provider_id=4): process_id=2, origin2, for ServiceWorker.
643 ServiceWorkerProviderHost* host4(new ServiceWorkerProviderHost(
644 kRenderProcessId2, MSG_ROUTING_NONE, provider_id++,
645 SERVICE_WORKER_PROVIDER_FOR_CONTROLLER, context()->AsWeakPtr(), nullptr));
646 host4->SetDocumentUrl(kOrigin2);
648 context()->AddProviderHost(make_scoped_ptr(host1));
649 context()->AddProviderHost(make_scoped_ptr(host2));
650 context()->AddProviderHost(make_scoped_ptr(host3));
651 context()->AddProviderHost(make_scoped_ptr(host4));
653 // Iterate over all provider hosts.
654 std::set<ServiceWorkerProviderHost*> results;
655 for (auto it = context()->GetProviderHostIterator(); !it->IsAtEnd();
656 it->Advance()) {
657 results.insert(it->GetProviderHost());
659 EXPECT_EQ(4u, results.size());
660 EXPECT_TRUE(ContainsKey(results, host1));
661 EXPECT_TRUE(ContainsKey(results, host2));
662 EXPECT_TRUE(ContainsKey(results, host3));
663 EXPECT_TRUE(ContainsKey(results, host4));
665 // Iterate over the client provider hosts that belong to kOrigin1.
666 results.clear();
667 for (auto it = context()->GetClientProviderHostIterator(kOrigin1);
668 !it->IsAtEnd(); it->Advance()) {
669 results.insert(it->GetProviderHost());
671 EXPECT_EQ(2u, results.size());
672 EXPECT_TRUE(ContainsKey(results, host1));
673 EXPECT_TRUE(ContainsKey(results, host3));
675 // Iterate over the provider hosts that belong to kOrigin2.
676 // (This should not include host4 as it's not for controllee.)
677 results.clear();
678 for (auto it = context()->GetClientProviderHostIterator(kOrigin2);
679 !it->IsAtEnd(); it->Advance()) {
680 results.insert(it->GetProviderHost());
682 EXPECT_EQ(1u, results.size());
683 EXPECT_TRUE(ContainsKey(results, host2));
685 context()->RemoveProviderHost(kRenderProcessId1, 1);
686 context()->RemoveProviderHost(kRenderProcessId2, 2);
687 context()->RemoveProviderHost(kRenderProcessId2, 3);
688 context()->RemoveProviderHost(kRenderProcessId2, 4);
691 } // namespace content