<!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>
