Fork off Karma file for in-browser debug
[dygraphs.git] / auto_tests / tests / simple_drawing.js
CommitLineData
718ad8e2 1// Copyright (c) 2011 Google, Inc.
644eff8b
RK
2//
3// Permission is hereby granted, free of charge, to any person obtaining a copy
4// of this software and associated documentation files (the "Software"), to deal
5// in the Software without restriction, including without limitation the rights
6// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7// copies of the Software, and to permit persons to whom the Software is
8// furnished to do so, subject to the following conditions:
9//
10// The above copyright notice and this permission notice shall be included in
11// all copies or substantial portions of the Software.
12//
13// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19// THE SOFTWARE.
20
21/**
22 * @fileoverview Test cases for drawing simple lines.
23 *
24 * @author konigsberg@google.com (Robert Konigsberg)
25 */
644eff8b 26
89fdcedb 27describe("simple-drawing", function() {
644eff8b 28
1b7afc93
DV
29var ZERO_TO_FIFTY = 'X,Y\n10,0\n20,50';
30
319d0361 31var _origFunc = Dygraph.getContext;
89fdcedb 32beforeEach(function() {
644eff8b 33 document.body.innerHTML = "<div id='graph'></div>";
063e83ba 34 Dygraph.getContext = function(canvas) {
319d0361 35 return new Proxy(_origFunc(canvas));
063e83ba 36 }
89fdcedb 37});
644eff8b 38
89fdcedb 39afterEach(function() {
319d0361 40 Dygraph.getContext = _origFunc;
89fdcedb 41});
644eff8b 42
89fdcedb 43it('testDrawSimpleRangePlusOne', function() {
644eff8b 44 var opts = {
1b7afc93
DV
45 axes: {
46 x: {
bfb3e0a4 47 drawGrid: false,
1b7afc93 48 drawAxis: false
bfb3e0a4 49 },
1b7afc93 50 y: {
bfb3e0a4 51 drawGrid: false,
1b7afc93 52 drawAxis: false
bfb3e0a4
RK
53 }
54 },
1b7afc93
DV
55 valueRange: [0,51]
56 };
644eff8b
RK
57
58 var graph = document.getElementById("graph");
59 var g = new Dygraph(graph, ZERO_TO_FIFTY, opts);
c20eabd3 60 var htx = g.hidden_ctx_;
644eff8b 61
124e8d98 62 CanvasAssertions.assertLineDrawn(htx, [0,320], [475,6.2745], {
063e83ba
DV
63 strokeStyle: "#008080",
64 lineWidth: 1
65 });
b941564d 66 g.destroy(); // to balance context saves and destroys.
6278f6fe 67 CanvasAssertions.assertBalancedSaveRestore(htx);
89fdcedb 68});
6278f6fe 69
7aabde96 70// See http://code.google.com/p/dygraphs/issues/detail?id=185
89fdcedb 71it('testDrawSimpleRangeZeroToFifty', function() {
7aabde96 72 var opts = {
bfb3e0a4
RK
73 axes : {
74 x : {
75 drawGrid: false,
76 drawAxis: false,
77 },
78 y : {
79 drawGrid: false,
80 drawAxis: false,
81 }
82 },
7aabde96
DV
83 valueRange: [0,50] }
84
85 var graph = document.getElementById("graph");
86 var g = new Dygraph(graph, ZERO_TO_FIFTY, opts);
87 var htx = g.hidden_ctx_;
88
89 var lines = CanvasAssertions.getLinesDrawn(htx, {
90 strokeStyle: "#008080",
91 lineWidth: 1
92 });
89fdcedb 93 assert.equal(1, lines.length);
b941564d 94 g.destroy(); // to balance context saves and destroys.
7aabde96 95 CanvasAssertions.assertBalancedSaveRestore(htx);
89fdcedb 96});
7aabde96 97
89fdcedb 98it('testDrawWithAxis', function() {
6278f6fe
DV
99 var graph = document.getElementById("graph");
100 var g = new Dygraph(graph, ZERO_TO_FIFTY);
101
102 var htx = g.hidden_ctx_;
b941564d 103 g.destroy(); // to balance context saves and destroys.
6278f6fe 104 CanvasAssertions.assertBalancedSaveRestore(htx);
89fdcedb 105});
79253bd0 106
107/**
108 * Tests that it is drawing dashes, and it remember the dash history between
109 * points.
110 */
89fdcedb 111it('testDrawSimpleDash', function() {
79253bd0 112 var opts = {
c715be42
DV
113 axes: {
114 x: {
115 drawGrid: false,
116 drawAxis: false
117 },
118 y: {
119 drawGrid: false,
120 drawAxis: false
121 }
122 },
8887663f 123 series: {
79253bd0 124 'Y1': {strokePattern: [25, 7, 7, 7]},
8887663f 125 },
1b7afc93
DV
126 colors: ['#ff0000'],
127 labels: ['X', 'Y']
79253bd0 128 };
129
130 var graph = document.getElementById("graph");
131 // Set the dims so we pass if default changes.
132 graph.style.width='480px';
133 graph.style.height='320px';
134 var g = new Dygraph(graph, [[1, 4], [2, 5], [3, 3], [4, 7], [5, 9]], opts);
89fdcedb 135 var htx = g.hidden_ctx_;
79253bd0 136
00b21593 137 // TODO(danvk): figure out a good way to restore this test.
89fdcedb 138 // assert.equal(29, CanvasAssertions.numLinesDrawn(htx, "#ff0000"));
b941564d 139 g.destroy(); // to balance context saves and destroys.
6278f6fe 140 CanvasAssertions.assertBalancedSaveRestore(htx);
89fdcedb 141});
9f636500
DV
142
143/**
144 * Tests that thick lines are drawn continuously.
145 * Regression test for http://code.google.com/p/dygraphs/issues/detail?id=328
146 */
89fdcedb 147it('testDrawThickLine', function() {
9f636500 148 var opts = {
1b7afc93
DV
149 axes: {
150 x: {
bfb3e0a4 151 drawGrid: false,
1b7afc93 152 drawAxis: false
bfb3e0a4 153 },
1b7afc93 154 y: {
bfb3e0a4 155 drawGrid: false,
1b7afc93 156 drawAxis: false
bfb3e0a4
RK
157 }
158 },
9f636500 159 strokeWidth: 15,
1b7afc93
DV
160 colors: ['#ff0000'],
161 labels: ['X', 'Y']
9f636500
DV
162 };
163
164 var graph = document.getElementById("graph");
165 // Set the dims so we pass if default changes.
166 graph.style.width='480px';
167 graph.style.height='320px';
168 var g = new Dygraph(graph, [[1, 2], [2, 5], [3, 2], [4, 7], [5, 0]], opts);
89fdcedb 169 var htx = g.hidden_ctx_;
9f636500
DV
170
171 // There's a big gap in the line at (2, 5)
172 // If the bug is fixed, then there should be some red going up from here.
173 var xy = g.toDomCoords(2, 5);
174 var x = Math.round(xy[0]), y = Math.round(xy[1]);
175
176 var sampler = new PixelSampler(g);
89fdcedb
DV
177 assert.deepEqual([255,0,0,255], sampler.colorAtPixel(x, y));
178 assert.deepEqual([255,0,0,255], sampler.colorAtPixel(x, y - 1));
179 assert.deepEqual([255,0,0,255], sampler.colorAtPixel(x, y - 2));
9f636500
DV
180
181 // TODO(danvk): figure out a good way to restore this test.
89fdcedb 182 // assert.equal(29, CanvasAssertions.numLinesDrawn(htx, "#ff0000"));
b941564d 183 g.destroy(); // to balance context saves and destroys.
9f636500 184 CanvasAssertions.assertBalancedSaveRestore(htx);
89fdcedb
DV
185});
186
187});