fakeselectionobserver.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document */
  6. import FakeSelectionObserver from '/ckeditor5/engine/view/observer/fakeselectionobserver.js';
  7. import ViewDocument from '/ckeditor5/engine/view/document.js';
  8. import DomEventData from '/ckeditor5/engine/view/observer/domeventdata.js';
  9. import { keyCodes } from '/ckeditor5/utils/keyboard.js';
  10. import { setData, stringify } from '/ckeditor5/engine/dev-utils/view.js';
  11. describe( 'FakeSelectionObserver', () => {
  12. let observer;
  13. let viewDocument;
  14. let root;
  15. beforeEach( () => {
  16. viewDocument = new ViewDocument();
  17. root = viewDocument.createRoot( document.getElementById( 'main' ) );
  18. observer = viewDocument.getObserver( FakeSelectionObserver );
  19. viewDocument.selection.setFake();
  20. } );
  21. it( 'should do nothing if selection is not fake', () => {
  22. viewDocument.selection.setFake( false );
  23. return checkEventPrevention( keyCodes.arrowleft, false );
  24. } );
  25. it( 'should do nothing if is disabled', () => {
  26. observer.disable();
  27. return checkEventPrevention( keyCodes.arrowleft, false );
  28. } );
  29. it( 'should prevent default for left arrow key', ( ) => {
  30. return checkEventPrevention( keyCodes.arrowleft );
  31. } );
  32. it( 'should prevent default for right arrow key', ( ) => {
  33. return checkEventPrevention( keyCodes.arrowright );
  34. } );
  35. it( 'should prevent default for up arrow key', ( ) => {
  36. return checkEventPrevention( keyCodes.arrowup );
  37. } );
  38. it( 'should prevent default for down arrow key', ( ) => {
  39. return checkEventPrevention( keyCodes.arrowdown );
  40. } );
  41. it( 'should fire selectionChange event with new selection when left arrow key is pressed', () => {
  42. return checkSelectionChange(
  43. '<container:p>foo[<strong>bar</strong>]baz</container:p>',
  44. keyCodes.arrowleft,
  45. '<container:p>foo[]<strong>bar</strong>baz</container:p>'
  46. );
  47. } );
  48. it( 'should fire selectionChange event with new selection when right arrow key is pressed', () => {
  49. return checkSelectionChange(
  50. '<container:p>foo[<strong>bar</strong>]baz</container:p>',
  51. keyCodes.arrowright,
  52. '<container:p>foo<strong>bar</strong>[]baz</container:p>'
  53. );
  54. } );
  55. it( 'should fire selectionChange event with new selection when up arrow key is pressed', () => {
  56. return checkSelectionChange(
  57. '<container:p>foo[<strong>bar</strong>]baz</container:p>',
  58. keyCodes.arrowup,
  59. '<container:p>foo[]<strong>bar</strong>baz</container:p>'
  60. );
  61. } );
  62. it( 'should fire selectionChange event with new selection when down arrow key is pressed', () => {
  63. return checkSelectionChange(
  64. '<container:p>foo[<strong>bar</strong>]baz</container:p>',
  65. keyCodes.arrowdown,
  66. '<container:p>foo<strong>bar</strong>[]baz</container:p>'
  67. );
  68. } );
  69. // Checks if preventDefault method was called by FakeSelectionObserver for specified key code.
  70. //
  71. // @param {Number} keyCode
  72. // @param {Boolean} shouldPrevent If set to true method checks if event was prevented.
  73. // @returns {Promise}
  74. function checkEventPrevention( keyCode, shouldPrevent = true ) {
  75. return new Promise( resolve => {
  76. const data = {
  77. keyCode,
  78. preventDefault: sinon.spy(),
  79. };
  80. viewDocument.once( 'keydown', () => {
  81. if ( shouldPrevent ) {
  82. sinon.assert.calledOnce( data.preventDefault );
  83. } else {
  84. sinon.assert.notCalled( data.preventDefault );
  85. }
  86. resolve();
  87. }, { priority: 'lowest' } );
  88. viewDocument.fire( 'keydown', new DomEventData( viewDocument, { target: document.body }, data ) );
  89. } );
  90. }
  91. // Checks if proper selectionChange event is fired by FakeSelectionObserver for specified key.
  92. //
  93. // @param {String} initialData
  94. // @param {Number} keyCode
  95. // @param {String} output
  96. // @returns {Promise}
  97. function checkSelectionChange( initialData, keyCode, output ) {
  98. return new Promise( resolve => {
  99. viewDocument.once( 'selectionChange', ( eventInfo, data ) => {
  100. expect( stringify( root.getChild( 0 ), data.newSelection, { showType: true } ) ).to.equal( output );
  101. resolve();
  102. } );
  103. setData( viewDocument, initialData );
  104. viewDocument.selection.setFake();
  105. const data = {
  106. keyCode,
  107. preventDefault: sinon.spy(),
  108. };
  109. viewDocument.fire( 'keydown', new DomEventData( viewDocument, { target: document.body }, data ) );
  110. } );
  111. }
  112. } );