| 1 |
// Copyright 2010 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
goog.provide('goog.events.KeyCodesTest'); |
| 16 |
goog.setTestOnly('goog.events.KeyCodesTest'); |
| 17 |
|
| 18 |
goog.require('goog.events.BrowserEvent'); |
| 19 |
goog.require('goog.events.KeyCodes'); |
| 20 |
goog.require('goog.object'); |
| 21 |
goog.require('goog.testing.PropertyReplacer'); |
| 22 |
goog.require('goog.testing.jsunit'); |
| 23 |
goog.require('goog.userAgent'); |
| 24 |
|
| 25 |
var KeyCodes; |
| 26 |
var stubs; |
| 27 |
|
| 28 |
function setUpPage() { |
| 29 |
KeyCodes = goog.events.KeyCodes; |
| 30 |
stubs = new goog.testing.PropertyReplacer(); |
| 31 |
} |
| 32 |
|
| 33 |
function tearDown() { |
| 34 |
stubs.reset(); |
| 35 |
} |
| 36 |
|
| 37 |
function testTextModifyingKeys() { |
| 38 |
var specialTextModifiers = goog.object.createSet( |
| 39 |
KeyCodes.BACKSPACE, KeyCodes.DELETE, KeyCodes.ENTER, KeyCodes.MAC_ENTER, |
| 40 |
KeyCodes.TAB, KeyCodes.WIN_IME); |
| 41 |
|
| 42 |
if (!goog.userAgent.GECKO) { |
| 43 |
specialTextModifiers[KeyCodes.WIN_KEY_FF_LINUX] = 1; |
| 44 |
} |
| 45 |
|
| 46 |
for (var keyId in KeyCodes) { |
| 47 |
var key = KeyCodes[keyId]; |
| 48 |
if (goog.isFunction(key)) { |
| 49 |
// skip static methods |
| 50 |
continue; |
| 51 |
} |
| 52 |
|
| 53 |
var fakeEvent = createEventWithKeyCode(key); |
| 54 |
|
| 55 |
if (KeyCodes.isCharacterKey(key) || (key in specialTextModifiers)) { |
| 56 |
assertTrue( |
| 57 |
'Expected key to modify text: ' + keyId, |
| 58 |
KeyCodes.isTextModifyingKeyEvent(fakeEvent)); |
| 59 |
} else { |
| 60 |
assertFalse( |
| 61 |
'Expected key to not modify text: ' + keyId, |
| 62 |
KeyCodes.isTextModifyingKeyEvent(fakeEvent)); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
for (var i = KeyCodes.FIRST_MEDIA_KEY; i <= KeyCodes.LAST_MEDIA_KEY; i++) { |
| 67 |
var fakeEvent = createEventWithKeyCode(i); |
| 68 |
assertFalse( |
| 69 |
'Expected key to not modify text: ' + i, |
| 70 |
KeyCodes.isTextModifyingKeyEvent(fakeEvent)); |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
function testKeyCodeZero() { |
| 75 |
var zeroEvent = createEventWithKeyCode(0); |
| 76 |
assertEquals( |
| 77 |
!goog.userAgent.GECKO, KeyCodes.isTextModifyingKeyEvent(zeroEvent)); |
| 78 |
assertEquals( |
| 79 |
goog.userAgent.WEBKIT || goog.userAgent.EDGE, KeyCodes.isCharacterKey(0)); |
| 80 |
} |
| 81 |
|
| 82 |
function testPhantomKey() { |
| 83 |
// KeyCode 255 deserves its own test to make sure this does not regress, |
| 84 |
// because it's so weird. See the comments in the KeyCode enum. |
| 85 |
var fakeEvent = createEventWithKeyCode(goog.events.KeyCodes.PHANTOM); |
| 86 |
assertFalse( |
| 87 |
'Expected phantom key to not modify text', |
| 88 |
KeyCodes.isTextModifyingKeyEvent(fakeEvent)); |
| 89 |
assertFalse(KeyCodes.isCharacterKey(fakeEvent)); |
| 90 |
} |
| 91 |
|
| 92 |
function testNonUsKeyboards() { |
| 93 |
var fakeEvent = createEventWithKeyCode(1092 /* Russian a */); |
| 94 |
assertTrue( |
| 95 |
'Expected key to not modify text: 1092', |
| 96 |
KeyCodes.isTextModifyingKeyEvent(fakeEvent)); |
| 97 |
} |
| 98 |
|
| 99 |
function createEventWithKeyCode(i) { |
| 100 |
var fakeEvent = new goog.events.BrowserEvent('keydown'); |
| 101 |
fakeEvent.keyCode = i; |
| 102 |
return fakeEvent; |
| 103 |
} |
| 104 |
|
| 105 |
function testNormalizeGeckoKeyCode() { |
| 106 |
stubs.set(goog.userAgent, 'GECKO', true); |
| 107 |
|
| 108 |
// Test Gecko-specific key codes. |
| 109 |
assertEquals( |
| 110 |
goog.events.KeyCodes.normalizeGeckoKeyCode(KeyCodes.FF_EQUALS), |
| 111 |
KeyCodes.EQUALS); |
| 112 |
assertEquals( |
| 113 |
goog.events.KeyCodes.normalizeKeyCode(KeyCodes.FF_EQUALS), |
| 114 |
KeyCodes.EQUALS); |
| 115 |
|
| 116 |
assertEquals( |
| 117 |
goog.events.KeyCodes.normalizeGeckoKeyCode(KeyCodes.FF_SEMICOLON), |
| 118 |
KeyCodes.SEMICOLON); |
| 119 |
assertEquals( |
| 120 |
goog.events.KeyCodes.normalizeKeyCode(KeyCodes.FF_SEMICOLON), |
| 121 |
KeyCodes.SEMICOLON); |
| 122 |
|
| 123 |
assertEquals( |
| 124 |
goog.events.KeyCodes.normalizeGeckoKeyCode(KeyCodes.MAC_FF_META), |
| 125 |
KeyCodes.META); |
| 126 |
assertEquals( |
| 127 |
goog.events.KeyCodes.normalizeKeyCode(KeyCodes.MAC_FF_META), |
| 128 |
KeyCodes.META); |
| 129 |
|
| 130 |
assertEquals( |
| 131 |
goog.events.KeyCodes.normalizeGeckoKeyCode(KeyCodes.WIN_KEY_FF_LINUX), |
| 132 |
KeyCodes.WIN_KEY); |
| 133 |
assertEquals( |
| 134 |
goog.events.KeyCodes.normalizeKeyCode(KeyCodes.WIN_KEY_FF_LINUX), |
| 135 |
KeyCodes.WIN_KEY); |
| 136 |
|
| 137 |
// Test general key codes. |
| 138 |
assertEquals( |
| 139 |
goog.events.KeyCodes.normalizeGeckoKeyCode(KeyCodes.COMMA), |
| 140 |
KeyCodes.COMMA); |
| 141 |
assertEquals( |
| 142 |
goog.events.KeyCodes.normalizeKeyCode(KeyCodes.COMMA), KeyCodes.COMMA); |
| 143 |
} |
| 144 |
|
| 145 |
function testNormalizeMacWebKitKeyCode() { |
| 146 |
stubs.set(goog.userAgent, 'GECKO', false); |
| 147 |
stubs.set(goog.userAgent, 'MAC', true); |
| 148 |
stubs.set(goog.userAgent, 'WEBKIT', true); |
| 149 |
|
| 150 |
// Test Mac WebKit specific key codes. |
| 151 |
assertEquals( |
| 152 |
goog.events.KeyCodes.normalizeMacWebKitKeyCode(KeyCodes.MAC_WK_CMD_LEFT), |
| 153 |
KeyCodes.META); |
| 154 |
assertEquals( |
| 155 |
goog.events.KeyCodes.normalizeKeyCode(KeyCodes.MAC_WK_CMD_LEFT), |
| 156 |
KeyCodes.META); |
| 157 |
|
| 158 |
assertEquals( |
| 159 |
goog.events.KeyCodes.normalizeMacWebKitKeyCode(KeyCodes.MAC_WK_CMD_RIGHT), |
| 160 |
KeyCodes.META); |
| 161 |
assertEquals( |
| 162 |
goog.events.KeyCodes.normalizeKeyCode(KeyCodes.MAC_WK_CMD_RIGHT), |
| 163 |
KeyCodes.META); |
| 164 |
|
| 165 |
// Test general key codes. |
| 166 |
assertEquals( |
| 167 |
goog.events.KeyCodes.normalizeMacWebKitKeyCode(KeyCodes.COMMA), |
| 168 |
KeyCodes.COMMA); |
| 169 |
assertEquals( |
| 170 |
goog.events.KeyCodes.normalizeKeyCode(KeyCodes.COMMA), KeyCodes.COMMA); |
| 171 |
} |