Upstream version 5.34.104.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / bindings / v8 / ScriptPromiseResolverTest.cpp
1 /*
2  * Copyright (C) 2013 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/ScriptPromiseResolver.h"
33
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"
38
39 #include <gtest/gtest.h>
40 #include <v8.h>
41
42 namespace WebCore {
43
44 namespace {
45
46 class ScriptPromiseResolverTest : public testing::Test {
47 public:
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))
53     {
54     }
55
56     void SetUp()
57     {
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);
64     }
65
66     void TearDown()
67     {
68         m_resolver = 0;
69         m_promise.clear();
70         m_perContextData.clear();
71     }
72
73     V8PromiseCustom::PromiseState state()
74     {
75         return V8PromiseCustom::getState(V8PromiseCustom::getInternal(promise()));
76     }
77
78     v8::Local<v8::Value> result()
79     {
80         return V8PromiseCustom::getInternal(promise())->GetInternalField(V8PromiseCustom::InternalResultIndex);
81     }
82
83     v8::Local<v8::Object> promise()
84     {
85         ASSERT(!m_promise.hasNoValue());
86         return m_promise.v8Value().As<v8::Object>();
87     }
88
89 protected:
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;
97 };
98
99 TEST_F(ScriptPromiseResolverTest, initialState)
100 {
101     EXPECT_TRUE(m_resolver->isPending());
102     EXPECT_EQ(V8PromiseCustom::Pending, state());
103     EXPECT_TRUE(result()->IsUndefined());
104 }
105
106 TEST_F(ScriptPromiseResolverTest, resolve)
107 {
108     EXPECT_TRUE(m_resolver->isPending());
109     EXPECT_EQ(V8PromiseCustom::Pending, state());
110     EXPECT_TRUE(result()->IsUndefined());
111
112     m_resolver->resolve(ScriptValue(v8::Integer::New(m_isolate, 3), m_isolate));
113
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());
118 }
119
120 TEST_F(ScriptPromiseResolverTest, reject)
121 {
122     EXPECT_TRUE(m_resolver->isPending());
123     EXPECT_EQ(V8PromiseCustom::Pending, state());
124     EXPECT_TRUE(result()->IsUndefined());
125
126     m_resolver->reject(ScriptValue(v8::Integer::New(m_isolate, 3), m_isolate));
127
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());
132 }
133
134 TEST_F(ScriptPromiseResolverTest, resolveOverResolve)
135 {
136     EXPECT_TRUE(m_resolver->isPending());
137     EXPECT_EQ(V8PromiseCustom::Pending, state());
138     EXPECT_TRUE(result()->IsUndefined());
139
140     m_resolver->resolve(ScriptValue(v8::Integer::New(m_isolate, 3), m_isolate));
141
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());
146
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());
152 }
153
154 TEST_F(ScriptPromiseResolverTest, rejectOverResolve)
155 {
156     EXPECT_TRUE(m_resolver->isPending());
157     EXPECT_EQ(V8PromiseCustom::Pending, state());
158     EXPECT_TRUE(result()->IsUndefined());
159
160     m_resolver->resolve(ScriptValue(v8::Integer::New(m_isolate, 3), m_isolate));
161
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());
166
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());
172 }
173
174 } // namespace
175
176 } // namespace WebCore