001: package org.uispec4j.interception;
002:
003: import junit.framework.Assert;
004: import junit.framework.AssertionFailedError;
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.ComponentUtils;
011: import org.uispec4j.utils.Utils;
012:
013: import javax.swing.*;
014: import java.awt.*;
015:
016: /**
017: * @noinspection UnnecessaryLocalVariable
018: */
019: public class WindowInterceptorForDialogSequenceTest extends
020: WindowInterceptorTestCase {
021:
022: private Thread thread;
023:
024: protected void tearDown() throws Exception {
025: super .tearDown();
026: if (thread != null) {
027: thread.join();
028: thread = null;
029: }
030: }
031:
032: public void testStandardSequence() {
033: WindowInterceptor.init(getShowFirstDialogTrigger()).process(
034: new WindowHandler() {
035: public Trigger process(Window window)
036: throws Exception {
037: logger.log("firstDialogShown");
038: return window.getButton("OK").triggerClick();
039: }
040: }).process(new WindowHandler() {
041: public Trigger process(Window window) throws Exception {
042: logger.log("secondDialogShown");
043: return window.getButton("OK").triggerClick();
044: }
045: }).process(new WindowHandler() {
046: public Trigger process(Window window) throws Exception {
047: logger.log("thirdDialogShown");
048: return window.getButton("Dispose").triggerClick();
049: }
050: }).run();
051:
052: logger.assertEquals("<log>" + " <trigger/>"
053: + " <firstDialogShown/>" + " <click button='OK'/>"
054: + " <secondDialogShown/>" + " <click button='OK'/>"
055: + " <thirdDialogShown/>"
056: + " <click button='Dispose'/>" + "</log>");
057: }
058:
059: public void testInterceptingAModalDialogWithAReturnedValue()
060: throws Exception {
061: WindowInterceptor.init(new Trigger() {
062: public void run() {
063: logger.log("triggerRun");
064: JTextField textField = new JTextField();
065: JDialog dialog = createModalDialog("aDialog");
066: dialog.getContentPane().add(textField);
067: dialog.setVisible(true);
068: Assert.assertEquals("result", textField.getText());
069: logger.log("done");
070: }
071: }).process(new WindowHandler() {
072: public Trigger process(final Window window)
073: throws Exception {
074: window.getTextBox().setText("result");
075: logger.log("windowProcessed");
076: return new Trigger() {
077: public void run() throws Exception {
078: ComponentUtils.close(window);
079: }
080: };
081: }
082: }).run();
083: logger.assertEquals("<log>" + " <triggerRun/>"
084: + " <windowProcessed/>" + " <done/>" + "</log>");
085: }
086:
087: public void testModalInterceptionWithATriggerThatDisplaysNothing()
088: throws Exception {
089: checkAssertionFailedError(
090: WindowInterceptor.init(Trigger.DO_NOTHING).process(
091: new WindowHandler() {
092: public Trigger process(Window window) {
093: throw new AssertionFailedError(
094: "this should not be called");
095: }
096: }),
097: ShownInterceptionDetectionHandler.NO_WINDOW_WAS_SHOWN_ERROR_MESSAGE);
098: assertEquals(0, UISpecDisplay.instance().getHandlerCount());
099: }
100:
101: public void testInterceptingAModalDialogWithoutClosingItInTheHandler()
102: throws Exception {
103: checkAssertionFailedError(
104: WindowInterceptor.init(new Trigger() {
105: public void run() {
106: logger.log("show");
107: createAndShowModalDialog("aDialog");
108: logger.log("closedByUISpec");
109: }
110: }).process(new WindowHandler() {
111: public Trigger process(Window window) {
112: logger.log("windowProcessed");
113: return Trigger.DO_NOTHING;
114: }
115: }),
116: "Modal window 'aDialog' was not closed - make sure that "
117: + "setVisible(false) gets called by the production code");
118: logger.assertEquals("<log>" + " <show/>"
119: + " <windowProcessed/>" + " <closedByUISpec/>"
120: + "</log>");
121: }
122:
123: public void testInterceptingAModalDialogShownFromAnotherThread()
124: throws Exception {
125: showModalDialogInThread(200, 100);
126: logger.assertEquals("<log>" + " <triggerRun/>"
127: + " <windowProcessed/>"
128: + " <click button='Dispose'/>" + "</log>");
129: }
130:
131: public void testUsingDisposeInShowDialog() throws Exception {
132: showDialogAndDispose();
133: logger.assertEquals("<log>" + " <show/>"
134: + " <click button='Dispose'/>" + " <closed/>"
135: + "</log>");
136: }
137:
138: public void testInterceptionWorksEvenWhenInterceptionIsRunFromTheSwingThread()
139: throws Exception {
140: SwingUtilities.invokeAndWait(new Runnable() {
141: public void run() {
142: showDialogAndDispose();
143: }
144: });
145:
146: logger.assertEquals("<log>" + " <show/>"
147: + " <click button='Dispose'/>" + " <closed/>"
148: + "</log>");
149: }
150:
151: public void testInterceptingAModalDialogWithoutClosingItInTheWindowHandlerWhenRunFromTheSwingThread()
152: throws Exception {
153: SwingUtilities.invokeAndWait(new Runnable() {
154: public void run() {
155: checkAssertionFailedError(
156: WindowInterceptor.init(new Trigger() {
157: public void run() {
158: logger.log("show");
159: createAndShowModalDialog("aDialog");
160: logger.log("closedByUISpec");
161: }
162: }).process(new WindowHandler() {
163: public Trigger process(Window window) {
164: logger.log("windowProcessed");
165: return Trigger.DO_NOTHING;
166: }
167: }),
168: "Modal window 'aDialog' was not closed - make sure that setVisible(false) gets "
169: + "called by the production code");
170: }
171: });
172: logger.assertEquals("<log>" + " <show/>"
173: + " <windowProcessed/>" + " <closedByUISpec/>"
174: + "</log>");
175: }
176:
177: public void testImbricationOfInterceptions() throws Exception {
178: final JFrame frame = new JFrame("frame");
179:
180: JDialog firstDialog = new JDialog(frame, "first", true);
181: addHideButton(firstDialog, "Dispose");
182: addShowDialogButton(frame, "Show", firstDialog);
183:
184: JDialog secondDialog = new JDialog(frame, "second", true);
185: addHideButton(secondDialog, "Dispose");
186: addShowDialogButton(firstDialog, "Show", secondDialog);
187:
188: WindowInterceptor.init(new Trigger() {
189: public void run() throws Exception {
190: frame.setVisible(true);
191: }
192: }).process(new WindowHandler() {
193: public Trigger process(Window frame) throws Exception {
194: WindowInterceptor.init(
195: new ClickButtonTrigger(frame, "Show")).process(
196: new WindowHandler() {
197: public Trigger process(Window firstWindow)
198: throws Exception {
199: WindowInterceptor.init(
200: new ClickButtonTrigger(
201: firstWindow, "Show"))
202: .process(new WindowHandler() {
203: public Trigger process(
204: Window secondWindow)
205: throws Exception {
206: return secondWindow
207: .getButton(
208: "Dispose")
209: .triggerClick();
210: }
211: }).run();
212: return firstWindow.getButton("Dispose")
213: .triggerClick();
214: }
215: }).run();
216: return Trigger.DO_NOTHING;
217: }
218: }).run();
219: logger.assertEquals("<log>" + " <click button='Show'/>"
220: + " <click button='Show'/>"
221: + " <click button='Dispose'/>"
222: + " <click button='Dispose'/>" + "</log>");
223: }
224:
225: public void testShowingTheSameDialogTwice() throws Exception {
226: WindowInterceptor.init(new Trigger() {
227: public void run() throws Exception {
228: JDialog dialog = createModalDialog("aDialog");
229: addHideButton(dialog, "Hide");
230: dialog.setVisible(true);
231: logger.log("step1");
232: dialog.setVisible(true);
233: logger.log("step2");
234: }
235: }).processWithButtonClick("Hide")
236: .processWithButtonClick("Hide").run();
237: logger.assertEquals("<log>" + " <click button='Hide'/>"
238: + " <step1/>" + " <click button='Hide'/>"
239: + " <step2/>" + "</log>");
240: }
241:
242: public void testShowIsBlocked() throws Exception {
243: WindowInterceptor.init(new Trigger() {
244: public void run() throws Exception {
245: JDialog dialog = createModalDialog("aDialog");
246: addHideButton(dialog, "Hide");
247: logger.log("beforeShow");
248: dialog.setVisible(true);
249: logger.log("afterShow");
250: }
251: }).process(new WindowHandler() {
252: public Trigger process(Window window) throws Exception {
253: Utils.sleep(100);
254: logger.log("sleepCompleted");
255: return window.getButton("Hide").triggerClick();
256: }
257: }).run();
258: logger.assertEquals("<log>" + " <beforeShow/>"
259: + " <sleepCompleted/>" + " <click button='Hide'/>"
260: + " <afterShow/>" + "</log>");
261: }
262:
263: public void testAwtDialogsAreNotSupported() throws Exception {
264: java.awt.Window window = new Dialog(new Frame());
265: try {
266: window.setVisible(true);
267: } catch (Throwable e) {
268: assertEquals("Dialogs of type '"
269: + window.getClass().getName()
270: + "' are not supported.", e.getMessage());
271: }
272: }
273:
274: public void testErrorWhenTheInitialTriggerDisplaysNoWindow()
275: throws Exception {
276: checkAssertionFailedError(
277: WindowInterceptor.init(Trigger.DO_NOTHING).process(
278: new WindowHandler() {
279: public Trigger process(Window window) {
280: return Trigger.DO_NOTHING;
281: }
282: }).processWithButtonClick("OK"),
283: "Error in first handler: "
284: + ShownInterceptionDetectionHandler.NO_WINDOW_WAS_SHOWN_ERROR_MESSAGE);
285: }
286:
287: public void testErrorWhenTheFirstHandlerDisplaysNoWindow()
288: throws Exception {
289: checkAssertionFailedError(
290: WindowInterceptor.init(getShowFirstDialogTrigger())
291: .process(new WindowHandler("first") {
292: public Trigger process(final Window window)
293: throws Exception {
294: return window.getButton("Dispose")
295: .triggerClick();
296: }
297: }).process(new WindowHandler() {
298: public Trigger process(Window window) {
299: fail("This one should not be called");
300: return Trigger.DO_NOTHING;
301: }
302: }),
303: "Error in handler 'first': "
304: + ShownInterceptionDetectionHandler.NO_WINDOW_WAS_SHOWN_ERROR_MESSAGE);
305: }
306:
307: public void testErrorWhenTheFirstHandlerThrowsAnError()
308: throws Exception {
309: checkAssertionFailedError(WindowInterceptor.init(
310: getShowFirstDialogTrigger()).process(
311: new WindowHandler("first") {
312: public Trigger process(Window window) {
313: throw new AssertionFailedError("error");
314: }
315: }).processWithButtonClick("ok"),
316: "Error in handler 'first': error");
317: }
318:
319: public void testErrorWhenTheSecondHandlerThrowsAnError()
320: throws Exception {
321: checkAssertionFailedError(WindowInterceptor.init(
322: getShowFirstDialogTrigger()).processWithButtonClick(
323: "OK").process(new WindowHandler("second") {
324: public Trigger process(Window window) {
325: throw new AssertionFailedError("error");
326: }
327: }), "Error in handler 'second': error");
328: }
329:
330: public void testErrorWhenTheFirstHandlerThrowsAnException()
331: throws Exception {
332: checkAssertionFailedError(WindowInterceptor.init(
333: getShowFirstDialogTrigger()).process(
334: new WindowHandler("first") {
335: public Trigger process(Window window) {
336: throw new RuntimeException("exception");
337: }
338: }).processWithButtonClick("ok"),
339: "Error in handler 'first': exception");
340: }
341:
342: public void testErrorWhenTheSecondHandlerThrowsAnException()
343: throws Exception {
344: checkAssertionFailedError(WindowInterceptor.init(
345: getShowFirstDialogTrigger()).processWithButtonClick(
346: "OK").process(new WindowHandler("second") {
347: public Trigger process(Window window) {
348: throw new RuntimeException("exception");
349: }
350: }).processWithButtonClick("ok"),
351: "Error in handler 'second': exception");
352: }
353:
354: public void testErrorWhenAModalDialogIsNotClosedInTheOnlyWindowWithOnlyOneHandler()
355: throws Exception {
356: checkAssertionFailedError(
357: WindowInterceptor.init(getShowFirstDialogTrigger())
358: .process(new WindowHandler("first") {
359: public Trigger process(Window window) {
360: return Trigger.DO_NOTHING;
361: }
362: }),
363: "Modal window 'first dialog' was not closed - "
364: + "make sure that setVisible(false) gets called by the production code");
365: }
366:
367: public void testFirstWindowNotClosed() throws Exception {
368: checkAssertionFailedError(
369: WindowInterceptor.init(new Trigger() {
370: public void run() throws Exception {
371: JDialog firstDialog = new JDialog(new JFrame(),
372: "first", true);
373: firstDialog.setTitle("first");
374: JDialog secondDialog = new JDialog(firstDialog,
375: "second", true);
376: addShowDialogButton(firstDialog, "show",
377: secondDialog);
378: addHideButton(secondDialog, "close");
379: firstDialog.setVisible(true);
380: }
381: }).processWithButtonClick("show")
382: .processWithButtonClick("close"),
383: "Error in first handler: "
384: + "Modal window 'first' was not closed - make sure that setVisible(false) gets "
385: + "called by the production code");
386: }
387:
388: public void testErrorWhenTheFirstWindowOfASequenceIsNotClosed()
389: throws Exception {
390: checkAssertionFailedError(
391: WindowInterceptor.init(new Trigger() {
392: public void run() throws Exception {
393: JDialog firstDialog = new JDialog(new JFrame(),
394: "first", true);
395: firstDialog.setTitle("first");
396: JDialog secondDialog = new JDialog(firstDialog,
397: "second", true);
398: addShowDialogButton(firstDialog, "show",
399: secondDialog);
400: addHideButton(secondDialog, "close");
401: firstDialog.setVisible(true);
402: }
403: }).processWithButtonClick("show")
404: .processWithButtonClick("close"),
405: "Error in first handler: "
406: + "Modal window 'first' was not closed - make sure that setVisible(false) gets "
407: + "called by the production code");
408: }
409:
410: public void testErrorWhenAModalDialogIsNotClosedInTheSecondAndLastWindow()
411: throws Exception {
412: checkAssertionFailedError(
413: WindowInterceptor.init(getShowFirstDialogTrigger())
414: .process(new WindowHandler("first") {
415: public Trigger process(Window window)
416: throws Exception {
417: return window.getButton("ok")
418: .triggerClick();
419: }
420: }).process(new WindowHandler("second") {
421: public Trigger process(Window window) {
422: return Trigger.DO_NOTHING;
423: }
424: }),
425: "Error in handler 'second': Modal window 'second dialog' was not closed - "
426: + "make sure that setVisible(false) gets called by the production code");
427: }
428:
429: public void testErrorWhenAModalDialogIsNotClosedInTheSecondWindow()
430: throws Exception {
431: checkAssertionFailedError(
432: WindowInterceptor.init(getShowFirstDialogTrigger())
433: .processWithButtonClick("ok").process(
434: new WindowHandler("second") {
435: public Trigger process(Window window) {
436: return Trigger.DO_NOTHING;
437: }
438: }).process(new WindowHandler("third") {
439: public Trigger process(Window window) {
440: return Trigger.DO_NOTHING;
441: }
442: }),
443: "Error in handler 'second': "
444: + ShownInterceptionDetectionHandler.NO_WINDOW_WAS_SHOWN_ERROR_MESSAGE);
445: }
446:
447: public void testNoHandlerAdded() throws Exception {
448: checkAssertionFailedError(WindowInterceptor
449: .init(Trigger.DO_NOTHING),
450: "You must add at least one handler");
451: }
452:
453: public void testHandlerNameIsNotGivenInTheMessageIfThereIsOnlyOneHandler()
454: throws Exception {
455: checkAssertionFailedError(WindowInterceptor.init(
456: getShowFirstDialogTrigger()).process(
457: new WindowHandler() {
458: public Trigger process(Window window) {
459: throw new AssertionFailedError("error");
460: }
461: }), "error");
462: }
463:
464: public void testHandlersAreGivenANumberIfNoNameIsSet()
465: throws Exception {
466: checkAssertionFailedError(WindowInterceptor.init(
467: getShowFirstDialogTrigger()).process(
468: new WindowHandler() {
469: public Trigger process(Window window) {
470: throw new AssertionFailedError("error");
471: }
472: }).processWithButtonClick(""),
473: "Error in handler '1': error");
474: checkAssertionFailedError(WindowInterceptor.init(
475: getShowFirstDialogTrigger()).processWithButtonClick(
476: "OK").process(new WindowHandler() {
477: public Trigger process(Window window) {
478: throw new AssertionFailedError("error");
479: }
480: }).processWithButtonClick(""), "Error in handler '2': error");
481: }
482:
483: public void testModalDialogsShownInSequenceByTheInitialTrigger()
484: throws Exception {
485: WindowInterceptor.init(
486: createTriggerWithThreeModalDialogsSequence())
487: .processWithButtonClick("dispose")
488: .processWithButtonClick("dispose")
489: .processWithButtonClick("dispose").run();
490: }
491:
492: public void testErrorForModalDialogsShownInSequenceByTheInitialTrigger()
493: throws Exception {
494: checkAssertionFailedError(WindowInterceptor.init(
495: createTriggerWithThreeModalDialogsSequence())
496: .processWithButtonClick("dispose").process(
497: new WindowHandler() {
498: public Trigger process(Window window) {
499: throw new AssertionFailedError("error");
500: }
501: }), "Error in handler '2': error");
502: }
503:
504: public void testNotClosedErrorForModalDialogsShownInSequenceByTheInitialTrigger()
505: throws Exception {
506: checkAssertionFailedError(
507: WindowInterceptor.init(
508: createTriggerWithThreeModalDialogsSequence())
509: .processWithButtonClick("dispose").process(
510: new WindowHandler("second") {
511: public Trigger process(Window window) {
512: window.titleEquals("dialog 2");
513: return Trigger.DO_NOTHING;
514: }
515: }),
516: "Error in handler 'second': Modal window 'dialog 2' was not closed - "
517: + "make sure that setVisible(false) gets called by the production code");
518: }
519:
520: private Trigger createTriggerWithThreeModalDialogsSequence() {
521: final JFrame frame = new JFrame();
522: Trigger trigger = new Trigger() {
523: public void run() throws Exception {
524: for (int i = 1; i < 4; i++) {
525: final JDialog dialog = new JDialog(frame, "dialog "
526: + i, true);
527: addHideButton(dialog, "Dispose");
528: dialog.setVisible(true);
529: }
530: }
531: };
532: return trigger;
533: }
534:
535: private void showModalDialogInThread(int waitWindowTimeLimit,
536: final int waitTimeInThread) {
537: final JDialog dialog = createModalDialog("aDialog");
538: addHideButton(dialog, "Dispose");
539:
540: UISpec4J.setWindowInterceptionTimeLimit(waitWindowTimeLimit);
541: WindowInterceptor.init(new Trigger() {
542: public void run() {
543: logger.log("triggerRun");
544: Runnable runnable = new Runnable() {
545: public void run() {
546: try {
547: Utils.sleep(waitTimeInThread);
548: SwingUtilities
549: .invokeAndWait(new Runnable() {
550: public void run() {
551: dialog.setVisible(true);
552: }
553: });
554: } catch (Exception e) {
555: throw new RuntimeException(e);
556: }
557: }
558: };
559: thread = new Thread(runnable);
560: thread.start();
561: }
562: }).process(new WindowHandler() {
563: public Trigger process(Window window) throws Exception {
564: logger.log("windowProcessed");
565: return window.getButton("Dispose").triggerClick();
566: }
567: }).run();
568: }
569:
570: private void showDialogAndDispose() {
571: WindowInterceptor.init(new Trigger() {
572: public void run() throws Exception {
573: final JDialog dialog = createModalDialog("aDialog");
574: addHideButton(dialog, "Dispose");
575: logger.log("show");
576: dialog.setVisible(true);
577: logger.log("closed");
578: }
579: }).process(new ButtonClickHandler("Dispose")).run();
580: }
581:
582: private static class ClickButtonTrigger implements Trigger {
583: private Window window;
584: private String buttonName;
585:
586: public ClickButtonTrigger(Window window, String buttonName) {
587: this .window = window;
588: this .buttonName = buttonName;
589: }
590:
591: public void run() throws Exception {
592: window.getButton(buttonName).click();
593: }
594: }
595:
596: private static class ButtonClickHandler extends WindowHandler {
597: private String buttonLabel;
598:
599: public ButtonClickHandler(String buttonLabel) {
600: this .buttonLabel = buttonLabel;
601: }
602:
603: public Trigger process(final Window window) throws Exception {
604: return window.getButton(buttonLabel).triggerClick();
605: }
606: }
607: }
|