gecko/dom/tests/mochitest/browser-frame/test_browserFrame_keyEvents.html

100 lines
3.0 KiB
HTML
Raw Normal View History

<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=757486
-->
<head>
<title>Test for Bug 757486</title>
<script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
<script type="application/javascript" src="/tests/SimpleTest/EventUtils.js"></script>
<script type="application/javascript" src="browserFrameHelpers.js"></script>
<link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=757486">Mozilla Bug 757486</a>
<!--
Test that an iframe with the |mozbrowser| attribute does bubble some
whitelisted key events.
-->
<script type="application/javascript;version=1.7">
"use strict";
let Ci = Components.interfaces;
let whitelistedEvents = [
Ci.nsIDOMKeyEvent.DOM_VK_ESCAPE, // Back button.
Ci.nsIDOMKeyEvent.DOM_VK_CONTEXT_MENU,
Ci.nsIDOMKeyEvent.DOM_VK_F5, // Search button.
Ci.nsIDOMKeyEvent.DOM_VK_PAGE_UP, // Volume up.
Ci.nsIDOMKeyEvent.DOM_VK_PAGE_DOWN // Volume down.
];
SimpleTest.waitForExplicitFinish();
browserFrameHelpers.setEnabledPref(true);
browserFrameHelpers.addToWhitelist();
browserFrameHelpers.setOOPDisabledPref(true); // this is breaking the autofocus.
var iframe = document.createElement('iframe');
iframe.mozbrowser = true;
iframe.src = browserFrameHelpers.focusPage;
document.body.appendChild(iframe);
// Number of expected events at which point we will consider the test as done.
var nbEvents = 15;
function eventHandler(e) {
ok(((e.type == 'keydown' || e.type == 'keypress' || e.type == 'keyup') &&
!e.defaultPrevented &&
whitelistedEvents.indexOf(e.keyCode)),
"[ " + e.type + "] Handled event should be a non prevented key event in the white list.");
nbEvents--;
if (nbEvents == 0) {
browserFrameHelpers.restoreOriginalPrefs();
SimpleTest.finish();
return;
}
if (nbEvents < 0) {
ok(false, "got an unexpected event! " + e.type + " " + e.keyCode);
SimpleTest.finish();
return;
}
}
function runTest() {
is(document.activeElement, iframe, "iframe should be focused");
addEventListener('keydown', eventHandler);
addEventListener('keypress', eventHandler);
addEventListener('keyup', eventHandler);
// Those event should not be received because not whitelisted.
synthesizeKey("VK_A", {});
synthesizeKey("VK_B", {});
// Those events should not be received because prevent default is called.
synthesizeKey("VK_ESCAPE", {});
// Those events should be received.
synthesizeKey("VK_F5", {}); // F5 key is going to be canceled by ESC key.
synthesizeKey("VK_ESCAPE", {});
synthesizeKey("VK_PAGE_UP", {}); // keypress is ignored because .preventDefault() will be called.
synthesizeKey("VK_PAGE_DOWN", {}); // keypress is ignored because .preventDefault() will be called.
synthesizeKey("VK_CONTEXT_MENU", {});
}
SimpleTest.waitForFocus(function() {
iframe.focus();
SimpleTest.executeSoon(runTest);
});
</script>
</body>
</html>