Index: content/browser/service_worker/service_worker_write_to_cache_job_unittest.cc |
diff --git a/content/browser/service_worker/service_worker_write_to_cache_job_unittest.cc b/content/browser/service_worker/service_worker_write_to_cache_job_unittest.cc |
index 18115d9b8aa24b82b95b028b1dc49c1ed2ab22a0..f5fb4f638edc0495a4465b0820957fcc28a099d0 100644 |
--- a/content/browser/service_worker/service_worker_write_to_cache_job_unittest.cc |
+++ b/content/browser/service_worker/service_worker_write_to_cache_job_unittest.cc |
@@ -60,14 +60,45 @@ net::URLRequestJob* CreateNormalURLRequestJob( |
true); |
} |
-net::URLRequestJob* CreateResponseJob(const std::string& response_data, |
- net::URLRequest* request, |
- net::NetworkDelegate* network_delegate) { |
+net::URLRequestJob* CreateCachedResponseJob( |
+ const std::string& response_data, |
+ net::URLRequest* request, |
+ net::NetworkDelegate* network_delegate) { |
return new net::URLRequestTestJob(request, network_delegate, |
std::string(kHeaders, arraysize(kHeaders)), |
response_data, true); |
} |
+class NetworkResponseJob : public net::URLRequestTestJob { |
+ public: |
+ NetworkResponseJob(net::URLRequest* request, |
+ net::NetworkDelegate* network_delegate, |
+ const std::string& response_headers, |
+ const std::string& response_data, |
+ bool auto_advance) |
+ : net::URLRequestTestJob(request, |
+ network_delegate, |
+ response_headers, |
+ response_data, |
+ auto_advance) {} |
+ void GetResponseInfo(net::HttpResponseInfo* info) override { |
+ URLRequestTestJob::GetResponseInfo(info); |
+ info->network_accessed = true; |
+ } |
+ |
+ protected: |
+ ~NetworkResponseJob() override {} |
+}; |
+ |
+net::URLRequestJob* CreateNetworkResponseJob( |
+ const std::string& response_data, |
+ net::URLRequest* request, |
+ net::NetworkDelegate* network_delegate) { |
+ return new NetworkResponseJob(request, network_delegate, |
+ std::string(kHeaders, arraysize(kHeaders)), |
+ response_data, true); |
+} |
+ |
net::URLRequestJob* CreateInvalidMimeTypeJob( |
net::URLRequest* request, |
net::NetworkDelegate* network_delegate) { |
@@ -84,6 +115,15 @@ net::URLRequestJob* CreateInvalidMimeTypeJob( |
true); |
} |
+net::URLRequestJob* CreateErrorReponseJob( |
+ net::URLRequest* request, |
+ net::NetworkDelegate* network_delegate) { |
+ const char kErrorHeaders[] = "HTTP/1.1 404 Not Found\0\0"; |
+ return new net::URLRequestTestJob( |
+ request, network_delegate, |
+ std::string(kErrorHeaders, arraysize(kErrorHeaders)), kScriptCode, true); |
+} |
+ |
class SSLCertificateErrorJob : public net::URLRequestTestJob { |
public: |
SSLCertificateErrorJob(net::URLRequest* request, |
@@ -333,7 +373,7 @@ class ServiceWorkerWriteToCacheJobTest : public testing::Test { |
int CreateIncumbent(const std::string& response) { |
mock_protocol_handler_->SetCreateJobCallback( |
- base::Bind(&CreateResponseJob, response)); |
+ base::Bind(&CreateNetworkResponseJob, response)); |
request_->Start(); |
base::RunLoop().RunUntilIdle(); |
EXPECT_EQ(net::URLRequestStatus::SUCCESS, request_->status().status()); |
@@ -357,10 +397,14 @@ class ServiceWorkerWriteToCacheJobTest : public testing::Test { |
return version->script_cache_map()->LookupResourceId(script_url_); |
} |
- // Performs the net request for an update of |registration_|'s incumbent |
- // to the script |response|. Returns the new version. |
+ // Performs the net request for an update of |registration_|'s incumbent to |
+ // the script |response|. Returns the new version. |
+ // The types of the callbacks used for |callback|: |
+ // - CreateNetworkResponseJob: Network accessed, 200 OK |
+ // - CreateCachedResponseJob: Served from HTTP cache |
+ // - CreateErrorReponseJob: Network accessed, Non-2xx |
scoped_refptr<ServiceWorkerVersion> UpdateScript( |
- const std::string& response) { |
+ const MockHttpProtocolHandler::JobCallback& callback) { |
int render_process_id = NextRenderProcessId(); |
int provider_id = NextProviderId(); |
scoped_refptr<ServiceWorkerVersion> new_version = |
@@ -369,8 +413,7 @@ class ServiceWorkerWriteToCacheJobTest : public testing::Test { |
CreateHostForVersion(render_process_id, provider_id, new_version); |
SetUpScriptRequest(render_process_id, provider_id); |
- mock_protocol_handler_->SetCreateJobCallback( |
- base::Bind(&CreateResponseJob, response)); |
+ mock_protocol_handler_->SetCreateJobCallback(callback); |
request_->Start(); |
base::RunLoop().RunUntilIdle(); |
return new_version; |
@@ -462,7 +505,8 @@ TEST_F(ServiceWorkerWriteToCacheJobTest, CertStatusError) { |
TEST_F(ServiceWorkerWriteToCacheJobTest, Update_SameScript) { |
std::string response = GenerateLongResponse(); |
CreateIncumbent(response); |
- scoped_refptr<ServiceWorkerVersion> version = UpdateScript(response); |
+ scoped_refptr<ServiceWorkerVersion> version = |
+ UpdateScript(base::Bind(&CreateNetworkResponseJob, response)); |
EXPECT_EQ(kInvalidServiceWorkerResponseId, GetResourceId(version.get())); |
} |
@@ -472,31 +516,32 @@ TEST_F(ServiceWorkerWriteToCacheJobTest, Update_SameSizeScript) { |
// Change the first byte. |
response[0] = 'x'; |
- scoped_refptr<ServiceWorkerVersion> version = UpdateScript(response); |
+ scoped_refptr<ServiceWorkerVersion> version = |
+ UpdateScript(base::Bind(&CreateNetworkResponseJob, response)); |
VerifyResource(GetResourceId(version.get()), response); |
registration_->SetWaitingVersion(version); |
// Change something within the first block. |
response[5555] = 'x'; |
- version = UpdateScript(response); |
+ version = UpdateScript(base::Bind(&CreateNetworkResponseJob, response)); |
VerifyResource(GetResourceId(version.get()), response); |
registration_->SetWaitingVersion(version); |
// Change something in a middle block. |
response[kMiddleBlock * kBlockSize + 111] = 'x'; |
- version = UpdateScript(response); |
+ version = UpdateScript(base::Bind(&CreateNetworkResponseJob, response)); |
VerifyResource(GetResourceId(version.get()), response); |
registration_->SetWaitingVersion(version); |
// Change something within the last block. |
response[(kNumBlocks - 1) * kBlockSize] = 'x'; |
- version = UpdateScript(response); |
+ version = UpdateScript(base::Bind(&CreateNetworkResponseJob, response)); |
VerifyResource(GetResourceId(version.get()), response); |
registration_->SetWaitingVersion(version); |
// Change the last byte. |
response[(kNumBlocks * kBlockSize) - 1] = 'x'; |
- version = UpdateScript(response); |
+ version = UpdateScript(base::Bind(&CreateNetworkResponseJob, response)); |
VerifyResource(GetResourceId(version.get()), response); |
registration_->SetWaitingVersion(version); |
} |
@@ -507,25 +552,26 @@ TEST_F(ServiceWorkerWriteToCacheJobTest, Update_TruncatedScript) { |
// Truncate a single byte. |
response.resize(response.size() - 1); |
- scoped_refptr<ServiceWorkerVersion> version = UpdateScript(response); |
+ scoped_refptr<ServiceWorkerVersion> version = |
+ UpdateScript(base::Bind(&CreateNetworkResponseJob, response)); |
VerifyResource(GetResourceId(version.get()), response); |
registration_->SetWaitingVersion(version); |
// Truncate to a middle block. |
response.resize((kMiddleBlock + 1) * kBlockSize + 111); |
- version = UpdateScript(response); |
+ version = UpdateScript(base::Bind(&CreateNetworkResponseJob, response)); |
VerifyResource(GetResourceId(version.get()), response); |
registration_->SetWaitingVersion(version); |
// Truncate to a block boundary. |
response.resize((kMiddleBlock - 1) * kBlockSize); |
- version = UpdateScript(response); |
+ version = UpdateScript(base::Bind(&CreateNetworkResponseJob, response)); |
VerifyResource(GetResourceId(version.get()), response); |
registration_->SetWaitingVersion(version); |
// Truncate to a single byte. |
response.resize(1); |
- version = UpdateScript(response); |
+ version = UpdateScript(base::Bind(&CreateNetworkResponseJob, response)); |
VerifyResource(GetResourceId(version.get()), response); |
registration_->SetWaitingVersion(version); |
} |
@@ -536,19 +582,20 @@ TEST_F(ServiceWorkerWriteToCacheJobTest, Update_ElongatedScript) { |
// Extend a single byte. |
std::string new_response = original_response + 'a'; |
- scoped_refptr<ServiceWorkerVersion> version = UpdateScript(new_response); |
+ scoped_refptr<ServiceWorkerVersion> version = |
+ UpdateScript(base::Bind(&CreateNetworkResponseJob, new_response)); |
VerifyResource(GetResourceId(version.get()), new_response); |
registration_->SetWaitingVersion(version); |
// Extend multiple blocks. |
new_response = original_response + std::string(3 * kBlockSize, 'a'); |
- version = UpdateScript(new_response); |
+ version = UpdateScript(base::Bind(&CreateNetworkResponseJob, new_response)); |
VerifyResource(GetResourceId(version.get()), new_response); |
registration_->SetWaitingVersion(version); |
// Extend multiple blocks and bytes. |
new_response = original_response + std::string(7 * kBlockSize + 777, 'a'); |
- version = UpdateScript(new_response); |
+ version = UpdateScript(base::Bind(&CreateNetworkResponseJob, new_response)); |
VerifyResource(GetResourceId(version.get()), new_response); |
registration_->SetWaitingVersion(version); |
} |
@@ -559,18 +606,50 @@ TEST_F(ServiceWorkerWriteToCacheJobTest, Update_EmptyScript) { |
// Update from empty to non-empty. |
std::string response = GenerateLongResponse(); |
- scoped_refptr<ServiceWorkerVersion> version = UpdateScript(response); |
+ scoped_refptr<ServiceWorkerVersion> version = |
+ UpdateScript(base::Bind(&CreateNetworkResponseJob, response)); |
VerifyResource(GetResourceId(version.get()), response); |
registration_->SetWaitingVersion(version); |
// Update from non-empty to empty. |
- version = UpdateScript(std::string()); |
+ version = UpdateScript(base::Bind(&CreateNetworkResponseJob, std::string())); |
VerifyResource(GetResourceId(version.get()), std::string()); |
registration_->SetWaitingVersion(version); |
// Update from empty to empty. |
- version = UpdateScript(std::string()); |
+ version = UpdateScript(base::Bind(&CreateNetworkResponseJob, std::string())); |
EXPECT_EQ(kInvalidServiceWorkerResponseId, GetResourceId(version.get())); |
} |
+TEST_F(ServiceWorkerWriteToCacheJobTest, Update_BumpLastUpdateCheckTime) { |
+ std::string response = GenerateLongResponse(); |
+ CreateIncumbent(response); |
+ |
+ // Script was served from HTTPCache. |
+ base::Time current = base::Time::Now(); |
+ registration_->set_last_update_check(current); |
+ scoped_refptr<ServiceWorkerVersion> version = |
+ UpdateScript(base::Bind(&CreateCachedResponseJob, response)); |
+ EXPECT_EQ(current, registration_->last_update_check()); |
+ |
+ // Same script but the request accessed the network. |
+ version = UpdateScript(base::Bind(&CreateNetworkResponseJob, response)); |
+ EXPECT_LT(current, registration_->last_update_check()); |
+ |
+ // Changed the first byte and the request accessed the network. |
+ response[0] = 'x'; |
+ current = base::Time::Now(); |
+ registration_->set_last_update_check(current); |
+ version = UpdateScript(base::Bind(&CreateNetworkResponseJob, response)); |
+ VerifyResource(GetResourceId(version.get()), response); |
+ registration_->SetWaitingVersion(version); |
+ EXPECT_LT(current, registration_->last_update_check()); |
+ |
+ // The request accessed the network but received a non-2xx error. |
+ current = base::Time::Now(); |
+ registration_->set_last_update_check(current); |
+ version = UpdateScript(base::Bind(&CreateErrorReponseJob)); |
+ EXPECT_EQ(current, registration_->last_update_check()); |
+} |
+ |
} // namespace content |