Add some power to the canvas assertions. Also, the canvas assertions weren't actually...
[dygraphs.git] / auto_tests / tests / range_tests.js
1 // Copyright (c) 2011 Google, Inc.
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 /**
23 * @fileoverview Test valueRange and dateWindow changes.
24 *
25 * @author konigsberg@google.com (Robert Konigsberg)
26 */
27 var ZERO_TO_FIFTY = [[ 10, 0 ] , [ 20, 50 ]];
28 var ZERO_TO_FIFTY_STEPS = function() {
29 var a = [];
30 var x = 10;
31 var y = 0;
32 var step = 0;
33 for (step = 0; step <= 50; step++) {
34 a.push([x + (step * .2), y + step]);
35 }
36 return a;
37 } ();
38
39 var RangeTestCase = TestCase("range-tests");
40
41 RangeTestCase.prototype.setUp = function() {
42 document.body.innerHTML = "<div id='graph'></div>";
43 };
44
45 RangeTestCase.prototype.createGraph = function(opts) {
46 var graph = document.getElementById("graph");
47 var g = new Dygraph(graph, ZERO_TO_FIFTY_STEPS, opts);
48
49 assertEquals([10, 20], g.xAxisRange());
50 assertEquals([0, 55], g.yAxisRange(0));
51
52 return g;
53 };
54
55 /**
56 * Test that changes to valueRange and dateWindow are reflected
57 * appropriately.
58 */
59 RangeTestCase.prototype.testRangeSetOperations = function() {
60 var g = this.createGraph({valueRange : [ 0, 55 ]});
61
62 g.updateOptions({ dateWindow : [ 12, 18 ] });
63 assertEquals([12, 18], g.xAxisRange());
64 assertEquals([0, 55], g.yAxisRange(0));
65
66 g.updateOptions({ valueRange : [ 10, 40 ] });
67 assertEquals([12, 18], g.xAxisRange());
68 assertEquals([10, 40], g.yAxisRange(0));
69
70 g.updateOptions({ });
71 assertEquals([12, 18], g.xAxisRange());
72 assertEquals([10, 40], g.yAxisRange(0));
73
74 g.updateOptions({valueRange : null, axes: {y:{valueRange : [15, 20]}}});
75 assertEquals([12, 18], g.xAxisRange());
76 assertEquals([15, 20], g.yAxisRange(0));
77
78 g.updateOptions({ dateWindow : null, valueRange : null, axes: null });
79 assertEquals([10, 20], g.xAxisRange());
80 assertEquals([0, 55], g.yAxisRange(0));
81 };
82
83 /**
84 * Verify that when zoomed in by mouse operations, an empty call to
85 * updateOptions doesn't change the displayed ranges.
86 */
87 RangeTestCase.prototype.zoom = function(g, xRange, yRange) {
88 var originalXRange = g.xAxisRange();
89 var originalYRange = g.yAxisRange(0);
90
91 DygraphOps.dispatchMouseDown(g, xRange[0], yRange[0]);
92 DygraphOps.dispatchMouseMove(g, xRange[1], yRange[0]); // this is really necessary.
93 DygraphOps.dispatchMouseUp(g, xRange[1], yRange[0]);
94
95 assertEqualsDelta(xRange, g.xAxisRange(), 0.2);
96 // assertEqualsDelta(originalYRange, g.yAxisRange(0), 0.2); // Not true, it's something in the middle.
97
98 var midX = (xRange[1] - xRange[0]) / 2;
99 DygraphOps.dispatchMouseDown(g, midX, yRange[0]);
100 DygraphOps.dispatchMouseMove(g, midX, yRange[1]); // this is really necessary.
101 DygraphOps.dispatchMouseUp(g, midX, yRange[1]);
102
103 assertEqualsDelta(xRange, g.xAxisRange(), 0.2);
104 assertEqualsDelta(yRange, g.yAxisRange(0), 0.2);
105 }
106
107
108 /**
109 * Verify that when zoomed in by mouse operations, an empty call to
110 * updateOptions doesn't change the displayed ranges.
111 */
112 RangeTestCase.prototype.testEmptyUpdateOptions_doesntUnzoom = function() {
113 var g = this.createGraph();
114 this.zoom(g, [ 11, 18 ], [ 35, 40 ]);
115
116 assertEqualsDelta([11, 18], g.xAxisRange(), 0.1);
117 assertEqualsDelta([35, 40], g.yAxisRange(0), 0.2);
118
119 g.updateOptions({});
120
121 assertEqualsDelta([11, 18], g.xAxisRange(), 0.1);
122 assertEqualsDelta([35, 40], g.yAxisRange(0), 0.2);
123 }
124
125 /**
126 * Verify that when zoomed in by mouse operations, a call to
127 * updateOptions({ dateWindow : null, valueRange : null }) fully
128 * unzooms.
129 */
130 RangeTestCase.prototype.testRestoreOriginalRanges_viaUpdateOptions = function() {
131 var g = this.createGraph();
132 this.zoom(g, [ 11, 18 ], [ 35, 40 ]);
133
134 g.updateOptions({ dateWindow : null, valueRange : null });
135
136 assertEquals([0, 55], g.yAxisRange(0));
137 assertEquals([10, 20], g.xAxisRange());
138 }