This repository has been archived by the owner on May 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 66
/
Copy pathnativeevents.js
342 lines (303 loc) · 13.1 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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// Copyright 2011 Google Inc. All Rights Reserved.
/**
* @fileoverview Utility functions for generating native browser events.
*/
goog.provide('jsaction.testing.nativeEvents');
goog.setTestOnly();
goog.require('goog.dom.NodeType');
goog.require('goog.events.BrowserEvent');
goog.require('goog.events.EventType');
goog.require('goog.object');
goog.require('goog.style');
goog.require('goog.testing.events.Event');
goog.require('jsaction.KeyCodes');
goog.require('jsaction.createKeyboardEvent');
goog.require('jsaction.createMouseEvent');
goog.require('jsaction.createUiEvent');
goog.require('jsaction.triggerEvent');
/**
* @typedef {{
* ctrlKey:(boolean|undefined),
* altKey:(boolean|undefined),
* shiftKey:(boolean|undefined),
* metaKey:(boolean|undefined),
* }}
*/
jsaction.testing.nativeEvents.Modifiers;
/**
* 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) {
const e = new goog.testing.events.Event(goog.events.EventType.BLUR, target);
return jsaction.triggerEvent(target, jsaction.createUiEvent(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) {
const e = new goog.testing.events.Event(goog.events.EventType.FOCUS, target);
return jsaction.triggerEvent(target, jsaction.createUiEvent(e));
};
/**
* Simulates a scroll 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.fireScrollEvent = function(target) {
const e = new goog.testing.events.Event(goog.events.EventType.SCROLL, target);
return jsaction.triggerEvent(target, jsaction.createUiEvent(e));
};
/**
* Simulates a customizable event on the given target.
* @param {!goog.events.EventType} eventType The type of DOM event to fire.
* @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.fireDomEvent = function(eventType, target) {
const e = new goog.testing.events.Event(eventType, target);
return jsaction.triggerEvent(target, jsaction.createUiEvent(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 `goog.events.BrowserEvent.MouseButton.LEFT`.
* @param {?goog.math.Coordinate=} opt_coords Mouse position. Defaults to
* event's target's position (if available), otherwise (0, 0).
* @param {?Object=} opt_eventProperties Event properties to be mixed into the
* BrowserEvent.
* @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, opt_coords, opt_eventProperties) {
return !!(
jsaction.testing.nativeEvents.fireMouseDownEvent(
target, opt_button, opt_coords, opt_eventProperties) &
jsaction.testing.nativeEvents.fireMouseUpEvent(
target, opt_button, opt_coords, opt_eventProperties) &
jsaction.testing.nativeEvents.fireClickEvent(
target, opt_button, opt_coords, opt_eventProperties));
};
/**
* 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 `goog.events.BrowserEvent.MouseButton.LEFT`.
* @param {?goog.math.Coordinate=} opt_coords Mouse position. Defaults to
* event's target's position (if available), otherwise (0, 0).
* @param {?Object=} opt_eventProperties Event properties to be mixed into the
* BrowserEvent.
* @return {boolean} false if preventDefault() was called, true otherwise.
*/
jsaction.testing.nativeEvents.fireMouseDownEvent = function(
target, opt_button, opt_coords, opt_eventProperties) {
return jsaction.testing.nativeEvents.fireMouseButtonEvent_(
goog.events.EventType.MOUSEDOWN, target, opt_button, opt_coords,
opt_eventProperties);
};
/**
* 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 `goog.events.BrowserEvent.MouseButton.LEFT`.
* @param {?goog.math.Coordinate=} opt_coords Mouse position. Defaults to
* event's target's position (if available), otherwise (0, 0).
* @param {?Object=} opt_eventProperties Event properties to be mixed into the
* BrowserEvent.
* @return {boolean} false if preventDefault() was called, true otherwise.
*/
jsaction.testing.nativeEvents.fireMouseUpEvent = function(
target, opt_button, opt_coords, opt_eventProperties) {
return jsaction.testing.nativeEvents.fireMouseButtonEvent_(
goog.events.EventType.MOUSEUP, target, opt_button, opt_coords,
opt_eventProperties);
};
/**
* 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 `goog.events.BrowserEvent.MouseButton.LEFT`.
* @param {?goog.math.Coordinate=} opt_coords Mouse position. Defaults to
* event's target's position (if available), otherwise (0, 0).
* @param {?Object=} opt_eventProperties Event properties to be mixed into the
* BrowserEvent.
* @return {boolean} false if preventDefault() was called, true otherwise.
*/
jsaction.testing.nativeEvents.fireClickEvent =
function(target, opt_button, opt_coords, opt_eventProperties) {
return jsaction.testing.nativeEvents.fireMouseButtonEvent_(
goog.events.EventType.CLICK, target, opt_button, opt_coords,
opt_eventProperties);
};
/**
* 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) {
const e = new goog.testing.events.Event(
goog.events.EventType.MOUSEMOVE, target);
jsaction.testing.nativeEvents.setEventClientXY_(e, opt_coords);
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 `goog.events.BrowserEvent.MouseButton.LEFT`.
* @param {boolean=} opt_modifierKey Create the event with the modifier key
* registered as down.
* @param {?goog.math.Coordinate=} opt_coords Mouse position. Defaults to
* event's target's position (if available), otherwise (0, 0).
* @param {?Object=} opt_eventProperties Event properties to be mixed into the
* BrowserEvent.
* @return {!Event} The created event.
*/
jsaction.testing.nativeEvents.createMouseButtonEvent = function(
type, target, opt_button, opt_modifierKey, opt_coords,
opt_eventProperties) {
const e = new goog.testing.events.Event(type, target);
e.button = opt_button || goog.events.BrowserEvent.MouseButton.LEFT;
jsaction.testing.nativeEvents.setEventClientXY_(e, opt_coords);
if (opt_eventProperties) {
goog.object.extend(e, opt_eventProperties);
}
if (opt_modifierKey) {
e.ctrlKey = true;
e.metaKey = true;
}
return jsaction.createMouseEvent(e);
};
/**
* A static helper function that sets the mouse position to the event.
* @param {!Event} event A simulated native event.
* @param {?goog.math.Coordinate=} opt_coords Mouse position. Defaults to
* event's target's position (if available), otherwise (0, 0).
* @private
*/
jsaction.testing.nativeEvents.setEventClientXY_ = function(event, opt_coords) {
if (!opt_coords && event.target &&
event.target.nodeType == goog.dom.NodeType.ELEMENT) {
try {
opt_coords =
goog.style.getClientPosition(/** @type {!Element} **/ (event.target));
} catch (ex) {
// IE sometimes throws if it can't get the position.
}
}
event.clientX = opt_coords ? opt_coords.x : 0;
event.clientY = opt_coords ? opt_coords.y : 0;
// Pretend the browser window is at (0, 0).
event.screenX = event.clientX;
event.screenY = event.clientY;
};
/**
* 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 `goog.events.BrowserEvent.MouseButton.LEFT`.
* @param {?goog.math.Coordinate=} opt_coords Mouse position. Defaults to
* event's target's position (if available), otherwise (0, 0).
* @param {?Object=} opt_eventProperties Event properties to be mixed into the
* BrowserEvent.
* @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, opt_coords, opt_eventProperties) {
const e = jsaction.testing.nativeEvents.createMouseButtonEvent(
type, target, opt_button, undefined, opt_coords, opt_eventProperties);
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 {!EventTarget} target The event target.
* @param {number} keyCode The key code.
* @param {number} charCode The character code produced by the key.
* @param {!jsaction.testing.nativeEvents.Modifiers=} modifiers
* @return {boolean} The value returned by the browser event,
* which returns false iff 'preventDefault' was invoked.
*/
jsaction.testing.nativeEvents.fireKeyEvent = function(
eventType, target, keyCode, charCode, modifiers = {}) {
const e = new goog.testing.events.Event(eventType, target);
e.charCode = charCode;
e.keyCode = keyCode;
goog.object.extend(e, modifiers);
return jsaction.triggerEvent(target, jsaction.createKeyboardEvent(e));
};
/**
* Generates a series of events simulating a key press on the given element.
* @param {!EventTarget} target The event target.
* @param {number} keyCode The key code.
* @param {number=} charCode The character code produced by the key.
* @param {!jsaction.testing.nativeEvents.Modifiers=} modifiers
* @return {boolean} False if preventDefault() was called by any of the handlers
*/
jsaction.testing.nativeEvents.simulateKeyPress = function(
target, keyCode, charCode = keyCode, modifiers = {}) {
let e;
e = jsaction.testing.nativeEvents.fireKeyEvent(
goog.events.EventType.KEYDOWN, target, keyCode, charCode, modifiers);
e = jsaction.testing.nativeEvents.fireKeyEvent(
goog.events.EventType.KEYPRESS, target, keyCode, charCode,
modifiers) &&
e;
// A click event is fired by browsers when pressing Enter on <button>
// elements, for a11y.
if ((keyCode == jsaction.KeyCodes.ENTER ||
keyCode == jsaction.KeyCodes.MAC_ENTER) &&
target.nodeName == 'BUTTON') {
e = jsaction.testing.nativeEvents.fireClickEvent(
target, undefined, undefined, modifiers) &&
e;
}
e = jsaction.testing.nativeEvents.fireKeyEvent(
goog.events.EventType.KEYUP, target, keyCode, charCode, modifiers) &&
e;
// Same as the Enter "click", but for Space it happens after keyup.
if ((keyCode == jsaction.KeyCodes.SPACE) && target.nodeName == 'BUTTON') {
e = jsaction.testing.nativeEvents.fireClickEvent(
target, undefined, undefined, modifiers) &&
e;
}
return e;
};