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