diff --git a/terminal/org.eclipse.tm.terminal.test/META-INF/MANIFEST.MF b/terminal/org.eclipse.tm.terminal.test/META-INF/MANIFEST.MF index df2c0fd4454..78f2be856a3 100644 --- a/terminal/org.eclipse.tm.terminal.test/META-INF/MANIFEST.MF +++ b/terminal/org.eclipse.tm.terminal.test/META-INF/MANIFEST.MF @@ -6,5 +6,7 @@ Bundle-Version: 1.0.0.qualifier Bundle-Vendor: %providerName Bundle-Localization: plugin Require-Bundle: org.junit, - org.eclipse.tm.terminal + org.eclipse.tm.terminal, + org.eclipse.swt, + org.eclipse.jface Bundle-RequiredExecutionEnvironment: J2SE-1.4 diff --git a/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/emulator/VT100EmulatorBackendTest.java b/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/emulator/VT100EmulatorBackendTest.java new file mode 100644 index 00000000000..a98a351c2ea --- /dev/null +++ b/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/emulator/VT100EmulatorBackendTest.java @@ -0,0 +1,1220 @@ +/******************************************************************************* + * Copyright (c) 2007 Wind River Systems, Inc. and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Michael Scharf (Wind River) - initial API and implementation + *******************************************************************************/ +package org.eclipse.tm.internal.terminal.emulator; + +import junit.framework.TestCase; + +import org.eclipse.tm.internal.terminal.model.TerminalTextDataStore; +import org.eclipse.tm.internal.terminal.model.TerminalTextTestHelper; +import org.eclipse.tm.terminal.model.ITerminalTextData; +import org.eclipse.tm.terminal.model.ITerminalTextDataReadOnly; +import org.eclipse.tm.terminal.model.Style; + +public class VT100EmulatorBackendTest extends TestCase { + + protected IVT100EmulatorBackend makeBakend(ITerminalTextData term) { + return new VT100EmulatorBackend(term); + } + + protected ITerminalTextData makeITerminalTextData() { + return new TerminalTextDataStore(); + } + + protected String toSimple(ITerminalTextData term) { + return TerminalTextTestHelper.toSimple(term); + } + protected String toMultiLineText(ITerminalTextDataReadOnly term) { + return TerminalTextTestHelper.toMultiLineText(term); + } + + protected void fill(ITerminalTextData term, String s) { + TerminalTextTestHelper.fill(term,s); + } + + protected void fillSimple(ITerminalTextData term, String s) { + TerminalTextTestHelper.fillSimple(term, s); + } + + + /** + * Used for multi line text + * @param expected + * @param actual + */ + protected void assertEqualsTerm(String expected,String actual) { + assertEquals(expected.replaceAll(" ", "."), actual.replaceAll("\000", ".")); + } + /** + * Used for simple text + * @param expected + * @param actual + */ + protected void assertEqualsSimple(String expected,String actual) { + assertEquals(-1,actual.indexOf('\n')); + assertEquals(expected, actual); + } + public void testClearAll() { + ITerminalTextData term=makeITerminalTextData(); + IVT100EmulatorBackend vt100=makeBakend(term); + vt100.setDimensions(3, 4); + fill(term, "0000\n" + + "1111\n" + + "2222\n" + + "3333\n" + + "4444\n" + + "5555"); + vt100.clearAll(); + assertEqualsTerm( + " \n" + + " \n" + + " ",toMultiLineText(term)); + } + public void testSetDimensions() { + ITerminalTextData term=makeITerminalTextData(); + IVT100EmulatorBackend vt100=makeBakend(term); + String s = "0000\n" + + "1111\n" + + "2222\n" + + "3333\n" + + "4444\n" + + "5555"; + fill(term, s); + vt100.setDimensions(3, 4); + assertEquals(3,vt100.getLines()); + assertEquals(4,vt100.getColumns()); + assertEqualsTerm(s,toMultiLineText(term)); + + vt100.setCursor(0, 2); + vt100.setDimensions(2, 4); + assertEquals(0, vt100.getCursorLine()); + assertEquals(2, vt100.getCursorColumn()); + + vt100.setCursor(0, 2); + vt100.setDimensions(5, 4); + assertEquals(3, vt100.getCursorLine()); + assertEquals(2, vt100.getCursorColumn()); + + assertEqualsTerm(s,toMultiLineText(term)); + + vt100.setCursor(0, 3); + vt100.setDimensions(5, 2); + assertEquals(0, vt100.getCursorLine()); + assertEquals(1, vt100.getCursorColumn()); + } + + public void testToAbsoluteLine() { + ITerminalTextData term=makeITerminalTextData(); + VT100EmulatorBackend vt100=new VT100EmulatorBackend(term); + vt100.setDimensions(2, 3); + assertEquals(vt100.toAbsoluteLine(0),0); + // TODO + term=makeITerminalTextData(); + vt100=new VT100EmulatorBackend(term); + vt100.setDimensions(1, 10); + assertEquals(vt100.toAbsoluteLine(0),0); + } + + public void testInsertCharacters() { + ITerminalTextData term=makeITerminalTextData(); + IVT100EmulatorBackend vt100=makeBakend(term); + vt100.setDimensions(3, 4); + String s = + "aaaa\n" + + "bbbb\n" + + "cccc\n" + + "dddd\n" + + "eeee\n" + + "ffff\n" + + "1234\n" + + "4567\n" + + "9012"; + fill(term, s); + vt100.setCursor(0, 0); + vt100.insertCharacters(1); + assertEqualsTerm("aaaa\n" + + "bbbb\n" + + "cccc\n" + + "dddd\n" + + "eeee\n" + + "ffff\n" + + " 123\n" + + "4567\n" + + "9012",toMultiLineText(term)); + + fill(term, s); + vt100.setCursor(1, 1); + vt100.insertCharacters(1); + assertEqualsTerm("aaaa\n" + + "bbbb\n" + + "cccc\n" + + "dddd\n" + + "eeee\n" + + "ffff\n" + + "1234\n" + + "4 56\n" + + "9012",toMultiLineText(term)); + + fill(term, s); + vt100.setCursor(1, 1); + vt100.insertCharacters(2); + assertEqualsTerm("aaaa\n" + + "bbbb\n" + + "cccc\n" + + "dddd\n" + + "eeee\n" + + "ffff\n" + + "1234\n" + + "4 5\n" + + "9012",toMultiLineText(term)); + + vt100.setDimensions(1, 10); + fill(term, "0123456789"); + vt100.setCursor(0, 0); + vt100.insertCharacters(10); + assertEqualsTerm(" ",toMultiLineText(term)); + + vt100.setDimensions(1, 10); + fill(term, "0123456789"); + vt100.setCursor(0, 0); + vt100.insertCharacters(14); + assertEqualsTerm(" ",toMultiLineText(term)); + + vt100.setDimensions(1, 10); + fill(term, "0123456789"); + vt100.setCursor(0, 3); + vt100.insertCharacters(14); + assertEqualsTerm("012 ",toMultiLineText(term)); + + vt100.setDimensions(1, 10); + fill(term, "0123456789"); + vt100.setCursor(0, 3); + vt100.insertCharacters(0); + assertEqualsTerm("0123456789",toMultiLineText(term)); + + vt100.setDimensions(1, 10); + fill(term, "0123456789"); + vt100.setCursor(0, 3); + vt100.insertCharacters(2); + assertEqualsTerm("012 34567",toMultiLineText(term)); + } + + public void testEraseToEndOfScreen() { + ITerminalTextData term=makeITerminalTextData(); + IVT100EmulatorBackend vt100=makeBakend(term); + vt100.setDimensions(3, 4); + String s = + "aaaa\n" + + "bbbb\n" + + "cccc\n" + + "dddd\n" + + "eeee\n" + + "ffff\n" + + "0123\n" + + "4567\n" + + "8901"; + fill(term, s); + vt100.setCursor(0, 0); + vt100.eraseToEndOfScreen(); + assertEquals(0,vt100.getCursorLine()); + assertEquals(0,vt100.getCursorColumn()); + assertEqualsTerm("aaaa\n" + + "bbbb\n" + + "cccc\n" + + "dddd\n" + + "eeee\n" + + "ffff\n" + + " \n" + + " \n" + + " ",toMultiLineText(term)); + + fill(term, s); + vt100.setCursor(1, 0); + vt100.eraseToEndOfScreen(); + assertEquals(1,vt100.getCursorLine()); + assertEquals(0,vt100.getCursorColumn()); + assertEqualsTerm("aaaa\n" + + "bbbb\n" + + "cccc\n" + + "dddd\n" + + "eeee\n" + + "ffff\n" + + "0123\n" + + " \n" + + " ",toMultiLineText(term)); + + fill(term, s); + vt100.setCursor(1, 1); + vt100.eraseToEndOfScreen(); + assertEquals(1,vt100.getCursorLine()); + assertEquals(1,vt100.getCursorColumn()); + assertEqualsTerm("aaaa\n" + + "bbbb\n" + + "cccc\n" + + "dddd\n" + + "eeee\n" + + "ffff\n" + + "0123\n" + + "4 \n" + + " ",toMultiLineText(term)); + + fill(term, s); + vt100.setCursor(1, 4); + assertEquals(1,vt100.getCursorLine()); + assertEquals(3,vt100.getCursorColumn()); + vt100.eraseToEndOfScreen(); + assertEquals(1,vt100.getCursorLine()); + assertEquals(3,vt100.getCursorColumn()); + assertEqualsTerm("aaaa\n" + + "bbbb\n" + + "cccc\n" + + "dddd\n" + + "eeee\n" + + "ffff\n" + + "0123\n" + + "456.\n" + + " ",toMultiLineText(term)); + + + fill(term, s); + vt100.setCursor(1, 5); + vt100.eraseToEndOfScreen(); + assertEquals(1,vt100.getCursorLine()); + assertEquals(3,vt100.getCursorColumn()); + assertEqualsTerm("aaaa\n" + + "bbbb\n" + + "cccc\n" + + "dddd\n" + + "eeee\n" + + "ffff\n" + + "0123\n" + + "456.\n" + + " ",toMultiLineText(term)); + + fill(term, s); + vt100.setCursor(2, 3); + vt100.eraseToEndOfScreen(); + assertEquals(2,vt100.getCursorLine()); + assertEquals(3,vt100.getCursorColumn()); + assertEqualsTerm("aaaa\n" + + "bbbb\n" + + "cccc\n" + + "dddd\n" + + "eeee\n" + + "ffff\n" + + "0123\n" + + "4567\n" + + "890 ",toMultiLineText(term)); + + fill(term, s); + vt100.setCursor(2, 5); + vt100.eraseToEndOfScreen(); + assertEquals(2,vt100.getCursorLine()); + assertEquals(3,vt100.getCursorColumn()); + assertEqualsTerm("aaaa\n" + + "bbbb\n" + + "cccc\n" + + "dddd\n" + + "eeee\n" + + "ffff\n" + + "0123\n" + + "4567\n" + + "890.",toMultiLineText(term)); + } + + public void testEraseToCursor() { + ITerminalTextData term=makeITerminalTextData(); + IVT100EmulatorBackend vt100=makeBakend(term); + vt100.setDimensions(3, 4); + String s = + "aaaa\n" + + "bbbb\n" + + "cccc\n" + + "dddd\n" + + "eeee\n" + + "ffff\n" + + "0123\n" + + "4567\n" + + "8901"; + fill(term, s); + vt100.setCursor(0, 0); + vt100.eraseToCursor(); + assertEquals(0,vt100.getCursorLine()); + assertEquals(0,vt100.getCursorColumn()); + assertEqualsTerm("aaaa\n" + + "bbbb\n" + + "cccc\n" + + "dddd\n" + + "eeee\n" + + "ffff\n" + + " 123\n" + + "4567\n" + + "8901",toMultiLineText(term)); + + fill(term, s); + vt100.setCursor(1, 0); + vt100.eraseToCursor(); + assertEquals(1,vt100.getCursorLine()); + assertEquals(0,vt100.getCursorColumn()); + assertEqualsTerm("aaaa\n" + + "bbbb\n" + + "cccc\n" + + "dddd\n" + + "eeee\n" + + "ffff\n" + + " \n" + + " 567\n" + + "8901",toMultiLineText(term)); + + fill(term, s); + vt100.setCursor(1, 1); + vt100.eraseToCursor(); + assertEquals(1,vt100.getCursorLine()); + assertEquals(1,vt100.getCursorColumn()); + assertEqualsTerm("aaaa\n" + + "bbbb\n" + + "cccc\n" + + "dddd\n" + + "eeee\n" + + "ffff\n" + + " \n" + + " 67\n" + + "8901",toMultiLineText(term)); + + fill(term, s); + vt100.setCursor(1, 4); + vt100.eraseToCursor(); + assertEquals(1,vt100.getCursorLine()); + assertEquals(3,vt100.getCursorColumn()); + assertEqualsTerm("aaaa\n" + + "bbbb\n" + + "cccc\n" + + "dddd\n" + + "eeee\n" + + "ffff\n" + + " \n" + + " \n" + + "8901",toMultiLineText(term)); + + + fill(term, s); + vt100.setCursor(1, 5); + vt100.eraseToCursor(); + assertEquals(1,vt100.getCursorLine()); + assertEquals(3,vt100.getCursorColumn()); + assertEqualsTerm("aaaa\n" + + "bbbb\n" + + "cccc\n" + + "dddd\n" + + "eeee\n" + + "ffff\n" + + " \n" + + " \n" + + "8901",toMultiLineText(term)); + + fill(term, s); + vt100.setCursor(2, 3); + vt100.eraseToCursor(); + assertEquals(2,vt100.getCursorLine()); + assertEquals(3,vt100.getCursorColumn()); + assertEqualsTerm("aaaa\n" + + "bbbb\n" + + "cccc\n" + + "dddd\n" + + "eeee\n" + + "ffff\n" + + " \n" + + " \n" + + " ",toMultiLineText(term)); + + fill(term, s); + vt100.setCursor(2, 5); + vt100.eraseToCursor(); + assertEquals(2,vt100.getCursorLine()); + assertEquals(3,vt100.getCursorColumn()); + assertEqualsTerm("aaaa\n" + + "bbbb\n" + + "cccc\n" + + "dddd\n" + + "eeee\n" + + "ffff\n" + + " \n" + + " \n" + + " ",toMultiLineText(term)); + } + + public void testEraseAll() { + ITerminalTextData term=makeITerminalTextData(); + IVT100EmulatorBackend vt100=makeBakend(term); + vt100.setDimensions(3, 4); + fill(term, "0000\n" + + "1111\n" + + "2222\n" + + "3333\n" + + "4444\n" + + "5555"); + vt100.eraseAll(); + assertEqualsTerm("0000\n" + + "1111\n" + + "2222\n" + + " \n" + + " \n" + + " ",toMultiLineText(term)); + } + + public void testEraseLine() { + String s = + "abcde\n" + + "fghij\n" + + "klmno\n" + + "pqrst\n" + + "uvwxy\n" + + "zABCD\n" + + "EFGHI"; + + ITerminalTextData term=makeITerminalTextData(); + IVT100EmulatorBackend vt100=makeBakend(term); + + vt100.setDimensions(3, 5); + fill(term, s); + vt100.setCursor(0, 3); + vt100.eraseLine(); + assertEquals(0,vt100.getCursorLine()); + assertEquals(3,vt100.getCursorColumn()); + assertEqualsTerm("abcde\n" + + "fghij\n" + + "klmno\n" + + "pqrst\n" + + " \n" + + "zABCD\n" + + "EFGHI", toMultiLineText(term)); + + + vt100.setDimensions(3, 5); + fill(term, s); + vt100.setCursor(2, 3); + vt100.eraseLine(); + assertEquals(2,vt100.getCursorLine()); + assertEquals(3,vt100.getCursorColumn()); + assertEqualsTerm("abcde\n" + + "fghij\n" + + "klmno\n" + + "pqrst\n" + + "uvwxy\n" + + "zABCD\n" + + " ", toMultiLineText(term)); + } + + public void testEraseLineToEnd() { + String s = + "abcde\n" + + "fghij\n" + + "klmno\n" + + "pqrst\n" + + "uvwxy\n" + + "zABCD\n" + + "EFGHI"; + + ITerminalTextData term=makeITerminalTextData(); + IVT100EmulatorBackend vt100=makeBakend(term); + + vt100.setDimensions(3, 5); + fill(term, s); + vt100.setCursor(0, 3); + vt100.eraseLineToEnd(); + assertEquals(0,vt100.getCursorLine()); + assertEquals(3,vt100.getCursorColumn()); + assertEqualsTerm("abcde\n" + + "fghij\n" + + "klmno\n" + + "pqrst\n" + + "uvw \n" + + "zABCD\n" + + "EFGHI", toMultiLineText(term)); + + vt100.setDimensions(3, 5); + fill(term, s); + vt100.setCursor(0, 0); + vt100.eraseLineToEnd(); + assertEquals(0,vt100.getCursorLine()); + assertEquals(0,vt100.getCursorColumn()); + assertEqualsTerm("abcde\n" + + "fghij\n" + + "klmno\n" + + "pqrst\n" + + " \n" + + "zABCD\n" + + "EFGHI", toMultiLineText(term)); + + + vt100.setDimensions(3, 5); + fill(term, s); + vt100.setCursor(2, 3); + vt100.eraseLineToEnd(); + assertEquals(2,vt100.getCursorLine()); + assertEquals(3,vt100.getCursorColumn()); + assertEqualsTerm("abcde\n" + + "fghij\n" + + "klmno\n" + + "pqrst\n" + + "uvwxy\n" + + "zABCD\n" + + "EFG ", toMultiLineText(term)); + vt100.setDimensions(3, 5); + fill(term, s); + + vt100.setCursor(2, 4); + vt100.eraseLineToEnd(); + assertEquals(2,vt100.getCursorLine()); + assertEquals(4,vt100.getCursorColumn()); + assertEqualsTerm("abcde\n" + + "fghij\n" + + "klmno\n" + + "pqrst\n" + + "uvwxy\n" + + "zABCD\n" + + "EFGH ", toMultiLineText(term)); + + vt100.setCursor(2, 5); + vt100.eraseLineToEnd(); + assertEquals(2,vt100.getCursorLine()); + assertEquals(4,vt100.getCursorColumn()); + assertEqualsTerm("abcde\n" + + "fghij\n" + + "klmno\n" + + "pqrst\n" + + "uvwxy\n" + + "zABCD\n" + + "EFGH ", toMultiLineText(term)); + + } + + public void testEraseLineToCursor() { + String s = + "abcde\n" + + "fghij\n" + + "klmno\n" + + "pqrst\n" + + "uvwxy\n" + + "zABCD\n" + + "EFGHI"; + + ITerminalTextData term=makeITerminalTextData(); + IVT100EmulatorBackend vt100=makeBakend(term); + + vt100.setDimensions(3, 5); + fill(term, s); + vt100.setCursor(0, 3); + vt100.eraseLineToCursor(); + assertEquals(0,vt100.getCursorLine()); + assertEquals(3,vt100.getCursorColumn()); + assertEqualsTerm("abcde\n" + + "fghij\n" + + "klmno\n" + + "pqrst\n" + + " y\n" + + "zABCD\n" + + "EFGHI", toMultiLineText(term)); + + vt100.setDimensions(3, 5); + fill(term, s); + vt100.setCursor(0, 0); + vt100.eraseLineToCursor(); + assertEquals(0,vt100.getCursorLine()); + assertEquals(0,vt100.getCursorColumn()); + assertEqualsTerm("abcde\n" + + "fghij\n" + + "klmno\n" + + "pqrst\n" + + " vwxy\n" + + "zABCD\n" + + "EFGHI", toMultiLineText(term)); + + + vt100.setDimensions(3, 5); + fill(term, s); + vt100.setCursor(2, 3); + vt100.eraseLineToCursor(); + assertEquals(2,vt100.getCursorLine()); + assertEquals(3,vt100.getCursorColumn()); + assertEqualsTerm("abcde\n" + + "fghij\n" + + "klmno\n" + + "pqrst\n" + + "uvwxy\n" + + "zABCD\n" + + " I", toMultiLineText(term)); + vt100.setDimensions(3, 5); + fill(term, s); + + vt100.setCursor(2, 4); + vt100.eraseLineToCursor(); + assertEquals(2,vt100.getCursorLine()); + assertEquals(4,vt100.getCursorColumn()); + assertEqualsTerm("abcde\n" + + "fghij\n" + + "klmno\n" + + "pqrst\n" + + "uvwxy\n" + + "zABCD\n" + + " ", toMultiLineText(term)); + + vt100.setCursor(2, 5); + vt100.eraseLineToCursor(); + assertEquals(2,vt100.getCursorLine()); + assertEquals(4,vt100.getCursorColumn()); + assertEqualsTerm("abcde\n" + + "fghij\n" + + "klmno\n" + + "pqrst\n" + + "uvwxy\n" + + "zABCD\n" + + " ", toMultiLineText(term)); + + } + + public void testInsertLines() { + ITerminalTextData term=makeITerminalTextData(); + IVT100EmulatorBackend vt100=makeBakend(term); + String s = "0000\n" + + "1111\n" + + "2222\n" + + "3333\n" + + "4444\n" + + "5555"; + vt100.setDimensions(3, 4); + fill(term, s); + vt100.setCursor(0, 0); + vt100.insertLines(1); + assertEqualsTerm( + "0000\n" + + "1111\n" + + "2222\n" + + " \n" + + "3333\n" + + "4444", toMultiLineText(term)); + + fill(term, s); + vt100.setCursor(1, 0); + vt100.insertLines(1); + assertEqualsTerm( + "0000\n" + + "1111\n" + + "2222\n" + + "3333\n" + + " \n" + + "4444", toMultiLineText(term)); + + fill(term, s); + vt100.setCursor(1, 0); + vt100.insertLines(2); + assertEqualsTerm( + "0000\n" + + "1111\n" + + "2222\n" + + "3333\n" + + " \n" + + " ", toMultiLineText(term)); + + fill(term, s); + vt100.setCursor(1, 3); + vt100.insertLines(2); + assertEqualsTerm( + "0000\n" + + "1111\n" + + "2222\n" + + "3333\n" + + " \n" + + " ", toMultiLineText(term)); + + + vt100.setDimensions(6, 4); + fill(term, s); + vt100.setCursor(1, 3); + vt100.insertLines(2); + assertEqualsTerm( + "0000\n" + + " \n" + + " \n" + + "1111\n" + + "2222\n" + + "3333", toMultiLineText(term)); + + + vt100.setDimensions(6, 4); + fill(term, s); + vt100.setCursor(1, 3); + vt100.insertLines(7); + assertEqualsTerm( + "0000\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " ", toMultiLineText(term)); + + vt100.setDimensions(6, 4); + fill(term, s); + vt100.setCursor(0, 0); + vt100.insertLines(7); + assertEqualsTerm( + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " ", toMultiLineText(term)); + + vt100.setDimensions(6, 4); + fill(term, s); + vt100.setCursor(0, 0); + vt100.insertLines(5); + assertEqualsTerm( + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + "0000", toMultiLineText(term)); + } + + public void testDeleteCharacters() { + ITerminalTextData term=makeITerminalTextData(); + IVT100EmulatorBackend vt100=makeBakend(term); + vt100.setDimensions(3, 4); + String s = + "aaaa\n" + + "bbbb\n" + + "cccc\n" + + "dddd\n" + + "eeee\n" + + "ffff\n" + + "1234\n" + + "4567\n" + + "9012"; + fill(term, s); + vt100.setCursor(0, 0); + vt100.deleteCharacters(1); + assertEqualsTerm("aaaa\n" + + "bbbb\n" + + "cccc\n" + + "dddd\n" + + "eeee\n" + + "ffff\n" + + "234 \n" + + "4567\n" + + "9012",toMultiLineText(term)); + + fill(term, s); + vt100.setCursor(1, 1); + vt100.deleteCharacters(1); + assertEqualsTerm("aaaa\n" + + "bbbb\n" + + "cccc\n" + + "dddd\n" + + "eeee\n" + + "ffff\n" + + "1234\n" + + "467 \n" + + "9012",toMultiLineText(term)); + + fill(term, s); + vt100.setCursor(1, 1); + vt100.deleteCharacters(2); + assertEqualsTerm("aaaa\n" + + "bbbb\n" + + "cccc\n" + + "dddd\n" + + "eeee\n" + + "ffff\n" + + "1234\n" + + "47 \n" + + "9012",toMultiLineText(term)); + + vt100.setDimensions(1, 10); + fill(term, "0123456789"); + vt100.setCursor(0, 0); + vt100.deleteCharacters(10); + assertEqualsTerm(" ",toMultiLineText(term)); + + vt100.setDimensions(1, 10); + fill(term, "0123456789"); + vt100.setCursor(0, 0); + vt100.deleteCharacters(14); + assertEqualsTerm(" ",toMultiLineText(term)); + + vt100.setDimensions(1, 10); + fill(term, "0123456789"); + vt100.setCursor(0, 3); + vt100.deleteCharacters(0); + assertEqualsTerm("0123456789",toMultiLineText(term)); + + vt100.setDimensions(1, 10); + fill(term, "0123456789"); + vt100.setCursor(0, 3); + vt100.deleteCharacters(2); + assertEqualsTerm("01256789 ",toMultiLineText(term)); + + vt100.setDimensions(1, 10); + fill(term, "0123456789"); + vt100.setCursor(0, 3); + vt100.deleteCharacters(14); + assertEqualsTerm("012 ",toMultiLineText(term)); + + } + + public void testDeleteLines() { + ITerminalTextData term=makeITerminalTextData(); + IVT100EmulatorBackend vt100=makeBakend(term); + String s = "0000\n" + + "1111\n" + + "2222\n" + + "3333\n" + + "4444\n" + + "5555"; + vt100.setDimensions(3, 4); + fill(term, s); + vt100.setCursor(0, 0); + vt100.deleteLines(1); + assertEqualsTerm( + "0000\n" + + "1111\n" + + "2222\n" + + "4444\n" + + "5555\n" + + " ", toMultiLineText(term)); + + fill(term, s); + vt100.setCursor(1, 0); + vt100.deleteLines(1); + assertEqualsTerm( + "0000\n" + + "1111\n" + + "2222\n" + + "3333\n" + + "5555\n" + + " ", toMultiLineText(term)); + + fill(term, s); + vt100.setCursor(1, 0); + vt100.deleteLines(2); + assertEqualsTerm( + "0000\n" + + "1111\n" + + "2222\n" + + "3333\n" + + " \n" + + " ", toMultiLineText(term)); + + fill(term, s); + vt100.setCursor(1, 3); + vt100.deleteLines(2); + assertEqualsTerm( + "0000\n" + + "1111\n" + + "2222\n" + + "3333\n" + + " \n" + + " ", toMultiLineText(term)); + + + vt100.setDimensions(6, 4); + fill(term, s); + vt100.setCursor(1, 3); + vt100.deleteLines(2); + assertEqualsTerm( + "0000\n" + + "3333\n" + + "4444\n" + + "5555\n" + + " \n" + + " ", toMultiLineText(term)); + + + vt100.setDimensions(6, 4); + fill(term, s); + vt100.setCursor(1, 3); + vt100.deleteLines(7); + assertEqualsTerm( + "0000\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " ", toMultiLineText(term)); + + vt100.setDimensions(6, 4); + fill(term, s); + vt100.setCursor(0, 0); + vt100.deleteLines(7); + assertEqualsTerm( + " \n" + + " \n" + + " \n" + + " \n" + + " \n" + + " ", toMultiLineText(term)); + + vt100.setDimensions(6, 4); + fill(term, s); + vt100.setCursor(0, 0); + vt100.deleteLines(5); + assertEqualsTerm( + "5555\n" + + " \n" + + " \n" + + " \n" + + " \n" + + " ", toMultiLineText(term)); + } + + public void testGetDefaultStyle() { + ITerminalTextData term=makeITerminalTextData(); + IVT100EmulatorBackend vt100=makeBakend(term); + Style style=Style.getStyle("white", "black"); + vt100.setDefaultStyle(style); + assertSame(style, vt100.getDefaultStyle()); + Style style2=style.setBold(true); + vt100.setDefaultStyle(style2); + assertSame(style2, vt100.getDefaultStyle()); + } + + public void testGetStyle() { + ITerminalTextData term=makeITerminalTextData(); + IVT100EmulatorBackend vt100=makeBakend(term); + Style style=Style.getStyle("white", "black"); + vt100.setStyle(style); + assertSame(style, vt100.getStyle()); + Style style2=style.setBold(true); + vt100.setStyle(style2); + assertSame(style2, vt100.getStyle()); + } + + public void testAppendString() { + ITerminalTextData term=makeITerminalTextData(); + IVT100EmulatorBackend vt100=makeBakend(term); + term.setMaxHeight(6); + vt100.setDimensions(3, 4); + vt100.setCursor(0, 0); + assertEqualsTerm( + " \n" + + " \n" + + " ", toMultiLineText(term)); + vt100.appendString("012"); + assertEqualsTerm( + "012 \n" + + " \n" + + " ", toMultiLineText(term)); + assertEquals(0,vt100.getCursorLine()); + assertEquals(3,vt100.getCursorColumn()); + vt100.appendString("3"); + assertEqualsTerm( + "0123\n" + + " \n" + + " ", toMultiLineText(term)); + assertEquals(1,vt100.getCursorLine()); + assertEquals(0,vt100.getCursorColumn()); + + vt100.appendString("567890"); + assertEqualsTerm( + "0123\n" + + "5678\n" + + "90 ", toMultiLineText(term)); + assertEquals(2,vt100.getCursorLine()); + assertEquals(2,vt100.getCursorColumn()); + + + vt100.appendString("a"); + assertEqualsTerm( + "0123\n" + + "5678\n" + + "90a ", toMultiLineText(term)); + assertEquals(2,vt100.getCursorLine()); + assertEquals(3,vt100.getCursorColumn()); + + vt100.appendString("b"); + assertEqualsTerm( + "0123\n" + + "5678\n" + + "90ab\n" + + " ", toMultiLineText(term)); + assertEquals(2,vt100.getCursorLine()); + assertEquals(0,vt100.getCursorColumn()); + + vt100.appendString("cd"); + assertEqualsTerm( + "0123\n" + + "5678\n" + + "90ab\n" + + "cd ", toMultiLineText(term)); + assertEquals(2,vt100.getCursorLine()); + assertEquals(2,vt100.getCursorColumn()); + + vt100.appendString("efgh"); + assertEqualsTerm( + "0123\n" + + "5678\n" + + "90ab\n" + + "cdef\n" + + "gh ", toMultiLineText(term)); + assertEquals(2,vt100.getCursorLine()); + assertEquals(2,vt100.getCursorColumn()); + + vt100.appendString("ijklmnopqrstuvwx"); + assertEqualsTerm( + "cdef\n" + + "ghij\n" + + "klmn\n" + + "opqr\n" + + "stuv\n" + + "wx ", toMultiLineText(term)); + assertEquals(2,vt100.getCursorLine()); + assertEquals(2,vt100.getCursorColumn()); + + vt100.setCursor(1, 1); + vt100.appendString("123"); + assertEqualsTerm( + "cdef\n" + + "ghij\n" + + "klmn\n" + + "opqr\n" + + "s123\n" + + "wx ", toMultiLineText(term)); + assertEquals(2,vt100.getCursorLine()); + assertEquals(0,vt100.getCursorColumn()); + + vt100.setCursor(1, 1); + vt100.appendString("ABCDEFGHIJKL"); + assertEqualsTerm( + "klmn\n" + + "opqr\n" + + "sABC\n" + + "DEFG\n" + + "HIJK\n" + + "L ", toMultiLineText(term)); + assertEquals(2,vt100.getCursorLine()); + assertEquals(1,vt100.getCursorColumn()); + + } + + public void testProcessNewline() { + ITerminalTextData term=makeITerminalTextData(); + IVT100EmulatorBackend vt100=makeBakend(term); + String s = "0000\n" + + "1111\n" + + "2222\n" + + "3333\n" + + "4444\n" + + "5555"; + term.setMaxHeight(6); + vt100.setDimensions(3, 4); + vt100.setCursor(0, 0); + fill(term,s); + assertEquals(0,vt100.getCursorLine()); + assertEquals(0,vt100.getCursorColumn()); + vt100.processNewline(); + assertEqualsTerm(s, toMultiLineText(term)); + assertEquals(1,vt100.getCursorLine()); + assertEquals(0,vt100.getCursorColumn()); + vt100.setCursorColumn(3); + vt100.processNewline(); + assertEqualsTerm(s, toMultiLineText(term)); + assertEquals(2,vt100.getCursorLine()); + assertEquals(3,vt100.getCursorColumn()); + + vt100.processNewline(); + assertEqualsTerm( + "1111\n" + + "2222\n" + + "3333\n" + + "4444\n" + + "5555\n" + + " ", toMultiLineText(term)); + assertEquals(2,vt100.getCursorLine()); + assertEquals(3,vt100.getCursorColumn()); + + + vt100.processNewline(); + assertEqualsTerm( + "2222\n" + + "3333\n" + + "4444\n" + + "5555\n" + + " \n" + + " ", toMultiLineText(term)); + assertEquals(2,vt100.getCursorLine()); + assertEquals(3,vt100.getCursorColumn()); + } + + public void testSetCursorLine() { + ITerminalTextData term=makeITerminalTextData(); + IVT100EmulatorBackend vt100=makeBakend(term); + term.setMaxHeight(6); + vt100.setDimensions(3, 4); + // the cursor is now at the end.... + assertEquals(2,vt100.getCursorLine()); + assertEquals(0,vt100.getCursorColumn()); + vt100.setCursor(0, 2); + vt100.setCursorLine(1); + assertEquals(1,vt100.getCursorLine()); + assertEquals(2,vt100.getCursorColumn()); + vt100.setCursor(0, -2); + vt100.setCursorLine(-1); + assertEquals(0,vt100.getCursorLine()); + assertEquals(0,vt100.getCursorColumn()); + vt100.setCursor(0, 10); + vt100.setCursorLine(10); + assertEquals(2,vt100.getCursorLine()); + assertEquals(3,vt100.getCursorColumn()); + } + + public void testSetCursorColumn() { + ITerminalTextData term=makeITerminalTextData(); + IVT100EmulatorBackend vt100=makeBakend(term); + term.setMaxHeight(6); + vt100.setDimensions(3, 4); + assertEquals(2,vt100.getCursorLine()); + assertEquals(0,vt100.getCursorColumn()); + vt100.setCursor(1, 0); + vt100.setCursorColumn(2); + assertEquals(1,vt100.getCursorLine()); + assertEquals(2,vt100.getCursorColumn()); + vt100.setCursor(-1, -2); + vt100.setCursorColumn(-2); + assertEquals(0,vt100.getCursorLine()); + assertEquals(0,vt100.getCursorColumn()); + vt100.setCursor(10, 0); + vt100.setCursorColumn(10); + assertEquals(2,vt100.getCursorLine()); + assertEquals(3,vt100.getCursorColumn()); + } + + public void testSetCursor() { + ITerminalTextData term=makeITerminalTextData(); + IVT100EmulatorBackend vt100=makeBakend(term); + term.setMaxHeight(6); + vt100.setDimensions(3, 4); + assertEquals(2,vt100.getCursorLine()); + assertEquals(0,vt100.getCursorColumn()); + vt100.setCursor(0, 0); + assertEquals(0,vt100.getCursorLine()); + assertEquals(0,vt100.getCursorColumn()); + vt100.setCursor(1, 2); + assertEquals(1,vt100.getCursorLine()); + assertEquals(2,vt100.getCursorColumn()); + vt100.setCursor(-1, -2); + assertEquals(0,vt100.getCursorLine()); + assertEquals(0,vt100.getCursorColumn()); + vt100.setCursor(10, 10); + assertEquals(2,vt100.getCursorLine()); + assertEquals(3,vt100.getCursorColumn()); + } + +} diff --git a/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/model/AbstractITerminalTextDataTest.java b/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/model/AbstractITerminalTextDataTest.java new file mode 100644 index 00000000000..f8d039c6c8b --- /dev/null +++ b/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/model/AbstractITerminalTextDataTest.java @@ -0,0 +1,808 @@ +/******************************************************************************* + * Copyright (c) 2007 Wind River Systems, Inc. and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Michael Scharf (Wind River) - initial API and implementation + *******************************************************************************/ +package org.eclipse.tm.internal.terminal.model; +import junit.framework.TestCase; + +import org.eclipse.tm.terminal.model.ITerminalTextData; +import org.eclipse.tm.terminal.model.ITerminalTextDataReadOnly; +import org.eclipse.tm.terminal.model.LineSegment; +import org.eclipse.tm.terminal.model.Style; +import org.eclipse.tm.terminal.model.StyleColor; + +abstract public class AbstractITerminalTextDataTest extends TestCase { + abstract protected ITerminalTextData makeITerminalTextData(); + + protected String toSimple(ITerminalTextData term) { + return TerminalTextTestHelper.toSimple(term); + } + protected String toMultiLineText(ITerminalTextDataReadOnly term) { + return TerminalTextTestHelper.toMultiLineText(term); + } + + protected void fill(ITerminalTextData term, String s) { + TerminalTextTestHelper.fill(term,s); + } + + protected void fill(ITerminalTextData term, int i, int j, String s) { + TerminalTextTestHelper.fill(term,i,j,s); + } + + protected void fillSimple(ITerminalTextData term, String s) { + TerminalTextTestHelper.fillSimple(term, s); + } + + + /** + * Used for multi line text + * @param expected + * @param actual + */ + protected void assertEqualsTerm(String expected,String actual) { + assertEquals(expected, actual); + } + /** + * Used for simple text + * @param expected + * @param actual + */ + protected void assertEqualsSimple(String expected,String actual) { + assertEquals(-1,actual.indexOf('\n')); + assertEquals(expected, actual); + } + public void testGetWidth() { + ITerminalTextData term=makeITerminalTextData(); + assertEquals(0, term.getWidth()); + term.setDimensions(term.getHeight(), 10); + assertEquals(10, term.getWidth()); + term.setDimensions(term.getHeight(), 0); + assertEquals(0, term.getWidth()); + } + + public void testAddLine() { + String s= + "111\n" + + "222\n" + + "333\n" + + "444\n" + + "555"; + ITerminalTextData term=makeITerminalTextData(); + fill(term, s); + term.setMaxHeight(5); + term.addLine(); + assertEqualsTerm( + "222\n" + + "333\n" + + "444\n" + + "555\n" + + "\000\000\000", toMultiLineText(term)); + } + public void testCleanLine() { + String s= + "111\n" + + "222\n" + + "333\n" + + "444\n" + + "555"; + ITerminalTextData term=makeITerminalTextData(); + fill(term, s); + term.cleanLine(0); + assertEqualsTerm( + "\000\000\000\n" + + "222\n" + + "333\n" + + "444\n" + + "555", toMultiLineText(term)); + + fill(term, s); + term.cleanLine(4); + assertEqualsTerm( + "111\n" + + "222\n" + + "333\n" + + "444\n" + + "\000\000\000", toMultiLineText(term)); + } + + public void testMaxSize() { + String s= + "111\n" + + "222\n" + + "333\n" + + "444\n" + + "555"; + ITerminalTextData term=makeITerminalTextData(); + term.setMaxHeight(8); + fill(term, s); + assertEquals(5, term.getHeight()); + assertEquals(8, term.getMaxHeight()); + term.addLine(); + assertEquals(6, term.getHeight()); + assertEqualsTerm( + "111\n" + + "222\n" + + "333\n" + + "444\n" + + "555\n" + + "\000\000\000", toMultiLineText(term)); + term.addLine(); + assertEquals(7, term.getHeight()); + assertEqualsTerm( + "111\n" + + "222\n" + + "333\n" + + "444\n" + + "555\n" + + "\000\000\000\n" + + "\000\000\000", toMultiLineText(term)); + term.addLine(); + assertEquals(8, term.getHeight()); + assertEqualsTerm( + "111\n" + + "222\n" + + "333\n" + + "444\n" + + "555\n" + + "\000\000\000\n" + + "\000\000\000\n" + + "\000\000\000", toMultiLineText(term)); + term.addLine(); + assertEquals(8, term.getHeight()); + assertEqualsTerm( + "222\n" + + "333\n" + + "444\n" + + "555\n" + + "\000\000\000\n" + + "\000\000\000\n" + + "\000\000\000\n" + + "\000\000\000", toMultiLineText(term)); + term.addLine(); + assertEquals(8, term.getHeight()); + assertEqualsTerm( + "333\n" + + "444\n" + + "555\n" + + "\000\000\000\n" + + "\000\000\000\n" + + "\000\000\000\n" + + "\000\000\000\n" + + "\000\000\000", toMultiLineText(term)); + } + public void testGetHeight() { + ITerminalTextData term=makeITerminalTextData(); + assertEquals(0, term.getHeight()); + term.setDimensions(10, term.getWidth()); + assertEquals(10, term.getHeight()); + term.setDimensions(0, term.getWidth()); + assertEquals(0, term.getHeight()); + } + + public void testSetDimensions() { + ITerminalTextData term=makeITerminalTextData(); + assertEquals(0, term.getHeight()); + term.setDimensions(10, 5); + assertEquals(5, term.getWidth()); + assertEquals(10, term.getHeight()); + term.setDimensions(5, 10); + assertEquals(10, term.getWidth()); + assertEquals(5, term.getHeight()); + term.setDimensions(15, 0); + assertEquals(0, term.getWidth()); + assertEquals(15, term.getHeight()); + term.setDimensions(0, 12); + assertEquals(12, term.getWidth()); + assertEquals(0, term.getHeight()); + term.setDimensions(0, 0); + assertEquals(0, term.getWidth()); + assertEquals(0, term.getHeight()); + } + public void testResize() { + ITerminalTextData term=makeITerminalTextData(); + term.setDimensions(3, 5); + String s="12345\n" + + "abcde\n" + + "ABCDE"; + fill(term,0,0,s); + assertEqualsTerm(s, toMultiLineText(term)); + term.setDimensions(3, 4); + assertEqualsTerm( + "1234\n" + + "abcd\n" + + "ABCD", toMultiLineText(term)); + // the columns should be restored + term.setDimensions(3, 5); + assertEqualsTerm( + "12345\n" + + "abcde\n" + + "ABCDE", toMultiLineText(term)); + term.setDimensions(3, 6); + assertEqualsTerm( + "12345\000\n" + + "abcde\000\n" + + "ABCDE\000", toMultiLineText(term)); + term.setChar(0, 5, 'x', null); + term.setChar(1, 5, 'y', null); + term.setChar(2, 5, 'z', null); + assertEqualsTerm( + "12345x\n" + + "abcdey\n" + + "ABCDEz", toMultiLineText(term)); + term.setDimensions(2, 4); + assertEqualsTerm( + "1234\n" + + "abcd", toMultiLineText(term)); + } + + public void testResizeFailure() { + ITerminalTextData term=makeITerminalTextData(); + term.setDimensions(3, 5); + String s="12345\n" + + "abcde\n" + + "ABCDE"; + fill(term,0,0,s); + assertEqualsTerm(s, toMultiLineText(term)); + try { + term.setDimensions(-3, 4); + fail(); // make sure you run this code with assertions enabled (vmargs: -ea) + } catch (RuntimeException e) { + // OK + } +// assertEquals(5, term.getWidth()); +// assertEquals(3, term.getHeight()); +// assertEquals(s, toSimpleText(term)); + } + + public void testGetLineSegments() { + Style s1=getDefaultStyle(); + Style s2=s1.setBold(true); + Style s3=s1.setUnderline(true); + ITerminalTextData term=makeITerminalTextData(); + term.setDimensions(8, 8); + LineSegment[] segments; + + term.setChars(0, 0,"0123".toCharArray(), s1); + term.setChars(0, 4,"abcd".toCharArray(), null); + segments=term.getLineSegments(0, 0, term.getWidth()); + assertEquals(2, segments.length); + assertSegment(0, "0123", s1, segments[0]); + assertSegment(4, "abcd", null, segments[1]); + + + segments=term.getLineSegments(0, 4, term.getWidth()-4); + assertEquals(1, segments.length); + assertSegment(4, "abcd", null, segments[0]); + + segments=term.getLineSegments(0, 3, 2); + assertEquals(2, segments.length); + assertSegment(3, "3", s1, segments[0]); + assertSegment(4, "a", null, segments[1]); + + segments=term.getLineSegments(0, 7, 1); + assertEquals(1, segments.length); + assertSegment(7, "d", null, segments[0]); + + segments=term.getLineSegments(0, 0, 1); + assertEquals(1, segments.length); + assertSegment(0, "0", s1, segments[0]); + + // line 1 + term.setChars(1, 0,"x".toCharArray(), s1); + term.setChars(1, 1,"y".toCharArray(), s2); + term.setChars(1, 2,"z".toCharArray(), s3); + + segments=term.getLineSegments(1, 0, term.getWidth()); + assertEquals(4, segments.length); + assertSegment(0, "x", s1, segments[0]); + assertSegment(1, "y", s2, segments[1]); + assertSegment(2, "z", s3, segments[2]); + assertSegment(3, "\000\000\000\000\000", null, segments[3]); + + // line 2 + term.setChars(2, 4,"klm".toCharArray(), s1); + segments=term.getLineSegments(2, 0, term.getWidth()); + assertEquals(3, segments.length); + assertSegment(0, "\000\000\000\000", null, segments[0]); + assertSegment(4, "klm", s1, segments[1]); + assertSegment(7, "\000", null, segments[2]); + + // line 3 + segments=term.getLineSegments(3, 0, term.getWidth()); + assertEquals(1, segments.length); + assertSegment(0, "\000\000\000\000\000\000\000\000", null, segments[0]); + + } + public void testGetLineSegmentsNull() { + ITerminalTextData term=makeITerminalTextData(); + term.setDimensions(8, 8); + LineSegment[] segments=term.getLineSegments(0, 0, term.getWidth()); + assertEquals(1, segments.length); + } + public void testGetLineSegmentsOutOfBounds() { + ITerminalTextData term=makeITerminalTextData(); + term.setDimensions(1, 8); + term.setChars(0,0,"xx".toCharArray(),null); + LineSegment[] segments=term.getLineSegments(0, 5, 2); + assertEquals(1, segments.length); + + + } + void assertSegment(int col,String text, Style style,LineSegment segment) { + assertEquals(col, segment.getColumn()); + assertEqualsTerm(text, segment.getText()); + assertEquals(style, segment.getStyle()); + + } + public void testGetChar() { + String s="12345\n" + + "abcde\n" + + "ABCDE"; + ITerminalTextData term=makeITerminalTextData(); + fill(term, s); + assertEquals('1', term.getChar(0,0)); + assertEquals('2', term.getChar(0,1)); + assertEquals('3', term.getChar(0,2)); + assertEquals('4', term.getChar(0,3)); + assertEquals('5', term.getChar(0,4)); + assertEquals('a', term.getChar(1,0)); + assertEquals('b', term.getChar(1,1)); + assertEquals('c', term.getChar(1,2)); + assertEquals('d', term.getChar(1,3)); + assertEquals('e', term.getChar(1,4)); + assertEquals('A', term.getChar(2,0)); + assertEquals('B', term.getChar(2,1)); + assertEquals('C', term.getChar(2,2)); + assertEquals('D', term.getChar(2,3)); + assertEquals('E', term.getChar(2,4)); + try { + term.getChar(0,-1); + fail(); // make sure you run this code with assertions enabled (vmargs: -ea) + } catch (RuntimeException e) { + } + try { + term.getChar(-1,-1); + fail(); // make sure you run this code with assertions enabled (vmargs: -ea) + } catch (RuntimeException e) { + } + try { + term.getChar(-1,0); + fail(); // make sure you run this code with assertions enabled (vmargs: -ea) + } catch (RuntimeException e) { + } + try { + term.getChar(0,5); + fail(); // make sure you run this code with assertions enabled (vmargs: -ea) + } catch (RuntimeException e) { + } + try { + term.getChar(3,5); + fail(); // make sure you run this code with assertions enabled (vmargs: -ea) + } catch (RuntimeException e) { + } + try { + term.getChar(3,0); + fail(); // make sure you run this code with assertions enabled (vmargs: -ea) + } catch (RuntimeException e) { + } + } + + public void testGetStyle() { + ITerminalTextData term=makeITerminalTextData(); + Style style=getDefaultStyle(); + term.setDimensions(6, 3); + for (int line = 0; line < term.getHeight(); line++) { + for (int column = 0; column < term.getWidth(); column++) { + char c=(char)('a'+column+line); + term.setChar(line, column, c, style.setForground(StyleColor.getStyleColor(""+c))); + } + } + for (int line = 0; line < term.getHeight(); line++) { + for (int column = 0; column < term.getWidth(); column++) { + char c=(char)('a'+column+line); + assertSame(style.setForground(StyleColor.getStyleColor(""+c)), term.getStyle(line, column)); + } + } + + } + + protected Style getDefaultStyle() { + return Style.getStyle(StyleColor.getStyleColor("fg"), StyleColor.getStyleColor("bg"), false, false, false, false); + } + + public void testSetChar() { + ITerminalTextData term=makeITerminalTextData(); + term.setDimensions(6, 3); + for (int line = 0; line < term.getHeight(); line++) { + for (int column = 0; column < term.getWidth(); column++) { + term.setChar(line, column, (char)('a'+column+line), null); + } + } + for (int line = 0; line < term.getHeight(); line++) { + for (int column = 0; column < term.getWidth(); column++) { + char c=(char)('a'+column+line); + assertEquals(c, term.getChar(line,column)); + } + } + assertEqualsTerm( + "abc\n" + + "bcd\n" + + "cde\n" + + "def\n" + + "efg\n" + + "fgh", toMultiLineText(term)); + } + public void testSetChars() { + ITerminalTextData term=makeITerminalTextData(); + term.setDimensions(6, 3); + for (int line = 0; line < term.getHeight(); line++) { + char[] chars=new char[term.getWidth()]; + for (int column = 0; column < term.getWidth(); column++) { + chars[column]=(char)('a'+column+line); + } + term.setChars(line, 0, chars, null); + } + for (int line = 0; line < term.getHeight(); line++) { + for (int column = 0; column < term.getWidth(); column++) { + char c=(char)('a'+column+line); + assertEquals(c, term.getChar(line,column)); + } + } + assertEqualsTerm( + "abc\n" + + "bcd\n" + + "cde\n" + + "def\n" + + "efg\n" + + "fgh", toMultiLineText(term)); + + term.setChars(3, 1, new char[]{'1','2'}, null); + assertEqualsTerm( + "abc\n" + + "bcd\n" + + "cde\n" + + "d12\n" + + "efg\n" + + "fgh", toMultiLineText(term)); + try { + // check if we cannot exceed the range + term.setChars(4, 1, new char[]{'1','2','3','4','5'}, null); + fail(); // make sure you run this code with assertions enabled (vmargs: -ea) + } catch (RuntimeException e) {} + + } + public void testSetCharsLen() { + ITerminalTextData term=makeITerminalTextData(); + String s= "ZYXWVU\n" + + "abcdef\n" + + "ABCDEF"; + fill(term, s); + char[] chars=new char[]{'1','2','3','4','5','6','7','8'}; + term.setChars(1, 0, chars, 0, 6,null); + assertEqualsTerm( + "ZYXWVU\n" + + "123456\n" + + "ABCDEF", toMultiLineText(term)); + + fill(term, s); + term.setChars(1, 0, chars, 0, 5, null); + assertEqualsTerm("ZYXWVU\n" + + "12345f\n" + + "ABCDEF", toMultiLineText(term)); + + fill(term, s); + term.setChars(1, 0, chars, 1, 5, null); + assertEqualsTerm("ZYXWVU\n" + + "23456f\n" + + "ABCDEF", toMultiLineText(term)); + + fill(term, s); + term.setChars(1, 1, chars, 1, 4, null); + assertEqualsTerm("ZYXWVU\n" + + "a2345f\n" + + "ABCDEF", toMultiLineText(term)); + + + + fill(term, s); + term.setChars(1, 2, chars, 3, 4, null); + assertEqualsTerm("ZYXWVU\n" + + "ab4567\n" + + "ABCDEF", toMultiLineText(term)); + + fill(term, s); + try { + term.setChars(1, 0, chars, 7, 10, null); + fail(); // make sure you run this code with assertions enabled (vmargs: -ea) + } catch (RuntimeException e) {} + fill(term, s); + try { + term.setChars(1, -1, chars, 0, 2, null); + fail(); // make sure you run this code with assertions enabled (vmargs: -ea) + } catch (RuntimeException e) {} + try { + term.setChars(-1, 1, chars, 0, 2, null); + fail(); // make sure you run this code with assertions enabled (vmargs: -ea) + } catch (RuntimeException e) {} + try { + term.setChars(1, 10, chars, 0, 2, null); + fail(); // make sure you run this code with assertions enabled (vmargs: -ea) + } catch (RuntimeException e) {} + try { + term.setChars(10, 1, chars, 0, 2, null); + fail(); // make sure you run this code with assertions enabled (vmargs: -ea) + } catch (RuntimeException e) {} +// assertEquals(s, toSimpleText(term)); + } + public void testSetCopyInto() { + ITerminalTextData term=makeITerminalTextData(); + term.setDimensions(3, 5); + String s="12345\n" + + "abcde\n" + + "ABCDE"; + fill(term,0,0,s); + ITerminalTextData termCopy=makeITerminalTextData(); + termCopy.copy(term); + assertEqualsTerm(s, toMultiLineText(termCopy)); + assertEqualsTerm(s, toMultiLineText(term)); + + termCopy.setChar(1, 1, 'X', null); + assertEqualsTerm(s, toMultiLineText(term)); + term.setDimensions(2, 4); + assertEquals(5, termCopy.getWidth()); + assertEquals(3, termCopy.getHeight()); + + assertEqualsTerm("12345\n" + + "aXcde\n" + + "ABCDE", toMultiLineText(termCopy)); + + assertEquals(4, term.getWidth()); + assertEquals(2, term.getHeight()); + } + public void testSetCopyLines() { + ITerminalTextData term=makeITerminalTextData(); + String s="012345"; + fillSimple(term, s); + ITerminalTextData termCopy=makeITerminalTextData(); + String sCopy="abcde"; + fillSimple(termCopy, sCopy); + termCopy.copyRange(term,0,0,0); + assertEqualsSimple(s, toSimple(term)); + assertEqualsSimple(sCopy, toSimple(termCopy)); + + fillSimple(termCopy, sCopy); + termCopy.copyRange(term,0,0,5); + assertEqualsSimple(s, toSimple(term)); + assertEqualsSimple("01234", toSimple(termCopy)); + + fillSimple(termCopy, sCopy); + termCopy.copyRange(term,0,0,2); + assertEqualsSimple(s, toSimple(term)); + assertEqualsSimple("01cde", toSimple(termCopy)); + + fillSimple(termCopy, sCopy); + termCopy.copyRange(term,0,1,2); + assertEqualsSimple(s, toSimple(term)); + assertEqualsSimple("a01de", toSimple(termCopy)); + + fillSimple(termCopy, sCopy); + termCopy.copyRange(term,1,1,2); + assertEqualsSimple(s, toSimple(term)); + assertEqualsSimple("a12de", toSimple(termCopy)); + + fillSimple(termCopy, sCopy); + termCopy.copyRange(term,1,1,4); + assertEqualsSimple(s, toSimple(term)); + assertEqualsSimple("a1234", toSimple(termCopy)); + + fillSimple(termCopy, sCopy); + termCopy.copyRange(term,2,1,4); + assertEqualsSimple(s, toSimple(term)); + assertEqualsSimple("a2345", toSimple(termCopy)); + + try { + fillSimple(termCopy, sCopy); + termCopy.copyRange(term,1,1,5); + fail(); // make sure you run this code with assertions enabled (vmargs: -ea) + } catch (RuntimeException e) {} + try { + fillSimple(termCopy, sCopy); + termCopy.copyRange(term,0,0,6); + fail(); // make sure you run this code with assertions enabled (vmargs: -ea) + } catch (RuntimeException e) {} + try { + fillSimple(termCopy, sCopy); + termCopy.copyRange(term,7,0,1); + fail(); // make sure you run this code with assertions enabled (vmargs: -ea) + } catch (RuntimeException e) {} + try { + fillSimple(termCopy, sCopy); + termCopy.copyRange(term,0,7,1); + fail(); // make sure you run this code with assertions enabled (vmargs: -ea) + } catch (RuntimeException e) {} + } + public void testCopyLine() { + ITerminalTextData term=makeITerminalTextData(); + String s= + "111\n" + + "222\n" + + "333\n" + + "444\n" + + "555"; + fill(term, s); + ITerminalTextData dest=makeITerminalTextData(); + String sCopy= + "aaa\n" + + "bbb\n" + + "ccc\n" + + "ddd\n" + + "eee"; + fill(dest, sCopy); + copySelective(dest,term,0,0,new boolean []{true,true,false,false,true}); + assertEqualsTerm(s, toMultiLineText(term)); + assertEqualsTerm( + "111\n" + + "222\n" + + "ccc\n" + + "ddd\n" + + "555", toMultiLineText(dest)); + + fill(dest, sCopy); + copySelective(dest,term,0,0,new boolean []{true,true,true,true,true}); + assertEqualsTerm(s, toMultiLineText(term)); + assertEqualsTerm(s, toMultiLineText(dest)); + + fill(dest, sCopy); + copySelective(dest,term,0,0,new boolean []{false,false,false,false,false}); + assertEqualsTerm(s, toMultiLineText(term)); + assertEqualsTerm(sCopy, toMultiLineText(dest)); + } + protected void copySelective(ITerminalTextData dest, ITerminalTextData source, int sourceStartLine, int destStartLine, boolean[] linesToCopy) { + for (int i = 0; i < linesToCopy.length; i++) { + if(linesToCopy[i]) { + dest.copyLine(source, i+sourceStartLine, i+destStartLine); + } + } + } + + public void testCopyLineWithOffset() { + ITerminalTextData term=makeITerminalTextData(); + String s= + "111\n" + + "222\n" + + "333\n" + + "444\n" + + "555"; + fill(term, s); + ITerminalTextData dest=makeITerminalTextData(); + String sCopy= + "aaa\n" + + "bbb\n" + + "ccc\n" + + "ddd\n" + + "eee"; + fill(dest, sCopy); + copySelective(dest,term,1,0,new boolean []{true,false,false,true}); + assertEqualsTerm(s, toMultiLineText(term)); + assertEqualsTerm( + "222\n" + + "bbb\n" + + "ccc\n" + + "555\n" + + "eee", toMultiLineText(dest)); + + fill(dest, sCopy); + copySelective(dest,term,2,0,new boolean []{true,true}); + assertEqualsTerm(s, toMultiLineText(term)); + assertEqualsTerm( + "333\n" + + "444\n" + + "ccc\n" + + "ddd\n" + + "eee", toMultiLineText(dest)); + + fill(dest, sCopy); + copySelective(dest,term,0,0,new boolean []{true,true,true,true,true}); + assertEqualsTerm(s, toMultiLineText(term)); + assertEqualsTerm(s, toMultiLineText(dest)); + + fill(dest, sCopy); + copySelective(dest,term,0,0,new boolean []{false,false,false,false,false}); + assertEqualsTerm(s, toMultiLineText(term)); + assertEqualsTerm(sCopy, toMultiLineText(dest)); + } + public void testScrollNoop() { + scrollTest(0,0,0, "012345","012345"); + scrollTest(0,1,0, "012345","012345"); + scrollTest(0,6,0, "012345","012345"); + } + public void testScrollAll() { + scrollTest(0,6,1, "012345"," 01234"); + scrollTest(0,6,-1, "012345","12345 "); + scrollTest(0,6,2, "012345"," 0123"); + scrollTest(0,6,-2, "012345","2345 "); + } + public void testScrollNegative() { + scrollTest(0,2,-1,"012345","1 2345"); + scrollTest(0,1,-1,"012345"," 12345"); + scrollTest(0,6,-1,"012345","12345 "); + scrollTest(0,6,-6,"012345"," "); + scrollTest(0,6,-7,"012345"," "); + scrollTest(0,6,-8,"012345"," "); + scrollTest(0,6,-2,"012345","2345 "); + scrollTest(1,1,-1,"012345","0 2345"); + scrollTest(1,1,-1,"012345","0 2345"); + scrollTest(1,2,-1,"012345","02 345"); + scrollTest(5,1,-1,"012345","01234 "); + scrollTest(5,1,-1,"012345","01234 "); + } + public void testScrollNegative2() { + scrollTest(0,2,-1," 23 "," 23 "); + scrollTest(0,1,-1," 23 "," 23 "); + scrollTest(0,6,-1," 23 "," 23 "); + scrollTest(0,6,-6," 23 "," "); + scrollTest(0,6,-7," 23 "," "); + scrollTest(0,6,-8," 23 "," "); + scrollTest(0,6,-2," 23 ","23 "); + scrollTest(1,1,-1," 23 "," 23 "); + scrollTest(1,2,-1," 23 "," 2 3 "); + scrollTest(5,1,-1," 23 "," 23 "); + scrollTest(5,1,-1," 23 "," 23 "); + } + public void testScrollNegative3() { + scrollTest(1,5,-7,"012345","0 "); + } + public void testScrollPositive2() { + scrollTest(2,8,20, "0123456789", "01 "); + } + public void testScrollPositive() { + scrollTest(0,2,1, "012345", " 02345"); + scrollTest(0,2,2, "012345", " 2345"); + scrollTest(2,4,2, "012345", "01 23"); + scrollTest(2,4,2, "0123456", "01 236"); + scrollTest(0,7,6, "0123456", " 0"); + scrollTest(0,7,8, "0123456", " "); + scrollTest(0,7,9, "0123456", " "); + scrollTest(2,4,2, "0123456", "01 236"); + scrollTest(2,5,3, "0123456789", "01 23789"); + scrollTest(2,7,3, "0123456789", "01 23459"); + scrollTest(2,8,3, "0123456789", "01 23456"); + scrollTest(2,8,5, "0123456789", "01 234"); + scrollTest(2,8,9, "0123456789", "01 "); + scrollTest(0,10,9,"0123456789", " 0"); + scrollTest(0,6,6, "012345", " "); + } + public void testScrollFail() { + try { + scrollTest(5,2,-1,"012345","012345"); + fail(); // make sure you run this code with assertions enabled (vmargs: -ea) + } catch (RuntimeException e) { + } + try { + scrollTest(0,7,1,"012345"," "); + fail(); // make sure you run this code with assertions enabled (vmargs: -ea) + } catch (RuntimeException e) { + } + } + /** + * Makes a simple shift test + * @param line scroll start + * @param n number of lines to be scrolled + * @param shift amount of lines to be shifted + * @param start the original data + * @param result the expected result + */ + void scrollTest(int line,int n, int shift, String start,String result) { + ITerminalTextData term=makeITerminalTextData(); + fillSimple(term,start); + term.scroll(line, n, shift); + assertEqualsSimple(result, toSimple(term)); + + } +} diff --git a/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/model/SnapshotChangesTest.java b/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/model/SnapshotChangesTest.java new file mode 100644 index 00000000000..1fec8177063 --- /dev/null +++ b/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/model/SnapshotChangesTest.java @@ -0,0 +1,669 @@ +package org.eclipse.tm.internal.terminal.model; + +import org.eclipse.tm.terminal.model.ITerminalTextData; + +import junit.framework.TestCase; + +public class SnapshotChangesTest extends TestCase { + /** + * @param change + * @param expected a string of 0 and 1 (1 means changed) + */ + void assertChangedLines(ISnapshotChanges change, String expected) { + StringBuffer buffer=new StringBuffer(); + for (int line = 0; line < expected.length(); line++) { + if(change.hasLineChanged(line)) + buffer.append('1'); + else + buffer.append('0'); + } + assertEquals(expected, buffer.toString()); + } + + public void testSnapshotChanges() { + SnapshotChanges changes=new SnapshotChanges(1); + assertEquals(0, changes.getInterestWindowStartLine()); + assertEquals(0, changes.getInterestWindowSize()); + } + public void testSnapshotChangesWithWindow() { + SnapshotChanges changes=new SnapshotChanges(2,5); + assertEquals(2, changes.getInterestWindowStartLine()); + assertEquals(5, changes.getInterestWindowSize()); + } + + public void testIsInInterestWindowIntInt() { + SnapshotChanges changes=new SnapshotChanges(2,5); + assertFalse(changes.isInInterestWindow(0, 1)); + assertFalse(changes.isInInterestWindow(0, 2)); + assertTrue(changes.isInInterestWindow(0, 3)); + assertTrue(changes.isInInterestWindow(0, 4)); + assertTrue(changes.isInInterestWindow(0, 5)); + assertTrue(changes.isInInterestWindow(0, 6)); + assertTrue(changes.isInInterestWindow(0, 10)); + assertTrue(changes.isInInterestWindow(2, 5)); + assertTrue(changes.isInInterestWindow(6, 0)); + assertTrue(changes.isInInterestWindow(6, 1)); + assertTrue(changes.isInInterestWindow(6, 10)); + assertFalse(changes.isInInterestWindow(7, 0)); + assertFalse(changes.isInInterestWindow(7, 1)); + assertFalse(changes.isInInterestWindow(8, 10)); + } + public void testIsInInterestWindowIntIntNoWindow() { + SnapshotChanges changes=new SnapshotChanges(3); + for (int i = 0; i < 5; i++) { + for (int j = 0; j < 5; j++) { + assertTrue(changes.isInInterestWindow(i,j)); + } + } + } + + public void testIsInInterestWindowInt() { + SnapshotChanges changes=new SnapshotChanges(3,1); + assertFalse(changes.isInInterestWindow(0)); + assertFalse(changes.isInInterestWindow(1)); + assertFalse(changes.isInInterestWindow(2)); + assertTrue(changes.isInInterestWindow(3)); + assertFalse(changes.isInInterestWindow(4)); + assertFalse(changes.isInInterestWindow(5)); + } + + public void testIsInInterestWindowIntNoWindow() { + SnapshotChanges changes=new SnapshotChanges(3); + for (int i = 0; i < 10; i++) { + assertTrue(changes.isInInterestWindow(i)); + } + } + + public void testFitLineToWindow() { + SnapshotChanges changes=new SnapshotChanges(2,5); + assertEquals(2, changes.fitLineToWindow(0)); + assertEquals(2, changes.fitLineToWindow(1)); + assertEquals(2, changes.fitLineToWindow(2)); + assertEquals(3, changes.fitLineToWindow(3)); + + assertTrue(changes.isInInterestWindow(4)); + assertEquals(4, changes.fitLineToWindow(4)); + + assertTrue(changes.isInInterestWindow(5)); + assertEquals(5, changes.fitLineToWindow(5)); + + assertTrue(changes.isInInterestWindow(6)); + assertEquals(6, changes.fitLineToWindow(6)); + + assertFalse(changes.isInInterestWindow(7)); + // value undefined! + assertEquals(7, changes.fitLineToWindow(7)); + + assertFalse(changes.isInInterestWindow(8)); + // value undefined! + assertEquals(8, changes.fitLineToWindow(8)); + } + + public void testFitLineToWindowNoWindow() { + SnapshotChanges changes=new SnapshotChanges(5); + assertEquals(0, changes.fitLineToWindow(0)); + assertEquals(1, changes.fitLineToWindow(1)); + assertEquals(2, changes.fitLineToWindow(2)); + assertEquals(3, changes.fitLineToWindow(3)); + assertEquals(4, changes.fitLineToWindow(4)); + assertEquals(5, changes.fitLineToWindow(5)); + assertEquals(6, changes.fitLineToWindow(6)); + assertEquals(7, changes.fitLineToWindow(7)); + } + public void testFitSizeToWindow() { + SnapshotChanges changes=new SnapshotChanges(2,3); + assertFalse(changes.isInInterestWindow(0, 1)); + assertFalse(changes.isInInterestWindow(0, 2)); + assertTrue(changes.isInInterestWindow(0, 3)); + assertEquals(1, changes.fitSizeToWindow(0,3)); + assertEquals(2, changes.fitSizeToWindow(0,4)); + assertEquals(3, changes.fitSizeToWindow(0,5)); + assertEquals(3, changes.fitSizeToWindow(0,6)); + assertEquals(3, changes.fitSizeToWindow(0,7)); + assertEquals(3, changes.fitSizeToWindow(0,8)); + assertEquals(3, changes.fitSizeToWindow(0,9)); + assertEquals(3, changes.fitSizeToWindow(1,9)); + assertEquals(3, changes.fitSizeToWindow(2,9)); + assertEquals(3, changes.fitSizeToWindow(2,3)); + assertEquals(2, changes.fitSizeToWindow(2,2)); + assertEquals(1, changes.fitSizeToWindow(2,1)); + assertEquals(2, changes.fitSizeToWindow(3,9)); + assertEquals(2, changes.fitSizeToWindow(3,2)); + assertEquals(1, changes.fitSizeToWindow(3,1)); + assertEquals(2, changes.fitSizeToWindow(3,2)); + assertEquals(2, changes.fitSizeToWindow(3,3)); + assertEquals(1, changes.fitSizeToWindow(4,1)); + assertEquals(1, changes.fitSizeToWindow(4,2)); + assertFalse(changes.isInInterestWindow(5, 1)); + + } + public void testFitSizeToWindowNoWindow() { + SnapshotChanges changes=new SnapshotChanges(3); + assertEquals(1, changes.fitSizeToWindow(0,1)); + assertEquals(2, changes.fitSizeToWindow(0,2)); + assertEquals(3, changes.fitSizeToWindow(0,3)); + assertEquals(4, changes.fitSizeToWindow(0,4)); + assertEquals(5, changes.fitSizeToWindow(0,5)); + + assertEquals(5, changes.fitSizeToWindow(1,5)); + assertEquals(3, changes.fitSizeToWindow(2,3)); + assertEquals(2, changes.fitSizeToWindow(1,2)); + assertEquals(10, changes.fitSizeToWindow(5,10)); + } + + public void testMarkLineChanged() { + SnapshotChanges changes=new SnapshotChanges(2,3); + assertFalse(changes.hasChanged()); + changes.markLineChanged(0); + assertFalse(changes.hasChanged()); + changes.markLineChanged(1); + assertFalse(changes.hasChanged()); + changes.markLineChanged(2); + assertTrue(changes.hasChanged()); + + changes=new SnapshotChanges(2,3); + assertFalse(changes.hasChanged()); + changes.markLineChanged(3); + assertTrue(changes.hasChanged()); + + assertLineChange(false,2,3,0); + assertLineChange(false,2,3,1); + assertLineChange(true,2,3,2); + assertLineChange(true,2,3,3); + assertLineChange(true,2,3,4); + assertLineChange(false,2,3,5); + assertLineChange(false,2,3,6); + + assertLineChange(true,2,4,5); + } + void assertLineChange(boolean expected, int windowStart, int windowSize, int changedLine) { + SnapshotChanges changes=new SnapshotChanges(windowStart,windowSize); + assertFalse(changes.hasChanged()); + changes.markLineChanged(changedLine); + if(expected) { + assertEquals(changedLine, changes.getFirstChangedLine()); + assertEquals(changedLine, changes.getLastChangedLine()); + } else { + assertEquals(Integer.MAX_VALUE, changes.getFirstChangedLine()); + assertEquals(-1, changes.getLastChangedLine()); + + } + assertEquals(expected,changes.hasChanged()); + for (int i = 0; i < windowStart+windowSize+5; i++) { + boolean e= i==changedLine && i>=windowStart && iTIME) { + System.out.println(label+" "+(n*1000)/(System.currentTimeMillis()-t0)+" setChar()/sec "+ N); + break; + } + } + } + public void testPerformance1() { + ITerminalTextData term=new TerminalTextData(); + method1(term, "1 "); + } + public void testPerformance1a() { + ITerminalTextData term=new TerminalTextData(); + ITerminalTextDataSnapshot snapshot=term.makeSnapshot(); + method1(term, "1a"); + snapshot.updateSnapshot(true); + } + public void testPerformance1b() { + ITerminalTextData term=new TerminalTextData(); + ITerminalTextDataSnapshot snapshot=term.makeSnapshot(); + N=0; + snapshot.addListener(new ITerminalTextDataSnapshot.SnapshotOutOfDateListener(){ + public void snapshotOutOfDate(ITerminalTextDataSnapshot snapshot) { + N++; + }}); + method1(term, "1b"); + snapshot.updateSnapshot(true); + } + private void method1(ITerminalTextData term, String label) { + Style style=Style.getStyle(StyleColor.getStyleColor("fg"), StyleColor.getStyleColor("bg"), false, false, false, false); + initPerformance(term); + String s="This is a test string"; + long n=0; + long t0=System.currentTimeMillis(); + char[] chars=new char[term.getWidth()]; + for (int i = 0; i < 10000000; i++) { + for (int j = 0; j < chars.length; j++) { + chars[j]=s.charAt((i+j)%s.length()); + } + for (int line = 0; line < term.getHeight(); line++) { + term.setChars(line, 0, chars, style); + n+=chars.length; + } + if(System.currentTimeMillis()-t0>TIME) { + System.out.println(label+" "+(n*1000)/(System.currentTimeMillis()-t0)+" setChars()/sec "+ N); + break; + } + } + } + public void testPerformance2() { + TerminalTextData term=new TerminalTextData(); + Style style=Style.getStyle(StyleColor.getStyleColor("fg"), StyleColor.getStyleColor("bg"), false, false, false, false); + initPerformance(term); + TerminalTextData copy=new TerminalTextData(); + copy.copy(term); + + String s="This is a test string"; + long n=0; + long t0=System.currentTimeMillis(); + char[] chars=new char[term.getWidth()]; + for (int i = 0; i < 10000000; i++) { + for (int j = 0; j < chars.length; j++) { + chars[j]=s.charAt((i+j)%s.length()); + } + for (int line = 0; line < term.getHeight(); line++) { + term.setChars(line, 0, chars, 0,1,style); + copy.copy(term); + n+=1; + if(System.currentTimeMillis()-t0>TIME) { + System.out.println((n*1000)/(System.currentTimeMillis()-t0)+" copy()/sec"); + return; + } + } + } + } + public void testPerformance2a() { + TerminalTextData term=new TerminalTextData(); + ITerminalTextDataSnapshot snapshot=term.makeSnapshot(); + Style style=Style.getStyle(StyleColor.getStyleColor("fg"), StyleColor.getStyleColor("bg"), false, false, false, false); + initPerformance(term); + TerminalTextData copy=new TerminalTextData(); + copy.copy(term); + + String s="This is a test string"; + long n=0; + long t0=System.currentTimeMillis(); + char[] chars=new char[term.getWidth()]; + for (int i = 0; i < 10000000; i++) { + for (int j = 0; j < chars.length; j++) { + chars[j]=s.charAt((i+j)%s.length()); + } + for (int line = 0; line < term.getHeight(); line++) { + term.setChars(line, 0, chars, 0,1,style); + copy.copy(term); + n+=1; + if(System.currentTimeMillis()-t0>TIME) { + System.out.println((n*1000)/(System.currentTimeMillis()-t0)+" copy()/sec"); + return; + } + } + } + snapshot.updateSnapshot(true); + } + int N=0; + public void testPerformance2b() { + TerminalTextData term=new TerminalTextData(); + ITerminalTextDataSnapshot snapshot=term.makeSnapshot(); + N=0; + snapshot.addListener(new ITerminalTextDataSnapshot.SnapshotOutOfDateListener(){ + public void snapshotOutOfDate(ITerminalTextDataSnapshot snapshot) { + N++; + }}); + Style style=Style.getStyle(StyleColor.getStyleColor("fg"), StyleColor.getStyleColor("bg"), false, false, false, false); + initPerformance(term); + TerminalTextData copy=new TerminalTextData(); + copy.copy(term); + + String s="This is a test string"; + long n=0; + long t0=System.currentTimeMillis(); + char[] chars=new char[term.getWidth()]; + for (int i = 0; i < 10000000; i++) { + for (int j = 0; j < chars.length; j++) { + chars[j]=s.charAt((i+j)%s.length()); + } + for (int line = 0; line < term.getHeight(); line++) { + term.setChars(line, 0, chars, 0,1,style); + copy.copy(term); + n+=1; + if(System.currentTimeMillis()-t0>TIME) { + System.out.println((n*1000)/(System.currentTimeMillis()-t0)+" copy()/sec "+n); + return; + } + } + } + snapshot.updateSnapshot(true); + } + public void testPerformance3() { + TerminalTextData term=new TerminalTextData(); + Style style=Style.getStyle(StyleColor.getStyleColor("fg"), StyleColor.getStyleColor("bg"), false, false, false, false); + initPerformance(term); + TerminalTextData copy=new TerminalTextData(); + copy.copy(term); + String s="This is a test string"; + long n=0; + long t0=System.currentTimeMillis(); + char[] chars=new char[term.getWidth()]; + for (int i = 0; i < 10000000; i++) { + boolean[] linesToCopy=new boolean[term.getHeight()]; + for (int j = 0; j < chars.length; j++) { + chars[j]=s.charAt((i+j)%s.length()); + } + for (int line = 0; line < term.getHeight(); line++) { + term.setChars(line, 0, chars, 0,1,style); + linesToCopy[line]=true; + copy.copyLine(term,0,0); + linesToCopy[line]=false; + n+=1; + if(System.currentTimeMillis()-t0>TIME) { + System.out.println((n*1000)/(System.currentTimeMillis()-t0)+" copy()/sec"); + return; + } + } + } + } +} diff --git a/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/model/TerminalTextDataSnapshotTest.java b/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/model/TerminalTextDataSnapshotTest.java new file mode 100644 index 00000000000..d6e68ef4eeb --- /dev/null +++ b/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/model/TerminalTextDataSnapshotTest.java @@ -0,0 +1,1344 @@ +/******************************************************************************* + * Copyright (c) 2007 Wind River Systems, Inc. and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Michael Scharf (Wind River) - initial API and implementation + *******************************************************************************/ +package org.eclipse.tm.internal.terminal.model; + +import junit.framework.TestCase; + +import org.eclipse.tm.terminal.model.ITerminalTextData; +import org.eclipse.tm.terminal.model.ITerminalTextDataReadOnly; +import org.eclipse.tm.terminal.model.ITerminalTextDataSnapshot; +import org.eclipse.tm.terminal.model.Style; +import org.eclipse.tm.terminal.model.StyleColor; + +public class TerminalTextDataSnapshotTest extends TestCase { + String toMultiLineText(ITerminalTextDataReadOnly term) { + return TerminalTextTestHelper.toMultiLineText(term); + } + + protected ITerminalTextData makeITerminalTextData() { + return new TerminalTextData(); + } + + + public void testTerminalTextDataSnapshot() { + ITerminalTextData term=makeITerminalTextData(); + String s="12345\n" + + "abcde\n" + + "ABCDE\n" + + "vwxzy\n" + + "VWXYZ"; + TerminalTextTestHelper.fill(term,s); + + ITerminalTextDataSnapshot snapshot=term.makeSnapshot(); + snapshot.updateSnapshot(false); + + assertEquals(toMultiLineText(term), toMultiLineText(snapshot)); + + // new snapshots are fully changed + assertEquals(0,snapshot.getFirstChangedLine()); + assertEquals(term.getHeight()-1,snapshot.getLastChangedLine()); + for (int line = 0; line <= snapshot.getLastChangedLine(); line++) { + assertTrue(snapshot.hasLineChanged(line)); + } + // nothing has scrolled + assertEquals(0, snapshot.getScrollWindowSize()); + } + + public void testDetach() { + ITerminalTextData term=makeITerminalTextData(); + String s="12345\n" + + "abcde\n" + + "ABCDE\n" + + "vwxzy\n" + + "VWXYZ"; + TerminalTextTestHelper.fill(term,s); + + ITerminalTextDataSnapshot snapshot=term.makeSnapshot(); + snapshot.updateSnapshot(false); + + assertEquals(toMultiLineText(term),toMultiLineText(snapshot)); + snapshot.detach(); + // after detach changes to the term has no effect + term.setChar(0, 0, '?', null); + assertEquals(s, toMultiLineText(snapshot)); + term.setDimensions(2, 2); + assertEquals(s, toMultiLineText(snapshot)); + } + public void testIsOutOfDate() { + ITerminalTextData term=makeITerminalTextData(); + String s="12345\n" + + "abcde\n" + + "ABCDE\n" + + "vwxzy\n" + + "VWXYZ"; + TerminalTextTestHelper.fill(term,s); + + ITerminalTextDataSnapshot snapshot=term.makeSnapshot(); + assertTrue(snapshot.isOutOfDate()); + snapshot.updateSnapshot(false); + + assertFalse(snapshot.isOutOfDate()); + + // make a change and expect it to be changed + term.setChar(0, 0, '?', null); + assertTrue(snapshot.isOutOfDate()); + + snapshot.updateSnapshot(false); + assertFalse(snapshot.isOutOfDate()); + + // make a change and expect it to be changed + term.setChars(1, 1, new char[]{'?','!','.'},null); + assertTrue(snapshot.isOutOfDate()); + + snapshot.updateSnapshot(false); + assertFalse(snapshot.isOutOfDate()); + + // scroll + term.scroll(1, 2, -1); + assertTrue(snapshot.isOutOfDate()); + + snapshot.updateSnapshot(false); + assertFalse(snapshot.isOutOfDate()); + + // scroll + term.scroll(1, 2, 1); + assertTrue(snapshot.isOutOfDate()); + + snapshot.updateSnapshot(false); + assertFalse(snapshot.isOutOfDate()); + + // scroll + term.scroll(1, 2, -1); + assertTrue(snapshot.isOutOfDate()); + + snapshot.updateSnapshot(true); + assertFalse(snapshot.isOutOfDate()); + + // scroll + term.scroll(1, 2, 1); + assertTrue(snapshot.isOutOfDate()); + + snapshot.updateSnapshot(true); + assertFalse(snapshot.isOutOfDate()); + + // setDimensions + term.setDimensions(2, 2); + assertTrue(snapshot.isOutOfDate()); + + snapshot.updateSnapshot(false); + assertFalse(snapshot.isOutOfDate()); + + // setDimensions + term.setDimensions(20, 20); + assertTrue(snapshot.isOutOfDate()); + + snapshot.updateSnapshot(false); + assertFalse(snapshot.isOutOfDate()); + + } + ITerminalTextDataSnapshot snapshot(String text, ITerminalTextData term) { + TerminalTextTestHelper.fill(term,text); + ITerminalTextDataSnapshot snapshot=term.makeSnapshot(); + snapshot.updateSnapshot(false); + + return snapshot; + + } + public void testUpdateSnapshot() { + ITerminalTextData term=makeITerminalTextData(); + String s="12345\n" + + "abcde\n" + + "ABCDE\n" + + "vwxzy\n" + + "VWXYZ"; + TerminalTextTestHelper.fill(term,s); + String termString=toMultiLineText(term); + ITerminalTextDataSnapshot snapshot=term.makeSnapshot(); + snapshot.updateSnapshot(false); + + assertEquals(termString,toMultiLineText(snapshot)); + + // make changes and assert that the snapshot has not changed + // then update the snapshot and expect it to be the + // same as the changed terminal + + // make a change + term.setChar(0, 0, '?', null); + assertEquals(termString,toMultiLineText(snapshot)); + + snapshot.updateSnapshot(false); + termString=toMultiLineText(term); + assertEquals(termString,toMultiLineText(snapshot)); + + // make a change + term.setChars(1, 1, new char[]{'?','!','.'},null); + assertEquals(termString,toMultiLineText(snapshot)); + + snapshot.updateSnapshot(false); + termString=toMultiLineText(term); + assertEquals(termString,toMultiLineText(snapshot)); + + // scroll + term.scroll(1, 2, -1); + assertEquals(termString,toMultiLineText(snapshot)); + + snapshot.updateSnapshot(false); + termString=toMultiLineText(term); + assertEquals(termString,toMultiLineText(snapshot)); + + // scroll + term.scroll(1, 2, 1); + assertEquals(termString,toMultiLineText(snapshot)); + + snapshot.updateSnapshot(false); + termString=toMultiLineText(term); + assertEquals(termString,toMultiLineText(snapshot)); + + // scroll + term.scroll(1, 2, -1); + assertEquals(termString,toMultiLineText(snapshot)); + + snapshot.updateSnapshot(true); + termString=toMultiLineText(term); + assertEquals(termString,toMultiLineText(snapshot)); + + // scroll + term.scroll(1, 2, 1); + assertEquals(termString,toMultiLineText(snapshot)); + + snapshot.updateSnapshot(true); + termString=toMultiLineText(term); + assertEquals(termString,toMultiLineText(snapshot)); + + // set dimensions + term.setDimensions(2, 2); + assertEquals(termString,toMultiLineText(snapshot)); + + snapshot.updateSnapshot(false); + termString=toMultiLineText(term); + assertEquals(termString,toMultiLineText(snapshot)); + + // set dimensions + term.setDimensions(20, 20); + assertEquals(termString,toMultiLineText(snapshot)); + + snapshot.updateSnapshot(false); + termString=toMultiLineText(term); + assertEquals(termString,toMultiLineText(snapshot)); + } + + public void testMaxSize() { + String s= + "111\n" + + "222\n" + + "333\n" + + "444\n" + + "555"; + ITerminalTextData term=makeITerminalTextData(); + term.setMaxHeight(8); + TerminalTextTestHelper.fill(term, s); + ITerminalTextDataSnapshot snapshot=term.makeSnapshot(); + snapshot.updateSnapshot(false); + + term.addLine(); + assertTrue(snapshot.isOutOfDate()); + snapshot.updateSnapshot(false); + assertEquals(toMultiLineText(term), toMultiLineText(snapshot)); + + term.addLine(); + assertTrue(snapshot.isOutOfDate()); + snapshot.updateSnapshot(false); + assertEquals(toMultiLineText(term), toMultiLineText(snapshot)); + + term.addLine(); + assertTrue(snapshot.isOutOfDate()); + snapshot.updateSnapshot(false); + assertEquals(toMultiLineText(term), toMultiLineText(snapshot)); + + term.addLine(); + assertTrue(snapshot.isOutOfDate()); + snapshot.updateSnapshot(false); + assertEquals(toMultiLineText(term), toMultiLineText(snapshot)); + + term.addLine(); + assertTrue(snapshot.isOutOfDate()); + snapshot.updateSnapshot(false); + assertEquals(toMultiLineText(term), toMultiLineText(snapshot)); + + } + + + public void testGetChar() { + ITerminalTextData term=makeITerminalTextData(); + String s="12345\n" + + "abcde\n" + + "ABCDE\n" + + "vwxzy\n" + + "VWXYZ"; + TerminalTextTestHelper.fill(term,s); + ITerminalTextData termUnchanged=makeITerminalTextData(); + TerminalTextTestHelper.fill(termUnchanged,s); + + ITerminalTextDataSnapshot snapshot=term.makeSnapshot(); + snapshot.updateSnapshot(false); + + for (int line = 0; line < snapshot.getHeight(); line++) { + for (int column = 0; column < snapshot.getWidth(); column++) { + assertEquals(term.getChar(line, column),snapshot.getChar(line, column)); + } + } + // make a change + term.setChar(0, 0, '?', null); + // check against unchanged data + for (int line = 0; line < snapshot.getHeight(); line++) { + for (int column = 0; column < snapshot.getWidth(); column++) { + assertEquals(termUnchanged.getChar(line, column),snapshot.getChar(line, column)); + } + } + // update and compare against the terminal + snapshot.updateSnapshot(true); + for (int line = 0; line < snapshot.getHeight(); line++) { + for (int column = 0; column < snapshot.getWidth(); column++) { + assertEquals(term.getChar(line, column),snapshot.getChar(line, column)); + } + } + + } + + public void testGetHeight() { + ITerminalTextData term=makeITerminalTextData(); + String s="12345\n" + + "abcde\n" + + "ABCDE\n" + + "vwxzy\n" + + "VWXYZ"; + TerminalTextTestHelper.fill(term,s); + + ITerminalTextDataSnapshot snapshot=term.makeSnapshot(); + snapshot.updateSnapshot(false); + + int expectedHeight=term.getHeight(); + assertEquals(expectedHeight, snapshot.getHeight()); + term.setDimensions(term.getHeight()-1, term.getWidth()); + assertEquals(expectedHeight, snapshot.getHeight()); + + // + snapshot.updateSnapshot(false); + expectedHeight=term.getHeight(); + assertEquals(expectedHeight, snapshot.getHeight()); + term.setDimensions(term.getHeight()-1, term.getWidth()); + assertEquals(expectedHeight, snapshot.getHeight()); + } +// +// public void testGetLineSegments() { +// fail("Not yet implemented"); +// } +// + public void testGetStyle() { + ITerminalTextData term=makeITerminalTextData(); + Style style=Style.getStyle(StyleColor.getStyleColor("fg"), StyleColor.getStyleColor("bg"), false, false, false, false); + term.setDimensions(6, 3); + for (int line = 0; line < term.getHeight(); line++) { + for (int column = 0; column < term.getWidth(); column++) { + char c=(char)('a'+column+line); + term.setChar(line, column, c, style.setForground(StyleColor.getStyleColor(""+c))); + } + } + ITerminalTextDataSnapshot snapshot=term.makeSnapshot(); + snapshot.updateSnapshot(false); + + + for (int line = 0; line < term.getHeight(); line++) { + for (int column = 0; column < term.getWidth(); column++) { + char c=(char)('a'+column+line); + assertSame(style.setForground(StyleColor.getStyleColor(""+c)), snapshot.getStyle(line, column)); + } + } + + } + + public void testGetWidth() { + ITerminalTextData term=makeITerminalTextData(); + String s="12345\n" + + "abcde\n" + + "ABCDE\n" + + "vwxzy\n" + + "VWXYZ"; + TerminalTextTestHelper.fill(term,s); + + ITerminalTextDataSnapshot snapshot=term.makeSnapshot(); + snapshot.updateSnapshot(false); + + int expectedWidth=term.getWidth(); + assertEquals(expectedWidth, snapshot.getWidth()); + term.setDimensions(term.getHeight(), term.getWidth()-1); + assertEquals(expectedWidth, snapshot.getWidth()); + + // + snapshot.updateSnapshot(false); + expectedWidth=term.getWidth(); + assertEquals(expectedWidth, snapshot.getWidth()); + term.setDimensions(term.getHeight(), term.getWidth()-1); + assertEquals(expectedWidth, snapshot.getWidth()); + } + + public void testGetFirstChangedLine() { + ITerminalTextData term=makeITerminalTextData(); + String s="12345\n" + + "abcde\n" + + "ABCDE\n" + + "vwxzy\n" + + "VWXYZ"; + ITerminalTextDataSnapshot snapshot=snapshot(s,term); + + + assertEquals(0, snapshot.getFirstChangedLine()); + + // if nothing has changed the first changed line i height + snapshot.updateSnapshot(false); + assertEquals(Integer.MAX_VALUE, snapshot.getFirstChangedLine()); + + snapshot=snapshot(s,term); + term.setChar(0, 0, 'x', null); + snapshot.updateSnapshot(false); + assertEquals(0, snapshot.getFirstChangedLine()); + + snapshot=snapshot(s,term); + term.setChar(3, 0, 'x', null); + term.setChar(4, 0, 'x', null); + snapshot.updateSnapshot(false); + assertEquals(3, snapshot.getFirstChangedLine()); + + snapshot=snapshot(s,term); + term.scroll(0, 1, -1); + snapshot.updateSnapshot(false); + assertEquals(0, snapshot.getFirstChangedLine()); + + snapshot=snapshot(s,term); + term.scroll(2, 2, -1); + snapshot.updateSnapshot(false); + assertEquals(2, snapshot.getFirstChangedLine()); + + // when scrolling the end of the region 'has changed' + snapshot=snapshot(s,term); + term.scroll(2, 2, -1); + snapshot.updateSnapshot(true); + assertEquals(3, snapshot.getFirstChangedLine()); + + // when scrolling the end of the region 'has changed' + snapshot=snapshot(s,term); + term.scroll(2, 2, -1); + term.setChar(1, 0, 'x', null); + snapshot.updateSnapshot(true); + assertEquals(1, snapshot.getFirstChangedLine()); + + } + public void testGetLastChangedLine() { + ITerminalTextData term=makeITerminalTextData(); + String s="12345\n" + + "abcde\n" + + "ABCDE\n" + + "vwxzy\n" + + "VWXYZ"; + ITerminalTextDataSnapshot snapshot=snapshot(s,term); + + + assertEquals(4, snapshot.getLastChangedLine()); + + // if nothing has changed the first changed line i height + snapshot.updateSnapshot(false); + assertEquals(-1, snapshot.getLastChangedLine()); + + snapshot=snapshot(s,term); + term.setChar(0, 0, 'x', null); + snapshot.updateSnapshot(false); + assertEquals(0, snapshot.getLastChangedLine()); + + snapshot=snapshot(s,term); + term.cleanLine(1); + snapshot.updateSnapshot(false); + assertEquals(1, snapshot.getLastChangedLine()); + + snapshot=snapshot(s,term); + term.setChar(3, 0, 'x', null); + term.setChar(4, 0, 'x', null); + snapshot.updateSnapshot(false); + assertEquals(4, snapshot.getLastChangedLine()); + + snapshot=snapshot(s,term); + term.scroll(0, 1, -1); + snapshot.updateSnapshot(false); + assertEquals(0, snapshot.getLastChangedLine()); + + snapshot=snapshot(s,term); + term.scroll(2, 2, -1); + snapshot.updateSnapshot(false); + assertEquals(3, snapshot.getLastChangedLine()); + + // when scrolling the end of the region 'has changed' + snapshot=snapshot(s,term); + term.scroll(2, 2, -1); + snapshot.updateSnapshot(true); + assertEquals(3, snapshot.getLastChangedLine()); + + // when scrolling the end of the region 'has changed' + snapshot=snapshot(s,term); + term.scroll(2, 2, -1); + term.setChar(1, 0, 'x', null); + snapshot.updateSnapshot(true); + assertEquals(3, snapshot.getLastChangedLine()); + + } + /** + * @param snapshot + * @param expected a string of 0 and 1 (1 means changed) + */ + void assertChangedLines(ITerminalTextDataSnapshot snapshot, String expected) { + assertEquals(expected.length(),snapshot.getHeight()); + StringBuffer buffer=new StringBuffer(); + for (int line = 0; line < expected.length(); line++) { + if(snapshot.hasLineChanged(line)) + buffer.append('1'); + else + buffer.append('0'); + } + assertEquals(expected, buffer.toString()); + } + public void testHasLineChangedScroll() { + ITerminalTextData term=makeITerminalTextData(); + String s="00\n" + + "11\n" + + "22\n" + + "33\n" + + "44\n" + + "55\n" + + "66\n" + + "77\n" + + "88\n" + + "99"; + ITerminalTextDataSnapshot snapshot=snapshot(s,term); + + term.scroll(2,3,-1); + snapshot.updateSnapshot(true); + assertChangedLines(snapshot, "0000100000"); + + snapshot=snapshot(s,term); + term.scroll(2,3,-2); + snapshot.updateSnapshot(true); + assertChangedLines(snapshot, "0001100000"); + + snapshot=snapshot(s,term); + term.scroll(2,4,-1); + term.scroll(2,4,-1); + snapshot.updateSnapshot(true); + assertChangedLines(snapshot, "0000110000"); + + term.scroll(2,3,1); + snapshot.updateSnapshot(true); + assertChangedLines(snapshot, "0011100000"); + + snapshot=snapshot(s,term); + term.scroll(2,3,2); + snapshot.updateSnapshot(true); + assertChangedLines(snapshot, "0011100000"); + + snapshot=snapshot(s,term); + term.scroll(2,4,1); + term.scroll(2,4,1); + snapshot.updateSnapshot(true); + assertChangedLines(snapshot, "0011110000"); + + + snapshot=snapshot(s,term); + term.scroll(2,3,-1); + snapshot.updateSnapshot(false); + assertChangedLines(snapshot, "0011100000"); + + snapshot=snapshot(s,term); + term.scroll(2,3,-2); + snapshot.updateSnapshot(false); + assertChangedLines(snapshot, "0011100000"); + + snapshot=snapshot(s,term); + term.scroll(2,4,-1); + term.scroll(2,4,-1); + snapshot.updateSnapshot(false); + assertChangedLines(snapshot, "0011110000"); + } + public void testMultiScrollWithDifferentSizes() { + ITerminalTextData term=makeITerminalTextData(); + String s="00\n" + + "11\n" + + "22\n" + + "33\n" + + "44\n" + + "55\n" + + "66\n" + + "77\n" + + "88\n" + + "99"; + ITerminalTextDataSnapshot snapshot; + + snapshot=snapshot(s,term); + term.scroll(2,6,-1); + term.scroll(2,5,-1); + snapshot.updateSnapshot(false); + assertChangedLines(snapshot, "0011111100"); + assertEquals(2, snapshot.getFirstChangedLine()); + assertEquals(7, snapshot.getLastChangedLine()); + assertEquals(0, snapshot.getScrollWindowSize()); + assertEquals(0, snapshot.getScrollWindowStartLine()); + assertEquals(0, snapshot.getScrollWindowShift()); + + // scrolls with different ranges cause no scroll + // optimization + snapshot=snapshot(s,term); + term.scroll(2,6,-1); + term.scroll(2,5,-1); + snapshot.updateSnapshot(true); + assertChangedLines(snapshot, "0011111100"); + assertEquals(2, snapshot.getFirstChangedLine()); + assertEquals(7, snapshot.getLastChangedLine()); + assertEquals(0, snapshot.getScrollWindowShift()); + assertEquals(0, snapshot.getScrollWindowSize()); + assertEquals(0, snapshot.getScrollWindowStartLine()); + } + public void testHasLineChanged() { + ITerminalTextData term=makeITerminalTextData(); + String s="000000\n" + + "111111\n" + + "222222\n" + + "333333\n" + + "444444\n" + + "555555\n" + + "666666\n" + + "777777\n" + + "888888\n" + + "999999"; + ITerminalTextDataSnapshot snapshot; + + snapshot=snapshot(s,term); + term.scroll(2,3,-1); + term.setChar(7, 0, '.', null); + snapshot.updateSnapshot(true); + assertChangedLines(snapshot, "0000100100"); + + snapshot=snapshot(s,term); + term.scroll(2,3,-2); + term.setChar(9, 0, '.', null); + term.setChars(0, 0, new char[]{'.','!'}, null); + snapshot.updateSnapshot(true); + assertChangedLines(snapshot, "1001100001"); + + snapshot=snapshot(s,term); + term.scroll(2,4,-1); + term.scroll(2,4,-1); + term.setChars(2, 2, new char[]{'.','!','*'},1,1, null); + snapshot.updateSnapshot(true); + assertChangedLines(snapshot, "0010110000"); + + snapshot=snapshot(s,term); + term.scroll(2,7,-1); + term.setChar(5, 2, '.', null); + term.scroll(2,7,-2); + snapshot.updateSnapshot(true); + assertChangedLines(snapshot, "0001001110"); + + + snapshot=snapshot(s,term); + term.scroll(2,7,-1); + term.setChar(5, 2, '.', null); + term.scroll(2,7,-2); + snapshot.updateSnapshot(false); + assertChangedLines(snapshot, "0011111110"); + + } + + public void testScroll() { + ITerminalTextData term=makeITerminalTextData(); + String s="00\n" + + "11\n" + + "22\n" + + "33\n" + + "44\n" + + "55\n" + + "66\n" + + "77\n" + + "88\n" + + "99"; + ITerminalTextDataSnapshot snapshot=snapshot(s,term); + + term.scroll(2,3,-1); + snapshot.updateSnapshot(true); + assertEquals(2, snapshot.getScrollWindowStartLine()); + assertEquals(3, snapshot.getScrollWindowSize()); + assertEquals(-1, snapshot.getScrollWindowShift()); + assertEquals(4, snapshot.getFirstChangedLine()); + assertEquals(4, snapshot.getLastChangedLine()); + + term.scroll(2,3,-2); + snapshot.updateSnapshot(true); + assertEquals(2, snapshot.getScrollWindowStartLine()); + assertEquals(3, snapshot.getScrollWindowSize()); + assertEquals(-2, snapshot.getScrollWindowShift()); + assertEquals(3, snapshot.getFirstChangedLine()); + assertEquals(4, snapshot.getLastChangedLine()); + + term.scroll(2,4,-1); + term.scroll(2,4,-1); + snapshot.updateSnapshot(true); + assertEquals(2, snapshot.getScrollWindowStartLine()); + assertEquals(4, snapshot.getScrollWindowSize()); + assertEquals(-2, snapshot.getScrollWindowShift()); + assertEquals(4, snapshot.getFirstChangedLine()); + assertEquals(5, snapshot.getLastChangedLine()); + + + snapshot=snapshot(s,term); + term.scroll(2,3,-1); + snapshot.updateSnapshot(false); + assertEquals(0, snapshot.getScrollWindowStartLine()); + assertEquals(0, snapshot.getScrollWindowSize()); + assertEquals(0, snapshot.getScrollWindowShift()); + assertEquals(2, snapshot.getFirstChangedLine()); + assertEquals(4, snapshot.getLastChangedLine()); + + } + public void testDisjointScroll() { + ITerminalTextData term=makeITerminalTextData(); + String s="000000\n" + + "111111\n" + + "222222\n" + + "333333\n" + + "444444\n" + + "555555\n" + + "666666\n" + + "777777\n" + + "888888\n" + + "999999"; + ITerminalTextDataSnapshot snapshot; + + snapshot=snapshot(s,term); + term.scroll(0,2,-1); + term.scroll(4,2,-1); + snapshot.updateSnapshot(true); + assertChangedLines(snapshot, "1100110000"); + assertEquals(0, snapshot.getScrollWindowStartLine()); + assertEquals(0, snapshot.getScrollWindowSize()); + assertEquals(0, snapshot.getScrollWindowShift()); + + snapshot=snapshot(s,term); + term.scroll(0,3,-1); + term.scroll(2,2,-2); + snapshot.updateSnapshot(true); + assertChangedLines(snapshot, "1111000000"); + assertEquals(0, snapshot.getScrollWindowStartLine()); + assertEquals(0, snapshot.getScrollWindowSize()); + assertEquals(0, snapshot.getScrollWindowShift()); + + snapshot=snapshot(s,term); + term.scroll(0,3,-1); + term.scroll(2,2,-2); + term.scroll(0,3,-1); + snapshot.updateSnapshot(true); + assertChangedLines(snapshot, "1111000000"); + assertEquals(0, snapshot.getScrollWindowStartLine()); + assertEquals(0, snapshot.getScrollWindowSize()); + assertEquals(0, snapshot.getScrollWindowShift()); + + snapshot=snapshot(s,term); + term.scroll(0,3,-1); + term.scroll(2,2,-2); + term.scroll(0,3,-10); + snapshot.updateSnapshot(true); + assertChangedLines(snapshot, "1111000000"); + assertEquals(0, snapshot.getScrollWindowStartLine()); + assertEquals(0, snapshot.getScrollWindowSize()); + assertEquals(0, snapshot.getScrollWindowShift()); + + snapshot=snapshot(s,term); + term.scroll(1,3,-1); + term.scroll(1,3,1); + snapshot.updateSnapshot(true); + assertChangedLines(snapshot, "0111000000"); + assertEquals(0, snapshot.getScrollWindowStartLine()); + assertEquals(0, snapshot.getScrollWindowSize()); + assertEquals(0, snapshot.getScrollWindowShift()); + } + public void testResize() { + ITerminalTextData term=makeITerminalTextData(); + String s="000000\n" + + "111111\n" + + "222222\n" + + "333333"; + ITerminalTextDataSnapshot snapshot; + + snapshot=snapshot(s,term); + term.setDimensions(term.getHeight(), term.getWidth()+1); + snapshot.updateSnapshot(true); + assertChangedLines(snapshot, "1111"); + assertEquals(0, snapshot.getFirstChangedLine()); + assertEquals(3, snapshot.getLastChangedLine()); + assertEquals(0, snapshot.getScrollWindowStartLine()); + assertEquals(0, snapshot.getScrollWindowSize()); + assertEquals(0, snapshot.getScrollWindowShift()); + + snapshot=snapshot(s,term); + term.setDimensions(term.getHeight()+1, term.getWidth()); + snapshot.updateSnapshot(true); + assertChangedLines(snapshot, "11111"); + assertEquals(0, snapshot.getFirstChangedLine()); + assertEquals(4, snapshot.getLastChangedLine()); + assertEquals(0, snapshot.getScrollWindowStartLine()); + assertEquals(0, snapshot.getScrollWindowSize()); + assertEquals(0, snapshot.getScrollWindowShift()); + + snapshot=snapshot(s,term); + term.setDimensions(term.getHeight()-1, term.getWidth()); + snapshot.updateSnapshot(true); + assertChangedLines(snapshot, "111"); + assertEquals(0, snapshot.getFirstChangedLine()); + assertEquals(2, snapshot.getLastChangedLine()); + assertEquals(0, snapshot.getScrollWindowStartLine()); + assertEquals(0, snapshot.getScrollWindowSize()); + assertEquals(0, snapshot.getScrollWindowShift()); + + snapshot=snapshot(s,term); + term.setDimensions(0, 0); + snapshot.updateSnapshot(true); + assertChangedLines(snapshot, ""); + assertEquals(0, snapshot.getFirstChangedLine()); + assertEquals(-1, snapshot.getLastChangedLine()); + assertEquals(0, snapshot.getScrollWindowStartLine()); + assertEquals(0, snapshot.getScrollWindowSize()); + assertEquals(0, snapshot.getScrollWindowShift()); + + } + public void testResizeAfterScroll() { + ITerminalTextData term=makeITerminalTextData(); + String s="000000\n" + + "111111\n" + + "222222\n" + + "333333\n" + + "444444\n" + + "555555\n" + + "666666\n" + + "777777\n" + + "888888\n" + + "999999"; + ITerminalTextDataSnapshot snapshot; + + snapshot=snapshot(s,term); + term.scroll(1,2,-1); + term.setDimensions(5, 4); + snapshot.updateSnapshot(true); + assertChangedLines(snapshot, "11111"); + assertEquals(0, snapshot.getScrollWindowStartLine()); + assertEquals(0, snapshot.getScrollWindowSize()); + assertEquals(0, snapshot.getScrollWindowShift()); + + snapshot=snapshot(s,term); + term.scroll(1,2,-1); + term.setDimensions(7, 2); + term.scroll(4,2,-1); + snapshot.updateSnapshot(true); + assertChangedLines(snapshot, "1111111"); + assertEquals(0, snapshot.getScrollWindowStartLine()); + assertEquals(0, snapshot.getScrollWindowSize()); + assertEquals(0, snapshot.getScrollWindowShift()); + snapshot=snapshot(s,term); + + term.scroll(1,2,-1); + term.setDimensions(term.getHeight(),term.getWidth()+1); + snapshot.updateSnapshot(true); + assertChangedLines(snapshot, "1111111111"); + assertEquals(0, snapshot.getScrollWindowStartLine()); + assertEquals(0, snapshot.getScrollWindowSize()); + assertEquals(0, snapshot.getScrollWindowShift()); + } + public void testScrollAfterResize() { + ITerminalTextData term=makeITerminalTextData(); + String s="000000\n" + + "111111\n" + + "222222\n" + + "333333\n" + + "444444\n" + + "555555\n" + + "666666\n" + + "777777\n" + + "888888\n" + + "999999"; + ITerminalTextDataSnapshot snapshot; + + snapshot=snapshot(s,term); + term.setDimensions(14, 6); + term.scroll(0,14,-1); + snapshot.updateSnapshot(true); + assertChangedLines(snapshot, "11111111111111"); + assertEquals(0, snapshot.getScrollWindowStartLine()); + assertEquals(0, snapshot.getScrollWindowSize()); + assertEquals(0, snapshot.getScrollWindowShift()); + } + private final class SnapshotListener implements ITerminalTextDataSnapshot.SnapshotOutOfDateListener { + int N; + public void snapshotOutOfDate(ITerminalTextDataSnapshot snapshot) { + N++; + } + public void reset() { + N=0; + } + } + + public void testAddListener() { + ITerminalTextData term=makeITerminalTextData(); + String s="12345\n" + + "abcde\n" + + "ABCDE\n" + + "vwxzy\n" + + "VWXYZ"; + TerminalTextTestHelper.fill(term,s); + + ITerminalTextDataSnapshot snapshot=term.makeSnapshot(); + snapshot.updateSnapshot(false); + + SnapshotListener listener=new SnapshotListener(); + snapshot.addListener(listener); + assertEquals(0, listener.N); + + // make a change and expect it to be changed + term.setChar(0, 0, '?', null); + assertEquals(1, listener.N); + term.setChar(1, 1, '?', null); + assertEquals(1, listener.N); + + snapshot.updateSnapshot(false); + assertEquals(1, listener.N); + listener.reset(); + + // make a change and expect it to be changed + term.setChars(1, 1, new char[]{'?','!','.'},null); + assertEquals(1, listener.N); + term.setChars(2, 1, new char[]{'?','!','.'},null); + assertEquals(1, listener.N); + + snapshot.updateSnapshot(false); + assertEquals(1, listener.N); + listener.reset(); + + // scroll + term.scroll(1, 2, -1); + assertEquals(1, listener.N); + term.scroll(1, 2, -1); + assertEquals(1, listener.N); + + snapshot.updateSnapshot(false); + assertEquals(1, listener.N); + listener.reset(); + + // scroll + term.scroll(1, 2, 1); + assertEquals(1, listener.N); + term.scroll(1, 2, 1); + assertEquals(1, listener.N); + + snapshot.updateSnapshot(false); + assertEquals(1, listener.N); + listener.reset(); + + // scroll + term.scroll(1, 2, -1); + assertEquals(1, listener.N); + term.scroll(1, 2, -1); + assertEquals(1, listener.N); + + snapshot.updateSnapshot(false); + assertEquals(1, listener.N); + listener.reset(); + + // scroll + term.scroll(1, 2, 1); + assertEquals(1, listener.N); + + snapshot.updateSnapshot(false); + assertEquals(1, listener.N); + listener.reset(); + + // setDimensions + term.setDimensions(2, 2); + assertEquals(1, listener.N); + + snapshot.updateSnapshot(false); + assertEquals(1, listener.N); + listener.reset(); + + // setDimensions + term.setDimensions(20, 20); + assertEquals(1, listener.N); + + snapshot.updateSnapshot(false); + assertFalse(snapshot.isOutOfDate()); + } + + public void testRemoveListener() { + ITerminalTextData term=makeITerminalTextData(); + String s="12345\n" + + "abcde\n" + + "ABCDE\n" + + "vwxzy\n" + + "VWXYZ"; + TerminalTextTestHelper.fill(term,s); + + ITerminalTextDataSnapshot snapshot=term.makeSnapshot(); + snapshot.updateSnapshot(false); + + SnapshotListener listener1=new SnapshotListener(); + SnapshotListener listener2=new SnapshotListener(); + SnapshotListener listener3=new SnapshotListener(); + snapshot.addListener(listener1); + snapshot.addListener(listener2); + snapshot.addListener(listener3); + assertEquals(0, listener1.N); + + // make a change and expect it to be changed + term.setChar(0, 0, '?', null); + assertEquals(1, listener1.N); + assertEquals(1, listener2.N); + assertEquals(1, listener3.N); + term.setChar(1, 1, '?', null); + assertEquals(1, listener1.N); + assertEquals(1, listener2.N); + assertEquals(1, listener3.N); + + snapshot.updateSnapshot(false); + assertEquals(1, listener1.N); + assertEquals(1, listener2.N); + assertEquals(1, listener3.N); + listener1.reset(); + listener2.reset(); + listener3.reset(); + + // make a change and expect it to be changed + term.setChars(1, 1, new char[]{'?','!','.'},null); + assertEquals(1, listener1.N); + assertEquals(1, listener2.N); + assertEquals(1, listener3.N); + term.setChars(2, 1, new char[]{'?','!','.'},null); + assertEquals(1, listener1.N); + assertEquals(1, listener2.N); + assertEquals(1, listener3.N); + + + snapshot.updateSnapshot(false); + assertEquals(1, listener1.N); + assertEquals(1, listener2.N); + assertEquals(1, listener3.N); + + listener1.reset(); + listener2.reset(); + listener3.reset(); + + snapshot.removeListener(listener2); + + // scroll + term.scroll(1, 2, -1); + assertEquals(1, listener1.N); + assertEquals(0, listener2.N); + assertEquals(1, listener3.N); + + term.scroll(1, 2, -1); + assertEquals(1, listener1.N); + assertEquals(0, listener2.N); + assertEquals(1, listener3.N); + + + snapshot.updateSnapshot(false); + assertEquals(1, listener1.N); + assertEquals(0, listener2.N); + assertEquals(1, listener3.N); + + snapshot.addListener(listener2); + listener1.reset(); + listener2.reset(); + listener3.reset(); + + + snapshot.removeListener(listener3); + // scroll + term.scroll(1, 2, 1); + assertEquals(1, listener1.N); + assertEquals(1, listener2.N); + assertEquals(0, listener3.N); + + term.scroll(1, 2, 1); + assertEquals(1, listener1.N); + assertEquals(1, listener2.N); + assertEquals(0, listener3.N); + + + snapshot.updateSnapshot(false); + assertEquals(1, listener1.N); + assertEquals(1, listener2.N); + assertEquals(0, listener3.N); + + snapshot.addListener(listener3); + listener1.reset(); + listener2.reset(); + listener3.reset(); + + // add listener multiple times + snapshot.addListener(listener3); + + // scroll + term.scroll(1, 2, -1); + assertEquals(1, listener1.N); + assertEquals(1, listener2.N); + assertEquals(2, listener3.N); + + term.scroll(1, 2, -1); + assertEquals(1, listener1.N); + assertEquals(1, listener2.N); + assertEquals(2, listener3.N); + + + snapshot.updateSnapshot(false); + assertEquals(1, listener1.N); + assertEquals(1, listener2.N); + assertEquals(2, listener3.N); + + listener1.reset(); + listener2.reset(); + listener3.reset(); + // remove the duplicate listener + snapshot.removeListener(listener3); + + + // scroll + term.scroll(1, 2, 1); + assertEquals(1, listener1.N); + assertEquals(1, listener2.N); + assertEquals(1, listener3.N); + + + snapshot.updateSnapshot(false); + assertEquals(1, listener1.N); + assertEquals(1, listener2.N); + assertEquals(1, listener3.N); + + listener1.reset(); + listener2.reset(); + listener3.reset(); + + + // setDimensions + term.setDimensions(2, 2); + assertEquals(1, listener1.N); + assertEquals(1, listener2.N); + assertEquals(1, listener3.N); + + + snapshot.updateSnapshot(false); + assertEquals(1, listener1.N); + assertEquals(1, listener2.N); + assertEquals(1, listener3.N); + + listener1.reset(); + listener2.reset(); + listener3.reset(); + + + // setDimensions + term.setDimensions(20, 20); + assertEquals(1, listener1.N); + assertEquals(1, listener2.N); + assertEquals(1, listener3.N); + + + snapshot.updateSnapshot(false); + assertFalse(snapshot.isOutOfDate()); + } + public void testWindowOfInterest() { + ITerminalTextData term=makeITerminalTextData(); + TerminalTextTestHelper.fillSimple(term,"0123456789"); + ITerminalTextDataSnapshot snapshot=term.makeSnapshot(); + snapshot.updateSnapshot(false); + snapshot.setInterestWindow(7, 4); + snapshot.setInterestWindow(9, 4); + snapshot.updateSnapshot(false); + } + public void testWindowOfInterest2() { + ITerminalTextData term=makeITerminalTextData(); + TerminalTextTestHelper.fillSimple(term,"0123456789"); + ITerminalTextDataSnapshot snapshot=term.makeSnapshot(); + snapshot.updateSnapshot(false); + term.scroll(7, 3,-1); + snapshot.setInterestWindow(9, 4); + snapshot.updateSnapshot(false); + } + public void testAddLine() { + ITerminalTextData term=makeITerminalTextData(); + TerminalTextTestHelper.fillSimple(term,"0123456789"); + ITerminalTextDataSnapshot snapshot=term.makeSnapshot(); + term.setMaxHeight(20); + snapshot.updateSnapshot(false); + assertEquals(10,term.getHeight()); + assertEquals(20,term.getMaxHeight()); + assertFalse(snapshot.isOutOfDate()); + term.addLine(); + assertTrue(snapshot.isOutOfDate()); + assertEquals(11,term.getHeight()); + assertEquals(10,snapshot.getHeight()); + snapshot.updateSnapshot(false); + assertEquals(11,term.getHeight()); + assertEquals(11,snapshot.getHeight()); + assertEquals(20,term.getMaxHeight()); + + term.addLine(); + term.addLine(); + assertEquals(11,snapshot.getHeight()); + assertEquals(13,term.getHeight()); + assertTrue(snapshot.isOutOfDate()); + snapshot.updateSnapshot(false); + assertEquals(13,snapshot.getHeight()); + assertEquals(13,term.getHeight()); + assertEquals(20,term.getMaxHeight()); + } + public void testHasDimensionsChanged() { + ITerminalTextData term=makeITerminalTextData(); + TerminalTextTestHelper.fillSimple(term,"0123456789"); + ITerminalTextDataSnapshot snapshot=term.makeSnapshot(); + term.setMaxHeight(20); + snapshot.setInterestWindow(3, 4); + snapshot.updateSnapshot(false); + assertEquals(10,term.getHeight()); + assertEquals(20,term.getMaxHeight()); + assertFalse(snapshot.isOutOfDate()); + term.addLine(); + assertTrue(snapshot.isOutOfDate()); + assertEquals(11,term.getHeight()); + assertEquals(10,snapshot.getHeight()); + snapshot.updateSnapshot(false); + assertTrue(snapshot.hasDimensionsChanged()); + assertEquals(11,term.getHeight()); + assertEquals(11,snapshot.getHeight()); + assertEquals(20,term.getMaxHeight()); + + term.addLine(); + term.addLine(); + assertEquals(11,snapshot.getHeight()); + assertEquals(13,term.getHeight()); + assertTrue(snapshot.isOutOfDate()); + snapshot.updateSnapshot(false); + assertTrue(snapshot.hasDimensionsChanged()); + assertEquals(13,snapshot.getHeight()); + assertEquals(13,term.getHeight()); + assertEquals(20,term.getMaxHeight()); + } + public void testCursor() { + ITerminalTextData term=makeITerminalTextData(); + TerminalTextTestHelper.fillSimple(term,"0123456789"); + ITerminalTextDataSnapshot snapshot=term.makeSnapshot(); + term.setMaxHeight(20); + snapshot.setInterestWindow(3, 4); + snapshot.updateSnapshot(false); + term.setCursorLine(2); + term.setCursorColumn(1); + snapshot.updateSnapshot(false); + assertEquals(2, snapshot.getCursorLine()); + assertEquals(1, snapshot.getCursorColumn()); + term.setCursorLine(3); + term.setCursorColumn(2); + snapshot.updateSnapshot(false); + assertEquals(3, snapshot.getCursorLine()); + assertEquals(2, snapshot.getCursorColumn()); + } + public void testCursor2() { + ITerminalTextData term=makeITerminalTextData(); + TerminalTextTestHelper.fillSimple(term,"0123456789"); + ITerminalTextDataSnapshot snapshot=term.makeSnapshot(); + term.setMaxHeight(20); + snapshot.setInterestWindow(3, 4); + snapshot.updateSnapshot(false); + term.setCursorLine(2); + assertTrue(snapshot.isOutOfDate()); + snapshot.updateSnapshot(false); + term.setCursorColumn(1); + assertTrue(snapshot.isOutOfDate()); + } + public void testHasTerminalChanged() { + ITerminalTextData term=makeITerminalTextData(); + String s="12345\n" + + "abcde\n" + + "ABCDE\n" + + "vwxzy\n" + + "VWXYZ"; + TerminalTextTestHelper.fill(term,s); + + ITerminalTextDataSnapshot snapshot=term.makeSnapshot(); + assertTrue(snapshot.hasTerminalChanged()); + snapshot.updateSnapshot(false); + + assertTrue(snapshot.hasTerminalChanged()); + + // make a change and expect it to be changed + term.setChar(0, 0, '?', null); + snapshot.updateSnapshot(false); + assertTrue(snapshot.hasTerminalChanged()); + + // make a change and expect it to be changed + term.setChars(1, 1, new char[]{'?','!','.'},null); + snapshot.updateSnapshot(false); + assertTrue(snapshot.hasTerminalChanged()); + + // scroll + term.scroll(1, 2, -1); + snapshot.updateSnapshot(false); + assertTrue(snapshot.hasTerminalChanged()); + + // scroll + term.scroll(1, 2, 1); + snapshot.updateSnapshot(false); + assertTrue(snapshot.hasTerminalChanged()); + + // scroll + term.scroll(1, 2, -1); + snapshot.updateSnapshot(true); + assertTrue(snapshot.hasTerminalChanged()); + + // scroll + term.scroll(1, 2, 1); + snapshot.updateSnapshot(true); + assertTrue(snapshot.hasTerminalChanged()); + + // setDimensions + term.setDimensions(2, 2); + snapshot.updateSnapshot(false); + assertTrue(snapshot.hasTerminalChanged()); + + // setDimensions + term.setDimensions(20, 20); + snapshot.updateSnapshot(false); + assertTrue(snapshot.hasTerminalChanged()); + + snapshot.updateSnapshot(false); + assertFalse(snapshot.hasTerminalChanged()); + + // window of interest changes should NOT set hasTerminalChanged + snapshot.updateSnapshot(false); + snapshot.setInterestWindow(7, 4); + + assertFalse(snapshot.hasTerminalChanged()); + } + public void testGetTerminalTextData() { + ITerminalTextData term=makeITerminalTextData(); + ITerminalTextDataSnapshot snapshot=term.makeSnapshot(); + assertSame(term, snapshot.getTerminalTextData()); + } +} diff --git a/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/model/TerminalTextDataSnapshotWindowTest.java b/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/model/TerminalTextDataSnapshotWindowTest.java new file mode 100644 index 00000000000..7ce515cc100 --- /dev/null +++ b/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/model/TerminalTextDataSnapshotWindowTest.java @@ -0,0 +1,191 @@ +/******************************************************************************* + * Copyright (c) 2007 Wind River Systems, Inc. and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Michael Scharf (Wind River) - initial API and implementation + *******************************************************************************/ +package org.eclipse.tm.internal.terminal.model; + +import junit.framework.TestCase; + +import org.eclipse.tm.terminal.model.ITerminalTextData; +import org.eclipse.tm.terminal.model.ITerminalTextDataReadOnly; +import org.eclipse.tm.terminal.model.ITerminalTextDataSnapshot; + +public class TerminalTextDataSnapshotWindowTest extends TestCase { + String toMultiLineText(ITerminalTextDataReadOnly term) { + return TerminalTextTestHelper.toMultiLineText(term); + } + String toSimpleText(ITerminalTextDataReadOnly term) { + return TerminalTextTestHelper.toSimple(term); + } + + protected ITerminalTextData makeITerminalTextData() { + return new TerminalTextData(); + } + ITerminalTextDataSnapshot snapshotSimple(String text, ITerminalTextData term) { + TerminalTextTestHelper.fillSimple(term,text); + ITerminalTextDataSnapshot snapshot=term.makeSnapshot(); + return snapshot; + + } + /** + * @param snapshot + * @param expected a string of 0 and 1 (1 means changed) + */ + void assertChangedLines(ITerminalTextDataSnapshot snapshot, String expected) { + assertEquals(expected.length(),snapshot.getHeight()); + StringBuffer buffer=new StringBuffer(); + for (int line = 0; line < expected.length(); line++) { + if(snapshot.hasLineChanged(line)) + buffer.append('1'); + else + buffer.append('0'); + } + assertEquals(expected, buffer.toString()); + } + + public void testSetInterestWindow() { + ITerminalTextData term=makeITerminalTextData(); + ITerminalTextDataSnapshot snapshot=snapshotSimple("0123456789",term); + assertEquals(0, snapshot.getInterestWindowStartLine()); + assertEquals(-1, snapshot.getInterestWindowSize()); + snapshot.setInterestWindow(2, 3); + assertEquals(2, snapshot.getInterestWindowStartLine()); + assertEquals(3, snapshot.getInterestWindowSize()); + snapshot.updateSnapshot(false); + assertChangedLines(snapshot,"0011100000"); + } + public void testSetChar() { + ITerminalTextData term=makeITerminalTextData(); + ITerminalTextDataSnapshot snapshot=snapshotSimple("0123456789",term); + snapshot.setInterestWindow(2, 3); + snapshot.updateSnapshot(false); + assertEquals(" 234 ", toSimpleText(snapshot)); + + term.setChar(0, 0, 'x', null); + assertFalse(snapshot.isOutOfDate()); + snapshot.updateSnapshot(false); + assertChangedLines(snapshot,"0000000000"); + + term.setChar(1, 0, 'x', null); + assertFalse(snapshot.isOutOfDate()); + + term.setChar(2, 0, 'x', null); + assertTrue(snapshot.isOutOfDate()); + snapshot.updateSnapshot(false); + assertChangedLines(snapshot,"0010000000"); + + term.setChar(3, 0, 'x', null); + assertTrue(snapshot.isOutOfDate()); + snapshot.updateSnapshot(false); + assertChangedLines(snapshot,"0001000000"); + + term.setChar(4, 0, 'x', null); + assertTrue(snapshot.isOutOfDate()); + snapshot.updateSnapshot(false); + assertChangedLines(snapshot,"0000100000"); + + term.setChar(5, 0, 'x', null); + assertFalse(snapshot.isOutOfDate()); + + term.setChar(6, 0, 'x', null); + assertFalse(snapshot.isOutOfDate()); + + for (int i = 0; i < 9; i++) { + term.setChar(i, 0, (char)('a'+i), null); + } + assertTrue(snapshot.isOutOfDate()); + snapshot.updateSnapshot(false); + assertChangedLines(snapshot,"0011100000"); + } + + public void testSetChars() { + ITerminalTextData term=makeITerminalTextData(); + ITerminalTextDataSnapshot snapshot=snapshotSimple("0123456789",term); + snapshot.setInterestWindow(2, 3); + snapshot.updateSnapshot(false); + assertEquals(" 234 ", toSimpleText(snapshot)); + + term.setChars(0, 0, "x".toCharArray(), null); + assertFalse(snapshot.isOutOfDate()); + snapshot.updateSnapshot(false); + assertChangedLines(snapshot,"0000000000"); + + term.setChars(1, 0, "x".toCharArray(), null); + assertFalse(snapshot.isOutOfDate()); + + term.setChars(2, 0, "x".toCharArray(), null); + assertTrue(snapshot.isOutOfDate()); + snapshot.updateSnapshot(false); + assertChangedLines(snapshot,"0010000000"); + + term.setChars(3, 0, "x".toCharArray(), null); + assertTrue(snapshot.isOutOfDate()); + snapshot.updateSnapshot(false); + assertChangedLines(snapshot,"0001000000"); + + term.setChars(4, 0, "x".toCharArray(), null); + assertTrue(snapshot.isOutOfDate()); + snapshot.updateSnapshot(false); + assertChangedLines(snapshot,"0000100000"); + + term.setChars(5, 0, "x".toCharArray(), null); + assertFalse(snapshot.isOutOfDate()); + + term.setChars(6, 0, "x".toCharArray(), null); + assertFalse(snapshot.isOutOfDate()); + for (int i = 0; i < 9; i++) { + term.setChars(i, 0, (i+"").toCharArray(), null); + } + assertTrue(snapshot.isOutOfDate()); + snapshot.updateSnapshot(false); + assertChangedLines(snapshot,"0011100000"); + } + public void testSetChars2() { + ITerminalTextData term=makeITerminalTextData(); + ITerminalTextDataSnapshot snapshot=snapshotSimple("0123456789",term); + snapshot.setInterestWindow(2, 3); + snapshot.updateSnapshot(false); + assertEquals(" 234 ", toSimpleText(snapshot)); + + term.setChars(0, 0, "abcdef".toCharArray(),2,1, null); + assertFalse(snapshot.isOutOfDate()); + snapshot.updateSnapshot(false); + assertChangedLines(snapshot,"0000000000"); + + term.setChars(1, 0, "abcdef".toCharArray(),2 ,1, null); + assertFalse(snapshot.isOutOfDate()); + + term.setChars(2, 0, "abcdef".toCharArray(),2 ,1, null); + assertTrue(snapshot.isOutOfDate()); + snapshot.updateSnapshot(false); + assertChangedLines(snapshot,"0010000000"); + + term.setChars(3, 0, "abcdef".toCharArray(),2 ,1, null); + assertTrue(snapshot.isOutOfDate()); + snapshot.updateSnapshot(false); + assertChangedLines(snapshot,"0001000000"); + + term.setChars(4, 0, "abcdef".toCharArray(),2 ,1, null); + assertTrue(snapshot.isOutOfDate()); + snapshot.updateSnapshot(false); + assertChangedLines(snapshot,"0000100000"); + + term.setChars(5, 0, "abcdef".toCharArray(),2 ,1, null); + assertFalse(snapshot.isOutOfDate()); + + term.setChars(6, 0, "abcdef".toCharArray(),2 ,1, null); + assertFalse(snapshot.isOutOfDate()); + for (int i = 0; i < 9; i++) { + term.setChars(i, 0, ("ab"+i+"def").toCharArray(),2 ,1, null); + } + assertTrue(snapshot.isOutOfDate()); + snapshot.updateSnapshot(false); + assertChangedLines(snapshot,"0011100000"); + } +} diff --git a/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/model/TerminalTextDataStoreTest.java b/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/model/TerminalTextDataStoreTest.java new file mode 100644 index 00000000000..743d177923e --- /dev/null +++ b/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/model/TerminalTextDataStoreTest.java @@ -0,0 +1,20 @@ +/******************************************************************************* + * Copyright (c) 2007 Wind River Systems, Inc. and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Michael Scharf (Wind River) - initial API and implementation + *******************************************************************************/ +package org.eclipse.tm.internal.terminal.model; + +import org.eclipse.tm.terminal.model.ITerminalTextData; + +public class TerminalTextDataStoreTest extends AbstractITerminalTextDataTest { + protected ITerminalTextData makeITerminalTextData() { + return new TerminalTextDataStore(); + } + +} diff --git a/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/model/TerminalTextDataTest.java b/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/model/TerminalTextDataTest.java new file mode 100644 index 00000000000..457e18d42ae --- /dev/null +++ b/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/model/TerminalTextDataTest.java @@ -0,0 +1,20 @@ +/******************************************************************************* + * Copyright (c) 2007 Wind River Systems, Inc. and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Michael Scharf (Wind River) - initial API and implementation + *******************************************************************************/ +package org.eclipse.tm.internal.terminal.model; + +import org.eclipse.tm.terminal.model.ITerminalTextData; + +public class TerminalTextDataTest extends AbstractITerminalTextDataTest { + protected ITerminalTextData makeITerminalTextData() { + return new TerminalTextData(); + } + +} diff --git a/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/model/TerminalTextDataWindowTest.java b/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/model/TerminalTextDataWindowTest.java new file mode 100644 index 00000000000..0154dad1904 --- /dev/null +++ b/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/model/TerminalTextDataWindowTest.java @@ -0,0 +1,459 @@ +/******************************************************************************* + * Copyright (c) 2007 Wind River Systems, Inc. and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Michael Scharf (Wind River) - initial API and implementation + *******************************************************************************/ +package org.eclipse.tm.internal.terminal.model; + +import org.eclipse.tm.terminal.model.ITerminalTextData; +import org.eclipse.tm.terminal.model.LineSegment; +import org.eclipse.tm.terminal.model.Style; +import org.eclipse.tm.terminal.model.StyleColor; + +public class TerminalTextDataWindowTest extends AbstractITerminalTextDataTest { + int fOffset; + int fSize; + public TerminalTextDataWindowTest() { + fOffset=2; + fSize=2; + } + protected ITerminalTextData makeITerminalTextData() { + TerminalTextDataWindow term=new TerminalTextDataWindow(); + term.setWindow(fOffset,fSize); + return term; + } + /** + * Used for multi line text + * @param expected + * @param actual + */ + protected void assertEqualsTerm(String expected,String actual) { + assertEquals(stripMultiLine(expected), stripMultiLine(actual)); + } + private String stripMultiLine(String s) { + StringBuffer b=new StringBuffer(); + String[] lines=s.split("\n"); + for (int i = 0; i < lines.length; i++) { + if(i>0) + b.append("\n"); //$NON-NLS-1$ + if(i>=fOffset && i=fOffset && i=fOffset&&line=fOffset&&line=fOffset&&line0) + buff.append("\n"); //$NON-NLS-1$ + for (int column = 0; column < width; column++) { + buff.append(term.getChar(line, column)); + } + } + return buff.toString(); + } + static public String toSimple(String str) { + return str.replaceAll("\000", " ").replaceAll("\n", ""); + + } + /** + * @param term + * @param s each character is one line + */ + static public void fillSimple(ITerminalTextData term, String s) { + Style style=Style.getStyle(StyleColor.getStyleColor("fg"), StyleColor.getStyleColor("bg"), false, false, false, false); + term.setDimensions(s.length(), 1); + for (int i = 0; i < s.length(); i++) { + char c=s.charAt(i); + term.setChar(i, 0, c, style.setForground(StyleColor.getStyleColor(""+c))); + } + } + /** + * @param term + * @param s lines separated by \n. The terminal will automatically + * resized to fit the text. + */ + static public void fill(ITerminalTextData term, String s) { + int width=0; + int len=0; + int height=0; + for (int i = 0; i < s.length(); i++) { + char c=s.charAt(i); + if(c=='\n') { + width=Math.max(width,len); + len=0; + } else { + if(len==0) + height++; + len++; + } + } + width=Math.max(width,len); + term.setDimensions(height, width); + fill(term,0,0,s); + } + + static public void fill(ITerminalTextData term, int column, int line, String s) { + int xx=column; + int yy=line; + Style style=Style.getStyle(StyleColor.getStyleColor("fg"), StyleColor.getStyleColor("bg"), false, false, false, false); + for (int i = 0; i < s.length(); i++) { + char c=s.charAt(i); + if(c=='\n') { + yy++; + xx=column; + } else { + term.setChar(yy, xx, c, style.setForground(StyleColor.getStyleColor(""+c))); + xx++; + } + } + } + +} diff --git a/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/test/terminalcanvas/Main.java b/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/test/terminalcanvas/Main.java new file mode 100644 index 00000000000..6dde5a8bf64 --- /dev/null +++ b/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/test/terminalcanvas/Main.java @@ -0,0 +1,36 @@ +/******************************************************************************* + * Copyright (c) 2007 Wind River Systems, Inc. and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Michael Scharf (Wind River) - initial API and implementation + *******************************************************************************/ +package org.eclipse.tm.internal.terminal.test.terminalcanvas; + +import org.eclipse.swt.SWT; +import org.eclipse.swt.layout.FillLayout; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Shell; + +public class Main { + + /** + * @param args + */ + public static void main(String[] args) { + Display display = new Display (); + Shell shell = new Shell (display); + shell.setLayout(new FillLayout()); + new TerminalTextCanvas(shell,SWT.NONE); + shell.setSize (200, 150); + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + display.dispose (); + } + +} diff --git a/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/test/terminalcanvas/Snippet48.java b/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/test/terminalcanvas/Snippet48.java new file mode 100644 index 00000000000..c8fd8aacd95 --- /dev/null +++ b/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/test/terminalcanvas/Snippet48.java @@ -0,0 +1,100 @@ +package org.eclipse.tm.internal.terminal.test.terminalcanvas; + + +/* + * Canvas example snippet: scroll an image (flicker free, no double buffering) + * + * For a list of all SWT example snippets see + * http://www.eclipse.org/swt/snippets/ + */ +import org.eclipse.swt.*; +import org.eclipse.swt.graphics.*; +import org.eclipse.swt.layout.*; +import org.eclipse.swt.widgets.*; + +public class Snippet48 { + +public static void main (String [] args) { + Display display = new Display (); + Shell shell = new Shell (display); + shell.setLayout(new FillLayout()); + Image originalImage = null; + FileDialog dialog = new FileDialog (shell, SWT.OPEN); + dialog.setText ("Open an image file or cancel"); + String string = dialog.open (); + if (string != null) { + originalImage = new Image (display, string); + } + final Image image = originalImage; + final Point origin = new Point (0, 0); + final Canvas canvas = new Canvas (shell, SWT.NO_BACKGROUND | + SWT.NO_REDRAW_RESIZE | SWT.V_SCROLL | SWT.H_SCROLL); + final ScrollBar hBar = canvas.getHorizontalBar (); + hBar.addListener (SWT.Selection, new Listener () { + public void handleEvent (Event e) { + int hSelection = hBar.getSelection (); + int destX = -hSelection - origin.x; + Rectangle rect = image.getBounds (); + canvas.scroll (destX, 0, 0, 0, rect.width, rect.height, false); + origin.x = -hSelection; + } + }); + final ScrollBar vBar = canvas.getVerticalBar (); + vBar.addListener (SWT.Selection, new Listener () { + public void handleEvent (Event e) { + int vSelection = vBar.getSelection (); + int destY = -vSelection - origin.y; + Rectangle rect = image.getBounds (); + canvas.scroll (0, destY, 0, 0, rect.width, rect.height, false); + origin.y = -vSelection; + } + }); + canvas.addListener (SWT.Resize, new Listener () { + public void handleEvent (Event e) { + Rectangle rect = image.getBounds (); + Rectangle client = canvas.getClientArea (); + hBar.setMaximum (rect.width); + vBar.setMaximum (rect.height); + hBar.setThumb (Math.min (rect.width, client.width)); + vBar.setThumb (Math.min (rect.height, client.height)); + int hPage = rect.width - client.width; + int vPage = rect.height - client.height; + int hSelection = hBar.getSelection (); + int vSelection = vBar.getSelection (); + if (hSelection >= hPage) { + if (hPage <= 0) hSelection = 0; + origin.x = -hSelection; + } + if (vSelection >= vPage) { + if (vPage <= 0) vSelection = 0; + origin.y = -vSelection; + } + canvas.redraw (); + } + }); + canvas.addListener (SWT.Paint, new Listener () { + public void handleEvent (Event e) { + GC gc = e.gc; + gc.drawImage (image, origin.x, origin.y); + Rectangle rect = image.getBounds (); + Rectangle client = canvas.getClientArea (); + int marginWidth = client.width - rect.width; + if (marginWidth > 0) { + gc.fillRectangle (rect.width, 0, marginWidth, client.height); + } + int marginHeight = client.height - rect.height; + if (marginHeight > 0) { + gc.fillRectangle (0, rect.height, client.width, marginHeight); + } + } + }); + shell.setSize (200, 150); + shell.open (); + while (!shell.isDisposed ()) { + if (!display.readAndDispatch ()) display.sleep (); + } + originalImage.dispose(); + display.dispose (); +} + +} diff --git a/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/test/terminalcanvas/TerminalTextCanvas.java b/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/test/terminalcanvas/TerminalTextCanvas.java new file mode 100644 index 00000000000..8996bb96621 --- /dev/null +++ b/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/test/terminalcanvas/TerminalTextCanvas.java @@ -0,0 +1,103 @@ +/******************************************************************************* + * Copyright (c) 2007 Wind River Systems, Inc. and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Michael Scharf (Wind River) - initial API and implementation + *******************************************************************************/ +package org.eclipse.tm.internal.terminal.test.terminalcanvas; + +import org.eclipse.swt.SWT; +import org.eclipse.swt.graphics.GC; +import org.eclipse.swt.graphics.Image; +import org.eclipse.swt.graphics.Point; +import org.eclipse.swt.graphics.Rectangle; +import org.eclipse.swt.widgets.Canvas; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Event; +import org.eclipse.swt.widgets.FileDialog; +import org.eclipse.swt.widgets.Listener; +import org.eclipse.swt.widgets.ScrollBar; + +public class TerminalTextCanvas extends Canvas { + Image image; + Point origin = new Point (0, 0); + public TerminalTextCanvas(Composite parent, int style) { + super(parent, SWT.NO_BACKGROUND | + SWT.NO_REDRAW_RESIZE | SWT.V_SCROLL | SWT.H_SCROLL| style); + loadImage(parent); + + final ScrollBar hBar = getHorizontalBar (); + hBar.addListener (SWT.Selection, new Listener () { + public void handleEvent (Event e) { + int hSelection = hBar.getSelection (); + int destX = -hSelection - origin.x; + Rectangle rect = image.getBounds (); + scroll (destX, 0, 0, 0, rect.width, rect.height, false); + origin.x = -hSelection; + } + }); + final ScrollBar vBar = getVerticalBar (); + vBar.addListener (SWT.Selection, new Listener () { + public void handleEvent (Event e) { + int vSelection = vBar.getSelection (); + int destY = -vSelection - origin.y; + Rectangle rect = image.getBounds (); + scroll (0, destY, 0, 0, rect.width, rect.height, false); + origin.y = -vSelection; + } + }); + addListener (SWT.Resize, new Listener () { + public void handleEvent (Event e) { + Rectangle rect = image.getBounds (); + Rectangle client = getClientArea (); + hBar.setMaximum (rect.width); + vBar.setMaximum (rect.height); + hBar.setThumb (Math.min (rect.width, client.width)); + vBar.setThumb (Math.min (rect.height, client.height)); + int hPage = rect.width - client.width; + int vPage = rect.height - client.height; + int hSelection = hBar.getSelection (); + int vSelection = vBar.getSelection (); + if (hSelection >= hPage) { + if (hPage <= 0) hSelection = 0; + origin.x = -hSelection; + } + if (vSelection >= vPage) { + if (vPage <= 0) vSelection = 0; + origin.y = -vSelection; + } + redraw (); + } + }); + addListener (SWT.Paint, new Listener () { + public void handleEvent (Event e) { + GC gc = e.gc; + System.out.println(gc.getClipping()+" "+origin); + gc.drawImage (image, origin.x, origin.y); + Rectangle rect = image.getBounds (); + Rectangle client = getClientArea (); + int marginWidth = client.width - rect.width; + if (marginWidth > 0) { + gc.fillRectangle (rect.width, 0, marginWidth, client.height); + } + int marginHeight = client.height - rect.height; + if (marginHeight > 0) { + gc.fillRectangle (0, rect.height, client.width, marginHeight); + } + } + }); + } + private void loadImage(Composite parent) { + FileDialog dialog = new FileDialog (parent.getShell(), SWT.OPEN); + dialog.setText ("Open an image file or cancel"); + String string = dialog.open (); + if (string != null) { + image = new Image (getDisplay(), string); + } + } + +} diff --git a/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/test/terminalcanvas/VirtualCanvas.java b/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/test/terminalcanvas/VirtualCanvas.java new file mode 100644 index 00000000000..414622254e9 --- /dev/null +++ b/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/test/terminalcanvas/VirtualCanvas.java @@ -0,0 +1,333 @@ +/******************************************************************************* + * Copyright (c) 2007 Wind River Systems, Inc. and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Michael Scharf (Wind River) - initial API and implementation + *******************************************************************************/ +package org.eclipse.tm.internal.terminal.test.terminalcanvas; + + +import org.eclipse.swt.SWT; +import org.eclipse.swt.events.DisposeEvent; +import org.eclipse.swt.events.DisposeListener; +import org.eclipse.swt.graphics.Color; +import org.eclipse.swt.graphics.GC; +import org.eclipse.swt.graphics.Point; +import org.eclipse.swt.graphics.Rectangle; +import org.eclipse.swt.widgets.Canvas; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Event; +import org.eclipse.swt.widgets.Listener; +import org.eclipse.swt.widgets.ScrollBar; + +/** + * A Canvas showing a virtual object. + * Virtual: the extent of the total canvas. + * Screen: the visible client area in the screen. + */ +public abstract class VirtualCanvas extends Canvas { + + private Rectangle fVirtualBounds = new Rectangle(0,0,0,0); + private Rectangle fClientArea; + private GC fPaintGC=null; + public VirtualCanvas(Composite parent, int style) { + super(parent, style|SWT.NO_BACKGROUND|SWT.NO_REDRAW_RESIZE); + fPaintGC= new GC(this); + addListener(SWT.Paint, new Listener() { + public void handleEvent(Event event) { + paint(event.gc); + } + }); + addListener(SWT.Resize, new Listener() { + public void handleEvent(Event event) { + fClientArea=getClientArea(); + updateViewRectangle(); + } + }); + getVerticalBar().addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + scrollY((ScrollBar)e.widget); + postScrollEventHandling(e); + + } + + }); + getHorizontalBar().addListener(SWT.Selection, new Listener() { + public void handleEvent(Event e) { + scrollX((ScrollBar)e.widget); + postScrollEventHandling(e); + + } + }); + addDisposeListener(new DisposeListener(){ + public void widgetDisposed(DisposeEvent e) { + if(fPaintGC!=null){ + fPaintGC.dispose(); + fPaintGC=null; + } + } + + }); + } + public void setAutoSelect(boolean on) { + } + public boolean hasAutoSelect() { + return false; + } + public void doAutoSelect() { + } + + /** HACK: run an event loop if the scrollbar is dragged...*/ + private void postScrollEventHandling(Event e) { + if(true&&e.detail==SWT.DRAG) { + // TODO check if this is always ok??? + // used to process runnables while scrolling + // This fixes the update problems when scrolling! + // see: https://bugs.eclipse.org/bugs/show_bug.cgi?id=47582#5 + // TODO investigate: + // The alternative is to call redraw on the new visible area + // redraw(expose.x, expose.y, expose.width, expose.height, true); + + while (!getDisplay().isDisposed() && getDisplay().readAndDispatch()) { + // do nothing here... + } + } + } + + protected void scrollX(ScrollBar hBar) { + int hSelection = hBar.getSelection (); + int destX = -hSelection - fVirtualBounds.x; + fVirtualBounds.x = -hSelection; + scrollSmart(destX, 0); + updateViewRectangle(); + } + protected void scrollXDelta(int delta) { + getHorizontalBar().setSelection(-fVirtualBounds.x+delta); + scrollX(getHorizontalBar()); + } + + protected void scrollY(ScrollBar vBar) { + int vSelection = vBar.getSelection (); + int destY = -vSelection - fVirtualBounds.y; + fVirtualBounds.y = -vSelection; + scrollSmart(0,destY); + updateViewRectangle(); + + } + protected void scrollYDelta(int delta) { + getVerticalBar().setSelection(-fVirtualBounds.y+delta); + scrollY(getVerticalBar()); + } + + + private void scrollSmart(int deltaX, int deltaY) { + Rectangle rect = getBounds(); + scroll (deltaX, deltaY, 0, 0, rect.width, rect.height, false); + } + + protected void revealRect(Rectangle rect) { + Rectangle visibleRect=getScreenRectInVirtualSpace(); + // scroll the X part + int deltaX=0; + if(rect.x0||marginHeight>0){ + Color bg=getBackground(); + gc.setBackground(getBackgroundColor()); + if (marginWidth > 0) { + gc.fillRectangle (width, clipping.y, marginWidth, clipping.height); + } + if (marginHeight > 0) { + gc.fillRectangle (clipping.x, height, clipping.width, marginHeight); + } + gc.setBackground(bg); + } + } + /** + * @private + */ + protected boolean inClipping(Rectangle clipping, Rectangle r) { + // TODO check if this is OK in all cases (the <=!) + // + if(r.x+r.width<=clipping.x) + return false; + if(clipping.x+clipping.width<=r.x) + return false; + if(r.y+r.height<=clipping.y) + return false; + if(clipping.y+clipping.height<=r.y) + return false; + + return true; + } + /** + * @return the screen rect in virtual space (starting with (0,0)) + * of the visible screen. (x,y>=0) + */ + protected Rectangle getScreenRectInVirtualSpace() { + return new Rectangle(fClientArea.x-fVirtualBounds.x,fClientArea.y-fVirtualBounds.y,fClientArea.width,fClientArea.height); + } + /** + * @return the rect in virtual space (starting with (0,0)) + * of the visible screen. (x,y>=0) + */ + protected Rectangle getRectInVirtualSpace(Rectangle r) { + return new Rectangle(r.x-fVirtualBounds.x,r.y-fVirtualBounds.y,r.width,r.height); + } + + /** + * Sets the extend of the virtual dieplay ares + * @param width + * @param height + */ + protected void setVirtualExtend(int width, int height) { + fVirtualBounds.width=width; + fVirtualBounds.height=height; + updateScrollbars(); + updateViewRectangle(); + } + /** + * sets the scrolling origin. Also sets the scrollbars. + * Does NOT redraw! + * Use negative values (move the virtual origin to the top left + * to see something in the screen (which is located at (0,0)) + * @param x + * @param y + */ + protected void setVirtualOrigin(int x, int y) { + fVirtualBounds.x=x; + fVirtualBounds.y=y; + getHorizontalBar().setSelection(x); + getVerticalBar().setSelection(y); + updateViewRectangle(); + } + + /** + * @param x + * @return the virtual coordinate in scree space + */ + protected int virtualXtoScreen(int x) { + return x+fVirtualBounds.x; + } + protected int virtualYtoScreen(int y) { + return y+fVirtualBounds.y; + } + protected int screenXtoVirtual(int x) { + return x-fVirtualBounds.x; + } + protected int screenYtoVirtual(int y) { + return y-fVirtualBounds.y; + } + /** called when the viewed part is changing */ + private Rectangle fViewRectangle=new Rectangle(0,0,0,0); + void updateViewRectangle() { + if( + fViewRectangle.x==-fVirtualBounds.x + && fViewRectangle.y==-fVirtualBounds.y + && fViewRectangle.width==fClientArea.width + && fViewRectangle.height==fClientArea.height + ) + return; + fViewRectangle.x=-fVirtualBounds.x; + fViewRectangle.y=-fVirtualBounds.y; + fViewRectangle.width=fClientArea.width; + fViewRectangle.height=fClientArea.height; + viewRectangleChanged(fViewRectangle.x,fViewRectangle.y,fViewRectangle.width,fViewRectangle.height); + } + protected Rectangle getViewRectangle() { + return fViewRectangle; + } + /** + * Called when the viewed part has changed. + * Override when you need this information.... + * Is only called if the values change! + * @param x visible in virtual space + * @param y visible in virtual space + * @param width + * @param height + */ + protected void viewRectangleChanged(int x, int y, int width, int height) { +// System.out.println(x+" "+y+" "+width+" "+height); + } + /** + * @private + */ + private void updateScrollbars() { + Point size= getSize(); + Rectangle clientArea= getClientArea(); + + ScrollBar horizontal= getHorizontalBar(); + if (fVirtualBounds.width <= clientArea.width) { + // TODO IMPORTANT in ScrollBar.setVisible comment out the line + // that checks 'isvisible' and returns (at the beginning) + horizontal.setVisible(false); + horizontal.setSelection(0); + } else { + horizontal.setPageIncrement(clientArea.width - horizontal.getIncrement()); + int max= fVirtualBounds.width + (size.x - clientArea.width); + horizontal.setMaximum(max); + horizontal.setThumb(size.x > max ? max : size.x); + horizontal.setVisible(true); + } + + ScrollBar vertical= getVerticalBar(); + if (fVirtualBounds.height <= clientArea.height) { + vertical.setVisible(false); + vertical.setSelection(0); + } else { + vertical.setPageIncrement(clientArea.height - vertical.getIncrement()); + int max= fVirtualBounds.height + (size.y - clientArea.height); + vertical.setMaximum(max); + vertical.setThumb(size.y > max ? max : size.y); + vertical.setVisible(true); + } + } +} + diff --git a/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/test/ui/AbstractLineOrientedDataSource.java b/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/test/ui/AbstractLineOrientedDataSource.java new file mode 100644 index 00000000000..f0c29751165 --- /dev/null +++ b/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/test/ui/AbstractLineOrientedDataSource.java @@ -0,0 +1,42 @@ +/******************************************************************************* + * Copyright (c) 2007 Wind River Systems, Inc. and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Michael Scharf (Wind River) - initial API and implementation + *******************************************************************************/ +package org.eclipse.tm.internal.terminal.test.ui; + +import org.eclipse.tm.terminal.model.ITerminalTextData; +import org.eclipse.tm.terminal.model.Style; + +/** + * Adds line by line + * + */ +abstract class AbstractLineOrientedDataSource implements IDataSource { + abstract public char[] dataSource(); + abstract public Style getStyle(); + + abstract public void next(); + + public int step(ITerminalTextData terminal) { + next(); + char[] chars=dataSource(); + Style style= getStyle(); + int len; + // keep the synchronized block short! + synchronized (terminal) { + terminal.addLine(); + len=Math.min(terminal.getWidth(),chars.length); + int line=terminal.getHeight()-1; + terminal.setChars(line, 0, chars, 0, len, style); + terminal.setCursorLine(line); + terminal.setCursorColumn(len-1); + } + return len; + } +} \ No newline at end of file diff --git a/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/test/ui/DataReader.java b/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/test/ui/DataReader.java new file mode 100644 index 00000000000..6d017f0bd81 --- /dev/null +++ b/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/test/ui/DataReader.java @@ -0,0 +1,87 @@ +/******************************************************************************* + * Copyright (c) 2007 Wind River Systems, Inc. and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Michael Scharf (Wind River) - initial API and implementation + *******************************************************************************/ +package org.eclipse.tm.internal.terminal.test.ui; + +import org.eclipse.tm.terminal.model.ITerminalTextData; + +class DataReader implements Runnable { + final Thread fThread; + final IDataSource fDataSource; + final ITerminalTextData fTerminal; + volatile boolean fStart; + volatile boolean fStop; + volatile int fThrottleTime; + final IStatus fStatus; + final String fName; + DataReader(String name, ITerminalTextData terminal, IDataSource dataSource, IStatus status) { + fStatus=status; + fName=name; + fTerminal=terminal; + fDataSource=dataSource; + fThread=new Thread(this,name); + fThread.setDaemon(true); + fThread.start(); + } + public void run() { + long t0=System.currentTimeMillis()-1; + long c=0; + int lines=0; + while(!Thread.interrupted()) { + while(!fStart || fStop) { + sleep(1); + } + if(fThrottleTime>0) + sleep(fThrottleTime); + // synchronize because we have to be sure the size does not change while + // we add lines + int len=fDataSource.step(fTerminal); + // keep the synchronized block short! + c+=len; + lines++; + if((fThrottleTime>0 || (lines%100==0))&&(System.currentTimeMillis()-t0)>1000) { + long t=System.currentTimeMillis()-t0; + final String s=(c*1000)/(t*1024)+"kb/s " + (lines*1000)/t+"lines/sec "+(c*1000*8)/t+" bits/s "; + fStatus.setStatus(s); + lines=0; + t0=System.currentTimeMillis(); + c=0; + } + } + } + public int getThrottleTime() { + return fThrottleTime; + } + public void setThrottleTime(int throttleTime) { + fThrottleTime = throttleTime; + } + private void sleep(int ms) { + try { + Thread.sleep(ms); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } + public boolean isStart() { + return fStart; + } + public void setStart(boolean start) { + fStart = start; + } + public String getName() { + return fName; + } + public boolean isStop() { + return fStop; + } + public void setStop(boolean stop) { + fStop = stop; + } +} \ No newline at end of file diff --git a/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/test/ui/FastDataSource.java b/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/test/ui/FastDataSource.java new file mode 100644 index 00000000000..2ffb33edb4f --- /dev/null +++ b/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/test/ui/FastDataSource.java @@ -0,0 +1,35 @@ +/******************************************************************************* + * Copyright (c) 2007 Wind River Systems, Inc. and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Michael Scharf (Wind River) - initial API and implementation + *******************************************************************************/ +package org.eclipse.tm.internal.terminal.test.ui; + +import org.eclipse.tm.terminal.model.Style; + +final class FastDataSource extends AbstractLineOrientedDataSource { + char lines[][]=new char[][]{ + "123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 ".toCharArray(), + "abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi abcdefghi ".toCharArray(), + }; + + + int pos; + + public char[] dataSource() { + return lines[pos%lines.length]; + } + + public Style getStyle() { + return null; + } + + public void next() { + pos++; + } +} \ No newline at end of file diff --git a/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/test/ui/FileDataSource.java b/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/test/ui/FileDataSource.java new file mode 100644 index 00000000000..07539398102 --- /dev/null +++ b/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/test/ui/FileDataSource.java @@ -0,0 +1,72 @@ +/******************************************************************************* + * Copyright (c) 2007 Wind River Systems, Inc. and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Michael Scharf (Wind River) - initial API and implementation + *******************************************************************************/ +package org.eclipse.tm.internal.terminal.test.ui; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; + +import org.eclipse.tm.terminal.model.Style; +import org.eclipse.tm.terminal.model.StyleColor; + +/** + * Reads the file in an infinite loop. + * Makes lines containing 'x' bold. + * + */ +final class FileDataSource extends AbstractLineOrientedDataSource { + private final String fFile; + + BufferedReader reader; + + String line; + + Style style; + + Style styleNormal=Style.getStyle(StyleColor.getStyleColor("black"),StyleColor.getStyleColor("white")); + + Style styleBold=styleNormal.setBold(true); + + FileDataSource(String file) { + fFile = file; + } + + public char[] dataSource() { + return line.toCharArray(); + } + + public Style getStyle() { + return style; + } + + public void next() { + try { + if(reader==null) + reader = new BufferedReader(new FileReader(fFile)); + line=reader.readLine(); + if(line==null) { + reader.close(); + reader=null; + // reopen the file + next(); + return; + } + if(line.lastIndexOf('x')>0) + style=styleBold; + else + style=styleNormal; + } catch (IOException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + } +} \ No newline at end of file diff --git a/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/test/ui/IDataSource.java b/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/test/ui/IDataSource.java new file mode 100644 index 00000000000..938916d8048 --- /dev/null +++ b/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/test/ui/IDataSource.java @@ -0,0 +1,21 @@ +/******************************************************************************* + * Copyright (c) 2007 Wind River Systems, Inc. and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Michael Scharf (Wind River) - initial API and implementation + *******************************************************************************/ +package org.eclipse.tm.internal.terminal.test.ui; + +import org.eclipse.tm.terminal.model.ITerminalTextData; + +interface IDataSource { + /** + * @param terminal + * @return number of characters changed + */ + int step(ITerminalTextData terminal); +} \ No newline at end of file diff --git a/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/test/ui/IStatus.java b/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/test/ui/IStatus.java new file mode 100644 index 00000000000..d060e779f3a --- /dev/null +++ b/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/test/ui/IStatus.java @@ -0,0 +1,15 @@ +/******************************************************************************* + * Copyright (c) 2007 Wind River Systems, Inc. and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Michael Scharf (Wind River) - initial API and implementation + *******************************************************************************/ +package org.eclipse.tm.internal.terminal.test.ui; + +public interface IStatus { + void setStatus(String message); +} diff --git a/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/test/ui/LineCountingDataSource.java b/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/test/ui/LineCountingDataSource.java new file mode 100644 index 00000000000..7a3dc35981d --- /dev/null +++ b/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/test/ui/LineCountingDataSource.java @@ -0,0 +1,43 @@ +/******************************************************************************* + * Copyright (c) 2007 Wind River Systems, Inc. and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Michael Scharf (Wind River) - initial API and implementation + *******************************************************************************/ +package org.eclipse.tm.internal.terminal.test.ui; + +import org.eclipse.tm.terminal.model.Style; +import org.eclipse.tm.terminal.model.StyleColor; + +final class LineCountingDataSource extends AbstractLineOrientedDataSource { + Style styleNormal=Style.getStyle(StyleColor.getStyleColor("black"),StyleColor.getStyleColor("red")); + + Style styles[]=new Style[] { + styleNormal, + styleNormal.setBold(true), + styleNormal.setForground("blue"), + styleNormal.setForground("yellow"), + styleNormal.setBold(true).setUnderline(true), + styleNormal.setReverse(true), + styleNormal.setReverse(true).setBold(true), + styleNormal.setReverse(true).setUnderline(true) + }; + + int pos; + + public char[] dataSource() { + return (pos+" 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789 123456789").toCharArray(); + } + + public Style getStyle() { + return styles[pos%styles.length]; + } + + public void next() { + pos++; + } +} \ No newline at end of file diff --git a/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/test/ui/RandomDataSource.java b/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/test/ui/RandomDataSource.java new file mode 100644 index 00000000000..ee002861b79 --- /dev/null +++ b/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/test/ui/RandomDataSource.java @@ -0,0 +1,49 @@ +/******************************************************************************* + * Copyright (c) 2007 Wind River Systems, Inc. and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Michael Scharf (Wind River) - initial API and implementation + *******************************************************************************/ +package org.eclipse.tm.internal.terminal.test.ui; + +import java.util.Random; + +import org.eclipse.tm.terminal.model.ITerminalTextData; +import org.eclipse.tm.terminal.model.Style; +import org.eclipse.tm.terminal.model.StyleColor; + +public class RandomDataSource implements IDataSource { + Random fRandom=new Random(); + Style styleNormal=Style.getStyle(StyleColor.getStyleColor("black"),StyleColor.getStyleColor("green")); + Style styles[]=new Style[] { + styleNormal, + styleNormal.setBold(true), + styleNormal.setForground("red"), + styleNormal.setForground("yellow"), + styleNormal.setBold(true).setUnderline(true), + styleNormal.setReverse(true), + styleNormal.setReverse(true).setBold(true), + styleNormal.setReverse(true).setUnderline(true) + }; + + public int step(ITerminalTextData terminal) { + int N=fRandom.nextInt(1000); + int h=terminal.getHeight(); + int w=terminal.getWidth(); + synchronized (terminal) { + for (int i = 0; i < N; i++) { + int line=fRandom.nextInt(h); + int col=fRandom.nextInt(w); + char c=(char)('A'+fRandom.nextInt('z'-'A')); + Style style=styles[fRandom.nextInt(styles.length)]; + terminal.setChar(line, col, c, style); + } + } + return N; + } + +} diff --git a/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/test/ui/TerminalTextUITest.java b/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/test/ui/TerminalTextUITest.java new file mode 100644 index 00000000000..84ce334b59e --- /dev/null +++ b/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/test/ui/TerminalTextUITest.java @@ -0,0 +1,254 @@ +/******************************************************************************* + * Copyright (c) 2007 Wind River Systems, Inc. and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Michael Scharf (Wind River) - initial API and implementation + *******************************************************************************/ +package org.eclipse.tm.internal.terminal.test.ui; + +import java.io.File; +import java.util.ArrayList; +import java.util.Iterator; +import java.util.List; + +import org.eclipse.swt.SWT; +import org.eclipse.swt.events.ModifyEvent; +import org.eclipse.swt.events.ModifyListener; +import org.eclipse.swt.events.SelectionAdapter; +import org.eclipse.swt.events.SelectionEvent; +import org.eclipse.swt.layout.GridData; +import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.layout.RowData; +import org.eclipse.swt.layout.RowLayout; +import org.eclipse.swt.widgets.Button; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Control; +import org.eclipse.swt.widgets.Display; +import org.eclipse.swt.widgets.Label; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.swt.widgets.Text; +import org.eclipse.tm.internal.terminal.textcanvas.ITextCanvasModel; +import org.eclipse.tm.internal.terminal.textcanvas.PollingTextCanvasModel; +import org.eclipse.tm.internal.terminal.textcanvas.TextCanvas; +import org.eclipse.tm.internal.terminal.textcanvas.TextLineRenderer; +import org.eclipse.tm.terminal.model.ITerminalTextData; +import org.eclipse.tm.terminal.model.ITerminalTextDataSnapshot; +import org.eclipse.tm.terminal.model.TerminalTextDataFactory; + +/** + * adjust columns when table gets resized.... + * + */ +public class TerminalTextUITest { + static TextCanvas fgTextCanvas; + static ITextCanvasModel fgModel; + static ITerminalTextData fTerminalModel; + static Label fStatusLabel; + static volatile int fHeight; + static volatile int fWidth; + static DataReader fDataReader; + static List fDataReaders=new ArrayList(); + private static Text heightText; + static class Status implements IStatus { + public void setStatus(final String s) { + if(!fStatusLabel.isDisposed()) + Display.getDefault().asyncExec(new Runnable(){ + public void run() { + if(!fStatusLabel.isDisposed()) + fStatusLabel.setText(s); + }}); + } + + } + public static void main(String[] args) { + Display display = new Display(); + Shell shell = new Shell(display); + shell.setLayout(new GridLayout()); + Composite composite=new Composite(shell, SWT.NONE); + composite.setLayoutData(new GridData(GridData.FILL_HORIZONTAL)); + RowLayout layout = new RowLayout(SWT.HORIZONTAL); + layout.wrap = true; + layout.fill = false; + fTerminalModel=TerminalTextDataFactory.makeTerminalTextData(); + fHeight=24; + fWidth=80; + fTerminalModel.setDimensions(fHeight, fWidth); + fTerminalModel.setMaxHeight(fHeight); + ITerminalTextDataSnapshot snapshot=fTerminalModel.makeSnapshot(); + // TODO how to get the initial size correctly! + snapshot.updateSnapshot(false); + fgModel=new PollingTextCanvasModel(snapshot); + fgTextCanvas=new TextCanvas(shell,fgModel, SWT.NONE); + fgTextCanvas.setCellRenderer(new TextLineRenderer(fgTextCanvas,fgModel)); + fgTextCanvas.setLayoutData(new GridData(GridData.FILL_BOTH)); + + + composite.setLayout(layout); + addAutorevealCursorButton(composite); + Text maxHeightText = addMaxHeightInput(composite); + addHeightInput(composite, maxHeightText); + addWidthText(composite); + Text throttleText = addThrottleText(composite); + + IStatus status=new Status(); + DataReader reader=new DataReader("Line Count",fTerminalModel,new LineCountingDataSource(),status); + addDataReader(composite, reader); + reader=new DataReader("Fast",fTerminalModel,new FastDataSource(),status); + addDataReader(composite, reader); + reader=new DataReader("Random",fTerminalModel,new RandomDataSource(),status); + addDataReader(composite, reader); + for (int i = 0; i < args.length; i++) { + File file=new File(args[i]); + reader=new DataReader(file.getName(),fTerminalModel,new VT100DataSource(args[i]),status); + addDataReader(composite, reader); + } + addStopAllButton(composite, reader); + + fStatusLabel=new Label(shell,SWT.NONE); + fStatusLabel.setLayoutData(new GridData(250,15)); + throttleText.setText("100"); + setThrottleForAll(100); + + if(args.length==0) + addLabel(composite, "[Files can be added via commandline]"); + shell.setSize(600,300); + shell.open(); + while (!shell.isDisposed()) { + if (!display.readAndDispatch()) + display.sleep(); + } + display.dispose(); + } + private static Text addMaxHeightInput(Composite composite) { + addLabel(composite, "maxHeight:"); + final Text maxHeightText=new Text(composite,SWT.BORDER); + setLayoutData(maxHeightText,30); + maxHeightText.addModifyListener(new ModifyListener(){ + public void modifyText(ModifyEvent e) { + synchronized (fTerminalModel) { + int height=textToInt(maxHeightText); + if(height<1) + return; + if(fTerminalModel.getHeight()>height) { + fTerminalModel.scroll(0, fTerminalModel.getHeight(), height-fTerminalModel.getHeight()); + fTerminalModel.setDimensions(height,fTerminalModel.getWidth()); + heightText.setText(height+""); + } + fTerminalModel.setMaxHeight(height); + } + } + }); + maxHeightText.setText(fHeight+""); + return maxHeightText; + } + private static void addHeightInput(Composite composite, final Text maxHeightText) { + addLabel(composite,"heigth:"); + heightText=new Text(composite,SWT.BORDER); + setLayoutData(heightText,30); + heightText.addModifyListener(new ModifyListener(){ + public void modifyText(ModifyEvent e) { + synchronized (fTerminalModel) { + int height=textToInt(heightText); + if(height<1) + return; + maxHeightText.setText(""+height); + fTerminalModel.setDimensions(height,fTerminalModel.getWidth()); + fTerminalModel.setMaxHeight(height); + } + } + }); + heightText.setText(fHeight+""); + } + private static Text addWidthText(Composite composite) { + addLabel(composite,"width:"); + final Text widthText=new Text(composite,SWT.BORDER); + setLayoutData(widthText,30); + widthText.addModifyListener(new ModifyListener(){ + public void modifyText(ModifyEvent e) { + synchronized (fTerminalModel) { + int width=textToInt(widthText); + if(width>1) + fTerminalModel.setDimensions(fTerminalModel.getHeight(), width); + } + } + }); + widthText.setText(fWidth+""); + return widthText; + } + private static Text addThrottleText(Composite composite) { + addLabel(composite,"throttle:"); + final Text throttleText=new Text(composite,SWT.BORDER); + setLayoutData(throttleText,30); + throttleText.addModifyListener(new ModifyListener(){ + public void modifyText(ModifyEvent e) { + synchronized (fTerminalModel) { + int throttle=textToInt(throttleText); + setThrottleForAll(throttle); + } + }}); + return throttleText; + } + private static void addStopAllButton(Composite composite, DataReader reader) { + final Button stopAllButton=new Button(composite,SWT.CHECK); + stopAllButton.setText("Stop ALL"); + stopAllButton.addSelectionListener(new SelectionAdapter(){ + + public void widgetSelected(SelectionEvent e) { + boolean stop=stopAllButton.getSelection(); + for (Iterator iterator = fDataReaders.iterator(); iterator.hasNext();) { + DataReader reader = (DataReader) iterator.next(); + reader.setStop(stop); + + }}}); + stopAllButton.setSelection(reader.isStart()); + } + private static void addAutorevealCursorButton(Composite composite) { + final Button button=new Button(composite,SWT.CHECK); + button.setText("ScrollLock"); + button.addSelectionListener(new SelectionAdapter(){ + public void widgetSelected(SelectionEvent e) { + boolean scrollLock=button.getSelection(); + fgTextCanvas.setScrollLock(scrollLock); + } + }); + button.setSelection(fgTextCanvas.isScrollLock()); + } + private static void addLabel(Composite composite,String message) { + Label label; + label=new Label(composite, SWT.NONE); + label.setText(message); + } + private static void addDataReader(Composite composite, final DataReader reader) { + fDataReaders.add(reader); + final Button button=new Button(composite,SWT.CHECK); + button.setText(reader.getName()); + button.addSelectionListener(new SelectionAdapter(){ + + public void widgetSelected(SelectionEvent e) { + reader.setStart(button.getSelection()); + }}); + button.setSelection(reader.isStart()); + + } + static private void setThrottleForAll(int throttle) { + for (Iterator iterator = fDataReaders.iterator(); iterator.hasNext();) { + DataReader reader = (DataReader) iterator.next(); + reader.setThrottleTime(throttle); + } + } + static void setLayoutData(Control c,int width) { + c.setLayoutData(new RowData(width,-1)); + } + static int textToInt(Text text) { + try { + return Integer.valueOf(text.getText()).intValue(); + } catch (Exception ex) { + return 0; + } + } +} + diff --git a/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/test/ui/VT100DataSource.java b/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/test/ui/VT100DataSource.java new file mode 100644 index 00000000000..9e6908df0ec --- /dev/null +++ b/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/test/ui/VT100DataSource.java @@ -0,0 +1,116 @@ +/******************************************************************************* + * Copyright (c) 2007 Wind River Systems, Inc. and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Michael Scharf (Wind River) - initial API and implementation + *******************************************************************************/ +package org.eclipse.tm.internal.terminal.test.ui; + +import java.io.ByteArrayOutputStream; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +import org.eclipse.tm.internal.terminal.control.impl.ITerminalControlForText; +import org.eclipse.tm.internal.terminal.emulator.VT100Emulator; +import org.eclipse.tm.internal.terminal.provisional.api.ITerminalConnectorInfo; +import org.eclipse.tm.internal.terminal.provisional.api.TerminalState; +import org.eclipse.tm.terminal.model.ITerminalTextData; + +/** + * Reads the file in an infinite loop. + * Makes lines containing 'x' bold. + * + */ +final class VT100DataSource implements IDataSource { + VT100Emulator fEmulator; + volatile int fAvailable; + volatile int fRead; + private final String fFile; + + VT100DataSource(String file) { + fFile=file; + } + class InfiniteFileInputStream extends InputStream { + public InfiniteFileInputStream() { + try { + fInputStream=new FileInputStream(fFile); + } catch (FileNotFoundException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + public int available() throws IOException { + return fAvailable; + } + private InputStream fInputStream; + public int read() throws IOException { + throw new IOException(); + } + public int read(byte[] b, int off, int len) throws IOException { + while(fAvailable==0) { + try { + Thread.sleep(1); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + } + } + len=fAvailable; + int n=fInputStream.read(b, off, len); + if(n<=0) { + fInputStream.close(); + fInputStream=new FileInputStream(fFile); + n=fInputStream.read(b, off, len); + } + fAvailable-=n; + return n; + } + + } + void init(ITerminalTextData terminal) { + fEmulator=new VT100Emulator(terminal,new ITerminalControlForText() { + + public void disconnectTerminal() { + // TODO Auto-generated method stub + + } + + public OutputStream getOutputStream() { + return new ByteArrayOutputStream(); + } + + public TerminalState getState() { + return TerminalState.CONNECTED; + } + + public ITerminalConnectorInfo getTerminalConnectorInfo() { + return null; + } + + public void setState(TerminalState state) { + } + + public void setTerminalTitle(String title) { + }},new InfiniteFileInputStream()); + } + public int step(ITerminalTextData terminal) { + synchronized(terminal) { + if(fEmulator==null) { + init(terminal); +// fEmulator.setDimensions(48, 132); + fEmulator.setDimensions(24, 80); + fEmulator.setCrAfterNewLine(true); + + } + fAvailable=80; + fEmulator.processText(); + } + return 80; + } +} \ No newline at end of file diff --git a/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/textcanvas/PipedInputStreamPerformanceTest.java b/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/textcanvas/PipedInputStreamPerformanceTest.java new file mode 100644 index 00000000000..dd105f603ad --- /dev/null +++ b/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/textcanvas/PipedInputStreamPerformanceTest.java @@ -0,0 +1,32 @@ +/******************************************************************************* + * Copyright (c) 2007 Wind River Systems, Inc. and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Michael Scharf (Wind River) - initial API and implementation + *******************************************************************************/ +package org.eclipse.tm.internal.terminal.textcanvas; + +import java.io.OutputStream; + +public class PipedInputStreamPerformanceTest { + + /** + * @param args + * @throws InterruptedException + */ + public static void main(String[] args) throws InterruptedException { + runPerformanceTest(); + runPerformanceTest(); + } + + private static void runPerformanceTest() throws InterruptedException { + PipedInputStream in=new PipedInputStream(1024); + OutputStream out=in.getOutputStream(); + PipedStreamTest.runPipe("",in, out,100); + } + +} diff --git a/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/textcanvas/PipedStreamTest.java b/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/textcanvas/PipedStreamTest.java new file mode 100644 index 00000000000..22ac1a03b7e --- /dev/null +++ b/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/internal/terminal/textcanvas/PipedStreamTest.java @@ -0,0 +1,103 @@ +package org.eclipse.tm.internal.terminal.textcanvas; + +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + + +class PipedStreamTest { + static class ReadThread extends Thread implements Runnable { + + InputStream pi = null; + + OutputStream po = null; + + ReadThread(String process, InputStream pi, OutputStream po) { + this.pi = pi; + this.po = po; + setDaemon(true); + } + + public void run() { + byte[] buffer = new byte[2048]; + int bytes_read; + try { + for (;;) { + bytes_read = pi.read(buffer); + if (bytes_read == -1) { + po.close(); + pi.close(); + return; + } + po.write(buffer, 0, bytes_read); + } + } catch (Exception e) { + e.printStackTrace(); + } + } + + } + static class FakeInputStream extends InputStream { + int N; + FakeInputStream(int n) { + N=n; + } + public int read(byte[] b, int off, int len) throws IOException { + if(N==0) + return -1; + int n=Math.min(len,N); + for (int i = off; i < off+n; i++) { + b[i]='x'; + } + N-=n; + return n; + } + public int read() throws IOException { + throw new UnsupportedOperationException(); + } + /* + * available has to be implemented! + */ + public int available() throws IOException { + return N; + } + } + static class FakeOutputStream extends OutputStream { + long N; + public void write(int b) throws IOException { + throw new UnsupportedOperationException(); + } + public void write(byte[] b, int off, int len) throws IOException { + N+=len; + } + } + public static void main(String[] args) throws IOException, InterruptedException { + while(true) { + runSunTest(); + runMyTest(); + } + } + private static void runSunTest() throws IOException, InterruptedException { + java.io.PipedInputStream in = new java.io.PipedInputStream(); + OutputStream out = new java.io.PipedOutputStream(in); + runPipe("Sun ",in, out,10); + } + private static void runMyTest() throws IOException, InterruptedException { + PipedInputStream in=new PipedInputStream(4*1024); + OutputStream out=in.getOutputStream(); + runPipe("My ",in, out,99); + } + public static void runPipe(String what,InputStream writeIn, OutputStream readOut,int N) throws InterruptedException { + FakeInputStream in=new FakeInputStream(N*1000*1000); + FakeOutputStream out=new FakeOutputStream(); + ReadThread rt = new ReadThread("reader", in , readOut); + ReadThread wt = new ReadThread("writer", writeIn, out); + long t0=System.currentTimeMillis(); + rt.start(); + wt.start(); + wt.join(); + long t=System.currentTimeMillis(); + long n=out.N; + System.out.println(what+n + " byte in " +(t-t0)+" ms -> "+(1000*n)/((t-t0+1)*1024)+" kb/sec"); + } +} diff --git a/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/terminal/model/StyleColorTest.java b/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/terminal/model/StyleColorTest.java new file mode 100644 index 00000000000..e641c74d7b6 --- /dev/null +++ b/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/terminal/model/StyleColorTest.java @@ -0,0 +1,36 @@ +/******************************************************************************* + * Copyright (c) 2007 Wind River Systems, Inc. and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Michael Scharf (Wind River) - initial API and implementation + *******************************************************************************/ +package org.eclipse.tm.terminal.model; + +import junit.framework.TestCase; + +public class StyleColorTest extends TestCase { + + + public void testEqualsObject() { + assertEquals(StyleColor.getStyleColor("foo"),StyleColor.getStyleColor("foo")); + assertFalse(StyleColor.getStyleColor("foox").equals(StyleColor.getStyleColor("foo"))); + } + + public void testSameObject() { + assertSame(StyleColor.getStyleColor("foo"),StyleColor.getStyleColor("foo")); + assertNotSame(StyleColor.getStyleColor("foox"),StyleColor.getStyleColor("foo")); + } + + public void testToString() { + assertEquals("xxx", StyleColor.getStyleColor("xxx").toString()); + } + + public void testGetName() { + assertEquals("xxx", StyleColor.getStyleColor("xxx").getName()); + } + +} diff --git a/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/terminal/model/StyleTest.java b/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/terminal/model/StyleTest.java new file mode 100644 index 00000000000..9f0cd75172f --- /dev/null +++ b/terminal/org.eclipse.tm.terminal.test/src/org/eclipse/tm/terminal/model/StyleTest.java @@ -0,0 +1,116 @@ +/******************************************************************************* + * Copyright (c) 2007 Wind River Systems, Inc. and others. + * All rights reserved. This program and the accompanying materials + * are made available under the terms of the Eclipse Public License v1.0 + * which accompanies this distribution, and is available at + * http://www.eclipse.org/legal/epl-v10.html + * + * Contributors: + * Michael Scharf (Wind River) - initial API and implementation + *******************************************************************************/ +package org.eclipse.tm.terminal.model; + +import junit.framework.TestCase; + +public class StyleTest extends TestCase { + final StyleColor c1=StyleColor.getStyleColor("c1"); + final StyleColor c2=StyleColor.getStyleColor("c2"); + final StyleColor c3=StyleColor.getStyleColor("c3"); + + public void testGetStyle() { + Style s1=Style.getStyle(c1, c2, true, false, true, false); + Style s2=Style.getStyle(c1, c2, true, false, true, false); + assertEquals(s1,s2); + assertSame(s1,s2); + s1=s1.setBlink(!s1.isBlink()); + assertNotSame(s1,s2); + assertFalse(s1.equals(s2)); + s1=s1.setBlink(!s1.isBlink()); + assertSame(s1,s2); + } + + public void testSetForground() { + Style s1=Style.getStyle(c1, c2, true, false, true, false); + Style s2=s1; + s2=s1.setForground(c3); + assertNotSame(s1,s2); + assertFalse(s1.equals(s2)); + assertSame(s2.getForground(), c3); + assertSame(s1.getForground(), c1); + assertSame(s1.getBackground(), c2); + assertSame(s2.getBackground(), c2); + s2=s2.setForground(c1); + assertSame(s1, s2); + } + + public void testSetBackground() { + Style s1=Style.getStyle(c1, c2, true, false, true, false); + Style s2=s1; + s2=s1.setBackground(c3); + assertNotSame(s1,s2); + assertFalse(s1.equals(s2)); + assertSame(s2.getForground(), c1); + assertSame(s1.getForground(), c1); + assertSame(s1.getBackground(), c2); + assertSame(s2.getBackground(), c3); + s2=s2.setBackground(c2); + assertSame(s1, s2); + } + + public void testSetBold() { + Style s1=getDefaultStyle(); + Style s2=s1; + assertSame(s1,s2); + assertFalse(s2.isBold()); + s2=s2.setBold(true); + assertNotSame(s1,s2); + assertTrue(s2.isBold()); + s2=s2.setBold(false); + assertSame(s1,s2); + assertFalse(s2.isBold()); + } + + public void testSetBlink() { + Style s1=getDefaultStyle(); + Style s2=s1; + assertSame(s1,s2); + assertFalse(s2.isBlink()); + s2=s2.setBlink(true); + assertNotSame(s1,s2); + assertTrue(s2.isBlink()); + s2=s2.setBlink(false); + assertSame(s1,s2); + assertFalse(s2.isBlink()); + } + + public void testSetUnderline() { + Style s1=getDefaultStyle(); + Style s2=s1; + assertSame(s1,s2); + assertFalse(s2.isUnderline()); + s2=s2.setUnderline(true); + assertNotSame(s1,s2); + assertTrue(s2.isUnderline()); + s2=s2.setUnderline(false); + assertSame(s1,s2); + assertFalse(s2.isUnderline()); + } + + public void testSetReverse() { + Style s1=getDefaultStyle(); + Style s2=s1; + assertSame(s1,s2); + assertFalse(s2.isReverse()); + s2=s2.setReverse(true); + assertNotSame(s1,s2); + assertTrue(s2.isReverse()); + s2=s2.setReverse(false); + assertSame(s1,s2); + assertFalse(s2.isReverse()); + } + + private Style getDefaultStyle() { + return Style.getStyle(c1, c2, false, false, false, false); + } + +} diff --git a/terminal/org.eclipse.tm.terminal.test/src/readme.txt b/terminal/org.eclipse.tm.terminal.test/src/readme.txt deleted file mode 100644 index e69de29bb2d..00000000000