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

📄 buttontestcase.java

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

import junit.framework.AssertionFailedError;
import org.uispec4j.assertion.Assertion;
import org.uispec4j.assertion.UISpecAssert;
import org.uispec4j.utils.AssertionFailureNotDetectedError;
import org.uispec4j.utils.Chrono;
import org.uispec4j.utils.DummyActionListener;
import org.uispec4j.utils.Utils;

public abstract class ButtonTestCase extends UIComponentTestCase {

  protected abstract AbstractButton getButton();

  protected abstract javax.swing.AbstractButton getSwingButton();

  protected UIComponent createComponent() {
    return getButton();
  }

  public void testEnableDisable() throws Exception {
    checkEnabled(true);
    checkEnabled(false);
  }

  private void checkEnabled(boolean enabled) {
    getSwingButton().setEnabled(enabled);
    Assertion enabledAssertion = getButton().isEnabled();
    assertTrue((enabled) ? enabledAssertion : not(enabledAssertion));
  }

  public void testCheckText() throws Exception {
    getSwingButton().setText("text");
    assertTrue(getButton().textEquals("text"));
    try {
      assertTrue(getButton().textEquals("error"));
      throw new AssertionFailureNotDetectedError();
    }
    catch (AssertionFailedError e) {
    }
  }

  public void testCheckTextTrimsTheActualButtonText() throws Exception {
    getSwingButton().setText(" text  ");
    assertTrue(getButton().textEquals("text"));
  }

  public void testActivateIsRejectedIfTheButtonIsDisabled() throws Exception {
    getSwingButton().setEnabled(false);
    try {
      getButton().click();
      throw new AssertionFailureNotDetectedError();
    }
    catch (AssertionFailedError e) {
      assertEquals("The button is not enabled, it cannot be activated", e.getMessage());
    }
  }

  public void testClick() throws Exception {
    DummyActionListener listener = new DummyActionListener();
    getSwingButton().addActionListener(listener);
    getButton().click();
    assertEquals(1, listener.getCallCount());
  }

  public void testTriggerClick() throws Exception {
    DummyActionListener listener = new DummyActionListener();
    getSwingButton().addActionListener(listener);
    getButton().triggerClick().run();
    assertEquals(1, listener.getCallCount());
  }

  public void testClickTakesLessTimeThanWithDefaultSwingCalls() throws Exception {
    Chrono chrono = Chrono.start();
    getButton().click();
    chrono.assertElapsedTimeLessThan(30);
  }

  public void testWaitForEnabledState() throws Exception {
    AbstractButton button = getButton();
    final javax.swing.AbstractButton swingButton = (javax.swing.AbstractButton)button.getAwtComponent();
    swingButton.setEnabled(false);
    Thread thread = new Thread(new Runnable() {
      public void run() {
        Utils.sleep(10);
        swingButton.setEnabled(true);
      }
    });
    thread.start();
    assertFalse(button.isEnabled());
    UISpecAssert.waitUntil(button.isEnabled(), 30);
    assertTrue(button.isEnabled());
  }
}

⌨️ 快捷键说明

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