Commit | Line | Data |
---|---|---|
644eff8b RK |
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 cases that ensure Dygraphs works at all. | |
24 | * | |
25 | * @author konigsberg@google.com (Robert Konigsberg) | |
26 | */ | |
27 | var DEAD_SIMPLE_DATA = [[ 20061010, 2100 ]]; | |
28 | var ZERO_TO_FIFTY = [[ 20061010, 0 ] , [ 20061011, 50 ]]; | |
29 | ||
30 | var SanityTestCase = TestCase("dygraphs-sanity"); | |
31 | ||
32 | SanityTestCase.prototype.setUp = function() { | |
33 | document.body.innerHTML = "<div id='graph'></div>"; | |
34 | }; | |
35 | ||
36 | /** | |
37 | * The sanity test of sanity tests. | |
38 | */ | |
39 | SanityTestCase.prototype.testTrue = function() { | |
40 | assertTrue(true); | |
41 | }; | |
42 | ||
43 | /** | |
44 | * Sanity test that ensures the graph element exists. | |
45 | */ | |
46 | SanityTestCase.prototype.testGraphExists = function() { | |
47 | var graph = document.getElementById("graph"); | |
48 | assertNotNull(graph); | |
49 | }; | |
50 | ||
51 | /** | |
52 | * A sanity test of sorts, by ensuring the dygraph is created, and | |
53 | * isn't just some piece of junk object. | |
54 | */ | |
55 | SanityTestCase.prototype.testToString = function() { | |
56 | var graph = document.getElementById("graph"); | |
57 | var g = new Dygraph(graph, DEAD_SIMPLE_DATA, {}); | |
58 | assertNotNull(g); | |
59 | assertEquals("[Dygraph graph]", g.toString()); | |
60 | }; | |
61 | ||
62 | /** | |
63 | * Test that when no valueRange is specified, the y axis range is | |
64 | * adjusted by 10% on top. | |
65 | */ | |
66 | SanityTestCase.prototype.testYAxisRange_default = function() { | |
67 | var graph = document.getElementById("graph"); | |
68 | assertEquals(0, graph.style.length); | |
69 | var g = new Dygraph(graph, ZERO_TO_FIFTY, {}); | |
70 | assertEquals([0, 55], g.yAxisRange(0)); | |
71 | }; | |
72 | ||
73 | /** | |
74 | * Test that valueRange matches the y-axis range specifically. | |
75 | */ | |
76 | SanityTestCase.prototype.testYAxisRange_custom = function() { | |
77 | var graph = document.getElementById("graph"); | |
78 | var g = new Dygraph(graph, ZERO_TO_FIFTY, { valueRange: [0,50] }); | |
79 | assertEquals([0, 50], g.yAxisRange(0)); | |
80 | }; | |
81 | ||
82 | function assertEqualsDelta(msg, expected, actual, delta) { | |
83 | var args = this.argsWithOptionalMsg_(arguments, 4); | |
84 | ||
85 | var message = args[0]; | |
86 | var exp = args[1]; | |
87 | var act = args[2]; | |
88 | var d = args[3]; | |
89 | if (Math.abs(exp - act) > d) { | |
90 | fail(message + | |
91 | " Expected to be within " + d + " of " + exp + ", got " + act); | |
92 | } | |
93 | } | |
94 | ||
95 | /** | |
96 | * Test that valueRange matches the y-axis range specifically. | |
97 | * | |
98 | * This is based on the assumption that 20 pixels are dedicated to the | |
99 | * axis label and tick marks. | |
100 | * TODO(konigsberg): change yAxisLabelWidth to 0 (or 20) and try again. | |
101 | */ | |
102 | SanityTestCase.prototype.testToDomYCoord = function() { | |
103 | var graph = document.getElementById("graph"); | |
104 | var g = new Dygraph(graph, ZERO_TO_FIFTY, { height: 70, valueRange: [0,50] }); | |
105 | ||
106 | assertEquals(50, g.toDomYCoord(0)); | |
107 | assertEquals(0, g.toDomYCoord(50)); | |
108 | ||
109 | for (var x = 0; x <= 50; x++) { | |
110 | assertEqualsDelta(50 - x, g.toDomYCoord(x), 0.00001); | |
111 | } | |
112 | }; |