Debian Codegraph @

closure-library/closure/goog/positioning/viewportclientposition_test.js

raw
1 // Copyright 2009 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 goog.provide('goog.positioning.ViewportClientPositionTest');
16 goog.setTestOnly('goog.positioning.ViewportClientPositionTest');
17
18 goog.require('goog.dom');
19 goog.require('goog.positioning.Corner');
20 goog.require('goog.positioning.Overflow');
21 goog.require('goog.positioning.ViewportClientPosition');
22 goog.require('goog.style');
23 goog.require('goog.testing.jsunit');
24 goog.require('goog.userAgent');
25
26 var viewportSize, anchor, popup, dom, frameRect;
27 var corner = goog.positioning.Corner;
28
29 // Allow positions to be off by one in gecko as it reports scrolling
30 // offsets in steps of 2.
31 var ALLOWED_OFFSET = goog.userAgent.GECKO ? 1 : 0;
32
33
34 function setUp() {
35 var frame = document.getElementById('frame1');
36 var doc = goog.dom.getFrameContentDocument(frame);
37
38 dom = goog.dom.getDomHelper(doc);
39 viewportSize = dom.getViewportSize();
40 anchor = dom.getElement('anchor');
41 popup = dom.getElement('popup');
42 popup.style.overflowY = 'visible';
43 goog.style.setSize(popup, 20, 20);
44 frameRect = goog.style.getVisibleRectForElement(doc.body);
45 }
46
47
48 function testPositionAtCoordinateTopLeft() {
49 var pos = new goog.positioning.ViewportClientPosition(100, 100);
50 pos.reposition(popup, corner.TOP_LEFT);
51
52 var offset = goog.style.getPageOffset(popup);
53 assertEquals(
54 'Left edge of popup should be at specified x coordinate.', 100, offset.x);
55 assertEquals(
56 'Top edge of popup should be at specified y coordinate.', 100, offset.y);
57 }
58
59
60 function testPositionAtCoordinateBottomRight() {
61 var pos = new goog.positioning.ViewportClientPosition(100, 100);
62 pos.reposition(popup, corner.BOTTOM_RIGHT);
63
64 var bounds = goog.style.getBounds(popup);
65 assertEquals(
66 'Right edge of popup should be at specified x coordinate.', 100,
67 bounds.left + bounds.width);
68 assertEquals(
69 'Bottom edge of popup should be at specified x coordinate.', 100,
70 bounds.top + bounds.height);
71 }
72
73
74 function testPositionAtCoordinateTopLeftWithScroll() {
75 dom.getDocument().body.style.paddingTop = '300px';
76 dom.getDocument().body.style.height = '3000px';
77 dom.getDocumentScrollElement().scrollTop = 50;
78 dom.getDocument().body.scrollTop = 50;
79
80 var pos = new goog.positioning.ViewportClientPosition(0, 0);
81 pos.reposition(popup, corner.TOP_LEFT);
82
83 var offset = goog.style.getPageOffset(popup);
84 assertEquals(
85 'Left edge of popup should be at specified x coordinate.', 0, offset.x);
86 assertTrue(
87 'Top edge of popup should be at specified y coordinate ' +
88 'adjusted for scroll.',
89 Math.abs(offset.y - 50) <= ALLOWED_OFFSET);
90
91 dom.getDocument().body.style.paddingLeft = '1000px';
92 dom.getDocumentScrollElement().scrollLeft = 500;
93
94 pos.reposition(popup, corner.TOP_LEFT);
95 offset = goog.style.getPageOffset(popup);
96 assertTrue(
97 'Left edge of popup should be at specified x coordinate ' +
98 'adjusted for scroll.',
99 Math.abs(offset.x - 500) <= ALLOWED_OFFSET);
100
101 dom.getDocumentScrollElement().scrollLeft = 0;
102 dom.getDocumentScrollElement().scrollTop = 0;
103 dom.getDocument().body.style.paddingLeft = '';
104 dom.getDocument().body.style.paddingTop = '';
105
106 pos.reposition(popup, corner.TOP_LEFT);
107 offset = goog.style.getPageOffset(popup);
108 assertEquals(
109 'Left edge of popup should be at specified x coordinate.', 0, offset.x);
110 assertEquals(
111 'Top edge of popup should be at specified y coordinate.', 0, offset.y);
112 }
113
114
115 function testOverflowRightFlipHor() {
116 var pos = new goog.positioning.ViewportClientPosition(frameRect.right, 100);
117 pos.reposition(popup, corner.TOP_LEFT);
118
119 var offset = goog.style.getPageOffset(popup);
120 assertEquals(
121 'Left edge of popup should have been adjusted so that it ' +
122 'fits inside the viewport.',
123 frameRect.right - popup.offsetWidth, offset.x);
124 assertEquals(
125 'Top edge of popup should be at specified y coordinate.', 100, offset.y);
126 }
127
128
129 function testOverflowTopFlipVer() {
130 var pos = new goog.positioning.ViewportClientPosition(100, 0);
131 pos.reposition(popup, corner.TOP_RIGHT);
132
133 var offset = goog.style.getPageOffset(popup);
134 assertEquals(
135 'Left edge of popup should be at specified x coordinate.', 80, offset.x);
136 assertEquals(
137 'Top edge of popup should have been adjusted so that it ' +
138 'fits inside the viewport.',
139 0, offset.y);
140 }
141
142
143 function testOverflowBottomRightFlipBoth() {
144 var pos = new goog.positioning.ViewportClientPosition(
145 frameRect.right, frameRect.bottom);
146 pos.reposition(popup, corner.TOP_LEFT);
147
148 var offset = goog.style.getPageOffset(popup);
149 assertEquals(
150 'Left edge of popup should have been adjusted so that it ' +
151 'fits inside the viewport.',
152 frameRect.right - popup.offsetWidth, offset.x);
153 assertEquals(
154 'Top edge of popup should have been adjusted so that it ' +
155 'fits inside the viewport.',
156 frameRect.bottom - popup.offsetHeight, offset.y);
157 }
158
159
160 function testLastRespotOverflow() {
161 var large = 2000;
162 goog.style.setSize(popup, 20, large);
163 popup.style.overflowY = 'auto';
164
165 var pos = new goog.positioning.ViewportClientPosition(0, 0);
166 pos.reposition(popup, corner.TOP_LEFT);
167
168 assertEquals(large, popup.offsetHeight);
169 pos.setLastResortOverflow(goog.positioning.Overflow.RESIZE_HEIGHT);
170 pos.reposition(popup, corner.TOP_LEFT);
171 assertNotEquals(large, popup.offsetHeight);
172 }