TextViewCompatTest.java revision bf14265885d1cb7ecd6db9b0109a8b033181747b
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 android.content.res.Resources; 21import android.graphics.drawable.ColorDrawable; 22import android.graphics.drawable.Drawable; 23import android.support.annotation.ColorInt; 24import android.support.compat.test.R; 25import android.support.v4.BaseInstrumentationTestCase; 26import android.support.v4.testutils.TestUtils; 27import android.support.v4.view.ViewCompat; 28import android.test.suitebuilder.annotation.MediumTest; 29import android.test.suitebuilder.annotation.SmallTest; 30import android.widget.TextView; 31import org.junit.Before; 32import org.junit.Test; 33 34import static android.support.test.espresso.Espresso.onView; 35import static android.support.test.espresso.matcher.ViewMatchers.withId; 36import static android.support.v4.testutils.LayoutDirectionActions.setLayoutDirection; 37import static android.support.v4.testutils.TextViewActions.*; 38import static org.junit.Assert.*; 39 40public class TextViewCompatTest extends BaseInstrumentationTestCase<TextViewTestActivity> { 41 private static final String TAG = "TextViewCompatTest"; 42 43 private TextView mTextView; 44 45 private class TestDrawable extends ColorDrawable { 46 private int mWidth; 47 private int mHeight; 48 49 public TestDrawable(@ColorInt int color, int width, int height) { 50 super(color); 51 mWidth = width; 52 mHeight = height; 53 } 54 55 @Override 56 public int getIntrinsicWidth() { 57 return mWidth; 58 } 59 60 @Override 61 public int getIntrinsicHeight() { 62 return mHeight; 63 } 64 } 65 66 public TextViewCompatTest() { 67 super(TextViewTestActivity.class); 68 } 69 70 @Before 71 public void setUp() { 72 mTextView = (TextView) mActivityTestRule.getActivity().findViewById(R.id.text_view); 73 } 74 75 @Test 76 @SmallTest 77 public void testMaxLines() throws Throwable { 78 final int maxLinesCount = 4; 79 onView(withId(R.id.text_view)).perform(setMaxLines(maxLinesCount)); 80 81 assertEquals("Empty view: Max lines must match", TextViewCompat.getMaxLines(mTextView), 82 maxLinesCount); 83 84 onView(withId(R.id.text_view)).perform(setText(R.string.test_text_short)); 85 assertEquals("Short text: Max lines must match", TextViewCompat.getMaxLines(mTextView), 86 maxLinesCount); 87 88 onView(withId(R.id.text_view)).perform(setText(R.string.test_text_medium)); 89 assertEquals("Medium text: Max lines must match", TextViewCompat.getMaxLines(mTextView), 90 maxLinesCount); 91 92 onView(withId(R.id.text_view)).perform(setText(R.string.test_text_long)); 93 assertEquals("Long text: Max lines must match", TextViewCompat.getMaxLines(mTextView), 94 maxLinesCount); 95 } 96 97 @Test 98 @SmallTest 99 public void testMinLines() throws Throwable { 100 final int minLinesCount = 3; 101 onView(withId(R.id.text_view)).perform(setMinLines(minLinesCount)); 102 103 assertEquals("Empty view: Min lines must match", TextViewCompat.getMinLines(mTextView), 104 minLinesCount); 105 106 onView(withId(R.id.text_view)).perform(setText(R.string.test_text_short)); 107 assertEquals("Short text: Min lines must match", TextViewCompat.getMinLines(mTextView), 108 minLinesCount); 109 110 onView(withId(R.id.text_view)).perform(setText(R.string.test_text_medium)); 111 assertEquals("Medium text: Min lines must match", TextViewCompat.getMinLines(mTextView), 112 minLinesCount); 113 114 onView(withId(R.id.text_view)).perform(setText(R.string.test_text_long)); 115 assertEquals("Long text: Min lines must match", TextViewCompat.getMinLines(mTextView), 116 minLinesCount); 117 } 118 119 @Test 120 @SmallTest 121 public void testStyle() throws Throwable { 122 onView(withId(R.id.text_view)).perform(setTextAppearance(R.style.TextMediumStyle)); 123 124 final Resources res = mActivityTestRule.getActivity().getResources(); 125 assertTrue("Styled text view: style", 126 mTextView.getTypeface().isItalic() || (mTextView.getPaint().getTextSkewX() < 0)); 127 assertEquals("Styled text view: color", mTextView.getTextColors().getDefaultColor(), 128 res.getColor(R.color.text_color)); 129 assertEquals("Styled text view: size", mTextView.getTextSize(), 130 (float) res.getDimensionPixelSize(R.dimen.text_medium_size), 1.0f); 131 } 132 133 @Test 134 @SmallTest 135 public void testCompoundDrawablesRelative() throws Throwable { 136 final Drawable drawableStart = new ColorDrawable(0xFFFF0000); 137 drawableStart.setBounds(0, 0, 20, 20); 138 final Drawable drawableTop = new ColorDrawable(0xFF00FF00); 139 drawableTop.setBounds(0, 0, 30, 25); 140 final Drawable drawableEnd = new ColorDrawable(0xFF0000FF); 141 drawableEnd.setBounds(0, 0, 25, 20); 142 143 onView(withId(R.id.text_view)).perform(setText(R.string.test_text_medium)); 144 onView(withId(R.id.text_view)).perform(setCompoundDrawablesRelative(drawableStart, 145 drawableTop, drawableEnd, null)); 146 147 final Drawable[] drawablesAbsolute = mTextView.getCompoundDrawables(); 148 149 assertEquals("Compound drawable: left", drawablesAbsolute[0], drawableStart); 150 assertEquals("Compound drawable: left width", 151 drawablesAbsolute[0].getBounds().width(), 20); 152 assertEquals("Compound drawable: left height", 153 drawablesAbsolute[0].getBounds().height(), 20); 154 155 assertEquals("Compound drawable: top", drawablesAbsolute[1], drawableTop); 156 assertEquals("Compound drawable: top width", 157 drawablesAbsolute[1].getBounds().width(), 30); 158 assertEquals("Compound drawable: top height", 159 drawablesAbsolute[1].getBounds().height(), 25); 160 161 assertEquals("Compound drawable: right", drawablesAbsolute[2], drawableEnd); 162 assertEquals("Compound drawable: right width", 163 drawablesAbsolute[2].getBounds().width(), 25); 164 assertEquals("Compound drawable: right height", 165 drawablesAbsolute[2].getBounds().height(), 20); 166 167 assertNull("Compound drawable: bottom", drawablesAbsolute[3]); 168 } 169 170 @Test 171 @SmallTest 172 public void testCompoundDrawablesRelativeRtl() throws Throwable { 173 onView(withId(R.id.text_view)).perform(setLayoutDirection(ViewCompat.LAYOUT_DIRECTION_RTL)); 174 175 final Drawable drawableStart = new ColorDrawable(0xFFFF0000); 176 drawableStart.setBounds(0, 0, 20, 20); 177 final Drawable drawableTop = new ColorDrawable(0xFF00FF00); 178 drawableTop.setBounds(0, 0, 30, 25); 179 final Drawable drawableEnd = new ColorDrawable(0xFF0000FF); 180 drawableEnd.setBounds(0, 0, 25, 20); 181 182 onView(withId(R.id.text_view)).perform(setText(R.string.test_text_medium)); 183 onView(withId(R.id.text_view)).perform(setCompoundDrawablesRelative(drawableStart, 184 drawableTop, drawableEnd, null)); 185 186 // Check to see whether our text view is under RTL mode 187 if (ViewCompat.getLayoutDirection(mTextView) != ViewCompat.LAYOUT_DIRECTION_RTL) { 188 // This will happen on v17- devices 189 return; 190 } 191 192 final Drawable[] drawablesAbsolute = mTextView.getCompoundDrawables(); 193 194 // End drawable should be returned as left 195 assertEquals("Compound drawable: left", drawablesAbsolute[0], drawableEnd); 196 assertEquals("Compound drawable: left width", 197 drawablesAbsolute[0].getBounds().width(), 25); 198 assertEquals("Compound drawable: left height", 199 drawablesAbsolute[0].getBounds().height(), 20); 200 201 assertEquals("Compound drawable: top", drawablesAbsolute[1], drawableTop); 202 assertEquals("Compound drawable: left width", 203 drawablesAbsolute[1].getBounds().width(), 30); 204 assertEquals("Compound drawable: left height", 205 drawablesAbsolute[1].getBounds().height(), 25); 206 207 // Start drawable should be returned as right 208 assertEquals("Compound drawable: right", drawablesAbsolute[2], drawableStart); 209 assertEquals("Compound drawable: left width", 210 drawablesAbsolute[2].getBounds().width(), 20); 211 assertEquals("Compound drawable: left height", 212 drawablesAbsolute[2].getBounds().height(), 20); 213 214 assertNull("Compound drawable: bottom", drawablesAbsolute[3]); 215 } 216 217 @Test 218 @SmallTest 219 public void testCompoundDrawablesRelativeWithIntrinsicBounds() throws Throwable { 220 final Drawable drawableStart = new TestDrawable(0xFFFF0000, 30, 20); 221 final Drawable drawableEnd = new TestDrawable(0xFF0000FF, 25, 45); 222 final Drawable drawableBottom = new TestDrawable(0xFF00FF00, 15, 35); 223 224 onView(withId(R.id.text_view)).perform(setText(R.string.test_text_long)); 225 onView(withId(R.id.text_view)).perform(setCompoundDrawablesRelativeWithIntrinsicBounds( 226 drawableStart, null, drawableEnd, drawableBottom)); 227 228 final Drawable[] drawablesAbsolute = mTextView.getCompoundDrawables(); 229 230 assertEquals("Compound drawable: left", drawablesAbsolute[0], drawableStart); 231 assertEquals("Compound drawable: left width", 232 drawablesAbsolute[0].getBounds().width(), 30); 233 assertEquals("Compound drawable: left height", 234 drawablesAbsolute[0].getBounds().height(), 20); 235 236 assertNull("Compound drawable: top", drawablesAbsolute[1]); 237 238 assertEquals("Compound drawable: right", drawablesAbsolute[2], drawableEnd); 239 assertEquals("Compound drawable: right width", 240 drawablesAbsolute[2].getBounds().width(), 25); 241 assertEquals("Compound drawable: right height", 242 drawablesAbsolute[2].getBounds().height(), 45); 243 244 assertEquals("Compound drawable: bottom", drawablesAbsolute[3], drawableBottom); 245 assertEquals("Compound drawable: bottom width", 246 drawablesAbsolute[3].getBounds().width(), 15); 247 assertEquals("Compound drawable: bottom height", 248 drawablesAbsolute[3].getBounds().height(), 35); 249 } 250 251 @Test 252 @SmallTest 253 public void testCompoundDrawablesRelativeWithIntrinsicBoundsRtl() throws Throwable { 254 onView(withId(R.id.text_view)).perform(setLayoutDirection(ViewCompat.LAYOUT_DIRECTION_RTL)); 255 256 final Drawable drawableStart = new TestDrawable(0xFFFF0000, 30, 20); 257 final Drawable drawableEnd = new TestDrawable(0xFF0000FF, 25, 45); 258 final Drawable drawableBottom = new TestDrawable(0xFF00FF00, 15, 35); 259 260 onView(withId(R.id.text_view)).perform(setText(R.string.test_text_long)); 261 onView(withId(R.id.text_view)).perform(setCompoundDrawablesRelativeWithIntrinsicBounds( 262 drawableStart, null, drawableEnd, drawableBottom)); 263 264 // Check to see whether our text view is under RTL mode 265 if (ViewCompat.getLayoutDirection(mTextView) != ViewCompat.LAYOUT_DIRECTION_RTL) { 266 // This will happen on v17- devices 267 return; 268 } 269 270 final Drawable[] drawablesAbsolute = mTextView.getCompoundDrawables(); 271 272 // End drawable should be returned as left 273 assertEquals("Compound drawable: left", drawablesAbsolute[0], drawableEnd); 274 assertEquals("Compound drawable: left width", 275 drawablesAbsolute[0].getBounds().width(), 25); 276 assertEquals("Compound drawable: left height", 277 drawablesAbsolute[0].getBounds().height(), 45); 278 279 assertNull("Compound drawable: top", drawablesAbsolute[1]); 280 281 // Start drawable should be returned as right 282 assertEquals("Compound drawable: right", drawablesAbsolute[2], drawableStart); 283 assertEquals("Compound drawable: right width", 284 drawablesAbsolute[2].getBounds().width(), 30); 285 assertEquals("Compound drawable: right height", 286 drawablesAbsolute[2].getBounds().height(), 20); 287 288 assertEquals("Compound drawable: bottom", drawablesAbsolute[3], drawableBottom); 289 assertEquals("Compound drawable: bottom width", 290 drawablesAbsolute[3].getBounds().width(), 15); 291 assertEquals("Compound drawable: bottom height", 292 drawablesAbsolute[3].getBounds().height(), 35); 293 } 294 295 @Test 296 @MediumTest 297 public void testCompoundDrawablesRelativeWithIntrinsicBoundsById() throws Throwable { 298 onView(withId(R.id.text_view)).perform(setText(R.string.test_text_long)); 299 onView(withId(R.id.text_view)).perform(setCompoundDrawablesRelativeWithIntrinsicBounds( 300 R.drawable.test_drawable_red, 0, 301 R.drawable.test_drawable_green, R.drawable.test_drawable_blue)); 302 303 final Drawable[] drawablesAbsolute = mTextView.getCompoundDrawables(); 304 final Resources res = mActivityTestRule.getActivity().getResources(); 305 306 // The entire left drawable should be the specific red color 307 TestUtils.assertAllPixelsOfColor("Compound drawable: left color", 308 drawablesAbsolute[0], res.getColor(R.color.test_red)); 309 assertEquals("Compound drawable: left width", 310 drawablesAbsolute[0].getBounds().width(), 311 res.getDimensionPixelSize(R.dimen.drawable_small_size)); 312 assertEquals("Compound drawable: left height", 313 drawablesAbsolute[0].getBounds().height(), 314 res.getDimensionPixelSize(R.dimen.drawable_medium_size)); 315 316 assertNull("Compound drawable: top", drawablesAbsolute[1]); 317 318 // The entire right drawable should be the specific green color 319 TestUtils.assertAllPixelsOfColor("Compound drawable: right color", 320 drawablesAbsolute[2], res.getColor(R.color.test_green)); 321 assertEquals("Compound drawable: right width", 322 drawablesAbsolute[2].getBounds().width(), 323 res.getDimensionPixelSize(R.dimen.drawable_medium_size)); 324 assertEquals("Compound drawable: right height", 325 drawablesAbsolute[2].getBounds().height(), 326 res.getDimensionPixelSize(R.dimen.drawable_large_size)); 327 328 // The entire bottom drawable should be the specific blue color 329 TestUtils.assertAllPixelsOfColor("Compound drawable: bottom color", 330 drawablesAbsolute[3], res.getColor(R.color.test_blue)); 331 assertEquals("Compound drawable: bottom width", 332 drawablesAbsolute[3].getBounds().width(), 333 res.getDimensionPixelSize(R.dimen.drawable_large_size)); 334 assertEquals("Compound drawable: bottom height", 335 drawablesAbsolute[3].getBounds().height(), 336 res.getDimensionPixelSize(R.dimen.drawable_small_size)); 337 } 338 339 @Test 340 @MediumTest 341 public void testCompoundDrawablesRelativeWithIntrinsicBoundsByIdRtl() throws Throwable { 342 onView(withId(R.id.text_view)).perform(setLayoutDirection(ViewCompat.LAYOUT_DIRECTION_RTL)); 343 344 onView(withId(R.id.text_view)).perform(setText(R.string.test_text_long)); 345 onView(withId(R.id.text_view)).perform(setCompoundDrawablesRelativeWithIntrinsicBounds( 346 R.drawable.test_drawable_red, 0, 347 R.drawable.test_drawable_green, R.drawable.test_drawable_blue)); 348 349 // Check to see whether our text view is under RTL mode 350 if (ViewCompat.getLayoutDirection(mTextView) != ViewCompat.LAYOUT_DIRECTION_RTL) { 351 // This will happen on v17- devices 352 return; 353 } 354 355 final Drawable[] drawablesAbsolute = mTextView.getCompoundDrawables(); 356 final Resources res = mActivityTestRule.getActivity().getResources(); 357 358 // The entire left / end drawable should be the specific green color 359 TestUtils.assertAllPixelsOfColor("Compound drawable: left color", 360 drawablesAbsolute[0], res.getColor(R.color.test_green)); 361 assertEquals("Compound drawable: left width", 362 drawablesAbsolute[0].getBounds().width(), 363 res.getDimensionPixelSize(R.dimen.drawable_medium_size)); 364 assertEquals("Compound drawable: left height", 365 drawablesAbsolute[0].getBounds().height(), 366 res.getDimensionPixelSize(R.dimen.drawable_large_size)); 367 368 assertNull("Compound drawable: top", drawablesAbsolute[1]); 369 370 // The entire right drawable should be the specific red color 371 TestUtils.assertAllPixelsOfColor("Compound drawable: right color", 372 drawablesAbsolute[2], res.getColor(R.color.test_red)); 373 assertEquals("Compound drawable: right width", 374 drawablesAbsolute[2].getBounds().width(), 375 res.getDimensionPixelSize(R.dimen.drawable_small_size)); 376 assertEquals("Compound drawable: right height", 377 drawablesAbsolute[2].getBounds().height(), 378 res.getDimensionPixelSize(R.dimen.drawable_medium_size)); 379 380 // The entire bottom drawable should be the specific blue color 381 TestUtils.assertAllPixelsOfColor("Compound drawable: bottom color", 382 drawablesAbsolute[3], res.getColor(R.color.test_blue)); 383 assertEquals("Compound drawable: bottom width", 384 drawablesAbsolute[3].getBounds().width(), 385 res.getDimensionPixelSize(R.dimen.drawable_large_size)); 386 assertEquals("Compound drawable: bottom height", 387 drawablesAbsolute[3].getBounds().height(), 388 res.getDimensionPixelSize(R.dimen.drawable_small_size)); 389 } 390 391 @Test 392 @SmallTest 393 public void testCompoundDrawablesRelativeGetterAndSetter() { 394 final Drawable drawableStart = new TestDrawable(0xFFFF0000, 20, 20); 395 final Drawable drawableTop = new TestDrawable(0xFFFFFF00, 20, 20); 396 final Drawable drawableEnd = new TestDrawable(0xFF0000FF, 20, 20); 397 final Drawable drawableBottom = new TestDrawable(0xFF00FF00, 20, 20); 398 399 onView(withId(R.id.text_view)).perform(setLayoutDirection(ViewCompat.LAYOUT_DIRECTION_RTL)); 400 onView(withId(R.id.text_view)).perform(setCompoundDrawablesRelative(drawableStart, 401 drawableTop, drawableEnd, drawableBottom)); 402 403 // Check to see whether our text view is under RTL mode 404 if (ViewCompat.getLayoutDirection(mTextView) != ViewCompat.LAYOUT_DIRECTION_RTL) { 405 // This will happen on v17- devices 406 return; 407 } 408 409 final Drawable[] drawablesRelative = TextViewCompat.getCompoundDrawablesRelative(mTextView); 410 assertEquals(drawableStart, drawablesRelative[0]); 411 assertEquals(drawableTop, drawablesRelative[1]); 412 assertEquals(drawableEnd, drawablesRelative[2]); 413 assertEquals(drawableBottom, drawablesRelative[3]); 414 } 415} 416