Refactor some tests in HtmlConverterTest
@@ -9,11 +9,9 @@ import java.io.IOException;
import com.fsck.k9.K9;
import com.fsck.k9.K9.Theme;
import com.fsck.k9.K9RobolectricTestRunner;
-import com.fsck.k9.mail.internet.Viewable.Html;
import org.apache.commons.io.IOUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
-import org.jsoup.select.Elements;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@@ -315,47 +313,54 @@ public class HtmlConverterTest {
@Test
public void wrapMessageContent_addsViewportMetaElement() {
- Document document = Jsoup.parse(HtmlConverter.wrapMessageContent("Some text"));
- Elements metaElements = document.head().getElementsByTag("meta");
+ String html = HtmlConverter.wrapMessageContent("Some text");
- assertEquals(1, metaElements.size());
- assertEquals("viewport", metaElements.get(0).attributes().get("name"));
+ assertHtmlContainsElement(html, "head > meta[name=viewport]");
}
@Test
public void wrapMessageContent_setsDirToAuto() {
- Document document = Jsoup.parse(HtmlConverter.wrapMessageContent("Some text"));
- String direction = document.getElementsByTag("html").get(0).attributes().get("dir");
+ String html = HtmlConverter.wrapMessageContent("Some text");
- assertEquals("auto", direction);
+ assertHtmlContainsElement(html, "html[dir=auto]");
}
@Test
public void wrapMessageContent_addsPreCSS() {
K9.setK9MessageViewThemeSetting(Theme.LIGHT);
- Document document = Jsoup.parse(HtmlConverter.wrapMessageContent("Some text"));
- Elements styleElements = document.head().getElementsByTag("style");
+ String html = HtmlConverter.wrapMessageContent("Some text");
- assertEquals(1, styleElements.size());
+ assertHtmlContainsElement(html, "head > style");
}
@Test
public void wrapMessageContent_whenDarkMessageViewTheme_addsDarkThemeCSS() {
K9.setK9MessageViewThemeSetting(Theme.DARK);
- Document document = Jsoup.parse(HtmlConverter.wrapMessageContent("Some text"));
- Elements styleElements = document.head().getElementsByTag("style");
+ String html = HtmlConverter.wrapMessageContent("Some text");
- assertEquals(2, styleElements.size());
+ assertHtmlContainsElement(html, "head > style", 2);
}
@Test
public void wrapMessageContent_putsMessageContentInBody() {
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);
}
}
