2 * @fileoverview Tests for fastCanvasProxy, which drops superfluous segments.
4 * @author danvdk@gmail.com (Dan Vanderkam)
6 describe("fast-canvas-proxy", function() {
8 beforeEach(function() {
11 afterEach(function() {
14 var fakeCanvasContext
= {
15 moveTo
: function() {},
16 lineTo
: function() {},
17 beginPath
: function() {},
18 closePath
: function() {},
23 function extractMoveToAndLineToCalls(proxy
) {
24 var calls
= proxy
.calls__
;
26 for (var i
= 0; i
< calls
.length
; i
++) {
28 if (c
.name
== 'moveTo' || c
.name
== 'lineTo') {
29 out
.push([c
.name
, c
.args
[0], c
.args
[1]]);
35 it('testExtraMoveTosElided', function() {
36 var htx
= new Proxy(fakeCanvasContext
);
37 var fastProxy
= DygraphCanvasRenderer
._fastCanvasProxy(htx
);
39 fastProxy
.moveTo(1, 1);
40 fastProxy
.lineTo(2, 1);
41 fastProxy
.moveTo(2, 1);
42 fastProxy
.lineTo(3, 1);
43 fastProxy
.moveTo(3, 1);
46 assert
.deepEqual([['moveTo', 1, 1],
48 ['lineTo', 3, 1]], extractMoveToAndLineToCalls(htx
));
51 it('testConsecutiveMoveTosElided', function() {
52 var htx
= new Proxy(fakeCanvasContext
);
53 var fastProxy
= DygraphCanvasRenderer
._fastCanvasProxy(htx
);
55 fastProxy
.moveTo(1, 1);
56 fastProxy
.lineTo(2, 1);
57 fastProxy
.moveTo(3, 1);
58 fastProxy
.moveTo(3.1, 2);
59 fastProxy
.moveTo(3.2, 3);
62 assert
.deepEqual([['moveTo', 1, 1],
64 ['moveTo', 3.2, 3]], extractMoveToAndLineToCalls(htx
));
67 it('testSuperfluousSegmentsElided', function() {
68 var htx
= new Proxy(fakeCanvasContext
);
69 var fastProxy
= DygraphCanvasRenderer
._fastCanvasProxy(htx
);
71 fastProxy
.moveTo(0.6, 1);
72 fastProxy
.lineTo(0.7, 2);
73 fastProxy
.lineTo(0.8, 3);
74 fastProxy
.lineTo(0.9, 4);
75 fastProxy
.lineTo(1.0, 5); // max for Math.round(x) == 1
76 fastProxy
.lineTo(1.1, 3);
77 fastProxy
.lineTo(1.2, 0); // min for Math.round(x) == 1
78 fastProxy
.lineTo(1.3, 1);
79 fastProxy
.lineTo(1.4, 2);
80 fastProxy
.moveTo(1.4, 2);
81 fastProxy
.lineTo(1.5, 2); // rounding up to 2
82 fastProxy
.moveTo(1.5, 2);
83 fastProxy
.lineTo(1.6, 3);
84 fastProxy
.moveTo(1.6, 3);
85 fastProxy
.lineTo(1.7, 30); // max for Math.round(x) == 2
86 fastProxy
.moveTo(1.7, 30);
87 fastProxy
.lineTo(1.8, -30); // min for Math.round(x) == 2
88 fastProxy
.moveTo(1.8, -30);
89 fastProxy
.lineTo(1.9, 0);
90 fastProxy
.moveTo(3, 0); // dodge the "don't touch the last pixel" rule.
93 assert
.deepEqual([['moveTo', 0.6, 1],
98 ['moveTo', 3, 0]], extractMoveToAndLineToCalls(htx
));