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/ScriptValue.h" |
11 #include "bindings/core/v8/V8Binding.h" | 11 #include "bindings/core/v8/V8Binding.h" |
12 #include "bindings/core/v8/V8BindingForTesting.h" | 12 #include "bindings/core/v8/V8BindingForTesting.h" |
13 #include "bindings/core/v8/V8BindingMacros.h" | 13 #include "bindings/core/v8/V8BindingMacros.h" |
14 #include "bindings/core/v8/V8IteratorResultValue.h" | 14 #include "bindings/core/v8/V8IteratorResultValue.h" |
15 #include "bindings/core/v8/V8ThrowException.h" | 15 #include "bindings/core/v8/V8ThrowException.h" |
16 #include "core/dom/Document.h" | 16 #include "core/dom/Document.h" |
| 17 #include "core/streams/ReadableStreamController.h" |
| 18 #include "core/streams/UnderlyingSourceBase.h" |
17 #include "platform/heap/Handle.h" | 19 #include "platform/heap/Handle.h" |
18 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
19 #include <v8.h> | 21 #include <v8.h> |
20 | 22 |
21 namespace blink { | 23 namespace blink { |
22 | 24 |
23 namespace { | 25 namespace { |
24 | 26 |
25 class NotReached : public ScriptFunction { | 27 class NotReached : public ScriptFunction { |
26 public: | 28 public: |
(...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
103 | 105 |
104 ScriptValue call(ScriptValue value) override | 106 ScriptValue call(ScriptValue value) override |
105 { | 107 { |
106 m_iteration->set(value); | 108 m_iteration->set(value); |
107 return value; | 109 return value; |
108 } | 110 } |
109 | 111 |
110 Member<Iteration> m_iteration; | 112 Member<Iteration> m_iteration; |
111 }; | 113 }; |
112 | 114 |
| 115 class TestUnderlyingSource final : public UnderlyingSourceBase { |
| 116 public: |
| 117 explicit TestUnderlyingSource(ScriptState* scriptState) |
| 118 : UnderlyingSourceBase(scriptState) |
| 119 { |
| 120 } |
| 121 |
| 122 // Just expose the controller methods for easy testing |
| 123 void enqueue(ScriptValue value) { controller()->enqueue(value); } |
| 124 void close() { controller()->close(); } |
| 125 void error(ScriptValue value) { controller()->error(value); } |
| 126 double desiredSize() { return controller()->desiredSize(); } |
| 127 }; |
| 128 |
113 class ReadableStreamOperationsTest : public ::testing::Test { | 129 class ReadableStreamOperationsTest : public ::testing::Test { |
114 public: | 130 public: |
115 ReadableStreamOperationsTest() | 131 ReadableStreamOperationsTest() |
116 : m_scope(v8::Isolate::GetCurrent()) | 132 : m_scope(v8::Isolate::GetCurrent()) |
117 , m_block(isolate()) | 133 , m_block(isolate()) |
118 , m_document(Document::create()) | 134 , m_document(Document::create()) |
119 { | 135 { |
120 scriptState()->setExecutionContext(m_document.get()); | 136 scriptState()->setExecutionContext(m_document.get()); |
121 } | 137 } |
122 ~ReadableStreamOperationsTest() override | 138 ~ReadableStreamOperationsTest() override |
(...skipping 130 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
253 isolate()->RunMicrotasks(); | 269 isolate()->RunMicrotasks(); |
254 EXPECT_TRUE(it1->isSet()); | 270 EXPECT_TRUE(it1->isSet()); |
255 EXPECT_TRUE(it1->isValid()); | 271 EXPECT_TRUE(it1->isValid()); |
256 EXPECT_FALSE(it1->isDone()); | 272 EXPECT_FALSE(it1->isDone()); |
257 EXPECT_EQ("hello", it1->value()); | 273 EXPECT_EQ("hello", it1->value()); |
258 EXPECT_TRUE(it2->isSet()); | 274 EXPECT_TRUE(it2->isSet()); |
259 EXPECT_TRUE(it2->isValid()); | 275 EXPECT_TRUE(it2->isValid()); |
260 EXPECT_TRUE(it2->isDone()); | 276 EXPECT_TRUE(it2->isDone()); |
261 } | 277 } |
262 | 278 |
| 279 TEST_F(ReadableStreamOperationsTest, CreateReadableStreamWithCustomUnderlyingSou
rceAndStrategy) |
| 280 { |
| 281 auto underlyingSource = new TestUnderlyingSource(scriptState()); |
| 282 |
| 283 ScriptValue strategy = ReadableStreamOperations::createCountQueuingStrategy(
scriptState(), 10); |
| 284 ASSERT_FALSE(strategy.isEmpty()); |
| 285 |
| 286 ScriptValue stream = ReadableStreamOperations::createReadableStream(scriptSt
ate(), underlyingSource, strategy); |
| 287 ASSERT_FALSE(stream.isEmpty()); |
| 288 |
| 289 EXPECT_EQ(10, underlyingSource->desiredSize()); |
| 290 |
| 291 underlyingSource->enqueue(ScriptValue::from(scriptState(), "a")); |
| 292 EXPECT_EQ(9, underlyingSource->desiredSize()); |
| 293 |
| 294 underlyingSource->enqueue(ScriptValue::from(scriptState(), "b")); |
| 295 EXPECT_EQ(8, underlyingSource->desiredSize()); |
| 296 |
| 297 ScriptValue reader; |
| 298 { |
| 299 TrackExceptionState es; |
| 300 reader = ReadableStreamOperations::getReader(scriptState(), stream, es); |
| 301 ASSERT_FALSE(es.hadException()); |
| 302 } |
| 303 ASSERT_FALSE(reader.isEmpty()); |
| 304 |
| 305 Iteration* it1 = new Iteration(); |
| 306 Iteration* it2 = new Iteration(); |
| 307 Iteration* it3 = new Iteration(); |
| 308 ReadableStreamOperations::read(scriptState(), reader).then(Function::createF
unction(scriptState(), it1), NotReached::createFunction(scriptState())); |
| 309 ReadableStreamOperations::read(scriptState(), reader).then(Function::createF
unction(scriptState(), it2), NotReached::createFunction(scriptState())); |
| 310 ReadableStreamOperations::read(scriptState(), reader).then(Function::createF
unction(scriptState(), it3), NotReached::createFunction(scriptState())); |
| 311 |
| 312 isolate()->RunMicrotasks(); |
| 313 |
| 314 EXPECT_EQ(10, underlyingSource->desiredSize()); |
| 315 |
| 316 EXPECT_TRUE(it1->isSet()); |
| 317 EXPECT_TRUE(it1->isValid()); |
| 318 EXPECT_FALSE(it1->isDone()); |
| 319 EXPECT_EQ("a", it1->value()); |
| 320 |
| 321 EXPECT_TRUE(it2->isSet()); |
| 322 EXPECT_TRUE(it2->isValid()); |
| 323 EXPECT_FALSE(it2->isDone()); |
| 324 EXPECT_EQ("b", it2->value()); |
| 325 |
| 326 EXPECT_FALSE(it3->isSet()); |
| 327 |
| 328 underlyingSource->close(); |
| 329 isolate()->RunMicrotasks(); |
| 330 |
| 331 EXPECT_TRUE(it3->isSet()); |
| 332 EXPECT_TRUE(it3->isValid()); |
| 333 EXPECT_TRUE(it3->isDone()); |
| 334 } |
| 335 |
263 } // namespace | 336 } // namespace |
264 | 337 |
265 } // namespace blink | 338 } // namespace blink |
266 | 339 |
OLD | NEW |