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

Side by Side Diff: Source/bindings/v8/ScriptPromiseResolverTest.cpp

Issue 23479016: Introduce Promise mapping to the IDL generator (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 7 years, 3 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 /* 1 /*
2 * Copyright (C) 2013 Google Inc. All rights reserved. 2 * Copyright (C) 2013 Google Inc. All rights reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are 5 * modification, are permitted provided that the following conditions are
6 * met: 6 * met:
7 * 7 *
8 * * Redistributions of source code must retain the above copyright 8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above 10 * * Redistributions in binary form must reproduce the above
(...skipping 13 matching lines...) Expand all
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */ 29 */
30 30
31 #include "config.h" 31 #include "config.h"
32 #include "bindings/v8/ScriptPromiseResolver.h" 32 #include "bindings/v8/ScriptPromiseResolver.h"
33 33
34 #include "bindings/v8/ScriptPromise.h"
34 #include "bindings/v8/V8Binding.h" 35 #include "bindings/v8/V8Binding.h"
35 #include "bindings/v8/custom/V8PromiseCustom.h" 36 #include "bindings/v8/custom/V8PromiseCustom.h"
36 37
37 #include <gtest/gtest.h> 38 #include <gtest/gtest.h>
38 #include <v8.h> 39 #include <v8.h>
39 40
40 namespace WebCore { 41 namespace WebCore {
41 42
42 namespace { 43 namespace {
43 44
44 class ScriptPromiseResolverTest : public testing::Test { 45 class ScriptPromiseResolverTest : public testing::Test {
45 public: 46 public:
46 ScriptPromiseResolverTest() 47 ScriptPromiseResolverTest()
47 : m_isolate(v8::Isolate::GetCurrent()) 48 : m_isolate(v8::Isolate::GetCurrent())
48 , m_handleScope(m_isolate) 49 , m_handleScope(m_isolate)
49 , m_context(v8::Context::New(m_isolate)) 50 , m_context(v8::Context::New(m_isolate))
50 , m_contextScope(m_context.newLocal(m_isolate)) 51 , m_contextScope(m_context.newLocal(m_isolate))
51 { 52 {
52 } 53 }
53 54
54 void SetUp() 55 void SetUp()
55 { 56 {
56 m_perContextData = V8PerContextData::create(m_context.newLocal(m_isolate )); 57 m_perContextData = V8PerContextData::create(m_context.newLocal(m_isolate ));
57 m_perContextData->init(); 58 m_perContextData->init();
58 m_resolver = ScriptPromiseResolver::create(); 59 m_resolver = ScriptPromiseResolver::create();
59 m_promise = m_resolver->promise(); 60 m_promise = m_resolver.promise();
60 } 61 }
61 62
62 void TearDown() 63 void TearDown()
63 { 64 {
64 m_resolver = 0; 65 m_resolver.detach();
65 m_promise.clear(); 66 m_promise.clear();
66 m_perContextData.clear(); 67 m_perContextData.clear();
67 } 68 }
68 69
69 V8PromiseCustom::PromiseState state() 70 V8PromiseCustom::PromiseState state()
70 { 71 {
71 return V8PromiseCustom::getState(V8PromiseCustom::getInternal(promise()) ); 72 return V8PromiseCustom::getState(V8PromiseCustom::getInternal(promise()) );
72 } 73 }
73 74
74 v8::Local<v8::Value> result() 75 v8::Local<v8::Value> result()
75 { 76 {
76 return V8PromiseCustom::getInternal(promise())->GetInternalField(V8Promi seCustom::InternalResultIndex); 77 return V8PromiseCustom::getInternal(promise())->GetInternalField(V8Promi seCustom::InternalResultIndex);
77 } 78 }
78 79
79 v8::Local<v8::Object> promise() 80 v8::Local<v8::Object> promise()
80 { 81 {
81 ASSERT(!m_promise.hasNoValue()); 82 ASSERT(!m_promise.hasNoValue());
82 return m_promise.v8Object(); 83 return m_promise.v8Value().As<v8::Object>();
83 } 84 }
84 85
85 protected: 86 protected:
86 v8::Isolate* m_isolate; 87 v8::Isolate* m_isolate;
87 v8::HandleScope m_handleScope; 88 v8::HandleScope m_handleScope;
88 ScopedPersistent<v8::Context> m_context; 89 ScopedPersistent<v8::Context> m_context;
89 v8::Context::Scope m_contextScope; 90 v8::Context::Scope m_contextScope;
90 RefPtr<ScriptPromiseResolver> m_resolver; 91 ScriptPromiseResolver m_resolver;
91 ScriptObject m_promise; 92 ScriptPromise m_promise;
92 OwnPtr<V8PerContextData> m_perContextData; 93 OwnPtr<V8PerContextData> m_perContextData;
93 }; 94 };
94 95
95 TEST_F(ScriptPromiseResolverTest, initialState) 96 TEST_F(ScriptPromiseResolverTest, initialState)
96 { 97 {
97 EXPECT_TRUE(m_resolver->isPending()); 98 EXPECT_TRUE(m_resolver.isPending());
98 EXPECT_EQ(V8PromiseCustom::Pending, state()); 99 EXPECT_EQ(V8PromiseCustom::Pending, state());
99 EXPECT_TRUE(result()->IsUndefined()); 100 EXPECT_TRUE(result()->IsUndefined());
100 } 101 }
101 102
102 TEST_F(ScriptPromiseResolverTest, fulfill) 103 TEST_F(ScriptPromiseResolverTest, fulfill)
103 { 104 {
104 EXPECT_TRUE(m_resolver->isPending()); 105 EXPECT_TRUE(m_resolver.isPending());
105 EXPECT_EQ(V8PromiseCustom::Pending, state()); 106 EXPECT_EQ(V8PromiseCustom::Pending, state());
106 EXPECT_TRUE(result()->IsUndefined()); 107 EXPECT_TRUE(result()->IsUndefined());
107 108
108 m_resolver->fulfill(ScriptValue(v8::Integer::New(3))); 109 m_resolver.fulfill(ScriptValue(v8::Integer::New(3)));
109 110
110 EXPECT_FALSE(m_resolver->isPending()); 111 EXPECT_FALSE(m_resolver.isPending());
111 EXPECT_EQ(V8PromiseCustom::Fulfilled, state()); 112 EXPECT_EQ(V8PromiseCustom::Fulfilled, state());
112 ASSERT_TRUE(result()->IsNumber()); 113 ASSERT_TRUE(result()->IsNumber());
113 EXPECT_EQ(3, result().As<v8::Integer>()->Value()); 114 EXPECT_EQ(3, result().As<v8::Integer>()->Value());
114 } 115 }
115 116
116 TEST_F(ScriptPromiseResolverTest, resolve) 117 TEST_F(ScriptPromiseResolverTest, resolve)
117 { 118 {
118 EXPECT_TRUE(m_resolver->isPending()); 119 EXPECT_TRUE(m_resolver.isPending());
119 EXPECT_EQ(V8PromiseCustom::Pending, state()); 120 EXPECT_EQ(V8PromiseCustom::Pending, state());
120 EXPECT_TRUE(result()->IsUndefined()); 121 EXPECT_TRUE(result()->IsUndefined());
121 122
122 m_resolver->resolve(ScriptValue(v8::Integer::New(3))); 123 m_resolver.resolve(ScriptValue(v8::Integer::New(3)));
123 124
124 EXPECT_FALSE(m_resolver->isPending()); 125 EXPECT_FALSE(m_resolver.isPending());
125 EXPECT_EQ(V8PromiseCustom::Fulfilled, state()); 126 EXPECT_EQ(V8PromiseCustom::Fulfilled, state());
126 ASSERT_TRUE(result()->IsNumber()); 127 ASSERT_TRUE(result()->IsNumber());
127 EXPECT_EQ(3, result().As<v8::Integer>()->Value()); 128 EXPECT_EQ(3, result().As<v8::Integer>()->Value());
128 } 129 }
129 130
130 TEST_F(ScriptPromiseResolverTest, reject) 131 TEST_F(ScriptPromiseResolverTest, reject)
131 { 132 {
132 EXPECT_TRUE(m_resolver->isPending()); 133 EXPECT_TRUE(m_resolver.isPending());
133 EXPECT_EQ(V8PromiseCustom::Pending, state()); 134 EXPECT_EQ(V8PromiseCustom::Pending, state());
134 EXPECT_TRUE(result()->IsUndefined()); 135 EXPECT_TRUE(result()->IsUndefined());
135 136
136 m_resolver->reject(ScriptValue(v8::Integer::New(3))); 137 m_resolver.reject(ScriptValue(v8::Integer::New(3)));
137 138
138 EXPECT_FALSE(m_resolver->isPending()); 139 EXPECT_FALSE(m_resolver.isPending());
139 EXPECT_EQ(V8PromiseCustom::Rejected, state()); 140 EXPECT_EQ(V8PromiseCustom::Rejected, state());
140 ASSERT_TRUE(result()->IsNumber()); 141 ASSERT_TRUE(result()->IsNumber());
141 EXPECT_EQ(3, result().As<v8::Integer>()->Value()); 142 EXPECT_EQ(3, result().As<v8::Integer>()->Value());
142 } 143 }
143 144
144 TEST_F(ScriptPromiseResolverTest, fulfillOverFulfill) 145 TEST_F(ScriptPromiseResolverTest, fulfillOverFulfill)
145 { 146 {
146 EXPECT_TRUE(m_resolver->isPending()); 147 EXPECT_TRUE(m_resolver.isPending());
147 EXPECT_EQ(V8PromiseCustom::Pending, state()); 148 EXPECT_EQ(V8PromiseCustom::Pending, state());
148 EXPECT_TRUE(result()->IsUndefined()); 149 EXPECT_TRUE(result()->IsUndefined());
149 150
150 m_resolver->fulfill(ScriptValue(v8::Integer::New(3))); 151 m_resolver.fulfill(ScriptValue(v8::Integer::New(3)));
151 152
152 EXPECT_FALSE(m_resolver->isPending()); 153 EXPECT_FALSE(m_resolver.isPending());
153 EXPECT_EQ(V8PromiseCustom::Fulfilled, state()); 154 EXPECT_EQ(V8PromiseCustom::Fulfilled, state());
154 ASSERT_TRUE(result()->IsNumber()); 155 ASSERT_TRUE(result()->IsNumber());
155 EXPECT_EQ(3, result().As<v8::Integer>()->Value()); 156 EXPECT_EQ(3, result().As<v8::Integer>()->Value());
156 157
157 m_resolver->fulfill(ScriptValue(v8::Integer::New(4))); 158 m_resolver.fulfill(ScriptValue(v8::Integer::New(4)));
158 EXPECT_FALSE(m_resolver->isPending()); 159 EXPECT_FALSE(m_resolver.isPending());
159 EXPECT_EQ(V8PromiseCustom::Fulfilled, state()); 160 EXPECT_EQ(V8PromiseCustom::Fulfilled, state());
160 ASSERT_TRUE(result()->IsNumber()); 161 ASSERT_TRUE(result()->IsNumber());
161 EXPECT_EQ(3, result().As<v8::Integer>()->Value()); 162 EXPECT_EQ(3, result().As<v8::Integer>()->Value());
162 } 163 }
163 164
164 TEST_F(ScriptPromiseResolverTest, rejectOverFulfill) 165 TEST_F(ScriptPromiseResolverTest, rejectOverFulfill)
165 { 166 {
166 EXPECT_TRUE(m_resolver->isPending()); 167 EXPECT_TRUE(m_resolver.isPending());
167 EXPECT_EQ(V8PromiseCustom::Pending, state()); 168 EXPECT_EQ(V8PromiseCustom::Pending, state());
168 EXPECT_TRUE(result()->IsUndefined()); 169 EXPECT_TRUE(result()->IsUndefined());
169 170
170 m_resolver->fulfill(ScriptValue(v8::Integer::New(3))); 171 m_resolver.fulfill(ScriptValue(v8::Integer::New(3)));
171 172
172 EXPECT_FALSE(m_resolver->isPending()); 173 EXPECT_FALSE(m_resolver.isPending());
173 EXPECT_EQ(V8PromiseCustom::Fulfilled, state()); 174 EXPECT_EQ(V8PromiseCustom::Fulfilled, state());
174 ASSERT_TRUE(result()->IsNumber()); 175 ASSERT_TRUE(result()->IsNumber());
175 EXPECT_EQ(3, result().As<v8::Integer>()->Value()); 176 EXPECT_EQ(3, result().As<v8::Integer>()->Value());
176 177
177 m_resolver->reject(ScriptValue(v8::Integer::New(4))); 178 m_resolver.reject(ScriptValue(v8::Integer::New(4)));
178 EXPECT_FALSE(m_resolver->isPending()); 179 EXPECT_FALSE(m_resolver.isPending());
179 EXPECT_EQ(V8PromiseCustom::Fulfilled, state()); 180 EXPECT_EQ(V8PromiseCustom::Fulfilled, state());
180 ASSERT_TRUE(result()->IsNumber()); 181 ASSERT_TRUE(result()->IsNumber());
181 EXPECT_EQ(3, result().As<v8::Integer>()->Value()); 182 EXPECT_EQ(3, result().As<v8::Integer>()->Value());
182 } 183 }
183 184
184 TEST_F(ScriptPromiseResolverTest, detach) 185 TEST_F(ScriptPromiseResolverTest, detach)
185 { 186 {
186 EXPECT_TRUE(m_resolver->isPending()); 187 EXPECT_TRUE(m_resolver.isPending());
187 EXPECT_EQ(V8PromiseCustom::Pending, state()); 188 EXPECT_EQ(V8PromiseCustom::Pending, state());
188 EXPECT_TRUE(result()->IsUndefined()); 189 EXPECT_TRUE(result()->IsUndefined());
189 190
190 m_resolver->detach(); 191 m_resolver.detach();
191 192
192 EXPECT_FALSE(m_resolver->isPending()); 193 EXPECT_FALSE(m_resolver.isPending());
193 EXPECT_EQ(V8PromiseCustom::Rejected, state()); 194 EXPECT_EQ(V8PromiseCustom::Rejected, state());
194 EXPECT_TRUE(result()->IsUndefined()); 195 EXPECT_TRUE(result()->IsUndefined());
195 } 196 }
196 197
197 } // namespace 198 } // namespace
198 199
199 } // namespace WebCore 200 } // namespace WebCore
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698