From 4018f78d4f0a0e25a18f2ff1c817bc16fde7842d Mon Sep 17 00:00:00 2001 From: peter Date: Thu, 21 Mar 2013 23:54:08 +0100 Subject: [PATCH] automatically clear templateTesting flag --- .../completion/JavaAutoPopupTest.groovy | 57 +++++++--------- .../quickFix/CreateMethodFromUsageTest.groovy | 10 +-- .../DelegateWithDefaultParamValueTest.java | 17 ++--- .../surroundWith/JavaSurroundWithTest.java | 65 +++++-------------- .../template/LiveTemplateTest.groovy | 3 +- .../InplaceRenameInvariantTest.groovy | 13 ++-- .../intellij/refactoring/RenameLocalTest.java | 35 ++++------ .../template/impl/TemplateManagerImpl.java | 13 ++++ .../AbstractInplaceIntroduceTest.java | 10 +-- .../completion/XmlCompletionTest.java | 40 +++++------- .../template/XmlLiveTemplateTest.groovy | 6 +- 11 files changed, 99 insertions(+), 170 deletions(-) diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/completion/JavaAutoPopupTest.groovy b/java/java-tests/testSrc/com/intellij/codeInsight/completion/JavaAutoPopupTest.groovy index a49cdf6baa0a..76fe837c5a1b 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/completion/JavaAutoPopupTest.groovy +++ b/java/java-tests/testSrc/com/intellij/codeInsight/completion/JavaAutoPopupTest.groovy @@ -871,32 +871,26 @@ class Foo { } public void testCompletionWhenLiveTemplateAreNotSufficient() { - ((TemplateManagerImpl)TemplateManager.getInstance(getProject())).setTemplateTesting(true); - try { - myFixture.configureByText("a.java", """ - class Foo { - { - Iterable l1 = null; - Iterable l2 = null; - Object asdf = null; - iter - } - } - """) - type '\t' - assert myFixture.lookupElementStrings == ['l2', 'l1'] - type 'as' - assert lookup - assertContains 'asdf', 'assert' - type '\n.' - assert lookup - assert 'hashCode' in myFixture.lookupElementStrings - assert myFixture.file.text.contains('asdf.') + TemplateManagerImpl.setTemplateTesting(getProject(), getTestRootDisposable()); + myFixture.configureByText("a.java", """ +class Foo { + { + Iterable l1 = null; + Iterable l2 = null; + Object asdf = null; + iter } - finally { - ((TemplateManagerImpl)TemplateManager.getInstance(getProject())).setTemplateTesting(false); - } - +} +""") + type '\t' + assert myFixture.lookupElementStrings == ['l2', 'l1'] + type 'as' + assert lookup + assertContains 'asdf', 'assert' + type '\n.' + assert lookup + assert 'hashCode' in myFixture.lookupElementStrings + assert myFixture.file.text.contains('asdf.') } public void testNoWordCompletionAutoPopup() { @@ -1177,16 +1171,11 @@ class Foo extends Abcdefg ''' } public void testSoutvTemplate() { - ((TemplateManagerImpl)TemplateManager.getInstance(getProject())).setTemplateTesting(true); - try { - myFixture.configureByText 'a.java', 'class Foo {{ }}' - type 'soutv\tgetcl.' - myFixture.checkResult '''class Foo {{ + TemplateManagerImpl.setTemplateTesting(getProject(), getTestRootDisposable()); + myFixture.configureByText 'a.java', 'class Foo {{ }}' + type 'soutv\tgetcl.' + myFixture.checkResult '''class Foo {{ System.out.println("getClass(). = " + getClass().); }}''' - } - finally { - ((TemplateManagerImpl)TemplateManager.getInstance(getProject())).setTemplateTesting(false); - } } public void testReturnLParen() { diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/CreateMethodFromUsageTest.groovy b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/CreateMethodFromUsageTest.groovy index 94847677c57e..1e794b58a631 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/CreateMethodFromUsageTest.groovy +++ b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/CreateMethodFromUsageTest.groovy @@ -15,13 +15,11 @@ */ package com.intellij.codeInsight.daemon.quickFix import com.intellij.codeInsight.lookup.LookupManager -import com.intellij.codeInsight.template.TemplateManager import com.intellij.codeInsight.template.impl.TemplateManagerImpl import com.intellij.ide.DataManager import com.intellij.openapi.actionSystem.DataContext import com.intellij.openapi.actionSystem.IdeActions import com.intellij.openapi.editor.actionSystem.EditorActionManager - /** * @author ven */ @@ -41,7 +39,7 @@ class SomeOuterClassWithLongName { } } """ - ((TemplateManagerImpl)TemplateManager.getInstance(getProject())).setTemplateTesting(true); + TemplateManagerImpl.setTemplateTesting(project, testRootDisposable); doAction("Create Method 'addSubGroup'") def state = TemplateManagerImpl.getTemplateState(getEditor()) //skip void return type @@ -78,12 +76,6 @@ class SomeOuterClassWithLongName { } - @Override - protected void tearDown() throws Exception { - ((TemplateManagerImpl)TemplateManager.getInstance(getProject())).setTemplateTesting(false); - super.tearDown() - } - @Override protected String getBasePath() { return "/codeInsight/daemonCodeAnalyzer/quickFix/createMethodFromUsage"; diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/DelegateWithDefaultParamValueTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/DelegateWithDefaultParamValueTest.java index e15349aaeefe..fa38ee78500f 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/DelegateWithDefaultParamValueTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInsight/daemon/quickFix/DelegateWithDefaultParamValueTest.java @@ -15,7 +15,6 @@ */ package com.intellij.codeInsight.daemon.quickFix; -import com.intellij.codeInsight.template.TemplateManager; import com.intellij.codeInsight.template.impl.TemplateManagerImpl; import com.intellij.codeInsight.template.impl.TemplateState; @@ -26,17 +25,13 @@ public class DelegateWithDefaultParamValueTest extends LightQuickFixTestCase { @Override protected void doAction(String text, boolean actionShouldBeAvailable, String testFullPath, String testName) throws Exception { - try { - ((TemplateManagerImpl)TemplateManager.getInstance(getProject())).setTemplateTesting(true); - super.doAction(text, actionShouldBeAvailable, testFullPath, testName); + TemplateManagerImpl.setTemplateTesting(getProject(), getTestRootDisposable()); + super.doAction(text, actionShouldBeAvailable, testFullPath, testName); - if (actionShouldBeAvailable) { - TemplateState state = TemplateManagerImpl.getTemplateState(getEditor()); - assert state != null; - state.gotoEnd(false); - } - } finally { - ((TemplateManagerImpl)TemplateManager.getInstance(getProject())).setTemplateTesting(false); + if (actionShouldBeAvailable) { + TemplateState state = TemplateManagerImpl.getTemplateState(getEditor()); + assert state != null; + state.gotoEnd(false); } } diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/generation/surroundWith/JavaSurroundWithTest.java b/java/java-tests/testSrc/com/intellij/codeInsight/generation/surroundWith/JavaSurroundWithTest.java index c12ffac70e3b..a7aa17432d61 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/generation/surroundWith/JavaSurroundWithTest.java +++ b/java/java-tests/testSrc/com/intellij/codeInsight/generation/surroundWith/JavaSurroundWithTest.java @@ -15,7 +15,6 @@ */ package com.intellij.codeInsight.generation.surroundWith; -import com.intellij.codeInsight.template.TemplateManager; import com.intellij.codeInsight.template.impl.TemplateManagerImpl; import com.intellij.codeInsight.template.impl.TemplateState; import com.intellij.lang.surroundWith.Surrounder; @@ -107,47 +106,23 @@ public class JavaSurroundWithTest extends LightCodeInsightTestCase { } public void testSurroundBinaryWithCast() { - final TemplateManagerImpl templateManager = (TemplateManagerImpl)TemplateManager.getInstance(getProject()); - templateManager.setTemplateTesting(true); - try { - doTest(getTestName(false), new JavaWithCastSurrounder()); - } - finally { - templateManager.setTemplateTesting(false); - } + TemplateManagerImpl.setTemplateTesting(getProject(), getTestRootDisposable()); + doTest(getTestName(false), new JavaWithCastSurrounder()); } public void testSurroundConditionalWithCast() { - final TemplateManagerImpl templateManager = (TemplateManagerImpl)TemplateManager.getInstance(getProject()); - templateManager.setTemplateTesting(true); - try { - doTest(getTestName(false), new JavaWithCastSurrounder()); - } - finally { - templateManager.setTemplateTesting(false); - } + TemplateManagerImpl.setTemplateTesting(getProject(), getTestRootDisposable()); + doTest(getTestName(false), new JavaWithCastSurrounder()); } public void testSurroundAssignmentWithCast() { - final TemplateManagerImpl templateManager = (TemplateManagerImpl)TemplateManager.getInstance(getProject()); - templateManager.setTemplateTesting(true); - try { - doTest(getTestName(false), new JavaWithCastSurrounder()); - } - finally { - templateManager.setTemplateTesting(false); - } + TemplateManagerImpl.setTemplateTesting(getProject(), getTestRootDisposable()); + doTest(getTestName(false), new JavaWithCastSurrounder()); } public void testSurroundWithNotNullCheck() { - final TemplateManagerImpl templateManager = (TemplateManagerImpl)TemplateManager.getInstance(getProject()); - templateManager.setTemplateTesting(true); - try { - doTest(getTestName(false), new JavaWithNullCheckSurrounder()); - } - finally { - templateManager.setTemplateTesting(false); - } + TemplateManagerImpl.setTemplateTesting(getProject(), getTestRootDisposable()); + doTest(getTestName(false), new JavaWithNullCheckSurrounder()); } private void doTest(@NotNull String fileName, final Surrounder surrounder) { @@ -158,22 +133,16 @@ public class JavaSurroundWithTest extends LightCodeInsightTestCase { private void doTestWithTemplateFinish(@NotNull String fileName, final Surrounder surrounder, @Nullable String textToType) throws Exception { - final TemplateManagerImpl templateManager = (TemplateManagerImpl)TemplateManager.getInstance(getProject()); - try { - templateManager.setTemplateTesting(true); - configureByFile(BASE_PATH + fileName + ".java"); - SurroundWithHandler.invoke(getProject(), getEditor(), getFile(), surrounder); - if (textToType != null) { - type(textToType); - } - TemplateState templateState = TemplateManagerImpl.getTemplateState(getEditor()); - assertNotNull(templateState); - templateState.nextTab(); - checkResultByFile(BASE_PATH + fileName + "_after.java"); - } - finally { - templateManager.setTemplateTesting(false); + TemplateManagerImpl.setTemplateTesting(getProject(), getTestRootDisposable()); + configureByFile(BASE_PATH + fileName + ".java"); + SurroundWithHandler.invoke(getProject(), getEditor(), getFile(), surrounder); + if (textToType != null) { + type(textToType); } + TemplateState templateState = TemplateManagerImpl.getTemplateState(getEditor()); + assertNotNull(templateState); + templateState.nextTab(); + checkResultByFile(BASE_PATH + fileName + "_after.java"); } } diff --git a/java/java-tests/testSrc/com/intellij/codeInsight/template/LiveTemplateTest.groovy b/java/java-tests/testSrc/com/intellij/codeInsight/template/LiveTemplateTest.groovy index 9f5dd2820cdf..42ab14bb56a9 100644 --- a/java/java-tests/testSrc/com/intellij/codeInsight/template/LiveTemplateTest.groovy +++ b/java/java-tests/testSrc/com/intellij/codeInsight/template/LiveTemplateTest.groovy @@ -37,13 +37,12 @@ public class LiveTemplateTest extends LightCodeInsightFixtureTestCase { @Override protected void setUp() throws Exception { super.setUp(); - ((TemplateManagerImpl)TemplateManager.getInstance(getProject())).setTemplateTesting(true); + TemplateManagerImpl.setTemplateTesting(getProject(), getTestRootDisposable()); } @Override protected void tearDown() throws Exception { CodeInsightSettings.instance.COMPLETION_CASE_SENSITIVE = CodeInsightSettings.FIRST_LETTER - ((TemplateManagerImpl)TemplateManager.getInstance(getProject())).setTemplateTesting(false); if (state != null) { state.gotoEnd(); } diff --git a/java/java-tests/testSrc/com/intellij/refactoring/InplaceRenameInvariantTest.groovy b/java/java-tests/testSrc/com/intellij/refactoring/InplaceRenameInvariantTest.groovy index a82c77d7f1e0..314ddba10281 100644 --- a/java/java-tests/testSrc/com/intellij/refactoring/InplaceRenameInvariantTest.groovy +++ b/java/java-tests/testSrc/com/intellij/refactoring/InplaceRenameInvariantTest.groovy @@ -104,7 +104,7 @@ class InplaceRenameInvariantTest extends LightCodeInsightTestCase { TemplateManagerImpl templateManager = (TemplateManagerImpl)TemplateManager.getInstance(project) def oldPreselectSetting = myEditor.settings.preselectRename try { - templateManager.templateTesting = true + TemplateManagerImpl.setTemplateTesting(getProject(), getTestRootDisposable()); myEditor.settings.preselectRename = preselect; int offset = myEditor.caretModel.offset final PsiElement element = TargetElementUtilBase.findTargetElement(myEditor, TargetElementUtilBase.getInstance().getAllAccepted()) @@ -126,16 +126,11 @@ class InplaceRenameInvariantTest extends LightCodeInsightTestCase { finally { myEditor.settings.preselectRename = oldPreselectSetting - try { - TemplateState state = TemplateManagerImpl.getTemplateState(editor) + TemplateState state = TemplateManagerImpl.getTemplateState(editor) - assertNotNull(state) + assertNotNull(state) - state.gotoEnd(false) - } - finally { - templateManager.templateTesting = false - } + state.gotoEnd(false) } } diff --git a/java/java-tests/testSrc/com/intellij/refactoring/RenameLocalTest.java b/java/java-tests/testSrc/com/intellij/refactoring/RenameLocalTest.java index c254828e4d36..9a7ab8abb48b 100644 --- a/java/java-tests/testSrc/com/intellij/refactoring/RenameLocalTest.java +++ b/java/java-tests/testSrc/com/intellij/refactoring/RenameLocalTest.java @@ -2,7 +2,6 @@ package com.intellij.refactoring; import com.intellij.JavaTestUtil; import com.intellij.codeInsight.TargetElementUtilBase; -import com.intellij.codeInsight.template.TemplateManager; import com.intellij.codeInsight.template.impl.TemplateManagerImpl; import com.intellij.codeInsight.template.impl.TemplateState; import com.intellij.lang.java.JavaRefactoringSupportProvider; @@ -138,29 +137,23 @@ public class RenameLocalTest extends LightRefactoringTestCase { final String name = getTestName(false); configureByFile(BASE_PATH + name + ".java"); - final TemplateManagerImpl templateManager = (TemplateManagerImpl)TemplateManager.getInstance(getProject()); - try { - templateManager.setTemplateTesting(true); + TemplateManagerImpl.setTemplateTesting(getProject(), getTestRootDisposable()); - new RenameWrongRefHandler().invoke(getProject(), getEditor(), getFile(), null); + new RenameWrongRefHandler().invoke(getProject(), getEditor(), getFile(), null); - final TemplateState state = TemplateManagerImpl.getTemplateState(getEditor()); - assert state != null; - final TextRange range = state.getCurrentVariableRange(); - assert range != null; + final TemplateState state = TemplateManagerImpl.getTemplateState(getEditor()); + assert state != null; + final TextRange range = state.getCurrentVariableRange(); + assert range != null; - new WriteCommandAction.Simple(getProject()) { - @Override - protected void run() throws Throwable { - getEditor().getDocument().replaceString(range.getStartOffset(), range.getEndOffset(), newName); - } - }.execute().throwException(); + new WriteCommandAction.Simple(getProject()) { + @Override + protected void run() throws Throwable { + getEditor().getDocument().replaceString(range.getStartOffset(), range.getEndOffset(), newName); + } + }.execute().throwException(); - state.gotoEnd(false); - checkResultByFile(BASE_PATH + name + "_after.java"); - } - finally { - templateManager.setTemplateTesting(false); - } + state.gotoEnd(false); + checkResultByFile(BASE_PATH + name + "_after.java"); } } diff --git a/platform/lang-impl/src/com/intellij/codeInsight/template/impl/TemplateManagerImpl.java b/platform/lang-impl/src/com/intellij/codeInsight/template/impl/TemplateManagerImpl.java index b789691011eb..07031663c75c 100644 --- a/platform/lang-impl/src/com/intellij/codeInsight/template/impl/TemplateManagerImpl.java +++ b/platform/lang-impl/src/com/intellij/codeInsight/template/impl/TemplateManagerImpl.java @@ -90,10 +90,23 @@ public class TemplateManagerImpl extends TemplateManager implements ProjectCompo } @TestOnly + @Deprecated public void setTemplateTesting(final boolean templateTesting) { myTemplateTesting = templateTesting; } + @TestOnly + public static void setTemplateTesting(Project project, Disposable parentDisposable) { + final TemplateManagerImpl instance = (TemplateManagerImpl)getInstance(project); + instance.myTemplateTesting = true; + Disposer.register(parentDisposable, new Disposable() { + @Override + public void dispose() { + instance.myTemplateTesting = false; + } + }); + } + private void disposeState(final TemplateState tState) { Disposer.dispose(tState); myDisposables.remove(tState); diff --git a/platform/testFramework/src/com/intellij/refactoring/AbstractInplaceIntroduceTest.java b/platform/testFramework/src/com/intellij/refactoring/AbstractInplaceIntroduceTest.java index eb8b15e25aaa..82b202250851 100644 --- a/platform/testFramework/src/com/intellij/refactoring/AbstractInplaceIntroduceTest.java +++ b/platform/testFramework/src/com/intellij/refactoring/AbstractInplaceIntroduceTest.java @@ -15,13 +15,11 @@ */ package com.intellij.refactoring; -import com.intellij.codeInsight.template.TemplateManager; import com.intellij.codeInsight.template.impl.TemplateManagerImpl; import com.intellij.codeInsight.template.impl.TemplateState; import com.intellij.openapi.util.Pass; import com.intellij.refactoring.introduce.inplace.AbstractInplaceIntroducer; import com.intellij.testFramework.LightPlatformCodeInsightTestCase; -import com.intellij.testFramework.LightPlatformTestCase; /** * User: anna @@ -35,9 +33,8 @@ public abstract class AbstractInplaceIntroduceTest extends LightPlatformCodeInsi String name = getTestName(true); configureByFile(getBasePath() + name + getExtension()); final boolean enabled = getEditor().getSettings().isVariableInplaceRenameEnabled(); - TemplateManagerImpl templateManager = (TemplateManagerImpl)TemplateManager.getInstance(LightPlatformTestCase.getProject()); try { - templateManager.setTemplateTesting(true); + TemplateManagerImpl.setTemplateTesting(getProject(), getTestRootDisposable()); getEditor().getSettings().setVariableInplaceRenameEnabled(true); invokeRefactoring(); @@ -48,7 +45,6 @@ public abstract class AbstractInplaceIntroduceTest extends LightPlatformCodeInsi } finally { getEditor().getSettings().setVariableInplaceRenameEnabled(enabled); - templateManager.setTemplateTesting(false); } } @@ -58,9 +54,8 @@ public abstract class AbstractInplaceIntroduceTest extends LightPlatformCodeInsi String name = getTestName(true); configureByFile(getBasePath() + name + getExtension()); final boolean enabled = getEditor().getSettings().isVariableInplaceRenameEnabled(); - TemplateManagerImpl templateManager = (TemplateManagerImpl)TemplateManager.getInstance(LightPlatformTestCase.getProject()); try { - templateManager.setTemplateTesting(true); + TemplateManagerImpl.setTemplateTesting(getProject(), getTestRootDisposable()); getEditor().getSettings().setVariableInplaceRenameEnabled(true); final AbstractInplaceIntroducer introducer = invokeRefactoring(); @@ -72,7 +67,6 @@ public abstract class AbstractInplaceIntroduceTest extends LightPlatformCodeInsi } finally { getEditor().getSettings().setVariableInplaceRenameEnabled(enabled); - templateManager.setTemplateTesting(false); } } diff --git a/xml/tests/src/com/intellij/codeInsight/completion/XmlCompletionTest.java b/xml/tests/src/com/intellij/codeInsight/completion/XmlCompletionTest.java index 999dd037987c..cf2bd9a5bbfe 100644 --- a/xml/tests/src/com/intellij/codeInsight/completion/XmlCompletionTest.java +++ b/xml/tests/src/com/intellij/codeInsight/completion/XmlCompletionTest.java @@ -168,19 +168,15 @@ public class XmlCompletionTest extends LightCodeInsightFixtureTestCase { } public void testAttributesTemplateFinishWithSpace() throws Throwable { - ((TemplateManagerImpl)TemplateManager.getInstance(getProject())).setTemplateTesting(true); - try { - configureByFile(getTestName(false) + ".xml"); - type('b'); - type('e'); - type('a'); - type('n'); - type(' '); - checkResultByFile(getTestName(false) + "_after.xml"); - } - finally { - ((TemplateManagerImpl)TemplateManager.getInstance(getProject())).setTemplateTesting(false); - } + TemplateManagerImpl.setTemplateTesting(getProject(), getTestRootDisposable()); + + configureByFile(getTestName(false) + ".xml"); + type('b'); + type('e'); + type('a'); + type('n'); + type(' '); + checkResultByFile(getTestName(false) + "_after.xml"); } private void configureByFile(String s) { @@ -189,17 +185,13 @@ public class XmlCompletionTest extends LightCodeInsightFixtureTestCase { } public void testNoAttributesTemplateFinishWithSpace() throws Throwable { - ((TemplateManagerImpl)TemplateManager.getInstance(getProject())).setTemplateTesting(true); - try { - configureByFile(getTestName(false) + ".xml"); - type('d'); - type('e'); - type(' '); - checkResultByFile(getTestName(false) + "_after.xml"); - } - finally { - ((TemplateManagerImpl)TemplateManager.getInstance(getProject())).setTemplateTesting(false); - } + TemplateManagerImpl.setTemplateTesting(getProject(), getTestRootDisposable()); + + configureByFile(getTestName(false) + ".xml"); + type('d'); + type('e'); + type(' '); + checkResultByFile(getTestName(false) + "_after.xml"); } private void type(char c) { diff --git a/xml/tests/src/com/intellij/codeInsight/template/XmlLiveTemplateTest.groovy b/xml/tests/src/com/intellij/codeInsight/template/XmlLiveTemplateTest.groovy index 6ae391ddbf7b..c928a92a7e57 100644 --- a/xml/tests/src/com/intellij/codeInsight/template/XmlLiveTemplateTest.groovy +++ b/xml/tests/src/com/intellij/codeInsight/template/XmlLiveTemplateTest.groovy @@ -14,7 +14,7 @@ * limitations under the License. */ package com.intellij.codeInsight.template -import com.intellij.codeInsight.CodeInsightSettings + import com.intellij.codeInsight.template.impl.ConstantNode import com.intellij.codeInsight.template.impl.EmptyNode import com.intellij.codeInsight.template.impl.TemplateManagerImpl @@ -32,13 +32,11 @@ public class XmlLiveTemplateTest extends LightCodeInsightFixtureTestCase { @Override protected void setUp() throws Exception { super.setUp(); - ((TemplateManagerImpl)TemplateManager.getInstance(getProject())).setTemplateTesting(true); + TemplateManagerImpl.setTemplateTesting(getProject(), getTestRootDisposable()); } @Override protected void tearDown() throws Exception { - CodeInsightSettings.instance.COMPLETION_CASE_SENSITIVE = CodeInsightSettings.FIRST_LETTER - ((TemplateManagerImpl)TemplateManager.getInstance(getProject())).setTemplateTesting(false); if (state != null) { state.gotoEnd(); }