1 // Copyright 2011 Google Inc. All Rights Reserved.
4 * @fileoverview Regression test based on some strange customBars data.
5 * @author danvk@google.com (Dan Vanderkam)
7 var CssTestCase
= TestCase("css");
9 CssTestCase
.data
= "X,Y,Z\n1,2,3\n4,5,6\n";
11 CssTestCase
.prototype.setUp
= function() {
12 document
.body
.innerHTML
= "<div id='graph'></div>";
13 this.styleSheet
= document
.createElement("style");
14 this.styleSheet
.type
= "text/css";
15 document
.getElementsByTagName("head")[0].appendChild(this.styleSheet
);
18 CssTestCase
.prototype.tearDown
= function() {
21 // Verifies that an unstyled, unsized dygraph gets a default size.
22 CssTestCase
.prototype.testDefaultSize
= function() {
25 var graph
= document
.getElementById("graph");
26 var g
= new Dygraph(graph
, CssTestCase
.data
, opts
);
28 assertEquals(480, graph
.offsetWidth
);
29 assertEquals(320, graph
.offsetHeight
);
30 assertEquals({width
: 480, height
: 320}, g
.size());
33 // Verifies that the width/height parameters work
.
34 CssTestCase
.prototype.testExplicitParamSize
= function() {
39 var graph
= document
.getElementById("graph");
40 var g
= new Dygraph(graph
, CssTestCase
.data
, opts
);
42 assertEquals(640, graph
.offsetWidth
);
43 assertEquals(480, graph
.offsetHeight
);
44 assertEquals({width
: 640, height
: 480}, g
.size());
47 // Verifies that setting a style on the div works.
48 CssTestCase
.prototype.testExplicitStyleSize
= function() {
51 var graph
= document
.getElementById("graph");
52 graph
.style
.width
= '600px';
53 graph
.style
.height
= '400px';
55 var g
= new Dygraph(graph
, CssTestCase
.data
, opts
);
56 assertEquals(600, graph
.offsetWidth
);
57 assertEquals(400, graph
.offsetHeight
);
58 assertEquals({width
: 600, height
: 400}, g
.size());
61 // Verifies that CSS pixel styles on the div trump explicit parameters.
62 CssTestCase
.prototype.testPixelStyleWins
= function() {
67 var graph
= document
.getElementById("graph");
68 graph
.style
.width
= '600px';
69 graph
.style
.height
= '400px';
71 var g
= new Dygraph(graph
, CssTestCase
.data
, opts
);
72 assertEquals(600, graph
.offsetWidth
);
73 assertEquals(400, graph
.offsetHeight
);
74 assertEquals({width
: 600, height
: 400}, g
.size());
77 // Verifies that a CSS percentage size works.
78 CssTestCase
.prototype.testPercentageSize
= function() {
79 document
.body
.innerHTML
=
80 '<div style="width: 600px; height: 400px;">' +
81 '<div id="graph"></div></div>';
84 var graph
= document
.getElementById("graph");
85 graph
.style
.width
= '50%';
86 graph
.style
.height
= '50%';
88 var g
= new Dygraph(graph
, CssTestCase
.data
, opts
);
89 assertEquals(300, graph
.offsetWidth
);
90 assertEquals(200, graph
.offsetHeight
);
91 assertEquals({width
: 300, height
: 200}, g
.size());
94 // Verifies that a CSS class size works.
95 CssTestCase
.prototype.testClassPixelSize
= function() {
96 this.styleSheet
.innerHTML
= '.chart { width: 456px; height: 345px; }';
100 var graph
= document
.getElementById("graph");
101 graph
.className
= "chart";
102 var g
= new Dygraph(graph
, CssTestCase
.data
, opts
);
103 assertEquals(456, graph
.offsetWidth
);
104 assertEquals(345, graph
.offsetHeight
);
105 assertEquals({width
: 456, height
: 345}, g
.size());
108 // An invisible chart div shouldn't produce an error.
109 CssTestCase
.prototype.testInvisibleChart
= function() {
110 document
.body
.innerHTML
=
111 '<div style="display:none;">' +
112 '<div id="graph" style="width: 640px; height: 480px;"></div>' +
114 var graph
= document
.getElementById("graph");
115 g
= new Dygraph(graph
, CssTestCase
.data
, {});
118 // An invisible chart div shouldn't produce an error.
119 CssTestCase
.prototype.testInvisibleChartDate
= function() {
120 document
.body
.innerHTML
=
121 '<div style="display:none;">' +
122 '<div id="graph" style="width: 640px; height: 480px;"></div>' +
124 var graph
= document
.getElementById("graph");
125 g
= new Dygraph(graph
,
136 // An invisible chart div that becomes visible.
137 CssTestCase
.prototype.testInvisibleThenVisibleChart
= function() {
138 document
.body
.innerHTML
=
139 '<div id="x" style="display:none;">' +
140 '<div id="graph" style="width: 640px; height: 480px;"></div>' +
142 var graph
= document
.getElementById("graph");
143 g
= new Dygraph(graph
,
153 // g.size() is undefined here (probably 0x0)
154 document
.getElementById("x").style
.display
= "";
156 // This resize() call is annoying but essential.
157 // There are no DOM events to inform the dygraph that its div has changed size
158 // or visibility so we need to let it know ourselves.
161 assertEquals(640, graph
.offsetWidth
);
162 assertEquals(480, graph
.offsetHeight
);
163 assertEquals({width
: 640, height
: 480}, g
.size());
166 // Verifies that a div resize gets picked up.
168 this one isn't quite ready yet.
169 CssTestCase.prototype.testDivResize = function() {
172 var graph = document.getElementById("graph");
173 graph.style.width = '640px';
174 graph.style.height = '480px';
175 var g = new Dygraph(graph, CssTestCase.data, opts);
177 assertEquals(640, graph.offsetWidth);
178 assertEquals(480, graph.offsetHeight);
179 assertEquals({width: 640, height: 480}, g.size());
181 graph.style.width = '650px';
182 graph.style.height = '490px';
183 assertEquals(650, graph.offsetWidth);
184 assertEquals(490, graph.offsetHeight);
185 assertEquals({width: 650, height: 490}, g.size());