8
0

fakeselectionobserver.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document, setTimeout */
  6. import createElement from '@ckeditor/ckeditor5-utils/src/dom/createelement';
  7. import FakeSelectionObserver from '../../../src/view/observer/fakeselectionobserver';
  8. import ViewDocument from '../../../src/view/document';
  9. import DomEventData from '../../../src/view/observer/domeventdata';
  10. import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  11. import { setData, stringify } from '../../../src/dev-utils/view';
  12. describe( 'FakeSelectionObserver', () => {
  13. let observer, viewDocument, root, domRoot;
  14. before( () => {
  15. domRoot = createElement( document, 'div', {
  16. contenteditable: 'true'
  17. } );
  18. document.body.appendChild( domRoot );
  19. } );
  20. after( () => {
  21. domRoot.parentElement.removeChild( domRoot );
  22. } );
  23. beforeEach( () => {
  24. viewDocument = new ViewDocument();
  25. root = viewDocument.createRoot( domRoot );
  26. observer = viewDocument.getObserver( FakeSelectionObserver );
  27. viewDocument.selection.setFake();
  28. } );
  29. afterEach( () => {
  30. viewDocument.destroy();
  31. } );
  32. it( 'should do nothing if selection is not fake', () => {
  33. viewDocument.selection.setFake( false );
  34. return checkEventPrevention( keyCodes.arrowleft, false );
  35. } );
  36. it( 'should do nothing if is disabled', () => {
  37. observer.disable();
  38. return checkEventPrevention( keyCodes.arrowleft, false );
  39. } );
  40. it( 'should prevent default for left arrow key', ( ) => {
  41. return checkEventPrevention( keyCodes.arrowleft );
  42. } );
  43. it( 'should prevent default for right arrow key', ( ) => {
  44. return checkEventPrevention( keyCodes.arrowright );
  45. } );
  46. it( 'should prevent default for up arrow key', ( ) => {
  47. return checkEventPrevention( keyCodes.arrowup );
  48. } );
  49. it( 'should prevent default for down arrow key', ( ) => {
  50. return checkEventPrevention( keyCodes.arrowdown );
  51. } );
  52. it( 'should fire selectionChange event with new selection when left arrow key is pressed', () => {
  53. return checkSelectionChange(
  54. '<container:p>foo[<strong>bar</strong>]baz</container:p>',
  55. keyCodes.arrowleft,
  56. '<container:p>foo[]<strong>bar</strong>baz</container:p>'
  57. );
  58. } );
  59. it( 'should fire selectionChange event with new selection when right arrow key is pressed', () => {
  60. return checkSelectionChange(
  61. '<container:p>foo[<strong>bar</strong>]baz</container:p>',
  62. keyCodes.arrowright,
  63. '<container:p>foo<strong>bar</strong>[]baz</container:p>'
  64. );
  65. } );
  66. it( 'should fire selectionChange event with new selection when up arrow key is pressed', () => {
  67. return checkSelectionChange(
  68. '<container:p>foo[<strong>bar</strong>]baz</container:p>',
  69. keyCodes.arrowup,
  70. '<container:p>foo[]<strong>bar</strong>baz</container:p>'
  71. );
  72. } );
  73. it( 'should fire selectionChange event with new selection when down arrow key is pressed', () => {
  74. return checkSelectionChange(
  75. '<container:p>foo[<strong>bar</strong>]baz</container:p>',
  76. keyCodes.arrowdown,
  77. '<container:p>foo<strong>bar</strong>[]baz</container:p>'
  78. );
  79. } );
  80. it( 'should fire `selectionChangeDone` event after selection stop changing', done => {
  81. const spy = sinon.spy();
  82. viewDocument.on( 'selectionChangeDone', spy );
  83. // Change selection.
  84. changeFakeSelectionPressing( keyCodes.arrowdown );
  85. // Wait 100ms.
  86. // Note that it's difficult/not possible to test lodash#debounce with sinon fake timers.
  87. // See: https://github.com/lodash/lodash/issues/304
  88. setTimeout( () => {
  89. // Check if spy was called.
  90. expect( spy.notCalled ).to.true;
  91. // Change selection one more time.
  92. changeFakeSelectionPressing( keyCodes.arrowdown );
  93. // Wait 210ms (debounced function should be called).
  94. setTimeout( () => {
  95. expect( spy.calledOnce ).to.true;
  96. done();
  97. }, 210 );
  98. }, 100 );
  99. } );
  100. it( 'should not fire `selectionChangeDone` event when observer will be destroyed', done => {
  101. const spy = sinon.spy();
  102. viewDocument.on( 'selectionChangeDone', spy );
  103. // Change selection.
  104. changeFakeSelectionPressing( keyCodes.arrowdown );
  105. // Wait 100ms.
  106. // Note that it's difficult/not possible to test lodash#debounce with sinon fake timers.
  107. // See: https://github.com/lodash/lodash/issues/304
  108. setTimeout( () => {
  109. // And destroy observer.
  110. observer.destroy();
  111. // Wait another 110ms.
  112. setTimeout( () => {
  113. // Check that event won't be called.
  114. expect( spy.notCalled ).to.true;
  115. done();
  116. }, 110 );
  117. }, 100 );
  118. } );
  119. // Checks if preventDefault method was called by FakeSelectionObserver for specified key code.
  120. //
  121. // @param {Number} keyCode
  122. // @param {Boolean} shouldPrevent If set to true method checks if event was prevented.
  123. // @returns {Promise}
  124. function checkEventPrevention( keyCode, shouldPrevent = true ) {
  125. return new Promise( resolve => {
  126. const data = {
  127. keyCode,
  128. preventDefault: sinon.spy(),
  129. };
  130. viewDocument.once( 'keydown', () => {
  131. if ( shouldPrevent ) {
  132. sinon.assert.calledOnce( data.preventDefault );
  133. } else {
  134. sinon.assert.notCalled( data.preventDefault );
  135. }
  136. resolve();
  137. }, { priority: 'lowest' } );
  138. viewDocument.fire( 'keydown', new DomEventData( viewDocument, { target: document.body }, data ) );
  139. } );
  140. }
  141. // Checks if proper selectionChange event is fired by FakeSelectionObserver for specified key.
  142. //
  143. // @param {String} initialData
  144. // @param {Number} keyCode
  145. // @param {String} output
  146. // @returns {Promise}
  147. function checkSelectionChange( initialData, keyCode, output ) {
  148. return new Promise( resolve => {
  149. viewDocument.once( 'selectionChange', ( eventInfo, data ) => {
  150. expect( stringify( root.getChild( 0 ), data.newSelection, { showType: true } ) ).to.equal( output );
  151. resolve();
  152. } );
  153. setData( viewDocument, initialData );
  154. changeFakeSelectionPressing( keyCode );
  155. } );
  156. }
  157. // Sets fake selection to the document and fire `keydown` event what cause `selectionChange` event.
  158. //
  159. // @param {Number} keyCode
  160. function changeFakeSelectionPressing( keyCode ) {
  161. viewDocument.selection.setFake();
  162. const data = {
  163. keyCode,
  164. preventDefault: sinon.spy(),
  165. };
  166. viewDocument.fire( 'keydown', new DomEventData( viewDocument, { target: document.body }, data ) );
  167. }
  168. } );