inputhandler_test.js 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. // Copyright 2013 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.InputHandlerTest');
  15. goog.setTestOnly('goog.events.InputHandlerTest');
  16. goog.require('goog.dom');
  17. goog.require('goog.events.EventHandler');
  18. goog.require('goog.events.EventType');
  19. goog.require('goog.events.InputHandler');
  20. goog.require('goog.events.KeyCodes');
  21. goog.require('goog.testing.events');
  22. goog.require('goog.testing.events.Event');
  23. goog.require('goog.testing.jsunit');
  24. goog.require('goog.testing.recordFunction');
  25. goog.require('goog.userAgent');
  26. var inputHandler;
  27. var eventHandler;
  28. function setUp() {
  29. eventHandler = new goog.events.EventHandler();
  30. }
  31. function tearDown() {
  32. goog.dispose(inputHandler);
  33. goog.dispose(eventHandler);
  34. }
  35. function testInputWithPlaceholder() {
  36. var input = goog.dom.getElement('input-w-placeholder');
  37. inputHandler = new goog.events.InputHandler(input);
  38. var callback = listenToInput(inputHandler);
  39. fireFakeInputEvent(input);
  40. assertEquals(0, callback.getCallCount());
  41. }
  42. function testInputWithPlaceholder_withValue() {
  43. var input = goog.dom.getElement('input-w-placeholder');
  44. inputHandler = new goog.events.InputHandler(input);
  45. var callback = listenToInput(inputHandler);
  46. input.value = 'foo';
  47. fireFakeInputEvent(input);
  48. assertEquals(0, callback.getCallCount());
  49. }
  50. function testInputWithPlaceholder_someKeys() {
  51. var input = goog.dom.getElement('input-w-placeholder');
  52. inputHandler = new goog.events.InputHandler(input);
  53. var callback = listenToInput(inputHandler);
  54. input.focus();
  55. input.value = 'foo';
  56. fireInputEvent(input, goog.events.KeyCodes.M);
  57. assertEquals(1, callback.getCallCount());
  58. }
  59. function listenToInput(inputHandler) {
  60. var callback = goog.testing.recordFunction();
  61. eventHandler.listen(
  62. inputHandler, goog.events.InputHandler.EventType.INPUT, callback);
  63. return callback;
  64. }
  65. function fireFakeInputEvent(input) {
  66. // Simulate the input event that IE fires on focus when a placeholder
  67. // is present.
  68. input.focus();
  69. if (goog.userAgent.IE && goog.userAgent.isVersionOrHigher(10)) {
  70. // IE fires an input event with keycode 0
  71. fireInputEvent(input, 0);
  72. }
  73. }
  74. function fireInputEvent(input, keyCode) {
  75. var inputEvent =
  76. new goog.testing.events.Event(goog.events.EventType.INPUT, input);
  77. inputEvent.keyCode = keyCode;
  78. inputEvent.charCode = keyCode;
  79. goog.testing.events.fireBrowserEvent(inputEvent);
  80. }