fune/dom/promise/tests/file_promise_retval_tests.js
Boris Zbarsky a6fdc48869 Bug 1436276. Bindings should create their return promises in the current compartment even when called over Xrays. r=bholley
These are cases that are implementing the "convert an exception to a Promise"
steps of the Web IDL spec.  Typically the exception is thrown in the current
compartment; the Promise returned should simply match that.  Otherwise we can
end up with a situation in which the promise doesn't actaully have access to
its rejection value, which will cause problems if someone uses then() on the
promise: the return value of the then() call will get a sanitized exception
instead of the real one.

We _could_ try to match the actual compartment of the exception, in theory.
But it's not clear why this would be preferable to using the current
compartment, even if there were cases in which the exception _doesn't_ match
the current compartment.  Which there likely are not.

MozReview-Commit-ID: Ac2BHIHxfvY

--HG--
rename : dom/promise/tests/test_promise_argument.html => dom/promise/tests/test_promise_retval.html
rename : dom/promise/tests/test_promise_argument_xrays.html => dom/promise/tests/test_promise_retval_xrays.html
2018-02-10 01:34:10 -05:00

41 lines
1.7 KiB
JavaScript

/*
* This file is meant to provide common infrastructure for several consumers.
* The consumer is expected to define the following things:
*
* 1) A verifyPromiseGlobal function which does whatever test the consumer
* wants. This function is passed a promise and the global whose
* TestFunctions was used to get the promise.
* 2) A expectedExceptionGlobal function which is handed the global whose
* TestFunctions was used to trigger the exception and should return the
* global the exception is expected to live in.
* 3) A subframe (frames[0]) which can be used as a second global for creating
* promises.
*/
var label = "parent";
function testThrownException(global) {
var p = global.TestFunctions.throwToRejectPromise();
verifyPromiseGlobal(p, global, "throwToRejectPromise return value");
return p.then(() => {}).catch((err) => {
var expected = expectedExceptionGlobal(global)
is(SpecialPowers.unwrap(SpecialPowers.Cu.getGlobalForObject(err)),
expected,
"Should have an exception object from the right global too");
ok(err instanceof expected.DOMException,
"Should have a DOMException here");
is(Object.getPrototypeOf(err), expected.DOMException.prototype,
"Should have a DOMException from the right global");
is(err.name, "InvalidStateError", "Should have the right DOMException");
});
}
function runPromiseRetvalTests(finishFunc) {
Promise.resolve()
.then(testThrownException.bind(undefined, window))
.then(testThrownException.bind(undefined, frames[0]))
.then(finishFunc)
.catch(function(e) {
ok(false, `Exception thrown: ${e}@${location.pathname}:${e.lineNumber}:${e.columnNumber}`);
finishFunc();
});
}