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