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

Commit c81ed531 authored by Shai Barack's avatar Shai Barack
Browse files

Deduplicate code that asserts on instrumentation

Flag: EXEMPT refactor
Change-Id: Ib1d1cccebe48adc3de1dae8f206d3c8076128e98
parent 7713fc27
Loading
Loading
Loading
Loading
+26 −0
Original line number Diff line number Diff line
@@ -259,6 +259,7 @@ import com.android.org.conscrypt.TrustedCertificateStore;
import com.android.server.am.BitmapDumpProto;
import com.android.server.am.MemInfoDumpProto;

import dalvik.annotation.optimization.NeverInline;
import dalvik.annotation.optimization.NeverCompile;
import dalvik.system.AppSpecializationHooks;
import dalvik.system.CloseGuard;
@@ -3354,6 +3355,31 @@ public final class ActivityThread extends ClientTransactionHandler
        return mInstrumentation;
    }

    @NeverCompile // Only called by tests.
    @NeverInline  // Only called by tests.
    @RavenwoodReplace
    public static void throwIfNotInstrumenting() {
        final ActivityThread activityThread = ActivityThread.currentActivityThread();
        if (activityThread == null) {
            // Only tests can reach here.
            return;
        }
        final Instrumentation instrumentation = activityThread.getInstrumentation();
        if (instrumentation == null) {
            // Only tests can reach here.
            return;
        }
        if (instrumentation.isInstrumenting()) {
            return;
        }
        throw new IllegalStateException();
    }

    public static void throwIfNotInstrumenting$ravenwood() {
        // Treat Ravenwood tests as instrumenting, so that they can call code that is
        // used in instrumentation tests.
    }

    public boolean isProfiling() {
        return mProfiler != null && mProfiler.profileFile != null
                && mProfiler.profileFd == null;
+7 −22
Original line number Diff line number Diff line
@@ -1441,30 +1441,15 @@ public class PropertyInvalidatedCache<Query, Result> {
    /**
     * Throw if the current process is not allowed to use test APIs.
     */
    @android.ravenwood.annotation.RavenwoodReplace
    private static void throwIfNotTest() {
        final ActivityThread activityThread = ActivityThread.currentActivityThread();
        if (activityThread == null) {
            // Only tests can reach here.
            return;
        }
        final Instrumentation instrumentation = activityThread.getInstrumentation();
        if (instrumentation == null) {
            // Only tests can reach here.
            return;
        }
        if (instrumentation.isInstrumenting()) {
            return;
        }
        try {
            ActivityThread.throwIfNotInstrumenting();
        } catch (IllegalStateException e) {
            if (Flags.enforcePicTestmodeProtocol()) {
            throw new IllegalStateException("Test-only API called not from a test.");
                throw e;
            }
            // else swallow the exception
        }

    /**
     * Do not throw if running under ravenwood.
     */
    private static void throwIfNotTest$ravenwood() {
    }

    /**
+3 −26
Original line number Diff line number Diff line
@@ -22,7 +22,6 @@ import android.annotation.Nullable;
import android.annotation.SuppressLint;
import android.annotation.TestApi;
import android.app.ActivityThread;
import android.app.Instrumentation;
import android.compat.annotation.UnsupportedAppUsage;
import android.ravenwood.annotation.RavenwoodKeepWholeClass;
import android.ravenwood.annotation.RavenwoodRedirect;
@@ -211,28 +210,6 @@ public final class MessageQueue {
        return false;
    }

    @RavenwoodReplace
    private static void throwIfNotTest() {
        final ActivityThread activityThread = ActivityThread.currentActivityThread();
        if (activityThread == null) {
            // Only tests can reach here.
            return;
        }
        final Instrumentation instrumentation = activityThread.getInstrumentation();
        if (instrumentation == null) {
            // Only tests can reach here.
            return;
        }
        if (instrumentation.isInstrumenting()) {
            return;
        }
        throw new IllegalStateException("Test-only API called not from a test!");
    }

    private static void throwIfNotTest$ravenwood() {
        return;
    }

    @Override
    protected void finalize() throws Throwable {
        try {
@@ -1517,7 +1494,7 @@ public final class MessageQueue {
     */
    @SuppressLint("VisiblySynchronized") // Legacy MessageQueue synchronizes on this
    Long peekWhenForTest() {
        throwIfNotTest();
        ActivityThread.throwIfNotInstrumenting();
        Message ret;
        if (sUseConcurrent) {
            ret = nextMessage(true, true);
@@ -1536,7 +1513,7 @@ public final class MessageQueue {
    @SuppressLint("VisiblySynchronized") // Legacy MessageQueue synchronizes on this
    @Nullable
    Message pollForTest() {
        throwIfNotTest();
        ActivityThread.throwIfNotInstrumenting();
        if (sUseConcurrent) {
            return nextMessage(false, true);
        } else {
@@ -1552,7 +1529,7 @@ public final class MessageQueue {
     * and may not be resumed until after returning from this method.
     */
    boolean isBlockedOnSyncBarrier() {
        throwIfNotTest();
        ActivityThread.throwIfNotInstrumenting();
        if (sUseConcurrent) {
            // Call nextMessage to get the stack drained into our priority queues
            nextMessage(true, false);
+3 −26
Original line number Diff line number Diff line
@@ -22,7 +22,6 @@ import android.annotation.Nullable;
import android.annotation.SuppressLint;
import android.annotation.TestApi;
import android.app.ActivityThread;
import android.app.Instrumentation;
import android.compat.annotation.UnsupportedAppUsage;
import android.ravenwood.annotation.RavenwoodKeepWholeClass;
import android.ravenwood.annotation.RavenwoodRedirect;
@@ -103,28 +102,6 @@ public final class MessageQueue {
        mPtr = nativeInit();
    }

    @android.ravenwood.annotation.RavenwoodReplace
    private static void throwIfNotTest() {
        final ActivityThread activityThread = ActivityThread.currentActivityThread();
        if (activityThread == null) {
            // Only tests can reach here.
            return;
        }
        final Instrumentation instrumentation = activityThread.getInstrumentation();
        if (instrumentation == null) {
            // Only tests can reach here.
            return;
        }
        if (instrumentation.isInstrumenting()) {
            return;
        }
        throw new IllegalStateException("Test-only API called not from a test!");
    }

    private static void throwIfNotTest$ravenwood() {
        return;
    }

    @Override
    protected void finalize() throws Throwable {
        try {
@@ -758,7 +735,7 @@ public final class MessageQueue {
     */
    @SuppressLint("VisiblySynchronized") // Legacy MessageQueue synchronizes on this
    Long peekWhenForTest() {
        throwIfNotTest();
        ActivityThread.throwIfNotInstrumenting();
        Message ret = legacyPeekOrPoll(true);
        return ret != null ? ret.when : null;
    }
@@ -772,7 +749,7 @@ public final class MessageQueue {
    @SuppressLint("VisiblySynchronized") // Legacy MessageQueue synchronizes on this
    @Nullable
    Message pollForTest() {
        throwIfNotTest();
        ActivityThread.throwIfNotInstrumenting();
        return legacyPeekOrPoll(false);
    }

@@ -784,7 +761,7 @@ public final class MessageQueue {
     * and may not be resumed until after returning from this method.
     */
    boolean isBlockedOnSyncBarrier() {
        throwIfNotTest();
        ActivityThread.throwIfNotInstrumenting();
        synchronized (this) {
            Message msg = mMessages;
            return msg != null && msg.target == null;