actioneventwrapper_test.js 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. // Copyright 2009 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.events.actionEventWrapperTest');
  15. goog.setTestOnly('goog.events.actionEventWrapperTest');
  16. goog.require('goog.a11y.aria');
  17. goog.require('goog.a11y.aria.Role');
  18. goog.require('goog.events');
  19. goog.require('goog.events.EventHandler');
  20. goog.require('goog.events.KeyCodes');
  21. goog.require('goog.events.actionEventWrapper');
  22. goog.require('goog.testing.events');
  23. goog.require('goog.testing.jsunit');
  24. var a, eh, events;
  25. function setUpPage() {
  26. a = document.getElementById('a');
  27. }
  28. function setUp() {
  29. events = [];
  30. eh = new goog.events.EventHandler();
  31. assertEquals(
  32. 'No listeners registered yet', 0, goog.events.getListeners(a).length);
  33. }
  34. function tearDown() {
  35. eh.dispose();
  36. }
  37. var Foo = function() {};
  38. Foo.prototype.test = function(e) {
  39. events.push(e);
  40. };
  41. function assertListenersExist(el, listenerCount, capt) {
  42. var EVENT_TYPES = goog.events.ActionEventWrapper_.EVENT_TYPES_;
  43. for (var i = 0; i < EVENT_TYPES.length; ++i) {
  44. assertEquals(
  45. listenerCount,
  46. goog.events.getListeners(el, EVENT_TYPES[i], capt).length);
  47. }
  48. }
  49. function testAddActionListener() {
  50. var listener = function(e) { events.push(e); };
  51. goog.events.listenWithWrapper(a, goog.events.actionEventWrapper, listener);
  52. assertListenersExist(a, 1, false);
  53. goog.testing.events.fireClickSequence(a);
  54. assertEquals('1 event should have been dispatched', 1, events.length);
  55. assertEquals('Should be an click event', 'click', events[0].type);
  56. goog.testing.events.fireKeySequence(a, goog.events.KeyCodes.ENTER);
  57. assertEquals('2 events should have been dispatched', 2, events.length);
  58. assertEquals('Should be a keypress event', 'keypress', events[1].type);
  59. goog.a11y.aria.setRole(
  60. /** @type {!Element} */ (a), goog.a11y.aria.Role.BUTTON);
  61. goog.testing.events.fireKeySequence(a, goog.events.KeyCodes.SPACE);
  62. assertEquals('3 events should have been dispatched', 3, events.length);
  63. assertEquals('Should be a keyup event', 'keyup', events[2].type);
  64. assertTrue('Should be default prevented.', events[2].defaultPrevented);
  65. goog.a11y.aria.removeRole(/** @type {!Element} */ (a));
  66. goog.testing.events.fireKeySequence(a, goog.events.KeyCodes.SPACE);
  67. assertEquals('3 events should have been dispatched', 3, events.length);
  68. goog.testing.events.fireKeySequence(a, goog.events.KeyCodes.ESC);
  69. assertEquals('3 events should have been dispatched', 3, events.length);
  70. goog.a11y.aria.setRole(
  71. /** @type {!Element} */ (a), goog.a11y.aria.Role.TAB);
  72. goog.testing.events.fireKeySequence(a, goog.events.KeyCodes.SPACE);
  73. assertEquals('4 events should have been dispatched', 4, events.length);
  74. assertEquals('Should be a keyup event', 'keyup', events[2].type);
  75. assertTrue('Should be default prevented.', events[2].defaultPrevented);
  76. goog.a11y.aria.removeRole(/** @type {!Element} */ (a));
  77. goog.testing.events.fireKeySequence(a, goog.events.KeyCodes.SPACE);
  78. assertEquals('4 events should have been dispatched', 4, events.length);
  79. goog.testing.events.fireKeySequence(a, goog.events.KeyCodes.ESC);
  80. assertEquals('4 events should have been dispatched', 4, events.length);
  81. goog.events.unlistenWithWrapper(a, goog.events.actionEventWrapper, listener);
  82. assertListenersExist(a, 0, false);
  83. }
  84. function testAddActionListenerForHandleEvent() {
  85. var listener = {handleEvent: function(e) { events.push(e); }};
  86. goog.events.listenWithWrapper(a, goog.events.actionEventWrapper, listener);
  87. assertListenersExist(a, 1, false);
  88. goog.testing.events.fireClickSequence(a);
  89. assertEquals('1 event should have been dispatched', 1, events.length);
  90. assertEquals('Should be an click event', 'click', events[0].type);
  91. goog.testing.events.fireKeySequence(a, goog.events.KeyCodes.ENTER);
  92. assertEquals('2 events should have been dispatched', 2, events.length);
  93. assertEquals('Should be a keypress event', 'keypress', events[1].type);
  94. goog.a11y.aria.setRole(
  95. /** @type {!Element} */ (a), goog.a11y.aria.Role.BUTTON);
  96. goog.testing.events.fireKeySequence(a, goog.events.KeyCodes.SPACE);
  97. assertEquals('3 events should have been dispatched', 3, events.length);
  98. assertEquals('Should be a keyup event', 'keyup', events[2].type);
  99. assertTrue('Should be default prevented.', events[2].defaultPrevented);
  100. goog.a11y.aria.removeRole(/** @type {!Element} */ (a));
  101. goog.testing.events.fireKeySequence(a, goog.events.KeyCodes.SPACE);
  102. assertEquals('3 events should have been dispatched', 3, events.length);
  103. goog.testing.events.fireKeySequence(a, goog.events.KeyCodes.ESC);
  104. assertEquals('3 events should have been dispatched', 3, events.length);
  105. goog.events.unlistenWithWrapper(a, goog.events.actionEventWrapper, listener);
  106. assertListenersExist(a, 0, false);
  107. }
  108. function testAddActionListenerInCaptPhase() {
  109. var count = 0;
  110. var captListener = function(e) {
  111. events.push(e);
  112. assertEquals(0, count);
  113. count++;
  114. };
  115. var bubbleListener = function(e) {
  116. events.push(e);
  117. assertEquals(1, count);
  118. count = 0;
  119. };
  120. goog.events.listenWithWrapper(
  121. a, goog.events.actionEventWrapper, captListener, true);
  122. goog.events.listenWithWrapper(
  123. a, goog.events.actionEventWrapper, bubbleListener);
  124. assertListenersExist(a, 1, false);
  125. assertListenersExist(a, 1, true);
  126. goog.testing.events.fireClickSequence(a);
  127. assertEquals('2 event should have been dispatched', 2, events.length);
  128. assertEquals('Should be an click event', 'click', events[0].type);
  129. goog.testing.events.fireKeySequence(a, goog.events.KeyCodes.ENTER);
  130. assertEquals('4 events should have been dispatched', 4, events.length);
  131. assertEquals('Should be a keypress event', 'keypress', events[2].type);
  132. goog.testing.events.fireKeySequence(a, goog.events.KeyCodes.SPACE);
  133. assertEquals('4 events should have been dispatched', 4, events.length);
  134. goog.testing.events.fireKeySequence(a, goog.events.KeyCodes.ESC);
  135. assertEquals('4 events should have been dispatched', 4, events.length);
  136. goog.events.unlistenWithWrapper(
  137. a, goog.events.actionEventWrapper, captListener, true);
  138. goog.events.unlistenWithWrapper(
  139. a, goog.events.actionEventWrapper, bubbleListener);
  140. assertListenersExist(a, 0, false);
  141. assertListenersExist(a, 0, true);
  142. }
  143. function testRemoveActionListener() {
  144. var listener1 = function(e) { events.push(e); };
  145. var listener2 = function(e) { events.push({type: 'err'}); };
  146. goog.events.listenWithWrapper(a, goog.events.actionEventWrapper, listener1);
  147. assertListenersExist(a, 1, false);
  148. goog.events.listenWithWrapper(a, goog.events.actionEventWrapper, listener2);
  149. assertListenersExist(a, 2, false);
  150. goog.testing.events.fireKeySequence(a, goog.events.KeyCodes.ENTER);
  151. assertEquals('2 events should have been dispatched', 2, events.length);
  152. assertEquals('Should be a keypress event', 'keypress', events[0].type);
  153. assertEquals('Should be an err event', 'err', events[1].type);
  154. goog.events.unlistenWithWrapper(a, goog.events.actionEventWrapper, listener2);
  155. assertListenersExist(a, 1, false);
  156. events = [];
  157. goog.testing.events.fireKeySequence(a, goog.events.KeyCodes.ENTER);
  158. assertEquals('1 event should have been dispatched', 1, events.length);
  159. assertEquals('Should be a keypress event', 'keypress', events[0].type);
  160. goog.events.unlistenWithWrapper(a, goog.events.actionEventWrapper, listener1);
  161. assertListenersExist(a, 0, false);
  162. }
  163. function testEventHandlerActionListener() {
  164. var listener = function(e) { events.push(e); };
  165. eh.listenWithWrapper(a, goog.events.actionEventWrapper, listener);
  166. assertListenersExist(a, 1, false);
  167. goog.testing.events.fireClickSequence(a);
  168. assertEquals('1 event should have been dispatched', 1, events.length);
  169. assertEquals('Should be an click event', 'click', events[0].type);
  170. goog.testing.events.fireKeySequence(a, goog.events.KeyCodes.ENTER);
  171. assertEquals('2 events should have been dispatched', 2, events.length);
  172. assertEquals('Should be a keypress event', 'keypress', events[1].type);
  173. goog.testing.events.fireKeySequence(a, goog.events.KeyCodes.SPACE);
  174. assertEquals('2 events should have been dispatched', 2, events.length);
  175. goog.testing.events.fireKeySequence(a, goog.events.KeyCodes.ESC);
  176. assertEquals('2 events should have been dispatched', 2, events.length);
  177. eh.unlistenWithWrapper(a, goog.events.actionEventWrapper, listener);
  178. assertListenersExist(a, 0, false);
  179. }
  180. function testEventHandlerActionListenerWithScope() {
  181. var foo = new Foo();
  182. var eh2 = new goog.events.EventHandler(foo);
  183. eh2.listenWithWrapper(a, goog.events.actionEventWrapper, foo.test);
  184. assertListenersExist(a, 1, false);
  185. goog.testing.events.fireClickSequence(a);
  186. assertEquals('1 event should have been dispatched', 1, events.length);
  187. assertEquals('Should be an click event', 'click', events[0].type);
  188. goog.testing.events.fireKeySequence(a, goog.events.KeyCodes.ENTER);
  189. assertEquals('2 events should have been dispatched', 2, events.length);
  190. assertEquals('Should be a keypress event', 'keypress', events[1].type);
  191. goog.testing.events.fireKeySequence(a, goog.events.KeyCodes.SPACE);
  192. assertEquals('2 events should have been dispatched', 2, events.length);
  193. goog.testing.events.fireKeySequence(a, goog.events.KeyCodes.ESC);
  194. assertEquals('2 events should have been dispatched', 2, events.length);
  195. eh2.dispose();
  196. assertListenersExist(a, 0, false);
  197. delete foo;
  198. }