classiceditorui.js 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document, Event */
  6. import ComponentFactory from '@ckeditor/ckeditor5-ui/src/componentfactory';
  7. import View from '@ckeditor/ckeditor5-ui/src/view';
  8. import ClassicEditorUI from '../src/classiceditorui';
  9. import ClassicEditorUIView from '../src/classiceditoruiview';
  10. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  11. import FocusTracker from '@ckeditor/ckeditor5-utils/src/focustracker';
  12. import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  13. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  14. import utils from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  15. testUtils.createSinonSandbox();
  16. describe( 'ClassicEditorUI', () => {
  17. let editorElement, editor, editable, view, ui;
  18. beforeEach( () => {
  19. editorElement = document.createElement( 'div' );
  20. document.body.appendChild( editorElement );
  21. editor = new ClassicTestEditor( editorElement, {
  22. toolbar: [ 'foo', 'bar' ],
  23. ui: {
  24. width: 100,
  25. height: 200
  26. }
  27. } );
  28. view = new ClassicEditorUIView( editor.locale );
  29. ui = new ClassicEditorUI( editor, view );
  30. editable = editor.editing.view.getRoot();
  31. ui.componentFactory.add( 'foo', viewCreator( 'foo' ) );
  32. ui.componentFactory.add( 'bar', viewCreator( 'bar' ) );
  33. } );
  34. describe( 'constructor()', () => {
  35. it( 'sets #editor', () => {
  36. expect( ui.editor ).to.equal( editor );
  37. } );
  38. it( 'sets #view', () => {
  39. expect( ui.view ).to.equal( view );
  40. } );
  41. it( 'creates #componentFactory factory', () => {
  42. expect( ui.componentFactory ).to.be.instanceOf( ComponentFactory );
  43. } );
  44. it( 'creates #focusTracker', () => {
  45. expect( ui.focusTracker ).to.be.instanceOf( FocusTracker );
  46. } );
  47. it( 'sets view#width and view#height', () => {
  48. expect( view.width ).to.equal( 100 );
  49. expect( view.height ).to.equal( 200 );
  50. } );
  51. describe( 'toolbar', () => {
  52. it( 'binds view.toolbar#isFocused to editor#focusTracker', () => {
  53. ui.focusTracker.isFocused = false;
  54. expect( view.toolbar.isActive ).to.be.false;
  55. ui.focusTracker.isFocused = true;
  56. expect( view.toolbar.isActive ).to.be.true;
  57. } );
  58. it( 'sets view.toolbar#limiterElement', () => {
  59. expect( view.toolbar.limiterElement ).to.equal( view.element );
  60. } );
  61. } );
  62. describe( 'editable', () => {
  63. it( 'registers view.editable#element in editor focus tracker', () => {
  64. ui.focusTracker.isFocused = false;
  65. view.editable.element.dispatchEvent( new Event( 'focus' ) );
  66. expect( ui.focusTracker.isFocused ).to.true;
  67. } );
  68. it( 'sets view.editable#name', () => {
  69. expect( view.editable.name ).to.equal( editable.rootName );
  70. } );
  71. it( 'binds view.editable#isFocused', () => {
  72. utils.assertBinding(
  73. view.editable,
  74. { isFocused: false },
  75. [
  76. [ editor.editing.view, { isFocused: true } ]
  77. ],
  78. { isFocused: true }
  79. );
  80. } );
  81. it( 'binds view.editable#isReadOnly', () => {
  82. utils.assertBinding(
  83. view.editable,
  84. { isReadOnly: false },
  85. [
  86. [ editable, { isReadOnly: true } ]
  87. ],
  88. { isReadOnly: true }
  89. );
  90. } );
  91. } );
  92. } );
  93. describe( 'init()', () => {
  94. afterEach( () => {
  95. return ui.destroy();
  96. } );
  97. it( 'returns a promise', () => {
  98. document.body.appendChild( view.element );
  99. const promise = ui.init().then( () => {
  100. expect( promise ).to.be.instanceof( Promise );
  101. } );
  102. return promise;
  103. } );
  104. it( 'initializes the #view', () => {
  105. const spy = sinon.spy( view, 'init' );
  106. return ui.init().then( () => {
  107. sinon.assert.calledOnce( spy );
  108. } );
  109. } );
  110. it( 'fills view.toolbar#items with editor config', () => {
  111. return ui.init().then( () => {
  112. expect( view.toolbar.items ).to.have.length( 2 );
  113. expect( view.toolbar.items.get( 0 ).name ).to.equal( 'foo' );
  114. expect( view.toolbar.items.get( 1 ).name ).to.equal( 'bar' );
  115. } );
  116. } );
  117. describe( 'activates keyboard navigation for the toolbar', () => {
  118. it( 'Alt+F10: focus the first focusable toolbar item', () => {
  119. return ui.init().then( () => {
  120. const spy = sinon.spy( view.toolbar, 'focus' );
  121. const toolbarFocusTracker = view.toolbar.focusTracker;
  122. const keyEvtData = {
  123. keyCode: keyCodes.f10,
  124. altKey: true,
  125. preventDefault: sinon.spy(),
  126. stopPropagation: sinon.spy()
  127. };
  128. toolbarFocusTracker.isFocused = false;
  129. ui.focusTracker.isFocused = false;
  130. editor.keystrokes.press( keyEvtData );
  131. sinon.assert.notCalled( spy );
  132. toolbarFocusTracker.isFocused = true;
  133. ui.focusTracker.isFocused = true;
  134. editor.keystrokes.press( keyEvtData );
  135. sinon.assert.notCalled( spy );
  136. toolbarFocusTracker.isFocused = false;
  137. ui.focusTracker.isFocused = true;
  138. editor.keystrokes.press( keyEvtData );
  139. sinon.assert.calledOnce( spy );
  140. sinon.assert.calledOnce( keyEvtData.preventDefault );
  141. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  142. } );
  143. } );
  144. it( 'esc: re–foucus editable when toolbar is focused', () => {
  145. return ui.init().then( () => {
  146. const spy = sinon.spy( editor.editing.view, 'focus' );
  147. const toolbarFocusTracker = view.toolbar.focusTracker;
  148. const keyEvtData = { keyCode: keyCodes.esc,
  149. preventDefault: sinon.spy(),
  150. stopPropagation: sinon.spy()
  151. };
  152. toolbarFocusTracker.isFocused = false;
  153. ui.view.toolbar.keystrokes.press( keyEvtData );
  154. sinon.assert.notCalled( spy );
  155. toolbarFocusTracker.isFocused = true;
  156. ui.view.toolbar.keystrokes.press( keyEvtData );
  157. sinon.assert.calledOnce( spy );
  158. sinon.assert.calledOnce( keyEvtData.preventDefault );
  159. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  160. } );
  161. } );
  162. } );
  163. } );
  164. describe( 'destroy()', () => {
  165. beforeEach( () => {
  166. document.body.appendChild( view.element );
  167. } );
  168. it( 'returns a promise', () => {
  169. return ui.init().then( () => {
  170. const promise = ui.destroy().then( () => {
  171. expect( promise ).to.be.instanceof( Promise );
  172. } );
  173. return promise;
  174. } );
  175. } );
  176. it( 'destroys the #view', () => {
  177. const spy = sinon.spy( view, 'destroy' );
  178. return ui.init()
  179. .then( () => ui.destroy() )
  180. .then( () => {
  181. sinon.assert.calledOnce( spy );
  182. } );
  183. } );
  184. } );
  185. } );
  186. function viewCreator( name ) {
  187. return ( locale ) => {
  188. const view = new View( locale );
  189. view.name = name;
  190. view.element = document.createElement( 'a' );
  191. return view;
  192. };
  193. }