| 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 |
/** |
| 16 |
* @fileoverview Shared unit tests for scrollbar measurement. |
| 17 |
* |
| 18 |
* @author [email protected] (Ian Flanigan) |
| 19 |
*/ |
| 20 |
|
| 21 |
goog.provide('goog.styleScrollbarTester'); |
| 22 |
|
| 23 |
goog.require('goog.dom'); |
| 24 |
goog.require('goog.dom.TagName'); |
| 25 |
goog.require('goog.style'); |
| 26 |
goog.require('goog.testing.asserts'); |
| 27 |
goog.setTestOnly('goog.styleScrollbarTester'); |
| 28 |
|
| 29 |
|
| 30 |
/** |
| 31 |
* Tests the scrollbar width calculation. Assumes that there is an element with |
| 32 |
* id 'test-scrollbarwidth' in the page. |
| 33 |
*/ |
| 34 |
function testScrollbarWidth() { |
| 35 |
var width = goog.style.getScrollbarWidth(); |
| 36 |
assertTrue(width > 0); |
| 37 |
|
| 38 |
var outer = goog.dom.getElement('test-scrollbarwidth'); |
| 39 |
var inner = goog.dom.getElementsByTagNameAndClass( |
| 40 |
goog.dom.TagName.DIV, null, outer)[0]; |
| 41 |
assertTrue('should have a scroll bar', hasVerticalScroll(outer)); |
| 42 |
assertTrue('should have a scroll bar', hasHorizontalScroll(outer)); |
| 43 |
|
| 44 |
// Get the inner div absolute width |
| 45 |
goog.style.setStyle(outer, 'width', '100%'); |
| 46 |
assertTrue('should have a scroll bar', hasVerticalScroll(outer)); |
| 47 |
assertFalse('should not have a scroll bar', hasHorizontalScroll(outer)); |
| 48 |
var innerAbsoluteWidth = inner.offsetWidth; |
| 49 |
|
| 50 |
// Leave the vertical scroll and remove the horizontal by using the scroll |
| 51 |
// bar width calculation. |
| 52 |
goog.style.setStyle(outer, 'width', (innerAbsoluteWidth + width) + 'px'); |
| 53 |
assertTrue('should have a scroll bar', hasVerticalScroll(outer)); |
| 54 |
assertFalse('should not have a scroll bar', hasHorizontalScroll(outer)); |
| 55 |
|
| 56 |
// verify by adding 1 more pixel (brings back the vertical scroll bar). |
| 57 |
goog.style.setStyle(outer, 'width', (innerAbsoluteWidth + width - 1) + 'px'); |
| 58 |
assertTrue('should have a scroll bar', hasVerticalScroll(outer)); |
| 59 |
assertTrue('should have a scroll bar', hasHorizontalScroll(outer)); |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
function hasVerticalScroll(el) { |
| 64 |
return el.clientWidth != 0 && el.offsetWidth - el.clientWidth > 0; |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
function hasHorizontalScroll(el) { |
| 69 |
return el.clientHeight != 0 && el.offsetHeight - el.clientHeight > 0; |
| 70 |
} |