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: package com.ibm.richtext.test.unit;
014:
015: import com.ibm.icu.dev.test.TestFmwk;
016:
017: import com.ibm.richtext.textlayout.attributes.AttributeMap;
018: import com.ibm.richtext.textlayout.attributes.TextAttribute;
019: import com.ibm.richtext.styledtext.MText;
020: import com.ibm.richtext.styledtext.MConstText;
021: import com.ibm.richtext.styledtext.StandardTabRuler;
022: import com.ibm.richtext.styledtext.StyledText;
023:
024: import com.ibm.richtext.textformat.TextOffset;
025: import com.ibm.richtext.textformat.MFormatter;
026:
027: import java.awt.Color;
028: import java.awt.Graphics;
029: import java.awt.Point;
030: import java.awt.Rectangle;
031: import java.awt.image.BufferedImage;
032:
033: import java.util.Hashtable;
034:
035: public final class TestFormatter extends TestFmwk {
036:
037: static final String COPYRIGHT = "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";
038:
039: public static void main(String[] args) throws Exception {
040:
041: new TestFormatter().run(args);
042: }
043:
044: private static final Point ORIGIN = new Point(0, 0);
045:
046: private static final AttributeMap DEFAULTS;
047: static {
048: final Float floatZero = new Float(0.0f);
049:
050: Hashtable defaults = new Hashtable();
051: defaults.put(TextAttribute.FAMILY, "Serif");
052: defaults.put(TextAttribute.WEIGHT, new Float(1.0f));
053: defaults.put(TextAttribute.POSTURE, floatZero);
054: defaults.put(TextAttribute.SIZE, new Float(18.0f));
055: defaults.put(TextAttribute.SUPERSCRIPT, new Integer(0));
056: defaults.put(TextAttribute.FOREGROUND, Color.black);
057: defaults.put(TextAttribute.UNDERLINE, new Integer(-1));
058: defaults.put(TextAttribute.STRIKETHROUGH, Boolean.FALSE);
059:
060: defaults.put(TextAttribute.EXTRA_LINE_SPACING, floatZero);
061: defaults.put(TextAttribute.FIRST_LINE_INDENT, floatZero);
062: defaults.put(TextAttribute.MIN_LINE_SPACING, floatZero);
063: defaults.put(TextAttribute.LINE_FLUSH,
064: TextAttribute.FLUSH_LEADING);
065: defaults.put(TextAttribute.LEADING_MARGIN, floatZero);
066: defaults.put(TextAttribute.TRAILING_MARGIN, floatZero);
067: defaults.put(TextAttribute.TAB_RULER, new StandardTabRuler());
068:
069: DEFAULTS = new AttributeMap(defaults);
070: }
071:
072: // arg to testLineExceptions
073: private static final int UNKNOWN = -1;
074:
075: private Graphics fGraphics;
076:
077: public TestFormatter() {
078:
079: fGraphics = new BufferedImage(100, 100,
080: BufferedImage.TYPE_3BYTE_BGR).getGraphics();
081:
082: //JDK 1.1:
083: //Frame f = new Frame();
084: //f.show();
085: //fGraphics = f.getGraphics();
086: }
087:
088: private String fiveLines = "a\nb\nc\nd\ne";
089: private String twelveLines = fiveLines + "\n" + fiveLines + "\nf\n";
090: AttributeMap PLAIN = AttributeMap.EMPTY_ATTRIBUTE_MAP;
091:
092: public void test() {
093:
094: MConstText text = new StyledText(fiveLines, PLAIN);
095: _testLineExceptions(makeFormatter(text, 100, true), 5);
096: _testLineAccess(makeFormatter(text, 100, true), 5);
097:
098: text = new StyledText(twelveLines, PLAIN);
099: _testLineExceptions(makeFormatter(text, 3, false), 12);
100: _testLineAccess(makeFormatter(text, 100, true), 12);
101:
102: _testWithModification();
103: }
104:
105: private void _testWithModification() {
106:
107: MText text = new StyledText(fiveLines, PLAIN);
108: MFormatter formatter = makeFormatter(text, 100, true);
109: Rectangle viewRect = new Rectangle(0, 0, 100, Integer.MAX_VALUE);
110:
111: formatter.stopBackgroundFormatting();
112: text.append(new StyledText("\n", PLAIN));
113: formatter.updateFormat(text.length() - 1, 1, viewRect, ORIGIN);
114:
115: _testLineAccess(formatter, 6);
116:
117: formatter.stopBackgroundFormatting();
118: text.append(new StyledText("ad", PLAIN));
119: formatter.updateFormat(text.length() - 2, 2, viewRect, ORIGIN);
120: _testLineAccess(formatter, 6);
121: _testLineExceptions(formatter, 6);
122:
123: formatter.stopBackgroundFormatting();
124: text.remove(0, 1);
125: formatter.updateFormat(0, 0, viewRect, ORIGIN);
126: _testLineAccess(formatter, 6);
127: _testLineExceptions(formatter, 6);
128: }
129:
130: private MFormatter makeFormatter(MConstText text, int lineBound,
131: boolean wrap) {
132:
133: return MFormatter.createFormatter(text, DEFAULTS, lineBound,
134: wrap, fGraphics);
135: }
136:
137: private void _testLineExceptions(MFormatter formatter, int numLines) {
138:
139: if (numLines == UNKNOWN) {
140: numLines = formatter.getLineCount();
141: }
142:
143: boolean caught = false;
144:
145: try {
146: formatter.lineRangeLow(numLines);
147: } catch (IllegalArgumentException e) {
148: caught = true;
149: }
150:
151: if (!caught) {
152: errln("Didn't get exception");
153: }
154: caught = false;
155:
156: try {
157: formatter.lineRangeLimit(numLines);
158: } catch (IllegalArgumentException e) {
159: caught = true;
160: }
161:
162: if (!caught) {
163: errln("Didn't get exception");
164: }
165: caught = false;
166:
167: try {
168: formatter.lineGraphicStart(numLines + 1);
169: } catch (IllegalArgumentException e) {
170: caught = true;
171: }
172:
173: if (!caught) {
174: errln("Didn't get exception");
175: }
176: caught = false;
177: }
178:
179: private void _testLineAccess(MFormatter formatter, int numLines) {
180:
181: if (numLines == UNKNOWN) {
182: numLines = formatter.getLineCount();
183: }
184:
185: if (formatter.lineGraphicStart(0) != 0) {
186: errln("Line 0 doesn't start at height 0");
187: }
188: if (formatter.lineRangeLow(0) != 0) {
189: errln("Line 0 doesn't start at character 0");
190: }
191:
192: int lastLimit = formatter.lineRangeLimit(0);
193: final int lineBound = formatter.lineBound();
194: int[] hitX = new int[] { -1, 1, lineBound + 2 };
195:
196: TextOffset offset = new TextOffset();
197:
198: for (int i = 1; i < numLines; i++) {
199:
200: int height = formatter.lineGraphicStart(i);
201: if (lastLimit != formatter.lineRangeLow(i)) {
202: errln("lastLine limit is not current line start");
203: }
204: int limit = formatter.lineRangeLimit(i);
205:
206: if (limit < lastLimit
207: || (limit == lastLimit && i != numLines - 1)) {
208: errln("line has negative or 0 length");
209: }
210:
211: int nextHeight = formatter.lineGraphicStart(i + 1);
212: if (nextHeight <= height) {
213: errln("0-height line");
214: }
215: int incAmount = Math.max((nextHeight - height) / 4, 1);
216: for (int hitY = height; hitY < nextHeight; hitY += incAmount) {
217:
218: if (formatter.lineAtHeight(hitY) != i) {
219: errln("lineAtHeight is wrong");
220: }
221:
222: for (int j = 0; j < hitX.length; j++) {
223: offset = formatter.pointToTextOffset(offset,
224: hitX[j], hitY, ORIGIN, null, false);
225: if (offset.fOffset < lastLimit
226: || offset.fOffset > limit) {
227: errln("Inconsistent offset from pointToTextOffset");
228: }
229: //if (formatter.lineContaining(offset) != i) {
230: // int debug = formatter.lineContaining(offset);
231: // errln("lineContaining is incorrect");
232: //}
233: }
234: }
235:
236: lastLimit = limit;
237: }
238: }
239: }
|