keyobserver.js 3.6 KB

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