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