focuscycler.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. import ViewCollection from '../src/viewcollection';
  6. import View from '../src/view';
  7. import FocusCycler from '../src/focuscycler';
  8. import KeystrokeHandler from '@ckeditor/ckeditor5-utils/src/keystrokehandler';
  9. import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
  10. import global from '@ckeditor/ckeditor5-utils/src/dom/global';
  11. import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
  12. describe( 'FocusCycler', () => {
  13. let focusables, focusTracker, cycler;
  14. testUtils.createSinonSandbox();
  15. beforeEach( () => {
  16. testUtils.sinon.stub( global.window, 'getComputedStyle' );
  17. focusables = new ViewCollection( [
  18. nonFocusable(),
  19. focusable(),
  20. focusable(),
  21. focusable(),
  22. nonFocusable()
  23. ] );
  24. focusTracker = {
  25. focusedElement: null
  26. };
  27. cycler = new FocusCycler( {
  28. focusables,
  29. focusTracker
  30. } );
  31. } );
  32. describe( 'constructor()', () => {
  33. it( 'sets class properties', () => {
  34. expect( cycler.focusables ).to.equal( focusables );
  35. expect( cycler.focusTracker ).to.equal( focusTracker );
  36. } );
  37. } );
  38. describe( 'current()', () => {
  39. it( 'returns null when no view is focused', () => {
  40. expect( cycler.current ).to.equal( null );
  41. focusTracker.focusedElement = focusables.get( 2 ).element;
  42. expect( cycler.current ).to.equal( 2 );
  43. focusTracker.focusedElement = null;
  44. expect( cycler.current ).to.equal( null );
  45. } );
  46. } );
  47. describe( 'first()', () => {
  48. it( 'returns first focusable view', () => {
  49. expect( cycler.first ).to.equal( focusables.get( 1 ) );
  50. } );
  51. it( 'returns null when no focusable items', () => {
  52. focusables = new ViewCollection( [ nonFocusable(), nonFocusable() ] );
  53. cycler = new FocusCycler( { focusables, focusTracker } );
  54. expect( cycler.first ).to.be.null;
  55. } );
  56. it( 'returns null when no items', () => {
  57. focusables = new ViewCollection();
  58. cycler = new FocusCycler( { focusables, focusTracker } );
  59. expect( cycler.first ).to.be.null;
  60. } );
  61. } );
  62. describe( 'last()', () => {
  63. it( 'returns last focusable view', () => {
  64. expect( cycler.last ).to.equal( focusables.get( 3 ) );
  65. } );
  66. it( 'returns null when no focusable items', () => {
  67. focusables = new ViewCollection( [ nonFocusable(), nonFocusable() ] );
  68. cycler = new FocusCycler( { focusables, focusTracker } );
  69. expect( cycler.last ).to.be.null;
  70. } );
  71. it( 'returns null when no items', () => {
  72. focusables = new ViewCollection();
  73. cycler = new FocusCycler( { focusables, focusTracker } );
  74. expect( cycler.last ).to.be.null;
  75. } );
  76. } );
  77. describe( 'next()', () => {
  78. it( 'cycles to return the next focusable view', () => {
  79. focusTracker.focusedElement = focusables.get( 2 ).element;
  80. expect( cycler.next ).to.equal( focusables.get( 3 ) );
  81. focusTracker.focusedElement = focusables.get( 3 ).element;
  82. expect( cycler.next ).to.equal( focusables.get( 1 ) );
  83. focusTracker.focusedElement = focusables.get( 1 ).element;
  84. expect( cycler.next ).to.equal( focusables.get( 2 ) );
  85. } );
  86. it( 'focuses the first focusable view when no view is focused', () => {
  87. focusTracker.focusedElement = null;
  88. expect( cycler.next ).to.equal( focusables.get( 1 ) );
  89. } );
  90. it( 'returns null when no items', () => {
  91. focusables = new ViewCollection();
  92. cycler = new FocusCycler( { focusables, focusTracker } );
  93. expect( cycler.next ).to.be.null;
  94. } );
  95. it( 'returns null when no focusable items', () => {
  96. focusables = new ViewCollection( [ nonFocusable(), nonFocusable() ] );
  97. cycler = new FocusCycler( { focusables, focusTracker } );
  98. expect( cycler.next ).to.be.null;
  99. } );
  100. it( 'returns null if the only focusable in focusables', () => {
  101. focusables = new ViewCollection( [ nonFocusable(), focusable(), nonFocusable() ] );
  102. cycler = new FocusCycler( { focusables, focusTracker } );
  103. focusTracker.focusedElement = focusables.get( 1 ).element;
  104. expect( cycler.first ).to.equal( focusables.get( 1 ) );
  105. expect( cycler.next ).to.be.null;
  106. } );
  107. } );
  108. describe( 'previous()', () => {
  109. it( 'cycles to return the previous focusable view', () => {
  110. focusTracker.focusedElement = focusables.get( 1 ).element;
  111. expect( cycler.previous ).to.equal( focusables.get( 3 ) );
  112. focusTracker.focusedElement = focusables.get( 2 ).element;
  113. expect( cycler.previous ).to.equal( focusables.get( 1 ) );
  114. focusTracker.focusedElement = focusables.get( 3 ).element;
  115. expect( cycler.previous ).to.equal( focusables.get( 2 ) );
  116. } );
  117. it( 'focuses the last focusable view when no view is focused', () => {
  118. focusTracker.focusedElement = null;
  119. expect( cycler.previous ).to.equal( focusables.get( 3 ) );
  120. } );
  121. it( 'returns null when no items', () => {
  122. focusables = new ViewCollection();
  123. cycler = new FocusCycler( { focusables, focusTracker } );
  124. expect( cycler.previous ).to.be.null;
  125. } );
  126. it( 'returns null when no focusable items', () => {
  127. focusables = new ViewCollection( [ nonFocusable(), nonFocusable() ] );
  128. cycler = new FocusCycler( { focusables, focusTracker } );
  129. expect( cycler.previous ).to.be.null;
  130. } );
  131. it( 'returns null if the only focusable in focusables', () => {
  132. focusables = new ViewCollection( [ nonFocusable(), focusable(), nonFocusable() ] );
  133. cycler = new FocusCycler( { focusables, focusTracker } );
  134. focusTracker.focusedElement = focusables.get( 1 ).element;
  135. expect( cycler.first ).to.equal( focusables.get( 1 ) );
  136. expect( cycler.previous ).to.be.null;
  137. } );
  138. } );
  139. describe( 'focusFirst()', () => {
  140. it( 'focuses first focusable view', () => {
  141. cycler.focusFirst();
  142. sinon.assert.calledOnce( focusables.get( 1 ).focus );
  143. } );
  144. it( 'does not throw when no focusable items', () => {
  145. focusables = new ViewCollection( [ nonFocusable(), nonFocusable() ] );
  146. cycler = new FocusCycler( { focusables, focusTracker } );
  147. expect( () => {
  148. cycler.focusFirst();
  149. } ).to.not.throw();
  150. } );
  151. it( 'does not throw when no items', () => {
  152. focusables = new ViewCollection();
  153. cycler = new FocusCycler( { focusables, focusTracker } );
  154. expect( () => {
  155. cycler.focusFirst();
  156. } ).to.not.throw();
  157. } );
  158. it( 'ignores invisible items', () => {
  159. const item = focusable();
  160. focusables = new ViewCollection( [ nonFocusable(), focusable( true ), item ] );
  161. cycler = new FocusCycler( { focusables, focusTracker } );
  162. cycler.focusFirst();
  163. sinon.assert.calledOnce( item.focus );
  164. } );
  165. } );
  166. describe( 'focusLast()', () => {
  167. it( 'focuses last focusable view', () => {
  168. cycler.focusLast();
  169. sinon.assert.calledOnce( focusables.get( 3 ).focus );
  170. } );
  171. it( 'does not throw when no focusable items', () => {
  172. focusables = new ViewCollection( [ nonFocusable(), nonFocusable() ] );
  173. cycler = new FocusCycler( { focusables, focusTracker } );
  174. expect( () => {
  175. cycler.focusLast();
  176. } ).to.not.throw();
  177. } );
  178. it( 'does not throw when no items', () => {
  179. focusables = new ViewCollection();
  180. cycler = new FocusCycler( { focusables, focusTracker } );
  181. expect( () => {
  182. cycler.focusLast();
  183. } ).to.not.throw();
  184. } );
  185. } );
  186. describe( 'focusNext()', () => {
  187. it( 'focuses next focusable view', () => {
  188. focusTracker.focusedElement = focusables.get( 2 ).element;
  189. cycler.focusNext();
  190. sinon.assert.calledOnce( focusables.get( 3 ).focus );
  191. } );
  192. it( 'does not throw when no focusable items', () => {
  193. focusables = new ViewCollection( [ nonFocusable(), nonFocusable() ] );
  194. cycler = new FocusCycler( { focusables, focusTracker } );
  195. expect( () => {
  196. cycler.focusNext();
  197. } ).to.not.throw();
  198. } );
  199. it( 'does not throw when no items', () => {
  200. focusables = new ViewCollection();
  201. cycler = new FocusCycler( { focusables, focusTracker } );
  202. expect( () => {
  203. cycler.focusNext();
  204. } ).to.not.throw();
  205. } );
  206. } );
  207. describe( 'focusPrevious()', () => {
  208. it( 'focuses previous focusable view', () => {
  209. focusTracker.focusedElement = focusables.get( 1 ).element;
  210. cycler.focusPrevious();
  211. sinon.assert.calledOnce( focusables.get( 3 ).focus );
  212. } );
  213. it( 'does not throw when no focusable items', () => {
  214. focusables = new ViewCollection( [ nonFocusable(), nonFocusable() ] );
  215. cycler = new FocusCycler( { focusables, focusTracker } );
  216. expect( () => {
  217. cycler.focusPrevious();
  218. } ).to.not.throw();
  219. } );
  220. it( 'does not throw when no items', () => {
  221. focusables = new ViewCollection();
  222. cycler = new FocusCycler( { focusables, focusTracker } );
  223. expect( () => {
  224. cycler.focusPrevious();
  225. } ).to.not.throw();
  226. } );
  227. } );
  228. describe( 'keystrokes', () => {
  229. it( 'creates event listeners', () => {
  230. const keystrokeHandler = new KeystrokeHandler();
  231. cycler = new FocusCycler( {
  232. focusables, focusTracker, keystrokeHandler,
  233. actions: {
  234. focusPrevious: 'arrowup',
  235. focusNext: 'arrowdown'
  236. }
  237. } );
  238. const keyEvtData = {
  239. keyCode: keyCodes.arrowup,
  240. preventDefault: sinon.spy(),
  241. stopPropagation: sinon.spy()
  242. };
  243. const spy1 = sinon.spy( cycler, 'focusPrevious' );
  244. const spy2 = sinon.spy( cycler, 'focusNext' );
  245. keystrokeHandler.press( keyEvtData );
  246. sinon.assert.calledOnce( spy1 );
  247. sinon.assert.calledOnce( keyEvtData.preventDefault );
  248. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  249. sinon.assert.notCalled( spy2 );
  250. keyEvtData.keyCode = keyCodes.arrowdown;
  251. keystrokeHandler.press( keyEvtData );
  252. sinon.assert.calledOnce( spy1 );
  253. sinon.assert.calledTwice( keyEvtData.preventDefault );
  254. sinon.assert.calledTwice( keyEvtData.stopPropagation );
  255. sinon.assert.calledOnce( spy2 );
  256. } );
  257. it( 'supports array keystroke syntax', () => {
  258. const keystrokeHandler = new KeystrokeHandler();
  259. cycler = new FocusCycler( {
  260. focusables, focusTracker, keystrokeHandler,
  261. actions: {
  262. focusPrevious: [ 'arrowup', 'arrowleft' ]
  263. }
  264. } );
  265. const keyEvtData = {
  266. keyCode: keyCodes.arrowleft,
  267. preventDefault: sinon.spy(),
  268. stopPropagation: sinon.spy()
  269. };
  270. const spy = sinon.spy( cycler, 'focusPrevious' );
  271. keystrokeHandler.press( keyEvtData );
  272. sinon.assert.calledOnce( spy );
  273. sinon.assert.calledOnce( keyEvtData.preventDefault );
  274. sinon.assert.calledOnce( keyEvtData.stopPropagation );
  275. } );
  276. } );
  277. } );
  278. function nonFocusable( isHidden ) {
  279. const view = new View();
  280. view.element = Math.random();
  281. global.window.getComputedStyle
  282. .withArgs( view.element )
  283. .returns( {
  284. display: isHidden ? 'none' : 'block'
  285. } );
  286. return view;
  287. }
  288. function focusable( isHidden ) {
  289. const view = nonFocusable( isHidden );
  290. view.focus = sinon.spy();
  291. return view;
  292. }