Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 26047899 authored by Ziqi Chen's avatar Ziqi Chen Committed by Automerger Merge Worker
Browse files

Merge "Fix TextAppearanceInfo#getTextColor is wrong when there is a hint text"...

Merge "Fix TextAppearanceInfo#getTextColor is wrong when there is a hint text" into udc-qpr-dev am: 77ccd8bc

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/23440926



Change-Id: I08dc26c49174d96e3426050f56575e53702ee489
Signed-off-by: default avatarAutomerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
parents d23cf1ef 77ccd8bc
Loading
Loading
Loading
Loading
+4 −1
Original line number Original line Diff line number Diff line
@@ -238,7 +238,10 @@ public final class TextAppearanceInfo implements Parcelable {
                .setFontFeatureSettings(textPaint.getFontFeatureSettings())
                .setFontFeatureSettings(textPaint.getFontFeatureSettings())
                .setFontVariationSettings(textPaint.getFontVariationSettings())
                .setFontVariationSettings(textPaint.getFontVariationSettings())
                .setTextScaleX(textPaint.getTextScaleX())
                .setTextScaleX(textPaint.getTextScaleX())
                .setTextColor(textPaint.getColor())
                // When there is a hint text (text length is 0), the text color should be the normal
                // text color rather than hint text color.
                .setTextColor(text.length() == 0
                        ? textView.getCurrentTextColor() : textPaint.getColor())
                .setLinkTextColor(textPaint.linkColor)
                .setLinkTextColor(textPaint.linkColor)
                .setAllCaps(textView.isAllCaps())
                .setAllCaps(textView.isAllCaps())
                .setFallbackLineSpacing(textView.isFallbackLineSpacing())
                .setFallbackLineSpacing(textView.isFallbackLineSpacing())
+49 −34
Original line number Original line Diff line number Diff line
@@ -37,6 +37,7 @@ import android.text.style.ForegroundColorSpan;
import android.text.style.ScaleXSpan;
import android.text.style.ScaleXSpan;
import android.text.style.StyleSpan;
import android.text.style.StyleSpan;
import android.text.style.TypefaceSpan;
import android.text.style.TypefaceSpan;
import android.text.util.Linkify;
import android.view.ViewGroup;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.EditText;


@@ -53,7 +54,7 @@ import org.junit.runner.RunWith;
@RunWith(AndroidJUnit4.class)
@RunWith(AndroidJUnit4.class)
public class TextAppearanceInfoTest {
public class TextAppearanceInfoTest {
    private static final float EPSILON = 0.0000001f;
    private static final float EPSILON = 0.0000001f;
    private static final String TEST_TEXT = "Happy birthday!";
    private static final String TEST_TEXT = "Hello: google.com";
    private static final float TEXT_SIZE = 16.5f;
    private static final float TEXT_SIZE = 16.5f;
    private static final LocaleList TEXT_LOCALES = LocaleList.forLanguageTags("en,ja");
    private static final LocaleList TEXT_LOCALES = LocaleList.forLanguageTags("en,ja");
    private static final String FONT_FAMILY_NAME = "sans-serif";
    private static final String FONT_FAMILY_NAME = "sans-serif";
@@ -84,39 +85,7 @@ public class TextAppearanceInfoTest {


    @Before
    @Before
    public void setUp() {
    public void setUp() {
        mEditText.setText(mSpannableText);
        initEditText(mSpannableText);
        mEditText.getPaint().setTextSize(TEXT_SIZE);
        mEditText.setTextLocales(TEXT_LOCALES);
        Typeface family = Typeface.create(FONT_FAMILY_NAME, Typeface.NORMAL);
        mEditText.setTypeface(
                Typeface.create(family, TEXT_WEIGHT, (TEXT_STYLE & Typeface.ITALIC) != 0));
        mEditText.setAllCaps(ALL_CAPS);
        mEditText.setShadowLayer(SHADOW_RADIUS, SHADOW_DX, SHADOW_DY, SHADOW_COLOR);
        mEditText.setElegantTextHeight(ELEGANT_TEXT_HEIGHT);
        mEditText.setFallbackLineSpacing(FALLBACK_LINE_SPACING);
        mEditText.setLetterSpacing(LETTER_SPACING);
        mEditText.setFontFeatureSettings(FONT_FEATURE_SETTINGS);
        mEditText.setFontVariationSettings(FONT_VARIATION_SETTINGS);
        mEditText.setLineBreakStyle(LINE_BREAK_STYLE);
        mEditText.setLineBreakWordStyle(LINE_BREAK_WORD_STYLE);
        mEditText.setTextScaleX(TEXT_SCALEX);
        mEditText.setHighlightColor(HIGHLIGHT_TEXT_COLOR);
        mEditText.setTextColor(TEXT_COLOR);
        mEditText.setHintTextColor(HINT_TEXT_COLOR);
        mEditText.setLinkTextColor(LINK_TEXT_COLOR);
        ViewGroup.LayoutParams params =
                new ViewGroup.LayoutParams(
                        ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        mEditText.setLayoutParams(params);
        mEditText.measure(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        Bitmap bitmap =
                Bitmap.createBitmap(
                        Math.max(1, mEditText.getMeasuredWidth()),
                        Math.max(1, mEditText.getMeasuredHeight()),
                        Bitmap.Config.ARGB_8888);
        mEditText.layout(0, 0, mEditText.getMeasuredWidth(), mEditText.getMeasuredHeight());
        mCanvas = new Canvas(bitmap);
        mEditText.draw(mCanvas);
    }
    }


    @Test
    @Test
@@ -233,6 +202,43 @@ public class TextAppearanceInfoTest {
        assertEquals(info1.getSystemFontFamilyName(), FONT_FAMILY_NAME);
        assertEquals(info1.getSystemFontFamilyName(), FONT_FAMILY_NAME);
    }
    }


    private void initEditText(CharSequence text) {
        mEditText.setText(text);
        mEditText.getPaint().setTextSize(TEXT_SIZE);
        mEditText.setTextLocales(TEXT_LOCALES);
        Typeface family = Typeface.create(FONT_FAMILY_NAME, Typeface.NORMAL);
        mEditText.setTypeface(
                Typeface.create(family, TEXT_WEIGHT, (TEXT_STYLE & Typeface.ITALIC) != 0));
        mEditText.setAllCaps(ALL_CAPS);
        mEditText.setShadowLayer(SHADOW_RADIUS, SHADOW_DX, SHADOW_DY, SHADOW_COLOR);
        mEditText.setElegantTextHeight(ELEGANT_TEXT_HEIGHT);
        mEditText.setFallbackLineSpacing(FALLBACK_LINE_SPACING);
        mEditText.setLetterSpacing(LETTER_SPACING);
        mEditText.setFontFeatureSettings(FONT_FEATURE_SETTINGS);
        mEditText.setFontVariationSettings(FONT_VARIATION_SETTINGS);
        mEditText.setLineBreakStyle(LINE_BREAK_STYLE);
        mEditText.setLineBreakWordStyle(LINE_BREAK_WORD_STYLE);
        mEditText.setTextScaleX(TEXT_SCALEX);
        mEditText.setHighlightColor(HIGHLIGHT_TEXT_COLOR);
        mEditText.setTextColor(TEXT_COLOR);
        mEditText.setHintTextColor(HINT_TEXT_COLOR);
        mEditText.setHint("Hint text");
        mEditText.setLinkTextColor(LINK_TEXT_COLOR);
        mEditText.setAutoLinkMask(Linkify.WEB_URLS);
        ViewGroup.LayoutParams params =
                new ViewGroup.LayoutParams(
                        ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        mEditText.setLayoutParams(params);
        mEditText.measure(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        Bitmap bitmap =
                Bitmap.createBitmap(
                        Math.max(1, mEditText.getMeasuredWidth()),
                        Math.max(1, mEditText.getMeasuredHeight()),
                        Bitmap.Config.ARGB_8888);
        mEditText.layout(0, 0, mEditText.getMeasuredWidth(), mEditText.getMeasuredHeight());
        mCanvas = new Canvas(bitmap);
        mEditText.draw(mCanvas);
    }
    private void assertTextAppearanceInfoContentsEqual(TextAppearanceInfo textAppearanceInfo) {
    private void assertTextAppearanceInfoContentsEqual(TextAppearanceInfo textAppearanceInfo) {
        assertEquals(textAppearanceInfo.getTextSize(), TEXT_SIZE, EPSILON);
        assertEquals(textAppearanceInfo.getTextSize(), TEXT_SIZE, EPSILON);
        assertEquals(textAppearanceInfo.getTextLocales(), TEXT_LOCALES);
        assertEquals(textAppearanceInfo.getTextLocales(), TEXT_LOCALES);
@@ -258,6 +264,15 @@ public class TextAppearanceInfoTest {
        assertEquals(textAppearanceInfo.getLinkTextColor(), LINK_TEXT_COLOR);
        assertEquals(textAppearanceInfo.getLinkTextColor(), LINK_TEXT_COLOR);
    }
    }


    @Test
    public void testCreateFromTextView_withHintText() {
        // Make hint text display
        initEditText("");

        // The text color should not be hint color
        assertTextAppearanceInfoContentsEqual(TextAppearanceInfo.createFromTextView(mEditText));
    }

    static class CustomForegroundColorSpan extends ForegroundColorSpan {
    static class CustomForegroundColorSpan extends ForegroundColorSpan {
        @Nullable public TextPaint lastTextPaint = null;
        @Nullable public TextPaint lastTextPaint = null;