Skip to content

Commit 30c75af

Browse files
committed
test: add Fold All / Unfold All command test cases
1 parent 412b3e3 commit 30c75af

1 file changed

Lines changed: 121 additions & 0 deletions

File tree

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
/*******************************************************************************
2+
* Copyright (c) 2025 Vegard IT GmbH and others.
3+
* This program and the accompanying materials are made
4+
* available under the terms of the Eclipse Public License 2.0
5+
* which is available at https://www.eclipse.org/legal/epl-2.0/
6+
*
7+
* SPDX-License-Identifier: EPL-2.0
8+
******************************************************************************/
9+
package org.eclipse.lsp4e.test.folding;
10+
11+
import static org.junit.Assert.assertEquals;
12+
import static org.junit.Assert.assertTrue;
13+
14+
import java.util.List;
15+
16+
import org.eclipse.core.runtime.CoreException;
17+
import org.eclipse.jface.preference.IPreferenceStore;
18+
import org.eclipse.jface.text.ITextViewer;
19+
import org.eclipse.jface.text.source.Annotation;
20+
import org.eclipse.jface.text.source.projection.ProjectionAnnotation;
21+
import org.eclipse.jface.text.source.projection.ProjectionAnnotationModel;
22+
import org.eclipse.jface.text.source.projection.ProjectionViewer;
23+
import org.eclipse.lsp4e.LanguageServerPlugin;
24+
import org.eclipse.lsp4e.LSPEclipseUtils;
25+
import org.eclipse.lsp4e.test.utils.AbstractTest;
26+
import org.eclipse.lsp4e.test.utils.TestUtils;
27+
import org.eclipse.lsp4e.tests.mock.MockLanguageServer;
28+
import org.eclipse.lsp4e.ui.FoldingPreferencePage;
29+
import org.eclipse.lsp4j.FoldingRange;
30+
import org.eclipse.lsp4j.FoldingRangeKind;
31+
import org.eclipse.ui.PlatformUI;
32+
import org.eclipse.ui.handlers.IHandlerService;
33+
import org.junit.Test;
34+
35+
public class FoldingCommandsTest extends AbstractTest {
36+
37+
private static final int MAX_WAIT_MS = 5_000;
38+
39+
private static final String CONTENT = """
40+
/**
41+
* SPDX-License-Identifier: EPL-2.0
42+
*/
43+
import
44+
import
45+
import
46+
/**
47+
* Some comment
48+
*/
49+
visible
50+
""";
51+
52+
@Test
53+
public void foldAndUnfoldAllCommands() throws Exception {
54+
// Ensure no auto-folding interferes with the command behavior
55+
configureAutoFolding(false);
56+
57+
// Provide folding ranges from the Mock LS: license header and imports
58+
final var foldingRangeLicense = new FoldingRange(0, 2);
59+
foldingRangeLicense.setKind(FoldingRangeKind.Comment);
60+
final var foldingRangeImport = new FoldingRange(3, 5);
61+
foldingRangeImport.setKind(FoldingRangeKind.Imports);
62+
MockLanguageServer.INSTANCE.setFoldingRanges(List.of(foldingRangeLicense, foldingRangeImport));
63+
64+
// Open editor and wait until folding annotations are present
65+
final var editor = TestUtils.openEditor(TestUtils.createUniqueTestFile(null, CONTENT));
66+
final ITextViewer viewer = LSPEclipseUtils.getTextViewer(editor);
67+
assertTrue(viewer instanceof ProjectionViewer);
68+
69+
final var pViewer = (ProjectionViewer) viewer;
70+
TestUtils.waitForAndAssertCondition(MAX_WAIT_MS, () -> getAnnotationModel(pViewer) != null);
71+
final ProjectionAnnotationModel model = getAnnotationModel(pViewer);
72+
assertEquals(2, countAnnotations(model));
73+
74+
// Execute "Fold All"
75+
IHandlerService handlerService = PlatformUI.getWorkbench().getService(IHandlerService.class);
76+
handlerService.executeCommand("org.eclipse.lsp4e.folding.collapseAll", null);
77+
78+
TestUtils.waitForAndAssertCondition(MAX_WAIT_MS, () -> countCollapsed(model) == 2);
79+
assertEquals(2, countCollapsed(model));
80+
81+
// Execute "Unfold All"
82+
handlerService.executeCommand("org.eclipse.lsp4e.folding.expandAll", null);
83+
TestUtils.waitForAndAssertCondition(MAX_WAIT_MS, () -> countCollapsed(model) == 0);
84+
assertEquals(0, countCollapsed(model));
85+
}
86+
87+
private static ProjectionAnnotationModel getAnnotationModel(ProjectionViewer viewer) {
88+
return viewer.getProjectionAnnotationModel();
89+
}
90+
91+
private static int countAnnotations(ProjectionAnnotationModel model) {
92+
int count = 0;
93+
for (var it = model.getAnnotationIterator(); it != null && it.hasNext();) {
94+
if (it.next() instanceof ProjectionAnnotation) {
95+
count++;
96+
}
97+
}
98+
return count;
99+
}
100+
101+
private static int countCollapsed(ProjectionAnnotationModel model) {
102+
int count = 0;
103+
for (var it = model.getAnnotationIterator(); it != null && it.hasNext();) {
104+
Annotation a = it.next();
105+
if (a instanceof ProjectionAnnotation pa && pa.isCollapsed()) {
106+
count++;
107+
}
108+
}
109+
return count;
110+
}
111+
112+
private static void configureAutoFolding(boolean enabled) throws CoreException {
113+
IPreferenceStore store = LanguageServerPlugin.getDefault().getPreferenceStore();
114+
store.setValue(FoldingPreferencePage.PREF_FOLDING_ENABLED, true);
115+
store.setValue(FoldingPreferencePage.PREF_AUTOFOLD_COMMENTS, enabled);
116+
store.setValue(FoldingPreferencePage.PREF_AUTOFOLD_LICENSE_HEADERS_COMMENTS, enabled);
117+
store.setValue(FoldingPreferencePage.PREF_AUTOFOLD_REGIONS, enabled);
118+
store.setValue(FoldingPreferencePage.PREF_AUTOFOLD_IMPORT_STATEMENTS, enabled);
119+
}
120+
}
121+

0 commit comments

Comments
 (0)