TextViewCompatTest.java revision 92ad16973b68f0586c585da428946d851a7611bb
1/* 2 * Copyright (C) 2015 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 18package android.support.v4.widget; 19 20import org.junit.After; 21import org.junit.Before; 22import org.junit.Test; 23import org.junit.runner.RunWith; 24 25import android.app.Instrumentation; 26import android.content.res.Resources; 27import android.graphics.Typeface; 28import android.os.Looper; 29import android.test.ActivityInstrumentationTestCase2; 30import android.test.UiThreadTest; 31import android.test.suitebuilder.annotation.SmallTest; 32import android.support.annotation.LayoutRes; 33import android.support.test.InstrumentationRegistry; 34import android.support.v4.test.R; 35import android.support.v4.widget.TextViewCompat; 36import android.util.Log; 37import android.view.LayoutInflater; 38import android.widget.TextView; 39 40import android.support.test.runner.AndroidJUnit4; 41 42public class TextViewCompatTest extends ActivityInstrumentationTestCase2<TestActivity> { 43 private static final String TAG = "TextViewCompatTest"; 44 45 private TextView mTextView; 46 47 public TextViewCompatTest() { 48 super("android.support.v4.widget", TestActivity.class); 49 } 50 51 @Override 52 public void tearDown() throws Exception { 53 if (mTextView != null) { 54 removeTextView(); 55 } 56 57 getInstrumentation().waitForIdleSync(); 58 super.tearDown(); 59 } 60 61 private boolean isMainThread() { 62 return Looper.myLooper() == Looper.getMainLooper(); 63 } 64 65 private void removeTextView() { 66 if (mTextView == null) { 67 return; 68 } 69 if (!isMainThread()) { 70 getInstrumentation().waitForIdleSync(); 71 } 72 try { 73 runTestOnUiThread(new Runnable() { 74 @Override 75 public void run() { 76 getActivity().mContainer.removeAllViews(); 77 } 78 }); 79 } catch (Throwable throwable) { 80 Log.e(TAG, "", throwable); 81 } 82 mTextView = null; 83 } 84 85 private void createAndAddTextView() { 86 final TestActivity activity = getActivity(); 87 mTextView = new TextView(activity); 88 activity.mContainer.addView(mTextView); 89 } 90 91 @UiThreadTest 92 @SmallTest 93 public void testMaxLines() throws Throwable { 94 createAndAddTextView(); 95 final int maxLinesCount = 4; 96 mTextView.setMaxLines(maxLinesCount); 97 98 assertEquals("Empty view: Max lines must match", TextViewCompat.getMaxLines(mTextView), 99 maxLinesCount); 100 101 mTextView.setText(R.string.test_text_short); 102 assertEquals("Short text: Max lines must match", TextViewCompat.getMaxLines(mTextView), 103 maxLinesCount); 104 105 mTextView.setText(R.string.test_text_medium); 106 assertEquals("Medium text: Max lines must match", TextViewCompat.getMaxLines(mTextView), 107 maxLinesCount); 108 109 mTextView.setText(R.string.test_text_long); 110 assertEquals("Long text: Max lines must match", TextViewCompat.getMaxLines(mTextView), 111 maxLinesCount); 112 } 113 114 @UiThreadTest 115 @SmallTest 116 public void testMinLines() throws Throwable { 117 createAndAddTextView(); 118 final int minLinesCount = 3; 119 mTextView.setMinLines(minLinesCount); 120 121 assertEquals("Empty view: Min lines must match", TextViewCompat.getMinLines(mTextView), 122 minLinesCount); 123 124 mTextView.setText(R.string.test_text_short); 125 assertEquals("Short text: Min lines must match", TextViewCompat.getMinLines(mTextView), 126 minLinesCount); 127 128 mTextView.setText(R.string.test_text_medium); 129 assertEquals("Medium text: Min lines must match", TextViewCompat.getMinLines(mTextView), 130 minLinesCount); 131 132 mTextView.setText(R.string.test_text_long); 133 assertEquals("Long text: Min lines must match", TextViewCompat.getMinLines(mTextView), 134 minLinesCount); 135 } 136 137 @UiThreadTest 138 @SmallTest 139 public void testStyle() throws Throwable { 140 createAndAddTextView(); 141 142 TextViewCompat.setTextAppearance(mTextView, R.style.TextMediumStyle); 143 144 final Resources res = getActivity().getResources(); 145 assertEquals("Styled text view: style", mTextView.getTypeface().getStyle(), 146 Typeface.ITALIC); 147 assertEquals("Styled text view: color", mTextView.getTextColors().getDefaultColor(), 148 res.getColor(R.color.text_color)); 149 assertEquals("Styled text view: size", mTextView.getTextSize(), 150 res.getDimension(R.dimen.text_medium_size)); 151 } 152} 153