keyboard.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. 'use strict';
  6. import { knownKeyNames, getCode, parseKeystroke } from '/ckeditor5/utils/keyboard.js';
  7. import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
  8. describe( 'Keyboard', () => {
  9. describe( 'knownKeyNames', () => {
  10. it( 'contains numbers', () => {
  11. expect( knownKeyNames[ '0' ] ).to.equal( 48 );
  12. expect( knownKeyNames[ '9' ] ).to.equal( 57 );
  13. } );
  14. it( 'contains letters', () => {
  15. expect( knownKeyNames.a ).to.equal( 65 );
  16. expect( knownKeyNames.z ).to.equal( 90 );
  17. } );
  18. it( 'modifiers and other keys', () => {
  19. expect( knownKeyNames.delete ).to.equal( 46 );
  20. expect( knownKeyNames.ctrl ).to.equal( 0x110000 );
  21. expect( knownKeyNames.cmd ).to.equal( 0x110000 );
  22. } );
  23. } );
  24. describe( 'getCode', () => {
  25. it( 'gets code of a number', () => {
  26. expect( getCode( '0' ) ).to.equal( 48 );
  27. } );
  28. it( 'gets code of a letter', () => {
  29. expect( getCode( 'a' ) ).to.equal( 65 );
  30. } );
  31. it( 'is case insensitive', () => {
  32. expect( getCode( 'A' ) ).to.equal( 65 );
  33. expect( getCode( 'Ctrl' ) ).to.equal( 0x110000 );
  34. expect( getCode( 'ENTER' ) ).to.equal( 13 );
  35. } );
  36. it( 'throws when passed unknown key name', () => {
  37. expect( () => {
  38. getCode( 'foo' );
  39. } ).to.throw( CKEditorError, /^keyboard-unknown-key:/ );
  40. } );
  41. it( 'gets code of a keystroke info', () => {
  42. expect( getCode( { keyCode: 48 } ) ).to.equal( 48 );
  43. } );
  44. it( 'adds modifiers to the keystroke code', () => {
  45. expect( getCode( { keyCode: 48, altKey: true, ctrlKey: true, shiftKey: true } ) )
  46. .to.equal( 48 + 0x110000 + 0x220000 + 0x440000 );
  47. } );
  48. } );
  49. describe( 'parseKeystroke', () => {
  50. it( 'parses string', () => {
  51. expect( parseKeystroke( 'ctrl+a' ) ).to.equal( 0x110000 + 65 );
  52. } );
  53. it( 'allows spacing', () => {
  54. expect( parseKeystroke( 'ctrl + a' ) ).to.equal( 0x110000 + 65 );
  55. } );
  56. it( 'is case-insensitive', () => {
  57. expect( parseKeystroke( 'Ctrl+A' ) ).to.equal( 0x110000 + 65 );
  58. } );
  59. it( 'works with an array', () => {
  60. expect( parseKeystroke( [ 'ctrl', 'a' ] ) ).to.equal( 0x110000 + 65 );
  61. } );
  62. it( 'works with an array which contains numbers', () => {
  63. expect( parseKeystroke( [ 'shift', 33 ] ) ).to.equal( 0x220000 + 33 );
  64. } );
  65. it( 'works with two modifiers', () => {
  66. expect( parseKeystroke( 'ctrl+shift+a' ) ).to.equal( 0x110000 + 0x220000 + 65 );
  67. } );
  68. it( 'throws on unknown name', () => {
  69. expect( () => {
  70. parseKeystroke( 'foo' );
  71. } ).to.throw( CKEditorError, /^keyboard-unknown-key:/ );
  72. } );
  73. } );
  74. } );