triggerrunner.java

来自「基于Junit的 功能和单元测试的的测试工具。只支持Swing.」· Java 代码 · 共 60 行

JAVA
60
字号
package org.uispec4j.utils;

import org.uispec4j.Trigger;
import org.uispec4j.interception.toolkit.UISpecDisplay;

import javax.swing.*;
import java.lang.reflect.InvocationTargetException;

public class TriggerRunner {
  public static void runInCurrentThread(Trigger trigger) {
    try {
      trigger.run();
    }
    catch (Exception e) {
      throw new RuntimeException(e);
    }
  }

  public static void runInSwingThread(final Trigger trigger) {
    if (SwingUtilities.isEventDispatchThread()) {
      runInCurrentThread(trigger);
    }
    else {
      final ExceptionContainer container = new ExceptionContainer();
      try {
        SwingUtilities.invokeAndWait(new Runnable() {
          public void run() {
            try {
              runInCurrentThread(trigger);
            }
            catch (Throwable e) {
              container.set(e);
            }
          }
        });
      }
      catch (InterruptedException e) {
        throw new RuntimeException(e.getCause());
      }
      catch (InvocationTargetException e) {
        throw new RuntimeException(e.getCause());
      }
      container.rethrowIfNeeded();
    }
  }

  public static void runInUISpecThread(final Trigger trigger) {
    UISpecDisplay.instance().runInNewThread(new Runnable() {
      public void run() {
        try {
          trigger.run();
        }
        catch (Throwable e) {
          UISpecDisplay.instance().store(e);
        }
      }
    });
  }
}

⌨️ 快捷键说明

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