};
KeyboardFocusManager.getCurrentKeyboardFocusManager().
addPropertyChangeListener("activeWindow", activeWindowListener);
// Check that a simple short task is correctly executed with no exception
// and doesn't create any visible dialog at screen
new ThreadedTaskController(shortTask, "Message", noExceptionHandler, preferences, viewFactory).executeTask(null);
shortTaskLatch.await(1000, TimeUnit.MILLISECONDS);
assertEquals("Simple task wasn't executed", 0, shortTaskLatch.getCount());
KeyboardFocusManager.getCurrentKeyboardFocusManager().
removePropertyChangeListener("activeWindow", activeWindowListener);
// 2. Create a longer task
final CountDownLatch longTaskLatch = new CountDownLatch(2);
Callable<Void> longTask = new Callable<Void>() {
public Void call() throws Exception {
Thread.sleep(1000);
longTaskLatch.countDown();
return null;
}
};
// Add a listener that counts down latch once a waiting dialog is displayed
activeWindowListener = new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent ev) {
longTaskLatch.countDown();
}
};
KeyboardFocusManager.getCurrentKeyboardFocusManager().
addPropertyChangeListener("activeWindow", activeWindowListener);
// Check that a long task creates a visible dialog at screen
new ThreadedTaskController(longTask, "Message", noExceptionHandler, preferences, viewFactory).executeTask(null);
longTaskLatch.await(1500, TimeUnit.MILLISECONDS);
assertEquals("Long task wasn't executed with a waiting dialog", 0, longTaskLatch.getCount());
KeyboardFocusManager.getCurrentKeyboardFocusManager().
removePropertyChangeListener("activeWindow", activeWindowListener);
// 3. Create a long task that we will cancel
final CountDownLatch cancelledTaskLatch = new CountDownLatch(1);
Callable<Void> cancelledTask = new Callable<Void>() {
public Void call() throws Exception {
try {
Thread.sleep(1000);
} catch (InterruptedException ex) {
cancelledTaskLatch.countDown();
}
return null;
}
};
// Add a listener that closes the waiting dialog
activeWindowListener = new PropertyChangeListener() {
public void propertyChange(PropertyChangeEvent ev) {
final Window activeWindow = KeyboardFocusManager.getCurrentKeyboardFocusManager().getActiveWindow();
if (activeWindow != null) {
try {
((JButton)TestUtilities.findComponent(activeWindow, JButton.class)).doClick();
} catch (ComponentSearchException ex) {
fail("No button in waiting dialog");
}
}
}
};
KeyboardFocusManager.getCurrentKeyboardFocusManager().
addPropertyChangeListener("activeWindow", activeWindowListener);
// Check that a long task creates a visible dialog at screen
new ThreadedTaskController(cancelledTask, "Message", noExceptionHandler, preferences, viewFactory).executeTask(null);
cancelledTaskLatch.await(1500, TimeUnit.MILLISECONDS);
assertEquals("Task wasn't cancelled", 0, cancelledTaskLatch.getCount());
KeyboardFocusManager.getCurrentKeyboardFocusManager().
removePropertyChangeListener("activeWindow", activeWindowListener);
// 4. Create a task that fails
final CountDownLatch failingTaskLatch = new CountDownLatch(2);
Callable<Void> failingTask = new Callable<Void>() {
public Void call() throws Exception {
failingTaskLatch.countDown();
throw new Exception();
}
};
// Create an exception handler that counts down latch when it's called
ThreadedTaskController.ExceptionHandler exceptionHandler =
new ThreadedTaskController.ExceptionHandler() {
public void handleException(Exception ex) {
failingTaskLatch.countDown();
}
};
// Check that a long task creates a visible dialog at screen
new ThreadedTaskController(failingTask, "Message", exceptionHandler, preferences, viewFactory).executeTask(null);
failingTaskLatch.await(1000, TimeUnit.MILLISECONDS);
assertEquals("Exception in task wasn't handled", 0, failingTaskLatch.getCount());
}