summaryrefslogtreecommitdiffstats
path: root/dom/html/test/forms/test_input_untrusted_key_events.html
blob: 78e35f525fad5c2d68c812ac3a6089f91b3b8953 (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
<!DOCTYPE HTML>
<html>
<head>
  <title>Test for untrusted DOM KeyboardEvent on input element</title>
  <script src="/tests/SimpleTest/SimpleTest.js"></script>
  <script src="/tests/SimpleTest/EventUtils.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<p id="display"></p>
<div id="content">
  <input id="input">
</div>
<pre id="test">
<script type="application/javascript">

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

const kTests = [
  { type: "text", value: "foo", key: "b", expectedNewValue: "foo" },
  { type: "number", value: "123", key: "4", expectedNewValue: "123" },
  { type: "number", value: "123", key: KeyEvent.DOM_VK_UP, expectedNewValue: "123" },
  { type: "number", value: "123", key: KeyEvent.DOM_VK_DOWN, expectedNewValue: "123" },
];

function sendUntrustedKeyEvent(eventType, keyCode, target) {
  var evt = new KeyboardEvent(eventType, {
    bubbles: true,
    cancelable: true,
    view: document.defaultView,
    keyCode,
    charCode: 0,
  });
  target.dispatchEvent(evt);
}

var input = document.getElementById("input");

var gotEvents = {};

function handleEvent(event) {
  gotEvents[event.type] = true;
}

input.addEventListener("keydown", handleEvent);
input.addEventListener("keyup", handleEvent);
input.addEventListener("keypress", handleEvent);

var previousTest = null;

function runNextTest() {
  if (previousTest) {
    var msg = "For <input " + "type=" + previousTest.type + ">, ";
    is(gotEvents.keydown,  true, msg + "checking got keydown");
    is(gotEvents.keyup,    true, msg + "checking got keyup");
    is(gotEvents.keypress, true, msg + "checking got keypress");
    is(input.value, previousTest.expectedNewValue, msg + "checking element " +
         " after being sent '" + previousTest.key + "' key events");
  }

  // reset flags
  gotEvents.keydown = false;
  gotEvents.keyup = false;
  gotEvents.keypress = false;


  var test = kTests.shift();
  if (!test) {
    SimpleTest.finish();
    return; // We're all done
  }

  input.type = test.type;
  input.focus(); // make sure we still have focus after type change
  input.value = test.value;

  sendUntrustedKeyEvent("keydown", test.key, input);
  sendUntrustedKeyEvent("keyup", test.key, input);
  sendUntrustedKeyEvent("keypress", test.key, input);

  previousTest = test;

  SimpleTest.executeSoon(runNextTest);
};

</script>
</pre>
</body>
</html>