2 * @fileoverview Test cases for the callbacks.
4 * @author uemit.seren@gmail.com (Ümit Seren)
7 var CallbackTestCase
= TestCase("callback");
9 CallbackTestCase
.prototype.setUp
= function() {
10 document
.body
.innerHTML
= "<div id='graph'></div><div id='selection'></div>";
11 this.styleSheet
= document
.createElement("style");
12 this.styleSheet
.type
= "text/css";
13 document
.getElementsByTagName("head")[0].appendChild(this.styleSheet
);
16 CallbackTestCase
.prototype.tearDown
= function() {
19 var data
= "X,a\,b,c\n" +
27 * This tests that when the function idxToRow_ returns the proper row and the onHiglightCallback
28 * is properly called when the first series is hidden (setVisibility = false)
31 CallbackTestCase
.prototype.testHighlightCallbackIsCalled
= function() {
35 var highlightCallback
= function(e
, x
, pts
, row
) {
40 var graph
= document
.getElementById("graph");
41 var g
= new Dygraph(graph
, data
,
45 visibility
: [false, true, true],
46 highlightCallback
: highlightCallback
49 DygraphOps
.dispatchMouseMove(g
, 13, 10);
51 //check correct row is returned
52 assertEquals(3, h_row
);
53 //check there are only two points (because first series is hidden)
54 assertEquals(2, h_pts
.length
);
59 * Test that drawPointCallback isn't called when drawPoints is false
61 CallbackTestCase
.prototype.testDrawPointCallback_disabled
= function() {
64 var callback
= function() {
68 var graph
= document
.getElementById("graph");
69 var g
= new Dygraph(graph
, data
, {
70 drawPointCallback
: callback
,
77 * Test that drawPointCallback is called when drawPoints is true
79 CallbackTestCase
.prototype.testDrawPointCallback_enabled
= function() {
82 var callback
= function() {
86 var graph
= document
.getElementById("graph");
87 var g
= new Dygraph(graph
, data
, {
89 drawPointCallback
: callback
96 * Test that drawPointCallback is called when drawPoints is true
98 CallbackTestCase
.prototype.testDrawPointCallback_pointSize
= function() {
102 var callback
= function(g
, seriesName
, canvasContext
, cx
, cy
, color
, pointSizeParam
) {
103 pointSize
= pointSizeParam
;
107 var graph
= document
.getElementById("graph");
108 var g
= new Dygraph(graph
, data
, {
110 drawPointCallback
: callback
113 assertEquals(1.5, pointSize
);
114 assertEquals(12, count
); // one call per data point.
116 var g
= new Dygraph(graph
, data
, {
118 drawPointCallback
: callback
,
122 assertEquals(8, pointSize
);
126 * This tests that when the function idxToRow_ returns the proper row and the onHiglightCallback
127 * is properly called when the first series is hidden (setVisibility = false)
130 CallbackTestCase
.prototype.testDrawHighlightPointCallbackIsCalled
= function() {
133 var drawHighlightPointCallback
= function() {
137 var graph
= document
.getElementById("graph");
138 var g
= new Dygraph(graph
, data
,
142 drawHighlightPointCallback
: drawHighlightPointCallback
146 DygraphOps
.dispatchMouseMove(g
, 13, 10);
151 * Test the closest-series highlighting methods for normal and stacked modes.
152 * Also pass in line widths for plain and highlighted lines for easier visual
153 * confirmation that the highlighted line is drawn on top of the others.
155 var runClosestTest
= function(isStacked
, widthNormal
, widthHighlighted
) {
160 var graph
= document
.getElementById("graph");
161 var g
= new Dygraph(graph
, data
,
165 visibility
: [false, true, true],
166 stackedGraph
: isStacked
,
167 strokeWidth
: widthNormal
,
168 strokeBorderWidth
: 2,
169 highlightCircleSize
: widthNormal
* 2,
170 highlightSeriesBackgroundAlpha
: 0.3,
172 highlightSeriesOpts
: {
173 strokeWidth
: widthHighlighted
,
174 highlightCircleSize
: widthHighlighted
* 2
178 var highlightCallback
= function(e
, x
, pts
, row
, set
) {
182 document
.getElementById('selection').innerHTML
='row=' + row
+ ', set=' + set
;
185 g
.updateOptions({highlightCallback
: highlightCallback
}, true);
188 DygraphOps
.dispatchMouseMove(g
, 11.45, 1.4);
189 assertEquals(1, h_row
);
190 assertEquals('c', h_series
);
192 //now move up in the same row
193 DygraphOps
.dispatchMouseMove(g
, 11.45, 1.5);
194 assertEquals(1, h_row
);
195 assertEquals('b', h_series
);
197 //and a bit to the right
198 DygraphOps
.dispatchMouseMove(g
, 11.55, 1.5);
199 assertEquals(2, h_row
);
200 assertEquals('c', h_series
);
202 DygraphOps
.dispatchMouseMove(g
, 11, 1.5);
203 assertEquals(1, h_row
);
204 assertEquals('c', h_series
);
206 //now move up in the same row
207 DygraphOps
.dispatchMouseMove(g
, 11, 2.5);
208 assertEquals(1, h_row
);
209 assertEquals('b', h_series
);
216 * Test basic closest-point highlighting.
218 CallbackTestCase
.prototype.testClosestPointCallback
= function() {
219 runClosestTest(false, 1, 3);
223 * Test setSelection() with series name
225 CallbackTestCase
.prototype.testSetSelection
= function() {
226 var g
= runClosestTest(false, 1, 3);
227 assertEquals(1, g
.attr_('strokeWidth', 'c'));
228 g
.setSelection(false, 'c');
229 assertEquals(3, g
.attr_('strokeWidth', 'c'));
233 * Test closest-point highlighting for stacked graph
235 CallbackTestCase
.prototype.testClosestPointStackedCallback
= function() {
236 runClosestTest(true, 1, 3);
240 * Closest-point highlighting with legend CSS - border around active series.
242 CallbackTestCase
.prototype.testClosestPointCallbackCss1
= function() {
243 var css
= "div.dygraph-legend > span { display: block; }\n" +
244 "div.dygraph-legend > span.highlight { border: 1px solid grey; }\n";
245 this.styleSheet
.innerHTML
= css
;
246 runClosestTest(false, 2, 4);
247 this.styleSheet
.innerHTML
= '';
251 * Closest-point highlighting with legend CSS - show only closest series.
253 CallbackTestCase
.prototype.testClosestPointCallbackCss2
= function() {
254 var css
= "div.dygraph-legend > span { display: none; }\n" +
255 "div.dygraph-legend > span.highlight { display: inline; }\n";
256 this.styleSheet
.innerHTML
= css
;
257 runClosestTest(false, 10, 15);
258 this.styleSheet
.innerHTML
= '';
259 // TODO(klausw): verify that the highlighted line is drawn on top?
263 * This tests that closest point searches work for data containing NaNs.
265 * It's intended to catch a regression where a NaN Y value confuses the
266 * closest-point algorithm, treating it as closer as any previous point.
268 CallbackTestCase
.prototype.testNaNData
= function() {
280 var highlightCallback
= function(e
, x
, pts
, row
) {
285 var graph
= document
.getElementById("graph");
286 var g
= new Dygraph(graph
, dataNaN
,
290 labels
: ['x', 'a', 'b', 'c'],
291 visibility
: [false, true, true],
292 highlightCallback
: highlightCallback
295 DygraphOps
.dispatchMouseMove(g
, 10.1, 0.9);
296 //check correct row is returned
297 assertEquals(1, h_row
);
299 // Explicitly test closest point algorithms
300 var dom
= g
.toDomCoords(10.1, 0.9);
301 assertEquals(1, g
.findClosestRow(dom
[0]));
303 var res
= g
.findClosestPoint(dom
[0], dom
[1]);
304 assertEquals(1, res
.row
);
305 assertEquals('b', res
.seriesName
);
307 res
= g
.findStackedPoint(dom
[0], dom
[1]);
308 assertEquals(1, res
.row
);
309 assertEquals('c', res
.seriesName
);