From: Robert Konigsberg Date: Thu, 28 Apr 2011 16:18:30 +0000 (-0400) Subject: Added range_tests, which uses DygraphOps -- an API for pushing Dygraph-specific events. X-Git-Tag: v1.0.0~486^2~5 X-Git-Url: https://adrianiainlam.tk/git/?a=commitdiff_plain;h=72a74f044b9df59ef09e420f575e7080c7349fad;p=dygraphs.git Added range_tests, which uses DygraphOps -- an API for pushing Dygraph-specific events. --- diff --git a/auto_tests/misc/local.html b/auto_tests/misc/local.html index 1b1fecd..111d07a 100644 --- a/auto_tests/misc/local.html +++ b/auto_tests/misc/local.html @@ -15,10 +15,13 @@ + + +
diff --git a/auto_tests/tests/DygraphOps.js b/auto_tests/tests/DygraphOps.js new file mode 100644 index 0000000..2e9d754 --- /dev/null +++ b/auto_tests/tests/DygraphOps.js @@ -0,0 +1,97 @@ +// Copyright (c) 2011 Google, Inc. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + + +/** + * @fileoverview Utility functions for Dygraphs. + * + * @author konigsberg@google.com (Robert Konigsberg) + */ +var DygraphOps = {}; + +DygraphOps.dispatchDoubleClick = function(g, func) { + var evt = document.createEvent('MouseEvents'); + evt.initMouseEvent( + 'dblclick', + true, true, document.defaultView, + 2, 0, 0, 0, 0, + false, false, false, false, 0, null); + if (func) { + func(evt); + } + g.canvas_.dispatchEvent(evt); +}; + +DygraphOps.dispatchMouseDown = function(g, x, y, func) { + var px = Dygraph.findPosX(g.canvas_); + var py = Dygraph.findPosY(g.canvas_); + + var pageX = px + g.toDomXCoord(x); + var pageY = py + g.toDomYCoord(y); + + var evt = document.createEvent('MouseEvents'); + evt.initMouseEvent( + 'mousedown', + true, true, document.defaultView, + 1, pageX, pageY, pageX, pageY, + false, false, false, false, 0, null); + if (func) { + func(evt); + } + g.canvas_.dispatchEvent(evt); +}; + +DygraphOps.dispatchMouseMove = function(g, x, y, func) { + var px = Dygraph.findPosX(g.canvas_); + var py = Dygraph.findPosY(g.canvas_); + + var pageX = px + g.toDomXCoord(x); + var pageY = py + g.toDomYCoord(y); + + var evt = document.createEvent('MouseEvents'); + evt.initMouseEvent( + 'mousemove', + true, true, document.defaultView, + 0, pageX, pageY, pageX, pageY, + false, false, false, false, 0, null); + if (func) { + func(evt); + } + g.canvas_.dispatchEvent(evt); +}; + +DygraphOps.dispatchMouseUp = function(g, x, y, func) { + var px = Dygraph.findPosX(g.canvas_); + var py = Dygraph.findPosY(g.canvas_); + + var pageX = px + g.toDomXCoord(x); + var pageY = py + g.toDomYCoord(y); + + var evt = document.createEvent('MouseEvents'); + evt.initMouseEvent( + 'mouseup', + true, true, document.defaultView, + 0, pageX, pageY, pageX, pageY, + false, false, false, false, 0, null); + if (func) { + func(evt); + } + g.canvas_.dispatchEvent(evt); +}; diff --git a/auto_tests/tests/range_tests.js b/auto_tests/tests/range_tests.js new file mode 100644 index 0000000..846241e --- /dev/null +++ b/auto_tests/tests/range_tests.js @@ -0,0 +1,132 @@ +// Copyright (c) 2011 Google, Inc. +// +// Permission is hereby granted, free of charge, to any person obtaining a copy +// of this software and associated documentation files (the "Software"), to deal +// in the Software without restriction, including without limitation the rights +// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +// copies of the Software, and to permit persons to whom the Software is +// furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +// THE SOFTWARE. + + +/** + * @fileoverview Test valueRange and dateWindow changes. + * + * @author konigsberg@google.com (Robert Konigsberg) + */ +var ZERO_TO_FIFTY = [[ 10, 0 ] , [ 20, 50 ]]; +var ZERO_TO_FIFTY_STEPS = function() { + var a = []; + var x = 10; + var y = 0; + var step = 0; + for (step = 0; step <= 50; step++) { + a.push([x + (step * .2), y + step]); + } + return a; +} (); + +var RangeTestCase = TestCase("range-tests"); + +RangeTestCase.prototype.setUp = function() { + document.body.innerHTML = "
"; +}; + +RangeTestCase.prototype.createGraph = function(opts) { + var graph = document.getElementById("graph"); + var g = new Dygraph(graph, ZERO_TO_FIFTY_STEPS, opts); + + assertEquals([10, 20], g.xAxisRange()); + assertEquals([0, 55], g.yAxisRange(0)); + + return g; +}; + +/** + * Test that changes to valueRange and dateWindow are reflected + * appropriately. + */ +RangeTestCase.prototype.testRangeSetOperations = function() { + var g = this.createGraph({valueRange : [ 0, 55 ]}); + + g.updateOptions({ dateWindow : [ 12, 18 ] }); + assertEquals([12, 18], g.xAxisRange()); + assertEquals([0, 55], g.yAxisRange(0)); + + g.updateOptions({ valueRange : [ 10, 40 ] }); + assertEquals([12, 18], g.xAxisRange()); + assertEquals([10, 40], g.yAxisRange(0)); + + g.updateOptions({ }); + assertEquals([12, 18], g.xAxisRange()); + assertEquals([10, 40], g.yAxisRange(0)); + + g.updateOptions({ dateWindow : null, valueRange : null }); + assertEquals([10, 20], g.xAxisRange()); + assertEquals([0, 55], g.yAxisRange(0)); +}; + +/** + * Verify that when zoomed in by mouse operations, an empty call to + * updateOptions doesn't change the displayed ranges. + */ +RangeTestCase.prototype.zoom = function(g, xRange, yRange) { + var originalXRange = g.xAxisRange(); + var originalYRange = g.yAxisRange(0); + + // Editing e.shiftKey post construction doesn't work for Firefox. Damn. + DygraphOps.dispatchMouseDown(g, xRange[0], yRange[0], function(e) { e.shiftKey = true; }); + DygraphOps.dispatchMouseMove(g, xRange[1], yRange[0]); // this is really necessary. + DygraphOps.dispatchMouseUp(g, xRange[1], yRange[0]); + + assertEqualsDelta(xRange, g.xAxisRange(), 0.2); + // assertEqualsDelta(originalYRange, g.yAxisRange(0), 0.2); // Not true, it's something in the middle. + + var midX = (xRange[1] - xRange[0]) / 2; + DygraphOps.dispatchMouseDown(g, midX, yRange[0], function(e) { e.shiftKey = true; }); + DygraphOps.dispatchMouseMove(g, midX, yRange[1]); // this is really necessary. + DygraphOps.dispatchMouseUp(g, midX, yRange[1]); + + assertEqualsDelta(xRange, g.xAxisRange(), 0.2); + assertEqualsDelta(yRange, g.yAxisRange(0), 0.2); +} + + +/** + * Verify that when zoomed in by mouse operations, an empty call to + * updateOptions doesn't change the displayed ranges. + */ +RangeTestCase.prototype.testEmptyUpdateOptions_doesntUnzoom = function() { + var g = this.createGraph(); + this.zoom(g, [ 11, 18 ], [ 35, 40 ]); + + g.updateOptions({}); + + assertEqualsDelta([11, 18], g.xAxisRange(), 0.1); + assertEqualsDelta([35, 40], g.yAxisRange(0), 0.2); +} + +/** + * Verify that when zoomed in by mouse operations, a call to + * updateOptions({ dateWindow : null, valueRange : null }) fully + * unzooms. + */ +RangeTestCase.prototype.testRestoreOriginalRanges_viaUpdateOptions = function() { + var g = this.createGraph(); + this.zoom(g, [ 11, 18 ], [ 35, 40 ]); + + g.updateOptions({ dateWindow : null, valueRange : null }); + + assertEquals([0, 55], g.yAxisRange(0)); + assertEquals([10, 20], g.xAxisRange()); +}