OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "modules/fetch/BodyStreamBuffer.h" | 5 #include "modules/fetch/BodyStreamBuffer.h" |
6 | 6 |
7 #include "bindings/core/v8/V8BindingForTesting.h" | 7 #include "bindings/core/v8/V8BindingForTesting.h" |
| 8 #include "core/dom/Document.h" |
8 #include "core/html/FormData.h" | 9 #include "core/html/FormData.h" |
9 #include "modules/fetch/BlobBytesConsumer.h" | 10 #include "modules/fetch/BlobBytesConsumer.h" |
10 #include "modules/fetch/BytesConsumerTestUtil.h" | 11 #include "modules/fetch/BytesConsumerTestUtil.h" |
11 #include "modules/fetch/DataConsumerHandleTestUtil.h" | |
12 #include "modules/fetch/FormDataBytesConsumer.h" | 12 #include "modules/fetch/FormDataBytesConsumer.h" |
13 #include "platform/blob/BlobData.h" | 13 #include "platform/blob/BlobData.h" |
14 #include "platform/blob/BlobURL.h" | 14 #include "platform/blob/BlobURL.h" |
15 #include "platform/network/EncodedFormData.h" | 15 #include "platform/network/EncodedFormData.h" |
16 #include "platform/testing/UnitTestHelpers.h" | 16 #include "platform/testing/UnitTestHelpers.h" |
17 #include "testing/gmock/include/gmock/gmock.h" | 17 #include "testing/gmock/include/gmock/gmock.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
19 #include "wtf/PtrUtil.h" | 19 #include "wtf/PtrUtil.h" |
20 #include <memory> | 20 #include <memory> |
21 | 21 |
22 namespace blink { | 22 namespace blink { |
23 | 23 |
24 namespace { | 24 namespace { |
25 | 25 |
26 using ::testing::ByMove; | 26 using ::testing::ByMove; |
27 using ::testing::InSequence; | 27 using ::testing::InSequence; |
28 using ::testing::Return; | 28 using ::testing::Return; |
29 using ::testing::_; | 29 using ::testing::_; |
30 using ::testing::SaveArg; | 30 using ::testing::SaveArg; |
31 using Checkpoint = ::testing::StrictMock<::testing::MockFunction<void(int)>>; | 31 using Checkpoint = ::testing::StrictMock<::testing::MockFunction<void(int)>>; |
32 using Command = DataConsumerHandleTestUtil::Command; | 32 using Command = BytesConsumerTestUtil::Command; |
33 using ReplayingHandle = DataConsumerHandleTestUtil::ReplayingHandle; | 33 using ReplayingBytesConsumer = BytesConsumerTestUtil::ReplayingBytesConsumer; |
34 using MockFetchDataLoaderClient = | 34 using MockFetchDataLoaderClient = |
35 BytesConsumerTestUtil::MockFetchDataLoaderClient; | 35 BytesConsumerTestUtil::MockFetchDataLoaderClient; |
36 | 36 |
37 class BodyStreamBufferTest : public ::testing::Test { | 37 class BodyStreamBufferTest : public ::testing::Test { |
38 protected: | 38 protected: |
39 ScriptValue eval(ScriptState* scriptState, const char* s) { | 39 ScriptValue eval(ScriptState* scriptState, const char* s) { |
40 v8::Local<v8::String> source; | 40 v8::Local<v8::String> source; |
41 v8::Local<v8::Script> script; | 41 v8::Local<v8::Script> script; |
42 v8::MicrotasksScope microtasks(scriptState->isolate(), | 42 v8::MicrotasksScope microtasks(scriptState->isolate(), |
43 v8::MicrotasksScope::kDoNotRunMicrotasks); | 43 v8::MicrotasksScope::kDoNotRunMicrotasks); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
75 | 75 |
76 InSequence s; | 76 InSequence s; |
77 EXPECT_CALL(checkpoint, Call(0)); | 77 EXPECT_CALL(checkpoint, Call(0)); |
78 EXPECT_CALL(*client1, didFetchDataLoadedString(String("hello, world"))); | 78 EXPECT_CALL(*client1, didFetchDataLoadedString(String("hello, world"))); |
79 EXPECT_CALL(checkpoint, Call(1)); | 79 EXPECT_CALL(checkpoint, Call(1)); |
80 EXPECT_CALL(checkpoint, Call(2)); | 80 EXPECT_CALL(checkpoint, Call(2)); |
81 EXPECT_CALL(*client2, didFetchDataLoadedString(String("hello, world"))); | 81 EXPECT_CALL(*client2, didFetchDataLoadedString(String("hello, world"))); |
82 EXPECT_CALL(checkpoint, Call(3)); | 82 EXPECT_CALL(checkpoint, Call(3)); |
83 EXPECT_CALL(checkpoint, Call(4)); | 83 EXPECT_CALL(checkpoint, Call(4)); |
84 | 84 |
85 std::unique_ptr<DataConsumerHandleTestUtil::ReplayingHandle> handle = | 85 ReplayingBytesConsumer* src = new ReplayingBytesConsumer(&scope.document()); |
86 DataConsumerHandleTestUtil::ReplayingHandle::create(); | 86 src->add(Command(Command::Data, "hello, ")); |
87 handle->add(DataConsumerHandleTestUtil::Command( | 87 src->add(Command(Command::Data, "world")); |
88 DataConsumerHandleTestUtil::Command::Data, "hello, ")); | 88 src->add(Command(Command::Done)); |
89 handle->add(DataConsumerHandleTestUtil::Command( | 89 BodyStreamBuffer* buffer = new BodyStreamBuffer(scope.getScriptState(), src); |
90 DataConsumerHandleTestUtil::Command::Data, "world")); | |
91 handle->add(DataConsumerHandleTestUtil::Command( | |
92 DataConsumerHandleTestUtil::Command::Done)); | |
93 BodyStreamBuffer* buffer = new BodyStreamBuffer( | |
94 scope.getScriptState(), | |
95 createFetchDataConsumerHandleFromWebHandle(std::move(handle))); | |
96 | 90 |
97 BodyStreamBuffer* new1; | 91 BodyStreamBuffer* new1; |
98 BodyStreamBuffer* new2; | 92 BodyStreamBuffer* new2; |
99 buffer->tee(&new1, &new2); | 93 buffer->tee(&new1, &new2); |
100 | 94 |
101 EXPECT_TRUE(buffer->isStreamLocked()); | 95 EXPECT_TRUE(buffer->isStreamLocked()); |
102 EXPECT_TRUE(buffer->isStreamDisturbed()); | 96 EXPECT_TRUE(buffer->isStreamDisturbed()); |
103 EXPECT_FALSE(buffer->hasPendingActivity()); | 97 EXPECT_FALSE(buffer->hasPendingActivity()); |
104 | 98 |
105 checkpoint.Call(0); | 99 checkpoint.Call(0); |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
137 | 131 |
138 BodyStreamBuffer* buffer = | 132 BodyStreamBuffer* buffer = |
139 new BodyStreamBuffer(scope.getScriptState(), stream); | 133 new BodyStreamBuffer(scope.getScriptState(), stream); |
140 | 134 |
141 BodyStreamBuffer* new1; | 135 BodyStreamBuffer* new1; |
142 BodyStreamBuffer* new2; | 136 BodyStreamBuffer* new2; |
143 buffer->tee(&new1, &new2); | 137 buffer->tee(&new1, &new2); |
144 | 138 |
145 EXPECT_TRUE(buffer->isStreamLocked()); | 139 EXPECT_TRUE(buffer->isStreamLocked()); |
146 // Note that this behavior is slightly different from for the behavior of | 140 // Note that this behavior is slightly different from for the behavior of |
147 // a BodyStreamBuffer made from a FetchDataConsumerHandle. See the above | 141 // a BodyStreamBuffer made from a BytesConsumer. See the above test. In this |
148 // test. In this test, the stream will get disturbed when the microtask | 142 // test, the stream will get disturbed when the microtask is performed. |
149 // is performed. | |
150 // TODO(yhirano): A uniformed behavior is preferred. | 143 // TODO(yhirano): A uniformed behavior is preferred. |
151 EXPECT_FALSE(buffer->isStreamDisturbed()); | 144 EXPECT_FALSE(buffer->isStreamDisturbed()); |
152 EXPECT_FALSE(buffer->hasPendingActivity()); | 145 EXPECT_FALSE(buffer->hasPendingActivity()); |
153 | 146 |
154 v8::MicrotasksScope::PerformCheckpoint(scope.getScriptState()->isolate()); | 147 v8::MicrotasksScope::PerformCheckpoint(scope.getScriptState()->isolate()); |
155 | 148 |
156 EXPECT_TRUE(buffer->isStreamLocked()); | 149 EXPECT_TRUE(buffer->isStreamLocked()); |
157 EXPECT_TRUE(buffer->isStreamDisturbed()); | 150 EXPECT_TRUE(buffer->isStreamDisturbed()); |
158 EXPECT_FALSE(buffer->hasPendingActivity()); | 151 EXPECT_FALSE(buffer->hasPendingActivity()); |
159 | 152 |
(...skipping 26 matching lines...) Expand all Loading... |
186 BytesConsumer::BlobSizePolicy::AllowBlobWithInvalidSize); | 179 BytesConsumer::BlobSizePolicy::AllowBlobWithInvalidSize); |
187 | 180 |
188 EXPECT_TRUE(buffer->isStreamLocked()); | 181 EXPECT_TRUE(buffer->isStreamLocked()); |
189 EXPECT_TRUE(buffer->isStreamDisturbed()); | 182 EXPECT_TRUE(buffer->isStreamDisturbed()); |
190 EXPECT_FALSE(buffer->hasPendingActivity()); | 183 EXPECT_FALSE(buffer->hasPendingActivity()); |
191 EXPECT_EQ(blobDataHandle, outputBlobDataHandle); | 184 EXPECT_EQ(blobDataHandle, outputBlobDataHandle); |
192 } | 185 } |
193 | 186 |
194 TEST_F(BodyStreamBufferTest, DrainAsBlobDataHandleReturnsNull) { | 187 TEST_F(BodyStreamBufferTest, DrainAsBlobDataHandleReturnsNull) { |
195 V8TestingScope scope; | 188 V8TestingScope scope; |
196 // This handle is not drainable. | 189 // This BytesConsumer is not drainable. |
197 std::unique_ptr<FetchDataConsumerHandle> handle = | 190 BytesConsumer* src = new ReplayingBytesConsumer(&scope.document()); |
198 createFetchDataConsumerHandleFromWebHandle( | 191 BodyStreamBuffer* buffer = new BodyStreamBuffer(scope.getScriptState(), src); |
199 createWaitingDataConsumerHandle()); | |
200 BodyStreamBuffer* buffer = | |
201 new BodyStreamBuffer(scope.getScriptState(), std::move(handle)); | |
202 | 192 |
203 EXPECT_FALSE(buffer->isStreamLocked()); | 193 EXPECT_FALSE(buffer->isStreamLocked()); |
204 EXPECT_FALSE(buffer->isStreamDisturbed()); | 194 EXPECT_FALSE(buffer->isStreamDisturbed()); |
205 EXPECT_FALSE(buffer->hasPendingActivity()); | 195 EXPECT_FALSE(buffer->hasPendingActivity()); |
206 | 196 |
207 EXPECT_FALSE(buffer->drainAsBlobDataHandle( | 197 EXPECT_FALSE(buffer->drainAsBlobDataHandle( |
208 BytesConsumer::BlobSizePolicy::AllowBlobWithInvalidSize)); | 198 BytesConsumer::BlobSizePolicy::AllowBlobWithInvalidSize)); |
209 | 199 |
210 EXPECT_FALSE(buffer->isStreamLocked()); | 200 EXPECT_FALSE(buffer->isStreamLocked()); |
211 EXPECT_FALSE(buffer->isStreamDisturbed()); | 201 EXPECT_FALSE(buffer->isStreamDisturbed()); |
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
252 | 242 |
253 EXPECT_TRUE(buffer->isStreamLocked()); | 243 EXPECT_TRUE(buffer->isStreamLocked()); |
254 EXPECT_TRUE(buffer->isStreamDisturbed()); | 244 EXPECT_TRUE(buffer->isStreamDisturbed()); |
255 EXPECT_FALSE(buffer->hasPendingActivity()); | 245 EXPECT_FALSE(buffer->hasPendingActivity()); |
256 EXPECT_EQ(outputFormData->flattenToString(), | 246 EXPECT_EQ(outputFormData->flattenToString(), |
257 inputFormData->flattenToString()); | 247 inputFormData->flattenToString()); |
258 } | 248 } |
259 | 249 |
260 TEST_F(BodyStreamBufferTest, DrainAsFormDataReturnsNull) { | 250 TEST_F(BodyStreamBufferTest, DrainAsFormDataReturnsNull) { |
261 V8TestingScope scope; | 251 V8TestingScope scope; |
262 // This handle is not drainable. | 252 // This BytesConsumer is not drainable. |
263 std::unique_ptr<FetchDataConsumerHandle> handle = | 253 BytesConsumer* src = new ReplayingBytesConsumer(&scope.document()); |
264 createFetchDataConsumerHandleFromWebHandle( | 254 BodyStreamBuffer* buffer = new BodyStreamBuffer(scope.getScriptState(), src); |
265 createWaitingDataConsumerHandle()); | |
266 BodyStreamBuffer* buffer = | |
267 new BodyStreamBuffer(scope.getScriptState(), std::move(handle)); | |
268 | 255 |
269 EXPECT_FALSE(buffer->isStreamLocked()); | 256 EXPECT_FALSE(buffer->isStreamLocked()); |
270 EXPECT_FALSE(buffer->isStreamDisturbed()); | 257 EXPECT_FALSE(buffer->isStreamDisturbed()); |
271 EXPECT_FALSE(buffer->hasPendingActivity()); | 258 EXPECT_FALSE(buffer->hasPendingActivity()); |
272 | 259 |
273 EXPECT_FALSE(buffer->drainAsFormData()); | 260 EXPECT_FALSE(buffer->drainAsFormData()); |
274 | 261 |
275 EXPECT_FALSE(buffer->isStreamLocked()); | 262 EXPECT_FALSE(buffer->isStreamLocked()); |
276 EXPECT_FALSE(buffer->isStreamDisturbed()); | 263 EXPECT_FALSE(buffer->isStreamDisturbed()); |
277 EXPECT_FALSE(buffer->hasPendingActivity()); | 264 EXPECT_FALSE(buffer->hasPendingActivity()); |
(...skipping 25 matching lines...) Expand all Loading... |
303 Checkpoint checkpoint; | 290 Checkpoint checkpoint; |
304 MockFetchDataLoaderClient* client = MockFetchDataLoaderClient::create(); | 291 MockFetchDataLoaderClient* client = MockFetchDataLoaderClient::create(); |
305 DOMArrayBuffer* arrayBuffer = nullptr; | 292 DOMArrayBuffer* arrayBuffer = nullptr; |
306 | 293 |
307 InSequence s; | 294 InSequence s; |
308 EXPECT_CALL(checkpoint, Call(1)); | 295 EXPECT_CALL(checkpoint, Call(1)); |
309 EXPECT_CALL(*client, didFetchDataLoadedArrayBufferMock(_)) | 296 EXPECT_CALL(*client, didFetchDataLoadedArrayBufferMock(_)) |
310 .WillOnce(SaveArg<0>(&arrayBuffer)); | 297 .WillOnce(SaveArg<0>(&arrayBuffer)); |
311 EXPECT_CALL(checkpoint, Call(2)); | 298 EXPECT_CALL(checkpoint, Call(2)); |
312 | 299 |
313 std::unique_ptr<ReplayingHandle> handle = ReplayingHandle::create(); | 300 ReplayingBytesConsumer* src = new ReplayingBytesConsumer(&scope.document()); |
314 handle->add(Command(Command::Wait)); | 301 src->add(Command(Command::Wait)); |
315 handle->add(Command(Command::Data, "hello")); | 302 src->add(Command(Command::Data, "hello")); |
316 handle->add(Command(Command::Done)); | 303 src->add(Command(Command::Done)); |
317 BodyStreamBuffer* buffer = new BodyStreamBuffer( | 304 BodyStreamBuffer* buffer = new BodyStreamBuffer(scope.getScriptState(), src); |
318 scope.getScriptState(), | |
319 createFetchDataConsumerHandleFromWebHandle(std::move(handle))); | |
320 buffer->startLoading(FetchDataLoader::createLoaderAsArrayBuffer(), client); | 305 buffer->startLoading(FetchDataLoader::createLoaderAsArrayBuffer(), client); |
321 | 306 |
322 EXPECT_TRUE(buffer->isStreamLocked()); | 307 EXPECT_TRUE(buffer->isStreamLocked()); |
323 EXPECT_TRUE(buffer->isStreamDisturbed()); | 308 EXPECT_TRUE(buffer->isStreamDisturbed()); |
324 EXPECT_TRUE(buffer->hasPendingActivity()); | 309 EXPECT_TRUE(buffer->hasPendingActivity()); |
325 | 310 |
326 checkpoint.Call(1); | 311 checkpoint.Call(1); |
327 testing::runPendingTasks(); | 312 testing::runPendingTasks(); |
328 checkpoint.Call(2); | 313 checkpoint.Call(2); |
329 | 314 |
(...skipping 10 matching lines...) Expand all Loading... |
340 Checkpoint checkpoint; | 325 Checkpoint checkpoint; |
341 MockFetchDataLoaderClient* client = MockFetchDataLoaderClient::create(); | 326 MockFetchDataLoaderClient* client = MockFetchDataLoaderClient::create(); |
342 RefPtr<BlobDataHandle> blobDataHandle; | 327 RefPtr<BlobDataHandle> blobDataHandle; |
343 | 328 |
344 InSequence s; | 329 InSequence s; |
345 EXPECT_CALL(checkpoint, Call(1)); | 330 EXPECT_CALL(checkpoint, Call(1)); |
346 EXPECT_CALL(*client, didFetchDataLoadedBlobHandleMock(_)) | 331 EXPECT_CALL(*client, didFetchDataLoadedBlobHandleMock(_)) |
347 .WillOnce(SaveArg<0>(&blobDataHandle)); | 332 .WillOnce(SaveArg<0>(&blobDataHandle)); |
348 EXPECT_CALL(checkpoint, Call(2)); | 333 EXPECT_CALL(checkpoint, Call(2)); |
349 | 334 |
350 std::unique_ptr<ReplayingHandle> handle = ReplayingHandle::create(); | 335 ReplayingBytesConsumer* src = new ReplayingBytesConsumer(&scope.document()); |
351 handle->add(Command(Command::Wait)); | 336 src->add(Command(Command::Wait)); |
352 handle->add(Command(Command::Data, "hello")); | 337 src->add(Command(Command::Data, "hello")); |
353 handle->add(Command(Command::Done)); | 338 src->add(Command(Command::Done)); |
354 BodyStreamBuffer* buffer = new BodyStreamBuffer( | 339 BodyStreamBuffer* buffer = new BodyStreamBuffer(scope.getScriptState(), src); |
355 scope.getScriptState(), | |
356 createFetchDataConsumerHandleFromWebHandle(std::move(handle))); | |
357 buffer->startLoading(FetchDataLoader::createLoaderAsBlobHandle("text/plain"), | 340 buffer->startLoading(FetchDataLoader::createLoaderAsBlobHandle("text/plain"), |
358 client); | 341 client); |
359 | 342 |
360 EXPECT_TRUE(buffer->isStreamLocked()); | 343 EXPECT_TRUE(buffer->isStreamLocked()); |
361 EXPECT_TRUE(buffer->isStreamDisturbed()); | 344 EXPECT_TRUE(buffer->isStreamDisturbed()); |
362 EXPECT_TRUE(buffer->hasPendingActivity()); | 345 EXPECT_TRUE(buffer->hasPendingActivity()); |
363 | 346 |
364 checkpoint.Call(1); | 347 checkpoint.Call(1); |
365 testing::runPendingTasks(); | 348 testing::runPendingTasks(); |
366 checkpoint.Call(2); | 349 checkpoint.Call(2); |
367 | 350 |
368 EXPECT_TRUE(buffer->isStreamLocked()); | 351 EXPECT_TRUE(buffer->isStreamLocked()); |
369 EXPECT_TRUE(buffer->isStreamDisturbed()); | 352 EXPECT_TRUE(buffer->isStreamDisturbed()); |
370 EXPECT_FALSE(buffer->hasPendingActivity()); | 353 EXPECT_FALSE(buffer->hasPendingActivity()); |
371 EXPECT_EQ(5u, blobDataHandle->size()); | 354 EXPECT_EQ(5u, blobDataHandle->size()); |
372 } | 355 } |
373 | 356 |
374 TEST_F(BodyStreamBufferTest, LoadBodyStreamBufferAsString) { | 357 TEST_F(BodyStreamBufferTest, LoadBodyStreamBufferAsString) { |
375 V8TestingScope scope; | 358 V8TestingScope scope; |
376 Checkpoint checkpoint; | 359 Checkpoint checkpoint; |
377 MockFetchDataLoaderClient* client = MockFetchDataLoaderClient::create(); | 360 MockFetchDataLoaderClient* client = MockFetchDataLoaderClient::create(); |
378 | 361 |
379 InSequence s; | 362 InSequence s; |
380 EXPECT_CALL(checkpoint, Call(1)); | 363 EXPECT_CALL(checkpoint, Call(1)); |
381 EXPECT_CALL(*client, didFetchDataLoadedString(String("hello"))); | 364 EXPECT_CALL(*client, didFetchDataLoadedString(String("hello"))); |
382 EXPECT_CALL(checkpoint, Call(2)); | 365 EXPECT_CALL(checkpoint, Call(2)); |
383 | 366 |
384 std::unique_ptr<ReplayingHandle> handle = ReplayingHandle::create(); | 367 ReplayingBytesConsumer* src = new ReplayingBytesConsumer(&scope.document()); |
385 handle->add(Command(Command::Wait)); | 368 src->add(Command(Command::Wait)); |
386 handle->add(Command(Command::Data, "hello")); | 369 src->add(Command(Command::Data, "hello")); |
387 handle->add(Command(Command::Done)); | 370 src->add(Command(Command::Done)); |
388 BodyStreamBuffer* buffer = new BodyStreamBuffer( | 371 BodyStreamBuffer* buffer = new BodyStreamBuffer(scope.getScriptState(), src); |
389 scope.getScriptState(), | |
390 createFetchDataConsumerHandleFromWebHandle(std::move(handle))); | |
391 buffer->startLoading(FetchDataLoader::createLoaderAsString(), client); | 372 buffer->startLoading(FetchDataLoader::createLoaderAsString(), client); |
392 | 373 |
393 EXPECT_TRUE(buffer->isStreamLocked()); | 374 EXPECT_TRUE(buffer->isStreamLocked()); |
394 EXPECT_TRUE(buffer->isStreamDisturbed()); | 375 EXPECT_TRUE(buffer->isStreamDisturbed()); |
395 EXPECT_TRUE(buffer->hasPendingActivity()); | 376 EXPECT_TRUE(buffer->hasPendingActivity()); |
396 | 377 |
397 checkpoint.Call(1); | 378 checkpoint.Call(1); |
398 testing::runPendingTasks(); | 379 testing::runPendingTasks(); |
399 checkpoint.Call(2); | 380 checkpoint.Call(2); |
400 | 381 |
401 EXPECT_TRUE(buffer->isStreamLocked()); | 382 EXPECT_TRUE(buffer->isStreamLocked()); |
402 EXPECT_TRUE(buffer->isStreamDisturbed()); | 383 EXPECT_TRUE(buffer->isStreamDisturbed()); |
403 EXPECT_FALSE(buffer->hasPendingActivity()); | 384 EXPECT_FALSE(buffer->hasPendingActivity()); |
404 } | 385 } |
405 | 386 |
406 TEST_F(BodyStreamBufferTest, LoadClosedHandle) { | 387 TEST_F(BodyStreamBufferTest, LoadClosedHandle) { |
407 V8TestingScope scope; | 388 V8TestingScope scope; |
408 Checkpoint checkpoint; | 389 Checkpoint checkpoint; |
409 MockFetchDataLoaderClient* client = MockFetchDataLoaderClient::create(); | 390 MockFetchDataLoaderClient* client = MockFetchDataLoaderClient::create(); |
410 | 391 |
411 InSequence s; | 392 InSequence s; |
412 EXPECT_CALL(checkpoint, Call(1)); | 393 EXPECT_CALL(checkpoint, Call(1)); |
413 EXPECT_CALL(*client, didFetchDataLoadedString(String(""))); | 394 EXPECT_CALL(*client, didFetchDataLoadedString(String(""))); |
414 EXPECT_CALL(checkpoint, Call(2)); | 395 EXPECT_CALL(checkpoint, Call(2)); |
415 | 396 |
416 BodyStreamBuffer* buffer = new BodyStreamBuffer( | 397 BodyStreamBuffer* buffer = new BodyStreamBuffer( |
417 scope.getScriptState(), createFetchDataConsumerHandleFromWebHandle( | 398 scope.getScriptState(), BytesConsumer::createClosed()); |
418 createDoneDataConsumerHandle())); | |
419 | 399 |
420 EXPECT_TRUE(buffer->isStreamReadable()); | |
421 testing::runPendingTasks(); | |
422 EXPECT_TRUE(buffer->isStreamClosed()); | 400 EXPECT_TRUE(buffer->isStreamClosed()); |
423 | 401 |
424 EXPECT_FALSE(buffer->isStreamLocked()); | 402 EXPECT_FALSE(buffer->isStreamLocked()); |
425 EXPECT_FALSE(buffer->isStreamDisturbed()); | 403 EXPECT_FALSE(buffer->isStreamDisturbed()); |
426 EXPECT_FALSE(buffer->hasPendingActivity()); | 404 EXPECT_FALSE(buffer->hasPendingActivity()); |
427 | 405 |
428 checkpoint.Call(1); | 406 checkpoint.Call(1); |
429 buffer->startLoading(FetchDataLoader::createLoaderAsString(), client); | 407 buffer->startLoading(FetchDataLoader::createLoaderAsString(), client); |
430 checkpoint.Call(2); | 408 checkpoint.Call(2); |
431 | 409 |
432 EXPECT_TRUE(buffer->isStreamLocked()); | 410 EXPECT_TRUE(buffer->isStreamLocked()); |
433 EXPECT_TRUE(buffer->isStreamDisturbed()); | 411 EXPECT_TRUE(buffer->isStreamDisturbed()); |
434 EXPECT_FALSE(buffer->hasPendingActivity()); | 412 EXPECT_FALSE(buffer->hasPendingActivity()); |
435 } | 413 } |
436 | 414 |
437 TEST_F(BodyStreamBufferTest, LoadErroredHandle) { | 415 TEST_F(BodyStreamBufferTest, LoadErroredHandle) { |
438 V8TestingScope scope; | 416 V8TestingScope scope; |
439 Checkpoint checkpoint; | 417 Checkpoint checkpoint; |
440 MockFetchDataLoaderClient* client = MockFetchDataLoaderClient::create(); | 418 MockFetchDataLoaderClient* client = MockFetchDataLoaderClient::create(); |
441 | 419 |
442 InSequence s; | 420 InSequence s; |
443 EXPECT_CALL(checkpoint, Call(1)); | 421 EXPECT_CALL(checkpoint, Call(1)); |
444 EXPECT_CALL(*client, didFetchDataLoadFailed()); | 422 EXPECT_CALL(*client, didFetchDataLoadFailed()); |
445 EXPECT_CALL(checkpoint, Call(2)); | 423 EXPECT_CALL(checkpoint, Call(2)); |
446 | 424 |
447 BodyStreamBuffer* buffer = new BodyStreamBuffer( | 425 BodyStreamBuffer* buffer = new BodyStreamBuffer( |
448 scope.getScriptState(), createFetchDataConsumerHandleFromWebHandle( | 426 scope.getScriptState(), |
449 createUnexpectedErrorDataConsumerHandle())); | 427 BytesConsumer::createErrored(BytesConsumer::Error())); |
450 | 428 |
451 EXPECT_TRUE(buffer->isStreamReadable()); | |
452 testing::runPendingTasks(); | |
453 EXPECT_TRUE(buffer->isStreamErrored()); | 429 EXPECT_TRUE(buffer->isStreamErrored()); |
454 | 430 |
455 EXPECT_FALSE(buffer->isStreamLocked()); | 431 EXPECT_FALSE(buffer->isStreamLocked()); |
456 EXPECT_FALSE(buffer->isStreamDisturbed()); | 432 EXPECT_FALSE(buffer->isStreamDisturbed()); |
457 EXPECT_FALSE(buffer->hasPendingActivity()); | 433 EXPECT_FALSE(buffer->hasPendingActivity()); |
458 | 434 |
459 checkpoint.Call(1); | 435 checkpoint.Call(1); |
460 buffer->startLoading(FetchDataLoader::createLoaderAsString(), client); | 436 buffer->startLoading(FetchDataLoader::createLoaderAsString(), client); |
461 checkpoint.Call(2); | 437 checkpoint.Call(2); |
462 | 438 |
463 EXPECT_TRUE(buffer->isStreamLocked()); | 439 EXPECT_TRUE(buffer->isStreamLocked()); |
464 EXPECT_TRUE(buffer->isStreamDisturbed()); | 440 EXPECT_TRUE(buffer->isStreamDisturbed()); |
465 EXPECT_FALSE(buffer->hasPendingActivity()); | 441 EXPECT_FALSE(buffer->hasPendingActivity()); |
466 } | 442 } |
467 | 443 |
468 TEST_F(BodyStreamBufferTest, LoaderShouldBeKeptAliveByBodyStreamBuffer) { | 444 TEST_F(BodyStreamBufferTest, LoaderShouldBeKeptAliveByBodyStreamBuffer) { |
469 V8TestingScope scope; | 445 V8TestingScope scope; |
470 Checkpoint checkpoint; | 446 Checkpoint checkpoint; |
471 MockFetchDataLoaderClient* client = MockFetchDataLoaderClient::create(); | 447 MockFetchDataLoaderClient* client = MockFetchDataLoaderClient::create(); |
472 | 448 |
473 InSequence s; | 449 InSequence s; |
474 EXPECT_CALL(checkpoint, Call(1)); | 450 EXPECT_CALL(checkpoint, Call(1)); |
475 EXPECT_CALL(*client, didFetchDataLoadedString(String("hello"))); | 451 EXPECT_CALL(*client, didFetchDataLoadedString(String("hello"))); |
476 EXPECT_CALL(checkpoint, Call(2)); | 452 EXPECT_CALL(checkpoint, Call(2)); |
477 | 453 |
478 std::unique_ptr<ReplayingHandle> handle = ReplayingHandle::create(); | 454 ReplayingBytesConsumer* src = new ReplayingBytesConsumer(&scope.document()); |
479 handle->add(Command(Command::Wait)); | 455 src->add(Command(Command::Wait)); |
480 handle->add(Command(Command::Data, "hello")); | 456 src->add(Command(Command::Data, "hello")); |
481 handle->add(Command(Command::Done)); | 457 src->add(Command(Command::Done)); |
482 Persistent<BodyStreamBuffer> buffer = new BodyStreamBuffer( | 458 Persistent<BodyStreamBuffer> buffer = |
483 scope.getScriptState(), | 459 new BodyStreamBuffer(scope.getScriptState(), src); |
484 createFetchDataConsumerHandleFromWebHandle(std::move(handle))); | |
485 buffer->startLoading(FetchDataLoader::createLoaderAsString(), client); | 460 buffer->startLoading(FetchDataLoader::createLoaderAsString(), client); |
486 | 461 |
487 ThreadState::current()->collectAllGarbage(); | 462 ThreadState::current()->collectAllGarbage(); |
488 checkpoint.Call(1); | 463 checkpoint.Call(1); |
489 testing::runPendingTasks(); | 464 testing::runPendingTasks(); |
490 checkpoint.Call(2); | 465 checkpoint.Call(2); |
491 } | 466 } |
492 | 467 |
493 TEST_F(BodyStreamBufferTest, SourceShouldBeCanceledWhenCanceled) { | 468 TEST_F(BodyStreamBufferTest, SourceShouldBeCanceledWhenCanceled) { |
494 V8TestingScope scope; | 469 V8TestingScope scope; |
495 BytesConsumerTestUtil::ReplayingBytesConsumer* consumer = | 470 ReplayingBytesConsumer* consumer = |
496 new BytesConsumerTestUtil::ReplayingBytesConsumer( | 471 new BytesConsumerTestUtil::ReplayingBytesConsumer( |
497 scope.getExecutionContext()); | 472 scope.getExecutionContext()); |
498 | 473 |
499 BodyStreamBuffer* buffer = | 474 BodyStreamBuffer* buffer = |
500 new BodyStreamBuffer(scope.getScriptState(), consumer); | 475 new BodyStreamBuffer(scope.getScriptState(), consumer); |
501 ScriptValue reason(scope.getScriptState(), | 476 ScriptValue reason(scope.getScriptState(), |
502 v8String(scope.getScriptState()->isolate(), "reason")); | 477 v8String(scope.getScriptState()->isolate(), "reason")); |
503 EXPECT_FALSE(consumer->isCancelled()); | 478 EXPECT_FALSE(consumer->isCancelled()); |
504 buffer->cancel(scope.getScriptState(), reason); | 479 buffer->cancel(scope.getScriptState(), reason); |
505 EXPECT_TRUE(consumer->isCancelled()); | 480 EXPECT_TRUE(consumer->isCancelled()); |
506 } | 481 } |
507 | 482 |
508 } // namespace | 483 } // namespace |
509 | 484 |
510 } // namespace blink | 485 } // namespace blink |
OLD | NEW |