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.Graphics;
025: import java.awt.Rectangle;
026: import java.awt.Shape;
027: import java.awt.Toolkit;
028: import javax.swing.BasicSwingTestCase;
029: import javax.swing.SwingConstants;
030: import javax.swing.text.GlyphView.GlyphPainter;
031: import javax.swing.text.Position.Bias;
032:
033: /**
034: * Tests GlyphView class.
035: *
036: */
037: public class GlyphViewTest extends BasicSwingTestCase {
038: protected static class EmptyPainter extends GlyphPainter {
039: @Override
040: public float getSpan(GlyphView v, int p0, int p1,
041: TabExpander e, float x) {
042: return 0;
043: }
044:
045: @Override
046: public float getHeight(GlyphView v) {
047: return 0;
048: }
049:
050: @Override
051: public float getAscent(GlyphView v) {
052: return 0;
053: }
054:
055: @Override
056: public float getDescent(GlyphView v) {
057: return 0;
058: }
059:
060: @Override
061: public void paint(GlyphView v, Graphics g, Shape a, int p0,
062: int p1) {
063: }
064:
065: @Override
066: public Shape modelToView(GlyphView v, int pos, Bias bias,
067: Shape a) throws BadLocationException {
068: return null;
069: }
070:
071: @Override
072: public int viewToModel(GlyphView v, float x, float y, Shape a,
073: Bias[] biasRet) {
074: return 0;
075: }
076:
077: @Override
078: public int getBoundedPosition(GlyphView v, int p0, float x,
079: float len) {
080: return 0;
081: }
082:
083: @Override
084: public String toString() {
085: return "GlyphViewTest.EmptyPainter";
086: }
087: }
088:
089: private static final float ACCURACY = 0.00001f;
090:
091: private GlyphView view;
092:
093: private StyledDocument doc;
094:
095: private Element root;
096:
097: private Element leaf;
098:
099: private static final String TEXT = "012345";
100:
101: @Override
102: protected void setUp() throws Exception {
103: super .setUp();
104: doc = new DefaultStyledDocument();
105: root = doc.getDefaultRootElement();
106: doc.insertString(0, TEXT, null);
107: doc.setCharacterAttributes(0, TEXT.length() - 1,
108: SimpleAttributeSet.EMPTY, false);
109: leaf = root.getElement(0).getElement(0);
110: view = new GlyphView(leaf);
111: }
112:
113: public void testGlyphView() {
114: view = new GlyphView(root);
115: assertSame(root, view.getElement());
116: assertNull(view.getGlyphPainter());
117: }
118:
119: @SuppressWarnings("deprecation")
120: public void testGetPreferredSpan() {
121: assertNull(view.getGlyphPainter());
122: Font font = view.getFont();
123: FontMetrics metrics = Toolkit.getDefaultToolkit()
124: .getFontMetrics(font);
125: assertEquals(metrics.stringWidth(TEXT.substring(view
126: .getStartOffset(), view.getEndOffset())), view
127: .getPreferredSpan(View.X_AXIS), ACCURACY);
128: assertEquals(metrics.getHeight(), view
129: .getPreferredSpan(View.Y_AXIS), ACCURACY);
130: assertNotNull(view.getGlyphPainter());
131: }
132:
133: public void testGetPreferredSpanCustomPainter() {
134: view.setGlyphPainter(new EmptyPainter() {
135: @Override
136: public float getSpan(GlyphView v, int p0, int p1,
137: TabExpander e, float x) {
138: return (p1 - p0) * 0.4261f;
139: }
140:
141: @Override
142: public float getHeight(GlyphView v) {
143: return 0.524f;
144: }
145: });
146: assertEquals(
147: (view.getEndOffset() - view.getStartOffset()) * 0.4261f,
148: view.getPreferredSpan(View.X_AXIS), ACCURACY);
149: assertEquals(0.524f, view.getPreferredSpan(View.Y_AXIS),
150: ACCURACY);
151: }
152:
153: public void testGetStartOffset() {
154: assertEquals(leaf.getStartOffset(), view.getStartOffset());
155: }
156:
157: public void testGetEndOffset() {
158: assertEquals(leaf.getEndOffset(), view.getEndOffset());
159: }
160:
161: public void testSetGetGlyphPainter() {
162: assertNull(view.getGlyphPainter());
163: GlyphPainter painter = new EmptyPainter();
164: view.setGlyphPainter(painter);
165: assertSame(painter, view.getGlyphPainter());
166: }
167:
168: public void testGetText() {
169: Segment docText = view.getText(0, doc.getLength() + 1);
170: assertEquals(TEXT + "\n", docText.toString());
171: Segment text = view.getText(2, leaf.getEndOffset());
172: assertNotSame(docText, text);
173: assertEquals(TEXT.substring(2, TEXT.length() - 1), text
174: .toString());
175: try {
176: view.getText(3, 2).toString();
177: fail("an Error is expected");
178: } catch (Error e) {
179: // System.out.println(e.getMessage());
180: }
181: }
182:
183: public void testGetTabExpander() throws BadLocationException {
184: assertNull(view.getTabExpander());
185: View plainView = new ParagraphView(root.getElement(0));
186: assertTrue(plainView instanceof TabExpander);
187: plainView.replace(0, 0, new View[] { view });
188: assertTrue(view.getParent() instanceof TabExpander);
189: if (isHarmony()) {
190: assertSame(plainView, view.getTabExpander());
191: } else {
192: assertNull(view.getTabExpander());
193: }
194: doc.insertString(1, "\t", null);
195: if (isHarmony()) {
196: assertSame(plainView, view.getTabExpander());
197: } else {
198: assertNull(view.getTabExpander());
199: }
200: }
201:
202: @SuppressWarnings("deprecation")
203: public void testGetTabbedSpan() throws BadLocationException {
204: final Font font = view.getFont();
205: final FontMetrics metrics = Toolkit.getDefaultToolkit()
206: .getFontMetrics(font);
207: TabExpander expander = new TabExpander() {
208: public float nextTabStop(float x, int tabOffset) {
209: return (x / 10 + 1) * 10;
210: }
211: };
212: final String leafText = view.getText(view.getStartOffset(),
213: view.getEndOffset()).toString();
214: assertEquals(metrics.stringWidth(leafText), (int) view
215: .getTabbedSpan(0, expander));
216: doc.insertString(2, "\t", null); // leafText doesn't contain tab
217: final int beforeTab = metrics.stringWidth(leafText.substring(0,
218: 2));
219: assertEquals(metrics.stringWidth(leafText.substring(2))
220: + (int) expander.nextTabStop(beforeTab, 2), (int) view
221: .getTabbedSpan(0, expander));
222: }
223:
224: @SuppressWarnings("deprecation")
225: public void testGetPartialSpan() {
226: final Font font = view.getFont();
227: final FontMetrics metrics = Toolkit.getDefaultToolkit()
228: .getFontMetrics(font);
229: final String leafText = view.getText(view.getStartOffset(),
230: view.getEndOffset()).toString();
231: assertEquals(metrics.stringWidth(leafText), (int) view
232: .getPartialSpan(view.getStartOffset(), view
233: .getEndOffset()));
234: final String subText = leafText.substring(2, 4);
235: assertEquals(metrics.stringWidth(subText), (int) view
236: .getPartialSpan(2, 4));
237: }
238:
239: public void testCheckPainter() {
240: assertNull(view.getGlyphPainter());
241: view.checkPainter();
242: assertNotNull(view.getGlyphPainter());
243: GlyphView other = new GlyphView(root);
244: other.checkPainter();
245: assertSame(view.getGlyphPainter(), other.getGlyphPainter());
246: }
247:
248: /**
249: * Shape modelToView(int, Shape, Bias)
250: * This assures a default GlyphPainter is created.
251: */
252: public void testModelToView() throws BadLocationException {
253: assertNull(view.getGlyphPainter());
254: view.modelToView(0, new Rectangle(), Bias.Forward);
255: assertNotNull(view.getGlyphPainter());
256: }
257:
258: /**
259: * int viewToModel(float, float, Shape, Bias[])
260: * This assures a default GlyphPainter is created.
261: */
262: public void testViewToModel() {
263: assertNull(view.getGlyphPainter());
264: view.viewToModel(0, 0, new Rectangle(), new Bias[1]);
265: assertNotNull(view.getGlyphPainter());
266: }
267:
268: /**
269: * This assures a default GlyphPainter is created.
270: */
271: public void testGetNextVisualPositionFrom()
272: throws BadLocationException {
273: assertNull(view.getGlyphPainter());
274: try {
275: view.getNextVisualPositionFrom(0, Bias.Forward,
276: new Rectangle(), SwingConstants.EAST, new Bias[1]);
277: fail("No default painter is installed when this method is called");
278: } catch (NullPointerException e) {
279: }
280: view.setGlyphPainter(new EmptyPainter());
281: assertNotNull(view.getGlyphPainter());
282: // Causes no exception
283: view.getNextVisualPositionFrom(0, Bias.Forward,
284: new Rectangle(), SwingConstants.EAST, new Bias[1]);
285: }
286: }
|