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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
|
// Copyright 2010 The Closure Library Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS-IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
goog.provide('goog.events.KeyCodesTest');
goog.setTestOnly('goog.events.KeyCodesTest');
goog.require('goog.events.BrowserEvent');
goog.require('goog.events.KeyCodes');
goog.require('goog.object');
goog.require('goog.testing.PropertyReplacer');
goog.require('goog.testing.jsunit');
goog.require('goog.userAgent');
var KeyCodes;
var stubs;
function setUpPage() {
KeyCodes = goog.events.KeyCodes;
stubs = new goog.testing.PropertyReplacer();
}
function tearDown() {
stubs.reset();
}
function testTextModifyingKeys() {
var specialTextModifiers = goog.object.createSet(
KeyCodes.BACKSPACE, KeyCodes.DELETE, KeyCodes.ENTER, KeyCodes.MAC_ENTER,
KeyCodes.TAB, KeyCodes.WIN_IME);
if (!goog.userAgent.GECKO) {
specialTextModifiers[KeyCodes.WIN_KEY_FF_LINUX] = 1;
}
for (var keyId in KeyCodes) {
var key = KeyCodes[keyId];
if (goog.isFunction(key)) {
// skip static methods
continue;
}
var fakeEvent = createEventWithKeyCode(key);
if (KeyCodes.isCharacterKey(key) || (key in specialTextModifiers)) {
assertTrue(
'Expected key to modify text: ' + keyId,
KeyCodes.isTextModifyingKeyEvent(fakeEvent));
} else {
assertFalse(
'Expected key to not modify text: ' + keyId,
KeyCodes.isTextModifyingKeyEvent(fakeEvent));
}
}
for (var i = KeyCodes.FIRST_MEDIA_KEY; i <= KeyCodes.LAST_MEDIA_KEY; i++) {
var fakeEvent = createEventWithKeyCode(i);
assertFalse(
'Expected key to not modify text: ' + i,
KeyCodes.isTextModifyingKeyEvent(fakeEvent));
}
}
function testKeyCodeZero() {
var zeroEvent = createEventWithKeyCode(0);
assertEquals(
!goog.userAgent.GECKO, KeyCodes.isTextModifyingKeyEvent(zeroEvent));
assertEquals(
goog.userAgent.WEBKIT || goog.userAgent.EDGE, KeyCodes.isCharacterKey(0));
}
function testPhantomKey() {
// KeyCode 255 deserves its own test to make sure this does not regress,
// because it's so weird. See the comments in the KeyCode enum.
var fakeEvent = createEventWithKeyCode(goog.events.KeyCodes.PHANTOM);
assertFalse(
'Expected phantom key to not modify text',
KeyCodes.isTextModifyingKeyEvent(fakeEvent));
assertFalse(KeyCodes.isCharacterKey(fakeEvent));
}
function testNonUsKeyboards() {
var fakeEvent = createEventWithKeyCode(1092 /* Russian a */);
assertTrue(
'Expected key to not modify text: 1092',
KeyCodes.isTextModifyingKeyEvent(fakeEvent));
}
function createEventWithKeyCode(i) {
var fakeEvent = new goog.events.BrowserEvent('keydown');
fakeEvent.keyCode = i;
return fakeEvent;
}
function testNormalizeGeckoKeyCode() {
stubs.set(goog.userAgent, 'GECKO', true);
// Test Gecko-specific key codes.
assertEquals(
goog.events.KeyCodes.normalizeGeckoKeyCode(KeyCodes.FF_EQUALS),
KeyCodes.EQUALS);
assertEquals(
goog.events.KeyCodes.normalizeKeyCode(KeyCodes.FF_EQUALS),
KeyCodes.EQUALS);
assertEquals(
goog.events.KeyCodes.normalizeGeckoKeyCode(KeyCodes.FF_SEMICOLON),
KeyCodes.SEMICOLON);
assertEquals(
goog.events.KeyCodes.normalizeKeyCode(KeyCodes.FF_SEMICOLON),
KeyCodes.SEMICOLON);
assertEquals(
goog.events.KeyCodes.normalizeGeckoKeyCode(KeyCodes.MAC_FF_META),
KeyCodes.META);
assertEquals(
goog.events.KeyCodes.normalizeKeyCode(KeyCodes.MAC_FF_META),
KeyCodes.META);
assertEquals(
goog.events.KeyCodes.normalizeGeckoKeyCode(KeyCodes.WIN_KEY_FF_LINUX),
KeyCodes.WIN_KEY);
assertEquals(
goog.events.KeyCodes.normalizeKeyCode(KeyCodes.WIN_KEY_FF_LINUX),
KeyCodes.WIN_KEY);
// Test general key codes.
assertEquals(
goog.events.KeyCodes.normalizeGeckoKeyCode(KeyCodes.COMMA),
KeyCodes.COMMA);
assertEquals(
goog.events.KeyCodes.normalizeKeyCode(KeyCodes.COMMA), KeyCodes.COMMA);
}
function testNormalizeMacWebKitKeyCode() {
stubs.set(goog.userAgent, 'GECKO', false);
stubs.set(goog.userAgent, 'MAC', true);
stubs.set(goog.userAgent, 'WEBKIT', true);
// Test Mac WebKit specific key codes.
assertEquals(
goog.events.KeyCodes.normalizeMacWebKitKeyCode(KeyCodes.MAC_WK_CMD_LEFT),
KeyCodes.META);
assertEquals(
goog.events.KeyCodes.normalizeKeyCode(KeyCodes.MAC_WK_CMD_LEFT),
KeyCodes.META);
assertEquals(
goog.events.KeyCodes.normalizeMacWebKitKeyCode(KeyCodes.MAC_WK_CMD_RIGHT),
KeyCodes.META);
assertEquals(
goog.events.KeyCodes.normalizeKeyCode(KeyCodes.MAC_WK_CMD_RIGHT),
KeyCodes.META);
// Test general key codes.
assertEquals(
goog.events.KeyCodes.normalizeMacWebKitKeyCode(KeyCodes.COMMA),
KeyCodes.COMMA);
assertEquals(
goog.events.KeyCodes.normalizeKeyCode(KeyCodes.COMMA), KeyCodes.COMMA);
}
|