Bug 1281915: Skip trying to remove a frame script if there no messageManager r?ato draft
authorDavid Burns <dburns@mozilla.com>
Wed, 06 Jul 2016 23:23:35 +0100
changeset 384680 44e1497ae0d6ebb8ab1f246dfc8e0cbe603d14d3
parent 383755 4936acc6a6683fc222da08e297f774605b726dd1
child 524771 e07c3e88c9baf09033be9f9817f4aba1cd57ef7c
push id22343
push userbmo:dburns@mozilla.com
push dateWed, 06 Jul 2016 22:29:10 +0000
reviewersato
bugs1281915
milestone48.0
Bug 1281915: Skip trying to remove a frame script if there no messageManager r?ato When trying to load or unload FRAME_SCRIPT into a process we should check if we have messageManager available to do the work or error accordingly. MozReview-Commit-ID: 1xonnY34vI3
testing/marionette/driver.js
testing/marionette/harness/marionette/tests/unit/test_screenshot.py
--- a/testing/marionette/driver.js
+++ b/testing/marionette/driver.js
@@ -331,37 +331,36 @@ GeckoDriver.prototype.startBrowser = fun
  * Loads the Marionette frame script into the browser if needed.
  *
  * @param {nsIDOMWindow} win
  *     Window whose browser we need to access.
  * @param {boolean} isNewSession
  *     True if this is the first time we're talking to this browser.
  */
 GeckoDriver.prototype.whenBrowserStarted = function(win, isNewSession) {
-  try {
-    let mm = win.window.messageManager;
+  let mm = win.window.messageManager;
+  if (mm) {
     if (!isNewSession) {
       // Loading the frame script corresponds to a situation we need to
       // return to the server. If the messageManager is a message broadcaster
       // with no children, we don't have a hope of coming back from this call,
       // so send the ack here. Otherwise, make a note of how many child scripts
       // will be loaded so we known when it's safe to return.
       if (mm.childCount !== 0) {
         this.curBrowser.frameRegsPending = mm.childCount;
       }
     }
 
     if (!Preferences.get(CONTENT_LISTENER_PREF) || !isNewSession) {
       mm.loadFrameScript(FRAME_SCRIPT, true, true);
       Preferences.set(CONTENT_LISTENER_PREF, true);
     }
-  } catch (e) {
-    // there may not always be a content process
+  } else {
     logger.error(
-        `Could not load listener into content for page ${win.location.href}: ${e}`);
+        `Could not load listener into content for page ${win.location.href}`);
   }
 };
 
 /**
  * Recursively get all labeled text.
  *
  * @param {nsIDOMElement} el
  *     The parent element.
@@ -2267,17 +2266,23 @@ GeckoDriver.prototype.sessionTearDown = 
       for (let i in browser.knownFrames) {
         globalMessageManager.broadcastAsyncMessage(
             "Marionette:deleteSession" + browser.knownFrames[i], {});
       }
     }
 
     let winEn = Services.wm.getEnumerator(null);
     while (winEn.hasMoreElements()) {
-      winEn.getNext().messageManager.removeDelayedFrameScript(FRAME_SCRIPT);
+      let win = winEn.getNext();
+      if (win.messageManager){
+        win.messageManager.removeDelayedFrameScript(FRAME_SCRIPT);
+      } else {
+        logger.error(
+            `Could not remove listener from page ${win.location.href}`);
+      }
     }
 
     this.curBrowser.frameManager.removeMessageManagerListeners(
         globalMessageManager);
   }
 
   this.switchToGlobalMessageManager();
 
--- a/testing/marionette/harness/marionette/tests/unit/test_screenshot.py
+++ b/testing/marionette/harness/marionette/tests/unit/test_screenshot.py
@@ -1,16 +1,17 @@
 # This Source Code Form is subject to the terms of the Mozilla Public
 # License, v. 2.0. If a copy of the MPL was not distributed with this
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 import base64
 import hashlib
 import imghdr
 import struct
+import time
 import urllib
 
 from unittest import skip
 
 from marionette import MarionetteTestCase
 from marionette_driver.by import By
 
 
@@ -87,16 +88,20 @@ class Chrome(ScreenCaptureTestCase):
     def test_secondary_windows(self):
         ss = self.marionette.screenshot()
         self.marionette.execute_script("""
             window.open('chrome://marionette/content/doesnotexist.xul',
             'foo',
             'chrome');
             """)
         self.marionette.switch_to_window("foo")
+        # there can be a race between opening and registering the window
+        # and switching to it. Waiting a tiny amount of time is enough not to
+        # break anything.
+        time.sleep(0.002)
         ss = self.marionette.screenshot()
         size = self.get_image_dimensions(ss)
         self.assert_png(ss)
         self.assertNotEqual(self.primary_window_dimensions, size)
 
 
 class Content(ScreenCaptureTestCase):
     @property
@@ -193,9 +198,9 @@ class Content(ScreenCaptureTestCase):
         with self.assertRaises(ValueError):
             self.marionette.screenshot(format="cheese")
 
     def test_hash_format(self):
         self.marionette.navigate(box)
         el = self.marionette.find_element(By.TAG_NAME, "div")
         content = self.marionette.screenshot(element=el, format="hash")
         hash = hashlib.sha256(ELEMENT).hexdigest()
-        self.assertEqual(content, hash)
\ No newline at end of file
+        self.assertEqual(content, hash)