| 1 | = How to test code with dialogs = |
| 2 | |
| 3 | 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. |
| 4 | |
| 5 | Here is an example of testing the exit menu will ask for exit confirmation. Note that |
| 6 | |
| 7 | {{{ |
| 8 | * SystemTestBase calls TestingDialogManager.beginTesting() in setUp(); |
| 9 | * If someone attempts to show unexpected dialog, IllegalStateException is thrown |
| 10 | * If not all expected dialogs are shown during testing IllegalStateException is thrown. |
| 11 | |
| 12 | public void testExit_NoBooks2() { |
| 13 | FileMenu.ExitItem exitItem = findMenu(FileMenu.ExitItem.class); |
| 14 | |
| 15 | TestingDialogManager.get().expect(ConfirmDialog.Input.class, |
| 16 | ConfirmDialog.Response.NO); |
| 17 | exitItem.clicked(); |
| 18 | |
| 19 | assertEquals(LiveState.READY, getApp().liveState().get()); |
| 20 | |
| 21 | TestingDialogManager.get().expect(ConfirmDialog.Input.class, |
| 22 | ConfirmDialog.Response.YES); |
| 23 | exitItem.clicked(); |
| 24 | assertEquals(LiveState.DELETING, getApp().liveState().get()); |
| 25 | } |
| 26 | }}} |
| 27 | |
| 28 | And the same test, but with more detailed checking of the expectation |
| 29 | {{{ |
| 30 | public void testExit_NoBooks1() { |
| 31 | FileMenu.ExitItem exitItem = findMenu(FileMenu.ExitItem.class); |
| 32 | |
| 33 | TestingDialogManager.get().expect(new TestingDialogManager.Expected() { |
| 34 | |
| 35 | @Override |
| 36 | public Object getResult() { |
| 37 | return ConfirmDialog.Response.NO; |
| 38 | } |
| 39 | |
| 40 | @Override |
| 41 | public boolean like(DialogInput<?> input) { |
| 42 | if (input instanceof ConfirmDialog.Input) { |
| 43 | ConfirmDialog.Input confInput = (ConfirmDialog.Input) input; |
| 44 | return confInput.getTitle().contains("Exit") |
| 45 | && confInput.getOptions() == JOptionPane.YES_NO_OPTION; |
| 46 | } |
| 47 | return false; |
| 48 | } |
| 49 | |
| 50 | }); |
| 51 | exitItem.clicked(); |
| 52 | assertEquals(LiveState.READY, getApp().liveState().get()); |
| 53 | |
| 54 | TestingDialogManager.get().expect(new TestingDialogManager.Expected() { |
| 55 | |
| 56 | @Override |
| 57 | public Object getResult() { |
| 58 | return ConfirmDialog.Response.YES; |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public boolean like(DialogInput<?> input) { |
| 63 | if (input instanceof ConfirmDialog.Input) { |
| 64 | ConfirmDialog.Input confInput = (ConfirmDialog.Input) input; |
| 65 | return confInput.getTitle().contains("Exit") |
| 66 | && confInput.getOptions() == JOptionPane.YES_NO_OPTION; |
| 67 | } |
| 68 | return false; |
| 69 | } |
| 70 | |
| 71 | }); |
| 72 | exitItem.clicked(); |
| 73 | assertEquals(LiveState.DELETING, getApp().liveState().get()); |
| 74 | } |
| 75 | }}} |