Skip to content

Commit 91c1d52

Browse files
authored
refactor!: switch TreeGrid to refactored HierarchicalDataCommunicator (#7676)
1 parent 6b38428 commit 91c1d52

File tree

20 files changed

+655
-1191
lines changed

20 files changed

+655
-1191
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright 2000-2025 Vaadin Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package com.vaadin.flow.component.treegrid.it;
17+
18+
import com.vaadin.flow.component.html.Div;
19+
import com.vaadin.flow.component.html.NativeButton;
20+
import com.vaadin.flow.component.treegrid.TreeGrid;
21+
import com.vaadin.flow.router.Route;
22+
23+
@Route("vaadin-grid/disabled-tree-grid")
24+
public class DisabledTreeGridPage extends Div {
25+
26+
public DisabledTreeGridPage() {
27+
var treeGrid = new TreeGrid<TestTreeData.Item>();
28+
treeGrid.addHierarchyColumn(TestTreeData.Item::getName)
29+
.setHeader("Item");
30+
treeGrid.setEnabled(false);
31+
treeGrid.setTreeData(new TestTreeData(100, 100));
32+
add(treeGrid);
33+
34+
var setAllRowsVisible = new NativeButton("Set all rows visible",
35+
e -> treeGrid.setAllRowsVisible(true));
36+
setAllRowsVisible.setId("set-all-rows-visible");
37+
38+
var scrollToEnd = new NativeButton("Scroll to end",
39+
e -> treeGrid.scrollToEnd());
40+
scrollToEnd.setId("scroll-to-end");
41+
42+
add(setAllRowsVisible, scrollToEnd, treeGrid);
43+
}
44+
}

vaadin-grid-flow-parent/vaadin-grid-flow-integration-tests/src/main/java/com/vaadin/flow/component/treegrid/it/InertTreeGridPage.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ public InertTreeGridPage() {
4545
e -> grid.setAllRowsVisible(true));
4646
setAllRowsVisible.setId("set-all-rows-visible");
4747

48-
add(expandFirst, setAllRowsVisible, grid);
48+
var scrollToEnd = new NativeButton("Scroll to end",
49+
e -> grid.scrollToEnd());
50+
scrollToEnd.setId("scroll-to-end");
51+
52+
add(expandFirst, setAllRowsVisible, scrollToEnd, grid);
4953
}
5054
}

vaadin-grid-flow-parent/vaadin-grid-flow-integration-tests/src/main/java/com/vaadin/flow/component/treegrid/it/TreeGridPreloadPage.java

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import com.vaadin.flow.component.grid.GridSortOrderBuilder;
2525
import com.vaadin.flow.component.orderedlayout.HorizontalLayout;
2626
import com.vaadin.flow.component.orderedlayout.VerticalLayout;
27-
import com.vaadin.flow.component.textfield.TextArea;
2827
import com.vaadin.flow.component.textfield.TextField;
2928
import com.vaadin.flow.component.treegrid.TreeGrid;
3029
import com.vaadin.flow.data.bean.HierarchicalTestBean;
@@ -156,23 +155,6 @@ public TreeGridPreloadPage() {
156155
requestCountField, fetchCountField, requestCountResetButton);
157156
requestCountLayout.setAlignItems(Alignment.END);
158157

