michael@0: /* -*- Mode: Java; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- michael@0: * vim:set ts=2 sw=2 sts=2 et: michael@0: * This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: const TEST_URL_BASES = [ michael@0: "http://example.org/browser/browser/base/content/test/general/dummy_page.html#tabmatch", michael@0: "http://example.org/browser/browser/base/content/test/general/moz.png#tabmatch" michael@0: ]; michael@0: michael@0: var gController = Cc["@mozilla.org/autocomplete/controller;1"]. michael@0: getService(Ci.nsIAutoCompleteController); michael@0: michael@0: var gTabWaitCount = 0; michael@0: var gTabCounter = 0; michael@0: michael@0: var gTestSteps = [ michael@0: function() { michael@0: info("Running step 1"); michael@0: for (let i = 0; i < 10; i++) { michael@0: let tab = gBrowser.addTab(); michael@0: loadTab(tab, TEST_URL_BASES[0] + (++gTabCounter)); michael@0: } michael@0: }, michael@0: function() { michael@0: info("Running step 2"); michael@0: gBrowser.selectTabAtIndex(1); michael@0: gBrowser.removeCurrentTab(); michael@0: gBrowser.selectTabAtIndex(1); michael@0: gBrowser.removeCurrentTab(); michael@0: for (let i = 1; i < gBrowser.tabs.length; i++) michael@0: loadTab(gBrowser.tabs[i], TEST_URL_BASES[1] + (++gTabCounter)); michael@0: }, michael@0: function() { michael@0: info("Running step 3"); michael@0: for (let i = 1; i < gBrowser.tabs.length; i++) michael@0: loadTab(gBrowser.tabs[i], TEST_URL_BASES[0] + gTabCounter); michael@0: }, michael@0: function() { michael@0: info("Running step 4 - ensure we don't register subframes as open pages"); michael@0: let tab = gBrowser.addTab(); michael@0: tab.linkedBrowser.addEventListener("load", function () { michael@0: tab.linkedBrowser.removeEventListener("load", arguments.callee, true); michael@0: // Start the sub-document load. michael@0: executeSoon(function () { michael@0: tab.linkedBrowser.addEventListener("load", function (e) { michael@0: tab.linkedBrowser.removeEventListener("load", arguments.callee, true); michael@0: ensure_opentabs_match_db(nextStep); michael@0: }, true); michael@0: tab.linkedBrowser.contentDocument.querySelector("iframe").src = "http://test2.example.org/"; michael@0: }); michael@0: }, true); michael@0: tab.linkedBrowser.loadURI('data:text/html,
'); michael@0: }, michael@0: function() { michael@0: info("Running step 5 - remove tab immediately"); michael@0: let tab = gBrowser.addTab("about:logo"); michael@0: gBrowser.removeTab(tab); michael@0: ensure_opentabs_match_db(nextStep); michael@0: }, michael@0: function() { michael@0: info("Running step 6 - check swapBrowsersAndCloseOther preserves registered switch-to-tab result"); michael@0: let tabToKeep = gBrowser.addTab(); michael@0: let tab = gBrowser.addTab(); michael@0: tab.linkedBrowser.addEventListener("load", function () { michael@0: tab.linkedBrowser.removeEventListener("load", arguments.callee, true); michael@0: gBrowser.swapBrowsersAndCloseOther(tabToKeep, tab); michael@0: ensure_opentabs_match_db(function () { michael@0: gBrowser.removeTab(tabToKeep); michael@0: ensure_opentabs_match_db(nextStep); michael@0: }); michael@0: }, true); michael@0: tab.linkedBrowser.loadURI("about:mozilla"); michael@0: }, michael@0: function() { michael@0: info("Running step 7 - close all tabs"); michael@0: michael@0: Services.prefs.clearUserPref("browser.sessionstore.restore_on_demand"); michael@0: michael@0: gBrowser.addTab("about:blank", {skipAnimation: true}); michael@0: while (gBrowser.tabs.length > 1) { michael@0: info("Removing tab: " + gBrowser.tabs[0].linkedBrowser.currentURI.spec); michael@0: gBrowser.selectTabAtIndex(0); michael@0: gBrowser.removeCurrentTab(); michael@0: } michael@0: ensure_opentabs_match_db(nextStep); michael@0: } michael@0: ]; michael@0: michael@0: michael@0: michael@0: function test() { michael@0: waitForExplicitFinish(); michael@0: nextStep(); michael@0: } michael@0: michael@0: function loadTab(tab, url) { michael@0: // Because adding visits is async, we will not be notified immediately. michael@0: let visited = false; michael@0: let loaded = false; michael@0: michael@0: function maybeCheckResults() { michael@0: if (visited && loaded && --gTabWaitCount == 0) { michael@0: ensure_opentabs_match_db(nextStep); michael@0: } michael@0: } michael@0: michael@0: tab.linkedBrowser.addEventListener("load", function () { michael@0: tab.linkedBrowser.removeEventListener("load", arguments.callee, true); michael@0: loaded = true; michael@0: maybeCheckResults(); michael@0: }, true); michael@0: michael@0: if (!visited) { michael@0: Services.obs.addObserver( michael@0: function (aSubject, aTopic, aData) { michael@0: if (url != aSubject.QueryInterface(Ci.nsIURI).spec) michael@0: return; michael@0: Services.obs.removeObserver(arguments.callee, aTopic); michael@0: visited = true; michael@0: maybeCheckResults(); michael@0: }, michael@0: "uri-visit-saved", michael@0: false michael@0: ); michael@0: } michael@0: michael@0: gTabWaitCount++; michael@0: info("Loading page: " + url); michael@0: tab.linkedBrowser.loadURI(url); michael@0: } michael@0: michael@0: function waitForRestoredTab(tab) { michael@0: gTabWaitCount++; michael@0: michael@0: tab.linkedBrowser.addEventListener("load", function () { michael@0: tab.linkedBrowser.removeEventListener("load", arguments.callee, true); michael@0: if (--gTabWaitCount == 0) { michael@0: ensure_opentabs_match_db(nextStep); michael@0: } michael@0: }, true); michael@0: } michael@0: michael@0: michael@0: function nextStep() { michael@0: if (gTestSteps.length == 0) { michael@0: while (gBrowser.tabs.length > 1) { michael@0: gBrowser.selectTabAtIndex(1); michael@0: gBrowser.removeCurrentTab(); michael@0: } michael@0: michael@0: waitForClearHistory(finish); michael@0: michael@0: return; michael@0: } michael@0: michael@0: var stepFunc = gTestSteps.shift(); michael@0: stepFunc(); michael@0: } michael@0: michael@0: function ensure_opentabs_match_db(aCallback) { michael@0: var tabs = {}; michael@0: michael@0: var winEnum = Services.wm.getEnumerator("navigator:browser"); michael@0: while (winEnum.hasMoreElements()) { michael@0: let browserWin = winEnum.getNext(); michael@0: // skip closed-but-not-destroyed windows michael@0: if (browserWin.closed) michael@0: continue; michael@0: michael@0: for (let i = 0; i < browserWin.gBrowser.tabContainer.childElementCount; i++) { michael@0: let browser = browserWin.gBrowser.getBrowserAtIndex(i); michael@0: let url = browser.currentURI.spec; michael@0: if (browserWin.isBlankPageURL(url)) michael@0: continue; michael@0: if (!(url in tabs)) michael@0: tabs[url] = 1; michael@0: else michael@0: tabs[url]++; michael@0: } michael@0: } michael@0: michael@0: checkAutocompleteResults(tabs, aCallback); michael@0: } michael@0: michael@0: /** michael@0: * Clears history invoking callback when done. michael@0: */ michael@0: function waitForClearHistory(aCallback) { michael@0: const TOPIC_EXPIRATION_FINISHED = "places-expiration-finished"; michael@0: let observer = { michael@0: observe: function(aSubject, aTopic, aData) { michael@0: Services.obs.removeObserver(this, TOPIC_EXPIRATION_FINISHED); michael@0: aCallback(); michael@0: } michael@0: }; michael@0: Services.obs.addObserver(observer, TOPIC_EXPIRATION_FINISHED, false); michael@0: michael@0: PlacesUtils.bhistory.removeAllPages(); michael@0: } michael@0: michael@0: function checkAutocompleteResults(aExpected, aCallback) michael@0: { michael@0: gController.input = { michael@0: timeout: 10, michael@0: textValue: "", michael@0: searches: ["history"], michael@0: searchParam: "enable-actions", michael@0: popupOpen: false, michael@0: minResultsForPopup: 0, michael@0: invalidate: function() {}, michael@0: disableAutoComplete: false, michael@0: completeDefaultIndex: false, michael@0: get popup() { return this; }, michael@0: onSearchBegin: function() {}, michael@0: onSearchComplete: function () michael@0: { michael@0: info("Found " + gController.matchCount + " matches."); michael@0: // Check to see the expected uris and titles match up (in any order) michael@0: for (let i = 0; i < gController.matchCount; i++) { michael@0: let uri = gController.getValueAt(i).replace(/^moz-action:[^,]+,/i, ""); michael@0: michael@0: info("Search for '" + uri + "' in open tabs."); michael@0: let expected = uri in aExpected; michael@0: ok(expected, uri + " was found in autocomplete, was " + (expected ? "" : "not ") + "expected"); michael@0: // Remove the found entry from expected results. michael@0: delete aExpected[uri]; michael@0: } michael@0: michael@0: // Make sure there is no reported open page that is not open. michael@0: for (let entry in aExpected) { michael@0: ok(false, "'" + entry + "' should be found in autocomplete"); michael@0: } michael@0: michael@0: executeSoon(aCallback); michael@0: }, michael@0: setSelectedIndex: function() {}, michael@0: get searchCount() { return this.searches.length; }, michael@0: getSearchAt: function(aIndex) this.searches[aIndex], michael@0: QueryInterface: XPCOMUtils.generateQI([ michael@0: Ci.nsIAutoCompleteInput, michael@0: Ci.nsIAutoCompletePopup, michael@0: ]) michael@0: }; michael@0: michael@0: info("Searching open pages."); michael@0: gController.startSearch(Services.prefs.getCharPref("browser.urlbar.restrict.openpage")); michael@0: }