AttributeMap boldAttrs = new AttributeMap(TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD);
AttributeMap italicAttrs = new AttributeMap(TextAttribute.POSTURE, TextAttribute.POSTURE_OBLIQUE);
AttributeMap emptyAttrs = AttributeMap.EMPTY_ATTRIBUTE_MAP;
final String bold1Str_getString = "b";
MConstText bold1Str = new StyledText(bold1Str_getString, boldAttrs);
final String italic1Str_getString = "i";
MConstText italic1Str = new StyledText(italic1Str_getString, italicAttrs);
final String plain1Str_getString = "p";
MConstText plain1Str = new StyledText(plain1Str_getString, emptyAttrs);
StyledText temp = new StyledText();
temp.append(bold1Str);
temp.append(italic1Str);
final String boldItalicStr_getString = bold1Str_getString.concat(italic1Str_getString);
MConstText boldItalicStr = temp;
temp = new StyledText();
temp.append(bold1Str);
temp.append(bold1Str);
temp.append(bold1Str);
final String bold3Str_getString = "bbb";
MConstText bold3Str = temp;
MText buf = new StyledText();
String plainText = new String();
//int testIteration=0; - now instance variables so errln can report it
//int theCase=0;
final int NUM_CASES = 14;
boolean[] casesExecuted = new boolean[NUM_CASES];
final int stopAt = -1;
Random rand = new Random(RAND_SEED);
final String ALWAYS_DIFFERENT = "\uFEFF";
for (testIteration=0; testIteration < TEST_ITERATIONS; testIteration++) {
theCase = randInt(rand, NUM_CASES);
casesExecuted[theCase] = true;
if (testIteration == stopAt) {
testIteration = stopAt; // Convenient place to put breakpoint
}
int timeStamp = buf.getTimeStamp();
String oldPlainText = plainText;
if (oldPlainText == null) {
errln("oldPlainText is null!");
}
switch (theCase) {
case 0:
// create new string; replace chars at start with different style
buf = new StyledText();
buf.append(bold3Str);
buf.replace(0, 1, italic1Str, 0, italic1Str.length());
buf.replace(0, 0, italic1Str, 0, italic1Str.length());
plainText = bold3Str_getString.substring(1, bold3Str.length());
plainText = italic1Str_getString.concat(plainText);
plainText = italic1Str_getString.concat(plainText);
oldPlainText = null;
break;
case 1:
// delete the last character from the string
if (buf.length() == 0) {
buf.replace(0, 0, italic1Str, 0, italic1Str.length());
plainText = italic1Str_getString;
oldPlainText = ALWAYS_DIFFERENT;
}
buf.remove(buf.length()-1, buf.length());
plainText = plainText.substring(0, plainText.length()-1);
break;
case 2:
// replace some of the buffer with boldItalicStr
int rStart = randInt(rand, buf.length()+1);
int rStop = randInt(rand, rStart, buf.length()+1);
buf.replace(rStart, rStop, boldItalicStr);
{
String newString = (rStart>0)? plainText.substring(0, rStart) : new String();
newString = newString.concat(boldItalicStr_getString);
if (rStop < plainText.length())
newString = newString.concat(plainText.substring(rStop, plainText.length()));
oldPlainText = ALWAYS_DIFFERENT;
plainText = newString;
}
break;
case 3:
// repeatedly insert strings into the center of the buffer
{
int insPos = buf.length() / 2;
String prefix = plainText.substring(0, insPos);
String suffix = plainText.substring(insPos, plainText.length());
String middle = new String();
for (int ii=0; ii<4; ii++) {
MConstText which = (ii%2==0)? boldItalicStr : bold3Str;
String whichString = (ii%2==0)? boldItalicStr_getString : bold3Str_getString;
int tempPos = insPos+middle.length();
buf.insert(tempPos, which);
middle = middle.concat(whichString);
}
plainText = prefix.concat(middle).concat(suffix);
oldPlainText = ALWAYS_DIFFERENT;
}
break;
case 4:
// insert bold1Str at end
buf.append(bold1Str);
plainText = plainText.concat(bold1Str_getString);
break;
case 5:
// delete a character from the string
if (buf.length() > 0) {
int delPos = randInt(rand, buf.length()-1);
buf.remove(delPos, delPos+1);
plainText = plainText.substring(0, delPos).concat(plainText.substring(delPos+1));
}
else {
buf.replace(0, 0, plain1Str, 0, plain1Str.length());
plainText = plain1Str_getString;
}
break;
case 6:
// replace the contents of the buffer (except the first character) with itself
{
int start = buf.length() > 1? 1 : 0;
buf.replace(start, buf.length(), buf);
plainText = plainText.substring(0, start).concat(plainText);
if (buf.length() > 0) {
oldPlainText = ALWAYS_DIFFERENT;
}
}
break;
case 7:
// append the contents of the buffer to itself
{
MConstText content = buf;
buf.insert(buf.length(), content);
plainText = plainText.concat(plainText);
}
break;
case 8:
// replace the buffer with boldItalicStr+bold3Str
{
MText replacement = new StyledText();
replacement.append(boldItalicStr);
replacement.append(bold3Str);
buf.replace(0, buf.length(), replacement, 0, replacement.length());
plainText = boldItalicStr_getString.concat(bold3Str_getString);
oldPlainText = ALWAYS_DIFFERENT;
}
break;