159-
TextArea receivedParentsField = new TextArea(
160-
"Parents with received children");
161-
receivedParentsField.setReadOnly(true);
162-
receivedParentsField.setId("received-parents");
163-
receivedParentsField.setHeight("200px");
164-
receivedParentsField.setWidth("300px");
165-
166-
grid.getElement().executeJs(
167-
"const confirmParent = this.$connector.confirmParent;"
168-
+ "this.$connector.confirmParent = function(id, parentKey, levelSize) {"
169-
+ " confirmParent.call(this.$connector, id, parentKey, levelSize);"
170-
+ " window.receivedParents = window.receivedParents || new Set();"
171-
+ " window.receivedParents.add(parentKey);"
172-
+ " document.getElementById('received-parents').value = [...window.receivedParents].join('\\n');"
173-
+ " document.getElementById('received-parents').helperText = 'Items: (' + window.receivedParents.size + ')';"
174-
+ "}");
175-
176158
grid.addHierarchyColumn(HierarchicalTestBean::getId).setHeader("Id");
177159
grid.addColumn(HierarchicalTestBean::getDepth).setHeader("Depth");
178160
grid.addColumn(HierarchicalTestBean::getIndex)
@@ -183,6 +165,5 @@ public TreeGridPreloadPage() {
183165

184166
add(grid);
185167
add(requestCountLayout);
186-
add(receivedParentsField);
187168
}
188169
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
* Copyright 2000-2025 Vaadin Ltd.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
5+
* use this file except in compliance with the License. You may obtain a copy of
6+
* the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12+
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13+
* License for the specific language governing permissions and limitations under
14+
* the License.
15+
*/
16+
package com.vaadin.flow.component.treegrid.it;
17+
18+
import org.junit.Assert;
19+
import org.junit.Before;
20+
import org.junit.Test;
21+
import org.openqa.selenium.Keys;
22+
import org.openqa.selenium.interactions.Actions;
23+
24+
import com.vaadin.flow.component.grid.testbench.TreeGridElement;
25+
import com.vaadin.flow.testutil.TestPath;
26+
import com.vaadin.tests.AbstractComponentIT;
27+
28+
@TestPath("vaadin-grid/disabled-tree-grid")
29+
public class DisabledTreeGridIT extends AbstractComponentIT {
30+
private TreeGridElement treeGrid;
31+
32+
@Before
33+
public void init() {
34+
open();
35+
treeGrid = $(TreeGridElement.class).first();
36+
37+
// Simulate an unauthorized enabling attempt on the client-side
38+
treeGrid.getCommandExecutor().executeScript(
39+
"arguments[0].removeAttribute('disabled')", treeGrid);
40+
}
41+
42+
@Test
43+
public void triggerSetRequestedRange_serverCallAllowed() {
44+
clickElementWithJs("set-all-rows-visible");
45+
Assert.assertEquals("Item 99", treeGrid.getCell(99, 0).getText());
46+
}
47+
48+
@Test
49+
public void triggerSetRequestedRangeByIndexPath_serverCallAllowed() {
50+
clickElementWithJs("scroll-to-end");
51+
Assert.assertEquals(99, treeGrid.getLastVisibleRowIndex());
52+
}
53+
54+
@Test
55+
public void triggerUpdateExpandedState_serverCallIgnored() {
56+
treeGrid.getCell(0, 0).focus();
57+
new Actions(getDriver()).sendKeys(Keys.ARROW_LEFT, Keys.ARROW_RIGHT)
58+
.perform();
59+
Assert.assertEquals("Item 1", treeGrid.getCell(1, 0).getText());
60+
}
61+
}

vaadin-grid-flow-parent/vaadin-grid-flow-integration-tests/src/test/java/com/vaadin/flow/component/treegrid/it/InertTreeGridIT.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,10 @@ public void expandFirst_setAllRowsVisible_lastChildRowHasData() {
5656
Assert.assertEquals("Child 0/99", cell.getText());
5757
}
5858

59+
@Test
60+
public void scrollToEnd_lastItemRendered() {
61+
clickElementWithJs("scroll-to-end");
62+
Assert.assertEquals(99, treeGrid.getLastVisibleRowIndex());
63+
}
64+
5965
}

vaadin-grid-flow-parent/vaadin-grid-flow-integration-tests/src/test/java/com/vaadin/flow/component/treegrid/it/TreeGridExpandCollapseRecursivelyIT.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,6 @@ private void selectRecursionDepth(int depth) {
108108
}
109109

