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

Commit 835bd47b authored by Android Build Coastguard Worker's avatar Android Build Coastguard Worker
Browse files

Snap for 10017868 from dacc7908 to udc-qpr1-release

Change-Id: I8c070f251cf23174c04e799ace34ac54225ffc9a
parents 313a224f dacc7908
Loading
Loading
Loading
Loading
+38 −36
Original line number Diff line number Diff line
@@ -24,11 +24,9 @@ import static org.junit.Assume.assumeTrue;

import android.annotation.NonNull;
import android.app.ActivityManager;
import android.app.ActivityTaskManager;
import android.app.AppGlobals;
import android.app.IActivityManager;
import android.app.IStopUserCallback;
import android.app.WaitResult;
import android.app.WallpaperManager;
import android.content.BroadcastReceiver;
import android.content.Context;
@@ -69,6 +67,7 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

/**
 * Perf tests for user life cycle events.
@@ -101,7 +100,6 @@ public class UserLifecycleTests {
    private static final long TIMEOUT_MAX_TEST_TIME_MS = 24 * 60_000;

    private static final int TIMEOUT_IN_SECOND = 30;
    private static final int CHECK_USER_REMOVED_INTERVAL_MS = 200;

    /** Name of users/profiles in the test. Users with this name may be freely removed. */
    private static final String TEST_USER_NAME = "UserLifecycleTests_test_user";
