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

Commit 09f25fac authored by Jeff Sharkey's avatar Jeff Sharkey
Browse files

More tests for BroadcastRecord.

This change moves the existing BroadcastRecordTest into a consistent
suite location with the other broadcast-related tests, and then adds
tests for recently added logic.

Clearly annotate nullness of fields.

Bug: 250666387
Test: atest FrameworksMockingServicesTests:BroadcastRecordTest
Test: atest FrameworksMockingServicesTests:BroadcastQueueTest
Test: atest FrameworksMockingServicesTests:BroadcastQueueModernImplTest
Change-Id: I18847c61ea7b74c4324abf51005b61728dd2acb7
parent a6805188
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -764,6 +764,8 @@ class BroadcastQueueModernImpl extends BroadcastQueue {
                app.scheduleCrashLocked(msg, CannotDeliverBroadcastException.TYPE_ID, null);
            }
        }
        // Clear so both local and remote references can be GC'ed
        r.resultTo = null;
    }

    private void deliveryTimeoutSoftLocked(@NonNull BroadcastProcessQueue queue) {
+26 −25
Original line number Diff line number Diff line
@@ -68,10 +68,10 @@ import java.util.function.BiFunction;
 * An active intent broadcast.
 */
final class BroadcastRecord extends Binder {
    final Intent intent;    // the original intent that generated us
    final ComponentName targetComp; // original component name set on the intent
    final ProcessRecord callerApp; // process that sent this
    final String callerPackage; // who sent this
    final @NonNull Intent intent;    // the original intent that generated us
    final @Nullable ComponentName targetComp; // original component name set on the intent
    final @Nullable ProcessRecord callerApp; // process that sent this
    final @Nullable String callerPackage; // who sent this
    final @Nullable String callerFeatureId; // which feature in the package sent this
    final int callingPid;   // the pid of who sent this
    final int callingUid;   // the uid of who sent this
@@ -84,15 +84,15 @@ final class BroadcastRecord extends Binder {
    final boolean initialSticky; // initial broadcast from register to sticky?
    final boolean prioritized; // contains more than one priority tranche
    final int userId;       // user id this broadcast was for
    final String resolvedType; // the resolved data type
    final String[] requiredPermissions; // permissions the caller has required
    final String[] excludedPermissions; // permissions to exclude
    final String[] excludedPackages; // packages to exclude
    final @Nullable String resolvedType; // the resolved data type
    final @Nullable String[] requiredPermissions; // permissions the caller has required
    final @Nullable String[] excludedPermissions; // permissions to exclude
    final @Nullable String[] excludedPackages; // packages to exclude
    final int appOp;        // an app op that is associated with this broadcast
    final BroadcastOptions options; // BroadcastOptions supplied by caller
    final @Nullable BroadcastOptions options; // BroadcastOptions supplied by caller
    final @NonNull List<Object> receivers;   // contains BroadcastFilter and ResolveInfo
    final @DeliveryState int[] delivery;   // delivery state of each receiver
    IIntentReceiver resultTo; // who receives final result if non-null
    @Nullable IIntentReceiver resultTo; // who receives final result if non-null
    boolean deferred;
    int splitCount;         // refcount for result callback, when split
    int splitToken;         // identifier for cross-BroadcastRecord refcount
@@ -106,10 +106,10 @@ final class BroadcastRecord extends Binder {
    @UptimeMillisLong       long finishTime;         // when broadcast finished
    final @UptimeMillisLong long[] scheduledTime;    // when each receiver was scheduled
    final @UptimeMillisLong long[] terminalTime;     // when each receiver was terminal
    boolean timeoutExempt;  // true if this broadcast is not subject to receiver timeouts
    final boolean timeoutExempt;  // true if this broadcast is not subject to receiver timeouts
    int resultCode;         // current result code value.
    String resultData;      // current result data value.
    Bundle resultExtras;    // current result extra data values.
    @Nullable String resultData;      // current result data value.
    @Nullable Bundle resultExtras;    // current result extra data values.
    boolean resultAbort;    // current result abortBroadcast value.
    int nextReceiver;       // next receiver to be executed.
    int state;
@@ -117,7 +117,7 @@ final class BroadcastRecord extends Binder {
    int manifestCount;      // number of manifest receivers dispatched.
    int manifestSkipCount;  // number of manifest receivers skipped.
    int terminalCount;      // number of receivers in terminal state.
    BroadcastQueue queue;   // the outbound queue handling this broadcast
    @Nullable BroadcastQueue queue;   // the outbound queue handling this broadcast

    // if set to true, app's process will be temporarily allowed to start activities from background
    // for the duration of the broadcast dispatch
@@ -131,8 +131,8 @@ final class BroadcastRecord extends Binder {
    @Nullable
    final BiFunction<Integer, Bundle, Bundle> filterExtrasForReceiver;

    String cachedToString;
    String cachedToShortString;
    private @Nullable String mCachedToString;
    private @Nullable String mCachedToShortString;

    /** Empty immutable list of receivers */
    static final List<Object> EMPTY_RECEIVERS = List.of();
@@ -351,7 +351,7 @@ final class BroadcastRecord extends Binder {
            throw new NullPointerException("Can't construct with a null intent");
        }
        queue = _queue;
        intent = _intent;
        intent = Objects.requireNonNull(_intent);
        targetComp = _intent.getComponent();
        callerApp = _callerApp;
        callerPackage = _callerPackage;
@@ -394,7 +394,7 @@ final class BroadcastRecord extends Binder {
     * Only used by {@link #maybeStripForHistory}.
     */
    private BroadcastRecord(BroadcastRecord from, Intent newIntent) {
        intent = newIntent;
        intent = Objects.requireNonNull(newIntent);
        targetComp = newIntent.getComponent();

        callerApp = from.callerApp;
@@ -652,7 +652,8 @@ final class BroadcastRecord extends Binder {
    /**
     * Return if given receivers list has more than one traunch of priorities.
     */
    private static boolean isPrioritized(@NonNull List<Object> receivers) {
    @VisibleForTesting
    static boolean isPrioritized(@NonNull List<Object> receivers) {
        int firstPriority = 0;
        for (int i = 0; i < receivers.size(); i++) {
            final int thisPriority = getReceiverPriority(receivers.get(i));
@@ -790,27 +791,27 @@ final class BroadcastRecord extends Binder {

    @Override
    public String toString() {
        if (cachedToString == null) {
        if (mCachedToString == null) {
            String label = intent.getAction();
            if (label == null) {
                label = intent.toString();
            }
            cachedToString = "BroadcastRecord{"
            mCachedToString = "BroadcastRecord{"
                + Integer.toHexString(System.identityHashCode(this))
                + " u" + userId + " " + label + "}";
        }
        return cachedToString;
        return mCachedToString;
    }

    public String toShortString() {
        if (cachedToShortString == null) {
        if (mCachedToShortString == null) {
            String label = intent.getAction();
            if (label == null) {
                label = intent.toString();
            }
            cachedToShortString = label + "/u" + userId;
            mCachedToShortString = label + "/u" + userId;
        }
        return cachedToShortString;
        return mCachedToShortString;
    }

    public void dumpDebug(ProtoOutputStream proto, long fieldId) {
+1 −0
Original line number Diff line number Diff line
@@ -65,6 +65,7 @@
      "file_patterns": ["Broadcast"],
      "name": "FrameworksMockingServicesTests",
      "options": [
        { "include-filter": "com.android.server.am.BroadcastRecordTest" },
        { "include-filter": "com.android.server.am.BroadcastQueueTest" },
        { "include-filter": "com.android.server.am.BroadcastQueueModernImplTest" }
      ]
+2 −15
Original line number Diff line number Diff line
@@ -17,7 +17,6 @@
package com.android.server.am;

import static com.android.server.am.BroadcastProcessQueue.insertIntoRunnableList;
import static com.android.server.am.BroadcastProcessQueue.reasonToString;
import static com.android.server.am.BroadcastProcessQueue.removeFromRunnableList;
import static com.android.server.am.BroadcastQueueTest.CLASS_GREEN;
import static com.android.server.am.BroadcastQueueTest.PACKAGE_BLUE;
@@ -26,12 +25,10 @@ import static com.android.server.am.BroadcastQueueTest.PACKAGE_RED;
import static com.android.server.am.BroadcastQueueTest.PACKAGE_YELLOW;
import static com.android.server.am.BroadcastQueueTest.getUidForPackage;
import static com.android.server.am.BroadcastQueueTest.makeManifestReceiver;
import static com.android.server.am.BroadcastRecord.deliveryStateToString;

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.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.doReturn;
@@ -64,6 +61,7 @@ public class BroadcastQueueModernImplTest {
    private static final int TEST_UID = android.os.Process.FIRST_APPLICATION_UID;

    @Mock ActivityManagerService mAms;
    @Mock ProcessRecord mProcess;

    @Mock BroadcastProcessQueue mQueue1;
    @Mock BroadcastProcessQueue mQueue2;
@@ -141,7 +139,7 @@ public class BroadcastQueueModernImplTest {

    private BroadcastRecord makeBroadcastRecord(Intent intent, BroadcastOptions options,
            List receivers, boolean ordered) {
        return new BroadcastRecord(mImpl, intent, null, PACKAGE_RED, null, 21, 42, false, null,
        return new BroadcastRecord(mImpl, intent, mProcess, PACKAGE_RED, null, 21, 42, false, null,
                null, null, null, AppOpsManager.OP_NONE, options, receivers, null,
                Activity.RESULT_OK, null, null, ordered, false, false, UserHandle.USER_SYSTEM,
                false, null, false, null);
@@ -385,15 +383,4 @@ public class BroadcastQueueModernImplTest {
        assertEquals(Intent.ACTION_SCREEN_OFF, queue.getActive().intent.getAction());
        assertTrue(queue.isEmpty());
    }

    /**
     * Verify that we emit something valid for each debug value.
     */
    @Test
    public void testIntDefToString() {
        for (int i = Byte.MIN_VALUE; i < Byte.MAX_VALUE; i++) {
            assertNotNull(deliveryStateToString(i));
            assertNotNull(reasonToString(i));
        }
    }
}
+12 −0
Original line number Diff line number Diff line
@@ -18,6 +18,9 @@ package com.android.server.am;

import static android.os.UserHandle.USER_SYSTEM;

import static com.android.server.am.BroadcastProcessQueue.reasonToString;
import static com.android.server.am.BroadcastRecord.deliveryStateToString;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals;
@@ -644,6 +647,10 @@ public class BroadcastQueueTest {
        return UserHandle.getUid(userId, getUidForPackage(packageName));
    }

    /**
     * Baseline verification of common debugging infrastructure, mostly to make
     * sure it doesn't crash.
     */
    @Test
    public void testDebugging() throws Exception {
        // To maximize test coverage, dump current state; we're not worried
@@ -664,6 +671,11 @@ public class BroadcastQueueTest {

        assertNotNull(mQueue.toString());
        assertNotNull(mQueue.describeStateLocked());

        for (int i = Byte.MIN_VALUE; i < Byte.MAX_VALUE; i++) {
            assertNotNull(deliveryStateToString(i));
            assertNotNull(reasonToString(i));
        }
    }

    /**
Loading