From ce4ba7ecae664558088602648a71279bc65858b3 Mon Sep 17 00:00:00 2001 From: Vassiliy Kudryashov Date: Tue, 31 Jul 2012 16:28:38 +0400 Subject: [PATCH] IDEA-23142 Rerun failed tests keymap action should work when editor window has focus --- .../AbstractRerunFailedTestsAction.java | 74 ++++++++-- .../ui/actions/RerunFailedTestsAction.java | 126 +++++++++--------- .../ui/actions/RerunFailedTestsAction.java | 4 +- 3 files changed, 128 insertions(+), 76 deletions(-) diff --git a/java/execution/impl/src/com/intellij/execution/actions/AbstractRerunFailedTestsAction.java b/java/execution/impl/src/com/intellij/execution/actions/AbstractRerunFailedTestsAction.java index 88d1a6bd3165..51f2c7e41a77 100644 --- a/java/execution/impl/src/com/intellij/execution/actions/AbstractRerunFailedTestsAction.java +++ b/java/execution/impl/src/com/intellij/execution/actions/AbstractRerunFailedTestsAction.java @@ -30,10 +30,7 @@ import com.intellij.execution.runners.ExecutionEnvironment; import com.intellij.execution.runners.ProgramRunner; import com.intellij.execution.testframework.*; import com.intellij.idea.ActionsBundle; -import com.intellij.openapi.actionSystem.AnAction; -import com.intellij.openapi.actionSystem.AnActionEvent; -import com.intellij.openapi.actionSystem.DataContext; -import com.intellij.openapi.actionSystem.PlatformDataKeys; +import com.intellij.openapi.actionSystem.*; import com.intellij.openapi.diagnostic.Logger; import com.intellij.openapi.options.SettingsEditor; import com.intellij.openapi.project.Project; @@ -43,16 +40,36 @@ import com.intellij.openapi.util.JDOMExternalizable; import com.intellij.openapi.util.WriteExternalException; import org.jdom.Element; import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; +import javax.swing.*; +import java.awt.*; import java.util.ArrayList; +import java.util.Collections; +import java.util.Comparator; import java.util.List; -public class AbstractRerunFailedTestsAction extends AnAction { +public class AbstractRerunFailedTestsAction extends AnAction implements AnAction.TransparentUpdate { + private static List registry = new ArrayList(); private static final Logger LOG = Logger.getInstance("#com.intellij.execution.junit2.ui.actions.RerunFailedTestsAction"); private TestFrameworkRunningModel myModel; private Getter myModelProvider; protected TestConsoleProperties myConsoleProperties; protected ExecutionEnvironment myEnvironment; + private final JComponent myParent; + + + public AbstractRerunFailedTestsAction() { + //We call this constructor with a little help from reflection. + myParent = null; + } + + protected AbstractRerunFailedTestsAction(JComponent parent) { + myParent = parent; + registry.add(this); + copyFrom(ActionManager.getInstance().getAction("RerunFailedTests")); + registerCustomShortcutSet(getShortcutSet(), parent); + } public void init(final TestConsoleProperties consoleProperties, final ExecutionEnvironment environment) { @@ -68,8 +85,36 @@ public class AbstractRerunFailedTestsAction extends AnAction { myModelProvider = modelProvider; } - public void update(AnActionEvent e) { - e.getPresentation().setEnabled(isActive(e) && !getModel().isRunning()); + @NotNull + private AbstractRerunFailedTestsAction findActualAction() { + if (myParent != null || registry.isEmpty()) + return this; + List candidates = new ArrayList(registry); + Collections.sort(candidates, new Comparator() { + @Override + public int compare(AbstractRerunFailedTestsAction action1, AbstractRerunFailedTestsAction action2) { + Window window1 = SwingUtilities.windowForComponent(action1.myParent); + Window window2 = SwingUtilities.windowForComponent(action2.myParent); + if (window1 == null) + return 1; + if (window2 == null) + return -1; + boolean showing1 = action1.myParent.isShowing(); + boolean showing2 = action2.myParent.isShowing(); + if (showing1 && !showing2) + return -1; + if (showing2 && !showing1) + return 1; + return (window1.isActive() ? -1 : 1); + } + }); + return candidates.get(0); + } + + public final void update(AnActionEvent e) { + AbstractRerunFailedTestsAction action = findActualAction(); + TestFrameworkRunningModel model = action.getModel(); + e.getPresentation().setEnabled(action.isActive(e) && model != null && !model.isRunning()); } private boolean isActive(AnActionEvent e) { @@ -78,7 +123,7 @@ public class AbstractRerunFailedTestsAction extends AnAction { if (project == null) return false; TestFrameworkRunningModel model = getModel(); if (model == null || model.getRoot() == null) return false; - final List myAllTests = getModel().getRoot().getAllTests(); + final List myAllTests = model.getRoot().getAllTests(); for (Object test : myAllTests) { if (Filter.FAILED_OR_INTERRUPTED.and(JavaAwareFilter.METHOD(project)).shouldAccept((AbstractTestProxy)test)) return true; } @@ -87,13 +132,22 @@ public class AbstractRerunFailedTestsAction extends AnAction { @NotNull protected List getFailedTests(Project project) { - final List myAllTests = getModel().getRoot().getAllTests(); + TestFrameworkRunningModel model = getModel(); + final List myAllTests = model != null + ? model.getRoot().getAllTests() + : Collections.emptyList(); return Filter.FAILED_OR_INTERRUPTED.and(JavaAwareFilter.METHOD(project)).select(myAllTests); } public void actionPerformed(AnActionEvent e) { + findActualAction().performAction(); + } + + private void performAction() { boolean isDebug = myConsoleProperties.isDebug(); final MyRunProfile profile = getRunProfile(); + if (profile == null) + return; try { final Executor executor = isDebug ? DefaultDebugExecutor.getDebugExecutorInstance() : DefaultRunExecutor.getRunExecutorInstance(); final ProgramRunner runner = RunnerRegistry.getInstance().getRunner(executor.getId(), profile); @@ -114,10 +168,12 @@ public class AbstractRerunFailedTestsAction extends AnAction { } } + @Nullable public MyRunProfile getRunProfile() { return null; } + @Nullable public TestFrameworkRunningModel getModel() { if (myModel != null) { return myModel; diff --git a/plugins/junit/src/com/intellij/execution/junit2/ui/actions/RerunFailedTestsAction.java b/plugins/junit/src/com/intellij/execution/junit2/ui/actions/RerunFailedTestsAction.java index bd1a168e1fc2..b2f086f9122c 100644 --- a/plugins/junit/src/com/intellij/execution/junit2/ui/actions/RerunFailedTestsAction.java +++ b/plugins/junit/src/com/intellij/execution/junit2/ui/actions/RerunFailedTestsAction.java @@ -1,64 +1,62 @@ -/* - * Copyright 2000-2009 JetBrains s.r.o. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.intellij.execution.junit2.ui.actions; - -import com.intellij.execution.ExecutionException; -import com.intellij.execution.Executor; -import com.intellij.execution.actions.AbstractRerunFailedTestsAction; -import com.intellij.execution.configurations.RunProfileState; -import com.intellij.execution.junit.JUnitConfiguration; -import com.intellij.execution.junit.TestMethods; -import com.intellij.execution.runners.ExecutionEnvironment; -import com.intellij.openapi.actionSystem.ActionManager; -import com.intellij.openapi.module.Module; -import org.jetbrains.annotations.NotNull; - -import javax.swing.*; - -/** - * @author Alexey - */ -public class RerunFailedTestsAction extends AbstractRerunFailedTestsAction { - - public RerunFailedTestsAction(JComponent parent) { - copyFrom(ActionManager.getInstance().getAction("RerunFailedTests")); - registerCustomShortcutSet(getShortcutSet(), parent); - } - - @Override - public MyRunProfile getRunProfile() { - final JUnitConfiguration configuration = (JUnitConfiguration)getModel().getProperties().getConfiguration(); - final TestMethods testMethods = new TestMethods(configuration.getProject(), configuration, myEnvironment, getFailedTests(configuration.getProject())); - return new MyRunProfile(configuration) { - @NotNull - public Module[] getModules() { - return testMethods.getModulesToCompile(); - } - - public RunProfileState getState(@NotNull Executor executor, @NotNull ExecutionEnvironment env) throws ExecutionException { - testMethods.clear(); - return testMethods; - } - - @Override - public void clear() { - testMethods.clear(); - super.clear(); - } - }; - } -} +/* + * Copyright 2000-2009 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.intellij.execution.junit2.ui.actions; + +import com.intellij.execution.ExecutionException; +import com.intellij.execution.Executor; +import com.intellij.execution.actions.AbstractRerunFailedTestsAction; +import com.intellij.execution.configurations.RunProfileState; +import com.intellij.execution.junit.JUnitConfiguration; +import com.intellij.execution.junit.TestMethods; +import com.intellij.execution.runners.ExecutionEnvironment; +import com.intellij.openapi.module.Module; +import org.jetbrains.annotations.NotNull; + +import javax.swing.*; + +/** + * @author Alexey + */ +public class RerunFailedTestsAction extends AbstractRerunFailedTestsAction { + + public RerunFailedTestsAction(JComponent parent) { + super(parent); + } + + @Override + public MyRunProfile getRunProfile() { + final JUnitConfiguration configuration = (JUnitConfiguration)getModel().getProperties().getConfiguration(); + final TestMethods testMethods = new TestMethods(configuration.getProject(), configuration, myEnvironment, getFailedTests(configuration.getProject())); + return new MyRunProfile(configuration) { + @NotNull + public Module[] getModules() { + return testMethods.getModulesToCompile(); + } + + public RunProfileState getState(@NotNull Executor executor, @NotNull ExecutionEnvironment env) throws ExecutionException { + testMethods.clear(); + return testMethods; + } + + @Override + public void clear() { + testMethods.clear(); + super.clear(); + } + }; + } +} diff --git a/plugins/testng/src/com/theoryinpractice/testng/ui/actions/RerunFailedTestsAction.java b/plugins/testng/src/com/theoryinpractice/testng/ui/actions/RerunFailedTestsAction.java index 2be6709cf9a3..6a2cf15988b7 100644 --- a/plugins/testng/src/com/theoryinpractice/testng/ui/actions/RerunFailedTestsAction.java +++ b/plugins/testng/src/com/theoryinpractice/testng/ui/actions/RerunFailedTestsAction.java @@ -9,7 +9,6 @@ import com.intellij.execution.actions.AbstractRerunFailedTestsAction; import com.intellij.execution.configurations.RunProfileState; import com.intellij.execution.runners.ExecutionEnvironment; import com.intellij.execution.testframework.AbstractTestProxy; -import com.intellij.openapi.actionSystem.ActionManager; import com.intellij.openapi.module.Module; import com.intellij.psi.PsiClass; import com.intellij.psi.PsiElement; @@ -29,8 +28,7 @@ import java.util.Map; public class RerunFailedTestsAction extends AbstractRerunFailedTestsAction { public RerunFailedTestsAction(JComponent parent) { - copyFrom(ActionManager.getInstance().getAction("RerunFailedTests")); - registerCustomShortcutSet(getShortcutSet(), parent); + super(parent); } @Override