Chromium Code Reviews| OLD | NEW |
|---|---|
| (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/ExceptionState.h" | |
| 9 #include "bindings/core/v8/ScriptFunction.h" | |
| 10 #include "bindings/core/v8/ScriptState.h" | |
| 11 #include "bindings/core/v8/V8Binding.h" | |
| 12 #include "bindings/core/v8/V8IteratorResultValue.h" | |
| 13 #include "core/testing/DummyPageHolder.h" | |
| 14 #include "platform/heap/Handle.h" | |
| 15 #include "testing/gtest/include/gtest/gtest.h" | |
| 16 #include <v8.h> | |
| 17 | |
| 18 namespace blink { | |
| 19 | |
| 20 namespace { | |
| 21 | |
| 22 class NotReached : public ScriptFunction { | |
| 23 public: | |
| 24 static v8::Local<v8::Function> createFunction(ScriptState* scriptState) | |
| 25 { | |
| 26 NotReached* self = new NotReached(scriptState); | |
| 27 return self->bindToV8Function(); | |
| 28 } | |
| 29 | |
| 30 private: | |
| 31 explicit NotReached(ScriptState* scriptState) | |
| 32 : ScriptFunction(scriptState) | |
| 33 { | |
| 34 } | |
| 35 | |
| 36 ScriptValue call(ScriptValue) override; | |
| 37 }; | |
| 38 | |
| 39 ScriptValue NotReached::call(ScriptValue) | |
| 40 { | |
| 41 EXPECT_TRUE(false) << "'Unreachable' code was reached"; | |
| 42 return ScriptValue(); | |
| 43 } | |
| 44 | |
| 45 class Iteration final : public GarbageCollectedFinalized<Iteration> { | |
| 46 public: | |
| 47 Iteration() | |
| 48 : m_isSet(false) | |
| 49 , m_isDone(false) | |
| 50 , m_isValid(true) {} | |
| 51 | |
| 52 void set(ScriptValue v) | |
| 53 { | |
| 54 ASSERT(!v.isEmpty()); | |
| 55 m_isSet = true; | |
| 56 v8::Local<v8::Value> value; | |
| 57 if (!v8IteratorUnpack(v.scriptState(), v.v8Value(), &m_isDone).ToLocal(& value)) { | |
| 58 m_isValid = false; | |
| 59 return; | |
| 60 } | |
| 61 m_value = toCoreString(value->ToString()); | |
| 62 } | |
| 63 | |
| 64 bool isSet() const { return m_isSet; } | |
| 65 bool isDone() const { return m_isDone; } | |
| 66 bool isValid() const { return m_isValid; } | |
| 67 const String& value() const { return m_value; } | |
| 68 | |
| 69 DEFINE_INLINE_TRACE() {} | |
| 70 | |
| 71 private: | |
| 72 bool m_isSet; | |
| 73 bool m_isDone; | |
| 74 bool m_isValid; | |
| 75 String m_value; | |
| 76 }; | |
| 77 | |
| 78 class Function : public ScriptFunction { | |
| 79 public: | |
| 80 static v8::Local<v8::Function> createFunction(ScriptState* scriptState, Iter ation* iteration) | |
| 81 { | |
| 82 Function* self = new Function(scriptState, iteration); | |
| 83 return self->bindToV8Function(); | |
| 84 } | |
| 85 | |
| 86 DEFINE_INLINE_VIRTUAL_TRACE() | |
| 87 { | |
| 88 visitor->trace(m_iteration); | |
| 89 ScriptFunction::trace(visitor); | |
| 90 } | |
| 91 | |
| 92 private: | |
| 93 Function(ScriptState* scriptState, Iteration* iteration) | |
| 94 : ScriptFunction(scriptState) | |
| 95 , m_iteration(iteration) | |
| 96 { | |
| 97 } | |
| 98 | |
| 99 ScriptValue call(ScriptValue value) override | |
| 100 { | |
| 101 m_iteration->set(value); | |
| 102 return value; | |
| 103 } | |
| 104 | |
| 105 Member<Iteration> m_iteration; | |
| 106 }; | |
| 107 | |
| 108 class ReadableStreamOperationsTest : public ::testing::Test { | |
| 109 public: | |
| 110 ReadableStreamOperationsTest() | |
| 111 : m_pageHolder(DummyPageHolder::create()) | |
| 112 { | |
| 113 } | |
| 114 | |
| 115 ~ReadableStreamOperationsTest() override | |
| 116 { | |
| 117 ScriptState::Scope scope(scriptState()); | |
| 118 // Execute all pending microtasks | |
| 119 isolate()->RunMicrotasks(); | |
| 120 } | |
| 121 | |
| 122 OwnPtr<DummyPageHolder> m_pageHolder; | |
| 123 ScriptState* scriptState() const { return ScriptState::forMainWorld(&m_pageH older->frame()); } | |
| 124 v8::Isolate* isolate() const { return scriptState()->isolate(); } | |
| 125 | |
| 126 v8::MaybeLocal<v8::Value> eval(const char* s) | |
| 127 { | |
| 128 v8::MaybeLocal<v8::String> source = v8::String::NewFromUtf8(isolate(), s , v8::String::kNormalString); | |
|
bashi
2015/12/04 06:48:53
nit: It would be nice to combine empty check and v
yhirano
2015/12/04 06:51:21
I did essentially the same in PS9. Is it OK?
| |
| 129 if (source.IsEmpty()) { | |
| 130 ADD_FAILURE(); | |
| 131 return v8::MaybeLocal<v8::Value>(); | |
| 132 } | |
| 133 v8::MaybeLocal<v8::Script> script = v8::Script::Compile(scriptState()->c ontext(), source.ToLocalChecked()); | |
| 134 if (script.IsEmpty()) { | |
| 135 ADD_FAILURE() << "Compilation fails"; | |
| 136 return v8::MaybeLocal<v8::Value>(); | |
| 137 } | |
| 138 return script.ToLocalChecked()->Run(scriptState()->context()); | |
| 139 } | |
| 140 v8::MaybeLocal<v8::Value> evalNoThrow(const char* s) | |
| 141 { | |
| 142 v8::TryCatch block(isolate()); | |
| 143 v8::MaybeLocal<v8::Value> r = eval(s); | |
| 144 if (block.HasCaught()) | |
| 145 ADD_FAILURE() << toCoreString(block.Exception()->ToString(isolate()) ).utf8().data(); | |
| 146 return r; | |
| 147 } | |
| 148 }; | |
| 149 | |
| 150 TEST_F(ReadableStreamOperationsTest, IsReadableStream) | |
| 151 { | |
| 152 ScriptState::Scope scope(scriptState()); | |
| 153 | |
| 154 { | |
| 155 v8::TryCatch block(isolate()); | |
| 156 EXPECT_FALSE(ReadableStreamOperations::isReadableStream(scriptState(), v 8::Undefined(isolate()))); | |
| 157 EXPECT_FALSE(block.HasCaught()); | |
| 158 } | |
| 159 { | |
| 160 v8::TryCatch block(isolate()); | |
| 161 EXPECT_FALSE(ReadableStreamOperations::isReadableStream(scriptState(), v 8::Null(isolate()))); | |
| 162 EXPECT_FALSE(block.HasCaught()); | |
| 163 } | |
| 164 { | |
| 165 v8::TryCatch block(isolate()); | |
| 166 EXPECT_FALSE(ReadableStreamOperations::isReadableStream(scriptState(), v 8::Object::New(isolate()))); | |
| 167 EXPECT_FALSE(block.HasCaught()); | |
| 168 } | |
| 169 { | |
| 170 v8::Local<v8::Value> stream; | |
| 171 ASSERT_TRUE(evalNoThrow("new ReadableStream()").ToLocal(&stream)); | |
| 172 | |
| 173 v8::TryCatch block(isolate()); | |
| 174 EXPECT_TRUE(ReadableStreamOperations::isReadableStream(scriptState(), st ream)); | |
| 175 EXPECT_FALSE(block.HasCaught()); | |
| 176 } | |
| 177 } | |
| 178 | |
| 179 TEST_F(ReadableStreamOperationsTest, IsReadableStreamReaderInvalid) | |
| 180 { | |
| 181 ScriptState::Scope scope(scriptState()); | |
| 182 | |
| 183 { | |
| 184 v8::TryCatch block(isolate()); | |
| 185 EXPECT_FALSE(ReadableStreamOperations::isReadableStreamReader(scriptStat e(), v8::Undefined(isolate()))); | |
| 186 EXPECT_FALSE(block.HasCaught()); | |
| 187 } | |
| 188 { | |
| 189 v8::TryCatch block(isolate()); | |
| 190 EXPECT_FALSE(ReadableStreamOperations::isReadableStreamReader(scriptStat e(), v8::Null(isolate()))); | |
| 191 EXPECT_FALSE(block.HasCaught()); | |
| 192 } | |
| 193 { | |
| 194 v8::TryCatch block(isolate()); | |
| 195 EXPECT_FALSE(ReadableStreamOperations::isReadableStreamReader(scriptStat e(), v8::Object::New(isolate()))); | |
| 196 EXPECT_FALSE(block.HasCaught()); | |
| 197 } | |
| 198 { | |
| 199 v8::Local<v8::Value> stream; | |
| 200 ASSERT_TRUE(evalNoThrow("new ReadableStream()").ToLocal(&stream)); | |
| 201 | |
| 202 v8::TryCatch block(isolate()); | |
| 203 EXPECT_FALSE(ReadableStreamOperations::isReadableStreamReader(scriptStat e(), stream)); | |
| 204 EXPECT_FALSE(block.HasCaught()); | |
| 205 } | |
| 206 } | |
| 207 | |
| 208 TEST_F(ReadableStreamOperationsTest, GetReader) | |
| 209 { | |
| 210 ScriptState::Scope scope(scriptState()); | |
| 211 v8::Local<v8::Value> stream; | |
| 212 ASSERT_TRUE(evalNoThrow("new ReadableStream()").ToLocal(&stream)); | |
| 213 | |
| 214 { | |
| 215 v8::TryCatch block(isolate()); | |
| 216 EXPECT_FALSE(ReadableStreamOperations::isLocked(scriptState(), stream)); | |
| 217 ScriptValue reader; | |
| 218 { | |
| 219 TrackExceptionState es; | |
| 220 reader = ReadableStreamOperations::getReader(scriptState(), stream, es); | |
| 221 ASSERT_FALSE(es.hadException()); | |
| 222 } | |
| 223 EXPECT_TRUE(ReadableStreamOperations::isLocked(scriptState(), stream)); | |
| 224 ASSERT_FALSE(reader.isEmpty()); | |
| 225 EXPECT_FALSE(block.HasCaught()); | |
| 226 | |
| 227 EXPECT_FALSE(ReadableStreamOperations::isReadableStream(scriptState(), r eader.v8Value())); | |
| 228 EXPECT_TRUE(ReadableStreamOperations::isReadableStreamReader(scriptState (), reader.v8Value())); | |
| 229 EXPECT_FALSE(block.HasCaught()); | |
| 230 } | |
| 231 | |
| 232 { | |
| 233 // Already locked! | |
| 234 v8::TryCatch block(isolate()); | |
| 235 ScriptValue reader; | |
| 236 { | |
| 237 TrackExceptionState es; | |
| 238 reader = ReadableStreamOperations::getReader(scriptState(), stream, es); | |
| 239 ASSERT_TRUE(es.hadException()); | |
| 240 } | |
| 241 ASSERT_TRUE(reader.isEmpty()); | |
| 242 EXPECT_FALSE(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( | |
| 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 | |
| OLD | NEW |