transcendental-lisp/test/terminal/VirtualTerminalInteractor.java

171 lines
5.2 KiB
Java
Raw Normal View History

2017-03-23 16:14:26 -04:00
package terminal;
import static org.junit.Assert.*;
import static terminal.LispTerminal.END_OF_SEGMENT;
import java.io.*;
import com.googlecode.lanterna.*;
import com.googlecode.lanterna.input.*;
import com.googlecode.lanterna.terminal.virtual.*;
import stream.UncheckedIOException;
public class VirtualTerminalInteractor {
private PipedOutputStream inputWriter;
2017-03-23 18:48:37 -04:00
private PipedInputStream inputReader;
2017-03-23 16:14:26 -04:00
private PipedOutputStream outputWriter;
2017-03-23 18:48:37 -04:00
private PipedInputStream outputReader;
private VirtualTerminal virtualTerminal;
private FlushListener flushListener;
2017-03-23 16:14:26 -04:00
private LispTerminal lispTerminal;
2017-03-23 18:48:37 -04:00
private TerminalConfiguration configuration;
2017-03-23 16:14:26 -04:00
public VirtualTerminalInteractor() {
2017-03-23 18:48:37 -04:00
this.inputWriter = new PipedOutputStream();
this.inputReader = new PipedInputStream();
this.outputWriter = new PipedOutputStream();
this.outputReader = new PipedInputStream();
this.virtualTerminal = new DefaultVirtualTerminal();
this.flushListener = new FlushListener();
this.virtualTerminal.addVirtualTerminalListener(flushListener);
this.lispTerminal = new LispTerminal(createTerminalConfiguration());
}
private TerminalConfiguration createTerminalConfiguration() {
configuration = new TerminalConfiguration();
configuration.setInputPair(inputWriter, inputReader);
configuration.setOutputPair(outputWriter, outputReader);
configuration.setTerminal(virtualTerminal);
return configuration;
}
public TerminalConfiguration getConfiguration() {
return configuration;
2017-03-23 16:14:26 -04:00
}
public void start() {
lispTerminal.start();
}
public void stop() {
try {
lispTerminal.stop();
outputWriter.close();
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
public void pressKey(KeyType keyType) {
virtualTerminal.addInput(new KeyStroke(keyType));
waitForFlushes(1);
}
public void pressKeyTimes(KeyType keyType, int times) {
for (int i = times; i > 0; i--)
virtualTerminal.addInput(new KeyStroke(keyType));
waitForFlushes(times);
}
public void pressControlKey(KeyType keyType) {
virtualTerminal.addInput(new KeyStroke(keyType, true, false));
waitForFlushes(1);
}
public void enterCharacter(char character) {
virtualTerminal.addInput(new KeyStroke(character, false, false));
waitForFlushes(1);
}
public void enterControlCharacter(char character) {
virtualTerminal.addInput(new KeyStroke(character, true, false));
waitForFlushes(1);
}
public void enterCharacters(String characters) {
for (char c : characters.toCharArray())
virtualTerminal.addInput(new KeyStroke(c, false, false));
waitForFlushes(characters.length());
}
public void produceOutput(String output) {
try {
for (char c : output.toCharArray())
outputWriter.write(c);
outputWriter.write(END_OF_SEGMENT);
outputWriter.flush();
waitForFlushes(1);
} catch (IOException ignored) {}
}
public void waitForFlushes(int flushCount) {
try {
synchronized (flushListener) {
while (flushListener.getFlushCount() < flushCount)
flushListener.wait();
flushListener.reduceFlushCount(flushCount);
2017-03-23 16:14:26 -04:00
}
} catch (InterruptedException ignored) {}
}
2017-03-23 18:48:37 -04:00
public void waitForPrompt() {
waitForFlushes(1);
}
2017-03-23 16:14:26 -04:00
public void setColumns(int columns) {
int rows = virtualTerminal.getTerminalSize().getRows();
virtualTerminal.setTerminalSize(new TerminalSize(columns, rows));
}
public void setRows(int rows) {
int columns = virtualTerminal.getTerminalSize().getColumns();
virtualTerminal.setTerminalSize(new TerminalSize(columns, rows));
}
public void assertCursorPosition(int column, int row) {
assertEquals(column, virtualTerminal.getCursorPosition().getColumn());
assertEquals(row, virtualTerminal.getCursorPosition().getRow());
}
2017-04-12 09:16:09 -04:00
public void assertScreenText(String... rows) {
for (int row = 0; row < rows.length; row++)
for (int column = 0; column < rows[row].length(); column++)
assertCharacterAtPosition(rows[row].charAt(column), column, row);
}
2017-03-23 16:14:26 -04:00
public void assertCharacterAtPosition(char character, int column, int row) {
TerminalPosition position = new TerminalPosition(column, row);
String expected = String.valueOf(character);
String actual = String.valueOf(virtualTerminal.getCharacter(position).getCharacter());
assertEquals(expected, actual);
}
public void assertInputWritten(String expected) {
String actual = "";
try {
inputWriter.close();
for (int c = inputReader.read(); c != -1; c = inputReader.read())
actual += (char) c;
} catch (IOException ignored) {}
assertEquals(expected, actual);
}
public void assertInputStreamClosed() {
try {
inputWriter.write(0);
fail("input stream not closed");
} catch (IOException ignored) {}
}
}