summaryrefslogtreecommitdiffstats
path: root/dom/base/test/chrome/test_nsITextInputProcessorCallback_at_changing_default_value_of_textarea.html
blob: 179ffee08625f8c65a3405fa0370336f551f0837 (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
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Text change notifications at updating default value of non-dirty textarea</title>
<script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
<script>
"use strict";

SimpleTest.waitForExplicitFinish();
SimpleTest.waitForFocus(async () => {
  const textarea = document.createElement("textarea");
  document.body.appendChild(textarea);
  textarea.focus();

  function stringifyTextChangeNotification(aNotification) {
    if (!aNotification) {
      return "{}";
    }
    return `{ offset: ${aNotification.offset}, removedLength: ${aNotification.removedLength}, addedLength: ${aNotification.addedLength} }`;
  }

  const tip =
    Cc["@mozilla.org/text-input-processor;1"].
      createInstance(Ci.nsITextInputProcessor);
  let notifications = [];
  function callback(aTIP, aNotification) {
    switch (aNotification.type) {
      case "request-to-commit":
        aTIP.commitComposition();
        break;
      case "request-to-cancel":
        aTIP.cancelComposition();
        break;
      case "notify-text-change":
        notifications.push(aNotification);
        break;
    }
    return true;
  }

  tip.beginInputTransactionForTests(window, callback);

  notifications = [];
  textarea.appendChild(document.createTextNode("abc"));
  await new Promise(resolve => requestAnimationFrame(() => requestAnimationFrame(resolve)));
  is(
    stringifyTextChangeNotification(notifications[0]),
    stringifyTextChangeNotification({ offset: 0, removedLength: 0, addedLength: "abc".length }),
    "Adding text node to empty <textarea> should notify IME of a text change"
  );

  notifications = [];
  textarea.firstChild.remove();
  await new Promise(resolve => requestAnimationFrame(() => requestAnimationFrame(resolve)));
  is(
    stringifyTextChangeNotification(notifications[0]),
    stringifyTextChangeNotification({ offset: 0, removedLength: "abc".length, addedLength: 0 }),
    "Removing text node from <textarea> should notify IME of a text change"
  );

  // Update default value during reframes
  notifications = [];
  textarea.setAttribute("dir", "rtl");
  textarea.prepend("abc", document.createElement("marquee"));
  await new Promise(resolve => requestAnimationFrame(() => requestAnimationFrame(resolve)));
  is(
    stringifyTextChangeNotification(notifications[0]),
    stringifyTextChangeNotification({ offset: 0, removedLength: 0, addedLength: "abc".length }),
    "Adding text node to empty <textarea> during reframes should notify IME of a text change"
  );

  notifications = [];
  textarea.removeAttribute("dir");
  textarea.innerHTML = "";
  await new Promise(resolve => requestAnimationFrame(() => requestAnimationFrame(resolve)));
  is(
    stringifyTextChangeNotification(notifications[0]),
    stringifyTextChangeNotification({ offset: 0, removedLength: "abc".length, addedLength: 0 }),
    "Removing text node from <textarea> during reframes should notify IME of a text change"
  );

  // Make the textarea dirty
  textarea.value = "X";
  await new Promise(resolve => requestAnimationFrame(() => requestAnimationFrame(resolve)));

  notifications = [];
  textarea.appendChild(document.createTextNode("abc"));
  await new Promise(resolve => requestAnimationFrame(() => requestAnimationFrame(resolve)));
  is(
    textarea.value,
    "X",
    "The value should not be updated by adding text node into the dirty <textarea>"
  );
  is(
    stringifyTextChangeNotification(notifications[0]),
    stringifyTextChangeNotification(undefined),
    "Adding text node to empty but dirty <textarea> should not notify IME of a text change"
  );

  SimpleTest.finish();
});
</script>
</head>
<body></body>
</html>