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