/toolkit/content/tests/chrome/findbar_window.xul

http://github.com/zpao/v8monkey · Unknown · 363 lines · 307 code · 56 blank · 0 comment · 0 complexity · 892b6ed56fcb47fde106b5ec5fbe5ca1 MD5 · raw file

  1. <?xml version="1.0"?>
  2. <!-- ***** BEGIN LICENSE BLOCK *****
  3. - Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4. -
  5. - The contents of this file are subject to the Mozilla Public License Version
  6. - 1.1 (the "License"); you may not use this file except in compliance with
  7. - the License. You may obtain a copy of the License at
  8. - http://www.mozilla.org/MPL/
  9. -
  10. - Software distributed under the License is distributed on an "AS IS" basis,
  11. - WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. - for the specific language governing rights and limitations under the
  13. - License.
  14. -
  15. - The Original Code is Findbar Test code
  16. -
  17. - The Initial Developer of the Original Code is
  18. - Mozilla Corporation.
  19. - Portions created by the Initial Developer are Copyright (C) 2006
  20. - the Initial Developer. All Rights Reserved.
  21. -
  22. - Contributor(s):
  23. - Asaf Romano <mano@mozilla.com>
  24. -
  25. - Alternatively, the contents of this file may be used under the terms of
  26. - either the GNU General Public License Version 2 or later (the "GPL"), or
  27. - the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  28. - in which case the provisions of the GPL or the LGPL are applicable instead
  29. - of those above. If you wish to allow use of your version of this file only
  30. - under the terms of either the GPL or the LGPL, and not to allow others to
  31. - use your version of this file under the terms of the MPL, indicate your
  32. - decision by deleting the provisions above and replace them with the notice
  33. - and other provisions required by the GPL or the LGPL. If you do not delete
  34. - the provisions above, a recipient may use your version of this file under
  35. - the terms of any one of the MPL, the GPL or the LGPL.
  36. -
  37. - ***** END LICENSE BLOCK ***** -->
  38. <?xml-stylesheet href="chrome://global/skin" type="text/css"?>
  39. <window id="FindbarTest"
  40. xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
  41. width="600"
  42. height="600"
  43. onload="onLoad();"
  44. title="findbar test">
  45. <script type="application/javascript"
  46. src="chrome://mochikit/content/tests/SimpleTest/EventUtils.js"/>
  47. <script type="application/javascript"
  48. src="chrome://mochikit/content/tests/SimpleTest/ChromeUtils.js"/>
  49. <script type="application/javascript"><![CDATA[
  50. const Ci = Components.interfaces;
  51. const Cc = Components.classes;
  52. const Cr = Components.results;
  53. const SAMPLE_URL = "http://www.mozilla.org/";
  54. const SAMPLE_TEXT = "Some text in a text field.";
  55. const SEARCH_TEXT = "Text Test";
  56. var gFindBar = null;
  57. var gBrowser;
  58. var gStatusText;
  59. var gXULBrowserWindow = {
  60. QueryInterface: function(aIID) {
  61. if (aIID.Equals(Ci.nsIXULBrowserWindow) ||
  62. aIID.Equals(Ci.nsISupports))
  63. return this;
  64. throw Cr.NS_NOINTERFACE;
  65. },
  66. setJSStatus: function() { },
  67. setJSDefaultStatus: function() { },
  68. setOverLink: function(aStatusText, aLink) {
  69. gStatusText = aStatusText;
  70. },
  71. onBeforeLinkTraversal: function() { }
  72. };
  73. function ok(condition, message) {
  74. window.opener.wrappedJSObject.SimpleTest.ok(condition, message);
  75. }
  76. function finish() {
  77. window.close();
  78. window.opener.wrappedJSObject.SimpleTest.finish();
  79. }
  80. function onLoad() {
  81. window.QueryInterface(Ci.nsIInterfaceRequestor)
  82. .getInterface(Ci.nsIWebNavigation)
  83. .QueryInterface(Ci.nsIDocShellTreeItem)
  84. .treeOwner
  85. .QueryInterface(Ci.nsIInterfaceRequestor)
  86. .getInterface(Ci.nsIXULWindow)
  87. .XULBrowserWindow = gXULBrowserWindow;
  88. var _delayedOnLoad = function() {
  89. gFindBar = document.getElementById("FindToolbar");
  90. gBrowser = document.getElementById("content");
  91. gBrowser.addEventListener("pageshow", _delayedOnPageShow, false);
  92. 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'/>");
  93. }
  94. setTimeout(_delayedOnLoad, 1000);
  95. }
  96. function _delayedOnPageShow() {
  97. // setTimeout to the test runs after painting suppression ends
  98. setTimeout(onPageShow, 0);
  99. }
  100. function onPageShow() {
  101. testNormalFind();
  102. gFindBar.close();
  103. ok(gFindBar.hidden, "Failed to close findbar after testNormalFind");
  104. testNormalFindWithComposition();
  105. gFindBar.close();
  106. ok(gFindBar.hidden, "findbar should be hidden after testNormalFindWithComposition");
  107. testAutoCaseSensitivityUI();
  108. testQuickFindText();
  109. gFindBar.close();
  110. ok(gFindBar.hidden, "Failed to close findbar after testQuickFindText");
  111. testFindbarSelection();
  112. testDrop();
  113. testQuickFindLink();
  114. testStatusText();
  115. testQuickFindClose();
  116. }
  117. function testFindbarSelection() {
  118. function checkFindbarState(aTestName, aExpSelection) {
  119. document.getElementById("cmd_find").doCommand();
  120. ok(!gFindBar.hidden, "testFindbarSelection: failed to open findbar: " + aTestName);
  121. ok(document.commandDispatcher.focusedElement == gFindBar._findField.inputField,
  122. "testFindbarSelection: find field is not focused: " + aTestName);
  123. ok(gFindBar._findField.value == aExpSelection,
  124. "Incorrect selection in testFindbarSelection: " + aTestName + ". Selection: " + gFindBar._findField.value);
  125. // Clear the value, close the findbar
  126. gFindBar._findField.value = "";
  127. gFindBar.close();
  128. }
  129. // test normal selected text
  130. var cH2 = gBrowser.contentDocument.getElementById("h2");
  131. var cSelection = gBrowser.contentDocument.defaultView.getSelection();
  132. var cRange = gBrowser.contentDocument.createRange();
  133. cRange.setStart(cH2, 0);
  134. cRange.setEnd(cH2, 1);
  135. cSelection.removeAllRanges();
  136. cSelection.addRange(cRange);
  137. checkFindbarState("plain text", SEARCH_TEXT);
  138. // test nsIDOMNSEditableElement with selection
  139. var textInput = gBrowser.contentDocument.getElementById("text");
  140. textInput.focus();
  141. textInput.select();
  142. checkFindbarState("text input", SAMPLE_TEXT);
  143. // test non-editable nsIDOMNSEditableElement (button)
  144. gBrowser.contentDocument.getElementById("button").focus();
  145. checkFindbarState("button", "");
  146. }
  147. function testDrop()
  148. {
  149. gFindBar.open();
  150. // use an dummy image to start the drag so it doesn't get interrupted by a selection
  151. var img = gBrowser.contentDocument.getElementById("img");
  152. synthesizeDrop(img, gFindBar._findField, [[ {type: "text/plain", data: "Rabbits" } ]], "copy", window);
  153. window.opener.wrappedJSObject.SimpleTest.is(gFindBar._findField.inputField.value, "Rabbits", "drop on findbar");
  154. gFindBar.close();
  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. function testStatusText() {
  166. var _delayedCheckStatusText = function() {
  167. ok(gStatusText == SAMPLE_URL, "testStatusText: Failed to set status text of found link");
  168. };
  169. setTimeout(_delayedCheckStatusText, 100);
  170. }
  171. function enterStringIntoFindField(aString) {
  172. for (var i=0; i < aString.length; i++) {
  173. var event = document.createEvent("KeyEvents");
  174. event.initKeyEvent("keypress", true, true, null, false, false,
  175. false, false, 0, aString.charCodeAt(i));
  176. gFindBar._findField.inputField.dispatchEvent(event);
  177. }
  178. }
  179. // also test match-case
  180. function testNormalFind() {
  181. document.getElementById("cmd_find").doCommand();
  182. ok(!gFindBar.hidden, "testNormalFind: failed to open findbar");
  183. ok(document.commandDispatcher.focusedElement == gFindBar._findField.inputField,
  184. "testNormalFind: find field is not focused");
  185. var matchCaseCheckbox = gFindBar.getElement("find-case-sensitive");
  186. if (!matchCaseCheckbox.hidden & matchCaseCheckbox.checked)
  187. matchCaseCheckbox.click();
  188. var searchStr = "text tes";
  189. enterStringIntoFindField(searchStr);
  190. ok(gBrowser.contentWindow.getSelection().toString().toLowerCase() == searchStr,
  191. "testNormalFind: failed to find '" + searchStr + "'");
  192. if (!matchCaseCheckbox.hidden) {
  193. matchCaseCheckbox.click();
  194. enterStringIntoFindField("t");
  195. ok(gBrowser.contentWindow.getSelection() != searchStr,
  196. "testNormalFind: Case-sensitivy is broken '" + searchStr + "'");
  197. matchCaseCheckbox.click();
  198. }
  199. }
  200. function testNormalFindWithComposition() {
  201. document.getElementById("cmd_find").doCommand();
  202. ok(!gFindBar.hidden, "testNormalFindWithComposition: findbar should be open");
  203. ok(document.commandDispatcher.focusedElement == gFindBar._findField.inputField,
  204. "testNormalFindWithComposition: find field should be focused");
  205. var matchCaseCheckbox = gFindBar.getElement("find-case-sensitive");
  206. var clicked = false;
  207. if (!matchCaseCheckbox.hidden & matchCaseCheckbox.checked) {
  208. matchCaseCheckbox.click();
  209. clicked = true;
  210. }
  211. gFindBar._findField.inputField.focus();
  212. var searchStr = "text";
  213. synthesizeComposition({ type: "compositionstart" });
  214. synthesizeComposition({ type: "compositionupdate", data: searchStr });
  215. synthesizeText(
  216. { "composition":
  217. { "string": searchStr,
  218. "clauses":
  219. [
  220. { "length": searchStr.length, "attr": COMPOSITION_ATTR_RAWINPUT }
  221. ]
  222. },
  223. "caret": { "start": searchStr.length, "length": 0 }
  224. });
  225. ok(gBrowser.contentWindow.getSelection().toString().toLowerCase() != searchStr,
  226. "testNormalFindWithComposition: text shouldn't be found during composition");
  227. synthesizeText(
  228. { "composition":
  229. { "string": searchStr,
  230. "clauses":
  231. [
  232. { "length": 0, "attr": 0 }
  233. ]
  234. },
  235. "caret": { "start": searchStr.length, "length": 0 }
  236. });
  237. synthesizeComposition({ type: "compositionend", data: searchStr });
  238. ok(gBrowser.contentWindow.getSelection().toString().toLowerCase() == searchStr,
  239. "testNormalFindWithComposition: text should be found after committing composition");
  240. if (clicked) {
  241. matchCaseCheckbox.click();
  242. }
  243. }
  244. function testAutoCaseSensitivityUI() {
  245. var matchCaseCheckbox = gFindBar.getElement("find-case-sensitive");
  246. var matchCaseLabel = gFindBar.getElement("match-case-status");
  247. document.getElementById("cmd_find").doCommand();
  248. ok(!matchCaseCheckbox.hidden, "match case box is hidden in manual mode");
  249. ok(matchCaseLabel.hidden, "match case label is visible in manual mode");
  250. var prefsvc = Cc["@mozilla.org/preferences-service;1"].
  251. getService(Components.interfaces.nsIPrefBranch);
  252. prefsvc.setIntPref("accessibility.typeaheadfind.casesensitive", 2);
  253. ok(matchCaseCheckbox.hidden,
  254. "match case box is visible in automatic mode");
  255. ok(!matchCaseLabel.hidden,
  256. "match case label is hidden in automatic mode");
  257. enterStringIntoFindField("a");
  258. var insensitiveLabel = matchCaseLabel.value;
  259. enterStringIntoFindField("A");
  260. var sensitiveLabel = matchCaseLabel.value;
  261. ok(insensitiveLabel != sensitiveLabel,
  262. "Case Sensitive label was not correctly updated");
  263. // bug 365551
  264. gFindBar.onFindAgainCommand();
  265. ok(matchCaseCheckbox.hidden && !matchCaseLabel.hidden,
  266. "bug 365551: case sensitivity UI is broken after find-again");
  267. prefsvc.setIntPref("accessibility.typeaheadfind.casesensitive", 0);
  268. gFindBar.close();
  269. }
  270. function clearFocus() {
  271. document.commandDispatcher.focusedElement = null;
  272. document.commandDispatcher.focusedWindow = null;
  273. gBrowser.contentWindow.focus();
  274. }
  275. function testQuickFindLink() {
  276. clearFocus();
  277. var event = document.createEvent("KeyEvents");
  278. event.initKeyEvent("keypress", true, true, null, false, false,
  279. false, false, 0, "'".charCodeAt(0));
  280. gBrowser.contentDocument.documentElement.dispatchEvent(event);
  281. ok(!gFindBar.hidden, "testQuickFindLink: failed to open findbar");
  282. ok(document.commandDispatcher.focusedElement == gFindBar._findField.inputField,
  283. "testQuickFindLink: find field is not focused");
  284. var searchStr = "Link Test";
  285. enterStringIntoFindField(searchStr);
  286. ok(gFindBar._foundLink, "testQuickFindLink: failed to find sample link");
  287. }
  288. function testQuickFindText() {
  289. clearFocus();
  290. var event = document.createEvent("KeyEvents");
  291. event.initKeyEvent("keypress", true, true, null, false, false,
  292. false, false, 0, "/".charCodeAt(0));
  293. gBrowser.contentDocument.documentElement.dispatchEvent(event);
  294. ok(!gFindBar.hidden, "testQuickFindText: failed to open findbar");
  295. ok(document.commandDispatcher.focusedElement == gFindBar._findField.inputField,
  296. "testQuickFindText: find field is not focused");
  297. enterStringIntoFindField(SEARCH_TEXT);
  298. ok(gBrowser.contentWindow.getSelection() == SEARCH_TEXT,
  299. "testQuickFindText: failed to find '" + SEARCH_TEXT + "'");
  300. }
  301. ]]></script>
  302. <commandset>
  303. <command id="cmd_find" oncommand="document.getElementById('FindToolbar').onFindCommand();"/>
  304. </commandset>
  305. <browser type="content-primary" flex="1" id="content" src="about:blank"/>
  306. <findbar id="FindToolbar" browserid="content"/>
  307. </window>