package com.fsck.k9.helper; import java.io.BufferedWriter; import java.io.File; import java.io.FileWriter; import java.io.IOException; import org.apache.commons.io.IOUtils; import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.RobolectricTestRunner; import org.robolectric.annotation.Config; import static junit.framework.Assert.assertEquals; @RunWith(RobolectricTestRunner.class) @Config(manifest = Config.NONE) public class HtmlConverterTest { // Useful if you want to write stuff to a file for debugging in a browser. private static final boolean WRITE_TO_FILE = Boolean.parseBoolean(System.getProperty("k9.htmlConverterTest.writeToFile", "false")); private static final String OUTPUT_FILE = "C:/temp/parse.html"; @Test public void testTextQuoteToHtmlBlockquote() { String message = "Panama!\r\n" + "\r\n" + "Bob Barker wrote:\r\n" + "> a canal\r\n" + ">\r\n" + "> Dorothy Jo Gideon espoused:\r\n" + "> >A man, a plan...\r\n" + "> Too easy!\r\n" + "\r\n" + "Nice job :)\r\n" + ">> Guess!"; String result = HtmlConverter.textToHtml(message); writeToFile(result); assertEquals("
"
                + "Panama!
" + "
" + "Bob Barker <bob@aol.com> wrote:
" + "
" + " a canal
" + "
" + " Dorothy Jo Gideon <dorothy@aol.com> espoused:
" + "
" + "A man, a plan...
" + "
" + " Too easy!
" + "
" + "
" + "Nice job :)
" + "
" + "
" + " Guess!" + "
" + "
" + "
", result); } @Test public void testTextQuoteToHtmlBlockquoteIndented() { String message = "*facepalm*\r\n" + "\r\n" + "Bob Barker wrote:\r\n" + "> A wise man once said...\r\n" + ">\r\n" + "> LOL F1RST!!!!!\r\n" + ">\r\n" + "> :)"; String result = HtmlConverter.textToHtml(message); writeToFile(result); assertEquals("
"
                + "*facepalm*
" + "
" + "Bob Barker <bob@aol.com> wrote:
" + "
" + " A wise man once said...
" + "
" + " LOL F1RST!!!!!
" + "
" + " :)" + "
", result); } @Test public void testQuoteDepthColor() { assertEquals(HtmlConverter.getQuoteColor(1), HtmlConverter.QUOTE_COLOR_LEVEL_1); assertEquals(HtmlConverter.getQuoteColor(2), HtmlConverter.QUOTE_COLOR_LEVEL_2); assertEquals(HtmlConverter.getQuoteColor(3), HtmlConverter.QUOTE_COLOR_LEVEL_3); assertEquals(HtmlConverter.getQuoteColor(4), HtmlConverter.QUOTE_COLOR_LEVEL_4); assertEquals(HtmlConverter.getQuoteColor(5), HtmlConverter.QUOTE_COLOR_LEVEL_5); assertEquals(HtmlConverter.getQuoteColor(-1), HtmlConverter.QUOTE_COLOR_DEFAULT); assertEquals(HtmlConverter.getQuoteColor(0), HtmlConverter.QUOTE_COLOR_DEFAULT); assertEquals(HtmlConverter.getQuoteColor(6), HtmlConverter.QUOTE_COLOR_DEFAULT); String message = "zero\r\n" + "> one\r\n" + ">> two\r\n" + ">>> three\r\n" + ">>>> four\r\n" + ">>>>> five\r\n" + ">>>>>> six"; String result = HtmlConverter.textToHtml(message); writeToFile(result); assertEquals("
"
                + "zero
" + "
" + " one
" + "
" + " two
" + "
" + " three
" + "
" + " four
" + "
" + " five
" + "
" + " six" + "
" + "
" + "
" + "
" + "
" + "
" + "
", result); } private void writeToFile(final String content) { if (!WRITE_TO_FILE) { return; } FileWriter fstream = null; try { System.err.println(content); File f = new File(OUTPUT_FILE); f.delete(); fstream = new FileWriter(OUTPUT_FILE); BufferedWriter out = new BufferedWriter(fstream); out.write(content); out.close(); } catch (IOException e) { throw new RuntimeException(e); } finally { IOUtils.closeQuietly(fstream); } } @Test public void testPreserveSpacesAtFirst() { String message = "foo\r\n" + " bar\r\n" + " baz\r\n"; String result = HtmlConverter.textToHtml(message); writeToFile(result); assertEquals("
"
                + "foo
" + " bar
" + " baz
" + "
", result); } @Test public void testPreserveSpacesAtFirstForSpecialCharacters() { String message = " \r\n" + " &\r\n" + " \n" + " <\r\n" + " > \r\n"; String result = HtmlConverter.textToHtml(message); writeToFile(result); assertEquals("
"
                + " 
" + " &
" + "
" + " <
" + "
" + "
" + "
" + "
", result); } @Test public void testLinkifyBitcoinAndHttpUri() { String text = "bitcoin:19W6QZkx8SYPG7BBCS7odmWGRxqRph5jFU http://example.com/"; StringBuffer outputBuffer = new StringBuffer(); HtmlConverter.linkifyText(text, outputBuffer); assertEquals("" + "bitcoin:19W6QZkx8SYPG7BBCS7odmWGRxqRph5jFU" + " " + "" + "http://example.com/" + "", outputBuffer.toString()); } }