1 // Copyright (c) 2011 Google, Inc.
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:
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
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
23 * @fileoverview Test cases that ensure Dygraphs works at all.
25 * @author konigsberg@google.com (Robert Konigsberg)
27 var DEAD_SIMPLE_DATA
= [[ 10, 2100 ]];
28 var ZERO_TO_FIFTY
= [[ 10, 0 ] , [ 20, 50 ]];
30 var SanityTestCase
= TestCase("dygraphs-sanity");
32 SanityTestCase
.prototype.setUp
= function() {
33 document
.body
.innerHTML
= "<div id='graph'></div>";
37 * The sanity test of sanity tests.
39 SanityTestCase
.prototype.testTrue
= function() {
44 * Sanity test that ensures the graph element exists.
46 SanityTestCase
.prototype.testGraphExists
= function() {
47 var graph
= document
.getElementById("graph");
51 // TODO(konigsberg): Move the following tests to a new package that
52 // tests all kinds of toDomCoords, toDataCoords, toPercent, et cetera.
55 * A sanity test of sorts, by ensuring the dygraph is created, and
56 * isn't just some piece of junk object.
58 SanityTestCase
.prototype.testToString
= function() {
59 var graph
= document
.getElementById("graph");
60 var g
= new Dygraph(graph
, DEAD_SIMPLE_DATA
, {});
62 assertEquals("[Dygraph graph]", g
.toString());
66 * Test that when no valueRange is specified, the y axis range is
67 * adjusted by 10% on top.
69 SanityTestCase
.prototype.testYAxisRange_default
= function() {
70 var graph
= document
.getElementById("graph");
71 assertEquals(0, graph
.style
.length
);
72 var g
= new Dygraph(graph
, ZERO_TO_FIFTY
, {});
73 assertEquals([0, 55], g
.yAxisRange(0));
77 * Test that valueRange matches the y-axis range specifically.
79 SanityTestCase
.prototype.testYAxisRange_custom
= function() {
80 var graph
= document
.getElementById("graph");
81 var g
= new Dygraph(graph
, ZERO_TO_FIFTY
, { valueRange
: [0,50] });
82 assertEquals([0, 50], g
.yAxisRange(0));
86 * Test that valueRange matches the y-axis range specifically.
88 * This is based on the assumption that 20 pixels are dedicated to the
89 * axis label and tick marks.
90 * TODO(konigsberg): change yAxisLabelWidth to 0 (or 20) and try again.
92 SanityTestCase
.prototype.testToDomYCoord
= function() {
93 var graph
= document
.getElementById("graph");
94 var g
= new Dygraph(graph
, ZERO_TO_FIFTY
, { height
: 70, valueRange
: [0,50] });
96 assertEquals(50, g
.toDomYCoord(0));
97 assertEquals(0, g
.toDomYCoord(50));
99 for (var x
= 0; x
<= 50; x
++) {
100 assertEqualsDelta(50 - x
, g
.toDomYCoord(x
), 0.00001);
105 * Test that the two-argument form of the constructor (no options) works.
107 SanityTestCase
.prototype.testTwoArgumentConstructor
= function() {
108 var graph
= document
.getElementById("graph");
109 new Dygraph(graph
, ZERO_TO_FIFTY
);
112 // Here is the first of a series of tests that just ensure the graph is drawn
113 // without exception.
114 //TODO(konigsberg): Move to its own test case.
115 SanityTestCase
.prototype.testFillStack1
= function() {
116 var graph
= document
.getElementById("graph");
117 new Dygraph(graph
, ZERO_TO_FIFTY
, { stackedGraph
: true });
120 SanityTestCase
.prototype.testFillStack2
= function() {
121 var graph
= document
.getElementById("graph");
122 new Dygraph(graph
, ZERO_TO_FIFTY
, { stackedGraph
: true, fillGraph
: true });
125 SanityTestCase
.prototype.testFillStack3
= function() {
126 var graph
= document
.getElementById("graph");
127 new Dygraph(graph
, ZERO_TO_FIFTY
, { fillGraph
: true });