Updated the random function generator to be less sporadic.
[dygraphs.git] / auto_tests / tests / interaction_model.js
CommitLineData
9c831431
RK
1/**
2 * @fileoverview Test cases for the interaction model.
3 *
4 * @author konigsberg@google.com (Robert Konigsbrg)
5 */
6var InteractionModelTestCase = TestCase("interaction-model");
7
8InteractionModelTestCase.prototype.setUp = function() {
9 document.body.innerHTML = "<div id='graph'></div>";
10};
11
12InteractionModelTestCase.prototype.tearDown = function() {
13};
14
af3a17a8
RK
15var data1 = "X,Y\n" +
16 "20,-1\n" +
17 "21,0\n" +
18 "22,1\n" +
19 "23,0\n";
20
21var 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
9c831431
RK
32function 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
41InteractionModelTestCase.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 */
65InteractionModelTestCase.prototype.testClickCallbackIsCalled = function() {
66 var clicked;
67
68 var clickCallback = function(event, x) {
69 clicked = x;
70 };
71
9c831431 72 var graph = document.getElementById("graph");
af3a17a8 73 var g = new Dygraph(graph, data1,
9c831431
RK
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 */
91InteractionModelTestCase.prototype.testClickCallbackIsCalledOnCustomPan = function() {
92 var clicked;
93
94 var clickCallback = function(event, x) {
95 clicked = x;
96 };
97
9c831431
RK
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");
af3a17a8 123 var g = new Dygraph(graph, data1, opts);
9c831431
RK
124
125 DygraphOps.dispatchMouseDown_Point(g, 10, 10);
126 DygraphOps.dispatchMouseMove_Point(g, 10, 10);
127 DygraphOps.dispatchMouseUp_Point(g, 10, 10);
128
fc4e84fa 129 assertEquals(20, clicked);
9c831431
RK
130};
131
af3a17a8
RK
132InteractionModelTestCase.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 */
141InteractionModelTestCase.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 */
158InteractionModelTestCase.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 */
174InteractionModelTestCase.prototype.testPointClickCallback_circleSize = function() {
175 // TODO(konigsberg): Implement.
176};
177
178/**
179 * Ensures that pointClickCallback is called prior to clickCallback
180 */
181InteractionModelTestCase.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 */
205InteractionModelTestCase.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