我试图包裹一个跨越但是当我添加粗体跨度时,我写的测试不再通过了。
以下是测试:
@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;
}
}
当我使用boldify函数评论2行时,它会通过。
以下是加粗功能:
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);
}
我尝试了三种方法来分割Spanned,但没有一种方法可以工作:
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);