From 1e53a71bde9e522908cbcdedee8e162ff6da4f31 Mon Sep 17 00:00:00 2001 From: Yaroslav Lepenkin Date: Wed, 21 Oct 2015 19:57:50 +0300 Subject: [PATCH] Recents tests: added navigation --- ...nTestRunner.java => RecentTestRunner.java} | 35 +++++++++++++++++-- .../testIntegration/SelectTestStep.java | 28 +++------------ .../testIntegration/ShowRecentTests.java | 28 +++++++++++---- .../testIntergration/RecentTestsTest.kt | 10 +++--- 4 files changed, 63 insertions(+), 38 deletions(-) rename java/execution/impl/src/com/intellij/testIntegration/{LocationTestRunner.java => RecentTestRunner.java} (67%) diff --git a/java/execution/impl/src/com/intellij/testIntegration/LocationTestRunner.java b/java/execution/impl/src/com/intellij/testIntegration/RecentTestRunner.java similarity index 67% rename from java/execution/impl/src/com/intellij/testIntegration/LocationTestRunner.java rename to java/execution/impl/src/com/intellij/testIntegration/RecentTestRunner.java index e75cab71b4ef..bba8f12d8413 100644 --- a/java/execution/impl/src/com/intellij/testIntegration/LocationTestRunner.java +++ b/java/execution/impl/src/com/intellij/testIntegration/RecentTestRunner.java @@ -16,24 +16,39 @@ package com.intellij.testIntegration; import com.intellij.execution.Location; +import com.intellij.execution.testframework.JavaTestLocator; import com.intellij.openapi.actionSystem.ActionManager; import com.intellij.openapi.actionSystem.AnAction; import com.intellij.openapi.actionSystem.AnActionEvent; import com.intellij.openapi.actionSystem.DataContext; +import com.intellij.openapi.project.Project; +import com.intellij.openapi.vfs.VirtualFileManager; +import com.intellij.psi.search.GlobalSearchScope; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.Nullable; -public interface LocationTestRunner { +import java.util.List; + +public interface RecentTestRunner { enum Mode { RUN, DEBUG } + void setMode(Mode mode); + void run(Location location); + + Location getLocation(String url); } -class LocationTestRunnerImpl implements LocationTestRunner { +class RecentTestRunnerImpl implements RecentTestRunner { private static AnAction RUN = ActionManager.getInstance().getAction("RunClass"); private static AnAction DEBUG = ActionManager.getInstance().getAction("DebugClass"); - + protected AnAction myCurrentAction = DEBUG; + private final Project myProject; + + public RecentTestRunnerImpl(Project project) { + myProject = project; + } public void setMode(Mode mode) { switch (mode) { @@ -45,6 +60,20 @@ class LocationTestRunnerImpl implements LocationTestRunner { break; } } + + public Location getLocation(String url) { + String protocol = VirtualFileManager.extractProtocol(url); + String path = VirtualFileManager.extractPath(url); + + if (protocol != null) { + List locations = JavaTestLocator.INSTANCE.getLocation(protocol, path, myProject, GlobalSearchScope.allScope(myProject)); + if (!locations.isEmpty()) { + return locations.get(0); + } + } + + return null; + } public void run(final Location location) { DataContext data = new DataContext() { diff --git a/java/execution/impl/src/com/intellij/testIntegration/SelectTestStep.java b/java/execution/impl/src/com/intellij/testIntegration/SelectTestStep.java index c73897cd7d87..697d85b80250 100644 --- a/java/execution/impl/src/com/intellij/testIntegration/SelectTestStep.java +++ b/java/execution/impl/src/com/intellij/testIntegration/SelectTestStep.java @@ -17,14 +17,12 @@ package com.intellij.testIntegration; import com.intellij.execution.Location; import com.intellij.execution.TestStateStorage; -import com.intellij.execution.testframework.JavaTestLocator; import com.intellij.execution.testframework.TestIconMapper; import com.intellij.execution.testframework.sm.runner.states.TestStateInfo; import com.intellij.openapi.project.Project; import com.intellij.openapi.ui.popup.PopupStep; import com.intellij.openapi.ui.popup.util.BaseListPopupStep; import com.intellij.openapi.vfs.VirtualFileManager; -import com.intellij.psi.search.GlobalSearchScope; import com.intellij.util.containers.ContainerUtil; import org.jetbrains.annotations.NotNull; @@ -36,13 +34,11 @@ import java.util.Map; public class SelectTestStep extends BaseListPopupStep { private final Map myRecords; - private final LocationTestRunner myRunner; - private final Project myProject; - - public SelectTestStep(Project project, Map records, LocationTestRunner runner) { + private final RecentTestRunner myRunner; + + public SelectTestStep(Map records, RecentTestRunner runner) { super("Debug Recent Tests", getUrls(records)); myRunner = runner; - myProject = project; myRecords = records; } @@ -76,24 +72,10 @@ public class SelectTestStep extends BaseListPopupStep { TestStateInfo.Magnitude magnitude = TestIconMapper.getMagnitude(record.magnitude); return TestIconMapper.getIcon(magnitude); } - - private Location getLocation(String url) { - String protocol = VirtualFileManager.extractProtocol(url); - String path = VirtualFileManager.extractPath(url); - - if (protocol != null) { - List locations = JavaTestLocator.INSTANCE.getLocation(protocol, path, myProject, GlobalSearchScope.allScope(myProject)); - if (!locations.isEmpty()) { - return locations.get(0); - } - } - - return null; - } - + @Override public PopupStep onChosen(String url, boolean finalChoice) { - Location location = getLocation(url); + Location location = myRunner.getLocation(url); myRunner.run(location); return null; } diff --git a/java/execution/impl/src/com/intellij/testIntegration/ShowRecentTests.java b/java/execution/impl/src/com/intellij/testIntegration/ShowRecentTests.java index 0b064d8c95a4..555b50d9a6a4 100644 --- a/java/execution/impl/src/com/intellij/testIntegration/ShowRecentTests.java +++ b/java/execution/impl/src/com/intellij/testIntegration/ShowRecentTests.java @@ -15,6 +15,7 @@ */ package com.intellij.testIntegration; +import com.intellij.execution.Location; import com.intellij.execution.TestStateStorage; import com.intellij.openapi.actionSystem.AnAction; import com.intellij.openapi.actionSystem.AnActionEvent; @@ -22,10 +23,12 @@ import com.intellij.openapi.actionSystem.CommonDataKeys; import com.intellij.openapi.project.Project; import com.intellij.openapi.ui.popup.ListPopupStep; import com.intellij.ui.popup.list.ListPopupImpl; +import com.intellij.util.PsiNavigateUtil; import com.intellij.util.Time; import javax.swing.*; import java.awt.event.ActionEvent; +import java.awt.event.KeyEvent; import java.util.Date; import java.util.Map; @@ -42,18 +45,18 @@ public class ShowRecentTests extends AnAction { if (project == null) return; Map records = TestStateStorage.getInstance(project).getRecentTests(TEST_LIMIT, getSinceDate()); - LocationTestRunner testRunner = new LocationTestRunnerImpl(); + RecentTestRunner testRunner = new RecentTestRunnerImpl(project); - SelectTestStep selectStepTest = new SelectTestStep(project, records, testRunner); + SelectTestStep selectStepTest = new SelectTestStep(records, testRunner); RecentTestsListPopup popup = new RecentTestsListPopup(selectStepTest, testRunner); popup.showCenteredInCurrentWindow(project); } } class RecentTestsListPopup extends ListPopupImpl { - private final LocationTestRunner myTestRunner; + private final RecentTestRunner myTestRunner; - public RecentTestsListPopup(ListPopupStep popupStep, LocationTestRunner testRunner) { + public RecentTestsListPopup(ListPopupStep popupStep, RecentTestRunner testRunner) { super(popupStep); myTestRunner = testRunner; shiftReleased(); @@ -79,16 +82,29 @@ class RecentTestsListPopup extends ListPopupImpl { handleSelect(true); } }); + popup.registerAction("navigate", KeyStroke.getKeyStroke(KeyEvent.VK_F4, 0), new AbstractAction() { + @Override + public void actionPerformed(ActionEvent e) { + Object[] values = getSelectedValues(); + if (values.length == 1) { + Location location = myTestRunner.getLocation(values[0].toString()); + if (location != null) { + cancel(); + PsiNavigateUtil.navigate(location.getPsiElement()); + } + } + } + }); } private void shiftPressed() { setCaption("Run Recent Tests"); - myTestRunner.setMode(LocationTestRunner.Mode.RUN); + myTestRunner.setMode(RecentTestRunner.Mode.RUN); } private void shiftReleased() { setCaption("Debug Recent Tests"); - myTestRunner.setMode(LocationTestRunner.Mode.DEBUG); + myTestRunner.setMode(RecentTestRunner.Mode.DEBUG); } } diff --git a/java/java-tests/testSrc/com/intellij/testIntergration/RecentTestsTest.kt b/java/java-tests/testSrc/com/intellij/testIntergration/RecentTestsTest.kt index 32c716e4a98c..ba3181fec9bd 100644 --- a/java/java-tests/testSrc/com/intellij/testIntergration/RecentTestsTest.kt +++ b/java/java-tests/testSrc/com/intellij/testIntergration/RecentTestsTest.kt @@ -16,17 +16,15 @@ package com.intellij.testIntergration import com.intellij.execution.TestStateStorage -import com.intellij.openapi.project.Project import com.intellij.testFramework.UsefulTestCase.* -import com.intellij.testIntegration.LocationTestRunner +import com.intellij.testIntegration.RecentTestRunner import com.intellij.testIntegration.SelectTestStep import org.junit.Test import org.mockito.Mockito.mock import java.util.* class RecentTestsStepTest { - val project = mock(Project::class.java) - val runner = mock(LocationTestRunner::class.java) + val runner = mock(RecentTestRunner::class.java) @Test fun `suites comes before their children tests`() { @@ -41,7 +39,7 @@ class RecentTestsStepTest { map.put("java:test://Test.textQQQ", TestStateStorage.Record(1, now)) map.put("java:test://JavaFormatterSuperDuperTest.testUnconditionalAlignmentErrorneous", TestStateStorage.Record(1, now)) - val step = SelectTestStep(project, map, runner) + val step = SelectTestStep(map, runner) val expected = listOf( "java:suite://JavaFormatterSuperDuperTest", @@ -60,7 +58,7 @@ class RecentTestsStepTest { @Test fun `shown value without protocol`() { - val step = SelectTestStep(project, emptyMap(), runner) + val step = SelectTestStep(emptyMap(), runner) var shownValue = step.getTextFor("java:suite://JavaFormatterSuperDuperTest") assertEquals(shownValue, "JavaFormatterSuperDuperTest")