Bug 1299626 - Fix "TypeError: can't access dead object" in assert.window().
The assertion method is used to check for a valid window. But it can fail
itself when already accessing the ChromeWindow. To fix that we have to
catch a possible TypeError and let the method throw a NoSuchWindowError
instead.
MozReview-Commit-ID: 3Uaio4a3HtA
--- a/testing/marionette/assert.js
+++ b/testing/marionette/assert.js
@@ -128,17 +128,25 @@ assert.mobile = function (msg = "") {
* @return {ChromeWindow}
* |win| is returned unaltered.
*
* @throws {NoSuchWindowError}
* If |win| has been closed.
*/
assert.window = function (win, msg = "") {
msg = msg || "Unable to locate window";
- return assert.that(w => w && w.document.defaultView, msg, NoSuchWindowError)(win);
+ return assert.that(w => {
+ try {
+ return w && w.document.defaultView;
+
+ // If the window is no longer available a TypeError is thrown.
+ } catch (e if e.name === "TypeError") {
+ return null;
+ }
+ }, msg, NoSuchWindowError)(win);
}
/**
* Asserts that |obj| is defined.
*
* @param {?} obj
* Value to test.
* @param {string=} msg
--- a/testing/marionette/test_assert.js
+++ b/testing/marionette/test_assert.js
@@ -93,16 +93,28 @@ add_test(function test_boolean() {
add_test(function test_string() {
assert.string("foo");
assert.string(`bar`);
Assert.throws(() => assert.string(42), InvalidArgumentError);
run_next_test();
});
+add_test(function test_window() {
+ assert.window({ document: { defaultView: true }});
+
+ let deadWindow = { get document() { throw new TypeError("can't access dead object"); }};
+
+ for (let typ of [null, undefined, deadWindow]) {
+ Assert.throws(() => assert.window(typ), NoSuchWindowError);
+ }
+
+ run_next_test();
+});
+
add_test(function test_object() {
assert.object({});
assert.object(new Object());
for (let typ of [42, "foo", true, null, undefined]) {
Assert.throws(() => assert.object(typ), InvalidArgumentError);
}
run_next_test();