Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "config.h" | |
| 6 #include "bindings/v8/ScriptPromiseProperty.h" | |
| 7 | |
| 8 #include "bindings/v8/DOMWrapperWorld.h" | |
| 9 #include "bindings/v8/ScriptFunction.h" | |
| 10 #include "bindings/v8/ScriptPromise.h" | |
| 11 #include "bindings/v8/ScriptState.h" | |
| 12 #include "bindings/v8/ScriptValue.h" | |
| 13 #include "bindings/v8/V8Binding.h" | |
| 14 #include "core/dom/Document.h" | |
| 15 #include "core/events/Event.h" | |
| 16 #include "core/testing/DummyPageHolder.h" | |
| 17 #include "core/testing/GCObservation.h" | |
| 18 #include "wtf/OwnPtr.h" | |
| 19 #include "wtf/PassOwnPtr.h" | |
| 20 #include "wtf/PassRefPtr.h" | |
| 21 #include "wtf/RefPtr.h" | |
| 22 #include <gtest/gtest.h> | |
| 23 #include <v8.h> | |
| 24 | |
| 25 using namespace WebCore; | |
| 26 | |
| 27 namespace { | |
| 28 | |
| 29 class NotReached : public ScriptFunction { | |
| 30 public: | |
| 31 NotReached() : ScriptFunction(v8::Isolate::GetCurrent()) { } | |
| 32 | |
| 33 private: | |
| 34 virtual ScriptValue call(ScriptValue) OVERRIDE; | |
| 35 }; | |
| 36 | |
| 37 ScriptValue NotReached::call(ScriptValue) | |
| 38 { | |
| 39 EXPECT_TRUE(false) << "'Unreachable' code was reached"; | |
| 40 return ScriptValue(); | |
| 41 } | |
| 42 | |
| 43 class StubFunction : public ScriptFunction { | |
| 44 public: | |
| 45 StubFunction(ScriptValue&, size_t& callCount); | |
| 46 | |
| 47 private: | |
| 48 virtual ScriptValue call(ScriptValue) OVERRIDE; | |
| 49 | |
| 50 ScriptValue& m_value; | |
| 51 size_t& m_callCount; | |
| 52 }; | |
| 53 | |
| 54 StubFunction::StubFunction(ScriptValue& value, size_t& callCount) | |
| 55 : ScriptFunction(v8::Isolate::GetCurrent()) | |
| 56 , m_value(value) | |
| 57 , m_callCount(callCount) | |
| 58 { | |
| 59 } | |
| 60 | |
| 61 ScriptValue StubFunction::call(ScriptValue arg) | |
| 62 { | |
| 63 m_value = arg; | |
| 64 m_callCount++; | |
| 65 return ScriptValue(); | |
| 66 } | |
| 67 | |
| 68 class ScriptPromisePropertyTest : public ::testing::Test { | |
| 69 protected: | |
| 70 ScriptPromisePropertyTest() | |
| 71 : m_page(DummyPageHolder::create(IntSize(1, 1))) | |
| 72 { | |
| 73 } | |
| 74 | |
| 75 virtual ~ScriptPromisePropertyTest() | |
| 76 { | |
| 77 m_page.clear(); | |
| 78 gc(); | |
| 79 } | |
| 80 | |
| 81 Document& document() { return m_page->document(); } | |
| 82 v8::Isolate* isolate() { return toIsolate(&document()); } | |
| 83 ScriptState* scriptState() { return ScriptState::forMainWorld(document().fra me()); } | |
| 84 void gc() { v8::Isolate::GetCurrent()->RequestGarbageCollectionForTesting(v8 ::Isolate::kFullGarbageCollection); } | |
| 85 | |
| 86 PassOwnPtr<ScriptFunction> notReached() { return adoptPtr(new NotReached()); } | |
| 87 PassOwnPtr<ScriptFunction> stub(ScriptValue& value, size_t& callCount) { ret urn adoptPtr(new StubFunction(value, callCount)); } | |
| 88 | |
| 89 // These tests use Event because it is simple to manufacture lots of events. | |
| 90 | |
| 91 ScriptValue wrap(PassRefPtrWillBeRawPtr<Event> event) | |
| 92 { | |
| 93 ScriptState::Scope scope(scriptState()); | |
| 94 return ScriptValue(scriptState(), V8ValueTraits<Event>::toV8Value(event, scriptState()->context()->Global(), isolate())); | |
| 95 } | |
| 96 | |
| 97 typedef ScriptPromiseProperty<RefPtrWillBeMember<Event>, Event*, Event*> Pro perty; | |
| 98 PassRefPtrWillBeRawPtr<Property> newProperty() { return Property::create(&do cument(), Event::create(), "test"); } | |
| 99 | |
| 100 private: | |
| 101 OwnPtr<DummyPageHolder> m_page; | |
| 102 }; | |
| 103 | |
| 104 TEST_F(ScriptPromisePropertyTest, Promise_IsStableObject) | |
| 105 { | |
| 106 RefPtrWillBeRawPtr<Property> p(newProperty()); | |
| 107 ScriptPromise v = p->promise(DOMWrapperWorld::mainWorld()); | |
| 108 ScriptPromise w = p->promise(DOMWrapperWorld::mainWorld()); | |
| 109 EXPECT_EQ(v, w); | |
|
yhirano
2014/07/01 04:38:14
EXPECT_FALSE(v.isEmpty())
yhirano
2014/07/01 04:38:14
State check?
| |
| 110 } | |
| 111 | |
| 112 TEST_F(ScriptPromisePropertyTest, Promise_IsStableObjectAfterSettling) | |
| 113 { | |
| 114 RefPtrWillBeRawPtr<Property> p(newProperty()); | |
| 115 ScriptPromise v = p->promise(DOMWrapperWorld::mainWorld()); | |
| 116 | |
| 117 RefPtrWillBeRawPtr<Event> value(Event::create()); | |
| 118 p->resolve(value.get()); | |
| 119 | |
| 120 ScriptPromise w = p->promise(DOMWrapperWorld::mainWorld()); | |
| 121 EXPECT_EQ(v, w); | |
|
yhirano
2014/07/01 04:38:14
State check?
yhirano
2014/07/01 04:38:14
EXPECT_FALSE(v.isEmpty())
| |
| 122 } | |
| 123 | |
| 124 TEST_F(ScriptPromisePropertyTest, Promise_DoesNotImpedeGarbageCollection) | |
| 125 { | |
| 126 RefPtrWillBeRawPtr<Event> holder(Event::create()); | |
| 127 ScriptValue holderWrapper = wrap(holder); | |
| 128 | |
| 129 RefPtrWillBeRawPtr<Property> p(Property::create(&document(), holder, "test") ); | |
| 130 | |
| 131 RefPtrWillBeRawPtr<GCObservation> observation; | |
| 132 { | |
| 133 ScriptState::Scope scope(scriptState()); | |
| 134 observation = GCObservation::create(p->promise(DOMWrapperWorld::mainWorl d()).v8Value()); | |
| 135 } | |
| 136 | |
| 137 gc(); | |
| 138 EXPECT_FALSE(observation->wasCollected()); | |
| 139 | |
| 140 holderWrapper.clear(); | |
| 141 gc(); | |
| 142 EXPECT_TRUE(observation->wasCollected()); | |
| 143 } | |
| 144 | |
| 145 TEST_F(ScriptPromisePropertyTest, Resolve_ResolvesScriptPromise) | |
| 146 { | |
| 147 RefPtrWillBeRawPtr<Property> p(newProperty()); | |
| 148 | |
| 149 ScriptPromise promise = p->promise(DOMWrapperWorld::mainWorld()); | |
| 150 ScriptValue value; | |
| 151 size_t nResolveCalls = 0; | |
| 152 | |
| 153 { | |
| 154 ScriptState::Scope scope(scriptState()); | |
| 155 promise.then(stub(value, nResolveCalls), notReached()); | |
| 156 } | |
| 157 | |
| 158 RefPtrWillBeRawPtr<Event> event(Event::create()); | |
| 159 p->resolve(event.get()); | |
| 160 isolate()->RunMicrotasks(); | |
| 161 | |
| 162 EXPECT_EQ(1u, nResolveCalls); | |
| 163 EXPECT_EQ(wrap(event), value); | |
| 164 } | |
| 165 | |
| 166 TEST_F(ScriptPromisePropertyTest, Resolve_ChangesState) | |
| 167 { | |
| 168 RefPtrWillBeRawPtr<Property> p(newProperty()); | |
| 169 EXPECT_EQ(Property::Pending, p->state()); | |
| 170 RefPtrWillBeRawPtr<Event> value(Event::create()); | |
| 171 p->resolve(value.get()); | |
| 172 EXPECT_EQ(Property::Resolved, p->state()); | |
| 173 } | |
| 174 | |
| 175 TEST_F(ScriptPromisePropertyTest, Reject_RejectsScriptPromise) | |
| 176 { | |
| 177 RefPtrWillBeRawPtr<Property> p(newProperty()); | |
| 178 | |
| 179 RefPtrWillBeRawPtr<Event> event(Event::create()); | |
| 180 p->reject(event.get()); | |
| 181 | |
| 182 ScriptValue value; | |
| 183 size_t nRejectCalls = 0; | |
| 184 | |
| 185 { | |
| 186 ScriptState::Scope scope(scriptState()); | |
| 187 p->promise(DOMWrapperWorld::mainWorld()).then(notReached(), stub(value, nRejectCalls)); | |
| 188 } | |
| 189 | |
| 190 isolate()->RunMicrotasks(); | |
| 191 | |
| 192 EXPECT_EQ(1u, nRejectCalls); | |
| 193 EXPECT_EQ(wrap(event), value); | |
| 194 } | |
| 195 | |
| 196 TEST_F(ScriptPromisePropertyTest, Settle_IsIdempotent) | |
| 197 { | |
| 198 RefPtrWillBeRawPtr<Property> p(newProperty()); | |
| 199 | |
| 200 RefPtrWillBeRawPtr<Event> eventA(Event::create()); | |
| 201 p->reject(eventA.get()); | |
| 202 RefPtrWillBeRawPtr<Event> eventB(Event::create()); | |
| 203 p->resolve(eventB.get()); | |
| 204 | |
| 205 ScriptValue value; | |
| 206 size_t nRejectCalls = 0; | |
| 207 | |
| 208 { | |
| 209 ScriptState::Scope scope(scriptState()); | |
| 210 p->promise(DOMWrapperWorld::mainWorld()).then(notReached(), stub(value, nRejectCalls)); | |
| 211 } | |
| 212 | |
| 213 isolate()->RunMicrotasks(); | |
| 214 | |
| 215 EXPECT_EQ(1u, nRejectCalls); | |
| 216 EXPECT_EQ(wrap(eventA), value); | |
| 217 | |
| 218 RefPtrWillBeRawPtr<Event> eventC(Event::create()); | |
| 219 p->reject(eventC.get()); | |
| 220 EXPECT_EQ(1u, nRejectCalls); | |
| 221 } | |
| 222 | |
| 223 TEST_F(ScriptPromisePropertyTest, Settle_WaitsForResume) | |
| 224 { | |
| 225 document().suspendScheduledTasks(); | |
| 226 | |
| 227 RefPtrWillBeRawPtr<Property> p(newProperty()); | |
| 228 | |
| 229 RefPtrWillBeRawPtr<Event> event(Event::create()); | |
| 230 p->resolve(event.get()); | |
| 231 | |
| 232 ScriptValue value; | |
| 233 size_t nResolveCalls = 0; | |
| 234 | |
| 235 { | |
| 236 ScriptState::Scope scope(scriptState()); | |
| 237 p->promise(DOMWrapperWorld::mainWorld()).then(stub(value, nResolveCalls) , notReached()); | |
| 238 } | |
| 239 | |
| 240 isolate()->RunMicrotasks(); | |
| 241 EXPECT_EQ(0u, nResolveCalls); | |
| 242 | |
| 243 document().resumeScheduledTasks(); | |
| 244 isolate()->RunMicrotasks(); | |
| 245 EXPECT_EQ(1u, nResolveCalls); | |
| 246 EXPECT_EQ(wrap(event), value); | |
| 247 } | |
| 248 | |
| 249 } // namespace | |
| OLD | NEW |