diff options
author | Aleksi Hietanen <aleksi@vaadin.com> | 2017-04-06 13:09:31 +0300 |
---|---|---|
committer | Ilia Motornyi <elmot@vaadin.com> | 2017-04-06 12:09:31 +0200 |
commit | 6ad53c7d6679cc60961d07ed0766b679795ac8a8 (patch) | |
tree | 0c5ff17c7662bb6c858571553f7e902e8ae3b432 /uitest | |
parent | 6eed666314e1f9987bc6f52b4f2901ff7c0018b3 (diff) | |
download | vaadin-framework-6ad53c7d6679cc60961d07ed0766b679795ac8a8.tar.gz vaadin-framework-6ad53c7d6679cc60961d07ed0766b679795ac8a8.zip |
Add server-side expand and collapse to TreeGrid (#9021)
* Add server-side expand and collapse to TreeGrid
* Add javadocs
* Fix variable naming in TreeGridHugeTreeTest
* Fix review comments
* Merge remote-tracking branch 'github/master' into 8759-server-expand
* Clear pending expands when all data is dropped
* Add documentation
Diffstat (limited to 'uitest')
4 files changed, 254 insertions, 27 deletions
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 ae011355c9..a8ce0a7489 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 @@ -63,6 +63,8 @@ public class TreeGridBasicFeatures extends AbstractComponentTest<TreeGrid> { createDataProviderSelect(); createHierarchyColumnSelect(); createCollapseAllowedSelect(); + createExpandMenu(); + createCollapseMenu(); createListenerMenu(); } @@ -146,13 +148,45 @@ public class TreeGridBasicFeatures extends AbstractComponentTest<TreeGrid> { } @SuppressWarnings("unchecked") + private void createExpandMenu() { + createCategory("Server-side expand", CATEGORY_FEATURES); + createClickAction("Expand 0 | 0", "Server-side expand", + (treeGrid, value, data) -> treeGrid.expand(value), + new HierarchicalTestBean(null, 0, 0)); + createClickAction("Expand 1 | 1", "Server-side expand", + (treeGrid, value, data) -> treeGrid.expand(value), + new HierarchicalTestBean("/0/0", 1, 1)); + createClickAction("Expand 2 | 1", "Server-side expand", + (treeGrid, value, data) -> treeGrid.expand(value), + new HierarchicalTestBean("/0/0/1/1", 2, 1)); + } + + @SuppressWarnings("unchecked") + private void createCollapseMenu() { + createCategory("Server-side collapse", CATEGORY_FEATURES); + createClickAction("Collapse 0 | 0", "Server-side collapse", + (treeGrid, value, data) -> treeGrid.collapse(value), + new HierarchicalTestBean(null, 0, 0)); + createClickAction("Collapse 1 | 1", "Server-side collapse", + (treeGrid, value, data) -> treeGrid.collapse(value), + new HierarchicalTestBean("/0/0", 1, 1)); + createClickAction("Collapse 2 | 1", "Server-side collapse", + (treeGrid, value, data) -> treeGrid.collapse(value), + new HierarchicalTestBean("/0/0/1/1", 2, 1)); + } + + @SuppressWarnings("unchecked") private void createListenerMenu() { createListenerAction("Collapse listener", "State", treeGrid -> treeGrid.addCollapseListener(event -> log( - "Item collapsed: " + event.getCollapsedItem()))); + "Item collapsed (user originated: " + + event.isUserOriginated() + "): " + + event.getCollapsedItem()))); createListenerAction("Expand listener", "State", treeGrid -> treeGrid.addExpandListener(event -> log( - "Item expanded: " + event.getExpandedItem()))); + "Item expanded (user originated: " + + event.isUserOriginated() + "): " + + event.getExpandedItem()))); } static class HierarchicalTestBean { diff --git a/uitest/src/main/java/com/vaadin/tests/components/treegrid/TreeGridHugeTree.java b/uitest/src/main/java/com/vaadin/tests/components/treegrid/TreeGridHugeTree.java new file mode 100644 index 0000000000..7cfb926849 --- /dev/null +++ b/uitest/src/main/java/com/vaadin/tests/components/treegrid/TreeGridHugeTree.java @@ -0,0 +1,57 @@ +package com.vaadin.tests.components.treegrid; + +import com.vaadin.annotations.Theme; +import com.vaadin.annotations.Widgetset; +import com.vaadin.data.HierarchyData; +import com.vaadin.data.provider.InMemoryHierarchicalDataProvider; +import com.vaadin.server.VaadinRequest; +import com.vaadin.tests.components.AbstractTestUI; +import com.vaadin.ui.Button; +import com.vaadin.ui.TreeGrid; + +@Theme("valo") +@Widgetset("com.vaadin.DefaultWidgetSet") +public class TreeGridHugeTree + extends AbstractTestUI { + + private TreeGrid<String> treeGrid; + private InMemoryHierarchicalDataProvider<String> inMemoryDataProvider; + + private void initializeDataProvider() { + HierarchyData<String> data = new HierarchyData<>(); + for (int i = 0; i < 3; i++) { + String granddad = "Granddad " + i; + data.addItem(null, granddad); + for (int j = 0; j < 3; j++) { + String dad = "Dad " + i + "/" + j; + data.addItem(granddad, dad); + for (int k = 0; k < 300; k++) { + String son = "Son " + i + "/" + j + "/" + k; + data.addItem(dad, son); + } + } + } + inMemoryDataProvider = new InMemoryHierarchicalDataProvider<>(data); + } + + @Override + protected void setup(VaadinRequest request) { + initializeDataProvider(); + treeGrid = new TreeGrid<>(); + treeGrid.setDataProvider(inMemoryDataProvider); + treeGrid.setSizeFull(); + treeGrid.addColumn(String::toString).setCaption("String") + .setId("string"); + treeGrid.addColumn((i) -> "--").setCaption("Nothing"); + treeGrid.setHierarchyColumn("string"); + treeGrid.setId("testComponent"); + + Button expand = new Button("Expand Granddad 1"); + expand.addClickListener(event -> treeGrid.expand("Granddad 1")); + Button collapse = new Button("Collapse Granddad 1"); + collapse.addClickListener(event -> treeGrid.collapse("Granddad 1")); + + addComponents(treeGrid, expand, collapse); + } + +}
\ No newline at end of file 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 7fb7ddf3f3..dd4546c6c0 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 @@ -1,12 +1,13 @@ package com.vaadin.tests.components.treegrid; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + import java.util.Arrays; import java.util.Collection; -import java.util.List; -import com.vaadin.testbench.parallel.Browser; import org.junit.Before; -import org.junit.Ignore; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized.Parameters; @@ -18,10 +19,6 @@ import com.vaadin.testbench.elements.TreeGridElement; import com.vaadin.tests.tb3.MultiBrowserTest; import com.vaadin.tests.tb3.ParameterizedTB3Runner; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; - @RunWith(ParameterizedTB3Runner.class) public class TreeGridBasicFeaturesTest extends MultiBrowserTest { @@ -41,28 +38,82 @@ public class TreeGridBasicFeaturesTest extends MultiBrowserTest { @Before public void before() { setDebug(true); - openTestURL("theme=valo"); + openTestURL(); grid = $(TreeGridElement.class).first(); } @Test - @Ignore // currently no implementation exists for toggling from the server - // side public void toggle_collapse_server_side() { assertEquals(3, grid.getRowCount()); assertCellTexts(0, 0, new String[] { "0 | 0", "0 | 1", "0 | 2" }); - selectMenuPath("Component", "Features", "Toggle expand", "0 | 0"); + selectMenuPath("Component", "Features", "Server-side expand", + "Expand 0 | 0"); + assertEquals(6, grid.getRowCount()); + assertCellTexts(1, 0, new String[] { "1 | 0", "1 | 1", "1 | 2" }); + + // expanding already expanded item should have no effect + selectMenuPath("Component", "Features", "Server-side expand", + "Expand 0 | 0"); assertEquals(6, grid.getRowCount()); assertCellTexts(1, 0, new String[] { "1 | 0", "1 | 1", "1 | 2" }); - selectMenuPath("Component", "Features", "Toggle expand", "0 | 0"); + selectMenuPath("Component", "Features", "Server-side collapse", + "Collapse 0 | 0"); + assertEquals(3, grid.getRowCount()); + assertCellTexts(0, 0, new String[] { "0 | 0", "0 | 1", "0 | 2" }); + + // collapsing the same item twice should have no effect + selectMenuPath("Component", "Features", "Server-side collapse", + "Collapse 0 | 0"); + assertEquals(3, grid.getRowCount()); + assertCellTexts(0, 0, new String[] { "0 | 0", "0 | 1", "0 | 2" }); + + selectMenuPath("Component", "Features", "Server-side expand", + "Expand 1 | 1"); + // 1 | 1 not yet visible, shouldn't immediately expand anything assertEquals(3, grid.getRowCount()); assertCellTexts(0, 0, new String[] { "0 | 0", "0 | 1", "0 | 2" }); + selectMenuPath("Component", "Features", "Server-side expand", + "Expand 0 | 0"); + // 1 | 1 becomes visible and is also expanded + assertEquals(9, grid.getRowCount()); + assertCellTexts(1, 0, new String[] { "1 | 0", "1 | 1", "2 | 0", "2 | 1", + "2 | 2", "1 | 2" }); + // collapsing a leaf should have no effect - selectMenuPath("Component", "Features", "Toggle expand", "1 | 0"); + selectMenuPath("Component", "Features", "Server-side collapse", + "Collapse 2 | 1"); + assertEquals(9, grid.getRowCount()); + assertCellTexts(1, 0, new String[] { "1 | 0", "1 | 1", "2 | 0", "2 | 1", + "2 | 2", "1 | 2" }); + + // collapsing 0 | 0 should collapse the expanded 1 | 1 + selectMenuPath("Component", "Features", "Server-side collapse", + "Collapse 0 | 0"); assertEquals(3, grid.getRowCount()); + assertCellTexts(0, 0, new String[] { "0 | 0", "0 | 1", "0 | 2" }); + + // 1 | 1 should not be expanded this time + 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(); + } + + @Test + public void pending_expands_cleared_when_data_provider_set() { + selectMenuPath("Component", "Features", "Server-side expand", + "Expand 1 | 1"); + selectMenuPath("Component", "Features", "Set data provider", + "LazyHierarchicalDataProvider"); + grid.expandWithClick(0); + assertEquals(6, grid.getRowCount()); + assertCellTexts(1, 0, new String[] { "1 | 0", "1 | 1", "1 | 2" }); } @Test @@ -105,20 +156,21 @@ public class TreeGridBasicFeaturesTest extends MultiBrowserTest { // Should expand "1 | 1" without moving focus new Actions(getDriver()).sendKeys(Keys.RIGHT).perform(); assertEquals(9, grid.getRowCount()); - assertCellTexts(2, 0, new String[] { "1 | 1", "2 | 0", "2 | 1", "2 | 2", "1 | 2"}); + assertCellTexts(2, 0, + new String[] { "1 | 1", "2 | 0", "2 | 1", "2 | 2", "1 | 2" }); assertTrue(grid.getRow(2).hasClassName("v-grid-rowmode-row-focused")); // Should collapse "1 | 1" new Actions(getDriver()).sendKeys(Keys.LEFT).perform(); assertEquals(6, grid.getRowCount()); - assertCellTexts(2, 0, new String[] { "1 | 1", "1 | 2", "0 | 1"}); + assertCellTexts(2, 0, new String[] { "1 | 1", "1 | 2", "0 | 1" }); assertTrue(grid.getRow(2).hasClassName("v-grid-rowmode-row-focused")); - // Should navigate to "0 | 0" new Actions(getDriver()).sendKeys(Keys.LEFT).perform(); assertEquals(6, grid.getRowCount()); - assertCellTexts(0, 0, new String[] { "0 | 0", "1 | 0", "1 | 1", "1 | 2" , "0 | 1" }); + assertCellTexts(0, 0, + new String[] { "0 | 0", "1 | 0", "1 | 1", "1 | 2", "0 | 1" }); assertTrue(grid.getRow(0).hasClassName("v-grid-rowmode-row-focused")); // Should collapse "0 | 0" @@ -171,18 +223,40 @@ public class TreeGridBasicFeaturesTest extends MultiBrowserTest { selectMenuPath("Component", "State", "Expand listener"); selectMenuPath("Component", "State", "Collapse listener"); - assertFalse(logContainsText("Item expanded: 0 | 0")); - assertFalse(logContainsText("Item collapsed: 0 | 0")); + assertFalse(logContainsText( + "Item expanded (user originated: true): 0 | 0")); + assertFalse(logContainsText( + "Item collapsed (user originated: true): 0 | 0")); grid.expandWithClick(0); - assertTrue(logContainsText("Item expanded: 0 | 0")); - assertFalse(logContainsText("Item collapsed: 0 | 0")); + assertTrue(logContainsText( + "Item expanded (user originated: true): 0 | 0")); + assertFalse(logContainsText( + "Item collapsed (user originated: true): 0 | 0")); grid.collapseWithClick(0); - assertTrue(logContainsText("Item expanded: 0 | 0")); - assertTrue(logContainsText("Item collapsed: 0 | 0")); + assertTrue(logContainsText( + "Item expanded (user originated: true): 0 | 0")); + assertTrue(logContainsText( + "Item collapsed (user originated: true): 0 | 0")); + + selectMenuPath("Component", "Features", "Server-side expand", + "Expand 0 | 0"); + + assertTrue(logContainsText( + "Item expanded (user originated: false): 0 | 0")); + assertFalse(logContainsText( + "Item collapsed (user originated: false): 0 | 0")); + + selectMenuPath("Component", "Features", "Server-side collapse", + "Collapse 0 | 0"); + + assertTrue(logContainsText( + "Item expanded (user originated: false): 0 | 0")); + assertTrue(logContainsText( + "Item collapsed (user originated: false): 0 | 0")); selectMenuPath("Component", "State", "Expand listener"); selectMenuPath("Component", "State", "Collapse listener"); @@ -190,8 +264,10 @@ public class TreeGridBasicFeaturesTest extends MultiBrowserTest { grid.expandWithClick(1); grid.collapseWithClick(1); - assertFalse(logContainsText("Item expanded: 0 | 1")); - assertFalse(logContainsText("Item collapsed: 0 | 1")); + assertFalse(logContainsText( + "Item expanded (user originated: true): 0 | 1")); + assertFalse(logContainsText( + "Item collapsed (user originated: true): 0 | 1")); } private void assertCellTexts(int startRowIndex, int cellIndex, diff --git a/uitest/src/test/java/com/vaadin/tests/components/treegrid/TreeGridHugeTreeTest.java b/uitest/src/test/java/com/vaadin/tests/components/treegrid/TreeGridHugeTreeTest.java new file mode 100644 index 0000000000..1d5d9b309d --- /dev/null +++ b/uitest/src/test/java/com/vaadin/tests/components/treegrid/TreeGridHugeTreeTest.java @@ -0,0 +1,60 @@ +package com.vaadin.tests.components.treegrid; + +import org.junit.Assert; +import org.junit.Test; + +import com.vaadin.testbench.elements.ButtonElement; +import com.vaadin.testbench.elements.TreeGridElement; +import com.vaadin.tests.tb3.SingleBrowserTest; + +public class TreeGridHugeTreeTest extends SingleBrowserTest { + + private TreeGridElement grid; + + @Test + public void toggle_expand_when_row_out_of_cache() { + openTestURL(); + + grid = $(TreeGridElement.class).first(); + ButtonElement expandSecondRowButton = $(ButtonElement.class).get(0); + ButtonElement collapseSecondRowButton = $(ButtonElement.class).get(1); + + grid.expandWithClick(2); + grid.expandWithClick(3); + grid.scrollToRow(300); + grid.waitForVaadin(); + + expandSecondRowButton.click(); + + grid.scrollToRow(0); + assertCellTexts(0, 0, new String[] { "Granddad 0", "Granddad 1", + "Dad 1/0", "Dad 1/1", "Dad 1/2", "Granddad 2", "Dad 2/0" }); + + grid.scrollToRow(300); + grid.waitForVaadin(); + collapseSecondRowButton.click(); + grid.scrollToRow(0); + grid.waitForVaadin(); + assertCellTexts(0, 0, new String[] { "Granddad 0", "Granddad 1", + "Granddad 2", "Dad 2/0" }); + + grid.scrollToRow(300); + grid.waitForVaadin(); + expandSecondRowButton.click(); + collapseSecondRowButton.click(); + grid.scrollToRow(0); + grid.waitForVaadin(); + assertCellTexts(0, 0, new String[] { "Granddad 0", "Granddad 1", + "Granddad 2", "Dad 2/0" }); + } + + private void assertCellTexts(int startRowIndex, int cellIndex, + String[] cellTexts) { + int index = startRowIndex; + for (String cellText : cellTexts) { + Assert.assertEquals(cellText, + grid.getRow(index).getCell(cellIndex).getText()); + index++; + } + } +} |