keyobserver.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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 KeyObserver from '../../../src/view/observer/keyobserver';
  7. import View from '../../../src/view/view';
  8. import { getCode } from '@ckeditor/ckeditor5-utils/src/keyboard';
  9. import { StylesProcessor } from '../../../src/view/stylesmap';
  10. describe( 'KeyObserver', () => {
  11. let view, viewDocument, observer, stylesProcessor;
  12. beforeEach( () => {
  13. stylesProcessor = new StylesProcessor();
  14. view = new View( stylesProcessor );
  15. viewDocument = view.document;
  16. observer = view.getObserver( KeyObserver );
  17. } );
  18. afterEach( () => {
  19. view.destroy();
  20. } );
  21. it( 'should define domEventType', () => {
  22. expect( observer.domEventType ).to.contains( 'keydown' );
  23. expect( observer.domEventType ).to.contains( 'keyup' );
  24. } );
  25. describe( 'onDomEvent', () => {
  26. it( 'should fire keydown with the target and key info', () => {
  27. const spy = sinon.spy();
  28. viewDocument.on( 'keydown', spy );
  29. observer.onDomEvent( {
  30. type: 'keydown',
  31. target: document.body,
  32. keyCode: 111,
  33. altKey: false,
  34. ctrlKey: false,
  35. metaKey: false,
  36. shiftKey: false
  37. } );
  38. expect( spy.calledOnce ).to.be.true;
  39. const data = spy.args[ 0 ][ 1 ];
  40. expect( data ).to.have.property( 'domTarget', document.body );
  41. expect( data ).to.have.property( 'keyCode', 111 );
  42. expect( data ).to.have.property( 'altKey', false );
  43. expect( data ).to.have.property( 'ctrlKey', false );
  44. expect( data ).to.have.property( 'shiftKey', false );
  45. expect( data ).to.have.property( 'keystroke', getCode( data ) );
  46. // Just to be sure.
  47. expect( getCode( data ) ).to.equal( 111 );
  48. } );
  49. it( 'should fire keydown with proper key modifiers info', () => {
  50. const spy = sinon.spy();
  51. viewDocument.on( 'keydown', spy );
  52. observer.onDomEvent( {
  53. type: 'keydown',
  54. target: document.body,
  55. keyCode: 111,
  56. altKey: true,
  57. ctrlKey: true,
  58. metaKey: false,
  59. shiftKey: true
  60. } );
  61. const data = spy.args[ 0 ][ 1 ];
  62. expect( data ).to.have.property( 'keyCode', 111 );
  63. expect( data ).to.have.property( 'altKey', true );
  64. expect( data ).to.have.property( 'ctrlKey', true );
  65. expect( data ).to.have.property( 'shiftKey', true );
  66. expect( data ).to.have.property( 'keystroke', getCode( data ) );
  67. // Just to be sure.
  68. expect( getCode( data ) ).to.be.greaterThan( 111 );
  69. } );
  70. it( 'should fire keydown ctrlKey set to true one meta (cmd) was pressed', () => {
  71. const spy = sinon.spy();
  72. viewDocument.on( 'keydown', spy );
  73. observer.onDomEvent( { type: 'keydown', target: document.body, keyCode: 111, metaKey: true } );
  74. const data = spy.args[ 0 ][ 1 ];
  75. expect( data ).to.have.property( 'ctrlKey', true );
  76. } );
  77. it( 'should fire keyup with the target and key info', () => {
  78. const spy = sinon.spy();
  79. viewDocument.on( 'keyup', spy );
  80. observer.onDomEvent( {
  81. type: 'keyup',
  82. target: document.body,
  83. keyCode: 111,
  84. altKey: false,
  85. ctrlKey: false,
  86. metaKey: false,
  87. shiftKey: false
  88. } );
  89. expect( spy.calledOnce ).to.be.true;
  90. const data = spy.args[ 0 ][ 1 ];
  91. expect( data ).to.have.property( 'domTarget', document.body );
  92. expect( data ).to.have.property( 'keyCode', 111 );
  93. expect( data ).to.have.property( 'altKey', false );
  94. expect( data ).to.have.property( 'ctrlKey', false );
  95. expect( data ).to.have.property( 'shiftKey', false );
  96. expect( data ).to.have.property( 'keystroke', getCode( data ) );
  97. // Just to be sure.
  98. expect( getCode( data ) ).to.equal( 111 );
  99. } );
  100. } );
  101. } );