/*
* Copyright (C) 2016 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.support.v7.widget;
import static android.support.test.espresso.Espresso.onView;
import static android.support.test.espresso.matcher.ViewMatchers.withId;
import static android.support.v7.testutils.TestUtilsActions.setEnabled;
import static android.support.v7.testutils.TestUtilsActions.setTextAppearance;
import static org.junit.Assert.assertEquals;
import android.graphics.Color;
import android.support.test.filters.SmallTest;
import android.support.v4.content.ContextCompat;
import android.support.v7.appcompat.test.R;
import android.widget.TextView;
import org.junit.Test;
/**
* In addition to all tinting-related tests done by the base class, this class provides
* tests specific to {@link AppCompatTextView} class.
*/
@SmallTest
public class AppCompatTextViewTest
extends AppCompatBaseViewTest<AppCompatTextViewActivity, AppCompatTextView> {
public AppCompatTextViewTest() {
super(AppCompatTextViewActivity.class);
}
@Test
public void testAllCaps() {
final String text1 = mResources.getString(R.string.sample_text1);
final String text2 = mResources.getString(R.string.sample_text2);
final AppCompatTextView textView1 =
(AppCompatTextView) mContainer.findViewById(R.id.text_view_caps1);
final AppCompatTextView textView2 =
(AppCompatTextView) mContainer.findViewById(R.id.text_view_caps2);
// Note that TextView.getText() returns the original text. We are interested in
// the transformed text that is set on the Layout object used to draw the final
// (transformed) content.
assertEquals("Text view starts in all caps on", text1.toUpperCase(),
textView1.getLayout().getText());
assertEquals("Text view starts in all caps off", text2,
textView2.getLayout().getText());
// Toggle all-caps mode on the two text views
onView(withId(R.id.text_view_caps1)).perform(
setTextAppearance(R.style.TextStyleAllCapsOff));
assertEquals("Text view is now in all caps off", text1,
textView1.getLayout().getText());
onView(withId(R.id.text_view_caps2)).perform(
setTextAppearance(R.style.TextStyleAllCapsOn));
assertEquals("Text view is now in all caps on", text2.toUpperCase(),
textView2.getLayout().getText());
}
@Test
public void testAppCompatAllCapsFalseOnButton() {
final String text = mResources.getString(R.string.sample_text2);
final AppCompatTextView textView =
(AppCompatTextView) mContainer.findViewById(R.id.text_view_app_allcaps_false);
assertEquals("Text view is not in all caps", text, textView.getLayout().getText());
}
@Test
public void testTextColorSetHex() {
final TextView textView = (TextView) mContainer.findViewById(R.id.view_text_color_hex);
assertEquals(Color.RED, textView.getCurrentTextColor());
}
@Test
public void testTextColorSetColorStateList() {
final TextView textView = (TextView) mContainer.findViewById(R.id.view_text_color_csl);
onView(withId(R.id.view_text_color_csl)).perform(setEnabled(true));
assertEquals(ContextCompat.getColor(textView.getContext(), R.color.ocean_default),
textView.getCurrentTextColor());
onView(withId(R.id.view_text_color_csl)).perform(setEnabled(false));
assertEquals(ContextCompat.getColor(textView.getContext(), R.color.ocean_disabled),
textView.getCurrentTextColor());
}
@Test
public void testTextColorSetThemeAttrHex() {
final TextView textView = (TextView) mContainer.findViewById(R.id.view_text_color_primary);
assertEquals(Color.BLUE, textView.getCurrentTextColor());
}
@Test
public void testTextColorSetThemeAttrColorStateList() {
final TextView textView = (TextView)
mContainer.findViewById(R.id.view_text_color_secondary);
onView(withId(R.id.view_text_color_secondary)).perform(setEnabled(true));
assertEquals(ContextCompat.getColor(textView.getContext(), R.color.sand_default),
textView.getCurrentTextColor());
onView(withId(R.id.view_text_color_secondary)).perform(setEnabled(false));
assertEquals(ContextCompat.getColor(textView.getContext(), R.color.sand_disabled),
textView.getCurrentTextColor());
}
}