summaryrefslogtreecommitdiff
path: root/java/java-tests/testSrc/com/intellij/codeInsight/completion/SmartTypeCompletionOrderingTest.groovy
blob: 1d258fe0136747f27ba034ee63ba38b393db0193 (plain)
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
/*
 * Copyright (c) 2000-2007 JetBrains s.r.o. All Rights Reserved.
 */

package com.intellij.codeInsight.completion;


import com.intellij.JavaTestUtil
import com.intellij.codeInsight.lookup.LookupElement
import com.intellij.codeInsight.lookup.LookupElementPresentation
import com.intellij.codeInsight.lookup.impl.LookupImpl
import com.intellij.psi.PsiClass
import com.intellij.psi.statistics.StatisticsManager
import com.intellij.openapi.fileEditor.FileDocumentManager

public class SmartTypeCompletionOrderingTest extends CompletionSortingTestCase {
  private static final String BASE_PATH = "/codeInsight/completion/smartTypeSorting"; 

  public SmartTypeCompletionOrderingTest() {
    super(CompletionType.SMART);
  }

  public void testJComponentAdd() throws Throwable {
    checkPreferredItems(0, "name", "b", "fooBean239", "foo", "this");
  }
  
  public void testJComponentAddNew() throws Throwable {
    //there's no PopupMenu in mock jdk
    checkPreferredItems(2, "Component", "String", "FooBean3", "JComponent", "Container");
  }

  public void testJComponentAddNewWithStats() throws Throwable {
    //there's no PopupMenu in mock jdk
    final LookupImpl lookup = invokeCompletion("/JComponentAddNew.java");
    assertPreferredItems(2, "Component", "String", "FooBean3", "JComponent", "Container");
    incUseCount(lookup, 4); //Container
    assertPreferredItems(2, "Component", "String", "Container", "FooBean3", "JComponent");
    imitateItemSelection(lookup, 3); //FooBean3
    for (int i = 0; i < StatisticsManager.OBLIVION_THRESHOLD; i++) {
      imitateItemSelection(lookup, 2); //Container
    }
    refreshSorting(lookup);
    assertPreferredItems(2, "Component", "String", "Container", "FooBean3", "JComponent");

    int component = lookup.items.findIndexOf { it.lookupString == 'Component' }
    for (int i = 0; i < StatisticsManager.OBLIVION_THRESHOLD; i++) {
      imitateItemSelection(lookup, component);
    }
    refreshSorting(lookup);
    assertPreferredItems(1, "String", "Component", "FooBean3");
  }

  public void testNewListAlwaysFirst() {
    def lookup = invokeCompletion(getTestName(false) + ".java")
    assertPreferredItems 1, 'List', 'ArrayList', 'AbstractList', 'AbstractSequentialList'
    for (int i = 0; i < StatisticsManager.OBLIVION_THRESHOLD + 10; i++) {
      imitateItemSelection(lookup, 3) //AbstractSequentialList
    }
    refreshSorting(lookup)
    assertPreferredItems 1, 'List', 'AbstractSequentialList', 'ArrayList', 'AbstractList'
  }
  
  public void testNoStatsOnUnsuccessfulAttempt() {
    final LookupImpl lookup = invokeCompletion("/JComponentAddNew.java");
    assertPreferredItems(2, "Component", "String", "FooBean3", "JComponent", "Container");
    lookup.currentItem = lookup.items[4] //Container
    myFixture.type('\n\b')
    CompletionLookupArranger.applyLastCompletionStatisticsUpdate()
    FileDocumentManager.instance.saveAllDocuments()
    invokeCompletion("/JComponentAddNew.java")
    assertPreferredItems(2, "Component", "String", "FooBean3", "JComponent", "Container");
  }

  public void testMethodStats() throws Throwable {
    final LookupImpl lookup = invokeCompletion(getTestName(false) + ".java");
    assertPreferredItems(0, "bar", "foo", "goo");
    incUseCount(lookup, 2);
    assertPreferredItems(0, "goo", "bar", "foo");
  }

  public void testNewRunnable() throws Throwable {
    checkPreferredItems(0, "Runnable", "MyAnotherRunnable", "MyRunnable", "Thread");
  }

  public void testNewComponent() throws Throwable {
    checkPreferredItems(1, "Component", "Foo", "JComponent", "Container");
  }
  
  public void testClassLiteral() throws Throwable {
    checkPreferredItems(0, "String.class");
  }

  public void testMethodsWithSubstitutableReturnType() throws Throwable {
    checkPreferredItems(0, "foo", "toString", "bar");
  }

  public void testDontPreferKeywords() throws Throwable {
    checkPreferredItems(0, "o1", "foo", "name", "this");
  }

  public void testEnumValueOf() throws Throwable {
    checkPreferredItems(0, "e", "MyEnum.BAR", "MyEnum.FOO", "valueOf", "valueOf");
  }

