1 /*
2  * Copyright (C) 2013 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.latin.utils;
18 
19 import android.test.AndroidTestCase;
20 import android.test.suitebuilder.annotation.SmallTest;
21 import android.text.style.SuggestionSpan;
22 import android.text.style.URLSpan;
23 import android.text.SpannableStringBuilder;
24 import android.text.Spannable;
25 import android.text.Spanned;
26 
27 @SmallTest
28 public class SpannableStringUtilsTests extends AndroidTestCase {
testConcatWithSuggestionSpansOnly()29     public void testConcatWithSuggestionSpansOnly() {
30         SpannableStringBuilder s = new SpannableStringBuilder("test string\ntest string\n"
31                 + "test string\ntest string\ntest string\ntest string\ntest string\ntest string\n"
32                 + "test string\ntest string\n");
33         final int N = 10;
34         for (int i = 0; i < N; ++i) {
35             // Put a PARAGRAPH-flagged span that should not be found in the result.
36             s.setSpan(new SuggestionSpan(getContext(),
37                     new String[] {"" + i}, Spannable.SPAN_PARAGRAPH),
38                     i * 12, i * 12 + 12, Spannable.SPAN_PARAGRAPH);
39             // Put a normal suggestion span that should be found in the result.
40             s.setSpan(new SuggestionSpan(getContext(), new String[] {"" + i}, 0), i, i * 2, 0);
41             // Put a URL span than should not be found in the result.
42             s.setSpan(new URLSpan("http://a"), i, i * 2, 0);
43         }
44 
45         final CharSequence a = s.subSequence(0, 15);
46         final CharSequence b = s.subSequence(15, s.length());
47         final Spanned result =
48                 (Spanned)SpannableStringUtils.concatWithNonParagraphSuggestionSpansOnly(a, b);
49 
50         Object[] spans = result.getSpans(0, result.length(), SuggestionSpan.class);
51         for (int i = 0; i < spans.length; i++) {
52             final int flags = result.getSpanFlags(spans[i]);
53             assertEquals("Should not find a span with PARAGRAPH flag",
54                     flags & Spannable.SPAN_PARAGRAPH, 0);
55             assertTrue("Should be a SuggestionSpan", spans[i] instanceof SuggestionSpan);
56         }
57     }
58 }
59