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 263 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 getProperty()->resolve(value); | 281 getProperty()->resolve(value); |
282 EXPECT_EQ(Property::Resolved, getProperty()->getState()); | 282 EXPECT_EQ(Property::Resolved, getProperty()->getState()); |
283 | 283 |
284 v8::MicrotasksScope::PerformCheckpoint(isolate()); | 284 isolate()->RunMicrotasks(); |
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 = getProperty()->promise(DOMWrapperWorld::mainWorld())
; | 294 ScriptPromise promise = getProperty()->promise(DOMWrapperWorld::mainWorld())
; |
295 ScriptPromise otherPromise = getProperty()->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 getProperty()->resolve(value); | 307 getProperty()->resolve(value); |
308 EXPECT_EQ(Property::Resolved, getProperty()->getState()); | 308 EXPECT_EQ(Property::Resolved, getProperty()->getState()); |
309 | 309 |
310 v8::MicrotasksScope::PerformCheckpoint(isolate()); | 310 isolate()->RunMicrotasks(); |
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 isolate()->RunMicrotasks(); |
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 getProperty()->reject(reason); | 330 getProperty()->reject(reason); |
331 EXPECT_EQ(Property::Rejected, getProperty()->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 getProperty()->promise(DOMWrapperWorld::mainWorld()).then(notReached(cur
rentScriptState()), 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 getProperty()->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 isolate()->RunMicrotasks(); |
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 getProperty()->resolve(new GarbageCollectedScriptWrappable("value")); | 356 getProperty()->resolve(new GarbageCollectedScriptWrappable("value")); |
(...skipping 10 matching lines...) Expand all Loading... |
367 ScriptState::Scope scope(mainScriptState()); | 367 ScriptState::Scope scope(mainScriptState()); |
368 getProperty()->promise(DOMWrapperWorld::mainWorld()).then(notReached(cur
rentScriptState()), notReached(currentScriptState())); | 368 getProperty()->promise(DOMWrapperWorld::mainWorld()).then(notReached(cur
rentScriptState()), notReached(currentScriptState())); |
369 } | 369 } |
370 | 370 |
371 destroyContext(); | 371 destroyContext(); |
372 EXPECT_TRUE(!getProperty()->getExecutionContext() || getProperty()->getExecu
tionContext()->activeDOMObjectsAreStopped()); | 372 EXPECT_TRUE(!getProperty()->getExecutionContext() || getProperty()->getExecu
tionContext()->activeDOMObjectsAreStopped()); |
373 | 373 |
374 getProperty()->resolve(new GarbageCollectedScriptWrappable("value")); | 374 getProperty()->resolve(new GarbageCollectedScriptWrappable("value")); |
375 EXPECT_EQ(Property::Pending, getProperty()->getState()); | 375 EXPECT_EQ(Property::Pending, getProperty()->getState()); |
376 | 376 |
377 v8::MicrotasksScope::PerformCheckpoint(v8::Isolate::GetCurrent()); | 377 v8::Isolate::GetCurrent()->RunMicrotasks(); |
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; |
(...skipping 10 matching lines...) Expand all Loading... |
398 { | 398 { |
399 ScriptState::Scope scope(mainScriptState()); | 399 ScriptState::Scope scope(mainScriptState()); |
400 newPromise = getProperty()->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 getProperty()->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 isolate()->RunMicrotasks(); |
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 { |
(...skipping 19 matching lines...) Expand all Loading... |
438 | 438 |
439 { | 439 { |
440 ScriptState::Scope scope(mainScriptState()); | 440 ScriptState::Scope scope(mainScriptState()); |
441 getProperty()->promise(DOMWrapperWorld::mainWorld()).then(stub(currentSc
riptState(), 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 getProperty()->resolve(value.get()); | 445 getProperty()->resolve(value.get()); |
446 EXPECT_EQ(Property::Resolved, getProperty()->getState()); | 446 EXPECT_EQ(Property::Resolved, getProperty()->getState()); |
447 | 447 |
448 v8::MicrotasksScope::PerformCheckpoint(isolate()); | 448 isolate()->RunMicrotasks(); |
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 getProperty()->promise(DOMWrapperWorld::mainWorld()).then(notReached(cur
rentScriptState()), 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 getProperty()->reject(reason); | 464 getProperty()->reject(reason); |
465 EXPECT_EQ(Property::Rejected, getProperty()->getState()); | 465 EXPECT_EQ(Property::Rejected, getProperty()->getState()); |
466 | 466 |
467 v8::MicrotasksScope::PerformCheckpoint(isolate()); | 467 isolate()->RunMicrotasks(); |
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()); |
(...skipping 29 matching lines...) Expand all Loading... |
507 typedef ScriptPromiseProperty<Member<GarbageCollectedScriptWrappable>, T
, ToV8UndefinedGenerator> Property; | 507 typedef ScriptPromiseProperty<Member<GarbageCollectedScriptWrappable>, T
, ToV8UndefinedGenerator> Property; |
508 Property* property = new Property(&document(), new GarbageCollectedScrip
tWrappable("holder"), Property::Ready); | 508 Property* property = new Property(&document(), new GarbageCollectedScrip
tWrappable("holder"), Property::Ready); |
509 size_t nResolveCalls = 0; | 509 size_t nResolveCalls = 0; |
510 ScriptValue actualValue; | 510 ScriptValue actualValue; |
511 String actual; | 511 String actual; |
512 { | 512 { |
513 ScriptState::Scope scope(mainScriptState()); | 513 ScriptState::Scope scope(mainScriptState()); |
514 property->promise(DOMWrapperWorld::mainWorld()).then(stub(currentScr
iptState(), actualValue, nResolveCalls), notReached(currentScriptState())); | 514 property->promise(DOMWrapperWorld::mainWorld()).then(stub(currentScr
iptState(), actualValue, nResolveCalls), notReached(currentScriptState())); |
515 } | 515 } |
516 property->resolve(value); | 516 property->resolve(value); |
517 v8::MicrotasksScope::PerformCheckpoint(isolate()); | 517 isolate()->RunMicrotasks(); |
518 { | 518 { |
519 ScriptState::Scope scope(mainScriptState()); | 519 ScriptState::Scope scope(mainScriptState()); |
520 actual = toCoreString(actualValue.v8Value()->ToString(mainScriptStat
e()->context()).ToLocalChecked()); | 520 actual = toCoreString(actualValue.v8Value()->ToString(mainScriptStat
e()->context()).ToLocalChecked()); |
521 } | 521 } |
522 if (expected != actual) { | 522 if (expected != actual) { |
523 ADD_FAILURE_AT(file, line) << "toV8 returns an incorrect value.\n A
ctual: " << actual.utf8().data() << "\nExpected: " << expected; | 523 ADD_FAILURE_AT(file, line) << "toV8 returns an incorrect value.\n A
ctual: " << actual.utf8().data() << "\nExpected: " << expected; |
524 return; | 524 return; |
525 } | 525 } |
526 } | 526 } |
527 }; | 527 }; |
528 | 528 |
529 TEST_F(ScriptPromisePropertyNonScriptWrappableResolutionTargetTest, ResolveWithU
ndefined) | 529 TEST_F(ScriptPromisePropertyNonScriptWrappableResolutionTargetTest, ResolveWithU
ndefined) |
530 { | 530 { |
531 test(ToV8UndefinedGenerator(), "undefined", __FILE__, __LINE__); | 531 test(ToV8UndefinedGenerator(), "undefined", __FILE__, __LINE__); |
532 } | 532 } |
533 | 533 |
534 TEST_F(ScriptPromisePropertyNonScriptWrappableResolutionTargetTest, ResolveWithS
tring) | 534 TEST_F(ScriptPromisePropertyNonScriptWrappableResolutionTargetTest, ResolveWithS
tring) |
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 |