  public void testEnumValueOf2() throws Throwable {
    checkPreferredItems(0, "e", "MyEnum.BAR", "MyEnum.FOO", "bar", "valueOf");
  }

  public void testPreferMatchedWords() throws Throwable {
    checkPreferredItems(0, "getVersionString", "getTitle");
  }

  public void testPreferImportedClasses() throws Throwable {
    //there's no PopupMenu in mock jdk
    checkPreferredItems(2, "Component", "String", "FooBean3", "JPanel", "JComponent");
  }
  
  public void testPreferNestedClasses() throws Throwable {
    //there's no PopupMenu in mock jdk
    checkPreferredItems(2, "Component", "String", "FooBean3", "NestedClass", "JComponent");
  }

  public void testSmartCollections() throws Throwable {
    checkPreferredItems(0, "s");
  }

  public void testSmartEquals() throws Throwable {
    checkPreferredItems(0, "s");
  }

  public void testSmartEquals2() throws Throwable {
    checkPreferredItems(0, "foo", "this", "o", "s");
  }

  public void testSmartEquals3() throws Throwable {
    checkPreferredItems(0, "b", "this", "a", "z");
  }

  public void testSmartCollectionsNew() throws Throwable {
    checkPreferredItems(1, "Foo", "Bar");
  }

  public void testSmartEqualsNew() throws Throwable {
    checkPreferredItems(1, "Foo", "Bar");
  }
  
  public void testSmartEqualsNew2() throws Throwable {
    checkPreferredItems(0, "Foo");
  }

  public void testBooleanValueOf() throws Throwable {
    checkPreferredItems(0, "b", "Boolean.FALSE", "Boolean.TRUE", "equals", "false", "true", "valueOf", "valueOf");
  }
  
  public void testXmlTagGetAttribute() throws Throwable {
    checkPreferredItems(0, "getAttributeValue", "getNamespace", "toString");
  }

  public void testPreferFieldsToMethods() throws Throwable {
    checkPreferredItems(0, "myVersion", "getVersion", "getSelectedVersion", "calculateVersion");
  }

  public void testPreferParametersToGetters() throws Throwable {
    checkPreferredItems(0, "a", "I._1", "getLastI", "valueOf");
  }

  public void testExpectedInterfaceShouldGoFirst() throws Throwable {
    checkPreferredItems(0, "MyProcessor", "Proc1");
  }

  public void testStatisticsAffectsNonPreferableExpectedItems() throws Throwable {
    final LookupImpl lookup = invokeCompletion(getTestName(false) + ".java");
    assertPreferredItems(1, "List", "ArrayList", "AbstractList", "AbstractSequentialList");
    incUseCount(lookup, 0);
    assertPreferredItems(1, "List", "ArrayList", "AbstractList", "AbstractSequentialList");
    incUseCount(lookup, 0);
    assertPreferredItems(0, "List", "ArrayList", "AbstractList", "AbstractSequentialList");
  }

  public void testPreferNonRecursiveMethodParams() throws Throwable {
    checkPreferredItems(0, "b", "s", "a", "hashCode");
  }

  public void testPreferDelegatingMethodParams() throws Throwable {
    //there's no PopupMenu in mock jdk
    final LookupImpl lookup = invokeCompletion(getTestName(false) + ".java");
    assertPreferredItems(0, "xyz", "abc");
    incUseCount(lookup, 1);
    assertPreferredItems(0, "xyz", "abc");
  }

  public void testGwtButtons() throws Throwable {
    checkPreferredItems(0, "Button", "ButtonBase");
  }

  public void testNewArrayList() throws Throwable {
    checkPreferredItems(0, "ArrayList", "OtherList");
  }

  public void testPassingQualifierToMethodCall() throws Throwable {
    checkPreferredItems(0, "this", "param");
  }

  public void testPassingThisToUnqualifiedMethodCall() throws Throwable {
    checkPreferredItems(0, "param", "this");
  }

  public void testPreferAccessibleMembers() throws Throwable {
    checkPreferredItems(0, "Foo.C_NORMAL", "Foo.B_DEPRECATED");
  }

  public void testNoSkippingInSmartCast() throws Throwable {
    checkPreferredItems(0, "Foo", "Bar", "Goo");
  }

  public void testLiteralInReturn() throws Throwable {
    checkPreferredItems(0, "false", "true", "equals");
  }

  public void testLiteralInIf() throws Throwable {
    checkPreferredItems(0, "equals", "false", "true");
  }

  public void testFactoryMethodForDefaultType() throws Throwable {
    checkPreferredItems(0, "create", "this");
  }

  public void testLocalVarsBeforeClassLiterals() throws Throwable {
    checkPreferredItems(0, "local", "Foo.class", "Bar.class");
  }

  public void testDontPreferCasted() throws Throwable {
    checkPreferredItems(0, "b", "_o");
  }

