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