[duplicates] enable duplicates analysis in PyCharm/WebStorm/PhpStorm/RubyMine
[idea/community.git] / java / java-tests / testSrc / com / intellij / java / codeInsight / template / postfix / completion / TemplatesCompletionTest.java
1 // Copyright 2000-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
2 package com.intellij.java.codeInsight.template.postfix.completion;
3
4 import com.intellij.JavaTestUtil;
5 import com.intellij.codeInsight.completion.CompletionAutoPopupTestCase;
6 import com.intellij.codeInsight.completion.CompletionType;
7 import com.intellij.codeInsight.lookup.LookupElement;
8 import com.intellij.codeInsight.lookup.impl.LookupImpl;
9 import com.intellij.codeInsight.template.impl.LiveTemplateCompletionContributor;
10 import com.intellij.codeInsight.template.postfix.completion.PostfixTemplateLookupElement;
11 import com.intellij.codeInsight.template.postfix.settings.PostfixTemplatesSettings;
12 import com.intellij.codeInsight.template.postfix.templates.*;
13 import com.intellij.openapi.util.registry.Registry;
14 import com.intellij.testFramework.EdtTestUtil;
15 import com.intellij.util.containers.ContainerUtil;
16 import org.jetbrains.annotations.NotNull;
17 import org.jetbrains.annotations.Nullable;
18
19 import java.util.HashMap;
20
21 public class TemplatesCompletionTest extends CompletionAutoPopupTestCase {
22   @Override
23   public void setUp() throws Exception {
24     super.setUp();
25     LiveTemplateCompletionContributor.setShowTemplatesInTests(false, myFixture.getTestRootDisposable());
26   }
27
28   @Override
29   public void tearDown() throws Exception {
30     try {
31       PostfixTemplatesSettings settings = PostfixTemplatesSettings.getInstance();
32       assertNotNull(settings);
33       settings.setProviderToDisabledTemplates(new HashMap<>());
34       settings.setPostfixTemplatesEnabled(true);
35       settings.setTemplatesCompletionEnabled(true);
36     }
37     catch (Throwable e) {
38       addSuppressedException(e);
39     }
40     finally {
41       super.tearDown();
42     }
43   }
44
45   public void testSimpleCompletionList() {
46     LiveTemplateCompletionContributor.setShowTemplatesInTests(true, myFixture.getTestRootDisposable());
47     doAutoPopupTest("ins", InstanceofExpressionPostfixTemplate.class);
48   }
49
50   public void testAutopopupWithEnabledLiveTemplatesInCompletion() {
51     LiveTemplateCompletionContributor.setShowTemplatesInTests(false, myFixture.getTestRootDisposable());
52
53     configureByFile();
54     type("instanceof");
55     LookupImpl lookup = getLookup();
56     assertNotNull(lookup);
57     assertEquals(1, lookup.getItems().size());
58     LookupElement item = lookup.getCurrentItem();
59     assertNotNull(item);
60     assertInstanceOf(item, PostfixTemplateLookupElement.class);
61     assertInstanceOf(((PostfixTemplateLookupElement)item).getPostfixTemplate(), InstanceofExpressionPostfixTemplate.class);
62   }
63
64   public void testDoNotShowTemplateInInappropriateContext() {
65     doAutoPopupTest("instanceof", null);
66   }
67
68   // IDEA-119910 Middle matching doesn't work if pattern starts with a digit
69   public void testRestartCompletionForExactMatchOnly() {
70     doCompleteTest(".2", '\n');
71   }
72
73   public void testRestartCompletionForExactMatch() {
74     configureByFile();
75     type("not");
76     LookupElement currentItem = getLookup().getCurrentItem();
77     assertNotNull(currentItem);
78     assertInstanceOf(currentItem, PostfixTemplateLookupElement.class);
79     assertEquals(".not", currentItem.getLookupString());
80
81     type("null");
82     currentItem = getLookup().getCurrentItem();
83     assertNotNull(currentItem);
84     assertInstanceOf(currentItem, PostfixTemplateLookupElement.class);
85     assertEquals(".notnull", currentItem.getLookupString());
86   }
87
88   public void testShowTemplateInAutoPopup() {
89     doAutoPopupTest("instanceof", InstanceofExpressionPostfixTemplate.class);
90   }
91
92   public void testShowAutoPopupForAliases() {
93     doAutoPopupTest("nn", NotNullCheckPostfixTemplate.class);
94   }
95
96   public void testShowAutoPopupForFloatLiterals() {
97     doAutoPopupTest("fori", ForAscendingPostfixTemplate.class);
98   }
99
100   public void testDoNotShowTemplateIfPluginIsDisabled() {
101     PostfixTemplatesSettings settings = PostfixTemplatesSettings.getInstance();
102     assertNotNull(settings);
103     settings.setPostfixTemplatesEnabled(false);
104     doAutoPopupTest("instanceof", null);
105   }
106
107   public void testDoNotShowTemplateIfTemplateCompletionIsDisabled() {
108     PostfixTemplatesSettings settings = PostfixTemplatesSettings.getInstance();
109     assertNotNull(settings);
110     settings.setTemplatesCompletionEnabled(false);
111     doAutoPopupTest("instanceof", null);
112   }
113
114   public void testDoNotShowTemplateInMultiCaretMode() {
115     doAutoPopupTest("instanceof", null);
116   }
117
118   public void testDoNotAutoCompleteCompletionElementIfTemplateUnique() {
119     LiveTemplateCompletionContributor.setShowTemplatesInTests(true, myFixture.getTestRootDisposable());
120     configureByFile();
121     myFixture.completeBasic();
122     checkResultByFile();
123   }
124
125   public void testDoNotCompleteTemplateInMultiCaretMode() {
126     LiveTemplateCompletionContributor.setShowTemplatesInTests(true, myFixture.getTestRootDisposable());
127     configureByFile();
128     assertEmpty(myFixture.complete(CompletionType.BASIC));
129     checkResultByFile();
130   }
131
132   public void testShowTemplateOnDoubleLiteral() {
133     doAutoPopupTest("switch", SwitchStatementPostfixTemplate.class);
134   }
135
136   public void testSelectTemplateByTab() {
137     doCompleteTest("par", '\t');
138   }
139
140   public void testSelectTemplateByEnter() {
141     doCompleteTest("par", '\n');
142   }
143
144   public void testQuickTypingWithTab() {
145     configureByFile();
146     myFixture.type("par\t");
147     checkResultByFile();
148   }
149
150   public void testDoNotShowDisabledTemplate() {
151     PostfixTemplatesSettings settings = PostfixTemplatesSettings.getInstance();
152     assertNotNull(settings);
153     settings.disableTemplate(new InstanceofExpressionPostfixTemplate(), new JavaPostfixTemplateProvider().getId());
154     doAutoPopupTest("instanceof", null);
155   }
156
157   public void testDoNotShowTemplateOnCompletion() {
158     configureByFile();
159     myFixture.completeBasic();
160     LookupElement[] elements = myFixture.getLookupElements();
161     assertNotNull(elements);
162     assertNull(ContainerUtil.findInstance(elements, PostfixTemplateLookupElement.class));
163   }
164
165   public void testRecalculatePrefix() {
166     configureByFile();
167     type("par");
168     int selectedIndex = 0;
169     myFixture.assertPreferredCompletionItems(selectedIndex, ".par", "parents");
170
171     type("\b");
172     assertNotNull(getLookup());
173     myFixture.assertPreferredCompletionItems(selectedIndex, "parents");
174
175     type("r");
176     myFixture.assertPreferredCompletionItems(selectedIndex, ".par", "parents");
177   }
178
179   public void testTabCompletionWithTemplatesInAutopopup() {
180     LiveTemplateCompletionContributor.setShowTemplatesInTests(true, myFixture.getTestRootDisposable());
181
182     configureByFile();
183     type(".");
184     myFixture.assertPreferredCompletionItems(0, "parents");
185
186     type("\t");
187     assertNull(getLookup());
188     checkResultByFile();
189   }
190
191   public void testShouldNotExpandInMultiCaretMode() {
192     configureByFile();
193     type(".if\t");
194     checkResultByFile();
195   }
196
197   @Override
198   protected String getBasePath() {
199     return JavaTestUtil.getRelativeJavaTestDataPath() + "/codeInsight/template/postfix/completion";
200   }
201
202   private void doCompleteTest(String textToType, char c) {
203     configureByFile();
204     type(textToType);
205     assertNotNull(getLookup());
206     myFixture.type(c);
207     checkResultByFile();
208   }
209
210   private void doAutoPopupTest(@NotNull String textToType, @Nullable Class<? extends PostfixTemplate> expectedClass) {
211     configureByFile();
212     type(textToType);
213     LookupImpl lookup = getLookup();
214     if (expectedClass != null) {
215       assertNotNull(lookup);
216       LookupElement item = lookup.getCurrentItem();
217       assertNotNull(item);
218       assertInstanceOf(item, PostfixTemplateLookupElement.class);
219       assertInstanceOf(((PostfixTemplateLookupElement)item).getPostfixTemplate(), expectedClass);
220     }
221     else {
222       assertNull(lookup);
223     }
224   }
225
226   public void testOptionallyShowingSuitableLiveTemplatesBeforeOtherCompletionSuggestions() {
227     LiveTemplateCompletionContributor.setShowTemplatesInTests(true, myFixture.getTestRootDisposable());
228     Registry.get("ide.completion.show.live.templates.on.top").setValue(true, myFixture.getTestRootDisposable());
229
230     myFixture.configureByText("a.java", "class Foo { ps<caret> } class psvClass {}");
231     type("v");
232     myFixture.assertPreferredCompletionItems(0, "psvm", "psvClass");
233   }
234
235
236   private void configureByFile() {
237     EdtTestUtil.runInEdtAndWait(() -> myFixture.configureByFile(getTestName(true) + ".java"));
238   }
239
240   private void checkResultByFile() {
241     myFixture.checkResultByFile(getTestName(true) + "_after.java");
242   }
243 }