| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "bindings/core/v8/ScriptPromiseProperty.h" | 5 #include "bindings/core/v8/ScriptPromiseProperty.h" |
| 6 | 6 |
| 7 #include "bindings/core/v8/DOMWrapperWorld.h" | 7 #include "bindings/core/v8/DOMWrapperWorld.h" |
| 8 #include "bindings/core/v8/ScriptFunction.h" | 8 #include "bindings/core/v8/ScriptFunction.h" |
| 9 #include "bindings/core/v8/ScriptPromise.h" | 9 #include "bindings/core/v8/ScriptPromise.h" |
| 10 #include "bindings/core/v8/ScriptState.h" | 10 #include "bindings/core/v8/ScriptState.h" |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 79 size_t& m_callCount; | 79 size_t& m_callCount; |
| 80 }; | 80 }; |
| 81 | 81 |
| 82 class GarbageCollectedHolder : public GarbageCollectedScriptWrappable { | 82 class GarbageCollectedHolder : public GarbageCollectedScriptWrappable { |
| 83 public: | 83 public: |
| 84 typedef ScriptPromiseProperty<Member<GarbageCollectedScriptWrappable>, Membe
r<GarbageCollectedScriptWrappable>, Member<GarbageCollectedScriptWrappable>> Pro
perty; | 84 typedef ScriptPromiseProperty<Member<GarbageCollectedScriptWrappable>, Membe
r<GarbageCollectedScriptWrappable>, Member<GarbageCollectedScriptWrappable>> Pro
perty; |
| 85 GarbageCollectedHolder(ExecutionContext* executionContext) | 85 GarbageCollectedHolder(ExecutionContext* executionContext) |
| 86 : GarbageCollectedScriptWrappable("holder") | 86 : GarbageCollectedScriptWrappable("holder") |
| 87 , m_property(new Property(executionContext, toGarbageCollectedScriptWrap
pable(), Property::Ready)) { } | 87 , m_property(new Property(executionContext, toGarbageCollectedScriptWrap
pable(), Property::Ready)) { } |
| 88 | 88 |
| 89 Property* property() { return m_property; } | 89 Property* getProperty() { return m_property; } |
| 90 GarbageCollectedScriptWrappable* toGarbageCollectedScriptWrappable() { retur
n this; } | 90 GarbageCollectedScriptWrappable* toGarbageCollectedScriptWrappable() { retur
n this; } |
| 91 | 91 |
| 92 DEFINE_INLINE_VIRTUAL_TRACE() | 92 DEFINE_INLINE_VIRTUAL_TRACE() |
| 93 { | 93 { |
| 94 GarbageCollectedScriptWrappable::trace(visitor); | 94 GarbageCollectedScriptWrappable::trace(visitor); |
| 95 visitor->trace(m_property); | 95 visitor->trace(m_property); |
| 96 } | 96 } |
| 97 | 97 |
| 98 private: | 98 private: |
| 99 Member<Property> m_property; | 99 Member<Property> m_property; |
| 100 }; | 100 }; |
| 101 | 101 |
| 102 class RefCountedHolder : public RefCountedScriptWrappable { | 102 class RefCountedHolder : public RefCountedScriptWrappable { |
| 103 public: | 103 public: |
| 104 // Do not resolve or reject the property with the holder itself. It leads | 104 // Do not resolve or reject the property with the holder itself. It leads |
| 105 // to a leak. | 105 // to a leak. |
| 106 typedef ScriptPromiseProperty<RefCountedScriptWrappable*, RefPtr<RefCountedS
criptWrappable>, RefPtr<RefCountedScriptWrappable>> Property; | 106 typedef ScriptPromiseProperty<RefCountedScriptWrappable*, RefPtr<RefCountedS
criptWrappable>, RefPtr<RefCountedScriptWrappable>> Property; |
| 107 static PassRefPtr<RefCountedHolder> create(ExecutionContext* executionContex
t) | 107 static PassRefPtr<RefCountedHolder> create(ExecutionContext* executionContex
t) |
| 108 { | 108 { |
| 109 return adoptRef(new RefCountedHolder(executionContext)); | 109 return adoptRef(new RefCountedHolder(executionContext)); |
| 110 } | 110 } |
| 111 Property* property() { return m_property; } | 111 Property* getProperty() { return m_property; } |
| 112 RefCountedScriptWrappable* toRefCountedScriptWrappable() { return this; } | 112 RefCountedScriptWrappable* toRefCountedScriptWrappable() { return this; } |
| 113 | 113 |
| 114 private: | 114 private: |
| 115 RefCountedHolder(ExecutionContext* executionContext) | 115 RefCountedHolder(ExecutionContext* executionContext) |
| 116 : RefCountedScriptWrappable("holder") | 116 : RefCountedScriptWrappable("holder") |
| 117 , m_property(new Property(executionContext, toRefCountedScriptWrappable(
), Property::Ready)) { } | 117 , m_property(new Property(executionContext, toRefCountedScriptWrappable(
), Property::Ready)) { } |
| 118 | 118 |
| 119 Persistent<Property> m_property; | 119 Persistent<Property> m_property; |
| 120 }; | 120 }; |
| 121 | 121 |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 175 class ScriptPromisePropertyGarbageCollectedTest : public ScriptPromisePropertyTe
stBase, public ::testing::Test { | 175 class ScriptPromisePropertyGarbageCollectedTest : public ScriptPromisePropertyTe
stBase, public ::testing::Test { |
| 176 public: | 176 public: |
| 177 typedef GarbageCollectedHolder::Property Property; | 177 typedef GarbageCollectedHolder::Property Property; |
| 178 | 178 |
| 179 ScriptPromisePropertyGarbageCollectedTest() | 179 ScriptPromisePropertyGarbageCollectedTest() |
| 180 : m_holder(new GarbageCollectedHolder(&document())) | 180 : m_holder(new GarbageCollectedHolder(&document())) |
| 181 { | 181 { |
| 182 } | 182 } |
| 183 | 183 |
| 184 GarbageCollectedHolder* holder() { return m_holder; } | 184 GarbageCollectedHolder* holder() { return m_holder; } |
| 185 Property* property() { return m_holder->property(); } | 185 Property* getProperty() { return m_holder->getProperty(); } |
| 186 ScriptPromise promise(DOMWrapperWorld& world) { return property()->promise(w
orld); } | 186 ScriptPromise promise(DOMWrapperWorld& world) { return getProperty()->promis
e(world); } |
| 187 | 187 |
| 188 private: | 188 private: |
| 189 Persistent<GarbageCollectedHolder> m_holder; | 189 Persistent<GarbageCollectedHolder> m_holder; |
| 190 }; | 190 }; |
| 191 | 191 |
| 192 TEST_F(ScriptPromisePropertyGarbageCollectedTest, Promise_IsStableObjectInMainWo
rld) | 192 TEST_F(ScriptPromisePropertyGarbageCollectedTest, Promise_IsStableObjectInMainWo
rld) |
| 193 { | 193 { |
| 194 ScriptPromise v = property()->promise(DOMWrapperWorld::mainWorld()); | 194 ScriptPromise v = getProperty()->promise(DOMWrapperWorld::mainWorld()); |
| 195 ScriptPromise w = property()->promise(DOMWrapperWorld::mainWorld()); | 195 ScriptPromise w = getProperty()->promise(DOMWrapperWorld::mainWorld()); |
| 196 EXPECT_EQ(v, w); | 196 EXPECT_EQ(v, w); |
| 197 ASSERT_FALSE(v.isEmpty()); | 197 ASSERT_FALSE(v.isEmpty()); |
| 198 { | 198 { |
| 199 ScriptState::Scope scope(mainScriptState()); | 199 ScriptState::Scope scope(mainScriptState()); |
| 200 EXPECT_EQ(v.v8Value().As<v8::Object>()->CreationContext(), toV8Context(&
document(), mainWorld())); | 200 EXPECT_EQ(v.v8Value().As<v8::Object>()->CreationContext(), toV8Context(&
document(), mainWorld())); |
| 201 } | 201 } |
| 202 EXPECT_EQ(Property::Pending, property()->getState()); | 202 EXPECT_EQ(Property::Pending, getProperty()->getState()); |
| 203 } | 203 } |
| 204 | 204 |
| 205 TEST_F(ScriptPromisePropertyGarbageCollectedTest, Promise_IsStableObjectInVariou
sWorlds) | 205 TEST_F(ScriptPromisePropertyGarbageCollectedTest, Promise_IsStableObjectInVariou
sWorlds) |
| 206 { | 206 { |
| 207 ScriptPromise u = property()->promise(otherWorld()); | 207 ScriptPromise u = getProperty()->promise(otherWorld()); |
| 208 ScriptPromise v = property()->promise(DOMWrapperWorld::mainWorld()); | 208 ScriptPromise v = getProperty()->promise(DOMWrapperWorld::mainWorld()); |
| 209 ScriptPromise w = property()->promise(DOMWrapperWorld::mainWorld()); | 209 ScriptPromise w = getProperty()->promise(DOMWrapperWorld::mainWorld()); |
| 210 EXPECT_NE(mainScriptState(), otherScriptState()); | 210 EXPECT_NE(mainScriptState(), otherScriptState()); |
| 211 EXPECT_NE(&mainWorld(), &otherWorld()); | 211 EXPECT_NE(&mainWorld(), &otherWorld()); |
| 212 EXPECT_NE(u, v); | 212 EXPECT_NE(u, v); |
| 213 EXPECT_EQ(v, w); | 213 EXPECT_EQ(v, w); |
| 214 ASSERT_FALSE(u.isEmpty()); | 214 ASSERT_FALSE(u.isEmpty()); |
| 215 ASSERT_FALSE(v.isEmpty()); | 215 ASSERT_FALSE(v.isEmpty()); |
| 216 { | 216 { |
| 217 ScriptState::Scope scope(otherScriptState()); | 217 ScriptState::Scope scope(otherScriptState()); |
| 218 EXPECT_EQ(u.v8Value().As<v8::Object>()->CreationContext(), toV8Context(&
document(), otherWorld())); | 218 EXPECT_EQ(u.v8Value().As<v8::Object>()->CreationContext(), toV8Context(&
document(), otherWorld())); |
| 219 } | 219 } |
| 220 { | 220 { |
| 221 ScriptState::Scope scope(mainScriptState()); | 221 ScriptState::Scope scope(mainScriptState()); |
| 222 EXPECT_EQ(v.v8Value().As<v8::Object>()->CreationContext(), toV8Context(&
document(), mainWorld())); | 222 EXPECT_EQ(v.v8Value().As<v8::Object>()->CreationContext(), toV8Context(&
document(), mainWorld())); |
| 223 } | 223 } |
| 224 EXPECT_EQ(Property::Pending, property()->getState()); | 224 EXPECT_EQ(Property::Pending, getProperty()->getState()); |
| 225 } | 225 } |
| 226 | 226 |
| 227 TEST_F(ScriptPromisePropertyGarbageCollectedTest, Promise_IsStableObjectAfterSet
tling) | 227 TEST_F(ScriptPromisePropertyGarbageCollectedTest, Promise_IsStableObjectAfterSet
tling) |
| 228 { | 228 { |
| 229 ScriptPromise v = promise(DOMWrapperWorld::mainWorld()); | 229 ScriptPromise v = promise(DOMWrapperWorld::mainWorld()); |
| 230 GarbageCollectedScriptWrappable* value = new GarbageCollectedScriptWrappable
("value"); | 230 GarbageCollectedScriptWrappable* value = new GarbageCollectedScriptWrappable
("value"); |
| 231 | 231 |
| 232 property()->resolve(value); | 232 getProperty()->resolve(value); |
| 233 EXPECT_EQ(Property::Resolved, property()->getState()); | 233 EXPECT_EQ(Property::Resolved, getProperty()->getState()); |
| 234 | 234 |
| 235 ScriptPromise w = promise(DOMWrapperWorld::mainWorld()); | 235 ScriptPromise w = promise(DOMWrapperWorld::mainWorld()); |
| 236 EXPECT_EQ(v, w); | 236 EXPECT_EQ(v, w); |
| 237 EXPECT_FALSE(v.isEmpty()); | 237 EXPECT_FALSE(v.isEmpty()); |
| 238 } | 238 } |
| 239 | 239 |
| 240 TEST_F(ScriptPromisePropertyGarbageCollectedTest, Promise_DoesNotImpedeGarbageCo
llection) | 240 TEST_F(ScriptPromisePropertyGarbageCollectedTest, Promise_DoesNotImpedeGarbageCo
llection) |
| 241 { | 241 { |
| 242 ScriptValue holderWrapper = wrap(mainWorld(), holder()->toGarbageCollectedSc
riptWrappable()); | 242 ScriptValue holderWrapper = wrap(mainWorld(), holder()->toGarbageCollectedSc
riptWrappable()); |
| 243 | 243 |
| 244 Persistent<GCObservation> observation; | 244 Persistent<GCObservation> observation; |
| 245 { | 245 { |
| 246 ScriptState::Scope scope(mainScriptState()); | 246 ScriptState::Scope scope(mainScriptState()); |
| 247 observation = GCObservation::create(promise(DOMWrapperWorld::mainWorld()
).v8Value()); | 247 observation = GCObservation::create(promise(DOMWrapperWorld::mainWorld()
).v8Value()); |
| 248 } | 248 } |
| 249 | 249 |
| 250 gc(); | 250 gc(); |
| 251 EXPECT_FALSE(observation->wasCollected()); | 251 EXPECT_FALSE(observation->wasCollected()); |
| 252 | 252 |
| 253 holderWrapper.clear(); | 253 holderWrapper.clear(); |
| 254 gc(); | 254 gc(); |
| 255 EXPECT_TRUE(observation->wasCollected()); | 255 EXPECT_TRUE(observation->wasCollected()); |
| 256 | 256 |
| 257 EXPECT_EQ(Property::Pending, property()->getState()); | 257 EXPECT_EQ(Property::Pending, getProperty()->getState()); |
| 258 } | 258 } |
| 259 | 259 |
| 260 TEST_F(ScriptPromisePropertyGarbageCollectedTest, Resolve_ResolvesScriptPromise) | 260 TEST_F(ScriptPromisePropertyGarbageCollectedTest, Resolve_ResolvesScriptPromise) |
| 261 { | 261 { |
| 262 ScriptPromise promise = property()->promise(DOMWrapperWorld::mainWorld()); | 262 ScriptPromise promise = getProperty()->promise(DOMWrapperWorld::mainWorld())
; |
| 263 ScriptPromise otherPromise = property()->promise(otherWorld()); | 263 ScriptPromise otherPromise = getProperty()->promise(otherWorld()); |
| 264 ScriptValue actual, otherActual; | 264 ScriptValue actual, otherActual; |
| 265 size_t nResolveCalls = 0; | 265 size_t nResolveCalls = 0; |
| 266 size_t nOtherResolveCalls = 0; | 266 size_t nOtherResolveCalls = 0; |
| 267 | 267 |
| 268 { | 268 { |
| 269 ScriptState::Scope scope(mainScriptState()); | 269 ScriptState::Scope scope(mainScriptState()); |
| 270 promise.then(stub(currentScriptState(), actual, nResolveCalls), notReach
ed(currentScriptState())); | 270 promise.then(stub(currentScriptState(), actual, nResolveCalls), notReach
ed(currentScriptState())); |
| 271 } | 271 } |
| 272 | 272 |
| 273 { | 273 { |
| 274 ScriptState::Scope scope(otherScriptState()); | 274 ScriptState::Scope scope(otherScriptState()); |
| 275 otherPromise.then(stub(currentScriptState(), otherActual, nOtherResolveC
alls), notReached(currentScriptState())); | 275 otherPromise.then(stub(currentScriptState(), otherActual, nOtherResolveC
alls), notReached(currentScriptState())); |
| 276 } | 276 } |
| 277 | 277 |
| 278 EXPECT_NE(promise, otherPromise); | 278 EXPECT_NE(promise, otherPromise); |
| 279 | 279 |
| 280 GarbageCollectedScriptWrappable* value = new GarbageCollectedScriptWrappable
("value"); | 280 GarbageCollectedScriptWrappable* value = new GarbageCollectedScriptWrappable
("value"); |
| 281 property()->resolve(value); | 281 getProperty()->resolve(value); |
| 282 EXPECT_EQ(Property::Resolved, property()->getState()); | 282 EXPECT_EQ(Property::Resolved, getProperty()->getState()); |
| 283 | 283 |
| 284 v8::MicrotasksScope::PerformCheckpoint(isolate()); | 284 v8::MicrotasksScope::PerformCheckpoint(isolate()); |
| 285 EXPECT_EQ(1u, nResolveCalls); | 285 EXPECT_EQ(1u, nResolveCalls); |
| 286 EXPECT_EQ(1u, nOtherResolveCalls); | 286 EXPECT_EQ(1u, nOtherResolveCalls); |
| 287 EXPECT_EQ(wrap(mainWorld(), value), actual); | 287 EXPECT_EQ(wrap(mainWorld(), value), actual); |
| 288 EXPECT_NE(actual, otherActual); | 288 EXPECT_NE(actual, otherActual); |
| 289 EXPECT_EQ(wrap(otherWorld(), value), otherActual); | 289 EXPECT_EQ(wrap(otherWorld(), value), otherActual); |
| 290 } | 290 } |
| 291 | 291 |
| 292 TEST_F(ScriptPromisePropertyGarbageCollectedTest, ResolveAndGetPromiseOnOtherWor
ld) | 292 TEST_F(ScriptPromisePropertyGarbageCollectedTest, ResolveAndGetPromiseOnOtherWor
ld) |
| 293 { | 293 { |
| 294 ScriptPromise promise = property()->promise(DOMWrapperWorld::mainWorld()); | 294 ScriptPromise promise = getProperty()->promise(DOMWrapperWorld::mainWorld())
; |
| 295 ScriptPromise otherPromise = property()->promise(otherWorld()); | 295 ScriptPromise otherPromise = getProperty()->promise(otherWorld()); |
| 296 ScriptValue actual, otherActual; | 296 ScriptValue actual, otherActual; |
| 297 size_t nResolveCalls = 0; | 297 size_t nResolveCalls = 0; |
| 298 size_t nOtherResolveCalls = 0; | 298 size_t nOtherResolveCalls = 0; |
| 299 | 299 |
| 300 { | 300 { |
| 301 ScriptState::Scope scope(mainScriptState()); | 301 ScriptState::Scope scope(mainScriptState()); |
| 302 promise.then(stub(currentScriptState(), actual, nResolveCalls), notReach
ed(currentScriptState())); | 302 promise.then(stub(currentScriptState(), actual, nResolveCalls), notReach
ed(currentScriptState())); |
| 303 } | 303 } |
| 304 | 304 |
| 305 EXPECT_NE(promise, otherPromise); | 305 EXPECT_NE(promise, otherPromise); |
| 306 GarbageCollectedScriptWrappable* value = new GarbageCollectedScriptWrappable
("value"); | 306 GarbageCollectedScriptWrappable* value = new GarbageCollectedScriptWrappable
("value"); |
| 307 property()->resolve(value); | 307 getProperty()->resolve(value); |
| 308 EXPECT_EQ(Property::Resolved, property()->getState()); | 308 EXPECT_EQ(Property::Resolved, getProperty()->getState()); |
| 309 | 309 |
| 310 v8::MicrotasksScope::PerformCheckpoint(isolate()); | 310 v8::MicrotasksScope::PerformCheckpoint(isolate()); |
| 311 EXPECT_EQ(1u, nResolveCalls); | 311 EXPECT_EQ(1u, nResolveCalls); |
| 312 EXPECT_EQ(0u, nOtherResolveCalls); | 312 EXPECT_EQ(0u, nOtherResolveCalls); |
| 313 | 313 |
| 314 { | 314 { |
| 315 ScriptState::Scope scope(otherScriptState()); | 315 ScriptState::Scope scope(otherScriptState()); |
| 316 otherPromise.then(stub(currentScriptState(), otherActual, nOtherResolveC
alls), notReached(currentScriptState())); | 316 otherPromise.then(stub(currentScriptState(), otherActual, nOtherResolveC
alls), notReached(currentScriptState())); |
| 317 } | 317 } |
| 318 | 318 |
| 319 v8::MicrotasksScope::PerformCheckpoint(isolate()); | 319 v8::MicrotasksScope::PerformCheckpoint(isolate()); |
| 320 EXPECT_EQ(1u, nResolveCalls); | 320 EXPECT_EQ(1u, nResolveCalls); |
| 321 EXPECT_EQ(1u, nOtherResolveCalls); | 321 EXPECT_EQ(1u, nOtherResolveCalls); |
| 322 EXPECT_EQ(wrap(mainWorld(), value), actual); | 322 EXPECT_EQ(wrap(mainWorld(), value), actual); |
| 323 EXPECT_NE(actual, otherActual); | 323 EXPECT_NE(actual, otherActual); |
| 324 EXPECT_EQ(wrap(otherWorld(), value), otherActual); | 324 EXPECT_EQ(wrap(otherWorld(), value), otherActual); |
| 325 } | 325 } |
| 326 | 326 |
| 327 TEST_F(ScriptPromisePropertyGarbageCollectedTest, Reject_RejectsScriptPromise) | 327 TEST_F(ScriptPromisePropertyGarbageCollectedTest, Reject_RejectsScriptPromise) |
| 328 { | 328 { |
| 329 GarbageCollectedScriptWrappable* reason = new GarbageCollectedScriptWrappabl
e("reason"); | 329 GarbageCollectedScriptWrappable* reason = new GarbageCollectedScriptWrappabl
e("reason"); |
| 330 property()->reject(reason); | 330 getProperty()->reject(reason); |
| 331 EXPECT_EQ(Property::Rejected, property()->getState()); | 331 EXPECT_EQ(Property::Rejected, getProperty()->getState()); |
| 332 | 332 |
| 333 ScriptValue actual, otherActual; | 333 ScriptValue actual, otherActual; |
| 334 size_t nRejectCalls = 0; | 334 size_t nRejectCalls = 0; |
| 335 size_t nOtherRejectCalls = 0; | 335 size_t nOtherRejectCalls = 0; |
| 336 { | 336 { |
| 337 ScriptState::Scope scope(mainScriptState()); | 337 ScriptState::Scope scope(mainScriptState()); |
| 338 property()->promise(DOMWrapperWorld::mainWorld()).then(notReached(curren
tScriptState()), stub(currentScriptState(), actual, nRejectCalls)); | 338 getProperty()->promise(DOMWrapperWorld::mainWorld()).then(notReached(cur
rentScriptState()), stub(currentScriptState(), actual, nRejectCalls)); |
| 339 } | 339 } |
| 340 | 340 |
| 341 { | 341 { |
| 342 ScriptState::Scope scope(otherScriptState()); | 342 ScriptState::Scope scope(otherScriptState()); |
| 343 property()->promise(otherWorld()).then(notReached(currentScriptState()),
stub(currentScriptState(), otherActual, nOtherRejectCalls)); | 343 getProperty()->promise(otherWorld()).then(notReached(currentScriptState(
)), stub(currentScriptState(), otherActual, nOtherRejectCalls)); |
| 344 } | 344 } |
| 345 | 345 |
| 346 v8::MicrotasksScope::PerformCheckpoint(isolate()); | 346 v8::MicrotasksScope::PerformCheckpoint(isolate()); |
| 347 EXPECT_EQ(1u, nRejectCalls); | 347 EXPECT_EQ(1u, nRejectCalls); |
| 348 EXPECT_EQ(wrap(mainWorld(), reason), actual); | 348 EXPECT_EQ(wrap(mainWorld(), reason), actual); |
| 349 EXPECT_EQ(1u, nOtherRejectCalls); | 349 EXPECT_EQ(1u, nOtherRejectCalls); |
| 350 EXPECT_NE(actual, otherActual); | 350 EXPECT_NE(actual, otherActual); |
| 351 EXPECT_EQ(wrap(otherWorld(), reason), otherActual); | 351 EXPECT_EQ(wrap(otherWorld(), reason), otherActual); |
| 352 } | 352 } |
| 353 | 353 |
| 354 TEST_F(ScriptPromisePropertyGarbageCollectedTest, Promise_DeadContext) | 354 TEST_F(ScriptPromisePropertyGarbageCollectedTest, Promise_DeadContext) |
| 355 { | 355 { |
| 356 property()->resolve(new GarbageCollectedScriptWrappable("value")); | 356 getProperty()->resolve(new GarbageCollectedScriptWrappable("value")); |
| 357 EXPECT_EQ(Property::Resolved, property()->getState()); | 357 EXPECT_EQ(Property::Resolved, getProperty()->getState()); |
| 358 | 358 |
| 359 destroyContext(); | 359 destroyContext(); |
| 360 | 360 |
| 361 EXPECT_TRUE(property()->promise(DOMWrapperWorld::mainWorld()).isEmpty()); | 361 EXPECT_TRUE(getProperty()->promise(DOMWrapperWorld::mainWorld()).isEmpty()); |
| 362 } | 362 } |
| 363 | 363 |
| 364 TEST_F(ScriptPromisePropertyGarbageCollectedTest, Resolve_DeadContext) | 364 TEST_F(ScriptPromisePropertyGarbageCollectedTest, Resolve_DeadContext) |
| 365 { | 365 { |
| 366 { | 366 { |
| 367 ScriptState::Scope scope(mainScriptState()); | 367 ScriptState::Scope scope(mainScriptState()); |
| 368 property()->promise(DOMWrapperWorld::mainWorld()).then(notReached(curren
tScriptState()), notReached(currentScriptState())); | 368 getProperty()->promise(DOMWrapperWorld::mainWorld()).then(notReached(cur
rentScriptState()), notReached(currentScriptState())); |
| 369 } | 369 } |
| 370 | 370 |
| 371 destroyContext(); | 371 destroyContext(); |
| 372 EXPECT_TRUE(!property()->getExecutionContext() || property()->getExecutionCo
ntext()->activeDOMObjectsAreStopped()); | 372 EXPECT_TRUE(!getProperty()->getExecutionContext() || getProperty()->getExecu
tionContext()->activeDOMObjectsAreStopped()); |
| 373 | 373 |
| 374 property()->resolve(new GarbageCollectedScriptWrappable("value")); | 374 getProperty()->resolve(new GarbageCollectedScriptWrappable("value")); |
| 375 EXPECT_EQ(Property::Pending, property()->getState()); | 375 EXPECT_EQ(Property::Pending, getProperty()->getState()); |
| 376 | 376 |
| 377 v8::MicrotasksScope::PerformCheckpoint(v8::Isolate::GetCurrent()); | 377 v8::MicrotasksScope::PerformCheckpoint(v8::Isolate::GetCurrent()); |
| 378 } | 378 } |
| 379 | 379 |
| 380 TEST_F(ScriptPromisePropertyGarbageCollectedTest, Reset) | 380 TEST_F(ScriptPromisePropertyGarbageCollectedTest, Reset) |
| 381 { | 381 { |
| 382 ScriptPromise oldPromise, newPromise; | 382 ScriptPromise oldPromise, newPromise; |
| 383 ScriptValue oldActual, newActual; | 383 ScriptValue oldActual, newActual; |
| 384 GarbageCollectedScriptWrappable* oldValue = new GarbageCollectedScriptWrappa
ble("old"); | 384 GarbageCollectedScriptWrappable* oldValue = new GarbageCollectedScriptWrappa
ble("old"); |
| 385 GarbageCollectedScriptWrappable* newValue = new GarbageCollectedScriptWrappa
ble("new"); | 385 GarbageCollectedScriptWrappable* newValue = new GarbageCollectedScriptWrappa
ble("new"); |
| 386 size_t nOldResolveCalls = 0; | 386 size_t nOldResolveCalls = 0; |
| 387 size_t nNewRejectCalls = 0; | 387 size_t nNewRejectCalls = 0; |
| 388 | 388 |
| 389 { | 389 { |
| 390 ScriptState::Scope scope(mainScriptState()); | 390 ScriptState::Scope scope(mainScriptState()); |
| 391 property()->resolve(oldValue); | 391 getProperty()->resolve(oldValue); |
| 392 oldPromise = property()->promise(mainWorld()); | 392 oldPromise = getProperty()->promise(mainWorld()); |
| 393 oldPromise.then(stub(currentScriptState(), oldActual, nOldResolveCalls),
notReached(currentScriptState())); | 393 oldPromise.then(stub(currentScriptState(), oldActual, nOldResolveCalls),
notReached(currentScriptState())); |
| 394 } | 394 } |
| 395 | 395 |
| 396 property()->reset(); | 396 getProperty()->reset(); |
| 397 | 397 |
| 398 { | 398 { |
| 399 ScriptState::Scope scope(mainScriptState()); | 399 ScriptState::Scope scope(mainScriptState()); |
| 400 newPromise = property()->promise(mainWorld()); | 400 newPromise = getProperty()->promise(mainWorld()); |
| 401 newPromise.then(notReached(currentScriptState()), stub(currentScriptStat
e(), newActual, nNewRejectCalls)); | 401 newPromise.then(notReached(currentScriptState()), stub(currentScriptStat
e(), newActual, nNewRejectCalls)); |
| 402 property()->reject(newValue); | 402 getProperty()->reject(newValue); |
| 403 } | 403 } |
| 404 | 404 |
| 405 EXPECT_EQ(0u, nOldResolveCalls); | 405 EXPECT_EQ(0u, nOldResolveCalls); |
| 406 EXPECT_EQ(0u, nNewRejectCalls); | 406 EXPECT_EQ(0u, nNewRejectCalls); |
| 407 | 407 |
| 408 v8::MicrotasksScope::PerformCheckpoint(isolate()); | 408 v8::MicrotasksScope::PerformCheckpoint(isolate()); |
| 409 EXPECT_EQ(1u, nOldResolveCalls); | 409 EXPECT_EQ(1u, nOldResolveCalls); |
| 410 EXPECT_EQ(1u, nNewRejectCalls); | 410 EXPECT_EQ(1u, nNewRejectCalls); |
| 411 EXPECT_NE(oldPromise, newPromise); | 411 EXPECT_NE(oldPromise, newPromise); |
| 412 EXPECT_EQ(wrap(mainWorld(), oldValue), oldActual); | 412 EXPECT_EQ(wrap(mainWorld(), oldValue), oldActual); |
| 413 EXPECT_EQ(wrap(mainWorld(), newValue), newActual); | 413 EXPECT_EQ(wrap(mainWorld(), newValue), newActual); |
| 414 EXPECT_NE(oldActual, newActual); | 414 EXPECT_NE(oldActual, newActual); |
| 415 } | 415 } |
| 416 | 416 |
| 417 // Tests that ScriptPromiseProperty works with a ref-counted holder. | 417 // Tests that ScriptPromiseProperty works with a ref-counted holder. |
| 418 class ScriptPromisePropertyRefCountedTest : public ScriptPromisePropertyTestBase
, public ::testing::Test { | 418 class ScriptPromisePropertyRefCountedTest : public ScriptPromisePropertyTestBase
, public ::testing::Test { |
| 419 public: | 419 public: |
| 420 typedef RefCountedHolder::Property Property; | 420 typedef RefCountedHolder::Property Property; |
| 421 | 421 |
| 422 ScriptPromisePropertyRefCountedTest() | 422 ScriptPromisePropertyRefCountedTest() |
| 423 : m_holder(RefCountedHolder::create(&document())) | 423 : m_holder(RefCountedHolder::create(&document())) |
| 424 { | 424 { |
| 425 } | 425 } |
| 426 | 426 |
| 427 RefCountedHolder* holder() { return m_holder.get(); } | 427 RefCountedHolder* holder() { return m_holder.get(); } |
| 428 Property* property() { return m_holder->property(); } | 428 Property* getProperty() { return m_holder->getProperty(); } |
| 429 | 429 |
| 430 private: | 430 private: |
| 431 RefPtr<RefCountedHolder> m_holder; | 431 RefPtr<RefCountedHolder> m_holder; |
| 432 }; | 432 }; |
| 433 | 433 |
| 434 TEST_F(ScriptPromisePropertyRefCountedTest, Resolve) | 434 TEST_F(ScriptPromisePropertyRefCountedTest, Resolve) |
| 435 { | 435 { |
| 436 ScriptValue actual; | 436 ScriptValue actual; |
| 437 size_t nResolveCalls = 0; | 437 size_t nResolveCalls = 0; |
| 438 | 438 |
| 439 { | 439 { |
| 440 ScriptState::Scope scope(mainScriptState()); | 440 ScriptState::Scope scope(mainScriptState()); |
| 441 property()->promise(DOMWrapperWorld::mainWorld()).then(stub(currentScrip
tState(), actual, nResolveCalls), notReached(currentScriptState())); | 441 getProperty()->promise(DOMWrapperWorld::mainWorld()).then(stub(currentSc
riptState(), actual, nResolveCalls), notReached(currentScriptState())); |
| 442 } | 442 } |
| 443 | 443 |
| 444 RefPtr<RefCountedScriptWrappable> value = RefCountedScriptWrappable::create(
"value"); | 444 RefPtr<RefCountedScriptWrappable> value = RefCountedScriptWrappable::create(
"value"); |
| 445 property()->resolve(value.get()); | 445 getProperty()->resolve(value.get()); |
| 446 EXPECT_EQ(Property::Resolved, property()->getState()); | 446 EXPECT_EQ(Property::Resolved, getProperty()->getState()); |
| 447 | 447 |
| 448 v8::MicrotasksScope::PerformCheckpoint(isolate()); | 448 v8::MicrotasksScope::PerformCheckpoint(isolate()); |
| 449 EXPECT_EQ(1u, nResolveCalls); | 449 EXPECT_EQ(1u, nResolveCalls); |
| 450 EXPECT_EQ(wrap(mainWorld(), value), actual); | 450 EXPECT_EQ(wrap(mainWorld(), value), actual); |
| 451 } | 451 } |
| 452 | 452 |
| 453 TEST_F(ScriptPromisePropertyRefCountedTest, Reject) | 453 TEST_F(ScriptPromisePropertyRefCountedTest, Reject) |
| 454 { | 454 { |
| 455 ScriptValue actual; | 455 ScriptValue actual; |
| 456 size_t nRejectCalls = 0; | 456 size_t nRejectCalls = 0; |
| 457 | 457 |
| 458 { | 458 { |
| 459 ScriptState::Scope scope(mainScriptState()); | 459 ScriptState::Scope scope(mainScriptState()); |
| 460 property()->promise(DOMWrapperWorld::mainWorld()).then(notReached(curren
tScriptState()), stub(currentScriptState(), actual, nRejectCalls)); | 460 getProperty()->promise(DOMWrapperWorld::mainWorld()).then(notReached(cur
rentScriptState()), stub(currentScriptState(), actual, nRejectCalls)); |
| 461 } | 461 } |
| 462 | 462 |
| 463 RefPtr<RefCountedScriptWrappable> reason = RefCountedScriptWrappable::create
("reason"); | 463 RefPtr<RefCountedScriptWrappable> reason = RefCountedScriptWrappable::create
("reason"); |
| 464 property()->reject(reason); | 464 getProperty()->reject(reason); |
| 465 EXPECT_EQ(Property::Rejected, property()->getState()); | 465 EXPECT_EQ(Property::Rejected, getProperty()->getState()); |
| 466 | 466 |
| 467 v8::MicrotasksScope::PerformCheckpoint(isolate()); | 467 v8::MicrotasksScope::PerformCheckpoint(isolate()); |
| 468 EXPECT_EQ(1u, nRejectCalls); | 468 EXPECT_EQ(1u, nRejectCalls); |
| 469 EXPECT_EQ(wrap(mainWorld(), reason), actual); | 469 EXPECT_EQ(wrap(mainWorld(), reason), actual); |
| 470 } | 470 } |
| 471 | 471 |
| 472 TEST_F(ScriptPromisePropertyRefCountedTest, ReSolveAndReset) | 472 TEST_F(ScriptPromisePropertyRefCountedTest, ReSolveAndReset) |
| 473 { | 473 { |
| 474 RefPtr<RefCountedScriptWrappable> value = RefCountedScriptWrappable::create(
"value"); | 474 RefPtr<RefCountedScriptWrappable> value = RefCountedScriptWrappable::create(
"value"); |
| 475 | 475 |
| 476 { | 476 { |
| 477 ScriptState::Scope scope(mainScriptState()); | 477 ScriptState::Scope scope(mainScriptState()); |
| 478 property()->resolve(value); | 478 getProperty()->resolve(value); |
| 479 } | 479 } |
| 480 | 480 |
| 481 EXPECT_EQ(2, value->refCount()); | 481 EXPECT_EQ(2, value->refCount()); |
| 482 property()->reset(); | 482 getProperty()->reset(); |
| 483 EXPECT_EQ(1, value->refCount()); | 483 EXPECT_EQ(1, value->refCount()); |
| 484 } | 484 } |
| 485 | 485 |
| 486 TEST_F(ScriptPromisePropertyRefCountedTest, RejectAndReset) | 486 TEST_F(ScriptPromisePropertyRefCountedTest, RejectAndReset) |
| 487 { | 487 { |
| 488 RefPtr<RefCountedScriptWrappable> value = RefCountedScriptWrappable::create(
"value"); | 488 RefPtr<RefCountedScriptWrappable> value = RefCountedScriptWrappable::create(
"value"); |
| 489 | 489 |
| 490 { | 490 { |
| 491 ScriptState::Scope scope(mainScriptState()); | 491 ScriptState::Scope scope(mainScriptState()); |
| 492 property()->reject(value.get()); | 492 getProperty()->reject(value.get()); |
| 493 } | 493 } |
| 494 | 494 |
| 495 EXPECT_EQ(2, value->refCount()); | 495 EXPECT_EQ(2, value->refCount()); |
| 496 property()->reset(); | 496 getProperty()->reset(); |
| 497 EXPECT_EQ(1, value->refCount()); | 497 EXPECT_EQ(1, value->refCount()); |
| 498 } | 498 } |
| 499 | 499 |
| 500 // Tests that ScriptPromiseProperty works with a non ScriptWrappable resolution | 500 // Tests that ScriptPromiseProperty works with a non ScriptWrappable resolution |
| 501 // target. | 501 // target. |
| 502 class ScriptPromisePropertyNonScriptWrappableResolutionTargetTest : public Scrip
tPromisePropertyTestBase, public ::testing::Test { | 502 class ScriptPromisePropertyNonScriptWrappableResolutionTargetTest : public Scrip
tPromisePropertyTestBase, public ::testing::Test { |
| 503 public: | 503 public: |
| 504 template <typename T> | 504 template <typename T> |
| 505 void test(const T& value, const char* expected, const char* file, size_t lin
e) | 505 void test(const T& value, const char* expected, const char* file, size_t lin
e) |
| 506 { | 506 { |
| (...skipping 28 matching lines...) Expand all Loading... |
| 535 { | 535 { |
| 536 test(String("hello"), "hello", __FILE__, __LINE__); | 536 test(String("hello"), "hello", __FILE__, __LINE__); |
| 537 } | 537 } |
| 538 | 538 |
| 539 TEST_F(ScriptPromisePropertyNonScriptWrappableResolutionTargetTest, ResolveWithI
nteger) | 539 TEST_F(ScriptPromisePropertyNonScriptWrappableResolutionTargetTest, ResolveWithI
nteger) |
| 540 { | 540 { |
| 541 test(-1, "-1", __FILE__, __LINE__); | 541 test(-1, "-1", __FILE__, __LINE__); |
| 542 } | 542 } |
| 543 | 543 |
| 544 } // namespace | 544 } // namespace |
| OLD | NEW |