| 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 "modules/cachestorage/Cache.h" | 5 #include "modules/cachestorage/Cache.h" |
| 6 | 6 |
| 7 #include "bindings/core/v8/ExceptionState.h" | 7 #include "bindings/core/v8/ExceptionState.h" |
| 8 #include "bindings/core/v8/ScriptFunction.h" | 8 #include "bindings/core/v8/ScriptFunction.h" |
| 9 #include "bindings/core/v8/ScriptPromise.h" | 9 #include "bindings/core/v8/ScriptPromise.h" |
| 10 #include "bindings/core/v8/ScriptPromiseResolver.h" | 10 #include "bindings/core/v8/ScriptPromiseResolver.h" |
| 11 #include "bindings/core/v8/ScriptValue.h" | 11 #include "bindings/core/v8/ScriptValue.h" |
| 12 #include "bindings/core/v8/V8Binding.h" | 12 #include "bindings/core/v8/V8Binding.h" |
| 13 #include "bindings/modules/v8/V8Request.h" | 13 #include "bindings/modules/v8/V8Request.h" |
| 14 #include "bindings/modules/v8/V8Response.h" | 14 #include "bindings/modules/v8/V8Response.h" |
| 15 #include "core/dom/Document.h" | 15 #include "core/dom/Document.h" |
| 16 #include "core/frame/Frame.h" | 16 #include "core/frame/Frame.h" |
| 17 #include "core/testing/DummyPageHolder.h" | 17 #include "core/testing/DummyPageHolder.h" |
| 18 #include "modules/fetch/BodyStreamBuffer.h" | 18 #include "modules/fetch/BodyStreamBuffer.h" |
| 19 #include "modules/fetch/FetchFormDataConsumerHandle.h" | 19 #include "modules/fetch/FetchFormDataConsumerHandle.h" |
| 20 #include "modules/fetch/GlobalFetch.h" | 20 #include "modules/fetch/GlobalFetch.h" |
| 21 #include "modules/fetch/Request.h" | 21 #include "modules/fetch/Request.h" |
| 22 #include "modules/fetch/Response.h" | 22 #include "modules/fetch/Response.h" |
| 23 #include "modules/fetch/ResponseInit.h" | 23 #include "modules/fetch/ResponseInit.h" |
| 24 #include "public/platform/WebURLResponse.h" | 24 #include "public/platform/WebURLResponse.h" |
| 25 #include "public/platform/modules/serviceworker/WebServiceWorkerCache.h" | 25 #include "public/platform/modules/serviceworker/WebServiceWorkerCache.h" |
| 26 #include "testing/gtest/include/gtest/gtest.h" | 26 #include "testing/gtest/include/gtest/gtest.h" |
| 27 #include "wtf/OwnPtr.h" | 27 #include "wtf/PtrUtil.h" |
| 28 | |
| 29 #include <algorithm> | 28 #include <algorithm> |
| 29 #include <memory> |
| 30 #include <string> | 30 #include <string> |
| 31 | 31 |
| 32 namespace blink { | 32 namespace blink { |
| 33 | 33 |
| 34 namespace { | 34 namespace { |
| 35 | 35 |
| 36 const char kNotImplementedString[] = "NotSupportedError: Method is not implement
ed."; | 36 const char kNotImplementedString[] = "NotSupportedError: Method is not implement
ed."; |
| 37 | 37 |
| 38 class ScopedFetcherForTests final : public GarbageCollectedFinalized<ScopedFetch
erForTests>, public GlobalFetch::ScopedFetcher { | 38 class ScopedFetcherForTests final : public GarbageCollectedFinalized<ScopedFetch
erForTests>, public GlobalFetch::ScopedFetcher { |
| 39 USING_GARBAGE_COLLECTED_MIXIN(ScopedFetcherForTests); | 39 USING_GARBAGE_COLLECTED_MIXIN(ScopedFetcherForTests); |
| (...skipping 70 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 110 void setExpectedQueryParams(const QueryParams* expectedQueryParams) { m_expe
ctedQueryParams = expectedQueryParams; } | 110 void setExpectedQueryParams(const QueryParams* expectedQueryParams) { m_expe
ctedQueryParams = expectedQueryParams; } |
| 111 void setExpectedBatchOperations(const WebVector<BatchOperation>* expectedBat
chOperations) { m_expectedBatchOperations = expectedBatchOperations; } | 111 void setExpectedBatchOperations(const WebVector<BatchOperation>* expectedBat
chOperations) { m_expectedBatchOperations = expectedBatchOperations; } |
| 112 | 112 |
| 113 // From WebServiceWorkerCache: | 113 // From WebServiceWorkerCache: |
| 114 void dispatchMatch(CacheMatchCallbacks* callbacks, const WebServiceWorkerReq
uest& webRequest, const QueryParams& queryParams) override | 114 void dispatchMatch(CacheMatchCallbacks* callbacks, const WebServiceWorkerReq
uest& webRequest, const QueryParams& queryParams) override |
| 115 { | 115 { |
| 116 m_lastErrorWebCacheMethodCalled = "dispatchMatch"; | 116 m_lastErrorWebCacheMethodCalled = "dispatchMatch"; |
| 117 checkUrlIfProvided(webRequest.url()); | 117 checkUrlIfProvided(webRequest.url()); |
| 118 checkQueryParamsIfProvided(queryParams); | 118 checkQueryParamsIfProvided(queryParams); |
| 119 | 119 |
| 120 OwnPtr<CacheMatchCallbacks> ownedCallbacks(adoptPtr(callbacks)); | 120 std::unique_ptr<CacheMatchCallbacks> ownedCallbacks(wrapUnique(callbacks
)); |
| 121 return callbacks->onError(m_error); | 121 return callbacks->onError(m_error); |
| 122 } | 122 } |
| 123 | 123 |
| 124 void dispatchMatchAll(CacheWithResponsesCallbacks* callbacks, const WebServi
ceWorkerRequest& webRequest, const QueryParams& queryParams) override | 124 void dispatchMatchAll(CacheWithResponsesCallbacks* callbacks, const WebServi
ceWorkerRequest& webRequest, const QueryParams& queryParams) override |
| 125 { | 125 { |
| 126 m_lastErrorWebCacheMethodCalled = "dispatchMatchAll"; | 126 m_lastErrorWebCacheMethodCalled = "dispatchMatchAll"; |
| 127 checkUrlIfProvided(webRequest.url()); | 127 checkUrlIfProvided(webRequest.url()); |
| 128 checkQueryParamsIfProvided(queryParams); | 128 checkQueryParamsIfProvided(queryParams); |
| 129 | 129 |
| 130 OwnPtr<CacheWithResponsesCallbacks> ownedCallbacks(adoptPtr(callbacks)); | 130 std::unique_ptr<CacheWithResponsesCallbacks> ownedCallbacks(wrapUnique(c
allbacks)); |
| 131 return callbacks->onError(m_error); | 131 return callbacks->onError(m_error); |
| 132 } | 132 } |
| 133 | 133 |
| 134 void dispatchKeys(CacheWithRequestsCallbacks* callbacks, const WebServiceWor
kerRequest* webRequest, const QueryParams& queryParams) override | 134 void dispatchKeys(CacheWithRequestsCallbacks* callbacks, const WebServiceWor
kerRequest* webRequest, const QueryParams& queryParams) override |
| 135 { | 135 { |
| 136 m_lastErrorWebCacheMethodCalled = "dispatchKeys"; | 136 m_lastErrorWebCacheMethodCalled = "dispatchKeys"; |
| 137 if (webRequest) { | 137 if (webRequest) { |
| 138 checkUrlIfProvided(webRequest->url()); | 138 checkUrlIfProvided(webRequest->url()); |
| 139 checkQueryParamsIfProvided(queryParams); | 139 checkQueryParamsIfProvided(queryParams); |
| 140 } | 140 } |
| 141 | 141 |
| 142 OwnPtr<CacheWithRequestsCallbacks> ownedCallbacks(adoptPtr(callbacks)); | 142 std::unique_ptr<CacheWithRequestsCallbacks> ownedCallbacks(wrapUnique(ca
llbacks)); |
| 143 return callbacks->onError(m_error); | 143 return callbacks->onError(m_error); |
| 144 } | 144 } |
| 145 | 145 |
| 146 void dispatchBatch(CacheBatchCallbacks* callbacks, const WebVector<BatchOper
ation>& batchOperations) override | 146 void dispatchBatch(CacheBatchCallbacks* callbacks, const WebVector<BatchOper
ation>& batchOperations) override |
| 147 { | 147 { |
| 148 m_lastErrorWebCacheMethodCalled = "dispatchBatch"; | 148 m_lastErrorWebCacheMethodCalled = "dispatchBatch"; |
| 149 checkBatchOperationsIfProvided(batchOperations); | 149 checkBatchOperationsIfProvided(batchOperations); |
| 150 | 150 |
| 151 OwnPtr<CacheBatchCallbacks> ownedCallbacks(adoptPtr(callbacks)); | 151 std::unique_ptr<CacheBatchCallbacks> ownedCallbacks(wrapUnique(callbacks
)); |
| 152 return callbacks->onError(m_error); | 152 return callbacks->onError(m_error); |
| 153 } | 153 } |
| 154 | 154 |
| 155 protected: | 155 protected: |
| 156 void checkUrlIfProvided(const KURL& url) | 156 void checkUrlIfProvided(const KURL& url) |
| 157 { | 157 { |
| 158 if (!m_expectedUrl) | 158 if (!m_expectedUrl) |
| 159 return; | 159 return; |
| 160 EXPECT_EQ(*m_expectedUrl, url); | 160 EXPECT_EQ(*m_expectedUrl, url); |
| 161 } | 161 } |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 206 NotImplementedErrorCache() : ErrorWebCacheForTests(WebServiceWorkerCacheErro
rNotImplemented) { } | 206 NotImplementedErrorCache() : ErrorWebCacheForTests(WebServiceWorkerCacheErro
rNotImplemented) { } |
| 207 }; | 207 }; |
| 208 | 208 |
| 209 class CacheStorageTest : public ::testing::Test { | 209 class CacheStorageTest : public ::testing::Test { |
| 210 public: | 210 public: |
| 211 CacheStorageTest() | 211 CacheStorageTest() |
| 212 : m_page(DummyPageHolder::create(IntSize(1, 1))) { } | 212 : m_page(DummyPageHolder::create(IntSize(1, 1))) { } |
| 213 | 213 |
| 214 Cache* createCache(ScopedFetcherForTests* fetcher, WebServiceWorkerCache* we
bCache) | 214 Cache* createCache(ScopedFetcherForTests* fetcher, WebServiceWorkerCache* we
bCache) |
| 215 { | 215 { |
| 216 return Cache::create(fetcher, adoptPtr(webCache)); | 216 return Cache::create(fetcher, wrapUnique(webCache)); |
| 217 } | 217 } |
| 218 | 218 |
| 219 ScriptState* getScriptState() { return ScriptState::forMainWorld(m_page->doc
ument().frame()); } | 219 ScriptState* getScriptState() { return ScriptState::forMainWorld(m_page->doc
ument().frame()); } |
| 220 ExecutionContext* getExecutionContext() { return getScriptState()->getExecut
ionContext(); } | 220 ExecutionContext* getExecutionContext() { return getScriptState()->getExecut
ionContext(); } |
| 221 v8::Isolate* isolate() { return getScriptState()->isolate(); } | 221 v8::Isolate* isolate() { return getScriptState()->isolate(); } |
| 222 v8::Local<v8::Context> context() { return getScriptState()->context(); } | 222 v8::Local<v8::Context> context() { return getScriptState()->context(); } |
| 223 | 223 |
| 224 Request* newRequestFromUrl(const String& url) | 224 Request* newRequestFromUrl(const String& url) |
| 225 { | 225 { |
| 226 TrackExceptionState exceptionState; | 226 TrackExceptionState exceptionState; |
| (...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 299 return value; | 299 return value; |
| 300 } | 300 } |
| 301 | 301 |
| 302 private: | 302 private: |
| 303 TestFunction(ScriptState* scriptState, ScriptValue* outValue) : ScriptFu
nction(scriptState), m_value(outValue) { } | 303 TestFunction(ScriptState* scriptState, ScriptValue* outValue) : ScriptFu
nction(scriptState), m_value(outValue) { } |
| 304 | 304 |
| 305 ScriptValue* m_value; | 305 ScriptValue* m_value; |
| 306 }; | 306 }; |
| 307 | 307 |
| 308 // Lifetime is that of the text fixture. | 308 // Lifetime is that of the text fixture. |
| 309 OwnPtr<DummyPageHolder> m_page; | 309 std::unique_ptr<DummyPageHolder> m_page; |
| 310 | 310 |
| 311 NonThrowableExceptionState m_exceptionState; | 311 NonThrowableExceptionState m_exceptionState; |
| 312 }; | 312 }; |
| 313 | 313 |
| 314 RequestInfo stringToRequestInfo(const String& value) | 314 RequestInfo stringToRequestInfo(const String& value) |
| 315 { | 315 { |
| 316 RequestInfo info; | 316 RequestInfo info; |
| 317 info.setUSVString(value); | 317 info.setUSVString(value); |
| 318 return info; | 318 return info; |
| 319 } | 319 } |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 472 } | 472 } |
| 473 | 473 |
| 474 class MatchTestCache : public NotImplementedErrorCache { | 474 class MatchTestCache : public NotImplementedErrorCache { |
| 475 public: | 475 public: |
| 476 MatchTestCache(WebServiceWorkerResponse& response) | 476 MatchTestCache(WebServiceWorkerResponse& response) |
| 477 : m_response(response) { } | 477 : m_response(response) { } |
| 478 | 478 |
| 479 // From WebServiceWorkerCache: | 479 // From WebServiceWorkerCache: |
| 480 void dispatchMatch(CacheMatchCallbacks* callbacks, const WebServiceWorkerReq
uest& webRequest, const QueryParams& queryParams) override | 480 void dispatchMatch(CacheMatchCallbacks* callbacks, const WebServiceWorkerReq
uest& webRequest, const QueryParams& queryParams) override |
| 481 { | 481 { |
| 482 OwnPtr<CacheMatchCallbacks> ownedCallbacks(adoptPtr(callbacks)); | 482 std::unique_ptr<CacheMatchCallbacks> ownedCallbacks(wrapUnique(callbacks
)); |
| 483 return callbacks->onSuccess(m_response); | 483 return callbacks->onSuccess(m_response); |
| 484 } | 484 } |
| 485 | 485 |
| 486 private: | 486 private: |
| 487 WebServiceWorkerResponse& m_response; | 487 WebServiceWorkerResponse& m_response; |
| 488 }; | 488 }; |
| 489 | 489 |
| 490 TEST_F(CacheStorageTest, MatchResponseTest) | 490 TEST_F(CacheStorageTest, MatchResponseTest) |
| 491 { | 491 { |
| 492 ScriptState::Scope scope(getScriptState()); | 492 ScriptState::Scope scope(getScriptState()); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 508 EXPECT_EQ(responseUrl, response->url()); | 508 EXPECT_EQ(responseUrl, response->url()); |
| 509 } | 509 } |
| 510 | 510 |
| 511 class KeysTestCache : public NotImplementedErrorCache { | 511 class KeysTestCache : public NotImplementedErrorCache { |
| 512 public: | 512 public: |
| 513 KeysTestCache(WebVector<WebServiceWorkerRequest>& requests) | 513 KeysTestCache(WebVector<WebServiceWorkerRequest>& requests) |
| 514 : m_requests(requests) { } | 514 : m_requests(requests) { } |
| 515 | 515 |
| 516 void dispatchKeys(CacheWithRequestsCallbacks* callbacks, const WebServiceWor
kerRequest* webRequest, const QueryParams& queryParams) override | 516 void dispatchKeys(CacheWithRequestsCallbacks* callbacks, const WebServiceWor
kerRequest* webRequest, const QueryParams& queryParams) override |
| 517 { | 517 { |
| 518 OwnPtr<CacheWithRequestsCallbacks> ownedCallbacks(adoptPtr(callbacks)); | 518 std::unique_ptr<CacheWithRequestsCallbacks> ownedCallbacks(wrapUnique(ca
llbacks)); |
| 519 return callbacks->onSuccess(m_requests); | 519 return callbacks->onSuccess(m_requests); |
| 520 } | 520 } |
| 521 | 521 |
| 522 private: | 522 private: |
| 523 WebVector<WebServiceWorkerRequest>& m_requests; | 523 WebVector<WebServiceWorkerRequest>& m_requests; |
| 524 }; | 524 }; |
| 525 | 525 |
| 526 TEST_F(CacheStorageTest, KeysResponseTest) | 526 TEST_F(CacheStorageTest, KeysResponseTest) |
| 527 { | 527 { |
| 528 ScriptState::Scope scope(getScriptState()); | 528 ScriptState::Scope scope(getScriptState()); |
| (...skipping 24 matching lines...) Expand all Loading... |
| 553 } | 553 } |
| 554 } | 554 } |
| 555 | 555 |
| 556 class MatchAllAndBatchTestCache : public NotImplementedErrorCache { | 556 class MatchAllAndBatchTestCache : public NotImplementedErrorCache { |
| 557 public: | 557 public: |
| 558 MatchAllAndBatchTestCache(WebVector<WebServiceWorkerResponse>& responses) | 558 MatchAllAndBatchTestCache(WebVector<WebServiceWorkerResponse>& responses) |
| 559 : m_responses(responses) { } | 559 : m_responses(responses) { } |
| 560 | 560 |
| 561 void dispatchMatchAll(CacheWithResponsesCallbacks* callbacks, const WebServi
ceWorkerRequest& webRequest, const QueryParams& queryParams) override | 561 void dispatchMatchAll(CacheWithResponsesCallbacks* callbacks, const WebServi
ceWorkerRequest& webRequest, const QueryParams& queryParams) override |
| 562 { | 562 { |
| 563 OwnPtr<CacheWithResponsesCallbacks> ownedCallbacks(adoptPtr(callbacks)); | 563 std::unique_ptr<CacheWithResponsesCallbacks> ownedCallbacks(wrapUnique(c
allbacks)); |
| 564 return callbacks->onSuccess(m_responses); | 564 return callbacks->onSuccess(m_responses); |
| 565 } | 565 } |
| 566 | 566 |
| 567 void dispatchBatch(CacheBatchCallbacks* callbacks, const WebVector<BatchOper
ation>& batchOperations) override | 567 void dispatchBatch(CacheBatchCallbacks* callbacks, const WebVector<BatchOper
ation>& batchOperations) override |
| 568 { | 568 { |
| 569 OwnPtr<CacheBatchCallbacks> ownedCallbacks(adoptPtr(callbacks)); | 569 std::unique_ptr<CacheBatchCallbacks> ownedCallbacks(wrapUnique(callbacks
)); |
| 570 return callbacks->onSuccess(); | 570 return callbacks->onSuccess(); |
| 571 } | 571 } |
| 572 | 572 |
| 573 private: | 573 private: |
| 574 WebVector<WebServiceWorkerResponse>& m_responses; | 574 WebVector<WebServiceWorkerResponse>& m_responses; |
| 575 }; | 575 }; |
| 576 | 576 |
| 577 TEST_F(CacheStorageTest, MatchAllAndBatchResponseTest) | 577 TEST_F(CacheStorageTest, MatchAllAndBatchResponseTest) |
| 578 { | 578 { |
| 579 ScriptState::Scope scope(getScriptState()); | 579 ScriptState::Scope scope(getScriptState()); |
| (...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 641 | 641 |
| 642 ScriptPromise addResult = cache->add(getScriptState(), requestToRequestInfo(
request), exceptionState()); | 642 ScriptPromise addResult = cache->add(getScriptState(), requestToRequestInfo(
request), exceptionState()); |
| 643 | 643 |
| 644 EXPECT_EQ(kNotImplementedString, getRejectString(addResult)); | 644 EXPECT_EQ(kNotImplementedString, getRejectString(addResult)); |
| 645 EXPECT_EQ(1, fetcher->fetchCount()); | 645 EXPECT_EQ(1, fetcher->fetchCount()); |
| 646 EXPECT_EQ("dispatchBatch", testCache->getAndClearLastErrorWebCacheMethodCall
ed()); | 646 EXPECT_EQ("dispatchBatch", testCache->getAndClearLastErrorWebCacheMethodCall
ed()); |
| 647 } | 647 } |
| 648 | 648 |
| 649 } // namespace | 649 } // namespace |
| 650 } // namespace blink | 650 } // namespace blink |
| OLD | NEW |