toolbarview.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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, console */
  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. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  16. import { add as addTranslations, _clear as clearTranslations } from '@ckeditor/ckeditor5-utils/src/translation-service';
  17. import Locale from '@ckeditor/ckeditor5-utils/src/locale';
  18. describe( 'ToolbarView', () => {
  19. let locale, view;
  20. testUtils.createSinonSandbox();
  21. before( () => {
  22. addTranslations( 'pl', {
  23. 'Editor\'s toolbar': 'Pasek narzędzi edytora'
  24. } );
  25. addTranslations( 'en', {
  26. 'Editor\'s toolbar': 'Editor\'s toolbar'
  27. } );
  28. } );
  29. after( () => {
  30. clearTranslations();
  31. } );
  32. beforeEach( () => {
  33. locale = new Locale( 'en' );
  34. view = new ToolbarView( locale );
  35. view.render();
  36. } );
  37. afterEach( () => {
  38. sinon.restore();
  39. view.destroy();
  40. } );
  41. describe( 'constructor()', () => {
  42. it( 'should set view#locale', () => {
  43. expect( view.locale ).to.equal( locale );
  44. } );
  45. it( 'should set view#isVertical', () => {
  46. expect( view.isVertical ).to.be.false;
  47. } );
  48. it( 'should create view#children collection', () => {
  49. expect( view.items ).to.be.instanceOf( ViewCollection );
  50. } );
  51. it( 'creates #focusTracker instance', () => {
  52. expect( view.focusTracker ).to.be.instanceOf( FocusTracker );
  53. } );
  54. it( 'creates #keystrokes instance', () => {
  55. expect( view.keystrokes ).to.be.instanceOf( KeystrokeHandler );
  56. } );
  57. it( 'creates #_focusCycler instance', () => {
  58. expect( view._focusCycler ).to.be.instanceOf( FocusCycler );
  59. } );
  60. } );
  61. describe( 'template', () => {
  62. it( 'should create element from template', () => {
  63. expect( view.element.classList.contains( 'ck' ) ).to.true;
  64. expect( view.element.classList.contains( 'ck-toolbar' ) ).to.true;
  65. } );
  66. describe( 'event listeners', () => {
  67. it( 'prevent default on #mousedown', () => {
  68. const evt = new Event( 'mousedown', { bubbles: true } );
  69. const spy = sinon.spy( evt, 'preventDefault' );
  70. view.element.dispatchEvent( evt );
  71. sinon.assert.calledOnce( spy );
  72. } );
  73. } );
  74. } );
  75. describe( 'element bindings', () => {
  76. describe( 'class', () => {
  77. it( 'reacts on view#isVertical', () => {
  78. view.isVertical = false;
  79. expect( view.element.classList.contains( 'ck-toolbar_vertical' ) ).to.be.false;
  80. view.isVertical = true;
  81. expect( view.element.classList.contains( 'ck-toolbar_vertical' ) ).to.be.true;
  82. } );
  83. it( 'reacts on view#class', () => {
  84. view.class = 'foo';
  85. expect( view.element.classList.contains( 'foo' ) ).to.be.true;
  86. view.class = 'bar';
  87. expect( view.element.classList.contains( 'bar' ) ).to.be.true;
  88. view.class = false;
  89. expect( view.element.classList.contains( 'foo' ) ).to.be.false;
  90. expect( view.element.classList.contains( 'bar' ) ).to.be.false;
  91. } );
  92. } );
  93. } );
  94. describe( 'render()', () => {
  95. it( 'registers #items in #focusTracker', () => {
  96. const view = new ToolbarView( locale );
  97. const spyAdd = sinon.spy( view.focusTracker, 'add' );
  98. const spyRemove = sinon.spy( view.focusTracker, 'remove' );
  99. view.items.add( focusable() );
  100. view.items.add( focusable() );
  101. sinon.assert.notCalled( spyAdd );
  102. view.render();
  103. sinon.assert.calledTwice( spyAdd );
  104. view.items.remove( 1 );
  105. sinon.assert.calledOnce( spyRemove );
  106. view.destroy();
  107. } );
  108. it( 'starts listening for #keystrokes coming from #element', () => {
  109. const view = new ToolbarView( new Locale( 'en' ) );
  110. const spy = sinon.spy( view.keystrokes, 'listenTo' );
  111. view.render();
  112. sinon.assert.calledOnce( spy );
  113. sinon.assert.calledWithExactly( spy, view.element );
  114. view.destroy();
  115. } );
  116. describe( 'activates keyboard navigation for the toolbar', () => {
  117. it( 'so "arrowup" focuses previous focusable item', () => {
  118. const keyEvtData = {
  119. keyCode: keyCodes.arrowup,
  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 "arrowleft" focuses previous focusable item', () => {
  146. const keyEvtData = {
  147. keyCode: keyCodes.arrowleft,
  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( 2 ).element;
  157. const spy = sinon.spy( view.items.get( 0 ), 'focus' );
  158. view.keystrokes.press( keyEvtData );
  159. sinon.assert.calledOnce( spy );
  160. } );
  161. it( 'so "arrowdown" focuses next focusable item', () => {
  162. const keyEvtData = {
  163. keyCode: keyCodes.arrowdown,
  164. preventDefault: sinon.spy(),
  165. stopPropagation: sinon.spy()
  166. };
  167. // No children to focus.
  168. view.keystrokes.press( keyEvtData );
  169. sinon.assert.calledOnce( keyEvtData.preventDefault );
  170. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  171. view.items.add( nonFocusable() );
  172. view.items.add( nonFocusable() );
  173. // No focusable children.
  174. view.keystrokes.press( keyEvtData );
  175. sinon.assert.calledTwice( keyEvtData.preventDefault );
  176. sinon.assert.calledTwice( keyEvtData.stopPropagation );
  177. view.items.add( focusable() );
  178. view.items.add( nonFocusable() );
  179. view.items.add( focusable() );
  180. // Mock the last item is focused.
  181. view.focusTracker.isFocused = true;
  182. view.focusTracker.focusedElement = view.items.get( 4 ).element;
  183. const spy = sinon.spy( view.items.get( 2 ), 'focus' );
  184. view.keystrokes.press( keyEvtData );
  185. sinon.assert.calledThrice( keyEvtData.preventDefault );
  186. sinon.assert.calledThrice( keyEvtData.stopPropagation );
  187. sinon.assert.calledOnce( spy );
  188. } );
  189. it( 'so "arrowright" focuses next focusable item', () => {
  190. const keyEvtData = {
  191. keyCode: keyCodes.arrowright,
  192. preventDefault: sinon.spy(),
  193. stopPropagation: sinon.spy()
  194. };
  195. view.items.add( focusable() );
  196. view.items.add( nonFocusable() );
  197. view.items.add( focusable() );
  198. // Mock the last item is focused.
  199. view.focusTracker.isFocused = true;
  200. view.focusTracker.focusedElement = view.items.get( 0 ).element;
  201. const spy = sinon.spy( view.items.get( 2 ), 'focus' );
  202. view.keystrokes.press( keyEvtData );
  203. sinon.assert.calledOnce( spy );
  204. } );
  205. } );
  206. } );
  207. describe( 'focus()', () => {
  208. it( 'focuses the first focusable item in DOM', () => {
  209. // No children to focus.
  210. view.focus();
  211. // The second child is focusable.
  212. view.items.add( nonFocusable() );
  213. view.items.add( focusable() );
  214. view.items.add( nonFocusable() );
  215. const spy = sinon.spy( view.items.get( 1 ), 'focus' );
  216. view.focus();
  217. sinon.assert.calledOnce( spy );
  218. } );
  219. } );
  220. describe( 'focusLast()', () => {
  221. it( 'focuses the last focusable item in DOM', () => {
  222. // No children to focus.
  223. view.focusLast();
  224. // The second child is focusable.
  225. view.items.add( nonFocusable() );
  226. view.items.add( focusable() );
  227. view.items.add( focusable() );
  228. view.items.add( focusable() );
  229. view.items.add( nonFocusable() );
  230. const spy = sinon.spy( view.items.get( 3 ), 'focus' );
  231. view.focusLast();
  232. sinon.assert.calledOnce( spy );
  233. } );
  234. } );
  235. describe( 'fillFromConfig()', () => {
  236. let factory;
  237. beforeEach( () => {
  238. factory = new ComponentFactory( {} );
  239. factory.add( 'foo', namedFactory( 'foo' ) );
  240. factory.add( 'bar', namedFactory( 'bar' ) );
  241. } );
  242. it( 'expands the config into collection', () => {
  243. view.fillFromConfig( [ 'foo', 'bar', '|', 'foo' ], factory );
  244. const items = view.items;
  245. expect( items ).to.have.length( 4 );
  246. expect( items.get( 0 ).name ).to.equal( 'foo' );
  247. expect( items.get( 1 ).name ).to.equal( 'bar' );
  248. expect( items.get( 2 ) ).to.be.instanceOf( ToolbarSeparatorView );
  249. expect( items.get( 3 ).name ).to.equal( 'foo' );
  250. } );
  251. it( 'warns if there is no such component in the factory', () => {
  252. const items = view.items;
  253. const consoleWarnStub = sinon.stub( console, 'warn' );
  254. view.fillFromConfig( [ 'foo', 'bar', 'baz' ], factory );
  255. expect( items ).to.have.length( 2 );
  256. expect( items.get( 0 ).name ).to.equal( 'foo' );
  257. expect( items.get( 1 ).name ).to.equal( 'bar' );
  258. sinon.assert.calledOnce( consoleWarnStub );
  259. sinon.assert.calledWithExactly( consoleWarnStub,
  260. sinon.match( /^toolbarview-item-unavailable/ ),
  261. { name: 'baz' }
  262. );
  263. } );
  264. } );
  265. describe( 'aria', () => {
  266. it( 'should poses required attributes', () => {
  267. expect( view.element.getAttribute( 'role' ) ).to.equal( 'toolbar' );
  268. expect( view.element.getAttribute( 'aria-label' ) ).to.equal( 'Editor\'s toolbar' );
  269. } );
  270. it( 'should apply custom aria label', () => {
  271. const view = new ToolbarView( locale, { ariaLabel: 'Custom label' } );
  272. view.render();
  273. expect( view.element.getAttribute( 'aria-label' ) ).to.equal( 'Custom label' );
  274. } );
  275. } );
  276. describe( 'localization', () => {
  277. it( 'should have translated aria label', () => {
  278. const view = new ToolbarView( new Locale( 'pl' ) );
  279. view.render();
  280. expect( view.element.getAttribute( 'aria-label' ) ).to.equal( 'Pasek narzędzi edytora' );
  281. } );
  282. } );
  283. } );
  284. function focusable() {
  285. const view = nonFocusable();
  286. view.focus = () => {};
  287. return view;
  288. }
  289. function nonFocusable() {
  290. const view = new View();
  291. view.element = document.createElement( 'li' );
  292. return view;
  293. }
  294. function namedFactory( name ) {
  295. return locale => {
  296. const view = new View( locale );
  297. view.name = name;
  298. view.element = document.createElement( 'a' );
  299. return view;
  300. };
  301. }