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 "bindings/v8/DOMWrapperWorld.h"
35 #include "bindings/v8/ScriptPromise.h"
36 #include "bindings/v8/V8Binding.h"
37 #include "bindings/v8/custom/V8PromiseCustom.h"
39 #include <gtest/gtest.h>
46 class ScriptPromiseResolverTest : public testing::Test {
48 ScriptPromiseResolverTest()
49 : m_isolate(v8::Isolate::GetCurrent())
50 , m_handleScope(m_isolate)
51 , m_context(m_isolate, v8::Context::New(m_isolate))
52 , m_contextScope(m_context.newLocal(m_isolate))
58 v8::Handle<v8::Context> context(m_context.newLocal(m_isolate));
59 V8PerContextDataHolder::install(context, DOMWrapperWorld::current(m_isolate));
60 m_perContextData = V8PerContextData::create(context);
61 m_perContextData->init();
62 m_promise = ScriptPromise::createPending();
63 m_resolver = ScriptPromiseResolver::create(m_promise);
70 m_perContextData.clear();
73 V8PromiseCustom::PromiseState state()
75 return V8PromiseCustom::getState(V8PromiseCustom::getInternal(promise()));
78 v8::Local<v8::Value> result()
80 return V8PromiseCustom::getInternal(promise())->GetInternalField(V8PromiseCustom::InternalResultIndex);
83 v8::Local<v8::Object> promise()
85 ASSERT(!m_promise.hasNoValue());
86 return m_promise.v8Value().As<v8::Object>();
90 v8::Isolate* m_isolate;
91 v8::HandleScope m_handleScope;
92 ScopedPersistent<v8::Context> m_context;
93 v8::Context::Scope m_contextScope;
94 RefPtr<ScriptPromiseResolver> m_resolver;
95 ScriptPromise m_promise;
96 OwnPtr<V8PerContextData> m_perContextData;
99 TEST_F(ScriptPromiseResolverTest, initialState)
101 EXPECT_TRUE(m_resolver->isPending());
102 EXPECT_EQ(V8PromiseCustom::Pending, state());
103 EXPECT_TRUE(result()->IsUndefined());
106 TEST_F(ScriptPromiseResolverTest, resolve)
108 EXPECT_TRUE(m_resolver->isPending());
109 EXPECT_EQ(V8PromiseCustom::Pending, state());
110 EXPECT_TRUE(result()->IsUndefined());
112 m_resolver->resolve(ScriptValue(v8::Integer::New(m_isolate, 3), m_isolate));
114 EXPECT_FALSE(m_resolver->isPending());
115 EXPECT_EQ(V8PromiseCustom::Fulfilled, state());
116 ASSERT_TRUE(result()->IsNumber());
117 EXPECT_EQ(3, result().As<v8::Integer>()->Value());
120 TEST_F(ScriptPromiseResolverTest, reject)
122 EXPECT_TRUE(m_resolver->isPending());
123 EXPECT_EQ(V8PromiseCustom::Pending, state());
124 EXPECT_TRUE(result()->IsUndefined());
126 m_resolver->reject(ScriptValue(v8::Integer::New(m_isolate, 3), m_isolate));
128 EXPECT_FALSE(m_resolver->isPending());
129 EXPECT_EQ(V8PromiseCustom::Rejected, state());
130 ASSERT_TRUE(result()->IsNumber());
131 EXPECT_EQ(3, result().As<v8::Integer>()->Value());
134 TEST_F(ScriptPromiseResolverTest, resolveOverResolve)
136 EXPECT_TRUE(m_resolver->isPending());
137 EXPECT_EQ(V8PromiseCustom::Pending, state());
138 EXPECT_TRUE(result()->IsUndefined());
140 m_resolver->resolve(ScriptValue(v8::Integer::New(m_isolate, 3), m_isolate));
142 EXPECT_FALSE(m_resolver->isPending());
143 EXPECT_EQ(V8PromiseCustom::Fulfilled, state());
144 ASSERT_TRUE(result()->IsNumber());
145 EXPECT_EQ(3, result().As<v8::Integer>()->Value());
147 m_resolver->resolve(ScriptValue(v8::Integer::New(m_isolate, 4), m_isolate));
148 EXPECT_FALSE(m_resolver->isPending());
149 EXPECT_EQ(V8PromiseCustom::Fulfilled, state());
150 ASSERT_TRUE(result()->IsNumber());
151 EXPECT_EQ(3, result().As<v8::Integer>()->Value());
154 TEST_F(ScriptPromiseResolverTest, rejectOverResolve)
156 EXPECT_TRUE(m_resolver->isPending());
157 EXPECT_EQ(V8PromiseCustom::Pending, state());
158 EXPECT_TRUE(result()->IsUndefined());
160 m_resolver->resolve(ScriptValue(v8::Integer::New(m_isolate, 3), m_isolate));
162 EXPECT_FALSE(m_resolver->isPending());
163 EXPECT_EQ(V8PromiseCustom::Fulfilled, state());
164 ASSERT_TRUE(result()->IsNumber());
165 EXPECT_EQ(3, result().As<v8::Integer>()->Value());
167 m_resolver->reject(ScriptValue(v8::Integer::New(m_isolate, 4), m_isolate));
168 EXPECT_FALSE(m_resolver->isPending());
169 EXPECT_EQ(V8PromiseCustom::Fulfilled, state());
170 ASSERT_TRUE(result()->IsNumber());
171 EXPECT_EQ(3, result().As<v8::Integer>()->Value());
176 } // namespace WebCore