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