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