TestTextWithHighlightingFactory.java revision 0454960b4b9e43b75bbfd0fb296c25d59111a765
1/*
2 * Copyright (C) 2011 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.contacts.format;
18
19import com.android.contacts.widget.TextWithHighlighting;
20import com.android.contacts.widget.TextWithHighlightingFactory;
21
22import android.database.CharArrayBuffer;
23import android.graphics.Typeface;
24import android.test.suitebuilder.annotation.SmallTest;
25import android.text.SpannableStringBuilder;
26import android.text.style.StyleSpan;
27
28/** A factory for {@link TextWithHighlighting} that wraps its parts in italics. */
29@SmallTest
30public final class TestTextWithHighlightingFactory implements TextWithHighlightingFactory {
31    /** A {@link TextWithHighlighting} implementation that wraps its parts in italics. */
32    private final static class TestTextWithHighlighting extends SpannableStringBuilder
33            implements TextWithHighlighting {
34        @Override
35        public void setText(CharArrayBuffer baseText, CharArrayBuffer highlightedText) {
36            append(new String(baseText.data, 0, baseText.sizeCopied));
37            append(' ');
38            append(new String(highlightedText.data, 0, highlightedText.sizeCopied));
39            setSpan(new StyleSpan(Typeface.ITALIC), 0, baseText.sizeCopied, 0);
40            setSpan(new StyleSpan(Typeface.ITALIC), baseText.sizeCopied + 1,
41                    baseText.sizeCopied + 1 + highlightedText.sizeCopied, 0);
42        }
43    }
44
45    @Override
46    public TextWithHighlighting createTextWithHighlighting() {
47        return new TestTextWithHighlighting();
48    }
49}