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 | XPCOMUtils.defineLazyModuleGetter(this, "Promise", |
michael@0 | 6 | "resource://gre/modules/Promise.jsm"); |
michael@0 | 7 | XPCOMUtils.defineLazyModuleGetter(this, "Task", |
michael@0 | 8 | "resource://gre/modules/Task.jsm"); |
michael@0 | 9 | XPCOMUtils.defineLazyModuleGetter(this, "AboutHomeUtils", |
michael@0 | 10 | "resource:///modules/AboutHome.jsm"); |
michael@0 | 11 | |
michael@0 | 12 | let gRightsVersion = Services.prefs.getIntPref("browser.rights.version"); |
michael@0 | 13 | |
michael@0 | 14 | registerCleanupFunction(function() { |
michael@0 | 15 | // Ensure we don't pollute prefs for next tests. |
michael@0 | 16 | Services.prefs.clearUserPref("network.cookies.cookieBehavior"); |
michael@0 | 17 | Services.prefs.clearUserPref("network.cookie.lifetimePolicy"); |
michael@0 | 18 | Services.prefs.clearUserPref("browser.rights.override"); |
michael@0 | 19 | Services.prefs.clearUserPref("browser.rights." + gRightsVersion + ".shown"); |
michael@0 | 20 | }); |
michael@0 | 21 | |
michael@0 | 22 | let gTests = [ |
michael@0 | 23 | |
michael@0 | 24 | { |
michael@0 | 25 | desc: "Check that clearing cookies does not clear storage", |
michael@0 | 26 | setup: function () |
michael@0 | 27 | { |
michael@0 | 28 | Cc["@mozilla.org/observer-service;1"] |
michael@0 | 29 | .getService(Ci.nsIObserverService) |
michael@0 | 30 | .notifyObservers(null, "cookie-changed", "cleared"); |
michael@0 | 31 | }, |
michael@0 | 32 | run: function (aSnippetsMap) |
michael@0 | 33 | { |
michael@0 | 34 | isnot(aSnippetsMap.get("snippets-last-update"), null, |
michael@0 | 35 | "snippets-last-update should have a value"); |
michael@0 | 36 | } |
michael@0 | 37 | }, |
michael@0 | 38 | |
michael@0 | 39 | { |
michael@0 | 40 | desc: "Check default snippets are shown", |
michael@0 | 41 | setup: function () { }, |
michael@0 | 42 | run: function () |
michael@0 | 43 | { |
michael@0 | 44 | let doc = gBrowser.selectedTab.linkedBrowser.contentDocument; |
michael@0 | 45 | let snippetsElt = doc.getElementById("snippets"); |
michael@0 | 46 | ok(snippetsElt, "Found snippets element") |
michael@0 | 47 | is(snippetsElt.getElementsByTagName("span").length, 1, |
michael@0 | 48 | "A default snippet is present."); |
michael@0 | 49 | } |
michael@0 | 50 | }, |
michael@0 | 51 | |
michael@0 | 52 | { |
michael@0 | 53 | desc: "Check default snippets are shown if snippets are invalid xml", |
michael@0 | 54 | setup: function (aSnippetsMap) |
michael@0 | 55 | { |
michael@0 | 56 | // This must be some incorrect xhtml code. |
michael@0 | 57 | aSnippetsMap.set("snippets", "<p><b></p></b>"); |
michael@0 | 58 | }, |
michael@0 | 59 | run: function (aSnippetsMap) |
michael@0 | 60 | { |
michael@0 | 61 | let doc = gBrowser.selectedTab.linkedBrowser.contentDocument; |
michael@0 | 62 | |
michael@0 | 63 | let snippetsElt = doc.getElementById("snippets"); |
michael@0 | 64 | ok(snippetsElt, "Found snippets element"); |
michael@0 | 65 | is(snippetsElt.getElementsByTagName("span").length, 1, |
michael@0 | 66 | "A default snippet is present."); |
michael@0 | 67 | |
michael@0 | 68 | aSnippetsMap.delete("snippets"); |
michael@0 | 69 | } |
michael@0 | 70 | }, |
michael@0 | 71 | |
michael@0 | 72 | { |
michael@0 | 73 | desc: "Check that search engine logo has alt text", |
michael@0 | 74 | setup: function () { }, |
michael@0 | 75 | run: function () |
michael@0 | 76 | { |
michael@0 | 77 | let doc = gBrowser.selectedTab.linkedBrowser.contentDocument; |
michael@0 | 78 | |
michael@0 | 79 | let searchEngineLogoElt = doc.getElementById("searchEngineLogo"); |
michael@0 | 80 | ok(searchEngineLogoElt, "Found search engine logo"); |
michael@0 | 81 | |
michael@0 | 82 | let altText = searchEngineLogoElt.alt; |
michael@0 | 83 | ok(typeof altText == "string" && altText.length > 0, |
michael@0 | 84 | "Search engine logo's alt text is a nonempty string"); |
michael@0 | 85 | |
michael@0 | 86 | isnot(altText, "undefined", |
michael@0 | 87 | "Search engine logo's alt text shouldn't be the string 'undefined'"); |
michael@0 | 88 | } |
michael@0 | 89 | }, |
michael@0 | 90 | |
michael@0 | 91 | // Disabled on Linux for intermittent issues with FHR, see Bug 945667. |
michael@0 | 92 | { |
michael@0 | 93 | desc: "Check that performing a search fires a search event and records to " + |
michael@0 | 94 | "Firefox Health Report.", |
michael@0 | 95 | setup: function () { }, |
michael@0 | 96 | run: function () { |
michael@0 | 97 | // Skip this test on Linux. |
michael@0 | 98 | if (navigator.platform.indexOf("Linux") == 0) { return; } |
michael@0 | 99 | |
michael@0 | 100 | try { |
michael@0 | 101 | let cm = Cc["@mozilla.org/categorymanager;1"].getService(Ci.nsICategoryManager); |
michael@0 | 102 | cm.getCategoryEntry("healthreport-js-provider-default", "SearchesProvider"); |
michael@0 | 103 | } catch (ex) { |
michael@0 | 104 | // Health Report disabled, or no SearchesProvider. |
michael@0 | 105 | return Promise.resolve(); |
michael@0 | 106 | } |
michael@0 | 107 | |
michael@0 | 108 | let numSearchesBefore = 0; |
michael@0 | 109 | let searchEventDeferred = Promise.defer(); |
michael@0 | 110 | let doc = gBrowser.contentDocument; |
michael@0 | 111 | let engineName = doc.documentElement.getAttribute("searchEngineName"); |
michael@0 | 112 | |
michael@0 | 113 | doc.addEventListener("AboutHomeSearchEvent", function onSearch(e) { |
michael@0 | 114 | let data = JSON.parse(e.detail); |
michael@0 | 115 | is(data.engineName, engineName, "Detail is search engine name"); |
michael@0 | 116 | |
michael@0 | 117 | // We use executeSoon() to ensure that this code runs after the |
michael@0 | 118 | // count has been updated in browser.js, since it uses the same |
michael@0 | 119 | // event. |
michael@0 | 120 | executeSoon(function () { |
michael@0 | 121 | getNumberOfSearches(engineName).then(num => { |
michael@0 | 122 | is(num, numSearchesBefore + 1, "One more search recorded."); |
michael@0 | 123 | searchEventDeferred.resolve(); |
michael@0 | 124 | }); |
michael@0 | 125 | }); |
michael@0 | 126 | }, true, true); |
michael@0 | 127 | |
michael@0 | 128 | // Get the current number of recorded searches. |
michael@0 | 129 | let searchStr = "a search"; |
michael@0 | 130 | getNumberOfSearches(engineName).then(num => { |
michael@0 | 131 | numSearchesBefore = num; |
michael@0 | 132 | |
michael@0 | 133 | info("Perform a search."); |
michael@0 | 134 | doc.getElementById("searchText").value = searchStr; |
michael@0 | 135 | doc.getElementById("searchSubmit").click(); |
michael@0 | 136 | }); |
michael@0 | 137 | |
michael@0 | 138 | let expectedURL = Services.search.currentEngine. |
michael@0 | 139 | getSubmission(searchStr, null, "homepage"). |
michael@0 | 140 | uri.spec; |
michael@0 | 141 | let loadPromise = waitForDocLoadAndStopIt(expectedURL); |
michael@0 | 142 | |
michael@0 | 143 | return Promise.all([searchEventDeferred.promise, loadPromise]); |
michael@0 | 144 | } |
michael@0 | 145 | }, |
michael@0 | 146 | |
michael@0 | 147 | { |
michael@0 | 148 | desc: "Check snippets map is cleared if cached version is old", |
michael@0 | 149 | setup: function (aSnippetsMap) |
michael@0 | 150 | { |
michael@0 | 151 | aSnippetsMap.set("snippets", "test"); |
michael@0 | 152 | aSnippetsMap.set("snippets-cached-version", 0); |
michael@0 | 153 | }, |
michael@0 | 154 | run: function (aSnippetsMap) |
michael@0 | 155 | { |
michael@0 | 156 | ok(!aSnippetsMap.has("snippets"), "snippets have been properly cleared"); |
michael@0 | 157 | ok(!aSnippetsMap.has("snippets-cached-version"), |
michael@0 | 158 | "cached-version has been properly cleared"); |
michael@0 | 159 | } |
michael@0 | 160 | }, |
michael@0 | 161 | |
michael@0 | 162 | { |
michael@0 | 163 | desc: "Check cached snippets are shown if cached version is current", |
michael@0 | 164 | setup: function (aSnippetsMap) |
michael@0 | 165 | { |
michael@0 | 166 | aSnippetsMap.set("snippets", "test"); |
michael@0 | 167 | }, |
michael@0 | 168 | run: function (aSnippetsMap) |
michael@0 | 169 | { |
michael@0 | 170 | let doc = gBrowser.selectedTab.linkedBrowser.contentDocument; |
michael@0 | 171 | |
michael@0 | 172 | let snippetsElt = doc.getElementById("snippets"); |
michael@0 | 173 | ok(snippetsElt, "Found snippets element"); |
michael@0 | 174 | is(snippetsElt.innerHTML, "test", "Cached snippet is present."); |
michael@0 | 175 | |
michael@0 | 176 | is(aSnippetsMap.get("snippets"), "test", "snippets still cached"); |
michael@0 | 177 | is(aSnippetsMap.get("snippets-cached-version"), |
michael@0 | 178 | AboutHomeUtils.snippetsVersion, |
michael@0 | 179 | "cached-version is correct"); |
michael@0 | 180 | ok(aSnippetsMap.has("snippets-last-update"), "last-update still exists"); |
michael@0 | 181 | } |
michael@0 | 182 | }, |
michael@0 | 183 | |
michael@0 | 184 | { |
michael@0 | 185 | desc: "Check if the 'Know Your Rights default snippet is shown when 'browser.rights.override' pref is set", |
michael@0 | 186 | beforeRun: function () |
michael@0 | 187 | { |
michael@0 | 188 | Services.prefs.setBoolPref("browser.rights.override", false); |
michael@0 | 189 | }, |
michael@0 | 190 | setup: function () { }, |
michael@0 | 191 | run: function (aSnippetsMap) |
michael@0 | 192 | { |
michael@0 | 193 | let doc = gBrowser.selectedTab.linkedBrowser.contentDocument; |
michael@0 | 194 | let showRights = AboutHomeUtils.showKnowYourRights; |
michael@0 | 195 | |
michael@0 | 196 | ok(showRights, "AboutHomeUtils.showKnowYourRights should be TRUE"); |
michael@0 | 197 | |
michael@0 | 198 | let snippetsElt = doc.getElementById("snippets"); |
michael@0 | 199 | ok(snippetsElt, "Found snippets element"); |
michael@0 | 200 | is(snippetsElt.getElementsByTagName("a")[0].href, "about:rights", "Snippet link is present."); |
michael@0 | 201 | |
michael@0 | 202 | Services.prefs.clearUserPref("browser.rights.override"); |
michael@0 | 203 | } |
michael@0 | 204 | }, |
michael@0 | 205 | |
michael@0 | 206 | { |
michael@0 | 207 | desc: "Check if the 'Know Your Rights default snippet is NOT shown when 'browser.rights.override' pref is NOT set", |
michael@0 | 208 | beforeRun: function () |
michael@0 | 209 | { |
michael@0 | 210 | Services.prefs.setBoolPref("browser.rights.override", true); |
michael@0 | 211 | }, |
michael@0 | 212 | setup: function () { }, |
michael@0 | 213 | run: function (aSnippetsMap) |
michael@0 | 214 | { |
michael@0 | 215 | let doc = gBrowser.selectedTab.linkedBrowser.contentDocument; |
michael@0 | 216 | let rightsData = AboutHomeUtils.knowYourRightsData; |
michael@0 | 217 | |
michael@0 | 218 | ok(!rightsData, "AboutHomeUtils.knowYourRightsData should be FALSE"); |
michael@0 | 219 | |
michael@0 | 220 | let snippetsElt = doc.getElementById("snippets"); |
michael@0 | 221 | ok(snippetsElt, "Found snippets element"); |
michael@0 | 222 | ok(snippetsElt.getElementsByTagName("a")[0].href != "about:rights", "Snippet link should not point to about:rights."); |
michael@0 | 223 | |
michael@0 | 224 | Services.prefs.clearUserPref("browser.rights.override"); |
michael@0 | 225 | } |
michael@0 | 226 | }, |
michael@0 | 227 | |
michael@0 | 228 | { |
michael@0 | 229 | desc: "Check that the search UI/ action is updated when the search engine is changed", |
michael@0 | 230 | setup: function() {}, |
michael@0 | 231 | run: function() |
michael@0 | 232 | { |
michael@0 | 233 | let currEngine = Services.search.currentEngine; |
michael@0 | 234 | let unusedEngines = [].concat(Services.search.getVisibleEngines()).filter(x => x != currEngine); |
michael@0 | 235 | let searchbar = document.getElementById("searchbar"); |
michael@0 | 236 | |
michael@0 | 237 | function checkSearchUI(engine) { |
michael@0 | 238 | let doc = gBrowser.selectedTab.linkedBrowser.contentDocument; |
michael@0 | 239 | let searchText = doc.getElementById("searchText"); |
michael@0 | 240 | let logoElt = doc.getElementById("searchEngineLogo"); |
michael@0 | 241 | let engineName = doc.documentElement.getAttribute("searchEngineName"); |
michael@0 | 242 | |
michael@0 | 243 | is(engineName, engine.name, "Engine name should've been updated"); |
michael@0 | 244 | |
michael@0 | 245 | if (!logoElt.parentNode.hidden) { |
michael@0 | 246 | is(logoElt.alt, engineName, "Alt text of logo image should match search engine name") |
michael@0 | 247 | } else { |
michael@0 | 248 | is(searchText.placeholder, engineName, "Placeholder text should match search engine name"); |
michael@0 | 249 | } |
michael@0 | 250 | } |
michael@0 | 251 | // Do a sanity check that all attributes are correctly set to begin with |
michael@0 | 252 | checkSearchUI(currEngine); |
michael@0 | 253 | |
michael@0 | 254 | let deferred = Promise.defer(); |
michael@0 | 255 | promiseBrowserAttributes(gBrowser.selectedTab).then(function() { |
michael@0 | 256 | // Test if the update propagated |
michael@0 | 257 | checkSearchUI(unusedEngines[0]); |
michael@0 | 258 | searchbar.currentEngine = currEngine; |
michael@0 | 259 | deferred.resolve(); |
michael@0 | 260 | }); |
michael@0 | 261 | |
michael@0 | 262 | // The following cleanup function will set currentEngine back to the previous |
michael@0 | 263 | // engine if we fail to do so above. |
michael@0 | 264 | registerCleanupFunction(function() { |
michael@0 | 265 | searchbar.currentEngine = currEngine; |
michael@0 | 266 | }); |
michael@0 | 267 | // Set the current search engine to an unused one |
michael@0 | 268 | searchbar.currentEngine = unusedEngines[0]; |
michael@0 | 269 | searchbar.select(); |
michael@0 | 270 | return deferred.promise; |
michael@0 | 271 | } |
michael@0 | 272 | }, |
michael@0 | 273 | |
michael@0 | 274 | { |
michael@0 | 275 | desc: "Check POST search engine support", |
michael@0 | 276 | setup: function() {}, |
michael@0 | 277 | run: function() |
michael@0 | 278 | { |
michael@0 | 279 | let deferred = Promise.defer(); |
michael@0 | 280 | let currEngine = Services.search.defaultEngine; |
michael@0 | 281 | let searchObserver = function search_observer(aSubject, aTopic, aData) { |
michael@0 | 282 | let engine = aSubject.QueryInterface(Ci.nsISearchEngine); |
michael@0 | 283 | info("Observer: " + aData + " for " + engine.name); |
michael@0 | 284 | |
michael@0 | 285 | if (aData != "engine-added") |
michael@0 | 286 | return; |
michael@0 | 287 | |
michael@0 | 288 | if (engine.name != "POST Search") |
michael@0 | 289 | return; |
michael@0 | 290 | |
michael@0 | 291 | // Ready to execute the tests! |
michael@0 | 292 | let needle = "Search for something awesome."; |
michael@0 | 293 | let document = gBrowser.selectedTab.linkedBrowser.contentDocument; |
michael@0 | 294 | let searchText = document.getElementById("searchText"); |
michael@0 | 295 | |
michael@0 | 296 | // We're about to change the search engine. Once the change has |
michael@0 | 297 | // propagated to the about:home content, we want to perform a search. |
michael@0 | 298 | let mutationObserver = new MutationObserver(function (mutations) { |
michael@0 | 299 | for (let mutation of mutations) { |
michael@0 | 300 | if (mutation.attributeName == "searchEngineName") { |
michael@0 | 301 | searchText.value = needle; |
michael@0 | 302 | searchText.focus(); |
michael@0 | 303 | EventUtils.synthesizeKey("VK_RETURN", {}); |
michael@0 | 304 | } |
michael@0 | 305 | } |
michael@0 | 306 | }); |
michael@0 | 307 | mutationObserver.observe(document.documentElement, { attributes: true }); |
michael@0 | 308 | |
michael@0 | 309 | // Change the search engine, triggering the observer above. |
michael@0 | 310 | Services.search.defaultEngine = engine; |
michael@0 | 311 | |
michael@0 | 312 | registerCleanupFunction(function() { |
michael@0 | 313 | mutationObserver.disconnect(); |
michael@0 | 314 | Services.search.removeEngine(engine); |
michael@0 | 315 | Services.search.defaultEngine = currEngine; |
michael@0 | 316 | }); |
michael@0 | 317 | |
michael@0 | 318 | |
michael@0 | 319 | // When the search results load, check them for correctness. |
michael@0 | 320 | waitForLoad(function() { |
michael@0 | 321 | let loadedText = gBrowser.contentDocument.body.textContent; |
michael@0 | 322 | ok(loadedText, "search page loaded"); |
michael@0 | 323 | is(loadedText, "searchterms=" + escape(needle.replace(/\s/g, "+")), |
michael@0 | 324 | "Search text should arrive correctly"); |
michael@0 | 325 | deferred.resolve(); |
michael@0 | 326 | }); |
michael@0 | 327 | }; |
michael@0 | 328 | Services.obs.addObserver(searchObserver, "browser-search-engine-modified", false); |
michael@0 | 329 | registerCleanupFunction(function () { |
michael@0 | 330 | Services.obs.removeObserver(searchObserver, "browser-search-engine-modified"); |
michael@0 | 331 | }); |
michael@0 | 332 | Services.search.addEngine("http://test:80/browser/browser/base/content/test/general/POSTSearchEngine.xml", |
michael@0 | 333 | Ci.nsISearchEngine.DATA_XML, null, false); |
michael@0 | 334 | return deferred.promise; |
michael@0 | 335 | } |
michael@0 | 336 | }, |
michael@0 | 337 | |
michael@0 | 338 | { |
michael@0 | 339 | desc: "Make sure that a page can't imitate about:home", |
michael@0 | 340 | setup: function () { }, |
michael@0 | 341 | run: function (aSnippetsMap) |
michael@0 | 342 | { |
michael@0 | 343 | let deferred = Promise.defer(); |
michael@0 | 344 | |
michael@0 | 345 | let browser = gBrowser.selectedTab.linkedBrowser; |
michael@0 | 346 | waitForLoad(() => { |
michael@0 | 347 | let button = browser.contentDocument.getElementById("settings"); |
michael@0 | 348 | ok(button, "Found settings button in test page"); |
michael@0 | 349 | button.click(); |
michael@0 | 350 | |
michael@0 | 351 | // It may take a few turns of the event loop before the window |
michael@0 | 352 | // is displayed, so we wait. |
michael@0 | 353 | function check(n) { |
michael@0 | 354 | let win = Services.wm.getMostRecentWindow("Browser:Preferences"); |
michael@0 | 355 | ok(!win, "Preferences window not showing"); |
michael@0 | 356 | if (win) { |
michael@0 | 357 | win.close(); |
michael@0 | 358 | } |
michael@0 | 359 | |
michael@0 | 360 | if (n > 0) { |
michael@0 | 361 | executeSoon(() => check(n-1)); |
michael@0 | 362 | } else { |
michael@0 | 363 | deferred.resolve(); |
michael@0 | 364 | } |
michael@0 | 365 | } |
michael@0 | 366 | |
michael@0 | 367 | check(5); |
michael@0 | 368 | }); |
michael@0 | 369 | |
michael@0 | 370 | browser.loadURI("https://example.com/browser/browser/base/content/test/general/test_bug959531.html"); |
michael@0 | 371 | return deferred.promise; |
michael@0 | 372 | } |
michael@0 | 373 | }, |
michael@0 | 374 | |
michael@0 | 375 | ]; |
michael@0 | 376 | |
michael@0 | 377 | function test() |
michael@0 | 378 | { |
michael@0 | 379 | waitForExplicitFinish(); |
michael@0 | 380 | requestLongerTimeout(2); |
michael@0 | 381 | ignoreAllUncaughtExceptions(); |
michael@0 | 382 | |
michael@0 | 383 | Task.spawn(function () { |
michael@0 | 384 | for (let test of gTests) { |
michael@0 | 385 | info(test.desc); |
michael@0 | 386 | |
michael@0 | 387 | if (test.beforeRun) |
michael@0 | 388 | yield test.beforeRun(); |
michael@0 | 389 | |
michael@0 | 390 | // Create a tab to run the test. |
michael@0 | 391 | let tab = gBrowser.selectedTab = gBrowser.addTab("about:blank"); |
michael@0 | 392 | |
michael@0 | 393 | // Add an event handler to modify the snippets map once it's ready. |
michael@0 | 394 | let snippetsPromise = promiseSetupSnippetsMap(tab, test.setup); |
michael@0 | 395 | |
michael@0 | 396 | // Start loading about:home and wait for it to complete. |
michael@0 | 397 | yield promiseTabLoadEvent(tab, "about:home", "AboutHomeLoadSnippetsSucceeded"); |
michael@0 | 398 | |
michael@0 | 399 | // This promise should already be resolved since the page is done, |
michael@0 | 400 | // but we still want to get the snippets map out of it. |
michael@0 | 401 | let snippetsMap = yield snippetsPromise; |
michael@0 | 402 | |
michael@0 | 403 | info("Running test"); |
michael@0 | 404 | yield test.run(snippetsMap); |
michael@0 | 405 | info("Cleanup"); |
michael@0 | 406 | gBrowser.removeCurrentTab(); |
michael@0 | 407 | } |
michael@0 | 408 | }).then(finish, ex => { |
michael@0 | 409 | ok(false, "Unexpected Exception: " + ex); |
michael@0 | 410 | finish(); |
michael@0 | 411 | }); |
michael@0 | 412 | } |
michael@0 | 413 | |
michael@0 | 414 | /** |
michael@0 | 415 | * Starts a load in an existing tab and waits for it to finish (via some event). |
michael@0 | 416 | * |
michael@0 | 417 | * @param aTab |
michael@0 | 418 | * The tab to load into. |
michael@0 | 419 | * @param aUrl |
michael@0 | 420 | * The url to load. |
michael@0 | 421 | * @param aEvent |
michael@0 | 422 | * The load event type to wait for. Defaults to "load". |
michael@0 | 423 | * @return {Promise} resolved when the event is handled. |
michael@0 | 424 | */ |
michael@0 | 425 | function promiseTabLoadEvent(aTab, aURL, aEventType="load") |
michael@0 | 426 | { |
michael@0 | 427 | let deferred = Promise.defer(); |
michael@0 | 428 | info("Wait tab event: " + aEventType); |
michael@0 | 429 | aTab.linkedBrowser.addEventListener(aEventType, function load(event) { |
michael@0 | 430 | if (event.originalTarget != aTab.linkedBrowser.contentDocument || |
michael@0 | 431 | event.target.location.href == "about:blank") { |
michael@0 | 432 | info("skipping spurious load event"); |
michael@0 | 433 | return; |
michael@0 | 434 | } |
michael@0 | 435 | aTab.linkedBrowser.removeEventListener(aEventType, load, true); |
michael@0 | 436 | info("Tab event received: " + aEventType); |
michael@0 | 437 | deferred.resolve(); |
michael@0 | 438 | }, true, true); |
michael@0 | 439 | aTab.linkedBrowser.loadURI(aURL); |
michael@0 | 440 | return deferred.promise; |
michael@0 | 441 | } |
michael@0 | 442 | |
michael@0 | 443 | /** |
michael@0 | 444 | * Cleans up snippets and ensures that by default we don't try to check for |
michael@0 | 445 | * remote snippets since that may cause network bustage or slowness. |
michael@0 | 446 | * |
michael@0 | 447 | * @param aTab |
michael@0 | 448 | * The tab containing about:home. |
michael@0 | 449 | * @param aSetupFn |
michael@0 | 450 | * The setup function to be run. |
michael@0 | 451 | * @return {Promise} resolved when the snippets are ready. Gets the snippets map. |
michael@0 | 452 | */ |
michael@0 | 453 | function promiseSetupSnippetsMap(aTab, aSetupFn) |
michael@0 | 454 | { |
michael@0 | 455 | let deferred = Promise.defer(); |
michael@0 | 456 | info("Waiting for snippets map"); |
michael@0 | 457 | aTab.linkedBrowser.addEventListener("AboutHomeLoadSnippets", function load(event) { |
michael@0 | 458 | aTab.linkedBrowser.removeEventListener("AboutHomeLoadSnippets", load, true); |
michael@0 | 459 | |
michael@0 | 460 | let cw = aTab.linkedBrowser.contentWindow.wrappedJSObject; |
michael@0 | 461 | // The snippets should already be ready by this point. Here we're |
michael@0 | 462 | // just obtaining a reference to the snippets map. |
michael@0 | 463 | cw.ensureSnippetsMapThen(function (aSnippetsMap) { |
michael@0 | 464 | info("Got snippets map: " + |
michael@0 | 465 | "{ last-update: " + aSnippetsMap.get("snippets-last-update") + |
michael@0 | 466 | ", cached-version: " + aSnippetsMap.get("snippets-cached-version") + |
michael@0 | 467 | " }"); |
michael@0 | 468 | // Don't try to update. |
michael@0 | 469 | aSnippetsMap.set("snippets-last-update", Date.now()); |
michael@0 | 470 | aSnippetsMap.set("snippets-cached-version", AboutHomeUtils.snippetsVersion); |
michael@0 | 471 | // Clear snippets. |
michael@0 | 472 | aSnippetsMap.delete("snippets"); |
michael@0 | 473 | aSetupFn(aSnippetsMap); |
michael@0 | 474 | deferred.resolve(aSnippetsMap); |
michael@0 | 475 | }); |
michael@0 | 476 | }, true, true); |
michael@0 | 477 | return deferred.promise; |
michael@0 | 478 | } |
michael@0 | 479 | |
michael@0 | 480 | /** |
michael@0 | 481 | * Waits for the attributes being set by browser.js. |
michael@0 | 482 | * |
michael@0 | 483 | * @param aTab |
michael@0 | 484 | * The tab containing about:home. |
michael@0 | 485 | * @return {Promise} resolved when the attributes are ready. |
michael@0 | 486 | */ |
michael@0 | 487 | function promiseBrowserAttributes(aTab) |
michael@0 | 488 | { |
michael@0 | 489 | let deferred = Promise.defer(); |
michael@0 | 490 | |
michael@0 | 491 | let docElt = aTab.linkedBrowser.contentDocument.documentElement; |
michael@0 | 492 | let observer = new MutationObserver(function (mutations) { |
michael@0 | 493 | for (let mutation of mutations) { |
michael@0 | 494 | info("Got attribute mutation: " + mutation.attributeName + |
michael@0 | 495 | " from " + mutation.oldValue); |
michael@0 | 496 | // Now we just have to wait for the last attribute. |
michael@0 | 497 | if (mutation.attributeName == "searchEngineName") { |
michael@0 | 498 | info("Remove attributes observer"); |
michael@0 | 499 | observer.disconnect(); |
michael@0 | 500 | // Must be sure to continue after the page mutation observer. |
michael@0 | 501 | executeSoon(function() deferred.resolve()); |
michael@0 | 502 | break; |
michael@0 | 503 | } |
michael@0 | 504 | } |
michael@0 | 505 | }); |
michael@0 | 506 | info("Add attributes observer"); |
michael@0 | 507 | observer.observe(docElt, { attributes: true }); |
michael@0 | 508 | |
michael@0 | 509 | return deferred.promise; |
michael@0 | 510 | } |
michael@0 | 511 | |
michael@0 | 512 | /** |
michael@0 | 513 | * Retrieves the number of about:home searches recorded for the current day. |
michael@0 | 514 | * |
michael@0 | 515 | * @param aEngineName |
michael@0 | 516 | * name of the setup search engine. |
michael@0 | 517 | * |
michael@0 | 518 | * @return {Promise} Returns a promise resolving to the number of searches. |
michael@0 | 519 | */ |
michael@0 | 520 | function getNumberOfSearches(aEngineName) { |
michael@0 | 521 | let reporter = Components.classes["@mozilla.org/datareporting/service;1"] |
michael@0 | 522 | .getService() |
michael@0 | 523 | .wrappedJSObject |
michael@0 | 524 | .healthReporter; |
michael@0 | 525 | ok(reporter, "Health Reporter instance available."); |
michael@0 | 526 | |
michael@0 | 527 | return reporter.onInit().then(function onInit() { |
michael@0 | 528 | let provider = reporter.getProvider("org.mozilla.searches"); |
michael@0 | 529 | ok(provider, "Searches provider is available."); |
michael@0 | 530 | |
michael@0 | 531 | let m = provider.getMeasurement("counts", 3); |
michael@0 | 532 | return m.getValues().then(data => { |
michael@0 | 533 | let now = new Date(); |
michael@0 | 534 | let yday = new Date(now); |
michael@0 | 535 | yday.setDate(yday.getDate() - 1); |
michael@0 | 536 | |
michael@0 | 537 | // Add the number of searches recorded yesterday to the number of searches |
michael@0 | 538 | // recorded today. This makes the test not fail intermittently when it is |
michael@0 | 539 | // run at midnight and we accidentally compare the number of searches from |
michael@0 | 540 | // different days. Tests are always run with an empty profile so there |
michael@0 | 541 | // are no searches from yesterday, normally. Should the test happen to run |
michael@0 | 542 | // past midnight we make sure to count them in as well. |
michael@0 | 543 | return getNumberOfSearchesByDate(aEngineName, data, now) + |
michael@0 | 544 | getNumberOfSearchesByDate(aEngineName, data, yday); |
michael@0 | 545 | }); |
michael@0 | 546 | }); |
michael@0 | 547 | } |
michael@0 | 548 | |
michael@0 | 549 | function getNumberOfSearchesByDate(aEngineName, aData, aDate) { |
michael@0 | 550 | if (aData.days.hasDay(aDate)) { |
michael@0 | 551 | let id = Services.search.getEngineByName(aEngineName).identifier; |
michael@0 | 552 | |
michael@0 | 553 | let day = aData.days.getDay(aDate); |
michael@0 | 554 | let field = id + ".abouthome"; |
michael@0 | 555 | |
michael@0 | 556 | if (day.has(field)) { |
michael@0 | 557 | return day.get(field) || 0; |
michael@0 | 558 | } |
michael@0 | 559 | } |
michael@0 | 560 | |
michael@0 | 561 | return 0; // No records found. |
michael@0 | 562 | } |
michael@0 | 563 | |
michael@0 | 564 | function waitForLoad(cb) { |
michael@0 | 565 | let browser = gBrowser.selectedBrowser; |
michael@0 | 566 | browser.addEventListener("load", function listener() { |
michael@0 | 567 | if (browser.currentURI.spec == "about:blank") |
michael@0 | 568 | return; |
michael@0 | 569 | info("Page loaded: " + browser.currentURI.spec); |
michael@0 | 570 | browser.removeEventListener("load", listener, true); |
michael@0 | 571 | |
michael@0 | 572 | cb(); |
michael@0 | 573 | }, true); |
michael@0 | 574 | } |