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: }
|