OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2014 Google Inc. All rights reserved. | 2 * Copyright (C) 2014 Google Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions are | 5 * modification, are permitted provided that the following conditions are |
6 * met: | 6 * met: |
7 * | 7 * |
8 * * Redistributions of source code must retain the above copyright | 8 * * Redistributions of source code must retain the above copyright |
9 * notice, this list of conditions and the following disclaimer. | 9 * notice, this list of conditions and the following disclaimer. |
10 * * Redistributions in binary form must reproduce the above | 10 * * Redistributions in binary form must reproduce the above |
(...skipping 25 matching lines...) Expand all Loading... |
36 #include "bindings/v8/V8Binding.h" | 36 #include "bindings/v8/V8Binding.h" |
37 #include "bindings/v8/custom/V8PromiseCustom.h" | 37 #include "bindings/v8/custom/V8PromiseCustom.h" |
38 | 38 |
39 #include <gtest/gtest.h> | 39 #include <gtest/gtest.h> |
40 #include <v8.h> | 40 #include <v8.h> |
41 | 41 |
42 namespace WebCore { | 42 namespace WebCore { |
43 | 43 |
44 namespace { | 44 namespace { |
45 | 45 |
| 46 void callback(const v8::FunctionCallbackInfo<v8::Value>& info) { } |
| 47 |
46 class ScriptPromiseTest : public testing::Test { | 48 class ScriptPromiseTest : public testing::Test { |
47 public: | 49 public: |
48 ScriptPromiseTest() | 50 ScriptPromiseTest() |
49 : m_isolate(v8::Isolate::GetCurrent()) | 51 : m_isolate(v8::Isolate::GetCurrent()) |
50 , m_handleScope(m_isolate) | 52 , m_handleScope(m_isolate) |
51 , m_context(m_isolate, v8::Context::New(m_isolate)) | 53 , m_context(m_isolate, v8::Context::New(m_isolate)) |
52 , m_contextScope(m_context.newLocal(m_isolate)) | 54 , m_contextScope(m_context.newLocal(m_isolate)) |
53 { | 55 { |
54 } | |
55 | |
56 void SetUp() | |
57 { | |
58 v8::Handle<v8::Context> context(m_context.newLocal(m_isolate)); | 56 v8::Handle<v8::Context> context(m_context.newLocal(m_isolate)); |
59 V8PerContextDataHolder::install(context, DOMWrapperWorld::current(m_isol
ate)); | 57 V8PerContextDataHolder::install(context, DOMWrapperWorld::current(m_isol
ate)); |
60 m_perContextData = V8PerContextData::create(context); | 58 m_perContextData = V8PerContextData::create(context); |
61 m_perContextData->init(); | 59 m_perContextData->init(); |
62 } | 60 } |
63 | 61 |
64 void TearDown() | 62 ~ScriptPromiseTest() |
65 { | 63 { |
66 m_perContextData.clear(); | 64 // FIXME: We put this statement here to clear an exception from the isol
ate. |
| 65 createClosure(callback, v8::Undefined(m_isolate), m_isolate); |
67 } | 66 } |
68 | 67 |
69 V8PromiseCustom::PromiseState state(ScriptPromise promise) | 68 V8PromiseCustom::PromiseState state(ScriptPromise promise) |
70 { | 69 { |
71 return V8PromiseCustom::getState(V8PromiseCustom::getInternal(promise.v8
Value().As<v8::Object>())); | 70 return V8PromiseCustom::getState(V8PromiseCustom::getInternal(promise.v8
Value().As<v8::Object>())); |
72 } | 71 } |
73 | 72 |
74 protected: | 73 protected: |
75 v8::Isolate* m_isolate; | 74 v8::Isolate* m_isolate; |
76 v8::HandleScope m_handleScope; | 75 v8::HandleScope m_handleScope; |
77 ScopedPersistent<v8::Context> m_context; | 76 ScopedPersistent<v8::Context> m_context; |
78 v8::Context::Scope m_contextScope; | 77 v8::Context::Scope m_contextScope; |
79 OwnPtr<V8PerContextData> m_perContextData; | 78 OwnPtr<V8PerContextData> m_perContextData; |
80 }; | 79 }; |
81 | 80 |
| 81 TEST_F(ScriptPromiseTest, constructFromNonPromise) |
| 82 { |
| 83 v8::TryCatch trycatch; |
| 84 ScriptPromise promise(v8::Undefined(m_isolate), m_isolate); |
| 85 ASSERT_TRUE(trycatch.HasCaught()); |
| 86 ASSERT_TRUE(promise.hasNoValue()); |
| 87 } |
| 88 |
82 TEST_F(ScriptPromiseTest, castPromise) | 89 TEST_F(ScriptPromiseTest, castPromise) |
83 { | 90 { |
84 ScriptPromise promise = ScriptPromise::createPending(); | 91 ScriptPromise promise = ScriptPromise::createPending(); |
85 ScriptPromise newPromise(ScriptValue(promise.v8Value(), m_isolate)); | 92 ScriptPromise newPromise = ScriptPromise::cast(ScriptValue(promise.v8Value()
, m_isolate)); |
86 | 93 |
87 ASSERT_FALSE(promise.hasNoValue()); | 94 ASSERT_FALSE(promise.hasNoValue()); |
88 EXPECT_EQ(V8PromiseCustom::Pending, state(promise)); | 95 EXPECT_EQ(V8PromiseCustom::Pending, state(promise)); |
89 EXPECT_EQ(promise.v8Value(), newPromise.v8Value()); | 96 EXPECT_EQ(promise.v8Value(), newPromise.v8Value()); |
90 } | 97 } |
91 | 98 |
92 TEST_F(ScriptPromiseTest, castNonPromise) | 99 TEST_F(ScriptPromiseTest, castNonPromise) |
93 { | 100 { |
94 ScriptValue value = ScriptValue(v8String(m_isolate, "hello"), m_isolate); | 101 ScriptValue value = ScriptValue(v8String(m_isolate, "hello"), m_isolate); |
95 ScriptPromise promise1(ScriptValue(value.v8Value(), m_isolate)); | 102 ScriptPromise promise1 = ScriptPromise::cast(ScriptValue(value.v8Value(), m_
isolate)); |
96 ScriptPromise promise2(ScriptValue(value.v8Value(), m_isolate)); | 103 ScriptPromise promise2 = ScriptPromise::cast(ScriptValue(value.v8Value(), m_
isolate)); |
97 | 104 |
98 ASSERT_FALSE(promise1.hasNoValue()); | 105 ASSERT_FALSE(promise1.hasNoValue()); |
99 ASSERT_FALSE(promise2.hasNoValue()); | 106 ASSERT_FALSE(promise2.hasNoValue()); |
100 | 107 |
101 ASSERT_TRUE(V8PromiseCustom::isPromise(promise1.v8Value(), m_isolate)); | 108 ASSERT_TRUE(V8PromiseCustom::isPromise(promise1.v8Value(), m_isolate)); |
102 ASSERT_TRUE(V8PromiseCustom::isPromise(promise2.v8Value(), m_isolate)); | 109 ASSERT_TRUE(V8PromiseCustom::isPromise(promise2.v8Value(), m_isolate)); |
103 | 110 |
104 EXPECT_EQ(V8PromiseCustom::Fulfilled, state(promise1)); | 111 EXPECT_EQ(V8PromiseCustom::Fulfilled, state(promise1)); |
105 EXPECT_EQ(V8PromiseCustom::Fulfilled, state(promise2)); | 112 EXPECT_EQ(V8PromiseCustom::Fulfilled, state(promise2)); |
106 EXPECT_NE(promise1.v8Value(), promise2.v8Value()); | 113 EXPECT_NE(promise1.v8Value(), promise2.v8Value()); |
107 } | 114 } |
108 | 115 |
109 } // namespace | 116 } // namespace |
110 | 117 |
111 } // namespace WebCore | 118 } // namespace WebCore |
OLD | NEW |