@@ -127,6 +125,7 @@ public class UserLifecycleTests {
    private BroadcastWaiter mBroadcastWaiter;
    private UserSwitchWaiter mUserSwitchWaiter;
    private String mUserSwitchTimeoutMs;
    private String mDisableUserSwitchingDialogAnimations;

    private final BenchmarkRunner mRunner = new BenchmarkRunner();
    @Rule
@@ -153,16 +152,17 @@ public class UserLifecycleTests {
            Log.w(TAG, "WARNING: Tests are being run from user " + mAm.getCurrentUser()
                    + " rather than the system user");
        }
        mUserSwitchTimeoutMs = setSystemProperty("debug.usercontroller.user_switch_timeout_ms",
                "100000");
        if (TextUtils.isEmpty(mUserSwitchTimeoutMs)) {
            mUserSwitchTimeoutMs = "invalid";
        }
        mUserSwitchTimeoutMs = setSystemProperty(
                "debug.usercontroller.user_switch_timeout_ms", "100000");
        mDisableUserSwitchingDialogAnimations = setSystemProperty(
                "debug.usercontroller.disable_user_switching_dialog_animations", "true");
    }

    @After
    public void tearDown() throws Exception {
        setSystemProperty("debug.usercontroller.user_switch_timeout_ms", mUserSwitchTimeoutMs);
        setSystemProperty("debug.usercontroller.disable_user_switching_dialog_animations",
                mDisableUserSwitchingDialogAnimations);
        mBroadcastWaiter.close();
        mUserSwitchWaiter.close();
        for (int userId : mUsersToRemove) {
@@ -1259,15 +1259,13 @@ public class UserLifecycleTests {
     * <p> This should always be used for profiles since profiles cannot be started in foreground.
     */
    private void startUserInBackgroundAndWaitForUnlock(int userId) {
        final ProgressWaiter waiter = new ProgressWaiter();
        boolean success = false;
        try {
            mIam.startUserInBackgroundWithListener(userId, waiter);
            success = waiter.waitForFinish(TIMEOUT_IN_SECOND);
        } catch (RemoteException e) {
            Log.e(TAG, "startUserInBackgroundAndWaitForUnlock failed", e);
            attestTrue("Failed to start user " + userId + " in background.",
                    ShellHelper.runShellCommandWithTimeout("am start-user -w " + userId,
                            TIMEOUT_IN_SECOND).startsWith("Success:"));
        } catch (TimeoutException e) {
            fail("Could not start user " + userId + " in " + TIMEOUT_IN_SECOND + " seconds");
        }
        attestTrue("Failed to start user " + userId + " in background.", success);
    }

    /** Starts the given user in the foreground. */
@@ -1387,14 +1385,20 @@ public class UserLifecycleTests {
     * Launches the given package in the given user.
     * Make sure the keyguard has been dismissed prior to calling.
     */
    private void startApp(int userId, String packageName) throws RemoteException {
        final Context context = InstrumentationRegistry.getContext();
        final WaitResult result = ActivityTaskManager.getService().startActivityAndWait(null,
                context.getPackageName(), context.getAttributionTag(),
                context.getPackageManager().getLaunchIntentForPackage(packageName), null, null,
                null, 0, 0, null, null, userId);
        attestTrue("User " + userId + " failed to start " + packageName,
                result.result == ActivityManager.START_SUCCESS);
    private void startApp(int userId, String packageName) {
        final String failMessage = "User " + userId + " failed to start " + packageName;
        final String component = InstrumentationRegistry.getContext().getPackageManager()
                .getLaunchIntentForPackage(packageName).getComponent().flattenToShortString();
        try {
            final String result = ShellHelper.runShellCommandWithTimeout(
                    "am start -W -n " + component + " --user " + userId, TIMEOUT_IN_SECOND);
            assertTrue(failMessage + ", component=" + component + ", result=" + result,
                    result.contains("Status: ok")
                    && !result.contains("Warning:")
                    && !result.contains("Error:"));
        } catch (TimeoutException e) {
            fail(failMessage + " in " + TIMEOUT_IN_SECOND + " seconds");
        }
    }

    private class ProgressWaiter extends IProgressListener.Stub {
@@ -1469,17 +1473,11 @@ public class UserLifecycleTests {
    private void removeUser(int userId) throws RemoteException {
        stopUserAfterWaitingForBroadcastIdle(userId, true);
        try {
            mUm.removeUser(userId);
            final long startTime = System.currentTimeMillis();
            final long timeoutInMs = TIMEOUT_IN_SECOND * 1000;
            while (mUm.getUserInfo(userId) != null &&
                    System.currentTimeMillis() - startTime < timeoutInMs) {
                TimeUnit.MILLISECONDS.sleep(CHECK_USER_REMOVED_INTERVAL_MS);
            }
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        } catch (Exception e) {
            // Ignore
            ShellHelper.runShellCommandWithTimeout("pm remove-user -w " + userId,
                    TIMEOUT_IN_SECOND);
        } catch (TimeoutException e) {
            Log.e(TAG, String.format("Could not remove user %d in %d seconds",
                    userId, TIMEOUT_IN_SECOND), e);
        }
        if (mUm.getUserInfo(userId) != null) {
            mUsersToRemove.add(userId);
@@ -1538,11 +1536,15 @@ public class UserLifecycleTests {
    private String setSystemProperty(String name, String value) throws Exception {
        final String oldValue = ShellHelper.runShellCommand("getprop " + name);
        assertEquals("", ShellHelper.runShellCommand("setprop " + name + " " + value));
        return oldValue;
        return TextUtils.firstNotEmpty(oldValue, "invalid");
    }

    private void waitForBroadcastIdle() {
        ShellHelper.runShellCommand("am wait-for-broadcast-idle");
        try {
            ShellHelper.runShellCommandWithTimeout("am wait-for-broadcast-idle", TIMEOUT_IN_SECOND);
        } catch (TimeoutException e) {
            Log.e(TAG, "Ending waitForBroadcastIdle because it is taking too long", e);
        }
    }

    private void sleep(long ms) {
+41 −0
Original line number Diff line number Diff line
@@ -24,12 +24,53 @@ import androidx.annotation.NonNull;
import androidx.test.InstrumentationRegistry;

import java.io.FileInputStream;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
import java.util.concurrent.atomic.AtomicReference;

/**
 * Provides Shell-based utilities such as running a command.
 */
public final class ShellHelper {

    /**
     * Runs a Shell command with a timeout, returning a trimmed response.
     */
    @NonNull
    public static String runShellCommandWithTimeout(@NonNull String command, long timeoutInSecond)
            throws TimeoutException {
        AtomicReference<Exception> exception = new AtomicReference<>(null);
        AtomicReference<String> result = new AtomicReference<>(null);

        CountDownLatch latch = new CountDownLatch(1);

        new Thread(() -> {
            try {
                result.set(runShellCommandRaw(command));
            } catch (Exception e) {
                exception.set(e);
            } finally {
                latch.countDown();
            }
        }).start();

        try {
            if (!latch.await(timeoutInSecond, TimeUnit.SECONDS)) {
                throw new TimeoutException("Command: '" + command + "' could not run in "
                        + timeoutInSecond + " seconds");
            }
        } catch (InterruptedException e) {
            Thread.currentThread().interrupt();
        }

        if (exception.get() != null) {
            throw new AndroidRuntimeException(exception.get());
        }

        return result.get();
    }

    /**
     * Runs a Shell command, returning a trimmed response.
     */
+2 −1
Original line number Diff line number Diff line
@@ -265,7 +265,8 @@ public abstract class JobService extends Service {
     * @see JobInfo.Builder#setRequiredNetworkType(int)
     */
    public void onNetworkChanged(@NonNull JobParameters params) {
        Log.w(TAG, "onNetworkChanged() not implemented. Must override in a subclass.");
        Log.w(TAG, "onNetworkChanged() not implemented in " + getClass().getName()
                + ". Must override in a subclass.");
    }

    /**
+4 −0
Original line number Diff line number Diff line
@@ -292,6 +292,10 @@ class Alarm {
                return "permission";
            case EXACT_ALLOW_REASON_POLICY_PERMISSION:
                return "policy_permission";
            case EXACT_ALLOW_REASON_LISTENER:
                return "listener";
            case EXACT_ALLOW_REASON_PRIORITIZED:
                return "prioritized";
            case EXACT_ALLOW_REASON_NOT_APPLICABLE:
                return "N/A";
            default:
+1 −2
Original line number Diff line number Diff line
@@ -4873,8 +4873,7 @@ public class AlarmManagerService extends SystemService {
                                }
                            }
                            if (wakeupUids.size() > 0 && mBatteryStatsInternal != null) {
                                mBatteryStatsInternal.noteCpuWakingActivity(
                                        BatteryStatsInternal.CPU_WAKEUP_SUBSYSTEM_ALARM, nowELAPSED,
                                mBatteryStatsInternal.noteWakingAlarmBatch(nowELAPSED,
                                        wakeupUids.toArray());
                            }
                            deliverAlarmsLocked(triggerList, nowELAPSED);
Loading