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 Alexey A. Ivanov
019: * @version $Revision$
020: */package javax.swing.text;
021:
022: import java.awt.Font;
023: import java.awt.FontMetrics;
024: import java.awt.Rectangle;
025: import java.awt.Toolkit;
026: import javax.swing.BasicSwingTestCase;
027: import javax.swing.SwingConstants;
028: import javax.swing.text.GlyphView.GlyphPainter;
029: import javax.swing.text.Position.Bias;
030: import junit.framework.TestCase;
031:
032: /**
033: * Tests default implementation of GlyphPainter abstract class.
034: *
035: */
036: public class DefaultGlyphPainterTest extends TestCase {
037: private GlyphPainter painter;
038:
039: private GlyphView view;
040:
041: private StyledDocument doc;
042:
043: private Element root;
044:
045: private Element leaf;
046:
047: private Rectangle alloc;
048:
049: private Font font;
050:
051: private FontMetrics metrics;
052:
053: private static final String FULL_TEXT = "this text to check how view breaks";
054:
055: // 0123456789012345678901234567890123
056: private static final int startOffset = 5;
057:
058: private static final int endOffset = 28;
059:
060: private static final int length = endOffset - startOffset;
061:
062: private static final String LEAF_TEXT = FULL_TEXT.substring(
063: startOffset, endOffset);
064:
065: @SuppressWarnings("deprecation")
066: @Override
067: protected void setUp() throws Exception {
068: super .setUp();
069: doc = new DefaultStyledDocument();
070: root = doc.getDefaultRootElement();
071: doc.insertString(0, FULL_TEXT, null);
072: doc.setCharacterAttributes(startOffset, length,
073: SimpleAttributeSet.EMPTY, false);
074: leaf = root.getElement(0).getElement(1);
075: view = new GlyphView(leaf);
076: view.checkPainter();
077: painter = view.getGlyphPainter();
078: font = view.getFont();
079: metrics = Toolkit.getDefaultToolkit().getFontMetrics(font);
080: alloc = new Rectangle(12, 21, 129, 17);
081: alloc.width = getX(length - 1) - 1 - alloc.x;
082: }
083:
084: @Override
085: protected void tearDown() throws Exception {
086: super .tearDown();
087: }
088:
089: public void testGetSpan() {
090: assertEquals(metrics.stringWidth(LEAF_TEXT), (int) painter
091: .getSpan(view, startOffset, endOffset, null, 0));
092: final int end = LEAF_TEXT.indexOf(' ') + 1;
093: assertEquals(metrics.stringWidth(LEAF_TEXT.substring(0, end)),
094: (int) painter.getSpan(view, startOffset, startOffset
095: + end, null, 0));
096: }
097:
098: public void testGetHeight() {
099: assertEquals(metrics.getHeight(), (int) painter.getHeight(view));
100: }
101:
102: public void testGetAscent() {
103: assertEquals(metrics.getAscent(), (int) painter.getAscent(view));
104: }
105:
106: public void testGetDescent() {
107: assertEquals(metrics.getDescent(), (int) painter
108: .getDescent(view));
109: }
110:
111: public void testModelToView() throws BadLocationException {
112: assertEquals(new Rectangle(alloc.x, alloc.y, 0, metrics
113: .getHeight()), painter.modelToView(view, startOffset,
114: Bias.Forward, alloc));
115: assertEquals(new Rectangle(alloc.x, alloc.y, 0, metrics
116: .getHeight()), painter.modelToView(view, startOffset,
117: Bias.Backward, alloc));
118: assertEquals(new Rectangle(getX(1), alloc.y, 0, metrics
119: .getHeight()), painter.modelToView(view,
120: startOffset + 1, Bias.Forward, alloc));
121: assertEquals(new Rectangle(getX(2), alloc.y, 0, metrics
122: .getHeight()), painter.modelToView(view,
123: startOffset + 2, Bias.Forward, alloc));
124: assertEquals(new Rectangle(getX(length - 1), alloc.y, 0,
125: metrics.getHeight()), painter.modelToView(view,
126: endOffset - 1, Bias.Forward, alloc));
127: assertTrue(getX(length - 1) > alloc.x + alloc.width);
128: assertEquals(
129: new Rectangle(
130: BasicSwingTestCase.isHarmony() ? getX(length)
131: : alloc.x + alloc.width, alloc.y, 0,
132: metrics.getHeight()), painter.modelToView(view,
133: endOffset, Bias.Forward, alloc));
134: }
135:
136: public void testViewToModel() {
137: Bias[] bias = new Bias[1];
138: assertEquals(startOffset, painter.viewToModel(view,
139: alloc.x - 1, alloc.y, alloc, bias));
140: assertSame(Bias.Forward, bias[0]);
141: assertEquals(startOffset, painter.viewToModel(view, alloc.x,
142: alloc.y - 1, alloc, bias));
143: assertSame(Bias.Forward, bias[0]);
144: assertEquals(startOffset, painter.viewToModel(view, alloc.x,
145: alloc.y, alloc, bias));
146: assertSame(Bias.Forward, bias[0]);
147: assertEquals(startOffset + 1, painter.viewToModel(view,
148: getX(1), alloc.y, alloc, bias));
149: assertSame(Bias.Forward, bias[0]);
150: assertEquals(startOffset + 2, painter.viewToModel(view,
151: getX(2), alloc.y, alloc, bias));
152: assertSame(Bias.Forward, bias[0]);
153: assertEquals(endOffset - 1, painter.viewToModel(view,
154: getX(length - 1), alloc.y, alloc, bias));
155: assertSame(Bias.Forward, bias[0]);
156: assertEquals(endOffset - 1, painter.viewToModel(view,
157: getX(length), alloc.y, alloc, bias));
158: assertSame(Bias.Forward, bias[0]);
159: assertEquals(endOffset - 1, painter.viewToModel(view,
160: getX(length) + 100, alloc.y, alloc, bias));
161: assertSame(Bias.Forward, bias[0]);
162: assertEquals(endOffset - 1, painter.viewToModel(view,
163: getX(length), alloc.y + alloc.height + 10, alloc, bias));
164: assertSame(Bias.Forward, bias[0]);
165: }
166:
167: public void testGetBoundedPosition() {
168: // No char can fit; the same offset is returned
169: assertEquals(startOffset + 1, painter.getBoundedPosition(view,
170: startOffset + 1, alloc.x, 1));
171: // width is the width of the first char
172: float width = metrics.stringWidth(LEAF_TEXT.substring(0, 1));
173: assertEquals(startOffset, painter.getBoundedPosition(view,
174: startOffset, alloc.x, width - 0.01f));
175: assertEquals(startOffset + 1, painter.getBoundedPosition(view,
176: startOffset, alloc.x, width));
177: assertEquals(startOffset + 1, painter.getBoundedPosition(view,
178: startOffset, alloc.x, width + 0.01f));
179: // width includes two chars
180: width = metrics.stringWidth(LEAF_TEXT.substring(0, 2));
181: assertEquals(startOffset + 2, painter.getBoundedPosition(view,
182: startOffset, alloc.x, width));
183: // Two chars (the second and the third) starting not at the beginning
184: // of the view
185: width = metrics.stringWidth(LEAF_TEXT.substring(1, 3));
186: assertEquals(startOffset + 3, painter.getBoundedPosition(view,
187: startOffset + 1, alloc.x, width));
188: // Can't return offset greater than view.getEndOffset()
189: assertEquals(endOffset, painter.getBoundedPosition(view,
190: endOffset, alloc.x, width));
191: }
192:
193: public void testGetPainter() {
194: assertSame(painter, painter.getPainter(view, startOffset,
195: endOffset));
196: assertSame(painter, painter.getPainter(new GlyphView(leaf),
197: startOffset, endOffset));
198: assertSame(painter, painter.getPainter(view, 0, 4));
199: assertSame(painter, painter.getPainter(view, endOffset + 5,
200: endOffset + 10));
201: }
202:
203: public void testGetNextVisualPositionFromWest()
204: throws BadLocationException {
205: final boolean isHarmony = BasicSwingTestCase.isHarmony();
206: Bias[] bias = new Bias[1];
207: assertEquals(-1, painter.getNextVisualPositionFrom(view,
208: startOffset, Bias.Forward, alloc, SwingConstants.WEST,
209: bias));
210: assertNull(bias[0]);
211: assertEquals(isHarmony ? -1 : startOffset - 2, painter
212: .getNextVisualPositionFrom(view, startOffset - 1,
213: Bias.Forward, alloc, SwingConstants.WEST, bias));
214: if (isHarmony) {
215: assertNull(bias[0]);
216: } else {
217: assertSame(Bias.Forward, bias[0]);
218: bias[0] = null;
219: }
220: assertEquals(startOffset, painter.getNextVisualPositionFrom(
221: view, startOffset + 1, Bias.Forward, alloc,
222: SwingConstants.WEST, bias));
223: assertSame(Bias.Forward, bias[0]);
224: bias[0] = null;
225: assertEquals(startOffset + 1, painter
226: .getNextVisualPositionFrom(view, startOffset + 2,
227: Bias.Forward, alloc, SwingConstants.WEST, bias));
228: assertSame(Bias.Forward, bias[0]);
229: bias[0] = null;
230: }
231:
232: public void testGetNextVisualPositionFromEast()
233: throws BadLocationException {
234: final boolean isHarmony = BasicSwingTestCase.isHarmony();
235: Bias[] bias = new Bias[1];
236: assertEquals(-1, painter.getNextVisualPositionFrom(view,
237: endOffset, Bias.Forward, alloc, SwingConstants.EAST,
238: bias));
239: assertNull(bias[0]);
240: assertEquals(isHarmony ? -1 : endOffset + 2, painter
241: .getNextVisualPositionFrom(view, endOffset + 1,
242: Bias.Forward, alloc, SwingConstants.EAST, bias));
243: if (isHarmony) {
244: assertNull(bias[0]);
245: } else {
246: assertSame(Bias.Forward, bias[0]);
247: bias[0] = null;
248: }
249: assertEquals(-1, painter.getNextVisualPositionFrom(view,
250: endOffset - 1, Bias.Forward, alloc,
251: SwingConstants.EAST, bias));
252: assertNull(bias[0]);
253: assertEquals(endOffset - 1, painter.getNextVisualPositionFrom(
254: view, endOffset - 2, Bias.Forward, alloc,
255: SwingConstants.EAST, bias));
256: assertSame(Bias.Forward, bias[0]);
257: bias[0] = null;
258: }
259:
260: public void testGetNextVisualPositionFromNorth()
261: throws BadLocationException {
262: Bias[] bias = new Bias[1];
263: assertEquals(-1, painter.getNextVisualPositionFrom(view,
264: startOffset, Bias.Forward, alloc, SwingConstants.NORTH,
265: bias));
266: assertNull(bias[0]);
267: assertEquals(-1, painter.getNextVisualPositionFrom(view,
268: startOffset - 1, Bias.Forward, alloc,
269: SwingConstants.NORTH, bias));
270: assertNull(bias[0]);
271: assertEquals(-1, painter.getNextVisualPositionFrom(view,
272: startOffset + 1, Bias.Forward, alloc,
273: SwingConstants.NORTH, bias));
274: assertNull(bias[0]);
275: assertEquals(-1, painter.getNextVisualPositionFrom(view,
276: startOffset + 2, Bias.Forward, alloc,
277: SwingConstants.NORTH, bias));
278: assertNull(bias[0]);
279: }
280:
281: public void testGetNextVisualPositionFromSouth()
282: throws BadLocationException {
283: Bias[] bias = new Bias[1];
284: assertEquals(-1, painter.getNextVisualPositionFrom(view,
285: endOffset, Bias.Forward, alloc, SwingConstants.SOUTH,
286: bias));
287: assertNull(bias[0]);
288: assertEquals(-1, painter.getNextVisualPositionFrom(view,
289: endOffset + 1, Bias.Forward, alloc,
290: SwingConstants.SOUTH, bias));
291: assertNull(bias[0]);
292: assertEquals(-1, painter.getNextVisualPositionFrom(view,
293: endOffset - 1, Bias.Forward, alloc,
294: SwingConstants.SOUTH, bias));
295: assertNull(bias[0]);
296: assertEquals(-1, painter.getNextVisualPositionFrom(view,
297: endOffset - 2, Bias.Forward, alloc,
298: SwingConstants.SOUTH, bias));
299: assertNull(bias[0]);
300: }
301:
302: private int getX(final int end) {
303: return metrics.stringWidth(LEAF_TEXT.substring(0, end))
304: + alloc.x;
305: }
306: }
|