menuanchoredposition_test.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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.MenuAnchoredPositionTest');
  15. goog.setTestOnly('goog.positioning.MenuAnchoredPositionTest');
  16. goog.require('goog.dom');
  17. goog.require('goog.dom.TagName');
  18. goog.require('goog.positioning.Corner');
  19. goog.require('goog.positioning.MenuAnchoredPosition');
  20. goog.require('goog.testing.jsunit');
  21. var offscreenAnchor;
  22. var onscreenAnchor;
  23. var customAnchor;
  24. var menu;
  25. var corner = goog.positioning.Corner;
  26. var savedMenuHtml;
  27. function setUp() {
  28. offscreenAnchor = goog.dom.getElement('offscreen-anchor');
  29. onscreenAnchor = goog.dom.getElement('onscreen-anchor');
  30. customAnchor = goog.dom.getElement('custom-anchor');
  31. customAnchor.style.left = '0';
  32. customAnchor.style.top = '0';
  33. menu = goog.dom.getElement('menu');
  34. savedMenuHtml = menu.innerHTML;
  35. menu.style.left = '20px';
  36. menu.style.top = '20px';
  37. }
  38. function tearDown() {
  39. menu.innerHTML = savedMenuHtml;
  40. }
  41. function testRepositionWithAdjustAndOnscreenAnchor() {
  42. // Add so many children that it can't possibly fit onscreen.
  43. for (var i = 0; i < 200; i++) {
  44. menu.appendChild(
  45. goog.dom.createDom(goog.dom.TagName.DIV, null, 'New Item ' + i));
  46. }
  47. var pos = new goog.positioning.MenuAnchoredPosition(
  48. onscreenAnchor, corner.TOP_LEFT, true);
  49. pos.reposition(menu, corner.TOP_LEFT);
  50. var offset = 0;
  51. assertEquals(offset, menu.offsetTop);
  52. assertEquals(5, menu.offsetLeft);
  53. }
  54. function testRepositionWithAdjustAndOffscreenAnchor() {
  55. // This does not get adjusted because it's too far offscreen.
  56. var pos = new goog.positioning.MenuAnchoredPosition(
  57. offscreenAnchor, corner.TOP_LEFT, true);
  58. pos.reposition(menu, corner.TOP_LEFT);
  59. assertEquals(-1000, menu.offsetTop);
  60. assertEquals(-1000, menu.offsetLeft);
  61. }
  62. function testRespositionFailoverEvenWhenResizeHeightIsOn() {
  63. var pos = new goog.positioning.MenuAnchoredPosition(
  64. onscreenAnchor, corner.TOP_LEFT, true, true);
  65. pos.reposition(menu, corner.TOP_RIGHT);
  66. // The menu should not get positioned offscreen.
  67. assertEquals(5, menu.offsetTop);
  68. assertEquals(5, menu.offsetLeft);
  69. }
  70. function testRepositionToBottomLeftWhenBottomFailsAndRightFailsAndResizeOn() {
  71. var pageSize = goog.dom.getViewportSize();
  72. customAnchor.style.left = (pageSize.width - 10) + 'px';
  73. // Add so many children that it can't possibly fit onscreen.
  74. for (var i = 0; i < 200; i++) {
  75. menu.appendChild(
  76. goog.dom.createDom(goog.dom.TagName.DIV, null, 'New Item ' + i));
  77. }
  78. var pos = new goog.positioning.MenuAnchoredPosition(
  79. customAnchor, corner.TOP_LEFT, true, true);
  80. pos.reposition(menu, corner.TOP_LEFT);
  81. assertEquals(menu.offsetLeft + menu.offsetWidth, customAnchor.offsetLeft);
  82. }