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

📄 textboxfortextcomponenttest.java

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

import junit.framework.AssertionFailedError;
import org.uispec4j.utils.AssertionFailureNotDetectedError;
import org.uispec4j.utils.DummyActionListener;
import org.uispec4j.utils.Functor;
import org.uispec4j.utils.UIComponentFactory;
import org.uispec4j.xml.XmlAssert;

import javax.swing.*;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.text.JTextComponent;

public class TextBoxForTextComponentTest extends TextBoxComponentTestCase {
  private JTextComponent jTextComponent;

  protected void setUp() throws Exception {
    super.setUp();
    init(new JTextArea());
  }

  private void init(JTextComponent swingComponent) {
    jTextComponent = swingComponent;
    jTextComponent.setName("myText");
    createTextBox("");
  }

  protected void createTextBox(String text) {
    textBox = new TextBox(jTextComponent);
    textBox.setText(text);
  }

  private void initWithHtmlTextPane() {
    JTextPane pane = new JTextPane();
    pane.setContentType("text/html; charset=EUC-JP");
    init(pane);
  }

  public void testGetComponentTypeName() throws Exception {
    assertEquals("textBox", UIComponentFactory.createUIComponent(new JTextField()).getDescriptionTypeName());
  }

  public void testGetDescription() throws Exception {
    XmlAssert.assertEquivalent("<textBox name='myText'/>", textBox.getDescription());
  }

  public void testFactory() throws Exception {
    checkFactory(new JTextArea(), TextBox.class);
    checkFactory(new JTextPane(), TextBox.class);
    checkFactory(new JEditorPane(), TextBox.class);
    checkFactory(new JTextField(), TextBox.class);
  }

  public void testAssertTextEquals() throws Exception {
    assertTrue(textBox.textEquals(""));
    jTextComponent.setText("some text");
    assertTrue(textBox.textEquals("some text"));
    assertFalse(textBox.textEquals(""));
    try {
      assertTrue(textBox.textEquals("error"));
      throw new AssertionFailureNotDetectedError();
    }
    catch (AssertionFailedError e) {
      assertEquals("expected:<error> but was:<some text>", e.getMessage());
    }
  }

  public void testAssertTextEqualsWithHtml() throws Exception {
    initWithHtmlTextPane();
    String text = "Universal <b>rules</b>:" +
                  "<ul>" +
                  "<li style=\"margin-top: 0\" align=\"center\">a &lt; b</li>" +
                  "<li>2 &gt; 1</li>" +
                  "</ul>";
    textBox.setText(text);
    assertTrue(textBox.textEquals("Universal rules: a < b 2 > 1"));
    try {
      assertTrue(textBox.textEquals("Universal rules: a < b 2 > 1, seb is the best"));
      throw new AssertionFailureNotDetectedError();
    }
    catch (AssertionFailedError e) {
    }
  }

  public void testAssertHtmlEquals() throws Exception {
    initWithHtmlTextPane();
    String text = "Universal <b>rules</b>:" +
                  "<ul>" +
                  "<li>a &lt; b</li>" +
                  "<li>2 &gt; 1</li>" +
                  "</ul>";
    textBox.setText(text);
    assertTrue(textBox.htmlEquals(text));
    try {
      assertTrue(textBox.htmlEquals("Universal <b>rules</b>:" +
                                    "<ul>" +
                                    "<li>a &lt; b</li>" +
                                    "</ul>"));
      throw new AssertionFailureNotDetectedError();
    }
    catch (AssertionFailedError e) {
    }
  }

  public void testAssertTextContains() throws Exception {
    jTextComponent.setText("some text");
    assertTrue(textBox.textContains("some"));
    try {
      assertTrue(textBox.textContains("error"));
      throw new AssertionFailureNotDetectedError();
    }
    catch (AssertionFailedError e) {
      assertEquals("The component text does not contain 'error' - actual content is:some text",
                   e.getMessage());
    }
  }

  public void testAssertTextContainsWithHtml() throws Exception {
    initWithHtmlTextPane();
    String text = "My name is <b>Bond</b>";
    textBox.setText(text);
    assertTrue(textBox.textContains("Bond"));
    try {
      assertTrue(textBox.textContains("error"));
      throw new AssertionFailureNotDetectedError();
    }
    catch (AssertionFailedError e) {
      assertEquals("The component text does not contain 'error' - actual content is:<html>\n" +
                   "  <head>\n" +
                   "  </head>\n" +
                   "  <body>My name is <b>Bond</b></body>\n" +
                   "</html>\n",
                   e.getMessage());
    }
  }

  public void testAssertTextEqualsWithEmptyStringIsTheSameAsAssertTextIsEmpty() throws Exception {
    initWithHtmlTextPane();
    assertTrue(textBox.textEquals(""));
    jTextComponent.setText("blah");
    jTextComponent.setText("");
    assertTrue(textBox.textEquals(""));
  }

  public void testAssertTextContainsHandlesHtmlLineBreaksAndFormatting() throws Exception {
    initWithHtmlTextPane();
    StringBuffer buffer = new StringBuffer();
    for (int i = 0; i < 20; i++) {
      buffer.append("blah ");
    }
    String text = buffer.toString();
    textBox.setText(text);
    assertTrue(textBox.textContains(text));
  }

  public void testAssertTextDoesNotContain() throws Exception {
    jTextComponent.setText("some text");
    assertTrue(textBox.textDoesNotContain("xxx"));
    try {
      assertTrue(textBox.textDoesNotContain("some"));
      throw new AssertionFailureNotDetectedError();
    }
    catch (AssertionFailedError e) {
      assertEquals("The component text should not contain 'some' - actual content is:some text",
                   e.getMessage());
    }
  }

  public void testAssertTextIsEditable() throws Exception {
    jTextComponent.setEditable(true);
    assertTrue(textBox.isEditable());
    jTextComponent.setEditable(false);
    assertFalse(textBox.isEditable());
  }

  public void testAssertEmptyWithPlainText() throws Exception {
    jTextComponent.setText("");
    assertTrue(textBox.textIsEmpty());
    jTextComponent.setText("a");
    try {
      assertTrue(textBox.textIsEmpty());
      throw new AssertionFailureNotDetectedError();
    }
    catch (AssertionFailedError e) {
      assertEquals("Text should be empty but contains: a", e.getMessage());
    }
  }

  public void testAssertEmptyWithHtml() throws Exception {
    initWithHtmlTextPane();
    assertTrue(textBox.textIsEmpty());
    jTextComponent.setText("");
    assertTrue(textBox.textIsEmpty());
    jTextComponent.setText("a");
    try {
      assertTrue(textBox.textIsEmpty());
      throw new AssertionFailureNotDetectedError();
    }
    catch (AssertionFailedError e) {
      assertEquals("Text should be empty but contains: <html>\n" +
                   "  <head>\n" +
                   "    \n" +
                   "  </head>\n" +
                   "  <body>\n" +
                   "    a\n" +
                   "  </body>\n" +
                   "</html>\n",
                   e.getMessage());
    }

    jTextComponent.setText("<html>\n" +
                           "  <head>\n" +
                           "\n" +
                           "  </head>\n" +
                           "  <body>\n" +
                           "    <p>\n" +
                           "      \n" +
                           "    </p>\n" +
                           "  </body>\n" +
                           "</html>\n" +
                           "\n" +
                           "");
    assertTrue(textBox.textIsEmpty());

    jTextComponent.setText("<html><p></html>");
    jTextComponent.setText("");
    assertTrue(textBox.textIsEmpty());
  }

  public void testAssertEmptyAfterReset() throws Exception {

⌨️ 快捷键说明

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