Debian Codegraph @

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

raw
1 // Copyright 2008 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 /**
16 * @fileoverview Unit tests for goog.position.
17 *
18 * @author [email protected] (Emil A Eklund)
19 */
20
21 /** @suppress {extraProvide} */
22 goog.provide('goog.positioningTest');
23
24 goog.require('goog.dom');
25 goog.require('goog.dom.DomHelper');
26 goog.require('goog.dom.TagName');
27 goog.require('goog.labs.userAgent.browser');
28 goog.require('goog.math.Box');
29 goog.require('goog.math.Coordinate');
30 goog.require('goog.math.Size');
31 goog.require('goog.positioning');
32 goog.require('goog.positioning.Corner');
33 goog.require('goog.positioning.Overflow');
34 goog.require('goog.positioning.OverflowStatus');
35 goog.require('goog.style');
36 goog.require('goog.testing.ExpectedFailures');
37 goog.require('goog.testing.jsunit');
38 goog.require('goog.userAgent');
39 goog.require('goog.userAgent.product');
40
41 goog.setTestOnly('goog.positioningTest');
42
43 // Allow positions to be off by one in gecko as it reports scrolling
44 // offsets in steps of 2. Otherwise, allow for subpixel difference
45 // as seen in IE10+
46 var ALLOWED_OFFSET = goog.userAgent.GECKO ? 1 : 0.1;
47 // Error bar for positions since some browsers are not super accurate
48 // in reporting them.
49 var EPSILON = 2;
50
51 var expectedFailures;
52
53 var corner = goog.positioning.Corner;
54 var overflow = goog.positioning.Overflow;
55 var testArea;
56
57 function setUpPage() {
58 expectedFailures = new goog.testing.ExpectedFailures();
59 }
60
61 function setUp() {
62 window.scrollTo(0, 0);
63
64 var viewportSize = goog.dom.getViewportSize();
65 // Some tests need enough size viewport.
66 if (viewportSize.width < 600 || viewportSize.height < 600) {
67 window.moveTo(0, 0);
68 window.resizeTo(640, 640);
69 }
70
71 testArea = goog.dom.getElement('test-area');
72 }
73
74 function tearDown() {
75 expectedFailures.handleTearDown();
76 testArea.setAttribute('style', '');
77 goog.dom.removeChildren(testArea);
78 }
79
80
81 /**
82 * This is used to round pixel values on FF3 Mac.
83 */
84 function assertRoundedEquals(a, b, c) {
85 function round(x) {
86 return goog.userAgent.GECKO && (goog.userAgent.MAC || goog.userAgent.X11) &&
87 goog.userAgent.isVersionOrHigher('1.9') ?
88 Math.round(x) :
89 x;
90 }
91 if (arguments.length == 3) {
92 assertRoughlyEquals(a, round(b), round(c), ALLOWED_OFFSET);
93 } else {
94 assertRoughlyEquals(round(a), round(b), ALLOWED_OFFSET);
95 }
96 }
97
98 function testPositionAtAnchorLeftToRight() {
99 var anchor = document.getElementById('anchor1');
100 var popup = document.getElementById('popup1');
101
102 // Anchor top left to top left.
103 goog.positioning.positionAtAnchor(
104 anchor, corner.TOP_LEFT, popup, corner.TOP_LEFT);
105 var anchorRect = goog.style.getBounds(anchor);
106 var popupRect = goog.style.getBounds(popup);
107 assertRoundedEquals(
108 'Left edge of popup should line up with left edge ' +
109 'of anchor.',
110 anchorRect.left, popupRect.left);
111 assertRoundedEquals(
112 'Popup should have the same y position as the anchor.', anchorRect.top,
113 popupRect.top);
114
115 // Anchor top left to bottom left.
116 goog.positioning.positionAtAnchor(
117 anchor, corner.BOTTOM_LEFT, popup, corner.TOP_LEFT);
118 anchorRect = goog.style.getBounds(anchor);
119 popupRect = goog.style.getBounds(popup);
120 assertRoundedEquals(
121 'Left edge of popup should line up with left edge ' +
122 'of anchor.',
123 anchorRect.left, popupRect.left);
124 assertRoundedEquals(
125 'Popup should be positioned just below the anchor.',
126 anchorRect.top + anchorRect.height, popupRect.top);
127
128 // Anchor top left to top right.
129 goog.positioning.positionAtAnchor(
130 anchor, corner.TOP_RIGHT, popup, corner.TOP_LEFT);
131 anchorRect = goog.style.getBounds(anchor);
132 popupRect = goog.style.getBounds(popup);
133 assertRoundedEquals(
134 'Popup should be positioned just right of the anchor.',
135 anchorRect.left + anchorRect.width, popupRect.left);
136 assertRoundedEquals(
137 'Popup should have the same y position as the anchor.', anchorRect.top,
138 popupRect.top);
139
140 // Anchor top right to bottom right.
141 goog.positioning.positionAtAnchor(
142 anchor, corner.BOTTOM_RIGHT, popup, corner.TOP_RIGHT);
143 anchorRect = goog.style.getBounds(anchor);
144 popupRect = goog.style.getBounds(popup);
145 assertRoundedEquals(
146 'Right edge of popup should line up with right edge ' +
147 'of anchor.',
148 anchorRect.left + anchorRect.width, popupRect.left + popupRect.width);
149 assertRoundedEquals(
150 'Popup should be positioned just below the anchor.',
151 anchorRect.top + anchorRect.height, popupRect.top);
152
153 // Anchor top start to bottom start.
154 goog.positioning.positionAtAnchor(
155 anchor, corner.BOTTOM_START, popup, corner.TOP_START);
156 anchorRect = goog.style.getBounds(anchor);
157 popupRect = goog.style.getBounds(popup);
158 assertRoundedEquals(
159 'Left edge of popup should line up with left edge ' +
160 'of anchor.',
161 anchorRect.left, popupRect.left);
162 assertRoundedEquals(
163 'Popup should be positioned just below the anchor.',
164 anchorRect.top + anchorRect.height, popupRect.top);
165
166 // Anchor top center to top center.
167 goog.positioning.positionAtAnchor(
168 anchor, corner.TOP_CENTER, popup, corner.TOP_CENTER);
169 anchorRect = goog.style.getBounds(anchor);
170 popupRect = goog.style.getBounds(popup);
171 // We use flooring math because we consider 10 and 10.5 to be "equal".
172 // IE8 doesn't allow split pixels in positioning and as such this test would
173 // fail on it, as Anchor L+W/2 is round while Popup L+W/2 is .5 away.
174 assertRoundedEquals(
175 'The center of popup should line up with the center ' +
176 'of anchor.',
177 Math.floor(anchorRect.left + anchorRect.width / 2),
178 Math.floor(popupRect.left + popupRect.width / 2));
179 assertRoundedEquals(
180 'Popup should have the same y position as the anchor.', anchorRect.top,
181 popupRect.top);
182
183 // Anchor top center to top left.
184 goog.positioning.positionAtAnchor(
185 anchor, corner.TOP_CENTER, popup, corner.TOP_LEFT);
186 anchorRect = goog.style.getBounds(anchor);
187 popupRect = goog.style.getBounds(popup);
188 assertRoundedEquals(
189 'Left edge of popup should line up with the center ' +
190 'of anchor.',
191 Math.floor(anchorRect.left + anchorRect.width / 2),
192 Math.floor(popupRect.left));
193 assertRoundedEquals(
194 'Popup should have the same y position as the anchor.', anchorRect.top,
195 popupRect.top);
196
197 // Anchor bottom center to top left.
198 goog.positioning.positionAtAnchor(
199 anchor, corner.BOTTOM_CENTER, popup, corner.TOP_LEFT);
200 anchorRect = goog.style.getBounds(anchor);
201 popupRect = goog.style.getBounds(popup);
202 assertRoundedEquals(
203 'Left edge of popup should line up with the middle ' +
204 'of anchor.',
205 Math.floor(anchorRect.left + anchorRect.width / 2),
206 Math.floor(popupRect.left));
207 assertRoundedEquals(
208 'Popup should be positioned just below the anchor.',
209 anchorRect.top + anchorRect.height, popupRect.top);
210
211 // Anchor bottom left to top center.
212 goog.positioning.positionAtAnchor(
213 anchor, corner.BOTTOM_LEFT, popup, corner.TOP_CENTER);
214 anchorRect = goog.style.getBounds(anchor);
215 popupRect = goog.style.getBounds(popup);
216 assertRoundedEquals(
217 'Left edge of popup should line up with the middle ' +
218 'of anchor.',
219 anchorRect.left, Math.floor(popupRect.left + popupRect.width / 2));
220 assertRoundedEquals(
221 'Popup should be positioned just below the anchor.',
222 anchorRect.top + anchorRect.height, popupRect.top);
223 }
224
225
226 function testPositionAtAnchorWithOffset() {
227 var anchor = document.getElementById('anchor1');
228 var popup = document.getElementById('popup1');
229
230 // Anchor top left to top left with an offset moving the popup away from the
231 // anchor.
232 goog.positioning.positionAtAnchor(
233 anchor, corner.TOP_LEFT, popup, corner.TOP_LEFT, newCoord(-15, -20));
234 var anchorRect = goog.style.getBounds(anchor);
235 var popupRect = goog.style.getBounds(popup);
236 assertRoundedEquals(
237 'Left edge of popup should be fifteen pixels from ' +
238 'anchor.',
239 anchorRect.left, popupRect.left + 15);
240 assertRoundedEquals(
241 'Top edge of popup should be twenty pixels from anchor.', anchorRect.top,
242 popupRect.top + 20);
243
244 // Anchor top left to top left with an offset moving the popup towards the
245 // anchor.
246 goog.positioning.positionAtAnchor(
247 anchor, corner.TOP_LEFT, popup, corner.TOP_LEFT, newCoord(3, 1));
248 anchorRect = goog.style.getBounds(anchor);
249 popupRect = goog.style.getBounds(popup);
250 assertRoundedEquals(
251 'Left edge of popup should be three pixels right of ' +
252 'the anchor\'s left edge',
253 anchorRect.left, popupRect.left - 3);
254 assertRoundedEquals(
255 'Top edge of popup should be one pixel below of the ' +
256 'anchor\'s top edge',
257 anchorRect.top, popupRect.top - 1);
258 }
259
260
261 function testPositionAtAnchorOverflowLeftEdgeRightToLeft() {
262 var anchor = document.getElementById('anchor5');
263 var popup = document.getElementById('popup5');
264
265 var status = goog.positioning.positionAtAnchor(
266 anchor, corner.TOP_LEFT, popup, corner.TOP_RIGHT, undefined, undefined,
267 overflow.FAIL_X);
268 assertFalse(
269 'Positioning operation should have failed.',
270 (status & goog.positioning.OverflowStatus.FAILED) == 0);
271
272 // Change overflow strategy to ADJUST.
273 status = goog.positioning.positionAtAnchor(
274 anchor, corner.TOP_LEFT, popup, corner.TOP_RIGHT, undefined, undefined,
275 overflow.ADJUST_X);
276
277 // Fails in Chrome because of infrastructure issues, temporarily disabled.
278 // See b/4274723.
279 expectedFailures.expectFailureFor(goog.userAgent.product.CHROME);
280 try {
281 assertTrue(
282 'Positioning operation should have been successful.',
283 (status & goog.positioning.OverflowStatus.FAILED) == 0);
284 assertTrue(
285 'Positioning operation should have been adjusted.',
286 (status & goog.positioning.OverflowStatus.ADJUSTED_X) != 0);
287 } catch (e) {
288 expectedFailures.handleException(e);
289 }
290
291 var anchorRect = goog.style.getBounds(anchor);
292 var popupRect = goog.style.getBounds(popup);
293 var parentRect = goog.style.getBounds(anchor.parentNode);
294 assertTrue(
295 'Position should have been adjusted so that the left edge of ' +
296 'the popup is left of the anchor but still within the bounding ' +
297 'box of the parent container.',
298 anchorRect.left <= popupRect.left <= parentRect.left);
299 }
300
301
302 function testPositionAtAnchorWithMargin() {
303 var anchor = document.getElementById('anchor1');
304 var popup = document.getElementById('popup1');
305
306 // Anchor top left to top left.
307 goog.positioning.positionAtAnchor(
308 anchor, corner.TOP_LEFT, popup, corner.TOP_LEFT, undefined,
309 new goog.math.Box(1, 2, 3, 4));
310 var anchorRect = goog.style.getBounds(anchor);
311 var popupRect = goog.style.getBounds(popup);
312 assertRoundedEquals(
313 'Left edge of popup should be four pixels from anchor.', anchorRect.left,
314 popupRect.left - 4);
315 assertRoundedEquals(
316 'Top edge of popup should be one pixels from anchor.', anchorRect.top,
317 popupRect.top - 1);
318
319 // Anchor top right to bottom right.
320 goog.positioning.positionAtAnchor(
321 anchor, corner.BOTTOM_RIGHT, popup, corner.TOP_RIGHT, undefined,
322 new goog.math.Box(1, 2, 3, 4));
323 anchorRect = goog.style.getBounds(anchor);
324 popupRect = goog.style.getBounds(popup);
325
326 var visibleAnchorRect = goog.positioning.getVisiblePart_(anchor);
327
328 assertRoundedEquals(
329 'Right edge of popup should line up with right edge ' +
330 'of anchor.',
331 visibleAnchorRect.left + visibleAnchorRect.width,
332 popupRect.left + popupRect.width + 2);
333
334 assertRoundedEquals(
335 'Popup should be positioned just below the anchor.',
336 visibleAnchorRect.top + visibleAnchorRect.height, popupRect.top - 1);
337 }
338
339
340 function testPositionAtAnchorRightToLeft() {
341 if (goog.userAgent.IE && goog.userAgent.isVersionOrHigher('6')) {
342 // These tests fails with IE6.
343 // TODO(user): Investigate the reason.
344 return;
345 }
346 if (goog.userAgent.product.SAFARI) {
347 // TODO(b/20733468): Disabled so we can get the rest of the Closure test
348 // suite running in a continuous build. Will investigate later.
349 return;
350 }
351
352 var anchor = document.getElementById('anchor2');
353 var popup = document.getElementById('popup2');
354
355 // Anchor top left to top left.
356 goog.positioning.positionAtAnchor(
357 anchor, corner.TOP_LEFT, popup, corner.TOP_LEFT);
358 var anchorRect = goog.style.getBounds(anchor);
359 var popupRect = goog.style.getBounds(popup);
360
361 assertRoundedEquals(
362 'Left edge of popup should line up with left edge ' +
363 'of anchor.',
364 anchorRect.left, popupRect.left);
365 assertRoundedEquals(
366 'Popup should have the same y position as the anchor.', anchorRect.top,
367 popupRect.top);
368
369 // Anchor top start to bottom start.
370 goog.positioning.positionAtAnchor(
371 anchor, corner.BOTTOM_START, popup, corner.TOP_START);
372 anchorRect = goog.style.getBounds(anchor);
373 popupRect = goog.style.getBounds(popup);
374 assertRoundedEquals(
375 'Right edge of popup should line up with right edge ' +
376 'of anchor.',
377 anchorRect.left + anchorRect.width, popupRect.left + popupRect.width);
378 assertRoundedEquals(
379 'Popup should be positioned just below the anchor.',
380 anchorRect.top + anchorRect.height, popupRect.top);
381 }
382
383 function testPositionAtAnchorRightToLeftWithScroll() {
384 if (goog.userAgent.IE && goog.userAgent.isVersionOrHigher('6')) {
385 // These tests fails with IE6.
386 // TODO(user): Investigate the reason.
387 return;
388 }
389 if (goog.userAgent.product.SAFARI) {
390 // TODO(b/20733468): Disabled so we can get the rest of the Closure test
391 // suite running in a continuous build. Will investigate later.
392 return;
393 }
394
395 var anchor = document.getElementById('anchor8');
396 var popup = document.getElementById('popup8');
397
398 // Anchor top left to top left.
399 goog.positioning.positionAtAnchor(
400 anchor, corner.TOP_LEFT, popup, corner.TOP_LEFT);
401 var anchorRect = goog.style.getBounds(anchor);
402 var popupRect = goog.style.getBounds(popup);
403
404 // TODO(joeltine): Chrome 47 has issues with RTL scroll positioning. Remove
405 // chrome check when
406 // https://code.google.com/p/chromium/issues/detail?id=568706 is resolved.
407 if (!goog.labs.userAgent.browser.isChrome()) {
408 assertRoundedEquals(
409 'Left edge of popup should line up with left edge ' +
410 'of anchor.',
411 anchorRect.left, popupRect.left);
412 }
413 assertRoundedEquals(
414 'Popup should have the same y position as the anchor.', anchorRect.top,
415 popupRect.top);
416
417 // Anchor top start to bottom start.
418 goog.positioning.positionAtAnchor(
419 anchor, corner.BOTTOM_START, popup, corner.TOP_START);
420 anchorRect = goog.style.getBounds(anchor);
421 popupRect = goog.style.getBounds(popup);
422
423 var visibleAnchorRect = goog.positioning.getVisiblePart_(anchor);
424 var visibleAnchorBox = visibleAnchorRect.toBox();
425
426 // TODO(joeltine): Chrome 47 has issues with RTL scroll positioning. Remove
427 // chrome check when
428 // https://code.google.com/p/chromium/issues/detail?id=568706 is resolved.
429 if (!goog.labs.userAgent.browser.isChrome()) {
430 assertRoundedEquals(
431 'Right edge of popup should line up with right edge ' +
432 'of anchor.',
433 anchorRect.left + anchorRect.width, popupRect.left + popupRect.width);
434 }
435 assertRoundedEquals(
436 'Popup should be positioned just below the anchor.',
437 visibleAnchorBox.bottom, popupRect.top);
438 }
439
440 function testPositionAtAnchorBodyViewport() {
441 var anchor = document.getElementById('anchor1');
442 var popup = document.getElementById('popup3');
443
444 // Anchor top left to top left.
445 goog.positioning.positionAtAnchor(
446 anchor, corner.TOP_LEFT, popup, corner.TOP_LEFT);
447 var anchorRect = goog.style.getBounds(anchor);
448 var popupRect = goog.style.getBounds(popup);
449 assertEquals(
450 'Left edge of popup should line up with left edge of anchor.',
451 anchorRect.left, popupRect.left);
452 assertRoughlyEquals(
453 'Popup should have the same y position as the anchor.', anchorRect.top,
454 popupRect.top, 1);
455
456 // Anchor top start to bottom right.
457 goog.positioning.positionAtAnchor(
458 anchor, corner.BOTTOM_RIGHT, popup, corner.TOP_RIGHT);
459 anchorRect = goog.style.getBounds(anchor);
460 popupRect = goog.style.getBounds(popup);
461 assertEquals(
462 'Right edge of popup should line up with right edge of anchor.',
463 anchorRect.left + anchorRect.width, popupRect.left + popupRect.width);
464 assertRoughlyEquals(
465 'Popup should be positioned just below the anchor.',
466 anchorRect.top + anchorRect.height, popupRect.top, 1);
467
468 // Anchor top right to top left.
469 goog.positioning.positionAtAnchor(
470 anchor, corner.TOP_LEFT, popup, corner.TOP_RIGHT);
471 anchorRect = goog.style.getBounds(anchor);
472 popupRect = goog.style.getBounds(popup);
473 assertEquals(
474 'Right edge of popup should line up with left edge of anchor.',
475 anchorRect.left, popupRect.left + popupRect.width);
476 assertRoughlyEquals(
477 'Popup should have the same y position as the anchor.', anchorRect.top,
478 popupRect.top, 1);
479 }
480
481 function testPositionAtAnchorSpecificViewport() {
482 var anchor = document.getElementById('anchor1');
483 var popup = document.getElementById('popup3');
484
485 // Anchor top right to top left within outerbox.
486 var status = goog.positioning.positionAtAnchor(
487 anchor, corner.TOP_LEFT, popup, corner.TOP_RIGHT, undefined, undefined,
488 overflow.FAIL_X);
489 anchorRect = goog.style.getBounds(anchor);
490 popupRect = goog.style.getBounds(popup);
491 assertTrue(
492 'Positioning operation should have been successful.',
493 (status & goog.positioning.OverflowStatus.FAILED) == 0);
494 assertTrue(
495 'X position should not have been adjusted.',
496 (status & goog.positioning.OverflowStatus.ADJUSTED_X) == 0);
497 assertTrue(
498 'Y position should not have been adjusted.',
499 (status & goog.positioning.OverflowStatus.ADJUSTED_Y) == 0);
500 assertEquals(
501 'Right edge of popup should line up with left edge of anchor.',
502 anchorRect.left, popupRect.left + popupRect.width);
503 assertRoughlyEquals(
504 'Popup should have the same y position as the anchor.', anchorRect.top,
505 popupRect.top, 1);
506
507 // position again within box1.
508 var box = document.getElementById('box1');
509 var viewport = goog.style.getBounds(box);
510 status = goog.positioning.positionAtAnchor(
511 anchor, corner.TOP_LEFT, popup, corner.TOP_RIGHT, undefined, undefined,
512 overflow.FAIL_X, undefined, viewport);
513 assertFalse(
514 'Positioning operation should have failed.',
515 (status & goog.positioning.OverflowStatus.FAILED) == 0);
516
517 // Change overflow strategy to adjust.
518 status = goog.positioning.positionAtAnchor(
519 anchor, corner.TOP_LEFT, popup, corner.TOP_RIGHT, undefined, undefined,
520 overflow.ADJUST_X, undefined, viewport);
521 anchorRect = goog.style.getBounds(anchor);
522 popupRect = goog.style.getBounds(popup);
523 assertTrue(
524 'Positioning operation should have been successful.',
525 (status & goog.positioning.OverflowStatus.FAILED) == 0);
526 assertFalse(
527 'X position should have been adjusted.',
528 (status & goog.positioning.OverflowStatus.ADJUSTED_X) == 0);
529 assertTrue(
530 'Y position should not have been adjusted.',
531 (status & goog.positioning.OverflowStatus.ADJUSTED_Y) == 0);
532 assertRoughlyEquals(
533 'Left edge of popup should line up with left edge of viewport.',
534 viewport.left, popupRect.left, EPSILON);
535 assertRoughlyEquals(
536 'Popup should have the same y position as the anchor.', anchorRect.top,
537 popupRect.top, 1);
538 }
539
540 function testPositionAtAnchorOutsideViewport() {
541 var anchor = document.getElementById('anchor4');
542 var popup = document.getElementById('popup1');
543
544 var status = goog.positioning.positionAtAnchor(
545 anchor, corner.TOP_LEFT, popup, corner.TOP_RIGHT);
546 var anchorRect = goog.style.getBounds(anchor);
547 var popupRect = goog.style.getBounds(popup);
548 assertTrue(
549 'Positioning operation should have been successful.',
550 (status & goog.positioning.OverflowStatus.FAILED) == 0);
551 assertTrue(
552 'X position should not have been adjusted.',
553 (status & goog.positioning.OverflowStatus.ADJUSTED_X) == 0);
554 assertTrue(
555 'Y position should not have been adjusted.',
556 (status & goog.positioning.OverflowStatus.ADJUSTED_Y) == 0);
557
558 assertEquals(
559 'Right edge of popup should line up with left edge of anchor.',
560 anchorRect.left, popupRect.left + popupRect.width);
561
562 // Change overflow strategy to fail.
563 status = goog.positioning.positionAtAnchor(
564 anchor, corner.BOTTOM_RIGHT, popup, corner.TOP_RIGHT, undefined,
565 undefined, overflow.FAIL_X);
566 assertFalse(
567 'Positioning operation should have failed.',
568 (status & goog.positioning.OverflowStatus.FAILED) == 0);
569
570 // Change overflow strategy to adjust.
571 status = goog.positioning.positionAtAnchor(
572 anchor, corner.BOTTOM_RIGHT, popup, corner.TOP_RIGHT, undefined,
573 undefined, overflow.ADJUST_X);
574 anchorRect = goog.style.getBounds(anchor);
575 popupRect = goog.style.getBounds(popup);
576 assertTrue(
577 'Positioning operation should have been successful.',
578 (status & goog.positioning.OverflowStatus.FAILED) == 0);
579 assertFalse(
580 'X position should have been adjusted.',
581 (status & goog.positioning.OverflowStatus.ADJUSTED_X) == 0);
582 assertTrue(
583 'Y position should not have been adjusted.',
584 (status & goog.positioning.OverflowStatus.ADJUSTED_Y) == 0);
585 assertRoughlyEquals(
586 'Left edge of popup should line up with left edge of viewport.', 0,
587 popupRect.left, EPSILON);
588 assertEquals(
589 'Popup should be positioned just below the anchor.',
590 anchorRect.top + anchorRect.height, popupRect.top);
591 }
592
593
594 function testAdjustForViewportFailIgnore() {
595 var f = goog.positioning.adjustForViewport_;
596 var viewport = new goog.math.Box(100, 200, 200, 100);
597 var overflow = goog.positioning.Overflow.IGNORE;
598
599 var pos = newCoord(150, 150);
600 var size = newSize(50, 50);
601 assertEquals(
602 'Viewport overflow should be ignored.',
603 goog.positioning.OverflowStatus.NONE, f(pos, size, viewport, overflow));
604
605 pos = newCoord(150, 150);
606 size = newSize(100, 50);
607 assertEquals(
608 'Viewport overflow should be ignored.',
609 goog.positioning.OverflowStatus.NONE, f(pos, size, viewport, overflow));
610
611 pos = newCoord(50, 50);
612 size = newSize(50, 50);
613 assertEquals(
614 'Viewport overflow should be ignored.',
615 goog.positioning.OverflowStatus.NONE, f(pos, size, viewport, overflow));
616 }
617
618
619 function testAdjustForViewportFailXY() {
620 var f = goog.positioning.adjustForViewport_;
621 var viewport = new goog.math.Box(100, 200, 200, 100);
622 var overflow =
623 goog.positioning.Overflow.FAIL_X | goog.positioning.Overflow.FAIL_Y;
624
625 var pos = newCoord(150, 150);
626 var size = newSize(50, 50);
627 assertEquals(
628 'Element should not overflow viewport.',
629 goog.positioning.OverflowStatus.NONE, f(pos, size, viewport, overflow));
630
631 pos = newCoord(150, 150);
632 size = newSize(100, 50);
633 assertEquals(
634 'Element should overflow the right edge of viewport.',
635 goog.positioning.OverflowStatus.FAILED_RIGHT,
636 f(pos, size, viewport, overflow));
637
638 pos = newCoord(150, 150);
639 size = newSize(50, 100);
640 assertEquals(
641 'Element should overflow the bottom edge of viewport.',
642 goog.positioning.OverflowStatus.FAILED_BOTTOM,
643 f(pos, size, viewport, overflow));
644
645 pos = newCoord(50, 150);
646 size = newSize(50, 50);
647 assertEquals(
648 'Element should overflow the left edge of viewport.',
649 goog.positioning.OverflowStatus.FAILED_LEFT,
650 f(pos, size, viewport, overflow));
651
652 pos = newCoord(150, 50);
653 size = newSize(50, 50);
654 assertEquals(
655 'Element should overflow the top edge of viewport.',
656 goog.positioning.OverflowStatus.FAILED_TOP,
657 f(pos, size, viewport, overflow));
658
659 pos = newCoord(50, 50);
660 size = newSize(50, 50);
661 assertEquals(
662 'Element should overflow the left & top edges of viewport.',
663 goog.positioning.OverflowStatus.FAILED_LEFT |
664 goog.positioning.OverflowStatus.FAILED_TOP,
665 f(pos, size, viewport, overflow));
666 }
667
668
669 function testAdjustForViewportAdjustXFailY() {
670 var f = goog.positioning.adjustForViewport_;
671 var viewport = new goog.math.Box(100, 200, 200, 100);
672 var overflow =
673 goog.positioning.Overflow.ADJUST_X | goog.positioning.Overflow.FAIL_Y;
674
675 var pos = newCoord(150, 150);
676 var size = newSize(50, 50);
677 assertEquals(
678 'Element should not overflow viewport.',
679 goog.positioning.OverflowStatus.NONE, f(pos, size, viewport, overflow));
680 assertEquals('X Position should not have been changed.', 150, pos.x);
681 assertEquals('Y Position should not have been changed.', 150, pos.y);
682
683 pos = newCoord(150, 150);
684 size = newSize(100, 50);
685 assertEquals(
686 'Element position should be adjusted not to overflow right ' +
687 'edge of viewport.',
688 goog.positioning.OverflowStatus.ADJUSTED_X,
689 f(pos, size, viewport, overflow));
690 assertEquals('X Position should be adjusted to 100.', 100, pos.x);
691 assertEquals('Y Position should not have been changed.', 150, pos.y);
692
693 pos = newCoord(50, 150);
694 size = newSize(100, 50);
695 assertEquals(
696 'Element position should be adjusted not to overflow left ' +
697 'edge of viewport.',
698 goog.positioning.OverflowStatus.ADJUSTED_X,
699 f(pos, size, viewport, overflow));
700 assertEquals('X Position should be adjusted to 100.', 100, pos.x);
701 assertEquals('Y Position should not have been changed.', 150, pos.y);
702
703
704 pos = newCoord(50, 50);
705 size = newSize(100, 50);
706 assertEquals(
707 'Element position should be adjusted not to overflow left ' +
708 'edge of viewport, should overflow bottom edge.',
709 goog.positioning.OverflowStatus.ADJUSTED_X |
710 goog.positioning.OverflowStatus.FAILED_TOP,
711 f(pos, size, viewport, overflow));
712 assertEquals('X Position should be adjusted to 100.', 100, pos.x);
713 assertEquals('Y Position should not have been changed.', 50, pos.y);
714 }
715
716
717 function testAdjustForViewportResizeHeight() {
718 var f = goog.positioning.adjustForViewport_;
719 var viewport = new goog.math.Box(0, 200, 200, 0);
720 var overflow = goog.positioning.Overflow.RESIZE_HEIGHT;
721
722 var pos = newCoord(150, 150);
723 var size = newSize(25, 100);
724 assertEquals(
725 'Viewport height should be resized.',
726 goog.positioning.OverflowStatus.HEIGHT_ADJUSTED,
727 f(pos, size, viewport, overflow));
728 assertEquals('Height should be resized to 50.', 50, size.height);
729 assertTrue(
730 'Output box is within viewport',
731 viewport.contains(
732 new goog.math.Box(
733 pos.y, pos.x + size.width, pos.y + size.height, pos.x)));
734
735 var pos = newCoord(0, 0);
736 var size = newSize(50, 250);
737 assertEquals(
738 'Viewport height should be resized.',
739 goog.positioning.OverflowStatus.HEIGHT_ADJUSTED,
740 f(pos, size, viewport, overflow));
741 assertEquals('Height should be resized to 200.', 200, size.height);
742 assertTrue(
743 'Output box is within viewport',
744 viewport.contains(
745 new goog.math.Box(
746 pos.y, pos.x + size.width, pos.y + size.height, pos.x)));
747
748 var pos = newCoord(0, -50);
749 var size = newSize(50, 240);
750 assertEquals(
751 'Viewport height should be resized.',
752 goog.positioning.OverflowStatus.HEIGHT_ADJUSTED,
753 f(pos, size, viewport, overflow));
754 assertEquals('Height should be resized to 190.', 190, size.height);
755 assertTrue(
756 'Output box is within viewport',
757 viewport.contains(
758 new goog.math.Box(
759 pos.y, pos.x + size.width, pos.y + size.height, pos.x)));
760
761 var pos = newCoord(0, -50);
762 var size = newSize(50, 300);
763 assertEquals(
764 'Viewport height should be resized.',
765 goog.positioning.OverflowStatus.HEIGHT_ADJUSTED,
766 f(pos, size, viewport, overflow));
767 assertEquals('Height should be resized to 200.', 200, size.height);
768 assertTrue(
769 'Output box is within viewport',
770 viewport.contains(
771 new goog.math.Box(
772 pos.y, pos.x + size.width, pos.y + size.height, pos.x)));
773
774 pos = newCoord(150, 150);
775 size = newSize(50, 50);
776 assertEquals(
777 'No Viewport overflow.', goog.positioning.OverflowStatus.NONE,
778 f(pos, size, viewport, overflow));
779 assertTrue(
780 'Output box is within viewport',
781 viewport.contains(
782 new goog.math.Box(
783 pos.y, pos.x + size.width, pos.y + size.height, pos.x)));
784
785 var offsetViewport = new goog.math.Box(100, 200, 300, 0);
786 var pos = newCoord(0, 50);
787 var size = newSize(50, 240);
788 assertEquals(
789 'Viewport height should be resized.',
790 goog.positioning.OverflowStatus.HEIGHT_ADJUSTED,
791 f(pos, size, offsetViewport, overflow));
792 assertEquals('Height should be resized to 190.', 190, size.height);
793 assertTrue(
794 'Output box is within viewport',
795 offsetViewport.contains(
796 new goog.math.Box(
797 pos.y, pos.x + size.width, pos.y + size.height, pos.x)));
798 }
799
800
801
802 function testAdjustForViewportResizeWidth() {
803 var f = goog.positioning.adjustForViewport_;
804 var viewport = new goog.math.Box(0, 200, 200, 0);
805 var overflow = goog.positioning.Overflow.RESIZE_WIDTH;
806
807 var pos = newCoord(150, 150);
808 var size = newSize(100, 25);
809 assertEquals(
810 'Viewport width should be resized.',
811 goog.positioning.OverflowStatus.WIDTH_ADJUSTED,
812 f(pos, size, viewport, overflow));
813 assertEquals('Width should be resized to 50.', 50, size.width);
814 assertTrue(
815 'Output box is within viewport',
816 viewport.contains(
817 new goog.math.Box(
818 pos.y, pos.x + size.width, pos.y + size.height, pos.x)));
819
820 var pos = newCoord(0, 0);
821 var size = newSize(250, 50);
822 assertEquals(
823 'Viewport width should be resized.',
824 goog.positioning.OverflowStatus.WIDTH_ADJUSTED,
825 f(pos, size, viewport, overflow));
826 assertEquals('Width should be resized to 200.', 200, size.width);
827 assertTrue(
828 'Output box is within viewport',
829 viewport.contains(
830 new goog.math.Box(
831 pos.y, pos.x + size.width, pos.y + size.height, pos.x)));
832
833 var pos = newCoord(-50, 0);
834 var size = newSize(240, 50);
835 assertEquals(
836 'Viewport width should be resized.',
837 goog.positioning.OverflowStatus.WIDTH_ADJUSTED,
838 f(pos, size, viewport, overflow));
839 assertEquals('Width should be resized to 190.', 190, size.width);
840 assertTrue(
841 'Output box is within viewport',
842 viewport.contains(
843 new goog.math.Box(
844 pos.y, pos.x + size.width, pos.y + size.height, pos.x)));
845
846 var pos = newCoord(-50, 0);
847 var size = newSize(300, 50);
848 assertEquals(
849 'Viewport width should be resized.',
850 goog.positioning.OverflowStatus.WIDTH_ADJUSTED,
851 f(pos, size, viewport, overflow));
852 assertEquals('Width should be resized to 200.', 200, size.width);
853 assertTrue(
854 'Output box is within viewport',
855 viewport.contains(
856 new goog.math.Box(
857 pos.y, pos.x + size.width, pos.y + size.height, pos.x)));
858
859 pos = newCoord(150, 150);
860 size = newSize(50, 50);
861 assertEquals(
862 'No Viewport overflow.', goog.positioning.OverflowStatus.NONE,
863 f(pos, size, viewport, overflow));
864 assertTrue(
865 'Output box is within viewport',
866 viewport.contains(
867 new goog.math.Box(
868 pos.y, pos.x + size.width, pos.y + size.height, pos.x)));
869
870 var offsetViewport = new goog.math.Box(0, 300, 200, 100);
871 var pos = newCoord(50, 0);
872 var size = newSize(240, 50);
873 assertEquals(
874 'Viewport width should be resized.',
875 goog.positioning.OverflowStatus.WIDTH_ADJUSTED,
876 f(pos, size, offsetViewport, overflow));
877 assertEquals('Width should be resized to 190.', 190, size.width);
878 assertTrue(
879 'Output box is within viewport',
880 offsetViewport.contains(
881 new goog.math.Box(
882 pos.y, pos.x + size.width, pos.y + size.height, pos.x)));
883 }
884
885
886 function testPositionAtAnchorWithResizeHeight() {
887 var anchor = document.getElementById('anchor9');
888 var popup = document.getElementById('popup9');
889 var box = document.getElementById('box9');
890 var viewport = goog.style.getBounds(box);
891
892 var status = goog.positioning.positionAtAnchor(
893 anchor, corner.TOP_START, popup, corner.TOP_START,
894 new goog.math.Coordinate(0, -20), null,
895 goog.positioning.Overflow.RESIZE_HEIGHT, null, viewport.toBox());
896 assertEquals(
897 'Status should be HEIGHT_ADJUSTED.',
898 goog.positioning.OverflowStatus.HEIGHT_ADJUSTED, status);
899
900 var TOLERANCE = 0.1;
901 // Adjust the viewport to allow some tolerance for subpixel positioning,
902 // this is required for this test to pass on IE10,11
903 viewport.top -= TOLERANCE;
904 viewport.left -= TOLERANCE;
905
906 assertTrue(
907 'Popup ' + goog.style.getBounds(popup) + ' not is within viewport' +
908 viewport,
909 viewport.contains(goog.style.getBounds(popup)));
910 }
911
912
913 function testPositionAtCoordinateResizeHeight() {
914 var f = goog.positioning.positionAtCoordinate;
915 var viewport = new goog.math.Box(0, 50, 50, 0);
916 var overflow = goog.positioning.Overflow.RESIZE_HEIGHT |
917 goog.positioning.Overflow.ADJUST_Y;
918 var popup = document.getElementById('popup1');
919 var corner = goog.positioning.Corner.BOTTOM_LEFT;
920
921 var pos = newCoord(100, 100);
922
923 assertEquals(
924 'Viewport height should be resized.',
925 goog.positioning.OverflowStatus.HEIGHT_ADJUSTED |
926 goog.positioning.OverflowStatus.ADJUSTED_Y,
927 f(pos, popup, corner, undefined, viewport, overflow));
928 var bounds = goog.style.getSize(popup);
929 assertEquals(
930 'Height should be resized to the size of the viewport.', 50,
931 bounds.height);
932 }
933
934
935 function testGetPositionAtCoordinateResizeHeight() {
936 var f = goog.positioning.getPositionAtCoordinate;
937 var viewport = new goog.math.Box(0, 50, 50, 0);
938 var overflow = goog.positioning.Overflow.RESIZE_HEIGHT |
939 goog.positioning.Overflow.ADJUST_Y;
940 var popup = document.getElementById('popup1');
941 var corner = goog.positioning.Corner.BOTTOM_LEFT;
942
943 var pos = newCoord(100, 100);
944 var size = goog.style.getSize(popup);
945
946 var result = f(pos, size, corner, undefined, viewport, overflow);
947 assertEquals(
948 'Viewport height should be resized.',
949 goog.positioning.OverflowStatus.HEIGHT_ADJUSTED |
950 goog.positioning.OverflowStatus.ADJUSTED_Y,
951 result.status);
952 assertEquals(
953 'Height should be resized to the size of the viewport.', 50,
954 result.rect.height);
955 }
956
957
958 function testGetEffectiveCornerLeftToRight() {
959 var f = goog.positioning.getEffectiveCorner;
960 var el = document.getElementById('ltr');
961
962 assertEquals(
963 'TOP_LEFT should be unchanged for ltr.', corner.TOP_LEFT,
964 f(el, corner.TOP_LEFT));
965 assertEquals(
966 'TOP_RIGHT should be unchanged for ltr.', corner.TOP_RIGHT,
967 f(el, corner.TOP_RIGHT));
968 assertEquals(
969 'BOTTOM_LEFT should be unchanged for ltr.', corner.BOTTOM_LEFT,
970 f(el, corner.BOTTOM_LEFT));
971 assertEquals(
972 'BOTTOM_RIGHT should be unchanged for ltr.', corner.BOTTOM_RIGHT,
973 f(el, corner.BOTTOM_RIGHT));
974
975 assertEquals(
976 'TOP_START should be TOP_LEFT for ltr.', corner.TOP_LEFT,
977 f(el, corner.TOP_START));
978 assertEquals(
979 'TOP_END should be TOP_RIGHT for ltr.', corner.TOP_RIGHT,
980 f(el, corner.TOP_END));
981 assertEquals(
982 'BOTTOM_START should be BOTTOM_LEFT for ltr.', corner.BOTTOM_LEFT,
983 f(el, corner.BOTTOM_START));
984 assertEquals(
985 'BOTTOM_END should be BOTTOM_RIGHT for ltr.', corner.BOTTOM_RIGHT,
986 f(el, corner.BOTTOM_END));
987 }
988
989
990 function testGetEffectiveCornerRightToLeft() {
991 var f = goog.positioning.getEffectiveCorner;
992 var el = document.getElementById('rtl');
993
994 assertEquals(
995 'TOP_LEFT should be unchanged for rtl.', corner.TOP_LEFT,
996 f(el, corner.TOP_LEFT));
997 assertEquals(
998 'TOP_RIGHT should be unchanged for rtl.', corner.TOP_RIGHT,
999 f(el, corner.TOP_RIGHT));
1000 assertEquals(
1001 'BOTTOM_LEFT should be unchanged for rtl.', corner.BOTTOM_LEFT,
1002 f(el, corner.BOTTOM_LEFT));
1003 assertEquals(
1004 'BOTTOM_RIGHT should be unchanged for rtl.', corner.BOTTOM_RIGHT,
1005 f(el, corner.BOTTOM_RIGHT));
1006
1007 assertEquals(
1008 'TOP_START should be TOP_RIGHT for rtl.', corner.TOP_RIGHT,
1009 f(el, corner.TOP_START));
1010 assertEquals(
1011 'TOP_END should be TOP_LEFT for rtl.', corner.TOP_LEFT,
1012 f(el, corner.TOP_END));
1013 assertEquals(
1014 'BOTTOM_START should be BOTTOM_RIGHT for rtl.', corner.BOTTOM_RIGHT,
1015 f(el, corner.BOTTOM_START));
1016 assertEquals(
1017 'BOTTOM_END should be BOTTOM_LEFT for rtl.', corner.BOTTOM_LEFT,
1018 f(el, corner.BOTTOM_END));
1019 }
1020
1021
1022 function testFlipCornerHorizontal() {
1023 var f = goog.positioning.flipCornerHorizontal;
1024
1025 assertEquals(
1026 'TOP_LEFT should be flipped to TOP_RIGHT.', corner.TOP_RIGHT,
1027 f(corner.TOP_LEFT));
1028 assertEquals(
1029 'TOP_RIGHT should be flipped to TOP_LEFT.', corner.TOP_LEFT,
1030 f(corner.TOP_RIGHT));
1031 assertEquals(
1032 'BOTTOM_LEFT should be flipped to BOTTOM_RIGHT.', corner.BOTTOM_RIGHT,
1033 f(corner.BOTTOM_LEFT));
1034 assertEquals(
1035 'BOTTOM_RIGHT should be flipped to BOTTOM_LEFT.', corner.BOTTOM_LEFT,
1036 f(corner.BOTTOM_RIGHT));
1037
1038 assertEquals(
1039 'TOP_START should be flipped to TOP_END.', corner.TOP_END,
1040 f(corner.TOP_START));
1041 assertEquals(
1042 'TOP_END should be flipped to TOP_START.', corner.TOP_START,
1043 f(corner.TOP_END));
1044 assertEquals(
1045 'BOTTOM_START should be flipped to BOTTOM_END.', corner.BOTTOM_END,
1046 f(corner.BOTTOM_START));
1047 assertEquals(
1048 'BOTTOM_END should be flipped to BOTTOM_START.', corner.BOTTOM_START,
1049 f(corner.BOTTOM_END));
1050 }
1051
1052
1053 function testFlipCornerVertical() {
1054 var f = goog.positioning.flipCornerVertical;
1055
1056 assertEquals(
1057 'TOP_LEFT should be flipped to BOTTOM_LEFT.', corner.BOTTOM_LEFT,
1058 f(corner.TOP_LEFT));
1059 assertEquals(
1060 'TOP_RIGHT should be flipped to BOTTOM_RIGHT.', corner.BOTTOM_RIGHT,
1061 f(corner.TOP_RIGHT));
1062 assertEquals(
1063 'BOTTOM_LEFT should be flipped to TOP_LEFT.', corner.TOP_LEFT,
1064 f(corner.BOTTOM_LEFT));
1065 assertEquals(
1066 'BOTTOM_RIGHT should be flipped to TOP_RIGHT.', corner.TOP_RIGHT,
1067 f(corner.BOTTOM_RIGHT));
1068
1069 assertEquals(
1070 'TOP_START should be flipped to BOTTOM_START.', corner.BOTTOM_START,
1071 f(corner.TOP_START));
1072 assertEquals(
1073 'TOP_END should be flipped to BOTTOM_END.', corner.BOTTOM_END,
1074 f(corner.TOP_END));
1075 assertEquals(
1076 'BOTTOM_START should be flipped to TOP_START.', corner.TOP_START,
1077 f(corner.BOTTOM_START));
1078 assertEquals(
1079 'BOTTOM_END should be flipped to TOP_END.', corner.TOP_END,
1080 f(corner.BOTTOM_END));
1081 }
1082
1083
1084 function testFlipCorner() {
1085 var f = goog.positioning.flipCorner;
1086
1087 assertEquals(
1088 'TOP_LEFT should be flipped to BOTTOM_RIGHT.', corner.BOTTOM_RIGHT,
1089 f(corner.TOP_LEFT));
1090 assertEquals(
1091 'TOP_RIGHT should be flipped to BOTTOM_LEFT.', corner.BOTTOM_LEFT,
1092 f(corner.TOP_RIGHT));
1093 assertEquals(
1094 'BOTTOM_LEFT should be flipped to TOP_RIGHT.', corner.TOP_RIGHT,
1095 f(corner.BOTTOM_LEFT));
1096 assertEquals(
1097 'BOTTOM_RIGHT should be flipped to TOP_LEFT.', corner.TOP_LEFT,
1098 f(corner.BOTTOM_RIGHT));
1099
1100 assertEquals(
1101 'TOP_START should be flipped to BOTTOM_END.', corner.BOTTOM_END,
1102 f(corner.TOP_START));
1103 assertEquals(
1104 'TOP_END should be flipped to BOTTOM_START.', corner.BOTTOM_START,
1105 f(corner.TOP_END));
1106 assertEquals(
1107 'BOTTOM_START should be flipped to TOP_END.', corner.TOP_END,
1108 f(corner.BOTTOM_START));
1109 assertEquals(
1110 'BOTTOM_END should be flipped to TOP_START.', corner.TOP_START,
1111 f(corner.BOTTOM_END));
1112 }
1113
1114 function testPositionAtAnchorFrameViewportStandard() {
1115 var iframe = document.getElementById('iframe-standard');
1116 var iframeDoc = goog.dom.getFrameContentDocument(iframe);
1117 assertTrue(new goog.dom.DomHelper(iframeDoc).isCss1CompatMode());
1118
1119 new goog.dom.DomHelper(iframeDoc).getDocumentScrollElement().scrollTop = 100;
1120 var anchor = iframeDoc.getElementById('anchor1');
1121 var popup = document.getElementById('popup6');
1122
1123 var status = goog.positioning.positionAtAnchor(
1124 anchor, corner.TOP_RIGHT, popup, corner.BOTTOM_RIGHT);
1125 var iframeRect = goog.style.getBounds(iframe);
1126 var popupRect = goog.style.getBounds(popup);
1127 assertEquals(
1128 'Status should not have any ADJUSTED and FAILED.',
1129 goog.positioning.OverflowStatus.NONE, status);
1130 assertRoundedEquals(
1131 'Popup should be positioned just above the iframe, ' +
1132 'not above the anchor element inside the iframe',
1133 iframeRect.top, popupRect.top + popupRect.height);
1134 }
1135
1136 function testPositionAtAnchorFrameViewportQuirk() {
1137 var iframe = document.getElementById('iframe-quirk');
1138 var iframeDoc = goog.dom.getFrameContentDocument(iframe);
1139 assertFalse(new goog.dom.DomHelper(iframeDoc).isCss1CompatMode());
1140
1141 window.scrollTo(0, 100);
1142 new goog.dom.DomHelper(iframeDoc).getDocumentScrollElement().scrollTop = 100;
1143 var anchor = iframeDoc.getElementById('anchor1');
1144 var popup = document.getElementById('popup6');
1145
1146 var status = goog.positioning.positionAtAnchor(
1147 anchor, corner.TOP_RIGHT, popup, corner.BOTTOM_RIGHT);
1148 var iframeRect = goog.style.getBounds(iframe);
1149 var popupRect = goog.style.getBounds(popup);
1150 assertEquals(
1151 'Status should not have any ADJUSTED and FAILED.',
1152 goog.positioning.OverflowStatus.NONE, status);
1153 assertRoundedEquals(
1154 'Popup should be positioned just above the iframe, ' +
1155 'not above the anchor element inside the iframe',
1156 iframeRect.top, popupRect.top + popupRect.height);
1157 }
1158
1159 function testPositionAtAnchorFrameViewportWithPopupInScroller() {
1160 var iframe = document.getElementById('iframe-standard');
1161 var iframeDoc = goog.dom.getFrameContentDocument(iframe);
1162
1163 new goog.dom.DomHelper(iframeDoc).getDocumentScrollElement().scrollTop = 100;
1164 var anchor = iframeDoc.getElementById('anchor1');
1165 var popup = document.getElementById('popup7');
1166 popup.offsetParent.scrollTop = 50;
1167
1168 var status = goog.positioning.positionAtAnchor(
1169 anchor, corner.TOP_RIGHT, popup, corner.BOTTOM_RIGHT);
1170 var iframeRect = goog.style.getBounds(iframe);
1171 var popupRect = goog.style.getBounds(popup);
1172 assertEquals(
1173 'Status should not have any ADJUSTED and FAILED.',
1174 goog.positioning.OverflowStatus.NONE, status);
1175 assertRoughlyEquals(
1176 'Popup should be positioned just above the iframe, ' +
1177 'not above the anchor element inside the iframe',
1178 iframeRect.top, popupRect.top + popupRect.height, ALLOWED_OFFSET);
1179 }
1180
1181 function testPositionAtAnchorNestedFrames() {
1182 var outerIframe = document.getElementById('nested-outer');
1183 var outerDoc = goog.dom.getFrameContentDocument(outerIframe);
1184 var popup = outerDoc.getElementById('popup1');
1185 var innerIframe = outerDoc.getElementById('inner-frame');
1186 var innerDoc = goog.dom.getFrameContentDocument(innerIframe);
1187 var anchor = innerDoc.getElementById('anchor1');
1188
1189 var status = goog.positioning.positionAtAnchor(
1190 anchor, corner.TOP_LEFT, popup, corner.BOTTOM_LEFT);
1191 assertEquals(
1192 'Status should not have any ADJUSTED and FAILED.',
1193 goog.positioning.OverflowStatus.NONE, status);
1194 var innerIframeRect = goog.style.getBounds(innerIframe);
1195 var popupRect = goog.style.getBounds(popup);
1196 assertRoundedEquals(
1197 'Top of frame should align with bottom of the popup', innerIframeRect.top,
1198 popupRect.top + popupRect.height);
1199
1200 // The anchor is scrolled up by 10px.
1201 // Popup position should be the same as above.
1202 goog.dom.getWindow(innerDoc).scrollTo(0, 10);
1203 status = goog.positioning.positionAtAnchor(
1204 anchor, corner.TOP_LEFT, popup, corner.BOTTOM_LEFT);
1205 assertEquals(
1206 'Status should not have any ADJUSTED and FAILED.',
1207 goog.positioning.OverflowStatus.NONE, status);
1208 innerIframeRect = goog.style.getBounds(innerIframe);
1209 popupRect = goog.style.getBounds(popup);
1210 assertRoundedEquals(
1211 'Top of frame should align with bottom of the popup', innerIframeRect.top,
1212 popupRect.top + popupRect.height);
1213 }
1214
1215 function testPositionAtAnchorOffscreen() {
1216 var offset = 0;
1217 var anchor = goog.dom.getElement('offscreen-anchor');
1218 var popup = goog.dom.getElement('popup3');
1219
1220 goog.positioning.positionAtAnchor(
1221 anchor, corner.TOP_LEFT, popup, corner.TOP_LEFT, null, null,
1222 overflow.ADJUST_X | overflow.ADJUST_Y);
1223 assertObjectEquals(newCoord(offset, offset), goog.style.getPageOffset(popup));
1224
1225 goog.positioning.positionAtAnchor(
1226 anchor, corner.TOP_LEFT, popup, corner.TOP_LEFT, null, null,
1227 overflow.ADJUST_X_EXCEPT_OFFSCREEN | overflow.ADJUST_Y);
1228 assertObjectEquals(newCoord(-1000, offset), goog.style.getPageOffset(popup));
1229
1230 goog.positioning.positionAtAnchor(
1231 anchor, corner.TOP_LEFT, popup, corner.TOP_LEFT, null, null,
1232 overflow.ADJUST_X | overflow.ADJUST_Y_EXCEPT_OFFSCREEN);
1233 assertObjectEquals(newCoord(offset, -1000), goog.style.getPageOffset(popup));
1234
1235 goog.positioning.positionAtAnchor(
1236 anchor, corner.TOP_LEFT, popup, corner.TOP_LEFT, null, null,
1237 overflow.ADJUST_X_EXCEPT_OFFSCREEN | overflow.ADJUST_Y_EXCEPT_OFFSCREEN);
1238 assertObjectEquals(newCoord(-1000, -1000), goog.style.getPageOffset(popup));
1239 }
1240
1241 function testPositionAtAnchorWithOverflowScrollOffsetParent() {
1242 var testAreaOffset = goog.style.getPageOffset(testArea);
1243 var scrollbarWidth = goog.style.getScrollbarWidth();
1244 window.scrollTo(testAreaOffset.x, testAreaOffset.y);
1245
1246 var overflowDiv = goog.dom.createElement(goog.dom.TagName.DIV);
1247 overflowDiv.style.overflow = 'scroll';
1248 overflowDiv.style.position = 'relative';
1249 goog.style.setSize(overflowDiv, 200 /* width */, 100 /* height */);
1250
1251 var anchor = goog.dom.createElement(goog.dom.TagName.DIV);
1252 anchor.style.position = 'absolute';
1253 goog.style.setSize(anchor, 50 /* width */, 50 /* height */);
1254 goog.style.setPosition(anchor, 300 /* left */, 300 /* top */);
1255
1256 var popup = createPopupDiv(75 /* width */, 50 /* height */);
1257
1258 goog.dom.append(testArea, overflowDiv, anchor);
1259 goog.dom.append(overflowDiv, popup);
1260
1261 // Popup should always be positioned within the overflowDiv
1262 goog.style.setPosition(overflowDiv, 0 /* left */, 0 /* top */);
1263 goog.positioning.positionAtAnchor(
1264 anchor, corner.TOP_LEFT, popup, corner.TOP_RIGHT, null, null,
1265 overflow.ADJUST_X | overflow.ADJUST_Y);
1266 assertObjectRoughlyEquals(
1267 new goog.math.Coordinate(
1268 testAreaOffset.x + 200 - 75 - scrollbarWidth,
1269 testAreaOffset.y + 100 - 50 - scrollbarWidth),
1270 goog.style.getPageOffset(popup), 1);
1271
1272 goog.style.setPosition(overflowDiv, 400 /* left */, 0 /* top */);
1273 goog.positioning.positionAtAnchor(
1274 anchor, corner.TOP_RIGHT, popup, corner.TOP_LEFT, null, null,
1275 overflow.ADJUST_X | overflow.ADJUST_Y);
1276 assertObjectRoughlyEquals(
1277 new goog.math.Coordinate(
1278 testAreaOffset.x + 400, testAreaOffset.y + 100 - 50 - scrollbarWidth),
1279 goog.style.getPageOffset(popup), 1);
1280
1281 goog.style.setPosition(overflowDiv, 0 /* left */, 400 /* top */);
1282 goog.positioning.positionAtAnchor(
1283 anchor, corner.BOTTOM_LEFT, popup, corner.BOTTOM_RIGHT, null, null,
1284 overflow.ADJUST_X | overflow.ADJUST_Y);
1285 assertObjectRoughlyEquals(
1286 new goog.math.Coordinate(
1287 testAreaOffset.x + 200 - 75 - scrollbarWidth, testAreaOffset.y + 400),
1288 goog.style.getPageOffset(popup), 1);
1289
1290 goog.style.setPosition(overflowDiv, 400 /* left */, 400 /* top */);
1291 goog.positioning.positionAtAnchor(
1292 anchor, corner.BOTTOM_RIGHT, popup, corner.BOTTOM_LEFT, null, null,
1293 overflow.ADJUST_X | overflow.ADJUST_Y);
1294 assertObjectRoughlyEquals(
1295 new goog.math.Coordinate(testAreaOffset.x + 400, testAreaOffset.y + 400),
1296 goog.style.getPageOffset(popup), 1);
1297
1298 // No overflow.
1299 goog.style.setPosition(overflowDiv, 300 - 50 /* left */, 300 /* top */);
1300 goog.positioning.positionAtAnchor(
1301 anchor, corner.TOP_LEFT, popup, corner.TOP_RIGHT, null, null,
1302 overflow.ADJUST_X | overflow.ADJUST_Y);
1303 assertObjectRoughlyEquals(
1304 new goog.math.Coordinate(
1305 testAreaOffset.x + 300 - 50, testAreaOffset.y + 300),
1306 goog.style.getPageOffset(popup), 1);
1307 }
1308
1309 function testPositionAtAnchorWithOverflowHiddenParent() {
1310 var testAreaOffset = goog.style.getPageOffset(testArea);
1311 window.scrollTo(testAreaOffset.x, testAreaOffset.y);
1312
1313 var overflowDiv = goog.dom.createElement(goog.dom.TagName.DIV);
1314 overflowDiv.style.overflow = 'hidden';
1315 overflowDiv.style.position = 'relative';
1316 goog.style.setSize(overflowDiv, 200 /* width */, 100 /* height */);
1317
1318 var anchor = goog.dom.createElement(goog.dom.TagName.DIV);
1319 anchor.style.position = 'absolute';
1320 goog.style.setSize(anchor, 50 /* width */, 50 /* height */);
1321 goog.style.setPosition(anchor, 300 /* left */, 300 /* top */);
1322
1323 var popup = createPopupDiv(75 /* width */, 50 /* height */);
1324
1325 goog.dom.append(testArea, overflowDiv, anchor);
1326 goog.dom.append(overflowDiv, popup);
1327
1328 // Popup should always be positioned within the overflowDiv
1329 goog.style.setPosition(overflowDiv, 0 /* left */, 0 /* top */);
1330 goog.positioning.positionAtAnchor(
1331 anchor, corner.TOP_LEFT, popup, corner.TOP_RIGHT, null, null,
1332 overflow.ADJUST_X | overflow.ADJUST_Y);
1333 assertObjectRoughlyEquals(
1334 new goog.math.Coordinate(
1335 testAreaOffset.x + 200 - 75, testAreaOffset.y + 100 - 50),
1336 goog.style.getPageOffset(popup), 1);
1337
1338 goog.style.setPosition(overflowDiv, 400 /* left */, 0 /* top */);
1339 goog.positioning.positionAtAnchor(
1340 anchor, corner.TOP_RIGHT, popup, corner.TOP_LEFT, null, null,
1341 overflow.ADJUST_X | overflow.ADJUST_Y);
1342 assertObjectRoughlyEquals(
1343 new goog.math.Coordinate(
1344 testAreaOffset.x + 400, testAreaOffset.y + 100 - 50),
1345 goog.style.getPageOffset(popup), 1);
1346
1347 goog.style.setPosition(overflowDiv, 0 /* left */, 400 /* top */);
1348 goog.positioning.positionAtAnchor(
1349 anchor, corner.BOTTOM_LEFT, popup, corner.BOTTOM_RIGHT, null, null,
1350 overflow.ADJUST_X | overflow.ADJUST_Y);
1351 assertObjectRoughlyEquals(
1352 new goog.math.Coordinate(
1353 testAreaOffset.x + 200 - 75, testAreaOffset.y + 400),
1354 goog.style.getPageOffset(popup), 1);
1355
1356 goog.style.setPosition(overflowDiv, 400 /* left */, 400 /* top */);
1357 goog.positioning.positionAtAnchor(
1358 anchor, corner.BOTTOM_RIGHT, popup, corner.BOTTOM_LEFT, null, null,
1359 overflow.ADJUST_X | overflow.ADJUST_Y);
1360 assertObjectRoughlyEquals(
1361 new goog.math.Coordinate(testAreaOffset.x + 400, testAreaOffset.y + 400),
1362 goog.style.getPageOffset(popup), 1);
1363
1364 // No overflow.
1365 goog.style.setPosition(overflowDiv, 300 - 50 /* left */, 300 /* top */);
1366 goog.positioning.positionAtAnchor(
1367 anchor, corner.TOP_LEFT, popup, corner.TOP_RIGHT, null, null,
1368 overflow.ADJUST_X | overflow.ADJUST_Y);
1369 assertObjectRoughlyEquals(
1370 new goog.math.Coordinate(
1371 testAreaOffset.x + 300 - 50, testAreaOffset.y + 300),
1372 goog.style.getPageOffset(popup), 1);
1373 }
1374
1375 function createPopupDiv(width, height) {
1376 var popupDiv = goog.dom.createElement(goog.dom.TagName.DIV);
1377 popupDiv.style.position = 'absolute';
1378 goog.style.setSize(popupDiv, width, height);
1379 goog.style.setPosition(popupDiv, 0 /* left */, 250 /* top */);
1380 return popupDiv;
1381 }
1382
1383 function newCoord(x, y) {
1384 return new goog.math.Coordinate(x, y);
1385 }
1386
1387 function newSize(w, h) {
1388 return new goog.math.Size(w, h);
1389 }
1390
1391 function newBox(coord, size) {}