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',
118 rangeSelectorPlotFillGradientColor
: 'rgba(200, 200, 42, 10)',
132 var graph
= document
.getElementById("graph");
133 var g
= new Dygraph(graph
, data
, opts
);
134 this.assertGraphExistence(g
, graph
);
137 RangeSelectorTestCase
.prototype.testAdditionalRangeSelectorOptions
= function() {
141 showRangeSelector
: true,
142 rangeSelectorHeight
: 30,
143 rangeSelectorBackgroundStrokeColor
: 'blue',
144 rangeSelectorBackgroundLineWidth
: 3,
145 rangeSelectorPlotLineWidth
: 0.5,
146 rangeSelectorForegroundStrokeColor
: 'red',
147 rangeSelectorForegroundLineWidth
: 2,
148 rangeSelectorAlpha
: 0.8,
162 var graph
= document
.getElementById("graph");
163 var g
= new Dygraph(graph
, data
, opts
);
164 this.assertGraphExistence(g
, graph
);
167 RangeSelectorTestCase
.prototype.testRangeSelectorEnablingAfterCreation
= function() {
184 var graph
= document
.getElementById("graph");
185 var g
= new Dygraph(graph
, data
, opts
);
186 g
.updateOptions({showRangeSelector
: true});
187 this.assertGraphExistence(g
, graph
);
190 // The animatedZooms option does not work with the range selector. Make sure it gets turned off.
191 RangeSelectorTestCase
.prototype.testRangeSelectorWithAnimatedZoomsOption
= function() {
195 showRangeSelector
: true,
210 var graph
= document
.getElementById("graph");
211 var g
= new Dygraph(graph
, data
, opts
);
212 this.assertGraphExistence(g
, graph
);
213 assertFalse(g
.getOption('animatedZooms'));
216 RangeSelectorTestCase
.prototype.testRangeSelectorWithAnimatedZoomsOption2
= function() {
234 var graph
= document
.getElementById("graph");
235 var g
= new Dygraph(graph
, data
, opts
);
236 g
.updateOptions({showRangeSelector
: true});
237 this.assertGraphExistence(g
, graph
);
238 assertFalse(g
.getOption('animatedZooms'));
241 RangeSelectorTestCase
.prototype.testRangeSelectorInteraction
= function() {
245 showRangeSelector
: true,
259 var graph
= document
.getElementById("graph");
260 var g
= new Dygraph(graph
, data
, opts
);
261 this.assertGraphExistence(g
, graph
);
262 var zoomhandles
= graph
.getElementsByClassName('dygraph-rangesel-zoomhandle');
264 // Move left zoomhandle in
265 var xRange
= g
.xAxisRange().slice();
267 var mouseDownEvent
= DygraphOps
.createEvent({
273 zoomhandles
[0].dispatchEvent(mouseDownEvent
);
275 var mouseMoveEvent
= DygraphOps
.createEvent({
280 zoomhandles
[0].dispatchEvent(mouseMoveEvent
);
282 var mouseUpEvent
= DygraphOps
.createEvent({
288 zoomhandles
[0].dispatchEvent(mouseUpEvent
);
290 var newXRange
= g
.xAxisRange().slice();
291 assert('left zoomhandle should have moved: '+newXRange
[0]+'>'+xRange
[0], newXRange
[0] > xRange
[0]);
292 assertEquals('right zoomhandle should not have moved', xRange
[1], newXRange
[1]);
294 // Move right zoomhandle in
297 mouseDownEvent
= DygraphOps
.createEvent({
303 zoomhandles
[1].dispatchEvent(mouseDownEvent
);
305 mouseMoveEvent
= DygraphOps
.createEvent({
310 zoomhandles
[1].dispatchEvent(mouseMoveEvent
);
312 mouseUpEvent
= DygraphOps
.createEvent({
318 zoomhandles
[1].dispatchEvent(mouseUpEvent
);
320 var newXRange
= g
.xAxisRange().slice();
321 assert('right zoomhandle should have moved: '+newXRange
[1]+'<'+xRange
[1], newXRange
[1] < xRange
[1]);
322 assertEquals('left zoomhandle should not have moved', xRange
[0], newXRange
[0]);
326 var fgcanvas
= graph
.getElementsByClassName('dygraph-rangesel-fgcanvas')[0];
327 var x
= parseInt(zoomhandles
[0].style
.left
) + 20;
328 var y
= parseInt(zoomhandles
[0].style
.top
);
330 mouseDownEvent
= DygraphOps
.createEvent({
336 fgcanvas
.dispatchEvent(mouseDownEvent
);
340 mouseMoveEvent
= DygraphOps
.createEvent({
345 fgcanvas
.dispatchEvent(mouseMoveEvent
);
347 mouseUpEvent
= DygraphOps
.createEvent({
353 fgcanvas
.dispatchEvent(mouseUpEvent
);
355 var newXRange
= g
.xAxisRange().slice();
356 assert(newXRange
[0]+'<'+xRange
[0], newXRange
[0] < xRange
[0]);
357 assert(newXRange
[1]+'<'+xRange
[1], newXRange
[1] < xRange
[1]);
361 RangeSelectorTestCase
.prototype.testRangeSelectorPositionIfXAxisNotDrawn
= function() {
366 axes
: { x
: { drawAxis
: false }},
367 showRangeSelector
: true,
368 rangeSelectorHeight
: 30,
375 var graph
= document
.getElementById("graph");
376 var g
= new Dygraph(graph
, data
, opts
);
378 //assert, that the range selector is at top position 70 since the 30px of the
379 // xAxis shouldn't be reserved since it isn't drawn.
380 this.assertGraphExistence(g
, graph
);
381 var bgcanvas
= graph
.getElementsByClassName('dygraph-rangesel-bgcanvas')[0];
382 assertEquals("Range selector is not at the expected position.","70px", bgcanvas
.style
.top
);
383 var fgcanvas
= graph
.getElementsByClassName('dygraph-rangesel-fgcanvas')[0];
384 assertEquals("Range selector is not at the expected position.","70px", fgcanvas
.style
.top
);
387 RangeSelectorTestCase
.prototype.testMiniPlotDrawn
= function() {
388 // Install Proxy to track canvas calls.
389 var origFunc
= Dygraph
.getContext
;
391 Dygraph
.getContext
= function(canvas
) {
392 console
.log(canvas
.className
);
393 if (canvas
.className
!= 'dygraph-rangesel-bgcanvas') {
394 return origFunc(canvas
);
396 miniHtx
= new Proxy(origFunc(canvas
));
404 axes
: { x
: { drawAxis
: false }},
405 showRangeSelector
: true,
406 rangeSelectorHeight
: 30,
407 rangeSelectorPlotStrokeColor
: '#ff0000',
415 var graph
= document
.getElementById("graph");
416 var g
= new Dygraph(graph
, data
, opts
);
418 // TODO(danvk): more precise tests.
419 assertNotNull(miniHtx
);
420 assertTrue(0 < CanvasAssertions
.numLinesDrawn(miniHtx
, '#ff0000'));
422 Dygraph
.getContext
= origFunc
;
425 // Tests data computation for the mini plot with a single series.
426 RangeSelectorTestCase
.prototype.testSingleCombinedSeries
= function() {
428 showRangeSelector
: true,
436 var graph
= document
.getElementById("graph");
437 var g
= new Dygraph(graph
, data
, opts
);
439 var rangeSelector
= g
.getPluginInstance_(Dygraph
.Plugins
.RangeSelector
);
440 assertNotNull(rangeSelector
);
442 var combinedSeries
= rangeSelector
.computeCombinedSeriesAndLimits_();
444 yMin
: 1 - 7 * 0.25, // 25% padding
455 // Tests that multiple series are averaged for the miniplot.
456 RangeSelectorTestCase
.prototype.testCombinedSeries
= function() {
458 showRangeSelector
: true,
459 labels
: ['X', 'Y1', 'Y2']
462 [0, 1, 3], // average = 2
463 [5, 4, 6], // average = 5
464 [10, 7, 9] // average = 8
466 var graph
= document
.getElementById("graph");
467 var g
= new Dygraph(graph
, data
, opts
);
469 var rangeSelector
= g
.getPluginInstance_(Dygraph
.Plugins
.RangeSelector
);
470 assertNotNull(rangeSelector
);
472 var combinedSeries
= rangeSelector
.computeCombinedSeriesAndLimits_();
474 yMin
: 2 - 6 * 0.25, // 25% padding on combined series range.
484 // Tests selection of a specific series to average for the mini plot.
485 RangeSelectorTestCase
.prototype.testSelectedCombinedSeries
= function() {
487 showRangeSelector
: true,
488 labels
: ['X', 'Y1', 'Y2', 'Y3', 'Y4'],
490 'Y1': { showInRangeSelector
: true },
491 'Y3': { showInRangeSelector
: true }
495 [0, 5, 8, 13, 21], // average (first and third) = 9
496 [5, 1, 3, 7, 14], // average (first and third) = 4
497 [10, 0, 19, 10, 6] // average (first and third) = 5
499 var graph
= document
.getElementById("graph");
500 var g
= new Dygraph(graph
, data
, opts
);
502 var rangeSelector
= g
.getPluginInstance_(Dygraph
.Plugins
.RangeSelector
);
503 assertNotNull(rangeSelector
);
505 var combinedSeries
= rangeSelector
.computeCombinedSeriesAndLimits_();
507 yMin
: 4 - 5 * 0.25, // 25% padding on combined series range.
517 // Tests data computation for the mini plot with a single error bar series.
518 RangeSelectorTestCase
.prototype.testSingleCombinedSeriesCustomBars
= function() {
521 showRangeSelector
: true,
525 [0, [0, 1, 2]], // [low, value, high]
529 var graph
= document
.getElementById("graph");
530 var g
= new Dygraph(graph
, data
, opts
);
532 var rangeSelector
= g
.getPluginInstance_(Dygraph
.Plugins
.RangeSelector
);
533 assertNotNull(rangeSelector
);
535 var combinedSeries
= rangeSelector
.computeCombinedSeriesAndLimits_();
537 yMin
: 1 - 7 * 0.25, // 25% padding
547 RangeSelectorTestCase
.prototype.testSingleCombinedSeriesErrorBars
= function() {
550 showRangeSelector
: true,
554 [0, [1, 1]], // [value, standard deviation]
558 var graph
= document
.getElementById("graph");
559 var g
= new Dygraph(graph
, data
, opts
);
561 var rangeSelector
= g
.getPluginInstance_(Dygraph
.Plugins
.RangeSelector
);
562 assertNotNull(rangeSelector
);
564 var combinedSeries
= rangeSelector
.computeCombinedSeriesAndLimits_();
566 yMin
: 1 - 7 * 0.25, // 25% padding
576 // Tests data computation for the mini plot with two custom bar series.
577 RangeSelectorTestCase
.prototype.testTwoCombinedSeriesCustomBars
= function() {
580 showRangeSelector
: true,
581 labels
: ['X', 'Y1', 'Y2']
584 [0, [0, 1, 2], [4, 5, 6]], // [low, value, high], avg_val = 3
585 [5, [1, 4, 5], [5, 8, 9]], // avg_val = 6
586 [10, [7, 8, 9], [11, 12, 13]] // avg_val = 10
588 var graph
= document
.getElementById("graph");
589 var g
= new Dygraph(graph
, data
, opts
);
591 var rangeSelector
= g
.getPluginInstance_(Dygraph
.Plugins
.RangeSelector
);
592 assertNotNull(rangeSelector
);
594 var combinedSeries
= rangeSelector
.computeCombinedSeriesAndLimits_();
596 yMin
: 3 - 7 * 0.25, // 25% padding
607 RangeSelectorTestCase
.prototype.assertGraphExistence
= function(g
, graph
) {
609 var zoomhandles
= graph
.getElementsByClassName('dygraph-rangesel-zoomhandle');
610 assertEquals(2, zoomhandles
.length
);
611 var bgcanvas
= graph
.getElementsByClassName('dygraph-rangesel-bgcanvas');
612 assertEquals(1, bgcanvas
.length
);
613 var fgcanvas
= graph
.getElementsByClassName('dygraph-rangesel-fgcanvas');
614 assertEquals(1, fgcanvas
.length
);