anchoredposition_test.js 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. goog.provide('goog.positioning.AnchoredPositionTest');
  15. goog.setTestOnly('goog.positioning.AnchoredPositionTest');
  16. goog.require('goog.dom');
  17. goog.require('goog.positioning.AnchoredPosition');
  18. goog.require('goog.positioning.Corner');
  19. goog.require('goog.positioning.Overflow');
  20. goog.require('goog.style');
  21. goog.require('goog.testing.jsunit');
  22. var frame, doc, dom, viewportSize, anchor, popup;
  23. var corner = goog.positioning.Corner;
  24. var popupLength = 20;
  25. var anchorLength = 100;
  26. function setUp() {
  27. frame = document.getElementById('frame1');
  28. doc = goog.dom.getFrameContentDocument(frame);
  29. dom = goog.dom.getDomHelper(doc);
  30. viewportSize = dom.getViewportSize();
  31. anchor = dom.getElement('anchor');
  32. popup = dom.getElement('popup');
  33. goog.style.setSize(popup, popupLength, popupLength);
  34. goog.style.setPosition(popup, popupLength, popupLength);
  35. goog.style.setSize(anchor, anchorLength, anchorLength);
  36. }
  37. // No enough space at the bottom and no overflow adjustment.
  38. function testRepositionWithDefaultOverflow() {
  39. var avp = new goog.positioning.AnchoredPosition(anchor, corner.BOTTOM_LEFT);
  40. var newTop = viewportSize.height - anchorLength;
  41. goog.style.setPosition(anchor, 50, newTop);
  42. var anchorRect = goog.style.getBounds(anchor);
  43. avp.reposition(popup, corner.TOP_LEFT);
  44. var popupRect = goog.style.getBounds(popup);
  45. assertEquals(anchorRect.top + anchorRect.height, popupRect.top);
  46. }
  47. // No enough space at the bottom and ADJUST_Y overflow adjustment.
  48. function testRepositionWithOverflow() {
  49. var avp = new goog.positioning.AnchoredPosition(
  50. anchor, corner.BOTTOM_LEFT, goog.positioning.Overflow.ADJUST_Y);
  51. var newTop = viewportSize.height - anchorLength;
  52. goog.style.setPosition(anchor, 50, newTop);
  53. var anchorRect = goog.style.getBounds(anchor);
  54. avp.reposition(popup, corner.TOP_LEFT);
  55. var popupRect = goog.style.getBounds(popup);
  56. assertEquals(
  57. anchorRect.top + anchorRect.height, popupRect.top + popupRect.height);
  58. }