01: package org.uispec4j;
02:
03: import junit.framework.AssertionFailedError;
04: import org.uispec4j.assertion.Assertion;
05: import org.uispec4j.assertion.UISpecAssert;
06: import org.uispec4j.utils.AssertionFailureNotDetectedError;
07: import org.uispec4j.utils.Chrono;
08: import org.uispec4j.utils.DummyActionListener;
09: import org.uispec4j.utils.Utils;
10:
11: public abstract class ButtonTestCase extends UIComponentTestCase {
12:
13: protected abstract AbstractButton getButton();
14:
15: protected abstract javax.swing.AbstractButton getSwingButton();
16:
17: protected UIComponent createComponent() {
18: return getButton();
19: }
20:
21: public void testEnableDisable() throws Exception {
22: checkEnabled(true);
23: checkEnabled(false);
24: }
25:
26: private void checkEnabled(boolean enabled) {
27: getSwingButton().setEnabled(enabled);
28: Assertion enabledAssertion = getButton().isEnabled();
29: assertTrue((enabled) ? enabledAssertion : not(enabledAssertion));
30: }
31:
32: public void testCheckText() throws Exception {
33: getSwingButton().setText("text");
34: assertTrue(getButton().textEquals("text"));
35: try {
36: assertTrue(getButton().textEquals("error"));
37: throw new AssertionFailureNotDetectedError();
38: } catch (AssertionFailedError e) {
39: }
40: }
41:
42: public void testCheckTextTrimsTheActualButtonText()
43: throws Exception {
44: getSwingButton().setText(" text ");
45: assertTrue(getButton().textEquals("text"));
46: }
47:
48: public void testActivateIsRejectedIfTheButtonIsDisabled()
49: throws Exception {
50: getSwingButton().setEnabled(false);
51: try {
52: getButton().click();
53: throw new AssertionFailureNotDetectedError();
54: } catch (AssertionFailedError e) {
55: assertEquals(
56: "The button is not enabled, it cannot be activated",
57: e.getMessage());
58: }
59: }
60:
61: public void testClick() throws Exception {
62: DummyActionListener listener = new DummyActionListener();
63: getSwingButton().addActionListener(listener);
64: getButton().click();
65: assertEquals(1, listener.getCallCount());
66: }
67:
68: public void testTriggerClick() throws Exception {
69: DummyActionListener listener = new DummyActionListener();
70: getSwingButton().addActionListener(listener);
71: getButton().triggerClick().run();
72: assertEquals(1, listener.getCallCount());
73: }
74:
75: public void testClickTakesLessTimeThanWithDefaultSwingCalls()
76: throws Exception {
77: Chrono chrono = Chrono.start();
78: getButton().click();
79: chrono.assertElapsedTimeLessThan(30);
80: }
81:
82: public void testWaitForEnabledState() throws Exception {
83: AbstractButton button = getButton();
84: final javax.swing.AbstractButton swingButton = (javax.swing.AbstractButton) button
85: .getAwtComponent();
86: swingButton.setEnabled(false);
87: Thread thread = new Thread(new Runnable() {
88: public void run() {
89: Utils.sleep(10);
90: swingButton.setEnabled(true);
91: }
92: });
93: thread.start();
94: assertFalse(button.isEnabled());
95: UISpecAssert.waitUntil(button.isEnabled(), 30);
96: assertTrue(button.isEnabled());
97: }
98: }
|