style_document_scroll_test.js 4.1 KB

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