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

📄 windowinterceptorfordialogsequencetest.java

📁 基于Junit的 功能和单元测试的的测试工具。只支持Swing.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
package org.uispec4j.interception;

import junit.framework.Assert;
import junit.framework.AssertionFailedError;
import org.uispec4j.Trigger;
import org.uispec4j.UISpec4J;
import org.uispec4j.Window;
import org.uispec4j.interception.handlers.ShownInterceptionDetectionHandler;
import org.uispec4j.interception.toolkit.UISpecDisplay;
import org.uispec4j.utils.ComponentUtils;
import org.uispec4j.utils.Utils;

import javax.swing.*;
import java.awt.*;

/**
 * @noinspection UnnecessaryLocalVariable
 */
public class WindowInterceptorForDialogSequenceTest extends WindowInterceptorTestCase {

  private Thread thread;

  protected void tearDown() throws Exception {
    super.tearDown();
    if (thread != null) {
      thread.join();
      thread = null;
    }
  }

  public void testStandardSequence() {
    WindowInterceptor
        .init(getShowFirstDialogTrigger())
        .process(new WindowHandler() {
          public Trigger process(Window window) throws Exception {
            logger.log("firstDialogShown");
            return window.getButton("OK").triggerClick();
          }
        })
        .process(new WindowHandler() {
          public Trigger process(Window window) throws Exception {
            logger.log("secondDialogShown");
            return window.getButton("OK").triggerClick();
          }
        })
        .process(new WindowHandler() {
          public Trigger process(Window window) throws Exception {
            logger.log("thirdDialogShown");
            return window.getButton("Dispose").triggerClick();
          }
        })
        .run();

    logger.assertEquals("<log>" +
                        "  <trigger/>" +
                        "  <firstDialogShown/>" +
                        "  <click button='OK'/>" +
                        "  <secondDialogShown/>" +
                        "  <click button='OK'/>" +
                        "  <thirdDialogShown/>" +
                        "  <click button='Dispose'/>" +
                        "</log>");
  }

  public void testInterceptingAModalDialogWithAReturnedValue() throws Exception {
    WindowInterceptor
        .init(new Trigger() {
          public void run() {
            logger.log("triggerRun");
            JTextField textField = new JTextField();
            JDialog dialog = createModalDialog("aDialog");
            dialog.getContentPane().add(textField);
            dialog.setVisible(true);
            Assert.assertEquals("result", textField.getText());
            logger.log("done");
          }
        })
        .process(new WindowHandler() {
          public Trigger process(final Window window) throws Exception {
            window.getTextBox().setText("result");
            logger.log("windowProcessed");
            return new Trigger() {
              public void run() throws Exception {
                ComponentUtils.close(window);
              }
            };
          }
        })
        .run();
    logger.assertEquals("<log>" +
                        "  <triggerRun/>" +
                        "  <windowProcessed/>" +
                        "  <done/>" +
                        "</log>");
  }

  public void testModalInterceptionWithATriggerThatDisplaysNothing() throws Exception {
    checkAssertionFailedError(WindowInterceptor
                              .init(Trigger.DO_NOTHING)
                              .process(new WindowHandler() {
                                public Trigger process(Window window) {
                                  throw new AssertionFailedError("this should not be called");
                                }
                              }),
                              ShownInterceptionDetectionHandler.NO_WINDOW_WAS_SHOWN_ERROR_MESSAGE);
    assertEquals(0, UISpecDisplay.instance().getHandlerCount());
  }

  public void testInterceptingAModalDialogWithoutClosingItInTheHandler() throws Exception {
    checkAssertionFailedError(WindowInterceptor
      .init(new Trigger() {
        public void run() {
          logger.log("show");
          createAndShowModalDialog("aDialog");
          logger.log("closedByUISpec");
        }
      })
      .process(new WindowHandler() {
      public Trigger process(Window window) {
        logger.log("windowProcessed");
        return Trigger.DO_NOTHING;
      }
    }),
                              "Modal window 'aDialog' was not closed - make sure that " +
                              "setVisible(false) gets called by the production code");
    logger.assertEquals("<log>" +
                        "  <show/>" +
                        "  <windowProcessed/>" +
                        "  <closedByUISpec/>" +
                        "</log>");
  }

  public void testInterceptingAModalDialogShownFromAnotherThread() throws Exception {
    showModalDialogInThread(200, 100);
    logger.assertEquals("<log>" +
                        "  <triggerRun/>" +
                        "  <windowProcessed/>" +
                        "  <click button='Dispose'/>" +
                        "</log>");
  }

  public void testUsingDisposeInShowDialog() throws Exception {
    showDialogAndDispose();
    logger.assertEquals("<log>" +
                        "  <show/>" +
                        "  <click button='Dispose'/>" +
                        "  <closed/>" +
                        "</log>");
  }

