| 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" |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 64 resolver->resolve(m_response); | 64 resolver->resolve(m_response); |
| 65 m_response = nullptr; | 65 m_response = nullptr; |
| 66 return promise; | 66 return promise; |
| 67 } | 67 } |
| 68 return ScriptPromise::reject( | 68 return ScriptPromise::reject( |
| 69 scriptState, V8ThrowException::createTypeError( | 69 scriptState, V8ThrowException::createTypeError( |
| 70 scriptState->isolate(), | 70 scriptState->isolate(), |
| 71 "Unexpected call to fetch, no response available.")); | 71 "Unexpected call to fetch, no response available.")); |
| 72 } | 72 } |
| 73 | 73 |
| 74 // This does not take ownership of its parameter. The provided sample object i
s used to check the parameter when called. | 74 // This does not take ownership of its parameter. The provided sample object |
| 75 // is used to check the parameter when called. |
| 75 void setExpectedFetchUrl(const String* expectedUrl) { | 76 void setExpectedFetchUrl(const String* expectedUrl) { |
| 76 m_expectedUrl = expectedUrl; | 77 m_expectedUrl = expectedUrl; |
| 77 } | 78 } |
| 78 void setResponse(Response* response) { m_response = response; } | 79 void setResponse(Response* response) { m_response = response; } |
| 79 | 80 |
| 80 int fetchCount() const { return m_fetchCount; } | 81 int fetchCount() const { return m_fetchCount; } |
| 81 | 82 |
| 82 DEFINE_INLINE_TRACE() { | 83 DEFINE_INLINE_TRACE() { |
| 83 visitor->trace(m_response); | 84 visitor->trace(m_response); |
| 84 GlobalFetch::ScopedFetcher::trace(visitor); | 85 GlobalFetch::ScopedFetcher::trace(visitor); |
| 85 } | 86 } |
| 86 | 87 |
| 87 private: | 88 private: |
| 88 ScopedFetcherForTests() : m_fetchCount(0), m_expectedUrl(nullptr) {} | 89 ScopedFetcherForTests() : m_fetchCount(0), m_expectedUrl(nullptr) {} |
| 89 | 90 |
| 90 int m_fetchCount; | 91 int m_fetchCount; |
| 91 const String* m_expectedUrl; | 92 const String* m_expectedUrl; |
| 92 Member<Response> m_response; | 93 Member<Response> m_response; |
| 93 }; | 94 }; |
| 94 | 95 |
| 95 // A test implementation of the WebServiceWorkerCache interface which returns a
(provided) error for every operation, and optionally checks arguments | 96 // A test implementation of the WebServiceWorkerCache interface which returns a |
| 96 // to methods against provided arguments. Also used as a base class for test spe
cific caches. | 97 // (provided) error for every operation, and optionally checks arguments to |
| 98 // methods against provided arguments. Also used as a base class for test |
| 99 // specific caches. |
| 97 class ErrorWebCacheForTests : public WebServiceWorkerCache { | 100 class ErrorWebCacheForTests : public WebServiceWorkerCache { |
| 98 public: | 101 public: |
| 99 ErrorWebCacheForTests(const WebServiceWorkerCacheError error) | 102 ErrorWebCacheForTests(const WebServiceWorkerCacheError error) |
| 100 : m_error(error), | 103 : m_error(error), |
| 101 m_expectedUrl(0), | 104 m_expectedUrl(0), |
| 102 m_expectedQueryParams(0), | 105 m_expectedQueryParams(0), |
| 103 m_expectedBatchOperations(0) {} | 106 m_expectedBatchOperations(0) {} |
| 104 | 107 |
| 105 std::string getAndClearLastErrorWebCacheMethodCalled() { | 108 std::string getAndClearLastErrorWebCacheMethodCalled() { |
| 106 std::string old = m_lastErrorWebCacheMethodCalled; | 109 std::string old = m_lastErrorWebCacheMethodCalled; |
| 107 m_lastErrorWebCacheMethodCalled.clear(); | 110 m_lastErrorWebCacheMethodCalled.clear(); |
| 108 return old; | 111 return old; |
| 109 } | 112 } |
| 110 | 113 |
| 111 // These methods do not take ownership of their parameter. They provide an opt
ional sample object to check parameters against. | 114 // These methods do not take ownership of their parameter. They provide an |
| 115 // optional sample object to check parameters against. |
| 112 void setExpectedUrl(const String* expectedUrl) { | 116 void setExpectedUrl(const String* expectedUrl) { |
| 113 m_expectedUrl = expectedUrl; | 117 m_expectedUrl = expectedUrl; |
| 114 } | 118 } |
| 115 void setExpectedQueryParams(const QueryParams* expectedQueryParams) { | 119 void setExpectedQueryParams(const QueryParams* expectedQueryParams) { |
| 116 m_expectedQueryParams = expectedQueryParams; | 120 m_expectedQueryParams = expectedQueryParams; |
| 117 } | 121 } |
| 118 void setExpectedBatchOperations( | 122 void setExpectedBatchOperations( |
| 119 const WebVector<BatchOperation>* expectedBatchOperations) { | 123 const WebVector<BatchOperation>* expectedBatchOperations) { |
| 120 m_expectedBatchOperations = expectedBatchOperations; | 124 m_expectedBatchOperations = expectedBatchOperations; |
| 121 } | 125 } |
| (...skipping 176 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 298 ScriptValue call(ScriptValue value) override { | 302 ScriptValue call(ScriptValue value) override { |
| 299 ADD_FAILURE() << "Unexpected call to a null ScriptFunction."; | 303 ADD_FAILURE() << "Unexpected call to a null ScriptFunction."; |
| 300 return value; | 304 return value; |
| 301 } | 305 } |
| 302 | 306 |
| 303 private: | 307 private: |
| 304 UnreachableFunction(ScriptState* scriptState) | 308 UnreachableFunction(ScriptState* scriptState) |
| 305 : ScriptFunction(scriptState) {} | 309 : ScriptFunction(scriptState) {} |
| 306 }; | 310 }; |
| 307 | 311 |
| 308 // A ScriptFunction that saves its parameter; used by tests to assert on corre
ct | 312 // A ScriptFunction that saves its parameter; used by tests to assert on |
| 309 // values being passed. | 313 // correct values being passed. |
| 310 class TestFunction : public ScriptFunction { | 314 class TestFunction : public ScriptFunction { |
| 311 public: | 315 public: |
| 312 static v8::Local<v8::Function> create(ScriptState* scriptState, | 316 static v8::Local<v8::Function> create(ScriptState* scriptState, |
| 313 ScriptValue* outValue) { | 317 ScriptValue* outValue) { |
| 314 TestFunction* self = new TestFunction(scriptState, outValue); | 318 TestFunction* self = new TestFunction(scriptState, outValue); |
| 315 return self->bindToV8Function(); | 319 return self->bindToV8Function(); |
| 316 } | 320 } |
| 317 | 321 |
| 318 ScriptValue call(ScriptValue value) override { | 322 ScriptValue call(ScriptValue value) override { |
| 319 ASSERT(!value.isEmpty()); | 323 ASSERT(!value.isEmpty()); |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 368 EXPECT_TRUE(scriptValue.isUndefined()); | 372 EXPECT_TRUE(scriptValue.isUndefined()); |
| 369 | 373 |
| 370 cache = createCache(fetcher, testCache = new ErrorWebCacheForTests( | 374 cache = createCache(fetcher, testCache = new ErrorWebCacheForTests( |
| 371 WebServiceWorkerCacheErrorExists)); | 375 WebServiceWorkerCacheErrorExists)); |
| 372 matchPromise = cache->match(getScriptState(), stringToRequestInfo(url), | 376 matchPromise = cache->match(getScriptState(), stringToRequestInfo(url), |
| 373 options, exceptionState); | 377 options, exceptionState); |
| 374 EXPECT_EQ("InvalidAccessError: Entry already exists.", | 378 EXPECT_EQ("InvalidAccessError: Entry already exists.", |
| 375 getRejectString(matchPromise)); | 379 getRejectString(matchPromise)); |
| 376 } | 380 } |
| 377 | 381 |
| 378 // Tests that arguments are faithfully passed on calls to Cache methods, except
for methods which use batch operations, | 382 // Tests that arguments are faithfully passed on calls to Cache methods, except |
| 379 // which are tested later. | 383 // for methods which use batch operations, which are tested later. |
| 380 TEST_F(CacheStorageTest, BasicArguments) { | 384 TEST_F(CacheStorageTest, BasicArguments) { |
| 381 ScriptState::Scope scope(getScriptState()); | 385 ScriptState::Scope scope(getScriptState()); |
| 382 NonThrowableExceptionState exceptionState; | 386 NonThrowableExceptionState exceptionState; |
| 383 ScopedFetcherForTests* fetcher = ScopedFetcherForTests::create(); | 387 ScopedFetcherForTests* fetcher = ScopedFetcherForTests::create(); |
| 384 ErrorWebCacheForTests* testCache; | 388 ErrorWebCacheForTests* testCache; |
| 385 Cache* cache = | 389 Cache* cache = |
| 386 createCache(fetcher, testCache = new NotImplementedErrorCache()); | 390 createCache(fetcher, testCache = new NotImplementedErrorCache()); |
| 387 ASSERT(cache); | 391 ASSERT(cache); |
| 388 | 392 |
| 389 const String url = "http://www.cache.arguments.test/"; | 393 const String url = "http://www.cache.arguments.test/"; |
| (...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 439 testCache->getAndClearLastErrorWebCacheMethodCalled()); | 443 testCache->getAndClearLastErrorWebCacheMethodCalled()); |
| 440 EXPECT_EQ(kNotImplementedString, getRejectString(keysResult2)); | 444 EXPECT_EQ(kNotImplementedString, getRejectString(keysResult2)); |
| 441 | 445 |
| 442 ScriptPromise stringKeysResult2 = cache->keys( | 446 ScriptPromise stringKeysResult2 = cache->keys( |
| 443 getScriptState(), stringToRequestInfo(url), options, exceptionState); | 447 getScriptState(), stringToRequestInfo(url), options, exceptionState); |
| 444 EXPECT_EQ("dispatchKeys", | 448 EXPECT_EQ("dispatchKeys", |
| 445 testCache->getAndClearLastErrorWebCacheMethodCalled()); | 449 testCache->getAndClearLastErrorWebCacheMethodCalled()); |
| 446 EXPECT_EQ(kNotImplementedString, getRejectString(stringKeysResult2)); | 450 EXPECT_EQ(kNotImplementedString, getRejectString(stringKeysResult2)); |
| 447 } | 451 } |
| 448 | 452 |
| 449 // Tests that arguments are faithfully passed to API calls that degrade to batch
operations. | 453 // Tests that arguments are faithfully passed to API calls that degrade to batch |
| 454 // operations. |
| 450 TEST_F(CacheStorageTest, BatchOperationArguments) { | 455 TEST_F(CacheStorageTest, BatchOperationArguments) { |
| 451 ScriptState::Scope scope(getScriptState()); | 456 ScriptState::Scope scope(getScriptState()); |
| 452 NonThrowableExceptionState exceptionState; | 457 NonThrowableExceptionState exceptionState; |
| 453 ScopedFetcherForTests* fetcher = ScopedFetcherForTests::create(); | 458 ScopedFetcherForTests* fetcher = ScopedFetcherForTests::create(); |
| 454 ErrorWebCacheForTests* testCache; | 459 ErrorWebCacheForTests* testCache; |
| 455 Cache* cache = | 460 Cache* cache = |
| 456 createCache(fetcher, testCache = new NotImplementedErrorCache()); | 461 createCache(fetcher, testCache = new NotImplementedErrorCache()); |
| 457 ASSERT(cache); | 462 ASSERT(cache); |
| 458 | 463 |
| 459 WebServiceWorkerCache::QueryParams expectedQueryParams; | 464 WebServiceWorkerCache::QueryParams expectedQueryParams; |
| (...skipping 262 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 722 | 727 |
| 723 EXPECT_EQ(kNotImplementedString, getRejectString(addResult)); | 728 EXPECT_EQ(kNotImplementedString, getRejectString(addResult)); |
| 724 EXPECT_EQ(1, fetcher->fetchCount()); | 729 EXPECT_EQ(1, fetcher->fetchCount()); |
| 725 EXPECT_EQ("dispatchBatch", | 730 EXPECT_EQ("dispatchBatch", |
| 726 testCache->getAndClearLastErrorWebCacheMethodCalled()); | 731 testCache->getAndClearLastErrorWebCacheMethodCalled()); |
| 727 } | 732 } |
| 728 | 733 |
| 729 } // namespace | 734 } // namespace |
| 730 | 735 |
| 731 } // namespace blink | 736 } // namespace blink |
| OLD | NEW |