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 7.6KB

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