Last modified 17 years ago
Last modified on 09/17/08 13:54:48
How to test code with dialogs
If a dialog pops during automatic testing, this is a problem, because it will block the testing. Because of this, in the setUp and tearDown methods of some of the base test classes, the DialogManager is entering test mode.
Here is an example of testing the exit menu will ask for exit confirmation. Note that
* SystemTestBase calls TestingDialogManager.beginTesting() in setUp(); * If someone attempts to show unexpected dialog, IllegalStateException is thrown * If not all expected dialogs are shown during testing IllegalStateException is thrown. public void testExit_NoBooks2() { FileMenu.ExitItem exitItem = findMenu(FileMenu.ExitItem.class); TestingDialogManager.get().expect(ConfirmDialog.Input.class, ConfirmDialog.Response.NO); exitItem.clicked(); assertEquals(LiveState.READY, getApp().liveState().get()); TestingDialogManager.get().expect(ConfirmDialog.Input.class, ConfirmDialog.Response.YES); exitItem.clicked(); assertEquals(LiveState.DELETING, getApp().liveState().get()); }
And the same test, but with more detailed checking of the expectation
public void testExit_NoBooks1() { FileMenu.ExitItem exitItem = findMenu(FileMenu.ExitItem.class); TestingDialogManager.get().expect(new TestingDialogManager.Expected() { @Override public Object getResult() { return ConfirmDialog.Response.NO; } @Override public boolean like(DialogInput<?> input) { if (input instanceof ConfirmDialog.Input) { ConfirmDialog.Input confInput = (ConfirmDialog.Input) input; return confInput.getTitle().contains("Exit") && confInput.getOptions() == JOptionPane.YES_NO_OPTION; } return false; } }); exitItem.clicked(); assertEquals(LiveState.READY, getApp().liveState().get()); TestingDialogManager.get().expect(new TestingDialogManager.Expected() { @Override public Object getResult() { return ConfirmDialog.Response.YES; } @Override public boolean like(DialogInput<?> input) { if (input instanceof ConfirmDialog.Input) { ConfirmDialog.Input confInput = (ConfirmDialog.Input) input; return confInput.getTitle().contains("Exit") && confInput.getOptions() == JOptionPane.YES_NO_OPTION; } return false; } }); exitItem.clicked(); assertEquals(LiveState.DELETING, getApp().liveState().get()); }