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