PageRenderTime 54ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/java/java-tests/testSrc/com/intellij/java/codeInsight/completion/SmartTypeCompletionOrderingTest.groovy

http://github.com/JetBrains/intellij-community
Groovy | 384 lines | 291 code | 73 blank | 20 comment | 6 complexity | fb1e8845e07cc966c2ff9a5d2ad7005a MD5 | raw file
Possible License(s): BSD-3-Clause, Apache-2.0, MPL-2.0-no-copyleft-exception, MIT, EPL-1.0, AGPL-1.0
  1. /*
  2. * Copyright 2000-2017 JetBrains s.r.o.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.intellij.java.codeInsight.completion
  17. import com.intellij.JavaTestUtil
  18. import com.intellij.codeInsight.completion.CompletionType
  19. import com.intellij.codeInsight.completion.StatisticsUpdate
  20. import com.intellij.codeInsight.lookup.LookupElement
  21. import com.intellij.codeInsight.lookup.LookupElementPresentation
  22. import com.intellij.codeInsight.lookup.impl.LookupImpl
  23. import com.intellij.openapi.fileEditor.FileDocumentManager
  24. import com.intellij.psi.PsiClass
  25. import com.intellij.psi.statistics.StatisticsManager
  26. class SmartTypeCompletionOrderingTest extends CompletionSortingTestCase {
  27. private static final String BASE_PATH = "/codeInsight/completion/smartTypeSorting"
  28. SmartTypeCompletionOrderingTest() {
  29. super(CompletionType.SMART)
  30. }
  31. void testJComponentAdd() throws Throwable {
  32. checkPreferredItems(0, "name", "b", "fooBean239", "foo")
  33. }
  34. void testJComponentAddNew() throws Throwable {
  35. //there's no PopupMenu in mock jdk
  36. checkPreferredItems(2, "Component", "String", "FooBean3", "JComponent", "Container")
  37. }
  38. void testJComponentAddNewWithStats() throws Throwable {
  39. //there's no PopupMenu in mock jdk
  40. final LookupImpl lookup = invokeCompletion("/JComponentAddNew.java")
  41. assertPreferredItems(2, "Component", "String", "FooBean3", "JComponent", "Container")
  42. incUseCount(lookup, 4) //Container
  43. assertPreferredItems(2, "Component", "String", "Container", "FooBean3", "JComponent")
  44. imitateItemSelection(lookup, 3) //FooBean3
  45. for (int i = 0; i < StatisticsManager.OBLIVION_THRESHOLD; i++) {
  46. imitateItemSelection(lookup, 2) //Container
  47. }
  48. refreshSorting(lookup)
  49. assertPreferredItems(2, "Component", "String", "Container", "FooBean3", "JComponent")
  50. }
  51. void testNewListAlwaysFirst() {
  52. def lookup = invokeCompletion(getTestName(false) + ".java")
  53. assertPreferredItems 1, 'List', 'ArrayList', 'AbstractList', 'AbstractSequentialList'
  54. for (int i = 0; i < StatisticsManager.OBLIVION_THRESHOLD + 10; i++) {
  55. imitateItemSelection(lookup, 3) //AbstractSequentialList
  56. }
  57. refreshSorting(lookup)
  58. assertPreferredItems 1, 'List', 'AbstractSequentialList', 'ArrayList', 'AbstractList'
  59. }
  60. void testNoStatsOnUnsuccessfulAttempt() {
  61. final LookupImpl lookup = invokeCompletion("/JComponentAddNew.java")
  62. assertPreferredItems(2, "Component", "String", "FooBean3", "JComponent", "Container")
  63. lookup.currentItem = lookup.items[4] //Container
  64. myFixture.type('\n\b')
  65. StatisticsUpdate.applyLastCompletionStatisticsUpdate()
  66. FileDocumentManager.instance.saveAllDocuments()
  67. invokeCompletion("/JComponentAddNew.java")
  68. assertPreferredItems(2, "Component", "String", "FooBean3", "JComponent", "Container")
  69. }
  70. void testMethodStats() throws Throwable {
  71. final LookupImpl lookup = invokeCompletion(getTestName(false) + ".java")
  72. assertPreferredItems(0, "bar", "foo", "goo")
  73. incUseCount(lookup, 2)
  74. assertPreferredItems(0, "goo", "bar", "foo")
  75. }
  76. void testNewRunnable() throws Throwable {
  77. checkPreferredItems(0, "Runnable", "MyAnotherRunnable", "MyRunnable", "Thread")
  78. }
  79. void testNewComponent() throws Throwable {
  80. checkPreferredItems(1, "Component", "Foo", "JComponent", "Container")
  81. }
  82. void testClassLiteral() throws Throwable {
  83. checkPreferredItems(0, "String.class")
  84. }
  85. void testMethodsWithSubstitutableReturnType() throws Throwable {
  86. checkPreferredItems(0, "foo", "toString", "bar")
  87. }
  88. void testDontPreferKeywords() throws Throwable {
  89. checkPreferredItems(0, "o1", "foo", "name", "this")
  90. }
  91. void testEnumValueOf() throws Throwable {
  92. checkPreferredItems(0, "e", "MyEnum.BAR", "MyEnum.FOO", "valueOf", "valueOf")
  93. }
  94. void testEnumValueOf2() throws Throwable {
  95. checkPreferredItems(0, "e", "MyEnum.BAR", "MyEnum.FOO", "bar", "valueOf")
  96. }
  97. void testPreferMatchedWords() throws Throwable {
  98. checkPreferredItems(0, "getVersionString", "getTitle")
  99. }
  100. void testPreferImportedClasses() throws Throwable {
  101. //there's no PopupMenu in mock jdk
  102. checkPreferredItems(2, "Component", "String", "FooBean3", "JPanel", "JComponent")
  103. }
  104. void testPreferNestedClasses() throws Throwable {
  105. //there's no PopupMenu in mock jdk
  106. checkPreferredItems(2, "Component", "String", "FooBean3", "NestedClass", "JComponent")
  107. }
  108. void testSmartCollections() throws Throwable {
  109. checkPreferredItems(0, "s")
  110. }
  111. void testSmartEquals() throws Throwable {
  112. checkPreferredItems(0, "s")
  113. }
  114. void testSmartEquals2() throws Throwable {
  115. checkPreferredItems(0, "foo", "this", "o", "s")
  116. }
  117. void testSmartEquals3() throws Throwable {
  118. checkPreferredItems(0, "b", "this", "a", "z")
  119. }
  120. void testSmartCollectionsNew() throws Throwable {
  121. checkPreferredItems(1, "Foo", "Bar")
  122. }
  123. void testSmartEqualsNew() throws Throwable {
  124. checkPreferredItems(1, "Foo", "Bar")
  125. }
  126. void testSmartEqualsNew2() throws Throwable {
  127. checkPreferredItems(0, "Foo")
  128. }
  129. void testBooleanValueOf() throws Throwable {
  130. checkPreferredItems(0, "b", "Boolean.FALSE", "Boolean.TRUE", "equals", "false", "true", "valueOf", "valueOf")
  131. }
  132. void testXmlTagGetAttribute() throws Throwable {
  133. checkPreferredItems(0, "getAttributeValue", "getNamespace", "toString")
  134. }
  135. void testPreferFieldsToMethods() throws Throwable {
  136. checkPreferredItems(0, "myVersion", "getVersion", "getSelectedVersion", "calculateVersion")
  137. }
  138. void testPreferFieldsToConstants() {
  139. checkPreferredItems(0, "dateField", "LocalDate.MAX", "LocalDate.MIN")
  140. }
  141. void testPreferParametersToGetters() throws Throwable {
  142. checkPreferredItems(0, "a", "I._1", "getLastI", "valueOf")
  143. }
  144. void testExpectedInterfaceShouldGoFirst() throws Throwable {
  145. checkPreferredItems(0, "MyProcessor", "Proc1")
  146. }
  147. void testStatisticsAffectsNonPreferableExpectedItems() throws Throwable {
  148. final LookupImpl lookup = invokeCompletion(getTestName(false) + ".java")
  149. assertPreferredItems(1, "List", "ArrayList", "AbstractList", "AbstractSequentialList")
  150. incUseCount(lookup, 0)
  151. assertPreferredItems(1, "List", "ArrayList", "AbstractList", "AbstractSequentialList")
  152. incUseCount(lookup, 0)
  153. assertPreferredItems(0, "List", "ArrayList", "AbstractList", "AbstractSequentialList")
  154. }
  155. void testPreferNonRecursiveMethodParams() throws Throwable {
  156. checkPreferredItems(0, "b", "s", "a", "hashCode")
  157. }
  158. void testPreferDelegatingMethodParams() throws Throwable {
  159. //there's no PopupMenu in mock jdk
  160. final LookupImpl lookup = invokeCompletion(getTestName(false) + ".java")
  161. assertPreferredItems(0, "xyz", "abc")
  162. incUseCount(lookup, 1)
  163. assertPreferredItems(0, "xyz", "abc")
  164. }
  165. void testGwtButtons() throws Throwable {
  166. checkPreferredItems(0, "Button", "ButtonBase")
  167. }
  168. void testNewArrayList() throws Throwable {
  169. checkPreferredItems(0, "ArrayList", "OtherList")
  170. }
  171. void testPassingQualifierToMethodCall() throws Throwable {
  172. checkPreferredItems(0, "this", "param")
  173. }
  174. void testPassingThisToUnqualifiedMethodCall() throws Throwable {
  175. checkPreferredItems(0, "param", "this")
  176. }
  177. void testPreferAccessibleMembers() throws Throwable {
  178. checkPreferredItems(0, "Foo.C_NORMAL", "Foo.B_DEPRECATED")
  179. }
  180. void testNoSkippingInSmartCast() throws Throwable {
  181. checkPreferredItems(0, "Foo", "Bar", "Goo")
  182. }
  183. void testLiteralInReturn() throws Throwable {
  184. checkPreferredItems(0, "false", "true", "equals")
  185. }
  186. void testLiteralInIf() throws Throwable {
  187. checkPreferredItems(0, "equals", "false", "true")
  188. }
  189. void testFactoryMethodForDefaultType() throws Throwable {
  190. checkPreferredItems(0, "create", "this")
  191. }
  192. void testLocalVarsBeforeClassLiterals() throws Throwable {
  193. checkPreferredItems(0, "local", "Foo.class", "Bar.class")
  194. }
  195. void testPreferInstanceofed() throws Throwable {
  196. checkPreferredItems(0, "_o", "b")
  197. }
  198. void testInnerClassesProximity() throws Throwable {
  199. checkPreferredItems(0, "Goo", "InnerGoo", "Bar", "AGoo")
  200. }
  201. void testLocalVariablesOutweighStats() throws Throwable {
  202. final LookupImpl lookup = invokeCompletion(getTestName(false) + ".java")
  203. assertPreferredItems(0, "foo", "param", "this", "bar", "goo")
  204. incUseCount(lookup, 4)
  205. assertPreferredItems(0, "foo", "param", "this", "goo", "bar")
  206. for (int i = 0; i < StatisticsManager.OBLIVION_THRESHOLD; i++) {
  207. imitateItemSelection(lookup, 3) //goo
  208. }
  209. refreshSorting(lookup)
  210. assertPreferredItems(0, "foo", "param", "this", "goo", "bar")
  211. }
  212. void testPreferredByNameDontChangeStatistics() throws Throwable {
  213. invokeCompletion(getTestName(false) + ".java")
  214. assertPreferredItems(0, "foo", "false")
  215. myFixture.type(',')
  216. myFixture.complete(CompletionType.SMART)
  217. assertPreferredItems(0, "bar", "foo", "equals", "false", "true")
  218. }
  219. void testExpectedNameDependentStats() throws Throwable {
  220. final LookupImpl lookup = invokeCompletion(getTestName(false) + ".java")
  221. assertPreferredItems(0, "myFoo", "myBar")
  222. incUseCount(lookup, 1) //myBar
  223. assertPreferredItems(0, "myBar", "myFoo")
  224. }
  225. void testPreferSameNamedMethods() {
  226. checkPreferredItems(0, "foo", "boo", "doo", "hashCode")
  227. }
  228. void testErasureNotAffectingProximity() {
  229. myFixture.addClass("package foo; public interface Foo<T> {}")
  230. myFixture.addClass("package bar; public class Bar implements foo.Foo {}")
  231. myFixture.addClass("public class Bar<T> implements foo.Foo<T> {}")
  232. checkPreferredItems(0, "Bar", "Bar")
  233. LookupElementPresentation presentation = new LookupElementPresentation()
  234. List<LookupElement> items = getLookup().getItems()
  235. LookupElement first = items.get(0)
  236. assertEquals("Bar", ((PsiClass)first.getObject()).getQualifiedName())
  237. first.renderElement(presentation)
  238. assertEquals("Bar<String>", presentation.getItemText())
  239. LookupElement second = items.get(1)
  240. assertEquals("bar.Bar", ((PsiClass)second.getObject()).getQualifiedName())
  241. second.renderElement(presentation)
  242. assertEquals("Bar", presentation.getItemText())
  243. }
  244. void testAssertEquals() throws Throwable {
  245. myFixture.addClass("package junit.framework; public class Assert { public static void assertEquals(Object a, Object b) {} }")
  246. checkPreferredItems(0, "boo", "bar")
  247. }
  248. void testPreferCollectionsEmptyList() throws Throwable {
  249. myFixture.addClass("package foo; public class FList<T> implements java.util.List<T> { public static <T> FList<T> emptyList() {} }")
  250. configureNoCompletion(getTestName(false) + ".java")
  251. myFixture.complete(CompletionType.SMART, 2)
  252. assert lookup.items.findIndexOf { 'Collections.emptyList' in it.allLookupStrings } < lookup.items.findIndexOf { 'FList.emptyList' in it.allLookupStrings }
  253. assertPreferredItems(0, "local", "local.subList", "locMethod")
  254. }
  255. void testDispreferGetterInSetterCall() {
  256. checkPreferredItems 0, 'color', 'getZooColor', 'getColor', 'hashCode'
  257. }
  258. void testPreferOtherGetterInSetterCall() {
  259. checkPreferredItems 0, 'color', 'getColor', 'getZooColor', 'hashCode'
  260. }
  261. void testPreferLocalOverFactoryMatchingName() {
  262. checkPreferredItems 0, 'e', 'createEvent'
  263. }
  264. void testPreferLocalOverThis() {
  265. checkPreferredItems 0, 'value', 'this', 'hashCode'
  266. }
  267. void testGetLogger() {
  268. checkPreferredItems 0, 'Foo.class', 'forName'
  269. }
  270. void testGetWildcardLogger() {
  271. checkPreferredItems 0, 'Foo.class', 'forName'
  272. }
  273. void testGetWildcardFactoryLogger() {
  274. checkPreferredItems 0, 'Foo.class', 'forName'
  275. }
  276. void testPreferLocalWildcardClassOverObject() {
  277. checkPreferredItems 0, 'type', 'Object.class', 'forName', 'forName'
  278. }
  279. void testPreferStringsInStringConcatenation() {
  280. checkPreferredItems 0, 'toString'
  281. }
  282. void testGlobalStaticMemberStats() {
  283. configureNoCompletion(getTestName(false) + ".java")
  284. myFixture.complete(CompletionType.SMART, 2)
  285. assertPreferredItems 0, 'newLinkedSet0', 'newLinkedSet1', 'newLinkedSet2'
  286. incUseCount lookup, 1
  287. assertPreferredItems 0, 'newLinkedSet1', 'newLinkedSet0', 'newLinkedSet2'
  288. }
  289. void testPreferExpectedTypeMembers() {
  290. configureNoCompletion(getTestName(false) + ".java")
  291. myFixture.complete(CompletionType.SMART, 2)
  292. assertPreferredItems 0, 'MyColor.RED', 'Another.RED'
  293. assert lookup.items.size() == 2
  294. }
  295. void testPreferGlobalMembersReturningExpectedType() {
  296. configureNoCompletion(getTestName(false) + ".java")
  297. def items = myFixture.complete(CompletionType.SMART, 2)
  298. assert LookupElementPresentation.renderElement(items[0]).itemText == 'Map.builder'
  299. assert LookupElementPresentation.renderElement(items[1]).itemText == 'BiMap.builder'
  300. }
  301. void testPreferExpectedLocalOverExactlyDefaultMember() {
  302. checkPreferredItems 0, 'method', 'PsiUtil.NULL_PSI_ELEMENT'
  303. }
  304. void testPreferStaticsOfExpectedTypeToNonStaticGetterOfGenericOne() {
  305. checkPreferredItems 0, 'getE', 'myE', 'getGeneric'
  306. }
  307. @Override
  308. protected String getBasePath() {
  309. return JavaTestUtil.getRelativeJavaTestDataPath() + BASE_PATH
  310. }
  311. }