1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/browser/base/content/test/general/browser_tabMatchesInAwesomebar_perwindowpb.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,249 @@ 1.4 +/* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- 1.5 + * vim:set ts=2 sw=2 sts=2 et: 1.6 + * This Source Code Form is subject to the terms of the Mozilla Public 1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.9 + 1.10 +const TEST_URL_BASES = [ 1.11 + "http://example.org/browser/browser/base/content/test/general/dummy_page.html#tabmatch", 1.12 + "http://example.org/browser/browser/base/content/test/general/moz.png#tabmatch" 1.13 +]; 1.14 + 1.15 +var gController = Cc["@mozilla.org/autocomplete/controller;1"]. 1.16 + getService(Ci.nsIAutoCompleteController); 1.17 + 1.18 +var gTabWaitCount = 0; 1.19 +var gTabCounter = 0; 1.20 + 1.21 +var gTestSteps = [ 1.22 + function() { 1.23 + info("Running step 1"); 1.24 + for (let i = 0; i < 10; i++) { 1.25 + let tab = gBrowser.addTab(); 1.26 + loadTab(tab, TEST_URL_BASES[0] + (++gTabCounter)); 1.27 + } 1.28 + }, 1.29 + function() { 1.30 + info("Running step 2"); 1.31 + gBrowser.selectTabAtIndex(1); 1.32 + gBrowser.removeCurrentTab(); 1.33 + gBrowser.selectTabAtIndex(1); 1.34 + gBrowser.removeCurrentTab(); 1.35 + for (let i = 1; i < gBrowser.tabs.length; i++) 1.36 + loadTab(gBrowser.tabs[i], TEST_URL_BASES[1] + (++gTabCounter)); 1.37 + }, 1.38 + function() { 1.39 + info("Running step 3"); 1.40 + for (let i = 1; i < gBrowser.tabs.length; i++) 1.41 + loadTab(gBrowser.tabs[i], TEST_URL_BASES[0] + gTabCounter); 1.42 + }, 1.43 + function() { 1.44 + info("Running step 4 - ensure we don't register subframes as open pages"); 1.45 + let tab = gBrowser.addTab(); 1.46 + tab.linkedBrowser.addEventListener("load", function () { 1.47 + tab.linkedBrowser.removeEventListener("load", arguments.callee, true); 1.48 + // Start the sub-document load. 1.49 + executeSoon(function () { 1.50 + tab.linkedBrowser.addEventListener("load", function (e) { 1.51 + tab.linkedBrowser.removeEventListener("load", arguments.callee, true); 1.52 + ensure_opentabs_match_db(nextStep); 1.53 + }, true); 1.54 + tab.linkedBrowser.contentDocument.querySelector("iframe").src = "http://test2.example.org/"; 1.55 + }); 1.56 + }, true); 1.57 + tab.linkedBrowser.loadURI('data:text/html,<body><iframe src=""></iframe></body>'); 1.58 + }, 1.59 + function() { 1.60 + info("Running step 5 - remove tab immediately"); 1.61 + let tab = gBrowser.addTab("about:logo"); 1.62 + gBrowser.removeTab(tab); 1.63 + ensure_opentabs_match_db(nextStep); 1.64 + }, 1.65 + function() { 1.66 + info("Running step 6 - check swapBrowsersAndCloseOther preserves registered switch-to-tab result"); 1.67 + let tabToKeep = gBrowser.addTab(); 1.68 + let tab = gBrowser.addTab(); 1.69 + tab.linkedBrowser.addEventListener("load", function () { 1.70 + tab.linkedBrowser.removeEventListener("load", arguments.callee, true); 1.71 + gBrowser.swapBrowsersAndCloseOther(tabToKeep, tab); 1.72 + ensure_opentabs_match_db(function () { 1.73 + gBrowser.removeTab(tabToKeep); 1.74 + ensure_opentabs_match_db(nextStep); 1.75 + }); 1.76 + }, true); 1.77 + tab.linkedBrowser.loadURI("about:mozilla"); 1.78 + }, 1.79 + function() { 1.80 + info("Running step 7 - close all tabs"); 1.81 + 1.82 + Services.prefs.clearUserPref("browser.sessionstore.restore_on_demand"); 1.83 + 1.84 + gBrowser.addTab("about:blank", {skipAnimation: true}); 1.85 + while (gBrowser.tabs.length > 1) { 1.86 + info("Removing tab: " + gBrowser.tabs[0].linkedBrowser.currentURI.spec); 1.87 + gBrowser.selectTabAtIndex(0); 1.88 + gBrowser.removeCurrentTab(); 1.89 + } 1.90 + ensure_opentabs_match_db(nextStep); 1.91 + } 1.92 +]; 1.93 + 1.94 + 1.95 + 1.96 +function test() { 1.97 + waitForExplicitFinish(); 1.98 + nextStep(); 1.99 +} 1.100 + 1.101 +function loadTab(tab, url) { 1.102 + // Because adding visits is async, we will not be notified immediately. 1.103 + let visited = false; 1.104 + let loaded = false; 1.105 + 1.106 + function maybeCheckResults() { 1.107 + if (visited && loaded && --gTabWaitCount == 0) { 1.108 + ensure_opentabs_match_db(nextStep); 1.109 + } 1.110 + } 1.111 + 1.112 + tab.linkedBrowser.addEventListener("load", function () { 1.113 + tab.linkedBrowser.removeEventListener("load", arguments.callee, true); 1.114 + loaded = true; 1.115 + maybeCheckResults(); 1.116 + }, true); 1.117 + 1.118 + if (!visited) { 1.119 + Services.obs.addObserver( 1.120 + function (aSubject, aTopic, aData) { 1.121 + if (url != aSubject.QueryInterface(Ci.nsIURI).spec) 1.122 + return; 1.123 + Services.obs.removeObserver(arguments.callee, aTopic); 1.124 + visited = true; 1.125 + maybeCheckResults(); 1.126 + }, 1.127 + "uri-visit-saved", 1.128 + false 1.129 + ); 1.130 + } 1.131 + 1.132 + gTabWaitCount++; 1.133 + info("Loading page: " + url); 1.134 + tab.linkedBrowser.loadURI(url); 1.135 +} 1.136 + 1.137 +function waitForRestoredTab(tab) { 1.138 + gTabWaitCount++; 1.139 + 1.140 + tab.linkedBrowser.addEventListener("load", function () { 1.141 + tab.linkedBrowser.removeEventListener("load", arguments.callee, true); 1.142 + if (--gTabWaitCount == 0) { 1.143 + ensure_opentabs_match_db(nextStep); 1.144 + } 1.145 + }, true); 1.146 +} 1.147 + 1.148 + 1.149 +function nextStep() { 1.150 + if (gTestSteps.length == 0) { 1.151 + while (gBrowser.tabs.length > 1) { 1.152 + gBrowser.selectTabAtIndex(1); 1.153 + gBrowser.removeCurrentTab(); 1.154 + } 1.155 + 1.156 + waitForClearHistory(finish); 1.157 + 1.158 + return; 1.159 + } 1.160 + 1.161 + var stepFunc = gTestSteps.shift(); 1.162 + stepFunc(); 1.163 +} 1.164 + 1.165 +function ensure_opentabs_match_db(aCallback) { 1.166 + var tabs = {}; 1.167 + 1.168 + var winEnum = Services.wm.getEnumerator("navigator:browser"); 1.169 + while (winEnum.hasMoreElements()) { 1.170 + let browserWin = winEnum.getNext(); 1.171 + // skip closed-but-not-destroyed windows 1.172 + if (browserWin.closed) 1.173 + continue; 1.174 + 1.175 + for (let i = 0; i < browserWin.gBrowser.tabContainer.childElementCount; i++) { 1.176 + let browser = browserWin.gBrowser.getBrowserAtIndex(i); 1.177 + let url = browser.currentURI.spec; 1.178 + if (browserWin.isBlankPageURL(url)) 1.179 + continue; 1.180 + if (!(url in tabs)) 1.181 + tabs[url] = 1; 1.182 + else 1.183 + tabs[url]++; 1.184 + } 1.185 + } 1.186 + 1.187 + checkAutocompleteResults(tabs, aCallback); 1.188 +} 1.189 + 1.190 +/** 1.191 + * Clears history invoking callback when done. 1.192 + */ 1.193 +function waitForClearHistory(aCallback) { 1.194 + const TOPIC_EXPIRATION_FINISHED = "places-expiration-finished"; 1.195 + let observer = { 1.196 + observe: function(aSubject, aTopic, aData) { 1.197 + Services.obs.removeObserver(this, TOPIC_EXPIRATION_FINISHED); 1.198 + aCallback(); 1.199 + } 1.200 + }; 1.201 + Services.obs.addObserver(observer, TOPIC_EXPIRATION_FINISHED, false); 1.202 + 1.203 + PlacesUtils.bhistory.removeAllPages(); 1.204 +} 1.205 + 1.206 +function checkAutocompleteResults(aExpected, aCallback) 1.207 +{ 1.208 + gController.input = { 1.209 + timeout: 10, 1.210 + textValue: "", 1.211 + searches: ["history"], 1.212 + searchParam: "enable-actions", 1.213 + popupOpen: false, 1.214 + minResultsForPopup: 0, 1.215 + invalidate: function() {}, 1.216 + disableAutoComplete: false, 1.217 + completeDefaultIndex: false, 1.218 + get popup() { return this; }, 1.219 + onSearchBegin: function() {}, 1.220 + onSearchComplete: function () 1.221 + { 1.222 + info("Found " + gController.matchCount + " matches."); 1.223 + // Check to see the expected uris and titles match up (in any order) 1.224 + for (let i = 0; i < gController.matchCount; i++) { 1.225 + let uri = gController.getValueAt(i).replace(/^moz-action:[^,]+,/i, ""); 1.226 + 1.227 + info("Search for '" + uri + "' in open tabs."); 1.228 + let expected = uri in aExpected; 1.229 + ok(expected, uri + " was found in autocomplete, was " + (expected ? "" : "not ") + "expected"); 1.230 + // Remove the found entry from expected results. 1.231 + delete aExpected[uri]; 1.232 + } 1.233 + 1.234 + // Make sure there is no reported open page that is not open. 1.235 + for (let entry in aExpected) { 1.236 + ok(false, "'" + entry + "' should be found in autocomplete"); 1.237 + } 1.238 + 1.239 + executeSoon(aCallback); 1.240 + }, 1.241 + setSelectedIndex: function() {}, 1.242 + get searchCount() { return this.searches.length; }, 1.243 + getSearchAt: function(aIndex) this.searches[aIndex], 1.244 + QueryInterface: XPCOMUtils.generateQI([ 1.245 + Ci.nsIAutoCompleteInput, 1.246 + Ci.nsIAutoCompletePopup, 1.247 + ]) 1.248 + }; 1.249 + 1.250 + info("Searching open pages."); 1.251 + gController.startSearch(Services.prefs.getCharPref("browser.urlbar.restrict.openpage")); 1.252 +}