Upstream version 5.34.98.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/ScriptValue.h"
35 #include "bindings/v8/V8Binding.h"
36 #include "bindings/v8/custom/V8PromiseCustom.h"
37
38 #include <gtest/gtest.h>
39 #include <v8.h>
40
41 namespace WebCore {
42
43 namespace {
44
45 class ScriptPromiseTest : public testing::Test {
46 public:
47     ScriptPromiseTest()
48         : m_isolate(v8::Isolate::GetCurrent())
49         , m_handleScope(m_isolate)
50         , m_context(m_isolate, v8::Context::New(m_isolate))
51         , m_contextScope(m_context.newLocal(m_isolate))
52     {
53     }
54
55     void SetUp()
56     {
57         v8::Handle<v8::Context> context(m_context.newLocal(m_isolate));
58         V8PerContextDataHolder::install(context);
59         m_perContextData = V8PerContextData::create(context);
60         m_perContextData->init();
61     }
62
63     void TearDown()
64     {
65         m_perContextData.clear();
66     }
67
68     V8PromiseCustom::PromiseState state(ScriptPromise promise)
69     {
70         return V8PromiseCustom::getState(V8PromiseCustom::getInternal(promise.v8Value().As<v8::Object>()));
71     }
72
73 protected:
74     v8::Isolate* m_isolate;
75     v8::HandleScope m_handleScope;
76     ScopedPersistent<v8::Context> m_context;
77     v8::Context::Scope m_contextScope;
78     OwnPtr<V8PerContextData> m_perContextData;
79 };
80
81 TEST_F(ScriptPromiseTest, castPromise)
82 {
83     ScriptPromise promise = ScriptPromise::createPending();
84     ScriptPromise newPromise(ScriptValue(promise.v8Value(), m_isolate));
85
86     ASSERT_FALSE(promise.hasNoValue());
87     EXPECT_EQ(V8PromiseCustom::Pending, state(promise));
88     EXPECT_EQ(promise.v8Value(), newPromise.v8Value());
89 }
90
91 TEST_F(ScriptPromiseTest, castNonPromise)
92 {
93     ScriptValue value = ScriptValue(v8String(m_isolate, "hello"), m_isolate);
94     ScriptPromise promise1(ScriptValue(value.v8Value(), m_isolate));
95     ScriptPromise promise2(ScriptValue(value.v8Value(), m_isolate));
96
97     ASSERT_FALSE(promise1.hasNoValue());
98     ASSERT_FALSE(promise2.hasNoValue());
99
100     ASSERT_TRUE(V8PromiseCustom::isPromise(promise1.v8Value(), m_isolate));
101     ASSERT_TRUE(V8PromiseCustom::isPromise(promise2.v8Value(), m_isolate));
102
103     EXPECT_EQ(V8PromiseCustom::Fulfilled, state(promise1));
104     EXPECT_EQ(V8PromiseCustom::Fulfilled, state(promise2));
105     EXPECT_NE(promise1.v8Value(), promise2.v8Value());
106 }
107
108 } // namespace
109
110 } // namespace WebCore