From b8ecf666e7caa2bd2c1fc49b0c644a42f24a3186 Mon Sep 17 00:00:00 2001 From: Alexey Kudravtsev Date: Mon, 27 Feb 2017 19:13:16 +0300 Subject: [PATCH] dispose project components along with the project in tests (testRootDisposable is disposed later than the project, so all objects registered on it are still alive when the project disposed which was treated as leaks) --- .../fileEditor/NonProjectFileAccessTest.java | 27 ++++++++++--------- .../impl/CodeInsightTestFixtureImpl.java | 22 ++++++++------- 2 files changed, 28 insertions(+), 21 deletions(-) diff --git a/platform/platform-tests/testSrc/com/intellij/openapi/fileEditor/NonProjectFileAccessTest.java b/platform/platform-tests/testSrc/com/intellij/openapi/fileEditor/NonProjectFileAccessTest.java index cab8c8f3a88d..d2445814c027 100644 --- a/platform/platform-tests/testSrc/com/intellij/openapi/fileEditor/NonProjectFileAccessTest.java +++ b/platform/platform-tests/testSrc/com/intellij/openapi/fileEditor/NonProjectFileAccessTest.java @@ -52,16 +52,15 @@ import java.io.IOException; import java.util.*; public class NonProjectFileAccessTest extends HeavyFileEditorManagerTestCase { - - private Set myOpenedFiles = new THashSet<>(); - private Set myCreatedFiles = new THashSet<>(); + private final Set myOpenedFiles = new THashSet<>(); + private final Set myCreatedFiles = new THashSet<>(); @Override public void setUp() throws Exception { super.setUp(); EditorNotifications notifications = new EditorNotificationsImpl(getProject()); ((ComponentManagerImpl)getProject()).registerComponentInstance(EditorNotifications.class, notifications); - NonProjectFileWritingAccessProvider.enableChecksInTests(getTestRootDisposable()); + NonProjectFileWritingAccessProvider.enableChecksInTests(getProject()); ProjectManagerEx.getInstanceEx().blockReloadingProjectOnExternalChanges(); } @@ -69,10 +68,7 @@ public class NonProjectFileAccessTest extends HeavyFileEditorManagerTestCase { protected void tearDown() throws Exception { try { NonProjectFileWritingAccessProvider.setCustomUnlocker(null); - FileEditorManager editorManager = FileEditorManager.getInstance(getProject()); - for (VirtualFile file : myOpenedFiles) { - editorManager.closeFile(file); - } + closeOpenFiles(); ApplicationManager.getApplication().runWriteAction(() -> { for (VirtualFile each : myCreatedFiles) { try { @@ -90,6 +86,13 @@ public class NonProjectFileAccessTest extends HeavyFileEditorManagerTestCase { } } + private void closeOpenFiles() { + FileEditorManager editorManager = FileEditorManager.getInstance(getProject()); + for (VirtualFile file : myOpenedFiles) { + editorManager.closeFile(file); + } + } + public void testBasicAccessCheck() throws Exception { VirtualFile projectFile = createProjectFile(); typeAndCheck(projectFile, true); @@ -323,7 +326,7 @@ public class NonProjectFileAccessTest extends HeavyFileEditorManagerTestCase { public boolean isPotentiallyWritable(@NotNull VirtualFile file) { return true; } - }, getTestRootDisposable()); + }, getProject()); return requested; } @@ -340,7 +343,7 @@ public class NonProjectFileAccessTest extends HeavyFileEditorManagerTestCase { return filesToDeny.contains(file); } }, - getTestRootDisposable()); + getProject()); } @NotNull @@ -359,7 +362,7 @@ public class NonProjectFileAccessTest extends HeavyFileEditorManagerTestCase { @Override protected void run(@NotNull Result result) throws Throwable { // create externally, since files created via VFS are marked for editing automatically - File file = new File(dir, FileUtil.createSequentFileName(dir, "file", "txt")); + File file = new File(dir, FileUtil.createSequentFileName(dir, "extfile", "txt")); file.createNewFile(); result.setResult(LocalFileSystem.getInstance().refreshAndFindFileByIoFile(file)); } @@ -399,7 +402,7 @@ public class NonProjectFileAccessTest extends HeavyFileEditorManagerTestCase { return editor; } - protected void typeInChar(Editor e, char c) { + private void typeInChar(Editor e, char c) { getActionManager().getTypedAction().actionPerformed(e, c, createDataContextFor(e)); } diff --git a/platform/testFramework/src/com/intellij/testFramework/fixtures/impl/CodeInsightTestFixtureImpl.java b/platform/testFramework/src/com/intellij/testFramework/fixtures/impl/CodeInsightTestFixtureImpl.java index 525c9adb55d9..a57728b18138 100644 --- a/platform/testFramework/src/com/intellij/testFramework/fixtures/impl/CodeInsightTestFixtureImpl.java +++ b/platform/testFramework/src/com/intellij/testFramework/fixtures/impl/CodeInsightTestFixtureImpl.java @@ -347,7 +347,7 @@ public class CodeInsightTestFixtureImpl extends BaseFixture implements CodeInsig @Override public void enableInspections(@NotNull InspectionProfileEntry... inspections) { assertInitialized(); - InspectionsKt.enableInspectionTools(getProject(), getTestRootDisposable(), inspections); + InspectionsKt.enableInspectionTools(getProject(), getProject(), inspections); } @SafeVarargs @@ -446,7 +446,7 @@ public class CodeInsightTestFixtureImpl extends BaseFixture implements CodeInsig @NotNull @Override public HighlightTestInfo testFile(@NotNull String... filePath) { - return new HighlightTestInfo(getTestRootDisposable(), filePath) { + return new HighlightTestInfo(getProject(), filePath) { @Override public HighlightTestInfo doTest() { configureByFiles(filePaths); @@ -985,7 +985,7 @@ public class CodeInsightTestFixtureImpl extends BaseFixture implements CodeInsig assertInitialized(); final ExtensionPoint extensionPoint = area.getExtensionPoint(epName); extensionPoint.registerExtension(extension); - Disposer.register(getTestRootDisposable(), new Disposable() { + Disposer.register(getProject(), new Disposable() { @Override public void dispose() { extensionPoint.unregisterExtension(extension); @@ -1167,7 +1167,7 @@ public class CodeInsightTestFixtureImpl extends BaseFixture implements CodeInsig PlatformTestCase.synchronizeTempDirVfs(tempDir); myPsiManager = (PsiManagerImpl)PsiManager.getInstance(getProject()); - InspectionsKt.configureInspections(LocalInspectionTool.EMPTY_ARRAY, getProject(), getTestRootDisposable()); + InspectionsKt.configureInspections(LocalInspectionTool.EMPTY_ARRAY, getProject(), myProjectFixture.getProject()); DaemonCodeAnalyzerImpl daemonCodeAnalyzer = (DaemonCodeAnalyzerImpl)DaemonCodeAnalyzer.getInstance(getProject()); daemonCodeAnalyzer.prepareForTest(); @@ -1184,11 +1184,7 @@ public class CodeInsightTestFixtureImpl extends BaseFixture implements CodeInsig EdtTestUtil.runInEdtAndWait(() -> { try { DaemonCodeAnalyzerSettings.getInstance().setImportHintEnabled(true); // return default value to avoid unnecessary save - FileEditorManager editorManager = FileEditorManager.getInstance(getProject()); - PsiDocumentManager.getInstance(getProject()).commitAllDocuments(); - for (VirtualFile openFile : editorManager.getOpenFiles()) { - editorManager.closeFile(openFile); - } + closeOpenFiles(); } finally { myEditor = null; @@ -1210,6 +1206,14 @@ public class CodeInsightTestFixtureImpl extends BaseFixture implements CodeInsig } } + private void closeOpenFiles() { + FileEditorManager editorManager = FileEditorManager.getInstance(getProject()); + PsiDocumentManager.getInstance(getProject()).commitAllDocuments(); + for (VirtualFile openFile : editorManager.getOpenFiles()) { + editorManager.closeFile(openFile); + } + } + @NotNull private PsiFile[] configureByFilesInner(@NotNull String... filePaths) { assertInitialized();