OLD | NEW |
---|---|
(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 loop->Run(); | |
michaeln
2014/08/14 22:59:01
it's real common to see this one-liner to get queu
jkarlin
2014/08/15 11:49:44
I can do that once the cache uses the passed in Me
| |
136 | |
137 return callback_error_ == ServiceWorkerCache::ErrorTypeOK; | |
138 } | |
139 | |
140 bool Match(ServiceWorkerFetchRequest* request) { | |
141 scoped_ptr<base::RunLoop> loop(new base::RunLoop()); | |
142 | |
143 cache_->Match(request, | |
144 base::Bind(&ServiceWorkerCacheTest::ResponseAndErrorCallback, | |
145 base::Unretained(this), | |
146 base::Unretained(loop.get()))); | |
147 loop->Run(); | |
148 | |
149 return callback_error_ == ServiceWorkerCache::ErrorTypeOK; | |
150 } | |
151 | |
152 bool Delete(ServiceWorkerFetchRequest* request) { | |
153 scoped_ptr<base::RunLoop> loop(new base::RunLoop()); | |
154 | |
155 cache_->Delete(request, | |
156 base::Bind(&ServiceWorkerCacheTest::ErrorTypeCallback, | |
157 base::Unretained(this), | |
158 base::Unretained(loop.get()))); | |
159 loop->Run(); | |
160 | |
161 return callback_error_ == ServiceWorkerCache::ErrorTypeOK; | |
162 } | |
163 | |
164 void ErrorTypeCallback(base::RunLoop* run_loop, | |
165 ServiceWorkerCache::ErrorType error) { | |
166 callback_error_ = error; | |
167 run_loop->Quit(); | |
168 } | |
169 | |
170 void ResponseAndErrorCallback(base::RunLoop* run_loop, | |
171 ServiceWorkerCache::ErrorType error, | |
172 scoped_ptr<ServiceWorkerResponse> response) { | |
173 callback_error_ = error; | |
174 callback_response_ = response.Pass(); | |
175 | |
176 if (error == ServiceWorkerCache::ErrorTypeOK && | |
177 !callback_response_->blob_uuid.empty()) { | |
178 callback_response_data_ = blob_storage_context_->GetBlobDataFromUUID( | |
179 callback_response_->blob_uuid); | |
180 cache_->DerefBlob(callback_response_->blob_uuid); | |
181 } | |
182 | |
183 run_loop->Quit(); | |
184 } | |
185 | |
186 void CopyBody(webkit_blob::BlobDataHandle* blob_handle, std::string* output) { | |
187 webkit_blob::BlobData* data = blob_handle->data(); | |
188 std::vector<webkit_blob::BlobData::Item> items = data->items(); | |
189 for (size_t i = 0, max = items.size(); i < max; ++i) | |
190 output->append(items[i].bytes(), items[i].length()); | |
191 } | |
192 | |
193 virtual bool MemoryOnly() { return false; } | |
194 | |
195 protected: | |
196 TestBrowserContext browser_context_; | |
197 TestBrowserThreadBundle browser_thread_bundle_; | |
198 scoped_ptr<net::URLRequestJobFactoryImpl> url_request_job_factory_; | |
199 webkit_blob::BlobStorageContext* blob_storage_context_; | |
200 | |
201 base::ScopedTempDir temp_dir_; | |
202 scoped_ptr<ServiceWorkerCache> cache_; | |
203 | |
204 scoped_ptr<ServiceWorkerFetchRequest> body_request_; | |
205 scoped_ptr<ServiceWorkerResponse> body_response_; | |
206 scoped_ptr<ServiceWorkerFetchRequest> no_body_request_; | |
207 scoped_ptr<ServiceWorkerResponse> no_body_response_; | |
208 scoped_refptr<webkit_blob::BlobData> blob_data_; | |
209 scoped_ptr<webkit_blob::BlobDataHandle> blob_handle1_; | |
210 std::string expected_blob_data_; | |
211 | |
212 ServiceWorkerCache::ErrorType callback_error_; | |
213 scoped_ptr<ServiceWorkerResponse> callback_response_; | |
214 scoped_ptr<webkit_blob::BlobDataHandle> callback_response_data_; | |
215 }; | |
216 | |
217 class ServiceWorkerCacheTestP : public ServiceWorkerCacheTest, | |
218 public testing::WithParamInterface<bool> { | |
219 virtual bool MemoryOnly() OVERRIDE { return !GetParam(); } | |
220 }; | |
221 | |
222 TEST_P(ServiceWorkerCacheTestP, PutNoBody) { | |
michaeln
2014/08/14 22:59:01
i'm not familiar with TEST_P?
jkarlin
2014/08/15 11:49:44
It lets you parameterize the test class. In this
| |
223 EXPECT_TRUE(Put(no_body_request_.get(), no_body_response_.get())); | |
224 } | |
225 | |
226 TEST_P(ServiceWorkerCacheTestP, PutBody) { | |
227 EXPECT_TRUE(Put(body_request_.get(), body_response_.get())); | |
228 } | |
229 | |
230 TEST_P(ServiceWorkerCacheTestP, PutBodyDropBlobRef) { | |
231 scoped_ptr<base::RunLoop> loop(new base::RunLoop()); | |
232 cache_->Put(body_request_.get(), | |
233 body_response_.get(), | |
234 base::Bind(&ServiceWorkerCacheTestP::ErrorTypeCallback, | |
235 base::Unretained(this), | |
236 base::Unretained(loop.get()))); | |
237 // The handle should be held by the cache now so the deref here should be | |
238 // okay. | |
239 blob_handle1_.reset(); | |
240 loop->Run(); | |
241 | |
242 EXPECT_EQ(ServiceWorkerCache::ErrorTypeOK, callback_error_); | |
243 } | |
244 | |
245 TEST_P(ServiceWorkerCacheTestP, DeleteNoBody) { | |
246 LOG(ERROR) << "0"; | |
michaeln
2014/08/14 22:59:01
not sure you want to keep the log lines
jkarlin
2014/08/15 11:49:44
Done.
| |
247 EXPECT_TRUE(Put(no_body_request_.get(), no_body_response_.get())); | |
248 LOG(ERROR) << "1"; | |
249 EXPECT_TRUE(Match(no_body_request_.get())); | |
250 LOG(ERROR) << "2"; | |
251 EXPECT_TRUE(Delete(no_body_request_.get())); | |
252 LOG(ERROR) << "3"; | |
253 EXPECT_FALSE(Match(no_body_request_.get())); | |
254 LOG(ERROR) << "4"; | |
255 EXPECT_FALSE(Delete(no_body_request_.get())); | |
256 LOG(ERROR) << "5"; | |
257 EXPECT_TRUE(Put(no_body_request_.get(), no_body_response_.get())); | |
258 LOG(ERROR) << "6"; | |
259 EXPECT_TRUE(Match(no_body_request_.get())); | |
260 LOG(ERROR) << "7"; | |
261 EXPECT_TRUE(Delete(no_body_request_.get())); | |
262 LOG(ERROR) << "8"; | |
263 } | |
264 | |
265 TEST_P(ServiceWorkerCacheTestP, MatchNoBody) { | |
266 EXPECT_TRUE(Put(no_body_request_.get(), no_body_response_.get())); | |
267 EXPECT_TRUE(Match(no_body_request_.get())); | |
268 EXPECT_EQ(200, callback_response_->status_code); | |
269 EXPECT_STREQ("OK", callback_response_->status_text.c_str()); | |
270 EXPECT_STREQ("http://example.com/no_body.html", | |
271 callback_response_->url.spec().c_str()); | |
272 } | |
273 | |
274 TEST_P(ServiceWorkerCacheTestP, MatchBody) { | |
275 EXPECT_TRUE(Put(body_request_.get(), body_response_.get())); | |
276 EXPECT_TRUE(Match(body_request_.get())); | |
277 EXPECT_EQ(200, callback_response_->status_code); | |
278 EXPECT_STREQ("OK", callback_response_->status_text.c_str()); | |
279 EXPECT_STREQ("http://example.com/body.html", | |
280 callback_response_->url.spec().c_str()); | |
281 std::string response_body; | |
282 CopyBody(callback_response_data_.get(), &response_body); | |
283 EXPECT_STREQ(expected_blob_data_.c_str(), response_body.c_str()); | |
284 } | |
285 | |
286 INSTANTIATE_TEST_CASE_P(ServiceWorkerCacheTest, | |
287 ServiceWorkerCacheTestP, | |
288 ::testing::Values(false, true)); | |
289 | |
290 } // namespace content | |
OLD | NEW |