1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387 |
- // Copyright 2008 The Closure Library Authors. All Rights Reserved.
- //
- // Licensed under the Apache License, Version 2.0 (the "License");
- // you may not use this file except in compliance with the License.
- // You may obtain a copy of the License at
- //
- // http://www.apache.org/licenses/LICENSE-2.0
- //
- // Unless required by applicable law or agreed to in writing, software
- // distributed under the License is distributed on an "AS-IS" BASIS,
- // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- // See the License for the specific language governing permissions and
- // limitations under the License.
- /**
- * @fileoverview Unit tests for goog.position.
- *
- * @author eae@google.com (Emil A Eklund)
- */
- /** @suppress {extraProvide} */
- goog.provide('goog.positioningTest');
- goog.require('goog.dom');
- goog.require('goog.dom.DomHelper');
- goog.require('goog.dom.TagName');
- goog.require('goog.labs.userAgent.browser');
- goog.require('goog.math.Box');
- goog.require('goog.math.Coordinate');
- goog.require('goog.math.Size');
- goog.require('goog.positioning');
- goog.require('goog.positioning.Corner');
- goog.require('goog.positioning.Overflow');
- goog.require('goog.positioning.OverflowStatus');
- goog.require('goog.style');
- goog.require('goog.testing.ExpectedFailures');
- goog.require('goog.testing.jsunit');
- goog.require('goog.userAgent');
- goog.require('goog.userAgent.product');
- goog.setTestOnly('goog.positioningTest');
- // Allow positions to be off by one in gecko as it reports scrolling
- // offsets in steps of 2. Otherwise, allow for subpixel difference
- // as seen in IE10+
- var ALLOWED_OFFSET = goog.userAgent.GECKO ? 1 : 0.1;
- // Error bar for positions since some browsers are not super accurate
- // in reporting them.
- var EPSILON = 2;
- var expectedFailures = new goog.testing.ExpectedFailures();
- var corner = goog.positioning.Corner;
- var overflow = goog.positioning.Overflow;
- var testArea;
- function setUp() {
- window.scrollTo(0, 0);
- 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);
- }
- testArea = goog.dom.getElement('test-area');
- }
- function tearDown() {
- expectedFailures.handleTearDown();
- testArea.setAttribute('style', '');
- goog.dom.removeChildren(testArea);
- }
- /**
- * 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.isVersionOrHigher('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');
- // 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);
- // Anchor top center to top center.
- goog.positioning.positionAtAnchor(
- anchor, corner.TOP_CENTER, popup, corner.TOP_CENTER);
- anchorRect = goog.style.getBounds(anchor);
- popupRect = goog.style.getBounds(popup);
- // We use flooring math because we consider 10 and 10.5 to be "equal".
- // IE8 doesn't allow split pixels in positioning and as such this test would
- // fail on it, as Anchor L+W/2 is round while Popup L+W/2 is .5 away.
- assertRoundedEquals(
- 'The center of popup should line up with the center ' +
- 'of anchor.',
- Math.floor(anchorRect.left + anchorRect.width / 2),
- Math.floor(popupRect.left + popupRect.width / 2));
- assertRoundedEquals(
- 'Popup should have the same y position as the anchor.', anchorRect.top,
- popupRect.top);
- // Anchor top center to top left.
- goog.positioning.positionAtAnchor(
- anchor, corner.TOP_CENTER, popup, corner.TOP_LEFT);
- anchorRect = goog.style.getBounds(anchor);
- popupRect = goog.style.getBounds(popup);
- assertRoundedEquals(
- 'Left edge of popup should line up with the center ' +
- 'of anchor.',
- Math.floor(anchorRect.left + anchorRect.width / 2),
- Math.floor(popupRect.left));
- assertRoundedEquals(
- 'Popup should have the same y position as the anchor.', anchorRect.top,
- popupRect.top);
- // Anchor bottom center to top left.
- goog.positioning.positionAtAnchor(
- anchor, corner.BOTTOM_CENTER, popup, corner.TOP_LEFT);
- anchorRect = goog.style.getBounds(anchor);
- popupRect = goog.style.getBounds(popup);
- assertRoundedEquals(
- 'Left edge of popup should line up with the middle ' +
- 'of anchor.',
- Math.floor(anchorRect.left + anchorRect.width / 2),
- Math.floor(popupRect.left));
- assertRoundedEquals(
- 'Popup should be positioned just below the anchor.',
- anchorRect.top + anchorRect.height, popupRect.top);
- // Anchor bottom left to top center.
- goog.positioning.positionAtAnchor(
- anchor, corner.BOTTOM_LEFT, popup, corner.TOP_CENTER);
- anchorRect = goog.style.getBounds(anchor);
- popupRect = goog.style.getBounds(popup);
- assertRoundedEquals(
- 'Left edge of popup should line up with the middle ' +
- 'of anchor.',
- anchorRect.left, Math.floor(popupRect.left + popupRect.width / 2));
- 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');
- // 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, newCoord(-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, newCoord(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 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);
- // Fails in Chrome because of infrastructure issues, temporarily disabled.
- // See b/4274723.
- expectedFailures.expectFailureFor(goog.userAgent.product.CHROME);
- try {
- 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);
- } catch (e) {
- expectedFailures.handleException(e);
- }
- 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');
- // 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);
- var visibleAnchorRect = goog.positioning.getVisiblePart_(anchor);
- assertRoundedEquals(
- 'Right edge of popup should line up with right edge ' +
- 'of anchor.',
- visibleAnchorRect.left + visibleAnchorRect.width,
- popupRect.left + popupRect.width + 2);
- assertRoundedEquals(
- 'Popup should be positioned just below the anchor.',
- visibleAnchorRect.top + visibleAnchorRect.height, popupRect.top - 1);
- }
- function testPositionAtAnchorRightToLeft() {
- if (goog.userAgent.IE && goog.userAgent.isVersionOrHigher('6')) {
- // These tests fails with IE6.
- // TODO(user): Investigate the reason.
- return;
- }
- if (goog.userAgent.product.SAFARI) {
- // TODO(b/20733468): Disabled so we can get the rest of the Closure test
- // suite running in a continuous build. Will investigate later.
- return;
- }
- var anchor = document.getElementById('anchor2');
- var popup = document.getElementById('popup2');
- // 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 testPositionAtAnchorRightToLeftWithScroll() {
- if (goog.userAgent.IE && goog.userAgent.isVersionOrHigher('6')) {
- // These tests fails with IE6.
- // TODO(user): Investigate the reason.
- return;
- }
- if (goog.userAgent.product.SAFARI) {
- // TODO(b/20733468): Disabled so we can get the rest of the Closure test
- // suite running in a continuous build. Will investigate later.
- return;
- }
- var anchor = document.getElementById('anchor8');
- var popup = document.getElementById('popup8');
- // 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);
- // TODO(joeltine): Chrome 47 has issues with RTL scroll positioning. Remove
- // chrome check when
- // https://code.google.com/p/chromium/issues/detail?id=568706 is resolved.
- if (!goog.labs.userAgent.browser.isChrome()) {
- 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);
- var visibleAnchorRect = goog.positioning.getVisiblePart_(anchor);
- var visibleAnchorBox = visibleAnchorRect.toBox();
- // TODO(joeltine): Chrome 47 has issues with RTL scroll positioning. Remove
- // chrome check when
- // https://code.google.com/p/chromium/issues/detail?id=568706 is resolved.
- if (!goog.labs.userAgent.browser.isChrome()) {
- 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.',
- visibleAnchorBox.bottom, popupRect.top);
- }
- function testPositionAtAnchorBodyViewport() {
- var anchor = document.getElementById('anchor1');
- var popup = document.getElementById('popup3');
- // 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);
- assertRoughlyEquals(
- 'Popup should have the same y position as the anchor.', anchorRect.top,
- popupRect.top, 1);
- // 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);
- assertRoughlyEquals(
- 'Popup should be positioned just below the anchor.',
- anchorRect.top + anchorRect.height, popupRect.top, 1);
- // Anchor top right to top left.
- goog.positioning.positionAtAnchor(
- anchor, corner.TOP_LEFT, popup, corner.TOP_RIGHT);
- anchorRect = goog.style.getBounds(anchor);
- popupRect = goog.style.getBounds(popup);
- assertEquals(
- 'Right edge of popup should line up with left edge of anchor.',
- anchorRect.left, popupRect.left + popupRect.width);
- assertRoughlyEquals(
- 'Popup should have the same y position as the anchor.', anchorRect.top,
- popupRect.top, 1);
- }
- function testPositionAtAnchorSpecificViewport() {
- var anchor = document.getElementById('anchor1');
- var popup = document.getElementById('popup3');
- // Anchor top right to top left within outerbox.
- var status = goog.positioning.positionAtAnchor(
- anchor, corner.TOP_LEFT, popup, corner.TOP_RIGHT, undefined, undefined,
- overflow.FAIL_X);
- anchorRect = goog.style.getBounds(anchor);
- 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);
- assertRoughlyEquals(
- 'Popup should have the same y position as the anchor.', anchorRect.top,
- popupRect.top, 1);
- // position again within box1.
- var box = document.getElementById('box1');
- var viewport = goog.style.getBounds(box);
- status = goog.positioning.positionAtAnchor(
- anchor, corner.TOP_LEFT, popup, corner.TOP_RIGHT, undefined, undefined,
- overflow.FAIL_X, undefined, viewport);
- 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, undefined, viewport);
- 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);
- assertRoughlyEquals(
- 'Left edge of popup should line up with left edge of viewport.',
- viewport.left, popupRect.left, EPSILON);
- assertRoughlyEquals(
- 'Popup should have the same y position as the anchor.', anchorRect.top,
- popupRect.top, 1);
- }
- function testPositionAtAnchorOutsideViewport() {
- var anchor = document.getElementById('anchor4');
- var popup = document.getElementById('popup1');
- 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);
- assertRoughlyEquals(
- 'Left edge of popup should line up with left edge of viewport.', 0,
- popupRect.left, EPSILON);
- 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 = newCoord(150, 150);
- var size = newSize(50, 50);
- assertEquals(
- 'Viewport overflow should be ignored.',
- goog.positioning.OverflowStatus.NONE, f(pos, size, viewport, overflow));
- pos = newCoord(150, 150);
- size = newSize(100, 50);
- assertEquals(
- 'Viewport overflow should be ignored.',
- goog.positioning.OverflowStatus.NONE, f(pos, size, viewport, overflow));
- pos = newCoord(50, 50);
- size = newSize(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 = newCoord(150, 150);
- var size = newSize(50, 50);
- assertEquals(
- 'Element should not overflow viewport.',
- goog.positioning.OverflowStatus.NONE, f(pos, size, viewport, overflow));
- pos = newCoord(150, 150);
- size = newSize(100, 50);
- assertEquals(
- 'Element should overflow the right edge of viewport.',
- goog.positioning.OverflowStatus.FAILED_RIGHT,
- f(pos, size, viewport, overflow));
- pos = newCoord(150, 150);
- size = newSize(50, 100);
- assertEquals(
- 'Element should overflow the bottom edge of viewport.',
- goog.positioning.OverflowStatus.FAILED_BOTTOM,
- f(pos, size, viewport, overflow));
- pos = newCoord(50, 150);
- size = newSize(50, 50);
- assertEquals(
- 'Element should overflow the left edge of viewport.',
- goog.positioning.OverflowStatus.FAILED_LEFT,
- f(pos, size, viewport, overflow));
- pos = newCoord(150, 50);
- size = newSize(50, 50);
- assertEquals(
- 'Element should overflow the top edge of viewport.',
- goog.positioning.OverflowStatus.FAILED_TOP,
- f(pos, size, viewport, overflow));
- pos = newCoord(50, 50);
- size = newSize(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 = newCoord(150, 150);
- var size = newSize(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 = newCoord(150, 150);
- size = newSize(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 = newCoord(50, 150);
- size = newSize(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 = newCoord(50, 50);
- size = newSize(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 = newCoord(150, 150);
- var size = newSize(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);
- assertTrue(
- 'Output box is within viewport',
- viewport.contains(
- new goog.math.Box(
- pos.y, pos.x + size.width, pos.y + size.height, pos.x)));
- var pos = newCoord(0, 0);
- var size = newSize(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);
- assertTrue(
- 'Output box is within viewport',
- viewport.contains(
- new goog.math.Box(
- pos.y, pos.x + size.width, pos.y + size.height, pos.x)));
- var pos = newCoord(0, -50);
- var size = newSize(50, 240);
- assertEquals(
- 'Viewport height should be resized.',
- goog.positioning.OverflowStatus.HEIGHT_ADJUSTED,
- f(pos, size, viewport, overflow));
- assertEquals('Height should be resized to 190.', 190, size.height);
- assertTrue(
- 'Output box is within viewport',
- viewport.contains(
- new goog.math.Box(
- pos.y, pos.x + size.width, pos.y + size.height, pos.x)));
- var pos = newCoord(0, -50);
- var size = newSize(50, 300);
- 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);
- assertTrue(
- 'Output box is within viewport',
- viewport.contains(
- new goog.math.Box(
- pos.y, pos.x + size.width, pos.y + size.height, pos.x)));
- pos = newCoord(150, 150);
- size = newSize(50, 50);
- assertEquals(
- 'No Viewport overflow.', goog.positioning.OverflowStatus.NONE,
- f(pos, size, viewport, overflow));
- assertTrue(
- 'Output box is within viewport',
- viewport.contains(
- new goog.math.Box(
- pos.y, pos.x + size.width, pos.y + size.height, pos.x)));
- var offsetViewport = new goog.math.Box(100, 200, 300, 0);
- var pos = newCoord(0, 50);
- var size = newSize(50, 240);
- assertEquals(
- 'Viewport height should be resized.',
- goog.positioning.OverflowStatus.HEIGHT_ADJUSTED,
- f(pos, size, offsetViewport, overflow));
- assertEquals('Height should be resized to 190.', 190, size.height);
- assertTrue(
- 'Output box is within viewport',
- offsetViewport.contains(
- new goog.math.Box(
- pos.y, pos.x + size.width, pos.y + size.height, pos.x)));
- }
- function testAdjustForViewportResizeWidth() {
- var f = goog.positioning.adjustForViewport_;
- var viewport = new goog.math.Box(0, 200, 200, 0);
- var overflow = goog.positioning.Overflow.RESIZE_WIDTH;
- var pos = newCoord(150, 150);
- var size = newSize(100, 25);
- assertEquals(
- 'Viewport width should be resized.',
- goog.positioning.OverflowStatus.WIDTH_ADJUSTED,
- f(pos, size, viewport, overflow));
- assertEquals('Width should be resized to 50.', 50, size.width);
- assertTrue(
- 'Output box is within viewport',
- viewport.contains(
- new goog.math.Box(
- pos.y, pos.x + size.width, pos.y + size.height, pos.x)));
- var pos = newCoord(0, 0);
- var size = newSize(250, 50);
- assertEquals(
- 'Viewport width should be resized.',
- goog.positioning.OverflowStatus.WIDTH_ADJUSTED,
- f(pos, size, viewport, overflow));
- assertEquals('Width should be resized to 200.', 200, size.width);
- assertTrue(
- 'Output box is within viewport',
- viewport.contains(
- new goog.math.Box(
- pos.y, pos.x + size.width, pos.y + size.height, pos.x)));
- var pos = newCoord(-50, 0);
- var size = newSize(240, 50);
- assertEquals(
- 'Viewport width should be resized.',
- goog.positioning.OverflowStatus.WIDTH_ADJUSTED,
- f(pos, size, viewport, overflow));
- assertEquals('Width should be resized to 190.', 190, size.width);
- assertTrue(
- 'Output box is within viewport',
- viewport.contains(
- new goog.math.Box(
- pos.y, pos.x + size.width, pos.y + size.height, pos.x)));
- var pos = newCoord(-50, 0);
- var size = newSize(300, 50);
- assertEquals(
- 'Viewport width should be resized.',
- goog.positioning.OverflowStatus.WIDTH_ADJUSTED,
- f(pos, size, viewport, overflow));
- assertEquals('Width should be resized to 200.', 200, size.width);
- assertTrue(
- 'Output box is within viewport',
- viewport.contains(
- new goog.math.Box(
- pos.y, pos.x + size.width, pos.y + size.height, pos.x)));
- pos = newCoord(150, 150);
- size = newSize(50, 50);
- assertEquals(
- 'No Viewport overflow.', goog.positioning.OverflowStatus.NONE,
- f(pos, size, viewport, overflow));
- assertTrue(
- 'Output box is within viewport',
- viewport.contains(
- new goog.math.Box(
- pos.y, pos.x + size.width, pos.y + size.height, pos.x)));
- var offsetViewport = new goog.math.Box(0, 300, 200, 100);
- var pos = newCoord(50, 0);
- var size = newSize(240, 50);
- assertEquals(
- 'Viewport width should be resized.',
- goog.positioning.OverflowStatus.WIDTH_ADJUSTED,
- f(pos, size, offsetViewport, overflow));
- assertEquals('Width should be resized to 190.', 190, size.width);
- assertTrue(
- 'Output box is within viewport',
- offsetViewport.contains(
- new goog.math.Box(
- pos.y, pos.x + size.width, pos.y + size.height, pos.x)));
- }
- function testPositionAtAnchorWithResizeHeight() {
- var anchor = document.getElementById('anchor9');
- var popup = document.getElementById('popup9');
- var box = document.getElementById('box9');
- var viewport = goog.style.getBounds(box);
- var status = goog.positioning.positionAtAnchor(
- anchor, corner.TOP_START, popup, corner.TOP_START,
- new goog.math.Coordinate(0, -20), null,
- goog.positioning.Overflow.RESIZE_HEIGHT, null, viewport.toBox());
- assertEquals(
- 'Status should be HEIGHT_ADJUSTED.',
- goog.positioning.OverflowStatus.HEIGHT_ADJUSTED, status);
- var TOLERANCE = 0.1;
- // Adjust the viewport to allow some tolerance for subpixel positioning,
- // this is required for this test to pass on IE10,11
- viewport.top -= TOLERANCE;
- viewport.left -= TOLERANCE;
- assertTrue(
- 'Popup ' + goog.style.getBounds(popup) + ' not is within viewport' +
- viewport,
- viewport.contains(goog.style.getBounds(popup)));
- }
- function testPositionAtCoordinateResizeHeight() {
- var f = goog.positioning.positionAtCoordinate;
- var viewport = new goog.math.Box(0, 50, 50, 0);
- var overflow = goog.positioning.Overflow.RESIZE_HEIGHT |
- goog.positioning.Overflow.ADJUST_Y;
- var popup = document.getElementById('popup1');
- var corner = goog.positioning.Corner.BOTTOM_LEFT;
- var pos = newCoord(100, 100);
- assertEquals(
- 'Viewport height should be resized.',
- goog.positioning.OverflowStatus.HEIGHT_ADJUSTED |
- goog.positioning.OverflowStatus.ADJUSTED_Y,
- f(pos, popup, corner, undefined, viewport, overflow));
- var bounds = goog.style.getSize(popup);
- assertEquals(
- 'Height should be resized to the size of the viewport.', 50,
- bounds.height);
- }
- function testGetPositionAtCoordinateResizeHeight() {
- var f = goog.positioning.getPositionAtCoordinate;
- var viewport = new goog.math.Box(0, 50, 50, 0);
- var overflow = goog.positioning.Overflow.RESIZE_HEIGHT |
- goog.positioning.Overflow.ADJUST_Y;
- var popup = document.getElementById('popup1');
- var corner = goog.positioning.Corner.BOTTOM_LEFT;
- var pos = newCoord(100, 100);
- var size = goog.style.getSize(popup);
- var result = f(pos, size, corner, undefined, viewport, overflow);
- assertEquals(
- 'Viewport height should be resized.',
- goog.positioning.OverflowStatus.HEIGHT_ADJUSTED |
- goog.positioning.OverflowStatus.ADJUSTED_Y,
- result.status);
- assertEquals(
- 'Height should be resized to the size of the viewport.', 50,
- result.rect.height);
- }
- function testGetEffectiveCornerLeftToRight() {
- var f = goog.positioning.getEffectiveCorner;
- var el = document.getElementById('ltr');
- 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');
- 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;
- 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;
- 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;
- 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 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 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 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 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);
- }
- function testPositionAtAnchorOffscreen() {
- var offset = 0;
- var anchor = goog.dom.getElement('offscreen-anchor');
- var popup = goog.dom.getElement('popup3');
- goog.positioning.positionAtAnchor(
- anchor, corner.TOP_LEFT, popup, corner.TOP_LEFT, null, null,
- overflow.ADJUST_X | overflow.ADJUST_Y);
- assertObjectEquals(newCoord(offset, offset), goog.style.getPageOffset(popup));
- goog.positioning.positionAtAnchor(
- anchor, corner.TOP_LEFT, popup, corner.TOP_LEFT, null, null,
- overflow.ADJUST_X_EXCEPT_OFFSCREEN | overflow.ADJUST_Y);
- assertObjectEquals(newCoord(-1000, offset), goog.style.getPageOffset(popup));
- goog.positioning.positionAtAnchor(
- anchor, corner.TOP_LEFT, popup, corner.TOP_LEFT, null, null,
- overflow.ADJUST_X | overflow.ADJUST_Y_EXCEPT_OFFSCREEN);
- assertObjectEquals(newCoord(offset, -1000), goog.style.getPageOffset(popup));
- goog.positioning.positionAtAnchor(
- anchor, corner.TOP_LEFT, popup, corner.TOP_LEFT, null, null,
- overflow.ADJUST_X_EXCEPT_OFFSCREEN | overflow.ADJUST_Y_EXCEPT_OFFSCREEN);
- assertObjectEquals(newCoord(-1000, -1000), goog.style.getPageOffset(popup));
- }
- function testPositionAtAnchorWithOverflowScrollOffsetParent() {
- var testAreaOffset = goog.style.getPageOffset(testArea);
- var scrollbarWidth = goog.style.getScrollbarWidth();
- window.scrollTo(testAreaOffset.x, testAreaOffset.y);
- var overflowDiv = goog.dom.createElement(goog.dom.TagName.DIV);
- overflowDiv.style.overflow = 'scroll';
- overflowDiv.style.position = 'relative';
- goog.style.setSize(overflowDiv, 200 /* width */, 100 /* height */);
- var anchor = goog.dom.createElement(goog.dom.TagName.DIV);
- anchor.style.position = 'absolute';
- goog.style.setSize(anchor, 50 /* width */, 50 /* height */);
- goog.style.setPosition(anchor, 300 /* left */, 300 /* top */);
- var popup = createPopupDiv(75 /* width */, 50 /* height */);
- goog.dom.append(testArea, overflowDiv, anchor);
- goog.dom.append(overflowDiv, popup);
- // Popup should always be positioned within the overflowDiv
- goog.style.setPosition(overflowDiv, 0 /* left */, 0 /* top */);
- goog.positioning.positionAtAnchor(
- anchor, corner.TOP_LEFT, popup, corner.TOP_RIGHT, null, null,
- overflow.ADJUST_X | overflow.ADJUST_Y);
- assertObjectRoughlyEquals(
- new goog.math.Coordinate(
- testAreaOffset.x + 200 - 75 - scrollbarWidth,
- testAreaOffset.y + 100 - 50 - scrollbarWidth),
- goog.style.getPageOffset(popup), 1);
- goog.style.setPosition(overflowDiv, 400 /* left */, 0 /* top */);
- goog.positioning.positionAtAnchor(
- anchor, corner.TOP_RIGHT, popup, corner.TOP_LEFT, null, null,
- overflow.ADJUST_X | overflow.ADJUST_Y);
- assertObjectRoughlyEquals(
- new goog.math.Coordinate(
- testAreaOffset.x + 400, testAreaOffset.y + 100 - 50 - scrollbarWidth),
- goog.style.getPageOffset(popup), 1);
- goog.style.setPosition(overflowDiv, 0 /* left */, 400 /* top */);
- goog.positioning.positionAtAnchor(
- anchor, corner.BOTTOM_LEFT, popup, corner.BOTTOM_RIGHT, null, null,
- overflow.ADJUST_X | overflow.ADJUST_Y);
- assertObjectRoughlyEquals(
- new goog.math.Coordinate(
- testAreaOffset.x + 200 - 75 - scrollbarWidth, testAreaOffset.y + 400),
- goog.style.getPageOffset(popup), 1);
- goog.style.setPosition(overflowDiv, 400 /* left */, 400 /* top */);
- goog.positioning.positionAtAnchor(
- anchor, corner.BOTTOM_RIGHT, popup, corner.BOTTOM_LEFT, null, null,
- overflow.ADJUST_X | overflow.ADJUST_Y);
- assertObjectRoughlyEquals(
- new goog.math.Coordinate(testAreaOffset.x + 400, testAreaOffset.y + 400),
- goog.style.getPageOffset(popup), 1);
- // No overflow.
- goog.style.setPosition(overflowDiv, 300 - 50 /* left */, 300 /* top */);
- goog.positioning.positionAtAnchor(
- anchor, corner.TOP_LEFT, popup, corner.TOP_RIGHT, null, null,
- overflow.ADJUST_X | overflow.ADJUST_Y);
- assertObjectRoughlyEquals(
- new goog.math.Coordinate(
- testAreaOffset.x + 300 - 50, testAreaOffset.y + 300),
- goog.style.getPageOffset(popup), 1);
- }
- function testPositionAtAnchorWithOverflowHiddenParent() {
- var testAreaOffset = goog.style.getPageOffset(testArea);
- window.scrollTo(testAreaOffset.x, testAreaOffset.y);
- var overflowDiv = goog.dom.createElement(goog.dom.TagName.DIV);
- overflowDiv.style.overflow = 'hidden';
- overflowDiv.style.position = 'relative';
- goog.style.setSize(overflowDiv, 200 /* width */, 100 /* height */);
- var anchor = goog.dom.createElement(goog.dom.TagName.DIV);
- anchor.style.position = 'absolute';
- goog.style.setSize(anchor, 50 /* width */, 50 /* height */);
- goog.style.setPosition(anchor, 300 /* left */, 300 /* top */);
- var popup = createPopupDiv(75 /* width */, 50 /* height */);
- goog.dom.append(testArea, overflowDiv, anchor);
- goog.dom.append(overflowDiv, popup);
- // Popup should always be positioned within the overflowDiv
- goog.style.setPosition(overflowDiv, 0 /* left */, 0 /* top */);
- goog.positioning.positionAtAnchor(
- anchor, corner.TOP_LEFT, popup, corner.TOP_RIGHT, null, null,
- overflow.ADJUST_X | overflow.ADJUST_Y);
- assertObjectRoughlyEquals(
- new goog.math.Coordinate(
- testAreaOffset.x + 200 - 75, testAreaOffset.y + 100 - 50),
- goog.style.getPageOffset(popup), 1);
- goog.style.setPosition(overflowDiv, 400 /* left */, 0 /* top */);
- goog.positioning.positionAtAnchor(
- anchor, corner.TOP_RIGHT, popup, corner.TOP_LEFT, null, null,
- overflow.ADJUST_X | overflow.ADJUST_Y);
- assertObjectRoughlyEquals(
- new goog.math.Coordinate(
- testAreaOffset.x + 400, testAreaOffset.y + 100 - 50),
- goog.style.getPageOffset(popup), 1);
- goog.style.setPosition(overflowDiv, 0 /* left */, 400 /* top */);
- goog.positioning.positionAtAnchor(
- anchor, corner.BOTTOM_LEFT, popup, corner.BOTTOM_RIGHT, null, null,
- overflow.ADJUST_X | overflow.ADJUST_Y);
- assertObjectRoughlyEquals(
- new goog.math.Coordinate(
- testAreaOffset.x + 200 - 75, testAreaOffset.y + 400),
- goog.style.getPageOffset(popup), 1);
- goog.style.setPosition(overflowDiv, 400 /* left */, 400 /* top */);
- goog.positioning.positionAtAnchor(
- anchor, corner.BOTTOM_RIGHT, popup, corner.BOTTOM_LEFT, null, null,
- overflow.ADJUST_X | overflow.ADJUST_Y);
- assertObjectRoughlyEquals(
- new goog.math.Coordinate(testAreaOffset.x + 400, testAreaOffset.y + 400),
- goog.style.getPageOffset(popup), 1);
- // No overflow.
- goog.style.setPosition(overflowDiv, 300 - 50 /* left */, 300 /* top */);
- goog.positioning.positionAtAnchor(
- anchor, corner.TOP_LEFT, popup, corner.TOP_RIGHT, null, null,
- overflow.ADJUST_X | overflow.ADJUST_Y);
- assertObjectRoughlyEquals(
- new goog.math.Coordinate(
- testAreaOffset.x + 300 - 50, testAreaOffset.y + 300),
- goog.style.getPageOffset(popup), 1);
- }
- function createPopupDiv(width, height) {
- var popupDiv = goog.dom.createElement(goog.dom.TagName.DIV);
- popupDiv.style.position = 'absolute';
- goog.style.setSize(popupDiv, width, height);
- goog.style.setPosition(popupDiv, 0 /* left */, 250 /* top */);
- return popupDiv;
- }
- function newCoord(x, y) {
- return new goog.math.Coordinate(x, y);
- }
- function newSize(w, h) {
- return new goog.math.Size(w, h);
- }
- function newBox(coord, size) {}
|