1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-24 17:35:35 +02:00

Test cases for Call Hierarchy

This commit is contained in:
Markus Schorn 2006-09-05 12:51:14 +00:00
parent 5c49412ff9
commit 001c458e92
9 changed files with 478 additions and 45 deletions

View file

@ -7,6 +7,7 @@
*
* Contributors:
* QNX - Initial API and implementation
* Markus Schorn (Wind River Systems)
*******************************************************************************/
package org.eclipse.cdt.internal.core.pdom.dom.c;
@ -55,7 +56,7 @@ public class PDOMCLinkage extends PDOMLinkage {
}
public PDOMCLinkage(PDOM pdom) throws CoreException {
super(pdom, GCCLanguage.ID, "C".toCharArray());
super(pdom, GCCLanguage.ID, "C".toCharArray()); //$NON-NLS-1$
}
public int getNodeType() {
@ -225,8 +226,14 @@ public class PDOMCLinkage extends PDOMLinkage {
}
public PDOMBinding adaptBinding(IBinding binding) throws CoreException {
if (binding instanceof PDOMBinding)
return (PDOMBinding)binding;
if (binding instanceof PDOMBinding) {
// there is no guarantee, that the binding is from the same PDOM object.
PDOMBinding pdomBinding = (PDOMBinding) binding;
if (pdomBinding.getPDOM() == getPDOM()) {
return pdomBinding;
}
// so if the binding is from another pdom it has to be adapted.
}
PDOMNode parent = getParent(binding);
if (parent == this) {

View file

@ -15,6 +15,7 @@ package org.eclipse.cdt.ui.tests;
import junit.framework.Test;
import junit.framework.TestSuite;
import org.eclipse.cdt.ui.tests.callhierarchy.CallHierarchyTestSuite;
import org.eclipse.cdt.ui.tests.text.TextTestSuite;
import org.eclipse.cdt.ui.tests.text.contentassist.ContentAssistTestSuite;
import org.eclipse.cdt.ui.tests.viewsupport.ViewSupportTestSuite;
@ -40,9 +41,12 @@ public class AutomatedSuite extends TestSuite {
// tests from package org.eclipse.cdt.ui.tests.text
addTest(TextTestSuite.suite());
// tests for package viewsupport
// tests for package org.eclipse.cdt.ui.tests.viewsupport
addTest(ViewSupportTestSuite.suite());
// tests for package org.eclipse.cdt.ui.tests.callhierarchy
addTest(CallHierarchyTestSuite.suite());
// tests from package org.eclipse.cdt.ui.tests.text.contentAssist
addTest(ContentAssistTestSuite.suite());

View file

@ -0,0 +1,115 @@
/*******************************************************************************
* Copyright (c) 2006 Wind River Systems, Inc. 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:
* Markus Schorn - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.ui.tests;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import junit.framework.TestCase;
import org.eclipse.core.resources.IContainer;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.FileLocator;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.cdt.ui.testplugin.CTestPlugin;
public class BaseTestCase extends TestCase {
/**
* Reads a section in comments form the source of the given class. The section
* is started with '// {tag}' and ends with the first line not started by '//'
* @since 4.0
*/
protected String readTaggedComment(Class clazz, final String tag) throws IOException {
IPath filePath= new Path("ui/" + clazz.getName().replace('.', '/') + ".java");
InputStream in= FileLocator.openStream(CTestPlugin.getDefault().getBundle(), filePath, false);
LineNumberReader reader= new LineNumberReader(new InputStreamReader(in));
boolean found= false;
final StringBuffer content= new StringBuffer();
try {
String line= reader.readLine();
while (line != null) {
line= line.trim();
if (line.startsWith("//")) {
line= line.substring(2);
if (found) {
content.append(line);
content.append('\n');
}
else {
line= line.trim();
if (line.startsWith("{" + tag)) {
if (line.length() == tag.length()+1 ||
!Character.isJavaIdentifierPart(line.charAt(tag.length()+1))) {
found= true;
}
}
}
}
else if (found) {
break;
}
line= reader.readLine();
}
}
finally {
reader.close();
}
assertTrue("Tag '" + tag + "' is not defined inside of '" + filePath + "'.", found);
return content.toString();
}
/**
* Reads a section in comments form the source of the given class. Fully
* equivalent to <code>readTaggedComment(getClass(), tag)</code>
* @since 4.0
*/
protected String readTaggedComment(final String tag) throws IOException {
return readTaggedComment(getClass(), tag);
}
/**
* Creates a file with content at the given path inside the given container.
* If the file exists its content is replaced.
* @param container a container to create the file in
* @param filePath the path relative to the container to create the file at
* @param contents the content for the file
* @return a file object.
* @throws Exception
* @since 4.0
*/
protected IFile createFile(IContainer container, IPath filePath, String contents) throws Exception {
//Obtain file handle
IFile file = container.getFile(filePath);
InputStream stream = new ByteArrayInputStream(contents.getBytes());
//Create file input stream
if (file.exists()) {
file.setContents(stream, false, false, new NullProgressMonitor());
}
else {
file.create(stream, false, new NullProgressMonitor());
}
return file;
}
protected IFile createFile(IContainer container, String fileName, String contents) throws Exception {
return createFile(container, new Path(fileName), contents);
}
}

View file

@ -0,0 +1,119 @@
/*******************************************************************************
* Copyright (c) 2006 Wind River Systems, Inc. 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:
* Markus Schorn - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.ui.tests.callhierarchy;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.swt.widgets.TreeItem;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.testplugin.CProjectHelper;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.cdt.ui.tests.BaseTestCase;
import org.eclipse.cdt.internal.core.pdom.PDOM;
import org.eclipse.cdt.internal.core.pdom.dom.PDOMFile;
import org.eclipse.cdt.internal.ui.callhierarchy.CHViewPart;
import org.eclipse.cdt.internal.ui.callhierarchy.CallHierarchyUI;
import org.eclipse.cdt.internal.ui.editor.CEditor;
public class CallHierarchyBaseTest extends BaseTestCase {
private ICProject fCProject;
private PDOM fPdom;
protected void setUp() throws CoreException {
fCProject= CProjectHelper.createCProject("__chTest__", "bin");
CCorePlugin.getPDOMManager().setIndexerId(fCProject, "org.eclipse.cdt.core.fastIndexer");
fPdom= (PDOM) CCorePlugin.getPDOMManager().getPDOM(fCProject);
}
protected void tearDown() throws CoreException {
if (fCProject != null) {
fCProject.getProject().delete(IProject.FORCE | IProject.ALWAYS_DELETE_PROJECT_CONTENT, new NullProgressMonitor());
}
}
protected IProject getProject() {
return fCProject.getProject();
}
protected void waitForIndexer(IFile file, int maxmillis) throws Exception {
long endTime= System.currentTimeMillis() + maxmillis;
do {
fPdom.acquireReadLock();
try {
PDOMFile pfile= fPdom.getFile(file.getLocation());
// mstodo check timestamp
if (pfile != null) {
return;
}
}
finally {
fPdom.releaseReadLock();
}
Thread.sleep(50);
} while (System.currentTimeMillis() < endTime);
}
protected void openCallHierarchy(CEditor editor) {
CallHierarchyUI.setIsJUnitTest(true);
CallHierarchyUI.open(editor, (ITextSelection) editor.getSelectionProvider().getSelection());
}
protected Tree getCHTree(IWorkbenchPage page) {
CHViewPart ch= (CHViewPart)page.findView(CUIPlugin.ID_CALL_HIERARCHY);
assertNotNull(ch);
Tree tree= ch.getTreeViewer().getTree();
return tree;
}
protected void runEventQueue(int time) {
long endTime= System.currentTimeMillis()+time;
do {
while (Display.getCurrent().readAndDispatch());
}
while(System.currentTimeMillis() < endTime);
}
protected void checkTreeNode(Tree tree, int i0, String label) {
TreeItem root= null;
try {
root= tree.getItem(i0);
}
catch (IllegalArgumentException e) {
assertTrue("Tree node " + i0 + " does not exist!", false);
}
assertEquals(label, root.getText());
}
protected void checkTreeNode(Tree tree, int i0, int i1, String label) {
TreeItem item= null;
try {
TreeItem root= tree.getItem(i0);
item= root.getItem(i1);
}
catch (IllegalArgumentException e) {
assertTrue("Tree node " + i0 + "," + i1 + " does not exist!", false);
}
assertEquals(label, item.getText());
}
}

View file

@ -0,0 +1,26 @@
/*******************************************************************************
* Copyright (c) 2006 Wind River Systems, Inc. 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:
* Markus Schorn - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.ui.tests.callhierarchy;
import junit.framework.TestSuite;
public class CallHierarchyTestSuite extends TestSuite {
public static TestSuite suite() {
return new CallHierarchyTestSuite();
}
public CallHierarchyTestSuite() {
super("Tests in package org.eclipse.cdt.ui.tests.callhierarchy");
addTestSuite(OpenCallHierarchyFromEditorTest.class);
}
}

View file

@ -0,0 +1,111 @@
/*******************************************************************************
* Copyright (c) 2006 Wind River Systems, Inc. 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:
* Markus Schorn - initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.ui.tests.callhierarchy;
import org.eclipse.core.resources.IFile;
import org.eclipse.swt.widgets.Tree;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.ide.IDE;
import org.eclipse.cdt.internal.ui.editor.CEditor;
public class OpenCallHierarchyFromEditorTest extends CallHierarchyBaseTest {
// {testFunctions}
// void proto();
// void func() {
// };
// void main() {
// proto(); //ref
// func(); //ref
// };
public void testFunctions() throws Exception {
String content = readTaggedComment("testFunctions");
IFile file= createFile(getProject(), "functions.c", content);
waitForIndexer(file, 1000);
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
CEditor editor= (CEditor) IDE.openEditor(page, file);
editor.selectAndReveal(content.indexOf("proto"), 5);
openCallHierarchy(editor);
runEventQueue(100);
Tree tree = getCHTree(page);
checkTreeNode(tree, 0, "proto()");
checkTreeNode(tree, 0, 0, "main()");
editor.selectAndReveal(content.indexOf("func"), 2);
openCallHierarchy(editor);
runEventQueue(100);
checkTreeNode(tree, 0, "func()");
checkTreeNode(tree, 0, 0, "main()");
editor.selectAndReveal(content.indexOf("proto(); //ref"), 0);
openCallHierarchy(editor);
runEventQueue(100);
tree = getCHTree(page);
checkTreeNode(tree, 0, "proto()");
checkTreeNode(tree, 0, 0, "main()");
editor.selectAndReveal(content.indexOf("func(); //ref"), 7);
openCallHierarchy(editor);
runEventQueue(100);
tree = getCHTree(page);
checkTreeNode(tree, 0, "func()");
checkTreeNode(tree, 0, 0, "main()");
}
// {testVariables}
// extern int extern_var;
// int global_var= 0;
// void main() {
// int i= extern_var; //ref
// i= global_var; //ref
// };
public void testVariables() throws Exception {
String content = readTaggedComment("testVariables");
IFile file= createFile(getProject(), "variables.c", content);
waitForIndexer(file, 1000);
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
CEditor editor= (CEditor) IDE.openEditor(page, file);
editor.selectAndReveal(content.indexOf("extern_var"), 0);
openCallHierarchy(editor);
runEventQueue(100);
Tree tree = getCHTree(page);
checkTreeNode(tree, 0, "extern_var");
checkTreeNode(tree, 0, 0, "main()");
editor.selectAndReveal(content.indexOf("global_var"), 2);
openCallHierarchy(editor);
runEventQueue(100);
checkTreeNode(tree, 0, "global_var");
checkTreeNode(tree, 0, 0, "main()");
editor.selectAndReveal(content.indexOf("extern_var; //ref"), 0);
openCallHierarchy(editor);
runEventQueue(100);
tree = getCHTree(page);
checkTreeNode(tree, 0, "extern_var");
checkTreeNode(tree, 0, 0, "main()");
editor.selectAndReveal(content.indexOf("global_var; //ref"), 7);
openCallHierarchy(editor);
runEventQueue(100);
tree = getCHTree(page);
checkTreeNode(tree, 0, "global_var");
checkTreeNode(tree, 0, 0, "main()");
}
}

View file

@ -30,6 +30,7 @@ import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.jface.viewers.OpenEvent;
import org.eclipse.jface.viewers.StructuredSelection;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.jface.viewers.ViewerComparator;
import org.eclipse.jface.viewers.ViewerFilter;
@ -773,4 +774,8 @@ public class CHViewPart extends ViewPart {
}
return null;
}
public TreeViewer getTreeViewer() {
return fTreeViewer;
}
}

View file

@ -39,7 +39,12 @@ import org.eclipse.cdt.internal.ui.viewsupport.CElementLabels;
import org.eclipse.cdt.internal.ui.viewsupport.FindNameForSelectionVisitor;
public class CallHierarchyUI {
private static boolean sIsJUnitTest= false;
public static void setIsJUnitTest(boolean val) {
sIsJUnitTest= val;
}
public static CHViewPart open(ICElement input, IWorkbenchWindow window) {
if (input != null) {
return openInViewPart(window, input);
@ -68,6 +73,9 @@ public class CallHierarchyUI {
elem = input[0];
break;
default:
if (sIsJUnitTest) {
throw new RuntimeException("ambigous input"); //$NON-NLS-1$
}
elem = OpenActionUtil.selectCElement(input, window.getShell(),
CHMessages.CallHierarchyUI_label, CHMessages.CallHierarchyUI_selectMessage,
CElementLabels.ALL_DEFAULT | CElementLabels.MF_POST_FILE_QUALIFIED, 0);
@ -85,49 +93,72 @@ public class CallHierarchyUI {
final IEditorInput editorInput = editor.getEditorInput();
final Display display= Display.getCurrent();
Job job= new Job(CHMessages.CallHierarchyUI_label) {
protected IStatus run(IProgressMonitor monitor) {
try {
final ICElement[] elems= findDefinitions();
if (elems != null && elems.length > 0) {
display.asyncExec(new Runnable() {
public void run() {
openInViewPart(editor.getSite().getWorkbenchWindow(), elems);
}});
}
return Status.OK_STATUS;
}
catch (CoreException e) {
return e.getStatus();
}
}
private ICElement[] findDefinitions() throws CoreException {
CIndexQueries index= CIndexQueries.getInstance();
IASTName name= getSelectedName(editorInput, sel);
if (name != null) {
if (name.isDefinition()) {
ICElement elem= index.findDefinition(project, name);
if (elem != null) {
return new ICElement[]{elem};
if (!sIsJUnitTest) {
Job job= new Job(CHMessages.CallHierarchyUI_label) {
protected IStatus run(IProgressMonitor monitor) {
try {
final ICElement[] elems= findDefinitions(project, editorInput, sel);
if (elems != null && elems.length > 0) {
display.asyncExec(new Runnable() {
public void run() {
openInViewPart(editor.getSite().getWorkbenchWindow(), elems);
}});
}
}
else {
ICElement[] elems= index.findAllDefinitions(project, name);
if (elems.length == 0) {
ICProject[] allProjects= CoreModel.getDefault().getCModel().getCProjects();
elems= index.findAllDefinitions(allProjects, name);
}
return elems;
return Status.OK_STATUS;
}
catch (CoreException e) {
return e.getStatus();
}
}
return null;
};
job.setUser(true);
job.schedule();
}
else {
ICElement[] elems;
try {
elems = findDefinitions(project, editorInput, sel);
if (elems != null && elems.length > 0) {
openInViewPart(editor.getSite().getWorkbenchWindow(), elems);
}
} catch (CoreException e) {
CUIPlugin.getDefault().log(e);
}
};
job.setUser(true);
job.schedule();
}
}
}
private static ICElement[] findDefinitions(ICProject project, IEditorInput editorInput, ITextSelection sel) throws CoreException {
CIndexQueries index= CIndexQueries.getInstance();
IASTName name= getSelectedName(editorInput, sel);
if (name != null) {
if (name.isDefinition()) {
ICElement elem= index.findDefinition(project, name);
if (elem != null) {
return new ICElement[]{elem};
}
}
else {
ICElement[] elems= index.findAllDefinitions(project, name);
if (elems.length == 0) {
ICProject[] allProjects= CoreModel.getDefault().getCModel().getCProjects();
elems= index.findAllDefinitions(allProjects, name);
if (elems.length == 0) {
ICElement elem= index.findAnyDeclaration(project, name);
if (elem == null) {
elem= index.findAnyDeclaration(allProjects, name);
}
if (elem != null) {
elems= new ICElement[] {elem};
}
}
}
return elems;
}
}
return null;
}
private static IASTName getSelectedName(IEditorInput editorInput, ITextSelection selection) throws CoreException {
int selectionStart = selection.getOffset();

View file

@ -44,6 +44,7 @@ import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.cdt.internal.core.pdom.PDOM;
import org.eclipse.cdt.internal.core.pdom.dom.PDOMFile;
import org.eclipse.cdt.internal.core.pdom.dom.PDOMInclude;
import org.eclipse.cdt.internal.core.pdom.dom.PDOMLinkage;
import org.eclipse.cdt.internal.core.pdom.dom.PDOMName;
import org.eclipse.cdt.internal.corext.util.CModelUtil;
@ -246,7 +247,7 @@ public class CIndexQueries {
if (pdom != null) {
pdom.acquireReadLock();
try {
IBinding binding= pdom.resolveBinding(name);
IBinding binding= getPDOMBinding(pdom, name);
if (binding != null) {
IASTName[] names= pdom.getReferences(binding);
for (int i = 0; i < names.length; i++) {
@ -404,7 +405,7 @@ public class CIndexQueries {
if (pdom != null) {
pdom.acquireReadLock();
try {
IBinding binding= pdom.resolveBinding(name);
IBinding binding= getPDOMBinding(pdom, name);
if (binding != null) {
return allEnclosingElements(project, pdom.getDefinitions(binding));
}
@ -421,6 +422,20 @@ public class CIndexQueries {
return EMPTY_ELEMENTS;
}
private IBinding getPDOMBinding(PDOM pdom, IASTName name) {
IBinding binding= name.resolveBinding();
IASTTranslationUnit tu= name.getTranslationUnit();
ILanguage lang= tu.getLanguage();
PDOMLinkage linkage;
try {
linkage = pdom.getLinkage(lang);
return linkage.adaptBinding(binding);
} catch (CoreException e) {
CUIPlugin.getDefault().log(e);
}
return null;
}
private ICElement[] allEnclosingElements(ICProject project, IASTName[] defs) {
if (defs != null && defs.length > 0) {
ArrayList result= new ArrayList(defs.length);
@ -484,7 +499,7 @@ public class CIndexQueries {
if (pdom != null) {
pdom.acquireReadLock();
try {
IBinding binding= pdom.resolveBinding(name);
IBinding binding= getPDOMBinding(pdom, name);
if (binding != null) {
ICElement elem= firstEnclosingElement(project, pdom.getDefinitions(binding));
if (elem != null) {
@ -537,7 +552,7 @@ public class CIndexQueries {
if (pdom != null) {
pdom.acquireReadLock();
try {
IBinding binding= pdom.resolveBinding(name);
IBinding binding= getPDOMBinding(pdom, name);
if (binding != null) {
ICElement elem= firstEnclosingElement(project, pdom.getDefinitions(binding));
if (elem != null) {