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.

TreeGridDetailsManagerTest.java 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. package com.vaadin.tests.components.treegrid;
  2. import static org.hamcrest.Matchers.greaterThanOrEqualTo;
  3. import static org.hamcrest.number.IsCloseTo.closeTo;
  4. import static org.junit.Assert.assertEquals;
  5. import static org.junit.Assert.assertNotEquals;
  6. import static org.junit.Assert.assertNotNull;
  7. import static org.junit.Assert.assertThat;
  8. import java.util.List;
  9. import org.junit.Test;
  10. import org.openqa.selenium.StaleElementReferenceException;
  11. import org.openqa.selenium.WebDriver;
  12. import org.openqa.selenium.WebElement;
  13. import org.openqa.selenium.support.ui.ExpectedCondition;
  14. import org.openqa.selenium.support.ui.ExpectedConditions;
  15. import com.vaadin.testbench.By;
  16. import com.vaadin.testbench.elements.ButtonElement;
  17. import com.vaadin.testbench.elements.TreeGridElement;
  18. import com.vaadin.tests.tb3.MultiBrowserTest;
  19. public class TreeGridDetailsManagerTest extends MultiBrowserTest {
  20. private static final String CLASSNAME_ERROR = "v-Notification-error";
  21. private static final String CLASSNAME_LABEL = "v-label";
  22. private static final String CLASSNAME_LEAF = "v-treegrid-row-depth-1";
  23. private static final String CLASSNAME_SPACER = "v-treegrid-spacer";
  24. private static final String CLASSNAME_TREEGRID = "v-treegrid";
  25. private static final String EXPAND_ALL = "expandAll";
  26. private static final String COLLAPSE_ALL = "collapseAll";
  27. private static final String SHOW_DETAILS = "showDetails";
  28. private static final String HIDE_DETAILS = "hideDetails";
  29. private static final String ADD_GRID = "addGrid";
  30. private TreeGridElement treeGrid;
  31. private int expectedSpacerHeight = 0;
  32. private int expectedRowHeight = 0;
  33. private ExpectedCondition<Boolean> expectedConditionDetails(final int root,
  34. final int leaf) {
  35. return new ExpectedCondition<Boolean>() {
  36. @Override
  37. public Boolean apply(WebDriver arg0) {
  38. return getSpacer(root, leaf) != null;
  39. }
  40. @Override
  41. public String toString() {
  42. // waiting for...
  43. return String.format(
  44. "Leaf %s/%s details row contents to be found", root,
  45. leaf);
  46. }
  47. };
  48. }
  49. private WebElement getSpacer(final int root, final Integer leaf) {
  50. String text;
  51. if (leaf == null) {
  52. text = "details for Root %s";
  53. } else {
  54. text = "details for Leaf %s/%s";
  55. }
  56. try {
  57. List<WebElement> spacers = treeGrid
  58. .findElements(By.className(CLASSNAME_SPACER));
  59. for (WebElement spacer : spacers) {
  60. List<WebElement> labels = spacer
  61. .findElements(By.className(CLASSNAME_LABEL));
  62. for (WebElement label : labels) {
  63. if (String.format(text, root, leaf)
  64. .equals(label.getText())) {
  65. return spacer;
  66. }
  67. }
  68. }
  69. } catch (StaleElementReferenceException e) {
  70. treeGrid = $(TreeGridElement.class).first();
  71. }
  72. return null;
  73. }
  74. private void ensureExpectedSpacerHeightSet() {
  75. if (expectedSpacerHeight == 0) {
  76. expectedSpacerHeight = treeGrid
  77. .findElement(By.className(CLASSNAME_SPACER)).getSize()
  78. .getHeight();
  79. assertThat((double) expectedSpacerHeight, closeTo(27d, 2d));
  80. }
  81. }
  82. private void assertSpacerCount(int expectedSpacerCount) {
  83. assertEquals("Unexpected amount of spacers.", expectedSpacerCount,
  84. treeGrid.findElements(By.className(CLASSNAME_SPACER)).size());
  85. }
  86. /**
  87. * Asserts that every spacer has the same height.
  88. */
  89. private void assertSpacerHeights() {
  90. List<WebElement> spacers = treeGrid
  91. .findElements(By.className(CLASSNAME_SPACER));
  92. for (WebElement spacer : spacers) {
  93. assertEquals("Unexpected spacer height.", expectedSpacerHeight,
  94. spacer.getSize().getHeight());
  95. }
  96. }
  97. /**
  98. * Asserts that every spacer is at least a row height from the previous one.
  99. * Doesn't check that the spacers are in correct order or rendered properly.
  100. */
  101. private void assertSpacerPositions() {
  102. List<WebElement> spacers = treeGrid
  103. .findElements(By.className(CLASSNAME_SPACER));
  104. WebElement previousSpacer = null;
  105. for (WebElement spacer : spacers) {
  106. if (previousSpacer == null) {
  107. previousSpacer = spacer;
  108. continue;
  109. }
  110. assertThat("Unexpected spacer position.", spacer.getLocation().y,
  111. greaterThanOrEqualTo(previousSpacer.getLocation().y
  112. + expectedSpacerHeight + expectedRowHeight - 1));
  113. previousSpacer = spacer;
  114. }
  115. }
  116. private void assertNoErrors() {
  117. assertEquals("Error notification detected.", 0,
  118. treeGrid.findElements(By.className(CLASSNAME_ERROR)).size());
  119. }
  120. @Test
  121. public void expandAllOpenAllInitialDetails_toggleOne_hideAll() {
  122. openTestURL();
  123. $(ButtonElement.class).id(EXPAND_ALL).click();
  124. $(ButtonElement.class).id(SHOW_DETAILS).click();
  125. $(ButtonElement.class).id(ADD_GRID).click();
  126. waitForElementPresent(By.className(CLASSNAME_TREEGRID));
  127. treeGrid = $(TreeGridElement.class).first();
  128. int spacerCount = 6;
  129. waitUntil(expectedConditionDetails(0, 0));
  130. assertSpacerCount(spacerCount);
  131. ensureExpectedSpacerHeightSet();
  132. assertSpacerPositions();
  133. // toggle one root
  134. treeGrid.collapseWithClick(0);
  135. waitUntil(ExpectedConditions.not(expectedConditionDetails(0, 0)));
  136. assertSpacerCount(spacerCount - 2);
  137. assertSpacerHeights();
  138. assertSpacerPositions();
  139. treeGrid.expandWithClick(0);
  140. waitUntil(expectedConditionDetails(0, 0));
  141. assertSpacerCount(spacerCount);
  142. assertSpacerHeights();
  143. assertSpacerPositions();
  144. // test that repeating the toggle still doesn't change anything
  145. treeGrid.collapseWithClick(0);
  146. waitUntil(ExpectedConditions.not(expectedConditionDetails(0, 0)));
  147. assertSpacerCount(spacerCount - 2);
  148. assertSpacerHeights();
  149. assertSpacerPositions();
  150. treeGrid.expandWithClick(0);
  151. waitUntil(expectedConditionDetails(0, 0));
  152. assertSpacerCount(spacerCount);
  153. assertSpacerHeights();
  154. assertSpacerPositions();
  155. // test that hiding all still won't break things
  156. $(ButtonElement.class).id(HIDE_DETAILS).click();
  157. waitForElementNotPresent(By.className(CLASSNAME_SPACER));
  158. assertNoErrors();
  159. }
  160. @Test
  161. public void expandAllOpenAllInitialDetails_toggleAll() {
  162. openTestURL();
  163. $(ButtonElement.class).id(EXPAND_ALL).click();
  164. $(ButtonElement.class).id(SHOW_DETAILS).click();
  165. $(ButtonElement.class).id(ADD_GRID).click();
  166. waitForElementPresent(By.className(CLASSNAME_TREEGRID));
  167. treeGrid = $(TreeGridElement.class).first();
  168. int spacerCount = 6;
  169. waitUntil(expectedConditionDetails(0, 0));
  170. assertSpacerCount(spacerCount);
  171. ensureExpectedSpacerHeightSet();
  172. assertSpacerPositions();
  173. $(ButtonElement.class).id(COLLAPSE_ALL).click();
  174. waitForElementNotPresent(By.className(CLASSNAME_LEAF));
  175. assertSpacerCount(2);
  176. assertSpacerHeights();
  177. assertSpacerPositions();
  178. $(ButtonElement.class).id(EXPAND_ALL).click();
  179. waitUntil(expectedConditionDetails(0, 0));
  180. assertSpacerCount(spacerCount);
  181. assertSpacerHeights();
  182. assertSpacerPositions();
  183. // test that repeating the toggle still doesn't change anything
  184. $(ButtonElement.class).id(COLLAPSE_ALL).click();
  185. waitForElementNotPresent(By.className(CLASSNAME_LEAF));
  186. assertSpacerCount(2);
  187. assertSpacerHeights();
  188. assertSpacerPositions();
  189. $(ButtonElement.class).id(EXPAND_ALL).click();
  190. waitUntil(expectedConditionDetails(0, 0));
  191. assertSpacerCount(spacerCount);
  192. assertSpacerHeights();
  193. assertSpacerPositions();
  194. assertNoErrors();
  195. }
  196. @Test
  197. public void expandAllOpenNoInitialDetails_showAlmostAll_toggleOneByOne() {
  198. openTestURL();
  199. $(ButtonElement.class).id(EXPAND_ALL).click();
  200. $(ButtonElement.class).id(ADD_GRID).click();
  201. waitForElementPresent(By.className(CLASSNAME_TREEGRID));
  202. treeGrid = $(TreeGridElement.class).first();
  203. // expand almost all rows, leave one out from the hierarchy that is to
  204. // be collapsed
  205. treeGrid.getCell(0, 0).click();
  206. treeGrid.getCell(1, 0).click();
  207. treeGrid.getCell(3, 0).click();
  208. treeGrid.getCell(4, 0).click();
  209. treeGrid.getCell(5, 0).click();
  210. int spacerCount = 5;
  211. waitUntil(expectedConditionDetails(0, 0));
  212. assertSpacerCount(spacerCount);
  213. ensureExpectedSpacerHeightSet();
  214. assertSpacerPositions();
  215. treeGrid.collapseWithClick(0);
  216. waitUntil(ExpectedConditions.not(expectedConditionDetails(0, 0)));
  217. assertSpacerCount(spacerCount - 1);
  218. assertSpacerHeights();
  219. assertSpacerPositions();
  220. treeGrid.expandWithClick(0);
  221. waitUntil(expectedConditionDetails(0, 0));
  222. assertSpacerCount(spacerCount);
  223. assertSpacerHeights();
  224. assertSpacerPositions();
  225. assertNotNull(getSpacer(1, 0));
  226. treeGrid.collapseWithClick(3);
  227. waitUntil(ExpectedConditions.not(expectedConditionDetails(1, 0)));
  228. assertSpacerCount(spacerCount - 2);
  229. assertSpacerHeights();
  230. assertSpacerPositions();
  231. treeGrid.expandWithClick(3);
  232. waitUntil(expectedConditionDetails(1, 0));
  233. assertSpacerCount(spacerCount);
  234. assertSpacerHeights();
  235. assertSpacerPositions();
  236. assertNoErrors();
  237. }
  238. @Test
  239. public void expandAllOpenAllInitialDetails_hideOne() {
  240. openTestURL();
  241. $(ButtonElement.class).id(EXPAND_ALL).click();
  242. $(ButtonElement.class).id(SHOW_DETAILS).click();
  243. $(ButtonElement.class).id(ADD_GRID).click();
  244. waitForElementPresent(By.className(CLASSNAME_TREEGRID));
  245. treeGrid = $(TreeGridElement.class).first();
  246. // check the position of a row
  247. int oldY = treeGrid.getCell(2, 0).getLocation().getY();
  248. // hide the spacer from previous row
  249. treeGrid.getCell(1, 0).click();
  250. // ensure the investigated row moved
  251. assertNotEquals(oldY, treeGrid.getCell(2, 0).getLocation().getY());
  252. }
  253. }