focuscycler.js 11 KB

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