1733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa/*
2733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa * Copyright (C) 2014 The Android Open Source Project
3733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa *
4733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa * Licensed under the Apache License, Version 2.0 (the "License");
5733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa * you may not use this file except in compliance with the License.
6733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa * You may obtain a copy of the License at
7733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa *
8733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa *      http://www.apache.org/licenses/LICENSE-2.0
9733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa *
10733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa * Unless required by applicable law or agreed to in writing, software
11733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa * distributed under the License is distributed on an "AS IS" BASIS,
12733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa * See the License for the specific language governing permissions and
14733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa * limitations under the License.
15733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa */
16733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa
17733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawapackage com.android.inputmethod.compat;
18733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa
19733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawaimport android.graphics.Typeface;
20733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawaimport android.os.Parcel;
21733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawaimport android.test.AndroidTestCase;
22733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawaimport android.test.suitebuilder.annotation.SmallTest;
23733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawaimport android.text.SpannableString;
24733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawaimport android.text.Spanned;
25733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawaimport android.text.TextUtils;
26733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawaimport android.text.style.StyleSpan;
27733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawaimport android.text.style.URLSpan;
28733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawaimport android.view.textservice.TextInfo;
29733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa
30733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawaimport java.util.Arrays;
31733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa
32733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa@SmallTest
33733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawapublic class TextInfoCompatUtilsTests extends AndroidTestCase {
34733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa    final private static String TEST_TEXT = "0123456789";
35733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa    final private static int TEST_COOKIE = 0x1234;
36733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa    final private static int TEST_SEQUENCE_NUMBER = 0x4321;
37733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa    final private static int TEST_CHAR_SEQUENCE_START = 1;
38733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa    final private static int TEST_CHAR_SEQUENCE_END = 6;
39733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa    final private static StyleSpan TEST_STYLE_SPAN = new StyleSpan(Typeface.BOLD);
40733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa    final private static int TEST_STYLE_SPAN_START = 4;
41733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa    final private static int TEST_STYLE_SPAN_END = 5;
42733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa    final private static int TEST_STYLE_SPAN_FLAGS = Spanned.SPAN_EXCLUSIVE_INCLUSIVE;
43733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa    final private static URLSpan TEST_URL_SPAN_URL = new URLSpan("http://example.com");
44733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa    final private static int TEST_URL_SPAN_START = 3;
45733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa    final private static int TEST_URL_SPAN_END = 7;
46733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa    final private static int TEST_URL_SPAN_FLAGS = Spanned.SPAN_EXCLUSIVE_EXCLUSIVE;
47733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa
48733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa    public void testGetCharSequence() {
49733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa        final SpannableString text = new SpannableString(TEST_TEXT);
50733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa        text.setSpan(TEST_STYLE_SPAN, TEST_STYLE_SPAN_START, TEST_STYLE_SPAN_END,
51733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa                TEST_STYLE_SPAN_FLAGS);
52733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa        text.setSpan(TEST_URL_SPAN_URL, TEST_URL_SPAN_START, TEST_URL_SPAN_END,
53733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa                TEST_URL_SPAN_FLAGS);
54733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa
55733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa        final TextInfo textInfo = TextInfoCompatUtils.newInstance(text,
56733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa                TEST_CHAR_SEQUENCE_START, TEST_CHAR_SEQUENCE_END, TEST_COOKIE,
57733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa                TEST_SEQUENCE_NUMBER);
58733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa        final Spanned expectedSpanned = (Spanned) text.subSequence(TEST_CHAR_SEQUENCE_START,
59733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa                TEST_CHAR_SEQUENCE_END);
6005c053992b6ff47826cbb404c6ff025ccd7f9904Yohei Yukawa        final CharSequence actualCharSequence =
61fc137f35c8d7b03676475fbdeabdf82f89782419Yohei Yukawa                TextInfoCompatUtils.getCharSequenceOrString(textInfo);
62733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa
63733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa        // This should be valid even if TextInfo#getCharSequence is not supported.
64733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa        assertTrue(TextUtils.equals(expectedSpanned, actualCharSequence));
65733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa
66733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa        if (TextInfoCompatUtils.isCharSequenceSupported()) {
67733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa            // This is valid only if TextInfo#getCharSequence is supported.
68733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa            assertTrue("should be Spanned", actualCharSequence instanceof Spanned);
69733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa            assertTrue(Arrays.equals(marshall(expectedSpanned), marshall(actualCharSequence)));
70733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa        }
71733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa    }
72733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa
73733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa    private static byte[] marshall(final CharSequence cahrSequence) {
74733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa        Parcel parcel = null;
75733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa        try {
76733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa            parcel = Parcel.obtain();
77733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa            TextUtils.writeToParcel(cahrSequence, parcel, 0);
78733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa            return parcel.marshall();
79733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa        } finally {
80733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa            if (parcel != null) {
81733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa                parcel.recycle();
82733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa                parcel = null;
83733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa            }
84733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa        }
85733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa    }
86733ec699cdc4ace128c664ea4fe7eae46c15d0f0Yohei Yukawa}
87