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

Side by Side Diff: content/browser/service_worker/service_worker_cache_unittest.cc

Issue 465463002: Initial implementation of ServiceWorkerCache. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@cache2
Patch Set: Put and Match mostly work with blobs but the first few bytes are screwy Created 6 years, 4 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 unified diff | Download patch
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "content/browser/service_worker/service_worker_cache.h"
6
7 #include "base/files/file_path.h"
8 #include "base/files/scoped_temp_dir.h"
9 #include "base/message_loop/message_loop_proxy.h"
10 #include "base/run_loop.h"
11 #include "content/browser/fileapi/chrome_blob_storage_context.h"
12 #include "content/browser/fileapi/mock_url_request_delegate.h"
13 #include "content/common/service_worker/service_worker_types.h"
14 #include "content/public/browser/browser_thread.h"
15 #include "content/public/test/test_browser_context.h"
16 #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_getter.h"
19 #include "net/url_request/url_request_job_factory_impl.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21 #include "webkit/browser/blob/blob_data_handle.h"
22 #include "webkit/browser/blob/blob_storage_context.h"
23 #include "webkit/browser/blob/blob_url_request_job_factory.h"
24 #include "webkit/common/blob/blob_data.h"
25
26 namespace content {
27
28 namespace {
29 const char kTestData[] = "Hello World";
30
31 // Returns a BlobProtocolHandler that uses |blob_storage_context|. Caller owns
32 // the memory.
33 webkit_blob::BlobProtocolHandler* CreateMockBlobProtocolHandler(
34 webkit_blob::BlobStorageContext* blob_storage_context) {
35 // The FileSystemContext and MessageLoopProxy are not actually used but a
36 // MessageLoopProxy is needed to avoid a DCHECK in BlobURLRequestJob ctor.
37 return new webkit_blob::BlobProtocolHandler(
38 blob_storage_context, NULL, base::MessageLoopProxy::current().get());
39 }
40
41 } // namespace
42
43 class ServiceWorkerCacheTest : public testing::Test {
44 protected:
45 ServiceWorkerCacheTest()
46 : browser_thread_bundle_(TestBrowserThreadBundle::IO_MAINLOOP),
47 blob_data_(new webkit_blob::BlobData("blob-id:myblob")),
48 callback_error_(ServiceWorkerCache::ErrorTypeOK) {}
49
50 virtual void SetUp() OVERRIDE {
51 ChromeBlobStorageContext* blob_storage_context =
52 ChromeBlobStorageContext::GetFor(&browser_context_);
53 // Wait for chrome_blob_storage_context to finish initializing.
54 base::RunLoop().RunUntilIdle();
55
56 url_request_job_factory_.reset(new net::URLRequestJobFactoryImpl);
57 url_request_job_factory_->SetProtocolHandler(
58 "blob", CreateMockBlobProtocolHandler(blob_storage_context->context()));
59
60 net::URLRequestContext* url_request_context =
61 browser_context_.GetRequestContext()->GetURLRequestContext();
62
63 url_request_context->set_job_factory(url_request_job_factory_.get());
64
65 CreateRequests(blob_storage_context);
66
67 if (MemoryOnly()) {
68 cache_ = ServiceWorkerCache::CreateMemoryCache(
69 "test",
70 url_request_context,
71 blob_storage_context->context()->AsWeakPtr());
72 } else {
73 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
74 cache_ = ServiceWorkerCache::CreatePersistentCache(
75 temp_dir_.path(),
76 "test",
77 url_request_context,
78 blob_storage_context->context()->AsWeakPtr());
79 }
80 CreateBackend();
81 }
82
83 void CreateRequests(ChromeBlobStorageContext* blob_storage_context) {
84 std::map<std::string, std::string> headers;
85 headers.insert(std::make_pair("a", "a"));
86 headers.insert(std::make_pair("b", "b"));
87 body_request_.reset(new ServiceWorkerFetchRequest(
88 GURL("http://example.com/body.html"), "GET", headers, GURL(""), false));
89 no_body_request_.reset(
90 new ServiceWorkerFetchRequest(GURL("http://example.com/no_body.html"),
91 "GET",
92 headers,
93 GURL(""),
94 false));
95
96 std::string expected_response;
97 for (int i = 0; i < 1; ++i) {
98 blob_data_->AppendData(kTestData);
99 expected_blob_data_ += kTestData;
100 }
101
102 blob_handle1_ =
103 blob_storage_context->context()->AddFinishedBlob(blob_data_);
104
105 body_response_.reset(
106 new ServiceWorkerResponse(GURL("http://example.com/body.html"),
107 200,
108 "OK",
109 headers,
110 blob_handle1_->uuid()));
111
112 no_body_response_.reset(new ServiceWorkerResponse(
113 GURL("http://example.com/no_body.html"), 200, "OK", headers, ""));
114 }
115
116 void CreateBackend() {
117 scoped_ptr<base::RunLoop> loop(new base::RunLoop());
118 cache_->CreateBackend(base::Bind(&ServiceWorkerCacheTest::ErrorTypeCallback,
119 base::Unretained(this),
120 base::Unretained(loop.get())));
121 loop->Run();
122 EXPECT_EQ(ServiceWorkerCache::ErrorTypeOK, callback_error_);
123 }
124
125 bool Put(ServiceWorkerFetchRequest* request,
126 ServiceWorkerResponse* response) {
127 scoped_ptr<base::RunLoop> loop(new base::RunLoop());
128
129 cache_->Put(request,
130 response,
131 base::Bind(&ServiceWorkerCacheTest::ErrorTypeCallback,
132 base::Unretained(this),
133 base::Unretained(loop.get())));
134 loop->Run();
135
136 return callback_error_ == ServiceWorkerCache::ErrorTypeOK;
137 }
138
139 bool Match(ServiceWorkerFetchRequest* request) {
140 scoped_ptr<base::RunLoop> loop(new base::RunLoop());
141
142 cache_->Match(request,
143 base::Bind(&ServiceWorkerCacheTest::ResponseAndErrorCallback,
144 base::Unretained(this),
145 base::Unretained(loop.get())));
146 loop->Run();
147
148 return callback_error_ == ServiceWorkerCache::ErrorTypeOK;
149 }
150
151 void ErrorTypeCallback(base::RunLoop* run_loop,
152 ServiceWorkerCache::ErrorType error) {
153 callback_error_ = error;
154 run_loop->Quit();
155 }
156
157 void ResponseAndErrorCallback(
158 base::RunLoop* run_loop,
159 ServiceWorkerCache::ErrorType error,
160 scoped_ptr<ServiceWorkerResponse> response,
161 scoped_ptr<webkit_blob::BlobDataHandle> response_data_handle) {
162 callback_error_ = error;
163 callback_response_ = response.Pass();
164 callback_response_data_ = response_data_handle.Pass();
165 run_loop->Quit();
166 }
167
168 void CopyBody(webkit_blob::BlobDataHandle* blob_handle, std::string* output) {
169 webkit_blob::BlobData* data = blob_handle->data();
170 std::vector<webkit_blob::BlobData::Item> items = data->items();
171 for (size_t i = 0, max = items.size(); i < max; ++i)
172 output->append(items[i].bytes(), items[i].length());
173 }
174
175 virtual bool MemoryOnly() { return false; }
176
177 TestBrowserContext browser_context_;
178 TestBrowserThreadBundle browser_thread_bundle_;
179 scoped_ptr<net::URLRequestJobFactoryImpl> url_request_job_factory_;
180
181 base::ScopedTempDir temp_dir_;
182 scoped_ptr<ServiceWorkerCache> cache_;
183
184 scoped_ptr<ServiceWorkerFetchRequest> body_request_;
185 scoped_ptr<ServiceWorkerResponse> body_response_;
186 scoped_ptr<ServiceWorkerFetchRequest> no_body_request_;
187 scoped_ptr<ServiceWorkerResponse> no_body_response_;
188 scoped_refptr<webkit_blob::BlobData> blob_data_;
189 scoped_ptr<webkit_blob::BlobDataHandle> blob_handle1_;
190 std::string expected_blob_data_;
191
192 ServiceWorkerCache::ErrorType callback_error_;
193 scoped_ptr<ServiceWorkerResponse> callback_response_;
194 scoped_ptr<webkit_blob::BlobDataHandle> callback_response_data_;
195 };
196
197 class ServiceWorkerCacheTestP : public ServiceWorkerCacheTest,
198 public testing::WithParamInterface<bool> {
199 virtual bool MemoryOnly() OVERRIDE { return !GetParam(); }
200 };
201
202 TEST_P(ServiceWorkerCacheTestP, PutNoBody) {
203 EXPECT_TRUE(Put(no_body_request_.get(), no_body_response_.get()));
204 }
205
206 TEST_P(ServiceWorkerCacheTestP, PutBody) {
207 EXPECT_TRUE(Put(body_request_.get(), body_response_.get()));
208 }
209
210 TEST_P(ServiceWorkerCacheTestP, MatchNoBody) {
211 EXPECT_TRUE(Put(no_body_request_.get(), no_body_response_.get()));
212 EXPECT_TRUE(Match(no_body_request_.get()));
213 EXPECT_EQ(200, callback_response_->status_code);
214 EXPECT_STREQ("OK", callback_response_->status_text.c_str());
215 EXPECT_STREQ("http://example.com/no_body.html",
216 callback_response_->url.spec().c_str());
217 }
218
219 TEST_P(ServiceWorkerCacheTestP, MatchBody) {
220 EXPECT_TRUE(Put(body_request_.get(), body_response_.get()));
221 EXPECT_TRUE(Match(body_request_.get()));
222 EXPECT_EQ(200, callback_response_->status_code);
223 EXPECT_STREQ("OK", callback_response_->status_text.c_str());
224 EXPECT_STREQ("http://example.com/body.html",
225 callback_response_->url.spec().c_str());
226 std::string response_body;
227 CopyBody(callback_response_data_.get(), &response_body);
228 EXPECT_STREQ(expected_blob_data_.c_str(), response_body.c_str());
229 }
230
231 INSTANTIATE_TEST_CASE_P(ServiceWorkerCacheTest,
232 ServiceWorkerCacheTestP,
233 ::testing::Values(false, true));
234
235 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698