OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_cache.h" | 5 #include "content/browser/service_worker/service_worker_cache.h" |
6 | 6 |
7 #include "base/files/file_path.h" | 7 #include "base/files/file_path.h" |
8 #include "base/files/scoped_temp_dir.h" | 8 #include "base/files/scoped_temp_dir.h" |
9 #include "base/message_loop/message_loop_proxy.h" | 9 #include "base/message_loop/message_loop_proxy.h" |
10 #include "base/run_loop.h" | 10 #include "base/run_loop.h" |
11 #include "content/browser/fileapi/chrome_blob_storage_context.h" | 11 #include "content/browser/fileapi/chrome_blob_storage_context.h" |
12 #include "content/browser/fileapi/mock_url_request_delegate.h" | 12 #include "content/browser/fileapi/mock_url_request_delegate.h" |
| 13 #include "content/browser/quota/mock_quota_manager_proxy.h" |
13 #include "content/common/service_worker/service_worker_types.h" | 14 #include "content/common/service_worker/service_worker_types.h" |
14 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
15 #include "content/public/test/test_browser_context.h" | 16 #include "content/public/test/test_browser_context.h" |
16 #include "content/public/test/test_browser_thread_bundle.h" | 17 #include "content/public/test/test_browser_thread_bundle.h" |
17 #include "net/url_request/url_request_context.h" | 18 #include "net/url_request/url_request_context.h" |
18 #include "net/url_request/url_request_context_getter.h" | 19 #include "net/url_request/url_request_context_getter.h" |
19 #include "net/url_request/url_request_job_factory_impl.h" | 20 #include "net/url_request/url_request_job_factory_impl.h" |
20 #include "storage/browser/blob/blob_data_handle.h" | 21 #include "storage/browser/blob/blob_data_handle.h" |
21 #include "storage/browser/blob/blob_storage_context.h" | 22 #include "storage/browser/blob/blob_storage_context.h" |
22 #include "storage/browser/blob/blob_url_request_job_factory.h" | 23 #include "storage/browser/blob/blob_url_request_job_factory.h" |
| 24 #include "storage/browser/quota/quota_manager_proxy.h" |
23 #include "storage/common/blob/blob_data.h" | 25 #include "storage/common/blob/blob_data.h" |
24 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
25 | 27 |
26 namespace content { | 28 namespace content { |
27 | 29 |
28 namespace { | 30 namespace { |
29 const char kTestData[] = "Hello World"; | 31 const char kTestData[] = "Hello World"; |
30 | 32 |
31 // Returns a BlobProtocolHandler that uses |blob_storage_context|. Caller owns | 33 // Returns a BlobProtocolHandler that uses |blob_storage_context|. Caller owns |
32 // the memory. | 34 // the memory. |
(...skipping 13 matching lines...) Expand all Loading... |
46 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), | 48 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP), |
47 callback_error_(ServiceWorkerCache::ErrorTypeOK) {} | 49 callback_error_(ServiceWorkerCache::ErrorTypeOK) {} |
48 | 50 |
49 virtual void SetUp() override { | 51 virtual void SetUp() override { |
50 ChromeBlobStorageContext* blob_storage_context = | 52 ChromeBlobStorageContext* blob_storage_context = |
51 ChromeBlobStorageContext::GetFor(&browser_context_); | 53 ChromeBlobStorageContext::GetFor(&browser_context_); |
52 // Wait for chrome_blob_storage_context to finish initializing. | 54 // Wait for chrome_blob_storage_context to finish initializing. |
53 base::RunLoop().RunUntilIdle(); | 55 base::RunLoop().RunUntilIdle(); |
54 blob_storage_context_ = blob_storage_context->context(); | 56 blob_storage_context_ = blob_storage_context->context(); |
55 | 57 |
| 58 quota_manager_proxy_ = new MockQuotaManagerProxy( |
| 59 nullptr, base::MessageLoopProxy::current().get()); |
| 60 |
56 url_request_job_factory_.reset(new net::URLRequestJobFactoryImpl); | 61 url_request_job_factory_.reset(new net::URLRequestJobFactoryImpl); |
57 url_request_job_factory_->SetProtocolHandler( | 62 url_request_job_factory_->SetProtocolHandler( |
58 "blob", CreateMockBlobProtocolHandler(blob_storage_context->context())); | 63 "blob", CreateMockBlobProtocolHandler(blob_storage_context->context())); |
59 | 64 |
60 net::URLRequestContext* url_request_context = | 65 net::URLRequestContext* url_request_context = |
61 browser_context_.GetRequestContext()->GetURLRequestContext(); | 66 browser_context_.GetRequestContext()->GetURLRequestContext(); |
62 | 67 |
63 url_request_context->set_job_factory(url_request_job_factory_.get()); | 68 url_request_context->set_job_factory(url_request_job_factory_.get()); |
64 | 69 |
65 CreateRequests(blob_storage_context); | 70 CreateRequests(blob_storage_context); |
66 | 71 |
67 if (MemoryOnly()) { | 72 if (MemoryOnly()) { |
68 cache_ = ServiceWorkerCache::CreateMemoryCache( | 73 cache_ = ServiceWorkerCache::CreateMemoryCache( |
| 74 GURL("http://example.com"), |
69 url_request_context, | 75 url_request_context, |
| 76 quota_manager_proxy_, |
70 blob_storage_context->context()->AsWeakPtr()); | 77 blob_storage_context->context()->AsWeakPtr()); |
71 } else { | 78 } else { |
72 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); | 79 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir()); |
73 cache_ = ServiceWorkerCache::CreatePersistentCache( | 80 cache_ = ServiceWorkerCache::CreatePersistentCache( |
| 81 GURL("http://example.com"), |
74 temp_dir_.path(), | 82 temp_dir_.path(), |
75 url_request_context, | 83 url_request_context, |
| 84 quota_manager_proxy_, |
76 blob_storage_context->context()->AsWeakPtr()); | 85 blob_storage_context->context()->AsWeakPtr()); |
77 } | 86 } |
78 } | 87 } |
79 | 88 |
80 virtual void TearDown() override { | 89 virtual void TearDown() override { |
| 90 quota_manager_proxy_->SimulateQuotaManagerDestroyed(); |
81 base::RunLoop().RunUntilIdle(); | 91 base::RunLoop().RunUntilIdle(); |
82 } | 92 } |
83 | 93 |
84 void CreateRequests(ChromeBlobStorageContext* blob_storage_context) { | 94 void CreateRequests(ChromeBlobStorageContext* blob_storage_context) { |
85 ServiceWorkerHeaderMap headers; | 95 ServiceWorkerHeaderMap headers; |
86 headers.insert(std::make_pair("a", "a")); | 96 headers.insert(std::make_pair("a", "a")); |
87 headers.insert(std::make_pair("b", "b")); | 97 headers.insert(std::make_pair("b", "b")); |
88 body_request_ = ServiceWorkerFetchRequest( | 98 body_request_ = ServiceWorkerFetchRequest( |
89 GURL("http://example.com/body.html"), "GET", headers, GURL(""), false); | 99 GURL("http://example.com/body.html"), "GET", headers, GURL(""), false); |
90 no_body_request_ = | 100 no_body_request_ = |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
259 EXPECT_TRUE(Delete(body_request_)); | 269 EXPECT_TRUE(Delete(body_request_)); |
260 return response_type == callback_response_->response_type; | 270 return response_type == callback_response_->response_type; |
261 } | 271 } |
262 | 272 |
263 virtual bool MemoryOnly() { return false; } | 273 virtual bool MemoryOnly() { return false; } |
264 | 274 |
265 protected: | 275 protected: |
266 TestBrowserContext browser_context_; | 276 TestBrowserContext browser_context_; |
267 TestBrowserThreadBundle browser_thread_bundle_; | 277 TestBrowserThreadBundle browser_thread_bundle_; |
268 scoped_ptr<net::URLRequestJobFactoryImpl> url_request_job_factory_; | 278 scoped_ptr<net::URLRequestJobFactoryImpl> url_request_job_factory_; |
| 279 scoped_refptr<MockQuotaManagerProxy> quota_manager_proxy_; |
269 storage::BlobStorageContext* blob_storage_context_; | 280 storage::BlobStorageContext* blob_storage_context_; |
270 | 281 |
271 base::ScopedTempDir temp_dir_; | 282 base::ScopedTempDir temp_dir_; |
272 scoped_refptr<ServiceWorkerCache> cache_; | 283 scoped_refptr<ServiceWorkerCache> cache_; |
273 | 284 |
274 ServiceWorkerFetchRequest body_request_; | 285 ServiceWorkerFetchRequest body_request_; |
275 ServiceWorkerResponse body_response_; | 286 ServiceWorkerResponse body_response_; |
276 ServiceWorkerFetchRequest no_body_request_; | 287 ServiceWorkerFetchRequest no_body_request_; |
277 ServiceWorkerResponse no_body_response_; | 288 ServiceWorkerResponse no_body_response_; |
278 scoped_ptr<storage::BlobDataHandle> blob_handle_; | 289 scoped_ptr<storage::BlobDataHandle> blob_handle_; |
(...skipping 244 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
523 ServiceWorkerFetchRequest request(GURL("http://www.example.com"), | 534 ServiceWorkerFetchRequest request(GURL("http://www.example.com"), |
524 "GET", | 535 "GET", |
525 ServiceWorkerHeaderMap(), | 536 ServiceWorkerHeaderMap(), |
526 GURL(""), | 537 GURL(""), |
527 false); | 538 false); |
528 request.headers["content-type"] = "foo"; | 539 request.headers["content-type"] = "foo"; |
529 request.headers["Content-Type"] = "bar"; | 540 request.headers["Content-Type"] = "bar"; |
530 EXPECT_EQ("bar", request.headers["content-type"]); | 541 EXPECT_EQ("bar", request.headers["content-type"]); |
531 } | 542 } |
532 | 543 |
| 544 TEST_P(ServiceWorkerCacheTestP, QuotaManagerModified) { |
| 545 EXPECT_EQ(0, quota_manager_proxy_->notify_storage_modified_count()); |
| 546 |
| 547 EXPECT_TRUE(Put(no_body_request_, no_body_response_)); |
| 548 EXPECT_EQ(1, quota_manager_proxy_->notify_storage_modified_count()); |
| 549 EXPECT_LT(0, quota_manager_proxy_->last_notified_delta()); |
| 550 int64 sum_delta = quota_manager_proxy_->last_notified_delta(); |
| 551 |
| 552 EXPECT_TRUE(Put(body_request_, body_response_)); |
| 553 EXPECT_EQ(2, quota_manager_proxy_->notify_storage_modified_count()); |
| 554 EXPECT_LT(sum_delta, quota_manager_proxy_->last_notified_delta()); |
| 555 sum_delta += quota_manager_proxy_->last_notified_delta(); |
| 556 |
| 557 EXPECT_TRUE(Delete(body_request_)); |
| 558 EXPECT_EQ(3, quota_manager_proxy_->notify_storage_modified_count()); |
| 559 sum_delta += quota_manager_proxy_->last_notified_delta(); |
| 560 |
| 561 EXPECT_TRUE(Delete(no_body_request_)); |
| 562 EXPECT_EQ(4, quota_manager_proxy_->notify_storage_modified_count()); |
| 563 sum_delta += quota_manager_proxy_->last_notified_delta(); |
| 564 |
| 565 EXPECT_EQ(0, sum_delta); |
| 566 } |
| 567 |
533 INSTANTIATE_TEST_CASE_P(ServiceWorkerCacheTest, | 568 INSTANTIATE_TEST_CASE_P(ServiceWorkerCacheTest, |
534 ServiceWorkerCacheTestP, | 569 ServiceWorkerCacheTestP, |
535 ::testing::Values(false, true)); | 570 ::testing::Values(false, true)); |
536 | 571 |
537 } // namespace content | 572 } // namespace content |
OLD | NEW |