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 "config.h" | 5 #include "config.h" |
6 #include "modules/serviceworkers/Cache.h" | 6 #include "modules/serviceworkers/Cache.h" |
7 | 7 |
8 #include "bindings/core/v8/ExceptionState.h" | 8 #include "bindings/core/v8/ExceptionState.h" |
9 #include "bindings/core/v8/ScriptFunction.h" | 9 #include "bindings/core/v8/ScriptFunction.h" |
10 #include "bindings/core/v8/ScriptPromise.h" | 10 #include "bindings/core/v8/ScriptPromise.h" |
(...skipping 250 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
261 }; | 261 }; |
262 | 262 |
263 TEST_F(ServiceWorkerCacheTest, Basics) | 263 TEST_F(ServiceWorkerCacheTest, Basics) |
264 { | 264 { |
265 ErrorWebCacheForTests* testCache; | 265 ErrorWebCacheForTests* testCache; |
266 Cache* cache = Cache::create(testCache = new NotImplementedErrorCache()); | 266 Cache* cache = Cache::create(testCache = new NotImplementedErrorCache()); |
267 ASSERT(cache); | 267 ASSERT(cache); |
268 | 268 |
269 const String url = "http://www.cachetest.org/"; | 269 const String url = "http://www.cachetest.org/"; |
270 | 270 |
271 CacheQueryOptions* options = CacheQueryOptions::create(); | 271 CacheQueryOptions options; |
272 ScriptPromise matchPromise = cache->match(scriptState(), url, *options); | 272 ScriptPromise matchPromise = cache->match(scriptState(), url, options); |
273 EXPECT_EQ(kNotImplementedString, getRejectString(matchPromise)); | 273 EXPECT_EQ(kNotImplementedString, getRejectString(matchPromise)); |
274 | 274 |
275 cache = Cache::create(testCache = new ErrorWebCacheForTests(WebServiceWorker
CacheErrorNotFound)); | 275 cache = Cache::create(testCache = new ErrorWebCacheForTests(WebServiceWorker
CacheErrorNotFound)); |
276 matchPromise = cache->match(scriptState(), url, *options); | 276 matchPromise = cache->match(scriptState(), url, options); |
277 EXPECT_EQ("NotFoundError: Entry was not found.", getRejectString(matchPromis
e)); | 277 EXPECT_EQ("NotFoundError: Entry was not found.", getRejectString(matchPromis
e)); |
278 | 278 |
279 cache = Cache::create(testCache = new ErrorWebCacheForTests(WebServiceWorker
CacheErrorExists)); | 279 cache = Cache::create(testCache = new ErrorWebCacheForTests(WebServiceWorker
CacheErrorExists)); |
280 matchPromise = cache->match(scriptState(), url, *options); | 280 matchPromise = cache->match(scriptState(), url, options); |
281 EXPECT_EQ("InvalidAccessError: Entry already exists.", getRejectString(match
Promise)); | 281 EXPECT_EQ("InvalidAccessError: Entry already exists.", getRejectString(match
Promise)); |
282 } | 282 } |
283 | 283 |
284 // Tests that arguments are faithfully passed on calls to Cache methods, except
for methods which use batch operations, | 284 // Tests that arguments are faithfully passed on calls to Cache methods, except
for methods which use batch operations, |
285 // which are tested later. | 285 // which are tested later. |
286 TEST_F(ServiceWorkerCacheTest, BasicArguments) | 286 TEST_F(ServiceWorkerCacheTest, BasicArguments) |
287 { | 287 { |
288 ErrorWebCacheForTests* testCache; | 288 ErrorWebCacheForTests* testCache; |
289 Cache* cache = Cache::create(testCache = new NotImplementedErrorCache()); | 289 Cache* cache = Cache::create(testCache = new NotImplementedErrorCache()); |
290 ASSERT(cache); | 290 ASSERT(cache); |
291 | 291 |
292 const String url = "http://www.cache.arguments.test/"; | 292 const String url = "http://www.cache.arguments.test/"; |
293 testCache->setExpectedUrl(&url); | 293 testCache->setExpectedUrl(&url); |
294 | 294 |
295 WebServiceWorkerCache::QueryParams expectedQueryParams; | 295 WebServiceWorkerCache::QueryParams expectedQueryParams; |
296 expectedQueryParams.ignoreVary = true; | 296 expectedQueryParams.ignoreVary = true; |
297 expectedQueryParams.cacheName = "this is a cache name"; | 297 expectedQueryParams.cacheName = "this is a cache name"; |
298 testCache->setExpectedQueryParams(&expectedQueryParams); | 298 testCache->setExpectedQueryParams(&expectedQueryParams); |
299 | 299 |
300 CacheQueryOptions* options = CacheQueryOptions::create(); | 300 CacheQueryOptions options; |
301 options->setIgnoreVary(1); | 301 options.setIgnoreVary(1); |
302 options->setCacheName(expectedQueryParams.cacheName); | 302 options.setCacheName(expectedQueryParams.cacheName); |
303 | 303 |
304 Request* request = newRequestFromUrl(url); | 304 Request* request = newRequestFromUrl(url); |
305 ASSERT(request); | 305 ASSERT(request); |
306 ScriptPromise matchResult = cache->match(scriptState(), request, *options); | 306 ScriptPromise matchResult = cache->match(scriptState(), request, options); |
307 EXPECT_EQ("dispatchMatch", testCache->getAndClearLastErrorWebCacheMethodCall
ed()); | 307 EXPECT_EQ("dispatchMatch", testCache->getAndClearLastErrorWebCacheMethodCall
ed()); |
308 EXPECT_EQ(kNotImplementedString, getRejectString(matchResult)); | 308 EXPECT_EQ(kNotImplementedString, getRejectString(matchResult)); |
309 | 309 |
310 ScriptPromise stringMatchResult = cache->match(scriptState(), url, *options)
; | 310 ScriptPromise stringMatchResult = cache->match(scriptState(), url, options); |
311 EXPECT_EQ("dispatchMatch", testCache->getAndClearLastErrorWebCacheMethodCall
ed()); | 311 EXPECT_EQ("dispatchMatch", testCache->getAndClearLastErrorWebCacheMethodCall
ed()); |
312 EXPECT_EQ(kNotImplementedString, getRejectString(stringMatchResult)); | 312 EXPECT_EQ(kNotImplementedString, getRejectString(stringMatchResult)); |
313 | 313 |
314 request = newRequestFromUrl(url); | 314 request = newRequestFromUrl(url); |
315 ASSERT(request); | 315 ASSERT(request); |
316 ScriptPromise matchAllResult = cache->matchAll(scriptState(), request, *opti
ons); | 316 ScriptPromise matchAllResult = cache->matchAll(scriptState(), request, optio
ns); |
317 EXPECT_EQ("dispatchMatchAll", testCache->getAndClearLastErrorWebCacheMethodC
alled()); | 317 EXPECT_EQ("dispatchMatchAll", testCache->getAndClearLastErrorWebCacheMethodC
alled()); |
318 EXPECT_EQ(kNotImplementedString, getRejectString(matchAllResult)); | 318 EXPECT_EQ(kNotImplementedString, getRejectString(matchAllResult)); |
319 | 319 |
320 ScriptPromise stringMatchAllResult = cache->matchAll(scriptState(), url, *op
tions); | 320 ScriptPromise stringMatchAllResult = cache->matchAll(scriptState(), url, opt
ions); |
321 EXPECT_EQ("dispatchMatchAll", testCache->getAndClearLastErrorWebCacheMethodC
alled()); | 321 EXPECT_EQ("dispatchMatchAll", testCache->getAndClearLastErrorWebCacheMethodC
alled()); |
322 EXPECT_EQ(kNotImplementedString, getRejectString(stringMatchAllResult)); | 322 EXPECT_EQ(kNotImplementedString, getRejectString(stringMatchAllResult)); |
323 | 323 |
324 ScriptPromise keysResult1 = cache->keys(scriptState()); | 324 ScriptPromise keysResult1 = cache->keys(scriptState()); |
325 EXPECT_EQ("dispatchKeys", testCache->getAndClearLastErrorWebCacheMethodCalle
d()); | 325 EXPECT_EQ("dispatchKeys", testCache->getAndClearLastErrorWebCacheMethodCalle
d()); |
326 EXPECT_EQ(kNotImplementedString, getRejectString(keysResult1)); | 326 EXPECT_EQ(kNotImplementedString, getRejectString(keysResult1)); |
327 | 327 |
328 request = newRequestFromUrl(url); | 328 request = newRequestFromUrl(url); |
329 ASSERT(request); | 329 ASSERT(request); |
330 ScriptPromise keysResult2 = cache->keys(scriptState(), request, *options); | 330 ScriptPromise keysResult2 = cache->keys(scriptState(), request, options); |
331 EXPECT_EQ("dispatchKeys", testCache->getAndClearLastErrorWebCacheMethodCalle
d()); | 331 EXPECT_EQ("dispatchKeys", testCache->getAndClearLastErrorWebCacheMethodCalle
d()); |
332 EXPECT_EQ(kNotImplementedString, getRejectString(keysResult2)); | 332 EXPECT_EQ(kNotImplementedString, getRejectString(keysResult2)); |
333 | 333 |
334 ScriptPromise stringKeysResult2 = cache->keys(scriptState(), url, *options); | 334 ScriptPromise stringKeysResult2 = cache->keys(scriptState(), url, options); |
335 EXPECT_EQ("dispatchKeys", testCache->getAndClearLastErrorWebCacheMethodCalle
d()); | 335 EXPECT_EQ("dispatchKeys", testCache->getAndClearLastErrorWebCacheMethodCalle
d()); |
336 EXPECT_EQ(kNotImplementedString, getRejectString(stringKeysResult2)); | 336 EXPECT_EQ(kNotImplementedString, getRejectString(stringKeysResult2)); |
337 } | 337 } |
338 | 338 |
339 // Tests that arguments are faithfully passed to API calls that degrade to batch
operations. | 339 // Tests that arguments are faithfully passed to API calls that degrade to batch
operations. |
340 TEST_F(ServiceWorkerCacheTest, BatchOperationArguments) | 340 TEST_F(ServiceWorkerCacheTest, BatchOperationArguments) |
341 { | 341 { |
342 ErrorWebCacheForTests* testCache; | 342 ErrorWebCacheForTests* testCache; |
343 Cache* cache = Cache::create(testCache = new NotImplementedErrorCache()); | 343 Cache* cache = Cache::create(testCache = new NotImplementedErrorCache()); |
344 ASSERT(cache); | 344 ASSERT(cache); |
345 | 345 |
346 WebServiceWorkerCache::QueryParams expectedQueryParams; | 346 WebServiceWorkerCache::QueryParams expectedQueryParams; |
347 expectedQueryParams.prefixMatch = true; | 347 expectedQueryParams.prefixMatch = true; |
348 expectedQueryParams.cacheName = "this is another cache name"; | 348 expectedQueryParams.cacheName = "this is another cache name"; |
349 testCache->setExpectedQueryParams(&expectedQueryParams); | 349 testCache->setExpectedQueryParams(&expectedQueryParams); |
350 | 350 |
351 CacheQueryOptions* options = CacheQueryOptions::create(); | 351 CacheQueryOptions options; |
352 options->setPrefixMatch(1); | 352 options.setPrefixMatch(1); |
353 options->setCacheName(expectedQueryParams.cacheName); | 353 options.setCacheName(expectedQueryParams.cacheName); |
354 | 354 |
355 const String url = "http://batch.operations.test/"; | 355 const String url = "http://batch.operations.test/"; |
356 Request* request = newRequestFromUrl(url); | 356 Request* request = newRequestFromUrl(url); |
357 ASSERT(request); | 357 ASSERT(request); |
358 | 358 |
359 WebServiceWorkerResponse webResponse; | 359 WebServiceWorkerResponse webResponse; |
360 webResponse.setURL(KURL(ParsedURLString, url)); | 360 webResponse.setURL(KURL(ParsedURLString, url)); |
361 Response* response = Response::create(executionContext(), webResponse); | 361 Response* response = Response::create(executionContext(), webResponse); |
362 | 362 |
363 WebVector<WebServiceWorkerCache::BatchOperation> expectedDeleteOperations(si
ze_t(1)); | 363 WebVector<WebServiceWorkerCache::BatchOperation> expectedDeleteOperations(si
ze_t(1)); |
364 { | 364 { |
365 WebServiceWorkerCache::BatchOperation deleteOperation; | 365 WebServiceWorkerCache::BatchOperation deleteOperation; |
366 deleteOperation.operationType = WebServiceWorkerCache::OperationTypeDele
te; | 366 deleteOperation.operationType = WebServiceWorkerCache::OperationTypeDele
te; |
367 request->populateWebServiceWorkerRequest(deleteOperation.request); | 367 request->populateWebServiceWorkerRequest(deleteOperation.request); |
368 deleteOperation.matchParams = expectedQueryParams; | 368 deleteOperation.matchParams = expectedQueryParams; |
369 expectedDeleteOperations[0] = deleteOperation; | 369 expectedDeleteOperations[0] = deleteOperation; |
370 } | 370 } |
371 testCache->setExpectedBatchOperations(&expectedDeleteOperations); | 371 testCache->setExpectedBatchOperations(&expectedDeleteOperations); |
372 | 372 |
373 ScriptPromise deleteResult = cache->deleteFunction(scriptState(), request, *
options); | 373 ScriptPromise deleteResult = cache->deleteFunction(scriptState(), request, o
ptions); |
374 EXPECT_EQ("dispatchBatch", testCache->getAndClearLastErrorWebCacheMethodCall
ed()); | 374 EXPECT_EQ("dispatchBatch", testCache->getAndClearLastErrorWebCacheMethodCall
ed()); |
375 EXPECT_EQ(kNotImplementedString, getRejectString(deleteResult)); | 375 EXPECT_EQ(kNotImplementedString, getRejectString(deleteResult)); |
376 | 376 |
377 ScriptPromise stringDeleteResult = cache->deleteFunction(scriptState(), url,
*options); | 377 ScriptPromise stringDeleteResult = cache->deleteFunction(scriptState(), url,
options); |
378 EXPECT_EQ("dispatchBatch", testCache->getAndClearLastErrorWebCacheMethodCall
ed()); | 378 EXPECT_EQ("dispatchBatch", testCache->getAndClearLastErrorWebCacheMethodCall
ed()); |
379 EXPECT_EQ(kNotImplementedString, getRejectString(stringDeleteResult)); | 379 EXPECT_EQ(kNotImplementedString, getRejectString(stringDeleteResult)); |
380 | 380 |
381 WebVector<WebServiceWorkerCache::BatchOperation> expectedPutOperations(size_
t(1)); | 381 WebVector<WebServiceWorkerCache::BatchOperation> expectedPutOperations(size_
t(1)); |
382 { | 382 { |
383 WebServiceWorkerCache::BatchOperation putOperation; | 383 WebServiceWorkerCache::BatchOperation putOperation; |
384 putOperation.operationType = WebServiceWorkerCache::OperationTypePut; | 384 putOperation.operationType = WebServiceWorkerCache::OperationTypePut; |
385 request->populateWebServiceWorkerRequest(putOperation.request); | 385 request->populateWebServiceWorkerRequest(putOperation.request); |
386 response->populateWebServiceWorkerResponse(putOperation.response); | 386 response->populateWebServiceWorkerResponse(putOperation.response); |
387 expectedPutOperations[0] = putOperation; | 387 expectedPutOperations[0] = putOperation; |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
420 TEST_F(ServiceWorkerCacheTest, MatchResponseTest) | 420 TEST_F(ServiceWorkerCacheTest, MatchResponseTest) |
421 { | 421 { |
422 const String requestUrl = "http://request.url/"; | 422 const String requestUrl = "http://request.url/"; |
423 const String responseUrl = "http://match.response.test/"; | 423 const String responseUrl = "http://match.response.test/"; |
424 | 424 |
425 WebServiceWorkerResponse webResponse; | 425 WebServiceWorkerResponse webResponse; |
426 webResponse.setURL(KURL(ParsedURLString, responseUrl)); | 426 webResponse.setURL(KURL(ParsedURLString, responseUrl)); |
427 webResponse.setResponseType(WebServiceWorkerResponseTypeDefault); | 427 webResponse.setResponseType(WebServiceWorkerResponseTypeDefault); |
428 | 428 |
429 Cache* cache = Cache::create(new MatchTestCache(webResponse)); | 429 Cache* cache = Cache::create(new MatchTestCache(webResponse)); |
430 CacheQueryOptions* options = CacheQueryOptions::create(); | 430 CacheQueryOptions options; |
431 | 431 |
432 ScriptPromise result = cache->match(scriptState(), requestUrl, *options); | 432 ScriptPromise result = cache->match(scriptState(), requestUrl, options); |
433 ScriptValue scriptValue = getResolveValue(result); | 433 ScriptValue scriptValue = getResolveValue(result); |
434 Response* response = V8Response::toImplWithTypeCheck(isolate(), scriptValue.
v8Value()); | 434 Response* response = V8Response::toImplWithTypeCheck(isolate(), scriptValue.
v8Value()); |
435 ASSERT_TRUE(response); | 435 ASSERT_TRUE(response); |
436 EXPECT_EQ(responseUrl, response->url()); | 436 EXPECT_EQ(responseUrl, response->url()); |
437 } | 437 } |
438 | 438 |
439 class KeysTestCache : public NotImplementedErrorCache { | 439 class KeysTestCache : public NotImplementedErrorCache { |
440 public: | 440 public: |
441 KeysTestCache(WebVector<WebServiceWorkerRequest>& requests) | 441 KeysTestCache(WebVector<WebServiceWorkerRequest>& requests) |
442 : m_requests(requests) { } | 442 : m_requests(requests) { } |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
511 expectedUrls[1] = url2; | 511 expectedUrls[1] = url2; |
512 | 512 |
513 WebVector<WebServiceWorkerResponse> webResponses(size_t(2)); | 513 WebVector<WebServiceWorkerResponse> webResponses(size_t(2)); |
514 webResponses[0].setURL(KURL(ParsedURLString, url1)); | 514 webResponses[0].setURL(KURL(ParsedURLString, url1)); |
515 webResponses[0].setResponseType(WebServiceWorkerResponseTypeDefault); | 515 webResponses[0].setResponseType(WebServiceWorkerResponseTypeDefault); |
516 webResponses[1].setURL(KURL(ParsedURLString, url2)); | 516 webResponses[1].setURL(KURL(ParsedURLString, url2)); |
517 webResponses[1].setResponseType(WebServiceWorkerResponseTypeDefault); | 517 webResponses[1].setResponseType(WebServiceWorkerResponseTypeDefault); |
518 | 518 |
519 Cache* cache = Cache::create(new MatchAllAndBatchTestCache(webResponses)); | 519 Cache* cache = Cache::create(new MatchAllAndBatchTestCache(webResponses)); |
520 | 520 |
521 CacheQueryOptions* options = CacheQueryOptions::create(); | 521 CacheQueryOptions options; |
522 ScriptPromise result = cache->matchAll(scriptState(), "http://some.url/", *o
ptions); | 522 ScriptPromise result = cache->matchAll(scriptState(), "http://some.url/", op
tions); |
523 ScriptValue scriptValue = getResolveValue(result); | 523 ScriptValue scriptValue = getResolveValue(result); |
524 | 524 |
525 NonThrowableExceptionState exceptionState; | 525 NonThrowableExceptionState exceptionState; |
526 Vector<v8::Handle<v8::Value> > responses = toImplArray<v8::Handle<v8::Value>
>(scriptValue.v8Value(), 0, isolate(), exceptionState); | 526 Vector<v8::Handle<v8::Value> > responses = toImplArray<v8::Handle<v8::Value>
>(scriptValue.v8Value(), 0, isolate(), exceptionState); |
527 EXPECT_EQ(expectedUrls.size(), responses.size()); | 527 EXPECT_EQ(expectedUrls.size(), responses.size()); |
528 for (int i = 0, minsize = std::min(expectedUrls.size(), responses.size()); i
< minsize; ++i) { | 528 for (int i = 0, minsize = std::min(expectedUrls.size(), responses.size()); i
< minsize; ++i) { |
529 Response* response = V8Response::toImplWithTypeCheck(isolate(), response
s[i]); | 529 Response* response = V8Response::toImplWithTypeCheck(isolate(), response
s[i]); |
530 EXPECT_TRUE(response); | 530 EXPECT_TRUE(response); |
531 if (response) | 531 if (response) |
532 EXPECT_EQ(expectedUrls[i], response->url()); | 532 EXPECT_EQ(expectedUrls[i], response->url()); |
533 } | 533 } |
534 | 534 |
535 result = cache->deleteFunction(scriptState(), "http://some.url/", *options); | 535 result = cache->deleteFunction(scriptState(), "http://some.url/", options); |
536 scriptValue = getResolveValue(result); | 536 scriptValue = getResolveValue(result); |
537 EXPECT_TRUE(scriptValue.v8Value()->IsBoolean()); | 537 EXPECT_TRUE(scriptValue.v8Value()->IsBoolean()); |
538 EXPECT_EQ(true, scriptValue.v8Value()->BooleanValue()); | 538 EXPECT_EQ(true, scriptValue.v8Value()->BooleanValue()); |
539 } | 539 } |
540 | 540 |
541 } // namespace | 541 } // namespace |
542 } // namespace blink | 542 } // namespace blink |
OLD | NEW |