Bug 1261584: refactor bug360437_window.xul and findbar_events_window.xul to run the test on remote browsers as well. r=felipe
authorMike de Boer <mdeboer@mozilla.com>
Thu, 14 Apr 2016 13:06:47 +0200
changeset 331055 0d2ef08c72bb36dee98608bf394eed628a80e0c0
parent 331054 4a496faf1c5cac37ab6dac9db549cb65867fa7f6
child 331056 e7f78bd4bb2296c0130a6e9e92ec4bcf203dee84
push id6048
push userkmoir@mozilla.com
push dateMon, 06 Jun 2016 19:02:08 +0000
treeherdermozilla-beta@46d72a56c57d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersfelipe
bugs1261584
milestone48.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1261584: refactor bug360437_window.xul and findbar_events_window.xul to run the test on remote browsers as well. r=felipe
toolkit/content/tests/chrome/bug360437_window.xul
toolkit/content/tests/chrome/chrome.ini
toolkit/content/tests/chrome/findbar_events_window.xul
--- a/toolkit/content/tests/chrome/bug360437_window.xul
+++ b/toolkit/content/tests/chrome/bug360437_window.xul
@@ -1,86 +1,119 @@
 <?xml version="1.0"?>
 
 <!-- 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/. -->
 
 <?xml-stylesheet href="chrome://global/skin" type="text/css"?>
+<?xml-stylesheet
+  href="chrome://mochikit/content/tests/SimpleTest/test.css"
+  type="text/css"?>
 
 <window id="360437Test"
         xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
         width="600"
         height="600"
