eventhandler_test.js 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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. goog.provide('goog.events.EventHandlerTest');
  15. goog.setTestOnly('goog.events.EventHandlerTest');
  16. goog.require('goog.events');
  17. goog.require('goog.events.EventHandler');
  18. goog.require('goog.events.EventTarget');
  19. goog.require('goog.testing.PropertyReplacer');
  20. goog.require('goog.testing.jsunit');
  21. goog.require('goog.testing.recordFunction');
  22. var a, b, c, d, eh;
  23. var propertyReplacer;
  24. function setUpPage() {
  25. a = document.getElementById('a');
  26. b = document.getElementById('b');
  27. c = document.getElementById('c');
  28. d = document.getElementById('d');
  29. propertyReplacer = new goog.testing.PropertyReplacer();
  30. }
  31. function tearDown() {
  32. goog.dispose(eh);
  33. propertyReplacer.reset();
  34. }
  35. function testEventHandlerClearsListeners() {
  36. function tmp() {}
  37. goog.events.listen(a, 'click', tmp);
  38. assertEquals(1, goog.events.getListeners(a, 'click', false).length);
  39. eh = new goog.events.EventHandler();
  40. eh.listen(a, 'click');
  41. eh.listen(a, 'keypress');
  42. eh.listen(b, 'mouseover');
  43. eh.listen(c, 'mousedown');
  44. eh.listen(d, 'click');
  45. eh.listen(d, 'mousedown');
  46. assertEquals(2, goog.events.getListeners(a, 'click', false).length);
  47. assertEquals(1, goog.events.getListeners(a, 'keypress', false).length);
  48. assertEquals(1, goog.events.getListeners(b, 'mouseover', false).length);
  49. assertEquals(1, goog.events.getListeners(c, 'mousedown', false).length);
  50. assertEquals(1, goog.events.getListeners(d, 'click', false).length);
  51. assertEquals(1, goog.events.getListeners(d, 'mousedown', false).length);
  52. eh.unlisten(d, 'mousedown');
  53. assertEquals(2, goog.events.getListeners(a, 'click', false).length);
  54. assertEquals(1, goog.events.getListeners(a, 'keypress', false).length);
  55. assertEquals(1, goog.events.getListeners(b, 'mouseover', false).length);
  56. assertEquals(1, goog.events.getListeners(c, 'mousedown', false).length);
  57. assertEquals(1, goog.events.getListeners(d, 'click', false).length);
  58. assertEquals(0, goog.events.getListeners(d, 'mousedown', false).length);
  59. eh.dispose();
  60. assertEquals(1, goog.events.getListeners(a, 'click', false).length);
  61. assertEquals(0, goog.events.getListeners(a, 'keypress', false).length);
  62. assertEquals(0, goog.events.getListeners(b, 'mouseover', false).length);
  63. assertEquals(0, goog.events.getListeners(c, 'mousedown', false).length);
  64. assertEquals(0, goog.events.getListeners(d, 'click', false).length);
  65. assertEquals(0, goog.events.getListeners(d, 'mousedown', false).length);
  66. goog.events.unlisten(a, 'click', tmp);
  67. assertEquals(0, goog.events.getListeners(a, 'click', false).length);
  68. }
  69. function testListenArray() {
  70. eh = new goog.events.EventHandler();
  71. eh.listen(a, ['click', 'mousedown', 'mouseup']);
  72. assertEquals(1, goog.events.getListeners(a, 'click', false).length);
  73. assertEquals(1, goog.events.getListeners(a, 'mousedown', false).length);
  74. assertEquals(1, goog.events.getListeners(a, 'mouseup', false).length);
  75. eh.unlisten(a, ['click', 'mousedown', 'mouseup']);
  76. assertEquals(0, goog.events.getListeners(a, 'click', false).length);
  77. assertEquals(0, goog.events.getListeners(a, 'mousedown', false).length);
  78. assertEquals(0, goog.events.getListeners(a, 'mouseup', false).length);
  79. eh.listen(a, ['click', 'mousedown', 'mouseup']);
  80. assertEquals(1, goog.events.getListeners(a, 'click', false).length);
  81. assertEquals(1, goog.events.getListeners(a, 'mousedown', false).length);
  82. assertEquals(1, goog.events.getListeners(a, 'mouseup', false).length);
  83. eh.removeAll();
  84. assertEquals(0, goog.events.getListeners(a, 'click', false).length);
  85. assertEquals(0, goog.events.getListeners(a, 'mousedown', false).length);
  86. assertEquals(0, goog.events.getListeners(a, 'mouseup', false).length);
  87. }
  88. function testListenOnceRemovesListenerWhenFired() {
  89. var target = new goog.events.EventTarget();
  90. eh = new goog.events.EventHandler();
  91. var handler = goog.testing.recordFunction();
  92. eh.listenOnce(target, 'click', handler);
  93. target.dispatchEvent('click');
  94. assertEquals(
  95. 'One event should have been dispatched', 1, handler.getCallCount());
  96. target.dispatchEvent('click');
  97. assertEquals(
  98. 'No event should have been dispatched', 1, handler.getCallCount());
  99. }
  100. function testListenOnceListenerIsCleanedUp() {
  101. var target = new goog.events.EventTarget();
  102. eh = new goog.events.EventHandler();
  103. var handler = goog.testing.recordFunction();
  104. eh.listenOnce(target, 'click', handler);
  105. eh.removeAll();
  106. target.dispatchEvent('click');
  107. assertEquals(0, handler.getCallCount());
  108. }
  109. function testClearListenersWithListenOnceListenerRemoved() {
  110. var target = new goog.events.EventTarget();
  111. eh = new goog.events.EventHandler();
  112. var handler = goog.testing.recordFunction();
  113. eh.listenOnce(target, 'click', handler);
  114. assertNotNull(goog.events.getListener(target, 'click', handler, false, eh));
  115. target.dispatchEvent('click');
  116. assertEquals(
  117. 'One event should have been dispatched', 1, handler.getCallCount());
  118. assertNull(goog.events.getListener(target, 'click', handler, false, eh));
  119. eh.removeAll();
  120. target.dispatchEvent('click');
  121. assertEquals(
  122. 'No event should have been dispatched', 1, handler.getCallCount());
  123. }
  124. function testListenOnceArray() {
  125. var target = new goog.events.EventTarget();
  126. eh = new goog.events.EventHandler();
  127. var handler = goog.testing.recordFunction();
  128. eh.listenOnce(target, ['click', 'mousedown', 'mouseup'], handler);
  129. target.dispatchEvent('click');
  130. assertEquals(
  131. '1 event should have been dispatched', 1, handler.getCallCount());
  132. assertEquals(
  133. 'Should be a click event', 'click',
  134. handler.getLastCall().getArgument(0).type);
  135. target.dispatchEvent('click');
  136. assertEquals('No event should be dispatched', 1, handler.getCallCount());
  137. target.dispatchEvent('mouseup');
  138. assertEquals(
  139. '1 event should have been dispatched', 2, handler.getCallCount());
  140. assertEquals(
  141. 'Should be a mouseup event', 'mouseup',
  142. handler.getLastCall().getArgument(0).type);
  143. target.dispatchEvent('mouseup');
  144. assertEquals('No event should be dispatched', 2, handler.getCallCount());
  145. target.dispatchEvent('mousedown');
  146. assertEquals(
  147. '1 event should have been dispatched', 3, handler.getCallCount());
  148. assertEquals(
  149. 'Should be a mousedown event', 'mousedown',
  150. handler.getLastCall().getArgument(0).type);
  151. target.dispatchEvent('mousedown');
  152. assertEquals('No event should be dispatched', 3, handler.getCallCount());
  153. }
  154. function testListenUnlistenWithObjectHandler() {
  155. var target = new goog.events.EventTarget();
  156. eh = new goog.events.EventHandler();
  157. var handlerObj = {handleEvent: goog.testing.recordFunction()};
  158. eh.listen(target, 'click', handlerObj);
  159. target.dispatchEvent('click');
  160. assertEquals(
  161. 'One event should have been dispatched', 1,
  162. handlerObj.handleEvent.getCallCount());
  163. target.dispatchEvent('click');
  164. assertEquals(
  165. 'One event should have been dispatched', 2,
  166. handlerObj.handleEvent.getCallCount());
  167. eh.unlisten(target, 'click', handlerObj);
  168. target.dispatchEvent('click');
  169. assertEquals(
  170. 'No event should have been dispatched', 2,
  171. handlerObj.handleEvent.getCallCount());
  172. }
  173. function testListenOnceWithObjectHandler() {
  174. var target = new goog.events.EventTarget();
  175. eh = new goog.events.EventHandler();
  176. var handlerObj = {handleEvent: goog.testing.recordFunction()};
  177. eh.listenOnce(target, 'click', handlerObj);
  178. target.dispatchEvent('click');
  179. assertEquals(
  180. 'One event should have been dispatched', 1,
  181. handlerObj.handleEvent.getCallCount());
  182. target.dispatchEvent('click');
  183. assertEquals(
  184. 'No event should have been dispatched', 1,
  185. handlerObj.handleEvent.getCallCount());
  186. }
  187. function testGetListenerCount() {
  188. eh = new goog.events.EventHandler();
  189. assertEquals('0 listeners registered initially', 0, eh.getListenerCount());
  190. var target = new goog.events.EventTarget();
  191. eh.listen(target, 'click', goog.nullFunction, false);
  192. eh.listen(target, 'click', goog.nullFunction, true);
  193. assertEquals('2 listeners registered', 2, eh.getListenerCount());
  194. eh.unlisten(target, 'click', goog.nullFunction, true);
  195. assertEquals('1 listener removed, 1 left', 1, eh.getListenerCount());
  196. eh.removeAll();
  197. assertEquals('all listeners removed', 0, eh.getListenerCount());
  198. }
  199. function testRemoveAllCheckSForOwnKeys() {
  200. propertyReplacer.set(Object.prototype, 'customMethod', function() {
  201. throw new Error('NOOOOOOOOOOO');
  202. });
  203. eh = new goog.events.EventHandler();
  204. // If removeAll doesn't check keys using hasOwnProperty it will also try
  205. // unlisten using customMethod function as key and will throw error.
  206. eh.removeAll();
  207. }