toolkit/content/tests/chrome/findbar_events_window.xul

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/toolkit/content/tests/chrome/findbar_events_window.xul	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,153 @@
     1.4 +<?xml version="1.0"?>
     1.5 +
     1.6 +<!-- This Source Code Form is subject to the terms of the Mozilla Public
     1.7 +   - License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 +   - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
     1.9 +
    1.10 +<?xml-stylesheet href="chrome://global/skin" type="text/css"?>
    1.11 +
    1.12 +<window id="FindbarTest"
    1.13 +        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
    1.14 +        width="600"
    1.15 +        height="600"
    1.16 +        onload="SimpleTest.executeSoon(startTest);"
    1.17 +        title="findbar events test">
    1.18 +
    1.19 +  <script type="application/javascript"><![CDATA[
    1.20 +    const Ci = Components.interfaces;
    1.21 +    const Cc = Components.classes;
    1.22 +    const Cr = Components.results;
    1.23 +
    1.24 +    var gFindBar = null;
    1.25 +    var gBrowser;
    1.26 +
    1.27 +    var imports = ["SimpleTest", "ok"];
    1.28 +    for each (var name in imports) {
    1.29 +      window[name] = window.opener.wrappedJSObject[name];
    1.30 +    }
    1.31 +
    1.32 +    function finish() {
    1.33 +      window.close();
    1.34 +      SimpleTest.finish();
    1.35 +    }
    1.36 +
    1.37 +    function startTest() {
    1.38 +      gFindBar = document.getElementById("FindToolbar");
    1.39 +      gBrowser = document.getElementById("content");
    1.40 +      gBrowser.addEventListener("pageshow", onPageShow, false);
    1.41 +      gBrowser.loadURI('data:text/html,hello there');
    1.42 +    }
    1.43 +
    1.44 +    var tests = [
    1.45 +      testFind,
    1.46 +      testFindAgain,
    1.47 +      testCaseSensitivity,
    1.48 +      testHighlight,
    1.49 +      finish
    1.50 +    ];
    1.51 +
    1.52 +    // Iterates through the above tests and takes care of passing the done
    1.53 +    // callback for any async tests.
    1.54 +    function nextTest() {
    1.55 +      if (!tests.length) {
    1.56 +        return;
    1.57 +      }
    1.58 +      var func = tests.shift();
    1.59 +      if (!func.length) {
    1.60 +        // Test isn't async advance to the next test here.
    1.61 +        func();
    1.62 +        SimpleTest.executeSoon(nextTest);
    1.63 +      } else {
    1.64 +        func(nextTest);
    1.65 +      }
    1.66 +    }
    1.67 +
    1.68 +    function onPageShow() {
    1.69 +      gFindBar.open();
    1.70 +      gFindBar.onFindCommand();
    1.71 +      nextTest();
    1.72 +    }
    1.73 +
    1.74 +    function checkSelection(done) {
    1.75 +      SimpleTest.executeSoon(function() {
    1.76 +        var selected = gBrowser.contentWindow.getSelection();
    1.77 +        ok(selected == "", "No text is selected");
    1.78 +
    1.79 +        var controller = gFindBar.browser.docShell.QueryInterface(Ci.nsIInterfaceRequestor)
    1.80 +                                 .getInterface(Ci.nsISelectionDisplay)
    1.81 +                                 .QueryInterface(Ci.nsISelectionController);
    1.82 +        var selection = controller.getSelection(controller.SELECTION_FIND);
    1.83 +        ok(selection.rangeCount == 0, "No text is highlighted");
    1.84 +        done();
    1.85 +      });
    1.86 +    }
    1.87 +
    1.88 +    function testFind(done) {
    1.89 +      var findTriggered = false;
    1.90 +      var query = "t";
    1.91 +      gFindBar.addEventListener("find", function(e) {
    1.92 +        eventTriggered = true;
    1.93 +        ok(e.detail.query === query, "find event query should match '" + query + "'");
    1.94 +        e.preventDefault();
    1.95 +        // Since we're preventing the default make sure nothing was selected.
    1.96 +        checkSelection(done);
    1.97 +      });
    1.98 +
    1.99 +      // Put some text in the find box.
   1.100 +      var event = document.createEvent("KeyEvents");
   1.101 +      event.initKeyEvent("keypress", true, true, null, false, false,
   1.102 +                         false, false, 0, query.charCodeAt(0));
   1.103 +      gFindBar._findField.inputField.dispatchEvent(event);
   1.104 +      ok(eventTriggered, "find event should be triggered");
   1.105 +    }
   1.106 +
   1.107 +    function testFindAgain(done) {
   1.108 +      var eventTriggered = false;
   1.109 +      gFindBar.addEventListener("findagain", function(e) {
   1.110 +        eventTriggered = true;
   1.111 +        e.preventDefault();
   1.112 +        // Since we're preventing the default make sure nothing was selected.
   1.113 +        checkSelection(done);
   1.114 +      });
   1.115 +
   1.116 +      gFindBar.onFindAgainCommand();
   1.117 +      ok(eventTriggered, "findagain event should be triggered");
   1.118 +    }
   1.119 +
   1.120 +    function testCaseSensitivity() {
   1.121 +      var eventTriggered = false;
   1.122 +      gFindBar.addEventListener("findcasesensitivitychange", function(e) {
   1.123 +        eventTriggered = true;
   1.124 +        ok(e.detail.caseSensitive, "find should be case sensitive");
   1.125 +      });
   1.126 +
   1.127 +      var matchCaseCheckbox = gFindBar.getElement("find-case-sensitive");
   1.128 +      matchCaseCheckbox.click();
   1.129 +      ok(eventTriggered, "findcasesensitivitychange should be triggered");
   1.130 +    }
   1.131 +
   1.132 +    function testHighlight(done) {
   1.133 +      // Update the find state so the highlight button is clickable.
   1.134 +      gFindBar.updateControlState(Ci.nsITypeAheadFind.FIND_FOUND, false);
   1.135 +      var eventTriggered = false;
   1.136 +      gFindBar.addEventListener("findhighlightallchange", function(e) {
   1.137 +        eventTriggered = true;
   1.138 +        ok(e.detail.highlightAll, "find event should have highlight all set");
   1.139 +        e.preventDefault();
   1.140 +        // Since we're preventing the default make sure nothing was highlighted.
   1.141 +        SimpleTest.executeSoon(function() {
   1.142 +          checkSelection(done);
   1.143 +        });
   1.144 +      });
   1.145 +
   1.146 +      var highlightButton = gFindBar.getElement("highlight");
   1.147 +      if (!highlightButton.checked) {
   1.148 +        highlightButton.click();
   1.149 +      }
   1.150 +      ok(eventTriggered, "findhighlightallchange should be triggered");
   1.151 +    }
   1.152 +  ]]></script>
   1.153 +
   1.154 +  <browser type="content-primary" flex="1" id="content" src="about:blank"/>
   1.155 +  <findbar id="FindToolbar" browserid="content"/>
   1.156 +</window>

mercurial