Source Code Cross Referenced for SwingWaitTestCase.java in  » Apache-Harmony-Java-SE » javax-package » javax » swing » 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 
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;
021:
022:        import java.awt.Component;
023:        import java.awt.EventQueue;
024:        import java.awt.IllegalComponentStateException;
025:        import java.awt.Point;
026:        import java.awt.Window;
027:        import java.lang.reflect.InvocationTargetException;
028:
029:        /**
030:         * JUnit test case class which calls <code>setUp</code>, <code>tearDown</code>
031:         * and tests (<code>testXXX</code> methods) on event-dispatch thread, and
032:         * waits (stops) after calling <code>setUp</code> until <code>component</code>
033:         * is displayed on screen.
034:         *
035:         */
036:        public abstract class SwingWaitTestCase extends BasicSwingTestCase {
037:            private static final int MAX_WAIT_TIME = 5000;
038:
039:            /**
040:             * Default constructor.
041:             */
042:            public SwingWaitTestCase() {
043:                super ();
044:            }
045:
046:            /**
047:             * Parametricized constructor.
048:             *
049:             * @param name test name (actually test method name to be run)
050:             */
051:            public SwingWaitTestCase(final String name) {
052:                super (name);
053:            }
054:
055:            /**
056:             * Component to wait on. This component should be drawn correctly for
057:             * the test to run correctly.
058:             */
059:            protected Component component;
060:
061:            /**
062:             * Exception thrown during test execution if any.
063:             */
064:            protected Throwable exception;
065:
066:            private static boolean bWasIllegalComponentStateException;
067:
068:            private static Point next;
069:
070:            /**
071:             * This method will be called after setUp and waiting and before
072:             * test running.
073:             */
074:            public void init() {
075:            }
076:
077:            void internalRunBare() throws Throwable {
078:                // Call setUp on event-dispatch thread
079:                EventQueue.invokeAndWait(new Runnable() {
080:                    public void run() {
081:                        try {
082:                            setUp();
083:                        } catch (Throwable e) {
084:                            exception = e;
085:                        }
086:                    }
087:                });
088:                // Wait for component to be realized (displayed) if any
089:                if (component != null) {
090:                    isRealized(component);
091:                }
092:                // Calls init method to perform some actions after waiting and before
093:                // test running
094:                EventQueue.invokeAndWait(new Runnable() {
095:                    public void run() {
096:                        try {
097:                            init();
098:                            runTest();
099:                        } catch (Throwable e) {
100:                            if (exception == null) {
101:                                exception = e;
102:                            }
103:                        }
104:                    }
105:                });
106:                EventQueue.invokeAndWait(new Runnable() {
107:                    public void run() {
108:                        try {
109:                            tearDown();
110:                        } catch (Throwable e) {
111:                            if (exception == null) {
112:                                exception = e;
113:                            }
114:                        }
115:                    }
116:                });
117:            }
118:
119:            @Override
120:            public void runBare() throws Throwable {
121:                internalRunBare();
122:                if (exception != null) {
123:                    rethrow(exception);
124:                }
125:            }
126:
127:            /**
128:             * Method delays current thread while Component isn't realized.
129:             *
130:             * TODO:Replace Component method invokes, as soon as new awtfunction for
131:             * Component is written (to check, that component is realized by single
132:             * method).
133:             */
134:            public static void isRealized(final Component c) {
135:                Point prev = null;
136:                int counter = 50;
137:                do {
138:                    bWasIllegalComponentStateException = false;
139:                    prev = next;
140:                    next = null;
141:                    try {
142:                        // Get state from the component
143:                        SwingUtilities.invokeAndWait(new Runnable() {
144:                            public void run() {
145:                                try {
146:                                    next = c.getLocationOnScreen();
147:                                } catch (IllegalComponentStateException e) {
148:                                    bWasIllegalComponentStateException = true;
149:                                }
150:                            }
151:                        });
152:                        Thread.sleep(100);
153:                    } catch (IllegalArgumentException e) {
154:                    } catch (InterruptedException e) {
155:                    } catch (InvocationTargetException e) {
156:                    }
157:                } while (!c.isDisplayable() || !c.isVisible()
158:                        || bWasIllegalComponentStateException || (next == null)
159:                        || !next.equals(prev) || (counter-- <= 0));
160:                if (bWasIllegalComponentStateException) {
161:                    System.err.println("bWasIllegalComponentStateException");
162:                }
163:                assertTrue("frame showing timeout", counter >= 0);
164:            }
165:
166:            /*
167:             * Requests focus for the component and waits until it really
168:             * becomes focused.
169:             */
170:            public static void requestFocusInWindowForComponent(
171:                    final Component c) throws InterruptedException,
172:                    InvocationTargetException {
173:                final Component comp = c;
174:                final Window w = SwingUtilities.getWindowAncestor(comp);
175:                if (w == null) {
176:                    fail("no window is provided");
177:                    return;
178:                }
179:                long startTime = System.currentTimeMillis();
180:                while (!comp.isShowing()
181:                        && (System.currentTimeMillis() - startTime) < MAX_WAIT_TIME) {
182:                    Thread.sleep(10);
183:                }
184:                if (!comp.isShowing()) {
185:                    fail("component is not showing");
186:                    return;
187:                }
188:                startTime = System.currentTimeMillis();
189:                while (!w.isFocused()
190:                        && (System.currentTimeMillis() - startTime) < MAX_WAIT_TIME) {
191:                    Thread.sleep(10);
192:                }
193:                SwingUtilities.invokeAndWait(new Runnable() {
194:                    public void run() {
195:                        final boolean result = comp.requestFocusInWindow();
196:                        assertTrue("focus can be gained", result
197:                                || comp.isFocusOwner());
198:                    }
199:                });
200:                startTime = System.currentTimeMillis();
201:                while (!comp.isFocusOwner()
202:                        && (System.currentTimeMillis() - startTime) < MAX_WAIT_TIME) {
203:                    Thread.sleep(10);
204:                }
205:                Thread.sleep(10);
206:            }
207:        }
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.