2 * @fileoverview Tests for the plugins option.
4 * @author konigsberg@google.com (Robert Konigsberg)
6 var pluginsTestCase
= TestCase("plugins");
8 pluginsTestCase
.prototype.setUp
= function() {
9 document
.body
.innerHTML
= "<div id='graph'></div>";
11 this.data
= "X,Y1,Y2\n" +
19 pluginsTestCase
.prototype.tearDown
= function() {
22 pluginsTestCase
.prototype.testWillDrawChart
= function() {
25 var plugin
= (function() {
29 p
.prototype.activate
= function(g
) {
31 willDrawChart
: this.willDrawChart
35 p
.prototype.willDrawChart
= function(e
) {
42 var graph
= document
.getElementById("graph");
43 var g
= new Dygraph(graph
, this.data
, {plugins
: [plugin
]});
45 assertEquals(1, draw
);
48 pluginsTestCase
.prototype.testPassingInstance
= function() {
49 // You can also pass an instance of a plugin instead of a Plugin class.
52 activate
: function(g
) {
54 willDrawChart
: this.willDrawChart
57 willDrawChart
: function(g
) {
62 var graph
= document
.getElementById("graph");
63 var g
= new Dygraph(graph
, this.data
, {plugins
: [p
]});
65 assertEquals(1, draw
);
68 pluginsTestCase
.prototype.testPreventDefault
= function() {
78 pointClickPreventDefault
: false,
79 clickPreventDefault
: false,
80 activate
: function(g
) {
82 pointClick
: this.pointClick
,
86 pointClick
: function(e
) {
87 events
.push(['plugin.pointClick', e
.point
.xval
, e
.point
.yval
]);
88 if (this.pointClickPreventDefault
) {
93 events
.push(['plugin.click', e
.xval
]);
94 if (this.clickPreventDefault
) {
100 var graph
= document
.getElementById("graph");
101 var g
= new Dygraph(graph
, data1
, {
103 clickCallback
: function(e
, x
) {
104 events
.push(['clickCallback', x
]);
106 pointClickCallback
: function(e
, pt
) {
107 events
.push(['pointClickCallback', pt
.xval
, pt
.yval
]);
111 // Click the point at x=20
112 function clickOnPoint() {
114 DygraphOps
.dispatchMouseDown_Point(g
, x
, y
);
115 DygraphOps
.dispatchMouseMove_Point(g
, x
, y
);
116 DygraphOps
.dispatchMouseUp_Point(g
, x
, y
);
119 p
.pointClickPreventDefault
= false;
120 p
.clickPreventDefault
= false;
123 ['plugin.pointClick', 20, -1],
124 ['pointClickCallback', 20, -1],
125 ['plugin.click', 20],
126 ['clickCallback', 20]
130 p
.pointClickPreventDefault
= true;
131 p
.clickPreventDefault
= false;
134 ['plugin.pointClick', 20, -1]
138 p
.pointClickPreventDefault
= false;
139 p
.clickPreventDefault
= true;
142 ['plugin.pointClick', 20, -1],
143 ['pointClickCallback', 20, -1],
148 pluginsTestCase
.prototype.testEventSequence
= function() {
151 var eventLogger
= function(name
) {
158 activate
: function(g
) {
160 clearChart
: eventLogger('clearChart'),
161 predraw
: eventLogger('predraw'),
162 willDrawChart
: eventLogger('willDrawChart'),
163 didDrawChart
: eventLogger('didDrawChart'),
164 dataWillUpdate
: eventLogger('dataWillUpdate'),
165 dataDidUpdate
: eventLogger('dataDidUpdate')
170 var graph
= document
.getElementById("graph");
171 var g
= new Dygraph(graph
, this.data
, {plugins
: [p
]});
173 // Initial draw sequence
175 "dataDidUpdate", // should dataWillUpdate be called here, too?
182 // An options change triggers a redraw, but doesn't change the data.
184 g
.updateOptions({series
: {Y1
: {color
: 'blue'}}});
192 // A pan shouldn't cause a new "predraw"
194 DygraphOps
.dispatchMouseDown_Point(g
, 100, 100, {shiftKey
: true});
195 DygraphOps
.dispatchMouseMove_Point(g
, 200, 100, {shiftKey
: true});
196 DygraphOps
.dispatchMouseUp_Point(g
, 200, 100, {shiftKey
: true});
203 // New data triggers the full sequence.
205 g
.updateOptions({file
: this.data
+ '\n4,1,2'});
216 pluginsTestCase
.prototype.testDestroyCalledInOrder
= function() {
217 var destructions
= [];
218 var makePlugin
= function(name
) {
220 activate
: function(g
) { return {} },
221 destroy
: function() {
222 destructions
.push(name
);
227 var graph
= document
.getElementById("graph");
228 var g
= new Dygraph(graph
, this.data
, {
229 plugins
: [makePlugin('p'), makePlugin('q')]
232 assertEquals([], destructions
);
234 assertEquals(['q', 'p'], destructions
);