jquery.flot.canvaswrapper.Test.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /* eslint-disable */
  2. /* global $, describe, it, xit, xdescribe, after, afterEach, expect*/
  3. describe('CanvasWrapper', function() {
  4. var placeholder;
  5. beforeEach(function() {
  6. placeholder = setFixtures('<div id="test-container" style="width: 600px;height: 400px">')
  7. .find('#test-container');
  8. });
  9. it('should create a new canvas element', function() {
  10. var canvas = new Flot.Canvas('myCanvas', placeholder[0]);
  11. expect(canvas.element).toBeTruthy();
  12. expect(placeholder.find('.myCanvas')).toBeTruthy();
  13. });
  14. it('should reuse an existing canvas with the same class', function() {
  15. var canvas1 = new Flot.Canvas('myCanvas', placeholder[0]);
  16. var element1 = placeholder.find('.myCanvas')[0];
  17. var canvas2 = new Flot.Canvas('myCanvas', placeholder[0]);
  18. var element2 = placeholder.find('.myCanvas')[0];
  19. expect(element1).toBe(element2);
  20. expect(placeholder.find('.myCanvas').length).toBe(1);
  21. });
  22. it('should resize the canvas to the given width and height', function() {
  23. var canvas = newCanvas(placeholder);
  24. canvas.resize(222, 333);
  25. expect(canvas.width).toBe(222);
  26. expect(canvas.height).toBe(333);
  27. expect(canvas.element.style.width).toBe('222px');
  28. expect(canvas.element.style.height).toBe('333px');
  29. });
  30. it('should resize the canvas but not less than a minimum width and height', function() {
  31. var canvas = newCanvas(placeholder);
  32. canvas.resize(0, 0);
  33. expect(canvas.width).toBe(10);
  34. expect(canvas.height).toBe(10);
  35. expect(canvas.element.style.width).toBe('10px');
  36. expect(canvas.element.style.height).toBe('10px');
  37. });
  38. it('should measure the width and height of a text', function() {
  39. var canvas = newCanvas(placeholder);
  40. var info = canvas.getTextInfo('', 'text');
  41. expect(info.width).toBeGreaterThan(0);
  42. expect(info.height).toBeGreaterThan(0);
  43. });
  44. it('should measure the width and height of a text based on its own CSS', function() {
  45. var canvas = newCanvas(placeholder);
  46. appendSetStyleFixtures('.a { font-size: 10px; }');
  47. appendSetStyleFixtures('.b { font-size: 20px; }');
  48. var info1 = canvas.getTextInfo('', 'text', 'a');
  49. var info2 = canvas.getTextInfo('', 'text', 'b');
  50. expect(info2.width).toBeGreaterThan(info1.width);
  51. expect(info2.height).toBeGreaterThan(info1.height);
  52. });
  53. it('should measure the width and height of a text based on its layer CSS', function() {
  54. var canvas = newCanvas(placeholder);
  55. appendSetStyleFixtures('.a { font-size: 10px; }');
  56. appendSetStyleFixtures('.b { font-size: 20px; }');
  57. var info1 = canvas.getTextInfo('a', 'text');
  58. var info2 = canvas.getTextInfo('b', 'text');
  59. expect(info2.width).toBeGreaterThan(info1.width);
  60. expect(info2.height).toBeGreaterThan(info1.height);
  61. });
  62. it('should measure the width of a text based on its actual length', function() {
  63. var canvas = newCanvas(placeholder);
  64. appendSetStyleFixtures('.a { font-size: 10px; }');
  65. var info1 = canvas.getTextInfo('a', 'text');
  66. var info2 = canvas.getTextInfo('a', 'longer text');
  67. expect(info2.width).toBeGreaterThan(info1.width);
  68. });
  69. it('should return the same width and height for numbers with the same digit count', function() {
  70. var canvas = newCanvas(placeholder);
  71. var info1 = canvas.getTextInfo('', '01234');
  72. var info2 = canvas.getTextInfo('', '56789');
  73. expect(info2.width).toBe(info1.width);
  74. });
  75. it('should add text at the given layer, coords, and font CSS', function() {
  76. var canvas = newCanvas(placeholder);
  77. canvas.addText('layer', 100, 200, '123', 'a');
  78. canvas.render();
  79. var elem = placeholder.find('.a')[0],
  80. box = elem.getBoundingClientRect();
  81. //TODO Raluca: This should be fixed. Given the starting position for drawing SVG text element, there is a difference between HTML and SVG element placement.
  82. //expect(box.left).toBe(100);
  83. //expect(box.top).toBe(200);
  84. expect(elem.className.baseVal).toBe('a');
  85. expect(elem.parentNode.className.baseVal).toBe('layer');
  86. });
  87. it('should add the same text with the same CSS at different coords', function() {
  88. var canvas = newCanvas(placeholder);
  89. canvas.addText('layerA', 100, 200, '123', 'a');
  90. canvas.addText('layerA', 300, 400, '123', 'a');
  91. canvas.render();
  92. var elem1 = placeholder.find('.a')[0],
  93. elem2 = placeholder.find('.a')[1],
  94. box1 = elem1.getBoundingClientRect(),
  95. box2 = elem2.getBoundingClientRect();
  96. expect(elem2.textContent).toBe(elem2.textContent);
  97. expect(box2.left).not.toBe(box1.left);
  98. expect(box2.top).not.toBe(box1.top);
  99. });
  100. it('should add different text with the same CSS at different coords', function() {
  101. var canvas = newCanvas(placeholder);
  102. canvas.addText('layerA', 100, 200, '123', 'a');
  103. canvas.addText('layerA', 300, 400, '456', 'a');
  104. canvas.render();
  105. var elem1 = placeholder.find('.a')[0],
  106. elem2 = placeholder.find('.a')[1],
  107. box1 = elem1.getBoundingClientRect(),
  108. box2 = elem2.getBoundingClientRect();
  109. expect(elem2.textContent).not.toBe(elem1.textContent);
  110. expect(box2.left).not.toBe(box1.left);
  111. expect(box2.top).not.toBe(box1.top);
  112. });
  113. it('should add different text with the same CSS and the same coords', function() {
  114. var canvas = newCanvas(placeholder);
  115. canvas.addText('layerA', 100, 200, '123', 'a');
  116. canvas.addText('layerA', 100, 200, '456', 'a');
  117. canvas.addText('layerA', 100, 200, '7890', 'a');
  118. canvas.render();
  119. var elems = placeholder.find('.a');
  120. expect(elems.length).toBe(3);
  121. });
  122. it('should add multiple tspan for text that contains br tag', function() {
  123. var canvas = newCanvas(placeholder);
  124. canvas.addText('layerA', 100, 200, '1<br>2<br>3<br>4', 'a');
  125. canvas.render();
  126. var elem = placeholder.find('.a')[0];
  127. expect(elem.childNodes.length).toBe(4);
  128. });
  129. it('should update the tspan element content', function() {
  130. var canvas = newCanvas(placeholder);
  131. canvas.addText('layerA', 100, 200, '1<br>2<br>3', 'a');
  132. canvas.render();
  133. canvas.removeText('layerA', 100, 200, '1<br>2<br>3', 'a');
  134. canvas.addText('layerA', 130, 230, '1<br>2<br>3<br>4', 'a');
  135. canvas.render();
  136. var elem = placeholder.find('.a')[0];
  137. expect(elem.childNodes.length).toBe(4);
  138. });
  139. it('should update the cache position of the elements', function() {
  140. var canvas = newCanvas(placeholder);
  141. canvas.addText('layerA', 100, 200, '123', 'a');
  142. canvas.render();
  143. canvas.removeText('layerA', 100, 200, '123', 'a');
  144. canvas.addText('layerA', 130, 230, '123', 'a');
  145. canvas.render();
  146. canvas.addText('layerA', 100, 200, '123', 'a');
  147. canvas.render();
  148. var elems = placeholder.find('.a');
  149. expect(elems.length).toBe(2);
  150. });
  151. it('should not add the same text with the same CSS and the same coords twice', function() {
  152. var canvas = newCanvas(placeholder);
  153. canvas.addText('layerA', 100, 200, '123', 'a');
  154. canvas.addText('layerA', 100, 200, '123', 'a');
  155. canvas.render();
  156. var elems = placeholder.find('.a');
  157. expect(elems.length).toBe(1);
  158. });
  159. it('should remove all text from a given layer', function() {
  160. var canvas = newCanvas(placeholder);
  161. canvas.addText('layerA', 100, 200, '123', 'a');
  162. canvas.addText('layerA', 300, 400, '123', 'a');
  163. canvas.addText('layerA', 500, 600, '456', 'b');
  164. canvas.addText('layerA', 700, 800, '456', 'b');
  165. canvas.addText('layerB', 200, 100, '123', 'c');
  166. canvas.addText('layerB', 400, 300, '456', 'c');
  167. canvas.render();
  168. canvas.removeText('layerA', NaN, NaN, null);
  169. canvas.render();
  170. var as = placeholder.find('.a'),
  171. bs = placeholder.find('.b'),
  172. cs = placeholder.find('.c');
  173. expect(as.length).toBe(0);
  174. expect(bs.length).toBe(0);
  175. expect(cs.length).toBe(2);
  176. });
  177. it('should remove specific text from specific layer and coords', function() {
  178. var canvas = newCanvas(placeholder);
  179. canvas.addText('layerA', 100, 200, '123', 'a');
  180. canvas.addText('layerA', 300, 400, '123', 'a');
  181. canvas.addText('layerA', 500, 600, '123', 'b');
  182. canvas.addText('layerA', 700, 800, '123', 'b');
  183. canvas.addText('layerB', 200, 100, '123', 'c');
  184. canvas.addText('layerB', 400, 300, '123', 'c');
  185. canvas.render();
  186. canvas.removeText('layerA', 300, 400, '123', 'a');
  187. canvas.render();
  188. var as = placeholder.find('.a'),
  189. bs = placeholder.find('.b'),
  190. cs = placeholder.find('.c');
  191. expect(as.length).toBe(1);
  192. expect(bs.length).toBe(2);
  193. expect(cs.length).toBe(2);
  194. });
  195. it('should remove specific text from specific layer and coords when more texts overlaps', function() {
  196. var canvas = newCanvas(placeholder);
  197. canvas.addText('layerA', 100, 200, '123', 'a');
  198. canvas.addText('layerA', 100, 200, '456', 'a');
  199. canvas.addText('layerA', 100, 200, '7890', 'a');
  200. canvas.render();
  201. var initialTextElements = placeholder.find('.a');
  202. expect(initialTextElements.length).toBe(3);
  203. canvas.removeText('layerA', 100, 200, '456', 'a');
  204. canvas.render();
  205. var finalTextElements = placeholder.find('.a');
  206. expect(finalTextElements.length).toBe(2);
  207. var remainingTexts = [finalTextElements[0].textContent, finalTextElements[1].textContent];
  208. expect(remainingTexts).toContain('123');
  209. expect(remainingTexts).toContain('7890');
  210. });
  211. it('should remove all text', function() {
  212. var canvas = newCanvas(placeholder);
  213. canvas.addText('layerA', 100, 200, '123', 'a');
  214. canvas.addText('layerA', 300, 400, '456', 'b');
  215. canvas.addText('layerB', 500, 600, '789', 'c');
  216. canvas.render();
  217. canvas.clearCache();
  218. var as = placeholder.find('.a'),
  219. bs = placeholder.find('.b'),
  220. cs = placeholder.find('.c');
  221. expect(as.length).toBe(0);
  222. expect(bs.length).toBe(0);
  223. expect(cs.length).toBe(0);
  224. });
  225. it('should move&replace obsolete text', function() {
  226. var canvas = newCanvas(placeholder);
  227. canvas.addText('layerA', 100, 200, '123', 'a');
  228. canvas.render();
  229. var elem = placeholder.find('.a')[0];
  230. elem._marker = '_marker';
  231. canvas.removeText('layerA', NaN, NaN, null);
  232. canvas.addText('layerA', 300, 400, '456', 'a');
  233. canvas.render();
  234. elem = placeholder.find('.a')[0];
  235. expect(elem._marker).toBe('_marker');
  236. });
  237. it('should work with an object instead of a class name', function() {
  238. var canvas = newCanvas(placeholder)
  239. settings = {
  240. style: 'normal',
  241. variant: 'normal',
  242. weight: '400',
  243. size: '40',
  244. lineHeight: '23',
  245. family: '"Times New Roman"',
  246. fill: 'rgb(100, 200, 0)'
  247. };
  248. var info = canvas.getTextInfo('layerA', '123', settings);
  249. expect(info.width).toBeGreaterThan(10);
  250. canvas.addText('layerA', 100, 200, '123', settings);
  251. canvas.render();
  252. var as = placeholder.find('.layerA'),
  253. style = window.getComputedStyle(as[0].firstChild),
  254. layerAColor = $.color.parse(style.fill),
  255. styleColor = $.color.parse(settings.fill);
  256. expect(as.length).toBe(1);
  257. expect(style.fontFamily).toContain(settings.family.slice(1, -1));
  258. expect(layerAColor.toString()).toBe(styleColor.toString());
  259. canvas.removeText('layerA', 100, 200, '123', settings);
  260. canvas.render();
  261. as = placeholder.find('.a');
  262. expect(as.length).toBe(0);
  263. });
  264. function newCanvas(placeholder) {
  265. return new Flot.Canvas('myCanvas', placeholder[0]);
  266. }
  267. });