📄 textboxforhtmltestcomponenttest.java
字号:
editorScrollPane.setPreferredSize(new Dimension(250, 145));
editorScrollPane.setMinimumSize(new Dimension(10, 10));
clickOnHyperLink(editorPane, "about the manifesto", "history.html");
}
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 testClickOnHyperLinkWaitsForTheCompletePageLoad() throws Exception {
String content1 = "<html>\n" +
" <head>\n" +
" <title>Subject 1</title>\n" +
" </head>\n" +
" <body>\n" +
" <p>blabla</p>" +
" <a href=\"file2.html\">Subject 2</a>\n" +
" </body>\n" +
"</html>";
String content2 = "<html>\n" +
" <head>\n" +
" <title>Subject 2</title>\n" +
" </head>\n" +
" <body>\n" +
" <p>blabla</p>" +
" <a href=\"file1.html\">Subject 1</a>\n" +
" </body>\n" +
"</html>";
File file1 = FileTestUtils.dumpStringToFile("file1.html", content1);
File file2 = FileTestUtils.dumpStringToFile("file2.html", content2);
final JEditorPane textComponent = createTextPaneFromUrl(file1.toURL());
checkSwitchBetweenPages(textComponent, content1, content2);
File archiveFile = FileTestUtils.createZipArchive(FileTestUtils.getFile("archive.zip"), new File[]{file1, file2});
textComponent.setPage(new URL("jar:" + archiveFile.toURL() + "!/file1.html"));
checkSwitchBetweenPages(textComponent, content1, content2);
}
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 testClickOnHyperlinkAcceptsAttributesOnATag() throws Exception {
checkClickOnHyperlink("<html>blah blah<a title=\"JUNIT \" " +
" href=\"http://www.junit.org\" " +
" name = \"junit hyperlink\">link text</a>reblah</html>",
"link text",
"http://www.junit.org");
}
public void testClickOnHyperlinkWithBigHtml() throws Exception {
StringBuffer htmlText = new StringBuffer("<html>" +
" <head>" +
" <meta name=\"UISpec4J\" lang=\"en\" content=\"text/HTML; charset=utf-8\"/>" +
" <title name=\"Universal rules\"/>" +
" </head>" +
" <body>" +
" Universal <b>rules</b>:" +
" <a href=\"http://www.junit.org\">link text</a>");
for (int i = 0; i < 5000; i++) {
htmlText.append("<a href=\"http://www.otherlink.org\">other link text")
.append(i)
.append("</a>");
}
htmlText.append("</body></html>");
checkClickOnHyperlink(htmlText.toString(),
"link text",
"http://www.junit.org");
}
private void checkSwitchBetweenPages(JTextComponent textComponent, String content1, String content2) {
TextBox textBox = new TextBox(textComponent);
assertTrue(textBox.htmlEquals(content1));
textBox.clickOnHyperlink("Subject 2");
textBox.clickOnHyperlink("Subject 1");
waitUntil(textBox.htmlEquals(content1), 1000);
textBox.clickOnHyperlink("Subject 2");
assertTrue(textBox.htmlEquals(content2));
}
private JTextPane createTextPaneFromUrl(URL url) throws IOException {
final JTextPane jTextPane = new JTextPane();
jTextPane.setContentType("text/html");
jTextPane.setPage(url);
jTextPane.addHyperlinkListener(new HyperlinkListener() {
public void hyperlinkUpdate(HyperlinkEvent event) {
try {
jTextPane.setPage(event.getURL());
}
catch (IOException e) {
}
}
});
return jTextPane;
}
private void checkClickOnHyperlink(String html, String link, String expectedTarget) throws Exception {
JTextPane textPane = createHtmlTextPane(html);
clickOnHyperLink(textPane, link, expectedTarget);
}
private void clickOnHyperLink(JEditorPane editorPane, String link, String expectedTarget) throws Exception {
DummyHyperlinkListener listener = new DummyHyperlinkListener();
editorPane.addHyperlinkListener(listener);
TextBox textComponent = new TextBox(editorPane);
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(createHtmlTextPane(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;
}
}
private void initWithHtmlTextPane() {
JTextPane pane = new JTextPane();
pane.setContentType("text/html; charset=EUC-JP");
init(pane);
}
private void init(JTextComponent swingComponent) {
jTextComponent = swingComponent;
jTextComponent.setName("myText");
createTextBox("");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -