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

📄 textboxfortextcomponenttest.java

📁 基于Junit的 功能和单元测试的的测试工具。只支持Swing.
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
    initWithHtmlTextPane();
    assertTrue(textBox.textIsEmpty());
    jTextComponent.setText("blah");
    jTextComponent.setText("");
    assertTrue(textBox.textIsEmpty());
  }

  public void testSetText() throws Exception {
    textBox.setText("new text");
    assertEquals("new text", jTextComponent.getText());
  }

  public void testSetTextChecksThatTheComponentIsEditable() throws Exception {
    textBox.setText("text");
    jTextComponent.setEditable(false);
    try {
      textBox.setText("new text");
      throw new AssertionFailureNotDetectedError();
    }
    catch (AssertionFailedError e) {
      assertEquals("The text box is not editable", e.getMessage());
    }
    assertEquals("text", jTextComponent.getText());
  }

  public void testInsertText() throws Exception {
    jTextComponent.setEditable(true);
    textBox.insertText("text", 0);
    assertEquals("text", textBox.getText());
    textBox.insertText("this is some ", 0);
    assertEquals("this is some text", textBox.getText());
    textBox.insertText("interesting ", 13);
    assertEquals("this is some interesting text", textBox.getText());
    textBox.insertText(", isn't it?", textBox.getText().length());
    assertEquals("this is some interesting text, isn't it?", textBox.getText());
  }

  public void testInsertTextAtABadPosition() throws Exception {
    textBox.setText("text");
    try {
      textBox.insertText("a", 10);
      throw new AssertionFailureNotDetectedError();
    }
    catch (AssertionFailedError e) {
      assertEquals("Position should be between 0 and 4", e.getMessage());
    }
  }

  public void testInsertTextDoesNotNotifyActionListeners() throws Exception {
    JTextField textField = new JTextField();
    DummyActionListener actionListener = new DummyActionListener();
    textField.addActionListener(actionListener);
    textBox = new TextBox(textField);

    textBox.insertText("text", 0);
    assertEquals(0, actionListener.getCallCount());
  }

  public void testInsertTextChecksThatTheComponentIsEditable() throws Exception {
    textBox.setText("text");
    jTextComponent.setEditable(false);
    try {
      textBox.insertText("new text", 0);
      throw new AssertionFailureNotDetectedError();
    }
    catch (AssertionFailedError e) {
      assertEquals("The text box is not editable", e.getMessage());
    }
    assertEquals("text", jTextComponent.getText());
  }

  public void testGetText() throws Exception {
    jTextComponent.setText("some text");
    assertEquals("some text", textBox.getText());
    textBox.setText("new text");
    assertEquals("new text", textBox.getText());
    textBox.setText("new <b>text</b>");
    assertEquals("new <b>text</b>", textBox.getText());
  }

  public void testSetTextNotifiesActionListenersForJTextField() throws Exception {
    JTextField textField = new JTextField();
    DummyActionListener actionListener = new DummyActionListener();
    textField.addActionListener(actionListener);
    textBox = new TextBox(textField);

    textBox.setText("text");
    assertEquals(1, actionListener.getCallCount());
  }

  public void testClickOnHyperlink() throws Exception {
    checkClickOnHyperlink("<html>blah blah<a href=\"http://www.junit.org\">link text</a>reblah</html>",
                          "link text",
                          "http://www.junit.org");
  }

  public void testClickOnHyperlinkAcceptsSubstrings() throws Exception {
    checkClickOnHyperlink("<html>blah blah<a href=\"http://www.junit.org\">link text</a>reblah</html>",
                          "link",
                          "http://www.junit.org");
  }

  public void testClickOnHyperLinkAcceptsLineSeparators() throws Exception {
    String link = "link text is very long so it will be on two lines";
    checkClickOnHyperlink("<html>blah blah<a href=\"http://www.junit.org\">" + link + "</a>reblah</html>",
                          link,
                          "http://www.junit.org");
  }

  public void testClickOnHyperlinkIsCaseInsensitive() throws Exception {
    checkClickOnHyperlink("<html>blah blah<a href=\"http://www.junit.org\">link text</a>reblah</html>",
                          "liNk tEXt",
                          "http://www.junit.org");
  }

  public void testClickOnHyperlinkGivesPriorityToExactMatches() throws Exception {
    checkClickOnHyperlink("<html>blah blah<a href=\"http://www.junit.org\">a link text</a>reblah" +
                          "blah blah<a href=\"http://www.apache.org\">link text</a>reblah</html>",
                          "link text",
                          "http://www.apache.org");
  }

  public void testClickOnUnknownHyperlink() throws Exception {
    checkClickOnHyperlinkError("<html>blah blah<a href=\"http://www.junit.org\">a link text</a>reblah" +
                               "blah blah<a href=\"http://www.apache.org\">link text</a>reblah</html>",
                               "unknown",
                               "Hyperlink 'unknown' not found");
  }

  public void testClickOnHyperlinkWithAmbiguity() throws Exception {
    checkClickOnHyperlinkError("<html>blah blah<a href=\"http://www.junit.org\">a link text</a>reblah" +
                               "blah blah<a href=\"http://www.apache.org\">another link text</a>reblah</html>",
                               "link text",
                               "Ambiguous command - found several hyperlinks matching 'link text'");
  }

  public void testClickOnHyperLinkWithABadTextComponentFails() throws Exception {
    final TextBox textBox = new TextBox(new JTextArea());

    checkAssertionFailedError(new Functor() {
      public void run() throws Exception {
        textBox.clickOnHyperlink("toto");
      }
    }, "This component does not support hyperlinks.");

    checkAssertionFailedError(new Functor() {
      public void run() throws Exception {
        textBox.triggerClickOnHyperlink("toto").run();
      }
    }, "This component does not support hyperlinks.");
  }

  public void testPressingPrintableKeyAddsItToText() throws Exception {
    JTextField textField = new JTextField();
    TextBox textBox = new TextBox(textField);
    textBox.pressKey(Key.A);
    assertTrue(textBox.textEquals("A"));
    textBox.pressKey(Key.B);
    assertTrue(textBox.textEquals("AB"));
    textBox.pressKey(Key.C);
    assertTrue(textBox.textEquals("ABC"));
  }

  public void testPressingPrintableKeyInANonEmptyTextBoxStartsAtPosition0() throws Exception {
    JTextField textField = new JTextField("text");
    TextBox textBox = new TextBox(textField);
    textBox.pressKey(Key.A);
    assertTrue(textBox.textEquals("Atext"));
  }

  private JTextPane createTextPane(String html) {
    JTextPane textPane = new JTextPane();
    textPane.setContentType("text/html; charset=EUC-JP");
    textPane.setText(html);
    return textPane;
  }

  private void checkClickOnHyperlink(String html, String link, String expectedTarget) throws Exception {
    JTextPane textPane = createTextPane(html);
    DummyHyperlinkListener listener = new DummyHyperlinkListener();
    textPane.addHyperlinkListener(listener);
    TextBox textComponent = new TextBox(textPane);
    textComponent.clickOnHyperlink(link);
    assertEquals(1, listener.getCallCount());
    assertEquals(expectedTarget, listener.getLastEvent().getDescription());

    listener.reset();
    textComponent.triggerClickOnHyperlink(link).run();
    assertEquals(1, listener.getCallCount());
    assertEquals(expectedTarget, listener.getLastEvent().getDescription());
  }

  private void checkClickOnHyperlinkError(String html, final String link, String errorMessage) throws Exception {
    final TextBox textComponent = new TextBox(createTextPane(html));
    checkAssertionFailedError(new Functor() {
      public void run() throws Exception {
        textComponent.clickOnHyperlink(link);
      }
    }, errorMessage);

    checkAssertionFailedError(new Functor() {
      public void run() throws Exception {
        textComponent.triggerClickOnHyperlink(link).run();
      }
    }, errorMessage);
  }

  private static class DummyHyperlinkListener implements HyperlinkListener {
    int callCount;
    HyperlinkEvent lastEvent;

    public void hyperlinkUpdate(HyperlinkEvent event) {
      callCount++;
      this.lastEvent = event;
    }

    public int getCallCount() {
      return callCount;
    }

    public HyperlinkEvent getLastEvent() {
      return lastEvent;
    }

    public void reset() {
      callCount = 0;
      lastEvent = null;
    }
  }
}

⌨️ 快捷键说明

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