TextViewCompatTest.java revision 156b807bb159a532412685e6b7cfd4f0a61d1b5e
1d9d9aec0e671cf0db89a8b58a81b1980834f1dc2Kirill Grouchnikov/*
2d9d9aec0e671cf0db89a8b58a81b1980834f1dc2Kirill Grouchnikov * Copyright (C) 2015 The Android Open Source Project
3d9d9aec0e671cf0db89a8b58a81b1980834f1dc2Kirill Grouchnikov *
4d9d9aec0e671cf0db89a8b58a81b1980834f1dc2Kirill Grouchnikov * Licensed under the Apache License, Version 2.0 (the "License");
5d9d9aec0e671cf0db89a8b58a81b1980834f1dc2Kirill Grouchnikov * you may not use this file except in compliance with the License.
6d9d9aec0e671cf0db89a8b58a81b1980834f1dc2Kirill Grouchnikov * You may obtain a copy of the License at
7d9d9aec0e671cf0db89a8b58a81b1980834f1dc2Kirill Grouchnikov *
8d9d9aec0e671cf0db89a8b58a81b1980834f1dc2Kirill Grouchnikov *      http://www.apache.org/licenses/LICENSE-2.0
9d9d9aec0e671cf0db89a8b58a81b1980834f1dc2Kirill Grouchnikov *
10d9d9aec0e671cf0db89a8b58a81b1980834f1dc2Kirill Grouchnikov * Unless required by applicable law or agreed to in writing, software
11d9d9aec0e671cf0db89a8b58a81b1980834f1dc2Kirill Grouchnikov * distributed under the License is distributed on an "AS IS" BASIS,
12d9d9aec0e671cf0db89a8b58a81b1980834f1dc2Kirill Grouchnikov * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13d9d9aec0e671cf0db89a8b58a81b1980834f1dc2Kirill Grouchnikov * See the License for the specific language governing permissions and
14d9d9aec0e671cf0db89a8b58a81b1980834f1dc2Kirill Grouchnikov * limitations under the License.
15d9d9aec0e671cf0db89a8b58a81b1980834f1dc2Kirill Grouchnikov */
16d9d9aec0e671cf0db89a8b58a81b1980834f1dc2Kirill Grouchnikov
17d9d9aec0e671cf0db89a8b58a81b1980834f1dc2Kirill Grouchnikov
18d9d9aec0e671cf0db89a8b58a81b1980834f1dc2Kirill Grouchnikovpackage android.support.v4.widget;
19d9d9aec0e671cf0db89a8b58a81b1980834f1dc2Kirill Grouchnikov
20d9d9aec0e671cf0db89a8b58a81b1980834f1dc2Kirill Grouchnikovimport org.junit.After;
21d9d9aec0e671cf0db89a8b58a81b1980834f1dc2Kirill Grouchnikovimport org.junit.Before;
22d9d9aec0e671cf0db89a8b58a81b1980834f1dc2Kirill Grouchnikovimport org.junit.Test;
23d9d9aec0e671cf0db89a8b58a81b1980834f1dc2Kirill Grouchnikovimport org.junit.runner.RunWith;
24d9d9aec0e671cf0db89a8b58a81b1980834f1dc2Kirill Grouchnikov
25d9d9aec0e671cf0db89a8b58a81b1980834f1dc2Kirill Grouchnikovimport android.app.Instrumentation;
2692ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikovimport android.content.res.Resources;
2792ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikovimport android.graphics.Typeface;
28156b807bb159a532412685e6b7cfd4f0a61d1b5eKirill Grouchnikovimport android.graphics.drawable.Drawable;
29156b807bb159a532412685e6b7cfd4f0a61d1b5eKirill Grouchnikovimport android.graphics.drawable.ColorDrawable;
3092ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikovimport android.os.Looper;
31d9d9aec0e671cf0db89a8b58a81b1980834f1dc2Kirill Grouchnikovimport android.test.ActivityInstrumentationTestCase2;
3292ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikovimport android.test.UiThreadTest;
3392ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikovimport android.test.suitebuilder.annotation.SmallTest;
3492ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikovimport android.support.annotation.LayoutRes;
35d9d9aec0e671cf0db89a8b58a81b1980834f1dc2Kirill Grouchnikovimport android.support.test.InstrumentationRegistry;
3692ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikovimport android.support.v4.test.R;
37156b807bb159a532412685e6b7cfd4f0a61d1b5eKirill Grouchnikovimport android.support.v4.view.ViewCompat;
38d9d9aec0e671cf0db89a8b58a81b1980834f1dc2Kirill Grouchnikovimport android.support.v4.widget.TextViewCompat;
39156b807bb159a532412685e6b7cfd4f0a61d1b5eKirill Grouchnikovimport android.util.DisplayMetrics;
40d9d9aec0e671cf0db89a8b58a81b1980834f1dc2Kirill Grouchnikovimport android.util.Log;
41156b807bb159a532412685e6b7cfd4f0a61d1b5eKirill Grouchnikovimport android.util.TypedValue;
4292ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikovimport android.view.LayoutInflater;
43156b807bb159a532412685e6b7cfd4f0a61d1b5eKirill Grouchnikovimport android.view.View;
44d9d9aec0e671cf0db89a8b58a81b1980834f1dc2Kirill Grouchnikovimport android.widget.TextView;
45d9d9aec0e671cf0db89a8b58a81b1980834f1dc2Kirill Grouchnikov
46d9d9aec0e671cf0db89a8b58a81b1980834f1dc2Kirill Grouchnikovpublic class TextViewCompatTest extends ActivityInstrumentationTestCase2<TestActivity> {
4792ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov    private static final String TAG = "TextViewCompatTest";
48d9d9aec0e671cf0db89a8b58a81b1980834f1dc2Kirill Grouchnikov
4992ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov    private TextView mTextView;
50d9d9aec0e671cf0db89a8b58a81b1980834f1dc2Kirill Grouchnikov
51d9d9aec0e671cf0db89a8b58a81b1980834f1dc2Kirill Grouchnikov    public TextViewCompatTest() {
52d9d9aec0e671cf0db89a8b58a81b1980834f1dc2Kirill Grouchnikov        super("android.support.v4.widget", TestActivity.class);
53d9d9aec0e671cf0db89a8b58a81b1980834f1dc2Kirill Grouchnikov    }
54d9d9aec0e671cf0db89a8b58a81b1980834f1dc2Kirill Grouchnikov
55d9d9aec0e671cf0db89a8b58a81b1980834f1dc2Kirill Grouchnikov    @Override
56d9d9aec0e671cf0db89a8b58a81b1980834f1dc2Kirill Grouchnikov    public void tearDown() throws Exception {
5792ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov        if (mTextView != null) {
5892ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov            removeTextView();
5992ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov        }
6092ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov
61d9d9aec0e671cf0db89a8b58a81b1980834f1dc2Kirill Grouchnikov        getInstrumentation().waitForIdleSync();
62d9d9aec0e671cf0db89a8b58a81b1980834f1dc2Kirill Grouchnikov        super.tearDown();
63d9d9aec0e671cf0db89a8b58a81b1980834f1dc2Kirill Grouchnikov    }
64d9d9aec0e671cf0db89a8b58a81b1980834f1dc2Kirill Grouchnikov
6592ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov    private boolean isMainThread() {
6692ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov        return Looper.myLooper() == Looper.getMainLooper();
6792ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov    }
6892ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov
6992ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov    private void removeTextView() {
7092ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov        if (mTextView == null) {
7192ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov            return;
7292ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov        }
7392ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov        if (!isMainThread()) {
7492ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov            getInstrumentation().waitForIdleSync();
7592ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov        }
7692ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov        try {
7792ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov            runTestOnUiThread(new Runnable() {
7892ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov                @Override
7992ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov                public void run() {
8092ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov                    getActivity().mContainer.removeAllViews();
8192ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov                }
8292ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov            });
8392ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov        } catch (Throwable throwable) {
8492ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov            Log.e(TAG, "", throwable);
8592ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov        }
8692ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov        mTextView = null;
8792ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov    }
8892ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov
8992ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov    private void createAndAddTextView() {
9092ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov        final TestActivity activity = getActivity();
9192ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov        mTextView = new TextView(activity);
9292ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov        activity.mContainer.addView(mTextView);
9392ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov    }
9492ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov
9592ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov    @UiThreadTest
9692ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov    @SmallTest
97d9d9aec0e671cf0db89a8b58a81b1980834f1dc2Kirill Grouchnikov    public void testMaxLines() throws Throwable {
9892ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov        createAndAddTextView();
9992ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov        final int maxLinesCount = 4;
10092ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov        mTextView.setMaxLines(maxLinesCount);
10192ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov
10292ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov        assertEquals("Empty view: Max lines must match", TextViewCompat.getMaxLines(mTextView),
10392ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov                maxLinesCount);
10492ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov
10592ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov        mTextView.setText(R.string.test_text_short);
10692ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov        assertEquals("Short text: Max lines must match", TextViewCompat.getMaxLines(mTextView),
10792ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov                maxLinesCount);
10892ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov
10992ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov        mTextView.setText(R.string.test_text_medium);
11092ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov        assertEquals("Medium text: Max lines must match", TextViewCompat.getMaxLines(mTextView),
11192ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov                maxLinesCount);
11292ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov
11392ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov        mTextView.setText(R.string.test_text_long);
11492ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov        assertEquals("Long text: Max lines must match", TextViewCompat.getMaxLines(mTextView),
11592ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov                maxLinesCount);
11692ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov    }
11792ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov
11892ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov    @UiThreadTest
11992ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov    @SmallTest
12092ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov    public void testMinLines() throws Throwable {
12192ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov        createAndAddTextView();
12292ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov        final int minLinesCount = 3;
12392ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov        mTextView.setMinLines(minLinesCount);
12492ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov
12592ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov        assertEquals("Empty view: Min lines must match", TextViewCompat.getMinLines(mTextView),
12692ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov                minLinesCount);
12792ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov
12892ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov        mTextView.setText(R.string.test_text_short);
12992ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov        assertEquals("Short text: Min lines must match", TextViewCompat.getMinLines(mTextView),
13092ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov                minLinesCount);
13192ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov
13292ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov        mTextView.setText(R.string.test_text_medium);
13392ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov        assertEquals("Medium text: Min lines must match", TextViewCompat.getMinLines(mTextView),
13492ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov                minLinesCount);
13592ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov
13692ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov        mTextView.setText(R.string.test_text_long);
13792ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov        assertEquals("Long text: Min lines must match", TextViewCompat.getMinLines(mTextView),
13892ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov                minLinesCount);
13992ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov    }
14092ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov
14192ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov    @UiThreadTest
14292ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov    @SmallTest
14392ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov    public void testStyle() throws Throwable {
14492ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov        createAndAddTextView();
145d9d9aec0e671cf0db89a8b58a81b1980834f1dc2Kirill Grouchnikov
14692ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov        TextViewCompat.setTextAppearance(mTextView, R.style.TextMediumStyle);
147d9d9aec0e671cf0db89a8b58a81b1980834f1dc2Kirill Grouchnikov
14892ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov        final Resources res = getActivity().getResources();
14992ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov        assertEquals("Styled text view: style", mTextView.getTypeface().getStyle(),
15092ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov                Typeface.ITALIC);
15192ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov        assertEquals("Styled text view: color", mTextView.getTextColors().getDefaultColor(),
15292ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov                res.getColor(R.color.text_color));
15392ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov        assertEquals("Styled text view: size", mTextView.getTextSize(),
15492ad16973b68f0586c585da428946d851a7611bbKirill Grouchnikov                res.getDimension(R.dimen.text_medium_size));
155d9d9aec0e671cf0db89a8b58a81b1980834f1dc2Kirill Grouchnikov    }
156156b807bb159a532412685e6b7cfd4f0a61d1b5eKirill Grouchnikov
157156b807bb159a532412685e6b7cfd4f0a61d1b5eKirill Grouchnikov    @UiThreadTest
158156b807bb159a532412685e6b7cfd4f0a61d1b5eKirill Grouchnikov    @SmallTest
159156b807bb159a532412685e6b7cfd4f0a61d1b5eKirill Grouchnikov    public void testCompoundDrawablesRelative() throws Throwable {
160156b807bb159a532412685e6b7cfd4f0a61d1b5eKirill Grouchnikov        createAndAddTextView();
161156b807bb159a532412685e6b7cfd4f0a61d1b5eKirill Grouchnikov
162156b807bb159a532412685e6b7cfd4f0a61d1b5eKirill Grouchnikov        final Drawable drawableStart = new ColorDrawable(0xFFFF0000);
163156b807bb159a532412685e6b7cfd4f0a61d1b5eKirill Grouchnikov        drawableStart.setBounds(0, 0, 20, 20);
164156b807bb159a532412685e6b7cfd4f0a61d1b5eKirill Grouchnikov        final Drawable drawableTop = new ColorDrawable(0xFF00FF00);
165156b807bb159a532412685e6b7cfd4f0a61d1b5eKirill Grouchnikov        drawableTop.setBounds(0, 0, 20, 20);
166156b807bb159a532412685e6b7cfd4f0a61d1b5eKirill Grouchnikov        final Drawable drawableEnd = new ColorDrawable(0xFF0000FF);
167156b807bb159a532412685e6b7cfd4f0a61d1b5eKirill Grouchnikov        drawableEnd.setBounds(0, 0, 20, 20);
168156b807bb159a532412685e6b7cfd4f0a61d1b5eKirill Grouchnikov
169156b807bb159a532412685e6b7cfd4f0a61d1b5eKirill Grouchnikov        mTextView.setText(R.string.test_text_medium);
170156b807bb159a532412685e6b7cfd4f0a61d1b5eKirill Grouchnikov        TextViewCompat.setCompoundDrawablesRelative(mTextView, drawableStart, drawableTop,
171156b807bb159a532412685e6b7cfd4f0a61d1b5eKirill Grouchnikov                drawableEnd, null);
172156b807bb159a532412685e6b7cfd4f0a61d1b5eKirill Grouchnikov
173156b807bb159a532412685e6b7cfd4f0a61d1b5eKirill Grouchnikov        // Explicitly measure and layout the text view so that the core TextView updates its
174156b807bb159a532412685e6b7cfd4f0a61d1b5eKirill Grouchnikov        // internal tracking of where each drawable is positioned relative to the text.
175156b807bb159a532412685e6b7cfd4f0a61d1b5eKirill Grouchnikov        final DisplayMetrics metrics = getActivity().getResources().getDisplayMetrics();
176156b807bb159a532412685e6b7cfd4f0a61d1b5eKirill Grouchnikov        int textViewWidthPx = (int) TypedValue.applyDimension(
177156b807bb159a532412685e6b7cfd4f0a61d1b5eKirill Grouchnikov                TypedValue.COMPLEX_UNIT_DIP, 200, metrics);
178156b807bb159a532412685e6b7cfd4f0a61d1b5eKirill Grouchnikov        int textViewHeightPx = (int) TypedValue.applyDimension(
179156b807bb159a532412685e6b7cfd4f0a61d1b5eKirill Grouchnikov                TypedValue.COMPLEX_UNIT_DIP, 60, metrics);
180156b807bb159a532412685e6b7cfd4f0a61d1b5eKirill Grouchnikov        mTextView.measure(
181156b807bb159a532412685e6b7cfd4f0a61d1b5eKirill Grouchnikov                View.MeasureSpec.makeMeasureSpec(textViewWidthPx, View.MeasureSpec.EXACTLY),
182156b807bb159a532412685e6b7cfd4f0a61d1b5eKirill Grouchnikov                View.MeasureSpec.makeMeasureSpec(textViewHeightPx, View.MeasureSpec.EXACTLY));
183156b807bb159a532412685e6b7cfd4f0a61d1b5eKirill Grouchnikov        mTextView.layout(0, 0, textViewWidthPx, textViewHeightPx);
184156b807bb159a532412685e6b7cfd4f0a61d1b5eKirill Grouchnikov
185156b807bb159a532412685e6b7cfd4f0a61d1b5eKirill Grouchnikov        final Drawable[] drawablesAbsolute = mTextView.getCompoundDrawables();
186156b807bb159a532412685e6b7cfd4f0a61d1b5eKirill Grouchnikov        assertEquals("Compound drawable: left", drawablesAbsolute[0], drawableStart);
187156b807bb159a532412685e6b7cfd4f0a61d1b5eKirill Grouchnikov        assertEquals("Compound drawable: top", drawablesAbsolute[1], drawableTop);
188156b807bb159a532412685e6b7cfd4f0a61d1b5eKirill Grouchnikov        assertEquals("Compound drawable: right", drawablesAbsolute[2], drawableEnd);
189156b807bb159a532412685e6b7cfd4f0a61d1b5eKirill Grouchnikov        assertNull("Compound drawable: bottom", drawablesAbsolute[3]);
190156b807bb159a532412685e6b7cfd4f0a61d1b5eKirill Grouchnikov    }
191d9d9aec0e671cf0db89a8b58a81b1980834f1dc2Kirill Grouchnikov}
192