public void testTriggerExceptionsAreStoredAndRethrownWhenNotCaughtImmediately() throws Exception {
    final Exception exception = new RuntimeException("unexpected production code exception");
    Window window1 =
        WindowInterceptor.getModalDialog(
            new Trigger() {
              public void run() throws Exception {
                JDialog dialog = createModalDialog("dialog");
                addHideButton(dialog, "OK");
                dialog.setVisible(true);
                JDialog dialog2 = createModalDialog("dialog2");
                addHideButton(dialog2, "OK");
                dialog2.setVisible(true);
                throw exception;
              }
            });

    Window window2 = WindowInterceptor.getModalDialog(window1.getButton("OK").triggerClick());
    window2.titleEquals("dialog2");
    window2.getButton("OK").click();
    Utils.sleep(1);

    try {
      WindowInterceptor.run(
          new Trigger() {
            public void run() throws Exception {
              JDialog dialog3 = new JDialog();
              addHideButton(dialog3, "OK");
              dialog3.setVisible(true);
            }
          });
      fail();
    } catch (Exception e) {
      assertSame(exception, e.getCause());
    }
  }
 public void testInterceptionWithATriggerThatDisplaysNothing() throws Exception {
   try {
     WindowInterceptor.getModalDialog(Trigger.DO_NOTHING);
     throw new AssertionFailureNotDetectedError();
   } catch (AssertionError e) {
     assertEquals(
         ShownInterceptionDetectionHandler.NO_WINDOW_WAS_SHOWN_ERROR_MESSAGE, e.getMessage());
   }
 }
 public void testTriggerExceptionsAreConvertedIntoInterceptionErrors() throws Exception {
   final Exception exception = new IllegalAccessException("error");
   try {
     WindowInterceptor.getModalDialog(
         new Trigger() {
           public void run() throws Exception {
             throw exception;
           }
         });
     throw new AssertionFailureNotDetectedError();
   } catch (RuntimeException e) {
     assertSame(exception, e.getCause());
   }
 }
 public void testInterceptingAFrame() throws Exception {
   try {
     WindowInterceptor.getModalDialog(
         new Trigger() {
           public void run() {
             new JFrame("aFrame").setVisible(true);
           }
         });
     throw new AssertionFailureNotDetectedError();
   } catch (Exception e) {
     assertEquals(
         "Window 'aFrame' is non-modal, it must be intercepted with WindowInterceptor.run(Trigger)",
         e.getCause().getMessage());
   }
 }
 public void testInterceptingANonModalJDialog() throws Exception {
   try {
     WindowInterceptor.getModalDialog(
         new Trigger() {
           public void run() {
             JDialog dialog = new JDialog();
             dialog.setTitle("aDialog");
             dialog.setVisible(true);
           }
         });
     throw new AssertionFailureNotDetectedError();
   } catch (AssertionError e) {
     assertEquals(
         "Window 'aDialog' is non-modal, it must be intercepted with WindowInterceptor.run(Trigger)",
         e.getMessage());
   }
 }
 public void testInterceptingAModalDialog() throws Exception {
   Window window =
       WindowInterceptor.getModalDialog(
           new Trigger() {
             public void run() {
               logger.log("triggerRun");
               JDialog dialog = createModalDialog("aDialog");
               addHideButton(dialog, "OK");
               dialog.setVisible(true);
             }
           });
   logger.assertEquals("<log>" + "  <triggerRun/>" + "</log>");
   assertTrue(window.isVisible());
   window.getButton("OK").click();
   logger.assertEquals("<log>" + "  <click button='OK'/>" + "</log>");
   assertFalse(window.isVisible());
 }
 private void showModalDialogInThread(int waitWindowTimeLimit, final int waitTimeInThread) {
   final JDialog dialog = new JDialog(new JFrame(), "dialogShownInThread", true);
   UISpec4J.setWindowInterceptionTimeLimit(waitWindowTimeLimit);
   assertNotNull(
       WindowInterceptor.getModalDialog(
           new Trigger() {
             public void run() {
               logger.log("triggerRun");
               thread =
                   new Thread(
                       new Runnable() {
                         public void run() {
                           Utils.sleep(waitTimeInThread);
                           dialog.setVisible(true);
                         }
                       });
               thread.setName(thread.getName() + "(" + getName() + ")");
               thread.start();
             }
           }));
 }
 public void testInterceptingAJDialogShownFromAnotherThread() throws Exception {
   Window window =
       WindowInterceptor.getModalDialog(
           new Trigger() {
             public void run() throws Exception {
               thread =
                   new Thread() {
                     public void run() {
                       JDialog dialog = createModalDialog("expected title");
                       addHideButton(dialog, "OK");
                       dialog.setVisible(true);
                     }
                   };
               thread.start();
             }
           });
   window.titleEquals("expected title");
   window.getButton("OK").click();
   assertFalse(window.isVisible());
   window.dispose();
 }
  public void testTriggerExceptionsAreStoredWhenNotCaughtImmediately2() throws Exception {
    final RuntimeException exception = new RuntimeException("unexpected production code exception");
    Window window =
        WindowInterceptor.getModalDialog(
            new Trigger() {
              public void run() throws Exception {
                JDialog dialog = createModalDialog("dialog");
                addHideButton(dialog, "OK");
                dialog.setVisible(true);
                throw exception;
              }
            });

    window.getButton("OK").click();
    Utils.sleep(1);
    try {
      UISpecDisplay.instance().rethrowIfNeeded();
      fail();
    } catch (Exception e) {
      assertSame(exception, e.getCause());
    }
  }
 public void testInterceptingUsingAButtonTrigger() throws Exception {
   Button button = new Button(new JButton(new ShowDialogAction(true)));
   Window window = WindowInterceptor.getModalDialog(button.triggerClick());
   window.titleEquals("MyDialog");
   window.dispose();
 }