Skip to content

Commit

Permalink
Improve Generic Editor FoldingTest so it can handle async change to
Browse files Browse the repository at this point in the history
folding

The folding provider is going to become more reactive, so we improve it
a bit
to allow async folding info.
  • Loading branch information
mickaelistria committed Feb 9, 2023
1 parent 4a226c0 commit bb5b98a
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 36 deletions.
2 changes: 1 addition & 1 deletion org.eclipse.ui.genericeditor.tests/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: %Plugin.name
Bundle-SymbolicName: org.eclipse.ui.genericeditor.tests;singleton:=true
Bundle-Version: 1.2.300.qualifier
Bundle-Version: 1.2.400.qualifier
Bundle-Vendor: %Plugin.providerName
Bundle-Localization: plugin
Export-Package: org.eclipse.ui.genericeditor.tests,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,13 @@
import static org.junit.Assert.assertTrue;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;

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

import org.eclipse.swt.widgets.Display;

import org.eclipse.core.commands.Command;

import org.eclipse.jface.text.ITextViewer;
Expand All @@ -33,7 +31,7 @@
import org.eclipse.jface.text.source.IAnnotationModel;
import org.eclipse.jface.text.source.projection.ProjectionAnnotation;
import org.eclipse.jface.text.source.projection.ProjectionViewer;
import org.eclipse.jface.text.tests.util.DisplayHelper;
import org.eclipse.ui.tests.harness.util.DisplayHelper;

import org.eclipse.ui.commands.ICommandService;
import org.eclipse.ui.genericeditor.tests.contributions.EnabledPropertyTester;
Expand All @@ -50,53 +48,45 @@ protected void createAndOpenFile() throws Exception {
@Test
public void testDefaultIndentFoldingOneFold() throws Exception {
createAndOpenFile("bar.xml", "<a>\n b</a>");
checkFolding(pos(0, 10));
assertFoldingAsync(pos(0, 10));
}

@Test
public void testDefaultIndentFoldingTwoFold() throws Exception {
createAndOpenFile("bar.xml", "<a>\n <b>\n c\n </b>\n</a>");
checkFolding(pos(0, 19), pos(4, 9));
assertFoldingAsync(pos(0, 19), pos(4, 9));
}

@Test
public void testCustomFoldingReconciler() throws Exception {
createAndOpenFile("bar.txt", "<a>\n <b>\n c\n </b>\n</a>\n");
checkFolding(pos(0, 24), pos(5, 14));
assertFoldingAsync(pos(0, 24), pos(5, 14));
}

@Test
public void testEnabledWhenCustomFoldingReconciler() throws Exception {
EnabledPropertyTester.setEnabled(true);
createAndOpenFile("enabledWhen.txt", "<a>\n <b>\n c\n </b>\n</a>\n");
checkFolding(pos(0, 24), pos(5, 14));
assertFoldingAsync(pos(0, 24), pos(5, 14));
cleanFileAndEditor();

EnabledPropertyTester.setEnabled(false);
createAndOpenFile("enabledWhen.txt", "<a>\n <b>\n c\n </b>\n</a>\n");
checkFolding();
assertFoldingAsync();
}

private static Position pos(int offset, int length) {
return new Position(offset, length);
}

private void checkFolding(Position... expectedPositions) {
if (expectedPositions == null) {
expectedPositions= new Position[0];
}
waitForAnnotations(expectedPositions.length);
List<Annotation> folderAnnotations= getAnnotationsFromAnnotationModel();
Assert.assertEquals(expectedPositions.length, folderAnnotations.size());
List<Position> actualPositions= new ArrayList<>(expectedPositions.length);
for (int i= 0; i < expectedPositions.length; i++) {
Annotation folderAnnotation= folderAnnotations.get(i);
Position actualPosition= getProjectionAnnotationModel().getPosition(folderAnnotation);
actualPositions.add(actualPosition);
}
// Sort actual positions by offset
Collections.sort(actualPositions, (p1, p2) -> p1.offset - p2.offset);
Assert.assertArrayEquals(expectedPositions, actualPositions.toArray());
private void assertFoldingAsync(final Position... expectedPositions) {
DisplayHelper.waitForCondition(editor.getSite().getShell().getDisplay(), 5000, () -> {
Position[] actualPositions = getAnnotationsFromAnnotationModel().stream() //
.map(getProjectionAnnotationModel()::getPosition) //
.sorted(Comparator.comparingInt(Position::getOffset))
.toArray(Position[]::new);
return Arrays.deepEquals(actualPositions, expectedPositions);
});
}

private IAnnotationModel getProjectionAnnotationModel() {
Expand All @@ -105,15 +95,6 @@ private IAnnotationModel getProjectionAnnotationModel() {
return am;
}

private void waitForAnnotations(int count) {
new DisplayHelper() {
@Override
protected boolean condition() {
return getAnnotationsFromAnnotationModel().size() == count;
}
}.waitForCondition(Display.getDefault(), 2000);
}

private List<Annotation> getAnnotationsFromAnnotationModel() {
List<Annotation> annotationList= new ArrayList<>();
Iterator<Annotation> annotationIterator= getProjectionAnnotationModel().getAnnotationIterator();
Expand Down

0 comments on commit bb5b98a

Please sign in to comment.