summaryrefslogtreecommitdiffstats
path: root/toolkit/content/tests/chrome/bug451540_window.xhtml
blob: 64bb3e033a820ffbb361ba5ee8a2c0d7a2536316 (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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
<?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"?>

<window id="451540test"
        xmlns="http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul"
        width="600"
        height="600"
        title="451540 test">

  <script type="application/javascript"><![CDATA[
    const {BrowserTestUtils} = ChromeUtils.import("resource://testing-common/BrowserTestUtils.jsm");
    const SEARCH_TEXT = "minefield";

    let gFindBar = null;
    let gBrowser;

    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() {
      gFindBar = document.getElementById("FindToolbar");
      gBrowser = document.getElementById("content");
      gBrowser.addEventListener("pageshow", onPageShow, { once: true });
      let data = `data:text/html,<input id="inp" type="text" />
                                 <textarea id="tarea"/>`;
      BrowserTestUtils.loadURIString(gBrowser, data);
    }

    function promiseHighlightFinished() {
      return new Promise(resolve => {
        let listener = {
          onHighlightFinished() {
            gFindBar.browser.finder.removeResultListener(listener);
            resolve();
          }
        };
        gFindBar.browser.finder.addResultListener(listener);
      });
    }

    async function resetForNextTest(elementId, aText) {
      if (!aText)
        aText = SEARCH_TEXT;

      // Turn off highlighting
      let highlightButton = gFindBar.getElement("highlight");
      if (highlightButton.checked) {
        highlightButton.click();
      }

      // Initialise input
      info(`setting element value to ${aText}`);
      await SpecialPowers.spawn(gBrowser, [{elementId, aText}], async function(args) {
        let {elementId, aText} = args;
        let doc = content.document;
        let element = doc.getElementById(elementId);
        element.value = aText;
        element.focus();
      });
      info(`just set element value to ${aText}`);
      gFindBar._findField.value = SEARCH_TEXT;

      // Perform search and turn on highlighting
      gFindBar._find();
      highlightButton.click();
      await promiseHighlightFinished();

      // Move caret to start of element
      info(`focusing element`);
      await SpecialPowers.spawn(gBrowser, [elementId], async function(elementId) {
        let doc = content.document;
        let element = doc.getElementById(elementId);
        element.focus();
      });
      info(`focused element`);
      if (navigator.platform.includes("Mac")) {
        await BrowserTestUtils.synthesizeKey("KEY_ArrowLeft", { metaKey: true }, gBrowser);
      } else {
        await BrowserTestUtils.synthesizeKey("KEY_Home", {}, gBrowser);
      }
    }

    async function testSelection(elementId, expectedRangeCount, message) {
      await SpecialPowers.spawn(gBrowser, [{elementId, expectedRangeCount, message}], async function(args) {
        let {elementId, expectedRangeCount, message} = args;
        let doc = content.document;
        let element = doc.getElementById(elementId);
        let controller = element.editor.selectionController;
        let selection = controller.getSelection(controller.SELECTION_FIND);
        Assert.equal(selection.rangeCount, expectedRangeCount, message);
      });
    }

    async function testInput(elementId, testTypeText) {
      let isEditableElement = await SpecialPowers.spawn(gBrowser, [elementId], async function(elementId) {
        let doc = content.document;
        let element = doc.getElementById(elementId);
        let elementClass = ChromeUtils.getClassName(element);
        return elementClass === "HTMLInputElement" ||
               elementClass === "HTMLTextAreaElement";
      });
      if (!isEditableElement) {
        return;
      }

      let moveCaretToNextWordBoundary = async (aBrowser) => {
        if (!navigator.platform.includes("Mac")) {
          return BrowserTestUtils.synthesizeKey("KEY_ArrowRight", { accelKey: true }, aBrowser);
        }
        // macOS does not have default shortcut key to move caret per word.
        return SpecialPowers.spawn(aBrowser, [], async () => {
          content.docShell.doCommand("cmd_wordNext");
        });
      };

      // Initialize the findbar
      let matchCase = gFindBar.getElement("find-case-sensitive");
      if (matchCase.checked) {
        matchCase.doCommand();
      }

      // First check match has been correctly highlighted
      await resetForNextTest(elementId);

      await testSelection(elementId, 1, testTypeText + " correctly highlighted match");

      // Test 2: check highlight removed when text added within the highlight
      await BrowserTestUtils.synthesizeKey("KEY_ArrowRight", {}, gBrowser);
      await BrowserTestUtils.synthesizeKey("a", {}, gBrowser);

      await testSelection(elementId, 0, testTypeText + " correctly removed highlight on text insertion");

      // Test 3: check highlighting remains when text added before highlight
      await resetForNextTest(elementId);
      await BrowserTestUtils.synthesizeKey("a", {}, gBrowser);
      await testSelection(elementId, 1, testTypeText + " highlight correctly remained on text insertion at start");

      //  Test 4: check highlighting remains when text added after highlight
      await resetForNextTest(elementId);
      for (let x = 0; x < SEARCH_TEXT.length; x++) {
        await BrowserTestUtils.synthesizeKey("KEY_ArrowRight", {}, gBrowser);
      }
      await BrowserTestUtils.synthesizeKey("a", {}, gBrowser);
      await testSelection(elementId, 1, testTypeText + " highlight correctly remained on text insertion at end");

      // Test 5: deleting text within the highlight
      await resetForNextTest(elementId);
      await BrowserTestUtils.synthesizeKey("KEY_ArrowRight", {}, gBrowser);
      await BrowserTestUtils.synthesizeKey("KEY_Backspace", {}, gBrowser);
      await testSelection(elementId, 0, testTypeText + " correctly removed highlight on text deletion");

      // Test 6: deleting text at end of highlight
      await resetForNextTest(elementId, SEARCH_TEXT + "A");
      for (let x = 0; x < (SEARCH_TEXT + "A").length; x++) {
        await BrowserTestUtils.synthesizeKey("KEY_ArrowRight", {}, gBrowser);
      }
      await BrowserTestUtils.synthesizeKey("KEY_Backspace", {}, gBrowser);
      await testSelection(elementId, 1, testTypeText + " highlight correctly remained on text deletion at end");

      // Test 7: deleting text at start of highlight
      await resetForNextTest(elementId, "A" + SEARCH_TEXT);
      await BrowserTestUtils.synthesizeKey("KEY_ArrowRight", {}, gBrowser);
      await BrowserTestUtils.synthesizeKey("KEY_Backspace", {}, gBrowser);
      await testSelection(elementId, 1, testTypeText + " highlight correctly remained on text deletion at start");

      // Test 8: deleting selection
      await resetForNextTest(elementId);
      await BrowserTestUtils.synthesizeKey("KEY_ArrowRight", { shiftKey: true }, gBrowser);
      await BrowserTestUtils.synthesizeKey("KEY_ArrowRight", { shiftKey: true }, gBrowser);
      await BrowserTestUtils.synthesizeKey("x", { accelKey: true }, gBrowser);
      await testSelection(elementId, 0, testTypeText + " correctly removed highlight on selection deletion");

      // Test 9: Multiple matches within one editor (part 1)
      // Check second match remains highlighted after inserting text into
      // first match, and that its highlighting gets removed when the
      // second match is edited
      await resetForNextTest(elementId, SEARCH_TEXT + " " + SEARCH_TEXT);
      await testSelection(elementId, 2, testTypeText + " correctly highlighted both matches");
      await BrowserTestUtils.synthesizeKey("KEY_ArrowRight", {}, gBrowser);
      await BrowserTestUtils.synthesizeKey("a", {}, gBrowser);
      await testSelection(elementId, 1, testTypeText + " correctly removed only the first highlight on text insertion");
      await moveCaretToNextWordBoundary(gBrowser);
      await moveCaretToNextWordBoundary(gBrowser);
      await BrowserTestUtils.synthesizeKey("KEY_ArrowLeft", {}, gBrowser);
      await BrowserTestUtils.synthesizeKey("a", {}, gBrowser);
      await testSelection(elementId, 0, testTypeText + " correctly removed second highlight on text insertion");

      // Test 10: Multiple matches within one editor (part 2)
      // Check second match remains highlighted after deleting text in
      // first match, and that its highlighting gets removed when the
      // second match is edited
      await resetForNextTest(elementId, SEARCH_TEXT + " " + SEARCH_TEXT);
      await testSelection(elementId, 2, testTypeText + " correctly highlighted both matches");
      await BrowserTestUtils.synthesizeKey("KEY_ArrowRight", {}, gBrowser);
      await BrowserTestUtils.synthesizeKey("KEY_Backspace", {}, gBrowser);
      await testSelection(elementId, 1, testTypeText + " correctly removed only the first highlight on text deletion");
      await moveCaretToNextWordBoundary(gBrowser);
      await moveCaretToNextWordBoundary(gBrowser);
      await BrowserTestUtils.synthesizeKey("KEY_ArrowLeft", {}, gBrowser);
      await BrowserTestUtils.synthesizeKey("KEY_Backspace", {}, gBrowser);
      await testSelection(elementId, 0, testTypeText + " correctly removed second highlight on text deletion");

      // Test 11: Multiple matches within one editor (part 3)
      // Check second match remains highlighted after deleting selection
      // in first match, and that second match highlighting gets correctly
      // removed when it has a selection deleted from it
      await resetForNextTest(elementId, SEARCH_TEXT + " " + SEARCH_TEXT);
      await BrowserTestUtils.synthesizeKey("KEY_ArrowRight", { shiftKey: true }, gBrowser);
      await BrowserTestUtils.synthesizeKey("KEY_ArrowRight", { shiftKey: true }, gBrowser);
      await BrowserTestUtils.synthesizeKey("x", { accelKey: true }, gBrowser);
      await testSelection(elementId, 1, testTypeText + " correctly removed only first highlight on selection deletion");
      await moveCaretToNextWordBoundary(gBrowser);
      await moveCaretToNextWordBoundary(gBrowser);
      await BrowserTestUtils.synthesizeKey("KEY_ArrowLeft", { shiftKey: true }, gBrowser);
      await BrowserTestUtils.synthesizeKey("KEY_ArrowLeft", { shiftKey: true }, gBrowser);
      await BrowserTestUtils.synthesizeKey("x", { accelKey: true }, gBrowser);
      await testSelection(elementId, 0, testTypeText + " correctly removed second highlight on selection deletion");

      // Turn off highlighting
      let highlightButton = gFindBar.getElement("highlight");
      if (highlightButton.checked) {
        highlightButton.click();
      }
    }

    function onPageShow() {
      (async function() {
        gFindBar.open();
        await testInput("inp", "Input:");
        await testInput("tarea", "Textarea:");
      })().then(() => {
        window.close();
        SimpleTest.finish();
      });
    }

    SimpleTest.waitForFocus(startTest, window);
  ]]></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>