| 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 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 switch (result) { | 111 switch (result) { |
| 112 case blink::WebServiceWorkerEventResultCompleted: | 112 case blink::WebServiceWorkerEventResultCompleted: |
| 113 return SERVICE_WORKER_OK; | 113 return SERVICE_WORKER_OK; |
| 114 case blink::WebServiceWorkerEventResultRejected: | 114 case blink::WebServiceWorkerEventResultRejected: |
| 115 return SERVICE_WORKER_ERROR_EVENT_WAITUNTIL_REJECTED; | 115 return SERVICE_WORKER_ERROR_EVENT_WAITUNTIL_REJECTED; |
| 116 } | 116 } |
| 117 NOTREACHED() << "Got invalid result: " << result; | 117 NOTREACHED() << "Got invalid result: " << result; |
| 118 return SERVICE_WORKER_ERROR_FAILED; | 118 return SERVICE_WORKER_ERROR_FAILED; |
| 119 } | 119 } |
| 120 | 120 |
| 121 void NestedLoopRunUntilIdle() { |
| 122 base::MessageLoop::ScopedNestableTaskAllower nestable_allower( |
| 123 base::MessageLoop::current()); |
| 124 base::RunLoop().RunUntilIdle(); |
| 125 } |
| 126 |
| 121 } // namespace | 127 } // namespace |
| 122 | 128 |
| 123 class ServiceWorkerJobTest : public testing::Test { | 129 class ServiceWorkerJobTest : public testing::Test { |
| 124 public: | 130 public: |
| 125 ServiceWorkerJobTest() | 131 ServiceWorkerJobTest() |
| 126 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} | 132 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP) {} |
| 127 | 133 |
| 128 void SetUp() override { | 134 void SetUp() override { |
| 129 helper_.reset(new EmbeddedWorkerTestHelper(base::FilePath())); | 135 helper_.reset(new EmbeddedWorkerTestHelper(base::FilePath())); |
| 130 } | 136 } |
| (...skipping 241 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 372 scoped_refptr<ServiceWorkerRegistration> new_registration_by_pattern = | 378 scoped_refptr<ServiceWorkerRegistration> new_registration_by_pattern = |
| 373 FindRegistrationForPattern(pattern); | 379 FindRegistrationForPattern(pattern); |
| 374 | 380 |
| 375 ASSERT_EQ(new_registration, old_registration); | 381 ASSERT_EQ(new_registration, old_registration); |
| 376 } | 382 } |
| 377 | 383 |
| 378 class FailToStartWorkerTestHelper : public EmbeddedWorkerTestHelper { | 384 class FailToStartWorkerTestHelper : public EmbeddedWorkerTestHelper { |
| 379 public: | 385 public: |
| 380 FailToStartWorkerTestHelper() : EmbeddedWorkerTestHelper(base::FilePath()) {} | 386 FailToStartWorkerTestHelper() : EmbeddedWorkerTestHelper(base::FilePath()) {} |
| 381 | 387 |
| 382 void OnStartWorker( | 388 void OnStartWorker(int embedded_worker_id, |
| 383 int embedded_worker_id, | 389 int64_t service_worker_version_id, |
| 384 int64_t service_worker_version_id, | 390 const GURL& scope, |
| 385 const GURL& scope, | 391 const GURL& script_url, |
| 386 const GURL& script_url, | 392 bool pause_after_download, |
| 387 bool pause_after_download, | 393 mojom::ServiceWorkerEventDispatcherRequest request, |
| 388 mojom::ServiceWorkerEventDispatcherRequest request) override { | 394 mojom::EmbeddedWorkerInstanceHostAssociatedPtrInfo |
| 389 EmbeddedWorkerInstance* worker = registry()->GetWorker(embedded_worker_id); | 395 instance_host) override { |
| 390 registry()->OnWorkerStopped(worker->process_id(), embedded_worker_id); | 396 mojom::EmbeddedWorkerInstanceHostAssociatedPtr instance_host_ptr; |
| 397 instance_host_ptr.Bind(std::move(instance_host)); |
| 398 instance_host_ptr->OnStopped(); |
| 399 NestedLoopRunUntilIdle(); |
| 391 } | 400 } |
| 392 }; | 401 }; |
| 393 | 402 |
| 394 TEST_F(ServiceWorkerJobTest, Register_FailToStartWorker) { | 403 TEST_F(ServiceWorkerJobTest, Register_FailToStartWorker) { |
| 395 helper_.reset(new FailToStartWorkerTestHelper); | 404 helper_.reset(new FailToStartWorkerTestHelper); |
| 396 | 405 |
| 397 scoped_refptr<ServiceWorkerRegistration> registration = | 406 scoped_refptr<ServiceWorkerRegistration> registration = |
| 398 RunRegisterJob(GURL("http://www.example.com/"), | 407 RunRegisterJob(GURL("http://www.example.com/"), |
| 399 GURL("http://www.example.com/service_worker.js"), | 408 GURL("http://www.example.com/service_worker.js"), |
| 400 SERVICE_WORKER_ERROR_START_WORKER_FAILED); | 409 SERVICE_WORKER_ERROR_START_WORKER_FAILED); |
| (...skipping 432 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 833 EXPECT_TRUE(called); | 842 EXPECT_TRUE(called); |
| 834 EXPECT_TRUE(registration.get()); | 843 EXPECT_TRUE(registration.get()); |
| 835 EXPECT_TRUE(registration->active_version()); | 844 EXPECT_TRUE(registration->active_version()); |
| 836 EXPECT_FALSE(registration->installing_version()); | 845 EXPECT_FALSE(registration->installing_version()); |
| 837 EXPECT_FALSE(registration->waiting_version()); | 846 EXPECT_FALSE(registration->waiting_version()); |
| 838 registration_ = registration; | 847 registration_ = registration; |
| 839 return registration; | 848 return registration; |
| 840 } | 849 } |
| 841 | 850 |
| 842 // EmbeddedWorkerTestHelper overrides | 851 // EmbeddedWorkerTestHelper overrides |
| 843 void OnStartWorker( | 852 void OnStartWorker(int embedded_worker_id, |
| 844 int embedded_worker_id, | 853 int64_t version_id, |
| 845 int64_t version_id, | 854 const GURL& scope, |
| 846 const GURL& scope, | 855 const GURL& script, |
| 847 const GURL& script, | 856 bool pause_after_download, |
| 848 bool pause_after_download, | 857 mojom::ServiceWorkerEventDispatcherRequest request, |
| 849 mojom::ServiceWorkerEventDispatcherRequest request) override { | 858 mojom::EmbeddedWorkerInstanceHostAssociatedPtrInfo |
| 859 instance_host) override { |
| 850 const std::string kMockScriptBody = "mock_script"; | 860 const std::string kMockScriptBody = "mock_script"; |
| 851 const uint64_t kMockScriptSize = 19284; | 861 const uint64_t kMockScriptSize = 19284; |
| 852 ServiceWorkerVersion* version = context()->GetLiveVersion(version_id); | 862 ServiceWorkerVersion* version = context()->GetLiveVersion(version_id); |
| 853 ServiceWorkerRegistration* registration = | 863 ServiceWorkerRegistration* registration = |
| 854 context()->GetLiveRegistration(version->registration_id()); | 864 context()->GetLiveRegistration(version->registration_id()); |
| 855 bool is_update = registration->active_version() && | 865 bool is_update = registration->active_version() && |
| 856 version != registration->active_version(); | 866 version != registration->active_version(); |
| 857 | 867 |
| 858 ASSERT_TRUE(version); | 868 ASSERT_TRUE(version); |
| 859 version->AddListener(this); | 869 version->AddListener(this); |
| (...skipping 17 matching lines...) Expand all Loading... |
| 877 version->SetMainScriptHttpResponseInfo( | 887 version->SetMainScriptHttpResponseInfo( |
| 878 EmbeddedWorkerTestHelper::CreateHttpResponseInfo()); | 888 EmbeddedWorkerTestHelper::CreateHttpResponseInfo()); |
| 879 } else { | 889 } else { |
| 880 if (script.GetOrigin() == kNoChangeOrigin) { | 890 if (script.GetOrigin() == kNoChangeOrigin) { |
| 881 // Simulate fetching the updated script and finding it's identical to | 891 // Simulate fetching the updated script and finding it's identical to |
| 882 // the incumbent. | 892 // the incumbent. |
| 883 version->script_cache_map()->NotifyFinishedCaching( | 893 version->script_cache_map()->NotifyFinishedCaching( |
| 884 script, kMockScriptSize, net::ERR_FILE_EXISTS, std::string()); | 894 script, kMockScriptSize, net::ERR_FILE_EXISTS, std::string()); |
| 885 version->SetMainScriptHttpResponseInfo( | 895 version->SetMainScriptHttpResponseInfo( |
| 886 EmbeddedWorkerTestHelper::CreateHttpResponseInfo()); | 896 EmbeddedWorkerTestHelper::CreateHttpResponseInfo()); |
| 887 SimulateWorkerScriptLoaded(embedded_worker_id); | 897 |
| 898 mojom::EmbeddedWorkerInstanceHostAssociatedPtr instance_host_ptr; |
| 899 instance_host_ptr.Bind(std::move(instance_host)); |
| 900 instance_host_ptr->OnScriptLoaded(); |
| 901 NestedLoopRunUntilIdle(); |
| 888 return; | 902 return; |
| 889 } | 903 } |
| 890 | 904 |
| 891 // Spoof caching the script for the new version. | 905 // Spoof caching the script for the new version. |
| 892 WriteStringResponse(storage(), resource_id, "mock_different_script"); | 906 WriteStringResponse(storage(), resource_id, "mock_different_script"); |
| 893 version->script_cache_map()->NotifyFinishedCaching( | 907 version->script_cache_map()->NotifyFinishedCaching( |
| 894 script, kMockScriptSize, net::OK, std::string()); | 908 script, kMockScriptSize, net::OK, std::string()); |
| 895 version->SetMainScriptHttpResponseInfo( | 909 version->SetMainScriptHttpResponseInfo( |
| 896 EmbeddedWorkerTestHelper::CreateHttpResponseInfo()); | 910 EmbeddedWorkerTestHelper::CreateHttpResponseInfo()); |
| 897 } | 911 } |
| 898 | 912 |
| 899 EmbeddedWorkerTestHelper::OnStartWorker(embedded_worker_id, version_id, | 913 EmbeddedWorkerTestHelper::OnStartWorker( |
| 900 scope, script, pause_after_download, | 914 embedded_worker_id, version_id, scope, script, pause_after_download, |
| 901 std::move(request)); | 915 std::move(request), std::move(instance_host)); |
| 902 } | 916 } |
| 903 | 917 |
| 904 void OnResumeAfterDownload(int embedded_worker_id) override { | 918 void OnResumeAfterDownload(int embedded_worker_id) override { |
| 905 if (!force_start_worker_failure_) { | 919 if (!force_start_worker_failure_) { |
| 906 EmbeddedWorkerTestHelper::OnResumeAfterDownload(embedded_worker_id); | 920 EmbeddedWorkerTestHelper::OnResumeAfterDownload(embedded_worker_id); |
| 907 } else { | 921 } else { |
| 908 SimulateWorkerThreadStarted(GetNextThreadId(), embedded_worker_id); | 922 SimulateWorkerThreadStarted(GetNextThreadId(), embedded_worker_id, |
| 923 GetNextProviderId()); |
| 909 SimulateWorkerScriptEvaluated(embedded_worker_id, false); | 924 SimulateWorkerScriptEvaluated(embedded_worker_id, false); |
| 910 } | 925 } |
| 911 } | 926 } |
| 912 | 927 |
| 913 // ServiceWorkerRegistration::Listener overrides | 928 // ServiceWorkerRegistration::Listener overrides |
| 914 void OnVersionAttributesChanged( | 929 void OnVersionAttributesChanged( |
| 915 ServiceWorkerRegistration* registration, | 930 ServiceWorkerRegistration* registration, |
| 916 ChangedVersionAttributesMask changed_mask, | 931 ChangedVersionAttributesMask changed_mask, |
| 917 const ServiceWorkerRegistrationInfo& info) override { | 932 const ServiceWorkerRegistrationInfo& info) override { |
| 918 AttributeChangeLogEntry entry; | 933 AttributeChangeLogEntry entry; |
| (...skipping 27 matching lines...) Expand all Loading... |
| 946 bool force_start_worker_failure_ = false; | 961 bool force_start_worker_failure_ = false; |
| 947 }; | 962 }; |
| 948 | 963 |
| 949 // Helper class for update tests that evicts the active version when the update | 964 // Helper class for update tests that evicts the active version when the update |
| 950 // worker is about to be started. | 965 // worker is about to be started. |
| 951 class EvictIncumbentVersionHelper : public UpdateJobTestHelper { | 966 class EvictIncumbentVersionHelper : public UpdateJobTestHelper { |
| 952 public: | 967 public: |
| 953 EvictIncumbentVersionHelper() {} | 968 EvictIncumbentVersionHelper() {} |
| 954 ~EvictIncumbentVersionHelper() override {} | 969 ~EvictIncumbentVersionHelper() override {} |
| 955 | 970 |
| 956 void OnStartWorker( | 971 void OnStartWorker(int embedded_worker_id, |
| 957 int embedded_worker_id, | 972 int64_t version_id, |
| 958 int64_t version_id, | 973 const GURL& scope, |
| 959 const GURL& scope, | 974 const GURL& script, |
| 960 const GURL& script, | 975 bool pause_after_download, |
| 961 bool pause_after_download, | 976 mojom::ServiceWorkerEventDispatcherRequest request, |
| 962 mojom::ServiceWorkerEventDispatcherRequest request) override { | 977 mojom::EmbeddedWorkerInstanceHostAssociatedPtrInfo |
| 978 instance_host) override { |
| 963 ServiceWorkerVersion* version = context()->GetLiveVersion(version_id); | 979 ServiceWorkerVersion* version = context()->GetLiveVersion(version_id); |
| 964 ServiceWorkerRegistration* registration = | 980 ServiceWorkerRegistration* registration = |
| 965 context()->GetLiveRegistration(version->registration_id()); | 981 context()->GetLiveRegistration(version->registration_id()); |
| 966 bool is_update = registration->active_version() && | 982 bool is_update = registration->active_version() && |
| 967 version != registration->active_version(); | 983 version != registration->active_version(); |
| 968 if (is_update) { | 984 if (is_update) { |
| 969 // Evict the incumbent worker. | 985 // Evict the incumbent worker. |
| 970 ASSERT_FALSE(registration->waiting_version()); | 986 ASSERT_FALSE(registration->waiting_version()); |
| 971 registration->DeleteVersion( | 987 registration->DeleteVersion( |
| 972 make_scoped_refptr(registration->active_version())); | 988 make_scoped_refptr(registration->active_version())); |
| 973 } | 989 } |
| 974 UpdateJobTestHelper::OnStartWorker(embedded_worker_id, version_id, scope, | 990 UpdateJobTestHelper::OnStartWorker( |
| 975 script, pause_after_download, | 991 embedded_worker_id, version_id, scope, script, pause_after_download, |
| 976 std::move(request)); | 992 std::move(request), std::move(instance_host)); |
| 977 } | 993 } |
| 978 | 994 |
| 979 void OnRegistrationFailed(ServiceWorkerRegistration* registration) override { | 995 void OnRegistrationFailed(ServiceWorkerRegistration* registration) override { |
| 980 registration_failed_ = true; | 996 registration_failed_ = true; |
| 981 } | 997 } |
| 982 | 998 |
| 983 bool registration_failed_ = false; | 999 bool registration_failed_ = false; |
| 984 }; | 1000 }; |
| 985 | 1001 |
| 986 } // namespace | 1002 } // namespace |
| (...skipping 650 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1637 public: | 1653 public: |
| 1638 explicit CheckPauseAfterDownloadEmbeddedWorkerInstanceClient( | 1654 explicit CheckPauseAfterDownloadEmbeddedWorkerInstanceClient( |
| 1639 base::WeakPtr<EmbeddedWorkerTestHelper> helper) | 1655 base::WeakPtr<EmbeddedWorkerTestHelper> helper) |
| 1640 : EmbeddedWorkerTestHelper::MockEmbeddedWorkerInstanceClient(helper) {} | 1656 : EmbeddedWorkerTestHelper::MockEmbeddedWorkerInstanceClient(helper) {} |
| 1641 int num_of_startworker() const { return num_of_startworker_; } | 1657 int num_of_startworker() const { return num_of_startworker_; } |
| 1642 void set_next_pause_after_download(bool expectation) { | 1658 void set_next_pause_after_download(bool expectation) { |
| 1643 next_pause_after_download_ = expectation; | 1659 next_pause_after_download_ = expectation; |
| 1644 } | 1660 } |
| 1645 | 1661 |
| 1646 protected: | 1662 protected: |
| 1647 void StartWorker( | 1663 void StartWorker(const EmbeddedWorkerStartParams& params, |
| 1648 const EmbeddedWorkerStartParams& params, | 1664 mojom::ServiceWorkerEventDispatcherRequest request, |
| 1649 mojom::ServiceWorkerEventDispatcherRequest request) override { | 1665 mojom::EmbeddedWorkerInstanceHostAssociatedPtrInfo |
| 1666 instance_host) override { |
| 1650 ASSERT_TRUE(next_pause_after_download_.has_value()); | 1667 ASSERT_TRUE(next_pause_after_download_.has_value()); |
| 1651 EXPECT_EQ(next_pause_after_download_.value(), params.pause_after_download); | 1668 EXPECT_EQ(next_pause_after_download_.value(), params.pause_after_download); |
| 1652 num_of_startworker_++; | 1669 num_of_startworker_++; |
| 1653 EmbeddedWorkerTestHelper::MockEmbeddedWorkerInstanceClient::StartWorker( | 1670 EmbeddedWorkerTestHelper::MockEmbeddedWorkerInstanceClient::StartWorker( |
| 1654 params, std::move(request)); | 1671 params, std::move(request), std::move(instance_host)); |
| 1655 } | 1672 } |
| 1656 | 1673 |
| 1657 private: | 1674 private: |
| 1658 base::Optional<bool> next_pause_after_download_; | 1675 base::Optional<bool> next_pause_after_download_; |
| 1659 int num_of_startworker_ = 0; | 1676 int num_of_startworker_ = 0; |
| 1660 DISALLOW_COPY_AND_ASSIGN(CheckPauseAfterDownloadEmbeddedWorkerInstanceClient); | 1677 DISALLOW_COPY_AND_ASSIGN(CheckPauseAfterDownloadEmbeddedWorkerInstanceClient); |
| 1661 }; | 1678 }; |
| 1662 | 1679 |
| 1663 TEST_F(ServiceWorkerJobTest, Update_PauseAfterDownload) { | 1680 TEST_F(ServiceWorkerJobTest, Update_PauseAfterDownload) { |
| 1664 UpdateJobTestHelper* update_helper = new UpdateJobTestHelper; | 1681 UpdateJobTestHelper* update_helper = new UpdateJobTestHelper; |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1737 // should not be promoted to ACTIVATED because failure occur | 1754 // should not be promoted to ACTIVATED because failure occur |
| 1738 // during shutdown. | 1755 // during shutdown. |
| 1739 runner->RunUntilIdle(); | 1756 runner->RunUntilIdle(); |
| 1740 base::RunLoop().RunUntilIdle(); | 1757 base::RunLoop().RunUntilIdle(); |
| 1741 EXPECT_EQ(new_version.get(), registration->active_version()); | 1758 EXPECT_EQ(new_version.get(), registration->active_version()); |
| 1742 EXPECT_EQ(ServiceWorkerVersion::ACTIVATING, new_version->status()); | 1759 EXPECT_EQ(ServiceWorkerVersion::ACTIVATING, new_version->status()); |
| 1743 registration->RemoveListener(update_helper); | 1760 registration->RemoveListener(update_helper); |
| 1744 } | 1761 } |
| 1745 | 1762 |
| 1746 } // namespace content | 1763 } // namespace content |
| OLD | NEW |