Home | History | Annotate | Download | only in v8
      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/V8Binding.h"
     35 #include "bindings/v8/custom/V8PromiseCustom.h"
     36 
     37 #include <gtest/gtest.h>
     38 #include <v8.h>
     39 
     40 namespace WebCore {
     41 
     42 namespace {
     43 
     44 class ScriptPromiseResolverTest : public testing::Test {
     45 public:
     46     ScriptPromiseResolverTest()
     47         : m_isolate(v8::Isolate::GetCurrent())
     48         , m_handleScope(m_isolate)
     49         , m_context(v8::Context::New(m_isolate))
     50         , m_contextScope(m_context.newLocal(m_isolate))
     51     {
     52     }
     53 
     54     void SetUp()
     55     {
     56         m_perContextData = V8PerContextData::create(m_context.newLocal(m_isolate));
     57         m_perContextData->init();
     58         m_resolver = ScriptPromiseResolver::create();
     59         m_promise = m_resolver->promise();
     60     }
     61 
     62     void TearDown()
     63     {
     64         m_resolver = 0;
     65         m_promise.clear();
     66         m_perContextData.clear();
     67     }
     68 
     69     V8PromiseCustom::PromiseState state()
     70     {
     71         return V8PromiseCustom::getState(V8PromiseCustom::getInternal(promise()));
     72     }
     73 
     74     v8::Local<v8::Value> result()
     75     {
     76         return V8PromiseCustom::getInternal(promise())->GetInternalField(V8PromiseCustom::InternalResultIndex);
     77     }
     78 
     79     v8::Local<v8::Object> promise()
     80     {
     81         ASSERT(!m_promise.hasNoValue());
     82         return m_promise.v8Object();
     83     }
     84 
     85 protected:
     86     v8::Isolate* m_isolate;
     87     v8::HandleScope m_handleScope;
     88     ScopedPersistent<v8::Context> m_context;
     89     v8::Context::Scope m_contextScope;
     90     RefPtr<ScriptPromiseResolver> m_resolver;
     91     ScriptObject m_promise;
     92     OwnPtr<V8PerContextData> m_perContextData;
     93 };
     94 
     95 TEST_F(ScriptPromiseResolverTest, initialState)
     96 {
     97     EXPECT_TRUE(m_resolver->isPending());
     98     EXPECT_EQ(V8PromiseCustom::Pending, state());
     99     EXPECT_TRUE(result()->IsUndefined());
    100 }
    101 
    102 TEST_F(ScriptPromiseResolverTest, fulfill)
    103 {
    104     EXPECT_TRUE(m_resolver->isPending());
    105     EXPECT_EQ(V8PromiseCustom::Pending, state());
    106     EXPECT_TRUE(result()->IsUndefined());
    107 
    108     m_resolver->fulfill(ScriptValue(v8::Integer::New(3)));
    109 
    110     EXPECT_FALSE(m_resolver->isPending());
    111     EXPECT_EQ(V8PromiseCustom::Fulfilled, state());
    112     ASSERT_TRUE(result()->IsNumber());
    113     EXPECT_EQ(3, result().As<v8::Integer>()->Value());
    114 }
    115 
    116 TEST_F(ScriptPromiseResolverTest, resolve)
    117 {
    118     EXPECT_TRUE(m_resolver->isPending());
    119     EXPECT_EQ(V8PromiseCustom::Pending, state());
    120     EXPECT_TRUE(result()->IsUndefined());
    121 
    122     m_resolver->resolve(ScriptValue(v8::Integer::New(3)));
    123 
    124     EXPECT_FALSE(m_resolver->isPending());
    125     EXPECT_EQ(V8PromiseCustom::Fulfilled, state());
    126     ASSERT_TRUE(result()->IsNumber());
    127     EXPECT_EQ(3, result().As<v8::Integer>()->Value());
    128 }
    129 
    130 TEST_F(ScriptPromiseResolverTest, reject)
    131 {
    132     EXPECT_TRUE(m_resolver->isPending());
    133     EXPECT_EQ(V8PromiseCustom::Pending, state());
    134     EXPECT_TRUE(result()->IsUndefined());
    135 
    136     m_resolver->reject(ScriptValue(v8::Integer::New(3)));
    137 
    138     EXPECT_FALSE(m_resolver->isPending());
    139     EXPECT_EQ(V8PromiseCustom::Rejected, state());
    140     ASSERT_TRUE(result()->IsNumber());
    141     EXPECT_EQ(3, result().As<v8::Integer>()->Value());
    142 }
    143 
    144 TEST_F(ScriptPromiseResolverTest, fulfillOverFulfill)
    145 {
    146     EXPECT_TRUE(m_resolver->isPending());
    147     EXPECT_EQ(V8PromiseCustom::Pending, state());
    148     EXPECT_TRUE(result()->IsUndefined());
    149 
    150     m_resolver->fulfill(ScriptValue(v8::Integer::New(3)));
    151 
    152     EXPECT_FALSE(m_resolver->isPending());
    153     EXPECT_EQ(V8PromiseCustom::Fulfilled, state());
    154     ASSERT_TRUE(result()->IsNumber());
    155     EXPECT_EQ(3, result().As<v8::Integer>()->Value());
    156 
    157     m_resolver->fulfill(ScriptValue(v8::Integer::New(4)));
    158     EXPECT_FALSE(m_resolver->isPending());
    159     EXPECT_EQ(V8PromiseCustom::Fulfilled, state());
    160     ASSERT_TRUE(result()->IsNumber());
    161     EXPECT_EQ(3, result().As<v8::Integer>()->Value());
    162 }
    163 
    164 TEST_F(ScriptPromiseResolverTest, rejectOverFulfill)
    165 {
    166     EXPECT_TRUE(m_resolver->isPending());
    167     EXPECT_EQ(V8PromiseCustom::Pending, state());
    168     EXPECT_TRUE(result()->IsUndefined());
    169 
    170     m_resolver->fulfill(ScriptValue(v8::Integer::New(3)));
    171 
    172     EXPECT_FALSE(m_resolver->isPending());
    173     EXPECT_EQ(V8PromiseCustom::Fulfilled, state());
    174     ASSERT_TRUE(result()->IsNumber());
    175     EXPECT_EQ(3, result().As<v8::Integer>()->Value());
    176 
    177     m_resolver->reject(ScriptValue(v8::Integer::New(4)));
    178     EXPECT_FALSE(m_resolver->isPending());
    179     EXPECT_EQ(V8PromiseCustom::Fulfilled, state());
    180     ASSERT_TRUE(result()->IsNumber());
    181     EXPECT_EQ(3, result().As<v8::Integer>()->Value());
    182 }
    183 
    184 TEST_F(ScriptPromiseResolverTest, detach)
    185 {
    186     EXPECT_TRUE(m_resolver->isPending());
    187     EXPECT_EQ(V8PromiseCustom::Pending, state());
    188     EXPECT_TRUE(result()->IsUndefined());
    189 
    190     m_resolver->detach();
    191 
    192     EXPECT_FALSE(m_resolver->isPending());
    193     EXPECT_EQ(V8PromiseCustom::Rejected, state());
    194     EXPECT_TRUE(result()->IsUndefined());
    195 }
    196 
    197 } // namespace
    198 
    199 } // namespace WebCore
    200