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

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: Comments on inputs (request and response) and back to SimpleCache now that it's fixed 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 public:
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 blob_storage_context_ = blob_storage_context->context();
56
57 url_request_job_factory_.reset(new net::URLRequestJobFactoryImpl);
58 url_request_job_factory_->SetProtocolHandler(
59 "blob", CreateMockBlobProtocolHandler(blob_storage_context->context()));
60
61 net::URLRequestContext* url_request_context =
62 browser_context_.GetRequestContext()->GetURLRequestContext();
63
64 url_request_context->set_job_factory(url_request_job_factory_.get());
65
66 CreateRequests(blob_storage_context);
67
68 if (MemoryOnly()) {
69 cache_ = ServiceWorkerCache::CreateMemoryCache(
70 "test",
71 url_request_context,
72 blob_storage_context->context()->AsWeakPtr());
73 } else {
74 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
75 cache_ = ServiceWorkerCache::CreatePersistentCache(
76 temp_dir_.path(),
77 "test",
78 url_request_context,
79 blob_storage_context->context()->AsWeakPtr());
80 }
81 CreateBackend();
82 }
83
84 void CreateRequests(ChromeBlobStorageContext* blob_storage_context) {
85 std::map<std::string, std::string> headers;
86 headers.insert(std::make_pair("a", "a"));
87 headers.insert(std::make_pair("b", "b"));
88 body_request_.reset(new ServiceWorkerFetchRequest(
89 GURL("http://example.com/body.html"), "GET", headers, GURL(""), false));
90 no_body_request_.reset(
91 new ServiceWorkerFetchRequest(GURL("http://example.com/no_body.html"),
92 "GET",
93 headers,
94 GURL(""),
95 false));
96
97 std::string expected_response;
98 for (int i = 0; i < 100; ++i)
99 expected_blob_data_ += kTestData;
100
101 blob_data_->AppendData(expected_blob_data_);
102
103 blob_handle1_ =
104 blob_storage_context->context()->AddFinishedBlob(blob_data_);
105
106 body_response_.reset(
107 new ServiceWorkerResponse(GURL("http://example.com/body.html"),
108 200,
109 "OK",
110 headers,
111 blob_handle1_->uuid()));
112
113 no_body_response_.reset(new ServiceWorkerResponse(
114 GURL("http://example.com/no_body.html"), 200, "OK", headers, ""));
115 }
116
117 void CreateBackend() {
118 scoped_ptr<base::RunLoop> loop(new base::RunLoop());
119 cache_->CreateBackend(base::Bind(&ServiceWorkerCacheTest::ErrorTypeCallback,
120 base::Unretained(this),
121 base::Unretained(loop.get())));
122 loop->Run();
123 EXPECT_EQ(ServiceWorkerCache::ErrorTypeOK, callback_error_);
124 }
125
126 bool Put(ServiceWorkerFetchRequest* request,
127 ServiceWorkerResponse* response) {
128 scoped_ptr<base::RunLoop> loop(new base::RunLoop());
129
130 cache_->Put(request,
131 response,
132 base::Bind(&ServiceWorkerCacheTest::ErrorTypeCallback,
133 base::Unretained(this),
134 base::Unretained(loop.get())));
135 // TODO(jkarlin): These functions should use base::RunLoop().RunUntilIdle()
136 // once the cache uses a passed in MessageLoopProxy instead of the CACHE
137 // thread.
138 loop->Run();
139
140 return callback_error_ == ServiceWorkerCache::ErrorTypeOK;
141 }
142
143 bool Match(ServiceWorkerFetchRequest* request) {
144 scoped_ptr<base::RunLoop> loop(new base::RunLoop());
145
146 cache_->Match(request,
147 base::Bind(&ServiceWorkerCacheTest::ResponseAndErrorCallback,
148 base::Unretained(this),
149 base::Unretained(loop.get())));
150 loop->Run();
151
152 return callback_error_ == ServiceWorkerCache::ErrorTypeOK;
153 }
154
155 bool Delete(ServiceWorkerFetchRequest* request) {
156 scoped_ptr<base::RunLoop> loop(new base::RunLoop());
157
158 cache_->Delete(request,
159 base::Bind(&ServiceWorkerCacheTest::ErrorTypeCallback,
160 base::Unretained(this),
161 base::Unretained(loop.get())));
162 loop->Run();
163
164 return callback_error_ == ServiceWorkerCache::ErrorTypeOK;
165 }
166
167 void ErrorTypeCallback(base::RunLoop* run_loop,
168 ServiceWorkerCache::ErrorType error) {
169 callback_error_ = error;
170 run_loop->Quit();
171 }
172
173 void ResponseAndErrorCallback(
174 base::RunLoop* run_loop,
175 ServiceWorkerCache::ErrorType error,
176 scoped_ptr<ServiceWorkerResponse> response,
177 scoped_ptr<webkit_blob::BlobDataHandle> body_handle) {
178 callback_error_ = error;
179 callback_response_ = response.Pass();
180
181 if (error == ServiceWorkerCache::ErrorTypeOK &&
182 !callback_response_->blob_uuid.empty()) {
183 callback_response_data_ = body_handle.Pass();
184 }
185
186 run_loop->Quit();
187 }
188
189 void CopyBody(webkit_blob::BlobDataHandle* blob_handle, std::string* output) {
190 webkit_blob::BlobData* data = blob_handle->data();
191 std::vector<webkit_blob::BlobData::Item> items = data->items();
192 for (size_t i = 0, max = items.size(); i < max; ++i)
193 output->append(items[i].bytes(), items[i].length());
194 }
195
196 virtual bool MemoryOnly() { return false; }
197
198 protected:
199 TestBrowserContext browser_context_;
200 TestBrowserThreadBundle browser_thread_bundle_;
201 scoped_ptr<net::URLRequestJobFactoryImpl> url_request_job_factory_;
202 webkit_blob::BlobStorageContext* blob_storage_context_;
203
204 base::ScopedTempDir temp_dir_;
205 scoped_ptr<ServiceWorkerCache> cache_;
206
207 scoped_ptr<ServiceWorkerFetchRequest> body_request_;
208 scoped_ptr<ServiceWorkerResponse> body_response_;
209 scoped_ptr<ServiceWorkerFetchRequest> no_body_request_;
210 scoped_ptr<ServiceWorkerResponse> no_body_response_;
211 scoped_refptr<webkit_blob::BlobData> blob_data_;
michaeln 2014/08/25 21:16:48 i was looking around for where else this is used a
jkarlin 2014/08/25 23:51:46 Done.
212 scoped_ptr<webkit_blob::BlobDataHandle> blob_handle1_;
michaeln 2014/08/25 21:16:48 nit: since this has a trailing 1 on it, i was wond
jkarlin 2014/08/25 23:51:46 Done.
213 std::string expected_blob_data_;
214
215 ServiceWorkerCache::ErrorType callback_error_;
216 scoped_ptr<ServiceWorkerResponse> callback_response_;
217 scoped_ptr<webkit_blob::BlobDataHandle> callback_response_data_;
218 };
219
220 class ServiceWorkerCacheTestP : public ServiceWorkerCacheTest,
221 public testing::WithParamInterface<bool> {
222 virtual bool MemoryOnly() OVERRIDE { return !GetParam(); }
223 };
224
225 TEST_P(ServiceWorkerCacheTestP, PutNoBody) {
226 EXPECT_TRUE(Put(no_body_request_.get(), no_body_response_.get()));
michaeln 2014/08/25 21:16:48 nit: if Put returned ServiceWorkerCache::ErrorType
jkarlin 2014/08/25 23:51:46 Acknowledged.
227 }
228
229 TEST_P(ServiceWorkerCacheTestP, PutBody) {
230 EXPECT_TRUE(Put(body_request_.get(), body_response_.get()));
231 }
232
233 TEST_P(ServiceWorkerCacheTestP, PutBodyDropBlobRef) {
234 scoped_ptr<base::RunLoop> loop(new base::RunLoop());
235 cache_->Put(body_request_.get(),
236 body_response_.get(),
237 base::Bind(&ServiceWorkerCacheTestP::ErrorTypeCallback,
238 base::Unretained(this),
239 base::Unretained(loop.get())));
240 // The handle should be held by the cache now so the deref here should be
241 // okay.
242 blob_handle1_.reset();
243 loop->Run();
244
245 EXPECT_EQ(ServiceWorkerCache::ErrorTypeOK, callback_error_);
246 }
247
248 TEST_P(ServiceWorkerCacheTestP, DeleteNoBody) {
249 EXPECT_TRUE(Put(no_body_request_.get(), no_body_response_.get()));
250 EXPECT_TRUE(Match(no_body_request_.get()));
251 EXPECT_TRUE(Delete(no_body_request_.get()));
252 EXPECT_FALSE(Match(no_body_request_.get()));
253 EXPECT_FALSE(Delete(no_body_request_.get()));
254 EXPECT_TRUE(Put(no_body_request_.get(), no_body_response_.get()));
255 EXPECT_TRUE(Match(no_body_request_.get()));
256 EXPECT_TRUE(Delete(no_body_request_.get()));
257 }
258
259 TEST_P(ServiceWorkerCacheTestP, DeleteBody) {
260 EXPECT_TRUE(Put(body_request_.get(), body_response_.get()));
261 EXPECT_TRUE(Match(body_request_.get()));
262 EXPECT_TRUE(Delete(body_request_.get()));
263 EXPECT_FALSE(Match(body_request_.get()));
264 EXPECT_FALSE(Delete(body_request_.get()));
265 EXPECT_TRUE(Put(body_request_.get(), body_response_.get()));
266 EXPECT_TRUE(Match(body_request_.get()));
267 EXPECT_TRUE(Delete(body_request_.get()));
268 }
269
270 TEST_P(ServiceWorkerCacheTestP, MatchNoBody) {
271 EXPECT_TRUE(Put(no_body_request_.get(), no_body_response_.get()));
272 EXPECT_TRUE(Match(no_body_request_.get()));
273 EXPECT_EQ(200, callback_response_->status_code);
274 EXPECT_STREQ("OK", callback_response_->status_text.c_str());
275 EXPECT_STREQ("http://example.com/no_body.html",
276 callback_response_->url.spec().c_str());
277 }
278
279 TEST_P(ServiceWorkerCacheTestP, MatchBody) {
280 EXPECT_TRUE(Put(body_request_.get(), body_response_.get()));
281 EXPECT_TRUE(Match(body_request_.get()));
282 EXPECT_EQ(200, callback_response_->status_code);
283 EXPECT_STREQ("OK", callback_response_->status_text.c_str());
284 EXPECT_STREQ("http://example.com/body.html",
285 callback_response_->url.spec().c_str());
286 std::string response_body;
287 CopyBody(callback_response_data_.get(), &response_body);
288 EXPECT_STREQ(expected_blob_data_.c_str(), response_body.c_str());
289 }
290
291 TEST_P(ServiceWorkerCacheTestP, QuickStressNoBody) {
292 for (int i = 0; i < 100; ++i) {
293 EXPECT_FALSE(Match(no_body_request_.get()));
294 EXPECT_TRUE(Put(no_body_request_.get(), no_body_response_.get()));
295 EXPECT_TRUE(Match(no_body_request_.get()));
296 EXPECT_TRUE(Delete(no_body_request_.get()));
297 }
298 }
299
300 TEST_P(ServiceWorkerCacheTestP, QuickStressBody) {
301 for (int i = 0; i < 100; ++i) {
302 ASSERT_FALSE(Match(body_request_.get()));
303 ASSERT_TRUE(Put(body_request_.get(), body_response_.get()));
304 ASSERT_TRUE(Match(body_request_.get()));
305 ASSERT_TRUE(Delete(body_request_.get()));
306 }
307 }
308
309 INSTANTIATE_TEST_CASE_P(ServiceWorkerCacheTest,
310 ServiceWorkerCacheTestP,
311 ::testing::Values(false, true));
312
313 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698