Commit | Line | Data |
---|---|---|
20b87d28 DV |
1 | /** |
2 | * @fileoverview Tests for fastCanvasProxy, which drops superfluous segments. | |
3 | * | |
4 | * @author danvdk@gmail.com (Dan Vanderkam) | |
5 | */ | |
89fdcedb | 6 | describe("fast-canvas-proxy", function() { |
20b87d28 | 7 | |
89fdcedb DV |
8 | beforeEach(function() { |
9 | }); | |
20b87d28 | 10 | |
89fdcedb DV |
11 | afterEach(function() { |
12 | }); | |
20b87d28 DV |
13 | |
14 | var fakeCanvasContext = { | |
15 | moveTo: function() {}, | |
16 | lineTo: function() {}, | |
17 | beginPath: function() {}, | |
18 | closePath: function() {}, | |
19 | fill: function() {}, | |
20 | stroke: function() {} | |
21 | } | |
22 | ||
23 | function 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 | ||
89fdcedb | 35 | it('testExtraMoveTosElided', function() { |
20b87d28 DV |
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 | ||
89fdcedb | 46 | assert.deepEqual([['moveTo', 1, 1], |
20b87d28 DV |
47 | ['lineTo', 2, 1], |
48 | ['lineTo', 3, 1]], extractMoveToAndLineToCalls(htx)); | |
89fdcedb | 49 | }); |
20b87d28 | 50 | |
89fdcedb | 51 | it('testConsecutiveMoveTosElided', function() { |
20b87d28 DV |
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 | ||
89fdcedb | 62 | assert.deepEqual([['moveTo', 1, 1], |
20b87d28 DV |
63 | ['lineTo', 2, 1], |
64 | ['moveTo', 3.2, 3]], extractMoveToAndLineToCalls(htx)); | |
89fdcedb | 65 | }); |
20b87d28 | 66 | |
89fdcedb | 67 | it('testSuperfluousSegmentsElided', function() { |
20b87d28 DV |
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 | ||
89fdcedb | 93 | assert.deepEqual([['moveTo', 0.6, 1], |
20b87d28 DV |
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)); | |
89fdcedb DV |
99 | }); |
100 | ||
101 | }); |