Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 06e1bded2a412574993d54fe376a0d738bc05515 (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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
/*******************************************************************************
 * Copyright (c) 2004, 2014 IBM Corporation and others.
 *
 * This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-2.0/
 *
 * SPDX-License-Identifier: EPL-2.0
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Markus Schorn (Wind River Systems)
 *******************************************************************************/
package org.eclipse.cdt.ui.tests.text.contentassist;

import java.io.ByteArrayInputStream;
import java.io.InputStream;

import org.eclipse.cdt.core.dom.IPDOMManager;
import org.eclipse.cdt.core.model.CModelException;
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.model.IWorkingCopy;
import org.eclipse.cdt.core.testplugin.CProjectHelper;
import org.eclipse.cdt.internal.ui.CHelpProviderManager;
import org.eclipse.cdt.internal.ui.editor.CEditor;
import org.eclipse.cdt.internal.ui.text.CHelpBookDescriptor;
import org.eclipse.cdt.internal.ui.text.contentassist.CContentAssistProcessor;
import org.eclipse.cdt.ui.tests.BaseUITestCase;
import org.eclipse.cdt.ui.text.ICHelpInvocationContext;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.NullProgressMonitor;
import org.eclipse.jface.action.IAction;
import org.eclipse.jface.text.contentassist.ContentAssistant;
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;

import junit.framework.Test;
import junit.framework.TestSuite;

/**
 * @author aniefer
 */
public class ContentAssistTests extends BaseUITestCase {
	private final NullProgressMonitor monitor = new NullProgressMonitor();
	static IProject project;
	static ICProject cproject;
	static boolean disabledHelpContributions;

	@Override
	public void setUp() throws InterruptedException {
		//(CCorePlugin.getDefault().getCoreModel().getIndexManager()).reset();

		if (project == null) {
			try {
				cproject = CProjectHelper.createCCProject("ContentAssistTestProject", "bin", //$NON-NLS-1$//$NON-NLS-2$
						IPDOMManager.ID_FAST_INDEXER);
				project = cproject.getProject();
				waitForIndexer(cproject);
			} catch (CoreException e) {
				/*boo*/
			}
			if (project == null)
				fail("Unable to create project"); //$NON-NLS-1$
		}
	}

	public ContentAssistTests() {
		super();
	}

	/**
	 * @param name
	 */
	public ContentAssistTests(String name) {
		super(name);
	}

	private void disableContributions() {
		//disable the help books so we don't get proposals we weren't expecting
		CHelpBookDescriptor helpBooks[];
		helpBooks = CHelpProviderManager.getDefault().getCHelpBookDescriptors(new ICHelpInvocationContext() {
			@Override
			public IProject getProject() {
				return project;
			}

			@Override
			public ITranslationUnit getTranslationUnit() {
				return null;
			}
		});
		for (CHelpBookDescriptor helpBook : helpBooks) {
			if (helpBook != null)
				helpBook.enable(false);
		}
	}

	public static Test suite() {
		TestSuite suite = suite(ContentAssistTests.class, "_");
		suite.addTest(new ContentAssistTests("cleanupProject")); //$NON-NLS-1$
		return suite;
	}

	public void cleanupProject() throws Exception {
		closeAllEditors();
		try {
			project.delete(true, false, monitor);
			project = null;
		} catch (Throwable e) {
			/*boo*/
		}
	}

	@Override
	protected void tearDown() throws Exception {
		if (project == null || !project.exists())
			return;

		closeAllEditors();

		// wait for indexer before deleting project to avoid errors in the log
		waitForIndexer(cproject);

		IResource[] members = project.members();
		for (IResource member : members) {
			if (member.getName().equals(".project") || member.getName().equals(".cproject")) //$NON-NLS-1$ //$NON-NLS-2$
				continue;
			if (member.getName().equals(".settings"))
				continue;
			try {
				member.delete(false, monitor);
			} catch (Throwable e) {
				/*boo*/
			}
		}

	}

	protected IFile importFile(String fileName, String contents) throws Exception {
		// Obtain file handle
		IFile file = project.getProject().getFile(fileName);

		InputStream stream = new ByteArrayInputStream(contents.getBytes());
		// Create file input stream
		if (file.exists()) {
			file.setContents(stream, false, false, monitor);
		} else {
			file.create(stream, false, monitor);
		}

		return file;
	}

	protected ICompletionProposal[] getResults(IFile file, int offset) throws Exception {
		if (!disabledHelpContributions)
			disableContributions();

		ITranslationUnit tu = (ITranslationUnit) CoreModel.getDefault().create(file);
		String buffer = tu.getBuffer().getContents();
		IWorkingCopy wc = null;
		try {
			wc = tu.getWorkingCopy();
		} catch (CModelException e) {
			fail("Failed to get working copy"); //$NON-NLS-1$
		}

		// call the ContentAssistProcessor
		IWorkbenchPage page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage();
		FileEditorInput editorInput = new FileEditorInput(file);
		IEditorPart editorPart = page.openEditor(editorInput, "org.eclipse.cdt.ui.editor.CEditor");
		CEditor editor = (CEditor) editorPart;
		IAction completionAction = editor.getAction("ContentAssistProposal");

		String contentType = editor.getViewer().getDocument().getContentType(offset);
		ContentAssistant assistant = new ContentAssistant();
		CContentAssistProcessor processor = new CContentAssistProcessor(editor, assistant, contentType);
		return processor.computeCompletionProposals(editor.getViewer(), offset);
	}

	public void testBug69334a() throws Exception {
		importFile("test.h", "class Test{ public : Test( int ); }; \n"); //$NON-NLS-1$//$NON-NLS-2$
		StringBuilder buf = new StringBuilder();
		buf.append("#include \"test.h\"                \n"); //$NON-NLS-1$
		buf.append("Test::Test( int i ) { return; }    \n"); //$NON-NLS-1$
		buf.append("int main() {                       \n"); //$NON-NLS-1$
		buf.append("   int veryLongName = 1;           \n"); //$NON-NLS-1$
		buf.append("   Test * ptest = new Test( very   \n"); //$NON-NLS-1$

		String code = buf.toString();
		IFile cu = importFile("test.cpp", code); //$NON-NLS-1$

		ICompletionProposal[] results = getResults(cu, code.indexOf("very ") + 4); //$NON-NLS-1$

		assertEquals(1, results.length);
		assertEquals("veryLongName : int", results[0].getDisplayString()); //$NON-NLS-1$
	}

	public void testBug69334b() throws Exception {
		importFile("test.h", "class Test{ public : Test( int ); }; \n"); //$NON-NLS-1$//$NON-NLS-2$
		StringBuilder buf = new StringBuilder();
		buf.append("#include \"test.h\"                \n"); //$NON-NLS-1$
		buf.append("Test::Test( int i ) { return; }    \n"); //$NON-NLS-1$
		buf.append("int main() {                       \n"); //$NON-NLS-1$
		buf.append("   int veryLongName = 1;           \n"); //$NON-NLS-1$
		buf.append("   Test test( very   \n"); //$NON-NLS-1$

		String code = buf.toString();
		IFile cu = importFile("test.cpp", code); //$NON-NLS-1$

		ICompletionProposal[] results = getResults(cu, code.indexOf("very ") + 4); //$NON-NLS-1$

		assertEquals(1, results.length);
		assertEquals("veryLongName : int", results[0].getDisplayString()); //$NON-NLS-1$
	}

	public void testBug72824() throws Exception {
		StringBuilder buf = new StringBuilder();
		buf.append("class Strategy {                             \n"); //$NON-NLS-1$
		buf.append("public :                                     \n"); //$NON-NLS-1$
		buf.append("   enum _Ability { IDIOT, NORMAL, CHEAT } ;  \n"); //$NON-NLS-1$
		buf.append("   Strategy( _Ability a ) { }                \n"); //$NON-NLS-1$
		buf.append("   _Ability getAbility();                    \n"); //$NON-NLS-1$
		buf.append("};                                           \n"); //$NON-NLS-1$
		buf.append("int main(){                                  \n"); //$NON-NLS-1$

		String code = buf.toString();
		String c2 = code + "   Strategy *p[3] = { new Strategy( Str \n"; //$NON-NLS-1$

		IFile cu = importFile("strategy.cpp", c2); //$NON-NLS-1$

		ICompletionProposal[] results = getResults(cu, c2.indexOf("Str ") + 3); //$NON-NLS-1$
		assertEquals(1, results.length);
		assertEquals("Strategy", results[0].getDisplayString()); //$NON-NLS-1$

		c2 = code + "   Strategy *p[3] = { new Strategy( Strategy:: \n"; //$NON-NLS-1$

		cu = importFile("strategy.cpp", c2); //$NON-NLS-1$

		results = getResults(cu, c2.indexOf("::") + 2); //$NON-NLS-1$
		assertEquals(4, results.length);
		assertEquals("CHEAT", results[0].getDisplayString()); //$NON-NLS-1$
		assertEquals("IDIOT", results[1].getDisplayString()); //$NON-NLS-1$
		assertEquals("NORMAL", results[2].getDisplayString()); //$NON-NLS-1$
		// "_Ability" is here due to fix for bug 199598
		// Difficult to differentiate between declaration and expression context
		assertEquals("_Ability", results[3].getDisplayString()); //$NON-NLS-1$

		// in a method definition context, constructors and methods should be proposed

		c2 = code + "return 0;}\nStrategy::\n"; //$NON-NLS-1$

		cu = importFile("strategy.cpp", c2); //$NON-NLS-1$

		results = getResults(cu, c2.indexOf("::") + 2); //$NON-NLS-1$
		assertEquals(3, results.length);
		assertEquals("getAbility(void) : enum Strategy::_Ability", results[1].getDisplayString()); //$NON-NLS-1$
		assertEquals("Strategy(enum Strategy::_Ability a)", results[0].getDisplayString()); //$NON-NLS-1$
		assertEquals("_Ability", results[2].getDisplayString()); //$NON-NLS-1$
	}

	public void testBug72559() throws Exception {
		StringBuilder buf = new StringBuilder();
		buf.append("void foo(){               \n"); //$NON-NLS-1$
		buf.append("   int var;               \n"); //$NON-NLS-1$
		buf.append("   {                      \n"); //$NON-NLS-1$
		buf.append("      float var;          \n"); //$NON-NLS-1$
		buf.append("      v                   \n"); //$NON-NLS-1$
		buf.append("   }                      \n"); //$NON-NLS-1$
		buf.append("}                         \n"); //$NON-NLS-1$

		String code = buf.toString();
		IFile cu = importFile("t.cpp", code); //$NON-NLS-1$
		ICompletionProposal[] results = getResults(cu, code.indexOf("v ") + 1); //$NON-NLS-1$

		assertEquals(results.length, 3);
		assertEquals(results[0].getDisplayString(), "var : float"); //$NON-NLS-1$
		assertEquals(results[1].getDisplayString(), "virtual"); //$NON-NLS-1$
		assertEquals(results[2].getDisplayString(), "volatile"); //$NON-NLS-1$
	}
}

Back to the top