| 1 |
// Copyright 2012 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 |
/** |
| 16 |
* Tests for {@code goog.positioning.ClientPosition} |
| 17 |
* @author [email protected] (Chris Henry) |
| 18 |
*/ |
| 19 |
|
| 20 |
goog.provide('goog.positioning.clientPositionTest'); |
| 21 |
goog.setTestOnly('goog.positioning.clientPositionTest'); |
| 22 |
|
| 23 |
goog.require('goog.dom'); |
| 24 |
goog.require('goog.dom.TagName'); |
| 25 |
goog.require('goog.positioning.ClientPosition'); |
| 26 |
goog.require('goog.positioning.Corner'); |
| 27 |
goog.require('goog.style'); |
| 28 |
goog.require('goog.testing.jsunit'); |
| 29 |
|
| 30 |
|
| 31 |
/** |
| 32 |
* Prefabricated popup element for convenient. This is created during |
| 33 |
* setUp and is not attached to the document at the beginning of the |
| 34 |
* test. |
| 35 |
* @type {Element} |
| 36 |
*/ |
| 37 |
var popupElement; |
| 38 |
var testArea; |
| 39 |
var POPUP_HEIGHT = 100; |
| 40 |
var POPUP_WIDTH = 150; |
| 41 |
|
| 42 |
|
| 43 |
function setUp() { |
| 44 |
testArea = goog.dom.getElement('test-area'); |
| 45 |
|
| 46 |
// Enlarges the test area to 5000x5000px so that we can be confident |
| 47 |
// that scrolling the document to some small (x,y) value would work. |
| 48 |
goog.style.setSize(testArea, 5000, 5000); |
| 49 |
|
| 50 |
window.scrollTo(0, 0); |
| 51 |
|
| 52 |
popupElement = goog.dom.createDom(goog.dom.TagName.DIV); |
| 53 |
goog.style.setSize(popupElement, POPUP_WIDTH, POPUP_HEIGHT); |
| 54 |
popupElement.style.position = 'absolute'; |
| 55 |
|
| 56 |
// For ease of debugging. |
| 57 |
popupElement.style.background = 'blue'; |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
function tearDown() { |
| 62 |
popupElement = null; |
| 63 |
goog.dom.removeChildren(testArea); |
| 64 |
testArea.setAttribute('style', ''); |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
function testClientPositionWithZeroViewportOffset() { |
| 69 |
goog.dom.appendChild(testArea, popupElement); |
| 70 |
|
| 71 |
var x = 300; |
| 72 |
var y = 200; |
| 73 |
var pos = new goog.positioning.ClientPosition(x, y); |
| 74 |
|
| 75 |
pos.reposition(popupElement, goog.positioning.Corner.TOP_LEFT); |
| 76 |
assertPageOffset(x, y, popupElement); |
| 77 |
|
| 78 |
pos.reposition(popupElement, goog.positioning.Corner.TOP_RIGHT); |
| 79 |
assertPageOffset(x - POPUP_WIDTH, y, popupElement); |
| 80 |
|
| 81 |
pos.reposition(popupElement, goog.positioning.Corner.BOTTOM_LEFT); |
| 82 |
assertPageOffset(x, y - POPUP_HEIGHT, popupElement); |
| 83 |
|
| 84 |
pos.reposition(popupElement, goog.positioning.Corner.BOTTOM_RIGHT); |
| 85 |
assertPageOffset(x - POPUP_WIDTH, y - POPUP_HEIGHT, popupElement); |
| 86 |
} |
| 87 |
|
| 88 |
|
| 89 |
function testClientPositionWithSomeViewportOffset() { |
| 90 |
goog.dom.appendChild(testArea, popupElement); |
| 91 |
|
| 92 |
var x = 300; |
| 93 |
var y = 200; |
| 94 |
var scrollX = 135; |
| 95 |
var scrollY = 270; |
| 96 |
window.scrollTo(scrollX, scrollY); |
| 97 |
|
| 98 |
var pos = new goog.positioning.ClientPosition(x, y); |
| 99 |
pos.reposition(popupElement, goog.positioning.Corner.TOP_LEFT); |
| 100 |
assertPageOffset(scrollX + x, scrollY + y, popupElement); |
| 101 |
} |
| 102 |
|
| 103 |
|
| 104 |
function testClientPositionWithPositionContext() { |
| 105 |
var contextAbsoluteX = 90; |
| 106 |
var contextAbsoluteY = 110; |
| 107 |
var x = 300; |
| 108 |
var y = 200; |
| 109 |
|
| 110 |
var contextElement = |
| 111 |
goog.dom.createDom(goog.dom.TagName.DIV, undefined, popupElement); |
| 112 |
goog.style.setPosition(contextElement, contextAbsoluteX, contextAbsoluteY); |
| 113 |
contextElement.style.position = 'absolute'; |
| 114 |
goog.dom.appendChild(testArea, contextElement); |
| 115 |
|
| 116 |
var pos = new goog.positioning.ClientPosition(x, y); |
| 117 |
pos.reposition(popupElement, goog.positioning.Corner.TOP_LEFT); |
| 118 |
assertPageOffset(x, y, popupElement); |
| 119 |
} |
| 120 |
|
| 121 |
|
| 122 |
function assertPageOffset(expectedX, expectedY, el) { |
| 123 |
var offsetCoordinate = goog.style.getPageOffset(el); |
| 124 |
assertEquals('x-coord page offset is wrong.', expectedX, offsetCoordinate.x); |
| 125 |
assertEquals('y-coord page offset is wrong.', expectedY, offsetCoordinate.y); |
| 126 |
} |