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/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 #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.prefixMatch, queryParams.prefixMatch); |
| 139 EXPECT_EQ(expectedQueryParams.cacheName, queryParams.cacheName); |
| 140 } |
| 141 |
| 142 const WebServiceWorkerCacheError m_error; |
| 143 |
| 144 const String* m_expectedUrl; |
| 145 const QueryParams* m_expectedQueryParams; |
| 146 const WebVector<BatchOperation>* m_expectedBatchOperations; |
| 147 |
| 148 std::string m_lastErrorWebCacheMethodCalled; |
| 149 }; |
| 150 |
| 151 class NotImplementedErrorCache : public ErrorWebCacheForTests { |
| 152 public: |
| 153 NotImplementedErrorCache() : ErrorWebCacheForTests(WebServiceWorkerCacheErro
rNotImplemented) { } |
| 154 }; |
| 155 |
| 156 class ServiceWorkerCacheTest : public ::testing::Test { |
| 157 public: |
| 158 ServiceWorkerCacheTest() |
| 159 : m_page(DummyPageHolder::create(IntSize(1, 1))) { } |
| 160 |
| 161 ScriptState* scriptState() { return ScriptState::forMainWorld(m_page->docume
nt().frame()); } |
| 162 ExecutionContext* executionContext() { return scriptState()->executionContex
t(); } |
| 163 v8::Isolate* isolate() { return scriptState()->isolate(); } |
| 164 |
| 165 Request* newRequestFromUrl(const String& url) |
| 166 { |
| 167 TrackExceptionState exceptionState; |
| 168 Request* request = Request::create(executionContext(), url, exceptionSta
te); |
| 169 EXPECT_FALSE(exceptionState.hadException()); |
| 170 return exceptionState.hadException() ? 0 : request; |
| 171 } |
| 172 |
| 173 // Convenience methods for testing the returned promises. |
| 174 ScriptValue getRejectValue(ScriptPromise& promise) |
| 175 { |
| 176 ScriptValue onReject; |
| 177 promise.then(UnreachableFunction::create(scriptState()), TestFunction::c
reate(scriptState(), &onReject)); |
| 178 isolate()->RunMicrotasks(); |
| 179 return onReject; |
| 180 } |
| 181 |
| 182 std::string getRejectString(ScriptPromise& promise) |
| 183 { |
| 184 ScriptValue onReject = getRejectValue(promise); |
| 185 return toCoreString(onReject.v8Value()->ToString()).ascii().data(); |
| 186 } |
| 187 |
| 188 ScriptValue getResolveValue(ScriptPromise& promise) |
| 189 { |
| 190 ScriptValue onResolve; |
| 191 promise.then(TestFunction::create(scriptState(), &onResolve), Unreachabl
eFunction::create(scriptState())); |
| 192 isolate()->RunMicrotasks(); |
| 193 return onResolve; |
| 194 } |
| 195 |
| 196 std::string getResolveString(ScriptPromise& promise) |
| 197 { |
| 198 ScriptValue onResolve = getResolveValue(promise); |
| 199 return toCoreString(onResolve.v8Value()->ToString()).ascii().data(); |
| 200 } |
| 201 |
| 202 private: |
| 203 // A ScriptFunction that creates a test failure if it is ever called. |
| 204 class UnreachableFunction : public ScriptFunction { |
| 205 public: |
| 206 static v8::Handle<v8::Function> create(ScriptState* scriptState) |
| 207 { |
| 208 UnreachableFunction* self = new UnreachableFunction(scriptState); |
| 209 return self->bindToV8Function(); |
| 210 } |
| 211 |
| 212 virtual ScriptValue call(ScriptValue value) OVERRIDE |
| 213 { |
| 214 ADD_FAILURE() << "Unexpected call to a null ScriptFunction."; |
| 215 return value; |
| 216 } |
| 217 private: |
| 218 UnreachableFunction(ScriptState* scriptState) : ScriptFunction(scriptSta
te) { } |
| 219 }; |
| 220 |
| 221 // A ScriptFunction that saves its parameter; used by tests to assert on cor
rect |
| 222 // values being passed. |
| 223 class TestFunction : public ScriptFunction { |
| 224 public: |
| 225 static v8::Handle<v8::Function> create(ScriptState* scriptState, ScriptV
alue* outValue) |
| 226 { |
| 227 TestFunction* self = new TestFunction(scriptState, outValue); |
| 228 return self->bindToV8Function(); |
| 229 } |
| 230 |
| 231 virtual ScriptValue call(ScriptValue value) OVERRIDE |
| 232 { |
| 233 ASSERT(!value.isEmpty()); |
| 234 *m_value = value; |
| 235 return value; |
| 236 } |
| 237 |
| 238 private: |
| 239 TestFunction(ScriptState* scriptState, ScriptValue* outValue) : ScriptFu
nction(scriptState), m_value(outValue) { } |
| 240 |
| 241 ScriptValue* m_value; |
| 242 }; |
| 243 |
| 244 // From ::testing::Test: |
| 245 virtual void SetUp() OVERRIDE |
| 246 { |
| 247 EXPECT_FALSE(m_scriptScope); |
| 248 m_scriptScope = adoptPtr(new ScriptState::Scope(scriptState())); |
| 249 } |
| 250 |
| 251 virtual void TearDown() OVERRIDE |
| 252 { |
| 253 m_scriptScope = 0; |
| 254 } |
| 255 |
| 256 // Lifetime is that of the text fixture. |
| 257 OwnPtr<DummyPageHolder> m_page; |
| 258 |
| 259 // Lifetime is per test instance. |
| 260 OwnPtr<ScriptState::Scope> m_scriptScope; |
| 261 }; |
| 262 |
| 263 TEST_F(ServiceWorkerCacheTest, Basics) |
| 264 { |
| 265 ErrorWebCacheForTests* testCache; |
| 266 Cache* cache = Cache::create(testCache = new NotImplementedErrorCache()); |
| 267 ASSERT(cache); |
| 268 |
| 269 const String url = "http://www.cachetest.org/"; |
| 270 |
| 271 ScriptPromise matchPromise = cache->match(scriptState(), url, Dictionary()); |
| 272 EXPECT_EQ(kNotImplementedString, getRejectString(matchPromise)); |
| 273 |
| 274 cache = Cache::create(testCache = new ErrorWebCacheForTests(WebServiceWorker
CacheErrorNotFound)); |
| 275 matchPromise = cache->match(scriptState(), url, Dictionary()); |
| 276 EXPECT_EQ("NotFoundError: Entry was not found.", getRejectString(matchPromis
e)); |
| 277 |
| 278 cache = Cache::create(testCache = new ErrorWebCacheForTests(WebServiceWorker
CacheErrorExists)); |
| 279 matchPromise = cache->match(scriptState(), url, Dictionary()); |
| 280 EXPECT_EQ("InvalidAccessError: Entry already exists.", getRejectString(match
Promise)); |
| 281 } |
| 282 |
| 283 // Tests that arguments are faithfully passed on calls to Cache methods, except
for methods which use batch operations, |
| 284 // which are tested later. |
| 285 TEST_F(ServiceWorkerCacheTest, BasicArguments) |
| 286 { |
| 287 ErrorWebCacheForTests* testCache; |
| 288 Cache* cache = Cache::create(testCache = new NotImplementedErrorCache()); |
| 289 ASSERT(cache); |
| 290 |
| 291 const String url = "http://www.cache.arguments.test/"; |
| 292 testCache->setExpectedUrl(&url); |
| 293 |
| 294 WebServiceWorkerCache::QueryParams expectedQueryParams; |
| 295 expectedQueryParams.ignoreVary = true; |
| 296 expectedQueryParams.cacheName = "this is a cache name"; |
| 297 testCache->setExpectedQueryParams(&expectedQueryParams); |
| 298 |
| 299 Dictionary queryParamsDict = Dictionary::createEmpty(isolate()); |
| 300 queryParamsDict.set("ignoreVary", 1); |
| 301 queryParamsDict.set("cacheName", expectedQueryParams.cacheName); |
| 302 |
| 303 Request* request = newRequestFromUrl(url); |
| 304 ASSERT(request); |
| 305 ScriptPromise matchResult = cache->match(scriptState(), request, queryParams
Dict); |
| 306 EXPECT_EQ("dispatchMatch", testCache->getAndClearLastErrorWebCacheMethodCall
ed()); |
| 307 EXPECT_EQ(kNotImplementedString, getRejectString(matchResult)); |
| 308 |
| 309 ScriptPromise stringMatchResult = cache->match(scriptState(), url, queryPara
msDict); |
| 310 EXPECT_EQ("dispatchMatch", testCache->getAndClearLastErrorWebCacheMethodCall
ed()); |
| 311 EXPECT_EQ(kNotImplementedString, getRejectString(stringMatchResult)); |
| 312 |
| 313 request = newRequestFromUrl(url); |
| 314 ASSERT(request); |
| 315 ScriptPromise matchAllResult = cache->matchAll(scriptState(), request, query
ParamsDict); |
| 316 EXPECT_EQ("dispatchMatchAll", testCache->getAndClearLastErrorWebCacheMethodC
alled()); |
| 317 EXPECT_EQ(kNotImplementedString, getRejectString(matchAllResult)); |
| 318 |
| 319 ScriptPromise stringMatchAllResult = cache->matchAll(scriptState(), url, que
ryParamsDict); |
| 320 EXPECT_EQ("dispatchMatchAll", testCache->getAndClearLastErrorWebCacheMethodC
alled()); |
| 321 EXPECT_EQ(kNotImplementedString, getRejectString(stringMatchAllResult)); |
| 322 |
| 323 ScriptPromise keysResult1 = cache->keys(scriptState()); |
| 324 EXPECT_EQ("dispatchKeys", testCache->getAndClearLastErrorWebCacheMethodCalle
d()); |
| 325 EXPECT_EQ(kNotImplementedString, getRejectString(keysResult1)); |
| 326 |
| 327 request = newRequestFromUrl(url); |
| 328 ASSERT(request); |
| 329 ScriptPromise keysResult2 = cache->keys(scriptState(), request, queryParamsD
ict); |
| 330 EXPECT_EQ("dispatchKeys", testCache->getAndClearLastErrorWebCacheMethodCalle
d()); |
| 331 EXPECT_EQ(kNotImplementedString, getRejectString(keysResult2)); |
| 332 |
| 333 ScriptPromise stringKeysResult2 = cache->keys(scriptState(), url, queryParam
sDict); |
| 334 EXPECT_EQ("dispatchKeys", testCache->getAndClearLastErrorWebCacheMethodCalle
d()); |
| 335 EXPECT_EQ(kNotImplementedString, getRejectString(stringKeysResult2)); |
| 336 } |
| 337 |
| 338 // Tests that arguments are faithfully passed to API calls that degrade to batch
operations. |
| 339 TEST_F(ServiceWorkerCacheTest, BatchOperationArguments) |
| 340 { |
| 341 ErrorWebCacheForTests* testCache; |
| 342 Cache* cache = Cache::create(testCache = new NotImplementedErrorCache()); |
| 343 ASSERT(cache); |
| 344 |
| 345 WebServiceWorkerCache::QueryParams expectedQueryParams; |
| 346 expectedQueryParams.prefixMatch = true; |
| 347 expectedQueryParams.cacheName = "this is another cache name"; |
| 348 testCache->setExpectedQueryParams(&expectedQueryParams); |
| 349 |
| 350 Dictionary queryParamsDict = Dictionary::createEmpty(isolate()); |
| 351 queryParamsDict.set("prefixMatch", 1); |
| 352 queryParamsDict.set("cacheName", expectedQueryParams.cacheName); |
| 353 |
| 354 const String url = "http://batch.operations.test/"; |
| 355 Request* request = newRequestFromUrl(url); |
| 356 ASSERT(request); |
| 357 |
| 358 WebServiceWorkerResponse webResponse; |
| 359 webResponse.setURL(KURL(ParsedURLString, url)); |
| 360 Response* response = Response::create(executionContext(), webResponse); |
| 361 |
| 362 WebVector<WebServiceWorkerCache::BatchOperation> expectedDeleteOperations(si
ze_t(1)); |
| 363 { |
| 364 WebServiceWorkerCache::BatchOperation deleteOperation; |
| 365 deleteOperation.operationType = WebServiceWorkerCache::OperationTypeDele
te; |
| 366 request->populateWebServiceWorkerRequest(deleteOperation.request); |
| 367 deleteOperation.matchParams = expectedQueryParams; |
| 368 expectedDeleteOperations[0] = deleteOperation; |
| 369 } |
| 370 testCache->setExpectedBatchOperations(&expectedDeleteOperations); |
| 371 |
| 372 ScriptPromise deleteResult = cache->deleteFunction(scriptState(), request, q
ueryParamsDict); |
| 373 EXPECT_EQ("dispatchBatch", testCache->getAndClearLastErrorWebCacheMethodCall
ed()); |
| 374 EXPECT_EQ(kNotImplementedString, getRejectString(deleteResult)); |
| 375 |
| 376 ScriptPromise stringDeleteResult = cache->deleteFunction(scriptState(), url,
queryParamsDict); |
| 377 EXPECT_EQ("dispatchBatch", testCache->getAndClearLastErrorWebCacheMethodCall
ed()); |
| 378 EXPECT_EQ(kNotImplementedString, getRejectString(stringDeleteResult)); |
| 379 |
| 380 WebVector<WebServiceWorkerCache::BatchOperation> expectedPutOperations(size_
t(1)); |
| 381 { |
| 382 WebServiceWorkerCache::BatchOperation putOperation; |
| 383 putOperation.operationType = WebServiceWorkerCache::OperationTypePut; |
| 384 request->populateWebServiceWorkerRequest(putOperation.request); |
| 385 response->populateWebServiceWorkerResponse(putOperation.response); |
| 386 expectedPutOperations[0] = putOperation; |
| 387 } |
| 388 testCache->setExpectedBatchOperations(&expectedPutOperations); |
| 389 |
| 390 request = newRequestFromUrl(url); |
| 391 ASSERT(request); |
| 392 ScriptPromise putResult = cache->put(scriptState(), request, response); |
| 393 EXPECT_EQ("dispatchBatch", testCache->getAndClearLastErrorWebCacheMethodCall
ed()); |
| 394 EXPECT_EQ(kNotImplementedString, getRejectString(putResult)); |
| 395 |
| 396 ScriptPromise stringPutResult = cache->put(scriptState(), url, response); |
| 397 EXPECT_EQ("dispatchBatch", testCache->getAndClearLastErrorWebCacheMethodCall
ed()); |
| 398 EXPECT_EQ(kNotImplementedString, getRejectString(stringPutResult)); |
| 399 |
| 400 // FIXME: test add & addAll. |
| 401 } |
| 402 |
| 403 class MatchTestCache : public NotImplementedErrorCache { |
| 404 public: |
| 405 MatchTestCache(WebServiceWorkerResponse& response) |
| 406 : m_response(response) { } |
| 407 |
| 408 // From WebServiceWorkerCache: |
| 409 virtual void dispatchMatch(CacheMatchCallbacks* callbacks, const WebServiceW
orkerRequest& webRequest, const QueryParams& queryParams) OVERRIDE |
| 410 { |
| 411 OwnPtr<CacheMatchCallbacks> ownedCallbacks(adoptPtr(callbacks)); |
| 412 return callbacks->onSuccess(&m_response); |
| 413 } |
| 414 |
| 415 private: |
| 416 WebServiceWorkerResponse& m_response; |
| 417 }; |
| 418 |
| 419 TEST_F(ServiceWorkerCacheTest, MatchResponseTest) |
| 420 { |
| 421 const String requestUrl = "http://request.url/"; |
| 422 const String responseUrl = "http://match.response.test/"; |
| 423 |
| 424 WebServiceWorkerResponse webResponse; |
| 425 webResponse.setURL(KURL(ParsedURLString, responseUrl)); |
| 426 |
| 427 Cache* cache = Cache::create(new MatchTestCache(webResponse)); |
| 428 |
| 429 ScriptPromise result = cache->match(scriptState(), requestUrl, Dictionary())
; |
| 430 ScriptValue scriptValue = getResolveValue(result); |
| 431 Response* response = V8Response::toImplWithTypeCheck(isolate(), scriptValue.
v8Value()); |
| 432 ASSERT_TRUE(response); |
| 433 EXPECT_EQ(responseUrl, response->url()); |
| 434 } |
| 435 |
| 436 class KeysTestCache : public NotImplementedErrorCache { |
| 437 public: |
| 438 KeysTestCache(WebVector<WebServiceWorkerRequest>& requests) |
| 439 : m_requests(requests) { } |
| 440 |
| 441 virtual void dispatchKeys(CacheWithRequestsCallbacks* callbacks, const WebSe
rviceWorkerRequest* webRequest, const QueryParams& queryParams) OVERRIDE |
| 442 { |
| 443 OwnPtr<CacheWithRequestsCallbacks> ownedCallbacks(adoptPtr(callbacks)); |
| 444 return callbacks->onSuccess(&m_requests); |
| 445 } |
| 446 |
| 447 private: |
| 448 WebVector<WebServiceWorkerRequest>& m_requests; |
| 449 }; |
| 450 |
| 451 TEST_F(ServiceWorkerCacheTest, KeysResponseTest) |
| 452 { |
| 453 const String url1 = "http://first.request/"; |
| 454 const String url2 = "http://second.request/"; |
| 455 |
| 456 Vector<String> expectedUrls(size_t(2)); |
| 457 expectedUrls[0] = url1; |
| 458 expectedUrls[1] = url2; |
| 459 |
| 460 WebVector<WebServiceWorkerRequest> webRequests(size_t(2)); |
| 461 webRequests[0].setURL(KURL(ParsedURLString, url1)); |
| 462 webRequests[1].setURL(KURL(ParsedURLString, url2)); |
| 463 |
| 464 Cache* cache = Cache::create(new KeysTestCache(webRequests)); |
| 465 |
| 466 ScriptPromise result = cache->keys(scriptState()); |
| 467 ScriptValue scriptValue = getResolveValue(result); |
| 468 |
| 469 Vector<v8::Handle<v8::Value> > requests = toImplArray<v8::Handle<v8::Value>
>(scriptValue.v8Value(), 0, isolate()); |
| 470 EXPECT_EQ(expectedUrls.size(), requests.size()); |
| 471 for (int i = 0, minsize = std::min(expectedUrls.size(), requests.size()); i
< minsize; ++i) { |
| 472 Request* request = V8Request::toImplWithTypeCheck(isolate(), requests[i]
); |
| 473 EXPECT_TRUE(request); |
| 474 if (request) |
| 475 EXPECT_EQ(expectedUrls[i], request->url()); |
| 476 } |
| 477 } |
| 478 |
| 479 class MatchAllAndBatchTestCache : public NotImplementedErrorCache { |
| 480 public: |
| 481 MatchAllAndBatchTestCache(WebVector<WebServiceWorkerResponse>& responses) |
| 482 : m_responses(responses) { } |
| 483 |
| 484 virtual void dispatchMatchAll(CacheWithResponsesCallbacks* callbacks, const
WebServiceWorkerRequest& webRequest, const QueryParams& queryParams) OVERRIDE |
| 485 { |
| 486 OwnPtr<CacheWithResponsesCallbacks> ownedCallbacks(adoptPtr(callbacks)); |
| 487 return callbacks->onSuccess(&m_responses); |
| 488 } |
| 489 |
| 490 virtual void dispatchBatch(CacheWithResponsesCallbacks* callbacks, const Web
Vector<BatchOperation>& batchOperations) OVERRIDE |
| 491 { |
| 492 OwnPtr<CacheWithResponsesCallbacks> ownedCallbacks(adoptPtr(callbacks)); |
| 493 return callbacks->onSuccess(&m_responses); |
| 494 } |
| 495 |
| 496 private: |
| 497 WebVector<WebServiceWorkerResponse>& m_responses; |
| 498 }; |
| 499 |
| 500 TEST_F(ServiceWorkerCacheTest, MatchAllAndBatchResponseTest) |
| 501 { |
| 502 const String url1 = "http://first.response/"; |
| 503 const String url2 = "http://second.response/"; |
| 504 |
| 505 Vector<String> expectedUrls(size_t(2)); |
| 506 expectedUrls[0] = url1; |
| 507 expectedUrls[1] = url2; |
| 508 |
| 509 WebVector<WebServiceWorkerResponse> webResponses(size_t(2)); |
| 510 webResponses[0].setURL(KURL(ParsedURLString, url1)); |
| 511 webResponses[1].setURL(KURL(ParsedURLString, url2)); |
| 512 |
| 513 Cache* cache = Cache::create(new MatchAllAndBatchTestCache(webResponses)); |
| 514 |
| 515 ScriptPromise result = cache->matchAll(scriptState(), "http://some.url/", Di
ctionary()); |
| 516 ScriptValue scriptValue = getResolveValue(result); |
| 517 |
| 518 Vector<v8::Handle<v8::Value> > responses = toImplArray<v8::Handle<v8::Value>
>(scriptValue.v8Value(), 0, isolate()); |
| 519 EXPECT_EQ(expectedUrls.size(), responses.size()); |
| 520 for (int i = 0, minsize = std::min(expectedUrls.size(), responses.size()); i
< minsize; ++i) { |
| 521 Response* response = V8Response::toImplWithTypeCheck(isolate(), response
s[i]); |
| 522 EXPECT_TRUE(response); |
| 523 if (response) |
| 524 EXPECT_EQ(expectedUrls[i], response->url()); |
| 525 } |
| 526 |
| 527 result = cache->deleteFunction(scriptState(), "http://some.url/", Dictionary
()); |
| 528 scriptValue = getResolveValue(result); |
| 529 responses = toImplArray<v8::Handle<v8::Value> >(scriptValue.v8Value(), 0, is
olate()); |
| 530 EXPECT_EQ(expectedUrls.size(), responses.size()); |
| 531 for (int i = 0, minsize = std::min(expectedUrls.size(), responses.size()); i
< minsize; ++i) { |
| 532 Response* response = V8Response::toImplWithTypeCheck(isolate(), response
s[i]); |
| 533 EXPECT_TRUE(response); |
| 534 if (response) |
| 535 EXPECT_EQ(expectedUrls[i], response->url()); |
| 536 } |
| 537 } |
| 538 |
| 539 } // namespace |
| 540 } // namespace blink |
OLD | NEW |