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