-        onload="onLoad();"
+        onload="startTest();"
         title="360437 test">
 
   <script type="application/javascript"><![CDATA[
-    const Ci = Components.interfaces;
-    const Cc = Components.classes;
-    const Cr = Components.results;
+    const {interfaces: Ci, classes: Cc, results: Cr, utils: Cu} = Components;
+    Cu.import("resource://gre/modules/Task.jsm");
+    Cu.import("resource://testing-common/ContentTask.jsm");
+    ContentTask.setTestScope(window.opener.wrappedJSObject);
 
     var gFindBar = null;
     var gBrowser;
 
-    function ok(condition, message) {
-      window.opener.wrappedJSObject.SimpleTest.ok(condition, message);
+    var imports = ["SimpleTest", "ok", "is", "info"];
+    for (var name of imports) {
+      window[name] = window.opener.wrappedJSObject[name];
     }
-    function finish() {
-      window.close();
-      window.opener.wrappedJSObject.SimpleTest.finish();
+
+    function startTest() {
+      Task.spawn(function* () {
+        gFindBar = document.getElementById("FindToolbar");
+        for (let browserId of ["content", "content-remote"]) {
+          yield startTestWithBrowser(browserId);
+        }
+      }).then(() => {
+        window.close();
+        SimpleTest.finish();
+      });
     }
 
-    function onLoad() {
-      var _delayedOnLoad = function() {
-        gFindBar = document.getElementById("FindToolbar");
-        gBrowser = document.getElementById("content");
-        gBrowser.addEventListener("pageshow", onPageShow, false);
-        gBrowser.loadURI("data:text/html,<form><input id='input' type='text' value='text inside an input element'></form>");
-      }
-      setTimeout(_delayedOnLoad, 1000);
+    function* startTestWithBrowser(browserId) {
+      info("Starting test with browser '" + browserId + "'");
+      gBrowser = document.getElementById(browserId);
+      gFindBar.browser = gBrowser;
+      let promise = ContentTask.spawn(gBrowser, null, function* () {
+        return new Promise(resolve => {
+          addEventListener("DOMContentLoaded", function listener() {
+            removeEventListener("DOMContentLoaded", listener);
+            resolve();
+          });
+        });
+      });
+      gBrowser.loadURI("data:text/html,<form><input id='input' type='text' value='text inside an input element'></form>");
+      yield promise;
+      yield onDocumentLoaded();
     }
 
-    function onPageShow() {
-      testNormalFind();
-    }
-
-    function enterStringIntoFindField(aString) {
-      for (var i=0; i < aString.length; i++) {
-        var event = document.createEvent("KeyEvents");
-        event.initKeyEvent("keypress", true, true, null, false, false,
-                           false, false, 0, aString.charCodeAt(i));
-        gFindBar._findField.inputField.dispatchEvent(event);
-      }
-    }
-
-    function testNormalFind() {
+    function* onDocumentLoaded() {
       gFindBar.onFindCommand();
 
       // Make sure the findfield is correctly focused on open
       var searchStr = "text inside an input element";
-      enterStringIntoFindField(searchStr);
-      ok(document.commandDispatcher.focusedElement ==
+      yield* enterStringIntoFindField(searchStr);
+      is(document.commandDispatcher.focusedElement,
          gFindBar._findField.inputField, "Find field isn't focused");
 
       // Make sure "find again" correctly transfers focus to the content element
       // when the find bar is closed.
       gFindBar.close();
       gFindBar.onFindAgainCommand(false);
-      ok(document.commandDispatcher.focusedElement ==
-         gBrowser.contentDocument.getElementById("input"),
-             "Input Element isn't focused");
+      // For remote browsers, the content document DOM tree is not accessible, thus
+      // the focused element should fall back to the browser element.
+      if (gBrowser.hasAttribute("remote")) {
+        is(document.commandDispatcher.focusedElement, gBrowser,
+          "Browser element isn't focused");
+      }
+      yield ContentTask.spawn(gBrowser, null, function* () {
+        Assert.equal(content.document.activeElement,
+         content.document.getElementById("input"), "Input Element isn't focused");
+      });
 
       // Make sure "find again" doesn't focus the content element if focus
       // isn't in the content document.
       var textbox = document.getElementById("textbox");
       textbox.focus();
       gFindBar.close();
       gFindBar.onFindAgainCommand(false);
       ok(textbox.hasAttribute("focused"),
          "Focus was stolen from a chrome element");
-      finish();
+    }
+
+    function* enterStringIntoFindField(aString) {
+      for (let i = 0; i < aString.length; i++) {
+        let event = document.createEvent("KeyEvents");
+        let promise = new Promise(resolve => {
+          let listener = {
+            onFindResult: function() {
+              gFindBar.browser.finder.removeResultListener(listener);
+              resolve();
+            }
+          };
+          gFindBar.browser.finder.addResultListener(listener);
+        });
+        event.initKeyEvent("keypress", true, true, null, false, false,
+                           false, false, 0, aString.charCodeAt(i));
+        gFindBar._findField.inputField.dispatchEvent(event);
+        yield promise;
+      }
     }
   ]]></script>
   <textbox id="textbox"/>
   <browser type="content-primary" flex="1" id="content" src="about:blank"/>
+  <browser type="content-primary" flex="1" id="content-remote" remote="true" src="about:blank"/>
   <findbar id="FindToolbar" browserid="content"/>
 </window>
--- a/toolkit/content/tests/chrome/chrome.ini
+++ b/toolkit/content/tests/chrome/chrome.ini
@@ -63,16 +63,17 @@ support-files =
 [test_browser_drop.xul]
 skip-if = buildapp == 'mulet'
 [test_bug253481.xul]
 [test_bug263683.xul]
 [test_bug304188.xul]
 [test_bug331215.xul]
 [test_bug360220.xul]
 [test_bug360437.xul]
+skip-if = os == 'linux'
 [test_bug365773.xul]
 [test_bug366992.xul]
 [test_bug382990.xul]
 [test_bug409624.xul]
 [test_bug418874.xul]
 [test_bug429723.xul]
 [test_bug437844.xul]
 [test_bug457632.xul]
--- a/toolkit/content/tests/chrome/findbar_events_window.xul
+++ b/toolkit/content/tests/chrome/findbar_events_window.xul
@@ -1,164 +1,179 @@
 <?xml version="1.0"?>
 
 <!-- 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/. -->
 
 <?xml-stylesheet href="chrome://global/skin" type="text/css"?>
+<?xml-stylesheet
+  href="chrome://mochikit/content/tests/SimpleTest/test.css"
+  type="text/css"?>
 
 <window id="FindbarTest"
         xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
         width="600"
         height="600"
         onload="SimpleTest.executeSoon(startTest);"
         title="findbar events test">
 
   <script type="application/javascript"><![CDATA[
-    const Ci = Components.interfaces;
-    const Cc = Components.classes;
-    const Cr = Components.results;
+    const {interfaces: Ci, classes: Cc, results: Cr, utils: Cu} = Components;
+    Cu.import("resource://gre/modules/Task.jsm");
+    Cu.import("resource://testing-common/ContentTask.jsm");
+    ContentTask.setTestScope(window.opener.wrappedJSObject);
 
     var gFindBar = null;
     var gBrowser;
+    const kTimeout = 5000; // 5 seconds.
 
-    var imports = ["SimpleTest", "ok", "is"];
+    var imports = ["SimpleTest", "ok", "is", "info"];
     for (var name of imports) {
       window[name] = window.opener.wrappedJSObject[name];
     }
-
-    function finish() {
-      window.close();
-      SimpleTest.finish();
-    }
+    SimpleTest.requestLongerTimeout(2);
 
     function startTest() {
-      gFindBar = document.getElementById("FindToolbar");
-      gBrowser = document.getElementById("content");
-      gBrowser.addEventListener("pageshow", onPageShow, false);
-      gBrowser.loadURI('data:text/html,hello there');
+      Task.spawn(function* () {
+        gFindBar = document.getElementById("FindToolbar");
+        for (let browserId of ["content", "content-remote"]) {
+          yield startTestWithBrowser(browserId);
+        }
+      }).then(() => {
+        window.close();
+        SimpleTest.finish();
+      });
     }
 
-    var tests = [
-      testFind,
-      testFindAgain,
-      testCaseSensitivity,
-      testHighlight,
-      finish
-    ];
-
-    // Iterates through the above tests and takes care of passing the done
-    // callback for any async tests.
-    function nextTest() {
-      if (!tests.length) {
-        return;
-      }
-      var func = tests.shift();
-      if (!func.length) {
-        // Test isn't async advance to the next test here.
-        func();
-        SimpleTest.executeSoon(nextTest);
-      } else {
-        func(nextTest);
-      }
+    function* startTestWithBrowser(browserId) {
+      info("Starting test with browser '" + browserId + "'");
+      gBrowser = document.getElementById(browserId);
+      gFindBar.browser = gBrowser;
+      let promise = ContentTask.spawn(gBrowser, null, function* () {
+        return new Promise(resolve => {
+          addEventListener("DOMContentLoaded", function listener() {
+            removeEventListener("DOMContentLoaded", listener);
+            resolve();
+          });
+        });
+      });
+      gBrowser.loadURI("data:text/html,hello there");
+      yield promise;
+      yield onDocumentLoaded();
     }
 
-    function onPageShow() {
+    function* onDocumentLoaded() {
       gFindBar.open();
       gFindBar.onFindCommand();
-      nextTest();
+
+      yield testFind();
+      yield testFindAgain();
+      yield testCaseSensitivity();
+      yield testHighlight();
     }
 
-    function checkSelection(done) {
-      SimpleTest.executeSoon(function() {
-        var selected = gBrowser.contentWindow.getSelection();
-        is(String(selected), "", "No text is selected");
+    function checkSelection() {
+      return new Promise(resolve => {
+        SimpleTest.executeSoon(() => {
+          ContentTask.spawn(gBrowser, null, function* () {
+            let selected = content.getSelection();
+            Assert.equal(String(selected), "", "No text is selected");
 
-        var controller = gFindBar.browser.docShell.QueryInterface(Ci.nsIInterfaceRequestor)
-                                 .getInterface(Ci.nsISelectionDisplay)
-                                 .QueryInterface(Ci.nsISelectionController);
-        var selection = controller.getSelection(controller.SELECTION_FIND);
-        is(selection.rangeCount, 0, "No text is highlighted");
-        done();
+            let controller = docShell.QueryInterface(Ci.nsIInterfaceRequestor)
+                                     .getInterface(Ci.nsISelectionDisplay)
+                                     .QueryInterface(Ci.nsISelectionController);
+            let selection = controller.getSelection(controller.SELECTION_FIND);
+            Assert.equal(selection.rangeCount, 0, "No text is highlighted");
+          }).then(resolve);
+        });
       });
     }
 
-    function once(node, eventName, callback) {
-      node.addEventListener(eventName, function clb(e) {
-        node.removeEventListener(eventName, clb);
-        callback(e);
-      })
+    function once(node, eventName, preventDefault = true) {
+      return new Promise((resolve, reject) => {
+        let timeout = window.setTimeout(() => {
+          reject("Event wasn't fired within " + kTimeout + "ms for event '" +
+            eventName + "'.");
+        }, kTimeout);
+
+        node.addEventListener(eventName, function clb(e) {
+          window.clearTimeout(timeout);
+          node.removeEventListener(eventName, clb);
+          if (preventDefault)
+            e.preventDefault();
+          resolve(e);
+        });
+      });
     }
 
-    function testFind(done) {
-      var eventTriggered = false;
-      var query = "t";
-      once(gFindBar, "find", function(e) {
-        eventTriggered = true;
-        ok(e.detail.query === query, "find event query should match '" + query + "'");
-        e.preventDefault();
-        // Since we're preventing the default make sure nothing was selected.
-        checkSelection(done);
-      });
+    function* testFind() {
+      info("Testing normal find.");
+      let query = "t";
+      let promise = once(gFindBar, "find");
 
       // Put some text in the find box.
-      var event = document.createEvent("KeyEvents");
+      let event = document.createEvent("KeyEvents");
       event.initKeyEvent("keypress", true, true, null, false, false,
                          false, false, 0, query.charCodeAt(0));
       gFindBar._findField.inputField.dispatchEvent(event);
-      ok(eventTriggered, "find event should be triggered");
+
+      let e = yield promise;
+      ok(e.detail.query === query, "find event query should match '" + query + "'");
+      // Since we're preventing the default make sure nothing was selected.
+      yield checkSelection();
     }
 
-    function testFindAgain(done) {
-      var eventTriggered = false;
-      once(gFindBar, "findagain", function(e) {
-        eventTriggered = true;
-        e.preventDefault();
-        // Since we're preventing the default make sure nothing was selected.
-        checkSelection(done);
-      });
+    function testFindAgain() {
+      info("Testing repeating normal find.");
+      let promise = once(gFindBar, "findagain");
 
       gFindBar.onFindAgainCommand();
-      ok(eventTriggered, "findagain event should be triggered");
+
+      yield promise;
+      // Since we're preventing the default make sure nothing was selected.
+      yield checkSelection();
     }
 
-    function testCaseSensitivity() {
-      var eventTriggered = false;
-      once(gFindBar, "findcasesensitivitychange", function(e) {
-        eventTriggered = true;
-        ok(e.detail.caseSensitive, "find should be case sensitive");
-      });
+    function* testCaseSensitivity() {
+      info("Testing normal case sensitivity.");
+      let promise = once(gFindBar, "findcasesensitivitychange", false);
 
-      var matchCaseCheckbox = gFindBar.getElement("find-case-sensitive");
+      let matchCaseCheckbox = gFindBar.getElement("find-case-sensitive");
       matchCaseCheckbox.click();
-      ok(eventTriggered, "findcasesensitivitychange should be triggered");
+
+      let e = yield promise;
+      ok(e.detail.caseSensitive, "find should be case sensitive");
+
+      // Toggle it back to the original setting.
+      matchCaseCheckbox.click();
 
       // Changing case sensitivity does the search so clear the selected text
       // before the next test.
-      gBrowser.contentWindow.getSelection().removeAllRanges();
+      yield ContentTask.spawn(gBrowser, null, () => content.getSelection().removeAllRanges());
     }
 
-    function testHighlight(done) {
+    function* testHighlight() {
+      info("Testing find with highlight all.");
       // Update the find state so the highlight button is clickable.
       gFindBar.updateControlState(Ci.nsITypeAheadFind.FIND_FOUND, false);
-      var eventTriggered = false;
-      once(gFindBar, "findhighlightallchange", function(e) {
-        eventTriggered = true;
-        ok(e.detail.highlightAll, "find event should have highlight all set");
-        e.preventDefault();
-        // Since we're preventing the default make sure nothing was highlighted.
-        SimpleTest.executeSoon(function() {
-          checkSelection(done);
-        });
-      });
+
+      let promise = once(gFindBar, "findhighlightallchange");
+
+      let highlightButton = gFindBar.getElement("highlight");
+      if (!highlightButton.checked)
+        highlightButton.click();
 
-      var highlightButton = gFindBar.getElement("highlight");
-      if (!highlightButton.checked) {
+      let e = yield promise;
+      ok(e.detail.highlightAll, "find event should have highlight all set");
+      // Since we're preventing the default make sure nothing was highlighted.
+      yield checkSelection();
+
+      // Toggle it back to the original setting.
+      if (highlightButton.checked)
         highlightButton.click();
-      }
-      ok(eventTriggered, "findhighlightallchange should be triggered");
     }
   ]]></script>
 
   <browser type="content-primary" flex="1" id="content" src="about:blank"/>
+  <browser type="content-primary" flex="1" id="content-remote" remote="true" src="about:blank"/>
   <findbar id="FindToolbar" browserid="content"/>
 </window>