diff options
-rw-r--r-- | app/src/androidTest/java/it/niedermann/nextcloud/deck/util/SpannableUtilTest.java | 52 | ||||
-rw-r--r-- | app/src/main/java/it/niedermann/nextcloud/deck/util/SpannableUtil.java | 12 |
2 files changed, 58 insertions, 6 deletions
diff --git a/app/src/androidTest/java/it/niedermann/nextcloud/deck/util/SpannableUtilTest.java b/app/src/androidTest/java/it/niedermann/nextcloud/deck/util/SpannableUtilTest.java new file mode 100644 index 000000000..1797bc405 --- /dev/null +++ b/app/src/androidTest/java/it/niedermann/nextcloud/deck/util/SpannableUtilTest.java @@ -0,0 +1,52 @@ +package it.niedermann.nextcloud.deck.util; + +import android.content.Context; +import android.graphics.Typeface; +import android.text.SpannableString; +import android.text.style.ForegroundColorSpan; +import android.text.style.StyleSpan; +import android.text.style.URLSpan; + +import androidx.test.ext.junit.runners.AndroidJUnit4; +import androidx.test.platform.app.InstrumentationRegistry; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import static org.junit.Assert.assertEquals; + +@RunWith(AndroidJUnit4.class) +public class SpannableUtilTest { + + @Test + public void testStrong() { + final SpannableString spannableString = SpannableUtil.strong("test"); + assertEquals(1, spannableString.getSpans(0, spannableString.length(), Object.class).length); + assertEquals(1, spannableString.getSpans(0, spannableString.length(), StyleSpan.class).length); + assertEquals(4, spannableString.length()); + assertEquals(Typeface.BOLD, spannableString.getSpans(0, spannableString.length(), StyleSpan.class)[0].getStyle()); + } + + @Test + public void testDisabled() { + final Context appContext = InstrumentationRegistry.getInstrumentation().getTargetContext(); + + final SpannableString spannableString = SpannableUtil.disabled("test", appContext); + assertEquals(2, spannableString.getSpans(0, spannableString.length(), Object.class).length); + assertEquals(1, spannableString.getSpans(0, spannableString.length(), ForegroundColorSpan.class).length); + assertEquals(1, spannableString.getSpans(0, spannableString.length(), StyleSpan.class).length); + assertEquals(4, spannableString.length()); + assertEquals(Typeface.ITALIC, spannableString.getSpans(0, spannableString.length(), StyleSpan.class)[0].getStyle()); + } + + + @Test + public void testUrl() { + final SpannableString spannableString = SpannableUtil.url("test", "https://example.com"); + assertEquals(1, spannableString.getSpans(0, spannableString.length(), Object.class).length); + assertEquals(1, spannableString.getSpans(0, spannableString.length(), URLSpan.class).length); + assertEquals(4, spannableString.length()); + assertEquals("https://example.com", spannableString.getSpans(0, spannableString.length(), URLSpan.class)[0].getURL()); + } + +} diff --git a/app/src/main/java/it/niedermann/nextcloud/deck/util/SpannableUtil.java b/app/src/main/java/it/niedermann/nextcloud/deck/util/SpannableUtil.java index 54f3c8adc..c2ba6f46a 100644 --- a/app/src/main/java/it/niedermann/nextcloud/deck/util/SpannableUtil.java +++ b/app/src/main/java/it/niedermann/nextcloud/deck/util/SpannableUtil.java @@ -21,28 +21,28 @@ import it.niedermann.nextcloud.deck.R; public class SpannableUtil { public static SpannableString strong(@NonNull CharSequence text) { - SpannableString span = new SpannableString(text); + final SpannableString span = new SpannableString(text); span.setSpan(new StyleSpan(Typeface.BOLD), 0, span.length(), 0); return span; } public static SpannableString disabled(@NonNull CharSequence text, @NonNull Context context) { - SpannableString span = new SpannableString(text); + final SpannableString span = new SpannableString(text); span.setSpan(new StyleSpan(Typeface.ITALIC), 0, span.length(), 0); span.setSpan(new ForegroundColorSpan(ContextCompat.getColor(context, R.color.fg_secondary)), 0, span.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); return span; } public static SpannableString url(@NonNull CharSequence text, @NonNull String target) { - SpannableString span = new SpannableString(text); + final SpannableString span = new SpannableString(text); span.setSpan(new URLSpan(target), 0, span.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); return span; } public static void setTextWithURL(@NonNull TextView textView, @NonNull Resources resources, @StringRes int containerTextId, @StringRes int linkLabelId, @StringRes int urlId) { - String linkLabel = resources.getString(linkLabelId); - String finalText = resources.getString(containerTextId, linkLabel); - SpannableStringBuilder finalTextBuilder = new SpannableStringBuilder(finalText); + final String linkLabel = resources.getString(linkLabelId); + final String finalText = resources.getString(containerTextId, linkLabel); + final SpannableStringBuilder finalTextBuilder = new SpannableStringBuilder(finalText); finalTextBuilder.setSpan(new URLSpan(resources.getString(urlId)), finalText.indexOf(linkLabel), finalText.indexOf(linkLabel) + linkLabel.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE); textView.setText(finalTextBuilder); textView.setMovementMethod(new LinkMovementMethod()); |