KT-32366: Get rid of ScratchTopPanel in tests
- make `ScratchFile.file` public - remove obsolete functions from `ScratchTopPanel` and `scratchUtils`
This commit is contained in:
committed by
Roman Golyshev
parent
962260941b
commit
1177566386
@@ -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
|
||||
|
||||
|
||||
@@ -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<TextEditor> =
|
||||
FileEditorManager.getInstance(project).allEditors
|
||||
.filterIsInstance<TextEditor>()
|
||||
.filter { it.getScratchFile() != null }
|
||||
|
||||
fun getScratchFileFromSelectedEditor(project: Project): ScratchFile? {
|
||||
val editor = FileEditorManager.getInstance(project).selectedTextEditor ?: return null
|
||||
return TextEditorProvider.getInstance().getTextEditor(editor).getScratchFile()
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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()
|
||||
|
||||
|
||||
@@ -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()
|
||||
}
|
||||
|
||||
}
|
||||
@@ -39,7 +39,7 @@ class SequentialScratchExecutorTest : AbstractScratchRunActionTest() {
|
||||
}
|
||||
|
||||
fun doTest(expression: List<Pair<String, String>>) {
|
||||
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)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user