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

📄 spinner.java

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

import junit.framework.Assert;
import org.uispec4j.assertion.Assertion;
import org.uispec4j.finder.ComponentMatcher;

import javax.swing.JSpinner;
import java.awt.Component;

/**
 * Wrapper for JSpinner components.
 */
public class Spinner extends AbstractUIComponent {
  public static final String TYPE_NAME = "spinner";
  public static final Class[] SWING_CLASSES = {JSpinner.class};

  protected JSpinner jSpinner;

  public Spinner(JSpinner jSpinner) {
    this.jSpinner = jSpinner;
  }

  public Component getAwtComponent() {
    return jSpinner;
  }

  public String getDescriptionTypeName() {
    return TYPE_NAME;
  }

  /**
   * Checks that the spinner displays the given value
   */
  public Assertion valueEquals(final Object expectedValue) {
    return new Assertion() {
      public void check() throws Exception {
        Assert.assertEquals(expectedValue, jSpinner.getValue());
      }
    };
  }

  /**
   * Checks that the previous value is the given value
   */
  public Assertion previousValueEquals(final Object expectedPreviousValue) {
    return new Assertion() {
      public void check() throws Exception {
        Object previousValue = jSpinner.getPreviousValue();
        if (previousValue == null) {
          Assert.fail("No previous value from the start");
        }
        Assert.assertEquals(expectedPreviousValue, previousValue);
      }
    };
  }

  /**
   * Checks that the next value is the given value
   */
  public Assertion nextValueEquals(final Object expectedNextValue) {
    return new Assertion() {
      public void check() throws Exception {
        Object nextValue = jSpinner.getNextValue();
        if (nextValue == null) {
          Assert.fail("No previous value from the end");
        }
        Assert.assertEquals(expectedNextValue, nextValue);
      }
    };
  }

  /**
   * Changes the displayed text<p/>
   * This method will throw an exception if the value is not allowed in the spinner.<p/>
   */
  public void setValue(Object value) throws ItemNotFoundException {
    try {
      jSpinner.setValue(value);
    }
    catch (IllegalArgumentException e) {
      throw new ItemNotFoundException("'" + value + "' not found");
    }
  }

  /**
   * Clicks on the button for next value
   */
  public void clickForNextValue() {
    try {
      jSpinner.setValue(jSpinner.getNextValue());
    }
    catch (IllegalArgumentException e) {
      // Ignore it, as in the UI
    }
  }

  /**
   * Clicks on the button for previous value
   */
  public void clickForPreviousValue() {
    try {
      jSpinner.setValue(jSpinner.getPreviousValue());
    }
    catch (IllegalArgumentException e) {
      // Ignore it, as in the UI
    }
  }

  static ComponentMatcher getSpinnerMatcherByModel(final Class spinnerModelClass) {
    return new ComponentMatcher() {
      public boolean matches(Component component) {
        if (component instanceof JSpinner) {
          JSpinner jSpinner = (JSpinner) component;
          return jSpinner.getModel().getClass().isAssignableFrom(spinnerModelClass);
        }
        return false;
      }
    };
  }
}

⌨️ 快捷键说明

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