Bug 850349 - Cleanup browser search tests while debugging intermittent failure in browser_contextmenu.js. r=gavin
authorMatthew Noorenberghe <mozilla@noorenberghe.ca>
Wed, 13 Mar 2013 19:23:03 -0700
changeset 124775 b672877ed04656a9f4041f31ef5d8030df662726
parent 124739 8e68f4d73ec4668cf8d284c03b6688c29d9e964f
child 124776 7c406c24a3aebb557691e2485583a7be6c4996c1
child 124839 7036707114197511489a9e080bcc41f3ed10c07e
push id24596
push userphilringnalda@gmail.com
push dateThu, 14 Mar 2013 03:49:53 +0000
treeherdermozilla-inbound@7c406c24a3ae [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersgavin
bugs850349
milestone22.0a1
first release with
nightly linux32
b672877ed046 / 22.0a1 / 20130314030914 / files
nightly linux64
b672877ed046 / 22.0a1 / 20130314030914 / files
nightly mac
b672877ed046 / 22.0a1 / 20130314030914 / files
nightly win32
b672877ed046 / 22.0a1 / 20130314030914 / files
nightly win64
b672877ed046 / 22.0a1 / 20130314030914 / files
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
releases
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 850349 - Cleanup browser search tests while debugging intermittent failure in browser_contextmenu.js. r=gavin * browser_426329.js - Fix exception in popupshowing event listener causing search history dropdown to remain open on subsequent tests. * browser_contextmenu.js - Add some debug messages. - Move |doOnloadOnce(checkSearchURL);| just before the search tab opens so there is less interference with later tests. - Add nsISelectionListener to know when selection has finished. * head.js - Consolidate duplicated doOnloadOnce function. * test.html - Fix character set warning.
browser/components/search/test/browser_426329.js
browser/components/search/test/browser_contextmenu.js
browser/components/search/test/head.js
browser/components/search/test/test.html
--- a/browser/components/search/test/browser_426329.js
+++ b/browser/components/search/test/browser_426329.js
@@ -196,68 +196,61 @@ function test() {
       let exists = textbox._formHistSvc.entryExists(textbox.getAttribute("autocompletesearchparam"), searchEntries[i]);
       ok(exists, "form history entry '" + searchEntries[i] + "' should exist");
     }
     testAutocomplete();
   }
 
   function testAutocomplete() {
     var popup = searchBar.textbox.popup;
-    popup.addEventListener("popupshowing", function() {
+    popup.addEventListener("popupshowing", function testACPopupShowing() {
+      popup.removeEventListener("popupshowing", testACPopupShowing);
       checkMenuEntries(searchEntries);
-      finalize();
-      popup.removeEventListener("popupshowing", this, false);
-    }, false);
+      SimpleTest.executeSoon(finalize);
+    });
     searchBar.textbox.showHistoryPopup();
   }
 
   function finalize() {
     searchBar.value = "";
     while (gBrowser.tabs.length != 1) {
       gBrowser.removeTab(gBrowser.tabs[0]);
     }
     content.location.href = "about:blank";
     var engine = ss.getEngineByName("Bug 426329");
     ss.removeEngine(engine);
   }
 
-  function doOnloadOnce(callback) {
-    gBrowser.addEventListener("DOMContentLoaded", function(event) {
-      gBrowser.removeEventListener("DOMContentLoaded", arguments.callee, true);
-      callback(event);
-    }, true);
-  }
-
   function simulateClick(aEvent, aTarget) {
     var event = document.createEvent("MouseEvent");
     var ctrlKeyArg  = aEvent.ctrlKey  || false;
     var altKeyArg   = aEvent.altKey   || false;
     var shiftKeyArg = aEvent.shiftKey || false;
     var metaKeyArg  = aEvent.metaKey  || false;
     var buttonArg   = aEvent.button   || 0;
     event.initMouseEvent("click", true, true, window,
                           0, 0, 0, 0, 0,
                           ctrlKeyArg, altKeyArg, shiftKeyArg, metaKeyArg,
-                          buttonArg, null); 
+                          buttonArg, null);
     aTarget.dispatchEvent(event);
   }
 
   function expectedURL(aSearchTerms) {
     var textToSubURI = Cc["@mozilla.org/intl/texttosuburi;1"].
                        getService(Ci.nsITextToSubURI);
     var searchArg = textToSubURI.ConvertAndEscape("utf-8", aSearchTerms);
     return ENGINE_HTML_BASE + "?test=" + searchArg;
   }
 
   // modified from toolkit/components/satchel/test/test_form_autocomplete.html
   function checkMenuEntries(expectedValues) {
     var actualValues = getMenuEntries();
     is(actualValues.length, expectedValues.length, "Checking length of expected menu");
     for (var i = 0; i < expectedValues.length; i++)
-      is(actualValues[i], expectedValues[i], "Checking menu entry #"+i);
+      is(actualValues[i], expectedValues[i], "Checking menu entry #" + i);
   }
 
   function getMenuEntries() {
     var entries = [];
     var autocompleteMenu = searchBar.textbox.popup;
     // Could perhaps pull values directly from the controller, but it seems
     // more reliable to test the values that are actually in the tree?
     var column = autocompleteMenu.tree.columns[0];
--- a/browser/components/search/test/browser_contextmenu.js
+++ b/browser/components/search/test/browser_contextmenu.js
@@ -25,74 +25,79 @@ function test() {
         break;
       case "engine-removed":
         Services.obs.removeObserver(observer, "browser-search-engine-modified");
         finish();
         break;
     }
   }
 
