1 // Copyright 2011 Google Inc. All Rights Reserved.
4 * @fileoverview Regression tests for range selector.
5 * @author paul.eric.felix@gmail.com (Paul Felix)
7 var RangeSelectorTestCase
= TestCase("range-selector");
9 RangeSelectorTestCase
.prototype.setUp
= function() {
10 document
.body
.innerHTML
= "<div id='graph'></div>";
13 RangeSelectorTestCase
.prototype.tearDown
= function() {
16 RangeSelectorTestCase
.prototype.testRangeSelector
= function() {
20 showRangeSelector
: true,
34 var graph
= document
.getElementById("graph");
35 var g
= new Dygraph(graph
, data
, opts
);
36 this.assertGraphExistence(g
, graph
);
39 RangeSelectorTestCase
.prototype.testRangeSelectorWithErrorBars
= function() {
44 showRangeSelector
: true,
58 var graph
= document
.getElementById("graph");
59 var g
= new Dygraph(graph
, data
, opts
);
60 this.assertGraphExistence(g
, graph
);
63 RangeSelectorTestCase
.prototype.testRangeSelectorWithCustomBars
= function() {
68 showRangeSelector
: true,
82 var graph
= document
.getElementById("graph");
83 var g
= new Dygraph(graph
, data
, opts
);
84 this.assertGraphExistence(g
, graph
);
87 RangeSelectorTestCase
.prototype.testRangeSelectorWithLogScale
= function() {
92 showRangeSelector
: true,
106 var graph
= document
.getElementById("graph");
107 var g
= new Dygraph(graph
, data
, opts
);
108 this.assertGraphExistence(g
, graph
);
111 RangeSelectorTestCase
.prototype.testRangeSelectorOptions
= function() {
115 showRangeSelector
: true,
116 rangeSelectorHeight
: 30,
117 rangeSelectorPlotFillColor
: 'lightyellow',
131 var graph
= document
.getElementById("graph");
132 var g
= new Dygraph(graph
, data
, opts
);
133 this.assertGraphExistence(g
, graph
);
136 RangeSelectorTestCase
.prototype.testRangeSelectorEnablingAfterCreation
= function() {
153 var graph
= document
.getElementById("graph");
154 var g
= new Dygraph(graph
, data
, opts
);
155 g
.updateOptions({showRangeSelector
: true});
156 this.assertGraphExistence(g
, graph
);
159 // The animatedZooms option does not work with the range selector. Make sure it gets turned off.
160 RangeSelectorTestCase
.prototype.testRangeSelectorWithAnimatedZoomsOption
= function() {
164 showRangeSelector
: true,
179 var graph
= document
.getElementById("graph");
180 var g
= new Dygraph(graph
, data
, opts
);
181 this.assertGraphExistence(g
, graph
);
182 assertFalse(g
.getOption('animatedZooms'));
185 RangeSelectorTestCase
.prototype.testRangeSelectorWithAnimatedZoomsOption2
= function() {
203 var graph
= document
.getElementById("graph");
204 var g
= new Dygraph(graph
, data
, opts
);
205 g
.updateOptions({showRangeSelector
: true});
206 this.assertGraphExistence(g
, graph
);
207 assertFalse(g
.getOption('animatedZooms'));
210 RangeSelectorTestCase
.prototype.testRangeSelectorInteraction
= function() {
214 showRangeSelector
: true,
228 var graph
= document
.getElementById("graph");
229 var g
= new Dygraph(graph
, data
, opts
);
230 this.assertGraphExistence(g
, graph
);
231 var zoomhandles
= graph
.getElementsByClassName('dygraph-rangesel-zoomhandle');
233 // Move left zoomhandle in
234 var xRange
= g
.xAxisRange().slice();
236 var mouseDownEvent
= DygraphOps
.createEvent({
242 zoomhandles
[0].dispatchEvent(mouseDownEvent
);
244 var mouseMoveEvent
= DygraphOps
.createEvent({
249 zoomhandles
[0].dispatchEvent(mouseMoveEvent
);
251 var mouseUpEvent
= DygraphOps
.createEvent({
257 zoomhandles
[0].dispatchEvent(mouseUpEvent
);
259 var newXRange
= g
.xAxisRange().slice();
260 assert('left zoomhandle should have moved: '+newXRange
[0]+'>'+xRange
[0], newXRange
[0] > xRange
[0]);
261 assertEquals('right zoomhandle should not have moved', xRange
[1], newXRange
[1]);
263 // Move right zoomhandle in
266 mouseDownEvent
= DygraphOps
.createEvent({
272 zoomhandles
[1].dispatchEvent(mouseDownEvent
);
274 mouseMoveEvent
= DygraphOps
.createEvent({
279 zoomhandles
[1].dispatchEvent(mouseMoveEvent
);
281 mouseUpEvent
= DygraphOps
.createEvent({
287 zoomhandles
[1].dispatchEvent(mouseUpEvent
);
289 var newXRange
= g
.xAxisRange().slice();
290 assert('right zoomhandle should have moved: '+newXRange
[1]+'<'+xRange
[1], newXRange
[1] < xRange
[1]);
291 assertEquals('left zoomhandle should not have moved', xRange
[0], newXRange
[0]);
295 var fgcanvas
= graph
.getElementsByClassName('dygraph-rangesel-fgcanvas')[0];
296 var x
= parseInt(zoomhandles
[0].style
.left
) + 20;
297 var y
= parseInt(zoomhandles
[0].style
.top
);
299 mouseDownEvent
= DygraphOps
.createEvent({
305 fgcanvas
.dispatchEvent(mouseDownEvent
);
309 mouseMoveEvent
= DygraphOps
.createEvent({
314 fgcanvas
.dispatchEvent(mouseMoveEvent
);
316 mouseUpEvent
= DygraphOps
.createEvent({
322 fgcanvas
.dispatchEvent(mouseUpEvent
);
324 var newXRange
= g
.xAxisRange().slice();
325 assert(newXRange
[0]+'<'+xRange
[0], newXRange
[0] < xRange
[0]);
326 assert(newXRange
[1]+'<'+xRange
[1], newXRange
[1] < xRange
[1]);
330 RangeSelectorTestCase
.prototype.testRangeSelectorPositionIfXAxisNotDrawn
= function() {
335 axes
: { x
: { drawAxis
: false }},
336 showRangeSelector
: true,
337 rangeSelectorHeight
: 30,
344 var graph
= document
.getElementById("graph");
345 var g
= new Dygraph(graph
, data
, opts
);
347 //assert, that the range selector is at top position 70 since the 30px of the
348 // xAxis shouldn't be reserved since it isn't drawn.
349 this.assertGraphExistence(g
, graph
);
350 var bgcanvas
= graph
.getElementsByClassName('dygraph-rangesel-bgcanvas')[0];
351 assertEquals("Range selector is not at the expected position.","70px", bgcanvas
.style
.top
);
352 var fgcanvas
= graph
.getElementsByClassName('dygraph-rangesel-fgcanvas')[0];
353 assertEquals("Range selector is not at the expected position.","70px", fgcanvas
.style
.top
);
356 RangeSelectorTestCase
.prototype.testMiniPlotDrawn
= function() {
357 // Install Proxy to track canvas calls.
358 var origFunc
= Dygraph
.getContext
;
360 Dygraph
.getContext
= function(canvas
) {
361 console
.log(canvas
.className
);
362 if (canvas
.className
!= 'dygraph-rangesel-bgcanvas') {
363 return origFunc(canvas
);
365 miniHtx
= new Proxy(origFunc(canvas
));
373 axes
: { x
: { drawAxis
: false }},
374 showRangeSelector
: true,
375 rangeSelectorHeight
: 30,
376 rangeSelectorPlotStrokeColor
: '#ff0000',
384 var graph
= document
.getElementById("graph");
385 var g
= new Dygraph(graph
, data
, opts
);
387 // TODO(danvk): more precise tests.
388 assertNotNull(miniHtx
);
389 assertTrue(0 < CanvasAssertions
.numLinesDrawn(miniHtx
, '#ff0000'));
391 Dygraph
.getContext
= origFunc
;
394 // Tests data computation for the mini plot with a single series.
395 RangeSelectorTestCase
.prototype.testSingleCombinedSeries
= function() {
397 showRangeSelector
: true,
405 var graph
= document
.getElementById("graph");
406 var g
= new Dygraph(graph
, data
, opts
);
408 var rangeSelector
= g
.getPluginInstance_(Dygraph
.Plugins
.RangeSelector
);
409 assertNotNull(rangeSelector
);
411 var combinedSeries
= rangeSelector
.computeCombinedSeriesAndLimits_();
413 yMin
: 1 - 7 * 0.25, // 25% padding
424 // Tests that multiple series are averaged for the miniplot.
425 RangeSelectorTestCase
.prototype.testCombinedSeries
= function() {
427 showRangeSelector
: true,
428 labels
: ['X', 'Y1', 'Y2']
431 [0, 1, 3], // average = 2
432 [5, 4, 6], // average = 5
433 [10, 7, 9] // average = 8
435 var graph
= document
.getElementById("graph");
436 var g
= new Dygraph(graph
, data
, opts
);
438 var rangeSelector
= g
.getPluginInstance_(Dygraph
.Plugins
.RangeSelector
);
439 assertNotNull(rangeSelector
);
441 var combinedSeries
= rangeSelector
.computeCombinedSeriesAndLimits_();
443 yMin
: 2 - 6 * 0.25, // 25% padding on combined series range.
453 // Tests selection of a specific series to average for the mini plot.
454 RangeSelectorTestCase
.prototype.testSelectedCombinedSeries
= function() {
456 showRangeSelector
: true,
457 labels
: ['X', 'Y1', 'Y2', 'Y3', 'Y4'],
459 'Y1': { showInRangeSelector
: true },
460 'Y3': { showInRangeSelector
: true }
464 [0, 5, 8, 13, 21], // average (first and third) = 9
465 [5, 1, 3, 7, 14], // average (first and third) = 4
466 [10, 0, 19, 10, 6] // average (first and third) = 5
468 var graph
= document
.getElementById("graph");
469 var g
= new Dygraph(graph
, data
, opts
);
471 var rangeSelector
= g
.getPluginInstance_(Dygraph
.Plugins
.RangeSelector
);
472 assertNotNull(rangeSelector
);
474 var combinedSeries
= rangeSelector
.computeCombinedSeriesAndLimits_();
476 yMin
: 4 - 5 * 0.25, // 25% padding on combined series range.
486 // Tests data computation for the mini plot with a single error bar series.
487 RangeSelectorTestCase
.prototype.testSingleCombinedSeriesCustomBars
= function() {
490 showRangeSelector
: true,
494 [0, [0, 1, 2]], // [low, value, high]
498 var graph
= document
.getElementById("graph");
499 var g
= new Dygraph(graph
, data
, opts
);
501 var rangeSelector
= g
.getPluginInstance_(Dygraph
.Plugins
.RangeSelector
);
502 assertNotNull(rangeSelector
);
504 var combinedSeries
= rangeSelector
.computeCombinedSeriesAndLimits_();
506 yMin
: 1 - 7 * 0.25, // 25% padding
516 RangeSelectorTestCase
.prototype.testSingleCombinedSeriesErrorBars
= function() {
519 showRangeSelector
: true,
523 [0, [1, 1]], // [value, standard deviation]
527 var graph
= document
.getElementById("graph");
528 var g
= new Dygraph(graph
, data
, opts
);
530 var rangeSelector
= g
.getPluginInstance_(Dygraph
.Plugins
.RangeSelector
);
531 assertNotNull(rangeSelector
);
533 var combinedSeries
= rangeSelector
.computeCombinedSeriesAndLimits_();
535 yMin
: 1 - 7 * 0.25, // 25% padding
545 // Tests data computation for the mini plot with two custom bar series.
546 RangeSelectorTestCase
.prototype.testTwoCombinedSeriesCustomBars
= function() {
549 showRangeSelector
: true,
550 labels
: ['X', 'Y1', 'Y2']
553 [0, [0, 1, 2], [4, 5, 6]], // [low, value, high], avg_val = 3
554 [5, [1, 4, 5], [5, 8, 9]], // avg_val = 6
555 [10, [7, 8, 9], [11, 12, 13]] // avg_val = 10
557 var graph
= document
.getElementById("graph");
558 var g
= new Dygraph(graph
, data
, opts
);
560 var rangeSelector
= g
.getPluginInstance_(Dygraph
.Plugins
.RangeSelector
);
561 assertNotNull(rangeSelector
);
563 var combinedSeries
= rangeSelector
.computeCombinedSeriesAndLimits_();
565 yMin
: 3 - 7 * 0.25, // 25% padding
576 RangeSelectorTestCase
.prototype.assertGraphExistence
= function(g
, graph
) {
578 var zoomhandles
= graph
.getElementsByClassName('dygraph-rangesel-zoomhandle');
579 assertEquals(2, zoomhandles
.length
);
580 var bgcanvas
= graph
.getElementsByClassName('dygraph-rangesel-bgcanvas');
581 assertEquals(1, bgcanvas
.length
);
582 var fgcanvas
= graph
.getElementsByClassName('dygraph-rangesel-fgcanvas');
583 assertEquals(1, fgcanvas
.length
);