2 * @fileoverview Test cases for the interaction model.
4 * @author konigsberg@google.com (Robert Konigsbrg)
6 var InteractionModelTestCase
= TestCase("interaction-model");
8 InteractionModelTestCase
.prototype.setUp
= function() {
9 document
.body
.innerHTML
= "<div id='graph'></div>";
12 InteractionModelTestCase
.prototype.tearDown
= 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
);
41 InteractionModelTestCase
.prototype.pan
= function(g
, xRange
, yRange
) {
42 var originalXRange
= g
.xAxisRange();
43 var originalYRange
= g
.yAxisRange(0);
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]);
49 assertEqualsDelta(xRange
, g
.xAxisRange(), 0.2);
50 // assertEqualsDelta(originalYRange, g.yAxisRange(0), 0.2); // Not
true, it
's something in the middle.
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]);
57 assertEqualsDelta(xRange, g.xAxisRange(), 0.2);
58 assertEqualsDelta(yRange, g.yAxisRange(0), 0.2);
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.
65 InteractionModelTestCase.prototype.testClickCallbackIsCalled = function() {
68 var clickCallback = function(event, x) {
72 var graph = document.getElementById("graph");
73 var g = new Dygraph(graph, data1,
77 clickCallback : clickCallback
80 DygraphOps.dispatchMouseDown_Point(g, 10, 10);
81 DygraphOps.dispatchMouseMove_Point(g, 10, 10);
82 DygraphOps.dispatchMouseUp_Point(g, 10, 10);
84 assertEquals(20, clicked);
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.
91 InteractionModelTestCase.prototype.testClickCallbackIsCalledOnCustomPan = function() {
94 var clickCallback = function(event, x) {
98 function customDown(event, g, context) {
99 context.initializeMouseDown(event, g, context);
100 Dygraph.startPan(event, g, context);
103 function customMove(event, g, context) {
104 Dygraph.movePan(event, g, context);
107 function customUp(event, g, context) {
108 Dygraph.endPan(event, g, context);
114 clickCallback : clickCallback,
116 'mousedown
' : customDown,
117 'mousemove
' : customMove,
118 'mouseup
' : customUp,
122 var graph = document.getElementById("graph");
123 var g = new Dygraph(graph, data1, opts);
125 DygraphOps.dispatchMouseDown_Point(g, 10, 10);
126 DygraphOps.dispatchMouseMove_Point(g, 10, 10);
127 DygraphOps.dispatchMouseUp_Point(g, 10, 10);
129 assertEquals(20, clicked);
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);
139 * This tests that clickCallback is still called with the nonInteractiveModel.
141 InteractionModelTestCase.prototype.testClickCallbackIsCalledWithNonInteractiveModel = function() {
144 // TODO(danvk): also test pointClickCallback here.
145 var clickCallback = function(event, x) {
152 clickCallback : clickCallback,
153 interactionModel : Dygraph.Interaction.nonInteractiveModel_
156 var graph = document.getElementById("graph");
157 var g = new Dygraph(graph, data1, opts);
159 DygraphOps.dispatchMouseDown_Point(g, 10, 10);
160 DygraphOps.dispatchMouseMove_Point(g, 10, 10);
161 DygraphOps.dispatchMouseUp_Point(g, 10, 10);
163 assertEquals(20, clicked);
167 * A sanity test to ensure pointClickCallback is called.
169 InteractionModelTestCase.prototype.testPointClickCallback = function() {
171 var g = new Dygraph(document.getElementById("graph"), data2, {
172 pointClickCallback : function(event, point) {
177 InteractionModelTestCase.clickAt(g, 4, 40);
179 assertEquals(4, clicked.xval);
180 assertEquals(40, clicked.yval);
184 * A sanity test to ensure pointClickCallback is not called when out of range.
186 InteractionModelTestCase.prototype.testNoPointClickCallbackWhenOffPoint = function() {
188 var g = new Dygraph(document.getElementById("graph"), data2, {
189 pointClickCallback : function(event, point) {
194 InteractionModelTestCase.clickAt(g, 5, 40);
196 assertUndefined(clicked);
200 * Ensures pointClickCallback circle size is taken into account.
202 InteractionModelTestCase.prototype.testPointClickCallback_circleSize = function() {
203 // TODO(konigsberg): Implement.
207 * Ensures that pointClickCallback is called prior to clickCallback
209 InteractionModelTestCase.prototype.testPointClickCallbackCalledPriorToClickCallback = function() {
213 var g = new Dygraph(document.getElementById("graph"), data2, {
214 pointClickCallback : function(event, point) {
216 pointClicked = counter;
218 clickCallback : function(event, point) {
224 InteractionModelTestCase.clickAt(g, 4, 40);
225 assertEquals(1, pointClicked);
226 assertEquals(2, clicked);
230 * Ensures that when there's no pointClickCallback
, clicking on a point still calls
233 InteractionModelTestCase
.prototype.testClickCallback_clickOnPoint
= function() {
235 var g
= new Dygraph(document
.getElementById("graph"), data2
, {
236 clickCallback
: function(event
, point
) {
241 InteractionModelTestCase
.clickAt(g
, 4, 40);
242 assertEquals(1, clicked
);
245 InteractionModelTestCase
.prototype.testIsZoomed_none
= function() {
246 var g
= new Dygraph(document
.getElementById("graph"), data2
, {});
248 assertFalse(g
.isZoomed());
249 assertFalse(g
.isZoomed("x"));
250 assertFalse(g
.isZoomed("y"));
253 InteractionModelTestCase
.prototype.testIsZoomed_x
= function() {
254 var g
= new Dygraph(document
.getElementById("graph"), data2
, {});
256 DygraphOps
.dispatchMouseDown_Point(g
, 10, 10);
257 DygraphOps
.dispatchMouseMove_Point(g
, 30, 10);
258 DygraphOps
.dispatchMouseUp_Point(g
, 30, 10);
260 assertTrue(g
.isZoomed());
261 assertTrue(g
.isZoomed("x"));
262 assertFalse(g
.isZoomed("y"));
265 InteractionModelTestCase
.prototype.testIsZoomed_y
= function() {
266 var g
= new Dygraph(document
.getElementById("graph"), data2
, {});
268 DygraphOps
.dispatchMouseDown_Point(g
, 10, 10);
269 DygraphOps
.dispatchMouseMove_Point(g
, 10, 30);
270 DygraphOps
.dispatchMouseUp_Point(g
, 10, 30);
272 assertTrue(g
.isZoomed());
273 assertFalse(g
.isZoomed("x"));
274 assertTrue(g
.isZoomed("y"));
277 InteractionModelTestCase
.prototype.testIsZoomed_both
= function() {
278 var g
= new Dygraph(document
.getElementById("graph"), data2
, {});
281 DygraphOps
.dispatchMouseDown_Point(g
, 10, 10);
282 DygraphOps
.dispatchMouseMove_Point(g
, 30, 10);
283 DygraphOps
.dispatchMouseUp_Point(g
, 30, 10);
286 DygraphOps
.dispatchMouseDown_Point(g
, 10, 10);
287 DygraphOps
.dispatchMouseMove_Point(g
, 10, 30);
288 DygraphOps
.dispatchMouseUp_Point(g
, 10, 30);
291 assertTrue(g
.isZoomed());
292 assertTrue(g
.isZoomed("x"));
293 assertTrue(g
.isZoomed("y"));
296 InteractionModelTestCase
.prototype.testIsZoomed_updateOptions_none
= function() {
297 var g
= new Dygraph(document
.getElementById("graph"), data2
, {});
301 assertFalse(g
.isZoomed());
302 assertFalse(g
.isZoomed("x"));
303 assertFalse(g
.isZoomed("y"));
306 InteractionModelTestCase
.prototype.testIsZoomed_updateOptions_x
= function() {
307 var g
= new Dygraph(document
.getElementById("graph"), data2
, {});
309 g
.updateOptions({dateWindow
: [-.5, .3]});
310 assertTrue(g
.isZoomed());
311 assertTrue(g
.isZoomed("x"));
312 assertFalse(g
.isZoomed("y"));
315 InteractionModelTestCase
.prototype.testIsZoomed_updateOptions_y
= function() {
316 var g
= new Dygraph(document
.getElementById("graph"), data2
, {});
318 g
.updateOptions({valueRange
: [1, 10]});
320 assertTrue(g
.isZoomed());
321 assertFalse(g
.isZoomed("x"));
322 assertTrue(g
.isZoomed("y"));
325 InteractionModelTestCase
.prototype.testIsZoomed_updateOptions_both
= function() {
326 var g
= new Dygraph(document
.getElementById("graph"), data2
, {});
328 g
.updateOptions({dateWindow
: [-1, 1], valueRange
: [1, 10]});
330 assertTrue(g
.isZoomed());
331 assertTrue(g
.isZoomed("x"));
332 assertTrue(g
.isZoomed("y"));
336 InteractionModelTestCase
.prototype.testCorrectAxisValueRangeAfterUnzoom
= function() {
337 var g
= new Dygraph(document
.getElementById("graph"), data2
, {valueRange
:[1,50],dateRange
:[1,9],animatedZooms
:false});
340 DygraphOps
.dispatchMouseDown_Point(g
, 10, 10);
341 DygraphOps
.dispatchMouseMove_Point(g
, 30, 10);
342 DygraphOps
.dispatchMouseUp_Point(g
, 30, 10);
345 DygraphOps
.dispatchMouseDown_Point(g
, 10, 10);
346 DygraphOps
.dispatchMouseMove_Point(g
, 10, 30);
347 DygraphOps
.dispatchMouseUp_Point(g
, 10, 30);
348 currentYAxisRange
= g
.yAxisRange();
349 currentXAxisRange
= g
.xAxisRange();
351 //check that the range for the axis has changed
352 assertNotEquals(1,currentXAxisRange
[0]);
353 assertNotEquals(10,currentXAxisRange
[1]);
354 assertNotEquals(1,currentYAxisRange
[0]);
355 assertNotEquals(50,currentYAxisRange
[1]);
357 // unzoom by doubleclick. This is really the order in which a browser
358 // generates events, and we depend on it.
359 DygraphOps
.dispatchMouseDown_Point(g
, 10, 10);
360 DygraphOps
.dispatchMouseUp_Point(g
, 10, 10);
361 DygraphOps
.dispatchMouseDown_Point(g
, 10, 10);
362 DygraphOps
.dispatchMouseUp_Point(g
, 10, 10);
363 DygraphOps
.dispatchDoubleClick(g
, null);
365 // check if range for y-axis was reset to original value
366 // TODO check if range for x-axis is correct.
367 // Currently not possible because dateRange is set to null and extremes are returned
368 newYAxisRange
= g
.yAxisRange();
369 assertEquals(1,newYAxisRange
[0]);
370 assertEquals(50,newYAxisRange
[1]);