|
15 | 15 | */
|
16 | 16 | package com.vaadin.flow.component.treegrid;
|
17 | 17 |
|
| 18 | +import java.util.stream.IntStream; |
| 19 | + |
| 20 | +import org.junit.After; |
18 | 21 | import org.junit.Assert;
|
| 22 | +import org.junit.Before; |
19 | 23 | import org.junit.Test;
|
| 24 | +import org.mockito.Mockito; |
20 | 25 |
|
| 26 | +import com.vaadin.flow.component.UI; |
21 | 27 | import com.vaadin.flow.data.provider.hierarchy.HierarchicalDataProvider.HierarchyFormat;
|
22 | 28 | import com.vaadin.flow.data.provider.hierarchy.TreeData;
|
23 | 29 | import com.vaadin.flow.data.provider.hierarchy.TreeDataProvider;
|
| 30 | +import com.vaadin.flow.function.DeploymentConfiguration; |
| 31 | +import com.vaadin.flow.server.VaadinContext; |
| 32 | +import com.vaadin.flow.server.VaadinService; |
| 33 | +import com.vaadin.flow.server.VaadinSession; |
24 | 34 |
|
25 | 35 | public class ScrollToIndexTest {
|
26 |
| - private TreeGrid<String> treeGrid = new TreeGrid<>(); |
27 |
| - private TreeData<String> treeData = new TreeData<>(); |
| 36 | + private TreeGrid<String> treeGrid; |
| 37 | + private TreeData<String> treeData; |
| 38 | + private UI ui; |
| 39 | + |
| 40 | + @Before |
| 41 | + public void init() { |
| 42 | + ui = new UI(); |
| 43 | + UI.setCurrent(ui); |
| 44 | + var mockSession = Mockito.mock(VaadinSession.class); |
| 45 | + var mockService = Mockito.mock(VaadinService.class); |
| 46 | + var mockContext = Mockito.mock(VaadinContext.class); |
| 47 | + var mockConfiguration = Mockito.mock(DeploymentConfiguration.class); |
| 48 | + Mockito.when(mockSession.getService()).thenReturn(mockService); |
| 49 | + Mockito.when(mockSession.getConfiguration()) |
| 50 | + .thenReturn(mockConfiguration); |
| 51 | + Mockito.when(mockService.getContext()).thenReturn(mockContext); |
| 52 | + ui.getInternals().setSession(mockSession); |
| 53 | + treeGrid = new TreeGrid<>(); |
| 54 | + ui.add(treeGrid); |
| 55 | + treeData = getTreeData(); |
| 56 | + } |
| 57 | + |
| 58 | + @After |
| 59 | + public void tearDown() { |
| 60 | + UI.setCurrent(null); |
| 61 | + } |
28 | 62 |
|
29 | 63 | @Test
|
30 |
| - public void nestedHierarchyFormat_scrollToIndexPath_doesNotThrow() { |
| 64 | + public void flattenedHierarchyFormat_scrollToIndexPath_throwsUnsupportedOperationException() { |
| 65 | + treeGrid.setDataProvider( |
| 66 | + new TreeDataProvider<>(treeData, HierarchyFormat.FLATTENED)); |
| 67 | + Assert.assertThrows(UnsupportedOperationException.class, |
| 68 | + () -> treeGrid.scrollToIndex(0, 0)); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void nestedHierarchyFormat_scrollToIndexPathWithCollapsedParent_expandsParent() { |
31 | 73 | treeGrid.setDataProvider(
|
32 | 74 | new TreeDataProvider<>(treeData, HierarchyFormat.NESTED));
|
33 |
| - treeGrid.scrollToIndex(0, 0); |
| 75 | + var rootItem = treeData.getRootItems().get(10); |
| 76 | + var firstChild = treeData.getChildren(rootItem).getFirst(); |
| 77 | + Assert.assertFalse(treeGrid.isExpanded(rootItem)); |
| 78 | + Assert.assertFalse(treeGrid.isExpanded(firstChild)); |
| 79 | + treeGrid.scrollToIndex(10, 0); |
| 80 | + Assert.assertTrue(treeGrid.isExpanded(rootItem)); |
| 81 | + Assert.assertFalse(treeGrid.isExpanded(firstChild)); |
34 | 82 | }
|
35 | 83 |
|
36 | 84 | @Test
|
37 |
| - public void flattenedHierarchyFormat_scrollToIndexPath_throws() { |
| 85 | + public void flattenedHierarchyFormat_scrollToIndexNotPresent_notScrolled() { |
38 | 86 | treeGrid.setDataProvider(
|
39 | 87 | new TreeDataProvider<>(treeData, HierarchyFormat.FLATTENED));
|
40 |
| - Assert.assertThrows(UnsupportedOperationException.class, |
41 |
| - () -> treeGrid.scrollToIndex(0, 0)); |
| 88 | + assertScrollToIndexCalled(false, 1000); |
| 89 | + assertScrollToIndexCalled(false, -1000); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void flattenedHierarchyFormat_scrollToIndex_scrolled() { |
| 94 | + treeGrid.setDataProvider( |
| 95 | + new TreeDataProvider<>(treeData, HierarchyFormat.FLATTENED)); |
| 96 | + assertScrollToIndexCalled(true, 10); |
| 97 | + assertScrollToIndexCalled(true, -10); |
| 98 | + } |
| 99 | + |
| 100 | + @Test |
| 101 | + public void nestedHierarchyFormat_scrollToPathNotPresent_notScrolled() { |
| 102 | + treeGrid.setDataProvider( |
| 103 | + new TreeDataProvider<>(treeData, HierarchyFormat.NESTED)); |
| 104 | + assertScrollToPathCalled(false, 1000); |
| 105 | + assertScrollToPathCalled(false, -1000); |
| 106 | + assertScrollToPathCalled(false, 10, 5, 5); |
| 107 | + assertScrollToPathCalled(false, -10, -5, -5); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + public void nestedHierarchyFormat_scrollToPath_scrolled() { |
| 112 | + treeGrid.setDataProvider( |
| 113 | + new TreeDataProvider<>(treeData, HierarchyFormat.NESTED)); |
| 114 | + assertScrollToPathCalled(true, 10, 5); |
| 115 | + assertScrollToPathCalled(true, -10, -5); |
| 116 | + } |
| 117 | + |
| 118 | + private void assertScrollToIndexCalled(boolean called, int index) { |
| 119 | + treeGrid.scrollToIndex(index); |
| 120 | + Assert.assertEquals(called, isScrollToIndexCalled()); |
| 121 | + } |
| 122 | + |
| 123 | + private void assertScrollToPathCalled(boolean called, int... path) { |
| 124 | + treeGrid.scrollToIndex(path); |
| 125 | + Assert.assertEquals(called, isScrollToIndexCalled()); |
| 126 | + } |
| 127 | + |
| 128 | + private boolean isScrollToIndexCalled() { |
| 129 | + ui.getInternals().getStateTree().runExecutionsBeforeClientResponse(); |
| 130 | + ui.getInternals().getStateTree().collectChanges(ignore -> { |
| 131 | + }); |
| 132 | + var invocations = ui.getInternals().dumpPendingJavaScriptInvocations(); |
| 133 | + return invocations.stream().anyMatch(invocation -> invocation |
| 134 | + .getInvocation().getExpression().contains("scrollToIndex")); |
| 135 | + } |
| 136 | + |
| 137 | + private static TreeData<String> getTreeData() { |
| 138 | + var rootItemCount = 50; |
| 139 | + var childItemCountPerRootItem = 10; |
| 140 | + var treeData = new TreeData<String>(); |
| 141 | + var rootItems = IntStream.range(0, rootItemCount) |
| 142 | + .mapToObj(i -> "Item " + i).toList(); |
| 143 | + treeData.addRootItems(rootItems); |
| 144 | + rootItems.forEach(rootItem -> treeData.addItems(rootItem, |
| 145 | + IntStream.range(0, childItemCountPerRootItem) |
| 146 | + .mapToObj(i -> rootItem + "-" + i))); |
| 147 | + return treeData; |
42 | 148 | }
|
43 | 149 | }
|
0 commit comments