blob: fbc976ee697ab604d44e722056f2d62f2d9b8ec0 [file] [log] [blame]
<html>
<head>
<link rel="help" href="http://www.whatwg.org/specs/web-apps/current-work/multipage/webappapis.html#eventhandler">
<script src="../../resources/js-test.js"></script>
</head>
<body>
<div id="div"></div>
<script>
description("This test checks that EventHandler attributes only accept JS functions as input.");
var div = document.getElementById("div");
var callbackCount = 0;
function callback() {
++callbackCount;
}
function dispatchKeyEvent()
{
var event = new KeyboardEvent('keydown', { bubbles: true, cancelable: true, view: null, key: 'Enter' });
div.dispatchEvent(event);
}
// Input is a JS function, this should work.
shouldBeNull('div.onkeydown');
shouldNotThrow('div.onkeydown = callback');
shouldBe('div.onkeydown', 'callback');
shouldBe('callbackCount', '0');
dispatchKeyEvent();
shouldBe('callbackCount', '1');
// Non callable input should be treated as null.
var o = { handleEvent: callback };
shouldNotBe('div.onkeydown', 'null');
shouldNotThrow('div.onkeydown = o');
shouldBe('div.onkeydown', 'o');
dispatchKeyEvent();
shouldBe('callbackCount', '1');
// Test non-object assignment.
shouldNotThrow('div.onkeydown = callback');
shouldBe('div.onkeydown', 'callback');
shouldNotThrow('div.onkeydown = "non-object"');
shouldBeNull('div.onkeydown');
// Test null assignment.
shouldNotThrow('div.onkeydown = callback');
shouldBe('div.onkeydown', 'callback');
shouldNotThrow('div.onkeydown = null');
shouldBeNull('div.onkeydown');
</script>
</body>
</html>