popup_test.js 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. // Copyright 2008 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.ui.PopupTest');
  15. goog.setTestOnly('goog.ui.PopupTest');
  16. goog.require('goog.positioning.AnchoredPosition');
  17. goog.require('goog.positioning.Corner');
  18. goog.require('goog.style');
  19. goog.require('goog.testing.jsunit');
  20. goog.require('goog.ui.Popup');
  21. goog.require('goog.userAgent');
  22. /**
  23. * This is used to round pixel values on FF3 Mac.
  24. */
  25. function assertRoundedEquals(a, b, c) {
  26. function round(x) {
  27. return goog.userAgent.GECKO && (goog.userAgent.MAC || goog.userAgent.X11) &&
  28. goog.userAgent.isVersionOrHigher('1.9') ?
  29. Math.round(x) :
  30. x;
  31. }
  32. if (arguments.length == 3) {
  33. assertEquals(a, round(b), round(c));
  34. } else {
  35. assertEquals(round(a), round(b));
  36. }
  37. }
  38. function testCreateAndReposition() {
  39. var anchorEl = document.getElementById('anchor');
  40. var popupEl = document.getElementById('popup');
  41. var corner = goog.positioning.Corner;
  42. var pos =
  43. new goog.positioning.AnchoredPosition(anchorEl, corner.BOTTOM_START);
  44. var popup = new goog.ui.Popup(popupEl, pos);
  45. popup.setVisible(true);
  46. var anchorRect = goog.style.getBounds(anchorEl);
  47. var popupRect = goog.style.getBounds(popupEl);
  48. assertRoundedEquals(
  49. 'Left edge of popup should line up with left edge ' +
  50. 'of anchor.',
  51. anchorRect.left, popupRect.left);
  52. assertRoundedEquals(
  53. 'Popup should be positioned just below the anchor.',
  54. anchorRect.top + anchorRect.height, popupRect.top);
  55. // Reposition.
  56. anchorEl.style.marginTop = '7px';
  57. popup.reposition();
  58. anchorRect = goog.style.getBounds(anchorEl);
  59. popupRect = goog.style.getBounds(popupEl);
  60. assertRoundedEquals(
  61. 'Popup should be positioned just below the anchor.',
  62. anchorRect.top + anchorRect.height, popupRect.top);
  63. }
  64. function testSetPinnedCorner() {
  65. var anchorEl = document.getElementById('anchor');
  66. var popupEl = document.getElementById('popup');
  67. var corner = goog.positioning.Corner;
  68. var pos =
  69. new goog.positioning.AnchoredPosition(anchorEl, corner.BOTTOM_START);
  70. var popup = new goog.ui.Popup(popupEl, pos);
  71. popup.setVisible(true);
  72. var anchorRect = goog.style.getBounds(anchorEl);
  73. var popupRect = goog.style.getBounds(popupEl);
  74. assertRoundedEquals(
  75. 'Left edge of popup should line up with left edge ' +
  76. 'of anchor.',
  77. anchorRect.left, popupRect.left);
  78. assertRoundedEquals(
  79. 'Popup should be positioned just below the anchor.',
  80. anchorRect.top + anchorRect.height, popupRect.top);
  81. // Change pinned corner.
  82. popup.setPinnedCorner(corner.BOTTOM_END);
  83. anchorRect = goog.style.getBounds(anchorEl);
  84. popupRect = goog.style.getBounds(popupEl);
  85. assertRoundedEquals(
  86. 'Right edge of popup should line up with left edge ' +
  87. 'of anchor.',
  88. anchorRect.left, popupRect.left + popupRect.width);
  89. assertRoundedEquals(
  90. 'Bottom edge of popup should line up with bottom ' +
  91. 'of anchor.',
  92. anchorRect.top + anchorRect.height, popupRect.top + popupRect.height);
  93. // Position outside the viewport.
  94. anchorEl.style.marginLeft = '0';
  95. popup.reposition();
  96. anchorRect = goog.style.getBounds(anchorEl);
  97. popupRect = goog.style.getBounds(popupEl);
  98. assertRoundedEquals(
  99. 'Right edge of popup should line up with left edge ' +
  100. 'of anchor.',
  101. anchorRect.left, popupRect.left + popupRect.width);
  102. anchorEl.style.marginLeft = '';
  103. }