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

📄 basichandler.java

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

import junit.framework.AssertionFailedError;
import org.uispec4j.TextBox;
import org.uispec4j.Trigger;
import org.uispec4j.UIComponent;
import org.uispec4j.Window;
import org.uispec4j.interception.handlers.InterceptionHandler;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Ready-to-use window interception handler, designed for simple dialogs. <p>
 * Sample usage:
 * <pre><code>
 * WindowInterceptor
 *   .init(panel.getButton("Change value").triggerClick())
 *   .process(BasicHandler.init()
 *            .assertContainsText("Enter new value")
 *            .setText("13")
 *            .triggerButtonClick("OK"))
 *   .run();
 * </code></pre>
 * The last call must be {@link #triggerButtonClick(String)}, which returns the created WindowHandler.
 *
 * @see WindowInterceptor
 */
public class BasicHandler {

  private List handlers = new ArrayList();

  /**
   * Starts the definition of the handler.
   */
  public static BasicHandler init() {
    return new BasicHandler();
  }

  private BasicHandler() {
  }

  /**
   * Checks that there is a text component in the dialog displaying the given text.
   */
  public BasicHandler assertContainsText(final String text) {
    handlers.add(new InterceptionHandler() {
      public void process(Window window) {
        UIComponent component = window.findUIComponent(TextBox.class, text);
        if (component == null) {
          throw new AssertionFailedError("Text not found: " + text);
        }
      }
    });
    return this;
  }

  /**
   * Clicks on a button given its displayed label. This method will throw an exception if 
   * no button with this text is found.
   */
  public BasicHandler clickButton(final String buttonName) {
    handlers.add(new InterceptionHandler() {
      public void process(Window window) {
        window.getButton(buttonName).click();
      }
    });
    return this;
  }

  /**
   * Enters a text value, provided that there is only one input text field in the dialog.
   */
  public BasicHandler setText(final String text) {
    handlers.add(new InterceptionHandler() {
      public void process(Window window) {
        window.getInputTextBox().setText(text);
      }
    });
    return this;
  }

  /**
   * Returns the created window handler with a trigger for clicking on a button.
   */
  public WindowHandler triggerButtonClick(final String buttonName) {
    return new WindowHandler() {
      public Trigger process(Window window) throws Exception {
        for (Iterator iterator = handlers.iterator(); iterator.hasNext();) {
          InterceptionHandler handler = (InterceptionHandler)iterator.next();
          handler.process(window);
        }
        return window.getButton(buttonName).triggerClick();
      }
    };
  }
}

⌨️ 快捷键说明

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