| 1 |
// Copyright 2008 The Closure Library Authors. All Rights Reserved. |
| 2 |
// |
| 3 |
// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 |
// you may not use this file except in compliance with the License. |
| 5 |
// You may obtain a copy of the License at |
| 6 |
// |
| 7 |
// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 |
// |
| 9 |
// Unless required by applicable law or agreed to in writing, software |
| 10 |
// distributed under the License is distributed on an "AS-IS" BASIS, |
| 11 |
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 |
// See the License for the specific language governing permissions and |
| 13 |
// limitations under the License. |
| 14 |
|
| 15 |
goog.provide('goog.ui.PopupTest'); |
| 16 |
goog.setTestOnly('goog.ui.PopupTest'); |
| 17 |
|
| 18 |
goog.require('goog.positioning.AnchoredPosition'); |
| 19 |
goog.require('goog.positioning.Corner'); |
| 20 |
goog.require('goog.style'); |
| 21 |
goog.require('goog.testing.jsunit'); |
| 22 |
goog.require('goog.ui.Popup'); |
| 23 |
goog.require('goog.userAgent'); |
| 24 |
|
| 25 |
|
| 26 |
/** |
| 27 |
* This is used to round pixel values on FF3 Mac. |
| 28 |
*/ |
| 29 |
function assertRoundedEquals(a, b, c) { |
| 30 |
function round(x) { |
| 31 |
return goog.userAgent.GECKO && (goog.userAgent.MAC || goog.userAgent.X11) && |
| 32 |
goog.userAgent.isVersionOrHigher('1.9') ? |
| 33 |
Math.round(x) : |
| 34 |
x; |
| 35 |
} |
| 36 |
if (arguments.length == 3) { |
| 37 |
assertEquals(a, round(b), round(c)); |
| 38 |
} else { |
| 39 |
assertEquals(round(a), round(b)); |
| 40 |
} |
| 41 |
} |
| 42 |
|
| 43 |
function testCreateAndReposition() { |
| 44 |
var anchorEl = document.getElementById('anchor'); |
| 45 |
var popupEl = document.getElementById('popup'); |
| 46 |
var corner = goog.positioning.Corner; |
| 47 |
|
| 48 |
var pos = |
| 49 |
new goog.positioning.AnchoredPosition(anchorEl, corner.BOTTOM_START); |
| 50 |
var popup = new goog.ui.Popup(popupEl, pos); |
| 51 |
popup.setVisible(true); |
| 52 |
|
| 53 |
var anchorRect = goog.style.getBounds(anchorEl); |
| 54 |
var popupRect = goog.style.getBounds(popupEl); |
| 55 |
assertRoundedEquals( |
| 56 |
'Left edge of popup should line up with left edge ' + |
| 57 |
'of anchor.', |
| 58 |
anchorRect.left, popupRect.left); |
| 59 |
assertRoundedEquals( |
| 60 |
'Popup should be positioned just below the anchor.', |
| 61 |
anchorRect.top + anchorRect.height, popupRect.top); |
| 62 |
|
| 63 |
// Reposition. |
| 64 |
anchorEl.style.marginTop = '7px'; |
| 65 |
popup.reposition(); |
| 66 |
|
| 67 |
anchorRect = goog.style.getBounds(anchorEl); |
| 68 |
popupRect = goog.style.getBounds(popupEl); |
| 69 |
assertRoundedEquals( |
| 70 |
'Popup should be positioned just below the anchor.', |
| 71 |
anchorRect.top + anchorRect.height, popupRect.top); |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
function testSetPinnedCorner() { |
| 76 |
var anchorEl = document.getElementById('anchor'); |
| 77 |
var popupEl = document.getElementById('popup'); |
| 78 |
var corner = goog.positioning.Corner; |
| 79 |
|
| 80 |
var pos = |
| 81 |
new goog.positioning.AnchoredPosition(anchorEl, corner.BOTTOM_START); |
| 82 |
var popup = new goog.ui.Popup(popupEl, pos); |
| 83 |
popup.setVisible(true); |
| 84 |
|
| 85 |
var anchorRect = goog.style.getBounds(anchorEl); |
| 86 |
var popupRect = goog.style.getBounds(popupEl); |
| 87 |
assertRoundedEquals( |
| 88 |
'Left edge of popup should line up with left edge ' + |
| 89 |
'of anchor.', |
| 90 |
anchorRect.left, popupRect.left); |
| 91 |
assertRoundedEquals( |
| 92 |
'Popup should be positioned just below the anchor.', |
| 93 |
anchorRect.top + anchorRect.height, popupRect.top); |
| 94 |
|
| 95 |
// Change pinned corner. |
| 96 |
popup.setPinnedCorner(corner.BOTTOM_END); |
| 97 |
|
| 98 |
anchorRect = goog.style.getBounds(anchorEl); |
| 99 |
popupRect = goog.style.getBounds(popupEl); |
| 100 |
assertRoundedEquals( |
| 101 |
'Right edge of popup should line up with left edge ' + |
| 102 |
'of anchor.', |
| 103 |
anchorRect.left, popupRect.left + popupRect.width); |
| 104 |
assertRoundedEquals( |
| 105 |
'Bottom edge of popup should line up with bottom ' + |
| 106 |
'of anchor.', |
| 107 |
anchorRect.top + anchorRect.height, popupRect.top + popupRect.height); |
| 108 |
|
| 109 |
// Position outside the viewport. |
| 110 |
anchorEl.style.marginLeft = '0'; |
| 111 |
popup.reposition(); |
| 112 |
|
| 113 |
anchorRect = goog.style.getBounds(anchorEl); |
| 114 |
popupRect = goog.style.getBounds(popupEl); |
| 115 |
|
| 116 |
assertRoundedEquals( |
| 117 |
'Right edge of popup should line up with left edge ' + |
| 118 |
'of anchor.', |
| 119 |
anchorRect.left, popupRect.left + popupRect.width); |
| 120 |
|
| 121 |
anchorEl.style.marginLeft = ''; |
| 122 |
} |