Bug 1261233 - Prevent test_bug331215.xul from intermittently timing out. r=Gijs, a=test-only
authorMike de Boer <mdeboer@mozilla.com>
Sat, 11 Feb 2017 00:08:15 +0100
changeset 378415 b1aabc07a3877a7036b85adedc7e9a69bea95f97
parent 378414 b7af012f09fe2b6d09b73e25d864ef99bcf35612
child 378416 666413533f5acf0f19b2535f41cff5dcfc78f7cc
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)
reviewersGijs, test-only
bugs1261233, 331215
milestone53.0a2
Bug 1261233 - Prevent test_bug331215.xul from intermittently timing out. r=Gijs, a=test-only MozReview-Commit-ID: 4dvWUdNTBOx
toolkit/content/tests/chrome/bug331215_window.xul
--- a/toolkit/content/tests/chrome/bug331215_window.xul
+++ b/toolkit/content/tests/chrome/bug331215_window.xul
@@ -14,16 +14,17 @@
         width="600"
         height="600"
         onload="SimpleTest.executeSoon(startTest);"
         title="331215 test">
 
   <script type="application/javascript"><![CDATA[
     const {interfaces: Ci, classes: Cc, results: Cr, utils: Cu} = Components;
     Cu.import("resource://gre/modules/Task.jsm");
+    Cu.import("resource://testing-common/BrowserTestUtils.jsm");
     Cu.import("resource://testing-common/ContentTask.jsm");
     ContentTask.setTestScope(window.opener.wrappedJSObject);
 
     var gFindBar = null;
     var gBrowser;
 
     var imports = ["SimpleTest", "ok", "info"];
     for (var name of imports) {
@@ -42,62 +43,58 @@
         SimpleTest.finish();
       });
     }
 
     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();
-          });
-        });
-      });
+      let promise = BrowserTestUtils.browserLoaded(gBrowser);
       gBrowser.loadURI("data:text/plain,latest");
       yield promise;
       yield onDocumentLoaded();
     }
 
     function* onDocumentLoaded() {
       document.getElementById("cmd_find").doCommand();
-      yield enterStringIntoFindField("test");
+      yield promiseEnterStringIntoFindField("test");
       document.commandDispatcher
               .getControllerForCommand("cmd_moveTop")
               .doCommand("cmd_moveTop");
-      yield enterStringIntoFindField("l");
+      yield promiseEnterStringIntoFindField("l");
       ok(gFindBar._findField.getAttribute("status") == "notfound",
-         "Findfield status attribute should have been 'notfound'" +
-         " after entering ltest");              
-      yield enterStringIntoFindField("a");
+         "Findfield status attribute should have been 'notfound' after entering test");
+      yield promiseEnterStringIntoFindField("a");
       ok(gFindBar._findField.getAttribute("status") != "notfound",
-         "Findfield status attribute should not have been 'notfound'" +
-         " after entering latest");
+         "Findfield status attribute should not have been 'notfound' after entering latest");
     }
 
-    function* enterStringIntoFindField(aString) {
-      for (let i = 0; i < aString.length; i++) {      
-        let event = document.createEvent("KeyboardEvent");
-        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;
-      }
+    function promiseEnterStringIntoFindField(aString) {
+      return new Promise(resolve => {
+        let listener = {
+          onFindResult: function(result) {
+            if (result.result == Ci.nsITypeAheadFind.FIND_FOUND && result.searchString != aString)
+              return;
+            gFindBar.browser.finder.removeResultListener(listener);
+            resolve();
+          }
+        };
+        gFindBar.browser.finder.addResultListener(listener);
+
+        for (let c of aString) {
+          let code = c.charCodeAt(0);
+          let ev = new KeyboardEvent("keypress", {
+            keyCode: code,
+            charCode: code,
+            bubbles: true
+          });
+          gFindBar._findField.inputField.dispatchEvent(ev);
+        }
+      });
     }
   ]]></script>
 
   <commandset>
     <command id="cmd_find" oncommand="document.getElementById('FindToolbar').onFindCommand();"/>
   </commandset>
   <browser type="content" primary="true" flex="1" id="content" src="about:blank"/>
   <browser type="content" primary="true" flex="1" id="content-remote" remote="true" src="about:blank"/>