You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GridDetailsLocationTest.java 9.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. package com.vaadin.tests.components.grid;
  2. import static org.junit.Assert.assertEquals;
  3. import static org.junit.Assert.assertFalse;
  4. import java.util.ArrayList;
  5. import java.util.Collection;
  6. import java.util.List;
  7. import org.junit.Before;
  8. import org.junit.Test;
  9. import org.openqa.selenium.By;
  10. import org.openqa.selenium.Keys;
  11. import org.openqa.selenium.StaleElementReferenceException;
  12. import org.openqa.selenium.WebDriver;
  13. import org.openqa.selenium.WebElement;
  14. import org.openqa.selenium.support.ui.ExpectedCondition;
  15. import com.vaadin.testbench.TestBenchElement;
  16. import com.vaadin.testbench.elements.ButtonElement;
  17. import com.vaadin.testbench.elements.CheckBoxElement;
  18. import com.vaadin.testbench.elements.GridElement;
  19. import com.vaadin.testbench.elements.TextFieldElement;
  20. import com.vaadin.testbench.parallel.TestCategory;
  21. import com.vaadin.tests.tb3.MultiBrowserTest;
  22. @TestCategory("grid")
  23. public class GridDetailsLocationTest extends MultiBrowserTest {
  24. private static final int detailsDefaultHeight = 51;
  25. private static final int detailsDefinedHeight = 33;
  26. private static class Param {
  27. private final int rowIndex;
  28. private final boolean useGenerator;
  29. private final boolean scrollFirstToBottom;
  30. public Param(int rowIndex, boolean useGenerator,
  31. boolean scrollFirstToBottom) {
  32. this.rowIndex = rowIndex;
  33. this.useGenerator = useGenerator;
  34. this.scrollFirstToBottom = scrollFirstToBottom;
  35. }
  36. public int getRowIndex() {
  37. return rowIndex;
  38. }
  39. public boolean useGenerator() {
  40. return useGenerator;
  41. }
  42. public boolean scrollFirstToBottom() {
  43. return scrollFirstToBottom;
  44. }
  45. @Override
  46. public String toString() {
  47. return "Param [rowIndex=" + getRowIndex() + ", useGenerator="
  48. + useGenerator() + ", scrollFirstToBottom="
  49. + scrollFirstToBottom() + "]";
  50. }
  51. }
  52. public static Collection<Param> parameters() {
  53. List<Param> data = new ArrayList<>();
  54. int[] params = { 0, 500, 999 };
  55. for (int rowIndex : params) {
  56. data.add(new Param(rowIndex, true, false));
  57. data.add(new Param(rowIndex, true, true));
  58. }
  59. return data;
  60. }
  61. @Before
  62. public void setUp() {
  63. setDebug(true);
  64. }
  65. @Test
  66. public void toggleAndScroll() throws Throwable {
  67. for (Param param : parameters()) {
  68. try {
  69. openTestURL();
  70. useGenerator(param.useGenerator());
  71. scrollToBottom(param.scrollFirstToBottom());
  72. // the tested method
  73. toggleAndScroll(param.getRowIndex());
  74. verifyLocation(param);
  75. } catch (Throwable t) {
  76. throw new Throwable("" + param, t);
  77. }
  78. }
  79. }
  80. @Test
  81. public void scrollAndToggle() throws Throwable {
  82. for (Param param : parameters()) {
  83. try {
  84. openTestURL();
  85. useGenerator(param.useGenerator());
  86. scrollToBottom(param.scrollFirstToBottom());
  87. // the tested method
  88. scrollAndToggle(param.getRowIndex());
  89. verifyLocation(param);
  90. } catch (Throwable t) {
  91. throw new Throwable("" + param, t);
  92. }
  93. }
  94. }
  95. @Test
  96. public void testDetailsHeightWithGenerator() {
  97. openTestURL();
  98. useGenerator(true);
  99. toggleAndScroll(5);
  100. verifyDetailsRowHeight(5, detailsDefinedHeight, 0);
  101. verifyDetailsDecoratorLocation(5, 0, 0);
  102. toggleAndScroll(0);
  103. verifyDetailsRowHeight(0, detailsDefinedHeight, 0);
  104. // decorator elements are in DOM in the order they have been added
  105. verifyDetailsDecoratorLocation(0, 0, 1);
  106. verifyDetailsRowHeight(5, detailsDefinedHeight, 1);
  107. verifyDetailsDecoratorLocation(5, 1, 0);
  108. }
  109. private void verifyDetailsRowHeight(int rowIndex, int expectedHeight,
  110. int visibleIndexOfSpacer) {
  111. waitForDetailsVisible();
  112. WebElement details = getDetailsElement(visibleIndexOfSpacer);
  113. assertEquals("Wrong details row height", expectedHeight,
  114. details.getSize().getHeight());
  115. }
  116. private void verifyDetailsDecoratorLocation(int row,
  117. int visibleIndexOfSpacer, int visibleIndexOfDeco) {
  118. WebElement detailsElement = getDetailsElement(visibleIndexOfSpacer);
  119. WebElement detailsDecoElement = getDetailsDecoElement(
  120. visibleIndexOfDeco);
  121. GridElement.GridRowElement rowElement = getGrid().getRow(row);
  122. assertEquals("Details deco top position does not match row top pos",
  123. rowElement.getLocation().getY(),
  124. detailsDecoElement.getLocation().getY());
  125. assertEquals(
  126. "Details deco bottom position does not match details bottom pos",
  127. detailsElement.getLocation().getY()
  128. + detailsElement.getSize().getHeight(),
  129. detailsDecoElement.getLocation().getY()
  130. + detailsDecoElement.getSize().getHeight());
  131. }
  132. private void verifyLocation(Param param) {
  133. assertFalse("Notification was present",
  134. isElementPresent(By.className("v-Notification")));
  135. TestBenchElement headerRow = getGrid().getHeaderRow(0);
  136. final int topBoundary = headerRow.getLocation().getX()
  137. + headerRow.getSize().height;
  138. final int bottomBoundary = getGrid().getLocation().getX()
  139. + getGrid().getSize().getHeight()
  140. - getHorizontalScrollbar().getSize().height;
  141. GridElement.GridRowElement row = getGrid().getRow(param.getRowIndex());
  142. final int rowTop = row.getLocation().getX();
  143. waitForDetailsVisible();
  144. WebElement details = getDetailsElement();
  145. final int detailsBottom = details.getLocation().getX()
  146. + details.getSize().getHeight();
  147. assertGreaterOrEqual("Row top should be inside grid, gridTop:"
  148. + topBoundary + " rowTop" + rowTop, topBoundary, rowTop);
  149. assertLessThanOrEqual(
  150. "Decorator bottom should be inside grid, gridBottom:"
  151. + bottomBoundary + " decoratorBotton:" + detailsBottom,
  152. detailsBottom, bottomBoundary);
  153. verifyDetailsRowHeight(param.getRowIndex(),
  154. param.useGenerator() ? detailsDefinedHeight
  155. : detailsDefaultHeight,
  156. 0);
  157. verifyDetailsDecoratorLocation(param.getRowIndex(), 0, 0);
  158. assertFalse("Notification was present",
  159. isElementPresent(By.className("v-Notification")));
  160. }
  161. private final By locator = By.className("v-grid-spacer");
  162. private WebElement getDetailsElement() {
  163. return getDetailsElement(0);
  164. }
  165. private WebElement getDetailsElement(int index) {
  166. return findElements(locator).get(index);
  167. }
  168. private WebElement getDetailsDecoElement(int index) {
  169. return findElements(By.className("v-grid-spacer-deco")).get(index);
  170. }
  171. private void waitForDetailsVisible() {
  172. waitUntil(new ExpectedCondition<WebElement>() {
  173. @Override
  174. public WebElement apply(WebDriver driver) {
  175. try {
  176. WebElement detailsElement = getDetailsElement();
  177. return detailsElement.isDisplayed()
  178. && detailsElement.getSize().getHeight() > 3
  179. ? detailsElement
  180. : null;
  181. } catch (StaleElementReferenceException e) {
  182. return null;
  183. }
  184. }
  185. @Override
  186. public String toString() {
  187. return "visibility of element located by " + locator;
  188. }
  189. }, 5);
  190. waitForElementVisible(By.className("v-grid-spacer"));
  191. }
  192. private void scrollToBottom(boolean scrollFirstToBottom) {
  193. if (scrollFirstToBottom) {
  194. executeScript("arguments[0].scrollTop = 9999999",
  195. getVerticalScrollbar());
  196. }
  197. }
  198. private void useGenerator(boolean use) {
  199. CheckBoxElement checkBox = $(CheckBoxElement.class).first();
  200. boolean isChecked = isCheckedValo(checkBox);
  201. if (use != isChecked) {
  202. clickValo(checkBox);
  203. }
  204. }
  205. @SuppressWarnings("boxing")
  206. private boolean isCheckedValo(CheckBoxElement checkBoxElement) {
  207. WebElement checkbox = checkBoxElement.findElement(By.tagName("input"));
  208. Object value = executeScript("return arguments[0].checked;", checkbox);
  209. return (Boolean) value;
  210. }
  211. private void clickValo(CheckBoxElement checkBoxElement) {
  212. checkBoxElement.click(5, 5);
  213. }
  214. private void scrollAndToggle(int row) {
  215. setRow(row);
  216. getScrollAndToggle().click();
  217. }
  218. private void toggleAndScroll(int row) {
  219. setRow(row);
  220. getToggleAndScroll().click();
  221. }
  222. private ButtonElement getScrollAndToggle() {
  223. return $(ButtonElement.class).caption("Scroll and toggle").first();
  224. }
  225. private ButtonElement getToggleAndScroll() {
  226. return $(ButtonElement.class).caption("Toggle and scroll").first();
  227. }
  228. private void setRow(int row) {
  229. $(TextFieldElement.class).first().clear();
  230. $(TextFieldElement.class).first().sendKeys(String.valueOf(row),
  231. Keys.ENTER, Keys.TAB);
  232. }
  233. private GridElement getGrid() {
  234. return $(GridElement.class).first();
  235. }
  236. private WebElement getVerticalScrollbar() {
  237. WebElement scrollBar = getGrid()
  238. .findElement(By.className("v-grid-scroller-vertical"));
  239. return scrollBar;
  240. }
  241. private WebElement getHorizontalScrollbar() {
  242. WebElement scrollBar = getGrid()
  243. .findElement(By.className("v-grid-scroller-horizontal"));
  244. return scrollBar;
  245. }
  246. }