Skip to content

Commit

Permalink
End of episode 180
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesshore committed Apr 4, 2012
1 parent 10f7afb commit 5cce489
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 25 deletions.
2 changes: 0 additions & 2 deletions scratchpad.txt
@@ -1,5 +1,3 @@
Testing JOptionPane: see Esko Luontola's comment, episode 178.

Resolve "ignored" test

Move save file data clump into a "configuration" object?
Expand Down
51 changes: 28 additions & 23 deletions src/com/jamesshore/finances/ui/_ApplicationFrameTest.java
Expand Up @@ -123,9 +123,11 @@ public void newMenuItemShouldCreateANewWindow() {
}

@Test
public void closeMenuItemShouldCloseTheWindow() {
public void closeMenuItemShouldCloseTheWindow() throws Throwable {
frame.setVisible(true);
assertTrue("before disposable, frame is displayable", frame.isDisplayable());
// TODO: intermittent test failure where frame is not being disposed; tried invokeAndWait, did not work
// try next: run on event handler thread?
closeMenuItem.doClick();
assertTrue("frame should have been disposed", !frame.isDisplayable());
}
Expand All @@ -141,9 +143,9 @@ public void run() {
}
});

assertEventuallyTrue("Save As dialog should be visible", 1000, new AsynchronousAssertion() {
waitFor("Save As dialog should be visible", 1000, new WaitForCheck() {
@Override
public boolean assertTrue() {
public boolean waitConditionFulfilled() {
return saveAsDialog.isVisible();
}
});
Expand All @@ -170,13 +172,29 @@ public void saveAsDialogShouldDoNothingWhenCancelButtonPushed() {

@Test
public void saveAsDialogShouldHandleSaveExceptionsGracefully() {
// TODO: Clean up this method
causeSaveException(new IOException("generic exception"));

waitFor("Warning dialog should be visible", 1000, new WaitForCheck() {
@Override
public boolean waitConditionFulfilled() {
Dialog dialog = warningDialogOrNullIfNotFound();
return dialog != null && dialog.isVisible();
}
});
JDialog dialogWindow = (JDialog)warningDialogOrNullIfNotFound();
JOptionPane dialogPane = (JOptionPane)dialogWindow.getContentPane().getComponent(0);
assertEquals("Warning dialog parent", frame, dialogWindow.getParent());
assertEquals("Warning dialog title", "Save File", dialogWindow.getTitle());
assertEquals("Warning dialog message", "Could not save file: generic exception", dialogPane.getMessage());
assertEquals("Warning dialog type should be 'warning'", JOptionPane.WARNING_MESSAGE, dialogPane.getMessageType());
}

private void causeSaveException(final IOException exception) {
// Set up frame to throw exception on save
class ExceptionThrowingApplicationModel extends __ApplicationModelSpy {
@Override
public void save(File saveFile) throws IOException {
throw new IOException("generic exception");
throw exception;
}
}
frame = new ApplicationFrame(new ExceptionThrowingApplicationModel());
Expand All @@ -191,20 +209,6 @@ public void run() {
}
});

// Assert that error dialog is visible and has correct error message
assertEventuallyTrue("Warning dialog should be visible", 1000, new AsynchronousAssertion() {
@Override
public boolean assertTrue() {
Dialog dialog = warningDialogOrNullIfNotFound();
return dialog != null && dialog.isVisible();
}
});
JDialog dialogWindow = (JDialog)warningDialogOrNullIfNotFound();
JOptionPane dialogPane = (JOptionPane)dialogWindow.getContentPane().getComponent(0);
assertEquals("Warning dialog parent", frame, dialogWindow.getParent());
assertEquals("Warning dialog title", "Save File", dialogWindow.getTitle());
assertEquals("Warning dialog message", "Could not save file: generic exception", dialogPane.getMessage());
assertEquals("Warning dialog type should be 'warning'", JOptionPane.WARNING_MESSAGE, dialogPane.getMessageType());
}

private FileDialog saveAsDialog() {
Expand All @@ -217,13 +221,14 @@ private Dialog warningDialogOrNullIfNotFound() {
return (Dialog)childWindows[1];
}

abstract class AsynchronousAssertion {
abstract boolean assertTrue();
// TODO: rename me to match 'waitFor' style?
abstract class WaitForCheck {
abstract boolean waitConditionFulfilled();
}

private void assertEventuallyTrue(String message, int timeout, AsynchronousAssertion check) {
private void waitFor(String message, int timeout, WaitForCheck check) {
long startTime = new Date().getTime();
while (!check.assertTrue()) {
while (!check.waitConditionFulfilled()) {
Thread.yield();
long elapsedMilliseconds = new Date().getTime() - startTime;
if (elapsedMilliseconds > timeout) fail(message + " within " + timeout + " milliseconds");
Expand Down

0 comments on commit 5cce489

Please sign in to comment.