001: package example;
002:
003: import java.awt.Component;
004:
005: import javax.swing.JButton;
006: import javax.swing.JFrame;
007: import javax.swing.JLabel;
008: import javax.swing.JMenuItem;
009: import javax.swing.JTextField;
010:
011: import junit.extensions.abbot.ComponentTestFixture;
012: import junit.extensions.abbot.TestHelper;
013: import abbot.finder.matchers.ClassMatcher;
014: import abbot.finder.matchers.JMenuItemMatcher;
015: import abbot.tester.JButtonTester;
016: import abbot.tester.JTextComponentTester;
017:
018: /**
019: * Demonstrates testing a simple Swing UI ({@link CelsiusConverter}) with
020: * Abbot.
021: *
022: * @author Satadip Dutta (original)
023: * @author Tom Roche (ported to new-style Abbot, added more tests)
024: * @version $Id: CelsiusConverterTest.java 1597 2005-05-06 23:27:16Z tlroche $
025: */
026: public class CelsiusConverterTest extends ComponentTestFixture {
027:
028: // for precision testing
029: final static int DEFAULT_PRECISION = 0;
030: final static int PRECISION2 = 2;
031: final static int PRECISION3 = 3;
032:
033: private CelsiusConverter cc;
034: private JTextField tempCelsius;
035: private JButton convertButton;
036: private JLabel outputLabel;
037:
038: private JTextComponentTester tt;
039: private JButtonTester bt;
040:
041: /** For older versions of JUnit. */
042: public CelsiusConverterTest(String name) {
043: super (name);
044: }
045:
046: protected void setUp() throws Exception {
047: cc = new CelsiusConverter();
048: JFrame frame = new JFrame();
049: cc.enframe(frame);
050: // Display at the current frame's desired size (avoids packing)
051: showWindow(frame, null, false);
052:
053: // only one JTextField in our UI, so we can just class-match
054: tempCelsius = (JTextField) getFinder().find(
055: new ClassMatcher(JTextField.class));
056: // ditto for the JButton
057: convertButton = (JButton) getFinder().find(
058: new ClassMatcher(JButton.class));
059: // But there's 2 JLabel's in our UI, so we need to add more
060: // information
061: outputLabel = (JLabel) getFinder().find(
062: new ClassMatcher(JLabel.class) {
063: public boolean matches(Component c) {
064: String text = CelsiusConverter
065: .lookupString("output.label.text");
066: return super .matches(c)
067: && ((JLabel) c).getText().equals(text);
068: }
069: });
070:
071: tt = new JTextComponentTester();
072: bt = new JButtonTester();
073: }
074:
075: public void testNegativeNumber() throws Exception {
076:
077: tt.actionEnterText(tempCelsius, "-45"); //$NON-NLS-1$
078: bt.actionClick(convertButton);
079: assertEquals(CelsiusConverter.fahrenheitOutput(-49,
080: DEFAULT_PRECISION), outputLabel.getText());
081: }
082:
083: public void testBadInput() throws Exception {
084: // get default text
085: String originalText = outputLabel.getText();
086: // nothing should change if the input is not parseable as a double
087: tt.actionEnterText(tempCelsius, " HELLO "); //$NON-NLS-1$
088: bt.actionClick(convertButton);
089: assertTrue("Output changed for bad input", outputLabel
090: .getText().equals(originalText)); //$NON-NLS-1$
091: }
092:
093: public void testChangePrecision() throws Exception {
094: JMenuItem item2 = (JMenuItem) getFinder().find(
095: new JMenuItemMatcher(String.valueOf(PRECISION2))); //$NON-NLS-1$
096:
097: // the output should update to reflect a higher precision after making
098: // a change, even with no new input
099:
100: // initial precision is 0
101: tt.actionEnterText(tempCelsius, "25.23"); //$NON-NLS-1$
102: bt.actionClick(convertButton);
103:
104: // now update precision and make sure output fields update too
105: tt.actionSelectMenuItem(item2);
106:
107: assertEquals(
108: "Failed to reflect change in precision", //$NON-NLS-1$
109: CelsiusConverter.fahrenheitOutput(77.41, PRECISION2),
110: outputLabel.getText());
111: }
112:
113: public void testHighPrecision() throws Exception {
114: JMenuItem item3 = (JMenuItem) getFinder().find(
115: new JMenuItemMatcher(String.valueOf(PRECISION3))); //$NON-NLS-1$
116: tt.actionSelectMenuItem(item3);
117: tt.actionEnterText(tempCelsius, "-45.543"); //$NON-NLS-1$
118: bt.actionClick(convertButton);
119:
120: assertEquals(
121: "Failed to show answer with proper precision", //$NON-NLS-1$
122: CelsiusConverter.fahrenheitOutput(-49.977, PRECISION3),
123: outputLabel.getText());
124: }
125:
126: public static void main(String[] args) {
127: /*
128: junit.textui.TestRunner.main(new String[] {
129: CelsiusConverterTest.class.getName()
130: });
131: */
132: TestHelper.runTests(args, CelsiusConverterTest.class);
133: }
134:
135: }
|