From c8eb2558f41ed8463005d2c746f7ce6ed3fea288 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=90=D0=BB=D0=B5=D0=BA=D1=81=D0=B0=D0=BD=D0=B4=D1=8A?= =?UTF-8?q?=D1=80=20=D0=9A=D1=83=D1=80=D1=82=D0=B0=D0=BA=D0=BE=D0=B2?= Date: Fri, 4 Oct 2024 13:24:43 +0300 Subject: [PATCH] Stabilize EditorTest Query completions via LSContentAssistProcessor rather than complicated (and unstable ) way through opened shells. --- .../m2e/editor/lemminx/tests/EditorTest.java | 39 +++++++++++-------- 1 file changed, 22 insertions(+), 17 deletions(-) diff --git a/org.eclipse.m2e.editor.lemminx.tests/src/org/eclipse/m2e/editor/lemminx/tests/EditorTest.java b/org.eclipse.m2e.editor.lemminx.tests/src/org/eclipse/m2e/editor/lemminx/tests/EditorTest.java index 8e1248cec..76e239851 100644 --- a/org.eclipse.m2e.editor.lemminx.tests/src/org/eclipse/m2e/editor/lemminx/tests/EditorTest.java +++ b/org.eclipse.m2e.editor.lemminx.tests/src/org/eclipse/m2e/editor/lemminx/tests/EditorTest.java @@ -14,9 +14,8 @@ import java.io.IOException; import java.io.InputStream; +import java.lang.reflect.Method; import java.util.Arrays; -import java.util.Set; -import java.util.stream.Collectors; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IMarker; @@ -26,18 +25,19 @@ import org.eclipse.core.runtime.CoreException; import org.eclipse.jface.preference.IPreferenceStore; import org.eclipse.jface.text.TextSelection; +import org.eclipse.jface.text.contentassist.ICompletionProposal; +import org.eclipse.jface.text.source.ISourceViewer; +import org.eclipse.lsp4e.operations.completion.LSContentAssistProcessor; import org.eclipse.m2e.core.internal.preferences.MavenPreferenceConstants; import org.eclipse.m2e.core.ui.internal.M2EUIPluginActivator; import org.eclipse.m2e.editor.pom.MavenPomEditor; import org.eclipse.m2e.tests.common.AbstractMavenProjectTestCase; import org.eclipse.swt.widgets.Display; -import org.eclipse.swt.widgets.Shell; -import org.eclipse.swt.widgets.Table; -import org.eclipse.swt.widgets.TableItem; import org.eclipse.ui.IWorkbenchPage; import org.eclipse.ui.PlatformUI; import org.eclipse.ui.ide.IDE; import org.eclipse.ui.tests.harness.util.DisplayHelper; +import org.eclipse.ui.texteditor.AbstractTextEditor; import org.eclipse.ui.texteditor.ITextEditor; import org.eclipse.ui.texteditor.ITextEditorActionConstants; import org.eclipse.wildwebdeveloper.xml.internal.Activator; @@ -53,7 +53,7 @@ public class EditorTest extends AbstractMavenProjectTestCase { private static final String XML_PREFERENCES_DOWNLOAD_EXTERNAL_RESOURCES = "org.eclipse.wildwebdeveloper.xml.downloadExternalResources.enabled"; - private static final long WAIT_TIMEOUT = 60000; + private static final long WAIT_TIMEOUT = 15000; private IWorkbenchPage page; private IProject project; @@ -91,19 +91,13 @@ public void testGenericEditorHasMavenExtensionEnabled() throws Exception { })); int offset = editorPart.getDocumentProvider().getDocument(editorPart.getEditorInput()).get() .indexOf(""); - Set beforeShells = Arrays.stream(display.getShells()).filter(Shell::isVisible) - .collect(Collectors.toSet()); editorPart.getSelectionProvider().setSelection(new TextSelection(offset, 0)); editorPart.getAction(ITextEditorActionConstants.CONTENT_ASSIST).run(); - assertTrue("Missing completion proposals", DisplayHelper.waitForCondition(display, WAIT_TIMEOUT, () -> { - Set afterShells = Arrays.stream(display.getShells()).filter(Shell::isVisible) - .collect(Collectors.toSet()); - afterShells.removeAll(beforeShells); - return afterShells.stream().flatMap(shell -> Arrays.stream(shell.getChildren())) - .filter(Table.class::isInstance).map(Table.class::cast).findFirst() - .map(table -> Arrays.stream(table.getItems()).map(TableItem::getText).anyMatch("compile"::equals)) - .orElse(Boolean.FALSE).booleanValue(); - })); + LSContentAssistProcessor contentAssistProcessor = new LSContentAssistProcessor(); + ICompletionProposal[] proposals = contentAssistProcessor + .computeCompletionProposals(getSourceViewer(editorPart), offset); + assertTrue("Missing completion proposals", + Arrays.stream(proposals).map(ICompletionProposal::getDisplayString).anyMatch("compile"::equals)); } @Test @@ -145,4 +139,15 @@ private IProject createMavenProject(String projectName, String pomFileName) thro } } + private static ISourceViewer getSourceViewer(ITextEditor editor) { + try { + Method method = AbstractTextEditor.class.getDeclaredMethod("getSourceViewer"); + method.setAccessible(true); + return (ISourceViewer) method.invoke(editor); + } catch (Exception e) { + e.printStackTrace(); + } + return null; + } + }