OLD | NEW |
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 <stdint.h> | 5 #include <stdint.h> |
6 #include <tuple> | 6 #include <tuple> |
7 | 7 |
8 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
9 #include "base/logging.h" | 9 #include "base/logging.h" |
10 #include "base/macros.h" | 10 #include "base/macros.h" |
(...skipping 195 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
206 return job_coordinator()->job_timeout_timer_.IsRunning(); | 206 return job_coordinator()->job_timeout_timer_.IsRunning(); |
207 } | 207 } |
208 | 208 |
209 ServiceWorkerStorage* storage() const { return context()->storage(); } | 209 ServiceWorkerStorage* storage() const { return context()->storage(); } |
210 | 210 |
211 protected: | 211 protected: |
212 scoped_refptr<ServiceWorkerRegistration> RunRegisterJob( | 212 scoped_refptr<ServiceWorkerRegistration> RunRegisterJob( |
213 const GURL& pattern, | 213 const GURL& pattern, |
214 const GURL& script_url, | 214 const GURL& script_url, |
215 ServiceWorkerStatusCode expected_status = SERVICE_WORKER_OK); | 215 ServiceWorkerStatusCode expected_status = SERVICE_WORKER_OK); |
| 216 scoped_refptr<ServiceWorkerRegistration> RunRegisterJob( |
| 217 const GURL& script_url, |
| 218 const ServiceWorkerRegistrationOptions& options, |
| 219 ServiceWorkerStatusCode expected_status = SERVICE_WORKER_OK); |
216 void RunUnregisterJob( | 220 void RunUnregisterJob( |
217 const GURL& pattern, | 221 const GURL& pattern, |
218 ServiceWorkerStatusCode expected_status = SERVICE_WORKER_OK); | 222 ServiceWorkerStatusCode expected_status = SERVICE_WORKER_OK); |
219 scoped_refptr<ServiceWorkerRegistration> FindRegistrationForPattern( | 223 scoped_refptr<ServiceWorkerRegistration> FindRegistrationForPattern( |
220 const GURL& pattern, | 224 const GURL& pattern, |
221 ServiceWorkerStatusCode expected_status = SERVICE_WORKER_OK); | 225 ServiceWorkerStatusCode expected_status = SERVICE_WORKER_OK); |
222 std::unique_ptr<ServiceWorkerProviderHost> CreateControllee(); | 226 std::unique_ptr<ServiceWorkerProviderHost> CreateControllee(); |
223 void TimeOutFirstJob(); | 227 void TimeOutFirstJob(); |
224 | 228 |
225 TestBrowserThreadBundle browser_thread_bundle_; | 229 TestBrowserThreadBundle browser_thread_bundle_; |
226 std::unique_ptr<EmbeddedWorkerTestHelper> helper_; | 230 std::unique_ptr<EmbeddedWorkerTestHelper> helper_; |
227 std::vector<ServiceWorkerRemoteProviderEndpoint> remote_endpoints_; | 231 std::vector<ServiceWorkerRemoteProviderEndpoint> remote_endpoints_; |
228 }; | 232 }; |
229 | 233 |
230 scoped_refptr<ServiceWorkerRegistration> ServiceWorkerJobTest::RunRegisterJob( | 234 scoped_refptr<ServiceWorkerRegistration> ServiceWorkerJobTest::RunRegisterJob( |
231 const GURL& pattern, | |
232 const GURL& script_url, | 235 const GURL& script_url, |
| 236 const ServiceWorkerRegistrationOptions& options, |
233 ServiceWorkerStatusCode expected_status) { | 237 ServiceWorkerStatusCode expected_status) { |
234 scoped_refptr<ServiceWorkerRegistration> registration; | 238 scoped_refptr<ServiceWorkerRegistration> registration; |
235 bool called; | 239 bool called; |
236 job_coordinator()->Register( | 240 job_coordinator()->Register( |
237 script_url, ServiceWorkerRegistrationOptions(pattern), nullptr, | 241 script_url, options, nullptr, |
238 SaveRegistration(expected_status, &called, ®istration)); | 242 SaveRegistration(expected_status, &called, ®istration)); |
239 EXPECT_TRUE(is_job_timer_running()); | 243 EXPECT_TRUE(is_job_timer_running()); |
240 EXPECT_FALSE(called); | 244 EXPECT_FALSE(called); |
241 base::RunLoop().RunUntilIdle(); | 245 base::RunLoop().RunUntilIdle(); |
242 EXPECT_TRUE(called); | 246 EXPECT_TRUE(called); |
243 EXPECT_FALSE(is_job_timer_running()); | 247 EXPECT_FALSE(is_job_timer_running()); |
244 return registration; | 248 return registration; |
245 } | 249 } |
246 | 250 |
| 251 scoped_refptr<ServiceWorkerRegistration> ServiceWorkerJobTest::RunRegisterJob( |
| 252 const GURL& pattern, |
| 253 const GURL& script_url, |
| 254 ServiceWorkerStatusCode expected_status) { |
| 255 return RunRegisterJob(script_url, ServiceWorkerRegistrationOptions(pattern), |
| 256 expected_status); |
| 257 } |
| 258 |
247 void ServiceWorkerJobTest::RunUnregisterJob( | 259 void ServiceWorkerJobTest::RunUnregisterJob( |
248 const GURL& pattern, | 260 const GURL& pattern, |
249 ServiceWorkerStatusCode expected_status) { | 261 ServiceWorkerStatusCode expected_status) { |
250 bool called; | 262 bool called; |
251 job_coordinator()->Unregister(pattern, | 263 job_coordinator()->Unregister(pattern, |
252 SaveUnregistration(expected_status, &called)); | 264 SaveUnregistration(expected_status, &called)); |
253 EXPECT_FALSE(called); | 265 EXPECT_FALSE(called); |
254 base::RunLoop().RunUntilIdle(); | 266 base::RunLoop().RunUntilIdle(); |
255 EXPECT_TRUE(called); | 267 EXPECT_TRUE(called); |
256 } | 268 } |
(...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
554 ASSERT_EQ(old_registration, new_registration); | 566 ASSERT_EQ(old_registration, new_registration); |
555 | 567 |
556 ASSERT_FALSE(old_registration->HasOneRef()); | 568 ASSERT_FALSE(old_registration->HasOneRef()); |
557 | 569 |
558 scoped_refptr<ServiceWorkerRegistration> new_registration_by_pattern = | 570 scoped_refptr<ServiceWorkerRegistration> new_registration_by_pattern = |
559 FindRegistrationForPattern(pattern); | 571 FindRegistrationForPattern(pattern); |
560 | 572 |
561 ASSERT_EQ(new_registration, old_registration); | 573 ASSERT_EQ(new_registration, old_registration); |
562 } | 574 } |
563 | 575 |
| 576 // Make sure that when registering a duplicate pattern+script_url with a |
| 577 // different update_via_cache value, that the value is updated. |
| 578 TEST_F(ServiceWorkerJobTest, RegisterWithDifferentUpdateViaCache) { |
| 579 // During registration, handles will be created for hosting the worker's |
| 580 // context. KeepHandlesDispatcherHost will store the handles. |
| 581 scoped_refptr<KeepHandlesDispatcherHost> dispatcher_host = |
| 582 new KeepHandlesDispatcherHost( |
| 583 helper_->mock_render_process_id(), |
| 584 helper_->browser_context()->GetResourceContext()); |
| 585 helper_->RegisterDispatcherHost(helper_->mock_render_process_id(), |
| 586 dispatcher_host); |
| 587 dispatcher_host->Init(helper_->context_wrapper()); |
| 588 |
| 589 GURL pattern("http://www.example.com/"); |
| 590 GURL script_url("http://www.example.com/service_worker.js"); |
| 591 ServiceWorkerRegistrationOptions options(pattern); |
| 592 |
| 593 scoped_refptr<ServiceWorkerRegistration> old_registration = |
| 594 RunRegisterJob(script_url, options); |
| 595 |
| 596 ASSERT_EQ(blink::WebServiceWorkerUpdateViaCache::kImports, |
| 597 old_registration->update_via_cache()); |
| 598 |
| 599 // Ensure that the registration's handle doesn't have the reference. |
| 600 EXPECT_EQ(1UL, dispatcher_host->registration_handles().size()); |
| 601 dispatcher_host->RemoveHandles(); |
| 602 EXPECT_EQ(0UL, dispatcher_host->registration_handles().size()); |
| 603 ASSERT_TRUE(old_registration->HasOneRef()); |
| 604 |
| 605 scoped_refptr<ServiceWorkerRegistration> old_registration_by_pattern = |
| 606 FindRegistrationForPattern(pattern); |
| 607 |
| 608 ASSERT_TRUE(old_registration_by_pattern.get()); |
| 609 |
| 610 options.update_via_cache = blink::WebServiceWorkerUpdateViaCache::kNone; |
| 611 scoped_refptr<ServiceWorkerRegistration> new_registration = |
| 612 RunRegisterJob(script_url, options); |
| 613 |
| 614 ASSERT_EQ(old_registration, new_registration); |
| 615 ASSERT_EQ(blink::WebServiceWorkerUpdateViaCache::kNone, |
| 616 new_registration->update_via_cache()); |
| 617 |
| 618 ASSERT_FALSE(old_registration->HasOneRef()); |
| 619 |
| 620 scoped_refptr<ServiceWorkerRegistration> new_registration_by_pattern = |
| 621 FindRegistrationForPattern(pattern); |
| 622 |
| 623 ASSERT_EQ(new_registration, old_registration); |
| 624 } |
| 625 |
564 class FailToStartWorkerTestHelper : public EmbeddedWorkerTestHelper { | 626 class FailToStartWorkerTestHelper : public EmbeddedWorkerTestHelper { |
565 public: | 627 public: |
566 FailToStartWorkerTestHelper() : EmbeddedWorkerTestHelper(base::FilePath()) {} | 628 FailToStartWorkerTestHelper() : EmbeddedWorkerTestHelper(base::FilePath()) {} |
567 | 629 |
568 void OnStartWorker( | 630 void OnStartWorker( |
569 int embedded_worker_id, | 631 int embedded_worker_id, |
570 int64_t service_worker_version_id, | 632 int64_t service_worker_version_id, |
571 const GURL& scope, | 633 const GURL& scope, |
572 const GURL& script_url, | 634 const GURL& script_url, |
573 bool pause_after_download, | 635 bool pause_after_download, |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
625 // Register conflicting scripts for the same pattern. The most recent | 687 // Register conflicting scripts for the same pattern. The most recent |
626 // registration should win, and the old registration should have been | 688 // registration should win, and the old registration should have been |
627 // shutdown. | 689 // shutdown. |
628 TEST_F(ServiceWorkerJobTest, ParallelRegNewScript) { | 690 TEST_F(ServiceWorkerJobTest, ParallelRegNewScript) { |
629 GURL pattern("http://www.example.com/"); | 691 GURL pattern("http://www.example.com/"); |
630 | 692 |
631 GURL script_url1("http://www.example.com/service_worker1.js"); | 693 GURL script_url1("http://www.example.com/service_worker1.js"); |
632 bool registration1_called = false; | 694 bool registration1_called = false; |
633 scoped_refptr<ServiceWorkerRegistration> registration1; | 695 scoped_refptr<ServiceWorkerRegistration> registration1; |
634 job_coordinator()->Register( | 696 job_coordinator()->Register( |
635 script_url1, ServiceWorkerRegistrationOptions(pattern), nullptr, | 697 script_url1, |
| 698 ServiceWorkerRegistrationOptions( |
| 699 pattern, blink::WebServiceWorkerUpdateViaCache::kNone), |
| 700 nullptr, |
636 SaveRegistration(SERVICE_WORKER_OK, ®istration1_called, | 701 SaveRegistration(SERVICE_WORKER_OK, ®istration1_called, |
637 ®istration1)); | 702 ®istration1)); |
638 | 703 |
639 GURL script_url2("http://www.example.com/service_worker2.js"); | 704 GURL script_url2("http://www.example.com/service_worker2.js"); |
640 bool registration2_called = false; | 705 bool registration2_called = false; |
641 scoped_refptr<ServiceWorkerRegistration> registration2; | 706 scoped_refptr<ServiceWorkerRegistration> registration2; |
642 job_coordinator()->Register( | 707 job_coordinator()->Register( |
643 script_url2, ServiceWorkerRegistrationOptions(pattern), nullptr, | 708 script_url2, |
| 709 ServiceWorkerRegistrationOptions( |
| 710 pattern, blink::WebServiceWorkerUpdateViaCache::kAll), |
| 711 nullptr, |
644 SaveRegistration(SERVICE_WORKER_OK, ®istration2_called, | 712 SaveRegistration(SERVICE_WORKER_OK, ®istration2_called, |
645 ®istration2)); | 713 ®istration2)); |
646 | 714 |
647 ASSERT_FALSE(registration1_called); | 715 ASSERT_FALSE(registration1_called); |
648 ASSERT_FALSE(registration2_called); | 716 ASSERT_FALSE(registration2_called); |
649 base::RunLoop().RunUntilIdle(); | 717 base::RunLoop().RunUntilIdle(); |
650 ASSERT_TRUE(registration1_called); | 718 ASSERT_TRUE(registration1_called); |
651 ASSERT_TRUE(registration2_called); | 719 ASSERT_TRUE(registration2_called); |
652 | 720 |
653 scoped_refptr<ServiceWorkerRegistration> registration = | 721 scoped_refptr<ServiceWorkerRegistration> registration = |
(...skipping 1273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1927 // should not be promoted to ACTIVATED because failure occur | 1995 // should not be promoted to ACTIVATED because failure occur |
1928 // during shutdown. | 1996 // during shutdown. |
1929 runner->RunPendingTasks(); | 1997 runner->RunPendingTasks(); |
1930 base::RunLoop().RunUntilIdle(); | 1998 base::RunLoop().RunUntilIdle(); |
1931 EXPECT_EQ(new_version.get(), registration->active_version()); | 1999 EXPECT_EQ(new_version.get(), registration->active_version()); |
1932 EXPECT_EQ(ServiceWorkerVersion::ACTIVATING, new_version->status()); | 2000 EXPECT_EQ(ServiceWorkerVersion::ACTIVATING, new_version->status()); |
1933 registration->RemoveListener(update_helper); | 2001 registration->RemoveListener(update_helper); |
1934 } | 2002 } |
1935 | 2003 |
1936 } // namespace content | 2004 } // namespace content |
OLD | NEW |