OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/browser/service_worker/service_worker_read_from_cache_job.h" | 5 #include "content/browser/service_worker/service_worker_read_from_cache_job.h" |
6 | 6 |
7 #include <stdint.h> | 7 #include <stdint.h> |
8 | 8 |
9 #include <memory> | 9 #include <memory> |
10 | 10 |
11 #include "base/macros.h" | 11 #include "base/macros.h" |
12 #include "base/memory/ptr_util.h" | 12 #include "base/memory/ptr_util.h" |
13 #include "base/run_loop.h" | 13 #include "base/run_loop.h" |
14 #include "content/browser/fileapi/mock_url_request_delegate.h" | 14 #include "content/browser/fileapi/mock_url_request_delegate.h" |
15 #include "content/browser/service_worker/embedded_worker_test_helper.h" | 15 #include "content/browser/service_worker/embedded_worker_test_helper.h" |
16 #include "content/browser/service_worker/service_worker_context_core.h" | 16 #include "content/browser/service_worker/service_worker_context_core.h" |
17 #include "content/browser/service_worker/service_worker_registration.h" | 17 #include "content/browser/service_worker/service_worker_registration.h" |
18 #include "content/browser/service_worker/service_worker_version.h" | 18 #include "content/browser/service_worker/service_worker_version.h" |
19 #include "content/public/test/test_browser_thread_bundle.h" | 19 #include "content/public/test/test_browser_thread_bundle.h" |
20 #include "net/base/io_buffer.h" | 20 #include "net/base/io_buffer.h" |
21 #include "net/base/test_completion_callback.h" | 21 #include "net/base/test_completion_callback.h" |
22 #include "net/http/http_response_headers.h" | 22 #include "net/http/http_response_headers.h" |
23 #include "net/url_request/url_request_context.h" | 23 #include "net/url_request/url_request_context.h" |
| 24 #include "net/url_request/url_request_intercepting_job_factory.h" |
24 #include "net/url_request/url_request_job_factory_impl.h" | 25 #include "net/url_request/url_request_job_factory_impl.h" |
25 #include "net/url_request/url_request_status.h" | 26 #include "net/url_request/url_request_status.h" |
26 #include "net/url_request/url_request_test_util.h" | 27 #include "net/url_request/url_request_test_util.h" |
27 #include "testing/gtest/include/gtest/gtest.h" | 28 #include "testing/gtest/include/gtest/gtest.h" |
28 | 29 |
29 namespace content { | 30 namespace content { |
30 | 31 |
31 namespace { | 32 namespace { |
32 | 33 |
33 const int64_t kRegistrationId = 1; | 34 const int64_t kRegistrationId = 1; |
(...skipping 23 matching lines...) Expand all Loading... |
57 class ServiceWorkerReadFromCacheJobTest : public testing::Test { | 58 class ServiceWorkerReadFromCacheJobTest : public testing::Test { |
58 public: | 59 public: |
59 ServiceWorkerReadFromCacheJobTest() | 60 ServiceWorkerReadFromCacheJobTest() |
60 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), | 61 : thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), |
61 main_script_(kMainScriptResourceId, | 62 main_script_(kMainScriptResourceId, |
62 GURL("http://example.com/main.js"), | 63 GURL("http://example.com/main.js"), |
63 kResourceSize), | 64 kResourceSize), |
64 imported_script_(kImportedScriptResourceId, | 65 imported_script_(kImportedScriptResourceId, |
65 GURL("http://example.com/imported.js"), | 66 GURL("http://example.com/imported.js"), |
66 kResourceSize), | 67 kResourceSize), |
67 test_job_interceptor_(nullptr) {} | 68 test_job_interceptor_(nullptr), |
| 69 test_job_factory_(nullptr) {} |
68 ~ServiceWorkerReadFromCacheJobTest() override {} | 70 ~ServiceWorkerReadFromCacheJobTest() override {} |
69 | 71 |
70 void SetUp() override { | 72 void SetUp() override { |
71 helper_.reset(new EmbeddedWorkerTestHelper(base::FilePath())); | 73 helper_.reset(new EmbeddedWorkerTestHelper(base::FilePath())); |
72 InitializeStorage(); | 74 InitializeStorage(); |
73 | 75 |
74 url_request_context_.reset(new net::TestURLRequestContext(true)); | 76 url_request_context_.reset(new net::TestURLRequestContext(true)); |
75 | 77 |
76 // The |test_job_factory_| takes ownership of the interceptor. | 78 // The |test_job_factory_| takes ownership of the interceptor. |
77 test_job_interceptor_ = new net::TestJobInterceptor(); | 79 test_job_interceptor_ = new net::TestJobInterceptor(); |
78 EXPECT_TRUE(test_job_factory_.SetProtocolHandler( | 80 test_job_factory_.reset(new net::URLRequestInterceptingJobFactory( |
79 url::kHttpScheme, base::WrapUnique(test_job_interceptor_))); | 81 net::URLRequestJobFactoryImpl::CreateWithHttpProtocolHandlers(), |
80 url_request_context_->set_job_factory(&test_job_factory_); | 82 base::WrapUnique(test_job_interceptor_))); |
| 83 url_request_context_->set_job_factory(test_job_factory_.get()); |
81 | 84 |
82 url_request_context_->Init(); | 85 url_request_context_->Init(); |
83 } | 86 } |
84 | 87 |
85 void InitializeStorage() { | 88 void InitializeStorage() { |
86 base::RunLoop run_loop; | 89 base::RunLoop run_loop; |
87 context()->storage()->LazyInitialize(run_loop.QuitClosure()); | 90 context()->storage()->LazyInitialize(run_loop.QuitClosure()); |
88 run_loop.Run(); | 91 run_loop.Run(); |
89 | 92 |
90 // Populate a registration in the storage. | 93 // Populate a registration in the storage. |
(...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
175 TestBrowserThreadBundle thread_bundle_; | 178 TestBrowserThreadBundle thread_bundle_; |
176 std::unique_ptr<EmbeddedWorkerTestHelper> helper_; | 179 std::unique_ptr<EmbeddedWorkerTestHelper> helper_; |
177 | 180 |
178 scoped_refptr<ServiceWorkerRegistration> registration_; | 181 scoped_refptr<ServiceWorkerRegistration> registration_; |
179 scoped_refptr<ServiceWorkerVersion> version_; | 182 scoped_refptr<ServiceWorkerVersion> version_; |
180 ServiceWorkerDatabase::ResourceRecord main_script_; | 183 ServiceWorkerDatabase::ResourceRecord main_script_; |
181 ServiceWorkerDatabase::ResourceRecord imported_script_; | 184 ServiceWorkerDatabase::ResourceRecord imported_script_; |
182 | 185 |
183 // |test_job_interceptor_| is owned by |test_job_factory_|. | 186 // |test_job_interceptor_| is owned by |test_job_factory_|. |
184 net::TestJobInterceptor* test_job_interceptor_; | 187 net::TestJobInterceptor* test_job_interceptor_; |
185 net::URLRequestJobFactoryImpl test_job_factory_; | 188 std::unique_ptr<net::URLRequestJobFactory> test_job_factory_; |
186 | 189 |
187 std::unique_ptr<net::TestURLRequestContext> url_request_context_; | 190 std::unique_ptr<net::TestURLRequestContext> url_request_context_; |
188 MockURLRequestDelegate delegate_; | 191 MockURLRequestDelegate delegate_; |
189 }; | 192 }; |
190 | 193 |
191 TEST_F(ServiceWorkerReadFromCacheJobTest, ReadMainScript) { | 194 TEST_F(ServiceWorkerReadFromCacheJobTest, ReadMainScript) { |
192 // Read the main script from the diskcache. | 195 // Read the main script from the diskcache. |
193 std::unique_ptr<net::URLRequest> request = | 196 std::unique_ptr<net::URLRequest> request = |
194 url_request_context_->CreateRequest(main_script_.url, | 197 url_request_context_->CreateRequest(main_script_.url, |
195 net::DEFAULT_PRIORITY, &delegate_); | 198 net::DEFAULT_PRIORITY, &delegate_); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
243 EXPECT_EQ(net::ERR_CACHE_MISS, request->status().error()); | 246 EXPECT_EQ(net::ERR_CACHE_MISS, request->status().error()); |
244 EXPECT_EQ(SERVICE_WORKER_ERROR_DISK_CACHE, | 247 EXPECT_EQ(SERVICE_WORKER_ERROR_DISK_CACHE, |
245 DeduceStartWorkerFailureReason(SERVICE_WORKER_OK)); | 248 DeduceStartWorkerFailureReason(SERVICE_WORKER_OK)); |
246 | 249 |
247 // The version should be doomed by the job. | 250 // The version should be doomed by the job. |
248 EXPECT_EQ(ServiceWorkerVersion::REDUNDANT, version_->status()); | 251 EXPECT_EQ(ServiceWorkerVersion::REDUNDANT, version_->status()); |
249 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, FindRegistration()); | 252 EXPECT_EQ(SERVICE_WORKER_ERROR_NOT_FOUND, FindRegistration()); |
250 } | 253 } |
251 | 254 |
252 } // namespace content | 255 } // namespace content |
OLD | NEW |