TextInfoCompatUtilsTests.java revision 05c053992b6ff47826cbb404c6ff025ccd7f9904
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
17package com.android.inputmethod.compat;
18
19import android.graphics.Typeface;
20import android.os.Parcel;
21import android.test.AndroidTestCase;
22import android.test.suitebuilder.annotation.SmallTest;
23import android.text.SpannableString;
24import android.text.Spanned;
25import android.text.TextUtils;
26import android.text.style.StyleSpan;
27import android.text.style.URLSpan;
28import android.view.textservice.TextInfo;
29
30import java.util.Arrays;
31
32@SmallTest
33public class TextInfoCompatUtilsTests extends AndroidTestCase {
34    final private static String TEST_TEXT = "0123456789";
35    final private static int TEST_COOKIE = 0x1234;
36    final private static int TEST_SEQUENCE_NUMBER = 0x4321;
37    final private static int TEST_CHAR_SEQUENCE_START = 1;
38    final private static int TEST_CHAR_SEQUENCE_END = 6;
39    final private static StyleSpan TEST_STYLE_SPAN = new StyleSpan(Typeface.BOLD);
40    final private static int TEST_STYLE_SPAN_START = 4;
41    final private static int TEST_STYLE_SPAN_END = 5;
42    final private static int TEST_STYLE_SPAN_FLAGS = Spanned.SPAN_EXCLUSIVE_INCLUSIVE;
43    final private static URLSpan TEST_URL_SPAN_URL = new URLSpan("http://example.com");
44    final private static int TEST_URL_SPAN_START = 3;
45    final private static int TEST_URL_SPAN_END = 7;
46    final private static int TEST_URL_SPAN_FLAGS = Spanned.SPAN_EXCLUSIVE_EXCLUSIVE;
47
48    public void testGetCharSequence() {
49        final SpannableString text = new SpannableString(TEST_TEXT);
50        text.setSpan(TEST_STYLE_SPAN, TEST_STYLE_SPAN_START, TEST_STYLE_SPAN_END,
51                TEST_STYLE_SPAN_FLAGS);
52        text.setSpan(TEST_URL_SPAN_URL, TEST_URL_SPAN_START, TEST_URL_SPAN_END,
53                TEST_URL_SPAN_FLAGS);
54
55        final TextInfo textInfo = TextInfoCompatUtils.newInstance(text,
56                TEST_CHAR_SEQUENCE_START, TEST_CHAR_SEQUENCE_END, TEST_COOKIE,
57                TEST_SEQUENCE_NUMBER);
58        final Spanned expectedSpanned = (Spanned) text.subSequence(TEST_CHAR_SEQUENCE_START,
59                TEST_CHAR_SEQUENCE_END);
60        final CharSequence actualCharSequence =
61                TextInfoCompatUtils.getCharSequence(textInfo, textInfo.getText());
62
63        // This should be valid even if TextInfo#getCharSequence is not supported.
64        assertTrue(TextUtils.equals(expectedSpanned, actualCharSequence));
65
66        if (TextInfoCompatUtils.isCharSequenceSupported()) {
67            // This is valid only if TextInfo#getCharSequence is supported.
68            assertTrue("should be Spanned", actualCharSequence instanceof Spanned);
69            assertTrue(Arrays.equals(marshall(expectedSpanned), marshall(actualCharSequence)));
70        }
71    }
72
73    private static byte[] marshall(final CharSequence cahrSequence) {
74        Parcel parcel = null;
75        try {
76            parcel = Parcel.obtain();
77            TextUtils.writeToParcel(cahrSequence, parcel, 0);
78            return parcel.marshall();
79        } finally {
80            if (parcel != null) {
81                parcel.recycle();
82                parcel = null;
83            }
84        }
85    }
86}
87