Skip to content

Commit

Permalink
camel-tooling#24 - provide test checking completion available for Gen…
Browse files Browse the repository at this point in the history
…eric Text editor

Signed-off-by: Aurélien Pupier <[email protected]>
  • Loading branch information
apupier committed Apr 25, 2018
1 parent 16dd482 commit 3f1eb44
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ Require-Bundle: org.junit;bundle-version="4.12.0",
org.assertj;bundle-version="1.7.1",
org.eclipse.lsp4e;bundle-version="0.3.0",
com.github.camel-tooling.eclipse.client;bundle-version="1.0.0",
org.eclipse.jface.text;bundle-version="3.12.0",
org.eclipse.ui.ide;bundle-version="3.13.1",
org.eclipse.core.resources;bundle-version="3.12.0",
org.eclipse.core.runtime;bundle-version="3.13.0",
org.eclipse.ui.genericeditor;bundle-version="1.0.1"
org.eclipse.ui.genericeditor;bundle-version="1.0.1",
org.eclipse.ui.workbench.texteditor;bundle-version="3.10.100"
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,35 @@
package com.github.cameltooling.eclipse.client.tests.integration;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.fail;

import java.io.ByteArrayInputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.stream.Stream;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.jface.text.ITextViewer;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.eclipse.lsp4e.operations.completion.LSContentAssistProcessor;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.ide.IDE;
import org.eclipse.ui.internal.genericeditor.ExtensionBasedTextEditor;
import org.eclipse.ui.texteditor.AbstractTextEditor;
import org.eclipse.ui.texteditor.ITextEditor;
import org.junit.Test;

public class CamelLSPLoadedByExtensionPointIT {

private static final int ARBITRARY_NUMBER_OF_MINIMAL_CAMEL_COMPONENTS = 200;

@Test
public void testGenericEditorCanOpenCamelFile() throws Exception {
public void testGenericEditorProvideCompletion() throws Exception {
IWorkspaceRoot root = ResourcesPlugin.getWorkspace().getRoot();
IProject project = root.getProject(CamelLSPLoadedByExtensionPointIT.class.getSimpleName());
project.create(null);
Expand All @@ -43,6 +54,42 @@ public void testGenericEditorCanOpenCamelFile() throws Exception {
camelFile.create(new ByteArrayInputStream("<from uri=\"\" xmlns=\"http://camel.apache.org/schema/spring\"></from>\n".getBytes()), IResource.FORCE, null);
IEditorPart openEditor = IDE.openEditor(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(), camelFile, "org.eclipse.ui.genericeditor.GenericEditor");
assertThat(openEditor).isInstanceOf(ExtensionBasedTextEditor.class);

ITextViewer textViewer = getTextViewer(openEditor);

ICompletionProposal[] proposals = new LSContentAssistProcessor().computeCompletionProposals(textViewer, 11);
assertThat(proposals.length).isGreaterThan(ARBITRARY_NUMBER_OF_MINIMAL_CAMEL_COMPONENTS);
checkContainsATimerProposal(proposals);
}

private void checkContainsATimerProposal(ICompletionProposal[] proposals) {
assertThat(Stream.of(proposals)
.map(ICompletionProposal::getDisplayString)
.filter(displayString -> displayString.contains("timer"))
.findFirst().get()).isNotNull();
}

/**
* Retrieve the TextViewer by reflection on a protected method as it is not publicly available.
* it is used also by LSP4E Tests org.eclipse.lsp4e.test.TestUtils.getTextViewer(IEditorPart)
* Unfortunately, the code cannot be reused as it is in a fragment.
*/
public ITextViewer getTextViewer(IEditorPart part) throws InvocationTargetException {
try {
if (part instanceof ITextEditor) {
ITextEditor textEditor = (ITextEditor) part;

Method getSourceViewerMethod = AbstractTextEditor.class.getDeclaredMethod("getSourceViewer"); //$NON-NLS-1$
getSourceViewerMethod.setAccessible(true);
return (ITextViewer) getSourceViewerMethod.invoke(textEditor);
} else {
fail("Unable to open editor");
return null;
}
} catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException
| InvocationTargetException e) {
throw new InvocationTargetException(e);
}
}

}

0 comments on commit 3f1eb44

Please sign in to comment.