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/cachestorage/Cache.h" | 6 #include "modules/cachestorage/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 15 matching lines...) Expand all Loading... |
26 #include <algorithm> | 26 #include <algorithm> |
27 #include <gtest/gtest.h> | 27 #include <gtest/gtest.h> |
28 #include <string> | 28 #include <string> |
29 | 29 |
30 namespace blink { | 30 namespace blink { |
31 | 31 |
32 namespace { | 32 namespace { |
33 | 33 |
34 const char kNotImplementedString[] = "NotSupportedError: Method is not implement
ed."; | 34 const char kNotImplementedString[] = "NotSupportedError: Method is not implement
ed."; |
35 | 35 |
36 class ScopedFetcherForTests final : public GlobalFetch::ScopedFetcher { | 36 class ScopedFetcherForTests final : public NoBaseWillBeGarbageCollectedFinalized
<ScopedFetcherForTests>, public GlobalFetch::ScopedFetcher { |
| 37 WILL_BE_USING_GARBAGE_COLLECTED_MIXIN(ScopedFetcherForTests); |
37 public: | 38 public: |
38 ScopedFetcherForTests() | 39 static PassOwnPtrWillBeRawPtr<ScopedFetcherForTests> create() |
39 : m_fetchCount(0) | |
40 , m_expectedUrl(nullptr) | |
41 , m_weakFactory(this) | |
42 { | 40 { |
| 41 return adoptPtrWillBeNoop(new ScopedFetcherForTests); |
43 } | 42 } |
44 | 43 |
45 ScriptPromise fetch(ScriptState* scriptState, const RequestInfo& requestInfo
, const Dictionary&, ExceptionState&) override | 44 ScriptPromise fetch(ScriptState* scriptState, const RequestInfo& requestInfo
, const Dictionary&, ExceptionState&) override |
46 { | 45 { |
47 ++m_fetchCount; | 46 ++m_fetchCount; |
48 if (m_expectedUrl) { | 47 if (m_expectedUrl) { |
49 String fetchedUrl; | 48 String fetchedUrl; |
50 if (requestInfo.isRequest()) | 49 if (requestInfo.isRequest()) |
51 EXPECT_EQ(*m_expectedUrl, requestInfo.getAsRequest()->url()); | 50 EXPECT_EQ(*m_expectedUrl, requestInfo.getAsRequest()->url()); |
52 else | 51 else |
53 EXPECT_EQ(*m_expectedUrl, requestInfo.getAsUSVString()); | 52 EXPECT_EQ(*m_expectedUrl, requestInfo.getAsUSVString()); |
54 } | 53 } |
55 | 54 |
56 if (m_response) { | 55 if (m_response) { |
57 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scri
ptState); | 56 ScriptPromiseResolver* resolver = ScriptPromiseResolver::create(scri
ptState); |
58 const ScriptPromise promise = resolver->promise(); | 57 const ScriptPromise promise = resolver->promise(); |
59 resolver->resolve(m_response); | 58 resolver->resolve(m_response); |
60 m_response = nullptr; | 59 m_response = nullptr; |
61 return promise; | 60 return promise; |
62 } | 61 } |
63 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr
ror(scriptState->isolate(), "Unexpected call to fetch, no response available."))
; | 62 return ScriptPromise::reject(scriptState, V8ThrowException::createTypeEr
ror(scriptState->isolate(), "Unexpected call to fetch, no response available."))
; |
64 } | 63 } |
65 | 64 |
66 WeakPtr<GlobalFetch::ScopedFetcher> weakPtr() | 65 WeakPtrWillBeRawPtr<GlobalFetch::ScopedFetcher> weakPtr() |
67 { | 66 { |
| 67 #if ENABLE(OILPAN) |
| 68 return this; |
| 69 #else |
68 return m_weakFactory.createWeakPtr(); | 70 return m_weakFactory.createWeakPtr(); |
| 71 #endif |
69 } | 72 } |
70 | 73 |
71 // This does not take ownership of its parameter. The provided sample object
is used to check the parameter when called. | 74 // This does not take ownership of its parameter. The provided sample object
is used to check the parameter when called. |
72 void setExpectedFetchUrl(const String* expectedUrl) { m_expectedUrl = expect
edUrl; } | 75 void setExpectedFetchUrl(const String* expectedUrl) { m_expectedUrl = expect
edUrl; } |
73 void setResponse(Response* response) { m_response = response; } | 76 void setResponse(Response* response) { m_response = response; } |
74 | 77 |
75 int fetchCount() const { return m_fetchCount; } | 78 int fetchCount() const { return m_fetchCount; } |
76 | 79 |
| 80 DEFINE_INLINE_TRACE() |
| 81 { |
| 82 visitor->trace(m_response); |
| 83 GlobalFetch::ScopedFetcher::trace(visitor); |
| 84 } |
| 85 |
77 private: | 86 private: |
| 87 ScopedFetcherForTests() |
| 88 : m_fetchCount(0) |
| 89 , m_expectedUrl(nullptr) |
| 90 #if !ENABLE(OILPAN) |
| 91 , m_weakFactory(this) |
| 92 #endif |
| 93 { |
| 94 } |
| 95 |
78 int m_fetchCount; | 96 int m_fetchCount; |
79 const String* m_expectedUrl; | 97 const String* m_expectedUrl; |
80 Persistent<Response> m_response; | 98 PersistentWillBeMember<Response> m_response; |
81 | 99 |
| 100 #if !ENABLE(OILPAN) |
82 WeakPtrFactory<GlobalFetch::ScopedFetcher> m_weakFactory; | 101 WeakPtrFactory<GlobalFetch::ScopedFetcher> m_weakFactory; |
| 102 #endif |
83 }; | 103 }; |
84 | 104 |
85 // A test implementation of the WebServiceWorkerCache interface which returns a
(provided) error for every operation, and optionally checks arguments | 105 // A test implementation of the WebServiceWorkerCache interface which returns a
(provided) error for every operation, and optionally checks arguments |
86 // to methods against provided arguments. Also used as a base class for test spe
cific caches. | 106 // to methods against provided arguments. Also used as a base class for test spe
cific caches. |
87 class ErrorWebCacheForTests : public WebServiceWorkerCache { | 107 class ErrorWebCacheForTests : public WebServiceWorkerCache { |
88 public: | 108 public: |
89 ErrorWebCacheForTests(const WebServiceWorkerCacheError error) | 109 ErrorWebCacheForTests(const WebServiceWorkerCacheError error) |
90 : m_error(error) | 110 : m_error(error) |
91 , m_expectedUrl(0) | 111 , m_expectedUrl(0) |
92 , m_expectedQueryParams(0) | 112 , m_expectedQueryParams(0) |
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
315 RequestInfo requestToRequestInfo(Request* value) | 335 RequestInfo requestToRequestInfo(Request* value) |
316 { | 336 { |
317 RequestInfo info; | 337 RequestInfo info; |
318 info.setRequest(value); | 338 info.setRequest(value); |
319 return info; | 339 return info; |
320 } | 340 } |
321 | 341 |
322 TEST_F(CacheStorageTest, Basics) | 342 TEST_F(CacheStorageTest, Basics) |
323 { | 343 { |
324 ScriptState::Scope scope(scriptState()); | 344 ScriptState::Scope scope(scriptState()); |
325 OwnPtr<ScopedFetcherForTests> fetcher = adoptPtr(new ScopedFetcherForTests()
); | 345 OwnPtrWillBeRawPtr<ScopedFetcherForTests> fetcher = ScopedFetcherForTests::c
reate(); |
326 ErrorWebCacheForTests* testCache; | 346 ErrorWebCacheForTests* testCache; |
327 Cache* cache = createCache(fetcher.get(), testCache = new NotImplementedErro
rCache()); | 347 Cache* cache = createCache(fetcher.get(), testCache = new NotImplementedErro
rCache()); |
328 ASSERT(cache); | 348 ASSERT(cache); |
329 | 349 |
330 const String url = "http://www.cachetest.org/"; | 350 const String url = "http://www.cachetest.org/"; |
331 | 351 |
332 CacheQueryOptions options; | 352 CacheQueryOptions options; |
333 ScriptPromise matchPromise = cache->match(scriptState(), stringToRequestInfo
(url), options, exceptionState()); | 353 ScriptPromise matchPromise = cache->match(scriptState(), stringToRequestInfo
(url), options, exceptionState()); |
334 EXPECT_EQ(kNotImplementedString, getRejectString(matchPromise)); | 354 EXPECT_EQ(kNotImplementedString, getRejectString(matchPromise)); |
335 | 355 |
336 cache = createCache(fetcher.get(), testCache = new ErrorWebCacheForTests(Web
ServiceWorkerCacheErrorNotFound)); | 356 cache = createCache(fetcher.get(), testCache = new ErrorWebCacheForTests(Web
ServiceWorkerCacheErrorNotFound)); |
337 matchPromise = cache->match(scriptState(), stringToRequestInfo(url), options
, exceptionState()); | 357 matchPromise = cache->match(scriptState(), stringToRequestInfo(url), options
, exceptionState()); |
338 ScriptValue scriptValue = getResolveValue(matchPromise); | 358 ScriptValue scriptValue = getResolveValue(matchPromise); |
339 EXPECT_TRUE(scriptValue.isUndefined()); | 359 EXPECT_TRUE(scriptValue.isUndefined()); |
340 | 360 |
341 cache = createCache(fetcher.get(), testCache = new ErrorWebCacheForTests(Web
ServiceWorkerCacheErrorExists)); | 361 cache = createCache(fetcher.get(), testCache = new ErrorWebCacheForTests(Web
ServiceWorkerCacheErrorExists)); |
342 matchPromise = cache->match(scriptState(), stringToRequestInfo(url), options
, exceptionState()); | 362 matchPromise = cache->match(scriptState(), stringToRequestInfo(url), options
, exceptionState()); |
343 EXPECT_EQ("InvalidAccessError: Entry already exists.", getRejectString(match
Promise)); | 363 EXPECT_EQ("InvalidAccessError: Entry already exists.", getRejectString(match
Promise)); |
344 } | 364 } |
345 | 365 |
346 // Tests that arguments are faithfully passed on calls to Cache methods, except
for methods which use batch operations, | 366 // Tests that arguments are faithfully passed on calls to Cache methods, except
for methods which use batch operations, |
347 // which are tested later. | 367 // which are tested later. |
348 TEST_F(CacheStorageTest, BasicArguments) | 368 TEST_F(CacheStorageTest, BasicArguments) |
349 { | 369 { |
350 ScriptState::Scope scope(scriptState()); | 370 ScriptState::Scope scope(scriptState()); |
351 OwnPtr<ScopedFetcherForTests> fetcher = adoptPtr(new ScopedFetcherForTests()
); | 371 OwnPtrWillBeRawPtr<ScopedFetcherForTests> fetcher = ScopedFetcherForTests::c
reate(); |
352 ErrorWebCacheForTests* testCache; | 372 ErrorWebCacheForTests* testCache; |
353 Cache* cache = createCache(fetcher.get(), testCache = new NotImplementedErro
rCache()); | 373 Cache* cache = createCache(fetcher.get(), testCache = new NotImplementedErro
rCache()); |
354 ASSERT(cache); | 374 ASSERT(cache); |
355 | 375 |
356 const String url = "http://www.cache.arguments.test/"; | 376 const String url = "http://www.cache.arguments.test/"; |
357 testCache->setExpectedUrl(&url); | 377 testCache->setExpectedUrl(&url); |
358 | 378 |
359 WebServiceWorkerCache::QueryParams expectedQueryParams; | 379 WebServiceWorkerCache::QueryParams expectedQueryParams; |
360 expectedQueryParams.ignoreVary = true; | 380 expectedQueryParams.ignoreVary = true; |
361 expectedQueryParams.cacheName = "this is a cache name"; | 381 expectedQueryParams.cacheName = "this is a cache name"; |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
397 | 417 |
398 ScriptPromise stringKeysResult2 = cache->keys(scriptState(), stringToRequest
Info(url), options, exceptionState()); | 418 ScriptPromise stringKeysResult2 = cache->keys(scriptState(), stringToRequest
Info(url), options, exceptionState()); |
399 EXPECT_EQ("dispatchKeys", testCache->getAndClearLastErrorWebCacheMethodCalle
d()); | 419 EXPECT_EQ("dispatchKeys", testCache->getAndClearLastErrorWebCacheMethodCalle
d()); |
400 EXPECT_EQ(kNotImplementedString, getRejectString(stringKeysResult2)); | 420 EXPECT_EQ(kNotImplementedString, getRejectString(stringKeysResult2)); |
401 } | 421 } |
402 | 422 |
403 // Tests that arguments are faithfully passed to API calls that degrade to batch
operations. | 423 // Tests that arguments are faithfully passed to API calls that degrade to batch
operations. |
404 TEST_F(CacheStorageTest, BatchOperationArguments) | 424 TEST_F(CacheStorageTest, BatchOperationArguments) |
405 { | 425 { |
406 ScriptState::Scope scope(scriptState()); | 426 ScriptState::Scope scope(scriptState()); |
407 OwnPtr<ScopedFetcherForTests> fetcher = adoptPtr(new ScopedFetcherForTests()
); | 427 OwnPtrWillBeRawPtr<ScopedFetcherForTests> fetcher = ScopedFetcherForTests::c
reate(); |
408 ErrorWebCacheForTests* testCache; | 428 ErrorWebCacheForTests* testCache; |
409 Cache* cache = createCache(fetcher.get(), testCache = new NotImplementedErro
rCache()); | 429 Cache* cache = createCache(fetcher.get(), testCache = new NotImplementedErro
rCache()); |
410 ASSERT(cache); | 430 ASSERT(cache); |
411 | 431 |
412 WebServiceWorkerCache::QueryParams expectedQueryParams; | 432 WebServiceWorkerCache::QueryParams expectedQueryParams; |
413 expectedQueryParams.cacheName = "this is another cache name"; | 433 expectedQueryParams.cacheName = "this is another cache name"; |
414 testCache->setExpectedQueryParams(&expectedQueryParams); | 434 testCache->setExpectedQueryParams(&expectedQueryParams); |
415 | 435 |
416 CacheQueryOptions options; | 436 CacheQueryOptions options; |
417 options.setCacheName(expectedQueryParams.cacheName); | 437 options.setCacheName(expectedQueryParams.cacheName); |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
477 return callbacks->onSuccess(m_response); | 497 return callbacks->onSuccess(m_response); |
478 } | 498 } |
479 | 499 |
480 private: | 500 private: |
481 WebServiceWorkerResponse& m_response; | 501 WebServiceWorkerResponse& m_response; |
482 }; | 502 }; |
483 | 503 |
484 TEST_F(CacheStorageTest, MatchResponseTest) | 504 TEST_F(CacheStorageTest, MatchResponseTest) |
485 { | 505 { |
486 ScriptState::Scope scope(scriptState()); | 506 ScriptState::Scope scope(scriptState()); |
487 OwnPtr<ScopedFetcherForTests> fetcher = adoptPtr(new ScopedFetcherForTests()
); | 507 OwnPtrWillBeRawPtr<ScopedFetcherForTests> fetcher = ScopedFetcherForTests::c
reate(); |
488 const String requestUrl = "http://request.url/"; | 508 const String requestUrl = "http://request.url/"; |
489 const String responseUrl = "http://match.response.test/"; | 509 const String responseUrl = "http://match.response.test/"; |
490 | 510 |
491 WebServiceWorkerResponse webResponse; | 511 WebServiceWorkerResponse webResponse; |
492 webResponse.setURL(KURL(ParsedURLString, responseUrl)); | 512 webResponse.setURL(KURL(ParsedURLString, responseUrl)); |
493 webResponse.setResponseType(WebServiceWorkerResponseTypeDefault); | 513 webResponse.setResponseType(WebServiceWorkerResponseTypeDefault); |
494 | 514 |
495 Cache* cache = createCache(fetcher.get(), new MatchTestCache(webResponse)); | 515 Cache* cache = createCache(fetcher.get(), new MatchTestCache(webResponse)); |
496 CacheQueryOptions options; | 516 CacheQueryOptions options; |
497 | 517 |
(...skipping 15 matching lines...) Expand all Loading... |
513 return callbacks->onSuccess(m_requests); | 533 return callbacks->onSuccess(m_requests); |
514 } | 534 } |
515 | 535 |
516 private: | 536 private: |
517 WebVector<WebServiceWorkerRequest>& m_requests; | 537 WebVector<WebServiceWorkerRequest>& m_requests; |
518 }; | 538 }; |
519 | 539 |
520 TEST_F(CacheStorageTest, KeysResponseTest) | 540 TEST_F(CacheStorageTest, KeysResponseTest) |
521 { | 541 { |
522 ScriptState::Scope scope(scriptState()); | 542 ScriptState::Scope scope(scriptState()); |
523 OwnPtr<ScopedFetcherForTests> fetcher = adoptPtr(new ScopedFetcherForTests()
); | 543 OwnPtrWillBeRawPtr<ScopedFetcherForTests> fetcher = ScopedFetcherForTests::c
reate(); |
524 const String url1 = "http://first.request/"; | 544 const String url1 = "http://first.request/"; |
525 const String url2 = "http://second.request/"; | 545 const String url2 = "http://second.request/"; |
526 | 546 |
527 Vector<String> expectedUrls(size_t(2)); | 547 Vector<String> expectedUrls(size_t(2)); |
528 expectedUrls[0] = url1; | 548 expectedUrls[0] = url1; |
529 expectedUrls[1] = url2; | 549 expectedUrls[1] = url2; |
530 | 550 |
531 WebVector<WebServiceWorkerRequest> webRequests(size_t(2)); | 551 WebVector<WebServiceWorkerRequest> webRequests(size_t(2)); |
532 webRequests[0].setURL(KURL(ParsedURLString, url1)); | 552 webRequests[0].setURL(KURL(ParsedURLString, url1)); |
533 webRequests[1].setURL(KURL(ParsedURLString, url2)); | 553 webRequests[1].setURL(KURL(ParsedURLString, url2)); |
(...skipping 30 matching lines...) Expand all Loading... |
564 return callbacks->onSuccess(); | 584 return callbacks->onSuccess(); |
565 } | 585 } |
566 | 586 |
567 private: | 587 private: |
568 WebVector<WebServiceWorkerResponse>& m_responses; | 588 WebVector<WebServiceWorkerResponse>& m_responses; |
569 }; | 589 }; |
570 | 590 |
571 TEST_F(CacheStorageTest, MatchAllAndBatchResponseTest) | 591 TEST_F(CacheStorageTest, MatchAllAndBatchResponseTest) |
572 { | 592 { |
573 ScriptState::Scope scope(scriptState()); | 593 ScriptState::Scope scope(scriptState()); |
574 OwnPtr<ScopedFetcherForTests> fetcher = adoptPtr(new ScopedFetcherForTests()
); | 594 OwnPtrWillBeRawPtr<ScopedFetcherForTests> fetcher = ScopedFetcherForTests::c
reate(); |
575 const String url1 = "http://first.response/"; | 595 const String url1 = "http://first.response/"; |
576 const String url2 = "http://second.response/"; | 596 const String url2 = "http://second.response/"; |
577 | 597 |
578 Vector<String> expectedUrls(size_t(2)); | 598 Vector<String> expectedUrls(size_t(2)); |
579 expectedUrls[0] = url1; | 599 expectedUrls[0] = url1; |
580 expectedUrls[1] = url2; | 600 expectedUrls[1] = url2; |
581 | 601 |
582 WebVector<WebServiceWorkerResponse> webResponses(size_t(2)); | 602 WebVector<WebServiceWorkerResponse> webResponses(size_t(2)); |
583 webResponses[0].setURL(KURL(ParsedURLString, url1)); | 603 webResponses[0].setURL(KURL(ParsedURLString, url1)); |
584 webResponses[0].setResponseType(WebServiceWorkerResponseTypeDefault); | 604 webResponses[0].setResponseType(WebServiceWorkerResponseTypeDefault); |
(...skipping 17 matching lines...) Expand all Loading... |
602 | 622 |
603 result = cache->deleteFunction(scriptState(), stringToRequestInfo("http://so
me.url/"), options, exceptionState()); | 623 result = cache->deleteFunction(scriptState(), stringToRequestInfo("http://so
me.url/"), options, exceptionState()); |
604 scriptValue = getResolveValue(result); | 624 scriptValue = getResolveValue(result); |
605 EXPECT_TRUE(scriptValue.v8Value()->IsBoolean()); | 625 EXPECT_TRUE(scriptValue.v8Value()->IsBoolean()); |
606 EXPECT_EQ(true, scriptValue.v8Value().As<v8::Boolean>()->Value()); | 626 EXPECT_EQ(true, scriptValue.v8Value().As<v8::Boolean>()->Value()); |
607 } | 627 } |
608 | 628 |
609 TEST_F(CacheStorageTest, Add) | 629 TEST_F(CacheStorageTest, Add) |
610 { | 630 { |
611 ScriptState::Scope scope(scriptState()); | 631 ScriptState::Scope scope(scriptState()); |
612 OwnPtr<ScopedFetcherForTests> fetcher = adoptPtr(new ScopedFetcherForTests()
); | 632 OwnPtrWillBeRawPtr<ScopedFetcherForTests> fetcher = ScopedFetcherForTests::c
reate(); |
613 const String url = "http://www.cacheadd.test/"; | 633 const String url = "http://www.cacheadd.test/"; |
614 const KURL kurl(ParsedURLString, url); | 634 const KURL kurl(ParsedURLString, url); |
615 | 635 |
616 ErrorWebCacheForTests* testCache; | 636 ErrorWebCacheForTests* testCache; |
617 Cache* cache = createCache(fetcher.get(), testCache = new NotImplementedErro
rCache()); | 637 Cache* cache = createCache(fetcher.get(), testCache = new NotImplementedErro
rCache()); |
618 | 638 |
619 fetcher->setExpectedFetchUrl(&url); | 639 fetcher->setExpectedFetchUrl(&url); |
620 | 640 |
621 Request* request = newRequestFromUrl(url); | 641 Request* request = newRequestFromUrl(url); |
622 WebServiceWorkerResponse webResponse; | 642 WebServiceWorkerResponse webResponse; |
(...skipping 13 matching lines...) Expand all Loading... |
636 | 656 |
637 ScriptPromise addResult = cache->add(scriptState(), requestToRequestInfo(req
uest), exceptionState()); | 657 ScriptPromise addResult = cache->add(scriptState(), requestToRequestInfo(req
uest), exceptionState()); |
638 | 658 |
639 EXPECT_EQ(kNotImplementedString, getRejectString(addResult)); | 659 EXPECT_EQ(kNotImplementedString, getRejectString(addResult)); |
640 EXPECT_EQ(1, fetcher->fetchCount()); | 660 EXPECT_EQ(1, fetcher->fetchCount()); |
641 EXPECT_EQ("dispatchBatch", testCache->getAndClearLastErrorWebCacheMethodCall
ed()); | 661 EXPECT_EQ("dispatchBatch", testCache->getAndClearLastErrorWebCacheMethodCall
ed()); |
642 } | 662 } |
643 | 663 |
644 } // namespace | 664 } // namespace |
645 } // namespace blink | 665 } // namespace blink |
OLD | NEW |