Added test for trailing newline stripping. Renamed method for clarity. Doccomments.

Changed the helpful comments to a Javadoc comment, as tooling such as editors
will be more likely to make use of it like that.

Renamed to emphasise that only trailing new lines are stripped.

Added a basic test for this function to ensure it only strips trailing,
and also that it does actually strip trailing slashes.
This commit is contained in:
Peter Serwylo 2016-12-01 09:03:12 +11:00
parent ab917b6272
commit ab72797a41
2 changed files with 20 additions and 8 deletions

View File

@ -221,7 +221,7 @@ public class AppDetailsRecyclerViewAdapter
vh.summaryView.setText(mApp.summary);
final Spanned desc = Html.fromHtml(mApp.description, null, new Utils.HtmlTagHandler());
vh.descriptionView.setMovementMethod(LinkMovementMethod.getInstance());
vh.descriptionView.setText(trimNewlines(desc));
vh.descriptionView.setText(trimTrailingNewlines(desc));
if (vh.descriptionView.getText() instanceof Spannable) {
Spannable spannable = (Spannable) vh.descriptionView.getText();
URLSpan[] spans = spannable.getSpans(0, spannable.length(), URLSpan.class);
@ -776,13 +776,15 @@ public class AppDetailsRecyclerViewAdapter
return (intent.resolveActivity(mContext.getPackageManager()) != null);
}
// The HTML formatter adds "\n\n" at the end of every paragraph. This
// is desired between paragraphs, but not at the end of the whole
// string as it adds unwanted spacing at the end of the TextView.
// Remove all trailing newlines.
// Use this function instead of a trim() as that would require
// converting to String and thus losing formatting (e.g. bold).
private static CharSequence trimNewlines(CharSequence s) {
/**
* The HTML formatter adds "\n\n" at the end of every paragraph. This
* is desired between paragraphs, but not at the end of the whole
* string as it adds unwanted spacing at the end of the TextView.
* Remove all trailing newlines.
* Use this function instead of a trim() as that would require
* converting to String and thus losing formatting (e.g. bold).
*/
public static CharSequence trimTrailingNewlines(CharSequence s) {
if (TextUtils.isEmpty(s)) {
return s;
}

View File

@ -3,6 +3,7 @@ package org.fdroid.fdroid;
import android.content.Context;
import org.fdroid.fdroid.views.AppDetailsRecyclerViewAdapter;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricGradleTestRunner;
@ -48,6 +49,15 @@ public class UtilsTest {
String fingerprintLongByOneFingerprint = "59050C8155DCA377F23D5A15B77D37134000CDBD8B42FBFBE0E3F38096E68CECE";
String fingerprintLongByOnePubkey = "308203c5308202ada00302010202047b7cf549300d06092a864886f70d01010b0500308192310b30090603550406130255533111300f060355040813084e657720596f726b3111300f060355040713084e657720596f726b311d301b060355040a131454686520477561726469616e2050726f6a656374311f301d060355040b1316477561726469616e20462d44726f6964204275696c64311d301b06035504031314677561726469616e70726f6a6563742e696e666f301e170d3132313032393130323530305a170d3430303331363130323530305a308192310b30090603550406130255533111300f060355040813084e657720596f726b3111300f060355040713084e657720596f726b311d301b060355040a131454686520477561726469616e2050726f6a656374311f301d060355040b1316477561726469616e20462d44726f6964204275696c64311d301b06035504031314677561726469616e70726f6a6563742e696e666f30820122300d06092a864886f70d01010105000382010f003082010a0282010100b7f1f635fa3fce1a8042aaa960c2dc557e4ad2c082e5787488cba587fd26207cf59507919fc4dcebda5c8c0959d14146d0445593aa6c29dc639570b71712451fd5c231b0c9f5f0bec380503a1c2a3bc00048bc5db682915afa54d1ecf67b45e1e05c0934b3037a33d3a565899131f27a72c03a5de93df17a2376cc3107f03ee9d124c474dfab30d4053e8f39f292e2dcb6cc131bce12a0c5fc307985195d256bf1d7a2703d67c14bf18ed6b772bb847370b20335810e337c064fef7e2795a524c664a853cd46accb8494f865164dabfb698fa8318236432758bc40d52db00d5ce07fe2210dc06cd95298b4f09e6c9b7b7af61c1d62ea43ea36a2331e7b2d4e250203010001a321301f301d0603551d0e0416041404d763e981cf3a295b94a790d8536a783097232b300d06092a864886f70d01010b05000382010100654e6484ff032c54fed1d96d3c8e731302be9dbd7bb4fe635f2dac05b69f3ecbb5acb7c9fe405e2a066567a8f5c2beb8b199b5a4d5bb1b435cf02df026d4fb4edd9d8849078f085b00950083052d57467d65c6eebd98f037cff9b148d621cf8819c4f7dc1459bf8fc5c7d76f901495a7caf35d1e5c106e1d50610c4920c3c1b50adcfbd4ad83ce7353cdea7d856bba0419c224f89a2f3ebc203d20eb6247711ad2b55fd4737936dc42ced7a047cbbd24012079204a2883b6d55d5d5b66d9fd82fb51fca9a5db5fad9af8564cb380ff30ae8263dbbf01b46e01313f53279673daa3f893380285646b244359203e7eecde94ae141b7dfa8e6499bb8e7e0b25ab85";
@Test
public void trailingNewLines() {
CharSequence threeParagraphs = AppDetailsRecyclerViewAdapter.trimTrailingNewlines("Paragraph One\n\nParagraph Two\n\nParagraph Three\n\n");
assertEquals("Paragraph One\n\nParagraph Two\n\nParagraph Three", threeParagraphs);
CharSequence leadingAndExtraTrailing = AppDetailsRecyclerViewAdapter.trimTrailingNewlines("\n\n\nA\n\n\n");
assertEquals("\n\n\nA", leadingAndExtraTrailing);
}
@Test
public void commaSeparatedStrings() {
assertNull(Utils.parseCommaSeparatedString(null));