diff --git a/core/src/main/kotlin/org/jetbrains/research/testspark/core/progress/CustomProgressIndicator.kt b/core/src/main/kotlin/org/jetbrains/research/testspark/core/progress/CustomProgressIndicator.kt index 37d531e41..c451a4d74 100644 --- a/core/src/main/kotlin/org/jetbrains/research/testspark/core/progress/CustomProgressIndicator.kt +++ b/core/src/main/kotlin/org/jetbrains/research/testspark/core/progress/CustomProgressIndicator.kt @@ -11,4 +11,5 @@ interface CustomProgressIndicator { fun isCanceled(): Boolean fun start() fun stop() + fun isRunning(): Boolean } diff --git a/src/main/kotlin/org/jetbrains/research/testspark/actions/controllers/TestGenerationController.kt b/src/main/kotlin/org/jetbrains/research/testspark/actions/controllers/TestGenerationController.kt index 04e727a2e..618adba20 100644 --- a/src/main/kotlin/org/jetbrains/research/testspark/actions/controllers/TestGenerationController.kt +++ b/src/main/kotlin/org/jetbrains/research/testspark/actions/controllers/TestGenerationController.kt @@ -8,13 +8,14 @@ import com.intellij.openapi.project.Project import org.jetbrains.research.testspark.bundles.plugin.PluginMessagesBundle import org.jetbrains.research.testspark.core.monitor.DefaultErrorMonitor import org.jetbrains.research.testspark.core.monitor.ErrorMonitor +import org.jetbrains.research.testspark.core.progress.CustomProgressIndicator /** * Manager used for monitoring the unit test generation process. * It also limits TestSpark to generate tests only once at a time. */ class TestGenerationController { - private var isRunning: Boolean = false + var indicator: CustomProgressIndicator? = null // errorMonitor is passed in many places in the project // and reflects if any bug happened in the test generation process @@ -26,6 +27,7 @@ class TestGenerationController { private fun showGenerationRunningNotification(project: Project) { val terminateButton: AnAction = object : AnAction("Terminate") { override fun actionPerformed(e: AnActionEvent) { + indicator?.stop() errorMonitor.notifyErrorOccurrence() } } @@ -44,7 +46,11 @@ class TestGenerationController { } fun finished() { - isRunning = false + if (indicator != null && + indicator!!.isRunning() + ) { + indicator?.stop() + } } /** @@ -53,11 +59,15 @@ class TestGenerationController { * @return true if it is already running */ fun isGeneratorRunning(project: Project): Boolean { - if (isRunning) { + // If indicator is null, we have never initiated an indicator before and there is no running test generation + if (indicator == null) { + return false + } + + if (indicator!!.isRunning()) { showGenerationRunningNotification(project) return true } - isRunning = true return false } } diff --git a/src/main/kotlin/org/jetbrains/research/testspark/display/TestSparkDisplayManager.kt b/src/main/kotlin/org/jetbrains/research/testspark/display/TestSparkDisplayManager.kt index 87383ef72..710e98f79 100644 --- a/src/main/kotlin/org/jetbrains/research/testspark/display/TestSparkDisplayManager.kt +++ b/src/main/kotlin/org/jetbrains/research/testspark/display/TestSparkDisplayManager.kt @@ -4,6 +4,7 @@ import com.intellij.openapi.editor.Editor import com.intellij.openapi.project.Project import com.intellij.openapi.wm.ToolWindow import com.intellij.openapi.wm.ToolWindowManager +import com.intellij.serviceContainer.AlreadyDisposedException import com.intellij.ui.content.ContentManager import org.jetbrains.research.testspark.bundles.plugin.PluginLabelsBundle import org.jetbrains.research.testspark.bundles.plugin.PluginMessagesBundle @@ -94,7 +95,8 @@ class TestSparkDisplayManager { } } } - - toolWindow?.hide() + try { + toolWindow?.hide() + } catch (_: AlreadyDisposedException) {} // Make sure the process continues if the tool window is already closed } } diff --git a/src/main/kotlin/org/jetbrains/research/testspark/display/custom/IJProgressIndicator.kt b/src/main/kotlin/org/jetbrains/research/testspark/display/custom/IJProgressIndicator.kt index be6e78c88..813dadeff 100644 --- a/src/main/kotlin/org/jetbrains/research/testspark/display/custom/IJProgressIndicator.kt +++ b/src/main/kotlin/org/jetbrains/research/testspark/display/custom/IJProgressIndicator.kt @@ -24,6 +24,8 @@ class IJProgressIndicator(private val indicator: ProgressIndicator) : CustomProg override fun isCanceled(): Boolean = indicator.isCanceled + override fun isRunning(): Boolean = indicator.isRunning + override fun start() { indicator.start() } diff --git a/src/main/kotlin/org/jetbrains/research/testspark/display/generatedTests/GeneratedTestsTabBuilder.kt b/src/main/kotlin/org/jetbrains/research/testspark/display/generatedTests/GeneratedTestsTabBuilder.kt index 339fd39cb..bdde60d9b 100644 --- a/src/main/kotlin/org/jetbrains/research/testspark/display/generatedTests/GeneratedTestsTabBuilder.kt +++ b/src/main/kotlin/org/jetbrains/research/testspark/display/generatedTests/GeneratedTestsTabBuilder.kt @@ -3,6 +3,7 @@ package org.jetbrains.research.testspark.display.generatedTests import com.intellij.openapi.editor.Editor import com.intellij.openapi.project.Project import com.intellij.openapi.wm.ToolWindowManager +import com.intellij.serviceContainer.AlreadyDisposedException import com.intellij.ui.content.ContentFactory import com.intellij.ui.content.ContentManager import org.jetbrains.research.testspark.bundles.plugin.PluginLabelsBundle @@ -233,9 +234,11 @@ class GeneratedTestsTabBuilder( * Closes the tool window by removing the content and hiding the window. */ private fun closeToolWindow() { - generatedTestsTabData.contentManager?.removeContent(generatedTestsTabData.content!!, true) - ToolWindowManager.getInstance(project).getToolWindow("TestSpark")?.hide() - coverageVisualisationTabBuilder.closeToolWindowTab() + try { + generatedTestsTabData.contentManager?.removeContent(generatedTestsTabData.content!!, true) + ToolWindowManager.getInstance(project).getToolWindow("TestSpark")?.hide() + coverageVisualisationTabBuilder.closeToolWindowTab() + } catch (_: AlreadyDisposedException) {} // Make sure the process continues if the tool window is already closed } /** diff --git a/src/main/kotlin/org/jetbrains/research/testspark/progress/HeadlessProgressIndicator.kt b/src/main/kotlin/org/jetbrains/research/testspark/progress/HeadlessProgressIndicator.kt index 5cec334de..bea75791d 100644 --- a/src/main/kotlin/org/jetbrains/research/testspark/progress/HeadlessProgressIndicator.kt +++ b/src/main/kotlin/org/jetbrains/research/testspark/progress/HeadlessProgressIndicator.kt @@ -23,6 +23,8 @@ class HeadlessProgressIndicator : CustomProgressIndicator { override fun isCanceled(): Boolean = false + override fun isRunning(): Boolean = true + override fun start() {} override fun stop() {} diff --git a/src/main/kotlin/org/jetbrains/research/testspark/tools/Pipeline.kt b/src/main/kotlin/org/jetbrains/research/testspark/tools/Pipeline.kt index 33f526e0b..31499ec5f 100644 --- a/src/main/kotlin/org/jetbrains/research/testspark/tools/Pipeline.kt +++ b/src/main/kotlin/org/jetbrains/research/testspark/tools/Pipeline.kt @@ -90,6 +90,7 @@ class Pipeline( override fun run(indicator: ProgressIndicator) { try { val ijIndicator = IJProgressIndicator(indicator) + testGenerationController.indicator = ijIndicator if (ToolUtils.isProcessStopped(testGenerationController.errorMonitor, ijIndicator)) return