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

📄 abstracttextboxhandlerfortextcomponent.java

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

import junit.framework.Assert;
import org.uispec4j.assertion.Assertion;
import org.uispec4j.assertion.UISpecAssert;
import org.uispec4j.utils.KeyUtils;

import javax.swing.*;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;
import javax.swing.text.JTextComponent;
import java.awt.*;

abstract class AbstractTextBoxHandlerForTextComponent implements TextBox.Handler {
  protected JTextComponent jTextComponent;

  public AbstractTextBoxHandlerForTextComponent(JTextComponent textComponent) {
    this.jTextComponent = textComponent;
  }

  public Component getAwtComponent() {
    return jTextComponent;
  }

  public void setText(String text) {
    UISpecAssert.assertTrue(isEditable());
    jTextComponent.setText(text);
    if (jTextComponent instanceof JTextField) {
      ((JTextField)jTextComponent).postActionEvent();
    }
  }

  public void insertText(String text, int position) {
    UISpecAssert.assertTrue(isEditable());
    Document document = jTextComponent.getDocument();
    try {
      document.insertString(position, text, document.getDefaultRootElement().getAttributes());
    }
    catch (BadLocationException e) {
      Assert.fail("Position should be between 0 and " + document.getLength());
    }
  }

  public String getText() {
    return jTextComponent.getText();
  }

  public Assertion textContains(final String text) {
    return new Assertion() {
      public void check() {
        String actual = jTextComponent.getText().replaceAll("\n    ", "").replaceAll("\n  </body>", "</body>");
        Assert.assertTrue("The component text does not contain '" + text + "' - actual content is:" + actual,
                          actual.indexOf(text.trim()) >= 0);
      }
    };
  }

  public Assertion textDoesNotContain(final String text) {
    return new Assertion() {
      public void check() {
        String actual = jTextComponent.getText();
        Assert.assertTrue("The component text should not contain '" + text +
                          "' - actual content is:" + actual,
                          actual.indexOf(text) < 0);
      }
    };
  }

  public Assertion isEditable() {
    return new Assertion() {
      public void check() {
        Assert.assertTrue("The text box is not editable", jTextComponent.isEditable());
      }
    };
  }

  public void pressKey(Key key) {
    KeyUtils.pressKey(jTextComponent, key);
    if (jTextComponent.isEditable() && key.isPrintable()) {
      Document document = jTextComponent.getDocument();
      try {
        int position = jTextComponent.getCaretPosition();
        document.insertString(position,
                              new Character((char)key.getCode()).toString(),
                              document.getDefaultRootElement().getAttributes());
        jTextComponent.moveCaretPosition(document.getEndPosition().getOffset() - 1);
//        jTextComponent.moveCaretPosition(position + 1);
      }
      catch (BadLocationException e) {
        Assert.fail(e.getMessage());
      }
    }
  }

  public Assertion iconEquals(Icon icon) {
    throw new UnsupportedOperationException("assertIconEquals is not supported for JTextComponent components");
  }
}

⌨️ 快捷键说明

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