2 * Copyright 2000-2014 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package com.intellij.execution.impl;
18 import com.intellij.execution.process.ProcessHandler;
19 import com.intellij.execution.ui.ConsoleViewContentType;
20 import com.intellij.ide.DataManager;
21 import com.intellij.openapi.actionSystem.DataContext;
22 import com.intellij.openapi.editor.Editor;
23 import com.intellij.openapi.editor.actionSystem.EditorActionManager;
24 import com.intellij.openapi.editor.actionSystem.TypedAction;
25 import com.intellij.openapi.project.Project;
26 import com.intellij.openapi.util.Disposer;
27 import com.intellij.psi.search.GlobalSearchScope;
28 import com.intellij.testFramework.LightPlatformTestCase;
29 import com.intellij.testFramework.TestDataProvider;
30 import com.intellij.util.Alarm;
31 import com.intellij.util.SystemProperties;
32 import com.intellij.util.TimeoutUtil;
33 import com.intellij.util.ui.UIUtil;
34 import org.jetbrains.annotations.NotNull;
35 import org.jetbrains.annotations.Nullable;
37 import java.io.OutputStream;
38 import java.util.concurrent.CountDownLatch;
40 public class ConsoleViewImplTest extends LightPlatformTestCase {
42 private ConsoleViewImpl myConsole;
45 public void setUp() throws Exception {
47 myConsole = createConsole();
51 public void tearDown() throws Exception {
53 Disposer.dispose(myConsole);
60 public void testTypeText() throws Exception {
61 ConsoleViewImpl console = myConsole;
62 console.print("Initial", ConsoleViewContentType.NORMAL_OUTPUT);
63 console.flushDeferredText();
65 console.print("Hi", ConsoleViewContentType.NORMAL_OUTPUT);
66 assertEquals(2, console.getContentSize());
69 public void testDoubleClear() throws Exception {
70 ConsoleViewImpl console = myConsole;
71 Alarm alarm = new Alarm(Alarm.ThreadToUse.SHARED_THREAD);
72 CountDownLatch latch = new CountDownLatch(3);
73 alarm.addRequest(() -> {
77 alarm.addRequest(() -> {
81 alarm.addRequest(() -> {
82 console.print("Test", ConsoleViewContentType.NORMAL_OUTPUT);
86 UIUtil.dispatchAllInvocationEvents();
87 TimeoutUtil.sleep(SystemProperties.getIntProperty("console.flush.delay.ms", 200));
88 UIUtil.dispatchAllInvocationEvents();
89 assertFalse(console.hasDeferredOutput());
90 //assertEquals("Test", console.getText());
93 public void testTypeInEmptyConsole() throws Exception {
94 ConsoleViewImpl console = myConsole;
96 EditorActionManager actionManager = EditorActionManager.getInstance();
97 final DataContext dataContext = DataManager.getInstance().getDataContext();
98 TypedAction action = actionManager.getTypedAction();
99 action.actionPerformed(console.getEditor(), 'h', dataContext);
100 assertEquals(1, console.getContentSize());
103 public void testTypingAfterMultipleCR() throws Exception {
104 final EditorActionManager actionManager = EditorActionManager.getInstance();
105 final TypedAction typedAction = actionManager.getTypedAction();
106 final TestDataProvider dataContext = new TestDataProvider(getProject());
108 final ConsoleViewImpl console = myConsole;
109 final Editor editor = console.getEditor();
110 console.print("System output\n", ConsoleViewContentType.SYSTEM_OUTPUT);
111 console.print("\r\r\r\r\r\r\r", ConsoleViewContentType.NORMAL_OUTPUT);
112 console.flushDeferredText();
114 typedAction.actionPerformed(editor, '1', dataContext);
115 typedAction.actionPerformed(editor, '2', dataContext);
117 assertEquals("System output\n12", editor.getDocument().getText());
121 private static ConsoleViewImpl createConsole() {
122 Project project = getProject();
123 ConsoleViewImpl console = new ConsoleViewImpl(project,
124 GlobalSearchScope.allScope(project),
127 console.getComponent();
128 ProcessHandler processHandler = new MyProcessHandler();
129 processHandler.startNotify();
130 console.attachToProcess(processHandler);
134 private static class MyProcessHandler extends ProcessHandler {
136 protected void destroyProcessImpl() {
137 notifyProcessTerminated(0);
141 protected void detachProcessImpl() {
145 public boolean detachIsDefault() {
151 public OutputStream getProcessInput() {