  public void testInterceptionWorksEvenWhenInterceptionIsRunFromTheSwingThread() throws Exception {
    SwingUtilities.invokeAndWait(new Runnable() {
      public void run() {
        showDialogAndDispose();
      }
    });

    logger.assertEquals("<log>" +
                        "  <show/>" +
                        "  <click button='Dispose'/>" +
                        "  <closed/>" +
                        "</log>");
  }

  public void testInterceptingAModalDialogWithoutClosingItInTheWindowHandlerWhenRunFromTheSwingThread() throws Exception {
    SwingUtilities.invokeAndWait(new Runnable() {
      public void run() {
        checkAssertionFailedError(WindowInterceptor
                                  .init(new Trigger() {
                                    public void run() {
                                      logger.log("show");
                                      createAndShowModalDialog("aDialog");
                                      logger.log("closedByUISpec");
                                    }
                                  })
                                  .process(new WindowHandler() {
                                    public Trigger process(Window window) {
                                      logger.log("windowProcessed");
                                      return Trigger.DO_NOTHING;
                                    }
                                  }),
                                  "Modal window 'aDialog' was not closed - make sure that setVisible(false) gets " +
                                  "called by the production code");
      }
    });
    logger.assertEquals("<log>" +
                        "  <show/>" +
                        "  <windowProcessed/>" +
                        "  <closedByUISpec/>" +
                        "</log>");
  }

  public void testImbricationOfInterceptions() throws Exception {
    final JFrame frame = new JFrame("frame");

    JDialog firstDialog = new JDialog(frame, "first", true);
    addHideButton(firstDialog, "Dispose");
    addShowDialogButton(frame, "Show", firstDialog);

    JDialog secondDialog = new JDialog(frame, "second", true);
    addHideButton(secondDialog, "Dispose");
    addShowDialogButton(firstDialog, "Show", secondDialog);

    WindowInterceptor
        .init(new Trigger() {
          public void run() throws Exception {
            frame.setVisible(true);
          }
        })
        .process(new WindowHandler() {
          public Trigger process(Window frame) throws Exception {
            WindowInterceptor
                .init(new ClickButtonTrigger(frame, "Show"))
                .process(new WindowHandler() {
                  public Trigger process(Window firstWindow) throws Exception {
                    WindowInterceptor
                        .init(new ClickButtonTrigger(firstWindow, "Show"))
                        .process(new WindowHandler() {
                          public Trigger process(Window secondWindow) throws Exception {
                            return secondWindow.getButton("Dispose").triggerClick();
                          }
                        })
                        .run();
                    return firstWindow.getButton("Dispose").triggerClick();
                  }
                })
                .run();
            return Trigger.DO_NOTHING;
          }
        })
        .run();
    logger.assertEquals("<log>" +
                        "  <click button='Show'/>" +
                        "  <click button='Show'/>" +
                        "  <click button='Dispose'/>" +
                        "  <click button='Dispose'/>" +
                        "</log>");
  }

  public void testShowingTheSameDialogTwice() throws Exception {
    WindowInterceptor
        .init(new Trigger() {
          public void run() throws Exception {
            JDialog dialog = createModalDialog("aDialog");
            addHideButton(dialog, "Hide");
            dialog.setVisible(true);
            logger.log("step1");
            dialog.setVisible(true);
            logger.log("step2");
          }
        })
        .processWithButtonClick("Hide")
        .processWithButtonClick("Hide")
        .run();
    logger.assertEquals("<log>" +
                        "  <click button='Hide'/>" +
                        "  <step1/>" +
                        "  <click button='Hide'/>" +
                        "  <step2/>" +
                        "</log>");
  }

  public void testShowIsBlocked() throws Exception {
    WindowInterceptor
        .init(new Trigger() {
          public void run() throws Exception {
            JDialog dialog = createModalDialog("aDialog");
            addHideButton(dialog, "Hide");
            logger.log("beforeShow");
            dialog.setVisible(true);
            logger.log("afterShow");
          }
        })
        .process(new WindowHandler() {
          public Trigger process(Window window) throws Exception {
            Utils.sleep(100);
            logger.log("sleepCompleted");
            return window.getButton("Hide").triggerClick();
          }
        })
        .run();
    logger.assertEquals("<log>" +
                        "  <beforeShow/>" +
                        "  <sleepCompleted/>" +
                        "  <click button='Hide'/>" +
                        "  <afterShow/>" +
                        "</log>");
  }

  public void testAwtDialogsAreNotSupported() throws Exception {
    java.awt.Window window = new Dialog(new Frame());
    try {
      window.setVisible(true);
    }
    catch (Throwable e) {
      assertEquals("Dialogs of type '" + window.getClass().getName() + "' are not supported.",
                   e.getMessage());
    }
  }

  public void testErrorWhenTheInitialTriggerDisplaysNoWindow() throws Exception {
    checkAssertionFailedError(WindowInterceptor
                              .init(Trigger.DO_NOTHING)
                              .process(new WindowHandler() {
                                public Trigger process(Window window) {
                                  return Trigger.DO_NOTHING;
                                }
                              })

⌨️ 快捷键说明

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