anchoredviewportposition_test.js 6.3 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.AnchoredViewportPositionTest');
  15. goog.setTestOnly('goog.positioning.AnchoredViewportPositionTest');
  16. goog.require('goog.dom');
  17. goog.require('goog.math.Box');
  18. goog.require('goog.positioning.AnchoredViewportPosition');
  19. goog.require('goog.positioning.Corner');
  20. goog.require('goog.positioning.OverflowStatus');
  21. goog.require('goog.style');
  22. goog.require('goog.testing.jsunit');
  23. var frame, doc, dom, viewportSize, anchor, popup;
  24. var corner = goog.positioning.Corner;
  25. function setUp() {
  26. frame = document.getElementById('frame1');
  27. doc = goog.dom.getFrameContentDocument(frame);
  28. dom = goog.dom.getDomHelper(doc);
  29. viewportSize = dom.getViewportSize();
  30. anchor = dom.getElement('anchor');
  31. popup = dom.getElement('popup');
  32. goog.style.setSize(popup, 20, 20);
  33. }
  34. // The frame has enough space at the bottom of the anchor.
  35. function testRepositionBottom() {
  36. var avp = new goog.positioning.AnchoredViewportPosition(
  37. anchor, corner.BOTTOM_LEFT, false);
  38. goog.style.setSize(anchor, 100, 100);
  39. goog.style.setPosition(anchor, 0, 0);
  40. assertTrue(viewportSize.height >= 100 + 20);
  41. avp.reposition(popup, corner.TOP_LEFT);
  42. var anchorRect = goog.style.getBounds(anchor);
  43. assertEquals(
  44. anchorRect.top + anchorRect.height, goog.style.getPageOffset(popup).y);
  45. }
  46. // No enough space at the bottom, but at the top.
  47. function testRepositionTop() {
  48. var avp = new goog.positioning.AnchoredViewportPosition(
  49. anchor, corner.BOTTOM_LEFT, false);
  50. var newTop = viewportSize.height - 100;
  51. goog.style.setSize(anchor, 100, 100);
  52. goog.style.setPosition(anchor, 50, newTop);
  53. assertTrue(newTop >= 20);
  54. avp.reposition(popup, corner.TOP_LEFT);
  55. anchorRect = goog.style.getBounds(anchor);
  56. var popupRect = goog.style.getBounds(popup);
  57. assertEquals(anchorRect.top, popupRect.top + popupRect.height);
  58. }
  59. // Not enough space either at the bottom or right but there is enough space at
  60. // top left.
  61. function testRepositionBottomRight() {
  62. var avp = new goog.positioning.AnchoredViewportPosition(
  63. anchor, corner.BOTTOM_RIGHT, false);
  64. goog.style.setSize(anchor, 100, 100);
  65. goog.style.setPosition(
  66. anchor, viewportSize.width - 110, viewportSize.height - 110);
  67. avp.reposition(popup, corner.TOP_LEFT);
  68. anchorRect = goog.style.getBounds(anchor);
  69. var popupRect = goog.style.getBounds(popup);
  70. assertEquals(anchorRect.top, popupRect.top + popupRect.height);
  71. assertEquals(anchorRect.left, popupRect.left + popupRect.width);
  72. }
  73. // Enough space at neither the bottom nor the top. Adjustment flag is false.
  74. function testRepositionNoSpaceWithoutAdjustment() {
  75. var avp = new goog.positioning.AnchoredViewportPosition(
  76. anchor, corner.BOTTOM_LEFT, false);
  77. goog.style.setPosition(anchor, 50, 10);
  78. goog.style.setSize(anchor, 100, viewportSize.height - 20);
  79. avp.reposition(popup, corner.TOP_LEFT);
  80. anchorRect = goog.style.getBounds(anchor);
  81. popupRect = goog.style.getBounds(popup);
  82. assertEquals(anchorRect.top + anchorRect.height, popupRect.top);
  83. assertTrue(popupRect.top + popupRect.height > viewportSize.height);
  84. }
  85. // Enough space at neither the bottom nor the top. Adjustment flag is true.
  86. function testRepositionNoSpaceWithAdjustment() {
  87. var avp = new goog.positioning.AnchoredViewportPosition(
  88. anchor, corner.BOTTOM_LEFT, true);
  89. goog.style.setPosition(anchor, 50, 10);
  90. goog.style.setSize(anchor, 100, viewportSize.height - 20);
  91. avp.reposition(popup, corner.TOP_LEFT);
  92. anchorRect = goog.style.getBounds(anchor);
  93. popupRect = goog.style.getBounds(popup);
  94. assertTrue(anchorRect.top + anchorRect.height > popupRect.top);
  95. assertEquals(viewportSize.height, popupRect.top + popupRect.height);
  96. }
  97. function testAdjustCorner() {
  98. var avp =
  99. new goog.positioning.AnchoredViewportPosition(anchor, corner.BOTTOM_LEFT);
  100. assertEquals(corner.BOTTOM_LEFT, avp.adjustCorner(0, corner.BOTTOM_LEFT));
  101. assertEquals(
  102. corner.BOTTOM_RIGHT,
  103. avp.adjustCorner(
  104. goog.positioning.OverflowStatus.FAILED_HORIZONTAL,
  105. corner.BOTTOM_LEFT));
  106. assertEquals(
  107. corner.TOP_LEFT,
  108. avp.adjustCorner(
  109. goog.positioning.OverflowStatus.FAILED_VERTICAL, corner.BOTTOM_LEFT));
  110. assertEquals(
  111. corner.TOP_RIGHT,
  112. avp.adjustCorner(
  113. goog.positioning.OverflowStatus.FAILED_VERTICAL |
  114. goog.positioning.OverflowStatus.FAILED_HORIZONTAL,
  115. corner.BOTTOM_LEFT));
  116. }
  117. // No space to fit, so uses fallback.
  118. function testOverflowConstraint() {
  119. var tinyBox = new goog.math.Box(0, 0, 0, 0);
  120. var avp = new goog.positioning.AnchoredViewportPosition(
  121. anchor, corner.BOTTOM_LEFT, false, tinyBox);
  122. assertEquals(tinyBox, avp.getOverflowConstraint());
  123. goog.style.setSize(anchor, 50, 50);
  124. goog.style.setPosition(anchor, 80, 80);
  125. avp.reposition(popup, corner.TOP_LEFT);
  126. anchorRect = goog.style.getBounds(anchor);
  127. popupRect = goog.style.getBounds(popup);
  128. assertEquals(anchorRect.left, popupRect.left);
  129. assertEquals(anchorRect.top + anchorRect.height, popupRect.top);
  130. }
  131. // Initially no space to fit above, then changes to have room.
  132. function testChangeOverflowConstraint() {
  133. var tinyBox = new goog.math.Box(0, 0, 0, 0);
  134. var avp = new goog.positioning.AnchoredViewportPosition(
  135. anchor, corner.BOTTOM_LEFT, false, tinyBox);
  136. assertEquals(tinyBox, avp.getOverflowConstraint());
  137. goog.style.setSize(anchor, 50, 50);
  138. goog.style.setPosition(anchor, 80, 80);
  139. avp.reposition(popup, corner.TOP_LEFT);
  140. popupRect = goog.style.getBounds(popup);
  141. assertNotEquals(60, popupRect.top);
  142. var movedBox = new goog.math.Box(60, 100, 100, 60);
  143. avp.setOverflowConstraint(movedBox);
  144. assertEquals(movedBox, avp.getOverflowConstraint());
  145. avp.reposition(popup, corner.TOP_LEFT);
  146. popupRect = goog.style.getBounds(popup);
  147. assertEquals(80, popupRect.left);
  148. assertEquals(60, popupRect.top);
  149. }