Debian Codegraph @

closure-library/closure/goog/positioning/anchoredviewportposition_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.AnchoredViewportPositionTest');
16 goog.setTestOnly('goog.positioning.AnchoredViewportPositionTest');
17
18 goog.require('goog.dom');
19 goog.require('goog.math.Box');
20 goog.require('goog.positioning.AnchoredViewportPosition');
21 goog.require('goog.positioning.Corner');
22 goog.require('goog.positioning.OverflowStatus');
23 goog.require('goog.style');
24 goog.require('goog.testing.jsunit');
25
26 var frame, doc, dom, viewportSize, anchor, popup;
27 var corner = goog.positioning.Corner;
28
29 function setUp() {
30 frame = document.getElementById('frame1');
31 doc = goog.dom.getFrameContentDocument(frame);
32 dom = goog.dom.getDomHelper(doc);
33 viewportSize = dom.getViewportSize();
34 anchor = dom.getElement('anchor');
35 popup = dom.getElement('popup');
36 goog.style.setSize(popup, 20, 20);
37 }
38
39 // The frame has enough space at the bottom of the anchor.
40 function testRepositionBottom() {
41 var avp = new goog.positioning.AnchoredViewportPosition(
42 anchor, corner.BOTTOM_LEFT, false);
43 goog.style.setSize(anchor, 100, 100);
44 goog.style.setPosition(anchor, 0, 0);
45 assertTrue(viewportSize.height >= 100 + 20);
46
47 avp.reposition(popup, corner.TOP_LEFT);
48 var anchorRect = goog.style.getBounds(anchor);
49 assertEquals(
50 anchorRect.top + anchorRect.height, goog.style.getPageOffset(popup).y);
51 }
52
53 // No enough space at the bottom, but at the top.
54 function testRepositionTop() {
55 var avp = new goog.positioning.AnchoredViewportPosition(
56 anchor, corner.BOTTOM_LEFT, false);
57 var newTop = viewportSize.height - 100;
58 goog.style.setSize(anchor, 100, 100);
59 goog.style.setPosition(anchor, 50, newTop);
60 assertTrue(newTop >= 20);
61
62 avp.reposition(popup, corner.TOP_LEFT);
63 anchorRect = goog.style.getBounds(anchor);
64 var popupRect = goog.style.getBounds(popup);
65 assertEquals(anchorRect.top, popupRect.top + popupRect.height);
66 }
67
68 // Not enough space either at the bottom or right but there is enough space at
69 // top left.
70 function testRepositionBottomRight() {
71 var avp = new goog.positioning.AnchoredViewportPosition(
72 anchor, corner.BOTTOM_RIGHT, false);
73 goog.style.setSize(anchor, 100, 100);
74 goog.style.setPosition(
75 anchor, viewportSize.width - 110, viewportSize.height - 110);
76
77 avp.reposition(popup, corner.TOP_LEFT);
78 anchorRect = goog.style.getBounds(anchor);
79 var popupRect = goog.style.getBounds(popup);
80 assertEquals(anchorRect.top, popupRect.top + popupRect.height);
81 assertEquals(anchorRect.left, popupRect.left + popupRect.width);
82 }
83
84 // Enough space at neither the bottom nor the top. Adjustment flag is false.
85 function testRepositionNoSpaceWithoutAdjustment() {
86 var avp = new goog.positioning.AnchoredViewportPosition(
87 anchor, corner.BOTTOM_LEFT, false);
88 goog.style.setPosition(anchor, 50, 10);
89 goog.style.setSize(anchor, 100, viewportSize.height - 20);
90
91 avp.reposition(popup, corner.TOP_LEFT);
92 anchorRect = goog.style.getBounds(anchor);
93 popupRect = goog.style.getBounds(popup);
94 assertEquals(anchorRect.top + anchorRect.height, popupRect.top);
95 assertTrue(popupRect.top + popupRect.height > viewportSize.height);
96 }
97
98 // Enough space at neither the bottom nor the top. Adjustment flag is true.
99 function testRepositionNoSpaceWithAdjustment() {
100 var avp = new goog.positioning.AnchoredViewportPosition(
101 anchor, corner.BOTTOM_LEFT, true);
102 goog.style.setPosition(anchor, 50, 10);
103 goog.style.setSize(anchor, 100, viewportSize.height - 20);
104
105 avp.reposition(popup, corner.TOP_LEFT);
106 anchorRect = goog.style.getBounds(anchor);
107 popupRect = goog.style.getBounds(popup);
108 assertTrue(anchorRect.top + anchorRect.height > popupRect.top);
109 assertEquals(viewportSize.height, popupRect.top + popupRect.height);
110 }
111
112 function testAdjustCorner() {
113 var avp =
114 new goog.positioning.AnchoredViewportPosition(anchor, corner.BOTTOM_LEFT);
115 assertEquals(corner.BOTTOM_LEFT, avp.adjustCorner(0, corner.BOTTOM_LEFT));
116 assertEquals(
117 corner.BOTTOM_RIGHT,
118 avp.adjustCorner(
119 goog.positioning.OverflowStatus.FAILED_HORIZONTAL,
120 corner.BOTTOM_LEFT));
121 assertEquals(
122 corner.TOP_LEFT,
123 avp.adjustCorner(
124 goog.positioning.OverflowStatus.FAILED_VERTICAL, corner.BOTTOM_LEFT));
125 assertEquals(
126 corner.TOP_RIGHT,
127 avp.adjustCorner(
128 goog.positioning.OverflowStatus.FAILED_VERTICAL |
129 goog.positioning.OverflowStatus.FAILED_HORIZONTAL,
130 corner.BOTTOM_LEFT));
131 }
132
133 // No space to fit, so uses fallback.
134 function testOverflowConstraint() {
135 var tinyBox = new goog.math.Box(0, 0, 0, 0);
136 var avp = new goog.positioning.AnchoredViewportPosition(
137 anchor, corner.BOTTOM_LEFT, false, tinyBox);
138 assertEquals(tinyBox, avp.getOverflowConstraint());
139
140 goog.style.setSize(anchor, 50, 50);
141 goog.style.setPosition(anchor, 80, 80);
142 avp.reposition(popup, corner.TOP_LEFT);
143
144 anchorRect = goog.style.getBounds(anchor);
145 popupRect = goog.style.getBounds(popup);
146 assertEquals(anchorRect.left, popupRect.left);
147 assertEquals(anchorRect.top + anchorRect.height, popupRect.top);
148 }
149
150 // Initially no space to fit above, then changes to have room.
151 function testChangeOverflowConstraint() {
152 var tinyBox = new goog.math.Box(0, 0, 0, 0);
153 var avp = new goog.positioning.AnchoredViewportPosition(
154 anchor, corner.BOTTOM_LEFT, false, tinyBox);
155 assertEquals(tinyBox, avp.getOverflowConstraint());
156
157 goog.style.setSize(anchor, 50, 50);
158 goog.style.setPosition(anchor, 80, 80);
159
160 avp.reposition(popup, corner.TOP_LEFT);
161 popupRect = goog.style.getBounds(popup);
162 assertNotEquals(60, popupRect.top);
163
164 var movedBox = new goog.math.Box(60, 100, 100, 60);
165 avp.setOverflowConstraint(movedBox);
166 assertEquals(movedBox, avp.getOverflowConstraint());
167
168 avp.reposition(popup, corner.TOP_LEFT);
169 popupRect = goog.style.getBounds(popup);
170 assertEquals(80, popupRect.left);
171 assertEquals(60, popupRect.top);
172 }