001: package org.uispec4j.interception;
002:
003: import junit.framework.AssertionFailedError;
004: import org.uispec4j.UISpec4J;
005: import org.uispec4j.interception.toolkit.UISpecDisplay;
006: import org.uispec4j.utils.AssertionFailureNotDetectedError;
007: import org.uispec4j.utils.UnitTestCase;
008: import org.uispec4j.xml.EventLogger;
009:
010: import javax.swing.*;
011: import java.awt.event.ActionEvent;
012: import java.awt.event.ActionListener;
013:
014: /**
015: * TestCase which automatically enables UISpec4J's interception mechanism.
016: * <p/>
017: * This test case is useful when the default toolkit has already been initialized
018: * before an UISpec4J test was run.
019: * </p>
020: */
021: public abstract class InterceptionTestCase extends UnitTestCase {
022: protected EventLogger logger;
023:
024: protected void setUp() throws Exception {
025: super .setUp();
026: UISpecDisplay.instance().reset();
027: logger = new EventLogger();
028: UISpec4J.setWindowInterceptionTimeLimit(300);
029: }
030:
031: protected void tearDown() throws Exception {
032: super .tearDown();
033: assertEquals(0, UISpecDisplay.instance().getHandlerCount());
034: UISpecDisplay.instance().rethrowIfNeeded();
035: }
036:
037: public static JDialog createModalDialog(String title) {
038: return new JDialog(((JFrame) null), title, true);
039: }
040:
041: protected static void checkAssertionFailedError(
042: WindowInterceptor interceptor, String errorMessage) {
043: try {
044: interceptor.run();
045: throw new AssertionFailureNotDetectedError();
046: } catch (AssertionFailedError e) {
047: assertEquals(errorMessage, e.getMessage());
048: } catch (InterceptionError e) {
049: if (!e.getMessage().equals(errorMessage)) {
050: throw new InterceptionError(
051: "Unexpected error (expected '" + errorMessage
052: + "'", e);
053: }
054: }
055: }
056:
057: public static void createAndShowModalDialog(String title) {
058: createModalDialog(title).setVisible(true);
059: }
060:
061: protected void addLoggerButton(final RootPaneContainer container,
062: final String buttonLabel) {
063: JButton button = new JButton(buttonLabel);
064: button.addActionListener(new ActionListener() {
065: public void actionPerformed(ActionEvent e) {
066: logger.log("click").add("button", buttonLabel);
067: }
068: });
069: container.getContentPane().add(button);
070: }
071:
072: protected void addHideButton(final JDialog dialog,
073: final String buttonLabel) {
074: JButton button = new JButton(new AbstractAction(buttonLabel) {
075: public void actionPerformed(ActionEvent e) {
076: logger.log("click").add("button", buttonLabel);
077: dialog.setVisible(false);
078: }
079: });
080: dialog.getContentPane().add(button);
081: }
082:
083: protected void addShowDialogButton(
084: final RootPaneContainer container,
085: final String buttonLabel, final JDialog dialogToShow) {
086: JButton button = new JButton(new AbstractAction(buttonLabel) {
087: public void actionPerformed(ActionEvent e) {
088: logger.log("click").add("button", buttonLabel);
089: dialogToShow.setVisible(true);
090: }
091: });
092: container.getContentPane().add(button);
093: }
094:
095: protected void addShowDialogAndCloseButton(final JDialog container,
096: final String buttonLabel, final JDialog dialogToShow) {
097: JButton button = new JButton(new AbstractAction(buttonLabel) {
098: public void actionPerformed(ActionEvent e) {
099: logger.log("click").add("button", buttonLabel);
100: container.setVisible(false);
101: dialogToShow.setVisible(true);
102: }
103: });
104: container.getContentPane().add(button);
105: }
106: }
|