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
14 package com.ibm.richtext.textformat;
\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
20 import com.ibm.richtext.textlayout.attributes.TextAttribute;
\r
21 import com.ibm.richtext.textlayout.attributes.AttributeMap;
\r
23 import com.ibm.richtext.textpanel.TextPanel;
\r
26 * Test for MTextIterator.
\r
28 class TestMTextIterator {
\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
34 AttributeMap attrs = TextPanel.getDefaultSettings().getDefaultValues();
\r
35 FONT_MAPPER = new FontResolver(attrs);
\r
38 public static void compareIterToText(MTextIterator iter,
\r
41 //System.out.println("Text: " + text);
\r
42 final int beginIndex = iter.getBeginIndex();
\r
43 final int endIndex = iter.getEndIndex();
\r
45 char ch = iter.setIndex(beginIndex);
\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
55 if (ch != MTextIterator.DONE) {
\r
56 throw new Error("Iterator is not done.");
\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
67 iter.setIndex(beginIndex);
\r
70 for (int runStart = beginIndex; runStart < endIndex; runStart = runLimit) {
\r
72 runLimit = Math.min(endIndex, text.characterStyleLimit(runStart));
\r
74 if (iter.getRunStart() != runStart) {
\r
75 System.out.println(iter.getRunStart() + "; " + runStart);
\r
76 throw new Error("getRunStart is wrong.");
\r
78 if (iter.getRunLimit() != runLimit) {
\r
79 System.out.println(iter.getRunLimit() + "; " + runLimit);
\r
80 throw new Error("getRunLimit is wrong.");
\r
83 AttributeMap style = text.characterStyleAt(runStart);
\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
95 public void test() {
\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
102 MTextIterator iter = new MTextIterator(text, FONT_MAPPER, 0, text.length());
\r
103 compareIterToText(iter, text);
\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
109 iter = new MTextIterator(text, FONT_MAPPER, 0, text.length());
\r
110 compareIterToText(iter, text);
\r
113 public static void main(String[] args) {
\r
115 new TestMTextIterator().test();
\r
116 System.out.println("PASSED");
\r