8
0

keyboard.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. import env from '/ckeditor5/utils/env.js';
  6. import { keyCodes, getCode, parseKeystroke, getEnvKeystrokeText } from '/ckeditor5/utils/keyboard.js';
  7. import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
  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. } );
  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. describe( 'getEnvKeystrokeText', () => {
  75. let initialEnvMac = env.mac;
  76. afterEach( () => {
  77. env.mac = initialEnvMac;
  78. } );
  79. describe( 'on Macintosh', () => {
  80. beforeEach( () => {
  81. env.mac = true;
  82. } );
  83. it( 'replaces CTRL with ⌘', () => {
  84. expect( getEnvKeystrokeText( 'CTRL' ) ).to.equal( '⌘' );
  85. expect( getEnvKeystrokeText( 'CTRL+A' ) ).to.equal( '⌘A' );
  86. expect( getEnvKeystrokeText( 'ctrl+A' ) ).to.equal( '⌘A' );
  87. } );
  88. it( 'does not touch other keys', () => {
  89. expect( getEnvKeystrokeText( 'SHIFT+A' ) ).to.equal( 'SHIFT+A' );
  90. expect( getEnvKeystrokeText( 'A' ) ).to.equal( 'A' );
  91. } );
  92. } );
  93. describe( 'on non–Macintosh', () => {
  94. beforeEach( () => {
  95. env.mac = false;
  96. } );
  97. it( 'does not touch anything', () => {
  98. expect( getEnvKeystrokeText( 'CTRL+A' ) ).to.equal( 'CTRL+A' );
  99. expect( getEnvKeystrokeText( 'ctrl+A' ) ).to.equal( 'ctrl+A' );
  100. expect( getEnvKeystrokeText( 'SHIFT+A' ) ).to.equal( 'SHIFT+A' );
  101. expect( getEnvKeystrokeText( 'A' ) ).to.equal( 'A' );
  102. } );
  103. } );
  104. } );
  105. } );