001: package org.uispec4j.interception;
002:
003: import org.uispec4j.Trigger;
004: import org.uispec4j.Window;
005: import org.uispec4j.utils.Functor;
006: import org.uispec4j.utils.Utils;
007:
008: import javax.swing.*;
009:
010: public class WindowInterceptorCustomMethodsTest extends
011: WindowInterceptorTestCase {
012:
013: public void testProcessTransientWindow() throws Exception {
014: WindowInterceptor.init(new TransientWindowTrigger())
015: .processTransientWindow("Actual").run();
016: }
017:
018: public void testProcessTransientWindowWithNoTitle()
019: throws Exception {
020: WindowInterceptor.init(new TransientWindowTrigger())
021: .processTransientWindow().run();
022: }
023:
024: public void testProcessTransientWindowError() throws Exception {
025: checkInterceptionError(new Functor() {
026: public void run() throws Exception {
027: WindowInterceptor.init(new TransientWindowTrigger())
028: .processTransientWindow("Expected").run();
029: }
030: },
031: "Invalid window title - expected:<Expected> but was:<Actual>");
032: }
033:
034: public void testWindowTitleChecking() throws Exception {
035: WindowInterceptor.init(new Trigger() {
036: public void run() throws Exception {
037: JDialog dialog = createModalDialog("dialog title");
038: addHideButton(dialog, "Hide");
039: dialog.setVisible(true);
040: }
041: }).process("dialog title", new ButtonTriggerHandler("Hide"))
042: .run();
043: logger.assertEquals("<log>" + " <click button='Hide'/>"
044: + "</log>");
045: }
046:
047: public void testWindowTitleError() throws Exception {
048: checkAssertionFailedError(WindowInterceptor.init(new Trigger() {
049: public void run() throws Exception {
050: JDialog dialog = createModalDialog("dialog title");
051: addHideButton(dialog, "Hide");
052: dialog.setVisible(true);
053: }
054: }).process("error", new ButtonTriggerHandler("Hide")),
055: "expected:<error> but was:<dialog title>");
056: }
057:
058: public void testWindowTitleErrorInASequence() throws Exception {
059: checkAssertionFailedError(WindowInterceptor.init(
060: getShowFirstDialogTrigger()).processWithButtonClick(
061: "OK").process("error", new ButtonTriggerHandler("OK")),
062: "Error in handler 'error': expected:<error> but was:<second dialog>");
063: }
064:
065: public void testProcessWithButtonClick() {
066: WindowInterceptor.init(getShowFirstDialogTrigger())
067: .processWithButtonClick("OK").processWithButtonClick(
068: "second dialog", "OK").processWithButtonClick(
069: "Dispose").run();
070: logger.assertEquals("<log>" + " <trigger/>"
071: + " <click button='OK'/>" + " <click button='OK'/>"
072: + " <click button='Dispose'/>" + "</log>");
073: }
074:
075: public void testProcessSeveralHandlers() throws Exception {
076: WindowInterceptor.init(getShowFirstDialogTrigger()).process(
077: new WindowHandler[] { new ButtonTriggerHandler("OK"),
078: new ButtonTriggerHandler("OK"),
079: new ButtonTriggerHandler("Dispose"), }).run();
080: logger.assertEquals("<log>" + " <trigger/>"
081: + " <click button='OK'/>" + " <click button='OK'/>"
082: + " <click button='Dispose'/>" + "</log>");
083: }
084:
085: private static class ButtonTriggerHandler extends WindowHandler {
086: private String buttonName;
087:
088: public ButtonTriggerHandler(String buttonName) {
089: this .buttonName = buttonName;
090: }
091:
092: public Trigger process(Window window) throws Exception {
093: return window.getButton(buttonName).triggerClick();
094: }
095: }
096:
097: public void testProcessWithButtonClickWithAnUnknownButtonName()
098: throws Exception {
099: checkAssertionFailedError(
100: WindowInterceptor.init(getShowFirstDialogTrigger())
101: .processWithButtonClick("unknown"),
102: "Component 'unknown' of type 'button' not found - available names: [Dispose,OK]");
103: }
104:
105: public void testProcessWithButtonClickHandlesJOptionPaneDialogs()
106: throws Exception {
107: final JFrame frame = new JFrame();
108: WindowInterceptor.run(new Trigger() {
109: public void run() throws Exception {
110: frame.setVisible(true);
111: }
112: });
113: WindowInterceptor.init(new Trigger() {
114: public void run() throws Exception {
115: int result = JOptionPane
116: .showConfirmDialog(frame, "msg");
117: logger.log("confirm").add("result", result);
118: }
119: }).processWithButtonClick("Yes").run();
120: logger.assertEquals("<log>" + " <confirm result='0'/>"
121: + "</log>");
122: }
123:
124: public void testProcessWithButtonClickWithAnInvalidTitle()
125: throws Exception {
126: checkInterceptionError(new Functor() {
127: public void run() throws Exception {
128: WindowInterceptor.init(new Trigger() {
129: public void run() throws Exception {
130: JDialog dialog = new JDialog(new JFrame(),
131: "Actual");
132: addHideButton(dialog, "ok");
133: dialog.setVisible(true);
134: }
135: }).processWithButtonClick("Expected", "OK").run();
136: }
137: },
138: "Invalid window title - expected:<Expected> but was:<Actual>");
139: }
140:
141: private static class TransientWindowTrigger implements Trigger {
142: public void run() throws Exception {
143: JDialog dialog = new JDialog(new JFrame(), "Actual");
144: Thread thread = new Thread() {
145: public void run() {
146: Utils.sleep(20);
147: }
148: };
149: thread.run();
150: dialog.setVisible(true);
151: thread.join();
152: }
153: }
154: }
|