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

Commit eab67241 authored by Sudheer Shanka's avatar Sudheer Shanka
Browse files

Update netpolicy,bstats tests to not assume DUT will be plugged-in.

Bug: 73037837
Test: atest com.android.server.net.ConnOnActivityStartTest
Test: atest com.android.internal.os.BstatsCpuTimesValidationTest
Change-Id: I0647b6746d7d2cc9e4d24dfb14321f3fa5ac7f1c
parent f8ed7695
Loading
Loading
Loading
Loading
+13 −3
Original line number Diff line number Diff line
@@ -59,6 +59,7 @@ import android.text.TextUtils;
import android.util.DebugUtils;
import android.util.Log;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
@@ -118,6 +119,11 @@ public class BstatsCpuTimesValidationTest {
        checkCpuTimesAvailability();
    }

    @AfterClass
    public static void tearDownOnce() throws Exception {
        batteryReset();
    }

    // Checks cpu freq times of system uid as an indication of whether /proc/uid_time_in_state
    // and /proc/uid/<uid>/time_in_state kernel nodes are available.
    private static void checkCpuTimesAvailability() throws Exception {
@@ -127,7 +133,7 @@ public class BstatsCpuTimesValidationTest {
        final long[] totalCpuTimes = getAllCpuFreqTimes(Process.SYSTEM_UID);
        sCpuFreqTimesAvailable = totalCpuTimes != null;
        final long[] fgSvcCpuTimes = getAllCpuFreqTimes(Process.SYSTEM_UID,
                PROCESS_STATE_FOREGROUND_SERVICE);
                PROCESS_STATE_FOREGROUND);
        sPerProcStateTimesAvailable = fgSvcCpuTimes != null;
    }

@@ -868,12 +874,16 @@ public class BstatsCpuTimesValidationTest {

    private static void batteryOn() throws Exception {
        executeCmd("dumpsys battery unplug");
        assertBatteryState(false);
        assertBatteryState(false /* pluggedIn */);
    }

    private static void batteryOff() throws Exception {
        executeCmd("dumpsys battery set ac " + BatteryManager.BATTERY_PLUGGED_AC);
        assertBatteryState(true /* pluggedIn */);
    }

    private static void batteryReset() throws Exception {
        executeCmd("dumpsys battery reset");
        assertBatteryState(true);
    }

    private void screenOn() throws Exception {
+17 −12
Original line number Diff line number Diff line
@@ -117,7 +117,8 @@ public class ConnOnActivityStartTest {
    }

    @AfterClass
    public static void tearDownOnce() {
    public static void tearDownOnce() throws Exception {
        batteryReset();
        if (mAppIdleConstsUpdated) {
            Settings.Global.putString(mContext.getContentResolver(),
                    Settings.Global.APP_IDLE_CONSTANTS, mOriginalAppIdleConsts);
@@ -213,12 +214,12 @@ public class ConnOnActivityStartTest {
    @Test
    public void testStartActivity_appStandby() throws Exception {
        try{
            turnBatteryOff();
            turnBatteryOn();
            setAppIdle(true);
            turnScreenOn();
            startActivityAndCheckNetworkAccess();
        } finally {
            turnBatteryOn();
            turnBatteryOff();
            finishActivity();
            setAppIdle(false);
        }
@@ -251,11 +252,11 @@ public class ConnOnActivityStartTest {
    // create a static library which can be used by both servicestests and cts.
    private void setBatterySaverMode(boolean enabled) throws Exception {
        if (enabled) {
            turnBatteryOff();
            turnBatteryOn();
            executeCommand("settings put global low_power 1");
        } else {
            executeCommand("settings put global low_power 0");
            turnBatteryOn();
            turnBatteryOff();
        }
        final String result = executeCommand("settings get global low_power");
        assertEquals(enabled ? "1" : "0", result);
@@ -271,12 +272,12 @@ public class ConnOnActivityStartTest {

    private void setDozeMode(boolean enabled) throws Exception {
        if (enabled) {
            turnBatteryOff();
            turnBatteryOn();
            turnScreenOff();
            executeCommand("dumpsys deviceidle force-idle deep");
        } else {
            turnScreenOn();
            turnBatteryOn();
            turnBatteryOff();
            executeCommand("dumpsys deviceidle unforce");
        }
        assertDelayedCommandResult("dumpsys deviceidle get deep", enabled ? "IDLE" : "ACTIVE",
@@ -320,12 +321,12 @@ public class ConnOnActivityStartTest {
                + ". Full list: " + uids);
    }

    private void turnBatteryOff() throws Exception {
    private void turnBatteryOn() throws Exception {
        executeCommand("cmd battery unplug");
        assertBatteryOff();
        assertBatteryOn();
    }

    private void assertBatteryOff() throws Exception {
    private void assertBatteryOn() throws Exception {
        final long endTime = SystemClock.uptimeMillis() + BATTERY_OFF_TIMEOUT_MS;
        while (mBatteryManager.isCharging() && SystemClock.uptimeMillis() < endTime) {
            SystemClock.sleep(BATTERY_OFF_CHECK_INTERVAL_MS);
@@ -333,7 +334,11 @@ public class ConnOnActivityStartTest {
        assertFalse("Power should be disconnected", mBatteryManager.isCharging());
    }

    private void turnBatteryOn() throws Exception {
    private void turnBatteryOff() throws Exception {
        executeCommand("cmd battery set ac " + BatteryManager.BATTERY_PLUGGED_AC);
    }

    private static void batteryReset() throws Exception {
        executeCommand("cmd battery reset");
    }

@@ -348,7 +353,7 @@ public class ConnOnActivityStartTest {
        SystemClock.sleep(SCREEN_ON_DELAY_MS);
    }

    private String executeCommand(String cmd) throws IOException {
    private static String executeCommand(String cmd) throws IOException {
        final String result = executeSilentCommand(cmd);
        Log.d(TAG, String.format("Result for '%s': %s", cmd, result));
        return result;