use another fixture as base class for course creator tests
[idea/community.git] / python / educational-core / course-creator / testSrc / com / jetbrains / edu / coursecreator / actions / CCShowPreviewTest.java
1 package com.jetbrains.edu.coursecreator.actions;
2
3 import com.intellij.openapi.actionSystem.AnAction;
4 import com.intellij.openapi.actionSystem.CommonDataKeys;
5 import com.intellij.openapi.actionSystem.LangDataKeys;
6 import com.intellij.openapi.editor.Document;
7 import com.intellij.openapi.editor.Editor;
8 import com.intellij.openapi.editor.EditorFactory;
9 import com.intellij.openapi.util.Pair;
10 import com.intellij.openapi.vfs.VirtualFile;
11 import com.intellij.psi.PsiFile;
12 import com.intellij.psi.PsiManager;
13 import com.intellij.testFramework.MapDataContext;
14 import com.intellij.testFramework.TestActionEvent;
15 import com.jetbrains.edu.coursecreator.CCTestCase;
16 import com.jetbrains.edu.coursecreator.CCTestsUtil;
17 import com.jetbrains.edu.learning.courseFormat.AnswerPlaceholder;
18
19 import java.util.List;
20
21 public class CCShowPreviewTest extends CCTestCase {
22
23   public void testPreviewUnavailable() {
24     VirtualFile file = configureByTaskFile("noplaceholders.txt");
25     CCShowPreview action = new CCShowPreview();
26     TestActionEvent e = getActionEvent(action, PsiManager.getInstance(getProject()).findFile(file));
27     action.beforeActionPerformedUpdate(e);
28     assertTrue(e.getPresentation().isEnabled() && e.getPresentation().isVisible());
29     try {
30       action.actionPerformed(e);
31       assertTrue("No message shown", false);
32     } catch (RuntimeException ex) {
33       assertEquals(CCShowPreview.NO_PREVIEW_MESSAGE, ex.getMessage());
34     }
35   }
36
37   public void testOnePlaceholder() {
38     doTest("test");
39   }
40
41   public void testSeveralPlaceholders() {
42     doTest("several");
43   }
44
45   private void doTest(String name) {
46     VirtualFile file = configureByTaskFile(name + CCTestsUtil.BEFORE_POSTFIX);
47     CCShowPreview action = new CCShowPreview();
48     TestActionEvent e = getActionEvent(action,PsiManager.getInstance(getProject()).findFile(file));
49     action.beforeActionPerformedUpdate(e);
50     assertTrue(e.getPresentation().isEnabled() && e.getPresentation().isVisible());
51     action.actionPerformed(e);
52     Editor editor = EditorFactory.getInstance().getAllEditors()[1];
53     Pair<Document, List<AnswerPlaceholder>> pair = getPlaceholders(name + CCTestsUtil.AFTER_POSTFIX);
54     assertEquals("Files don't match", editor.getDocument().getText(), pair.getFirst().getText());
55     for (AnswerPlaceholder placeholder : pair.getSecond()) {
56       assertNotNull("No highlighter for placeholder:" + CCTestsUtil.getPlaceholderPresentation(placeholder), getHighlighter(editor.getMarkupModel(), placeholder));
57     }
58   }
59
60   @Override
61   protected String getBasePath() {
62     return super.getBasePath() + "/actions/preview";
63   }
64
65   TestActionEvent getActionEvent(AnAction action, PsiFile psiFile) {
66     MapDataContext context = new MapDataContext();
67     context.put(CommonDataKeys.PSI_FILE, psiFile);
68     context.put(CommonDataKeys.PROJECT, getProject());
69     context.put(LangDataKeys.MODULE, myFixture.getModule());
70     return new TestActionEvent(context, action);
71   }
72 }