| 1 |
// Copyright 2008 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.ui.KeyboardShortcutHandlerTest'); |
| 16 |
goog.setTestOnly('goog.ui.KeyboardShortcutHandlerTest'); |
| 17 |
|
| 18 |
goog.require('goog.dom'); |
| 19 |
goog.require('goog.events'); |
| 20 |
goog.require('goog.events.BrowserEvent'); |
| 21 |
goog.require('goog.events.KeyCodes'); |
| 22 |
goog.require('goog.testing.MockClock'); |
| 23 |
goog.require('goog.testing.PropertyReplacer'); |
| 24 |
goog.require('goog.testing.StrictMock'); |
| 25 |
goog.require('goog.testing.events'); |
| 26 |
goog.require('goog.testing.jsunit'); |
| 27 |
goog.require('goog.ui.KeyboardShortcutHandler'); |
| 28 |
goog.require('goog.userAgent'); |
| 29 |
|
| 30 |
var Modifiers = goog.ui.KeyboardShortcutHandler.Modifiers; |
| 31 |
var KeyCodes = goog.events.KeyCodes; |
| 32 |
|
| 33 |
var handler; |
| 34 |
var targetDiv; |
| 35 |
var listener; |
| 36 |
var mockClock; |
| 37 |
var stubs = new goog.testing.PropertyReplacer(); |
| 38 |
|
| 39 |
|
| 40 |
/** |
| 41 |
* Fires a keypress on the target div. |
| 42 |
* @return {boolean} The returnValue of the sequence: false if |
| 43 |
* preventDefault() was called on any of the events, true otherwise. |
| 44 |
*/ |
| 45 |
function fire(keycode, opt_extraProperties, opt_element) { |
| 46 |
return goog.testing.events.fireKeySequence( |
| 47 |
opt_element || targetDiv, keycode, opt_extraProperties); |
| 48 |
} |
| 49 |
|
| 50 |
/** |
| 51 |
* Simulates a complete keystroke (keydown, keypress, and keyup) when typing |
| 52 |
* a non-ASCII character. |
| 53 |
* |
| 54 |
* @param {number} keycode The keycode of the keydown and keyup events. |
| 55 |
* @param {number} keyPressKeyCode The keycode of the keypress event. |
| 56 |
* @param {Object=} opt_extraProperties Event properties to be mixed into the |
| 57 |
* BrowserEvent. |
| 58 |
* @param {EventTarget=} opt_element Optional target for the event. |
| 59 |
* @return {boolean} The returnValue of the sequence: false if preventDefault() |
| 60 |
* was called on any of the events, true otherwise. |
| 61 |
*/ |
| 62 |
function fireAltGraphKey( |
| 63 |
keycode, keyPressKeyCode, opt_extraProperties, opt_element) { |
| 64 |
return goog.testing.events.fireNonAsciiKeySequence( |
| 65 |
opt_element || targetDiv, keycode, keyPressKeyCode, opt_extraProperties); |
| 66 |
} |
| 67 |
|
| 68 |
function setUp() { |
| 69 |
targetDiv = goog.dom.getElement('targetDiv'); |
| 70 |
handler = new goog.ui.KeyboardShortcutHandler(goog.dom.getElement('rootDiv')); |
| 71 |
|
| 72 |
// Create a mock event listener in order to set expectations on what |
| 73 |
// events are fired. We create a fake class whose only method is |
| 74 |
// shortcutFired(shortcut identifier). |
| 75 |
listener = new goog.testing.StrictMock({shortcutFired: goog.nullFunction}); |
| 76 |
goog.events.listen( |
| 77 |
handler, goog.ui.KeyboardShortcutHandler.EventType.SHORTCUT_TRIGGERED, |
| 78 |
function(event) { listener.shortcutFired(event.identifier); }); |
| 79 |
|
| 80 |
// Set up a fake clock, because keyboard shortcuts *are* time |
| 81 |
// sensitive. |
| 82 |
mockClock = new goog.testing.MockClock(true); |
| 83 |
} |
| 84 |
|
| 85 |
function tearDown() { |
| 86 |
mockClock.uninstall(); |
| 87 |
handler.dispose(); |
| 88 |
stubs.reset(); |
| 89 |
} |
| 90 |
|
| 91 |
function testAllowsSingleLetterKeyBindingsSpecifiedAsString() { |
| 92 |
listener.shortcutFired('lettergee'); |
| 93 |
listener.$replay(); |
| 94 |
|
| 95 |
handler.registerShortcut('lettergee', 'g'); |
| 96 |
fire(KeyCodes.G); |
| 97 |
|
| 98 |
listener.$verify(); |
| 99 |
} |
| 100 |
|
| 101 |
function testAllowsSingleLetterKeyBindingsSpecifiedAsStringKeyValue() { |
| 102 |
listener.shortcutFired('lettergee'); |
| 103 |
listener.$replay(); |
| 104 |
|
| 105 |
handler.registerShortcut('lettergee', 'g'); |
| 106 |
fire('g'); |
| 107 |
|
| 108 |
listener.$verify(); |
| 109 |
} |
| 110 |
|
| 111 |
function testAllowsSingleLetterKeyBindingsSpecifiedAsKeyCode() { |
| 112 |
listener.shortcutFired('lettergee'); |
| 113 |
listener.$replay(); |
| 114 |
|
| 115 |
handler.registerShortcut('lettergee', KeyCodes.G); |
| 116 |
fire(KeyCodes.G); |
| 117 |
|
| 118 |
listener.$verify(); |
| 119 |
} |
| 120 |
|
| 121 |
function testDoesntFireWhenWrongKeyIsPressed() { |
| 122 |
listener.$replay(); // no events expected |
| 123 |
|
| 124 |
handler.registerShortcut('letterjay', 'j'); |
| 125 |
fire(KeyCodes.G); |
| 126 |
fire('g'); |
| 127 |
|
| 128 |
listener.$verify(); |
| 129 |
} |
| 130 |
|
| 131 |
function testAllowsControlAndLetterSpecifiedAsAString() { |
| 132 |
listener.shortcutFired('lettergee'); |
| 133 |
listener.$replay(); |
| 134 |
|
| 135 |
handler.registerShortcut('lettergee', 'ctrl+g'); |
| 136 |
fire(KeyCodes.G, {ctrlKey: true}); |
| 137 |
|
| 138 |
listener.$verify(); |
| 139 |
} |
| 140 |
|
| 141 |
function testAllowsControlAndLetterSpecifiedAsAStringKeyValue() { |
| 142 |
listener.shortcutFired('lettergee'); |
| 143 |
listener.$replay(); |
| 144 |
|
| 145 |
handler.registerShortcut('lettergee', 'ctrl+g'); |
| 146 |
fire('g', {ctrlKey: true}); |
| 147 |
|
| 148 |
listener.$verify(); |
| 149 |
} |
| 150 |
|
| 151 |
function testAllowsControlAndLetterSpecifiedAsArgSequence() { |
| 152 |
listener.shortcutFired('lettergeectrl'); |
| 153 |
listener.$replay(); |
| 154 |
|
| 155 |
handler.registerShortcut('lettergeectrl', KeyCodes.G, Modifiers.CTRL); |
| 156 |
fire(KeyCodes.G, {ctrlKey: true}); |
| 157 |
|
| 158 |
listener.$verify(); |
| 159 |
} |
| 160 |
|
| 161 |
function testAllowsControlAndLetterSpecifiedAsArray() { |
| 162 |
listener.shortcutFired('lettergeectrl'); |
| 163 |
listener.$replay(); |
| 164 |
|
| 165 |
handler.registerShortcut('lettergeectrl', [KeyCodes.G, Modifiers.CTRL]); |
| 166 |
fire(KeyCodes.G, {ctrlKey: true}); |
| 167 |
|
| 168 |
listener.$verify(); |
| 169 |
} |
| 170 |
|
| 171 |
function testAllowsShift() { |
| 172 |
listener.shortcutFired('lettergeeshift'); |
| 173 |
listener.$replay(); |
| 174 |
|
| 175 |
handler.registerShortcut('lettergeeshift', [KeyCodes.G, Modifiers.SHIFT]); |
| 176 |
fire(KeyCodes.G, {shiftKey: true}); |
| 177 |
|
| 178 |
listener.$verify(); |
| 179 |
} |
| 180 |
|
| 181 |
function testAllowsAlt() { |
| 182 |
listener.shortcutFired('lettergeealt'); |
| 183 |
listener.$replay(); |
| 184 |
|
| 185 |
handler.registerShortcut('lettergeealt', [KeyCodes.G, Modifiers.ALT]); |
| 186 |
fire(KeyCodes.G, {altKey: true}); |
| 187 |
|
| 188 |
listener.$verify(); |
| 189 |
} |
| 190 |
|
| 191 |
function testAllowsMeta() { |
| 192 |
listener.shortcutFired('lettergeemeta'); |
| 193 |
listener.$replay(); |
| 194 |
|
| 195 |
handler.registerShortcut('lettergeemeta', [KeyCodes.G, Modifiers.META]); |
| 196 |
fire(KeyCodes.G, {metaKey: true}); |
| 197 |
|
| 198 |
listener.$verify(); |
| 199 |
} |
| 200 |
|
| 201 |
function testAllowsMultipleModifiers() { |
| 202 |
listener.shortcutFired('lettergeectrlaltshift'); |
| 203 |
listener.$replay(); |
| 204 |
|
| 205 |
handler.registerShortcut( |
| 206 |
'lettergeectrlaltshift', KeyCodes.G, |
| 207 |
Modifiers.CTRL | Modifiers.ALT | Modifiers.SHIFT); |
| 208 |
fireAltGraphKey(KeyCodes.G, 0, {ctrlKey: true, altKey: true, shiftKey: true}); |
| 209 |
|
| 210 |
listener.$verify(); |
| 211 |
} |
| 212 |
|
| 213 |
function testAllowsMultipleModifiersSpecifiedAsString() { |
| 214 |
listener.shortcutFired('lettergeectrlaltshiftmeta'); |
| 215 |
listener.$replay(); |
| 216 |
|
| 217 |
handler.registerShortcut( |
| 218 |
'lettergeectrlaltshiftmeta', 'ctrl+shift+alt+meta+g'); |
| 219 |
fireAltGraphKey( |
| 220 |
KeyCodes.G, 0, |
| 221 |
{ctrlKey: true, altKey: true, shiftKey: true, metaKey: true}); |
| 222 |
|
| 223 |
listener.$verify(); |
| 224 |
} |
| 225 |
|
| 226 |
function testPreventsDefaultOnReturnFalse() { |
| 227 |
listener.shortcutFired('x'); |
| 228 |
listener.$replay(); |
| 229 |
|
| 230 |
handler.registerShortcut('x', 'x'); |
| 231 |
var key = goog.events.listen( |
| 232 |
handler, goog.ui.KeyboardShortcutHandler.EventType.SHORTCUT_TRIGGERED, |
| 233 |
function(event) { return false }); |
| 234 |
|
| 235 |
assertFalse( |
| 236 |
'return false in listener must prevent default', fire(KeyCodes.X)); |
| 237 |
|
| 238 |
listener.$verify(); |
| 239 |
|
| 240 |
goog.events.unlistenByKey(key); |
| 241 |
} |
| 242 |
|
| 243 |
function testPreventsDefaultWhenExceptionThrown() { |
| 244 |
handler.registerShortcut('x', 'x'); |
| 245 |
handler.setAlwaysPreventDefault(true); |
| 246 |
goog.events.listenOnce( |
| 247 |
handler, goog.ui.KeyboardShortcutHandler.EventType.SHORTCUT_TRIGGERED, |
| 248 |
function(event) { throw new Error('x'); }); |
| 249 |
|
| 250 |
// We can't use the standard infrastructure to detect that |
| 251 |
// the event was preventDefaulted, because of the exception. |
| 252 |
var callCount = 0; |
| 253 |
stubs.set(goog.events.BrowserEvent.prototype, 'preventDefault', function() { |
| 254 |
callCount++; |
| 255 |
}); |
| 256 |
|
| 257 |
var e = assertThrows(goog.partial(fire, KeyCodes.X)); |
| 258 |
assertEquals('x', e.message); |
| 259 |
|
| 260 |
assertEquals(1, callCount); |
| 261 |
} |
| 262 |
|
| 263 |
function testDoesntFireWhenUserForgetsRequiredModifier() { |
| 264 |
listener.$replay(); // no events expected |
| 265 |
|
| 266 |
handler.registerShortcut('lettergeectrl', KeyCodes.G, Modifiers.CTRL); |
| 267 |
fire(KeyCodes.G); |
| 268 |
|
| 269 |
listener.$verify(); |
| 270 |
} |
| 271 |
|
| 272 |
function testDoesntFireIfTooManyModifiersPressed() { |
| 273 |
listener.$replay(); // no events expected |
| 274 |
|
| 275 |
handler.registerShortcut('lettergeectrl', KeyCodes.G, Modifiers.CTRL); |
| 276 |
fire(KeyCodes.G, {ctrlKey: true, metaKey: true}); |
| 277 |
|
| 278 |
listener.$verify(); |
| 279 |
} |
| 280 |
|
| 281 |
function testDoesntFireIfAnyRequiredModifierForgotten() { |
| 282 |
listener.$replay(); // no events expected |
| 283 |
|
| 284 |
handler.registerShortcut( |
| 285 |
'lettergeectrlaltshift', KeyCodes.G, |
| 286 |
Modifiers.CTRL | Modifiers.ALT | Modifiers.SHIFT); |
| 287 |
fire(KeyCodes.G, {altKey: true, shiftKey: true}); |
| 288 |
|
| 289 |
listener.$verify(); |
| 290 |
} |
| 291 |
|
| 292 |
function testAllowsMultiKeySequenceSpecifiedAsArray() { |
| 293 |
listener.shortcutFired('quitemacs'); |
| 294 |
listener.$replay(); |
| 295 |
|
| 296 |
handler.registerShortcut( |
| 297 |
'quitemacs', [KeyCodes.X, Modifiers.CTRL, KeyCodes.C]); |
| 298 |
assertFalse(fire(KeyCodes.X, {ctrlKey: true})); |
| 299 |
fire(KeyCodes.C); |
| 300 |
|
| 301 |
listener.$verify(); |
| 302 |
} |
| 303 |
|
| 304 |
function testAllowsMultiKeySequenceSpecifiedAsArguments() { |
| 305 |
listener.shortcutFired('quitvi'); |
| 306 |
listener.$replay(); |
| 307 |
|
| 308 |
handler.registerShortcut( |
| 309 |
'quitvi', KeyCodes.SEMICOLON, Modifiers.SHIFT, KeyCodes.Q, Modifiers.NONE, |
| 310 |
KeyCodes.NUM_ONE, Modifiers.SHIFT); |
| 311 |
var shiftProperties = {shiftKey: true}; |
| 312 |
assertFalse(fire(KeyCodes.SEMICOLON, shiftProperties)); |
| 313 |
assertFalse(fire(KeyCodes.Q)); |
| 314 |
fire(KeyCodes.NUM_ONE, shiftProperties); |
| 315 |
|
| 316 |
listener.$verify(); |
| 317 |
} |
| 318 |
|
| 319 |
function testMultiKeyEventIsNotFiredIfUserIsTooSlow() { |
| 320 |
listener.$replay(); // no events expected |
| 321 |
|
| 322 |
handler.registerShortcut( |
| 323 |
'quitemacs', [KeyCodes.X, Modifiers.CTRL, KeyCodes.C]); |
| 324 |
|
| 325 |
fire(KeyCodes.X, {ctrlKey: true}); |
| 326 |
|
| 327 |
// Wait 3 seconds before hitting C. Although the actual limit is 1500 |
| 328 |
// at time of writing, it's best not to over-specify functionality. |
| 329 |
mockClock.tick(3000); |
| 330 |
|
| 331 |
fire(KeyCodes.C); |
| 332 |
|
| 333 |
listener.$verify(); |
| 334 |
} |
| 335 |
|
| 336 |
function testAllowsMultipleAHandlers() { |
| 337 |
listener.shortcutFired('quitvi'); |
| 338 |
listener.shortcutFired('letterex'); |
| 339 |
listener.shortcutFired('quitemacs'); |
| 340 |
listener.$replay(); |
| 341 |
|
| 342 |
// register 3 handlers in 3 diferent ways |
| 343 |
handler.registerShortcut( |
| 344 |
'quitvi', KeyCodes.SEMICOLON, Modifiers.SHIFT, KeyCodes.Q, Modifiers.NONE, |
| 345 |
KeyCodes.NUM_ONE, Modifiers.SHIFT); |
| 346 |
handler.registerShortcut( |
| 347 |
'quitemacs', [KeyCodes.X, Modifiers.CTRL, KeyCodes.C]); |
| 348 |
handler.registerShortcut('letterex', 'x'); |
| 349 |
|
| 350 |
|
| 351 |
// quit vi |
| 352 |
var shiftProperties = {shiftKey: true}; |
| 353 |
fire(KeyCodes.SEMICOLON, shiftProperties); |
| 354 |
fire(KeyCodes.Q); |
| 355 |
fire(KeyCodes.NUM_ONE, shiftProperties); |
| 356 |
|
| 357 |
// then press the letter x |
| 358 |
fire(KeyCodes.X); |
| 359 |
|
| 360 |
// then quit emacs |
| 361 |
fire(KeyCodes.X, {ctrlKey: true}); |
| 362 |
fire(KeyCodes.C); |
| 363 |
|
| 364 |
listener.$verify(); |
| 365 |
} |
| 366 |
|
| 367 |
function testCanRemoveOneHandler() { |
| 368 |
listener.shortcutFired('letterex'); |
| 369 |
listener.$replay(); |
| 370 |
|
| 371 |
// register 2 handlers, then remove quitvi |
| 372 |
handler.registerShortcut( |
| 373 |
'quitvi', KeyCodes.COLON, Modifiers.NONE, KeyCodes.Q, Modifiers.NONE, |
| 374 |
KeyCodes.EXCLAMATION, Modifiers.NONE); |
| 375 |
handler.registerShortcut('letterex', 'x'); |
| 376 |
handler.unregisterShortcut( |
| 377 |
KeyCodes.COLON, Modifiers.NONE, KeyCodes.Q, Modifiers.NONE, |
| 378 |
KeyCodes.EXCLAMATION, Modifiers.NONE); |
| 379 |
|
| 380 |
// call the "quit VI" keycodes, even though it is removed |
| 381 |
fire(KeyCodes.COLON); |
| 382 |
fire(KeyCodes.Q); |
| 383 |
fire(KeyCodes.EXCLAMATION); |
| 384 |
|
| 385 |
// press the letter x |
| 386 |
fire(KeyCodes.X); |
| 387 |
|
| 388 |
listener.$verify(); |
| 389 |
} |
| 390 |
|
| 391 |
function testCanRemoveTwoHandlers() { |
| 392 |
listener.$replay(); // no events expected |
| 393 |
|
| 394 |
handler.registerShortcut( |
| 395 |
'quitemacs', [KeyCodes.X, Modifiers.CTRL, KeyCodes.C]); |
| 396 |
handler.registerShortcut('letterex', 'x'); |
| 397 |
handler.unregisterShortcut([KeyCodes.X, Modifiers.CTRL, KeyCodes.C]); |
| 398 |
handler.unregisterShortcut('x'); |
| 399 |
|
| 400 |
fire(KeyCodes.X, {ctrlKey: true}); |
| 401 |
fire(KeyCodes.C); |
| 402 |
fire(KeyCodes.X); |
| 403 |
|
| 404 |
listener.$verify(); |
| 405 |
} |
| 406 |
|
| 407 |
function testIsShortcutRegistered_single() { |
| 408 |
assertFalse(handler.isShortcutRegistered('x')); |
| 409 |
handler.registerShortcut('letterex', 'x'); |
| 410 |
assertTrue(handler.isShortcutRegistered('x')); |
| 411 |
handler.unregisterShortcut('x'); |
| 412 |
assertFalse(handler.isShortcutRegistered('x')); |
| 413 |
} |
| 414 |
|
| 415 |
function testIsShortcutRegistered_multi() { |
| 416 |
assertFalse(handler.isShortcutRegistered('a')); |
| 417 |
assertFalse(handler.isShortcutRegistered('a b')); |
| 418 |
assertFalse(handler.isShortcutRegistered('a b c')); |
| 419 |
|
| 420 |
handler.registerShortcut('ab', 'a b'); |
| 421 |
|
| 422 |
assertFalse(handler.isShortcutRegistered('a')); |
| 423 |
assertTrue(handler.isShortcutRegistered('a b')); |
| 424 |
assertFalse(handler.isShortcutRegistered('a b c')); |
| 425 |
|
| 426 |
handler.unregisterShortcut('a b'); |
| 427 |
|
| 428 |
assertFalse(handler.isShortcutRegistered('a')); |
| 429 |
assertFalse(handler.isShortcutRegistered('a b')); |
| 430 |
assertFalse(handler.isShortcutRegistered('a b c')); |
| 431 |
} |
| 432 |
|
| 433 |
function testUnregister_subsequence() { |
| 434 |
// Unregistering a partial sequence should not orphan shortcuts further in the |
| 435 |
// sequence. |
| 436 |
handler.registerShortcut('abc', 'a b c'); |
| 437 |
handler.unregisterShortcut('a b'); |
| 438 |
assertTrue(handler.isShortcutRegistered('a b c')); |
| 439 |
} |
| 440 |
|
| 441 |
function testUnregister_supersequence() { |
| 442 |
// Unregistering a sequence that extends beyond a registered sequence should |
| 443 |
// do nothing. |
| 444 |
handler.registerShortcut('ab', 'a b'); |
| 445 |
handler.unregisterShortcut('a b c'); |
| 446 |
assertTrue(handler.isShortcutRegistered('a b')); |
| 447 |
} |
| 448 |
|
| 449 |
function testUnregister_partialMatchSequence() { |
| 450 |
// Unregistering a sequence that partially matches a registered sequence |
| 451 |
// should do nothing. |
| 452 |
handler.registerShortcut('abc', 'a b c'); |
| 453 |
handler.unregisterShortcut('a b x'); |
| 454 |
assertTrue(handler.isShortcutRegistered('a b c')); |
| 455 |
} |
| 456 |
|
| 457 |
function testUnregister_deadBranch() { |
| 458 |
// Unregistering a sequence should prune any dead branches in the tree. |
| 459 |
handler.registerShortcut('abc', 'a b c'); |
| 460 |
handler.unregisterShortcut('a b c'); |
| 461 |
// Default is not should not be prevented in the A key stroke because the A |
| 462 |
// branch has been removed from the tree. |
| 463 |
assertTrue(fire(KeyCodes.A)); |
| 464 |
} |
| 465 |
|
| 466 |
|
| 467 |
/** |
| 468 |
* Registers a slew of keyboard shortcuts to test each primary category |
| 469 |
* of shortcuts. |
| 470 |
*/ |
| 471 |
function registerEnterSpaceXF1AltY() { |
| 472 |
// Enter and space are specially handled keys. |
| 473 |
handler.registerShortcut('enter', KeyCodes.ENTER); |
| 474 |
handler.registerShortcut('space', KeyCodes.SPACE); |
| 475 |
// 'x' should be treated as text in many contexts |
| 476 |
handler.registerShortcut('x', 'x'); |
| 477 |
// F1 is a global shortcut. |
| 478 |
handler.registerShortcut('global', KeyCodes.F1); |
| 479 |
// Alt-Y has modifiers, which pass through most form elements. |
| 480 |
handler.registerShortcut('withAlt', 'alt+y'); |
| 481 |
} |
| 482 |
|
| 483 |
|
| 484 |
/** |
| 485 |
* Fires enter, space, X, F1, and Alt-Y keys on a widget. |
| 486 |
* @param {Element} target The element on which to fire the events. |
| 487 |
*/ |
| 488 |
function fireEnterSpaceXF1AltY(target) { |
| 489 |
fire(KeyCodes.ENTER, undefined, target); |
| 490 |
fire(KeyCodes.SPACE, undefined, target); |
| 491 |
fire(KeyCodes.X, undefined, target); |
| 492 |
fire(KeyCodes.F1, undefined, target); |
| 493 |
fire(KeyCodes.Y, {altKey: true}, target); |
| 494 |
} |
| 495 |
|
| 496 |
function testIgnoreNonGlobalShortcutsInSelect() { |
| 497 |
var targetSelect = goog.dom.getElement('targetSelect'); |
| 498 |
|
| 499 |
listener.shortcutFired('global'); |
| 500 |
listener.shortcutFired('withAlt'); |
| 501 |
listener.$replay(); |
| 502 |
|
| 503 |
registerEnterSpaceXF1AltY(); |
| 504 |
fireEnterSpaceXF1AltY(goog.dom.getElement('targetSelect')); |
| 505 |
|
| 506 |
listener.$verify(); |
| 507 |
} |
| 508 |
|
| 509 |
function testIgnoreNonGlobalShortcutsInTextArea() { |
| 510 |
listener.shortcutFired('global'); |
| 511 |
listener.shortcutFired('withAlt'); |
| 512 |
listener.$replay(); |
| 513 |
|
| 514 |
registerEnterSpaceXF1AltY(); |
| 515 |
fireEnterSpaceXF1AltY(goog.dom.getElement('targetTextArea')); |
| 516 |
|
| 517 |
listener.$verify(); |
| 518 |
} |
| 519 |
|
| 520 |
|
| 521 |
/** |
| 522 |
* Checks that the shortcuts are fired on each target. |
| 523 |
* @param {Array<string>} shortcuts A list of shortcut identifiers. |
| 524 |
* @param {Array<string>} targets A list of element IDs. |
| 525 |
* @param {function(Element)} fireEvents Function that fires events. |
| 526 |
*/ |
| 527 |
function expectShortcutsOnTargets(shortcuts, targets, fireEvents) { |
| 528 |
for (var i = 0, ii = targets.length; i < ii; i++) { |
| 529 |
for (var j = 0, jj = shortcuts.length; j < jj; j++) { |
| 530 |
listener.shortcutFired(shortcuts[j]); |
| 531 |
} |
| 532 |
listener.$replay(); |
| 533 |
fireEvents(goog.dom.getElement(targets[i])); |
| 534 |
listener.$verify(); |
| 535 |
listener.$reset(); |
| 536 |
} |
| 537 |
} |
| 538 |
|
| 539 |
function testIgnoreShortcutsExceptEnterInTextInputFields() { |
| 540 |
var targets = [ |
| 541 |
'targetColor', 'targetDate', 'targetDateTime', 'targetDateTimeLocal', |
| 542 |
'targetEmail', 'targetMonth', 'targetNumber', 'targetPassword', |
| 543 |
'targetSearch', 'targetTel', 'targetText', 'targetTime', 'targetUrl', |
| 544 |
'targetWeek' |
| 545 |
]; |
| 546 |
registerEnterSpaceXF1AltY(); |
| 547 |
expectShortcutsOnTargets( |
| 548 |
['enter', 'global', 'withAlt'], targets, fireEnterSpaceXF1AltY); |
| 549 |
} |
| 550 |
|
| 551 |
function testIgnoreSpaceInCheckBoxAndButton() { |
| 552 |
registerEnterSpaceXF1AltY(); |
| 553 |
expectShortcutsOnTargets( |
| 554 |
['enter', 'x', 'global', 'withAlt'], ['targetCheckBox', 'targetButton'], |
| 555 |
fireEnterSpaceXF1AltY); |
| 556 |
} |
| 557 |
|
| 558 |
function testIgnoreNonGlobalShortcutsInContentEditable() { |
| 559 |
// Don't set design mode in later IE as javascripts don't run when in |
| 560 |
// that mode. |
| 561 |
var setDesignMode = |
| 562 |
!goog.userAgent.IE || !goog.userAgent.isVersionOrHigher('9'); |
| 563 |
try { |
| 564 |
if (setDesignMode) { |
| 565 |
document.designMode = 'on'; |
| 566 |
} |
| 567 |
targetDiv.contentEditable = 'true'; |
| 568 |
|
| 569 |
// Expect only global shortcuts. |
| 570 |
listener.shortcutFired('global'); |
| 571 |
listener.$replay(); |
| 572 |
|
| 573 |
registerEnterSpaceXF1AltY(); |
| 574 |
fireEnterSpaceXF1AltY(targetDiv); |
| 575 |
|
| 576 |
listener.$verify(); |
| 577 |
} finally { |
| 578 |
if (setDesignMode) { |
| 579 |
document.designMode = 'off'; |
| 580 |
} |
| 581 |
targetDiv.contentEditable = 'false'; |
| 582 |
} |
| 583 |
} |
| 584 |
|
| 585 |
function testSetAllShortcutsAreGlobal() { |
| 586 |
handler.setAllShortcutsAreGlobal(true); |
| 587 |
registerEnterSpaceXF1AltY(); |
| 588 |
|
| 589 |
expectShortcutsOnTargets( |
| 590 |
['enter', 'space', 'x', 'global', 'withAlt'], ['targetTextArea'], |
| 591 |
fireEnterSpaceXF1AltY); |
| 592 |
} |
| 593 |
|
| 594 |
function testSetModifierShortcutsAreGlobalFalse() { |
| 595 |
handler.setModifierShortcutsAreGlobal(false); |
| 596 |
registerEnterSpaceXF1AltY(); |
| 597 |
|
| 598 |
expectShortcutsOnTargets( |
| 599 |
['global'], ['targetTextArea'], fireEnterSpaceXF1AltY); |
| 600 |
} |
| 601 |
|
| 602 |
function testAltGraphKeyOnUSLayout() { |
| 603 |
// Windows does not assign printable characters to any ctrl+alt keys of |
| 604 |
// the US layout. This test verifies we fire shortcut events when typing |
| 605 |
// ctrl+alt keys on the US layout. |
| 606 |
listener.shortcutFired('letterOne'); |
| 607 |
listener.shortcutFired('letterTwo'); |
| 608 |
listener.shortcutFired('letterThree'); |
| 609 |
listener.shortcutFired('letterFour'); |
| 610 |
listener.shortcutFired('letterFive'); |
| 611 |
if (goog.userAgent.WINDOWS) { |
| 612 |
listener.$replay(); |
| 613 |
|
| 614 |
handler.registerShortcut('letterOne', 'ctrl+alt+1'); |
| 615 |
handler.registerShortcut('letterTwo', 'ctrl+alt+2'); |
| 616 |
handler.registerShortcut('letterThree', 'ctrl+alt+3'); |
| 617 |
handler.registerShortcut('letterFour', 'ctrl+alt+4'); |
| 618 |
handler.registerShortcut('letterFive', 'ctrl+alt+5'); |
| 619 |
|
| 620 |
// Send key events on the English (United States) layout. |
| 621 |
fireAltGraphKey(KeyCodes.ONE, 0, {ctrlKey: true, altKey: true}); |
| 622 |
fireAltGraphKey(KeyCodes.TWO, 0, {ctrlKey: true, altKey: true}); |
| 623 |
fireAltGraphKey(KeyCodes.THREE, 0, {ctrlKey: true, altKey: true}); |
| 624 |
fireAltGraphKey(KeyCodes.FOUR, 0, {ctrlKey: true, altKey: true}); |
| 625 |
fireAltGraphKey(KeyCodes.FIVE, 0, {ctrlKey: true, altKey: true}); |
| 626 |
|
| 627 |
listener.$verify(); |
| 628 |
} |
| 629 |
} |
| 630 |
|
| 631 |
function testAltGraphKeyOnFrenchLayout() { |
| 632 |
// Windows assigns printable characters to ctrl+alt+[2-5] keys of the |
| 633 |
// French layout. This test verifies we fire shortcut events only when |
| 634 |
// we type ctrl+alt+1 keys on the French layout. |
| 635 |
listener.shortcutFired('letterOne'); |
| 636 |
if (goog.userAgent.WINDOWS) { |
| 637 |
listener.$replay(); |
| 638 |
|
| 639 |
handler.registerShortcut('letterOne', 'ctrl+alt+1'); |
| 640 |
handler.registerShortcut('letterTwo', 'ctrl+alt+2'); |
| 641 |
handler.registerShortcut('letterThree', 'ctrl+alt+3'); |
| 642 |
handler.registerShortcut('letterFour', 'ctrl+alt+4'); |
| 643 |
handler.registerShortcut('letterFive', 'ctrl+alt+5'); |
| 644 |
|
| 645 |
// Send key events on the French (France) layout. |
| 646 |
fireAltGraphKey(KeyCodes.ONE, 0, {ctrlKey: true, altKey: true}); |
| 647 |
fireAltGraphKey(KeyCodes.TWO, 0x0303, {ctrlKey: true, altKey: true}); |
| 648 |
fireAltGraphKey(KeyCodes.THREE, 0x0023, {ctrlKey: true, altKey: true}); |
| 649 |
fireAltGraphKey(KeyCodes.FOUR, 0x007b, {ctrlKey: true, altKey: true}); |
| 650 |
fireAltGraphKey(KeyCodes.FIVE, 0x205b, {ctrlKey: true, altKey: true}); |
| 651 |
|
| 652 |
listener.$verify(); |
| 653 |
} |
| 654 |
} |
| 655 |
|
| 656 |
function testAltGraphKeyOnSpanishLayout() { |
| 657 |
// Windows assigns printable characters to ctrl+alt+[1-5] keys of the |
| 658 |
// Spanish layout. This test verifies we do not fire shortcut events at |
| 659 |
// all when typing ctrl+alt+[1-5] keys on the Spanish layout. |
| 660 |
if (goog.userAgent.WINDOWS) { |
| 661 |
listener.$replay(); |
| 662 |
|
| 663 |
handler.registerShortcut('letterOne', 'ctrl+alt+1'); |
| 664 |
handler.registerShortcut('letterTwo', 'ctrl+alt+2'); |
| 665 |
handler.registerShortcut('letterThree', 'ctrl+alt+3'); |
| 666 |
handler.registerShortcut('letterFour', 'ctrl+alt+4'); |
| 667 |
handler.registerShortcut('letterFive', 'ctrl+alt+5'); |
| 668 |
|
| 669 |
// Send key events on the Spanish (Spain) layout. |
| 670 |
fireAltGraphKey(KeyCodes.ONE, 0x007c, {ctrlKey: true, altKey: true}); |
| 671 |
fireAltGraphKey(KeyCodes.TWO, 0x0040, {ctrlKey: true, altKey: true}); |
| 672 |
fireAltGraphKey(KeyCodes.THREE, 0x0023, {ctrlKey: true, altKey: true}); |
| 673 |
fireAltGraphKey(KeyCodes.FOUR, 0x0303, {ctrlKey: true, altKey: true}); |
| 674 |
fireAltGraphKey(KeyCodes.FIVE, 0x20ac, {ctrlKey: true, altKey: true}); |
| 675 |
|
| 676 |
listener.$verify(); |
| 677 |
} |
| 678 |
} |
| 679 |
|
| 680 |
function testAltGraphKeyOnPolishLayout_withShift() { |
| 681 |
// Windows assigns printable characters to ctrl+alt+shift+A key in polish |
| 682 |
// layout. This test verifies that we do not fire shortcut events for A, but |
| 683 |
// does fire for Q which does not have a printable character. |
| 684 |
if (goog.userAgent.WINDOWS) { |
| 685 |
listener.shortcutFired('letterQ'); |
| 686 |
listener.$replay(); |
| 687 |
|
| 688 |
handler.registerShortcut('letterA', 'ctrl+alt+shift+A'); |
| 689 |
handler.registerShortcut('letterQ', 'ctrl+alt+shift+Q'); |
| 690 |
|
| 691 |
// Send key events on the Polish (Programmer) layout. |
| 692 |
assertTrue(fireAltGraphKey( |
| 693 |
KeyCodes.A, 0x0104, {ctrlKey: true, altKey: true, shiftKey: true})); |
| 694 |
assertFalse(fireAltGraphKey( |
| 695 |
KeyCodes.Q, 0, {ctrlKey: true, altKey: true, shiftKey: true})); |
| 696 |
|
| 697 |
listener.$verify(); |
| 698 |
} |
| 699 |
} |
| 700 |
|
| 701 |
function testNumpadKeyShortcuts() { |
| 702 |
var testCases = [ |
| 703 |
['letterNumpad0', 'num-0', KeyCodes.NUM_ZERO], |
| 704 |
['letterNumpad1', 'num-1', KeyCodes.NUM_ONE], |
| 705 |
['letterNumpad2', 'num-2', KeyCodes.NUM_TWO], |
| 706 |
['letterNumpad3', 'num-3', KeyCodes.NUM_THREE], |
| 707 |
['letterNumpad4', 'num-4', KeyCodes.NUM_FOUR], |
| 708 |
['letterNumpad5', 'num-5', KeyCodes.NUM_FIVE], |
| 709 |
['letterNumpad6', 'num-6', KeyCodes.NUM_SIX], |
| 710 |
['letterNumpad7', 'num-7', KeyCodes.NUM_SEVEN], |
| 711 |
['letterNumpad8', 'num-8', KeyCodes.NUM_EIGHT], |
| 712 |
['letterNumpad9', 'num-9', KeyCodes.NUM_NINE], |
| 713 |
['letterNumpadMultiply', 'num-multiply', KeyCodes.NUM_MULTIPLY], |
| 714 |
['letterNumpadPlus', 'num-plus', KeyCodes.NUM_PLUS], |
| 715 |
['letterNumpadMinus', 'num-minus', KeyCodes.NUM_MINUS], |
| 716 |
['letterNumpadPERIOD', 'num-period', KeyCodes.NUM_PERIOD], |
| 717 |
['letterNumpadDIVISION', 'num-division', KeyCodes.NUM_DIVISION] |
| 718 |
]; |
| 719 |
for (var i = 0; i < testCases.length; ++i) { |
| 720 |
listener.shortcutFired(testCases[i][0]); |
| 721 |
} |
| 722 |
listener.$replay(); |
| 723 |
|
| 724 |
// Register shortcuts for numpad keys and send numpad-key events. |
| 725 |
for (var i = 0; i < testCases.length; ++i) { |
| 726 |
handler.registerShortcut(testCases[i][0], testCases[i][1]); |
| 727 |
fire(testCases[i][2]); |
| 728 |
} |
| 729 |
listener.$verify(); |
| 730 |
} |
| 731 |
|
| 732 |
function testGeckoShortcuts() { |
| 733 |
listener.shortcutFired('1'); |
| 734 |
listener.$replay(); |
| 735 |
|
| 736 |
handler.registerShortcut('1', 'semicolon'); |
| 737 |
|
| 738 |
if (goog.userAgent.GECKO) { |
| 739 |
fire(goog.events.KeyCodes.FF_SEMICOLON); |
| 740 |
} else { |
| 741 |
fire(goog.events.KeyCodes.SEMICOLON); |
| 742 |
} |
| 743 |
|
| 744 |
listener.$verify(); |
| 745 |
} |
| 746 |
|
| 747 |
function testWindows_multiKeyShortcuts() { |
| 748 |
if (goog.userAgent.WINDOWS) { |
| 749 |
listener.shortcutFired('nextComment'); |
| 750 |
listener.$replay(); |
| 751 |
|
| 752 |
handler.registerShortcut('nextComment', 'ctrl+alt+n ctrl+alt+c'); |
| 753 |
// We need to specify a keyPressKeyCode of 0 here because on Windows, |
| 754 |
// keystrokes that don't produce printable characters don't cause a keyPress |
| 755 |
// event to fire. |
| 756 |
assertFalse(fireAltGraphKey(KeyCodes.N, 0, {ctrlKey: true, altKey: true})); |
| 757 |
assertFalse(fireAltGraphKey(KeyCodes.C, 0, {ctrlKey: true, altKey: true})); |
| 758 |
listener.$verify(); |
| 759 |
} |
| 760 |
} |
| 761 |
|
| 762 |
function testWindows_multikeyShortcuts_repeatedKeyDoesntInterfere() { |
| 763 |
if (goog.userAgent.WINDOWS) { |
| 764 |
listener.shortcutFired('announceCursorLocation'); |
| 765 |
listener.$replay(); |
| 766 |
|
| 767 |
handler.registerShortcut('announceAnchorText', 'ctrl+alt+a ctrl+alt+a'); |
| 768 |
handler.registerShortcut('announceCursorLocation', 'ctrl+alt+a ctrl+alt+l'); |
| 769 |
|
| 770 |
// We need to specify a keyPressKeyCode of 0 here because on Windows, |
| 771 |
// keystrokes that don't produce printable characters don't cause a keyPress |
| 772 |
// event to fire. |
| 773 |
assertFalse(fireAltGraphKey(KeyCodes.A, 0, {ctrlKey: true, altKey: true})); |
| 774 |
assertFalse(fireAltGraphKey(KeyCodes.L, 0, {ctrlKey: true, altKey: true})); |
| 775 |
listener.$verify(); |
| 776 |
} |
| 777 |
} |
| 778 |
|
| 779 |
function testWindows_multikeyShortcuts_polishKey() { |
| 780 |
if (goog.userAgent.WINDOWS) { |
| 781 |
listener.$replay(); |
| 782 |
|
| 783 |
handler.registerShortcut('announceCursorLocation', 'ctrl+alt+a ctrl+alt+l'); |
| 784 |
|
| 785 |
// If a Polish key is a subsection of a keyboard shortcut, then |
| 786 |
// the key should still be written. |
| 787 |
assertTrue( |
| 788 |
fireAltGraphKey(KeyCodes.A, 0x0105, {ctrlKey: true, altKey: true})); |
| 789 |
listener.$verify(); |
| 790 |
} |
| 791 |
} |
| 792 |
|
| 793 |
function testRegisterShortcut_modifierOnly() { |
| 794 |
assertThrows( |
| 795 |
'Registering a shortcut with just modifiers should fail.', |
| 796 |
goog.bind(handler.registerShortcut, handler, 'name', 'Shift')); |
| 797 |
} |
| 798 |
|
| 799 |
function testParseStringShortcut_unknownKey() { |
| 800 |
assertThrows( |
| 801 |
'Unknown keys should fail.', |
| 802 |
goog.bind( |
| 803 |
goog.ui.KeyboardShortcutHandler.parseStringShortcut, null, |
| 804 |
'NotAKey')); |
| 805 |
} |
| 806 |
|
| 807 |
// Regression test for failure to reset keyCode between strokes. |
| 808 |
function testParseStringShortcut_resetKeyCode() { |
| 809 |
var strokes = goog.ui.KeyboardShortcutHandler.parseStringShortcut('A Shift'); |
| 810 |
assertNull('The second stroke only has a modifier key.', strokes[1].keyCode); |
| 811 |
} |