|
1 <?xml version="1.0"?> |
|
2 |
|
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 <?xml-stylesheet href="chrome://global/skin" type="text/css"?> |
|
8 |
|
9 <window id="FindbarTest" |
|
10 xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul" |
|
11 width="600" |
|
12 height="600" |
|
13 onload="onLoad();" |
|
14 title="findbar test"> |
|
15 |
|
16 <script type="application/javascript" |
|
17 src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"/> |
|
18 <script type="application/javascript" |
|
19 src="chrome://mochikit/content/tests/SimpleTest/ChromeUtils.js"/> |
|
20 |
|
21 <script type="application/javascript"><![CDATA[ |
|
22 const Ci = Components.interfaces; |
|
23 const Cc = Components.classes; |
|
24 const Cr = Components.results; |
|
25 |
|
26 const SAMPLE_URL = "http://www.mozilla.org/"; |
|
27 const SAMPLE_TEXT = "Some text in a text field."; |
|
28 const SEARCH_TEXT = "Text Test"; |
|
29 |
|
30 var gFindBar = null; |
|
31 var gBrowser; |
|
32 |
|
33 var gClipboard = Cc["@mozilla.org/widget/clipboard;1"].getService(Ci.nsIClipboard); |
|
34 var gHasFindClipboard = gClipboard.supportsFindClipboard(); |
|
35 |
|
36 var gStatusText; |
|
37 var gXULBrowserWindow = { |
|
38 QueryInterface: function(aIID) { |
|
39 if (aIID.Equals(Ci.nsIXULBrowserWindow) || |
|
40 aIID.Equals(Ci.nsISupports)) |
|
41 return this; |
|
42 |
|
43 throw Cr.NS_NOINTERFACE; |
|
44 }, |
|
45 |
|
46 setJSStatus: function() { }, |
|
47 |
|
48 setOverLink: function(aStatusText, aLink) { |
|
49 gStatusText = aStatusText; |
|
50 }, |
|
51 |
|
52 onBeforeLinkTraversal: function() { } |
|
53 }; |
|
54 |
|
55 function ok(condition, message) { |
|
56 window.opener.wrappedJSObject.SimpleTest.ok(condition, message); |
|
57 } |
|
58 function finish() { |
|
59 window.close(); |
|
60 window.opener.wrappedJSObject.SimpleTest.finish(); |
|
61 } |
|
62 |
|
63 function onLoad() { |
|
64 window.QueryInterface(Ci.nsIInterfaceRequestor) |
|
65 .getInterface(Ci.nsIWebNavigation) |
|
66 .QueryInterface(Ci.nsIDocShellTreeItem) |
|
67 .treeOwner |
|
68 .QueryInterface(Ci.nsIInterfaceRequestor) |
|
69 .getInterface(Ci.nsIXULWindow) |
|
70 .XULBrowserWindow = gXULBrowserWindow; |
|
71 |
|
72 var _delayedOnLoad = function() { |
|
73 gFindBar = document.getElementById("FindToolbar"); |
|
74 gBrowser = document.getElementById("content"); |
|
75 gBrowser.addEventListener("pageshow", _delayedOnPageShow, false); |
|
76 gBrowser.loadURI("data:text/html,<h2 id='h2'>" + SEARCH_TEXT + "</h2><h2><a href='" + SAMPLE_URL + "'>Link Test</a></h2><input id='text' type='text' value='" + SAMPLE_TEXT + "'></input><input id='button' type='button'></input><img id='img' width='50' height='50'/>"); |
|
77 } |
|
78 setTimeout(_delayedOnLoad, 1000); |
|
79 } |
|
80 |
|
81 function _delayedOnPageShow() { |
|
82 // setTimeout to the test runs after painting suppression ends |
|
83 setTimeout(onPageShow, 0); |
|
84 } |
|
85 |
|
86 function onPageShow() { |
|
87 testNormalFind(); |
|
88 gFindBar.close(); |
|
89 ok(gFindBar.hidden, "Failed to close findbar after testNormalFind"); |
|
90 testNormalFindWithComposition(); |
|
91 gFindBar.close(); |
|
92 ok(gFindBar.hidden, "findbar should be hidden after testNormalFindWithComposition"); |
|
93 testAutoCaseSensitivityUI(); |
|
94 testQuickFindText(); |
|
95 gFindBar.close(); |
|
96 ok(gFindBar.hidden, "Failed to close findbar after testQuickFindText"); |
|
97 testFindWithHighlight(); |
|
98 gFindBar.close(); |
|
99 ok(gFindBar.hidden, "Failed to close findbar after testFindWithHighlight"); |
|
100 testFindbarSelection(); |
|
101 testDrop(); |
|
102 testQuickFindLink(); |
|
103 if (gHasFindClipboard) |
|
104 testStatusText(); |
|
105 testQuickFindClose(); |
|
106 } |
|
107 |
|
108 function testFindbarSelection() { |
|
109 function checkFindbarState(aTestName, aExpSelection) { |
|
110 document.getElementById("cmd_find").doCommand(); |
|
111 ok(!gFindBar.hidden, "testFindbarSelection: failed to open findbar: " + aTestName); |
|
112 ok(document.commandDispatcher.focusedElement == gFindBar._findField.inputField, |
|
113 "testFindbarSelection: find field is not focused: " + aTestName); |
|
114 if (!gHasFindClipboard) { |
|
115 ok(gFindBar._findField.value == aExpSelection, |
|
116 "Incorrect selection in testFindbarSelection: " + aTestName + |
|
117 ". Selection: " + gFindBar._findField.value); |
|
118 } |
|
119 |
|
120 // Clear the value, close the findbar |
|
121 gFindBar._findField.value = ""; |
|
122 gFindBar.close(); |
|
123 } |
|
124 |
|
125 // test normal selected text |
|
126 var cH2 = gBrowser.contentDocument.getElementById("h2"); |
|
127 var cSelection = gBrowser.contentDocument.defaultView.getSelection(); |
|
128 var cRange = gBrowser.contentDocument.createRange(); |
|
129 cRange.setStart(cH2, 0); |
|
130 cRange.setEnd(cH2, 1); |
|
131 cSelection.removeAllRanges(); |
|
132 cSelection.addRange(cRange); |
|
133 checkFindbarState("plain text", SEARCH_TEXT); |
|
134 |
|
135 // test nsIDOMNSEditableElement with selection |
|
136 var textInput = gBrowser.contentDocument.getElementById("text"); |
|
137 textInput.focus(); |
|
138 textInput.select(); |
|
139 checkFindbarState("text input", SAMPLE_TEXT); |
|
140 |
|
141 // test non-editable nsIDOMNSEditableElement (button) |
|
142 gBrowser.contentDocument.getElementById("button").focus(); |
|
143 checkFindbarState("button", ""); |
|
144 } |
|
145 |
|
146 function testDrop() |
|
147 { |
|
148 gFindBar.open(); |
|
149 // use an dummy image to start the drag so it doesn't get interrupted by a selection |
|
150 var img = gBrowser.contentDocument.getElementById("img"); |
|
151 synthesizeDrop(img, gFindBar._findField, [[ {type: "text/plain", data: "Rabbits" } ]], "copy", window); |
|
152 window.opener.wrappedJSObject.SimpleTest.is(gFindBar._findField.inputField.value, "Rabbits", "drop on findbar"); |
|
153 gFindBar.close(); |
|
154 } |
|
155 |
|
156 function testQuickFindClose() { |
|
157 var _isClosedCallback = function() { |
|
158 ok(gFindBar.hidden, |
|
159 "_isClosedCallback: Failed to auto-close quick find bar after " + |
|
160 gFindBar._quickFindTimeoutLength + "ms"); |
|
161 finish(); |
|
162 }; |
|
163 setTimeout(_isClosedCallback, gFindBar._quickFindTimeoutLength + 100); |
|
164 } |
|
165 |
|
166 function testStatusText() { |
|
167 var _delayedCheckStatusText = function() { |
|
168 ok(gStatusText == SAMPLE_URL, "testStatusText: Failed to set status text of found link"); |
|
169 }; |
|
170 setTimeout(_delayedCheckStatusText, 100); |
|
171 } |
|
172 |
|
173 function enterStringIntoFindField(aString) { |
|
174 for (var i=0; i < aString.length; i++) { |
|
175 var event = document.createEvent("KeyEvents"); |
|
176 event.initKeyEvent("keypress", true, true, null, false, false, |
|
177 false, false, 0, aString.charCodeAt(i)); |
|
178 gFindBar._findField.inputField.dispatchEvent(event); |
|
179 } |
|
180 } |
|
181 |
|
182 // also test match-case |
|
183 function testNormalFind() { |
|
184 document.getElementById("cmd_find").doCommand(); |
|
185 |
|
186 ok(!gFindBar.hidden, "testNormalFind: failed to open findbar"); |
|
187 ok(document.commandDispatcher.focusedElement == gFindBar._findField.inputField, |
|
188 "testNormalFind: find field is not focused"); |
|
189 |
|
190 var matchCaseCheckbox = gFindBar.getElement("find-case-sensitive"); |
|
191 if (!matchCaseCheckbox.hidden & matchCaseCheckbox.checked) |
|
192 matchCaseCheckbox.click(); |
|
193 |
|
194 var searchStr = "text tes"; |
|
195 enterStringIntoFindField(searchStr); |
|
196 ok(gBrowser.contentWindow.getSelection().toString().toLowerCase() == searchStr, |
|
197 "testNormalFind: failed to find '" + searchStr + "'"); |
|
198 testClipboardSearchString(gBrowser.contentWindow.getSelection().toString()); |
|
199 |
|
200 if (!matchCaseCheckbox.hidden) { |
|
201 matchCaseCheckbox.click(); |
|
202 enterStringIntoFindField("t"); |
|
203 ok(gBrowser.contentWindow.getSelection() != searchStr, |
|
204 "testNormalFind: Case-sensitivy is broken '" + searchStr + "'"); |
|
205 matchCaseCheckbox.click(); |
|
206 } |
|
207 } |
|
208 |
|
209 function testNormalFindWithComposition() { |
|
210 document.getElementById("cmd_find").doCommand(); |
|
211 |
|
212 ok(!gFindBar.hidden, "testNormalFindWithComposition: findbar should be open"); |
|
213 ok(document.commandDispatcher.focusedElement == gFindBar._findField.inputField, |
|
214 "testNormalFindWithComposition: find field should be focused"); |
|
215 |
|
216 var matchCaseCheckbox = gFindBar.getElement("find-case-sensitive"); |
|
217 var clicked = false; |
|
218 if (!matchCaseCheckbox.hidden & matchCaseCheckbox.checked) { |
|
219 matchCaseCheckbox.click(); |
|
220 clicked = true; |
|
221 } |
|
222 |
|
223 gFindBar._findField.inputField.focus(); |
|
224 |
|
225 var searchStr = "text"; |
|
226 |
|
227 synthesizeComposition({ type: "compositionstart" }); |
|
228 synthesizeComposition({ type: "compositionupdate", data: searchStr }); |
|
229 synthesizeText( |
|
230 { "composition": |
|
231 { "string": searchStr, |
|
232 "clauses": |
|
233 [ |
|
234 { "length": searchStr.length, "attr": COMPOSITION_ATTR_RAWINPUT } |
|
235 ] |
|
236 }, |
|
237 "caret": { "start": searchStr.length, "length": 0 } |
|
238 }); |
|
239 |
|
240 ok(gBrowser.contentWindow.getSelection().toString().toLowerCase() != searchStr, |
|
241 "testNormalFindWithComposition: text shouldn't be found during composition"); |
|
242 |
|
243 synthesizeText( |
|
244 { "composition": |
|
245 { "string": searchStr, |
|
246 "clauses": |
|
247 [ |
|
248 { "length": 0, "attr": 0 } |
|
249 ] |
|
250 }, |
|
251 "caret": { "start": searchStr.length, "length": 0 } |
|
252 }); |
|
253 synthesizeComposition({ type: "compositionend", data: searchStr }); |
|
254 |
|
255 ok(gBrowser.contentWindow.getSelection().toString().toLowerCase() == searchStr, |
|
256 "testNormalFindWithComposition: text should be found after committing composition"); |
|
257 testClipboardSearchString(gBrowser.contentWindow.getSelection().toString()); |
|
258 |
|
259 if (clicked) { |
|
260 matchCaseCheckbox.click(); |
|
261 } |
|
262 } |
|
263 |
|
264 function testAutoCaseSensitivityUI() { |
|
265 var matchCaseCheckbox = gFindBar.getElement("find-case-sensitive"); |
|
266 var matchCaseLabel = gFindBar.getElement("match-case-status"); |
|
267 document.getElementById("cmd_find").doCommand(); |
|
268 ok(!matchCaseCheckbox.hidden, "match case box is hidden in manual mode"); |
|
269 ok(matchCaseLabel.hidden, "match case label is visible in manual mode"); |
|
270 |
|
271 var prefsvc = Cc["@mozilla.org/preferences-service;1"]. |
|
272 getService(Components.interfaces.nsIPrefBranch); |
|
273 prefsvc.setIntPref("accessibility.typeaheadfind.casesensitive", 2); |
|
274 |
|
275 ok(matchCaseCheckbox.hidden, |
|
276 "match case box is visible in automatic mode"); |
|
277 ok(!matchCaseLabel.hidden, |
|
278 "match case label is hidden in automatic mode"); |
|
279 |
|
280 enterStringIntoFindField("a"); |
|
281 var insensitiveLabel = matchCaseLabel.value; |
|
282 enterStringIntoFindField("A"); |
|
283 var sensitiveLabel = matchCaseLabel.value; |
|
284 ok(insensitiveLabel != sensitiveLabel, |
|
285 "Case Sensitive label was not correctly updated"); |
|
286 |
|
287 // bug 365551 |
|
288 gFindBar.onFindAgainCommand(); |
|
289 ok(matchCaseCheckbox.hidden && !matchCaseLabel.hidden, |
|
290 "bug 365551: case sensitivity UI is broken after find-again"); |
|
291 prefsvc.setIntPref("accessibility.typeaheadfind.casesensitive", 0); |
|
292 gFindBar.close(); |
|
293 } |
|
294 |
|
295 function clearFocus() { |
|
296 document.commandDispatcher.focusedElement = null; |
|
297 document.commandDispatcher.focusedWindow = null; |
|
298 gBrowser.contentWindow.focus(); |
|
299 } |
|
300 |
|
301 function testQuickFindLink() { |
|
302 clearFocus(); |
|
303 |
|
304 var event = document.createEvent("KeyEvents"); |
|
305 event.initKeyEvent("keypress", true, true, null, false, false, |
|
306 false, false, 0, "'".charCodeAt(0)); |
|
307 gBrowser.contentDocument.documentElement.dispatchEvent(event); |
|
308 |
|
309 ok(!gFindBar.hidden, "testQuickFindLink: failed to open findbar"); |
|
310 ok(document.commandDispatcher.focusedElement == gFindBar._findField.inputField, |
|
311 "testQuickFindLink: find field is not focused"); |
|
312 |
|
313 var searchStr = "Link Test"; |
|
314 enterStringIntoFindField(searchStr); |
|
315 ok(gBrowser.contentWindow.getSelection() == searchStr, |
|
316 "testQuickFindLink: failed to find sample link"); |
|
317 testClipboardSearchString(searchStr); |
|
318 } |
|
319 |
|
320 // See bug 963925 for more details on this test. |
|
321 function testFindWithHighlight() { |
|
322 //clearFocus(); |
|
323 gFindBar._findField.value = ""; |
|
324 |
|
325 let findCommand = document.getElementById("cmd_find"); |
|
326 findCommand.doCommand(); |
|
327 |
|
328 let searchStr = "e"; |
|
329 enterStringIntoFindField(searchStr); |
|
330 |
|
331 let a = gFindBar._findField.value; |
|
332 let b = gFindBar._browser.finder._fastFind.searchString; |
|
333 let c = gFindBar._browser.finder.searchString; |
|
334 ok(a == b && b == c, "testFindWithHighlight 1: " + a + ", " + b + ", " + c + "."); |
|
335 |
|
336 let oldGetInitialSelection = gFindBar._getInitialSelection; |
|
337 let searchStr = "t"; |
|
338 gFindBar._getInitialSelection = () => searchStr; |
|
339 findCommand.doCommand(); |
|
340 gFindBar._getInitialSelection = oldGetInitialSelection; |
|
341 |
|
342 a = gFindBar._findField.value; |
|
343 b = gFindBar._browser.finder._fastFind.searchString; |
|
344 c = gFindBar._browser.finder.searchString; |
|
345 ok(a == searchStr && b == c, "testFindWithHighlight 2: " + searchStr + |
|
346 ", " + a + ", " + b + ", " + c + "."); |
|
347 |
|
348 let highlightButton = gFindBar.getElement("highlight"); |
|
349 highlightButton.click(); |
|
350 ok(highlightButton.checked, "testFindWithHighlight 3: Highlight All should be checked."); |
|
351 |
|
352 a = gFindBar._findField.value; |
|
353 b = gFindBar._browser.finder._fastFind.searchString; |
|
354 c = gFindBar._browser.finder.searchString; |
|
355 ok(a == searchStr && b == c, "testFindWithHighlight 4: " + a + ", " + b + ", " + c + "."); |
|
356 |
|
357 gFindBar.onFindAgainCommand(); |
|
358 a = gFindBar._findField.value; |
|
359 b = gFindBar._browser.finder._fastFind.searchString; |
|
360 c = gFindBar._browser.finder.searchString; |
|
361 ok(a == b && b == c, "testFindWithHighlight 5: " + a + ", " + b + ", " + c + "."); |
|
362 |
|
363 highlightButton.click(); |
|
364 ok(!highlightButton.checked, "testFindWithHighlight: Highlight All should be unchecked."); |
|
365 } |
|
366 |
|
367 function testQuickFindText() { |
|
368 clearFocus(); |
|
369 |
|
370 var event = document.createEvent("KeyEvents"); |
|
371 event.initKeyEvent("keypress", true, true, null, false, false, |
|
372 false, false, 0, "/".charCodeAt(0)); |
|
373 gBrowser.contentDocument.documentElement.dispatchEvent(event); |
|
374 |
|
375 ok(!gFindBar.hidden, "testQuickFindText: failed to open findbar"); |
|
376 ok(document.commandDispatcher.focusedElement == gFindBar._findField.inputField, |
|
377 "testQuickFindText: find field is not focused"); |
|
378 |
|
379 enterStringIntoFindField(SEARCH_TEXT); |
|
380 ok(gBrowser.contentWindow.getSelection() == SEARCH_TEXT, |
|
381 "testQuickFindText: failed to find '" + SEARCH_TEXT + "'"); |
|
382 testClipboardSearchString(SEARCH_TEXT); |
|
383 } |
|
384 |
|
385 function testClipboardSearchString(aExpected) { |
|
386 if (!gHasFindClipboard) |
|
387 return; |
|
388 |
|
389 if (!aExpected) |
|
390 aExpected = ""; |
|
391 var searchStr = gFindBar.browser.finder.clipboardSearchString; |
|
392 ok(searchStr.toLowerCase() == aExpected.toLowerCase(), |
|
393 "testClipboardSearchString: search string not set to '" + aExpected + |
|
394 "', instead found '" + searchStr + "'"); |
|
395 } |
|
396 ]]></script> |
|
397 |
|
398 <commandset> |
|
399 <command id="cmd_find" oncommand="document.getElementById('FindToolbar').onFindCommand();"/> |
|
400 </commandset> |
|
401 <browser type="content-primary" flex="1" id="content" src="about:blank"/> |
|
402 <findbar id="FindToolbar" browserid="content"/> |
|
403 </window> |