PageRenderTime 26ms CodeModel.GetById 20ms app.highlight 2ms RepoModel.GetById 1ms app.codeStats 0ms

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