summaryrefslogtreecommitdiffstats
path: root/toolkit/content/tests/chrome/findbar_events_window.xhtml
blob: bc02ef6bcca591b836231ed5ac7d4da5ac2a4206 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
<?xml version="1.0"?>

<!-- This Source Code Form is subject to the terms of the Mozilla Public
   - License, v. 2.0. If a copy of the MPL was not distributed with this
   - file, You can obtain one at http://mozilla.org/MPL/2.0/. -->

<?xml-stylesheet href="chrome://global/skin" type="text/css"?>
<?xml-stylesheet
  href="chrome://mochikit/content/tests/SimpleTest/test.css"
  type="text/css"?>

<window id="FindbarTest"
        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
        width="600"
        height="600"
        onload="SimpleTest.executeSoon(startTest);"
        title="findbar events test">

  <script type="application/javascript"><![CDATA[
    const {BrowserTestUtils} = ChromeUtils.importESModule(
      "resource://testing-common/BrowserTestUtils.sys.mjs"
    );

    var gFindBar = null;
    var gBrowser;
    const kTimeout = 5000; // 5 seconds.

    var SimpleTest = window.arguments[0].SimpleTest;
    var ok = window.arguments[0].ok;
    var is = window.arguments[0].is;
    var info = window.arguments[0].info;
    SimpleTest.requestLongerTimeout(2);

    function startTest() {
      (async function() {
        gFindBar = document.getElementById("FindToolbar");
        for (let browserId of ["content", "content-remote"]) {
          await startTestWithBrowser(browserId);
        }
      })().then(() => {
        window.close();
        SimpleTest.finish();
      });
    }

    async function startTestWithBrowser(browserId) {
      info("Starting test with browser '" + browserId + "'");
      gBrowser = document.getElementById(browserId);
      const url = "data:text/html,hello there";
      let promise = BrowserTestUtils.browserLoaded(gBrowser, false, url);
      BrowserTestUtils.startLoadingURIString(gBrowser, url);
      await promise;
      gFindBar.browser = gBrowser;
      await onDocumentLoaded();
    }

    async function onDocumentLoaded() {
      gFindBar.open();
      gFindBar.onFindCommand();

      await testFind();
      await testFindAgain();
      await testCaseSensitivity();
      await testDiacriticMatching();
      await testHighlight();
    }

    function checkSelection() {
      return new Promise(resolve => {
        SimpleTest.executeSoon(() => {
          SpecialPowers.spawn(gBrowser, [], async function() {
            let selected = content.getSelection();
            Assert.equal(String(selected), "", "No text is selected");

            let controller = docShell.QueryInterface(Ci.nsIInterfaceRequestor)
                                       .getInterface(Ci.nsISelectionDisplay)
                                       .QueryInterface(Ci.nsISelectionController);
            let selection = controller.getSelection(controller.SELECTION_FIND);
            Assert.equal(selection.rangeCount, 0, "No text is highlighted");
          }).then(resolve);
        });
      });
    }

    function once(node, eventName, preventDefault = true) {
      return new Promise((resolve, reject) => {
        let timeout = window.setTimeout(() => {
          reject("Event wasn't fired within " + kTimeout + "ms for event '" +
            eventName + "'.");
        }, kTimeout);

        node.addEventListener(eventName, function clb(e) {
          window.clearTimeout(timeout);
          node.removeEventListener(eventName, clb);
          if (preventDefault)
            e.preventDefault();
          resolve(e);
        });
      });
    }

    async function testFind() {
      info("Testing normal find.");
      let query = "t";
      let promise = once(gFindBar, "find");

      // Put some text in the find box.
      let event = new KeyboardEvent("keypress", {
        bubbles: true,
        cancelable: true,
        view: null,
        keyCode: 0,
        charCode: query.charCodeAt(0),
      });
      gFindBar._findField.dispatchEvent(event);

      let e = await promise;
      ok(e.detail.query === query, "find event query should match '" + query + "'");
      // Since we're preventing the default make sure nothing was selected.
      await checkSelection();
    }

    async function testFindAgain() {
      info("Testing repeating normal find.");
      let promise = once(gFindBar, "findagain");

      gFindBar.onFindAgainCommand();

      await promise;
      // Since we're preventing the default make sure nothing was selected.
      await checkSelection();
    }

    async function testCaseSensitivity() {
      info("Testing normal case sensitivity.");
      let promise = once(gFindBar, "findcasesensitivitychange", false);

      let matchCaseCheckbox = gFindBar.getElement("find-case-sensitive");
      matchCaseCheckbox.click();

      let e = await promise;
      ok(e.detail.caseSensitive, "find should be case sensitive");

      // Toggle it back to the original setting.
      matchCaseCheckbox.click();

      // Changing case sensitivity does the search so clear the selected text
      // before the next test.
      await SpecialPowers.spawn(gBrowser, [], () => content.getSelection().removeAllRanges());
    }

    async function testDiacriticMatching() {
      info("Testing normal diacritic matching.");
      let promise = once(gFindBar, "finddiacriticmatchingchange", false);

      let matchDiacriticsCheckbox = gFindBar.getElement("find-match-diacritics");
      matchDiacriticsCheckbox.click();

      let e = await promise;
      ok(e.detail.matchDiacritics, "find should match diacritics");

      // Toggle it back to the original setting.
      matchDiacriticsCheckbox.click();

      // Changing diacritic matching does the search so clear the selected text
      // before the next test.
      await SpecialPowers.spawn(gBrowser, [], () => content.getSelection().removeAllRanges());
    }

    async function testHighlight() {
      info("Testing find with highlight all.");
      // Update the find state so the highlight button is clickable.
      gFindBar.updateControlState(Ci.nsITypeAheadFind.FIND_FOUND, false);

      let promise = once(gFindBar, "findhighlightallchange");

      let highlightButton = gFindBar.getElement("highlight");
      if (!highlightButton.checked)
        highlightButton.click();

      let e = await promise;
      ok(e.detail.highlightAll, "find event should have highlight all set");
      // Since we're preventing the default make sure nothing was highlighted.
      await checkSelection();

      // Toggle it back to the original setting.
      if (highlightButton.checked)
        highlightButton.click();
    }
  ]]></script>

  <browser type="content" primary="true" flex="1" id="content" messagemanagergroup="test" src="about:blank"/>
  <browser type="content" primary="true" flex="1" id="content-remote" remote="true" messagemanagergroup="test" src="about:blank"/>
  <findbar id="FindToolbar" browserid="content"/>
</window>