Debian Codegraph @

closure-library/closure/goog/testing/style/layoutasserts_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.testing.style.layoutassertsTest');
16 goog.setTestOnly('goog.testing.style.layoutassertsTest');
17
18 goog.require('goog.dom');
19 goog.require('goog.dom.TagName');
20 goog.require('goog.style');
21 goog.require('goog.testing.TestCase');
22 goog.require('goog.testing.jsunit');
23 /** @suppress {extraRequire} */
24 goog.require('goog.testing.style.layoutasserts');
25
26 var div1;
27 var div2;
28 var DEFAULT_WIDTH = 200;
29 var DEFAULT_HEIGHT = 100;
30
31 function setUp() {
32 // TODO(b/25875505): Fix unreported assertions (go/failonunreportedasserts).
33 goog.testing.TestCase.getActiveTestCase().failOnUnreportedAsserts = false;
34
35 div1 = goog.dom.createDom(goog.dom.TagName.DIV, {
36 id: 'test',
37 className: 'test',
38 style: 'position:absolute;top:0;left:0;' +
39 'width:' + DEFAULT_WIDTH + 'px;' +
40 'height:' + DEFAULT_HEIGHT + 'px;' +
41 'background-color:#EEE',
42 innerHTML: 'abc'
43 });
44 div2 = goog.dom.createDom(goog.dom.TagName.DIV, {
45 id: 'test2',
46 className: 'test2',
47 style: 'position:absolute;' +
48 'top:0;left:0;' +
49 'width:' + DEFAULT_WIDTH + 'px;' +
50 'height:' + DEFAULT_HEIGHT + 'px;' +
51 'background-color:#F00',
52 innerHTML: 'abc'
53 });
54 }
55
56
57 function tearDown() {
58 div1 = null;
59 div2 = null;
60 }
61
62
63 /**
64 * Tests assertIsVisible.
65 */
66 function testAssertIsVisible() {
67 assertThrows(
68 'Exception should be thrown when asserting visibility.',
69 goog.bind(assertIsVisible, null, null)); // assertIsVisible(null)
70
71 // Attach it to BODY tag and assert that it is visible.
72 document.body.appendChild(div1);
73 assertIsVisible('Div should be visible.', div1);
74
75 // Tests with hidden element
76 failed = false;
77 goog.style.setElementShown(div1, false /* display */);
78 assertThrows(
79 'Exception should be thrown when asserting visibility.',
80 goog.bind(assertIsVisible, null, div1));
81
82 // Clean up.
83 document.body.removeChild(div1);
84 }
85
86
87 /**
88 * Tests assertNotVisible.
89 */
90 function testAssertNotVisible() {
91 // Tests null as a parameter.
92 var element = null;
93 assertNotVisible(element);
94
95 // Attach the element to BODY element, assert should fail.
96 document.body.appendChild(div1);
97 assertThrows(
98 'Exception should be thrown when asserting non-visibility.',
99 goog.bind(assertNotVisible, null, div1));
100
101 // Clean up.
102 document.body.removeChild(div1);
103 }
104
105
106 /**
107 * Tests assertIsIntersect.
108 */
109 function testAssertIntersect() {
110 document.body.appendChild(div1);
111 document.body.appendChild(div2);
112
113 // No intersection
114 goog.style.setPosition(div1, 0, 0);
115 goog.style.setPosition(div2, 500, 500);
116 assertThrows(
117 'Exception should be thrown when asserting intersection.',
118 goog.bind(assertIntersect, null, div1, div2));
119 assertNoIntersect(div1, div2);
120
121 // Some intersection
122 goog.style.setPosition(div1, 0, 0);
123 goog.style.setPosition(div2, 50, 50);
124 assertThrows(
125 'Exception should be thrown when asserting no intersection.',
126 goog.bind(assertNoIntersect, null, div1, div2));
127 assertIntersect(div1, div2);
128
129 // Completely superimposed.
130 goog.style.setPosition(div1, 0, 0);
131 goog.style.setPosition(div2, 0, 0);
132 assertThrows(
133 'Exception should be thrown when asserting no intersection.',
134 goog.bind(assertNoIntersect, null, div1, div2));
135 assertIntersect(div1, div2);
136 }
137
138
139 /**
140 * Tests assertWidth.
141 */
142 function testAssertWidth() {
143 document.body.appendChild(div1);
144
145 // Test correct width
146 assertWidth(div1, DEFAULT_WIDTH);
147
148 // Test wrong width
149 assertThrows(
150 'Exception should be thrown when elements has wrong width',
151 goog.bind(assertWidth, null, div1, 400));
152
153 // Test a valid tolerance value
154 assertWidthWithinTolerance(div1, 180, 20);
155
156 // Test exceeding tolerance value
157 assertThrows(
158 'Exception should be thrown when element\'s width exceeds tolerance',
159 goog.bind(assertWidthWithinTolerance, null, div1, 100, 0.1));
160 }
161
162
163 /**
164 * Tests assertHeight.
165 */
166 function testAssertHeight() {
167 document.body.appendChild(div1);
168
169 // Test correct height
170 assertHeight(div1, DEFAULT_HEIGHT);
171
172 // Test wrong height
173 assertThrows(
174 'Exception should be thrown when element has wrong height.',
175 goog.bind(assertHeightWithinTolerance, null, div1, 300));
176
177 // Test a valid tolerance value
178 assertHeightWithinTolerance(div1, 90, 10);
179
180 // Test exceeding tolerance value
181 assertThrows(
182 'Exception should be thrown when element\'s height exceeds tolerance',
183 goog.bind(assertHeight, null, div1, 50, 0.2));
184 }
185
186
187 /**
188 * Tests assertIsLeftOf.
189 */
190 function testAssertIsLeftOf() {
191 document.body.appendChild(div1);
192 document.body.appendChild(div2);
193
194 // Test elements of same size & location
195 assertThrows(
196 'Exception should be thrown when elements intersect.',
197 goog.bind(assertIsLeftOf, null, div1, div2));
198 assertThrows(
199 'Exception should be thrown when elements intersect.',
200 goog.bind(assertIsStrictlyLeftOf, null, div1, div2));
201
202 // Test elements that are not left to right
203 goog.style.setPosition(div1, 100, 0);
204 goog.style.setPosition(div2, 0, 0);
205 assertThrows(
206 'Exception should be thrown when elements are not left to right.',
207 goog.bind(assertIsLeftOf, null, div1, div2));
208 assertThrows(
209 'Exception should be thrown when elements are not left to right.',
210 goog.bind(assertIsStrictlyLeftOf, null, div1, div2));
211
212 // Test elements that intersect, but is left to right
213 goog.style.setPosition(div1, 0, 0);
214 goog.style.setPosition(div2, 100, 0);
215 assertIsLeftOf(div1, div2);
216 assertThrows(
217 'Exception should be thrown when elements intersect.',
218 goog.bind(assertIsStrictlyLeftOf, null, div1, div2));
219
220 // Test elements that are strictly left to right
221 goog.style.setPosition(div1, 0, 0);
222 goog.style.setPosition(div2, 999, 0);
223 assertIsLeftOf(div1, div2);
224 assertIsStrictlyLeftOf(div1, div2);
225 }
226
227
228 /**
229 * Tests assertIsAbove.
230 */
231 function testAssertIsAbove() {
232 document.body.appendChild(div1);
233 document.body.appendChild(div2);
234
235 // Test elements of same size & location
236 assertThrows(
237 'Exception should be thrown when elements intersect.',
238 goog.bind(assertIsAbove, null, div1, div2));
239 assertThrows(
240 'Exception should be thrown when elements intersect.',
241 goog.bind(assertIsStrictlyAbove, null, div1, div2));
242
243 // Test elements that are not top to bottom
244 goog.style.setPosition(div1, 0, 999);
245 goog.style.setPosition(div2, 0, 0);
246 assertThrows(
247 'Exception should be thrown when elements are not top to bottom.',
248 goog.bind(assertIsAbove, null, div1, div2));
249 assertThrows(
250 'Exception should be thrown when elements are not top to bottom.',
251 goog.bind(assertIsStrictlyAbove, null, div1, div2));
252
253 // Test elements that intersect, but is top to bottom
254 goog.style.setPosition(div1, 0, 0);
255 goog.style.setPosition(div2, 0, 50);
256 assertIsAbove(div1, div2);
257 assertThrows(
258 'Exception should be thrown when elements intersect.',
259 goog.bind(assertIsStrictlyAbove, null, div1, div2));
260
261 // Test elements that are top to bottom
262 goog.style.setPosition(div1, 0, 0);
263 goog.style.setPosition(div2, 0, 999);
264 assertIsAbove(div1, div2);
265 assertIsStrictlyAbove(div1, div2);
266 }