2 * @fileoverview Test cases for the interaction model.
4 * @author konigsberg@google.com (Robert Konigsbrg)
6 describe("interaction-model", function() {
8 beforeEach(function() {
9 document
.body
.innerHTML
= "<div id='graph'></div>";
12 afterEach(function() {
32 function getXLabels() {
33 var x_labels
= document
.getElementsByClassName("dygraph-axis-label-x");
35 for (var i
= 0; i
< x_labels
.length
; i
++) {
36 ary
.push(x_labels
[i
].innerHTML
);
42 it('testPan', function() {
43 var originalXRange = g.xAxisRange();
44 var originalYRange = g.yAxisRange(0);
46 DygraphOps.dispatchMouseDown(g, xRange[0], yRange[0]);
47 DygraphOps.dispatchMouseMove(g, xRange[1], yRange[0]); // this is really necessary.
48 DygraphOps.dispatchMouseUp(g, xRange[1], yRange[0]);
50 assert.closeTo(xRange, g.xAxisRange(), 0.2);
51 // assert.closeTo(originalYRange, g.yAxisRange(0), 0.2); // Not true, it's something in the middle.
53 var midX = (xRange[1] - xRange[0]) / 2;
54 DygraphOps.dispatchMouseDown(g, midX, yRange[0]);
55 DygraphOps.dispatchMouseMove(g, midX, yRange[1]); // this is really necessary.
56 DygraphOps.dispatchMouseUp(g, midX, yRange[1]);
58 assert.closeTo(xRange, g.xAxisRange(), 0.2);
59 assert.closeTo(yRange, g.yAxisRange(0), 0.2);
64 * This tests that when changing the interaction model so pan is used instead
65 * of zoom as the default behavior, a standard click method is still called.
67 it('testClickCallbackIsCalled', function() {
70 var clickCallback
= function(event
, x
) {
74 var graph
= document
.getElementById("graph");
75 var g
= new Dygraph(graph
, data1
,
79 clickCallback
: clickCallback
82 DygraphOps
.dispatchMouseDown_Point(g
, 10, 10);
83 DygraphOps
.dispatchMouseMove_Point(g
, 10, 10);
84 DygraphOps
.dispatchMouseUp_Point(g
, 10, 10);
86 assert
.equal(20, clicked
);
90 * This tests that when changing the interaction model so pan is used instead
91 * of zoom as the default behavior, a standard click method is still called.
93 it('testClickCallbackIsCalledOnCustomPan', function() {
96 var clickCallback
= function(event
, x
) {
100 function customDown(event
, g
, context
) {
101 context
.initializeMouseDown(event
, g
, context
);
102 Dygraph
.startPan(event
, g
, context
);
105 function customMove(event
, g
, context
) {
106 Dygraph
.movePan(event
, g
, context
);
109 function customUp(event
, g
, context
) {
110 Dygraph
.endPan(event
, g
, context
);
116 clickCallback
: clickCallback
,
118 'mousedown' : customDown
,
119 'mousemove' : customMove
,
120 'mouseup' : customUp
,
124 var graph
= document
.getElementById("graph");
125 var g
= new Dygraph(graph
, data1
, opts
);
127 DygraphOps
.dispatchMouseDown_Point(g
, 10, 10);
128 DygraphOps
.dispatchMouseMove_Point(g
, 10, 10);
129 DygraphOps
.dispatchMouseUp_Point(g
, 10, 10);
131 assert
.equal(20, clicked
);
134 var clickAt
= function(g
, x
, y
) {
135 DygraphOps
.dispatchMouseDown(g
, x
, y
);
136 DygraphOps
.dispatchMouseMove(g
, x
, y
);
137 DygraphOps
.dispatchMouseUp(g
, x
, y
);
141 * This tests that clickCallback is still called with the nonInteractiveModel.
143 it('testClickCallbackIsCalledWithNonInteractiveModel', function() {
146 // TODO(danvk): also test pointClickCallback here.
147 var clickCallback
= function(event
, x
) {
154 clickCallback
: clickCallback
,
155 interactionModel
: Dygraph
.Interaction
.nonInteractiveModel_
158 var graph
= document
.getElementById("graph");
159 var g
= new Dygraph(graph
, data1
, opts
);
161 DygraphOps
.dispatchMouseDown_Point(g
, 10, 10);
162 DygraphOps
.dispatchMouseMove_Point(g
, 10, 10);
163 DygraphOps
.dispatchMouseUp_Point(g
, 10, 10);
165 assert
.equal(20, clicked
);
169 * A sanity test to ensure pointClickCallback is called.
171 it('testPointClickCallback', function() {
173 var g
= new Dygraph(document
.getElementById("graph"), data2
, {
174 pointClickCallback
: function(event
, point
) {
181 assert
.equal(4, clicked
.xval
);
182 assert
.equal(40, clicked
.yval
);
186 * A sanity test to ensure pointClickCallback is not called when out of range.
188 it('testNoPointClickCallbackWhenOffPoint', function() {
190 var g
= new Dygraph(document
.getElementById("graph"), data2
, {
191 pointClickCallback
: function(event
, point
) {
198 assert
.isUndefined(clicked
);
202 * Ensures pointClickCallback circle size is taken into account.
204 it('testPointClickCallback_circleSize', function() {
205 // TODO(konigsberg): Implement.
209 * Ensures that pointClickCallback is called prior to clickCallback
211 it('testPointClickCallbackCalledPriorToClickCallback', function() {
215 var g
= new Dygraph(document
.getElementById("graph"), data2
, {
216 pointClickCallback
: function(event
, point
) {
218 pointClicked
= counter
;
220 clickCallback
: function(event
, point
) {
227 assert
.equal(1, pointClicked
);
228 assert
.equal(2, clicked
);
232 * Ensures that when there's no pointClickCallback, clicking on a point still calls
235 it('testClickCallback_clickOnPoint', function() {
237 var g
= new Dygraph(document
.getElementById("graph"), data2
, {
238 clickCallback
: function(event
, point
) {
244 assert
.equal(1, clicked
);
247 it('testIsZoomed_none', function() {
248 var g
= new Dygraph(document
.getElementById("graph"), data2
, {});
250 assert
.isFalse(g
.isZoomed());
251 assert
.isFalse(g
.isZoomed("x"));
252 assert
.isFalse(g
.isZoomed("y"));
255 it('testIsZoomed_x', function() {
256 var g
= new Dygraph(document
.getElementById("graph"), data2
, {});
258 DygraphOps
.dispatchMouseDown_Point(g
, 100, 100);
259 DygraphOps
.dispatchMouseMove_Point(g
, 130, 100);
260 DygraphOps
.dispatchMouseUp_Point(g
, 130, 100);
262 assert
.isTrue(g
.isZoomed());
263 assert
.isTrue(g
.isZoomed("x"));
264 assert
.isFalse(g
.isZoomed("y"));
267 it('testIsZoomed_y', function() {
268 var g
= new Dygraph(document
.getElementById("graph"), data2
, {});
270 DygraphOps
.dispatchMouseDown_Point(g
, 10, 10);
271 DygraphOps
.dispatchMouseMove_Point(g
, 10, 30);
272 DygraphOps
.dispatchMouseUp_Point(g
, 10, 30);
274 assert
.isTrue(g
.isZoomed());
275 assert
.isFalse(g
.isZoomed("x"));
276 assert
.isTrue(g
.isZoomed("y"));
279 it('testIsZoomed_both', function() {
280 var g
= new Dygraph(document
.getElementById("graph"), data2
, {});
283 DygraphOps
.dispatchMouseDown_Point(g
, 100, 100);
284 DygraphOps
.dispatchMouseMove_Point(g
, 130, 100);
285 DygraphOps
.dispatchMouseUp_Point(g
, 130, 100);
288 DygraphOps
.dispatchMouseDown_Point(g
, 100, 100);
289 DygraphOps
.dispatchMouseMove_Point(g
, 100, 130);
290 DygraphOps
.dispatchMouseUp_Point(g
, 100, 130);
293 assert
.isTrue(g
.isZoomed());
294 assert
.isTrue(g
.isZoomed("x"));
295 assert
.isTrue(g
.isZoomed("y"));
298 it('testIsZoomed_updateOptions_none', function() {
299 var g
= new Dygraph(document
.getElementById("graph"), data2
, {});
303 assert
.isFalse(g
.isZoomed());
304 assert
.isFalse(g
.isZoomed("x"));
305 assert
.isFalse(g
.isZoomed("y"));
308 it('testIsZoomed_updateOptions_x', function() {
309 var g
= new Dygraph(document
.getElementById("graph"), data2
, {});
311 g
.updateOptions({dateWindow
: [-.5, .3]});
312 assert
.isTrue(g
.isZoomed());
313 assert
.isTrue(g
.isZoomed("x"));
314 assert
.isFalse(g
.isZoomed("y"));
317 it('testIsZoomed_updateOptions_y', function() {
318 var g
= new Dygraph(document
.getElementById("graph"), data2
, {});
320 g
.updateOptions({valueRange
: [1, 10]});
322 assert
.isTrue(g
.isZoomed());
323 assert
.isFalse(g
.isZoomed("x"));
324 assert
.isTrue(g
.isZoomed("y"));
327 it('testIsZoomed_updateOptions_both', function() {
328 var g
= new Dygraph(document
.getElementById("graph"), data2
, {});
330 g
.updateOptions({dateWindow
: [-1, 1], valueRange
: [1, 10]});
332 assert
.isTrue(g
.isZoomed());
333 assert
.isTrue(g
.isZoomed("x"));
334 assert
.isTrue(g
.isZoomed("y"));
338 it('testCorrectAxisValueRangeAfterUnzoom', function() {
339 var g
= new Dygraph(document
.getElementById("graph"),
347 DygraphOps
.dispatchMouseDown_Point(g
, 100, 100);
348 DygraphOps
.dispatchMouseMove_Point(g
, 130, 100);
349 DygraphOps
.dispatchMouseUp_Point(g
, 130, 100);
352 DygraphOps
.dispatchMouseDown_Point(g
, 100, 100);
353 DygraphOps
.dispatchMouseMove_Point(g
, 100, 130);
354 DygraphOps
.dispatchMouseUp_Point(g
, 100, 130);
355 var currentYAxisRange
= g
.yAxisRange();
356 var currentXAxisRange
= g
.xAxisRange();
358 //check that the range for the axis has changed
359 assert
.notEqual(1, currentXAxisRange
[0]);
360 assert
.notEqual(10, currentXAxisRange
[1]);
361 assert
.notEqual(1, currentYAxisRange
[0]);
362 assert
.notEqual(50, currentYAxisRange
[1]);
364 // unzoom by doubleclick. This is really the order in which a browser
365 // generates events, and we depend on it.
366 DygraphOps
.dispatchMouseDown_Point(g
, 10, 10);
367 DygraphOps
.dispatchMouseUp_Point(g
, 10, 10);
368 DygraphOps
.dispatchMouseDown_Point(g
, 10, 10);
369 DygraphOps
.dispatchMouseUp_Point(g
, 10, 10);
370 DygraphOps
.dispatchDoubleClick(g
, null);
372 // check if range for y-axis was reset to original value
373 // TODO check if range for x-axis is correct.
374 // Currently not possible because dateRange is set to null and extremes are returned
375 var newYAxisRange
= g
.yAxisRange();
376 assert
.equal(1, newYAxisRange
[0]);
377 assert
.equal(50, newYAxisRange
[1]);
381 * Ensures pointClickCallback is called when some points along the y-axis don't
384 it('testPointClickCallback_missingData', function() {
386 // There's a B-value at 2, but no A-value.
396 var g
= new Dygraph(document
.getElementById("graph"), data
, {
397 pointClickCallback
: function(event
, point
) {
404 assert
.equal(2, clicked
.xval
);
405 assert
.equal(110, clicked
.yval
);