OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 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 "modules/fetch/BlobBytesConsumer.h" | |
6 | |
7 #include "core/dom/Document.h" | |
8 #include "core/loader/ThreadableLoader.h" | |
9 #include "core/testing/DummyPageHolder.h" | |
10 #include "modules/fetch/BytesConsumerTestUtil.h" | |
11 #include "modules/fetch/DataConsumerHandleTestUtil.h" | |
12 #include "platform/blob/BlobData.h" | |
13 #include "platform/network/EncodedFormData.h" | |
14 #include "platform/network/ResourceError.h" | |
15 #include "platform/network/ResourceResponse.h" | |
16 #include "platform/testing/UnitTestHelpers.h" | |
17 #include "testing/gtest/include/gtest/gtest.h" | |
18 #include "wtf/Vector.h" | |
19 #include "wtf/text/WTFString.h" | |
20 | |
21 namespace blink { | |
22 | |
23 namespace { | |
24 | |
25 using Command = DataConsumerHandleTestUtil::Command; | |
26 using PublicState = BytesConsumer::PublicState; | |
27 using ReplayingHandle = DataConsumerHandleTestUtil::ReplayingHandle; | |
28 using Result = BytesConsumer::Result; | |
29 | |
30 String toString(const Vector<char>& v) | |
31 { | |
32 return String(v.data(), v.size()); | |
33 } | |
34 | |
35 class TestThreadableLoader : public ThreadableLoader { | |
36 public: | |
37 ~TestThreadableLoader() override | |
38 { | |
39 EXPECT_FALSE(m_shouldBeCancelled && !m_isCancelled) << "The loader shoul d be cancelled but is not cancelled."; | |
40 } | |
41 | |
42 void start(const ResourceRequest& request) override | |
43 { | |
44 m_isStarted = true; | |
45 } | |
46 | |
47 void overrideTimeout(unsigned long timeoutMilliseconds) override | |
48 { | |
49 ADD_FAILURE() << "overrideTimeout should not be called."; | |
50 } | |
51 | |
52 void cancel() override | |
53 { | |
54 m_isCancelled = true; | |
55 } | |
56 | |
57 bool isStarted() const { return m_isStarted; } | |
58 bool isCancelled() const { return m_isCancelled; } | |
59 void setShouldBeCancelled() { m_shouldBeCancelled = true; } | |
60 | |
61 private: | |
62 bool m_isStarted = false; | |
63 bool m_isCancelled = false; | |
64 bool m_shouldBeCancelled = false; | |
65 }; | |
66 | |
67 class SyncLoadingTestThreadableLoader : public ThreadableLoader { | |
68 public: | |
69 ~SyncLoadingTestThreadableLoader() override | |
70 { | |
71 DCHECK(!m_handle); | |
72 } | |
73 | |
74 void start(const ResourceRequest& request) override | |
75 { | |
76 m_isStarted = true; | |
77 m_client->didReceiveResponse(0, ResourceResponse(), std::move(m_handle)) ; | |
78 m_client->didFinishLoading(0, 0); | |
79 } | |
80 | |
81 void overrideTimeout(unsigned long timeoutMilliseconds) override | |
82 { | |
83 ADD_FAILURE() << "overrideTimeout should not be called."; | |
84 } | |
85 | |
86 void cancel() override | |
87 { | |
88 m_isCancelled = true; | |
89 } | |
90 | |
91 bool isStarted() const { return m_isStarted; } | |
92 bool isCancelled() const { return m_isCancelled; } | |
93 | |
94 void setClient(ThreadableLoaderClient* client) | |
95 { | |
96 m_client = client; | |
97 } | |
98 | |
99 void setHandle(std::unique_ptr<WebDataConsumerHandle> handle) | |
100 { | |
101 m_handle = std::move(handle); | |
102 } | |
103 | |
104 private: | |
105 bool m_isStarted = false; | |
106 bool m_isCancelled = false; | |
107 ThreadableLoaderClient* m_client = nullptr; | |
108 std::unique_ptr<WebDataConsumerHandle> m_handle; | |
109 }; | |
110 | |
111 class SyncErrorTestThreadableLoader : public ThreadableLoader { | |
112 public: | |
113 ~SyncErrorTestThreadableLoader() override {} | |
114 | |
115 void start(const ResourceRequest& request) override | |
116 { | |
117 m_isStarted = true; | |
118 m_client->didFail(ResourceError()); | |
119 } | |
120 | |
121 void overrideTimeout(unsigned long timeoutMilliseconds) override | |
122 { | |
123 ADD_FAILURE() << "overrideTimeout should not be called."; | |
124 } | |
125 | |
126 void cancel() override | |
127 { | |
128 m_isCancelled = true; | |
129 } | |
130 | |
131 bool isStarted() const { return m_isStarted; } | |
132 bool isCancelled() const { return m_isCancelled; } | |
133 | |
134 void setClient(ThreadableLoaderClient* client) | |
135 { | |
136 m_client = client; | |
137 } | |
138 | |
139 private: | |
140 bool m_isStarted = false; | |
141 bool m_isCancelled = false; | |
142 ThreadableLoaderClient* m_client = nullptr; | |
143 }; | |
144 | |
145 class TestClient final : public GarbageCollectedFinalized<TestClient>, public By tesConsumer::Client { | |
146 USING_GARBAGE_COLLECTED_MIXIN(TestClient); | |
147 public: | |
148 void onStateChange() override { ++m_numOnStateChangeCalled; } | |
149 int numOnStateChangeCalled() const { return m_numOnStateChangeCalled; } | |
150 | |
151 private: | |
152 int m_numOnStateChangeCalled = 0; | |
153 }; | |
154 | |
155 class BlobBytesConsumerTest : public ::testing::Test { | |
156 public: | |
157 BlobBytesConsumerTest() | |
158 : m_dummyPageHolder(DummyPageHolder::create(IntSize(1, 1))) {} | |
159 | |
160 Document& document() { return m_dummyPageHolder->document(); } | |
161 | |
162 private: | |
163 std::unique_ptr<DummyPageHolder> m_dummyPageHolder; | |
164 }; | |
165 | |
166 TEST_F(BlobBytesConsumerTest, TwoPhaseRead) | |
167 { | |
168 RefPtr<BlobDataHandle> blobDataHandle = BlobDataHandle::create(BlobData::cre ate(), 12345); | |
169 TestThreadableLoader* loader = new TestThreadableLoader(); | |
170 BlobBytesConsumer* consumer = BlobBytesConsumer::createForTesting(&document( ), blobDataHandle, loader); | |
171 std::unique_ptr<ReplayingHandle> src = ReplayingHandle::create(); | |
172 src->add(Command(Command::Data, "hello, ")); | |
173 src->add(Command(Command::Wait)); | |
174 src->add(Command(Command::Data, "world")); | |
175 src->add(Command(Command::Done)); | |
176 | |
177 EXPECT_EQ(PublicState::ReadableOrWaiting, consumer->getPublicState()); | |
178 EXPECT_FALSE(loader->isStarted()); | |
179 | |
180 const char* buffer = nullptr; | |
181 size_t available = 0; | |
182 EXPECT_EQ(Result::ShouldWait, consumer->beginRead(&buffer, &available)); | |
183 EXPECT_TRUE(loader->isStarted()); | |
184 EXPECT_FALSE(consumer->drainAsBlobDataHandle(BytesConsumer::BlobSizePolicy:: AllowBlobWithInvalidSize)); | |
185 EXPECT_FALSE(consumer->drainAsFormData()); | |
186 EXPECT_EQ(PublicState::ReadableOrWaiting, consumer->getPublicState()); | |
187 | |
188 consumer->didReceiveResponse(0, ResourceResponse(), std::move(src)); | |
189 consumer->didFinishLoading(0, 0); | |
190 | |
191 auto result = (new BytesConsumerTestUtil::TwoPhaseReader(consumer))->run(); | |
192 EXPECT_EQ(Result::Done, result.first); | |
193 EXPECT_EQ("hello, world", toString(result.second)); | |
194 } | |
195 | |
196 TEST_F(BlobBytesConsumerTest, FailLoading) | |
197 { | |
198 RefPtr<BlobDataHandle> blobDataHandle = BlobDataHandle::create(BlobData::cre ate(), 12345); | |
199 TestThreadableLoader* loader = new TestThreadableLoader(); | |
200 BlobBytesConsumer* consumer = BlobBytesConsumer::createForTesting(&document( ), blobDataHandle, loader); | |
201 TestClient* client = new TestClient(); | |
202 consumer->setClient(client); | |
203 | |
204 const char* buffer = nullptr; | |
205 size_t available = 0; | |
206 EXPECT_EQ(Result::ShouldWait, consumer->beginRead(&buffer, &available)); | |
207 EXPECT_TRUE(loader->isStarted()); | |
208 EXPECT_EQ(PublicState::ReadableOrWaiting, consumer->getPublicState()); | |
209 | |
210 int numOnStateChangeCalled = client->numOnStateChangeCalled(); | |
211 consumer->didFail(ResourceError()); | |
212 | |
213 EXPECT_EQ(numOnStateChangeCalled + 1, client->numOnStateChangeCalled()); | |
214 EXPECT_EQ(PublicState::Errored, consumer->getPublicState()); | |
215 EXPECT_EQ(Result::Error, consumer->beginRead(&buffer, &available)); | |
216 } | |
217 | |
218 TEST_F(BlobBytesConsumerTest, FailLoadingAfterResponseReceived) | |
219 { | |
220 RefPtr<BlobDataHandle> blobDataHandle = BlobDataHandle::create(BlobData::cre ate(), 12345); | |
221 TestThreadableLoader* loader = new TestThreadableLoader(); | |
222 BlobBytesConsumer* consumer = BlobBytesConsumer::createForTesting(&document( ), blobDataHandle, loader); | |
223 TestClient* client = new TestClient(); | |
224 consumer->setClient(client); | |
225 | |
226 const char* buffer = nullptr; | |
227 size_t available; | |
228 EXPECT_EQ(Result::ShouldWait, consumer->beginRead(&buffer, &available)); | |
229 EXPECT_TRUE(loader->isStarted()); | |
230 EXPECT_EQ(PublicState::ReadableOrWaiting, consumer->getPublicState()); | |
231 | |
232 int numOnStateChangeCalled = client->numOnStateChangeCalled(); | |
233 consumer->didReceiveResponse(0, ResourceResponse(), createWaitingDataConsume rHandle()); | |
234 EXPECT_EQ(numOnStateChangeCalled + 1, client->numOnStateChangeCalled()); | |
235 EXPECT_EQ(Result::ShouldWait, consumer->beginRead(&buffer, &available)); | |
236 EXPECT_EQ(PublicState::ReadableOrWaiting, consumer->getPublicState()); | |
237 | |
238 consumer->didFail(ResourceError()); | |
239 EXPECT_EQ(numOnStateChangeCalled + 2, client->numOnStateChangeCalled()); | |
240 EXPECT_EQ(PublicState::Errored, consumer->getPublicState()); | |
241 EXPECT_EQ(Result::Error, consumer->beginRead(&buffer, &available)); | |
242 } | |
243 | |
244 TEST_F(BlobBytesConsumerTest, FailAccessControlCheck) | |
245 { | |
246 RefPtr<BlobDataHandle> blobDataHandle = BlobDataHandle::create(BlobData::cre ate(), 12345); | |
247 TestThreadableLoader* loader = new TestThreadableLoader(); | |
248 BlobBytesConsumer* consumer = BlobBytesConsumer::createForTesting(&document( ), blobDataHandle, loader); | |
249 TestClient* client = new TestClient(); | |
250 consumer->setClient(client); | |
251 | |
252 const char* buffer = nullptr; | |
253 size_t available; | |
254 EXPECT_EQ(Result::ShouldWait, consumer->beginRead(&buffer, &available)); | |
255 EXPECT_TRUE(loader->isStarted()); | |
256 EXPECT_EQ(PublicState::ReadableOrWaiting, consumer->getPublicState()); | |
257 | |
258 int numOnStateChangeCalled = client->numOnStateChangeCalled(); | |
259 consumer->didFailAccessControlCheck(ResourceError()); | |
260 EXPECT_EQ(numOnStateChangeCalled + 1, client->numOnStateChangeCalled()); | |
261 | |
262 EXPECT_EQ(PublicState::Errored, consumer->getPublicState()); | |
263 EXPECT_EQ(Result::Error, consumer->beginRead(&buffer, &available)); | |
264 } | |
265 | |
266 TEST_F(BlobBytesConsumerTest, CancelBeforeStarting) | |
267 { | |
268 RefPtr<BlobDataHandle> blobDataHandle = BlobDataHandle::create(BlobData::cre ate(), 12345); | |
269 TestThreadableLoader* loader = new TestThreadableLoader(); | |
270 BlobBytesConsumer* consumer = BlobBytesConsumer::createForTesting(&document( ), blobDataHandle, loader); | |
271 TestClient* client = new TestClient(); | |
272 consumer->setClient(client); | |
273 | |
274 consumer->cancel(); | |
275 // This should be FALSE in production, but TRUE here because we set the | |
276 // loader before starting loading in tests. | |
277 EXPECT_TRUE(loader->isCancelled()); | |
278 | |
279 const char* buffer = nullptr; | |
280 size_t available; | |
281 EXPECT_EQ(Result::Done, consumer->beginRead(&buffer, &available)); | |
282 EXPECT_EQ(PublicState::Closed, consumer->getPublicState()); | |
283 EXPECT_FALSE(loader->isStarted()); | |
284 EXPECT_EQ(0, client->numOnStateChangeCalled()); | |
285 } | |
286 | |
287 TEST_F(BlobBytesConsumerTest, CancelAfterStarting) | |
288 { | |
289 RefPtr<BlobDataHandle> blobDataHandle = BlobDataHandle::create(BlobData::cre ate(), 12345); | |
290 TestThreadableLoader* loader = new TestThreadableLoader(); | |
291 BlobBytesConsumer* consumer = BlobBytesConsumer::createForTesting(&document( ), blobDataHandle, loader); | |
292 TestClient* client = new TestClient(); | |
293 consumer->setClient(client); | |
294 | |
295 const char* buffer = nullptr; | |
296 size_t available; | |
297 EXPECT_EQ(Result::ShouldWait, consumer->beginRead(&buffer, &available)); | |
298 EXPECT_EQ(PublicState::ReadableOrWaiting, consumer->getPublicState()); | |
299 EXPECT_TRUE(loader->isStarted()); | |
300 EXPECT_EQ(0, client->numOnStateChangeCalled()); | |
301 | |
302 consumer->cancel(); | |
303 EXPECT_TRUE(loader->isCancelled()); | |
304 EXPECT_EQ(PublicState::Closed, consumer->getPublicState()); | |
305 EXPECT_EQ(Result::Done, consumer->beginRead(&buffer, &available)); | |
306 EXPECT_EQ(0, client->numOnStateChangeCalled()); | |
307 } | |
308 | |
309 TEST_F(BlobBytesConsumerTest, ReadLastChunkBeforeDidFinishLoadingArrives) | |
310 { | |
311 RefPtr<BlobDataHandle> blobDataHandle = BlobDataHandle::create(BlobData::cre ate(), 12345); | |
312 TestThreadableLoader* loader = new TestThreadableLoader(); | |
313 BlobBytesConsumer* consumer = BlobBytesConsumer::createForTesting(&document( ), blobDataHandle, loader); | |
314 TestClient* client = new TestClient(); | |
315 consumer->setClient(client); | |
316 std::unique_ptr<ReplayingHandle> src = ReplayingHandle::create(); | |
317 src->add(Command(Command::Data, "hello")); | |
318 src->add(Command(Command::Done)); | |
319 | |
320 EXPECT_EQ(PublicState::ReadableOrWaiting, consumer->getPublicState()); | |
321 EXPECT_FALSE(loader->isStarted()); | |
322 | |
323 const char* buffer = nullptr; | |
324 size_t available; | |
325 EXPECT_EQ(Result::ShouldWait, consumer->beginRead(&buffer, &available)); | |
326 EXPECT_EQ(PublicState::ReadableOrWaiting, consumer->getPublicState()); | |
327 EXPECT_TRUE(loader->isStarted()); | |
328 EXPECT_EQ(0, client->numOnStateChangeCalled()); | |
329 | |
330 consumer->didReceiveResponse(0, ResourceResponse(), std::move(src)); | |
331 EXPECT_EQ(1, client->numOnStateChangeCalled()); | |
332 testing::runPendingTasks(); | |
333 EXPECT_EQ(2, client->numOnStateChangeCalled()); | |
334 | |
335 EXPECT_EQ(PublicState::ReadableOrWaiting, consumer->getPublicState()); | |
336 ASSERT_EQ(Result::Ok, consumer->beginRead(&buffer, &available)); | |
337 ASSERT_EQ(5u, available); | |
338 EXPECT_EQ("hello", String(buffer, available)); | |
339 ASSERT_EQ(Result::Ok, consumer->endRead(available)); | |
340 | |
341 EXPECT_EQ(PublicState::ReadableOrWaiting, consumer->getPublicState()); | |
342 ASSERT_EQ(Result::ShouldWait, consumer->beginRead(&buffer, &available)); | |
343 EXPECT_EQ(PublicState::ReadableOrWaiting, consumer->getPublicState()); | |
344 | |
345 consumer->didFinishLoading(0, 0); | |
346 EXPECT_EQ(3, client->numOnStateChangeCalled()); | |
347 EXPECT_EQ(PublicState::Closed, consumer->getPublicState()); | |
348 ASSERT_EQ(Result::Done, consumer->beginRead(&buffer, &available)); | |
349 } | |
350 | |
351 TEST_F(BlobBytesConsumerTest, ReadLastChunkAfterDidFinishLoadingArrives) | |
352 { | |
353 RefPtr<BlobDataHandle> blobDataHandle = BlobDataHandle::create(BlobData::cre ate(), 12345); | |
354 TestThreadableLoader* loader = new TestThreadableLoader(); | |
355 BlobBytesConsumer* consumer = BlobBytesConsumer::createForTesting(&document( ), blobDataHandle, loader); | |
356 TestClient* client = new TestClient(); | |
357 consumer->setClient(client); | |
358 std::unique_ptr<ReplayingHandle> src = ReplayingHandle::create(); | |
359 src->add(Command(Command::Data, "hello")); | |
360 src->add(Command(Command::Done)); | |
361 | |
362 EXPECT_EQ(PublicState::ReadableOrWaiting, consumer->getPublicState()); | |
363 EXPECT_FALSE(loader->isStarted()); | |
364 | |
365 const char* buffer = nullptr; | |
366 size_t available; | |
367 EXPECT_EQ(Result::ShouldWait, consumer->beginRead(&buffer, &available)); | |
368 EXPECT_EQ(PublicState::ReadableOrWaiting, consumer->getPublicState()); | |
369 EXPECT_TRUE(loader->isStarted()); | |
370 EXPECT_EQ(0, client->numOnStateChangeCalled()); | |
371 | |
372 consumer->didReceiveResponse(0, ResourceResponse(), std::move(src)); | |
373 EXPECT_EQ(1, client->numOnStateChangeCalled()); | |
374 testing::runPendingTasks(); | |
375 EXPECT_EQ(2, client->numOnStateChangeCalled()); | |
376 | |
377 consumer->didFinishLoading(0, 0); | |
378 testing::runPendingTasks(); | |
379 EXPECT_EQ(2, client->numOnStateChangeCalled()); | |
380 | |
381 EXPECT_EQ(PublicState::ReadableOrWaiting, consumer->getPublicState()); | |
382 ASSERT_EQ(Result::Ok, consumer->beginRead(&buffer, &available)); | |
383 ASSERT_EQ(5u, available); | |
384 EXPECT_EQ("hello", String(buffer, available)); | |
385 ASSERT_EQ(Result::Ok, consumer->endRead(available)); | |
386 | |
387 EXPECT_EQ(PublicState::ReadableOrWaiting, consumer->getPublicState()); | |
388 EXPECT_EQ(Result::Done, consumer->beginRead(&buffer, &available)); | |
389 EXPECT_EQ(2, client->numOnStateChangeCalled()); | |
390 } | |
391 | |
392 TEST_F(BlobBytesConsumerTest, DrainAsBlobDataHandle) | |
393 { | |
394 RefPtr<BlobDataHandle> blobDataHandle = BlobDataHandle::create(BlobData::cre ate(), 12345); | |
395 TestThreadableLoader* loader = new TestThreadableLoader(); | |
396 BlobBytesConsumer* consumer = BlobBytesConsumer::createForTesting(&document( ), blobDataHandle, loader); | |
397 | |
398 EXPECT_EQ(PublicState::ReadableOrWaiting, consumer->getPublicState()); | |
399 EXPECT_FALSE(loader->isStarted()); | |
400 | |
401 RefPtr<BlobDataHandle> result = consumer->drainAsBlobDataHandle(BytesConsume r::BlobSizePolicy::DisallowBlobWithInvalidSize); | |
402 ASSERT_TRUE(result); | |
403 EXPECT_FALSE(consumer->drainAsBlobDataHandle(BytesConsumer::BlobSizePolicy:: DisallowBlobWithInvalidSize)); | |
404 EXPECT_EQ(12345u, result->size()); | |
hiroshige
2016/09/27 07:31:35
Could you check |EXPECT_FALSE(loader->isStarted())
yhirano
2016/09/27 07:47:06
Done.
| |
405 } | |
406 | |
407 | |
408 TEST_F(BlobBytesConsumerTest, DrainAsBlobDataHandle_2) | |
409 { | |
410 RefPtr<BlobDataHandle> blobDataHandle = BlobDataHandle::create(BlobData::cre ate(), -1); | |
411 TestThreadableLoader* loader = new TestThreadableLoader(); | |
412 BlobBytesConsumer* consumer = BlobBytesConsumer::createForTesting(&document( ), blobDataHandle, loader); | |
413 | |
414 EXPECT_EQ(PublicState::ReadableOrWaiting, consumer->getPublicState()); | |
415 EXPECT_FALSE(loader->isStarted()); | |
416 | |
417 RefPtr<BlobDataHandle> result = consumer->drainAsBlobDataHandle(BytesConsume r::BlobSizePolicy::AllowBlobWithInvalidSize); | |
418 ASSERT_TRUE(result); | |
419 EXPECT_FALSE(consumer->drainAsBlobDataHandle(BytesConsumer::BlobSizePolicy:: AllowBlobWithInvalidSize)); | |
420 EXPECT_EQ(UINT64_MAX, result->size()); | |
hiroshige
2016/09/27 07:31:35
ditto.
yhirano
2016/09/27 07:47:06
Done.
| |
421 } | |
422 | |
423 TEST_F(BlobBytesConsumerTest, DrainAsBlobDataHandle_3) | |
424 { | |
425 RefPtr<BlobDataHandle> blobDataHandle = BlobDataHandle::create(BlobData::cre ate(), -1); | |
426 TestThreadableLoader* loader = new TestThreadableLoader(); | |
427 BlobBytesConsumer* consumer = BlobBytesConsumer::createForTesting(&document( ), blobDataHandle, loader); | |
428 | |
429 EXPECT_EQ(PublicState::ReadableOrWaiting, consumer->getPublicState()); | |
430 EXPECT_FALSE(loader->isStarted()); | |
431 | |
432 EXPECT_FALSE(consumer->drainAsBlobDataHandle(BytesConsumer::BlobSizePolicy:: DisallowBlobWithInvalidSize)); | |
hiroshige
2016/09/27 07:31:35
ditto.
yhirano
2016/09/27 07:47:06
Done.
| |
433 } | |
434 | |
435 TEST_F(BlobBytesConsumerTest, DrainAsFormData) | |
436 { | |
437 RefPtr<BlobDataHandle> blobDataHandle = BlobDataHandle::create(BlobData::cre ate(), 12345); | |
438 TestThreadableLoader* loader = new TestThreadableLoader(); | |
439 BlobBytesConsumer* consumer = BlobBytesConsumer::createForTesting(&document( ), blobDataHandle, loader); | |
440 | |
441 EXPECT_EQ(PublicState::ReadableOrWaiting, consumer->getPublicState()); | |
442 EXPECT_FALSE(loader->isStarted()); | |
443 | |
444 RefPtr<EncodedFormData> result = consumer->drainAsFormData(); | |
445 ASSERT_TRUE(result); | |
446 ASSERT_EQ(1u, result->elements().size()); | |
447 ASSERT_EQ(FormDataElement::encodedBlob, result->elements()[0].m_type); | |
448 ASSERT_TRUE(result->elements()[0].m_optionalBlobDataHandle); | |
449 EXPECT_EQ(12345u, result->elements()[0].m_optionalBlobDataHandle->size()); | |
450 EXPECT_EQ(blobDataHandle->uuid(), result->elements()[0].m_blobUUID); | |
hiroshige
2016/09/27 07:31:35
ditto.
yhirano
2016/09/27 07:47:06
Done.
| |
451 } | |
452 | |
453 TEST_F(BlobBytesConsumerTest, LoaderShouldBeCancelled) | |
454 { | |
455 { | |
456 RefPtr<BlobDataHandle> blobDataHandle = BlobDataHandle::create(BlobData: :create(), 12345); | |
457 TestThreadableLoader* loader = new TestThreadableLoader(); | |
458 BlobBytesConsumer* consumer = BlobBytesConsumer::createForTesting(&docum ent(), blobDataHandle, loader); | |
459 | |
460 const char* buffer = nullptr; | |
461 size_t available; | |
462 EXPECT_EQ(Result::ShouldWait, consumer->beginRead(&buffer, &available)); | |
463 EXPECT_TRUE(loader->isStarted()); | |
464 loader->setShouldBeCancelled(); | |
465 } | |
466 ThreadState::current()->collectAllGarbage(); | |
467 } | |
468 | |
469 TEST_F(BlobBytesConsumerTest, SyncErrorDispatch) | |
470 { | |
471 RefPtr<BlobDataHandle> blobDataHandle = BlobDataHandle::create(BlobData::cre ate(), 12345); | |
472 SyncErrorTestThreadableLoader* loader = new SyncErrorTestThreadableLoader(); | |
473 BlobBytesConsumer* consumer = BlobBytesConsumer::createForTesting(&document( ), blobDataHandle, loader); | |
474 loader->setClient(consumer); | |
475 TestClient* client = new TestClient(); | |
476 consumer->setClient(client); | |
477 | |
478 const char* buffer = nullptr; | |
479 size_t available; | |
480 EXPECT_EQ(Result::Error, consumer->beginRead(&buffer, &available)); | |
481 EXPECT_TRUE(loader->isStarted()); | |
482 | |
483 EXPECT_EQ(0, client->numOnStateChangeCalled()); | |
484 EXPECT_EQ(BytesConsumer::PublicState::Errored, consumer->getPublicState()); | |
485 } | |
486 | |
487 TEST_F(BlobBytesConsumerTest, SyncLoading) | |
488 { | |
489 RefPtr<BlobDataHandle> blobDataHandle = BlobDataHandle::create(BlobData::cre ate(), 12345); | |
490 SyncLoadingTestThreadableLoader* loader = new SyncLoadingTestThreadableLoade r(); | |
491 BlobBytesConsumer* consumer = BlobBytesConsumer::createForTesting(&document( ), blobDataHandle, loader); | |
492 std::unique_ptr<ReplayingHandle> src = ReplayingHandle::create(); | |
493 src->add(Command(Command::Data, "hello, ")); | |
494 src->add(Command(Command::Wait)); | |
495 src->add(Command(Command::Data, "world")); | |
496 src->add(Command(Command::Done)); | |
497 loader->setClient(consumer); | |
498 loader->setHandle(std::move(src)); | |
499 TestClient* client = new TestClient(); | |
500 consumer->setClient(client); | |
501 | |
502 const char* buffer = nullptr; | |
503 size_t available; | |
504 ASSERT_EQ(Result::Ok, consumer->beginRead(&buffer, &available)); | |
505 EXPECT_TRUE(loader->isStarted()); | |
506 ASSERT_EQ(7u, available); | |
507 EXPECT_EQ("hello, ", String(buffer, available)); | |
508 | |
509 EXPECT_EQ(0, client->numOnStateChangeCalled()); | |
510 EXPECT_EQ(BytesConsumer::PublicState::ReadableOrWaiting, consumer->getPublic State()); | |
511 } | |
512 | |
513 TEST_F(BlobBytesConsumerTest, ConstructedFromNullHandle) | |
514 { | |
515 BlobBytesConsumer* consumer = new BlobBytesConsumer(&document(), nullptr); | |
516 const char* buffer = nullptr; | |
517 size_t available; | |
518 EXPECT_EQ(BytesConsumer::PublicState::Closed, consumer->getPublicState()); | |
519 EXPECT_EQ(Result::Done, consumer->beginRead(&buffer, &available)); | |
520 } | |
521 | |
522 } // namespace | |
523 | |
524 } // namespace blink | |
OLD | NEW |