• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/v8/ScriptPromise.h"
33 
34 #include "bindings/v8/ScriptFunction.h"
35 #include "bindings/v8/ScriptPromiseResolver.h"
36 #include "bindings/v8/ScriptValue.h"
37 #include "bindings/v8/V8Binding.h"
38 #include "core/dom/DOMException.h"
39 #include "core/dom/ExceptionCode.h"
40 
41 #include <gtest/gtest.h>
42 #include <v8.h>
43 
44 namespace WebCore {
45 
46 namespace {
47 
callback(const v8::FunctionCallbackInfo<v8::Value> & info)48 void callback(const v8::FunctionCallbackInfo<v8::Value>& info) { }
49 
50 class Function : public ScriptFunction {
51 public:
create(v8::Isolate * isolate,String * value)52     static PassOwnPtr<Function> create(v8::Isolate* isolate, String* value)
53     {
54         return adoptPtr(new Function(isolate, value));
55     }
56 
call(ScriptValue value)57     virtual ScriptValue call(ScriptValue value) OVERRIDE
58     {
59         ASSERT(!value.isEmpty());
60         *m_value = toCoreString(value.v8Value()->ToString());
61         return value;
62     }
63 
64 private:
Function(v8::Isolate * isolate,String * value)65     Function(v8::Isolate* isolate, String* value) : ScriptFunction(isolate), m_value(value) { }
66 
67     String* m_value;
68 };
69 
70 class ScriptPromiseTest : public testing::Test {
71 public:
ScriptPromiseTest()72     ScriptPromiseTest()
73         : m_scope(v8::Isolate::GetCurrent())
74     {
75     }
76 
~ScriptPromiseTest()77     ~ScriptPromiseTest()
78     {
79         // FIXME: We put this statement here to clear an exception from the isolate.
80         createClosure(callback, v8::Undefined(m_scope.isolate()), m_scope.isolate());
81 
82         // Execute all pending microtasks
83         isolate()->RunMicrotasks();
84     }
85 
scriptState() const86     ScriptState* scriptState() const { return m_scope.scriptState(); }
isolate() const87     v8::Isolate* isolate() const { return m_scope.isolate(); }
88 
89 protected:
90     V8TestingScope m_scope;
91 };
92 
TEST_F(ScriptPromiseTest,constructFromNonPromise)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 
TEST_F(ScriptPromiseTest,thenResolve)101 TEST_F(ScriptPromiseTest, thenResolve)
102 {
103     RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(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("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 
TEST_F(ScriptPromiseTest,resolveThen)124 TEST_F(ScriptPromiseTest, resolveThen)
125 {
126     RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scriptState());
127     ScriptPromise promise = resolver->promise();
128     String onFulfilled, onRejected;
129     resolver->resolve("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 
TEST_F(ScriptPromiseTest,thenReject)142 TEST_F(ScriptPromiseTest, thenReject)
143 {
144     RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(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("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 
TEST_F(ScriptPromiseTest,rejectThen)165 TEST_F(ScriptPromiseTest, rejectThen)
166 {
167     RefPtr<ScriptPromiseResolver> resolver = ScriptPromiseResolver::create(scriptState());
168     ScriptPromise promise = resolver->promise();
169     String onFulfilled, onRejected;
170     resolver->reject("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 
TEST_F(ScriptPromiseTest,castPromise)183 TEST_F(ScriptPromiseTest, castPromise)
184 {
185     ScriptPromise promise = ScriptPromiseResolver::create(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 
TEST_F(ScriptPromiseTest,castNonPromise)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 
TEST_F(ScriptPromiseTest,reject)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 
TEST_F(ScriptPromiseTest,rejectWithExceptionState)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 WebCore
261