watchdog.js 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751
  1. /**
  2. * @license Copyright (c) 2003-2019, 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 ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  8. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  9. import Paragraph from '@ckeditor/ckeditor5-paragraph/src/paragraph';
  10. import { expectToThrowCKEditorError } from '@ckeditor/ckeditor5-utils/tests/_utils/utils';
  11. describe( 'Watchdog', () => {
  12. let element;
  13. beforeEach( () => {
  14. element = document.createElement( 'div' );
  15. document.body.appendChild( element );
  16. } );
  17. afterEach( () => {
  18. element.remove();
  19. sinon.restore();
  20. } );
  21. describe( 'create()', () => {
  22. it( 'should create an editor instance', () => {
  23. const watchdog = new Watchdog();
  24. const editorCreateSpy = sinon.spy( ClassicTestEditor, 'create' );
  25. const editorDestroySpy = sinon.spy( ClassicTestEditor.prototype, 'destroy' );
  26. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  27. watchdog.setDestructor( editor => editor.destroy() );
  28. return watchdog.create( element, {} )
  29. .then( () => {
  30. sinon.assert.calledOnce( editorCreateSpy );
  31. sinon.assert.notCalled( editorDestroySpy );
  32. return watchdog.destroy();
  33. } )
  34. .then( () => {
  35. sinon.assert.calledOnce( editorCreateSpy );
  36. sinon.assert.calledOnce( editorDestroySpy );
  37. } );
  38. } );
  39. it( 'should throw an error when the creator is not defined', () => {
  40. const watchdog = new Watchdog();
  41. watchdog.setDestructor( editor => editor.destroy() );
  42. expectToThrowCKEditorError(
  43. () => watchdog.create(),
  44. /^watchdog-creator-not-defined/,
  45. null
  46. );
  47. } );
  48. it( 'should throw an error when the destructor is not defined', () => {
  49. const watchdog = new Watchdog();
  50. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  51. expectToThrowCKEditorError(
  52. () => watchdog.create(),
  53. /^watchdog-destructor-not-defined/,
  54. null
  55. );
  56. } );
  57. it( 'should properly copy the config', () => {
  58. const watchdog = new Watchdog();
  59. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  60. watchdog.setDestructor( editor => editor.destroy() );
  61. const config = {
  62. foo: [],
  63. bar: document.createElement( 'div' )
  64. };
  65. return watchdog.create( element, config ).then( () => {
  66. expect( watchdog.editor.config._config.foo ).to.not.equal( config.foo );
  67. expect( watchdog.editor.config._config.bar ).to.equal( config.bar );
  68. } );
  69. } );
  70. } );
  71. describe( 'restart()', () => {
  72. it( 'should restart the editor', () => {
  73. const watchdog = new Watchdog();
  74. const editorCreateSpy = sinon.spy( ClassicTestEditor, 'create' );
  75. const editorDestroySpy = sinon.spy( ClassicTestEditor.prototype, 'destroy' );
  76. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  77. watchdog.setDestructor( editor => editor.destroy() );
  78. return watchdog.create( element, {} )
  79. .then( () => {
  80. sinon.assert.calledOnce( editorCreateSpy );
  81. sinon.assert.notCalled( editorDestroySpy );
  82. return watchdog.restart();
  83. } )
  84. .then( () => {
  85. sinon.assert.calledTwice( editorCreateSpy );
  86. sinon.assert.calledOnce( editorDestroySpy );
  87. return watchdog.destroy();
  88. } );
  89. } );
  90. it( 'should restart the editor with the same data', () => {
  91. const watchdog = new Watchdog();
  92. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  93. watchdog.setDestructor( editor => editor.destroy() );
  94. return watchdog.create( element, {
  95. initialData: '<p>foo</p>',
  96. plugins: [ Paragraph ]
  97. } )
  98. .then( () => {
  99. expect( watchdog.editor.getData() ).to.equal( '<p>foo</p>' );
  100. return watchdog.restart();
  101. } )
  102. .then( () => {
  103. expect( watchdog.editor.getData() ).to.equal( '<p>foo</p>' );
  104. return watchdog.destroy();
  105. } );
  106. } );
  107. it( 'should support editor data passed as the `Editor.create()` as the first argument', () => {
  108. const watchdog = new Watchdog();
  109. watchdog.setCreator( ( data, config ) => ClassicTestEditor.create( data, config ) );
  110. watchdog.setDestructor( editor => editor.destroy() );
  111. return watchdog.create( '<p>foo</p>', { plugins: [ Paragraph ] } )
  112. .then( () => {
  113. expect( watchdog.editor.getData() ).to.equal( '<p>foo</p>' );
  114. return watchdog.restart();
  115. } )
  116. .then( () => {
  117. expect( watchdog.editor.getData() ).to.equal( '<p>foo</p>' );
  118. return watchdog.destroy();
  119. } );
  120. } );
  121. } );
  122. describe( 'editor', () => {
  123. it( 'should be the current editor instance', () => {
  124. const watchdog = new Watchdog();
  125. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  126. watchdog.setDestructor( editor => editor.destroy() );
  127. expect( watchdog.editor ).to.be.null;
  128. let oldEditor;
  129. return watchdog.create( element, {} )
  130. .then( () => {
  131. oldEditor = watchdog.editor;
  132. expect( watchdog.editor ).to.be.instanceOf( ClassicTestEditor );
  133. return watchdog.restart();
  134. } )
  135. .then( () => {
  136. expect( watchdog.editor ).to.be.instanceOf( ClassicTestEditor );
  137. expect( watchdog.editor ).to.not.equal( oldEditor );
  138. return watchdog.destroy();
  139. } )
  140. .then( () => {
  141. expect( watchdog.editor ).to.be.null;
  142. } );
  143. } );
  144. } );
  145. describe( 'error handling', () => {
  146. it( 'Watchdog should not restart editor during the initialization', () => {
  147. const watchdog = new Watchdog();
  148. watchdog.setCreator( el =>
  149. ClassicTestEditor.create( el )
  150. .then( () => Promise.reject( new Error( 'foo' ) ) )
  151. );
  152. watchdog.setDestructor( editor => editor.destroy() );
  153. return watchdog.create( element ).then(
  154. () => { throw new Error( '`watchdog.create()` should throw an error.' ); },
  155. err => {
  156. expect( err ).to.be.instanceOf( Error );
  157. expect( err.message ).to.equal( 'foo' );
  158. }
  159. );
  160. } );
  161. it( 'Watchdog should not restart editor during the destroy', () => {
  162. const watchdog = new Watchdog();
  163. watchdog.setCreator( el => ClassicTestEditor.create( el ) );
  164. watchdog.setDestructor( () => Promise.reject( new Error( 'foo' ) ) );
  165. return Promise.resolve()
  166. .then( () => watchdog.create( element ) )
  167. .then( () => watchdog.destroy() )
  168. .then(
  169. () => { throw new Error( '`watchdog.create()` should throw an error.' ); },
  170. err => {
  171. expect( err ).to.be.instanceOf( Error );
  172. expect( err.message ).to.equal( 'foo' );
  173. }
  174. );
  175. } );
  176. it( 'Watchdog should not hide intercepted errors', () => {
  177. const watchdog = new Watchdog();
  178. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  179. watchdog.setDestructor( editor => editor.destroy() );
  180. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  181. const originalErrorHandler = window.onerror;
  182. const windowErrorSpy = sinon.spy();
  183. window.onerror = windowErrorSpy;
  184. return watchdog.create( element ).then( () => {
  185. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  186. return new Promise( res => {
  187. watchdog.on( 'restart', () => {
  188. window.onerror = originalErrorHandler;
  189. sinon.assert.calledOnce( windowErrorSpy );
  190. // Various browsers will display the error slightly differently.
  191. expect( windowErrorSpy.getCall( 0 ).args[ 0 ] ).to.match( /foo/ );
  192. watchdog.destroy().then( res );
  193. } );
  194. } );
  195. } );
  196. } );
  197. it( 'Watchdog should intercept editor errors and restart the editor during the runtime', () => {
  198. const watchdog = new Watchdog();
  199. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  200. watchdog.setDestructor( editor => editor.destroy() );
  201. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  202. const originalErrorHandler = window.onerror;
  203. window.onerror = undefined;
  204. return watchdog.create( element ).then( () => {
  205. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  206. return new Promise( res => {
  207. watchdog.on( 'restart', () => {
  208. window.onerror = originalErrorHandler;
  209. watchdog.destroy().then( res );
  210. } );
  211. } );
  212. } );
  213. } );
  214. it( 'Watchdog should not intercept non-editor errors', () => {
  215. const watchdog = new Watchdog();
  216. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  217. watchdog.setDestructor( editor => editor.destroy() );
  218. const editorErrorSpy = sinon.spy();
  219. watchdog.on( 'error', editorErrorSpy );
  220. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  221. const originalErrorHandler = window.onerror;
  222. window.onerror = undefined;
  223. return watchdog.create( element ).then( () => {
  224. setTimeout( () => {
  225. throw new Error( 'foo' );
  226. } );
  227. return new Promise( res => {
  228. setTimeout( () => {
  229. window.onerror = originalErrorHandler;
  230. sinon.assert.notCalled( editorErrorSpy );
  231. watchdog.destroy().then( res );
  232. } );
  233. } );
  234. } );
  235. } );
  236. it( 'Watchdog should not intercept other editor errors', () => {
  237. const watchdog1 = Watchdog.for( ClassicTestEditor );
  238. const watchdog2 = Watchdog.for( ClassicTestEditor );
  239. const config = {
  240. plugins: []
  241. };
  242. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  243. const originalErrorHandler = window.onerror;
  244. window.onerror = undefined;
  245. return Promise.all( [
  246. watchdog1.create( element, config ),
  247. watchdog2.create( element, config )
  248. ] ).then( () => {
  249. return new Promise( res => {
  250. const watchdog1ErrorSpy = sinon.spy();
  251. const watchdog2ErrorSpy = sinon.spy();
  252. watchdog1.on( 'restart', watchdog1ErrorSpy );
  253. watchdog2.on( 'restart', watchdog2ErrorSpy );
  254. setTimeout( () => throwCKEditorError( 'foo', watchdog2.editor ) );
  255. setTimeout( () => {
  256. window.onerror = originalErrorHandler;
  257. sinon.assert.notCalled( watchdog1ErrorSpy );
  258. sinon.assert.calledOnce( watchdog2ErrorSpy );
  259. Promise.all( [ watchdog1.destroy(), watchdog2.destroy() ] )
  260. .then( res );
  261. } );
  262. } );
  263. } );
  264. } );
  265. it( 'Watchdog should intercept editor errors and restart the editor if the editor can be found from the context', () => {
  266. const watchdog = new Watchdog();
  267. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  268. watchdog.setDestructor( editor => editor.destroy() );
  269. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  270. const originalErrorHandler = window.onerror;
  271. window.onerror = undefined;
  272. return watchdog.create( element ).then( () => {
  273. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor.model.document ) );
  274. return new Promise( res => {
  275. watchdog.on( 'restart', () => {
  276. window.onerror = originalErrorHandler;
  277. watchdog.destroy().then( res );
  278. } );
  279. } );
  280. } );
  281. } );
  282. it( 'Watchdog should intercept editor errors and restart the editor if the editor can be found from the context #2', () => {
  283. const watchdog = new Watchdog();
  284. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  285. watchdog.setDestructor( editor => editor.destroy() );
  286. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  287. const originalErrorHandler = window.onerror;
  288. window.onerror = undefined;
  289. return watchdog.create( element ).then( () => {
  290. setTimeout( () => throwCKEditorError( 'foo', {
  291. foo: [ 1, 2, 3, {
  292. bar: new Set( [
  293. new Map( [
  294. [ 'foo', 'bar' ],
  295. [ 0, watchdog.editor ]
  296. ] )
  297. ] )
  298. } ]
  299. } ) );
  300. return new Promise( res => {
  301. watchdog.on( 'restart', () => {
  302. window.onerror = originalErrorHandler;
  303. watchdog.destroy().then( res );
  304. } );
  305. } );
  306. } );
  307. } );
  308. it( 'editor should be restarted up to 3 times by default', () => {
  309. const watchdog = new Watchdog();
  310. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  311. watchdog.setDestructor( editor => editor.destroy() );
  312. const errorSpy = sinon.spy();
  313. watchdog.on( 'error', errorSpy );
  314. const restartSpy = sinon.spy();
  315. watchdog.on( 'restart', restartSpy );
  316. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  317. const originalErrorHandler = window.onerror;
  318. window.onerror = undefined;
  319. return watchdog.create( element ).then( () => {
  320. setTimeout( () => throwCKEditorError( 'foo1', watchdog.editor ) );
  321. setTimeout( () => throwCKEditorError( 'foo2', watchdog.editor ) );
  322. setTimeout( () => throwCKEditorError( 'foo3', watchdog.editor ) );
  323. setTimeout( () => throwCKEditorError( 'foo4', watchdog.editor ) );
  324. return new Promise( res => {
  325. setTimeout( () => {
  326. expect( errorSpy.callCount ).to.equal( 4 );
  327. expect( watchdog.crashes.length ).to.equal( 4 );
  328. expect( restartSpy.callCount ).to.equal( 3 );
  329. window.onerror = originalErrorHandler;
  330. watchdog.destroy().then( res );
  331. } );
  332. } );
  333. } );
  334. } );
  335. it( 'editor should be restarted up to `crashNumberLimit` times if the option is set', () => {
  336. const watchdog = new Watchdog( { crashNumberLimit: 2 } );
  337. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  338. watchdog.setDestructor( editor => editor.destroy() );
  339. const errorSpy = sinon.spy();
  340. watchdog.on( 'error', errorSpy );
  341. const restartSpy = sinon.spy();
  342. watchdog.on( 'restart', restartSpy );
  343. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  344. const originalErrorHandler = window.onerror;
  345. window.onerror = undefined;
  346. return watchdog.create( element ).then( () => {
  347. setTimeout( () => throwCKEditorError( 'foo1', watchdog.editor ) );
  348. setTimeout( () => throwCKEditorError( 'foo2', watchdog.editor ) );
  349. setTimeout( () => throwCKEditorError( 'foo3', watchdog.editor ) );
  350. setTimeout( () => throwCKEditorError( 'foo4', watchdog.editor ) );
  351. return new Promise( res => {
  352. setTimeout( () => {
  353. expect( errorSpy.callCount ).to.equal( 4 );
  354. expect( watchdog.crashes.length ).to.equal( 4 );
  355. expect( restartSpy.callCount ).to.equal( 2 );
  356. window.onerror = originalErrorHandler;
  357. watchdog.destroy().then( res );
  358. } );
  359. } );
  360. } );
  361. } );
  362. it( 'Watchdog should warn if the CKEditorError missing its context', () => {
  363. const watchdog = new Watchdog();
  364. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  365. watchdog.setDestructor( editor => editor.destroy() );
  366. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  367. const originalErrorHandler = window.onerror;
  368. window.onerror = undefined;
  369. sinon.stub( console, 'error' );
  370. return watchdog.create( element ).then( () => {
  371. setTimeout( () => throwCKEditorError( 'foo' ) );
  372. return new Promise( res => {
  373. setTimeout( () => {
  374. window.onerror = originalErrorHandler;
  375. expect( watchdog.crashes ).to.deep.equal( [] );
  376. sinon.assert.calledWithExactly(
  377. console.error,
  378. 'The error is missing its context and Watchdog cannot restart the proper editor.'
  379. );
  380. watchdog.destroy().then( res );
  381. } );
  382. } );
  383. } );
  384. } );
  385. it( 'Watchdog should omit error if the CKEditorError context is equal to null', () => {
  386. const watchdog = new Watchdog();
  387. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  388. watchdog.setDestructor( editor => editor.destroy() );
  389. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  390. const originalErrorHandler = window.onerror;
  391. window.onerror = undefined;
  392. sinon.stub( console, 'error' );
  393. return watchdog.create( element ).then( () => {
  394. setTimeout( () => throwCKEditorError( 'foo', null ) );
  395. return new Promise( res => {
  396. setTimeout( () => {
  397. window.onerror = originalErrorHandler;
  398. expect( watchdog.crashes ).to.deep.equal( [] );
  399. sinon.assert.notCalled( console.error );
  400. watchdog.destroy().then( res );
  401. } );
  402. } );
  403. } );
  404. } );
  405. it( 'editor should be restarted with the data before the crash #1', () => {
  406. const watchdog = new Watchdog();
  407. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  408. watchdog.setDestructor( editor => editor.destroy() );
  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. watchdog.setDestructor( editor => editor.destroy() );
  430. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  431. const originalErrorHandler = window.onerror;
  432. window.onerror = undefined;
  433. return watchdog.create( element, {
  434. initialData: '<p>foo</p>',
  435. plugins: [ Paragraph ]
  436. } ).then( () => {
  437. const doc = watchdog.editor.model.document;
  438. watchdog.editor.model.change( writer => {
  439. writer.insertText( 'bar', writer.createPositionAt( doc.getRoot(), 1 ) );
  440. } );
  441. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  442. return new Promise( res => {
  443. watchdog.on( 'restart', () => {
  444. window.onerror = originalErrorHandler;
  445. expect( watchdog.editor.getData() ).to.equal( '<p>foo</p><p>bar</p>' );
  446. watchdog.destroy().then( res );
  447. } );
  448. } );
  449. } );
  450. } );
  451. it( 'editor should be restarted with the data of the latest document version before the crash', () => {
  452. const watchdog = new Watchdog();
  453. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  454. watchdog.setDestructor( editor => editor.destroy() );
  455. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  456. const originalErrorHandler = window.onerror;
  457. window.onerror = undefined;
  458. return watchdog.create( element, {
  459. initialData: '<p>foo</p>',
  460. plugins: [ Paragraph ]
  461. } ).then( () => {
  462. const model = watchdog.editor.model;
  463. const doc = model.document;
  464. // Decrement the document version to simulate a situation when an operation
  465. // don't produce new document version.
  466. doc.version--;
  467. model.change( writer => {
  468. writer.insertText( 'bar', writer.createPositionAt( doc.getRoot(), 1 ) );
  469. } );
  470. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  471. return new Promise( res => {
  472. watchdog.on( 'restart', () => {
  473. window.onerror = originalErrorHandler;
  474. expect( watchdog.editor.getData() ).to.equal( '<p>foo</p>' );
  475. watchdog.destroy().then( res );
  476. } );
  477. } );
  478. } );
  479. } );
  480. it( 'editor should be restarted with the latest available data before the crash', () => {
  481. const watchdog = new Watchdog();
  482. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  483. watchdog.setDestructor( editor => editor.destroy() );
  484. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  485. const originalErrorHandler = window.onerror;
  486. window.onerror = undefined;
  487. sinon.stub( console, 'error' );
  488. return watchdog.create( element, {
  489. initialData: '<p>foo</p>',
  490. plugins: [ Paragraph ]
  491. } ).then( () => {
  492. const editorGetDataError = new Error( 'Some error' );
  493. const getDataStub = sinon.stub( watchdog.editor.data, 'get' )
  494. .throwsException( editorGetDataError );
  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( res );
  517. } );
  518. } );
  519. } );
  520. } );
  521. } );
  522. describe( 'static for()', () => {
  523. it( 'should be a shortcut method for creating the watchdog', () => {
  524. const watchdog = Watchdog.for( ClassicTestEditor );
  525. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  526. const originalErrorHandler = window.onerror;
  527. window.onerror = undefined;
  528. return watchdog.create( element, {
  529. initialData: '<p>foo</p>',
  530. plugins: [ Paragraph ]
  531. } ).then( () => {
  532. const oldEditor = watchdog.editor;
  533. expect( watchdog.editor ).to.be.an.instanceOf( ClassicTestEditor );
  534. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  535. return new Promise( res => {
  536. watchdog.on( 'restart', () => {
  537. window.onerror = originalErrorHandler;
  538. expect( watchdog.editor ).to.be.an.instanceOf( ClassicTestEditor );
  539. expect( watchdog.editor ).to.not.equal( oldEditor );
  540. expect( watchdog.editor.getData() ).to.equal( '<p>foo</p>' );
  541. watchdog.destroy().then( res );
  542. } );
  543. } );
  544. } );
  545. } );
  546. } );
  547. describe( 'crashes', () => {
  548. it( 'should be an array of caught errors by the watchdog', () => {
  549. const watchdog = new Watchdog();
  550. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  551. watchdog.setDestructor( editor => editor.destroy() );
  552. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  553. const originalErrorHandler = window.onerror;
  554. window.onerror = undefined;
  555. return watchdog.create( element ).then( () => {
  556. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  557. setTimeout( () => throwCKEditorError( 'bar', watchdog.editor ) );
  558. return new Promise( res => {
  559. setTimeout( () => {
  560. window.onerror = originalErrorHandler;
  561. expect( watchdog.crashes[ 0 ].message ).to.equal( 'foo' );
  562. expect( watchdog.crashes[ 1 ].message ).to.equal( 'bar' );
  563. watchdog.destroy().then( res );
  564. } );
  565. } );
  566. } );
  567. } );
  568. } );
  569. } );
  570. function throwCKEditorError( name, context ) {
  571. throw new CKEditorError( name, context );
  572. }