package DisplayProject;
import java.lang.Thread.State;
import javax.swing.JFrame;
import org.fest.swing.core.ComponentFinder;
import org.fest.swing.core.matcher.FrameMatcher;
import org.fest.swing.exception.ComponentLookupException;
import org.fest.swing.fixture.FrameFixture;
import Framework.EventManager;
import Framework.Task;
public class DisplayProjectTestUtils {
public static FrameFixture findFrameWithName(ComponentFinder finder, String frameName) {
FrameFixture result = null;
JFrame windowFrame = null;
FrameFixture frameFixture = null;
for (int i = 0; i < 1000 && result == null; i++) {
try {
if (windowFrame == null) {
windowFrame = (JFrame)finder.find(FrameMatcher.withName(frameName));
}
if (frameFixture == null) {
frameFixture = new FrameFixture(windowFrame);
}
if (!frameFixture.target.isVisible()) {
Task.delay(50);
continue;
}
result = frameFixture;
}
catch (ComponentLookupException ignoreAndTryAgain) {
}
Task.delay(50);
}
if (result == null) {
finder.find(FrameMatcher.withName(frameName));
}
return result;
}
/**
* Wait for the thread passed to be idle, that is waiting for events from the event manager. To satisfy this,
* the thread must be WAITING and there can be no pending events on the event manager queue. The EDT is also
* waited for (until it is idle) although due to the asynchronous nature of threads, there is no guarantee
* that either the passed thread nor the EDT is idle when this method returns.
* @param mainThread
*/
public static void waitForIdle(Thread mainThread) {
UIutils.waitForEDTToBeIdle();
outerLoop:
while (true) {
if (!mainThread.isAlive()) {
break;
}
else if (!EventManager.hasPendingEvents(mainThread) && mainThread.getState() == State.WAITING) {
// The thread is waiting and it could be waiting for an event, so get the stack trace
// and make sure it's waiting in the event manager.
StackTraceElement[] elements = mainThread.getStackTrace();
for (StackTraceElement element : elements) {
if (element.getClassName().equals("java.lang.Object")) {
continue;
}
else if (element.getClassName().indexOf("EventManager$ThreadData") >= 0) {
// We're waiting inside the event manager, break out
break outerLoop;
}
else {
// The top of the call stack is neither the event manager nor object, so we're in
// the wrong spot. We must break out now, otherwise we run the risk of the event
// manager calling an even handler which gets blocked on a monitor. This would
// satisfy all the other criteria except the depth within the call stack.
continue outerLoop;
}
}
}
Task.delay(50);
}
UIutils.waitForEDTToBeIdle();
}
}