001: package org.uispec4j.interception;
002:
003: import junit.framework.AssertionFailedError;
004: import org.uispec4j.Button;
005: import org.uispec4j.Trigger;
006: import org.uispec4j.UISpec4J;
007: import org.uispec4j.Window;
008: import org.uispec4j.interception.handlers.ShownInterceptionDetectionHandler;
009: import org.uispec4j.utils.AssertionFailureNotDetectedError;
010: import org.uispec4j.utils.Utils;
011:
012: import javax.swing.*;
013:
014: public class WindowInterceptorForNonModalWindowsTest extends
015: WindowInterceptorTestCase {
016: private Thread thread;
017:
018: protected void tearDown() throws Exception {
019: super .tearDown();
020: if (thread != null) {
021: thread.join();
022: thread = null;
023: }
024: }
025:
026: public void testInterceptingAFrame() throws Exception {
027: Window window = WindowInterceptor.run(new Trigger() {
028: public void run() {
029: logger.log("triggerRun");
030: final JFrame frame = new JFrame(
031: WindowInterceptorForNonModalWindowsTest.this
032: .getName());
033: addLoggerButton(frame, "OK");
034: frame.show();
035: }
036: });
037: assertNotNull(window);
038: window.getButton("OK").click();
039: logger.assertEquals("<log>" + " <triggerRun/>"
040: + " <click button='OK'/>" + "</log>");
041: }
042:
043: public void testInterceptingANonModalJDialog() throws Exception {
044: Window window = WindowInterceptor.run(new Trigger() {
045: public void run() {
046: logger.log("triggerRun");
047: JDialog dialog = new JDialog();
048: dialog
049: .setTitle(WindowInterceptorForNonModalWindowsTest.this
050: .getName());
051: addLoggerButton(dialog, "OK");
052: dialog.show();
053: }
054: });
055: assertNotNull(window);
056: window.getButton("OK").click();
057: logger.assertEquals("<log>" + " <triggerRun/>"
058: + " <click button='OK'/>" + "</log>");
059: }
060:
061: public void testInterceptionWithATriggerThatDisplaysNothing()
062: throws Exception {
063: try {
064: WindowInterceptor.run(Trigger.DO_NOTHING);
065: throw new AssertionFailureNotDetectedError();
066: } catch (AssertionFailedError e) {
067: assertEquals(
068: ShownInterceptionDetectionHandler.NO_WINDOW_WAS_SHOWN_ERROR_MESSAGE,
069: e.getMessage());
070: }
071: }
072:
073: public void testTriggerExceptionsAreConvertedIntoInterceptionErrors()
074: throws Exception {
075: final Exception exception = new IllegalAccessException("error");
076: try {
077: WindowInterceptor.run(new Trigger() {
078: public void run() throws Exception {
079: throw exception;
080: }
081: });
082: throw new AssertionFailureNotDetectedError();
083: } catch (RuntimeException e) {
084: assertSame(exception, e.getCause());
085: }
086:
087: try {
088: WindowInterceptor.init(new Trigger() {
089: public void run() throws Exception {
090: throw exception;
091: }
092: }).process(new WindowHandler() {
093: public Trigger process(Window window) {
094: return null;
095: }
096: }).run();
097: throw new AssertionFailureNotDetectedError();
098: } catch (InterceptionError e) {
099: assertEquals("java.lang.IllegalAccessException: error", e
100: .getMessage());
101: }
102: }
103:
104: public void testInterceptingUsingAButtonTrigger() throws Exception {
105: Button button = new Button(new JButton(new ShowDialogAction(
106: false)));
107: Window window = WindowInterceptor.run(button.triggerClick());
108: window.titleEquals("MyDialog");
109: }
110:
111: public void testInterceptingAModalDialogMustUseAHandler()
112: throws Exception {
113: try {
114: WindowInterceptor.run(new Trigger() {
115: public void run() {
116: createAndShowModalDialog("aDialog");
117: }
118: });
119: throw new AssertionFailureNotDetectedError();
120: } catch (AssertionFailedError e) {
121: assertEquals(
122: "Window 'aDialog' is modal, it must be intercepted with a WindowHandler",
123: e.getMessage());
124: }
125: }
126:
127: public void testInterceptingAJFrameShownFromAnotherThread()
128: throws Exception {
129: Window window = WindowInterceptor.run(new Trigger() {
130: public void run() throws Exception {
131: thread = new Thread() {
132: public void run() {
133: JFrame frame = new JFrame("expected title");
134: frame.show();
135: }
136: };
137: thread.start();
138: }
139: });
140: window.titleEquals("expected title");
141: }
142:
143: public void testInterceptingANonModalDialogShownFromAnotherThread()
144: throws Exception {
145: showNonModalDialogInThread(200, 100);
146: logger.assertEquals("<log>" + " <triggerRun/>" + "</log>");
147: }
148:
149: public void testNonModalWindowsDoNotNeedToBeClosed()
150: throws Exception {
151: final JFrame frame = new JFrame();
152: WindowInterceptor.init(new Trigger() {
153: public void run() throws Exception {
154: frame.show();
155: }
156: }).process(new WindowHandler() {
157: public Trigger process(Window window) {
158: Utils
159: .sleep(UISpec4J
160: .getWindowInterceptionTimeLimit() + 10);
161: return Trigger.DO_NOTHING;
162: }
163: }).run();
164: assertTrue(frame.isVisible());
165: }
166:
167: private void showNonModalDialogInThread(int waitWindowTimeLimit,
168: final int waitTimeInThread) {
169: final JDialog dialog = new JDialog(new JFrame(),
170: "dialogShownInThread", false);
171: UISpec4J.setWindowInterceptionTimeLimit(waitWindowTimeLimit);
172: assertNotNull(WindowInterceptor.run(new Trigger() {
173: public void run() {
174: logger.log("triggerRun");
175: thread = new Thread(new Runnable() {
176: public void run() {
177: Utils.sleep(waitTimeInThread);
178: dialog.show();
179: }
180: });
181: thread
182: .setName(thread.getName() + "(" + getName()
183: + ")");
184: thread.start();
185: }
186: }));
187: }
188: }
|