OLD | NEW |
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 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 | 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 "bindings/core/v8/ReadableStreamOperations.h" | 5 #include "bindings/core/v8/ReadableStreamOperations.h" |
6 | 6 |
7 #include "bindings/core/v8/ExceptionState.h" | 7 #include "bindings/core/v8/ExceptionState.h" |
8 #include "bindings/core/v8/ScriptFunction.h" | 8 #include "bindings/core/v8/ScriptFunction.h" |
9 #include "bindings/core/v8/ScriptState.h" | 9 #include "bindings/core/v8/ScriptState.h" |
| 10 #include "bindings/core/v8/ScriptValue.h" |
10 #include "bindings/core/v8/V8Binding.h" | 11 #include "bindings/core/v8/V8Binding.h" |
11 #include "bindings/core/v8/V8BindingForTesting.h" | 12 #include "bindings/core/v8/V8BindingForTesting.h" |
12 #include "bindings/core/v8/V8BindingMacros.h" | 13 #include "bindings/core/v8/V8BindingMacros.h" |
13 #include "bindings/core/v8/V8IteratorResultValue.h" | 14 #include "bindings/core/v8/V8IteratorResultValue.h" |
14 #include "bindings/core/v8/V8ThrowException.h" | 15 #include "bindings/core/v8/V8ThrowException.h" |
15 #include "core/dom/Document.h" | 16 #include "core/dom/Document.h" |
16 #include "platform/heap/Handle.h" | 17 #include "platform/heap/Handle.h" |
17 #include "testing/gtest/include/gtest/gtest.h" | 18 #include "testing/gtest/include/gtest/gtest.h" |
18 #include <v8.h> | 19 #include <v8.h> |
19 | 20 |
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
121 ~ReadableStreamOperationsTest() override | 122 ~ReadableStreamOperationsTest() override |
122 { | 123 { |
123 // Execute all pending microtasks | 124 // Execute all pending microtasks |
124 isolate()->RunMicrotasks(); | 125 isolate()->RunMicrotasks(); |
125 EXPECT_FALSE(m_block.HasCaught()); | 126 EXPECT_FALSE(m_block.HasCaught()); |
126 } | 127 } |
127 | 128 |
128 ScriptState* scriptState() const { return m_scope.scriptState(); } | 129 ScriptState* scriptState() const { return m_scope.scriptState(); } |
129 v8::Isolate* isolate() const { return scriptState()->isolate(); } | 130 v8::Isolate* isolate() const { return scriptState()->isolate(); } |
130 | 131 |
131 v8::MaybeLocal<v8::Value> eval(const char* s) | 132 ScriptValue eval(const char* s) |
132 { | 133 { |
133 v8::Local<v8::String> source; | 134 v8::Local<v8::String> source; |
134 v8::Local<v8::Script> script; | 135 v8::Local<v8::Script> script; |
135 if (!v8Call(v8::String::NewFromUtf8(isolate(), s, v8::NewStringType::kNo
rmal), source)) { | 136 if (!v8Call(v8::String::NewFromUtf8(isolate(), s, v8::NewStringType::kNo
rmal), source)) { |
136 ADD_FAILURE(); | 137 ADD_FAILURE(); |
137 return v8::MaybeLocal<v8::Value>(); | 138 return ScriptValue(); |
138 } | 139 } |
139 if (!v8Call(v8::Script::Compile(scriptState()->context(), source), scrip
t)) { | 140 if (!v8Call(v8::Script::Compile(scriptState()->context(), source), scrip
t)) { |
140 ADD_FAILURE() << "Compilation fails"; | 141 ADD_FAILURE() << "Compilation fails"; |
141 return v8::MaybeLocal<v8::Value>(); | 142 return ScriptValue(); |
142 } | 143 } |
143 return script->Run(scriptState()->context()); | 144 return ScriptValue(scriptState(), script->Run(scriptState()->context()))
; |
144 } | 145 } |
145 v8::MaybeLocal<v8::Value> evalWithPrintingError(const char* s) | 146 ScriptValue evalWithPrintingError(const char* s) |
146 { | 147 { |
147 v8::TryCatch block(isolate()); | 148 v8::TryCatch block(isolate()); |
148 v8::MaybeLocal<v8::Value> r = eval(s); | 149 ScriptValue r = eval(s); |
149 if (block.HasCaught()) { | 150 if (block.HasCaught()) { |
150 ADD_FAILURE() << toCoreString(block.Exception()->ToString(isolate())
).utf8().data(); | 151 ADD_FAILURE() << toCoreString(block.Exception()->ToString(isolate())
).utf8().data(); |
151 block.ReThrow(); | 152 block.ReThrow(); |
152 } | 153 } |
153 return r; | 154 return r; |
154 } | 155 } |
155 | 156 |
156 V8TestingScope m_scope; | 157 V8TestingScope m_scope; |
157 v8::TryCatch m_block; | 158 v8::TryCatch m_block; |
158 RefPtrWillBePersistent<Document> m_document; | 159 RefPtrWillBePersistent<Document> m_document; |
159 }; | 160 }; |
160 | 161 |
161 TEST_F(ReadableStreamOperationsTest, IsReadableStream) | 162 TEST_F(ReadableStreamOperationsTest, IsReadableStream) |
162 { | 163 { |
163 EXPECT_FALSE(ReadableStreamOperations::isReadableStream(scriptState(), v8::U
ndefined(isolate()))); | 164 EXPECT_FALSE(ReadableStreamOperations::isReadableStream(scriptState(), Scrip
tValue(scriptState(), v8::Undefined(isolate())))); |
164 EXPECT_FALSE(ReadableStreamOperations::isReadableStream(scriptState(), v8::N
ull(isolate()))); | 165 EXPECT_FALSE(ReadableStreamOperations::isReadableStream(scriptState(), Scrip
tValue::createNull(scriptState()))); |
165 EXPECT_FALSE(ReadableStreamOperations::isReadableStream(scriptState(), v8::O
bject::New(isolate()))); | 166 EXPECT_FALSE(ReadableStreamOperations::isReadableStream(scriptState(), Scrip
tValue(scriptState(), v8::Object::New(isolate())))); |
166 v8::Local<v8::Value> stream; | 167 ScriptValue stream = evalWithPrintingError("new ReadableStream()"); |
167 ASSERT_TRUE(v8Call(evalWithPrintingError("new ReadableStream()"), stream)); | 168 EXPECT_FALSE(stream.isEmpty()); |
168 EXPECT_TRUE(ReadableStreamOperations::isReadableStream(scriptState(), stream
)); | 169 EXPECT_TRUE(ReadableStreamOperations::isReadableStream(scriptState(), stream
)); |
169 } | 170 } |
170 | 171 |
171 TEST_F(ReadableStreamOperationsTest, IsReadableStreamReaderInvalid) | 172 TEST_F(ReadableStreamOperationsTest, IsReadableStreamReaderInvalid) |
172 { | 173 { |
173 EXPECT_FALSE(ReadableStreamOperations::isReadableStreamReader(scriptState(),
v8::Undefined(isolate()))); | 174 EXPECT_FALSE(ReadableStreamOperations::isReadableStreamReader(scriptState(),
ScriptValue(scriptState(), v8::Undefined(isolate())))); |
174 EXPECT_FALSE(ReadableStreamOperations::isReadableStreamReader(scriptState(),
v8::Null(isolate()))); | 175 EXPECT_FALSE(ReadableStreamOperations::isReadableStreamReader(scriptState(),
ScriptValue::createNull(scriptState()))); |
175 EXPECT_FALSE(ReadableStreamOperations::isReadableStreamReader(scriptState(),
v8::Object::New(isolate()))); | 176 EXPECT_FALSE(ReadableStreamOperations::isReadableStreamReader(scriptState(),
ScriptValue(scriptState(), v8::Object::New(isolate())))); |
176 v8::Local<v8::Value> stream; | 177 ScriptValue stream = evalWithPrintingError("new ReadableStream()"); |
177 ASSERT_TRUE(v8Call(evalWithPrintingError("new ReadableStream()"), stream)); | 178 EXPECT_FALSE(stream.isEmpty()); |
178 | 179 |
179 EXPECT_FALSE(ReadableStreamOperations::isReadableStreamReader(scriptState(),
stream)); | 180 EXPECT_FALSE(ReadableStreamOperations::isReadableStreamReader(scriptState(),
stream)); |
180 } | 181 } |
181 | 182 |
182 TEST_F(ReadableStreamOperationsTest, GetReader) | 183 TEST_F(ReadableStreamOperationsTest, GetReader) |
183 { | 184 { |
184 v8::Local<v8::Value> stream; | 185 ScriptValue stream = evalWithPrintingError("new ReadableStream()"); |
185 ASSERT_TRUE(v8Call(evalWithPrintingError("new ReadableStream()"), stream)); | 186 EXPECT_FALSE(stream.isEmpty()); |
186 | 187 |
187 EXPECT_FALSE(ReadableStreamOperations::isLocked(scriptState(), stream)); | 188 EXPECT_FALSE(ReadableStreamOperations::isLocked(scriptState(), stream)); |
188 ScriptValue reader; | 189 ScriptValue reader; |
189 { | 190 { |
190 TrackExceptionState es; | 191 TrackExceptionState es; |
191 reader = ReadableStreamOperations::getReader(scriptState(), stream, es); | 192 reader = ReadableStreamOperations::getReader(scriptState(), stream, es); |
192 ASSERT_FALSE(es.hadException()); | 193 ASSERT_FALSE(es.hadException()); |
193 } | 194 } |
194 EXPECT_TRUE(ReadableStreamOperations::isLocked(scriptState(), stream)); | 195 EXPECT_TRUE(ReadableStreamOperations::isLocked(scriptState(), stream)); |
195 ASSERT_FALSE(reader.isEmpty()); | 196 ASSERT_FALSE(reader.isEmpty()); |
196 | 197 |
197 EXPECT_FALSE(ReadableStreamOperations::isReadableStream(scriptState(), reade
r.v8Value())); | 198 EXPECT_FALSE(ReadableStreamOperations::isReadableStream(scriptState(), reade
r)); |
198 EXPECT_TRUE(ReadableStreamOperations::isReadableStreamReader(scriptState(),
reader.v8Value())); | 199 EXPECT_TRUE(ReadableStreamOperations::isReadableStreamReader(scriptState(),
reader)); |
199 | 200 |
200 // Already locked! | 201 // Already locked! |
201 { | 202 { |
202 TrackExceptionState es; | 203 TrackExceptionState es; |
203 reader = ReadableStreamOperations::getReader(scriptState(), stream, es); | 204 reader = ReadableStreamOperations::getReader(scriptState(), stream, es); |
204 ASSERT_TRUE(es.hadException()); | 205 ASSERT_TRUE(es.hadException()); |
205 } | 206 } |
206 ASSERT_TRUE(reader.isEmpty()); | 207 ASSERT_TRUE(reader.isEmpty()); |
207 } | 208 } |
208 | 209 |
209 TEST_F(ReadableStreamOperationsTest, IsDisturbed) | 210 TEST_F(ReadableStreamOperationsTest, IsDisturbed) |
210 { | 211 { |
211 v8::Local<v8::Value> stream; | 212 ScriptValue stream = evalWithPrintingError("stream = new ReadableStream()"); |
212 ASSERT_TRUE(v8Call(evalWithPrintingError("stream = new ReadableStream()"), s
tream)); | 213 EXPECT_FALSE(stream.isEmpty()); |
213 | 214 |
214 EXPECT_FALSE(ReadableStreamOperations::isDisturbed(scriptState(), stream)); | 215 EXPECT_FALSE(ReadableStreamOperations::isDisturbed(scriptState(), stream)); |
215 | 216 |
216 ASSERT_FALSE(evalWithPrintingError("stream.cancel()").IsEmpty()); | 217 ASSERT_FALSE(evalWithPrintingError("stream.cancel()").isEmpty()); |
217 | 218 |
218 EXPECT_TRUE(ReadableStreamOperations::isDisturbed(scriptState(), stream)); | 219 EXPECT_TRUE(ReadableStreamOperations::isDisturbed(scriptState(), stream)); |
219 } | 220 } |
220 | 221 |
221 TEST_F(ReadableStreamOperationsTest, Read) | 222 TEST_F(ReadableStreamOperationsTest, Read) |
222 { | 223 { |
223 v8::Local<v8::Value> reader; | 224 ScriptValue reader = evalWithPrintingError( |
224 ASSERT_TRUE(v8Call(evalWithPrintingError( | |
225 "var controller;" | 225 "var controller;" |
226 "function start(c) { controller = c; }" | 226 "function start(c) { controller = c; }" |
227 "new ReadableStream({start}).getReader()"), reader)); | 227 "new ReadableStream({start}).getReader()"); |
| 228 EXPECT_FALSE(reader.isEmpty()); |
228 ASSERT_TRUE(ReadableStreamOperations::isReadableStreamReader(scriptState(),
reader)); | 229 ASSERT_TRUE(ReadableStreamOperations::isReadableStreamReader(scriptState(),
reader)); |
229 | 230 |
230 Iteration* it1 = new Iteration(); | 231 Iteration* it1 = new Iteration(); |
231 Iteration* it2 = new Iteration(); | 232 Iteration* it2 = new Iteration(); |
232 ReadableStreamOperations::read(scriptState(), reader).then( | 233 ReadableStreamOperations::read(scriptState(), reader).then( |
233 Function::createFunction(scriptState(), it1), | 234 Function::createFunction(scriptState(), it1), |
234 NotReached::createFunction(scriptState())); | 235 NotReached::createFunction(scriptState())); |
235 ReadableStreamOperations::read(scriptState(), reader).then( | 236 ReadableStreamOperations::read(scriptState(), reader).then( |
236 Function::createFunction(scriptState(), it2), | 237 Function::createFunction(scriptState(), it2), |
237 NotReached::createFunction(scriptState())); | 238 NotReached::createFunction(scriptState())); |
238 | 239 |
239 isolate()->RunMicrotasks(); | 240 isolate()->RunMicrotasks(); |
240 EXPECT_FALSE(it1->isSet()); | 241 EXPECT_FALSE(it1->isSet()); |
241 EXPECT_FALSE(it2->isSet()); | 242 EXPECT_FALSE(it2->isSet()); |
242 | 243 |
243 ASSERT_FALSE(evalWithPrintingError("controller.enqueue('hello')").IsEmpty())
; | 244 ASSERT_FALSE(evalWithPrintingError("controller.enqueue('hello')").isEmpty())
; |
244 isolate()->RunMicrotasks(); | 245 isolate()->RunMicrotasks(); |
245 EXPECT_TRUE(it1->isSet()); | 246 EXPECT_TRUE(it1->isSet()); |
246 EXPECT_TRUE(it1->isValid()); | 247 EXPECT_TRUE(it1->isValid()); |
247 EXPECT_FALSE(it1->isDone()); | 248 EXPECT_FALSE(it1->isDone()); |
248 EXPECT_EQ("hello", it1->value()); | 249 EXPECT_EQ("hello", it1->value()); |
249 EXPECT_FALSE(it2->isSet()); | 250 EXPECT_FALSE(it2->isSet()); |
250 | 251 |
251 ASSERT_FALSE(evalWithPrintingError("controller.close()").IsEmpty()); | 252 ASSERT_FALSE(evalWithPrintingError("controller.close()").isEmpty()); |
252 isolate()->RunMicrotasks(); | 253 isolate()->RunMicrotasks(); |
253 EXPECT_TRUE(it1->isSet()); | 254 EXPECT_TRUE(it1->isSet()); |
254 EXPECT_TRUE(it1->isValid()); | 255 EXPECT_TRUE(it1->isValid()); |
255 EXPECT_FALSE(it1->isDone()); | 256 EXPECT_FALSE(it1->isDone()); |
256 EXPECT_EQ("hello", it1->value()); | 257 EXPECT_EQ("hello", it1->value()); |
257 EXPECT_TRUE(it2->isSet()); | 258 EXPECT_TRUE(it2->isSet()); |
258 EXPECT_TRUE(it2->isValid()); | 259 EXPECT_TRUE(it2->isValid()); |
259 EXPECT_TRUE(it2->isDone()); | 260 EXPECT_TRUE(it2->isDone()); |
260 } | 261 } |
261 | 262 |
262 } // namespace | 263 } // namespace |
263 | 264 |
264 } // namespace blink | 265 } // namespace blink |
265 | 266 |
OLD | NEW |