Upstream version 7.36.149.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 "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"
39
40 #include <gtest/gtest.h>
41 #include <v8.h>
42
43 namespace WebCore {
44
45 namespace {
46
47 class ScriptPromiseResolverTest : public testing::Test {
48 public:
49     ScriptPromiseResolverTest()
50         : m_isolate(v8::Isolate::GetCurrent())
51     {
52     }
53
54     void SetUp()
55     {
56         m_scope = V8ExecutionScope::create(m_isolate);
57         m_resolver = ScriptPromiseResolver::create(m_scope->scriptState());
58         m_promise = m_resolver->promise();
59     }
60
61     void TearDown()
62     {
63         m_resolver = nullptr;
64         m_promise.clear();
65         m_scope.clear();
66     }
67
68     V8PromiseCustom::PromiseState state()
69     {
70         return V8PromiseCustom::getState(V8PromiseCustom::getInternal(promise()));
71     }
72
73     v8::Local<v8::Value> result()
74     {
75         return V8PromiseCustom::getInternal(promise())->GetInternalField(V8PromiseCustom::InternalResultIndex);
76     }
77
78     v8::Local<v8::Object> promise()
79     {
80         ASSERT(!m_promise.isEmpty());
81         return m_promise.v8Value().As<v8::Object>();
82     }
83
84 protected:
85     v8::Isolate* m_isolate;
86     RefPtr<ScriptPromiseResolver> m_resolver;
87     ScriptPromise m_promise;
88 private:
89     OwnPtr<V8ExecutionScope> m_scope;
90 };
91
92 TEST_F(ScriptPromiseResolverTest, initialState)
93 {
94     if (RuntimeEnabledFeatures::scriptPromiseOnV8PromiseEnabled())
95         return;
96     EXPECT_EQ(V8PromiseCustom::Pending, state());
97     EXPECT_TRUE(result()->IsUndefined());
98 }
99
100 TEST_F(ScriptPromiseResolverTest, resolve)
101 {
102     if (RuntimeEnabledFeatures::scriptPromiseOnV8PromiseEnabled())
103         return;
104     EXPECT_EQ(V8PromiseCustom::Pending, state());
105     EXPECT_TRUE(result()->IsUndefined());
106
107     m_resolver->resolve(ScriptValue(ScriptState::current(m_isolate), v8::Integer::New(m_isolate, 3)));
108
109     EXPECT_EQ(V8PromiseCustom::Fulfilled, state());
110     ASSERT_TRUE(result()->IsNumber());
111     EXPECT_EQ(3, result().As<v8::Integer>()->Value());
112 }
113
114 TEST_F(ScriptPromiseResolverTest, reject)
115 {
116     if (RuntimeEnabledFeatures::scriptPromiseOnV8PromiseEnabled())
117         return;
118     EXPECT_EQ(V8PromiseCustom::Pending, state());
119     EXPECT_TRUE(result()->IsUndefined());
120
121     m_resolver->reject(ScriptValue(ScriptState::current(m_isolate), v8::Integer::New(m_isolate, 3)));
122
123     EXPECT_EQ(V8PromiseCustom::Rejected, state());
124     ASSERT_TRUE(result()->IsNumber());
125     EXPECT_EQ(3, result().As<v8::Integer>()->Value());
126 }
127
128 TEST_F(ScriptPromiseResolverTest, resolveOverResolve)
129 {
130     if (RuntimeEnabledFeatures::scriptPromiseOnV8PromiseEnabled())
131         return;
132     EXPECT_EQ(V8PromiseCustom::Pending, state());
133     EXPECT_TRUE(result()->IsUndefined());
134
135     m_resolver->resolve(ScriptValue(ScriptState::current(m_isolate), v8::Integer::New(m_isolate, 3)));
136
137     EXPECT_EQ(V8PromiseCustom::Fulfilled, state());
138     ASSERT_TRUE(result()->IsNumber());
139     EXPECT_EQ(3, result().As<v8::Integer>()->Value());
140
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());
145 }
146
147 TEST_F(ScriptPromiseResolverTest, rejectOverResolve)
148 {
149     if (RuntimeEnabledFeatures::scriptPromiseOnV8PromiseEnabled())
150         return;
151     EXPECT_EQ(V8PromiseCustom::Pending, state());
152     EXPECT_TRUE(result()->IsUndefined());
153
154     m_resolver->resolve(ScriptValue(ScriptState::current(m_isolate), v8::Integer::New(m_isolate, 3)));
155
156     EXPECT_EQ(V8PromiseCustom::Fulfilled, state());
157     ASSERT_TRUE(result()->IsNumber());
158     EXPECT_EQ(3, result().As<v8::Integer>()->Value());
159
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());
164 }
165
166 } // namespace
167
168 } // namespace WebCore