diff options
author | Adam Wagner <wbadam@users.noreply.github.com> | 2018-02-08 11:20:32 +0200 |
---|---|---|
committer | Teemu Suo-Anttila <tsuoanttila@users.noreply.github.com> | 2018-02-08 11:20:32 +0200 |
commit | 1817a82ed8f3c8839c18d9ce6b68e5838940a22c (patch) | |
tree | 3a110ba043aa4bea415b4060c4eba4439703711b /uitest | |
parent | 299520fac11dac9b31d6fd762a3eb76ef827ec5b (diff) | |
download | vaadin-framework-1817a82ed8f3c8839c18d9ce6b68e5838940a22c.tar.gz vaadin-framework-1817a82ed8f3c8839c18d9ce6b68e5838940a22c.zip |
Add recursive expand and collapse method to TreeGrid and Tree (#10283)
Diffstat (limited to 'uitest')
5 files changed, 242 insertions, 1 deletions
diff --git a/uitest/src/main/java/com/vaadin/tests/components/treegrid/LazyHierarchicalDataProvider.java b/uitest/src/main/java/com/vaadin/tests/components/treegrid/LazyHierarchicalDataProvider.java index a468f34d29..014ee7d1f0 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/treegrid/LazyHierarchicalDataProvider.java +++ b/uitest/src/main/java/com/vaadin/tests/components/treegrid/LazyHierarchicalDataProvider.java @@ -49,7 +49,8 @@ public class LazyHierarchicalDataProvider extends .flatMap(parent -> Optional.of(parent.getId())); List<HierarchicalTestBean> list = new ArrayList<>(); - for (int i = 0; i < query.getLimit(); i++) { + int limit = Math.min(query.getLimit(), nodesPerLevel); + for (int i = 0; i < limit; i++) { list.add(new HierarchicalTestBean(parentKey.orElse(null), depth, i + query.getOffset())); } diff --git a/uitest/src/main/java/com/vaadin/tests/components/treegrid/TreeGridBasicFeatures.java b/uitest/src/main/java/com/vaadin/tests/components/treegrid/TreeGridBasicFeatures.java index 2ac0c0673d..c12baf3900 100644 --- a/uitest/src/main/java/com/vaadin/tests/components/treegrid/TreeGridBasicFeatures.java +++ b/uitest/src/main/java/com/vaadin/tests/components/treegrid/TreeGridBasicFeatures.java @@ -180,6 +180,11 @@ public class TreeGridBasicFeatures extends AbstractComponentTest<TreeGrid> { createClickAction("Expand 2 | 1", "Server-side expand", (treeGrid, value, data) -> treeGrid.expand(value), new HierarchicalTestBean("/0/0/1/1", 2, 1)); + + createClickAction("Expand 0 | 0 recursively", "Server-side expand", + (treeGrid, value, data) -> treeGrid + .expandRecursively(Arrays.asList(value), 1), + new HierarchicalTestBean(null, 0, 0)); } @SuppressWarnings("unchecked") @@ -194,6 +199,11 @@ public class TreeGridBasicFeatures extends AbstractComponentTest<TreeGrid> { createClickAction("Collapse 2 | 1", "Server-side collapse", (treeGrid, value, data) -> treeGrid.collapse(value), new HierarchicalTestBean("/0/0/1/1", 2, 1)); + + createClickAction("Collapse 0 | 0 recursively", "Server-side collapse", + (treeGrid, value, data) -> treeGrid + .collapseRecursively(Arrays.asList(value), 2), + new HierarchicalTestBean(null, 0, 0)); } @SuppressWarnings("unchecked") diff --git a/uitest/src/main/java/com/vaadin/tests/components/treegrid/TreeGridExpandCollapseRecursively.java b/uitest/src/main/java/com/vaadin/tests/components/treegrid/TreeGridExpandCollapseRecursively.java new file mode 100644 index 0000000000..f85eba5815 --- /dev/null +++ b/uitest/src/main/java/com/vaadin/tests/components/treegrid/TreeGridExpandCollapseRecursively.java @@ -0,0 +1,106 @@ +package com.vaadin.tests.components.treegrid; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; + +import com.vaadin.annotations.Widgetset; +import com.vaadin.server.VaadinRequest; +import com.vaadin.tests.components.AbstractTestUI; +import com.vaadin.ui.Button; +import com.vaadin.ui.HorizontalLayout; +import com.vaadin.ui.RadioButtonGroup; +import com.vaadin.ui.TreeGrid; + +@Widgetset("com.vaadin.DefaultWidgetSet") +public class TreeGridExpandCollapseRecursively extends AbstractTestUI { + + private static class Directory { + + private String name; + private Directory parent; + private List<Directory> subDirectories = new ArrayList<>(); + + public Directory(String name, Directory parent) { + this.name = name; + this.parent = parent; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Directory getParent() { + return parent; + } + + public void setParent( + Directory parent) { + this.parent = parent; + } + + public List<Directory> getSubDirectories() { + return subDirectories; + } + + public void setSubDirectories(List<Directory> subDirectories) { + this.subDirectories = subDirectories; + } + } + + private static final int DEPTH = 4; + private static final int CHILDREN = 5; + + @Override + protected void setup(VaadinRequest request) { + + Collection<Directory> roots = generateDirectoryStructure(DEPTH); + + TreeGrid<Directory> grid = new TreeGrid<>(); + grid.addColumn(item -> "Item" + item.getName()); + + grid.setItems(roots, Directory::getSubDirectories); + + RadioButtonGroup<Integer> depthSelector = new RadioButtonGroup<>( + "Depth", Arrays.asList(0, 1, 2, 3)); + depthSelector.addStyleName("horizontal"); + depthSelector.setValue(3); + + HorizontalLayout buttons = new HorizontalLayout(); + buttons.addComponent(new Button("Expand recursively", e -> grid + .expandRecursively(roots, depthSelector.getValue()))); + buttons.addComponent(new Button("Collapse recursively", e -> grid + .collapseRecursively(roots, depthSelector.getValue()))); + + addComponents(depthSelector, buttons, grid); + } + + private Collection<Directory> generateDirectoryStructure(int depth) { + return generateDirectories(depth, null, CHILDREN); + } + + private Collection<Directory> generateDirectories(int depth, + Directory parent, int childCount) { + Collection<Directory> dirs = new ArrayList<>(); + if (depth >= 0) { + for (int i = 0; i < childCount; i++) { + String name = parent != null + ? parent.getName() + "-" + i + : "-" + i; + Directory dir = new Directory(name, parent); + if (parent != null) { + parent.getSubDirectories().add(dir); + } + dirs.add(dir); + + generateDirectories(depth - 1, dir, childCount); + } + } + return dirs; + } +} diff --git a/uitest/src/test/java/com/vaadin/tests/components/treegrid/TreeGridBasicFeaturesTest.java b/uitest/src/test/java/com/vaadin/tests/components/treegrid/TreeGridBasicFeaturesTest.java index 43c08415df..a96e2d3c1d 100644 --- a/uitest/src/test/java/com/vaadin/tests/components/treegrid/TreeGridBasicFeaturesTest.java +++ b/uitest/src/test/java/com/vaadin/tests/components/treegrid/TreeGridBasicFeaturesTest.java @@ -95,6 +95,25 @@ public class TreeGridBasicFeaturesTest extends MultiBrowserTest { assertEquals(3, grid.getRowCount()); assertCellTexts(0, 0, new String[] { "0 | 0", "0 | 1", "0 | 2" }); + // expand 0 | 0 recursively + selectMenuPath("Component", "Features", "Server-side expand", + "Expand 0 | 0 recursively"); + assertEquals(15, grid.getRowCount()); + assertCellTexts(0, 0, new String[] { "0 | 0", "1 | 0", "2 | 0" }); + + // collapse 0 | 0 recursively + selectMenuPath("Component", "Features", "Server-side collapse", + "Collapse 0 | 0 recursively"); + assertEquals(3, grid.getRowCount()); + assertCellTexts(0, 0, new String[] { "0 | 0", "0 | 1", "0 | 2" }); + + // expanding 0 | 0 should result in 3 additional nodes after recursive + // collapse + selectMenuPath("Component", "Features", "Server-side expand", + "Expand 0 | 0"); + assertEquals(6, grid.getRowCount()); + assertCellTexts(1, 0, new String[] { "1 | 0", "1 | 1", "1 | 2" }); + assertNoSystemNotifications(); assertNoErrorNotifications(); } diff --git a/uitest/src/test/java/com/vaadin/tests/components/treegrid/TreeGridExpandCollapseRecursivelyTest.java b/uitest/src/test/java/com/vaadin/tests/components/treegrid/TreeGridExpandCollapseRecursivelyTest.java new file mode 100644 index 0000000000..6e2451b96a --- /dev/null +++ b/uitest/src/test/java/com/vaadin/tests/components/treegrid/TreeGridExpandCollapseRecursivelyTest.java @@ -0,0 +1,105 @@ +package com.vaadin.tests.components.treegrid; + +import org.junit.Before; +import org.junit.Test; + +import com.vaadin.testbench.elements.ButtonElement; +import com.vaadin.testbench.elements.RadioButtonGroupElement; +import com.vaadin.testbench.elements.TreeGridElement; +import com.vaadin.tests.tb3.SingleBrowserTest; + +import static org.junit.Assert.assertEquals; + +public class TreeGridExpandCollapseRecursivelyTest extends SingleBrowserTest { + + private static final int rowCount0 = 5; + private static final int rowCount1 = rowCount0 + rowCount0 * 5; + private static final int rowCount2 = + rowCount1 + (rowCount1 - rowCount0) * 5; + private static final int rowCount3 = + rowCount2 + (rowCount2 - rowCount1) * 5; + private static final int rowCount4 = + rowCount3 + (rowCount3 - rowCount2) * 5; + + private TreeGridElement grid; + private RadioButtonGroupElement depthSelector; + private ButtonElement expandButton; + private ButtonElement collapseButton; + + @Before + public void before() { + openTestURL(); + grid = $(TreeGridElement.class).first(); + depthSelector = $(RadioButtonGroupElement.class).first(); + expandButton = $(ButtonElement.class).get(0); + collapseButton = $(ButtonElement.class).get(1); + } + + @Test + public void expandVariousDepth() { + assertEquals(rowCount0, grid.getRowCount()); + + selectDepth(0); + expandButton.click(); + + assertEquals(rowCount1, grid.getRowCount()); + + selectDepth(1); + expandButton.click(); + + assertEquals(rowCount2, grid.getRowCount()); + + selectDepth(2); + expandButton.click(); + + assertEquals(rowCount3, grid.getRowCount()); + + selectDepth(3); + expandButton.click(); + + assertEquals(rowCount4, grid.getRowCount()); + } + + @Test(timeout = 5000) + public void expandAndCollapseAllItems() { + assertEquals(rowCount0, grid.getRowCount()); + + selectDepth(3); + expandButton.click(); + + assertEquals(rowCount4, grid.getRowCount()); + + collapseButton.click(); + + assertEquals(rowCount0, grid.getRowCount()); + } + + @Test + public void partialCollapse() { + assertEquals(rowCount0, grid.getRowCount()); + + selectDepth(3); + expandButton.click(); + + assertEquals(rowCount4, grid.getRowCount()); + + selectDepth(1); + collapseButton.click(); + + assertEquals(rowCount0, grid.getRowCount()); + + selectDepth(0); + expandButton.click(); + + assertEquals(rowCount1, grid.getRowCount()); + + // Open just one subtree to see if it is still fully expanded + grid.getExpandElement(2, 0).click(); + + assertEquals(rowCount1 + rowCount2, grid.getRowCount()); + } + + private void selectDepth(int depth) { + depthSelector.setValue(String.valueOf(depth)); + } +} |