001: /**
002: * Created on Dec 2, 2004
003: *
004: * @author karthikeyanr
005: *
006: */package test.helper;
007:
008: import java.awt.*;
009: import java.awt.event.KeyEvent;
010: import java.util.List;
011:
012: import javax.swing.*;
013:
014: import junit.extensions.jfcunit.*;
015: import junit.extensions.jfcunit.eventdata.MouseEventData;
016: import junit.extensions.jfcunit.finder.*;
017:
018: public class ComponentTestCase extends JFCTestCase {
019: private static ClassFinder classFinder = new ClassFinder();
020:
021: protected JDialog dialog;
022:
023: private Thread thread;
024:
025: public ComponentTestCase() {
026: setHelper(new JFCTestHelper());
027: }
028:
029: protected JDialog createDialog() {
030: return null;
031: }
032:
033: protected void setUp() throws Exception {
034: WindowMonitor.start();
035: dialog = createDialog();
036: thread = new Thread(new Runnable() {
037: public void run() {
038: dialog.setVisible(true);
039: }
040: });
041: thread.start();
042: super .setUp();
043: sleep(200);
044: }
045:
046: protected void tearDown() {
047: if (thread.isAlive())
048: thread.interrupt();
049:
050: thread = null;
051: dialog.dispose();
052: dialog = null;
053: }
054:
055: protected static Component findComponent(Container container,
056: Class componentClass, int index) {
057: classFinder.setClass(componentClass);
058: return classFinder.find(container, index);
059: }
060:
061: protected Component assertComponent(Class componentClass, int index) {
062: Component component = findComponent(dialog, componentClass,
063: index);
064: assertNotNull(component);
065: return component;
066: }
067:
068: protected JTextField assertTextField(String value,
069: boolean isEditable, int index) {
070: JTextField textField = (JTextField) assertComponent(
071: JTextField.class, index);
072:
073: assertEquals(isEditable, textField.isEditable());
074: assertEquals(value, textField.getText());
075: return textField;
076: }
077:
078: protected JCheckBox assertCheckBoxState(boolean isSelected,
079: boolean isEnabled, int index) {
080: JCheckBox checkBox = (JCheckBox) assertComponent(
081: JCheckBox.class, index);
082: assertEquals(isSelected, checkBox.isSelected());
083: assertEquals(isEnabled, checkBox.isEnabled());
084: return checkBox;
085: }
086:
087: protected void clickCheckBox(int index) throws Exception {
088: JCheckBox checkbox = (JCheckBox) assertComponent(
089: JCheckBox.class, index);
090: getHelper().enterClickAndLeave(
091: new MouseEventData(this , checkbox, 1));
092: }
093:
094: protected JCheckBox assertCheckBox(String text, boolean isSelected,
095: boolean isEnabled, int index) {
096: JCheckBox checkBox = assertCheckBoxState(isSelected, isEnabled,
097: index);
098: assertEquals(text, checkBox.getText());
099: return checkBox;
100: }
101:
102: protected void click(Class className, int count, int index)
103: throws Exception {
104: Component component = findComponent(dialog, className, index);
105: assertNotNull(component);
106: getHelper().enterClickAndLeave(
107: new MouseEventData(this , component, count));
108: }
109:
110: protected void clickButton(int index) throws Exception {
111: JButton button = (JButton) findComponent(dialog, JButton.class,
112: index);
113: assertNotNull(button);
114: assertButtonState(true, index);
115: getHelper().enterClickAndLeave(
116: new MouseEventData(this , button, 1));
117: }
118:
119: protected JLabel assertLabel(String labelText, int index) {
120: JLabel label = (JLabel) assertComponent(JLabel.class, index);
121: assertEquals(labelText, label.getText());
122: return label;
123: }
124:
125: protected JButton assertButton(String text, Class actionClass,
126: boolean isEnabled, int index) {
127: JButton button = (JButton) assertComponent(JButton.class, index);
128: assertNotNull(button);
129:
130: if (text != null) {
131: assertEquals(text, button.getText());
132: assertNull(button.getToolTipText());
133: }
134:
135: assertEquals(actionClass, button.getAction().getClass());
136: assertEquals(button.isEnabled(), isEnabled);
137: return button;
138: }
139:
140: protected void assertButtonState(boolean isEnabled, int buttonIndex) {
141: JButton button = (JButton) assertComponent(JButton.class,
142: buttonIndex);
143: assertEquals(isEnabled, button.isEnabled());
144: }
145:
146: protected void assertConfirmationDialog(String message)
147: throws Exception {
148: List dialogList = getDialogList();
149: JDialog infoDialog = (JDialog) dialogList
150: .get(dialogList.size() - 1);
151:
152: assertEquals(message, getHelper().getMessageFromJDialog(
153: infoDialog));
154:
155: JButton button = (JButton) findComponent(infoDialog,
156: JButton.class, 0);
157: assertNotNull(button);
158: getHelper()
159: .enterClickAndLeave(new MouseEventData(this , button));
160: }
161:
162: public List getDialogList() {
163: DialogFinder finder = new DialogFinder(null);
164: finder.setWait(0);
165: List dialogList = finder.findAll(dialog);
166: return dialogList;
167: }
168:
169: protected void assertEscapeKeyOnDialog() throws AWTException {
170: Robot robot = new Robot();
171: robot.keyPress(KeyEvent.VK_ESCAPE);
172: robot.keyRelease(KeyEvent.VK_ESCAPE);
173: sleep(1);
174: assertFalse(dialog.isVisible());
175: }
176:
177: protected static class ClassFinder extends Finder {
178: private Class componentClass;
179:
180: public ClassFinder() {
181: setWait(0);
182: }
183:
184: public void setClass(Class componentClass) {
185: this .componentClass = componentClass;
186: }
187:
188: public boolean testComponent(Component comp) {
189: return (isValidForProcessing(comp, componentClass) && comp
190: .getClass() == componentClass);
191: }
192: }
193: }
|