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

Commit 3d3e69ce authored by Diego Perez's avatar Diego Perez
Browse files

Fix BridgeContext.resolveThemeAttribute color parsing

When passed a color, resolveThemeAttribute was not parsing the value
into the .data field.

Bug: http://b.android.com/232870
Test: Added new test
Change-Id: I9d91e3a70c24f1e71d9d89977fe79b0965e98893
parent 6fd0e8d5
Loading
Loading
Loading
Loading
+14 −1
Original line number Diff line number Diff line
@@ -65,6 +65,7 @@ import android.database.DatabaseErrorHandler;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.hardware.display.DisplayManager;
import android.net.Uri;
@@ -381,6 +382,18 @@ public class BridgeContext extends Context {
            return true;
        }

        String stringValue = value.getValue();
        if (!stringValue.isEmpty()) {
            if (stringValue.charAt(0) == '#') {
                outValue.type = TypedValue.TYPE_INT_COLOR_ARGB8;
                outValue.data = Color.parseColor(value.getValue());
            }
            else if (stringValue.charAt(0) == '@') {
                outValue.type = TypedValue.TYPE_REFERENCE;
            }

        }

        int a;
        // if this is a framework value.
        if (value.isFramework()) {
@@ -399,7 +412,7 @@ public class BridgeContext extends Context {
        }

        // If the value is not a valid reference, fallback to pass the value as a string.
        outValue.string = value.getValue();
        outValue.string = stringValue;
        return true;
    }

+34 −0
Original line number Diff line number Diff line
@@ -36,6 +36,7 @@ import android.content.res.AssetManager;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.util.DisplayMetrics;
import android.util.TypedValue;

import java.lang.reflect.Field;
import java.util.concurrent.TimeUnit;
@@ -43,6 +44,7 @@ import java.util.concurrent.TimeUnit;
import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

@@ -334,6 +336,7 @@ public class RenderTests extends RenderTestBase {
        AssetManager assetManager = AssetManager.getSystem();
        DisplayMetrics metrics = new DisplayMetrics();
        Configuration configuration = RenderAction.getConfiguration(params);
        //noinspection deprecation
        Resources resources = new Resources(assetManager, metrics, configuration);
        resources.mLayoutlibCallback = params.getLayoutlibCallback();
        resources.mContext =
@@ -370,6 +373,7 @@ public class RenderTests extends RenderTestBase {
        AssetManager assetManager = AssetManager.getSystem();
        DisplayMetrics metrics = new DisplayMetrics();
        Configuration configuration = RenderAction.getConfiguration(params);
        //noinspection deprecation
        Resources resources = new Resources(assetManager, metrics, configuration);
        resources.mLayoutlibCallback = params.getLayoutlibCallback();
        resources.mContext =
@@ -390,4 +394,34 @@ public class RenderTests extends RenderTestBase {
        // TODO: styles seem to be broken in TextView
        renderAndVerify("fonts_test.xml", "font_test.png");
    }

    @Test
    public void testColorTypedValue() throws Exception {
        // Setup
        // Create the layout pull parser for our resources (empty.xml can not be part of the test
        // app as it won't compile).
        LayoutPullParser parser = new LayoutPullParser("/empty.xml");
        // Create LayoutLibCallback.
        LayoutLibTestCallback layoutLibCallback =
                new LayoutLibTestCallback(RenderTestBase.getLogger(), mDefaultClassLoader);
        layoutLibCallback.initResources();
        SessionParams params = getSessionParams(parser, ConfigGenerator.NEXUS_4,
                layoutLibCallback, "AppTheme", true, RenderingMode.NORMAL, 22);
        AssetManager assetManager = AssetManager.getSystem();
        DisplayMetrics metrics = new DisplayMetrics();
        Configuration configuration = RenderAction.getConfiguration(params);
        //noinspection deprecation
        Resources resources = new Resources(assetManager, metrics, configuration);
        resources.mLayoutlibCallback = params.getLayoutlibCallback();
        resources.mContext =
                new BridgeContext(params.getProjectKey(), metrics, params.getResources(),
                        params.getAssets(), params.getLayoutlibCallback(), configuration,
                        params.getTargetSdkVersion(), params.isRtlSupported());

        TypedValue outValue = new TypedValue();
        resources.mContext.resolveThemeAttribute(android.R.attr.colorPrimary, outValue, true);
        assertEquals(TypedValue.TYPE_INT_COLOR_ARGB8, outValue.type);
        assertNotEquals(0, outValue.data);
        assertTrue(sRenderMessages.isEmpty());
    }
}