Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(466)

Unified Diff: content/browser/service_worker/service_worker_context_request_handler_unittest.cc

Issue 2771823002: Implement updateViaCache flag and no-cache by default for main service worker scripts
Patch Set: change useCache to updateViaCache Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
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 5f08d2307fe6daae715bd05b42e60ce40cd4b803..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(
- scope_, 1L, context()->AsWeakPtr());
- version_ = new ServiceWorkerVersion(registration_.get(), script_url_,
- context()->storage()->NewVersionId(),
- context()->AsWeakPtr());
+ provider_host_ = nullptr;
+
+ SetUpServiceWorker();
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);
+ }
+
+ void SetUpServiceWorkerWithOptions(
+ blink::WebServiceWorkerUpdateViaCache update_via_cache) {
+ // A new unstored registration/version.
+
+ 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;

Powered by Google App Engine
This is Rietveld 408576698