watchdog.js 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880
  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. it( 'should support editor data passed as the first argument', () => {
  71. const watchdog = new Watchdog();
  72. watchdog.setCreator( ( data, config ) => ClassicTestEditor.create( data, config ) );
  73. watchdog.setDestructor( editor => editor.destroy() );
  74. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  75. const originalErrorHandler = window.onerror;
  76. const windowErrorSpy = sinon.spy();
  77. window.onerror = windowErrorSpy;
  78. return watchdog.create( '<p>foo</p>', { plugins: [ Paragraph ] } )
  79. .then( () => {
  80. expect( watchdog.editor.getData() ).to.equal( '<p>foo</p>' );
  81. return new Promise( res => {
  82. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  83. watchdog.on( 'restart', () => {
  84. window.onerror = originalErrorHandler;
  85. res();
  86. } );
  87. } );
  88. } )
  89. .then( () => {
  90. expect( watchdog.editor.getData() ).to.equal( '<p>foo</p>' );
  91. return watchdog.destroy();
  92. } );
  93. } );
  94. } );
  95. describe( 'editor', () => {
  96. it( 'should be the current editor instance', () => {
  97. const watchdog = Watchdog.for( ClassicTestEditor );
  98. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  99. const originalErrorHandler = window.onerror;
  100. const windowErrorSpy = sinon.spy();
  101. window.onerror = windowErrorSpy;
  102. expect( watchdog.editor ).to.be.null;
  103. let oldEditor;
  104. return watchdog.create( element, {} )
  105. .then( () => {
  106. oldEditor = watchdog.editor;
  107. expect( watchdog.editor ).to.be.instanceOf( ClassicTestEditor );
  108. return new Promise( res => {
  109. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  110. watchdog.on( 'restart', () => {
  111. window.onerror = originalErrorHandler;
  112. res();
  113. } );
  114. } );
  115. } )
  116. .then( () => {
  117. expect( watchdog.editor ).to.be.instanceOf( ClassicTestEditor );
  118. expect( watchdog.editor ).to.not.equal( oldEditor );
  119. return watchdog.destroy();
  120. } )
  121. .then( () => {
  122. expect( watchdog.editor ).to.be.null;
  123. } );
  124. } );
  125. } );
  126. describe( 'error handling', () => {
  127. it( 'Watchdog should not restart editor during the initialization', () => {
  128. const watchdog = new Watchdog();
  129. watchdog.setCreator( el =>
  130. ClassicTestEditor.create( el )
  131. .then( () => Promise.reject( new Error( 'foo' ) ) )
  132. );
  133. watchdog.setDestructor( editor => editor.destroy() );
  134. return watchdog.create( element ).then(
  135. () => { throw new Error( '`watchdog.create()` should throw an error.' ); },
  136. err => {
  137. expect( err ).to.be.instanceOf( Error );
  138. expect( err.message ).to.equal( 'foo' );
  139. }
  140. );
  141. } );
  142. it( 'Watchdog should not restart editor during the destroy', () => {
  143. const watchdog = new Watchdog();
  144. watchdog.setCreator( el => ClassicTestEditor.create( el ) );
  145. watchdog.setDestructor( () => Promise.reject( new Error( 'foo' ) ) );
  146. return Promise.resolve()
  147. .then( () => watchdog.create( element ) )
  148. .then( () => watchdog.destroy() )
  149. .then(
  150. () => { throw new Error( '`watchdog.create()` should throw an error.' ); },
  151. err => {
  152. expect( err ).to.be.instanceOf( Error );
  153. expect( err.message ).to.equal( 'foo' );
  154. }
  155. );
  156. } );
  157. it( 'Watchdog should not hide intercepted errors', () => {
  158. const watchdog = new Watchdog();
  159. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  160. watchdog.setDestructor( editor => editor.destroy() );
  161. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  162. const originalErrorHandler = window.onerror;
  163. const windowErrorSpy = sinon.spy();
  164. window.onerror = windowErrorSpy;
  165. return watchdog.create( element ).then( () => {
  166. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  167. return new Promise( res => {
  168. watchdog.on( 'restart', () => {
  169. window.onerror = originalErrorHandler;
  170. sinon.assert.calledOnce( windowErrorSpy );
  171. // Various browsers will display the error slightly differently.
  172. expect( windowErrorSpy.getCall( 0 ).args[ 0 ] ).to.match( /foo/ );
  173. watchdog.destroy().then( res );
  174. } );
  175. } );
  176. } );
  177. } );
  178. it( 'Watchdog should intercept editor errors and restart the editor during the runtime', () => {
  179. const watchdog = new Watchdog();
  180. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  181. watchdog.setDestructor( editor => editor.destroy() );
  182. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  183. const originalErrorHandler = window.onerror;
  184. window.onerror = undefined;
  185. return watchdog.create( element ).then( () => {
  186. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  187. return new Promise( res => {
  188. watchdog.on( 'restart', () => {
  189. window.onerror = originalErrorHandler;
  190. watchdog.destroy().then( res );
  191. } );
  192. } );
  193. } );
  194. } );
  195. it( 'Watchdog should not intercept non-editor errors', () => {
  196. const watchdog = new Watchdog();
  197. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  198. watchdog.setDestructor( editor => editor.destroy() );
  199. const editorErrorSpy = sinon.spy();
  200. watchdog.on( 'error', editorErrorSpy );
  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( () => {
  206. throw new Error( 'foo' );
  207. } );
  208. return new Promise( res => {
  209. setTimeout( () => {
  210. window.onerror = originalErrorHandler;
  211. sinon.assert.notCalled( editorErrorSpy );
  212. watchdog.destroy().then( res );
  213. } );
  214. } );
  215. } );
  216. } );
  217. it( 'Watchdog should not intercept other editor errors', () => {
  218. const watchdog1 = Watchdog.for( ClassicTestEditor );
  219. const watchdog2 = Watchdog.for( ClassicTestEditor );
  220. const config = {
  221. plugins: []
  222. };
  223. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  224. const originalErrorHandler = window.onerror;
  225. window.onerror = undefined;
  226. return Promise.all( [
  227. watchdog1.create( element, config ),
  228. watchdog2.create( element, config )
  229. ] ).then( () => {
  230. return new Promise( res => {
  231. const watchdog1ErrorSpy = sinon.spy();
  232. const watchdog2ErrorSpy = sinon.spy();
  233. watchdog1.on( 'restart', watchdog1ErrorSpy );
  234. watchdog2.on( 'restart', watchdog2ErrorSpy );
  235. setTimeout( () => throwCKEditorError( 'foo', watchdog2.editor ) );
  236. setTimeout( () => {
  237. window.onerror = originalErrorHandler;
  238. sinon.assert.notCalled( watchdog1ErrorSpy );
  239. sinon.assert.calledOnce( watchdog2ErrorSpy );
  240. Promise.all( [ watchdog1.destroy(), watchdog2.destroy() ] )
  241. .then( res );
  242. } );
  243. } );
  244. } );
  245. } );
  246. it( 'Watchdog should intercept editor errors and restart the editor if the editor can be found from the context', () => {
  247. const watchdog = new Watchdog();
  248. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  249. watchdog.setDestructor( editor => editor.destroy() );
  250. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  251. const originalErrorHandler = window.onerror;
  252. window.onerror = undefined;
  253. return watchdog.create( element ).then( () => {
  254. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor.model.document ) );
  255. return new Promise( res => {
  256. watchdog.on( 'restart', () => {
  257. window.onerror = originalErrorHandler;
  258. watchdog.destroy().then( res );
  259. } );
  260. } );
  261. } );
  262. } );
  263. it( 'Watchdog should intercept editor errors and restart the editor if the editor can be found from the context #2', () => {
  264. const watchdog = new Watchdog();
  265. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  266. watchdog.setDestructor( editor => editor.destroy() );
  267. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  268. const originalErrorHandler = window.onerror;
  269. window.onerror = undefined;
  270. return watchdog.create( element ).then( () => {
  271. setTimeout( () => throwCKEditorError( 'foo', {
  272. foo: [ 1, 2, 3, {
  273. bar: new Set( [
  274. new Map( [
  275. [ 'foo', 'bar' ],
  276. [ 0, watchdog.editor ]
  277. ] )
  278. ] )
  279. } ]
  280. } ) );
  281. return new Promise( res => {
  282. watchdog.on( 'restart', () => {
  283. window.onerror = originalErrorHandler;
  284. watchdog.destroy().then( res );
  285. } );
  286. } );
  287. } );
  288. } );
  289. it( 'Watchdog should crash permanently if the `crashNumberLimit` is reached' +
  290. ' and the average time between errors is lower than `minimumNonErrorTimePeriod` (default values)', () => {
  291. const watchdog = new Watchdog();
  292. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  293. watchdog.setDestructor( editor => editor.destroy() );
  294. const errorSpy = sinon.spy();
  295. watchdog.on( 'error', errorSpy );
  296. const restartSpy = sinon.spy();
  297. watchdog.on( 'restart', restartSpy );
  298. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  299. const originalErrorHandler = window.onerror;
  300. window.onerror = undefined;
  301. return watchdog.create( element ).then( () => {
  302. setTimeout( () => throwCKEditorError( 'foo1', watchdog.editor ) );
  303. setTimeout( () => throwCKEditorError( 'foo2', watchdog.editor ) );
  304. setTimeout( () => throwCKEditorError( 'foo3', watchdog.editor ) );
  305. setTimeout( () => throwCKEditorError( 'foo4', watchdog.editor ) );
  306. return new Promise( res => {
  307. setTimeout( () => {
  308. expect( errorSpy.callCount ).to.equal( 4 );
  309. expect( watchdog.crashes.length ).to.equal( 4 );
  310. expect( restartSpy.callCount ).to.equal( 3 );
  311. window.onerror = originalErrorHandler;
  312. watchdog.destroy().then( res );
  313. } );
  314. } );
  315. } );
  316. } );
  317. it( 'Watchdog should crash permanently if the `crashNumberLimit` is reached' +
  318. ' and the average time between errors is lower than `minimumNonErrorTimePeriod` (custom values)', () => {
  319. const watchdog = new Watchdog( { crashNumberLimit: 2, minimumNonErrorTimePeriod: 1000 } );
  320. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  321. watchdog.setDestructor( editor => editor.destroy() );
  322. const errorSpy = sinon.spy();
  323. watchdog.on( 'error', errorSpy );
  324. const restartSpy = sinon.spy();
  325. watchdog.on( 'restart', restartSpy );
  326. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  327. const originalErrorHandler = window.onerror;
  328. window.onerror = undefined;
  329. return watchdog.create( element ).then( () => {
  330. setTimeout( () => throwCKEditorError( 'foo1', watchdog.editor ) );
  331. setTimeout( () => throwCKEditorError( 'foo2', watchdog.editor ) );
  332. setTimeout( () => throwCKEditorError( 'foo3', watchdog.editor ) );
  333. setTimeout( () => throwCKEditorError( 'foo4', watchdog.editor ) );
  334. return new Promise( res => {
  335. setTimeout( () => {
  336. expect( errorSpy.callCount ).to.equal( 3 );
  337. expect( watchdog.crashes.length ).to.equal( 3 );
  338. expect( restartSpy.callCount ).to.equal( 2 );
  339. window.onerror = originalErrorHandler;
  340. watchdog.destroy().then( res );
  341. } );
  342. } );
  343. } );
  344. } );
  345. it( 'Watchdog should not crash permantently when average time between errors is longer than `minimumNonErrorTimePeriod`', () => {
  346. const watchdog = new Watchdog( { crashNumberLimit: 2, minimumNonErrorTimePeriod: 0 } );
  347. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  348. watchdog.setDestructor( editor => editor.destroy() );
  349. const errorSpy = sinon.spy();
  350. watchdog.on( 'error', errorSpy );
  351. const restartSpy = sinon.spy();
  352. watchdog.on( 'restart', restartSpy );
  353. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  354. const originalErrorHandler = window.onerror;
  355. window.onerror = undefined;
  356. return watchdog.create( element ).then( () => {
  357. setTimeout( () => throwCKEditorError( 'foo1', watchdog.editor ), 5 );
  358. setTimeout( () => throwCKEditorError( 'foo2', watchdog.editor ), 10 );
  359. setTimeout( () => throwCKEditorError( 'foo3', watchdog.editor ), 15 );
  360. setTimeout( () => throwCKEditorError( 'foo4', watchdog.editor ), 20 );
  361. return new Promise( res => {
  362. setTimeout( () => {
  363. expect( errorSpy.callCount ).to.equal( 4 );
  364. expect( watchdog.crashes.length ).to.equal( 4 );
  365. expect( restartSpy.callCount ).to.equal( 4 );
  366. window.onerror = originalErrorHandler;
  367. watchdog.destroy().then( res );
  368. }, 20 );
  369. } );
  370. } );
  371. } );
  372. it( 'Watchdog should warn if the CKEditorError missing its context', () => {
  373. const watchdog = new Watchdog();
  374. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  375. watchdog.setDestructor( editor => editor.destroy() );
  376. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  377. const originalErrorHandler = window.onerror;
  378. window.onerror = undefined;
  379. sinon.stub( console, 'error' );
  380. return watchdog.create( element ).then( () => {
  381. setTimeout( () => throwCKEditorError( 'foo' ) );
  382. return new Promise( res => {
  383. setTimeout( () => {
  384. window.onerror = originalErrorHandler;
  385. expect( watchdog.crashes ).to.deep.equal( [] );
  386. sinon.assert.calledWithExactly(
  387. console.error,
  388. 'The error is missing its context and Watchdog cannot restart the proper editor.'
  389. );
  390. watchdog.destroy().then( res );
  391. } );
  392. } );
  393. } );
  394. } );
  395. it( 'Watchdog should omit error if the CKEditorError context is equal to null', () => {
  396. const watchdog = new Watchdog();
  397. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  398. watchdog.setDestructor( editor => editor.destroy() );
  399. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  400. const originalErrorHandler = window.onerror;
  401. window.onerror = undefined;
  402. sinon.stub( console, 'error' );
  403. return watchdog.create( element ).then( () => {
  404. setTimeout( () => throwCKEditorError( 'foo', null ) );
  405. return new Promise( res => {
  406. setTimeout( () => {
  407. window.onerror = originalErrorHandler;
  408. expect( watchdog.crashes ).to.deep.equal( [] );
  409. sinon.assert.notCalled( console.error );
  410. watchdog.destroy().then( res );
  411. } );
  412. } );
  413. } );
  414. } );
  415. it( 'editor should be restarted with the data before the crash #1', () => {
  416. const watchdog = new Watchdog();
  417. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  418. watchdog.setDestructor( editor => editor.destroy() );
  419. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  420. const originalErrorHandler = window.onerror;
  421. window.onerror = undefined;
  422. return watchdog.create( element, {
  423. initialData: '<p>foo</p>',
  424. plugins: [ Paragraph ]
  425. } ).then( () => {
  426. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  427. return new Promise( res => {
  428. watchdog.on( 'restart', () => {
  429. window.onerror = originalErrorHandler;
  430. expect( watchdog.editor.getData() ).to.equal( '<p>foo</p>' );
  431. watchdog.destroy().then( res );
  432. } );
  433. } );
  434. } );
  435. } );
  436. it( 'editor should be restarted with the data before the crash #2', () => {
  437. const watchdog = new Watchdog();
  438. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  439. watchdog.setDestructor( editor => editor.destroy() );
  440. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  441. const originalErrorHandler = window.onerror;
  442. window.onerror = undefined;
  443. return watchdog.create( element, {
  444. initialData: '<p>foo</p>',
  445. plugins: [ Paragraph ]
  446. } ).then( () => {
  447. const doc = watchdog.editor.model.document;
  448. watchdog.editor.model.change( writer => {
  449. writer.insertText( 'bar', writer.createPositionAt( doc.getRoot(), 1 ) );
  450. } );
  451. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  452. return new Promise( res => {
  453. watchdog.on( 'restart', () => {
  454. window.onerror = originalErrorHandler;
  455. expect( watchdog.editor.getData() ).to.equal( '<p>foo</p><p>bar</p>' );
  456. watchdog.destroy().then( res );
  457. } );
  458. } );
  459. } );
  460. } );
  461. it( 'editor should be restarted with the data of the latest document version before the crash', () => {
  462. const watchdog = new Watchdog();
  463. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  464. watchdog.setDestructor( editor => editor.destroy() );
  465. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  466. const originalErrorHandler = window.onerror;
  467. window.onerror = undefined;
  468. return watchdog.create( element, {
  469. initialData: '<p>foo</p>',
  470. plugins: [ Paragraph ]
  471. } ).then( () => {
  472. const model = watchdog.editor.model;
  473. const doc = model.document;
  474. // Decrement the document version to simulate a situation when an operation
  475. // don't produce new document version.
  476. doc.version--;
  477. model.change( writer => {
  478. writer.insertText( 'bar', writer.createPositionAt( doc.getRoot(), 1 ) );
  479. } );
  480. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  481. return new Promise( res => {
  482. watchdog.on( 'restart', () => {
  483. window.onerror = originalErrorHandler;
  484. expect( watchdog.editor.getData() ).to.equal( '<p>foo</p>' );
  485. watchdog.destroy().then( res );
  486. } );
  487. } );
  488. } );
  489. } );
  490. it( 'editor should be restarted with the latest available data before the crash', () => {
  491. const watchdog = new Watchdog();
  492. watchdog.setCreator( ( el, config ) => ClassicTestEditor.create( el, config ) );
  493. watchdog.setDestructor( editor => editor.destroy() );
  494. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  495. const originalErrorHandler = window.onerror;
  496. window.onerror = undefined;
  497. sinon.stub( console, 'error' );
  498. return watchdog.create( element, {
  499. initialData: '<p>foo</p>',
  500. plugins: [ Paragraph ]
  501. } ).then( () => {
  502. const editorGetDataError = new Error( 'Some error' );
  503. const getDataStub = sinon.stub( watchdog.editor.data, 'get' )
  504. .throwsException( editorGetDataError );
  505. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  506. return new Promise( res => {
  507. const doc = watchdog.editor.model.document;
  508. watchdog.editor.model.change( writer => {
  509. writer.insertText( 'bar', writer.createPositionAt( doc.getRoot(), 1 ) );
  510. } );
  511. watchdog.on( 'restart', () => {
  512. window.onerror = originalErrorHandler;
  513. // It is called second time by during the default editor destruction
  514. // to update the source element.
  515. sinon.assert.calledTwice( getDataStub );
  516. expect( watchdog.editor.getData() ).to.equal( '<p>foo</p>' );
  517. sinon.assert.calledWith(
  518. console.error,
  519. editorGetDataError,
  520. 'An error happened during restoring editor data. Editor will be restored from the previously saved data.'
  521. );
  522. sinon.assert.calledWith(
  523. console.error,
  524. 'An error happened during the editor destructing.'
  525. );
  526. watchdog.destroy().then( res );
  527. } );
  528. } );
  529. } );
  530. } );
  531. } );
  532. describe( 'destroy()', () => {
  533. // See #19.
  534. it( 'should clean internal stuff', () => {
  535. // 30ms should be enough to make the two data changes split into two data save actions.
  536. // This will ensure that the second data save action will be put off in time.
  537. const SAVE_INTERVAL = 30;
  538. const watchdog = Watchdog.for( ClassicTestEditor, {
  539. saveInterval: SAVE_INTERVAL,
  540. } );
  541. return watchdog.create( element, {
  542. initialData: '<p>foo</p>',
  543. plugins: [ Paragraph ]
  544. } ).then( () => {
  545. const doc = watchdog.editor.model.document;
  546. watchdog.editor.model.change( writer => {
  547. writer.insertText( 'bar', writer.createPositionAt( doc.getRoot(), 1 ) );
  548. } );
  549. watchdog.editor.model.change( writer => {
  550. writer.insertText( 'foo', writer.createPositionAt( doc.getRoot(), 1 ) );
  551. } );
  552. } ).then( () => {
  553. return watchdog.destroy();
  554. } ).then( () => {
  555. // Wait to ensure that the throttled save is cleared and won't be executed
  556. // on the non-existing editor.
  557. return new Promise( res => setTimeout( res, SAVE_INTERVAL ) );
  558. } ).then( () => {
  559. expect( watchdog.editor ).to.equal( null );
  560. expect( watchdog.state ).to.equal( 'destroyed' );
  561. expect( watchdog.crashes ).to.deep.equal( [] );
  562. } );
  563. } );
  564. } );
  565. describe( 'static for()', () => {
  566. it( 'should be a shortcut method for creating the watchdog', () => {
  567. const watchdog = Watchdog.for( ClassicTestEditor );
  568. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  569. const originalErrorHandler = window.onerror;
  570. window.onerror = undefined;
  571. return watchdog.create( element, {
  572. initialData: '<p>foo</p>',
  573. plugins: [ Paragraph ]
  574. } ).then( () => {
  575. const oldEditor = watchdog.editor;
  576. expect( watchdog.editor ).to.be.an.instanceOf( ClassicTestEditor );
  577. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  578. return new Promise( res => {
  579. watchdog.on( 'restart', () => {
  580. window.onerror = originalErrorHandler;
  581. expect( watchdog.editor ).to.be.an.instanceOf( ClassicTestEditor );
  582. expect( watchdog.editor ).to.not.equal( oldEditor );
  583. expect( watchdog.editor.getData() ).to.equal( '<p>foo</p>' );
  584. watchdog.destroy().then( res );
  585. } );
  586. } );
  587. } );
  588. } );
  589. } );
  590. describe( 'crashes', () => {
  591. it( 'should be an array of caught errors by the watchdog', () => {
  592. const watchdog = Watchdog.for( ClassicTestEditor );
  593. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  594. const originalErrorHandler = window.onerror;
  595. window.onerror = undefined;
  596. return watchdog.create( element ).then( () => {
  597. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  598. setTimeout( () => throwCKEditorError( 'bar', watchdog.editor ) );
  599. return new Promise( res => {
  600. setTimeout( () => {
  601. window.onerror = originalErrorHandler;
  602. expect( watchdog.crashes[ 0 ].message ).to.equal( 'foo' );
  603. expect( watchdog.crashes[ 1 ].message ).to.equal( 'bar' );
  604. watchdog.destroy().then( res );
  605. } );
  606. } );
  607. } );
  608. } );
  609. } );
  610. describe( 'state', () => {
  611. it( 'should reflect the state of the watchdog', () => {
  612. const watchdog = Watchdog.for( ClassicTestEditor );
  613. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  614. const originalErrorHandler = window.onerror;
  615. window.onerror = undefined;
  616. expect( watchdog.state ).to.equal( 'initializing' );
  617. return watchdog.create( element ).then( () => {
  618. expect( watchdog.state ).to.equal( 'ready' );
  619. return watchdog.create( element ).then( () => {
  620. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  621. setTimeout( () => throwCKEditorError( 'bar', watchdog.editor ) );
  622. return new Promise( res => {
  623. setTimeout( () => {
  624. window.onerror = originalErrorHandler;
  625. expect( watchdog.state ).to.equal( 'ready' );
  626. watchdog.destroy().then( () => {
  627. expect( watchdog.state ).to.equal( 'destroyed' );
  628. res();
  629. } );
  630. } );
  631. } );
  632. } );
  633. } );
  634. } );
  635. it( 'should be observable', () => {
  636. const watchdog = Watchdog.for( ClassicTestEditor );
  637. const states = [];
  638. watchdog.on( 'change:state', ( evt, propName, newValue ) => {
  639. states.push( newValue );
  640. } );
  641. // sinon.stub( window, 'onerror' ).value( undefined ); and similar do not work.
  642. const originalErrorHandler = window.onerror;
  643. window.onerror = undefined;
  644. return watchdog.create( element ).then( () => {
  645. return watchdog.create( element ).then( () => {
  646. setTimeout( () => throwCKEditorError( 'foo', watchdog.editor ) );
  647. setTimeout( () => throwCKEditorError( 'bar', watchdog.editor ) );
  648. setTimeout( () => throwCKEditorError( 'baz', watchdog.editor ) );
  649. setTimeout( () => throwCKEditorError( 'biz', watchdog.editor ) );
  650. return new Promise( res => {
  651. setTimeout( () => {
  652. window.onerror = originalErrorHandler;
  653. watchdog.destroy().then( () => {
  654. expect( states ).to.deep.equal( [
  655. 'ready',
  656. 'crashed',
  657. 'initializing',
  658. 'ready',
  659. 'crashed',
  660. 'initializing',
  661. 'ready',
  662. 'crashed',
  663. 'initializing',
  664. 'ready',
  665. 'crashed',
  666. 'crashedPermanently',
  667. 'destroyed'
  668. ] );
  669. res();
  670. } );
  671. } );
  672. } );
  673. } );
  674. } );
  675. } );
  676. } );
  677. } );
  678. function throwCKEditorError( name, context ) {
  679. throw new CKEditorError( name, context );
  680. }