Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* Any copyright is dedicated to the Public Domain. |
michael@0 | 2 | * http://creativecommons.org/publicdomain/zero/1.0/ */ |
michael@0 | 3 | |
michael@0 | 4 | /* |
michael@0 | 5 | * Test Bing search plugin URLs |
michael@0 | 6 | */ |
michael@0 | 7 | |
michael@0 | 8 | "use strict"; |
michael@0 | 9 | |
michael@0 | 10 | const BROWSER_SEARCH_PREF = "browser.search."; |
michael@0 | 11 | |
michael@0 | 12 | let runtime = Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULRuntime); |
michael@0 | 13 | // Custom search parameters |
michael@0 | 14 | const PC_PARAM_VALUE = runtime.isOfficialBranding ? "MOZI" : null; |
michael@0 | 15 | |
michael@0 | 16 | function test() { |
michael@0 | 17 | let engine = Services.search.getEngineByName("Bing"); |
michael@0 | 18 | ok(engine, "Bing is installed"); |
michael@0 | 19 | |
michael@0 | 20 | let previouslySelectedEngine = Services.search.currentEngine; |
michael@0 | 21 | Services.search.currentEngine = engine; |
michael@0 | 22 | |
michael@0 | 23 | let base = "http://www.bing.com/search?q=foo"; |
michael@0 | 24 | if (typeof(PC_PARAM_VALUE) == "string") |
michael@0 | 25 | base += "&pc=" + PC_PARAM_VALUE; |
michael@0 | 26 | |
michael@0 | 27 | let url; |
michael@0 | 28 | |
michael@0 | 29 | // Test search URLs (including purposes). |
michael@0 | 30 | url = engine.getSubmission("foo").uri.spec; |
michael@0 | 31 | is(url, base, "Check search URL for 'foo'"); |
michael@0 | 32 | |
michael@0 | 33 | waitForExplicitFinish(); |
michael@0 | 34 | |
michael@0 | 35 | var gCurrTest; |
michael@0 | 36 | var gTests = [ |
michael@0 | 37 | { |
michael@0 | 38 | name: "context menu search", |
michael@0 | 39 | searchURL: base + "&form=MOZCON", |
michael@0 | 40 | run: function () { |
michael@0 | 41 | // Simulate a contextmenu search |
michael@0 | 42 | // FIXME: This is a bit "low-level"... |
michael@0 | 43 | BrowserSearch.loadSearch("foo", false, "contextmenu"); |
michael@0 | 44 | } |
michael@0 | 45 | }, |
michael@0 | 46 | { |
michael@0 | 47 | name: "keyword search", |
michael@0 | 48 | searchURL: base + "&form=MOZLBR", |
michael@0 | 49 | run: function () { |
michael@0 | 50 | gURLBar.value = "? foo"; |
michael@0 | 51 | gURLBar.focus(); |
michael@0 | 52 | EventUtils.synthesizeKey("VK_RETURN", {}); |
michael@0 | 53 | } |
michael@0 | 54 | }, |
michael@0 | 55 | { |
michael@0 | 56 | name: "search bar search", |
michael@0 | 57 | searchURL: base + "&form=MOZSBR", |
michael@0 | 58 | run: function () { |
michael@0 | 59 | let sb = BrowserSearch.searchBar; |
michael@0 | 60 | sb.focus(); |
michael@0 | 61 | sb.value = "foo"; |
michael@0 | 62 | registerCleanupFunction(function () { |
michael@0 | 63 | sb.value = ""; |
michael@0 | 64 | }); |
michael@0 | 65 | EventUtils.synthesizeKey("VK_RETURN", {}); |
michael@0 | 66 | } |
michael@0 | 67 | }, |
michael@0 | 68 | { |
michael@0 | 69 | name: "new tab search", |
michael@0 | 70 | searchURL: base + "&form=MOZTSB", |
michael@0 | 71 | run: function () { |
michael@0 | 72 | function doSearch(doc) { |
michael@0 | 73 | // Re-add the listener, and perform a search |
michael@0 | 74 | gBrowser.addProgressListener(listener); |
michael@0 | 75 | doc.getElementById("newtab-search-text").value = "foo"; |
michael@0 | 76 | doc.getElementById("newtab-search-submit").click(); |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | // load about:newtab, but remove the listener first so it doesn't |
michael@0 | 80 | // get in the way |
michael@0 | 81 | gBrowser.removeProgressListener(listener); |
michael@0 | 82 | gBrowser.loadURI("about:newtab"); |
michael@0 | 83 | info("Waiting for about:newtab load"); |
michael@0 | 84 | tab.linkedBrowser.addEventListener("load", function load(event) { |
michael@0 | 85 | if (event.originalTarget != tab.linkedBrowser.contentDocument || |
michael@0 | 86 | event.target.location.href == "about:blank") { |
michael@0 | 87 | info("skipping spurious load event"); |
michael@0 | 88 | return; |
michael@0 | 89 | } |
michael@0 | 90 | tab.linkedBrowser.removeEventListener("load", load, true); |
michael@0 | 91 | |
michael@0 | 92 | // Observe page setup |
michael@0 | 93 | let win = gBrowser.contentWindow; |
michael@0 | 94 | if (win.gSearch.currentEngineName == |
michael@0 | 95 | Services.search.currentEngine.name) { |
michael@0 | 96 | doSearch(win.document); |
michael@0 | 97 | } |
michael@0 | 98 | else { |
michael@0 | 99 | info("Waiting for newtab search init"); |
michael@0 | 100 | win.addEventListener("ContentSearchService", function done(event) { |
michael@0 | 101 | info("Got newtab search event " + event.detail.type); |
michael@0 | 102 | if (event.detail.type == "State") { |
michael@0 | 103 | win.removeEventListener("ContentSearchService", done); |
michael@0 | 104 | // Let gSearch respond to the event before continuing. |
michael@0 | 105 | executeSoon(() => doSearch(win.document)); |
michael@0 | 106 | } |
michael@0 | 107 | }); |
michael@0 | 108 | } |
michael@0 | 109 | }, true); |
michael@0 | 110 | } |
michael@0 | 111 | }, |
michael@0 | 112 | { |
michael@0 | 113 | name: "home page search", |
michael@0 | 114 | searchURL: base + "&form=MOZSPG", |
michael@0 | 115 | run: function () { |
michael@0 | 116 | // Bug 992270: Ignore uncaught about:home exceptions (related to snippets from IndexedDB) |
michael@0 | 117 | ignoreAllUncaughtExceptions(true); |
michael@0 | 118 | |
michael@0 | 119 | // load about:home, but remove the listener first so it doesn't |
michael@0 | 120 | // get in the way |
michael@0 | 121 | gBrowser.removeProgressListener(listener); |
michael@0 | 122 | gBrowser.loadURI("about:home"); |
michael@0 | 123 | info("Waiting for about:home load"); |
michael@0 | 124 | tab.linkedBrowser.addEventListener("load", function load(event) { |
michael@0 | 125 | if (event.originalTarget != tab.linkedBrowser.contentDocument || |
michael@0 | 126 | event.target.location.href == "about:blank") { |
michael@0 | 127 | info("skipping spurious load event"); |
michael@0 | 128 | return; |
michael@0 | 129 | } |
michael@0 | 130 | tab.linkedBrowser.removeEventListener("load", load, true); |
michael@0 | 131 | |
michael@0 | 132 | // Observe page setup |
michael@0 | 133 | let doc = gBrowser.contentDocument; |
michael@0 | 134 | let mutationObserver = new MutationObserver(function (mutations) { |
michael@0 | 135 | for (let mutation of mutations) { |
michael@0 | 136 | if (mutation.attributeName == "searchEngineName") { |
michael@0 | 137 | // Re-add the listener, and perform a search |
michael@0 | 138 | gBrowser.addProgressListener(listener); |
michael@0 | 139 | doc.getElementById("searchText").value = "foo"; |
michael@0 | 140 | doc.getElementById("searchSubmit").click(); |
michael@0 | 141 | } |
michael@0 | 142 | } |
michael@0 | 143 | }); |
michael@0 | 144 | mutationObserver.observe(doc.documentElement, { attributes: true }); |
michael@0 | 145 | }, true); |
michael@0 | 146 | } |
michael@0 | 147 | } |
michael@0 | 148 | ]; |
michael@0 | 149 | |
michael@0 | 150 | function nextTest() { |
michael@0 | 151 | // Make sure we listen again for uncaught exceptions in the next test or cleanup. |
michael@0 | 152 | ignoreAllUncaughtExceptions(false); |
michael@0 | 153 | |
michael@0 | 154 | if (gTests.length) { |
michael@0 | 155 | gCurrTest = gTests.shift(); |
michael@0 | 156 | info("Running : " + gCurrTest.name); |
michael@0 | 157 | executeSoon(gCurrTest.run); |
michael@0 | 158 | } else { |
michael@0 | 159 | finish(); |
michael@0 | 160 | } |
michael@0 | 161 | } |
michael@0 | 162 | |
michael@0 | 163 | let tab = gBrowser.selectedTab = gBrowser.addTab(); |
michael@0 | 164 | |
michael@0 | 165 | let listener = { |
michael@0 | 166 | onStateChange: function onStateChange(webProgress, req, flags, status) { |
michael@0 | 167 | info("onStateChange"); |
michael@0 | 168 | // Only care about top-level document starts |
michael@0 | 169 | let docStart = Ci.nsIWebProgressListener.STATE_IS_DOCUMENT | |
michael@0 | 170 | Ci.nsIWebProgressListener.STATE_START; |
michael@0 | 171 | if (!(flags & docStart) || !webProgress.isTopLevel) |
michael@0 | 172 | return; |
michael@0 | 173 | |
michael@0 | 174 | info("received document start"); |
michael@0 | 175 | |
michael@0 | 176 | ok(req instanceof Ci.nsIChannel, "req is a channel"); |
michael@0 | 177 | is(req.originalURI.spec, gCurrTest.searchURL, "search URL was loaded"); |
michael@0 | 178 | info("Actual URI: " + req.URI.spec); |
michael@0 | 179 | |
michael@0 | 180 | req.cancel(Components.results.NS_ERROR_FAILURE); |
michael@0 | 181 | |
michael@0 | 182 | executeSoon(nextTest); |
michael@0 | 183 | } |
michael@0 | 184 | } |
michael@0 | 185 | |
michael@0 | 186 | registerCleanupFunction(function () { |
michael@0 | 187 | gBrowser.removeProgressListener(listener); |
michael@0 | 188 | gBrowser.removeTab(tab); |
michael@0 | 189 | Services.search.currentEngine = previouslySelectedEngine; |
michael@0 | 190 | }); |
michael@0 | 191 | |
michael@0 | 192 | tab.linkedBrowser.addEventListener("load", function load() { |
michael@0 | 193 | tab.linkedBrowser.removeEventListener("load", load, true); |
michael@0 | 194 | gBrowser.addProgressListener(listener); |
michael@0 | 195 | nextTest(); |
michael@0 | 196 | }, true); |
michael@0 | 197 | } |