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

Side by Side Diff: third_party/WebKit/Source/bindings/core/v8/ReadableStreamOperationsTest.cpp

Issue 1492763002: Add a utility class to call stream methods implemented with v8 extras. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@v8-extra-switch
Patch Set: Created 5 years 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 2015 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 "bindings/core/v8/ReadableStreamOperations.h"
7
8 #include "bindings/core/v8/ScriptFunction.h"
9 #include "bindings/core/v8/ScriptState.h"
10 #include "bindings/core/v8/V8Binding.h"
11 #include "core/testing/DummyPageHolder.h"
12 #include "platform/heap/Handle.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14 #include <v8.h>
15
16 namespace blink {
17
18 namespace {
19
20 class NotReached : public ScriptFunction {
21 public:
22 static v8::Local<v8::Function> createFunction(ScriptState* scriptState)
23 {
24 NotReached* self = new NotReached(scriptState);
25 return self->bindToV8Function();
26 }
27
28 private:
29 explicit NotReached(ScriptState* scriptState)
30 : ScriptFunction(scriptState)
31 {
32 }
33
34 ScriptValue call(ScriptValue) override;
35 };
36
37 ScriptValue NotReached::call(ScriptValue)
38 {
39 EXPECT_TRUE(false) << "'Unreachable' code was reached";
40 return ScriptValue();
41 }
42
43 class Iteration final : public GarbageCollectedFinalized<Iteration> {
44 public:
45 Iteration()
46 : m_isSet(false)
47 , m_isDone(false)
48 , m_isValid(true) {}
49
50 void set(ScriptValue v)
51 {
52 m_isSet = true;
53 if (v.isEmpty() || !v.v8Value()->IsObject()) {
54 m_isValid = false;
55 return;
56 }
57 v8::Local<v8::Object> object = v.v8Value().As<v8::Object>();
58 v8::Local<v8::Value> done;
59 if (!object->Get(v.scriptState()->context(), v8String(v.isolate(), "done ")).ToLocal(&done)
60 || done->IsUndefined()) {
61 m_isValid = false;
62 return;
63 }
64 m_isDone = done->ToBoolean()->Value();
65
66 v8::Local<v8::Value> value;
67 if (!object->Get(v.scriptState()->context(), v8String(v.isolate(), "valu e")).ToLocal(&value)) {
68 m_isValid = false;
69 return;
70 }
71 m_value = toCoreString(value->ToString());
72 }
73
74 bool isSet() const { return m_isSet; }
75 bool isDone() const { return m_isDone; }
76 bool isValid() const { return m_isValid; }
77 const String& value() const { return m_value; }
78
79 DEFINE_INLINE_TRACE() {}
80
81 private:
82 bool m_isSet;
83 bool m_isDone;
84 bool m_isValid;
85 String m_value;
86 };
87
88 class Function : public ScriptFunction {
89 public:
90 static v8::Local<v8::Function> createFunction(ScriptState* scriptState, Iter ation* iteration)
91 {
92 Function* self = new Function(scriptState, iteration);
93 return self->bindToV8Function();
94 }
95
96 DEFINE_INLINE_VIRTUAL_TRACE()
97 {
98 visitor->trace(m_iteration);
99 ScriptFunction::trace(visitor);
100 }
101
102 private:
103 Function(ScriptState* scriptState, Iteration* iteration)
104 : ScriptFunction(scriptState)
105 , m_iteration(iteration)
106 {
107 }
108
109 ScriptValue call(ScriptValue value) override
110 {
111 m_iteration->set(value);
112 return value;
113 }
114
115 Member<Iteration> m_iteration;
116 };
117
118 class ReadableStreamOperationsTest : public ::testing::Test {
119 public:
120 ReadableStreamOperationsTest()
121 : m_pageHolder(DummyPageHolder::create())
122 {
123 }
124
125 ~ReadableStreamOperationsTest() override
126 {
127 ScriptState::Scope scope(scriptState());
128 // Execute all pending microtasks
129 isolate()->RunMicrotasks();
130 }
131
132 OwnPtr<DummyPageHolder> m_pageHolder;
133 ScriptState* scriptState() const { return ScriptState::forMainWorld(&m_pageH older->frame()); }
134 v8::Isolate* isolate() const { return scriptState()->isolate(); }
135
136 v8::MaybeLocal<v8::Value> eval(const char* s)
137 {
138 v8::MaybeLocal<v8::String> source = v8::String::NewFromUtf8(isolate(), s , v8::String::kNormalString);
139 if (source.IsEmpty()) {
140 ADD_FAILURE();
141 return v8::MaybeLocal<v8::Value>();
142 }
143 v8::MaybeLocal<v8::Script> script = v8::Script::Compile(scriptState()->c ontext(), source.ToLocalChecked());
144 if (script.IsEmpty()) {
145 ADD_FAILURE() << "Compilation fails";
146 return v8::MaybeLocal<v8::Value>();
147 }
148 return script.ToLocalChecked()->Run(scriptState()->context());
149 }
150 v8::MaybeLocal<v8::Value> evalNoThrow(const char* s)
151 {
152 v8::TryCatch block(isolate());
153 v8::MaybeLocal<v8::Value> r = eval(s);
154 if (block.HasCaught())
155 ADD_FAILURE() << toCoreString(block.Exception()->ToString(isolate()) ).utf8().data();
156 return r;
157 }
158 };
159
160 TEST_F(ReadableStreamOperationsTest, IsReadableStream)
161 {
162 ScriptState::Scope scope(scriptState());
163
164 {
165 v8::TryCatch block(isolate());
166 EXPECT_FALSE(ReadableStreamOperations::isReadableStream(scriptState(), v 8::Undefined(isolate())));
167 EXPECT_FALSE(block.HasCaught());
168 }
169 {
170 v8::TryCatch block(isolate());
171 EXPECT_FALSE(ReadableStreamOperations::isReadableStream(scriptState(), v 8::Null(isolate())));
172 EXPECT_FALSE(block.HasCaught());
173 }
174 {
175 v8::TryCatch block(isolate());
176 EXPECT_FALSE(ReadableStreamOperations::isReadableStream(scriptState(), v 8::Object::New(isolate())));
177 EXPECT_FALSE(block.HasCaught());
178 }
179 {
180 v8::Local<v8::Value> stream;
181 ASSERT_TRUE(evalNoThrow("new ReadableStream()").ToLocal(&stream));
182
183 v8::TryCatch block(isolate());
184 EXPECT_TRUE(ReadableStreamOperations::isReadableStream(scriptState(), st ream));
185 EXPECT_FALSE(block.HasCaught());
186 }
187 }
188
189 TEST_F(ReadableStreamOperationsTest, IsReadableStreamReaderInvalid)
190 {
191 ScriptState::Scope scope(scriptState());
192
193 {
194 v8::TryCatch block(isolate());
195 EXPECT_FALSE(ReadableStreamOperations::isReadableStreamReader(scriptStat e(), v8::Undefined(isolate())));
196 EXPECT_FALSE(block.HasCaught());
197 }
198 {
199 v8::TryCatch block(isolate());
200 EXPECT_FALSE(ReadableStreamOperations::isReadableStreamReader(scriptStat e(), v8::Null(isolate())));
201 EXPECT_FALSE(block.HasCaught());
202 }
203 {
204 v8::TryCatch block(isolate());
205 EXPECT_FALSE(ReadableStreamOperations::isReadableStreamReader(scriptStat e(), v8::Object::New(isolate())));
206 EXPECT_FALSE(block.HasCaught());
207 }
208 {
209 v8::Local<v8::Value> stream;
210 ASSERT_TRUE(evalNoThrow("new ReadableStream()").ToLocal(&stream));
211
212 v8::TryCatch block(isolate());
213 EXPECT_FALSE(ReadableStreamOperations::isReadableStreamReader(scriptStat e(), stream));
214 EXPECT_FALSE(block.HasCaught());
215 }
216 }
217
218 TEST_F(ReadableStreamOperationsTest, GetReader)
219 {
220 ScriptState::Scope scope(scriptState());
221 v8::Local<v8::Value> stream;
222 ASSERT_TRUE(evalNoThrow("new ReadableStream()").ToLocal(&stream));
223
224 {
225 v8::TryCatch block(isolate());
226 EXPECT_FALSE(ReadableStreamOperations::isLocked(scriptState(), stream));
227 ScriptValue reader = ReadableStreamOperations::getReader(scriptState(), stream);
228 EXPECT_TRUE(ReadableStreamOperations::isLocked(scriptState(), stream));
229 ASSERT_FALSE(reader.isEmpty());
230 EXPECT_FALSE(block.HasCaught());
231
232 EXPECT_FALSE(ReadableStreamOperations::isReadableStream(scriptState(), r eader.v8Value()));
233 EXPECT_TRUE(ReadableStreamOperations::isReadableStreamReader(scriptState (), reader.v8Value()));
234 EXPECT_FALSE(block.HasCaught());
235 }
236
237 {
238 // Already locked!
239 v8::TryCatch block(isolate());
240 ScriptValue reader = ReadableStreamOperations::getReader(scriptState(), stream);
241 ASSERT_TRUE(reader.isEmpty());
242 EXPECT_TRUE(block.HasCaught());
243 }
244 }
245
246 TEST_F(ReadableStreamOperationsTest, IsDisturbed)
247 {
248 ScriptState::Scope scope(scriptState());
249 v8::Local<v8::Value> stream;
250 ASSERT_TRUE(evalNoThrow("stream = new ReadableStream()").ToLocal(&stream));
251
252 v8::TryCatch block(isolate());
253 EXPECT_FALSE(ReadableStreamOperations::isDisturbed(scriptState(), stream));
254
255 ASSERT_FALSE(evalNoThrow("stream.cancel()").IsEmpty());
256
257 EXPECT_TRUE(ReadableStreamOperations::isDisturbed(scriptState(), stream));
258 EXPECT_FALSE(block.HasCaught());
259 }
260
261 TEST_F(ReadableStreamOperationsTest, Read)
262 {
263 ScriptState::Scope scope(scriptState());
264 v8::Local<v8::Value> reader;
265 ASSERT_TRUE(evalNoThrow(
266 "var controller;"
267 "function start(c) { controller = c; }"
268 "new ReadableStream({start}).getReader()").ToLocal(&reader));
269 ASSERT_TRUE(ReadableStreamOperations::isReadableStreamReader(scriptState(), reader));
270
271 Iteration* it1 = new Iteration();
272 Iteration* it2 = new Iteration();
273 v8::TryCatch block(isolate());
274 ReadableStreamOperations::read(scriptState(), reader).then(
domenic 2015/12/02 17:53:03 This is workable but kind of takes a lot of suppor
yhirano 2015/12/03 11:18:29 Unfortunately, we cannot use lambda for the purpos
275 Function::createFunction(scriptState(), it1),
276 NotReached::createFunction(scriptState()));
277 ReadableStreamOperations::read(scriptState(), reader).then(
278 Function::createFunction(scriptState(), it2),
279 NotReached::createFunction(scriptState()));
280
281 isolate()->RunMicrotasks();
282 EXPECT_FALSE(it1->isSet());
283 EXPECT_FALSE(it2->isSet());
284
285 ASSERT_FALSE(evalNoThrow("controller.enqueue('hello')").IsEmpty());
286 isolate()->RunMicrotasks();
287 EXPECT_TRUE(it1->isSet());
288 EXPECT_TRUE(it1->isValid());
289 EXPECT_FALSE(it1->isDone());
290 EXPECT_EQ("hello", it1->value());
291 EXPECT_FALSE(it2->isSet());
292
293 ASSERT_FALSE(evalNoThrow("controller.close()").IsEmpty());
294 isolate()->RunMicrotasks();
295 EXPECT_TRUE(it1->isSet());
296 EXPECT_TRUE(it1->isValid());
297 EXPECT_FALSE(it1->isDone());
298 EXPECT_EQ("hello", it1->value());
299 EXPECT_TRUE(it2->isSet());
300 EXPECT_TRUE(it2->isValid());
301 EXPECT_TRUE(it2->isDone());
302 }
303
304 } // namespace
305
306 } // namespace blink
307
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698