diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/contentassist2/CompletionProposalsBaseTest.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/contentassist2/CompletionProposalsBaseTest.java new file mode 100644 index 00000000000..665194b088f --- /dev/null +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/contentassist2/CompletionProposalsBaseTest.java @@ -0,0 +1,222 @@ +/******************************************************************************* + * Copyright (c) 2004, 2005 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Rational Software - Initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.ui.tests.text.contentassist2; + +/** + * @author hamer + * + * This abstract class is the base class for all completion proposals test cases + * + */ +import java.io.FileInputStream; + +import junit.framework.TestCase; + +import org.eclipse.cdt.core.CCProjectNature; +import org.eclipse.cdt.core.dom.ast.ASTCompletionNode; +import org.eclipse.cdt.core.dom.ast.IASTName; +import org.eclipse.cdt.core.model.CoreModel; +import org.eclipse.cdt.core.model.ICProject; +import org.eclipse.cdt.core.model.ITranslationUnit; +import org.eclipse.cdt.core.parser.ast.IASTCompletionNode; +import org.eclipse.cdt.core.parser.ast.IASTNode; +import org.eclipse.cdt.core.parser.ast.IASTScope; +import org.eclipse.cdt.core.testplugin.CProjectHelper; +import org.eclipse.cdt.internal.core.browser.cache.TypeCacheManager; +import org.eclipse.cdt.internal.ui.editor.CEditor; +import org.eclipse.cdt.internal.ui.text.contentassist.CCompletionProcessor2; +import org.eclipse.cdt.ui.testplugin.CTestPlugin; +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IProjectDescription; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.NullProgressMonitor; +import org.eclipse.core.runtime.Path; +import org.eclipse.jface.action.IAction; +import org.eclipse.jface.text.Document; +import org.eclipse.jface.text.contentassist.ICompletionProposal; +import org.eclipse.ui.IEditorPart; +import org.eclipse.ui.IWorkbenchPage; +import org.eclipse.ui.PlatformUI; +import org.eclipse.ui.part.FileEditorInput; + +public abstract class CompletionProposalsBaseTest extends TestCase{ + protected static final String EMPTY_STRING = ""; //$NON-NLS-1$ + private final String projectName = "TestProject1"; //$NON-NLS-1$ + private final String projectType = "bin"; //$NON-NLS-1$ + private ICProject fCProject; + private IFile fCFile; + private IFile fHeaderFile; + private NullProgressMonitor monitor; + private ITranslationUnit tu = null; + private String buffer = EMPTY_STRING; + private Document document = null; + + + public CompletionProposalsBaseTest(String name) { + super(name); + } + + /* + * Derived classes have to provide there abstract methods + */ + protected abstract String getFileName(); + protected abstract String getFileFullPath(); + protected abstract String getHeaderFileName(); + protected abstract String getHeaderFileFullPath(); + protected abstract int getCompletionPosition(); + protected abstract String getExpectedScopeClassName(); + protected abstract String getExpectedContextClassName(); + protected abstract String getExpectedPrefix(); + protected abstract IASTCompletionNode.CompletionKind getExpectedKind(); + protected abstract String[] getExpectedResultsValues(); + protected String getFunctionOrConstructorName() { return EMPTY_STRING; } + + protected void setUp() throws Exception { + monitor = new NullProgressMonitor(); + + fCProject= CProjectHelper.createCProject(projectName, projectType); + fHeaderFile = fCProject.getProject().getFile(getHeaderFileName()); + String fileName = getFileName(); + fCFile = fCProject.getProject().getFile(fileName); + if ( (!fCFile.exists()) &&( !fHeaderFile.exists() )) { + try{ + FileInputStream headerFileIn = new FileInputStream( + CTestPlugin.getDefault().getFileInPlugin(new Path(getHeaderFileFullPath()))); + fHeaderFile.create(headerFileIn,false, monitor); + FileInputStream bodyFileIn = new FileInputStream( + CTestPlugin.getDefault().getFileInPlugin(new Path(getFileFullPath()))); + fCFile.create(bodyFileIn,false, monitor); + } catch (CoreException e) { + e.printStackTrace(); + } + } + if (!fCProject.getProject().hasNature(CCProjectNature.CC_NATURE_ID)) { + addNatureToProject(fCProject.getProject(), CCProjectNature.CC_NATURE_ID, null); + } + + //TEMPORARY: Disable type cache + TypeCacheManager typeCacheManager = TypeCacheManager.getInstance(); + typeCacheManager.setProcessTypeCacheEvents(false); + + // use the new indexer + //IndexManager indexManager = CCorePlugin.getDefault().getCoreModel().getIndexManager(); + } + + private static void addNatureToProject(IProject proj, String natureId, IProgressMonitor monitor) throws CoreException { + IProjectDescription description = proj.getDescription(); + String[] prevNatures= description.getNatureIds(); + String[] newNatures= new String[prevNatures.length + 1]; + System.arraycopy(prevNatures, 0, newNatures, 0, prevNatures.length); + newNatures[prevNatures.length]= natureId; + description.setNatureIds(newNatures); + proj.setDescription(description, monitor); + } + + protected void tearDown() { + CProjectHelper.delete(fCProject); + } + + public void testCompletionProposals() throws Exception { + // setup the translation unit, the buffer and the document + //ITranslationUnit header = (ITranslationUnit)CoreModel.getDefault().create(fHeaderFile); + ITranslationUnit tu = (ITranslationUnit)CoreModel.getDefault().create(fCFile); + buffer = tu.getBuffer().getContents(); +// document = new Document(buffer); + + IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage(); + FileEditorInput editorInput = new FileEditorInput(fCFile); + IEditorPart editorPart = page.openEditor(editorInput, "org.eclipse.cdt.ui.editor.CEditor"); + + CEditor editor = (CEditor) editorPart ; + IAction completionAction = editor.getAction("ContentAssistProposal"); + + CCompletionProcessor2 completionProcessor = new CCompletionProcessor2(editorPart); +// IWorkingCopy wc = null; +// try{ +// wc = tu.getWorkingCopy(); +// }catch (CModelException e){ +// fail("Failed to get working copy"); //$NON-NLS-1$ +// } + + // call the CompletionProcessor +// ICompletionProposal[] results = completionProcessor.evalProposals(document, pos, wc, null); + ICompletionProposal[] results = completionProcessor.computeCompletionProposals(editor.getCSourceViewer(), getCompletionPosition()); // (document, pos, wc, null); + assertTrue(results != null); + + // check the completion node + ASTCompletionNode currentCompletionNode = completionProcessor.getCurrentCompletionNode(); + IASTName[] names = currentCompletionNode.getNames(); + IASTName currentName = names[0]; +// IASTCompletionNode completionNode = (IASTCompletionNode) currentCompletionNode ; +// assertNotNull(completionNode); + assertNotNull(currentCompletionNode); + // scope +// IASTScope scope = completionNode.getCompletionScope(); +// assertNotNull(scope); +// assertTrue(scope.getClass().getName().endsWith(getExpectedScopeClassName())); +// // context +// IASTNode context = completionNode.getCompletionContext(); +// if(context == null) +// assertTrue(getExpectedContextClassName().equals("null")); //$NON-NLS-1$ +// else +// assertTrue(context.getClass().getName().endsWith(getExpectedContextClassName())); +// // kind +// IASTCompletionNode.CompletionKind kind = completionNode.getCompletionKind(); +// assertTrue(kind == getExpectedKind()); + // prefix +// String prefix = completionNode.getCompletionPrefix(); + String prefix = currentCompletionNode.getPrefix(); + assertEquals(prefix, getExpectedPrefix()); + +// assertEquals( completionNode.getFunctionName(), getFunctionOrConstructorName() ); + + String[] expected = getExpectedResultsValues(); + assertTrue(results.length >= expected.length); + + for (int i = 0; i< expected.length; i++){ + boolean found = false; + for(int j = 0; j< results.length; j++){ + ICompletionProposal proposal = results[j]; + String displayString = proposal.getDisplayString(); + if(expected[i].equals(displayString)){ + found = true; + break; + } + } + assertTrue( "Lookup failed for " + expected[i], found ); //$NON-NLS-1$ + } + + } + + /** + * @return Returns the buffer. + */ + public String getBuffer() { + return buffer; + } + + /** + * @return Returns the document. + */ + public Document getDocument() { + return document; + } + + /** + * @return Returns the tu. + */ + public ITranslationUnit getTranslationUnit() { + return tu; + } + +} diff --git a/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/contentassist2/CompletionTest_FunctionReference_Prefix.java b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/contentassist2/CompletionTest_FunctionReference_Prefix.java new file mode 100644 index 00000000000..98abdafe28f --- /dev/null +++ b/core/org.eclipse.cdt.ui.tests/ui/org/eclipse/cdt/ui/tests/text/contentassist2/CompletionTest_FunctionReference_Prefix.java @@ -0,0 +1,140 @@ +/******************************************************************************* + * Copyright (c) 2004 IBM Corporation and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * IBM Rational Software - Initial API and implementation + *******************************************************************************/ +package org.eclipse.cdt.ui.tests.text.contentassist2; + + +import junit.framework.Test; +import junit.framework.TestSuite; + +import org.eclipse.cdt.core.parser.ast.IASTCompletionNode.CompletionKind; + +/** + * @author hamer + * + * Testing Single_Name_Reference in parameter passing + * Bug# + * + */ +public class CompletionTest_FunctionReference_Prefix extends CompletionProposalsBaseTest{ + private final String fileName = "CompletionTestStart36.cpp"; + private final String fileFullPath ="resources/contentassist/" + fileName; + private final String headerFileName = "CompletionTestStart.h"; + private final String headerFileFullPath ="resources/contentassist/" + headerFileName; + private final String expectedScopeName = "ASTMethod"; + private final String expectedContextName = "null"; + private final CompletionKind expectedKind = CompletionKind.FUNCTION_REFERENCE; + private final String expectedPrefix = "x"; + private final String[] expectedResults = { + "xLocal", // TODO: should be "xLocal : int", + "xAClassField", // TODO: should be "xAClassField : float", + "xVariable", // TODO: should be "xVariable : int", + "xAClassMethod(int x) void", + "xFunction(void) bool", + "xOtherFunction(void) void", + "xNamespace", + "xOtherClass", + "xFirstEnum", + "xSecondEnum", + "xThirdEnum", + "xEnumeration", + /* FIXME: DOM search is currently case sensitive? + "XMacro(x,y)" */ + }; + + public CompletionTest_FunctionReference_Prefix(String name) { + super(name); + } + + public static Test suite() { + TestSuite suite= new TestSuite(CompletionTest_FunctionReference_Prefix.class.getName()); + suite.addTest(new CompletionTest_FunctionReference_Prefix("testCompletionProposals")); + return suite; + } + /* (non-Javadoc) + * @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getCompletionPosition() + */ + protected int getCompletionPosition() { + return getBuffer().indexOf(" x ") + 2; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getExpectedScope() + */ + protected String getExpectedScopeClassName() { + return expectedScopeName; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getExpectedContext() + */ + protected String getExpectedContextClassName() { + return expectedContextName; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getExpectedKind() + */ + protected CompletionKind getExpectedKind() { + return expectedKind; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getExpectedPrefix() + */ + protected String getExpectedPrefix() { + return expectedPrefix; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getExpectedResultsValues() + */ + protected String[] getExpectedResultsValues() { + return expectedResults; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getFileName() + */ + protected String getFileName() { + return fileName; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getFileFullPath() + */ + protected String getFileFullPath() { + return fileFullPath; + } + /* (non-Javadoc) + * @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getHeaderFileFullPath() + */ + protected String getHeaderFileFullPath() { + return headerFileFullPath; + } + + /* (non-Javadoc) + * @see org.eclipse.cdt.core.codeassist.tests.CompletionProposalsTest#getHeaderFileName() + */ + protected String getHeaderFileName() { + return headerFileName; + } + + + + + + /* (non-Javadoc) + * @see org.eclipse.cdt.ui.tests.text.contentassist.CompletionProposalsBaseTest#getFunctionOrConstructorName() + */ + protected String getFunctionOrConstructorName() { + return "xAClassMethod"; + } +}