mirror of
https://gitflic.ru/project/openide/openide.git
synced 2026-09-27 10:03:11 +07:00
IDEA-CR-57893 Move testing code from CreateFileFromTemplateDialog to CreateFromTemplateAction and a separate file.
GitOrigin-RevId: 2eb3dcf8c457d4c800432d2893b717997f33e5c5
This commit is contained in:
committed by
intellij-monorepo-bot
parent
b045ac2b12
commit
2ea07c424a
@@ -2,12 +2,10 @@
|
||||
|
||||
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;
|
||||
@@ -27,7 +25,6 @@ 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.*;
|
||||
@@ -123,17 +120,6 @@ 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);
|
||||
@@ -323,72 +309,6 @@ 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);
|
||||
|
||||
@@ -5,6 +5,7 @@ import com.intellij.CommonBundle;
|
||||
import com.intellij.ide.IdeView;
|
||||
import com.intellij.ide.util.PropertiesComponent;
|
||||
import com.intellij.openapi.actionSystem.*;
|
||||
import com.intellij.openapi.application.ApplicationManager;
|
||||
import com.intellij.openapi.application.WriteActionAware;
|
||||
import com.intellij.openapi.diagnostic.Logger;
|
||||
import com.intellij.openapi.editor.Editor;
|
||||
@@ -48,7 +49,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, dataContext);
|
||||
final CreateFileFromTemplateDialog.Builder builder = createDialogBuilder(project, dataContext);
|
||||
buildDialog(project, dir, builder);
|
||||
|
||||
final Ref<String> selectedTemplateName = Ref.create(null);
|
||||
@@ -85,6 +86,17 @@ public abstract class CreateFromTemplateAction<T extends PsiElement> extends AnA
|
||||
});
|
||||
}
|
||||
|
||||
@SuppressWarnings("TestOnlyProblems")
|
||||
private static CreateFileFromTemplateDialog.Builder createDialogBuilder(Project project, DataContext dataContext) {
|
||||
if (ApplicationManager.getApplication().isUnitTestMode()) {
|
||||
TestDialogBuilder.TestAnswers answers = dataContext.getData(TestDialogBuilder.TestAnswers.KEY);
|
||||
if (answers != null) {
|
||||
return new TestDialogBuilder(answers);
|
||||
}
|
||||
}
|
||||
return CreateFileFromTemplateDialog.createDialog(project);
|
||||
}
|
||||
|
||||
protected void postProcess(T createdElement, String templateName, Map<String,String> customProperties) {
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,85 @@
|
||||
// Copyright 2000-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license that can be found in the LICENSE file.
|
||||
package com.intellij.ide.actions;
|
||||
|
||||
import com.google.common.base.Preconditions;
|
||||
import com.intellij.openapi.actionSystem.AnActionEvent;
|
||||
import com.intellij.openapi.actionSystem.DataKey;
|
||||
import com.intellij.openapi.ui.InputValidator;
|
||||
import com.intellij.psi.PsiElement;
|
||||
import com.intellij.util.Consumer;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
import org.jetbrains.annotations.Nullable;
|
||||
import org.jetbrains.annotations.TestOnly;
|
||||
|
||||
import javax.swing.*;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* To use inside unit-tests, provide an instance of {@link TestDialogBuilder.TestAnswers}
|
||||
* in {@link AnActionEvent#getDataContext()} with the key {@link TestDialogBuilder.TestAnswers#KEY}.
|
||||
*/
|
||||
@TestOnly
|
||||
public class TestDialogBuilder implements CreateFileFromTemplateDialog.Builder {
|
||||
private final TestAnswers myAnswers;
|
||||
private InputValidator myValidator;
|
||||
|
||||
TestDialogBuilder(@NotNull TestDialogBuilder.TestAnswers answers) {
|
||||
myAnswers = answers;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CreateFileFromTemplateDialog.Builder setTitle(String title) {
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CreateFileFromTemplateDialog.Builder setValidator(InputValidator validator) {
|
||||
myValidator = validator;
|
||||
return this;
|
||||
}
|
||||
|
||||
@Override
|
||||
public CreateFileFromTemplateDialog.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 CreateFileFromTemplateDialog.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 CreateFileFromTemplateDialog.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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user