summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Wilson Wu <wilsonwu@google.com> 2020-09-22 13:51:36 +0800
committer Wilson Wu <wilsonwu@google.com> 2020-09-23 19:20:07 +0800
commitec4ad52274b9e842e5c6040e1eeee6c600eba089 (patch)
treef7fe59dadb868296e8a0af2da628b191fab47c77
parenta67c5344ac3033c7f72e354919c16bbe46ff5f77 (diff)
Test span after initial surrounding text retrieval
CL[1] make a deep copy with Parcelable for initial surrounding text. With CL[1], the text spans after initial surrounding text retrieval should only have Parcelable spans, and those Parcelable spans are different from the original Span instance. [1]: I75edcf6046467f2776d62bb38137f9d15dbfab52 Bug: 163175243 Test: atest FrameworksCoreTests:EditorInfoTest Change-Id: I9876bf5784bf3c3d93f249f7230a91bb2177e4fd
-rw-r--r--core/tests/coretests/src/android/view/inputmethod/EditorInfoTest.java56
1 files changed, 56 insertions, 0 deletions
diff --git a/core/tests/coretests/src/android/view/inputmethod/EditorInfoTest.java b/core/tests/coretests/src/android/view/inputmethod/EditorInfoTest.java
index 5c410878c99d..92fb52837c36 100644
--- a/core/tests/coretests/src/android/view/inputmethod/EditorInfoTest.java
+++ b/core/tests/coretests/src/android/view/inputmethod/EditorInfoTest.java
@@ -17,16 +17,23 @@
package android.view.inputmethod;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.anyInt;
import android.annotation.Nullable;
+import android.graphics.BlurMaskFilter;
import android.os.Parcel;
import android.os.UserHandle;
+import android.text.Spannable;
+import android.text.SpannableString;
import android.text.SpannableStringBuilder;
import android.text.TextUtils;
+import android.text.style.MaskFilterSpan;
+import android.text.style.UnderlineSpan;
import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4;
@@ -280,6 +287,55 @@ public class EditorInfoTest {
editorInfo.getInitialTextBeforeCursor(/* length= */ 60, /* flags= */ 1);
}
+ @Test
+ public void testSpanAfterSurroundingTextRetrieval() {
+ final int flags = Spannable.SPAN_EXCLUSIVE_EXCLUSIVE;
+ final SpannableStringBuilder sb =
+ new SpannableStringBuilder("ParcelableSpan and non-ParcelableSpan test");
+ final int parcelableStart = 0;
+ final int parcelableEnd = 14;
+ final int nonParcelableStart = 19;
+ final int nonParcelableEnd = 37;
+ final UnderlineSpan parcelableSpan = new UnderlineSpan();
+ final MaskFilterSpan nonParcelableSpan =
+ new MaskFilterSpan(new BlurMaskFilter(5f, BlurMaskFilter.Blur.NORMAL));
+
+ // Set spans
+ sb.setSpan(parcelableSpan, parcelableStart, parcelableEnd, flags);
+ sb.setSpan(nonParcelableSpan, nonParcelableStart, nonParcelableEnd, flags);
+
+ Object[] spansBefore = sb.getSpans(/* queryStart= */ 0, sb.length(), Object.class);
+ Object[] parcelableSpanBefore = sb.getSpans(parcelableStart, parcelableEnd, Object.class);
+
+ // Verify the original spans length is 2, include ParcelableSpan and non-ParcelableSpan.
+ assertNotNull(spansBefore);
+ assertEquals(2, spansBefore.length);
+
+ // Set initial surrounding text then retrieve the text.
+ EditorInfo editorInfo = new EditorInfo();
+ editorInfo.initialSelStart = sb.length();
+ editorInfo.initialSelEnd = sb.length();
+ editorInfo.inputType = EditorInfo.TYPE_CLASS_TEXT;
+ editorInfo.setInitialSurroundingText(sb);
+ SpannableString textBeforeCursor =
+ (SpannableString) editorInfo.getInitialTextBeforeCursor(
+ /* length= */ 60, /* flags= */ 1);
+
+ Object[] spansAfter =
+ textBeforeCursor.getSpans(/* queryStart= */ 0, sb.length(), Object.class);
+ Object[] parcelableSpanAfter =
+ textBeforeCursor.getSpans(parcelableStart, parcelableEnd, Object.class);
+ Object[] nonParcelableSpanAfter =
+ textBeforeCursor.getSpans(nonParcelableStart, nonParcelableEnd, Object.class);
+
+ // Verify only remain ParcelableSpan and it's different from the original Span instance.
+ assertNotNull(spansAfter);
+ assertEquals(1, spansAfter.length);
+ assertEquals(1, parcelableSpanAfter.length);
+ assertEquals(0, nonParcelableSpanAfter.length);
+ assertNotEquals(parcelableSpanBefore, parcelableSpanAfter);
+ }
+
private static void assertExpectedTextLength(EditorInfo editorInfo,
@Nullable Integer expectBeforeCursorLength, @Nullable Integer expectSelectionLength,
@Nullable Integer expectAfterCursorLength) {