Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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 Google 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 | const MOZ_PARAM_LOCALE = /\{moz:locale\}/g; |
michael@0 | 13 | const MOZ_PARAM_DIST_ID = /\{moz:distributionID\}/g; |
michael@0 | 14 | const MOZ_PARAM_OFFICIAL = /\{moz:official\}/g; |
michael@0 | 15 | |
michael@0 | 16 | let runtime = Cc["@mozilla.org/xre/app-info;1"].getService(Ci.nsIXULRuntime); |
michael@0 | 17 | // Custom search parameters |
michael@0 | 18 | const MOZ_OFFICIAL = runtime.isOfficialBranding ? "official" : "unofficial"; |
michael@0 | 19 | |
michael@0 | 20 | var google_client; |
michael@0 | 21 | switch (runtime.defaultUpdateChannel) { |
michael@0 | 22 | case "beta": |
michael@0 | 23 | google_client = "firefox-beta"; |
michael@0 | 24 | break; |
michael@0 | 25 | case "aurora": |
michael@0 | 26 | google_client = "firefox-aurora"; |
michael@0 | 27 | break; |
michael@0 | 28 | case "nightly": |
michael@0 | 29 | google_client = "firefox-nightly"; |
michael@0 | 30 | break; |
michael@0 | 31 | default: |
michael@0 | 32 | google_client = "firefox-a"; |
michael@0 | 33 | break; |
michael@0 | 34 | } |
michael@0 | 35 | |
michael@0 | 36 | const GOOGLE_CLIENT = google_client; |
michael@0 | 37 | const MOZ_DISTRIBUTION_ID = runtime.distributionID; |
michael@0 | 38 | |
michael@0 | 39 | function test() { |
michael@0 | 40 | let engine = Services.search.getEngineByName("Google"); |
michael@0 | 41 | ok(engine, "Google is installed"); |
michael@0 | 42 | |
michael@0 | 43 | is(Services.search.defaultEngine, engine, "Check that Google is the default search engine"); |
michael@0 | 44 | |
michael@0 | 45 | let distributionID; |
michael@0 | 46 | try { |
michael@0 | 47 | distributionID = Services.prefs.getCharPref(BROWSER_SEARCH_PREF + "distributionID"); |
michael@0 | 48 | } catch (ex) { |
michael@0 | 49 | distributionID = MOZ_DISTRIBUTION_ID; |
michael@0 | 50 | } |
michael@0 | 51 | |
michael@0 | 52 | let base = "https://www.google.com/search?q=foo&ie=utf-8&oe=utf-8&aq=t&rls={moz:distributionID}:{moz:locale}:{moz:official}&client=" + GOOGLE_CLIENT; |
michael@0 | 53 | base = base.replace(MOZ_PARAM_LOCALE, getLocale()); |
michael@0 | 54 | base = base.replace(MOZ_PARAM_DIST_ID, distributionID); |
michael@0 | 55 | base = base.replace(MOZ_PARAM_OFFICIAL, MOZ_OFFICIAL); |
michael@0 | 56 | |
michael@0 | 57 | let url; |
michael@0 | 58 | |
michael@0 | 59 | // Test search URLs (including purposes). |
michael@0 | 60 | url = engine.getSubmission("foo").uri.spec; |
michael@0 | 61 | is(url, base, "Check search URL for 'foo'"); |
michael@0 | 62 | |
michael@0 | 63 | waitForExplicitFinish(); |
michael@0 | 64 | |
michael@0 | 65 | var gCurrTest; |
michael@0 | 66 | var gTests = [ |
michael@0 | 67 | { |
michael@0 | 68 | name: "context menu search", |
michael@0 | 69 | searchURL: base + "&channel=rcs", |
michael@0 | 70 | run: function () { |
michael@0 | 71 | // Simulate a contextmenu search |
michael@0 | 72 | // FIXME: This is a bit "low-level"... |
michael@0 | 73 | BrowserSearch.loadSearch("foo", false, "contextmenu"); |
michael@0 | 74 | } |
michael@0 | 75 | }, |
michael@0 | 76 | { |
michael@0 | 77 | name: "keyword search", |
michael@0 | 78 | searchURL: base + "&channel=fflb", |
michael@0 | 79 | run: function () { |
michael@0 | 80 | gURLBar.value = "? foo"; |
michael@0 | 81 | gURLBar.focus(); |
michael@0 | 82 | EventUtils.synthesizeKey("VK_RETURN", {}); |
michael@0 | 83 | } |
michael@0 | 84 | }, |
michael@0 | 85 | { |
michael@0 | 86 | name: "search bar search", |
michael@0 | 87 | searchURL: base + "&channel=sb", |
michael@0 | 88 | run: function () { |
michael@0 | 89 | let sb = BrowserSearch.searchBar; |
michael@0 | 90 | sb.focus(); |
michael@0 | 91 | sb.value = "foo"; |
michael@0 | 92 | registerCleanupFunction(function () { |
michael@0 | 93 | sb.value = ""; |
michael@0 | 94 | }); |
michael@0 | 95 | EventUtils.synthesizeKey("VK_RETURN", {}); |
michael@0 | 96 | } |
michael@0 | 97 | }, |
michael@0 | 98 | { |
michael@0 | 99 | name: "new tab search", |
michael@0 | 100 | searchURL: base + "&channel=nts", |
michael@0 | 101 | run: function () { |
michael@0 | 102 | function doSearch(doc) { |
michael@0 | 103 | // Re-add the listener, and perform a search |
michael@0 | 104 | gBrowser.addProgressListener(listener); |
michael@0 | 105 | doc.getElementById("newtab-search-text").value = "foo"; |
michael@0 | 106 | doc.getElementById("newtab-search-submit").click(); |
michael@0 | 107 | } |
michael@0 | 108 | |
michael@0 | 109 | // load about:newtab, but remove the listener first so it doesn't |
michael@0 | 110 | // get in the way |
michael@0 | 111 | gBrowser.removeProgressListener(listener); |
michael@0 | 112 | gBrowser.loadURI("about:newtab"); |
michael@0 | 113 | info("Waiting for about:newtab load"); |
michael@0 | 114 | tab.linkedBrowser.addEventListener("load", function load(event) { |
michael@0 | 115 | if (event.originalTarget != tab.linkedBrowser.contentDocument || |
michael@0 | 116 | event.target.location.href == "about:blank") { |
michael@0 | 117 | info("skipping spurious load event"); |
michael@0 | 118 | return; |
michael@0 | 119 | } |
michael@0 | 120 | tab.linkedBrowser.removeEventListener("load", load, true); |
michael@0 | 121 | |
michael@0 | 122 | // Observe page setup |
michael@0 | 123 | let win = gBrowser.contentWindow; |
michael@0 | 124 | if (win.gSearch.currentEngineName == |
michael@0 | 125 | Services.search.currentEngine.name) { |
michael@0 | 126 | doSearch(win.document); |
michael@0 | 127 | } |
michael@0 | 128 | else { |
michael@0 | 129 | info("Waiting for newtab search init"); |
michael@0 | 130 | win.addEventListener("ContentSearchService", function done(event) { |
michael@0 | 131 | info("Got newtab search event " + event.detail.type); |
michael@0 | 132 | if (event.detail.type == "State") { |
michael@0 | 133 | win.removeEventListener("ContentSearchService", done); |
michael@0 | 134 | // Let gSearch respond to the event before continuing. |
michael@0 | 135 | executeSoon(() => doSearch(win.document)); |
michael@0 | 136 | } |
michael@0 | 137 | }); |
michael@0 | 138 | } |
michael@0 | 139 | }, true); |
michael@0 | 140 | } |
michael@0 | 141 | }, |
michael@0 | 142 | { |
michael@0 | 143 | name: "home page search", |
michael@0 | 144 | searchURL: base + "&channel=np&source=hp", |
michael@0 | 145 | run: function () { |
michael@0 | 146 | // Bug 992270: Ignore uncaught about:home exceptions (related to snippets from IndexedDB) |
michael@0 | 147 | ignoreAllUncaughtExceptions(true); |
michael@0 | 148 | |
michael@0 | 149 | // load about:home, but remove the listener first so it doesn't |
michael@0 | 150 | // get in the way |
michael@0 | 151 | gBrowser.removeProgressListener(listener); |
michael@0 | 152 | gBrowser.loadURI("about:home"); |
michael@0 | 153 | info("Waiting for about:home load"); |
michael@0 | 154 | tab.linkedBrowser.addEventListener("load", function load(event) { |
michael@0 | 155 | if (event.originalTarget != tab.linkedBrowser.contentDocument || |
michael@0 | 156 | event.target.location.href == "about:blank") { |
michael@0 | 157 | info("skipping spurious load event"); |
michael@0 | 158 | return; |
michael@0 | 159 | } |
michael@0 | 160 | tab.linkedBrowser.removeEventListener("load", load, true); |
michael@0 | 161 | |
michael@0 | 162 | // Observe page setup |
michael@0 | 163 | let doc = gBrowser.contentDocument; |
michael@0 | 164 | let mutationObserver = new MutationObserver(function (mutations) { |
michael@0 | 165 | for (let mutation of mutations) { |
michael@0 | 166 | if (mutation.attributeName == "searchEngineName") { |
michael@0 | 167 | // Re-add the listener, and perform a search |
michael@0 | 168 | gBrowser.addProgressListener(listener); |
michael@0 | 169 | doc.getElementById("searchText").value = "foo"; |
michael@0 | 170 | doc.getElementById("searchSubmit").click(); |
michael@0 | 171 | } |
michael@0 | 172 | } |
michael@0 | 173 | }); |
michael@0 | 174 | mutationObserver.observe(doc.documentElement, { attributes: true }); |
michael@0 | 175 | }, true); |
michael@0 | 176 | } |
michael@0 | 177 | } |
michael@0 | 178 | ]; |
michael@0 | 179 | |
michael@0 | 180 | function nextTest() { |
michael@0 | 181 | // Make sure we listen again for uncaught exceptions in the next test or cleanup. |
michael@0 | 182 | ignoreAllUncaughtExceptions(false); |
michael@0 | 183 | |
michael@0 | 184 | if (gTests.length) { |
michael@0 | 185 | gCurrTest = gTests.shift(); |
michael@0 | 186 | info("Running : " + gCurrTest.name); |
michael@0 | 187 | executeSoon(gCurrTest.run); |
michael@0 | 188 | } else { |
michael@0 | 189 | finish(); |
michael@0 | 190 | } |
michael@0 | 191 | } |
michael@0 | 192 | |
michael@0 | 193 | let tab = gBrowser.selectedTab = gBrowser.addTab(); |
michael@0 | 194 | |
michael@0 | 195 | let listener = { |
michael@0 | 196 | onStateChange: function onStateChange(webProgress, req, flags, status) { |
michael@0 | 197 | info("onStateChange"); |
michael@0 | 198 | // Only care about top-level document starts |
michael@0 | 199 | let docStart = Ci.nsIWebProgressListener.STATE_IS_DOCUMENT | |
michael@0 | 200 | Ci.nsIWebProgressListener.STATE_START; |
michael@0 | 201 | if (!(flags & docStart) || !webProgress.isTopLevel) |
michael@0 | 202 | return; |
michael@0 | 203 | |
michael@0 | 204 | info("received document start"); |
michael@0 | 205 | |
michael@0 | 206 | ok(req instanceof Ci.nsIChannel, "req is a channel"); |
michael@0 | 207 | is(req.originalURI.spec, gCurrTest.searchURL, "search URL was loaded"); |
michael@0 | 208 | info("Actual URI: " + req.URI.spec); |
michael@0 | 209 | |
michael@0 | 210 | req.cancel(Components.results.NS_ERROR_FAILURE); |
michael@0 | 211 | |
michael@0 | 212 | executeSoon(nextTest); |
michael@0 | 213 | } |
michael@0 | 214 | } |
michael@0 | 215 | |
michael@0 | 216 | registerCleanupFunction(function () { |
michael@0 | 217 | gBrowser.removeProgressListener(listener); |
michael@0 | 218 | gBrowser.removeTab(tab); |
michael@0 | 219 | }); |
michael@0 | 220 | |
michael@0 | 221 | tab.linkedBrowser.addEventListener("load", function load() { |
michael@0 | 222 | tab.linkedBrowser.removeEventListener("load", load, true); |
michael@0 | 223 | gBrowser.addProgressListener(listener); |
michael@0 | 224 | nextTest(); |
michael@0 | 225 | }, true); |
michael@0 | 226 | } |