Merge "MediaRouter: Check sGlobal before call RouteInfo.isDefault" into oc-support-26.0-dev
diff --git a/v7/appcompat/src/android/support/v7/widget/AppCompatButton.java b/v7/appcompat/src/android/support/v7/widget/AppCompatButton.java
index 04698e2..2156dce 100644
--- a/v7/appcompat/src/android/support/v7/widget/AppCompatButton.java
+++ b/v7/appcompat/src/android/support/v7/widget/AppCompatButton.java
@@ -22,16 +22,21 @@
import android.content.res.ColorStateList;
import android.graphics.PorterDuff;
import android.graphics.drawable.Drawable;
+import android.os.Build;
import android.support.annotation.DrawableRes;
+import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.annotation.RestrictTo;
import android.support.v4.view.TintableBackgroundView;
+import android.support.v4.widget.AutoSizeableTextView;
+import android.support.v4.widget.TextViewCompat;
import android.support.v7.appcompat.R;
import android.util.AttributeSet;
import android.view.accessibility.AccessibilityEvent;
import android.view.accessibility.AccessibilityNodeInfo;
import android.widget.Button;
+import android.widget.TextView;
/**
* A {@link Button} which supports compatible features on older versions of the platform,
@@ -48,7 +53,8 @@
* <a href="{@docRoot}topic/libraries/support-library/packages.html#v7-appcompat">appcompat</a>.
* You should only need to manually use this class when writing custom views.</p>
*/
-public class AppCompatButton extends Button implements TintableBackgroundView {
+public class AppCompatButton extends Button implements TintableBackgroundView,
+ AutoSizeableTextView {
private final AppCompatBackgroundHelper mBackgroundTintHelper;
private final AppCompatTextHelper mTextHelper;
@@ -176,6 +182,169 @@
info.setClassName(Button.class.getName());
}
+ @Override
+ protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
+ super.onLayout(changed, left, top, right, bottom);
+ if (mTextHelper != null) {
+ mTextHelper.onLayout(changed, left, top, right, bottom);
+ }
+ }
+
+ @Override
+ public void setTextSize(int unit, float size) {
+ if (Build.VERSION.SDK_INT >= 26) {
+ super.setTextSize(unit, size);
+ } else {
+ if (mTextHelper != null) {
+ mTextHelper.setTextSize(unit, size);
+ }
+ }
+ }
+
+ @Override
+ protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) {
+ super.onTextChanged(text, start, lengthBefore, lengthAfter);
+ if (mTextHelper != null && Build.VERSION.SDK_INT < 26 && mTextHelper.isAutoSizeEnabled()) {
+ mTextHelper.autoSizeText();
+ }
+ }
+
+ /**
+ * @hide
+ */
+ @RestrictTo(LIBRARY_GROUP)
+ @Override
+ public void setAutoSizeTextTypeWithDefaults(
+ @TextViewCompat.AutoSizeTextType int autoSizeTextType) {
+ if (Build.VERSION.SDK_INT >= 26) {
+ super.setAutoSizeTextTypeWithDefaults(autoSizeTextType);
+ } else {
+ if (mTextHelper != null) {
+ mTextHelper.setAutoSizeTextTypeWithDefaults(autoSizeTextType);
+ }
+ }
+ }
+
+ /**
+ * @hide
+ */
+ @RestrictTo(LIBRARY_GROUP)
+ @Override
+ public void setAutoSizeTextTypeUniformWithConfiguration(
+ int autoSizeMinTextSize,
+ int autoSizeMaxTextSize,
+ int autoSizeStepGranularity,
+ int unit) throws IllegalArgumentException {
+ if (Build.VERSION.SDK_INT >= 26) {
+ super.setAutoSizeTextTypeUniformWithConfiguration(
+ autoSizeMinTextSize, autoSizeMaxTextSize, autoSizeStepGranularity, unit);
+ } else {
+ if (mTextHelper != null) {
+ mTextHelper.setAutoSizeTextTypeUniformWithConfiguration(
+ autoSizeMinTextSize, autoSizeMaxTextSize, autoSizeStepGranularity, unit);
+ }
+ }
+ }
+
+ /**
+ * @hide
+ */
+ @RestrictTo(LIBRARY_GROUP)
+ @Override
+ public void setAutoSizeTextTypeUniformWithPresetSizes(@NonNull int[] presetSizes, int unit)
+ throws IllegalArgumentException {
+ if (Build.VERSION.SDK_INT >= 26) {
+ super.setAutoSizeTextTypeUniformWithPresetSizes(presetSizes, unit);
+ } else {
+ if (mTextHelper != null) {
+ mTextHelper.setAutoSizeTextTypeUniformWithPresetSizes(presetSizes, unit);
+ }
+ }
+ }
+
+ /**
+ * @hide
+ */
+ @RestrictTo(LIBRARY_GROUP)
+ @Override
+ @TextViewCompat.AutoSizeTextType
+ public int getAutoSizeTextType() {
+ if (Build.VERSION.SDK_INT >= 26) {
+ return super.getAutoSizeTextType() == TextView.AUTO_SIZE_TEXT_TYPE_UNIFORM
+ ? TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM
+ : TextViewCompat.AUTO_SIZE_TEXT_TYPE_NONE;
+ } else {
+ if (mTextHelper != null) {
+ return mTextHelper.getAutoSizeTextType();
+ }
+ }
+ return TextViewCompat.AUTO_SIZE_TEXT_TYPE_NONE;
+ }
+
+ /**
+ * @hide
+ */
+ @RestrictTo(LIBRARY_GROUP)
+ @Override
+ public int getAutoSizeStepGranularity() {
+ if (Build.VERSION.SDK_INT >= 26) {
+ return super.getAutoSizeStepGranularity();
+ } else {
+ if (mTextHelper != null) {
+ return mTextHelper.getAutoSizeStepGranularity();
+ }
+ }
+ return -1;
+ }
+
+ /**
+ * @hide
+ */
+ @RestrictTo(LIBRARY_GROUP)
+ @Override
+ public int getAutoSizeMinTextSize() {
+ if (Build.VERSION.SDK_INT >= 26) {
+ return super.getAutoSizeMinTextSize();
+ } else {
+ if (mTextHelper != null) {
+ return mTextHelper.getAutoSizeMinTextSize();
+ }
+ }
+ return -1;
+ }
+
+ /**
+ * @hide
+ */
+ @RestrictTo(LIBRARY_GROUP)
+ @Override
+ public int getAutoSizeMaxTextSize() {
+ if (Build.VERSION.SDK_INT >= 26) {
+ return super.getAutoSizeMaxTextSize();
+ } else {
+ if (mTextHelper != null) {
+ return mTextHelper.getAutoSizeMaxTextSize();
+ }
+ }
+ return -1;
+ }
+
+ /**
+ * @hide
+ */
+ @RestrictTo(LIBRARY_GROUP)
+ @Override
+ public int[] getAutoSizeTextAvailableSizes() {
+ if (Build.VERSION.SDK_INT >= 26) {
+ return super.getAutoSizeTextAvailableSizes();
+ } else {
+ if (mTextHelper != null) {
+ return mTextHelper.getAutoSizeTextAvailableSizes();
+ }
+ }
+ return new int[0];
+ }
+
/**
* Sets the properties of this field to transform input to ALL CAPS
* display. This may use a "small caps" formatting if available.
diff --git a/v7/appcompat/tests/AndroidManifest.xml b/v7/appcompat/tests/AndroidManifest.xml
index cb58573..abad7d6 100644
--- a/v7/appcompat/tests/AndroidManifest.xml
+++ b/v7/appcompat/tests/AndroidManifest.xml
@@ -79,6 +79,11 @@
android:theme="@style/Theme.AppCompat.Light"/>
<activity
+ android:name="android.support.v7.widget.AppCompatButtonAutoSizeActivity"
+ android:label="@string/app_compat_button_auto_size_activity"
+ android:theme="@style/Theme.AppCompat.Light"/>
+
+ <activity
android:name="android.support.v7.widget.AppCompatImageButtonActivity"
android:label="@string/app_compat_image_button_activity"
android:theme="@style/Theme.AppCompat.Light"/>
diff --git a/v7/appcompat/tests/res/layout/appcompat_button_autosize_activity.xml b/v7/appcompat/tests/res/layout/appcompat_button_autosize_activity.xml
new file mode 100644
index 0000000..407154a
--- /dev/null
+++ b/v7/appcompat/tests/res/layout/appcompat_button_autosize_activity.xml
@@ -0,0 +1,57 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+ ~ Copyright (C) 2017 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.
+ -->
+<LinearLayout
+ xmlns:android="http://schemas.android.com/apk/res/android"
+ xmlns:app="http://schemas.android.com/apk/res-auto"
+ android:id="@+id/container"
+ android:layout_width="match_parent"
+ android:layout_height="wrap_content"
+ android:orientation="vertical">
+
+ <android.support.v7.widget.AppCompatButton
+ android:id="@+id/view_autosize_uniform"
+ android:layout_width="100dp"
+ android:layout_height="200dp"
+ android:text="@string/sample_text1"
+ app:autoSizeTextType="uniform"
+ app:autoSizeMinTextSize="2px"
+ app:autoSizeMaxTextSize="50dp"
+ app:autoSizeStepGranularity="1px" />
+
+ <android.support.v7.widget.AppCompatButton
+ android:id="@+id/view_autosize_uniform_predef_sizes"
+ android:layout_width="100dp"
+ android:layout_height="200dp"
+ android:text="@string/sample_text1"
+ app:autoSizeTextType="uniform"
+ app:autoSizePresetSizes="@array/auto_size_predefined_sizes" />
+
+ <android.support.v7.widget.AppCompatButton
+ android:id="@+id/view_autosize_uniform_predef_sizes_redundant_values"
+ android:layout_width="100dp"
+ android:layout_height="200dp"
+ android:text="@string/sample_text1"
+ app:autoSizeTextType="uniform"
+ app:autoSizePresetSizes="@array/auto_size_predefined_sizes_redundant_values" />
+
+ <android.support.v7.widget.AppCompatButton
+ android:id="@+id/view_text"
+ android:layout_width="100dp"
+ android:layout_height="200dp"
+ android:text="@string/sample_text1" />
+
+</LinearLayout>
\ No newline at end of file
diff --git a/v7/appcompat/tests/res/layout/appcompat_textview_autosize_activity.xml b/v7/appcompat/tests/res/layout/appcompat_textview_autosize_activity.xml
index b9df4cc..f9aafcc 100644
--- a/v7/appcompat/tests/res/layout/appcompat_textview_autosize_activity.xml
+++ b/v7/appcompat/tests/res/layout/appcompat_textview_autosize_activity.xml
@@ -16,13 +16,13 @@
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
- android:id="@+id/layout_textviewtest"
+ android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
- android:orientation="horizontal">
+ android:orientation="vertical">
<android.support.v7.widget.AppCompatTextView
- android:id="@+id/textview_autosize_uniform"
+ android:id="@+id/view_autosize_uniform"
android:layout_width="100dp"
android:layout_height="200dp"
android:text="@string/sample_text1"
@@ -32,7 +32,7 @@
app:autoSizeStepGranularity="1px" />
<android.support.v7.widget.AppCompatTextView
- android:id="@+id/textview_autosize_uniform_predef_sizes"
+ android:id="@+id/view_autosize_uniform_predef_sizes"
android:layout_width="100dp"
android:layout_height="200dp"
android:text="@string/sample_text1"
@@ -40,7 +40,7 @@
app:autoSizePresetSizes="@array/auto_size_predefined_sizes" />
<android.support.v7.widget.AppCompatTextView
- android:id="@+id/textview_autosize_uniform_predef_sizes_redundant_values"
+ android:id="@+id/view_autosize_uniform_predef_sizes_redundant_values"
android:layout_width="100dp"
android:layout_height="200dp"
android:text="@string/sample_text1"
@@ -48,10 +48,10 @@
app:autoSizePresetSizes="@array/auto_size_predefined_sizes_redundant_values" />
<android.support.v7.widget.AppCompatTextView
- android:id="@+id/textview_text"
- android:text="@string/sample_text1"
+ android:id="@+id/view_text"
android:layout_width="100dp"
- android:layout_height="200dp"/>
+ android:layout_height="200dp"
+ android:text="@string/sample_text1" />
<android.support.v7.widget.AppCompatEditText
android:id="@+id/edittext_autosize_uniform"
diff --git a/v7/appcompat/tests/res/values/strings.xml b/v7/appcompat/tests/res/values/strings.xml
index 86386e2..90be8b3 100644
--- a/v7/appcompat/tests/res/values/strings.xml
+++ b/v7/appcompat/tests/res/values/strings.xml
@@ -56,6 +56,7 @@
<string name="app_compat_spinner_activity">AppCompat spinner</string>
<string name="app_compat_text_view_activity">AppCompat text view</string>
<string name="app_compat_text_view_auto_size_activity">AppCompat text view auto-size</string>
+ <string name="app_compat_button_auto_size_activity">AppCompat button auto-size</string>
<string name="sample_text1">Sample text 1</string>
<string name="sample_text2">Sample text 2</string>
<string name="app_compat_image_button_activity">AppCompat image button</string>
diff --git a/v7/appcompat/tests/src/android/support/v7/widget/AppCompatBaseAutoSizeTest.java b/v7/appcompat/tests/src/android/support/v7/widget/AppCompatBaseAutoSizeTest.java
new file mode 100644
index 0000000..cc6f91b
--- /dev/null
+++ b/v7/appcompat/tests/src/android/support/v7/widget/AppCompatBaseAutoSizeTest.java
@@ -0,0 +1,1210 @@
+/*
+ * Copyright (C) 2017 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 org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import android.app.Instrumentation;
+import android.graphics.Typeface;
+import android.graphics.drawable.Drawable;
+import android.os.Build;
+import android.support.annotation.IdRes;
+import android.support.test.InstrumentationRegistry;
+import android.support.test.filters.MediumTest;
+import android.support.test.filters.SdkSuppress;
+import android.support.test.filters.SmallTest;
+import android.support.test.rule.ActivityTestRule;
+import android.support.v4.content.res.ResourcesCompat;
+import android.support.v4.widget.AutoSizeableTextView;
+import android.support.v4.widget.TextViewCompat;
+import android.support.v7.appcompat.test.R;
+import android.support.v7.testutils.BaseTestActivity;
+import android.text.TextUtils;
+import android.util.DisplayMetrics;
+import android.util.TypedValue;
+import android.view.ViewGroup;
+import android.widget.LinearLayout;
+import android.widget.TextView;
+
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+
+/**
+ * Base class for testing auto-size-text enabled views in appcompat-v7 that implement the
+ * <code>AutoSizeableTextView</code> interface. Extensions of this class run all tests
+ * from here and can add test cases specific to the functionality they add to the relevant
+ * base view class.
+ */
+public abstract class AppCompatBaseAutoSizeTest<A extends BaseTestActivity,
+ T extends TextView & AutoSizeableTextView> {
+
+ @Rule
+ public final ActivityTestRule<A> mActivityTestRule;
+
+ protected A mActivity;
+ protected Instrumentation mInstrumentation;
+ protected ViewGroup mContainer;
+
+ public AppCompatBaseAutoSizeTest(Class clazz) {
+ mActivityTestRule = new ActivityTestRule<A>(clazz);
+ }
+
+ @Before
+ public void setup() {
+ mInstrumentation = InstrumentationRegistry.getInstrumentation();
+ mActivity = mActivityTestRule.getActivity();
+ mContainer = mActivity.findViewById(R.id.container);
+ }
+
+ @Test
+ @MediumTest
+ @SdkSuppress(minSdkVersion = 16)
+ // public TextView#getMaxLines only introduced in API 16.
+ public void testAutoSizeCallers_setMaxLines() throws Throwable {
+ final T autoSizeView = prepareAndRetrieveAutoSizeTestData(R.id.view_autosize_uniform,
+ false);
+ // Configure layout params and auto-size both in pixels to dodge flakiness on different
+ // devices.
+ final LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
+ 500, 500);
+ final String text = "one two three four five six seven eight nine ten";
+ mActivity.runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ autoSizeView.setLayoutParams(layoutParams);
+ ((AutoSizeableTextView) autoSizeView).setAutoSizeTextTypeUniformWithConfiguration(
+ 1 /* autoSizeMinTextSize */,
+ 5000 /* autoSizeMaxTextSize */,
+ 1 /* autoSizeStepGranularity */,
+ TypedValue.COMPLEX_UNIT_PX);
+ autoSizeView.setText(text);
+ }
+ });
+ mInstrumentation.waitForIdleSync();
+
+ float initialSize = 0;
+ for (int i = 1; i < 10; i++) {
+ final int maxLines = i;
+ mActivity.runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ autoSizeView.setMaxLines(maxLines);
+ }
+ });
+ mInstrumentation.waitForIdleSync();
+ float expectedSmallerSize = autoSizeView.getTextSize();
+ if (i == 1) {
+ initialSize = expectedSmallerSize;
+ }
+
+ mActivity.runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ autoSizeView.setMaxLines(maxLines + 1);
+ }
+ });
+ mInstrumentation.waitForIdleSync();
+ assertTrue(expectedSmallerSize <= autoSizeView.getTextSize());
+ }
+ assertTrue(initialSize < autoSizeView.getTextSize());
+
+ initialSize = 999999;
+ for (int i = 10; i > 1; i--) {
+ final int maxLines = i;
+ mActivity.runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ autoSizeView.setMaxLines(maxLines);
+ }
+ });
+ mInstrumentation.waitForIdleSync();
+ float expectedLargerSize = autoSizeView.getTextSize();
+ if (i == 10) {
+ initialSize = expectedLargerSize;
+ }
+
+ mActivity.runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ autoSizeView.setMaxLines(maxLines - 1);
+ }
+ });
+ mInstrumentation.waitForIdleSync();
+ assertTrue(expectedLargerSize >= autoSizeView.getTextSize());
+ }
+ assertTrue(initialSize > autoSizeView.getTextSize());
+ }
+
+ @Test
+ @MediumTest
+ public void testAutoSizeUniform_autoSizeCalledWhenTypeChanged() throws Throwable {
+ final T view = mContainer.findViewById(R.id.view_text);
+
+ // Make sure we pick an already inflated non auto-sized text view.
+ assertEquals(TextViewCompat.AUTO_SIZE_TEXT_TYPE_NONE,
+ ((AutoSizeableTextView) view).getAutoSizeTextType());
+ // Set the text size to a very low value in order to prepare for auto-size.
+ final int customTextSize = 3;
+ mActivityTestRule.runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ view.setTextSize(TypedValue.COMPLEX_UNIT_PX, customTextSize);
+ }
+ });
+ mInstrumentation.waitForIdleSync();
+ assertEquals(customTextSize, view.getTextSize(), 0f);
+ mActivityTestRule.runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ ((AutoSizeableTextView) view).setAutoSizeTextTypeWithDefaults(
+ TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM);
+ }
+ });
+ mInstrumentation.waitForIdleSync();
+ // The size of the text should have changed.
+ assertNotEquals(customTextSize, view.getTextSize(), 0f);
+ }
+
+ @Test
+ @MediumTest
+ public void testAutoSizeCallers_setText() throws Throwable {
+ final T autoSizeView = prepareAndRetrieveAutoSizeTestData(R.id.view_autosize_uniform,
+ false);
+
+ // Configure layout params and auto-size both in pixels to dodge flakiness on different
+ // devices.
+ final LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
+ 500, 500);
+ mActivity.runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ autoSizeView.setLayoutParams(layoutParams);
+ ((AutoSizeableTextView) autoSizeView).setAutoSizeTextTypeUniformWithConfiguration(
+ 1, 5000, 1, TypedValue.COMPLEX_UNIT_PX);
+ }
+ });
+ mInstrumentation.waitForIdleSync();
+
+ final String initialText = "13characters ";
+ final StringBuilder textToSet = new StringBuilder().append(initialText);
+ float initialSize = 0;
+
+ // As we add characters the text size shrinks.
+ for (int i = 0; i < 10; i++) {
+ mActivity.runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ autoSizeView.setText(textToSet.toString());
+ }
+ });
+
+ mInstrumentation.waitForIdleSync();
+ float expectedLargerSize = autoSizeView.getTextSize();
+ if (i == 0) {
+ initialSize = expectedLargerSize;
+ }
+
+ textToSet.append(initialText);
+ mActivity.runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ autoSizeView.setText(textToSet.toString());
+ }
+ });
+ mInstrumentation.waitForIdleSync();
+ assertTrue(expectedLargerSize >= autoSizeView.getTextSize());
+ }
+ assertTrue(initialSize > autoSizeView.getTextSize());
+
+ initialSize = 9999999;
+ // As we remove characters the text size expands.
+ for (int i = 9; i >= 0; i--) {
+ mActivity.runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ autoSizeView.setText(textToSet.toString());
+ }
+ });
+ mInstrumentation.waitForIdleSync();
+ float expectedSmallerSize = autoSizeView.getTextSize();
+ if (i == 0) {
+ initialSize = expectedSmallerSize;
+ }
+
+ textToSet.replace((textToSet.length() - initialText.length()),
+ textToSet.length(), "");
+ mActivity.runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ autoSizeView.setText(textToSet.toString());
+ }
+ });
+ mInstrumentation.waitForIdleSync();
+
+ assertTrue(autoSizeView.getTextSize() >= expectedSmallerSize);
+ }
+ assertTrue(autoSizeView.getTextSize() > initialSize);
+ }
+
+ @Test
+ @MediumTest
+ public void testAutoSizeUniform_equivalentConfigurations() throws Throwable {
+ final DisplayMetrics dm = mActivity.getResources().getDisplayMetrics();
+ final int minTextSize = 10;
+ final int maxTextSize = 20;
+ final int granularity = 2;
+ final int unit = TypedValue.COMPLEX_UNIT_SP;
+
+ final T granularityView = getNewAutoSizeViewInstance();
+ ((AutoSizeableTextView) granularityView).setAutoSizeTextTypeUniformWithConfiguration(
+ minTextSize, maxTextSize, granularity, unit);
+
+ final T presetView = getNewAutoSizeViewInstance();
+ ((AutoSizeableTextView) presetView).setAutoSizeTextTypeUniformWithPresetSizes(
+ new int[]{minTextSize, 12, 14, 16, 18, maxTextSize}, unit);
+
+ // The TextViews have been configured differently but the end result should be nearly
+ // identical.
+ final int expectedAutoSizeType = AppCompatTextView.AUTO_SIZE_TEXT_TYPE_UNIFORM;
+ assertEquals(expectedAutoSizeType,
+ ((AutoSizeableTextView) granularityView).getAutoSizeTextType());
+ assertEquals(expectedAutoSizeType,
+ ((AutoSizeableTextView) presetView).getAutoSizeTextType());
+
+ final int expectedMinTextSizeInPx = Math.round(
+ TypedValue.applyDimension(unit, minTextSize, dm));
+ assertEquals(expectedMinTextSizeInPx,
+ ((AutoSizeableTextView) granularityView).getAutoSizeMinTextSize());
+ assertEquals(expectedMinTextSizeInPx,
+ ((AutoSizeableTextView) presetView).getAutoSizeMinTextSize());
+
+ final int expectedMaxTextSizeInPx = Math.round(
+ TypedValue.applyDimension(unit, maxTextSize, dm));
+ assertEquals(expectedMaxTextSizeInPx,
+ ((AutoSizeableTextView) granularityView).getAutoSizeMaxTextSize());
+ assertEquals(expectedMaxTextSizeInPx,
+ ((AutoSizeableTextView) presetView).getAutoSizeMaxTextSize());
+
+ // Configured with granularity.
+ assertEquals(Math.round(TypedValue.applyDimension(unit, granularity, dm)),
+ ((AutoSizeableTextView) granularityView).getAutoSizeStepGranularity());
+ // Configured with preset values, there is no granularity.
+ assertEquals(-1,
+ ((AutoSizeableTextView) presetView).getAutoSizeStepGranularity());
+
+ // Both TextViews generate exactly the same sizes in pixels to choose from when auto-sizing.
+ assertArrayEquals(
+ ((AutoSizeableTextView) granularityView).getAutoSizeTextAvailableSizes(),
+ ((AutoSizeableTextView) presetView).getAutoSizeTextAvailableSizes());
+
+ final String someText = "This is a string";
+ final LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
+ 500, 500);
+ // Configure identically and attach to layout.
+ mActivityTestRule.runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ granularityView.setLayoutParams(layoutParams);
+ presetView.setLayoutParams(layoutParams);
+
+ LinearLayout ll = mActivity.findViewById(R.id.container);
+ ll.removeAllViews();
+ ll.addView(granularityView);
+ ll.addView(presetView);
+
+ granularityView.setText(someText);
+ presetView.setText(someText);
+ }
+ });
+ mInstrumentation.waitForIdleSync();
+
+ assertEquals(granularityView.getTextSize(), presetView.getTextSize(), 0f);
+ }
+
+ @Test
+ @MediumTest
+ public void testAutoSizeCallers_setHeight() throws Throwable {
+ final T autoSizeView = prepareAndRetrieveAutoSizeTestData(
+ R.id.view_autosize_uniform, true);
+ // Do not force exact height only.
+ final LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
+ 200,
+ LinearLayout.LayoutParams.WRAP_CONTENT);
+ mActivityTestRule.runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ autoSizeView.setLayoutParams(layoutParams);
+ }
+ });
+ mInstrumentation.waitForIdleSync();
+ final float initialTextSize = autoSizeView.getTextSize();
+ mActivityTestRule.runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ autoSizeView.setHeight(autoSizeView.getHeight() / 4);
+ }
+ });
+ mInstrumentation.waitForIdleSync();
+
+ assertTrue(autoSizeView.getTextSize() < initialTextSize);
+ }
+
+ @Test
+ @MediumTest
+ public void testAutoSizeCallers_setCompoundDrawables() throws Throwable {
+ final T autoSizeView = prepareAndRetrieveAutoSizeTestData(R.id.view_autosize_uniform,
+ false);
+ final float initialTextSize = autoSizeView.getTextSize();
+ final Drawable drawable = ResourcesCompat.getDrawable(mActivity.getResources(),
+ R.drawable.test_drawable_red, null);
+ drawable.setBounds(0, 0, autoSizeView.getWidth() / 3, autoSizeView.getHeight() / 3);
+ mActivityTestRule.runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ autoSizeView.setCompoundDrawables(drawable, drawable, drawable, drawable);
+ }
+ });
+ mInstrumentation.waitForIdleSync();
+
+ assertTrue(autoSizeView.getTextSize() < initialTextSize);
+ }
+
+ @Test
+ @MediumTest
+ public void testAutoSizeCallers_setCompoundDrawablesRelative() throws Throwable {
+ if (Build.VERSION.SDK_INT >= 17) {
+ final T autoSizeView = prepareAndRetrieveAutoSizeTestData(
+ R.id.view_autosize_uniform, false);
+ final float initialTextSize = autoSizeView.getTextSize();
+ final Drawable drawable = ResourcesCompat.getDrawable(mActivity.getResources(),
+ R.drawable.test_drawable_red, null);
+ drawable.setBounds(0, 0, autoSizeView.getWidth() / 3,
+ autoSizeView.getHeight() / 3);
+ mActivityTestRule.runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ if (Build.VERSION.SDK_INT >= 17) {
+ autoSizeView.setCompoundDrawablesRelative(
+ drawable, drawable, drawable, drawable);
+ }
+ }
+ });
+ mInstrumentation.waitForIdleSync();
+
+ assertTrue(autoSizeView.getTextSize() < initialTextSize);
+ }
+ }
+
+ @Test
+ @MediumTest
+ public void testAutoSizeCallers_setCompoundDrawablePadding() throws Throwable {
+ final T autoSizeView = prepareAndRetrieveAutoSizeTestData(R.id.view_autosize_uniform,
+ false);
+ // Prepare a larger layout in order not to hit the min value easily.
+ mActivityTestRule.runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ autoSizeView.setWidth(autoSizeView.getWidth() * 2);
+ autoSizeView.setHeight(autoSizeView.getHeight() * 2);
+ }
+ });
+ mInstrumentation.waitForIdleSync();
+ // Setup the drawables before setting their padding in order to modify the available
+ // space and trigger a resize.
+ final Drawable drawable = ResourcesCompat.getDrawable(mActivity.getResources(),
+ R.drawable.test_drawable_red, null);
+ drawable.setBounds(0, 0, autoSizeView.getWidth() / 4, autoSizeView.getHeight() / 4);
+ mActivityTestRule.runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ autoSizeView.setCompoundDrawables(
+ drawable, drawable, drawable, drawable);
+ }
+ });
+ mInstrumentation.waitForIdleSync();
+ final float initialTextSize = autoSizeView.getTextSize();
+ mActivityTestRule.runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ autoSizeView.setCompoundDrawablePadding(
+ autoSizeView.getCompoundDrawablePadding() + 10);
+ }
+ });
+ mInstrumentation.waitForIdleSync();
+
+ assertTrue(autoSizeView.getTextSize() < initialTextSize);
+ }
+
+ @Test
+ @MediumTest
+ public void testAutoSizeCallers_setPadding() throws Throwable {
+ final T autoSizeView = prepareAndRetrieveAutoSizeTestData(R.id.view_autosize_uniform,
+ false);
+ final float initialTextSize = autoSizeView.getTextSize();
+ mActivityTestRule.runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ autoSizeView.setPadding(
+ autoSizeView.getWidth() / 3, autoSizeView.getHeight() / 3,
+ autoSizeView.getWidth() / 3, autoSizeView.getHeight() / 3);
+ }
+ });
+ mInstrumentation.waitForIdleSync();
+
+ assertTrue(autoSizeView.getTextSize() < initialTextSize);
+ }
+
+ @Test
+ @MediumTest
+ public void testAutoSizeCallers_setPaddingRelative() throws Throwable {
+ if (Build.VERSION.SDK_INT >= 16) {
+ final T autoSizeView = prepareAndRetrieveAutoSizeTestData(
+ R.id.view_autosize_uniform, false);
+ final float initialTextSize = autoSizeView.getTextSize();
+
+ mActivityTestRule.runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ if (Build.VERSION.SDK_INT > 16) {
+ autoSizeView.setPaddingRelative(
+ autoSizeView.getWidth() / 3, autoSizeView.getHeight() / 3,
+ autoSizeView.getWidth() / 3, autoSizeView.getHeight() / 3);
+ }
+ }
+ });
+ mInstrumentation.waitForIdleSync();
+
+ assertTrue(autoSizeView.getTextSize() < initialTextSize);
+ }
+ }
+
+ @Test
+ @MediumTest
+ public void testAutoSizeCallers_setTypeface() throws Throwable {
+ final T autoSizeView = prepareAndRetrieveAutoSizeTestData(R.id.view_autosize_uniform,
+ false);
+ mActivityTestRule.runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ autoSizeView.setText("The typeface change needs a bit more text then "
+ + "the default used for this batch of tests in order to get to resize text."
+ + " The resize function is always called but even with different typefaces "
+ + "there may not be a need to resize text because it just fits. The longer "
+ + "the text, the higher the chance for a resize. And here is yet another "
+ + "sentence to make sure this test is not flaky. Not flaky at all.");
+ }
+ });
+ mInstrumentation.waitForIdleSync();
+ final float initialTextSize = autoSizeView.getTextSize();
+
+ mActivityTestRule.runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ Typeface differentTypeface = Typeface.MONOSPACE;
+ if (autoSizeView.getTypeface() == Typeface.MONOSPACE) {
+ differentTypeface = Typeface.SANS_SERIF;
+ }
+ autoSizeView.setTypeface(differentTypeface);
+ }
+ });
+ mInstrumentation.waitForIdleSync();
+ final float changedTextSize = autoSizeView.getTextSize();
+
+ // Don't really know if it is larger or smaller (depends on the typeface chosen above),
+ // but it should definitely have changed.
+ assertNotEquals(initialTextSize, changedTextSize, 0f);
+
+ mActivityTestRule.runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ autoSizeView.setTypeface(autoSizeView.getTypeface());
+ }
+ });
+ mInstrumentation.waitForIdleSync();
+
+ assertEquals(changedTextSize, autoSizeView.getTextSize(), 0f);
+ }
+
+ @Test
+ @MediumTest
+ public void testAutoSizeCallers_setLetterSpacing() throws Throwable {
+ if (Build.VERSION.SDK_INT >= 21) {
+ final T autoSizeView = prepareAndRetrieveAutoSizeTestData(
+ R.id.view_autosize_uniform, false);
+ final float initialTextSize = autoSizeView.getTextSize();
+
+ mActivityTestRule.runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ if (Build.VERSION.SDK_INT >= 21) {
+ autoSizeView.setLetterSpacing(
+ autoSizeView.getLetterSpacing() * 1.5f + 4.5f);
+ }
+ }
+ });
+ mInstrumentation.waitForIdleSync();
+ final float changedTextSize = autoSizeView.getTextSize();
+
+ assertTrue(changedTextSize < initialTextSize);
+
+ mActivityTestRule.runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ if (Build.VERSION.SDK_INT >= 21) {
+ autoSizeView.setLetterSpacing(autoSizeView.getLetterSpacing());
+ }
+ }
+ });
+ mInstrumentation.waitForIdleSync();
+
+ assertEquals(changedTextSize, autoSizeView.getTextSize(), 0f);
+ }
+ }
+
+ @Test
+ @MediumTest
+ public void testAutoSizeCallers_setMaxHeight() throws Throwable {
+ final T autoSizeView = prepareAndRetrieveAutoSizeTestData(R.id.view_autosize_uniform,
+ true);
+ // Do not force exact height only.
+ final LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
+ 200,
+ LinearLayout.LayoutParams.WRAP_CONTENT);
+ mActivityTestRule.runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ autoSizeView.setLayoutParams(layoutParams);
+ }
+ });
+ mInstrumentation.waitForIdleSync();
+ final float initialTextSize = autoSizeView.getTextSize();
+ mActivityTestRule.runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ autoSizeView.setMaxHeight(autoSizeView.getHeight() / 4);
+ }
+ });
+ mInstrumentation.waitForIdleSync();
+
+ assertTrue(autoSizeView.getTextSize() < initialTextSize);
+ }
+
+ @Test
+ @MediumTest
+ public void testAutoSizeCallers_setMaxWidth() throws Throwable {
+ final T autoSizeView = prepareAndRetrieveAutoSizeTestData(R.id.view_autosize_uniform,
+ true);
+ // Do not force exact width only.
+ final LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
+ LinearLayout.LayoutParams.WRAP_CONTENT,
+ 200);
+ mActivityTestRule.runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ autoSizeView.setLayoutParams(layoutParams);
+ }
+ });
+ mInstrumentation.waitForIdleSync();
+ final float initialTextSize = autoSizeView.getTextSize();
+ mActivityTestRule.runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ autoSizeView.setMaxWidth(autoSizeView.getWidth() / 4);
+ }
+ });
+ mInstrumentation.waitForIdleSync();
+
+ assertTrue(autoSizeView.getTextSize() != initialTextSize);
+ }
+
+ @Test
+ @MediumTest
+ public void testAutoSizeCallers_setWidth() throws Throwable {
+ final T autoSizeView = prepareAndRetrieveAutoSizeTestData(R.id.view_autosize_uniform,
+ true);
+ // Do not force exact width only.
+ final LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
+ LinearLayout.LayoutParams.WRAP_CONTENT,
+ 200);
+ mActivityTestRule.runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ autoSizeView.setLayoutParams(layoutParams);
+ }
+ });
+ mInstrumentation.waitForIdleSync();
+
+ final float initialTextSize = autoSizeView.getTextSize();
+ mActivityTestRule.runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ autoSizeView.setWidth(autoSizeView.getWidth() / 4);
+ }
+ });
+ mInstrumentation.waitForIdleSync();
+
+ assertTrue(autoSizeView.getTextSize() != initialTextSize);
+ }
+
+ @Test
+ @MediumTest
+ public void testAutoSizeCallers_setTextSizeIsNoOp() throws Throwable {
+ final T autoSizeView = prepareAndRetrieveAutoSizeTestData(R.id.view_autosize_uniform,
+ false);
+ final float initialTextSize = autoSizeView.getTextSize();
+
+ mActivityTestRule.runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ autoSizeView.setTextSize(initialTextSize + 123f);
+ }
+ });
+ mInstrumentation.waitForIdleSync();
+
+ assertEquals(initialTextSize, autoSizeView.getTextSize(), 0f);
+ }
+
+ @Test
+ @MediumTest
+ public void testAutoSizeCallers_setHorizontallyScrolling() throws Throwable {
+ final T autoSizeView = prepareAndRetrieveAutoSizeTestData(R.id.view_autosize_uniform,
+ false);
+ // Horizontal scrolling is expected to be deactivated for this test.
+ final float initialTextSize = autoSizeView.getTextSize();
+ mActivityTestRule.runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ autoSizeView.setHorizontallyScrolling(true);
+ }
+ });
+ mInstrumentation.waitForIdleSync();
+ assertTrue(autoSizeView.getTextSize() > initialTextSize);
+
+ mActivityTestRule.runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ autoSizeView.setHorizontallyScrolling(false);
+ }
+ });
+ mInstrumentation.waitForIdleSync();
+ assertEquals(initialTextSize, autoSizeView.getTextSize(), 0f);
+ }
+
+ @Test
+ @MediumTest
+ public void testAutoSize_setEllipsize() throws Throwable {
+ final T autoSizeView = mActivity.findViewById(R.id.view_autosize_uniform_predef_sizes);
+ final int initialAutoSizeType = ((AutoSizeableTextView) autoSizeView).getAutoSizeTextType();
+ final int initialMinTextSize =
+ ((AutoSizeableTextView) autoSizeView).getAutoSizeMinTextSize();
+ final int initialMaxTextSize =
+ ((AutoSizeableTextView) autoSizeView).getAutoSizeMaxTextSize();
+ final int initialAutoSizeGranularity =
+ ((AutoSizeableTextView) autoSizeView).getAutoSizeStepGranularity();
+ final int initialSizes =
+ ((AutoSizeableTextView) autoSizeView).getAutoSizeTextAvailableSizes().length;
+
+ assertEquals(null, autoSizeView.getEllipsize());
+ // Verify styled attributes.
+ assertEquals(TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM, initialAutoSizeType);
+ assertNotEquals(-1, initialMinTextSize);
+ assertNotEquals(-1, initialMaxTextSize);
+ // Because this TextView has been configured to use predefined sizes.
+ assertEquals(-1, initialAutoSizeGranularity);
+ assertNotEquals(0, initialSizes);
+
+ final TextUtils.TruncateAt newEllipsizeValue = TextUtils.TruncateAt.END;
+ mActivityTestRule.runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ autoSizeView.setEllipsize(newEllipsizeValue);
+ }
+ });
+ mInstrumentation.waitForIdleSync();
+ assertEquals(newEllipsizeValue, autoSizeView.getEllipsize());
+ // Beside the ellipsis no auto-size attribute has changed.
+ assertEquals(initialAutoSizeType,
+ ((AutoSizeableTextView) autoSizeView).getAutoSizeTextType());
+ assertEquals(initialMinTextSize,
+ ((AutoSizeableTextView) autoSizeView).getAutoSizeMinTextSize());
+ assertEquals(initialMaxTextSize,
+ ((AutoSizeableTextView) autoSizeView).getAutoSizeMaxTextSize());
+ assertEquals(initialAutoSizeGranularity,
+ ((AutoSizeableTextView) autoSizeView).getAutoSizeStepGranularity());
+ assertEquals(initialSizes,
+ ((AutoSizeableTextView) autoSizeView).getAutoSizeTextAvailableSizes().length);
+ }
+
+ @Test
+ @MediumTest
+ public void testEllipsize_setAutoSize() throws Throwable {
+ final T view = mActivity.findViewById(R.id.view_text);
+ final TextUtils.TruncateAt newEllipsizeValue = TextUtils.TruncateAt.END;
+ mActivityTestRule.runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ view.setEllipsize(newEllipsizeValue);
+ }
+ });
+ mInstrumentation.waitForIdleSync();
+ assertEquals(newEllipsizeValue, view.getEllipsize());
+ assertEquals(TextViewCompat.AUTO_SIZE_TEXT_TYPE_NONE,
+ ((AutoSizeableTextView) view).getAutoSizeTextType());
+ assertEquals(-1, ((AutoSizeableTextView) view).getAutoSizeMinTextSize());
+ assertEquals(-1, ((AutoSizeableTextView) view).getAutoSizeMaxTextSize());
+ assertEquals(-1, ((AutoSizeableTextView) view).getAutoSizeStepGranularity());
+ assertEquals(0,
+ ((AutoSizeableTextView) view).getAutoSizeTextAvailableSizes().length);
+
+ mActivityTestRule.runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ ((AutoSizeableTextView) view).setAutoSizeTextTypeWithDefaults(
+ TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM);
+ }
+ });
+ mInstrumentation.waitForIdleSync();
+ assertEquals(newEllipsizeValue, view.getEllipsize());
+ assertEquals(TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM,
+ ((AutoSizeableTextView) view).getAutoSizeTextType());
+ // The auto-size defaults have been used.
+ assertNotEquals(-1, ((AutoSizeableTextView) view).getAutoSizeMinTextSize());
+ assertNotEquals(-1, ((AutoSizeableTextView) view).getAutoSizeMaxTextSize());
+ assertNotEquals(-1, ((AutoSizeableTextView) view).getAutoSizeStepGranularity());
+ assertNotEquals(0,
+ ((AutoSizeableTextView) view).getAutoSizeTextAvailableSizes().length);
+ }
+
+ @Test
+ @MediumTest
+ public void testAutoSizeUniform_obtainStyledAttributesUsingPredefinedSizes() {
+ DisplayMetrics m = mActivity.getResources().getDisplayMetrics();
+ final T autoSizeViewUniform = mActivity.findViewById(
+ R.id.view_autosize_uniform_predef_sizes);
+
+ // In arrays.xml predefined the step sizes as: 5px, 11dip, 19sp, 29pt, 43mm and 53in.
+ // TypedValue can not use the math library and instead rounds the value by adding
+ // 0.5f when obtaining styled attributes. Check TypedValue#complexToDimensionPixelSize(...)
+ int[] expectedSizesInPx = new int[] {
+ Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, 5f, m)),
+ Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 11f, m)),
+ Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 19f, m)),
+ Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PT, 29f, m)),
+ Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_MM, 43f, m)),
+ Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_IN, 53f, m))};
+
+ boolean containsValueFromExpectedSizes = false;
+ final int textSize = (int) autoSizeViewUniform.getTextSize();
+ for (int i = 0; i < expectedSizesInPx.length; i++) {
+ if (expectedSizesInPx[i] == textSize) {
+ containsValueFromExpectedSizes = true;
+ break;
+ }
+ }
+ assertTrue(containsValueFromExpectedSizes);
+ }
+
+ @Test
+ @MediumTest
+ public void testAutoSizeUniform_obtainStyledAttributesPredefinedSizesFiltering() {
+ T autoSizeViewUniform = mActivity.findViewById(
+ R.id.view_autosize_uniform_predef_sizes_redundant_values);
+
+ // In arrays.xml predefined the step sizes as: 40px, 10px, 10px, 10px, 0dp.
+ final int[] expectedSizes = new int[] {10, 40};
+ assertArrayEquals(expectedSizes,
+ ((AutoSizeableTextView) autoSizeViewUniform).getAutoSizeTextAvailableSizes());
+ }
+
+ @Test
+ @MediumTest
+ public void testAutoSizeUniform_predefinedSizesFilteringAndSorting() throws Throwable {
+ final T view = mActivity.findViewById(R.id.view_text);
+ assertEquals(TextViewCompat.AUTO_SIZE_TEXT_TYPE_NONE,
+ ((AutoSizeableTextView) view).getAutoSizeTextType());
+
+ final int[] predefinedSizes = new int[] {400, 0, 10, 40, 10, 10, 0, 0};
+ mActivityTestRule.runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ ((AutoSizeableTextView) view).setAutoSizeTextTypeUniformWithPresetSizes(
+ predefinedSizes, TypedValue.COMPLEX_UNIT_PX);
+ }
+ });
+ mInstrumentation.waitForIdleSync();
+ assertArrayEquals(new int[] {10, 40, 400},
+ ((AutoSizeableTextView) view).getAutoSizeTextAvailableSizes());
+ }
+
+ @Test(expected = NullPointerException.class)
+ @SmallTest
+ public void testAutoSizeUniform_predefinedSizesNullArray() throws Throwable {
+ final T view = mActivity.findViewById(R.id.view_text);
+ assertEquals(TextViewCompat.AUTO_SIZE_TEXT_TYPE_NONE,
+ ((AutoSizeableTextView) view).getAutoSizeTextType());
+
+ final int[] predefinedSizes = null;
+ mActivityTestRule.runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ ((AutoSizeableTextView) view).setAutoSizeTextTypeUniformWithPresetSizes(
+ predefinedSizes, TypedValue.COMPLEX_UNIT_PX);
+ }
+ });
+ mInstrumentation.waitForIdleSync();
+ }
+
+ @Test
+ @MediumTest
+ public void testAutoSizeUniform_predefinedSizesEmptyArray() throws Throwable {
+ final T view = mActivity.findViewById(R.id.view_text);
+ assertEquals(TextViewCompat.AUTO_SIZE_TEXT_TYPE_NONE,
+ ((AutoSizeableTextView) view).getAutoSizeTextType());
+
+ mActivityTestRule.runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ ((AutoSizeableTextView) view).setAutoSizeTextTypeWithDefaults(
+ TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM);
+ }
+ });
+ mInstrumentation.waitForIdleSync();
+
+ final int[] defaultSizes = ((AutoSizeableTextView) view).getAutoSizeTextAvailableSizes();
+ assertNotNull(defaultSizes);
+ assertTrue(defaultSizes.length > 0);
+
+ final int[] predefinedSizes = new int[0];
+ mActivityTestRule.runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ ((AutoSizeableTextView) view).setAutoSizeTextTypeUniformWithPresetSizes(
+ predefinedSizes, TypedValue.COMPLEX_UNIT_PX);
+ }
+ });
+ mInstrumentation.waitForIdleSync();
+
+ final int[] newSizes = ((AutoSizeableTextView) view).getAutoSizeTextAvailableSizes();
+ assertNotNull(defaultSizes);
+ assertArrayEquals(defaultSizes, newSizes);
+ }
+
+ @Test
+ @MediumTest
+ public void testAutoSizeUniform_buildsSizes() throws Throwable {
+ final T autoSizeViewUniform = mActivity.findViewById(R.id.view_autosize_uniform);
+
+ // Verify that the interval limits are both included.
+ mActivityTestRule.runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ ((AutoSizeableTextView) autoSizeViewUniform)
+ .setAutoSizeTextTypeUniformWithConfiguration(10, 20, 2,
+ TypedValue.COMPLEX_UNIT_PX);
+ }
+ });
+ mInstrumentation.waitForIdleSync();
+ assertArrayEquals(
+ new int[] {10, 12, 14, 16, 18, 20},
+ ((AutoSizeableTextView) autoSizeViewUniform).getAutoSizeTextAvailableSizes());
+
+ mActivityTestRule.runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ ((AutoSizeableTextView) autoSizeViewUniform)
+ .setAutoSizeTextTypeUniformWithConfiguration(
+ ((AutoSizeableTextView) autoSizeViewUniform)
+ .getAutoSizeMinTextSize(),
+ 19,
+ ((AutoSizeableTextView) autoSizeViewUniform)
+ .getAutoSizeStepGranularity(),
+ TypedValue.COMPLEX_UNIT_PX);
+ }
+ });
+ mInstrumentation.waitForIdleSync();
+ assertArrayEquals(
+ new int[] {10, 12, 14, 16, 18},
+ ((AutoSizeableTextView) autoSizeViewUniform).getAutoSizeTextAvailableSizes());
+
+ mActivityTestRule.runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ ((AutoSizeableTextView) autoSizeViewUniform)
+ .setAutoSizeTextTypeUniformWithConfiguration(
+ ((AutoSizeableTextView) autoSizeViewUniform)
+ .getAutoSizeMinTextSize(),
+ 21,
+ ((AutoSizeableTextView) autoSizeViewUniform)
+ .getAutoSizeStepGranularity(),
+ TypedValue.COMPLEX_UNIT_PX);
+ }
+ });
+ mInstrumentation.waitForIdleSync();
+ assertArrayEquals(
+ new int[] {10, 12, 14, 16, 18, 20},
+ ((AutoSizeableTextView) autoSizeViewUniform).getAutoSizeTextAvailableSizes());
+ }
+
+ @Test
+ @MediumTest
+ public void testAutoSizeUniform_getSetAutoSizeTextDefaults() {
+ final T autoSizeView = getNewAutoSizeViewInstance();
+ assertEquals(TextViewCompat.AUTO_SIZE_TEXT_TYPE_NONE,
+ ((AutoSizeableTextView) autoSizeView).getAutoSizeTextType());
+ // Min/Max/Granularity values for auto-sizing are 0 because they are not used.
+ assertEquals(-1, ((AutoSizeableTextView) autoSizeView).getAutoSizeMinTextSize());
+ assertEquals(-1, ((AutoSizeableTextView) autoSizeView).getAutoSizeMaxTextSize());
+ assertEquals(-1,
+ ((AutoSizeableTextView) autoSizeView).getAutoSizeStepGranularity());
+
+ ((AutoSizeableTextView) autoSizeView).setAutoSizeTextTypeWithDefaults(
+ TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM);
+ assertEquals(TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM,
+ ((AutoSizeableTextView) autoSizeView).getAutoSizeTextType());
+ // Min/Max default values for auto-sizing XY have been loaded.
+ final int minSize = ((AutoSizeableTextView) autoSizeView).getAutoSizeMinTextSize();
+ final int maxSize = ((AutoSizeableTextView) autoSizeView).getAutoSizeMaxTextSize();
+ assertTrue(0 < minSize);
+ assertTrue(minSize < maxSize);
+ assertNotEquals(0,
+ ((AutoSizeableTextView) autoSizeView).getAutoSizeStepGranularity());
+
+ ((AutoSizeableTextView) autoSizeView)
+ .setAutoSizeTextTypeWithDefaults(TextViewCompat.AUTO_SIZE_TEXT_TYPE_NONE);
+ assertEquals(TextViewCompat.AUTO_SIZE_TEXT_TYPE_NONE,
+ ((AutoSizeableTextView) autoSizeView).getAutoSizeTextType());
+ // Min/Max values for auto-sizing XY have been cleared.
+ assertEquals(-1, ((AutoSizeableTextView) autoSizeView).getAutoSizeMinTextSize());
+ assertEquals(-1, ((AutoSizeableTextView) autoSizeView).getAutoSizeMaxTextSize());
+ assertEquals(-1,
+ ((AutoSizeableTextView) autoSizeView).getAutoSizeStepGranularity());
+ }
+
+ @Test
+ @MediumTest
+ public void testAutoSizeUniform_getSetAutoSizeStepGranularity() {
+ final T autoSizeView = getNewAutoSizeViewInstance();
+ assertEquals(TextViewCompat.AUTO_SIZE_TEXT_TYPE_NONE,
+ ((AutoSizeableTextView) autoSizeView).getAutoSizeTextType());
+ final int initialValue = -1;
+ assertEquals(initialValue,
+ ((AutoSizeableTextView) autoSizeView).getAutoSizeStepGranularity());
+
+ ((AutoSizeableTextView) autoSizeView).setAutoSizeTextTypeWithDefaults(
+ TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM);
+ assertEquals(TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM,
+ ((AutoSizeableTextView) autoSizeView).getAutoSizeTextType());
+ final int defaultValue = 1; // 1px.
+ // If the auto-size type is AUTO_SIZE_TEXT_TYPE_UNIFORM then it means autoSizeView went
+ // through the auto-size setup and given that 0 is an invalid value it changed it to the
+ // default.
+ assertEquals(defaultValue,
+ ((AutoSizeableTextView) autoSizeView).getAutoSizeStepGranularity());
+
+ final int newValue = 33;
+ ((AutoSizeableTextView) autoSizeView).setAutoSizeTextTypeUniformWithConfiguration(
+ ((AutoSizeableTextView) autoSizeView).getAutoSizeMinTextSize(),
+ ((AutoSizeableTextView) autoSizeView).getAutoSizeMaxTextSize(),
+ newValue,
+ TypedValue.COMPLEX_UNIT_PX);
+ assertEquals(newValue, ((AutoSizeableTextView) autoSizeView).getAutoSizeStepGranularity());
+ }
+
+ @Test
+ @MediumTest
+ public void testAutoSizeUniform_getSetAutoSizeMinTextSize() {
+ final T autoSizeView = getNewAutoSizeViewInstance();
+ ((AutoSizeableTextView) autoSizeView).setAutoSizeTextTypeWithDefaults(
+ TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM);
+ assertEquals(TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM,
+ ((AutoSizeableTextView) autoSizeView).getAutoSizeTextType());
+ final int minSize = ((AutoSizeableTextView) autoSizeView).getAutoSizeMinTextSize();
+ assertNotEquals(0, minSize);
+ final int maxSize = ((AutoSizeableTextView) autoSizeView).getAutoSizeMaxTextSize();
+ assertNotEquals(0, maxSize);
+
+ // This is just a test check to verify the next assertions. If this fails it is a problem
+ // of this test setup (we need at least 2 units).
+ assertTrue((maxSize - minSize) > 1);
+ final int newMinSize = maxSize - 1;
+ ((AutoSizeableTextView) autoSizeView).setAutoSizeTextTypeUniformWithConfiguration(
+ newMinSize,
+ ((AutoSizeableTextView) autoSizeView).getAutoSizeMaxTextSize(),
+ ((AutoSizeableTextView) autoSizeView).getAutoSizeStepGranularity(),
+ TypedValue.COMPLEX_UNIT_PX);
+
+ assertEquals(newMinSize, ((AutoSizeableTextView) autoSizeView).getAutoSizeMinTextSize());
+ // Max size has not changed.
+ assertEquals(maxSize, ((AutoSizeableTextView) autoSizeView).getAutoSizeMaxTextSize());
+
+ ((AutoSizeableTextView) autoSizeView).setAutoSizeTextTypeUniformWithConfiguration(
+ newMinSize,
+ newMinSize + 10,
+ ((AutoSizeableTextView) autoSizeView).getAutoSizeStepGranularity(),
+ TypedValue.COMPLEX_UNIT_SP);
+
+ // It does not matter which unit has been used to set the min size, the getter always
+ // returns it in pixels.
+ assertEquals(Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
+ newMinSize, mActivity.getResources().getDisplayMetrics())),
+ ((AutoSizeableTextView) autoSizeView).getAutoSizeMinTextSize());
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ @SmallTest
+ public void testAutoSizeUniform_throwsException_whenMaxLessThanMin() {
+ final T autoSizeView = getNewAutoSizeViewInstance();
+ ((AutoSizeableTextView) autoSizeView).setAutoSizeTextTypeUniformWithConfiguration(
+ 10, 9, 1, TypedValue.COMPLEX_UNIT_SP);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ @SmallTest
+ public void testAutoSizeUniform_throwsException_minLessThanZero() {
+ final T autoSizeView = getNewAutoSizeViewInstance();
+ ((AutoSizeableTextView) autoSizeView).setAutoSizeTextTypeUniformWithConfiguration(
+ -1, 9, 1, TypedValue.COMPLEX_UNIT_SP);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ @SmallTest
+ public void testAutoSizeUniform_throwsException_maxLessThanZero() {
+ final T autoSizeView = getNewAutoSizeViewInstance();
+ ((AutoSizeableTextView) autoSizeView).setAutoSizeTextTypeUniformWithConfiguration(
+ 10, -1, 1, TypedValue.COMPLEX_UNIT_SP);
+ }
+
+ @Test(expected = IllegalArgumentException.class)
+ @SmallTest
+ public void testAutoSizeUniform_throwsException_granularityLessThanZero() {
+ final T autoSizeView = getNewAutoSizeViewInstance();
+ ((AutoSizeableTextView) autoSizeView).setAutoSizeTextTypeUniformWithConfiguration(
+ 10, 20, -1, TypedValue.COMPLEX_UNIT_SP);
+ }
+
+ @Test
+ @MediumTest
+ public void testAutoSizeUniform_getSetAutoSizeMaxTextSize() {
+ final T autoSizeView = getNewAutoSizeViewInstance();
+ ((AutoSizeableTextView) autoSizeView).setAutoSizeTextTypeWithDefaults(
+ TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM);
+ assertEquals(TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM,
+ ((AutoSizeableTextView) autoSizeView).getAutoSizeTextType());
+ final int minSize = ((AutoSizeableTextView) autoSizeView).getAutoSizeMinTextSize();
+ assertNotEquals(0, minSize);
+ final int maxSize = ((AutoSizeableTextView) autoSizeView).getAutoSizeMaxTextSize();
+ assertNotEquals(0, maxSize);
+
+ final int newMaxSize = maxSize + 11;
+ ((AutoSizeableTextView) autoSizeView).setAutoSizeTextTypeUniformWithConfiguration(
+ ((AutoSizeableTextView) autoSizeView).getAutoSizeMinTextSize(),
+ newMaxSize,
+ ((AutoSizeableTextView) autoSizeView).getAutoSizeStepGranularity(),
+ TypedValue.COMPLEX_UNIT_PX);
+
+ assertEquals(newMaxSize, ((AutoSizeableTextView) autoSizeView).getAutoSizeMaxTextSize());
+ // Min size has not changed.
+ assertEquals(minSize, ((AutoSizeableTextView) autoSizeView).getAutoSizeMinTextSize());
+ ((AutoSizeableTextView) autoSizeView).setAutoSizeTextTypeUniformWithConfiguration(
+ ((AutoSizeableTextView) autoSizeView).getAutoSizeMinTextSize(),
+ newMaxSize,
+ ((AutoSizeableTextView) autoSizeView).getAutoSizeStepGranularity(),
+ TypedValue.COMPLEX_UNIT_SP);
+ // It does not matter which unit has been used to set the max size, the getter always
+ // returns it in pixels.
+ assertEquals(Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
+ newMaxSize, mActivity.getResources().getDisplayMetrics())),
+ ((AutoSizeableTextView) autoSizeView).getAutoSizeMaxTextSize());
+ }
+
+ @Test
+ @MediumTest
+ public void testAutoSizeCallers_setTextSizeChangesSizeWhenAutoSizeDisabled() throws Throwable {
+ final T autoSizeView = prepareAndRetrieveAutoSizeTestData(R.id.view_autosize_uniform,
+ false);
+ // Disable auto-size.
+ mActivityTestRule.runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ ((AutoSizeableTextView) autoSizeView).setAutoSizeTextTypeWithDefaults(
+ TextViewCompat.AUTO_SIZE_TEXT_TYPE_NONE);
+ }
+ });
+ mInstrumentation.waitForIdleSync();
+ final float newTextSizeInPx = 123f;
+ assertNotEquals(newTextSizeInPx, autoSizeView.getTextSize(), 0f);
+
+ mActivityTestRule.runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ autoSizeView.setTextSize(TypedValue.COMPLEX_UNIT_PX, newTextSizeInPx);
+ }
+ });
+ mInstrumentation.waitForIdleSync();
+
+ assertEquals(newTextSizeInPx, autoSizeView.getTextSize(), 0f);
+ }
+
+ /**
+ * Some View attributes require non-fixed width and/or layout height. This function removes
+ * all other existing views from the layout leaving only one auto-size TextView (for exercising
+ * the auto-size behavior) which has been set up to suit the test needs.
+ *
+ * @param viewId The id of the view to prepare.
+ * @param shouldWrapLayoutContent Specifies if the layout params should wrap content
+ *
+ * @return a View configured for auto size tests.
+ */
+ private T prepareAndRetrieveAutoSizeTestData(final @IdRes int viewId,
+ final boolean shouldWrapLayoutContent) throws Throwable {
+ mActivityTestRule.runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ LinearLayout ll = mActivity.findViewById(R.id.container);
+ T targetedView = mActivity.findViewById(viewId);
+ ll.removeAllViews();
+ ll.addView(targetedView);
+ }
+ });
+ mInstrumentation.waitForIdleSync();
+
+ final T view = mActivity.findViewById(viewId);
+ if (shouldWrapLayoutContent) {
+ // Do not force exact width or height.
+ final LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
+ LinearLayout.LayoutParams.WRAP_CONTENT,
+ LinearLayout.LayoutParams.WRAP_CONTENT);
+ mActivityTestRule.runOnUiThread(new Runnable() {
+ @Override
+ public void run() {
+ view.setLayoutParams(layoutParams);
+ }
+ });
+ mInstrumentation.waitForIdleSync();
+ }
+
+ return view;
+ }
+
+ // Returns a new instance of the auto-sizable view for mActivity.
+ protected abstract T getNewAutoSizeViewInstance();
+}
diff --git a/v7/appcompat/tests/src/android/support/v7/widget/AppCompatButtonAutoSizeActivity.java b/v7/appcompat/tests/src/android/support/v7/widget/AppCompatButtonAutoSizeActivity.java
new file mode 100644
index 0000000..cd5c6a9
--- /dev/null
+++ b/v7/appcompat/tests/src/android/support/v7/widget/AppCompatButtonAutoSizeActivity.java
@@ -0,0 +1,31 @@
+/*
+ * Copyright (C) 2017 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 android.support.v7.appcompat.test.R;
+import android.support.v7.testutils.BaseTestActivity;
+
+/**
+ * This activity is used to test the auto-size feature of the {@link AppCompatButton}
+ * class.
+ */
+public class AppCompatButtonAutoSizeActivity extends BaseTestActivity {
+ @Override
+ protected int getContentViewLayoutResId() {
+ return R.layout.appcompat_button_autosize_activity;
+ }
+}
diff --git a/v7/appcompat/tests/src/android/support/v7/widget/AppCompatButtonAutoSizeTest.java b/v7/appcompat/tests/src/android/support/v7/widget/AppCompatButtonAutoSizeTest.java
new file mode 100644
index 0000000..cc54463
--- /dev/null
+++ b/v7/appcompat/tests/src/android/support/v7/widget/AppCompatButtonAutoSizeTest.java
@@ -0,0 +1,37 @@
+/*
+ * Copyright (C) 2017 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 android.support.test.filters.MediumTest;
+import android.support.test.runner.AndroidJUnit4;
+
+import org.junit.runner.RunWith;
+
+@MediumTest
+@RunWith(AndroidJUnit4.class)
+public class AppCompatButtonAutoSizeTest extends
+ AppCompatBaseAutoSizeTest<AppCompatButtonAutoSizeActivity, AppCompatButton> {
+
+ public AppCompatButtonAutoSizeTest() {
+ super(AppCompatButtonAutoSizeActivity.class);
+ }
+
+ @Override
+ protected AppCompatButton getNewAutoSizeViewInstance() {
+ return new AppCompatButton(mActivity);
+ }
+}
diff --git a/v7/appcompat/tests/src/android/support/v7/widget/AppCompatTextViewAutoSizeActivity.java b/v7/appcompat/tests/src/android/support/v7/widget/AppCompatTextViewAutoSizeActivity.java
index 17ff6a2..5736dd9 100644
--- a/v7/appcompat/tests/src/android/support/v7/widget/AppCompatTextViewAutoSizeActivity.java
+++ b/v7/appcompat/tests/src/android/support/v7/widget/AppCompatTextViewAutoSizeActivity.java
@@ -15,6 +15,7 @@
*/
package android.support.v7.widget;
+import android.support.v7.appcompat.test.R;
import android.support.v7.testutils.BaseTestActivity;
/**
@@ -24,6 +25,6 @@
public class AppCompatTextViewAutoSizeActivity extends BaseTestActivity {
@Override
protected int getContentViewLayoutResId() {
- return android.support.v7.appcompat.test.R.layout.appcompat_textview_autosize_activity;
+ return R.layout.appcompat_textview_autosize_activity;
}
}
diff --git a/v7/appcompat/tests/src/android/support/v7/widget/AppCompatTextViewAutoSizeTest.java b/v7/appcompat/tests/src/android/support/v7/widget/AppCompatTextViewAutoSizeTest.java
index 95d4af3..45da1e4 100644
--- a/v7/appcompat/tests/src/android/support/v7/widget/AppCompatTextViewAutoSizeTest.java
+++ b/v7/appcompat/tests/src/android/support/v7/widget/AppCompatTextViewAutoSizeTest.java
@@ -17,286 +17,39 @@
import static junit.framework.TestCase.assertEquals;
-import static org.junit.Assert.assertArrayEquals;
-import static org.junit.Assert.assertNotEquals;
-import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
-import android.app.Instrumentation;
import android.content.Context;
-import android.graphics.Typeface;
-import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.annotation.Nullable;
-import android.support.test.InstrumentationRegistry;
import android.support.test.filters.MediumTest;
-import android.support.test.filters.SdkSuppress;
-import android.support.test.rule.ActivityTestRule;
import android.support.test.runner.AndroidJUnit4;
-import android.support.v4.content.res.ResourcesCompat;
import android.support.v4.widget.TextViewCompat;
import android.support.v7.appcompat.test.R;
-import android.text.TextUtils;
import android.text.method.SingleLineTransformationMethod;
import android.util.AttributeSet;
-import android.util.DisplayMetrics;
-import android.util.TypedValue;
import android.widget.LinearLayout;
-import android.widget.TextView;
-import org.junit.Assert;
-import org.junit.Before;
-import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
@MediumTest
@RunWith(AndroidJUnit4.class)
-public class AppCompatTextViewAutoSizeTest {
- @Rule
- public final ActivityTestRule<AppCompatTextViewAutoSizeActivity> mActivityTestRule;
-
- private Instrumentation mInstrumentation;
- private AppCompatTextViewAutoSizeActivity mActivity;
+public class AppCompatTextViewAutoSizeTest extends
+ AppCompatBaseAutoSizeTest<AppCompatTextViewAutoSizeActivity, AppCompatTextView> {
public AppCompatTextViewAutoSizeTest() {
- mActivityTestRule = new ActivityTestRule<>(AppCompatTextViewAutoSizeActivity.class);
+ super(AppCompatTextViewAutoSizeActivity.class);
}
- @Before
- public void setup() {
- mInstrumentation = InstrumentationRegistry.getInstrumentation();
- mActivity = mActivityTestRule.getActivity();
- }
-
- @Test
- @SdkSuppress(minSdkVersion = 16)
- // public TextView#getMaxLines only introduced in API 16.
- public void testAutoSizeCallers_setMaxLines() throws Throwable {
- final AppCompatTextView autoSizeTextView = prepareAndRetrieveAutoSizeTestData(
- R.id.textview_autosize_uniform, false);
- // Configure layout params and auto-size both in pixels to dodge flakiness on different
- // devices.
- final LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
- 500, 500);
- final String text = "one two three four five six seven eight nine ten";
- mActivity.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- autoSizeTextView.setLayoutParams(layoutParams);
- autoSizeTextView.setAutoSizeTextTypeUniformWithConfiguration(
- 1 /* autoSizeMinTextSize */,
- 5000 /* autoSizeMaxTextSize */,
- 1 /* autoSizeStepGranularity */,
- TypedValue.COMPLEX_UNIT_PX);
- autoSizeTextView.setText(text);
- }
- });
- mInstrumentation.waitForIdleSync();
-
- float initialSize = 0;
- for (int i = 1; i < 10; i++) {
- final int maxLines = i;
- mActivity.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- autoSizeTextView.setMaxLines(maxLines);
- }
- });
- mInstrumentation.waitForIdleSync();
- float expectedSmallerSize = autoSizeTextView.getTextSize();
- if (i == 1) {
- initialSize = expectedSmallerSize;
- }
-
- mActivity.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- autoSizeTextView.setMaxLines(maxLines + 1);
- }
- });
- mInstrumentation.waitForIdleSync();
- assertTrue(expectedSmallerSize <= autoSizeTextView.getTextSize());
- }
- assertTrue(initialSize < autoSizeTextView.getTextSize());
-
- initialSize = 999999;
- for (int i = 10; i > 1; i--) {
- final int maxLines = i;
- mActivity.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- autoSizeTextView.setMaxLines(maxLines);
- }
- });
- mInstrumentation.waitForIdleSync();
- float expectedLargerSize = autoSizeTextView.getTextSize();
- if (i == 10) {
- initialSize = expectedLargerSize;
- }
-
- mActivity.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- autoSizeTextView.setMaxLines(maxLines - 1);
- }
- });
- mInstrumentation.waitForIdleSync();
- assertTrue(expectedLargerSize >= autoSizeTextView.getTextSize());
- }
- assertTrue(initialSize > autoSizeTextView.getTextSize());
- }
-
- @Test
- public void testAutoSizeCallers_setText() throws Throwable {
- final AppCompatTextView autoSizeTextView = prepareAndRetrieveAutoSizeTestData(
- R.id.textview_autosize_uniform, false);
-
- // Configure layout params and auto-size both in pixels to dodge flakiness on different
- // devices.
- final LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
- 500, 500);
- mActivity.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- autoSizeTextView.setLayoutParams(layoutParams);
- autoSizeTextView.setAutoSizeTextTypeUniformWithConfiguration(
- 1, 5000, 1, TypedValue.COMPLEX_UNIT_PX);
- }
- });
- mInstrumentation.waitForIdleSync();
-
- final String initialText = "13characters ";
- final StringBuilder textToSet = new StringBuilder().append(initialText);
- float initialSize = 0;
-
- // As we add characters the text size shrinks.
- for (int i = 0; i < 10; i++) {
- mActivity.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- autoSizeTextView.setText(textToSet.toString());
- }
- });
-
- mInstrumentation.waitForIdleSync();
- float expectedLargerSize = autoSizeTextView.getTextSize();
- if (i == 0) {
- initialSize = expectedLargerSize;
- }
-
- textToSet.append(initialText);
- mActivity.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- autoSizeTextView.setText(textToSet.toString());
- }
- });
- mInstrumentation.waitForIdleSync();
- assertTrue(expectedLargerSize >= autoSizeTextView.getTextSize());
- }
- assertTrue(initialSize > autoSizeTextView.getTextSize());
-
- initialSize = 9999999;
- // As we remove characters the text size expands.
- for (int i = 9; i >= 0; i--) {
- mActivity.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- autoSizeTextView.setText(textToSet.toString());
- }
- });
- mInstrumentation.waitForIdleSync();
- float expectedSmallerSize = autoSizeTextView.getTextSize();
- if (i == 0) {
- initialSize = expectedSmallerSize;
- }
-
- textToSet.replace((textToSet.length() - initialText.length()),
- textToSet.length(), "");
- mActivity.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- autoSizeTextView.setText(textToSet.toString());
- }
- });
- mInstrumentation.waitForIdleSync();
-
- assertTrue(autoSizeTextView.getTextSize() >= expectedSmallerSize);
- }
- assertTrue(autoSizeTextView.getTextSize() > initialSize);
- }
-
- @Test
- public void testAutoSizeUniform_equivalentConfigurations() throws Throwable {
- final DisplayMetrics dm = mActivity.getResources().getDisplayMetrics();
- final int minTextSize = 10;
- final int maxTextSize = 20;
- final int granularity = 2;
- final int unit = TypedValue.COMPLEX_UNIT_SP;
-
- final AppCompatTextView granularityTextView = new AppCompatTextView(mActivity);
- granularityTextView.setAutoSizeTextTypeUniformWithConfiguration(
- minTextSize, maxTextSize, granularity, unit);
-
- final AppCompatTextView presetTextView = new AppCompatTextView(mActivity);
- presetTextView.setAutoSizeTextTypeUniformWithPresetSizes(
- new int[]{minTextSize, 12, 14, 16, 18, maxTextSize}, unit);
-
- // The TextViews have been configured differently but the end result should be nearly
- // identical.
- final int expectedAutoSizeType = AppCompatTextView.AUTO_SIZE_TEXT_TYPE_UNIFORM;
- assertEquals(expectedAutoSizeType, granularityTextView.getAutoSizeTextType());
- assertEquals(expectedAutoSizeType, presetTextView.getAutoSizeTextType());
-
- final int expectedMinTextSizeInPx = Math.round(
- TypedValue.applyDimension(unit, minTextSize, dm));
- assertEquals(expectedMinTextSizeInPx, granularityTextView.getAutoSizeMinTextSize());
- assertEquals(expectedMinTextSizeInPx, presetTextView.getAutoSizeMinTextSize());
-
- final int expectedMaxTextSizeInPx = Math.round(
- TypedValue.applyDimension(unit, maxTextSize, dm));
- assertEquals(expectedMaxTextSizeInPx, granularityTextView.getAutoSizeMaxTextSize());
- assertEquals(expectedMaxTextSizeInPx, presetTextView.getAutoSizeMaxTextSize());
-
- // Configured with granularity.
- assertEquals(Math.round(TypedValue.applyDimension(unit, granularity, dm)),
- granularityTextView.getAutoSizeStepGranularity());
- // Configured with preset values, there is no granularity.
- assertEquals(-1, presetTextView.getAutoSizeStepGranularity());
-
- // Both TextViews generate exactly the same sizes in pixels to choose from when auto-sizing.
- assertArrayEquals(
- granularityTextView.getAutoSizeTextAvailableSizes(),
- presetTextView.getAutoSizeTextAvailableSizes());
-
- final String someText = "This is a string";
- final LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
- 500, 500);
- // Configure identically and attach to layout.
- mActivityTestRule.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- granularityTextView.setLayoutParams(layoutParams);
- presetTextView.setLayoutParams(layoutParams);
-
- LinearLayout ll = mActivity.findViewById(R.id.layout_textviewtest);
- ll.removeAllViews();
- ll.addView(granularityTextView);
- ll.addView(presetTextView);
-
- granularityTextView.setText(someText);
- presetTextView.setText(someText);
- }
- });
- mInstrumentation.waitForIdleSync();
-
- assertEquals(granularityTextView.getTextSize(), presetTextView.getTextSize(), 0f);
+ @Override
+ protected AppCompatTextView getNewAutoSizeViewInstance() {
+ return new AppCompatTextView(mActivity);
}
@Test
public void testAutoSize_notSupportedByEditText() throws Throwable {
- final AppCompatEditText autoSizeEditText = (AppCompatEditText) mActivity.findViewById(
+ final AppCompatEditText autoSizeEditText = mActivity.findViewById(
R.id.edittext_autosize_uniform);
// Do not force exact height only.
final LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
@@ -322,854 +75,6 @@
}
@Test
- public void testAutoSizeCallers_setHeight() throws Throwable {
- final AppCompatTextView autoSizeTextView = prepareAndRetrieveAutoSizeTestData(
- R.id.textview_autosize_uniform, true);
- // Do not force exact height only.
- final LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
- 200,
- LinearLayout.LayoutParams.WRAP_CONTENT);
- mActivityTestRule.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- autoSizeTextView.setLayoutParams(layoutParams);
- }
- });
- mInstrumentation.waitForIdleSync();
- final float initialTextSize = autoSizeTextView.getTextSize();
- mActivityTestRule.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- autoSizeTextView.setHeight(autoSizeTextView.getHeight() / 4);
- }
- });
- mInstrumentation.waitForIdleSync();
-
- assertTrue(autoSizeTextView.getTextSize() < initialTextSize);
- }
-
- @Test
- public void testAutoSizeCallers_setCompoundDrawables() throws Throwable {
- final AppCompatTextView autoSizeTextView = prepareAndRetrieveAutoSizeTestData(
- R.id.textview_autosize_uniform, false);
- final float initialTextSize = autoSizeTextView.getTextSize();
- final Drawable drawable = ResourcesCompat.getDrawable(mActivity.getResources(),
- R.drawable.test_drawable_red, null);
- drawable.setBounds(0, 0, autoSizeTextView.getWidth() / 3, autoSizeTextView.getHeight() / 3);
- mActivityTestRule.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- autoSizeTextView.setCompoundDrawables(drawable, drawable, drawable, drawable);
- }
- });
- mInstrumentation.waitForIdleSync();
-
- assertTrue(autoSizeTextView.getTextSize() < initialTextSize);
- }
-
- @Test
- public void testAutoSizeCallers_setCompoundDrawablesRelative() throws Throwable {
- if (Build.VERSION.SDK_INT >= 17) {
- final AppCompatTextView autoSizeTextView = prepareAndRetrieveAutoSizeTestData(
- R.id.textview_autosize_uniform, false);
- final float initialTextSize = autoSizeTextView.getTextSize();
- final Drawable drawable = ResourcesCompat.getDrawable(mActivity.getResources(),
- R.drawable.test_drawable_red, null);
- drawable.setBounds(0, 0, autoSizeTextView.getWidth() / 3,
- autoSizeTextView.getHeight() / 3);
- mActivityTestRule.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- if (Build.VERSION.SDK_INT >= 17) {
- autoSizeTextView.setCompoundDrawablesRelative(
- drawable, drawable, drawable, drawable);
- }
- }
- });
- mInstrumentation.waitForIdleSync();
-
- assertTrue(autoSizeTextView.getTextSize() < initialTextSize);
- }
- }
-
- @Test
- public void testAutoSizeCallers_setCompoundDrawablePadding() throws Throwable {
- final AppCompatTextView autoSizeTextView = prepareAndRetrieveAutoSizeTestData(
- R.id.textview_autosize_uniform, false);
- // Prepare a larger layout in order not to hit the min value easily.
- mActivityTestRule.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- autoSizeTextView.setWidth(autoSizeTextView.getWidth() * 2);
- autoSizeTextView.setHeight(autoSizeTextView.getHeight() * 2);
- }
- });
- mInstrumentation.waitForIdleSync();
- // Setup the drawables before setting their padding in order to modify the available
- // space and trigger a resize.
- final Drawable drawable = ResourcesCompat.getDrawable(mActivity.getResources(),
- R.drawable.test_drawable_red, null);
- drawable.setBounds(0, 0, autoSizeTextView.getWidth() / 4, autoSizeTextView.getHeight() / 4);
- mActivityTestRule.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- autoSizeTextView.setCompoundDrawables(
- drawable, drawable, drawable, drawable);
- }
- });
- mInstrumentation.waitForIdleSync();
- final float initialTextSize = autoSizeTextView.getTextSize();
- mActivityTestRule.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- autoSizeTextView.setCompoundDrawablePadding(
- autoSizeTextView.getCompoundDrawablePadding() + 10);
- }
- });
- mInstrumentation.waitForIdleSync();
-
- assertTrue(autoSizeTextView.getTextSize() < initialTextSize);
- }
-
- @Test
- public void testAutoSizeCallers_setPadding() throws Throwable {
- final AppCompatTextView autoSizeTextView = prepareAndRetrieveAutoSizeTestData(
- R.id.textview_autosize_uniform, false);
- final float initialTextSize = autoSizeTextView.getTextSize();
- mActivityTestRule.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- autoSizeTextView.setPadding(
- autoSizeTextView.getWidth() / 3, autoSizeTextView.getHeight() / 3,
- autoSizeTextView.getWidth() / 3, autoSizeTextView.getHeight() / 3);
- }
- });
- mInstrumentation.waitForIdleSync();
-
- assertTrue(autoSizeTextView.getTextSize() < initialTextSize);
- }
-
- @Test
- public void testAutoSizeCallers_setPaddingRelative() throws Throwable {
- if (Build.VERSION.SDK_INT >= 16) {
- final AppCompatTextView autoSizeTextView = prepareAndRetrieveAutoSizeTestData(
- R.id.textview_autosize_uniform, false);
- final float initialTextSize = autoSizeTextView.getTextSize();
-
- mActivityTestRule.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- if (Build.VERSION.SDK_INT > 16) {
- autoSizeTextView.setPaddingRelative(
- autoSizeTextView.getWidth() / 3, autoSizeTextView.getHeight() / 3,
- autoSizeTextView.getWidth() / 3, autoSizeTextView.getHeight() / 3);
- }
- }
- });
- mInstrumentation.waitForIdleSync();
-
- assertTrue(autoSizeTextView.getTextSize() < initialTextSize);
- }
- }
-
- @Test
- public void testAutoSizeCallers_setTypeface() throws Throwable {
- final AppCompatTextView autoSizeTextView = prepareAndRetrieveAutoSizeTestData(
- R.id.textview_autosize_uniform, false);
- mActivityTestRule.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- autoSizeTextView.setText("The typeface change needs a bit more text then "
- + "the default used for this batch of tests in order to get to resize text."
- + " The resize function is always called but even with different typefaces "
- + "there may not be a need to resize text because it just fits. The longer "
- + "the text, the higher the chance for a resize. And here is yet another "
- + "sentence to make sure this test is not flaky. Not flaky at all.");
- }
- });
- mInstrumentation.waitForIdleSync();
- final float initialTextSize = autoSizeTextView.getTextSize();
-
- mActivityTestRule.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- Typeface differentTypeface = Typeface.MONOSPACE;
- if (autoSizeTextView.getTypeface() == Typeface.MONOSPACE) {
- differentTypeface = Typeface.SANS_SERIF;
- }
- autoSizeTextView.setTypeface(differentTypeface);
- }
- });
- mInstrumentation.waitForIdleSync();
- final float changedTextSize = autoSizeTextView.getTextSize();
-
- // Don't really know if it is larger or smaller (depends on the typeface chosen above),
- // but it should definitely have changed.
- assertNotEquals(initialTextSize, changedTextSize, 0f);
-
- mActivityTestRule.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- autoSizeTextView.setTypeface(autoSizeTextView.getTypeface());
- }
- });
- mInstrumentation.waitForIdleSync();
-
- assertEquals(changedTextSize, autoSizeTextView.getTextSize(), 0f);
- }
-
- @Test
- public void testAutoSizeCallers_setLetterSpacing() throws Throwable {
- if (Build.VERSION.SDK_INT >= 21) {
- final AppCompatTextView autoSizeTextView = prepareAndRetrieveAutoSizeTestData(
- R.id.textview_autosize_uniform, false);
- final float initialTextSize = autoSizeTextView.getTextSize();
-
- mActivityTestRule.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- if (Build.VERSION.SDK_INT >= 21) {
- autoSizeTextView.setLetterSpacing(
- autoSizeTextView.getLetterSpacing() * 1.5f + 4.5f);
- }
- }
- });
- mInstrumentation.waitForIdleSync();
- final float changedTextSize = autoSizeTextView.getTextSize();
-
- assertTrue(changedTextSize < initialTextSize);
-
- mActivityTestRule.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- if (Build.VERSION.SDK_INT >= 21) {
- autoSizeTextView.setLetterSpacing(autoSizeTextView.getLetterSpacing());
- }
- }
- });
- mInstrumentation.waitForIdleSync();
-
- assertEquals(changedTextSize, autoSizeTextView.getTextSize(), 0f);
- }
- }
-
- @Test
- public void testAutoSizeCallers_setMaxHeight() throws Throwable {
- final AppCompatTextView autoSizeTextView = prepareAndRetrieveAutoSizeTestData(
- R.id.textview_autosize_uniform, true);
- // Do not force exact height only.
- final LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
- 200,
- LinearLayout.LayoutParams.WRAP_CONTENT);
- mActivityTestRule.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- autoSizeTextView.setLayoutParams(layoutParams);
- }
- });
- mInstrumentation.waitForIdleSync();
- final float initialTextSize = autoSizeTextView.getTextSize();
- mActivityTestRule.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- autoSizeTextView.setMaxHeight(
- autoSizeTextView.getHeight() / 4);
- }
- });
- mInstrumentation.waitForIdleSync();
-
- assertTrue(autoSizeTextView.getTextSize() < initialTextSize);
- }
-
- @Test
- public void testAutoSizeCallers_setMaxWidth() throws Throwable {
- final AppCompatTextView autoSizeTextView = prepareAndRetrieveAutoSizeTestData(
- R.id.textview_autosize_uniform, true);
- // Do not force exact width only.
- final LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
- LinearLayout.LayoutParams.WRAP_CONTENT,
- 200);
- mActivityTestRule.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- autoSizeTextView.setLayoutParams(layoutParams);
- }
- });
- mInstrumentation.waitForIdleSync();
- final float initialTextSize = autoSizeTextView.getTextSize();
- mActivityTestRule.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- autoSizeTextView.setMaxWidth(
- autoSizeTextView.getWidth() / 4);
- }
- });
- mInstrumentation.waitForIdleSync();
-
- assertTrue(autoSizeTextView.getTextSize() != initialTextSize);
- }
-
- @Test
- public void testAutoSizeCallers_setWidth() throws Throwable {
- final AppCompatTextView autoSizeTextView = prepareAndRetrieveAutoSizeTestData(
- R.id.textview_autosize_uniform, true);
- // Do not force exact width only.
- final LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
- LinearLayout.LayoutParams.WRAP_CONTENT,
- 200);
- mActivityTestRule.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- autoSizeTextView.setLayoutParams(layoutParams);
- }
- });
- mInstrumentation.waitForIdleSync();
-
- final float initialTextSize = autoSizeTextView.getTextSize();
- mActivityTestRule.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- autoSizeTextView.setWidth(
- autoSizeTextView.getWidth() / 4);
- }
- });
- mInstrumentation.waitForIdleSync();
-
- assertTrue(autoSizeTextView.getTextSize() != initialTextSize);
- }
-
- @Test
- public void testAutoSizeCallers_setTextSizeIsNoOp() throws Throwable {
- final AppCompatTextView autoSizeTextView = prepareAndRetrieveAutoSizeTestData(
- R.id.textview_autosize_uniform, false);
- final float initialTextSize = autoSizeTextView.getTextSize();
-
- mActivityTestRule.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- autoSizeTextView.setTextSize(initialTextSize + 123f);
- }
- });
- mInstrumentation.waitForIdleSync();
-
- assertEquals(initialTextSize, autoSizeTextView.getTextSize(), 0f);
- }
-
- @Test
- public void testAutoSizeCallers_setHorizontallyScrolling() throws Throwable {
- final AppCompatTextView autoSizeTextView = prepareAndRetrieveAutoSizeTestData(
- R.id.textview_autosize_uniform, false);
- // Horizontal scrolling is expected to be deactivated for this test.
- final float initialTextSize = autoSizeTextView.getTextSize();
- mActivityTestRule.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- autoSizeTextView.setHorizontallyScrolling(true);
- }
- });
- mInstrumentation.waitForIdleSync();
- assertTrue(autoSizeTextView.getTextSize() > initialTextSize);
-
- mActivityTestRule.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- autoSizeTextView.setHorizontallyScrolling(false);
- }
- });
- mInstrumentation.waitForIdleSync();
- Assert.assertEquals(initialTextSize, autoSizeTextView.getTextSize(), 0f);
- }
-
- @Test
- public void testAutoSize_setEllipsize() throws Throwable {
- final AppCompatTextView textView = (AppCompatTextView) mActivity.findViewById(
- R.id.textview_autosize_uniform_predef_sizes);
- final int initialAutoSizeType = textView.getAutoSizeTextType();
- final int initialMinTextSize = textView.getAutoSizeMinTextSize();
- final int initialMaxTextSize = textView.getAutoSizeMaxTextSize();
- final int initialAutoSizeGranularity = textView.getAutoSizeStepGranularity();
- final int initialSizes = textView.getAutoSizeTextAvailableSizes().length;
-
- Assert.assertEquals(null, textView.getEllipsize());
- // Verify styled attributes.
- Assert.assertEquals(TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM, initialAutoSizeType);
- assertNotEquals(-1, initialMinTextSize);
- assertNotEquals(-1, initialMaxTextSize);
- // Because this TextView has been configured to use predefined sizes.
- Assert.assertEquals(-1, initialAutoSizeGranularity);
- assertNotEquals(0, initialSizes);
-
- final TextUtils.TruncateAt newEllipsizeValue = TextUtils.TruncateAt.END;
- mActivityTestRule.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- textView.setEllipsize(newEllipsizeValue);
- }
- });
- mInstrumentation.waitForIdleSync();
- Assert.assertEquals(newEllipsizeValue, textView.getEllipsize());
- // Beside the ellipsis no auto-size attribute has changed.
- Assert.assertEquals(initialAutoSizeType, textView.getAutoSizeTextType());
- Assert.assertEquals(initialMinTextSize, textView.getAutoSizeMinTextSize());
- Assert.assertEquals(initialMaxTextSize, textView.getAutoSizeMaxTextSize());
- Assert.assertEquals(initialAutoSizeGranularity, textView.getAutoSizeStepGranularity());
- Assert.assertEquals(initialSizes, textView.getAutoSizeTextAvailableSizes().length);
- }
-
- @Test
- public void testEllipsize_setAutoSize() throws Throwable {
- final AppCompatTextView textView =
- (AppCompatTextView) mActivity.findViewById(R.id.textview_text);
- final TextUtils.TruncateAt newEllipsizeValue = TextUtils.TruncateAt.END;
- mActivityTestRule.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- textView.setEllipsize(newEllipsizeValue);
- }
- });
- mInstrumentation.waitForIdleSync();
- Assert.assertEquals(newEllipsizeValue, textView.getEllipsize());
- Assert.assertEquals(TextViewCompat.AUTO_SIZE_TEXT_TYPE_NONE,
- textView.getAutoSizeTextType());
- Assert.assertEquals(-1, textView.getAutoSizeMinTextSize());
- Assert.assertEquals(-1, textView.getAutoSizeMaxTextSize());
- Assert.assertEquals(-1, textView.getAutoSizeStepGranularity());
- Assert.assertEquals(0, textView.getAutoSizeTextAvailableSizes().length);
-
- mActivityTestRule.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- textView.setAutoSizeTextTypeWithDefaults(
- TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM);
- }
- });
- mInstrumentation.waitForIdleSync();
- Assert.assertEquals(newEllipsizeValue, textView.getEllipsize());
- Assert.assertEquals(TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM,
- textView.getAutoSizeTextType());
- // The auto-size defaults have been used.
- assertNotEquals(-1, textView.getAutoSizeMinTextSize());
- assertNotEquals(-1, textView.getAutoSizeMaxTextSize());
- assertNotEquals(-1, textView.getAutoSizeStepGranularity());
- assertNotEquals(0, textView.getAutoSizeTextAvailableSizes().length);
- }
-
- @Test
- public void testAutoSizeUniform_obtainStyledAttributesUsingPredefinedSizes() {
- DisplayMetrics m = mActivity.getResources().getDisplayMetrics();
- final AppCompatTextView autoSizeTextViewUniform =
- (AppCompatTextView) mActivity.findViewById(
- R.id.textview_autosize_uniform_predef_sizes);
-
- // In arrays.xml predefined the step sizes as: 5px, 11dip, 19sp, 29pt, 43mm and 53in.
- // TypedValue can not use the math library and instead rounds the value by adding
- // 0.5f when obtaining styled attributes. Check TypedValue#complexToDimensionPixelSize(...)
- int[] expectedSizesInPx = new int[] {
- (int) (0.5f + TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PX, 5f, m)),
- (int) (0.5f + TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 11f, m)),
- (int) (0.5f + TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 19f, m)),
- (int) (0.5f + TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_PT, 29f, m)),
- (int) (0.5f + TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_MM, 43f, m)),
- (int) (0.5f + TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_IN, 53f, m))};
-
- boolean containsValueFromExpectedSizes = false;
- final int textSize = (int) autoSizeTextViewUniform.getTextSize();
- for (int i = 0; i < expectedSizesInPx.length; i++) {
- if (expectedSizesInPx[i] == textSize) {
- containsValueFromExpectedSizes = true;
- break;
- }
- }
- assertTrue(containsValueFromExpectedSizes);
- }
-
- @Test
- public void testAutoSizeUniform_obtainStyledAttributesPredefinedSizesFiltering() {
- AppCompatTextView autoSizeTextViewUniform = (AppCompatTextView) mActivity.findViewById(
- R.id.textview_autosize_uniform_predef_sizes_redundant_values);
-
- // In arrays.xml predefined the step sizes as: 40px, 10px, 10px, 10px, 0dp.
- final int[] expectedSizes = new int[] {10, 40};
- assertArrayEquals(expectedSizes, autoSizeTextViewUniform.getAutoSizeTextAvailableSizes());
- }
-
- @Test
- public void testAutoSizeUniform_predefinedSizesFilteringAndSorting() throws Throwable {
- final AppCompatTextView textView = (AppCompatTextView) mActivity.findViewById(
- R.id.textview_text);
- Assert.assertEquals(TextViewCompat.AUTO_SIZE_TEXT_TYPE_NONE,
- textView.getAutoSizeTextType());
-
- final int[] predefinedSizes = new int[] {400, 0, 10, 40, 10, 10, 0, 0};
- mActivityTestRule.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- textView.setAutoSizeTextTypeUniformWithPresetSizes(
- predefinedSizes, TypedValue.COMPLEX_UNIT_PX);
- }
- });
- mInstrumentation.waitForIdleSync();
- assertArrayEquals(new int[] {10, 40, 400}, textView.getAutoSizeTextAvailableSizes());
- }
-
- @Test(expected = NullPointerException.class)
- public void testAutoSizeUniform_predefinedSizesNullArray() throws Throwable {
- final AppCompatTextView textView = (AppCompatTextView) mActivity.findViewById(
- R.id.textview_text);
- Assert.assertEquals(TextViewCompat.AUTO_SIZE_TEXT_TYPE_NONE,
- textView.getAutoSizeTextType());
-
- final int[] predefinedSizes = null;
- mActivityTestRule.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- textView.setAutoSizeTextTypeUniformWithPresetSizes(
- predefinedSizes, TypedValue.COMPLEX_UNIT_PX);
- }
- });
- mInstrumentation.waitForIdleSync();
- }
-
- @Test
- public void testAutoSizeUniform_predefinedSizesEmptyArray() throws Throwable {
- final AppCompatTextView textView = (AppCompatTextView) mActivity.findViewById(
- R.id.textview_text);
- Assert.assertEquals(TextViewCompat.AUTO_SIZE_TEXT_TYPE_NONE,
- textView.getAutoSizeTextType());
-
- mActivityTestRule.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- textView.setAutoSizeTextTypeWithDefaults(
- TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM);
- }
- });
- mInstrumentation.waitForIdleSync();
-
- final int[] defaultSizes = textView.getAutoSizeTextAvailableSizes();
- assertNotNull(defaultSizes);
- assertTrue(defaultSizes.length > 0);
-
- final int[] predefinedSizes = new int[0];
- mActivityTestRule.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- textView.setAutoSizeTextTypeUniformWithPresetSizes(
- predefinedSizes, TypedValue.COMPLEX_UNIT_PX);
- }
- });
- mInstrumentation.waitForIdleSync();
-
- final int[] newSizes = textView.getAutoSizeTextAvailableSizes();
- assertNotNull(defaultSizes);
- assertArrayEquals(defaultSizes, newSizes);
- }
-
- @Test
- public void testAutoSizeUniform_buildsSizes() throws Throwable {
- final AppCompatTextView autoSizeTextViewUniform =
- (AppCompatTextView) mActivity.findViewById(
- R.id.textview_autosize_uniform);
-
- // Verify that the interval limits are both included.
- mActivityTestRule.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- autoSizeTextViewUniform
- .setAutoSizeTextTypeUniformWithConfiguration(10, 20, 2,
- TypedValue.COMPLEX_UNIT_PX);
- }
- });
- mInstrumentation.waitForIdleSync();
- assertArrayEquals(
- new int[] {10, 12, 14, 16, 18, 20},
- autoSizeTextViewUniform.getAutoSizeTextAvailableSizes());
-
- mActivityTestRule.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- autoSizeTextViewUniform
- .setAutoSizeTextTypeUniformWithConfiguration(
- autoSizeTextViewUniform.getAutoSizeMinTextSize(),
- 19,
- autoSizeTextViewUniform.getAutoSizeStepGranularity(),
- TypedValue.COMPLEX_UNIT_PX);
- }
- });
- mInstrumentation.waitForIdleSync();
- assertArrayEquals(
- new int[] {10, 12, 14, 16, 18},
- autoSizeTextViewUniform.getAutoSizeTextAvailableSizes());
-
- mActivityTestRule.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- autoSizeTextViewUniform
- .setAutoSizeTextTypeUniformWithConfiguration(
- autoSizeTextViewUniform.getAutoSizeMinTextSize(),
- 21,
- autoSizeTextViewUniform.getAutoSizeStepGranularity(),
- TypedValue.COMPLEX_UNIT_PX);
- }
- });
- mInstrumentation.waitForIdleSync();
- assertArrayEquals(
- new int[] {10, 12, 14, 16, 18, 20},
- autoSizeTextViewUniform.getAutoSizeTextAvailableSizes());
- }
-
- @Test
- public void testAutoSizeUniform_getSetAutoSizeTextDefaults() {
- final AppCompatTextView textView = new AppCompatTextView(mActivity);
- Assert.assertEquals(TextViewCompat.AUTO_SIZE_TEXT_TYPE_NONE,
- textView.getAutoSizeTextType());
- // Min/Max/Granularity values for auto-sizing are 0 because they are not used.
- Assert.assertEquals(-1, textView.getAutoSizeMinTextSize());
- Assert.assertEquals(-1, textView.getAutoSizeMaxTextSize());
- Assert.assertEquals(-1, textView.getAutoSizeStepGranularity());
-
- textView.setAutoSizeTextTypeWithDefaults(TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM);
- Assert.assertEquals(TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM,
- textView.getAutoSizeTextType());
- // Min/Max default values for auto-sizing XY have been loaded.
- final int minSize = textView.getAutoSizeMinTextSize();
- final int maxSize = textView.getAutoSizeMaxTextSize();
- assertTrue(0 < minSize);
- assertTrue(minSize < maxSize);
- assertNotEquals(0, textView.getAutoSizeStepGranularity());
-
- textView.setAutoSizeTextTypeWithDefaults(TextViewCompat.AUTO_SIZE_TEXT_TYPE_NONE);
- Assert.assertEquals(TextViewCompat.AUTO_SIZE_TEXT_TYPE_NONE,
- textView.getAutoSizeTextType());
- // Min/Max values for auto-sizing XY have been cleared.
- Assert.assertEquals(-1, textView.getAutoSizeMinTextSize());
- Assert.assertEquals(-1, textView.getAutoSizeMaxTextSize());
- Assert.assertEquals(-1, textView.getAutoSizeStepGranularity());
- }
-
- @Test
- public void testAutoSizeUniform_getSetAutoSizeStepGranularity() {
- final AppCompatTextView textView = new AppCompatTextView(mActivity);
- Assert.assertEquals(TextViewCompat.AUTO_SIZE_TEXT_TYPE_NONE,
- textView.getAutoSizeTextType());
- final int initialValue = -1;
- Assert.assertEquals(initialValue, textView.getAutoSizeStepGranularity());
-
- textView.setAutoSizeTextTypeWithDefaults(TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM);
- Assert.assertEquals(TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM,
- textView.getAutoSizeTextType());
- final int defaultValue = 1; // 1px.
- // If the auto-size type is AUTO_SIZE_TEXT_TYPE_UNIFORM then it means textView went through
- // the auto-size setup and given that 0 is an invalid value it changed it to the default.
- Assert.assertEquals(defaultValue, textView.getAutoSizeStepGranularity());
-
- final int newValue = 33;
- textView.setAutoSizeTextTypeUniformWithConfiguration(
- textView.getAutoSizeMinTextSize(),
- textView.getAutoSizeMaxTextSize(),
- newValue,
- TypedValue.COMPLEX_UNIT_PX);
- Assert.assertEquals(newValue, textView.getAutoSizeStepGranularity());
- }
-
- @Test
- public void testAutoSizeUniform_getSetAutoSizeMinTextSize() {
- final AppCompatTextView textView = new AppCompatTextView(mActivity);
- textView.setAutoSizeTextTypeWithDefaults(TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM);
- Assert.assertEquals(TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM,
- textView.getAutoSizeTextType());
- final int minSize = textView.getAutoSizeMinTextSize();
- assertNotEquals(0, minSize);
- final int maxSize = textView.getAutoSizeMaxTextSize();
- assertNotEquals(0, maxSize);
-
- // This is just a test check to verify the next assertions. If this fails it is a problem
- // of this test setup (we need at least 2 units).
- assertTrue((maxSize - minSize) > 1);
- final int newMinSize = maxSize - 1;
- textView.setAutoSizeTextTypeUniformWithConfiguration(
- newMinSize,
- textView.getAutoSizeMaxTextSize(),
- textView.getAutoSizeStepGranularity(),
- TypedValue.COMPLEX_UNIT_PX);
-
- Assert.assertEquals(newMinSize, textView.getAutoSizeMinTextSize());
- // Max size has not changed.
- Assert.assertEquals(maxSize, textView.getAutoSizeMaxTextSize());
-
- textView.setAutoSizeTextTypeUniformWithConfiguration(
- newMinSize,
- newMinSize + 10,
- textView.getAutoSizeStepGranularity(),
- TypedValue.COMPLEX_UNIT_SP);
-
- // It does not matter which unit has been used to set the min size, the getter always
- // returns it in pixels.
- Assert.assertEquals(Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
- newMinSize, mActivity.getResources().getDisplayMetrics())),
- textView.getAutoSizeMinTextSize());
- }
-
- @Test(expected = IllegalArgumentException.class)
- public void testAutoSizeUniform_throwsException_whenMaxLessThanMin() {
- final AppCompatTextView textView = new AppCompatTextView(mActivity);
- textView.setAutoSizeTextTypeUniformWithConfiguration(
- 10, 9, 1, TypedValue.COMPLEX_UNIT_SP);
- }
-
- @Test(expected = IllegalArgumentException.class)
- public void testAutoSizeUniform_throwsException_minLessThanZero() {
- final AppCompatTextView textView = new AppCompatTextView(mActivity);
- textView.setAutoSizeTextTypeUniformWithConfiguration(
- -1, 9, 1, TypedValue.COMPLEX_UNIT_SP);
- }
-
- @Test(expected = IllegalArgumentException.class)
- public void testAutoSizeUniform_throwsException_maxLessThanZero() {
- final AppCompatTextView textView = new AppCompatTextView(mActivity);
- textView.setAutoSizeTextTypeUniformWithConfiguration(
- 10, -1, 1, TypedValue.COMPLEX_UNIT_SP);
- }
-
- @Test(expected = IllegalArgumentException.class)
- public void testAutoSizeUniform_throwsException_granularityLessThanZero() {
- final AppCompatTextView textView = new AppCompatTextView(mActivity);
- textView.setAutoSizeTextTypeUniformWithConfiguration(
- 10, 20, -1, TypedValue.COMPLEX_UNIT_SP);
- }
-
- @Test
- public void testAutoSizeUniform_getSetAutoSizeMaxTextSize() {
- final AppCompatTextView textView = new AppCompatTextView(mActivity);
- textView.setAutoSizeTextTypeWithDefaults(TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM);
- Assert.assertEquals(TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM,
- textView.getAutoSizeTextType());
- final int minSize = textView.getAutoSizeMinTextSize();
- assertNotEquals(0, minSize);
- final int maxSize = textView.getAutoSizeMaxTextSize();
- assertNotEquals(0, maxSize);
-
- final int newMaxSize = maxSize + 11;
- textView.setAutoSizeTextTypeUniformWithConfiguration(
- textView.getAutoSizeMinTextSize(),
- newMaxSize,
- textView.getAutoSizeStepGranularity(),
- TypedValue.COMPLEX_UNIT_PX);
-
- Assert.assertEquals(newMaxSize, textView.getAutoSizeMaxTextSize());
- // Min size has not changed.
- Assert.assertEquals(minSize, textView.getAutoSizeMinTextSize());
- textView.setAutoSizeTextTypeUniformWithConfiguration(
- textView.getAutoSizeMinTextSize(),
- newMaxSize,
- textView.getAutoSizeStepGranularity(),
- TypedValue.COMPLEX_UNIT_SP);
- // It does not matter which unit has been used to set the max size, the getter always
- // returns it in pixels.
- Assert.assertEquals(Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,
- newMaxSize, mActivity.getResources().getDisplayMetrics())),
- textView.getAutoSizeMaxTextSize());
- }
-
- @Test
- public void testAutoSizeUniform_autoSizeCalledWhenTypeChanged() throws Throwable {
- final AppCompatTextView textView = (AppCompatTextView) mActivity.findViewById(
- R.id.textview_text);
- // Make sure we pick an already inflated non auto-sized text view.
- Assert.assertEquals(TextViewCompat.AUTO_SIZE_TEXT_TYPE_NONE,
- textView.getAutoSizeTextType());
- // Set the text size to a very low value in order to prepare for auto-size.
- final int customTextSize = 3;
- mActivityTestRule.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, customTextSize);
- }
- });
- mInstrumentation.waitForIdleSync();
- Assert.assertEquals(customTextSize, textView.getTextSize(), 0f);
- mActivityTestRule.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- textView.setAutoSizeTextTypeWithDefaults(
- TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM);
- }
- });
- mInstrumentation.waitForIdleSync();
- // The size of the text should have changed.
- assertNotEquals(customTextSize, textView.getTextSize(), 0f);
- }
-
- @Test
- public void testAutoSizeCallers_setTextSizeChangesSizeWhenAutoSizeDisabled() throws Throwable {
- final AppCompatTextView textView = (AppCompatTextView) prepareAndRetrieveAutoSizeTestData(
- R.id.textview_autosize_uniform, false);
- // Disable auto-size.
- mActivityTestRule.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- textView.setAutoSizeTextTypeWithDefaults(TextViewCompat.AUTO_SIZE_TEXT_TYPE_NONE);
- }
- });
- mInstrumentation.waitForIdleSync();
- final float newTextSizeInPx = 123f;
- assertNotEquals(newTextSizeInPx, textView.getTextSize(), 0f);
-
- mActivityTestRule.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- textView.setTextSize(TypedValue.COMPLEX_UNIT_PX, newTextSizeInPx);
- }
- });
- mInstrumentation.waitForIdleSync();
-
- assertEquals(newTextSizeInPx, textView.getTextSize(), 0f);
- }
-
- /**
- * Some TextView attributes require non-fixed width and/or layout height. This function removes
- * all other existing views from the layout leaving only one auto-size TextView (for exercising
- * the auto-size behavior) which has been set up to suit the test needs.
- *
- * @param viewId The id of the view to prepare.
- * @param shouldWrapLayoutContent Specifies if the layout params should wrap content
- *
- * @return a TextView configured for auto size tests.
- */
- private AppCompatTextView prepareAndRetrieveAutoSizeTestData(final int viewId,
- final boolean shouldWrapLayoutContent) throws Throwable {
- mActivityTestRule.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- LinearLayout ll = (LinearLayout) mActivity.findViewById(
- android.support.v7.appcompat.test.R.id.layout_textviewtest);
- AppCompatTextView targetedTextView =
- (AppCompatTextView) mActivity.findViewById(viewId);
- ll.removeAllViews();
- ll.addView(targetedTextView);
- }
- });
- mInstrumentation.waitForIdleSync();
-
- final AppCompatTextView textView = mActivity.findViewById(viewId);
- if (shouldWrapLayoutContent) {
- // Do not force exact width or height.
- final LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
- LinearLayout.LayoutParams.WRAP_CONTENT,
- LinearLayout.LayoutParams.WRAP_CONTENT);
- mActivityTestRule.runOnUiThread(new Runnable() {
- @Override
- public void run() {
- textView.setLayoutParams(layoutParams);
- }
- });
- mInstrumentation.waitForIdleSync();
- }
-
- return textView;
- }
-
- @Test
public void testAutoSizeWithMaxLines_shouldNotThrowException() throws Throwable {
// the layout contains an instance of CustomTextViewWithTransformationMethod
final AppCompatTextView textView = (AppCompatTextView) mActivity
@@ -1179,7 +84,7 @@
if (Build.VERSION.SDK_INT >= 16) {
assertEquals(1, textView.getMaxLines());
}
- assertEquals(TextView.AUTO_SIZE_TEXT_TYPE_UNIFORM, textView.getAutoSizeTextType());
+ assertEquals(TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM, textView.getAutoSizeTextType());
assertTrue(textView.getTransformationMethod() instanceof SingleLineTransformationMethod);
}