Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -755,8 +755,7 @@ public void expand(Collection<T> items) {
*/
protected void expand(Collection<T> items, boolean userOriginated) {
Collection<T> expandedItems = getDataCommunicator().expand(items);
fireEvent(new ExpandEvent<T, TreeGrid<T>>(this, userOriginated,
expandedItems));
fireEvent(new ExpandEvent<>(this, userOriginated, expandedItems));
}

/**
Expand All @@ -767,8 +766,6 @@ protected void expand(Collection<T> items, boolean userOriginated) {
* descendant, meaning that {@code expandRecursively(items, 0)} expands only
* the given items while {@code expandRecursively(items, 2)} expands the
* given items as well as their children and grandchildren.
* <p>
* This method will <i>not</i> fire events for expanded nodes.
*
* @param items
* the items to expand recursively
Expand All @@ -777,7 +774,7 @@ protected void expand(Collection<T> items, boolean userOriginated) {
* @since 8.4
*/
public void expandRecursively(Stream<T> items, int depth) {
expandRecursively(items.collect(Collectors.toList()), depth);
expandRecursively(items.toList(), depth);
}

/**
Expand All @@ -788,8 +785,6 @@ public void expandRecursively(Stream<T> items, int depth) {
* descendant, meaning that {@code expandRecursively(items, 0)} expands only
* the given items while {@code expandRecursively(items, 2)} expands the
* given items as well as their children and grandchildren.
* <p>
* This method will <i>not</i> fire events for expanded nodes.
*
* @param items
* the items to expand recursively
Expand All @@ -798,8 +793,9 @@ public void expandRecursively(Stream<T> items, int depth) {
* @since 8.4
*/
public void expandRecursively(Collection<T> items, int depth) {
getDataCommunicator()
var expandedItems = getDataCommunicator()
.expand(getItemsWithChildrenRecursively(items, depth));
fireEvent(new ExpandEvent<>(this, false, expandedItems));
}

/**
Expand Down Expand Up @@ -838,8 +834,7 @@ public void collapse(Collection<T> items) {
*/
protected void collapse(Collection<T> items, boolean userOriginated) {
Collection<T> collapsedItems = getDataCommunicator().collapse(items);
fireEvent(new CollapseEvent<T, TreeGrid<T>>(this, userOriginated,
collapsedItems));
fireEvent(new CollapseEvent<>(this, userOriginated, collapsedItems));
}

/**
Expand All @@ -850,8 +845,6 @@ protected void collapse(Collection<T> items, boolean userOriginated) {
* descendant, meaning that {@code collapseRecursively(items, 0)} collapses
* only the given items while {@code collapseRecursively(items, 2)}
* collapses the given items as well as their children and grandchildren.
* <p>
* This method will <i>not</i> fire events for collapsed nodes.
*
* @param items
* the items to collapse recursively
Expand All @@ -860,7 +853,7 @@ protected void collapse(Collection<T> items, boolean userOriginated) {
* @since 8.4
*/
public void collapseRecursively(Stream<T> items, int depth) {
collapseRecursively(items.collect(Collectors.toList()), depth);
collapseRecursively(items.toList(), depth);
}

/**
Expand All @@ -871,8 +864,6 @@ public void collapseRecursively(Stream<T> items, int depth) {
* descendant, meaning that {@code collapseRecursively(items, 0)} collapses
* only the given items while {@code collapseRecursively(items, 2)}
* collapses the given items as well as their children and grandchildren.
* <p>
* This method will <i>not</i> fire events for collapsed nodes.
*
* @param items
* the items to collapse recursively
Expand All @@ -881,8 +872,9 @@ public void collapseRecursively(Stream<T> items, int depth) {
* @since 8.4
*/
public void collapseRecursively(Collection<T> items, int depth) {
getDataCommunicator()
var collapsedItems = getDataCommunicator()
.collapse(getItemsWithChildrenRecursively(items, depth));
fireEvent(new CollapseEvent<>(this, false, collapsedItems));
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
/*
* Copyright 2000-2025 Vaadin Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
* the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations under
* the License.
*/
package com.vaadin.flow.component.treegrid;

import java.util.List;
import java.util.concurrent.atomic.AtomicReference;

import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import com.vaadin.flow.data.provider.hierarchy.TreeData;
import com.vaadin.flow.data.provider.hierarchy.TreeDataProvider;

public class ExpandCollapseTest {

private TreeGrid<String> treeGrid;
private AtomicReference<ExpandEvent<String, TreeGrid<String>>> expandEvent;
private AtomicReference<CollapseEvent<String, TreeGrid<String>>> collapseEvent;

@Before
public void init() {
treeGrid = new TreeGrid<>();
var treeData = new TreeData<String>();
treeData.addRootItems("Item 0", "Item 1");
treeData.addItems("Item 0", "Item 0-0", "Item 0-1");
treeData.addItems("Item 0-0", "Item 0-0-0", "Item 0-0-1");
treeData.addItems("Item 0-0-0", "Item 0-0-0-0", "Item 0-0-0-1");
var treeDataProvider = new TreeDataProvider<>(treeData);
treeGrid.setDataProvider(treeDataProvider);
expandEvent = new AtomicReference<>();
treeGrid.addExpandListener(expandEvent::set);
collapseEvent = new AtomicReference<>();
treeGrid.addCollapseListener(collapseEvent::set);
}

@Test
public void expand_itemExpandedCorrectly() {
treeGrid.expand("Item 0");
Assert.assertTrue(treeGrid.isExpanded("Item 0"));
Assert.assertFalse(treeGrid.isExpanded("Item 0-0"));
Assert.assertEquals(List.of("Item 0"), expandEvent.get().getItems());
}

@Test
public void collapse_itemCollapsedCorrectly() {
treeGrid.expand("Item 0");
treeGrid.collapse("Item 0");
Assert.assertFalse(treeGrid.isExpanded("Item 0"));
Assert.assertEquals(List.of("Item 0"), collapseEvent.get().getItems());
}

@Test
public void expandRecursivelyWithNonMaxDepth_itemsExpandedCorrectly() {
treeGrid.expandRecursively(List.of("Item 0"), 1);
Assert.assertTrue(treeGrid.isExpanded("Item 0"));
Assert.assertTrue(treeGrid.isExpanded("Item 0-0"));
Assert.assertFalse(treeGrid.isExpanded("Item 0-0-0"));
Assert.assertEquals(List.of("Item 0", "Item 0-0"),
expandEvent.get().getItems());
}

@Test
public void expandRecursivelyWithMaxDepth_itemsExpandedCorrectly() {
treeGrid.expandRecursively(List.of("Item 0"), 2);
Assert.assertTrue(treeGrid.isExpanded("Item 0"));
Assert.assertTrue(treeGrid.isExpanded("Item 0-0"));
Assert.assertTrue(treeGrid.isExpanded("Item 0-0-0"));
Assert.assertEquals(List.of("Item 0", "Item 0-0", "Item 0-0-0"),
expandEvent.get().getItems());
}

@Test
public void collapseRecursivelyWithNonMaxDepth_itemsCollapsedCorrectly() {
treeGrid.expandRecursively(List.of("Item 0"), 2);
treeGrid.collapseRecursively(List.of("Item 0"), 1);
Assert.assertFalse(treeGrid.isExpanded("Item 0"));
Assert.assertFalse(treeGrid.isExpanded("Item 0-0"));
Assert.assertTrue(treeGrid.isExpanded("Item 0-0-0"));
Assert.assertEquals(List.of("Item 0", "Item 0-0"),
collapseEvent.get().getItems());
}

@Test
public void collapseRecursivelyWithMaxDepth_itemsCollapsedCorrectly() {
treeGrid.expandRecursively(List.of("Item 0"), 2);
treeGrid.collapseRecursively(List.of("Item 0"), 2);
Assert.assertFalse(treeGrid.isExpanded("Item 0"));
Assert.assertFalse(treeGrid.isExpanded("Item 0-0"));
Assert.assertFalse(treeGrid.isExpanded("Item 0-0-0"));
Assert.assertEquals(List.of("Item 0", "Item 0-0", "Item 0-0-0"),
collapseEvent.get().getItems());
}
}