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