contextwatchdog.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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 document, setTimeout, window */
  6. import ContextWatchdog from '../src/contextwatchdog';
  7. import ClassicTestEditor from '@ckeditor/ckeditor5-core/tests/_utils/classictesteditor';
  8. import Context from '@ckeditor/ckeditor5-core/src/context';
  9. import sinon from 'sinon';
  10. import { expect } from 'chai';
  11. import CKEditorError from '@ckeditor/ckeditor5-utils/src/ckeditorerror';
  12. describe( 'ContextWatchdog', () => {
  13. let element1, element2;
  14. let mainWatchdog;
  15. let originalErrorHandler;
  16. beforeEach( () => {
  17. element1 = document.createElement( 'div' );
  18. element2 = document.createElement( 'div' );
  19. document.body.appendChild( element1 );
  20. document.body.appendChild( element2 );
  21. originalErrorHandler = window.onerror;
  22. window.onerror = sinon.spy();
  23. } );
  24. afterEach( () => {
  25. window.onerror = originalErrorHandler;
  26. element1.remove();
  27. element2.remove();
  28. sinon.restore();
  29. } );
  30. it( 'should disable adding items once the Watchdog is destroyed', async () => {
  31. mainWatchdog = ContextWatchdog.for( Context, {} );
  32. await mainWatchdog.destroy();
  33. let err;
  34. try {
  35. await mainWatchdog.add( {
  36. editor2: {
  37. type: 'editor',
  38. creator: ( el, config ) => ClassicTestEditor.create( el, config ),
  39. sourceElementOrData: element1,
  40. config: {}
  41. },
  42. } );
  43. } catch ( _err ) {
  44. err = _err;
  45. }
  46. expect( err ).to.be.instanceOf( Error );
  47. expect( err.message ).to.match( /Cannot add items to destroyed watchdog\./ );
  48. } );
  49. it.skip( 'case: editor, contextItem', async () => {
  50. mainWatchdog = ContextWatchdog.for( Context, {} );
  51. mainWatchdog.add( {
  52. editor1: {
  53. type: 'editor',
  54. creator: ( el, config ) => ClassicTestEditor.create( el, config ),
  55. sourceElementOrData: element1,
  56. config: {}
  57. },
  58. annotatedInput: {
  59. type: 'contextItem',
  60. creator: () => { }
  61. }
  62. } );
  63. await mainWatchdog.waitForReady();
  64. await mainWatchdog.destroy();
  65. } );
  66. describe( 'for scenario with no items', () => {
  67. it( 'should create only context', async () => {
  68. mainWatchdog = ContextWatchdog.for( Context, {} );
  69. await mainWatchdog.waitForReady();
  70. expect( mainWatchdog.context ).to.be.instanceOf( Context );
  71. await mainWatchdog.destroy();
  72. } );
  73. it( 'should have proper states', async () => {
  74. mainWatchdog = ContextWatchdog.for( Context, {} );
  75. expect( mainWatchdog.state ).to.equal( 'initializing' );
  76. await mainWatchdog.waitForReady();
  77. expect( mainWatchdog.state ).to.equal( 'ready' );
  78. await mainWatchdog.destroy();
  79. expect( mainWatchdog.state ).to.equal( 'destroyed' );
  80. } );
  81. it( 'should set custom destructor if provided', async () => {
  82. const mainWatchdog = new ContextWatchdog();
  83. const customDestructor = sinon.spy( context => context.destroy() );
  84. mainWatchdog.setCreator( config => Context.create( config ) );
  85. mainWatchdog.setDestructor( customDestructor );
  86. mainWatchdog._create();
  87. await mainWatchdog.destroy();
  88. sinon.assert.calledOnce( customDestructor );
  89. } );
  90. describe( 'in case of error handling', () => {
  91. it( 'should restart the `Context`', async () => {
  92. mainWatchdog = ContextWatchdog.for( Context, {} );
  93. const errorSpy = sinon.spy();
  94. await mainWatchdog.waitForReady();
  95. const oldContext = mainWatchdog.context;
  96. mainWatchdog.on( 'restart', errorSpy );
  97. setTimeout( () => throwCKEditorError( 'foo', mainWatchdog.context ) );
  98. await waitCycle();
  99. sinon.assert.calledOnce( errorSpy );
  100. expect( mainWatchdog.context ).to.not.equal( oldContext );
  101. } );
  102. } );
  103. } );
  104. describe( 'for multiple items scenario', () => {
  105. it( 'should allow adding multiple items without waiting for promises', async () => {
  106. mainWatchdog = ContextWatchdog.for( Context, {} );
  107. mainWatchdog.add( {
  108. editor1: {
  109. type: 'editor',
  110. creator: ( el, config ) => ClassicTestEditor.create( el, config ),
  111. sourceElementOrData: element1,
  112. config: {}
  113. },
  114. } );
  115. mainWatchdog.add( {
  116. editor2: {
  117. type: 'editor',
  118. creator: ( el, config ) => ClassicTestEditor.create( el, config ),
  119. sourceElementOrData: element2,
  120. config: {}
  121. },
  122. } );
  123. await mainWatchdog.waitForReady();
  124. await mainWatchdog.destroy();
  125. } );
  126. it( 'should throw when multiple items with the same name are added', async () => {
  127. mainWatchdog = ContextWatchdog.for( Context, {} );
  128. mainWatchdog.add( {
  129. editor1: {
  130. type: 'editor',
  131. creator: ( el, config ) => ClassicTestEditor.create( el, config ),
  132. sourceElementOrData: element1,
  133. config: {}
  134. },
  135. } );
  136. await mainWatchdog.waitForReady();
  137. let err;
  138. try {
  139. await mainWatchdog.add( {
  140. editor1: {
  141. type: 'editor',
  142. creator: ( el, config ) => ClassicTestEditor.create( el, config ),
  143. sourceElementOrData: element1,
  144. config: {}
  145. },
  146. } );
  147. } catch ( _err ) {
  148. err = _err;
  149. }
  150. mainWatchdog._actionQueue.clear();
  151. await mainWatchdog.destroy();
  152. expect( err ).to.be.instanceOf( Error );
  153. expect( err.message ).to.match( /Watchdog with the given name is already added: 'editor1'./ );
  154. } );
  155. it( 'should throw when not added item is removed', async () => {
  156. mainWatchdog = ContextWatchdog.for( Context, {} );
  157. await mainWatchdog.waitForReady();
  158. let err;
  159. try {
  160. await mainWatchdog.remove( [ 'foo' ] );
  161. } catch ( _err ) {
  162. err = _err;
  163. }
  164. mainWatchdog._actionQueue.clear();
  165. await mainWatchdog.destroy();
  166. expect( err ).to.be.instanceOf( Error );
  167. expect( err.message ).to.match( /There is no watchdog named: 'foo'\./ );
  168. } );
  169. it( 'should throw when the item is added before the context is created', async () => {
  170. const mainWatchdog = new ContextWatchdog( {}, {} );
  171. let err;
  172. try {
  173. await mainWatchdog.add( {} );
  174. } catch ( _err ) {
  175. err = _err;
  176. }
  177. expect( err ).to.be.instanceOf( Error );
  178. expect( err.message ).to.match(
  179. /Context was not created yet\. You should call the `ContextWatchdog#create\(\)` method first\./
  180. );
  181. } );
  182. it( 'should allow setting editor custom destructors', async () => {
  183. mainWatchdog = ContextWatchdog.for( Context, {} );
  184. const destructorSpy = sinon.spy( editor => editor.destroy() );
  185. mainWatchdog.add( {
  186. editor1: {
  187. type: 'editor',
  188. creator: ( el, config ) => ClassicTestEditor.create( el, config ),
  189. destructor: destructorSpy,
  190. sourceElementOrData: element1,
  191. config: {},
  192. },
  193. } );
  194. await mainWatchdog.destroy();
  195. sinon.assert.calledOnce( destructorSpy );
  196. } );
  197. it( 'should throw when the item is of not known type', async () => {
  198. mainWatchdog = ContextWatchdog.for( Context, {} );
  199. await mainWatchdog.waitForReady();
  200. let err;
  201. try {
  202. await mainWatchdog.add( {
  203. editor1: {
  204. type: 'foo',
  205. creator: ( el, config ) => ClassicTestEditor.create( el, config ),
  206. sourceElementOrData: element1,
  207. config: {}
  208. },
  209. } );
  210. } catch ( _err ) {
  211. mainWatchdog._stopErrorHandling();
  212. err = _err;
  213. }
  214. mainWatchdog._actionQueue.clear();
  215. await mainWatchdog.destroy();
  216. expect( err ).to.be.instanceOf( Error );
  217. expect( err.message ).to.match( /Not supported item type: 'foo'\./ );
  218. } );
  219. it( 'should allow adding and removing items without waiting', async () => {
  220. mainWatchdog = ContextWatchdog.for( Context, {} );
  221. mainWatchdog.add( {
  222. editor1: {
  223. type: 'editor',
  224. creator: ( el, config ) => ClassicTestEditor.create( el, config ),
  225. sourceElementOrData: element1,
  226. config: {}
  227. },
  228. } );
  229. mainWatchdog.add( {
  230. editor2: {
  231. type: 'editor',
  232. creator: ( el, config ) => ClassicTestEditor.create( el, config ),
  233. sourceElementOrData: element2,
  234. config: {}
  235. },
  236. } );
  237. await mainWatchdog.waitForReady();
  238. expect( mainWatchdog.state ).to.equal( 'ready' );
  239. mainWatchdog.remove( [ 'editor1' ] );
  240. await mainWatchdog.waitForReady();
  241. mainWatchdog.remove( [ 'editor2' ] );
  242. await mainWatchdog.waitForReady();
  243. await mainWatchdog.destroy();
  244. } );
  245. it( 'should not change the input items', async () => {
  246. mainWatchdog = ContextWatchdog.for( Context, {} );
  247. mainWatchdog.add( Object.freeze( {
  248. editor1: {
  249. type: 'editor',
  250. creator: ( el, config ) => ClassicTestEditor.create( el, config ),
  251. sourceElementOrData: element1,
  252. config: {}
  253. }
  254. } ) );
  255. mainWatchdog._restart();
  256. await mainWatchdog.waitForReady();
  257. await mainWatchdog.destroy();
  258. } );
  259. describe( 'in case of error handling', () => {
  260. it( 'should restart the whole structure of editors if an error happens inside the `Context`', async () => {
  261. mainWatchdog = ContextWatchdog.for( Context, {} );
  262. mainWatchdog.add( {
  263. editor1: {
  264. type: 'editor',
  265. creator: ( el, config ) => ClassicTestEditor.create( el, config ),
  266. sourceElementOrData: element1,
  267. config: {}
  268. }
  269. } );
  270. await mainWatchdog.waitForReady();
  271. const oldContext = mainWatchdog.context;
  272. const restartSpy = sinon.spy();
  273. mainWatchdog.on( 'restart', restartSpy );
  274. setTimeout( () => throwCKEditorError( 'foo', mainWatchdog.context ) );
  275. await waitCycle();
  276. sinon.assert.calledOnce( restartSpy );
  277. expect( mainWatchdog._watchdogs.get( 'editor1' ).state ).to.equal( 'ready' );
  278. expect( mainWatchdog.context ).to.not.equal( oldContext );
  279. await mainWatchdog.destroy();
  280. } );
  281. it( 'should restart only the editor if an error happens inside the editor', async () => {
  282. mainWatchdog = ContextWatchdog.for( Context, {} );
  283. mainWatchdog.add( {
  284. editor1: {
  285. type: 'editor',
  286. creator: ( el, config ) => ClassicTestEditor.create( el, config ),
  287. sourceElementOrData: element1,
  288. config: {}
  289. }
  290. } );
  291. await mainWatchdog.waitForReady();
  292. const oldContext = mainWatchdog.context;
  293. const restartSpy = sinon.spy();
  294. const oldEditor = mainWatchdog.getWatchdog( 'editor1' ).editor;
  295. mainWatchdog.on( 'restart', restartSpy );
  296. setTimeout( () => throwCKEditorError( 'foo', oldEditor ) );
  297. await waitCycle();
  298. sinon.assert.notCalled( restartSpy );
  299. expect( mainWatchdog.context ).to.equal( oldContext );
  300. expect( mainWatchdog.getWatchdog( 'editor1' ).editor ).to.not.equal( oldEditor );
  301. expect( mainWatchdog.getWatchdog( 'editor1' ).state ).to.equal( 'ready' );
  302. await mainWatchdog.destroy();
  303. } );
  304. it( 'should restart only the editor if an error happens inside one of the editors', async () => {
  305. mainWatchdog = ContextWatchdog.for( Context, {} );
  306. mainWatchdog.add( {
  307. editor1: {
  308. type: 'editor',
  309. creator: ( el, config ) => ClassicTestEditor.create( el, config ),
  310. sourceElementOrData: element1,
  311. config: {}
  312. },
  313. editor2: {
  314. type: 'editor',
  315. creator: ( el, config ) => ClassicTestEditor.create( el, config ),
  316. sourceElementOrData: element1,
  317. config: {}
  318. }
  319. } );
  320. await mainWatchdog.waitForReady();
  321. const oldContext = mainWatchdog.context;
  322. const editorWatchdog1 = mainWatchdog.getWatchdog( 'editor1' );
  323. const editorWatchdog2 = mainWatchdog.getWatchdog( 'editor2' );
  324. const oldEditor1 = editorWatchdog1.editor;
  325. const oldEditor2 = editorWatchdog2.editor;
  326. const mainWatchdogRestartSpy = sinon.spy();
  327. const editorWatchdog1RestartSpy = sinon.spy();
  328. const editorWatchdog2RestartSpy = sinon.spy();
  329. mainWatchdog.on( 'restart', mainWatchdogRestartSpy );
  330. editorWatchdog1.on( 'restart', editorWatchdog1RestartSpy );
  331. editorWatchdog2.on( 'restart', editorWatchdog2RestartSpy );
  332. setTimeout( () => throwCKEditorError( 'foo', editorWatchdog1.editor ) );
  333. await waitCycle();
  334. sinon.assert.calledOnce( editorWatchdog1RestartSpy );
  335. sinon.assert.notCalled( mainWatchdogRestartSpy );
  336. sinon.assert.notCalled( editorWatchdog2RestartSpy );
  337. expect( editorWatchdog1.state ).to.equal( 'ready' );
  338. expect( editorWatchdog2.state ).to.equal( 'ready' );
  339. expect( mainWatchdog.state ).to.equal( 'ready' );
  340. expect( oldEditor1 ).to.not.equal( editorWatchdog1.editor );
  341. expect( oldEditor2 ).to.equal( editorWatchdog2.editor );
  342. expect( mainWatchdog.context ).to.equal( oldContext );
  343. await mainWatchdog.destroy();
  344. } );
  345. } );
  346. } );
  347. } );
  348. function throwCKEditorError( name, context ) {
  349. throw new CKEditorError( name, context );
  350. }
  351. function waitCycle() {
  352. return new Promise( res => setTimeout( res ) );
  353. }