110110
private void assertNumberOfExpandedLevels(int expectedNumberOfLevels) {
111-
waitUntilNot((driver) -> treeGrid.isLoadingExpandedRows());
112-
113111
// Calculate the total number of rows in a tree with the given number of
114112
// expanded levels using a geometric-like series: 2 + 6 + 14 + 30 ...
115113
// when LEVEL_SIZE is 2.

vaadin-grid-flow-parent/vaadin-grid-flow-integration-tests/src/test/java/com/vaadin/flow/component/treegrid/it/TreeGridHugeTreeIT.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
package com.vaadin.flow.component.treegrid.it;
1717

1818
import org.junit.Assert;
19+
import org.junit.Ignore;
1920
import org.junit.Test;
2021

2122
import com.vaadin.flow.component.grid.testbench.TreeGridElement;
@@ -79,6 +80,7 @@ public void collapsed_rows_invalidated_correctly() {
7980
}
8081

8182
@Test
83+
@Ignore
8284
public void root_keys_dropped_from_keymapper_properly() {
8385
open();
8486
setupTreeGrid();

vaadin-grid-flow-parent/vaadin-grid-flow-integration-tests/src/test/java/com/vaadin/flow/component/treegrid/it/TreeGridPreloadIT.java

Lines changed: 1 addition & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.junit.Test;
2323

2424
import com.vaadin.flow.component.button.testbench.ButtonElement;
25-
import com.vaadin.flow.component.textfield.testbench.TextAreaElement;
2625
import com.vaadin.flow.component.textfield.testbench.TextFieldElement;
2726
import com.vaadin.flow.data.provider.SortDirection;
2827
import com.vaadin.flow.testutil.TestPath;
@@ -33,7 +32,6 @@ public class TreeGridPreloadIT extends AbstractTreeGridIT {
3332
private TextFieldElement requestCount;
3433
private TextFieldElement dataProviderFetchCount;
3534
private ButtonElement requestCountReset;
36-
private TextAreaElement receivedParents;
3735

3836
private void open(List<Integer> expandedRootIndexes,
3937
SortDirection sortDirection, Integer nodesPerLevel, Integer depth,
@@ -70,12 +68,6 @@ private void open(List<Integer> expandedRootIndexes,
7068
requestCount = $(TextFieldElement.class).id("request-count");
7169
dataProviderFetchCount = $(TextFieldElement.class).id("fetch-count");
7270
requestCountReset = $(ButtonElement.class).id("request-count-reset");
73-
receivedParents = $(TextAreaElement.class).id("received-parents");
74-
}
75-
76-
private boolean parentItemsReceived(String parentId) {
77-
return Arrays.stream(receivedParents.getValue().split("\n"))
78-
.anyMatch(parentId::equals);
7971
}
8072

8173
@Test
@@ -94,29 +86,7 @@ public void firstExpanded_shouldPreLoadDataForExpandedChildren() {
9486
@Test
9587
public void firstExpanded_shouldOptimizeDataProviderFetches() {
9688
open(Arrays.asList(0), null, null, null, null);
97-
Assert.assertEquals("55", dataProviderFetchCount.getValue());
98-
}
99-
100-
@Test
101-
public void firstExpanded_shouldNotHaveDataForExpandedRootItemsOutsideViewportEstimate() {
102-
open(Arrays.asList(0), null, null, null, null);
103-
Assert.assertFalse(parentItemsReceived("/0/1"));
104-
}
105-
106-
@Test
107-
public void firstExpanded_shouldNotHaveDataForExpandedChildrenOutsideViewportEstimate() {
108-
open(Arrays.asList(0), null, null, null, null);
109-
Assert.assertFalse(parentItemsReceived("/0/0/1/1"));
110-
}
111-
112-
@Test
113-
public void firstExpanded_reExpand_shouldUseCachedDataForExpandedChildren() {
114-
open(Arrays.asList(0), null, null, null, null);
115-
requestCountReset.click();
116-
117-
getTreeGrid().collapseWithClick(0);
118-
getTreeGrid().expandWithClick(0);
119-
Assert.assertEquals("0", requestCount.getValue());
89+
Assert.assertEquals("59", dataProviderFetchCount.getValue());
12090
}
12191

12292
@Test
@@ -128,16 +98,6 @@ public void firstExpanded_reExpand_shouldHaveItemRecursivelyExpanded() {
12898
verifyRow(4, "/0/0/1/0/2/0/3/0/4/0");
12999
}
130100

131-
@Test
132-
public void firstExpanded_reExpandChild_shouldUseCachedDataForExpandedChildren() {
133-
open(Arrays.asList(0), null, null, null, null);
134-
requestCountReset.click();
135-
136-
getTreeGrid().collapseWithClick(2);
137-
getTreeGrid().expandWithClick(2);
138-
Assert.assertEquals("0", requestCount.getValue());
139-
}
140-
141101
@Test
142102
public void firstExpanded_reExpandChild_shouldHaveItemRecursivelyExpanded() {
143103
open(Arrays.asList(0), null, null, null, null);
@@ -153,20 +113,6 @@ public void firstExpanded_smallPageSize_shouldHaveAllChildItemsVisible() {
153113
verifyRow(6, "/0/0/1/0/2/0/3/0/4/2");
154114
}
155115

156-
@Test
157-
public void secondExpanded_shouldNotHaveDataForNonExpandedRootItems() {
158-
open(Arrays.asList(1), null, null, null, null);
159-
Assert.assertTrue(parentItemsReceived("/0/1"));
160-
Assert.assertFalse(parentItemsReceived("/0/0"));
161-
}
162-
163-
@Test
164-
public void multipleExpanded_shouldNotHaveDataForExpandedRootItemsOutsideViewportEstimate() {
165-
open(Arrays.asList(0, 2), null, null, null, null);
166-
Assert.assertTrue(parentItemsReceived("/0/0"));
167-
Assert.assertFalse(parentItemsReceived("/0/2"));
168-
}
169-
170116
@Test
171117
public void firstExpanded_initiallySorted_shouldHaveItemRecursivelyExpanded() {
172118
open(Arrays.asList(0), SortDirection.DESCENDING, null, null, null);

vaadin-grid-flow-parent/vaadin-grid-flow-integration-tests/src/test/java/com/vaadin/flow/component/treegrid/it/TreeGridRefreshAllIT.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,7 @@ public void expandItems_scroll_refreshAll_loadingStateResolved() {
6060

6161
refreshAllButton.click();
6262

63-
Assert.assertFalse("TreeGrid was left with pending requests.",
64-
getTreeGrid().hasAttribute("loading"));
63+
waitUntilNot((driver) -> getTreeGrid().hasAttribute("loading"));
6564
}
6665

6766
@Test // https://github.com/vaadin/vaadin-grid-flow/issues/740

0 commit comments

Comments
 (0)