Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2016 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 package android.widget.cts;
     18 
     19 import static android.view.Gravity.CENTER;
     20 import static android.view.Gravity.LEFT;
     21 import static android.view.Gravity.NO_GRAVITY;
     22 import static android.view.Gravity.RIGHT;
     23 import static android.view.View.TEXT_ALIGNMENT_INHERIT;
     24 import static android.widget.TextView.TEXT_ALIGNMENT_TEXT_END;
     25 import static android.widget.TextView.TEXT_ALIGNMENT_TEXT_START;
     26 import static android.widget.TextView.TEXT_ALIGNMENT_VIEW_END;
     27 import static android.widget.TextView.TEXT_ALIGNMENT_VIEW_START;
     28 
     29 import static org.junit.Assert.assertEquals;
     30 
     31 import android.app.Activity;
     32 import android.app.Instrumentation;
     33 import android.support.test.InstrumentationRegistry;
     34 import android.support.test.filters.MediumTest;
     35 import android.support.test.rule.ActivityTestRule;
     36 import android.support.test.runner.AndroidJUnit4;
     37 import android.util.TypedValue;
     38 import android.view.ViewGroup;
     39 import android.widget.FrameLayout;
     40 
     41 import com.android.compatibility.common.util.PollingCheck;
     42 
     43 import org.junit.Before;
     44 import org.junit.Rule;
     45 import org.junit.Test;
     46 import org.junit.runner.RunWith;
     47 
     48 @MediumTest
     49 @RunWith(AndroidJUnit4.class)
     50 public class TextViewFadingEdgeTest {
     51     private Activity mActivity;
     52 
     53     @Rule
     54     public ActivityTestRule<EmptyCtsActivity> mActivityRule =
     55             new ActivityTestRule<>(EmptyCtsActivity.class);
     56 
     57     public static final float DELTA = 0.01f;
     58     private static final String LONG_RTL_STRING = "     ";
     59     private static final String LONG_LTR_STRING = "start start1 middle middle1 end end1";
     60     private static final String SHORT_RTL_STRING = "";
     61     private static final String SHORT_LTR_STRING = "s";
     62     public static final int ANY_PADDING = 20;
     63     public static final int ANY_FADE_LENGTH = 20;
     64     public static final int TEXT_SIZE = 20;
     65     public static final int WIDTH = 100;
     66 
     67     private static final TestCase[] TEST_DATA = {
     68             // no fade - fading disabled
     69             new TestCase("Should not fade when text:empty, fading:disabled",
     70                     null, false, NO_GRAVITY, TEXT_ALIGNMENT_INHERIT, 0f, 0f),
     71             new TestCase("Should not fade when text:short, dir:LTR, fading:disabled",
     72                     SHORT_LTR_STRING, false, NO_GRAVITY, TEXT_ALIGNMENT_INHERIT, 0f, 0f),
     73             new TestCase("Should not fade when text:short, dir:RTL, fading:disabled",
     74                     SHORT_RTL_STRING, false, NO_GRAVITY, TEXT_ALIGNMENT_INHERIT, 0f, 0f),
     75             new TestCase("Should not fade when text:long , dir:LTR, fading:disabled",
     76                     LONG_LTR_STRING, false, NO_GRAVITY, TEXT_ALIGNMENT_INHERIT, 0f, 0f),
     77             new TestCase("Should not fade when text:long , dir:RTL, fading:disabled",
     78                     LONG_RTL_STRING, false, NO_GRAVITY, TEXT_ALIGNMENT_INHERIT, 0f, 0f),
     79             new TestCase("Should not fade when text:long, dir:LTR, scroll:middle",
     80                     LONG_LTR_STRING, false, NO_GRAVITY, TEXT_ALIGNMENT_INHERIT, true, 0f, 0f),
     81             new TestCase("Should not fade when text:long, dir:RTL, scroll:middle",
     82                     LONG_RTL_STRING, false, NO_GRAVITY, TEXT_ALIGNMENT_INHERIT, true, 0f, 0f),
     83 
     84             // no fade - text is shorter than view width
     85             new TestCase("Should not fade when text:empty",
     86                     null, true, NO_GRAVITY, TEXT_ALIGNMENT_INHERIT, 0f, 0f),
     87             new TestCase("Should not fade when text:short, dir:LTR",
     88                     SHORT_LTR_STRING, true, NO_GRAVITY, TEXT_ALIGNMENT_INHERIT, 0f, 0f),
     89             new TestCase("Should not fade when text:short, dir:LTR, gravity:center",
     90                     SHORT_LTR_STRING, true, CENTER, TEXT_ALIGNMENT_INHERIT, 0f, 0f),
     91             new TestCase("Should not fade when text:short, dir:RTL",
     92                     SHORT_RTL_STRING, true, NO_GRAVITY, TEXT_ALIGNMENT_INHERIT, 0f, 0f),
     93             new TestCase("Should not fade when text:short, dir:RTL, gravity:center",
     94                     SHORT_RTL_STRING, true, CENTER, TEXT_ALIGNMENT_INHERIT, 0f, 0f),
     95 
     96             // left fade - LTR
     97             new TestCase("Should fade left when text:long, dir:LTR, gravity:right",
     98                     LONG_LTR_STRING, true, RIGHT, TEXT_ALIGNMENT_INHERIT, 1f, 0f),
     99             new TestCase("Should fade left when text:long, dir:LTR, textAlignment:textEnd",
    100                     LONG_LTR_STRING, true, NO_GRAVITY, TEXT_ALIGNMENT_TEXT_END, 1f, 0f),
    101             new TestCase("Should fade left when text:long, dir:LTR, textAlignment:viewEnd",
    102                     LONG_LTR_STRING, true, NO_GRAVITY, TEXT_ALIGNMENT_VIEW_END, 1f, 0f),
    103 
    104             // left fade - RTL
    105             new TestCase("Should fade left when text:long, dir:RTL",
    106                     LONG_RTL_STRING, true, NO_GRAVITY, TEXT_ALIGNMENT_INHERIT, 1f, 0f),
    107             new TestCase("Should fade left when text:long, dir:RTL, gravity:right",
    108                     LONG_RTL_STRING, true, RIGHT, TEXT_ALIGNMENT_INHERIT, 1f, 0f),
    109             new TestCase("Should fade left when text:long, dir:RTL, textAlignment:textStart",
    110                     LONG_RTL_STRING, true, NO_GRAVITY, TEXT_ALIGNMENT_TEXT_START, 1f, 0f),
    111             new TestCase("Should fade left when text:long, dir:RTL, textAlignment:viewEnd",
    112                     LONG_RTL_STRING, true, NO_GRAVITY, TEXT_ALIGNMENT_VIEW_END, 1f, 0f),
    113 
    114             // right fade - LTR
    115             new TestCase("Should fade right when text:long, dir:LTR",
    116                     LONG_LTR_STRING, true, NO_GRAVITY, TEXT_ALIGNMENT_INHERIT, 0f, 1f),
    117             new TestCase("Should fade right when text:long, dir:LTR, textAlignment:textStart",
    118                     LONG_LTR_STRING, true, NO_GRAVITY, TEXT_ALIGNMENT_TEXT_START, 0f, 1f),
    119             new TestCase("Should fade right when text:long, dir:LTR, gravity:left",
    120                     LONG_LTR_STRING, true, LEFT, TEXT_ALIGNMENT_INHERIT, 0f, 1f),
    121             new TestCase("Should fade right when text:long, dir:LTR, textAlignment:viewStart",
    122                     LONG_LTR_STRING, true, NO_GRAVITY, TEXT_ALIGNMENT_VIEW_START, 0f, 1f),
    123 
    124             // right fade - RTL
    125             new TestCase("Should fade right when text:long, dir:RTL, gravity:left",
    126                     LONG_RTL_STRING, true, LEFT, TEXT_ALIGNMENT_INHERIT, 0f, 1f),
    127             new TestCase("Should fade right when text:long, dir:RTL, textAlignment:viewStart",
    128                     LONG_RTL_STRING, true, NO_GRAVITY, TEXT_ALIGNMENT_VIEW_START, 0f, 1f),
    129             new TestCase("Should fade right when text:long, dir:RTL, textAlignment:textEnd",
    130                     LONG_RTL_STRING, true, NO_GRAVITY, TEXT_ALIGNMENT_TEXT_END, 0f, 1f),
    131 
    132             // left and right fade
    133             new TestCase("Should fade both when text:long, dir:LTR, scroll:middle",
    134                     LONG_LTR_STRING, true, NO_GRAVITY, TEXT_ALIGNMENT_INHERIT, true, 1f, 1f),
    135             new TestCase("Should fade both when text:long, dir:RTL, scroll:middle",
    136                     LONG_RTL_STRING, true, NO_GRAVITY, TEXT_ALIGNMENT_INHERIT, true, 1f, 1f)
    137     };
    138 
    139     @Before
    140     public void setup() {
    141         mActivity = mActivityRule.getActivity();
    142         PollingCheck.waitFor(mActivity::hasWindowFocus);
    143     }
    144 
    145     @Test
    146     public void testFadingEdge() throws Throwable {
    147         for (TestCase data : TEST_DATA) {
    148             MockTextView textView = createTextView(data.text, data.horizontalFadingEnabled,
    149                     data.gravity, data.textAlignment, data.scrollToMiddle);
    150 
    151             assertEquals(data.errorMsg,
    152                     data.expectationLeft, textView.getLeftFadingEdgeStrength(), DELTA);
    153             assertEquals(data.errorMsg,
    154                     data.expectationRight, textView.getRightFadingEdgeStrength(), DELTA);
    155         }
    156     }
    157 
    158     private final MockTextView createTextView(String text, boolean horizontalFadingEnabled,
    159             int gravity, int textAlignment, boolean scrollToMiddle) throws Throwable {
    160         final MockTextView textView = new MockTextView(mActivity);
    161         textView.setSingleLine(true);
    162         textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, TEXT_SIZE);
    163         textView.setPadding(ANY_PADDING, ANY_PADDING, ANY_PADDING, ANY_PADDING);
    164         textView.setFadingEdgeLength(ANY_FADE_LENGTH);
    165         textView.setLayoutParams(new ViewGroup.LayoutParams(WIDTH,
    166                 ViewGroup.LayoutParams.WRAP_CONTENT));
    167         textView.setHorizontalFadingEdgeEnabled(horizontalFadingEnabled);
    168         textView.setText(text);
    169         textView.setGravity(gravity);
    170         textView.setTextAlignment(textAlignment);
    171 
    172         final FrameLayout layout = new FrameLayout(mActivity);
    173         ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(
    174                 ViewGroup.LayoutParams.MATCH_PARENT,
    175                 ViewGroup.LayoutParams.MATCH_PARENT);
    176         layout.setLayoutParams(layoutParams);
    177         layout.addView(textView);
    178 
    179         final Instrumentation instrumentation = InstrumentationRegistry.getInstrumentation();
    180         mActivityRule.runOnUiThread(() -> mActivity.setContentView(layout));
    181         if (scrollToMiddle) {
    182             mActivityRule.runOnUiThread(() -> {
    183                 float lineMid = (textView.getLayout().getLineLeft(0) +
    184                         textView.getLayout().getLineRight(0)) / 2;
    185                 int scrollPosition = (int) lineMid;
    186                 textView.setScrollX(scrollPosition);
    187             });
    188         }
    189         instrumentation.waitForIdleSync();
    190         return textView;
    191     }
    192 
    193     private static class TestCase {
    194         final String errorMsg;
    195         final String text;
    196         final boolean horizontalFadingEnabled;
    197         final int gravity;
    198         final int textAlignment;
    199         final float expectationLeft;
    200         final float expectationRight;
    201         final boolean scrollToMiddle;
    202 
    203         TestCase(String errorMsg, String text, boolean horizontalFadingEnabled, int gravity,
    204                  int textAlignment, boolean scrollToMiddle, float expectationLeft,
    205                  float expectationRight) {
    206             this.errorMsg = errorMsg;
    207             this.text = text;
    208             this.horizontalFadingEnabled = horizontalFadingEnabled;
    209             this.gravity = gravity;
    210             this.textAlignment = textAlignment;
    211             this.expectationLeft = expectationLeft;
    212             this.expectationRight = expectationRight;
    213             this.scrollToMiddle = scrollToMiddle;
    214         }
    215 
    216         TestCase(String errorMsg, String text, boolean horizontalFadingEnabled, int gravity,
    217                  int textAlignment, float expectationLeft, float expectationRight) {
    218             this(errorMsg, text, horizontalFadingEnabled, gravity, textAlignment, false,
    219                     expectationLeft, expectationRight);
    220         }
    221     }
    222 }
    223