toolbarview.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 View from '../../src/view';
  15. describe( 'ToolbarView', () => {
  16. let locale, view;
  17. beforeEach( () => {
  18. locale = {};
  19. view = new ToolbarView( locale );
  20. view.init();
  21. } );
  22. describe( 'constructor()', () => {
  23. it( 'should set view#locale', () => {
  24. expect( view.locale ).to.equal( locale );
  25. } );
  26. it( 'should create view#children collection', () => {
  27. expect( view.items ).to.be.instanceOf( ViewCollection );
  28. } );
  29. it( 'creates #focusTracker instance', () => {
  30. expect( view.focusTracker ).to.be.instanceOf( FocusTracker );
  31. } );
  32. it( 'creates #keystrokes instance', () => {
  33. expect( view.keystrokes ).to.be.instanceOf( KeystrokeHandler );
  34. } );
  35. it( 'creates #_focusCycler instance', () => {
  36. expect( view._focusCycler ).to.be.instanceOf( FocusCycler );
  37. } );
  38. it( 'registers #items in #focusTracker', () => {
  39. const spyAdd = sinon.spy( view.focusTracker, 'add' );
  40. const spyRemove = sinon.spy( view.focusTracker, 'remove' );
  41. view.items.add( focusable() );
  42. view.items.add( focusable() );
  43. sinon.assert.calledTwice( spyAdd );
  44. view.items.remove( 1 );
  45. sinon.assert.calledOnce( spyRemove );
  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( 'init()', () => {
  62. it( 'starts listening for #keystrokes coming from #element', () => {
  63. view = new ToolbarView();
  64. const spy = sinon.spy( view.keystrokes, 'listenTo' );
  65. view.init();
  66. sinon.assert.calledOnce( spy );
  67. sinon.assert.calledWithExactly( spy, view.element );
  68. } );
  69. describe( 'activates keyboard navigation for the toolbar', () => {
  70. it( 'so "arrowup" focuses previous focusable item', () => {
  71. const keyEvtData = {
  72. keyCode: keyCodes.arrowup,
  73. preventDefault: sinon.spy(),
  74. stopPropagation: sinon.spy()
  75. };
  76. // No children to focus.
  77. view.keystrokes.press( keyEvtData );
  78. sinon.assert.calledOnce( keyEvtData.preventDefault );
  79. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  80. view.items.add( nonFocusable() );
  81. view.items.add( nonFocusable() );
  82. // No focusable children.
  83. view.keystrokes.press( keyEvtData );
  84. sinon.assert.calledTwice( keyEvtData.preventDefault );
  85. sinon.assert.calledTwice( keyEvtData.stopPropagation );
  86. view.items.add( focusable() );
  87. view.items.add( nonFocusable() );
  88. view.items.add( focusable() );
  89. // Mock the last item is focused.
  90. view.focusTracker.isFocused = true;
  91. view.focusTracker.focusedElement = view.items.get( 4 ).element;
  92. const spy = sinon.spy( view.items.get( 2 ), 'focus' );
  93. view.keystrokes.press( keyEvtData );
  94. sinon.assert.calledThrice( keyEvtData.preventDefault );
  95. sinon.assert.calledThrice( keyEvtData.stopPropagation );
  96. sinon.assert.calledOnce( spy );
  97. } );
  98. it( 'so "arrowleft" focuses previous focusable item', () => {
  99. const keyEvtData = {
  100. keyCode: keyCodes.arrowleft,
  101. preventDefault: sinon.spy(),
  102. stopPropagation: sinon.spy()
  103. };
  104. view.items.add( focusable() );
  105. view.items.add( nonFocusable() );
  106. view.items.add( focusable() );
  107. // Mock the last item is focused.
  108. view.focusTracker.isFocused = true;
  109. view.focusTracker.focusedElement = view.items.get( 2 ).element;
  110. const spy = sinon.spy( view.items.get( 0 ), 'focus' );
  111. view.keystrokes.press( keyEvtData );
  112. sinon.assert.calledOnce( spy );
  113. } );
  114. it( 'so "arrowdown" focuses next focusable item', () => {
  115. const keyEvtData = {
  116. keyCode: keyCodes.arrowdown,
  117. preventDefault: sinon.spy(),
  118. stopPropagation: sinon.spy()
  119. };
  120. // No children to focus.
  121. view.keystrokes.press( keyEvtData );
  122. sinon.assert.calledOnce( keyEvtData.preventDefault );
  123. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  124. view.items.add( nonFocusable() );
  125. view.items.add( nonFocusable() );
  126. // No focusable children.
  127. view.keystrokes.press( keyEvtData );
  128. sinon.assert.calledTwice( keyEvtData.preventDefault );
  129. sinon.assert.calledTwice( keyEvtData.stopPropagation );
  130. view.items.add( focusable() );
  131. view.items.add( nonFocusable() );
  132. view.items.add( focusable() );
  133. // Mock the last item is focused.
  134. view.focusTracker.isFocused = true;
  135. view.focusTracker.focusedElement = view.items.get( 4 ).element;
  136. const spy = sinon.spy( view.items.get( 2 ), 'focus' );
  137. view.keystrokes.press( keyEvtData );
  138. sinon.assert.calledThrice( keyEvtData.preventDefault );
  139. sinon.assert.calledThrice( keyEvtData.stopPropagation );
  140. sinon.assert.calledOnce( spy );
  141. } );
  142. it( 'so "arrowright" focuses next focusable item', () => {
  143. const keyEvtData = {
  144. keyCode: keyCodes.arrowright,
  145. preventDefault: sinon.spy(),
  146. stopPropagation: sinon.spy()
  147. };
  148. view.items.add( focusable() );
  149. view.items.add( nonFocusable() );
  150. view.items.add( focusable() );
  151. // Mock the last item is focused.
  152. view.focusTracker.isFocused = true;
  153. view.focusTracker.focusedElement = view.items.get( 0 ).element;
  154. const spy = sinon.spy( view.items.get( 2 ), 'focus' );
  155. view.keystrokes.press( keyEvtData );
  156. sinon.assert.calledOnce( spy );
  157. } );
  158. } );
  159. } );
  160. describe( 'focus()', () => {
  161. it( 'focuses the first focusable item in DOM', () => {
  162. // No children to focus.
  163. view.focus();
  164. // The second child is focusable.
  165. view.items.add( nonFocusable() );
  166. view.items.add( focusable() );
  167. view.items.add( nonFocusable() );
  168. const spy = sinon.spy( view.items.get( 1 ), 'focus' );
  169. view.focus();
  170. sinon.assert.calledOnce( spy );
  171. } );
  172. } );
  173. describe( 'fillFromConfig()', () => {
  174. let factory;
  175. beforeEach( () => {
  176. factory = new ComponentFactory( {} );
  177. factory.add( 'foo', namedFactory( 'foo' ) );
  178. factory.add( 'bar', namedFactory( 'bar' ) );
  179. } );
  180. it( 'does not throw when no config is provided', () => {
  181. expect( () => {
  182. view.fillFromConfig();
  183. } ).to.not.throw();
  184. } );
  185. it( 'expands the config into collection', () => {
  186. view.fillFromConfig( [ 'foo', 'bar', '|', 'foo' ], factory );
  187. const items = view.items;
  188. expect( items ).to.have.length( 4 );
  189. expect( items.get( 0 ).name ).to.equal( 'foo' );
  190. expect( items.get( 1 ).name ).to.equal( 'bar' );
  191. expect( items.get( 2 ) ).to.be.instanceOf( ToolbarSeparatorView );
  192. expect( items.get( 3 ).name ).to.equal( 'foo' );
  193. } );
  194. } );
  195. } );
  196. function focusable() {
  197. const view = nonFocusable();
  198. view.focus = () => {};
  199. return view;
  200. }
  201. function nonFocusable() {
  202. const view = new View();
  203. view.element = document.createElement( 'li' );
  204. return view;
  205. }
  206. function namedFactory( name ) {
  207. return locale => {
  208. const view = new View( locale );
  209. view.name = name;
  210. view.element = document.createElement( 'a' );
  211. return view;
  212. };
  213. }