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