Source Code Cross Referenced for TestTextPanel.java in  » Internationalization-Localization » icu4j » com » ibm » richtext » test » 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 » Internationalization Localization » icu4j » com.ibm.richtext.test 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /*
002:         * (C) Copyright IBM Corp. 1998-2004.  All Rights Reserved.
003:         *
004:         * The program is provided "as is" without any warranty express or
005:         * implied, including the warranty of non-infringement and the implied
006:         * warranties of merchantibility and fitness for a particular purpose.
007:         * IBM will not be liable for any damages suffered by you as a result
008:         * of using the Program. In no event will IBM be liable for any
009:         * special, indirect or consequential damages or lost profits even if
010:         * IBM has been advised of the possibility of their occurrence. IBM
011:         * will not be liable for any third party claims against you.
012:         */
013:        package com.ibm.richtext.test;
014:
015:        import java.util.Random;
016:        import java.awt.Color;
017:        import java.awt.Frame;
018:        import java.awt.event.KeyEvent;
019:
020:        import java.awt.datatransfer.Clipboard;
021:        import java.awt.datatransfer.ClipboardOwner;
022:        import java.awt.datatransfer.StringSelection;
023:        import java.awt.datatransfer.Transferable;
024:
025:        import com.ibm.richtext.textpanel.KeyRemap;
026:        import com.ibm.richtext.textpanel.KeyEventForwarder;
027:        import com.ibm.richtext.textpanel.MTextPanel;
028:        import com.ibm.richtext.textpanel.TextPanel;
029:        import com.ibm.richtext.textpanel.TextPanelEvent;
030:        import com.ibm.richtext.textpanel.TextPanelListener;
031:
032:        import com.ibm.richtext.styledtext.MConstText;
033:        import com.ibm.richtext.styledtext.StyledText;
034:        import com.ibm.richtext.textlayout.attributes.TextAttribute;
035:        import com.ibm.richtext.textlayout.attributes.AttributeMap;
036:
037:        import com.ibm.richtext.styledtext.StyleModifier;
038:        import com.ibm.richtext.textlayout.attributes.AttributeSet;
039:
040:        // Note:  this used to be a TestFmwk test.  If you add
041:        // more tests to it, be sure to add them to 
042:        // com.ibm.richtext.test.unit.FmwkTestTextPanel.test() -
043:        // otherwise they won't get run!
044:
045:        public class TestTextPanel /*extends TestFmwk*/{
046:
047:            private final class TestListener implements  TextPanelListener {
048:
049:                private int NO_WAY = 0;
050:                private int DEFINITELY = 1;
051:                private int MAYBE = 2;
052:
053:                private final int[] status;
054:
055:                TestListener(MTextPanel textPanel) {
056:
057:                    int length = TextPanelEvent.TEXT_PANEL_LAST
058:                            - TextPanelEvent.TEXT_PANEL_FIRST + 1;
059:                    status = new int[length];
060:                    textPanel.addListener(this );
061:                    allowAll();
062:                }
063:
064:                void refuseAll() {
065:
066:                    for (int i = 0; i < status.length; i++) {
067:                        status[i] = NO_WAY;
068:                    }
069:                }
070:
071:                void allowAll() {
072:
073:                    for (int i = 0; i < status.length; i++) {
074:                        status[i] = MAYBE;
075:                    }
076:                }
077:
078:                void expectEvent(int type) {
079:
080:                    int index = type - TextPanelEvent.TEXT_PANEL_FIRST;
081:                    status[index] = DEFINITELY;
082:                }
083:
084:                void allowEvent(int type) {
085:                    int index = type - TextPanelEvent.TEXT_PANEL_FIRST;
086:                    status[index] = MAYBE;
087:                }
088:
089:                void assertNotExpectingEvents() {
090:                    assertNotExpectingEvents(false, 0, false);
091:                }
092:
093:                void assertNotExpectingEvents(int iterationCount, boolean exp) {
094:                    assertNotExpectingEvents(true, iterationCount, exp);
095:                }
096:
097:                private void assertNotExpectingEvents(boolean logDetails,
098:                        int iterationCount, boolean exp) {
099:
100:                    boolean e = false;
101:                    for (int i = 0; i < status.length; i++) {
102:                        if (status[i] == DEFINITELY) {
103:                            if (logDetails) {
104:                                logMessage("Expecting event "
105:                                        + (i + TextPanelEvent.TEXT_PANEL_FIRST));
106:                                logMessage("iterationCount=" + iterationCount
107:                                        + ";  expexting=" + exp);
108:                            }
109:                            e = true;
110:                        }
111:                    }
112:                    if (e) {
113:                        reportError("Some events pending");
114:                    }
115:                }
116:
117:                public void textEventOccurred(TextPanelEvent event) {
118:
119:                    int index = event.getID() - TextPanelEvent.TEXT_PANEL_FIRST;
120:                    if (status[index] == NO_WAY) {
121:                        reportError("Unexpected event: " + event);
122:                    } else if (status[index] == DEFINITELY) {
123:                        status[index] = NO_WAY;
124:                    }
125:                }
126:
127:                public boolean respondsToEventType(int type) {
128:
129:                    return true;
130:                }
131:            }
132:
133:            static final String COPYRIGHT = "(C) Copyright IBM Corp. 1998-1999 - All Rights Reserved";
134:
135:            private static final String STRING_CONTENT = "Some content";
136:            private static final int TEST_ITERS = 2;
137:            public static final MConstText MOD_TEXT = new StyledText("Styled",
138:                    AttributeMap.EMPTY_ATTRIBUTE_MAP);
139:
140:            private Clipboard fClipboard;
141:            private MTextPanel fTextPanel = null;
142:            private TestListener fListener = null;
143:            private int fRandSeed = 0;
144:            private Random rand;
145:
146:            private static final int BIG_COMMAND_LOG_SIZE = 40;
147:            private static final int SMALL_COMMAND_LOG_SIZE = 8;
148:
149:            private static final StyleModifier[] paraMods = {
150:                    StyleModifier.createAddModifier(new AttributeMap(
151:                            TextAttribute.LINE_FLUSH,
152:                            TextAttribute.FLUSH_LEADING)),
153:                    StyleModifier.createAddModifier(TextAttribute.LINE_FLUSH,
154:                            TextAttribute.FLUSH_CENTER),
155:                    StyleModifier.createAddModifier(TextAttribute.LINE_FLUSH,
156:                            TextAttribute.FLUSH_TRAILING),
157:                    StyleModifier.createAddModifier(TextAttribute.LINE_FLUSH,
158:                            TextAttribute.FULLY_JUSTIFIED),
159:                    StyleModifier.createAddModifier(
160:                            TextAttribute.RUN_DIRECTION,
161:                            TextAttribute.RUN_DIRECTION_RTL),
162:                    StyleModifier.createAddModifier(
163:                            TextAttribute.RUN_DIRECTION,
164:                            TextAttribute.RUN_DIRECTION_LTR),
165:                    StyleModifier.createRemoveModifier(new AttributeSet(
166:                            TextAttribute.LINE_FLUSH)),
167:                    StyleModifier.createRemoveModifier(new AttributeSet(
168:                            TextAttribute.RUN_DIRECTION)) };
169:
170:            private static final int SELECT = 0;
171:            private static final int SET_CARET_POS = 1;
172:            private static final int SET_START = 2;
173:            private static final int SET_END = 3;
174:
175:            // using both styles of add modifier: AttributeMap and
176:            // key-value, just for variety...
177:            private static final StyleModifier[] charMods = {
178:                    StyleModifier.createAddModifier(new AttributeMap(
179:                            TextAttribute.WEIGHT, TextAttribute.WEIGHT_BOLD)),
180:                    StyleModifier.createAddModifier(TextAttribute.FOREGROUND,
181:                            Color.green),
182:                    StyleModifier
183:                            .createAddModifier(new AttributeMap(
184:                                    TextAttribute.UNDERLINE,
185:                                    TextAttribute.UNDERLINE_ON)
186:                                    .addAttributes(new AttributeMap(
187:                                            TextAttribute.SIZE, new Float(6)))),
188:                    StyleModifier.createReplaceModifier(new AttributeMap(
189:                            TextAttribute.FAMILY, "Dialog")),
190:                    StyleModifier.createRemoveModifier(new AttributeSet(
191:                            new Object[] { TextAttribute.WEIGHT,
192:                                    TextAttribute.POSTURE,
193:                                    TextAttribute.UNDERLINE,
194:                                    TextAttribute.STRIKETHROUGH,
195:                                    TextAttribute.SUPERSCRIPT })),
196:                    StyleModifier.IDENTITY };
197:
198:            private static final char[] TYPED_CHARS = new char[128 - ' ' + 3];
199:            static {
200:                TYPED_CHARS[0] = 8; // backspace
201:                TYPED_CHARS[1] = '\t';
202:                TYPED_CHARS[2] = '\n';
203:                for (int i = 3; i < TYPED_CHARS.length; i++) {
204:                    TYPED_CHARS[i] = (char) (' ' + i - 3);
205:                }
206:            }
207:
208:            public TestTextPanel() {
209:
210:                fClipboard = new Clipboard("TestTextPanel");
211:                incRandSeed();
212:            }
213:
214:            protected void reportError(String message) {
215:
216:                System.err.println(message);
217:                throw new RuntimeException(message);
218:                //super.errln(message);
219:            }
220:
221:            protected void logMessage(String message) {
222:
223:                System.err.println(message);
224:                //super.logMessage(message);
225:            }
226:
227:            public TestTextPanel(MTextPanel panel) {
228:
229:                this ();
230:                setTextPanel(panel);
231:            }
232:
233:            void incRandSeed() {
234:
235:                rand = new Random(++fRandSeed);
236:            }
237:
238:            int getRandSeed() {
239:
240:                return fRandSeed;
241:            }
242:
243:            int randInt(int limit) {
244:
245:                return randInt(0, limit);
246:            }
247:
248:            int randInt(int start, int limit) {
249:
250:                if (start > limit) {
251:                    throw new IllegalArgumentException("Range is 0-length.");
252:                } else if (start == limit) {
253:                    return start;
254:                }
255:
256:                return start + (Math.abs(rand.nextInt()) % (limit - start));
257:            }
258:
259:            public void test() {
260:
261:                AttributeMap bold = new AttributeMap(TextAttribute.WEIGHT,
262:                        TextAttribute.WEIGHT_BOLD);
263:                MConstText text1 = new StyledText(
264:                        "Test contents. 1234\nHow about it?",
265:                        AttributeMap.EMPTY_ATTRIBUTE_MAP);
266:                MConstText text2 = new StyledText("Another test string.", bold);
267:
268:                _testWithText(text1);
269:                _testWithText(text2);
270:                _testWithText(new StyledText());
271:
272:                StyledText big1 = new StyledText();
273:                for (int i = 0; i < 50; i++) {
274:                    big1.append(text1);
275:                }
276:
277:                _testWithText(big1);
278:                StyledText big2 = new StyledText(text1);
279:                for (int i = 0; i < 80; i++) {
280:                    big2.append(text2);
281:                }
282:
283:                _testWithText(big2);
284:            }
285:
286:            private void setTextPanel(MTextPanel panel) {
287:
288:                fTextPanel = panel;
289:                fListener = new TestListener(panel);
290:            }
291:
292:            private void _testWithText(MConstText text) {
293:
294:                setTextPanel(new TextPanel(text, fClipboard));
295:
296:                for (int i = 0; i < TEST_ITERS; i++) {
297:                    _testSetSelection();
298:                    _testModifications(MOD_TEXT, true);
299:                    _testEditMenuOperations(fClipboard);
300:                    _testModFlag(fTextPanel.getCommandLogSize());
301:                    _testCommandLogControl();
302:                }
303:            }
304:
305:            private void _testSelection(int function, final int aStart,
306:                    final int aLimit) {
307:
308:                int oldStart = fTextPanel.getSelectionStart();
309:                int oldLimit = fTextPanel.getSelectionEnd();
310:
311:                final int length = fTextPanel.getTextLength();
312:
313:                int start = aStart;
314:                int limit = aLimit;
315:
316:                if (start < 0) {
317:                    start = 0;
318:                } else if (start > length) {
319:                    start = length;
320:                }
321:
322:                if (limit < start) {
323:                    limit = start;
324:                } else if (limit > length) {
325:                    limit = length;
326:                }
327:
328:                fListener.refuseAll();
329:
330:                if (oldStart != start || oldLimit != limit) {
331:                    fListener
332:                            .expectEvent(TextPanelEvent.SELECTION_RANGE_CHANGED);
333:                    fListener
334:                            .allowEvent(TextPanelEvent.SELECTION_STYLES_CHANGED);
335:                }
336:                if ((oldStart == oldLimit) != (start == limit)) {
337:                    fListener
338:                            .expectEvent(TextPanelEvent.SELECTION_EMPTY_CHANGED);
339:                }
340:                if (oldStart == oldLimit) {
341:                    fListener.allowEvent(TextPanelEvent.UNDO_STATE_CHANGED);
342:                }
343:
344:                switch (function) {
345:                case SELECT:
346:                    fTextPanel.select(aStart, aLimit);
347:                    break;
348:                case SET_CARET_POS:
349:                    fTextPanel.setCaretPosition(aStart);
350:                    break;
351:                case SET_START:
352:                    fTextPanel.setSelectionStart(aStart);
353:                    break;
354:                case SET_END:
355:                    fTextPanel.setSelectionEnd(aLimit);
356:                    break;
357:                default:
358:                    throw new IllegalArgumentException("Invalid function");
359:                }
360:
361:                if (fTextPanel.getSelectionStart() != start) {
362:                    reportError("getSelectionStart is incorrect after set");
363:                }
364:                if (fTextPanel.getSelectionEnd() != limit) {
365:                    reportError("getSelectionEnd is incorrect after set");
366:                }
367:                fListener.assertNotExpectingEvents();
368:                fListener.allowAll();
369:            }
370:
371:            private void setAndTestSelection(int start, int limit) {
372:
373:                _testSelection(SELECT, start, limit);
374:            }
375:
376:            private void setAndTestCaret(int caretPos) {
377:
378:                _testSelection(SET_CARET_POS, caretPos, caretPos);
379:            }
380:
381:            private void setAndTestSelectionStart(int selStart) {
382:
383:                int limit = fTextPanel.getSelectionEnd();
384:                _testSelection(SET_START, selStart, limit);
385:            }
386:
387:            private void setAndTestSelectionEnd(int selEnd) {
388:
389:                int start = fTextPanel.getSelectionStart();
390:                _testSelection(SET_END, start, selEnd);
391:            }
392:
393:            public void _testSetSelection() {
394:
395:                int textLength = fTextPanel.getTextLength();
396:                if (textLength != fTextPanel.getText().length()) {
397:                    reportError("Text panel length is not correct");
398:                }
399:
400:                setAndTestSelection(0, textLength / 2);
401:                setAndTestSelection(textLength / 2, textLength);
402:                setAndTestSelection(0, textLength);
403:                setAndTestSelection(-1, textLength + 1);
404:                if (textLength > 0) {
405:                    setAndTestSelection(0, textLength - 1);
406:                    setAndTestSelection(0, 1);
407:                }
408:
409:                final int incAmount = Math.max(1, textLength / 5);
410:                for (int index = 0; index <= textLength; index += incAmount) {
411:
412:                    setAndTestCaret(index);
413:                    setAndTestSelectionStart(textLength - index);
414:                    setAndTestSelectionEnd(textLength);
415:                    setAndTestSelectionStart(0);
416:                    setAndTestSelectionEnd(textLength - index);
417:                }
418:            }
419:
420:            /**
421:             * Text must be editable to pass this test.
422:             */
423:            public void _testModifications(MConstText insertionText,
424:                    boolean restoreOldText) {
425:
426:                MConstText oldText = new StyledText(fTextPanel.getText());
427:                final int insLength = insertionText.length();
428:
429:                fListener.allowAll();
430:                fListener.expectEvent(TextPanelEvent.TEXT_CHANGED);
431:                fListener.expectEvent(TextPanelEvent.SELECTION_RANGE_CHANGED);
432:                fTextPanel.append(insertionText);
433:                fListener.assertNotExpectingEvents();
434:
435:                if (fTextPanel.getSelectionStart() != oldText.length()
436:                        + insLength) {
437:                    reportError("Append didn't result in correct selection");
438:                }
439:
440:                fListener.expectEvent(TextPanelEvent.TEXT_CHANGED);
441:                fListener.expectEvent(TextPanelEvent.SELECTION_RANGE_CHANGED);
442:                fTextPanel.insert(insertionText, 0);
443:                fListener.assertNotExpectingEvents();
444:                fListener.allowAll();
445:
446:                if (fTextPanel.getSelectionStart() != insLength) {
447:                    reportError("Insert didn't result in correct selection");
448:                }
449:
450:                fTextPanel.replaceRange(insertionText, insLength, insLength
451:                        + oldText.length());
452:                if (fTextPanel.getSelectionStart() != insLength * 2) {
453:                    reportError("Replace didn't result in correct selection");
454:                }
455:                if (fTextPanel.getSelectionEnd() != insLength * 2) {
456:                    reportError("Replace didn't result in correct selection");
457:                }
458:                if (fTextPanel.getTextLength() != insLength * 3) {
459:                    reportError("textLength is incorrect");
460:                }
461:
462:                if (restoreOldText) {
463:                    fTextPanel.setText(oldText);
464:                    if (fTextPanel.getSelectionStart() != oldText.length()) {
465:                        reportError("setText didn't result in correct selection");
466:                    }
467:                    if (fTextPanel.getTextLength() != oldText.length()) {
468:                        reportError("length incorrect after setText");
469:                    }
470:                }
471:
472:                fListener.allowAll();
473:            }
474:
475:            private static int iterationCount = 0;
476:
477:            public void _testCommandLogControl() {
478:
479:                fListener.refuseAll();
480:                iterationCount++;
481:                boolean exp = false;
482:                if (fTextPanel.canRedo() || BIG_COMMAND_LOG_SIZE == 0) {
483:                    fListener.expectEvent(TextPanelEvent.UNDO_STATE_CHANGED);
484:                    exp = true;
485:                }
486:                //try {
487:                fTextPanel.setCommandLogSize(BIG_COMMAND_LOG_SIZE);
488:
489:                if (fTextPanel.canRedo()) {
490:                    reportError("canRedo after setCommandLogSize");
491:                }
492:                fListener.assertNotExpectingEvents(iterationCount, exp);
493:                //}
494:                //catch(Error e) {
495:                //    logMessage("iterationCount="+iterationCount+";  expexting="+exp);
496:                //    throw e;
497:                //}
498:
499:                MConstText insText = new StyledText("7",
500:                        AttributeMap.EMPTY_ATTRIBUTE_MAP);
501:
502:                final int origLength = fTextPanel.getTextLength();
503:                int start = origLength / 3;
504:
505:                fListener.allowEvent(TextPanelEvent.SELECTION_RANGE_CHANGED);
506:                fListener.allowEvent(TextPanelEvent.SELECTION_STYLES_CHANGED);
507:
508:                for (int i = start; i < BIG_COMMAND_LOG_SIZE + start; i++) {
509:                    fListener.expectEvent(TextPanelEvent.UNDO_STATE_CHANGED);
510:                    fListener.expectEvent(TextPanelEvent.TEXT_CHANGED);
511:                    if (fTextPanel.getSelectionStart() != fTextPanel
512:                            .getSelectionEnd()) {
513:                        fListener
514:                                .expectEvent(TextPanelEvent.SELECTION_EMPTY_CHANGED);
515:                    }
516:                    fTextPanel.insert(insText, i);
517:                    fListener.assertNotExpectingEvents();
518:                }
519:
520:                fListener.allowEvent(TextPanelEvent.SELECTION_EMPTY_CHANGED);
521:
522:                for (int i = 0; i < BIG_COMMAND_LOG_SIZE - 1; i++) {
523:                    fListener.expectEvent(TextPanelEvent.UNDO_STATE_CHANGED);
524:                    fListener.expectEvent(TextPanelEvent.TEXT_CHANGED);
525:                    fTextPanel.undo();
526:                    fListener.assertNotExpectingEvents();
527:                }
528:                if (!fTextPanel.canUndo()) {
529:                    reportError("Command log is too small");
530:                }
531:
532:                fListener.allowAll();
533:                fTextPanel.undo();
534:                if (fTextPanel.canUndo()) {
535:                    reportError("Command log is too large");
536:                }
537:
538:                if (fTextPanel.getTextLength() != origLength * insText.length()) {
539:                    reportError("Text length was not restored");
540:                }
541:
542:                for (int i = 0; i < BIG_COMMAND_LOG_SIZE; i++) {
543:                    fTextPanel.redo();
544:                }
545:
546:                if (fTextPanel.getTextLength() != origLength
547:                        + BIG_COMMAND_LOG_SIZE) {
548:                    reportError("Text length was not restored after redo");
549:                }
550:
551:                if (fTextPanel.canRedo()) {
552:                    reportError("Should not be able to redo");
553:                }
554:
555:                fTextPanel.undo();
556:
557:                fTextPanel.setCommandLogSize(SMALL_COMMAND_LOG_SIZE);
558:
559:                if (fTextPanel.canRedo()) {
560:                    reportError("canRedo after setCommandLogSize(small)");
561:                }
562:
563:                for (int i = 0; i < SMALL_COMMAND_LOG_SIZE; i++) {
564:                    if (!fTextPanel.canUndo()) {
565:                        reportError("should be able to undo");
566:                    }
567:                    fTextPanel.undo();
568:                }
569:                if (fTextPanel.canUndo()) {
570:                    reportError("should not be able to undo after setCommandLogSize(small)");
571:                }
572:                if (!fTextPanel.canRedo()) {
573:                    reportError("why can't this redo???");
574:                }
575:                fTextPanel.redo();
576:
577:                fTextPanel.clearCommandLog();
578:
579:                if (fTextPanel.canUndo() || fTextPanel.canRedo()) {
580:                    reportError("Command log wasn't cleared");
581:                }
582:            }
583:
584:            /**
585:             * Test cut, copy, paste, undo, redo, clear, canUndo, canRedo.
586:             * Text must be editable to pass this test.
587:             */
588:            public void _testEditMenuOperations(Clipboard clipboard) {
589:
590:                if (clipboard != null) {
591:                    // test paste and undo / redo
592:                    Transferable tr = new StringSelection(STRING_CONTENT);
593:                    clipboard.setContents(tr, new ClipboardOwner() {
594:                        public void lostOwnership(Clipboard c, Transferable t) {
595:                        }
596:                    });
597:                    if (!fTextPanel.clipboardNotEmpty()) {
598:                        reportError("MTextPanel doesn't recognize string content.");
599:                    }
600:
601:                    fTextPanel.setCaretPosition(fTextPanel.getSelectionStart());
602:                    int oldLength = fTextPanel.getTextLength();
603:                    fTextPanel.paste();
604:                    if (fTextPanel.getTextLength() != oldLength
605:                            + STRING_CONTENT.length()) {
606:                        reportError("Text length is wrong after paste.");
607:                    }
608:
609:                    if (!fTextPanel.canUndo()) {
610:                        reportError("canUndo should be true");
611:                    }
612:                    fTextPanel.undo();
613:                    if (fTextPanel.getTextLength() != oldLength) {
614:                        reportError("Length is wrong after undo");
615:                    }
616:                    if (!fTextPanel.canRedo()) {
617:                        reportError("canRedo should be true");
618:                    }
619:                    fTextPanel.redo();
620:                    if (fTextPanel.getTextLength() != oldLength
621:                            + STRING_CONTENT.length()) {
622:                        reportError("Text length is wrong after redo.");
623:                    }
624:                }
625:
626:                int origLength = fTextPanel.getTextLength();
627:                fTextPanel.selectAll();
628:                fTextPanel.clear();
629:                if (fTextPanel.getTextLength() != 0) {
630:                    reportError("Length is nonzero after clear");
631:                }
632:                if (!fTextPanel.canUndo()) {
633:                    reportError("canUndo should be true");
634:                }
635:                fTextPanel.undo();
636:                if (fTextPanel.getTextLength() != origLength) {
637:                    reportError("Old text not restored");
638:                }
639:
640:                if (origLength > 0) {
641:
642:                    fTextPanel.select(0, 1);
643:                    fTextPanel.cut();
644:                    if (fTextPanel.getTextLength() != origLength - 1) {
645:                        reportError("Length wrong after cut");
646:                    }
647:                    fTextPanel.paste();
648:                    if (fTextPanel.getTextLength() != origLength) {
649:                        reportError("Length wrong after paste");
650:                    }
651:                    fTextPanel.select(0, origLength);
652:                    fTextPanel.copy();
653:                    fTextPanel.setCaretPosition(0);
654:                    fTextPanel.paste();
655:                    if (fTextPanel.getTextLength() != 2 * origLength) {
656:                        reportError("Length wrong after paste");
657:                    }
658:                    fTextPanel.undo();
659:                    if (fTextPanel.getTextLength() != origLength) {
660:                        reportError("Length wrong after undo");
661:                    }
662:                }
663:            }
664:
665:            private void setAndTestModFlag(final int depth, boolean modified) {
666:
667:                fTextPanel.setModified(modified);
668:                for (int i = 0; i < depth; i++) {
669:                    if (!fTextPanel.canUndo()) {
670:                        reportError("Panel cannot undo at valid depth.  Depth="
671:                                + i);
672:                    }
673:                    fTextPanel.undo();
674:                    fTextPanel.setModified(modified);
675:                }
676:
677:                // check that all mod flags are false:
678:                if (fTextPanel.isModified() != modified) {
679:                    reportError("isModified is not correct");
680:                }
681:
682:                for (int i = 0; i < depth; i++) {
683:                    fTextPanel.redo();
684:                    if (fTextPanel.isModified() != modified) {
685:                        reportError("isModified is not correct");
686:                    }
687:                }
688:            }
689:
690:            /**
691:             * Make <code>depth</code> modifications to the text in textfTextPanel.
692:             * Set the modified flag on each operation, and then retrieve its
693:             * value.  Finally, undo the modifications.
694:             */
695:            public void _testModFlag(final int depth) {
696:
697:                final int oldLength = fTextPanel.getTextLength();
698:
699:                for (int i = 0; i < depth; i++) {
700:                    fTextPanel.insert(MOD_TEXT, 0);
701:                }
702:
703:                setAndTestModFlag(depth, false);
704:                setAndTestModFlag(depth, true);
705:
706:                for (int i = 0; i < depth; i++) {
707:                    fTextPanel.undo();
708:                }
709:
710:                if (fTextPanel.getTextLength() != oldLength) {
711:                    reportError("Undo did not restore old text.");
712:                }
713:            }
714:
715:            void applyCharacterStyle() {
716:
717:                StyleModifier stMod = charMods[randInt(charMods.length)];
718:                fListener.refuseAll();
719:                fListener.expectEvent(TextPanelEvent.SELECTION_STYLES_CHANGED);
720:                if (fTextPanel.getSelectionStart() != fTextPanel
721:                        .getSelectionEnd()) {
722:                    fListener.expectEvent(TextPanelEvent.TEXT_CHANGED);
723:                    fListener
724:                            .allowEvent(TextPanelEvent.SELECTION_RANGE_CHANGED);
725:                }
726:                fListener.allowEvent(TextPanelEvent.UNDO_STATE_CHANGED);
727:                fTextPanel.modifyCharacterStyleOnSelection(stMod);
728:                fListener.assertNotExpectingEvents();
729:                fListener.allowAll();
730:            }
731:
732:            void applyParagraphStyle() {
733:
734:                fListener.refuseAll();
735:                fListener.expectEvent(TextPanelEvent.SELECTION_STYLES_CHANGED);
736:                fListener.expectEvent(TextPanelEvent.TEXT_CHANGED);
737:                fListener.allowEvent(TextPanelEvent.UNDO_STATE_CHANGED);
738:                fListener.allowEvent(TextPanelEvent.SELECTION_RANGE_CHANGED);
739:                StyleModifier stMod = paraMods[randInt(paraMods.length)];
740:                fTextPanel.modifyParagraphStyleOnSelection(stMod);
741:                fListener.assertNotExpectingEvents();
742:                fListener.allowAll();
743:            }
744:
745:            void applyKeyRemap() {
746:
747:                fListener.refuseAll();
748:                fListener.expectEvent(TextPanelEvent.KEYREMAP_CHANGED);
749:                int op = randInt(5);
750:                switch (op) {
751:
752:                case 0:
753:                    fTextPanel.setKeyRemap(KeyRemap.getIdentityRemap());
754:                    break;
755:
756:                case 1:
757:                    fTextPanel.setKeyRemap(KeyRemap.getArabicTransliteration());
758:                    break;
759:
760:                case 2:
761:                    fTextPanel.setKeyRemap(KeyRemap.getHebrewTransliteration());
762:                    break;
763:
764:                case 3:
765:                    fTextPanel.setKeyRemap(KeyRemap.getIsraelNikud());
766:                    break;
767:
768:                case 4:
769:                    //fTextPanel.setKeyRemap(KeyRemap.getThaiKetmanee());
770:                    fTextPanel.setKeyRemap(KeyRemap.getIsraelNikud());
771:                    break;
772:
773:                default:
774:                    reportError("Invalid operation!");
775:                }
776:                fListener.assertNotExpectingEvents();
777:                fListener.allowAll();
778:            }
779:
780:            void resizeFrame(Frame frame) {
781:
782:                fListener.refuseAll();
783:                fListener.allowEvent(TextPanelEvent.FORMAT_WIDTH_CHANGED);
784:                int wd = randInt(50, 1000);
785:                int ht = randInt(20, 800);
786:
787:                frame.setSize(wd, ht);
788:                fListener.allowAll();
789:            }
790:
791:            void selectText() {
792:
793:                int selStart = randInt(-10, fTextPanel.getTextLength());
794:                int selLimit = randInt(0, fTextPanel.getTextLength() + 10);
795:                _testSelection(SELECT, selStart, selLimit);
796:            }
797:
798:            void undoRedo() {
799:
800:                final int opCount = randInt(-10, 15);
801:
802:                for (int i = opCount; i <= 0; i++) {
803:                    fTextPanel.redo();
804:                }
805:                for (int i = 0; i < opCount; i++) {
806:                    fTextPanel.undo();
807:                }
808:            }
809:
810:            void typeKeys() {
811:
812:                final int keyCount = randInt(1, 100);
813:                TextPanel textPanel = (TextPanel) fTextPanel;
814:
815:                KeyEventForwarder forwarder = new KeyEventForwarder(textPanel);
816:
817:                fListener.refuseAll();
818:                fListener.allowEvent(TextPanelEvent.UNDO_STATE_CHANGED);
819:
820:                if (fTextPanel.getSelectionStart() != fTextPanel
821:                        .getSelectionEnd()) {
822:                    fListener
823:                            .expectEvent(TextPanelEvent.SELECTION_EMPTY_CHANGED);
824:                }
825:
826:                for (int i = 0; i < keyCount; i++) {
827:                    char typedChar = TYPED_CHARS[randInt(TYPED_CHARS.length)];
828:                    KeyEvent event = new KeyEvent(textPanel,
829:                            KeyEvent.KEY_TYPED, 0, 0, KeyEvent.VK_UNDEFINED,
830:                            typedChar);
831:                    if (typedChar == 8 || typedChar == 0x7f) {
832:                        fListener.allowEvent(TextPanelEvent.TEXT_CHANGED);
833:                        fListener
834:                                .allowEvent(TextPanelEvent.SELECTION_RANGE_CHANGED);
835:                    } else {
836:                        fListener.expectEvent(TextPanelEvent.TEXT_CHANGED);
837:                        fListener
838:                                .expectEvent(TextPanelEvent.SELECTION_RANGE_CHANGED);
839:                    }
840:                    forwarder.handleKeyEvent(event);
841:                    //try {
842:                    fListener.assertNotExpectingEvents(i, false);
843:                    //}
844:                    //catch(Error e) {
845:                    //    logMessage("i="+i+"; typedChar="+Integer.toHexString(typedChar));
846:                    //    throw e;
847:                    //}
848:                }
849:                fListener.allowAll();
850:            }
851:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.