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.

GridHeightTest.java 6.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. package com.vaadin.tests.components.grid;
  2. import static org.hamcrest.MatcherAssert.assertThat;
  3. import static org.hamcrest.Matchers.is;
  4. import static org.hamcrest.number.IsCloseTo.closeTo;
  5. import static org.junit.Assert.fail;
  6. import java.util.HashMap;
  7. import java.util.Map;
  8. import java.util.Map.Entry;
  9. import org.junit.Ignore;
  10. import org.junit.Test;
  11. import org.openqa.selenium.By;
  12. import org.openqa.selenium.WebElement;
  13. import com.vaadin.testbench.elements.GridElement;
  14. import com.vaadin.testbench.elements.GridElement.GridRowElement;
  15. import com.vaadin.testbench.elements.RadioButtonGroupElement;
  16. import com.vaadin.testbench.parallel.TestCategory;
  17. import com.vaadin.tests.tb3.MultiBrowserTest;
  18. /**
  19. * Tests that Grid gets correct height based on height mode, and resizes
  20. * properly with details row if height is undefined.
  21. *
  22. * @author Vaadin Ltd
  23. */
  24. @TestCategory("grid")
  25. public class GridHeightTest extends MultiBrowserTest {
  26. @Override
  27. public void setup() throws Exception {
  28. super.setup();
  29. openTestURL();
  30. waitForElementPresent(By.className("v-grid"));
  31. }
  32. @Test
  33. @Ignore
  34. public void testGridHeightAndResizingUndefined()
  35. throws InterruptedException {
  36. assertNoErrors(testGridHeightAndResizing(GridHeight.UNDEFINED));
  37. }
  38. @Test
  39. @Ignore
  40. public void testGridHeightAndResizingRow() throws InterruptedException {
  41. assertNoErrors(testGridHeightAndResizing(GridHeight.ROW3));
  42. }
  43. @Test
  44. public void testGridHeightAndResizingFull() throws InterruptedException {
  45. assertNoErrors(testGridHeightAndResizing(GridHeight.FULL));
  46. }
  47. private Map<AssertionError, Object[]> testGridHeightAndResizing(
  48. Object gridHeight) throws InterruptedException {
  49. Map<AssertionError, Object[]> errors = new HashMap<>();
  50. String caption;
  51. if (GridHeight.ROW3.equals(gridHeight)) {
  52. caption = gridHeight + " rows";
  53. } else {
  54. caption = (String) gridHeight;
  55. }
  56. $(RadioButtonGroupElement.class).id("gridHeightSelector")
  57. .selectByText(caption);
  58. for (String gridWidth : GridHeight.gridWidths) {
  59. $(RadioButtonGroupElement.class).id("gridWidthSelector")
  60. .selectByText(gridWidth);
  61. for (String detailsRowHeight : GridHeight.detailsRowHeights) {
  62. $(RadioButtonGroupElement.class).id("detailsHeightSelector")
  63. .selectByText(detailsRowHeight);
  64. sleep(500);
  65. GridElement grid = $(GridElement.class).first();
  66. int initialHeight = grid.getSize().getHeight();
  67. try {
  68. // check default height
  69. assertGridHeight(getExpectedInitialHeight(gridHeight),
  70. initialHeight);
  71. } catch (AssertionError e) {
  72. errors.put(e, new Object[] { gridHeight, gridWidth,
  73. detailsRowHeight, "initial" });
  74. fail();
  75. }
  76. GridRowElement row = grid.getRow(2);
  77. WebElement cell = row.findElements(By.className("v-grid-cell"))
  78. .get(0);
  79. cell.click();
  80. waitForElementPresent(By.id("lbl1"));
  81. int openHeight = grid.getSize().getHeight();
  82. try {
  83. // check height with details row opened
  84. assertGridHeight(getExpectedOpenedHeight(gridHeight,
  85. detailsRowHeight), openHeight);
  86. } catch (AssertionError e) {
  87. errors.put(e, new Object[] { gridHeight, gridWidth,
  88. detailsRowHeight, "opened" });
  89. }
  90. cell.click();
  91. waitForElementNotPresent(By.id("lbl1"));
  92. int afterHeight = grid.getSize().getHeight();
  93. try {
  94. // check height with details row closed again
  95. assertThat("Unexpected Grid Height", afterHeight,
  96. is(initialHeight));
  97. } catch (AssertionError e) {
  98. errors.put(e, new Object[] { gridHeight, gridWidth,
  99. detailsRowHeight, "closed" });
  100. }
  101. }
  102. }
  103. return errors;
  104. }
  105. private void assertNoErrors(Map<AssertionError, Object[]> errors) {
  106. if (!errors.isEmpty()) {
  107. StringBuilder sb = new StringBuilder("Exceptions: ");
  108. for (Entry<AssertionError, Object[]> entry : errors.entrySet()) {
  109. sb.append("\n");
  110. for (Object value : entry.getValue()) {
  111. sb.append(value);
  112. sb.append(" - ");
  113. }
  114. sb.append(entry.getKey().getMessage());
  115. }
  116. fail(sb.toString());
  117. }
  118. }
  119. private int getExpectedInitialHeight(Object gridHeight) {
  120. int result = 0;
  121. if (GridHeight.UNDEFINED.equals(gridHeight)
  122. || GridHeight.ROW3.equals(gridHeight)) {
  123. result = 81;
  124. } else if (GridHeight.FULL.equals(gridHeight)) {
  125. // pre-existing issue
  126. result = 400;
  127. }
  128. return result;
  129. }
  130. private int getExpectedOpenedHeight(Object gridHeight,
  131. Object detailsRowHeight) {
  132. int result = 0;
  133. if (GridHeight.UNDEFINED.equals(gridHeight)) {
  134. if (GridHeight.PX100.equals(detailsRowHeight)) {
  135. result = 182;
  136. } else if (GridHeight.FULL.equals(detailsRowHeight)) {
  137. result = 131;
  138. } else if (GridHeight.UNDEFINED.equals(detailsRowHeight)) {
  139. result = 100;
  140. }
  141. } else if (GridHeight.ROW3.equals(gridHeight)
  142. || GridHeight.FULL.equals(gridHeight)) {
  143. result = getExpectedInitialHeight(gridHeight);
  144. }
  145. return result;
  146. }
  147. private void assertGridHeight(int expected, int actual) {
  148. assertThat("Unexpected Grid Height", (double) actual,
  149. closeTo(expected, 1));
  150. }
  151. }