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 androidx.appcompat.widget;
     17 
     18 import static android.support.test.espresso.Espresso.onView;
     19 import static android.support.test.espresso.matcher.ViewMatchers.withId;
     20 
     21 import static androidx.appcompat.testutils.TestUtilsActions.setTextAppearance;
     22 
     23 import static org.junit.Assert.assertEquals;
     24 
     25 import android.graphics.Typeface;
     26 import android.support.test.filters.SmallTest;
     27 
     28 import androidx.appcompat.test.R;
     29 import androidx.core.content.res.ResourcesCompat;
     30 
     31 import org.junit.Test;
     32 
     33 /**
     34  * In addition to all tinting-related tests done by the base class, this class provides
     35  * tests specific to {@link AppCompatButton} class.
     36  */
     37 @SmallTest
     38 public class AppCompatButtonTest
     39         extends AppCompatBaseViewTest<AppCompatButtonActivity, AppCompatButton> {
     40     public AppCompatButtonTest() {
     41         super(AppCompatButtonActivity.class);
     42     }
     43 
     44     @Override
     45     protected boolean hasBackgroundByDefault() {
     46         // Button has default background set on it
     47         return true;
     48     }
     49 
     50     @Test
     51     public void testAllCaps() {
     52         final String text1 = mResources.getString(R.string.sample_text1);
     53         final String text2 = mResources.getString(R.string.sample_text2);
     54 
     55         final AppCompatButton button1 =
     56                 (AppCompatButton) mContainer.findViewById(R.id.button_caps1);
     57         final AppCompatButton button2 =
     58                 (AppCompatButton) mContainer.findViewById(R.id.button_caps2);
     59 
     60         // Note that Button.getText() returns the original text. We are interested in
     61         // the transformed text that is set on the Layout object used to draw the final
     62         // (transformed) content.
     63         assertEquals("Button starts in all caps on", text1.toUpperCase(),
     64                 button1.getLayout().getText().toString());
     65         assertEquals("Button starts in all caps off", text2,
     66                 button2.getLayout().getText().toString());
     67 
     68         // Toggle all-caps mode on the two buttons
     69         onView(withId(R.id.button_caps1)).perform(
     70                 setTextAppearance(R.style.TextStyleAllCapsOff));
     71         assertEquals("Button is now in all caps off", text1,
     72                 button1.getLayout().getText().toString());
     73 
     74         onView(withId(R.id.button_caps2)).perform(
     75                 setTextAppearance(R.style.TextStyleAllCapsOn));
     76         assertEquals("Button is now in all caps on", text2.toUpperCase(),
     77                 button2.getLayout().getText().toString());
     78     }
     79 
     80     @Test
     81     public void testAppCompatAllCapsFalseOnButton() {
     82         final String text = mResources.getString(R.string.sample_text2);
     83         final AppCompatButton button =
     84                 (AppCompatButton) mContainer.findViewById(R.id.button_app_allcaps_false);
     85 
     86         assertEquals("Button is not in all caps", text, button.getLayout().getText());
     87     }
     88 
     89     @Test
     90     public void testBackgroundTintListOnColoredButton() {
     91         testUntintedBackgroundTintingViewCompatAcrossStateChange(R.id.button_colored_untinted);
     92     }
     93 
     94     @Test
     95     public void testBackgroundTintListOnButton() {
     96         testUntintedBackgroundTintingViewCompatAcrossStateChange(R.id.button_untinted);
     97     }
     98 
     99     @Test
    100     public void testFontResources() {
    101         AppCompatButton button = mContainer.findViewById(R.id.button_fontresource);
    102         Typeface expected = ResourcesCompat.getFont(mActivity, R.font.samplefont);
    103 
    104         assertEquals(expected, button.getTypeface());
    105     }
    106 }
    107