]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/richtext/test/unit/TestFormatter.java
icu4jsrc
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / richtext / test / unit / TestFormatter.java
1 /*\r
2  * (C) Copyright IBM Corp. 1998-2004.  All Rights Reserved.\r
3  *\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
12  */\r
13 package com.ibm.richtext.test.unit;\r
14 \r
15 import com.ibm.icu.dev.test.TestFmwk;\r
16 \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
23 \r
24 import com.ibm.richtext.textformat.TextOffset;\r
25 import com.ibm.richtext.textformat.MFormatter;\r
26 \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
32 \r
33 import java.util.Hashtable;\r
34 \r
35 public final class TestFormatter extends TestFmwk {\r
36 \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
40 \r
41         new TestFormatter().run(args);\r
42     }\r
43 \r
44     private static final Point ORIGIN = new Point(0, 0);\r
45 \r
46     private static final AttributeMap DEFAULTS;\r
47     static {\r
48         final Float floatZero = new Float(0.0f);\r
49 \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
59 \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
67 \r
68         DEFAULTS = new AttributeMap(defaults);\r
69     }\r
70     \r
71     // arg to testLineExceptions\r
72     private static final int UNKNOWN = -1;\r
73 \r
74     private Graphics fGraphics;\r
75 \r
76     public TestFormatter() {\r
77 \r
78         fGraphics = new BufferedImage(100, 100, BufferedImage.TYPE_3BYTE_BGR).getGraphics();\r
79         \r
80         //JDK 1.1:\r
81         //Frame f = new Frame();\r
82         //f.show();\r
83         //fGraphics = f.getGraphics();\r
84     }\r
85 \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
89 \r
90     public void test() {\r
91 \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
95 \r
96         text = new StyledText(twelveLines, PLAIN);\r
97         _testLineExceptions(makeFormatter(text, 3, false), 12);\r
98         _testLineAccess(makeFormatter(text, 100, true), 12);\r
99 \r
100         _testWithModification();\r
101     }\r
102 \r
103     private void _testWithModification() {\r
104 \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
108 \r
109         formatter.stopBackgroundFormatting();\r
110         text.append(new StyledText("\n", PLAIN));\r
111         formatter.updateFormat(text.length()-1, 1, viewRect, ORIGIN);\r
112 \r
113         _testLineAccess(formatter, 6);\r
114 \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
120 \r
121         formatter.stopBackgroundFormatting();\r
122         text.remove(0, 1);\r
123         formatter.updateFormat(0, 0, viewRect, ORIGIN);\r
124         _testLineAccess(formatter, 6);\r
125         _testLineExceptions(formatter, 6);\r
126     }\r
127 \r
128 \r
129     private MFormatter makeFormatter(MConstText text,\r
130                                      int lineBound,\r
131                                      boolean wrap) {\r
132 \r
133         return MFormatter.createFormatter(text, \r
134                                           DEFAULTS,\r
135                                           lineBound,\r
136                                           wrap,\r
137                                           fGraphics);\r
138     }\r
139 \r
140     private void _testLineExceptions(MFormatter formatter,\r
141                                     int numLines) {\r
142 \r
143         if (numLines == UNKNOWN) {\r
144             numLines = formatter.getLineCount();\r
145         }\r
146 \r
147         boolean caught = false;\r
148 \r
149         try {\r
150             formatter.lineRangeLow(numLines);\r
151         }\r
152         catch(IllegalArgumentException e) {\r
153             caught = true;\r
154         }\r
155 \r
156         if (!caught) {\r
157             errln("Didn't get exception");\r
158         }\r
159         caught = false;\r
160 \r
161         try {\r
162             formatter.lineRangeLimit(numLines);\r
163         }\r
164         catch(IllegalArgumentException e) {\r
165             caught = true;\r
166         }\r
167 \r
168         if (!caught) {\r
169             errln("Didn't get exception");\r
170         }\r
171         caught = false;\r
172 \r
173         try {\r
174             formatter.lineGraphicStart(numLines+1);\r
175         }\r
176         catch(IllegalArgumentException e) {\r
177             caught = true;\r
178         }\r
179 \r
180         if (!caught) {\r
181             errln("Didn't get exception");\r
182         }\r
183         caught = false;\r
184     }\r
185 \r
186     private void _testLineAccess(MFormatter formatter,\r
187                                  int numLines) {\r
188 \r
189         if (numLines == UNKNOWN) {\r
190             numLines = formatter.getLineCount();\r
191         }\r
192 \r
193         if (formatter.lineGraphicStart(0) != 0) {\r
194             errln("Line 0 doesn't start at height 0");\r
195         }\r
196         if (formatter.lineRangeLow(0) != 0) {\r
197             errln("Line 0 doesn't start at character 0");\r
198         }\r
199 \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
203 \r
204         TextOffset offset = new TextOffset();\r
205 \r
206         for (int i=1; i < numLines; i++) {\r
207 \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
211             }\r
212             int limit = formatter.lineRangeLimit(i);\r
213 \r
214             if (limit < lastLimit || (limit == lastLimit && i != numLines-1)) {\r
215                 errln("line has negative or 0 length");\r
216             }\r
217 \r
218             int nextHeight = formatter.lineGraphicStart(i+1);\r
219             if (nextHeight <= height) {\r
220                 errln("0-height line");\r
221             }\r
222             int incAmount = Math.max((nextHeight-height)/4, 1);\r
223             for (int hitY = height; hitY < nextHeight; hitY += incAmount) {\r
224 \r
225                 if (formatter.lineAtHeight(hitY) != i) {\r
226                     errln("lineAtHeight is wrong");\r
227                 }\r
228 \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
234                     }\r
235                     //if (formatter.lineContaining(offset) != i) {\r
236                     //    int debug = formatter.lineContaining(offset);\r
237                     //    errln("lineContaining is incorrect");\r
238                     //}\r
239                 }\r
240             }\r
241 \r
242             lastLimit = limit;\r
243         }\r
244     }\r
245 }