editorwatchdog.js 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019
  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 item.'
  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.match( /^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.match( /^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.match( /^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. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  650. setTimeout( () => throwCKEditorError( 'bar', watchdog.editor ) );
  651. await waitCycle();
  652. window.onerror = originalErrorHandler;
  653. expect( watchdog.state ).to.equal( 'ready' );
  654. await watchdog.destroy();
  655. expect( watchdog.state ).to.equal( 'destroyed' );
  656. } );
  657. it( 'should be observable', async () => {
  658. const watchdog = new EditorWatchdog( ClassicTestEditor );
  659. const states = [];
  660. watchdog.on( 'stateChange', () => {
  661. states.push( watchdog.state );
  662. } );
  663. const originalErrorHandler = window.onerror;
  664. window.onerror = undefined;
  665. await watchdog.create( element );
  666. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  667. setTimeout( () => throwCKEditorError( 'bar', watchdog.editor ) );
  668. setTimeout( () => throwCKEditorError( 'baz', watchdog.editor ) );
  669. setTimeout( () => throwCKEditorError( 'biz', watchdog.editor ) );
  670. await waitCycle();
  671. window.onerror = originalErrorHandler;
  672. await watchdog.destroy();
  673. expect( states ).to.deep.equal( [
  674. 'ready',
  675. 'crashed',
  676. 'initializing',
  677. 'ready',
  678. 'crashed',
  679. 'initializing',
  680. 'ready',
  681. 'crashed',
  682. 'initializing',
  683. 'ready',
  684. 'crashed',
  685. 'crashedPermanently',
  686. 'destroyed'
  687. ] );
  688. } );
  689. } );
  690. describe( 'multi-root editors', () => {
  691. it( 'should support multi-root editors', async () => {
  692. class MultiRootEditor extends Editor {
  693. constructor( sourceElements, config ) {
  694. super( config );
  695. this.data.processor = new HtmlDataProcessor( this.data.viewDocument );
  696. // Create a root for each source element.
  697. for ( const rootName of Object.keys( sourceElements ) ) {
  698. this.model.document.createRoot( '$root', rootName );
  699. }
  700. }
  701. static async create( sourceElements, config ) {
  702. const editor = new this( sourceElements, config );
  703. await editor.initPlugins();
  704. await editor.data.init( config.initialData );
  705. editor.fire( 'ready' );
  706. return editor;
  707. }
  708. }
  709. const watchdog = new EditorWatchdog( MultiRootEditor );
  710. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  711. const originalErrorHandler = window.onerror;
  712. window.onerror = undefined;
  713. await watchdog.create( {
  714. header: element
  715. }, {
  716. initialData: {
  717. header: '<p>Foo</p>'
  718. },
  719. plugins: [ Paragraph ]
  720. } );
  721. expect( watchdog.editor.data.get( { rootName: 'header' } ) ).to.equal( '<p>Foo</p>' );
  722. const restartSpy = sinon.spy();
  723. watchdog.on( 'restart', restartSpy );
  724. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  725. await waitCycle();
  726. window.onerror = originalErrorHandler;
  727. sinon.assert.calledOnce( restartSpy );
  728. expect( watchdog.editor.data.get( { rootName: 'header' } ) ).to.equal( '<p>Foo</p>' );
  729. await watchdog.destroy();
  730. } );
  731. } );
  732. } );
  733. function throwCKEditorError( name, context ) {
  734. throw new CKEditorError( name, context );
  735. }
  736. // Feature detection works as a race condition - if the `unhandledrejection` event
  737. // is supported then the listener should be called first. Otherwise the timeout will be reached.
  738. function isUnhandledRejectionEventSupported() {
  739. return new Promise( res => {
  740. window.addEventListener( 'unhandledrejection', function listener() {
  741. res( true );
  742. window.removeEventListener( 'unhandledrejection', listener );
  743. } );
  744. Promise.resolve().then( () => Promise.reject( new Error() ) );
  745. setTimeout( () => res( false ) );
  746. } );
  747. }
  748. function waitCycle() {
  749. return new Promise( res => setTimeout( res ) );
  750. }