utHelper.js 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. (function (context) {
  2. /**
  3. * @public
  4. * @type {Object}
  5. */
  6. var helper = context.utHelper = {};
  7. var nativeSlice = Array.prototype.slice;
  8. /**
  9. * Usage:
  10. * var testCase = helper.prepare([
  11. * 'echarts/chart/line',
  12. * 'echarts/component/grid',
  13. * 'echarts/component/toolbox'
  14. * ])
  15. *
  16. * testCase('test_case_1', function (grid, line, toolbox) {
  17. * // Real test case.
  18. * // this.echarts can be visited.
  19. * });
  20. *
  21. * testCase.requireId(['echarts/model/Component'])('test_case_2', function (Component) {
  22. * // Real test case.
  23. * // this.echarts can be visited.
  24. * });
  25. *
  26. * testCase.createChart()(function(grid, line, toolbox) {
  27. * // this.echarts can be visited.
  28. * // this.chart can be visited.
  29. * // this.charts[0] can be visited, this.charts[0] === this.chart
  30. * // this.el can be visited.
  31. * // this.els[0] can be visited, this.els[0] === this.el
  32. * });
  33. *
  34. * testCase.createChart(2)(function(grid, line, toolbox) {
  35. * // this.echarts can be visited.
  36. * // this.chart can be visited.
  37. * // this.charts[0] can be visited, this.charts[0] === this.chart
  38. * // this.charts[1] can be visited.
  39. * // this.el can be visited.
  40. * // this.els[0] can be visited, this.els[0] === this.el
  41. * // this.els[1] can be visited.
  42. * });
  43. *
  44. * testCase.createChart(1, 300, 200)(...);
  45. *
  46. *
  47. * @public
  48. * @params {Array.<string>} [requireId] Like:
  49. * @return {Function} testCase function wrap.
  50. */
  51. helper.prepare = function (requireId) {
  52. window.beforeEach(function (done) {
  53. window.jasmine.DEFAULT_TIMEOUT_INTERVAL = 10000;
  54. done();
  55. });
  56. return wrapTestCaseFn(genContext({requireId: requireId}));
  57. function wrapTestCaseFn(context) {
  58. var testCase = function (name, doTest) {
  59. var requireId = context.requireId;
  60. if (!(requireId instanceof Array)) {
  61. requireId = requireId != null ? [] : [requireId];
  62. }
  63. requireId = ['echarts'].concat(requireId);
  64. window.it(name, function (done) {
  65. helper.resetPackageLoader(onLoaderReset);
  66. function onLoaderReset() {
  67. window.require(requireId, onModuleLoaded);
  68. }
  69. function onModuleLoaded(echarts) {
  70. var createResult = createChart(context, echarts);
  71. var userScope = {
  72. echarts: echarts,
  73. chart: createResult.charts[0],
  74. charts: createResult.charts.slice(),
  75. el: createResult.els[0],
  76. els: createResult.els.slice()
  77. };
  78. doTest.apply(
  79. userScope,
  80. Array.prototype.slice.call(arguments, 1)
  81. );
  82. removeChart(createResult);
  83. done();
  84. }
  85. });
  86. };
  87. testCase.requireId = function (requireId) {
  88. return wrapTestCaseFn(genContext({requireId: requireId}, context));
  89. };
  90. testCase.createChart = function (chartCount, width, height) {
  91. chartCount == null && (chartCount = 1);
  92. return wrapTestCaseFn(genContext({
  93. chartCount: chartCount,
  94. width: width,
  95. height: height
  96. }, context));
  97. };
  98. return testCase;
  99. }
  100. function genContext(props, originalContext) {
  101. var context = {};
  102. if (originalContext) {
  103. for (var key in originalContext) {
  104. if (originalContext.hasOwnProperty(key)) {
  105. context[key] = originalContext[key];
  106. }
  107. }
  108. }
  109. if (props) {
  110. for (var key in props) {
  111. if (props.hasOwnProperty(key)) {
  112. context[key] = props[key];
  113. }
  114. }
  115. }
  116. return context;
  117. }
  118. function createChart(context, echarts) {
  119. var els = [];
  120. var charts = [];
  121. for (var i = 0; i < context.chartCount || 0; i++) {
  122. var el = document.createElement('div');
  123. document.body.appendChild(el);
  124. el.style.cssText = [
  125. 'visibility:hidden',
  126. 'width:' + (context.width || '500') + 'px',
  127. 'height:' + (context.height || '400') + 'px',
  128. 'position:absolute',
  129. 'bottom:0',
  130. 'right:0'
  131. ].join(';');
  132. els.push(el);
  133. charts.push(echarts.init(el, null, {renderer: 'canvas'}));
  134. }
  135. return {charts: charts, els: els};
  136. }
  137. function removeChart(createResult) {
  138. for (var i = 0; i < createResult.charts.length; i++) {
  139. var chart = createResult.charts[i];
  140. chart && chart.dispose();
  141. }
  142. for (var i = 0; i < createResult.els.length; i++) {
  143. var el = createResult.els[i];
  144. el && document.body.removeChild(el);
  145. }
  146. }
  147. };
  148. /**
  149. * @param {*} target
  150. * @param {*} source
  151. */
  152. helper.extend = function (target, source) {
  153. for (var key in source) {
  154. if (source.hasOwnProperty(key)) {
  155. target[key] = source[key];
  156. }
  157. }
  158. return target;
  159. };
  160. /**
  161. * @public
  162. */
  163. helper.g = function (id) {
  164. return document.getElementById(id);
  165. };
  166. /**
  167. * @public
  168. */
  169. helper.removeEl = function (el) {
  170. var parent = helper.parentEl(el);
  171. parent && parent.removeChild(el);
  172. };
  173. /**
  174. * @public
  175. */
  176. helper.parentEl = function (el) {
  177. //parentElement for ie.
  178. return el.parentElement || el.parentNode;
  179. };
  180. /**
  181. * 得到head
  182. *
  183. * @public
  184. */
  185. helper.getHeadEl = function (s) {
  186. return document.head
  187. || document.getElementsByTagName('head')[0]
  188. || document.documentElement;
  189. };
  190. /**
  191. * @public
  192. */
  193. helper.curry = function (func) {
  194. var args = nativeSlice.call(arguments, 1);
  195. return function () {
  196. return func.apply(this, args.concat(nativeSlice.call(arguments)));
  197. };
  198. };
  199. /**
  200. * @public
  201. */
  202. helper.bind = function (func, context) {
  203. var args = nativeSlice.call(arguments, 2);
  204. return function () {
  205. return func.apply(context, args.concat(nativeSlice.call(arguments)));
  206. };
  207. };
  208. /**
  209. * Load javascript script
  210. *
  211. * @param {string} resource Like 'xx/xx/xx.js';
  212. */
  213. helper.loadScript = function (url, id, callback) {
  214. var head = helper.getHeadEl();
  215. var script = document.createElement('script');
  216. script.setAttribute('type', 'text/javascript');
  217. script.setAttribute('charset', 'utf-8');
  218. if (id) {
  219. script.setAttribute('id', id);
  220. }
  221. script.setAttribute('src', url);
  222. // @see jquery
  223. // Attach handlers for all browsers
  224. script.onload = script.onreadystatechange = function () {
  225. if (!script.readyState || /loaded|complete/.test(script.readyState)) {
  226. // Handle memory leak in IE
  227. script.onload = script.onreadystatechange = null;
  228. // Dereference the script
  229. script = undefined;
  230. callback && callback();
  231. }
  232. };
  233. // Use insertBefore instead of appendChild to circumvent an IE6 bug.
  234. // This arises when a base node is used (jquery #2709 and #4378).
  235. head.insertBefore(script, head.firstChild);
  236. };
  237. /**
  238. * Reset package loader, where esl is cleaned and reloaded.
  239. *
  240. * @public
  241. */
  242. helper.resetPackageLoader = function (then) {
  243. // Clean esl
  244. var eslEl = helper.g('esl');
  245. if (eslEl) {
  246. helper.removeEl(eslEl);
  247. }
  248. var eslConfig = helper.g('esl');
  249. if (eslConfig) {
  250. helper.removeEl(eslConfig);
  251. }
  252. context.define = null;
  253. context.require = null;
  254. // Import esl.
  255. helper.loadScript('../esl.js', 'esl', function () {
  256. helper.loadScript('config.js', 'config', function () {
  257. then();
  258. });
  259. });
  260. };
  261. /**
  262. * @public
  263. * @param {Array.<string>} deps
  264. * @param {Array.<Function>} testFnList
  265. * @param {Function} done All done callback.
  266. */
  267. helper.resetPackageLoaderEachTest = function (deps, testFnList, done) {
  268. var i = -1;
  269. next();
  270. function next() {
  271. i++;
  272. if (testFnList.length <= i) {
  273. done();
  274. return;
  275. }
  276. helper.resetPackageLoader(function () {
  277. window.require(deps, function () {
  278. testFnList[i].apply(null, arguments);
  279. next();
  280. });
  281. });
  282. }
  283. };
  284. /**
  285. * @public
  286. */
  287. helper.printElement = function (el) {
  288. var result = {};
  289. var props = ['position', 'scale', 'rotation', 'style', 'shape'];
  290. for (var i = 0; i < props.length; i++) {
  291. result[props[i]] = el[props[i]];
  292. }
  293. return window.JSON.stringify(result, null, 4);
  294. };
  295. })(window);