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.

GridLayoutDetailsRowResizeTest.java 5.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. package com.vaadin.tests.components.grid;
  2. import static org.hamcrest.MatcherAssert.assertThat;
  3. import static org.hamcrest.core.Is.is;
  4. import static org.hamcrest.number.IsCloseTo.closeTo;
  5. import java.util.LinkedHashMap;
  6. import java.util.List;
  7. import java.util.Map;
  8. import java.util.Map.Entry;
  9. import java.util.TreeMap;
  10. import org.junit.Test;
  11. import org.openqa.selenium.By;
  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.elements.ButtonElement;
  16. import com.vaadin.testbench.elements.GridElement;
  17. import com.vaadin.testbench.elements.LabelElement;
  18. import com.vaadin.testbench.parallel.TestCategory;
  19. import com.vaadin.tests.tb3.MultiBrowserTest;
  20. /**
  21. * Tests that details row resizes along with the contents properly.
  22. *
  23. * @author Vaadin Ltd
  24. */
  25. @TestCategory("grid")
  26. public class GridLayoutDetailsRowResizeTest extends MultiBrowserTest {
  27. @Test
  28. public void testLabelHeights() {
  29. openTestURL();
  30. waitForElementPresent(By.className("v-grid"));
  31. GridElement grid = $(GridElement.class).first();
  32. grid.getCell(2, 0).click();
  33. waitForElementPresent(By.id("lbl2"));
  34. WebElement layout = grid.findElement(By.className("v-gridlayout"));
  35. int layoutHeight = layout.getSize().height;
  36. ButtonElement button = $(ButtonElement.class).id("btn");
  37. int buttonHeight = button.getSize().height;
  38. // height should be divided equally
  39. double expectedLabelHeight = (layoutHeight - buttonHeight) / 3;
  40. assertLabelHeight("lbl1", expectedLabelHeight);
  41. assertLabelHeight("lbl2", expectedLabelHeight);
  42. assertLabelHeight("lbl3", expectedLabelHeight);
  43. assertDetailsRowHeight(layoutHeight);
  44. // ensure fourth label isn't present yet
  45. assertElementNotPresent(By.id("lbl4"));
  46. button.click();
  47. waitForElementPresent(By.id("lbl4"));
  48. // get layout height after the new label has been added
  49. layoutHeight = layout.getSize().height;
  50. expectedLabelHeight = (layoutHeight - buttonHeight) / 4;
  51. assertLabelHeight("lbl1", expectedLabelHeight);
  52. assertLabelHeight("lbl2", expectedLabelHeight);
  53. assertLabelHeight("lbl3", expectedLabelHeight);
  54. assertLabelHeight("lbl4", expectedLabelHeight);
  55. assertDetailsRowHeight(layoutHeight);
  56. }
  57. @Test
  58. public void testMultipleDetailsRows() {
  59. setDebug(true);
  60. openTestURL();
  61. waitForElementPresent(By.className("v-grid"));
  62. // all rows won't fit if using Valo, toggle back to reindeer
  63. ButtonElement themeButton = $(ButtonElement.class)
  64. .caption("Toggle theme").first();
  65. int buttonHeight = themeButton.getSize().height;
  66. themeButton.click();
  67. // wait for the theme change to take hold
  68. waitUntil(new ExpectedCondition<Boolean>() {
  69. @Override
  70. public Boolean apply(WebDriver arg0) {
  71. return buttonHeight > $(ButtonElement.class)
  72. .caption("Toggle theme").first().getSize().height;
  73. }
  74. @Override
  75. public String toString() {
  76. // Expected condition failed: waiting for ...
  77. return "button's theme to change";
  78. }
  79. });
  80. ButtonElement detailsButton = $(ButtonElement.class)
  81. .caption("Open details").first();
  82. detailsButton.click();
  83. waitForElementPresent(By.id("lbl2"));
  84. List<ButtonElement> buttons = $(ButtonElement.class)
  85. .caption("Toggle visibility").all();
  86. assertThat("Unexpected amount of details rows.", buttons.size(), is(3));
  87. Map<ButtonElement, Integer> positions = new LinkedHashMap<ButtonElement, Integer>();
  88. Map<Integer, ButtonElement> ordered = new TreeMap<Integer, ButtonElement>();
  89. for (ButtonElement button : buttons) {
  90. positions.put(button, button.getLocation().getY());
  91. ordered.put(button.getLocation().getY(), button);
  92. }
  93. int labelHeight = 0;
  94. for (LabelElement label : $(LabelElement.class).all()) {
  95. if ("test1".equals(label.getText())) {
  96. labelHeight = label.getSize().height;
  97. }
  98. }
  99. // toggle the contents
  100. for (ButtonElement button : buttons) {
  101. button.click();
  102. }
  103. int i = 0;
  104. for (Entry<Integer, ButtonElement> entry : ordered.entrySet()) {
  105. ++i;
  106. ButtonElement button = entry.getValue();
  107. assertThat(
  108. String.format("Unexpected button position: details row %s.",
  109. i),
  110. (double) button.getLocation().getY(),
  111. closeTo(positions.get(button) + (i * labelHeight), 1d));
  112. }
  113. // toggle the contents
  114. for (ButtonElement button : buttons) {
  115. button.click();
  116. }
  117. // assert original positions back
  118. for (ButtonElement button : buttons) {
  119. assertThat(String.format("Unexpected button position."),
  120. (double) button.getLocation().getY(),
  121. closeTo(positions.get(button), 1d));
  122. }
  123. }
  124. private void assertLabelHeight(String id, double expectedHeight) {
  125. // 1px leeway for calculations
  126. assertThat("Unexpected label height.",
  127. (double) $(LabelElement.class).id(id).getSize().height,
  128. closeTo(expectedHeight, 1d));
  129. }
  130. private void assertDetailsRowHeight(int layoutHeight) {
  131. // check that details row height matches layout height (3px leeway)
  132. WebElement detailsRow = findElement(By.className("v-grid-spacer"));
  133. assertThat("Unexpected details row height", (double) layoutHeight,
  134. closeTo(detailsRow.getSize().height, 3d));
  135. }
  136. }