| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382 |
- /**
- * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
- */
- import ViewCollection from '../src/viewcollection';
- import View from '../src/view';
- import FocusCycler from '../src/focuscycler';
- import KeystrokeHandler from '@ckeditor/ckeditor5-utils/src/keystrokehandler';
- import { keyCodes } from '@ckeditor/ckeditor5-utils/src/keyboard';
- import global from '@ckeditor/ckeditor5-utils/src/dom/global';
- import testUtils from '@ckeditor/ckeditor5-core/tests/_utils/utils';
- describe( 'FocusCycler', () => {
- let focusables, focusTracker, cycler;
- testUtils.createSinonSandbox();
- beforeEach( () => {
- testUtils.sinon.stub( global.window, 'getComputedStyle' );
- focusables = new ViewCollection( [
- nonFocusable(),
- focusable(),
- focusable(),
- focusable(),
- nonFocusable()
- ] );
- focusTracker = {
- focusedElement: null
- };
- cycler = new FocusCycler( {
- focusables,
- focusTracker
- } );
- } );
- describe( 'constructor()', () => {
- it( 'sets class properties', () => {
- expect( cycler.focusables ).to.equal( focusables );
- expect( cycler.focusTracker ).to.equal( focusTracker );
- } );
- } );
- describe( 'current()', () => {
- it( 'returns null when no view is focused', () => {
- expect( cycler.current ).to.equal( null );
- focusTracker.focusedElement = focusables.get( 2 ).element;
- expect( cycler.current ).to.equal( 2 );
- focusTracker.focusedElement = null;
- expect( cycler.current ).to.equal( null );
- } );
- } );
- describe( 'first()', () => {
- it( 'returns first focusable view', () => {
- expect( cycler.first ).to.equal( focusables.get( 1 ) );
- } );
- it( 'returns null when no focusable items', () => {
- focusables = new ViewCollection( [ nonFocusable(), nonFocusable() ] );
- cycler = new FocusCycler( { focusables, focusTracker } );
- expect( cycler.first ).to.be.null;
- } );
- it( 'returns null when no items', () => {
- focusables = new ViewCollection();
- cycler = new FocusCycler( { focusables, focusTracker } );
- expect( cycler.first ).to.be.null;
- } );
- } );
- describe( 'last()', () => {
- it( 'returns last focusable view', () => {
- expect( cycler.last ).to.equal( focusables.get( 3 ) );
- } );
- it( 'returns null when no focusable items', () => {
- focusables = new ViewCollection( [ nonFocusable(), nonFocusable() ] );
- cycler = new FocusCycler( { focusables, focusTracker } );
- expect( cycler.last ).to.be.null;
- } );
- it( 'returns null when no items', () => {
- focusables = new ViewCollection();
- cycler = new FocusCycler( { focusables, focusTracker } );
- expect( cycler.last ).to.be.null;
- } );
- } );
- describe( 'next()', () => {
- it( 'cycles to return the next focusable view', () => {
- focusTracker.focusedElement = focusables.get( 2 ).element;
- expect( cycler.next ).to.equal( focusables.get( 3 ) );
- focusTracker.focusedElement = focusables.get( 3 ).element;
- expect( cycler.next ).to.equal( focusables.get( 1 ) );
- focusTracker.focusedElement = focusables.get( 1 ).element;
- expect( cycler.next ).to.equal( focusables.get( 2 ) );
- } );
- it( 'focuses the first focusable view when no view is focused', () => {
- focusTracker.focusedElement = null;
- expect( cycler.next ).to.equal( focusables.get( 1 ) );
- } );
- it( 'returns null when no items', () => {
- focusables = new ViewCollection();
- cycler = new FocusCycler( { focusables, focusTracker } );
- expect( cycler.next ).to.be.null;
- } );
- it( 'returns null when no focusable items', () => {
- focusables = new ViewCollection( [ nonFocusable(), nonFocusable() ] );
- cycler = new FocusCycler( { focusables, focusTracker } );
- expect( cycler.next ).to.be.null;
- } );
- it( 'returns null if the only focusable in focusables', () => {
- focusables = new ViewCollection( [ nonFocusable(), focusable(), nonFocusable() ] );
- cycler = new FocusCycler( { focusables, focusTracker } );
- focusTracker.focusedElement = focusables.get( 1 ).element;
- expect( cycler.first ).to.equal( focusables.get( 1 ) );
- expect( cycler.next ).to.be.null;
- } );
- } );
- describe( 'previous()', () => {
- it( 'cycles to return the previous focusable view', () => {
- focusTracker.focusedElement = focusables.get( 1 ).element;
- expect( cycler.previous ).to.equal( focusables.get( 3 ) );
- focusTracker.focusedElement = focusables.get( 2 ).element;
- expect( cycler.previous ).to.equal( focusables.get( 1 ) );
- focusTracker.focusedElement = focusables.get( 3 ).element;
- expect( cycler.previous ).to.equal( focusables.get( 2 ) );
- } );
- it( 'focuses the last focusable view when no view is focused', () => {
- focusTracker.focusedElement = null;
- expect( cycler.previous ).to.equal( focusables.get( 3 ) );
- } );
- it( 'returns null when no items', () => {
- focusables = new ViewCollection();
- cycler = new FocusCycler( { focusables, focusTracker } );
- expect( cycler.previous ).to.be.null;
- } );
- it( 'returns null when no focusable items', () => {
- focusables = new ViewCollection( [ nonFocusable(), nonFocusable() ] );
- cycler = new FocusCycler( { focusables, focusTracker } );
- expect( cycler.previous ).to.be.null;
- } );
- it( 'returns null if the only focusable in focusables', () => {
- focusables = new ViewCollection( [ nonFocusable(), focusable(), nonFocusable() ] );
- cycler = new FocusCycler( { focusables, focusTracker } );
- focusTracker.focusedElement = focusables.get( 1 ).element;
- expect( cycler.first ).to.equal( focusables.get( 1 ) );
- expect( cycler.previous ).to.be.null;
- } );
- } );
- describe( 'focusFirst()', () => {
- it( 'focuses first focusable view', () => {
- cycler.focusFirst();
- sinon.assert.calledOnce( focusables.get( 1 ).focus );
- } );
- it( 'does not throw when no focusable items', () => {
- focusables = new ViewCollection( [ nonFocusable(), nonFocusable() ] );
- cycler = new FocusCycler( { focusables, focusTracker } );
- expect( () => {
- cycler.focusFirst();
- } ).to.not.throw();
- } );
- it( 'does not throw when no items', () => {
- focusables = new ViewCollection();
- cycler = new FocusCycler( { focusables, focusTracker } );
- expect( () => {
- cycler.focusFirst();
- } ).to.not.throw();
- } );
- it( 'ignores invisible items', () => {
- const item = focusable();
- focusables = new ViewCollection( [ nonFocusable(), focusable( true ), item ] );
- cycler = new FocusCycler( { focusables, focusTracker } );
- cycler.focusFirst();
- sinon.assert.calledOnce( item.focus );
- } );
- } );
- describe( 'focusLast()', () => {
- it( 'focuses last focusable view', () => {
- cycler.focusLast();
- sinon.assert.calledOnce( focusables.get( 3 ).focus );
- } );
- it( 'does not throw when no focusable items', () => {
- focusables = new ViewCollection( [ nonFocusable(), nonFocusable() ] );
- cycler = new FocusCycler( { focusables, focusTracker } );
- expect( () => {
- cycler.focusLast();
- } ).to.not.throw();
- } );
- it( 'does not throw when no items', () => {
- focusables = new ViewCollection();
- cycler = new FocusCycler( { focusables, focusTracker } );
- expect( () => {
- cycler.focusLast();
- } ).to.not.throw();
- } );
- } );
- describe( 'focusNext()', () => {
- it( 'focuses next focusable view', () => {
- focusTracker.focusedElement = focusables.get( 2 ).element;
- cycler.focusNext();
- sinon.assert.calledOnce( focusables.get( 3 ).focus );
- } );
- it( 'does not throw when no focusable items', () => {
- focusables = new ViewCollection( [ nonFocusable(), nonFocusable() ] );
- cycler = new FocusCycler( { focusables, focusTracker } );
- expect( () => {
- cycler.focusNext();
- } ).to.not.throw();
- } );
- it( 'does not throw when no items', () => {
- focusables = new ViewCollection();
- cycler = new FocusCycler( { focusables, focusTracker } );
- expect( () => {
- cycler.focusNext();
- } ).to.not.throw();
- } );
- } );
- describe( 'focusPrevious()', () => {
- it( 'focuses previous focusable view', () => {
- focusTracker.focusedElement = focusables.get( 1 ).element;
- cycler.focusPrevious();
- sinon.assert.calledOnce( focusables.get( 3 ).focus );
- } );
- it( 'does not throw when no focusable items', () => {
- focusables = new ViewCollection( [ nonFocusable(), nonFocusable() ] );
- cycler = new FocusCycler( { focusables, focusTracker } );
- expect( () => {
- cycler.focusPrevious();
- } ).to.not.throw();
- } );
- it( 'does not throw when no items', () => {
- focusables = new ViewCollection();
- cycler = new FocusCycler( { focusables, focusTracker } );
- expect( () => {
- cycler.focusPrevious();
- } ).to.not.throw();
- } );
- } );
- describe( 'keystrokes', () => {
- it( 'creates event listeners', () => {
- const keystrokeHandler = new KeystrokeHandler();
- cycler = new FocusCycler( {
- focusables, focusTracker, keystrokeHandler,
- actions: {
- focusPrevious: 'arrowup',
- focusNext: 'arrowdown'
- }
- } );
- const keyEvtData = {
- keyCode: keyCodes.arrowup,
- preventDefault: sinon.spy(),
- stopPropagation: sinon.spy()
- };
- const spy1 = sinon.spy( cycler, 'focusPrevious' );
- const spy2 = sinon.spy( cycler, 'focusNext' );
- keystrokeHandler.press( keyEvtData );
- sinon.assert.calledOnce( spy1 );
- sinon.assert.calledOnce( keyEvtData.preventDefault );
- sinon.assert.calledOnce( keyEvtData.stopPropagation );
- sinon.assert.notCalled( spy2 );
- keyEvtData.keyCode = keyCodes.arrowdown;
- keystrokeHandler.press( keyEvtData );
- sinon.assert.calledOnce( spy1 );
- sinon.assert.calledTwice( keyEvtData.preventDefault );
- sinon.assert.calledTwice( keyEvtData.stopPropagation );
- sinon.assert.calledOnce( spy2 );
- } );
- it( 'supports array keystroke syntax', () => {
- const keystrokeHandler = new KeystrokeHandler();
- cycler = new FocusCycler( {
- focusables, focusTracker, keystrokeHandler,
- actions: {
- focusPrevious: [ 'arrowup', 'arrowleft' ]
- }
- } );
- const keyEvtData = {
- keyCode: keyCodes.arrowleft,
- preventDefault: sinon.spy(),
- stopPropagation: sinon.spy()
- };
- const spy = sinon.spy( cycler, 'focusPrevious' );
- keystrokeHandler.press( keyEvtData );
- sinon.assert.calledOnce( spy );
- sinon.assert.calledOnce( keyEvtData.preventDefault );
- sinon.assert.calledOnce( keyEvtData.stopPropagation );
- } );
- } );
- } );
- function nonFocusable( isHidden ) {
- const view = new View();
- view.element = Math.random();
- global.window.getComputedStyle
- .withArgs( view.element )
- .returns( {
- display: isHidden ? 'none' : 'block'
- } );
- return view;
- }
- function focusable( isHidden ) {
- const view = nonFocusable( isHidden );
- view.focus = sinon.spy();
- return view;
- }
|