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/ScriptPromise.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 ScriptPromiseResolverTest : public testing::Test { 46 public: 47 ScriptPromiseResolverTest() 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 m_promise = ScriptPromise::createPending(); 62 m_resolver = ScriptPromiseResolver::create(m_promise); 63 } 64 65 void TearDown() 66 { 67 m_resolver = 0; 68 m_promise.clear(); 69 m_perContextData.clear(); 70 } 71 72 V8PromiseCustom::PromiseState state() 73 { 74 return V8PromiseCustom::getState(V8PromiseCustom::getInternal(promise())); 75 } 76 77 v8::Local<v8::Value> result() 78 { 79 return V8PromiseCustom::getInternal(promise())->GetInternalField(V8PromiseCustom::InternalResultIndex); 80 } 81 82 v8::Local<v8::Object> promise() 83 { 84 ASSERT(!m_promise.hasNoValue()); 85 return m_promise.v8Value().As<v8::Object>(); 86 } 87 88 protected: 89 v8::Isolate* m_isolate; 90 v8::HandleScope m_handleScope; 91 ScopedPersistent<v8::Context> m_context; 92 v8::Context::Scope m_contextScope; 93 RefPtr<ScriptPromiseResolver> m_resolver; 94 ScriptPromise m_promise; 95 OwnPtr<V8PerContextData> m_perContextData; 96 }; 97 98 TEST_F(ScriptPromiseResolverTest, initialState) 99 { 100 EXPECT_TRUE(m_resolver->isPending()); 101 EXPECT_EQ(V8PromiseCustom::Pending, state()); 102 EXPECT_TRUE(result()->IsUndefined()); 103 } 104 105 TEST_F(ScriptPromiseResolverTest, resolve) 106 { 107 EXPECT_TRUE(m_resolver->isPending()); 108 EXPECT_EQ(V8PromiseCustom::Pending, state()); 109 EXPECT_TRUE(result()->IsUndefined()); 110 111 m_resolver->resolve(ScriptValue(v8::Integer::New(3, m_isolate), m_isolate)); 112 113 EXPECT_FALSE(m_resolver->isPending()); 114 EXPECT_EQ(V8PromiseCustom::Fulfilled, state()); 115 ASSERT_TRUE(result()->IsNumber()); 116 EXPECT_EQ(3, result().As<v8::Integer>()->Value()); 117 } 118 119 TEST_F(ScriptPromiseResolverTest, reject) 120 { 121 EXPECT_TRUE(m_resolver->isPending()); 122 EXPECT_EQ(V8PromiseCustom::Pending, state()); 123 EXPECT_TRUE(result()->IsUndefined()); 124 125 m_resolver->reject(ScriptValue(v8::Integer::New(3, m_isolate), m_isolate)); 126 127 EXPECT_FALSE(m_resolver->isPending()); 128 EXPECT_EQ(V8PromiseCustom::Rejected, state()); 129 ASSERT_TRUE(result()->IsNumber()); 130 EXPECT_EQ(3, result().As<v8::Integer>()->Value()); 131 } 132 133 TEST_F(ScriptPromiseResolverTest, resolveOverResolve) 134 { 135 EXPECT_TRUE(m_resolver->isPending()); 136 EXPECT_EQ(V8PromiseCustom::Pending, state()); 137 EXPECT_TRUE(result()->IsUndefined()); 138 139 m_resolver->resolve(ScriptValue(v8::Integer::New(3, m_isolate), m_isolate)); 140 141 EXPECT_FALSE(m_resolver->isPending()); 142 EXPECT_EQ(V8PromiseCustom::Fulfilled, state()); 143 ASSERT_TRUE(result()->IsNumber()); 144 EXPECT_EQ(3, result().As<v8::Integer>()->Value()); 145 146 m_resolver->resolve(ScriptValue(v8::Integer::New(4, m_isolate), m_isolate)); 147 EXPECT_FALSE(m_resolver->isPending()); 148 EXPECT_EQ(V8PromiseCustom::Fulfilled, state()); 149 ASSERT_TRUE(result()->IsNumber()); 150 EXPECT_EQ(3, result().As<v8::Integer>()->Value()); 151 } 152 153 TEST_F(ScriptPromiseResolverTest, rejectOverResolve) 154 { 155 EXPECT_TRUE(m_resolver->isPending()); 156 EXPECT_EQ(V8PromiseCustom::Pending, state()); 157 EXPECT_TRUE(result()->IsUndefined()); 158 159 m_resolver->resolve(ScriptValue(v8::Integer::New(3, m_isolate), m_isolate)); 160 161 EXPECT_FALSE(m_resolver->isPending()); 162 EXPECT_EQ(V8PromiseCustom::Fulfilled, state()); 163 ASSERT_TRUE(result()->IsNumber()); 164 EXPECT_EQ(3, result().As<v8::Integer>()->Value()); 165 166 m_resolver->reject(ScriptValue(v8::Integer::New(4, m_isolate), m_isolate)); 167 EXPECT_FALSE(m_resolver->isPending()); 168 EXPECT_EQ(V8PromiseCustom::Fulfilled, state()); 169 ASSERT_TRUE(result()->IsNumber()); 170 EXPECT_EQ(3, result().As<v8::Integer>()->Value()); 171 } 172 173 } // namespace 174 175 } // namespace WebCore 176