toolbarview.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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.render();
  24. } );
  25. afterEach( () => {
  26. view.destroy();
  27. } );
  28. describe( 'constructor()', () => {
  29. it( 'should set view#locale', () => {
  30. expect( view.locale ).to.equal( locale );
  31. } );
  32. it( 'should set view#isVertical', () => {
  33. expect( view.isVertical ).to.be.false;
  34. } );
  35. it( 'should create view#children collection', () => {
  36. expect( view.items ).to.be.instanceOf( ViewCollection );
  37. } );
  38. it( 'creates #focusTracker instance', () => {
  39. expect( view.focusTracker ).to.be.instanceOf( FocusTracker );
  40. } );
  41. it( 'creates #keystrokes instance', () => {
  42. expect( view.keystrokes ).to.be.instanceOf( KeystrokeHandler );
  43. } );
  44. it( 'creates #_focusCycler instance', () => {
  45. expect( view._focusCycler ).to.be.instanceOf( FocusCycler );
  46. } );
  47. } );
  48. describe( 'template', () => {
  49. it( 'should create element from template', () => {
  50. expect( view.element.classList.contains( 'ck-toolbar' ) ).to.true;
  51. } );
  52. describe( 'event listeners', () => {
  53. it( 'prevent default on #mousedown', () => {
  54. const evt = new Event( 'mousedown', { bubbles: true } );
  55. const spy = sinon.spy( evt, 'preventDefault' );
  56. view.element.dispatchEvent( evt );
  57. sinon.assert.calledOnce( spy );
  58. } );
  59. } );
  60. } );
  61. describe( 'element bindings', () => {
  62. describe( 'class', () => {
  63. it( 'reacts on view#isVertical', () => {
  64. view.isVertical = false;
  65. expect( view.element.classList.contains( 'ck-toolbar_vertical' ) ).to.be.false;
  66. view.isVertical = true;
  67. expect( view.element.classList.contains( 'ck-toolbar_vertical' ) ).to.be.true;
  68. } );
  69. it( 'reacts on view#className', () => {
  70. view.className = 'foo';
  71. expect( view.element.classList.contains( 'foo' ) ).to.be.true;
  72. view.className = 'bar';
  73. expect( view.element.classList.contains( 'bar' ) ).to.be.true;
  74. view.className = false;
  75. expect( view.element.classList.contains( 'foo' ) ).to.be.false;
  76. expect( view.element.classList.contains( 'bar' ) ).to.be.false;
  77. } );
  78. } );
  79. } );
  80. describe( 'render()', () => {
  81. it( 'registers #items in #focusTracker', () => {
  82. const view = new ToolbarView( locale );
  83. const spyAdd = sinon.spy( view.focusTracker, 'add' );
  84. const spyRemove = sinon.spy( view.focusTracker, 'remove' );
  85. view.items.add( focusable() );
  86. view.items.add( focusable() );
  87. sinon.assert.notCalled( spyAdd );
  88. view.render();
  89. sinon.assert.calledTwice( spyAdd );
  90. view.items.remove( 1 );
  91. sinon.assert.calledOnce( spyRemove );
  92. view.destroy();
  93. } );
  94. it( 'starts listening for #keystrokes coming from #element', () => {
  95. const view = new ToolbarView();
  96. const spy = sinon.spy( view.keystrokes, 'listenTo' );
  97. view.render();
  98. sinon.assert.calledOnce( spy );
  99. sinon.assert.calledWithExactly( spy, view.element );
  100. view.destroy();
  101. } );
  102. describe( 'activates keyboard navigation for the toolbar', () => {
  103. it( 'so "arrowup" focuses previous focusable item', () => {
  104. const keyEvtData = {
  105. keyCode: keyCodes.arrowup,
  106. preventDefault: sinon.spy(),
  107. stopPropagation: sinon.spy()
  108. };
  109. // No children to focus.
  110. view.keystrokes.press( keyEvtData );
  111. sinon.assert.calledOnce( keyEvtData.preventDefault );
  112. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  113. view.items.add( nonFocusable() );
  114. view.items.add( nonFocusable() );
  115. // No focusable children.
  116. view.keystrokes.press( keyEvtData );
  117. sinon.assert.calledTwice( keyEvtData.preventDefault );
  118. sinon.assert.calledTwice( keyEvtData.stopPropagation );
  119. view.items.add( focusable() );
  120. view.items.add( nonFocusable() );
  121. view.items.add( focusable() );
  122. // Mock the last item is focused.
  123. view.focusTracker.isFocused = true;
  124. view.focusTracker.focusedElement = view.items.get( 4 ).element;
  125. const spy = sinon.spy( view.items.get( 2 ), 'focus' );
  126. view.keystrokes.press( keyEvtData );
  127. sinon.assert.calledThrice( keyEvtData.preventDefault );
  128. sinon.assert.calledThrice( keyEvtData.stopPropagation );
  129. sinon.assert.calledOnce( spy );
  130. } );
  131. it( 'so "arrowleft" focuses previous focusable item', () => {
  132. const keyEvtData = {
  133. keyCode: keyCodes.arrowleft,
  134. preventDefault: sinon.spy(),
  135. stopPropagation: sinon.spy()
  136. };
  137. view.items.add( focusable() );
  138. view.items.add( nonFocusable() );
  139. view.items.add( focusable() );
  140. // Mock the last item is focused.
  141. view.focusTracker.isFocused = true;
  142. view.focusTracker.focusedElement = view.items.get( 2 ).element;
  143. const spy = sinon.spy( view.items.get( 0 ), 'focus' );
  144. view.keystrokes.press( keyEvtData );
  145. sinon.assert.calledOnce( spy );
  146. } );
  147. it( 'so "arrowdown" focuses next focusable item', () => {
  148. const keyEvtData = {
  149. keyCode: keyCodes.arrowdown,
  150. preventDefault: sinon.spy(),
  151. stopPropagation: sinon.spy()
  152. };
  153. // No children to focus.
  154. view.keystrokes.press( keyEvtData );
  155. sinon.assert.calledOnce( keyEvtData.preventDefault );
  156. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  157. view.items.add( nonFocusable() );
  158. view.items.add( nonFocusable() );
  159. // No focusable children.
  160. view.keystrokes.press( keyEvtData );
  161. sinon.assert.calledTwice( keyEvtData.preventDefault );
  162. sinon.assert.calledTwice( keyEvtData.stopPropagation );
  163. view.items.add( focusable() );
  164. view.items.add( nonFocusable() );
  165. view.items.add( focusable() );
  166. // Mock the last item is focused.
  167. view.focusTracker.isFocused = true;
  168. view.focusTracker.focusedElement = view.items.get( 4 ).element;
  169. const spy = sinon.spy( view.items.get( 2 ), 'focus' );
  170. view.keystrokes.press( keyEvtData );
  171. sinon.assert.calledThrice( keyEvtData.preventDefault );
  172. sinon.assert.calledThrice( keyEvtData.stopPropagation );
  173. sinon.assert.calledOnce( spy );
  174. } );
  175. it( 'so "arrowright" focuses next focusable item', () => {
  176. const keyEvtData = {
  177. keyCode: keyCodes.arrowright,
  178. preventDefault: sinon.spy(),
  179. stopPropagation: sinon.spy()
  180. };
  181. view.items.add( focusable() );
  182. view.items.add( nonFocusable() );
  183. view.items.add( focusable() );
  184. // Mock the last item is focused.
  185. view.focusTracker.isFocused = true;
  186. view.focusTracker.focusedElement = view.items.get( 0 ).element;
  187. const spy = sinon.spy( view.items.get( 2 ), 'focus' );
  188. view.keystrokes.press( keyEvtData );
  189. sinon.assert.calledOnce( spy );
  190. } );
  191. } );
  192. } );
  193. describe( 'focus()', () => {
  194. it( 'focuses the first focusable item in DOM', () => {
  195. // No children to focus.
  196. view.focus();
  197. // The second child is focusable.
  198. view.items.add( nonFocusable() );
  199. view.items.add( focusable() );
  200. view.items.add( nonFocusable() );
  201. const spy = sinon.spy( view.items.get( 1 ), 'focus' );
  202. view.focus();
  203. sinon.assert.calledOnce( spy );
  204. } );
  205. } );
  206. describe( 'focusLast()', () => {
  207. it( 'focuses the last focusable item in DOM', () => {
  208. // No children to focus.
  209. view.focusLast();
  210. // The second child is focusable.
  211. view.items.add( nonFocusable() );
  212. view.items.add( focusable() );
  213. view.items.add( focusable() );
  214. view.items.add( focusable() );
  215. view.items.add( nonFocusable() );
  216. const spy = sinon.spy( view.items.get( 3 ), 'focus' );
  217. view.focusLast();
  218. sinon.assert.calledOnce( spy );
  219. } );
  220. } );
  221. describe( 'fillFromConfig()', () => {
  222. let factory;
  223. beforeEach( () => {
  224. factory = new ComponentFactory( {} );
  225. factory.add( 'foo', namedFactory( 'foo' ) );
  226. factory.add( 'bar', namedFactory( 'bar' ) );
  227. } );
  228. it( 'expands the config into collection', () => {
  229. view.fillFromConfig( [ 'foo', 'bar', '|', 'foo' ], factory );
  230. const items = view.items;
  231. expect( items ).to.have.length( 4 );
  232. expect( items.get( 0 ).name ).to.equal( 'foo' );
  233. expect( items.get( 1 ).name ).to.equal( 'bar' );
  234. expect( items.get( 2 ) ).to.be.instanceOf( ToolbarSeparatorView );
  235. expect( items.get( 3 ).name ).to.equal( 'foo' );
  236. } );
  237. it( 'warns if there is no such component in the factory', () => {
  238. const items = view.items;
  239. testUtils.sinon.stub( log, 'warn' );
  240. view.fillFromConfig( [ 'foo', 'bar', 'baz' ], factory );
  241. expect( items ).to.have.length( 2 );
  242. expect( items.get( 0 ).name ).to.equal( 'foo' );
  243. expect( items.get( 1 ).name ).to.equal( 'bar' );
  244. sinon.assert.calledOnce( log.warn );
  245. sinon.assert.calledWithExactly( log.warn,
  246. sinon.match( /^toolbarview-item-unavailable/ ),
  247. { name: 'baz' }
  248. );
  249. } );
  250. } );
  251. } );
  252. function focusable() {
  253. const view = nonFocusable();
  254. view.focus = () => {};
  255. return view;
  256. }
  257. function nonFocusable() {
  258. const view = new View();
  259. view.element = document.createElement( 'li' );
  260. return view;
  261. }
  262. function namedFactory( name ) {
  263. return locale => {
  264. const view = new View( locale );
  265. view.name = name;
  266. view.element = document.createElement( 'a' );
  267. return view;
  268. };
  269. }