| 1 |
// Copyright 2011 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.positioning.MenuAnchoredPositionTest'); |
| 16 |
goog.setTestOnly('goog.positioning.MenuAnchoredPositionTest'); |
| 17 |
|
| 18 |
goog.require('goog.dom'); |
| 19 |
goog.require('goog.dom.TagName'); |
| 20 |
goog.require('goog.positioning.Corner'); |
| 21 |
goog.require('goog.positioning.MenuAnchoredPosition'); |
| 22 |
goog.require('goog.testing.jsunit'); |
| 23 |
|
| 24 |
var offscreenAnchor; |
| 25 |
var onscreenAnchor; |
| 26 |
var customAnchor; |
| 27 |
var menu; |
| 28 |
var corner = goog.positioning.Corner; |
| 29 |
var savedMenuHtml; |
| 30 |
|
| 31 |
function setUp() { |
| 32 |
offscreenAnchor = goog.dom.getElement('offscreen-anchor'); |
| 33 |
onscreenAnchor = goog.dom.getElement('onscreen-anchor'); |
| 34 |
customAnchor = goog.dom.getElement('custom-anchor'); |
| 35 |
customAnchor.style.left = '0'; |
| 36 |
customAnchor.style.top = '0'; |
| 37 |
|
| 38 |
menu = goog.dom.getElement('menu'); |
| 39 |
savedMenuHtml = menu.innerHTML; |
| 40 |
menu.style.left = '20px'; |
| 41 |
menu.style.top = '20px'; |
| 42 |
} |
| 43 |
|
| 44 |
function tearDown() { |
| 45 |
menu.innerHTML = savedMenuHtml; |
| 46 |
} |
| 47 |
|
| 48 |
function testRepositionWithAdjustAndOnscreenAnchor() { |
| 49 |
// Add so many children that it can't possibly fit onscreen. |
| 50 |
for (var i = 0; i < 200; i++) { |
| 51 |
menu.appendChild( |
| 52 |
goog.dom.createDom(goog.dom.TagName.DIV, null, 'New Item ' + i)); |
| 53 |
} |
| 54 |
|
| 55 |
var pos = new goog.positioning.MenuAnchoredPosition( |
| 56 |
onscreenAnchor, corner.TOP_LEFT, true); |
| 57 |
pos.reposition(menu, corner.TOP_LEFT); |
| 58 |
|
| 59 |
var offset = 0; |
| 60 |
assertEquals(offset, menu.offsetTop); |
| 61 |
assertEquals(5, menu.offsetLeft); |
| 62 |
} |
| 63 |
|
| 64 |
function testRepositionWithAdjustAndOffscreenAnchor() { |
| 65 |
// This does not get adjusted because it's too far offscreen. |
| 66 |
var pos = new goog.positioning.MenuAnchoredPosition( |
| 67 |
offscreenAnchor, corner.TOP_LEFT, true); |
| 68 |
pos.reposition(menu, corner.TOP_LEFT); |
| 69 |
|
| 70 |
assertEquals(-1000, menu.offsetTop); |
| 71 |
assertEquals(-1000, menu.offsetLeft); |
| 72 |
} |
| 73 |
|
| 74 |
function testRespositionFailoverEvenWhenResizeHeightIsOn() { |
| 75 |
var pos = new goog.positioning.MenuAnchoredPosition( |
| 76 |
onscreenAnchor, corner.TOP_LEFT, true, true); |
| 77 |
pos.reposition(menu, corner.TOP_RIGHT); |
| 78 |
|
| 79 |
// The menu should not get positioned offscreen. |
| 80 |
assertEquals(5, menu.offsetTop); |
| 81 |
assertEquals(5, menu.offsetLeft); |
| 82 |
} |
| 83 |
|
| 84 |
function testRepositionToBottomLeftWhenBottomFailsAndRightFailsAndResizeOn() { |
| 85 |
var pageSize = goog.dom.getViewportSize(); |
| 86 |
customAnchor.style.left = (pageSize.width - 10) + 'px'; |
| 87 |
|
| 88 |
// Add so many children that it can't possibly fit onscreen. |
| 89 |
for (var i = 0; i < 200; i++) { |
| 90 |
menu.appendChild( |
| 91 |
goog.dom.createDom(goog.dom.TagName.DIV, null, 'New Item ' + i)); |
| 92 |
} |
| 93 |
|
| 94 |
var pos = new goog.positioning.MenuAnchoredPosition( |
| 95 |
customAnchor, corner.TOP_LEFT, true, true); |
| 96 |
pos.reposition(menu, corner.TOP_LEFT); |
| 97 |
assertEquals(menu.offsetLeft + menu.offsetWidth, customAnchor.offsetLeft); |
| 98 |
} |