OLD | NEW |
(Empty) | |
| 1 // Copyright 2014 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 "core/streams/ReadableStream.h" |
| 7 |
| 8 #include "bindings/core/v8/ExceptionState.h" |
| 9 #include "bindings/core/v8/ScriptPromiseResolver.h" |
| 10 #include "bindings/core/v8/ScriptState.h" |
| 11 #include "bindings/core/v8/V8Binding.h" |
| 12 #include "core/streams/UnderlyingSource.h" |
| 13 #include "core/testing/DummyPageHolder.h" |
| 14 #include "platform/testing/TestWithGC.h" |
| 15 #include <gmock/gmock.h> |
| 16 #include <gtest/gtest.h> |
| 17 |
| 18 namespace WebCore { |
| 19 |
| 20 namespace { |
| 21 |
| 22 using ::testing::_; |
| 23 using ::testing::InSequence; |
| 24 using ::testing::Invoke; |
| 25 using ::testing::Return; |
| 26 |
| 27 class MockUnderlyingSource : public UnderlyingSource { |
| 28 public: |
| 29 virtual ~MockUnderlyingSource() { } |
| 30 |
| 31 MOCK_METHOD1(startStream, ScriptPromise(ExceptionState*)); |
| 32 MOCK_METHOD1(pullStream, void(ExceptionState*)); |
| 33 MOCK_METHOD1(cancelStream, void(ExceptionState*)); |
| 34 }; |
| 35 |
| 36 class ThrowError { |
| 37 public: |
| 38 explicit ThrowError(const String& message) |
| 39 : m_message(message) { } |
| 40 |
| 41 void operator()(ExceptionState* exceptionState) |
| 42 { |
| 43 exceptionState->throwTypeError(m_message); |
| 44 } |
| 45 private: |
| 46 String m_message; |
| 47 }; |
| 48 |
| 49 class ReadableStreamTest : public TestWithGC { |
| 50 public: |
| 51 virtual ~ReadableStreamTest() { } |
| 52 |
| 53 ReadableStreamTest() |
| 54 : m_page(DummyPageHolder::create(IntSize(1, 1))) |
| 55 , m_scope(scriptState()) |
| 56 , m_underlyingSource(new ::testing::StrictMock<MockUnderlyingSource>) |
| 57 , m_exceptionState(ExceptionState::ConstructionContext, "property", "int
erface", scriptState()->context()->Global(), isolate()) |
| 58 { |
| 59 } |
| 60 |
| 61 ScriptState* scriptState() { return ScriptState::forMainWorld(m_page->docume
nt().frame()); } |
| 62 v8::Isolate* isolate() { return scriptState()->isolate(); } |
| 63 |
| 64 OwnPtr<DummyPageHolder> m_page; |
| 65 ScriptState::Scope m_scope; |
| 66 Persistent<MockUnderlyingSource> m_underlyingSource; |
| 67 ExceptionState m_exceptionState; |
| 68 }; |
| 69 |
| 70 TEST_F(ReadableStreamTest, construct) |
| 71 { |
| 72 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scrip
tState()); |
| 73 ScriptPromise promise = resolver->promise(); |
| 74 { |
| 75 InSequence s; |
| 76 EXPECT_CALL(*m_underlyingSource, startStream(&m_exceptionState)).WillOnc
e(Return(promise)); |
| 77 } |
| 78 ReadableStream* stream = new ReadableStream(scriptState(), m_underlyingSourc
e, &m_exceptionState); |
| 79 EXPECT_FALSE(m_exceptionState.hadException()); |
| 80 EXPECT_FALSE(stream->isStarted()); |
| 81 EXPECT_FALSE(stream->isDraining()); |
| 82 EXPECT_FALSE(stream->isPulling()); |
| 83 EXPECT_EQ(stream->state(), ReadableStream::Waiting); |
| 84 |
| 85 isolate()->RunMicrotasks(); |
| 86 |
| 87 EXPECT_FALSE(stream->isStarted()); |
| 88 |
| 89 resolver->resolve(); |
| 90 isolate()->RunMicrotasks(); |
| 91 |
| 92 EXPECT_TRUE(stream->isStarted()); |
| 93 EXPECT_FALSE(stream->isDraining()); |
| 94 EXPECT_FALSE(stream->isPulling()); |
| 95 EXPECT_EQ(stream->state(), ReadableStream::Waiting); |
| 96 } |
| 97 |
| 98 TEST_F(ReadableStreamTest, constructError) |
| 99 { |
| 100 { |
| 101 InSequence s; |
| 102 EXPECT_CALL(*m_underlyingSource, startStream(&m_exceptionState)) |
| 103 .WillOnce(DoAll(Invoke(ThrowError("hello")), Return(ScriptPromise())
)); |
| 104 } |
| 105 new ReadableStream(scriptState(), m_underlyingSource, &m_exceptionState); |
| 106 EXPECT_TRUE(m_exceptionState.hadException()); |
| 107 } |
| 108 |
| 109 TEST_F(ReadableStreamTest, startFailAsynchronously) |
| 110 { |
| 111 RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scrip
tState()); |
| 112 ScriptPromise promise = resolver->promise(); |
| 113 { |
| 114 InSequence s; |
| 115 EXPECT_CALL(*m_underlyingSource, startStream(&m_exceptionState)).WillOnc
e(Return(promise)); |
| 116 } |
| 117 ReadableStream* stream = new ReadableStream(scriptState(), m_underlyingSourc
e, &m_exceptionState); |
| 118 EXPECT_FALSE(m_exceptionState.hadException()); |
| 119 EXPECT_FALSE(stream->isStarted()); |
| 120 EXPECT_FALSE(stream->isDraining()); |
| 121 EXPECT_FALSE(stream->isPulling()); |
| 122 EXPECT_EQ(stream->state(), ReadableStream::Waiting); |
| 123 |
| 124 isolate()->RunMicrotasks(); |
| 125 |
| 126 EXPECT_FALSE(stream->isStarted()); |
| 127 EXPECT_FALSE(stream->isDraining()); |
| 128 EXPECT_FALSE(stream->isPulling()); |
| 129 EXPECT_EQ(stream->state(), ReadableStream::Waiting); |
| 130 |
| 131 resolver->reject(); |
| 132 isolate()->RunMicrotasks(); |
| 133 |
| 134 EXPECT_FALSE(stream->isStarted()); |
| 135 EXPECT_FALSE(stream->isDraining()); |
| 136 EXPECT_FALSE(stream->isPulling()); |
| 137 EXPECT_EQ(stream->state(), ReadableStream::Errored); |
| 138 } |
| 139 |
| 140 } // namespace |
| 141 |
| 142 } // namespace WebCore |
| 143 |
OLD | NEW |