summaryrefslogtreecommitdiffstats
path: root/editor/libeditor/tests/test_dom_input_event_on_texteditor.html
blob: b1395e99c619b638fa99597a6ba9afdec9070ddf (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
<html>
<head>
  <title>Test for input event of text editor</title>
  <script type="text/javascript"
          src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript"
          src="/tests/SimpleTest/EventUtils.js"></script>
  <link rel="stylesheet" type="text/css"
          href="/tests/SimpleTest/test.css" />
</head>
<body>
<div id="display">
  <input type="text" id="input">
  <textarea id="textarea"></textarea>
</div>
<div id="content" style="display: none">
  
</div>
<pre id="test">
</pre>

<script class="testbody" type="application/javascript">

SimpleTest.waitForExplicitFinish();
SimpleTest.waitForFocus(runTests, window);

const kIsMac = navigator.platform.indexOf("Mac") == 0;

function runTests()
{
  function doTests(aElement, aDescription, aIsTextarea)
  {
    aDescription += ": ";
    aElement.focus();
    aElement.value = "";

    var inputEvent = null;

    var handler = function (aEvent) {
      is(aEvent.target, aElement,
         "input event is fired on unexpected element: " + aEvent.target.tagName);
      ok(!aEvent.cancelable, "input event must not be cancelable");
      ok(aEvent.bubbles, "input event must be bubbles");
      if (SpecialPowers.getBoolPref("dom.event.highrestimestamp.enabled")) {
        var duration = Math.abs(window.performance.now() - aEvent.timeStamp);
        ok(duration < 30 * 1000,
           "perhaps, timestamp wasn't set correctly :" + aEvent.timeStamp +
           " (expected it to be within 30s of the current time but it " +
           "differed by " + duration + "ms)");
      } else {
        var eventTime = new Date(aEvent.timeStamp);
        var duration = Math.abs(Date.now() - aEvent.timeStamp);
        ok(duration < 30 * 1000,
           "perhaps, timestamp wasn't set correctly :" +
           eventTime.toLocaleString() +
           " (expected it to be within 30s of the current time but it " +
           "differed by " + duration + "ms)");
      }
      inputEvent = aEvent;
    };

    aElement.addEventListener("input", handler, true);

    inputEvent = null;
    synthesizeKey("a", { });
    is(aElement.value, "a", aDescription + "'a' key didn't change the value");
    ok(inputEvent, aDescription + "input event wasn't fired by 'a' key");
    ok(inputEvent.isTrusted, aDescription + "input event by 'a' key wasn't trusted event");

    inputEvent = null;
    synthesizeKey("VK_BACK_SPACE", { });
    is(aElement.value, "", aDescription + "BackSpace key didn't remove the value");
    ok(inputEvent, aDescription + "input event wasn't fired by BackSpace key");
    ok(inputEvent.isTrusted, aDescription + "input event by BackSpace key wasn't trusted event");

    if (aIsTextarea) {
      inputEvent = null;
      synthesizeKey("VK_RETURN", { });
      is(aElement.value, "\n", aDescription + "Enter key didn't change the value");
      ok(inputEvent, aDescription + "input event wasn't fired by Enter key");
      ok(inputEvent.isTrusted, aDescription + "input event by Enter key wasn't trusted event");
    }

    inputEvent = null;
    aElement.value = "foo-bar";
    is(aElement.value, "foo-bar", aDescription + "value wasn't set");
    ok(!inputEvent, aDescription + "input event was fired by setting value");

    inputEvent = null;
    aElement.value = "";
    is(aElement.value, "", aDescription + "value wasn't set (empty)");
    ok(!inputEvent, aDescription + "input event was fired by setting empty value");

    inputEvent = null;
    synthesizeKey(" ", { });
    is(aElement.value, " ", aDescription + "Space key didn't change the value");
    ok(inputEvent, aDescription + "input event wasn't fired by Space key");
    ok(inputEvent.isTrusted, aDescription + "input event by Space key wasn't trusted event");

    inputEvent = null;
    synthesizeKey("VK_DELETE", { });
    is(aElement.value, " ", aDescription + "Delete key removed the value");
    ok(!inputEvent, aDescription + "input event was fired by Delete key at the end");

    inputEvent = null;
    synthesizeKey("VK_LEFT", { });
    is(aElement.value, " ", aDescription + "Left key removed the value");
    ok(!inputEvent, aDescription + "input event was fired by Left key");

    inputEvent = null;
    synthesizeKey("VK_DELETE", { });
    is(aElement.value, "", aDescription + "Delete key didn't remove the value");
    ok(inputEvent, aDescription + "input event wasn't fired by Delete key at the start");
    ok(inputEvent.isTrusted, aDescription + "input event by Delete key wasn't trusted event");

    inputEvent = null;
    synthesizeKey("z", { accelKey: true });
    is(aElement.value, " ", aDescription + "Accel+Z key didn't undo the value");
    ok(inputEvent, aDescription + "input event wasn't fired by Undo");
    ok(inputEvent.isTrusted, aDescription + "input event by Undo wasn't trusted event");

    inputEvent = null;
    synthesizeKey("z", { accelKey: true, shiftKey: true });
    is(aElement.value, "", aDescription + "Accel+Y key didn't redo the value");
    ok(inputEvent, aDescription + "input event wasn't fired by Redo");
    ok(inputEvent.isTrusted, aDescription + "input event by Redo wasn't trusted event");

    aElement.removeEventListener("input", handler, true);
  }

  doTests(document.getElementById("input"), "<input type=\"text\">", false);
  doTests(document.getElementById("textarea"), "<textarea>", true);

  SimpleTest.finish();
}

</script>
</body>

</html>