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
|
// Copyright 2012 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.style.bidiTest');
goog.setTestOnly('goog.style.bidiTest');
goog.require('goog.dom');
goog.require('goog.style');
goog.require('goog.style.bidi');
goog.require('goog.testing.jsunit');
goog.require('goog.userAgent');
// Updates the calculated metrics.
function updateInfo() {
var element = document.getElementById('scrolledElementRtl');
document.getElementById('elementScrollLeftRtl').innerHTML =
element.offsetParent.scrollLeft;
document.getElementById('bidiOffsetStartRtl').innerHTML =
goog.style.bidi.getOffsetStart(element);
document.getElementById('bidiScrollLeftRtl').innerHTML =
goog.style.bidi.getScrollLeft(element.offsetParent);
element = document.getElementById('scrolledElementLtr');
document.getElementById('elementScrollLeftLtr').innerHTML =
element.offsetParent.scrollLeft;
document.getElementById('bidiOffsetStartLtr').innerHTML =
goog.style.bidi.getOffsetStart(element);
document.getElementById('bidiScrollLeftLtr').innerHTML =
goog.style.bidi.getScrollLeft(element.offsetParent);
}
function setUpPage() {
updateInfo();
}
function tearDown() {
document.documentElement.dir = 'ltr';
document.body.dir = 'ltr';
}
function testGetOffsetStart() {
var elm = document.getElementById('scrolledElementRtl');
assertEquals(elm.style['right'], goog.style.bidi.getOffsetStart(elm) + 'px');
elm = document.getElementById('scrolledElementLtr');
assertEquals(elm.style['left'], goog.style.bidi.getOffsetStart(elm) + 'px');
}
function testSetScrollOffsetRtl() {
var scrollElm = document.getElementById('scrollDivRtl');
var scrolledElm = document.getElementById('scrolledElementRtl');
var originalDistance =
goog.style.getRelativePosition(scrolledElm, document.body).x;
var scrollAndAssert = function(pixels) {
goog.style.bidi.setScrollOffset(scrollElm, pixels);
assertEquals(
originalDistance + pixels,
goog.style.getRelativePosition(scrolledElm, document.body).x);
};
scrollAndAssert(0);
scrollAndAssert(50);
scrollAndAssert(100);
scrollAndAssert(150);
scrollAndAssert(155);
scrollAndAssert(0);
}
function testSetScrollOffsetLtr() {
var scrollElm = document.getElementById('scrollDivLtr');
var scrolledElm = document.getElementById('scrolledElementLtr');
var originalDistance =
goog.style.getRelativePosition(scrolledElm, document.body).x;
var scrollAndAssert = function(pixels) {
goog.style.bidi.setScrollOffset(scrollElm, pixels);
assertEquals(
originalDistance - pixels,
goog.style.getRelativePosition(scrolledElm, document.body).x);
};
scrollAndAssert(0);
scrollAndAssert(50);
scrollAndAssert(100);
scrollAndAssert(150);
scrollAndAssert(155);
scrollAndAssert(0);
}
function testFixedBodyChildLtr() {
var bodyChild = document.getElementById('bodyChild');
assertEquals(
goog.userAgent.GECKO ? document.body : null, bodyChild.offsetParent);
assertEquals(60, goog.style.bidi.getOffsetStart(bodyChild));
}
function testFixedBodyChildRtl() {
document.documentElement.dir = 'rtl';
document.body.dir = 'rtl';
var bodyChild = document.getElementById('bodyChild');
assertEquals(
goog.userAgent.GECKO ? document.body : null, bodyChild.offsetParent);
var expectedOffsetStart =
goog.dom.getViewportSize().width - 60 - bodyChild.offsetWidth;
// Gecko seems to also add in the marginbox for the body.
// It's not really clear to me if this is true in the general case,
// or just under certain conditions.
if (goog.userAgent.GECKO) {
var marginBox = goog.style.getMarginBox(document.body);
expectedOffsetStart -= (marginBox.left + marginBox.right);
}
assertEquals(expectedOffsetStart, goog.style.bidi.getOffsetStart(bodyChild));
}
function testGetScrollLeftRTL() {
var scrollLeftDiv = document.getElementById('scrollLeftRtl');
scrollLeftDiv.style.overflow = 'visible';
assertEquals(0, goog.style.bidi.getScrollLeft(scrollLeftDiv));
scrollLeftDiv.style.overflow = 'hidden';
assertEquals(0, goog.style.bidi.getScrollLeft(scrollLeftDiv));
// NOTE: 'auto' must go above the 'scroll' assertion. Chrome 47 has a bug
// with non-deterministic scroll positioning. Maybe it recalculates the
// layout on accessing those properties?
// TODO(joeltine): Remove this comment when
// https://code.google.com/p/chromium/issues/detail?id=568706 is resolved.
scrollLeftDiv.style.overflow = 'auto';
assertEquals(0, goog.style.bidi.getScrollLeft(scrollLeftDiv));
scrollLeftDiv.style.overflow = 'scroll';
assertEquals(0, goog.style.bidi.getScrollLeft(scrollLeftDiv));
}
|