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
);