Bug 1348872 - Add contentBrowser property to browser's Context class. draft
authorHenrik Skupin <mail@hskupin.info>
Mon, 27 Mar 2017 11:02:36 +0200
changeset 552448 82d8355fbf5675216eb7717916b9941616ed2e4b
parent 551502 cc53710589fb500610495da5258b7b9221edf681
child 552449 965734dc018665f4b1f44dfa41800542ef2d0b6c
child 552695 f0d8ecd5aaeacfb655609400d31c322bc247203a
child 552699 6b0d43b9d891fd7173efc548d081756146d12b1c
push id51344
push userbmo:hskupin@gmail.com
push dateTue, 28 Mar 2017 12:28:51 +0000
bugs1348872
milestone55.0a1
Bug 1348872 - Add contentBrowser property to browser's Context class. Make it easier to access the content browser for the currently selected tab by not requiring to go through the global getBrowserForTab() method. MozReview-Commit-ID: 3zdCGJFanto
testing/marionette/browser.js
testing/marionette/driver.js
--- a/testing/marionette/browser.js
+++ b/testing/marionette/browser.js
@@ -120,42 +120,54 @@ browser.Context = class {
     this.getIdForBrowser = driver.getIdForBrowser.bind(driver);
     this.updateIdForBrowser = driver.updateIdForBrowser.bind(driver);
     this._curFrameId = null;
     this._browserWasRemote = null;
     this._hasRemotenessChange = false;
   }
 
   /**
+   * Returns the content browser for the currently selected tab.
+   * If there is no tab selected, null will be returned.
+   */
+  get contentBrowser() {
+    if (this.tab) {
+      return browser.getBrowserForTab(this.tab);
+    }
+
+    return null;
+  }
+
+  /**
    * The current frame ID is managed per browser element on desktop in
    * case the ID needs to be refreshed. The currently selected window is
    * identified by a tab.
    */
   get curFrameId() {
     let rv = null;
     if (this.driver.appName == "B2G") {
       rv = this._curFrameId;
     } else if (this.tab) {
-      rv = this.getIdForBrowser(browser.getBrowserForTab(this.tab));
+      rv = this.getIdForBrowser(this.contentBrowser);
     }
     return rv;
   }
 
   set curFrameId(id) {
     if (this.driver.appName != "Firefox") {
       this._curFrameId = id;
     }
   }
 
   /**
    * Retrieves the current tabmodal UI object.  According to the browser
    * associated with the currently selected tab.
    */
   getTabModalUI() {
-    let br = browser.getBrowserForTab(this.tab);
+    let br = this.contentBrowser;
     if (!br.hasAttribute("tabmodalPromptShowing")) {
       return null;
     }
 
     // The modal is a direct sibling of the browser element.
     // See tabbrowser.xml's getTabModalPromptBox.
     let modals = br.parentNode.getElementsByTagNameNS(
         XUL_NS, "tabmodalprompt");
@@ -271,17 +283,17 @@ browser.Context = class {
 
         } else {
           throw new UnsupportedOperationError("switchToTab() not supported");
         }
       }
     }
 
     if (this.driver.appName == "Firefox") {
-      this._browserWasRemote = browser.getBrowserForTab(this.tab).isRemoteBrowser;
+      this._browserWasRemote = this.contentBrowser.isRemoteBrowser;
       this._hasRemotenessChange = false;
     }
   }
 
   /**
    * Registers a new frame, and sets its current frame id to this frame
    * if it is not already assigned, and if a) we already have a session
    * or b) we're starting a new session and it is the right start frame.
@@ -295,18 +307,18 @@ browser.Context = class {
     if (this.curFrameId === null || remotenessChange) {
       if (this.tabBrowser) {
         // If we're setting up a new session on Firefox, we only process the
         // registration for this frame if it belongs to the current tab.
         if (!this.tab) {
           this.switchToTab();
         }
 
-        if (target == browser.getBrowserForTab(this.tab)) {
-          this.updateIdForBrowser(browser.getBrowserForTab(this.tab), uid);
+        if (target === this.contentBrowser) {
+          this.updateIdForBrowser(this.contentBrowser, uid);
           this.mainContentId = uid;
         }
       } else {
         this._curFrameId = uid;
         this.mainContentId = uid;
       }
     }
 
@@ -320,25 +332,25 @@ browser.Context = class {
    * process, we need to take measures not to lose contact with a listener
    * script. This function does the necessary bookkeeping.
    */
   hasRemotenessChange() {
     // None of these checks are relevant on b2g or if we don't have a tab yet,
     // and may not apply on Fennec.
     if (this.driver.appName != "Firefox" ||
         this.tab === null ||
-        browser.getBrowserForTab(this.tab) === null) {
+        this.contentBrowser === null) {
       return false;
     }
 
     if (this._hasRemotenessChange) {
       return true;
     }
 
-    let currentIsRemote = browser.getBrowserForTab(this.tab).isRemoteBrowser;
+    let currentIsRemote = this.contentBrowser.isRemoteBrowser;
     this._hasRemotenessChange = this._browserWasRemote !== currentIsRemote;
     this._browserWasRemote = currentIsRemote;
     return this._hasRemotenessChange;
   }
 
   /**
    * Flushes any pending commands queued when a remoteness change is being
    * processed and mark this remotenessUpdate as complete.
--- a/testing/marionette/driver.js
+++ b/testing/marionette/driver.js
@@ -333,17 +333,17 @@ GeckoDriver.prototype.getCurrentWindow =
 
     case Context.CONTENT:
       if (this.curFrame !== null) {
         win = this.curFrame;
 
       } else if (this.curBrowser !== null) {
         if (browser.getTabBrowser(this.curBrowser.window)) {
           // For browser windows we have to check if the current tab still exists.
-          if (this.curBrowser.tab && browser.getBrowserForTab(this.curBrowser.tab)) {
+          if (this.curBrowser.tab && this.curBrowser.contentBrowser) {
             win = this.curBrowser.window;
           }
         } else {
           // For non-browser windows just return the window.
           win = this.curBrowser.window;
         }
       }
 
@@ -654,17 +654,17 @@ GeckoDriver.prototype.newSession = funct
     throw new WebDriverError("Session already running");
   }
   this.switchToGlobalMessageManager();
 
   yield registerBrowsers;
   yield browserListening;
 
   if (this.curBrowser.tab) {
-    browser.getBrowserForTab(this.curBrowser.tab).focus();
+    this.curBrowser.contentBrowser.focus();
   }
 
   return {
     sessionId: this.sessionId,
     capabilities: this.capabilities,
   };
 };
 
@@ -980,17 +980,18 @@ GeckoDriver.prototype.get = function*(cm
       startTime: new Date().getTime(),
     };
     this.mm.broadcastAsyncMessage(
         "Marionette:pollForReadyState" + this.curBrowser.curFrameId,
         parameters);
   });
 
   yield get;
-  browser.getBrowserForTab(this.curBrowser.tab).focus();
+
+  this.curBrowser.contentBrowser.focus();
 };
 
 /**
  * Get a string representing the current URL.
  *
  * On Desktop this returns a string representation of the URL of the
  * current top level browsing context.  This is equivalent to
  * document.location.href.
@@ -1056,18 +1057,17 @@ GeckoDriver.prototype.goBack = function*
   assert.content(this.context);
   assert.window(this.getCurrentWindow());
 
   if (!this.curBrowser.tab) {
     // Navigation does not work for non-browser windows
     return;
   }
 
-  let contentBrowser = browser.getBrowserForTab(this.curBrowser.tab)
-  if (!contentBrowser.webNavigation.canGoBack) {
+  if (!this.curBrowser.contentBrowser.webNavigation.canGoBack) {
     return;
   }
 
   let currentURL = yield this.listener.getCurrentUrl();
   let goBack = this.listener.goBack({pageTimeout: this.timeouts.pageLoad});
 
   // If a remoteness update interrupts our page load, this will never return
   // We need to re-issue this request to correctly poll for readyState and
@@ -1098,18 +1098,17 @@ GeckoDriver.prototype.goForward = functi
   assert.content(this.context);
   assert.window(this.getCurrentWindow());
 
   if (!this.curBrowser.tab) {
     // Navigation does not work for non-browser windows
     return;
   }
 
-  let contentBrowser = browser.getBrowserForTab(this.curBrowser.tab)
-  if (!contentBrowser.webNavigation.canGoForward) {
+  if (!this.curBrowser.contentBrowser.webNavigation.canGoForward) {
     return;
   }
 
   let currentURL = yield this.listener.getCurrentUrl();
   let goForward = this.listener.goForward({pageTimeout: this.timeouts.pageLoad});
 
   // If a remoteness update interrupts our page load, this will never return
   // We need to re-issue this request to correctly poll for readyState and