Source Code Cross Referenced for CompositeViewTest.java in  » Apache-Harmony-Java-SE » javax-package » javax » swing » text » 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 » javax package » javax.swing.text 
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 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 javax.swing.BasicSwingTestCase;
026:        import javax.swing.SwingConstants;
027:        import javax.swing.text.Position.Bias;
028:
029:        public class CompositeViewTest extends BasicSwingTestCase {
030:            private Document doc; // Document used in tests
031:
032:            private Element root; // Default root element of the document
033:
034:            private CompositeView view; // View object used in tests
035:
036:            private ViewFactory factory; // View factory used to create new views
037:
038:            private Shape shape; // View allocation (area to render into)
039:
040:            private Bias[] bias; // Place for bias return
041:
042:            private boolean childrenLoaded;
043:
044:            private int modelPosition;
045:
046:            /**
047:             * Controls implementation of isAfter(), isBefore() in CompositeViewImpl.
048:             */
049:            static boolean useBoth = true;
050:
051:            static boolean useX;
052:
053:            /**
054:             * Just implements all abstract methods of CompositeView.
055:             */
056:            protected static class CompositeViewImpl extends CompositeView {
057:                public CompositeViewImpl(final Element element) {
058:                    super (element);
059:                }
060:
061:                @Override
062:                protected void childAllocation(final int index,
063:                        final Rectangle rc) {
064:                }
065:
066:                @Override
067:                protected View getViewAtPoint(final int x, final int y,
068:                        final Rectangle shape) {
069:                    return null;
070:                }
071:
072:                @Override
073:                protected boolean isAfter(final int x, final int y,
074:                        final Rectangle rc) {
075:                    boolean result;
076:                    if (useBoth) {
077:                        result = x > rc.x + rc.width || y > rc.y + rc.height;
078:                    } else if (useX) {
079:                        result = x > rc.x + rc.width;
080:                    } else {
081:                        result = y > rc.y + rc.height;
082:                    }
083:                    return result;
084:                }
085:
086:                @Override
087:                protected boolean isBefore(final int x, final int y,
088:                        final Rectangle rc) {
089:                    boolean result;
090:                    if (useBoth) {
091:                        result = x < rc.x || y < rc.y;
092:                    } else if (useX) {
093:                        result = x < rc.x;
094:                    } else {
095:                        result = y < rc.y;
096:                    }
097:                    return result;
098:                }
099:
100:                @Override
101:                public void paint(final Graphics g, final Shape shape) {
102:                }
103:
104:                @Override
105:                public float getPreferredSpan(final int axis) {
106:                    return 0;
107:                }
108:            }
109:
110:            /*
111:             * @see TestCase#setUp()
112:             */
113:            @Override
114:            protected void setUp() throws Exception {
115:                super .setUp();
116:                doc = new PlainDocument();
117:                doc
118:                        .insertString(0, "line1\nline2\n\u05DC\u05DD\nline3\n",
119:                                null);
120:                // positions:        012345 678901 2     3     4 567890
121:                //                   0          1                     2
122:                view = new CompositeViewImpl(root = doc.getDefaultRootElement());
123:                view.loadChildren(factory = new ViewFactory() {
124:                    public View create(final Element line) {
125:                        return new PlainView(line);
126:                    }
127:                });
128:                shape = new Rectangle(100, 200);
129:                bias = new Position.Bias[1];
130:            }
131:
132:            public void testGetChildAllocation() {
133:                final class Params {
134:                    boolean called;
135:
136:                    int index;
137:
138:                    Rectangle rc;
139:
140:                    public void check(final int i, final Rectangle r) {
141:                        assertTrue(called);
142:                        assertEquals(i, index);
143:                        if (r != null) {
144:                            assertEquals(rc, r);
145:                        }
146:                        called = false;
147:                    }
148:                }
149:                final Params params = new Params();
150:                view = new CompositeViewImpl(root) {
151:                    @Override
152:                    protected void childAllocation(final int index,
153:                            final Rectangle rc) {
154:                        params.called = true;
155:                        params.index = index;
156:                        params.rc = rc;
157:                    }
158:                };
159:                // Make insets
160:                view.setParagraphInsets(getAttributeSet());
161:                assertNull(view.getChildAllocation(0, null));
162:                params.check(0, null);
163:                Shape alloc = view.getChildAllocation(1, shape);
164:                // The shape parameter is passed through getInsideAllocation
165:                params.check(1, new Rectangle(7, 0, 100 - 7 - 4, 200 - 5));
166:                assertSame(params.rc, alloc);
167:                assertSame(alloc, view.getChildAllocation(1, new Rectangle(0,
168:                        0, 300, 200)));
169:            }
170:
171:            /**
172:             * General tests for <code>replace</code> method.
173:             */
174:            public void testReplace01() throws BadLocationException {
175:                int count = root.getElementCount();
176:                assertEquals(count, view.getViewCount());
177:                int end = root.getElement(0).getEndOffset();
178:                doc.insertString(0, "\nnew1\nnew2", null); // inserted 10 chars
179:                View[] views = new View[] { new PlainView(root.getElement(0)),
180:                        new PlainView(root.getElement(1)) };
181:                View[] removed = new View[count - 1 - 1];
182:                for (int i = 0, index = 1; i < removed.length; i++, index++) {
183:                    removed[i] = view.getView(index);
184:                    assertSame(view, removed[i].getParent());
185:                }
186:                view.replace(1, count - 1, views);
187:                assertEquals(3, view.getViewCount()); // removed all except [0], added 2
188:                // Check first item is not deleted
189:                assertEquals(0, view.getView(0).getStartOffset());
190:                assertEquals(end + 10, view.getView(0).getEndOffset());
191:                // Check other items
192:                assertSame(views[0], view.getView(1));
193:                assertSame(views[1], view.getView(2));
194:                // Check parents of views added
195:                assertSame(view, views[0].getParent());
196:                assertSame(view, views[1].getParent());
197:                // Check removed views have no parent
198:                for (int i = 0; i < removed.length; i++) {
199:                    assertNull(removed[i].getParent());
200:                }
201:            }
202:
203:            public void testGetView() {
204:                assertSame(root.getElement(0), view.getView(0).getElement());
205:                assertTrue(view.getView(0) instanceof  PlainView);
206:                assertSame(root.getElement(1), view.getView(1).getElement());
207:                // Invalid indexes
208:                try {
209:                    view.getView(-1);
210:                    fail("ArrayIndexOutOfBoundsException must be thrown");
211:                } catch (ArrayIndexOutOfBoundsException e) {
212:                }
213:                try {
214:                    view.getView(view.getViewCount());
215:                    fail("ArrayIndexOutOfBoundsException must be thrown");
216:                } catch (ArrayIndexOutOfBoundsException e) {
217:                }
218:            }
219:
220:            public void testGetViewCount() {
221:                assertEquals(root.getElementCount(), view.getViewCount());
222:            }
223:
224:            public void testSetParent() {
225:                view = new CompositeViewImpl(root) {
226:                    @Override
227:                    protected void loadChildren(final ViewFactory factory) {
228:                        childrenLoaded = true;
229:                        assertSame(getViewFactory(), factory);
230:                        super .loadChildren(factory);
231:                    }
232:                };
233:                View parent = new PlainView(root.getElement(0)) {
234:                    @Override
235:                    public ViewFactory getViewFactory() {
236:                        return factory;
237:                    }
238:                };
239:                assertFalse(childrenLoaded);
240:                view.setParent(parent);
241:                assertTrue(childrenLoaded);
242:                assertSame(parent, view.getParent());
243:                // Call setParent one more time
244:                childrenLoaded = false;
245:                view.setParent(new PlainView(root));
246:                // As the view already has children, loadChildren must not be called
247:                assertFalse(childrenLoaded);
248:            }
249:
250:            public void testSetParentNull() throws Exception {
251:                view = new CompositeViewImpl(root) {
252:                    @Override
253:                    protected void loadChildren(final ViewFactory factory) {
254:                        childrenLoaded = true;
255:                        assertSame(getViewFactory(), factory);
256:                        super .loadChildren(factory);
257:                    }
258:                };
259:                assertEquals(0, view.getViewCount());
260:                assertFalse(childrenLoaded);
261:                view.setParent(null);
262:                assertFalse(childrenLoaded);
263:            }
264:
265:            public void testSetParentNoViewFactory() throws Exception {
266:                view = new CompositeViewImpl(root) {
267:                    @Override
268:                    protected void loadChildren(final ViewFactory factory) {
269:                        childrenLoaded = true;
270:                        assertNull(factory);
271:                        super .loadChildren(factory);
272:                    }
273:                };
274:                assertEquals(0, view.getViewCount());
275:                assertFalse(childrenLoaded);
276:                final PlainView parent = new PlainView(root);
277:                assertNull(parent.getViewFactory());
278:                view.setParent(parent);
279:                assertTrue(childrenLoaded);
280:            }
281:
282:            public void testCompositeView() {
283:                view = new CompositeViewImpl(root);
284:                assertEquals(0, view.getViewCount());
285:                assertNull(view.getViewFactory());
286:                assertNull(view.getParent());
287:            }
288:
289:            public void testGetInsideAllocation() {
290:                view.setParagraphInsets(getAttributeSet());
291:                Rectangle rc = view.getInsideAllocation(new Rectangle(20, 30,
292:                        50, 40));
293:                assertEquals(20 + 7, rc.x);
294:                assertEquals(30 + 0, rc.y);
295:                assertEquals(50 - 7 - 4, rc.width);
296:                assertEquals(40 - 0 - 5, rc.height);
297:                // Returns the same instance whenever called
298:                assertSame(rc, view.getInsideAllocation(new Rectangle(rc)));
299:                assertNull(view.getInsideAllocation(null));
300:            }
301:
302:            // Regression test for HARMONY-2189
303:            public void testGetInsideAllocationOverridden() {
304:                final Marker top = new Marker();
305:                final Marker left = new Marker();
306:                final Marker bottom = new Marker();
307:                final Marker right = new Marker();
308:
309:                view = new CompositeViewImpl(root = doc.getDefaultRootElement()) {
310:                    @Override
311:                    protected short getTopInset() {
312:                        top.setOccurred();
313:                        return 12;
314:                    }
315:
316:                    @Override
317:                    protected short getLeftInset() {
318:                        left.setOccurred();
319:                        return 11;
320:                    }
321:
322:                    @Override
323:                    protected short getBottomInset() {
324:                        bottom.setOccurred();
325:                        return 3;
326:                    }
327:
328:                    @Override
329:                    protected short getRightInset() {
330:                        right.setOccurred();
331:                        return 9;
332:                    }
333:                };
334:                view.loadChildren(factory);
335:
336:                view.setParagraphInsets(getAttributeSet());
337:                assertFalse(top.isOccurred());
338:                assertFalse(left.isOccurred());
339:                assertFalse(bottom.isOccurred());
340:                assertFalse(right.isOccurred());
341:
342:                Rectangle rc = view.getInsideAllocation(new Rectangle(20, 30,
343:                        50, 40));
344:                assertTrue(top.isOccurred());
345:                assertTrue(left.isOccurred());
346:                assertTrue(bottom.isOccurred());
347:                assertTrue(right.isOccurred());
348:
349:                assertEquals(20 + 11, rc.x);
350:                assertEquals(30 + 12, rc.y);
351:                assertEquals(50 - 11 - 9, rc.width);
352:                assertEquals(40 - 12 - 3, rc.height);
353:            }
354:
355:            /*
356:             * See tests for individual methods namely EastWest and NorthSouth in
357:             * class CompositeView_NextNSVisPosTest
358:             * @throws BadLocationException
359:             */
360:            public void testGetNextVisualPositionFrom()
361:                    throws BadLocationException {
362:                final class CompositeViewNextPos extends CompositeViewImpl {
363:                    public CompositeViewNextPos(final Element element) {
364:                        super (element);
365:                    }
366:
367:                    int offset;
368:
369:                    Position.Bias bias;
370:
371:                    Shape shape;
372:
373:                    int dir;
374:
375:                    boolean isEastWest;
376:
377:                    boolean isNorthSouth;
378:
379:                    @Override
380:                    protected int getNextEastWestVisualPositionFrom(
381:                            final int pos, final Position.Bias b,
382:                            final Shape a, final int direction,
383:                            final Position.Bias[] biasRet) {
384:                        isEastWest = true;
385:                        isNorthSouth = false;
386:                        offset = pos;
387:                        bias = b;
388:                        shape = a;
389:                        dir = direction;
390:                        return 0;
391:                    }
392:
393:                    @Override
394:                    protected int getNextNorthSouthVisualPositionFrom(
395:                            final int pos, final Position.Bias b,
396:                            final Shape a, final int direction,
397:                            final Position.Bias[] biasRet) {
398:                        isEastWest = false;
399:                        isNorthSouth = true;
400:                        offset = pos;
401:                        bias = b;
402:                        shape = a;
403:                        dir = direction;
404:                        return 0;
405:                    }
406:                }
407:                CompositeViewNextPos view = new CompositeViewNextPos(root);
408:                view.getNextVisualPositionFrom(0, Bias.Backward, shape,
409:                        SwingConstants.EAST, bias);
410:                assertTrue(view.isEastWest);
411:                view.isEastWest = false;
412:                assertFalse(view.isNorthSouth);
413:                assertEquals(0, view.offset);
414:                assertSame(Bias.Backward, view.bias);
415:                assertSame(shape, view.shape);
416:                assertEquals(SwingConstants.EAST, view.dir);
417:                view.getNextVisualPositionFrom(5, Bias.Forward, shape,
418:                        SwingConstants.WEST, bias);
419:                assertTrue(view.isEastWest);
420:                view.isEastWest = false;
421:                assertFalse(view.isNorthSouth);
422:                assertEquals(5, view.offset);
423:                assertSame(Bias.Forward, view.bias);
424:                assertSame(shape, view.shape);
425:                assertEquals(SwingConstants.WEST, view.dir);
426:                // Vertical directions
427:                view.getNextVisualPositionFrom(1, Bias.Backward, shape,
428:                        SwingConstants.SOUTH, bias);
429:                assertFalse(view.isEastWest);
430:                assertTrue(view.isNorthSouth);
431:                view.isNorthSouth = false;
432:                assertEquals(1, view.offset);
433:                assertSame(Bias.Backward, view.bias);
434:                assertSame(shape, view.shape);
435:                assertEquals(SwingConstants.SOUTH, view.dir);
436:                view.getNextVisualPositionFrom(0, Bias.Forward, shape,
437:                        SwingConstants.NORTH, bias);
438:                assertFalse(view.isEastWest);
439:                assertTrue(view.isNorthSouth);
440:                view.isNorthSouth = false;
441:                assertEquals(0, view.offset);
442:                assertSame(Bias.Forward, view.bias);
443:                assertSame(shape, view.shape);
444:                assertEquals(SwingConstants.NORTH, view.dir);
445:                try {
446:                    view.getNextVisualPositionFrom(0, Bias.Backward, shape,
447:                            SwingConstants.NORTH_EAST, bias);
448:                    fail("IllegalArgumentException must be thrown");
449:                } catch (IllegalArgumentException e) {
450:                }
451:            }
452:
453:            public void testFlipEastAndWestAtEnds() {
454:                assertFalse(view.flipEastAndWestAtEnds(0,
455:                        Position.Bias.Backward));
456:            }
457:
458:            /**
459:             * Tests the number of children before and after the call.
460:             */
461:            public void testLoadChildren01() {
462:                view = new CompositeViewImpl(root);
463:                assertEquals(0, view.getViewCount());
464:                view.loadChildren(factory);
465:                assertEquals(root.getElementCount(), view.getViewCount());
466:                assertSame(view, view.getView(0).getParent());
467:                assertSame(root.getElement(0), view.getView(0).getElement());
468:            }
469:
470:            public void testLoadChildrenNull() {
471:                view = new CompositeViewImpl(root);
472:                assertEquals(0, view.getViewCount());
473:                view.loadChildren(null);
474:                assertEquals(0, view.getViewCount());
475:            }
476:
477:            /**
478:             * This tests <code>int getViewIndex(int, Bias)</code>.
479:             * <p>The object is constructed upon root element of plain text document.
480:             * This element starts at document start (offset of 0) and ends at
481:             * document end (<code>view.getEndOffset() == doc.getLength()</code>).
482:             */
483:            public void testGetViewIndexintBias() {
484:                final Marker marker = new Marker(true);
485:                view = new CompositeViewImpl(root) {
486:                    @Override
487:                    protected int getViewIndexAtPosition(final int pos) {
488:                        marker.setOccurred();
489:                        modelPosition = pos;
490:                        return -10101;
491:                    }
492:                };
493:                view.loadChildren(factory);
494:                // Before document beginning
495:                assertEquals(-1, view.getViewIndex(-1, Bias.Forward));
496:                assertFalse(marker.isOccurred());
497:                assertEquals(-1, view.getViewIndex(-1, Bias.Backward));
498:                assertFalse(marker.isOccurred());
499:                // The first position (0)
500:                view.getViewIndex(0, Bias.Forward);
501:                assertTrue(marker.isOccurred());
502:                assertEquals(0, modelPosition);
503:                assertEquals(-1, view.getViewIndex(0, Bias.Backward));
504:                assertFalse(marker.isOccurred());
505:                // In the middle
506:                view.getViewIndex(4, Bias.Forward);
507:                assertTrue(marker.isOccurred());
508:                assertEquals(4, modelPosition);
509:                view.getViewIndex(4, Bias.Backward);
510:                assertTrue(marker.isOccurred());
511:                assertEquals(3, modelPosition);
512:                view.getViewIndex(7, Bias.Forward);
513:                assertTrue(marker.isOccurred());
514:                assertEquals(7, modelPosition);
515:                view.getViewIndex(7, Bias.Backward);
516:                assertTrue(marker.isOccurred());
517:                assertEquals(6, modelPosition);
518:                // At an edge
519:                view.getViewIndex(5, Bias.Forward);
520:                assertTrue(marker.isOccurred());
521:                assertEquals(5, modelPosition);
522:                view.getViewIndex(5, Bias.Backward);
523:                assertTrue(marker.isOccurred());
524:                assertEquals(4, modelPosition);
525:                view.getViewIndex(6, Bias.Forward);
526:                assertTrue(marker.isOccurred());
527:                assertEquals(6, modelPosition);
528:                view.getViewIndex(6, Bias.Backward);
529:                assertTrue(marker.isOccurred());
530:                assertEquals(5, modelPosition);
531:                // In the end
532:                final int lastOffset = doc.getLength();
533:                view.getViewIndex(doc.getLength(), Bias.Forward);
534:                assertTrue(marker.isOccurred());
535:                assertEquals(doc.getLength(), modelPosition);
536:                view.getViewIndex(doc.getLength(), Bias.Backward);
537:                assertTrue(marker.isOccurred());
538:                assertEquals(lastOffset - 1, modelPosition);
539:                assertEquals(-1, view.getViewIndex(doc.getLength() + 3,
540:                        Bias.Forward));
541:                assertFalse(marker.isOccurred());
542:                assertEquals(-1, view.getViewIndex(doc.getLength() + 3,
543:                        Bias.Backward));
544:                assertFalse(marker.isOccurred());
545:            }
546:
547:            /**
548:             * This tests <code>int getViewIndex(int, Bias)</code>.
549:             * <p>The object is constructed upon element which represents a paragraph
550:             * of a styled text document. The element niether starts at 0, nor it ends
551:             * at <code>doc.getLength()</code>.
552:             */
553:            public void testGetViewIndexintBiasNotRoot()
554:                    throws BadLocationException {
555:                doc = new DefaultStyledDocument();
556:                final MutableAttributeSet bold = new SimpleAttributeSet();
557:                StyleConstants.setBold(bold, true);
558:                final MutableAttributeSet italic = new SimpleAttributeSet();
559:                StyleConstants.setItalic(italic, true);
560:                doc.insertString(doc.getLength(), "line1\n", null); //  0 (6)
561:                doc.insertString(doc.getLength(), "plain", null); //  6 (5) [ 6, 11]
562:                doc.insertString(doc.getLength(), "bold", bold); // 11 (4) [11, 15]
563:                doc.insertString(doc.getLength(), "italic", italic); // 15 (6) [15, 21]
564:                doc.insertString(doc.getLength(), "\nline3: ", null); // 21 (8) [21, 22]
565:                doc.insertString(doc.getLength(), "bold2", bold); // 29 (5)
566:                root = doc.getDefaultRootElement();
567:                final Element paragraph = root.getElement(1);
568:                assertEquals(4, paragraph.getElementCount());
569:                assertEquals(6, paragraph.getStartOffset());
570:                assertEquals(22, paragraph.getEndOffset());
571:                final Marker marker = new Marker(true);
572:                view = new CompositeViewImpl(paragraph) {
573:                    @Override
574:                    protected int getViewIndexAtPosition(final int pos) {
575:                        marker.setOccurred();
576:                        modelPosition = pos;
577:                        return -10101;
578:                    }
579:                };
580:                // Init the view by adding children
581:                view.loadChildren(factory);
582:                // Before document beginning
583:                assertEquals(-1, view.getViewIndex(-1, Bias.Forward));
584:                assertFalse(marker.isOccurred());
585:                assertEquals(-1, view.getViewIndex(-1, Bias.Backward));
586:                assertFalse(marker.isOccurred());
587:                // Before the start offset (6)
588:                assertEquals(-1, view.getViewIndex(0, Bias.Forward));
589:                assertFalse(marker.isOccurred());
590:                assertEquals(-1, view.getViewIndex(0, Bias.Backward));
591:                assertFalse(marker.isOccurred());
592:                assertEquals(-1, view.getViewIndex(3, Bias.Forward));
593:                assertFalse(marker.isOccurred());
594:                assertEquals(-1, view.getViewIndex(3, Bias.Backward));
595:                assertFalse(marker.isOccurred());
596:                // At the start offset (6)
597:                assertEquals(-1, view.getViewIndex(5, Bias.Forward));
598:                assertFalse(marker.isOccurred());
599:                assertEquals(-1, view.getViewIndex(5, Bias.Backward));
600:                assertFalse(marker.isOccurred());
601:                assertEquals(-10101, view.getViewIndex(6, Bias.Forward));
602:                assertTrue(marker.isOccurred());
603:                assertEquals(6, modelPosition);
604:                assertEquals(-1, view.getViewIndex(6, Bias.Backward));
605:                assertFalse(marker.isOccurred());
606:                assertEquals(-10101, view.getViewIndex(7, Bias.Forward));
607:                assertTrue(marker.isOccurred());
608:                assertEquals(7, modelPosition);
609:                assertEquals(-10101, view.getViewIndex(7, Bias.Backward));
610:                assertTrue(marker.isOccurred());
611:                assertEquals(6, modelPosition);
612:                // At an edge
613:                view.getViewIndex(10, Bias.Forward);
614:                assertTrue(marker.isOccurred());
615:                assertEquals(10, modelPosition);
616:                view.getViewIndex(10, Bias.Backward);
617:                assertTrue(marker.isOccurred());
618:                assertEquals(9, modelPosition);
619:                view.getViewIndex(11, Bias.Forward);
620:                assertTrue(marker.isOccurred());
621:                assertEquals(11, modelPosition);
622:                view.getViewIndex(11, Bias.Backward);
623:                assertTrue(marker.isOccurred());
624:                assertEquals(10, modelPosition);
625:                view.getViewIndex(12, Bias.Forward);
626:                assertTrue(marker.isOccurred());
627:                assertEquals(12, modelPosition);
628:                view.getViewIndex(12, Bias.Backward);
629:                assertTrue(marker.isOccurred());
630:                assertEquals(11, modelPosition);
631:                // In the end
632:                view.getViewIndex(21, Bias.Forward);
633:                assertTrue(marker.isOccurred());
634:                assertEquals(21, modelPosition);
635:                view.getViewIndex(21, Bias.Backward);
636:                assertTrue(marker.isOccurred());
637:                assertEquals(20, modelPosition);
638:                assertEquals(-1, view.getViewIndex(22, Bias.Forward));
639:                assertFalse(marker.isOccurred());
640:                assertEquals(-10101, view.getViewIndex(22, Bias.Backward));
641:                assertTrue(marker.isOccurred());
642:                assertEquals(21, modelPosition);
643:                assertEquals(-1, view.getViewIndex(23, Bias.Forward));
644:                assertFalse(marker.isOccurred());
645:                assertEquals(-1, view.getViewIndex(23, Bias.Backward));
646:                assertFalse(marker.isOccurred());
647:                assertEquals(-1, view.getViewIndex(24, Bias.Forward));
648:                assertFalse(marker.isOccurred());
649:                assertEquals(-1, view.getViewIndex(24, Bias.Backward));
650:                assertFalse(marker.isOccurred());
651:            }
652:
653:            public void testGetViewIndexAtPosition() {
654:                assertEquals(0, view.getViewIndexAtPosition(-1));
655:                assertEquals(0, view.getViewIndexAtPosition(0));
656:                assertEquals(0, view.getViewIndexAtPosition(5));
657:                assertEquals(1, view.getViewIndexAtPosition(6));
658:                assertEquals(1, view.getViewIndexAtPosition(11));
659:                assertEquals(2, view.getViewIndexAtPosition(12));
660:                assertEquals(view.getViewCount() - 1, view
661:                        .getViewIndexAtPosition(doc.getLength()));
662:                assertEquals(view.getViewCount() - 1, view
663:                        .getViewIndexAtPosition(doc.getLength() + 2));
664:            }
665:
666:            public void testGetViewIndexAtPositionNotRoot()
667:                    throws BadLocationException {
668:                doc = new DefaultStyledDocument();
669:                final MutableAttributeSet bold = new SimpleAttributeSet();
670:                StyleConstants.setBold(bold, true);
671:                final MutableAttributeSet italic = new SimpleAttributeSet();
672:                StyleConstants.setItalic(italic, true);
673:                doc.insertString(doc.getLength(), "line1\n", null); //  0 (6)
674:                doc.insertString(doc.getLength(), "plain", null); //  6 (5)
675:                doc.insertString(doc.getLength(), "bold", bold); // 11 (4)
676:                doc.insertString(doc.getLength(), "italic", italic); // 15 (6)
677:                doc.insertString(doc.getLength(), "\nline3: ", null); // 21 (8) / 22
678:                doc.insertString(doc.getLength(), "bold2", bold); // 29 (5)
679:                root = doc.getDefaultRootElement();
680:                final Element paragraph = root.getElement(1);
681:                assertEquals(4, paragraph.getElementCount());
682:                assertEquals(6, paragraph.getStartOffset());
683:                assertEquals(22, paragraph.getEndOffset());
684:                view = new CompositeViewImpl(paragraph);
685:                view.loadChildren(factory);
686:                assertEquals(0, view.getViewIndexAtPosition(-1));
687:                assertEquals(0, view.getViewIndexAtPosition(0));
688:                assertEquals(0, view.getViewIndexAtPosition(5));
689:                assertEquals(0, view.getViewIndexAtPosition(6));
690:                assertEquals(0, view.getViewIndexAtPosition(7));
691:                assertEquals(0, view.getViewIndexAtPosition(10));
692:                assertEquals(1, view.getViewIndexAtPosition(11));
693:                assertEquals(1, view.getViewIndexAtPosition(12));
694:                assertEquals(1, view.getViewIndexAtPosition(14));
695:                assertEquals(2, view.getViewIndexAtPosition(15));
696:                assertEquals(2, view.getViewIndexAtPosition(16));
697:                assertEquals(3, view.getViewIndexAtPosition(21));
698:                assertEquals(3, view.getViewIndexAtPosition(22));
699:                assertEquals(3, view.getViewIndexAtPosition(23));
700:                assertEquals(3, view.getViewIndexAtPosition(doc.getLength()));
701:            }
702:
703:            public void testGetViewAtPosition() {
704:                Rectangle alloc;
705:                alloc = view.getInsideAllocation(shape);
706:                assertSame(view.getView(0), view.getViewAtPosition(-1, alloc));
707:                assertEquals(alloc, view.getChildAllocation(-1, shape));
708:                alloc = view.getInsideAllocation(shape);
709:                assertSame(view.getView(0), view.getViewAtPosition(0, alloc));
710:                assertEquals(alloc, view.getChildAllocation(0, shape));
711:                alloc = view.getInsideAllocation(shape);
712:                assertSame(view.getView(0), view.getViewAtPosition(5, alloc));
713:                assertEquals(alloc, view.getChildAllocation(5, shape));
714:                alloc = view.getInsideAllocation(shape);
715:                assertSame(view.getView(1), view.getViewAtPosition(6, alloc));
716:                assertEquals(alloc, view.getChildAllocation(6, shape));
717:                alloc = view.getInsideAllocation(shape);
718:                assertSame(view.getView(1), view.getViewAtPosition(11, alloc));
719:                assertEquals(alloc, view.getChildAllocation(11, shape));
720:                alloc = view.getInsideAllocation(shape);
721:                assertSame(view.getView(2), view.getViewAtPosition(12, alloc));
722:                assertEquals(alloc, view.getChildAllocation(12, shape));
723:                alloc = view.getInsideAllocation(shape);
724:                assertSame(view.getView(view.getViewCount() - 1), view
725:                        .getViewAtPosition(doc.getLength(), alloc));
726:                assertEquals(alloc, view.getChildAllocation(doc.getLength(),
727:                        shape));
728:            }
729:
730:            /**
731:             * Keys for attribute set creation.
732:             */
733:            private static Object[] keys = { StyleConstants.SpaceAbove,
734:                    StyleConstants.LeftIndent, StyleConstants.SpaceBelow,
735:                    StyleConstants.RightIndent };
736:
737:            /**
738:             * Values for attribute set creation.
739:             */
740:            private static Object[] values = { new Float(0.1f), // top inset
741:                    new Float(7.0f), // left
742:                    new Float(5.3f), // bottom
743:                    new Float(4.7f) // right
744:            };
745:
746:            /**
747:             * Creates an attribute set with a subset of key-value pairs.
748:             *
749:             * @param start the first index to use
750:             * @param end <code>(end - 1)</code> is the last index to use
751:             *
752:             * @return created attribute set
753:             */
754:            static AttributeSet getAttributeSet(final int start, final int end) {
755:                SimpleAttributeSet attrs = new SimpleAttributeSet();
756:                for (int i = start; i < end; i++) {
757:                    attrs.addAttribute(keys[i], values[i]);
758:                }
759:                return attrs;
760:            }
761:
762:            /**
763:             * Creates attribute set with all the key-value pairs.
764:             *
765:             * @return created attribute set
766:             */
767:            static AttributeSet getAttributeSet() {
768:                return getAttributeSet(0, 4);
769:            }
770:
771:            public void testGetLeftInset() {
772:                AttributeSet attrs = getAttributeSet(1, 2);
773:                view.setParagraphInsets(attrs);
774:                assertEquals(7.0f, StyleConstants.getLeftIndent(attrs),
775:                        0.00001f);
776:                assertInsets(0, 7, 0, 0);
777:            }
778:
779:            public void testGetTopInset() {
780:                AttributeSet attrs = getAttributeSet(0, 1);
781:                view.setParagraphInsets(attrs);
782:                assertEquals(0.1f, StyleConstants.getSpaceAbove(attrs),
783:                        0.00001f);
784:                assertInsets(0, 0, 0, 0);
785:            }
786:
787:            public void testGetRightInset() {
788:                AttributeSet attrs = getAttributeSet(3, 4);
789:                view.setParagraphInsets(attrs);
790:                assertEquals(4.7f, StyleConstants.getRightIndent(attrs),
791:                        0.00001f);
792:                assertInsets(0, 0, 0, 4);
793:            }
794:
795:            public void testGetBottomInset() {
796:                AttributeSet attrs = getAttributeSet(2, 3);
797:                view.setParagraphInsets(attrs);
798:                assertEquals(5.3f, StyleConstants.getSpaceBelow(attrs),
799:                        0.00001f);
800:                assertInsets(0, 0, 5, 0);
801:            }
802:
803:            /**
804:             * Checks view has the expected inset values.
805:             *
806:             * @param top expected top inset
807:             * @param left expected left inset
808:             * @param bottom expected bottom inset
809:             * @param right expected right inset
810:             */
811:            private void assertInsets(final short top, final short left,
812:                    final short bottom, final short right) {
813:                assertEquals("Top", top, view.getTopInset());
814:                assertEquals("Left", left, view.getLeftInset());
815:                assertEquals("Bottom", bottom, view.getBottomInset());
816:                assertEquals("Right", right, view.getRightInset());
817:            }
818:
819:            /**
820:             * Checks view has the expected inset values. A conveniece method
821:             * to convert <code>int</code> to <code>short</code>
822:             *
823:             * @param top expected top inset
824:             * @param left expected left inset
825:             * @param bottom expected bottom inset
826:             * @param right expected right inset
827:             */
828:            private void assertInsets(final int top, final int left,
829:                    final int bottom, final int right) {
830:                assertInsets((short) top, (short) left, (short) bottom,
831:                        (short) right);
832:            }
833:
834:            public void testSetInsets() {
835:                assertInsets(0, 0, 0, 0);
836:                view.setInsets((short) 5, (short) 10, (short) 7, (short) 13);
837:                assertInsets(5, 10, 7, 13);
838:            }
839:
840:            public void testSetParagraphInsets() {
841:                view.setParagraphInsets(getAttributeSet());
842:                assertInsets(0, 7, 5, 4);
843:            }
844:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.