focuscycler.js 10 KB

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