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.

TreeBasicFeaturesTest.java 9.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. package com.vaadin.tests.components.tree;
  2. import java.io.IOException;
  3. import java.util.Arrays;
  4. import java.util.function.Predicate;
  5. import org.junit.Assert;
  6. import org.junit.Before;
  7. import org.junit.Test;
  8. import org.openqa.selenium.By;
  9. import org.openqa.selenium.Keys;
  10. import org.openqa.selenium.interactions.Actions;
  11. import com.vaadin.testbench.TestBenchElement;
  12. import com.vaadin.testbench.elements.TreeElement;
  13. import com.vaadin.testbench.elements.TreeGridElement;
  14. import com.vaadin.tests.tb3.MultiBrowserTest;
  15. public class TreeBasicFeaturesTest extends MultiBrowserTest {
  16. private static final Predicate<TestBenchElement> THEME_RESOURCE = e -> {
  17. return e.isElementPresent(By.tagName("img"))
  18. && e.findElement(By.tagName("img")).getAttribute("src")
  19. .contains("bullet.png");
  20. };
  21. private static final Predicate<TestBenchElement> VAADIN_ICON = e -> {
  22. return e.isElementPresent(By.className("v-icon"))
  23. && e.findElement(By.className("v-icon")).getAttribute("class")
  24. .contains("Vaadin-Icons");
  25. };
  26. private static final Predicate<TestBenchElement> CLASS_RESOURCE = e -> {
  27. return e.isElementPresent(By.tagName("img"))
  28. && e.findElement(By.tagName("img")).getAttribute("src")
  29. .contains("m.gif");
  30. };
  31. @Before
  32. public void before() {
  33. setDebug(true);
  34. openTestURL();
  35. }
  36. @Test
  37. public void tree_expand_and_collapse() {
  38. TreeElement tree = $(TreeElement.class).first();
  39. tree.expand(0);
  40. Assert.assertEquals("1 | 0", tree.getItem(1).getText());
  41. tree.collapse(0);
  42. Assert.assertEquals("0 | 1", tree.getItem(1).getText());
  43. assertNoErrorNotifications();
  44. }
  45. @Test
  46. public void tree_expand_all() throws IOException {
  47. expandAll();
  48. assertAllExpanded(false);
  49. assertNoErrorNotifications();
  50. }
  51. @Test
  52. public void tree_expand_all_with_icons() throws IOException {
  53. selectMenuPath("Component", "Icons", "By Depth");
  54. Assert.assertTrue("Icon not present", $(TreeElement.class).first()
  55. .getItem(0).isElementPresent(By.tagName("img")));
  56. expandAll();
  57. assertAllExpanded(true);
  58. assertNoErrorNotifications();
  59. }
  60. private void expandAll() {
  61. TreeElement tree = $(TreeElement.class).first();
  62. for (int i = 0; i < 2; ++i) {
  63. int max = tree.getAllItems().size();
  64. for (int j = 1; j <= max; ++j) {
  65. if (tree.isExpanded(max - j)) {
  66. continue;
  67. }
  68. tree.expand(max - j);
  69. }
  70. }
  71. }
  72. private void assertAllExpanded(boolean shouldHaveIcon) {
  73. TreeElement tree = $(TreeElement.class).first();
  74. TestBenchElement item;
  75. int n = 0;
  76. for (int i = 0; i < 3; ++i) {
  77. item = tree.getItem(n++);
  78. Assert.assertEquals("0 | " + i, item.getText());
  79. Assert.assertEquals("Unexpected icon state", shouldHaveIcon,
  80. THEME_RESOURCE.test(item));
  81. for (int j = 0; j < 3; ++j) {
  82. item = tree.getItem(n++);
  83. Assert.assertEquals(
  84. (shouldHaveIcon ? "\ue92d" : "") + "1 | " + j,
  85. item.getText());
  86. Assert.assertEquals("Unexpected icon state", shouldHaveIcon,
  87. VAADIN_ICON.test(item));
  88. for (int k = 0; k < 3; ++k) {
  89. item = tree.getItem(n++);
  90. Assert.assertEquals("2 | " + k, item.getText());
  91. Assert.assertEquals("Unexpected icon state", shouldHaveIcon,
  92. CLASS_RESOURCE.test(item));
  93. }
  94. }
  95. }
  96. }
  97. @Test
  98. public void tree_custom_caption() {
  99. // Set row height big enough to show whole content.
  100. selectMenuPath("Component", "Row Height",
  101. String.valueOf(TreeBasicFeatures.ROW_HEIGHTS[1]));
  102. selectMenuPath("Component", "Captions", "Custom caption");
  103. TreeElement tree = $(TreeElement.class).first();
  104. Assert.assertEquals("Id: /0/0\nDepth: 0, Index: 0",
  105. tree.getItem(0).getText());
  106. Assert.assertEquals("Id: /0/1\nDepth: 0, Index: 1",
  107. tree.getItem(1).getText());
  108. tree.expand(0);
  109. Assert.assertEquals("Id: /0/0/1/0\nDepth: 1, Index: 0",
  110. tree.getItem(1).getText());
  111. Assert.assertEquals("Id: /0/0/1/1\nDepth: 1, Index: 1",
  112. tree.getItem(2).getText());
  113. tree.expand(1);
  114. Assert.assertEquals("Id: /0/0/1/0/2/0\nDepth: 2, Index: 0",
  115. tree.getItem(2).getText());
  116. Assert.assertEquals("Id: /0/0/1/0/2/1\nDepth: 2, Index: 1",
  117. tree.getItem(3).getText());
  118. assertNoErrorNotifications();
  119. }
  120. @Test
  121. public void tree_html_caption_and_expander_position() {
  122. // Set row height big enough to show whole content.
  123. selectMenuPath("Component", "Row Height",
  124. String.valueOf(TreeBasicFeatures.ROW_HEIGHTS[1]));
  125. selectMenuPath("Component", "Captions", "HTML caption");
  126. TreeElement tree = $(TreeElement.class).first();
  127. Assert.assertEquals("Id: /0/0\nDepth: 0\nIndex: 0",
  128. tree.getItem(0).getText());
  129. Assert.assertEquals("Expander element not aligned to top",
  130. tree.getExpandElement(0).getLocation().getY(),
  131. tree.getItem(0).getLocation().getY());
  132. assertNoErrorNotifications();
  133. }
  134. @Test
  135. public void tree_html_caption_text_mode() {
  136. // Set row height big enough to show whole content.
  137. selectMenuPath("Component", "Captions", "HTML caption");
  138. selectMenuPath("Component", "ContentMode", "TEXT");
  139. TreeElement tree = $(TreeElement.class).first();
  140. Assert.assertEquals("Id: /0/0<br/>Depth: 0<br/>Index: 0",
  141. tree.getItem(0).getText());
  142. assertNoErrorNotifications();
  143. }
  144. @Test
  145. public void tree_item_click() {
  146. selectMenuPath("Component", "Item Click Listener");
  147. $(TreeElement.class).first().getItem(1).click();
  148. Assert.assertTrue(logContainsText("ItemClick: 0 | 1"));
  149. }
  150. @Test
  151. public void tree_style_generator() {
  152. selectMenuPath("Component", "Style Generator");
  153. TreeElement tree = $(TreeElement.class).first();
  154. Assert.assertTrue("Style name not present",
  155. tree.wrap(TreeGridElement.class).getRow(0).getAttribute("class")
  156. .contains("level0"));
  157. tree.expand(0);
  158. Assert.assertTrue("Style name not present",
  159. tree.wrap(TreeGridElement.class).getRow(1).getAttribute("class")
  160. .contains("level1"));
  161. tree.expand(1);
  162. Assert.assertTrue("Style name not present",
  163. tree.wrap(TreeGridElement.class).getRow(2).getAttribute("class")
  164. .contains("level2"));
  165. }
  166. @Test
  167. public void tree_disable_collapse() {
  168. selectMenuPath("Component", "Collapse Allowed");
  169. TreeElement tree = $(TreeElement.class).first();
  170. tree.expand(0);
  171. tree.expand(1);
  172. Assert.assertEquals("2 | 0", tree.getItem(2).getText());
  173. tree.collapse(1);
  174. Assert.assertEquals("Tree should prevent collapsing all nodes.",
  175. "2 | 0", tree.getItem(2).getText());
  176. }
  177. @Test
  178. public void tree_multiselect() {
  179. selectMenuPath("Component", "Selection Mode", "MULTI");
  180. TreeElement tree = $(TreeElement.class).first();
  181. tree.getItem(0).click();
  182. TreeGridElement wrap = tree.wrap(TreeGridElement.class);
  183. Assert.assertFalse(
  184. "Tree MultiSelection shouldn't have selection column",
  185. wrap.getCell(0, 0).isElementPresent(By.tagName("input")));
  186. Assert.assertTrue("First row was not selected",
  187. wrap.getRow(0).isSelected());
  188. new Actions(getDriver()).sendKeys(Keys.ARROW_DOWN, Keys.SPACE)
  189. .perform();
  190. Assert.assertTrue("First row was deselected",
  191. wrap.getRow(0).isSelected());
  192. Assert.assertTrue("Second row was not selected",
  193. wrap.getRow(1).isSelected());
  194. }
  195. @Test
  196. public void tree_multiselect_click() {
  197. selectMenuPath("Component", "Selection Mode", "MULTI");
  198. TreeElement tree = $(TreeElement.class).first();
  199. TreeGridElement wrap = tree.wrap(TreeGridElement.class);
  200. tree.getItem(0).click();
  201. Assert.assertTrue("First row was not selected",
  202. wrap.getRow(0).isSelected());
  203. tree.getItem(1).click();
  204. Assert.assertTrue("First row was deselected",
  205. wrap.getRow(0).isSelected());
  206. Assert.assertTrue("Second row was not selected",
  207. wrap.getRow(1).isSelected());
  208. tree.getItem(0).click();
  209. Assert.assertFalse("First row was not deselected",
  210. wrap.getRow(0).isSelected());
  211. }
  212. @Test
  213. public void tree_row_heigth() {
  214. TreeElement tree = $(TreeElement.class).first();
  215. TreeGridElement wrap = tree.wrap(TreeGridElement.class);
  216. Arrays.stream(TreeBasicFeatures.ROW_HEIGHTS).boxed()
  217. .map(String::valueOf).forEach(height -> {
  218. selectMenuPath("Component", "Row Height", height);
  219. Assert.assertTrue(wrap.getCell(0, 0).getAttribute("style")
  220. .contains("height: " + height + "px;"));
  221. });
  222. }
  223. }