1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
|
// 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);
}
|