clientposition_test.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. /**
  15. * Tests for {@code goog.positioning.ClientPosition}
  16. * @author chrishenry@google.com (Chris Henry)
  17. */
  18. goog.provide('goog.positioning.clientPositionTest');
  19. goog.setTestOnly('goog.positioning.clientPositionTest');
  20. goog.require('goog.dom');
  21. goog.require('goog.dom.TagName');
  22. goog.require('goog.positioning.ClientPosition');
  23. goog.require('goog.positioning.Corner');
  24. goog.require('goog.style');
  25. goog.require('goog.testing.jsunit');
  26. /**
  27. * Prefabricated popup element for convenient. This is created during
  28. * setUp and is not attached to the document at the beginning of the
  29. * test.
  30. * @type {Element}
  31. */
  32. var popupElement;
  33. var testArea;
  34. var POPUP_HEIGHT = 100;
  35. var POPUP_WIDTH = 150;
  36. function setUp() {
  37. testArea = goog.dom.getElement('test-area');
  38. // Enlarges the test area to 5000x5000px so that we can be confident
  39. // that scrolling the document to some small (x,y) value would work.
  40. goog.style.setSize(testArea, 5000, 5000);
  41. window.scrollTo(0, 0);
  42. popupElement = goog.dom.createDom(goog.dom.TagName.DIV);
  43. goog.style.setSize(popupElement, POPUP_WIDTH, POPUP_HEIGHT);
  44. popupElement.style.position = 'absolute';
  45. // For ease of debugging.
  46. popupElement.style.background = 'blue';
  47. }
  48. function tearDown() {
  49. popupElement = null;
  50. goog.dom.removeChildren(testArea);
  51. testArea.setAttribute('style', '');
  52. }
  53. function testClientPositionWithZeroViewportOffset() {
  54. goog.dom.appendChild(testArea, popupElement);
  55. var x = 300;
  56. var y = 200;
  57. var pos = new goog.positioning.ClientPosition(x, y);
  58. pos.reposition(popupElement, goog.positioning.Corner.TOP_LEFT);
  59. assertPageOffset(x, y, popupElement);
  60. pos.reposition(popupElement, goog.positioning.Corner.TOP_RIGHT);
  61. assertPageOffset(x - POPUP_WIDTH, y, popupElement);
  62. pos.reposition(popupElement, goog.positioning.Corner.BOTTOM_LEFT);
  63. assertPageOffset(x, y - POPUP_HEIGHT, popupElement);
  64. pos.reposition(popupElement, goog.positioning.Corner.BOTTOM_RIGHT);
  65. assertPageOffset(x - POPUP_WIDTH, y - POPUP_HEIGHT, popupElement);
  66. }
  67. function testClientPositionWithSomeViewportOffset() {
  68. goog.dom.appendChild(testArea, popupElement);
  69. var x = 300;
  70. var y = 200;
  71. var scrollX = 135;
  72. var scrollY = 270;
  73. window.scrollTo(scrollX, scrollY);
  74. var pos = new goog.positioning.ClientPosition(x, y);
  75. pos.reposition(popupElement, goog.positioning.Corner.TOP_LEFT);
  76. assertPageOffset(scrollX + x, scrollY + y, popupElement);
  77. }
  78. function testClientPositionWithPositionContext() {
  79. var contextAbsoluteX = 90;
  80. var contextAbsoluteY = 110;
  81. var x = 300;
  82. var y = 200;
  83. var contextElement =
  84. goog.dom.createDom(goog.dom.TagName.DIV, undefined, popupElement);
  85. goog.style.setPosition(contextElement, contextAbsoluteX, contextAbsoluteY);
  86. contextElement.style.position = 'absolute';
  87. goog.dom.appendChild(testArea, contextElement);
  88. var pos = new goog.positioning.ClientPosition(x, y);
  89. pos.reposition(popupElement, goog.positioning.Corner.TOP_LEFT);
  90. assertPageOffset(x, y, popupElement);
  91. }
  92. function assertPageOffset(expectedX, expectedY, el) {
  93. var offsetCoordinate = goog.style.getPageOffset(el);
  94. assertEquals('x-coord page offset is wrong.', expectedX, offsetCoordinate.x);
  95. assertEquals('y-coord page offset is wrong.', expectedY, offsetCoordinate.y);
  96. }