001: /*
002: * (C) Copyright IBM Corp. 1998-2004. All Rights Reserved.
003: *
004: * The program is provided "as is" without any warranty express or
005: * implied, including the warranty of non-infringement and the implied
006: * warranties of merchantibility and fitness for a particular purpose.
007: * IBM will not be liable for any damages suffered by you as a result
008: * of using the Program. In no event will IBM be liable for any
009: * special, indirect or consequential damages or lost profits even if
010: * IBM has been advised of the possibility of their occurrence. IBM
011: * will not be liable for any third party claims against you.
012: */
013: // Requires Java2
014: package com.ibm.richtext.textformat;
015:
016: import com.ibm.richtext.styledtext.StyledText;
017: import com.ibm.richtext.styledtext.MConstText;
018: import com.ibm.richtext.styledtext.MText;
019:
020: import com.ibm.richtext.textlayout.attributes.TextAttribute;
021: import com.ibm.richtext.textlayout.attributes.AttributeMap;
022:
023: import com.ibm.richtext.textpanel.TextPanel;
024:
025: /**
026: * Test for MTextIterator.
027: */
028: class TestMTextIterator {
029:
030: static final String COPYRIGHT = "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";
031: private static final FontResolver FONT_MAPPER;
032: static {
033: AttributeMap attrs = TextPanel.getDefaultSettings()
034: .getDefaultValues();
035: FONT_MAPPER = new FontResolver(attrs);
036: }
037:
038: public static void compareIterToText(MTextIterator iter,
039: MConstText text) {
040:
041: //System.out.println("Text: " + text);
042: final int beginIndex = iter.getBeginIndex();
043: final int endIndex = iter.getEndIndex();
044:
045: char ch = iter.setIndex(beginIndex);
046:
047: for (int i = beginIndex; i < endIndex; i++) {
048: //System.out.print(ch+ " ");
049: if (ch != text.at(i)) {
050: throw new Error("Characters are not the same.");
051: }
052: ch = iter.next();
053: }
054:
055: if (ch != MTextIterator.DONE) {
056: throw new Error("Iterator is not done.");
057: }
058:
059: for (int i = endIndex - 1; i >= beginIndex; i--) {
060: ch = iter.previous();
061: //System.out.print(ch+ " ");
062: if (ch != text.at(i)) {
063: throw new Error("Backward iteration failed.");
064: }
065: }
066:
067: iter.setIndex(beginIndex);
068:
069: int runLimit;
070: for (int runStart = beginIndex; runStart < endIndex; runStart = runLimit) {
071:
072: runLimit = Math.min(endIndex, text
073: .characterStyleLimit(runStart));
074:
075: if (iter.getRunStart() != runStart) {
076: System.out
077: .println(iter.getRunStart() + "; " + runStart);
078: throw new Error("getRunStart is wrong.");
079: }
080: if (iter.getRunLimit() != runLimit) {
081: System.out
082: .println(iter.getRunLimit() + "; " + runLimit);
083: throw new Error("getRunLimit is wrong.");
084: }
085:
086: AttributeMap style = text.characterStyleAt(runStart);
087:
088: while (iter.getIndex() < runLimit) {
089: AttributeMap resolved = FONT_MAPPER.applyFont(style);
090: if (!iter.getAttributes().equals(resolved)) {
091: throw new Error("Style is wrong.");
092: }
093: iter.next();
094: }
095: }
096: }
097:
098: public void test() {
099:
100: AttributeMap bold = new AttributeMap(TextAttribute.WEIGHT,
101: TextAttribute.WEIGHT_BOLD);
102: MText text = new StyledText("Hello there!",
103: AttributeMap.EMPTY_ATTRIBUTE_MAP);
104: text.replace(2, 2, 'V', bold);
105:
106: MTextIterator iter = new MTextIterator(text, FONT_MAPPER, 0,
107: text.length());
108: compareIterToText(iter, text);
109:
110: text.replace(6, 8, new StyledText("ALL_BOLD", bold), 0, 8);
111: iter = new MTextIterator(text, FONT_MAPPER, 1,
112: text.length() - 3);
113: compareIterToText(iter, text);
114:
115: iter = new MTextIterator(text, FONT_MAPPER, 0, text.length());
116: compareIterToText(iter, text);
117: }
118:
119: public static void main(String[] args) {
120:
121: new TestMTextIterator().test();
122: System.out.println("PASSED");
123: }
124: }
|