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

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

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

Powered by Google App Engine
This is Rietveld 408576698