reorganize answer placeholder actions
[idea/community.git] / python / educational-core / course-creator / testSrc / com / jetbrains / edu / coursecreator / actions / CCAnswerPlaceholderActionTest.java
1 package com.jetbrains.edu.coursecreator.actions;
2
3 import com.intellij.openapi.actionSystem.AnAction;
4 import com.intellij.openapi.actionSystem.Presentation;
5 import com.intellij.openapi.command.undo.UndoManager;
6 import com.intellij.openapi.fileEditor.FileEditorManager;
7 import com.intellij.openapi.project.Project;
8 import com.intellij.openapi.vfs.VirtualFile;
9 import com.jetbrains.edu.coursecreator.CCTestCase;
10 import com.jetbrains.edu.coursecreator.CCTestsUtil;
11 import com.jetbrains.edu.coursecreator.actions.placeholder.CCAddAnswerPlaceholder;
12 import com.jetbrains.edu.coursecreator.actions.placeholder.CCDeleteAnswerPlaceholder;
13 import com.jetbrains.edu.learning.StudyUtils;
14 import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder;
15 import com.jetbrains.edu.learning.courseFormat.TaskFile;
16 import com.jetbrains.edu.learning.ui.CCCreateAnswerPlaceholderDialog;
17
18 import java.util.Collections;
19 import java.util.List;
20
21 public class CCAnswerPlaceholderActionTest extends CCTestCase {
22   static class CCTestAction extends CCAddAnswerPlaceholder {
23     @Override
24     protected CCCreateAnswerPlaceholderDialog createDialog(Project project, AnswerPlaceholder answerPlaceholder) {
25       return new CCCreateAnswerPlaceholderDialog(project, answerPlaceholder.getTaskText(), answerPlaceholder.getHints()) {
26         @Override
27         public boolean showAndGet() {
28           return true;
29         }
30
31         @Override
32         public String getTaskText() {
33           return "type here";
34         }
35
36         @Override
37         public List<String> getHints() {
38           return Collections.singletonList("Test hint");
39         }
40       };
41     }
42   }
43
44   public void testPlaceholderWithSelection() {
45     doTest("onePlaceholder", new CCTestAction());
46   }
47
48   public void testPlaceholderWithoutSelection() {
49     doTest("withoutSelection", new CCTestAction());
50   }
51
52   public void testPlaceholderIntersection() {
53     configureByTaskFile("placeholderIntersection.txt");
54     Presentation presentation = myFixture.testAction(new CCTestAction());
55     assertTrue(presentation.isVisible() && !presentation.isEnabled());
56   }
57
58   public void testPlaceholderDeleted() {
59     doTest("deletePlaceholder", new CCDeleteAnswerPlaceholder());
60   }
61
62   private void doTest(String name, AnAction action) {
63     VirtualFile virtualFile = configureByTaskFile(name + CCTestsUtil.BEFORE_POSTFIX);
64     myFixture.testAction(action);
65     TaskFile taskFile = StudyUtils.getTaskFile(getProject(), virtualFile);
66     checkByFile(taskFile, name + CCTestsUtil.AFTER_POSTFIX, false);
67     checkHighlighters(taskFile, myFixture.getEditor().getMarkupModel());
68     UndoManager.getInstance(getProject()).undo(FileEditorManager.getInstance(getProject()).getSelectedEditor(virtualFile));
69     checkByFile(taskFile, name + CCTestsUtil.BEFORE_POSTFIX, false);
70   }
71
72   @Override
73   protected String getBasePath() {
74     return super.getBasePath()  + "/actions/addPlaceholder";
75   }
76 }