| 1 |
// Copyright 2014 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.style_document_scroll_test'); |
| 16 |
goog.setTestOnly('goog.style.style_document_scroll_test'); |
| 17 |
|
| 18 |
goog.require('goog.dom'); |
| 19 |
goog.require('goog.style'); |
| 20 |
goog.require('goog.testing.jsunit'); |
| 21 |
|
| 22 |
|
| 23 |
var EPSILON = 2; |
| 24 |
var documentScroll; |
| 25 |
|
| 26 |
function setUp() { |
| 27 |
documentScroll = goog.dom.getDocumentScrollElement(); |
| 28 |
documentScroll.scrollTop = 100; |
| 29 |
documentScroll.scrollLeft = 100; |
| 30 |
} |
| 31 |
|
| 32 |
|
| 33 |
function tearDown() { |
| 34 |
documentScroll.style.border = ''; |
| 35 |
documentScroll.style.padding = ''; |
| 36 |
documentScroll.style.margin = ''; |
| 37 |
documentScroll.scrollTop = 0; |
| 38 |
documentScroll.scrollLeft = 0; |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
function testDocumentScrollWithZeroedBodyProperties() { |
| 43 |
assertRoughlyEquals( |
| 44 |
200, |
| 45 |
goog.style.getContainerOffsetToScrollInto(goog.dom.getElement('testEl1')) |
| 46 |
.y, |
| 47 |
EPSILON); |
| 48 |
assertRoughlyEquals( |
| 49 |
300, |
| 50 |
goog.style.getContainerOffsetToScrollInto(goog.dom.getElement('testEl2')) |
| 51 |
.x, |
| 52 |
EPSILON); |
| 53 |
} |
| 54 |
|
| 55 |
|
| 56 |
function testDocumentScrollWithMargin() { |
| 57 |
documentScroll.style.margin = '20px 0 0 30px'; |
| 58 |
assertRoughlyEquals( |
| 59 |
220, |
| 60 |
goog.style.getContainerOffsetToScrollInto(goog.dom.getElement('testEl1')) |
| 61 |
.y, |
| 62 |
EPSILON); |
| 63 |
assertRoughlyEquals( |
| 64 |
330, |
| 65 |
goog.style.getContainerOffsetToScrollInto(goog.dom.getElement('testEl2')) |
| 66 |
.x, |
| 67 |
EPSILON); |
| 68 |
} |
| 69 |
|
| 70 |
|
| 71 |
function testDocumentScrollWithPadding() { |
| 72 |
documentScroll.style.padding = '20px 0 0 30px'; |
| 73 |
assertRoughlyEquals( |
| 74 |
220, |
| 75 |
goog.style.getContainerOffsetToScrollInto(goog.dom.getElement('testEl1')) |
| 76 |
.y, |
| 77 |
EPSILON); |
| 78 |
assertRoughlyEquals( |
| 79 |
330, |
| 80 |
goog.style.getContainerOffsetToScrollInto(goog.dom.getElement('testEl2')) |
| 81 |
.x, |
| 82 |
EPSILON); |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
function testDocumentScrollWithBorder() { |
| 87 |
documentScroll.style.border = '20px solid green'; |
| 88 |
assertRoughlyEquals( |
| 89 |
220, |
| 90 |
goog.style.getContainerOffsetToScrollInto(goog.dom.getElement('testEl1')) |
| 91 |
.y, |
| 92 |
EPSILON); |
| 93 |
assertRoughlyEquals( |
| 94 |
320, |
| 95 |
goog.style.getContainerOffsetToScrollInto(goog.dom.getElement('testEl2')) |
| 96 |
.x, |
| 97 |
EPSILON); |
| 98 |
} |
| 99 |
|
| 100 |
|
| 101 |
function testDocumentScrollWithAllProperties() { |
| 102 |
documentScroll.style.margin = '20px 0 0 30px'; |
| 103 |
documentScroll.style.padding = '40px 0 0 50px'; |
| 104 |
documentScroll.style.border = '10px solid green'; |
| 105 |
assertRoughlyEquals( |
| 106 |
270, |
| 107 |
goog.style.getContainerOffsetToScrollInto(goog.dom.getElement('testEl1')) |
| 108 |
.y, |
| 109 |
EPSILON); |
| 110 |
assertRoughlyEquals( |
| 111 |
390, |
| 112 |
goog.style.getContainerOffsetToScrollInto(goog.dom.getElement('testEl2')) |
| 113 |
.x, |
| 114 |
EPSILON); |
| 115 |
} |
| 116 |
|
| 117 |
|
| 118 |
function testDocumentScrollNoOpIfElementAlreadyInView() { |
| 119 |
// Scroll once to make testEl3 visible. |
| 120 |
documentScroll.scrollTop = |
| 121 |
goog.style.getContainerOffsetToScrollInto(goog.dom.getElement('testEl3')) |
| 122 |
.y; |
| 123 |
|
| 124 |
// Scroll a bit more so that now the element is approximately at the middle. |
| 125 |
var viewportHeight = documentScroll.clientHeight; |
| 126 |
documentScroll.scrollTop += viewportHeight / 2; |
| 127 |
|
| 128 |
// Since the element is fully within viewport, additional calls to |
| 129 |
// getContainerOffsetToScrollInto should be a no-op. |
| 130 |
assertEquals( |
| 131 |
documentScroll.scrollTop, |
| 132 |
goog.style.getContainerOffsetToScrollInto(goog.dom.getElement('testEl3')) |
| 133 |
.y); |
| 134 |
} |
| 135 |
|
| 136 |
|
| 137 |
function testScrollIntoContainerView() { |
| 138 |
goog.style.scrollIntoContainerView(goog.dom.getElement('testEl1')); |
| 139 |
assertRoughlyEquals(200, documentScroll.scrollTop, EPSILON); |
| 140 |
goog.style.scrollIntoContainerView(goog.dom.getElement('testEl2')); |
| 141 |
assertRoughlyEquals(300, documentScroll.scrollLeft, EPSILON); |
| 142 |
} |