Add local test selector. SOO MUCH BETTER.
[dygraphs.git] / auto_tests / tests / interaction_model.js
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 * This tests that clickCallback is still called with the nonInteractiveModel.
140 */
141 InteractionModelTestCase.prototype.testClickCallbackIsCalledWithNonInteractiveModel = function() {
142 var clicked;
143
144 // TODO(danvk): also test pointClickCallback here.
145 var clickCallback = function(event, x) {
146 clicked = x;
147 };
148
149 var opts = {
150 width: 100,
151 height : 100,
152 clickCallback : clickCallback,
153 interactionModel : Dygraph.Interaction.nonInteractiveModel_
154 };
155
156 var graph = document.getElementById("graph");
157 var g = new Dygraph(graph, data1, opts);
158
159 DygraphOps.dispatchMouseDown_Point(g, 10, 10);
160 DygraphOps.dispatchMouseMove_Point(g, 10, 10);
161 DygraphOps.dispatchMouseUp_Point(g, 10, 10);
162
163 assertEquals(20, clicked);
164 };
165
166 /**
167 * A sanity test to ensure pointClickCallback is called.
168 */
169 InteractionModelTestCase.prototype.testPointClickCallback = function() {
170 var clicked;
171 var g = new Dygraph(document.getElementById("graph"), data2, {
172 pointClickCallback : function(event, point) {
173 clicked = point;
174 }
175 });
176
177 InteractionModelTestCase.clickAt(g, 4, 40);
178
179 assertEquals(4, clicked.xval);
180 assertEquals(40, clicked.yval);
181 };
182
183 /**
184 * A sanity test to ensure pointClickCallback is not called when out of range.
185 */
186 InteractionModelTestCase.prototype.testNoPointClickCallbackWhenOffPoint = function() {
187 var clicked;
188 var g = new Dygraph(document.getElementById("graph"), data2, {
189 pointClickCallback : function(event, point) {
190 clicked = point;
191 }
192 });
193
194 InteractionModelTestCase.clickAt(g, 5, 40);
195
196 assertUndefined(clicked);
197 };
198
199 /**
200 * Ensures pointClickCallback circle size is taken into account.
201 */
202 InteractionModelTestCase.prototype.testPointClickCallback_circleSize = function() {
203 // TODO(konigsberg): Implement.
204 };
205
206 /**
207 * Ensures that pointClickCallback is called prior to clickCallback
208 */
209 InteractionModelTestCase.prototype.testPointClickCallbackCalledPriorToClickCallback = function() {
210 var counter = 0;
211 var pointClicked;
212 var clicked;
213 var g = new Dygraph(document.getElementById("graph"), data2, {
214 pointClickCallback : function(event, point) {
215 counter++;
216 pointClicked = counter;
217 },
218 clickCallback : function(event, point) {
219 counter++;
220 clicked = counter;
221 }
222 });
223
224 InteractionModelTestCase.clickAt(g, 4, 40);
225 assertEquals(1, pointClicked);
226 assertEquals(2, clicked);
227 };
228
229 /**
230 * Ensures that when there's no pointClickCallback, clicking on a point still calls
231 * clickCallback
232 */
233 InteractionModelTestCase.prototype.testClickCallback_clickOnPoint = function() {
234 var clicked;
235 var g = new Dygraph(document.getElementById("graph"), data2, {
236 clickCallback : function(event, point) {
237 clicked = 1;
238 }
239 });
240
241 InteractionModelTestCase.clickAt(g, 4, 40);
242 assertEquals(1, clicked);
243 };
244
245 InteractionModelTestCase.prototype.testIsZoomed_1 = function() {
246 };
247