keyhandler_test.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  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. goog.provide('goog.events.KeyEventTest');
  15. goog.setTestOnly('goog.events.KeyEventTest');
  16. goog.require('goog.dom');
  17. goog.require('goog.dom.TagName');
  18. goog.require('goog.events');
  19. goog.require('goog.events.BrowserEvent');
  20. goog.require('goog.events.EventType');
  21. goog.require('goog.events.KeyCodes');
  22. goog.require('goog.events.KeyHandler');
  23. goog.require('goog.testing.events');
  24. goog.require('goog.testing.jsunit');
  25. goog.require('goog.userAgent');
  26. function setUp() {
  27. // Have this based on a fictitious DOCUMENT_MODE constant.
  28. goog.userAgent.isDocumentMode = function(mode) {
  29. return mode <= goog.userAgent.DOCUMENT_MODE;
  30. };
  31. }
  32. /**
  33. * Tests the key handler for the IE 8 and lower behavior.
  34. */
  35. function testIe8StyleKeyHandling() {
  36. goog.userAgent.OPERA = false;
  37. goog.userAgent.IE = true;
  38. goog.userAgent.GECKO = false;
  39. goog.userAgent.WEBKIT = false;
  40. goog.userAgent.MAC = false;
  41. goog.userAgent.WINDOWS = true;
  42. goog.userAgent.LINUX = false;
  43. goog.userAgent.VERSION = 8;
  44. goog.userAgent.DOCUMENT_MODE = 8;
  45. goog.events.KeyHandler.USES_KEYDOWN_ = true;
  46. assertIe8StyleKeyHandling();
  47. }
  48. /**
  49. * Tests the key handler for the IE 8 and lower behavior.
  50. */
  51. function testIe8StyleKeyHandlingInIe9DocumentMode() {
  52. goog.userAgent.OPERA = false;
  53. goog.userAgent.IE = true;
  54. goog.userAgent.GECKO = false;
  55. goog.userAgent.WEBKIT = false;
  56. goog.userAgent.MAC = false;
  57. goog.userAgent.WINDOWS = true;
  58. goog.userAgent.LINUX = false;
  59. goog.userAgent.VERSION = 9; // Try IE9 in IE8 document mode.
  60. goog.userAgent.DOCUMENT_MODE = 8;
  61. goog.events.KeyHandler.USES_KEYDOWN_ = true;
  62. assertIe8StyleKeyHandling();
  63. }
  64. function assertIe8StyleKeyHandling() {
  65. var keyEvent, keyHandler = new goog.events.KeyHandler();
  66. goog.events.listen(
  67. keyHandler, goog.events.KeyHandler.EventType.KEY,
  68. function(e) { keyEvent = e; });
  69. fireKeyDown(keyHandler, goog.events.KeyCodes.ENTER);
  70. fireKeyPress(keyHandler, goog.events.KeyCodes.ENTER);
  71. assertEquals(
  72. 'Enter should fire a key event with the keycode 13',
  73. goog.events.KeyCodes.ENTER, keyEvent.keyCode);
  74. assertEquals(
  75. 'Enter should fire a key event with the charcode 0', 0,
  76. keyEvent.charCode);
  77. fireKeyDown(keyHandler, goog.events.KeyCodes.ESC);
  78. fireKeyPress(keyHandler, goog.events.KeyCodes.ESC);
  79. assertEquals(
  80. 'Esc should fire a key event with the keycode 27',
  81. goog.events.KeyCodes.ESC, keyEvent.keyCode);
  82. assertEquals(
  83. 'Esc should fire a key event with the charcode 0', 0, keyEvent.charCode);
  84. fireKeyDown(keyHandler, goog.events.KeyCodes.UP);
  85. assertEquals(
  86. 'Up should fire a key event with the keycode 38', goog.events.KeyCodes.UP,
  87. keyEvent.keyCode);
  88. assertEquals(
  89. 'Up should fire a key event with the charcode 0', 0, keyEvent.charCode);
  90. fireKeyDown(
  91. keyHandler, goog.events.KeyCodes.SEVEN, undefined, undefined, undefined,
  92. undefined, true);
  93. fireKeyPress(
  94. keyHandler, 38, undefined, undefined, undefined, undefined, true);
  95. assertEquals(
  96. 'Shift+7 should fire a key event with the keycode 55',
  97. goog.events.KeyCodes.SEVEN, keyEvent.keyCode);
  98. assertEquals(
  99. 'Shift+7 should fire a key event with the charcode 38', 38,
  100. keyEvent.charCode);
  101. fireKeyDown(keyHandler, goog.events.KeyCodes.A);
  102. fireKeyPress(keyHandler, 97);
  103. assertEquals(
  104. 'Lower case a should fire a key event with the keycode 65',
  105. goog.events.KeyCodes.A, keyEvent.keyCode);
  106. assertEquals(
  107. 'Lower case a should fire a key event with the charcode 97', 97,
  108. keyEvent.charCode);
  109. fireKeyDown(keyHandler, goog.events.KeyCodes.A);
  110. fireKeyPress(keyHandler, 65);
  111. assertEquals(
  112. 'Upper case A should fire a key event with the keycode 65',
  113. goog.events.KeyCodes.A, keyEvent.keyCode);
  114. assertEquals(
  115. 'Upper case A should fire a key event with the charcode 65', 65,
  116. keyEvent.charCode);
  117. fireKeyDown(keyHandler, goog.events.KeyCodes.DELETE);
  118. assertEquals(
  119. 'Delete should fire a key event with the keycode 46',
  120. goog.events.KeyCodes.DELETE, keyEvent.keyCode);
  121. assertEquals(
  122. 'Delete should fire a key event with the charcode 0', 0,
  123. keyEvent.charCode);
  124. fireKeyDown(keyHandler, goog.events.KeyCodes.PERIOD);
  125. fireKeyPress(keyHandler, 46);
  126. assertEquals(
  127. 'Period should fire a key event with the keycode 190',
  128. goog.events.KeyCodes.PERIOD, keyEvent.keyCode);
  129. assertEquals(
  130. 'Period should fire a key event with the charcode 46', 46,
  131. keyEvent.charCode);
  132. fireKeyDown(keyHandler, goog.events.KeyCodes.CTRL);
  133. fireKeyDown(keyHandler, goog.events.KeyCodes.A);
  134. assertEquals(
  135. 'A with control down should fire a key event', goog.events.KeyCodes.A,
  136. keyEvent.keyCode);
  137. // On IE, when Ctrl+<key> is held down, there is a KEYDOWN, a KEYPRESS, and
  138. // then a series of KEYDOWN events for each repeat.
  139. fireKeyDown(keyHandler, goog.events.KeyCodes.B, undefined, undefined, true);
  140. fireKeyPress(keyHandler, goog.events.KeyCodes.B, undefined, undefined, true);
  141. assertEquals(
  142. 'B with control down should fire a key event', goog.events.KeyCodes.B,
  143. keyEvent.keyCode);
  144. assertTrue('Ctrl should be down.', keyEvent.ctrlKey);
  145. assertFalse(
  146. 'Should not have repeat=true on the first key press.', keyEvent.repeat);
  147. // Fire one repeated keydown event.
  148. fireKeyDown(keyHandler, goog.events.KeyCodes.B, undefined, undefined, true);
  149. assertEquals(
  150. 'A with control down should fire a key event', goog.events.KeyCodes.B,
  151. keyEvent.keyCode);
  152. assertTrue('Should have repeat=true on key repeat.', keyEvent.repeat);
  153. assertTrue('Ctrl should be down.', keyEvent.ctrlKey);
  154. }
  155. /**
  156. * Tests special cases for IE9.
  157. */
  158. function testIe9StyleKeyHandling() {
  159. goog.userAgent.OPERA = false;
  160. goog.userAgent.IE = true;
  161. goog.userAgent.GECKO = false;
  162. goog.userAgent.WEBKIT = false;
  163. goog.userAgent.MAC = false;
  164. goog.userAgent.WINDOWS = true;
  165. goog.userAgent.LINUX = false;
  166. goog.userAgent.VERSION = 9;
  167. goog.userAgent.DOCUMENT_MODE = 9;
  168. goog.events.KeyHandler.USES_KEYDOWN_ = true;
  169. var keyEvent, keyHandler = new goog.events.KeyHandler();
  170. goog.events.listen(
  171. keyHandler, goog.events.KeyHandler.EventType.KEY,
  172. function(e) { keyEvent = e; });
  173. fireKeyDown(keyHandler, goog.events.KeyCodes.ENTER);
  174. fireKeyPress(keyHandler, goog.events.KeyCodes.ENTER);
  175. assertEquals(
  176. 'Enter should fire a key event with the keycode 13',
  177. goog.events.KeyCodes.ENTER, keyEvent.keyCode);
  178. assertEquals(
  179. 'Enter should fire a key event with the charcode 0', 0,
  180. keyEvent.charCode);
  181. }
  182. /**
  183. * Tests the key handler for the Gecko behavior.
  184. */
  185. function testGeckoStyleKeyHandling() {
  186. goog.userAgent.OPERA = false;
  187. goog.userAgent.IE = false;
  188. goog.userAgent.GECKO = true;
  189. goog.userAgent.WEBKIT = false;
  190. goog.userAgent.MAC = false;
  191. goog.userAgent.WINDOWS = true;
  192. goog.userAgent.LINUX = false;
  193. goog.events.KeyHandler.USES_KEYDOWN_ = false;
  194. var keyEvent, keyHandler = new goog.events.KeyHandler();
  195. goog.events.listen(
  196. keyHandler, goog.events.KeyHandler.EventType.KEY,
  197. function(e) { keyEvent = e; });
  198. fireKeyDown(keyHandler, goog.events.KeyCodes.ENTER);
  199. fireKeyPress(keyHandler, goog.events.KeyCodes.ENTER);
  200. assertEquals(
  201. 'Enter should fire a key event with the keycode 13',
  202. goog.events.KeyCodes.ENTER, keyEvent.keyCode);
  203. assertEquals(
  204. 'Enter should fire a key event with the charcode 0', 0,
  205. keyEvent.charCode);
  206. fireKeyDown(keyHandler, goog.events.KeyCodes.ESC);
  207. fireKeyPress(keyHandler, goog.events.KeyCodes.ESC);
  208. assertEquals(
  209. 'Esc should fire a key event with the keycode 27',
  210. goog.events.KeyCodes.ESC, keyEvent.keyCode);
  211. assertEquals(
  212. 'Esc should fire a key event with the charcode 0', 0, keyEvent.charCode);
  213. fireKeyDown(keyHandler, goog.events.KeyCodes.UP);
  214. fireKeyPress(keyHandler, goog.events.KeyCodes.UP);
  215. assertEquals(
  216. 'Up should fire a key event with the keycode 38', goog.events.KeyCodes.UP,
  217. keyEvent.keyCode);
  218. assertEquals(
  219. 'Up should fire a key event with the charcode 0', 0, keyEvent.charCode);
  220. fireKeyDown(
  221. keyHandler, goog.events.KeyCodes.SEVEN, undefined, undefined, undefined,
  222. undefined, true);
  223. fireKeyPress(
  224. keyHandler, undefined, 38, undefined, undefined, undefined, true);
  225. assertEquals(
  226. 'Shift+7 should fire a key event with the keycode 55',
  227. goog.events.KeyCodes.SEVEN, keyEvent.keyCode);
  228. assertEquals(
  229. 'Shift+7 should fire a key event with the charcode 38', 38,
  230. keyEvent.charCode);
  231. fireKeyDown(keyHandler, goog.events.KeyCodes.A);
  232. fireKeyPress(keyHandler, undefined, 97);
  233. assertEquals(
  234. 'Lower case a should fire a key event with the keycode 65',
  235. goog.events.KeyCodes.A, keyEvent.keyCode);
  236. assertEquals(
  237. 'Lower case a should fire a key event with the charcode 97', 97,
  238. keyEvent.charCode);
  239. fireKeyDown(keyHandler, goog.events.KeyCodes.A);
  240. fireKeyPress(keyHandler, undefined, 65);
  241. assertEquals(
  242. 'Upper case A should fire a key event with the keycode 65',
  243. goog.events.KeyCodes.A, keyEvent.keyCode);
  244. assertEquals(
  245. 'Upper case A should fire a key event with the charcode 65', 65,
  246. keyEvent.charCode);
  247. fireKeyDown(keyHandler, goog.events.KeyCodes.DELETE);
  248. fireKeyPress(keyHandler, goog.events.KeyCodes.DELETE);
  249. assertEquals(
  250. 'Delete should fire a key event with the keycode 46',
  251. goog.events.KeyCodes.DELETE, keyEvent.keyCode);
  252. assertEquals(
  253. 'Delete should fire a key event with the charcode 0', 0,
  254. keyEvent.charCode);
  255. fireKeyDown(keyHandler, goog.events.KeyCodes.PERIOD);
  256. fireKeyPress(keyHandler, undefined, 46);
  257. assertEquals(
  258. 'Period should fire a key event with the keycode 190',
  259. goog.events.KeyCodes.PERIOD, keyEvent.keyCode);
  260. assertEquals(
  261. 'Period should fire a key event with the charcode 46', 46,
  262. keyEvent.charCode);
  263. }
  264. /**
  265. * Tests the key handler for the Safari 3 behavior.
  266. */
  267. function testSafari3StyleKeyHandling() {
  268. goog.userAgent.OPERA = false;
  269. goog.userAgent.IE = false;
  270. goog.userAgent.GECKO = false;
  271. goog.userAgent.WEBKIT = true;
  272. goog.userAgent.MAC = true;
  273. goog.userAgent.WINDOWS = false;
  274. goog.userAgent.LINUX = false;
  275. goog.events.KeyHandler.USES_KEYDOWN_ = true;
  276. goog.userAgent.VERSION = 525.3;
  277. var keyEvent, keyHandler = new goog.events.KeyHandler();
  278. // Make sure all events are caught while testing
  279. goog.events.listen(
  280. keyHandler, goog.events.KeyHandler.EventType.KEY,
  281. function(e) { keyEvent = e; });
  282. fireKeyDown(keyHandler, goog.events.KeyCodes.ENTER);
  283. fireKeyPress(keyHandler, goog.events.KeyCodes.ENTER);
  284. assertEquals(
  285. 'Enter should fire a key event with the keycode 13',
  286. goog.events.KeyCodes.ENTER, keyEvent.keyCode);
  287. assertEquals(
  288. 'Enter should fire a key event with the charcode 0', 0,
  289. keyEvent.charCode);
  290. fireKeyUp(keyHandler, goog.events.KeyCodes.ENTER);
  291. // Add a listener to ensure that an extra ENTER event is not dispatched
  292. // by a subsequent keypress.
  293. var enterCheck = goog.events.listen(
  294. keyHandler, goog.events.KeyHandler.EventType.KEY, function(e) {
  295. assertNotEquals(
  296. 'Unexpected ENTER keypress dispatched', e.keyCode,
  297. goog.events.KeyCodes.ENTER);
  298. });
  299. fireKeyDown(keyHandler, goog.events.KeyCodes.ESC);
  300. assertEquals(
  301. 'Esc should fire a key event with the keycode 27',
  302. goog.events.KeyCodes.ESC, keyEvent.keyCode);
  303. assertEquals(
  304. 'Esc should fire a key event with the charcode 0', 0, keyEvent.charCode);
  305. fireKeyPress(keyHandler, goog.events.KeyCodes.ESC);
  306. goog.events.unlistenByKey(enterCheck);
  307. fireKeyDown(keyHandler, goog.events.KeyCodes.UP);
  308. assertEquals(
  309. 'Up should fire a key event with the keycode 38', goog.events.KeyCodes.UP,
  310. keyEvent.keyCode);
  311. assertEquals(
  312. 'Up should fire a key event with the charcode 0', 0, keyEvent.charCode);
  313. fireKeyDown(
  314. keyHandler, goog.events.KeyCodes.SEVEN, undefined, undefined, undefined,
  315. undefined, true);
  316. fireKeyPress(keyHandler, 38, 38, undefined, undefined, undefined, true);
  317. assertEquals(
  318. 'Shift+7 should fire a key event with the keycode 55',
  319. goog.events.KeyCodes.SEVEN, keyEvent.keyCode);
  320. assertEquals(
  321. 'Shift+7 should fire a key event with the charcode 38', 38,
  322. keyEvent.charCode);
  323. fireKeyDown(keyHandler, goog.events.KeyCodes.A);
  324. fireKeyPress(keyHandler, 97, 97);
  325. assertEquals(
  326. 'Lower case a should fire a key event with the keycode 65',
  327. goog.events.KeyCodes.A, keyEvent.keyCode);
  328. assertEquals(
  329. 'Lower case a should fire a key event with the charcode 97', 97,
  330. keyEvent.charCode);
  331. fireKeyDown(keyHandler, goog.events.KeyCodes.A);
  332. fireKeyPress(keyHandler, 65, 65);
  333. assertEquals(
  334. 'Upper case A should fire a key event with the keycode 65',
  335. goog.events.KeyCodes.A, keyEvent.keyCode);
  336. assertEquals(
  337. 'Upper case A should fire a key event with the charcode 65', 65,
  338. keyEvent.charCode);
  339. fireKeyDown(keyHandler, goog.events.KeyCodes.CTRL);
  340. fireKeyDown(keyHandler, goog.events.KeyCodes.A, null, null, true /*ctrl*/);
  341. assertEquals(
  342. 'A with control down should fire a key event', goog.events.KeyCodes.A,
  343. keyEvent.keyCode);
  344. // Test that Alt-Tab outside the window doesn't break things.
  345. fireKeyDown(keyHandler, goog.events.KeyCodes.ALT);
  346. keyEvent.keyCode = -1; // Reset the event.
  347. fireKeyDown(keyHandler, goog.events.KeyCodes.A);
  348. assertEquals('Should not have dispatched an Alt-A', -1, keyEvent.keyCode);
  349. fireKeyPress(keyHandler, 65, 65);
  350. assertEquals(
  351. 'Alt should be ignored since it isn\'t currently depressed',
  352. goog.events.KeyCodes.A, keyEvent.keyCode);
  353. fireKeyDown(keyHandler, goog.events.KeyCodes.DELETE);
  354. assertEquals(
  355. 'Delete should fire a key event with the keycode 46',
  356. goog.events.KeyCodes.DELETE, keyEvent.keyCode);
  357. assertEquals(
  358. 'Delete should fire a key event with the charcode 0', 0,
  359. keyEvent.charCode);
  360. fireKeyDown(keyHandler, goog.events.KeyCodes.PERIOD);
  361. fireKeyPress(keyHandler, 46, 46);
  362. assertEquals(
  363. 'Period should fire a key event with the keycode 190',
  364. goog.events.KeyCodes.PERIOD, keyEvent.keyCode);
  365. assertEquals(
  366. 'Period should fire a key event with the charcode 46', 46,
  367. keyEvent.charCode);
  368. // Safari sends zero key code for non-latin characters.
  369. fireKeyDown(keyHandler, 0, 0);
  370. fireKeyPress(keyHandler, 1092, 1092);
  371. assertEquals(
  372. 'Cyrillic small letter "Ef" should fire a key event with ' +
  373. 'the keycode 0',
  374. 0, keyEvent.keyCode);
  375. assertEquals(
  376. 'Cyrillic small letter "Ef" should fire a key event with ' +
  377. 'the charcode 1092',
  378. 1092, keyEvent.charCode);
  379. }
  380. /**
  381. * Tests the key handler for the Opera behavior.
  382. */
  383. function testOperaStyleKeyHandling() {
  384. goog.userAgent.OPERA = true;
  385. goog.userAgent.IE = false;
  386. goog.userAgent.GECKO = false;
  387. goog.userAgent.WEBKIT = false;
  388. goog.userAgent.MAC = false;
  389. goog.userAgent.WINDOWS = true;
  390. goog.userAgent.LINUX = false;
  391. goog.events.KeyHandler.USES_KEYDOWN_ = false;
  392. var keyEvent, keyHandler = new goog.events.KeyHandler();
  393. goog.events.listen(
  394. keyHandler, goog.events.KeyHandler.EventType.KEY,
  395. function(e) { keyEvent = e; });
  396. fireKeyDown(keyHandler, goog.events.KeyCodes.ENTER);
  397. fireKeyPress(keyHandler, goog.events.KeyCodes.ENTER);
  398. assertEquals(
  399. 'Enter should fire a key event with the keycode 13',
  400. goog.events.KeyCodes.ENTER, keyEvent.keyCode);
  401. assertEquals(
  402. 'Enter should fire a key event with the charcode 0', 0,
  403. keyEvent.charCode);
  404. fireKeyDown(keyHandler, goog.events.KeyCodes.ESC);
  405. fireKeyPress(keyHandler, goog.events.KeyCodes.ESC);
  406. assertEquals(
  407. 'Esc should fire a key event with the keycode 27',
  408. goog.events.KeyCodes.ESC, keyEvent.keyCode);
  409. assertEquals(
  410. 'Esc should fire a key event with the charcode 0', 0, keyEvent.charCode);
  411. fireKeyDown(keyHandler, goog.events.KeyCodes.UP);
  412. fireKeyPress(keyHandler, goog.events.KeyCodes.UP);
  413. assertEquals(
  414. 'Up should fire a key event with the keycode 38', goog.events.KeyCodes.UP,
  415. keyEvent.keyCode);
  416. assertEquals(
  417. 'Up should fire a key event with the charcode 0', 0, keyEvent.charCode);
  418. fireKeyDown(
  419. keyHandler, goog.events.KeyCodes.SEVEN, undefined, undefined, undefined,
  420. undefined, true);
  421. fireKeyPress(
  422. keyHandler, 38, undefined, undefined, undefined, undefined, true);
  423. assertEquals(
  424. 'Shift+7 should fire a key event with the keycode 55',
  425. goog.events.KeyCodes.SEVEN, keyEvent.keyCode);
  426. assertEquals(
  427. 'Shift+7 should fire a key event with the charcode 38', 38,
  428. keyEvent.charCode);
  429. fireKeyDown(keyHandler, goog.events.KeyCodes.A);
  430. fireKeyPress(keyHandler, 97);
  431. assertEquals(
  432. 'Lower case a should fire a key event with the keycode 65',
  433. goog.events.KeyCodes.A, keyEvent.keyCode);
  434. assertEquals(
  435. 'Lower case a should fire a key event with the charcode 97', 97,
  436. keyEvent.charCode);
  437. fireKeyDown(keyHandler, goog.events.KeyCodes.A);
  438. fireKeyPress(keyHandler, 65);
  439. assertEquals(
  440. 'Upper case A should fire a key event with the keycode 65',
  441. goog.events.KeyCodes.A, keyEvent.keyCode);
  442. assertEquals(
  443. 'Upper case A should fire a key event with the charcode 65', 65,
  444. keyEvent.charCode);
  445. fireKeyDown(keyHandler, goog.events.KeyCodes.DELETE);
  446. fireKeyPress(keyHandler, goog.events.KeyCodes.DELETE);
  447. assertEquals(
  448. 'Delete should fire a key event with the keycode 46',
  449. goog.events.KeyCodes.DELETE, keyEvent.keyCode);
  450. assertEquals(
  451. 'Delete should fire a key event with the charcode 0', 0,
  452. keyEvent.charCode);
  453. fireKeyDown(keyHandler, goog.events.KeyCodes.PERIOD);
  454. fireKeyPress(keyHandler, 46);
  455. assertEquals(
  456. 'Period should fire a key event with the keycode 190',
  457. goog.events.KeyCodes.PERIOD, keyEvent.keyCode);
  458. assertEquals(
  459. 'Period should fire a key event with the charcode 46', 46,
  460. keyEvent.charCode);
  461. }
  462. function testGeckoOnMacAltHandling() {
  463. goog.userAgent.OPERA = false;
  464. goog.userAgent.IE = false;
  465. goog.userAgent.GECKO = true;
  466. goog.userAgent.WEBKIT = false;
  467. goog.userAgent.MAC = true;
  468. goog.userAgent.WINDOWS = false;
  469. goog.userAgent.LINUX = false;
  470. goog.userAgent.EDGE = false;
  471. goog.events.KeyHandler.SAVE_ALT_FOR_KEYPRESS_ = true;
  472. var keyEvent, keyHandler = new goog.events.KeyHandler();
  473. goog.events.listen(
  474. keyHandler, goog.events.KeyHandler.EventType.KEY,
  475. function(e) { keyEvent = e; });
  476. fireKeyDown(
  477. keyHandler, goog.events.KeyCodes.COMMA, 0, null, false, true, false);
  478. fireKeyPress(keyHandler, 0, 8804, null, false, false, false);
  479. assertEquals(
  480. 'should fire a key event with COMMA', goog.events.KeyCodes.COMMA,
  481. keyEvent.keyCode);
  482. assertEquals(
  483. 'should fire a key event with alt key set', true, keyEvent.altKey);
  484. // Scenario: alt down, a down, a press, a up (should say alt is true),
  485. // alt up.
  486. keyEvent = undefined;
  487. fireKeyDown(keyHandler, 18, 0, null, false, true, false);
  488. fireKeyDown(keyHandler, goog.events.KeyCodes.A, 0, null, false, true, false);
  489. fireKeyPress(keyHandler, 0, 229, null, false, false, false);
  490. assertEquals(
  491. 'should fire a key event with alt key set', true, keyEvent.altKey);
  492. fireKeyUp(keyHandler, 0, 229, null, false, true, false);
  493. assertEquals('alt key should still be set', true, keyEvent.altKey);
  494. fireKeyUp(keyHandler, 18, 0, null, false, false, false);
  495. }
  496. function testGeckoEqualSign() {
  497. goog.userAgent.OPERA = false;
  498. goog.userAgent.IE = false;
  499. goog.userAgent.GECKO = true;
  500. goog.userAgent.WEBKIT = false;
  501. goog.userAgent.MAC = false;
  502. goog.userAgent.WINDOWS = true;
  503. goog.userAgent.LINUX = false;
  504. goog.events.KeyHandler.USES_KEYDOWN_ = false;
  505. var keyEvent, keyHandler = new goog.events.KeyHandler();
  506. goog.events.listen(
  507. keyHandler, goog.events.KeyHandler.EventType.KEY,
  508. function(e) { keyEvent = e; });
  509. fireKeyDown(keyHandler, 61, 0);
  510. fireKeyPress(keyHandler, 0, 61);
  511. assertEquals(
  512. '= should fire should fire a key event with the keyCode 187',
  513. goog.events.KeyCodes.EQUALS, keyEvent.keyCode);
  514. assertEquals(
  515. '= should fire a key event with the charCode 61',
  516. goog.events.KeyCodes.FF_EQUALS, keyEvent.charCode);
  517. }
  518. function testMacGeckoSlash() {
  519. goog.userAgent.OPERA = false;
  520. goog.userAgent.IE = false;
  521. goog.userAgent.GECKO = true;
  522. goog.userAgent.WEBKIT = false;
  523. goog.userAgent.MAC = true;
  524. goog.userAgent.WINDOWS = false;
  525. goog.userAgent.LINUX = false;
  526. goog.events.KeyHandler.USES_KEYDOWN_ = false;
  527. var keyEvent, keyHandler = new goog.events.KeyHandler();
  528. goog.events.listen(
  529. keyHandler, goog.events.KeyHandler.EventType.KEY,
  530. function(e) { keyEvent = e; });
  531. fireKeyDown(keyHandler, 0, 63, null, false, false, true);
  532. fireKeyPress(keyHandler, 0, 63, null, false, false, true);
  533. assertEquals(
  534. '/ should fire a key event with the keyCode 191',
  535. goog.events.KeyCodes.SLASH, keyEvent.keyCode);
  536. assertEquals(
  537. '? should fire a key event with the charCode 63',
  538. goog.events.KeyCodes.QUESTION_MARK, keyEvent.charCode);
  539. }
  540. function testGetElement() {
  541. var target = goog.dom.createDom(goog.dom.TagName.DIV);
  542. var target2 = goog.dom.createDom(goog.dom.TagName.DIV);
  543. var keyHandler = new goog.events.KeyHandler();
  544. assertNull(keyHandler.getElement());
  545. keyHandler.attach(target);
  546. assertEquals(target, keyHandler.getElement());
  547. keyHandler.attach(target2);
  548. assertNotEquals(target, keyHandler.getElement());
  549. assertEquals(target2, keyHandler.getElement());
  550. var doc = goog.dom.getDocument();
  551. keyHandler.attach(doc);
  552. assertEquals(doc, keyHandler.getElement());
  553. keyHandler = new goog.events.KeyHandler(doc);
  554. assertEquals(doc, keyHandler.getElement());
  555. keyHandler = new goog.events.KeyHandler(target);
  556. assertEquals(target, keyHandler.getElement());
  557. }
  558. function testDetach() {
  559. var target = goog.dom.createDom(goog.dom.TagName.DIV);
  560. var keyHandler = new goog.events.KeyHandler(target);
  561. assertEquals(target, keyHandler.getElement());
  562. fireKeyDown(keyHandler, 0, 63, null, false, false, true);
  563. fireKeyPress(keyHandler, 0, 63, null, false, false, true);
  564. keyHandler.detach();
  565. assertNull(keyHandler.getElement());
  566. // All listeners should be cleared.
  567. assertNull(keyHandler.keyDownKey_);
  568. assertNull(keyHandler.keyPressKey_);
  569. assertNull(keyHandler.keyUpKey_);
  570. // All key related state should be cleared.
  571. assertEquals('Last key should be -1', -1, keyHandler.lastKey_);
  572. assertEquals('keycode should be -1', -1, keyHandler.keyCode_);
  573. }
  574. function testCapturePhase() {
  575. var gotInCapturePhase;
  576. var gotInBubblePhase;
  577. var target = goog.dom.createDom(goog.dom.TagName.DIV);
  578. goog.events.listen(
  579. new goog.events.KeyHandler(target, false /* bubble */),
  580. goog.events.KeyHandler.EventType.KEY, function() {
  581. gotInBubblePhase = true;
  582. assertTrue(gotInCapturePhase);
  583. });
  584. goog.events.listen(
  585. new goog.events.KeyHandler(target, true /* capture */),
  586. goog.events.KeyHandler.EventType.KEY,
  587. function() { gotInCapturePhase = true; });
  588. goog.testing.events.fireKeySequence(target, goog.events.KeyCodes.ESC);
  589. assertTrue(gotInBubblePhase);
  590. }
  591. function fireKeyDown(
  592. keyHandler, keyCode, opt_charCode, opt_keyIdentifier, opt_ctrlKey,
  593. opt_altKey, opt_shiftKey) {
  594. var fakeEvent = createFakeKeyEvent(
  595. goog.events.EventType.KEYDOWN, keyCode, opt_charCode, opt_keyIdentifier,
  596. opt_ctrlKey, opt_altKey, opt_shiftKey);
  597. keyHandler.handleKeyDown_(fakeEvent);
  598. return fakeEvent.returnValue_;
  599. }
  600. function fireKeyPress(
  601. keyHandler, keyCode, opt_charCode, opt_keyIdentifier, opt_ctrlKey,
  602. opt_altKey, opt_shiftKey) {
  603. var fakeEvent = createFakeKeyEvent(
  604. goog.events.EventType.KEYPRESS, keyCode, opt_charCode, opt_keyIdentifier,
  605. opt_ctrlKey, opt_altKey, opt_shiftKey);
  606. keyHandler.handleEvent(fakeEvent);
  607. return fakeEvent.returnValue_;
  608. }
  609. function fireKeyUp(
  610. keyHandler, keyCode, opt_charCode, opt_keyIdentifier, opt_ctrlKey,
  611. opt_altKey, opt_shiftKey) {
  612. var fakeEvent = createFakeKeyEvent(
  613. goog.events.EventType.KEYUP, keyCode, opt_charCode, opt_keyIdentifier,
  614. opt_ctrlKey, opt_altKey, opt_shiftKey);
  615. keyHandler.handleKeyup_(fakeEvent);
  616. return fakeEvent.returnValue_;
  617. }
  618. function createFakeKeyEvent(
  619. type, keyCode, opt_charCode, opt_keyIdentifier, opt_ctrlKey, opt_altKey,
  620. opt_shiftKey) {
  621. var event = {
  622. type: type,
  623. keyCode: keyCode,
  624. charCode: opt_charCode || undefined,
  625. keyIdentifier: opt_keyIdentifier || undefined,
  626. ctrlKey: opt_ctrlKey || false,
  627. altKey: opt_altKey || false,
  628. shiftKey: opt_shiftKey || false,
  629. timeStamp: goog.now()
  630. };
  631. return new goog.events.BrowserEvent(event);
  632. }