1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-23 22:52:11 +02:00

Bug 463234 - Reuse more code between different selection tests

Change-Id: I312217b9324e0f1786e76d30d7c517a2f417b51d
Signed-off-by: Nathan Ridge <zeratul976@hotmail.com>
This commit is contained in:
Nathan Ridge 2015-03-29 22:28:47 -04:00 committed by Gerrit Code Review @ Eclipse.org
parent 94aa97e0b6
commit f4cf37f562
4 changed files with 128 additions and 199 deletions

View file

@ -0,0 +1,117 @@
/*******************************************************************************
* Copyright (c) 2015 Nathan Ridge 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:
* Nathan Ridge - Initial API and implementation
*******************************************************************************/
package org.eclipse.cdt.ui.tests.text.selection;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.text.TextSelection;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.FileEditorInput;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.model.ILanguage;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.ui.testplugin.EditorTestHelper;
import org.eclipse.cdt.ui.tests.BaseUITestCase;
import org.eclipse.cdt.internal.core.model.ASTCache.ASTRunnable;
import org.eclipse.cdt.internal.core.parser.ParserException;
import org.eclipse.cdt.internal.ui.editor.ASTProvider;
import org.eclipse.cdt.internal.ui.editor.CEditor;
import org.eclipse.cdt.internal.ui.search.actions.OpenDeclarationsAction;
/**
* Base class for all selection tests, using the indexer or not.
*/
public abstract class BaseSelectionTests extends BaseUITestCase {
private IProgressMonitor monitor = new NullProgressMonitor();
public BaseSelectionTests() {
super();
}
public BaseSelectionTests(String name) {
super(name);
}
/**
* Derived classes should override this to return 'true' if they run tests where the
* OpenDeclarationsAction can open a different editor than the one from which the action was invoked.
*/
protected boolean shouldUpdateEditor() {
return false;
}
protected IASTNode testF3(IFile file, int offset) throws ParserException, CoreException {
return testF3(file, offset, 0);
}
protected IASTNode testF3(IFile file, int offset, int length) throws ParserException, CoreException {
if (offset < 0)
throw new ParserException("offset can not be less than 0 and was " + offset); //$NON-NLS-1$
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
IEditorPart part = null;
try {
part = page.openEditor(new FileEditorInput(file), "org.eclipse.cdt.ui.editor.CEditor"); //$NON-NLS-1$
} catch (PartInitException e) {
assertFalse(true);
}
if (part instanceof CEditor) {
CEditor editor= (CEditor) part;
EditorTestHelper.joinReconciler(EditorTestHelper.getSourceViewer(editor), 100, 5000, 10);
editor.getSelectionProvider().setSelection(new TextSelection(offset,length));
final OpenDeclarationsAction action = (OpenDeclarationsAction) editor.getAction("OpenDeclarations"); //$NON-NLS-1$
action.runSync();
if (shouldUpdateEditor()) {
// update the file/part to point to the newly opened IFile/IEditorPart
part = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
assertTrue (part instanceof CEditor);
editor= (CEditor) part;
EditorTestHelper.joinReconciler(EditorTestHelper.getSourceViewer(editor), 0, 5000, 10);
}
// the action above should highlight the declaration, so now retrieve it and use that selection to get the IASTName selected on the TU
ISelection sel = editor.getSelectionProvider().getSelection();
final IASTName[] result= { null };
if (sel instanceof ITextSelection) {
final ITextSelection textSel = (ITextSelection)sel;
ITranslationUnit tu= (ITranslationUnit) editor.getInputCElement();
IStatus ok= ASTProvider.getASTProvider().runOnAST(tu, ASTProvider.WAIT_IF_OPEN, monitor, new ASTRunnable() {
@Override
public IStatus runOnAST(ILanguage language, IASTTranslationUnit ast) throws CoreException {
result[0]= ast.getNodeSelector(null).findName(textSel.getOffset(), textSel.getLength());
return Status.OK_STATUS;
}
});
assertTrue(ok.isOK());
return result[0];
}
}
return null;
}
}

View file

