fakeselectionobserver.js 6.3 KB

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