| 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 |
goog.provide('goog.style.bidiTest'); |
| 16 |
goog.setTestOnly('goog.style.bidiTest'); |
| 17 |
|
| 18 |
goog.require('goog.dom'); |
| 19 |
goog.require('goog.style'); |
| 20 |
goog.require('goog.style.bidi'); |
| 21 |
goog.require('goog.testing.jsunit'); |
| 22 |
goog.require('goog.userAgent'); |
| 23 |
|
| 24 |
// Updates the calculated metrics. |
| 25 |
function updateInfo() { |
| 26 |
var element = document.getElementById('scrolledElementRtl'); |
| 27 |
document.getElementById('elementScrollLeftRtl').innerHTML = |
| 28 |
element.offsetParent.scrollLeft; |
| 29 |
document.getElementById('bidiOffsetStartRtl').innerHTML = |
| 30 |
goog.style.bidi.getOffsetStart(element); |
| 31 |
document.getElementById('bidiScrollLeftRtl').innerHTML = |
| 32 |
goog.style.bidi.getScrollLeft(element.offsetParent); |
| 33 |
|
| 34 |
element = document.getElementById('scrolledElementLtr'); |
| 35 |
document.getElementById('elementScrollLeftLtr').innerHTML = |
| 36 |
element.offsetParent.scrollLeft; |
| 37 |
document.getElementById('bidiOffsetStartLtr').innerHTML = |
| 38 |
goog.style.bidi.getOffsetStart(element); |
| 39 |
document.getElementById('bidiScrollLeftLtr').innerHTML = |
| 40 |
goog.style.bidi.getScrollLeft(element.offsetParent); |
| 41 |
} |
| 42 |
|
| 43 |
function setUpPage() { |
| 44 |
updateInfo(); |
| 45 |
} |
| 46 |
|
| 47 |
function tearDown() { |
| 48 |
document.documentElement.dir = 'ltr'; |
| 49 |
document.body.dir = 'ltr'; |
| 50 |
} |
| 51 |
|
| 52 |
function testGetOffsetStart() { |
| 53 |
var elm = document.getElementById('scrolledElementRtl'); |
| 54 |
assertEquals(elm.style['right'], goog.style.bidi.getOffsetStart(elm) + 'px'); |
| 55 |
elm = document.getElementById('scrolledElementLtr'); |
| 56 |
assertEquals(elm.style['left'], goog.style.bidi.getOffsetStart(elm) + 'px'); |
| 57 |
} |
| 58 |
|
| 59 |
function testSetScrollOffsetRtl() { |
| 60 |
var scrollElm = document.getElementById('scrollDivRtl'); |
| 61 |
var scrolledElm = document.getElementById('scrolledElementRtl'); |
| 62 |
var originalDistance = |
| 63 |
goog.style.getRelativePosition(scrolledElm, document.body).x; |
| 64 |
var scrollAndAssert = function(pixels) { |
| 65 |
goog.style.bidi.setScrollOffset(scrollElm, pixels); |
| 66 |
assertEquals( |
| 67 |
originalDistance + pixels, |
| 68 |
goog.style.getRelativePosition(scrolledElm, document.body).x); |
| 69 |
}; |
| 70 |
scrollAndAssert(0); |
| 71 |
scrollAndAssert(50); |
| 72 |
scrollAndAssert(100); |
| 73 |
scrollAndAssert(150); |
| 74 |
scrollAndAssert(155); |
| 75 |
scrollAndAssert(0); |
| 76 |
} |
| 77 |
|
| 78 |
function testSetScrollOffsetLtr() { |
| 79 |
var scrollElm = document.getElementById('scrollDivLtr'); |
| 80 |
var scrolledElm = document.getElementById('scrolledElementLtr'); |
| 81 |
var originalDistance = |
| 82 |
goog.style.getRelativePosition(scrolledElm, document.body).x; |
| 83 |
var scrollAndAssert = function(pixels) { |
| 84 |
goog.style.bidi.setScrollOffset(scrollElm, pixels); |
| 85 |
assertEquals( |
| 86 |
originalDistance - pixels, |
| 87 |
goog.style.getRelativePosition(scrolledElm, document.body).x); |
| 88 |
}; |
| 89 |
scrollAndAssert(0); |
| 90 |
scrollAndAssert(50); |
| 91 |
scrollAndAssert(100); |
| 92 |
scrollAndAssert(150); |
| 93 |
scrollAndAssert(155); |
| 94 |
scrollAndAssert(0); |
| 95 |
} |
| 96 |
|
| 97 |
function testFixedBodyChildLtr() { |
| 98 |
var bodyChild = document.getElementById('bodyChild'); |
| 99 |
assertEquals( |
| 100 |
goog.userAgent.GECKO ? document.body : null, bodyChild.offsetParent); |
| 101 |
assertEquals(60, goog.style.bidi.getOffsetStart(bodyChild)); |
| 102 |
} |
| 103 |
|
| 104 |
function testFixedBodyChildRtl() { |
| 105 |
document.documentElement.dir = 'rtl'; |
| 106 |
document.body.dir = 'rtl'; |
| 107 |
|
| 108 |
var bodyChild = document.getElementById('bodyChild'); |
| 109 |
assertEquals( |
| 110 |
goog.userAgent.GECKO ? document.body : null, bodyChild.offsetParent); |
| 111 |
|
| 112 |
var expectedOffsetStart = |
| 113 |
goog.dom.getViewportSize().width - 60 - bodyChild.offsetWidth; |
| 114 |
|
| 115 |
// Gecko seems to also add in the marginbox for the body. |
| 116 |
// It's not really clear to me if this is true in the general case, |
| 117 |
// or just under certain conditions. |
| 118 |
if (goog.userAgent.GECKO) { |
| 119 |
var marginBox = goog.style.getMarginBox(document.body); |
| 120 |
expectedOffsetStart -= (marginBox.left + marginBox.right); |
| 121 |
} |
| 122 |
|
| 123 |
assertEquals(expectedOffsetStart, goog.style.bidi.getOffsetStart(bodyChild)); |
| 124 |
} |
| 125 |
|
| 126 |
function testGetScrollLeftRTL() { |
| 127 |
var scrollLeftDiv = document.getElementById('scrollLeftRtl'); |
| 128 |
scrollLeftDiv.style.overflow = 'visible'; |
| 129 |
assertEquals(0, goog.style.bidi.getScrollLeft(scrollLeftDiv)); |
| 130 |
scrollLeftDiv.style.overflow = 'hidden'; |
| 131 |
assertEquals(0, goog.style.bidi.getScrollLeft(scrollLeftDiv)); |
| 132 |
// NOTE: 'auto' must go above the 'scroll' assertion. Chrome 47 has a bug |
| 133 |
// with non-deterministic scroll positioning. Maybe it recalculates the |
| 134 |
// layout on accessing those properties? |
| 135 |
// TODO(joeltine): Remove this comment when |
| 136 |
// https://code.google.com/p/chromium/issues/detail?id=568706 is resolved. |
| 137 |
scrollLeftDiv.style.overflow = 'auto'; |
| 138 |
assertEquals(0, goog.style.bidi.getScrollLeft(scrollLeftDiv)); |
| 139 |
scrollLeftDiv.style.overflow = 'scroll'; |
| 140 |
assertEquals(0, goog.style.bidi.getScrollLeft(scrollLeftDiv)); |
| 141 |
} |