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

Commit 724a38ab authored by Dmitri Plotnikov's avatar Dmitri Plotnikov
Browse files

Enable PowerProfile tests in Ravenwood

Bug: 314797745
Test: atest --host FrameworksCoreTestsRavenwood PowerStatsTestsRavenwood
Test: atest --host FrameworksCoreTests PowerStatsTests
Change-Id: I405c7d26bdbee48e42eb2f3f774246d80ce8a186
parent ab14d225
Loading
Loading
Loading
Loading
+35 −7
Original line number Diff line number Diff line
@@ -18,6 +18,7 @@ package com.android.internal.os;


import android.annotation.LongDef;
import android.annotation.Nullable;
import android.annotation.StringDef;
import android.annotation.XmlRes;
import android.compat.annotation.UnsupportedAppUsage;
@@ -352,19 +353,39 @@ public class PowerProfile {
     * WARNING: use only for testing!
     */
    @VisibleForTesting
    public void forceInitForTesting(Context context, @XmlRes int xmlId) {
    public void initForTesting(XmlPullParser parser) {
        initForTesting(parser, null);
    }

    /**
     * Reinitialize the PowerProfile with the provided XML, using optional Resources for fallback
     * configuration settings.
     * WARNING: use only for testing!
     */
    @VisibleForTesting
    public void initForTesting(XmlPullParser parser, @Nullable Resources resources) {
        synchronized (sLock) {
            sPowerItemMap.clear();
            sPowerArrayMap.clear();
            sModemPowerProfile.clear();
            initLocked(context, xmlId);

            try {
                readPowerValuesFromXml(parser, resources);
            } finally {
                if (parser instanceof XmlResourceParser) {
                    ((XmlResourceParser) parser).close();
                }
            }
            initLocked();
        }
    }

    @GuardedBy("sLock")
    private void initLocked(Context context, @XmlRes int xmlId) {
        if (sPowerItemMap.size() == 0 && sPowerArrayMap.size() == 0) {
            readPowerValuesFromXml(context, xmlId);
            final Resources resources = context.getResources();
            XmlResourceParser parser = resources.getXml(xmlId);
            readPowerValuesFromXml(parser, resources);
        }
        initLocked();
    }
@@ -377,9 +398,8 @@ public class PowerProfile {
        initModem();
    }

    private void readPowerValuesFromXml(Context context, @XmlRes int xmlId) {
        final Resources resources = context.getResources();
        XmlResourceParser parser = resources.getXml(xmlId);
    private static void readPowerValuesFromXml(XmlPullParser parser,
            @Nullable Resources resources) {
        boolean parsingArray = false;
        ArrayList<Double> array = new ArrayList<>();
        String arrayName = null;
@@ -430,9 +450,17 @@ public class PowerProfile {
        } catch (IOException e) {
            throw new RuntimeException(e);
        } finally {
            parser.close();
            if (parser instanceof XmlResourceParser) {
                ((XmlResourceParser) parser).close();
            }
        }

        if (resources != null) {
            getDefaultValuesFromConfig(resources);
        }
    }

    private static void getDefaultValuesFromConfig(Resources resources) {
        // Now collect other config variables.
        int[] configResIds = new int[]{
                com.android.internal.R.integer.config_bluetooth_idle_cur_ma,
+3 −4
Original line number Diff line number Diff line
@@ -17,7 +17,6 @@
package com.android.internal.power;

import android.annotation.IntDef;
import android.content.res.XmlResourceParser;
import android.telephony.ModemActivityInfo;
import android.telephony.ServiceState;
import android.telephony.TelephonyManager;
@@ -256,7 +255,7 @@ public class ModemPowerProfile {
    /**
     * Generates a ModemPowerProfile object from the <modem /> element of a power_profile.xml
     */
    public void parseFromXml(XmlResourceParser parser) throws IOException,
    public void parseFromXml(XmlPullParser parser) throws IOException,
            XmlPullParserException {
        final int depth = parser.getDepth();
        while (XmlUtils.nextElementWithin(parser, depth)) {
@@ -286,7 +285,7 @@ public class ModemPowerProfile {
    }

    /** Parse the <active /> XML element */
    private void parseActivePowerConstantsFromXml(XmlResourceParser parser)
    private void parseActivePowerConstantsFromXml(XmlPullParser parser)
            throws IOException, XmlPullParserException {
        // Parse attributes to get the type of active modem usage the power constants are for.
        final int ratType;
@@ -339,7 +338,7 @@ public class ModemPowerProfile {
        }
    }

    private static int getTypeFromAttribute(XmlResourceParser parser, String attr,
    private static int getTypeFromAttribute(XmlPullParser parser, String attr,
            SparseArray<String> names) {
        final String value = XmlUtils.readStringAttribute(parser, attr);
        if (value == null) {
+5 −0
Original line number Diff line number Diff line
@@ -267,5 +267,10 @@ android_ravenwood_test {
        generate_get_transaction_name: true,
        local_include_dirs: ["aidl"],
    },
    java_resources: [
        "res/xml/power_profile_test.xml",
        "res/xml/power_profile_test_cpu_legacy.xml",
        "res/xml/power_profile_test_modem.xml",
    ],
    auto_gen_config: true,
}
+13 −1
Original line number Diff line number Diff line
@@ -98,4 +98,16 @@
        <value>40</value>
        <value>50</value>
    </array>

    <!-- Idle current for bluetooth in mA.-->
    <item name="bluetooth.controller.idle">0.02</item>

    <!-- Rx current for bluetooth in mA.-->
    <item name="bluetooth.controller.rx">3</item>

    <!-- Tx current for bluetooth in mA-->
    <item name="bluetooth.controller.tx">5</item>

    <!-- Operating voltage for bluetooth in mV.-->
    <item name="bluetooth.controller.voltage">3300</item>
</device>
+61 −18
Original line number Diff line number Diff line
@@ -21,20 +21,21 @@ import static com.android.internal.os.PowerProfile.POWER_GROUP_DISPLAY_AMBIENT;
import static com.android.internal.os.PowerProfile.POWER_GROUP_DISPLAY_SCREEN_FULL;
import static com.android.internal.os.PowerProfile.POWER_GROUP_DISPLAY_SCREEN_ON;

import static com.google.common.truth.Truth.assertThat;

import static org.junit.Assert.fail;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import android.annotation.XmlRes;
import android.content.Context;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.platform.test.annotations.IgnoreUnderRavenwood;
import android.platform.test.annotations.DisabledOnRavenwood;
import android.platform.test.ravenwood.RavenwoodRule;
import android.util.Xml;

import androidx.test.InstrumentationRegistry;
import androidx.test.filters.SmallTest;
import androidx.test.runner.AndroidJUnit4;

import com.android.frameworks.coretests.R;
import com.android.internal.power.ModemPowerProfile;
import com.android.internal.util.XmlUtils;

@@ -43,6 +44,11 @@ import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;

import java.io.IOException;
import java.io.StringReader;

/*
 * Keep this file in sync with frameworks/base/core/res/res/xml/power_profile_test.xml and
@@ -53,7 +59,6 @@ import org.junit.runner.RunWith;
 */
@SmallTest
@RunWith(AndroidJUnit4.class)
@IgnoreUnderRavenwood(blockedBy = PowerProfile.class)
public class PowerProfileTest {
    @Rule
    public final RavenwoodRule mRavenwood = new RavenwoodRule();
@@ -62,17 +67,15 @@ public class PowerProfileTest {
    static final String ATTR_NAME = "name";

    private PowerProfile mProfile;
    private Context mContext;

    @Before
    public void setUp() {
        mContext = InstrumentationRegistry.getContext();
        mProfile = new PowerProfile(mContext);
        mProfile = new PowerProfile();
    }

    @Test
    public void testPowerProfile() {
        mProfile.forceInitForTesting(mContext, R.xml.power_profile_test);
        mProfile.initForTesting(resolveParser("power_profile_test"));

        assertEquals(5.0, mProfile.getAveragePower(PowerProfile.POWER_CPU_SUSPEND));
        assertEquals(1.11, mProfile.getAveragePower(PowerProfile.POWER_CPU_IDLE));
@@ -127,11 +130,36 @@ public class PowerProfileTest {
                PowerProfile.SUBSYSTEM_MODEM | ModemPowerProfile.MODEM_RAT_TYPE_DEFAULT
                        | ModemPowerProfile.MODEM_DRAIN_TYPE_TX
                        | ModemPowerProfile.MODEM_TX_LEVEL_4));

        assertEquals(0.02, mProfile.getAveragePower(PowerProfile.POWER_BLUETOOTH_CONTROLLER_IDLE));
        assertEquals(3, mProfile.getAveragePower(PowerProfile.POWER_BLUETOOTH_CONTROLLER_RX));
        assertEquals(5, mProfile.getAveragePower(PowerProfile.POWER_BLUETOOTH_CONTROLLER_TX));
        assertEquals(3300, mProfile.getAveragePower(
                PowerProfile.POWER_BLUETOOTH_CONTROLLER_OPERATING_VOLTAGE));
    }

    @DisabledOnRavenwood
    @Test
    public void configDefaults() throws XmlPullParserException {
        Resources mockResources = mock(Resources.class);
        when(mockResources.getInteger(com.android.internal.R.integer.config_bluetooth_rx_cur_ma))
                .thenReturn(123);
        XmlPullParser parser = Xml.newPullParser();
        parser.setInput(new StringReader(
                "<device name='Android'>"
                + "<item name='bluetooth.controller.idle'>10</item>"
                + "</device>"));
        mProfile.initForTesting(parser, mockResources);
        assertThat(mProfile.getAveragePower(PowerProfile.POWER_BLUETOOTH_CONTROLLER_IDLE))
                .isEqualTo(10);
        assertThat(mProfile.getAveragePower(PowerProfile.POWER_BLUETOOTH_CONTROLLER_RX))
                .isEqualTo(123);
    }

    @Test
    public void testPowerProfile_legacyCpuConfig() {
        // This power profile has per-cluster data, rather than per-policy
        mProfile.forceInitForTesting(mContext, R.xml.power_profile_test_cpu_legacy);
        mProfile.initForTesting(resolveParser("power_profile_test_cpu_legacy"));

        assertEquals(2.11, mProfile.getAveragePowerForCpuScalingPolicy(0));
        assertEquals(2.22, mProfile.getAveragePowerForCpuScalingPolicy(4));
@@ -148,7 +176,7 @@ public class PowerProfileTest {

    @Test
    public void testModemPowerProfile_defaultRat() throws Exception {
        final XmlResourceParser parser = getTestModemElement(R.xml.power_profile_test_modem,
        final XmlPullParser parser = getTestModemElement("power_profile_test_modem",
                "testModemPowerProfile_defaultRat");
        ModemPowerProfile mpp = new ModemPowerProfile();
        mpp.parseFromXml(parser);
@@ -216,7 +244,7 @@ public class PowerProfileTest {

    @Test
    public void testModemPowerProfile_partiallyDefined() throws Exception {
        final XmlResourceParser parser = getTestModemElement(R.xml.power_profile_test_modem,
        final XmlPullParser parser = getTestModemElement("power_profile_test_modem",
                "testModemPowerProfile_partiallyDefined");
        ModemPowerProfile mpp = new ModemPowerProfile();
        mpp.parseFromXml(parser);
@@ -369,7 +397,7 @@ public class PowerProfileTest {

    @Test
    public void testModemPowerProfile_fullyDefined() throws Exception {
        final XmlResourceParser parser = getTestModemElement(R.xml.power_profile_test_modem,
        final XmlPullParser parser = getTestModemElement("power_profile_test_modem",
                "testModemPowerProfile_fullyDefined");
        ModemPowerProfile mpp = new ModemPowerProfile();
        mpp.parseFromXml(parser);
@@ -519,11 +547,10 @@ public class PowerProfileTest {
                | ModemPowerProfile.MODEM_DRAIN_TYPE_TX | ModemPowerProfile.MODEM_TX_LEVEL_4));
    }

    private XmlResourceParser getTestModemElement(@XmlRes int xmlId, String elementName)
    private XmlPullParser getTestModemElement(String resourceName, String elementName)
            throws Exception {
        XmlPullParser parser = resolveParser(resourceName);
        final String element = TAG_TEST_MODEM;
        final Resources resources = mContext.getResources();
        XmlResourceParser parser = resources.getXml(xmlId);
        while (true) {
            XmlUtils.nextElement(parser);
            final String e = parser.getName();
@@ -535,10 +562,26 @@ public class PowerProfileTest {

            return parser;
        }
        fail("Unanable to find element " + element + " with name " + elementName);
        fail("Unable to find element " + element + " with name " + elementName);
        return null;
    }

    private XmlPullParser resolveParser(String resourceName) {
        if (RavenwoodRule.isOnRavenwood()) {
            try {
                return Xml.resolvePullParser(getClass().getClassLoader()
                        .getResourceAsStream("res/xml/" + resourceName + ".xml"));
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        } else {
            Context context = androidx.test.InstrumentationRegistry.getContext();
            Resources resources = context.getResources();
            int resId = resources.getIdentifier(resourceName, "xml", context.getPackageName());
            return resources.getXml(resId);
        }
    }

    private void assertEquals(double expected, double actual) {
        Assert.assertEquals(expected, actual, 0.1);
    }
Loading