keycodes_test.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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. goog.provide('goog.events.KeyCodesTest');
  15. goog.setTestOnly('goog.events.KeyCodesTest');
  16. goog.require('goog.events.BrowserEvent');
  17. goog.require('goog.events.KeyCodes');
  18. goog.require('goog.object');
  19. goog.require('goog.testing.PropertyReplacer');
  20. goog.require('goog.testing.jsunit');
  21. goog.require('goog.userAgent');
  22. var KeyCodes;
  23. var stubs;
  24. function setUpPage() {
  25. KeyCodes = goog.events.KeyCodes;
  26. stubs = new goog.testing.PropertyReplacer();
  27. }
  28. function tearDown() {
  29. stubs.reset();
  30. }
  31. function testTextModifyingKeys() {
  32. var specialTextModifiers = goog.object.createSet(
  33. KeyCodes.BACKSPACE, KeyCodes.DELETE, KeyCodes.ENTER, KeyCodes.MAC_ENTER,
  34. KeyCodes.TAB, KeyCodes.WIN_IME);
  35. if (!goog.userAgent.GECKO) {
  36. specialTextModifiers[KeyCodes.WIN_KEY_FF_LINUX] = 1;
  37. }
  38. for (var keyId in KeyCodes) {
  39. var key = KeyCodes[keyId];
  40. if (goog.isFunction(key)) {
  41. // skip static methods
  42. continue;
  43. }
  44. var fakeEvent = createEventWithKeyCode(key);
  45. if (KeyCodes.isCharacterKey(key) || (key in specialTextModifiers)) {
  46. assertTrue(
  47. 'Expected key to modify text: ' + keyId,
  48. KeyCodes.isTextModifyingKeyEvent(fakeEvent));
  49. } else {
  50. assertFalse(
  51. 'Expected key to not modify text: ' + keyId,
  52. KeyCodes.isTextModifyingKeyEvent(fakeEvent));
  53. }
  54. }
  55. for (var i = KeyCodes.FIRST_MEDIA_KEY; i <= KeyCodes.LAST_MEDIA_KEY; i++) {
  56. var fakeEvent = createEventWithKeyCode(i);
  57. assertFalse(
  58. 'Expected key to not modify text: ' + i,
  59. KeyCodes.isTextModifyingKeyEvent(fakeEvent));
  60. }
  61. }
  62. function testKeyCodeZero() {
  63. var zeroEvent = createEventWithKeyCode(0);
  64. assertEquals(
  65. !goog.userAgent.GECKO, KeyCodes.isTextModifyingKeyEvent(zeroEvent));
  66. assertEquals(
  67. goog.userAgent.WEBKIT || goog.userAgent.EDGE, KeyCodes.isCharacterKey(0));
  68. }
  69. function testPhantomKey() {
  70. // KeyCode 255 deserves its own test to make sure this does not regress,
  71. // because it's so weird. See the comments in the KeyCode enum.
  72. var fakeEvent = createEventWithKeyCode(goog.events.KeyCodes.PHANTOM);
  73. assertFalse(
  74. 'Expected phantom key to not modify text',
  75. KeyCodes.isTextModifyingKeyEvent(fakeEvent));
  76. assertFalse(KeyCodes.isCharacterKey(fakeEvent));
  77. }
  78. function testNonUsKeyboards() {
  79. var fakeEvent = createEventWithKeyCode(1092 /* Russian a */);
  80. assertTrue(
  81. 'Expected key to not modify text: 1092',
  82. KeyCodes.isTextModifyingKeyEvent(fakeEvent));
  83. }
  84. function createEventWithKeyCode(i) {
  85. var fakeEvent = new goog.events.BrowserEvent('keydown');
  86. fakeEvent.keyCode = i;
  87. return fakeEvent;
  88. }
  89. function testNormalizeGeckoKeyCode() {
  90. stubs.set(goog.userAgent, 'GECKO', true);
  91. // Test Gecko-specific key codes.
  92. assertEquals(
  93. goog.events.KeyCodes.normalizeGeckoKeyCode(KeyCodes.FF_EQUALS),
  94. KeyCodes.EQUALS);
  95. assertEquals(
  96. goog.events.KeyCodes.normalizeKeyCode(KeyCodes.FF_EQUALS),
  97. KeyCodes.EQUALS);
  98. assertEquals(
  99. goog.events.KeyCodes.normalizeGeckoKeyCode(KeyCodes.FF_SEMICOLON),
  100. KeyCodes.SEMICOLON);
  101. assertEquals(
  102. goog.events.KeyCodes.normalizeKeyCode(KeyCodes.FF_SEMICOLON),
  103. KeyCodes.SEMICOLON);
  104. assertEquals(
  105. goog.events.KeyCodes.normalizeGeckoKeyCode(KeyCodes.MAC_FF_META),
  106. KeyCodes.META);
  107. assertEquals(
  108. goog.events.KeyCodes.normalizeKeyCode(KeyCodes.MAC_FF_META),
  109. KeyCodes.META);
  110. assertEquals(
  111. goog.events.KeyCodes.normalizeGeckoKeyCode(KeyCodes.WIN_KEY_FF_LINUX),
  112. KeyCodes.WIN_KEY);
  113. assertEquals(
  114. goog.events.KeyCodes.normalizeKeyCode(KeyCodes.WIN_KEY_FF_LINUX),
  115. KeyCodes.WIN_KEY);
  116. // Test general key codes.
  117. assertEquals(
  118. goog.events.KeyCodes.normalizeGeckoKeyCode(KeyCodes.COMMA),
  119. KeyCodes.COMMA);
  120. assertEquals(
  121. goog.events.KeyCodes.normalizeKeyCode(KeyCodes.COMMA), KeyCodes.COMMA);
  122. }
  123. function testNormalizeMacWebKitKeyCode() {
  124. stubs.set(goog.userAgent, 'GECKO', false);
  125. stubs.set(goog.userAgent, 'MAC', true);
  126. stubs.set(goog.userAgent, 'WEBKIT', true);
  127. // Test Mac WebKit specific key codes.
  128. assertEquals(
  129. goog.events.KeyCodes.normalizeMacWebKitKeyCode(KeyCodes.MAC_WK_CMD_LEFT),
  130. KeyCodes.META);
  131. assertEquals(
  132. goog.events.KeyCodes.normalizeKeyCode(KeyCodes.MAC_WK_CMD_LEFT),
  133. KeyCodes.META);
  134. assertEquals(
  135. goog.events.KeyCodes.normalizeMacWebKitKeyCode(KeyCodes.MAC_WK_CMD_RIGHT),
  136. KeyCodes.META);
  137. assertEquals(
  138. goog.events.KeyCodes.normalizeKeyCode(KeyCodes.MAC_WK_CMD_RIGHT),
  139. KeyCodes.META);
  140. // Test general key codes.
  141. assertEquals(
  142. goog.events.KeyCodes.normalizeMacWebKitKeyCode(KeyCodes.COMMA),
  143. KeyCodes.COMMA);
  144. assertEquals(
  145. goog.events.KeyCodes.normalizeKeyCode(KeyCodes.COMMA), KeyCodes.COMMA);
  146. }