domemittermixin.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global document, Event, MouseEvent */
  6. /* bender-tags: ui */
  7. 'use strict';
  8. import testUtils from '/tests/ckeditor5/_utils/utils.js';
  9. import extend from '/ckeditor5/utils/lib/lodash/extend.js';
  10. import DOMEmitterMixin from '/ckeditor5/ui/domemittermixin.js';
  11. import EmitterMixin from '/ckeditor5/utils/emittermixin.js';
  12. let emitter, domEmitter, node;
  13. testUtils.createSinonSandbox();
  14. const getEmitterInstance = () => extend( {}, EmitterMixin );
  15. const getDOMEmitterInstance = () => extend( {}, DOMEmitterMixin );
  16. const getDOMNodeInstance = () => document.createElement( 'div' );
  17. function updateEmitterInstance() {
  18. emitter = getEmitterInstance();
  19. }
  20. function updateDOMEmitterInstance() {
  21. domEmitter = getDOMEmitterInstance();
  22. }
  23. function updateDOMNodeInstance() {
  24. node = getDOMNodeInstance();
  25. }
  26. describe( 'DOMEmitterMixin', () => {
  27. beforeEach( () => {
  28. updateEmitterInstance();
  29. updateDOMEmitterInstance();
  30. updateDOMNodeInstance();
  31. } );
  32. describe( 'listenTo', () => {
  33. it( 'should listen to EmitterMixin events', () => {
  34. const spy = testUtils.sinon.spy();
  35. domEmitter.listenTo( emitter, 'test', spy );
  36. emitter.fire( 'test' );
  37. sinon.assert.calledOnce( spy );
  38. } );
  39. it( 'should listen to native DOM events', () => {
  40. const spy = testUtils.sinon.spy();
  41. domEmitter.listenTo( node, 'test', spy );
  42. node.dispatchEvent( new Event( 'test' ) );
  43. sinon.assert.calledOnce( spy );
  44. } );
  45. it( 'should listen to native DOM events - window as source', () => {
  46. const spy = testUtils.sinon.spy();
  47. domEmitter.listenTo( window, 'test', spy );
  48. window.dispatchEvent( new Event( 'test' ) );
  49. sinon.assert.calledOnce( spy );
  50. } );
  51. it( 'should listen to native DOM events - document as source', () => {
  52. const spy = testUtils.sinon.spy();
  53. domEmitter.listenTo( document, 'test', spy );
  54. document.dispatchEvent( new Event( 'test' ) );
  55. sinon.assert.calledOnce( spy );
  56. } );
  57. // #187
  58. it( 'should work for DOM Nodes belonging to another window', () => {
  59. const spy = testUtils.sinon.spy();
  60. const iframe = document.createElement( 'iframe' );
  61. document.body.appendChild( iframe );
  62. const iframeNode = iframe.contentWindow.document.createElement( 'div' );
  63. domEmitter.listenTo( iframeNode, 'test', spy );
  64. iframeNode.dispatchEvent( new Event( 'test' ) );
  65. sinon.assert.calledOnce( spy );
  66. } );
  67. } );
  68. describe( 'stopListening', () => {
  69. it( 'should stop listening to a specific event callback', () => {
  70. const spy1 = testUtils.sinon.spy();
  71. const spy2 = testUtils.sinon.spy();
  72. domEmitter.listenTo( node, 'event1', spy1 );
  73. domEmitter.listenTo( node, 'event2', spy2 );
  74. node.dispatchEvent( new Event( 'event1' ) );
  75. node.dispatchEvent( new Event( 'event2' ) );
  76. domEmitter.stopListening( node, 'event1', spy1 );
  77. node.dispatchEvent( new Event( 'event1' ) );
  78. node.dispatchEvent( new Event( 'event2' ) );
  79. sinon.assert.calledOnce( spy1 );
  80. sinon.assert.calledTwice( spy2 );
  81. } );
  82. it( 'should stop listening to an specific event', () => {
  83. const spy1a = testUtils.sinon.spy();
  84. const spy1b = testUtils.sinon.spy();
  85. const spy2 = testUtils.sinon.spy();
  86. domEmitter.listenTo( node, 'event1', spy1a );
  87. domEmitter.listenTo( node, 'event1', spy1b );
  88. domEmitter.listenTo( node, 'event2', spy2 );
  89. node.dispatchEvent( new Event( 'event1' ) );
  90. node.dispatchEvent( new Event( 'event2' ) );
  91. sinon.assert.calledOnce( spy1a );
  92. sinon.assert.calledOnce( spy1b );
  93. sinon.assert.calledOnce( spy2 );
  94. domEmitter.stopListening( node, 'event1' );
  95. node.dispatchEvent( new Event( 'event1' ) );
  96. node.dispatchEvent( new Event( 'event2' ) );
  97. sinon.assert.calledOnce( spy1a );
  98. sinon.assert.calledOnce( spy1b );
  99. sinon.assert.calledTwice( spy2 );
  100. } );
  101. it( 'should stop listening to all events from a specific node', () => {
  102. const spy1 = testUtils.sinon.spy();
  103. const spy2 = testUtils.sinon.spy();
  104. domEmitter.listenTo( node, 'event1', spy1 );
  105. domEmitter.listenTo( node, 'event2', spy2 );
  106. node.dispatchEvent( new Event( 'event1' ) );
  107. node.dispatchEvent( new Event( 'event2' ) );
  108. domEmitter.stopListening( node );
  109. node.dispatchEvent( new Event( 'event1' ) );
  110. node.dispatchEvent( new Event( 'event2' ) );
  111. sinon.assert.calledOnce( spy1 );
  112. sinon.assert.calledOnce( spy2 );
  113. } );
  114. it( 'should stop listening to everything', () => {
  115. const spy1 = testUtils.sinon.spy();
  116. const spy2 = testUtils.sinon.spy();
  117. const node1 = getDOMNodeInstance();
  118. const node2 = getDOMNodeInstance();
  119. domEmitter.listenTo( node1, 'event1', spy1 );
  120. domEmitter.listenTo( node2, 'event2', spy2 );
  121. expect( domEmitter ).to.have.property( '_listeningTo' );
  122. node1.dispatchEvent( new Event( 'event1' ) );
  123. node2.dispatchEvent( new Event( 'event2' ) );
  124. domEmitter.stopListening();
  125. node1.dispatchEvent( new Event( 'event1' ) );
  126. node2.dispatchEvent( new Event( 'event2' ) );
  127. sinon.assert.calledOnce( spy1 );
  128. sinon.assert.calledOnce( spy2 );
  129. expect( domEmitter ).to.not.have.property( '_listeningTo' );
  130. } );
  131. it( 'should not stop other nodes when a non-listened node is provided', () => {
  132. const spy = testUtils.sinon.spy();
  133. const node1 = getDOMNodeInstance();
  134. const node2 = getDOMNodeInstance();
  135. domEmitter.listenTo( node1, 'test', spy );
  136. domEmitter.stopListening( node2 );
  137. node1.dispatchEvent( new Event( 'test' ) );
  138. sinon.assert.called( spy );
  139. } );
  140. it( 'should pass DOM Event data to the listener', () => {
  141. const spy = testUtils.sinon.spy();
  142. const node = getDOMNodeInstance();
  143. domEmitter.listenTo( node, 'click', spy );
  144. const mouseEvent = new MouseEvent( 'click', {
  145. screenX: 10,
  146. screenY: 20
  147. } );
  148. node.dispatchEvent( mouseEvent );
  149. sinon.assert.calledOnce( spy );
  150. expect( spy.args[ 0 ][ 1 ] ).to.be.equal( mouseEvent );
  151. } );
  152. it( 'should detach native DOM event listener proxy, specific event', () => {
  153. const spy1a = testUtils.sinon.spy();
  154. const spy1b = testUtils.sinon.spy();
  155. domEmitter.listenTo( node, 'test', spy1a );
  156. const proxyEmitter = domEmitter._getProxyEmitter( node );
  157. const spy2 = testUtils.sinon.spy( proxyEmitter, 'fire' );
  158. node.dispatchEvent( new Event( 'test' ) );
  159. sinon.assert.calledOnce( spy1a );
  160. sinon.assert.calledOnce( spy2 );
  161. domEmitter.stopListening( node, 'test' );
  162. node.dispatchEvent( new Event( 'test' ) );
  163. sinon.assert.calledOnce( spy1a );
  164. sinon.assert.calledOnce( spy2 );
  165. // Attach same event again.
  166. domEmitter.listenTo( node, 'test', spy1b );
  167. node.dispatchEvent( new Event( 'test' ) );
  168. expect( proxyEmitter ).to.be.equal( domEmitter._getProxyEmitter( node ) );
  169. sinon.assert.calledOnce( spy1a );
  170. sinon.assert.calledOnce( spy1b );
  171. sinon.assert.calledTwice( spy2 );
  172. } );
  173. it( 'should detach native DOM event listener proxy, specific callback', () => {
  174. const spy1a = testUtils.sinon.spy();
  175. const spy1b = testUtils.sinon.spy();
  176. const spy1c = testUtils.sinon.spy();
  177. domEmitter.listenTo( node, 'test', spy1a );
  178. domEmitter.listenTo( node, 'test', spy1b );
  179. const proxyEmitter = domEmitter._getProxyEmitter( node );
  180. const spy2 = testUtils.sinon.spy( proxyEmitter, 'fire' );
  181. node.dispatchEvent( new Event( 'test' ) );
  182. sinon.assert.calledOnce( spy1a );
  183. sinon.assert.calledOnce( spy1b );
  184. sinon.assert.calledOnce( spy2 );
  185. domEmitter.stopListening( node, 'test', spy1a );
  186. node.dispatchEvent( new Event( 'test' ) );
  187. sinon.assert.calledOnce( spy1a );
  188. sinon.assert.calledTwice( spy1b );
  189. sinon.assert.calledTwice( spy2 );
  190. domEmitter.stopListening( node, 'test', spy1b );
  191. node.dispatchEvent( new Event( 'test' ) );
  192. sinon.assert.calledOnce( spy1a );
  193. sinon.assert.calledTwice( spy1b );
  194. sinon.assert.calledTwice( spy2 );
  195. // Attach same event again.
  196. domEmitter.listenTo( node, 'test', spy1c );
  197. node.dispatchEvent( new Event( 'test' ) );
  198. expect( proxyEmitter ).to.be.equal( domEmitter._getProxyEmitter( node ) );
  199. sinon.assert.calledOnce( spy1a );
  200. sinon.assert.calledTwice( spy1b );
  201. sinon.assert.calledOnce( spy1c );
  202. sinon.assert.calledThrice( spy2 );
  203. } );
  204. it( 'should detach native DOM event listener proxy, specific emitter', () => {
  205. const spy1a = testUtils.sinon.spy();
  206. const spy1b = testUtils.sinon.spy();
  207. const spy1c = testUtils.sinon.spy();
  208. const spy1d = testUtils.sinon.spy();
  209. domEmitter.listenTo( node, 'test1', spy1a );
  210. domEmitter.listenTo( node, 'test2', spy1b );
  211. const proxyEmitter = domEmitter._getProxyEmitter( node );
  212. const spy2 = testUtils.sinon.spy( proxyEmitter, 'fire' );
  213. node.dispatchEvent( new Event( 'test1' ) );
  214. node.dispatchEvent( new Event( 'test2' ) );
  215. sinon.assert.calledOnce( spy1a );
  216. sinon.assert.calledOnce( spy1b );
  217. sinon.assert.calledTwice( spy2 );
  218. domEmitter.stopListening( node );
  219. node.dispatchEvent( new Event( 'test1' ) );
  220. node.dispatchEvent( new Event( 'test2' ) );
  221. sinon.assert.calledOnce( spy1a );
  222. sinon.assert.calledOnce( spy1b );
  223. sinon.assert.calledTwice( spy2 );
  224. // Attach same event again.
  225. domEmitter.listenTo( node, 'test1', spy1c );
  226. domEmitter.listenTo( node, 'test2', spy1d );
  227. // Old proxy emitter died when stopped listening to the node.
  228. const proxyEmitter2 = domEmitter._getProxyEmitter( node );
  229. const spy3 = testUtils.sinon.spy( proxyEmitter2, 'fire' );
  230. node.dispatchEvent( new Event( 'test1' ) );
  231. node.dispatchEvent( new Event( 'test2' ) );
  232. expect( proxyEmitter ).to.not.be.equal( proxyEmitter2 );
  233. sinon.assert.calledOnce( spy1a );
  234. sinon.assert.calledOnce( spy1b );
  235. sinon.assert.calledOnce( spy1c );
  236. sinon.assert.calledOnce( spy1d );
  237. sinon.assert.calledTwice( spy2 );
  238. sinon.assert.calledTwice( spy3 );
  239. } );
  240. it( 'should detach native DOM event listener proxy, everything', () => {
  241. const spy1a = testUtils.sinon.spy();
  242. const spy1b = testUtils.sinon.spy();
  243. const spy1c = testUtils.sinon.spy();
  244. const spy1d = testUtils.sinon.spy();
  245. domEmitter.listenTo( node, 'test1', spy1a );
  246. domEmitter.listenTo( node, 'test2', spy1b );
  247. const proxyEmitter = domEmitter._getProxyEmitter( node );
  248. const spy2 = testUtils.sinon.spy( proxyEmitter, 'fire' );
  249. node.dispatchEvent( new Event( 'test1' ) );
  250. node.dispatchEvent( new Event( 'test2' ) );
  251. sinon.assert.calledOnce( spy1a );
  252. sinon.assert.calledOnce( spy1b );
  253. sinon.assert.calledTwice( spy2 );
  254. domEmitter.stopListening();
  255. node.dispatchEvent( new Event( 'test1' ) );
  256. node.dispatchEvent( new Event( 'test2' ) );
  257. sinon.assert.calledOnce( spy1a );
  258. sinon.assert.calledOnce( spy1b );
  259. sinon.assert.calledTwice( spy2 );
  260. // Attach same event again.
  261. domEmitter.listenTo( node, 'test1', spy1c );
  262. domEmitter.listenTo( node, 'test2', spy1d );
  263. // Old proxy emitter died when stopped listening to the node.
  264. const proxyEmitter2 = domEmitter._getProxyEmitter( node );
  265. const spy3 = testUtils.sinon.spy( proxyEmitter2, 'fire' );
  266. node.dispatchEvent( new Event( 'test1' ) );
  267. node.dispatchEvent( new Event( 'test2' ) );
  268. expect( proxyEmitter ).to.not.be.equal( proxyEmitter2 );
  269. sinon.assert.calledOnce( spy1a );
  270. sinon.assert.calledOnce( spy1b );
  271. sinon.assert.calledOnce( spy1c );
  272. sinon.assert.calledOnce( spy1d );
  273. sinon.assert.calledTwice( spy2 );
  274. sinon.assert.calledTwice( spy3 );
  275. } );
  276. // #187
  277. it( 'should work for DOM Nodes belonging to another window', () => {
  278. const spy = testUtils.sinon.spy();
  279. const iframe = document.createElement( 'iframe' );
  280. document.body.appendChild( iframe );
  281. const iframeNode = iframe.contentWindow.document.createElement( 'div' );
  282. domEmitter.listenTo( iframeNode, 'test', spy );
  283. iframeNode.dispatchEvent( new Event( 'test' ) );
  284. domEmitter.stopListening( iframeNode );
  285. iframeNode.dispatchEvent( new Event( 'test' ) );
  286. sinon.assert.calledOnce( spy );
  287. } );
  288. } );
  289. } );