| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665 |
- /**
- * @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 VirtualTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/virtualtesteditor';
- import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
- import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
- describe( 'Watchdog', () => {
- afterEach( () => sinon.restore() );
- describe( 'create()', () => {
- it( 'should create an editor instance', () => {
- const watchdog = new Watchdog();
- const editorCreateSpy = sinon.spy( FakeEditor, 'create' );
- const editorDestroySpy = sinon.spy( FakeEditor.prototype, 'destroy' );
- watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
- watchdog.setDestructor( editor => editor.destroy() );
- return watchdog.create( document.createElement( 'div' ), {} )
- .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() );
- expect( () => watchdog.create() ).to.throw( CKEditorError, /^watchdog-creator-not-defined/ );
- } );
- it( 'should throw an error when the destructor is not defined', () => {
- const watchdog = new Watchdog();
- watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
- expect( () => watchdog.create() ).to.throw( CKEditorError, /^watchdog-destructor-not-defined/ );
- } );
- } );
- describe( 'restart()', () => {
- it( 'should restart the editor', () => {
- const watchdog = new Watchdog();
- const editorCreateSpy = sinon.spy( FakeEditor, 'create' );
- const editorDestroySpy = sinon.spy( FakeEditor.prototype, 'destroy' );
- watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
- watchdog.setDestructor( editor => editor.destroy() );
- return watchdog.create( document.createElement( 'div' ), {} )
- .then( () => {
- sinon.assert.calledOnce( editorCreateSpy );
- sinon.assert.notCalled( editorDestroySpy );
- return watchdog.restart();
- } )
- .then( () => {
- sinon.assert.calledTwice( editorCreateSpy );
- sinon.assert.calledOnce( editorDestroySpy );
- return watchdog.destroy();
- } );
- } );
- it( 'should restart the editor with the same data', () => {
- const watchdog = new Watchdog();
- watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
- watchdog.setDestructor( editor => editor.destroy() );
- return watchdog.create( document.createElement( 'div' ), {
- initialData: '<p>foo</p>',
- plugins: [ Paragraph ]
- } )
- .then( () => {
- expect( watchdog.editor.getData() ).to.equal( '<p>foo</p>' );
- return watchdog.restart();
- } )
- .then( () => {
- expect( watchdog.editor.getData() ).to.equal( '<p>foo</p>' );
- return watchdog.destroy();
- } );
- } );
- } );
- describe( 'editor', () => {
- it( 'should be the current editor instance', () => {
- const watchdog = new Watchdog();
- watchdog.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
- watchdog.setDestructor( editor => editor.destroy() );
- expect( watchdog.editor ).to.be.null;
- let oldEditor;
- return watchdog.create( document.createElement( 'div' ), {} )
- .then( () => {
- oldEditor = watchdog.editor;
- expect( watchdog.editor ).to.be.instanceOf( FakeEditor );
- return watchdog.restart();
- } )
- .then( () => {
- expect( watchdog.editor ).to.be.instanceOf( FakeEditor );
- 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 =>
- FakeEditor.create( el )
- .then( () => Promise.reject( new Error( 'foo' ) ) )
- );
- watchdog.setDestructor( editor => editor.destroy() );
- return watchdog.create( document.createElement( 'div' ) ).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 => FakeEditor.create( el ) );
- watchdog.setDestructor( () => Promise.reject( new Error( 'foo' ) ) );
- return Promise.resolve()
- .then( () => watchdog.create( document.createElement( 'div' ) ) )
- .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 ) => FakeEditor.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( document.createElement( 'div' ) ).then( () => {
- setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
- return new Promise( res => {
- watchdog.on( 'restart', () => {
- window.onerror = originalErrorHandler;
- sinon.assert.calledOnce( windowErrorSpy );
- expect( windowErrorSpy.getCall( 0 ).args[ 0 ] ).to.equal( 'Uncaught CKEditorError: 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 ) => FakeEditor.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( document.createElement( 'div' ) ).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 ) => FakeEditor.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( document.createElement( 'div' ) ).then( () => {
- setTimeout( () => {
- throw new Error( 'foo' );
- } );
- 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 = new Watchdog();
- const watchdog2 = new Watchdog();
- watchdog1.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
- watchdog1.setDestructor( editor => editor.destroy() );
- watchdog2.setCreator( ( el, config ) => FakeEditor.create( el, config ) );
- watchdog2.setDestructor( editor => editor.destroy() );
- // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
- const originalErrorHandler = window.onerror;
- window.onerror = undefined;
- return Promise.all( [
- watchdog1.create( document.createElement( 'div' ) ),
- watchdog2.create( document.createElement( 'div' ) )
- ] ).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 ) => FakeEditor.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( document.createElement( 'div' ) ).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 ) => FakeEditor.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( document.createElement( 'div' ) ).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( 'editor should be restarted up to 3 times by default', () => {
- const watchdog = new Watchdog();
- watchdog.setCreator( ( el, config ) => FakeEditor.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( document.createElement( 'div' ) ).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( 'editor should be restarted up to `crashNumberLimit` times if the option is set', () => {
- const watchdog = new Watchdog( { crashNumberLimit: 2 } );
- watchdog.setCreator( ( el, config ) => FakeEditor.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( document.createElement( 'div' ) ).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( 2 );
- window.onerror = originalErrorHandler;
- watchdog.destroy().then( res );
- } );
- } );
- } );
- } );
- it( 'Watchdog should warn if the CKEditorError missing its context', () => {
- const watchdog = new Watchdog();
- watchdog.setCreator( ( el, config ) => FakeEditor.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( document.createElement( 'div' ) ).then( () => {
- setTimeout( () => throwCKEditorError( 'foo' ) );
- return new Promise( res => {
- setTimeout( () => {
- window.onerror = originalErrorHandler;
- expect( watchdog.crashes ).to.deep.equal( [] );
- sinon.assert.calledOnce( console.error );
- sinon.assert.calledWithExactly(
- console.error,
- 'The error is missing its context and Watchdog cannot restart the proper editor.'
- );
- watchdog.destroy().then( res );
- } );
- } );
- } );
- } );
- it( 'editor should be restarted with the data before the crash #1', () => {
- const watchdog = new Watchdog();
- watchdog.setCreator( ( el, config ) => FakeEditor.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( document.createElement( 'div' ), {
- 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 ) => FakeEditor.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( document.createElement( 'div' ), {
- 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 ) => FakeEditor.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( document.createElement( 'div' ), {
- initialData: '<p>foo</p>',
- plugins: [ Paragraph ]
- } ).then( () => {
- const doc = watchdog.editor.model.document;
- watchdog.editor.model.document.version = -1000;
- 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>' );
- 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 ) => FakeEditor.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( document.createElement( 'div' ), {
- initialData: '<p>foo</p>',
- plugins: [ Paragraph ]
- } ).then( () => {
- const editorGetDataError = new Error( 'Some error' );
- const getDataStub = sinon.stub( watchdog.editor, 'getData' )
- .throwsException( editorGetDataError );
- setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
- const doc = watchdog.editor.model.document;
- watchdog.editor.model.change( writer => {
- writer.insertText( 'bar', writer.createPositionAt( doc.getRoot(), 1 ) );
- } );
- return new Promise( res => {
- watchdog.on( 'restart', () => {
- window.onerror = originalErrorHandler;
- sinon.assert.calledOnce( 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.'
- );
- watchdog.destroy().then( res );
- } );
- } );
- } );
- } );
- } );
- describe( 'static for()', () => {
- it( 'should be a shortcut method for creating the watchdog', () => {
- const watchdog = Watchdog.for( FakeEditor );
- // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
- const originalErrorHandler = window.onerror;
- window.onerror = undefined;
- return watchdog.create( document.createElement( 'div' ), {
- initialData: '<p>foo</p>',
- plugins: [ Paragraph ]
- } ).then( () => {
- const oldEditor = watchdog.editor;
- expect( watchdog.editor ).to.be.an.instanceOf( FakeEditor );
- setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
- return new Promise( res => {
- watchdog.on( 'restart', () => {
- window.onerror = originalErrorHandler;
- expect( watchdog.editor ).to.be.an.instanceOf( FakeEditor );
- 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 = new Watchdog();
- watchdog.setCreator( ( el, config ) => FakeEditor.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( document.createElement( 'div' ) ).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[ 1 ].message ).to.equal( 'bar' );
- watchdog.destroy().then( res );
- } );
- } );
- } );
- } );
- } );
- } );
- // Wrap Editor to align the API to the "full" editors.
- class FakeEditor extends VirtualTestEditor {
- static create( _elementOrData, config ) {
- return super.create( config );
- }
- }
- function throwCKEditorError( name, context ) {
- throw new CKEditorError( name, context );
- }
|