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 rangeSelectorPlotStyleColor
: 'yellow',
132 var graph
= document
.getElementById("graph");
133 var g
= new Dygraph(graph
, data
, opts
);
134 this.assertGraphExistence(g
, graph
);
137 RangeSelectorTestCase
.prototype.testRangeSelectorEnablingAfterCreation
= function() {
154 var graph
= document
.getElementById("graph");
155 var g
= new Dygraph(graph
, data
, opts
);
156 g
.updateOptions({showRangeSelector
: true});
157 this.assertGraphExistence(g
, graph
);
160 // The animatedZooms option does not work with the range selector. Make sure it gets turned off.
161 RangeSelectorTestCase
.prototype.testRangeSelectorWithAnimatedZoomsOption
= function() {
165 showRangeSelector
: true,
180 var graph
= document
.getElementById("graph");
181 var g
= new Dygraph(graph
, data
, opts
);
182 this.assertGraphExistence(g
, graph
);
183 assertFalse(g
.getOption('animatedZooms'));
186 RangeSelectorTestCase
.prototype.testRangeSelectorWithAnimatedZoomsOption2
= function() {
204 var graph
= document
.getElementById("graph");
205 var g
= new Dygraph(graph
, data
, opts
);
206 g
.updateOptions({showRangeSelector
: true});
207 this.assertGraphExistence(g
, graph
);
208 assertFalse(g
.getOption('animatedZooms'));
211 RangeSelectorTestCase
.prototype.testRangeSelectorInteraction
= function() {
215 showRangeSelector
: true,
229 var graph
= document
.getElementById("graph");
230 var g
= new Dygraph(graph
, data
, opts
);
231 this.assertGraphExistence(g
, graph
);
232 var zoomhandles
= graph
.getElementsByClassName('dygraph-rangesel-zoomhandle');
234 // Move left zoomhandle in
235 var xRange
= g
.xAxisRange().slice();
237 var mouseDownEvent
= DygraphOps
.createEvent({
243 zoomhandles
[0].dispatchEvent(mouseDownEvent
);
245 var mouseMoveEvent
= DygraphOps
.createEvent({
250 zoomhandles
[0].dispatchEvent(mouseMoveEvent
);
252 var mouseUpEvent
= DygraphOps
.createEvent({
258 zoomhandles
[0].dispatchEvent(mouseUpEvent
);
260 var newXRange
= g
.xAxisRange().slice();
261 assert('left zoomhandle should have moved: '+newXRange
[0]+'>'+xRange
[0], newXRange
[0] > xRange
[0]);
262 assertEquals('right zoomhandle should not have moved', xRange
[1], newXRange
[1]);
264 // Move right zoomhandle in
267 mouseDownEvent
= DygraphOps
.createEvent({
273 zoomhandles
[1].dispatchEvent(mouseDownEvent
);
275 mouseMoveEvent
= DygraphOps
.createEvent({
280 zoomhandles
[1].dispatchEvent(mouseMoveEvent
);
282 mouseUpEvent
= DygraphOps
.createEvent({
288 zoomhandles
[1].dispatchEvent(mouseUpEvent
);
290 var newXRange
= g
.xAxisRange().slice();
291 assert('right zoomhandle should have moved: '+newXRange
[1]+'<'+xRange
[1], newXRange
[1] < xRange
[1]);
292 assertEquals('left zoomhandle should not have moved', xRange
[0], newXRange
[0]);
296 var fgcanvas
= graph
.getElementsByClassName('dygraph-rangesel-fgcanvas')[0];
297 var x
= parseInt(zoomhandles
[0].style
.left
) + 20;
298 var y
= parseInt(zoomhandles
[0].style
.top
);
300 mouseDownEvent
= DygraphOps
.createEvent({
306 fgcanvas
.dispatchEvent(mouseDownEvent
);
310 mouseMoveEvent
= DygraphOps
.createEvent({
315 fgcanvas
.dispatchEvent(mouseMoveEvent
);
317 mouseUpEvent
= DygraphOps
.createEvent({
323 fgcanvas
.dispatchEvent(mouseUpEvent
);
325 var newXRange
= g
.xAxisRange().slice();
326 assert(newXRange
[0]+'<'+xRange
[0], newXRange
[0] < xRange
[0]);
327 assert(newXRange
[1]+'<'+xRange
[1], newXRange
[1] < xRange
[1]);
331 RangeSelectorTestCase
.prototype.testRangeSelectorPositionIfXAxisNotDrawn
= function() {
337 showRangeSelector
: true,
338 rangeSelectorHeight
: 30,
345 var graph
= document
.getElementById("graph");
346 var g
= new Dygraph(graph
, data
, opts
);
348 //assert, that the range selector is at top position 70 since the 30px of the
349 // xAxis shouldn't be reserved since it isn't drawn.
350 this.assertGraphExistence(g
, graph
);
351 var bgcanvas
= graph
.getElementsByClassName('dygraph-rangesel-bgcanvas')[0];
352 assertEquals("Range selector is not at the expected position.","70px", bgcanvas
.style
.top
);
353 var fgcanvas
= graph
.getElementsByClassName('dygraph-rangesel-fgcanvas')[0];
354 assertEquals("Range selector is not at the expected position.","70px", fgcanvas
.style
.top
);
357 RangeSelectorTestCase
.prototype.testMiniPlotDrawn
= function() {
358 // Install Proxy to track canvas calls.
359 var origFunc
= Dygraph
.getContext
;
361 Dygraph
.getContext
= function(canvas
) {
362 console
.log(canvas
.className
);
363 if (canvas
.className
!= 'dygraph-rangesel-bgcanvas') {
364 return origFunc(canvas
);
366 miniHtx
= new Proxy(origFunc(canvas
));
375 showRangeSelector
: true,
376 rangeSelectorHeight
: 30,
377 rangeSelectorPlotStrokeColor
: '#ff0000',
385 var graph
= document
.getElementById("graph");
386 var g
= new Dygraph(graph
, data
, opts
);
388 // TODO(danvk): more precise tests.
389 assertNotNull(miniHtx
);
390 assertTrue(0 < CanvasAssertions
.numLinesDrawn(miniHtx
, '#ff0000'));
392 Dygraph
.getContext
= origFunc
;
395 // Tests data computation for the mini plot with a single series.
396 RangeSelectorTestCase
.prototype.testSingleCombinedSeries
= function() {
398 showRangeSelector
: true,
406 var graph
= document
.getElementById("graph");
407 var g
= new Dygraph(graph
, data
, opts
);
409 var rangeSelector
= g
.getPluginInstance_(Dygraph
.Plugins
.RangeSelector
);
410 assertNotNull(rangeSelector
);
412 var combinedSeries
= rangeSelector
.computeCombinedSeriesAndLimits_();
414 yMin
: 1 - 7 * 0.25, // 25% padding
425 // Tests that multiple series are averaged for the miniplot.
426 RangeSelectorTestCase
.prototype.testCombinedSeries
= function() {
428 showRangeSelector
: true,
429 labels
: ['X', 'Y1', 'Y2']
432 [0, 1, 3], // average = 2
433 [5, 4, 6], // average = 5
434 [10, 7, 9] // average = 8
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
: 2 - 6 * 0.25, // 25% padding on combined series range.
454 // Tests data computation for the mini plot with a single error bar series.
455 RangeSelectorTestCase
.prototype.testSingleCombinedSeriesCustomBars
= function() {
458 showRangeSelector
: true,
462 [0, [0, 1, 2]], // [low, value, high]
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
: 1 - 7 * 0.25, // 25% padding
484 RangeSelectorTestCase
.prototype.testSingleCombinedSeriesErrorBars
= function() {
487 showRangeSelector
: true,
491 [0, [1, 1]], // [value, standard deviation]
495 var graph
= document
.getElementById("graph");
496 var g
= new Dygraph(graph
, data
, opts
);
498 var rangeSelector
= g
.getPluginInstance_(Dygraph
.Plugins
.RangeSelector
);
499 assertNotNull(rangeSelector
);
501 var combinedSeries
= rangeSelector
.computeCombinedSeriesAndLimits_();
503 yMin
: 1 - 7 * 0.25, // 25% padding
513 // Tests data computation for the mini plot with two custom bar series.
514 RangeSelectorTestCase
.prototype.testTwoCombinedSeriesCustomBars
= function() {
517 showRangeSelector
: true,
518 labels
: ['X', 'Y1', 'Y2']
521 [0, [0, 1, 2], [4, 5, 6]], // [low, value, high], avg_val = 3
522 [5, [1, 4, 5], [5, 8, 9]], // avg_val = 6
523 [10, [7, 8, 9], [11, 12, 13]] // avg_val = 10
525 var graph
= document
.getElementById("graph");
526 var g
= new Dygraph(graph
, data
, opts
);
528 var rangeSelector
= g
.getPluginInstance_(Dygraph
.Plugins
.RangeSelector
);
529 assertNotNull(rangeSelector
);
531 var combinedSeries
= rangeSelector
.computeCombinedSeriesAndLimits_();
533 yMin
: 3 - 7 * 0.25, // 25% padding
544 RangeSelectorTestCase
.prototype.assertGraphExistence
= function(g
, graph
) {
546 var zoomhandles
= graph
.getElementsByClassName('dygraph-rangesel-zoomhandle');
547 assertEquals(2, zoomhandles
.length
);
548 var bgcanvas
= graph
.getElementsByClassName('dygraph-rangesel-bgcanvas');
549 assertEquals(1, bgcanvas
.length
);
550 var fgcanvas
= graph
.getElementsByClassName('dygraph-rangesel-fgcanvas');
551 assertEquals(1, fgcanvas
.length
);