Merge pull request #5235 from plan3d/issue_1430
This commit is contained in:
commit
783182a718
4 changed files with 265 additions and 377 deletions
|
@ -284,9 +284,7 @@ public abstract class MessageBuilder {
|
|||
* original message.
|
||||
*/
|
||||
private TextBody buildText(boolean isDraft, SimpleMessageFormat simpleMessageFormat) {
|
||||
String messageText = text;
|
||||
|
||||
TextBodyBuilder textBodyBuilder = new TextBodyBuilder(messageText);
|
||||
TextBodyBuilder textBodyBuilder = new TextBodyBuilder(text);
|
||||
|
||||
/*
|
||||
* Find out if we need to include the original message as quoted text.
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
package com.fsck.k9.message;
|
||||
|
||||
|
||||
import android.text.TextUtils;
|
||||
import timber.log.Timber;
|
||||
|
||||
import com.fsck.k9.K9;
|
||||
|
@ -12,6 +11,9 @@ import com.fsck.k9.message.quote.InsertableHtmlContent;
|
|||
|
||||
|
||||
class TextBodyBuilder {
|
||||
public static final String HTML_AND_BODY_START = "<!DOCTYPE html><html><body>";
|
||||
public static final String HTML_AND_BODY_END = "</body></html>";
|
||||
|
||||
private boolean mIncludeQuotedText = true;
|
||||
private boolean mReplyAfterQuote = false;
|
||||
private boolean mSignatureBeforeQuotedText = false;
|
||||
|
@ -38,7 +40,7 @@ class TextBodyBuilder {
|
|||
int composedMessageLength;
|
||||
|
||||
// The offset of the user-supplied text/reply in the final text body
|
||||
int composedMessageOffset;
|
||||
int composedMessageOffset = 0;
|
||||
|
||||
// Get the user-supplied text
|
||||
String text = mMessageContent;
|
||||
|
@ -51,16 +53,17 @@ class TextBodyBuilder {
|
|||
Timber.d("insertable: %s", quotedHtmlContent.toDebugString());
|
||||
}
|
||||
|
||||
if (mAppendSignature) {
|
||||
// Append signature to the reply
|
||||
if (mReplyAfterQuote || mSignatureBeforeQuotedText) {
|
||||
text += getSignature();
|
||||
}
|
||||
}
|
||||
|
||||
// Convert the text to HTML
|
||||
text = textToHtmlFragment(text);
|
||||
|
||||
// Save length of the body. This is used when thawing drafts.
|
||||
composedMessageLength = text.length();
|
||||
|
||||
// Append signature to the reply
|
||||
if (mAppendSignature && (mReplyAfterQuote || mSignatureBeforeQuotedText)) {
|
||||
text += getSignatureHtml();
|
||||
}
|
||||
|
||||
/*
|
||||
* Set the insertion location based upon our reply after quote
|
||||
* setting. Additionally, add some extra separators between the
|
||||
|
@ -73,7 +76,9 @@ class TextBodyBuilder {
|
|||
quotedHtmlContent.setInsertionLocation(
|
||||
InsertableHtmlContent.InsertionLocation.AFTER_QUOTE);
|
||||
if (mInsertSeparator) {
|
||||
text = "<br clear=\"all\">" + text;
|
||||
final String separator = "<br clear=\"all\">";
|
||||
text = separator + text;
|
||||
composedMessageOffset = separator.length();
|
||||
}
|
||||
} else {
|
||||
quotedHtmlContent.setInsertionLocation(
|
||||
|
@ -83,33 +88,30 @@ class TextBodyBuilder {
|
|||
}
|
||||
}
|
||||
|
||||
if (mAppendSignature) {
|
||||
// Place signature immediately after the quoted text
|
||||
if (!(mReplyAfterQuote || mSignatureBeforeQuotedText)) {
|
||||
quotedHtmlContent.insertIntoQuotedFooter(getSignatureHtml());
|
||||
}
|
||||
// Place signature immediately after the quoted text
|
||||
if (mAppendSignature && !(mReplyAfterQuote || mSignatureBeforeQuotedText)) {
|
||||
quotedHtmlContent.insertIntoQuotedFooter(getSignatureHtml());
|
||||
}
|
||||
|
||||
quotedHtmlContent.setUserContent(text);
|
||||
|
||||
// Save length of the body and its offset. This is used when thawing drafts.
|
||||
composedMessageLength = text.length();
|
||||
composedMessageOffset = quotedHtmlContent.getInsertionPoint();
|
||||
// Save the body's offset. This is used when thawing drafts.
|
||||
composedMessageOffset += quotedHtmlContent.getInsertionPoint();
|
||||
text = quotedHtmlContent.toString();
|
||||
|
||||
} else {
|
||||
// There is no text to quote so simply append the signature if available
|
||||
if (mAppendSignature) {
|
||||
text += getSignature();
|
||||
}
|
||||
|
||||
// Convert the text to HTML
|
||||
text = textToHtmlFragment(text);
|
||||
|
||||
//TODO: Wrap this in proper HTML tags
|
||||
|
||||
composedMessageLength = text.length();
|
||||
composedMessageOffset = 0;
|
||||
composedMessageOffset = HTML_AND_BODY_START.length();
|
||||
|
||||
// There is no text to quote so simply append the signature if available
|
||||
if (mAppendSignature) {
|
||||
text += getSignatureHtml();
|
||||
}
|
||||
|
||||
text = HTML_AND_BODY_START + text + HTML_AND_BODY_END;
|
||||
}
|
||||
|
||||
TextBody body = new TextBody(text);
|
||||
|
@ -143,11 +145,9 @@ class TextBodyBuilder {
|
|||
if (mIncludeQuotedText) {
|
||||
String quotedText = getQuotedText();
|
||||
|
||||
if (mAppendSignature) {
|
||||
// Append signature to the text/reply
|
||||
if (mReplyAfterQuote || mSignatureBeforeQuotedText) {
|
||||
text += getSignature();
|
||||
}
|
||||
// Append signature to the text/reply
|
||||
if (mAppendSignature && (mReplyAfterQuote || mSignatureBeforeQuotedText)) {
|
||||
text += getSignature();
|
||||
}
|
||||
|
||||
if (mReplyAfterQuote) {
|
||||
|
@ -157,11 +157,9 @@ class TextBodyBuilder {
|
|||
text += "\r\n\r\n" + quotedText;
|
||||
}
|
||||
|
||||
if (mAppendSignature) {
|
||||
// Place signature immediately after the quoted text
|
||||
if (!(mReplyAfterQuote || mSignatureBeforeQuotedText)) {
|
||||
text += getSignature();
|
||||
}
|
||||
// Place signature immediately after the quoted text
|
||||
if (mAppendSignature && !(mReplyAfterQuote || mSignatureBeforeQuotedText)) {
|
||||
text += getSignature();
|
||||
}
|
||||
} else {
|
||||
// There is no text to quote so simply append the signature if available
|
||||
|
@ -180,7 +178,7 @@ class TextBodyBuilder {
|
|||
|
||||
private String getSignature() {
|
||||
String signature = "";
|
||||
if (!TextUtils.isEmpty(mSignature)) {
|
||||
if (!isEmpty(mSignature)) {
|
||||
signature = "\r\n" + mSignature;
|
||||
}
|
||||
|
||||
|
@ -189,15 +187,15 @@ class TextBodyBuilder {
|
|||
|
||||
private String getSignatureHtml() {
|
||||
String signature = "";
|
||||
if (!TextUtils.isEmpty(mSignature)) {
|
||||
signature = textToHtmlFragment("\r\n" + mSignature);
|
||||
if (!isEmpty(mSignature)) {
|
||||
signature = "<div style='white-space: pre-wrap'>" + textToHtmlFragment(mSignature) + "</div>";
|
||||
}
|
||||
return signature;
|
||||
}
|
||||
|
||||
private String getQuotedText() {
|
||||
String quotedText = "";
|
||||
if (!TextUtils.isEmpty(mQuotedText)) {
|
||||
if (!isEmpty(mQuotedText)) {
|
||||
quotedText = mQuotedText;
|
||||
}
|
||||
return quotedText;
|
||||
|
@ -245,4 +243,8 @@ class TextBodyBuilder {
|
|||
public void setAppendSignature(boolean appendSignature) {
|
||||
mAppendSignature = appendSignature;
|
||||
}
|
||||
|
||||
private static boolean isEmpty(String s) {
|
||||
return s == null || s.length() == 0;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,334 +0,0 @@
|
|||
package com.fsck.k9.message;
|
||||
|
||||
import com.fsck.k9.Account.QuoteStyle;
|
||||
import com.fsck.k9.mail.internet.TextBody;
|
||||
|
||||
import com.fsck.k9.message.quote.InsertableHtmlContent;
|
||||
import org.junit.Ignore;
|
||||
import org.junit.experimental.theories.DataPoints;
|
||||
import org.junit.experimental.theories.Theories;
|
||||
import org.junit.experimental.theories.Theory;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.instanceOf;
|
||||
import static org.hamcrest.CoreMatchers.is;
|
||||
import static org.junit.Assert.assertThat;
|
||||
|
||||
//TODO: Get rid of 'Theories' and write simple tests
|
||||
@Ignore
|
||||
@RunWith(Theories.class)
|
||||
public class TextBodyBuilderTest {
|
||||
|
||||
@DataPoints
|
||||
public static boolean[] BOOLEANS = { true, false };
|
||||
|
||||
@DataPoints
|
||||
public static QuoteStyle[] QUOTESTYLES = { QuoteStyle.PREFIX, QuoteStyle.HEADER };
|
||||
|
||||
@Theory
|
||||
public void testBuildTextPlain(boolean includeQuotedText,
|
||||
QuoteStyle quoteStyle,
|
||||
boolean isReplyAfterQuote,
|
||||
boolean isSignatureUse,
|
||||
boolean isSignatureBeforeQuotedText,
|
||||
boolean isDraft) {
|
||||
|
||||
String expectedText;
|
||||
int expectedMessageLength;
|
||||
int expectedMessagePosition;
|
||||
|
||||
// 1.quoted text
|
||||
// 2.message content
|
||||
// 3.signature
|
||||
if (quoteStyle == QuoteStyle.PREFIX && isReplyAfterQuote) {
|
||||
String expectedQuotedText = "";
|
||||
|
||||
if (isDraft || includeQuotedText) {
|
||||
expectedQuotedText = "quoted text" + "\r\n";
|
||||
}
|
||||
|
||||
expectedText = expectedQuotedText;
|
||||
|
||||
expectedText += "message content";
|
||||
|
||||
if (!isDraft && isSignatureUse) {
|
||||
expectedText += "\r\n" + "signature";
|
||||
}
|
||||
expectedMessageLength = "message content".length();
|
||||
expectedMessagePosition = expectedQuotedText.length();
|
||||
}
|
||||
// 1.message content
|
||||
// 2.signature
|
||||
// 3.quoted text
|
||||
else if (isSignatureBeforeQuotedText) {
|
||||
expectedText = "message content";
|
||||
|
||||
if (!isDraft && isSignatureUse) {
|
||||
expectedText += "\r\n" + "signature";
|
||||
}
|
||||
|
||||
if (isDraft || includeQuotedText) {
|
||||
expectedText += "\r\n\r\nquoted text";
|
||||
}
|
||||
|
||||
expectedMessageLength = "message content".length();
|
||||
expectedMessagePosition = 0;
|
||||
}
|
||||
// 1.message content
|
||||
// 2.quoted text
|
||||
// 3.signature
|
||||
else {
|
||||
expectedText = "message content";
|
||||
|
||||
if (isDraft || includeQuotedText) {
|
||||
expectedText += "\r\n\r\nquoted text";
|
||||
}
|
||||
|
||||
if (!isDraft && isSignatureUse) {
|
||||
expectedText += "\r\n" + "signature";
|
||||
}
|
||||
|
||||
expectedMessageLength = "message content".length();
|
||||
expectedMessagePosition = 0;
|
||||
}
|
||||
|
||||
String quotedText = "quoted text";
|
||||
String messageText = "message content";
|
||||
String signatureText = "signature";
|
||||
|
||||
TestingTextBodyBuilder textBodyBuilder = new TestingTextBodyBuilder(
|
||||
includeQuotedText,
|
||||
isDraft,
|
||||
quoteStyle,
|
||||
isReplyAfterQuote,
|
||||
isSignatureBeforeQuotedText,
|
||||
isSignatureUse,
|
||||
messageText,
|
||||
signatureText
|
||||
);
|
||||
textBodyBuilder.setQuotedText(quotedText);
|
||||
TextBody textBody = textBodyBuilder.buildTextPlain();
|
||||
|
||||
assertThat(textBody, instanceOf(TextBody.class));
|
||||
assertThat(textBody.getRawText(), is(expectedText));
|
||||
assertThat(textBody.getComposedMessageLength(), is(expectedMessageLength));
|
||||
assertThat(textBody.getComposedMessageOffset(), is(expectedMessagePosition));
|
||||
assertThat(textBody.getRawText().substring(expectedMessagePosition, expectedMessagePosition + expectedMessageLength),
|
||||
is("message content"));
|
||||
}
|
||||
|
||||
/**
|
||||
* generate expected HtmlContent debug string
|
||||
*
|
||||
* @param expectedText
|
||||
* @param quotedContent
|
||||
* @param footerInsertionPoint
|
||||
* @param isBefore
|
||||
* @param userContent
|
||||
* @param compiledResult
|
||||
* @return expected string
|
||||
*
|
||||
* @see InsertableHtmlContent#toDebugString()
|
||||
*/
|
||||
public String makeExpectedHtmlContent(String expectedText, String quotedContent,
|
||||
int footerInsertionPoint, boolean isBefore,
|
||||
String userContent, String compiledResult) {
|
||||
return "InsertableHtmlContent{"
|
||||
+ "headerInsertionPoint=0,"
|
||||
+ " footerInsertionPoint=" + footerInsertionPoint + ","
|
||||
+ " insertionLocation=" + (isBefore ? "BEFORE_QUOTE" : "AFTER_QUOTE") + ","
|
||||
+ " quotedContent=" + quotedContent + ","
|
||||
+ " userContent=" + userContent + ","
|
||||
+ " compiledResult=" + compiledResult
|
||||
+ "}";
|
||||
}
|
||||
|
||||
@Theory
|
||||
public void testBuildTextHtml(boolean includeQuotedText,
|
||||
QuoteStyle quoteStyle,
|
||||
boolean isReplyAfterQuote,
|
||||
boolean isSignatureUse,
|
||||
boolean isSignatureBeforeQuotedText,
|
||||
boolean isDraft) {
|
||||
String expectedText;
|
||||
int expectedMessageLength;
|
||||
int expectedMessagePosition = 0;
|
||||
String expectedHtmlContent;
|
||||
|
||||
String expectedPrefix = "";
|
||||
|
||||
if (includeQuotedText && quoteStyle == QuoteStyle.PREFIX && isReplyAfterQuote && !isDraft) {
|
||||
expectedPrefix = "<br clear=\"all\">";
|
||||
}
|
||||
String expectedPostfix = "";
|
||||
if (!isDraft && includeQuotedText) {
|
||||
expectedPostfix = "<br><br>";
|
||||
}
|
||||
|
||||
// 1.quoted text
|
||||
// 2.message content
|
||||
// 3.signature
|
||||
if (quoteStyle == QuoteStyle.PREFIX && isReplyAfterQuote) {
|
||||
expectedText = expectedPrefix
|
||||
+ "<html>message content";
|
||||
if (!isDraft && isSignatureUse) {
|
||||
expectedText += "\r\n" + "signature";
|
||||
}
|
||||
expectedText += "</html>";
|
||||
expectedMessageLength = expectedText.length();
|
||||
String quotedContent = "quoted text";
|
||||
|
||||
if (isDraft || includeQuotedText) {
|
||||
expectedHtmlContent = makeExpectedHtmlContent(expectedText, quotedContent,
|
||||
0,
|
||||
false,
|
||||
expectedText,
|
||||
expectedText + quotedContent);
|
||||
expectedText += quotedContent;
|
||||
} else {
|
||||
expectedHtmlContent = makeExpectedHtmlContent(expectedText, quotedContent,
|
||||
0,
|
||||
true,
|
||||
"",
|
||||
quotedContent);
|
||||
// expectedText += quotedContent;
|
||||
}
|
||||
}
|
||||
// 1.message content
|
||||
// 2.signature
|
||||
// 3.quoted text
|
||||
else if (isSignatureBeforeQuotedText) {
|
||||
expectedText = expectedPrefix
|
||||
+ "<html>message content";
|
||||
if (!isDraft && isSignatureUse) {
|
||||
expectedText += "\r\n" + "signature";
|
||||
}
|
||||
expectedText += "</html>";
|
||||
expectedText += expectedPostfix;
|
||||
|
||||
expectedMessageLength = expectedText.length();
|
||||
String quotedContent = "quoted text";
|
||||
|
||||
if (isDraft || includeQuotedText) {
|
||||
expectedHtmlContent = makeExpectedHtmlContent(expectedText, quotedContent,
|
||||
0,
|
||||
true,
|
||||
expectedText,
|
||||
expectedText + quotedContent);
|
||||
expectedText += quotedContent;
|
||||
} else {
|
||||
expectedHtmlContent = makeExpectedHtmlContent(expectedText, quotedContent,
|
||||
0,
|
||||
true,
|
||||
"",
|
||||
quotedContent);
|
||||
// expectedText += quotedContent;
|
||||
}
|
||||
}
|
||||
// 1.message content
|
||||
// 2.quoted text
|
||||
// 3.signature
|
||||
else {
|
||||
String expectedSignature = "";
|
||||
|
||||
expectedText = expectedPrefix
|
||||
+ "<html>message content";
|
||||
|
||||
if (!isDraft && isSignatureUse) {
|
||||
if (!includeQuotedText) {
|
||||
expectedText += "\r\n" + "signature";
|
||||
} else {
|
||||
expectedSignature = "<html>\r\nsignature</html>";
|
||||
}
|
||||
}
|
||||
expectedText += "</html>";
|
||||
expectedText += expectedPostfix;
|
||||
|
||||
expectedMessageLength = expectedText.length();
|
||||
String quotedContent = "quoted text";
|
||||
|
||||
if (isDraft || includeQuotedText) {
|
||||
expectedHtmlContent = makeExpectedHtmlContent(expectedText, expectedSignature + quotedContent,
|
||||
expectedSignature.length(),
|
||||
true,
|
||||
expectedText,
|
||||
expectedText + expectedSignature + quotedContent);
|
||||
expectedText += expectedSignature + quotedContent;
|
||||
} else {
|
||||
expectedHtmlContent = makeExpectedHtmlContent(expectedText, quotedContent,
|
||||
0,
|
||||
true,
|
||||
"",
|
||||
quotedContent);
|
||||
// expectedText += quotedContent;
|
||||
}
|
||||
}
|
||||
|
||||
InsertableHtmlContent insertableHtmlContent = new InsertableHtmlContent();
|
||||
|
||||
String quotedText = "quoted text";
|
||||
insertableHtmlContent.setQuotedContent(new StringBuilder(quotedText));
|
||||
String messageText = "message content";
|
||||
String signatureText = "signature";
|
||||
|
||||
TestingTextBodyBuilder textBodyBuilder = new TestingTextBodyBuilder(
|
||||
includeQuotedText,
|
||||
isDraft,
|
||||
quoteStyle,
|
||||
isReplyAfterQuote,
|
||||
isSignatureBeforeQuotedText,
|
||||
isSignatureUse,
|
||||
messageText,
|
||||
signatureText
|
||||
);
|
||||
textBodyBuilder.setQuotedTextHtml(insertableHtmlContent);
|
||||
TextBody textBody = textBodyBuilder.buildTextHtml();
|
||||
|
||||
assertThat(textBody, instanceOf(TextBody.class));
|
||||
assertThat(textBody.getRawText(), is(expectedText));
|
||||
assertThat(textBody.getComposedMessageLength(), is(expectedMessageLength));
|
||||
assertThat(textBody.getComposedMessageOffset(), is(expectedMessagePosition));
|
||||
assertThat(insertableHtmlContent.toDebugString(), is(expectedHtmlContent));
|
||||
}
|
||||
|
||||
|
||||
static class TestingTextBodyBuilder extends TextBodyBuilder {
|
||||
|
||||
public TestingTextBodyBuilder(boolean includeQuotedText,
|
||||
boolean isDraft,
|
||||
QuoteStyle quoteStyle,
|
||||
boolean replyAfterQuote,
|
||||
boolean signatureBeforeQuotedText,
|
||||
boolean useSignature,
|
||||
String messageText,
|
||||
String signatureText) {
|
||||
super(messageText);
|
||||
|
||||
includeQuotedText = (isDraft || includeQuotedText);
|
||||
if (includeQuotedText) {
|
||||
this.setIncludeQuotedText(true);
|
||||
this.setReplyAfterQuote(quoteStyle == QuoteStyle.PREFIX && replyAfterQuote);
|
||||
} else {
|
||||
this.setIncludeQuotedText(false);
|
||||
}
|
||||
|
||||
this.setInsertSeparator(!isDraft);
|
||||
|
||||
useSignature = (!isDraft && useSignature);
|
||||
if (useSignature) {
|
||||
this.setAppendSignature(true);
|
||||
this.setSignature(signatureText);
|
||||
this.setSignatureBeforeQuotedText(signatureBeforeQuotedText);
|
||||
} else {
|
||||
this.setAppendSignature(false);
|
||||
}
|
||||
}
|
||||
|
||||
// HtmlConverter depends on Android.
|
||||
// So we use dummy method for tests.
|
||||
@Override
|
||||
protected String textToHtmlFragment(String text) {
|
||||
return "<html>" + text + "</html>";
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,222 @@
|
|||
package com.fsck.k9.message
|
||||
|
||||
import com.fsck.k9.message.quote.InsertableHtmlContent
|
||||
import com.google.common.truth.Truth.assertThat
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.junit.runners.Parameterized
|
||||
|
||||
@RunWith(Parameterized::class)
|
||||
class TextBodyBuilderTest(val testData: TestData) {
|
||||
|
||||
companion object {
|
||||
|
||||
private const val MESSAGE_TEXT = "my message\r\nwith two lines"
|
||||
private const val MESSAGE_TEXT_HTML = "my message<br>with two lines"
|
||||
private const val QUOTED_TEXT = ">quoted text\r\n>-- \r\n>Other signature"
|
||||
private const val QUOTED_HTML_BODY = "<blockquote>quoted text</blockquote>"
|
||||
private const val QUOTED_HTML_TAGS_END = "</body>\n</html>"
|
||||
private const val QUOTED_HTML_TAGS_START = "<!DOCTYPE html><html><head></head><body>"
|
||||
private const val SIGNATURE_TEXT = "-- \r\n\r\nsignature\r\n indented second line"
|
||||
private const val SIGNATURE_TEXT_HTML = "<div style='white-space: pre-wrap'>" +
|
||||
"<div class='k9mail-signature'>-- <br><br>signature<br> indented second line</div></div>"
|
||||
|
||||
@JvmStatic
|
||||
@Parameterized.Parameters(name = "{index}: {0}")
|
||||
fun data(): Collection<TestData> {
|
||||
return listOf(
|
||||
TestData(
|
||||
appendSignature = false,
|
||||
includeQuotedText = false,
|
||||
insertSeparator = false,
|
||||
replyAfterQuote = false,
|
||||
signatureBeforeQuotedText = false,
|
||||
expectedPlainTextMessage = MESSAGE_TEXT,
|
||||
expectedHtmlTextMessage = TextBodyBuilder.HTML_AND_BODY_START + MESSAGE_TEXT_HTML +
|
||||
TextBodyBuilder.HTML_AND_BODY_END
|
||||
),
|
||||
TestData(
|
||||
appendSignature = true,
|
||||
includeQuotedText = false,
|
||||
insertSeparator = false,
|
||||
replyAfterQuote = false,
|
||||
signatureBeforeQuotedText = false,
|
||||
expectedPlainTextMessage = MESSAGE_TEXT + "\r\n" + SIGNATURE_TEXT,
|
||||
expectedHtmlTextMessage = TextBodyBuilder.HTML_AND_BODY_START + MESSAGE_TEXT_HTML +
|
||||
SIGNATURE_TEXT_HTML + TextBodyBuilder.HTML_AND_BODY_END
|
||||
),
|
||||
TestData(
|
||||
appendSignature = false,
|
||||
includeQuotedText = true,
|
||||
insertSeparator = false,
|
||||
replyAfterQuote = false,
|
||||
signatureBeforeQuotedText = false,
|
||||
expectedPlainTextMessage = MESSAGE_TEXT + "\r\n\r\n" + QUOTED_TEXT,
|
||||
expectedHtmlTextMessage = QUOTED_HTML_TAGS_START + MESSAGE_TEXT_HTML + QUOTED_HTML_BODY +
|
||||
QUOTED_HTML_TAGS_END
|
||||
),
|
||||
TestData(
|
||||
appendSignature = false,
|
||||
includeQuotedText = true,
|
||||
insertSeparator = true,
|
||||
replyAfterQuote = false,
|
||||
signatureBeforeQuotedText = false,
|
||||
expectedPlainTextMessage = MESSAGE_TEXT + "\r\n\r\n" + QUOTED_TEXT,
|
||||
expectedHtmlTextMessage = QUOTED_HTML_TAGS_START + MESSAGE_TEXT_HTML + "<br><br>" +
|
||||
QUOTED_HTML_BODY + QUOTED_HTML_TAGS_END
|
||||
),
|
||||
TestData(
|
||||
appendSignature = false,
|
||||
includeQuotedText = true,
|
||||
insertSeparator = false,
|
||||
replyAfterQuote = true,
|
||||
signatureBeforeQuotedText = false,
|
||||
expectedPlainTextMessage = QUOTED_TEXT + "\r\n" + MESSAGE_TEXT,
|
||||
expectedHtmlTextMessage = QUOTED_HTML_TAGS_START + QUOTED_HTML_BODY + MESSAGE_TEXT_HTML +
|
||||
QUOTED_HTML_TAGS_END
|
||||
),
|
||||
TestData(
|
||||
appendSignature = false,
|
||||
includeQuotedText = true,
|
||||
insertSeparator = true,
|
||||
replyAfterQuote = true,
|
||||
signatureBeforeQuotedText = false,
|
||||
expectedPlainTextMessage = QUOTED_TEXT + "\r\n" + MESSAGE_TEXT,
|
||||
expectedHtmlTextMessage = QUOTED_HTML_TAGS_START + QUOTED_HTML_BODY + "<br clear=\"all\">" +
|
||||
MESSAGE_TEXT_HTML + QUOTED_HTML_TAGS_END
|
||||
),
|
||||
TestData(
|
||||
appendSignature = true,
|
||||
includeQuotedText = true,
|
||||
insertSeparator = false,
|
||||
replyAfterQuote = false,
|
||||
signatureBeforeQuotedText = false,
|
||||
expectedPlainTextMessage = MESSAGE_TEXT + "\r\n\r\n" + QUOTED_TEXT + "\r\n" + SIGNATURE_TEXT,
|
||||
expectedHtmlTextMessage = QUOTED_HTML_TAGS_START + MESSAGE_TEXT_HTML + QUOTED_HTML_BODY +
|
||||
SIGNATURE_TEXT_HTML + QUOTED_HTML_TAGS_END
|
||||
),
|
||||
TestData(
|
||||
appendSignature = true,
|
||||
includeQuotedText = true,
|
||||
insertSeparator = true,
|
||||
replyAfterQuote = false,
|
||||
signatureBeforeQuotedText = false,
|
||||
expectedPlainTextMessage = MESSAGE_TEXT + "\r\n\r\n" + QUOTED_TEXT + "\r\n" + SIGNATURE_TEXT,
|
||||
expectedHtmlTextMessage = QUOTED_HTML_TAGS_START + MESSAGE_TEXT_HTML + "<br><br>" +
|
||||
QUOTED_HTML_BODY + SIGNATURE_TEXT_HTML + QUOTED_HTML_TAGS_END
|
||||
),
|
||||
TestData(
|
||||
appendSignature = true,
|
||||
includeQuotedText = true,
|
||||
insertSeparator = false,
|
||||
replyAfterQuote = true,
|
||||
signatureBeforeQuotedText = false,
|
||||
expectedPlainTextMessage = QUOTED_TEXT + "\r\n" + MESSAGE_TEXT + "\r\n" + SIGNATURE_TEXT,
|
||||
expectedHtmlTextMessage = QUOTED_HTML_TAGS_START + QUOTED_HTML_BODY + MESSAGE_TEXT_HTML +
|
||||
SIGNATURE_TEXT_HTML + QUOTED_HTML_TAGS_END
|
||||
),
|
||||
TestData(
|
||||
appendSignature = true,
|
||||
includeQuotedText = true,
|
||||
insertSeparator = true,
|
||||
replyAfterQuote = true,
|
||||
signatureBeforeQuotedText = false,
|
||||
expectedPlainTextMessage = QUOTED_TEXT + "\r\n" + MESSAGE_TEXT + "\r\n" + SIGNATURE_TEXT,
|
||||
expectedHtmlTextMessage = QUOTED_HTML_TAGS_START + QUOTED_HTML_BODY + "<br clear=\"all\">" +
|
||||
MESSAGE_TEXT_HTML + SIGNATURE_TEXT_HTML + QUOTED_HTML_TAGS_END
|
||||
),
|
||||
TestData(
|
||||
appendSignature = true,
|
||||
includeQuotedText = true,
|
||||
insertSeparator = false,
|
||||
replyAfterQuote = false,
|
||||
signatureBeforeQuotedText = true,
|
||||
expectedPlainTextMessage = MESSAGE_TEXT + "\r\n" + SIGNATURE_TEXT + "\r\n\r\n" + QUOTED_TEXT,
|
||||
expectedHtmlTextMessage = QUOTED_HTML_TAGS_START + MESSAGE_TEXT_HTML + SIGNATURE_TEXT_HTML +
|
||||
QUOTED_HTML_BODY + QUOTED_HTML_TAGS_END
|
||||
),
|
||||
TestData(
|
||||
appendSignature = true,
|
||||
includeQuotedText = true,
|
||||
insertSeparator = true,
|
||||
replyAfterQuote = false,
|
||||
signatureBeforeQuotedText = true,
|
||||
expectedPlainTextMessage = MESSAGE_TEXT + "\r\n" + SIGNATURE_TEXT + "\r\n\r\n" + QUOTED_TEXT,
|
||||
expectedHtmlTextMessage = QUOTED_HTML_TAGS_START + MESSAGE_TEXT_HTML + SIGNATURE_TEXT_HTML +
|
||||
"<br><br>" + QUOTED_HTML_BODY + QUOTED_HTML_TAGS_END
|
||||
)
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
private val toTest: TextBodyBuilder
|
||||
|
||||
init {
|
||||
toTest = TextBodyBuilder(MESSAGE_TEXT)
|
||||
toTest.setAppendSignature(testData.appendSignature)
|
||||
toTest.setIncludeQuotedText(testData.includeQuotedText)
|
||||
toTest.setInsertSeparator(testData.insertSeparator)
|
||||
toTest.setReplyAfterQuote(testData.replyAfterQuote)
|
||||
toTest.setSignatureBeforeQuotedText(testData.signatureBeforeQuotedText)
|
||||
toTest.setQuotedText(QUOTED_TEXT)
|
||||
val quotedHtmlContent = InsertableHtmlContent()
|
||||
quotedHtmlContent.setQuotedContent(
|
||||
StringBuilder(QUOTED_HTML_TAGS_START + QUOTED_HTML_BODY + QUOTED_HTML_TAGS_END)
|
||||
)
|
||||
quotedHtmlContent.setHeaderInsertionPoint(QUOTED_HTML_TAGS_START.length)
|
||||
quotedHtmlContent.footerInsertionPoint =
|
||||
QUOTED_HTML_TAGS_START.length + QUOTED_HTML_BODY.length
|
||||
toTest.setQuotedTextHtml(quotedHtmlContent)
|
||||
toTest.setSignature(SIGNATURE_TEXT)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun plainTextBody_expectCorrectRawText() {
|
||||
val textBody = toTest.buildTextPlain()
|
||||
|
||||
assertThat(textBody.rawText).isEqualTo(testData.expectedPlainTextMessage)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun plainTextBodySubstring_expectMessage() {
|
||||
val textBody = toTest.buildTextPlain()
|
||||
|
||||
val startIndex = textBody.composedMessageOffset!!
|
||||
val endIndex = startIndex + textBody.composedMessageLength!!
|
||||
assertThat(textBody.rawText.substring(startIndex, endIndex)).isEqualTo(MESSAGE_TEXT)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun htmlTextBody_expectCorrectRawText() {
|
||||
val textBody = toTest.buildTextHtml()
|
||||
|
||||
assertThat(textBody.rawText).isEqualTo(testData.expectedHtmlTextMessage)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun htmlTextBodySubstring_expectMessage() {
|
||||
val textBody = toTest.buildTextHtml()
|
||||
|
||||
val startIndex = textBody.composedMessageOffset!!
|
||||
val endIndex = startIndex + textBody.composedMessageLength!!
|
||||
assertThat(textBody.rawText.substring(startIndex, endIndex)).isEqualTo(MESSAGE_TEXT_HTML)
|
||||
}
|
||||
|
||||
class TestData(
|
||||
val appendSignature: Boolean,
|
||||
val includeQuotedText: Boolean,
|
||||
val insertSeparator: Boolean,
|
||||
val replyAfterQuote: Boolean,
|
||||
val signatureBeforeQuotedText: Boolean,
|
||||
val expectedPlainTextMessage: String,
|
||||
val expectedHtmlTextMessage: String
|
||||
) {
|
||||
override fun toString(): String {
|
||||
return "appendSignature=$appendSignature," +
|
||||
"includeQuotedText=$includeQuotedText," +
|
||||
"insertSeparator=$insertSeparator," +
|
||||
"replyAfterQuote=$replyAfterQuote," +
|
||||
"signatureBeforeQuotedText=$signatureBeforeQuotedText"
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in a new issue