8
0

toolbarview.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global document */
  6. import ToolbarView from '../../src/toolbar/toolbarview';
  7. import ToolbarSeparatorView from '../../src/toolbar/toolbarseparatorview';
  8. import KeystrokeHandler from '@ckeditor/ckeditor5-utils/src/keystrokehandler';
  9. import ComponentFactory from '../../src/componentfactory';
  10. import FocusTracker from '@ckeditor/ckeditor5-utils/src/focustracker';
  11. import FocusCycler from '../../src/focuscycler';
  12. import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  13. import ViewCollection from '../../src/viewcollection';
  14. import View from '../../src/view';
  15. describe( 'ToolbarView', () => {
  16. let locale, view;
  17. beforeEach( () => {
  18. locale = {};
  19. view = new ToolbarView( locale );
  20. return view.init();
  21. } );
  22. describe( 'constructor()', () => {
  23. it( 'should set view#locale', () => {
  24. expect( view.locale ).to.equal( locale );
  25. } );
  26. it( 'should create view#children collection', () => {
  27. expect( view.items ).to.be.instanceOf( ViewCollection );
  28. } );
  29. it( 'creates #focusTracker instance', () => {
  30. expect( view.focusTracker ).to.be.instanceOf( FocusTracker );
  31. } );
  32. it( 'creates #keystrokes instance', () => {
  33. expect( view.keystrokes ).to.be.instanceOf( KeystrokeHandler );
  34. } );
  35. it( 'creates #_focusCycler instance', () => {
  36. expect( view._focusCycler ).to.be.instanceOf( FocusCycler );
  37. } );
  38. it( 'registers #items in #focusTracker', () => {
  39. const spyAdd = sinon.spy( view.focusTracker, 'add' );
  40. const spyRemove = sinon.spy( view.focusTracker, 'remove' );
  41. view.items.add( focusable() );
  42. view.items.add( focusable() );
  43. sinon.assert.calledTwice( spyAdd );
  44. view.items.remove( 1 );
  45. sinon.assert.calledOnce( spyRemove );
  46. } );
  47. } );
  48. describe( 'template', () => {
  49. it( 'should create element from template', () => {
  50. expect( view.element.classList.contains( 'ck-toolbar' ) ).to.true;
  51. } );
  52. } );
  53. describe( 'init()', () => {
  54. it( 'starts listening for #keystrokes coming from #element', () => {
  55. view = new ToolbarView();
  56. const spy = sinon.spy( view.keystrokes, 'listenTo' );
  57. return view.init().then( () => {
  58. sinon.assert.calledOnce( spy );
  59. sinon.assert.calledWithExactly( spy, view.element );
  60. } );
  61. } );
  62. describe( 'activates keyboard navigation for the toolbar', () => {
  63. it( 'so "arrowup" focuses previous focusable item', () => {
  64. const keyEvtData = {
  65. keyCode: keyCodes.arrowup,
  66. preventDefault: sinon.spy(),
  67. stopPropagation: sinon.spy()
  68. };
  69. // No children to focus.
  70. view.keystrokes.press( keyEvtData );
  71. sinon.assert.calledOnce( keyEvtData.preventDefault );
  72. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  73. view.items.add( nonFocusable() );
  74. view.items.add( nonFocusable() );
  75. // No focusable children.
  76. view.keystrokes.press( keyEvtData );
  77. sinon.assert.calledTwice( keyEvtData.preventDefault );
  78. sinon.assert.calledTwice( keyEvtData.stopPropagation );
  79. view.items.add( focusable() );
  80. view.items.add( nonFocusable() );
  81. view.items.add( focusable() );
  82. // Mock the last item is focused.
  83. view.focusTracker.isFocused = true;
  84. view.focusTracker.focusedElement = view.items.get( 4 ).element;
  85. const spy = sinon.spy( view.items.get( 2 ), 'focus' );
  86. view.keystrokes.press( keyEvtData );
  87. sinon.assert.calledThrice( keyEvtData.preventDefault );
  88. sinon.assert.calledThrice( keyEvtData.stopPropagation );
  89. sinon.assert.calledOnce( spy );
  90. } );
  91. it( 'so "arrowleft" focuses previous focusable item', () => {
  92. const keyEvtData = {
  93. keyCode: keyCodes.arrowleft,
  94. preventDefault: sinon.spy(),
  95. stopPropagation: sinon.spy()
  96. };
  97. view.items.add( focusable() );
  98. view.items.add( nonFocusable() );
  99. view.items.add( focusable() );
  100. // Mock the last item is focused.
  101. view.focusTracker.isFocused = true;
  102. view.focusTracker.focusedElement = view.items.get( 2 ).element;
  103. const spy = sinon.spy( view.items.get( 0 ), 'focus' );
  104. view.keystrokes.press( keyEvtData );
  105. sinon.assert.calledOnce( spy );
  106. } );
  107. it( 'so "arrowdown" focuses next focusable item', () => {
  108. const keyEvtData = {
  109. keyCode: keyCodes.arrowdown,
  110. preventDefault: sinon.spy(),
  111. stopPropagation: sinon.spy()
  112. };
  113. // No children to focus.
  114. view.keystrokes.press( keyEvtData );
  115. sinon.assert.calledOnce( keyEvtData.preventDefault );
  116. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  117. view.items.add( nonFocusable() );
  118. view.items.add( nonFocusable() );
  119. // No focusable children.
  120. view.keystrokes.press( keyEvtData );
  121. sinon.assert.calledTwice( keyEvtData.preventDefault );
  122. sinon.assert.calledTwice( keyEvtData.stopPropagation );
  123. view.items.add( focusable() );
  124. view.items.add( nonFocusable() );
  125. view.items.add( focusable() );
  126. // Mock the last item is focused.
  127. view.focusTracker.isFocused = true;
  128. view.focusTracker.focusedElement = view.items.get( 4 ).element;
  129. const spy = sinon.spy( view.items.get( 2 ), 'focus' );
  130. view.keystrokes.press( keyEvtData );
  131. sinon.assert.calledThrice( keyEvtData.preventDefault );
  132. sinon.assert.calledThrice( keyEvtData.stopPropagation );
  133. sinon.assert.calledOnce( spy );
  134. } );
  135. it( 'so "arrowright" focuses next focusable item', () => {
  136. const keyEvtData = {
  137. keyCode: keyCodes.arrowright,
  138. preventDefault: sinon.spy(),
  139. stopPropagation: sinon.spy()
  140. };
  141. view.items.add( focusable() );
  142. view.items.add( nonFocusable() );
  143. view.items.add( focusable() );
  144. // Mock the last item is focused.
  145. view.focusTracker.isFocused = true;
  146. view.focusTracker.focusedElement = view.items.get( 0 ).element;
  147. const spy = sinon.spy( view.items.get( 2 ), 'focus' );
  148. view.keystrokes.press( keyEvtData );
  149. sinon.assert.calledOnce( spy );
  150. } );
  151. } );
  152. } );
  153. describe( 'focus()', () => {
  154. it( 'focuses the first focusable item in DOM', () => {
  155. // No children to focus.
  156. view.focus();
  157. // The second child is focusable.
  158. view.items.add( nonFocusable() );
  159. view.items.add( focusable() );
  160. view.items.add( nonFocusable() );
  161. const spy = sinon.spy( view.items.get( 1 ), 'focus' );
  162. view.focus();
  163. sinon.assert.calledOnce( spy );
  164. } );
  165. } );
  166. describe( 'fillFromConfig()', () => {
  167. let factory;
  168. beforeEach( () => {
  169. factory = new ComponentFactory( {} );
  170. factory.add( 'foo', namedFactory( 'foo' ) );
  171. factory.add( 'bar', namedFactory( 'bar' ) );
  172. } );
  173. it( 'returns a promise', () => {
  174. expect( view.fillFromConfig() ).to.be.instanceOf( Promise );
  175. } );
  176. it( 'expands the config into collection', () => {
  177. return view.fillFromConfig( [ 'foo', 'bar', '|', 'foo' ], factory )
  178. .then( () => {
  179. const items = view.items;
  180. expect( items ).to.have.length( 4 );
  181. expect( items.get( 0 ).name ).to.equal( 'foo' );
  182. expect( items.get( 1 ).name ).to.equal( 'bar' );
  183. expect( items.get( 2 ) ).to.be.instanceOf( ToolbarSeparatorView );
  184. expect( items.get( 3 ).name ).to.equal( 'foo' );
  185. } );
  186. } );
  187. } );
  188. } );
  189. function focusable() {
  190. const view = nonFocusable();
  191. view.focus = () => {};
  192. return view;
  193. }
  194. function nonFocusable() {
  195. const view = new View();
  196. view.element = document.createElement( 'li' );
  197. return view;
  198. }
  199. function namedFactory( name ) {
  200. return ( locale ) => {
  201. const view = new View( locale );
  202. view.name = name;
  203. view.element = document.createElement( 'a' );
  204. return view;
  205. };
  206. }