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 | /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- |
michael@0 | 2 | * vim:set ts=2 sw=2 sts=2 et: |
michael@0 | 3 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | const TEST_URL_BASES = [ |
michael@0 | 8 | "http://example.org/browser/browser/base/content/test/general/dummy_page.html#tabmatch", |
michael@0 | 9 | "http://example.org/browser/browser/base/content/test/general/moz.png#tabmatch" |
michael@0 | 10 | ]; |
michael@0 | 11 | |
michael@0 | 12 | var gController = Cc["@mozilla.org/autocomplete/controller;1"]. |
michael@0 | 13 | getService(Ci.nsIAutoCompleteController); |
michael@0 | 14 | |
michael@0 | 15 | var gTabWaitCount = 0; |
michael@0 | 16 | var gTabCounter = 0; |
michael@0 | 17 | |
michael@0 | 18 | var gTestSteps = [ |
michael@0 | 19 | function() { |
michael@0 | 20 | info("Running step 1"); |
michael@0 | 21 | for (let i = 0; i < 10; i++) { |
michael@0 | 22 | let tab = gBrowser.addTab(); |
michael@0 | 23 | loadTab(tab, TEST_URL_BASES[0] + (++gTabCounter)); |
michael@0 | 24 | } |
michael@0 | 25 | }, |
michael@0 | 26 | function() { |
michael@0 | 27 | info("Running step 2"); |
michael@0 | 28 | gBrowser.selectTabAtIndex(1); |
michael@0 | 29 | gBrowser.removeCurrentTab(); |
michael@0 | 30 | gBrowser.selectTabAtIndex(1); |
michael@0 | 31 | gBrowser.removeCurrentTab(); |
michael@0 | 32 | for (let i = 1; i < gBrowser.tabs.length; i++) |
michael@0 | 33 | loadTab(gBrowser.tabs[i], TEST_URL_BASES[1] + (++gTabCounter)); |
michael@0 | 34 | }, |
michael@0 | 35 | function() { |
michael@0 | 36 | info("Running step 3"); |
michael@0 | 37 | for (let i = 1; i < gBrowser.tabs.length; i++) |
michael@0 | 38 | loadTab(gBrowser.tabs[i], TEST_URL_BASES[0] + gTabCounter); |
michael@0 | 39 | }, |
michael@0 | 40 | function() { |
michael@0 | 41 | info("Running step 4 - ensure we don't register subframes as open pages"); |
michael@0 | 42 | let tab = gBrowser.addTab(); |
michael@0 | 43 | tab.linkedBrowser.addEventListener("load", function () { |
michael@0 | 44 | tab.linkedBrowser.removeEventListener("load", arguments.callee, true); |
michael@0 | 45 | // Start the sub-document load. |
michael@0 | 46 | executeSoon(function () { |
michael@0 | 47 | tab.linkedBrowser.addEventListener("load", function (e) { |
michael@0 | 48 | tab.linkedBrowser.removeEventListener("load", arguments.callee, true); |
michael@0 | 49 | ensure_opentabs_match_db(nextStep); |
michael@0 | 50 | }, true); |
michael@0 | 51 | tab.linkedBrowser.contentDocument.querySelector("iframe").src = "http://test2.example.org/"; |
michael@0 | 52 | }); |
michael@0 | 53 | }, true); |
michael@0 | 54 | tab.linkedBrowser.loadURI('data:text/html,<body><iframe src=""></iframe></body>'); |
michael@0 | 55 | }, |
michael@0 | 56 | function() { |
michael@0 | 57 | info("Running step 5 - remove tab immediately"); |
michael@0 | 58 | let tab = gBrowser.addTab("about:logo"); |
michael@0 | 59 | gBrowser.removeTab(tab); |
michael@0 | 60 | ensure_opentabs_match_db(nextStep); |
michael@0 | 61 | }, |
michael@0 | 62 | function() { |
michael@0 | 63 | info("Running step 6 - check swapBrowsersAndCloseOther preserves registered switch-to-tab result"); |
michael@0 | 64 | let tabToKeep = gBrowser.addTab(); |
michael@0 | 65 | let tab = gBrowser.addTab(); |
michael@0 | 66 | tab.linkedBrowser.addEventListener("load", function () { |
michael@0 | 67 | tab.linkedBrowser.removeEventListener("load", arguments.callee, true); |
michael@0 | 68 | gBrowser.swapBrowsersAndCloseOther(tabToKeep, tab); |
michael@0 | 69 | ensure_opentabs_match_db(function () { |
michael@0 | 70 | gBrowser.removeTab(tabToKeep); |
michael@0 | 71 | ensure_opentabs_match_db(nextStep); |
michael@0 | 72 | }); |
michael@0 | 73 | }, true); |
michael@0 | 74 | tab.linkedBrowser.loadURI("about:mozilla"); |
michael@0 | 75 | }, |
michael@0 | 76 | function() { |
michael@0 | 77 | info("Running step 7 - close all tabs"); |
michael@0 | 78 | |
michael@0 | 79 | Services.prefs.clearUserPref("browser.sessionstore.restore_on_demand"); |
michael@0 | 80 | |
michael@0 | 81 | gBrowser.addTab("about:blank", {skipAnimation: true}); |
michael@0 | 82 | while (gBrowser.tabs.length > 1) { |
michael@0 | 83 | info("Removing tab: " + gBrowser.tabs[0].linkedBrowser.currentURI.spec); |
michael@0 | 84 | gBrowser.selectTabAtIndex(0); |
michael@0 | 85 | gBrowser.removeCurrentTab(); |
michael@0 | 86 | } |
michael@0 | 87 | ensure_opentabs_match_db(nextStep); |
michael@0 | 88 | } |
michael@0 | 89 | ]; |
michael@0 | 90 | |
michael@0 | 91 | |
michael@0 | 92 | |
michael@0 | 93 | function test() { |
michael@0 | 94 | waitForExplicitFinish(); |
michael@0 | 95 | nextStep(); |
michael@0 | 96 | } |
michael@0 | 97 | |
michael@0 | 98 | function loadTab(tab, url) { |
michael@0 | 99 | // Because adding visits is async, we will not be notified immediately. |
michael@0 | 100 | let visited = false; |
michael@0 | 101 | let loaded = false; |
michael@0 | 102 | |
michael@0 | 103 | function maybeCheckResults() { |
michael@0 | 104 | if (visited && loaded && --gTabWaitCount == 0) { |
michael@0 | 105 | ensure_opentabs_match_db(nextStep); |
michael@0 | 106 | } |
michael@0 | 107 | } |
michael@0 | 108 | |
michael@0 | 109 | tab.linkedBrowser.addEventListener("load", function () { |
michael@0 | 110 | tab.linkedBrowser.removeEventListener("load", arguments.callee, true); |
michael@0 | 111 | loaded = true; |
michael@0 | 112 | maybeCheckResults(); |
michael@0 | 113 | }, true); |
michael@0 | 114 | |
michael@0 | 115 | if (!visited) { |
michael@0 | 116 | Services.obs.addObserver( |
michael@0 | 117 | function (aSubject, aTopic, aData) { |
michael@0 | 118 | if (url != aSubject.QueryInterface(Ci.nsIURI).spec) |
michael@0 | 119 | return; |
michael@0 | 120 | Services.obs.removeObserver(arguments.callee, aTopic); |
michael@0 | 121 | visited = true; |
michael@0 | 122 | maybeCheckResults(); |
michael@0 | 123 | }, |
michael@0 | 124 | "uri-visit-saved", |
michael@0 | 125 | false |
michael@0 | 126 | ); |
michael@0 | 127 | } |
michael@0 | 128 | |
michael@0 | 129 | gTabWaitCount++; |
michael@0 | 130 | info("Loading page: " + url); |
michael@0 | 131 | tab.linkedBrowser.loadURI(url); |
michael@0 | 132 | } |
michael@0 | 133 | |
michael@0 | 134 | function waitForRestoredTab(tab) { |
michael@0 | 135 | gTabWaitCount++; |
michael@0 | 136 | |
michael@0 | 137 | tab.linkedBrowser.addEventListener("load", function () { |
michael@0 | 138 | tab.linkedBrowser.removeEventListener("load", arguments.callee, true); |
michael@0 | 139 | if (--gTabWaitCount == 0) { |
michael@0 | 140 | ensure_opentabs_match_db(nextStep); |
michael@0 | 141 | } |
michael@0 | 142 | }, true); |
michael@0 | 143 | } |
michael@0 | 144 | |
michael@0 | 145 | |
michael@0 | 146 | function nextStep() { |
michael@0 | 147 | if (gTestSteps.length == 0) { |
michael@0 | 148 | while (gBrowser.tabs.length > 1) { |
michael@0 | 149 | gBrowser.selectTabAtIndex(1); |
michael@0 | 150 | gBrowser.removeCurrentTab(); |
michael@0 | 151 | } |
michael@0 | 152 | |
michael@0 | 153 | waitForClearHistory(finish); |
michael@0 | 154 | |
michael@0 | 155 | return; |
michael@0 | 156 | } |
michael@0 | 157 | |
michael@0 | 158 | var stepFunc = gTestSteps.shift(); |
michael@0 | 159 | stepFunc(); |
michael@0 | 160 | } |
michael@0 | 161 | |
michael@0 | 162 | function ensure_opentabs_match_db(aCallback) { |
michael@0 | 163 | var tabs = {}; |
michael@0 | 164 | |
michael@0 | 165 | var winEnum = Services.wm.getEnumerator("navigator:browser"); |
michael@0 | 166 | while (winEnum.hasMoreElements()) { |
michael@0 | 167 | let browserWin = winEnum.getNext(); |
michael@0 | 168 | // skip closed-but-not-destroyed windows |
michael@0 | 169 | if (browserWin.closed) |
michael@0 | 170 | continue; |
michael@0 | 171 | |
michael@0 | 172 | for (let i = 0; i < browserWin.gBrowser.tabContainer.childElementCount; i++) { |
michael@0 | 173 | let browser = browserWin.gBrowser.getBrowserAtIndex(i); |
michael@0 | 174 | let url = browser.currentURI.spec; |
michael@0 | 175 | if (browserWin.isBlankPageURL(url)) |
michael@0 | 176 | continue; |
michael@0 | 177 | if (!(url in tabs)) |
michael@0 | 178 | tabs[url] = 1; |
michael@0 | 179 | else |
michael@0 | 180 | tabs[url]++; |
michael@0 | 181 | } |
michael@0 | 182 | } |
michael@0 | 183 | |
michael@0 | 184 | checkAutocompleteResults(tabs, aCallback); |
michael@0 | 185 | } |
michael@0 | 186 | |
michael@0 | 187 | /** |
michael@0 | 188 | * Clears history invoking callback when done. |
michael@0 | 189 | */ |
michael@0 | 190 | function waitForClearHistory(aCallback) { |
michael@0 | 191 | const TOPIC_EXPIRATION_FINISHED = "places-expiration-finished"; |
michael@0 | 192 | let observer = { |
michael@0 | 193 | observe: function(aSubject, aTopic, aData) { |
michael@0 | 194 | Services.obs.removeObserver(this, TOPIC_EXPIRATION_FINISHED); |
michael@0 | 195 | aCallback(); |
michael@0 | 196 | } |
michael@0 | 197 | }; |
michael@0 | 198 | Services.obs.addObserver(observer, TOPIC_EXPIRATION_FINISHED, false); |
michael@0 | 199 | |
michael@0 | 200 | PlacesUtils.bhistory.removeAllPages(); |
michael@0 | 201 | } |
michael@0 | 202 | |
michael@0 | 203 | function checkAutocompleteResults(aExpected, aCallback) |
michael@0 | 204 | { |
michael@0 | 205 | gController.input = { |
michael@0 | 206 | timeout: 10, |
michael@0 | 207 | textValue: "", |
michael@0 | 208 | searches: ["history"], |
michael@0 | 209 | searchParam: "enable-actions", |
michael@0 | 210 | popupOpen: false, |
michael@0 | 211 | minResultsForPopup: 0, |
michael@0 | 212 | invalidate: function() {}, |
michael@0 | 213 | disableAutoComplete: false, |
michael@0 | 214 | completeDefaultIndex: false, |
michael@0 | 215 | get popup() { return this; }, |
michael@0 | 216 | onSearchBegin: function() {}, |
michael@0 | 217 | onSearchComplete: function () |
michael@0 | 218 | { |
michael@0 | 219 | info("Found " + gController.matchCount + " matches."); |
michael@0 | 220 | // Check to see the expected uris and titles match up (in any order) |
michael@0 | 221 | for (let i = 0; i < gController.matchCount; i++) { |
michael@0 | 222 | let uri = gController.getValueAt(i).replace(/^moz-action:[^,]+,/i, ""); |
michael@0 | 223 | |
michael@0 | 224 | info("Search for '" + uri + "' in open tabs."); |
michael@0 | 225 | let expected = uri in aExpected; |
michael@0 | 226 | ok(expected, uri + " was found in autocomplete, was " + (expected ? "" : "not ") + "expected"); |
michael@0 | 227 | // Remove the found entry from expected results. |
michael@0 | 228 | delete aExpected[uri]; |
michael@0 | 229 | } |
michael@0 | 230 | |
michael@0 | 231 | // Make sure there is no reported open page that is not open. |
michael@0 | 232 | for (let entry in aExpected) { |
michael@0 | 233 | ok(false, "'" + entry + "' should be found in autocomplete"); |
michael@0 | 234 | } |
michael@0 | 235 | |
michael@0 | 236 | executeSoon(aCallback); |
michael@0 | 237 | }, |
michael@0 | 238 | setSelectedIndex: function() {}, |
michael@0 | 239 | get searchCount() { return this.searches.length; }, |
michael@0 | 240 | getSearchAt: function(aIndex) this.searches[aIndex], |
michael@0 | 241 | QueryInterface: XPCOMUtils.generateQI([ |
michael@0 | 242 | Ci.nsIAutoCompleteInput, |
michael@0 | 243 | Ci.nsIAutoCompletePopup, |
michael@0 | 244 | ]) |
michael@0 | 245 | }; |
michael@0 | 246 | |
michael@0 | 247 | info("Searching open pages."); |
michael@0 | 248 | gController.startSearch(Services.prefs.getCharPref("browser.urlbar.restrict.openpage")); |
michael@0 | 249 | } |