editorwatchdog.js 30 KB

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