forked from google/jsaction
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnativeevents.js
224 lines (193 loc) · 8.16 KB
/
nativeevents.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
// Copyright 2011 Google Inc. All Rights Reserved.
/**
* @fileoverview Utility functions for generating native browser events.
*/
goog.provide('jsaction.testing.nativeEvents');
goog.setTestOnly('jsaction.testing.nativeEvents');
goog.require('goog.dom.NodeType');
goog.require('goog.events.BrowserEvent');
goog.require('goog.events.EventType');
goog.require('goog.style');
goog.require('goog.testing.events.Event');
goog.require('jsaction.createKeyboardEvent');
goog.require('jsaction.createMouseEvent');
goog.require('jsaction.triggerEvent');
/**
* Simulates a blur event on the given target.
* @param {EventTarget} target The target for the event.
* @return {boolean} The returnValue of the event: false if preventDefault() was
* called on it, true otherwise.
*/
jsaction.testing.nativeEvents.fireBlurEvent = function(target) {
var e = new goog.testing.events.Event(goog.events.EventType.BLUR, target);
return jsaction.triggerEvent(target, jsaction.createFocusEvent(e));
};
/**
* Simulates a focus event on the given target.
* @param {EventTarget} target The target for the event.
* @return {boolean} The returnValue of the event: false if preventDefault() was
* called on it, true otherwise.
*/
jsaction.testing.nativeEvents.fireFocusEvent = function(target) {
var e = new goog.testing.events.Event(goog.events.EventType.FOCUS, target);
return jsaction.triggerEvent(target, jsaction.createFocusEvent(e));
};
/**
* Simulates a mousedown, mouseup, and then click on the given event target,
* with the left mouse button.
* @param {EventTarget} target The target for the event.
* @param {goog.events.BrowserEvent.MouseButton=} opt_button Mouse button;
* defaults to {@code goog.events.BrowserEvent.MouseButton.LEFT}.
* @return {boolean} The returnValue of the sequence: false if preventDefault()
* was called on any of the events, true otherwise.
*/
jsaction.testing.nativeEvents.fireClickSequence = function(target, opt_button) {
return !!(
jsaction.testing.nativeEvents.fireMouseDownEvent(target, opt_button) &
jsaction.testing.nativeEvents.fireMouseUpEvent(target, opt_button) &
jsaction.testing.nativeEvents.fireClickEvent(target, opt_button));
};
/**
* Simulates a mousedown event on the given target.
* @param {EventTarget} target The target for the event.
* @param {goog.events.BrowserEvent.MouseButton=} opt_button Mouse button;
* defaults to {@code goog.events.BrowserEvent.MouseButton.LEFT}.
* @return {boolean} false if preventDefault() was called, true otherwise.
*/
jsaction.testing.nativeEvents.fireMouseDownEvent = function(
target, opt_button) {
return jsaction.testing.nativeEvents.fireMouseButtonEvent_(
goog.events.EventType.MOUSEDOWN, target, opt_button);
};
/**
* Simulates a mouseup event on the given target.
* @param {EventTarget} target The target for the event.
* @param {goog.events.BrowserEvent.MouseButton=} opt_button Mouse button;
* defaults to {@code goog.events.BrowserEvent.MouseButton.LEFT}.
* @return {boolean} false if preventDefault() was called, true otherwise.
*/
jsaction.testing.nativeEvents.fireMouseUpEvent = function(target, opt_button) {
return jsaction.testing.nativeEvents.fireMouseButtonEvent_(
goog.events.EventType.MOUSEUP, target, opt_button);
};
/**
* Simulates a click event on the given target.
* @param {EventTarget} target The target for the event.
* @param {goog.events.BrowserEvent.MouseButton=} opt_button Mouse button;
* defaults to {@code goog.events.BrowserEvent.MouseButton.LEFT}.
* @return {boolean} false if preventDefault() was called, true otherwise.
*/
jsaction.testing.nativeEvents.fireClickEvent = function(target, opt_button) {
return jsaction.testing.nativeEvents.fireMouseButtonEvent_(
goog.events.EventType.CLICK, target, opt_button);
};
/**
* Simulates a mouseover event on the given target.
* @param {EventTarget} target The target for the event.
* @return {boolean} false if preventDefault() was called, true otherwise.
*/
jsaction.testing.nativeEvents.fireMouseOverEvent = function(target) {
return jsaction.testing.nativeEvents.fireMouseButtonEvent_(
goog.events.EventType.MOUSEOVER, target);
};
/**
* Simulates a mouseout event on the given target.
* @param {EventTarget} target The target for the event.
* @return {boolean} false if preventDefault() was called, true otherwise.
*/
jsaction.testing.nativeEvents.fireMouseOutEvent = function(target) {
return jsaction.testing.nativeEvents.fireMouseButtonEvent_(
goog.events.EventType.MOUSEOUT, target);
};
/**
* Simulates a mousemove event on the given target.
* @param {EventTarget} target The target for the event.
* @param {goog.math.Coordinate=} opt_coords Mouse position. Defaults to event's
* target's position (if available), otherwise (0, 0).
* @return {boolean} The returnValue of the event: false if preventDefault() was
* called on it, true otherwise.
*/
jsaction.testing.nativeEvents.fireMouseMoveEvent = function(
target, opt_coords) {
if (!opt_coords && target &&
target.nodeType == goog.dom.NodeType.ELEMENT) {
try {
opt_coords =
goog.style.getClientPosition(/** @type {Element} **/ (target));
} catch (ex) {
// IE sometimes throws if it can't get the position.
}
}
var e = new goog.testing.events.Event(
goog.events.EventType.MOUSEMOVE, target);
var xPos = opt_coords ? opt_coords.x : 0;
var yPos = opt_coords ? opt_coords.y : 0;
e.clientX = e.screenX = xPos;
e.clientY = e.screenY = yPos;
return jsaction.triggerEvent(target, jsaction.createMouseEvent(e));
};
/**
* Creates a mouse button event.
* @param {string} type The event type.
* @param {!EventTarget} target The target for the event.
* @param {goog.events.BrowserEvent.MouseButton=} opt_button Mouse button;
* defaults to {@code goog.events.BrowserEvent.MouseButton.LEFT}.
* @return {!Event} The created event.
*/
jsaction.testing.nativeEvents.createMouseButtonEvent = function(
type, target, opt_button) {
var e = new goog.testing.events.Event(type, target);
e.button = opt_button || goog.events.BrowserEvent.MouseButton.LEFT;
return jsaction.createMouseEvent(e);
};
/**
* Helper function to fire a mouse event with a mouse button. IE < 9 only allows
* firing events using the left mouse button.
* @param {string} type The event type.
* @param {EventTarget} target The target for the event.
* @param {goog.events.BrowserEvent.MouseButton=} opt_button Mouse button;
* defaults to {@code goog.events.BrowserEvent.MouseButton.LEFT}.
* @return {boolean} The value returned by the browser event,
* which returns false iff 'preventDefault' was invoked.
* @private
*/
jsaction.testing.nativeEvents.fireMouseButtonEvent_ = function(
type, target, opt_button) {
var e = jsaction.testing.nativeEvents.createMouseButtonEvent(
type, target, opt_button);
return jsaction.triggerEvent(target, e);
};
/**
* Creates and initializes a key event.
* @param {string} eventType The type of event to create ("keydown", "keyup",
* or "keypress").
* @param {HTMLElement} node The event target.
* @param {number} keyCode The key code.
* @param {number} charCode The character code produced by the key.
* @return {Object} an initialized event object.
*/
jsaction.testing.nativeEvents.fireKeyEvent = function(
eventType, node, keyCode, charCode) {
var e = new goog.testing.events.Event(eventType, node);
e.charCode = charCode;
e.keyCode = keyCode;
var nativeEvent = jsaction.createKeyboardEvent(e);
jsaction.triggerEvent(node, nativeEvent);
return nativeEvent;
};
/**
* Generates a series of events simulating a key press on the given element.
* @param {HTMLElement} node The event target.
* @param {number} keyCode The key code.
* @param {number} charCode The character code produced by the key.
*/
jsaction.testing.nativeEvents.simulateKeyPress = function(
node, keyCode, charCode) {
var e;
e = jsaction.testing.nativeEvents.fireKeyEvent(
goog.events.EventType.KEYDOWN, node, keyCode, charCode);
e = jsaction.testing.nativeEvents.fireKeyEvent(
goog.events.EventType.KEYPRESS, node, keyCode, charCode);
e = jsaction.testing.nativeEvents.fireKeyEvent(
goog.events.EventType.KEYUP, node, keyCode, charCode);
};