summaryrefslogtreecommitdiffstats
path: root/editor/libeditor/tests/test_dom_input_event_on_htmleditor.html
blob: d1716a228edaa1ef2b3f4e5bb0e4a2afe5acafc3 (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
<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">
  <iframe id="editor1" src="data:text/html,<html><body contenteditable id='eventTarget'></body></html>"></iframe>
  <iframe id="editor2" src="data:text/html,<html contenteditable id='eventTarget'><body></body></html>"></iframe>
  <iframe id="editor3" src="data:text/html,<html><body><div contenteditable id='eventTarget'></div></body></html>"></iframe>
  <iframe id="editor4" src="data:text/html,<html contenteditable id='eventTarget'><body><div contenteditable id='editTarget'></div></body></html>"></iframe>
  <iframe id="editor5" src="data:text/html,<html><body id='eventTarget'></body><script>document.designMode='on';</script></html>"></iframe>
</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(aDocument, aWindow, aDescription)
  {
    aDescription += ": ";
    aWindow.focus();

    var body = aDocument.body;

    var eventTarget = aDocument.getElementById("eventTarget");
    // The event target must be focusable because it's the editing host.
    eventTarget.focus();

    var editTarget = aDocument.getElementById("editTarget");
    if (!editTarget) {
      editTarget = eventTarget;
    }

    // Root element never can be edit target.  If the editTarget is the root
    // element, replace with its body.
    if (editTarget == aDocument.documentElement) {
      editTarget = body;
    }

    editTarget.innerHTML = "";

    // If the editTarget isn't its editing host, move caret to the start of it.
    if (eventTarget != editTarget) {
      aDocument.getSelection().collapse(editTarget, 0);
    }

    var inputEvent = null;

    var handler = function (aEvent) {
      is(aEvent.target, eventTarget,
         "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;
    };

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

    inputEvent = null;
    synthesizeKey("a", { }, aWindow);
    is(editTarget.innerHTML, "a", aDescription + "wrong element was edited");
    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", { }, aWindow);
    ok(inputEvent, aDescription + "input event wasn't fired by BackSpace key");
    ok(inputEvent.isTrusted, aDescription + "input event by BackSpace key wasn't trusted event");

    inputEvent = null;
    synthesizeKey("B", { shiftKey: true }, aWindow);
    ok(inputEvent, aDescription + "input event wasn't fired by 'B' key");
    ok(inputEvent.isTrusted, aDescription + "input event by 'B' key wasn't trusted event");

    inputEvent = null;
    synthesizeKey("VK_RETURN", { }, aWindow);
    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;
    synthesizeKey("C", { shiftKey: true }, aWindow);
    ok(inputEvent, aDescription + "input event wasn't fired by 'C' key");
    ok(inputEvent.isTrusted, aDescription + "input event by 'C' key wasn't trusted event");

    inputEvent = null;
    synthesizeKey("VK_RETURN", { }, aWindow);
    ok(inputEvent, aDescription + "input event wasn't fired by Enter key (again)");
    ok(inputEvent.isTrusted, aDescription + "input event by Enter key (again) wasn't trusted event");

    inputEvent = null;
    editTarget.innerHTML = "foo-bar";
    ok(!inputEvent, aDescription + "input event was fired by setting value");

    inputEvent = null;
    editTarget.innerHTML = "";
    ok(!inputEvent, aDescription + "input event was fired by setting empty value");

    inputEvent = null;
    synthesizeKey(" ", { }, aWindow);
    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", { }, aWindow);
    ok(!inputEvent, aDescription + "input event was fired by Delete key at the end");

    inputEvent = null;
    synthesizeKey("VK_LEFT", { }, aWindow);
    ok(!inputEvent, aDescription + "input event was fired by Left key");

    inputEvent = null;
    synthesizeKey("VK_DELETE", { }, aWindow);
    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 }, aWindow);
    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 }, aWindow);
    ok(inputEvent, aDescription + "input event wasn't fired by Redo");
    ok(inputEvent.isTrusted, aDescription + "input event by Redo wasn't trusted event");

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

  doTests(document.getElementById("editor1").contentDocument,
          document.getElementById("editor1").contentWindow,
          "Editor1, body has contenteditable attribute");
  doTests(document.getElementById("editor2").contentDocument,
          document.getElementById("editor2").contentWindow,
          "Editor2, html has contenteditable attribute");
  doTests(document.getElementById("editor3").contentDocument,
          document.getElementById("editor3").contentWindow,
          "Editor3, div has contenteditable attribute");
  doTests(document.getElementById("editor4").contentDocument,
          document.getElementById("editor4").contentWindow,
          "Editor4, html and div has contenteditable attribute");
  doTests(document.getElementById("editor5").contentDocument,
          document.getElementById("editor5").contentWindow,
          "Editor5, html and div has contenteditable attribute");

  SimpleTest.finish();
}

</script>
</body>

</html>