Commit | Line | Data |
---|---|---|
24f7710b RK |
1 | /** |
2 | * @fileoverview Test cases for resizing. | |
3 | * | |
4 | * @author konigsberg@google.com (Robert Konigsberg) | |
5 | */ | |
89fdcedb | 6 | describe("resize", function() { |
24f7710b | 7 | |
319d0361 | 8 | var data = |
d82a3164 KW |
9 | "X,Y\n" + |
10 | "1,100\n" + | |
11 | "2,200\n" + | |
12 | "3,300\n" + | |
13 | "4,400\n" + | |
14 | "5,300\n" + | |
15 | "6,100\n"; | |
c28088bc | 16 | |
89fdcedb | 17 | beforeEach(function() { |
24f7710b | 18 | document.body.innerHTML = "<div id='graph'></div>"; |
89fdcedb | 19 | }); |
24f7710b | 20 | |
89fdcedb DV |
21 | afterEach(function() { |
22 | }); | |
24f7710b | 23 | |
89fdcedb | 24 | it('testResizeMaintainsMouseOperations', function() { |
24f7710b RK |
25 | document.body.innerHTML = |
26 | '<div id="graph" style="width: 640px; height: 480px;"></div>' + | |
27 | '</div>'; | |
28 | var graph = document.getElementById("graph"); | |
29 | ||
30 | var callbackCount = 0; | |
31 | var callback = function() { | |
32 | callbackCount++; | |
33 | } | |
34 | ||
35 | // Strum the mouse along the y-coordinate y, from 0 to x2. These are DOM values. | |
36 | var strum = function(g, y, x2) { | |
37 | DygraphOps.dispatchMouseDown_Point(g, 0, y); | |
38 | for (var x = 0; x < x2; x++) { | |
39 | DygraphOps.dispatchMouseMove_Point(g, x, y); | |
40 | } | |
41 | DygraphOps.dispatchMouseUp_Point(g, x2 - 1, y); | |
42 | } | |
43 | ||
89fdcedb | 44 | var g = new Dygraph(graph, data, {highlightCallback: callback}); |
24f7710b RK |
45 | |
46 | strum(g, 300, 640); | |
89fdcedb | 47 | assert.equal(6, callbackCount); |
24f7710b RK |
48 | |
49 | document.getElementById("graph").style.width = "500px"; | |
50 | g.resize(); | |
51 | ||
52 | callbackCount = 0; | |
53 | strum(g, 300, 500); | |
89fdcedb DV |
54 | assert.equal(6, callbackCount); |
55 | }); | |
c28088bc KW |
56 | |
57 | /** | |
58 | * Tests that a graph created in a not-displayed div works as expected | |
59 | * if the graph options include height and width. Resize not needed. | |
60 | */ | |
89fdcedb | 61 | it('testHiddenDivWithSizedGraph', function() { |
c28088bc KW |
62 | var div = document.getElementById("graph"); |
63 | ||
64 | div.style.display = 'none'; | |
319d0361 | 65 | var g = new Dygraph(div, data, {width: 400, height: 300}); |
c28088bc KW |
66 | div.style.display = ''; |
67 | ||
68 | var area = g.getArea(); | |
89fdcedb DV |
69 | assert.isTrue(area.w > 0); |
70 | assert.isTrue(area.h > 0); | |
71 | }); | |
c28088bc KW |
72 | |
73 | /** | |
74 | * Tests that a graph created in a not-displayed div with | |
75 | * CSS-specified size but no graph height or width options works as | |
76 | * expected. The user needs to call resize() on it after displaying | |
77 | * it. | |
78 | */ | |
89fdcedb | 79 | it('testHiddenDivWithResize', function() { |
c28088bc KW |
80 | var div = document.getElementById("graph"); |
81 | ||
82 | div.style.display = 'none'; | |
83 | div.style.width = '400px'; | |
84 | div.style.height = '300px'; | |
85 | ||
18117d89 RK |
86 | // Setting strokeWidth 3 removes any ambiguitiy from the pixel sampling |
87 | // request, below. | |
319d0361 | 88 | var g = new Dygraph(div, data, {strokeWidth: 3}); |
c28088bc KW |
89 | div.style.display = ''; |
90 | ||
91 | g.resize(); | |
89fdcedb DV |
92 | var area = g.getArea(); |
93 | assert.isTrue(area.w > 0); | |
94 | assert.isTrue(area.h > 0); | |
d82a3164 KW |
95 | |
96 | // Regression test: check that graph remains visible after no-op resize. | |
97 | g.resize(); | |
98 | var x = Math.floor(g.toDomXCoord(2)); | |
99 | var y = Math.floor(g.toDomYCoord(200)); | |
dc910fce DV |
100 | assert.deepEqual([0, 128, 128, 255], Util.samplePixel(g.hidden_, x, y), |
101 | "Unexpected grid color found at pixel: x: " + x + " y: " + y); | |
89fdcedb DV |
102 | }); |
103 | ||
104 | }); |