IDEA-23142 Rerun failed tests keymap action should work when editor window has focus

This commit is contained in:
Vassiliy Kudryashov
2012-07-31 16:28:38 +04:00
parent 9eff0ee755
commit ce4ba7ecae
3 changed files with 128 additions and 76 deletions
@@ -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<AbstractRerunFailedTestsAction> registry = new ArrayList<AbstractRerunFailedTestsAction>();
private static final Logger LOG = Logger.getInstance("#com.intellij.execution.junit2.ui.actions.RerunFailedTestsAction");
private TestFrameworkRunningModel myModel;
private Getter<TestFrameworkRunningModel> 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<AbstractRerunFailedTestsAction> candidates = new ArrayList<AbstractRerunFailedTestsAction>(registry);
Collections.sort(candidates, new Comparator<AbstractRerunFailedTestsAction>() {
@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<? extends AbstractTestProxy> myAllTests = getModel().getRoot().getAllTests();
final List<? extends AbstractTestProxy> 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<AbstractTestProxy> getFailedTests(Project project) {
final List<? extends AbstractTestProxy> myAllTests = getModel().getRoot().getAllTests();
TestFrameworkRunningModel model = getModel();
final List<? extends AbstractTestProxy> myAllTests = model != null
? model.getRoot().getAllTests()
: Collections.<AbstractTestProxy>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;
@@ -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();
}
};
}
}
@@ -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