ballooneditorui.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals Event */
  6. import ComponentFactory from '@ckeditor/ckeditor5-ui/src/componentfactory';
  7. import BalloonEditorUI from '../src/ballooneditorui';
  8. import BalloonEditorUIView from '../src/ballooneditoruiview';
  9. import VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
  10. import ContextualToolbar from '@ckeditor/ckeditor5-ui/src/toolbar/contextual/contextualtoolbar';
  11. import FocusTracker from '@ckeditor/ckeditor5-utils/src/focustracker';
  12. import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  13. import utils from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  14. describe( 'BalloonEditorUI', () => {
  15. let editor, view, ui;
  16. beforeEach( () => {
  17. return VirtualBalloonTestEditor
  18. .create( {
  19. plugins: [ ContextualToolbar ]
  20. } )
  21. .then( newEditor => {
  22. editor = newEditor;
  23. ui = editor.ui;
  24. view = ui.view;
  25. } );
  26. } );
  27. afterEach( () => {
  28. editor.destroy();
  29. } );
  30. describe( 'constructor()', () => {
  31. it( 'sets #editor', () => {
  32. expect( ui.editor ).to.equal( editor );
  33. } );
  34. it( 'sets #view', () => {
  35. expect( ui.view ).to.equal( view );
  36. } );
  37. it( 'creates #componentFactory factory', () => {
  38. expect( ui.componentFactory ).to.be.instanceOf( ComponentFactory );
  39. } );
  40. it( 'creates #focusTracker', () => {
  41. expect( ui.focusTracker ).to.be.instanceOf( FocusTracker );
  42. } );
  43. describe( 'editable', () => {
  44. let editable;
  45. beforeEach( () => {
  46. editable = editor.editing.view.getRoot();
  47. } );
  48. it( 'registers view.editable#element in editor focus tracker', () => {
  49. ui.focusTracker.isFocused = false;
  50. view.editable.element.dispatchEvent( new Event( 'focus' ) );
  51. expect( ui.focusTracker.isFocused ).to.true;
  52. } );
  53. it( 'sets view.editable#name', () => {
  54. expect( view.editable.name ).to.equal( editable.rootName );
  55. } );
  56. it( 'binds view.editable#isFocused', () => {
  57. utils.assertBinding(
  58. view.editable,
  59. { isFocused: false },
  60. [
  61. [ ui.focusTracker, { isFocused: true } ]
  62. ],
  63. { isFocused: true }
  64. );
  65. } );
  66. it( 'binds view.editable#isReadOnly', () => {
  67. utils.assertBinding(
  68. view.editable,
  69. { isReadOnly: false },
  70. [
  71. [ editable, { isReadOnly: true } ]
  72. ],
  73. { isReadOnly: true }
  74. );
  75. } );
  76. } );
  77. } );
  78. describe( 'init()', () => {
  79. it( 'initializes the #view', () => {
  80. expect( view.isRendered ).to.be.true;
  81. } );
  82. it( 'initializes keyboard navigation between view#toolbar and view#editable', () => {
  83. const toolbar = editor.plugins.get( 'ContextualToolbar' );
  84. const toolbarFocusSpy = sinon.stub( toolbar.toolbarView, 'focus' ).returns( {} );
  85. const toolbarShowSpy = sinon.stub( toolbar, 'show' ).returns( {} );
  86. const toolbarHideSpy = sinon.stub( toolbar, 'hide' ).returns( {} );
  87. const editingFocusSpy = sinon.stub( editor.editing.view, 'focus' ).returns( {} );
  88. ui.focusTracker.isFocused = true;
  89. // #show and #hide are mocked so mocking the focus as well.
  90. toolbar.toolbarView.focusTracker.isFocused = false;
  91. editor.keystrokes.press( {
  92. keyCode: keyCodes.f10,
  93. altKey: true,
  94. preventDefault: sinon.spy(),
  95. stopPropagation: sinon.spy()
  96. } );
  97. sinon.assert.callOrder( toolbarShowSpy, toolbarFocusSpy );
  98. sinon.assert.notCalled( toolbarHideSpy );
  99. sinon.assert.notCalled( editingFocusSpy );
  100. // #show and #hide are mocked so mocking the focus as well.
  101. toolbar.toolbarView.focusTracker.isFocused = true;
  102. toolbar.toolbarView.keystrokes.press( {
  103. keyCode: keyCodes.esc,
  104. preventDefault: sinon.spy(),
  105. stopPropagation: sinon.spy()
  106. } );
  107. sinon.assert.callOrder( editingFocusSpy, toolbarHideSpy );
  108. } );
  109. } );
  110. describe( 'destroy()', () => {
  111. it( 'destroys the #view', () => {
  112. const spy = sinon.spy( view, 'destroy' );
  113. return editor.destroy()
  114. .then( () => {
  115. sinon.assert.calledOnce( spy );
  116. } );
  117. } );
  118. } );
  119. } );
  120. class VirtualBalloonTestEditor extends VirtualTestEditor {
  121. constructor( config ) {
  122. super( config );
  123. const view = new BalloonEditorUIView( this.locale );
  124. this.ui = new BalloonEditorUI( this, view );
  125. }
  126. destroy() {
  127. this.ui.destroy();
  128. return super.destroy();
  129. }
  130. static create( config ) {
  131. return new Promise( resolve => {
  132. const editor = new this( config );
  133. resolve(
  134. editor.initPlugins()
  135. .then( () => {
  136. editor.ui.init();
  137. editor.fire( 'uiReady' );
  138. editor.fire( 'dataReady' );
  139. editor.fire( 'ready' );
  140. } )
  141. .then( () => editor )
  142. );
  143. } );
  144. }
  145. }