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