Debian Codegraph @

closure-library/closure/goog/events/inputhandler_test.js

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