/
AbstractContentAssistTest.java
376 lines (334 loc) · 11.9 KB
/
AbstractContentAssistTest.java
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
/*
* $Id$
*
* SARL is an general-purpose agent programming language.
* More details on http://www.sarl.io
*
* Copyright (C) 2014-2017 the original authors or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.sarl.lang.ui.tests.contentassist;
import static org.junit.Assert.assertEquals;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import com.google.common.collect.Lists;
import com.google.inject.Binder;
import com.google.inject.Inject;
import com.google.inject.Injector;
import com.google.inject.Module;
import com.google.inject.name.Named;
import org.eclipse.core.resources.IFile;
import org.eclipse.jface.text.BadLocationException;
import org.eclipse.jface.text.contentassist.ICompletionProposal;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.IWorkbench;
import org.eclipse.ui.IWorkbenchWindow;
import org.eclipse.xtext.Constants;
import org.eclipse.xtext.IGrammarAccess;
import org.eclipse.xtext.junit4.internal.LineDelimiters;
import org.eclipse.xtext.junit4.ui.ContentAssistProcessorTestBuilder;
import org.eclipse.xtext.junit4.util.ResourceLoadHelper;
import org.eclipse.xtext.resource.XtextResource;
import org.eclipse.xtext.ui.editor.contentassist.ConfigurableCompletionProposal;
import org.eclipse.xtext.ui.editor.contentassist.ReplacementTextApplier;
import org.eclipse.xtext.ui.editor.model.IXtextDocument;
import org.eclipse.xtext.util.StringInputStream;
import org.junit.Assert;
import org.junit.ComparisonFailure;
import io.sarl.lang.sarl.SarlScript;
import io.sarl.tests.api.AbstractSarlUiTest;
import io.sarl.tests.api.WorkbenchTestHelper;
@SuppressWarnings("all")
public abstract class AbstractContentAssistTest extends AbstractSarlUiTest implements ResourceLoadHelper {
@Inject
private IGrammarAccess access;
@Override
protected Module[] getInjectionModules() {
final Module[] originals = super.getInjectionModules();
final Module[] newModules = Arrays.copyOf(originals, originals.length + 1);
newModules[newModules.length - 1] = new Module() {
@Override
public void configure(Binder binder) {
binder.bind(ContentAssistProcessorTestBuilder.Factory.class).to(ProcessorTestBuilderFactory.class);
}
};
return newModules;
}
@Override
public final XtextResource getResourceFor(InputStream stream) {
try {
final StringBuilder content = new StringBuilder();
try (final InputStreamReader reader = new InputStreamReader(stream)) {
final BufferedReader breader = new BufferedReader(reader);
String line = breader.readLine();
while (line != null) {
content.append(line);
content.append(getLineSeparator());
line = breader.readLine();
}
}
final WorkbenchTestHelper helper = helper();
final String strContent = content.toString();
final IFile file = helper.createFile(helper.generateFilename(), strContent);
final XtextResource resource = (XtextResource) helper.getResourceSet().createResource(helper.uri(file));
try (InputStream is = new StringInputStream(strContent)) {
resource.load(is, null);
}
return resource;
} catch(Exception e) {
throw new RuntimeException(e);
}
}
/** Replies the default prefix.
*
* <p>The prefix is appended when calling {@link #newBuilder()}.
*
* @return the default prefix.
*/
protected String getPrefix() {
return "";
}
/** Replies the default suffix.
*
* <p>The suffix is appended when calling {@link #newBuilder()}.
*
* @return the default suffix.
*/
protected String getSuffix() {
return "";
}
/** Create a builder for content assist tests.
*
* @return the builder.
* @throws Exception if the builder cannot be created.
*/
protected final ContentAssistProcessorTestBuilder newBuilder() throws Exception {
ContentAssistProcessorTestBuilder builder = new ContentAssistProcessorTestBuilder(getInjectedInjector(), this);
String prefix = getPrefix();
if (prefix.length() > 0) {
builder = builder.appendNl(prefix);
}
String suffix = getSuffix();
if (suffix.length() > 0) {
builder = builder.appendSuffix(suffix);
}
return builder;
}
/** Flattening the arrays.
*
* @param arrays the arrays to be flattened.
* @return the content of the arrays.
*/
protected static String[] expect(String[]... arrays) {
List<String> expectation = Lists.newArrayList();
for(String[] array: arrays) {
expectation.addAll(Arrays.asList(array));
}
return expectation.toArray(new String[expectation.size()]);
}
/** Assert that all the given texts are not in the proposal.
*
* @param builder the proposal builder.
* @param notExpectations the texts to not be proposed.
* @return the builder.
* @throws Exception
*/
protected static ContentAssistProcessorTestBuilder assertNoText(ContentAssistProcessorTestBuilder builder, String... notExpectations)
throws Exception {
ICompletionProposal[] computeCompletionProposals = builder.computeCompletionProposals();
if (computeCompletionProposals == null) {
computeCompletionProposals = new ICompletionProposal[0];
}
Arrays.sort(notExpectations);
List<String> sortedNotExpectations = Lists.newArrayList();
for (String expectation : notExpectations) {
sortedNotExpectations.add(LineDelimiters.toPlatform(expectation));
}
for (final ICompletionProposal completionProposal : computeCompletionProposals) {
String proposedText = getProposedText(completionProposal);
Assert.assertFalse("Unexpected proposal '" + proposedText + "'",
sortedNotExpectations.contains(proposedText));
}
return builder;
}
/** Assert that all the given texts are in the proposal.
*
* <p>In opposite to {@link ContentAssistProcessorTestBuilder#assertText(String...)}, this function
* is not failing if the more text than the given ones are proposed.
*
* @param builder the proposal builder.
* @param expectations the texts to be proposed.
* @return the builder.
* @throws Exception
*/
protected static ContentAssistProcessorTestBuilder assertTextInsideProposals(ContentAssistProcessorTestBuilder builder,
String... expectations)
throws Exception {
ICompletionProposal[] computeCompletionProposals = builder.computeCompletionProposals();
if (computeCompletionProposals == null) {
computeCompletionProposals = new ICompletionProposal[0];
}
Arrays.sort(expectations);
final List<String> sortedExpectations = Lists.newArrayList();
for (String expectation : expectations) {
sortedExpectations.add(LineDelimiters.toPlatform(expectation));
}
for (final ICompletionProposal completionProposal : computeCompletionProposals) {
String proposedText = getProposedText(completionProposal);
sortedExpectations.remove(proposedText);
}
if (!sortedExpectations.isEmpty()) {
String[] actual = new String[computeCompletionProposals.length];
int i = 0;
for (final ICompletionProposal completionProposal : computeCompletionProposals) {
String proposedText = getProposedText(completionProposal);
actual[i] = proposedText;
++i;
}
Arrays.sort(actual);
throw new ComparisonFailure("Expecting text: " + sortedExpectations.toString(),
toString(expectations), toString(actual));
}
return builder;
}
private static String toString(Object[] array) {
StringBuilder buf = new StringBuilder();
if (array != null) {
for (Object obj : array) {
if (obj != null) {
buf.append(obj);
}
buf.append(getLineSeparator());
}
}
return buf.toString();
}
private static String getProposedText(ICompletionProposal completionProposal) {
String proposedText = completionProposal.getDisplayString();
if (completionProposal instanceof ConfigurableCompletionProposal) {
ConfigurableCompletionProposal configurableProposal = (ConfigurableCompletionProposal) completionProposal;
proposedText = configurableProposal.getReplacementString();
if (configurableProposal.getTextApplier() instanceof ReplacementTextApplier) {
proposedText = ((ReplacementTextApplier) configurableProposal.getTextApplier())
.getActualReplacementString(configurableProposal);
}
}
return proposedText;
}
/**
* @author $Author: sgalland$
* @version $FullVersion$
* @mavengroupid $GroupId$
* @mavenartifactid $ArtifactId$
*/
public static class ProcessorTestBuilderFactory extends ContentAssistProcessorTestBuilder.Factory {
private final Injector injector;
@Inject
public ProcessorTestBuilderFactory(Injector injector) {
super(injector);
this.injector = injector;
}
public ContentAssistProcessorTestBuilder create(ResourceLoadHelper resourceLoadHelper) throws Exception {
final ProcessorTestBuilder builder = new ProcessorTestBuilder(this.injector, resourceLoadHelper);
this.injector.injectMembers(builder);
return builder;
}
}
/**
* @author $Author: sgalland$
* @version $FullVersion$
* @mavengroupid $GroupId$
* @mavenartifactid $ArtifactId$
*/
private static class ProcessorTestBuilder extends ContentAssistProcessorTestBuilder {
@Inject
private IWorkbench workbench;
public ProcessorTestBuilder(Injector injector, ResourceLoadHelper helper) throws Exception {
super(injector, helper);
}
protected Shell getShell() {
if (this.workbench != null) {
final IWorkbenchWindow window = this.workbench.getActiveWorkbenchWindow();
if (window != null) {
return window.getShell();
} else {
for (final IWorkbenchWindow window1 : this.workbench.getWorkbenchWindows()) {
Shell shell = window.getShell();
if (shell != null) {
return shell;
}
}
}
}
return null;
}
private Shell ensureShell() {
Shell shell;
if (isEclipseRuntimeEnvironment()) {
shell = getShell();
if (shell == null) {
shell = new Shell();
}
} else {
shell = new Shell();
}
return shell;
}
@Override
protected ICompletionProposal[] computeCompletionProposals(IXtextDocument xtextDocument, int cursorPosition)
throws BadLocationException {
if (isEclipseRuntimeEnvironment()) {
Shell shell = getShell();
if (shell != null) {
return computeCompletionProposals(xtextDocument, cursorPosition, shell);
}
}
final Display display = Display.getDefault();
final List<ICompletionProposal[]> resultBuffer = new ArrayList<>(1);
final List<BadLocationException> exceptionBuffer = new ArrayList<>(1);
display.syncExec(() -> {
final Shell asyncShell = new Shell(display);
try {
final ICompletionProposal[] result = computeCompletionProposals(xtextDocument, cursorPosition, asyncShell);
resultBuffer.add(result);
} catch (BadLocationException exception) {
exceptionBuffer.add(exception);
} finally {
asyncShell.dispose();
}
});
if (!exceptionBuffer.isEmpty()) {
throw exceptionBuffer.get(0);
}
return resultBuffer.get(0);
}
@Override
public ContentAssistProcessorTestBuilder applyProposal(int position, String proposalString) throws Exception {
throw new UnsupportedOperationException("must be overriden for Shell access");
}
@Override
public ContentAssistProcessorTestBuilder appendAndApplyProposal(String model, int position, String proposalString) throws Exception {
throw new UnsupportedOperationException("must be overriden for Shell access");
}
public ContentAssistProcessorTestBuilder assertMatchString(String matchString)
throws Exception {
throw new UnsupportedOperationException("must be overriden for Shell access");
}
}
}