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

Side by Side Diff: content/browser/service_worker/service_worker_cache_unittest.cc

Issue 528233003: This is a reland of ServiceWorkerCache::Keys. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase Created 6 years, 3 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
« no previous file with comments | « content/browser/service_worker/service_worker_cache.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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
166 void ErrorTypeCallback(base::RunLoop* run_loop, 187 void ErrorTypeCallback(base::RunLoop* run_loop,
167 ServiceWorkerCache::ErrorType error) { 188 ServiceWorkerCache::ErrorType error) {
168 callback_error_ = error; 189 callback_error_ = error;
169 run_loop->Quit(); 190 run_loop->Quit();
170 } 191 }
171 192
172 void ResponseAndErrorCallback( 193 void ResponseAndErrorCallback(
173 base::RunLoop* run_loop, 194 base::RunLoop* run_loop,
174 ServiceWorkerCache::ErrorType error, 195 ServiceWorkerCache::ErrorType error,
175 scoped_ptr<ServiceWorkerResponse> response, 196 scoped_ptr<ServiceWorkerResponse> response,
176 scoped_ptr<storage::BlobDataHandle> body_handle) { 197 scoped_ptr<storage::BlobDataHandle> body_handle) {
177 callback_error_ = error; 198 callback_error_ = error;
178 callback_response_ = response.Pass(); 199 callback_response_ = response.Pass();
179 200
180 if (error == ServiceWorkerCache::ErrorTypeOK && 201 if (error == ServiceWorkerCache::ErrorTypeOK &&
181 !callback_response_->blob_uuid.empty()) { 202 !callback_response_->blob_uuid.empty()) {
182 callback_response_data_ = body_handle.Pass(); 203 callback_response_data_ = body_handle.Pass();
183 } 204 }
184 205
185 run_loop->Quit(); 206 run_loop->Quit();
186 } 207 }
187 208
188 void CopyBody(storage::BlobDataHandle* blob_handle, std::string* output) { 209 void CopyBody(storage::BlobDataHandle* blob_handle, std::string* output) {
189 storage::BlobData* data = blob_handle->data(); 210 storage::BlobData* data = blob_handle->data();
190 std::vector<storage::BlobData::Item> items = data->items(); 211 std::vector<storage::BlobData::Item> items = data->items();
191 for (size_t i = 0, max = items.size(); i < max; ++i) 212 for (size_t i = 0, max = items.size(); i < max; ++i)
192 output->append(items[i].bytes(), items[i].length()); 213 output->append(items[i].bytes(), items[i].length());
193 } 214 }
194 215
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
195 virtual bool MemoryOnly() { return false; } 231 virtual bool MemoryOnly() { return false; }
196 232
197 protected: 233 protected:
198 TestBrowserContext browser_context_; 234 TestBrowserContext browser_context_;
199 TestBrowserThreadBundle browser_thread_bundle_; 235 TestBrowserThreadBundle browser_thread_bundle_;
200 scoped_ptr<net::URLRequestJobFactoryImpl> url_request_job_factory_; 236 scoped_ptr<net::URLRequestJobFactoryImpl> url_request_job_factory_;
201 storage::BlobStorageContext* blob_storage_context_; 237 storage::BlobStorageContext* blob_storage_context_;
202 238
203 base::ScopedTempDir temp_dir_; 239 base::ScopedTempDir temp_dir_;
204 scoped_ptr<ServiceWorkerCache> cache_; 240 scoped_ptr<ServiceWorkerCache> cache_;
205 241
206 scoped_ptr<ServiceWorkerFetchRequest> body_request_; 242 scoped_ptr<ServiceWorkerFetchRequest> body_request_;
207 scoped_ptr<ServiceWorkerResponse> body_response_; 243 scoped_ptr<ServiceWorkerResponse> body_response_;
208 scoped_ptr<ServiceWorkerFetchRequest> no_body_request_; 244 scoped_ptr<ServiceWorkerFetchRequest> no_body_request_;
209 scoped_ptr<ServiceWorkerResponse> no_body_response_; 245 scoped_ptr<ServiceWorkerResponse> no_body_response_;
210 scoped_ptr<storage::BlobDataHandle> blob_handle_; 246 scoped_ptr<storage::BlobDataHandle> blob_handle_;
211 std::string expected_blob_data_; 247 std::string expected_blob_data_;
212 248
213 ServiceWorkerCache::ErrorType callback_error_; 249 ServiceWorkerCache::ErrorType callback_error_;
214 scoped_ptr<ServiceWorkerResponse> callback_response_; 250 scoped_ptr<ServiceWorkerResponse> callback_response_;
215 scoped_ptr<storage::BlobDataHandle> callback_response_data_; 251 scoped_ptr<storage::BlobDataHandle> callback_response_data_;
252 std::vector<std::string> callback_strings_;
216 }; 253 };
217 254
218 class ServiceWorkerCacheTestP : public ServiceWorkerCacheTest, 255 class ServiceWorkerCacheTestP : public ServiceWorkerCacheTest,
219 public testing::WithParamInterface<bool> { 256 public testing::WithParamInterface<bool> {
220 virtual bool MemoryOnly() OVERRIDE { return !GetParam(); } 257 virtual bool MemoryOnly() OVERRIDE { return !GetParam(); }
221 }; 258 };
222 259
223 TEST_P(ServiceWorkerCacheTestP, PutNoBody) { 260 TEST_P(ServiceWorkerCacheTestP, PutNoBody) {
224 EXPECT_TRUE(Put(no_body_request_.get(), no_body_response_.get())); 261 EXPECT_TRUE(Put(no_body_request_.get(), no_body_response_.get()));
225 } 262 }
(...skipping 10 matching lines...) Expand all
236 base::Unretained(this), 273 base::Unretained(this),
237 base::Unretained(loop.get()))); 274 base::Unretained(loop.get())));
238 // The handle should be held by the cache now so the deref here should be 275 // The handle should be held by the cache now so the deref here should be
239 // okay. 276 // okay.
240 blob_handle_.reset(); 277 blob_handle_.reset();
241 loop->Run(); 278 loop->Run();
242 279
243 EXPECT_EQ(ServiceWorkerCache::ErrorTypeOK, callback_error_); 280 EXPECT_EQ(ServiceWorkerCache::ErrorTypeOK, callback_error_);
244 } 281 }
245 282
283 TEST_P(ServiceWorkerCacheTestP, MatchNoBody) {
284 EXPECT_TRUE(Put(no_body_request_.get(), no_body_response_.get()));
285 EXPECT_TRUE(Match(no_body_request_.get()));
286 EXPECT_EQ(200, callback_response_->status_code);
287 EXPECT_STREQ("OK", callback_response_->status_text.c_str());
288 EXPECT_STREQ("http://example.com/no_body.html",
289 callback_response_->url.spec().c_str());
290 }
291
292 TEST_P(ServiceWorkerCacheTestP, MatchBody) {
293 EXPECT_TRUE(Put(body_request_.get(), body_response_.get()));
294 EXPECT_TRUE(Match(body_request_.get()));
295 EXPECT_EQ(200, callback_response_->status_code);
296 EXPECT_STREQ("OK", callback_response_->status_text.c_str());
297 EXPECT_STREQ("http://example.com/body.html",
298 callback_response_->url.spec().c_str());
299 std::string response_body;
300 CopyBody(callback_response_data_.get(), &response_body);
301 EXPECT_STREQ(expected_blob_data_.c_str(), response_body.c_str());
302 }
303
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
246 TEST_P(ServiceWorkerCacheTestP, DeleteNoBody) { 342 TEST_P(ServiceWorkerCacheTestP, DeleteNoBody) {
247 EXPECT_TRUE(Put(no_body_request_.get(), no_body_response_.get())); 343 EXPECT_TRUE(Put(no_body_request_.get(), no_body_response_.get()));
248 EXPECT_TRUE(Match(no_body_request_.get())); 344 EXPECT_TRUE(Match(no_body_request_.get()));
249 EXPECT_TRUE(Delete(no_body_request_.get())); 345 EXPECT_TRUE(Delete(no_body_request_.get()));
250 EXPECT_FALSE(Match(no_body_request_.get())); 346 EXPECT_FALSE(Match(no_body_request_.get()));
251 EXPECT_FALSE(Delete(no_body_request_.get())); 347 EXPECT_FALSE(Delete(no_body_request_.get()));
252 EXPECT_TRUE(Put(no_body_request_.get(), no_body_response_.get())); 348 EXPECT_TRUE(Put(no_body_request_.get(), no_body_response_.get()));
253 EXPECT_TRUE(Match(no_body_request_.get())); 349 EXPECT_TRUE(Match(no_body_request_.get()));
254 EXPECT_TRUE(Delete(no_body_request_.get())); 350 EXPECT_TRUE(Delete(no_body_request_.get()));
255 } 351 }
256 352
257 TEST_P(ServiceWorkerCacheTestP, DeleteBody) { 353 TEST_P(ServiceWorkerCacheTestP, DeleteBody) {
258 EXPECT_TRUE(Put(body_request_.get(), body_response_.get())); 354 EXPECT_TRUE(Put(body_request_.get(), body_response_.get()));
259 EXPECT_TRUE(Match(body_request_.get())); 355 EXPECT_TRUE(Match(body_request_.get()));
260 EXPECT_TRUE(Delete(body_request_.get())); 356 EXPECT_TRUE(Delete(body_request_.get()));
261 EXPECT_FALSE(Match(body_request_.get())); 357 EXPECT_FALSE(Match(body_request_.get()));
262 EXPECT_FALSE(Delete(body_request_.get())); 358 EXPECT_FALSE(Delete(body_request_.get()));
263 EXPECT_TRUE(Put(body_request_.get(), body_response_.get())); 359 EXPECT_TRUE(Put(body_request_.get(), body_response_.get()));
264 EXPECT_TRUE(Match(body_request_.get())); 360 EXPECT_TRUE(Match(body_request_.get()));
265 EXPECT_TRUE(Delete(body_request_.get())); 361 EXPECT_TRUE(Delete(body_request_.get()));
266 } 362 }
267 363
268 TEST_P(ServiceWorkerCacheTestP, MatchNoBody) {
269 EXPECT_TRUE(Put(no_body_request_.get(), no_body_response_.get()));
270 EXPECT_TRUE(Match(no_body_request_.get()));
271 EXPECT_EQ(200, callback_response_->status_code);
272 EXPECT_STREQ("OK", callback_response_->status_text.c_str());
273 EXPECT_STREQ("http://example.com/no_body.html",
274 callback_response_->url.spec().c_str());
275 }
276
277 TEST_P(ServiceWorkerCacheTestP, MatchBody) {
278 EXPECT_TRUE(Put(body_request_.get(), body_response_.get()));
279 EXPECT_TRUE(Match(body_request_.get()));
280 EXPECT_EQ(200, callback_response_->status_code);
281 EXPECT_STREQ("OK", callback_response_->status_text.c_str());
282 EXPECT_STREQ("http://example.com/body.html",
283 callback_response_->url.spec().c_str());
284 std::string response_body;
285 CopyBody(callback_response_data_.get(), &response_body);
286 EXPECT_STREQ(expected_blob_data_.c_str(), response_body.c_str());
287 }
288
289 TEST_P(ServiceWorkerCacheTestP, QuickStressNoBody) { 364 TEST_P(ServiceWorkerCacheTestP, QuickStressNoBody) {
290 for (int i = 0; i < 100; ++i) { 365 for (int i = 0; i < 100; ++i) {
291 EXPECT_FALSE(Match(no_body_request_.get())); 366 EXPECT_FALSE(Match(no_body_request_.get()));
292 EXPECT_TRUE(Put(no_body_request_.get(), no_body_response_.get())); 367 EXPECT_TRUE(Put(no_body_request_.get(), no_body_response_.get()));
293 EXPECT_TRUE(Match(no_body_request_.get())); 368 EXPECT_TRUE(Match(no_body_request_.get()));
294 EXPECT_TRUE(Delete(no_body_request_.get())); 369 EXPECT_TRUE(Delete(no_body_request_.get()));
295 } 370 }
296 } 371 }
297 372
298 TEST_P(ServiceWorkerCacheTestP, QuickStressBody) { 373 TEST_P(ServiceWorkerCacheTestP, QuickStressBody) {
299 for (int i = 0; i < 100; ++i) { 374 for (int i = 0; i < 100; ++i) {
300 ASSERT_FALSE(Match(body_request_.get())); 375 ASSERT_FALSE(Match(body_request_.get()));
301 ASSERT_TRUE(Put(body_request_.get(), body_response_.get())); 376 ASSERT_TRUE(Put(body_request_.get(), body_response_.get()));
302 ASSERT_TRUE(Match(body_request_.get())); 377 ASSERT_TRUE(Match(body_request_.get()));
303 ASSERT_TRUE(Delete(body_request_.get())); 378 ASSERT_TRUE(Delete(body_request_.get()));
304 } 379 }
305 } 380 }
381 #endif // OS_WIN
306 382
307 INSTANTIATE_TEST_CASE_P(ServiceWorkerCacheTest, 383 INSTANTIATE_TEST_CASE_P(ServiceWorkerCacheTest,
308 ServiceWorkerCacheTestP, 384 ServiceWorkerCacheTestP,
309 ::testing::Values(false, true)); 385 ::testing::Values(false, true));
310 386
311 } // namespace content 387 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/service_worker/service_worker_cache.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698