123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719 |
- // Copyright 2008 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.KeyEventTest');
- goog.setTestOnly('goog.events.KeyEventTest');
- goog.require('goog.dom');
- goog.require('goog.dom.TagName');
- goog.require('goog.events');
- goog.require('goog.events.BrowserEvent');
- goog.require('goog.events.EventType');
- goog.require('goog.events.KeyCodes');
- goog.require('goog.events.KeyHandler');
- goog.require('goog.testing.events');
- goog.require('goog.testing.jsunit');
- goog.require('goog.userAgent');
- function setUp() {
- // Have this based on a fictitious DOCUMENT_MODE constant.
- goog.userAgent.isDocumentMode = function(mode) {
- return mode <= goog.userAgent.DOCUMENT_MODE;
- };
- }
- /**
- * Tests the key handler for the IE 8 and lower behavior.
- */
- function testIe8StyleKeyHandling() {
- goog.userAgent.OPERA = false;
- goog.userAgent.IE = true;
- goog.userAgent.GECKO = false;
- goog.userAgent.WEBKIT = false;
- goog.userAgent.MAC = false;
- goog.userAgent.WINDOWS = true;
- goog.userAgent.LINUX = false;
- goog.userAgent.VERSION = 8;
- goog.userAgent.DOCUMENT_MODE = 8;
- goog.events.KeyHandler.USES_KEYDOWN_ = true;
- assertIe8StyleKeyHandling();
- }
- /**
- * Tests the key handler for the IE 8 and lower behavior.
- */
- function testIe8StyleKeyHandlingInIe9DocumentMode() {
- goog.userAgent.OPERA = false;
- goog.userAgent.IE = true;
- goog.userAgent.GECKO = false;
- goog.userAgent.WEBKIT = false;
- goog.userAgent.MAC = false;
- goog.userAgent.WINDOWS = true;
- goog.userAgent.LINUX = false;
- goog.userAgent.VERSION = 9; // Try IE9 in IE8 document mode.
- goog.userAgent.DOCUMENT_MODE = 8;
- goog.events.KeyHandler.USES_KEYDOWN_ = true;
- assertIe8StyleKeyHandling();
- }
- function assertIe8StyleKeyHandling() {
- var keyEvent, keyHandler = new goog.events.KeyHandler();
- goog.events.listen(
- keyHandler, goog.events.KeyHandler.EventType.KEY,
- function(e) { keyEvent = e; });
- fireKeyDown(keyHandler, goog.events.KeyCodes.ENTER);
- fireKeyPress(keyHandler, goog.events.KeyCodes.ENTER);
- assertEquals(
- 'Enter should fire a key event with the keycode 13',
- goog.events.KeyCodes.ENTER, keyEvent.keyCode);
- assertEquals(
- 'Enter should fire a key event with the charcode 0', 0,
- keyEvent.charCode);
- fireKeyDown(keyHandler, goog.events.KeyCodes.ESC);
- fireKeyPress(keyHandler, goog.events.KeyCodes.ESC);
- assertEquals(
- 'Esc should fire a key event with the keycode 27',
- goog.events.KeyCodes.ESC, keyEvent.keyCode);
- assertEquals(
- 'Esc should fire a key event with the charcode 0', 0, keyEvent.charCode);
- fireKeyDown(keyHandler, goog.events.KeyCodes.UP);
- assertEquals(
- 'Up should fire a key event with the keycode 38', goog.events.KeyCodes.UP,
- keyEvent.keyCode);
- assertEquals(
- 'Up should fire a key event with the charcode 0', 0, keyEvent.charCode);
- fireKeyDown(
- keyHandler, goog.events.KeyCodes.SEVEN, undefined, undefined, undefined,
- undefined, true);
- fireKeyPress(
- keyHandler, 38, undefined, undefined, undefined, undefined, true);
- assertEquals(
- 'Shift+7 should fire a key event with the keycode 55',
- goog.events.KeyCodes.SEVEN, keyEvent.keyCode);
- assertEquals(
- 'Shift+7 should fire a key event with the charcode 38', 38,
- keyEvent.charCode);
- fireKeyDown(keyHandler, goog.events.KeyCodes.A);
- fireKeyPress(keyHandler, 97);
- assertEquals(
- 'Lower case a should fire a key event with the keycode 65',
- goog.events.KeyCodes.A, keyEvent.keyCode);
- assertEquals(
- 'Lower case a should fire a key event with the charcode 97', 97,
- keyEvent.charCode);
- fireKeyDown(keyHandler, goog.events.KeyCodes.A);
- fireKeyPress(keyHandler, 65);
- assertEquals(
- 'Upper case A should fire a key event with the keycode 65',
- goog.events.KeyCodes.A, keyEvent.keyCode);
- assertEquals(
- 'Upper case A should fire a key event with the charcode 65', 65,
- keyEvent.charCode);
- fireKeyDown(keyHandler, goog.events.KeyCodes.DELETE);
- assertEquals(
- 'Delete should fire a key event with the keycode 46',
- goog.events.KeyCodes.DELETE, keyEvent.keyCode);
- assertEquals(
- 'Delete should fire a key event with the charcode 0', 0,
- keyEvent.charCode);
- fireKeyDown(keyHandler, goog.events.KeyCodes.PERIOD);
- fireKeyPress(keyHandler, 46);
- assertEquals(
- 'Period should fire a key event with the keycode 190',
- goog.events.KeyCodes.PERIOD, keyEvent.keyCode);
- assertEquals(
- 'Period should fire a key event with the charcode 46', 46,
- keyEvent.charCode);
- fireKeyDown(keyHandler, goog.events.KeyCodes.CTRL);
- fireKeyDown(keyHandler, goog.events.KeyCodes.A);
- assertEquals(
- 'A with control down should fire a key event', goog.events.KeyCodes.A,
- keyEvent.keyCode);
- // On IE, when Ctrl+<key> is held down, there is a KEYDOWN, a KEYPRESS, and
- // then a series of KEYDOWN events for each repeat.
- fireKeyDown(keyHandler, goog.events.KeyCodes.B, undefined, undefined, true);
- fireKeyPress(keyHandler, goog.events.KeyCodes.B, undefined, undefined, true);
- assertEquals(
- 'B with control down should fire a key event', goog.events.KeyCodes.B,
- keyEvent.keyCode);
- assertTrue('Ctrl should be down.', keyEvent.ctrlKey);
- assertFalse(
- 'Should not have repeat=true on the first key press.', keyEvent.repeat);
- // Fire one repeated keydown event.
- fireKeyDown(keyHandler, goog.events.KeyCodes.B, undefined, undefined, true);
- assertEquals(
- 'A with control down should fire a key event', goog.events.KeyCodes.B,
- keyEvent.keyCode);
- assertTrue('Should have repeat=true on key repeat.', keyEvent.repeat);
- assertTrue('Ctrl should be down.', keyEvent.ctrlKey);
- }
- /**
- * Tests special cases for IE9.
- */
- function testIe9StyleKeyHandling() {
- goog.userAgent.OPERA = false;
- goog.userAgent.IE = true;
- goog.userAgent.GECKO = false;
- goog.userAgent.WEBKIT = false;
- goog.userAgent.MAC = false;
- goog.userAgent.WINDOWS = true;
- goog.userAgent.LINUX = false;
- goog.userAgent.VERSION = 9;
- goog.userAgent.DOCUMENT_MODE = 9;
- goog.events.KeyHandler.USES_KEYDOWN_ = true;
- var keyEvent, keyHandler = new goog.events.KeyHandler();
- goog.events.listen(
- keyHandler, goog.events.KeyHandler.EventType.KEY,
- function(e) { keyEvent = e; });
- fireKeyDown(keyHandler, goog.events.KeyCodes.ENTER);
- fireKeyPress(keyHandler, goog.events.KeyCodes.ENTER);
- assertEquals(
- 'Enter should fire a key event with the keycode 13',
- goog.events.KeyCodes.ENTER, keyEvent.keyCode);
- assertEquals(
- 'Enter should fire a key event with the charcode 0', 0,
- keyEvent.charCode);
- }
- /**
- * Tests the key handler for the Gecko behavior.
- */
- function testGeckoStyleKeyHandling() {
- goog.userAgent.OPERA = false;
- goog.userAgent.IE = false;
- goog.userAgent.GECKO = true;
- goog.userAgent.WEBKIT = false;
- goog.userAgent.MAC = false;
- goog.userAgent.WINDOWS = true;
- goog.userAgent.LINUX = false;
- goog.events.KeyHandler.USES_KEYDOWN_ = false;
- var keyEvent, keyHandler = new goog.events.KeyHandler();
- goog.events.listen(
- keyHandler, goog.events.KeyHandler.EventType.KEY,
- function(e) { keyEvent = e; });
- fireKeyDown(keyHandler, goog.events.KeyCodes.ENTER);
- fireKeyPress(keyHandler, goog.events.KeyCodes.ENTER);
- assertEquals(
- 'Enter should fire a key event with the keycode 13',
- goog.events.KeyCodes.ENTER, keyEvent.keyCode);
- assertEquals(
- 'Enter should fire a key event with the charcode 0', 0,
- keyEvent.charCode);
- fireKeyDown(keyHandler, goog.events.KeyCodes.ESC);
- fireKeyPress(keyHandler, goog.events.KeyCodes.ESC);
- assertEquals(
- 'Esc should fire a key event with the keycode 27',
- goog.events.KeyCodes.ESC, keyEvent.keyCode);
- assertEquals(
- 'Esc should fire a key event with the charcode 0', 0, keyEvent.charCode);
- fireKeyDown(keyHandler, goog.events.KeyCodes.UP);
- fireKeyPress(keyHandler, goog.events.KeyCodes.UP);
- assertEquals(
- 'Up should fire a key event with the keycode 38', goog.events.KeyCodes.UP,
- keyEvent.keyCode);
- assertEquals(
- 'Up should fire a key event with the charcode 0', 0, keyEvent.charCode);
- fireKeyDown(
- keyHandler, goog.events.KeyCodes.SEVEN, undefined, undefined, undefined,
- undefined, true);
- fireKeyPress(
- keyHandler, undefined, 38, undefined, undefined, undefined, true);
- assertEquals(
- 'Shift+7 should fire a key event with the keycode 55',
- goog.events.KeyCodes.SEVEN, keyEvent.keyCode);
- assertEquals(
- 'Shift+7 should fire a key event with the charcode 38', 38,
- keyEvent.charCode);
- fireKeyDown(keyHandler, goog.events.KeyCodes.A);
- fireKeyPress(keyHandler, undefined, 97);
- assertEquals(
- 'Lower case a should fire a key event with the keycode 65',
- goog.events.KeyCodes.A, keyEvent.keyCode);
- assertEquals(
- 'Lower case a should fire a key event with the charcode 97', 97,
- keyEvent.charCode);
- fireKeyDown(keyHandler, goog.events.KeyCodes.A);
- fireKeyPress(keyHandler, undefined, 65);
- assertEquals(
- 'Upper case A should fire a key event with the keycode 65',
- goog.events.KeyCodes.A, keyEvent.keyCode);
- assertEquals(
- 'Upper case A should fire a key event with the charcode 65', 65,
- keyEvent.charCode);
- fireKeyDown(keyHandler, goog.events.KeyCodes.DELETE);
- fireKeyPress(keyHandler, goog.events.KeyCodes.DELETE);
- assertEquals(
- 'Delete should fire a key event with the keycode 46',
- goog.events.KeyCodes.DELETE, keyEvent.keyCode);
- assertEquals(
- 'Delete should fire a key event with the charcode 0', 0,
- keyEvent.charCode);
- fireKeyDown(keyHandler, goog.events.KeyCodes.PERIOD);
- fireKeyPress(keyHandler, undefined, 46);
- assertEquals(
- 'Period should fire a key event with the keycode 190',
- goog.events.KeyCodes.PERIOD, keyEvent.keyCode);
- assertEquals(
- 'Period should fire a key event with the charcode 46', 46,
- keyEvent.charCode);
- }
- /**
- * Tests the key handler for the Safari 3 behavior.
- */
- function testSafari3StyleKeyHandling() {
- goog.userAgent.OPERA = false;
- goog.userAgent.IE = false;
- goog.userAgent.GECKO = false;
- goog.userAgent.WEBKIT = true;
- goog.userAgent.MAC = true;
- goog.userAgent.WINDOWS = false;
- goog.userAgent.LINUX = false;
- goog.events.KeyHandler.USES_KEYDOWN_ = true;
- goog.userAgent.VERSION = 525.3;
- var keyEvent, keyHandler = new goog.events.KeyHandler();
- // Make sure all events are caught while testing
- goog.events.listen(
- keyHandler, goog.events.KeyHandler.EventType.KEY,
- function(e) { keyEvent = e; });
- fireKeyDown(keyHandler, goog.events.KeyCodes.ENTER);
- fireKeyPress(keyHandler, goog.events.KeyCodes.ENTER);
- assertEquals(
- 'Enter should fire a key event with the keycode 13',
- goog.events.KeyCodes.ENTER, keyEvent.keyCode);
- assertEquals(
- 'Enter should fire a key event with the charcode 0', 0,
- keyEvent.charCode);
- fireKeyUp(keyHandler, goog.events.KeyCodes.ENTER);
- // Add a listener to ensure that an extra ENTER event is not dispatched
- // by a subsequent keypress.
- var enterCheck = goog.events.listen(
- keyHandler, goog.events.KeyHandler.EventType.KEY, function(e) {
- assertNotEquals(
- 'Unexpected ENTER keypress dispatched', e.keyCode,
- goog.events.KeyCodes.ENTER);
- });
- fireKeyDown(keyHandler, goog.events.KeyCodes.ESC);
- assertEquals(
- 'Esc should fire a key event with the keycode 27',
- goog.events.KeyCodes.ESC, keyEvent.keyCode);
- assertEquals(
- 'Esc should fire a key event with the charcode 0', 0, keyEvent.charCode);
- fireKeyPress(keyHandler, goog.events.KeyCodes.ESC);
- goog.events.unlistenByKey(enterCheck);
- fireKeyDown(keyHandler, goog.events.KeyCodes.UP);
- assertEquals(
- 'Up should fire a key event with the keycode 38', goog.events.KeyCodes.UP,
- keyEvent.keyCode);
- assertEquals(
- 'Up should fire a key event with the charcode 0', 0, keyEvent.charCode);
- fireKeyDown(
- keyHandler, goog.events.KeyCodes.SEVEN, undefined, undefined, undefined,
- undefined, true);
- fireKeyPress(keyHandler, 38, 38, undefined, undefined, undefined, true);
- assertEquals(
- 'Shift+7 should fire a key event with the keycode 55',
- goog.events.KeyCodes.SEVEN, keyEvent.keyCode);
- assertEquals(
- 'Shift+7 should fire a key event with the charcode 38', 38,
- keyEvent.charCode);
- fireKeyDown(keyHandler, goog.events.KeyCodes.A);
- fireKeyPress(keyHandler, 97, 97);
- assertEquals(
- 'Lower case a should fire a key event with the keycode 65',
- goog.events.KeyCodes.A, keyEvent.keyCode);
- assertEquals(
- 'Lower case a should fire a key event with the charcode 97', 97,
- keyEvent.charCode);
- fireKeyDown(keyHandler, goog.events.KeyCodes.A);
- fireKeyPress(keyHandler, 65, 65);
- assertEquals(
- 'Upper case A should fire a key event with the keycode 65',
- goog.events.KeyCodes.A, keyEvent.keyCode);
- assertEquals(
- 'Upper case A should fire a key event with the charcode 65', 65,
- keyEvent.charCode);
- fireKeyDown(keyHandler, goog.events.KeyCodes.CTRL);
- fireKeyDown(keyHandler, goog.events.KeyCodes.A, null, null, true /*ctrl*/);
- assertEquals(
- 'A with control down should fire a key event', goog.events.KeyCodes.A,
- keyEvent.keyCode);
- // Test that Alt-Tab outside the window doesn't break things.
- fireKeyDown(keyHandler, goog.events.KeyCodes.ALT);
- keyEvent.keyCode = -1; // Reset the event.
- fireKeyDown(keyHandler, goog.events.KeyCodes.A);
- assertEquals('Should not have dispatched an Alt-A', -1, keyEvent.keyCode);
- fireKeyPress(keyHandler, 65, 65);
- assertEquals(
- 'Alt should be ignored since it isn\'t currently depressed',
- goog.events.KeyCodes.A, keyEvent.keyCode);
- fireKeyDown(keyHandler, goog.events.KeyCodes.DELETE);
- assertEquals(
- 'Delete should fire a key event with the keycode 46',
- goog.events.KeyCodes.DELETE, keyEvent.keyCode);
- assertEquals(
- 'Delete should fire a key event with the charcode 0', 0,
- keyEvent.charCode);
- fireKeyDown(keyHandler, goog.events.KeyCodes.PERIOD);
- fireKeyPress(keyHandler, 46, 46);
- assertEquals(
- 'Period should fire a key event with the keycode 190',
- goog.events.KeyCodes.PERIOD, keyEvent.keyCode);
- assertEquals(
- 'Period should fire a key event with the charcode 46', 46,
- keyEvent.charCode);
- // Safari sends zero key code for non-latin characters.
- fireKeyDown(keyHandler, 0, 0);
- fireKeyPress(keyHandler, 1092, 1092);
- assertEquals(
- 'Cyrillic small letter "Ef" should fire a key event with ' +
- 'the keycode 0',
- 0, keyEvent.keyCode);
- assertEquals(
- 'Cyrillic small letter "Ef" should fire a key event with ' +
- 'the charcode 1092',
- 1092, keyEvent.charCode);
- }
- /**
- * Tests the key handler for the Opera behavior.
- */
- function testOperaStyleKeyHandling() {
- goog.userAgent.OPERA = true;
- goog.userAgent.IE = false;
- goog.userAgent.GECKO = false;
- goog.userAgent.WEBKIT = false;
- goog.userAgent.MAC = false;
- goog.userAgent.WINDOWS = true;
- goog.userAgent.LINUX = false;
- goog.events.KeyHandler.USES_KEYDOWN_ = false;
- var keyEvent, keyHandler = new goog.events.KeyHandler();
- goog.events.listen(
- keyHandler, goog.events.KeyHandler.EventType.KEY,
- function(e) { keyEvent = e; });
- fireKeyDown(keyHandler, goog.events.KeyCodes.ENTER);
- fireKeyPress(keyHandler, goog.events.KeyCodes.ENTER);
- assertEquals(
- 'Enter should fire a key event with the keycode 13',
- goog.events.KeyCodes.ENTER, keyEvent.keyCode);
- assertEquals(
- 'Enter should fire a key event with the charcode 0', 0,
- keyEvent.charCode);
- fireKeyDown(keyHandler, goog.events.KeyCodes.ESC);
- fireKeyPress(keyHandler, goog.events.KeyCodes.ESC);
- assertEquals(
- 'Esc should fire a key event with the keycode 27',
- goog.events.KeyCodes.ESC, keyEvent.keyCode);
- assertEquals(
- 'Esc should fire a key event with the charcode 0', 0, keyEvent.charCode);
- fireKeyDown(keyHandler, goog.events.KeyCodes.UP);
- fireKeyPress(keyHandler, goog.events.KeyCodes.UP);
- assertEquals(
- 'Up should fire a key event with the keycode 38', goog.events.KeyCodes.UP,
- keyEvent.keyCode);
- assertEquals(
- 'Up should fire a key event with the charcode 0', 0, keyEvent.charCode);
- fireKeyDown(
- keyHandler, goog.events.KeyCodes.SEVEN, undefined, undefined, undefined,
- undefined, true);
- fireKeyPress(
- keyHandler, 38, undefined, undefined, undefined, undefined, true);
- assertEquals(
- 'Shift+7 should fire a key event with the keycode 55',
- goog.events.KeyCodes.SEVEN, keyEvent.keyCode);
- assertEquals(
- 'Shift+7 should fire a key event with the charcode 38', 38,
- keyEvent.charCode);
- fireKeyDown(keyHandler, goog.events.KeyCodes.A);
- fireKeyPress(keyHandler, 97);
- assertEquals(
- 'Lower case a should fire a key event with the keycode 65',
- goog.events.KeyCodes.A, keyEvent.keyCode);
- assertEquals(
- 'Lower case a should fire a key event with the charcode 97', 97,
- keyEvent.charCode);
- fireKeyDown(keyHandler, goog.events.KeyCodes.A);
- fireKeyPress(keyHandler, 65);
- assertEquals(
- 'Upper case A should fire a key event with the keycode 65',
- goog.events.KeyCodes.A, keyEvent.keyCode);
- assertEquals(
- 'Upper case A should fire a key event with the charcode 65', 65,
- keyEvent.charCode);
- fireKeyDown(keyHandler, goog.events.KeyCodes.DELETE);
- fireKeyPress(keyHandler, goog.events.KeyCodes.DELETE);
- assertEquals(
- 'Delete should fire a key event with the keycode 46',
- goog.events.KeyCodes.DELETE, keyEvent.keyCode);
- assertEquals(
- 'Delete should fire a key event with the charcode 0', 0,
- keyEvent.charCode);
- fireKeyDown(keyHandler, goog.events.KeyCodes.PERIOD);
- fireKeyPress(keyHandler, 46);
- assertEquals(
- 'Period should fire a key event with the keycode 190',
- goog.events.KeyCodes.PERIOD, keyEvent.keyCode);
- assertEquals(
- 'Period should fire a key event with the charcode 46', 46,
- keyEvent.charCode);
- }
- function testGeckoOnMacAltHandling() {
- goog.userAgent.OPERA = false;
- goog.userAgent.IE = false;
- goog.userAgent.GECKO = true;
- goog.userAgent.WEBKIT = false;
- goog.userAgent.MAC = true;
- goog.userAgent.WINDOWS = false;
- goog.userAgent.LINUX = false;
- goog.userAgent.EDGE = false;
- goog.events.KeyHandler.SAVE_ALT_FOR_KEYPRESS_ = true;
- var keyEvent, keyHandler = new goog.events.KeyHandler();
- goog.events.listen(
- keyHandler, goog.events.KeyHandler.EventType.KEY,
- function(e) { keyEvent = e; });
- fireKeyDown(
- keyHandler, goog.events.KeyCodes.COMMA, 0, null, false, true, false);
- fireKeyPress(keyHandler, 0, 8804, null, false, false, false);
- assertEquals(
- 'should fire a key event with COMMA', goog.events.KeyCodes.COMMA,
- keyEvent.keyCode);
- assertEquals(
- 'should fire a key event with alt key set', true, keyEvent.altKey);
- // Scenario: alt down, a down, a press, a up (should say alt is true),
- // alt up.
- keyEvent = undefined;
- fireKeyDown(keyHandler, 18, 0, null, false, true, false);
- fireKeyDown(keyHandler, goog.events.KeyCodes.A, 0, null, false, true, false);
- fireKeyPress(keyHandler, 0, 229, null, false, false, false);
- assertEquals(
- 'should fire a key event with alt key set', true, keyEvent.altKey);
- fireKeyUp(keyHandler, 0, 229, null, false, true, false);
- assertEquals('alt key should still be set', true, keyEvent.altKey);
- fireKeyUp(keyHandler, 18, 0, null, false, false, false);
- }
- function testGeckoEqualSign() {
- goog.userAgent.OPERA = false;
- goog.userAgent.IE = false;
- goog.userAgent.GECKO = true;
- goog.userAgent.WEBKIT = false;
- goog.userAgent.MAC = false;
- goog.userAgent.WINDOWS = true;
- goog.userAgent.LINUX = false;
- goog.events.KeyHandler.USES_KEYDOWN_ = false;
- var keyEvent, keyHandler = new goog.events.KeyHandler();
- goog.events.listen(
- keyHandler, goog.events.KeyHandler.EventType.KEY,
- function(e) { keyEvent = e; });
- fireKeyDown(keyHandler, 61, 0);
- fireKeyPress(keyHandler, 0, 61);
- assertEquals(
- '= should fire should fire a key event with the keyCode 187',
- goog.events.KeyCodes.EQUALS, keyEvent.keyCode);
- assertEquals(
- '= should fire a key event with the charCode 61',
- goog.events.KeyCodes.FF_EQUALS, keyEvent.charCode);
- }
- function testMacGeckoSlash() {
- goog.userAgent.OPERA = false;
- goog.userAgent.IE = false;
- goog.userAgent.GECKO = true;
- goog.userAgent.WEBKIT = false;
- goog.userAgent.MAC = true;
- goog.userAgent.WINDOWS = false;
- goog.userAgent.LINUX = false;
- goog.events.KeyHandler.USES_KEYDOWN_ = false;
- var keyEvent, keyHandler = new goog.events.KeyHandler();
- goog.events.listen(
- keyHandler, goog.events.KeyHandler.EventType.KEY,
- function(e) { keyEvent = e; });
- fireKeyDown(keyHandler, 0, 63, null, false, false, true);
- fireKeyPress(keyHandler, 0, 63, null, false, false, true);
- assertEquals(
- '/ should fire a key event with the keyCode 191',
- goog.events.KeyCodes.SLASH, keyEvent.keyCode);
- assertEquals(
- '? should fire a key event with the charCode 63',
- goog.events.KeyCodes.QUESTION_MARK, keyEvent.charCode);
- }
- function testGetElement() {
- var target = goog.dom.createDom(goog.dom.TagName.DIV);
- var target2 = goog.dom.createDom(goog.dom.TagName.DIV);
- var keyHandler = new goog.events.KeyHandler();
- assertNull(keyHandler.getElement());
- keyHandler.attach(target);
- assertEquals(target, keyHandler.getElement());
- keyHandler.attach(target2);
- assertNotEquals(target, keyHandler.getElement());
- assertEquals(target2, keyHandler.getElement());
- var doc = goog.dom.getDocument();
- keyHandler.attach(doc);
- assertEquals(doc, keyHandler.getElement());
- keyHandler = new goog.events.KeyHandler(doc);
- assertEquals(doc, keyHandler.getElement());
- keyHandler = new goog.events.KeyHandler(target);
- assertEquals(target, keyHandler.getElement());
- }
- function testDetach() {
- var target = goog.dom.createDom(goog.dom.TagName.DIV);
- var keyHandler = new goog.events.KeyHandler(target);
- assertEquals(target, keyHandler.getElement());
- fireKeyDown(keyHandler, 0, 63, null, false, false, true);
- fireKeyPress(keyHandler, 0, 63, null, false, false, true);
- keyHandler.detach();
- assertNull(keyHandler.getElement());
- // All listeners should be cleared.
- assertNull(keyHandler.keyDownKey_);
- assertNull(keyHandler.keyPressKey_);
- assertNull(keyHandler.keyUpKey_);
- // All key related state should be cleared.
- assertEquals('Last key should be -1', -1, keyHandler.lastKey_);
- assertEquals('keycode should be -1', -1, keyHandler.keyCode_);
- }
- function testCapturePhase() {
- var gotInCapturePhase;
- var gotInBubblePhase;
- var target = goog.dom.createDom(goog.dom.TagName.DIV);
- goog.events.listen(
- new goog.events.KeyHandler(target, false /* bubble */),
- goog.events.KeyHandler.EventType.KEY, function() {
- gotInBubblePhase = true;
- assertTrue(gotInCapturePhase);
- });
- goog.events.listen(
- new goog.events.KeyHandler(target, true /* capture */),
- goog.events.KeyHandler.EventType.KEY,
- function() { gotInCapturePhase = true; });
- goog.testing.events.fireKeySequence(target, goog.events.KeyCodes.ESC);
- assertTrue(gotInBubblePhase);
- }
- function fireKeyDown(
- keyHandler, keyCode, opt_charCode, opt_keyIdentifier, opt_ctrlKey,
- opt_altKey, opt_shiftKey) {
- var fakeEvent = createFakeKeyEvent(
- goog.events.EventType.KEYDOWN, keyCode, opt_charCode, opt_keyIdentifier,
- opt_ctrlKey, opt_altKey, opt_shiftKey);
- keyHandler.handleKeyDown_(fakeEvent);
- return fakeEvent.returnValue_;
- }
- function fireKeyPress(
- keyHandler, keyCode, opt_charCode, opt_keyIdentifier, opt_ctrlKey,
- opt_altKey, opt_shiftKey) {
- var fakeEvent = createFakeKeyEvent(
- goog.events.EventType.KEYPRESS, keyCode, opt_charCode, opt_keyIdentifier,
- opt_ctrlKey, opt_altKey, opt_shiftKey);
- keyHandler.handleEvent(fakeEvent);
- return fakeEvent.returnValue_;
- }
- function fireKeyUp(
- keyHandler, keyCode, opt_charCode, opt_keyIdentifier, opt_ctrlKey,
- opt_altKey, opt_shiftKey) {
- var fakeEvent = createFakeKeyEvent(
- goog.events.EventType.KEYUP, keyCode, opt_charCode, opt_keyIdentifier,
- opt_ctrlKey, opt_altKey, opt_shiftKey);
- keyHandler.handleKeyup_(fakeEvent);
- return fakeEvent.returnValue_;
- }
- function createFakeKeyEvent(
- type, keyCode, opt_charCode, opt_keyIdentifier, opt_ctrlKey, opt_altKey,
- opt_shiftKey) {
- var event = {
- type: type,
- keyCode: keyCode,
- charCode: opt_charCode || undefined,
- keyIdentifier: opt_keyIdentifier || undefined,
- ctrlKey: opt_ctrlKey || false,
- altKey: opt_altKey || false,
- shiftKey: opt_shiftKey || false,
- timeStamp: goog.now()
- };
- return new goog.events.BrowserEvent(event);
- }
|