| 1 |
// Copyright 2006 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.eventsTest'); |
| 16 |
goog.setTestOnly('goog.eventsTest'); |
| 17 |
|
| 18 |
goog.require('goog.asserts.AssertionError'); |
| 19 |
goog.require('goog.debug.EntryPointMonitor'); |
| 20 |
goog.require('goog.debug.ErrorHandler'); |
| 21 |
goog.require('goog.debug.entryPointRegistry'); |
| 22 |
goog.require('goog.dom'); |
| 23 |
goog.require('goog.dom.TagName'); |
| 24 |
goog.require('goog.events'); |
| 25 |
goog.require('goog.events.BrowserFeature'); |
| 26 |
goog.require('goog.events.CaptureSimulationMode'); |
| 27 |
goog.require('goog.events.Event'); |
| 28 |
goog.require('goog.events.EventTarget'); |
| 29 |
goog.require('goog.events.EventType'); |
| 30 |
goog.require('goog.events.Listener'); |
| 31 |
goog.require('goog.functions'); |
| 32 |
goog.require('goog.testing.PropertyReplacer'); |
| 33 |
goog.require('goog.testing.jsunit'); |
| 34 |
goog.require('goog.testing.recordFunction'); |
| 35 |
|
| 36 |
var originalHandleBrowserEvent = goog.events.handleBrowserEvent_; |
| 37 |
var propertyReplacer; |
| 38 |
var et1, et2, et3; |
| 39 |
|
| 40 |
function setUp() { |
| 41 |
et1 = new goog.events.EventTarget(); |
| 42 |
et2 = new goog.events.EventTarget(); |
| 43 |
et3 = new goog.events.EventTarget(); |
| 44 |
propertyReplacer = new goog.testing.PropertyReplacer(); |
| 45 |
} |
| 46 |
|
| 47 |
function tearDown() { |
| 48 |
goog.events.CAPTURE_SIMULATION_MODE = goog.events.CaptureSimulationMode.ON; |
| 49 |
goog.events.handleBrowserEvent_ = originalHandleBrowserEvent; |
| 50 |
goog.disposeAll(et1, et2, et3); |
| 51 |
goog.events.removeAll(document.body); |
| 52 |
propertyReplacer.reset(); |
| 53 |
} |
| 54 |
|
| 55 |
function testProtectBrowserEventEntryPoint() { |
| 56 |
var errorHandlerFn = goog.testing.recordFunction(); |
| 57 |
var errorHandler = new goog.debug.ErrorHandler(errorHandlerFn); |
| 58 |
|
| 59 |
goog.events.protectBrowserEventEntryPoint(errorHandler); |
| 60 |
|
| 61 |
var browserEventHandler = |
| 62 |
goog.testing.recordFunction(goog.events.handleBrowserEvent_); |
| 63 |
goog.events.handleBrowserEvent_ = function() { |
| 64 |
try { |
| 65 |
browserEventHandler.apply(this, arguments); |
| 66 |
} catch (e) { |
| 67 |
// Ignored. |
| 68 |
} |
| 69 |
}; |
| 70 |
|
| 71 |
var err = Error('test'); |
| 72 |
var body = document.body; |
| 73 |
goog.events.listen( |
| 74 |
body, goog.events.EventType.CLICK, function() { throw err; }); |
| 75 |
|
| 76 |
dispatchClick(body); |
| 77 |
|
| 78 |
assertEquals( |
| 79 |
'Error handler callback should be called.', 1, |
| 80 |
errorHandlerFn.getCallCount()); |
| 81 |
assertEquals(err, errorHandlerFn.getLastCall().getArgument(0)); |
| 82 |
|
| 83 |
assertEquals(1, browserEventHandler.getCallCount()); |
| 84 |
var err2 = browserEventHandler.getLastCall().getError(); |
| 85 |
assertNotNull(err2); |
| 86 |
assertTrue(err2 instanceof goog.debug.ErrorHandler.ProtectedFunctionError); |
| 87 |
} |
| 88 |
|
| 89 |
function testSelfRemove() { |
| 90 |
var callback = function() { |
| 91 |
// This listener removes itself during event dispatching, so it |
| 92 |
// is marked as 'removed' but not actually removed until after event |
| 93 |
// dispatching ends. |
| 94 |
goog.events.removeAll(et1, 'click'); |
| 95 |
|
| 96 |
// Test that goog.events.getListener ignores events marked as 'removed'. |
| 97 |
assertNull(goog.events.getListener(et1, 'click', callback)); |
| 98 |
}; |
| 99 |
goog.events.listen(et1, 'click', callback); |
| 100 |
goog.events.dispatchEvent(et1, 'click'); |
| 101 |
} |
| 102 |
|
| 103 |
function testMediaQueryList() { |
| 104 |
if (!window.matchMedia) return; |
| 105 |
|
| 106 |
var mql = window.matchMedia('(max-width: 640px)'); |
| 107 |
var key = goog.events.listen(mql, 'change', goog.nullFunction); |
| 108 |
|
| 109 |
// I don't know of any way to make it raise an event in a test. |
| 110 |
|
| 111 |
goog.events.unlistenByKey(key); |
| 112 |
} |
| 113 |
|
| 114 |
function testHasListener() { |
| 115 |
var div = goog.dom.createElement(goog.dom.TagName.DIV); |
| 116 |
assertFalse(goog.events.hasListener(div)); |
| 117 |
|
| 118 |
var key = goog.events.listen(div, 'click', function() {}); |
| 119 |
assertTrue(goog.events.hasListener(div)); |
| 120 |
assertTrue(goog.events.hasListener(div, 'click')); |
| 121 |
assertTrue(goog.events.hasListener(div, 'click', false)); |
| 122 |
assertTrue(goog.events.hasListener(div, undefined, false)); |
| 123 |
|
| 124 |
assertFalse(goog.events.hasListener(div, 'click', true)); |
| 125 |
assertFalse(goog.events.hasListener(div, undefined, true)); |
| 126 |
assertFalse(goog.events.hasListener(div, 'mouseup')); |
| 127 |
|
| 128 |
// Test that hasListener returns false when all listeners are removed. |
| 129 |
goog.events.unlistenByKey(key); |
| 130 |
assertFalse(goog.events.hasListener(div)); |
| 131 |
} |
| 132 |
|
| 133 |
function testHasListenerWithEventTarget() { |
| 134 |
assertFalse(goog.events.hasListener(et1)); |
| 135 |
|
| 136 |
function callback() {} |
| 137 |
goog.events.listen(et1, 'test', callback, true); |
| 138 |
assertTrue(goog.events.hasListener(et1)); |
| 139 |
assertTrue(goog.events.hasListener(et1, 'test')); |
| 140 |
assertTrue(goog.events.hasListener(et1, 'test', true)); |
| 141 |
assertTrue(goog.events.hasListener(et1, undefined, true)); |
| 142 |
|
| 143 |
assertFalse(goog.events.hasListener(et1, 'click')); |
| 144 |
assertFalse(goog.events.hasListener(et1, 'test', false)); |
| 145 |
|
| 146 |
goog.events.unlisten(et1, 'test', callback, true); |
| 147 |
assertFalse(goog.events.hasListener(et1)); |
| 148 |
} |
| 149 |
|
| 150 |
function testHasListenerWithMultipleTargets() { |
| 151 |
function callback() {} |
| 152 |
|
| 153 |
goog.events.listen(et1, 'test1', callback, true); |
| 154 |
goog.events.listen(et2, 'test2', callback, true); |
| 155 |
|
| 156 |
assertTrue(goog.events.hasListener(et1)); |
| 157 |
assertTrue(goog.events.hasListener(et2)); |
| 158 |
assertTrue(goog.events.hasListener(et1, 'test1')); |
| 159 |
assertTrue(goog.events.hasListener(et2, 'test2')); |
| 160 |
|
| 161 |
assertFalse(goog.events.hasListener(et1, 'et2')); |
| 162 |
assertFalse(goog.events.hasListener(et2, 'et1')); |
| 163 |
|
| 164 |
goog.events.removeAll(et1); |
| 165 |
goog.events.removeAll(et2); |
| 166 |
} |
| 167 |
|
| 168 |
function testBubbleSingle() { |
| 169 |
et1.setParentEventTarget(et2); |
| 170 |
et2.setParentEventTarget(et3); |
| 171 |
|
| 172 |
var count = 0; |
| 173 |
function callback() { count++; } |
| 174 |
|
| 175 |
goog.events.listen(et3, 'test', callback, false); |
| 176 |
|
| 177 |
et1.dispatchEvent('test'); |
| 178 |
|
| 179 |
assertEquals(1, count); |
| 180 |
|
| 181 |
goog.events.removeAll(et1); |
| 182 |
goog.events.removeAll(et2); |
| 183 |
goog.events.removeAll(et3); |
| 184 |
} |
| 185 |
|
| 186 |
function testCaptureSingle() { |
| 187 |
et1.setParentEventTarget(et2); |
| 188 |
et2.setParentEventTarget(et3); |
| 189 |
|
| 190 |
var count = 0; |
| 191 |
function callback() { count++; } |
| 192 |
|
| 193 |
goog.events.listen(et3, 'test', callback, true); |
| 194 |
|
| 195 |
et1.dispatchEvent('test'); |
| 196 |
|
| 197 |
assertEquals(1, count); |
| 198 |
|
| 199 |
goog.events.removeAll(et1); |
| 200 |
goog.events.removeAll(et2); |
| 201 |
goog.events.removeAll(et3); |
| 202 |
} |
| 203 |
|
| 204 |
function testCaptureAndBubble() { |
| 205 |
et1.setParentEventTarget(et2); |
| 206 |
et2.setParentEventTarget(et3); |
| 207 |
|
| 208 |
var count = 0; |
| 209 |
function callbackCapture1() { |
| 210 |
count++; |
| 211 |
assertEquals(3, count); |
| 212 |
} |
| 213 |
function callbackBubble1() { |
| 214 |
count++; |
| 215 |
assertEquals(4, count); |
| 216 |
} |
| 217 |
|
| 218 |
function callbackCapture2() { |
| 219 |
count++; |
| 220 |
assertEquals(2, count); |
| 221 |
} |
| 222 |
function callbackBubble2() { |
| 223 |
count++; |
| 224 |
assertEquals(5, count); |
| 225 |
} |
| 226 |
|
| 227 |
function callbackCapture3() { |
| 228 |
count++; |
| 229 |
assertEquals(1, count); |
| 230 |
} |
| 231 |
function callbackBubble3() { |
| 232 |
count++; |
| 233 |
assertEquals(6, count); |
| 234 |
} |
| 235 |
|
| 236 |
goog.events.listen(et1, 'test', callbackCapture1, true); |
| 237 |
goog.events.listen(et1, 'test', callbackBubble1, false); |
| 238 |
goog.events.listen(et2, 'test', callbackCapture2, true); |
| 239 |
goog.events.listen(et2, 'test', callbackBubble2, false); |
| 240 |
goog.events.listen(et3, 'test', callbackCapture3, true); |
| 241 |
goog.events.listen(et3, 'test', callbackBubble3, false); |
| 242 |
|
| 243 |
et1.dispatchEvent('test'); |
| 244 |
|
| 245 |
assertEquals(6, count); |
| 246 |
|
| 247 |
goog.events.removeAll(et1); |
| 248 |
goog.events.removeAll(et2); |
| 249 |
goog.events.removeAll(et3); |
| 250 |
|
| 251 |
// Try again with the new API: |
| 252 |
count = 0; |
| 253 |
|
| 254 |
goog.events.listen(et1, 'test', callbackCapture1, {capture: true}); |
| 255 |
goog.events.listen(et1, 'test', callbackBubble1, {capture: false}); |
| 256 |
goog.events.listen(et2, 'test', callbackCapture2, {capture: true}); |
| 257 |
goog.events.listen(et2, 'test', callbackBubble2, {capture: false}); |
| 258 |
goog.events.listen(et3, 'test', callbackCapture3, {capture: true}); |
| 259 |
goog.events.listen(et3, 'test', callbackBubble3, {capture: false}); |
| 260 |
|
| 261 |
et1.dispatchEvent('test'); |
| 262 |
|
| 263 |
assertEquals(6, count); |
| 264 |
|
| 265 |
goog.events.removeAll(et1); |
| 266 |
goog.events.removeAll(et2); |
| 267 |
goog.events.removeAll(et3); |
| 268 |
|
| 269 |
|
| 270 |
// Try again with the new API and without capture simulation: |
| 271 |
if (!goog.events.BrowserFeature.HAS_W3C_EVENT_SUPPORT) return; |
| 272 |
goog.events.CAPTURE_SIMULATION_MODE = |
| 273 |
goog.events.CaptureSimulationMode.OFF_AND_FAIL; |
| 274 |
count = 0; |
| 275 |
|
| 276 |
goog.events.listen(et1, 'test', callbackCapture1, {capture: true}); |
| 277 |
goog.events.listen(et1, 'test', callbackBubble1, {capture: false}); |
| 278 |
goog.events.listen(et2, 'test', callbackCapture2, {capture: true}); |
| 279 |
goog.events.listen(et2, 'test', callbackBubble2, {capture: false}); |
| 280 |
goog.events.listen(et3, 'test', callbackCapture3, {capture: true}); |
| 281 |
goog.events.listen(et3, 'test', callbackBubble3, {capture: false}); |
| 282 |
|
| 283 |
et1.dispatchEvent('test'); |
| 284 |
|
| 285 |
assertEquals(6, count); |
| 286 |
|
| 287 |
goog.events.removeAll(et1); |
| 288 |
goog.events.removeAll(et2); |
| 289 |
goog.events.removeAll(et3); |
| 290 |
} |
| 291 |
|
| 292 |
function testCapturingRemovesBubblingListener() { |
| 293 |
var bubbleCount = 0; |
| 294 |
function callbackBubble() { bubbleCount++; } |
| 295 |
|
| 296 |
var captureCount = 0; |
| 297 |
function callbackCapture() { |
| 298 |
captureCount++; |
| 299 |
goog.events.removeAll(et1); |
| 300 |
} |
| 301 |
|
| 302 |
goog.events.listen(et1, 'test', callbackCapture, true); |
| 303 |
goog.events.listen(et1, 'test', callbackBubble, false); |
| 304 |
|
| 305 |
et1.dispatchEvent('test'); |
| 306 |
assertEquals(1, captureCount); |
| 307 |
assertEquals(0, bubbleCount); |
| 308 |
} |
| 309 |
|
| 310 |
function dispatchClick(target) { |
| 311 |
if (target.click) { |
| 312 |
target.click(); |
| 313 |
} else { |
| 314 |
var e = document.createEvent('MouseEvents'); |
| 315 |
e.initMouseEvent( |
| 316 |
'click', true, true, window, 0, 0, 0, 0, 0, false, false, false, false, |
| 317 |
0, null); |
| 318 |
target.dispatchEvent(e); |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
function testHandleBrowserEventBubblingListener() { |
| 323 |
var count = 0; |
| 324 |
var body = document.body; |
| 325 |
goog.events.listen(body, 'click', function() { count++; }); |
| 326 |
dispatchClick(body); |
| 327 |
assertEquals(1, count); |
| 328 |
} |
| 329 |
|
| 330 |
function testHandleBrowserEventCapturingListener() { |
| 331 |
var count = 0; |
| 332 |
var body = document.body; |
| 333 |
goog.events.listen(body, 'click', function() { count++; }, true); |
| 334 |
dispatchClick(body); |
| 335 |
assertEquals(1, count); |
| 336 |
} |
| 337 |
|
| 338 |
function testHandleBrowserEventCapturingAndBubblingListener() { |
| 339 |
var count = 1; |
| 340 |
var body = document.body; |
| 341 |
goog.events.listen(body, 'click', function() { count += 3; }, true); |
| 342 |
goog.events.listen(body, 'click', function() { count *= 5; }, false); |
| 343 |
dispatchClick(body); |
| 344 |
assertEquals(20, count); |
| 345 |
} |
| 346 |
|
| 347 |
function testHandleBrowserEventCapturingRemovesBubblingListener() { |
| 348 |
var body = document.body; |
| 349 |
|
| 350 |
var bubbleCount = 0; |
| 351 |
function callbackBubble() { bubbleCount++; } |
| 352 |
|
| 353 |
var captureCount = 0; |
| 354 |
function callbackCapture() { |
| 355 |
captureCount++; |
| 356 |
goog.events.removeAll(body); |
| 357 |
} |
| 358 |
|
| 359 |
goog.events.listen(body, 'click', callbackCapture, true); |
| 360 |
goog.events.listen(body, 'click', callbackBubble, false); |
| 361 |
|
| 362 |
dispatchClick(body); |
| 363 |
assertEquals(1, captureCount); |
| 364 |
assertEquals(0, bubbleCount); |
| 365 |
} |
| 366 |
|
| 367 |
function testHandleEventPropagationOnParentElement() { |
| 368 |
var count = 1; |
| 369 |
goog.events.listen( |
| 370 |
document.documentElement, 'click', function() { count += 3; }, true); |
| 371 |
goog.events.listen( |
| 372 |
document.documentElement, 'click', function() { count *= 5; }, false); |
| 373 |
dispatchClick(document.body); |
| 374 |
assertEquals(20, count); |
| 375 |
} |
| 376 |
|
| 377 |
function testEntryPointRegistry() { |
| 378 |
var monitor = new goog.debug.EntryPointMonitor(); |
| 379 |
var replacement = function() {}; |
| 380 |
monitor.wrap = |
| 381 |
goog.testing.recordFunction(goog.functions.constant(replacement)); |
| 382 |
|
| 383 |
goog.debug.entryPointRegistry.monitorAll(monitor); |
| 384 |
assertTrue(monitor.wrap.getCallCount() >= 1); |
| 385 |
assertEquals(replacement, goog.events.handleBrowserEvent_); |
| 386 |
} |
| 387 |
|
| 388 |
// Fixes bug http://b/6434926 |
| 389 |
function testListenOnceHandlerDispatchCausingInfiniteLoop() { |
| 390 |
var handleFoo = |
| 391 |
goog.testing.recordFunction(function() { et1.dispatchEvent('foo'); }); |
| 392 |
|
| 393 |
goog.events.listenOnce(et1, 'foo', handleFoo); |
| 394 |
|
| 395 |
et1.dispatchEvent('foo'); |
| 396 |
|
| 397 |
assertEquals( |
| 398 |
'Handler should be called only once.', 1, handleFoo.getCallCount()); |
| 399 |
} |
| 400 |
|
| 401 |
function testCreationStack() { |
| 402 |
if (!new Error().stack) return; |
| 403 |
propertyReplacer.replace(goog.events.Listener, 'ENABLE_MONITORING', true); |
| 404 |
|
| 405 |
var div = goog.dom.createElement(goog.dom.TagName.DIV); |
| 406 |
var key = |
| 407 |
goog.events.listen(div, goog.events.EventType.CLICK, goog.nullFunction); |
| 408 |
var listenerStack = key.creationStack; |
| 409 |
|
| 410 |
// Check that the name of this test function occurs in the stack trace. |
| 411 |
assertContains('testCreationStack', listenerStack); |
| 412 |
goog.events.unlistenByKey(key); |
| 413 |
} |
| 414 |
|
| 415 |
function testListenOnceAfterListenDoesNotChangeExistingListener() { |
| 416 |
var listener = goog.testing.recordFunction(); |
| 417 |
goog.events.listen(document.body, 'click', listener); |
| 418 |
goog.events.listenOnce(document.body, 'click', listener); |
| 419 |
|
| 420 |
dispatchClick(document.body); |
| 421 |
dispatchClick(document.body); |
| 422 |
dispatchClick(document.body); |
| 423 |
|
| 424 |
assertEquals(3, listener.getCallCount()); |
| 425 |
} |
| 426 |
|
| 427 |
function testListenOnceAfterListenOnceDoesNotChangeExistingListener() { |
| 428 |
var listener = goog.testing.recordFunction(); |
| 429 |
goog.events.listenOnce(document.body, 'click', listener); |
| 430 |
goog.events.listenOnce(document.body, 'click', listener); |
| 431 |
|
| 432 |
dispatchClick(document.body); |
| 433 |
dispatchClick(document.body); |
| 434 |
dispatchClick(document.body); |
| 435 |
|
| 436 |
assertEquals(1, listener.getCallCount()); |
| 437 |
} |
| 438 |
|
| 439 |
function testListenAfterListenOnceRemoveOnceness() { |
| 440 |
var listener = goog.testing.recordFunction(); |
| 441 |
goog.events.listenOnce(document.body, 'click', listener); |
| 442 |
goog.events.listen(document.body, 'click', listener); |
| 443 |
|
| 444 |
dispatchClick(document.body); |
| 445 |
dispatchClick(document.body); |
| 446 |
dispatchClick(document.body); |
| 447 |
|
| 448 |
assertEquals(3, listener.getCallCount()); |
| 449 |
} |
| 450 |
|
| 451 |
function testUnlistenAfterListenOnce() { |
| 452 |
var listener = goog.testing.recordFunction(); |
| 453 |
|
| 454 |
goog.events.listenOnce(document.body, 'click', listener); |
| 455 |
goog.events.unlisten(document.body, 'click', listener); |
| 456 |
dispatchClick(document.body); |
| 457 |
|
| 458 |
goog.events.listenOnce(document.body, 'click', listener); |
| 459 |
goog.events.listen(document.body, 'click', listener); |
| 460 |
goog.events.unlisten(document.body, 'click', listener); |
| 461 |
dispatchClick(document.body); |
| 462 |
|
| 463 |
goog.events.listen(document.body, 'click', listener); |
| 464 |
goog.events.listenOnce(document.body, 'click', listener); |
| 465 |
goog.events.unlisten(document.body, 'click', listener); |
| 466 |
dispatchClick(document.body); |
| 467 |
|
| 468 |
goog.events.listenOnce(document.body, 'click', listener); |
| 469 |
goog.events.listenOnce(document.body, 'click', listener); |
| 470 |
goog.events.unlisten(document.body, 'click', listener); |
| 471 |
dispatchClick(document.body); |
| 472 |
|
| 473 |
assertEquals(0, listener.getCallCount()); |
| 474 |
} |
| 475 |
|
| 476 |
function testEventBubblingWithReentrantDispatch_bubbling() { |
| 477 |
runEventPropagationWithReentrantDispatch(false); |
| 478 |
} |
| 479 |
|
| 480 |
function testEventBubblingWithReentrantDispatch_capture() { |
| 481 |
runEventPropagationWithReentrantDispatch(true); |
| 482 |
} |
| 483 |
|
| 484 |
function runEventPropagationWithReentrantDispatch(useCapture) { |
| 485 |
var eventType = 'test-event-type'; |
| 486 |
|
| 487 |
var child = et1; |
| 488 |
var parent = et2; |
| 489 |
child.setParentEventTarget(parent); |
| 490 |
|
| 491 |
var firstTarget = useCapture ? parent : child; |
| 492 |
var secondTarget = useCapture ? child : parent; |
| 493 |
|
| 494 |
var firstListener = function(evt) { |
| 495 |
if (evt.isFirstEvent) { |
| 496 |
// Fires another event of the same type the first time it is invoked. |
| 497 |
child.dispatchEvent(new goog.events.Event(eventType)); |
| 498 |
} |
| 499 |
}; |
| 500 |
goog.events.listen(firstTarget, eventType, firstListener, useCapture); |
| 501 |
|
| 502 |
var secondListener = goog.testing.recordFunction(); |
| 503 |
goog.events.listen(secondTarget, eventType, secondListener, useCapture); |
| 504 |
|
| 505 |
// Fire the first event. |
| 506 |
var firstEvent = new goog.events.Event(eventType); |
| 507 |
firstEvent.isFirstEvent = true; |
| 508 |
child.dispatchEvent(firstEvent); |
| 509 |
|
| 510 |
assertEquals(2, secondListener.getCallCount()); |
| 511 |
} |
| 512 |
|
| 513 |
function testEventPropagationWhenListenerRemoved_bubbling() { |
| 514 |
runEventPropagationWhenListenerRemoved(false); |
| 515 |
} |
| 516 |
|
| 517 |
function testEventPropagationWhenListenerRemoved_capture() { |
| 518 |
runEventPropagationWhenListenerRemoved(true); |
| 519 |
} |
| 520 |
|
| 521 |
function runEventPropagationWhenListenerRemoved(useCapture) { |
| 522 |
var eventType = 'test-event-type'; |
| 523 |
|
| 524 |
var child = et1; |
| 525 |
var parent = et2; |
| 526 |
child.setParentEventTarget(parent); |
| 527 |
|
| 528 |
var firstTarget = useCapture ? parent : child; |
| 529 |
var secondTarget = useCapture ? child : parent; |
| 530 |
|
| 531 |
var firstListener = goog.testing.recordFunction(); |
| 532 |
var secondListener = goog.testing.recordFunction(); |
| 533 |
goog.events.listenOnce(firstTarget, eventType, firstListener, useCapture); |
| 534 |
goog.events.listen(secondTarget, eventType, secondListener, useCapture); |
| 535 |
|
| 536 |
child.dispatchEvent(new goog.events.Event(eventType)); |
| 537 |
|
| 538 |
assertEquals(1, secondListener.getCallCount()); |
| 539 |
} |
| 540 |
|
| 541 |
function testEventPropagationWhenListenerAdded_bubbling() { |
| 542 |
runEventPropagationWhenListenerAdded(false); |
| 543 |
} |
| 544 |
|
| 545 |
function testEventPropagationWhenListenerAdded_capture() { |
| 546 |
runEventPropagationWhenListenerAdded(true); |
| 547 |
} |
| 548 |
|
| 549 |
function runEventPropagationWhenListenerAdded(useCapture) { |
| 550 |
var eventType = 'test-event-type'; |
| 551 |
|
| 552 |
var child = et1; |
| 553 |
var parent = et2; |
| 554 |
child.setParentEventTarget(parent); |
| 555 |
|
| 556 |
var firstTarget = useCapture ? parent : child; |
| 557 |
var secondTarget = useCapture ? child : parent; |
| 558 |
|
| 559 |
var firstListener = function() { |
| 560 |
goog.events.listen(secondTarget, eventType, secondListener, useCapture); |
| 561 |
}; |
| 562 |
var secondListener = goog.testing.recordFunction(); |
| 563 |
goog.events.listen(firstTarget, eventType, firstListener, useCapture); |
| 564 |
|
| 565 |
child.dispatchEvent(new goog.events.Event(eventType)); |
| 566 |
|
| 567 |
assertEquals(1, secondListener.getCallCount()); |
| 568 |
} |
| 569 |
|
| 570 |
function testEventPropagationWhenListenerAddedAndRemoved_bubbling() { |
| 571 |
runEventPropagationWhenListenerAddedAndRemoved(false); |
| 572 |
} |
| 573 |
|
| 574 |
function testEventPropagationWhenListenerAddedAndRemoved_capture() { |
| 575 |
runEventPropagationWhenListenerAddedAndRemoved(true); |
| 576 |
} |
| 577 |
|
| 578 |
function runEventPropagationWhenListenerAddedAndRemoved(useCapture) { |
| 579 |
var eventType = 'test-event-type'; |
| 580 |
|
| 581 |
var child = et1; |
| 582 |
var parent = et2; |
| 583 |
child.setParentEventTarget(parent); |
| 584 |
|
| 585 |
var firstTarget = useCapture ? parent : child; |
| 586 |
var secondTarget = useCapture ? child : parent; |
| 587 |
|
| 588 |
var firstListener = function() { |
| 589 |
goog.events.listen(secondTarget, eventType, secondListener, useCapture); |
| 590 |
}; |
| 591 |
var secondListener = goog.testing.recordFunction(); |
| 592 |
goog.events.listenOnce(firstTarget, eventType, firstListener, useCapture); |
| 593 |
|
| 594 |
child.dispatchEvent(new goog.events.Event(eventType)); |
| 595 |
|
| 596 |
assertEquals(1, secondListener.getCallCount()); |
| 597 |
} |
| 598 |
|
| 599 |
function testAssertWhenUsedWithUninitializedCustomEventTarget() { |
| 600 |
var SubClass = function() { /* does not call superclass ctor */ }; |
| 601 |
goog.inherits(SubClass, goog.events.EventTarget); |
| 602 |
|
| 603 |
var instance = new SubClass(); |
| 604 |
|
| 605 |
var e; |
| 606 |
e = assertThrows(function() { |
| 607 |
goog.events.listen(instance, 'test1', function() {}); |
| 608 |
}); |
| 609 |
assertTrue(e instanceof goog.asserts.AssertionError); |
| 610 |
e = assertThrows(function() { |
| 611 |
goog.events.dispatchEvent(instance, 'test1'); |
| 612 |
}); |
| 613 |
assertTrue(e instanceof goog.asserts.AssertionError); |
| 614 |
e = assertThrows(function() { instance.dispatchEvent('test1'); }); |
| 615 |
assertTrue(e instanceof goog.asserts.AssertionError); |
| 616 |
} |
| 617 |
|
| 618 |
function testAssertWhenDispatchEventIsUsedWithNonCustomEventTarget() { |
| 619 |
var obj = {}; |
| 620 |
e = assertThrows(function() { goog.events.dispatchEvent(obj, 'test1'); }); |
| 621 |
assertTrue(e instanceof goog.asserts.AssertionError); |
| 622 |
} |
| 623 |
|
| 624 |
|
| 625 |
function testPropagationStoppedDuringCapture() { |
| 626 |
var captureHandler = |
| 627 |
goog.testing.recordFunction(function(e) { e.stopPropagation(); }); |
| 628 |
var bubbleHandler = goog.testing.recordFunction(); |
| 629 |
|
| 630 |
var body = document.body; |
| 631 |
var div = goog.dom.createElement(goog.dom.TagName.DIV); |
| 632 |
body.appendChild(div); |
| 633 |
try { |
| 634 |
goog.events.listen(body, 'click', captureHandler, true); |
| 635 |
goog.events.listen(div, 'click', bubbleHandler, false); |
| 636 |
goog.events.listen(body, 'click', bubbleHandler, false); |
| 637 |
|
| 638 |
dispatchClick(div); |
| 639 |
assertEquals(1, captureHandler.getCallCount()); |
| 640 |
assertEquals(0, bubbleHandler.getCallCount()); |
| 641 |
|
| 642 |
goog.events.unlisten(body, 'click', captureHandler, true); |
| 643 |
|
| 644 |
dispatchClick(div); |
| 645 |
assertEquals(2, bubbleHandler.getCallCount()); |
| 646 |
} finally { |
| 647 |
goog.dom.removeNode(div); |
| 648 |
goog.events.removeAll(body); |
| 649 |
goog.events.removeAll(div); |
| 650 |
} |
| 651 |
} |
| 652 |
|
| 653 |
function testPropagationStoppedDuringBubble() { |
| 654 |
var captureHandler = goog.testing.recordFunction(); |
| 655 |
var bubbleHandler1 = |
| 656 |
goog.testing.recordFunction(function(e) { e.stopPropagation(); }); |
| 657 |
var bubbleHandler2 = goog.testing.recordFunction(); |
| 658 |
|
| 659 |
var body = document.body; |
| 660 |
var div = goog.dom.createElement(goog.dom.TagName.DIV); |
| 661 |
body.appendChild(div); |
| 662 |
try { |
| 663 |
goog.events.listen(body, 'click', captureHandler, true); |
| 664 |
goog.events.listen(div, 'click', bubbleHandler1, false); |
| 665 |
goog.events.listen(body, 'click', bubbleHandler2, false); |
| 666 |
|
| 667 |
dispatchClick(div); |
| 668 |
assertEquals(1, captureHandler.getCallCount()); |
| 669 |
assertEquals(1, bubbleHandler1.getCallCount()); |
| 670 |
assertEquals(0, bubbleHandler2.getCallCount()); |
| 671 |
} finally { |
| 672 |
goog.dom.removeNode(div); |
| 673 |
goog.events.removeAll(body); |
| 674 |
goog.events.removeAll(div); |
| 675 |
} |
| 676 |
} |
| 677 |
|
| 678 |
function testAddingCaptureListenerDuringBubbleShouldNotFireTheListener() { |
| 679 |
var body = document.body; |
| 680 |
var div = goog.dom.createElement(goog.dom.TagName.DIV); |
| 681 |
body.appendChild(div); |
| 682 |
|
| 683 |
var captureHandler1 = goog.testing.recordFunction(); |
| 684 |
var captureHandler2 = goog.testing.recordFunction(); |
| 685 |
var bubbleHandler = goog.testing.recordFunction(function(e) { |
| 686 |
goog.events.listen(body, 'click', captureHandler1, true); |
| 687 |
goog.events.listen(div, 'click', captureHandler2, true); |
| 688 |
}); |
| 689 |
|
| 690 |
try { |
| 691 |
goog.events.listen(div, 'click', bubbleHandler, false); |
| 692 |
|
| 693 |
dispatchClick(div); |
| 694 |
|
| 695 |
// These verify that the capture handlers registered in the bubble |
| 696 |
// handler is not invoked in the same event propagation phase. |
| 697 |
assertEquals(0, captureHandler1.getCallCount()); |
| 698 |
assertEquals(0, captureHandler2.getCallCount()); |
| 699 |
assertEquals(1, bubbleHandler.getCallCount()); |
| 700 |
} finally { |
| 701 |
goog.dom.removeNode(div); |
| 702 |
goog.events.removeAll(body); |
| 703 |
goog.events.removeAll(div); |
| 704 |
} |
| 705 |
} |
| 706 |
|
| 707 |
function testRemovingCaptureListenerDuringBubbleWouldNotFireListenerTwice() { |
| 708 |
var body = document.body; |
| 709 |
var div = goog.dom.createElement(goog.dom.TagName.DIV); |
| 710 |
body.appendChild(div); |
| 711 |
|
| 712 |
var captureHandler = goog.testing.recordFunction(); |
| 713 |
var bubbleHandler1 = goog.testing.recordFunction(function(e) { |
| 714 |
goog.events.unlisten(body, 'click', captureHandler, true); |
| 715 |
}); |
| 716 |
var bubbleHandler2 = goog.testing.recordFunction(); |
| 717 |
|
| 718 |
try { |
| 719 |
goog.events.listen(body, 'click', captureHandler, true); |
| 720 |
goog.events.listen(div, 'click', bubbleHandler1, false); |
| 721 |
goog.events.listen(body, 'click', bubbleHandler2, false); |
| 722 |
|
| 723 |
dispatchClick(div); |
| 724 |
assertEquals(1, captureHandler.getCallCount()); |
| 725 |
|
| 726 |
// Verify that neither of these handlers are called more than once. |
| 727 |
assertEquals(1, bubbleHandler1.getCallCount()); |
| 728 |
assertEquals(1, bubbleHandler2.getCallCount()); |
| 729 |
} finally { |
| 730 |
goog.dom.removeNode(div); |
| 731 |
goog.events.removeAll(body); |
| 732 |
goog.events.removeAll(div); |
| 733 |
} |
| 734 |
} |
| 735 |
|
| 736 |
function testCaptureSimulationModeOffAndFail() { |
| 737 |
goog.events.CAPTURE_SIMULATION_MODE = |
| 738 |
goog.events.CaptureSimulationMode.OFF_AND_FAIL; |
| 739 |
var captureHandler = goog.testing.recordFunction(); |
| 740 |
|
| 741 |
if (!goog.events.BrowserFeature.HAS_W3C_EVENT_SUPPORT) { |
| 742 |
var err = assertThrows(function() { |
| 743 |
goog.events.listen(document.body, 'click', captureHandler, true); |
| 744 |
}); |
| 745 |
assertTrue(err instanceof goog.asserts.AssertionError); |
| 746 |
|
| 747 |
// Sanity tests. |
| 748 |
dispatchClick(document.body); |
| 749 |
assertEquals(0, captureHandler.getCallCount()); |
| 750 |
} else { |
| 751 |
goog.events.listen(document.body, 'click', captureHandler, true); |
| 752 |
dispatchClick(document.body); |
| 753 |
assertEquals(1, captureHandler.getCallCount()); |
| 754 |
} |
| 755 |
} |
| 756 |
|
| 757 |
function testCaptureSimulationModeOffAndSilent() { |
| 758 |
goog.events.CAPTURE_SIMULATION_MODE = |
| 759 |
goog.events.CaptureSimulationMode.OFF_AND_SILENT; |
| 760 |
var captureHandler = goog.testing.recordFunction(); |
| 761 |
|
| 762 |
goog.events.listen(document.body, 'click', captureHandler, true); |
| 763 |
if (!goog.events.BrowserFeature.HAS_W3C_EVENT_SUPPORT) { |
| 764 |
dispatchClick(document.body); |
| 765 |
assertEquals(0, captureHandler.getCallCount()); |
| 766 |
} else { |
| 767 |
dispatchClick(document.body); |
| 768 |
assertEquals(1, captureHandler.getCallCount()); |
| 769 |
} |
| 770 |
} |