package com.jediterm;

import com.jediterm.terminal.ArrayTerminalDataStream;
import com.jediterm.terminal.Terminal;
import com.jediterm.terminal.display.BackBuffer;
import com.jediterm.terminal.display.LinesBuffer;
import com.jediterm.terminal.display.StyleState;
import com.jediterm.terminal.emulator.Emulator;
import com.jediterm.terminal.emulator.JediEmulator;
import com.jediterm.util.BackBufferTerminal;
import com.jediterm.util.FileUtil;
import com.jediterm.util.NullTerminalOutputStream;
import junit.framework.TestCase;
import testData.TestPathsManager;

import java.io.File;
import java.io.IOException;

/**
 * @author traff
 */
public class EmulatorTest extends TestCase {
  public void testSetCursorPosition() throws IOException {
    doTest(3, 4, "X00\n" +  //X wins
                 "0X \n" +
                 "X X\n" +
                 "   \n");
  }

  //public void testTooLargeScrollRegion() throws IOException { TODO: this test fails on Travis somehow
  //  doTest(80, 24);
  //}

  public void testMidnightCommanderOnVT100() throws IOException { 
    doTest(80, 24);
  }

  public void testMidnightCommanderOnXTerm() throws IOException {
    doTest(80, 24);
  }

  private void doTest(int width, int height) throws IOException {
    doTest(width, height, FileUtil.loadFileLines(new File(TestPathsManager.getTestDataPath() + getName() + ".after.txt")));
  }

  private void doTest(int width, int height, String expected) throws IOException {
    StyleState state = new StyleState();

    LinesBuffer scrollBuffer = new LinesBuffer();

    BackBuffer backBuffer = new BackBuffer(width, height, state, scrollBuffer);

    Terminal terminal = new BackBufferTerminal(backBuffer, state);

    ArrayTerminalDataStream
      fileStream = new ArrayTerminalDataStream(FileUtil.loadFileText(new File(TestPathsManager.getTestDataPath() + getName() + ".txt"),
                                                                     "UTF-8"));

    Emulator emulator = new JediEmulator(fileStream, new NullTerminalOutputStream(), terminal);

    while (emulator.hasNext()) {
      emulator.next();
    }

    assertEquals(expected, backBuffer.getLines());
  }
}

@@ -101,32 +101,72 @@
 minal.Terminal;%0A
+import com.jediterm.terminal.TextStyle;%0A
 import com.jedit
@@ -297,16 +297,68 @@
 eState;%0A
+import com.jediterm.terminal.emulator.ColorPalette;%0A
 import c
@@ -648,16 +648,35 @@
 nager;%0A%0A
+import java.awt.*;%0A
 import j
@@ -1167,17 +1167,16 @@
 eption %7B
- 
 %0A    doT
@@ -1171,38 +1171,32 @@
 on %7B%0A    doTest(
-80, 24
 );%0A  %7D%0A%0A  public
@@ -1261,43 +1261,688 @@
     
-doTest(80, 24);%0A  %7D%0A%0A  private void
+BackBuffer backBuffer = doTest();%0A%0A    assertColor(backBuffer.getStyleAt(8, 2), ColorPalette.getIndexedColor(3), ColorPalette.getIndexedColor(4));%0A    assertColor(backBuffer.getStyleAt(23, 4), ColorPalette.getIndexedColor(7), ColorPalette.getIndexedColor(4));%0A    assertColor(backBuffer.getStyleAt(2, 0), ColorPalette.getIndexedColor(0), ColorPalette.getIndexedColor(6));%0A  %7D%0A%0A  private static void assertColor(TextStyle style, Color foreground, Color background) %7B%0A    assertEquals(foreground, style.getForeground());%0A    assertEquals(background, style.getBackground());%0A  %7D%0A%0A  private BackBuffer doTest() throws IOException %7B%0A    return doTest(80, 24);%0A  %7D%0A%0A  private BackBuffer
  doT
@@ -1989,24 +1989,31 @@
 ption %7B%0A    
+return 
 doTest(width
@@ -2135,20 +2135,26 @@
 private 
-void
+BackBuffer
  doTest(
@@ -2908,18 +2908,46 @@
 etLines());%0A
+    %0A    return backBuffer;%0A
   %7D%0A%7D%0A

Test colors in MC under xterm