Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 3567cdf7684499d8f83b32c6991338e806cfc7b1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/**********************************************************************
 * Copyright (c) 2004 Rational Software Corporation and others.
 * All rights reserved.   This program and the accompanying materials
 * are made available under the terms of the Common Public License v0.5
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/cpl-v05.html
 * 
 * Contributors: 
 * IBM Rational Software - Initial API and implementation
***********************************************************************/
package org.eclipse.cdt.ui.tests.text.contentassist;

/**
 * @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.CCorePlugin;
import org.eclipse.cdt.core.model.CModelException;
import org.eclipse.cdt.core.model.ICProject;
import org.eclipse.cdt.core.model.IWorkingCopy;
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.internal.core.model.TranslationUnit;
import org.eclipse.cdt.internal.core.search.indexing.IndexManager;
import org.eclipse.cdt.internal.ui.text.contentassist.CCompletionProcessor;
import org.eclipse.cdt.testplugin.CProjectHelper;
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.text.Document;
import org.eclipse.jface.text.contentassist.ICompletionProposal;

public abstract class CompletionProposalsBaseTest  extends TestCase{
	private final String pluginName = "org.eclipse.cdt.ui.tests";
	private final String projectName = "TestProject1";
	private final String projectType = "bin";
	private ICProject fCProject;
	private IFile fCFile;
	private IFile fHeaderFile;
	private NullProgressMonitor monitor;
	private TranslationUnit tu = null;
	private String buffer = "";
	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 void setUp() throws Exception {
		monitor = new NullProgressMonitor();
		String pluginRoot=org.eclipse.core.runtime.Platform.getPlugin(pluginName).find(new Path("/")).getFile();
		
		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(pluginRoot+ getHeaderFileFullPath()); 
				fHeaderFile.create(headerFileIn,false, monitor);  
				String fileFullPath = pluginRoot+ getFileFullPath();
				FileInputStream bodyFileIn = new FileInputStream(fileFullPath); 
				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);
		}

		// use the new indexer
		IndexManager indexManager = CCorePlugin.getDefault().getCoreModel().getIndexManager();
		indexManager.reset();		
	}

	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(){
		try{
			// setup the translation unit, the buffer and the document
			TranslationUnit header = new TranslationUnit(fCProject, fHeaderFile);
			tu = new TranslationUnit(fCProject, fCFile);
			buffer = tu.getBuffer().getContents();
			document = new Document(buffer);
			
			int pos = getCompletionPosition();
			
			CCompletionProcessor completionProcessor = new CCompletionProcessor(null);
			IWorkingCopy wc = null;
			try{
				wc = tu.getWorkingCopy();
			}catch (CModelException e){
				fail("Failed to get working copy");
			}
		
			// call the CompletionProcessor
			ICompletionProposal[] results = completionProcessor.evalProposals(document, pos, wc, null);
			assertTrue(results != null);
			
			// check the completion node
			IASTCompletionNode completionNode = completionProcessor.getCurrentCompletionNode();
			assertNotNull(completionNode);
			// scope
			IASTScope scope = completionNode.getCompletionScope();
			assertNotNull(scope);
			String scopeClassName = scope.getClass().getName();
			assertTrue(scope.getClass().getName().endsWith(getExpectedScopeClassName()));
			// context
			IASTNode context = completionNode.getCompletionContext();
			if(context == null)
				assertTrue(getExpectedContextClassName().equals("null"));
			else
				assertTrue(context.getClass().getName().endsWith(getExpectedContextClassName()));
			// kind
			IASTCompletionNode.CompletionKind kind = completionNode.getCompletionKind();
			assertTrue(kind == getExpectedKind());
			// prefix
			String prefix = completionNode.getCompletionPrefix();
			assertEquals(prefix, getExpectedPrefix());
			
			String[] expected = getExpectedResultsValues();
			assertTrue(results.length >= expected.length);
			
			for (int i = 0; i< expected.length; i++){
				ICompletionProposal proposal = results[i];
				String displayString = proposal.getDisplayString();
				assertEquals(displayString, expected[i]);
			}	
			
		} catch(CModelException e){
		}			
	}	
	
	/**
	 * @return Returns the buffer.
	 */
	public String getBuffer() {
		return buffer;
	}

	/**
	 * @return Returns the document.
	 */
	public Document getDocument() {
		return document;
	}

	/**
	 * @return Returns the tu.
	 */
	public TranslationUnit getTranslationUnit() {
		return tu;
	}

}

Back to the top