I'm trying to put a Spanned package, but when I add bold spaces, the test I wrote no longer passes.
Here is the test:
@RunWith(Parameterized.class) public class DNTest { private String testData; private Spanned spanned; private Intent intent; private Class<NI> itemClass; @Before public void initialize() throws IOException { testData = Tests.readTestData(getInstrumentation(), "file.json); intent = new Intent("ACTION"); spanned = getDisplayableString("thing", "Wojtek Wntrzak"); assertThat(testData).isNotNull().isNotEmpty(); } @Parameterized.Parameters public static Collection<Object[]> testNI() { return Arrays.asList(new Object[][] { { LANI.class }, { FANI.class }, { ACNI.class }, { CRNI.class }, }); } public DNTest(Class<NI> nItemClass){ itemClass = nItemClass; } @Test public void testParcelable() { NI notification = mGson.fromJson(testData, itemClass); DN dN = new DN(spanned, intent, notification); Parcel parcel = Parcel.obtain(); parcel.writeString(dN.getClass().getName()); dN.writeToParcel(parcel, 0); parcel.setDataPosition(0); final DN parcelled = parcel.readParcelable(dN.getClass().getClassLoader()); assertThat(parcelled).isNotNull(); assertEquals(parcelled.getSpanned(), spanned); assertEquals(new Intent.FilterComparison(parcelled.getIntent()), new Intent.FilterComparison(intent)); assertEquals(parcelled.getItem(), notification); assertThat(dN).isNotNull(); assertParcelableCorrect(dN); } private Spanned getDisplayableString(String name, String actor) { final String string; string = actor + " blablabla " + name; SpannableString spannable = new SpannableString(string); Notifications.boldify(string, spannable, actor); Notifications.boldify(string, spannable, name); Spanned spanned = spannable; return spanned; } }
It passes when I comment on 2 lines using the boldify function.
Here is the boldify function:
public static void boldify(String source, SpannableString spannable, String text) { int start = source.indexOf(text); if (start == -1) { return; } spannable.setSpan(new StyleSpan(Typeface.BOLD), start, start + text.length(), Spanned.SPAN_INCLUSIVE_INCLUSIVE); }
And I tried 3 ways to send Spanned, but none of them worked:
dest.writeString(Html.toHtml(mSpanned)); mSpanned = Html.fromHtml(in.readString()); dest.writeValue(mSpanned); mSpanned = (Spanned) in.readValue(DisplayableNotification.class.getClassLoader()); TextUtils.writeToParcel(mSpanned, dest, flags); mSpanned = (Spanned) TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(in);