| 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 | var data1 = "X,Y\n" + |
| 16 | "20,-1\n" + |
| 17 | "21,0\n" + |
| 18 | "22,1\n" + |
| 19 | "23,0\n"; |
| 20 | |
| 21 | var data2 = |
| 22 | [[1, 10], |
| 23 | [2, 20], |
| 24 | [3, 30], |
| 25 | [4, 40], |
| 26 | [5, 120], |
| 27 | [6, 50], |
| 28 | [7, 70], |
| 29 | [8, 90], |
| 30 | [9, 50]]; |
| 31 | |
| 32 | function getXLabels() { |
| 33 | var x_labels = document.getElementsByClassName("dygraph-axis-label-x"); |
| 34 | var ary = []; |
| 35 | for (var i = 0; i < x_labels.length; i++) { |
| 36 | ary.push(x_labels[i].innerHTML); |
| 37 | } |
| 38 | return ary; |
| 39 | } |
| 40 | |
| 41 | InteractionModelTestCase.prototype.pan = function(g, xRange, yRange) { |
| 42 | var originalXRange = g.xAxisRange(); |
| 43 | var originalYRange = g.yAxisRange(0); |
| 44 | |
| 45 | DygraphOps.dispatchMouseDown(g, xRange[0], yRange[0]); |
| 46 | DygraphOps.dispatchMouseMove(g, xRange[1], yRange[0]); // this is really necessary. |
| 47 | DygraphOps.dispatchMouseUp(g, xRange[1], yRange[0]); |
| 48 | |
| 49 | assertEqualsDelta(xRange, g.xAxisRange(), 0.2); |
| 50 | // assertEqualsDelta(originalYRange, g.yAxisRange(0), 0.2); // Not true, it's something in the middle. |
| 51 | |
| 52 | var midX = (xRange[1] - xRange[0]) / 2; |
| 53 | DygraphOps.dispatchMouseDown(g, midX, yRange[0]); |
| 54 | DygraphOps.dispatchMouseMove(g, midX, yRange[1]); // this is really necessary. |
| 55 | DygraphOps.dispatchMouseUp(g, midX, yRange[1]); |
| 56 | |
| 57 | assertEqualsDelta(xRange, g.xAxisRange(), 0.2); |
| 58 | assertEqualsDelta(yRange, g.yAxisRange(0), 0.2); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * This tests that when changing the interaction model so pan is used instead |
| 63 | * of zoom as the default behavior, a standard click method is still called. |
| 64 | */ |
| 65 | InteractionModelTestCase.prototype.testClickCallbackIsCalled = function() { |
| 66 | var clicked; |
| 67 | |
| 68 | var clickCallback = function(event, x) { |
| 69 | clicked = x; |
| 70 | }; |
| 71 | |
| 72 | var graph = document.getElementById("graph"); |
| 73 | var g = new Dygraph(graph, data1, |
| 74 | { |
| 75 | width: 100, |
| 76 | height : 100, |
| 77 | clickCallback : clickCallback |
| 78 | }); |
| 79 | |
| 80 | DygraphOps.dispatchMouseDown_Point(g, 10, 10); |
| 81 | DygraphOps.dispatchMouseMove_Point(g, 10, 10); |
| 82 | DygraphOps.dispatchMouseUp_Point(g, 10, 10); |
| 83 | |
| 84 | assertEquals(20, clicked); |
| 85 | }; |
| 86 | |
| 87 | /** |
| 88 | * This tests that when changing the interaction model so pan is used instead |
| 89 | * of zoom as the default behavior, a standard click method is still called. |
| 90 | */ |
| 91 | InteractionModelTestCase.prototype.testClickCallbackIsCalledOnCustomPan = function() { |
| 92 | var clicked; |
| 93 | |
| 94 | var clickCallback = function(event, x) { |
| 95 | clicked = x; |
| 96 | }; |
| 97 | |
| 98 | function customDown(event, g, context) { |
| 99 | context.initializeMouseDown(event, g, context); |
| 100 | Dygraph.startPan(event, g, context); |
| 101 | } |
| 102 | |
| 103 | function customMove(event, g, context) { |
| 104 | Dygraph.movePan(event, g, context); |
| 105 | } |
| 106 | |
| 107 | function customUp(event, g, context) { |
| 108 | Dygraph.endPan(event, g, context); |
| 109 | } |
| 110 | |
| 111 | var opts = { |
| 112 | width: 100, |
| 113 | height : 100, |
| 114 | clickCallback : clickCallback, |
| 115 | interactionModel : { |
| 116 | 'mousedown' : customDown, |
| 117 | 'mousemove' : customMove, |
| 118 | 'mouseup' : customUp, |
| 119 | } |
| 120 | }; |
| 121 | |
| 122 | var graph = document.getElementById("graph"); |
| 123 | var g = new Dygraph(graph, data1, opts); |
| 124 | |
| 125 | DygraphOps.dispatchMouseDown_Point(g, 10, 10); |
| 126 | DygraphOps.dispatchMouseMove_Point(g, 10, 10); |
| 127 | DygraphOps.dispatchMouseUp_Point(g, 10, 10); |
| 128 | |
| 129 | assertEquals(20, clicked); |
| 130 | }; |
| 131 | |
| 132 | InteractionModelTestCase.clickAt = function(g, x, y) { |
| 133 | DygraphOps.dispatchMouseDown(g, x, y); |
| 134 | DygraphOps.dispatchMouseMove(g, x, y); |
| 135 | DygraphOps.dispatchMouseUp(g, x, y); |
| 136 | } |
| 137 | |
| 138 | /** |
| 139 | * A sanity test to ensure pointClickCallback is called. |
| 140 | */ |
| 141 | InteractionModelTestCase.prototype.testPointClickCallback = function() { |
| 142 | var clicked; |
| 143 | var g = new Dygraph(document.getElementById("graph"), data2, { |
| 144 | pointClickCallback : function(event, point) { |
| 145 | clicked = point; |
| 146 | } |
| 147 | }); |
| 148 | |
| 149 | InteractionModelTestCase.clickAt(g, 4, 40); |
| 150 | |
| 151 | assertEquals(4, clicked.xval); |
| 152 | assertEquals(40, clicked.yval); |
| 153 | }; |
| 154 | |
| 155 | /** |
| 156 | * A sanity test to ensure pointClickCallback is not called when out of range. |
| 157 | */ |
| 158 | InteractionModelTestCase.prototype.testNoPointClickCallbackWhenOffPoint = function() { |
| 159 | var clicked; |
| 160 | var g = new Dygraph(document.getElementById("graph"), data2, { |
| 161 | pointClickCallback : function(event, point) { |
| 162 | clicked = point; |
| 163 | } |
| 164 | }); |
| 165 | |
| 166 | InteractionModelTestCase.clickAt(g, 5, 40); |
| 167 | |
| 168 | assertUndefined(clicked); |
| 169 | }; |
| 170 | |
| 171 | /** |
| 172 | * Ensures pointClickCallback circle size is taken into account. |
| 173 | */ |
| 174 | InteractionModelTestCase.prototype.testPointClickCallback_circleSize = function() { |
| 175 | // TODO(konigsberg): Implement. |
| 176 | }; |
| 177 | |
| 178 | /** |
| 179 | * Ensures that pointClickCallback is called prior to clickCallback |
| 180 | */ |
| 181 | InteractionModelTestCase.prototype.testPointClickCallbackCalledPriorToClickCallback = function() { |
| 182 | var counter = 0; |
| 183 | var pointClicked; |
| 184 | var clicked; |
| 185 | var g = new Dygraph(document.getElementById("graph"), data2, { |
| 186 | pointClickCallback : function(event, point) { |
| 187 | counter++; |
| 188 | pointClicked = counter; |
| 189 | }, |
| 190 | clickCallback : function(event, point) { |
| 191 | counter++; |
| 192 | clicked = counter; |
| 193 | } |
| 194 | }); |
| 195 | |
| 196 | InteractionModelTestCase.clickAt(g, 4, 40); |
| 197 | assertEquals(1, pointClicked); |
| 198 | assertEquals(2, clicked); |
| 199 | }; |
| 200 | |
| 201 | /** |
| 202 | * Ensures that when there's no pointClickCallback, clicking on a point still calls |
| 203 | * clickCallback |
| 204 | */ |
| 205 | InteractionModelTestCase.prototype.testClickCallback_clickOnPoint = function() { |
| 206 | var clicked; |
| 207 | var g = new Dygraph(document.getElementById("graph"), data2, { |
| 208 | clickCallback : function(event, point) { |
| 209 | clicked = 1; |
| 210 | } |
| 211 | }); |
| 212 | |
| 213 | InteractionModelTestCase.clickAt(g, 4, 40); |
| 214 | assertEquals(1, clicked); |
| 215 | }; |
| 216 | |