summaryrefslogtreecommitdiffstats
path: root/dom/html/test/forms/test_input_untrusted_key_events.html
blob: b356316ceff639ef9cfd586559267f3c37ed0980 (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
<!DOCTYPE HTML>
<html>
<head>
  <title>Test for untrusted DOM KeyboardEvent on input element</title>
  <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="application/javascript" 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 = document.createEvent("KeyboardEvent");
  var canBubbleArg = true;
  var cancelableArg = true;
  var viewArg = document.defaultView;
  var ctrlKeyArg = false;
  var altKeyArg = false;
  var shiftKeyArg = false;
  var metaKeyArg = false;
  var keyCodeArg = keyCode;
  var charCodeArg = 0;
  evt.initKeyEvent(eventType, canBubbleArg, cancelableArg, viewArg,
                   ctrlKeyArg, altKeyArg, shiftKeyArg, metaKeyArg,
                   keyCodeArg, charCodeArg);
  target.dispatchEvent(evt);
}

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

var gotEvents = {};

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

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

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>