001: package org.uispec4j.interception;
002:
003: import org.uispec4j.Trigger;
004: import org.uispec4j.Window;
005:
006: import javax.swing.*;
007:
008: public class BasicHandlerTest extends InterceptionTestCase {
009:
010: public void testStandardUsage() throws Exception {
011: WindowInterceptor.init(triggerShowDialog()).process(
012: BasicHandler.init().assertContainsText("some text")
013: .clickButton("OK").triggerButtonClick("Hide"))
014: .run();
015: logger.assertEquals("<log>" + " <click button='OK'/>"
016: + " <click button='Hide'/>" + "</log>");
017: }
018:
019: public void testAssertContainsTextError() throws Exception {
020: checkAssertionFailedError(WindowInterceptor.init(
021: triggerShowDialog()).process(
022: BasicHandler.init().assertContainsText("Error")
023: .triggerButtonClick("Hide")),
024: "Text not found: Error");
025: }
026:
027: public void testClickButtonError() throws Exception {
028: checkAssertionFailedError(WindowInterceptor.init(
029: triggerShowDialog()).process(
030: BasicHandler.init().clickButton("Unknown")
031: .triggerButtonClick("Hide")),
032: "Component 'Unknown' of type 'button' not found - available names: [Hide,OK]");
033: }
034:
035: public void testJOptionPaneConfirmationReplies() throws Exception {
036: checkSelectedValue(JOptionPane.YES_OPTION,
037: JOptionPane.YES_NO_OPTION, "Yes");
038: checkSelectedValue(JOptionPane.NO_OPTION,
039: JOptionPane.YES_NO_OPTION, "No");
040: checkSelectedValue(JOptionPane.OK_OPTION,
041: JOptionPane.OK_CANCEL_OPTION, "OK");
042: checkSelectedValue(JOptionPane.CANCEL_OPTION,
043: JOptionPane.OK_CANCEL_OPTION, "Cancel");
044: }
045:
046: public void testSetInputInJOptionPane() throws Exception {
047: WindowInterceptor.init(new Trigger() {
048: public void run() throws Exception {
049: assertEquals("result", JOptionPane
050: .showInputDialog("Message"));
051: }
052: }).process(
053: BasicHandler.init().setText("result")
054: .triggerButtonClick("OK")).run();
055: }
056:
057: public void testSetInputWithNullValueInJOptionPane()
058: throws Exception {
059: WindowInterceptor.init(new Trigger() {
060: public void run() throws Exception {
061: assertEquals("", JOptionPane.showInputDialog("Message"));
062: }
063: }).process(
064: BasicHandler.init().setText(null).triggerButtonClick(
065: "OK")).run();
066: }
067:
068: /* This is not a feature, but a known limitation */
069: public void testSetInputFollowedByACancelInJOptionPaneReturnsTheInputValue()
070: throws Exception {
071: WindowInterceptor.init(new Trigger() {
072: public void run() throws Exception {
073: assertEquals("Result", JOptionPane
074: .showInputDialog("Message"));
075: }
076: }).process(
077: BasicHandler.init().setText("Result")
078: .triggerButtonClick("Cancel")).run();
079: }
080:
081: public void testInterceptingAJOptionPaneFromInsideATrigger()
082: throws Exception {
083: final JFrame frame = new JFrame();
084: WindowInterceptor.init(new Trigger() {
085: public void run() throws Exception {
086: WindowInterceptor.init(new Trigger() {
087: public void run() throws Exception {
088: int result = JOptionPane.showConfirmDialog(
089: frame, "OK?", "Title",
090: JOptionPane.OK_CANCEL_OPTION);
091: if (result == JOptionPane.OK_OPTION) {
092: logger.log("showDialog");
093: JDialog dialog = new JDialog(frame,
094: "Dialog", true);
095: dialog.setVisible(true);
096: } else {
097: throw new Error("unexpected result "
098: + result);
099: }
100: }
101: })
102: .process(
103: BasicHandler.init().triggerButtonClick(
104: "OK")).run();
105: }
106: }).process(new WindowHandler() {
107: public Trigger process(final Window window) {
108: logger.log("dialogShown").add("title",
109: window.getTitle());
110: return new Trigger() {
111: public void run() throws Exception {
112: window.getAwtContainer().setVisible(false);
113: }
114: };
115: }
116: }).run();
117: logger.assertEquals("<log>" + " <showDialog/>"
118: + " <dialogShown title='Dialog'/>" + "</log>");
119: }
120:
121: public void testJOptionPaneInterceptionInAWindowSequence()
122: throws Exception {
123: final JFrame frame = new JFrame();
124: WindowInterceptor
125: .init(new Trigger() {
126: public void run() throws Exception {
127: int result = JOptionPane.showConfirmDialog(
128: frame, "Confirm?", "Title",
129: JOptionPane.YES_NO_OPTION);
130: if (result == JOptionPane.YES_OPTION) {
131: logger.log("start");
132: JDialog dialog = new JDialog(frame,
133: "dialog", true);
134: addHideButton(dialog, "Close");
135: dialog.setVisible(true);
136: logger.log("end");
137: } else {
138: throw new Error("Unexpected result "
139: + result);
140: }
141: }
142: }).process(
143: BasicHandler.init().assertContainsText(
144: "Confirm?").triggerButtonClick("Yes"))
145: .processWithButtonClick("Close").run();
146: logger.assertEquals("<log>" + " <start/>"
147: + " <click button='Close'/>" + " <end/>" + "</log>");
148: }
149:
150: private void checkSelectedValue(final int value,
151: final int optionType, String button) {
152: WindowInterceptor.init(new Trigger() {
153: public void run() throws Exception {
154: assertEquals(value, JOptionPane.showConfirmDialog(
155: new JFrame(), "msg", "title", optionType,
156: JOptionPane.WARNING_MESSAGE));
157: }
158: }).process(BasicHandler.init().triggerButtonClick(button))
159: .run();
160: }
161:
162: private Trigger triggerShowDialog() {
163: return new Trigger() {
164: public void run() throws Exception {
165: JDialog dialog = createModalDialog("aDialog");
166: dialog.setTitle("Dialog title");
167: dialog.getContentPane().add(new JTextArea("some text"));
168: addHideButton(dialog, "Hide");
169: addLoggerButton(dialog, "OK");
170: dialog.setVisible(true);
171: }
172: };
173: }
174: }
|