Commit | Line | Data |
---|---|---|
1c6b239c | 1 | /** |
2 | * @fileoverview Test cases for the callbacks. | |
3 | * | |
4 | * @author uemit.seren@gmail.com (Ümit Seren) | |
5 | */ | |
6 | ||
7 | var CallbackTestCase = TestCase("callback"); | |
8 | ||
9 | CallbackTestCase.prototype.setUp = function() { | |
10 | document.body.innerHTML = "<div id='graph'></div>"; | |
11 | }; | |
12 | ||
13 | CallbackTestCase.prototype.tearDown = function() { | |
14 | }; | |
15 | ||
72c12eda RK |
16 | var data = "X,a\,b,c\n" + |
17 | "10,-1,1,2\n" + | |
18 | "11,0,3,1\n" + | |
19 | "12,1,4,2\n" + | |
20 | "13,0,2,3\n"; | |
1c6b239c | 21 | |
22 | ||
3447a945 RK |
23 | /** |
24 | * This tests that when the function idxToRow_ returns the proper row and the onHiglightCallback | |
a8332379 | 25 | * is properly called when the first series is hidden (setVisibility = false) |
3447a945 RK |
26 | * |
27 | */ | |
28 | CallbackTestCase.prototype.testHighlightCallbackIsCalled = function() { | |
29 | var h_row; | |
30 | var h_pts; | |
31 | ||
72c12eda | 32 | var highlightCallback = function(e, x, pts, row) { |
3447a945 RK |
33 | h_row = row; |
34 | h_pts = pts; | |
35 | }; | |
1c6b239c | 36 | |
3447a945 RK |
37 | var graph = document.getElementById("graph"); |
38 | var g = new Dygraph(graph, data, | |
39 | { | |
40 | width: 100, | |
41 | height : 100, | |
42 | visibility: [false, true, true], | |
43 | highlightCallback : highlightCallback, | |
44 | }); | |
45 | ||
46 | DygraphOps.dispatchMouseMove(g, 13, 10); | |
47 | ||
48 | //check correct row is returned | |
49 | assertEquals(3, h_row); | |
50 | //check there are only two points (because first series is hidden) | |
51 | assertEquals(2, h_pts.length); | |
52 | }; | |
a8332379 RK |
53 | |
54 | ||
55 | /** | |
56 | * Test that drawPointCallback isn't called when drawPoints is false | |
57 | */ | |
58 | CallbackTestCase.prototype.testDrawPointCallback_disabled = function() { | |
59 | var called = false; | |
60 | ||
72c12eda | 61 | var callback = function() { |
a8332379 RK |
62 | called = true; |
63 | }; | |
64 | ||
65 | var graph = document.getElementById("graph"); | |
66 | var g = new Dygraph(graph, data, { | |
67 | drawPointCallback : callback, | |
68 | }); | |
69 | ||
70 | assertFalse(called); | |
71 | }; | |
72 | ||
73 | /** | |
74 | * Test that drawPointCallback is called when drawPoints is true | |
75 | */ | |
76 | CallbackTestCase.prototype.testDrawPointCallback_enabled = function() { | |
77 | var called = false; | |
78 | ||
72c12eda | 79 | var callback = function() { |
a8332379 RK |
80 | called = true; |
81 | }; | |
82 | ||
83 | var graph = document.getElementById("graph"); | |
84 | var g = new Dygraph(graph, data, { | |
85 | drawPoints : true, | |
86 | drawPointCallback : callback | |
87 | }); | |
88 | ||
89 | assertTrue(called); | |
90 | }; | |
72c12eda RK |
91 | |
92 | /** | |
93 | * Test that drawPointCallback is called when drawPoints is true | |
94 | */ | |
95 | CallbackTestCase.prototype.testDrawPointCallback_pointSize = function() { | |
96 | var pointSize = 0; | |
97 | var count = 0; | |
98 | ||
99 | var callback = function(g, seriesName, canvasContext, cx, cy, color, pointSizeParam) { | |
100 | pointSize = pointSizeParam; | |
101 | count++; | |
102 | }; | |
103 | ||
104 | var graph = document.getElementById("graph"); | |
105 | var g = new Dygraph(graph, data, { | |
106 | drawPoints : true, | |
107 | drawPointCallback : callback | |
108 | }); | |
109 | ||
110 | assertEquals(1.5, pointSize); | |
111 | assertEquals(12, count); // one call per data point. | |
112 | ||
113 | var g = new Dygraph(graph, data, { | |
114 | drawPoints : true, | |
115 | drawPointCallback : callback, | |
116 | pointSize : 8 | |
117 | }); | |
118 | ||
119 | assertEquals(8, pointSize); | |
120 | }; | |
121 | ||
122 | /** | |
123 | * This tests that when the function idxToRow_ returns the proper row and the onHiglightCallback | |
124 | * is properly called when the first series is hidden (setVisibility = false) | |
125 | * | |
126 | */ | |
5879307d | 127 | CallbackTestCase.prototype.testDrawHighlightPointCallbackIsCalled = function() { |
72c12eda RK |
128 | var called = false; |
129 | ||
5879307d | 130 | var drawHighlightPointCallback = function() { |
72c12eda RK |
131 | called = true; |
132 | }; | |
133 | ||
134 | var graph = document.getElementById("graph"); | |
135 | var g = new Dygraph(graph, data, | |
136 | { | |
137 | width: 100, | |
138 | height : 100, | |
5879307d | 139 | drawHighlightPointCallback : drawHighlightPointCallback |
72c12eda RK |
140 | }); |
141 | ||
142 | assertFalse(called); | |
143 | DygraphOps.dispatchMouseMove(g, 13, 10); | |
144 | assertTrue(called); | |
145 | }; |