8
0

toolbarview.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. /**
  2. * @license Copyright (c) 2003-2017, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* global document */
  6. import ToolbarView from '../../src/toolbar/toolbarview';
  7. import KeystrokeHandler from '@ckeditor/ckeditor5-utils/src/keystrokehandler';
  8. import FocusTracker from '@ckeditor/ckeditor5-utils/src/focustracker';
  9. import FocusCycler from '../../src/focuscycler';
  10. import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  11. import ViewCollection from '../../src/viewcollection';
  12. import View from '../../src/view';
  13. describe( 'ToolbarView', () => {
  14. let locale, view;
  15. beforeEach( () => {
  16. locale = {};
  17. view = new ToolbarView( locale );
  18. return view.init();
  19. } );
  20. describe( 'constructor()', () => {
  21. it( 'should set view#locale', () => {
  22. expect( view.locale ).to.equal( locale );
  23. } );
  24. it( 'should create view#children collection', () => {
  25. expect( view.items ).to.be.instanceOf( ViewCollection );
  26. } );
  27. it( 'creates #focusTracker instance', () => {
  28. expect( view.focusTracker ).to.be.instanceOf( FocusTracker );
  29. } );
  30. it( 'creates #keystrokes instance', () => {
  31. expect( view.keystrokes ).to.be.instanceOf( KeystrokeHandler );
  32. } );
  33. it( 'creates #_focusCycler instance', () => {
  34. expect( view._focusCycler ).to.be.instanceOf( FocusCycler );
  35. } );
  36. it( 'registers #items in #focusTracker', () => {
  37. const spyAdd = sinon.spy( view.focusTracker, 'add' );
  38. const spyRemove = sinon.spy( view.focusTracker, 'remove' );
  39. view.items.add( focusable() );
  40. view.items.add( focusable() );
  41. sinon.assert.calledTwice( spyAdd );
  42. view.items.remove( 1 );
  43. sinon.assert.calledOnce( spyRemove );
  44. } );
  45. } );
  46. describe( 'template', () => {
  47. it( 'should create element from template', () => {
  48. expect( view.element.classList.contains( 'ck-toolbar' ) ).to.true;
  49. } );
  50. } );
  51. describe( 'init()', () => {
  52. it( 'starts listening for #keystrokes coming from #element', () => {
  53. view = new ToolbarView();
  54. const spy = sinon.spy( view.keystrokes, 'listenTo' );
  55. return view.init().then( () => {
  56. sinon.assert.calledOnce( spy );
  57. sinon.assert.calledWithExactly( spy, view.element );
  58. } );
  59. } );
  60. describe( 'activates keyboard navigation for the toolbar', () => {
  61. it( 'so "arrowup" focuses previous focusable item', () => {
  62. const keyEvtData = {
  63. keyCode: keyCodes.arrowup,
  64. preventDefault: sinon.spy(),
  65. stopPropagation: sinon.spy()
  66. };
  67. // No children to focus.
  68. view.keystrokes.press( keyEvtData );
  69. sinon.assert.calledOnce( keyEvtData.preventDefault );
  70. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  71. view.items.add( nonFocusable() );
  72. view.items.add( nonFocusable() );
  73. // No focusable children.
  74. view.keystrokes.press( keyEvtData );
  75. sinon.assert.calledTwice( keyEvtData.preventDefault );
  76. sinon.assert.calledTwice( keyEvtData.stopPropagation );
  77. view.items.add( focusable() );
  78. view.items.add( nonFocusable() );
  79. view.items.add( focusable() );
  80. // Mock the last item is focused.
  81. view.focusTracker.isFocused = true;
  82. view.focusTracker.focusedElement = view.items.get( 4 ).element;
  83. const spy = sinon.spy( view.items.get( 2 ), 'focus' );
  84. view.keystrokes.press( keyEvtData );
  85. sinon.assert.calledThrice( keyEvtData.preventDefault );
  86. sinon.assert.calledThrice( keyEvtData.stopPropagation );
  87. sinon.assert.calledOnce( spy );
  88. } );
  89. it( 'so "arrowleft" focuses previous focusable item', () => {
  90. const keyEvtData = {
  91. keyCode: keyCodes.arrowleft,
  92. preventDefault: sinon.spy(),
  93. stopPropagation: sinon.spy()
  94. };
  95. view.items.add( focusable() );
  96. view.items.add( nonFocusable() );
  97. view.items.add( focusable() );
  98. // Mock the last item is focused.
  99. view.focusTracker.isFocused = true;
  100. view.focusTracker.focusedElement = view.items.get( 2 ).element;
  101. const spy = sinon.spy( view.items.get( 0 ), 'focus' );
  102. view.keystrokes.press( keyEvtData );
  103. sinon.assert.calledOnce( spy );
  104. } );
  105. it( 'so "arrowdown" focuses next focusable item', () => {
  106. const keyEvtData = {
  107. keyCode: keyCodes.arrowdown,
  108. preventDefault: sinon.spy(),
  109. stopPropagation: sinon.spy()
  110. };
  111. // No children to focus.
  112. view.keystrokes.press( keyEvtData );
  113. sinon.assert.calledOnce( keyEvtData.preventDefault );
  114. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  115. view.items.add( nonFocusable() );
  116. view.items.add( nonFocusable() );
  117. // No focusable children.
  118. view.keystrokes.press( keyEvtData );
  119. sinon.assert.calledTwice( keyEvtData.preventDefault );
  120. sinon.assert.calledTwice( keyEvtData.stopPropagation );
  121. view.items.add( focusable() );
  122. view.items.add( nonFocusable() );
  123. view.items.add( focusable() );
  124. // Mock the last item is focused.
  125. view.focusTracker.isFocused = true;
  126. view.focusTracker.focusedElement = view.items.get( 4 ).element;
  127. const spy = sinon.spy( view.items.get( 2 ), 'focus' );
  128. view.keystrokes.press( keyEvtData );
  129. sinon.assert.calledThrice( keyEvtData.preventDefault );
  130. sinon.assert.calledThrice( keyEvtData.stopPropagation );
  131. sinon.assert.calledOnce( spy );
  132. } );
  133. it( 'so "arrowright" focuses next focusable item', () => {
  134. const keyEvtData = {
  135. keyCode: keyCodes.arrowright,
  136. preventDefault: sinon.spy(),
  137. stopPropagation: sinon.spy()
  138. };
  139. view.items.add( focusable() );
  140. view.items.add( nonFocusable() );
  141. view.items.add( focusable() );
  142. // Mock the last item is focused.
  143. view.focusTracker.isFocused = true;
  144. view.focusTracker.focusedElement = view.items.get( 0 ).element;
  145. const spy = sinon.spy( view.items.get( 2 ), 'focus' );
  146. view.keystrokes.press( keyEvtData );
  147. sinon.assert.calledOnce( spy );
  148. } );
  149. } );
  150. } );
  151. describe( 'focus()', () => {
  152. it( 'focuses the first focusable item in DOM', () => {
  153. // No children to focus.
  154. view.focus();
  155. // The second child is focusable.
  156. view.items.add( nonFocusable() );
  157. view.items.add( focusable() );
  158. view.items.add( nonFocusable() );
  159. const spy = sinon.spy( view.items.get( 1 ), 'focus' );
  160. view.focus();
  161. sinon.assert.calledOnce( spy );
  162. } );
  163. } );
  164. } );
  165. function focusable() {
  166. const view = nonFocusable();
  167. view.focus = () => {};
  168. return view;
  169. }
  170. function nonFocusable() {
  171. const view = new View();
  172. view.element = document.createElement( 'li' );
  173. return view;
  174. }