summaryrefslogtreecommitdiffstats
path: root/widget/tests/browser/browser_test_ContentCache.js
blob: 1859e682ec9bdf8373c8cad0b8e35f6953c654c7 (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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
/* Any copyright is dedicated to the Public Domain.
   http://creativecommons.org/publicdomain/zero/1.0/ */

"use strict";

add_task(async function() {
  const TIP = Cc["@mozilla.org/text-input-processor;1"].createInstance(
    Ci.nsITextInputProcessor
  );
  let notifications = [];
  const observer = (aTIP, aNotification) => {
    switch (aNotification.type) {
      case "request-to-commit":
        aTIP.commitComposition();
        break;
      case "request-to-cancel":
        aTIP.cancelComposition();
        break;
      case "notify-end-input-transaction":
      case "notify-focus":
      case "notify-blur":
      case "notify-text-change":
      case "notify-selection-change":
        notifications.push(aNotification);
        break;
    }
    return true;
  };

  function checkNotifications(aExpectedNotifications, aDescription) {
    for (const expectedNotification of aExpectedNotifications) {
      const notification = notifications.find(
        element => element.type == expectedNotification.type
      );
      if (expectedNotification.expected) {
        isnot(
          notification,
          undefined,
          `"${expectedNotification.type}" should be notified ${aDescription}`
        );
      } else {
        is(
          notification,
          undefined,
          `"${expectedNotification.type}" should not be notified ${aDescription}`
        );
      }
    }
  }

  ok(
    TIP.beginInputTransaction(window, observer),
    "nsITextInputProcessor.beingInputTransaction should return true"
  );
  ok(
    TIP.beginInputTransactionForTests(window, observer),
    "nsITextInputProcessor.beginInputTransactionForTests should return true"
  );

  await BrowserTestUtils.withNewTab(
    "https://example.com/browser/toolkit/content/tests/browser/file_empty.html",
    async function(browser) {
      ok(browser.isRemoteBrowser, "This test passes only in e10s mode");

      // IMEContentObserver flushes pending IME notifications at next vsync
      // after something happens.  Therefore, after doing something in content
      // process, we need to guarantee that IMEContentObserver has a change to
      // send IME notifications to the main process with calling this function.
      function waitForSendingIMENotificationsInContent() {
        return SpecialPowers.spawn(browser, [], async () => {
          await new Promise(resolve =>
            content.requestAnimationFrame(() =>
              content.requestAnimationFrame(resolve)
            )
          );
        });
      }

      /**
       * Test when empty editor gets focus
       */
      notifications = [];
      await SpecialPowers.spawn(browser, [], () => {
        content.document.body.innerHTML = "<div contenteditable><br></div>";
        const editor = content.document.querySelector("div[contenteditable]");
        editor.focus();
      });

      await waitForSendingIMENotificationsInContent();

      (function() {
        checkNotifications(
          [
            { type: "notify-focus", expected: true },
            { type: "notify-blur", expected: false },
            { type: "notify-end-input-transaction", expected: false },
            { type: "notify-text-change", expected: false },
            { type: "notify-selection-change", expected: false },
          ],
          "after empty editor gets focus"
        );
        const text = EventUtils.synthesizeQueryTextContent(0, 1000);
        ok(
          text?.succeeded,
          "query text content should succeed after empty editor gets focus"
        );
        if (text?.succeeded) {
          is(
            text.text.replace(/[\r\n]/g, ""),
            "",
            "text should be only line breaks after empty editor gets focus"
          );
        }
        const selection = EventUtils.synthesizeQuerySelectedText();
        ok(
          selection?.succeeded,
          "query selected text should succeed after empty editor gets focus"
        );
        if (selection?.succeeded) {
          ok(
            !selection.notFound,
            "query selected text should find a selection range after empty editor gets focus"
          );
          if (!selection.notFound) {
            is(
              selection.text,
              "",
              "selection should be collapsed after empty editor gets focus"
            );
          }
        }
      })();

      /**
       * Test when there is non-collapsed selection
       */
      notifications = [];
      await SpecialPowers.spawn(browser, [], () => {
        const editor = content.document.querySelector("div[contenteditable]");
        editor.innerHTML = "<p>abc</p><p>def</p>";
        content
          .getSelection()
          .setBaseAndExtent(
            editor.querySelector("p").firstChild,
            2,
            editor.querySelector("p + p").firstChild,
            1
          );
      });

      await waitForSendingIMENotificationsInContent();

      (function() {
        checkNotifications(
          [
            { type: "notify-focus", expected: false },
            { type: "notify-blur", expected: false },
            { type: "notify-end-input-transaction", expected: false },
            { type: "notify-text-change", expected: true },
            { type: "notify-selection-change", expected: true },
          ],
          "after modifying focused editor"
        );
        const text = EventUtils.synthesizeQueryTextContent(0, 1000);
        ok(
          text?.succeeded,
          "query text content should succeed after modifying focused editor"
        );
        if (text?.succeeded) {
          is(
            text.text
              .trim()
              .replace(/\r\n/g, "\n")
              .replace(/\n\n+/g, "\n"),
            "abc\ndef",
            "text should include the both paragraph's text after modifying focused editor"
          );
        }
        const selection = EventUtils.synthesizeQuerySelectedText();
        ok(
          selection?.succeeded,
          "query selected text should succeed after modifying focused editor"
        );
        if (selection?.succeeded) {
          ok(
            !selection.notFound,
            "query selected text should find a selection range after modifying focused editor"
          );
          if (!selection.notFound) {
            is(
              selection.text
                .trim()
                .replace(/\r\n/g, "\n")
                .replace(/\n\n+/g, "\n"),
              "c\nd",
              "selection should have the selected characters in the both paragraphs after modifying focused editor"
            );
          }
        }
      })();

      /**
       * Test when there is no selection ranges
       */
      notifications = [];
      await SpecialPowers.spawn(browser, [], () => {
        content.getSelection().removeAllRanges();
      });

      await waitForSendingIMENotificationsInContent();

      (function() {
        checkNotifications(
          [
            { type: "notify-focus", expected: false },
            { type: "notify-blur", expected: false },
            { type: "notify-end-input-transaction", expected: false },
            { type: "notify-text-change", expected: false },
            { type: "notify-selection-change", expected: true },
          ],
          "after removing all selection ranges from the focused editor"
        );
        const text = EventUtils.synthesizeQueryTextContent(0, 1000);
        ok(
          text?.succeeded,
          "query text content should succeed after removing all selection ranges from the focused editor"
        );
        if (text?.succeeded) {
          is(
            text.text
              .trim()
              .replace(/\r\n/g, "\n")
              .replace(/\n\n+/g, "\n"),
            "abc\ndef",
            "text should include the both paragraph's text after removing all selection ranges from the focused editor"
          );
        }
        const selection = EventUtils.synthesizeQuerySelectedText();
        ok(
          selection?.succeeded,
          "query selected text should succeed after removing all selection ranges from the focused editor"
        );
        if (selection?.succeeded) {
          ok(
            selection.notFound,
            "query selected text should find no selection range after removing all selection ranges from the focused editor"
          );
        }
      })();

      /**
       * Test when no editable element has focus.
       */
      notifications = [];
      await SpecialPowers.spawn(browser, [], () => {
        content.document.body.innerHTML = "abcdef";
      });

      await waitForSendingIMENotificationsInContent();

      (function() {
        checkNotifications(
          [
            { type: "notify-focus", expected: false },
            { type: "notify-blur", expected: true },
          ],
          "removing editor should make ContentCacheInParent not have any data"
        );
        const text = EventUtils.synthesizeQueryTextContent(0, 1000);
        ok(
          !text?.succeeded,
          "query text content should fail because no editable element has focus"
        );
        const selection = EventUtils.synthesizeQuerySelectedText();
        ok(
          !selection?.succeeded,
          "query selected text should fail because no editable element has focus"
        );
        const caret = EventUtils.synthesizeQueryCaretRect(0);
        ok(
          !caret?.succeeded,
          "query caret rect should fail because no editable element has focus"
        );
        const textRect = EventUtils.synthesizeQueryTextRect(0, 5, false);
        ok(
          !textRect?.succeeded,
          "query text rect should fail because no editable element has focus"
        );
        const textRectArray = EventUtils.synthesizeQueryTextRectArray(0, 5);
        ok(
          !textRectArray?.succeeded,
          "query text rect array should fail because no editable element has focus"
        );
        const editorRect = EventUtils.synthesizeQueryEditorRect();
        todo(
          !editorRect?.succeeded,
          "query editor rect should fail because no editable element has focus"
        );
      })();
    }
  );
});