diff options
Diffstat (limited to 'browser/components/search/test')
40 files changed, 4597 insertions, 0 deletions
diff --git a/browser/components/search/test/.eslintrc.js b/browser/components/search/test/.eslintrc.js new file mode 100644 index 000000000..c764b133d --- /dev/null +++ b/browser/components/search/test/.eslintrc.js @@ -0,0 +1,7 @@ +"use strict"; + +module.exports = { + "extends": [ + "../../../../testing/mochitest/browser.eslintrc.js" + ] +}; diff --git a/browser/components/search/test/426329.xml b/browser/components/search/test/426329.xml new file mode 100644 index 000000000..e4545cc77 --- /dev/null +++ b/browser/components/search/test/426329.xml @@ -0,0 +1,11 @@ +<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/" + xmlns:moz="http://www.mozilla.org/2006/browser/search/"> + <ShortName>Bug 426329</ShortName> + <Description>426329 Search</Description> + <InputEncoding>utf-8</InputEncoding> + <Image width="16" height="16">data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAABGklEQVQoz2NgGB6AnZ1dUlJSXl4eSDIyMhLW4Ovr%2B%2Fr168uXL69Zs4YoG%2BLi4i5dusTExMTGxsbNzd3f37937976%2BnpmZmagbHR09J49e5YvX66kpATVEBYW9ubNm2nTphkbG7e2tp44cQLIuHfvXm5urpaWFlDKysqqu7v73LlzECMYIiIiHj58mJCQoKKicvXq1bS0NKBgW1vbjh074uPjgeqAXE1NzSdPnvDz84M0AEUvXLgAsW379u1z5swBen3jxo2zZ892cHB4%2BvQp0KlAfwI1cHJyghQFBwfv2rULokFXV%2FfixYu7d%2B8GGqGgoMDKyrpu3br9%2B%2FcDuXl5eVA%2FAEWBfoWHAdAYoNuAYQ0XAeoUERFhGDYAAPoUaT2dfWJuAAAAAElFTkSuQmCC</Image> + <Url type="text/html" method="GET" template="http://mochi.test:8888/browser/browser/components/search/test/test.html"> + <Param name="test" value="{searchTerms}"/> + </Url> + <moz:SearchForm>http://mochi.test:8888/browser/browser/components/search/test/test.html</moz:SearchForm> +</OpenSearchDescription> diff --git a/browser/components/search/test/483086-1.xml b/browser/components/search/test/483086-1.xml new file mode 100644 index 000000000..9dbba4886 --- /dev/null +++ b/browser/components/search/test/483086-1.xml @@ -0,0 +1,10 @@ +<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/" + xmlns:moz="http://www.mozilla.org/2006/browser/search/"> + <ShortName>483086a</ShortName> + <Description>Bug 483086 Test 1</Description> + <Image width="16" height="16">data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAABGklEQVQoz2NgGB6AnZ1dUlJSXl4eSDIyMhLW4Ovr%2B%2Fr168uXL69Zs4YoG%2BLi4i5dusTExMTGxsbNzd3f37937976%2BnpmZmagbHR09J49e5YvX66kpATVEBYW9ubNm2nTphkbG7e2tp44cQLIuHfvXm5urpaWFlDKysqqu7v73LlzECMYIiIiHj58mJCQoKKicvXq1bS0NKBgW1vbjh074uPjgeqAXE1NzSdPnvDz84M0AEUvXLgAsW379u1z5swBen3jxo2zZ892cHB4%2BvQp0KlAfwI1cHJyghQFBwfv2rULokFXV%2FfixYu7d%2B8GGqGgoMDKyrpu3br9%2B%2FcDuXl5eVA%2FAEWBfoWHAdAYoNuAYQ0XAeoUERFhGDYAAPoUaT2dfWJuAAAAAElFTkSuQmCC</Image> + <Url type="text/html" method="GET" template="http://mochi.test:8888/browser/browser/components/search/test/?search"> + <Param name="test" value="{searchTerms}"/> + </Url> + <moz:SearchForm>foo://example.com</moz:SearchForm> +</OpenSearchDescription> diff --git a/browser/components/search/test/483086-2.xml b/browser/components/search/test/483086-2.xml new file mode 100644 index 000000000..f130b9068 --- /dev/null +++ b/browser/components/search/test/483086-2.xml @@ -0,0 +1,10 @@ +<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/" + xmlns:moz="http://www.mozilla.org/2006/browser/search/"> + <ShortName>483086b</ShortName> + <Description>Bug 483086 Test 2</Description> + <Image width="16" height="16">data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAABGklEQVQoz2NgGB6AnZ1dUlJSXl4eSDIyMhLW4Ovr%2B%2Fr168uXL69Zs4YoG%2BLi4i5dusTExMTGxsbNzd3f37937976%2BnpmZmagbHR09J49e5YvX66kpATVEBYW9ubNm2nTphkbG7e2tp44cQLIuHfvXm5urpaWFlDKysqqu7v73LlzECMYIiIiHj58mJCQoKKicvXq1bS0NKBgW1vbjh074uPjgeqAXE1NzSdPnvDz84M0AEUvXLgAsW379u1z5swBen3jxo2zZ892cHB4%2BvQp0KlAfwI1cHJyghQFBwfv2rULokFXV%2FfixYu7d%2B8GGqGgoMDKyrpu3br9%2B%2FcDuXl5eVA%2FAEWBfoWHAdAYoNuAYQ0XAeoUERFhGDYAAPoUaT2dfWJuAAAAAElFTkSuQmCC</Image> + <Url type="text/html" method="GET" template="http://mochi.test:8888/browser/browser/components/search/test/?search"> + <Param name="test" value="{searchTerms}"/> + </Url> + <moz:SearchForm>http://example.com</moz:SearchForm> +</OpenSearchDescription> diff --git a/browser/components/search/test/browser.ini b/browser/components/search/test/browser.ini new file mode 100644 index 000000000..f1070264d --- /dev/null +++ b/browser/components/search/test/browser.ini @@ -0,0 +1,44 @@ +[DEFAULT] +support-files = + 426329.xml + 483086-1.xml + 483086-2.xml + head.js + opensearch.html + test.html + testEngine.xml + testEngine_diacritics.xml + testEngine_dupe.xml + testEngine_mozsearch.xml + webapi.html + +[browser_426329.js] +[browser_483086.js] +[browser_addEngine.js] +[browser_amazon.js] +[browser_amazon_behavior.js] +[browser_bing.js] +[browser_bing_behavior.js] +[browser_contextmenu.js] +[browser_contextSearchTabPosition.js] +skip-if = os == "mac" # bug 967013 +[browser_google.js] +[browser_google_codes.js] +[browser_google_behavior.js] +[browser_healthreport.js] +[browser_hiddenOneOffs_cleanup.js] +[browser_hiddenOneOffs_diacritics.js] +[browser_oneOffContextMenu.js] +[browser_oneOffContextMenu_setDefault.js] +[browser_oneOffHeader.js] +[browser_private_search_perwindowpb.js] +[browser_yahoo.js] +[browser_yahoo_behavior.js] +[browser_abouthome_behavior.js] +skip-if = true # Bug ??????, Bug 1100301 - leaks windows until shutdown when --run-by-dir +[browser_aboutSearchReset.js] +[browser_searchbar_openpopup.js] +skip-if = os == "linux" # Linux has different focus behaviours. +[browser_searchbar_keyboard_navigation.js] +[browser_searchbar_smallpanel_keyboard_navigation.js] +[browser_webapi.js] diff --git a/browser/components/search/test/browser_426329.js b/browser/components/search/test/browser_426329.js new file mode 100644 index 000000000..d9cbd3f7a --- /dev/null +++ b/browser/components/search/test/browser_426329.js @@ -0,0 +1,250 @@ +XPCOMUtils.defineLazyModuleGetter(this, "FormHistory", + "resource://gre/modules/FormHistory.jsm"); + +function expectedURL(aSearchTerms) { + const ENGINE_HTML_BASE = "http://mochi.test:8888/browser/browser/components/search/test/test.html"; + var textToSubURI = Cc["@mozilla.org/intl/texttosuburi;1"]. + getService(Ci.nsITextToSubURI); + var searchArg = textToSubURI.ConvertAndEscape("utf-8", aSearchTerms); + return ENGINE_HTML_BASE + "?test=" + searchArg; +} + +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); + aTarget.dispatchEvent(event); +} + +// 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); +} + +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]; + var numRows = autocompleteMenu.tree.view.rowCount; + for (var i = 0; i < numRows; i++) { + entries.push(autocompleteMenu.tree.view.getValueAt(i, column)); + } + return entries; +} + +function countEntries(name, value) { + return new Promise(resolve => { + let count = 0; + let obj = name && value ? {fieldname: name, value: value} : {}; + FormHistory.count(obj, + { handleResult: function(result) { count = result; }, + handleError: function(error) { throw error; }, + handleCompletion: function(reason) { + if (!reason) { + resolve(count); + } + } + }); + }); +} + +var searchBar; +var searchButton; +var searchEntries = ["test"]; +function promiseSetEngine() { + return new Promise(resolve => { + var ss = Services.search; + + function observer(aSub, aTopic, aData) { + switch (aData) { + case "engine-added": + var engine = ss.getEngineByName("Bug 426329"); + ok(engine, "Engine was added."); + ss.currentEngine = engine; + break; + case "engine-current": + ok(ss.currentEngine.name == "Bug 426329", "currentEngine set"); + searchBar = BrowserSearch.searchBar; + searchButton = document.getAnonymousElementByAttribute(searchBar, + "anonid", "search-go-button"); + ok(searchButton, "got search-go-button"); + searchBar.value = "test"; + + Services.obs.removeObserver(observer, "browser-search-engine-modified"); + resolve(); + break; + } + } + + Services.obs.addObserver(observer, "browser-search-engine-modified", false); + ss.addEngine("http://mochi.test:8888/browser/browser/components/search/test/426329.xml", + null, "data:image/x-icon,%00", false); + }); +} + +function promiseRemoveEngine() { + return new Promise(resolve => { + var ss = Services.search; + + function observer(aSub, aTopic, aData) { + if (aData == "engine-removed") { + Services.obs.removeObserver(observer, "browser-search-engine-modified"); + resolve(); + } + } + + Services.obs.addObserver(observer, "browser-search-engine-modified", false); + var engine = ss.getEngineByName("Bug 426329"); + ss.removeEngine(engine); + }); +} + + +var preSelectedBrowser; +var preTabNo; +function* prepareTest() { + preSelectedBrowser = gBrowser.selectedBrowser; + preTabNo = gBrowser.tabs.length; + searchBar = BrowserSearch.searchBar; + + yield SimpleTest.promiseFocus(); + + if (document.activeElement == searchBar) + return; + + let focusPromise = BrowserTestUtils.waitForEvent(searchBar, "focus"); + gURLBar.focus(); + searchBar.focus(); + yield focusPromise; +} + +add_task(function* testSetupEngine() { + yield promiseSetEngine(); +}); + +add_task(function* testReturn() { + yield* prepareTest(); + EventUtils.synthesizeKey("VK_RETURN", {}); + yield BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser); + + is(gBrowser.tabs.length, preTabNo, "Return key did not open new tab"); + is(gBrowser.currentURI.spec, expectedURL(searchBar.value), "testReturn opened correct search page"); +}); + +add_task(function* testAltReturn() { + yield* prepareTest(); + yield BrowserTestUtils.openNewForegroundTab(gBrowser, () => { + EventUtils.synthesizeKey("VK_RETURN", { altKey: true }); + }); + + is(gBrowser.tabs.length, preTabNo + 1, "Alt+Return key added new tab"); + is(gBrowser.currentURI.spec, expectedURL(searchBar.value), "testAltReturn opened correct search page"); +}); + +// Shift key has no effect for now, so skip it +add_task(function* testShiftAltReturn() { + return; + /* + yield* prepareTest(); + + let url = expectedURL(searchBar.value); + + let newTabPromise = BrowserTestUtils.waitForNewTab(gBrowser, url); + EventUtils.synthesizeKey("VK_RETURN", { shiftKey: true, altKey: true }); + yield newTabPromise; + + is(gBrowser.tabs.length, preTabNo + 1, "Shift+Alt+Return key added new tab"); + is(gBrowser.currentURI.spec, url, "testShiftAltReturn opened correct search page"); + */ +}); + +add_task(function* testLeftClick() { + yield* prepareTest(); + simulateClick({ button: 0 }, searchButton); + yield BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser); + is(gBrowser.tabs.length, preTabNo, "LeftClick did not open new tab"); + is(gBrowser.currentURI.spec, expectedURL(searchBar.value), "testLeftClick opened correct search page"); +}); + +add_task(function* testMiddleClick() { + yield* prepareTest(); + yield BrowserTestUtils.openNewForegroundTab(gBrowser, () => { + simulateClick({ button: 1 }, searchButton); + }); + is(gBrowser.tabs.length, preTabNo + 1, "MiddleClick added new tab"); + is(gBrowser.currentURI.spec, expectedURL(searchBar.value), "testMiddleClick opened correct search page"); +}); + +add_task(function* testShiftMiddleClick() { + yield* prepareTest(); + + let url = expectedURL(searchBar.value); + + let newTabPromise = BrowserTestUtils.waitForNewTab(gBrowser, url); + simulateClick({ button: 1, shiftKey: true }, searchButton); + let newTab = yield newTabPromise; + + is(gBrowser.tabs.length, preTabNo + 1, "Shift+MiddleClick added new tab"); + is(newTab.linkedBrowser.currentURI.spec, url, "testShiftMiddleClick opened correct search page"); +}); + +add_task(function* testRightClick() { + preTabNo = gBrowser.tabs.length; + gBrowser.selectedBrowser.loadURI("about:blank"); + yield new Promise(resolve => { + setTimeout(function() { + is(gBrowser.tabs.length, preTabNo, "RightClick did not open new tab"); + is(gBrowser.currentURI.spec, "about:blank", "RightClick did nothing"); + resolve(); + }, 5000); + simulateClick({ button: 2 }, searchButton); + }); + // The click in the searchbox focuses it, which opens the suggestion + // panel. Clean up after ourselves. + searchBar.textbox.popup.hidePopup(); +}); + +add_task(function* testSearchHistory() { + var textbox = searchBar._textbox; + for (var i = 0; i < searchEntries.length; i++) { + let count = yield countEntries(textbox.getAttribute("autocompletesearchparam"), searchEntries[i]); + ok(count > 0, "form history entry '" + searchEntries[i] + "' should exist"); + } +}); + +add_task(function* testAutocomplete() { + var popup = searchBar.textbox.popup; + let popupShownPromise = BrowserTestUtils.waitForEvent(popup, "popupshown"); + searchBar.textbox.showHistoryPopup(); + yield popupShownPromise; + checkMenuEntries(searchEntries); +}); + +add_task(function* testClearHistory() { + let controller = searchBar.textbox.controllers.getControllerForCommand("cmd_clearhistory") + ok(controller.isCommandEnabled("cmd_clearhistory"), "Clear history command enabled"); + controller.doCommand("cmd_clearhistory"); + let count = yield countEntries(); + ok(count == 0, "History cleared"); +}); + +add_task(function* asyncCleanup() { + searchBar.value = ""; + while (gBrowser.tabs.length != 1) { + gBrowser.removeTab(gBrowser.tabs[0], {animate: false}); + } + gBrowser.selectedBrowser.loadURI("about:blank"); + yield promiseRemoveEngine(); +}); diff --git a/browser/components/search/test/browser_483086.js b/browser/components/search/test/browser_483086.js new file mode 100644 index 000000000..208add867 --- /dev/null +++ b/browser/components/search/test/browser_483086.js @@ -0,0 +1,49 @@ +/* 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/. */ +var gSS = Services.search; + +function test() { + waitForExplicitFinish(); + + function observer(aSubject, aTopic, aData) { + switch (aData) { + case "engine-added": + let engine = gSS.getEngineByName("483086a"); + ok(engine, "Test engine 1 installed"); + isnot(engine.searchForm, "foo://example.com", + "Invalid SearchForm URL dropped"); + gSS.removeEngine(engine); + break; + case "engine-removed": + Services.obs.removeObserver(observer, "browser-search-engine-modified"); + test2(); + break; + } + } + + Services.obs.addObserver(observer, "browser-search-engine-modified", false); + gSS.addEngine("http://mochi.test:8888/browser/browser/components/search/test/483086-1.xml", + null, "data:image/x-icon;%00", false); +} + +function test2() { + function observer(aSubject, aTopic, aData) { + switch (aData) { + case "engine-added": + let engine = gSS.getEngineByName("483086b"); + ok(engine, "Test engine 2 installed"); + is(engine.searchForm, "http://example.com", "SearchForm is correct"); + gSS.removeEngine(engine); + break; + case "engine-removed": + Services.obs.removeObserver(observer, "browser-search-engine-modified"); + finish(); + break; + } + } + + Services.obs.addObserver(observer, "browser-search-engine-modified", false); + gSS.addEngine("http://mochi.test:8888/browser/browser/components/search/test/483086-2.xml", + null, "data:image/x-icon;%00", false); +} diff --git a/browser/components/search/test/browser_aboutSearchReset.js b/browser/components/search/test/browser_aboutSearchReset.js new file mode 100644 index 000000000..64376d6da --- /dev/null +++ b/browser/components/search/test/browser_aboutSearchReset.js @@ -0,0 +1,159 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ + */ + +const TELEMETRY_RESULT_ENUM = { + RESTORED_DEFAULT: 0, + KEPT_CURRENT: 1, + CHANGED_ENGINE: 2, + CLOSED_PAGE: 3, + OPENED_SETTINGS: 4 +}; + +const kSearchStr = "a search"; +const kSearchPurpose = "searchbar"; + +const kTestEngine = "testEngine.xml"; + +function checkTelemetryRecords(expectedValue) { + let histogram = Services.telemetry.getHistogramById("SEARCH_RESET_RESULT"); + let snapshot = histogram.snapshot(); + // The probe is declared with 5 values, but we get 6 back from .counts + let expectedCounts = [0, 0, 0, 0, 0, 0]; + if (expectedValue != null) { + expectedCounts[expectedValue] = 1; + } + Assert.deepEqual(snapshot.counts, expectedCounts, + "histogram has expected content"); + histogram.clear(); +} + +function promiseStoppedLoad(expectedURL) { + return new Promise(resolve => { + let browser = gBrowser.selectedBrowser; + let original = browser.loadURIWithFlags; + browser.loadURIWithFlags = function(URI) { + if (URI == expectedURL) { + browser.loadURIWithFlags = original; + ok(true, "loaded expected url: " + URI); + resolve(); + return; + } + + original.apply(browser, arguments); + }; + }); +} + +var gTests = [ + +{ + desc: "Test the 'Keep Current Settings' button.", + run: function* () { + let engine = yield promiseNewEngine(kTestEngine, {setAsCurrent: true}); + + let expectedURL = engine. + getSubmission(kSearchStr, null, kSearchPurpose). + uri.spec; + + let rawEngine = engine.wrappedJSObject; + let initialHash = rawEngine.getAttr("loadPathHash"); + rawEngine.setAttr("loadPathHash", "broken"); + + let loadPromise = promiseStoppedLoad(expectedURL); + gBrowser.contentDocument.getElementById("searchResetKeepCurrent").click(); + yield loadPromise; + + is(engine, Services.search.currentEngine, + "the custom engine is still default"); + is(rawEngine.getAttr("loadPathHash"), initialHash, + "the loadPathHash has been fixed"); + + checkTelemetryRecords(TELEMETRY_RESULT_ENUM.KEPT_CURRENT); + } +}, + +{ + desc: "Test the 'Restore Search Defaults' button.", + run: function* () { + let currentEngine = Services.search.currentEngine; + let originalEngine = Services.search.originalDefaultEngine; + let doc = gBrowser.contentDocument; + let defaultEngineSpan = doc.getElementById("defaultEngine"); + is(defaultEngineSpan.textContent, originalEngine.name, + "the name of the original default engine is displayed"); + + let expectedURL = originalEngine. + getSubmission(kSearchStr, null, kSearchPurpose). + uri.spec; + let loadPromise = promiseStoppedLoad(expectedURL); + let button = doc.getElementById("searchResetChangeEngine"); + is(doc.activeElement, button, + "the 'Change Search Engine' button is focused"); + button.click(); + yield loadPromise; + + is(originalEngine, Services.search.currentEngine, + "the default engine is back to the original one"); + + checkTelemetryRecords(TELEMETRY_RESULT_ENUM.RESTORED_DEFAULT); + Services.search.currentEngine = currentEngine; + } +}, + +{ + desc: "Click the settings link.", + run: function* () { + let loadPromise = BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser, + false, + "about:preferences#search") + gBrowser.contentDocument.getElementById("linkSettingsPage").click(); + yield loadPromise; + + checkTelemetryRecords(TELEMETRY_RESULT_ENUM.OPENED_SETTINGS); + } +}, + +{ + desc: "Load another page without clicking any of the buttons.", + run: function* () { + yield promiseTabLoadEvent(gBrowser.selectedTab, "about:mozilla"); + + checkTelemetryRecords(TELEMETRY_RESULT_ENUM.CLOSED_PAGE); + } +}, + +]; + +function test() +{ + waitForExplicitFinish(); + Task.spawn(function* () { + let oldCanRecord = Services.telemetry.canRecordExtended; + Services.telemetry.canRecordExtended = true; + checkTelemetryRecords(); + + for (let test of gTests) { + info(test.desc); + + // Create a tab to run the test. + let tab = gBrowser.selectedTab = gBrowser.addTab("about:blank"); + + // Start loading about:searchreset and wait for it to complete. + let url = "about:searchreset?data=" + encodeURIComponent(kSearchStr) + + "&purpose=" + kSearchPurpose; + yield promiseTabLoadEvent(tab, url); + + info("Running test"); + yield test.run(); + + info("Cleanup"); + gBrowser.removeCurrentTab(); + } + + Services.telemetry.canRecordExtended = oldCanRecord; + }).then(finish, ex => { + ok(false, "Unexpected Exception: " + ex); + finish(); + }); +} diff --git a/browser/components/search/test/browser_abouthome_behavior.js b/browser/components/search/test/browser_abouthome_behavior.js new file mode 100644 index 000000000..3291b41f4 --- /dev/null +++ b/browser/components/search/test/browser_abouthome_behavior.js @@ -0,0 +1,144 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ */ + +/* + * Test home page search for all plugin URLs + */ + +"use strict"; + +function test() { + // Bug 992270: Ignore uncaught about:home exceptions (related to snippets from IndexedDB) + ignoreAllUncaughtExceptions(true); + + let previouslySelectedEngine = Services.search.currentEngine; + + function replaceUrl(base) { + return base; + } + + let gMutationObserver = null; + + function verify_about_home_search(engine_name) { + let engine = Services.search.getEngineByName(engine_name); + ok(engine, engine_name + " is installed"); + + Services.search.currentEngine = engine; + + // load about:home, but remove the listener first so it doesn't + // get in the way + gBrowser.removeProgressListener(listener); + gBrowser.loadURI("about:home"); + info("Waiting for about:home load"); + tab.linkedBrowser.addEventListener("load", function load(event) { + if (event.originalTarget != tab.linkedBrowser.contentDocument || + event.target.location.href == "about:blank") { + info("skipping spurious load event"); + return; + } + tab.linkedBrowser.removeEventListener("load", load, true); + + // Observe page setup + let doc = gBrowser.contentDocument; + gMutationObserver = new MutationObserver(function (mutations) { + for (let mutation of mutations) { + if (mutation.attributeName == "searchEngineName") { + // Re-add the listener, and perform a search + gBrowser.addProgressListener(listener); + gMutationObserver.disconnect() + gMutationObserver = null; + executeSoon(function() { + doc.getElementById("searchText").value = "foo"; + doc.getElementById("searchSubmit").click(); + }); + } + } + }); + gMutationObserver.observe(doc.documentElement, { attributes: true }); + }, true); + } + waitForExplicitFinish(); + + let gCurrTest; + let gTests = [ + { + name: "Search with Bing from about:home", + searchURL: replaceUrl("http://www.bing.com/search?q=foo&pc=MOZI&form=MOZSPG"), + run: function () { + verify_about_home_search("Bing"); + } + }, + { + name: "Search with Yahoo from about:home", + searchURL: replaceUrl("https://search.yahoo.com/search?p=foo&ei=UTF-8&fr=moz35"), + run: function () { + verify_about_home_search("Yahoo"); + } + }, + { + name: "Search with Google from about:home", + searchURL: replaceUrl("https://www.google.com/search?q=foo&ie=utf-8&oe=utf-8"), + run: function () { + verify_about_home_search("Google"); + } + }, + { + name: "Search with Amazon.com from about:home", + searchURL: replaceUrl("https://www.amazon.com/exec/obidos/external-search/?field-keywords=foo&mode=blended&tag=mozilla-20&sourceid=Mozilla-search"), + run: function () { + verify_about_home_search("Amazon.com"); + } + } + ]; + + function nextTest() { + if (gTests.length) { + gCurrTest = gTests.shift(); + info("Running : " + gCurrTest.name); + executeSoon(gCurrTest.run); + } else { + // Make sure we listen again for uncaught exceptions in the next test or cleanup. + executeSoon(finish); + } + } + + let tab = gBrowser.selectedTab = gBrowser.addTab(); + + let listener = { + onStateChange: function onStateChange(webProgress, req, flags, status) { + info("onStateChange"); + // Only care about top-level document starts + let docStart = Ci.nsIWebProgressListener.STATE_IS_DOCUMENT | + Ci.nsIWebProgressListener.STATE_START; + if (!(flags & docStart) || !webProgress.isTopLevel) + return; + + if (req.originalURI.spec == "about:blank") + return; + + info("received document start"); + + ok(req instanceof Ci.nsIChannel, "req is a channel"); + is(req.originalURI.spec, gCurrTest.searchURL, "search URL was loaded"); + info("Actual URI: " + req.URI.spec); + + req.cancel(Components.results.NS_ERROR_FAILURE); + + executeSoon(nextTest); + } + } + + registerCleanupFunction(function () { + Services.search.currentEngine = previouslySelectedEngine; + gBrowser.removeProgressListener(listener); + gBrowser.removeTab(tab); + if (gMutationObserver) + gMutationObserver.disconnect(); + }); + + tab.linkedBrowser.addEventListener("load", function load() { + tab.linkedBrowser.removeEventListener("load", load, true); + gBrowser.addProgressListener(listener); + nextTest(); + }, true); +} diff --git a/browser/components/search/test/browser_addEngine.js b/browser/components/search/test/browser_addEngine.js new file mode 100644 index 000000000..b971ea5f7 --- /dev/null +++ b/browser/components/search/test/browser_addEngine.js @@ -0,0 +1,105 @@ +/* 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/. */ +var gSS = Services.search; + +function observer(aSubject, aTopic, aData) { + if (!gCurrentTest) { + info("Observer called with no test active"); + return; + } + + let engine = aSubject.QueryInterface(Ci.nsISearchEngine); + info("Observer: " + aData + " for " + engine.name); + let method; + switch (aData) { + case "engine-added": + if (gCurrentTest.added) + method = "added" + break; + case "engine-current": + if (gCurrentTest.current) + method = "current"; + break; + case "engine-removed": + if (gCurrentTest.removed) + method = "removed"; + break; + } + + if (method) + gCurrentTest[method](engine); +} + +function checkEngine(checkObj, engineObj) { + info("Checking engine"); + for (var prop in checkObj) + is(checkObj[prop], engineObj[prop], prop + " is correct"); +} + +var gTests = [ + { + name: "opensearch install", + engine: { + name: "Foo", + alias: null, + description: "Foo Search", + searchForm: "http://mochi.test:8888/browser/browser/components/search/test/" + }, + run: function () { + Services.obs.addObserver(observer, "browser-search-engine-modified", false); + + gSS.addEngine("http://mochi.test:8888/browser/browser/components/search/test/testEngine.xml", + null, "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAABGklEQVQoz2NgGB6AnZ1dUlJSXl4eSDIyMhLW4Ovr%2B%2Fr168uXL69Zs4YoG%2BLi4i5dusTExMTGxsbNzd3f37937976%2BnpmZmagbHR09J49e5YvX66kpATVEBYW9ubNm2nTphkbG7e2tp44cQLIuHfvXm5urpaWFlDKysqqu7v73LlzECMYIiIiHj58mJCQoKKicvXq1bS0NKBgW1vbjh074uPjgeqAXE1NzSdPnvDz84M0AEUvXLgAsW379u1z5swBen3jxo2zZ892cHB4%2BvQp0KlAfwI1cHJyghQFBwfv2rULokFXV%2FfixYu7d%2B8GGqGgoMDKyrpu3br9%2B%2FcDuXl5eVA%2FAEWBfoWHAdAYoNuAYQ0XAeoUERFhGDYAAPoUaT2dfWJuAAAAAElFTkSuQmCC", + false); + }, + added: function (engine) { + ok(engine, "engine was added."); + + checkEngine(this.engine, engine); + + let engineFromSS = gSS.getEngineByName(this.engine.name); + is(engine, engineFromSS, "engine is obtainable via getEngineByName"); + + let aEngine = gSS.getEngineByAlias("fooalias"); + ok(!aEngine, "Alias was not parsed from engine description"); + + gSS.currentEngine = engine; + }, + current: function (engine) { + let currentEngine = gSS.currentEngine; + is(engine, currentEngine, "engine is current"); + is(engine.name, this.engine.name, "current engine was changed successfully"); + + gSS.removeEngine(engine); + }, + removed: function (engine) { + // Remove the observer before calling the currentEngine getter, + // as that getter will set the currentEngine to the original default + // which will trigger a notification causing the test to loop over all + // engines. + Services.obs.removeObserver(observer, "browser-search-engine-modified"); + + let currentEngine = gSS.currentEngine; + ok(currentEngine, "An engine is present."); + isnot(currentEngine.name, this.engine.name, "Current engine reset after removal"); + + nextTest(); + } + } +]; + +var gCurrentTest = null; +function nextTest() { + if (gTests.length) { + gCurrentTest = gTests.shift(); + info("Running " + gCurrentTest.name); + gCurrentTest.run(); + } else + executeSoon(finish); +} + +function test() { + waitForExplicitFinish(); + nextTest(); +} diff --git a/browser/components/search/test/browser_amazon.js b/browser/components/search/test/browser_amazon.js new file mode 100644 index 000000000..965a3dcf8 --- /dev/null +++ b/browser/components/search/test/browser_amazon.js @@ -0,0 +1,82 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ */ + +/* + * Test Amazon search plugin URLs + */ + +"use strict"; + +const BROWSER_SEARCH_PREF = "browser.search."; + +function test() { + let engine = Services.search.getEngineByName("Amazon.com"); + ok(engine, "Amazon.com"); + + let base = "https://www.amazon.com/exec/obidos/external-search/?field-keywords=foo&ie=UTF-8&mode=blended&tag=mozilla-20&sourceid=Mozilla-search"; + let url; + + // Test search URLs (including purposes). + url = engine.getSubmission("foo").uri.spec; + is(url, base, "Check search URL for 'foo'"); + + // Check search suggestion URL. + url = engine.getSubmission("foo", "application/x-suggestions+json").uri.spec; + is(url, "https://completion.amazon.com/search/complete?q=foo&search-alias=aps&mkt=1", "Check search suggestion URL for 'foo'"); + + // Check all other engine properties. + const EXPECTED_ENGINE = { + name: "Amazon.com", + alias: null, + description: "Amazon.com Search", + searchForm: "https://www.amazon.com/exec/obidos/external-search/?field-keywords=&ie=UTF-8&mode=blended&tag=mozilla-20&sourceid=Mozilla-search", + hidden: false, + wrappedJSObject: { + queryCharset: "UTF-8", + "_iconURL": "data:image/x-icon;base64,AAABAAIAEBAAAAAAAAC0AQAAJgAAACAgAAAAAAAA6QIAANoBAACJUE5HDQoaCgAAAA1JSERSAAAAEAAAABAIBgAAAB/z/2EAAAF7SURBVDjLlZPLasJAFIaFRF+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rUHGiKZCP4tNgO6BAEI08EQH9Z2Qow0hyPypJGIa9p6JWKCn4SA8jSKmJIDgyRvPJkcRxjfUwNGr/i8+Mo32iHzWiThBD4NM60bet9P77/ubA728RlTjMiwiH6zEEfvIrwdZFtQmMJ7W/ofIDBZD5m3mVZGwJcOP2kmILIlCkE45HoPWurwCSg0+UQRD4ZyXxId+T7gQb9+4q9sioY5ltrOG3L5vqXiiJffDx/aUi83ZJ7jr2ohcEu8Hh6/m+I7OWGiVxbWKHsz+O3vSOakqFQdsFgQeJUiKD7Wv9YKXBgCeSUC3v2kM5EJhlHDh3NcgcPlG1BXZu98sDmTuBa4fsMnz9fniJUaGzs+eMC540XuR0aDO2L8Y3qPyMcdOM+R/8XcqRA3qp9gAAAABJRU5ErkJggg==", + _urls : [ + { + type: "application/x-suggestions+json", + method: "GET", + template: "https://completion.amazon.com/search/complete?q={searchTerms}&search-alias=aps&mkt=1", + params: "", + }, + { + type: "text/html", + method: "GET", + template: "https://www.amazon.com/exec/obidos/external-search/", + params: [ + { + name: "field-keywords", + value: "{searchTerms}", + purpose: undefined, + }, + { + name: "ie", + value: "{inputEncoding}", + purpose: undefined, + }, + { + name: "mode", + value: "blended", + purpose: undefined, + }, + { + name: "tag", + value: "mozilla-20", + purpose: undefined, + }, + { + name: "sourceid", + value: "Mozilla-search", + purpose: undefined, + }, + ], + mozparams: {}, + }, + ], + }, + }; + + isSubObjectOf(EXPECTED_ENGINE, engine, "Amazon"); +} diff --git a/browser/components/search/test/browser_amazon_behavior.js b/browser/components/search/test/browser_amazon_behavior.js new file mode 100644 index 000000000..22d16581a --- /dev/null +++ b/browser/components/search/test/browser_amazon_behavior.js @@ -0,0 +1,166 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ */ + +/* + * Test Amazon search plugin URLs + */ + +"use strict"; + +const BROWSER_SEARCH_PREF = "browser.search."; + + +function test() { + let engine = Services.search.getEngineByName("Amazon.com"); + ok(engine, "Amazon is installed"); + + let previouslySelectedEngine = Services.search.currentEngine; + Services.search.currentEngine = engine; + engine.alias = "a"; + + let base = "https://www.amazon.com/exec/obidos/external-search/?field-keywords=foo&ie=UTF-8&mode=blended&tag=mozilla-20&sourceid=Mozilla-search"; + let url; + + // Test search URLs (including purposes). + url = engine.getSubmission("foo").uri.spec; + is(url, base, "Check search URL for 'foo'"); + + waitForExplicitFinish(); + + var gCurrTest; + var gTests = [ + { + name: "context menu search", + searchURL: base, + run: function () { + // Simulate a contextmenu search + // FIXME: This is a bit "low-level"... + BrowserSearch.loadSearch("foo", false, "contextmenu"); + } + }, + { + name: "keyword search", + searchURL: base, + run: function () { + gURLBar.value = "? foo"; + gURLBar.focus(); + EventUtils.synthesizeKey("VK_RETURN", {}); + } + }, + { + name: "keyword search", + searchURL: base, + run: function () { + gURLBar.value = "a foo"; + gURLBar.focus(); + EventUtils.synthesizeKey("VK_RETURN", {}); + } + }, + { + name: "search bar search", + searchURL: base, + run: function () { + let sb = BrowserSearch.searchBar; + sb.focus(); + sb.value = "foo"; + registerCleanupFunction(function () { + sb.value = ""; + }); + EventUtils.synthesizeKey("VK_RETURN", {}); + } + }, + { + name: "new tab search", + searchURL: base, + run: function () { + function doSearch(doc) { + // Re-add the listener, and perform a search + gBrowser.addProgressListener(listener); + doc.getElementById("newtab-search-text").value = "foo"; + doc.getElementById("newtab-search-submit").click(); + } + + // load about:newtab, but remove the listener first so it doesn't + // get in the way + gBrowser.removeProgressListener(listener); + gBrowser.loadURI("about:newtab"); + info("Waiting for about:newtab load"); + tab.linkedBrowser.addEventListener("load", function load(event) { + if (event.originalTarget != tab.linkedBrowser.contentDocument || + event.target.location.href == "about:blank") { + info("skipping spurious load event"); + return; + } + tab.linkedBrowser.removeEventListener("load", load, true); + + // Observe page setup + let win = gBrowser.contentWindow; + if (win.gSearch.currentEngineName == + Services.search.currentEngine.name) { + doSearch(win.document); + } + else { + info("Waiting for newtab search init"); + win.addEventListener("ContentSearchService", function done(event) { + info("Got newtab search event " + event.detail.type); + if (event.detail.type == "State") { + win.removeEventListener("ContentSearchService", done); + // Let gSearch respond to the event before continuing. + executeSoon(() => doSearch(win.document)); + } + }); + } + }, true); + } + } + ]; + + function nextTest() { + if (gTests.length) { + gCurrTest = gTests.shift(); + info("Running : " + gCurrTest.name); + executeSoon(gCurrTest.run); + } else { + finish(); + } + } + + let tab = gBrowser.selectedTab = gBrowser.addTab(); + + let listener = { + onStateChange: function onStateChange(webProgress, req, flags, status) { + info("onStateChange"); + // Only care about top-level document starts + let docStart = Ci.nsIWebProgressListener.STATE_IS_DOCUMENT | + Ci.nsIWebProgressListener.STATE_START; + if (!(flags & docStart) || !webProgress.isTopLevel) + return; + + if (req.originalURI.spec == "about:blank") + return; + + info("received document start"); + + ok(req instanceof Ci.nsIChannel, "req is a channel"); + is(req.originalURI.spec, gCurrTest.searchURL, "search URL was loaded"); + info("Actual URI: " + req.URI.spec); + + req.cancel(Components.results.NS_ERROR_FAILURE); + + executeSoon(nextTest); + } + } + + registerCleanupFunction(function () { + engine.alias = undefined; + gBrowser.removeProgressListener(listener); + gBrowser.removeTab(tab); + Services.search.currentEngine = previouslySelectedEngine; + }); + + tab.linkedBrowser.addEventListener("load", function load() { + tab.linkedBrowser.removeEventListener("load", load, true); + gBrowser.addProgressListener(listener); + nextTest(); + }, true); +} diff --git a/browser/components/search/test/browser_bing.js b/browser/components/search/test/browser_bing.js new file mode 100644 index 000000000..3a41ae0ac --- /dev/null +++ b/browser/components/search/test/browser_bing.js @@ -0,0 +1,118 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ */ + +/* + * Test Bing search plugin URLs + */ + +"use strict"; + +const BROWSER_SEARCH_PREF = "browser.search."; + +function test() { + let engine = Services.search.getEngineByName("Bing"); + ok(engine, "Bing"); + + let base = "https://www.bing.com/search?q=foo&pc=MOZI"; + let url; + + // Test search URLs (including purposes). + url = engine.getSubmission("foo").uri.spec; + is(url, base + "&form=MOZSBR", "Check search URL for 'foo'"); + url = engine.getSubmission("foo", null, "contextmenu").uri.spec; + is(url, base + "&form=MOZCON", "Check context menu search URL for 'foo'"); + url = engine.getSubmission("foo", null, "keyword").uri.spec; + is(url, base + "&form=MOZLBR", "Check keyword search URL for 'foo'"); + url = engine.getSubmission("foo", null, "searchbar").uri.spec; + is(url, base + "&form=MOZSBR", "Check search bar search URL for 'foo'"); + url = engine.getSubmission("foo", null, "homepage").uri.spec; + is(url, base + "&form=MOZSPG", "Check homepage search URL for 'foo'"); + url = engine.getSubmission("foo", null, "newtab").uri.spec; + is(url, base + "&form=MOZTSB", "Check newtab search URL for 'foo'"); + + // Check search suggestion URL. + url = engine.getSubmission("foo", "application/x-suggestions+json").uri.spec; + is(url, "https://www.bing.com/osjson.aspx?query=foo&form=OSDJAS&language=" + getLocale(), "Check search suggestion URL for 'foo'"); + + // Check all other engine properties. + const EXPECTED_ENGINE = { + name: "Bing", + alias: null, + description: "Bing. Search by Microsoft.", + searchForm: "https://www.bing.com/search?q=&pc=MOZI&form=MOZSBR", + hidden: false, + wrappedJSObject: { + queryCharset: "UTF-8", + "_iconURL": "data:image/x-icon;base64,AAABAAIAICAAAAEACACoCAAAJgAAABAQAAABAAgAaAUAAM4IAAAoAAAAIAAAAEAAAAABAAgAAAAAAIAEAAAAAAAAAAAAAAABAAAAAAAAhIQMAI+PIwCXlzEAn59BAKioUwCurl8AtbVuAL6+fwDJyZMA0tKlANjYsgDe3r4A5eXMAOzs2QDy8uUA+fnzAP///wAAGi8AAC1QAAA/cAAAUZAAAGOwAAB2zwAAiPAAEZj/ADGm/wBRs/8AccH/AJHP/wCx3f8A0ev/AP///wAAAAAAACwvAABLUAAAaHAAAIaQAAClsAAAw88AAOHwABHv/wAx8f8AUfP/AHH1/wCR9/8Asfn/ANH7/wD///8AAAAAAAAvIQAAUDcAAHBMAACQYwAAsHkAAM+PAADwpgAR/7QAMf++AFH/yABx/9MAkf/cALH/5QDR//AA////AAAAAAAALw4AAFAYAABwIgAAkCwA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AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA=", + _urls : [ + { + type: "application/x-suggestions+json", + method: "GET", + template: "https://www.bing.com/osjson.aspx", + params: [ + { + name: "query", + value: "{searchTerms}", + purpose: undefined, + }, + { + name: "form", + value: "OSDJAS", + purpose: undefined, + }, + { + name: "language", + value: "{moz:locale}", + purpose: undefined, + }, + ], + }, + { + type: "text/html", + method: "GET", + template: "https://www.bing.com/search", + params: [ + { + name: "q", + value: "{searchTerms}", + purpose: undefined, + }, + { + name: "pc", + value: "MOZI", + purpose: undefined, + }, + { + name: "form", + value: "MOZCON", + purpose: "contextmenu", + }, + { + name: "form", + value: "MOZSBR", + purpose: "searchbar", + }, + { + name: "form", + value: "MOZSPG", + purpose: "homepage", + }, + { + name: "form", + value: "MOZLBR", + purpose:"keyword", + }, + { + name: "form", + value: "MOZTSB", + purpose: "newtab", + }, + ], + mozparams: {}, + }, + ], + }, + }; + + isSubObjectOf(EXPECTED_ENGINE, engine, "Bing"); +} diff --git a/browser/components/search/test/browser_bing_behavior.js b/browser/components/search/test/browser_bing_behavior.js new file mode 100644 index 000000000..bc9b187ec --- /dev/null +++ b/browser/components/search/test/browser_bing_behavior.js @@ -0,0 +1,166 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ */ + +/* + * Test Bing search plugin URLs + */ + +"use strict"; + +const BROWSER_SEARCH_PREF = "browser.search."; + + +function test() { + let engine = Services.search.getEngineByName("Bing"); + ok(engine, "Bing is installed"); + + let previouslySelectedEngine = Services.search.currentEngine; + Services.search.currentEngine = engine; + engine.alias = "b"; + + let base = "https://www.bing.com/search?q=foo&pc=MOZI"; + let url; + + // Test search URLs (including purposes). + url = engine.getSubmission("foo").uri.spec; + is(url, base + "&form=MOZSBR", "Check search URL for 'foo'"); + + waitForExplicitFinish(); + + var gCurrTest; + var gTests = [ + { + name: "context menu search", + searchURL: base + "&form=MOZCON", + run: function () { + // Simulate a contextmenu search + // FIXME: This is a bit "low-level"... + BrowserSearch.loadSearch("foo", false, "contextmenu"); + } + }, + { + name: "keyword search", + searchURL: base + "&form=MOZLBR", + run: function () { + gURLBar.value = "? foo"; + gURLBar.focus(); + EventUtils.synthesizeKey("VK_RETURN", {}); + } + }, + { + name: "keyword search with alias", + searchURL: base + "&form=MOZLBR", + run: function () { + gURLBar.value = "b foo"; + gURLBar.focus(); + EventUtils.synthesizeKey("VK_RETURN", {}); + } + }, + { + name: "search bar search", + searchURL: base + "&form=MOZSBR", + run: function () { + let sb = BrowserSearch.searchBar; + sb.focus(); + sb.value = "foo"; + registerCleanupFunction(function () { + sb.value = ""; + }); + EventUtils.synthesizeKey("VK_RETURN", {}); + } + }, + { + name: "new tab search", + searchURL: base + "&form=MOZTSB", + run: function () { + function doSearch(doc) { + // Re-add the listener, and perform a search + gBrowser.addProgressListener(listener); + doc.getElementById("newtab-search-text").value = "foo"; + doc.getElementById("newtab-search-submit").click(); + } + + // load about:newtab, but remove the listener first so it doesn't + // get in the way + gBrowser.removeProgressListener(listener); + gBrowser.loadURI("about:newtab"); + info("Waiting for about:newtab load"); + tab.linkedBrowser.addEventListener("load", function load(event) { + if (event.originalTarget != tab.linkedBrowser.contentDocument || + event.target.location.href == "about:blank") { + info("skipping spurious load event"); + return; + } + tab.linkedBrowser.removeEventListener("load", load, true); + + // Observe page setup + let win = gBrowser.contentWindow; + if (win.gSearch.currentEngineName == + Services.search.currentEngine.name) { + doSearch(win.document); + } + else { + info("Waiting for newtab search init"); + win.addEventListener("ContentSearchService", function done(event) { + info("Got newtab search event " + event.detail.type); + if (event.detail.type == "State") { + win.removeEventListener("ContentSearchService", done); + // Let gSearch respond to the event before continuing. + executeSoon(() => doSearch(win.document)); + } + }); + } + }, true); + } + } + ]; + + function nextTest() { + if (gTests.length) { + gCurrTest = gTests.shift(); + info("Running : " + gCurrTest.name); + executeSoon(gCurrTest.run); + } else { + finish(); + } + } + + let tab = gBrowser.selectedTab = gBrowser.addTab(); + + let listener = { + onStateChange: function onStateChange(webProgress, req, flags, status) { + info("onStateChange"); + // Only care about top-level document starts + let docStart = Ci.nsIWebProgressListener.STATE_IS_DOCUMENT | + Ci.nsIWebProgressListener.STATE_START; + if (!(flags & docStart) || !webProgress.isTopLevel) + return; + + if (req.originalURI.spec == "about:blank") + return; + + info("received document start"); + + ok(req instanceof Ci.nsIChannel, "req is a channel"); + is(req.originalURI.spec, gCurrTest.searchURL, "search URL was loaded"); + info("Actual URI: " + req.URI.spec); + + req.cancel(Components.results.NS_ERROR_FAILURE); + + executeSoon(nextTest); + } + } + + registerCleanupFunction(function () { + engine.alias = undefined; + gBrowser.removeProgressListener(listener); + gBrowser.removeTab(tab); + Services.search.currentEngine = previouslySelectedEngine; + }); + + tab.linkedBrowser.addEventListener("load", function load() { + tab.linkedBrowser.removeEventListener("load", load, true); + gBrowser.addProgressListener(listener); + nextTest(); + }, true); +} diff --git a/browser/components/search/test/browser_contextSearchTabPosition.js b/browser/components/search/test/browser_contextSearchTabPosition.js new file mode 100644 index 000000000..21a8c1130 --- /dev/null +++ b/browser/components/search/test/browser_contextSearchTabPosition.js @@ -0,0 +1,62 @@ +/* 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/. */ + +add_task(function* test() { + yield SpecialPowers.pushPrefEnv({set: [["toolkit.telemetry.enabled", true]]}); + let engine = yield promiseNewEngine("testEngine.xml"); + let histogramKey = "other-" + engine.name + ".contextmenu"; + let numSearchesBefore = 0; + + try { + let hs = Services.telemetry.getKeyedHistogramById("SEARCH_COUNTS").snapshot(); + if (histogramKey in hs) { + numSearchesBefore = hs[histogramKey].sum; + } + } catch (ex) { + // No searches performed yet, not a problem, |numSearchesBefore| is 0. + } + + let tabs = []; + let tabsLoadedDeferred = new Deferred(); + + function tabAdded(event) { + let tab = event.target; + tabs.push(tab); + + // We wait for the blank tab and the two context searches tabs to open. + if (tabs.length == 3) { + tabsLoadedDeferred.resolve(); + } + } + + let container = gBrowser.tabContainer; + container.addEventListener("TabOpen", tabAdded, false); + + gBrowser.addTab("about:blank"); + BrowserSearch.loadSearchFromContext("mozilla"); + BrowserSearch.loadSearchFromContext("firefox"); + + // Wait for all the tabs to open. + yield tabsLoadedDeferred.promise; + + is(tabs[0], gBrowser.tabs[3], "blank tab has been pushed to the end"); + is(tabs[1], gBrowser.tabs[1], "first search tab opens next to the current tab"); + is(tabs[2], gBrowser.tabs[2], "second search tab opens next to the first search tab"); + + container.removeEventListener("TabOpen", tabAdded, false); + tabs.forEach(gBrowser.removeTab, gBrowser); + + // Make sure that the context searches are correctly recorded. + let hs = Services.telemetry.getKeyedHistogramById("SEARCH_COUNTS").snapshot(); + Assert.ok(histogramKey in hs, "The histogram must contain the correct key"); + Assert.equal(hs[histogramKey].sum, numSearchesBefore + 2, + "The histogram must contain the correct search count"); +}); + +function Deferred() { + this.promise = new Promise((resolve, reject) => { + this.resolve = resolve; + this.reject = reject; + }); +} diff --git a/browser/components/search/test/browser_contextmenu.js b/browser/components/search/test/browser_contextmenu.js new file mode 100644 index 000000000..c485242b4 --- /dev/null +++ b/browser/components/search/test/browser_contextmenu.js @@ -0,0 +1,101 @@ +/* Any copyright is dedicated to the Public Domain. + * * http://creativecommons.org/publicdomain/zero/1.0/ */ +/* + * Test searching for the selected text using the context menu + */ + +add_task(function* () { + const ss = Services.search; + const ENGINE_NAME = "Foo"; + var contextMenu; + + // We want select events to be fired. + yield new Promise(resolve => SpecialPowers.pushPrefEnv({"set": [["dom.select_events.enabled", true]]}, resolve)); + + let envService = Cc["@mozilla.org/process/environment;1"].getService(Ci.nsIEnvironment); + let originalValue = envService.get("XPCSHELL_TEST_PROFILE_DIR"); + envService.set("XPCSHELL_TEST_PROFILE_DIR", "1"); + + let url = "chrome://mochitests/content/browser/browser/components/search/test/"; + let resProt = Services.io.getProtocolHandler("resource") + .QueryInterface(Ci.nsIResProtocolHandler); + let originalSubstitution = resProt.getSubstitution("search-plugins"); + resProt.setSubstitution("search-plugins", + Services.io.newURI(url, null, null)); + + let searchDonePromise; + yield new Promise(resolve => { + function observer(aSub, aTopic, aData) { + switch (aData) { + case "engine-added": + var engine = ss.getEngineByName(ENGINE_NAME); + ok(engine, "Engine was added."); + ss.currentEngine = engine; + envService.set("XPCSHELL_TEST_PROFILE_DIR", originalValue); + resProt.setSubstitution("search-plugins", originalSubstitution); + break; + case "engine-current": + is(ss.currentEngine.name, ENGINE_NAME, "currentEngine set"); + resolve(); + break; + case "engine-removed": + Services.obs.removeObserver(observer, "browser-search-engine-modified"); + if (searchDonePromise) { + searchDonePromise(); + } + break; + } + } + + Services.obs.addObserver(observer, "browser-search-engine-modified", false); + ss.addEngine("resource://search-plugins/testEngine_mozsearch.xml", + null, "data:image/x-icon,%00", false); + }); + + contextMenu = document.getElementById("contentAreaContextMenu"); + ok(contextMenu, "Got context menu XUL"); + + let tab = yield BrowserTestUtils.openNewForegroundTab(gBrowser, "data:text/plain;charset=utf8,test%20search"); + + yield ContentTask.spawn(tab.linkedBrowser, "", function*() { + return new Promise(resolve => { + content.document.addEventListener("selectionchange", function selectionChanged() { + content.document.removeEventListener("selectionchange", selectionChanged); + resolve(); + }); + content.document.getSelection().selectAllChildren(content.document.body); + }); + }); + + var eventDetails = { type: "contextmenu", button: 2 }; + + let popupPromise = BrowserTestUtils.waitForEvent(contextMenu, "popupshown"); + BrowserTestUtils.synthesizeMouseAtCenter("body", eventDetails, gBrowser.selectedBrowser); + yield popupPromise; + + info("checkContextMenu"); + var searchItem = contextMenu.getElementsByAttribute("id", "context-searchselect")[0]; + ok(searchItem, "Got search context menu item"); + is(searchItem.label, 'Search ' + ENGINE_NAME + ' for \u201ctest search\u201d', "Check context menu label"); + is(searchItem.disabled, false, "Check that search context menu item is enabled"); + + yield BrowserTestUtils.openNewForegroundTab(gBrowser, () => { + searchItem.click(); + }); + + is(gBrowser.currentURI.spec, + "http://mochi.test:8888/browser/browser/components/search/test/?test=test+search&ie=utf-8&channel=contextsearch", + "Checking context menu search URL"); + + contextMenu.hidePopup(); + + // Remove the tab opened by the search + gBrowser.removeCurrentTab(); + + yield new Promise(resolve => { + searchDonePromise = resolve; + ss.removeEngine(ss.currentEngine); + }); + + gBrowser.removeCurrentTab(); +}); diff --git a/browser/components/search/test/browser_google.js b/browser/components/search/test/browser_google.js new file mode 100644 index 000000000..2b0cabea7 --- /dev/null +++ b/browser/components/search/test/browser_google.js @@ -0,0 +1,100 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ */ + +/* + * Test Google search plugin URLs + */ + +"use strict"; + +function test() { + let engine = Services.search.getEngineByName("Google"); + ok(engine, "Google"); + + let base = "https://www.google.com/search?q=foo&ie=utf-8&oe=utf-8&client=firefox-b"; + let keywordBase = base + "-ab"; + + let url; + + // Test search URLs (including purposes). + url = engine.getSubmission("foo").uri.spec; + is(url, base, "Check search URL for 'foo'"); + url = engine.getSubmission("foo", null, "contextmenu").uri.spec; + is(url, base, "Check context menu search URL for 'foo'"); + url = engine.getSubmission("foo", null, "keyword").uri.spec; + is(url, keywordBase, "Check keyword search URL for 'foo'"); + url = engine.getSubmission("foo", null, "searchbar").uri.spec; + is(url, base, "Check search bar search URL for 'foo'"); + url = engine.getSubmission("foo", null, "homepage").uri.spec; + is(url, base, "Check homepage search URL for 'foo'"); + url = engine.getSubmission("foo", null, "newtab").uri.spec; + is(url, base, "Check newtab search URL for 'foo'"); + + // Check search suggestion URL. + url = engine.getSubmission("foo", "application/x-suggestions+json").uri.spec; + is(url, "https://www.google.com/complete/search?client=firefox&q=foo", "Check search suggestion URL for 'foo'"); + + // Check result parsing and alternate domains. + let alternateBase = base.replace("www.google.com", "www.google.fr"); + is(Services.search.parseSubmissionURL(base).terms, "foo", + "Check result parsing"); + is(Services.search.parseSubmissionURL(alternateBase).terms, "foo", + "Check alternate domain"); + + // Check all other engine properties. + const EXPECTED_ENGINE = { + name: "Google", + alias: null, + description: "Google Search", + searchForm: "https://www.google.com/search?q=&ie=utf-8&oe=utf-8&client=firefox-b", + hidden: false, + wrappedJSObject: { + queryCharset: "UTF-8", + "_iconURL": "data:image/x-icon;base64,AAABAAIAEBAAAAEAIABoBAAAJgAAACAgAAABACAAqBAAAI4EAAAoAAAAEAAAACAAAAABACAAAAAAAAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP///zD9/f2W/f392P39/fn9/f35/f391/39/ZT+/v4uAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA/v7+Cf39/Zn///////////////////////////////////////////39/ZX///8IAAAAAAAAAAAAAAAA/v7+Cf39/cH/////+v35/7TZp/92ul3/WKs6/1iqOv9yuFn/rNWd//j79v///////f39v////wgAAAAAAAAAAP39/Zn/////7PXp/3G3WP9TqDT/U6g0/1OoNP9TqDT/U6g0/1OoNP+Or1j//vDo///////9/f2VAAAAAP///zD/////+vz5/3G3V/9TqDT/WKo6/6LQkf/U6cz/1urO/6rUm/+Zo0r/8IZB//adZ////v7///////7+/i79/f2Y/////4nWzf9Lqkj/Vqo4/9Xqzv///////////////////////ebY//SHRv/0hUL//NjD///////9/f2U/f392v////8sxPH/Ebzt/43RsP/////////////////////////////////4roL/9IVC//i1jf///////f391/39/fr/////Cr37/wW8+/+16/7/////////////////9IVC//SFQv/0hUL/9IVC//SFQv/3pnX///////39/fn9/f36/////wu++/8FvPv/tuz+//////////////////SFQv/0hUL/9IVC//SFQv/0hUL/96p7///////9/f35/f392/////81yfz/CrL5/2uk9v///////////////////////////////////////////////////////f392P39/Zn/////ks/7/zdS7P84Rur/0NT6///////////////////////9/f////////////////////////39/Zb+/v4y//////n5/v9WYu3/NUPq/ztJ6/+VnPT/z9L6/9HU+v+WnfT/Ul7t/+Hj/P////////////////////8wAAAAAP39/Z3/////6Or9/1hj7v81Q+r/NUPq/zVD6v81Q+r/NUPq/zVD6v9sdvD////////////9/f2YAAAAAAAAAAD///8K/f39w//////5+f7/paz2/11p7v88Suv/Okfq/1pm7v+iqfX/+fn+///////9/f3B/v7+CQAAAAAAAAAAAAAAAP///wr9/f2d///////////////////////////////////////////9/f2Z/v7+CQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP7+/jL9/f2Z/f392/39/fr9/f36/f392v39/Zj///8wAAAAAAAAAAAAAAAAAAAAAPAPAADAAwAAgAEAAIABAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAIABAACAAQAAwAMAAPAPAAAoAAAAIAAAAEAAAAABACAAAAAAAAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP7+/g3+/v5X/f39mf39/cj9/f3q/f39+f39/fn9/f3q/f39yP39/Zn+/v5W////DAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP7+/iT9/f2c/f399f/////////////////////////////////////////////////////9/f31/f39mv7+/iMAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP7+/gn9/f2K/f39+////////////////////////////////////////////////////////////////////////////f39+v39/Yf///8IAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD+/v4k/f390v////////////////////////////////////////////////////////////////////////////////////////////////39/dD///8iAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA////MP39/er//////////////////////////+r05v+v16H/gsBs/2WxSf9Wqjj/Vqk3/2OwRv99vWX/pdKV/97u2P////////////////////////////39/ej+/v4vAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP7+/iT9/f3q/////////////////////+v15/+Pxnv/VKk2/1OoNP9TqDT/U6g0/1OoNP9TqDT/U6g0/1OoNP9TqDT/U6g0/36+Z//d7tf///////////////////////39/ej///8iAAAAAAAAAAAAAAAAAAAAAAAAAAD///8K/f390//////////////////////E4bn/XKw+/1OoNP9TqDT/U6g0/1OoNP9TqDT/U6g0/1OoNP9TqDT/U6g0/1OoNP9TqDT/U6g0/1apN/+x0pv///////////////////////39/dD///8IAAAAAAAAAAAAAAAAAAAAAP39/Yv/////////////////////sdij/1OoNP9TqDT/U6g0/1OoNP9TqDT/U6g0/1OoNP9TqDT/U6g0/1OoNP9TqDT/U6g0/1OoNP9TqDT/YKU1/8qOPv/5wZ////////////////////////39/YcAAAAAAAAAAAAAAAD+/v4l/f39+////////////////8Lgt/9TqDT/U6g0/1OoNP9TqDT/U6g0/1OoNP9utlT/n86N/7faqv+426v/pdKV/3u8ZP9UqDX/U6g0/3egN//jiUH/9IVC//SFQv/82MP//////////////////f39+v7+/iMAAAAAAAAAAP39/Z3////////////////q9Ob/W6w+/1OoNP9TqDT/U6g0/1OoNP9nskz/zOXC/////////////////////////////////+Dv2v+osWP/8YVC//SFQv/0hUL/9IVC//WQVP/++fb//////////////////f39mgAAAAD+/v4O/f399v///////////////4LHj/9TqDT/U6g0/1OoNP9TqDT/dblc//L58P/////////////////////////////////////////////8+v/3p3f/9IVC//SFQv/0hUL/9IVC//rIqf/////////////////9/f31////DP7+/ln////////////////f9v7/Cbz2/zOwhv9TqDT/U6g0/2KwRv/v9+z///////////////////////////////////////////////////////738//1kFT/9IVC//SFQv/0hUL/9plg///////////////////////+/v5W/f39nP///////////////4jf/f8FvPv/Bbz7/yG1s/9QqDz/vN2w//////////////////////////////////////////////////////////////////rHqP/0hUL/9IVC//SFQv/0hUL//vDn//////////////////39/Zn9/f3L////////////////R878/wW8+/8FvPv/Bbz7/y7C5P/7/fr//////////////////////////////////////////////////////////////////ere//SFQv/0hUL/9IVC//SFQv/718H//////////////////f39yP39/ez///////////////8cwvv/Bbz7/wW8+/8FvPv/WNL8///////////////////////////////////////0hUL/9IVC//SFQv/0hUL/9IVC//SFQv/0hUL/9IVC//SFQv/0hUL/9IVC//rIqv/////////////////9/f3q/f39+v///////////////we9+/8FvPv/Bbz7/wW8+/993P3///////////////////////////////////////SFQv/0hUL/9IVC//SFQv/0hUL/9IVC//SFQv/0hUL/9IVC//SFQv/0hUL/+cGf//////////////////39/fn9/f36////////////////B737/wW8+/8FvPv/Bbz7/33c/f//////////////////////////////////////9IVC//SFQv/0hUL/9IVC//SFQv/0hUL/9IVC//SFQv/0hUL/9IVC//SFQv/6xaX//////////////////f39+f39/e3///////////////8cwvv/Bbz7/wW8+/8FvPv/WdP8///////////////////////////////////////0hUL/9IVC//SFQv/0hUL/9IVC//SFQv/0hUL/9IVC//SFQv/0hUL/9IVC//vVv//////////////////9/f3q/f39y////////////////0bN/P8FvPv/Bbz7/wW8+/8hrvn/+/v///////////////////////////////////////////////////////////////////////////////////////////////////////////////////39/cj9/f2c////////////////ht/9/wW8+/8FvPv/FZP1/zRJ6/+zuPf//////////////////////////////////////////////////////////////////////////////////////////////////////////////////f39mf7+/lr////////////////d9v7/B7n7/yB38f81Q+r/NUPq/0hV7P/u8P3////////////////////////////////////////////////////////////////////////////////////////////////////////////+/v5X////D/39/ff///////////////9tkPT/NUPq/zVD6v81Q+r/NUPq/2Fs7//y8v7////////////////////////////////////////////09f7//////////////////////////////////////////////////f399f7+/g0AAAAA/f39n////////////////+Tm/P89Suv/NUPq/zVD6v81Q+r/NUPq/1Bc7f/IzPn/////////////////////////////////x8v5/0xY7P+MlPP////////////////////////////////////////////9/f2cAAAAAAAAAAD+/v4n/f39/P///////////////7W69/81Q+r/NUPq/zVD6v81Q+r/NUPq/zVD6v9ZZe7/k5v0/6609/+vtff/lJv0/1pm7v81Q+r/NUPq/zVD6v+GjvL//v7//////////////////////////////f39+/7+/iQAAAAAAAAAAAAAAAD9/f2N/////////////////////6Cn9f81Q+r/NUPq/zVD6v81Q+r/NUPq/zVD6v81Q+r/NUPq/zVD6v81Q+r/NUPq/zVD6v81Q+r/NUPq/zVD6v+BivL////////////////////////////9/f2KAAAAAAAAAAAAAAAAAAAAAP7+/gv9/f3V/////////////////////7W69/8+S+v/NUPq/zVD6v81Q+r/NUPq/zVD6v81Q+r/NUPq/zVD6v81Q+r/NUPq/zVD6v81Q+r/P0zr/7q/+P///////////////////////f390v7+/gkAAAAAAAAAAAAAAAAAAAAAAAAAAP7+/ib9/f3r/////////////////////+Xn/P94gfH/NkTq/zVD6v81Q+r/NUPq/zVD6v81Q+r/NUPq/zVD6v81Q+r/NkTq/3Z/8f/l5/z///////////////////////39/er+/v4kAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP7+/jL9/f3r///////////////////////////k5vz/nqX1/2p08P9IVez/OEbq/zdF6v9GU+z/aHLv/5qh9f/i5Pz////////////////////////////9/f3q////MAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP7+/ib9/f3V/////////////////////////////////////////////////////////////////////////////////////////////////f390v7+/iQAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP///wr9/f2N/f39/P///////////////////////////////////////////////////////////////////////////f39+/39/Yv+/v4JAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAD+/v4n/f39n/39/ff//////////////////////////////////////////////////////f399v39/Z3+/v4lAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA/v7+Dv7+/lr9/f2c/f39y/39/e39/f36/f39+v39/ez9/f3L/f39nP7+/ln+/v4OAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAP/AA///AAD//AAAP/gAAB/wAAAP4AAAB8AAAAPAAAADgAAAAYAAAAEAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACAAAABgAAAAcAAAAPAAAAD4AAAB/AAAA/4AAAf/AAAP/8AAP//wAP/", + _urls : [ + { + type: "application/x-suggestions+json", + method: "GET", + template: "https://www.google.com/complete/search?client=firefox&q={searchTerms}", + params: "", + }, + { + type: "text/html", + method: "GET", + template: "https://www.google.com/search", + params: [ + { + "name": "q", + "value": "{searchTerms}", + "purpose": undefined, + }, + { + "name": "ie", + "value": "utf-8", + "purpose": undefined, + }, + { + "name": "oe", + "value": "utf-8", + "purpose": undefined, + }, + { + "name": "client", + "value": "firefox-b-ab", + "purpose": "keyword", + }, + { + "name": "client", + "value": "firefox-b", + "purpose": "searchbar", + }, + ], + mozparams: { + }, + }, + ], + }, + }; + + isSubObjectOf(EXPECTED_ENGINE, engine, "Google"); +} diff --git a/browser/components/search/test/browser_google_behavior.js b/browser/components/search/test/browser_google_behavior.js new file mode 100644 index 000000000..55405bb29 --- /dev/null +++ b/browser/components/search/test/browser_google_behavior.js @@ -0,0 +1,165 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ */ + +/* + * Test Google search plugin URLs + */ + +"use strict"; + +function test() { + let engine = Services.search.getEngineByName("Google"); + ok(engine, "Google is installed"); + + let previouslySelectedEngine = Services.search.currentEngine; + Services.search.currentEngine = engine; + engine.alias = "g"; + + let base = "https://www.google.com/search?q=foo&ie=utf-8&oe=utf-8&client=firefox-b"; + let keywordBase = base + "-ab"; + + let url; + + // Test search URLs (including purposes). + url = engine.getSubmission("foo").uri.spec; + is(url, base, "Check search URL for 'foo'"); + + waitForExplicitFinish(); + + var gCurrTest; + var gTests = [ + { + name: "context menu search", + searchURL: base, + run: function () { + // Simulate a contextmenu search + // FIXME: This is a bit "low-level"... + BrowserSearch.loadSearch("foo", false, "contextmenu"); + } + }, + { + name: "keyword search", + searchURL: keywordBase, + run: function () { + gURLBar.value = "? foo"; + gURLBar.focus(); + EventUtils.synthesizeKey("VK_RETURN", {}); + } + }, + { + name: "keyword search", + searchURL: keywordBase, + run: function () { + gURLBar.value = "g foo"; + gURLBar.focus(); + EventUtils.synthesizeKey("VK_RETURN", {}); + } + }, + { + name: "search bar search", + searchURL: base, + run: function () { + let sb = BrowserSearch.searchBar; + sb.focus(); + sb.value = "foo"; + registerCleanupFunction(function () { + sb.value = ""; + }); + EventUtils.synthesizeKey("VK_RETURN", {}); + } + }, + { + name: "new tab search", + searchURL: base, + run: function () { + function doSearch(doc) { + // Re-add the listener, and perform a search + gBrowser.addProgressListener(listener); + doc.getElementById("newtab-search-text").value = "foo"; + doc.getElementById("newtab-search-submit").click(); + } + + // load about:newtab, but remove the listener first so it doesn't + // get in the way + gBrowser.removeProgressListener(listener); + gBrowser.loadURI("about:newtab"); + info("Waiting for about:newtab load"); + tab.linkedBrowser.addEventListener("load", function load(event) { + if (event.originalTarget != tab.linkedBrowser.contentDocument || + event.target.location.href == "about:blank") { + info("skipping spurious load event"); + return; + } + tab.linkedBrowser.removeEventListener("load", load, true); + + // Observe page setup + let win = gBrowser.contentWindow; + if (win.gSearch.currentEngineName == + Services.search.currentEngine.name) { + doSearch(win.document); + } + else { + info("Waiting for newtab search init"); + win.addEventListener("ContentSearchService", function done(event) { + info("Got newtab search event " + event.detail.type); + if (event.detail.type == "State") { + win.removeEventListener("ContentSearchService", done); + // Let gSearch respond to the event before continuing. + executeSoon(() => doSearch(win.document)); + } + }); + } + }, true); + } + } + ]; + + function nextTest() { + if (gTests.length) { + gCurrTest = gTests.shift(); + info("Running : " + gCurrTest.name); + executeSoon(gCurrTest.run); + } else { + finish(); + } + } + + let tab = gBrowser.selectedTab = gBrowser.addTab(); + + let listener = { + onStateChange: function onStateChange(webProgress, req, flags, status) { + info("onStateChange"); + // Only care about top-level document starts + let docStart = Ci.nsIWebProgressListener.STATE_IS_DOCUMENT | + Ci.nsIWebProgressListener.STATE_START; + if (!(flags & docStart) || !webProgress.isTopLevel) + return; + + if (req.originalURI.spec == "about:blank") + return; + + info("received document start"); + + ok(req instanceof Ci.nsIChannel, "req is a channel"); + is(req.originalURI.spec, gCurrTest.searchURL, "search URL was loaded"); + info("Actual URI: " + req.URI.spec); + + req.cancel(Components.results.NS_ERROR_FAILURE); + + executeSoon(nextTest); + } + } + + registerCleanupFunction(function () { + engine.alias = undefined; + gBrowser.removeProgressListener(listener); + gBrowser.removeTab(tab); + Services.search.currentEngine = previouslySelectedEngine; + }); + + tab.linkedBrowser.addEventListener("load", function load() { + tab.linkedBrowser.removeEventListener("load", load, true); + gBrowser.addProgressListener(listener); + nextTest(); + }, true); +} diff --git a/browser/components/search/test/browser_google_codes.js b/browser/components/search/test/browser_google_codes.js new file mode 100644 index 000000000..e166b6868 --- /dev/null +++ b/browser/components/search/test/browser_google_codes.js @@ -0,0 +1,161 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +const kUrlPref = "geoSpecificDefaults.url"; +const BROWSER_SEARCH_PREF = "browser.search."; + +var originalGeoURL; + +/** + * Clean the profile of any cache file left from a previous run. + * Returns a boolean indicating if the cache file existed. + */ +function removeCacheFile() +{ + const CACHE_FILENAME = "search.json.mozlz4"; + + let file = Services.dirsvc.get("ProfD", Ci.nsIFile); + file.append(CACHE_FILENAME); + if (file.exists()) { + file.remove(false); + return true; + } + return false; +} + +/** + * Returns a promise that is resolved when an observer notification from the + * search service fires with the specified data. + * + * @param aExpectedData + * The value the observer notification sends that causes us to resolve + * the promise. + */ +function waitForSearchNotification(aExpectedData, aCallback) { + const SEARCH_SERVICE_TOPIC = "browser-search-service"; + Services.obs.addObserver(function observer(aSubject, aTopic, aData) { + if (aData != aExpectedData) + return; + + Services.obs.removeObserver(observer, SEARCH_SERVICE_TOPIC); + aCallback(); + }, SEARCH_SERVICE_TOPIC, false); +} + +function asyncInit() { + return new Promise(resolve => { + Services.search.init(function() { + ok(Services.search.isInitialized, "search service should be initialized"); + resolve(); + }); + }); +} + +function asyncReInit() { + const kLocalePref = "general.useragent.locale"; + + let promise = new Promise(resolve => { + waitForSearchNotification("reinit-complete", resolve); + }); + + Services.search.QueryInterface(Ci.nsIObserver) + .observe(null, "nsPref:changed", kLocalePref); + + return promise; +} + +let gEngineCount; + +add_task(function* preparation() { + // ContentSearch is interferring with our async re-initializations of the + // search service: once _initServicePromise has resolved, it will access + // the search service, thus causing unpredictable behavior due to + // synchronous initializations of the service. + let originalContentSearchPromise = ContentSearch._initServicePromise; + ContentSearch._initServicePromise = new Promise(resolve => { + registerCleanupFunction(() => { + ContentSearch._initServicePromise = originalContentSearchPromise; + resolve(); + }); + }); + + yield asyncInit(); + gEngineCount = Services.search.getVisibleEngines().length; + + waitForSearchNotification("uninit-complete", () => { + // Verify search service is not initialized + is(Services.search.isInitialized, false, "Search service should NOT be initialized"); + + removeCacheFile(); + + // Geo specific defaults won't be fetched if there's no country code. + Services.prefs.setCharPref("browser.search.geoip.url", + 'data:application/json,{"country_code": "US"}'); + + Services.prefs.setBoolPref("browser.search.geoSpecificDefaults", true); + + // Make the new Google the only engine + originalGeoURL = Services.prefs.getCharPref(BROWSER_SEARCH_PREF + kUrlPref); + let geoUrl = 'data:application/json,{"interval": 31536000, "settings": {"searchDefault": "Google", "visibleDefaultEngines": ["google"]}}'; + Services.prefs.getDefaultBranch(BROWSER_SEARCH_PREF).setCharPref(kUrlPref, geoUrl); + }); + + yield asyncReInit(); + + yield new Promise(resolve => { + waitForSearchNotification("write-cache-to-disk-complete", resolve); + }); +}); + +add_task(function* tests() { + let engines = Services.search.getEngines(); + is(Services.search.currentEngine.name, "Google", "Search engine should be Google"); + is(engines.length, 1, "There should only be one engine"); + + let engine = Services.search.getEngineByName("Google"); + ok(engine, "Google"); + + let base = "https://www.google.com/search?q=foo&ie=utf-8&oe=utf-8&client=firefox-b"; + + // Keyword uses a slightly different code + let keywordBase = base + "-ab"; + + let url; + + // Test search URLs (including purposes). + url = engine.getSubmission("foo", null, "contextmenu").uri.spec; + is(url, base, "Check context menu search URL for 'foo'"); + url = engine.getSubmission("foo", null, "keyword").uri.spec; + is(url, keywordBase, "Check keyword search URL for 'foo'"); + url = engine.getSubmission("foo", null, "searchbar").uri.spec; + is(url, base, "Check search bar search URL for 'foo'"); + url = engine.getSubmission("foo", null, "homepage").uri.spec; + is(url, base, "Check homepage search URL for 'foo'"); + url = engine.getSubmission("foo", null, "newtab").uri.spec; + is(url, base, "Check newtab search URL for 'foo'"); + url = engine.getSubmission("foo", null, "system").uri.spec; + is(url, base, "Check system search URL for 'foo'"); +}); + + +add_task(function* cleanup() { + waitForSearchNotification("uninit-complete", () => { + // Verify search service is not initialized + is(Services.search.isInitialized, false, + "Search service should NOT be initialized"); + removeCacheFile(); + + Services.prefs.clearUserPref("browser.search.geoip.url"); + + // We can't clear the pref because it's set to false by testing/profiles/prefs_general.js + Services.prefs.setBoolPref("browser.search.geoSpecificDefaults", false); + + Services.prefs.getDefaultBranch(BROWSER_SEARCH_PREF).setCharPref(kUrlPref, originalGeoURL); + }); + + yield asyncReInit(); + is(gEngineCount, Services.search.getVisibleEngines().length, + "correct engine count after cleanup"); +}); diff --git a/browser/components/search/test/browser_healthreport.js b/browser/components/search/test/browser_healthreport.js new file mode 100644 index 000000000..c68ad174c --- /dev/null +++ b/browser/components/search/test/browser_healthreport.js @@ -0,0 +1,82 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ */ + +"use strict"; + +var Preferences = Cu.import("resource://gre/modules/Preferences.jsm", {}).Preferences; + +function test() { + waitForExplicitFinish(); + resetPreferences(); + + function testTelemetry() { + // Find the right bucket for the "Foo" engine. + let engine = Services.search.getEngineByName("Foo"); + let histogramKey = (engine.identifier || "other-Foo") + ".searchbar"; + let numSearchesBefore = 0; + try { + let hs = Services.telemetry.getKeyedHistogramById("SEARCH_COUNTS").snapshot(); + if (histogramKey in hs) { + numSearchesBefore = hs[histogramKey].sum; + } + } catch (ex) { + // No searches performed yet, not a problem, |numSearchesBefore| is 0. + } + + // Now perform a search and ensure the count is incremented. + let tab = gBrowser.addTab(); + gBrowser.selectedTab = tab; + let searchBar = BrowserSearch.searchBar; + + searchBar.value = "firefox health report"; + searchBar.focus(); + + function afterSearch() { + searchBar.value = ""; + gBrowser.removeTab(tab); + + // Make sure that the context searches are correctly recorded. + let hs = Services.telemetry.getKeyedHistogramById("SEARCH_COUNTS").snapshot(); + Assert.ok(histogramKey in hs, "The histogram must contain the correct key"); + Assert.equal(hs[histogramKey].sum, numSearchesBefore + 1, + "Performing a search increments the related SEARCH_COUNTS key by 1."); + + let engine = Services.search.getEngineByName("Foo"); + Services.search.removeEngine(engine); + } + + EventUtils.synthesizeKey("VK_RETURN", {}); + executeSoon(() => executeSoon(afterSearch)); + } + + function observer(subject, topic, data) { + switch (data) { + case "engine-added": + let engine = Services.search.getEngineByName("Foo"); + ok(engine, "Engine was added."); + Services.search.currentEngine = engine; + break; + + case "engine-current": + is(Services.search.currentEngine.name, "Foo", "Current engine is Foo"); + testTelemetry(); + break; + + case "engine-removed": + Services.obs.removeObserver(observer, "browser-search-engine-modified"); + finish(); + break; + } + } + + Services.obs.addObserver(observer, "browser-search-engine-modified", false); + SpecialPowers.pushPrefEnv({set: [["toolkit.telemetry.enabled", true]]}).then(function() { + Services.search.addEngine("http://mochi.test:8888/browser/browser/components/search/test/testEngine.xml", + null, "data:image/x-icon,%00", false); + }); +} + +function resetPreferences() { + Preferences.resetBranch("datareporting.policy."); + Preferences.set("datareporting.policy.dataSubmissionPolicyBypassNotification", true); +} diff --git a/browser/components/search/test/browser_hiddenOneOffs_cleanup.js b/browser/components/search/test/browser_hiddenOneOffs_cleanup.js new file mode 100644 index 000000000..9a584feb6 --- /dev/null +++ b/browser/components/search/test/browser_hiddenOneOffs_cleanup.js @@ -0,0 +1,99 @@ +/* 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/. */ +const testPref = "Foo,FooDupe"; + +function promiseNewEngine(basename) { + return new Promise((resolve, reject) => { + info("Waiting for engine to be added: " + basename); + Services.search.init({ + onInitComplete: function() { + let url = getRootDirectory(gTestPath) + basename; + Services.search.addEngine(url, null, "", false, { + onSuccess: function (engine) { + info("Search engine added: " + basename); + resolve(engine); + }, + onError: function (errCode) { + ok(false, "addEngine failed with error code " + errCode); + reject(); + } + }); + } + }); + }); +} + +add_task(function* test_remove() { + yield promiseNewEngine("testEngine_dupe.xml"); + yield promiseNewEngine("testEngine.xml"); + Services.prefs.setCharPref("browser.search.hiddenOneOffs", testPref); + + info("Removing testEngine_dupe.xml"); + Services.search.removeEngine(Services.search.getEngineByName("FooDupe")); + + let hiddenOneOffs = + Services.prefs.getCharPref("browser.search.hiddenOneOffs").split(","); + + is(hiddenOneOffs.length, 1, + "hiddenOneOffs has the correct engine count post removal."); + is(hiddenOneOffs.some(x => x == "FooDupe"), false, + "Removed Engine is not in hiddenOneOffs after removal"); + is(hiddenOneOffs.some(x => x == "Foo"), true, + "Current hidden engine is not affected by removal."); + + info("Removing testEngine.xml"); + Services.search.removeEngine(Services.search.getEngineByName("Foo")); + + is(Services.prefs.getCharPref("browser.search.hiddenOneOffs"), "", + "hiddenOneOffs is empty after removing all hidden engines."); +}); + +add_task(function* test_add() { + yield promiseNewEngine("testEngine.xml"); + info("setting prefs to " + testPref); + Services.prefs.setCharPref("browser.search.hiddenOneOffs", testPref); + yield promiseNewEngine("testEngine_dupe.xml"); + + let hiddenOneOffs = + Services.prefs.getCharPref("browser.search.hiddenOneOffs").split(","); + + is(hiddenOneOffs.length, 1, + "hiddenOneOffs has the correct number of hidden engines present post add."); + is(hiddenOneOffs.some(x => x == "FooDupe"), false, + "Added engine is not present in hidden list."); + is(hiddenOneOffs.some(x => x == "Foo"), true, + "Adding an engine does not remove engines from hidden list."); +}); + +add_task(function* test_diacritics() { + const diacritic_engine = "Foo \u2661"; + let Preferences = + Cu.import("resource://gre/modules/Preferences.jsm", {}).Preferences; + + Preferences.set("browser.search.hiddenOneOffs", diacritic_engine); + yield promiseNewEngine("testEngine_diacritics.xml"); + + let hiddenOneOffs = + Preferences.get("browser.search.hiddenOneOffs").split(","); + is(hiddenOneOffs.some(x => x == diacritic_engine), false, + "Observer cleans up added hidden engines that include a diacritic."); + + Preferences.set("browser.search.hiddenOneOffs", diacritic_engine); + + info("Removing testEngine_diacritics.xml"); + Services.search.removeEngine(Services.search.getEngineByName(diacritic_engine)); + + hiddenOneOffs = + Preferences.get("browser.search.hiddenOneOffs").split(","); + is(hiddenOneOffs.some(x => x == diacritic_engine), false, + "Observer cleans up removed hidden engines that include a diacritic."); +}); + +registerCleanupFunction(() => { + info("Removing testEngine.xml"); + Services.search.removeEngine(Services.search.getEngineByName("Foo")); + info("Removing testEngine_dupe.xml"); + Services.search.removeEngine(Services.search.getEngineByName("FooDupe")); + Services.prefs.clearUserPref("browser.search.hiddenOneOffs"); +}); diff --git a/browser/components/search/test/browser_hiddenOneOffs_diacritics.js b/browser/components/search/test/browser_hiddenOneOffs_diacritics.js new file mode 100644 index 000000000..db24c7192 --- /dev/null +++ b/browser/components/search/test/browser_hiddenOneOffs_diacritics.js @@ -0,0 +1,59 @@ +/* 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/. */ +// Tests that keyboard navigation in the search panel works as designed. + +const searchbar = document.getElementById("searchbar"); +const textbox = searchbar._textbox; +const searchPopup = document.getElementById("PopupSearchAutoComplete"); +const searchIcon = document.getAnonymousElementByAttribute(searchbar, "anonid", + "searchbar-search-button"); + +const diacritic_engine = "Foo \u2661"; + +var Preferences = + Cu.import("resource://gre/modules/Preferences.jsm", {}).Preferences; + +add_task(function* init() { + let currentEngine = Services.search.currentEngine; + yield promiseNewEngine("testEngine_diacritics.xml", {setAsCurrent: false}); + registerCleanupFunction(() => { + Services.search.currentEngine = currentEngine; + Services.prefs.clearUserPref("browser.search.hiddenOneOffs"); + }); +}); + +add_task(function* test_hidden() { + Preferences.set("browser.search.hiddenOneOffs", diacritic_engine); + + let promise = promiseEvent(searchPopup, "popupshown"); + info("Opening search panel"); + EventUtils.synthesizeMouseAtCenter(searchIcon, {}); + yield promise; + + ok(!getOneOffs().some(x => x.getAttribute("tooltiptext") == diacritic_engine), + "Search engines with diacritics are hidden when added to hiddenOneOffs preference."); + + promise = promiseEvent(searchPopup, "popuphidden"); + info("Closing search panel"); + EventUtils.synthesizeKey("VK_ESCAPE", {}); + yield promise; +}); + +add_task(function* test_shown() { + Preferences.set("browser.search.hiddenOneOffs", ""); + + let promise = promiseEvent(searchPopup, "popupshown"); + info("Opening search panel"); + SimpleTest.executeSoon(() => { + EventUtils.synthesizeMouseAtCenter(searchIcon, {}); + }); + yield promise; + + ok(getOneOffs().some(x => x.getAttribute("tooltiptext") == diacritic_engine), + "Search engines with diacritics are shown when removed from hiddenOneOffs preference."); + + promise = promiseEvent(searchPopup, "popuphidden"); + searchPopup.hidePopup(); + yield promise; +}); diff --git a/browser/components/search/test/browser_oneOffContextMenu.js b/browser/components/search/test/browser_oneOffContextMenu.js new file mode 100644 index 000000000..69207923b --- /dev/null +++ b/browser/components/search/test/browser_oneOffContextMenu.js @@ -0,0 +1,105 @@ +"use strict"; + +const TEST_ENGINE_NAME = "Foo"; +const TEST_ENGINE_BASENAME = "testEngine.xml"; + +const searchbar = document.getElementById("searchbar"); +const searchPopup = document.getElementById("PopupSearchAutoComplete"); +const searchIcon = document.getAnonymousElementByAttribute( + searchbar, "anonid", "searchbar-search-button" +); +const oneOffBinding = document.getAnonymousElementByAttribute( + searchPopup, "anonid", "search-one-off-buttons" +); +const contextMenu = document.getAnonymousElementByAttribute( + oneOffBinding, "anonid", "search-one-offs-context-menu" +); +const oneOffButtons = document.getAnonymousElementByAttribute( + oneOffBinding, "anonid", "search-panel-one-offs" +); +const searchInNewTabMenuItem = document.getAnonymousElementByAttribute( + oneOffBinding, "anonid", "search-one-offs-context-open-in-new-tab" +); + +add_task(function* init() { + yield promiseNewEngine(TEST_ENGINE_BASENAME, { + setAsCurrent: false, + }); +}); + +add_task(function* extendedTelemetryDisabled() { + yield SpecialPowers.pushPrefEnv({set: [["toolkit.telemetry.enabled", false]]}); + yield doTest(); + checkTelemetry("other"); +}); + +add_task(function* extendedTelemetryEnabled() { + yield SpecialPowers.pushPrefEnv({set: [["toolkit.telemetry.enabled", true]]}); + yield doTest(); + checkTelemetry("other-" + TEST_ENGINE_NAME); +}); + +function* doTest() { + // Open the popup. + let promise = promiseEvent(searchPopup, "popupshown"); + info("Opening search panel"); + EventUtils.synthesizeMouseAtCenter(searchIcon, {}); + yield promise; + + // Get the one-off button for the test engine. + let oneOffButton; + for (let node of oneOffButtons.childNodes) { + if (node.engine && node.engine.name == TEST_ENGINE_NAME) { + oneOffButton = node; + break; + } + } + Assert.notEqual(oneOffButton, undefined, + "One-off for test engine should exist"); + + // Open the context menu on the one-off. + promise = BrowserTestUtils.waitForEvent(contextMenu, "popupshown"); + EventUtils.synthesizeMouseAtCenter(oneOffButton, { + type: "contextmenu", + button: 2, + }); + yield promise; + + // Click the Search in New Tab menu item. + promise = BrowserTestUtils.waitForNewTab(gBrowser); + EventUtils.synthesizeMouseAtCenter(searchInNewTabMenuItem, {}); + let tab = yield promise; + + // By default the search will open in the background and the popup will stay open: + promise = promiseEvent(searchPopup, "popuphidden"); + info("Closing search panel"); + EventUtils.synthesizeKey("VK_ESCAPE", {}); + yield promise; + + // Check the loaded tab. + Assert.equal(tab.linkedBrowser.currentURI.spec, + "http://mochi.test:8888/browser/browser/components/search/test/", + "Expected search tab should have loaded"); + + yield BrowserTestUtils.removeTab(tab); + + // Move the cursor out of the panel area to avoid messing with other tests. + yield EventUtils.synthesizeNativeMouseMove(searchbar); +} + +function checkTelemetry(expectedEngineName) { + let propertyPath = [ + "countableEvents", + "__DEFAULT__", + "search-oneoff", + expectedEngineName + ".oneoff-context-searchbar", + "unknown", + "tab-background", + ]; + let telem = BrowserUITelemetry.getToolbarMeasures(); + for (let prop of propertyPath) { + Assert.ok(prop in telem, "Property " + prop + " should be in the telemetry"); + telem = telem[prop]; + } + Assert.equal(telem, 1, "Click count"); +} diff --git a/browser/components/search/test/browser_oneOffContextMenu_setDefault.js b/browser/components/search/test/browser_oneOffContextMenu_setDefault.js new file mode 100644 index 000000000..ff49cb0c6 --- /dev/null +++ b/browser/components/search/test/browser_oneOffContextMenu_setDefault.js @@ -0,0 +1,195 @@ +"use strict"; + +const TEST_ENGINE_NAME = "Foo"; +const TEST_ENGINE_BASENAME = "testEngine.xml"; +const SEARCHBAR_BASE_ID = "searchbar-engine-one-off-item-"; +const URLBAR_BASE_ID = "urlbar-engine-one-off-item-"; +const ONEOFF_URLBAR_PREF = "browser.urlbar.oneOffSearches"; + +const searchbar = document.getElementById("searchbar"); +const urlbar = document.getElementById("urlbar"); +const searchPopup = document.getElementById("PopupSearchAutoComplete"); +const urlbarPopup = document.getElementById("PopupAutoCompleteRichResult"); +const searchIcon = document.getAnonymousElementByAttribute( + searchbar, "anonid", "searchbar-search-button" +); +const searchOneOffBinding = document.getAnonymousElementByAttribute( + searchPopup, "anonid", "search-one-off-buttons" +); +const urlBarOneOffBinding = document.getAnonymousElementByAttribute( + urlbarPopup, "anonid", "one-off-search-buttons" +); + +let originalEngine = Services.search.currentEngine; + +function resetEngine() { + Services.search.currentEngine = originalEngine; +} + +registerCleanupFunction(resetEngine); + +add_task(function* init() { + yield promiseNewEngine(TEST_ENGINE_BASENAME, { + setAsCurrent: false, + }); +}); + +add_task(function* test_searchBarChangeEngine() { + let oneOffButton = yield openPopupAndGetEngineButton(true, searchPopup, + searchOneOffBinding, + SEARCHBAR_BASE_ID); + + const setDefaultEngineMenuItem = document.getAnonymousElementByAttribute( + searchOneOffBinding, "anonid", "search-one-offs-context-set-default" + ); + + // Click the set default engine menu item. + let promise = promiseCurrentEngineChanged(); + EventUtils.synthesizeMouseAtCenter(setDefaultEngineMenuItem, {}); + + // This also checks the engine correctly changed. + yield promise; + + Assert.equal(oneOffButton.id, SEARCHBAR_BASE_ID + originalEngine.name, + "Should now have the original engine's id for the button"); + Assert.equal(oneOffButton.getAttribute("tooltiptext"), originalEngine.name, + "Should now have the original engine's name for the tooltip"); + Assert.equal(oneOffButton.image, originalEngine.iconURI.spec, + "Should now have the original engine's uri for the image"); + + yield promiseClosePopup(searchPopup); +}); + +add_task(function* test_urlBarChangeEngine() { + Services.prefs.setBoolPref(ONEOFF_URLBAR_PREF, true); + registerCleanupFunction(function* () { + Services.prefs.clearUserPref(ONEOFF_URLBAR_PREF); + }); + + // Ensure the engine is reset. + resetEngine(); + + let oneOffButton = yield openPopupAndGetEngineButton(false, urlbarPopup, + urlBarOneOffBinding, + URLBAR_BASE_ID); + + const setDefaultEngineMenuItem = document.getAnonymousElementByAttribute( + urlBarOneOffBinding, "anonid", "search-one-offs-context-set-default" + ); + + // Click the set default engine menu item. + let promise = promiseCurrentEngineChanged(); + EventUtils.synthesizeMouseAtCenter(setDefaultEngineMenuItem, {}); + + // This also checks the engine correctly changed. + yield promise; + + let currentEngine = Services.search.currentEngine; + + // For the urlbar, we should keep the new engine's icon. + Assert.equal(oneOffButton.id, URLBAR_BASE_ID + currentEngine.name, + "Should now have the original engine's id for the button"); + Assert.equal(oneOffButton.getAttribute("tooltiptext"), currentEngine.name, + "Should now have the original engine's name for the tooltip"); + Assert.equal(oneOffButton.image, currentEngine.iconURI.spec, + "Should now have the original engine's uri for the image"); + + yield promiseClosePopup(urlbarPopup); +}); + +/** + * Promises that an engine change has happened for the current engine, which + * has resulted in the test engine now being the current engine. + * + * @return {Promise} Resolved once the test engine is set as the current engine. + */ +function promiseCurrentEngineChanged() { + return new Promise(resolve => { + function observer(aSub, aTopic, aData) { + if (aData == "engine-current") { + Assert.ok(Services.search.currentEngine.name, TEST_ENGINE_NAME, "currentEngine set"); + Services.obs.removeObserver(observer, "browser-search-engine-modified"); + resolve(); + } + } + + Services.obs.addObserver(observer, "browser-search-engine-modified", false); + }); +} + +/** + * Opens the specified urlbar/search popup and gets the test engine from the + * one-off buttons. + * + * @param {Boolean} isSearch true if the search popup should be opened; false + * for the urlbar popup. + * @param {Object} popup The expected popup. + * @param {Object} oneOffBinding The expected one-off-binding for the popup. + * @param {String} baseId The expected string for the id of the current + * engine button, without the engine name. + * @return {Object} Returns an object that represents the one off button for the + * test engine. + */ +function* openPopupAndGetEngineButton(isSearch, popup, oneOffBinding, baseId) { + // Open the popup. + let promise = promiseEvent(popup, "popupshown"); + info("Opening panel"); + + // We have to open the popups in differnt ways. + if (isSearch) { + // Use the search icon to avoid hitting the network. + EventUtils.synthesizeMouseAtCenter(searchIcon, {}); + } else { + // There's no history at this stage, so we need to press a key. + urlbar.focus(); + EventUtils.synthesizeKey("a", {}); + } + yield promise; + + const contextMenu = document.getAnonymousElementByAttribute( + oneOffBinding, "anonid", "search-one-offs-context-menu" + ); + const oneOffButtons = document.getAnonymousElementByAttribute( + oneOffBinding, "anonid", "search-panel-one-offs" + ); + + // Get the one-off button for the test engine. + let oneOffButton; + for (let node of oneOffButtons.childNodes) { + if (node.engine && node.engine.name == TEST_ENGINE_NAME) { + oneOffButton = node; + break; + } + } + Assert.notEqual(oneOffButton, undefined, + "One-off for test engine should exist"); + Assert.equal(oneOffButton.getAttribute("tooltiptext"), TEST_ENGINE_NAME, + "One-off should have the tooltip set to the engine name"); + Assert.equal(oneOffButton.id, baseId + TEST_ENGINE_NAME, + "Should have the correct id"); + + // Open the context menu on the one-off. + promise = BrowserTestUtils.waitForEvent(contextMenu, "popupshown"); + EventUtils.synthesizeMouseAtCenter(oneOffButton, { + type: "contextmenu", + button: 2, + }); + yield promise; + + return oneOffButton; +} + +/** + * Closes the popup and moves the mouse away from it. + * + * @param {Button} popup The popup to close. + */ +function* promiseClosePopup(popup) { + // close the panel using the escape key. + let promise = promiseEvent(popup, "popuphidden"); + EventUtils.synthesizeKey("VK_ESCAPE", {}); + yield promise; + + // Move the cursor out of the panel area to avoid messing with other tests. + yield EventUtils.synthesizeNativeMouseMove(popup); +} diff --git a/browser/components/search/test/browser_oneOffHeader.js b/browser/components/search/test/browser_oneOffHeader.js new file mode 100644 index 000000000..3a209bf56 --- /dev/null +++ b/browser/components/search/test/browser_oneOffHeader.js @@ -0,0 +1,142 @@ +/* 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/. */ +// Tests that keyboard navigation in the search panel works as designed. + +const isMac = ("nsILocalFileMac" in Ci); + +const searchbar = document.getElementById("searchbar"); +const textbox = searchbar._textbox; +const searchPopup = document.getElementById("PopupSearchAutoComplete"); +const searchIcon = document.getAnonymousElementByAttribute(searchbar, "anonid", + "searchbar-search-button"); + +const oneOffsContainer = + document.getAnonymousElementByAttribute(searchPopup, "anonid", + "search-one-off-buttons"); +const searchSettings = + document.getAnonymousElementByAttribute(oneOffsContainer, "anonid", + "search-settings"); +var header = + document.getAnonymousElementByAttribute(oneOffsContainer, "anonid", + "search-panel-one-offs-header"); +function getHeaderText() { + let headerChild = header.selectedPanel; + while (headerChild.hasChildNodes()) { + headerChild = headerChild.firstChild; + } + let headerStrings = []; + for (let label = headerChild; label; label = label.nextSibling) { + headerStrings.push(label.value); + } + return headerStrings.join(""); +} + +const msg = isMac ? 5 : 1; +const utils = window.QueryInterface(Ci.nsIInterfaceRequestor) + .getInterface(Ci.nsIDOMWindowUtils); +const scale = utils.screenPixelsPerCSSPixel; +function* synthesizeNativeMouseMove(aElement) { + let rect = aElement.getBoundingClientRect(); + let win = aElement.ownerGlobal; + let x = win.mozInnerScreenX + (rect.left + rect.right) / 2; + let y = win.mozInnerScreenY + (rect.top + rect.bottom) / 2; + + // Wait for the mouseup event to occur before continuing. + return new Promise((resolve, reject) => { + function eventOccurred(e) + { + aElement.removeEventListener("mouseover", eventOccurred, true); + resolve(); + } + + aElement.addEventListener("mouseover", eventOccurred, true); + + utils.sendNativeMouseEvent(x * scale, y * scale, msg, 0, null); + }); +} + + +add_task(function* init() { + yield promiseNewEngine("testEngine.xml"); +}); + +add_task(function* test_notext() { + let promise = promiseEvent(searchPopup, "popupshown"); + info("Opening search panel"); + EventUtils.synthesizeMouseAtCenter(searchIcon, {}); + yield promise; + + is(header.getAttribute("selectedIndex"), 0, + "Header has the correct index selected with no search terms."); + + is(getHeaderText(), "Search with:", + "Search header string is correct when no search terms have been entered"); + + yield synthesizeNativeMouseMove(searchSettings); + is(header.getAttribute("selectedIndex"), 0, + "Header has the correct index when no search terms have been entered and the Change Search Settings button is selected."); + is(getHeaderText(), "Search with:", + "Header has the correct text when no search terms have been entered and the Change Search Settings button is selected."); + + let buttons = getOneOffs(); + yield synthesizeNativeMouseMove(buttons[0]); + is(header.getAttribute("selectedIndex"), 2, + "Header has the correct index selected when a search engine has been selected"); + is(getHeaderText(), "Search " + buttons[0].engine.name, + "Is the header text correct when a search engine is selected and no terms have been entered."); + + promise = promiseEvent(searchPopup, "popuphidden"); + info("Closing search panel"); + EventUtils.synthesizeKey("VK_ESCAPE", {}); + yield promise; +}); + +add_task(function* test_text() { + textbox.value = "foo"; + registerCleanupFunction(() => { + textbox.value = ""; + }); + + let promise = promiseEvent(searchPopup, "popupshown"); + info("Opening search panel"); + SimpleTest.executeSoon(() => { + EventUtils.synthesizeMouseAtCenter(searchIcon, {}); + }); + yield promise; + + is(header.getAttribute("selectedIndex"), 1, + "Header has the correct index selected with a search term."); + is(getHeaderText(), "Search for foo with:", + "Search header string is correct when a search term has been entered"); + + let buttons = getOneOffs(); + yield synthesizeNativeMouseMove(buttons[0]); + is(header.getAttribute("selectedIndex"), 2, + "Header has the correct index selected when a search engine has been selected"); + is(getHeaderText(), "Search " + buttons[0].engine.name, + "Is the header text correct when search terms are entered after a search engine has been selected."); + + yield synthesizeNativeMouseMove(searchSettings); + is(header.getAttribute("selectedIndex"), 1, + "Header has the correct index selected when search terms have been entered and the Change Search Settings button is selected."); + is(getHeaderText(), "Search for foo with:", + "Header has the correct text when search terms have been entered and the Change Search Settings button is selected."); + + // Click the "Foo Search" header at the top of the popup and make sure it + // loads the search results. + let searchbarEngine = + document.getAnonymousElementByAttribute(searchPopup, "anonid", + "searchbar-engine"); + + yield synthesizeNativeMouseMove(searchbarEngine); + SimpleTest.executeSoon(() => { + EventUtils.synthesizeMouseAtCenter(searchbarEngine, {}); + }); + + let url = Services.search.currentEngine.getSubmission(textbox.value).uri.spec; + yield promiseTabLoadEvent(gBrowser.selectedTab, url); + + // Move the cursor out of the panel area to avoid messing with other tests. + yield synthesizeNativeMouseMove(searchbar); +}); diff --git a/browser/components/search/test/browser_private_search_perwindowpb.js b/browser/components/search/test/browser_private_search_perwindowpb.js new file mode 100644 index 000000000..c0410371b --- /dev/null +++ b/browser/components/search/test/browser_private_search_perwindowpb.js @@ -0,0 +1,76 @@ +// This test performs a search in a public window, then a different +// search in a private window, and then checks in the public window +// whether there is an autocomplete entry for the private search. + +add_task(function* () { + // Don't use about:home as the homepage for new windows + Services.prefs.setIntPref("browser.startup.page", 0); + registerCleanupFunction(() => Services.prefs.clearUserPref("browser.startup.page")); + + let windowsToClose = []; + + function performSearch(aWin, aIsPrivate) { + let searchBar = aWin.BrowserSearch.searchBar; + ok(searchBar, "got search bar"); + + let loadPromise = BrowserTestUtils.browserLoaded(aWin.gBrowser.selectedBrowser); + + searchBar.value = aIsPrivate ? "private test" : "public test"; + searchBar.focus(); + EventUtils.synthesizeKey("VK_RETURN", {}, aWin); + + return loadPromise; + } + + function* testOnWindow(aIsPrivate) { + let win = yield BrowserTestUtils.openNewBrowserWindow({ private: aIsPrivate }); + yield SimpleTest.promiseFocus(win); + windowsToClose.push(win); + return win; + } + + yield promiseNewEngine("426329.xml", { iconURL: "data:image/x-icon,%00" }); + + let newWindow = yield* testOnWindow(false); + yield performSearch(newWindow, false); + + newWindow = yield* testOnWindow(true); + yield performSearch(newWindow, true); + + newWindow = yield* testOnWindow(false); + + let searchBar = newWindow.BrowserSearch.searchBar; + searchBar.value = "p"; + searchBar.focus(); + + let popup = searchBar.textbox.popup; + let popupPromise = BrowserTestUtils.waitForEvent(popup, "popupshown"); + searchBar.textbox.showHistoryPopup(); + yield popupPromise; + + let entries = getMenuEntries(searchBar); + for (let i = 0; i < entries.length; i++) { + isnot(entries[i], "private test", + "shouldn't see private autocomplete entries"); + } + + searchBar.textbox.toggleHistoryPopup(); + searchBar.value = ""; + + windowsToClose.forEach(function(win) { + win.close(); + }); +}); + +function getMenuEntries(searchBar) { + let entries = []; + let 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? + let column = autocompleteMenu.tree.columns[0]; + let numRows = autocompleteMenu.tree.view.rowCount; + for (let i = 0; i < numRows; i++) { + entries.push(autocompleteMenu.tree.view.getValueAt(i, column)); + } + return entries; +} diff --git a/browser/components/search/test/browser_searchbar_keyboard_navigation.js b/browser/components/search/test/browser_searchbar_keyboard_navigation.js new file mode 100644 index 000000000..d395dfdc2 --- /dev/null +++ b/browser/components/search/test/browser_searchbar_keyboard_navigation.js @@ -0,0 +1,425 @@ +// Tests that keyboard navigation in the search panel works as designed. + +const searchbar = document.getElementById("searchbar"); +const textbox = searchbar._textbox; +const searchPopup = document.getElementById("PopupSearchAutoComplete"); +const oneOffsContainer = + document.getAnonymousElementByAttribute(searchPopup, "anonid", + "search-one-off-buttons"); + +const kValues = ["foo1", "foo2", "foo3"]; +const kUserValue = "foo"; + +function getOpenSearchItems() { + let os = []; + + let addEngineList = + document.getAnonymousElementByAttribute(oneOffsContainer, "anonid", + "add-engines"); + for (let item = addEngineList.firstChild; item; item = item.nextSibling) + os.push(item); + + return os; +} + +add_task(function* init() { + yield promiseNewEngine("testEngine.xml"); + + // First cleanup the form history in case other tests left things there. + yield new Promise((resolve, reject) => { + info("cleanup the search history"); + searchbar.FormHistory.update({op: "remove", fieldname: "searchbar-history"}, + {handleCompletion: resolve, + handleError: reject}); + }); + + yield new Promise((resolve, reject) => { + info("adding search history values: " + kValues); + let ops = kValues.map(value => { return {op: "add", + fieldname: "searchbar-history", + value: value} + }); + searchbar.FormHistory.update(ops, { + handleCompletion: function() { + registerCleanupFunction(() => { + info("removing search history values: " + kValues); + let ops = + kValues.map(value => { return {op: "remove", + fieldname: "searchbar-history", + value: value} + }); + searchbar.FormHistory.update(ops); + }); + resolve(); + }, + handleError: reject + }); + }); + + textbox.value = kUserValue; + registerCleanupFunction(() => { textbox.value = ""; }); +}); + + +add_task(function* test_arrows() { + let promise = promiseEvent(searchPopup, "popupshown"); + info("Opening search panel"); + searchbar.focus(); + yield promise; + is(textbox.mController.searchString, kUserValue, "The search string should be 'foo'"); + + // Check the initial state of the panel before sending keyboard events. + is(searchPopup.view.rowCount, kValues.length, "There should be 3 suggestions"); + is(searchPopup.selectedIndex, -1, "no suggestion should be selected"); + + // The tests will be less meaningful if the first, second, last, and + // before-last one-off buttons aren't different. We should always have more + // than 4 default engines, but it's safer to check this assumption. + let oneOffs = getOneOffs(); + ok(oneOffs.length >= 4, "we have at least 4 one-off buttons displayed") + + ok(!textbox.selectedButton, "no one-off button should be selected"); + + // The down arrow should first go through the suggestions. + for (let i = 0; i < kValues.length; ++i) { + EventUtils.synthesizeKey("VK_DOWN", {}); + is(searchPopup.selectedIndex, i, + "the suggestion at index " + i + " should be selected"); + is(textbox.value, kValues[i], + "the textfield value should be " + kValues[i]); + } + + // Pressing down again should remove suggestion selection and change the text + // field value back to what the user typed, and select the first one-off. + EventUtils.synthesizeKey("VK_DOWN", {}); + is(searchPopup.selectedIndex, -1, "no suggestion should be selected"); + is(textbox.value, kUserValue, + "the textfield value should be back to initial value"); + + // now cycle through the one-off items, the first one is already selected. + for (let i = 0; i < oneOffs.length; ++i) { + is(textbox.selectedButton, oneOffs[i], + "the one-off button #" + (i + 1) + " should be selected"); + EventUtils.synthesizeKey("VK_DOWN", {}); + } + + is(textbox.selectedButton.getAttribute("anonid"), "search-settings", + "the settings item should be selected"); + EventUtils.synthesizeKey("VK_DOWN", {}); + + // We should now be back to the initial situation. + is(searchPopup.selectedIndex, -1, "no suggestion should be selected"); + ok(!textbox.selectedButton, "no one-off button should be selected"); + + info("now test the up arrow key"); + EventUtils.synthesizeKey("VK_UP", {}); + is(textbox.selectedButton.getAttribute("anonid"), "search-settings", + "the settings item should be selected"); + + // cycle through the one-off items, the first one is already selected. + for (let i = oneOffs.length; i; --i) { + EventUtils.synthesizeKey("VK_UP", {}); + is(textbox.selectedButton, oneOffs[i - 1], + "the one-off button #" + i + " should be selected"); + } + + // Another press on up should clear the one-off selection and select the + // last suggestion. + EventUtils.synthesizeKey("VK_UP", {}); + ok(!textbox.selectedButton, "no one-off button should be selected"); + + for (let i = kValues.length - 1; i >= 0; --i) { + is(searchPopup.selectedIndex, i, + "the suggestion at index " + i + " should be selected"); + is(textbox.value, kValues[i], + "the textfield value should be " + kValues[i]); + EventUtils.synthesizeKey("VK_UP", {}); + } + + is(searchPopup.selectedIndex, -1, "no suggestion should be selected"); + is(textbox.value, kUserValue, + "the textfield value should be back to initial value"); +}); + +add_task(function* test_typing_clears_button_selection() { + is(Services.focus.focusedElement, textbox.inputField, + "the search bar should be focused"); // from the previous test. + ok(!textbox.selectedButton, "no button should be selected"); + + EventUtils.synthesizeKey("VK_UP", {}); + is(textbox.selectedButton.getAttribute("anonid"), "search-settings", + "the settings item should be selected"); + + // Type a character. + EventUtils.synthesizeKey("a", {}); + ok(!textbox.selectedButton, "the settings item should be de-selected"); + + // Remove the character. + EventUtils.synthesizeKey("VK_BACK_SPACE", {}); +}); + +add_task(function* test_tab() { + is(Services.focus.focusedElement, textbox.inputField, + "the search bar should be focused"); // from the previous test. + + let oneOffs = getOneOffs(); + ok(!textbox.selectedButton, "no one-off button should be selected"); + + // Pressing tab should select the first one-off without selecting suggestions. + // now cycle through the one-off items, the first one is already selected. + for (let i = 0; i < oneOffs.length; ++i) { + EventUtils.synthesizeKey("VK_TAB", {}); + is(textbox.selectedButton, oneOffs[i], + "the one-off button #" + (i + 1) + " should be selected"); + } + is(searchPopup.selectedIndex, -1, "no suggestion should be selected"); + is(textbox.value, kUserValue, "the textfield value should be unmodified"); + + // One more <tab> selects the settings button. + EventUtils.synthesizeKey("VK_TAB", {}); + is(textbox.selectedButton.getAttribute("anonid"), "search-settings", + "the settings item should be selected"); + + // Pressing tab again should close the panel... + let promise = promiseEvent(searchPopup, "popuphidden"); + EventUtils.synthesizeKey("VK_TAB", {}); + yield promise; + + // ... and move the focus out of the searchbox. + isnot(Services.focus.focusedElement, textbox.inputField, + "the search bar no longer be focused"); +}); + +add_task(function* test_shift_tab() { + // First reopen the panel. + let promise = promiseEvent(searchPopup, "popupshown"); + info("Opening search panel"); + searchbar.focus(); + yield promise; + + let oneOffs = getOneOffs(); + ok(!textbox.selectedButton, "no one-off button should be selected"); + + // Press up once to select the last button. + EventUtils.synthesizeKey("VK_UP", {}); + is(textbox.selectedButton.getAttribute("anonid"), "search-settings", + "the settings item should be selected"); + + // Press up again to select the last one-off button. + EventUtils.synthesizeKey("VK_UP", {}); + + // Pressing shift+tab should cycle through the one-off items. + for (let i = oneOffs.length - 1; i >= 0; --i) { + is(textbox.selectedButton, oneOffs[i], + "the one-off button #" + (i + 1) + " should be selected"); + if (i) + EventUtils.synthesizeKey("VK_TAB", {shiftKey: true}); + } + is(searchPopup.selectedIndex, -1, "no suggestion should be selected"); + is(textbox.value, kUserValue, "the textfield value should be unmodified"); + + // Pressing shift+tab again should close the panel... + promise = promiseEvent(searchPopup, "popuphidden"); + EventUtils.synthesizeKey("VK_TAB", {shiftKey: true}); + yield promise; + + // ... and move the focus out of the searchbox. + isnot(Services.focus.focusedElement, textbox.inputField, + "the search bar no longer be focused"); +}); + +add_task(function* test_alt_down() { + // First refocus the panel. + let promise = promiseEvent(searchPopup, "popupshown"); + info("Opening search panel"); + searchbar.focus(); + yield promise; + + // close the panel using the escape key. + promise = promiseEvent(searchPopup, "popuphidden"); + EventUtils.synthesizeKey("VK_ESCAPE", {}); + yield promise; + + // check that alt+down opens the panel... + promise = promiseEvent(searchPopup, "popupshown"); + EventUtils.synthesizeKey("VK_DOWN", {altKey: true}); + yield promise; + + // ... and does nothing else. + ok(!textbox.selectedButton, "no one-off button should be selected"); + is(searchPopup.selectedIndex, -1, "no suggestion should be selected"); + is(textbox.value, kUserValue, "the textfield value should be unmodified"); + + // Pressing alt+down should select the first one-off without selecting suggestions + // and cycle through the one-off items. + let oneOffs = getOneOffs(); + for (let i = 0; i < oneOffs.length; ++i) { + EventUtils.synthesizeKey("VK_DOWN", {altKey: true}); + is(textbox.selectedButton, oneOffs[i], + "the one-off button #" + (i + 1) + " should be selected"); + is(searchPopup.selectedIndex, -1, "no suggestion should be selected"); + } + + // One more alt+down keypress and nothing should be selected. + EventUtils.synthesizeKey("VK_DOWN", {altKey: true}); + ok(!textbox.selectedButton, "no one-off button should be selected"); + + // another one and the first one-off should be selected. + EventUtils.synthesizeKey("VK_DOWN", {altKey: true}); + is(textbox.selectedButton, oneOffs[0], + "the first one-off button should be selected"); +}); + +add_task(function* test_alt_up() { + // close the panel using the escape key. + let promise = promiseEvent(searchPopup, "popuphidden"); + EventUtils.synthesizeKey("VK_ESCAPE", {}); + yield promise; + + // check that alt+up opens the panel... + promise = promiseEvent(searchPopup, "popupshown"); + EventUtils.synthesizeKey("VK_UP", {altKey: true}); + yield promise; + + // ... and does nothing else. + ok(!textbox.selectedButton, "no one-off button should be selected"); + is(searchPopup.selectedIndex, -1, "no suggestion should be selected"); + is(textbox.value, kUserValue, "the textfield value should be unmodified"); + + // Pressing alt+up should select the last one-off without selecting suggestions + // and cycle up through the one-off items. + let oneOffs = getOneOffs(); + for (let i = oneOffs.length - 1; i >= 0; --i) { + EventUtils.synthesizeKey("VK_UP", {altKey: true}); + is(textbox.selectedButton, oneOffs[i], + "the one-off button #" + (i + 1) + " should be selected"); + is(searchPopup.selectedIndex, -1, "no suggestion should be selected"); + } + + // One more alt+down keypress and nothing should be selected. + EventUtils.synthesizeKey("VK_UP", {altKey: true}); + ok(!textbox.selectedButton, "no one-off button should be selected"); + + // another one and the last one-off should be selected. + EventUtils.synthesizeKey("VK_UP", {altKey: true}); + is(textbox.selectedButton, oneOffs[oneOffs.length - 1], + "the last one-off button should be selected"); + + // Cleanup for the next test. + EventUtils.synthesizeKey("VK_DOWN", {}); + is(textbox.selectedButton.getAttribute("anonid"), "search-settings", + "the settings item should be selected"); + EventUtils.synthesizeKey("VK_DOWN", {}); + ok(!textbox.selectedButton, "no one-off should be selected anymore"); +}); + +add_task(function* test_tab_and_arrows() { + // Check the initial state is as expected. + ok(!textbox.selectedButton, "no one-off button should be selected"); + is(searchPopup.selectedIndex, -1, "no suggestion should be selected"); + is(textbox.value, kUserValue, "the textfield value should be unmodified"); + + // After pressing down, the first sugggestion should be selected. + EventUtils.synthesizeKey("VK_DOWN", {}); + is(searchPopup.selectedIndex, 0, "first suggestion should be selected"); + is(textbox.value, kValues[0], "the textfield value should have changed"); + ok(!textbox.selectedButton, "no one-off button should be selected"); + + // After pressing tab, the first one-off should be selected, + // and the first suggestion still selected. + let oneOffs = getOneOffs(); + EventUtils.synthesizeKey("VK_TAB", {}); + is(textbox.selectedButton, oneOffs[0], + "the first one-off button should be selected"); + is(searchPopup.selectedIndex, 0, "first suggestion should still be selected"); + + // After pressing down, the second suggestion should be selected, + // and the first one-off still selected. + EventUtils.synthesizeKey("VK_DOWN", {}); + is(textbox.selectedButton, oneOffs[0], + "the first one-off button should still be selected"); + is(searchPopup.selectedIndex, 1, "second suggestion should be selected"); + + // After pressing up, the first suggestion should be selected again, + // and the first one-off still selected. + EventUtils.synthesizeKey("VK_UP", {}); + is(textbox.selectedButton, oneOffs[0], + "the first one-off button should still be selected"); + is(searchPopup.selectedIndex, 0, "second suggestion should be selected again"); + + // After pressing up again, we should have no suggestion selected anymore, + // the textfield value back to the user-typed value, and still the first one-off + // selected. + EventUtils.synthesizeKey("VK_UP", {}); + is(searchPopup.selectedIndex, -1, "no suggestion should be selected"); + is(textbox.value, kUserValue, + "the textfield value should be back to user typed value"); + is(textbox.selectedButton, oneOffs[0], + "the first one-off button should still be selected"); + + // Now pressing down should select the second one-off. + EventUtils.synthesizeKey("VK_DOWN", {}); + is(textbox.selectedButton, oneOffs[1], + "the second one-off button should be selected"); + is(searchPopup.selectedIndex, -1, "there should still be no selected suggestion"); + + // Finally close the panel. + let promise = promiseEvent(searchPopup, "popuphidden"); + searchPopup.hidePopup(); + yield promise; +}); + +add_task(function* test_open_search() { + let rootDir = getRootDirectory(gTestPath); + yield BrowserTestUtils.openNewForegroundTab(gBrowser, rootDir + "opensearch.html"); + + let promise = promiseEvent(searchPopup, "popupshown"); + info("Opening search panel"); + searchbar.focus(); + yield promise; + + let engines = getOpenSearchItems(); + is(engines.length, 2, "the opensearch.html page exposes 2 engines") + + // Check that there's initially no selection. + is(searchPopup.selectedIndex, -1, "no suggestion should be selected"); + ok(!textbox.selectedButton, "no button should be selected"); + + // Pressing up once selects the setting button... + EventUtils.synthesizeKey("VK_UP", {}); + is(textbox.selectedButton.getAttribute("anonid"), "search-settings", + "the settings item should be selected"); + + // ...and then pressing up selects open search engines. + for (let i = engines.length; i; --i) { + EventUtils.synthesizeKey("VK_UP", {}); + let selectedButton = textbox.selectedButton; + is(selectedButton, engines[i - 1], + "the engine #" + i + " should be selected"); + ok(selectedButton.classList.contains("addengine-item"), + "the button is themed as an engine item"); + } + + // Pressing up again should select the last one-off button. + EventUtils.synthesizeKey("VK_UP", {}); + is(textbox.selectedButton, getOneOffs().pop(), + "the last one-off button should be selected"); + + info("now check that the down key navigates open search items as expected"); + for (let i = 0; i < engines.length; ++i) { + EventUtils.synthesizeKey("VK_DOWN", {}); + is(textbox.selectedButton, engines[i], + "the engine #" + (i + 1) + " should be selected"); + } + + // Pressing down on the last engine item selects the settings button. + EventUtils.synthesizeKey("VK_DOWN", {}); + is(textbox.selectedButton.getAttribute("anonid"), "search-settings", + "the settings item should be selected"); + + promise = promiseEvent(searchPopup, "popuphidden"); + searchPopup.hidePopup(); + yield promise; + + gBrowser.removeCurrentTab(); +}); diff --git a/browser/components/search/test/browser_searchbar_openpopup.js b/browser/components/search/test/browser_searchbar_openpopup.js new file mode 100644 index 000000000..befc8f142 --- /dev/null +++ b/browser/components/search/test/browser_searchbar_openpopup.js @@ -0,0 +1,521 @@ +// Tests that the suggestion popup appears at the right times in response to +// focus and user events (mouse, keyboard, drop). + +// Instead of loading EventUtils.js into the test scope in browser-test.js for all tests, +// we only need EventUtils.js for a few files which is why we are using loadSubScript. +var EventUtils = {}; +this._scriptLoader = Cc["@mozilla.org/moz/jssubscript-loader;1"]. + getService(Ci.mozIJSSubScriptLoader); +this._scriptLoader.loadSubScript("chrome://mochikit/content/tests/SimpleTest/EventUtils.js", EventUtils); + +const searchbar = document.getElementById("searchbar"); +const searchIcon = document.getAnonymousElementByAttribute(searchbar, "anonid", "searchbar-search-button"); +const goButton = document.getAnonymousElementByAttribute(searchbar, "anonid", "search-go-button"); +const textbox = searchbar._textbox; +const searchPopup = document.getElementById("PopupSearchAutoComplete"); +const kValues = ["long text", "long text 2", "long text 3"]; + +const isWindows = Services.appinfo.OS == "WINNT"; +const mouseDown = isWindows ? 2 : 1; +const mouseUp = isWindows ? 4 : 2; +const utils = window.QueryInterface(Ci.nsIInterfaceRequestor) + .getInterface(Ci.nsIDOMWindowUtils); +const scale = utils.screenPixelsPerCSSPixel; + +function* synthesizeNativeMouseClick(aElement) { + let rect = aElement.getBoundingClientRect(); + let win = aElement.ownerGlobal; + let x = win.mozInnerScreenX + (rect.left + rect.right) / 2; + let y = win.mozInnerScreenY + (rect.top + rect.bottom) / 2; + + // Wait for the mouseup event to occur before continuing. + return new Promise((resolve, reject) => { + function eventOccurred(e) + { + aElement.removeEventListener("mouseup", eventOccurred, true); + resolve(); + } + + aElement.addEventListener("mouseup", eventOccurred, true); + + utils.sendNativeMouseEvent(x * scale, y * scale, mouseDown, 0, null); + utils.sendNativeMouseEvent(x * scale, y * scale, mouseUp, 0, null); + }); +} + +add_task(function* init() { + yield promiseNewEngine("testEngine.xml"); + + // First cleanup the form history in case other tests left things there. + yield new Promise((resolve, reject) => { + info("cleanup the search history"); + searchbar.FormHistory.update({op: "remove", fieldname: "searchbar-history"}, + {handleCompletion: resolve, + handleError: reject}); + }); + + yield new Promise((resolve, reject) => { + info("adding search history values: " + kValues); + let ops = kValues.map(value => { return {op: "add", + fieldname: "searchbar-history", + value: value} + }); + searchbar.FormHistory.update(ops, { + handleCompletion: function() { + registerCleanupFunction(() => { + info("removing search history values: " + kValues); + let ops = + kValues.map(value => { return {op: "remove", + fieldname: "searchbar-history", + value: value} + }); + searchbar.FormHistory.update(ops); + }); + resolve(); + }, + handleError: reject + }); + }); +}); + +// Adds a task that shouldn't show the search suggestions popup. +function add_no_popup_task(task) { + add_task(function*() { + let sawPopup = false; + function listener() { + sawPopup = true; + } + + info("Entering test " + task.name); + searchPopup.addEventListener("popupshowing", listener, false); + yield Task.spawn(task); + searchPopup.removeEventListener("popupshowing", listener, false); + ok(!sawPopup, "Shouldn't have seen the suggestions popup"); + info("Leaving test " + task.name); + }); +} + +// Simulates the full set of events for a context click +function context_click(target) { + for (let event of ["mousedown", "contextmenu", "mouseup"]) + EventUtils.synthesizeMouseAtCenter(target, { type: event, button: 2 }); +} + +// Right clicking the icon should not open the popup. +add_no_popup_task(function* open_icon_context() { + gURLBar.focus(); + let toolbarPopup = document.getElementById("toolbar-context-menu"); + + let promise = promiseEvent(toolbarPopup, "popupshown"); + context_click(searchIcon); + yield promise; + + promise = promiseEvent(toolbarPopup, "popuphidden"); + toolbarPopup.hidePopup(); + yield promise; +}); + +// With no text in the search box left clicking the icon should open the popup. +// Clicking the icon again should hide the popup and not show it again. +add_task(function* open_empty() { + gURLBar.focus(); + + let promise = promiseEvent(searchPopup, "popupshown"); + info("Clicking icon"); + EventUtils.synthesizeMouseAtCenter(searchIcon, {}); + yield promise; + is(searchPopup.getAttribute("showonlysettings"), "true", "Should only show the settings"); + is(textbox.mController.searchString, "", "Should be an empty search string"); + + // By giving the textbox some text any next attempt to open the search popup + // from the click handler will try to search for this text. + textbox.value = "foo"; + + promise = promiseEvent(searchPopup, "popuphidden"); + + info("Hiding popup"); + yield synthesizeNativeMouseClick(searchIcon); + yield promise; + + is(textbox.mController.searchString, "", "Should not have started to search for the new text"); + + // Cancel the search if it started. + if (textbox.mController.searchString != "") { + textbox.mController.stopSearch(); + } + + textbox.value = ""; +}); + +// With no text in the search box left clicking it should not open the popup. +add_no_popup_task(function* click_doesnt_open_popup() { + gURLBar.focus(); + + EventUtils.synthesizeMouseAtCenter(textbox, {}); + is(Services.focus.focusedElement, textbox.inputField, "Should have focused the search bar"); + is(textbox.selectionStart, 0, "Should have selected all of the text"); + is(textbox.selectionEnd, 0, "Should have selected all of the text"); +}); + +// Left clicking in a non-empty search box when unfocused should focus it and open the popup. +add_task(function* click_opens_popup() { + gURLBar.focus(); + textbox.value = "foo"; + + let promise = promiseEvent(searchPopup, "popupshown"); + EventUtils.synthesizeMouseAtCenter(textbox, {}); + yield promise; + isnot(searchPopup.getAttribute("showonlysettings"), "true", "Should show the full popup"); + + is(Services.focus.focusedElement, textbox.inputField, "Should have focused the search bar"); + is(textbox.selectionStart, 0, "Should have selected all of the text"); + is(textbox.selectionEnd, 3, "Should have selected all of the text"); + + promise = promiseEvent(searchPopup, "popuphidden"); + searchPopup.hidePopup(); + yield promise; + + textbox.value = ""; +}); + +// Right clicking in a non-empty search box when unfocused should open the edit context menu. +add_no_popup_task(function* right_click_doesnt_open_popup() { + gURLBar.focus(); + textbox.value = "foo"; + + let contextPopup = document.getAnonymousElementByAttribute(textbox.inputField.parentNode, "anonid", "input-box-contextmenu"); + let promise = promiseEvent(contextPopup, "popupshown"); + context_click(textbox); + yield promise; + + is(Services.focus.focusedElement, textbox.inputField, "Should have focused the search bar"); + is(textbox.selectionStart, 0, "Should have selected all of the text"); + is(textbox.selectionEnd, 3, "Should have selected all of the text"); + + promise = promiseEvent(contextPopup, "popuphidden"); + contextPopup.hidePopup(); + yield promise; + + textbox.value = ""; +}); + +// Moving focus away from the search box should close the popup +add_task(function* focus_change_closes_popup() { + gURLBar.focus(); + textbox.value = "foo"; + + let promise = promiseEvent(searchPopup, "popupshown"); + EventUtils.synthesizeMouseAtCenter(textbox, {}); + yield promise; + isnot(searchPopup.getAttribute("showonlysettings"), "true", "Should show the full popup"); + + is(Services.focus.focusedElement, textbox.inputField, "Should have focused the search bar"); + is(textbox.selectionStart, 0, "Should have selected all of the text"); + is(textbox.selectionEnd, 3, "Should have selected all of the text"); + + promise = promiseEvent(searchPopup, "popuphidden"); + let promise2 = promiseEvent(searchbar, "blur"); + EventUtils.synthesizeKey("VK_TAB", { shiftKey: true }); + yield promise; + yield promise2; + + textbox.value = ""; +}); + +// Moving focus away from the search box should close the small popup +add_task(function* focus_change_closes_small_popup() { + gURLBar.focus(); + + let promise = promiseEvent(searchPopup, "popupshown"); + // For some reason sending the mouse event immediately doesn't open the popup. + SimpleTest.executeSoon(() => { + EventUtils.synthesizeMouseAtCenter(searchIcon, {}); + }); + yield promise; + is(searchPopup.getAttribute("showonlysettings"), "true", "Should show the small popup"); + + is(Services.focus.focusedElement, textbox.inputField, "Should have focused the search bar"); + + promise = promiseEvent(searchPopup, "popuphidden"); + let promise2 = promiseEvent(searchbar, "blur"); + EventUtils.synthesizeKey("VK_TAB", { shiftKey: true }); + yield promise; + yield promise2; +}); + +// Pressing escape should close the popup. +add_task(function* escape_closes_popup() { + gURLBar.focus(); + textbox.value = "foo"; + + let promise = promiseEvent(searchPopup, "popupshown"); + EventUtils.synthesizeMouseAtCenter(textbox, {}); + yield promise; + isnot(searchPopup.getAttribute("showonlysettings"), "true", "Should show the full popup"); + + is(Services.focus.focusedElement, textbox.inputField, "Should have focused the search bar"); + is(textbox.selectionStart, 0, "Should have selected all of the text"); + is(textbox.selectionEnd, 3, "Should have selected all of the text"); + + promise = promiseEvent(searchPopup, "popuphidden"); + EventUtils.synthesizeKey("VK_ESCAPE", {}); + yield promise; + + textbox.value = ""; +}); + +// Pressing contextmenu should close the popup. +add_task(function* contextmenu_closes_popup() { + gURLBar.focus(); + textbox.value = "foo"; + + let promise = promiseEvent(searchPopup, "popupshown"); + EventUtils.synthesizeMouseAtCenter(textbox, {}); + yield promise; + isnot(searchPopup.getAttribute("showonlysettings"), "true", "Should show the full popup"); + + is(Services.focus.focusedElement, textbox.inputField, "Should have focused the search bar"); + is(textbox.selectionStart, 0, "Should have selected all of the text"); + is(textbox.selectionEnd, 3, "Should have selected all of the text"); + + promise = promiseEvent(searchPopup, "popuphidden"); + + // synthesizeKey does not work with VK_CONTEXT_MENU (bug 1127368) + EventUtils.synthesizeMouseAtCenter(textbox, { type: "contextmenu", button: null }); + + yield promise; + + let contextPopup = + document.getAnonymousElementByAttribute(textbox.inputField.parentNode, + "anonid", "input-box-contextmenu"); + promise = promiseEvent(contextPopup, "popuphidden"); + contextPopup.hidePopup(); + yield promise; + + textbox.value = ""; +}); + +// Tabbing to the search box should open the popup if it contains text. +add_task(function* tab_opens_popup() { + gURLBar.focus(); + textbox.value = "foo"; + + let promise = promiseEvent(searchPopup, "popupshown"); + EventUtils.synthesizeKey("VK_TAB", {}); + yield promise; + isnot(searchPopup.getAttribute("showonlysettings"), "true", "Should show the full popup"); + + is(Services.focus.focusedElement, textbox.inputField, "Should have focused the search bar"); + is(textbox.selectionStart, 0, "Should have selected all of the text"); + is(textbox.selectionEnd, 3, "Should have selected all of the text"); + + promise = promiseEvent(searchPopup, "popuphidden"); + searchPopup.hidePopup(); + yield promise; + + textbox.value = ""; +}); + +// Tabbing to the search box should not open the popup if it doesn't contain text. +add_no_popup_task(function* tab_doesnt_open_popup() { + gURLBar.focus(); + textbox.value = "foo"; + + EventUtils.synthesizeKey("VK_TAB", {}); + + is(Services.focus.focusedElement, textbox.inputField, "Should have focused the search bar"); + is(textbox.selectionStart, 0, "Should have selected all of the text"); + is(textbox.selectionEnd, 3, "Should have selected all of the text"); + + textbox.value = ""; +}); + +// Switching back to the window when the search box has focus from mouse should not open the popup. +add_task(function* refocus_window_doesnt_open_popup_mouse() { + gURLBar.focus(); + textbox.value = "foo"; + + let promise = promiseEvent(searchPopup, "popupshown"); + EventUtils.synthesizeMouseAtCenter(searchbar, {}); + yield promise; + isnot(searchPopup.getAttribute("showonlysettings"), "true", "Should show the full popup"); + + is(Services.focus.focusedElement, textbox.inputField, "Should have focused the search bar"); + is(textbox.selectionStart, 0, "Should have selected all of the text"); + is(textbox.selectionEnd, 3, "Should have selected all of the text"); + + promise = promiseEvent(searchPopup, "popuphidden"); + let newWin = OpenBrowserWindow(); + yield new Promise(resolve => waitForFocus(resolve, newWin)); + yield promise; + + function listener() { + ok(false, "Should not have shown the popup."); + } + searchPopup.addEventListener("popupshowing", listener, false); + + promise = promiseEvent(searchbar, "focus"); + newWin.close(); + yield promise; + + // Wait a few ticks to allow any focus handlers to show the popup if they are going to. + yield new Promise(resolve => executeSoon(resolve)); + yield new Promise(resolve => executeSoon(resolve)); + yield new Promise(resolve => executeSoon(resolve)); + + searchPopup.removeEventListener("popupshowing", listener, false); + textbox.value = ""; +}); + +// Switching back to the window when the search box has focus from keyboard should not open the popup. +add_task(function* refocus_window_doesnt_open_popup_keyboard() { + gURLBar.focus(); + textbox.value = "foo"; + + let promise = promiseEvent(searchPopup, "popupshown"); + EventUtils.synthesizeKey("VK_TAB", {}); + yield promise; + isnot(searchPopup.getAttribute("showonlysettings"), "true", "Should show the full popup"); + + is(Services.focus.focusedElement, textbox.inputField, "Should have focused the search bar"); + is(textbox.selectionStart, 0, "Should have selected all of the text"); + is(textbox.selectionEnd, 3, "Should have selected all of the text"); + + promise = promiseEvent(searchPopup, "popuphidden"); + let newWin = OpenBrowserWindow(); + yield new Promise(resolve => waitForFocus(resolve, newWin)); + yield promise; + + function listener() { + ok(false, "Should not have shown the popup."); + } + searchPopup.addEventListener("popupshowing", listener, false); + + promise = promiseEvent(searchbar, "focus"); + newWin.close(); + yield promise; + + // Wait a few ticks to allow any focus handlers to show the popup if they are going to. + yield new Promise(resolve => executeSoon(resolve)); + yield new Promise(resolve => executeSoon(resolve)); + yield new Promise(resolve => executeSoon(resolve)); + + searchPopup.removeEventListener("popupshowing", listener, false); + textbox.value = ""; +}); + +// Clicking the search go button shouldn't open the popup +add_no_popup_task(function* search_go_doesnt_open_popup() { + gBrowser.selectedTab = gBrowser.addTab(); + + gURLBar.focus(); + textbox.value = "foo"; + searchbar.updateGoButtonVisibility(); + + let promise = BrowserTestUtils.browserLoaded(gBrowser.selectedBrowser); + EventUtils.synthesizeMouseAtCenter(goButton, {}); + yield promise; + + textbox.value = ""; + gBrowser.removeCurrentTab(); +}); + +// Clicks outside the search popup should close the popup but not consume the click. +add_task(function* dont_consume_clicks() { + gURLBar.focus(); + textbox.value = "foo"; + + let promise = promiseEvent(searchPopup, "popupshown"); + EventUtils.synthesizeMouseAtCenter(textbox, {}); + yield promise; + isnot(searchPopup.getAttribute("showonlysettings"), "true", "Should show the full popup"); + + is(Services.focus.focusedElement, textbox.inputField, "Should have focused the search bar"); + is(textbox.selectionStart, 0, "Should have selected all of the text"); + is(textbox.selectionEnd, 3, "Should have selected all of the text"); + + promise = promiseEvent(searchPopup, "popuphidden"); + yield synthesizeNativeMouseClick(gURLBar); + yield promise; + + is(Services.focus.focusedElement, gURLBar.inputField, "Should have focused the URL bar"); + + textbox.value = ""; +}); + +// Dropping text to the searchbar should open the popup +add_task(function* drop_opens_popup() { + let promise = promiseEvent(searchPopup, "popupshown"); + EventUtils.synthesizeDrop(searchIcon, textbox.inputField, [[ {type: "text/plain", data: "foo" } ]], "move", window); + yield promise; + + isnot(searchPopup.getAttribute("showonlysettings"), "true", "Should show the full popup"); + is(Services.focus.focusedElement, textbox.inputField, "Should have focused the search bar"); + promise = promiseEvent(searchPopup, "popuphidden"); + searchPopup.hidePopup(); + yield promise; + + textbox.value = ""; +}); + +// Moving the caret using the cursor keys should not close the popup. +add_task(function* dont_rollup_oncaretmove() { + gURLBar.focus(); + textbox.value = "long text"; + + let promise = promiseEvent(searchPopup, "popupshown"); + EventUtils.synthesizeMouseAtCenter(textbox, {}); + yield promise; + + // Deselect the text + EventUtils.synthesizeKey("VK_RIGHT", {}); + is(textbox.selectionStart, 9, "Should have moved the caret (selectionStart after deselect right)"); + is(textbox.selectionEnd, 9, "Should have moved the caret (selectionEnd after deselect right)"); + is(searchPopup.state, "open", "Popup should still be open"); + + EventUtils.synthesizeKey("VK_LEFT", {}); + is(textbox.selectionStart, 8, "Should have moved the caret (selectionStart after left)"); + is(textbox.selectionEnd, 8, "Should have moved the caret (selectionEnd after left)"); + is(searchPopup.state, "open", "Popup should still be open"); + + EventUtils.synthesizeKey("VK_RIGHT", {}); + is(textbox.selectionStart, 9, "Should have moved the caret (selectionStart after right)"); + is(textbox.selectionEnd, 9, "Should have moved the caret (selectionEnd after right)"); + is(searchPopup.state, "open", "Popup should still be open"); + + // Ensure caret movement works while a suggestion is selected. + is(textbox.popup.selectedIndex, -1, "No selected item in list"); + EventUtils.synthesizeKey("VK_DOWN", {}); + is(textbox.popup.selectedIndex, 0, "Selected item in list"); + is(textbox.selectionStart, 9, "Should have moved the caret to the end (selectionStart after selection)"); + is(textbox.selectionEnd, 9, "Should have moved the caret to the end (selectionEnd after selection)"); + + EventUtils.synthesizeKey("VK_LEFT", {}); + is(textbox.selectionStart, 8, "Should have moved the caret again (selectionStart after left)"); + is(textbox.selectionEnd, 8, "Should have moved the caret again (selectionEnd after left)"); + is(searchPopup.state, "open", "Popup should still be open"); + + EventUtils.synthesizeKey("VK_LEFT", {}); + is(textbox.selectionStart, 7, "Should have moved the caret (selectionStart after left)"); + is(textbox.selectionEnd, 7, "Should have moved the caret (selectionEnd after left)"); + is(searchPopup.state, "open", "Popup should still be open"); + + EventUtils.synthesizeKey("VK_RIGHT", {}); + is(textbox.selectionStart, 8, "Should have moved the caret (selectionStart after right)"); + is(textbox.selectionEnd, 8, "Should have moved the caret (selectionEnd after right)"); + is(searchPopup.state, "open", "Popup should still be open"); + + if (navigator.platform.indexOf("Mac") == -1) { + EventUtils.synthesizeKey("VK_HOME", {}); + is(textbox.selectionStart, 0, "Should have moved the caret (selectionStart after home)"); + is(textbox.selectionEnd, 0, "Should have moved the caret (selectionEnd after home)"); + is(searchPopup.state, "open", "Popup should still be open"); + } + + // Close the popup again + promise = promiseEvent(searchPopup, "popuphidden"); + EventUtils.synthesizeKey("VK_ESCAPE", {}); + yield promise; + + textbox.value = ""; +}); diff --git a/browser/components/search/test/browser_searchbar_smallpanel_keyboard_navigation.js b/browser/components/search/test/browser_searchbar_smallpanel_keyboard_navigation.js new file mode 100644 index 000000000..37ca32cf2 --- /dev/null +++ b/browser/components/search/test/browser_searchbar_smallpanel_keyboard_navigation.js @@ -0,0 +1,354 @@ +// Tests that keyboard navigation in the search panel works as designed. + +const searchbar = document.getElementById("searchbar"); +const textbox = searchbar._textbox; +const searchPopup = document.getElementById("PopupSearchAutoComplete"); +const oneOffsContainer = + document.getAnonymousElementByAttribute(searchPopup, "anonid", + "search-one-off-buttons"); +const searchIcon = document.getAnonymousElementByAttribute(searchbar, "anonid", + "searchbar-search-button"); + +const kValues = ["foo1", "foo2", "foo3"]; + +function getOpenSearchItems() { + let os = []; + + let addEngineList = + document.getAnonymousElementByAttribute(oneOffsContainer, "anonid", + "add-engines"); + for (let item = addEngineList.firstChild; item; item = item.nextSibling) + os.push(item); + + return os; +} + +add_task(function* init() { + yield promiseNewEngine("testEngine.xml"); + + // First cleanup the form history in case other tests left things there. + yield new Promise((resolve, reject) => { + info("cleanup the search history"); + searchbar.FormHistory.update({op: "remove", fieldname: "searchbar-history"}, + {handleCompletion: resolve, + handleError: reject}); + }); + + yield new Promise((resolve, reject) => { + info("adding search history values: " + kValues); + let ops = kValues.map(value => { return {op: "add", + fieldname: "searchbar-history", + value: value} + }); + searchbar.FormHistory.update(ops, { + handleCompletion: function() { + registerCleanupFunction(() => { + info("removing search history values: " + kValues); + let ops = + kValues.map(value => { return {op: "remove", + fieldname: "searchbar-history", + value: value} + }); + searchbar.FormHistory.update(ops); + }); + resolve(); + }, + handleError: reject + }); + }); +}); + + +add_task(function* test_arrows() { + let promise = promiseEvent(searchPopup, "popupshown"); + info("Opening search panel"); + EventUtils.synthesizeMouseAtCenter(searchIcon, {}); + yield promise; +info("textbox.mController.searchString = " + textbox.mController.searchString); + is(textbox.mController.searchString, "", "The search string should be empty"); + + // Check the initial state of the panel before sending keyboard events. + is(searchPopup.getAttribute("showonlysettings"), "true", "Should show the small popup"); + // Having suggestions populated (but hidden) is important, because if there + // are none we can't ensure the keyboard events don't reach them. + is(searchPopup.view.rowCount, kValues.length, "There should be 3 suggestions"); + is(searchPopup.selectedIndex, -1, "no suggestion should be selected"); + + // The tests will be less meaningful if the first, second, last, and + // before-last one-off buttons aren't different. We should always have more + // than 4 default engines, but it's safer to check this assumption. + let oneOffs = getOneOffs(); + ok(oneOffs.length >= 4, "we have at least 4 one-off buttons displayed") + + ok(!textbox.selectedButton, "no one-off button should be selected"); + + // Pressing should select the first one-off. + EventUtils.synthesizeKey("VK_DOWN", {}); + is(searchPopup.selectedIndex, -1, "no suggestion should be selected"); + is(textbox.value, "", "the textfield value should be unmodified"); + + // now cycle through the one-off items, the first one is already selected. + for (let i = 0; i < oneOffs.length; ++i) { + is(textbox.selectedButton, oneOffs[i], + "the one-off button #" + (i + 1) + " should be selected"); + EventUtils.synthesizeKey("VK_DOWN", {}); + } + + is(textbox.selectedButton.getAttribute("anonid"), "search-settings", + "the settings item should be selected"); + EventUtils.synthesizeKey("VK_DOWN", {}); + + // We should now be back to the initial situation. + is(searchPopup.selectedIndex, -1, "no suggestion should be selected"); + ok(!textbox.selectedButton, "no one-off button should be selected"); + + info("now test the up arrow key"); + EventUtils.synthesizeKey("VK_UP", {}); + is(textbox.selectedButton.getAttribute("anonid"), "search-settings", + "the settings item should be selected"); + + // cycle through the one-off items, the first one is already selected. + for (let i = oneOffs.length; i; --i) { + EventUtils.synthesizeKey("VK_UP", {}); + is(textbox.selectedButton, oneOffs[i - 1], + "the one-off button #" + i + " should be selected"); + } + + // Another press on up should clear the one-off selection. + EventUtils.synthesizeKey("VK_UP", {}); + ok(!textbox.selectedButton, "no one-off button should be selected"); + is(searchPopup.selectedIndex, -1, "no suggestion should be selected"); + is(textbox.value, "", "the textfield value should be unmodified"); +}); + +add_task(function* test_tab() { + is(Services.focus.focusedElement, textbox.inputField, + "the search bar should be focused"); // from the previous test. + + let oneOffs = getOneOffs(); + ok(!textbox.selectedButton, "no one-off button should be selected"); + + // Pressing tab should select the first one-off without selecting suggestions. + // now cycle through the one-off items, the first one is already selected. + for (let i = 0; i < oneOffs.length; ++i) { + EventUtils.synthesizeKey("VK_TAB", {}); + is(textbox.selectedButton, oneOffs[i], + "the one-off button #" + (i + 1) + " should be selected"); + } + is(searchPopup.selectedIndex, -1, "no suggestion should be selected"); + is(textbox.value, "", "the textfield value should be unmodified"); + + // One more <tab> selects the settings button. + EventUtils.synthesizeKey("VK_TAB", {}); + is(textbox.selectedButton.getAttribute("anonid"), "search-settings", + "the settings item should be selected"); + + // Pressing tab again should close the panel... + let promise = promiseEvent(searchPopup, "popuphidden"); + EventUtils.synthesizeKey("VK_TAB", {}); + yield promise; + + // ... and move the focus out of the searchbox. + isnot(Services.focus.focusedElement, textbox.inputField, + "the search bar no longer be focused"); +}); + +add_task(function* test_shift_tab() { + // First reopen the panel. + let promise = promiseEvent(searchPopup, "popupshown"); + info("Opening search panel"); + SimpleTest.executeSoon(() => { + EventUtils.synthesizeMouseAtCenter(searchIcon, {}); + }); + yield promise; + + let oneOffs = getOneOffs(); + ok(!textbox.selectedButton, "no one-off button should be selected"); + is(searchPopup.getAttribute("showonlysettings"), "true", "Should show the small popup"); + + // Press up once to select the last button. + EventUtils.synthesizeKey("VK_UP", {}); + is(textbox.selectedButton.getAttribute("anonid"), "search-settings", + "the settings item should be selected"); + + // Press up again to select the last one-off button. + EventUtils.synthesizeKey("VK_UP", {}); + + // Pressing shift+tab should cycle through the one-off items. + for (let i = oneOffs.length - 1; i >= 0; --i) { + is(textbox.selectedButton, oneOffs[i], + "the one-off button #" + (i + 1) + " should be selected"); + if (i) + EventUtils.synthesizeKey("VK_TAB", {shiftKey: true}); + } + is(searchPopup.selectedIndex, -1, "no suggestion should be selected"); + is(textbox.value, "", "the textfield value should be unmodified"); + + // Pressing shift+tab again should close the panel... + promise = promiseEvent(searchPopup, "popuphidden"); + EventUtils.synthesizeKey("VK_TAB", {shiftKey: true}); + yield promise; + + // ... and move the focus out of the searchbox. + isnot(Services.focus.focusedElement, textbox.inputField, + "the search bar no longer be focused"); +}); + +add_task(function* test_alt_down() { + // First reopen the panel. + let promise = promiseEvent(searchPopup, "popupshown"); + info("Opening search panel"); + SimpleTest.executeSoon(() => { + EventUtils.synthesizeMouseAtCenter(searchIcon, {}); + }); + yield promise; + + // and check it's in a correct initial state. + is(searchPopup.getAttribute("showonlysettings"), "true", "Should show the small popup"); + ok(!textbox.selectedButton, "no one-off button should be selected"); + is(searchPopup.selectedIndex, -1, "no suggestion should be selected"); + is(textbox.value, "", "the textfield value should be unmodified"); + + // Pressing alt+down should select the first one-off without selecting suggestions + // and cycle through the one-off items. + let oneOffs = getOneOffs(); + for (let i = 0; i < oneOffs.length; ++i) { + EventUtils.synthesizeKey("VK_DOWN", {altKey: true}); + is(textbox.selectedButton, oneOffs[i], + "the one-off button #" + (i + 1) + " should be selected"); + is(searchPopup.selectedIndex, -1, "no suggestion should be selected"); + } + + // One more alt+down keypress and nothing should be selected. + EventUtils.synthesizeKey("VK_DOWN", {altKey: true}); + ok(!textbox.selectedButton, "no one-off button should be selected"); + + // another one and the first one-off should be selected. + EventUtils.synthesizeKey("VK_DOWN", {altKey: true}); + is(textbox.selectedButton, oneOffs[0], + "the first one-off button should be selected"); + + // Clear the selection with an alt+up keypress + EventUtils.synthesizeKey("VK_UP", {altKey: true}); + ok(!textbox.selectedButton, "no one-off button should be selected"); +}); + +add_task(function* test_alt_up() { + // Check the initial state of the panel + ok(!textbox.selectedButton, "no one-off button should be selected"); + is(searchPopup.selectedIndex, -1, "no suggestion should be selected"); + is(textbox.value, "", "the textfield value should be unmodified"); + + // Pressing alt+up should select the last one-off without selecting suggestions + // and cycle up through the one-off items. + let oneOffs = getOneOffs(); + for (let i = oneOffs.length - 1; i >= 0; --i) { + EventUtils.synthesizeKey("VK_UP", {altKey: true}); + is(textbox.selectedButton, oneOffs[i], + "the one-off button #" + (i + 1) + " should be selected"); + is(searchPopup.selectedIndex, -1, "no suggestion should be selected"); + } + + // One more alt+down keypress and nothing should be selected. + EventUtils.synthesizeKey("VK_UP", {altKey: true}); + ok(!textbox.selectedButton, "no one-off button should be selected"); + + // another one and the last one-off should be selected. + EventUtils.synthesizeKey("VK_UP", {altKey: true}); + is(textbox.selectedButton, oneOffs[oneOffs.length - 1], + "the last one-off button should be selected"); + + // Cleanup for the next test. + EventUtils.synthesizeKey("VK_DOWN", {}); + is(textbox.selectedButton.getAttribute("anonid"), "search-settings", + "the settings item should be selected"); + EventUtils.synthesizeKey("VK_DOWN", {}); + ok(!textbox.selectedButton, "no one-off should be selected anymore"); +}); + +add_task(function* test_tab_and_arrows() { + // Check the initial state is as expected. + ok(!textbox.selectedButton, "no one-off button should be selected"); + is(searchPopup.selectedIndex, -1, "no suggestion should be selected"); + is(textbox.value, "", "the textfield value should be unmodified"); + + // After pressing down, the first one-off should be selected. + let oneOffs = getOneOffs(); + EventUtils.synthesizeKey("VK_DOWN", {}); + is(textbox.selectedButton, oneOffs[0], + "the first one-off button should be selected"); + is(searchPopup.selectedIndex, -1, "no suggestion should be selected"); + + // After pressing tab, the second one-off should be selected. + EventUtils.synthesizeKey("VK_TAB", {}); + is(textbox.selectedButton, oneOffs[1], + "the second one-off button should be selected"); + is(searchPopup.selectedIndex, -1, "no suggestion should be selected"); + + // After pressing up, the first one-off should be selected again. + EventUtils.synthesizeKey("VK_UP", {}); + is(textbox.selectedButton, oneOffs[0], + "the first one-off button should be selected"); + is(searchPopup.selectedIndex, -1, "no suggestion should be selected"); + + // Finally close the panel. + let promise = promiseEvent(searchPopup, "popuphidden"); + searchPopup.hidePopup(); + yield promise; +}); + +add_task(function* test_open_search() { + let rootDir = getRootDirectory(gTestPath); + yield BrowserTestUtils.openNewForegroundTab(gBrowser, rootDir + "opensearch.html"); + + let promise = promiseEvent(searchPopup, "popupshown"); + info("Opening search panel"); + EventUtils.synthesizeMouseAtCenter(searchIcon, {}); + yield promise; + is(searchPopup.getAttribute("showonlysettings"), "true", "Should show the small popup"); + + let engines = getOpenSearchItems(); + is(engines.length, 2, "the opensearch.html page exposes 2 engines") + + // Check that there's initially no selection. + is(searchPopup.selectedIndex, -1, "no suggestion should be selected"); + ok(!textbox.selectedButton, "no button should be selected"); + + // Pressing up once selects the setting button... + EventUtils.synthesizeKey("VK_UP", {}); + is(textbox.selectedButton.getAttribute("anonid"), "search-settings", + "the settings item should be selected"); + + // ...and then pressing up selects open search engines. + for (let i = engines.length; i; --i) { + EventUtils.synthesizeKey("VK_UP", {}); + let selectedButton = textbox.selectedButton; + is(selectedButton, engines[i - 1], + "the engine #" + i + " should be selected"); + ok(selectedButton.classList.contains("addengine-item"), + "the button is themed as an engine item"); + } + + // Pressing up again should select the last one-off button. + EventUtils.synthesizeKey("VK_UP", {}); + is(textbox.selectedButton, getOneOffs().pop(), + "the last one-off button should be selected"); + + info("now check that the down key navigates open search items as expected"); + for (let i = 0; i < engines.length; ++i) { + EventUtils.synthesizeKey("VK_DOWN", {}); + is(textbox.selectedButton, engines[i], + "the engine #" + (i + 1) + " should be selected"); + } + + // Pressing down on the last engine item selects the settings button. + EventUtils.synthesizeKey("VK_DOWN", {}); + is(textbox.selectedButton.getAttribute("anonid"), "search-settings", + "the settings item should be selected"); + + promise = promiseEvent(searchPopup, "popuphidden"); + searchPopup.hidePopup(); + yield promise; + + gBrowser.removeCurrentTab(); +}); diff --git a/browser/components/search/test/browser_webapi.js b/browser/components/search/test/browser_webapi.js new file mode 100644 index 000000000..d8161ffbe --- /dev/null +++ b/browser/components/search/test/browser_webapi.js @@ -0,0 +1,92 @@ +var ROOT = getRootDirectory(gTestPath).replace("chrome://mochitests/content", "http://example.com"); +const searchBundle = Services.strings.createBundle("chrome://global/locale/search/search.properties"); +const brandBundle = Services.strings.createBundle("chrome://branding/locale/brand.properties"); +const brandName = brandBundle.GetStringFromName("brandShortName"); + +function getString(key, ...params) { + return searchBundle.formatStringFromName(key, params, params.length); +} + +function AddSearchProvider(...args) { + return gBrowser.addTab(ROOT + "webapi.html?" + encodeURIComponent(JSON.stringify(args))); +} + +function promiseDialogOpened() { + return new Promise((resolve, reject) => { + Services.wm.addListener({ + onOpenWindow: function(xulWin) { + Services.wm.removeListener(this); + + let win = xulWin.QueryInterface(Ci.nsIInterfaceRequestor) + .getInterface(Ci.nsIDOMWindow); + waitForFocus(() => { + if (win.location == "chrome://global/content/commonDialog.xul") + resolve(win) + else + reject(); + }, win); + } + }); + }); +} + +add_task(function* test_working() { + gBrowser.selectedTab = AddSearchProvider(ROOT + "testEngine.xml"); + + let dialog = yield promiseDialogOpened(); + is(dialog.args.promptType, "confirmEx", "Should see the confirmation dialog."); + is(dialog.args.text, getString("addEngineConfirmation", "Foo", "example.com"), + "Should have seen the right install message"); + dialog.document.documentElement.cancelDialog(); + + gBrowser.removeCurrentTab(); +}); + +add_task(function* test_HTTP() { + gBrowser.selectedTab = AddSearchProvider(ROOT.replace("http:", "HTTP:") + "testEngine.xml"); + + let dialog = yield promiseDialogOpened(); + is(dialog.args.promptType, "confirmEx", "Should see the confirmation dialog."); + is(dialog.args.text, getString("addEngineConfirmation", "Foo", "example.com"), + "Should have seen the right install message"); + dialog.document.documentElement.cancelDialog(); + + gBrowser.removeCurrentTab(); +}); + +add_task(function* test_relative() { + gBrowser.selectedTab = AddSearchProvider("testEngine.xml"); + + let dialog = yield promiseDialogOpened(); + is(dialog.args.promptType, "confirmEx", "Should see the confirmation dialog."); + is(dialog.args.text, getString("addEngineConfirmation", "Foo", "example.com"), + "Should have seen the right install message"); + dialog.document.documentElement.cancelDialog(); + + gBrowser.removeCurrentTab(); +}); + +add_task(function* test_invalid() { + gBrowser.selectedTab = AddSearchProvider("z://foobar"); + + let dialog = yield promiseDialogOpened(); + is(dialog.args.promptType, "alert", "Should see the alert dialog."); + is(dialog.args.text, getString("error_invalid_engine_msg", brandName), + "Should have seen the right error message") + dialog.document.documentElement.acceptDialog(); + + gBrowser.removeCurrentTab(); +}); + +add_task(function* test_missing() { + let url = ROOT + "foobar.xml"; + gBrowser.selectedTab = AddSearchProvider(url); + + let dialog = yield promiseDialogOpened(); + is(dialog.args.promptType, "alert", "Should see the alert dialog."); + is(dialog.args.text, getString("error_loading_engine_msg2", brandName, url), + "Should have seen the right error message") + dialog.document.documentElement.acceptDialog(); + + gBrowser.removeCurrentTab(); +}); diff --git a/browser/components/search/test/browser_yahoo.js b/browser/components/search/test/browser_yahoo.js new file mode 100644 index 000000000..f45b47d0c --- /dev/null +++ b/browser/components/search/test/browser_yahoo.js @@ -0,0 +1,132 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ */ + +/* + * Test Yahoo search plugin URLs + */ + +"use strict"; + +const BROWSER_SEARCH_PREF = "browser.search."; + +function test() { + let engine = Services.search.getEngineByName("Yahoo"); + ok(engine, "Yahoo"); + + let base = "https://search.yahoo.com/yhs/search?p=foo&ei=UTF-8&hspart=mozilla"; + let url; + + // Test search URLs (including purposes). + url = engine.getSubmission("foo").uri.spec; + is(url, base + "&hsimp=yhs-001", "Check search URL for 'foo'"); + url = engine.getSubmission("foo", null, "searchbar").uri.spec; + is(url, base + "&hsimp=yhs-001", "Check search bar search URL for 'foo'"); + url = engine.getSubmission("foo", null, "keyword").uri.spec; + is(url, base + "&hsimp=yhs-002", "Check keyword search URL for 'foo'"); + url = engine.getSubmission("foo", null, "homepage").uri.spec; + is(url, base + "&hsimp=yhs-003", "Check homepage search URL for 'foo'"); + url = engine.getSubmission("foo", null, "newtab").uri.spec; + is(url, base + "&hsimp=yhs-004", "Check newtab search URL for 'foo'"); + url = engine.getSubmission("foo", null, "contextmenu").uri.spec; + is(url, base + "&hsimp=yhs-005", "Check context menu search URL for 'foo'"); + url = engine.getSubmission("foo", null, "system").uri.spec; + is(url, base + "&hsimp=yhs-007", "Check system search URL for 'foo'"); + url = engine.getSubmission("foo", null, "invalid").uri.spec; + is(url, base + "&hsimp=yhs-001", "Check invalid URL for 'foo'"); + + // Check search suggestion URL. + url = engine.getSubmission("foo", "application/x-suggestions+json").uri.spec; + is(url, "https://search.yahoo.com/sugg/ff?output=fxjson&appid=ffd&command=foo", "Check search suggestion URL for 'foo'"); + + // Check all other engine properties. + const EXPECTED_ENGINE = { + name: "Yahoo", + alias: null, + description: "Yahoo Search", + searchForm: "https://search.yahoo.com/yhs/search?p=&ei=UTF-8&hspart=mozilla&hsimp=yhs-001", + hidden: false, + wrappedJSObject: { + queryCharset: "UTF-8", + "_iconURL": "data:image/x-icon;base64,AAABAAIAEBAAAAEACAA8DQAAJgAAACAgAAABAAgAowsAAGINAACJUE5HDQoaCgAAAA1JSERSAAAAEAAAABAIBgAAAB/z/2EAAAAJcEhZcwAACxMAAAsTAQCanBgAAApPaUNDUFBob3Rvc2hvcCBJQ0MgcHJvZmlsZQAAeNqdU2dUU+kWPffe9EJLiICUS29SFQggUkKLgBSRJiohCRBKiCGh2RVRwRFFRQQbyKCIA46OgIwVUSwMigrYB+Qhoo6Do4iKyvvhe6Nr1rz35s3+tdc+56zznbPPB8AIDJZIM1E1gAypQh4R4IPHxMbh5C5AgQokcAAQCLNkIXP9IwEA+H48PCsiwAe+AAF40wsIAMBNm8AwHIf/D+pCmVwBgIQBwHSROEsIgBQAQHqOQqYAQEYBgJ2YJlMAoAQAYMtjYuMAUC0AYCd/5tMAgJ34mXsBAFuUIRUBoJEAIBNliEQAaDsArM9WikUAWDAAFGZLxDkA2C0AMElXZkgAsLcAwM4QC7IACAwAMFGIhSkABHsAYMgjI3gAhJkAFEbyVzzxK64Q5yoAAHiZsjy5JDlFgVsILXEHV1cuHijOSRcrFDZhAmGaQC7CeZkZMoE0D+DzzAAAoJEVEeCD8/14zg6uzs42jrYOXy3qvwb/ImJi4/7lz6twQAAA4XR+0f4sL7MagDsGgG3+oiXuBGheC6B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gO93hQJCBsSa2GQyyoLlBzWDxgnm9l0JgADgNgVxElCH22xs4NCsaieSUyzWXaSTLDAPlGQB0Kt6JaqpzYjkJQT9id60aNwqZjVqlz9Kqp+JcfDjOAqhirNoCI6MelpVPAjZ/CbFv45Y9YNcicqDMKm/Xo/FPJdMlqZ9SIK7qSrrci9mbl6q3/DGQ5f7XuK347rgKeuMgiicEfLPmT0rGY1K5SdI/ryritlMbJrr/PZ8+I8qf9PF8qhMrT39QHfHLkhj/fz/bi+eb83F/VxX1b6jWvt6KdTs/AvvCmqXE235jAAAAAElFTkSuQmCC", + _urls : [ + { + type: "application/x-suggestions+json", + method: "GET", + template: "https://search.yahoo.com/sugg/ff", + params: [ + { + name: "output", + value: "fxjson", + purpose: undefined, + }, + { + name: "appid", + value: "ffd", + purpose: undefined, + }, + { + name: "command", + value: "{searchTerms}", + purpose: undefined, + }, + ], + }, + { + type: "text/html", + method: "GET", + template: "https://search.yahoo.com/yhs/search", + params: [ + { + name: "p", + value: "{searchTerms}", + purpose: undefined, + }, + { + name: "ei", + value: "UTF-8", + purpose: undefined, + }, + { + name: "hspart", + value: "mozilla", + purpose: undefined, + }, + { + name: "hsimp", + value: "yhs-001", + purpose: "searchbar", + }, + { + name: "hsimp", + value: "yhs-002", + purpose: "keyword", + }, + { + name: "hsimp", + value: "yhs-003", + purpose: "homepage", + }, + { + name: "hsimp", + value: "yhs-004", + purpose: "newtab", + }, + { + name: "hsimp", + value: "yhs-005", + purpose: "contextmenu", + }, + { + name: "hsimp", + value: "yhs-007", + purpose: "system", + }, + ], + mozparams: {}, + }, + ], + }, + }; + + isSubObjectOf(EXPECTED_ENGINE, engine, "Yahoo"); +} diff --git a/browser/components/search/test/browser_yahoo_behavior.js b/browser/components/search/test/browser_yahoo_behavior.js new file mode 100644 index 000000000..5b2d61422 --- /dev/null +++ b/browser/components/search/test/browser_yahoo_behavior.js @@ -0,0 +1,166 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ */ + +/* + * Test Yahoo search plugin URLs + */ + +"use strict"; + +const BROWSER_SEARCH_PREF = "browser.search."; + + +function test() { + let engine = Services.search.getEngineByName("Yahoo"); + ok(engine, "Yahoo is installed"); + + let previouslySelectedEngine = Services.search.currentEngine; + Services.search.currentEngine = engine; + engine.alias = "y"; + + let base = "https://search.yahoo.com/yhs/search?p=foo&ei=UTF-8&hspart=mozilla"; + let url; + + // Test search URLs (including purposes). + url = engine.getSubmission("foo").uri.spec; + is(url, base + "&hsimp=yhs-001", "Check search URL for 'foo'"); + + waitForExplicitFinish(); + + var gCurrTest; + var gTests = [ + { + name: "context menu search", + searchURL: base + "&hsimp=yhs-005", + run: function () { + // Simulate a contextmenu search + // FIXME: This is a bit "low-level"... + BrowserSearch.loadSearch("foo", false, "contextmenu"); + } + }, + { + name: "keyword search", + searchURL: base + "&hsimp=yhs-002", + run: function () { + gURLBar.value = "? foo"; + gURLBar.focus(); + EventUtils.synthesizeKey("VK_RETURN", {}); + } + }, + { + name: "keyword search with alias", + searchURL: base + "&hsimp=yhs-002", + run: function () { + gURLBar.value = "y foo"; + gURLBar.focus(); + EventUtils.synthesizeKey("VK_RETURN", {}); + } + }, + { + name: "search bar search", + searchURL: base + "&hsimp=yhs-001", + run: function () { + let sb = BrowserSearch.searchBar; + sb.focus(); + sb.value = "foo"; + registerCleanupFunction(function () { + sb.value = ""; + }); + EventUtils.synthesizeKey("VK_RETURN", {}); + } + }, + { + name: "new tab search", + searchURL: base + "&hsimp=yhs-004", + run: function () { + function doSearch(doc) { + // Re-add the listener, and perform a search + gBrowser.addProgressListener(listener); + doc.getElementById("newtab-search-text").value = "foo"; + doc.getElementById("newtab-search-submit").click(); + } + + // load about:newtab, but remove the listener first so it doesn't + // get in the way + gBrowser.removeProgressListener(listener); + gBrowser.loadURI("about:newtab"); + info("Waiting for about:newtab load"); + tab.linkedBrowser.addEventListener("load", function load(event) { + if (event.originalTarget != tab.linkedBrowser.contentDocument || + event.target.location.href == "about:blank") { + info("skipping spurious load event"); + return; + } + tab.linkedBrowser.removeEventListener("load", load, true); + + // Observe page setup + let win = gBrowser.contentWindow; + if (win.gSearch.currentEngineName == + Services.search.currentEngine.name) { + doSearch(win.document); + } + else { + info("Waiting for newtab search init"); + win.addEventListener("ContentSearchService", function done(event) { + info("Got newtab search event " + event.detail.type); + if (event.detail.type == "State") { + win.removeEventListener("ContentSearchService", done); + // Let gSearch respond to the event before continuing. + executeSoon(() => doSearch(win.document)); + } + }); + } + }, true); + } + } + ]; + + function nextTest() { + if (gTests.length) { + gCurrTest = gTests.shift(); + info("Running : " + gCurrTest.name); + executeSoon(gCurrTest.run); + } else { + finish(); + } + } + + let tab = gBrowser.selectedTab = gBrowser.addTab(); + + let listener = { + onStateChange: function onStateChange(webProgress, req, flags, status) { + info("onStateChange"); + // Only care about top-level document starts + let docStart = Ci.nsIWebProgressListener.STATE_IS_DOCUMENT | + Ci.nsIWebProgressListener.STATE_START; + if (!(flags & docStart) || !webProgress.isTopLevel) + return; + + if (req.originalURI.spec == "about:blank") + return; + + info("received document start"); + + ok(req instanceof Ci.nsIChannel, "req is a channel"); + is(req.originalURI.spec, gCurrTest.searchURL, "search URL was loaded"); + info("Actual URI: " + req.URI.spec); + + req.cancel(Components.results.NS_ERROR_FAILURE); + + executeSoon(nextTest); + } + } + + registerCleanupFunction(function () { + engine.alias = undefined; + gBrowser.removeProgressListener(listener); + gBrowser.removeTab(tab); + Services.search.currentEngine = previouslySelectedEngine; + }); + + tab.linkedBrowser.addEventListener("load", function load() { + tab.linkedBrowser.removeEventListener("load", load, true); + gBrowser.addProgressListener(listener); + nextTest(); + }, true); +} diff --git a/browser/components/search/test/head.js b/browser/components/search/test/head.js new file mode 100644 index 000000000..de27b5e1e --- /dev/null +++ b/browser/components/search/test/head.js @@ -0,0 +1,156 @@ +/* Any copyright is dedicated to the Public Domain. + * http://creativecommons.org/publicdomain/zero/1.0/ */ + +Cu.import("resource://gre/modules/Promise.jsm"); + +/** + * Recursively compare two objects and check that every property of expectedObj has the same value + * on actualObj. + */ +function isSubObjectOf(expectedObj, actualObj, name) { + for (let prop in expectedObj) { + if (typeof expectedObj[prop] == 'function') + continue; + if (expectedObj[prop] instanceof Object) { + is(actualObj[prop].length, expectedObj[prop].length, name + "[" + prop + "]"); + isSubObjectOf(expectedObj[prop], actualObj[prop], name + "[" + prop + "]"); + } else { + is(actualObj[prop], expectedObj[prop], name + "[" + prop + "]"); + } + } +} + +function getLocale() { + const localePref = "general.useragent.locale"; + return getLocalizedPref(localePref, Services.prefs.getCharPref(localePref)); +} + +/** + * Wrapper for nsIPrefBranch::getComplexValue. + * @param aPrefName + * The name of the pref to get. + * @returns aDefault if the requested pref doesn't exist. + */ +function getLocalizedPref(aPrefName, aDefault) { + try { + return Services.prefs.getComplexValue(aPrefName, Ci.nsIPrefLocalizedString).data; + } catch (ex) { + return aDefault; + } +} + +function promiseEvent(aTarget, aEventName, aPreventDefault) { + function cancelEvent(event) { + if (aPreventDefault) { + event.preventDefault(); + } + + return true; + } + + return BrowserTestUtils.waitForEvent(aTarget, aEventName, false, cancelEvent); +} + +/** + * Adds a new search engine to the search service and confirms it completes. + * + * @param {String} basename The file to load that contains the search engine + * details. + * @param {Object} [options] Options for the test: + * - {String} [iconURL] The icon to use for the search engine. + * - {Boolean} [setAsCurrent] Whether to set the new engine to be the + * current engine or not. + * - {String} [testPath] Used to override the current test path if this + * file is used from a different directory. + * @returns {Promise} The promise is resolved once the engine is added, or + * rejected if the addition failed. + */ +function promiseNewEngine(basename, options = {}) { + return new Promise((resolve, reject) => { + // Default the setAsCurrent option to true. + let setAsCurrent = + options.setAsCurrent == undefined ? true : options.setAsCurrent; + info("Waiting for engine to be added: " + basename); + Services.search.init({ + onInitComplete: function() { + let url = getRootDirectory(options.testPath || gTestPath) + basename; + let current = Services.search.currentEngine; + Services.search.addEngine(url, null, options.iconURL || "", false, { + onSuccess: function (engine) { + info("Search engine added: " + basename); + if (setAsCurrent) { + Services.search.currentEngine = engine; + } + registerCleanupFunction(() => { + if (setAsCurrent) { + Services.search.currentEngine = current; + } + Services.search.removeEngine(engine); + info("Search engine removed: " + basename); + }); + resolve(engine); + }, + onError: function (errCode) { + ok(false, "addEngine failed with error code " + errCode); + reject(); + } + }); + } + }); + }); +} + +/** + * Waits for a load (or custom) event to finish in a given tab. If provided + * load an uri into the tab. + * + * @param tab + * The tab to load into. + * @param [optional] url + * The url to load, or the current url. + * @return {Promise} resolved when the event is handled. + * @resolves to the received event + * @rejects if a valid load event is not received within a meaningful interval + */ +function promiseTabLoadEvent(tab, url) +{ + info("Wait tab event: load"); + + function handle(loadedUrl) { + if (loadedUrl === "about:blank" || (url && loadedUrl !== url)) { + info(`Skipping spurious load event for ${loadedUrl}`); + return false; + } + + info("Tab event received: load"); + return true; + } + + let loaded = BrowserTestUtils.browserLoaded(tab.linkedBrowser, false, handle); + + if (url) + BrowserTestUtils.loadURI(tab.linkedBrowser, url); + + return loaded; +} + +// Get an array of the one-off buttons. +function getOneOffs() { + let oneOffs = []; + let searchPopup = document.getElementById("PopupSearchAutoComplete"); + let oneOffsContainer = + document.getAnonymousElementByAttribute(searchPopup, "anonid", + "search-one-off-buttons"); + let oneOff = + document.getAnonymousElementByAttribute(oneOffsContainer, "anonid", + "search-panel-one-offs"); + for (oneOff = oneOff.firstChild; oneOff; oneOff = oneOff.nextSibling) { + if (oneOff.nodeType == Node.ELEMENT_NODE) { + if (oneOff.classList.contains("dummy") || + oneOff.classList.contains("search-setting-button-compact")) + break; + oneOffs.push(oneOff); + } + } + return oneOffs; +} diff --git a/browser/components/search/test/opensearch.html b/browser/components/search/test/opensearch.html new file mode 100644 index 000000000..f4c0cc98e --- /dev/null +++ b/browser/components/search/test/opensearch.html @@ -0,0 +1,9 @@ +<!DOCTYPE html> +<html> +<head> +<meta charset="UTF-8"> +<link rel="search" type="application/opensearchdescription+xml" title="engine1" href="http://mochi.test:8888/browser/browser/components/search/test/testEngine.xml"> +<link rel="search" type="application/opensearchdescription+xml" title="engine2" href="http://mochi.test:8888/browser/browser/components/search/test/testEngine_mozsearch.xml"> +</head> +<body></body> +</html> diff --git a/browser/components/search/test/test.html b/browser/components/search/test/test.html new file mode 100644 index 000000000..a39bece4f --- /dev/null +++ b/browser/components/search/test/test.html @@ -0,0 +1,8 @@ +<!DOCTYPE html> +<html> +<head> + <meta charset="utf-8" /> + <title>Bug 426329</title> +</head> +<body></body> +</html> diff --git a/browser/components/search/test/testEngine.xml b/browser/components/search/test/testEngine.xml new file mode 100644 index 000000000..21ddc4b9a --- /dev/null +++ b/browser/components/search/test/testEngine.xml @@ -0,0 +1,12 @@ +<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/" + xmlns:moz="http://www.mozilla.org/2006/browser/search/"> + <ShortName>Foo</ShortName> + <Description>Foo Search</Description> + <InputEncoding>utf-8</InputEncoding> + <Image width="16" height="16">data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAABGklEQVQoz2NgGB6AnZ1dUlJSXl4eSDIyMhLW4Ovr%2B%2Fr168uXL69Zs4YoG%2BLi4i5dusTExMTGxsbNzd3f37937976%2BnpmZmagbHR09J49e5YvX66kpATVEBYW9ubNm2nTphkbG7e2tp44cQLIuHfvXm5urpaWFlDKysqqu7v73LlzECMYIiIiHj58mJCQoKKicvXq1bS0NKBgW1vbjh074uPjgeqAXE1NzSdPnvDz84M0AEUvXLgAsW379u1z5swBen3jxo2zZ892cHB4%2BvQp0KlAfwI1cHJyghQFBwfv2rULokFXV%2FfixYu7d%2B8GGqGgoMDKyrpu3br9%2B%2FcDuXl5eVA%2FAEWBfoWHAdAYoNuAYQ0XAeoUERFhGDYAAPoUaT2dfWJuAAAAAElFTkSuQmCC</Image> + <Url type="text/html" method="GET" template="http://mochi.test:8888/browser/browser/components/search/test/?search"> + <Param name="test" value="{searchTerms}"/> + </Url> + <moz:SearchForm>http://mochi.test:8888/browser/browser/components/search/test/</moz:SearchForm> + <moz:Alias>fooalias</moz:Alias> +</OpenSearchDescription> diff --git a/browser/components/search/test/testEngine_diacritics.xml b/browser/components/search/test/testEngine_diacritics.xml new file mode 100644 index 000000000..0744921eb --- /dev/null +++ b/browser/components/search/test/testEngine_diacritics.xml @@ -0,0 +1,12 @@ +<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/" + xmlns:moz="http://www.mozilla.org/2006/browser/search/"> + <ShortName>Foo ♡</ShortName> + <Description>Engine whose ShortName contains non-BMP Unicode characters</Description> + <InputEncoding>utf-8</InputEncoding> + <Image width="16" height="16">data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAABGklEQVQoz2NgGB6AnZ1dUlJSXl4eSDIyMhLW4Ovr%2B%2Fr168uXL69Zs4YoG%2BLi4i5dusTExMTGxsbNzd3f37937976%2BnpmZmagbHR09J49e5YvX66kpATVEBYW9ubNm2nTphkbG7e2tp44cQLIuHfvXm5urpaWFlDKysqqu7v73LlzECMYIiIiHj58mJCQoKKicvXq1bS0NKBgW1vbjh074uPjgeqAXE1NzSdPnvDz84M0AEUvXLgAsW379u1z5swBen3jxo2zZ892cHB4%2BvQp0KlAfwI1cHJyghQFBwfv2rULokFXV%2FfixYu7d%2B8GGqGgoMDKyrpu3br9%2B%2FcDuXl5eVA%2FAEWBfoWHAdAYoNuAYQ0XAeoUERFhGDYAAPoUaT2dfWJuAAAAAElFTkSuQmCC</Image> + <Url type="text/html" method="GET" template="http://mochi.test:8888/browser/browser/components/search/test/?search"> + <Param name="test" value="{searchTerms}"/> + </Url> + <moz:SearchForm>http://mochi.test:8888/browser/browser/components/search/test/</moz:SearchForm> + <moz:Alias>diacriticalias</moz:Alias> +</OpenSearchDescription> diff --git a/browser/components/search/test/testEngine_dupe.xml b/browser/components/search/test/testEngine_dupe.xml new file mode 100644 index 000000000..d2db580c4 --- /dev/null +++ b/browser/components/search/test/testEngine_dupe.xml @@ -0,0 +1,12 @@ +<OpenSearchDescription xmlns="http://a9.com/-/spec/opensearch/1.1/" + xmlns:moz="http://www.mozilla.org/2006/browser/search/"> + <ShortName>FooDupe</ShortName> + <Description>Second Engine Search</Description> + <InputEncoding>utf-8</InputEncoding> + <Image width="16" height="16">data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAABGklEQVQoz2NgGB6AnZ1dUlJSXl4eSDIyMhLW4Ovr%2B%2Fr168uXL69Zs4YoG%2BLi4i5dusTExMTGxsbNzd3f37937976%2BnpmZmagbHR09J49e5YvX66kpATVEBYW9ubNm2nTphkbG7e2tp44cQLIuHfvXm5urpaWFlDKysqqu7v73LlzECMYIiIiHj58mJCQoKKicvXq1bS0NKBgW1vbjh074uPjgeqAXE1NzSdPnvDz84M0AEUvXLgAsW379u1z5swBen3jxo2zZ892cHB4%2BvQp0KlAfwI1cHJyghQFBwfv2rULokFXV%2FfixYu7d%2B8GGqGgoMDKyrpu3br9%2B%2FcDuXl5eVA%2FAEWBfoWHAdAYoNuAYQ0XAeoUERFhGDYAAPoUaT2dfWJuAAAAAElFTkSuQmCC</Image> + <Url type="text/html" method="GET" template="http://mochi.test:8888/browser/browser/components/search/test/?search"> + <Param name="test" value="{searchTerms}"/> + </Url> + <moz:SearchForm>http://mochi.test:8888/browser/browser/components/search/test/</moz:SearchForm> + <moz:Alias>secondalias</moz:Alias> +</OpenSearchDescription> diff --git a/browser/components/search/test/testEngine_mozsearch.xml b/browser/components/search/test/testEngine_mozsearch.xml new file mode 100644 index 000000000..9b4c02a0c --- /dev/null +++ b/browser/components/search/test/testEngine_mozsearch.xml @@ -0,0 +1,14 @@ +<SearchPlugin xmlns="http://www.mozilla.org/2006/browser/search/"> + <ShortName>Foo</ShortName> + <Description>Foo Search</Description> + <InputEncoding>utf-8</InputEncoding> + <Image width="16" height="16">data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAIAAACQkWg2AAABGklEQVQoz2NgGB6AnZ1dUlJSXl4eSDIyMhLW4Ovr%2B%2Fr168uXL69Zs4YoG%2BLi4i5dusTExMTGxsbNzd3f37937976%2BnpmZmagbHR09J49e5YvX66kpATVEBYW9ubNm2nTphkbG7e2tp44cQLIuHfvXm5urpaWFlDKysqqu7v73LlzECMYIiIiHj58mJCQoKKicvXq1bS0NKBgW1vbjh074uPjgeqAXE1NzSdPnvDz84M0AEUvXLgAsW379u1z5swBen3jxo2zZ892cHB4%2BvQp0KlAfwI1cHJyghQFBwfv2rULokFXV%2FfixYu7d%2B8GGqGgoMDKyrpu3br9%2B%2FcDuXl5eVA%2FAEWBfoWHAdAYoNuAYQ0XAeoUERFhGDYAAPoUaT2dfWJuAAAAAElFTkSuQmCC</Image> + <Url type="application/x-suggestions+json" method="GET" template="http://mochi.test:8888/browser/browser/components/search/test/?suggestions&locale={moz:locale}&test={searchTerms}"/> + <Url type="text/html" method="GET" template="http://mochi.test:8888/browser/browser/components/search/test/"> + <Param name="test" value="{searchTerms}"/> + <Param name="ie" value="utf-8"/> + <MozParam name="channel" condition="purpose" purpose="keyword" value="keywordsearch"/> + <MozParam name="channel" condition="purpose" purpose="contextmenu" value="contextsearch"/> + </Url> + <SearchForm>http://mochi.test:8888/browser/browser/components/search/test/</SearchForm> +</SearchPlugin> diff --git a/browser/components/search/test/webapi.html b/browser/components/search/test/webapi.html new file mode 100644 index 000000000..1ef38b895 --- /dev/null +++ b/browser/components/search/test/webapi.html @@ -0,0 +1,16 @@ +<!DOCTYPE html> + +<html> +<head> +<script> +function installEngine() { + var query = window.location.search.substring(1); + var args = JSON.parse(decodeURIComponent(query)); + + window.external.AddSearchProvider(...args); +} +</script> +</head> +<body onload="installEngine()"> +</body> +</html> |