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

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: fix tests Created 3 years, 9 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 cae7d7e543211aa804d114662a9d4effbd910079..34e33d5bafc08127a360cd66133127f84e1f1de4 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
@@ -60,15 +60,33 @@ class ServiceWorkerContextRequestHandlerTest : public testing::Test {
context()->storage()->LazyInitialize(base::Bind(&base::DoNothing));
base::RunLoop().RunUntilIdle();
+ SetUpServiceWorker();
+ }
+
+ void TearDown() override {
+ version_ = nullptr;
+ registration_ = nullptr;
+ helper_.reset();
+ }
+
+ ServiceWorkerContextCore* context() const { return helper_->context(); }
+
+ void SetUpServiceWorker() {
+ SetUpServiceWorkerWithOptions(false /* use_cache */);
+ }
+
+ void SetUpServiceWorkerWithOptions(bool use_cache) {
// 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());
+ scope_, use_cache, context()->storage()->NewRegistrationId(),
+ context()->AsWeakPtr());
version_ = new ServiceWorkerVersion(registration_.get(), script_url_,
context()->storage()->NewVersionId(),
context()->AsWeakPtr());
+
SetUpProvider();
std::unique_ptr<MockHttpProtocolHandler> handler(
@@ -78,14 +96,6 @@ class ServiceWorkerContextRequestHandlerTest : public testing::Test {
url_request_context_.set_job_factory(&url_request_job_factory_);
}
- void TearDown() override {
- version_ = nullptr;
- registration_ = nullptr;
- helper_.reset();
- }
-
- ServiceWorkerContextCore* context() const { return helper_->context(); }
-
void SetUpProvider() {
std::unique_ptr<ServiceWorkerProviderHost> host =
CreateProviderHostForServiceWorkerContext(
@@ -138,6 +148,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(true /* use_cache */);
// 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());
@@ -163,7 +201,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(true /* use_cache */);
// Give the registration a old update time and pretend
// we're installing a new version.
registration_->set_last_update_check(
@@ -191,6 +230,7 @@ TEST_F(ServiceWorkerContextRequestHandlerTest, UpdateAfter24Hours) {
}
TEST_F(ServiceWorkerContextRequestHandlerTest, UpdateForceBypassCache) {
+ SetUpServiceWorkerWithOptions(true /* use_cache */);
// 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());
@@ -213,6 +253,7 @@ TEST_F(ServiceWorkerContextRequestHandlerTest, UpdateForceBypassCache) {
TEST_F(ServiceWorkerContextRequestHandlerTest,
ServiceWorkerDataRequestAnnotation) {
+ SetUpServiceWorker();
version_->SetStatus(ServiceWorkerVersion::NEW);
// Conduct a resource fetch for the main script.
@@ -241,6 +282,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_));
@@ -258,6 +300,7 @@ TEST_F(ServiceWorkerContextRequestHandlerTest,
}
TEST_F(ServiceWorkerContextRequestHandlerTest, NewWorker) {
+ SetUpServiceWorker();
// Conduct a resource fetch for the main script.
{
base::HistogramTester histograms;
@@ -292,6 +335,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),
@@ -337,6 +381,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(),
@@ -368,6 +413,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