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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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 com.vaadin.testbench.TestBenchElement;
  9. import com.vaadin.testbench.elements.TreeElement;
  10. import com.vaadin.tests.tb3.MultiBrowserTest;
  11. public class TreeBasicFeaturesTest extends MultiBrowserTest {
  12. private static final Predicate<TestBenchElement> THEME_RESOURCE = e -> {
  13. return e.isElementPresent(By.tagName("img"))
  14. && e.findElement(By.tagName("img")).getAttribute("src")
  15. .contains("bullet.png");
  16. };
  17. private static final Predicate<TestBenchElement> VAADIN_ICON = e -> {
  18. return e.isElementPresent(By.className("v-icon"))
  19. && e.findElement(By.className("v-icon")).getAttribute("class")
  20. .contains("Vaadin-Icons");
  21. };
  22. private static final Predicate<TestBenchElement> CLASS_RESOURCE = e -> {
  23. return e.isElementPresent(By.tagName("img"))
  24. && e.findElement(By.tagName("img")).getAttribute("src")
  25. .contains("m.gif");
  26. };
  27. @Before
  28. public void before() {
  29. setDebug(true);
  30. openTestURL();
  31. }
  32. @Test
  33. public void tree_expand_and_collapse() {
  34. TreeElement tree = $(TreeElement.class).first();
  35. tree.expand(0);
  36. Assert.assertEquals("1 | 0", tree.getItem(1).getText());
  37. tree.collapse(0);
  38. Assert.assertEquals("0 | 1", tree.getItem(1).getText());
  39. assertNoErrorNotifications();
  40. }
  41. @Test
  42. public void tree_expand_all() throws IOException {
  43. expandAll();
  44. assertAllExpanded(false);
  45. assertNoErrorNotifications();
  46. }
  47. @Test
  48. public void tree_expand_all_with_icons() throws IOException {
  49. selectMenuPath("Component", "Icons", "By Depth");
  50. Assert.assertTrue("Icon not present", $(TreeElement.class).first()
  51. .getItem(0).isElementPresent(By.tagName("img")));
  52. expandAll();
  53. assertAllExpanded(true);
  54. assertNoErrorNotifications();
  55. }
  56. private void expandAll() {
  57. TreeElement tree = $(TreeElement.class).first();
  58. for (int i = 0; i < 2; ++i) {
  59. int max = tree.getAllItems().size();
  60. for (int j = 1; j <= max; ++j) {
  61. if (tree.isExpanded(max - j)) {
  62. continue;
  63. }
  64. tree.expand(max - j);
  65. }
  66. }
  67. }
  68. private void assertAllExpanded(boolean shouldHaveIcon) {
  69. TreeElement tree = $(TreeElement.class).first();
  70. TestBenchElement item;
  71. int n = 0;
  72. for (int i = 0; i < 3; ++i) {
  73. item = tree.getItem(n++);
  74. Assert.assertEquals("0 | " + i, item.getText());
  75. Assert.assertEquals("Unexpected icon state", shouldHaveIcon,
  76. THEME_RESOURCE.test(item));
  77. for (int j = 0; j < 3; ++j) {
  78. item = tree.getItem(n++);
  79. Assert.assertEquals((shouldHaveIcon ? " " : "") + "1 | " + j,
  80. item.getText());
  81. Assert.assertEquals("Unexpected icon state", shouldHaveIcon,
  82. VAADIN_ICON.test(item));
  83. for (int k = 0; k < 3; ++k) {
  84. item = tree.getItem(n++);
  85. Assert.assertEquals("2 | " + k, item.getText());
  86. Assert.assertEquals("Unexpected icon state", shouldHaveIcon,
  87. CLASS_RESOURCE.test(item));
  88. }
  89. }
  90. }
  91. }
  92. @Test
  93. public void tree_custom_caption() {
  94. selectMenuPath("Component", "Captions", "Custom caption");
  95. TreeElement tree = $(TreeElement.class).first();
  96. Assert.assertEquals("Id: /0/0, Depth: 0, Index: 0",
  97. tree.getItem(0).getText());
  98. Assert.assertEquals("Id: /0/1, Depth: 0, Index: 1",
  99. tree.getItem(1).getText());
  100. tree.expand(0);
  101. Assert.assertEquals("Id: /0/0/1/0, Depth: 1, Index: 0",
  102. tree.getItem(1).getText());
  103. Assert.assertEquals("Id: /0/0/1/1, Depth: 1, Index: 1",
  104. tree.getItem(2).getText());
  105. tree.expand(1);
  106. Assert.assertEquals("Id: /0/0/1/0/2/0, Depth: 2, Index: 0",
  107. tree.getItem(2).getText());
  108. Assert.assertEquals("Id: /0/0/1/0/2/1, Depth: 2, Index: 1",
  109. tree.getItem(3).getText());
  110. assertNoErrorNotifications();
  111. }
  112. }