Bug 1299626 - Fix "TypeError: can't access dead object" in assert.window(). draft
authorHenrik Skupin <mail@hskupin.info>
Tue, 28 Mar 2017 21:10:06 +0200
changeset 555213 75ec75350b8a27d98781c48f885df245f39257c7
parent 554958 aaa0cd3bd620daf6be29c72625f6e63fd0bc1d46
child 622571 58dd7301157b55cd989210f4aa494b6719f6c787
push id52198
push userbmo:hskupin@gmail.com
push dateMon, 03 Apr 2017 21:26:18 +0000
bugs1299626
milestone55.0a1
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
testing/marionette/assert.js
testing/marionette/test_assert.js
--- 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();