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" |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 | 156 |
157 cache_->Delete(request, | 157 cache_->Delete(request, |
158 base::Bind(&ServiceWorkerCacheTest::ErrorTypeCallback, | 158 base::Bind(&ServiceWorkerCacheTest::ErrorTypeCallback, |
159 base::Unretained(this), | 159 base::Unretained(this), |
160 base::Unretained(loop.get()))); | 160 base::Unretained(loop.get()))); |
161 loop->Run(); | 161 loop->Run(); |
162 | 162 |
163 return callback_error_ == ServiceWorkerCache::ErrorTypeOK; | 163 return callback_error_ == ServiceWorkerCache::ErrorTypeOK; |
164 } | 164 } |
165 | 165 |
166 bool Keys() { | |
167 scoped_ptr<base::RunLoop> loop(new base::RunLoop()); | |
168 | |
169 cache_->Keys(base::Bind(&ServiceWorkerCacheTest::RequestsCallback, | |
170 base::Unretained(this), | |
171 base::Unretained(loop.get()))); | |
172 loop->Run(); | |
173 | |
174 return callback_error_ == ServiceWorkerCache::ErrorTypeOK; | |
175 } | |
176 | |
177 void RequestsCallback(base::RunLoop* run_loop, | |
178 ServiceWorkerCache::ErrorType error, | |
179 scoped_ptr<ServiceWorkerCache::Requests> requests) { | |
180 callback_error_ = error; | |
181 callback_strings_.clear(); | |
182 for (size_t i = 0u; i < requests->size(); ++i) | |
183 callback_strings_.push_back(requests->at(i).url.spec()); | |
184 run_loop->Quit(); | |
185 } | |
186 | |
187 void ErrorTypeCallback(base::RunLoop* run_loop, | 166 void ErrorTypeCallback(base::RunLoop* run_loop, |
188 ServiceWorkerCache::ErrorType error) { | 167 ServiceWorkerCache::ErrorType error) { |
189 callback_error_ = error; | 168 callback_error_ = error; |
190 run_loop->Quit(); | 169 run_loop->Quit(); |
191 } | 170 } |
192 | 171 |
193 void ResponseAndErrorCallback( | 172 void ResponseAndErrorCallback( |
194 base::RunLoop* run_loop, | 173 base::RunLoop* run_loop, |
195 ServiceWorkerCache::ErrorType error, | 174 ServiceWorkerCache::ErrorType error, |
196 scoped_ptr<ServiceWorkerResponse> response, | 175 scoped_ptr<ServiceWorkerResponse> response, |
197 scoped_ptr<storage::BlobDataHandle> body_handle) { | 176 scoped_ptr<storage::BlobDataHandle> body_handle) { |
198 callback_error_ = error; | 177 callback_error_ = error; |
199 callback_response_ = response.Pass(); | 178 callback_response_ = response.Pass(); |
200 | 179 |
201 if (error == ServiceWorkerCache::ErrorTypeOK && | 180 if (error == ServiceWorkerCache::ErrorTypeOK && |
202 !callback_response_->blob_uuid.empty()) { | 181 !callback_response_->blob_uuid.empty()) { |
203 callback_response_data_ = body_handle.Pass(); | 182 callback_response_data_ = body_handle.Pass(); |
204 } | 183 } |
205 | 184 |
206 run_loop->Quit(); | 185 run_loop->Quit(); |
207 } | 186 } |
208 | 187 |
209 void CopyBody(storage::BlobDataHandle* blob_handle, std::string* output) { | 188 void CopyBody(storage::BlobDataHandle* blob_handle, std::string* output) { |
210 storage::BlobData* data = blob_handle->data(); | 189 storage::BlobData* data = blob_handle->data(); |
211 std::vector<storage::BlobData::Item> items = data->items(); | 190 std::vector<storage::BlobData::Item> items = data->items(); |
212 for (size_t i = 0, max = items.size(); i < max; ++i) | 191 for (size_t i = 0, max = items.size(); i < max; ++i) |
213 output->append(items[i].bytes(), items[i].length()); | 192 output->append(items[i].bytes(), items[i].length()); |
214 } | 193 } |
215 | 194 |
216 bool VerifyKeys(const std::vector<std::string>& expected_keys) { | |
217 if (expected_keys.size() != callback_strings_.size()) | |
218 return false; | |
219 | |
220 std::set<std::string> found_set; | |
221 for (int i = 0, max = callback_strings_.size(); i < max; ++i) | |
222 found_set.insert(callback_strings_[i]); | |
223 | |
224 for (int i = 0, max = expected_keys.size(); i < max; ++i) { | |
225 if (found_set.find(expected_keys[i]) == found_set.end()) | |
226 return false; | |
227 } | |
228 return true; | |
229 } | |
230 | |
231 virtual bool MemoryOnly() { return false; } | 195 virtual bool MemoryOnly() { return false; } |
232 | 196 |
233 protected: | 197 protected: |
234 TestBrowserContext browser_context_; | 198 TestBrowserContext browser_context_; |
235 TestBrowserThreadBundle browser_thread_bundle_; | 199 TestBrowserThreadBundle browser_thread_bundle_; |
236 scoped_ptr<net::URLRequestJobFactoryImpl> url_request_job_factory_; | 200 scoped_ptr<net::URLRequestJobFactoryImpl> url_request_job_factory_; |
237 storage::BlobStorageContext* blob_storage_context_; | 201 storage::BlobStorageContext* blob_storage_context_; |
238 | 202 |
239 base::ScopedTempDir temp_dir_; | 203 base::ScopedTempDir temp_dir_; |
240 scoped_ptr<ServiceWorkerCache> cache_; | 204 scoped_ptr<ServiceWorkerCache> cache_; |
241 | 205 |
242 scoped_ptr<ServiceWorkerFetchRequest> body_request_; | 206 scoped_ptr<ServiceWorkerFetchRequest> body_request_; |
243 scoped_ptr<ServiceWorkerResponse> body_response_; | 207 scoped_ptr<ServiceWorkerResponse> body_response_; |
244 scoped_ptr<ServiceWorkerFetchRequest> no_body_request_; | 208 scoped_ptr<ServiceWorkerFetchRequest> no_body_request_; |
245 scoped_ptr<ServiceWorkerResponse> no_body_response_; | 209 scoped_ptr<ServiceWorkerResponse> no_body_response_; |
246 scoped_ptr<storage::BlobDataHandle> blob_handle_; | 210 scoped_ptr<storage::BlobDataHandle> blob_handle_; |
247 std::string expected_blob_data_; | 211 std::string expected_blob_data_; |
248 | 212 |
249 ServiceWorkerCache::ErrorType callback_error_; | 213 ServiceWorkerCache::ErrorType callback_error_; |
250 scoped_ptr<ServiceWorkerResponse> callback_response_; | 214 scoped_ptr<ServiceWorkerResponse> callback_response_; |
251 scoped_ptr<storage::BlobDataHandle> callback_response_data_; | 215 scoped_ptr<storage::BlobDataHandle> callback_response_data_; |
252 std::vector<std::string> callback_strings_; | |
253 }; | 216 }; |
254 | 217 |
255 class ServiceWorkerCacheTestP : public ServiceWorkerCacheTest, | 218 class ServiceWorkerCacheTestP : public ServiceWorkerCacheTest, |
256 public testing::WithParamInterface<bool> { | 219 public testing::WithParamInterface<bool> { |
257 virtual bool MemoryOnly() OVERRIDE { return !GetParam(); } | 220 virtual bool MemoryOnly() OVERRIDE { return !GetParam(); } |
258 }; | 221 }; |
259 | 222 |
260 TEST_P(ServiceWorkerCacheTestP, PutNoBody) { | 223 TEST_P(ServiceWorkerCacheTestP, PutNoBody) { |
261 EXPECT_TRUE(Put(no_body_request_.get(), no_body_response_.get())); | 224 EXPECT_TRUE(Put(no_body_request_.get(), no_body_response_.get())); |
262 } | 225 } |
(...skipping 10 matching lines...) Expand all Loading... |
273 base::Unretained(this), | 236 base::Unretained(this), |
274 base::Unretained(loop.get()))); | 237 base::Unretained(loop.get()))); |
275 // The handle should be held by the cache now so the deref here should be | 238 // The handle should be held by the cache now so the deref here should be |
276 // okay. | 239 // okay. |
277 blob_handle_.reset(); | 240 blob_handle_.reset(); |
278 loop->Run(); | 241 loop->Run(); |
279 | 242 |
280 EXPECT_EQ(ServiceWorkerCache::ErrorTypeOK, callback_error_); | 243 EXPECT_EQ(ServiceWorkerCache::ErrorTypeOK, callback_error_); |
281 } | 244 } |
282 | 245 |
| 246 TEST_P(ServiceWorkerCacheTestP, DeleteNoBody) { |
| 247 EXPECT_TRUE(Put(no_body_request_.get(), no_body_response_.get())); |
| 248 EXPECT_TRUE(Match(no_body_request_.get())); |
| 249 EXPECT_TRUE(Delete(no_body_request_.get())); |
| 250 EXPECT_FALSE(Match(no_body_request_.get())); |
| 251 EXPECT_FALSE(Delete(no_body_request_.get())); |
| 252 EXPECT_TRUE(Put(no_body_request_.get(), no_body_response_.get())); |
| 253 EXPECT_TRUE(Match(no_body_request_.get())); |
| 254 EXPECT_TRUE(Delete(no_body_request_.get())); |
| 255 } |
| 256 |
| 257 TEST_P(ServiceWorkerCacheTestP, DeleteBody) { |
| 258 EXPECT_TRUE(Put(body_request_.get(), body_response_.get())); |
| 259 EXPECT_TRUE(Match(body_request_.get())); |
| 260 EXPECT_TRUE(Delete(body_request_.get())); |
| 261 EXPECT_FALSE(Match(body_request_.get())); |
| 262 EXPECT_FALSE(Delete(body_request_.get())); |
| 263 EXPECT_TRUE(Put(body_request_.get(), body_response_.get())); |
| 264 EXPECT_TRUE(Match(body_request_.get())); |
| 265 EXPECT_TRUE(Delete(body_request_.get())); |
| 266 } |
| 267 |
283 TEST_P(ServiceWorkerCacheTestP, MatchNoBody) { | 268 TEST_P(ServiceWorkerCacheTestP, MatchNoBody) { |
284 EXPECT_TRUE(Put(no_body_request_.get(), no_body_response_.get())); | 269 EXPECT_TRUE(Put(no_body_request_.get(), no_body_response_.get())); |
285 EXPECT_TRUE(Match(no_body_request_.get())); | 270 EXPECT_TRUE(Match(no_body_request_.get())); |
286 EXPECT_EQ(200, callback_response_->status_code); | 271 EXPECT_EQ(200, callback_response_->status_code); |
287 EXPECT_STREQ("OK", callback_response_->status_text.c_str()); | 272 EXPECT_STREQ("OK", callback_response_->status_text.c_str()); |
288 EXPECT_STREQ("http://example.com/no_body.html", | 273 EXPECT_STREQ("http://example.com/no_body.html", |
289 callback_response_->url.spec().c_str()); | 274 callback_response_->url.spec().c_str()); |
290 } | 275 } |
291 | 276 |
292 TEST_P(ServiceWorkerCacheTestP, MatchBody) { | 277 TEST_P(ServiceWorkerCacheTestP, MatchBody) { |
293 EXPECT_TRUE(Put(body_request_.get(), body_response_.get())); | 278 EXPECT_TRUE(Put(body_request_.get(), body_response_.get())); |
294 EXPECT_TRUE(Match(body_request_.get())); | 279 EXPECT_TRUE(Match(body_request_.get())); |
295 EXPECT_EQ(200, callback_response_->status_code); | 280 EXPECT_EQ(200, callback_response_->status_code); |
296 EXPECT_STREQ("OK", callback_response_->status_text.c_str()); | 281 EXPECT_STREQ("OK", callback_response_->status_text.c_str()); |
297 EXPECT_STREQ("http://example.com/body.html", | 282 EXPECT_STREQ("http://example.com/body.html", |
298 callback_response_->url.spec().c_str()); | 283 callback_response_->url.spec().c_str()); |
299 std::string response_body; | 284 std::string response_body; |
300 CopyBody(callback_response_data_.get(), &response_body); | 285 CopyBody(callback_response_data_.get(), &response_body); |
301 EXPECT_STREQ(expected_blob_data_.c_str(), response_body.c_str()); | 286 EXPECT_STREQ(expected_blob_data_.c_str(), response_body.c_str()); |
302 } | 287 } |
303 | 288 |
304 TEST_P(ServiceWorkerCacheTestP, EmptyKeys) { | |
305 EXPECT_TRUE(Keys()); | |
306 EXPECT_EQ(0u, callback_strings_.size()); | |
307 } | |
308 | |
309 TEST_P(ServiceWorkerCacheTestP, TwoKeys) { | |
310 EXPECT_TRUE(Put(no_body_request_.get(), no_body_response_.get())); | |
311 EXPECT_TRUE(Put(body_request_.get(), body_response_.get())); | |
312 EXPECT_TRUE(Keys()); | |
313 EXPECT_EQ(2u, callback_strings_.size()); | |
314 std::vector<std::string> expected_keys; | |
315 expected_keys.push_back(no_body_request_->url.spec()); | |
316 expected_keys.push_back(body_request_->url.spec()); | |
317 EXPECT_TRUE(VerifyKeys(expected_keys)); | |
318 } | |
319 | |
320 TEST_P(ServiceWorkerCacheTestP, TwoKeysThenOne) { | |
321 EXPECT_TRUE(Put(no_body_request_.get(), no_body_response_.get())); | |
322 EXPECT_TRUE(Put(body_request_.get(), body_response_.get())); | |
323 EXPECT_TRUE(Keys()); | |
324 EXPECT_EQ(2u, callback_strings_.size()); | |
325 std::vector<std::string> expected_keys; | |
326 expected_keys.push_back(no_body_request_->url.spec()); | |
327 expected_keys.push_back(body_request_->url.spec()); | |
328 EXPECT_TRUE(VerifyKeys(expected_keys)); | |
329 | |
330 EXPECT_TRUE(Delete(body_request_.get())); | |
331 EXPECT_TRUE(Keys()); | |
332 EXPECT_EQ(1u, callback_strings_.size()); | |
333 std::vector<std::string> expected_key; | |
334 expected_key.push_back(no_body_request_->url.spec()); | |
335 EXPECT_TRUE(VerifyKeys(expected_key)); | |
336 } | |
337 | |
338 // TODO(jkarlin): Once SimpleCache is working bug-free on Windows reenable these | |
339 // tests. In the meanwhile we know that Windows operations will be a little | |
340 // flaky (though not crashy). See https://crbug.com/409109 | |
341 #ifndef OS_WIN | |
342 TEST_P(ServiceWorkerCacheTestP, DeleteNoBody) { | |
343 EXPECT_TRUE(Put(no_body_request_.get(), no_body_response_.get())); | |
344 EXPECT_TRUE(Match(no_body_request_.get())); | |
345 EXPECT_TRUE(Delete(no_body_request_.get())); | |
346 EXPECT_FALSE(Match(no_body_request_.get())); | |
347 EXPECT_FALSE(Delete(no_body_request_.get())); | |
348 EXPECT_TRUE(Put(no_body_request_.get(), no_body_response_.get())); | |
349 EXPECT_TRUE(Match(no_body_request_.get())); | |
350 EXPECT_TRUE(Delete(no_body_request_.get())); | |
351 } | |
352 | |
353 TEST_P(ServiceWorkerCacheTestP, DeleteBody) { | |
354 EXPECT_TRUE(Put(body_request_.get(), body_response_.get())); | |
355 EXPECT_TRUE(Match(body_request_.get())); | |
356 EXPECT_TRUE(Delete(body_request_.get())); | |
357 EXPECT_FALSE(Match(body_request_.get())); | |
358 EXPECT_FALSE(Delete(body_request_.get())); | |
359 EXPECT_TRUE(Put(body_request_.get(), body_response_.get())); | |
360 EXPECT_TRUE(Match(body_request_.get())); | |
361 EXPECT_TRUE(Delete(body_request_.get())); | |
362 } | |
363 | |
364 TEST_P(ServiceWorkerCacheTestP, QuickStressNoBody) { | 289 TEST_P(ServiceWorkerCacheTestP, QuickStressNoBody) { |
365 for (int i = 0; i < 100; ++i) { | 290 for (int i = 0; i < 100; ++i) { |
366 EXPECT_FALSE(Match(no_body_request_.get())); | 291 EXPECT_FALSE(Match(no_body_request_.get())); |
367 EXPECT_TRUE(Put(no_body_request_.get(), no_body_response_.get())); | 292 EXPECT_TRUE(Put(no_body_request_.get(), no_body_response_.get())); |
368 EXPECT_TRUE(Match(no_body_request_.get())); | 293 EXPECT_TRUE(Match(no_body_request_.get())); |
369 EXPECT_TRUE(Delete(no_body_request_.get())); | 294 EXPECT_TRUE(Delete(no_body_request_.get())); |
370 } | 295 } |
371 } | 296 } |
372 | 297 |
373 TEST_P(ServiceWorkerCacheTestP, QuickStressBody) { | 298 TEST_P(ServiceWorkerCacheTestP, QuickStressBody) { |
374 for (int i = 0; i < 100; ++i) { | 299 for (int i = 0; i < 100; ++i) { |
375 ASSERT_FALSE(Match(body_request_.get())); | 300 ASSERT_FALSE(Match(body_request_.get())); |
376 ASSERT_TRUE(Put(body_request_.get(), body_response_.get())); | 301 ASSERT_TRUE(Put(body_request_.get(), body_response_.get())); |
377 ASSERT_TRUE(Match(body_request_.get())); | 302 ASSERT_TRUE(Match(body_request_.get())); |
378 ASSERT_TRUE(Delete(body_request_.get())); | 303 ASSERT_TRUE(Delete(body_request_.get())); |
379 } | 304 } |
380 } | 305 } |
381 #endif // OS_WIN | |
382 | 306 |
383 INSTANTIATE_TEST_CASE_P(ServiceWorkerCacheTest, | 307 INSTANTIATE_TEST_CASE_P(ServiceWorkerCacheTest, |
384 ServiceWorkerCacheTestP, | 308 ServiceWorkerCacheTestP, |
385 ::testing::Values(false, true)); | 309 ::testing::Values(false, true)); |
386 | 310 |
387 } // namespace content | 311 } // namespace content |
OLD | NEW |