imehandler_test.js 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  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.ImeHandlerTest');
  15. goog.setTestOnly('goog.events.ImeHandlerTest');
  16. goog.require('goog.array');
  17. goog.require('goog.dom');
  18. goog.require('goog.events');
  19. goog.require('goog.events.ImeHandler');
  20. goog.require('goog.events.KeyCodes');
  21. goog.require('goog.object');
  22. goog.require('goog.string');
  23. goog.require('goog.testing.PropertyReplacer');
  24. goog.require('goog.testing.events');
  25. goog.require('goog.testing.events.Event');
  26. goog.require('goog.testing.jsunit');
  27. goog.require('goog.userAgent');
  28. var sandbox;
  29. var imeHandler;
  30. var eventsFired;
  31. var stubs = new goog.testing.PropertyReplacer();
  32. var eventTypes = goog.events.ImeHandler.EventType;
  33. function setUp() {
  34. sandbox = goog.dom.getElement('sandbox');
  35. }
  36. function initImeHandler() {
  37. goog.events.ImeHandler.USES_COMPOSITION_EVENTS = goog.userAgent.GECKO ||
  38. (goog.userAgent.WEBKIT && goog.userAgent.isVersionOrHigher(532));
  39. imeHandler = new goog.events.ImeHandler(sandbox);
  40. eventsFired = [];
  41. goog.events.listen(
  42. imeHandler, goog.object.getValues(goog.events.ImeHandler.EventType),
  43. function(e) { eventsFired.push(e.type); });
  44. }
  45. function tearDown() {
  46. imeHandler.dispose();
  47. imeHandler = null;
  48. stubs.reset();
  49. }
  50. function tearDownPage() {
  51. // Set up a test bed.
  52. sandbox.innerHTML = '<div contentEditable="true">hello world</div>';
  53. initImeHandler();
  54. function unshiftEvent(e) {
  55. last10Events.unshift(
  56. e.type + ':' + e.keyCode + ':' +
  57. goog.string.htmlEscape(goog.dom.getTextContent(sandbox)));
  58. last10Events.length = Math.min(last10Events.length, 10);
  59. goog.dom.getElement('logger').innerHTML = last10Events.join('<br>');
  60. }
  61. var last10Events = [];
  62. goog.events.listen(
  63. imeHandler, goog.object.getValues(goog.events.ImeHandler.EventType),
  64. unshiftEvent);
  65. goog.events.listen(sandbox, ['keydown', 'textInput'], unshiftEvent);
  66. }
  67. function assertEventsFired(var_args) {
  68. assertArrayEquals(goog.array.clone(arguments), eventsFired);
  69. }
  70. function fireInputEvent(type) {
  71. return goog.testing.events.fireBrowserEvent(
  72. new goog.testing.events.Event(type, sandbox));
  73. }
  74. function fireImeKeySequence() {
  75. return fireKeySequence(goog.events.KeyCodes.WIN_IME);
  76. }
  77. function fireKeySequence(keyCode) {
  78. return (
  79. goog.testing.events.fireBrowserEvent(
  80. new goog.testing.events.Event('textInput', sandbox)) &
  81. goog.testing.events.fireKeySequence(sandbox, keyCode));
  82. }
  83. function testHandleKeyDown_GeckoCompositionEvents() {
  84. // This test verifies that our IME functions can dispatch IME events to
  85. // InputHandler in the expected order on Gecko.
  86. // Set the userAgent used for this test to Firefox.
  87. setUserAgent('GECKO');
  88. stubs.set(goog.userAgent, 'MAC', false);
  89. initImeHandler();
  90. fireInputEvent('compositionstart');
  91. assertImeMode();
  92. fireInputEvent('compositionupdate');
  93. fireInputEvent('compositionupdate');
  94. fireInputEvent('compositionend');
  95. assertEventsFired(
  96. eventTypes.START, eventTypes.UPDATE, eventTypes.UPDATE, eventTypes.END);
  97. assertNotImeMode();
  98. }
  99. /**
  100. * Verifies that our IME functions can dispatch IME events to the input handler
  101. * in the expected order on Chrome. jsUnitFarm does not have Linux Chrome or
  102. * Mac Chrome. So, we manually change the platform and run this test three
  103. * times.
  104. */
  105. function testChromeCompositionEventsLinux() {
  106. runChromeCompositionEvents('LINUX');
  107. }
  108. function testChromeCompositionEventsMac() {
  109. runChromeCompositionEvents('MAC');
  110. }
  111. function testChromeCompositionEventsWindows() {
  112. runChromeCompositionEvents('WINDOWS');
  113. }
  114. function runChromeCompositionEvents(platform) {
  115. setUserAgent('WEBKIT');
  116. setVersion(532);
  117. stubs.set(goog.userAgent, platform, true);
  118. initImeHandler();
  119. fireImeKeySequence();
  120. fireInputEvent('compositionstart');
  121. assertImeMode();
  122. fireInputEvent('compositionupdate');
  123. fireInputEvent('compositionupdate');
  124. fireInputEvent('compositionend');
  125. assertEventsFired(
  126. eventTypes.START, eventTypes.UPDATE, eventTypes.UPDATE, eventTypes.END);
  127. assertNotImeMode();
  128. }
  129. /**
  130. * Ensures that the IME mode turn on/off correctly.
  131. */
  132. function testHandlerKeyDownForIme_imeOnOff() {
  133. setUserAgent('IE');
  134. initImeHandler();
  135. // Send a WIN_IME keyDown event and see whether IME mode turns on.
  136. fireImeKeySequence();
  137. assertImeMode();
  138. // Send keyDown events which should not turn off IME mode and see whether
  139. // IME mode holds on.
  140. fireKeySequence(goog.events.KeyCodes.SHIFT);
  141. assertImeMode();
  142. fireKeySequence(goog.events.KeyCodes.CTRL);
  143. assertImeMode();
  144. // Send a keyDown event with keyCode = ENTER and see whether IME mode
  145. // turns off.
  146. fireKeySequence(goog.events.KeyCodes.ENTER);
  147. assertNotImeMode();
  148. assertEventsFired(eventTypes.START, eventTypes.END);
  149. }
  150. /**
  151. * Ensures that IME mode turns off when keyup events which are involved
  152. * in committing IME text occurred in Safari.
  153. */
  154. function testHandleKeyUpForSafari() {
  155. setUserAgent('WEBKIT');
  156. setVersion(531);
  157. initImeHandler();
  158. fireImeKeySequence();
  159. assertImeMode();
  160. fireKeySequence(goog.events.KeyCodes.ENTER);
  161. assertNotImeMode();
  162. }
  163. /**
  164. * SCIM on Linux will fire WIN_IME keycodes for random characters.
  165. * Fortunately, all Linux-based browsers use composition events.
  166. * This test just verifies that we ignore the WIN_IME keycodes.
  167. */
  168. function testScimFiresWinImeKeycodesGeckoLinux() {
  169. setUserAgent('GECKO');
  170. assertScimInputIgnored();
  171. }
  172. function testScimFiresWinImeKeycodesChromeLinux() {
  173. setUserAgent('WEBKIT');
  174. setVersion(532);
  175. assertScimInputIgnored();
  176. }
  177. function assertScimInputIgnored() {
  178. initImeHandler();
  179. fireImeKeySequence();
  180. assertNotImeMode();
  181. fireInputEvent('compositionstart');
  182. assertImeMode();
  183. fireImeKeySequence();
  184. assertImeMode();
  185. fireInputEvent('compositionend');
  186. assertNotImeMode();
  187. }
  188. var userAgents = ['IE', 'GECKO', 'WEBKIT'];
  189. function setUserAgent(userAgent) {
  190. for (var i = 0; i < userAgents.length; i++) {
  191. stubs.set(goog.userAgent, userAgents[i], userAgents[i] == userAgent);
  192. }
  193. }
  194. function setVersion(version) {
  195. goog.userAgent.VERSION = version;
  196. goog.userAgent.isVersionOrHigherCache_ = {};
  197. }
  198. function assertImeMode() {
  199. assertTrue('Should be in IME mode.', imeHandler.isImeMode());
  200. }
  201. function assertNotImeMode() {
  202. assertFalse('Should not be in IME mode.', imeHandler.isImeMode());
  203. }