Upstream version 7.35.139.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / bindings / v8 / ScriptPromiseTest.cpp
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/DOMWrapperWorld.h"
35 #include "bindings/v8/ScriptPromiseResolver.h"
36 #include "bindings/v8/ScriptValue.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 void callback(const v8::FunctionCallbackInfo<v8::Value>& info) { }
48
49 class ScriptPromiseTest : public testing::Test {
50 public:
51     ScriptPromiseTest()
52         : m_isolate(v8::Isolate::GetCurrent())
53     {
54         m_scope = V8ExecutionScope::create(m_isolate);
55     }
56
57     ~ScriptPromiseTest()
58     {
59         // FIXME: We put this statement here to clear an exception from the isolate.
60         createClosure(callback, v8::Undefined(m_isolate), m_isolate);
61     }
62
63     V8PromiseCustom::PromiseState state(ScriptPromise promise)
64     {
65         return V8PromiseCustom::getState(V8PromiseCustom::getInternal(promise.v8Value().As<v8::Object>()));
66     }
67
68 protected:
69     v8::Isolate* m_isolate;
70
71 private:
72     OwnPtr<V8ExecutionScope> m_scope;
73 };
74
75 TEST_F(ScriptPromiseTest, constructFromNonPromise)
76 {
77     v8::TryCatch trycatch;
78     ScriptPromise promise(v8::Undefined(m_isolate), m_isolate);
79     ASSERT_TRUE(trycatch.HasCaught());
80     ASSERT_TRUE(promise.hasNoValue());
81 }
82
83 TEST_F(ScriptPromiseTest, castPromise)
84 {
85     ScriptPromise promise = ScriptPromiseResolver::create(m_isolate)->promise();
86     ScriptPromise newPromise = ScriptPromise::cast(ScriptValue(promise.v8Value(), m_isolate));
87
88     ASSERT_FALSE(promise.hasNoValue());
89     EXPECT_EQ(V8PromiseCustom::Pending, state(promise));
90     EXPECT_EQ(promise.v8Value(), newPromise.v8Value());
91 }
92
93 TEST_F(ScriptPromiseTest, castNonPromise)
94 {
95     ScriptValue value = ScriptValue(v8String(m_isolate, "hello"), m_isolate);
96     ScriptPromise promise1 = ScriptPromise::cast(ScriptValue(value.v8Value(), m_isolate));
97     ScriptPromise promise2 = ScriptPromise::cast(ScriptValue(value.v8Value(), m_isolate));
98
99     ASSERT_FALSE(promise1.hasNoValue());
100     ASSERT_FALSE(promise2.hasNoValue());
101
102     ASSERT_TRUE(V8PromiseCustom::isPromise(promise1.v8Value(), m_isolate));
103     ASSERT_TRUE(V8PromiseCustom::isPromise(promise2.v8Value(), m_isolate));
104
105     EXPECT_EQ(V8PromiseCustom::Fulfilled, state(promise1));
106     EXPECT_EQ(V8PromiseCustom::Fulfilled, state(promise2));
107     EXPECT_NE(promise1.v8Value(), promise2.v8Value());
108 }
109
110 } // namespace
111
112 } // namespace WebCore