summaryrefslogtreecommitdiffstats
path: root/editor/libeditor/tests/browser_content_command_insert_text.js
blob: 073b6f830e587be422f3502ca067fa139a6663ad (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
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

const { CustomizableUITestUtils } = ChromeUtils.importESModule(
  "resource://testing-common/CustomizableUITestUtils.sys.mjs"
);
const { ContentTaskUtils } = ChromeUtils.importESModule(
  "resource://testing-common/ContentTaskUtils.sys.mjs"
);
const gCUITestUtils = new CustomizableUITestUtils(window);
const gDOMWindowUtils = EventUtils._getDOMWindowUtils(window);

add_task(async function test_setup() {
  await gCUITestUtils.addSearchBar();
  registerCleanupFunction(() => {
    gCUITestUtils.removeSearchBar();
  });
});

function promiseResettingSearchBarAndFocus() {
  const waitForFocusInSearchBar = BrowserTestUtils.waitForEvent(
    BrowserSearch.searchBar.textbox,
    "focus"
  );
  BrowserSearch.searchBar.textbox.focus();
  BrowserSearch.searchBar.textbox.value = "";
  return Promise.all([
    waitForFocusInSearchBar,
    TestUtils.waitForCondition(
      () =>
        gDOMWindowUtils.IMEStatus === Ci.nsIDOMWindowUtils.IME_STATUS_ENABLED &&
        gDOMWindowUtils.inputContextOrigin ===
          Ci.nsIDOMWindowUtils.INPUT_CONTEXT_ORIGIN_MAIN
    ),
  ]);
}

function promiseIMEStateEnabledByRemote() {
  return TestUtils.waitForCondition(
    () =>
      gDOMWindowUtils.IMEStatus === Ci.nsIDOMWindowUtils.IME_STATUS_ENABLED &&
      gDOMWindowUtils.inputContextOrigin ===
        Ci.nsIDOMWindowUtils.INPUT_CONTEXT_ORIGIN_CONTENT
  );
}

function promiseContentTick(browser) {
  return SpecialPowers.spawn(browser, [], async () => {
    await new Promise(r => {
      content.requestAnimationFrame(() => {
        content.requestAnimationFrame(r);
      });
    });
  });
}

add_task(async function test_text_editor_in_chrome() {
  await promiseResettingSearchBarAndFocus();

  let events = [];
  function logEvent(event) {
    events.push(event);
  }
  BrowserSearch.searchBar.textbox.addEventListener("beforeinput", logEvent);
  gDOMWindowUtils.sendContentCommandEvent("insertText", null, "XYZ");

  is(
    BrowserSearch.searchBar.textbox.value,
    "XYZ",
    "The string should be inserted into the focused search bar"
  );
  is(
    events.length,
    1,
    "One beforeinput event should be fired in the searchbar"
  );
  is(events[0]?.inputType, "insertText", 'inputType should be "insertText"');
  is(events[0]?.data, "XYZ", 'inputType should be "XYZ"');
  is(events[0]?.cancelable, true, "beforeinput event should be cancelable");
  BrowserSearch.searchBar.textbox.removeEventListener("beforeinput", logEvent);

  BrowserSearch.searchBar.textbox.blur();
});

add_task(async function test_text_editor_in_content() {
  for (const test of [
    {
      tag: "input",
      target: "input",
      nonTarget: "input + input",
      page: 'data:text/html,<input value="abc"><input value="def">',
    },
    {
      tag: "textarea",
      target: "textarea",
      nonTarget: "textarea + textarea",
      page: "data:text/html,<textarea>abc</textarea><textarea>def</textarea>",
    },
  ]) {
    // Once, move focus to chrome's searchbar.
    await promiseResettingSearchBarAndFocus();

    await BrowserTestUtils.withNewTab(test.page, async browser => {
      await SpecialPowers.spawn(browser, [test], async function (aTest) {
        content.window.focus();
        await ContentTaskUtils.waitForCondition(() =>
          content.document.hasFocus()
        );
        const target = content.document.querySelector(aTest.target);
        target.focus();
        target.selectionStart = target.selectionEnd = 2;
        content.document.documentElement.scrollTop; // Flush pending things
      });

      await promiseIMEStateEnabledByRemote();
      const waitForBeforeInputEvent = SpecialPowers.spawn(
        browser,
        [test],
        async function (aTest) {
          await new Promise(resolve => {
            content.document.querySelector(aTest.target).addEventListener(
              "beforeinput",
              event => {
                is(
                  event.inputType,
                  "insertText",
                  `The inputType of beforeinput event fired on <${aTest.target}> should be "insertText"`
                );
                is(
                  event.data,
                  "XYZ",
                  `The data of beforeinput event fired on <${aTest.target}> should be "XYZ"`
                );
                is(
                  event.cancelable,
                  true,
                  `The beforeinput event fired on <${aTest.target}> should be cancelable`
                );
                resolve();
              },
              { once: true }
            );
          });
        }
      );
      const waitForInputEvent = BrowserTestUtils.waitForContentEvent(
        browser,
        "input"
      );
      await promiseContentTick(browser); // Ensure "input" event listener in the remote process
      gDOMWindowUtils.sendContentCommandEvent("insertText", null, "XYZ");
      await waitForBeforeInputEvent;
      await waitForInputEvent;

      await SpecialPowers.spawn(browser, [test], async function (aTest) {
        is(
          content.document.querySelector(aTest.target).value,
          "abXYZc",
          `The string should be inserted into the focused <${aTest.target}> element`
        );
        is(
          content.document.querySelector(aTest.nonTarget).value,
          "def",
          `The string should not be inserted into the non-focused <${aTest.nonTarget}> element`
        );
      });
    });

    is(
      BrowserSearch.searchBar.textbox.value,
      "",
      "The string should not be inserted into the previously focused search bar"
    );
  }
});

add_task(async function test_html_editor_in_content() {
  for (const test of [
    {
      mode: "contenteditable",
      target: "div",
      page: "data:text/html,<div contenteditable>abc</div>",
    },
    {
      mode: "designMode",
      target: "div",
      page: "data:text/html,<div>abc</div>",
    },
  ]) {
    // Once, move focus to chrome's searchbar.
    await promiseResettingSearchBarAndFocus();

    await BrowserTestUtils.withNewTab(test.page, async browser => {
      await SpecialPowers.spawn(browser, [test], async function (aTest) {
        content.window.focus();
        await ContentTaskUtils.waitForCondition(() =>
          content.document.hasFocus()
        );
        const target = content.document.querySelector(aTest.target);
        if (aTest.mode == "designMode") {
          content.document.designMode = "on";
          content.window.focus();
        } else {
          target.focus();
        }
        content.window.getSelection().collapse(target.firstChild, 2);
        content.document.documentElement.scrollTop; // Flush pending things
      });

      await promiseIMEStateEnabledByRemote();
      const waitForBeforeInputEvent = SpecialPowers.spawn(
        browser,
        [test],
        async function (aTest) {
          await new Promise(resolve => {
            const eventTarget =
              aTest.mode === "designMode"
                ? content.document
                : content.document.querySelector(aTest.target);
            eventTarget.addEventListener(
              "beforeinput",
              event => {
                is(
                  event.inputType,
                  "insertText",
                  `The inputType of beforeinput event fired on ${aTest.mode} editor should be "insertText"`
                );
                is(
                  event.data,
                  "XYZ",
                  `The data of beforeinput event fired on ${aTest.mode} editor should be "XYZ"`
                );
                is(
                  event.cancelable,
                  true,
                  `The beforeinput event fired on ${aTest.mode} editor should be cancelable`
                );
                resolve();
              },
              { once: true }
            );
          });
        }
      );
      const waitForInputEvent = BrowserTestUtils.waitForContentEvent(
        browser,
        "input"
      );
      await promiseContentTick(browser); // Ensure "input" event listener in the remote process
      gDOMWindowUtils.sendContentCommandEvent("insertText", null, "XYZ");
      await waitForBeforeInputEvent;
      await waitForInputEvent;

      await SpecialPowers.spawn(browser, [test], async function (aTest) {
        is(
          content.document.querySelector(aTest.target).innerHTML,
          "abXYZc",
          `The string should be inserted into the focused ${aTest.mode} editor`
        );
      });
    });

    is(
      BrowserSearch.searchBar.textbox.value,
      "",
      "The string should not be inserted into the previously focused search bar"
    );
  }
});