|
| 1 | +package dev.robotcode.robotcode4ij.execution |
| 2 | + |
| 3 | +import com.intellij.execution.Executor |
| 4 | +import com.intellij.execution.ExecutorRegistry |
| 5 | +import com.intellij.execution.executors.DefaultDebugExecutor |
| 6 | +import com.intellij.execution.executors.DefaultRunExecutor |
| 7 | +import com.intellij.execution.lineMarker.ExecutorAction |
| 8 | +import com.intellij.execution.lineMarker.RunLineMarkerContributor |
| 9 | +import com.intellij.icons.AllIcons |
| 10 | +import com.intellij.openapi.actionSystem.ActionManager |
| 11 | +import com.intellij.openapi.actionSystem.AnAction |
| 12 | +import com.intellij.openapi.wm.ToolWindowId |
| 13 | +import com.intellij.psi.PsiDocumentManager |
| 14 | +import com.intellij.testFramework.fixtures.BasePlatformTestCase |
| 15 | +import dev.robotcode.robotcode4ij.actions.DEBUG_WITH_PROFILE_ACTION_ID |
| 16 | +import dev.robotcode.robotcode4ij.actions.RUN_WITH_PROFILE_ACTION_ID |
| 17 | +import dev.robotcode.robotcode4ij.testing.Position |
| 18 | +import dev.robotcode.robotcode4ij.testing.Range |
| 19 | +import dev.robotcode.robotcode4ij.testing.RobotCodeTestItem |
| 20 | +import dev.robotcode.robotcode4ij.testing.testManger |
| 21 | +import dev.robotcode.robotcode4ij.testing.uri |
| 22 | +import javax.swing.Icon |
| 23 | + |
| 24 | +private const val OTHER_EXECUTOR_ID = "dev.robotcode.robotcode4ij.test.OtherExecutor" |
| 25 | + |
| 26 | +// an executor besides Run and Debug, as PyCharm Professional's profiler group is one; the platform registers its |
| 27 | +// context action, so that the gutter offers it |
| 28 | +private class OtherExecutor : Executor() { |
| 29 | + override fun getToolWindowId(): String = ToolWindowId.RUN |
| 30 | + override fun getToolWindowIcon(): Icon = AllIcons.Actions.Execute |
| 31 | + override fun getIcon(): Icon = AllIcons.Actions.Execute |
| 32 | + override fun getDisabledIcon(): Icon? = null |
| 33 | + override fun getDescription(): String = "Runs with another tool" |
| 34 | + override fun getActionName(): String = "Other" |
| 35 | + override fun getId(): String = OTHER_EXECUTOR_ID |
| 36 | + override fun getStartActionText(): String = "Run with Other" |
| 37 | + override fun getContextActionId(): String = "$OTHER_EXECUTOR_ID.context" |
| 38 | + override fun getHelpId(): String? = null |
| 39 | +} |
| 40 | + |
| 41 | +class RobotCodeRunMarkerExecutorsTest : BasePlatformTestCase() { |
| 42 | + |
| 43 | + override fun setUp() { |
| 44 | + super.setUp() |
| 45 | + // the registry adds the context action of an executor that is registered later |
| 46 | + ExecutorRegistry.getInstance() |
| 47 | + Executor.EXECUTOR_EXTENSION_NAME.point.registerExtension(OtherExecutor(), testRootDisposable) |
| 48 | + } |
| 49 | + |
| 50 | + override fun tearDown() { |
| 51 | + try { |
| 52 | + project.testManger.setTestItemsForTests(arrayOf()) |
| 53 | + } finally { |
| 54 | + super.tearDown() |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + // a test "First" on the second line of a suite file |
| 59 | + private fun suiteWithOneTest() { |
| 60 | + val file = myFixture.configureByText("suite.robot", "*** Test Cases ***\nFirst\n Log first\n").virtualFile |
| 61 | + val test = RobotCodeTestItem( |
| 62 | + type = "test", id = "suite.First", name = "First", longname = "Suite.First", lineno = 2, |
| 63 | + uri = file.uri, source = file.path, range = Range(Position(1u, 0u), Position(1u, 5u)) |
| 64 | + ) |
| 65 | + val suite = RobotCodeTestItem( |
| 66 | + type = "suite", id = "suite", name = "Suite", longname = "Suite", uri = file.uri, source = file.path, |
| 67 | + children = arrayOf(test) |
| 68 | + ) |
| 69 | + project.testManger.setTestItemsForTests(arrayOf(suite)) |
| 70 | + } |
| 71 | + |
| 72 | + private fun executorIds(actions: Array<AnAction>) = actions.filterIsInstance<ExecutorAction>().map { it.executor.id } |
| 73 | + |
| 74 | + private fun assertOnlyRunAndDebugAmongTheExecutors(info: RunLineMarkerContributor.Info?) { |
| 75 | + val gutter = ExecutorAction.getActions(1) |
| 76 | + assertTrue(OTHER_EXECUTOR_ID in executorIds(gutter)) |
| 77 | + |
| 78 | + val actions = info!!.actions |
| 79 | + val manager = ActionManager.getInstance() |
| 80 | + val extras = gutter.filter { it !is ExecutorAction } |
| 81 | + |
| 82 | + assertEquals(listOf(DefaultRunExecutor.EXECUTOR_ID, DefaultDebugExecutor.EXECUTOR_ID), executorIds(actions)) |
| 83 | + assertEquals(listOf(RUN_WITH_PROFILE_ACTION_ID, DEBUG_WITH_PROFILE_ACTION_ID), actions.drop(2).take(2).map { |
| 84 | + manager.getId(it) |
| 85 | + }) |
| 86 | + assertEquals(extras, actions.drop(4)) |
| 87 | + } |
| 88 | + |
| 89 | + fun testTheRunMarkerOfATestOffersOnlyRunAndDebugAmongTheExecutors() { |
| 90 | + suiteWithOneTest() |
| 91 | + val document = PsiDocumentManager.getInstance(project).getDocument(myFixture.file)!! |
| 92 | + val testName = myFixture.file.findElementAt(document.getLineStartOffset(1))!! |
| 93 | + |
| 94 | + assertOnlyRunAndDebugAmongTheExecutors(RobotCodeRunLineMarkerContributor().getInfo(testName)) |
| 95 | + } |
| 96 | + |
| 97 | + fun testTheRunMarkerOfASuiteOffersOnlyRunAndDebugAmongTheExecutors() { |
| 98 | + suiteWithOneTest() |
| 99 | + |
| 100 | + assertOnlyRunAndDebugAmongTheExecutors(RobotCodeRunLineMarkerContributor().getInfo(myFixture.file)) |
| 101 | + } |
| 102 | +} |
0 commit comments