Skip to content

Commit 19653c6

Browse files
cco3Android Git Automerger
authored andcommitted
am 425db47: Merge "CHAR_SEQUENCE_CREATOR cannot handle null string"
* commit '425db473a05e4204301bf46d4ab6f51b12d9c447': CHAR_SEQUENCE_CREATOR cannot handle null string
2 parents a47c0d2 + 425db47 commit 19653c6

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

core/java/android/text/TextUtils.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -627,10 +627,16 @@ private static void writeWhere(Parcel p, Spanned sp, Object o) {
627627
public CharSequence createFromParcel(Parcel p) {
628628
int kind = p.readInt();
629629

630-
if (kind == 1)
631-
return p.readString();
630+
String string = p.readString();
631+
if (string == null) {
632+
return null;
633+
}
634+
635+
if (kind == 1) {
636+
return string;
637+
}
632638

633-
SpannableString sp = new SpannableString(p.readString());
639+
SpannableString sp = new SpannableString(string);
634640

635641
while (true) {
636642
kind = p.readInt();

core/tests/coretests/src/android/text/TextUtilsTest.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package android.text;
1818

1919
import android.graphics.Paint;
20+
import android.os.Parcel;
2021
import android.test.suitebuilder.annotation.LargeTest;
2122
import android.test.suitebuilder.annotation.SmallTest;
2223
import android.text.Spannable;
@@ -352,6 +353,51 @@ public void testDelimitedStringContains() {
352353
assertFalse(TextUtils.delimitedStringContains("network,mock,gpsx", ',', "gps"));
353354
}
354355

356+
@SmallTest
357+
public void testCharSequenceCreator() {
358+
Parcel p = Parcel.obtain();
359+
TextUtils.writeToParcel(null, p, 0);
360+
CharSequence text = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(p);
361+
assertNull("null CharSequence should generate null from parcel", text);
362+
p = Parcel.obtain();
363+
TextUtils.writeToParcel("test", p, 0);
364+
text = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(p);
365+
assertEquals("conversion to/from parcel failed", "test", text);
366+
}
367+
368+
@SmallTest
369+
public void testCharSequenceCreatorNull() {
370+
Parcel p;
371+
CharSequence text;
372+
p = Parcel.obtain();
373+
TextUtils.writeToParcel(null, p, 0);
374+
p.setDataPosition(0);
375+
text = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(p);
376+
assertNull("null CharSequence should generate null from parcel", text);
377+
}
378+
379+
@SmallTest
380+
public void testCharSequenceCreatorSpannable() {
381+
Parcel p;
382+
CharSequence text;
383+
p = Parcel.obtain();
384+
TextUtils.writeToParcel(new SpannableString("test"), p, 0);
385+
p.setDataPosition(0);
386+
text = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(p);
387+
assertEquals("conversion to/from parcel failed", "test", text.toString());
388+
}
389+
390+
@SmallTest
391+
public void testCharSequenceCreatorString() {
392+
Parcel p;
393+
CharSequence text;
394+
p = Parcel.obtain();
395+
TextUtils.writeToParcel("test", p, 0);
396+
p.setDataPosition(0);
397+
text = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(p);
398+
assertEquals("conversion to/from parcel failed", "test", text.toString());
399+
}
400+
355401
/**
356402
* CharSequence wrapper for testing the cases where text is copied into
357403
* a char array instead of working from a String or a Spanned.

0 commit comments

Comments
 (0)