123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172 |
- // Copyright 2009 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.
- goog.provide('goog.positioning.ViewportClientPositionTest');
- goog.setTestOnly('goog.positioning.ViewportClientPositionTest');
- goog.require('goog.dom');
- goog.require('goog.positioning.Corner');
- goog.require('goog.positioning.Overflow');
- goog.require('goog.positioning.ViewportClientPosition');
- goog.require('goog.style');
- goog.require('goog.testing.jsunit');
- goog.require('goog.userAgent');
- var viewportSize, anchor, popup, dom, frameRect;
- var corner = goog.positioning.Corner;
- // 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 setUp() {
- var frame = document.getElementById('frame1');
- var doc = goog.dom.getFrameContentDocument(frame);
- dom = goog.dom.getDomHelper(doc);
- viewportSize = dom.getViewportSize();
- anchor = dom.getElement('anchor');
- popup = dom.getElement('popup');
- popup.style.overflowY = 'visible';
- goog.style.setSize(popup, 20, 20);
- frameRect = goog.style.getVisibleRectForElement(doc.body);
- }
- function testPositionAtCoordinateTopLeft() {
- var pos = new goog.positioning.ViewportClientPosition(100, 100);
- pos.reposition(popup, corner.TOP_LEFT);
- var offset = goog.style.getPageOffset(popup);
- assertEquals(
- 'Left edge of popup should be at specified x coordinate.', 100, offset.x);
- assertEquals(
- 'Top edge of popup should be at specified y coordinate.', 100, offset.y);
- }
- function testPositionAtCoordinateBottomRight() {
- var pos = new goog.positioning.ViewportClientPosition(100, 100);
- pos.reposition(popup, corner.BOTTOM_RIGHT);
- var bounds = goog.style.getBounds(popup);
- assertEquals(
- 'Right edge of popup should be at specified x coordinate.', 100,
- bounds.left + bounds.width);
- assertEquals(
- 'Bottom edge of popup should be at specified x coordinate.', 100,
- bounds.top + bounds.height);
- }
- function testPositionAtCoordinateTopLeftWithScroll() {
- dom.getDocument().body.style.paddingTop = '300px';
- dom.getDocument().body.style.height = '3000px';
- dom.getDocumentScrollElement().scrollTop = 50;
- dom.getDocument().body.scrollTop = 50;
- var pos = new goog.positioning.ViewportClientPosition(0, 0);
- pos.reposition(popup, corner.TOP_LEFT);
- var offset = goog.style.getPageOffset(popup);
- assertEquals(
- 'Left edge of popup should be at specified x coordinate.', 0, offset.x);
- assertTrue(
- 'Top edge of popup should be at specified y coordinate ' +
- 'adjusted for scroll.',
- Math.abs(offset.y - 50) <= ALLOWED_OFFSET);
- dom.getDocument().body.style.paddingLeft = '1000px';
- dom.getDocumentScrollElement().scrollLeft = 500;
- pos.reposition(popup, corner.TOP_LEFT);
- offset = goog.style.getPageOffset(popup);
- assertTrue(
- 'Left edge of popup should be at specified x coordinate ' +
- 'adjusted for scroll.',
- Math.abs(offset.x - 500) <= ALLOWED_OFFSET);
- dom.getDocumentScrollElement().scrollLeft = 0;
- dom.getDocumentScrollElement().scrollTop = 0;
- dom.getDocument().body.style.paddingLeft = '';
- dom.getDocument().body.style.paddingTop = '';
- pos.reposition(popup, corner.TOP_LEFT);
- offset = goog.style.getPageOffset(popup);
- assertEquals(
- 'Left edge of popup should be at specified x coordinate.', 0, offset.x);
- assertEquals(
- 'Top edge of popup should be at specified y coordinate.', 0, offset.y);
- }
- function testOverflowRightFlipHor() {
- var pos = new goog.positioning.ViewportClientPosition(frameRect.right, 100);
- pos.reposition(popup, corner.TOP_LEFT);
- var offset = goog.style.getPageOffset(popup);
- assertEquals(
- 'Left edge of popup should have been adjusted so that it ' +
- 'fits inside the viewport.',
- frameRect.right - popup.offsetWidth, offset.x);
- assertEquals(
- 'Top edge of popup should be at specified y coordinate.', 100, offset.y);
- }
- function testOverflowTopFlipVer() {
- var pos = new goog.positioning.ViewportClientPosition(100, 0);
- pos.reposition(popup, corner.TOP_RIGHT);
- var offset = goog.style.getPageOffset(popup);
- assertEquals(
- 'Left edge of popup should be at specified x coordinate.', 80, offset.x);
- assertEquals(
- 'Top edge of popup should have been adjusted so that it ' +
- 'fits inside the viewport.',
- 0, offset.y);
- }
- function testOverflowBottomRightFlipBoth() {
- var pos = new goog.positioning.ViewportClientPosition(
- frameRect.right, frameRect.bottom);
- pos.reposition(popup, corner.TOP_LEFT);
- var offset = goog.style.getPageOffset(popup);
- assertEquals(
- 'Left edge of popup should have been adjusted so that it ' +
- 'fits inside the viewport.',
- frameRect.right - popup.offsetWidth, offset.x);
- assertEquals(
- 'Top edge of popup should have been adjusted so that it ' +
- 'fits inside the viewport.',
- frameRect.bottom - popup.offsetHeight, offset.y);
- }
- function testLastRespotOverflow() {
- var large = 2000;
- goog.style.setSize(popup, 20, large);
- popup.style.overflowY = 'auto';
- var pos = new goog.positioning.ViewportClientPosition(0, 0);
- pos.reposition(popup, corner.TOP_LEFT);
- assertEquals(large, popup.offsetHeight);
- pos.setLastResortOverflow(goog.positioning.Overflow.RESIZE_HEIGHT);
- pos.reposition(popup, corner.TOP_LEFT);
- assertNotEquals(large, popup.offsetHeight);
- }
|