| 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 | 6 |
| 7 #include "base/files/scoped_temp_dir.h" | 7 #include "base/files/scoped_temp_dir.h" |
| 8 #include "base/logging.h" | 8 #include "base/logging.h" |
| 9 #include "base/macros.h" | 9 #include "base/macros.h" |
| 10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
| (...skipping 774 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 785 ~UpdateJobTestHelper() override { | 785 ~UpdateJobTestHelper() override { |
| 786 if (registration_.get()) | 786 if (registration_.get()) |
| 787 registration_->RemoveListener(this); | 787 registration_->RemoveListener(this); |
| 788 } | 788 } |
| 789 | 789 |
| 790 ServiceWorkerStorage* storage() { return context()->storage(); } | 790 ServiceWorkerStorage* storage() { return context()->storage(); } |
| 791 ServiceWorkerJobCoordinator* job_coordinator() { | 791 ServiceWorkerJobCoordinator* job_coordinator() { |
| 792 return context()->job_coordinator(); | 792 return context()->job_coordinator(); |
| 793 } | 793 } |
| 794 | 794 |
| 795 bool force_bypass_cache_for_scripts() const { | |
| 796 return force_bypass_cache_for_scripts_; | |
| 797 } | |
| 798 void set_force_bypass_cache_for_scripts(bool force_bypass_cache_for_scripts) { | |
| 799 force_bypass_cache_for_scripts_ = force_bypass_cache_for_scripts; | |
| 800 } | |
| 801 | |
| 802 void set_force_start_worker_failure(bool force_start_worker_failure) { | 795 void set_force_start_worker_failure(bool force_start_worker_failure) { |
| 803 force_start_worker_failure_ = force_start_worker_failure; | 796 force_start_worker_failure_ = force_start_worker_failure; |
| 804 } | 797 } |
| 805 | 798 |
| 806 scoped_refptr<ServiceWorkerRegistration> SetupInitialRegistration( | 799 scoped_refptr<ServiceWorkerRegistration> SetupInitialRegistration( |
| 807 const GURL& test_origin) { | 800 const GURL& test_origin) { |
| 808 scoped_refptr<ServiceWorkerRegistration> registration; | 801 scoped_refptr<ServiceWorkerRegistration> registration; |
| 809 bool called = false; | 802 bool called = false; |
| 810 job_coordinator()->Register( | 803 job_coordinator()->Register( |
| 811 test_origin.Resolve(kScope), | 804 test_origin.Resolve(kScope), |
| (...skipping 20 matching lines...) Expand all Loading... |
| 832 const uint64_t kMockScriptSize = 19284; | 825 const uint64_t kMockScriptSize = 19284; |
| 833 ServiceWorkerVersion* version = context()->GetLiveVersion(version_id); | 826 ServiceWorkerVersion* version = context()->GetLiveVersion(version_id); |
| 834 ServiceWorkerRegistration* registration = | 827 ServiceWorkerRegistration* registration = |
| 835 context()->GetLiveRegistration(version->registration_id()); | 828 context()->GetLiveRegistration(version->registration_id()); |
| 836 bool is_update = registration->active_version() && | 829 bool is_update = registration->active_version() && |
| 837 version != registration->active_version(); | 830 version != registration->active_version(); |
| 838 | 831 |
| 839 ASSERT_TRUE(version); | 832 ASSERT_TRUE(version); |
| 840 version->AddListener(this); | 833 version->AddListener(this); |
| 841 | 834 |
| 842 if (force_bypass_cache_for_scripts()) | 835 // Simulate network access. |
| 843 version->set_force_bypass_cache_for_scripts(true); | 836 base::TimeDelta time_since_last_check = |
| 837 base::Time::Now() - registration->last_update_check(); |
| 838 if (!is_update || script.GetOrigin() != kNoChangeOrigin || |
| 839 time_since_last_check > base::TimeDelta::FromHours( |
| 840 kServiceWorkerScriptMaxCacheAgeInHours)) { |
| 841 version->embedded_worker()->OnNetworkAccessedForScriptLoad(); |
| 842 } |
| 844 | 843 |
| 844 int64_t resource_id = storage()->NewResourceId(); |
| 845 version->script_cache_map()->NotifyStartedCaching(script, resource_id); |
| 845 if (!is_update) { | 846 if (!is_update) { |
| 846 // Spoof caching the script for the initial version. | 847 // Spoof caching the script for the initial version. |
| 847 int64_t resource_id = storage()->NewResourceId(); | |
| 848 version->script_cache_map()->NotifyStartedCaching(script, resource_id); | |
| 849 WriteStringResponse(storage(), resource_id, kMockScriptBody); | 848 WriteStringResponse(storage(), resource_id, kMockScriptBody); |
| 850 version->script_cache_map()->NotifyFinishedCaching( | 849 version->script_cache_map()->NotifyFinishedCaching( |
| 851 script, kMockScriptSize, net::URLRequestStatus(), std::string()); | 850 script, kMockScriptSize, net::URLRequestStatus(), std::string()); |
| 852 } else { | 851 } else { |
| 853 if (script.GetOrigin() == kNoChangeOrigin) { | 852 if (script.GetOrigin() == kNoChangeOrigin) { |
| 854 version->SetStartWorkerStatusCode(SERVICE_WORKER_ERROR_EXISTS); | 853 // Simulate fetching the updated script and finding it's identical to |
| 855 EmbeddedWorkerTestHelper::OnStopWorker(embedded_worker_id); | 854 // the incumbent. |
| 855 net::URLRequestStatus status = |
| 856 net::URLRequestStatus::FromError(net::ERR_FILE_EXISTS); |
| 857 version->script_cache_map()->NotifyFinishedCaching( |
| 858 script, kMockScriptSize, status, std::string()); |
| 859 SimulateWorkerScriptLoaded(embedded_worker_id); |
| 856 return; | 860 return; |
| 857 } | 861 } |
| 858 | 862 |
| 859 // Spoof caching the script for the new version. | 863 // Spoof caching the script for the new version. |
| 860 int64_t resource_id = storage()->NewResourceId(); | |
| 861 version->script_cache_map()->NotifyStartedCaching(script, resource_id); | |
| 862 WriteStringResponse(storage(), resource_id, "mock_different_script"); | 864 WriteStringResponse(storage(), resource_id, "mock_different_script"); |
| 863 version->script_cache_map()->NotifyFinishedCaching( | 865 version->script_cache_map()->NotifyFinishedCaching( |
| 864 script, kMockScriptSize, net::URLRequestStatus(), std::string()); | 866 script, kMockScriptSize, net::URLRequestStatus(), std::string()); |
| 865 } | 867 } |
| 866 | 868 |
| 867 EmbeddedWorkerTestHelper::OnStartWorker( | 869 EmbeddedWorkerTestHelper::OnStartWorker( |
| 868 embedded_worker_id, version_id, scope, script, pause_after_download); | 870 embedded_worker_id, version_id, scope, script, pause_after_download); |
| 869 } | 871 } |
| 870 | 872 |
| 871 void OnResumeAfterDownload(int embedded_worker_id) override { | 873 void OnResumeAfterDownload(int embedded_worker_id) override { |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 903 entry.version_id = version->version_id(); | 905 entry.version_id = version->version_id(); |
| 904 entry.status = version->status(); | 906 entry.status = version->status(); |
| 905 state_change_log_.push_back(entry); | 907 state_change_log_.push_back(entry); |
| 906 } | 908 } |
| 907 | 909 |
| 908 scoped_refptr<ServiceWorkerRegistration> registration_; | 910 scoped_refptr<ServiceWorkerRegistration> registration_; |
| 909 | 911 |
| 910 std::vector<AttributeChangeLogEntry> attribute_change_log_; | 912 std::vector<AttributeChangeLogEntry> attribute_change_log_; |
| 911 std::vector<StateChangeLogEntry> state_change_log_; | 913 std::vector<StateChangeLogEntry> state_change_log_; |
| 912 bool update_found_ = false; | 914 bool update_found_ = false; |
| 913 bool force_bypass_cache_for_scripts_ = false; | |
| 914 bool force_start_worker_failure_ = false; | 915 bool force_start_worker_failure_ = false; |
| 915 }; | 916 }; |
| 916 | 917 |
| 917 // Helper class for update tests that evicts the active version when the update | 918 // Helper class for update tests that evicts the active version when the update |
| 918 // worker is about to be started. | 919 // worker is about to be started. |
| 919 class EvictIncumbentVersionHelper : public UpdateJobTestHelper { | 920 class EvictIncumbentVersionHelper : public UpdateJobTestHelper { |
| 920 public: | 921 public: |
| 921 EvictIncumbentVersionHelper() {} | 922 EvictIncumbentVersionHelper() {} |
| 922 ~EvictIncumbentVersionHelper() override {} | 923 ~EvictIncumbentVersionHelper() override {} |
| 923 | 924 |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 993 const base::Time kYesterday = | 994 const base::Time kYesterday = |
| 994 kToday - base::TimeDelta::FromDays(1) - base::TimeDelta::FromHours(1); | 995 kToday - base::TimeDelta::FromDays(1) - base::TimeDelta::FromHours(1); |
| 995 UpdateJobTestHelper* update_helper = new UpdateJobTestHelper; | 996 UpdateJobTestHelper* update_helper = new UpdateJobTestHelper; |
| 996 helper_.reset(update_helper); | 997 helper_.reset(update_helper); |
| 997 scoped_refptr<ServiceWorkerRegistration> registration = | 998 scoped_refptr<ServiceWorkerRegistration> registration = |
| 998 update_helper->SetupInitialRegistration(kNoChangeOrigin); | 999 update_helper->SetupInitialRegistration(kNoChangeOrigin); |
| 999 ASSERT_TRUE(registration.get()); | 1000 ASSERT_TRUE(registration.get()); |
| 1000 | 1001 |
| 1001 registration->AddListener(update_helper); | 1002 registration->AddListener(update_helper); |
| 1002 | 1003 |
| 1003 // Run an update that does not bypass the network cache. The check time | 1004 // Run an update where the script did not change and the network was not |
| 1004 // should not be updated. | 1005 // accessed. The check time should not be updated. |
| 1005 registration->set_last_update_check(kToday); | 1006 registration->set_last_update_check(kToday); |
| 1006 registration->active_version()->StartUpdate(); | 1007 registration->active_version()->StartUpdate(); |
| 1007 base::RunLoop().RunUntilIdle(); | 1008 base::RunLoop().RunUntilIdle(); |
| 1008 EXPECT_EQ(kToday, registration->last_update_check()); | 1009 EXPECT_EQ(kToday, registration->last_update_check()); |
| 1010 EXPECT_FALSE(update_helper->update_found_); |
| 1011 |
| 1012 // Run an update where the script did not change and the network was accessed. |
| 1013 // The check time should be updated. |
| 1014 registration->set_last_update_check(kYesterday); |
| 1015 registration->active_version()->StartUpdate(); |
| 1016 base::RunLoop().RunUntilIdle(); |
| 1017 EXPECT_LT(kYesterday, registration->last_update_check()); |
| 1018 EXPECT_FALSE(update_helper->update_found_); |
| 1009 registration->RemoveListener(update_helper); | 1019 registration->RemoveListener(update_helper); |
| 1010 | 1020 |
| 1011 registration = update_helper->SetupInitialRegistration(kNewVersionOrigin); | 1021 registration = update_helper->SetupInitialRegistration(kNewVersionOrigin); |
| 1012 ASSERT_TRUE(registration.get()); | 1022 ASSERT_TRUE(registration.get()); |
| 1013 | 1023 |
| 1014 registration->AddListener(update_helper); | 1024 registration->AddListener(update_helper); |
| 1015 | 1025 |
| 1016 // Run an update that bypasses the network cache. The check time should be | 1026 // Run an update where the script changed. The check time should be updated. |
| 1017 // updated. | |
| 1018 update_helper->set_force_bypass_cache_for_scripts(true); | |
| 1019 registration->set_last_update_check(kYesterday); | 1027 registration->set_last_update_check(kYesterday); |
| 1020 registration->active_version()->StartUpdate(); | 1028 registration->active_version()->StartUpdate(); |
| 1021 base::RunLoop().RunUntilIdle(); | 1029 base::RunLoop().RunUntilIdle(); |
| 1022 EXPECT_LT(kYesterday, registration->last_update_check()); | 1030 EXPECT_LT(kYesterday, registration->last_update_check()); |
| 1023 | 1031 |
| 1024 // Run an update to a worker that loads successfully but fails to start up | 1032 // Run an update to a worker that loads successfully but fails to start up |
| 1025 // (script evaluation failure). The check time should be updated. | 1033 // (script evaluation failure). The check time should be updated. |
| 1026 update_helper->set_force_bypass_cache_for_scripts(true); | |
| 1027 update_helper->set_force_start_worker_failure(true); | 1034 update_helper->set_force_start_worker_failure(true); |
| 1028 registration->set_last_update_check(kYesterday); | 1035 registration->set_last_update_check(kYesterday); |
| 1029 registration->active_version()->StartUpdate(); | 1036 registration->active_version()->StartUpdate(); |
| 1030 base::RunLoop().RunUntilIdle(); | 1037 base::RunLoop().RunUntilIdle(); |
| 1031 EXPECT_LT(kYesterday, registration->last_update_check()); | 1038 EXPECT_LT(kYesterday, registration->last_update_check()); |
| 1032 } | 1039 } |
| 1033 | 1040 |
| 1034 TEST_F(ServiceWorkerJobTest, Update_NewVersion) { | 1041 TEST_F(ServiceWorkerJobTest, Update_NewVersion) { |
| 1035 UpdateJobTestHelper* update_helper = new UpdateJobTestHelper; | 1042 UpdateJobTestHelper* update_helper = new UpdateJobTestHelper; |
| 1036 helper_.reset(update_helper); | 1043 helper_.reset(update_helper); |
| (...skipping 530 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1567 ASSERT_TRUE(start_msg); | 1574 ASSERT_TRUE(start_msg); |
| 1568 EmbeddedWorkerMsg_StartWorker::Param param; | 1575 EmbeddedWorkerMsg_StartWorker::Param param; |
| 1569 EmbeddedWorkerMsg_StartWorker::Read(start_msg, ¶m); | 1576 EmbeddedWorkerMsg_StartWorker::Read(start_msg, ¶m); |
| 1570 EmbeddedWorkerMsg_StartWorker_Params start_params = base::get<0>(param); | 1577 EmbeddedWorkerMsg_StartWorker_Params start_params = base::get<0>(param); |
| 1571 EXPECT_TRUE(start_params.pause_after_download); | 1578 EXPECT_TRUE(start_params.pause_after_download); |
| 1572 sink->ClearMessages(); | 1579 sink->ClearMessages(); |
| 1573 } | 1580 } |
| 1574 } | 1581 } |
| 1575 | 1582 |
| 1576 } // namespace content | 1583 } // namespace content |
| OLD | NEW |