stylescrollbartester.js 2.5 KB

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