| 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 |
goog.provide('goog.testing.eventsTest'); |
| 16 |
goog.setTestOnly('goog.testing.eventsTest'); |
| 17 |
|
| 18 |
goog.require('goog.array'); |
| 19 |
goog.require('goog.dom'); |
| 20 |
goog.require('goog.dom.InputType'); |
| 21 |
goog.require('goog.dom.TagName'); |
| 22 |
goog.require('goog.events'); |
| 23 |
goog.require('goog.events.EventType'); |
| 24 |
goog.require('goog.events.KeyCodes'); |
| 25 |
goog.require('goog.math.Coordinate'); |
| 26 |
goog.require('goog.string'); |
| 27 |
goog.require('goog.style'); |
| 28 |
goog.require('goog.testing.PropertyReplacer'); |
| 29 |
goog.require('goog.testing.events'); |
| 30 |
goog.require('goog.testing.jsunit'); |
| 31 |
goog.require('goog.testing.recordFunction'); |
| 32 |
goog.require('goog.userAgent'); |
| 33 |
|
| 34 |
var firedEventTypes; |
| 35 |
var firedEventCoordinates; |
| 36 |
var firedScreenCoordinates; |
| 37 |
var firedShiftKeys; |
| 38 |
var firedKeyCodes; |
| 39 |
var root; |
| 40 |
var log; |
| 41 |
var input; |
| 42 |
var testButton; |
| 43 |
var parentEl; |
| 44 |
var childEl; |
| 45 |
var coordinate = new goog.math.Coordinate(123, 456); |
| 46 |
var stubs = new goog.testing.PropertyReplacer(); |
| 47 |
var eventCount; |
| 48 |
|
| 49 |
function setUpPage() { |
| 50 |
root = goog.dom.getElement('root'); |
| 51 |
log = goog.dom.getElement('log'); |
| 52 |
input = goog.dom.getElement('input'); |
| 53 |
testButton = goog.dom.getElement('testButton'); |
| 54 |
parentEl = goog.dom.getElement('parentEl'); |
| 55 |
childEl = goog.dom.getElement('childEl'); |
| 56 |
} |
| 57 |
|
| 58 |
function setUp() { |
| 59 |
stubs.reset(); |
| 60 |
goog.events.removeAll(root); |
| 61 |
goog.events.removeAll(log); |
| 62 |
goog.events.removeAll(input); |
| 63 |
goog.events.removeAll(testButton); |
| 64 |
goog.events.removeAll(parentEl); |
| 65 |
goog.events.removeAll(childEl); |
| 66 |
|
| 67 |
goog.dom.removeChildren(root); |
| 68 |
firedEventTypes = []; |
| 69 |
firedEventCoordinates = []; |
| 70 |
firedScreenCoordinates = []; |
| 71 |
firedShiftKeys = []; |
| 72 |
firedKeyCodes = []; |
| 73 |
|
| 74 |
for (var key in goog.events.EventType) { |
| 75 |
goog.events.listen(root, goog.events.EventType[key], function(e) { |
| 76 |
firedEventTypes.push(e.type); |
| 77 |
var coord = new goog.math.Coordinate(e.clientX, e.clientY); |
| 78 |
firedEventCoordinates.push(coord); |
| 79 |
|
| 80 |
firedScreenCoordinates.push( |
| 81 |
new goog.math.Coordinate(e.screenX, e.screenY)); |
| 82 |
|
| 83 |
firedShiftKeys.push(!!e.shiftKey); |
| 84 |
firedKeyCodes.push(e.keyCode); |
| 85 |
}); |
| 86 |
} |
| 87 |
|
| 88 |
eventCount = |
| 89 |
{parentBubble: 0, parentCapture: 0, childCapture: 0, childBubble: 0}; |
| 90 |
// Event listeners for the capture/bubble test. |
| 91 |
goog.events.listen(parentEl, goog.events.EventType.CLICK, function(e) { |
| 92 |
eventCount.parentCapture++; |
| 93 |
assertEquals(parentEl, e.currentTarget); |
| 94 |
assertEquals(childEl, e.target); |
| 95 |
}, true); |
| 96 |
goog.events.listen(childEl, goog.events.EventType.CLICK, function(e) { |
| 97 |
eventCount.childCapture++; |
| 98 |
assertEquals(childEl, e.currentTarget); |
| 99 |
assertEquals(childEl, e.target); |
| 100 |
}, true); |
| 101 |
goog.events.listen(childEl, goog.events.EventType.CLICK, function(e) { |
| 102 |
eventCount.childBubble++; |
| 103 |
assertEquals(childEl, e.currentTarget); |
| 104 |
assertEquals(childEl, e.target); |
| 105 |
}); |
| 106 |
goog.events.listen(parentEl, goog.events.EventType.CLICK, function(e) { |
| 107 |
eventCount.parentBubble++; |
| 108 |
assertEquals(parentEl, e.currentTarget); |
| 109 |
assertEquals(childEl, e.target); |
| 110 |
}); |
| 111 |
} |
| 112 |
|
| 113 |
function tearDownPage() { |
| 114 |
for (var key in goog.events.EventType) { |
| 115 |
var type = goog.events.EventType[key]; |
| 116 |
if (type == 'mousemove' || type == 'mouseout' || type == 'mouseover') { |
| 117 |
continue; |
| 118 |
} |
| 119 |
goog.dom.appendChild( |
| 120 |
input, goog.dom.createDom( |
| 121 |
goog.dom.TagName.LABEL, null, |
| 122 |
goog.dom.createDom( |
| 123 |
goog.dom.TagName.INPUT, |
| 124 |
{'id': type, 'type': goog.dom.InputType.CHECKBOX}), |
| 125 |
type, goog.dom.createDom(goog.dom.TagName.BR))); |
| 126 |
goog.events.listen(testButton, type, function(e) { |
| 127 |
if (goog.dom.getElement(e.type).checked) { |
| 128 |
e.preventDefault(); |
| 129 |
} |
| 130 |
|
| 131 |
log.innerHTML += |
| 132 |
goog.string.subs('<br />%s (%s, %s)', e.type, e.clientX, e.clientY); |
| 133 |
}); |
| 134 |
} |
| 135 |
} |
| 136 |
|
| 137 |
function testMouseEnter() { |
| 138 |
goog.testing.events.fireMouseEnterEvent(root, null); |
| 139 |
goog.testing.events.fireMouseEnterEvent(root, null, coordinate); |
| 140 |
assertEventTypes(['mouseenter', 'mouseenter']); |
| 141 |
assertCoordinates([goog.style.getClientPosition(root), coordinate]); |
| 142 |
} |
| 143 |
|
| 144 |
function testMouseLeave() { |
| 145 |
goog.testing.events.fireMouseLeaveEvent(root, null); |
| 146 |
goog.testing.events.fireMouseLeaveEvent(root, null, coordinate); |
| 147 |
assertEventTypes(['mouseleave', 'mouseleave']); |
| 148 |
assertCoordinates([goog.style.getClientPosition(root), coordinate]); |
| 149 |
} |
| 150 |
|
| 151 |
function testMouseOver() { |
| 152 |
goog.testing.events.fireMouseOverEvent(root, null); |
| 153 |
goog.testing.events.fireMouseOverEvent(root, null, coordinate); |
| 154 |
assertEventTypes(['mouseover', 'mouseover']); |
| 155 |
assertCoordinates([goog.style.getClientPosition(root), coordinate]); |
| 156 |
} |
| 157 |
|
| 158 |
function testMouseOut() { |
| 159 |
goog.testing.events.fireMouseOutEvent(root, null); |
| 160 |
goog.testing.events.fireMouseOutEvent(root, null, coordinate); |
| 161 |
assertEventTypes(['mouseout', 'mouseout']); |
| 162 |
assertCoordinates([goog.style.getClientPosition(root), coordinate]); |
| 163 |
} |
| 164 |
|
| 165 |
function testFocus() { |
| 166 |
goog.testing.events.fireFocusEvent(root); |
| 167 |
assertEventTypes(['focus']); |
| 168 |
} |
| 169 |
|
| 170 |
function testFocusIn() { |
| 171 |
goog.testing.events.fireFocusInEvent(root); |
| 172 |
assertEventTypes([goog.events.EventType.FOCUSIN]); |
| 173 |
} |
| 174 |
|
| 175 |
function testBlur() { |
| 176 |
goog.testing.events.fireBlurEvent(root); |
| 177 |
assertEventTypes(['blur']); |
| 178 |
} |
| 179 |
|
| 180 |
function testClickSequence() { |
| 181 |
assertTrue(goog.testing.events.fireClickSequence(root)); |
| 182 |
assertEventTypes(['mousedown', 'mouseup', 'click']); |
| 183 |
var rootPosition = goog.style.getClientPosition(root); |
| 184 |
assertCoordinates([rootPosition, rootPosition, rootPosition]); |
| 185 |
} |
| 186 |
|
| 187 |
function testClickSequenceWithCoordinate() { |
| 188 |
assertTrue(goog.testing.events.fireClickSequence(root, null, coordinate)); |
| 189 |
assertCoordinates([coordinate, coordinate, coordinate]); |
| 190 |
assertArrayEquals([false, false, false], firedShiftKeys); |
| 191 |
} |
| 192 |
|
| 193 |
function testTouchStart() { |
| 194 |
goog.testing.events.fireTouchStartEvent(root); |
| 195 |
goog.testing.events.fireTouchStartEvent(root, coordinate); |
| 196 |
assertEventTypes(['touchstart', 'touchstart']); |
| 197 |
assertCoordinates([goog.style.getClientPosition(root), coordinate]); |
| 198 |
} |
| 199 |
|
| 200 |
function testTouchMove() { |
| 201 |
goog.testing.events.fireTouchMoveEvent(root); |
| 202 |
goog.testing.events.fireTouchMoveEvent(root, coordinate, {touches: []}); |
| 203 |
assertEventTypes(['touchmove', 'touchmove']); |
| 204 |
assertCoordinates([goog.style.getClientPosition(root), coordinate]); |
| 205 |
} |
| 206 |
|
| 207 |
function testTouchEnd() { |
| 208 |
goog.testing.events.fireTouchEndEvent(root); |
| 209 |
goog.testing.events.fireTouchEndEvent(root, coordinate); |
| 210 |
assertEventTypes(['touchend', 'touchend']); |
| 211 |
assertCoordinates([goog.style.getClientPosition(root), coordinate]); |
| 212 |
} |
| 213 |
|
| 214 |
function testTouchSequence() { |
| 215 |
assertTrue(goog.testing.events.fireTouchSequence(root)); |
| 216 |
assertEventTypes(['touchstart', 'touchend']); |
| 217 |
var rootPosition = goog.style.getClientPosition(root); |
| 218 |
assertCoordinates([rootPosition, rootPosition]); |
| 219 |
} |
| 220 |
|
| 221 |
function testTouchSequenceWithCoordinate() { |
| 222 |
assertTrue(goog.testing.events.fireTouchSequence(root, coordinate)); |
| 223 |
assertCoordinates([coordinate, coordinate]); |
| 224 |
} |
| 225 |
|
| 226 |
function testClickSequenceWithEventProperty() { |
| 227 |
assertTrue( |
| 228 |
goog.testing.events.fireClickSequence( |
| 229 |
root, null, undefined, {shiftKey: true})); |
| 230 |
assertArrayEquals([true, true, true], firedShiftKeys); |
| 231 |
} |
| 232 |
|
| 233 |
function testClickSequenceCancellingMousedown() { |
| 234 |
preventDefaultEventType('mousedown'); |
| 235 |
assertFalse(goog.testing.events.fireClickSequence(root)); |
| 236 |
assertEventTypes(['mousedown', 'mouseup', 'click']); |
| 237 |
} |
| 238 |
|
| 239 |
function testClickSequenceCancellingMousedownWithCoordinate() { |
| 240 |
preventDefaultEventType('mousedown'); |
| 241 |
assertFalse(goog.testing.events.fireClickSequence(root, null, coordinate)); |
| 242 |
assertCoordinates([coordinate, coordinate, coordinate]); |
| 243 |
} |
| 244 |
|
| 245 |
function testClickSequenceCancellingMouseup() { |
| 246 |
preventDefaultEventType('mouseup'); |
| 247 |
assertFalse(goog.testing.events.fireClickSequence(root)); |
| 248 |
assertEventTypes(['mousedown', 'mouseup', 'click']); |
| 249 |
} |
| 250 |
|
| 251 |
function testClickSequenceCancellingMouseupWithCoordinate() { |
| 252 |
preventDefaultEventType('mouseup'); |
| 253 |
assertFalse(goog.testing.events.fireClickSequence(root, null, coordinate)); |
| 254 |
assertCoordinates([coordinate, coordinate, coordinate]); |
| 255 |
} |
| 256 |
|
| 257 |
function testClickSequenceCancellingClick() { |
| 258 |
preventDefaultEventType('click'); |
| 259 |
assertFalse(goog.testing.events.fireClickSequence(root)); |
| 260 |
assertEventTypes(['mousedown', 'mouseup', 'click']); |
| 261 |
} |
| 262 |
|
| 263 |
function testClickSequenceCancellingClickWithCoordinate() { |
| 264 |
preventDefaultEventType('click'); |
| 265 |
assertFalse(goog.testing.events.fireClickSequence(root, null, coordinate)); |
| 266 |
assertCoordinates([coordinate, coordinate, coordinate]); |
| 267 |
} |
| 268 |
|
| 269 |
// For a double click, IE fires selectstart instead of the second mousedown, |
| 270 |
// but we don't simulate selectstart. Also, IE doesn't fire the second click. |
| 271 |
var DBLCLICK_SEQ = |
| 272 |
(goog.userAgent.IE ? |
| 273 |
['mousedown', 'mouseup', 'click', 'mouseup', 'dblclick'] : |
| 274 |
[ |
| 275 |
'mousedown', 'mouseup', 'click', 'mousedown', 'mouseup', 'click', |
| 276 |
'dblclick' |
| 277 |
]); |
| 278 |
|
| 279 |
|
| 280 |
var DBLCLICK_SEQ_COORDS = goog.array.repeat(coordinate, DBLCLICK_SEQ.length); |
| 281 |
|
| 282 |
function testDoubleClickSequence() { |
| 283 |
assertTrue(goog.testing.events.fireDoubleClickSequence(root)); |
| 284 |
assertEventTypes(DBLCLICK_SEQ); |
| 285 |
} |
| 286 |
|
| 287 |
function testDoubleClickSequenceWithCoordinate() { |
| 288 |
assertTrue(goog.testing.events.fireDoubleClickSequence(root, coordinate)); |
| 289 |
assertCoordinates(DBLCLICK_SEQ_COORDS); |
| 290 |
} |
| 291 |
|
| 292 |
function testDoubleClickSequenceCancellingMousedown() { |
| 293 |
preventDefaultEventType('mousedown'); |
| 294 |
assertFalse(goog.testing.events.fireDoubleClickSequence(root)); |
| 295 |
assertEventTypes(DBLCLICK_SEQ); |
| 296 |
} |
| 297 |
|
| 298 |
function testDoubleClickSequenceCancellingMousedownWithCoordinate() { |
| 299 |
preventDefaultEventType('mousedown'); |
| 300 |
assertFalse(goog.testing.events.fireDoubleClickSequence(root, coordinate)); |
| 301 |
assertCoordinates(DBLCLICK_SEQ_COORDS); |
| 302 |
} |
| 303 |
|
| 304 |
function testDoubleClickSequenceCancellingMouseup() { |
| 305 |
preventDefaultEventType('mouseup'); |
| 306 |
assertFalse(goog.testing.events.fireDoubleClickSequence(root)); |
| 307 |
assertEventTypes(DBLCLICK_SEQ); |
| 308 |
} |
| 309 |
|
| 310 |
function testDoubleClickSequenceCancellingMouseupWithCoordinate() { |
| 311 |
preventDefaultEventType('mouseup'); |
| 312 |
assertFalse(goog.testing.events.fireDoubleClickSequence(root, coordinate)); |
| 313 |
assertCoordinates(DBLCLICK_SEQ_COORDS); |
| 314 |
} |
| 315 |
|
| 316 |
function testDoubleClickSequenceCancellingClick() { |
| 317 |
preventDefaultEventType('click'); |
| 318 |
assertFalse(goog.testing.events.fireDoubleClickSequence(root)); |
| 319 |
assertEventTypes(DBLCLICK_SEQ); |
| 320 |
} |
| 321 |
|
| 322 |
function testDoubleClickSequenceCancellingClickWithCoordinate() { |
| 323 |
preventDefaultEventType('click'); |
| 324 |
assertFalse(goog.testing.events.fireDoubleClickSequence(root, coordinate)); |
| 325 |
assertCoordinates(DBLCLICK_SEQ_COORDS); |
| 326 |
} |
| 327 |
|
| 328 |
function testDoubleClickSequenceCancellingDoubleClick() { |
| 329 |
preventDefaultEventType('dblclick'); |
| 330 |
assertFalse(goog.testing.events.fireDoubleClickSequence(root)); |
| 331 |
assertEventTypes(DBLCLICK_SEQ); |
| 332 |
} |
| 333 |
|
| 334 |
function testDoubleClickSequenceCancellingDoubleClickWithCoordinate() { |
| 335 |
preventDefaultEventType('dblclick'); |
| 336 |
assertFalse(goog.testing.events.fireDoubleClickSequence(root, coordinate)); |
| 337 |
assertCoordinates(DBLCLICK_SEQ_COORDS); |
| 338 |
} |
| 339 |
|
| 340 |
function testKeySequence() { |
| 341 |
assertTrue( |
| 342 |
goog.testing.events.fireKeySequence(root, goog.events.KeyCodes.ZERO)); |
| 343 |
assertEventTypes(['keydown', 'keypress', 'keyup']); |
| 344 |
} |
| 345 |
|
| 346 |
function testKeySequenceCancellingKeydown() { |
| 347 |
preventDefaultEventType('keydown'); |
| 348 |
assertFalse( |
| 349 |
goog.testing.events.fireKeySequence(root, goog.events.KeyCodes.ZERO)); |
| 350 |
assertEventTypes(['keydown', 'keyup']); |
| 351 |
} |
| 352 |
|
| 353 |
function testKeySequenceCancellingKeypress() { |
| 354 |
preventDefaultEventType('keypress'); |
| 355 |
assertFalse( |
| 356 |
goog.testing.events.fireKeySequence(root, goog.events.KeyCodes.ZERO)); |
| 357 |
assertEventTypes(['keydown', 'keypress', 'keyup']); |
| 358 |
} |
| 359 |
|
| 360 |
function testKeySequenceCancellingKeyup() { |
| 361 |
preventDefaultEventType('keyup'); |
| 362 |
assertFalse( |
| 363 |
goog.testing.events.fireKeySequence(root, goog.events.KeyCodes.ZERO)); |
| 364 |
assertEventTypes(['keydown', 'keypress', 'keyup']); |
| 365 |
} |
| 366 |
|
| 367 |
function testKeySequenceWithEscapeKey() { |
| 368 |
assertTrue( |
| 369 |
goog.testing.events.fireKeySequence(root, goog.events.KeyCodes.ESC)); |
| 370 |
if (goog.userAgent.EDGE || |
| 371 |
(goog.userAgent.WEBKIT && goog.userAgent.isVersionOrHigher('525'))) { |
| 372 |
assertEventTypes(['keydown', 'keyup']); |
| 373 |
} else { |
| 374 |
assertEventTypes(['keydown', 'keypress', 'keyup']); |
| 375 |
} |
| 376 |
} |
| 377 |
|
| 378 |
function testKeySequenceForMacActionKeysNegative() { |
| 379 |
stubs.set(goog.userAgent, 'GECKO', false); |
| 380 |
goog.testing.events.fireKeySequence( |
| 381 |
root, goog.events.KeyCodes.C, {'metaKey': true}); |
| 382 |
assertEventTypes(['keydown', 'keypress', 'keyup']); |
| 383 |
} |
| 384 |
|
| 385 |
function testKeySequenceForMacActionKeysPositive() { |
| 386 |
stubs.set(goog.userAgent, 'GECKO', true); |
| 387 |
stubs.set(goog.userAgent, 'MAC', true); |
| 388 |
goog.testing.events.fireKeySequence( |
| 389 |
root, goog.events.KeyCodes.C, {'metaKey': true}); |
| 390 |
assertEventTypes(['keypress', 'keyup']); |
| 391 |
} |
| 392 |
|
| 393 |
function testKeySequenceForOptionKeysOnMac() { |
| 394 |
// Mac uses an option (or alt) key to type non-ASCII characters. This test |
| 395 |
// verifies we can emulate key events sent when typing such non-ASCII |
| 396 |
// characters. |
| 397 |
stubs.set(goog.userAgent, 'WEBKIT', true); |
| 398 |
stubs.set(goog.userAgent, 'MAC', true); |
| 399 |
|
| 400 |
var optionKeyCodes = [ |
| 401 |
[0xc0, 0x00e6], // option+' |
| 402 |
[0xbc, 0x2264], // option+, |
| 403 |
[0xbd, 0x2013], // option+- |
| 404 |
[0xbe, 0x2265], // option+. |
| 405 |
[0xbf, 0x00f7], // option+/ |
| 406 |
[0x30, 0x00ba], // option+0 |
| 407 |
[0x31, 0x00a1], // option+1 |
| 408 |
[0x32, 0x2122], // option+2 |
| 409 |
[0x33, 0x00a3], // option+3 |
| 410 |
[0x34, 0x00a2], // option+4 |
| 411 |
[0x35, 0x221e], // option+5 |
| 412 |
[0x36, 0x00a7], // option+6 |
| 413 |
[0x37, 0x00b6], // option+7 |
| 414 |
[0x38, 0x2022], // option+8 |
| 415 |
[0x39, 0x00aa], // option+9 |
| 416 |
[0xba, 0x2026], // option+; |
| 417 |
[0xbb, 0x2260], // option+= |
| 418 |
[0xdb, 0x201c], // option+[ |
| 419 |
[ |
| 420 |
0xdc, 0x00ab |
| 421 |
], // option+\ |
| 422 |
[0xdd, 0x2018], // option+] |
| 423 |
[0x41, 0x00e5], // option+a |
| 424 |
[0x42, 0x222b], // option+b |
| 425 |
[0x43, 0x00e7], // option+c |
| 426 |
[0x44, 0x2202], // option+d |
| 427 |
[0x45, 0x00b4], // option+e |
| 428 |
[0x46, 0x0192], // option+f |
| 429 |
[0x47, 0x00a9], // option+g |
| 430 |
[0x48, 0x02d9], // option+h |
| 431 |
[0x49, 0x02c6], // option+i |
| 432 |
[0x4a, 0x2206], // option+j |
| 433 |
[0x4b, 0x02da], // option+k |
| 434 |
[0x4c, 0x00ac], // option+l |
| 435 |
[0x4d, 0x00b5], // option+m |
| 436 |
[0x4e, 0x02dc], // option+n |
| 437 |
[0x4f, 0x00f8], // option+o |
| 438 |
[0x50, 0x03c0], // option+p |
| 439 |
[0x51, 0x0153], // option+q |
| 440 |
[0x52, 0x00ae], // option+r |
| 441 |
[0x53, 0x00df], // option+s |
| 442 |
[0x54, 0x2020], // option+t |
| 443 |
[0x56, 0x221a], // option+v |
| 444 |
[0x57, 0x2211], // option+w |
| 445 |
[0x58, 0x2248], // option+x |
| 446 |
[0x59, 0x00a5], // option+y |
| 447 |
[0x5a, 0x03a9] // option+z |
| 448 |
]; |
| 449 |
|
| 450 |
for (var i = 0; i < optionKeyCodes.length; ++i) { |
| 451 |
firedEventTypes = []; |
| 452 |
firedKeyCodes = []; |
| 453 |
var keyCode = optionKeyCodes[i][0]; |
| 454 |
var keyPressKeyCode = optionKeyCodes[i][1]; |
| 455 |
goog.testing.events.fireNonAsciiKeySequence( |
| 456 |
root, keyCode, keyPressKeyCode, {'altKey': true}); |
| 457 |
assertEventTypes(['keydown', 'keypress', 'keyup']); |
| 458 |
assertArrayEquals([keyCode, keyPressKeyCode, keyCode], firedKeyCodes); |
| 459 |
} |
| 460 |
} |
| 461 |
|
| 462 |
var CONTEXTMENU_SEQ = goog.userAgent.WINDOWS ? |
| 463 |
['mousedown', 'mouseup', 'contextmenu'] : |
| 464 |
goog.userAgent.GECKO ? ['mousedown', 'contextmenu', 'mouseup'] : |
| 465 |
goog.userAgent.WEBKIT && goog.userAgent.MAC ? |
| 466 |
['mousedown', 'contextmenu', 'mouseup', 'click'] : |
| 467 |
['mousedown', 'contextmenu', 'mouseup']; |
| 468 |
|
| 469 |
function testContextMenuSequence() { |
| 470 |
assertTrue(goog.testing.events.fireContextMenuSequence(root)); |
| 471 |
assertEventTypes(CONTEXTMENU_SEQ); |
| 472 |
} |
| 473 |
|
| 474 |
function testContextMenuSequenceWithCoordinate() { |
| 475 |
assertTrue(goog.testing.events.fireContextMenuSequence(root, coordinate)); |
| 476 |
assertEventTypes(CONTEXTMENU_SEQ); |
| 477 |
assertCoordinates(goog.array.repeat(coordinate, CONTEXTMENU_SEQ.length)); |
| 478 |
} |
| 479 |
|
| 480 |
function testContextMenuSequenceCancellingMousedown() { |
| 481 |
preventDefaultEventType('mousedown'); |
| 482 |
assertFalse(goog.testing.events.fireContextMenuSequence(root)); |
| 483 |
assertEventTypes(CONTEXTMENU_SEQ); |
| 484 |
} |
| 485 |
|
| 486 |
function testContextMenuSequenceCancellingMouseup() { |
| 487 |
preventDefaultEventType('mouseup'); |
| 488 |
assertFalse(goog.testing.events.fireContextMenuSequence(root)); |
| 489 |
assertEventTypes(CONTEXTMENU_SEQ); |
| 490 |
} |
| 491 |
|
| 492 |
function testContextMenuSequenceCancellingContextMenu() { |
| 493 |
preventDefaultEventType('contextmenu'); |
| 494 |
assertFalse(goog.testing.events.fireContextMenuSequence(root)); |
| 495 |
assertEventTypes(CONTEXTMENU_SEQ); |
| 496 |
} |
| 497 |
|
| 498 |
function testContextMenuSequenceFakeMacWebkit() { |
| 499 |
stubs.set(goog.userAgent, 'WINDOWS', false); |
| 500 |
stubs.set(goog.userAgent, 'MAC', true); |
| 501 |
stubs.set(goog.userAgent, 'WEBKIT', true); |
| 502 |
assertTrue(goog.testing.events.fireContextMenuSequence(root)); |
| 503 |
assertEventTypes(['mousedown', 'contextmenu', 'mouseup', 'click']); |
| 504 |
} |
| 505 |
|
| 506 |
function testCaptureBubble_simple() { |
| 507 |
assertTrue(goog.testing.events.fireClickEvent(childEl)); |
| 508 |
assertObjectEquals( |
| 509 |
{parentCapture: 1, childCapture: 1, childBubble: 1, parentBubble: 1}, |
| 510 |
eventCount); |
| 511 |
} |
| 512 |
|
| 513 |
function testCaptureBubble_preventDefault() { |
| 514 |
goog.events.listen(childEl, goog.events.EventType.CLICK, function(e) { |
| 515 |
e.preventDefault(); |
| 516 |
}); |
| 517 |
assertFalse(goog.testing.events.fireClickEvent(childEl)); |
| 518 |
assertObjectEquals( |
| 519 |
{parentCapture: 1, childCapture: 1, childBubble: 1, parentBubble: 1}, |
| 520 |
eventCount); |
| 521 |
} |
| 522 |
|
| 523 |
function testCaptureBubble_stopPropagationParentCapture() { |
| 524 |
goog.events.listen(parentEl, goog.events.EventType.CLICK, function(e) { |
| 525 |
e.stopPropagation(); |
| 526 |
}, true /* capture */); |
| 527 |
assertTrue(goog.testing.events.fireClickEvent(childEl)); |
| 528 |
assertObjectEquals( |
| 529 |
{parentCapture: 1, childCapture: 0, childBubble: 0, parentBubble: 0}, |
| 530 |
eventCount); |
| 531 |
} |
| 532 |
|
| 533 |
function testCaptureBubble_stopPropagationChildCapture() { |
| 534 |
goog.events.listen(childEl, goog.events.EventType.CLICK, function(e) { |
| 535 |
e.stopPropagation(); |
| 536 |
}, true /* capture */); |
| 537 |
assertTrue(goog.testing.events.fireClickEvent(childEl)); |
| 538 |
assertObjectEquals( |
| 539 |
{parentCapture: 1, childCapture: 1, childBubble: 0, parentBubble: 0}, |
| 540 |
eventCount); |
| 541 |
} |
| 542 |
|
| 543 |
function testCaptureBubble_stopPropagationChildBubble() { |
| 544 |
goog.events.listen(childEl, goog.events.EventType.CLICK, function(e) { |
| 545 |
e.stopPropagation(); |
| 546 |
}); |
| 547 |
assertTrue(goog.testing.events.fireClickEvent(childEl)); |
| 548 |
assertObjectEquals( |
| 549 |
{parentCapture: 1, childCapture: 1, childBubble: 1, parentBubble: 0}, |
| 550 |
eventCount); |
| 551 |
} |
| 552 |
|
| 553 |
function testCaptureBubble_stopPropagationParentBubble() { |
| 554 |
goog.events.listen(parentEl, goog.events.EventType.CLICK, function(e) { |
| 555 |
e.stopPropagation(); |
| 556 |
}); |
| 557 |
assertTrue(goog.testing.events.fireClickEvent(childEl)); |
| 558 |
assertObjectEquals( |
| 559 |
{parentCapture: 1, childCapture: 1, childBubble: 1, parentBubble: 1}, |
| 560 |
eventCount); |
| 561 |
} |
| 562 |
|
| 563 |
function testMixinListenable() { |
| 564 |
var obj = {}; |
| 565 |
obj.doFoo = goog.testing.recordFunction(); |
| 566 |
|
| 567 |
goog.testing.events.mixinListenable(obj); |
| 568 |
|
| 569 |
obj.doFoo(); |
| 570 |
assertEquals(1, obj.doFoo.getCallCount()); |
| 571 |
|
| 572 |
var handler = goog.testing.recordFunction(); |
| 573 |
goog.events.listen(obj, 'test', handler); |
| 574 |
obj.dispatchEvent('test'); |
| 575 |
assertEquals(1, handler.getCallCount()); |
| 576 |
assertEquals(obj, handler.getLastCall().getArgument(0).target); |
| 577 |
|
| 578 |
goog.events.unlisten(obj, 'test', handler); |
| 579 |
obj.dispatchEvent('test'); |
| 580 |
assertEquals(1, handler.getCallCount()); |
| 581 |
|
| 582 |
goog.events.listen(obj, 'test', handler); |
| 583 |
obj.dispose(); |
| 584 |
obj.dispatchEvent('test'); |
| 585 |
assertEquals(1, handler.getCallCount()); |
| 586 |
} |
| 587 |
|
| 588 |
|
| 589 |
/** |
| 590 |
* Assert that the list of events given was fired, in that order. |
| 591 |
*/ |
| 592 |
function assertEventTypes(list) { |
| 593 |
assertArrayEquals(list, firedEventTypes); |
| 594 |
} |
| 595 |
|
| 596 |
|
| 597 |
/** |
| 598 |
* Assert that the list of event coordinates given was caught, in that order. |
| 599 |
*/ |
| 600 |
function assertCoordinates(list) { |
| 601 |
assertArrayEquals(list, firedEventCoordinates); |
| 602 |
assertArrayEquals(list, firedScreenCoordinates); |
| 603 |
} |
| 604 |
|
| 605 |
|
| 606 |
/** Prevent default the event of the given type on the root element. */ |
| 607 |
function preventDefaultEventType(type) { |
| 608 |
goog.events.listen(root, type, preventDefault); |
| 609 |
} |
| 610 |
|
| 611 |
function preventDefault(e) { |
| 612 |
e.preventDefault(); |
| 613 |
} |