Recent tests popup - initial

This commit is contained in:
Yaroslav Lepenkin
2015-10-21 16:42:10 +03:00
parent 0594d76bf6
commit af6933e0d2
8 changed files with 358 additions and 0 deletions
@@ -0,0 +1,63 @@
/*
* Copyright 2000-2015 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.testIntegration;
import com.intellij.execution.Location;
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 org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.Nullable;
public interface LocationTestRunner {
enum Mode { RUN, DEBUG }
void setMode(Mode mode);
void run(Location location);
}
class LocationTestRunnerImpl implements LocationTestRunner {
private static AnAction RUN = ActionManager.getInstance().getAction("RunClass");
private static AnAction DEBUG = ActionManager.getInstance().getAction("DebugClass");
protected AnAction myCurrentAction = DEBUG;
public void setMode(Mode mode) {
switch (mode) {
case RUN:
myCurrentAction = RUN;
break;
case DEBUG:
myCurrentAction = DEBUG;
break;
}
}
public void run(final Location location) {
DataContext data = new DataContext() {
@Nullable
@Override
public Object getData(@NonNls String dataId) {
if (Location.DATA_KEY.is(dataId)) {
return location;
}
return null;
}
};
myCurrentAction.actionPerformed(AnActionEvent.createFromAnAction(myCurrentAction, null, "", data));
}
}
@@ -0,0 +1,100 @@
/*
* Copyright 2000-2015 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.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;
import javax.swing.*;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
public class SelectTestStep extends BaseListPopupStep<String> {
private final Map<String, TestStateStorage.Record> myRecords;
private final LocationTestRunner myRunner;
private final Project myProject;
public SelectTestStep(Project project, Map<String, TestStateStorage.Record> records, LocationTestRunner runner) {
super("Debug Recent Tests", getUrls(records));
myRunner = runner;
myProject = project;
myRecords = records;
}
private static List<String> getUrls(Map<String, TestStateStorage.Record> records) {
List<String> list = ContainerUtil.newArrayList(records.keySet());
Collections.sort(list, new Comparator<String>() {
@Override
public int compare(String o1, String o2) {
String path1 = VirtualFileManager.extractPath(o1);
String path2 = VirtualFileManager.extractPath(o2);
return path1.compareTo(path2);
}
});
return list;
}
@NotNull
@Override
public String getTextFor(String value) {
return VirtualFileManager.extractPath(value);
}
@Override
public boolean isSpeedSearchEnabled() {
return true;
}
@Override
public Icon getIconFor(String value) {
TestStateStorage.Record record = myRecords.get(value);
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<Location> 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);
myRunner.run(location);
return null;
}
}
@@ -0,0 +1,95 @@
/*
* Copyright 2000-2015 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.testIntegration;
import com.intellij.execution.TestStateStorage;
import com.intellij.openapi.actionSystem.AnAction;
import com.intellij.openapi.actionSystem.AnActionEvent;
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.Time;
import javax.swing.*;
import java.awt.event.ActionEvent;
import java.util.Date;
import java.util.Map;
public class ShowRecentTests extends AnAction {
private static final int TEST_LIMIT = 20;
private static Date getSinceDate() {
return new Date(System.currentTimeMillis() - 2 * Time.HOUR);
}
@Override
public void actionPerformed(AnActionEvent e) {
final Project project = CommonDataKeys.PROJECT.getData(e.getDataContext());
if (project == null) return;
Map<String, TestStateStorage.Record> records = TestStateStorage.getInstance(project).getRecentTests(TEST_LIMIT, getSinceDate());
LocationTestRunner testRunner = new LocationTestRunnerImpl();
SelectTestStep selectStepTest = new SelectTestStep(project, records, testRunner);
RecentTestsListPopup popup = new RecentTestsListPopup(selectStepTest, testRunner);
popup.showCenteredInCurrentWindow(project);
}
}
class RecentTestsListPopup extends ListPopupImpl {
private final LocationTestRunner myTestRunner;
public RecentTestsListPopup(ListPopupStep<String> popupStep, LocationTestRunner testRunner) {
super(popupStep);
myTestRunner = testRunner;
shiftReleased();
registerActions(this);
}
private void registerActions(ListPopupImpl popup) {
popup.registerAction("alternate", KeyStroke.getKeyStroke("shift pressed SHIFT"), new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
shiftPressed();
}
});
popup.registerAction("restoreDefault", KeyStroke.getKeyStroke("released SHIFT"), new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
shiftReleased();
}
});
popup.registerAction("invokeAction", KeyStroke.getKeyStroke("shift ENTER"), new AbstractAction() {
@Override
public void actionPerformed(ActionEvent e) {
handleSelect(true);
}
});
}
private void shiftPressed() {
setCaption("Run Recent Tests");
myTestRunner.setMode(LocationTestRunner.Mode.RUN);
}
private void shiftReleased() {
setCaption("Debug Recent Tests");
myTestRunner.setMode(LocationTestRunner.Mode.DEBUG);
}
}