1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-07-03 15:15:25 +02:00

merged performance branch into main

This commit is contained in:
Michael Scharf 2007-10-04 18:38:31 +00:00
parent a4699ff8ef
commit e0ac61bfdc
33 changed files with 6703 additions and 1 deletions

View file

@ -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

View file

@ -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));
}
}

View file

@ -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 && i<windowStart+windowSize;
assertEquals(e, changes.hasLineChanged(i));
}
}
public void testMarkLinesChanged() {
SnapshotChanges changes=new SnapshotChanges(2,3);
assertFalse(changes.hasChanged());
assertEquals(Integer.MAX_VALUE, changes.getFirstChangedLine());
assertEquals(-1, changes.getLastChangedLine());
changes.markLinesChanged(0, 1);
assertChangedLines(changes, "00000000000");
assertFalse(changes.hasChanged());
assertEquals(Integer.MAX_VALUE, changes.getFirstChangedLine());
assertEquals(-1, changes.getLastChangedLine());
changes.markLinesChanged(0, 2);
assertChangedLines(changes, "00000000000");
assertFalse(changes.hasChanged());
assertEquals(Integer.MAX_VALUE, changes.getFirstChangedLine());
assertEquals(-1, changes.getLastChangedLine());
changes.markLinesChanged(0, 3);
assertEquals(2, changes.getFirstChangedLine());
assertEquals(2, changes.getLastChangedLine());
assertTrue(changes.hasChanged());
assertChangedLines(changes, "00100000000");
changes=new SnapshotChanges(2,3);
changes.markLinesChanged(1, 3);
assertTrue(changes.hasChanged());
assertEquals(2, changes.getFirstChangedLine());
assertEquals(3, changes.getLastChangedLine());
assertChangedLines(changes, "00110000000");
changes=new SnapshotChanges(2,3);
changes.markLinesChanged(1, 4);
assertEquals(2, changes.getFirstChangedLine());
assertEquals(4, changes.getLastChangedLine());
assertTrue(changes.hasChanged());
assertChangedLines(changes, "00111000000");
changes=new SnapshotChanges(2,3);
changes.markLinesChanged(1, 4);
assertEquals(2, changes.getFirstChangedLine());
assertEquals(4, changes.getLastChangedLine());
assertTrue(changes.hasChanged());
assertChangedLines(changes, "00111000000");
changes=new SnapshotChanges(2,3);
changes.markLinesChanged(2, 4);
assertEquals(2, changes.getFirstChangedLine());
assertEquals(4, changes.getLastChangedLine());
assertTrue(changes.hasChanged());
assertChangedLines(changes, "00111000000");
changes=new SnapshotChanges(2,3);
changes.markLinesChanged(3, 4);
assertEquals(3, changes.getFirstChangedLine());
assertEquals(4, changes.getLastChangedLine());
assertTrue(changes.hasChanged());
assertChangedLines(changes, "00011000000");
changes=new SnapshotChanges(2,3);
changes.markLinesChanged(3, 1);
assertEquals(3, changes.getFirstChangedLine());
assertEquals(3, changes.getLastChangedLine());
assertTrue(changes.hasChanged());
assertChangedLines(changes, "00010000000");
changes=new SnapshotChanges(2,3);
changes.markLinesChanged(4, 1);
assertEquals(4, changes.getFirstChangedLine());
assertEquals(4, changes.getLastChangedLine());
assertTrue(changes.hasChanged());
assertChangedLines(changes, "00001000000");
changes=new SnapshotChanges(2,3);
changes.markLinesChanged(5, 1);
assertEquals(Integer.MAX_VALUE, changes.getFirstChangedLine());
assertEquals(-1, changes.getLastChangedLine());
assertFalse(changes.hasChanged());
assertChangedLines(changes, "00000000000");
}
public void testMarkLinesChangedNoWindow() {
SnapshotChanges changes=new SnapshotChanges(10);
assertFalse(changes.hasChanged());
assertEquals(Integer.MAX_VALUE, changes.getFirstChangedLine());
assertEquals(-1, changes.getLastChangedLine());
changes.markLinesChanged(0, 1);
assertTrue(changes.hasChanged());
assertEquals(0, changes.getFirstChangedLine());
assertEquals(0, changes.getLastChangedLine());
assertChangedLines(changes, "1000000000");
changes=new SnapshotChanges(10);
changes.markLinesChanged(0, 5);
assertTrue(changes.hasChanged());
assertEquals(0, changes.getFirstChangedLine());
assertEquals(4, changes.getLastChangedLine());
assertChangedLines(changes, "1111100000");
changes=new SnapshotChanges(3);
changes.markLinesChanged(1, 6);
assertTrue(changes.hasChanged());
assertEquals(1, changes.getFirstChangedLine());
assertEquals(6, changes.getLastChangedLine());
assertChangedLines(changes, "011");
changes=new SnapshotChanges(10);
changes.markLinesChanged(5, 6);
assertTrue(changes.hasChanged());
assertEquals(5, changes.getFirstChangedLine());
assertEquals(10, changes.getLastChangedLine());
assertChangedLines(changes, "0000011111");
}
public void testHasChanged() {
SnapshotChanges changes=new SnapshotChanges(0);
assertFalse(changes.hasChanged());
changes=new SnapshotChanges(1);
assertFalse(changes.hasChanged());
changes=new SnapshotChanges(1,9);
assertFalse(changes.hasChanged());
}
public void testSetAllChanged() {
SnapshotChanges changes;
changes=new SnapshotChanges(2,3);
changes.setAllChanged(10);
assertEquals(2, changes.getFirstChangedLine());
assertEquals(4, changes.getLastChangedLine());
assertTrue(changes.hasChanged());
assertChangedLines(changes, "00111000000");
changes=new SnapshotChanges(2,3);
changes.setAllChanged(3);
assertEquals(2, changes.getFirstChangedLine());
assertEquals(2, changes.getLastChangedLine());
assertTrue(changes.hasChanged());
assertChangedLines(changes, "00111000000");
changes=new SnapshotChanges(2,3);
changes.setAllChanged(4);
assertEquals(2, changes.getFirstChangedLine());
assertEquals(3, changes.getLastChangedLine());
assertTrue(changes.hasChanged());
assertChangedLines(changes, "00111000000");
changes=new SnapshotChanges(2,3);
changes.setAllChanged(5);
assertEquals(2, changes.getFirstChangedLine());
assertEquals(4, changes.getLastChangedLine());
assertTrue(changes.hasChanged());
assertChangedLines(changes, "00111000000");
changes=new SnapshotChanges(2,3);
changes.setAllChanged(6);
assertEquals(2, changes.getFirstChangedLine());
assertEquals(4, changes.getLastChangedLine());
assertTrue(changes.hasChanged());
assertChangedLines(changes, "00111000000");
}
public void testSetAllChangedNoWindow() {
SnapshotChanges changes;
changes=new SnapshotChanges(5);
changes.setAllChanged(10);
assertEquals(0, changes.getFirstChangedLine());
assertEquals(9, changes.getLastChangedLine());
assertTrue(changes.hasChanged());
assertChangedLines(changes, "1111111111");
changes=new SnapshotChanges(5);
changes.setAllChanged(3);
assertEquals(0, changes.getFirstChangedLine());
assertEquals(2, changes.getLastChangedLine());
assertTrue(changes.hasChanged());
assertChangedLines(changes, "1111111111");
}
public void testConvertScrollingIntoChanges() {
SnapshotChanges changes;
changes=new SnapshotChanges(2,3);
changes.scroll(0, 4, -1);
assertTrue(changes.hasChanged());
assertChangedLines(changes, "000100");
changes.convertScrollingIntoChanges();
assertEquals(2, changes.getFirstChangedLine());
assertEquals(3, changes.getLastChangedLine());
assertEquals(0, changes.getScrollWindowStartLine());
assertEquals(0, changes.getScrollWindowSize());
assertEquals(0, changes.getScrollWindowShift());
assertChangedLines(changes, "001100");
}
public void testConvertScrollingIntoChangesNoWindow() {
SnapshotChanges changes;
changes=new SnapshotChanges(7);
changes.scroll(0, 4, -1);
assertTrue(changes.hasChanged());
assertChangedLines(changes, "000100");
changes.convertScrollingIntoChanges();
assertEquals(0, changes.getFirstChangedLine());
assertEquals(3, changes.getLastChangedLine());
assertEquals(0, changes.getScrollWindowStartLine());
assertEquals(0, changes.getScrollWindowSize());
assertEquals(0, changes.getScrollWindowShift());
assertChangedLines(changes, "111100");
}
public void testScrollNoWindow() {
SnapshotChanges changes;
changes=new SnapshotChanges(7);
changes.scroll(0, 3, -2);
assertEquals(1, changes.getFirstChangedLine());
assertEquals(2, changes.getLastChangedLine());
assertEquals(0, changes.getScrollWindowStartLine());
assertEquals(3, changes.getScrollWindowSize());
assertEquals(-2, changes.getScrollWindowShift());
assertTrue(changes.hasChanged());
assertChangedLines(changes, "0110000");
changes=new SnapshotChanges(7);
changes.scroll(0, 3, -1);
changes.scroll(0, 3, -1);
assertEquals(1, changes.getFirstChangedLine());
assertEquals(2, changes.getLastChangedLine());
assertEquals(0, changes.getScrollWindowStartLine());
assertEquals(3, changes.getScrollWindowSize());
assertEquals(-2, changes.getScrollWindowShift());
assertTrue(changes.hasChanged());
assertChangedLines(changes, "0110000");
changes=new SnapshotChanges(7);
changes.scroll(0, 7, -1);
changes.scroll(0, 7, -1);
assertEquals(5, changes.getFirstChangedLine());
assertEquals(6, changes.getLastChangedLine());
assertEquals(0, changes.getScrollWindowStartLine());
assertEquals(7, changes.getScrollWindowSize());
assertEquals(-2, changes.getScrollWindowShift());
assertTrue(changes.hasChanged());
assertChangedLines(changes, "0000011");
// positive scrolls cannot be optimized at the moment
changes=new SnapshotChanges(7);
changes.scroll(0, 7, 1);
changes.scroll(0, 7, 1);
assertEquals(0, changes.getFirstChangedLine());
assertEquals(6, changes.getLastChangedLine());
assertEquals(0, changes.getScrollWindowStartLine());
assertEquals(0, changes.getScrollWindowSize());
assertEquals(0, changes.getScrollWindowShift());
assertTrue(changes.hasChanged());
assertChangedLines(changes, "1111111");
}
public void testScroll() {
SnapshotChanges changes;
changes=new SnapshotChanges(2,3);
changes.scroll(0, 7, -1);
assertEquals(4, changes.getFirstChangedLine());
assertEquals(4, changes.getLastChangedLine());
assertEquals(2, changes.getScrollWindowStartLine());
assertEquals(3, changes.getScrollWindowSize());
assertEquals(-1, changes.getScrollWindowShift());
assertTrue(changes.hasChanged());
assertChangedLines(changes, "0000100000");
changes=new SnapshotChanges(2,3);
changes.scroll(0, 7, -2);
assertEquals(3, changes.getFirstChangedLine());
assertEquals(4, changes.getLastChangedLine());
assertEquals(2, changes.getScrollWindowStartLine());
assertEquals(3, changes.getScrollWindowSize());
assertEquals(-2, changes.getScrollWindowShift());
assertTrue(changes.hasChanged());
assertChangedLines(changes, "0001100000");
}
public void testScrollNergative() {
SnapshotChanges changes;
changes=new SnapshotChanges(2,3);
changes.scroll(0, 7, -1);
changes.scroll(0, 7, -1);
assertEquals(3, changes.getFirstChangedLine());
assertEquals(4, changes.getLastChangedLine());
assertEquals(2, changes.getScrollWindowStartLine());
assertEquals(3, changes.getScrollWindowSize());
assertEquals(-2, changes.getScrollWindowShift());
assertTrue(changes.hasChanged());
assertChangedLines(changes, "0001100000");
}
public void testScrollPositive() {
SnapshotChanges changes;
changes=new SnapshotChanges(2,3);
changes.scroll(0, 7, 1);
changes.scroll(0, 7, 1);
assertEquals(2, changes.getFirstChangedLine());
assertEquals(4, changes.getLastChangedLine());
assertEquals(0, changes.getScrollWindowStartLine());
assertEquals(0, changes.getScrollWindowSize());
assertEquals(0, changes.getScrollWindowShift());
assertTrue(changes.hasChanged());
assertChangedLines(changes, "0011100000");
changes=new SnapshotChanges(2,3);
changes.scroll(0, 3, 1);
changes.scroll(0, 3, 1);
assertEquals(2, changes.getFirstChangedLine());
assertEquals(2, changes.getLastChangedLine());
assertEquals(0, changes.getScrollWindowStartLine());
assertEquals(0, changes.getScrollWindowSize());
assertEquals(0, changes.getScrollWindowShift());
assertTrue(changes.hasChanged());
assertChangedLines(changes, "0010000000");
changes=new SnapshotChanges(2,3);
changes.scroll(0, 4, 1);
changes.scroll(0, 4, 1);
assertEquals(2, changes.getFirstChangedLine());
assertEquals(3, changes.getLastChangedLine());
assertEquals(0, changes.getScrollWindowStartLine());
assertEquals(0, changes.getScrollWindowSize());
assertEquals(0, changes.getScrollWindowShift());
assertTrue(changes.hasChanged());
assertChangedLines(changes, "0011000000");
changes=new SnapshotChanges(2,3);
changes.scroll(0, 5, 1);
changes.scroll(0, 5, 1);
assertEquals(2, changes.getFirstChangedLine());
assertEquals(4, changes.getLastChangedLine());
assertEquals(0, changes.getScrollWindowStartLine());
assertEquals(0, changes.getScrollWindowSize());
assertEquals(0, changes.getScrollWindowShift());
assertTrue(changes.hasChanged());
assertChangedLines(changes, "0011100000");
changes=new SnapshotChanges(2,3);
changes.scroll(3, 5, 1);
changes.scroll(3, 5, 1);
assertEquals(3, changes.getFirstChangedLine());
assertEquals(4, changes.getLastChangedLine());
assertEquals(0, changes.getScrollWindowStartLine());
assertEquals(0, changes.getScrollWindowSize());
assertEquals(0, changes.getScrollWindowShift());
assertTrue(changes.hasChanged());
assertChangedLines(changes, "0001100000");
}
public void testCopyChangedLines() {
SnapshotChanges changes;
changes=new SnapshotChanges(2,3);
changes.markLineChanged(3);
ITerminalTextData source=new TerminalTextDataStore();
TerminalTextTestHelper.fillSimple(source, "01234567890");
ITerminalTextData dest=new TerminalTextDataStore();
TerminalTextTestHelper.fillSimple(dest, "abcdefghijk");
changes.copyChangedLines(dest, source);
assertEquals("abc3efghijk",TerminalTextTestHelper.toSimple(dest));
changes=new SnapshotChanges(2,3);
changes.setAllChanged(7);
source=new TerminalTextDataStore();
TerminalTextTestHelper.fillSimple(source, "01234567890");
dest=new TerminalTextDataStore();
TerminalTextTestHelper.fillSimple(dest, "abcdefghijk");
changes.copyChangedLines(dest, source);
assertEquals("ab234fghijk",TerminalTextTestHelper.toSimple(dest));
changes=new SnapshotChanges(2,3);
changes.scroll(0,7,-1);
source=new TerminalTextDataStore();
TerminalTextTestHelper.fillSimple(source, "01234567890");
dest=new TerminalTextDataStore();
TerminalTextTestHelper.fillSimple(dest, "abcdefghijk");
// only one line has changed! The other lines are scrolled!
assertChangedLines(changes,"00001000");
changes.copyChangedLines(dest, source);
assertEquals("abcd4fghijk",TerminalTextTestHelper.toSimple(dest));
}
public void testCopyChangedLinesWithSmallSource() {
SnapshotChanges changes;
changes=new SnapshotChanges(2,3);
changes.markLineChanged(3);
ITerminalTextData source=new TerminalTextDataStore();
source.setDimensions(2, 2);
TerminalTextDataWindow dest=new TerminalTextDataWindow();
dest.setWindow(2, 2);
changes.copyChangedLines(dest, source);
}
public void testCopyChangedLinesWithSmallSource1() {
SnapshotChanges changes;
changes=new SnapshotChanges(2,3);
changes.markLineChanged(3);
ITerminalTextData source=new TerminalTextDataStore();
TerminalTextTestHelper.fillSimple(source, "01");
ITerminalTextData dest=new TerminalTextDataStore();
changes.copyChangedLines(dest, source);
}
public void testSetInterestWindowSize() {
SnapshotChanges changes;
changes=new SnapshotChanges(2,3);
// move the window
changes.setInterestWindow(3, 3);
// only one line has changed! The other lines are scrolled!
assertEquals(3, changes.getScrollWindowStartLine());
assertEquals(3, changes.getScrollWindowSize());
assertEquals(-1, changes.getScrollWindowShift());
assertChangedLines(changes,"0000010");
changes.convertScrollingIntoChanges();
assertChangedLines(changes,"0001110");
changes=new SnapshotChanges(2,3);
// move the window
changes.setInterestWindow(3, 4);
// only one line has changed! The other lines are scrolled!
assertEquals(3, changes.getScrollWindowStartLine());
assertEquals(3, changes.getScrollWindowSize());
assertEquals(-1, changes.getScrollWindowShift());
assertChangedLines(changes,"0000011");
changes.convertScrollingIntoChanges();
assertChangedLines(changes,"0001111");
changes=new SnapshotChanges(2,3);
// move the window
changes.setInterestWindow(6, 3);
// cannot scroll
assertEquals(0, changes.getScrollWindowStartLine());
assertEquals(0, changes.getScrollWindowSize());
assertEquals(0, changes.getScrollWindowShift());
assertChangedLines(changes,"000000111000");
changes=new SnapshotChanges(2,3);
// expand the window
changes.setInterestWindow(2, 5);
// cannot scroll
assertEquals(0, changes.getScrollWindowStartLine());
assertEquals(0, changes.getScrollWindowSize());
assertEquals(0, changes.getScrollWindowShift());
assertChangedLines(changes,"0000011000");
}
public void testSetInterestWindowSize2() {
SnapshotChanges changes;
changes=new SnapshotChanges(2,3);
// move the window
changes.setInterestWindow(1, 3);
assertChangedLines(changes,"0111000");
changes=new SnapshotChanges(2,3);
// move the window
changes.setInterestWindow(1, 4);
assertChangedLines(changes,"01111000");
changes=new SnapshotChanges(2,3);
// expand the window
changes.setInterestWindow(6, 3);
assertChangedLines(changes,"000000111000");
changes=new SnapshotChanges(2,3);
// expand the window
changes.setInterestWindow(1, 2);
assertChangedLines(changes,"0110000");
}
}

