Unit test for animated zooms.
[dygraphs.git] / auto_tests / tests / interaction_model.js
index 2698b5f..41d0e43 100644 (file)
@@ -3,14 +3,31 @@
  *
  * @author konigsberg@google.com (Robert Konigsbrg)
  */
-var InteractionModelTestCase = TestCase("interaction-model");
+describe("interaction-model", function() {
 
-InteractionModelTestCase.prototype.setUp = function() {
+beforeEach(function() {
   document.body.innerHTML = "<div id='graph'></div>";
-};
+});
 
-InteractionModelTestCase.prototype.tearDown = function() {
-};
+afterEach(function() {
+});
+
+var data1 = "X,Y\n" +
+    "20,-1\n" +
+    "21,0\n" +
+    "22,1\n" +
+    "23,0\n";
+
+var data2 =
+    [[1, 10],
+    [2, 20],
+    [3, 30],
+    [4, 40],
+    [5, 120],
+    [6, 50],
+    [7, 70],
+    [8, 90],
+    [9, 50]];
 
 function getXLabels() {
   var x_labels = document.getElementsByClassName("dygraph-axis-label-x");
@@ -21,7 +38,8 @@ function getXLabels() {
   return ary;
 }
 
-InteractionModelTestCase.prototype.pan = function(g, xRange, yRange) {
+/*
+it('testPan', function() {
   var originalXRange = g.xAxisRange();
   var originalYRange = g.yAxisRange(0);
 
@@ -29,38 +47,32 @@ InteractionModelTestCase.prototype.pan = function(g, xRange, yRange) {
   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.
+  assert.closeTo(xRange, g.xAxisRange(), 0.2);
+  // assert.closeTo(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]);
   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);
-}
+  assert.closeTo(xRange, g.xAxisRange(), 0.2);
+  assert.closeTo(yRange, g.yAxisRange(0), 0.2);
+});
+*/
 
 /**
  * This tests that when changing the interaction model so pan is used instead
  * of zoom as the default behavior, a standard click method is still called.
  */
-InteractionModelTestCase.prototype.testClickCallbackIsCalled = function() {
+it('testClickCallbackIsCalled', function() {
   var clicked;
 
   var clickCallback = function(event, x) {
     clicked = x;
   };
 
-  var data = "X,Y\n" +
-      "20,-1\n" +
-      "21,0\n" +
-      "22,1\n" +
-      "23,0\n"
-  ;
-
   var graph = document.getElementById("graph");
-  var g = new Dygraph(graph, data,
+  var g = new Dygraph(graph, data1,
       {
         width: 100,
         height : 100,
@@ -71,27 +83,20 @@ InteractionModelTestCase.prototype.testClickCallbackIsCalled = function() {
   DygraphOps.dispatchMouseMove_Point(g, 10, 10);
   DygraphOps.dispatchMouseUp_Point(g, 10, 10);
 
-  assertEquals(20, clicked);
-};
+  assert.equal(20, clicked);
+});
 
 /**
  * This tests that when changing the interaction model so pan is used instead
  * of zoom as the default behavior, a standard click method is still called.
  */
-InteractionModelTestCase.prototype.testClickCallbackIsCalledOnCustomPan = function() {
+it('testClickCallbackIsCalledOnCustomPan', function() {
   var clicked;
 
   var clickCallback = function(event, x) {
     clicked = x;
   };
 
-  var data = "X,Y\n" +
-      "20,-1\n" +
-      "21,0\n" +
-      "22,1\n" +
-      "23,0\n"
-  ;
-
   function customDown(event, g, context) {
     context.initializeMouseDown(event, g, context);
     Dygraph.startPan(event, g, context);
@@ -117,13 +122,337 @@ InteractionModelTestCase.prototype.testClickCallbackIsCalledOnCustomPan = functi
   };
 
   var graph = document.getElementById("graph");
-  var g = new Dygraph(graph, data, opts);
+  var g = new Dygraph(graph, data1, opts);
 
   DygraphOps.dispatchMouseDown_Point(g, 10, 10);
   DygraphOps.dispatchMouseMove_Point(g, 10, 10);
   DygraphOps.dispatchMouseUp_Point(g, 10, 10);
 
-  // THIS STILL FAILS. It's clicked, but x is undefined.
-  // assertEquals(20, clicked);
+  assert.equal(20, clicked);
+});
+
+var clickAt = function(g, x, y) {
+  DygraphOps.dispatchMouseDown(g, x, y);
+  DygraphOps.dispatchMouseMove(g, x, y);
+  DygraphOps.dispatchMouseUp(g, x, y);
 };
 
+/**
+ * This tests that clickCallback is still called with the nonInteractiveModel.
+ */
+it('testClickCallbackIsCalledWithNonInteractiveModel', function() {
+  var clicked;
+
+  // TODO(danvk): also test pointClickCallback here.
+  var clickCallback = function(event, x) {
+    clicked = x;
+  };
+
+  var opts = {
+    width: 100,
+    height : 100,
+    clickCallback : clickCallback,
+    interactionModel : Dygraph.Interaction.nonInteractiveModel_
+  };
+
+  var graph = document.getElementById("graph");
+  var g = new Dygraph(graph, data1, opts);
+
+  DygraphOps.dispatchMouseDown_Point(g, 10, 10);
+  DygraphOps.dispatchMouseMove_Point(g, 10, 10);
+  DygraphOps.dispatchMouseUp_Point(g, 10, 10);
+
+  assert.equal(20, clicked);
+});
+
+/**
+ * A sanity test to ensure pointClickCallback is called.
+ */
+it('testPointClickCallback', function() {
+  var clicked;
+  var g = new Dygraph(document.getElementById("graph"), data2, {
+    pointClickCallback : function(event, point) {
+      clicked = point;
+    }
+  });
+
+  clickAt(g, 4, 40);
+
+  assert.equal(4, clicked.xval);
+  assert.equal(40, clicked.yval);
+});
+
+/**
+ * A sanity test to ensure pointClickCallback is not called when out of range.
+ */
+it('testNoPointClickCallbackWhenOffPoint', function() {
+  var clicked;
+  var g = new Dygraph(document.getElementById("graph"), data2, {
+    pointClickCallback : function(event, point) {
+      clicked = point;
+    }
+  });
+
+  clickAt(g, 5, 40);
+
+  assert.isUndefined(clicked);
+});
+
+/**
+ * Ensures pointClickCallback circle size is taken into account.
+ */
+it('testPointClickCallback_circleSize', function() {
+  // TODO(konigsberg): Implement.
+});
+
+/**
+ * Ensures that pointClickCallback is called prior to clickCallback
+ */
+it('testPointClickCallbackCalledPriorToClickCallback', function() {
+  var counter = 0;
+  var pointClicked;
+  var clicked;
+  var g = new Dygraph(document.getElementById("graph"), data2, {
+    pointClickCallback : function(event, point) {
+      counter++;
+      pointClicked = counter;
+    },
+    clickCallback : function(event, point) {
+      counter++;
+      clicked = counter;
+    }
+  });
+
+  clickAt(g, 4, 40);
+  assert.equal(1, pointClicked);
+  assert.equal(2, clicked);
+});
+
+/**
+ * Ensures that when there's no pointClickCallback, clicking on a point still calls
+ * clickCallback
+ */
+it('testClickCallback_clickOnPoint', function() {
+  var clicked;
+  var g = new Dygraph(document.getElementById("graph"), data2, {
+    clickCallback : function(event, point) {
+      clicked = 1;
+    }
+  });
+
+  clickAt(g, 4, 40);
+  assert.equal(1, clicked);
+});
+
+it('testIsZoomed_none', function() {
+  var g = new Dygraph(document.getElementById("graph"), data2, {});
+
+  assert.isFalse(g.isZoomed());
+  assert.isFalse(g.isZoomed("x"));
+  assert.isFalse(g.isZoomed("y"));
+});
+
+it('testIsZoomed_x', function() {
+  var g = new Dygraph(document.getElementById("graph"), data2, {});
+
+  DygraphOps.dispatchMouseDown_Point(g, 100, 100);
+  DygraphOps.dispatchMouseMove_Point(g, 130, 100);
+  DygraphOps.dispatchMouseUp_Point(g, 130, 100);
+
+  assert.isTrue(g.isZoomed());
+  assert.isTrue(g.isZoomed("x"));
+  assert.isFalse(g.isZoomed("y"));
+});
+
+it('testIsZoomed_y', function() {
+  var g = new Dygraph(document.getElementById("graph"), data2, {});
+
+  DygraphOps.dispatchMouseDown_Point(g, 10, 10);
+  DygraphOps.dispatchMouseMove_Point(g, 10, 30);
+  DygraphOps.dispatchMouseUp_Point(g, 10, 30);
+
+  assert.isTrue(g.isZoomed());
+  assert.isFalse(g.isZoomed("x"));
+  assert.isTrue(g.isZoomed("y"));
+});
+
+it('testIsZoomed_both', function() {
+  var g = new Dygraph(document.getElementById("graph"), data2, {});
+
+  // Zoom x axis
+  DygraphOps.dispatchMouseDown_Point(g, 100, 100);
+  DygraphOps.dispatchMouseMove_Point(g, 130, 100);
+  DygraphOps.dispatchMouseUp_Point(g, 130, 100);
+
+  // Now zoom y axis
+  DygraphOps.dispatchMouseDown_Point(g, 100, 100);
+  DygraphOps.dispatchMouseMove_Point(g, 100, 130);
+  DygraphOps.dispatchMouseUp_Point(g, 100, 130);
+
+
+  assert.isTrue(g.isZoomed());
+  assert.isTrue(g.isZoomed("x"));
+  assert.isTrue(g.isZoomed("y"));
+});
+
+it('testIsZoomed_updateOptions_none', function() {
+  var g = new Dygraph(document.getElementById("graph"), data2, {});
+
+  g.updateOptions({});
+
+  assert.isFalse(g.isZoomed());
+  assert.isFalse(g.isZoomed("x"));
+  assert.isFalse(g.isZoomed("y"));
+});
+
+it('testIsZoomed_updateOptions_x', function() {
+  var g = new Dygraph(document.getElementById("graph"), data2, {});
+
+  g.updateOptions({dateWindow: [-.5, .3]});
+  assert.isTrue(g.isZoomed());
+  assert.isTrue(g.isZoomed("x"));
+  assert.isFalse(g.isZoomed("y"));
+});
+
+it('testIsZoomed_updateOptions_y', function() {
+  var g = new Dygraph(document.getElementById("graph"), data2, {});
+
+  g.updateOptions({valueRange: [1, 10]});
+
+  assert.isTrue(g.isZoomed());
+  assert.isFalse(g.isZoomed("x"));
+  assert.isTrue(g.isZoomed("y"));
+});
+
+it('testIsZoomed_updateOptions_both', function() {
+  var g = new Dygraph(document.getElementById("graph"), data2, {});
+
+  g.updateOptions({dateWindow: [-1, 1], valueRange: [1, 10]});
+
+  assert.isTrue(g.isZoomed());
+  assert.isTrue(g.isZoomed("x"));
+  assert.isTrue(g.isZoomed("y"));
+});
+
+
+it('testCorrectAxisValueRangeAfterUnzoom', function() {
+  var g = new Dygraph(document.getElementById("graph"),
+      data2, {
+        valueRange: [1, 50],
+        dateWindow: [1, 9],
+        animatedZooms:false
+      });
+  
+  // Zoom x axis
+  DygraphOps.dispatchMouseDown_Point(g, 100, 100);
+  DygraphOps.dispatchMouseMove_Point(g, 130, 100);
+  DygraphOps.dispatchMouseUp_Point(g, 130, 100);
+
+  // Zoom y axis
+  DygraphOps.dispatchMouseDown_Point(g, 100, 100);
+  DygraphOps.dispatchMouseMove_Point(g, 100, 130);
+  DygraphOps.dispatchMouseUp_Point(g, 100, 130);
+  var currentYAxisRange = g.yAxisRange();
+  var currentXAxisRange = g.xAxisRange();
+  
+  //check that the range for the axis has changed
+  assert.notEqual(1, currentXAxisRange[0]);
+  assert.notEqual(10, currentXAxisRange[1]);
+  assert.notEqual(1, currentYAxisRange[0]);
+  assert.notEqual(50, currentYAxisRange[1]);
+  
+  // unzoom by doubleclick.  This is really the order in which a browser
+  // generates events, and we depend on it.
+  DygraphOps.dispatchMouseDown_Point(g, 10, 10);
+  DygraphOps.dispatchMouseUp_Point(g, 10, 10);
+  DygraphOps.dispatchMouseDown_Point(g, 10, 10);
+  DygraphOps.dispatchMouseUp_Point(g, 10, 10);
+  DygraphOps.dispatchDoubleClick(g, null);
+  
+  // check if range for y-axis was reset to original value 
+  // TODO check if range for x-axis is correct. 
+  // Currently not possible because dateRange is set to null and extremes are returned
+  var newYAxisRange = g.yAxisRange();
+  assert.equal(1, newYAxisRange[0]);
+  assert.equal(50, newYAxisRange[1]);
+});
+
+/**
+ * Ensures pointClickCallback is called when some points along the y-axis don't
+ * exist.
+ */
+it('testPointClickCallback_missingData', function() {
+
+  // There's a B-value at 2, but no A-value.
+  var data =
+    "X,A,B\n" +
+    "1,,100\n"+
+    "2,,110\n"+
+    "3,140,120\n"+
+    "4,130,110\n"+
+    "";
+
+  var clicked;
+  var g = new Dygraph(document.getElementById("graph"), data, {
+    pointClickCallback : function(event, point) {
+      clicked = point;
+    }
+  });
+
+  clickAt(g, 2, 110);
+
+  assert.equal(2, clicked.xval);
+  assert.equal(110, clicked.yval);
+});
+
+describe('animated zooms', function() {
+  var oldDuration;
+
+  before(function() {
+    oldDuration = Dygraph.ANIMATION_DURATION;
+    Dygraph.ANIMATION_DURATION = 100;  // speed up the animation for testing
+  });
+  after(function() {
+    Dygraph.ANIMATION_DURATION = oldDuration;
+  });
+
+  it('should support animated zooms', function(done) {
+    var data =
+      "X,A,B\n" +
+      "1,120,100\n"+
+      "2,110,110\n"+
+      "3,140,120\n"+
+      "4,130,110\n";
+
+    var ranges = [];
+
+    var g = new Dygraph('graph', data, {
+      animatedZooms: true,
+    });
+
+    // updating the dateWindow does not result in an animation.
+    assert.deepEqual([1, 4], g.xAxisRange());
+    g.updateOptions({dateWindow: [2, 4]});
+    assert.deepEqual([2, 4], g.xAxisRange());
+
+    g.updateOptions({
+      // zoomCallback is called once when the animation is complete.
+      zoomCallback: function(xMin, xMax) {
+        assert.equal(1, xMin);
+        assert.equal(4, xMax);
+        assert.deepEqual([1, 4], g.xAxisRange());
+        done();
+      }
+    }, false);
+
+    // Zoom out -- resetZoom() _does_ produce an animation.
+    g.resetZoom();
+    assert.notDeepEqual([2, 4], g.xAxisRange());  // first frame is synchronous
+    assert.notDeepEqual([1, 4], g.xAxisRange());
+
+    // at this point control flow goes up to zoomCallback
+  });
+
+});
+
+});