From 1177566386d70c39e4699d953127c5e0e66a43f6 Mon Sep 17 00:00:00 2001 From: Roman Golyshev Date: Mon, 19 Aug 2019 08:59:28 +0300 Subject: [PATCH] KT-32366: Get rid of `ScratchTopPanel` in tests - make `ScratchFile.file` public - remove obsolete functions from `ScratchTopPanel` and `scratchUtils` --- .../kotlin/idea/scratch/ScratchFile.kt | 2 +- .../kotlin/idea/scratch/scratchUtils.kt | 11 ----- .../idea/scratch/ui/ModulesComboBoxAction.kt | 9 +++- .../scratch/ui/ModulesComboBoxAction.kt.183 | 9 +++- .../kotlin/idea/scratch/ui/ScratchTopPanel.kt | 26 ----------- .../scratch/AbstractScratchRunActionTest.kt | 31 +++++-------- .../kotlin/idea/scratch/ScratchOptionsTest.kt | 45 ++++--------------- .../scratch/SequentialScratchExecutorTest.kt | 2 +- 8 files changed, 36 insertions(+), 99 deletions(-) diff --git a/idea/idea-jvm/src/org/jetbrains/kotlin/idea/scratch/ScratchFile.kt b/idea/idea-jvm/src/org/jetbrains/kotlin/idea/scratch/ScratchFile.kt index 0476561e9b9..45b3bbb9b0d 100644 --- a/idea/idea-jvm/src/org/jetbrains/kotlin/idea/scratch/ScratchFile.kt +++ b/idea/idea-jvm/src/org/jetbrains/kotlin/idea/scratch/ScratchFile.kt @@ -26,7 +26,7 @@ import com.intellij.util.messages.Topic import org.jetbrains.kotlin.idea.core.util.toPsiFile import org.jetbrains.kotlin.idea.scratch.ui.scratchFileOptions -abstract class ScratchFile(val project: Project, private val file: VirtualFile) { +abstract class ScratchFile(val project: Project, val file: VirtualFile) { var replScratchExecutor: SequentialScratchExecutor? = null var compilingScratchExecutor: ScratchExecutor? = null diff --git a/idea/idea-jvm/src/org/jetbrains/kotlin/idea/scratch/scratchUtils.kt b/idea/idea-jvm/src/org/jetbrains/kotlin/idea/scratch/scratchUtils.kt index e3ea0706ede..201740fa48c 100644 --- a/idea/idea-jvm/src/org/jetbrains/kotlin/idea/scratch/scratchUtils.kt +++ b/idea/idea-jvm/src/org/jetbrains/kotlin/idea/scratch/scratchUtils.kt @@ -38,22 +38,11 @@ fun getEditorWithoutScratchFile(fileManager: FileEditorManager, virtualFile: Vir return editor } -@TestOnly -fun getScratchPanelFromEditorSelectedForFile(fileManager: FileEditorManager, virtualFile: VirtualFile): ScratchTopPanel? { - val editor = fileManager.getSelectedEditor(virtualFile) as? TextEditor ?: return null - return editor.scratchTopPanel -} - fun getScratchFileFromEditorSelectedForFile(fileManager: FileEditorManager, virtualFile: VirtualFile): ScratchFile? { val editor = fileManager.getSelectedEditor(virtualFile) as? TextEditor ?: return null return editor.getScratchFile() } -fun getAllEditorsWithScratchFiles(project: Project): List = - FileEditorManager.getInstance(project).allEditors - .filterIsInstance() - .filter { it.getScratchFile() != null } - fun getScratchFileFromSelectedEditor(project: Project): ScratchFile? { val editor = FileEditorManager.getInstance(project).selectedTextEditor ?: return null return TextEditorProvider.getInstance().getTextEditor(editor).getScratchFile() diff --git a/idea/idea-jvm/src/org/jetbrains/kotlin/idea/scratch/ui/ModulesComboBoxAction.kt b/idea/idea-jvm/src/org/jetbrains/kotlin/idea/scratch/ui/ModulesComboBoxAction.kt index a812b700e82..6af81143dc0 100644 --- a/idea/idea-jvm/src/org/jetbrains/kotlin/idea/scratch/ui/ModulesComboBoxAction.kt +++ b/idea/idea-jvm/src/org/jetbrains/kotlin/idea/scratch/ui/ModulesComboBoxAction.kt @@ -15,6 +15,7 @@ import com.intellij.openapi.module.ModuleType import com.intellij.openapi.project.DumbAwareAction import com.intellij.openapi.vcs.changes.committed.LabeledComboBoxAction import com.intellij.util.ui.UIUtil +import org.jetbrains.annotations.TestOnly import org.jetbrains.kotlin.idea.caches.project.productionSourceInfo import org.jetbrains.kotlin.idea.caches.project.testSourceInfo import org.jetbrains.kotlin.idea.scratch.ScratchFile @@ -54,8 +55,12 @@ class ModulesComboBoxAction(private val scratchFile: ScratchFile) : LabeledCombo text = selectedModule?.name ?: ConfigurationModuleSelector.NO_MODULE_TEXT } - val isWorksheetFile = scratchFile.getPsiFile()?.virtualFile?.isKotlinWorksheet == true - e.presentation.isVisible = !isWorksheetFile + e.presentation.isVisible = isModuleSelectorVisible() + } + + @TestOnly + fun isModuleSelectorVisible(): Boolean { + return !scratchFile.file.isKotlinWorksheet } private inner class ModuleIsNotSelectedAction(placeholder: String) : DumbAwareAction(placeholder) { diff --git a/idea/idea-jvm/src/org/jetbrains/kotlin/idea/scratch/ui/ModulesComboBoxAction.kt.183 b/idea/idea-jvm/src/org/jetbrains/kotlin/idea/scratch/ui/ModulesComboBoxAction.kt.183 index 6dfa4fa39eb..c4385dabd2c 100644 --- a/idea/idea-jvm/src/org/jetbrains/kotlin/idea/scratch/ui/ModulesComboBoxAction.kt.183 +++ b/idea/idea-jvm/src/org/jetbrains/kotlin/idea/scratch/ui/ModulesComboBoxAction.kt.183 @@ -15,6 +15,7 @@ import com.intellij.openapi.module.ModuleType import com.intellij.openapi.project.DumbAwareAction import com.intellij.openapi.vcs.changes.committed.LabeledComboBoxAction import com.intellij.util.ui.UIUtil +import org.jetbrains.annotations.TestOnly import org.jetbrains.kotlin.idea.caches.project.productionSourceInfo import org.jetbrains.kotlin.idea.caches.project.testSourceInfo import org.jetbrains.kotlin.idea.scratch.ScratchFile @@ -54,8 +55,12 @@ class ModulesComboBoxAction(private val scratchFile: ScratchFile) : LabeledCombo text = selectedModule?.name ?: ConfigurationModuleSelector.NO_MODULE_TEXT } - val isWorksheetFile = scratchFile.getPsiFile()?.virtualFile?.isKotlinWorksheet == true - e.presentation.isVisible = !isWorksheetFile + e.presentation.isVisible = isModuleSelectorVisible() + } + + @TestOnly + fun isModuleSelectorVisible(): Boolean { + return !scratchFile.file.isKotlinWorksheet } private inner class ModuleIsNotSelectedAction(placeholder: String) : DumbAwareAction(placeholder) { diff --git a/idea/idea-jvm/src/org/jetbrains/kotlin/idea/scratch/ui/ScratchTopPanel.kt b/idea/idea-jvm/src/org/jetbrains/kotlin/idea/scratch/ui/ScratchTopPanel.kt index aa7ceadfa36..ae2dd14d2d6 100644 --- a/idea/idea-jvm/src/org/jetbrains/kotlin/idea/scratch/ui/ScratchTopPanel.kt +++ b/idea/idea-jvm/src/org/jetbrains/kotlin/idea/scratch/ui/ScratchTopPanel.kt @@ -79,24 +79,6 @@ class ScratchTopPanel(val scratchFile: ScratchFile) : Disposable { } } - @TestOnly - fun setReplMode(isSelected: Boolean) { - scratchFile.saveOptions { copy(isRepl = isSelected) } - } - - @TestOnly - fun setMakeBeforeRun(isSelected: Boolean) { - scratchFile.saveOptions { copy(isMakeBeforeRun = isSelected) } - } - - @TestOnly - fun setInteractiveMode(isSelected: Boolean) { - scratchFile.saveOptions { copy(isInteractiveMode = isSelected) } - } - - @TestOnly - fun getModuleSelectorAction(): AnAction = moduleChooserAction - private fun updateToolbar() { ApplicationManager.getApplication().invokeLater { actionsToolbar.updateActionsImmediately() @@ -146,11 +128,3 @@ class ScratchTopPanel(val scratchFile: ScratchFile) : Disposable { } } } - -interface ScratchPanelListener { - fun panelAdded(panel: ScratchTopPanel) - - companion object { - val TOPIC = Topic.create("ScratchPanelListener", ScratchPanelListener::class.java) - } -} diff --git a/idea/tests/org/jetbrains/kotlin/idea/scratch/AbstractScratchRunActionTest.kt b/idea/tests/org/jetbrains/kotlin/idea/scratch/AbstractScratchRunActionTest.kt index 5f49e19ac34..f56477d8772 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/scratch/AbstractScratchRunActionTest.kt +++ b/idea/tests/org/jetbrains/kotlin/idea/scratch/AbstractScratchRunActionTest.kt @@ -30,7 +30,6 @@ import org.jetbrains.kotlin.idea.scratch.actions.RunScratchAction import org.jetbrains.kotlin.idea.scratch.actions.ScratchCompilationSupport import org.jetbrains.kotlin.idea.scratch.output.InlayScratchFileRenderer import org.jetbrains.kotlin.idea.scratch.output.getInlays -import org.jetbrains.kotlin.idea.scratch.ui.ScratchTopPanel import org.jetbrains.kotlin.idea.test.KotlinLightProjectDescriptor import org.jetbrains.kotlin.idea.test.KotlinWithJdkAndRuntimeLightProjectDescriptor import org.jetbrains.kotlin.idea.test.PluginTestCaseBase @@ -163,8 +162,8 @@ abstract class AbstractScratchRunActionTest : FileEditorManagerTestCase() { return inlineElementsInRange.map { it.renderer as InlayScratchFileRenderer } } - protected fun configureScratchByText(name: String, text: String): ScratchTopPanel { - val scratchFile = ScratchRootType.getInstance().createScratchFile( + protected fun configureScratchByText(name: String, text: String): ScratchFile { + val scratchVirtualFile = ScratchRootType.getInstance().createScratchFile( project, name, KotlinLanguage.INSTANCE, @@ -172,31 +171,31 @@ abstract class AbstractScratchRunActionTest : FileEditorManagerTestCase() { ScratchFileService.Option.create_if_missing ) ?: error("Couldn't create scratch file") - myFixture.openFileInEditor(scratchFile) + myFixture.openFileInEditor(scratchVirtualFile) - ScriptDependenciesManager.updateScriptDependenciesSynchronously(scratchFile, project) + ScriptDependenciesManager.updateScriptDependenciesSynchronously(scratchVirtualFile, project) - val scratchPanel= getScratchPanelFromEditorSelectedForFile(myManager, myFixture.file.virtualFile) - ?: error("Couldn't find scratch panel") + val scratchFile = getScratchFileFromEditorSelectedForFile(myManager, myFixture.file.virtualFile) + ?: error("Couldn't find scratch file") - configureOptions(scratchPanel.scratchFile, text, myFixture.module) + configureOptions(scratchFile, text, myFixture.module) - return scratchPanel + return scratchFile } - protected fun configureWorksheetByText(name: String, text: String): ScratchTopPanel { + protected fun configureWorksheetByText(name: String, text: String): ScratchFile { val worksheetFile = myFixture.configureByText(name, text).virtualFile ScriptDependenciesManager.updateScriptDependenciesSynchronously(worksheetFile, project) - val scratchPanel = getScratchPanelFromEditorSelectedForFile(myManager, myFixture.file.virtualFile) + val scratchFile = getScratchFileFromEditorSelectedForFile(myManager, myFixture.file.virtualFile) ?: error("Couldn't find scratch panel") // We want to check that correct module is selected automatically, // that's why we set `module` to null so it wouldn't be changed - configureOptions(scratchPanel.scratchFile, text, null) + configureOptions(scratchFile, text, null) - return scratchPanel + return scratchFile } @@ -212,12 +211,6 @@ abstract class AbstractScratchRunActionTest : FileEditorManagerTestCase() { action.actionPerformed(e) } - protected fun getActionVisibility(action: AnAction): Boolean { - val e = getActionEvent(myFixture.file.virtualFile, action) - action.beforeActionPerformedUpdate(e) - return e.presentation.isVisible - } - protected fun waitUntilScratchFinishes(shouldStopRepl: Boolean = true) { UIUtil.dispatchAllInvocationEvents() diff --git a/idea/tests/org/jetbrains/kotlin/idea/scratch/ScratchOptionsTest.kt b/idea/tests/org/jetbrains/kotlin/idea/scratch/ScratchOptionsTest.kt index 7cf0f06105f..d58a4bde377 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/scratch/ScratchOptionsTest.kt +++ b/idea/tests/org/jetbrains/kotlin/idea/scratch/ScratchOptionsTest.kt @@ -5,7 +5,7 @@ package org.jetbrains.kotlin.idea.scratch -import org.jetbrains.kotlin.idea.scratch.ui.ScratchTopPanel +import org.jetbrains.kotlin.idea.scratch.ui.ModulesComboBoxAction import org.jetbrains.kotlin.test.JUnit3WithIdeaConfigurationRunner import org.junit.Assert import org.junit.runner.RunWith @@ -13,49 +13,20 @@ import org.junit.runner.RunWith @RunWith(JUnit3WithIdeaConfigurationRunner::class) class ScratchOptionsTest : AbstractScratchRunActionTest() { - fun testOptionsSaveOnClosingFile() { - val scratchPanelBeforeClosingFile = configureScratchByText("scratch_1.kts", testScratchText()) - - val newIsReplValue = !scratchPanelBeforeClosingFile.scratchFile.options.isRepl - val newIsMakeBeforeRunValue = !scratchPanelBeforeClosingFile.scratchFile.options.isMakeBeforeRun - val newIsInteractiveModeValue = !scratchPanelBeforeClosingFile.scratchFile.options.isInteractiveMode - - scratchPanelBeforeClosingFile.setReplMode(newIsReplValue) - scratchPanelBeforeClosingFile.setMakeBeforeRun(newIsMakeBeforeRunValue) - scratchPanelBeforeClosingFile.setInteractiveMode(newIsInteractiveModeValue) - - myManager.closeFile(myFixture.file.virtualFile) - myManager.openFile(myFixture.file.virtualFile, true) - - val scratchPanelAfterClosingFile = getScratchPanelFromEditorSelectedForFile(myManager, myFixture.file.virtualFile) ?: error("Couldn't find scratch panel") - - Assert.assertEquals("Wrong value for isRepl checkbox", newIsReplValue, scratchPanelAfterClosingFile.scratchFile.options.isRepl) - Assert.assertEquals( - "Wrong value for isMakeBeforeRun checkbox", - newIsMakeBeforeRunValue, - scratchPanelAfterClosingFile.scratchFile.options.isMakeBeforeRun - ) - Assert.assertEquals( - "Wrong value for isInteractiveMode checkbox", - newIsInteractiveModeValue, - scratchPanelAfterClosingFile.scratchFile.options.isInteractiveMode - ) - } - fun testModuleSelectionPanelIsVisibleForScratchFile() { - val scratchTopPanel = configureScratchByText("scratch_1.kts", testScratchText()) + val scratchFile = configureScratchByText("scratch_1.kts", testScratchText()) - Assert.assertTrue("Module selector should be visible for scratches", isModuleSelectorVisible(scratchTopPanel)) + Assert.assertTrue("Module selector should be visible for scratches", isModuleSelectorVisible(scratchFile)) } fun testModuleSelectionPanelIsHiddenForWorksheetFile() { - val scratchTopPanel = configureWorksheetByText("worksheet.ws.kts", testScratchText()) + val scratchFile = configureWorksheetByText("worksheet.ws.kts", testScratchText()) - Assert.assertFalse("Module selector should be hidden for worksheets", isModuleSelectorVisible(scratchTopPanel)) + Assert.assertFalse("Module selector should be hidden for worksheets", isModuleSelectorVisible(scratchFile)) } fun testCurrentModuleIsAutomaticallySelectedForWorksheetFile() { - val scratchFile = configureWorksheetByText("worksheet.ws.kts", testScratchText()).scratchFile + val scratchFile = configureWorksheetByText("worksheet.ws.kts", testScratchText()) Assert.assertEquals( "Selected module should be equal to current project module for worksheets", @@ -64,8 +35,8 @@ class ScratchOptionsTest : AbstractScratchRunActionTest() { ) } - private fun isModuleSelectorVisible(scratchTopPanel: ScratchTopPanel): Boolean { - return getActionVisibility(scratchTopPanel.getModuleSelectorAction()) + private fun isModuleSelectorVisible(scratchTopPanel: ScratchFile): Boolean { + return ModulesComboBoxAction(scratchTopPanel).isModuleSelectorVisible() } } \ No newline at end of file diff --git a/idea/tests/org/jetbrains/kotlin/idea/scratch/SequentialScratchExecutorTest.kt b/idea/tests/org/jetbrains/kotlin/idea/scratch/SequentialScratchExecutorTest.kt index 6d905cd5197..53d8a538e0a 100644 --- a/idea/tests/org/jetbrains/kotlin/idea/scratch/SequentialScratchExecutorTest.kt +++ b/idea/tests/org/jetbrains/kotlin/idea/scratch/SequentialScratchExecutorTest.kt @@ -39,7 +39,7 @@ class SequentialScratchExecutorTest : AbstractScratchRunActionTest() { } fun doTest(expression: List>) { - val scratchPanel = configureScratchByText("scratch_1.kts", testScratchText().inlinePropertiesValues(isRepl = true)) + configureScratchByText("scratch_1.kts", testScratchText().inlinePropertiesValues(isRepl = true)) myFixture.editor.caretModel.moveToOffset(myFixture.file.textLength)