Source Code Cross Referenced for TextLayoutTest.java in  » Apache-Harmony-Java-SE » java-package » java » awt » font » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Apache Harmony Java SE » java package » java.awt.font 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


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.awt.geom.*;
031:        import java.text.AttributedString;
032:        import java.text.AttributedCharacterIterator;
033:        import java.text.AttributedCharacterIterator.Attribute;
034:        import java.util.HashMap;
035:        import java.util.Map;
036:
037:        public class TextLayoutTest extends TestCase {
038:            private final int width = 500;
039:            private final int height = 200;
040:            private final BufferedImage im = new BufferedImage(width, height,
041:                    BufferedImage.TYPE_INT_RGB);
042:            TextLayout tl;
043:
044:            String strings[] = new String[] { "String 1", "String 2",
045:                    "String 3" };
046:
047:            TextLayout equals[] = new TextLayout[strings.length];
048:
049:            // For tl layout
050:            String s = "I TestItalic TestPlain I";
051:            Font f = new Font("times new roman", Font.ITALIC, 24);
052:            Font f1 = new Font("times new roman", Font.PLAIN, 60);
053:            FontRenderContext frc = ((Graphics2D) im.getGraphics())
054:                    .getFontRenderContext();
055:
056:            private final int layoutStartX = 1;
057:            private int layoutStartY;
058:
059:            public TextLayoutTest(String name) {
060:                super (name);
061:            }
062:
063:            @Override
064:            public void setUp() throws Exception {
065:                super .setUp();
066:
067:                AttributedString as = new AttributedString(s);
068:                as.addAttribute(TextAttribute.FONT, f, 0, 12);
069:                as.addAttribute(TextAttribute.FONT, f1, 12, s.length());
070:
071:                AttributedCharacterIterator aci = as.getIterator();
072:                tl = new TextLayout(aci, frc);
073:
074:                // Init layouts
075:                for (int i = 0; i < strings.length; i++) {
076:                    as = new AttributedString(strings[i]);
077:                    aci = as.getIterator();
078:                    equals[i] = new TextLayout(aci, frc);
079:                }
080:
081:                // To be able to measure advance and other metrics
082:                Graphics2D g2 = (Graphics2D) im.getGraphics();
083:                g2.setPaint(Color.WHITE);
084:                g2.fillRect(0, 0, im.getWidth(), im.getHeight());
085:                g2.setPaint(Color.BLACK);
086:                layoutStartY = (int) tl.getBounds().getHeight() + 1;
087:                tl.draw(g2, 1, layoutStartY);
088:            }
089:
090:            @Override
091:            public void tearDown() throws Exception {
092:                super .tearDown();
093:            }
094:
095:            public void testHashCode() throws Exception {
096:                for (int i = 0; i < strings.length; i++) {
097:                    for (int j = 0; j < strings.length; j++) {
098:                        if (i == j) {
099:                            assertTrue("HashCode " + equals[i] + " "
100:                                    + equals[j],
101:                                    equals[i].hashCode() == equals[j]
102:                                            .hashCode());
103:                        } else {
104:                            assertTrue("HashCode " + equals[i] + " "
105:                                    + equals[j],
106:                                    equals[i].hashCode() != equals[j]
107:                                            .hashCode());
108:                        }
109:                    }
110:                }
111:            }
112:
113:            public void testClone() throws Exception {
114:                assertTrue(tl.equals(tl.clone()));
115:            }
116:
117:            public void testEquals() throws Exception {
118:                for (int i = 0; i < strings.length; i++) {
119:                    for (int j = 0; j < strings.length; j++) {
120:                        if (i == j) {
121:                            assertTrue(equals[i].equals(equals[j]));
122:                        } else {
123:                            assertFalse(equals[i].equals(equals[j]));
124:                        }
125:                    }
126:                }
127:            }
128:
129:            public void testToString() throws Exception {
130:                tl.toString();
131:            }
132:
133:            public void testDraw() throws Exception {
134:                tl.draw((Graphics2D) im.getGraphics(), 1, (float) tl
135:                        .getBounds().getHeight() + 1);
136:            }
137:
138:            public void testGetAdvance() throws Exception {
139:                int left = im.getWidth(), right = 0;
140:
141:                for (int i = 0; i < im.getWidth(); i++) {
142:                    for (int j = 0; j < im.getHeight(); j++) {
143:                        if (im.getRGB(i, j) != 0xFFFFFFFF) {
144:                            left = Math.min(i, left);
145:                            right = Math.max(i, right);
146:                        }
147:                    }
148:                }
149:                assertEquals((int) tl.getAdvance(), right - left, 3);
150:            }
151:
152:            public void testGetAscent() throws Exception {
153:                assertEquals((int) tl.getAscent(), (int) f1.getLineMetrics(s,
154:                        frc).getAscent());
155:            }
156:
157:            public void testGetBaseline() throws Exception {
158:                assertEquals(tl.getBaseline(), Font.ROMAN_BASELINE);
159:            }
160:
161:            public void testGetBaselineOffsets() throws Exception {
162:                LineMetrics lm = f.getLineMetrics("A", frc);
163:                float correctBaselineOffsets[] = lm.getBaselineOffsets();
164:                float compatibleBaselineOffsets[] = { 0, -9, -21 };
165:
166:                float baselineOffsets[] = tl.getBaselineOffsets();
167:                assertEquals((int) baselineOffsets[tl.getBaseline()], 0);
168:
169:                for (int i = 0; i < baselineOffsets.length; i++) {
170:                    assertEquals(correctBaselineOffsets[i], baselineOffsets[i],
171:                            0.01);
172:                    assertEquals(compatibleBaselineOffsets[i],
173:                            baselineOffsets[i], 1);
174:                }
175:            }
176:
177:            private int[] findFirstLetterBounds() {
178:                int left = im.getWidth(), top = im.getHeight(), right = 0, bottom = 0;
179:
180:                boolean pointsStarted = false;
181:                for (int i = 0; i < im.getWidth(); i++) {
182:                    boolean hasPoint = false;
183:                    for (int j = 0; j < im.getHeight(); j++) {
184:                        if (im.getRGB(i, j) != 0xFFFFFFFF) {
185:                            hasPoint = true;
186:                            pointsStarted = true;
187:                            left = Math.min(i, left);
188:                            right = Math.max(right, i);
189:                            top = Math.min(top, j);
190:                            bottom = Math.max(bottom, j);
191:                        }
192:                    }
193:                    // Want to get only first letter
194:                    if (!hasPoint && pointsStarted) {
195:                        break;
196:                    }
197:                }
198:
199:                return new int[] { left, top, right, bottom };
200:            }
201:
202:            public void testGetBlackBoxBounds() throws Exception {
203:                Shape bounds = tl.getBlackBoxBounds(2, 7);
204:                bounds = tl.getBlackBoxBounds(0, 1);
205:                Rectangle2D rect = bounds.getBounds2D();
206:
207:                int letterBounds[] = findFirstLetterBounds();
208:
209:                assertEquals(rect.getMinX() + layoutStartX, letterBounds[0], 1);
210:                assertEquals(rect.getMaxX() + layoutStartX, letterBounds[2], 1);
211:                assertEquals(rect.getMaxY() + layoutStartY, letterBounds[3], 1);
212:                assertEquals(rect.getMinY() + layoutStartY, letterBounds[1], 1);
213:            }
214:
215:            public void testGetBounds() throws Exception {
216:                int left = im.getWidth(), right = 0, top = 0, bottom = im
217:                        .getHeight();
218:
219:                for (int i = 0; i < im.getWidth(); i++) {
220:                    for (int j = 0; j < im.getHeight(); j++) {
221:                        if (im.getRGB(i, j) != 0xFFFFFFFF) {
222:                            left = Math.min(i, left);
223:                            right = Math.max(i, right);
224:                            top = Math.max(j, top);
225:                            bottom = Math.min(j, bottom);
226:                        }
227:                    }
228:                }
229:
230:                Rectangle2D rect = tl.getBounds();
231:                Rectangle2D intRect = new Rectangle2D.Float((int) rect.getX(),
232:                        (int) rect.getY(), (int) rect.getWidth(), (int) rect
233:                                .getHeight());
234:                assertEquals(intRect.getX(), 0, 2);
235:                assertEquals(intRect.getY(), bottom - top, 2);
236:                assertEquals(intRect.getWidth(), right - left, 2);
237:                assertEquals(intRect.getHeight(), top - bottom, 2);
238:            }
239:
240:            public void testGetCaretInfo() throws Exception {
241:                float cInfo[] = tl.getCaretInfo(TextHitInfo.beforeOffset(0));
242:                assertEquals(cInfo[0], 0f, 0.1f);
243:                assertEquals(cInfo[1], f.getItalicAngle(), 0.1f);
244:
245:                cInfo = tl.getCaretInfo(TextHitInfo.afterOffset(s.length()));
246:                assertEquals(cInfo[0], tl.getAdvance(), 3);
247:                assertEquals(cInfo[1], 0, 0.1f);
248:
249:                cInfo = tl.getCaretInfo(TextHitInfo.afterOffset(3));
250:                assertTrue(cInfo[0] > 0);
251:                assertTrue(cInfo[0] < tl.getAdvance());
252:                assertEquals(cInfo[1], f.getItalicAngle(), 0.1f);
253:            }
254:
255:            public void testGetCaretInfo1() throws Exception {
256:                testGetCaretInfo(); // Same thing
257:            }
258:
259:            public void testGetCaretShape() throws Exception {
260:                Shape cShape = tl.getCaretShape(TextHitInfo.trailing(0));
261:
262:                int letterBounds[] = findFirstLetterBounds();
263:
264:                Point2D.Float p1 = new Point2D.Float(letterBounds[2],
265:                        letterBounds[1]); // Top right of first letter
266:
267:                boolean started = false;
268:                int i;
269:                for (i = 0; i < im.getWidth(); i++) {
270:                    if (im.getRGB(i, letterBounds[3]) != 0xFFFFFFFF) {
271:                        started = true;
272:                    } else {
273:                        if (started) {
274:                            break;
275:                        }
276:                    }
277:                }
278:                int bottomRight = i - 1;
279:
280:                Point2D.Float p2 = new Point2D.Float(bottomRight,
281:                        letterBounds[3]); // Bottom right of first letter
282:
283:                Rectangle2D rect1 = new Rectangle2D.Double(p1.getX() - 3, p1
284:                        .getY(), 3, 2);
285:                Rectangle2D rect2 = new Rectangle2D.Double(p2.getX() + 3, p2
286:                        .getY(), 3, 2);
287:
288:                cShape = AffineTransform.getTranslateInstance(layoutStartX,
289:                        layoutStartY).createTransformedShape(cShape);
290:
291:                Rectangle2D bounds = cShape.getBounds2D();
292:                Point2D.Double pShape1 = new Point2D.Double(bounds.getMinX(),
293:                        bounds.getMaxY());
294:                Point2D.Double pShape2 = new Point2D.Double(bounds.getMaxX(),
295:                        bounds.getMinY());
296:                Line2D cursor = new Line2D.Double(pShape1, pShape2);
297:
298:                assertTrue(rect1.intersectsLine(cursor));
299:                assertTrue(rect2.intersectsLine(cursor));
300:
301:            }
302:
303:            public void testGetCaretShape1() throws Exception {
304:                testGetCaretShape(); // Same
305:            }
306:
307:            public void testGetCaretShapes() throws Exception {
308:                Shape[] shapes = tl.getCaretShapes(1);
309:                assertNull(shapes[1]);
310:
311:                PathIterator it1 = shapes[0].getPathIterator(null);
312:                PathIterator it2 = tl.getCaretShape(TextHitInfo.trailing(0))
313:                        .getPathIterator(null);
314:
315:                while (!it1.isDone()) {
316:                    float arr1[] = new float[6];
317:                    float arr2[] = new float[6];
318:                    int seg1 = it1.currentSegment(arr1);
319:                    int seg2 = it2.currentSegment(arr2);
320:
321:                    assertEquals(seg1, seg2);
322:                    for (int i = 0; i < arr2.length; i++) {
323:                        assertEquals(arr1[i], arr2[i], 0.01);
324:                    }
325:
326:                    it1.next();
327:                    it2.next();
328:                }
329:            }
330:
331:            public void testGetCaretShapes1() throws Exception {
332:                testGetCaretShapes();
333:            }
334:
335:            public void testGetCaretShapes2() throws Exception {
336:                testGetCaretShapes();
337:            }
338:
339:            public void testGetCharacterCount() throws Exception {
340:                assertEquals(tl.getCharacterCount(), s.length());
341:            }
342:
343:            public void testGetCharacterLevel() throws Exception {
344:                assertEquals(tl.getCharacterLevel(5), 0);
345:            }
346:
347:            public void testGetDescent() throws Exception {
348:                assertEquals((int) tl.getDescent(), (int) f1.getLineMetrics(s,
349:                        frc).getDescent());
350:            }
351:
352:            public void testGetJustifiedLayout() throws Exception {
353:                TextLayout j1 = tl.getJustifiedLayout(500);
354:                TextLayout j2 = tl.getJustifiedLayout(200);
355:
356:                assertNotNull(j1);
357:                assertNotNull(j2);
358:
359:                assertEquals(500, j1.getAdvance(), 7);
360:                assertEquals(200, j2.getAdvance(), 7);
361:            }
362:
363:            public void testGetLeading() throws Exception {
364:                assertEquals((int) tl.getLeading(), (int) f1.getLineMetrics(s,
365:                        frc).getLeading());
366:            }
367:
368:            public void testGetLogicalHighlightShape() throws Exception {
369:                Shape highlight = tl.getLogicalHighlightShape(7, 3);
370:                Rectangle2D bbb = tl.getBlackBoxBounds(3, 7).getBounds2D();
371:                Rectangle2D bounds = highlight.getBounds2D();
372:
373:                bounds.add(bounds.getMaxX() + 2, bounds.getMaxY());
374:                assertTrue(bounds.contains(bbb));
375:
376:                Rectangle2D smallBounds = new Rectangle2D.Double(
377:                        bounds.getX() + 3, bounds.getY(),
378:                        bounds.getWidth() - 4, bounds.getHeight());
379:                assertFalse(smallBounds.contains(bbb));
380:
381:                assertEquals(
382:                        tl.getAscent() + tl.getDescent() + tl.getLeading(),
383:                        bounds.getHeight(), 5);
384:            }
385:
386:            public void testGetLogicalHighlightShape1() throws Exception {
387:                Shape highlight = tl.getLogicalHighlightShape(7, 3, tl
388:                        .getBounds());
389:                Rectangle2D bbb = tl.getBlackBoxBounds(3, 7).getBounds2D();
390:                Rectangle2D bounds = highlight.getBounds2D();
391:
392:                bounds.add(bounds.getMaxX() + 2, bounds.getMaxY());
393:                assertTrue(bounds.contains(bbb));
394:
395:                Rectangle2D smallBounds = new Rectangle2D.Double(
396:                        bounds.getX() + 3, bounds.getY(),
397:                        bounds.getWidth() - 4, bounds.getHeight());
398:                assertFalse(smallBounds.contains(bbb));
399:
400:                assertEquals(tl.getBounds().getHeight(), bounds.getHeight(), 0);
401:            }
402:
403:            public void testGetLogicalRangesForVisualSelection()
404:                    throws Exception {
405:                TextHitInfo i1 = TextHitInfo.leading(0);
406:                TextHitInfo i2 = TextHitInfo.leading(4);
407:                TextHitInfo i3 = TextHitInfo.trailing(9);
408:
409:                int res1[] = tl.getLogicalRangesForVisualSelection(i1, i2);
410:                int res2[] = tl.getLogicalRangesForVisualSelection(i2, i1);
411:
412:                assertEquals(2, res1.length);
413:                assertEquals(2, res2.length);
414:
415:                assertEquals(0, res1[0]);
416:                assertEquals(4, res1[1]);
417:
418:                assertEquals(res1[0], res2[0]);
419:                assertEquals(res1[1], res2[1]);
420:
421:                int res3[] = tl.getLogicalRangesForVisualSelection(i3, i2);
422:
423:                assertEquals(2, res3.length);
424:                assertEquals(4, res3[0]);
425:                assertEquals(10, res3[1]);
426:            }
427:
428:            public void testGetNextLeftHit() throws Exception {
429:                TextHitInfo i1 = tl.getNextLeftHit(0);
430:                assertNull(i1);
431:
432:                TextHitInfo i2 = tl.getNextLeftHit(4);
433:                assertEquals(3, i2.getCharIndex());
434:                assertTrue(i2.isLeadingEdge());
435:            }
436:
437:            public void testGetNextLeftHit1() throws Exception {
438:                TextHitInfo i1 = tl.getNextLeftHit(TextHitInfo.leading(0));
439:                assertNull(i1);
440:
441:                TextHitInfo i2 = tl.getNextLeftHit(TextHitInfo.trailing(3));
442:                assertEquals(3, i2.getCharIndex());
443:                assertTrue(i2.isLeadingEdge());
444:            }
445:
446:            public void testGetNextLeftHit2() throws Exception {
447:                TextHitInfo i1 = tl.getNextLeftHit(0,
448:                        TextLayout.DEFAULT_CARET_POLICY);
449:                assertNull(i1);
450:
451:                TextHitInfo i2 = tl.getNextLeftHit(4,
452:                        TextLayout.DEFAULT_CARET_POLICY);
453:                assertEquals(3, i2.getCharIndex());
454:                assertTrue(i2.isLeadingEdge());
455:            }
456:
457:            public void testGetNextRightHit() throws Exception {
458:                TextHitInfo i1 = tl.getNextRightHit(tl.getCharacterCount());
459:                assertNull(i1);
460:
461:                TextHitInfo i2 = tl.getNextRightHit(4);
462:                assertEquals(5, i2.getCharIndex());
463:                assertTrue(i2.isLeadingEdge());
464:            }
465:
466:            public void testGetNextRightHit1() throws Exception {
467:                TextHitInfo i1 = tl.getNextRightHit(TextHitInfo.trailing(tl
468:                        .getCharacterCount() - 1));
469:                assertNull(i1);
470:
471:                TextHitInfo i2 = tl.getNextRightHit(TextHitInfo.leading(4));
472:                assertEquals(5, i2.getCharIndex());
473:                assertTrue(i2.isLeadingEdge());
474:            }
475:
476:            public void testGetNextRightHit2() throws Exception {
477:                TextHitInfo i1 = tl.getNextRightHit(tl.getCharacterCount(),
478:                        TextLayout.DEFAULT_CARET_POLICY);
479:                assertNull(i1);
480:
481:                TextHitInfo i2 = tl.getNextRightHit(4,
482:                        TextLayout.DEFAULT_CARET_POLICY);
483:                assertEquals(5, i2.getCharIndex());
484:                assertTrue(i2.isLeadingEdge());
485:            }
486:
487:            public void testGetOutline() throws Exception {
488:                Shape outline1 = equals[0].getOutline(null);
489:                assertFalse(outline1.getBounds2D().getWidth() == 0);
490:
491:                Shape outline = tl.getOutline(AffineTransform
492:                        .getTranslateInstance(layoutStartX, layoutStartY));
493:                PathIterator pi = outline.getPathIterator(null);
494:
495:                while (!pi.isDone()) {
496:                    double seg[] = new double[6];
497:                    int segType = pi.currentSegment(seg);
498:
499:                    if (segType != PathIterator.SEG_CLOSE) {
500:                        int x = 1, y = 1;
501:                        switch (segType) {
502:                        case PathIterator.SEG_LINETO:
503:                        case PathIterator.SEG_MOVETO:
504:                            x += (int) seg[0];
505:                            y += (int) seg[1];
506:                            break;
507:                        case PathIterator.SEG_QUADTO:
508:                            //x += (int) seg[2];
509:                            //y += (int) seg[3];
510:                            //break;
511:                            pi.next();
512:                            continue;
513:                        case PathIterator.SEG_CUBICTO:
514:                            //x += (int) seg[4];
515:                            //y += (int) seg[5];
516:                            //break;
517:                            pi.next();
518:                            continue;
519:                        }
520:
521:                        x = Math.max(x, 1);
522:                        y = Math.max(y, 1);
523:
524:                        //System.out.println("x= " + x + "; y= "+ y + " Seg type= " + segType);
525:
526:                        // Now check if outlines are close enough to real letters.
527:                        boolean passed = false;
528:
529:                        for (int dx = -3; dx < 4; dx++) {
530:                            for (int dy = -2; dy < 3; dy++) {
531:                                if (x + dx < 1 || x + dx > im.getWidth()) {
532:                                    continue;
533:                                }
534:                                if (y + dy < 1 || y + dy > im.getHeight()) {
535:                                    continue;
536:                                }
537:
538:                                if (im.getRGB(x + dx, y + dy) != 0xFFFFFFFF) {
539:                                    passed = true;
540:                                    break;
541:                                }
542:                            }
543:                        }
544:
545:                        if (!passed) {
546:                            fail("Outline contains point located too far from glyph");
547:                        }
548:                    }
549:
550:                    pi.next();
551:                }
552:            }
553:
554:            public void testGetVisibleAdvance() throws Exception {
555:                assertEquals(tl.getBounds().getWidth(), tl.getVisibleAdvance(),
556:                        1f);
557:            }
558:
559:            public void testGetVisualHighlightShape() throws Exception {
560:                Shape highlight = tl.getVisualHighlightShape(TextHitInfo
561:                        .trailing(7), TextHitInfo.leading(3));
562:                Rectangle2D bbb = tl.getBlackBoxBounds(3, 7).getBounds2D();
563:                Rectangle2D bounds = highlight.getBounds2D();
564:
565:                bounds.add(bounds.getMaxX() + 2, bounds.getMaxY());
566:                assertTrue(bounds.contains(bbb));
567:
568:                Rectangle2D smallBounds = new Rectangle2D.Double(
569:                        bounds.getX() + 3, bounds.getY(),
570:                        bounds.getWidth() - 4, bounds.getHeight());
571:                assertFalse(smallBounds.contains(bbb));
572:
573:                assertEquals(
574:                        tl.getAscent() + tl.getDescent() + tl.getLeading(),
575:                        bounds.getHeight(), 5);
576:            }
577:
578:            public void testGetVisualHighlightShape1() throws Exception {
579:                Shape highlight = tl.getVisualHighlightShape(TextHitInfo
580:                        .trailing(7), TextHitInfo.leading(3), tl.getBounds());
581:                Rectangle2D bbb = tl.getBlackBoxBounds(3, 7).getBounds2D();
582:                Rectangle2D bounds = highlight.getBounds2D();
583:
584:                bounds.add(bounds.getMaxX() + 2, bounds.getMaxY());
585:                assertTrue(bounds.contains(bbb));
586:
587:                Rectangle2D smallBounds = new Rectangle2D.Double(
588:                        bounds.getX() + 3, bounds.getY(),
589:                        bounds.getWidth() - 4, bounds.getHeight());
590:                assertFalse(smallBounds.contains(bbb));
591:
592:                assertEquals(tl.getBounds().getHeight(), bounds.getHeight(), 0);
593:            }
594:
595:            public void testGetVisualOtherHit() throws Exception {
596:                TextHitInfo i1 = tl.getVisualOtherHit(TextHitInfo.leading(3));
597:                assertEquals(TextHitInfo.trailing(2), i1);
598:
599:                TextHitInfo i2 = tl.getVisualOtherHit(TextHitInfo.leading(0));
600:                assertEquals(TextHitInfo.trailing(-1), i2);
601:
602:                TextHitInfo i3 = tl.getVisualOtherHit(TextHitInfo.trailing(tl
603:                        .getCharacterCount() - 1));
604:                assertEquals(TextHitInfo.leading(tl.getCharacterCount()), i3);
605:            }
606:
607:            public void testHandleJustify() throws Exception {
608:                tl.handleJustify(500);
609:                assertEquals(500, tl.getAdvance(), 7);
610:
611:                try {
612:                    tl.handleJustify(200);
613:                    fail("Handle justify should throw an exception");
614:                } catch (IllegalStateException e) {
615:                }
616:
617:                assertEquals(500, tl.getAdvance(), 7);
618:            }
619:
620:            public void testHitTestChar() throws Exception {
621:                Rectangle2D bounds = tl.getBlackBoxBounds(3, 4).getBounds2D();
622:
623:                TextHitInfo i1 = tl.hitTestChar(
624:                        (float) bounds.getCenterX() + 2, (float) bounds
625:                                .getCenterY() + 2, tl.getBounds());
626:                assertEquals(TextHitInfo.trailing(3), i1);
627:
628:                TextHitInfo i2 = tl.hitTestChar(-20, 10, tl.getBounds());
629:                assertEquals(TextHitInfo.leading(0), i2);
630:
631:                TextHitInfo i3 = tl.hitTestChar(700, 10, tl.getBounds());
632:                assertEquals(TextHitInfo.trailing(tl.getCharacterCount() - 1),
633:                        i3);
634:            }
635:
636:            public void testHitTestChar1() throws Exception {
637:                Rectangle2D bounds = tl.getBlackBoxBounds(3, 4).getBounds2D();
638:
639:                TextHitInfo i1 = tl.hitTestChar(
640:                        (float) bounds.getCenterX() + 2, (float) bounds
641:                                .getCenterY() + 2);
642:                assertEquals(TextHitInfo.trailing(3), i1);
643:
644:                TextHitInfo i2 = tl.hitTestChar(-20, 10);
645:                assertEquals(TextHitInfo.leading(0), i2);
646:
647:                TextHitInfo i3 = tl.hitTestChar(700, 10);
648:                assertEquals(TextHitInfo.trailing(tl.getCharacterCount() - 1),
649:                        i3);
650:            }
651:
652:            public void testIsLeftToRight() throws Exception {
653:                assertTrue(tl.isLeftToRight());
654:            }
655:
656:            public void testIsVertical() throws Exception {
657:                assertFalse(tl.isVertical());
658:            }
659:
660:            public void testGetStrongCaret() throws Exception {
661:                TextHitInfo i = TextLayout.DEFAULT_CARET_POLICY.getStrongCaret(
662:                        TextHitInfo.trailing(4), TextHitInfo.leading(5), tl);
663:                assertEquals(TextHitInfo.leading(5), i);
664:            }
665:
666:            public void testTextLayoutConstructorConstraints() throws Exception {
667:                // regression test for Harmony-1464
668:                try {
669:                    new TextLayout(null, (Font) null, null);
670:                } catch (IllegalArgumentException e) {
671:                    // expected
672:                }
673:
674:                try {
675:                    new TextLayout(null, f, null);
676:                } catch (IllegalArgumentException e) {
677:                    // expected
678:                }
679:
680:                try {
681:                    new TextLayout("", f, null);
682:                } catch (IllegalArgumentException e) {
683:                    // expected
684:                }
685:
686:                try {
687:                    new TextLayout("aa", f, null);
688:                } catch (NullPointerException e) {
689:                    // expected
690:                }
691:
692:                try {
693:                    new TextLayout(null, null);
694:                } catch (IllegalArgumentException e) {
695:                    // expected
696:                }
697:
698:                AttributedString as = new AttributedString("test");
699:                as.addAttribute(TextAttribute.FONT, f, 0, 2);
700:
701:                try {
702:                    new TextLayout(as.getIterator(), null);
703:                } catch (NullPointerException e) {
704:                    // expected
705:                }
706:
707:                try {
708:                    new TextLayout(null, (Map<? extends Attribute, ?>) null,
709:                            (FontRenderContext) null);
710:                } catch (IllegalArgumentException e) {
711:                    System.out.println("success: " + e.getMessage());
712:                    // as expected
713:                }
714:
715:                try {
716:                    new TextLayout(null, new HashMap<Attribute, Object>(),
717:                            (FontRenderContext) null);
718:                } catch (IllegalArgumentException e) {
719:                    // as expected
720:                }
721:
722:                try {
723:                    new TextLayout("aa", new HashMap<Attribute, Object>(),
724:                            (FontRenderContext) null);
725:                } catch (NullPointerException e) {
726:                    // as expected
727:                }
728:
729:                try {
730:                    new TextLayout("", new HashMap<Attribute, Object>(),
731:                            (FontRenderContext) null);
732:                } catch (IllegalArgumentException e) {
733:                    // as expected
734:                    System.out.println("success: " + e.getMessage());
735:                }
736:
737:            }
738:
739:            public static Test suite() {
740:                return new TestSuite(TextLayoutTest.class);
741:            }
742:
743:            public static void main(String[] args) {
744:                junit.textui.TestRunner.run(TextLayoutTest.class);
745:            }
746:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.