Merge branch 'master' of https://github.com/danvk/dygraphs
authorRobert Konigsberg <konigsberg@google.com>
Thu, 5 May 2011 22:23:27 +0000 (15:23 -0700)
committerRobert Konigsberg <konigsberg@google.com>
Thu, 5 May 2011 22:23:27 +0000 (15:23 -0700)
README
auto_tests/lib/README [deleted file]
auto_tests/misc/fake-jstestdriver.js
auto_tests/misc/local.html
auto_tests/tests/CanvasAssertions.js
auto_tests/tests/DygraphOps.js [new file with mode: 0644]
auto_tests/tests/MoreAsserts.js
auto_tests/tests/Proxy.js
auto_tests/tests/range_tests.js [new file with mode: 0644]
auto_tests/tests/sanity.js
auto_tests/tests/simple_drawing.js

diff --git a/README b/README
index 0fc135d..135ba59 100644 (file)
--- a/README
+++ b/README
@@ -35,7 +35,8 @@ Minimal Example
         "Date,Temperature\n" +                // the data series
         "2008-05-07,75\n" +
         "2008-05-08,70\n" +
-        "2008-05-09,80\n"
+        "2008-05-09,80\n",
+        { }                                   // the options
       );
 </script>
 </body>
@@ -52,9 +53,18 @@ dygraphs uses:
  - excanvas.js (Apache License)
  - YUI compressor (BSD License)
 
+automated tests use:
+ - auto_tests/lib/jquery-1.4.2.js (MIT & GPL2)
+ - auto_tests/lib/Asserts.js (Apache 2.0 License)
+ - auto-tests/lib/JsTestDriver-1.3.2.jar (Apache 2.0 License
+
 rgbcolor: http://www.phpied.com/rgb-color-parser-in-javascript/
 strftime: http://tech.bluesmoon.info/2008/04/strftime-in-javascript.html
 excanvas: http://code.google.com/p/explorercanvas/
 yui compressor: http://developer.yahoo.com/yui/compressor/
 
+jquery: http://code.jquery.com/jquery-1.4.2.js
+Asserts.js: http://www.google.com/codesearch/p?#3tsINRJRCro/trunk/JsTestDriver/src/com/google/jstestdriver/javascript/Asserts.js
+JSTestDriver: http://code.google.com/p/js-test-driver/
+
 dygraphs is available under the MIT license, included in LICENSE.txt.
diff --git a/auto_tests/lib/README b/auto_tests/lib/README
deleted file mode 100644 (file)
index 25a99e2..0000000
+++ /dev/null
@@ -1,6 +0,0 @@
-JQuery lib:
-  License: MIT & GPL2
-  http://code.jquery.com/jquery-1.4.2.js
-Asserts.js:
-  License: Apache 2.0
-  http://www.google.com/codesearch/p?#3tsINRJRCro/trunk/JsTestDriver/src/com/google/jstestdriver/javascript/Asserts.js
index 4849f99..9ca10cd 100644 (file)
@@ -43,18 +43,32 @@ function TestCase(name) {
     return "Fake test case " + name;
   };
 
-  var emptyFunction = function() { return this; };
-  emptyFunction.prototype.setUp = function() { };
-  emptyFunction.prototype.tearDown = function() { };
-  emptyFunction.prototype.runTest = function(name) {
+  var testCase = function() { return this; };
+  testCase.prototype.setUp = function() { };
+  testCase.prototype.tearDown = function() { };
+  testCase.prototype.runTest = function(name) {
     try {
       this.setUp();
       var fn = this[name];
       fn.apply(this, []);
       this.tearDown();
+      return true;
     } catch (e) {
       console.log(e.stack);
+      return false;
     }
   };
-  return emptyFunction;
+  testCase.prototype.runAllTests = function() {
+    // what's better than for ... in for non-array objects?
+    var tests = {};
+    for (var name in this) {
+      if (name.indexOf('test') == 0 && typeof(this[name]) == 'function') {
+        console.log("Running " + name);
+        var result = this.runTest(name);
+        tests[name] = result;
+      }
+    }
+    console.log(prettyPrintEntity_(tests));
+  };
+  return testCase;
 };
index 1b1fecd..111d07a 100644 (file)
   <script type="text/javascript" src="../lib/jquery-1.4.2.js"></script>
   <script type="text/javascript" src="../lib/Asserts.js"></script>
   <script type="text/javascript" src="fake-jstestdriver.js"></script>
+  <script type="text/javascript" src="../tests/MoreAsserts.js"></script>
   <script type="text/javascript" src="../tests/Proxy.js"></script>
   <script type="text/javascript" src="../tests/CanvasAssertions.js"></script>
+  <script type="text/javascript" src="../tests/DygraphOps.js"></script>
   <script type="text/javascript" src="../tests/sanity.js"></script>
   <script type="text/javascript" src="../tests/simple_drawing.js"></script>
+  <script type="text/javascript" src="../tests/range_tests.js"></script>
 </head>
 <body>
   <div id='graph'></div>
index 851b7dd..1ef0573 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (c)  2011 Google, Inc.
+// 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
diff --git a/auto_tests/tests/DygraphOps.js b/auto_tests/tests/DygraphOps.js
new file mode 100644 (file)
index 0000000..3bd3c44
--- /dev/null
@@ -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);
+};
index b2a690a..1380966 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (c)  2011 Google, Inc.
+// 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
index 51a6e94..5c03dc6 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (c)  2011 Google, Inc.
+// 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
diff --git a/auto_tests/tests/range_tests.js b/auto_tests/tests/range_tests.js
new file mode 100644 (file)
index 0000000..608c05e
--- /dev/null
@@ -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 = "<div id='graph'></div>";
+};
+
+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());
+}
index ebe8302..44e860c 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (c)  2011 Google, Inc.
+// 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
index 0b8f34e..7e9e6ef 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright (c)  2011 Google, Inc.
+// 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