8
0

toolbarview.js 8.3 KB

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