2012-04-17 13:22:43 -04:00
|
|
|
package com.fsck.k9.helper;
|
|
|
|
|
2012-04-25 10:11:15 -04:00
|
|
|
import junit.framework.TestCase;
|
2012-04-17 13:22:43 -04:00
|
|
|
|
|
|
|
import java.io.BufferedWriter;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileWriter;
|
|
|
|
|
2012-04-25 10:11:15 -04:00
|
|
|
public class HtmlConverterTest extends TestCase {
|
2012-04-17 13:22:43 -04:00
|
|
|
// Useful if you want to write stuff to a file for debugging in a browser.
|
2012-04-23 18:24:02 -04:00
|
|
|
private static final boolean WRITE_TO_FILE = Boolean.parseBoolean(System.getProperty("k9.htmlConverterTest.writeToFile", "false"));
|
2012-04-17 13:22:43 -04:00
|
|
|
private static final String OUTPUT_FILE = "C:/temp/parse.html";
|
|
|
|
|
|
|
|
public void testTextQuoteToHtmlBlockquote() {
|
|
|
|
String message = "Panama!\n" +
|
|
|
|
"\n" +
|
|
|
|
"Bob Barker <bob@aol.com> wrote:\n" +
|
|
|
|
"> a canal\n" +
|
|
|
|
">\n" +
|
|
|
|
"> Dorothy Jo Gideon <dorothy@aol.com> espoused:\n" +
|
|
|
|
"> >A man, a plan...\n" +
|
|
|
|
"> Too easy!\n" +
|
|
|
|
"\n" +
|
|
|
|
"Nice job :)\n" +
|
|
|
|
">> Guess!";
|
2013-03-01 12:30:25 -05:00
|
|
|
String result = HtmlConverter.textToHtml(message);
|
2012-04-17 13:22:43 -04:00
|
|
|
writeToFile(result);
|
2013-05-09 09:10:37 -04:00
|
|
|
assertEquals("<pre class=\"k9mail\">"
|
|
|
|
+ "Panama!<br />"
|
|
|
|
+ "<br />"
|
|
|
|
+ "Bob Barker <bob@aol.com> wrote:<br />"
|
|
|
|
+ "<blockquote class=\"gmail_quote\" style=\"margin: 0pt 0pt 1ex 0.8ex; border-left: 1px solid #729fcf; padding-left: 1ex;\">"
|
2013-05-09 09:11:05 -04:00
|
|
|
+ " a canal<br />"
|
2013-05-09 09:10:37 -04:00
|
|
|
+ "<br />"
|
2013-05-09 09:11:05 -04:00
|
|
|
+ " Dorothy Jo Gideon <dorothy@aol.com> espoused:<br />"
|
2013-05-09 09:10:37 -04:00
|
|
|
+ "<blockquote class=\"gmail_quote\" style=\"margin: 0pt 0pt 1ex 0.8ex; border-left: 1px solid #ad7fa8; padding-left: 1ex;\">"
|
|
|
|
+ "A man, a plan...<br />"
|
|
|
|
+ "</blockquote>"
|
2013-05-10 19:44:17 -04:00
|
|
|
+ " Too easy!<br />"
|
|
|
|
+ "</blockquote>"
|
2013-05-09 09:10:37 -04:00
|
|
|
+ "<br />"
|
|
|
|
+ "Nice job :)<br />"
|
|
|
|
+ "<blockquote class=\"gmail_quote\" style=\"margin: 0pt 0pt 1ex 0.8ex; border-left: 1px solid #729fcf; padding-left: 1ex;\">"
|
|
|
|
+ "<blockquote class=\"gmail_quote\" style=\"margin: 0pt 0pt 1ex 0.8ex; border-left: 1px solid #ad7fa8; padding-left: 1ex;\">"
|
2013-05-09 09:11:05 -04:00
|
|
|
+ " Guess!"
|
2013-05-09 09:10:37 -04:00
|
|
|
+ "</blockquote>"
|
|
|
|
+ "</blockquote>"
|
|
|
|
+ "</pre>", result);
|
2012-04-17 13:22:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public void testTextQuoteToHtmlBlockquoteIndented() {
|
|
|
|
String message = "*facepalm*\n" +
|
|
|
|
"\n" +
|
|
|
|
"Bob Barker <bob@aol.com> wrote:\n" +
|
|
|
|
"> A wise man once said...\n" +
|
|
|
|
">\n" +
|
|
|
|
"> LOL F1RST!!!!!\n" +
|
|
|
|
">\n" +
|
|
|
|
"> :)";
|
2013-03-01 12:30:25 -05:00
|
|
|
String result = HtmlConverter.textToHtml(message);
|
2012-04-17 13:22:43 -04:00
|
|
|
writeToFile(result);
|
2013-05-09 09:10:37 -04:00
|
|
|
assertEquals("<pre class=\"k9mail\">"
|
|
|
|
+ "*facepalm*<br />"
|
|
|
|
+ "<br />"
|
|
|
|
+ "Bob Barker <bob@aol.com> wrote:<br />"
|
|
|
|
+ "<blockquote class=\"gmail_quote\" style=\"margin: 0pt 0pt 1ex 0.8ex; border-left: 1px solid #729fcf; padding-left: 1ex;\">"
|
2013-05-09 09:11:05 -04:00
|
|
|
+ " A wise man once said...<br />"
|
2013-05-09 09:10:37 -04:00
|
|
|
+ "<br />"
|
2013-05-09 09:11:05 -04:00
|
|
|
+ " LOL F1RST!!!!!<br />"
|
2013-05-09 09:10:37 -04:00
|
|
|
+ "<br />"
|
2013-05-09 09:11:05 -04:00
|
|
|
+ " :)"
|
2013-05-09 09:10:37 -04:00
|
|
|
+ "</blockquote></pre>", result);
|
|
|
|
|
2012-04-23 18:10:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
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\n" +
|
|
|
|
"> one\n" +
|
|
|
|
">> two\n" +
|
|
|
|
">>> three\n" +
|
|
|
|
">>>> four\n" +
|
|
|
|
">>>>> five\n" +
|
|
|
|
">>>>>> six";
|
2013-03-01 12:30:25 -05:00
|
|
|
String result = HtmlConverter.textToHtml(message);
|
2012-04-23 18:10:34 -04:00
|
|
|
writeToFile(result);
|
2013-05-09 09:10:37 -04:00
|
|
|
assertEquals("<pre class=\"k9mail\">"
|
|
|
|
+ "zero<br />"
|
|
|
|
+ "<blockquote class=\"gmail_quote\" style=\"margin: 0pt 0pt 1ex 0.8ex; border-left: 1px solid #729fcf; padding-left: 1ex;\">"
|
2013-05-09 09:11:05 -04:00
|
|
|
+ " one<br />"
|
2013-05-09 09:10:37 -04:00
|
|
|
+ "<blockquote class=\"gmail_quote\" style=\"margin: 0pt 0pt 1ex 0.8ex; border-left: 1px solid #ad7fa8; padding-left: 1ex;\">"
|
2013-05-09 09:11:05 -04:00
|
|
|
+ " two<br />"
|
2013-05-09 09:10:37 -04:00
|
|
|
+ "<blockquote class=\"gmail_quote\" style=\"margin: 0pt 0pt 1ex 0.8ex; border-left: 1px solid #8ae234; padding-left: 1ex;\">"
|
2013-05-09 09:11:05 -04:00
|
|
|
+ " three<br />"
|
2013-05-09 09:10:37 -04:00
|
|
|
+ "<blockquote class=\"gmail_quote\" style=\"margin: 0pt 0pt 1ex 0.8ex; border-left: 1px solid #fcaf3e; padding-left: 1ex;\">"
|
2013-05-09 09:11:05 -04:00
|
|
|
+ " four<br />"
|
2013-05-09 09:10:37 -04:00
|
|
|
+ "<blockquote class=\"gmail_quote\" style=\"margin: 0pt 0pt 1ex 0.8ex; border-left: 1px solid #e9b96e; padding-left: 1ex;\">"
|
2013-05-09 09:11:05 -04:00
|
|
|
+ " five<br />"
|
2013-05-09 09:10:37 -04:00
|
|
|
+ "<blockquote class=\"gmail_quote\" style=\"margin: 0pt 0pt 1ex 0.8ex; border-left: 1px solid #ccc; padding-left: 1ex;\">"
|
2013-05-09 09:11:05 -04:00
|
|
|
+ " six"
|
2013-05-09 09:10:37 -04:00
|
|
|
+ "</blockquote>"
|
|
|
|
+ "</blockquote>"
|
|
|
|
+ "</blockquote>"
|
|
|
|
+ "</blockquote>"
|
|
|
|
+ "</blockquote>"
|
|
|
|
+ "</blockquote>"
|
|
|
|
+ "</pre>", result);
|
2012-04-17 13:22:43 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
private void writeToFile(final String content) {
|
2012-04-25 12:46:05 -04:00
|
|
|
if (!WRITE_TO_FILE) {
|
2012-04-17 13:22:43 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
System.err.println(content);
|
|
|
|
|
|
|
|
File f = new File(OUTPUT_FILE);
|
|
|
|
f.delete();
|
|
|
|
|
|
|
|
FileWriter fstream = new FileWriter(OUTPUT_FILE);
|
|
|
|
BufferedWriter out = new BufferedWriter(fstream);
|
|
|
|
out.write(content);
|
|
|
|
out.close();
|
|
|
|
} catch (Exception e) {
|
|
|
|
e.printStackTrace();
|
|
|
|
}
|
|
|
|
}
|
2013-05-09 09:11:05 -04:00
|
|
|
|
|
|
|
public void testPreserveSpacesAtFirst() {
|
|
|
|
String message = "foo\n"
|
|
|
|
+ " bar\n"
|
2013-05-10 19:44:17 -04:00
|
|
|
+ " baz\n";
|
2013-05-09 09:11:05 -04:00
|
|
|
String result = HtmlConverter.textToHtml(message);
|
|
|
|
writeToFile(result);
|
|
|
|
assertEquals("<pre class=\"k9mail\">"
|
|
|
|
+ "foo<br />"
|
|
|
|
+ " bar<br />"
|
|
|
|
+ " baz<br />"
|
2013-05-10 19:44:17 -04:00
|
|
|
+ "</pre>", result);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void testPreserveSpacesAtFirstForSpecialCharacters() {
|
|
|
|
String message =
|
|
|
|
" \n"
|
|
|
|
+ " &\n"
|
|
|
|
+ " \r\n"
|
|
|
|
+ " <\n"
|
|
|
|
+ " > \n";
|
|
|
|
String result = HtmlConverter.textToHtml(message);
|
|
|
|
writeToFile(result);
|
|
|
|
assertEquals("<pre class=\"k9mail\">"
|
2013-05-09 10:07:58 -04:00
|
|
|
+ " <br />"
|
|
|
|
+ " &<br />"
|
|
|
|
+ " <br />"
|
|
|
|
+ " <<br />"
|
2013-05-10 19:44:17 -04:00
|
|
|
+ "<blockquote class=\"gmail_quote\" style=\"margin: 0pt 0pt 1ex 0.8ex; border-left: 1px solid #729fcf; padding-left: 1ex;\">"
|
|
|
|
+ " <br />"
|
|
|
|
+ "</blockquote>"
|
2013-05-09 09:11:05 -04:00
|
|
|
+ "</pre>", result);
|
|
|
|
}
|
2012-04-17 13:22:43 -04:00
|
|
|
}
|