Cleanup: NotNull/Nullable
[idea/community.git] / java / compiler / tests / com / intellij / compiler / artifacts / ui / ArtifactEditorTestCase.java
1 package com.intellij.compiler.artifacts.ui;
2
3 import com.intellij.compiler.artifacts.PackagingElementsTestCase;
4 import com.intellij.openapi.application.ApplicationManager;
5 import com.intellij.openapi.roots.ui.configuration.artifacts.*;
6 import com.intellij.openapi.roots.ui.configuration.artifacts.nodes.ComplexPackagingElementNode;
7 import com.intellij.openapi.roots.ui.configuration.artifacts.nodes.PackagingElementNode;
8 import com.intellij.openapi.ui.Messages;
9 import com.intellij.openapi.ui.TestDialog;
10 import com.intellij.openapi.util.Ref;
11 import com.intellij.packaging.artifacts.Artifact;
12 import com.intellij.packaging.elements.PackagingElementFactory;
13 import com.intellij.util.PathUtil;
14
15 import java.util.Arrays;
16 import java.util.Collections;
17
18 /**
19  * @author nik
20  */
21 public abstract class ArtifactEditorTestCase extends PackagingElementsTestCase {
22   protected ArtifactEditorImpl myArtifactEditor;
23
24   @Override
25   protected void tearDown() throws Exception {
26     myArtifactEditor = null;
27     super.tearDown();
28   }
29
30   protected void createEditor(Artifact artifact) {
31     createEditor(artifact, false);
32   }
33
34   protected void showContent(String path) {
35     selectNode(path);
36     final PackagingElementNode<?> node = myArtifactEditor.getLayoutTreeComponent().getSelection().getNodeIfSingle();
37     myArtifactEditor.getSubstitutionParameters().setShowContent(assertInstanceOf(node, ComplexPackagingElementNode.class));
38     myArtifactEditor.rebuildTries();
39   }
40
41   protected void createEditor(Artifact artifact, final boolean showContent) {
42     final ArtifactEditorSettings settings;
43     if (showContent) {
44       settings = new ArtifactEditorSettings(false, Arrays.asList(PackagingElementFactory.getInstance().getComplexElementTypes()));
45     }
46     else {
47       settings = new ArtifactEditorSettings(false, Collections.emptyList());
48     }
49
50     myArtifactEditor = new ArtifactEditorImpl(new MockArtifactsStructureConfigurableContext(), artifact, settings) {
51       @Override
52       protected ArtifactEditorContextImpl createArtifactEditorContext(ArtifactsStructureConfigurableContext parentContext) {
53         return new MockPackagingEditorContext(parentContext, this);
54       }
55     };
56     myArtifactEditor.createMainComponent();
57     disposeOnTearDown(myArtifactEditor);
58   }
59
60   protected void selectNode(String path) {
61     final LayoutTreeComponent layoutTreeComponent = myArtifactEditor.getLayoutTreeComponent();
62     layoutTreeComponent.getLayoutTree().clearSelection();
63     layoutTreeComponent.selectNode(PathUtil.getParentPath(path), PathUtil.getFileName(path));
64     assertFalse("Node " + path + " not found", layoutTreeComponent.getSelection().getNodes().isEmpty());
65   }
66
67   protected void assertLayout(String expected) {
68     assertLayout(myArtifactEditor.getArtifact(), expected);
69   }
70
71   protected void applyChanges() {
72     ApplicationManager.getApplication().runWriteAction(() -> {
73       myArtifactEditor.apply();
74       ((MockArtifactsStructureConfigurableContext)myArtifactEditor.getContext().getParent()).commitModel();
75     });
76   }
77
78   protected static void runAction(final Runnable action, boolean confirmationExpected) {
79     final Ref<Boolean> dialogShown = Ref.create(false);
80     final TestDialog oldDialog = Messages.setTestDialog(new TestDialog() {
81       @Override
82       public int show(String message) {
83         dialogShown.set(true);
84         return 0;
85       }
86     });
87
88     try {
89       action.run();
90     }
91     finally {
92       Messages.setTestDialog(oldDialog);
93     }
94
95     if (confirmationExpected) {
96       assertTrue("Dialog wasn't shown", dialogShown.get());
97     }
98     else {
99       assertFalse("Dialog was shown", dialogShown.get());
100     }
101   }
102 }