tests for task file actions
[idea/community.git] / python / educational-core / course-creator / testSrc / com / jetbrains / edu / coursecreator / actions / CCTaskFileActionTest.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.command.undo.UndoManager;
6 import com.intellij.openapi.fileEditor.FileEditor;
7 import com.intellij.openapi.fileEditor.FileEditorManager;
8 import com.intellij.openapi.vfs.VirtualFile;
9 import com.intellij.testFramework.MapDataContext;
10 import com.intellij.testFramework.TestActionEvent;
11 import com.jetbrains.edu.coursecreator.CCTestCase;
12 import com.jetbrains.edu.learning.StudyUtils;
13 import com.jetbrains.edu.learning.courseFormat.TaskFile;
14 import org.jetbrains.annotations.NotNull;
15
16 public class CCTaskFileActionTest extends CCTestCase {
17   public void testHideTaskFile() {
18     VirtualFile virtualFile = configureByTaskFile("taskFile.txt");
19     launchAction(virtualFile, new CCHideFromStudent());
20     assertNull(StudyUtils.getTaskFile(getProject(), virtualFile));
21     UndoManager.getInstance(getProject()).undo(FileEditorManager.getInstance(getProject()).getSelectedEditor(virtualFile));
22     TaskFile taskFile = StudyUtils.getTaskFile(getProject(), virtualFile);
23     assertNotNull(taskFile);
24     checkHighlighters(taskFile, myFixture.getEditor().getMarkupModel());
25   }
26
27   public void testAddTaskFile() {
28     VirtualFile virtualFile = copyFileToTask("nonTaskFile.txt");
29     myFixture.configureFromExistingVirtualFile(virtualFile);
30     launchAction(virtualFile, new CCAddAsTaskFile());
31     TaskFile taskFile = StudyUtils.getTaskFile(getProject(), virtualFile);
32     assertNotNull(taskFile);
33     FileEditor fileEditor = FileEditorManager.getInstance(getProject()).getSelectedEditor(virtualFile);
34     UndoManager.getInstance(getProject()).undo(fileEditor);
35     assertNull(StudyUtils.getTaskFile(getProject(), virtualFile));
36   }
37
38   private void launchAction(VirtualFile virtualFile, AnAction action) {
39     TestActionEvent e = getActionEvent(virtualFile, action);
40     action.beforeActionPerformedUpdate(e);
41     assertTrue(e.getPresentation().isEnabled() && e.getPresentation().isVisible());
42     action.actionPerformed(e);
43   }
44
45   @NotNull
46   private TestActionEvent getActionEvent(VirtualFile virtualFile, AnAction action) {
47     MapDataContext context = new MapDataContext();
48     context.put(CommonDataKeys.VIRTUAL_FILE, virtualFile);
49     context.put(CommonDataKeys.PROJECT, getProject());
50     return new TestActionEvent(context, action);
51   }
52
53   @Override
54   protected String getBasePath() {
55     return super.getBasePath() + "/actions/taskFileActions";
56   }
57 }