bidi_test.js 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // Copyright 2012 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.bidiTest');
  15. goog.setTestOnly('goog.style.bidiTest');
  16. goog.require('goog.dom');
  17. goog.require('goog.style');
  18. goog.require('goog.style.bidi');
  19. goog.require('goog.testing.jsunit');
  20. goog.require('goog.userAgent');
  21. // Updates the calculated metrics.
  22. function updateInfo() {
  23. var element = document.getElementById('scrolledElementRtl');
  24. document.getElementById('elementScrollLeftRtl').innerHTML =
  25. element.offsetParent.scrollLeft;
  26. document.getElementById('bidiOffsetStartRtl').innerHTML =
  27. goog.style.bidi.getOffsetStart(element);
  28. document.getElementById('bidiScrollLeftRtl').innerHTML =
  29. goog.style.bidi.getScrollLeft(element.offsetParent);
  30. element = document.getElementById('scrolledElementLtr');
  31. document.getElementById('elementScrollLeftLtr').innerHTML =
  32. element.offsetParent.scrollLeft;
  33. document.getElementById('bidiOffsetStartLtr').innerHTML =
  34. goog.style.bidi.getOffsetStart(element);
  35. document.getElementById('bidiScrollLeftLtr').innerHTML =
  36. goog.style.bidi.getScrollLeft(element.offsetParent);
  37. }
  38. function setUpPage() {
  39. updateInfo();
  40. }
  41. function tearDown() {
  42. document.documentElement.dir = 'ltr';
  43. document.body.dir = 'ltr';
  44. }
  45. function testGetOffsetStart() {
  46. var elm = document.getElementById('scrolledElementRtl');
  47. assertEquals(elm.style['right'], goog.style.bidi.getOffsetStart(elm) + 'px');
  48. elm = document.getElementById('scrolledElementLtr');
  49. assertEquals(elm.style['left'], goog.style.bidi.getOffsetStart(elm) + 'px');
  50. }
  51. function testSetScrollOffsetRtl() {
  52. var scrollElm = document.getElementById('scrollDivRtl');
  53. var scrolledElm = document.getElementById('scrolledElementRtl');
  54. var originalDistance =
  55. goog.style.getRelativePosition(scrolledElm, document.body).x;
  56. var scrollAndAssert = function(pixels) {
  57. goog.style.bidi.setScrollOffset(scrollElm, pixels);
  58. assertEquals(
  59. originalDistance + pixels,
  60. goog.style.getRelativePosition(scrolledElm, document.body).x);
  61. };
  62. scrollAndAssert(0);
  63. scrollAndAssert(50);
  64. scrollAndAssert(100);
  65. scrollAndAssert(150);
  66. scrollAndAssert(155);
  67. scrollAndAssert(0);
  68. }
  69. function testSetScrollOffsetLtr() {
  70. var scrollElm = document.getElementById('scrollDivLtr');
  71. var scrolledElm = document.getElementById('scrolledElementLtr');
  72. var originalDistance =
  73. goog.style.getRelativePosition(scrolledElm, document.body).x;
  74. var scrollAndAssert = function(pixels) {
  75. goog.style.bidi.setScrollOffset(scrollElm, pixels);
  76. assertEquals(
  77. originalDistance - pixels,
  78. goog.style.getRelativePosition(scrolledElm, document.body).x);
  79. };
  80. scrollAndAssert(0);
  81. scrollAndAssert(50);
  82. scrollAndAssert(100);
  83. scrollAndAssert(150);
  84. scrollAndAssert(155);
  85. scrollAndAssert(0);
  86. }
  87. function testFixedBodyChildLtr() {
  88. var bodyChild = document.getElementById('bodyChild');
  89. assertEquals(
  90. goog.userAgent.GECKO ? document.body : null, bodyChild.offsetParent);
  91. assertEquals(60, goog.style.bidi.getOffsetStart(bodyChild));
  92. }
  93. function testFixedBodyChildRtl() {
  94. document.documentElement.dir = 'rtl';
  95. document.body.dir = 'rtl';
  96. var bodyChild = document.getElementById('bodyChild');
  97. assertEquals(
  98. goog.userAgent.GECKO ? document.body : null, bodyChild.offsetParent);
  99. var expectedOffsetStart =
  100. goog.dom.getViewportSize().width - 60 - bodyChild.offsetWidth;
  101. // Gecko seems to also add in the marginbox for the body.
  102. // It's not really clear to me if this is true in the general case,
  103. // or just under certain conditions.
  104. if (goog.userAgent.GECKO) {
  105. var marginBox = goog.style.getMarginBox(document.body);
  106. expectedOffsetStart -= (marginBox.left + marginBox.right);
  107. }
  108. assertEquals(expectedOffsetStart, goog.style.bidi.getOffsetStart(bodyChild));
  109. }
  110. function testGetScrollLeftRTL() {
  111. var scrollLeftDiv = document.getElementById('scrollLeftRtl');
  112. scrollLeftDiv.style.overflow = 'visible';
  113. assertEquals(0, goog.style.bidi.getScrollLeft(scrollLeftDiv));
  114. scrollLeftDiv.style.overflow = 'hidden';
  115. assertEquals(0, goog.style.bidi.getScrollLeft(scrollLeftDiv));
  116. // NOTE: 'auto' must go above the 'scroll' assertion. Chrome 47 has a bug
  117. // with non-deterministic scroll positioning. Maybe it recalculates the
  118. // layout on accessing those properties?
  119. // TODO(joeltine): Remove this comment when
  120. // https://code.google.com/p/chromium/issues/detail?id=568706 is resolved.
  121. scrollLeftDiv.style.overflow = 'auto';
  122. assertEquals(0, goog.style.bidi.getScrollLeft(scrollLeftDiv));
  123. scrollLeftDiv.style.overflow = 'scroll';
  124. assertEquals(0, goog.style.bidi.getScrollLeft(scrollLeftDiv));
  125. }