Index: content/browser/service_worker/service_worker_read_from_cache_job_unittest.cc |
diff --git a/content/browser/service_worker/service_worker_read_from_cache_job_unittest.cc b/content/browser/service_worker/service_worker_read_from_cache_job_unittest.cc |
index aa7508b7520af5f3aee65090be767c6e8e69906c..6cb1cbd28b4ffa268539e3e25544780bebb50aec 100644 |
--- a/content/browser/service_worker/service_worker_read_from_cache_job_unittest.cc |
+++ b/content/browser/service_worker/service_worker_read_from_cache_job_unittest.cc |
@@ -7,6 +7,7 @@ |
#include <stdint.h> |
#include "base/macros.h" |
+#include "base/memory/scoped_ptr.h" |
#include "base/run_loop.h" |
#include "content/browser/fileapi/mock_url_request_delegate.h" |
#include "content/browser/service_worker/embedded_worker_test_helper.h" |
@@ -20,6 +21,7 @@ |
#include "net/url_request/url_request_context.h" |
#include "net/url_request/url_request_job_factory_impl.h" |
#include "net/url_request/url_request_status.h" |
+#include "net/url_request/url_request_test_util.h" |
#include "testing/gtest/include/gtest/gtest.h" |
namespace content { |
@@ -59,16 +61,23 @@ class ServiceWorkerReadFromCacheJobTest : public testing::Test { |
kResourceSize), |
imported_script_(kImportedScriptResourceId, |
GURL("http://example.com/imported.js"), |
- kResourceSize) {} |
+ kResourceSize), |
+ test_job_interceptor_(nullptr) {} |
~ServiceWorkerReadFromCacheJobTest() override {} |
void SetUp() override { |
helper_.reset(new EmbeddedWorkerTestHelper(base::FilePath())); |
InitializeStorage(); |
- url_request_context_.reset(new net::URLRequestContext); |
- url_request_job_factory_.reset(new net::URLRequestJobFactoryImpl); |
- url_request_context_->set_job_factory(url_request_job_factory_.get()); |
+ url_request_context_.reset(new net::TestURLRequestContext(true)); |
+ |
+ // The |test_job_factory_| takes ownership of the interceptor. |
+ test_job_interceptor_ = new net::TestJobInterceptor(); |
+ EXPECT_TRUE(test_job_factory_.SetProtocolHandler( |
+ url::kHttpScheme, make_scoped_ptr(test_job_interceptor_))); |
+ url_request_context_->set_job_factory(&test_job_factory_); |
+ |
+ url_request_context_->Init(); |
} |
void InitializeStorage() { |
@@ -145,9 +154,8 @@ class ServiceWorkerReadFromCacheJobTest : public testing::Test { |
return status; |
} |
- void StartAndWaitForJob( |
- const scoped_ptr<ServiceWorkerReadFromCacheJob>& job) { |
- job->Start(); |
+ void StartAndWaitForRequest(net::URLRequest* request) { |
+ request->Start(); |
// MockURLRequestDelegate quits the loop when the request is completed. |
base::RunLoop().RunUntilIdle(); |
} |
@@ -168,8 +176,11 @@ class ServiceWorkerReadFromCacheJobTest : public testing::Test { |
ServiceWorkerDatabase::ResourceRecord main_script_; |
ServiceWorkerDatabase::ResourceRecord imported_script_; |
- scoped_ptr<net::URLRequestContext> url_request_context_; |
- scoped_ptr<net::URLRequestJobFactoryImpl> url_request_job_factory_; |
+ // |test_job_interceptor_| is owned by |test_job_factory_|. |
+ net::TestJobInterceptor* test_job_interceptor_; |
+ net::URLRequestJobFactoryImpl test_job_factory_; |
+ |
+ scoped_ptr<net::TestURLRequestContext> url_request_context_; |
MockURLRequestDelegate delegate_; |
}; |
@@ -177,12 +188,12 @@ TEST_F(ServiceWorkerReadFromCacheJobTest, ReadMainScript) { |
// Read the main script from the diskcache. |
scoped_ptr<net::URLRequest> request = url_request_context_->CreateRequest( |
main_script_.url, net::DEFAULT_PRIORITY, &delegate_); |
- scoped_ptr<ServiceWorkerReadFromCacheJob> job( |
- new ServiceWorkerReadFromCacheJob( |
+ test_job_interceptor_->set_main_intercept_job( |
+ make_scoped_ptr(new ServiceWorkerReadFromCacheJob( |
request.get(), nullptr /* NetworkDelegate */, |
RESOURCE_TYPE_SERVICE_WORKER, context()->AsWeakPtr(), version_, |
- main_script_.resource_id)); |
- StartAndWaitForJob(job); |
+ main_script_.resource_id))); |
+ StartAndWaitForRequest(request.get()); |
EXPECT_EQ(net::URLRequestStatus::SUCCESS, request->status().status()); |
EXPECT_EQ(0, request->status().error()); |
@@ -194,11 +205,11 @@ TEST_F(ServiceWorkerReadFromCacheJobTest, ReadImportedScript) { |
// Read the imported script from the diskcache. |
scoped_ptr<net::URLRequest> request = url_request_context_->CreateRequest( |
imported_script_.url, net::DEFAULT_PRIORITY, &delegate_); |
- scoped_ptr<ServiceWorkerReadFromCacheJob> job( |
- new ServiceWorkerReadFromCacheJob( |
+ test_job_interceptor_->set_main_intercept_job( |
+ make_scoped_ptr(new ServiceWorkerReadFromCacheJob( |
request.get(), nullptr /* NetworkDelegate */, RESOURCE_TYPE_SCRIPT, |
- context()->AsWeakPtr(), version_, imported_script_.resource_id)); |
- StartAndWaitForJob(job); |
+ context()->AsWeakPtr(), version_, imported_script_.resource_id))); |
+ StartAndWaitForRequest(request.get()); |
EXPECT_EQ(net::URLRequestStatus::SUCCESS, request->status().status()); |
EXPECT_EQ(0, request->status().error()); |
@@ -214,12 +225,12 @@ TEST_F(ServiceWorkerReadFromCacheJobTest, ResourceNotFound) { |
GURL("http://example.com/nonexistent"), net::DEFAULT_PRIORITY, |
&delegate_); |
const int64_t kNonexistentResourceId = 100; |
- scoped_ptr<ServiceWorkerReadFromCacheJob> job( |
- new ServiceWorkerReadFromCacheJob( |
+ test_job_interceptor_->set_main_intercept_job( |
+ make_scoped_ptr(new ServiceWorkerReadFromCacheJob( |
request.get(), nullptr /* NetworkDelegate */, |
RESOURCE_TYPE_SERVICE_WORKER, context()->AsWeakPtr(), version_, |
- kNonexistentResourceId)); |
- StartAndWaitForJob(job); |
+ kNonexistentResourceId))); |
+ StartAndWaitForRequest(request.get()); |
EXPECT_EQ(net::URLRequestStatus::FAILED, request->status().status()); |
EXPECT_EQ(net::ERR_CACHE_MISS, request->status().error()); |