001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: /*
018: * @author Oleg V. Khaschansky
019: * @version $Revision$
020: */
021:
022: package java.awt.font;
023:
024: import junit.framework.Test;
025: import junit.framework.TestSuite;
026: import junit.framework.TestCase;
027:
028: import java.awt.image.BufferedImage;
029: import java.awt.*;
030: import java.text.AttributedString;
031: import java.text.AttributedCharacterIterator;
032:
033: public class LineBreakMeasurerTest extends TestCase {
034: LineBreakMeasurer measurer;
035:
036: private final int width = 500;
037: private final int height = 200;
038: private final BufferedImage im = new BufferedImage(width, height,
039: BufferedImage.TYPE_INT_RGB);
040:
041: private final String s = "I TestItalic TestPlain I";
042: private final Font f = new Font("times new roman", Font.ITALIC, 24);
043: private final Font f1 = new Font("serif", Font.PLAIN, 60);
044: private final FontRenderContext frc = ((Graphics2D) im
045: .getGraphics()).getFontRenderContext();
046:
047: public LineBreakMeasurerTest(String name) {
048: super (name);
049: }
050:
051: @Override
052: public void setUp() throws Exception {
053: super .setUp();
054:
055: AttributedString as = new AttributedString(s);
056: as.addAttribute(TextAttribute.FONT, f, 0, 12);
057: as.addAttribute(TextAttribute.FONT, f1, 12, s.length());
058: AttributedCharacterIterator aci = as.getIterator();
059:
060: measurer = new LineBreakMeasurer(aci, frc);
061: }
062:
063: @Override
064: public void tearDown() throws Exception {
065: super .tearDown();
066: }
067:
068: public void testDeleteChar() throws Exception {
069: String s1 = "I TestItalic estPlain I";
070:
071: AttributedString as = new AttributedString(s1);
072: as.addAttribute(TextAttribute.FONT, f, 0, 12);
073: as.addAttribute(TextAttribute.FONT, f1, 12, s1.length());
074: AttributedCharacterIterator aci = as.getIterator();
075:
076: int offset = measurer.nextOffset(30); // This won't change
077:
078: measurer.setPosition(12);
079: int offset1 = measurer.nextOffset(500); // And this should change
080:
081: measurer.deleteChar(aci, 13);
082: assertEquals(0, measurer.getPosition());
083:
084: measurer.setPosition(0);
085: assertEquals(offset, measurer.nextOffset(30));
086:
087: measurer.setPosition(12);
088: assertFalse(offset1 == measurer.nextOffset(500));
089: }
090:
091: public void testGetPosition() throws Exception {
092: assertEquals(0, measurer.getPosition());
093: measurer.nextLayout(1000, 5, false);
094: assertEquals(5, measurer.getPosition());
095: }
096:
097: public void testInsertChar() throws Exception {
098: int offset = measurer.nextOffset(30); // This won't change
099: measurer.setPosition(12);
100: int offset1 = measurer.nextOffset(500); // And this should change
101:
102: String s1 = "I TestItalic WTestPlain I";
103: AttributedString as = new AttributedString(s1);
104: as.addAttribute(TextAttribute.FONT, f, 0, 12);
105: as.addAttribute(TextAttribute.FONT, f1, 12, s1.length());
106: AttributedCharacterIterator aci = as.getIterator();
107:
108: measurer.insertChar(aci, 13);
109: assertEquals(0, measurer.getPosition());
110:
111: measurer.setPosition(0);
112: assertEquals(offset, measurer.nextOffset(30));
113:
114: measurer.setPosition(12);
115: assertFalse(offset1 == measurer.nextOffset(500));
116: }
117:
118: public void testNextLayout() throws Exception {
119: TextLayout l1 = measurer.nextLayout(100);
120: TextLayout l2 = measurer.nextLayout(100);
121: TextLayout l3 = measurer.nextLayout(500);
122: TextLayout l4 = measurer.nextLayout(500);
123:
124: assertEquals(2, l1.getCharacterCount());
125: assertEquals(11, l2.getCharacterCount());
126: assertEquals(11, l3.getCharacterCount());
127: assertNull(l4);
128: }
129:
130: public void testNextLayout1() throws Exception {
131: TextLayout l1 = measurer.nextLayout(100, 5, false);
132: TextLayout l2 = measurer.nextLayout(15, 20, true);
133: TextLayout l3 = measurer.nextLayout(600, 20, true);
134: TextLayout l4 = measurer.nextLayout(500, 21, false);
135: TextLayout l5 = measurer.nextLayout(500, 25, false);
136:
137: assertEquals(2, l1.getCharacterCount());
138: assertNull(l2);
139: assertEquals(18, l3.getCharacterCount());
140: assertEquals(1, l4.getCharacterCount());
141: assertEquals(3, l5.getCharacterCount());
142: }
143:
144: public void testNextOffset() throws Exception {
145: int o1 = measurer.nextOffset(40);
146: int o2 = measurer.nextOffset(60);
147: measurer.setPosition(o1);
148: int o3 = measurer.nextOffset(60);
149: measurer.setPosition(o3);
150: int o4 = measurer.nextOffset(60);
151: measurer.setPosition(o4);
152: int o5 = measurer.nextOffset(500);
153:
154: assertEquals(2, o1);
155: assertEquals(2, o2);
156: assertEquals(8, o3);
157: assertEquals(13, o4);
158: assertEquals(24, o5);
159: }
160:
161: public void testNextOffset1() throws Exception {
162: int o1 = measurer.nextOffset(40, 5, true);
163: int o2 = measurer.nextOffset(60, 5, true);
164: measurer.setPosition(o1);
165: int o3 = measurer.nextOffset(60, 25, true);
166: measurer.setPosition(o3);
167: int o4 = measurer.nextOffset(60, 8, false);
168: int o5 = measurer.nextOffset(60, 9, false);
169: measurer.setPosition(o4);
170: int o6 = measurer.nextOffset(500);
171:
172: assertEquals(2, o1);
173: assertEquals(2, o2);
174: assertEquals(2, o3);
175: assertEquals(8, o4);
176: assertEquals(8, o5);
177: assertEquals(24, o6);
178: }
179:
180: public void testSetPosition() throws Exception {
181: measurer.setPosition(10);
182: assertEquals(measurer.getPosition(), 10);
183: }
184:
185: public static Test suite() {
186: return new TestSuite(LineBreakMeasurerTest.class);
187: }
188: }
|