keyboard.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import env from '../src/env';
  6. import { keyCodes, getCode, parseKeystroke, getEnvKeystrokeText } from '../src/keyboard';
  7. import CKEditorError from '../src/ckeditorerror';
  8. describe( 'Keyboard', () => {
  9. describe( 'keyCodes', () => {
  10. it( 'contains numbers', () => {
  11. expect( keyCodes[ '0' ] ).to.equal( 48 );
  12. expect( keyCodes[ '9' ] ).to.equal( 57 );
  13. } );
  14. it( 'contains letters', () => {
  15. expect( keyCodes.a ).to.equal( 65 );
  16. expect( keyCodes.z ).to.equal( 90 );
  17. } );
  18. it( 'modifiers and other keys', () => {
  19. expect( keyCodes.delete ).to.equal( 46 );
  20. expect( keyCodes.ctrl ).to.equal( 0x110000 );
  21. expect( keyCodes.cmd ).to.equal( 0x110000 );
  22. expect( keyCodes.f1 ).to.equal( 112 );
  23. expect( keyCodes.f12 ).to.equal( 123 );
  24. expect( keyCodes ).to.include.keys(
  25. 'ctrl', 'cmd', 'shift', 'alt',
  26. 'arrowleft', 'arrowup', 'arrowright', 'arrowdown',
  27. 'backspace', 'delete', 'enter', 'esc', 'tab',
  28. 'f1', 'f2', 'f3', 'f4', 'f5', 'f6', 'f7', 'f8', 'f9', 'f10', 'f11', 'f12'
  29. );
  30. } );
  31. } );
  32. describe( 'getCode', () => {
  33. it( 'gets code of a number', () => {
  34. expect( getCode( '0' ) ).to.equal( 48 );
  35. } );
  36. it( 'gets code of a letter', () => {
  37. expect( getCode( 'a' ) ).to.equal( 65 );
  38. } );
  39. it( 'gets code of a function key', () => {
  40. expect( getCode( 'f6' ) ).to.equal( 117 );
  41. } );
  42. it( 'is case insensitive', () => {
  43. expect( getCode( 'A' ) ).to.equal( 65 );
  44. expect( getCode( 'Ctrl' ) ).to.equal( 0x110000 );
  45. expect( getCode( 'ENTER' ) ).to.equal( 13 );
  46. } );
  47. it( 'throws when passed unknown key name', () => {
  48. expect( () => {
  49. getCode( 'foo' );
  50. } ).to.throw( CKEditorError, /^keyboard-unknown-key:/ );
  51. } );
  52. it( 'gets code of a keystroke info', () => {
  53. expect( getCode( { keyCode: 48 } ) ).to.equal( 48 );
  54. } );
  55. it( 'adds modifiers to the keystroke code', () => {
  56. expect( getCode( { keyCode: 48, altKey: true, ctrlKey: true, shiftKey: true } ) )
  57. .to.equal( 48 + 0x110000 + 0x220000 + 0x440000 );
  58. } );
  59. } );
  60. describe( 'parseKeystroke', () => {
  61. it( 'parses string', () => {
  62. expect( parseKeystroke( 'ctrl+a' ) ).to.equal( 0x110000 + 65 );
  63. } );
  64. it( 'allows spacing', () => {
  65. expect( parseKeystroke( 'ctrl + a' ) ).to.equal( 0x110000 + 65 );
  66. } );
  67. it( 'is case-insensitive', () => {
  68. expect( parseKeystroke( 'Ctrl+A' ) ).to.equal( 0x110000 + 65 );
  69. } );
  70. it( 'works with an array', () => {
  71. expect( parseKeystroke( [ 'ctrl', 'a' ] ) ).to.equal( 0x110000 + 65 );
  72. } );
  73. it( 'works with an array which contains numbers', () => {
  74. expect( parseKeystroke( [ 'shift', 33 ] ) ).to.equal( 0x220000 + 33 );
  75. } );
  76. it( 'works with two modifiers', () => {
  77. expect( parseKeystroke( 'ctrl+shift+a' ) ).to.equal( 0x110000 + 0x220000 + 65 );
  78. } );
  79. it( 'throws on unknown name', () => {
  80. expect( () => {
  81. parseKeystroke( 'foo' );
  82. } ).to.throw( CKEditorError, /^keyboard-unknown-key:/ );
  83. } );
  84. } );
  85. describe( 'getEnvKeystrokeText', () => {
  86. let initialEnvMac = env.mac;
  87. afterEach( () => {
  88. env.mac = initialEnvMac;
  89. } );
  90. describe( 'on Macintosh', () => {
  91. beforeEach( () => {
  92. env.mac = true;
  93. } );
  94. it( 'replaces CTRL with ⌘', () => {
  95. expect( getEnvKeystrokeText( 'CTRL' ) ).to.equal( '⌘' );
  96. expect( getEnvKeystrokeText( 'CTRL+A' ) ).to.equal( '⌘A' );
  97. expect( getEnvKeystrokeText( 'ctrl+A' ) ).to.equal( '⌘A' );
  98. } );
  99. it( 'does not touch other keys', () => {
  100. expect( getEnvKeystrokeText( 'SHIFT+A' ) ).to.equal( 'SHIFT+A' );
  101. expect( getEnvKeystrokeText( 'A' ) ).to.equal( 'A' );
  102. } );
  103. } );
  104. describe( 'on non–Macintosh', () => {
  105. beforeEach( () => {
  106. env.mac = false;
  107. } );
  108. it( 'does not touch anything', () => {
  109. expect( getEnvKeystrokeText( 'CTRL+A' ) ).to.equal( 'CTRL+A' );
  110. expect( getEnvKeystrokeText( 'ctrl+A' ) ).to.equal( 'ctrl+A' );
  111. expect( getEnvKeystrokeText( 'SHIFT+A' ) ).to.equal( 'SHIFT+A' );
  112. expect( getEnvKeystrokeText( 'A' ) ).to.equal( 'A' );
  113. } );
  114. } );
  115. } );
  116. } );