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