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