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

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

Issue 2269953004: Implment BytesConsumer::tee (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
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
(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/BytesConsumer.h"
6
7 #include "core/testing/DummyPageHolder.h"
8 #include "modules/fetch/BytesConsumerTestUtil.h"
9 #include "platform/blob/BlobData.h"
10 #include "platform/testing/UnitTestHelpers.h"
11 #include "testing/gtest/include/gtest/gtest.h"
12 #include "wtf/RefPtr.h"
13
14 namespace blink {
15
16 namespace {
17
18 using Command = BytesConsumerTestUtil::Command;
19 using Result = BytesConsumer::Result;
20 using ReplayingBytesConsumer = BytesConsumerTestUtil::ReplayingBytesConsumer;
21
22 String toString(const Vector<char>& v)
23 {
24 return String(v.data(), v.size());
25 }
26
27 class BytesConsumerTeeTest : public ::testing::Test {
28 public:
29 BytesConsumerTeeTest()
30 : m_page(DummyPageHolder::create())
31 {
32 }
33
34 Document* document() { return &m_page->document(); }
35
36 private:
37 std::unique_ptr<DummyPageHolder> m_page;
38 };
39
40 class FakeBlobBytesConsumer : public BytesConsumer {
41 public:
42 explicit FakeBlobBytesConsumer(PassRefPtr<BlobDataHandle> handle) : m_blobHa ndle(handle) {}
43 ~FakeBlobBytesConsumer() override {}
44
45 Result beginRead(const char** buffer, size_t* available) override
46 {
47 if (m_state == PublicState::Closed)
48 return Result::Done;
49 m_blobHandle = nullptr;
50 m_state = PublicState::Errored;
51 return Result::Error;
52 }
53 Result endRead(size_t readSize) override
54 {
55 if (m_state == PublicState::Closed)
56 return Result::Error;
57 m_blobHandle = nullptr;
58 m_state = PublicState::Errored;
59 return Result::Error;
60 }
61 PassRefPtr<BlobDataHandle> drainAsBlobDataHandle(BlobSizePolicy policy)
62 {
63 if (m_state != PublicState::ReadableOrWaiting)
64 return nullptr;
65 DCHECK(m_blobHandle);
66 if (policy == BlobSizePolicy::DisallowBlobWithInvalidSize && m_blobHandl e->size() == UINT64_MAX)
67 return nullptr;
68 m_state = PublicState::Closed;
69 return m_blobHandle.release();
70 }
71
72 void setClient(Client*) override {}
73 void clearClient() override {}
74 void cancel() override {}
75 PublicState getPublicState() const override { return m_state; }
76 Error getError() const override { return Error(); }
77 String debugName() const override { return "FakeBlobBytesConsumer"; }
78
79 private:
80 PublicState m_state = PublicState::ReadableOrWaiting;
81 RefPtr<BlobDataHandle> m_blobHandle;
82 };
83
84 TEST_F(BytesConsumerTeeTest, CreateDone)
85 {
86 ReplayingBytesConsumer* src = new ReplayingBytesConsumer(document());
87 src->add(Command(Command::Done));
88 EXPECT_FALSE(src->isCancelled());
89
90 BytesConsumer* dest1 = nullptr;
91 BytesConsumer* dest2 = nullptr;
92 BytesConsumer::tee(document(), src, &dest1, &dest2);
93
94 auto result1 = (new BytesConsumerTestUtil::Reader(dest1))->run();
95 auto result2 = (new BytesConsumerTestUtil::Reader(dest2))->run();
96
97 EXPECT_EQ(Result::Done, result1.first);
98 EXPECT_TRUE(result1.second.isEmpty());
99 EXPECT_EQ(Result::Done, result2.first);
100 EXPECT_TRUE(result2.second.isEmpty());
101 EXPECT_FALSE(src->isCancelled());
102 }
103
104 TEST_F(BytesConsumerTeeTest, Read)
105 {
106 ReplayingBytesConsumer* src = new ReplayingBytesConsumer(document());
107
108 src->add(Command(Command::Wait));
109 src->add(Command(Command::Data, "hello, "));
110 src->add(Command(Command::Wait));
111 src->add(Command(Command::Data, "world"));
112 src->add(Command(Command::Wait));
113 src->add(Command(Command::Wait));
114 src->add(Command(Command::Done));
115
116 BytesConsumer* dest1 = nullptr;
117 BytesConsumer* dest2 = nullptr;
118 BytesConsumer::tee(document(), src, &dest1, &dest2);
119
120 auto result1 = (new BytesConsumerTestUtil::Reader(dest1))->run();
121 auto result2 = (new BytesConsumerTestUtil::Reader(dest2))->run();
122
123 EXPECT_EQ(Result::Done, result1.first);
124 EXPECT_EQ("hello, world", toString(result1.second));
125 EXPECT_EQ(Result::Done, result2.first);
126 EXPECT_EQ("hello, world", toString(result2.second));
127 EXPECT_FALSE(src->isCancelled());
128 }
129
130 TEST_F(BytesConsumerTeeTest, TwoPhaseRead)
131 {
132 ReplayingBytesConsumer* src = new ReplayingBytesConsumer(document());
133
134 src->add(Command(Command::Wait));
135 src->add(Command(Command::Data, "hello, "));
136 src->add(Command(Command::Wait));
137 src->add(Command(Command::Data, "world"));
138 src->add(Command(Command::Wait));
139 src->add(Command(Command::Wait));
140 src->add(Command(Command::Done));
141
142 BytesConsumer* dest1 = nullptr;
143 BytesConsumer* dest2 = nullptr;
144 BytesConsumer::tee(document(), src, &dest1, &dest2);
145
146 auto result1 = (new BytesConsumerTestUtil::TwoPhaseReader(dest1))->run();
147 auto result2 = (new BytesConsumerTestUtil::TwoPhaseReader(dest2))->run();
148
149 EXPECT_EQ(Result::Done, result1.first);
150 EXPECT_EQ("hello, world", toString(result1.second));
151 EXPECT_EQ(Result::Done, result2.first);
152 EXPECT_EQ("hello, world", toString(result2.second));
153 EXPECT_FALSE(src->isCancelled());
154 }
155
156 TEST_F(BytesConsumerTeeTest, Error)
157 {
158 ReplayingBytesConsumer* src = new ReplayingBytesConsumer(document());
159
160 src->add(Command(Command::Data, "hello, "));
161 src->add(Command(Command::Data, "world"));
162 src->add(Command(Command::Error));
163
164 BytesConsumer* dest1 = nullptr;
165 BytesConsumer* dest2 = nullptr;
166 BytesConsumer::tee(document(), src, &dest1, &dest2);
167
168 auto result1 = (new BytesConsumerTestUtil::TwoPhaseReader(dest1))->run();
169 auto result2 = (new BytesConsumerTestUtil::TwoPhaseReader(dest2))->run();
170
171 EXPECT_EQ(Result::Error, result1.first);
172 EXPECT_EQ(Result::Error, result2.first);
173 EXPECT_FALSE(src->isCancelled());
174 }
175
176 TEST_F(BytesConsumerTeeTest, Cancel)
177 {
178 ReplayingBytesConsumer* src = new ReplayingBytesConsumer(document());
179
180 src->add(Command(Command::Data, "hello, "));
181 src->add(Command(Command::Wait));
182
183 BytesConsumer* dest1 = nullptr;
184 BytesConsumer* dest2 = nullptr;
185 BytesConsumer::tee(document(), src, &dest1, &dest2);
186
187 EXPECT_FALSE(src->isCancelled());
188 dest1->cancel();
189 EXPECT_FALSE(src->isCancelled());
190 dest2->cancel();
191 EXPECT_TRUE(src->isCancelled());
192 }
193
194 TEST_F(BytesConsumerTeeTest, BlobHandle)
195 {
196 RefPtr<BlobDataHandle> blobDataHandle = BlobDataHandle::create(BlobData::cre ate(), 12345);
197 BytesConsumer* src = new FakeBlobBytesConsumer(blobDataHandle);
198
199 BytesConsumer* dest1 = nullptr;
200 BytesConsumer* dest2 = nullptr;
201 BytesConsumer::tee(document(), src, &dest1, &dest2);
202
203 RefPtr<BlobDataHandle> destBlobDataHandle1 = dest1->drainAsBlobDataHandle(By tesConsumer::BlobSizePolicy::AllowBlobWithInvalidSize);
204 RefPtr<BlobDataHandle> destBlobDataHandle2 = dest2->drainAsBlobDataHandle(By tesConsumer::BlobSizePolicy::DisallowBlobWithInvalidSize);
205 ASSERT_TRUE(destBlobDataHandle1);
206 ASSERT_TRUE(destBlobDataHandle2);
207 EXPECT_EQ(12345u, destBlobDataHandle1->size());
208 EXPECT_EQ(12345u, destBlobDataHandle2->size());
209 }
210
211 TEST_F(BytesConsumerTeeTest, BlobHandleWithInvalidSize)
212 {
213 RefPtr<BlobDataHandle> blobDataHandle = BlobDataHandle::create(BlobData::cre ate(), -1);
214 BytesConsumer* src = new FakeBlobBytesConsumer(blobDataHandle);
215
216 BytesConsumer* dest1 = nullptr;
217 BytesConsumer* dest2 = nullptr;
218 BytesConsumer::tee(document(), src, &dest1, &dest2);
219
220 RefPtr<BlobDataHandle> destBlobDataHandle1 = dest1->drainAsBlobDataHandle(By tesConsumer::BlobSizePolicy::AllowBlobWithInvalidSize);
221 RefPtr<BlobDataHandle> destBlobDataHandle2 = dest2->drainAsBlobDataHandle(By tesConsumer::BlobSizePolicy::DisallowBlobWithInvalidSize);
222 ASSERT_TRUE(destBlobDataHandle1);
223 ASSERT_FALSE(destBlobDataHandle2);
224 EXPECT_EQ(UINT64_MAX, destBlobDataHandle1->size());
225 }
226
227 } // namespace
228
229 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698