Commit | Line | Data |
---|---|---|
bf06e350 RK |
1 | /** |
2 | * @fileoverview Test cases for a graph contained in a scrolling div | |
3 | * | |
4 | * @author konigsberg@google.com (Robert Konigsbrg) | |
5 | */ | |
89fdcedb | 6 | describe("scrolling-div", function() { |
bf06e350 | 7 | |
319d0361 DV |
8 | var point, g; |
9 | ||
89fdcedb | 10 | beforeEach(function() { |
bf06e350 RK |
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 = | |
a3077002 RK |
22 | "<div id='scroller' style='overflow: scroll; height: 450px; width: 800px;'>" + |
23 | "<div id='graph'></div>" + | |
bf06e350 RK |
24 | "<div style='height:100px; background-color:green;'>" + LOREM_IPSUM + " </div>" + |
25 | "<div style='height:100px; background-color:red;'>" + LOREM_IPSUM + "</div>" + | |
bf06e350 RK |
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 | ||
319d0361 DV |
40 | point = null; |
41 | g = new Dygraph(graph, data, | |
bf06e350 RK |
42 | { |
43 | labels : ['a', 'b'], | |
44 | drawPoints : true, | |
45 | highlightCircleSize : 6, | |
dc910fce DV |
46 | pointClickCallback : function(evt, p) { |
47 | point = p; | |
bf06e350 RK |
48 | } |
49 | } | |
50 | ); | |
51 | ||
89fdcedb | 52 | }); |
bf06e350 | 53 | |
27561e51 DV |
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 | |
319d0361 | 58 | var detectScrollbarWidth = function() { |
27561e51 DV |
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 | ||
89fdcedb DV |
77 | afterEach(function() { |
78 | }); | |
bf06e350 RK |
79 | |
80 | /** | |
81 | * This tests that when the nested div is unscrolled, things work normally. | |
82 | */ | |
89fdcedb | 83 | it('testUnscrolledDiv', function() { |
bf06e350 | 84 | |
a3077002 | 85 | document.getElementById('scroller').scrollTop = 0; |
bf06e350 RK |
86 | |
87 | var clickOn4_40 = { | |
88 | clientX: 244, | |
89 | clientY: 131, | |
90 | screenX: 416, | |
91 | screenY: 320 | |
92 | }; | |
93 | ||
319d0361 DV |
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' })); | |
bf06e350 | 97 | |
89fdcedb DV |
98 | assert.equal(40, point.xval); |
99 | assert.equal(4, point.yval); | |
100 | }); | |
bf06e350 RK |
101 | |
102 | /** | |
103 | * This tests that when the nested div is scrolled, things work normally. | |
104 | */ | |
89fdcedb | 105 | it('testScrolledDiv', function() { |
a3077002 | 106 | document.getElementById('scroller').scrollTop = 117; |
bf06e350 RK |
107 | |
108 | var clickOn4_40 = { | |
109 | clientX: 244, | |
319d0361 | 110 | clientY: 30 - detectScrollbarWidth(), |
bf06e350 RK |
111 | screenX: 416, |
112 | screenY: 160 | |
113 | }; | |
114 | ||
319d0361 DV |
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' })); | |
bf06e350 | 118 | |
89fdcedb DV |
119 | assert.equal(40, point.xval); |
120 | assert.equal(4, point.yval); | |
121 | }); | |
122 | ||
123 | }); |