mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-CR-57874: Ability to specify an answer for dialog window of create file/directory action in unit-test mode.
(cherry picked from commit 721651fd0183cd0508f18b61c9661e1cf46ce31c) GitOrigin-RevId: 2df563e4cb24a584a7fe6930f95ffbb496004d34
This commit is contained in:
committed by
intellij-monorepo-bot
parent
7679a4a2e1
commit
67b3c48e4f
@@ -12,6 +12,7 @@ import com.intellij.internal.statistic.eventLog.FeatureUsageData;
|
||||
import com.intellij.internal.statistic.service.fus.collectors.FUCounterUsageLogger;
|
||||
import com.intellij.internal.statistic.utils.StatisticsUtilKt;
|
||||
import com.intellij.openapi.actionSystem.*;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.application.Experiments;
|
||||
import com.intellij.openapi.application.WriteAction;
|
||||
import com.intellij.openapi.extensions.ExtensionPointName;
|
||||
@@ -46,6 +47,7 @@ import com.intellij.util.containers.FList;
|
||||
import com.intellij.util.ui.UIUtil;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.annotations.TestOnly;
|
||||
import org.jetbrains.jps.model.module.JpsModuleSourceRootType;
|
||||
|
||||
import javax.swing.*;
|
||||
@@ -62,6 +64,9 @@ public class CreateDirectoryOrPackageAction extends AnAction implements DumbAwar
|
||||
private static final ExtensionPointName<CreateDirectoryCompletionContributorEP>
|
||||
EP = ExtensionPointName.create("com.intellij.createDirectoryCompletionContributor");
|
||||
|
||||
@TestOnly
|
||||
public static final DataKey<String> TEST_DIRECTORY_NAME_KEY = DataKey.create("CreateDirectoryOrPackageAction.testName");
|
||||
|
||||
public CreateDirectoryOrPackageAction() {
|
||||
super(() -> IdeBundle.message("action.create.new.directory.or.package"),
|
||||
() -> IdeBundle.message("action.create.new.directory.or.package"), null);
|
||||
@@ -99,6 +104,15 @@ public class CreateDirectoryOrPackageAction extends AnAction implements DumbAwar
|
||||
}
|
||||
};
|
||||
|
||||
if (ApplicationManager.getApplication().isUnitTestMode()) {
|
||||
@SuppressWarnings("TestOnlyProblems")
|
||||
String testDirectoryName = event.getData(TEST_DIRECTORY_NAME_KEY);
|
||||
if (testDirectoryName != null && validator.checkInput(testDirectoryName) && validator.canClose(testDirectoryName)) {
|
||||
consumer.accept(Collections.singletonList(validator.getCreatedElement()));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (Experiments.getInstance().isFeatureEnabled("show.create.new.element.in.popup")) {
|
||||
createLightWeightPopup(title, initialText, directory, validator, consumer).showCenteredInCurrentWindow(project);
|
||||
}
|
||||
|
||||
@@ -2,9 +2,13 @@
|
||||
|
||||
package com.intellij.ide.actions;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import com.intellij.ide.actions.newclass.CreateWithTemplatesDialogPanel;
|
||||
import com.intellij.ide.ui.newItemPopup.NewItemPopupUtil;
|
||||
import com.intellij.lang.LangBundle;
|
||||
import com.intellij.openapi.actionSystem.DataContext;
|
||||
import com.intellij.openapi.actionSystem.DataKey;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.application.Experiments;
|
||||
import com.intellij.openapi.project.Project;
|
||||
import com.intellij.openapi.ui.DialogWrapper;
|
||||
@@ -23,12 +27,10 @@ import com.intellij.util.Consumer;
|
||||
import com.intellij.util.PlatformIcons;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.annotations.TestOnly;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import java.util.*;
|
||||
|
||||
/**
|
||||
* @author peter
|
||||
@@ -121,6 +123,17 @@ public class CreateFileFromTemplateDialog extends DialogWrapper {
|
||||
myUpDownHint.setVisible(flag);
|
||||
}
|
||||
|
||||
public static Builder createDialog(@NotNull final Project project, @NotNull final DataContext dataContext) {
|
||||
if (ApplicationManager.getApplication().isUnitTestMode()) {
|
||||
@SuppressWarnings("TestOnlyProblems")
|
||||
TestAnswers answers = dataContext.getData(TestAnswers.KEY);
|
||||
if (answers != null) {
|
||||
return new TestDialogBuilder(answers);
|
||||
}
|
||||
}
|
||||
return createDialog(project);
|
||||
}
|
||||
|
||||
public static Builder createDialog(@NotNull final Project project) {
|
||||
if (Experiments.getInstance().isFeatureEnabled("show.create.new.element.in.popup")) {
|
||||
return new NonBlockingPopupBuilderImpl(project);
|
||||
@@ -310,6 +323,72 @@ public class CreateFileFromTemplateDialog extends DialogWrapper {
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("TestOnlyProblems")
|
||||
private static class TestDialogBuilder implements Builder {
|
||||
private final TestAnswers myAnswers;
|
||||
private InputValidator myValidator;
|
||||
|
||||
TestDialogBuilder(@NotNull TestAnswers answers) {
|
||||
myAnswers = answers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder setTitle(String title) {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder setValidator(InputValidator validator) {
|
||||
myValidator = validator;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Builder addKind(@NotNull String kind, @Nullable Icon icon, @NotNull String templateName) {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public @Nullable Map<String, String> getCustomProperties() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T extends PsiElement> @Nullable T show(@NotNull String errorTitle,
|
||||
@Nullable String selectedItem,
|
||||
@NotNull FileCreator<T> creator) {
|
||||
if (myValidator != null) {
|
||||
Preconditions.checkState(myValidator.checkInput(myAnswers.myName), "The answer '%s' is not valid.", myAnswers.myName);
|
||||
Preconditions.checkState(myValidator.canClose(myAnswers.myName), "Can't close dialog with the answer '%s'.", myAnswers.myName);
|
||||
}
|
||||
if (myAnswers.myName != null && myAnswers.myTemplateName != null) {
|
||||
return creator.createFile(myAnswers.myName, myAnswers.myTemplateName);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public <T extends PsiElement> void show(@NotNull String errorTitle,
|
||||
@Nullable String selectedItem,
|
||||
@NotNull FileCreator<T> creator,
|
||||
Consumer<? super T> elementConsumer) {
|
||||
elementConsumer.consume(show(errorTitle, selectedItem, creator));
|
||||
}
|
||||
}
|
||||
|
||||
@TestOnly
|
||||
public static class TestAnswers {
|
||||
public static final DataKey<TestAnswers> KEY = DataKey.create("CreateFileFromTemplateDialog.TestDataContext");
|
||||
|
||||
private final String myName;
|
||||
private final String myTemplateName;
|
||||
|
||||
public TestAnswers(@Nullable String name, @Nullable String templateName) {
|
||||
myName = name;
|
||||
myTemplateName = templateName;
|
||||
}
|
||||
}
|
||||
|
||||
public interface Builder {
|
||||
Builder setTitle(String title);
|
||||
Builder setValidator(InputValidator validator);
|
||||
|
||||
@@ -48,7 +48,7 @@ public abstract class CreateFromTemplateAction<T extends PsiElement> extends AnA
|
||||
final PsiDirectory dir = view.getOrChooseDirectory();
|
||||
if (dir == null || project == null) return;
|
||||
|
||||
final CreateFileFromTemplateDialog.Builder builder = CreateFileFromTemplateDialog.createDialog(project);
|
||||
final CreateFileFromTemplateDialog.Builder builder = CreateFileFromTemplateDialog.createDialog(project, dataContext);
|
||||
buildDialog(project, dir, builder);
|
||||
|
||||
final Ref<String> selectedTemplateName = Ref.create(null);
|
||||
|
||||
Reference in New Issue
Block a user