dom/browser-element/mochitest/browserElement_KeyEvents.js

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* Any copyright is dedicated to the public domain.
michael@0 2 http://creativecommons.org/publicdomain/zero/1.0/ */
michael@0 3
michael@0 4 // Test that an iframe with the |mozbrowser| attribute does bubble some
michael@0 5 // whitelisted key events.
michael@0 6 "use strict";
michael@0 7
michael@0 8 let Ci = SpecialPowers.Ci;
michael@0 9
michael@0 10 let whitelistedKeyCodes = [
michael@0 11 Ci.nsIDOMKeyEvent.DOM_VK_ESCAPE, // Back button.
michael@0 12 Ci.nsIDOMKeyEvent.DOM_VK_SLEEP, // Power button
michael@0 13 Ci.nsIDOMKeyEvent.DOM_VK_CONTEXT_MENU,
michael@0 14 Ci.nsIDOMKeyEvent.DOM_VK_F5, // Search button.
michael@0 15 Ci.nsIDOMKeyEvent.DOM_VK_PAGE_UP, // Volume up.
michael@0 16 Ci.nsIDOMKeyEvent.DOM_VK_PAGE_DOWN // Volume down.
michael@0 17 ];
michael@0 18
michael@0 19 let blacklistedKeyCodes = [
michael@0 20 Ci.nsIDOMKeyEvent.DOM_VK_A,
michael@0 21 Ci.nsIDOMKeyEvent.DOM_VK_B
michael@0 22 ];
michael@0 23
michael@0 24 SimpleTest.waitForExplicitFinish();
michael@0 25 browserElementTestHelpers.setEnabledPref(true);
michael@0 26 browserElementTestHelpers.addPermission();
michael@0 27
michael@0 28 // Number of expected events at which point we will consider the test as done.
michael@0 29 var nbEvents = whitelistedKeyCodes.length * 3;
michael@0 30
michael@0 31 var iframe;
michael@0 32 var finished = false;
michael@0 33 function runTest() {
michael@0 34 iframe = document.createElement('iframe');
michael@0 35 SpecialPowers.wrap(iframe).mozbrowser = true;
michael@0 36 iframe.src = browserElementTestHelpers.focusPage;
michael@0 37
michael@0 38 var gotFocus = false;
michael@0 39 var gotLoadend = false;
michael@0 40
michael@0 41 function maybeTest2() {
michael@0 42 if (gotFocus && gotLoadend) {
michael@0 43 SimpleTest.executeSoon(test2);
michael@0 44 }
michael@0 45 }
michael@0 46
michael@0 47 iframe.addEventListener('mozbrowserloadend', function() {
michael@0 48 gotLoadend = true;
michael@0 49 maybeTest2();
michael@0 50 });
michael@0 51
michael@0 52 document.body.appendChild(iframe);
michael@0 53
michael@0 54 SimpleTest.waitForFocus(function() {
michael@0 55 iframe.focus();
michael@0 56 gotFocus = true;
michael@0 57 maybeTest2();
michael@0 58 });
michael@0 59 }
michael@0 60
michael@0 61 function eventHandler(e) {
michael@0 62 if (whitelistedKeyCodes.indexOf(e.keyCode) == -1 &&
michael@0 63 blacklistedKeyCodes.indexOf(e.keyCode) == -1) {
michael@0 64 // See bug 856006: We sometimes get unexpected key presses, and we don't
michael@0 65 // know why. Don't turn the test orange over this.
michael@0 66 ok(true, "Ignoring unexpected " + e.type +
michael@0 67 " with keyCode " + e.keyCode + ".");
michael@0 68 return;
michael@0 69 }
michael@0 70
michael@0 71 ok(e.type == 'keydown' || e.type == 'keypress' || e.type == 'keyup',
michael@0 72 "e.type was " + e.type + ", expected keydown, keypress, or keyup");
michael@0 73 ok(!e.defaultPrevented, "expected !e.defaultPrevented");
michael@0 74 ok(whitelistedKeyCodes.indexOf(e.keyCode) != -1,
michael@0 75 "Expected a whitelited keycode, but got " + e.keyCode + " instead.");
michael@0 76
michael@0 77 nbEvents--;
michael@0 78
michael@0 79 if (nbEvents == 0) {
michael@0 80 SimpleTest.finish();
michael@0 81 return;
michael@0 82 }
michael@0 83
michael@0 84 if (nbEvents < 0 && !finished) {
michael@0 85 ok(false, "got an unexpected event! " + e.type + " " + e.keyCode);
michael@0 86 }
michael@0 87 }
michael@0 88
michael@0 89 function test2() {
michael@0 90 is(document.activeElement, iframe, "iframe should be focused");
michael@0 91
michael@0 92 addEventListener('keydown', eventHandler);
michael@0 93 addEventListener('keypress', eventHandler);
michael@0 94 addEventListener('keyup', eventHandler);
michael@0 95
michael@0 96 // These events should not be received because they are not whitelisted.
michael@0 97 synthesizeKey("VK_A", {});
michael@0 98 synthesizeKey("VK_B", {});
michael@0 99
michael@0 100 // These events should not be received because preventDefault is called.
michael@0 101 synthesizeKey("VK_ESCAPE", {});
michael@0 102
michael@0 103 // These events should be received.
michael@0 104 synthesizeKey("VK_F5", {});
michael@0 105 synthesizeKey("VK_ESCAPE", {});
michael@0 106 synthesizeKey("VK_PAGE_UP", {});
michael@0 107 synthesizeKey("VK_PAGE_DOWN", {});
michael@0 108 synthesizeKey("VK_CONTEXT_MENU", {});
michael@0 109 synthesizeKey("VK_SLEEP", {});
michael@0 110 finished = true;
michael@0 111 }
michael@0 112
michael@0 113 addEventListener('testready', runTest);

mercurial