plugincollection.js 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  1. /**
  2. * @license Copyright (c) 2003-2016, CKSource - Frederico Knabben. All rights reserved.
  3. * For licensing, see LICENSE.md.
  4. */
  5. /* bender-tags: browser-only */
  6. 'use strict';
  7. import moduleUtils from '/tests/ckeditor5/_utils/module.js';
  8. import testUtils from '/tests/ckeditor5/_utils/utils.js';
  9. import Editor from '/ckeditor5/editor/editor.js';
  10. import PluginCollection from '/ckeditor5/plugincollection.js';
  11. import Plugin from '/ckeditor5/plugin.js';
  12. import Feature from '/ckeditor5/feature.js';
  13. import CKEditorError from '/ckeditor5/utils/ckeditorerror.js';
  14. import log from '/ckeditor5/utils/log.js';
  15. let editor;
  16. let PluginA, PluginB, PluginC, PluginD, PluginE, PluginF, PluginG, PluginH, PluginI;
  17. class TestError extends Error {}
  18. class GrandPlugin extends Feature {}
  19. testUtils.createSinonSandbox();
  20. before( () => {
  21. PluginA = createPlugin( 'A' );
  22. PluginB = createPlugin( 'B' );
  23. PluginC = createPlugin( 'C' );
  24. PluginD = createPlugin( 'D' );
  25. PluginE = createPlugin( 'E' );
  26. PluginF = createPlugin( 'F' );
  27. PluginG = createPlugin( 'G', GrandPlugin );
  28. PluginH = createPlugin( 'H' );
  29. PluginI = createPlugin( 'I' );
  30. PluginC.requires = [ PluginB ];
  31. PluginD.requires = [ PluginA, PluginC ];
  32. PluginF.requires = [ PluginE ];
  33. PluginE.requires = [ PluginF ];
  34. PluginH.requires = [ PluginI ];
  35. editor = new Editor();
  36. } );
  37. // Create fake plugins that will be used on tests.
  38. moduleUtils.define( 'A/A', () => {
  39. return PluginA;
  40. } );
  41. moduleUtils.define( 'B/B', () => {
  42. return PluginB;
  43. } );
  44. moduleUtils.define( 'C/C', [ 'editor/editor', 'B/B' ], () => {
  45. return PluginC;
  46. } );
  47. moduleUtils.define( 'D/D', [ 'editor/editor', 'A/A', 'C/C' ], () => {
  48. return PluginD;
  49. } );
  50. moduleUtils.define( 'E/E', [ 'editor/editor', 'F/F' ], () => {
  51. return PluginE;
  52. } );
  53. moduleUtils.define( 'F/F', [ 'editor/editor', 'E/E' ], () => {
  54. return PluginF;
  55. } );
  56. moduleUtils.define( 'G/G', () => {
  57. return PluginG;
  58. } );
  59. moduleUtils.define( 'H/H', () => {
  60. return PluginH;
  61. } );
  62. moduleUtils.define( 'I/I', () => {
  63. return PluginI;
  64. } );
  65. // Erroneous cases.
  66. moduleUtils.define( 'X/X', () => {
  67. throw new TestError( 'Some error inside a plugin' );
  68. } );
  69. moduleUtils.define( 'Y/Y', () => {
  70. return class {};
  71. } );
  72. /////////////
  73. describe( 'load', () => {
  74. it( 'should not fail when trying to load 0 plugins (empty array)', () => {
  75. let plugins = new PluginCollection( editor );
  76. return plugins.load( [] )
  77. .then( () => {
  78. expect( getPlugins( plugins ) ).to.be.empty();
  79. } );
  80. } );
  81. it( 'should add collection items for loaded plugins', () => {
  82. let plugins = new PluginCollection( editor );
  83. return plugins.load( [ 'A', 'B' ] )
  84. .then( () => {
  85. expect( getPlugins( plugins ).length ).to.equal( 2 );
  86. expect( plugins.get( PluginA ) ).to.be.an.instanceof( PluginA );
  87. expect( plugins.get( PluginB ) ).to.be.an.instanceof( PluginB );
  88. expect( plugins.get( 'A' ) ).to.be.an.instanceof( PluginA );
  89. expect( plugins.get( 'B' ) ).to.be.an.instanceof( PluginB );
  90. } );
  91. } );
  92. it( 'should load dependency plugins', () => {
  93. let plugins = new PluginCollection( editor );
  94. let spy = sinon.spy( plugins, '_add' );
  95. return plugins.load( [ 'A', 'C' ] )
  96. .then( ( loadedPlugins ) => {
  97. expect( getPlugins( plugins ).length ).to.equal( 3 );
  98. expect( getPluginNames( getPluginsFromSpy( spy ) ) )
  99. .to.deep.equal( [ 'A', 'B', 'C' ], 'order by plugins._add()' );
  100. expect( getPluginNames( loadedPlugins ) )
  101. .to.deep.equal( [ 'A', 'B', 'C' ], 'order by returned value' );
  102. } );
  103. } );
  104. it( 'should be ok when dependencies are loaded first', () => {
  105. let plugins = new PluginCollection( editor );
  106. let spy = sinon.spy( plugins, '_add' );
  107. return plugins.load( [ 'A', 'B', 'C' ] )
  108. .then( ( loadedPlugins ) => {
  109. expect( getPlugins( plugins ).length ).to.equal( 3 );
  110. expect( getPluginNames( getPluginsFromSpy( spy ) ) )
  111. .to.deep.equal( [ 'A', 'B', 'C' ], 'order by plugins._add()' );
  112. expect( getPluginNames( loadedPlugins ) )
  113. .to.deep.equal( [ 'A', 'B', 'C' ], 'order by returned value' );
  114. } );
  115. } );
  116. it( 'should load deep dependency plugins', () => {
  117. let plugins = new PluginCollection( editor );
  118. let spy = sinon.spy( plugins, '_add' );
  119. return plugins.load( [ 'D' ] )
  120. .then( ( loadedPlugins ) => {
  121. expect( getPlugins( plugins ).length ).to.equal( 4 );
  122. // The order must have dependencies first.
  123. expect( getPluginNames( getPluginsFromSpy( spy ) ) )
  124. .to.deep.equal( [ 'A', 'B', 'C', 'D' ], 'order by plugins._add()' );
  125. expect( getPluginNames( loadedPlugins ) )
  126. .to.deep.equal( [ 'A', 'B', 'C', 'D' ], 'order by returned value' );
  127. } );
  128. } );
  129. it( 'should handle cross dependency plugins', () => {
  130. let plugins = new PluginCollection( editor );
  131. let spy = sinon.spy( plugins, '_add' );
  132. return plugins.load( [ 'A', 'E' ] )
  133. .then( ( loadedPlugins ) => {
  134. expect( getPlugins( plugins ).length ).to.equal( 3 );
  135. // The order must have dependencies first.
  136. expect( getPluginNames( getPluginsFromSpy( spy ) ) )
  137. .to.deep.equal( [ 'A', 'F', 'E' ], 'order by plugins._add()' );
  138. expect( getPluginNames( loadedPlugins ) )
  139. .to.deep.equal( [ 'A', 'F', 'E' ], 'order by returned value' );
  140. } );
  141. } );
  142. it( 'should load grand child classes', () => {
  143. let plugins = new PluginCollection( editor );
  144. return plugins.load( [ 'G' ] )
  145. .then( () => {
  146. expect( getPlugins( plugins ).length ).to.equal( 1 );
  147. } );
  148. } );
  149. it( 'should make plugin available to get by name when plugin was loaded as dependency first', () => {
  150. let plugins = new PluginCollection( editor );
  151. return plugins.load( [ 'H', 'I' ] )
  152. .then( () => {
  153. expect( plugins.get( 'I' ) ).to.be.instanceof( PluginI );
  154. } );
  155. } );
  156. it( 'should set the `editor` property on loaded plugins', () => {
  157. let plugins = new PluginCollection( editor );
  158. return plugins.load( [ 'A', 'B' ] )
  159. .then( () => {
  160. expect( plugins.get( 'A' ).editor ).to.equal( editor );
  161. expect( plugins.get( 'B' ).editor ).to.equal( editor );
  162. } );
  163. } );
  164. it( 'should reject on invalid plugin names (forward require.js loading error)', () => {
  165. let logSpy = testUtils.sinon.stub( log, 'error' );
  166. let plugins = new PluginCollection( editor );
  167. return plugins.load( [ 'A', 'BAD', 'B' ] )
  168. // Throw here, so if by any chance plugins.load() was resolved correctly catch() will be stil executed.
  169. .then( () => {
  170. throw new Error( 'Test error: this promise should not be resolved successfully' );
  171. } )
  172. .catch( ( err ) => {
  173. expect( err ).to.be.an.instanceof( Error );
  174. // Make sure it's the Require.JS error, not the one thrown above.
  175. expect( err.message ).to.match( /^Script error for/ );
  176. sinon.assert.calledOnce( logSpy );
  177. expect( logSpy.args[ 0 ][ 0 ] ).to.match( /^plugincollection-load:/ );
  178. } );
  179. } );
  180. it( 'should reject on broken plugins (forward the error thrown in a plugin)', () => {
  181. let logSpy = testUtils.sinon.stub( log, 'error' );
  182. let plugins = new PluginCollection( editor );
  183. return plugins.load( [ 'A', 'X', 'B' ] )
  184. // Throw here, so if by any chance plugins.load() was resolved correctly catch() will be stil executed.
  185. .then( () => {
  186. throw new Error( 'Test error: this promise should not be resolved successfully' );
  187. } )
  188. .catch( ( err ) => {
  189. expect( err ).to.be.an.instanceof( TestError );
  190. expect( err ).to.have.property( 'message', 'Some error inside a plugin' );
  191. sinon.assert.calledOnce( logSpy );
  192. expect( logSpy.args[ 0 ][ 0 ] ).to.match( /^plugincollection-load:/ );
  193. } );
  194. } );
  195. it( 'should reject when loading a module which is not a plugin', () => {
  196. let logSpy = testUtils.sinon.stub( log, 'error' );
  197. let plugins = new PluginCollection( editor );
  198. return plugins.load( [ 'Y' ] )
  199. // Throw here, so if by any chance plugins.load() was resolved correctly catch() will be stil executed.
  200. .then( () => {
  201. throw new Error( 'Test error: this promise should not be resolved successfully' );
  202. } )
  203. .catch( ( err ) => {
  204. expect( err ).to.be.an.instanceof( CKEditorError );
  205. expect( err.message ).to.match( /^plugincollection-instance/ );
  206. sinon.assert.calledOnce( logSpy );
  207. expect( logSpy.args[ 0 ][ 0 ] ).to.match( /^plugincollection-load:/ );
  208. } );
  209. } );
  210. } );
  211. describe( 'getPluginPath()', () => {
  212. it( 'generates path for modules within some package', () => {
  213. const p = PluginCollection.getPluginPath( 'some/ba' );
  214. expect( p ).to.equal( 'ckeditor5/some/ba.js' );
  215. } );
  216. it( 'generates path from simplified feature name', () => {
  217. const p = PluginCollection.getPluginPath( 'foo' );
  218. expect( p ).to.equal( 'ckeditor5/foo/foo.js' );
  219. } );
  220. } );
  221. function createPlugin( name, baseClass ) {
  222. baseClass = baseClass || Plugin;
  223. const P = class extends baseClass {
  224. constructor( editor ) {
  225. super( editor );
  226. this._pluginName = name;
  227. }
  228. };
  229. P._pluginName = name;
  230. return P;
  231. }
  232. function getPlugins( pluginCollection ) {
  233. const plugins = [];
  234. for ( let entry of pluginCollection ) {
  235. // Keep only plugins kept under their classes.
  236. if ( typeof entry[ 0 ] == 'function' ) {
  237. plugins.push( entry[ 1 ] );
  238. }
  239. }
  240. return plugins;
  241. }
  242. function getPluginsFromSpy( addSpy ) {
  243. return addSpy.args
  244. .map( ( arg ) => arg[ 0 ] )
  245. // Entries may be kept twice in the plugins map - once as a pluginName => plugin, once as pluginClass => plugin.
  246. // Return only pluginClass => plugin entries as these will always represent all plugins.
  247. .filter( ( plugin ) => typeof plugin == 'function' );
  248. }
  249. function getPluginNames( plugins ) {
  250. return plugins.map( ( plugin ) => plugin._pluginName );
  251. }