Chromium Code Reviews| Index: content/browser/service_worker/service_worker_context_request_handler_unittest.cc |
| diff --git a/content/browser/service_worker/service_worker_context_request_handler_unittest.cc b/content/browser/service_worker/service_worker_context_request_handler_unittest.cc |
| index 635ad578ea196e7df07bfe5b71916e21e3566fc8..70df066ae84437841ce8ca33914b949036f098ad 100644 |
| --- a/content/browser/service_worker/service_worker_context_request_handler_unittest.cc |
| +++ b/content/browser/service_worker/service_worker_context_request_handler_unittest.cc |
| @@ -61,15 +61,12 @@ class ServiceWorkerContextRequestHandlerTest : public testing::Test { |
| context()->storage()->LazyInitialize(base::Bind(&base::DoNothing)); |
| base::RunLoop().RunUntilIdle(); |
| - // A new unstored registration/version. |
| scope_ = GURL("https://host/scope/"); |
| script_url_ = GURL("https://host/script.js"); |
| import_script_url_ = GURL("https://host/import.js"); |
| - registration_ = new ServiceWorkerRegistration( |
| - ServiceWorkerRegistrationOptions(scope_), 1L, context()->AsWeakPtr()); |
| - version_ = new ServiceWorkerVersion(registration_.get(), script_url_, |
| - context()->storage()->NewVersionId(), |
| - context()->AsWeakPtr()); |
| + provider_host_ = nullptr; |
| + |
| + SetUpServiceWorker(); |
|
nhiroki
2017/07/21 04:39:46
All tests seem to explicitly call SetUpServiceWork
yuryu
2017/08/03 04:20:05
Actually these SetUpServiceWorker calls were not n
|
| SetUpProvider(); |
| std::unique_ptr<MockHttpProtocolHandler> handler( |
| @@ -87,6 +84,26 @@ class ServiceWorkerContextRequestHandlerTest : public testing::Test { |
| ServiceWorkerContextCore* context() const { return helper_->context(); } |
| + void SetUpServiceWorker() { |
| + SetUpServiceWorkerWithOptions( |
| + blink::WebServiceWorkerUpdateViaCache::kImports); |
|
nhiroki
2017/07/21 04:39:46
How about using default value? Default value in pr
yuryu
2017/08/03 04:20:05
Merged SetUpServiceWorker and SetUpServiceWorkerWi
|
| + } |
| + |
| + void SetUpServiceWorkerWithOptions( |
| + blink::WebServiceWorkerUpdateViaCache update_via_cache) { |
| + // A new unstored registration/version. |
|
nhiroki
2017/07/21 04:39:46
I feel this comment is not so useful for reading c
yuryu
2017/08/03 04:20:05
Removed.
|
| + |
| + registration_ = new ServiceWorkerRegistration( |
| + ServiceWorkerRegistrationOptions(scope_, update_via_cache), |
| + context()->storage()->NewRegistrationId(), context()->AsWeakPtr()); |
| + version_ = new ServiceWorkerVersion(registration_.get(), script_url_, |
| + context()->storage()->NewVersionId(), |
| + context()->AsWeakPtr()); |
| + |
| + if (provider_host_) |
| + provider_host_->running_hosted_version_ = version_; |
| + } |
| + |
| void SetUpProvider() { |
| std::unique_ptr<ServiceWorkerProviderHost> host = |
| CreateProviderHostForServiceWorkerContext( |
| @@ -142,6 +159,34 @@ class ServiceWorkerContextRequestHandlerTest : public testing::Test { |
| }; |
| TEST_F(ServiceWorkerContextRequestHandlerTest, UpdateBefore24Hours) { |
| + SetUpServiceWorker(); |
| + // Give the registration a very recent last update time and pretend |
| + // we're installing a new version. |
| + registration_->set_last_update_check(base::Time::Now()); |
| + version_->SetStatus(ServiceWorkerVersion::NEW); |
| + |
| + // Conduct a resource fetch for the main script. |
| + base::HistogramTester histograms; |
| + std::unique_ptr<net::URLRequest> request(CreateRequest(script_url_)); |
| + std::unique_ptr<ServiceWorkerContextRequestHandler> handler( |
| + CreateHandler(RESOURCE_TYPE_SERVICE_WORKER)); |
| + std::unique_ptr<net::URLRequestJob> job( |
| + handler->MaybeCreateJob(request.get(), nullptr, nullptr)); |
| + ASSERT_TRUE(job.get()); |
| + ServiceWorkerWriteToCacheJob* sw_job = |
| + static_cast<ServiceWorkerWriteToCacheJob*>(job.get()); |
| + histograms.ExpectUniqueSample( |
| + "ServiceWorker.ContextRequestHandlerStatus.NewWorker.MainScript", |
| + static_cast<int>( |
| + ServiceWorkerContextRequestHandler::CreateJobStatus::WRITE_JOB), |
| + 1); |
| + |
| + // Verify the net request is initialized to bypass the browser cache. |
| + EXPECT_TRUE(sw_job->net_request_->load_flags() & net::LOAD_BYPASS_CACHE); |
| +} |
| + |
| +TEST_F(ServiceWorkerContextRequestHandlerTest, UpdateBefore24HoursUsingCache) { |
| + SetUpServiceWorkerWithOptions(blink::WebServiceWorkerUpdateViaCache::kNone); |
| // Give the registration a very recent last update time and pretend |
| // we're installing a new version. |
| registration_->set_last_update_check(base::Time::Now()); |
| @@ -167,7 +212,8 @@ TEST_F(ServiceWorkerContextRequestHandlerTest, UpdateBefore24Hours) { |
| EXPECT_FALSE(sw_job->net_request_->load_flags() & net::LOAD_BYPASS_CACHE); |
| } |
| -TEST_F(ServiceWorkerContextRequestHandlerTest, UpdateAfter24Hours) { |
| +TEST_F(ServiceWorkerContextRequestHandlerTest, UpdateAfter24HoursUsingCache) { |
| + SetUpServiceWorkerWithOptions(blink::WebServiceWorkerUpdateViaCache::kNone); |
| // Give the registration a old update time and pretend |
| // we're installing a new version. |
| registration_->set_last_update_check( |
| @@ -195,6 +241,7 @@ TEST_F(ServiceWorkerContextRequestHandlerTest, UpdateAfter24Hours) { |
| } |
| TEST_F(ServiceWorkerContextRequestHandlerTest, UpdateForceBypassCache) { |
| + SetUpServiceWorkerWithOptions(blink::WebServiceWorkerUpdateViaCache::kNone); |
| // Give the registration a very recent last update time and pretend |
| // we're installing a new version. |
| registration_->set_last_update_check(base::Time::Now()); |
| @@ -217,6 +264,7 @@ TEST_F(ServiceWorkerContextRequestHandlerTest, UpdateForceBypassCache) { |
| TEST_F(ServiceWorkerContextRequestHandlerTest, |
| ServiceWorkerDataRequestAnnotation) { |
| + SetUpServiceWorker(); |
| version_->SetStatus(ServiceWorkerVersion::NEW); |
| // Conduct a resource fetch for the main script. |
| @@ -245,6 +293,7 @@ TEST_F(ServiceWorkerContextRequestHandlerTest, |
| // flag should be ignored. |
| TEST_F(ServiceWorkerContextRequestHandlerTest, |
| SkipServiceWorkerForServiceWorkerRequest) { |
| + SetUpServiceWorker(); |
| // Conduct a resource fetch for the main script. |
| version_->SetStatus(ServiceWorkerVersion::NEW); |
| std::unique_ptr<net::URLRequest> request(CreateRequest(script_url_)); |
| @@ -262,6 +311,7 @@ TEST_F(ServiceWorkerContextRequestHandlerTest, |
| } |
| TEST_F(ServiceWorkerContextRequestHandlerTest, NewWorker) { |
| + SetUpServiceWorker(); |
| // Conduct a resource fetch for the main script. |
| { |
| base::HistogramTester histograms; |
| @@ -296,6 +346,7 @@ TEST_F(ServiceWorkerContextRequestHandlerTest, NewWorker) { |
| } |
| TEST_F(ServiceWorkerContextRequestHandlerTest, InstalledWorker) { |
| + SetUpServiceWorker(); |
| using Resource = ServiceWorkerDatabase::ResourceRecord; |
| std::vector<Resource> resources = { |
| Resource(context()->storage()->NewResourceId(), script_url_, 100), |
| @@ -341,6 +392,7 @@ TEST_F(ServiceWorkerContextRequestHandlerTest, InstalledWorker) { |
| } |
| TEST_F(ServiceWorkerContextRequestHandlerTest, Incumbent) { |
| + SetUpServiceWorker(); |
| // Make an incumbent version. |
| scoped_refptr<ServiceWorkerVersion> incumbent = new ServiceWorkerVersion( |
| registration_.get(), script_url_, context()->storage()->NewVersionId(), |
| @@ -372,6 +424,7 @@ TEST_F(ServiceWorkerContextRequestHandlerTest, Incumbent) { |
| } |
| TEST_F(ServiceWorkerContextRequestHandlerTest, ErrorCases) { |
| + SetUpServiceWorker(); |
| { |
| // Set up a request. |
| base::HistogramTester histograms; |