View file

@ -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 SynchronizedTerminalTextDataTest extends AbstractITerminalTextDataTest {
protected ITerminalTextData makeITerminalTextData() {
return new SynchronizedTerminalTextData(new TerminalTextData());
}
}

View file

@ -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 TerminalTextDataFastScrollTest extends AbstractITerminalTextDataTest {
protected ITerminalTextData makeITerminalTextData() {
return new TerminalTextDataFastScroll(3);
}
}

View file

@ -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 TerminalTextDataFastScrollTestMaxHeigth extends AbstractITerminalTextDataTest {
protected ITerminalTextData makeITerminalTextData() {
return new TerminalTextDataFastScroll(1);
}
}

View file

@ -0,0 +1,224 @@
/*******************************************************************************
* 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.ITerminalTextDataSnapshot;
import org.eclipse.tm.terminal.model.Style;
import org.eclipse.tm.terminal.model.StyleColor;
public class TerminalTextDataPerformanceTest extends TestCase {
long TIME=100;
private void initPerformance(ITerminalTextData term) {
term.setDimensions(300,200);
}
public void testPerformance0() {
ITerminalTextData term=new TerminalTextData();
method0(term,"0 ");
}
public void testPerformance0a() {
ITerminalTextData term=new TerminalTextData();
ITerminalTextDataSnapshot snapshot=term.makeSnapshot();
method0(term,"0a");
snapshot.updateSnapshot(true);
}
public void testPerformance0b() {
ITerminalTextData term=new TerminalTextData();
ITerminalTextDataSnapshot snapshot=term.makeSnapshot();
N=0;
snapshot.addListener(new ITerminalTextDataSnapshot.SnapshotOutOfDateListener(){
public void snapshotOutOfDate(ITerminalTextDataSnapshot snapshot) {
N++;
}});
method0(term,"0b");
snapshot.updateSnapshot(true);
}
private void method0(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();
for (int i = 0; i < 10000000; i++) {
char c=s.charAt(i%s.length());
for (int line = 0; line < term.getHeight(); line++) {
for (int column = 0; column < term.getWidth(); column++) {
term.setChar(line, column, c, style);
n++;
}
}
if(System.currentTimeMillis()-t0>TIME) {
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;
}
}
}
}
}

View file

@ -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");
}
}

View file

@ -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();
}
}

View file

@ -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();
}
}

View file

@ -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+fSize)
b.append(lines[i]);
else
b.append(new String(new char[lines[i].length()]));
}
return b.toString();
}
/**
* Used for simple text
* @param expected
* @param actual
*/
protected void assertEqualsSimple(String expected,String actual) {
assertEquals(stripSimple(expected), stripSimple(actual));
}
String stripSimple(String s) {
StringBuffer b=new StringBuffer();
for (int i = 0; i < s.length(); i++) {
if(i>=fOffset && i<fOffset+fSize)
b.append(s.charAt(i));
else
b.append(' ');
}
return b.toString();
}
public void testAddLine() {
String s=
"111\n" +
"222\n" +
"333\n" +
"444\n" +
"555";
ITerminalTextData term=makeITerminalTextData();
fill(term, s);
term.addLine();
assertEqualsTerm(
"222\n" +
"333\n" +
"444\n" +
"\0\0\0\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" +
"\000\000\000\n" +
"\000\000\000\n" +
"\000\000\000\n" +
"\000\000\000\n" +
"\000\000\000", toMultiLineText(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(2, 0,"0123".toCharArray(), s1);
term.setChars(2, 4,"abcd".toCharArray(), null);
segments=term.getLineSegments(2, 0, term.getWidth());
assertEquals(2, segments.length);
assertSegment(0, "0123", s1, segments[0]);
assertSegment(4, "abcd", null, segments[1]);
segments=term.getLineSegments(2, 4, term.getWidth()-4);
assertEquals(1, segments.length);
assertSegment(4, "abcd", null, segments[0]);
segments=term.getLineSegments(2, 3, 2);
assertEquals(2, segments.length);
assertSegment(3, "3", s1, segments[0]);
assertSegment(4, "a", null, segments[1]);
segments=term.getLineSegments(2, 7, 1);
assertEquals(1, segments.length);
assertSegment(7, "d", null, segments[0]);
segments=term.getLineSegments(2, 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(1, segments.length);
assertSegment(0, "\000\000\000\000\000\000\000\000", null, segments[0]);
// 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 testGetChar() {
String s="12345\n" +
"abcde\n" +
"ABCDE";
ITerminalTextData term=makeITerminalTextData();
fill(term, s);
assertEquals('\000', term.getChar(0,0));
assertEquals('\000', term.getChar(0,1));
assertEquals('\000', term.getChar(0,2));
assertEquals('\000', term.getChar(0,3));
assertEquals('\000', term.getChar(0,4));
assertEquals('\000', term.getChar(1,0));
assertEquals('\000', term.getChar(1,1));
assertEquals('\000', term.getChar(1,2));
assertEquals('\000', term.getChar(1,3));
assertEquals('\000', 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));
}
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);
Style s=null;
if(line>=fOffset&&line<fOffset+fSize)
s=style.setForground(StyleColor.getStyleColor(""+c));
assertSame(s, term.getStyle(line, column));
}
}
}
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=0;
if(line>=fOffset&&line<fOffset+fSize)
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=0;
if(line>=fOffset&&line<fOffset+fSize)
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));
// check if chars are correctly chopped
term.setChars(4, 1, new char[]{'1','2','3','4','5'}, null);
assertEqualsTerm(
"abc\n"
+ "bcd\n"
+ "cde\n"
+ "d12\n"
+ "e12\n"
+ "fgh", toMultiLineText(term));
}
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);
}
public void testSetCopyLines() {
ITerminalTextData term=new TerminalTextDataStore();
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));
}
public void testScrollNegative() {
scrollTest(0,2,-1," 23 "," 23 ");
scrollTest(0,1,-1," 23 "," 23 ");
scrollTest(0,6,-1," 23 "," 3 ");
scrollTest(0,6,-6," 23 "," ");
scrollTest(0,6,-7," 23 "," ");
scrollTest(0,6,-8," 23 "," ");
scrollTest(0,6,-2," 23 "," ");
scrollTest(1,1,-1," 23 "," 23 ");
scrollTest(1,2,-1," 23 "," 3 ");
scrollTest(5,1,-1," 23 "," 23 ");
scrollTest(5,1,-1," 23 "," 23 ");
}
public void testScrollAll() {
scrollTest(0,6,1, " 2345"," 2 ");
scrollTest(0,6,-1, " 2345"," 3 ");
scrollTest(0,6,2, " 2345"," ");
scrollTest(0,6,-2, " 2345"," ");
}
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" +
"\00\00\00\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 testCopy() {
ITerminalTextData term=makeITerminalTextData();
term.setDimensions(3, 1);
ITerminalTextData data=new TerminalTextData();
fillSimple(data,"abcd");
term.copy(data);
}
}

