2 * Copyright 2000-2015 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.testIntegration;
18 import com.intellij.execution.Location;
19 import com.intellij.execution.TestStateStorage;
20 import com.intellij.openapi.actionSystem.AnAction;
21 import com.intellij.openapi.actionSystem.AnActionEvent;
22 import com.intellij.openapi.actionSystem.CommonDataKeys;
23 import com.intellij.openapi.project.Project;
24 import com.intellij.openapi.ui.popup.ListPopupStep;
25 import com.intellij.ui.popup.list.ListPopupImpl;
26 import com.intellij.util.PsiNavigateUtil;
27 import com.intellij.util.Time;
30 import java.awt.event.ActionEvent;
31 import java.awt.event.KeyEvent;
32 import java.util.Date;
35 public class ShowRecentTests extends AnAction {
36 private static final int TEST_LIMIT = Integer.MAX_VALUE;
38 private static Date getSinceDate() {
39 return new Date(System.currentTimeMillis() - Time.DAY);
43 public void actionPerformed(AnActionEvent e) {
44 final Project project = CommonDataKeys.PROJECT.getData(e.getDataContext());
45 if (project == null) return;
47 Map<String, TestStateStorage.Record> records = TestStateStorage.getInstance(project).getRecentTests(TEST_LIMIT, getSinceDate());
48 RecentTestRunner testRunner = new RecentTestRunnerImpl(project);
50 SelectTestStep selectStepTest = new SelectTestStep(records, testRunner);
51 RecentTestsListPopup popup = new RecentTestsListPopup(selectStepTest, testRunner);
52 popup.showCenteredInCurrentWindow(project);
56 class RecentTestsListPopup extends ListPopupImpl {
57 private final RecentTestRunner myTestRunner;
59 public RecentTestsListPopup(ListPopupStep<String> popupStep, RecentTestRunner testRunner) {
61 myTestRunner = testRunner;
63 registerActions(this);
66 private void registerActions(ListPopupImpl popup) {
67 popup.registerAction("alternate", KeyStroke.getKeyStroke("shift pressed SHIFT"), new AbstractAction() {
69 public void actionPerformed(ActionEvent e) {
73 popup.registerAction("restoreDefault", KeyStroke.getKeyStroke("released SHIFT"), new AbstractAction() {
75 public void actionPerformed(ActionEvent e) {
79 popup.registerAction("invokeAction", KeyStroke.getKeyStroke("shift ENTER"), new AbstractAction() {
81 public void actionPerformed(ActionEvent e) {
85 popup.registerAction("navigate", KeyStroke.getKeyStroke(KeyEvent.VK_F4, 0), new AbstractAction() {
87 public void actionPerformed(ActionEvent e) {
88 Object[] values = getSelectedValues();
89 if (values.length == 1) {
90 Location location = myTestRunner.getLocation(values[0].toString());
91 if (location != null) {
93 PsiNavigateUtil.navigate(location.getPsiElement());
100 private void shiftPressed() {
101 setCaption("Run Recent Tests");
102 myTestRunner.setMode(RecentTestRunner.Mode.RUN);
105 private void shiftReleased() {
106 setCaption("Debug Recent Tests");
107 myTestRunner.setMode(RecentTestRunner.Mode.DEBUG);