viewportclientposition_test.js 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Copyright 2009 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.positioning.ViewportClientPositionTest');
  15. goog.setTestOnly('goog.positioning.ViewportClientPositionTest');
  16. goog.require('goog.dom');
  17. goog.require('goog.positioning.Corner');
  18. goog.require('goog.positioning.Overflow');
  19. goog.require('goog.positioning.ViewportClientPosition');
  20. goog.require('goog.style');
  21. goog.require('goog.testing.jsunit');
  22. goog.require('goog.userAgent');
  23. var viewportSize, anchor, popup, dom, frameRect;
  24. var corner = goog.positioning.Corner;
  25. // Allow positions to be off by one in gecko as it reports scrolling
  26. // offsets in steps of 2.
  27. var ALLOWED_OFFSET = goog.userAgent.GECKO ? 1 : 0;
  28. function setUp() {
  29. var frame = document.getElementById('frame1');
  30. var doc = goog.dom.getFrameContentDocument(frame);
  31. dom = goog.dom.getDomHelper(doc);
  32. viewportSize = dom.getViewportSize();
  33. anchor = dom.getElement('anchor');
  34. popup = dom.getElement('popup');
  35. popup.style.overflowY = 'visible';
  36. goog.style.setSize(popup, 20, 20);
  37. frameRect = goog.style.getVisibleRectForElement(doc.body);
  38. }
  39. function testPositionAtCoordinateTopLeft() {
  40. var pos = new goog.positioning.ViewportClientPosition(100, 100);
  41. pos.reposition(popup, corner.TOP_LEFT);
  42. var offset = goog.style.getPageOffset(popup);
  43. assertEquals(
  44. 'Left edge of popup should be at specified x coordinate.', 100, offset.x);
  45. assertEquals(
  46. 'Top edge of popup should be at specified y coordinate.', 100, offset.y);
  47. }
  48. function testPositionAtCoordinateBottomRight() {
  49. var pos = new goog.positioning.ViewportClientPosition(100, 100);
  50. pos.reposition(popup, corner.BOTTOM_RIGHT);
  51. var bounds = goog.style.getBounds(popup);
  52. assertEquals(
  53. 'Right edge of popup should be at specified x coordinate.', 100,
  54. bounds.left + bounds.width);
  55. assertEquals(
  56. 'Bottom edge of popup should be at specified x coordinate.', 100,
  57. bounds.top + bounds.height);
  58. }
  59. function testPositionAtCoordinateTopLeftWithScroll() {
  60. dom.getDocument().body.style.paddingTop = '300px';
  61. dom.getDocument().body.style.height = '3000px';
  62. dom.getDocumentScrollElement().scrollTop = 50;
  63. dom.getDocument().body.scrollTop = 50;
  64. var pos = new goog.positioning.ViewportClientPosition(0, 0);
  65. pos.reposition(popup, corner.TOP_LEFT);
  66. var offset = goog.style.getPageOffset(popup);
  67. assertEquals(
  68. 'Left edge of popup should be at specified x coordinate.', 0, offset.x);
  69. assertTrue(
  70. 'Top edge of popup should be at specified y coordinate ' +
  71. 'adjusted for scroll.',
  72. Math.abs(offset.y - 50) <= ALLOWED_OFFSET);
  73. dom.getDocument().body.style.paddingLeft = '1000px';
  74. dom.getDocumentScrollElement().scrollLeft = 500;
  75. pos.reposition(popup, corner.TOP_LEFT);
  76. offset = goog.style.getPageOffset(popup);
  77. assertTrue(
  78. 'Left edge of popup should be at specified x coordinate ' +
  79. 'adjusted for scroll.',
  80. Math.abs(offset.x - 500) <= ALLOWED_OFFSET);
  81. dom.getDocumentScrollElement().scrollLeft = 0;
  82. dom.getDocumentScrollElement().scrollTop = 0;
  83. dom.getDocument().body.style.paddingLeft = '';
  84. dom.getDocument().body.style.paddingTop = '';
  85. pos.reposition(popup, corner.TOP_LEFT);
  86. offset = goog.style.getPageOffset(popup);
  87. assertEquals(
  88. 'Left edge of popup should be at specified x coordinate.', 0, offset.x);
  89. assertEquals(
  90. 'Top edge of popup should be at specified y coordinate.', 0, offset.y);
  91. }
  92. function testOverflowRightFlipHor() {
  93. var pos = new goog.positioning.ViewportClientPosition(frameRect.right, 100);
  94. pos.reposition(popup, corner.TOP_LEFT);
  95. var offset = goog.style.getPageOffset(popup);
  96. assertEquals(
  97. 'Left edge of popup should have been adjusted so that it ' +
  98. 'fits inside the viewport.',
  99. frameRect.right - popup.offsetWidth, offset.x);
  100. assertEquals(
  101. 'Top edge of popup should be at specified y coordinate.', 100, offset.y);
  102. }
  103. function testOverflowTopFlipVer() {
  104. var pos = new goog.positioning.ViewportClientPosition(100, 0);
  105. pos.reposition(popup, corner.TOP_RIGHT);
  106. var offset = goog.style.getPageOffset(popup);
  107. assertEquals(
  108. 'Left edge of popup should be at specified x coordinate.', 80, offset.x);
  109. assertEquals(
  110. 'Top edge of popup should have been adjusted so that it ' +
  111. 'fits inside the viewport.',
  112. 0, offset.y);
  113. }
  114. function testOverflowBottomRightFlipBoth() {
  115. var pos = new goog.positioning.ViewportClientPosition(
  116. frameRect.right, frameRect.bottom);
  117. pos.reposition(popup, corner.TOP_LEFT);
  118. var offset = goog.style.getPageOffset(popup);
  119. assertEquals(
  120. 'Left edge of popup should have been adjusted so that it ' +
  121. 'fits inside the viewport.',
  122. frameRect.right - popup.offsetWidth, offset.x);
  123. assertEquals(
  124. 'Top edge of popup should have been adjusted so that it ' +
  125. 'fits inside the viewport.',
  126. frameRect.bottom - popup.offsetHeight, offset.y);
  127. }
  128. function testLastRespotOverflow() {
  129. var large = 2000;
  130. goog.style.setSize(popup, 20, large);
  131. popup.style.overflowY = 'auto';
  132. var pos = new goog.positioning.ViewportClientPosition(0, 0);
  133. pos.reposition(popup, corner.TOP_LEFT);
  134. assertEquals(large, popup.offsetHeight);
  135. pos.setLastResortOverflow(goog.positioning.Overflow.RESIZE_HEIGHT);
  136. pos.reposition(popup, corner.TOP_LEFT);
  137. assertNotEquals(large, popup.offsetHeight);
  138. }