From 50f707b73c875fd69e970ea4169d805c3652b901 Mon Sep 17 00:00:00 2001 From: Matthew Noorenberghe Date: Wed, 13 Mar 2013 19:23:03 -0700 Subject: [PATCH] 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. --- .../components/search/test/browser_426329.js | 19 +++------- .../search/test/browser_contextmenu.js | 37 +++++++++++-------- browser/components/search/test/head.js | 21 ++++++++++- browser/components/search/test/test.html | 1 + 4 files changed, 48 insertions(+), 30 deletions(-) diff --git a/browser/components/search/test/browser_426329.js b/browser/components/search/test/browser_426329.js index 413ee8ebaea..d21e07a8899 100644 --- a/browser/components/search/test/browser_426329.js +++ b/browser/components/search/test/browser_426329.js @@ -201,11 +201,11 @@ function test() { 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(); } @@ -219,13 +219,6 @@ function test() { 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; @@ -236,7 +229,7 @@ function test() { event.initMouseEvent("click", true, true, window, 0, 0, 0, 0, 0, ctrlKeyArg, altKeyArg, shiftKeyArg, metaKeyArg, - buttonArg, null); + buttonArg, null); aTarget.dispatchEvent(event); } @@ -252,7 +245,7 @@ function test() { 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() { diff --git a/browser/components/search/test/browser_contextmenu.js b/browser/components/search/test/browser_contextmenu.js index c7bfe8c8278..eacfb0c9c10 100644 --- a/browser/components/search/test/browser_contextmenu.js +++ b/browser/components/search/test/browser_contextmenu.js @@ -30,6 +30,7 @@ function test() { } } + 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", @@ -46,6 +47,7 @@ function test() { function testContextMenu() { function rightClickOnDocument() { + info("rightClickOnDocument: " + content.window.location); waitForBrowserContextMenu(checkContextMenu); var clickTarget = content.document.body; var eventDetails = { type: "contextmenu", button: 2 }; @@ -54,30 +56,39 @@ function test() { // 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() { @@ -86,13 +97,7 @@ function test() { } 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); } } diff --git a/browser/components/search/test/head.js b/browser/components/search/test/head.js index 103b91faa1d..471d23a3f30 100644 --- a/browser/components/search/test/head.js +++ b/browser/components/search/test/head.js @@ -20,13 +20,32 @@ function isSubObjectOf(expectedObj, actualObj, name) { 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); +} diff --git a/browser/components/search/test/test.html b/browser/components/search/test/test.html index 3289d6ea4f1..a39bece4ff7 100644 --- a/browser/components/search/test/test.html +++ b/browser/components/search/test/test.html @@ -1,6 +1,7 @@ + Bug 426329