001: /**
002: * Created on Dec 2, 2004
003: *
004: * @author karthikeyanr
005: *
006: */package test.wizard.ui;
007:
008: import java.awt.event.WindowListener;
009:
010: import javax.swing.*;
011:
012: import test.helper.ComponentTestCase;
013: import wizard.action.CancelWizardAction;
014: import wizard.ui.*;
015: import wizard.ui.AbstractWizard.*;
016:
017: public class WizardTest extends ComponentTestCase {
018: private static final int BACK_BUTTON_INDEX = 0;
019:
020: private static final int CANCEL_BUTTON_INDEX = 2;
021:
022: private static final int NEXT_BUTTON_INDEX = 1;
023:
024: protected JDialog createDialog() {
025: AbstractWizard wizard = new MockWizard(null, "Test Wizard");
026: return wizard;
027: }
028:
029: public void testBasicUI() {
030: assertNavigationTreeContents(3);
031: assertButton("Back", BackNavigationAction.class, false,
032: BACK_BUTTON_INDEX);
033: assertButton("Next", NextNavigationAction.class, false,
034: NEXT_BUTTON_INDEX);
035: assertButton("Cancel", CancelWizardAction.class, true,
036: CANCEL_BUTTON_INDEX);
037: }
038:
039: public void testEscapeAction() throws Exception {
040: assertEscapeKeyOnDialog();
041: }
042:
043: public void testFinishWizard() throws Exception {
044: gotoFinalPanel();
045: clickButton(NEXT_BUTTON_INDEX);
046: assertEquals(false, dialog.isVisible());
047: }
048:
049: public void testFinishWizardFailure() throws Exception {
050: gotoFinalPanel();
051: ((MockWizard) dialog).setShouldSucceed(false);
052: clickButton(NEXT_BUTTON_INDEX);
053: assertEquals(true, dialog.isVisible());
054: assertDummyPanel(1, "First");
055: assertButton("Next", NextNavigationAction.class, true,
056: NEXT_BUTTON_INDEX);
057: assertButton("Back", BackNavigationAction.class, false,
058: BACK_BUTTON_INDEX);
059: }
060:
061: public void testMultiPathNavigation() throws Exception {
062: assertFirstPanel(false, false);
063:
064: clickCheckBox(0);
065: assertFirstPanel(true, false);
066: clickButton(NEXT_BUTTON_INDEX);
067:
068: assertSecondPanel();
069: clickButton(NEXT_BUTTON_INDEX);
070:
071: assertLastPanel(false);
072:
073: clickCheckBox(0);
074: assertLastPanel(true);
075:
076: gotoFirstPanel();
077:
078: clickCheckBox(1);
079: assertFirstPanel(true, true);
080: clickButton(NEXT_BUTTON_INDEX);
081:
082: clickCheckBox(0);
083: assertLastPanel(true);
084: }
085:
086: public void testDialogWindowListner() throws Exception {
087: assertEquals(WindowConstants.DO_NOTHING_ON_CLOSE, dialog
088: .getDefaultCloseOperation());
089: assertEquals(1,
090: dialog.getListeners(WindowListener.class).length);
091: }
092:
093: private void assertFirstPanel(boolean isChecked,
094: boolean isMoveLastPanel) {
095: assertDummyPanel(1, "First");
096:
097: assertCheckBox("Check this to proceed", isChecked, true, 0);
098: assertCheckBox("Check this to go to Last Panel",
099: isMoveLastPanel, true, 1);
100: assertButton("Next", NextNavigationAction.class, isChecked,
101: NEXT_BUTTON_INDEX);
102: assertButton("Back", BackNavigationAction.class, false,
103: BACK_BUTTON_INDEX);
104: }
105:
106: private void assertDummyPanel(int cardIndex, String text) {
107: assertLabel("description for " + text, 0);
108: assertSelectedNavigationElement(cardIndex + ". " + text
109: + " Card", cardIndex);
110: }
111:
112: private void assertNavigationTreeContents(int itemCount) {
113: JTree tree = assertNavigationTree();
114: Object root = tree.getModel().getRoot();
115: assertEquals(itemCount, tree.getModel().getChildCount(root));
116: }
117:
118: private void assertSecondPanel() {
119: assertLabel("description for Second", 0);
120: assertSelectedNavigationElement("2. Second Card", 2);
121: assertButton("Back", BackNavigationAction.class, true,
122: BACK_BUTTON_INDEX);
123: assertButton("Next", NextNavigationAction.class, true,
124: NEXT_BUTTON_INDEX);
125: }
126:
127: private void assertLastPanel(boolean isChecked) {
128: assertDummyPanel(3, "Third");
129:
130: assertCheckBox("Check this to proceed", isChecked, true, 0);
131: assertButton("Finish", NextNavigationAction.class, isChecked,
132: NEXT_BUTTON_INDEX);
133: assertButton("Back", BackNavigationAction.class, true,
134: BACK_BUTTON_INDEX);
135: }
136:
137: private void assertSelectedNavigationElement(String text, int index) {
138: JTree tree = assertNavigationTree();
139: assertEquals(text, tree.getSelectionPath()
140: .getLastPathComponent().toString());
141: int selectedRow = tree.getSelectionRows()[0];
142: assertEquals(index, selectedRow);
143: assertTrue(tree.getVisibleRect().contains(
144: tree.getRowBounds(selectedRow)));
145: }
146:
147: private JTree assertNavigationTree() {
148: JTree tree = (JTree) assertComponent(NavigationTree.class, 0);
149: assertNotNull(tree);
150: assertTrue(tree.isExpanded(0));
151: assertFalse(tree.isEnabled());
152: assertEquals("Overview", tree.getModel().getRoot().toString());
153: assertFalse(tree.getShowsRootHandles());
154: assertEquals(NavigationRenderer.class, tree.getCellRenderer()
155: .getClass());
156: assertFalse(tree.isOpaque());
157: return tree;
158: }
159:
160: private void gotoFinalPanel() throws Exception {
161: assertFirstPanel(false, false);
162:
163: clickCheckBox(0);
164: assertFirstPanel(true, false);
165: clickButton(NEXT_BUTTON_INDEX);
166:
167: assertSecondPanel();
168: clickButton(NEXT_BUTTON_INDEX);
169:
170: assertLastPanel(false);
171:
172: clickCheckBox(0);
173: assertLastPanel(true);
174: }
175:
176: private void gotoFirstPanel() throws Exception {
177: clickCheckBox(0);
178: clickButton(BACK_BUTTON_INDEX);
179: clickButton(BACK_BUTTON_INDEX);
180: }
181: }
|