Fix and regression test for fast proxy artifacts.
[dygraphs.git] / auto_tests / tests / fast_canvas_proxy.js
... / ...
CommitLineData
1/**
2 * @fileoverview Tests for fastCanvasProxy, which drops superfluous segments.
3 *
4 * @author danvdk@gmail.com (Dan Vanderkam)
5 */
6describe("fast-canvas-proxy", function() {
7
8beforeEach(function() {
9});
10
11afterEach(function() {
12});
13
14var fakeCanvasContext = {
15 moveTo: function() {},
16 lineTo: function() {},
17 beginPath: function() {},
18 closePath: function() {},
19 fill: function() {},
20 stroke: function() {}
21}
22
23function extractMoveToAndLineToCalls(proxy) {
24 var calls = proxy.calls__;
25 var out = [];
26 for (var i = 0; i < calls.length; i++) {
27 var c = calls[i];
28 if (c.name == 'moveTo' || c.name == 'lineTo') {
29 out.push([c.name, c.args[0], c.args[1]]);
30 }
31 }
32 return out;
33}
34
35it('testExtraMoveTosElided', function() {
36 var htx = new Proxy(fakeCanvasContext);
37 var fastProxy = DygraphCanvasRenderer._fastCanvasProxy(htx);
38
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);
44 fastProxy.stroke();
45
46 assert.deepEqual([['moveTo', 1, 1],
47 ['lineTo', 2, 1],
48 ['lineTo', 3, 1]], extractMoveToAndLineToCalls(htx));
49});
50
51it('testConsecutiveMoveTosElided', function() {
52 var htx = new Proxy(fakeCanvasContext);
53 var fastProxy = DygraphCanvasRenderer._fastCanvasProxy(htx);
54
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);
60 fastProxy.stroke();
61
62 assert.deepEqual([['moveTo', 1, 1],
63 ['lineTo', 2, 1],
64 ['moveTo', 3.2, 3]], extractMoveToAndLineToCalls(htx));
65});
66
67it('testSuperfluousSegmentsElided', function() {
68 var htx = new Proxy(fakeCanvasContext);
69 var fastProxy = DygraphCanvasRenderer._fastCanvasProxy(htx);
70
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.
91 fastProxy.stroke();
92
93 assert.deepEqual([['moveTo', 0.6, 1],
94 ['lineTo', 1.0, 5],
95 ['lineTo', 1.2, 0],
96 ['lineTo', 1.7, 30],
97 ['lineTo', 1.8, -30],
98 ['moveTo', 3, 0]], extractMoveToAndLineToCalls(htx));
99});
100
101
102// For a more visual version of this test, see
103// https://gist.github.com/danvk/e98dbb24253c9b153696
104// The drawing commands in the following two tests are taken from there.
105it('should handle gaps on the left', function() {
106 var htx = new Proxy(fakeCanvasContext);
107 var fastProxy = DygraphCanvasRenderer._fastCanvasProxy(htx);
108
109 fastProxy.moveTo(0, 320);
110 fastProxy.lineTo(0, 320);
111 fastProxy.lineTo(53.21, 187);
112 fastProxy.lineTo(53.23, 29);
113 fastProxy.lineTo(53.41, 320);
114 fastProxy.lineTo(54.15, 320);
115 fastProxy.lineTo(475, 320);
116 fastProxy.lineTo(475, 320);
117 fastProxy.fill();
118
119 assert.deepEqual([
120 ['moveTo', 0, 320],
121 ['lineTo', 0, 320],
122 ['lineTo', 53.21, 187],
123 ['lineTo', 53.23, 29],
124 ['lineTo', 53.41, 320],
125 ['lineTo', 54.15, 320],
126 ['lineTo', 475, 320],
127 ['lineTo', 475, 320]
128 ], extractMoveToAndLineToCalls(htx));
129});
130
131it('should handle gaps on the right', function() {
132 var htx = new Proxy(fakeCanvasContext);
133 var fastProxy = DygraphCanvasRenderer._fastCanvasProxy(htx);
134 fastProxy.moveTo(240.2, 320);
135 fastProxy.lineTo(240.2, 320);
136 fastProxy.lineTo(240.2, 174);
137 fastProxy.lineTo(240.7, 145);
138 fastProxy.lineTo(240.8, 320);
139 fastProxy.lineTo(241.3, 29);
140 fastProxy.lineTo(241.4, 320);
141 fastProxy.lineTo(715.9, 320);
142 fastProxy.lineTo(715.9, 320);
143 fastProxy.fill();
144
145 assert.deepEqual([
146 ['moveTo', 240.2, 320],
147 ['lineTo', 240.2, 320],
148 ['lineTo', 240.2, 174],
149 ['lineTo', 240.7, 145],
150 ['lineTo', 240.8, 320],
151 ['lineTo', 241.3, 29],
152 ['lineTo', 241.4, 320],
153 ['lineTo', 715.9, 320],
154 ['lineTo', 715.9, 320]
155 ], extractMoveToAndLineToCalls(htx));
156});
157
158});