8
0

focusobserver.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /**
  2. * @license Copyright (c) 2003-2015, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* globals document */
  6. /* bender-tags: view, browser-only */
  7. import FocusObserver from '/ckeditor5/engine/view/observer/focusobserver.js';
  8. import ViewDocument from '/ckeditor5/engine/view/document.js';
  9. import ViewRange from '/ckeditor5/engine/view/range.js';
  10. describe( 'FocusObserver', () => {
  11. let viewDocument, observer;
  12. beforeEach( () => {
  13. viewDocument = new ViewDocument();
  14. observer = viewDocument.getObserver( FocusObserver );
  15. } );
  16. it( 'should define domEventType', () => {
  17. expect( observer.domEventType ).to.deep.equal( [ 'focus', 'blur' ] );
  18. } );
  19. describe( 'onDomEvent', () => {
  20. it( 'should fire focus with the right event data', () => {
  21. const spy = sinon.spy();
  22. viewDocument.on( 'focus', spy );
  23. observer.onDomEvent( { type: 'focus', target: document.body } );
  24. expect( spy.calledOnce ).to.be.true;
  25. const data = spy.args[ 0 ][ 1 ];
  26. expect( data.domTarget ).to.equal( document.body );
  27. } );
  28. it( 'should fire blur with the right event data', () => {
  29. const spy = sinon.spy();
  30. viewDocument.on( 'blur', spy );
  31. observer.onDomEvent( { type: 'blur', target: document.body } );
  32. expect( spy.calledOnce ).to.be.true;
  33. const data = spy.args[ 0 ][ 1 ];
  34. expect( data.domTarget ).to.equal( document.body );
  35. } );
  36. } );
  37. describe( 'handle isFocused property of the document', () => {
  38. let domMain, domHeader, viewMain, viewHeader;
  39. beforeEach( () => {
  40. domMain = document.createElement( 'div' );
  41. domHeader = document.createElement( 'h1' );
  42. viewMain = viewDocument.createRoot( domMain );
  43. viewHeader = viewDocument.createRoot( domHeader, 'header' );
  44. } );
  45. it( 'should set isFocused to true on focus', () => {
  46. observer.onDomEvent( { type: 'focus', target: domMain } );
  47. expect( viewDocument.isFocused ).to.equal( true );
  48. } );
  49. it( 'should set isFocused to false on blur', () => {
  50. observer.onDomEvent( { type: 'focus', target: domMain } );
  51. expect( viewDocument.isFocused ).to.equal( true );
  52. observer.onDomEvent( { type: 'blur', target: domMain } );
  53. expect( viewDocument.isFocused ).to.be.false;
  54. } );
  55. it( 'should set isFocused to false on blur when selection in same editable', () => {
  56. viewDocument.selection.addRange( ViewRange.createFromParentsAndOffsets( viewMain, 0, viewMain, 0 ) );
  57. observer.onDomEvent( { type: 'focus', target: domMain } );
  58. expect( viewDocument.isFocused ).to.equal( true );
  59. observer.onDomEvent( { type: 'blur', target: domMain } );
  60. expect( viewDocument.isFocused ).to.be.false;
  61. } );
  62. it( 'should not set isFocused to false on blur when it is fired on other editable', () => {
  63. viewDocument.selection.addRange( ViewRange.createFromParentsAndOffsets( viewMain, 0, viewMain, 0 ) );
  64. observer.onDomEvent( { type: 'focus', target: domMain } );
  65. expect( viewDocument.isFocused ).to.equal( true );
  66. observer.onDomEvent( { type: 'blur', target: domHeader } );
  67. expect( viewDocument.isFocused ).to.be.true;
  68. } );
  69. } );
  70. } );