events_test.js 20 KB

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