| 1 | /** |
| 2 | * @fileoverview Test cases for the interaction model. |
| 3 | * |
| 4 | * @author konigsberg@google.com (Robert Konigsbrg) |
| 5 | */ |
| 6 | var InteractionModelTestCase = TestCase("interaction-model"); |
| 7 | |
| 8 | InteractionModelTestCase.prototype.setUp = function() { |
| 9 | document.body.innerHTML = "<div id='graph'></div>"; |
| 10 | }; |
| 11 | |
| 12 | InteractionModelTestCase.prototype.tearDown = function() { |
| 13 | }; |
| 14 | |
| 15 | function getXLabels() { |
| 16 | var x_labels = document.getElementsByClassName("dygraph-axis-label-x"); |
| 17 | var ary = []; |
| 18 | for (var i = 0; i < x_labels.length; i++) { |
| 19 | ary.push(x_labels[i].innerHTML); |
| 20 | } |
| 21 | return ary; |
| 22 | } |
| 23 | |
| 24 | InteractionModelTestCase.prototype.pan = function(g, xRange, yRange) { |
| 25 | var originalXRange = g.xAxisRange(); |
| 26 | var originalYRange = g.yAxisRange(0); |
| 27 | |
| 28 | DygraphOps.dispatchMouseDown(g, xRange[0], yRange[0]); |
| 29 | DygraphOps.dispatchMouseMove(g, xRange[1], yRange[0]); // this is really necessary. |
| 30 | DygraphOps.dispatchMouseUp(g, xRange[1], yRange[0]); |
| 31 | |
| 32 | assertEqualsDelta(xRange, g.xAxisRange(), 0.2); |
| 33 | // assertEqualsDelta(originalYRange, g.yAxisRange(0), 0.2); // Not true, it's something in the middle. |
| 34 | |
| 35 | var midX = (xRange[1] - xRange[0]) / 2; |
| 36 | DygraphOps.dispatchMouseDown(g, midX, yRange[0]); |
| 37 | DygraphOps.dispatchMouseMove(g, midX, yRange[1]); // this is really necessary. |
| 38 | DygraphOps.dispatchMouseUp(g, midX, yRange[1]); |
| 39 | |
| 40 | assertEqualsDelta(xRange, g.xAxisRange(), 0.2); |
| 41 | assertEqualsDelta(yRange, g.yAxisRange(0), 0.2); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * This tests that when changing the interaction model so pan is used instead |
| 46 | * of zoom as the default behavior, a standard click method is still called. |
| 47 | */ |
| 48 | InteractionModelTestCase.prototype.testClickCallbackIsCalled = function() { |
| 49 | var clicked; |
| 50 | |
| 51 | var clickCallback = function(event, x) { |
| 52 | clicked = x; |
| 53 | }; |
| 54 | |
| 55 | var data = "X,Y\n" + |
| 56 | "20,-1\n" + |
| 57 | "21,0\n" + |
| 58 | "22,1\n" + |
| 59 | "23,0\n" |
| 60 | ; |
| 61 | |
| 62 | var graph = document.getElementById("graph"); |
| 63 | var g = new Dygraph(graph, data, |
| 64 | { |
| 65 | width: 100, |
| 66 | height : 100, |
| 67 | clickCallback : clickCallback |
| 68 | }); |
| 69 | |
| 70 | DygraphOps.dispatchMouseDown_Point(g, 10, 10); |
| 71 | DygraphOps.dispatchMouseMove_Point(g, 10, 10); |
| 72 | DygraphOps.dispatchMouseUp_Point(g, 10, 10); |
| 73 | |
| 74 | assertEquals(20, clicked); |
| 75 | }; |
| 76 | |
| 77 | /** |
| 78 | * This tests that when changing the interaction model so pan is used instead |
| 79 | * of zoom as the default behavior, a standard click method is still called. |
| 80 | */ |
| 81 | InteractionModelTestCase.prototype.testClickCallbackIsCalledOnCustomPan = function() { |
| 82 | var clicked; |
| 83 | |
| 84 | var clickCallback = function(event, x) { |
| 85 | clicked = x; |
| 86 | }; |
| 87 | |
| 88 | var data = "X,Y\n" + |
| 89 | "20,-1\n" + |
| 90 | "21,0\n" + |
| 91 | "22,1\n" + |
| 92 | "23,0\n" |
| 93 | ; |
| 94 | |
| 95 | function customDown(event, g, context) { |
| 96 | context.initializeMouseDown(event, g, context); |
| 97 | Dygraph.startPan(event, g, context); |
| 98 | } |
| 99 | |
| 100 | function customMove(event, g, context) { |
| 101 | Dygraph.movePan(event, g, context); |
| 102 | } |
| 103 | |
| 104 | function customUp(event, g, context) { |
| 105 | Dygraph.endPan(event, g, context); |
| 106 | } |
| 107 | |
| 108 | var opts = { |
| 109 | width: 100, |
| 110 | height : 100, |
| 111 | clickCallback : clickCallback, |
| 112 | interactionModel : { |
| 113 | 'mousedown' : customDown, |
| 114 | 'mousemove' : customMove, |
| 115 | 'mouseup' : customUp, |
| 116 | } |
| 117 | }; |
| 118 | |
| 119 | var graph = document.getElementById("graph"); |
| 120 | var g = new Dygraph(graph, data, opts); |
| 121 | |
| 122 | DygraphOps.dispatchMouseDown_Point(g, 10, 10); |
| 123 | DygraphOps.dispatchMouseMove_Point(g, 10, 10); |
| 124 | DygraphOps.dispatchMouseUp_Point(g, 10, 10); |
| 125 | |
| 126 | assertEquals(20, clicked); |
| 127 | }; |
| 128 | |