Source Code Cross Referenced for WrappedPlainView_SimpleTest.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.Container;
023:        import java.awt.FontMetrics;
024:        import java.awt.Rectangle;
025:        import javax.swing.BasicSwingTestCase;
026:        import javax.swing.JTextArea;
027:        import javax.swing.SwingTestCase;
028:        import javax.swing.event.DocumentEvent;
029:        import javax.swing.event.DocumentListener;
030:        import javax.swing.event.DocumentEvent.ElementChange;
031:
032:        /**
033:         * Tests WrappedPlainView methods which can be tested with more simple
034:         * initialization (without actual GUI being created).
035:         */
036:        public class WrappedPlainView_SimpleTest extends SwingTestCase {
037:            public class WrappedPlainViewImpl extends WrappedPlainView {
038:                public WrappedPlainViewImpl(final Element element,
039:                        final boolean wordWrap) {
040:                    super (element, wordWrap);
041:                }
042:
043:                public WrappedPlainViewImpl(final Element element) {
044:                    super (element);
045:                }
046:
047:                @Override
048:                public Container getContainer() {
049:                    return textArea;
050:                }
051:
052:                @Override
053:                public void preferenceChanged(final View child,
054:                        final boolean width, final boolean height) {
055:                    preferenceParams = new PreferenceChanged(child, width,
056:                            height);
057:                    super .preferenceChanged(child, width, height);
058:                }
059:
060:                @Override
061:                protected boolean updateChildren(final ElementChange change,
062:                        final DocumentEvent event, final ViewFactory factory) {
063:                    factoryUsed = factory;
064:                    return super .updateChildren(change, event, factory);
065:                }
066:            }
067:
068:            private static class PreferenceChanged {
069:                public View child;
070:
071:                public boolean width;
072:
073:                public boolean height;
074:
075:                public PreferenceChanged(final View child, final boolean width,
076:                        final boolean height) {
077:                    this .child = child;
078:                    this .width = width;
079:                    this .height = height;
080:                }
081:
082:                public void check(final View child, final boolean width,
083:                        final boolean height) {
084:                    assertEquals("Children are different", this .child, child);
085:                    assertEquals("Width is different", this .width, width);
086:                    assertEquals("Height is different", this .height, height);
087:                }
088:            }
089:
090:            private PreferenceChanged preferenceParams;
091:
092:            private ViewFactory factoryUsed;
093:
094:            private Document doc;
095:
096:            private Element root;
097:
098:            private WrappedPlainView view;
099:
100:            private JTextArea textArea;
101:
102:            private DocumentEvent docEvent;
103:
104:            private final int width = 40;
105:
106:            private final int height = 100;
107:
108:            private final Rectangle shape = new Rectangle(width, height);
109:
110:            @Override
111:            protected void setUp() throws Exception {
112:                super .setUp();
113:                doc = new PlainDocument();
114:                doc.addDocumentListener(new DocumentListener() {
115:                    public void changedUpdate(final DocumentEvent e) {
116:                        docEvent = e;
117:                    }
118:
119:                    public void insertUpdate(final DocumentEvent e) {
120:                        docEvent = e;
121:                    }
122:
123:                    public void removeUpdate(final DocumentEvent e) {
124:                        docEvent = e;
125:                    }
126:                });
127:                doc.insertString(0, "one, two, three, four, five\n"
128:                        + "eins, zwei, drei, vier, funf\n"
129:                        + "uno, dos, tres, cuatro, cinco", null);
130:                root = doc.getDefaultRootElement();
131:                textArea = new JTextArea(doc);
132:                view = new WrappedPlainViewImpl(root);
133:            }
134:
135:            public void testLoadChildren() {
136:                assertEquals(0, view.getViewCount());
137:                view.loadChildren(null);
138:                assertEquals(root.getElementCount(), view.getViewCount());
139:            }
140:
141:            /*
142:             * Class under test for void WrappedPlainView(Element)
143:             */
144:            public void testWrappedPlainViewElement()
145:                    throws BadLocationException {
146:                view = new WrappedPlainViewImpl(root);
147:                assertSame(root, view.getElement());
148:                assertEquals("Major axis expected to be Y", View.Y_AXIS, view
149:                        .getAxis());
150:                Element line2 = root.getElement(1);
151:                view.setSize(width, height);
152:                final int start = line2.getStartOffset();
153:                int breakOffset = view.calculateBreakPosition(start, line2
154:                        .getEndOffset());
155:                Container container = view.getContainer();
156:                FontMetrics metrics = container.getFontMetrics(container
157:                        .getFont());
158:                // Assert: text from start up to breakOffset fits into width, but
159:                //         it doesn't fit if we add the very next character
160:                if (isHarmony()) {
161:                    // 1.5 considers a symbol fits in available width if
162:                    // its half fits. We require it to entirely fit.
163:                    assertTrue(width >= metrics.stringWidth(doc.getText(start,
164:                            breakOffset - start)));
165:                }
166:                assertFalse(width >= metrics.stringWidth(doc.getText(start,
167:                        breakOffset - start + 1)));
168:            }
169:
170:            /*
171:             * Class under test for void WrappedPlainView(Element, boolean)
172:             */
173:            public void testWrappedPlainViewElementboolean()
174:                    throws BadLocationException {
175:                view = new WrappedPlainViewImpl(root, true);
176:                assertSame(root, view.getElement());
177:                assertEquals("Major axis expected to be Y", View.Y_AXIS, view
178:                        .getAxis());
179:                Element line2 = root.getElement(1);
180:                view.setSize(width, height);
181:                int breakOffset = view.calculateBreakPosition(line2
182:                        .getStartOffset(), line2.getEndOffset());
183:                assertEquals(34, breakOffset);
184:                assertEquals(" zw", doc.getText(breakOffset - 1, 3));
185:            }
186:
187:            public void testNextTabStop() {
188:                Container container = view.getContainer();
189:                FontMetrics metrics = container.getFontMetrics(container
190:                        .getFont());
191:                view.setSize(300, 100);
192:                float tabPos = view.getTabSize() * metrics.charWidth('m');
193:                assertEquals(8, view.getTabSize());
194:                assertEquals(tabPos, view.nextTabStop(0.0f, 0), 0.00001f);
195:                assertEquals(tabPos, view.nextTabStop(10.0f, 0), 0.00001f);
196:                assertEquals(tabPos, view.nextTabStop(tabPos - 1, 0), 0.00001f);
197:                assertEquals(tabPos * 2, view.nextTabStop(tabPos, 0), 0.00001f);
198:                // Setting tab size to 4 has no effect on already initialized view
199:                doc.putProperty(PlainDocument.tabSizeAttribute, new Integer(4));
200:                assertEquals(4, view.getTabSize());
201:                assertEquals(tabPos, view.nextTabStop(0.0f, 0), 0.00001f);
202:                assertEquals(tabPos * 2, view.nextTabStop(tabPos, 0), 0.00001f);
203:            }
204:
205:            public void testGetTabSize() {
206:                assertEquals(8, view.getTabSize());
207:                doc.putProperty(PlainDocument.tabSizeAttribute, new Integer(4));
208:                assertEquals(4, view.getTabSize());
209:            }
210:
211:            public void testGetLineBuffer() {
212:                Segment buffer = view.getLineBuffer();
213:                assertNotNull(buffer);
214:                assertSame(buffer, view.getLineBuffer());
215:                assertNotSame(buffer, new WrappedPlainView(root)
216:                        .getLineBuffer());
217:            }
218:
219:            /**
220:             * Tests calculateBreakPosition method with word wrapping turned off.
221:             */
222:            public void testCalculateBreakPosition01()
223:                    throws BadLocationException {
224:                Container container = view.getContainer();
225:                FontMetrics metrics = container.getFontMetrics(container
226:                        .getFont());
227:                Element line = root.getElement(0);
228:                int start = line.getStartOffset();
229:                int end = line.getEndOffset();
230:                int width = metrics.stringWidth(doc.getText(0, 7))
231:                        - metrics.stringWidth(doc.getText(6, 1)) / 2 - 1;
232:                view.setSize(width, height);
233:                assertEquals(6, view.calculateBreakPosition(start, end));
234:                assertFalse(" ".equals(doc.getText(5, 1)));
235:                if (!isHarmony()) {
236:                    // The next assertion may fail on 1.5 as it considers a symbol
237:                    // fits on the line if only half of it fits.
238:                    return;
239:                }
240:                start = 6;
241:                int index = start + 1;
242:                while (width > metrics.stringWidth(doc.getText(start, index
243:                        - start))
244:                        && index <= end) {
245:                    ++index;
246:                }
247:                if (index < end) {
248:                    --index;
249:                    assertEquals(index, view.calculateBreakPosition(start, end));
250:                    assertFalse(" ".equals(doc.getText(index - 1, 1)));
251:                } else {
252:                    fail("didn't managed to find another break location");
253:                }
254:            }
255:
256:            /**
257:             * Tests calculateBreakPosition method with word wrapping turned on.
258:             */
259:            public void testCalculateBreakPosition02()
260:                    throws BadLocationException {
261:                view = new WrappedPlainViewImpl(root, true);
262:                Container container = view.getContainer();
263:                FontMetrics metrics = container.getFontMetrics(container
264:                        .getFont());
265:                Element line = root.getElement(0);
266:                int start = line.getStartOffset();
267:                int end = line.getEndOffset();
268:                int width = metrics.stringWidth(doc.getText(0, 7))
269:                        - metrics.stringWidth(doc.getText(6, 1)) / 2 - 1;
270:                view.setSize(width, height);
271:                assertEquals(5, view.calculateBreakPosition(start, end));
272:                assertTrue(" ".equals(doc.getText(4, 1)));
273:                start = 6;
274:                int index = start + 1;
275:                while (width > metrics.stringWidth(doc.getText(start, index
276:                        - start))
277:                        && index <= end) {
278:                    ++index;
279:                }
280:                if (index < end) {
281:                    // Now move back to the space
282:                    do {
283:                        --index;
284:                    } while (!" ".equals(doc.getText(index, 1)));
285:                    assertEquals(index + 1, view.calculateBreakPosition(start,
286:                            end));
287:                    assertTrue(" ".equals(doc.getText(index, 1)));
288:                } else {
289:                    fail("didn't managed to find another break location");
290:                }
291:            }
292:
293:            /**
294:             * Tests calculateBreakPosition method with word wrapping turned on and
295:             * very-very long string.
296:             */
297:            public void testCalculateBreakPosition03()
298:                    throws BadLocationException {
299:                String veryLongString = "aVeryVeryVeryLongString";
300:                doc.insertString(root.getElement(1).getStartOffset(),
301:                        veryLongString, null);
302:                Container container = view.getContainer();
303:                FontMetrics metrics = container.getFontMetrics(container
304:                        .getFont());
305:                Element line = root.getElement(1);
306:                int start = line.getStartOffset();
307:                int end = line.getEndOffset();
308:                int width = metrics.stringWidth(veryLongString) / 2;
309:                view.setSize(width, height);
310:                int breakPos = view.calculateBreakPosition(start, end);
311:                // Create a new view with word wrapping
312:                view = new WrappedPlainViewImpl(root, true);
313:                view.setSize(width, height);
314:                assertEquals(breakPos, view.calculateBreakPosition(start, end));
315:                assertFalse(" ".equals(doc.getText(breakPos - 1, 1)));
316:            }
317:
318:            public void testInsertUpdate() {
319:                view.loadChildren(null);
320:                view.insertUpdate(docEvent, shape, null);
321:                if (BasicSwingTestCase.isHarmony()) {
322:                    assertNotNull(factoryUsed);
323:                    assertSame(factoryUsed, view.viewFactory);
324:                    assertEquals("javax.swing.text.WrappedPlainView$LineView",
325:                            factoryUsed.create(root).getClass().getName());
326:                }
327:            }
328:
329:            public void testRemoveUpdate() {
330:                view.loadChildren(null);
331:                view.removeUpdate(docEvent, shape, null);
332:                if (BasicSwingTestCase.isHarmony()) {
333:                    assertNotNull(factoryUsed);
334:                    assertSame(factoryUsed, view.viewFactory);
335:                }
336:            }
337:
338:            public void testChangedUpdate() {
339:                view.loadChildren(null);
340:                view.changedUpdate(docEvent, shape, null);
341:                if (BasicSwingTestCase.isHarmony()) {
342:                    assertNotNull(factoryUsed);
343:                    assertSame(factoryUsed, view.viewFactory);
344:                }
345:            }
346:
347:            public void testSetSize() {
348:                assertFalse(view.isLayoutValid(View.X_AXIS));
349:                assertFalse(view.isLayoutValid(View.Y_AXIS));
350:                view.setSize(width, height);
351:                assertTrue(view.isLayoutValid(View.X_AXIS));
352:                assertTrue(view.isLayoutValid(View.Y_AXIS));
353:                assertNotNull(preferenceParams);
354:                preferenceParams.check(null, true, true);
355:            }
356:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.