Merge pull request #674 from danvk/module
[dygraphs.git] / auto_tests / tests / scrolling_div.js
CommitLineData
bf06e350
RK
1/**
2 * @fileoverview Test cases for a graph contained in a scrolling div
3 *
4 * @author konigsberg@google.com (Robert Konigsbrg)
5 */
e8c70e4e
DV
6
7import Dygraph from '../../src/dygraph';
8import DygraphOps from './DygraphOps';
9
89fdcedb 10describe("scrolling-div", function() {
bf06e350 11
319d0361
DV
12var point, g;
13
89fdcedb 14beforeEach(function() {
bf06e350
RK
15
16var LOREM_IPSUM =
17 "<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod\n" +
18 "tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,\n" +
19 "quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo\n" +
20 "consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse\n" +
21 "cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat\n" +
22 "non proident, sunt in culpa qui officia deserunt mollit anim id est\n" +
23 "laborum.</p>";
24
e8c70e4e
DV
25 var testDiv = document.getElementById('graph');
26 testDiv.innerHTML =
a3077002 27 "<div id='scroller' style='overflow: scroll; height: 450px; width: 800px;'>" +
e8c70e4e 28 "<div id='graph-inner'></div>" +
bf06e350
RK
29 "<div style='height:100px; background-color:green;'>" + LOREM_IPSUM + " </div>" +
30 "<div style='height:100px; background-color:red;'>" + LOREM_IPSUM + "</div>" +
bf06e350
RK
31 "</div>";
32
e8c70e4e
DV
33 // The old test runner had an 8px margin on the body
34 // The Mocha test runner does not. We set it on the test div to keep the
35 // coordinates the same.
36 testDiv.style.margin = '8px';
37
bf06e350
RK
38 var data = [
39 [ 10, 1 ],
40 [ 20, 3 ],
41 [ 30, 2 ],
42 [ 40, 4 ],
43 [ 50, 3 ],
44 [ 60, 5 ],
45 [ 70, 4 ],
46 [ 80, 6 ] ];
47
e8c70e4e 48 var graph = document.getElementById("graph-inner");
bf06e350 49
319d0361
DV
50 point = null;
51 g = new Dygraph(graph, data,
bf06e350
RK
52 {
53 labels : ['a', 'b'],
54 drawPoints : true,
55 highlightCircleSize : 6,
dc910fce
DV
56 pointClickCallback : function(evt, p) {
57 point = p;
bf06e350
RK
58 }
59 }
60 );
61
89fdcedb 62});
bf06e350 63
27561e51
DV
64// This is usually something like 15, but for OS X Lion and its auto-hiding
65// scrollbars, it's 0. This is a large enough difference that we need to
66// consider it when synthesizing clicks.
67// Adapted from http://davidwalsh.name/detect-scrollbar-width
319d0361 68var detectScrollbarWidth = function() {
27561e51
DV
69 // Create the measurement node
70 var scrollDiv = document.createElement("div");
71 scrollDiv.style.width = "100px";
72 scrollDiv.style.height = "100px";
73 scrollDiv.style.overflow = "scroll";
74 scrollDiv.style.position = "absolute";
75 scrollDiv.style.top = "-9999px";
76 document.body.appendChild(scrollDiv);
77
78 // Get the scrollbar width
79 var scrollbarWidth = scrollDiv.offsetWidth - scrollDiv.clientWidth;
80
81 // Delete the DIV
82 document.body.removeChild(scrollDiv);
83
84 return scrollbarWidth;
85};
86
bf06e350
RK
87/**
88 * This tests that when the nested div is unscrolled, things work normally.
89 */
89fdcedb 90it('testUnscrolledDiv', function() {
bf06e350 91
a3077002 92 document.getElementById('scroller').scrollTop = 0;
bf06e350
RK
93
94 var clickOn4_40 = {
95 clientX: 244,
96 clientY: 131,
97 screenX: 416,
98 screenY: 320
99 };
100
319d0361
DV
101 DygraphOps.dispatchCanvasEvent(g, DygraphOps.createEvent(clickOn4_40, { type : 'mousemove' }));
102 DygraphOps.dispatchCanvasEvent(g, DygraphOps.createEvent(clickOn4_40, { type : 'mousedown' }));
103 DygraphOps.dispatchCanvasEvent(g, DygraphOps.createEvent(clickOn4_40, { type : 'mouseup' }));
bf06e350 104
89fdcedb
DV
105 assert.equal(40, point.xval);
106 assert.equal(4, point.yval);
107});
bf06e350
RK
108
109/**
110 * This tests that when the nested div is scrolled, things work normally.
111 */
89fdcedb 112it('testScrolledDiv', function() {
a3077002 113 document.getElementById('scroller').scrollTop = 117;
bf06e350
RK
114
115 var clickOn4_40 = {
116 clientX: 244,
319d0361 117 clientY: 30 - detectScrollbarWidth(),
bf06e350
RK
118 screenX: 416,
119 screenY: 160
120 };
121
319d0361
DV
122 DygraphOps.dispatchCanvasEvent(g, DygraphOps.createEvent(clickOn4_40, { type : 'mousemove' }));
123 DygraphOps.dispatchCanvasEvent(g, DygraphOps.createEvent(clickOn4_40, { type : 'mousedown' }));
124 DygraphOps.dispatchCanvasEvent(g, DygraphOps.createEvent(clickOn4_40, { type : 'mouseup' }));
bf06e350 125
89fdcedb
DV
126 assert.equal(40, point.xval);
127 assert.equal(4, point.yval);
128});
129
130});