UNPKG

sku-pg

Version:

Skulpt is a Javascript implementation of Python 2.x. Python that runs in your browser!

878 lines (762 loc) 34.2 kB
<!DOCTYPE html> <!-- Author: eae@google.com (Emil A Eklund) --> <html> <!-- Copyright 2008 The Closure Library Authors. All Rights Reserved. Use of this source code is governed by an Apache 2.0 License. See the COPYING file for details. --> <head> <title>Closure Unit Tests - goog.positioning</title> <script src="../base.js"></script> <script> goog.require('goog.math.Box'); goog.require('goog.math.Coordinate'); goog.require('goog.math.Rect'); goog.require('goog.style'); goog.require('goog.positioning'); goog.require('goog.testing.jsunit'); goog.require('goog.userAgent'); </script> <style> .box1 { border: 1px solid black; margin: 10px; padding: 5px; height: 150px; } .outerbox { border: 1px solid gray; padding: 3px; margin: 5px; } .box2 { position: relative; padding: 0px; /* If the padding has >0 value, IE6 fails some tests. */ margin: -2px; } .anchorFrame { overflow: auto; width: 100px; height: 100px; } #popup1, #popup2, #popup3, #popup5, #popup6, #popup7 { position: absolute; border: 1px solid red; width: 100px; height: 100px; } #anchor1 { border: 1px solid blue; } #anchor4 { position: absolute; left: 2px; } </style> </head> <body> <div id="ltr" dir="ltr"> Left to right element. </div> <div id="rtl" dir="rtl"> Right to left element. </div> <div class="outerbox"> <div class="box1"> <span id="anchor1">Anchor LTR.</span> </div> <div class="box2"> <div id="popup1"> <div>Popup ltr.</div> </div> </div> </div> <div class="outerbox" dir="rtl"> <div class="box1"> <span id="anchor2">Anchor RTL.</span> </div> <div class="box2"> <div id="popup2"> <div>Popup rtl.</div> </div> </div> </div> <div id="anchor4"> Anchor 4. </div> <div id="popup3"> Popup. </div> <div dir="rtl" style="border: 1px solid red;"> <div dir="rtl" style="position: relative; overflow: auto; width: 150px; height: 100px; border: 1px solid black;"> <div style="height: 200px;"> <span id="anchor5">Anchor 5.</span> </div> <div id="popup5"> Popup. </div> </div> </div> <iframe id="iframe-standard" src="positioning_test_standard.html" class="anchorFrame"> </iframe> <iframe id="iframe-quirk" src="positioning_test_quirk.html" class="anchorFrame"> </iframe> <div id="popup6">Popup6</div> <div style="position:relative;height:100px;width:100px;overflow:auto;"> I hate positioning! <div>1</div> <div>2</div> <div>3</div> <div>4</div> <div>5</div> <div>6</div> <div>7</div> <div id="popup7">Popup7</div> </div> <iframe id="nested-outer" src="positioning_test_iframe1.html" style="overflow:auto;width:150px;height:150px;"></iframe> <script> // Allow positions to be off by one in gecko as it reports scrolling // offsets in steps of 2. var ALLOWED_OFFSET = goog.userAgent.GECKO ? 1 : 0; function setUpPage() { var viewportSize = goog.dom.getViewportSize(); // Some tests need enough size viewport. if (viewportSize.width < 600 || viewportSize.height < 600) { window.moveTo(0, 0); window.resizeTo(640, 640); } } function setUp() { window.scrollTo(0, 0); } /** * This is used to round pixel values on FF3 Mac. */ function assertRoundedEquals(a, b, c) { function round(x) { return goog.userAgent.GECKO && (goog.userAgent.MAC || goog.userAgent.X11) && goog.userAgent.isVersion('1.9') ? Math.round(x) : x; } if (arguments.length == 3) { assertRoughlyEquals(a, round(b), round(c), ALLOWED_OFFSET); } else { assertRoughlyEquals(round(a), round(b), ALLOWED_OFFSET); } } function testPositionAtAnchorLeftToRight() { var anchor = document.getElementById('anchor1'); var popup = document.getElementById('popup1'); var corner = goog.positioning.Corner; // Anchor top left to top left. goog.positioning.positionAtAnchor(anchor, corner.TOP_LEFT, popup, corner.TOP_LEFT); var anchorRect = goog.style.getBounds(anchor); var popupRect = goog.style.getBounds(popup); assertRoundedEquals('Left edge of popup should line up with left edge ' + 'of anchor.', anchorRect.left, popupRect.left); assertRoundedEquals('Popup should have the same y position as the anchor.', anchorRect.top, popupRect.top); // Anchor top left to bottom left. goog.positioning.positionAtAnchor(anchor, corner.BOTTOM_LEFT, popup, corner.TOP_LEFT); anchorRect = goog.style.getBounds(anchor); popupRect = goog.style.getBounds(popup); assertRoundedEquals('Left edge of popup should line up with left edge ' + 'of anchor.', anchorRect.left, popupRect.left); assertRoundedEquals('Popup should be positioned just below the anchor.', anchorRect.top + anchorRect.height, popupRect.top); // Anchor top left to top right. goog.positioning.positionAtAnchor(anchor, corner.TOP_RIGHT, popup, corner.TOP_LEFT); anchorRect = goog.style.getBounds(anchor); popupRect = goog.style.getBounds(popup); assertRoundedEquals('Popup should be positioned just right of the anchor.', anchorRect.left + anchorRect.width, popupRect.left); assertRoundedEquals('Popup should have the same y position as the anchor.', anchorRect.top, popupRect.top); // Anchor top right to bottom right. goog.positioning.positionAtAnchor(anchor, corner.BOTTOM_RIGHT, popup, corner.TOP_RIGHT); anchorRect = goog.style.getBounds(anchor); popupRect = goog.style.getBounds(popup); assertRoundedEquals('Right edge of popup should line up with right edge ' + 'of anchor.', anchorRect.left + anchorRect.width, popupRect.left + popupRect.width); assertRoundedEquals('Popup should be positioned just below the anchor.', anchorRect.top + anchorRect.height, popupRect.top); // Anchor top start to bottom start. goog.positioning.positionAtAnchor(anchor, corner.BOTTOM_START, popup, corner.TOP_START); anchorRect = goog.style.getBounds(anchor); popupRect = goog.style.getBounds(popup); assertRoundedEquals('Left edge of popup should line up with left edge ' + 'of anchor.', anchorRect.left, popupRect.left); assertRoundedEquals('Popup should be positioned just below the anchor.', anchorRect.top + anchorRect.height, popupRect.top); } function testPositionAtAnchorWithOffset() { var anchor = document.getElementById('anchor1'); var popup = document.getElementById('popup1'); var corner = goog.positioning.Corner; // Anchor top left to top left with an offset moving the popup away from the // anchor. goog.positioning.positionAtAnchor(anchor, corner.TOP_LEFT, popup, corner.TOP_LEFT, new goog.math.Coordinate(-15, -20)); var anchorRect = goog.style.getBounds(anchor); var popupRect = goog.style.getBounds(popup); assertRoundedEquals('Left edge of popup should be fifteen pixels from ' + 'anchor.', anchorRect.left, popupRect.left + 15); assertRoundedEquals('Top edge of popup should be twenty pixels from anchor.', anchorRect.top, popupRect.top + 20); // Anchor top left to top left with an offset moving the popup towards the // anchor. goog.positioning.positionAtAnchor(anchor, corner.TOP_LEFT, popup, corner.TOP_LEFT, new goog.math.Coordinate(3, 1)); anchorRect = goog.style.getBounds(anchor); popupRect = goog.style.getBounds(popup); assertRoundedEquals('Left edge of popup should be three pixels right of ' + 'the anchor\'s left edge', anchorRect.left, popupRect.left - 3); assertRoundedEquals('Top edge of popup should be one pixel below of the ' + 'anchor\'s top edge', anchorRect.top, popupRect.top - 1); } function testPositionAtAnchorOverflowLeftEdgeRightToLeft() { var anchor = document.getElementById('anchor5'); var popup = document.getElementById('popup5'); var corner = goog.positioning.Corner; var overflow = goog.positioning.Overflow; var status = goog.positioning.positionAtAnchor(anchor, corner.TOP_LEFT, popup, corner.TOP_RIGHT, undefined, undefined, overflow.FAIL_X); assertFalse('Positioning operation should have failed.', (status & goog.positioning.OverflowStatus.FAILED) == 0); // Change overflow strategy to ADJUST. status = goog.positioning.positionAtAnchor(anchor, corner.TOP_LEFT, popup, corner.TOP_RIGHT, undefined, undefined, overflow.ADJUST_X); assertTrue('Positioning operation should have been successful.', (status & goog.positioning.OverflowStatus.FAILED) == 0); assertTrue('Positioning operation should have been adjusted.', (status & goog.positioning.OverflowStatus.ADJUSTED_X) != 0); var anchorRect = goog.style.getBounds(anchor); var popupRect = goog.style.getBounds(popup); var parentRect = goog.style.getBounds(anchor.parentNode); assertTrue('Position should have been adjusted so that the left edge of ' + 'the popup is left of the anchor but still within the bounding ' + 'box of the parent container.', anchorRect.left <= popupRect.left <= parentRect.left); } function testPositionAtAnchorWithMargin() { var anchor = document.getElementById('anchor1'); var popup = document.getElementById('popup1'); var corner = goog.positioning.Corner; // Anchor top left to top left. goog.positioning.positionAtAnchor(anchor, corner.TOP_LEFT, popup, corner.TOP_LEFT, undefined, new goog.math.Box(1, 2, 3, 4)); var anchorRect = goog.style.getBounds(anchor); var popupRect = goog.style.getBounds(popup); assertRoundedEquals('Left edge of popup should be four pixels from anchor.', anchorRect.left, popupRect.left - 4); assertRoundedEquals('Top edge of popup should be one pixels from anchor.', anchorRect.top, popupRect.top - 1); // Anchor top right to bottom right. goog.positioning.positionAtAnchor(anchor, corner.BOTTOM_RIGHT, popup, corner.TOP_RIGHT, undefined, new goog.math.Box(1, 2, 3, 4)); anchorRect = goog.style.getBounds(anchor); popupRect = goog.style.getBounds(popup); assertRoundedEquals('Right edge of popup should line up with right edge ' + 'of anchor.', anchorRect.left + anchorRect.width, popupRect.left + popupRect.width + 2); assertRoundedEquals('Popup should be positioned just below the anchor.', anchorRect.top + anchorRect.height, popupRect.top - 1); } function testPositionAtAnchorRightToLeft() { if (goog.userAgent.IE && goog.userAgent.isVersion('6')) { // These tests fails with IE6. // TODO(user): Investigate the reason. return; } var anchor = document.getElementById('anchor2'); var popup = document.getElementById('popup2'); var corner = goog.positioning.Corner; // Anchor top left to top left. goog.positioning.positionAtAnchor(anchor, corner.TOP_LEFT, popup, corner.TOP_LEFT); var anchorRect = goog.style.getBounds(anchor); var popupRect = goog.style.getBounds(popup); assertRoundedEquals('Left edge of popup should line up with left edge ' + 'of anchor.', anchorRect.left, popupRect.left); assertRoundedEquals('Popup should have the same y position as the anchor.', anchorRect.top, popupRect.top); // Anchor top start to bottom start. goog.positioning.positionAtAnchor(anchor, corner.BOTTOM_START, popup, corner.TOP_START); anchorRect = goog.style.getBounds(anchor); popupRect = goog.style.getBounds(popup); assertRoundedEquals('Right edge of popup should line up with right edge ' + 'of anchor.', anchorRect.left + anchorRect.width, popupRect.left + popupRect.width); assertRoundedEquals('Popup should be positioned just below the anchor.', anchorRect.top + anchorRect.height, popupRect.top); } function testPositionAtAnchorBodyViewport() { var anchor = document.getElementById('anchor1'); var popup = document.getElementById('popup3'); var corner = goog.positioning.Corner; // Anchor top left to top left. goog.positioning.positionAtAnchor(anchor, corner.TOP_LEFT, popup, corner.TOP_LEFT); var anchorRect = goog.style.getBounds(anchor); var popupRect = goog.style.getBounds(popup); assertEquals('Left edge of popup should line up with left edge of anchor.', anchorRect.left, popupRect.left); assertEquals('Popup should have the same y position as the anchor.', anchorRect.top, popupRect.top); // Anchor top start to bottom right. goog.positioning.positionAtAnchor(anchor, corner.BOTTOM_RIGHT, popup, corner.TOP_RIGHT); anchorRect = goog.style.getBounds(anchor); popupRect = goog.style.getBounds(popup); assertEquals('Right edge of popup should line up with right edge of anchor.', anchorRect.left + anchorRect.width, popupRect.left + popupRect.width); assertEquals('Popup should be positioned just below the anchor.', anchorRect.top + anchorRect.height, popupRect.top); } function testPositionAtAnchorOutsideViewport() { var anchor = document.getElementById('anchor4'); var popup = document.getElementById('popup1'); var corner = goog.positioning.Corner; var overflow = goog.positioning.Overflow; var status = goog.positioning.positionAtAnchor(anchor, corner.TOP_LEFT, popup, corner.TOP_RIGHT); var anchorRect = goog.style.getBounds(anchor); var popupRect = goog.style.getBounds(popup); assertTrue('Positioning operation should have been successful.', (status & goog.positioning.OverflowStatus.FAILED) == 0); assertTrue('X position should not have been adjusted.', (status & goog.positioning.OverflowStatus.ADJUSTED_X) == 0); assertTrue('Y position should not have been adjusted.', (status & goog.positioning.OverflowStatus.ADJUSTED_Y) == 0); assertEquals('Right edge of popup should line up with left edge of anchor.', anchorRect.left, popupRect.left + popupRect.width); // Change overflow strategy to fail. status = goog.positioning.positionAtAnchor(anchor, corner.BOTTOM_RIGHT, popup, corner.TOP_RIGHT, undefined, undefined, overflow.FAIL_X); assertFalse('Positioning operation should have failed.', (status & goog.positioning.OverflowStatus.FAILED) == 0); // Change overflow strategy to adjust. status = goog.positioning.positionAtAnchor(anchor, corner.BOTTOM_RIGHT, popup, corner.TOP_RIGHT, undefined, undefined, overflow.ADJUST_X); anchorRect = goog.style.getBounds(anchor); popupRect = goog.style.getBounds(popup); assertTrue('Positioning operation should have been successful.', (status & goog.positioning.OverflowStatus.FAILED) == 0); assertFalse('X position should have been adjusted.', (status & goog.positioning.OverflowStatus.ADJUSTED_X) == 0); assertTrue('Y position should not have been adjusted.', (status & goog.positioning.OverflowStatus.ADJUSTED_Y) == 0); assertEquals('Left edge of popup should line up with left edge of viewport.', goog.style.getPageOffset(popup.parentNode).x, popupRect.left); assertEquals('Popup should be positioned just below the anchor.', anchorRect.top + anchorRect.height, popupRect.top); } function testAdjustForViewportFailIgnore() { var f = goog.positioning.adjustForViewport; var viewport = new goog.math.Box(100, 200, 200, 100); var overflow = goog.positioning.Overflow.IGNORE; var pos = new goog.math.Coordinate(150, 150); var size = new goog.math.Size(50, 50); assertEquals('Viewport overflow should be ignored.', goog.positioning.OverflowStatus.NONE, f(pos, size, viewport, overflow)); pos = new goog.math.Coordinate(150, 150); size = new goog.math.Size(100, 50); assertEquals('Viewport overflow should be ignored.', goog.positioning.OverflowStatus.NONE, f(pos, size, viewport, overflow)); pos = new goog.math.Coordinate(50, 50); size = new goog.math.Size(50, 50); assertEquals('Viewport overflow should be ignored.', goog.positioning.OverflowStatus.NONE, f(pos, size, viewport, overflow)); } function testAdjustForViewportFailXY() { var f = goog.positioning.adjustForViewport; var viewport = new goog.math.Box(100, 200, 200, 100); var overflow = goog.positioning.Overflow.FAIL_X | goog.positioning.Overflow.FAIL_Y; var pos = new goog.math.Coordinate(150, 150); var size = new goog.math.Size(50, 50); assertEquals('Element should not overflow viewport.', goog.positioning.OverflowStatus.NONE, f(pos, size, viewport, overflow)); pos = new goog.math.Coordinate(150, 150); size = new goog.math.Size(100, 50); assertEquals('Element should overflow the right edge of viewport.', goog.positioning.OverflowStatus.FAILED_RIGHT, f(pos, size, viewport, overflow)); pos = new goog.math.Coordinate(150, 150); size = new goog.math.Size(50, 100); assertEquals('Element should overflow the bottom edge of viewport.', goog.positioning.OverflowStatus.FAILED_BOTTOM, f(pos, size, viewport, overflow)); pos = new goog.math.Coordinate(50, 150); size = new goog.math.Size(50, 50); assertEquals('Element should overflow the left edge of viewport.', goog.positioning.OverflowStatus.FAILED_LEFT, f(pos, size, viewport, overflow)); pos = new goog.math.Coordinate(150, 50); size = new goog.math.Size(50, 50); assertEquals('Element should overflow the top edge of viewport.', goog.positioning.OverflowStatus.FAILED_TOP, f(pos, size, viewport, overflow)); pos = new goog.math.Coordinate(50, 50); size = new goog.math.Size(50, 50); assertEquals('Element should overflow the left & top edges of viewport.', goog.positioning.OverflowStatus.FAILED_LEFT | goog.positioning.OverflowStatus.FAILED_TOP, f(pos, size, viewport, overflow)); } function testAdjustForViewportAdjustXFailY() { var f = goog.positioning.adjustForViewport; var viewport = new goog.math.Box(100, 200, 200, 100); var overflow = goog.positioning.Overflow.ADJUST_X | goog.positioning.Overflow.FAIL_Y; var pos = new goog.math.Coordinate(150, 150); var size = new goog.math.Size(50, 50); assertEquals('Element should not overflow viewport.', goog.positioning.OverflowStatus.NONE, f(pos, size, viewport, overflow)); assertEquals('X Position should not have been changed.', 150, pos.x); assertEquals('Y Position should not have been changed.', 150, pos.y); pos = new goog.math.Coordinate(150, 150); size = new goog.math.Size(100, 50); assertEquals('Element position should be adjusted not to overflow right ' + 'edge of viewport.', goog.positioning.OverflowStatus.ADJUSTED_X, f(pos, size, viewport, overflow)); assertEquals('X Position should be adjusted to 100.', 100, pos.x); assertEquals('Y Position should not have been changed.', 150, pos.y); pos = new goog.math.Coordinate(50, 150); size = new goog.math.Size(100, 50); assertEquals('Element position should be adjusted not to overflow left ' + 'edge of viewport.', goog.positioning.OverflowStatus.ADJUSTED_X, f(pos, size, viewport, overflow)); assertEquals('X Position should be adjusted to 100.', 100, pos.x); assertEquals('Y Position should not have been changed.', 150, pos.y); pos = new goog.math.Coordinate(50, 50); size = new goog.math.Size(100, 50); assertEquals('Element position should be adjusted not to overflow left ' + 'edge of viewport, should overflow bottom edge.', goog.positioning.OverflowStatus.ADJUSTED_X | goog.positioning.OverflowStatus.FAILED_TOP, f(pos, size, viewport, overflow)); assertEquals('X Position should be adjusted to 100.', 100, pos.x); assertEquals('Y Position should not have been changed.', 50, pos.y); } function testAdjustForViewportResizeHeight() { var f = goog.positioning.adjustForViewport; var viewport = new goog.math.Box(0, 200, 200, 0); var overflow = goog.positioning.Overflow.RESIZE_HEIGHT; var pos = new goog.math.Coordinate(150, 150); var size = new goog.math.Size(25, 100); assertEquals('Viewport height should be resized.', goog.positioning.OverflowStatus.HEIGHT_ADJUSTED, f(pos, size, viewport, overflow)); assertEquals('Height should be resized to 50.', 50, size.height); var pos = new goog.math.Coordinate(0, 0); var size = new goog.math.Size(50, 250); assertEquals('Viewport height should be resized.', goog.positioning.OverflowStatus.HEIGHT_ADJUSTED, f(pos, size, viewport, overflow)); assertEquals('Height should be resized to 200.', 200, size.height); pos = new goog.math.Coordinate(150, 150); size = new goog.math.Size(50, 50); assertEquals('No Viewport overflow.', goog.positioning.OverflowStatus.NONE, f(pos, size, viewport, overflow)); } function testGetEffectiveCornerLeftToRight() { var f = goog.positioning.getEffectiveCorner; var el = document.getElementById('ltr'); var corner = goog.positioning.Corner; assertEquals('TOP_LEFT should be unchanged for ltr.', corner.TOP_LEFT, f(el, corner.TOP_LEFT)); assertEquals('TOP_RIGHT should be unchanged for ltr.', corner.TOP_RIGHT, f(el, corner.TOP_RIGHT)); assertEquals('BOTTOM_LEFT should be unchanged for ltr.', corner.BOTTOM_LEFT, f(el, corner.BOTTOM_LEFT)); assertEquals('BOTTOM_RIGHT should be unchanged for ltr.', corner.BOTTOM_RIGHT, f(el, corner.BOTTOM_RIGHT)); assertEquals('TOP_START should be TOP_LEFT for ltr.', corner.TOP_LEFT, f(el, corner.TOP_START)); assertEquals('TOP_END should be TOP_RIGHT for ltr.', corner.TOP_RIGHT, f(el, corner.TOP_END)); assertEquals('BOTTOM_START should be BOTTOM_LEFT for ltr.', corner.BOTTOM_LEFT, f(el, corner.BOTTOM_START)); assertEquals('BOTTOM_END should be BOTTOM_RIGHT for ltr.', corner.BOTTOM_RIGHT, f(el, corner.BOTTOM_END)); } function testGetEffectiveCornerRightToLeft() { var f = goog.positioning.getEffectiveCorner; var el = document.getElementById('rtl'); var corner = goog.positioning.Corner; assertEquals('TOP_LEFT should be unchanged for rtl.', corner.TOP_LEFT, f(el, corner.TOP_LEFT)); assertEquals('TOP_RIGHT should be unchanged for rtl.', corner.TOP_RIGHT, f(el, corner.TOP_RIGHT)); assertEquals('BOTTOM_LEFT should be unchanged for rtl.', corner.BOTTOM_LEFT, f(el, corner.BOTTOM_LEFT)); assertEquals('BOTTOM_RIGHT should be unchanged for rtl.', corner.BOTTOM_RIGHT, f(el, corner.BOTTOM_RIGHT)); assertEquals('TOP_START should be TOP_RIGHT for rtl.', corner.TOP_RIGHT, f(el, corner.TOP_START)); assertEquals('TOP_END should be TOP_LEFT for rtl.', corner.TOP_LEFT, f(el, corner.TOP_END)); assertEquals('BOTTOM_START should be BOTTOM_RIGHT for rtl.', corner.BOTTOM_RIGHT, f(el, corner.BOTTOM_START)); assertEquals('BOTTOM_END should be BOTTOM_LEFT for rtl.', corner.BOTTOM_LEFT, f(el, corner.BOTTOM_END)); } function testFlipCornerHorizontal() { var f = goog.positioning.flipCornerHorizontal; var corner = goog.positioning.Corner; assertEquals('TOP_LEFT should be flipped to TOP_RIGHT.', corner.TOP_RIGHT, f(corner.TOP_LEFT)); assertEquals('TOP_RIGHT should be flipped to TOP_LEFT.', corner.TOP_LEFT, f(corner.TOP_RIGHT)); assertEquals('BOTTOM_LEFT should be flipped to BOTTOM_RIGHT.', corner.BOTTOM_RIGHT, f(corner.BOTTOM_LEFT)); assertEquals('BOTTOM_RIGHT should be flipped to BOTTOM_LEFT.', corner.BOTTOM_LEFT, f(corner.BOTTOM_RIGHT)); assertEquals('TOP_START should be flipped to TOP_END.', corner.TOP_END, f(corner.TOP_START)); assertEquals('TOP_END should be flipped to TOP_START.', corner.TOP_START, f(corner.TOP_END)); assertEquals('BOTTOM_START should be flipped to BOTTOM_END.', corner.BOTTOM_END, f(corner.BOTTOM_START)); assertEquals('BOTTOM_END should be flipped to BOTTOM_START.', corner.BOTTOM_START, f(corner.BOTTOM_END)); } function testFlipCornerVertical() { var f = goog.positioning.flipCornerVertical; var corner = goog.positioning.Corner; assertEquals('TOP_LEFT should be flipped to BOTTOM_LEFT.', corner.BOTTOM_LEFT, f(corner.TOP_LEFT)); assertEquals('TOP_RIGHT should be flipped to BOTTOM_RIGHT.', corner.BOTTOM_RIGHT, f(corner.TOP_RIGHT)); assertEquals('BOTTOM_LEFT should be flipped to TOP_LEFT.', corner.TOP_LEFT, f(corner.BOTTOM_LEFT)); assertEquals('BOTTOM_RIGHT should be flipped to TOP_RIGHT.', corner.TOP_RIGHT, f(corner.BOTTOM_RIGHT)); assertEquals('TOP_START should be flipped to BOTTOM_START.', corner.BOTTOM_START, f(corner.TOP_START)); assertEquals('TOP_END should be flipped to BOTTOM_END.', corner.BOTTOM_END, f(corner.TOP_END)); assertEquals('BOTTOM_START should be flipped to TOP_START.', corner.TOP_START, f(corner.BOTTOM_START)); assertEquals('BOTTOM_END should be flipped to TOP_END.', corner.TOP_END, f(corner.BOTTOM_END)); } function testFlipCorner() { var f = goog.positioning.flipCorner; var corner = goog.positioning.Corner; assertEquals('TOP_LEFT should be flipped to BOTTOM_RIGHT.', corner.BOTTOM_RIGHT, f(corner.TOP_LEFT)); assertEquals('TOP_RIGHT should be flipped to BOTTOM_LEFT.', corner.BOTTOM_LEFT, f(corner.TOP_RIGHT)); assertEquals('BOTTOM_LEFT should be flipped to TOP_RIGHT.', corner.TOP_RIGHT, f(corner.BOTTOM_LEFT)); assertEquals('BOTTOM_RIGHT should be flipped to TOP_LEFT.', corner.TOP_LEFT, f(corner.BOTTOM_RIGHT)); assertEquals('TOP_START should be flipped to BOTTOM_END.', corner.BOTTOM_END, f(corner.TOP_START)); assertEquals('TOP_END should be flipped to BOTTOM_START.', corner.BOTTOM_START, f(corner.TOP_END)); assertEquals('BOTTOM_START should be flipped to TOP_END.', corner.TOP_END, f(corner.BOTTOM_START)); assertEquals('BOTTOM_END should be flipped to TOP_START.', corner.TOP_START, f(corner.BOTTOM_END)); } function testPositionAtAnchorFrameViewportStandard() { var corner = goog.positioning.Corner; var iframe = document.getElementById('iframe-standard'); var iframeDoc = goog.dom.getFrameContentDocument(iframe); assertTrue(new goog.dom.DomHelper(iframeDoc).isCss1CompatMode()); new goog.dom.DomHelper(iframeDoc).getDocumentScrollElement().scrollTop = 100; var anchor = iframeDoc.getElementById('anchor1'); var popup = document.getElementById('popup6'); var status = goog.positioning.positionAtAnchor( anchor, corner.TOP_RIGHT, popup, corner.BOTTOM_RIGHT); var iframeRect = goog.style.getBounds(iframe); var popupRect = goog.style.getBounds(popup); assertEquals('Status should not have any ADJUSTED and FAILED.', goog.positioning.OverflowStatus.NONE, status); assertRoundedEquals('Popup should be positioned just above the iframe, ' + 'not above the anchor element inside the iframe', iframeRect.top, popupRect.top + popupRect.height); } function testPositionAtAnchorFrameViewportQuirk() { var corner = goog.positioning.Corner; var iframe = document.getElementById('iframe-quirk'); var iframeDoc = goog.dom.getFrameContentDocument(iframe); assertFalse(new goog.dom.DomHelper(iframeDoc).isCss1CompatMode()); window.scrollTo(0, 100); new goog.dom.DomHelper(iframeDoc).getDocumentScrollElement().scrollTop = 100; var anchor = iframeDoc.getElementById('anchor1'); var popup = document.getElementById('popup6'); var status = goog.positioning.positionAtAnchor( anchor, corner.TOP_RIGHT, popup, corner.BOTTOM_RIGHT); var iframeRect = goog.style.getBounds(iframe); var popupRect = goog.style.getBounds(popup); assertEquals('Status should not have any ADJUSTED and FAILED.', goog.positioning.OverflowStatus.NONE, status); assertRoundedEquals('Popup should be positioned just above the iframe, ' + 'not above the anchor element inside the iframe', iframeRect.top, popupRect.top + popupRect.height); } function testPositionAtAnchorFrameViewportWithPopupInScroller() { var corner = goog.positioning.Corner; var iframe = document.getElementById('iframe-standard'); var iframeDoc = goog.dom.getFrameContentDocument(iframe); new goog.dom.DomHelper(iframeDoc).getDocumentScrollElement().scrollTop = 100; var anchor = iframeDoc.getElementById('anchor1'); var popup = document.getElementById('popup7'); popup.offsetParent.scrollTop = 50; var status = goog.positioning.positionAtAnchor( anchor, corner.TOP_RIGHT, popup, corner.BOTTOM_RIGHT); var iframeRect = goog.style.getBounds(iframe); var popupRect = goog.style.getBounds(popup); assertEquals('Status should not have any ADJUSTED and FAILED.', goog.positioning.OverflowStatus.NONE, status); assertRoughlyEquals('Popup should be positioned just above the iframe, ' + 'not above the anchor element inside the iframe', iframeRect.top, popupRect.top + popupRect.height, ALLOWED_OFFSET); } function testPositionAtAnchorNestedFrames() { var corner = goog.positioning.Corner; var outerIframe = document.getElementById('nested-outer'); var outerDoc = goog.dom.getFrameContentDocument(outerIframe); var popup = outerDoc.getElementById('popup1'); var innerIframe = outerDoc.getElementById('inner-frame'); var innerDoc = goog.dom.getFrameContentDocument(innerIframe); var anchor = innerDoc.getElementById('anchor1'); var status = goog.positioning.positionAtAnchor( anchor, corner.TOP_LEFT, popup, corner.BOTTOM_LEFT); assertEquals('Status should not have any ADJUSTED and FAILED.', goog.positioning.OverflowStatus.NONE, status); var innerIframeRect = goog.style.getBounds(innerIframe); var popupRect = goog.style.getBounds(popup); assertRoundedEquals('Top of frame should align with bottom of the popup', innerIframeRect.top, popupRect.top + popupRect.height); // The anchor is scrolled up by 10px. // Popup position should be the same as above. goog.dom.getWindow(innerDoc).scrollTo(0, 10); status = goog.positioning.positionAtAnchor( anchor, corner.TOP_LEFT, popup, corner.BOTTOM_LEFT); assertEquals('Status should not have any ADJUSTED and FAILED.', goog.positioning.OverflowStatus.NONE, status); innerIframeRect = goog.style.getBounds(innerIframe); popupRect = goog.style.getBounds(popup); assertRoundedEquals('Top of frame should align with bottom of the popup', innerIframeRect.top, popupRect.top + popupRect.height); } </script> </body> </html>