+  registerCleanupFunction(finalize);
   Services.obs.addObserver(observer, "browser-search-engine-modified", false);
   ss.addEngine("http://mochi.test:8888/browser/browser/components/search/test/testEngine_mozsearch.xml",
                Ci.nsISearchEngine.DATA_XML, "data:image/x-icon,%00",
                false);
 
   function startTest() {
     contextMenu = document.getElementById("contentAreaContextMenu");
     ok(contextMenu, "Got context menu XUL");
 
     doOnloadOnce(testContextMenu);
     var tab = gBrowser.addTab("data:text/plain;charset=utf8,test%20search");
     gBrowser.selectedTab = tab;
   }
 
   function testContextMenu() {
     function rightClickOnDocument() {
+      info("rightClickOnDocument: " + content.window.location);
       waitForBrowserContextMenu(checkContextMenu);
       var clickTarget = content.document.body;
       var eventDetails = { type: "contextmenu", button: 2 };
       EventUtils.synthesizeMouseAtCenter(clickTarget, eventDetails, content);
     }
 
     // check the search menu item and then perform a search
     function checkContextMenu() {
+      info("checkContextMenu");
       var searchItem = contextMenu.getElementsByAttribute("id", "context-searchselect")[0];
       ok(searchItem, "Got search context menu item");
       is(searchItem.label, 'Search ' + ENGINE_NAME + ' for "test search"', "Check context menu label");
       is(searchItem.disabled, false, "Check that search context menu item is enabled");
+      doOnloadOnce(checkSearchURL);
       searchItem.click();
       contextMenu.hidePopup();
     }
 
-    function checkSearchURL(event){
+    function checkSearchURL(event) {
       is(event.originalTarget.URL,
          "http://mochi.test:8888/browser/browser/components/search/test/?test=test+search&ie=utf-8&client=app&channel=contextsearch",
          "Checking context menu search URL");
       finalize();
     }
 
-    doOnloadOnce(checkSearchURL);
+    var selectionListener = {
+      notifySelectionChanged: function(doc, sel, reason) {
+        if (reason != Ci.nsISelectionListener.SELECTALL_REASON || sel.toString() != "test search")
+          return;
+        info("notifySelectionChanged: Text selected");
+        content.window.getSelection().QueryInterface(Ci.nsISelectionPrivate).
+                                      removeSelectionListener(selectionListener);
+        SimpleTest.executeSoon(rightClickOnDocument);
+      }
+    };
 
+    // add a listener to know when the selection takes effect
+    content.window.getSelection().QueryInterface(Ci.nsISelectionPrivate).
+                                  addSelectionListener(selectionListener);
     // select the text on the page
-    var selectAllItem = contextMenu.getElementsByAttribute("id", "context-selectall")[0];
-    ok(selectAllItem, "Got select all context menu item");
-    selectAllItem.click();
-
-    // wait for the selection to take effect
-    SimpleTest.executeSoon(rightClickOnDocument);
+    goDoCommand('cmd_selectAll');
   }
 
   function finalize() {
     while (gBrowser.tabs.length != 1) {
       gBrowser.removeTab(gBrowser.tabs[0]);
     }
     content.location.href = "about:blank";
     var engine = ss.getEngineByName(ENGINE_NAME);
-    ss.removeEngine(engine);
-  }
-
-  function doOnloadOnce(callback) {
-    gBrowser.addEventListener("DOMContentLoaded", function handleLoad(event) {
-      gBrowser.removeEventListener("DOMContentLoaded", handleLoad, true);
-      callback(event);
-    }, true);
+    if (engine)
+      ss.removeEngine(engine);
   }
 }
--- a/browser/components/search/test/head.js
+++ b/browser/components/search/test/head.js
@@ -15,18 +15,37 @@ function isSubObjectOf(expectedObj, actu
     } else {
       is(actualObj[prop], expectedObj[prop], name + "[" + prop + "]");
     }
   }
 }
 
 function waitForPopupShown(aPopupId, aCallback) {
   let popup = document.getElementById(aPopupId);
+  info("waitForPopupShown: got popup: " + popup.id);
   function onPopupShown() {
-    popup.removeEventListener("popupshown", onPopupShown);
+    info("onPopupShown");
+    removePopupShownListener();
     SimpleTest.executeSoon(aCallback);
   }
+  function removePopupShownListener() {
+    popup.removeEventListener("popupshown", onPopupShown);
+  }
   popup.addEventListener("popupshown", onPopupShown);
+  registerCleanupFunction(removePopupShownListener);
 }
 
 function waitForBrowserContextMenu(aCallback) {
   waitForPopupShown(gBrowser.selectedBrowser.contextMenu, aCallback);
 }
+
+function doOnloadOnce(aCallback) {
+  function doOnloadOnceListener(aEvent) {
+    info("doOnloadOnce: " + aEvent.originalTarget.location);
+    removeDoOnloadOnceListener();
+    aCallback(aEvent);
+  }
+  function removeDoOnloadOnceListener() {
+    gBrowser.removeEventListener("DOMContentLoaded", doOnloadOnceListener);
+  }
+  gBrowser.addEventListener("DOMContentLoaded", doOnloadOnceListener);
+  registerCleanupFunction(removeDoOnloadOnceListener);
+}
--- a/browser/components/search/test/test.html
+++ b/browser/components/search/test/test.html
@@ -1,7 +1,8 @@
 <!DOCTYPE html>
 <html>
 <head>
+  <meta charset="utf-8" />
   <title>Bug 426329</title>
 </head>
 <body></body>
 </html>