Refactor some tests in HtmlConverterTest

This commit is contained in:
cketti 2018-01-11 20:48:46 +01:00
parent 5cbd0be736
commit 9b7a8cd8ab

View file

@ -9,11 +9,9 @@ import java.io.IOException;
import com.fsck.k9.K9; import com.fsck.k9.K9;
import com.fsck.k9.K9.Theme; import com.fsck.k9.K9.Theme;
import com.fsck.k9.K9RobolectricTestRunner; import com.fsck.k9.K9RobolectricTestRunner;
import com.fsck.k9.mail.internet.Viewable.Html;
import org.apache.commons.io.IOUtils; import org.apache.commons.io.IOUtils;
import org.jsoup.Jsoup; import org.jsoup.Jsoup;
import org.jsoup.nodes.Document; import org.jsoup.nodes.Document;
import org.jsoup.select.Elements;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
@ -315,47 +313,54 @@ public class HtmlConverterTest {
@Test @Test
public void wrapMessageContent_addsViewportMetaElement() { public void wrapMessageContent_addsViewportMetaElement() {
Document document = Jsoup.parse(HtmlConverter.wrapMessageContent("Some text")); String html = HtmlConverter.wrapMessageContent("Some text");
Elements metaElements = document.head().getElementsByTag("meta");
assertEquals(1, metaElements.size()); assertHtmlContainsElement(html, "head > meta[name=viewport]");
assertEquals("viewport", metaElements.get(0).attributes().get("name"));
} }
@Test @Test
public void wrapMessageContent_setsDirToAuto() { public void wrapMessageContent_setsDirToAuto() {
Document document = Jsoup.parse(HtmlConverter.wrapMessageContent("Some text")); String html = HtmlConverter.wrapMessageContent("Some text");
String direction = document.getElementsByTag("html").get(0).attributes().get("dir");
assertEquals("auto", direction); assertHtmlContainsElement(html, "html[dir=auto]");
} }
@Test @Test
public void wrapMessageContent_addsPreCSS() { public void wrapMessageContent_addsPreCSS() {
K9.setK9MessageViewThemeSetting(Theme.LIGHT); K9.setK9MessageViewThemeSetting(Theme.LIGHT);
Document document = Jsoup.parse(HtmlConverter.wrapMessageContent("Some text")); String html = HtmlConverter.wrapMessageContent("Some text");
Elements styleElements = document.head().getElementsByTag("style");
assertEquals(1, styleElements.size()); assertHtmlContainsElement(html, "head > style");
} }
@Test @Test
public void wrapMessageContent_whenDarkMessageViewTheme_addsDarkThemeCSS() { public void wrapMessageContent_whenDarkMessageViewTheme_addsDarkThemeCSS() {
K9.setK9MessageViewThemeSetting(Theme.DARK); K9.setK9MessageViewThemeSetting(Theme.DARK);
Document document = Jsoup.parse(HtmlConverter.wrapMessageContent("Some text")); String html = HtmlConverter.wrapMessageContent("Some text");
Elements styleElements = document.head().getElementsByTag("style");
assertEquals(2, styleElements.size()); assertHtmlContainsElement(html, "head > style", 2);
} }
@Test @Test
public void wrapMessageContent_putsMessageContentInBody() { public void wrapMessageContent_putsMessageContentInBody() {
String content = "Some text"; String content = "Some text";
Document document = Jsoup.parse(HtmlConverter.wrapMessageContent(content));
String actualContent = document.getElementsByTag("body").text();
assertEquals(content, actualContent); String html = HtmlConverter.wrapMessageContent(content);
assertEquals(content, Jsoup.parse(html).body().text());
}
private void assertHtmlContainsElement(String html, String cssQuery) {
assertHtmlContainsElement(html, cssQuery, 1);
}
private void assertHtmlContainsElement(String html, String cssQuery, int numberOfExpectedOccurrences) {
Document document = Jsoup.parse(html);
int numberOfFoundElements = document.select(cssQuery).size();
assertEquals("Expected to find '" + cssQuery + "' " + numberOfExpectedOccurrences + " time(s) in:\n" + html,
numberOfExpectedOccurrences, numberOfFoundElements);
} }
} }