⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 windowinterceptorfordialogsequencetest.java

📁 基于Junit的 功能和单元测试的的测试工具。只支持Swing.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
                              .processWithButtonClick("OK"),
                              "Error in first handler: " +
                              ShownInterceptionDetectionHandler.NO_WINDOW_WAS_SHOWN_ERROR_MESSAGE);
  }

  public void testErrorWhenTheFirstHandlerDisplaysNoWindow() throws Exception {
    checkAssertionFailedError(WindowInterceptor
                              .init(getShowFirstDialogTrigger())
                              .process(new WindowHandler("first") {
                                public Trigger process(final Window window) throws Exception {
                                  return window.getButton("Dispose").triggerClick();
                                }
                              })
                              .process(new WindowHandler() {
                                public Trigger process(Window window) {
                                  fail("This one should not be called");
                                  return Trigger.DO_NOTHING;
                                }
                              }), "Error in handler 'first': " +
                                  ShownInterceptionDetectionHandler.NO_WINDOW_WAS_SHOWN_ERROR_MESSAGE);
  }

  public void testErrorWhenTheFirstHandlerThrowsAnError() throws Exception {
    checkAssertionFailedError(WindowInterceptor
                              .init(getShowFirstDialogTrigger())
                              .process(new WindowHandler("first") {
                                public Trigger process(Window window) {
                                  throw new AssertionFailedError("error");
                                }
                              })
                              .processWithButtonClick("ok"), "Error in handler 'first': error");
  }

  public void testErrorWhenTheSecondHandlerThrowsAnError() throws Exception {
    checkAssertionFailedError(WindowInterceptor
                              .init(getShowFirstDialogTrigger())
                              .processWithButtonClick("OK")
                              .process(new WindowHandler("second") {
                                public Trigger process(Window window) {
                                  throw new AssertionFailedError("error");
                                }
                              }), "Error in handler 'second': error");
  }

  public void testErrorWhenTheFirstHandlerThrowsAnException() throws Exception {
    checkAssertionFailedError(WindowInterceptor
                              .init(getShowFirstDialogTrigger())
                              .process(new WindowHandler("first") {
                                public Trigger process(Window window) {
                                  throw new RuntimeException("exception");
                                }
                              })
                              .processWithButtonClick("ok"), "Error in handler 'first': exception");
  }

  public void testErrorWhenTheSecondHandlerThrowsAnException() throws Exception {
    checkAssertionFailedError(WindowInterceptor
                              .init(getShowFirstDialogTrigger())
                              .processWithButtonClick("OK")
                              .process(new WindowHandler("second") {
                                public Trigger process(Window window) {
                                  throw new RuntimeException("exception");
                                }
                              })
                              .processWithButtonClick("ok"), "Error in handler 'second': exception");
  }

  public void testErrorWhenAModalDialogIsNotClosedInTheOnlyWindowWithOnlyOneHandler() throws Exception {
    checkAssertionFailedError(WindowInterceptor
                              .init(getShowFirstDialogTrigger())
                              .process(new WindowHandler("first") {
                                public Trigger process(Window window) {
                                  return Trigger.DO_NOTHING;
                                }
                              }), "Modal window 'first dialog' was not closed - " +
                                  "make sure that setVisible(false) gets called by the production code");
  }

  public void testFirstWindowNotClosed() throws Exception {
    checkAssertionFailedError(WindowInterceptor
                              .init(new Trigger() {
                                public void run() throws Exception {
                                  JDialog firstDialog = new JDialog(new JFrame(), "first", true);
                                  firstDialog.setTitle("first");
                                  JDialog secondDialog = new JDialog(firstDialog, "second", true);
                                  addShowDialogButton(firstDialog, "show", secondDialog);
                                  addHideButton(secondDialog, "close");
                                  firstDialog.setVisible(true);
                                }
                              })
                              .processWithButtonClick("show")
                              .processWithButtonClick("close"), "Error in first handler: " +
                                                                "Modal window 'first' was not closed - make sure that setVisible(false) gets " +
                                                                "called by the production code");
  }

  public void testErrorWhenTheFirstWindowOfASequenceIsNotClosed() throws Exception {
    checkAssertionFailedError(WindowInterceptor
                              .init(new Trigger() {
                                public void run() throws Exception {
                                  JDialog firstDialog = new JDialog(new JFrame(), "first", true);
                                  firstDialog.setTitle("first");
                                  JDialog secondDialog = new JDialog(firstDialog, "second", true);
                                  addShowDialogButton(firstDialog, "show", secondDialog);
                                  addHideButton(secondDialog, "close");
                                  firstDialog.setVisible(true);
                                }
                              })
                              .processWithButtonClick("show")
                              .processWithButtonClick("close"), "Error in first handler: " +
                                                                "Modal window 'first' was not closed - make sure that setVisible(false) gets " +
                                                                "called by the production code");
  }

  public void testErrorWhenAModalDialogIsNotClosedInTheSecondAndLastWindow() throws Exception {
    checkAssertionFailedError(WindowInterceptor
                              .init(getShowFirstDialogTrigger())
                              .process(new WindowHandler("first") {
                                public Trigger process(Window window) throws Exception {
                                  return window.getButton("ok").triggerClick();
                                }
                              })
                              .process(new WindowHandler("second") {
                                public Trigger process(Window window) {
                                  return Trigger.DO_NOTHING;
                                }
                              }), "Error in handler 'second': Modal window 'second dialog' was not closed - " +
                                  "make sure that setVisible(false) gets called by the production code");
  }

  public void testErrorWhenAModalDialogIsNotClosedInTheSecondWindow() throws Exception {
    checkAssertionFailedError(WindowInterceptor
                              .init(getShowFirstDialogTrigger())
                              .processWithButtonClick("ok")
                              .process(new WindowHandler("second") {
                                public Trigger process(Window window) {
                                  return Trigger.DO_NOTHING;
                                }
                              })
                              .process(new WindowHandler("third") {
                                public Trigger process(Window window) {
                                  return Trigger.DO_NOTHING;
                                }
                              }), "Error in handler 'second': " +
                                  ShownInterceptionDetectionHandler.NO_WINDOW_WAS_SHOWN_ERROR_MESSAGE);
  }

  public void testNoHandlerAdded() throws Exception {
    checkAssertionFailedError(WindowInterceptor.init(Trigger.DO_NOTHING), "You must add at least one handler");
  }

  public void testHandlerNameIsNotGivenInTheMessageIfThereIsOnlyOneHandler() throws Exception {
    checkAssertionFailedError(WindowInterceptor
                              .init(getShowFirstDialogTrigger())
                              .process(new WindowHandler() {
                                public Trigger process(Window window) {
                                  throw new AssertionFailedError("error");
                                }
                              }), "error");
  }

  public void testHandlersAreGivenANumberIfNoNameIsSet() throws Exception {
    checkAssertionFailedError(WindowInterceptor
                              .init(getShowFirstDialogTrigger())
                              .process(new WindowHandler() {
                                public Trigger process(Window window) {
                                  throw new AssertionFailedError("error");
                                }
                              })
                              .processWithButtonClick(""), "Error in handler '1': error");
    checkAssertionFailedError(WindowInterceptor
                              .init(getShowFirstDialogTrigger())
                              .processWithButtonClick("OK")
                              .process(new WindowHandler() {
                                public Trigger process(Window window) {
                                  throw new AssertionFailedError("error");
                                }
                              })
                              .processWithButtonClick(""), "Error in handler '2': error");
  }

  public void testModalDialogsShownInSequenceByTheInitialTrigger() throws Exception {
    WindowInterceptor
        .init(createTriggerWithThreeModalDialogsSequence())
        .processWithButtonClick("dispose")
        .processWithButtonClick("dispose")
        .processWithButtonClick("dispose")
        .run();
  }

  public void testErrorForModalDialogsShownInSequenceByTheInitialTrigger() throws Exception {
    checkAssertionFailedError(WindowInterceptor
                              .init(createTriggerWithThreeModalDialogsSequence())
                              .processWithButtonClick("dispose")
                              .process(new WindowHandler() {
                                public Trigger process(Window window) {
                                  throw new AssertionFailedError("error");
                                }
                              }), "Error in handler '2': error");
  }

  public void testNotClosedErrorForModalDialogsShownInSequenceByTheInitialTrigger() throws Exception {
    checkAssertionFailedError(WindowInterceptor
                              .init(createTriggerWithThreeModalDialogsSequence())
                              .processWithButtonClick("dispose")
                              .process(new WindowHandler("second") {
                                public Trigger process(Window window) {
                                  window.titleEquals("dialog 2");
                                  return Trigger.DO_NOTHING;
                                }
                              }), "Error in handler 'second': Modal window 'dialog 2' was not closed - " +
                                  "make sure that setVisible(false) gets called by the production code");
  }

  private Trigger createTriggerWithThreeModalDialogsSequence() {
    final JFrame frame = new JFrame();
    Trigger trigger = new Trigger() {
      public void run() throws Exception {
        for (int i = 1; i < 4; i++) {
          final JDialog dialog = new JDialog(frame, "dialog " + i, true);
          addHideButton(dialog, "Dispose");
          dialog.setVisible(true);
        }
      }
    };
    return trigger;
  }

  private void showModalDialogInThread(int waitWindowTimeLimit, final int waitTimeInThread) {
    final JDialog dialog = createModalDialog("aDialog");
    addHideButton(dialog, "Dispose");

    UISpec4J.setWindowInterceptionTimeLimit(waitWindowTimeLimit);
    WindowInterceptor
        .init(new Trigger() {
          public void run() {
            logger.log("triggerRun");
            Runnable runnable = new Runnable() {
              public void run() {
                try {
                  Utils.sleep(waitTimeInThread);
                  SwingUtilities.invokeAndWait(new Runnable() {
                    public void run() {
                      dialog.setVisible(true);
                    }
                  });
                }
                catch (Exception e) {
                  throw new RuntimeException(e);
                }
              }
            };
            thread = new Thread(runnable);
            thread.start();
          }
        })
        .process(new WindowHandler() {
          public Trigger process(Window window) throws Exception {
            logger.log("windowProcessed");
            return window.getButton("Dispose").triggerClick();
          }
        })
        .run();
  }

  private void showDialogAndDispose() {
    WindowInterceptor
        .init(new Trigger() {
          public void run() throws Exception {
            final JDialog dialog = createModalDialog("aDialog");
            addHideButton(dialog, "Dispose");
            logger.log("show");
            dialog.setVisible(true);
            logger.log("closed");
          }
        })
        .process(new ButtonClickHandler("Dispose"))
        .run();
  }

  private static class ClickButtonTrigger implements Trigger {
    private Window window;
    private String buttonName;

    public ClickButtonTrigger(Window window, String buttonName) {
      this.window = window;
      this.buttonName = buttonName;
    }

    public void run() throws Exception {
      window.getButton(buttonName).click();
    }
  }

  private static class ButtonClickHandler extends WindowHandler {
    private String buttonLabel;

    public ButtonClickHandler(String buttonLabel) {
      this.buttonLabel = buttonLabel;
    }

    public Trigger process(final Window window) throws Exception {
      return window.getButton(buttonLabel).triggerClick();
    }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -