Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

TooltipPositionTest.java 6.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Copyright 2000-2014 Vaadin Ltd.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License"); you may not
  5. * use this file except in compliance with the License. You may obtain a copy of
  6. * the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  12. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  13. * License for the specific language governing permissions and limitations under
  14. * the License.
  15. */
  16. package com.vaadin.tests.components;
  17. import java.util.List;
  18. import org.junit.Assert;
  19. import org.junit.Test;
  20. import org.openqa.selenium.By;
  21. import org.openqa.selenium.Dimension;
  22. import org.openqa.selenium.Point;
  23. import org.openqa.selenium.StaleElementReferenceException;
  24. import org.openqa.selenium.WebDriver;
  25. import org.openqa.selenium.WebDriver.Window;
  26. import org.openqa.selenium.WebElement;
  27. import org.openqa.selenium.interactions.Actions;
  28. import org.openqa.selenium.support.ui.ExpectedCondition;
  29. import com.vaadin.testbench.elements.ButtonElement;
  30. import com.vaadin.tests.tb3.MultiBrowserTest;
  31. /**
  32. * Tests that the tooltip is positioned so that it fits in the displayed area.
  33. *
  34. * @author Vaadin Ltd
  35. */
  36. public class TooltipPositionTest extends MultiBrowserTest {
  37. @Test
  38. public void testRegression_EmptyTooltipShouldNotBeAppearedDuringInitialization()
  39. throws Exception {
  40. openTestURL();
  41. waitForElementVisible(By.cssSelector(".v-tooltip"));
  42. WebElement tooltip = driver.findElement(By.cssSelector(".v-tooltip"));
  43. Assert.assertTrue(
  44. "This init tooltip with text ' ' is present in the DOM and should be entirely outside the browser window",
  45. isOutsideOfWindow(tooltip));
  46. }
  47. @Test
  48. public void testTooltipPosition() throws Exception {
  49. openTestURL();
  50. for (int i = 0; i < TooltipPosition.NUMBER_OF_BUTTONS; i++) {
  51. ButtonElement button = $(ButtonElement.class).get(i);
  52. // Move the mouse to display the tooltip.
  53. Actions actions = new Actions(driver);
  54. actions.moveToElement(button, 10, 10);
  55. actions.build().perform();
  56. waitUntil(tooltipToBeInsideWindow(By.cssSelector(".v-tooltip"),
  57. driver.manage().window()));
  58. if (i < TooltipPosition.NUMBER_OF_BUTTONS - 1) {
  59. // Remove the tooltip by moving the mouse.
  60. actions = new Actions(driver);
  61. actions.moveByOffset(300, 0);
  62. actions.build().perform();
  63. waitUntil(tooltipNotToBeShown(By.cssSelector(".v-tooltip"),
  64. driver.manage().window()));
  65. }
  66. }
  67. }
  68. /*
  69. * An expectation for checking that the tooltip found by the given locator
  70. * is present in the DOM and entirely inside the browser window. The
  71. * coordinate of the top left corner of the window is supposed to be (0, 0).
  72. */
  73. private ExpectedCondition<Boolean> tooltipToBeInsideWindow(
  74. final By tooltipLocator, final Window window) {
  75. return new ExpectedCondition<Boolean>() {
  76. @Override
  77. public Boolean apply(WebDriver input) {
  78. List<WebElement> elements = findElements(tooltipLocator);
  79. if (elements.isEmpty()) {
  80. return false;
  81. }
  82. WebElement element = elements.get(0);
  83. try {
  84. if (!element.isDisplayed()) {
  85. return false;
  86. }
  87. Point topLeft = element.getLocation();
  88. int xLeft = topLeft.getX();
  89. int yTop = topLeft.getY();
  90. if (xLeft < 0 || yTop < 0) {
  91. return false;
  92. }
  93. Dimension elementSize = element.getSize();
  94. int xRight = xLeft + elementSize.getWidth() - 1;
  95. int yBottom = yTop + elementSize.getHeight() - 1;
  96. Dimension browserSize = window.getSize();
  97. return xRight < browserSize.getWidth()
  98. && yBottom < browserSize.getHeight();
  99. } catch (StaleElementReferenceException e) {
  100. return false;
  101. }
  102. }
  103. @Override
  104. public String toString() {
  105. return "the tooltip to be displayed inside the window";
  106. }
  107. };
  108. };
  109. /*
  110. * An expectation for checking that the tooltip found by the given locator
  111. * is not shown in the window, even partially. The top left corner of window
  112. * should have coordinates (0, 0).
  113. */
  114. private ExpectedCondition<Boolean> tooltipNotToBeShown(
  115. final By tooltipLocator, final Window window) {
  116. return new ExpectedCondition<Boolean>() {
  117. @Override
  118. public Boolean apply(WebDriver input) {
  119. List<WebElement> elements = findElements(tooltipLocator);
  120. if (elements.isEmpty()) {
  121. return true;
  122. }
  123. WebElement tooltip = elements.get(0);
  124. try {
  125. return isOutsideOfWindow(tooltip);
  126. } catch (StaleElementReferenceException e) {
  127. return true;
  128. }
  129. }
  130. @Override
  131. public String toString() {
  132. return "the tooltip not to be displayed inside the window";
  133. }
  134. };
  135. }
  136. private boolean isOutsideOfWindow(WebElement tooltip) {
  137. if (!tooltip.isDisplayed()) {
  138. return true;
  139. }
  140. // The tooltip is shown, at least partially, if
  141. // its intervals of both horizontal and vertical coordinates
  142. // overlap those of the window.
  143. Point topLeft = tooltip.getLocation();
  144. Dimension tooltipSize = tooltip.getSize();
  145. Dimension windowSize = driver.manage().window().getSize();
  146. int xLeft = topLeft.getX();
  147. int yTop = topLeft.getY();
  148. int xRight = xLeft + tooltipSize.getWidth() - 1;
  149. int yBottom = yTop + tooltipSize.getHeight() - 1;
  150. boolean overlapHorizontally = !(xRight < 0 || xLeft >= windowSize
  151. .getWidth());
  152. boolean overlapVertically = !(yBottom < 0 || yTop >= windowSize
  153. .getHeight());
  154. return !(overlapHorizontally && overlapVertically);
  155. }
  156. }