contextwatchdog.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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 do 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 allow setting editor custom destructors', async () => {
  170. mainWatchdog = ContextWatchdog.for( Context, {} );
  171. const destructorSpy = sinon.spy( editor => editor.destroy() );
  172. mainWatchdog.add( {
  173. editor1: {
  174. type: 'editor',
  175. creator: ( el, config ) => ClassicTestEditor.create( el, config ),
  176. destructor: destructorSpy,
  177. sourceElementOrData: element1,
  178. config: {},
  179. },
  180. } );
  181. await mainWatchdog.destroy();
  182. sinon.assert.calledOnce( destructorSpy );
  183. } );
  184. it( 'should throw when the item is of not known type', async () => {
  185. mainWatchdog = ContextWatchdog.for( Context, {} );
  186. await mainWatchdog.waitForReady();
  187. let err;
  188. try {
  189. await mainWatchdog.add( {
  190. editor1: {
  191. type: 'foo',
  192. creator: ( el, config ) => ClassicTestEditor.create( el, config ),
  193. sourceElementOrData: element1,
  194. config: {}
  195. },
  196. } );
  197. } catch ( _err ) {
  198. mainWatchdog._stopErrorHandling();
  199. err = _err;
  200. }
  201. mainWatchdog._actionQueue.clear();
  202. await mainWatchdog.destroy();
  203. expect( err ).to.be.instanceOf( Error );
  204. expect( err.message ).to.match( /Not supported item type: 'foo'\./ );
  205. } );
  206. it( 'should allow adding and removing items without waiting', async () => {
  207. mainWatchdog = ContextWatchdog.for( Context, {} );
  208. mainWatchdog.add( {
  209. editor1: {
  210. type: 'editor',
  211. creator: ( el, config ) => ClassicTestEditor.create( el, config ),
  212. sourceElementOrData: element1,
  213. config: {}
  214. },
  215. } );
  216. mainWatchdog.add( {
  217. editor2: {
  218. type: 'editor',
  219. creator: ( el, config ) => ClassicTestEditor.create( el, config ),
  220. sourceElementOrData: element2,
  221. config: {}
  222. },
  223. } );
  224. await mainWatchdog.waitForReady();
  225. expect( mainWatchdog.state ).to.equal( 'ready' );
  226. mainWatchdog.remove( [ 'editor1' ] );
  227. await mainWatchdog.waitForReady();
  228. mainWatchdog.remove( [ 'editor2' ] );
  229. await mainWatchdog.waitForReady();
  230. await mainWatchdog.destroy();
  231. } );
  232. it( 'should not change the input items', async () => {
  233. mainWatchdog = ContextWatchdog.for( Context, {} );
  234. mainWatchdog.add( Object.freeze( {
  235. editor1: {
  236. type: 'editor',
  237. creator: ( el, config ) => ClassicTestEditor.create( el, config ),
  238. sourceElementOrData: element1,
  239. config: {}
  240. }
  241. } ) );
  242. mainWatchdog._restart();
  243. await mainWatchdog.waitForReady();
  244. await mainWatchdog.destroy();
  245. } );
  246. describe( 'in case of error handling', () => {
  247. it( 'should restart the whole structure of editors if an error happens inside the `Context`', async () => {
  248. mainWatchdog = ContextWatchdog.for( Context, {} );
  249. mainWatchdog.add( {
  250. editor1: {
  251. type: 'editor',
  252. creator: ( el, config ) => ClassicTestEditor.create( el, config ),
  253. sourceElementOrData: element1,
  254. config: {}
  255. }
  256. } );
  257. await mainWatchdog.waitForReady();
  258. const oldContext = mainWatchdog.context;
  259. const restartSpy = sinon.spy();
  260. mainWatchdog.on( 'restart', restartSpy );
  261. setTimeout( () => throwCKEditorError( 'foo', mainWatchdog.context ) );
  262. await waitCycle();
  263. sinon.assert.calledOnce( restartSpy );
  264. expect( mainWatchdog._watchdogs.get( 'editor1' ).state ).to.equal( 'ready' );
  265. expect( mainWatchdog.context ).to.not.equal( oldContext );
  266. await mainWatchdog.destroy();
  267. } );
  268. it( 'should restart only the editor if an error happens inside the editor', async () => {
  269. mainWatchdog = ContextWatchdog.for( Context, {} );
  270. mainWatchdog.add( {
  271. editor1: {
  272. type: 'editor',
  273. creator: ( el, config ) => ClassicTestEditor.create( el, config ),
  274. sourceElementOrData: element1,
  275. config: {}
  276. }
  277. } );
  278. await mainWatchdog.waitForReady();
  279. const oldContext = mainWatchdog.context;
  280. const restartSpy = sinon.spy();
  281. const oldEditor = mainWatchdog.getWatchdog( 'editor1' ).editor;
  282. mainWatchdog.on( 'restart', restartSpy );
  283. setTimeout( () => throwCKEditorError( 'foo', oldEditor ) );
  284. await waitCycle();
  285. sinon.assert.notCalled( restartSpy );
  286. expect( mainWatchdog.context ).to.equal( oldContext );
  287. expect( mainWatchdog.getWatchdog( 'editor1' ).editor ).to.not.equal( oldEditor );
  288. expect( mainWatchdog.getWatchdog( 'editor1' ).state ).to.equal( 'ready' );
  289. await mainWatchdog.destroy();
  290. } );
  291. it( 'should restart only the editor if an error happens inside one of the editors', async () => {
  292. mainWatchdog = ContextWatchdog.for( Context, {} );
  293. mainWatchdog.add( {
  294. editor1: {
  295. type: 'editor',
  296. creator: ( el, config ) => ClassicTestEditor.create( el, config ),
  297. sourceElementOrData: element1,
  298. config: {}
  299. },
  300. editor2: {
  301. type: 'editor',
  302. creator: ( el, config ) => ClassicTestEditor.create( el, config ),
  303. sourceElementOrData: element1,
  304. config: {}
  305. }
  306. } );
  307. await mainWatchdog.waitForReady();
  308. const oldContext = mainWatchdog.context;
  309. const editorWatchdog1 = mainWatchdog.getWatchdog( 'editor1' );
  310. const editorWatchdog2 = mainWatchdog.getWatchdog( 'editor2' );
  311. const oldEditor1 = editorWatchdog1.editor;
  312. const oldEditor2 = editorWatchdog2.editor;
  313. const mainWatchdogRestartSpy = sinon.spy();
  314. const editorWatchdog1RestartSpy = sinon.spy();
  315. const editorWatchdog2RestartSpy = sinon.spy();
  316. mainWatchdog.on( 'restart', mainWatchdogRestartSpy );
  317. editorWatchdog1.on( 'restart', editorWatchdog1RestartSpy );
  318. editorWatchdog2.on( 'restart', editorWatchdog2RestartSpy );
  319. setTimeout( () => throwCKEditorError( 'foo', editorWatchdog1.editor ) );
  320. await waitCycle();
  321. sinon.assert.calledOnce( editorWatchdog1RestartSpy );
  322. sinon.assert.notCalled( mainWatchdogRestartSpy );
  323. sinon.assert.notCalled( editorWatchdog2RestartSpy );
  324. expect( editorWatchdog1.state ).to.equal( 'ready' );
  325. expect( editorWatchdog2.state ).to.equal( 'ready' );
  326. expect( mainWatchdog.state ).to.equal( 'ready' );
  327. expect( oldEditor1 ).to.not.equal( editorWatchdog1.editor );
  328. expect( oldEditor2 ).to.equal( editorWatchdog2.editor );
  329. expect( mainWatchdog.context ).to.equal( oldContext );
  330. await mainWatchdog.destroy();
  331. } );
  332. } );
  333. } );
  334. } );
  335. function throwCKEditorError( name, context ) {
  336. throw new CKEditorError( name, context );
  337. }
  338. function waitCycle() {
  339. return new Promise( res => setTimeout( res ) );
  340. }