8
0

toolbarview.js 9.7 KB

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