Home | History | Annotate | Download | only in widget
      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 package android.support.v7.widget;
     17 
     18 import static android.support.test.espresso.Espresso.onView;
     19 import static android.support.test.espresso.matcher.ViewMatchers.withId;
     20 import static android.support.v7.testutils.TestUtilsActions.setEnabled;
     21 import static android.support.v7.testutils.TestUtilsActions.setTextAppearance;
     22 
     23 import static org.junit.Assert.assertEquals;
     24 import static org.junit.Assert.assertNotEquals;
     25 import static org.junit.Assert.assertNotNull;
     26 
     27 import android.content.res.ColorStateList;
     28 import android.graphics.Color;
     29 import android.graphics.Typeface;
     30 import android.os.Build;
     31 import android.support.test.annotation.UiThreadTest;
     32 import android.support.test.filters.SmallTest;
     33 import android.support.v4.content.ContextCompat;
     34 import android.support.v4.content.res.ResourcesCompat;
     35 import android.support.v4.widget.TextViewCompat;
     36 import android.support.v7.appcompat.test.R;
     37 import android.widget.TextView;
     38 
     39 import org.junit.Test;
     40 
     41 /**
     42  * In addition to all tinting-related tests done by the base class, this class provides
     43  * tests specific to {@link AppCompatTextView} class.
     44  */
     45 @SmallTest
     46 public class AppCompatTextViewTest
     47         extends AppCompatBaseViewTest<AppCompatTextViewActivity, AppCompatTextView> {
     48 
     49     public AppCompatTextViewTest() {
     50         super(AppCompatTextViewActivity.class);
     51     }
     52 
     53     @Test
     54     public void testAllCaps() {
     55         final String text1 = mResources.getString(R.string.sample_text1);
     56         final String text2 = mResources.getString(R.string.sample_text2);
     57 
     58         final AppCompatTextView textView1 = mContainer.findViewById(R.id.text_view_caps1);
     59         final AppCompatTextView textView2 = mContainer.findViewById(R.id.text_view_caps2);
     60 
     61         // Note that TextView.getText() returns the original text. We are interested in
     62         // the transformed text that is set on the Layout object used to draw the final
     63         // (transformed) content.
     64         assertEquals("Text view starts in all caps on",
     65                 text1.toUpperCase(), textView1.getLayout().getText().toString());
     66         assertEquals("Text view starts in all caps off",
     67                 text2, textView2.getLayout().getText().toString());
     68 
     69         // Toggle all-caps mode on the two text views
     70         onView(withId(R.id.text_view_caps1)).perform(
     71                 setTextAppearance(R.style.TextStyleAllCapsOff));
     72         assertEquals("Text view is now in all caps off",
     73                 text1, textView1.getLayout().getText().toString());
     74 
     75         onView(withId(R.id.text_view_caps2)).perform(
     76                 setTextAppearance(R.style.TextStyleAllCapsOn));
     77         assertEquals("Text view is now in all caps on",
     78                 text2.toUpperCase(), textView2.getLayout().getText().toString());
     79     }
     80 
     81     @Test
     82     public void testAppCompatAllCapsFalseOnButton() {
     83         final String text = mResources.getString(R.string.sample_text2);
     84         final AppCompatTextView textView =
     85                  mContainer.findViewById(R.id.text_view_app_allcaps_false);
     86 
     87         assertEquals("Text view is not in all caps", text, textView.getLayout().getText());
     88     }
     89 
     90     @Test
     91     public void testTextColorSetHex() {
     92         final TextView textView =  mContainer.findViewById(R.id.view_text_color_hex);
     93         assertEquals(Color.RED, textView.getCurrentTextColor());
     94     }
     95 
     96     @Test
     97     public void testTextColorSetColorStateList() {
     98         final TextView textView =  mContainer.findViewById(R.id.view_text_color_csl);
     99 
    100         onView(withId(R.id.view_text_color_csl)).perform(setEnabled(true));
    101         assertEquals(ContextCompat.getColor(textView.getContext(), R.color.ocean_default),
    102                 textView.getCurrentTextColor());
    103 
    104         onView(withId(R.id.view_text_color_csl)).perform(setEnabled(false));
    105         assertEquals(ContextCompat.getColor(textView.getContext(), R.color.ocean_disabled),
    106                 textView.getCurrentTextColor());
    107     }
    108 
    109     @Test
    110     public void testTextColorSetThemeAttrHex() {
    111         final TextView textView =  mContainer.findViewById(R.id.view_text_color_primary);
    112         assertEquals(Color.BLUE, textView.getCurrentTextColor());
    113     }
    114 
    115     @Test
    116     public void testTextColorSetThemeAttrColorStateList() {
    117         final TextView textView =  mContainer.findViewById(R.id.view_text_color_secondary);
    118 
    119         onView(withId(R.id.view_text_color_secondary)).perform(setEnabled(true));
    120         assertEquals(ContextCompat.getColor(textView.getContext(), R.color.sand_default),
    121                 textView.getCurrentTextColor());
    122 
    123         onView(withId(R.id.view_text_color_secondary)).perform(setEnabled(false));
    124         assertEquals(ContextCompat.getColor(textView.getContext(), R.color.sand_disabled),
    125                 textView.getCurrentTextColor());
    126     }
    127 
    128     private void verifyTextLinkColor(TextView textView) {
    129         ColorStateList linkColorStateList = textView.getLinkTextColors();
    130         assertEquals(ContextCompat.getColor(textView.getContext(), R.color.lilac_default),
    131                 linkColorStateList.getColorForState(new int[] { android.R.attr.state_enabled}, 0));
    132         assertEquals(ContextCompat.getColor(textView.getContext(), R.color.lilac_disabled),
    133                 linkColorStateList.getColorForState(new int[] { -android.R.attr.state_enabled}, 0));
    134     }
    135 
    136     @Test
    137     public void testTextLinkColor() {
    138         verifyTextLinkColor((TextView) mContainer.findViewById(R.id.view_text_link_enabled));
    139         verifyTextLinkColor((TextView) mContainer.findViewById(R.id.view_text_link_disabled));
    140     }
    141 
    142     @Test
    143     public void testFontResources_setInStringFamilyName() {
    144         TextView textView =
    145                 mContainer.findViewById(R.id.textview_fontresource_fontfamily_string_resource);
    146         assertNotNull(textView.getTypeface());
    147         // Pre-L, Typeface always resorts to native for a Typeface object, hence giving you a
    148         // different one each call.
    149         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    150             assertEquals(Typeface.SANS_SERIF, textView.getTypeface());
    151         }
    152         textView = mContainer.findViewById(R.id.textview_fontresource_fontfamily_string_direct);
    153         assertNotNull(textView.getTypeface());
    154         if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
    155             assertEquals(Typeface.SANS_SERIF, textView.getTypeface());
    156         }
    157     }
    158 
    159     @Test
    160     public void testFontResources_setInXmlFamilyName() {
    161         TextView textView = mContainer.findViewById(R.id.textview_fontresource_fontfamily);
    162         Typeface expected = ResourcesCompat.getFont(mActivity, R.font.samplefont);
    163 
    164         assertEquals(expected, textView.getTypeface());
    165     }
    166 
    167     @Test
    168     public void testFontResourcesXml_setInXmlFamilyName() {
    169         TextView textView = mContainer.findViewById(R.id.textview_fontxmlresource_fontfamily);
    170         Typeface expected = ResourcesCompat.getFont(mActivity, R.font.samplexmlfont);
    171 
    172         assertEquals(expected, textView.getTypeface());
    173     }
    174 
    175     @Test
    176     public void testFontResourcesXml_setInXmlFamilyNameWithTextStyle() {
    177         TextView textView =
    178                 mContainer.findViewById(R.id.textview_fontxmlresource_fontfamily_textstyle);
    179 
    180         assertNotEquals(Typeface.DEFAULT, textView.getTypeface());
    181     }
    182 
    183     @Test
    184     public void testFontResourcesXml_setInXmlFamilyNameWithTextStyle2() {
    185         TextView textView =
    186                 mContainer.findViewById(R.id.textview_fontxmlresource_fontfamily_textstyle2);
    187 
    188         assertNotEquals(Typeface.DEFAULT, textView.getTypeface());
    189     }
    190 
    191     @Test
    192     public void testFontResources_setInXmlStyle() {
    193         TextView textView = mContainer.findViewById(R.id.textview_fontresource_style);
    194         Typeface expected = ResourcesCompat.getFont(mActivity, R.font.samplefont);
    195 
    196         assertEquals(expected, textView.getTypeface());
    197     }
    198 
    199     @Test
    200     public void testFontResourcesXml_setInXmlStyle() {
    201         TextView textView = mContainer.findViewById(R.id.textview_fontxmlresource_style);
    202         Typeface expected = ResourcesCompat.getFont(mActivity, R.font.samplexmlfont);
    203 
    204         assertEquals(expected, textView.getTypeface());
    205     }
    206 
    207     @Test
    208     public void testFontResources_setInXmlTextAppearance() {
    209         TextView textView = mContainer.findViewById(R.id.textview_fontresource_textAppearance);
    210         Typeface expected = ResourcesCompat.getFont(mActivity, R.font.samplefont);
    211 
    212         assertEquals(expected, textView.getTypeface());
    213     }
    214 
    215     @Test
    216     public void testFontResourcesXml_setInXmlTextAppearance() {
    217         TextView textView = mContainer.findViewById(R.id.textview_fontxmlresource_textAppearance);
    218         Typeface expected = ResourcesCompat.getFont(mActivity, R.font.samplexmlfont);
    219 
    220         assertEquals(expected, textView.getTypeface());
    221     }
    222 
    223     @Test
    224     public void testTextStyle_setTextStyleInStyle() {
    225         // TextView has a TextAppearance by default, but the textStyle can be overriden in style.
    226         TextView textView = mContainer.findViewById(R.id.textview_textStyleOverride);
    227 
    228         assertEquals(Typeface.ITALIC, textView.getTypeface().getStyle());
    229     }
    230 
    231     @Test
    232     public void testTextStyle_setTextStyleDirectly() {
    233         TextView textView = mContainer.findViewById(R.id.textview_textStyleDirect);
    234 
    235         assertEquals(Typeface.ITALIC, textView.getTypeface().getStyle());
    236     }
    237 
    238     @Test
    239     @UiThreadTest
    240     public void testFontResources_setTextAppearance() {
    241         TextView textView = mContainer.findViewById(R.id.textview_simple);
    242 
    243         TextViewCompat.setTextAppearance(textView, R.style.TextView_FontResourceWithStyle);
    244 
    245         assertNotEquals(Typeface.DEFAULT, textView.getTypeface());
    246     }
    247 }
    248