| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069 |
- /**
- * @license Copyright (c) 2003-2019, CKSource - Frederico Knabben. All rights reserved.
- * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
- */
- /* globals setTimeout, window, console, document */
- import Watchdog from '../src/watchdog';
- import Editor from '@ckeditor/ckeditor5-core/src/editor/editor';
- import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
- import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
- import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
- import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
- import HtmlDataProcessor from '@ckeditor/ckeditor5-engine/src/dataprocessor/htmldataprocessor';
- describe( 'Watchdog', () => {
- let element;
- beforeEach( () => {
- element = document.createElement( 'div' );
- document.body.appendChild( element );
- } );
- afterEach( () => {
- element.remove();
- sinon.restore();
- } );
- describe( 'create()', () => {
- it( 'should create an editor instance', () => {
- const watchdog = new Watchdog();
- const editorCreateSpy = sinon.spy( ClassicTestEditor, 'create' );
- const editorDestroySpy = sinon.spy( ClassicTestEditor.prototype, 'destroy' );
- watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
- watchdog.setDestructor( editor => editor.destroy() );
- return watchdog.create( element, {} )
- .then( () => {
- sinon.assert.calledOnce( editorCreateSpy );
- sinon.assert.notCalled( editorDestroySpy );
- return watchdog.destroy();
- } )
- .then( () => {
- sinon.assert.calledOnce( editorCreateSpy );
- sinon.assert.calledOnce( editorDestroySpy );
- } );
- } );
- it( 'should throw an error when the creator is not defined', () => {
- const watchdog = new Watchdog();
- watchdog.setDestructor( editor => editor.destroy() );
- expectToThrowCKEditorError(
- () => watchdog.create(),
- /^watchdog-creator-not-defined/,
- null
- );
- } );
- it( 'should throw an error when the destructor is not defined', () => {
- const watchdog = new Watchdog();
- watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
- expectToThrowCKEditorError(
- () => watchdog.create(),
- /^watchdog-destructor-not-defined/,
- null
- );
- } );
- it( 'should properly copy the config', () => {
- const watchdog = new Watchdog();
- watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
- watchdog.setDestructor( editor => editor.destroy() );
- const config = {
- foo: [],
- bar: document.createElement( 'div' )
- };
- return watchdog.create( element, config ).then( () => {
- expect( watchdog.editor.config._config.foo ).to.not.equal( config.foo );
- expect( watchdog.editor.config._config.bar ).to.equal( config.bar );
- } );
- } );
- it( 'should support editor data passed as the first argument', () => {
- const watchdog = new Watchdog();
- watchdog.setCreator( ( data, config ) => ClassicTestEditor.create( data, config ) );
- watchdog.setDestructor( editor => editor.destroy() );
- // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
- const originalErrorHandler = window.onerror;
- const windowErrorSpy = sinon.spy();
- window.onerror = windowErrorSpy;
- return watchdog.create( '<p>foo</p>', { plugins: [ Paragraph ] } )
- .then( () => {
- expect( watchdog.editor.getData() ).to.equal( '<p>foo</p>' );
- return new Promise( res => {
- setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
- watchdog.on( 'restart', () => {
- window.onerror = originalErrorHandler;
- res();
- } );
- } );
- } )
- .then( () => {
- expect( watchdog.editor.getData() ).to.equal( '<p>foo</p>' );
- return watchdog.destroy();
- } );
- } );
- } );
- describe( 'editor', () => {
- it( 'should be the current editor instance', () => {
- const watchdog = Watchdog.for( ClassicTestEditor );
- // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
- const originalErrorHandler = window.onerror;
- const windowErrorSpy = sinon.spy();
- window.onerror = windowErrorSpy;
- expect( watchdog.editor ).to.be.null;
- let oldEditor;
- return watchdog.create( element, {} )
- .then( () => {
- oldEditor = watchdog.editor;
- expect( watchdog.editor ).to.be.instanceOf( ClassicTestEditor );
- return new Promise( res => {
- setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
- watchdog.on( 'restart', () => {
- window.onerror = originalErrorHandler;
- res();
- } );
- } );
- } )
- .then( () => {
- expect( watchdog.editor ).to.be.instanceOf( ClassicTestEditor );
- expect( watchdog.editor ).to.not.equal( oldEditor );
- return watchdog.destroy();
- } )
- .then( () => {
- expect( watchdog.editor ).to.be.null;
- } );
- } );
- } );
- describe( 'error handling', () => {
- it( 'Watchdog should not restart editor during the initialization', () => {
- const watchdog = new Watchdog();
- watchdog.setCreator( el =>
- ClassicTestEditor.create( el )
- .then( () => Promise.reject( new Error( 'foo' ) ) )
- );
- watchdog.setDestructor( editor => editor.destroy() );
- return watchdog.create( element ).then(
- () => { throw new Error( '`watchdog.create()` should throw an error.' ); },
- err => {
- expect( err ).to.be.instanceOf( Error );
- expect( err.message ).to.equal( 'foo' );
- }
- );
- } );
- it( 'Watchdog should not restart editor during the destroy', () => {
- const watchdog = new Watchdog();
- watchdog.setCreator( el => ClassicTestEditor.create( el ) );
- watchdog.setDestructor( () => Promise.reject( new Error( 'foo' ) ) );
- return Promise.resolve()
- .then( () => watchdog.create( element ) )
- .then( () => watchdog.destroy() )
- .then(
- () => { throw new Error( '`watchdog.create()` should throw an error.' ); },
- err => {
- expect( err ).to.be.instanceOf( Error );
- expect( err.message ).to.equal( 'foo' );
- }
- );
- } );
- it( 'Watchdog should not hide intercepted errors', () => {
- const watchdog = new Watchdog();
- watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
- watchdog.setDestructor( editor => editor.destroy() );
- // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
- const originalErrorHandler = window.onerror;
- const windowErrorSpy = sinon.spy();
- window.onerror = windowErrorSpy;
- return watchdog.create( element ).then( () => {
- setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
- return new Promise( res => {
- watchdog.on( 'restart', () => {
- window.onerror = originalErrorHandler;
- sinon.assert.calledOnce( windowErrorSpy );
- // Various browsers will display the error slightly differently.
- expect( windowErrorSpy.getCall( 0 ).args[ 0 ] ).to.match( /foo/ );
- watchdog.destroy().then( res );
- } );
- } );
- } );
- } );
- it( 'Watchdog should intercept editor errors and restart the editor during the runtime', () => {
- const watchdog = new Watchdog();
- watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
- watchdog.setDestructor( editor => editor.destroy() );
- // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
- const originalErrorHandler = window.onerror;
- window.onerror = undefined;
- return watchdog.create( element ).then( () => {
- setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
- return new Promise( res => {
- watchdog.on( 'restart', () => {
- window.onerror = originalErrorHandler;
- watchdog.destroy().then( res );
- } );
- } );
- } );
- } );
- it( 'Watchdog should not intercept non-editor errors', () => {
- const watchdog = new Watchdog();
- watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
- watchdog.setDestructor( editor => editor.destroy() );
- const editorErrorSpy = sinon.spy();
- watchdog.on( 'error', editorErrorSpy );
- // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
- const originalErrorHandler = window.onerror;
- window.onerror = undefined;
- return watchdog.create( element ).then( () => {
- setTimeout( () => {
- throw new Error( 'foo' );
- } );
- setTimeout( () => {
- throw 'bar';
- } );
- return new Promise( res => {
- setTimeout( () => {
- window.onerror = originalErrorHandler;
- sinon.assert.notCalled( editorErrorSpy );
- watchdog.destroy().then( res );
- } );
- } );
- } );
- } );
- it( 'Watchdog should not intercept other editor errors', () => {
- const watchdog1 = Watchdog.for( ClassicTestEditor );
- const watchdog2 = Watchdog.for( ClassicTestEditor );
- const config = {
- plugins: []
- };
- // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
- const originalErrorHandler = window.onerror;
- window.onerror = undefined;
- return Promise.all( [
- watchdog1.create( element, config ),
- watchdog2.create( element, config )
- ] ).then( () => {
- return new Promise( res => {
- const watchdog1ErrorSpy = sinon.spy();
- const watchdog2ErrorSpy = sinon.spy();
- watchdog1.on( 'restart', watchdog1ErrorSpy );
- watchdog2.on( 'restart', watchdog2ErrorSpy );
- setTimeout( () => throwCKEditorError( 'foo', watchdog2.editor ) );
- setTimeout( () => {
- window.onerror = originalErrorHandler;
- sinon.assert.notCalled( watchdog1ErrorSpy );
- sinon.assert.calledOnce( watchdog2ErrorSpy );
- Promise.all( [ watchdog1.destroy(), watchdog2.destroy() ] )
- .then( res );
- } );
- } );
- } );
- } );
- it( 'Watchdog should intercept editor errors and restart the editor if the editor can be found from the context', () => {
- const watchdog = new Watchdog();
- watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
- watchdog.setDestructor( editor => editor.destroy() );
- // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
- const originalErrorHandler = window.onerror;
- window.onerror = undefined;
- return watchdog.create( element ).then( () => {
- setTimeout( () => throwCKEditorError( 'foo', watchdog.editor.model.document ) );
- return new Promise( res => {
- watchdog.on( 'restart', () => {
- window.onerror = originalErrorHandler;
- watchdog.destroy().then( res );
- } );
- } );
- } );
- } );
- it( 'Watchdog should intercept editor errors and restart the editor if the editor can be found from the context #2', () => {
- const watchdog = new Watchdog();
- watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
- watchdog.setDestructor( editor => editor.destroy() );
- // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
- const originalErrorHandler = window.onerror;
- window.onerror = undefined;
- return watchdog.create( element ).then( () => {
- setTimeout( () => throwCKEditorError( 'foo', {
- foo: [ 1, 2, 3, {
- bar: new Set( [
- new Map( [
- [ 'foo', 'bar' ],
- [ 0, watchdog.editor ]
- ] )
- ] )
- } ]
- } ) );
- return new Promise( res => {
- watchdog.on( 'restart', () => {
- window.onerror = originalErrorHandler;
- watchdog.destroy().then( res );
- } );
- } );
- } );
- } );
- it( 'Watchdog should crash permanently if the `crashNumberLimit` is reached' +
- ' and the average time between errors is lower than `minimumNonErrorTimePeriod` (default values)', () => {
- const watchdog = new Watchdog();
- watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
- watchdog.setDestructor( editor => editor.destroy() );
- const errorSpy = sinon.spy();
- watchdog.on( 'error', errorSpy );
- const restartSpy = sinon.spy();
- watchdog.on( 'restart', restartSpy );
- // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
- const originalErrorHandler = window.onerror;
- window.onerror = undefined;
- return watchdog.create( element ).then( () => {
- setTimeout( () => throwCKEditorError( 'foo1', watchdog.editor ) );
- setTimeout( () => throwCKEditorError( 'foo2', watchdog.editor ) );
- setTimeout( () => throwCKEditorError( 'foo3', watchdog.editor ) );
- setTimeout( () => throwCKEditorError( 'foo4', watchdog.editor ) );
- return new Promise( res => {
- setTimeout( () => {
- expect( errorSpy.callCount ).to.equal( 4 );
- expect( watchdog.crashes.length ).to.equal( 4 );
- expect( restartSpy.callCount ).to.equal( 3 );
- window.onerror = originalErrorHandler;
- watchdog.destroy().then( res );
- } );
- } );
- } );
- } );
- it( 'Watchdog should crash permanently if the `crashNumberLimit` is reached' +
- ' and the average time between errors is lower than `minimumNonErrorTimePeriod` (custom values)', () => {
- const watchdog = new Watchdog( { crashNumberLimit: 2, minimumNonErrorTimePeriod: 1000 } );
- watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
- watchdog.setDestructor( editor => editor.destroy() );
- const errorSpy = sinon.spy();
- watchdog.on( 'error', errorSpy );
- const restartSpy = sinon.spy();
- watchdog.on( 'restart', restartSpy );
- // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
- const originalErrorHandler = window.onerror;
- window.onerror = undefined;
- return watchdog.create( element ).then( () => {
- setTimeout( () => throwCKEditorError( 'foo1', watchdog.editor ) );
- setTimeout( () => throwCKEditorError( 'foo2', watchdog.editor ) );
- setTimeout( () => throwCKEditorError( 'foo3', watchdog.editor ) );
- setTimeout( () => throwCKEditorError( 'foo4', watchdog.editor ) );
- return new Promise( res => {
- setTimeout( () => {
- expect( errorSpy.callCount ).to.equal( 3 );
- expect( watchdog.crashes.length ).to.equal( 3 );
- expect( restartSpy.callCount ).to.equal( 2 );
- window.onerror = originalErrorHandler;
- watchdog.destroy().then( res );
- } );
- } );
- } );
- } );
- it( 'Watchdog should not crash permantently when average time between errors is longer than `minimumNonErrorTimePeriod`', () => {
- const watchdog = new Watchdog( { crashNumberLimit: 2, minimumNonErrorTimePeriod: 0 } );
- watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
- watchdog.setDestructor( editor => editor.destroy() );
- const errorSpy = sinon.spy();
- watchdog.on( 'error', errorSpy );
- const restartSpy = sinon.spy();
- watchdog.on( 'restart', restartSpy );
- // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
- const originalErrorHandler = window.onerror;
- window.onerror = undefined;
- return watchdog.create( element ).then( () => {
- setTimeout( () => throwCKEditorError( 'foo1', watchdog.editor ), 5 );
- setTimeout( () => throwCKEditorError( 'foo2', watchdog.editor ), 10 );
- setTimeout( () => throwCKEditorError( 'foo3', watchdog.editor ), 15 );
- setTimeout( () => throwCKEditorError( 'foo4', watchdog.editor ), 20 );
- return new Promise( res => {
- setTimeout( () => {
- expect( errorSpy.callCount ).to.equal( 4 );
- expect( watchdog.crashes.length ).to.equal( 4 );
- expect( restartSpy.callCount ).to.equal( 4 );
- window.onerror = originalErrorHandler;
- watchdog.destroy().then( res );
- }, 20 );
- } );
- } );
- } );
- it( 'Watchdog should warn if the CKEditorError missing its context', () => {
- const watchdog = new Watchdog();
- watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
- watchdog.setDestructor( editor => editor.destroy() );
- // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
- const originalErrorHandler = window.onerror;
- window.onerror = undefined;
- sinon.stub( console, 'error' );
- return watchdog.create( element ).then( () => {
- setTimeout( () => throwCKEditorError( 'foo' ) );
- return new Promise( res => {
- setTimeout( () => {
- window.onerror = originalErrorHandler;
- expect( watchdog.crashes ).to.deep.equal( [] );
- sinon.assert.calledWithExactly(
- console.error,
- 'The error is missing its context and Watchdog cannot restart the proper editor.'
- );
- watchdog.destroy().then( res );
- } );
- } );
- } );
- } );
- it( 'Watchdog should omit error if the CKEditorError context is equal to null', () => {
- const watchdog = new Watchdog();
- watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
- watchdog.setDestructor( editor => editor.destroy() );
- // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
- const originalErrorHandler = window.onerror;
- window.onerror = undefined;
- sinon.stub( console, 'error' );
- return watchdog.create( element ).then( () => {
- setTimeout( () => throwCKEditorError( 'foo', null ) );
- return new Promise( res => {
- setTimeout( () => {
- window.onerror = originalErrorHandler;
- expect( watchdog.crashes ).to.deep.equal( [] );
- sinon.assert.notCalled( console.error );
- watchdog.destroy().then( res );
- } );
- } );
- } );
- } );
- it( 'editor should be restarted with the data before the crash #1', () => {
- const watchdog = new Watchdog();
- watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
- watchdog.setDestructor( editor => editor.destroy() );
- // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
- const originalErrorHandler = window.onerror;
- window.onerror = undefined;
- return watchdog.create( element, {
- initialData: '<p>foo</p>',
- plugins: [ Paragraph ]
- } ).then( () => {
- setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
- return new Promise( res => {
- watchdog.on( 'restart', () => {
- window.onerror = originalErrorHandler;
- expect( watchdog.editor.getData() ).to.equal( '<p>foo</p>' );
- watchdog.destroy().then( res );
- } );
- } );
- } );
- } );
- it( 'editor should be restarted with the data before the crash #2', () => {
- const watchdog = new Watchdog();
- watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
- watchdog.setDestructor( editor => editor.destroy() );
- // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
- const originalErrorHandler = window.onerror;
- window.onerror = undefined;
- return watchdog.create( element, {
- initialData: '<p>foo</p>',
- plugins: [ Paragraph ]
- } ).then( () => {
- const doc = watchdog.editor.model.document;
- watchdog.editor.model.change( writer => {
- writer.insertText( 'bar', writer.createPositionAt( doc.getRoot(), 1 ) );
- } );
- setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
- return new Promise( res => {
- watchdog.on( 'restart', () => {
- window.onerror = originalErrorHandler;
- expect( watchdog.editor.getData() ).to.equal( '<p>foo</p><p>bar</p>' );
- watchdog.destroy().then( res );
- } );
- } );
- } );
- } );
- it( 'editor should be restarted with the data of the latest document version before the crash', () => {
- const watchdog = new Watchdog();
- watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
- watchdog.setDestructor( editor => editor.destroy() );
- // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
- const originalErrorHandler = window.onerror;
- window.onerror = undefined;
- return watchdog.create( element, {
- initialData: '<p>foo</p>',
- plugins: [ Paragraph ]
- } ).then( () => {
- const model = watchdog.editor.model;
- const doc = model.document;
- // Decrement the document version to simulate a situation when an operation
- // don't produce new document version.
- doc.version--;
- model.change( writer => {
- writer.insertText( 'bar', writer.createPositionAt( doc.getRoot(), 1 ) );
- } );
- setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
- return new Promise( res => {
- watchdog.on( 'restart', () => {
- window.onerror = originalErrorHandler;
- expect( watchdog.editor.getData() ).to.equal( '<p>foo</p>' );
- watchdog.destroy().then( res );
- } );
- } );
- } );
- } );
- it( 'editor should be restarted with the latest available data before the crash', () => {
- const watchdog = new Watchdog();
- watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
- watchdog.setDestructor( editor => editor.destroy() );
- // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
- const originalErrorHandler = window.onerror;
- window.onerror = undefined;
- sinon.stub( console, 'error' );
- return watchdog.create( element, {
- initialData: '<p>foo</p>',
- plugins: [ Paragraph ]
- } ).then( () => {
- const editorGetDataError = new Error( 'Some error' );
- const getDataStub = sinon.stub( watchdog.editor.data, 'get' )
- .throwsException( editorGetDataError );
- setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
- return new Promise( res => {
- const doc = watchdog.editor.model.document;
- watchdog.editor.model.change( writer => {
- writer.insertText( 'bar', writer.createPositionAt( doc.getRoot(), 1 ) );
- } );
- watchdog.on( 'restart', () => {
- window.onerror = originalErrorHandler;
- // It is called second time by during the default editor destruction
- // to update the source element.
- sinon.assert.calledTwice( getDataStub );
- expect( watchdog.editor.getData() ).to.equal( '<p>foo</p>' );
- sinon.assert.calledWith(
- console.error,
- editorGetDataError,
- 'An error happened during restoring editor data. Editor will be restored from the previously saved data.'
- );
- sinon.assert.calledWith(
- console.error,
- 'An error happened during the editor destructing.'
- );
- watchdog.destroy().then( res );
- } );
- } );
- } );
- } );
- } );
- describe( 'async error handling', () => {
- let unhandledRejectionEventSupported;
- before( () => {
- return isUnhandledRejectionEventSupported()
- .then( val => {
- unhandledRejectionEventSupported = val;
- } );
- } );
- it( 'Watchdog should handle async CKEditorError errors', () => {
- if ( !unhandledRejectionEventSupported ) {
- return;
- }
- const watchdog = Watchdog.for( ClassicTestEditor );
- const originalErrorHandler = window.onerror;
- window.onerror = undefined;
- return watchdog.create( element, {
- initialData: '<p>foo</p>',
- plugins: [ Paragraph ]
- } ).then( () => {
- const oldEditor = watchdog.editor;
- Promise.resolve().then( () => throwCKEditorError( 'foo', watchdog.editor ) );
- return new Promise( res => {
- watchdog.on( 'restart', () => {
- window.onerror = originalErrorHandler;
- expect( watchdog.editor ).to.not.equal( oldEditor );
- expect( watchdog.editor.getData() ).to.equal( '<p>foo</p>' );
- watchdog.destroy().then( res );
- } );
- } );
- } );
- } );
- it( 'Watchdog should not react to non-editor async errors', () => {
- if ( !unhandledRejectionEventSupported ) {
- return;
- }
- const watchdog = Watchdog.for( ClassicTestEditor );
- const originalErrorHandler = window.onerror;
- const editorErrorSpy = sinon.spy();
- window.onerror = undefined;
- return watchdog.create( element, {
- initialData: '<p>foo</p>',
- plugins: [ Paragraph ]
- } ).then( () => {
- watchdog.on( 'error', editorErrorSpy );
- Promise.resolve().then( () => Promise.reject( 'foo' ) );
- Promise.resolve().then( () => Promise.reject( new Error( 'bar' ) ) );
- // Wait a cycle.
- return new Promise( res => setTimeout( res ) );
- } ).then( () => {
- window.onerror = originalErrorHandler;
- sinon.assert.notCalled( editorErrorSpy );
- expect( watchdog.editor.getData() ).to.equal( '<p>foo</p>' );
- return watchdog.destroy();
- } );
- } );
- } );
- describe( 'destroy()', () => {
- // See #19.
- it( 'should clean internal stuff', () => {
- // 30ms should be enough to make the two data changes split into two data save actions.
- // This will ensure that the second data save action will be put off in time.
- const SAVE_INTERVAL = 30;
- const watchdog = Watchdog.for( ClassicTestEditor, {
- saveInterval: SAVE_INTERVAL,
- } );
- return watchdog.create( element, {
- initialData: '<p>foo</p>',
- plugins: [ Paragraph ]
- } ).then( () => {
- const doc = watchdog.editor.model.document;
- watchdog.editor.model.change( writer => {
- writer.insertText( 'bar', writer.createPositionAt( doc.getRoot(), 1 ) );
- } );
- watchdog.editor.model.change( writer => {
- writer.insertText( 'foo', writer.createPositionAt( doc.getRoot(), 1 ) );
- } );
- return watchdog.destroy();
- } ).then( () => {
- // Wait to ensure that the throttled save is cleared and won't be executed
- // on the non-existing editor.
- return new Promise( res => setTimeout( res, SAVE_INTERVAL ) );
- } ).then( () => {
- expect( watchdog.editor ).to.equal( null );
- expect( watchdog.state ).to.equal( 'destroyed' );
- expect( watchdog.crashes ).to.deep.equal( [] );
- } );
- } );
- } );
- describe( 'static for()', () => {
- it( 'should be a shortcut method for creating the watchdog', () => {
- const watchdog = Watchdog.for( ClassicTestEditor );
- // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
- const originalErrorHandler = window.onerror;
- window.onerror = undefined;
- return watchdog.create( element, {
- initialData: '<p>foo</p>',
- plugins: [ Paragraph ]
- } ).then( () => {
- const oldEditor = watchdog.editor;
- expect( watchdog.editor ).to.be.an.instanceOf( ClassicTestEditor );
- setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
- return new Promise( res => {
- watchdog.on( 'restart', () => {
- window.onerror = originalErrorHandler;
- expect( watchdog.editor ).to.be.an.instanceOf( ClassicTestEditor );
- expect( watchdog.editor ).to.not.equal( oldEditor );
- expect( watchdog.editor.getData() ).to.equal( '<p>foo</p>' );
- watchdog.destroy().then( res );
- } );
- } );
- } );
- } );
- } );
- describe( 'crashes', () => {
- it( 'should be an array of caught errors by the watchdog', () => {
- const watchdog = Watchdog.for( ClassicTestEditor );
- // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
- const originalErrorHandler = window.onerror;
- window.onerror = undefined;
- return watchdog.create( element ).then( () => {
- setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
- setTimeout( () => throwCKEditorError( 'bar', watchdog.editor ) );
- return new Promise( res => {
- setTimeout( () => {
- window.onerror = originalErrorHandler;
- expect( watchdog.crashes[ 0 ].message ).to.equal( 'foo' );
- expect( watchdog.crashes[ 0 ].stack ).to.be.a( 'string' );
- expect( watchdog.crashes[ 0 ].date ).to.be.a( 'number' );
- expect( watchdog.crashes[ 0 ].filename ).to.be.a( 'string' );
- expect( watchdog.crashes[ 0 ].lineno ).to.be.a( 'number' );
- expect( watchdog.crashes[ 0 ].colno ).to.be.a( 'number' );
- expect( watchdog.crashes[ 1 ].message ).to.equal( 'bar' );
- watchdog.destroy().then( res );
- } );
- } );
- } );
- } );
- it( 'should include async errors', () => {
- return isUnhandledRejectionEventSupported().then( isSupported => {
- if ( !isSupported ) {
- return;
- }
- const watchdog = Watchdog.for( ClassicTestEditor );
- // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
- const originalErrorHandler = window.onerror;
- window.onerror = undefined;
- return watchdog.create( element ).then( () => {
- Promise.resolve().then( () => throwCKEditorError( 'foo', watchdog.editor ) );
- return new Promise( res => {
- setTimeout( () => {
- window.onerror = originalErrorHandler;
- expect( watchdog.crashes[ 0 ].message ).to.equal( 'foo' );
- expect( watchdog.crashes[ 0 ].stack ).to.be.a( 'string' );
- expect( watchdog.crashes[ 0 ].date ).to.be.a( 'number' );
- expect( watchdog.crashes[ 0 ].filename ).to.be.an( 'undefined' );
- expect( watchdog.crashes[ 0 ].lineno ).to.be.an( 'undefined' );
- expect( watchdog.crashes[ 0 ].colno ).to.be.an( 'undefined' );
- watchdog.destroy().then( res );
- } );
- } );
- } );
- } );
- } );
- } );
- describe( 'state', () => {
- it( 'should reflect the state of the watchdog', () => {
- const watchdog = Watchdog.for( ClassicTestEditor );
- // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
- const originalErrorHandler = window.onerror;
- window.onerror = undefined;
- expect( watchdog.state ).to.equal( 'initializing' );
- return watchdog.create( element ).then( () => {
- expect( watchdog.state ).to.equal( 'ready' );
- return watchdog.create( element ).then( () => {
- setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
- setTimeout( () => throwCKEditorError( 'bar', watchdog.editor ) );
- return new Promise( res => {
- setTimeout( () => {
- window.onerror = originalErrorHandler;
- expect( watchdog.state ).to.equal( 'ready' );
- watchdog.destroy().then( () => {
- expect( watchdog.state ).to.equal( 'destroyed' );
- res();
- } );
- } );
- } );
- } );
- } );
- } );
- it( 'should be observable', () => {
- const watchdog = Watchdog.for( ClassicTestEditor );
- const states = [];
- watchdog.on( 'change:state', ( evt, propName, newValue ) => {
- states.push( newValue );
- } );
- // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
- const originalErrorHandler = window.onerror;
- window.onerror = undefined;
- return watchdog.create( element ).then( () => {
- return watchdog.create( element ).then( () => {
- setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
- setTimeout( () => throwCKEditorError( 'bar', watchdog.editor ) );
- setTimeout( () => throwCKEditorError( 'baz', watchdog.editor ) );
- setTimeout( () => throwCKEditorError( 'biz', watchdog.editor ) );
- return new Promise( res => {
- setTimeout( () => {
- window.onerror = originalErrorHandler;
- watchdog.destroy().then( () => {
- expect( states ).to.deep.equal( [
- 'ready',
- 'crashed',
- 'initializing',
- 'ready',
- 'crashed',
- 'initializing',
- 'ready',
- 'crashed',
- 'initializing',
- 'ready',
- 'crashed',
- 'crashedPermanently',
- 'destroyed'
- ] );
- res();
- } );
- } );
- } );
- } );
- } );
- } );
- } );
- it( 'should support multi-root editors', () => {
- class MultiRootEditor extends Editor {
- constructor( sourceElements, config ) {
- super( config );
- this.data.processor = new HtmlDataProcessor();
- // Create root and UIView element for each editable container.
- for ( const rootName of Object.keys( sourceElements ) ) {
- this.model.document.createRoot( '$root', rootName );
- }
- }
- static async create( sourceElements, config ) {
- const editor = new this( sourceElements, config );
- await editor.initPlugins();
- await editor.data.init( config.initialData );
- editor.fire( 'ready' );
- return editor;
- }
- }
- const watchdog = Watchdog.for( MultiRootEditor );
- // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
- const originalErrorHandler = window.onerror;
- window.onerror = undefined;
- return watchdog
- .create( {
- header: element
- }, {
- initialData: {
- header: '<p>Foo</p>'
- },
- plugins: [ Paragraph ]
- } )
- .then( () => {
- expect( watchdog.editor.data.get( { rootName: 'header' } ) ).to.equal( '<p>Foo</p>' );
- setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
- return new Promise( res => {
- window.onerror = originalErrorHandler;
- expect( watchdog.editor.data.get( { rootName: 'header' } ) ).to.equal( '<p>Foo</p>' );
- res();
- } );
- } );
- } );
- } );
- function throwCKEditorError( name, context ) {
- throw new CKEditorError( name, context );
- }
- // Feature detection works as a race condition - if the `unhandledrejection` event
- // is supported then the listener should be called first. Otherwise the timeout will be reached.
- function isUnhandledRejectionEventSupported() {
- return new Promise( res => {
- window.addEventListener( 'unhandledrejection', function listener() {
- res( true );
- window.removeEventListener( 'unhandledrejection', listener );
- } );
- Promise.resolve().then( () => Promise.reject( new Error() ) );
- setTimeout( () => res( false ) );
- } );
- }
|