@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2005, 2010 IBM Corporation and others.
* Copyright (c) 2005, 2015 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
@ -8,6 +8,7 @@
* Contributors:
* IBM - Initial API and implementation
* Markus Schorn (Wind River Systems)
* Nathan Ridge
*******************************************************************************/
package org.eclipse.cdt.ui.tests.text.selection;
@ -19,15 +20,12 @@ import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.text.TextSelection;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.search.ui.ISearchResult;
@ -43,21 +41,11 @@ import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.FileEditorInput;
import org.eclipse.ui.texteditor.AbstractTextEditor;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.model.ILanguage;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.core.testplugin.FileManager;
import org.eclipse.cdt.ui.testplugin.EditorTestHelper;
import org.eclipse.cdt.ui.tests.BaseUITestCase;
import org.eclipse.cdt.internal.core.model.ASTCache.ASTRunnable;
import org.eclipse.cdt.internal.core.parser.ParserException;
import org.eclipse.cdt.internal.ui.editor.ASTProvider;
import org.eclipse.cdt.internal.ui.editor.CEditor;
import org.eclipse.cdt.internal.ui.editor.ICEditorActionDefinitionIds;
import org.eclipse.cdt.internal.ui.search.actions.OpenDeclarationsAction;
@ -66,7 +54,7 @@ import org.eclipse.cdt.internal.ui.search.actions.OpenDeclarationsAction;
*
* @author dsteffle
*/
public class BaseSelectionTestsIndexer extends BaseUITestCase {
public class BaseSelectionTestsIndexer extends BaseSelectionTests {
protected ICProject fCProject;
static FileManager fileManager = new FileManager();
IProgressMonitor monitor = new NullProgressMonitor();
@ -187,56 +175,9 @@ public class BaseSelectionTestsIndexer extends BaseUITestCase {
return "org.eclipse.cdt.ui.editor.CEditor";
}
protected IASTNode testF3(IFile file, int offset) throws ParserException, CoreException {
return testF3(file, offset, 0);
}
protected IASTNode testF3(IFile file, int offset, int length) throws ParserException, CoreException {
if (offset < 0)
throw new ParserException("offset can not be less than 0 and was " + offset); //$NON-NLS-1$
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
IEditorPart part = null;
try {
part = page.openEditor(new FileEditorInput(file), getEditorID(), true);
} catch (PartInitException e) {
assertFalse(true);
}
if (part instanceof CEditor) {
CEditor editor= (CEditor) part;
EditorTestHelper.joinReconciler(EditorTestHelper.getSourceViewer(editor), 0, 5000, 10);
((AbstractTextEditor)part).getSelectionProvider().setSelection(new TextSelection(offset,length));
final OpenDeclarationsAction action = (OpenDeclarationsAction) editor.getAction("OpenDeclarations"); //$NON-NLS-1$
action.runSync();
// update the file/part to point to the newly opened IFile/IEditorPart
part = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
assertTrue (part instanceof CEditor);
editor= (CEditor) part;
EditorTestHelper.joinReconciler(EditorTestHelper.getSourceViewer(editor), 0, 5000, 10);
// the action above should highlight the declaration, so now retrieve it and use that selection to get the IASTName selected on the TU
ISelection sel= editor.getSelectionProvider().getSelection();
final IASTName[] result= {null};
if (sel instanceof ITextSelection) {
final ITextSelection textSel = (ITextSelection)sel;
ITranslationUnit tu = editor.getInputCElement();
IStatus ok= ASTProvider.getASTProvider().runOnAST(tu, ASTProvider.WAIT_IF_OPEN, monitor, new ASTRunnable() {
@Override
public IStatus runOnAST(ILanguage language, IASTTranslationUnit ast) throws CoreException {
result[0]= ast.getNodeSelector(null).findName(textSel.getOffset(), textSel.getLength());
return Status.OK_STATUS;
}
});
assertTrue(ok.isOK());
return result[0];
}
}
return null;
@Override
protected boolean shouldUpdateEditor() {
return true;
}
protected ISelection testF3Selection(IFile file, int offset) throws ParserException {

View file

@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2004, 2013 IBM Corporation and others.
* Copyright (c) 2004, 2015 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
@ -29,41 +29,23 @@ import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.text.IDocument;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.text.TextSelection;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.FileEditorInput;
import org.eclipse.ui.texteditor.AbstractTextEditor;
import org.eclipse.ui.texteditor.ITextEditor;
import org.eclipse.cdt.core.CCorePlugin;
import org.eclipse.cdt.core.dom.IPDOMManager;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.model.ILanguage;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.core.testplugin.CProjectHelper;
import org.eclipse.cdt.core.testplugin.FileManager;
import org.eclipse.cdt.ui.CUIPlugin;
import org.eclipse.cdt.ui.testplugin.EditorTestHelper;
import org.eclipse.cdt.ui.tests.BaseUITestCase;
import org.eclipse.cdt.internal.core.dom.parser.ASTNode;
import org.eclipse.cdt.internal.core.model.ASTCache.ASTRunnable;
import org.eclipse.cdt.internal.core.parser.ParserException;
import org.eclipse.cdt.internal.ui.editor.ASTProvider;
import org.eclipse.cdt.internal.ui.search.actions.OpenDeclarationsAction;
/**
@ -75,7 +57,7 @@ import org.eclipse.cdt.internal.ui.search.actions.OpenDeclarationsAction;
*
* @author dsteffle
*/
public class CPPSelectionTestsNoIndexer extends BaseUITestCase {
public class CPPSelectionTestsNoIndexer extends BaseSelectionTests {
private static final String INDEX_FILE_ID = "2946365241"; //$NON-NLS-1$
static NullProgressMonitor monitor;
static IWorkspace workspace;
@ -238,52 +220,6 @@ public class CPPSelectionTestsNoIndexer extends BaseUITestCase {
return file;
}
protected IASTNode testF3(IFile file, int offset) throws ParserException, CoreException {
return testF3(file, offset, 0);
}
protected IASTNode testF3(IFile file, int offset, int length) throws ParserException, CoreException {
if (offset < 0)
throw new ParserException("offset can not be less than 0 and was " + offset); //$NON-NLS-1$
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
IEditorPart part = null;
try {
part = page.openEditor(new FileEditorInput(file), "org.eclipse.cdt.ui.editor.CEditor"); //$NON-NLS-1$
EditorTestHelper.joinReconciler(EditorTestHelper.getSourceViewer((AbstractTextEditor) part), 100, 5000, 10);
} catch (PartInitException e) {
assertFalse(true);
}
if (part instanceof ITextEditor) {
ITextEditor editor= (ITextEditor) part;
editor.getSelectionProvider().setSelection(new TextSelection(offset,length));
final OpenDeclarationsAction action = (OpenDeclarationsAction) ((AbstractTextEditor) part).getAction("OpenDeclarations"); //$NON-NLS-1$
action.runSync();
// the action above should highlight the declaration, so now retrieve it and use that selection to get the IASTName selected on the TU
ISelection sel = ((AbstractTextEditor)part).getSelectionProvider().getSelection();
final IASTName[] result= { null };
if (sel instanceof ITextSelection) {
final ITextSelection textSel = (ITextSelection)sel;
ITranslationUnit tu= CUIPlugin.getDefault().getWorkingCopyManager().getWorkingCopy(editor.getEditorInput());
IStatus ok= ASTProvider.getASTProvider().runOnAST(tu, ASTProvider.WAIT_IF_OPEN, monitor, new ASTRunnable() {
@Override
public IStatus runOnAST(ILanguage language, IASTTranslationUnit ast) throws CoreException {
result[0]= ast.getNodeSelector(null).findName(textSel.getOffset(), textSel.getLength());
return Status.OK_STATUS;
}
});
assertTrue(ok.isOK());
return result[0];
}
}
return null;
}
private void assertContents(String code, int offset, String expected) {
assertEquals(expected, code.substring(offset, offset + expected.length()));
}

View file

@ -1,5 +1,5 @@
/*******************************************************************************
* Copyright (c) 2004, 2010 IBM Corporation and others.
* Copyright (c) 2004, 2015 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
@ -8,6 +8,7 @@
* Contributors:
* IBM - Initial API and implementation
* Markus Schorn (Wind River Systems)
* Nathan Ridge
*******************************************************************************/
package org.eclipse.cdt.ui.tests.text.selection;
@ -26,38 +27,18 @@ import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Status;
import org.eclipse.jface.text.ITextSelection;
import org.eclipse.jface.text.TextSelection;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.part.FileEditorInput;
import org.eclipse.ui.texteditor.AbstractTextEditor;
import org.eclipse.cdt.core.dom.IPDOMManager;
import org.eclipse.cdt.core.dom.ast.IASTName;
import org.eclipse.cdt.core.dom.ast.IASTNode;
import org.eclipse.cdt.core.dom.ast.IASTTranslationUnit;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.model.ILanguage;
import org.eclipse.cdt.core.model.ITranslationUnit;
import org.eclipse.cdt.core.testplugin.CProjectHelper;
import org.eclipse.cdt.core.testplugin.FileManager;
import org.eclipse.cdt.ui.testplugin.EditorTestHelper;
import org.eclipse.cdt.ui.tests.BaseUITestCase;
import org.eclipse.cdt.internal.core.dom.parser.ASTNode;
import org.eclipse.cdt.internal.core.model.ASTCache.ASTRunnable;
import org.eclipse.cdt.internal.core.parser.ParserException;
import org.eclipse.cdt.internal.ui.editor.ASTProvider;
import org.eclipse.cdt.internal.ui.editor.CEditor;
import org.eclipse.cdt.internal.ui.search.actions.OpenDeclarationsAction;
/**
@ -67,7 +48,7 @@ import org.eclipse.cdt.internal.ui.search.actions.OpenDeclarationsAction;
*
* @author dsteffle
*/
public class CSelectionTestsNoIndexer extends BaseUITestCase {
public class CSelectionTestsNoIndexer extends BaseSelectionTests {
private static final String INDEX_FILE_ID = "2324852323"; //$NON-NLS-1$
static NullProgressMonitor monitor;
@ -259,52 +240,6 @@ public class CSelectionTestsNoIndexer extends BaseUITestCase {
return folder;
}
protected IASTNode testF3(IFile file, int offset) throws ParserException, CoreException {
return testF3(file, offset, 0);
}
protected IASTNode testF3(IFile file, int offset, int length) throws ParserException, CoreException {
if (offset < 0)
throw new ParserException("offset can not be less than 0 and was " + offset); //$NON-NLS-1$
IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
IEditorPart part = null;
try {
part = page.openEditor(new FileEditorInput(file), "org.eclipse.cdt.ui.editor.CEditor"); //$NON-NLS-1$
} catch (PartInitException e) {
assertFalse(true);
}
if (part instanceof CEditor) {
CEditor editor= (CEditor) part;
EditorTestHelper.joinReconciler(EditorTestHelper.getSourceViewer(editor), 100, 5000, 10);
editor.getSelectionProvider().setSelection(new TextSelection(offset,length));
final OpenDeclarationsAction action = (OpenDeclarationsAction) editor.getAction("OpenDeclarations"); //$NON-NLS-1$
action.runSync();
// the action above should highlight the declaration, so now retrieve it and use that selection to get the IASTName selected on the TU
ISelection sel = ((AbstractTextEditor)part).getSelectionProvider().getSelection();
final IASTName[] result= {null};
if (sel instanceof ITextSelection) {
final ITextSelection textSel = (ITextSelection)sel;
ITranslationUnit tu = (ITranslationUnit) editor.getInputCElement();
IStatus ok= ASTProvider.getASTProvider().runOnAST(tu, ASTProvider.WAIT_IF_OPEN, monitor, new ASTRunnable() {
@Override
public IStatus runOnAST(ILanguage language, IASTTranslationUnit ast) throws CoreException {
result[0]= ast.getNodeSelector(null).findName(textSel.getOffset(), textSel.getLength());
return Status.OK_STATUS;
}
});
assertTrue(ok.isOK());
return result[0];
}
}
return null;
}
public void testBasicDefinition() throws Exception {
StringBuffer buffer = new StringBuffer();
buffer.append("extern int MyInt; // def is in another file \n"); //$NON-NLS-1$