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 "base/bind.h" | 5 #include "base/bind.h" |
6 #include "base/callback.h" | 6 #include "base/callback.h" |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/run_loop.h" | 8 #include "base/run_loop.h" |
9 #include "base/strings/utf_string_conversions.h" | 9 #include "base/strings/utf_string_conversions.h" |
10 #include "content/browser/fileapi/chrome_blob_storage_context.h" | 10 #include "content/browser/fileapi/chrome_blob_storage_context.h" |
(...skipping 554 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
565 | 565 |
566 AssociateRendererProcessToPattern(pattern); | 566 AssociateRendererProcessToPattern(pattern); |
567 } | 567 } |
568 | 568 |
569 void TimeoutWorkerOnIOThread() { | 569 void TimeoutWorkerOnIOThread() { |
570 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 570 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
571 version_->PingWorker(); | 571 version_->PingWorker(); |
572 version_->OnPingTimeout(); | 572 version_->OnPingTimeout(); |
573 } | 573 } |
574 | 574 |
575 void AddControlleeOnIOThread() { | |
576 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
577 scoped_ptr<ServiceWorkerProviderHost> host(new ServiceWorkerProviderHost( | |
578 33 /* dummy render process id */, | |
579 MSG_ROUTING_NONE /* render_frame_id */, 1 /* dummy provider_id */, | |
580 SERVICE_WORKER_PROVIDER_FOR_WINDOW, wrapper()->context()->AsWeakPtr(), | |
581 NULL)); | |
582 host->SetDocumentUrl( | |
583 embedded_test_server()->GetURL("/service_worker/host")); | |
584 host->AssociateRegistration(registration_.get(), | |
585 false /* notify_controllerchange */); | |
586 wrapper()->context()->AddProviderHost(host.Pass()); | |
587 } | |
588 | |
589 void AddWaitingWorkerOnIOThread(const std::string& worker_url) { | |
590 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
591 scoped_refptr<ServiceWorkerVersion> waiting_version( | |
592 new ServiceWorkerVersion( | |
593 registration_.get(), embedded_test_server()->GetURL(worker_url), | |
594 wrapper()->context()->storage()->NewVersionId(), | |
595 wrapper()->context()->AsWeakPtr())); | |
596 waiting_version->SetStatus(ServiceWorkerVersion::INSTALLED); | |
597 registration_->SetWaitingVersion(waiting_version.get()); | |
598 registration_->ActivateWaitingVersionWhenReady(); | |
599 } | |
600 | |
601 void StartWorker(ServiceWorkerStatusCode expected_status) { | 575 void StartWorker(ServiceWorkerStatusCode expected_status) { |
602 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 576 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
603 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED; | 577 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED; |
604 base::RunLoop start_run_loop; | 578 base::RunLoop start_run_loop; |
605 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | 579 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
606 base::Bind(&self::StartOnIOThread, this, | 580 base::Bind(&self::StartOnIOThread, this, |
607 start_run_loop.QuitClosure(), | 581 start_run_loop.QuitClosure(), |
608 &status)); | 582 &status)); |
609 start_run_loop.Run(); | 583 start_run_loop.Run(); |
610 ASSERT_EQ(expected_status, status); | 584 ASSERT_EQ(expected_status, status); |
(...skipping 22 matching lines...) Expand all Loading... |
633 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 607 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
634 version_->SetStatus(ServiceWorkerVersion::INSTALLING); | 608 version_->SetStatus(ServiceWorkerVersion::INSTALLING); |
635 version_->DispatchInstallEvent( | 609 version_->DispatchInstallEvent( |
636 CreateReceiver(BrowserThread::UI, done, result)); | 610 CreateReceiver(BrowserThread::UI, done, result)); |
637 } | 611 } |
638 | 612 |
639 void ActivateOnIOThread(const base::Closure& done, | 613 void ActivateOnIOThread(const base::Closure& done, |
640 ServiceWorkerStatusCode* result) { | 614 ServiceWorkerStatusCode* result) { |
641 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 615 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
642 version_->SetStatus(ServiceWorkerVersion::ACTIVATING); | 616 version_->SetStatus(ServiceWorkerVersion::ACTIVATING); |
643 registration_->SetActiveVersion(version_.get()); | |
644 version_->DispatchActivateEvent( | 617 version_->DispatchActivateEvent( |
645 CreateReceiver(BrowserThread::UI, done, result)); | 618 CreateReceiver(BrowserThread::UI, done, result)); |
646 } | 619 } |
647 | 620 |
648 void FetchOnIOThread(const base::Closure& done, | 621 void FetchOnIOThread(const base::Closure& done, |
649 bool* prepare_result, | 622 bool* prepare_result, |
650 FetchResult* result) { | 623 FetchResult* result) { |
651 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 624 ASSERT_TRUE(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
652 ServiceWorkerFetchRequest request( | 625 ServiceWorkerFetchRequest request( |
653 embedded_test_server()->GetURL("/service_worker/empty.html"), | 626 embedded_test_server()->GetURL("/service_worker/empty.html"), |
(...skipping 96 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
750 } | 723 } |
751 | 724 |
752 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, StartNotFound) { | 725 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, StartNotFound) { |
753 RunOnIOThread(base::Bind(&self::SetUpRegistrationOnIOThread, this, | 726 RunOnIOThread(base::Bind(&self::SetUpRegistrationOnIOThread, this, |
754 "/service_worker/nonexistent.js")); | 727 "/service_worker/nonexistent.js")); |
755 | 728 |
756 // Start a worker for nonexistent URL. | 729 // Start a worker for nonexistent URL. |
757 StartWorker(SERVICE_WORKER_ERROR_NETWORK); | 730 StartWorker(SERVICE_WORKER_ERROR_NETWORK); |
758 } | 731 } |
759 | 732 |
760 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, ReadResourceFailure) { | |
761 RunOnIOThread(base::Bind(&self::SetUpRegistrationOnIOThread, this, | |
762 "/service_worker/worker.js")); | |
763 version_->SetStatus(ServiceWorkerVersion::ACTIVATED); | |
764 | |
765 // Add a non-existent resource to the version. | |
766 std::vector<ServiceWorkerDatabase::ResourceRecord> records; | |
767 records.push_back( | |
768 ServiceWorkerDatabase::ResourceRecord(30, version_->script_url(), 100)); | |
769 version_->script_cache_map()->SetResources(records); | |
770 | |
771 // We'll fail to read from disk and the worker should be doomed. | |
772 StartWorker(SERVICE_WORKER_ERROR_DISK_CACHE); | |
773 EXPECT_EQ(ServiceWorkerVersion::REDUNDANT, version_->status()); | |
774 | |
775 // The registration should be deleted since the version was the stored one. | |
776 EXPECT_TRUE(registration_->is_deleted()); | |
777 } | |
778 | |
779 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, | |
780 ReadResourceFailure_WaitingWorker) { | |
781 RunOnIOThread(base::Bind(&self::SetUpRegistrationOnIOThread, this, | |
782 "/service_worker/worker.js")); | |
783 base::RunLoop acrivate_run_loop; | |
784 ServiceWorkerStatusCode status = SERVICE_WORKER_ERROR_FAILED; | |
785 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | |
786 base::Bind(&self::ActivateOnIOThread, this, | |
787 acrivate_run_loop.QuitClosure(), &status)); | |
788 acrivate_run_loop.Run(); | |
789 ASSERT_TRUE(registration_->active_version()); | |
790 | |
791 // Give the version a controllee. | |
792 RunOnIOThread(base::Bind(&self::AddControlleeOnIOThread, this)); | |
793 | |
794 // Add a non-existent resource to the version. | |
795 std::vector<ServiceWorkerDatabase::ResourceRecord> records; | |
796 records.push_back( | |
797 ServiceWorkerDatabase::ResourceRecord(30, version_->script_url(), 100)); | |
798 version_->script_cache_map()->SetResources(records); | |
799 | |
800 // Make a waiting version. | |
801 RunOnIOThread(base::Bind(&self::AddWaitingWorkerOnIOThread, this, | |
802 "/service_worker/worker.js")); | |
803 | |
804 // Start the worker. We'll fail to read from disk and the worker should be | |
805 // doomed. | |
806 StopWorker(SERVICE_WORKER_OK); // in case it's already running | |
807 StartWorker(SERVICE_WORKER_ERROR_DISK_CACHE); | |
808 EXPECT_EQ(ServiceWorkerVersion::REDUNDANT, version_->status()); | |
809 | |
810 // The registration is still alive since the waiting version was the stored | |
811 // one. | |
812 EXPECT_FALSE(registration_->is_deleted()); | |
813 } | |
814 | |
815 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, Install) { | 733 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, Install) { |
816 InstallTestHelper("/service_worker/worker.js", SERVICE_WORKER_OK); | 734 InstallTestHelper("/service_worker/worker.js", SERVICE_WORKER_OK); |
817 } | 735 } |
818 | 736 |
819 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, | 737 IN_PROC_BROWSER_TEST_F(ServiceWorkerVersionBrowserTest, |
820 InstallWithWaitUntil_Fulfilled) { | 738 InstallWithWaitUntil_Fulfilled) { |
821 InstallTestHelper("/service_worker/worker_install_fulfilled.js", | 739 InstallTestHelper("/service_worker/worker_install_fulfilled.js", |
822 SERVICE_WORKER_OK); | 740 SERVICE_WORKER_OK); |
823 } | 741 } |
824 | 742 |
(...skipping 543 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1368 ASSERT_EQ(SERVICE_WORKER_OK, status); | 1286 ASSERT_EQ(SERVICE_WORKER_OK, status); |
1369 // Stop the worker. | 1287 // Stop the worker. |
1370 StopWorker(SERVICE_WORKER_OK); | 1288 StopWorker(SERVICE_WORKER_OK); |
1371 // Restart the worker. | 1289 // Restart the worker. |
1372 StartWorker(SERVICE_WORKER_OK); | 1290 StartWorker(SERVICE_WORKER_OK); |
1373 // Stop the worker. | 1291 // Stop the worker. |
1374 StopWorker(SERVICE_WORKER_OK); | 1292 StopWorker(SERVICE_WORKER_OK); |
1375 } | 1293 } |
1376 | 1294 |
1377 } // namespace content | 1295 } // namespace content |
OLD | NEW |