1/*
2 * Copyright (C) 2007 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 android.text;
18
19import android.support.test.filters.MediumTest;
20import android.support.test.runner.AndroidJUnit4;
21import android.test.MoreAsserts;
22
23import org.junit.Test;
24import org.junit.runner.RunWith;
25
26@MediumTest
27@RunWith(AndroidJUnit4.class)
28public abstract class SpannableTest {
29
30    protected abstract Spannable newSpannableWithText(String text);
31
32    @Test
33    public void testGetSpans() {
34        Spannable spannable = newSpannableWithText("abcdef");
35        Object emptySpan = new Object();
36        spannable.setSpan(emptySpan, 1, 1, 0);
37        Object unemptySpan = new Object();
38        spannable.setSpan(unemptySpan, 1, 2, 0);
39
40        Object[] spans;
41
42        // Empty spans are included when they merely abut the query region
43        // but other spans are not, unless the query region is empty, in
44        // in which case any abutting spans are returned.
45        spans = spannable.getSpans(0, 1, Object.class);
46        MoreAsserts.assertEquals(new Object[]{emptySpan}, spans);
47        spans = spannable.getSpans(0, 2, Object.class);
48        MoreAsserts.assertEquals(new Object[]{emptySpan, unemptySpan}, spans);
49        spans = spannable.getSpans(1, 2, Object.class);
50        MoreAsserts.assertEquals(new Object[]{emptySpan, unemptySpan}, spans);
51        spans = spannable.getSpans(2, 2, Object.class);
52        MoreAsserts.assertEquals(new Object[]{unemptySpan}, spans);
53    }
54}
55