toolbarview.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /**
  2. * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  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. it( 'sets #labelView', () => {
  48. expect( view.labelView ).to.be.instanceOf( View );
  49. } );
  50. } );
  51. describe( 'template', () => {
  52. it( 'should create element from template', () => {
  53. expect( view.element.classList.contains( 'ck' ) ).to.true;
  54. expect( view.element.classList.contains( 'ck-toolbar' ) ).to.true;
  55. } );
  56. describe( 'event listeners', () => {
  57. it( 'prevent default on #mousedown', () => {
  58. const evt = new Event( 'mousedown', { bubbles: true } );
  59. const spy = sinon.spy( evt, 'preventDefault' );
  60. view.element.dispatchEvent( evt );
  61. sinon.assert.calledOnce( spy );
  62. } );
  63. } );
  64. } );
  65. describe( 'element bindings', () => {
  66. describe( 'class', () => {
  67. it( 'reacts on view#isVertical', () => {
  68. view.isVertical = false;
  69. expect( view.element.classList.contains( 'ck-toolbar_vertical' ) ).to.be.false;
  70. view.isVertical = true;
  71. expect( view.element.classList.contains( 'ck-toolbar_vertical' ) ).to.be.true;
  72. } );
  73. it( 'reacts on view#class', () => {
  74. view.class = 'foo';
  75. expect( view.element.classList.contains( 'foo' ) ).to.be.true;
  76. view.class = 'bar';
  77. expect( view.element.classList.contains( 'bar' ) ).to.be.true;
  78. view.class = false;
  79. expect( view.element.classList.contains( 'foo' ) ).to.be.false;
  80. expect( view.element.classList.contains( 'bar' ) ).to.be.false;
  81. } );
  82. } );
  83. } );
  84. describe( 'render()', () => {
  85. it( 'registers #items in #focusTracker', () => {
  86. const view = new ToolbarView( locale );
  87. const spyAdd = sinon.spy( view.focusTracker, 'add' );
  88. const spyRemove = sinon.spy( view.focusTracker, 'remove' );
  89. view.items.add( focusable() );
  90. view.items.add( focusable() );
  91. sinon.assert.notCalled( spyAdd );
  92. view.render();
  93. sinon.assert.calledTwice( spyAdd );
  94. view.items.remove( 1 );
  95. sinon.assert.calledOnce( spyRemove );
  96. view.destroy();
  97. } );
  98. it( 'starts listening for #keystrokes coming from #element', () => {
  99. const view = new ToolbarView();
  100. const spy = sinon.spy( view.keystrokes, 'listenTo' );
  101. view.render();
  102. sinon.assert.calledOnce( spy );
  103. sinon.assert.calledWithExactly( spy, view.element );
  104. view.destroy();
  105. } );
  106. describe( 'activates keyboard navigation for the toolbar', () => {
  107. it( 'so "arrowup" focuses previous focusable item', () => {
  108. const keyEvtData = {
  109. keyCode: keyCodes.arrowup,
  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 "arrowleft" focuses previous focusable item', () => {
  136. const keyEvtData = {
  137. keyCode: keyCodes.arrowleft,
  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( 2 ).element;
  147. const spy = sinon.spy( view.items.get( 0 ), 'focus' );
  148. view.keystrokes.press( keyEvtData );
  149. sinon.assert.calledOnce( spy );
  150. } );
  151. it( 'so "arrowdown" focuses next focusable item', () => {
  152. const keyEvtData = {
  153. keyCode: keyCodes.arrowdown,
  154. preventDefault: sinon.spy(),
  155. stopPropagation: sinon.spy()
  156. };
  157. // No children to focus.
  158. view.keystrokes.press( keyEvtData );
  159. sinon.assert.calledOnce( keyEvtData.preventDefault );
  160. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  161. view.items.add( nonFocusable() );
  162. view.items.add( nonFocusable() );
  163. // No focusable children.
  164. view.keystrokes.press( keyEvtData );
  165. sinon.assert.calledTwice( keyEvtData.preventDefault );
  166. sinon.assert.calledTwice( keyEvtData.stopPropagation );
  167. view.items.add( focusable() );
  168. view.items.add( nonFocusable() );
  169. view.items.add( focusable() );
  170. // Mock the last item is focused.
  171. view.focusTracker.isFocused = true;
  172. view.focusTracker.focusedElement = view.items.get( 4 ).element;
  173. const spy = sinon.spy( view.items.get( 2 ), 'focus' );
  174. view.keystrokes.press( keyEvtData );
  175. sinon.assert.calledThrice( keyEvtData.preventDefault );
  176. sinon.assert.calledThrice( keyEvtData.stopPropagation );
  177. sinon.assert.calledOnce( spy );
  178. } );
  179. it( 'so "arrowright" focuses next focusable item', () => {
  180. const keyEvtData = {
  181. keyCode: keyCodes.arrowright,
  182. preventDefault: sinon.spy(),
  183. stopPropagation: sinon.spy()
  184. };
  185. view.items.add( focusable() );
  186. view.items.add( nonFocusable() );
  187. view.items.add( focusable() );
  188. // Mock the last item is focused.
  189. view.focusTracker.isFocused = true;
  190. view.focusTracker.focusedElement = view.items.get( 0 ).element;
  191. const spy = sinon.spy( view.items.get( 2 ), 'focus' );
  192. view.keystrokes.press( keyEvtData );
  193. sinon.assert.calledOnce( spy );
  194. } );
  195. } );
  196. } );
  197. describe( 'focus()', () => {
  198. it( 'focuses the first focusable item in DOM', () => {
  199. // No children to focus.
  200. view.focus();
  201. // The second child is focusable.
  202. view.items.add( nonFocusable() );
  203. view.items.add( focusable() );
  204. view.items.add( nonFocusable() );
  205. const spy = sinon.spy( view.items.get( 1 ), 'focus' );
  206. view.focus();
  207. sinon.assert.calledOnce( spy );
  208. } );
  209. } );
  210. describe( 'focusLast()', () => {
  211. it( 'focuses the last focusable item in DOM', () => {
  212. // No children to focus.
  213. view.focusLast();
  214. // The second child is focusable.
  215. view.items.add( nonFocusable() );
  216. view.items.add( focusable() );
  217. view.items.add( focusable() );
  218. view.items.add( focusable() );
  219. view.items.add( nonFocusable() );
  220. const spy = sinon.spy( view.items.get( 3 ), 'focus' );
  221. view.focusLast();
  222. sinon.assert.calledOnce( spy );
  223. } );
  224. } );
  225. describe( 'fillFromConfig()', () => {
  226. let factory;
  227. beforeEach( () => {
  228. factory = new ComponentFactory( {} );
  229. factory.add( 'foo', namedFactory( 'foo' ) );
  230. factory.add( 'bar', namedFactory( 'bar' ) );
  231. } );
  232. it( 'expands the config into collection', () => {
  233. view.fillFromConfig( [ 'foo', 'bar', '|', 'foo' ], factory );
  234. const items = view.items;
  235. expect( items ).to.have.length( 4 );
  236. expect( items.get( 0 ).name ).to.equal( 'foo' );
  237. expect( items.get( 1 ).name ).to.equal( 'bar' );
  238. expect( items.get( 2 ) ).to.be.instanceOf( ToolbarSeparatorView );
  239. expect( items.get( 3 ).name ).to.equal( 'foo' );
  240. } );
  241. it( 'warns if there is no such component in the factory', () => {
  242. const items = view.items;
  243. testUtils.sinon.stub( log, 'warn' );
  244. view.fillFromConfig( [ 'foo', 'bar', 'baz' ], factory );
  245. expect( items ).to.have.length( 2 );
  246. expect( items.get( 0 ).name ).to.equal( 'foo' );
  247. expect( items.get( 1 ).name ).to.equal( 'bar' );
  248. sinon.assert.calledOnce( log.warn );
  249. sinon.assert.calledWithExactly( log.warn,
  250. sinon.match( /^toolbarview-item-unavailable/ ),
  251. { name: 'baz' }
  252. );
  253. } );
  254. } );
  255. describe( 'aria', () => {
  256. it( 'poses required attributes', () => {
  257. expect( view.element.getAttribute( 'role' ) ).to.equal( 'toolbar' );
  258. expect( view.element.getAttribute( 'aria-labelledby' ) ).to.equal( view.labelView.element.id )
  259. .and.to.match( /^ck-editor__aria-label_[0-9a-f]{16}/ );
  260. } );
  261. } );
  262. describe( '_createLabelView()', () => {
  263. let labelElement;
  264. beforeEach( () => {
  265. labelElement = view.labelView.element;
  266. } );
  267. it( 'label is attached as last element in toolbar', () => {
  268. expect( labelElement ).to.equal( view.element.lastElementChild );
  269. } );
  270. it( 'label has proper attributes', () => {
  271. view.label = 'Foo';
  272. expect( labelElement.innerHTML ).to.equal( 'Foo' );
  273. expect( labelElement.classList.contains( 'ck' ) ).to.be.true;
  274. expect( labelElement.classList.contains( 'ck-toolbar__label' ) ).to.be.true;
  275. } );
  276. } );
  277. } );
  278. function focusable() {
  279. const view = nonFocusable();
  280. view.focus = () => {};
  281. return view;
  282. }
  283. function nonFocusable() {
  284. const view = new View();
  285. view.element = document.createElement( 'li' );
  286. return view;
  287. }
  288. function namedFactory( name ) {
  289. return locale => {
  290. const view = new View( locale );
  291. view.name = name;
  292. view.element = document.createElement( 'a' );
  293. return view;
  294. };
  295. }