2 * (C) Copyright IBM Corp. 1998-2004. All Rights Reserved.
\r
4 * The program is provided "as is" without any warranty express or
\r
5 * implied, including the warranty of non-infringement and the implied
\r
6 * warranties of merchantibility and fitness for a particular purpose.
\r
7 * IBM will not be liable for any damages suffered by you as a result
\r
8 * of using the Program. In no event will IBM be liable for any
\r
9 * special, indirect or consequential damages or lost profits even if
\r
10 * IBM has been advised of the possibility of their occurrence. IBM
\r
11 * will not be liable for any third party claims against you.
\r
13 package com.ibm.richtext.test.unit;
\r
15 import com.ibm.icu.dev.test.TestFmwk;
\r
17 import com.ibm.richtext.textlayout.attributes.AttributeMap;
\r
18 import com.ibm.richtext.textlayout.attributes.TextAttribute;
\r
19 import com.ibm.richtext.styledtext.MText;
\r
20 import com.ibm.richtext.styledtext.MConstText;
\r
21 import com.ibm.richtext.styledtext.StandardTabRuler;
\r
22 import com.ibm.richtext.styledtext.StyledText;
\r
24 import com.ibm.richtext.textformat.TextOffset;
\r
25 import com.ibm.richtext.textformat.MFormatter;
\r
27 import java.awt.Color;
\r
28 import java.awt.Graphics;
\r
29 import java.awt.Point;
\r
30 import java.awt.Rectangle;
\r
31 import java.awt.image.BufferedImage;
\r
33 import java.util.Hashtable;
\r
35 public final class TestFormatter extends TestFmwk {
\r
37 static final String COPYRIGHT =
\r
38 "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";
\r
39 public static void main(String[] args) throws Exception {
\r
41 new TestFormatter().run(args);
\r
44 private static final Point ORIGIN = new Point(0, 0);
\r
46 private static final AttributeMap DEFAULTS;
\r
48 final Float floatZero = new Float(0.0f);
\r
50 Hashtable defaults = new Hashtable();
\r
51 defaults.put(TextAttribute.FAMILY, "Serif");
\r
52 defaults.put(TextAttribute.WEIGHT, new Float(1.0f));
\r
53 defaults.put(TextAttribute.POSTURE, floatZero);
\r
54 defaults.put(TextAttribute.SIZE, new Float(18.0f));
\r
55 defaults.put(TextAttribute.SUPERSCRIPT, new Integer(0));
\r
56 defaults.put(TextAttribute.FOREGROUND, Color.black);
\r
57 defaults.put(TextAttribute.UNDERLINE, new Integer(-1));
\r
58 defaults.put(TextAttribute.STRIKETHROUGH, Boolean.FALSE);
\r
60 defaults.put(TextAttribute.EXTRA_LINE_SPACING, floatZero);
\r
61 defaults.put(TextAttribute.FIRST_LINE_INDENT, floatZero);
\r
62 defaults.put(TextAttribute.MIN_LINE_SPACING, floatZero);
\r
63 defaults.put(TextAttribute.LINE_FLUSH, TextAttribute.FLUSH_LEADING);
\r
64 defaults.put(TextAttribute.LEADING_MARGIN, floatZero);
\r
65 defaults.put(TextAttribute.TRAILING_MARGIN, floatZero);
\r
66 defaults.put(TextAttribute.TAB_RULER, new StandardTabRuler());
\r
68 DEFAULTS = new AttributeMap(defaults);
\r
71 // arg to testLineExceptions
\r
72 private static final int UNKNOWN = -1;
\r
74 private Graphics fGraphics;
\r
76 public TestFormatter() {
\r
78 fGraphics = new BufferedImage(100, 100, BufferedImage.TYPE_3BYTE_BGR).getGraphics();
\r
81 //Frame f = new Frame();
\r
83 //fGraphics = f.getGraphics();
\r
86 private String fiveLines = "a\nb\nc\nd\ne";
\r
87 private String twelveLines = fiveLines + "\n" + fiveLines + "\nf\n";
\r
88 AttributeMap PLAIN = AttributeMap.EMPTY_ATTRIBUTE_MAP;
\r
90 public void test() {
\r
92 MConstText text = new StyledText(fiveLines, PLAIN);
\r
93 _testLineExceptions(makeFormatter(text, 100, true), 5);
\r
94 _testLineAccess(makeFormatter(text, 100, true), 5);
\r
96 text = new StyledText(twelveLines, PLAIN);
\r
97 _testLineExceptions(makeFormatter(text, 3, false), 12);
\r
98 _testLineAccess(makeFormatter(text, 100, true), 12);
\r
100 _testWithModification();
\r
103 private void _testWithModification() {
\r
105 MText text = new StyledText(fiveLines, PLAIN);
\r
106 MFormatter formatter = makeFormatter(text, 100, true);
\r
107 Rectangle viewRect = new Rectangle(0, 0, 100, Integer.MAX_VALUE);
\r
109 formatter.stopBackgroundFormatting();
\r
110 text.append(new StyledText("\n", PLAIN));
\r
111 formatter.updateFormat(text.length()-1, 1, viewRect, ORIGIN);
\r
113 _testLineAccess(formatter, 6);
\r
115 formatter.stopBackgroundFormatting();
\r
116 text.append(new StyledText("ad", PLAIN));
\r
117 formatter.updateFormat(text.length()-2, 2, viewRect, ORIGIN);
\r
118 _testLineAccess(formatter, 6);
\r
119 _testLineExceptions(formatter, 6);
\r
121 formatter.stopBackgroundFormatting();
\r
123 formatter.updateFormat(0, 0, viewRect, ORIGIN);
\r
124 _testLineAccess(formatter, 6);
\r
125 _testLineExceptions(formatter, 6);
\r
129 private MFormatter makeFormatter(MConstText text,
\r
133 return MFormatter.createFormatter(text,
\r
140 private void _testLineExceptions(MFormatter formatter,
\r
143 if (numLines == UNKNOWN) {
\r
144 numLines = formatter.getLineCount();
\r
147 boolean caught = false;
\r
150 formatter.lineRangeLow(numLines);
\r
152 catch(IllegalArgumentException e) {
\r
157 errln("Didn't get exception");
\r
162 formatter.lineRangeLimit(numLines);
\r
164 catch(IllegalArgumentException e) {
\r
169 errln("Didn't get exception");
\r
174 formatter.lineGraphicStart(numLines+1);
\r
176 catch(IllegalArgumentException e) {
\r
181 errln("Didn't get exception");
\r
186 private void _testLineAccess(MFormatter formatter,
\r
189 if (numLines == UNKNOWN) {
\r
190 numLines = formatter.getLineCount();
\r
193 if (formatter.lineGraphicStart(0) != 0) {
\r
194 errln("Line 0 doesn't start at height 0");
\r
196 if (formatter.lineRangeLow(0) != 0) {
\r
197 errln("Line 0 doesn't start at character 0");
\r
200 int lastLimit = formatter.lineRangeLimit(0);
\r
201 final int lineBound = formatter.lineBound();
\r
202 int[] hitX = new int[] { -1, 1, lineBound + 2 };
\r
204 TextOffset offset = new TextOffset();
\r
206 for (int i=1; i < numLines; i++) {
\r
208 int height = formatter.lineGraphicStart(i);
\r
209 if (lastLimit != formatter.lineRangeLow(i)) {
\r
210 errln("lastLine limit is not current line start");
\r
212 int limit = formatter.lineRangeLimit(i);
\r
214 if (limit < lastLimit || (limit == lastLimit && i != numLines-1)) {
\r
215 errln("line has negative or 0 length");
\r
218 int nextHeight = formatter.lineGraphicStart(i+1);
\r
219 if (nextHeight <= height) {
\r
220 errln("0-height line");
\r
222 int incAmount = Math.max((nextHeight-height)/4, 1);
\r
223 for (int hitY = height; hitY < nextHeight; hitY += incAmount) {
\r
225 if (formatter.lineAtHeight(hitY) != i) {
\r
226 errln("lineAtHeight is wrong");
\r
229 for (int j=0; j < hitX.length; j++) {
\r
230 offset = formatter.pointToTextOffset(offset,
\r
231 hitX[j], hitY, ORIGIN, null, false);
\r
232 if (offset.fOffset < lastLimit || offset.fOffset > limit) {
\r
233 errln("Inconsistent offset from pointToTextOffset");
\r
235 //if (formatter.lineContaining(offset) != i) {
\r
236 // int debug = formatter.lineContaining(offset);
\r
237 // errln("lineContaining is incorrect");
\r