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

📄 popupmenuinterceptor.java

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

import junit.framework.AssertionFailedError;
import org.uispec4j.*;
import org.uispec4j.assertion.Assertion;
import org.uispec4j.assertion.UISpecAssert;
import org.uispec4j.interception.handlers.InterceptionHandler;
import org.uispec4j.interception.toolkit.UISpecDisplay;
import org.uispec4j.utils.TriggerRunner;

import javax.swing.*;

/**
 * Interceptor for (usually right-click triggered) pop-up menus.<p>
 * For instance, in the following code snippedt we right-click in the (2,4) cell of a table,
 * then click on the "Copy" item found in the popped-up menu:
 * <pre><code>
 * PopupMenuInterceptor
 *   .run(table.triggerRightClick(2, 4))
 *   .getSubMenu("Copy")
 *   .click();
 * </code></pre>
 *
 * @see <a href="http://www.uispec4j.org/interception.html">Intercepting windows</a>
 */
public class PopupMenuInterceptor {

  /**
   * Runs the given trigger and returns the intercepted popup menu.<p/>
   * This method will wait for the popup to be shown, up to a time specified with
   * {@link UISpec4J#setWindowInterceptionTimeLimit(long)}.
   * @throws AssertionFailedError if no popup is shown before the timeout expires.
   */
  public static MenuItem run(final Trigger trigger) {
    PopupHandler interceptor = new PopupHandler();
    final UISpecDisplay display = UISpecDisplay.instance();
    try {
      display.add(interceptor);
      display.setCurrentPopup(null);
      TriggerRunner.runInSwingThread(trigger);
      UISpecAssert.waitUntil("No popup was shown",
                             new Assertion() {
                               public void check() throws Exception {
                                 if (display.getCurrentPopup() == null) {
                                   throw new Exception("No popup shown");
                                 };
                               }
                             },
                             UISpec4J.getWindowInterceptionTimeLimit());
      return new MenuItem(display.getCurrentPopup());
    }
    finally {
      display.remove(interceptor);
    }
  }

  private static class PopupHandler implements InterceptionHandler {
    MenuItem menu;

    public void process(Window window) {
      try {
        this.menu = new MenuItem((JPopupMenu)window.findSwingComponent(JPopupMenu.class));
      }
      catch (ItemNotFoundException e) {
      }
      catch (ComponentAmbiguityException e) {
      }
    }
  }
}

⌨️ 快捷键说明

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