]> gitweb.fperrin.net Git - Dictionary.git/blob - jars/icu4j-4_2_1-src/src/com/ibm/richtext/textformat/TestMTextIterator.java
icu4jsrc
[Dictionary.git] / jars / icu4j-4_2_1-src / src / com / ibm / richtext / textformat / TestMTextIterator.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 // Requires Java2\r
14 package com.ibm.richtext.textformat;\r
15 \r
16 import com.ibm.richtext.styledtext.StyledText;\r
17 import com.ibm.richtext.styledtext.MConstText;\r
18 import com.ibm.richtext.styledtext.MText;\r
19 \r
20 import com.ibm.richtext.textlayout.attributes.TextAttribute;\r
21 import com.ibm.richtext.textlayout.attributes.AttributeMap;\r
22 \r
23 import com.ibm.richtext.textpanel.TextPanel;\r
24 \r
25 /**\r
26  * Test for MTextIterator.\r
27  */\r
28 class TestMTextIterator {\r
29 \r
30     static final String COPYRIGHT =\r
31                 "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";\r
32     private static final FontResolver FONT_MAPPER;\r
33     static {\r
34         AttributeMap attrs = TextPanel.getDefaultSettings().getDefaultValues();\r
35         FONT_MAPPER = new FontResolver(attrs);\r
36     }\r
37 \r
38     public static void compareIterToText(MTextIterator iter,\r
39                                          MConstText text) {\r
40 \r
41         //System.out.println("Text: " + text);\r
42         final int beginIndex = iter.getBeginIndex();\r
43         final int endIndex = iter.getEndIndex();\r
44 \r
45         char ch = iter.setIndex(beginIndex);\r
46 \r
47         for (int i=beginIndex; i < endIndex; i++) {\r
48             //System.out.print(ch+ " ");\r
49             if (ch != text.at(i)) {\r
50                 throw new Error("Characters are not the same.");\r
51             }\r
52             ch = iter.next();\r
53         }\r
54 \r
55         if (ch != MTextIterator.DONE) {\r
56             throw new Error("Iterator is not done.");\r
57         }\r
58 \r
59         for (int i=endIndex-1; i >= beginIndex; i--) {\r
60             ch = iter.previous();\r
61             //System.out.print(ch+ " ");\r
62             if (ch != text.at(i)) {\r
63                 throw new Error("Backward iteration failed.");\r
64             }\r
65         }\r
66 \r
67         iter.setIndex(beginIndex);\r
68 \r
69         int runLimit;\r
70         for (int runStart = beginIndex; runStart < endIndex; runStart = runLimit) {\r
71 \r
72             runLimit = Math.min(endIndex, text.characterStyleLimit(runStart));\r
73 \r
74             if (iter.getRunStart() != runStart) {\r
75                 System.out.println(iter.getRunStart() + "; " + runStart);\r
76                 throw new Error("getRunStart is wrong.");\r
77             }\r
78             if (iter.getRunLimit() != runLimit) {\r
79                 System.out.println(iter.getRunLimit() + "; " + runLimit);\r
80                 throw new Error("getRunLimit is wrong.");\r
81             }\r
82 \r
83             AttributeMap style = text.characterStyleAt(runStart);\r
84 \r
85             while (iter.getIndex() < runLimit) {\r
86                 AttributeMap resolved = FONT_MAPPER.applyFont(style);\r
87                 if (!iter.getAttributes().equals(resolved)) {\r
88                     throw new Error("Style is wrong.");\r
89                 }\r
90                 iter.next();\r
91             }\r
92         }\r
93     }\r
94 \r
95     public void test() {\r
96 \r
97         AttributeMap bold = new AttributeMap(TextAttribute.WEIGHT,\r
98                                              TextAttribute.WEIGHT_BOLD);\r
99         MText text = new StyledText("Hello there!", AttributeMap.EMPTY_ATTRIBUTE_MAP);\r
100         text.replace(2, 2, 'V', bold);\r
101 \r
102         MTextIterator iter = new MTextIterator(text, FONT_MAPPER, 0, text.length());\r
103         compareIterToText(iter, text);\r
104 \r
105         text.replace(6, 8, new StyledText("ALL_BOLD", bold), 0, 8);\r
106         iter = new MTextIterator(text, FONT_MAPPER, 1, text.length()-3);\r
107         compareIterToText(iter, text);\r
108 \r
109         iter = new MTextIterator(text, FONT_MAPPER, 0, text.length());\r
110         compareIterToText(iter, text);\r
111     }\r
112 \r
113     public static void main(String[] args) {\r
114 \r
115         new TestMTextIterator().test();\r
116         System.out.println("PASSED");\r
117     }\r
118 }\r