View file

@ -0,0 +1,92 @@
/*******************************************************************************
* 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.ITerminalTextDataReadOnly;
import org.eclipse.tm.terminal.model.Style;
import org.eclipse.tm.terminal.model.StyleColor;
public class TerminalTextTestHelper {
static public String toSimple(ITerminalTextDataReadOnly term) {
return toMultiLineText(term).replaceAll("\000", " ").replaceAll("\n", "");
}
static public String toMultiLineText(ITerminalTextDataReadOnly term) {
StringBuffer buff=new StringBuffer();
int width=term.getWidth();
for (int line = 0; line < term.getHeight(); line++) {
if(line>0)
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++;
}
}
}
}

View file

@ -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 ();
}
}

View file

@ -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 ();
}
}

View file

@ -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);
}
}
}

View file

@ -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 <code>Canvas</code> 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.x<visibleRect.x) {
deltaX=rect.x-visibleRect.x;
} else if(visibleRect.x+visibleRect.width<rect.x+rect.width){
deltaX=(rect.x+rect.width)-(visibleRect.x+visibleRect.width);
}
if(deltaX!=0) {
getHorizontalBar().setSelection(-fVirtualBounds.x+deltaX);
scrollX(getHorizontalBar());
}
// scroll the Y part
int deltaY=0;
if(rect.y<visibleRect.y){
deltaY=rect.y-visibleRect.y;
} else if(visibleRect.y+visibleRect.height<rect.y+rect.height){
deltaY=(rect.y+rect.height)-(visibleRect.y+visibleRect.height);
}
if(deltaY!=0) {
getVerticalBar().setSelection(-fVirtualBounds.y+deltaY);
scrollY(getVerticalBar());
}
}
protected void repaint(Rectangle r) {
if (fPaintGC!=null) {
if(inClipping(r,fClientArea)) {
fPaintGC.setClipping(r);
paint(fPaintGC);
}
}
}
/**
* @param gc
*/
abstract protected void paint(GC gc);
protected Color getBackgroundColor() {
return getDisplay().getSystemColor(SWT.COLOR_LIST_BACKGROUND);
}
protected void paintUnoccupiedSpace(GC gc, Rectangle clipping) {
int width=fVirtualBounds.width;
int height=fVirtualBounds.height;
int marginWidth = (clipping.x+clipping.width) - width;
int marginHeight = (clipping.y+clipping.height) - height;
if(marginWidth>0||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);
}
}
}

View file

@ -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;
}
}

View file

@ -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;
}
}

View file

@ -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++;
}
}

View file

@ -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();
}
}
}

View file

@ -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);
}

View file

@ -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);
}

View file

@ -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++;
}
}

View file

@ -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;
}
}

View file

@ -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;
}
}
}

View file

@ -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;
}
}

View file

@ -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);
}
}

View file

@ -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");
}
}

View file

@ -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());
}
}

View file

@ -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);
}
}