  public void testInnerClassesProximity() throws Throwable {
    checkPreferredItems(0, "Goo", "InnerGoo", "Bar", "AGoo");
  }

  public void testLocalVariablesOutweighStats() throws Throwable {
    final LookupImpl lookup = invokeCompletion(getTestName(false) + ".java");
    assertPreferredItems(0, "foo", "param", "this", "bar", "goo");
    incUseCount(lookup, 4);
    assertPreferredItems(0, "foo", "param", "this", "goo", "bar");
    for (int i = 0; i < StatisticsManager.OBLIVION_THRESHOLD; i++) {
      imitateItemSelection(lookup, 3); //goo
    }
    refreshSorting(lookup);
    assertPreferredItems(0, "foo", "param", "this", "goo", "bar");
  }

  public void testPreferredByNameDontChangeStatistics() throws Throwable {
    invokeCompletion(getTestName(false) + ".java");
    assertPreferredItems(0, "foo", "false");
    myFixture.type(',');
    complete();
    assertPreferredItems(0, "bar", "foo", "equals", "false", "true");
  }

  public void testExpectedNameDependentStats() throws Throwable {
    final LookupImpl lookup = invokeCompletion(getTestName(false) + ".java");
    assertPreferredItems(0, "myFoo", "myBar");
    incUseCount(lookup, 1); //myBar
    assertPreferredItems(0, "myBar", "myFoo");
  }

  public void testPreferSameNamedMethods() {
    checkPreferredItems(0, "foo", "boo", "doo", "hashCode");
  }

  public void testErasureNotAffectingProximity() {
    myFixture.addClass("package foo; public interface Foo<T> {}");
    myFixture.addClass("package bar; public class Bar implements foo.Foo {}");
    myFixture.addClass("public class Bar<T> implements foo.Foo<T> {}");
    checkPreferredItems(0, "Bar", "Bar");

    LookupElementPresentation presentation = new LookupElementPresentation();
    List<LookupElement> items = getLookup().getItems();

    LookupElement first = items.get(0);
    assertEquals("Bar", ((PsiClass)first.getObject()).getQualifiedName());
    first.renderElement(presentation);
    assertEquals("Bar<String>", presentation.getItemText());

    LookupElement second = items.get(1);
    assertEquals("bar.Bar", ((PsiClass)second.getObject()).getQualifiedName());
    second.renderElement(presentation);
    assertEquals("Bar", presentation.getItemText());
  }

  public void testAssertEquals() throws Throwable {
    myFixture.addClass("package junit.framework; public class Assert { public static void assertEquals(Object a, Object b) {} }");
    checkPreferredItems(0, "boo", "bar")
  }

  public void testPreferCollectionsEmptyList() throws Throwable {
    myFixture.addClass("package foo; public class FList<T> implements java.util.List<T> { public static <T> FList<T> emptyList() {} }");
    configureNoCompletion(getTestName(false) + ".java");
    myFixture.complete(CompletionType.SMART, 2);
    assert lookup.items.findIndexOf { 'Collections.emptyList' in it.allLookupStrings } < lookup.items.findIndexOf { 'FList.emptyList' in it.allLookupStrings }
    assertPreferredItems(0, "local", "local.subList", "locMethod")
  }

  public void testDispreferGetterInSetterCall() {
    checkPreferredItems 0, 'color', 'getZooColor', 'getColor', 'hashCode'
  }
  public void testPreferOtherGetterInSetterCall() {
    checkPreferredItems 0, 'color', 'getColor', 'getZooColor', 'hashCode'
  }
  public void testPreferLocalOverFactoryMatchingName() {
    checkPreferredItems 0, 'e', 'createEvent'
  }

  public void testGetLogger() {
    checkPreferredItems 0, 'Foo.class', 'forName'
  }
  public void testGetWildcardLogger() {
    checkPreferredItems 0, 'Foo.class', 'forName'
  }
  public void testPreferLocalWildcardClassOverObject() {
    checkPreferredItems 0, 'type', 'Object.class'
  }

  public void testPreferStringsInStringConcatenation() {
    checkPreferredItems 0, 'toString'
  }

  public void testGlobalStaticMemberStats() {
    configureNoCompletion(getTestName(false) + ".java")
    myFixture.complete(CompletionType.SMART, 2)
    assertPreferredItems 0, 'newLinkedSet0', 'newLinkedSet1', 'newLinkedSet2'
    incUseCount lookup, 1
    assertPreferredItems 0, 'newLinkedSet1', 'newLinkedSet0', 'newLinkedSet2'
  }

  public void testPreferExpectedTypeMembers() {
    configureNoCompletion(getTestName(false) + ".java")
    myFixture.complete(CompletionType.SMART, 2)
    assertPreferredItems 0, 'MyColor.RED', 'Another.RED'
    assert lookup.items.size() == 2
  }

  @Override
  protected String getBasePath() {
    return JavaTestUtil.getRelativeJavaTestDataPath() + BASE_PATH;
  }
}