• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2014 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.inputmethod.compat;
18 
19 import static org.junit.Assert.assertTrue;
20 
21 import android.graphics.Typeface;
22 import android.os.Parcel;
23 import android.text.SpannableString;
24 import android.text.Spanned;
25 import android.text.TextUtils;
26 import android.text.style.StyleSpan;
27 import android.text.style.URLSpan;
28 import android.view.textservice.TextInfo;
29 
30 import androidx.test.filters.SmallTest;
31 import androidx.test.runner.AndroidJUnit4;
32 
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 
36 import java.util.Arrays;
37 
38 @SmallTest
39 @RunWith(AndroidJUnit4.class)
40 public class TextInfoCompatUtilsTests {
41     final private static String TEST_TEXT = "0123456789";
42     final private static int TEST_COOKIE = 0x1234;
43     final private static int TEST_SEQUENCE_NUMBER = 0x4321;
44     final private static int TEST_CHAR_SEQUENCE_START = 1;
45     final private static int TEST_CHAR_SEQUENCE_END = 6;
46     final private static StyleSpan TEST_STYLE_SPAN = new StyleSpan(Typeface.BOLD);
47     final private static int TEST_STYLE_SPAN_START = 4;
48     final private static int TEST_STYLE_SPAN_END = 5;
49     final private static int TEST_STYLE_SPAN_FLAGS = Spanned.SPAN_EXCLUSIVE_INCLUSIVE;
50     final private static URLSpan TEST_URL_SPAN_URL = new URLSpan("http://example.com");
51     final private static int TEST_URL_SPAN_START = 3;
52     final private static int TEST_URL_SPAN_END = 7;
53     final private static int TEST_URL_SPAN_FLAGS = Spanned.SPAN_EXCLUSIVE_EXCLUSIVE;
54 
55     @Test
testGetCharSequence()56     public void testGetCharSequence() {
57         final SpannableString text = new SpannableString(TEST_TEXT);
58         text.setSpan(TEST_STYLE_SPAN, TEST_STYLE_SPAN_START, TEST_STYLE_SPAN_END,
59                 TEST_STYLE_SPAN_FLAGS);
60         text.setSpan(TEST_URL_SPAN_URL, TEST_URL_SPAN_START, TEST_URL_SPAN_END,
61                 TEST_URL_SPAN_FLAGS);
62 
63         final TextInfo textInfo = TextInfoCompatUtils.newInstance(text,
64                 TEST_CHAR_SEQUENCE_START, TEST_CHAR_SEQUENCE_END, TEST_COOKIE,
65                 TEST_SEQUENCE_NUMBER);
66         final Spanned expectedSpanned = (Spanned) text.subSequence(TEST_CHAR_SEQUENCE_START,
67                 TEST_CHAR_SEQUENCE_END);
68         final CharSequence actualCharSequence =
69                 TextInfoCompatUtils.getCharSequenceOrString(textInfo);
70 
71         // This should be valid even if TextInfo#getCharSequence is not supported.
72         assertTrue(TextUtils.equals(expectedSpanned, actualCharSequence));
73 
74         if (TextInfoCompatUtils.isCharSequenceSupported()) {
75             // This is valid only if TextInfo#getCharSequence is supported.
76             assertTrue("should be Spanned", actualCharSequence instanceof Spanned);
77             assertTrue(Arrays.equals(marshall(expectedSpanned), marshall(actualCharSequence)));
78         }
79     }
80 
marshall(final CharSequence cahrSequence)81     private static byte[] marshall(final CharSequence cahrSequence) {
82         Parcel parcel = null;
83         try {
84             parcel = Parcel.obtain();
85             TextUtils.writeToParcel(cahrSequence, parcel, 0);
86             return parcel.marshall();
87         } finally {
88             if (parcel != null) {
89                 parcel.recycle();
90                 parcel = null;
91             }
92         }
93     }
94 }
95