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.Graphics;
023: import java.awt.Rectangle;
024: import java.awt.Shape;
025: import java.io.ByteArrayOutputStream;
026: import java.io.PrintStream;
027: import javax.swing.BasicSwingTestCase;
028: import javax.swing.SwingConstants;
029: import javax.swing.text.Position.Bias;
030:
031: /**
032: * Tests how GlyphView uses GlyphPainter.
033: *
034: */
035: public class GlyphView_GlyphPainterTest extends BasicSwingTestCase {
036: private static final float ACCURACY = 0.00001f;
037:
038: private GlyphView view;
039:
040: private StyledDocument doc;
041:
042: private Element root;
043:
044: private Element leaf;
045:
046: private Rectangle shape;
047:
048: private static ByteArrayOutputStream stream;
049:
050: private static PrintStream out;
051:
052: private static final String TEXT = "012345";
053:
054: private static final float DESCENT = 6;
055:
056: private static final float ASCENT = -30;
057:
058: private static final float HEIGHT = 20;
059:
060: private static class Painter extends GlyphView.GlyphPainter {
061: @Override
062: public float getSpan(GlyphView v, int p0, int p1,
063: TabExpander e, float x) {
064: out.println("getSpan(" + v + ", " + p0 + ", " + p1 + ", "
065: + e + ", " + x + ")");
066: return 0.125f;
067: }
068:
069: @Override
070: public float getHeight(GlyphView v) {
071: out.println("getHeight(" + v + ")");
072: return HEIGHT;
073: }
074:
075: @Override
076: public float getAscent(GlyphView v) {
077: out.println("getAscent(" + v + ")");
078: return ASCENT;
079: }
080:
081: @Override
082: public float getDescent(GlyphView v) {
083: out.println("getDescent(" + v + ")");
084: return DESCENT;
085: }
086:
087: @Override
088: public void paint(GlyphView v, Graphics g, Shape a, int p0,
089: int p1) {
090: out.println("paint(" + v + ", " + a + ", " + p0 + ", " + p1
091: + ")");
092: }
093:
094: @Override
095: public Shape modelToView(GlyphView v, int pos, Bias bias,
096: Shape a) throws BadLocationException {
097: out.println("modelToView(" + v + ", " + pos + ", " + bias
098: + ", " + a + ")");
099: return null;
100: }
101:
102: @Override
103: public int viewToModel(GlyphView v, float x, float y, Shape a,
104: Bias[] biasRet) {
105: out.println("viewToModel(" + v + ", " + x + ", " + y + ", "
106: + a + ")");
107: return 0;
108: }
109:
110: @Override
111: public int getBoundedPosition(GlyphView v, int p0, float x,
112: float len) {
113: out.println("getBoundedPosition(" + v + ", " + p0 + ", "
114: + x + ", " + len + ")");
115: return 0;
116: }
117:
118: @Override
119: public int getNextVisualPositionFrom(GlyphView v, int pos,
120: Bias b, Shape a, int direction, Bias[] biasRet)
121: throws BadLocationException {
122: out.println("getNextVisualPositionFrom(" + v + ", " + pos
123: + ", " + b + ", " + a + ", " + direction + ")");
124: int result = super .getNextVisualPositionFrom(v, pos, b, a,
125: direction, biasRet);
126: out.println("--> result " + result);
127: return result;
128: }
129:
130: @Override
131: public String toString() {
132: return "GlyphView_GlyphPainterTest.Painter";
133: }
134: }
135:
136: @Override
137: protected void setUp() throws Exception {
138: super .setUp();
139: stream = new ByteArrayOutputStream();
140: out = new PrintStream(stream);
141: doc = new DefaultStyledDocument();
142: root = doc.getDefaultRootElement();
143: doc.insertString(0, TEXT, null);
144: doc.setCharacterAttributes(0, TEXT.length() - 1,
145: SimpleAttributeSet.EMPTY, false);
146: leaf = root.getElement(0).getElement(0);
147: view = new GlyphView(leaf) {
148: @Override
149: public String toString() {
150: return "thisTest.view";
151: }
152: };
153: shape = new Rectangle(23, 41, 523, 671);
154: view.setGlyphPainter(new Painter());
155: // System.out.println(">>> " + getName());
156: }
157:
158: @Override
159: protected void tearDown() throws Exception {
160: super .tearDown();
161: // System.out.print(stream.toString());
162: // System.out.println("<<< " + getName() + "\n\n");
163: }
164:
165: public void testGetPreferredSpan() {
166: view.getPreferredSpan(View.X_AXIS);
167: assertEquals("getSpan(thisTest.view, 0, 5, null, 0.0)\n",
168: getFilteredString());
169: stream.reset();
170: view.getPreferredSpan(View.Y_AXIS);
171: assertEquals("getHeight(thisTest.view)\n", getFilteredString());
172: }
173:
174: public void testGetPartialSpan() {
175: view.getPartialSpan(2, 4);
176: assertEquals("getSpan(thisTest.view, 2, 4, null, 0.0)\n",
177: getFilteredString());
178: }
179:
180: public void testGetTabbedSpan() {
181: view.getTabbedSpan(1.5f, null);
182: assertEquals("getSpan(thisTest.view, 0, 5, null, 1.5)\n",
183: getFilteredString());
184: }
185:
186: public void testGetTabExpander() {
187: assertNull(view.getTabExpander());
188: assertEquals("", stream.toString());
189: }
190:
191: public void testModelToView() throws BadLocationException {
192: view.modelToView(2, new Rectangle(13, 21, 500, 200),
193: Bias.Forward);
194: assertEquals(
195: "modelToView(thisTest.view, 2, Forward, "
196: + "java.awt.Rectangle[x=13,y=21,width=500,height=200])\n",
197: getFilteredString());
198: }
199:
200: public void testViewToModel() {
201: Bias[] bias = new Bias[1];
202: view.viewToModel(17, 16, shape, bias);
203: assertEquals(
204: "viewToModel(thisTest.view, 17.0, 16.0, "
205: + "java.awt.Rectangle[x=23,y=41,width=523,height=671])\n",
206: getFilteredString());
207: assertNull(bias[0]);
208: }
209:
210: public void testGetNextVisualPositionFrom()
211: throws BadLocationException {
212: Bias[] bias = new Bias[1];
213: view.getNextVisualPositionFrom(3, Bias.Forward, shape,
214: SwingConstants.EAST, bias);
215: assertEquals(
216: "getNextVisualPositionFrom(thisTest.view, 3, Forward, "
217: + "java.awt.Rectangle[x=23,y=41,width=523,height=671],"
218: + " 3)\n" + "--> result 4\n",
219: getFilteredString());
220: assertSame(Bias.Forward, bias[0]);
221: }
222:
223: public void testGetAlignment() {
224: assertEquals(0.5f, view.getAlignment(View.X_AXIS), ACCURACY);
225: assertEquals("", stream.toString());
226: assertEquals((HEIGHT - DESCENT) / HEIGHT, view
227: .getAlignment(View.Y_AXIS), ACCURACY);
228: assertEquals("getHeight(thisTest.view)\n"
229: + "getDescent(thisTest.view)\n"
230: + (!isHarmony() ? "getAscent(thisTest.view)\n" : ""),
231: getFilteredString());
232: }
233:
234: public void testGetBreakWeight() {
235: view.getBreakWeight(View.X_AXIS, shape.x + 5.2f, 16);
236: assertEquals(
237: "getBoundedPosition(thisTest.view, 0, 28.2, 16.0)\n",
238: getFilteredString());
239: }
240:
241: public void testPaint() {
242: view.paint(createTestGraphics(), shape);
243: assertEquals(
244: "paint(thisTest.view, "
245: + "java.awt.Rectangle[x=23,y=41,width=523,height=671], "
246: + "0, 5)\n", getFilteredString());
247: }
248:
249: private String getFilteredString() {
250: return stream.toString().replaceAll("\r", "");
251: }
252: }
|