watchdog.js 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125
  1. /**
  2. * @license Copyright (c) 2003-2020, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md or https://ckeditor.com/legal/ckeditor-oss-license
  4. */
  5. /* globals setTimeout, window, console, document */
  6. import Watchdog from '../src/watchdog';
  7. import Editor from '@ckeditor/ckeditor5-core/src/editor/editor';
  8. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  9. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  10. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  11. import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  12. import HtmlDataProcessor from '@ckeditor/ckeditor5-engine/src/dataprocessor/htmldataprocessor';
  13. describe( 'Watchdog', () => {
  14. let element;
  15. beforeEach( () => {
  16. element = document.createElement( 'div' );
  17. document.body.appendChild( element );
  18. } );
  19. afterEach( () => {
  20. element.remove();
  21. sinon.restore();
  22. } );
  23. describe( 'create()', () => {
  24. it( 'should create an editor instance', () => {
  25. const watchdog = new Watchdog();
  26. const editorCreateSpy = sinon.spy( ClassicTestEditor, 'create' );
  27. const editorDestroySpy = sinon.spy( ClassicTestEditor.prototype, 'destroy' );
  28. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  29. return watchdog.create( element, {} )
  30. .then( () => {
  31. sinon.assert.calledOnce( editorCreateSpy );
  32. sinon.assert.notCalled( editorDestroySpy );
  33. return watchdog.destroy();
  34. } )
  35. .then( () => {
  36. sinon.assert.calledOnce( editorCreateSpy );
  37. sinon.assert.calledOnce( editorDestroySpy );
  38. } );
  39. } );
  40. it( 'should throw an error when the creator is not defined', () => {
  41. const watchdog = new Watchdog();
  42. expectToThrowCKEditorError(
  43. () => watchdog.create(),
  44. /^watchdog-creator-not-defined/,
  45. null
  46. );
  47. } );
  48. it( 'should properly copy the config', () => {
  49. const watchdog = new Watchdog();
  50. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  51. const config = {
  52. foo: [],
  53. bar: document.createElement( 'div' )
  54. };
  55. return watchdog.create( element, config ).then( () => {
  56. expect( watchdog.editor.config._config.foo ).to.not.equal( config.foo );
  57. expect( watchdog.editor.config._config.bar ).to.equal( config.bar );
  58. return watchdog.destroy();
  59. } );
  60. } );
  61. it( 'should support editor data passed as the first argument', () => {
  62. const watchdog = new Watchdog();
  63. watchdog.setCreator( ( data, config ) => ClassicTestEditor.create( data, config ) );
  64. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  65. const originalErrorHandler = window.onerror;
  66. const windowErrorSpy = sinon.spy();
  67. window.onerror = windowErrorSpy;
  68. return watchdog.create( '<p>foo</p>', { plugins: [ Paragraph ] } )
  69. .then( () => {
  70. expect( watchdog.editor.getData() ).to.equal( '<p>foo</p>' );
  71. return new Promise( res => {
  72. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  73. watchdog.on( 'restart', () => {
  74. window.onerror = originalErrorHandler;
  75. res();
  76. } );
  77. } );
  78. } )
  79. .then( () => {
  80. expect( watchdog.editor.getData() ).to.equal( '<p>foo</p>' );
  81. return watchdog.destroy();
  82. } );
  83. } );
  84. } );
  85. describe( 'editor', () => {
  86. it( 'should be the current editor instance', () => {
  87. const watchdog = Watchdog.for( ClassicTestEditor );
  88. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  89. const originalErrorHandler = window.onerror;
  90. const windowErrorSpy = sinon.spy();
  91. window.onerror = windowErrorSpy;
  92. expect( watchdog.editor ).to.be.null;
  93. let oldEditor;
  94. return watchdog.create( element, {} )
  95. .then( () => {
  96. oldEditor = watchdog.editor;
  97. expect( watchdog.editor ).to.be.instanceOf( ClassicTestEditor );
  98. return new Promise( res => {
  99. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  100. watchdog.on( 'restart', () => {
  101. window.onerror = originalErrorHandler;
  102. res();
  103. } );
  104. } );
  105. } )
  106. .then( () => {
  107. expect( watchdog.editor ).to.be.instanceOf( ClassicTestEditor );
  108. expect( watchdog.editor ).to.not.equal( oldEditor );
  109. return watchdog.destroy();
  110. } )
  111. .then( () => {
  112. expect( watchdog.editor ).to.be.null;
  113. } );
  114. } );
  115. } );
  116. describe( 'error handling', () => {
  117. it( 'Watchdog should not restart editor during the initialization', () => {
  118. const watchdog = new Watchdog();
  119. watchdog.setCreator( el =>
  120. ClassicTestEditor.create( el )
  121. .then( () => Promise.reject( new Error( 'foo' ) ) )
  122. );
  123. return watchdog.create( element ).then(
  124. () => { throw new Error( '`watchdog.create()` should throw an error.' ); },
  125. err => {
  126. expect( err ).to.be.instanceOf( Error );
  127. expect( err.message ).to.equal( 'foo' );
  128. return destroyEditorOrphans();
  129. }
  130. );
  131. } );
  132. it( 'Watchdog should not restart editor during the destroy', () => {
  133. const watchdog = new Watchdog();
  134. watchdog.setCreator( el => ClassicTestEditor.create( el ) );
  135. watchdog.setDestructor( () => Promise.reject( new Error( 'foo' ) ) );
  136. return Promise.resolve()
  137. .then( () => watchdog.create( element ) )
  138. .then( () => watchdog.destroy() )
  139. .then(
  140. () => { throw new Error( '`watchdog.create()` should throw an error.' ); },
  141. err => {
  142. expect( err ).to.be.instanceOf( Error );
  143. expect( err.message ).to.equal( 'foo' );
  144. return destroyEditorOrphans();
  145. }
  146. );
  147. } );
  148. it( 'Watchdog should not hide intercepted errors', () => {
  149. const watchdog = new Watchdog();
  150. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  151. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  152. const originalErrorHandler = window.onerror;
  153. const windowErrorSpy = sinon.spy();
  154. window.onerror = windowErrorSpy;
  155. return watchdog.create( element ).then( () => {
  156. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  157. return new Promise( res => {
  158. watchdog.on( 'restart', () => {
  159. window.onerror = originalErrorHandler;
  160. sinon.assert.calledOnce( windowErrorSpy );
  161. // Various browsers will display the error slightly differently.
  162. expect( windowErrorSpy.getCall( 0 ).args[ 0 ] ).to.match( /foo/ );
  163. watchdog.destroy().then( res );
  164. } );
  165. } );
  166. } );
  167. } );
  168. it( 'Watchdog should intercept editor errors and restart the editor during the runtime', () => {
  169. const watchdog = new Watchdog();
  170. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  171. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  172. const originalErrorHandler = window.onerror;
  173. window.onerror = undefined;
  174. return watchdog.create( element ).then( () => {
  175. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  176. return new Promise( res => {
  177. watchdog.on( 'restart', () => {
  178. window.onerror = originalErrorHandler;
  179. watchdog.destroy().then( res );
  180. } );
  181. } );
  182. } );
  183. } );
  184. it( 'Watchdog should not intercept non-editor errors', () => {
  185. const watchdog = new Watchdog();
  186. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  187. const editorErrorSpy = sinon.spy();
  188. watchdog.on( 'error', editorErrorSpy );
  189. const watchdogErrorHandlerSpy = sinon.spy( watchdog, '_handleError' );
  190. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  191. const originalErrorHandler = window.onerror;
  192. window.onerror = undefined;
  193. return watchdog.create( element ).then( () => {
  194. const error = new Error( 'foo' );
  195. setTimeout( () => {
  196. throw error;
  197. } );
  198. setTimeout( () => {
  199. throw 'bar';
  200. } );
  201. setTimeout( () => {
  202. throw null;
  203. } );
  204. return new Promise( res => {
  205. setTimeout( () => {
  206. window.onerror = originalErrorHandler;
  207. sinon.assert.notCalled( editorErrorSpy );
  208. // Assert that only instances of the `Error` class will be checked deeper.
  209. sinon.assert.calledOnce( watchdogErrorHandlerSpy );
  210. expect( watchdogErrorHandlerSpy.getCall( 0 ).args[ 0 ] ).to.equal( error );
  211. watchdog.destroy().then( res );
  212. } );
  213. } );
  214. } );
  215. } );
  216. it( 'Watchdog should not intercept other editor errors', () => {
  217. const watchdog1 = Watchdog.for( ClassicTestEditor );
  218. const watchdog2 = Watchdog.for( ClassicTestEditor );
  219. const config = {
  220. plugins: []
  221. };
  222. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  223. const originalErrorHandler = window.onerror;
  224. window.onerror = undefined;
  225. return Promise.all( [
  226. watchdog1.create( element, config ),
  227. watchdog2.create( element, config )
  228. ] ).then( () => {
  229. return new Promise( res => {
  230. const watchdog1ErrorSpy = sinon.spy();
  231. const watchdog2ErrorSpy = sinon.spy();
  232. watchdog1.on( 'restart', watchdog1ErrorSpy );
  233. watchdog2.on( 'restart', watchdog2ErrorSpy );
  234. setTimeout( () => throwCKEditorError( 'foo', watchdog2.editor ) );
  235. setTimeout( () => {
  236. window.onerror = originalErrorHandler;
  237. sinon.assert.notCalled( watchdog1ErrorSpy );
  238. sinon.assert.calledOnce( watchdog2ErrorSpy );
  239. Promise.all( [ watchdog1.destroy(), watchdog2.destroy() ] )
  240. .then( res );
  241. } );
  242. } );
  243. } );
  244. } );
  245. it( 'Watchdog should intercept editor errors and restart the editor if the editor can be found from the context', () => {
  246. const watchdog = new Watchdog();
  247. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  248. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  249. const originalErrorHandler = window.onerror;
  250. window.onerror = undefined;
  251. return watchdog.create( element ).then( () => {
  252. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor.model.document ) );
  253. return new Promise( res => {
  254. watchdog.on( 'restart', () => {
  255. window.onerror = originalErrorHandler;
  256. watchdog.destroy().then( res );
  257. } );
  258. } );
  259. } );
  260. } );
  261. it( 'Watchdog should intercept editor errors and restart the editor if the editor can be found from the context #2', () => {
  262. const watchdog = new Watchdog();
  263. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  264. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  265. const originalErrorHandler = window.onerror;
  266. window.onerror = undefined;
  267. return watchdog.create( element ).then( () => {
  268. setTimeout( () => throwCKEditorError( 'foo', {
  269. foo: [ 1, 2, 3, {
  270. bar: new Set( [
  271. new Map( [
  272. [ 'foo', 'bar' ],
  273. [ 0, watchdog.editor ]
  274. ] )
  275. ] )
  276. } ]
  277. } ) );
  278. return new Promise( res => {
  279. watchdog.on( 'restart', () => {
  280. window.onerror = originalErrorHandler;
  281. watchdog.destroy().then( res );
  282. } );
  283. } );
  284. } );
  285. } );
  286. it( 'Watchdog should crash permanently if the `crashNumberLimit` is reached' +
  287. ' and the average time between errors is lower than `minimumNonErrorTimePeriod` (default values)', () => {
  288. const watchdog = new Watchdog();
  289. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  290. const errorSpy = sinon.spy();
  291. watchdog.on( 'error', errorSpy );
  292. const restartSpy = sinon.spy();
  293. watchdog.on( 'restart', restartSpy );
  294. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  295. const originalErrorHandler = window.onerror;
  296. window.onerror = undefined;
  297. return watchdog.create( element ).then( () => {
  298. setTimeout( () => throwCKEditorError( 'foo1', watchdog.editor ) );
  299. setTimeout( () => throwCKEditorError( 'foo2', watchdog.editor ) );
  300. setTimeout( () => throwCKEditorError( 'foo3', watchdog.editor ) );
  301. setTimeout( () => throwCKEditorError( 'foo4', watchdog.editor ) );
  302. return new Promise( res => {
  303. setTimeout( () => {
  304. expect( errorSpy.callCount ).to.equal( 4 );
  305. expect( watchdog.crashes.length ).to.equal( 4 );
  306. expect( restartSpy.callCount ).to.equal( 3 );
  307. window.onerror = originalErrorHandler;
  308. watchdog.destroy().then( res );
  309. } );
  310. } );
  311. } );
  312. } );
  313. it( 'Watchdog should crash permanently if the `crashNumberLimit` is reached' +
  314. ' and the average time between errors is lower than `minimumNonErrorTimePeriod` (custom values)', () => {
  315. const watchdog = new Watchdog( { crashNumberLimit: 2, minimumNonErrorTimePeriod: 1000 } );
  316. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  317. const errorSpy = sinon.spy();
  318. watchdog.on( 'error', errorSpy );
  319. const restartSpy = sinon.spy();
  320. watchdog.on( 'restart', restartSpy );
  321. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  322. const originalErrorHandler = window.onerror;
  323. window.onerror = undefined;
  324. return watchdog.create( element ).then( () => {
  325. setTimeout( () => throwCKEditorError( 'foo1', watchdog.editor ) );
  326. setTimeout( () => throwCKEditorError( 'foo2', watchdog.editor ) );
  327. setTimeout( () => throwCKEditorError( 'foo3', watchdog.editor ) );
  328. setTimeout( () => throwCKEditorError( 'foo4', watchdog.editor ) );
  329. return new Promise( res => {
  330. setTimeout( () => {
  331. expect( errorSpy.callCount ).to.equal( 3 );
  332. expect( watchdog.crashes.length ).to.equal( 3 );
  333. expect( restartSpy.callCount ).to.equal( 2 );
  334. window.onerror = originalErrorHandler;
  335. watchdog.destroy().then( res );
  336. } );
  337. } );
  338. } );
  339. } );
  340. it( 'Watchdog should not crash permantently when average time between errors is longer than `minimumNonErrorTimePeriod`', () => {
  341. const watchdog = new Watchdog( { crashNumberLimit: 2, minimumNonErrorTimePeriod: 0 } );
  342. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  343. const errorSpy = sinon.spy();
  344. watchdog.on( 'error', errorSpy );
  345. const restartSpy = sinon.spy();
  346. watchdog.on( 'restart', restartSpy );
  347. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  348. const originalErrorHandler = window.onerror;
  349. window.onerror = undefined;
  350. return watchdog.create( element ).then( () => {
  351. setTimeout( () => throwCKEditorError( 'foo1', watchdog.editor ), 5 );
  352. setTimeout( () => throwCKEditorError( 'foo2', watchdog.editor ), 10 );
  353. setTimeout( () => throwCKEditorError( 'foo3', watchdog.editor ), 15 );
  354. setTimeout( () => throwCKEditorError( 'foo4', watchdog.editor ), 20 );
  355. return new Promise( res => {
  356. setTimeout( () => {
  357. expect( errorSpy.callCount ).to.equal( 4 );
  358. expect( watchdog.crashes.length ).to.equal( 4 );
  359. expect( restartSpy.callCount ).to.equal( 4 );
  360. window.onerror = originalErrorHandler;
  361. watchdog.destroy().then( res );
  362. }, 20 );
  363. } );
  364. } );
  365. } );
  366. it( 'Watchdog should warn if the CKEditorError missing its context', () => {
  367. const watchdog = new Watchdog();
  368. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  369. watchdog.setDestructor( editor => editor.destroy() );
  370. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  371. const originalErrorHandler = window.onerror;
  372. window.onerror = undefined;
  373. sinon.stub( console, 'warn' );
  374. return watchdog.create( element ).then( () => {
  375. setTimeout( () => throwCKEditorError( 'foo' ) );
  376. return new Promise( res => {
  377. setTimeout( () => {
  378. window.onerror = originalErrorHandler;
  379. expect( watchdog.crashes ).to.deep.equal( [] );
  380. sinon.assert.calledWithExactly(
  381. console.warn,
  382. 'The error is missing its context and Watchdog cannot restart the proper editor.'
  383. );
  384. watchdog.destroy().then( res );
  385. } );
  386. } );
  387. } );
  388. } );
  389. it( 'Watchdog should omit error if the CKEditorError context is equal to null', () => {
  390. const watchdog = new Watchdog();
  391. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  392. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  393. const originalErrorHandler = window.onerror;
  394. window.onerror = undefined;
  395. return watchdog.create( element ).then( () => {
  396. setTimeout( () => throwCKEditorError( 'foo', null ) );
  397. return new Promise( res => {
  398. setTimeout( () => {
  399. window.onerror = originalErrorHandler;
  400. expect( watchdog.crashes ).to.deep.equal( [] );
  401. watchdog.destroy().then( res );
  402. } );
  403. } );
  404. } );
  405. } );
  406. it( 'editor should be restarted with the data before the crash #1', () => {
  407. const watchdog = new Watchdog();
  408. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  409. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  410. const originalErrorHandler = window.onerror;
  411. window.onerror = undefined;
  412. return watchdog.create( element, {
  413. initialData: '<p>foo</p>',
  414. plugins: [ Paragraph ]
  415. } ).then( () => {
  416. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  417. return new Promise( res => {
  418. watchdog.on( 'restart', () => {
  419. window.onerror = originalErrorHandler;
  420. expect( watchdog.editor.getData() ).to.equal( '<p>foo</p>' );
  421. watchdog.destroy().then( res );
  422. } );
  423. } );
  424. } );
  425. } );
  426. it( 'editor should be restarted with the data before the crash #2', () => {
  427. const watchdog = new Watchdog();
  428. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  429. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  430. const originalErrorHandler = window.onerror;
  431. window.onerror = undefined;
  432. return watchdog.create( element, {
  433. initialData: '<p>foo</p>',
  434. plugins: [ Paragraph ]
  435. } ).then( () => {
  436. const doc = watchdog.editor.model.document;
  437. watchdog.editor.model.change( writer => {
  438. writer.insertText( 'bar', writer.createPositionAt( doc.getRoot(), 1 ) );
  439. } );
  440. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  441. return new Promise( res => {
  442. watchdog.on( 'restart', () => {
  443. window.onerror = originalErrorHandler;
  444. expect( watchdog.editor.getData() ).to.equal( '<p>foo</p><p>bar</p>' );
  445. watchdog.destroy().then( res );
  446. } );
  447. } );
  448. } );
  449. } );
  450. it( 'editor should be restarted with the data of the latest document version before the crash', () => {
  451. const watchdog = new Watchdog();
  452. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  453. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  454. const originalErrorHandler = window.onerror;
  455. window.onerror = undefined;
  456. return watchdog.create( element, {
  457. initialData: '<p>foo</p>',
  458. plugins: [ Paragraph ]
  459. } ).then( () => {
  460. const model = watchdog.editor.model;
  461. const doc = model.document;
  462. // Decrement the document version to simulate a situation when an operation
  463. // don't produce new document version.
  464. doc.version--;
  465. model.change( writer => {
  466. writer.insertText( 'bar', writer.createPositionAt( doc.getRoot(), 1 ) );
  467. } );
  468. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  469. return new Promise( res => {
  470. watchdog.on( 'restart', () => {
  471. window.onerror = originalErrorHandler;
  472. expect( watchdog.editor.getData() ).to.equal( '<p>foo</p>' );
  473. watchdog.destroy().then( res );
  474. } );
  475. } );
  476. } );
  477. } );
  478. it( 'editor should be restarted with the latest available data before the crash', () => {
  479. const watchdog = new Watchdog();
  480. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  481. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  482. const originalErrorHandler = window.onerror;
  483. window.onerror = undefined;
  484. sinon.stub( console, 'error' );
  485. return watchdog.create( element, {
  486. initialData: '<p>foo</p>',
  487. plugins: [ Paragraph ]
  488. } ).then( () => {
  489. const editorGetDataError = new Error( 'Some error' );
  490. const getDataStub = sinon.stub( watchdog.editor.data, 'get' )
  491. .throwsException( editorGetDataError );
  492. // Keep the reference to cleanly destroy it at in the end, as during the TC it
  493. // throws an exception during destruction.
  494. const firstEditor = watchdog.editor;
  495. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  496. return new Promise( res => {
  497. const doc = watchdog.editor.model.document;
  498. watchdog.editor.model.change( writer => {
  499. writer.insertText( 'bar', writer.createPositionAt( doc.getRoot(), 1 ) );
  500. } );
  501. watchdog.on( 'restart', () => {
  502. window.onerror = originalErrorHandler;
  503. // It is called second time by during the default editor destruction
  504. // to update the source element.
  505. sinon.assert.calledTwice( getDataStub );
  506. expect( watchdog.editor.getData() ).to.equal( '<p>foo</p>' );
  507. sinon.assert.calledWith(
  508. console.error,
  509. editorGetDataError,
  510. 'An error happened during restoring editor data. Editor will be restored from the previously saved data.'
  511. );
  512. sinon.assert.calledWith(
  513. console.error,
  514. 'An error happened during the editor destructing.'
  515. );
  516. watchdog.destroy().then( () => {
  517. getDataStub.restore();
  518. return firstEditor.destroy();
  519. } ).then( res );
  520. } );
  521. } );
  522. } );
  523. } );
  524. it( 'should use the custom destructor if passed', () => {
  525. const watchdog = new Watchdog();
  526. const destructionSpy = sinon.spy();
  527. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  528. watchdog.setDestructor( editor => {
  529. destructionSpy();
  530. return editor.destroy();
  531. } );
  532. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  533. const originalErrorHandler = window.onerror;
  534. window.onerror = undefined;
  535. return watchdog.create( element ).then( () => {
  536. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  537. return new Promise( res => {
  538. watchdog.on( 'restart', () => {
  539. window.onerror = originalErrorHandler;
  540. sinon.assert.calledOnce( destructionSpy );
  541. watchdog.destroy().then( res );
  542. } );
  543. } );
  544. } );
  545. } );
  546. // Searches for orphaned editors based on DOM.
  547. //
  548. // This is useful if in your tests you have no access to editor, instance because editor
  549. // creation method doesn't complete in a graceful manner.
  550. function destroyEditorOrphans() {
  551. const promises = [];
  552. for ( const editableOrphan of document.querySelectorAll( '.ck-editor__editable' ) ) {
  553. if ( editableOrphan.ckeditorInstance ) {
  554. promises.push( editableOrphan.ckeditorInstance.destroy() );
  555. }
  556. }
  557. return Promise.all( promises );
  558. }
  559. } );
  560. describe( 'async error handling', () => {
  561. let unhandledRejectionEventSupported;
  562. before( () => {
  563. return isUnhandledRejectionEventSupported()
  564. .then( val => {
  565. unhandledRejectionEventSupported = val;
  566. } );
  567. } );
  568. it( 'Watchdog should handle async CKEditorError errors', () => {
  569. if ( !unhandledRejectionEventSupported ) {
  570. return;
  571. }
  572. const watchdog = Watchdog.for( ClassicTestEditor );
  573. const originalErrorHandler = window.onerror;
  574. window.onerror = undefined;
  575. return watchdog.create( element, {
  576. initialData: '<p>foo</p>',
  577. plugins: [ Paragraph ]
  578. } ).then( () => {
  579. const oldEditor = watchdog.editor;
  580. Promise.resolve().then( () => throwCKEditorError( 'foo', watchdog.editor ) );
  581. return new Promise( res => {
  582. watchdog.on( 'restart', () => {
  583. window.onerror = originalErrorHandler;
  584. expect( watchdog.editor ).to.not.equal( oldEditor );
  585. expect( watchdog.editor.getData() ).to.equal( '<p>foo</p>' );
  586. watchdog.destroy().then( res );
  587. } );
  588. } );
  589. } );
  590. } );
  591. it( 'Watchdog should not react to non-editor async errors', () => {
  592. if ( !unhandledRejectionEventSupported ) {
  593. return;
  594. }
  595. const watchdog = Watchdog.for( ClassicTestEditor );
  596. const originalErrorHandler = window.onerror;
  597. const editorErrorSpy = sinon.spy();
  598. window.onerror = undefined;
  599. return watchdog.create( element, {
  600. initialData: '<p>foo</p>',
  601. plugins: [ Paragraph ]
  602. } ).then( () => {
  603. watchdog.on( 'error', editorErrorSpy );
  604. Promise.resolve().then( () => Promise.reject( 'foo' ) );
  605. Promise.resolve().then( () => Promise.reject( new Error( 'bar' ) ) );
  606. // Wait a cycle.
  607. return new Promise( res => setTimeout( res ) );
  608. } ).then( () => {
  609. window.onerror = originalErrorHandler;
  610. sinon.assert.notCalled( editorErrorSpy );
  611. expect( watchdog.editor.getData() ).to.equal( '<p>foo</p>' );
  612. return watchdog.destroy();
  613. } );
  614. } );
  615. } );
  616. describe( 'destroy()', () => {
  617. // See #19.
  618. it( 'should clean internal stuff', () => {
  619. // 30ms should be enough to make the two data changes split into two data save actions.
  620. // This will ensure that the second data save action will be put off in time.
  621. const SAVE_INTERVAL = 30;
  622. const watchdog = Watchdog.for( ClassicTestEditor, {
  623. saveInterval: SAVE_INTERVAL,
  624. } );
  625. return watchdog.create( element, {
  626. initialData: '<p>foo</p>',
  627. plugins: [ Paragraph ]
  628. } ).then( () => {
  629. const doc = watchdog.editor.model.document;
  630. watchdog.editor.model.change( writer => {
  631. writer.insertText( 'bar', writer.createPositionAt( doc.getRoot(), 1 ) );
  632. } );
  633. watchdog.editor.model.change( writer => {
  634. writer.insertText( 'foo', writer.createPositionAt( doc.getRoot(), 1 ) );
  635. } );
  636. return watchdog.destroy();
  637. } ).then( () => {
  638. // Wait to ensure that the throttled save is cleared and won't be executed
  639. // on the non-existing editor.
  640. return new Promise( res => setTimeout( res, SAVE_INTERVAL ) );
  641. } ).then( () => {
  642. expect( watchdog.editor ).to.equal( null );
  643. expect( watchdog.state ).to.equal( 'destroyed' );
  644. expect( watchdog.crashes ).to.deep.equal( [] );
  645. } );
  646. } );
  647. } );
  648. describe( 'static for()', () => {
  649. it( 'should be a shortcut method for creating the watchdog', () => {
  650. const watchdog = Watchdog.for( ClassicTestEditor );
  651. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  652. const originalErrorHandler = window.onerror;
  653. window.onerror = undefined;
  654. return watchdog.create( element, {
  655. initialData: '<p>foo</p>',
  656. plugins: [ Paragraph ]
  657. } ).then( () => {
  658. const oldEditor = watchdog.editor;
  659. expect( watchdog.editor ).to.be.an.instanceOf( ClassicTestEditor );
  660. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  661. return new Promise( res => {
  662. watchdog.on( 'restart', () => {
  663. window.onerror = originalErrorHandler;
  664. expect( watchdog.editor ).to.be.an.instanceOf( ClassicTestEditor );
  665. expect( watchdog.editor ).to.not.equal( oldEditor );
  666. expect( watchdog.editor.getData() ).to.equal( '<p>foo</p>' );
  667. watchdog.destroy().then( res );
  668. } );
  669. } );
  670. } );
  671. } );
  672. } );
  673. describe( 'crashes', () => {
  674. it( 'should be an array of caught errors by the watchdog', () => {
  675. const watchdog = Watchdog.for( ClassicTestEditor );
  676. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  677. const originalErrorHandler = window.onerror;
  678. window.onerror = undefined;
  679. return watchdog.create( element ).then( () => {
  680. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  681. setTimeout( () => throwCKEditorError( 'bar', watchdog.editor ) );
  682. return new Promise( res => {
  683. setTimeout( () => {
  684. window.onerror = originalErrorHandler;
  685. expect( watchdog.crashes[ 0 ].message ).to.equal( 'foo' );
  686. expect( watchdog.crashes[ 0 ].stack ).to.be.a( 'string' );
  687. expect( watchdog.crashes[ 0 ].date ).to.be.a( 'number' );
  688. expect( watchdog.crashes[ 0 ].filename ).to.be.a( 'string' );
  689. expect( watchdog.crashes[ 0 ].lineno ).to.be.a( 'number' );
  690. expect( watchdog.crashes[ 0 ].colno ).to.be.a( 'number' );
  691. expect( watchdog.crashes[ 1 ].message ).to.equal( 'bar' );
  692. watchdog.destroy().then( res );
  693. } );
  694. } );
  695. } );
  696. } );
  697. it( 'should include async errors', () => {
  698. return isUnhandledRejectionEventSupported().then( isSupported => {
  699. if ( !isSupported ) {
  700. return;
  701. }
  702. const watchdog = Watchdog.for( ClassicTestEditor );
  703. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  704. const originalErrorHandler = window.onerror;
  705. window.onerror = undefined;
  706. return watchdog.create( element ).then( () => {
  707. Promise.resolve().then( () => throwCKEditorError( 'foo', watchdog.editor ) );
  708. return new Promise( res => {
  709. // This `setTimeout` needs to have a timer defined because Firefox calls the code in random order
  710. // and causes the test failed.
  711. setTimeout( () => {
  712. window.onerror = originalErrorHandler;
  713. expect( watchdog.crashes[ 0 ].message ).to.equal( 'foo' );
  714. expect( watchdog.crashes[ 0 ].stack ).to.be.a( 'string' );
  715. expect( watchdog.crashes[ 0 ].date ).to.be.a( 'number' );
  716. expect( watchdog.crashes[ 0 ].filename ).to.be.an( 'undefined' );
  717. expect( watchdog.crashes[ 0 ].lineno ).to.be.an( 'undefined' );
  718. expect( watchdog.crashes[ 0 ].colno ).to.be.an( 'undefined' );
  719. watchdog.destroy().then( res );
  720. }, 10 );
  721. } );
  722. } );
  723. } );
  724. } );
  725. } );
  726. describe( 'state', () => {
  727. let orphanEditors = [];
  728. afterEach( () => {
  729. return Promise.all( orphanEditors.map( editor => editor.destroy() ) )
  730. .then( () => {
  731. orphanEditors = [];
  732. } );
  733. } );
  734. it( 'should reflect the state of the watchdog', () => {
  735. const watchdog = Watchdog.for( ClassicTestEditor );
  736. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  737. const originalErrorHandler = window.onerror;
  738. window.onerror = undefined;
  739. expect( watchdog.state ).to.equal( 'initializing' );
  740. return watchdog.create( element ).then( () => {
  741. orphanEditors.push( watchdog.editor );
  742. expect( watchdog.state ).to.equal( 'ready' );
  743. return watchdog.create( element ).then( () => {
  744. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  745. setTimeout( () => throwCKEditorError( 'bar', watchdog.editor ) );
  746. return new Promise( res => {
  747. setTimeout( () => {
  748. window.onerror = originalErrorHandler;
  749. expect( watchdog.state ).to.equal( 'ready' );
  750. watchdog.destroy().then( () => {
  751. expect( watchdog.state ).to.equal( 'destroyed' );
  752. res();
  753. } );
  754. } );
  755. } );
  756. } );
  757. } );
  758. } );
  759. it( 'should be observable', () => {
  760. const watchdog = Watchdog.for( ClassicTestEditor );
  761. const states = [];
  762. watchdog.on( 'change:state', ( evt, propName, newValue ) => {
  763. states.push( newValue );
  764. } );
  765. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  766. const originalErrorHandler = window.onerror;
  767. window.onerror = undefined;
  768. return watchdog.create( element ).then( () => {
  769. orphanEditors.push( watchdog.editor );
  770. return watchdog.create( element ).then( () => {
  771. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  772. setTimeout( () => throwCKEditorError( 'bar', watchdog.editor ) );
  773. setTimeout( () => throwCKEditorError( 'baz', watchdog.editor ) );
  774. setTimeout( () => throwCKEditorError( 'biz', watchdog.editor ) );
  775. return new Promise( res => {
  776. setTimeout( () => {
  777. window.onerror = originalErrorHandler;
  778. watchdog.destroy().then( () => {
  779. expect( states ).to.deep.equal( [
  780. 'ready',
  781. 'crashed',
  782. 'initializing',
  783. 'ready',
  784. 'crashed',
  785. 'initializing',
  786. 'ready',
  787. 'crashed',
  788. 'initializing',
  789. 'ready',
  790. 'crashed',
  791. 'crashedPermanently',
  792. 'destroyed'
  793. ] );
  794. res();
  795. } );
  796. } );
  797. } );
  798. } );
  799. } );
  800. } );
  801. } );
  802. describe( 'multi-root editors', () => {
  803. it( 'should support multi-root editors', () => {
  804. class MultiRootEditor extends Editor {
  805. constructor( sourceElements, config ) {
  806. super( config );
  807. this.data.processor = new HtmlDataProcessor();
  808. // Create a root for each source element.
  809. for ( const rootName of Object.keys( sourceElements ) ) {
  810. this.model.document.createRoot( '$root', rootName );
  811. }
  812. }
  813. static async create( sourceElements, config ) {
  814. const editor = new this( sourceElements, config );
  815. await editor.initPlugins();
  816. await editor.data.init( config.initialData );
  817. editor.fire( 'ready' );
  818. return editor;
  819. }
  820. }
  821. const watchdog = Watchdog.for( MultiRootEditor );
  822. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  823. const originalErrorHandler = window.onerror;
  824. window.onerror = undefined;
  825. return watchdog
  826. .create( {
  827. header: element
  828. }, {
  829. initialData: {
  830. header: '<p>Foo</p>'
  831. },
  832. plugins: [ Paragraph ]
  833. } )
  834. .then( () => {
  835. expect( watchdog.editor.data.get( { rootName: 'header' } ) ).to.equal( '<p>Foo</p>' );
  836. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  837. return new Promise( res => {
  838. window.onerror = originalErrorHandler;
  839. expect( watchdog.editor.data.get( { rootName: 'header' } ) ).to.equal( '<p>Foo</p>' );
  840. res();
  841. } );
  842. } ).then( () => {
  843. return watchdog.destroy();
  844. } );
  845. } );
  846. } );
  847. } );
  848. function throwCKEditorError( name, context ) {
  849. throw new CKEditorError( name, context );
  850. }
  851. // Feature detection works as a race condition - if the `unhandledrejection` event
  852. // is supported then the listener should be called first. Otherwise the timeout will be reached.
  853. function isUnhandledRejectionEventSupported() {
  854. return new Promise( res => {
  855. window.addEventListener( 'unhandledrejection', function listener() {
  856. res( true );
  857. window.removeEventListener( 'unhandledrejection', listener );
  858. } );
  859. Promise.resolve().then( () => Promise.reject( new Error() ) );
  860. setTimeout( () => res( false ) );
  861. } );
  862. }