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.interception.toolkit.UISpecDisplay;
010: import org.uispec4j.utils.AssertionFailureNotDetectedError;
011: import org.uispec4j.utils.Utils;
012:
013: import javax.swing.*;
014:
015: public class WindowInterceptorForModalDialogsTest extends
016: WindowInterceptorTestCase {
017: private Thread thread;
018:
019: protected void tearDown() throws Exception {
020: super .tearDown();
021: if (thread != null) {
022: thread.join();
023: thread = null;
024: }
025: }
026:
027: public void testInterceptingAModalDialog() throws Exception {
028: Window window = WindowInterceptor.getModalDialog(new Trigger() {
029: public void run() {
030: logger.log("triggerRun");
031: JDialog dialog = createModalDialog("aDialog");
032: addHideButton(dialog, "OK");
033: dialog.show();
034: }
035: });
036: logger.assertEquals("<log>" + " <triggerRun/>" + "</log>");
037: assertTrue(window.isVisible());
038: window.getButton("OK").click();
039: logger.assertEquals("<log>" + " <click button='OK'/>"
040: + "</log>");
041: assertFalse(window.isVisible());
042: }
043:
044: public void testInterceptingAFrame() throws Exception {
045: try {
046: WindowInterceptor.getModalDialog(new Trigger() {
047: public void run() {
048: new JFrame("aFrame").show();
049: }
050: });
051: throw new AssertionFailureNotDetectedError();
052: } catch (AssertionFailedError e) {
053: assertEquals(
054: "Window 'aFrame' is non-modal, it must be intercepted with WindowInterceptor.run(Trigger)",
055: e.getMessage());
056: }
057: }
058:
059: public void testInterceptingANonModalJDialog() throws Exception {
060: try {
061: WindowInterceptor.getModalDialog(new Trigger() {
062: public void run() {
063: JDialog dialog = new JDialog();
064: dialog.setTitle("aDialog");
065: dialog.show();
066: }
067: });
068: throw new AssertionFailureNotDetectedError();
069: } catch (AssertionFailedError e) {
070: assertEquals(
071: "Window 'aDialog' is non-modal, it must be intercepted with WindowInterceptor.run(Trigger)",
072: e.getMessage());
073: }
074: }
075:
076: public void testInterceptionWithATriggerThatDisplaysNothing()
077: throws Exception {
078: try {
079: WindowInterceptor.getModalDialog(Trigger.DO_NOTHING);
080: throw new AssertionFailureNotDetectedError();
081: } catch (AssertionFailedError e) {
082: assertEquals(
083: ShownInterceptionDetectionHandler.NO_WINDOW_WAS_SHOWN_ERROR_MESSAGE,
084: e.getMessage());
085: }
086: }
087:
088: public void testTriggerExceptionsAreConvertedIntoInterceptionErrors()
089: throws Exception {
090: final Exception exception = new IllegalAccessException("error");
091: try {
092: WindowInterceptor.getModalDialog(new Trigger() {
093: public void run() throws Exception {
094: throw exception;
095: }
096: });
097: throw new AssertionFailureNotDetectedError();
098: } catch (RuntimeException e) {
099: assertSame(exception, e.getCause());
100: }
101: }
102:
103: public void testTriggerExceptionsAreStoredAndRethrownWhenNotCaughtImmediately()
104: throws Exception {
105: final Exception exception = new RuntimeException(
106: "unexpected production code exception");
107: Window window1 = WindowInterceptor
108: .getModalDialog(new Trigger() {
109: public void run() throws Exception {
110: JDialog dialog = createModalDialog("dialog");
111: addHideButton(dialog, "OK");
112: dialog.show();
113: JDialog dialog2 = createModalDialog("dialog2");
114: addHideButton(dialog2, "OK");
115: dialog2.show();
116: throw exception;
117: }
118: });
119:
120: Window window2 = WindowInterceptor.getModalDialog(window1
121: .getButton("OK").triggerClick());
122: window2.titleEquals("dialog2");
123: window2.getButton("OK").click();
124: Utils.sleep(1);
125:
126: try {
127: WindowInterceptor.run(new Trigger() {
128: public void run() throws Exception {
129: JDialog dialog3 = new JDialog();
130: addHideButton(dialog3, "OK");
131: dialog3.show();
132: }
133: });
134: fail();
135: } catch (Exception e) {
136: assertSame(exception, e);
137: }
138: }
139:
140: public void testTriggerExceptionsAreStoredWhenNotCaughtImmediately2()
141: throws Exception {
142: final RuntimeException exception = new RuntimeException(
143: "unexpected production code exception");
144: Window window = WindowInterceptor.getModalDialog(new Trigger() {
145: public void run() throws Exception {
146: JDialog dialog = createModalDialog("dialog");
147: addHideButton(dialog, "OK");
148: dialog.show();
149: throw exception;
150: }
151: });
152:
153: window.getButton("OK").click();
154: Utils.sleep(1);
155: try {
156: UISpecDisplay.instance().rethrowIfNeeded();
157: fail();
158: } catch (Exception e) {
159: assertSame(exception, e);
160: }
161: }
162:
163: public void testInterceptingUsingAButtonTrigger() throws Exception {
164: Button button = new Button(new JButton(new ShowDialogAction(
165: true)));
166: Window window = WindowInterceptor.getModalDialog(button
167: .triggerClick());
168: window.titleEquals("MyDialog");
169: }
170:
171: public void testInterceptingAJDialogShownFromAnotherThread()
172: throws Exception {
173: Window window = WindowInterceptor.getModalDialog(new Trigger() {
174: public void run() throws Exception {
175: thread = new Thread() {
176: public void run() {
177: JDialog dialog = createModalDialog("expected title");
178: addHideButton(dialog, "OK");
179: dialog.show();
180: }
181: };
182: thread.start();
183: }
184: });
185: window.titleEquals("expected title");
186: window.getButton("OK").click();
187: assertFalse(window.isVisible());
188: }
189:
190: public void disabled_testInterceptingAModalDialogShownFromAnotherThread()
191: throws Exception {
192: showModalDialogInThread(200, 100);
193: logger.assertEquals("<log>" + " <triggerRun/>" + "</log>");
194: }
195:
196: private void showModalDialogInThread(int waitWindowTimeLimit,
197: final int waitTimeInThread) {
198: final JDialog dialog = new JDialog(new JFrame(),
199: "dialogShownInThread", true);
200: UISpec4J.setWindowInterceptionTimeLimit(waitWindowTimeLimit);
201: assertNotNull(WindowInterceptor.getModalDialog(new Trigger() {
202: public void run() {
203: logger.log("triggerRun");
204: thread = new Thread(new Runnable() {
205: public void run() {
206: Utils.sleep(waitTimeInThread);
207: dialog.show();
208: }
209: });
210: thread
211: .setName(thread.getName() + "(" + getName()
212: + ")");
213: thread.start();
214: }
215: }));
216: }
217: }
|