Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(333)

Side by Side Diff: third_party/WebKit/Source/modules/fetch/BodyStreamBufferTest.cpp

Issue 2277143002: Use BytesConsumer in BodyStreamBuffer (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@bytes-consumer-tee
Patch Set: fix Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/html/FormData.h" 8 #include "core/html/FormData.h"
9 #include "modules/fetch/BytesConsumerTestUtil.h"
9 #include "modules/fetch/DataConsumerHandleTestUtil.h" 10 #include "modules/fetch/DataConsumerHandleTestUtil.h"
10 #include "modules/fetch/FetchBlobDataConsumerHandle.h" 11 #include "modules/fetch/FetchBlobDataConsumerHandle.h"
11 #include "modules/fetch/FetchFormDataConsumerHandle.h" 12 #include "modules/fetch/FetchFormDataConsumerHandle.h"
12 #include "platform/blob/BlobData.h" 13 #include "platform/blob/BlobData.h"
13 #include "platform/blob/BlobURL.h" 14 #include "platform/blob/BlobURL.h"
14 #include "platform/network/EncodedFormData.h" 15 #include "platform/network/EncodedFormData.h"
15 #include "platform/testing/UnitTestHelpers.h" 16 #include "platform/testing/UnitTestHelpers.h"
16 #include "testing/gmock/include/gmock/gmock.h" 17 #include "testing/gmock/include/gmock/gmock.h"
17 #include "testing/gtest/include/gtest/gtest.h" 18 #include "testing/gtest/include/gtest/gtest.h"
18 #include "wtf/PtrUtil.h" 19 #include "wtf/PtrUtil.h"
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
71 }; 72 };
72 73
73 TEST_F(BodyStreamBufferTest, Tee) 74 TEST_F(BodyStreamBufferTest, Tee)
74 { 75 {
75 V8TestingScope scope; 76 V8TestingScope scope;
76 Checkpoint checkpoint; 77 Checkpoint checkpoint;
77 MockFetchDataLoaderClient* client1 = MockFetchDataLoaderClient::create(); 78 MockFetchDataLoaderClient* client1 = MockFetchDataLoaderClient::create();
78 MockFetchDataLoaderClient* client2 = MockFetchDataLoaderClient::create(); 79 MockFetchDataLoaderClient* client2 = MockFetchDataLoaderClient::create();
79 80
80 InSequence s; 81 InSequence s;
82 EXPECT_CALL(checkpoint, Call(0));
83 EXPECT_CALL(*client1, didFetchDataLoadedString(String("hello, world")));
hiroshige 2016/09/07 09:25:54 What causes this behavior change?
yhirano 2016/09/08 01:41:05 Unlike DataConsuerTee, BytesConsumerTee start enqu
81 EXPECT_CALL(checkpoint, Call(1)); 84 EXPECT_CALL(checkpoint, Call(1));
82 EXPECT_CALL(*client1, didFetchDataLoadedString(String("hello, world")));
83 EXPECT_CALL(checkpoint, Call(2)); 85 EXPECT_CALL(checkpoint, Call(2));
84 EXPECT_CALL(*client2, didFetchDataLoadedString(String("hello, world"))); 86 EXPECT_CALL(*client2, didFetchDataLoadedString(String("hello, world")));
85 EXPECT_CALL(checkpoint, Call(3)); 87 EXPECT_CALL(checkpoint, Call(3));
86 EXPECT_CALL(checkpoint, Call(4)); 88 EXPECT_CALL(checkpoint, Call(4));
87 89
88 std::unique_ptr<DataConsumerHandleTestUtil::ReplayingHandle> handle = DataCo nsumerHandleTestUtil::ReplayingHandle::create(); 90 std::unique_ptr<DataConsumerHandleTestUtil::ReplayingHandle> handle = DataCo nsumerHandleTestUtil::ReplayingHandle::create();
89 handle->add(DataConsumerHandleTestUtil::Command(DataConsumerHandleTestUtil:: Command::Data, "hello, ")); 91 handle->add(DataConsumerHandleTestUtil::Command(DataConsumerHandleTestUtil:: Command::Data, "hello, "));
90 handle->add(DataConsumerHandleTestUtil::Command(DataConsumerHandleTestUtil:: Command::Data, "world")); 92 handle->add(DataConsumerHandleTestUtil::Command(DataConsumerHandleTestUtil:: Command::Data, "world"));
91 handle->add(DataConsumerHandleTestUtil::Command(DataConsumerHandleTestUtil:: Command::Done)); 93 handle->add(DataConsumerHandleTestUtil::Command(DataConsumerHandleTestUtil:: Command::Done));
92 BodyStreamBuffer* buffer = new BodyStreamBuffer(scope.getScriptState(), crea teFetchDataConsumerHandleFromWebHandle(std::move(handle))); 94 BodyStreamBuffer* buffer = new BodyStreamBuffer(scope.getScriptState(), crea teFetchDataConsumerHandleFromWebHandle(std::move(handle)));
93 95
94 BodyStreamBuffer* new1; 96 BodyStreamBuffer* new1;
95 BodyStreamBuffer* new2; 97 BodyStreamBuffer* new2;
96 buffer->tee(&new1, &new2); 98 buffer->tee(&new1, &new2);
97 99
98 EXPECT_TRUE(buffer->isStreamLocked()); 100 EXPECT_TRUE(buffer->isStreamLocked());
99 EXPECT_TRUE(buffer->isStreamDisturbed()); 101 EXPECT_TRUE(buffer->isStreamDisturbed());
100 EXPECT_FALSE(buffer->hasPendingActivity()); 102 EXPECT_FALSE(buffer->hasPendingActivity());
101 103
104 checkpoint.Call(0);
102 new1->startLoading(FetchDataLoader::createLoaderAsString(), client1); 105 new1->startLoading(FetchDataLoader::createLoaderAsString(), client1);
103 checkpoint.Call(1); 106 checkpoint.Call(1);
104 testing::runPendingTasks(); 107 testing::runPendingTasks();
105 checkpoint.Call(2); 108 checkpoint.Call(2);
106 109
107 new2->startLoading(FetchDataLoader::createLoaderAsString(), client2); 110 new2->startLoading(FetchDataLoader::createLoaderAsString(), client2);
108 checkpoint.Call(3); 111 checkpoint.Call(3);
109 testing::runPendingTasks(); 112 testing::runPendingTasks();
110 checkpoint.Call(4); 113 checkpoint.Call(4);
111 } 114 }
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
169 V8TestingScope scope; 172 V8TestingScope scope;
170 std::unique_ptr<BlobData> data = BlobData::create(); 173 std::unique_ptr<BlobData> data = BlobData::create();
171 data->appendText("hello", false); 174 data->appendText("hello", false);
172 auto size = data->length(); 175 auto size = data->length();
173 RefPtr<BlobDataHandle> blobDataHandle = BlobDataHandle::create(std::move(dat a), size); 176 RefPtr<BlobDataHandle> blobDataHandle = BlobDataHandle::create(std::move(dat a), size);
174 BodyStreamBuffer* buffer = new BodyStreamBuffer(scope.getScriptState(), Fetc hBlobDataConsumerHandle::create(scope.getExecutionContext(), blobDataHandle, new FakeLoaderFactory)); 177 BodyStreamBuffer* buffer = new BodyStreamBuffer(scope.getScriptState(), Fetc hBlobDataConsumerHandle::create(scope.getExecutionContext(), blobDataHandle, new FakeLoaderFactory));
175 178
176 EXPECT_FALSE(buffer->isStreamLocked()); 179 EXPECT_FALSE(buffer->isStreamLocked());
177 EXPECT_FALSE(buffer->isStreamDisturbed()); 180 EXPECT_FALSE(buffer->isStreamDisturbed());
178 EXPECT_FALSE(buffer->hasPendingActivity()); 181 EXPECT_FALSE(buffer->hasPendingActivity());
179 RefPtr<BlobDataHandle> outputBlobDataHandle = buffer->drainAsBlobDataHandle( FetchDataConsumerHandle::Reader::AllowBlobWithInvalidSize); 182 RefPtr<BlobDataHandle> outputBlobDataHandle = buffer->drainAsBlobDataHandle( BytesConsumer::BlobSizePolicy::AllowBlobWithInvalidSize);
180 183
181 EXPECT_TRUE(buffer->isStreamLocked()); 184 EXPECT_TRUE(buffer->isStreamLocked());
182 EXPECT_TRUE(buffer->isStreamDisturbed()); 185 EXPECT_TRUE(buffer->isStreamDisturbed());
183 EXPECT_FALSE(buffer->hasPendingActivity()); 186 EXPECT_FALSE(buffer->hasPendingActivity());
184 EXPECT_EQ(blobDataHandle, outputBlobDataHandle); 187 EXPECT_EQ(blobDataHandle, outputBlobDataHandle);
185 } 188 }
186 189
187 TEST_F(BodyStreamBufferTest, DrainAsBlobDataHandleReturnsNull) 190 TEST_F(BodyStreamBufferTest, DrainAsBlobDataHandleReturnsNull)
188 { 191 {
189 V8TestingScope scope; 192 V8TestingScope scope;
190 // This handle is not drainable. 193 // This handle is not drainable.
191 std::unique_ptr<FetchDataConsumerHandle> handle = createFetchDataConsumerHan dleFromWebHandle(createWaitingDataConsumerHandle()); 194 std::unique_ptr<FetchDataConsumerHandle> handle = createFetchDataConsumerHan dleFromWebHandle(createWaitingDataConsumerHandle());
192 BodyStreamBuffer* buffer = new BodyStreamBuffer(scope.getScriptState(), std: :move(handle)); 195 BodyStreamBuffer* buffer = new BodyStreamBuffer(scope.getScriptState(), std: :move(handle));
193 196
194 EXPECT_FALSE(buffer->isStreamLocked()); 197 EXPECT_FALSE(buffer->isStreamLocked());
195 EXPECT_FALSE(buffer->isStreamDisturbed()); 198 EXPECT_FALSE(buffer->isStreamDisturbed());
196 EXPECT_FALSE(buffer->hasPendingActivity()); 199 EXPECT_FALSE(buffer->hasPendingActivity());
197 200
198 EXPECT_FALSE(buffer->drainAsBlobDataHandle(FetchDataConsumerHandle::Reader:: AllowBlobWithInvalidSize)); 201 EXPECT_FALSE(buffer->drainAsBlobDataHandle(BytesConsumer::BlobSizePolicy::Al lowBlobWithInvalidSize));
199 202
200 EXPECT_FALSE(buffer->isStreamLocked()); 203 EXPECT_FALSE(buffer->isStreamLocked());
201 EXPECT_FALSE(buffer->isStreamDisturbed()); 204 EXPECT_FALSE(buffer->isStreamDisturbed());
202 EXPECT_FALSE(buffer->hasPendingActivity()); 205 EXPECT_FALSE(buffer->hasPendingActivity());
203 } 206 }
204 207
205 TEST_F(BodyStreamBufferTest, DrainAsBlobFromBufferMadeFromBufferMadeFromStream) 208 TEST_F(BodyStreamBufferTest, DrainAsBlobFromBufferMadeFromBufferMadeFromStream)
206 { 209 {
207 V8TestingScope scope; 210 V8TestingScope scope;
208 ScriptValue stream = evalWithPrintingError(scope.getScriptState(), "new Read ableStream()"); 211 ScriptValue stream = evalWithPrintingError(scope.getScriptState(), "new Read ableStream()");
209 BodyStreamBuffer* buffer = new BodyStreamBuffer(scope.getScriptState(), stre am); 212 BodyStreamBuffer* buffer = new BodyStreamBuffer(scope.getScriptState(), stre am);
210 213
211 EXPECT_FALSE(buffer->hasPendingActivity()); 214 EXPECT_FALSE(buffer->hasPendingActivity());
212 EXPECT_FALSE(buffer->isStreamLocked()); 215 EXPECT_FALSE(buffer->isStreamLocked());
213 EXPECT_FALSE(buffer->isStreamDisturbed()); 216 EXPECT_FALSE(buffer->isStreamDisturbed());
214 EXPECT_TRUE(buffer->isStreamReadable()); 217 EXPECT_TRUE(buffer->isStreamReadable());
215 218
216 EXPECT_FALSE(buffer->drainAsBlobDataHandle(FetchDataConsumerHandle::Reader:: AllowBlobWithInvalidSize)); 219 EXPECT_FALSE(buffer->drainAsBlobDataHandle(BytesConsumer::BlobSizePolicy::Al lowBlobWithInvalidSize));
217 220
218 EXPECT_FALSE(buffer->hasPendingActivity()); 221 EXPECT_FALSE(buffer->hasPendingActivity());
219 EXPECT_FALSE(buffer->isStreamLocked()); 222 EXPECT_FALSE(buffer->isStreamLocked());
220 EXPECT_FALSE(buffer->isStreamDisturbed()); 223 EXPECT_FALSE(buffer->isStreamDisturbed());
221 EXPECT_TRUE(buffer->isStreamReadable()); 224 EXPECT_TRUE(buffer->isStreamReadable());
222 } 225 }
223 226
224 TEST_F(BodyStreamBufferTest, DrainAsFormData) 227 TEST_F(BodyStreamBufferTest, DrainAsFormData)
225 { 228 {
226 V8TestingScope scope; 229 V8TestingScope scope;
(...skipping 227 matching lines...) Expand 10 before | Expand all | Expand 10 after
454 handle->add(Command(Command::Done)); 457 handle->add(Command(Command::Done));
455 Persistent<BodyStreamBuffer> buffer = new BodyStreamBuffer(scope.getScriptSt ate(), createFetchDataConsumerHandleFromWebHandle(std::move(handle))); 458 Persistent<BodyStreamBuffer> buffer = new BodyStreamBuffer(scope.getScriptSt ate(), createFetchDataConsumerHandleFromWebHandle(std::move(handle)));
456 buffer->startLoading(FetchDataLoader::createLoaderAsString(), client); 459 buffer->startLoading(FetchDataLoader::createLoaderAsString(), client);
457 460
458 ThreadHeap::collectAllGarbage(); 461 ThreadHeap::collectAllGarbage();
459 checkpoint.Call(1); 462 checkpoint.Call(1);
460 testing::runPendingTasks(); 463 testing::runPendingTasks();
461 checkpoint.Call(2); 464 checkpoint.Call(2);
462 } 465 }
463 466
464 // TODO(hiroshige): Merge this class into MockFetchDataConsumerHandle. 467 TEST_F(BodyStreamBufferTest, SourceShouldBeCanceledWhenCanceled)
465 class MockFetchDataConsumerHandleWithMockDestructor : public DataConsumerHandleT estUtil::MockFetchDataConsumerHandle {
466 public:
467 static std::unique_ptr<::testing::StrictMock<MockFetchDataConsumerHandleWith MockDestructor>> create() { return wrapUnique(new ::testing::StrictMock<MockFetc hDataConsumerHandleWithMockDestructor>); }
468
469 ~MockFetchDataConsumerHandleWithMockDestructor() override
470 {
471 destruct();
472 }
473
474 MOCK_METHOD0(destruct, void());
475 };
476
477 TEST_F(BodyStreamBufferTest, SourceHandleAndReaderShouldBeDestructedWhenCanceled )
478 { 468 {
479 V8TestingScope scope; 469 V8TestingScope scope;
480 using MockHandle = MockFetchDataConsumerHandleWithMockDestructor; 470 BytesConsumerTestUtil::ReplayingBytesConsumer* consumer = new BytesConsumerT estUtil::ReplayingBytesConsumer(scope.getExecutionContext());
481 using MockReader = DataConsumerHandleTestUtil::MockFetchDataConsumerReader;
482 std::unique_ptr<MockHandle> handle = MockHandle::create();
483 // |reader| will be adopted by |obtainFetchDataReader|.
484 MockReader* reader = MockReader::create().release();
485 471
486 Checkpoint checkpoint; 472 BodyStreamBuffer* buffer = new BodyStreamBuffer(scope.getScriptState(), cons umer);
487 InSequence s;
488
489 EXPECT_CALL(*handle, obtainFetchDataReader(_)).WillOnce(Return(ByMove(WTF::w rapUnique(reader))));
490 EXPECT_CALL(checkpoint, Call(1));
491 EXPECT_CALL(*reader, destruct());
492 EXPECT_CALL(*handle, destruct());
493 EXPECT_CALL(checkpoint, Call(2));
494
495 BodyStreamBuffer* buffer = new BodyStreamBuffer(scope.getScriptState(), std: :move(handle));
496 checkpoint.Call(1);
497 ScriptValue reason(scope.getScriptState(), v8String(scope.getScriptState()-> isolate(), "reason")); 473 ScriptValue reason(scope.getScriptState(), v8String(scope.getScriptState()-> isolate(), "reason"));
474 EXPECT_FALSE(consumer->isCancelled());
498 buffer->cancel(scope.getScriptState(), reason); 475 buffer->cancel(scope.getScriptState(), reason);
499 checkpoint.Call(2); 476 EXPECT_TRUE(consumer->isCancelled());
500 } 477 }
501 478
502 } // namespace 479 } // namespace
503 480
504 } // namespace blink 481 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698