Fix bug 428, add test which catches exception. What an annoying little bug.
[dygraphs.git] / auto_tests / tests / callback.js
index 8c054a9..601aceb 100644 (file)
@@ -8,12 +8,14 @@ var CallbackTestCase = TestCase("callback");
 
 CallbackTestCase.prototype.setUp = function() {
   document.body.innerHTML = "<div id='graph'></div><div id='selection'></div>";
+  this.xhr = XMLHttpRequest;
   this.styleSheet = document.createElement("style");
   this.styleSheet.type = "text/css";
   document.getElementsByTagName("head")[0].appendChild(this.styleSheet);
 };
 
 CallbackTestCase.prototype.tearDown = function() {
+  XMLHttpRequest = this.xhr;
 };
 
 var data = "X,a\,b,c\n" +
@@ -123,6 +125,48 @@ CallbackTestCase.prototype.testDrawPointCallback_pointSize = function() {
 };
 
 /**
+ * Test that drawPointCallback is called for isolated points when
+ * drawPoints is false, and also for gap points if that's enabled.
+ */
+CallbackTestCase.prototype.testDrawPointCallback_isolated = function() {
+  var xvalues = [];
+
+  var g;
+  var callback = function(g, seriesName, canvasContext, cx, cy, color, pointSizeParam) {
+    var dx = g.toDataXCoord(cx);
+    xvalues.push(dx);
+    Dygraph.Circles.DEFAULT.apply(this, arguments);
+  };
+
+  var graph = document.getElementById("graph");
+  var testdata = [[10, 2], [11, 3], [12, NaN], [13, 2], [14, NaN], [15, 3]];
+  var graphOpts = {
+      labels: ['X', 'Y'],
+      valueRange: [0, 4],
+      drawPoints : false,
+      drawPointCallback : callback,
+      pointSize : 8
+  };
+
+  // Test that isolated points get drawn
+  g = new Dygraph(graph, testdata, graphOpts);
+  assertEquals(2, xvalues.length);
+  assertEquals(13, xvalues[0]);
+  assertEquals(15, xvalues[1]);
+
+  // Test that isolated points + gap points get drawn when
+  // drawGapEdgePoints is set.  This should add one point at the right
+  // edge of the segment at x=11, but not at the graph edge at x=10.
+  xvalues = []; // Reset for new test
+  graphOpts.drawGapEdgePoints = true;
+  g = new Dygraph(graph, testdata, graphOpts);
+  assertEquals(3, xvalues.length);
+  assertEquals(11, xvalues[0]);
+  assertEquals(13, xvalues[1]);
+  assertEquals(15, xvalues[2]);
+};
+
+/**
  * This tests that when the function idxToRow_ returns the proper row and the onHiglightCallback
  * is properly called when the first series is hidden (setVisibility = false)
  *
@@ -260,6 +304,27 @@ CallbackTestCase.prototype.testClosestPointCallbackCss2 = function() {
 }
 
 /**
+ * Closest-point highlighting with locked series.
+ */
+CallbackTestCase.prototype.testSetSelectionLocking = function() {
+  var g = runClosestTest(false, 2, 4);
+
+  // Default behavior, 'b' is closest
+  DygraphOps.dispatchMouseMove(g, 11, 4);
+  assertEquals('b', g.getHighlightSeries());
+
+  // Now lock selection to 'c'
+  g.setSelection(false, 'c', true);
+  DygraphOps.dispatchMouseMove(g, 11, 4);
+  assertEquals('c', g.getHighlightSeries());
+
+  // Unlock, should be back to 'b'
+  g.clearSelection();
+  DygraphOps.dispatchMouseMove(g, 11, 4);
+  assertEquals('b', g.getHighlightSeries());
+}
+
+/**
  * This tests that closest point searches work for data containing NaNs.
  *
  * It's intended to catch a regression where a NaN Y value confuses the
@@ -375,7 +440,7 @@ CallbackTestCase.prototype.testNaNDataStack = function() {
 };
 
 CallbackTestCase.prototype.testGapHighlight = function() {
-var dataGap = [
+  var dataGap = [
     [1, null, 3],
     [2, 2, null],
     [3, null, 5],
@@ -424,3 +489,35 @@ var dataGap = [
   assertEquals(8, h_pts[0].yval);
   assertEquals('A', h_pts[0].name);
 };
+
+CallbackTestCase.prototype.testFailedResponse = function() {
+
+  // Fake out the XMLHttpRequest so it doesn't do anything.
+  XMLHttpRequest = function () {};
+  XMLHttpRequest.prototype.open = function () {};
+  XMLHttpRequest.prototype.send = function () {};
+
+  var highlightCallback = function(e, x, pts, row) {
+    fail("should not reach here");
+  };
+
+  var graph = document.getElementById("graph");
+  graph.style.border = "2px solid black";
+  var g = new Dygraph(graph, "data.csv", { // fake name
+     width: 400,
+     height: 300,
+     highlightCallback : highlightCallback
+  });
+
+  DygraphOps.dispatchMouseOver_Point(g, 800, 800);
+  DygraphOps.dispatchMouseMove_Point(g, 100, 100);
+  DygraphOps.dispatchMouseMove_Point(g, 800, 800);
+
+  var oldOnerror = window.onerror;
+  var failed = false;
+  window.onerror = function() { failed = true; return false; }
+
+  DygraphOps.dispatchMouseOut_Point(g, 800, 800); // This call should not throw an exception.
+
+  assertFalse("exception thrown during mouseout", failed);
+};