| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "config.h" | |
| 6 #include "modules/serviceworkers/Cache.h" | |
| 7 | |
| 8 #include "bindings/core/v8/ExceptionState.h" | |
| 9 #include "bindings/core/v8/ScriptFunction.h" | |
| 10 #include "bindings/core/v8/ScriptPromise.h" | |
| 11 #include "bindings/core/v8/ScriptValue.h" | |
| 12 #include "bindings/core/v8/V8Binding.h" | |
| 13 #include "bindings/modules/v8/V8Request.h" | |
| 14 #include "bindings/modules/v8/V8Response.h" | |
| 15 #include "core/dom/Document.h" | |
| 16 #include "core/frame/Frame.h" | |
| 17 #include "core/testing/DummyPageHolder.h" | |
| 18 #include "modules/fetch/Request.h" | |
| 19 #include "modules/fetch/Response.h" | |
| 20 #include "public/platform/WebServiceWorkerCache.h" | |
| 21 #include "wtf/OwnPtr.h" | |
| 22 | |
| 23 #include <algorithm> | |
| 24 #include <gtest/gtest.h> | |
| 25 #include <string> | |
| 26 | |
| 27 namespace blink { | |
| 28 | |
| 29 namespace { | |
| 30 | |
| 31 const char kNotImplementedString[] = "NotSupportedError: Method is not implement
ed."; | |
| 32 | |
| 33 // A test implementation of the WebServiceWorkerCache interface which returns a
(provided) error for every operation, and optionally checks arguments | |
| 34 // to methods against provided arguments. Also used as a base class for test spe
cific caches. | |
| 35 class ErrorWebCacheForTests : public WebServiceWorkerCache { | |
| 36 public: | |
| 37 ErrorWebCacheForTests(const WebServiceWorkerCacheError error) | |
| 38 : m_error(error) | |
| 39 , m_expectedUrl(0) | |
| 40 , m_expectedQueryParams(0) | |
| 41 , m_expectedBatchOperations(0) { } | |
| 42 | |
| 43 std::string getAndClearLastErrorWebCacheMethodCalled() | |
| 44 { | |
| 45 std::string old = m_lastErrorWebCacheMethodCalled; | |
| 46 m_lastErrorWebCacheMethodCalled.clear(); | |
| 47 return old; | |
| 48 } | |
| 49 | |
| 50 // These methods do not take ownership of their parameter. They provide an o
ptional sample object to check parameters against. | |
| 51 void setExpectedUrl(const String* expectedUrl) { m_expectedUrl = expectedUrl
; } | |
| 52 void setExpectedQueryParams(const QueryParams* expectedQueryParams) { m_expe
ctedQueryParams = expectedQueryParams; } | |
| 53 void setExpectedBatchOperations(const WebVector<BatchOperation>* expectedBat
chOperations) { m_expectedBatchOperations = expectedBatchOperations; } | |
| 54 | |
| 55 // From WebServiceWorkerCache: | |
| 56 virtual void dispatchMatch(CacheMatchCallbacks* callbacks, const WebServiceW
orkerRequest& webRequest, const QueryParams& queryParams) override | |
| 57 { | |
| 58 m_lastErrorWebCacheMethodCalled = "dispatchMatch"; | |
| 59 checkUrlIfProvided(webRequest.url()); | |
| 60 checkQueryParamsIfProvided(queryParams); | |
| 61 | |
| 62 OwnPtr<CacheMatchCallbacks> ownedCallbacks(adoptPtr(callbacks)); | |
| 63 WebServiceWorkerCacheError error = m_error; | |
| 64 return callbacks->onError(&error); | |
| 65 } | |
| 66 | |
| 67 virtual void dispatchMatchAll(CacheWithResponsesCallbacks* callbacks, const
WebServiceWorkerRequest& webRequest, const QueryParams& queryParams) override | |
| 68 { | |
| 69 m_lastErrorWebCacheMethodCalled = "dispatchMatchAll"; | |
| 70 checkUrlIfProvided(webRequest.url()); | |
| 71 checkQueryParamsIfProvided(queryParams); | |
| 72 | |
| 73 OwnPtr<CacheWithResponsesCallbacks> ownedCallbacks(adoptPtr(callbacks)); | |
| 74 WebServiceWorkerCacheError error = m_error; | |
| 75 return callbacks->onError(&error); | |
| 76 } | |
| 77 | |
| 78 virtual void dispatchKeys(CacheWithRequestsCallbacks* callbacks, const WebSe
rviceWorkerRequest* webRequest, const QueryParams& queryParams) override | |
| 79 { | |
| 80 m_lastErrorWebCacheMethodCalled = "dispatchKeys"; | |
| 81 if (webRequest) { | |
| 82 checkUrlIfProvided(webRequest->url()); | |
| 83 checkQueryParamsIfProvided(queryParams); | |
| 84 } | |
| 85 | |
| 86 OwnPtr<CacheWithRequestsCallbacks> ownedCallbacks(adoptPtr(callbacks)); | |
| 87 WebServiceWorkerCacheError error = m_error; | |
| 88 return callbacks->onError(&error); | |
| 89 } | |
| 90 | |
| 91 virtual void dispatchBatch(CacheWithResponsesCallbacks* callbacks, const Web
Vector<BatchOperation>& batchOperations) override | |
| 92 { | |
| 93 m_lastErrorWebCacheMethodCalled = "dispatchBatch"; | |
| 94 checkBatchOperationsIfProvided(batchOperations); | |
| 95 | |
| 96 OwnPtr<CacheWithResponsesCallbacks> ownedCallbacks(adoptPtr(callbacks)); | |
| 97 WebServiceWorkerCacheError error = m_error; | |
| 98 return callbacks->onError(&error); | |
| 99 } | |
| 100 | |
| 101 protected: | |
| 102 void checkUrlIfProvided(const KURL& url) | |
| 103 { | |
| 104 if (!m_expectedUrl) | |
| 105 return; | |
| 106 EXPECT_EQ(*m_expectedUrl, url); | |
| 107 } | |
| 108 | |
| 109 void checkQueryParamsIfProvided(const QueryParams& queryParams) | |
| 110 { | |
| 111 if (!m_expectedQueryParams) | |
| 112 return; | |
| 113 CompareQueryParamsForTest(*m_expectedQueryParams, queryParams); | |
| 114 } | |
| 115 | |
| 116 void checkBatchOperationsIfProvided(const WebVector<BatchOperation>& batchOp
erations) | |
| 117 { | |
| 118 if (!m_expectedBatchOperations) | |
| 119 return; | |
| 120 const WebVector<BatchOperation> expectedBatchOperations = *m_expectedBat
chOperations; | |
| 121 EXPECT_EQ(expectedBatchOperations.size(), batchOperations.size()); | |
| 122 for (int i = 0, minsize = std::min(expectedBatchOperations.size(), batch
Operations.size()); i < minsize; ++i) { | |
| 123 EXPECT_EQ(expectedBatchOperations[i].operationType, batchOperations[
i].operationType); | |
| 124 const String expectedRequestUrl = KURL(expectedBatchOperations[i].re
quest.url()); | |
| 125 EXPECT_EQ(expectedRequestUrl, KURL(batchOperations[i].request.url())
); | |
| 126 const String expectedResponseUrl = KURL(expectedBatchOperations[i].r
esponse.url()); | |
| 127 EXPECT_EQ(expectedResponseUrl, KURL(batchOperations[i].response.url(
))); | |
| 128 CompareQueryParamsForTest(expectedBatchOperations[i].matchParams, ba
tchOperations[i].matchParams); | |
| 129 } | |
| 130 } | |
| 131 | |
| 132 private: | |
| 133 static void CompareQueryParamsForTest(const QueryParams& expectedQueryParams
, const QueryParams& queryParams) | |
| 134 { | |
| 135 EXPECT_EQ(expectedQueryParams.ignoreSearch, queryParams.ignoreSearch); | |
| 136 EXPECT_EQ(expectedQueryParams.ignoreMethod, queryParams.ignoreMethod); | |
| 137 EXPECT_EQ(expectedQueryParams.ignoreVary, queryParams.ignoreVary); | |
| 138 EXPECT_EQ(expectedQueryParams.cacheName, queryParams.cacheName); | |
| 139 } | |
| 140 | |
| 141 const WebServiceWorkerCacheError m_error; | |
| 142 | |
| 143 const String* m_expectedUrl; | |
| 144 const QueryParams* m_expectedQueryParams; | |
| 145 const WebVector<BatchOperation>* m_expectedBatchOperations; | |
| 146 | |
| 147 std::string m_lastErrorWebCacheMethodCalled; | |
| 148 }; | |
| 149 | |
| 150 class NotImplementedErrorCache : public ErrorWebCacheForTests { | |
| 151 public: | |
| 152 NotImplementedErrorCache() : ErrorWebCacheForTests(WebServiceWorkerCacheErro
rNotImplemented) { } | |
| 153 }; | |
| 154 | |
| 155 class ServiceWorkerCacheTest : public ::testing::Test { | |
| 156 public: | |
| 157 ServiceWorkerCacheTest() | |
| 158 : m_page(DummyPageHolder::create(IntSize(1, 1))) { } | |
| 159 | |
| 160 ScriptState* scriptState() { return ScriptState::forMainWorld(m_page->docume
nt().frame()); } | |
| 161 ExecutionContext* executionContext() { return scriptState()->executionContex
t(); } | |
| 162 v8::Isolate* isolate() { return scriptState()->isolate(); } | |
| 163 | |
| 164 Request* newRequestFromUrl(const String& url) | |
| 165 { | |
| 166 TrackExceptionState exceptionState; | |
| 167 Request* request = Request::create(executionContext(), url, exceptionSta
te); | |
| 168 EXPECT_FALSE(exceptionState.hadException()); | |
| 169 return exceptionState.hadException() ? 0 : request; | |
| 170 } | |
| 171 | |
| 172 // Convenience methods for testing the returned promises. | |
| 173 ScriptValue getRejectValue(ScriptPromise& promise) | |
| 174 { | |
| 175 ScriptValue onReject; | |
| 176 promise.then(UnreachableFunction::create(scriptState()), TestFunction::c
reate(scriptState(), &onReject)); | |
| 177 isolate()->RunMicrotasks(); | |
| 178 return onReject; | |
| 179 } | |
| 180 | |
| 181 std::string getRejectString(ScriptPromise& promise) | |
| 182 { | |
| 183 ScriptValue onReject = getRejectValue(promise); | |
| 184 return toCoreString(onReject.v8Value()->ToString(isolate())).ascii().dat
a(); | |
| 185 } | |
| 186 | |
| 187 ScriptValue getResolveValue(ScriptPromise& promise) | |
| 188 { | |
| 189 ScriptValue onResolve; | |
| 190 promise.then(TestFunction::create(scriptState(), &onResolve), Unreachabl
eFunction::create(scriptState())); | |
| 191 isolate()->RunMicrotasks(); | |
| 192 return onResolve; | |
| 193 } | |
| 194 | |
| 195 std::string getResolveString(ScriptPromise& promise) | |
| 196 { | |
| 197 ScriptValue onResolve = getResolveValue(promise); | |
| 198 return toCoreString(onResolve.v8Value()->ToString(isolate())).ascii().da
ta(); | |
| 199 } | |
| 200 | |
| 201 ExceptionState& exceptionState() | |
| 202 { | |
| 203 return m_exceptionState; | |
| 204 } | |
| 205 | |
| 206 private: | |
| 207 // A ScriptFunction that creates a test failure if it is ever called. | |
| 208 class UnreachableFunction : public ScriptFunction { | |
| 209 public: | |
| 210 static v8::Handle<v8::Function> create(ScriptState* scriptState) | |
| 211 { | |
| 212 UnreachableFunction* self = new UnreachableFunction(scriptState); | |
| 213 return self->bindToV8Function(); | |
| 214 } | |
| 215 | |
| 216 virtual ScriptValue call(ScriptValue value) override | |
| 217 { | |
| 218 ADD_FAILURE() << "Unexpected call to a null ScriptFunction."; | |
| 219 return value; | |
| 220 } | |
| 221 private: | |
| 222 UnreachableFunction(ScriptState* scriptState) : ScriptFunction(scriptSta
te) { } | |
| 223 }; | |
| 224 | |
| 225 // A ScriptFunction that saves its parameter; used by tests to assert on cor
rect | |
| 226 // values being passed. | |
| 227 class TestFunction : public ScriptFunction { | |
| 228 public: | |
| 229 static v8::Handle<v8::Function> create(ScriptState* scriptState, ScriptV
alue* outValue) | |
| 230 { | |
| 231 TestFunction* self = new TestFunction(scriptState, outValue); | |
| 232 return self->bindToV8Function(); | |
| 233 } | |
| 234 | |
| 235 virtual ScriptValue call(ScriptValue value) override | |
| 236 { | |
| 237 ASSERT(!value.isEmpty()); | |
| 238 *m_value = value; | |
| 239 return value; | |
| 240 } | |
| 241 | |
| 242 private: | |
| 243 TestFunction(ScriptState* scriptState, ScriptValue* outValue) : ScriptFu
nction(scriptState), m_value(outValue) { } | |
| 244 | |
| 245 ScriptValue* m_value; | |
| 246 }; | |
| 247 | |
| 248 // From ::testing::Test: | |
| 249 virtual void SetUp() override | |
| 250 { | |
| 251 EXPECT_FALSE(m_scriptScope); | |
| 252 m_scriptScope = adoptPtr(new ScriptState::Scope(scriptState())); | |
| 253 } | |
| 254 | |
| 255 virtual void TearDown() override | |
| 256 { | |
| 257 m_scriptScope = 0; | |
| 258 } | |
| 259 | |
| 260 // Lifetime is that of the text fixture. | |
| 261 OwnPtr<DummyPageHolder> m_page; | |
| 262 | |
| 263 // Lifetime is per test instance. | |
| 264 OwnPtr<ScriptState::Scope> m_scriptScope; | |
| 265 | |
| 266 NonThrowableExceptionState m_exceptionState; | |
| 267 }; | |
| 268 | |
| 269 RequestInfo stringToRequestInfo(const String& value) | |
| 270 { | |
| 271 RequestInfo info; | |
| 272 info.setUSVString(value); | |
| 273 return info; | |
| 274 } | |
| 275 | |
| 276 RequestInfo requestToRequestInfo(Request* value) | |
| 277 { | |
| 278 RequestInfo info; | |
| 279 info.setRequest(value); | |
| 280 return info; | |
| 281 } | |
| 282 | |
| 283 TEST_F(ServiceWorkerCacheTest, Basics) | |
| 284 { | |
| 285 ErrorWebCacheForTests* testCache; | |
| 286 Cache* cache = Cache::create(testCache = new NotImplementedErrorCache()); | |
| 287 ASSERT(cache); | |
| 288 | |
| 289 const String url = "http://www.cachetest.org/"; | |
| 290 | |
| 291 CacheQueryOptions options; | |
| 292 ScriptPromise matchPromise = cache->match(scriptState(), stringToRequestInfo
(url), options, exceptionState()); | |
| 293 EXPECT_EQ(kNotImplementedString, getRejectString(matchPromise)); | |
| 294 | |
| 295 cache = Cache::create(testCache = new ErrorWebCacheForTests(WebServiceWorker
CacheErrorNotFound)); | |
| 296 matchPromise = cache->match(scriptState(), stringToRequestInfo(url), options
, exceptionState()); | |
| 297 ScriptValue scriptValue = getResolveValue(matchPromise); | |
| 298 EXPECT_TRUE(scriptValue.isUndefined()); | |
| 299 | |
| 300 cache = Cache::create(testCache = new ErrorWebCacheForTests(WebServiceWorker
CacheErrorExists)); | |
| 301 matchPromise = cache->match(scriptState(), stringToRequestInfo(url), options
, exceptionState()); | |
| 302 EXPECT_EQ("InvalidAccessError: Entry already exists.", getRejectString(match
Promise)); | |
| 303 } | |
| 304 | |
| 305 // Tests that arguments are faithfully passed on calls to Cache methods, except
for methods which use batch operations, | |
| 306 // which are tested later. | |
| 307 TEST_F(ServiceWorkerCacheTest, BasicArguments) | |
| 308 { | |
| 309 ErrorWebCacheForTests* testCache; | |
| 310 Cache* cache = Cache::create(testCache = new NotImplementedErrorCache()); | |
| 311 ASSERT(cache); | |
| 312 | |
| 313 const String url = "http://www.cache.arguments.test/"; | |
| 314 testCache->setExpectedUrl(&url); | |
| 315 | |
| 316 WebServiceWorkerCache::QueryParams expectedQueryParams; | |
| 317 expectedQueryParams.ignoreVary = true; | |
| 318 expectedQueryParams.cacheName = "this is a cache name"; | |
| 319 testCache->setExpectedQueryParams(&expectedQueryParams); | |
| 320 | |
| 321 CacheQueryOptions options; | |
| 322 options.setIgnoreVary(1); | |
| 323 options.setCacheName(expectedQueryParams.cacheName); | |
| 324 | |
| 325 Request* request = newRequestFromUrl(url); | |
| 326 ASSERT(request); | |
| 327 ScriptPromise matchResult = cache->match(scriptState(), requestToRequestInfo
(request), options, exceptionState()); | |
| 328 EXPECT_EQ("dispatchMatch", testCache->getAndClearLastErrorWebCacheMethodCall
ed()); | |
| 329 EXPECT_EQ(kNotImplementedString, getRejectString(matchResult)); | |
| 330 | |
| 331 ScriptPromise stringMatchResult = cache->match(scriptState(), stringToReques
tInfo(url), options, exceptionState()); | |
| 332 EXPECT_EQ("dispatchMatch", testCache->getAndClearLastErrorWebCacheMethodCall
ed()); | |
| 333 EXPECT_EQ(kNotImplementedString, getRejectString(stringMatchResult)); | |
| 334 | |
| 335 request = newRequestFromUrl(url); | |
| 336 ASSERT(request); | |
| 337 ScriptPromise matchAllResult = cache->matchAll(scriptState(), requestToReque
stInfo(request), options, exceptionState()); | |
| 338 EXPECT_EQ("dispatchMatchAll", testCache->getAndClearLastErrorWebCacheMethodC
alled()); | |
| 339 EXPECT_EQ(kNotImplementedString, getRejectString(matchAllResult)); | |
| 340 | |
| 341 ScriptPromise stringMatchAllResult = cache->matchAll(scriptState(), stringTo
RequestInfo(url), options, exceptionState()); | |
| 342 EXPECT_EQ("dispatchMatchAll", testCache->getAndClearLastErrorWebCacheMethodC
alled()); | |
| 343 EXPECT_EQ(kNotImplementedString, getRejectString(stringMatchAllResult)); | |
| 344 | |
| 345 ScriptPromise keysResult1 = cache->keys(scriptState(), exceptionState()); | |
| 346 EXPECT_EQ("dispatchKeys", testCache->getAndClearLastErrorWebCacheMethodCalle
d()); | |
| 347 EXPECT_EQ(kNotImplementedString, getRejectString(keysResult1)); | |
| 348 | |
| 349 request = newRequestFromUrl(url); | |
| 350 ASSERT(request); | |
| 351 ScriptPromise keysResult2 = cache->keys(scriptState(), requestToRequestInfo(
request), options, exceptionState()); | |
| 352 EXPECT_EQ("dispatchKeys", testCache->getAndClearLastErrorWebCacheMethodCalle
d()); | |
| 353 EXPECT_EQ(kNotImplementedString, getRejectString(keysResult2)); | |
| 354 | |
| 355 ScriptPromise stringKeysResult2 = cache->keys(scriptState(), stringToRequest
Info(url), options, exceptionState()); | |
| 356 EXPECT_EQ("dispatchKeys", testCache->getAndClearLastErrorWebCacheMethodCalle
d()); | |
| 357 EXPECT_EQ(kNotImplementedString, getRejectString(stringKeysResult2)); | |
| 358 } | |
| 359 | |
| 360 // Tests that arguments are faithfully passed to API calls that degrade to batch
operations. | |
| 361 TEST_F(ServiceWorkerCacheTest, BatchOperationArguments) | |
| 362 { | |
| 363 ErrorWebCacheForTests* testCache; | |
| 364 Cache* cache = Cache::create(testCache = new NotImplementedErrorCache()); | |
| 365 ASSERT(cache); | |
| 366 | |
| 367 WebServiceWorkerCache::QueryParams expectedQueryParams; | |
| 368 expectedQueryParams.cacheName = "this is another cache name"; | |
| 369 testCache->setExpectedQueryParams(&expectedQueryParams); | |
| 370 | |
| 371 CacheQueryOptions options; | |
| 372 options.setCacheName(expectedQueryParams.cacheName); | |
| 373 | |
| 374 const String url = "http://batch.operations.test/"; | |
| 375 Request* request = newRequestFromUrl(url); | |
| 376 ASSERT(request); | |
| 377 | |
| 378 WebServiceWorkerResponse webResponse; | |
| 379 webResponse.setURL(KURL(ParsedURLString, url)); | |
| 380 Response* response = Response::create(executionContext(), webResponse); | |
| 381 | |
| 382 WebVector<WebServiceWorkerCache::BatchOperation> expectedDeleteOperations(si
ze_t(1)); | |
| 383 { | |
| 384 WebServiceWorkerCache::BatchOperation deleteOperation; | |
| 385 deleteOperation.operationType = WebServiceWorkerCache::OperationTypeDele
te; | |
| 386 request->populateWebServiceWorkerRequest(deleteOperation.request); | |
| 387 deleteOperation.matchParams = expectedQueryParams; | |
| 388 expectedDeleteOperations[0] = deleteOperation; | |
| 389 } | |
| 390 testCache->setExpectedBatchOperations(&expectedDeleteOperations); | |
| 391 | |
| 392 ScriptPromise deleteResult = cache->deleteFunction(scriptState(), requestToR
equestInfo(request), options, exceptionState()); | |
| 393 EXPECT_EQ("dispatchBatch", testCache->getAndClearLastErrorWebCacheMethodCall
ed()); | |
| 394 EXPECT_EQ(kNotImplementedString, getRejectString(deleteResult)); | |
| 395 | |
| 396 ScriptPromise stringDeleteResult = cache->deleteFunction(scriptState(), stri
ngToRequestInfo(url), options, exceptionState()); | |
| 397 EXPECT_EQ("dispatchBatch", testCache->getAndClearLastErrorWebCacheMethodCall
ed()); | |
| 398 EXPECT_EQ(kNotImplementedString, getRejectString(stringDeleteResult)); | |
| 399 | |
| 400 WebVector<WebServiceWorkerCache::BatchOperation> expectedPutOperations(size_
t(1)); | |
| 401 { | |
| 402 WebServiceWorkerCache::BatchOperation putOperation; | |
| 403 putOperation.operationType = WebServiceWorkerCache::OperationTypePut; | |
| 404 request->populateWebServiceWorkerRequest(putOperation.request); | |
| 405 response->populateWebServiceWorkerResponse(putOperation.response); | |
| 406 expectedPutOperations[0] = putOperation; | |
| 407 } | |
| 408 testCache->setExpectedBatchOperations(&expectedPutOperations); | |
| 409 | |
| 410 request = newRequestFromUrl(url); | |
| 411 ASSERT(request); | |
| 412 ScriptPromise putResult = cache->put(scriptState(), requestToRequestInfo(req
uest), response->clone(exceptionState()), exceptionState()); | |
| 413 EXPECT_EQ("dispatchBatch", testCache->getAndClearLastErrorWebCacheMethodCall
ed()); | |
| 414 EXPECT_EQ(kNotImplementedString, getRejectString(putResult)); | |
| 415 | |
| 416 ScriptPromise stringPutResult = cache->put(scriptState(), stringToRequestInf
o(url), response, exceptionState()); | |
| 417 EXPECT_EQ("dispatchBatch", testCache->getAndClearLastErrorWebCacheMethodCall
ed()); | |
| 418 EXPECT_EQ(kNotImplementedString, getRejectString(stringPutResult)); | |
| 419 | |
| 420 // FIXME: test add & addAll. | |
| 421 } | |
| 422 | |
| 423 class MatchTestCache : public NotImplementedErrorCache { | |
| 424 public: | |
| 425 MatchTestCache(WebServiceWorkerResponse& response) | |
| 426 : m_response(response) { } | |
| 427 | |
| 428 // From WebServiceWorkerCache: | |
| 429 virtual void dispatchMatch(CacheMatchCallbacks* callbacks, const WebServiceW
orkerRequest& webRequest, const QueryParams& queryParams) override | |
| 430 { | |
| 431 OwnPtr<CacheMatchCallbacks> ownedCallbacks(adoptPtr(callbacks)); | |
| 432 return callbacks->onSuccess(&m_response); | |
| 433 } | |
| 434 | |
| 435 private: | |
| 436 WebServiceWorkerResponse& m_response; | |
| 437 }; | |
| 438 | |
| 439 TEST_F(ServiceWorkerCacheTest, MatchResponseTest) | |
| 440 { | |
| 441 const String requestUrl = "http://request.url/"; | |
| 442 const String responseUrl = "http://match.response.test/"; | |
| 443 | |
| 444 WebServiceWorkerResponse webResponse; | |
| 445 webResponse.setURL(KURL(ParsedURLString, responseUrl)); | |
| 446 webResponse.setResponseType(WebServiceWorkerResponseTypeDefault); | |
| 447 | |
| 448 Cache* cache = Cache::create(new MatchTestCache(webResponse)); | |
| 449 CacheQueryOptions options; | |
| 450 | |
| 451 ScriptPromise result = cache->match(scriptState(), stringToRequestInfo(reque
stUrl), options, exceptionState()); | |
| 452 ScriptValue scriptValue = getResolveValue(result); | |
| 453 Response* response = V8Response::toImplWithTypeCheck(isolate(), scriptValue.
v8Value()); | |
| 454 ASSERT_TRUE(response); | |
| 455 EXPECT_EQ(responseUrl, response->url()); | |
| 456 } | |
| 457 | |
| 458 class KeysTestCache : public NotImplementedErrorCache { | |
| 459 public: | |
| 460 KeysTestCache(WebVector<WebServiceWorkerRequest>& requests) | |
| 461 : m_requests(requests) { } | |
| 462 | |
| 463 virtual void dispatchKeys(CacheWithRequestsCallbacks* callbacks, const WebSe
rviceWorkerRequest* webRequest, const QueryParams& queryParams) override | |
| 464 { | |
| 465 OwnPtr<CacheWithRequestsCallbacks> ownedCallbacks(adoptPtr(callbacks)); | |
| 466 return callbacks->onSuccess(&m_requests); | |
| 467 } | |
| 468 | |
| 469 private: | |
| 470 WebVector<WebServiceWorkerRequest>& m_requests; | |
| 471 }; | |
| 472 | |
| 473 TEST_F(ServiceWorkerCacheTest, KeysResponseTest) | |
| 474 { | |
| 475 const String url1 = "http://first.request/"; | |
| 476 const String url2 = "http://second.request/"; | |
| 477 | |
| 478 Vector<String> expectedUrls(size_t(2)); | |
| 479 expectedUrls[0] = url1; | |
| 480 expectedUrls[1] = url2; | |
| 481 | |
| 482 WebVector<WebServiceWorkerRequest> webRequests(size_t(2)); | |
| 483 webRequests[0].setURL(KURL(ParsedURLString, url1)); | |
| 484 webRequests[1].setURL(KURL(ParsedURLString, url2)); | |
| 485 | |
| 486 Cache* cache = Cache::create(new KeysTestCache(webRequests)); | |
| 487 | |
| 488 ScriptPromise result = cache->keys(scriptState(), exceptionState()); | |
| 489 ScriptValue scriptValue = getResolveValue(result); | |
| 490 | |
| 491 Vector<v8::Local<v8::Value>> requests = toImplArray<v8::Local<v8::Value>>(sc
riptValue.v8Value(), 0, isolate(), exceptionState()); | |
| 492 EXPECT_EQ(expectedUrls.size(), requests.size()); | |
| 493 for (int i = 0, minsize = std::min(expectedUrls.size(), requests.size()); i
< minsize; ++i) { | |
| 494 Request* request = V8Request::toImplWithTypeCheck(isolate(), requests[i]
); | |
| 495 EXPECT_TRUE(request); | |
| 496 if (request) | |
| 497 EXPECT_EQ(expectedUrls[i], request->url()); | |
| 498 } | |
| 499 } | |
| 500 | |
| 501 class MatchAllAndBatchTestCache : public NotImplementedErrorCache { | |
| 502 public: | |
| 503 MatchAllAndBatchTestCache(WebVector<WebServiceWorkerResponse>& responses) | |
| 504 : m_responses(responses) { } | |
| 505 | |
| 506 virtual void dispatchMatchAll(CacheWithResponsesCallbacks* callbacks, const
WebServiceWorkerRequest& webRequest, const QueryParams& queryParams) override | |
| 507 { | |
| 508 OwnPtr<CacheWithResponsesCallbacks> ownedCallbacks(adoptPtr(callbacks)); | |
| 509 return callbacks->onSuccess(&m_responses); | |
| 510 } | |
| 511 | |
| 512 virtual void dispatchBatch(CacheWithResponsesCallbacks* callbacks, const Web
Vector<BatchOperation>& batchOperations) override | |
| 513 { | |
| 514 OwnPtr<CacheWithResponsesCallbacks> ownedCallbacks(adoptPtr(callbacks)); | |
| 515 return callbacks->onSuccess(&m_responses); | |
| 516 } | |
| 517 | |
| 518 private: | |
| 519 WebVector<WebServiceWorkerResponse>& m_responses; | |
| 520 }; | |
| 521 | |
| 522 TEST_F(ServiceWorkerCacheTest, MatchAllAndBatchResponseTest) | |
| 523 { | |
| 524 const String url1 = "http://first.response/"; | |
| 525 const String url2 = "http://second.response/"; | |
| 526 | |
| 527 Vector<String> expectedUrls(size_t(2)); | |
| 528 expectedUrls[0] = url1; | |
| 529 expectedUrls[1] = url2; | |
| 530 | |
| 531 WebVector<WebServiceWorkerResponse> webResponses(size_t(2)); | |
| 532 webResponses[0].setURL(KURL(ParsedURLString, url1)); | |
| 533 webResponses[0].setResponseType(WebServiceWorkerResponseTypeDefault); | |
| 534 webResponses[1].setURL(KURL(ParsedURLString, url2)); | |
| 535 webResponses[1].setResponseType(WebServiceWorkerResponseTypeDefault); | |
| 536 | |
| 537 Cache* cache = Cache::create(new MatchAllAndBatchTestCache(webResponses)); | |
| 538 | |
| 539 CacheQueryOptions options; | |
| 540 ScriptPromise result = cache->matchAll(scriptState(), stringToRequestInfo("h
ttp://some.url/"), options, exceptionState()); | |
| 541 ScriptValue scriptValue = getResolveValue(result); | |
| 542 | |
| 543 Vector<v8::Local<v8::Value>> responses = toImplArray<v8::Local<v8::Value>>(s
criptValue.v8Value(), 0, isolate(), exceptionState()); | |
| 544 EXPECT_EQ(expectedUrls.size(), responses.size()); | |
| 545 for (int i = 0, minsize = std::min(expectedUrls.size(), responses.size()); i
< minsize; ++i) { | |
| 546 Response* response = V8Response::toImplWithTypeCheck(isolate(), response
s[i]); | |
| 547 EXPECT_TRUE(response); | |
| 548 if (response) | |
| 549 EXPECT_EQ(expectedUrls[i], response->url()); | |
| 550 } | |
| 551 | |
| 552 result = cache->deleteFunction(scriptState(), stringToRequestInfo("http://so
me.url/"), options, exceptionState()); | |
| 553 scriptValue = getResolveValue(result); | |
| 554 EXPECT_TRUE(scriptValue.v8Value()->IsBoolean()); | |
| 555 EXPECT_EQ(true, scriptValue.v8Value()->BooleanValue()); | |
| 556 } | |
| 557 | |
| 558 } // namespace | |
| 559 } // namespace blink | |
| OLD | NEW |