Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / bindings / core / v8 / ScriptPromiseTest.cpp
1 /*
2  * Copyright (C) 2014 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
8  *     * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *     * Redistributions in binary form must reproduce the above
11  * copyright notice, this list of conditions and the following disclaimer
12  * in the documentation and/or other materials provided with the
13  * distribution.
14  *     * Neither the name of Google Inc. nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
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.
29  */
30
31 #include "config.h"
32 #include "bindings/core/v8/ScriptPromise.h"
33
34 #include "bindings/core/v8/ScriptFunction.h"
35 #include "bindings/core/v8/ScriptValue.h"
36 #include "bindings/core/v8/V8Binding.h"
37 #include "core/dom/DOMException.h"
38 #include "core/dom/ExceptionCode.h"
39
40 #include <gtest/gtest.h>
41 #include <v8.h>
42
43 namespace blink {
44
45 namespace {
46
47 void callback(const v8::FunctionCallbackInfo<v8::Value>& info) { }
48
49 class Function : public ScriptFunction {
50 public:
51     static PassOwnPtr<Function> create(v8::Isolate* isolate, String* value)
52     {
53         return adoptPtr(new Function(isolate, value));
54     }
55
56     virtual ScriptValue call(ScriptValue value) OVERRIDE
57     {
58         ASSERT(!value.isEmpty());
59         *m_value = toCoreString(value.v8Value()->ToString());
60         return value;
61     }
62
63 private:
64     Function(v8::Isolate* isolate, String* value) : ScriptFunction(isolate), m_value(value) { }
65
66     String* m_value;
67 };
68
69 class ScriptPromiseTest : public testing::Test {
70 public:
71     ScriptPromiseTest()
72         : m_scope(v8::Isolate::GetCurrent())
73     {
74     }
75
76     ~ScriptPromiseTest()
77     {
78         // FIXME: We put this statement here to clear an exception from the isolate.
79         createClosure(callback, v8::Undefined(m_scope.isolate()), m_scope.isolate());
80
81         // Execute all pending microtasks
82         isolate()->RunMicrotasks();
83     }
84
85     ScriptState* scriptState() const { return m_scope.scriptState(); }
86     v8::Isolate* isolate() const { return m_scope.isolate(); }
87
88 protected:
89     typedef ScriptPromise::InternalResolver Resolver;
90     V8TestingScope m_scope;
91 };
92
93 TEST_F(ScriptPromiseTest, constructFromNonPromise)
94 {
95     v8::TryCatch trycatch;
96     ScriptPromise promise(scriptState(), v8::Undefined(isolate()));
97     ASSERT_TRUE(trycatch.HasCaught());
98     ASSERT_TRUE(promise.isEmpty());
99 }
100
101 TEST_F(ScriptPromiseTest, thenResolve)
102 {
103     Resolver resolver(scriptState());
104     ScriptPromise promise = resolver.promise();
105     String onFulfilled, onRejected;
106     promise.then(Function::create(isolate(), &onFulfilled), Function::create(isolate(), &onRejected));
107
108     ASSERT_FALSE(promise.isEmpty());
109     EXPECT_EQ(String(), onFulfilled);
110     EXPECT_EQ(String(), onRejected);
111
112     isolate()->RunMicrotasks();
113     resolver.resolve(v8String(isolate(), "hello"));
114
115     EXPECT_EQ(String(), onFulfilled);
116     EXPECT_EQ(String(), onRejected);
117
118     isolate()->RunMicrotasks();
119
120     EXPECT_EQ("hello", onFulfilled);
121     EXPECT_EQ(String(), onRejected);
122 }
123
124 TEST_F(ScriptPromiseTest, resolveThen)
125 {
126     Resolver resolver(scriptState());
127     ScriptPromise promise = resolver.promise();
128     String onFulfilled, onRejected;
129     resolver.resolve(v8String(isolate(), "hello"));
130     promise.then(Function::create(isolate(), &onFulfilled), Function::create(isolate(), &onRejected));
131
132     ASSERT_FALSE(promise.isEmpty());
133     EXPECT_EQ(String(), onFulfilled);
134     EXPECT_EQ(String(), onRejected);
135
136     isolate()->RunMicrotasks();
137
138     EXPECT_EQ("hello", onFulfilled);
139     EXPECT_EQ(String(), onRejected);
140 }
141
142 TEST_F(ScriptPromiseTest, thenReject)
143 {
144     Resolver resolver(scriptState());
145     ScriptPromise promise = resolver.promise();
146     String onFulfilled, onRejected;
147     promise.then(Function::create(isolate(), &onFulfilled), Function::create(isolate(), &onRejected));
148
149     ASSERT_FALSE(promise.isEmpty());
150     EXPECT_EQ(String(), onFulfilled);
151     EXPECT_EQ(String(), onRejected);
152
153     isolate()->RunMicrotasks();
154     resolver.reject(v8String(isolate(), "hello"));
155
156     EXPECT_EQ(String(), onFulfilled);
157     EXPECT_EQ(String(), onRejected);
158
159     isolate()->RunMicrotasks();
160
161     EXPECT_EQ(String(), onFulfilled);
162     EXPECT_EQ("hello", onRejected);
163 }
164
165 TEST_F(ScriptPromiseTest, rejectThen)
166 {
167     Resolver resolver(scriptState());
168     ScriptPromise promise = resolver.promise();
169     String onFulfilled, onRejected;
170     resolver.reject(v8String(isolate(), "hello"));
171     promise.then(Function::create(isolate(), &onFulfilled), Function::create(isolate(), &onRejected));
172
173     ASSERT_FALSE(promise.isEmpty());
174     EXPECT_EQ(String(), onFulfilled);
175     EXPECT_EQ(String(), onRejected);
176
177     isolate()->RunMicrotasks();
178
179     EXPECT_EQ(String(), onFulfilled);
180     EXPECT_EQ("hello", onRejected);
181 }
182
183 TEST_F(ScriptPromiseTest, castPromise)
184 {
185     ScriptPromise promise = Resolver(scriptState()).promise();
186     ScriptPromise newPromise = ScriptPromise::cast(scriptState(), promise.v8Value());
187
188     ASSERT_FALSE(promise.isEmpty());
189     EXPECT_EQ(promise.v8Value(), newPromise.v8Value());
190 }
191
192 TEST_F(ScriptPromiseTest, castNonPromise)
193 {
194     String onFulfilled1, onFulfilled2, onRejected1, onRejected2;
195
196     ScriptValue value = ScriptValue(scriptState(), v8String(isolate(), "hello"));
197     ScriptPromise promise1 = ScriptPromise::cast(scriptState(), ScriptValue(value));
198     ScriptPromise promise2 = ScriptPromise::cast(scriptState(), ScriptValue(value));
199     promise1.then(Function::create(isolate(), &onFulfilled1), Function::create(isolate(), &onRejected1));
200     promise2.then(Function::create(isolate(), &onFulfilled2), Function::create(isolate(), &onRejected2));
201
202     ASSERT_FALSE(promise1.isEmpty());
203     ASSERT_FALSE(promise2.isEmpty());
204     EXPECT_NE(promise1.v8Value(), promise2.v8Value());
205
206     ASSERT_TRUE(promise1.v8Value()->IsPromise());
207     ASSERT_TRUE(promise2.v8Value()->IsPromise());
208
209     EXPECT_EQ(String(), onFulfilled1);
210     EXPECT_EQ(String(), onFulfilled2);
211     EXPECT_EQ(String(), onRejected1);
212     EXPECT_EQ(String(), onRejected2);
213
214     isolate()->RunMicrotasks();
215
216     EXPECT_EQ("hello", onFulfilled1);
217     EXPECT_EQ("hello", onFulfilled2);
218     EXPECT_EQ(String(), onRejected1);
219     EXPECT_EQ(String(), onRejected2);
220 }
221
222 TEST_F(ScriptPromiseTest, reject)
223 {
224     String onFulfilled, onRejected;
225
226     ScriptValue value = ScriptValue(scriptState(), v8String(isolate(), "hello"));
227     ScriptPromise promise = ScriptPromise::reject(scriptState(), ScriptValue(value));
228     promise.then(Function::create(isolate(), &onFulfilled), Function::create(isolate(), &onRejected));
229
230     ASSERT_FALSE(promise.isEmpty());
231     ASSERT_TRUE(promise.v8Value()->IsPromise());
232
233     EXPECT_EQ(String(), onFulfilled);
234     EXPECT_EQ(String(), onRejected);
235
236     isolate()->RunMicrotasks();
237
238     EXPECT_EQ(String(), onFulfilled);
239     EXPECT_EQ("hello", onRejected);
240 }
241
242 TEST_F(ScriptPromiseTest, rejectWithExceptionState)
243 {
244     String onFulfilled, onRejected;
245     ScriptPromise promise = ScriptPromise::rejectWithDOMException(scriptState(), DOMException::create(SyntaxError, "some syntax error"));
246     promise.then(Function::create(isolate(), &onFulfilled), Function::create(isolate(), &onRejected));
247
248     ASSERT_FALSE(promise.isEmpty());
249     EXPECT_EQ(String(), onFulfilled);
250     EXPECT_EQ(String(), onRejected);
251
252     isolate()->RunMicrotasks();
253
254     EXPECT_EQ(String(), onFulfilled);
255     EXPECT_EQ("SyntaxError: some syntax error", onRejected);
256 }
257
258 } // namespace
259
260 } // namespace blink