Bug 1299626 - Fix "TypeError: can't access dead object" in assert.window(). r=ato, a=test-only
authorHenrik Skupin <mail@hskupin.info>
Tue, 28 Mar 2017 21:10:06 +0200
changeset 379435 e081483e770de80eb78f7e77df7e06ee6ef944a3
parent 379434 6e2ce3dd9d87e587a66426931f5146b7e73db3d0
child 379436 9b81631b130a821b2af787c28e4ba02b2c5c5696
push id1419
push userjlund@mozilla.com
push dateMon, 10 Apr 2017 20:44:07 +0000
treeherdermozilla-release@5e6801b73ef6 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersato, test-only
bugs1299626
milestone53.0
Bug 1299626 - Fix "TypeError: can't access dead object" in assert.window(). r=ato, a=test-only 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
@@ -82,16 +82,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();