Debian Codegraph @

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

raw
1 // Copyright 2008 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 /**
16 * @fileoverview Event Simulation.
17 *
18 * Utility functions for simulating events at the Closure level. All functions
19 * in this package generate events by calling goog.events.fireListeners,
20 * rather than interfacing with the browser directly. This is intended for
21 * testing purposes, and should not be used in production code.
22 *
23 * The decision to use Closure events and dispatchers instead of the browser's
24 * native events and dispatchers was conscious and deliberate. Native event
25 * dispatchers have their own set of quirks and edge cases. Pure JS dispatchers
26 * are more robust and transparent.
27 *
28 * If you think you need a testing mechanism that uses native Event objects,
29 * please, please email closure-tech first to explain your use case before you
30 * sink time into this.
31 *
32 * TODO(b/8933952): Migrate to explicitly non-nullable types. At present, many
33 * functions in this file expect non-null inputs but do not explicitly
34 * indicate this.
35 *
36 * @author [email protected] (Nick Santos)
37 */
38
39 goog.setTestOnly('goog.testing.events');
40 goog.provide('goog.testing.events');
41 goog.provide('goog.testing.events.Event');
42
43 goog.require('goog.Disposable');
44 goog.require('goog.asserts');
45 goog.require('goog.dom.NodeType');
46 goog.require('goog.events');
47 goog.require('goog.events.BrowserEvent');
48 goog.require('goog.events.BrowserFeature');
49 goog.require('goog.events.EventTarget');
50 goog.require('goog.events.EventType');
51 goog.require('goog.events.KeyCodes');
52 goog.require('goog.object');
53 goog.require('goog.style');
54 goog.require('goog.userAgent');
55
56
57
58 /**
59 * goog.events.BrowserEvent expects an Event so we provide one for JSCompiler.
60 *
61 * This clones a lot of the functionality of goog.events.Event. This used to
62 * use a mixin, but the mixin results in confusing the two types when compiled.
63 *
64 * @param {string} type Event Type.
65 * @param {Object=} opt_target Reference to the object that is the target of
66 * this event.
67 * @constructor
68 * @extends {Event}
69 */
70 goog.testing.events.Event = function(type, opt_target) {
71 this.type = type;
72
73 this.target = /** @type {EventTarget} */ (opt_target || null);
74
75 this.currentTarget = this.target;
76 };
77
78
79 /**
80 * Whether to cancel the event in internal capture/bubble processing for IE.
81 * @type {boolean}
82 * @public
83 * @suppress {underscore|visibility} Technically public, but referencing this
84 * outside this package is strongly discouraged.
85 */
86 goog.testing.events.Event.prototype.propagationStopped_ = false;
87
88
89 /** @override */
90 goog.testing.events.Event.prototype.defaultPrevented = false;
91
92
93 /**
94 * Return value for in internal capture/bubble processing for IE.
95 * @type {boolean}
96 * @public
97 * @suppress {underscore|visibility} Technically public, but referencing this
98 * outside this package is strongly discouraged.
99 */
100 goog.testing.events.Event.prototype.returnValue_ = true;
101
102
103 /** @override */
104 goog.testing.events.Event.prototype.stopPropagation = function() {
105 this.propagationStopped_ = true;
106 };
107
108
109 /** @override */
110 goog.testing.events.Event.prototype.preventDefault = function() {
111 this.defaultPrevented = true;
112 this.returnValue_ = false;
113 };
114
115
116 /**
117 * Asserts an event target exists. This will fail if target is not defined.
118 *
119 * TODO(nnaze): Gradually add this to the methods in this file, and eventually
120 * update the method signatures to not take nullables. See http://b/8961907
121 *
122 * @param {EventTarget} target A target to assert.
123 * @return {!EventTarget} The target, guaranteed to exist.
124 * @private
125 */
126 goog.testing.events.assertEventTarget_ = function(target) {
127 return goog.asserts.assert(target, 'EventTarget should be defined.');
128 };
129
130
131 /**
132 * A static helper function that sets the mouse position to the event.
133 * @param {Event} event A simulated native event.
134 * @param {goog.math.Coordinate=} opt_coords Mouse position. Defaults to event's
135 * target's position (if available), otherwise (0, 0).
136 * @private
137 */
138 goog.testing.events.setEventClientXY_ = function(event, opt_coords) {
139 if (!opt_coords && event.target &&
140 /** @type {!Node} */ (event.target).nodeType ==
141 goog.dom.NodeType.ELEMENT) {
142 try {
143 opt_coords =
144 goog.style.getClientPosition(/** @type {!Element} **/ (event.target));
145 } catch (ex) {
146 // IE sometimes throws if it can't get the position.
147 }
148 }
149 event.clientX = opt_coords ? opt_coords.x : 0;
150 event.clientY = opt_coords ? opt_coords.y : 0;
151
152 // Pretend the browser window is at (0, 0).
153 event.screenX = event.clientX;
154 event.screenY = event.clientY;
155 };
156
157
158 /**
159 * Simulates a mousedown, mouseup, and then click on the given event target,
160 * with the left mouse button.
161 * @param {EventTarget} target The target for the event.
162 * @param {goog.events.BrowserEvent.MouseButton=} opt_button Mouse button;
163 * defaults to {@code goog.events.BrowserEvent.MouseButton.LEFT}.
164 * @param {goog.math.Coordinate=} opt_coords Mouse position. Defaults to event's
165 * target's position (if available), otherwise (0, 0).
166 * @param {Object=} opt_eventProperties Event properties to be mixed into the
167 * BrowserEvent.
168 * @return {boolean} The returnValue of the sequence: false if preventDefault()
169 * was called on any of the events, true otherwise.
170 */
171 goog.testing.events.fireClickSequence = function(
172 target, opt_button, opt_coords, opt_eventProperties) {
173 // Fire mousedown, mouseup, and click. Then return the bitwise AND of the 3.
174 return !!(
175 goog.testing.events.fireMouseDownEvent(
176 target, opt_button, opt_coords, opt_eventProperties) &
177 goog.testing.events.fireMouseUpEvent(
178 target, opt_button, opt_coords, opt_eventProperties) &
179 goog.testing.events.fireClickEvent(
180 target, opt_button, opt_coords, opt_eventProperties));
181 };
182
183
184 /**
185 * Simulates the sequence of events fired by the browser when the user double-
186 * clicks the given target.
187 * @param {EventTarget} target The target for the event.
188 * @param {goog.math.Coordinate=} opt_coords Mouse position. Defaults to event's
189 * target's position (if available), otherwise (0, 0).
190 * @param {Object=} opt_eventProperties Event properties to be mixed into the
191 * BrowserEvent.
192 * @return {boolean} The returnValue of the sequence: false if preventDefault()
193 * was called on any of the events, true otherwise.
194 */
195 goog.testing.events.fireDoubleClickSequence = function(
196 target, opt_coords, opt_eventProperties) {
197 // Fire mousedown, mouseup, click, mousedown, mouseup, click, dblclick.
198 // Then return the bitwise AND of the 7.
199 var btn = goog.events.BrowserEvent.MouseButton.LEFT;
200 return !!(
201 goog.testing.events.fireMouseDownEvent(
202 target, btn, opt_coords, opt_eventProperties) &
203 goog.testing.events.fireMouseUpEvent(
204 target, btn, opt_coords, opt_eventProperties) &
205 goog.testing.events.fireClickEvent(
206 target, btn, opt_coords, opt_eventProperties) &
207 // IE fires a selectstart instead of the second mousedown in a
208 // dblclick, but we don't care about selectstart.
209 (goog.userAgent.IE ||
210 goog.testing.events.fireMouseDownEvent(
211 target, btn, opt_coords, opt_eventProperties)) &
212 goog.testing.events.fireMouseUpEvent(
213 target, btn, opt_coords, opt_eventProperties) &
214 // IE doesn't fire the second click in a dblclick.
215 (goog.userAgent.IE ||
216 goog.testing.events.fireClickEvent(
217 target, btn, opt_coords, opt_eventProperties)) &
218 goog.testing.events.fireDoubleClickEvent(
219 target, opt_coords, opt_eventProperties));
220 };
221
222
223 /**
224 * Simulates a complete keystroke (keydown, keypress, and keyup). Note that
225 * if preventDefault is called on the keydown, the keypress will not fire.
226 *
227 * @param {EventTarget} target The target for the event.
228 * @param {string|number} keyOrKeyCode The key value or keycode of the key
229 * pressed.
230 * @param {Object=} opt_eventProperties Event properties to be mixed into the
231 * BrowserEvent.
232 * @return {boolean} The returnValue of the sequence: false if preventDefault()
233 * was called on any of the events, true otherwise.
234 */
235 goog.testing.events.fireKeySequence = function(
236 target, keyOrKeyCode, opt_eventProperties) {
237 return goog.testing.events.fireNonAsciiKeySequence(
238 target, keyOrKeyCode, keyOrKeyCode, opt_eventProperties);
239 };
240
241
242 /**
243 * Simulates a complete keystroke (keydown, keypress, and keyup) when typing
244 * a non-ASCII character. Same as fireKeySequence, the keypress will not fire
245 * if preventDefault is called on the keydown.
246 *
247 * @param {EventTarget} target The target for the event.
248 * @param {string|number} keyOrKeyCode The key value or keycode of the keydown
249 * and keyup events.
250 * @param {string|number} keyPressKeyOrKeyCode The key value or keycode of the
251 * keypress event.
252 * @param {Object=} opt_eventProperties Event properties to be mixed into the
253 * BrowserEvent.
254 * @return {boolean} The returnValue of the sequence: false if preventDefault()
255 * was called on any of the events, true otherwise.
256 */
257 goog.testing.events.fireNonAsciiKeySequence = function(
258 target, keyOrKeyCode, keyPressKeyOrKeyCode, opt_eventProperties) {
259 var keydown =
260 new goog.testing.events.Event(goog.events.EventType.KEYDOWN, target);
261 var keyup =
262 new goog.testing.events.Event(goog.events.EventType.KEYUP, target);
263 var keypress =
264 new goog.testing.events.Event(goog.events.EventType.KEYPRESS, target);
265 if (goog.isString(keyOrKeyCode)) {
266 keydown.key = keyup.key = /** @type {string} */ (keyOrKeyCode);
267 keypress.key = /** @type {string} */ (keyPressKeyOrKeyCode);
268 } else {
269 keydown.keyCode = keyup.keyCode = /** @type {number} */ (keyOrKeyCode);
270 keypress.keyCode = /** @type {number} */ (keyPressKeyOrKeyCode);
271 }
272
273 if (opt_eventProperties) {
274 goog.object.extend(keydown, opt_eventProperties);
275 goog.object.extend(keyup, opt_eventProperties);
276 goog.object.extend(keypress, opt_eventProperties);
277 }
278
279 // Fire keydown, keypress, and keyup. Note that if the keydown is
280 // prevent-defaulted, then the keypress will not fire.
281 var result = true;
282 if (!goog.testing.events.isBrokenGeckoMacActionKey_(keydown)) {
283 result = goog.testing.events.fireBrowserEvent(keydown);
284 }
285 if (goog.isString(keyOrKeyCode)) {
286 if (/** @type {string} */ (keyPressKeyOrKeyCode) != '' && result) {
287 result &= goog.testing.events.fireBrowserEvent(keypress);
288 }
289 } else {
290 if (goog.events.KeyCodes.firesKeyPressEvent(
291 /** @type {number} */ (keyOrKeyCode), undefined, keydown.shiftKey,
292 keydown.ctrlKey, keydown.altKey) &&
293 result) {
294 result &= goog.testing.events.fireBrowserEvent(keypress);
295 }
296 }
297 return !!(result & goog.testing.events.fireBrowserEvent(keyup));
298 };
299
300
301 /**
302 * @param {goog.testing.events.Event} e The event.
303 * @return {boolean} Whether this is the Gecko/Mac's Meta-C/V/X, which
304 * is broken and requires special handling.
305 * @private
306 */
307 goog.testing.events.isBrokenGeckoMacActionKey_ = function(e) {
308 return goog.userAgent.MAC && goog.userAgent.GECKO &&
309 (e.keyCode == goog.events.KeyCodes.C ||
310 e.keyCode == goog.events.KeyCodes.X ||
311 e.keyCode == goog.events.KeyCodes.V) &&
312 e.metaKey;
313 };
314
315
316 /**
317 * Simulates a mouseenter event on the given target.
318 * @param {!EventTarget} target The target for the event.
319 * @param {?EventTarget} relatedTarget The related target for the event (e.g.,
320 * the node that the mouse is being moved out of).
321 * @param {!goog.math.Coordinate=} opt_coords Mouse position. Defaults to
322 * event's target's position (if available), otherwise (0, 0).
323 * @return {boolean} The returnValue of the event: false if preventDefault() was
324 * called on it, true otherwise.
325 */
326 goog.testing.events.fireMouseEnterEvent = function(
327 target, relatedTarget, opt_coords) {
328 var mouseenter =
329 new goog.testing.events.Event(goog.events.EventType.MOUSEENTER, target);
330 mouseenter.relatedTarget = relatedTarget;
331 goog.testing.events.setEventClientXY_(mouseenter, opt_coords);
332 return goog.testing.events.fireBrowserEvent(mouseenter);
333 };
334
335
336 /**
337 * Simulates a mouseleave event on the given target.
338 * @param {!EventTarget} target The target for the event.
339 * @param {?EventTarget} relatedTarget The related target for the event (e.g.,
340 * the node that the mouse is being moved into).
341 * @param {!goog.math.Coordinate=} opt_coords Mouse position. Defaults to
342 * event's target's position (if available), otherwise (0, 0).
343 * @return {boolean} The returnValue of the event: false if preventDefault() was
344 * called on it, true otherwise.
345 */
346 goog.testing.events.fireMouseLeaveEvent = function(
347 target, relatedTarget, opt_coords) {
348 var mouseleave =
349 new goog.testing.events.Event(goog.events.EventType.MOUSELEAVE, target);
350 mouseleave.relatedTarget = relatedTarget;
351 goog.testing.events.setEventClientXY_(mouseleave, opt_coords);
352 return goog.testing.events.fireBrowserEvent(mouseleave);
353 };
354
355
356 /**
357 * Simulates a mouseover event on the given target.
358 * @param {EventTarget} target The target for the event.
359 * @param {EventTarget} relatedTarget The related target for the event (e.g.,
360 * the node that the mouse is being moved out of).
361 * @param {goog.math.Coordinate=} opt_coords Mouse position. Defaults to event's
362 * target's position (if available), otherwise (0, 0).
363 * @return {boolean} The returnValue of the event: false if preventDefault() was
364 * called on it, true otherwise.
365 */
366 goog.testing.events.fireMouseOverEvent = function(
367 target, relatedTarget, opt_coords) {
368 var mouseover =
369 new goog.testing.events.Event(goog.events.EventType.MOUSEOVER, target);
370 mouseover.relatedTarget = relatedTarget;
371 goog.testing.events.setEventClientXY_(mouseover, opt_coords);
372 return goog.testing.events.fireBrowserEvent(mouseover);
373 };
374
375
376 /**
377 * Simulates a mousemove event on the given target.
378 * @param {EventTarget} target The target for the event.
379 * @param {goog.math.Coordinate=} opt_coords Mouse position. Defaults to event's
380 * target's position (if available), otherwise (0, 0).
381 * @return {boolean} The returnValue of the event: false if preventDefault() was
382 * called on it, true otherwise.
383 */
384 goog.testing.events.fireMouseMoveEvent = function(target, opt_coords) {
385 var mousemove =
386 new goog.testing.events.Event(goog.events.EventType.MOUSEMOVE, target);
387
388 goog.testing.events.setEventClientXY_(mousemove, opt_coords);
389 return goog.testing.events.fireBrowserEvent(mousemove);
390 };
391
392
393 /**
394 * Simulates a mouseout event on the given target.
395 * @param {EventTarget} target The target for the event.
396 * @param {EventTarget} relatedTarget The related target for the event (e.g.,
397 * the node that the mouse is being moved into).
398 * @param {goog.math.Coordinate=} opt_coords Mouse position. Defaults to event's
399 * target's position (if available), otherwise (0, 0).
400 * @return {boolean} The returnValue of the event: false if preventDefault() was
401 * called on it, true otherwise.
402 */
403 goog.testing.events.fireMouseOutEvent = function(
404 target, relatedTarget, opt_coords) {
405 var mouseout =
406 new goog.testing.events.Event(goog.events.EventType.MOUSEOUT, target);
407 mouseout.relatedTarget = relatedTarget;
408 goog.testing.events.setEventClientXY_(mouseout, opt_coords);
409 return goog.testing.events.fireBrowserEvent(mouseout);
410 };
411
412
413 /**
414 * Simulates a mousedown event on the given target.
415 * @param {EventTarget} target The target for the event.
416 * @param {goog.events.BrowserEvent.MouseButton=} opt_button Mouse button;
417 * defaults to {@code goog.events.BrowserEvent.MouseButton.LEFT}.
418 * @param {goog.math.Coordinate=} opt_coords Mouse position. Defaults to event's
419 * target's position (if available), otherwise (0, 0).
420 * @param {Object=} opt_eventProperties Event properties to be mixed into the
421 * BrowserEvent.
422 * @return {boolean} The returnValue of the event: false if preventDefault() was
423 * called on it, true otherwise.
424 */
425 goog.testing.events.fireMouseDownEvent = function(
426 target, opt_button, opt_coords, opt_eventProperties) {
427
428 var button = opt_button || goog.events.BrowserEvent.MouseButton.LEFT;
429 button = !goog.events.BrowserFeature.HAS_W3C_BUTTON ?
430 goog.events.BrowserEvent.IE_BUTTON_MAP[button] :
431 button;
432 return goog.testing.events.fireMouseButtonEvent_(
433 goog.events.EventType.MOUSEDOWN, target, button, opt_coords,
434 opt_eventProperties);
435 };
436
437
438 /**
439 * Simulates a mouseup event on the given target.
440 * @param {EventTarget} target The target for the event.
441 * @param {goog.events.BrowserEvent.MouseButton=} opt_button Mouse button;
442 * defaults to {@code goog.events.BrowserEvent.MouseButton.LEFT}.
443 * @param {goog.math.Coordinate=} opt_coords Mouse position. Defaults to event's
444 * target's position (if available), otherwise (0, 0).
445 * @param {Object=} opt_eventProperties Event properties to be mixed into the
446 * BrowserEvent.
447 * @return {boolean} The returnValue of the event: false if preventDefault() was
448 * called on it, true otherwise.
449 */
450 goog.testing.events.fireMouseUpEvent = function(
451 target, opt_button, opt_coords, opt_eventProperties) {
452 var button = opt_button || goog.events.BrowserEvent.MouseButton.LEFT;
453 button = !goog.events.BrowserFeature.HAS_W3C_BUTTON ?
454 goog.events.BrowserEvent.IE_BUTTON_MAP[button] :
455 button;
456 return goog.testing.events.fireMouseButtonEvent_(
457 goog.events.EventType.MOUSEUP, target, button, opt_coords,
458 opt_eventProperties);
459 };
460
461
462 /**
463 * Simulates a click event on the given target. IE only supports click with
464 * the left mouse button.
465 * @param {EventTarget} target The target for the event.
466 * @param {goog.events.BrowserEvent.MouseButton=} opt_button Mouse button;
467 * defaults to {@code goog.events.BrowserEvent.MouseButton.LEFT}.
468 * @param {goog.math.Coordinate=} opt_coords Mouse position. Defaults to event's
469 * target's position (if available), otherwise (0, 0).
470 * @param {Object=} opt_eventProperties Event properties to be mixed into the
471 * BrowserEvent.
472 * @return {boolean} The returnValue of the event: false if preventDefault() was
473 * called on it, true otherwise.
474 */
475 goog.testing.events.fireClickEvent = function(
476 target, opt_button, opt_coords, opt_eventProperties) {
477 return goog.testing.events.fireMouseButtonEvent_(
478 goog.events.EventType.CLICK, target, opt_button, opt_coords,
479 opt_eventProperties);
480 };
481
482
483 /**
484 * Simulates a double-click event on the given target. Always double-clicks
485 * with the left mouse button since no browser supports double-clicking with
486 * any other buttons.
487 * @param {EventTarget} target The target for the event.
488 * @param {goog.math.Coordinate=} opt_coords Mouse position. Defaults to event's
489 * target's position (if available), otherwise (0, 0).
490 * @param {Object=} opt_eventProperties Event properties to be mixed into the
491 * BrowserEvent.
492 * @return {boolean} The returnValue of the event: false if preventDefault() was
493 * called on it, true otherwise.
494 */
495 goog.testing.events.fireDoubleClickEvent = function(
496 target, opt_coords, opt_eventProperties) {
497 return goog.testing.events.fireMouseButtonEvent_(
498 goog.events.EventType.DBLCLICK, target,
499 goog.events.BrowserEvent.MouseButton.LEFT, opt_coords,
500 opt_eventProperties);
501 };
502
503
504 /**
505 * Helper function to fire a mouse event.
506 * with the left mouse button since no browser supports double-clicking with
507 * any other buttons.
508 * @param {string} type The event type.
509 * @param {EventTarget} target The target for the event.
510 * @param {number=} opt_button Mouse button; defaults to
511 * {@code goog.events.BrowserEvent.MouseButton.LEFT}.
512 * @param {goog.math.Coordinate=} opt_coords Mouse position. Defaults to event's
513 * target's position (if available), otherwise (0, 0).
514 * @param {Object=} opt_eventProperties Event properties to be mixed into the
515 * BrowserEvent.
516 * @return {boolean} The returnValue of the event: false if preventDefault() was
517 * called on it, true otherwise.
518 * @private
519 */
520 goog.testing.events.fireMouseButtonEvent_ = function(
521 type, target, opt_button, opt_coords, opt_eventProperties) {
522 var e = new goog.testing.events.Event(type, target);
523 e.button = opt_button || goog.events.BrowserEvent.MouseButton.LEFT;
524 goog.testing.events.setEventClientXY_(e, opt_coords);
525 if (opt_eventProperties) {
526 goog.object.extend(e, opt_eventProperties);
527 }
528 return goog.testing.events.fireBrowserEvent(e);
529 };
530
531
532 /**
533 * Simulates a contextmenu event on the given target.
534 * @param {EventTarget} target The target for the event.
535 * @param {goog.math.Coordinate=} opt_coords Mouse position. Defaults to event's
536 * target's position (if available), otherwise (0, 0).
537 * @return {boolean} The returnValue of the event: false if preventDefault() was
538 * called on it, true otherwise.
539 */
540 goog.testing.events.fireContextMenuEvent = function(target, opt_coords) {
541 var button = (goog.userAgent.MAC && goog.userAgent.WEBKIT) ?
542 goog.events.BrowserEvent.MouseButton.LEFT :
543 goog.events.BrowserEvent.MouseButton.RIGHT;
544 var contextmenu =
545 new goog.testing.events.Event(goog.events.EventType.CONTEXTMENU, target);
546 contextmenu.button = !goog.events.BrowserFeature.HAS_W3C_BUTTON ?
547 goog.events.BrowserEvent.IE_BUTTON_MAP[button] :
548 button;
549 contextmenu.ctrlKey = goog.userAgent.MAC;
550 goog.testing.events.setEventClientXY_(contextmenu, opt_coords);
551 return goog.testing.events.fireBrowserEvent(contextmenu);
552 };
553
554
555 /**
556 * Simulates a mousedown, contextmenu, and the mouseup on the given event
557 * target, with the right mouse button.
558 * @param {EventTarget} target The target for the event.
559 * @param {goog.math.Coordinate=} opt_coords Mouse position. Defaults to event's
560 * target's position (if available), otherwise (0, 0).
561 * @return {boolean} The returnValue of the sequence: false if preventDefault()
562 * was called on any of the events, true otherwise.
563 */
564 goog.testing.events.fireContextMenuSequence = function(target, opt_coords) {
565 var props = goog.userAgent.MAC ? {ctrlKey: true} : {};
566 var button = (goog.userAgent.MAC && goog.userAgent.WEBKIT) ?
567 goog.events.BrowserEvent.MouseButton.LEFT :
568 goog.events.BrowserEvent.MouseButton.RIGHT;
569
570 var result =
571 goog.testing.events.fireMouseDownEvent(target, button, opt_coords, props);
572 if (goog.userAgent.WINDOWS) {
573 // All browsers are consistent on Windows.
574 result &= goog.testing.events.fireMouseUpEvent(target, button, opt_coords) &
575 goog.testing.events.fireContextMenuEvent(target, opt_coords);
576 } else {
577 result &= goog.testing.events.fireContextMenuEvent(target, opt_coords);
578
579 // GECKO on Mac and Linux always fires the mouseup after the contextmenu.
580
581 // WEBKIT is really weird.
582 //
583 // On Linux, it sometimes fires mouseup, but most of the time doesn't.
584 // It's really hard to reproduce consistently. I think there's some
585 // internal race condition. If contextmenu is preventDefaulted, then
586 // mouseup always fires.
587 //
588 // On Mac, it always fires mouseup and then fires a click.
589 result &=
590 goog.testing.events.fireMouseUpEvent(target, button, opt_coords, props);
591
592 if (goog.userAgent.WEBKIT && goog.userAgent.MAC) {
593 result &=
594 goog.testing.events.fireClickEvent(target, button, opt_coords, props);
595 }
596 }
597 return !!result;
598 };
599
600
601 /**
602 * Simulates a popstate event on the given target.
603 * @param {EventTarget} target The target for the event.
604 * @param {Object} state History state object.
605 * @return {boolean} The returnValue of the event: false if preventDefault() was
606 * called on it, true otherwise.
607 */
608 goog.testing.events.firePopStateEvent = function(target, state) {
609 var e = new goog.testing.events.Event(goog.events.EventType.POPSTATE, target);
610 e.state = state;
611 return goog.testing.events.fireBrowserEvent(e);
612 };
613
614
615 /**
616 * Simulate a blur event on the given target.
617 * @param {EventTarget} target The target for the event.
618 * @return {boolean} The value returned by firing the blur browser event,
619 * which returns false iff 'preventDefault' was invoked.
620 */
621 goog.testing.events.fireBlurEvent = function(target) {
622 var e = new goog.testing.events.Event(goog.events.EventType.BLUR, target);
623 return goog.testing.events.fireBrowserEvent(e);
624 };
625
626
627 /**
628 * Simulate a focus event on the given target.
629 * @param {EventTarget} target The target for the event.
630 * @return {boolean} The value returned by firing the focus browser event,
631 * which returns false iff 'preventDefault' was invoked.
632 */
633 goog.testing.events.fireFocusEvent = function(target) {
634 var e = new goog.testing.events.Event(goog.events.EventType.FOCUS, target);
635 return goog.testing.events.fireBrowserEvent(e);
636 };
637
638
639 /**
640 * Simulate a focus-in event on the given target.
641 * @param {!EventTarget} target The target for the event.
642 * @return {boolean} The value returned by firing the focus-in browser event,
643 * which returns false iff 'preventDefault' was invoked.
644 */
645 goog.testing.events.fireFocusInEvent = function(target) {
646 var e = new goog.testing.events.Event(goog.events.EventType.FOCUSIN, target);
647 return goog.testing.events.fireBrowserEvent(e);
648 };
649
650
651 /**
652 * Simulates an event's capturing and bubbling phases.
653 * @param {Event} event A simulated native event. It will be wrapped in a
654 * normalized BrowserEvent and dispatched to Closure listeners on all
655 * ancestors of its target (inclusive).
656 * @return {boolean} The returnValue of the event: false if preventDefault() was
657 * called on it, true otherwise.
658 */
659 goog.testing.events.fireBrowserEvent = function(event) {
660 event.returnValue_ = true;
661
662 // generate a list of ancestors
663 var ancestors = [];
664 for (var current = event.target; current; current = current.parentNode) {
665 ancestors.push(current);
666 }
667
668 // dispatch capturing listeners
669 for (var j = ancestors.length - 1; j >= 0 && !event.propagationStopped_;
670 j--) {
671 goog.events.fireListeners(
672 ancestors[j], event.type, true,
673 new goog.events.BrowserEvent(event, ancestors[j]));
674 }
675
676 // dispatch bubbling listeners
677 for (var j = 0; j < ancestors.length && !event.propagationStopped_; j++) {
678 goog.events.fireListeners(
679 ancestors[j], event.type, false,
680 new goog.events.BrowserEvent(event, ancestors[j]));
681 }
682
683 return event.returnValue_;
684 };
685
686
687 /**
688 * Simulates a touchstart event on the given target.
689 * @param {EventTarget} target The target for the event.
690 * @param {goog.math.Coordinate=} opt_coords Touch position. Defaults to event's
691 * target's position (if available), otherwise (0, 0).
692 * @param {Object=} opt_eventProperties Event properties to be mixed into the
693 * BrowserEvent.
694 * @return {boolean} The returnValue of the event: false if preventDefault() was
695 * called on it, true otherwise.
696 */
697 goog.testing.events.fireTouchStartEvent = function(
698 target, opt_coords, opt_eventProperties) {
699 // TODO: Support multi-touch events with array of coordinates.
700 var touchstart =
701 new goog.testing.events.Event(goog.events.EventType.TOUCHSTART, target);
702 goog.testing.events.setEventClientXY_(touchstart, opt_coords);
703 if (opt_eventProperties) {
704 goog.object.extend(touchstart, opt_eventProperties);
705 }
706 return goog.testing.events.fireBrowserEvent(touchstart);
707 };
708
709
710 /**
711 * Simulates a touchmove event on the given target.
712 * @param {EventTarget} target The target for the event.
713 * @param {goog.math.Coordinate=} opt_coords Touch position. Defaults to event's
714 * target's position (if available), otherwise (0, 0).
715 * @param {Object=} opt_eventProperties Event properties to be mixed into the
716 * BrowserEvent.
717 * @return {boolean} The returnValue of the event: false if preventDefault() was
718 * called on it, true otherwise.
719 */
720 goog.testing.events.fireTouchMoveEvent = function(
721 target, opt_coords, opt_eventProperties) {
722 // TODO: Support multi-touch events with array of coordinates.
723 var touchmove =
724 new goog.testing.events.Event(goog.events.EventType.TOUCHMOVE, target);
725 goog.testing.events.setEventClientXY_(touchmove, opt_coords);
726 if (opt_eventProperties) {
727 goog.object.extend(touchmove, opt_eventProperties);
728 }
729 return goog.testing.events.fireBrowserEvent(touchmove);
730 };
731
732
733 /**
734 * Simulates a touchend event on the given target.
735 * @param {EventTarget} target The target for the event.
736 * @param {goog.math.Coordinate=} opt_coords Touch position. Defaults to event's
737 * target's position (if available), otherwise (0, 0).
738 * @param {Object=} opt_eventProperties Event properties to be mixed into the
739 * BrowserEvent.
740 * @return {boolean} The returnValue of the event: false if preventDefault() was
741 * called on it, true otherwise.
742 */
743 goog.testing.events.fireTouchEndEvent = function(
744 target, opt_coords, opt_eventProperties) {
745 // TODO: Support multi-touch events with array of coordinates.
746 var touchend =
747 new goog.testing.events.Event(goog.events.EventType.TOUCHEND, target);
748 goog.testing.events.setEventClientXY_(touchend, opt_coords);
749 if (opt_eventProperties) {
750 goog.object.extend(touchend, opt_eventProperties);
751 }
752 return goog.testing.events.fireBrowserEvent(touchend);
753 };
754
755
756 /**
757 * Simulates a simple touch sequence on the given target.
758 * @param {EventTarget} target The target for the event.
759 * @param {goog.math.Coordinate=} opt_coords Touch position. Defaults to event
760 * target's position (if available), otherwise (0, 0).
761 * @param {Object=} opt_eventProperties Event properties to be mixed into the
762 * BrowserEvent.
763 * @return {boolean} The returnValue of the sequence: false if preventDefault()
764 * was called on any of the events, true otherwise.
765 */
766 goog.testing.events.fireTouchSequence = function(
767 target, opt_coords, opt_eventProperties) {
768 // TODO: Support multi-touch events with array of coordinates.
769 // Fire touchstart, touchmove, touchend then return the bitwise AND of the 3.
770 return !!(
771 goog.testing.events.fireTouchStartEvent(
772 target, opt_coords, opt_eventProperties) &
773 goog.testing.events.fireTouchEndEvent(
774 target, opt_coords, opt_eventProperties));
775 };
776
777
778 /**
779 * Mixins a listenable into the given object. This turns the object
780 * into a goog.events.Listenable. This is useful, for example, when
781 * you need to mock a implementation of listenable and still want it
782 * to work with goog.events.
783 * @param {!Object} obj The object to mixin into.
784 */
785 goog.testing.events.mixinListenable = function(obj) {
786 var listenable = new goog.events.EventTarget();
787
788 listenable.setTargetForTesting(obj);
789
790 var listenablePrototype = goog.events.EventTarget.prototype;
791 var disposablePrototype = goog.Disposable.prototype;
792 for (var key in listenablePrototype) {
793 if (listenablePrototype.hasOwnProperty(key) ||
794 disposablePrototype.hasOwnProperty(key)) {
795 var member = listenablePrototype[key];
796 if (goog.isFunction(member)) {
797 obj[key] = goog.bind(member, listenable);
798 } else {
799 obj[key] = member;
800 }
801 }
802 }
803 };