2 * Copyright (C) 2013 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
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
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.
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.
32 #include "bindings/v8/ScriptPromiseResolver.h"
34 #include "RuntimeEnabledFeatures.h"
35 #include "bindings/v8/DOMWrapperWorld.h"
36 #include "bindings/v8/ScriptPromise.h"
37 #include "bindings/v8/V8Binding.h"
38 #include "bindings/v8/custom/V8PromiseCustom.h"
40 #include <gtest/gtest.h>
47 class ScriptPromiseResolverTest : public testing::Test {
49 ScriptPromiseResolverTest()
50 : m_isolate(v8::Isolate::GetCurrent())
56 m_scope = V8ExecutionScope::create(m_isolate);
57 m_resolver = ScriptPromiseResolver::create(m_scope->scriptState());
58 m_promise = m_resolver->promise();
68 V8PromiseCustom::PromiseState state()
70 return V8PromiseCustom::getState(V8PromiseCustom::getInternal(promise()));
73 v8::Local<v8::Value> result()
75 return V8PromiseCustom::getInternal(promise())->GetInternalField(V8PromiseCustom::InternalResultIndex);
78 v8::Local<v8::Object> promise()
80 ASSERT(!m_promise.isEmpty());
81 return m_promise.v8Value().As<v8::Object>();
85 v8::Isolate* m_isolate;
86 RefPtr<ScriptPromiseResolver> m_resolver;
87 ScriptPromise m_promise;
89 OwnPtr<V8ExecutionScope> m_scope;
92 TEST_F(ScriptPromiseResolverTest, initialState)
94 if (RuntimeEnabledFeatures::scriptPromiseOnV8PromiseEnabled())
96 EXPECT_EQ(V8PromiseCustom::Pending, state());
97 EXPECT_TRUE(result()->IsUndefined());
100 TEST_F(ScriptPromiseResolverTest, resolve)
102 if (RuntimeEnabledFeatures::scriptPromiseOnV8PromiseEnabled())
104 EXPECT_EQ(V8PromiseCustom::Pending, state());
105 EXPECT_TRUE(result()->IsUndefined());
107 m_resolver->resolve(ScriptValue(ScriptState::current(m_isolate), v8::Integer::New(m_isolate, 3)));
109 EXPECT_EQ(V8PromiseCustom::Fulfilled, state());
110 ASSERT_TRUE(result()->IsNumber());
111 EXPECT_EQ(3, result().As<v8::Integer>()->Value());
114 TEST_F(ScriptPromiseResolverTest, reject)
116 if (RuntimeEnabledFeatures::scriptPromiseOnV8PromiseEnabled())
118 EXPECT_EQ(V8PromiseCustom::Pending, state());
119 EXPECT_TRUE(result()->IsUndefined());
121 m_resolver->reject(ScriptValue(ScriptState::current(m_isolate), v8::Integer::New(m_isolate, 3)));
123 EXPECT_EQ(V8PromiseCustom::Rejected, state());
124 ASSERT_TRUE(result()->IsNumber());
125 EXPECT_EQ(3, result().As<v8::Integer>()->Value());
128 TEST_F(ScriptPromiseResolverTest, resolveOverResolve)
130 if (RuntimeEnabledFeatures::scriptPromiseOnV8PromiseEnabled())
132 EXPECT_EQ(V8PromiseCustom::Pending, state());
133 EXPECT_TRUE(result()->IsUndefined());
135 m_resolver->resolve(ScriptValue(ScriptState::current(m_isolate), v8::Integer::New(m_isolate, 3)));
137 EXPECT_EQ(V8PromiseCustom::Fulfilled, state());
138 ASSERT_TRUE(result()->IsNumber());
139 EXPECT_EQ(3, result().As<v8::Integer>()->Value());
141 m_resolver->resolve(ScriptValue(ScriptState::current(m_isolate), v8::Integer::New(m_isolate, 4)));
142 EXPECT_EQ(V8PromiseCustom::Fulfilled, state());
143 ASSERT_TRUE(result()->IsNumber());
144 EXPECT_EQ(3, result().As<v8::Integer>()->Value());
147 TEST_F(ScriptPromiseResolverTest, rejectOverResolve)
149 if (RuntimeEnabledFeatures::scriptPromiseOnV8PromiseEnabled())
151 EXPECT_EQ(V8PromiseCustom::Pending, state());
152 EXPECT_TRUE(result()->IsUndefined());
154 m_resolver->resolve(ScriptValue(ScriptState::current(m_isolate), v8::Integer::New(m_isolate, 3)));
156 EXPECT_EQ(V8PromiseCustom::Fulfilled, state());
157 ASSERT_TRUE(result()->IsNumber());
158 EXPECT_EQ(3, result().As<v8::Integer>()->Value());
160 m_resolver->reject(ScriptValue(ScriptState::current(m_isolate), v8::Integer::New(m_isolate, 4)));
161 EXPECT_EQ(V8PromiseCustom::Fulfilled, state());
162 ASSERT_TRUE(result()->IsNumber());
163 EXPECT_EQ(3, result().As<v8::Integer>()->Value());
168 } // namespace WebCore