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

Commit cace2f05 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge changes I4b43e6a2,I2d77740c into main

* changes:
  Remove MessageQueue reflection from TestableLooper
  Return messages in future in pollForTest.
parents efc34112 b30488c6
Loading
Loading
Loading
Loading
+25 −25
Original line number Diff line number Diff line
@@ -601,7 +601,7 @@ public final class MessageQueue {
    /* This is only read/written from the Looper thread. For use with Concurrent MQ */
    private int mNextPollTimeoutMillis;
    private boolean mMessageDirectlyQueued;
    private Message nextMessage(boolean peek) {
    private Message nextMessage(boolean peek, boolean returnEarliest) {
        int i = 0;

        while (true) {
@@ -678,7 +678,7 @@ public final class MessageQueue {
             * If we have a barrier we should return the async node (if it exists and is ready)
             */
            if (msgNode != null && msgNode.isBarrier()) {
                if (asyncMsgNode != null && now >= asyncMsgNode.getWhen()) {
                if (asyncMsgNode != null && (returnEarliest || now >= asyncMsgNode.getWhen())) {
                    found = asyncMsgNode;
                } else {
                    next = asyncMsgNode;
@@ -692,7 +692,7 @@ public final class MessageQueue {
                earliest = pickEarliestNode(msgNode, asyncMsgNode);

                if (earliest != null) {
                    if (now >= earliest.getWhen()) {
                    if (returnEarliest || now >= earliest.getWhen()) {
                        found = earliest;
                    } else {
                        next = earliest;
@@ -797,7 +797,7 @@ public final class MessageQueue {
            mMessageDirectlyQueued = false;
            nativePollOnce(ptr, mNextPollTimeoutMillis);

            Message msg = nextMessage(false);
            Message msg = nextMessage(false, false);
            if (msg != null) {
                msg.markInUse();
                return msg;
@@ -1374,6 +1374,7 @@ public final class MessageQueue {
                if (now >= msg.when) {
                    // Got a message.
                    mBlocked = false;
                }
                if (prevMsg != null) {
                    prevMsg.next = msg.next;
                    if (prevMsg.next == null) {
@@ -1396,7 +1397,6 @@ public final class MessageQueue {
                return msg;
            }
        }
        }
        return null;
    }

@@ -1411,7 +1411,7 @@ public final class MessageQueue {
        throwIfNotTest();
        Message ret;
        if (mUseConcurrent) {
            ret = nextMessage(true);
            ret = nextMessage(true, true);
        } else {
            ret = legacyPeekOrPoll(true);
        }
@@ -1429,7 +1429,7 @@ public final class MessageQueue {
    Message pollForTest() {
        throwIfNotTest();
        if (mUseConcurrent) {
            return nextMessage(false);
            return nextMessage(false, true);
        } else {
            return legacyPeekOrPoll(false);
        }
@@ -1446,7 +1446,7 @@ public final class MessageQueue {
        throwIfNotTest();
        if (mUseConcurrent) {
            // Call nextMessage to get the stack drained into our priority queues
            nextMessage(true);
            nextMessage(true, false);

            Iterator<MessageNode> queueIter = mPriorityQueue.iterator();
            MessageNode queueNode = iterateNext(queueIter);
+7 −7
Original line number Diff line number Diff line
@@ -588,7 +588,7 @@ public final class MessageQueue {
    private static final AtomicLong mMessagesDelivered = new AtomicLong();
    private boolean mMessageDirectlyQueued;

    private Message nextMessage(boolean peek) {
    private Message nextMessage(boolean peek, boolean returnEarliest) {
        int i = 0;

        while (true) {
@@ -665,7 +665,7 @@ public final class MessageQueue {
             * If we have a barrier we should return the async node (if it exists and is ready)
             */
            if (msgNode != null && msgNode.isBarrier()) {
                if (asyncMsgNode != null && now >= asyncMsgNode.getWhen()) {
                if (asyncMsgNode != null && (returnEarliest || now >= asyncMsgNode.getWhen())) {
                    found = asyncMsgNode;
                } else {
                    next = asyncMsgNode;
@@ -679,7 +679,7 @@ public final class MessageQueue {
                earliest = pickEarliestNode(msgNode, asyncMsgNode);

                if (earliest != null) {
                    if (now >= earliest.getWhen()) {
                    if (returnEarliest || now >= earliest.getWhen()) {
                        found = earliest;
                    } else {
                        next = earliest;
@@ -784,7 +784,7 @@ public final class MessageQueue {
            mMessageDirectlyQueued = false;
            nativePollOnce(ptr, mNextPollTimeoutMillis);

            Message msg = nextMessage(false);
            Message msg = nextMessage(false, false);
            if (msg != null) {
                msg.markInUse();
                return msg;
@@ -1089,7 +1089,7 @@ public final class MessageQueue {
     */
    Long peekWhenForTest() {
        throwIfNotTest();
        Message ret = nextMessage(true);
        Message ret = nextMessage(true, true);
        return ret != null ? ret.when : null;
    }

@@ -1102,7 +1102,7 @@ public final class MessageQueue {
    @Nullable
    Message pollForTest() {
        throwIfNotTest();
        return nextMessage(false);
        return nextMessage(false, true);
    }

    /**
@@ -1116,7 +1116,7 @@ public final class MessageQueue {
        throwIfNotTest();

        // Call nextMessage to get the stack drained into our priority queues
        nextMessage(true);
        nextMessage(true, false);

        Iterator<MessageNode> queueIter = mPriorityQueue.iterator();
        MessageNode queueNode = iterateNext(queueIter);
+18 −18
Original line number Diff line number Diff line
@@ -759,6 +759,7 @@ public final class MessageQueue {
                if (now >= msg.when) {
                    // Got a message.
                    mBlocked = false;
                }
                if (prevMsg != null) {
                    prevMsg.next = msg.next;
                    if (prevMsg.next == null) {
@@ -781,7 +782,6 @@ public final class MessageQueue {
                return msg;
            }
        }
        }
        return null;
    }

+13 −39
Original line number Diff line number Diff line
@@ -16,11 +16,13 @@ package android.testing;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.app.Instrumentation;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.os.MessageQueue;
import android.os.SystemClock;
import android.os.TestLooperManager;
import android.util.ArrayMap;

@@ -32,7 +34,7 @@ import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Field;
import java.util.LinkedList;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;
@@ -56,9 +58,6 @@ public class TestableLooper {
     * catch crashes.
     */
    public static final boolean HOLD_MAIN_THREAD = false;
    private static final Field MESSAGE_QUEUE_MESSAGES_FIELD;
    private static final Field MESSAGE_NEXT_FIELD;
    private static final Field MESSAGE_WHEN_FIELD;

    private Looper mLooper;
    private MessageQueue mQueue;
@@ -67,19 +66,6 @@ public class TestableLooper {
    private Handler mHandler;
    private TestLooperManager mQueueWrapper;

    static {
        try {
            MESSAGE_QUEUE_MESSAGES_FIELD = MessageQueue.class.getDeclaredField("mMessages");
            MESSAGE_QUEUE_MESSAGES_FIELD.setAccessible(true);
            MESSAGE_NEXT_FIELD = Message.class.getDeclaredField("next");
            MESSAGE_NEXT_FIELD.setAccessible(true);
            MESSAGE_WHEN_FIELD = Message.class.getDeclaredField("when");
            MESSAGE_WHEN_FIELD.setAccessible(true);
        } catch (NoSuchFieldException e) {
            throw new RuntimeException("Failed to initialize TestableLooper", e);
        }
    }

    public TestableLooper(Looper l) throws Exception {
        this(acquireLooperManager(l), l);
    }
@@ -222,29 +208,17 @@ public class TestableLooper {
    }

    public void moveTimeForward(long milliSeconds) {
        try {
            Message msg = getMessageLinkedList();
            while (msg != null) {
                long updatedWhen = msg.getWhen() - milliSeconds;
                if (updatedWhen < 0) {
                    updatedWhen = 0;
        long futureWhen = SystemClock.uptimeMillis() + milliSeconds;
        // Find messages in the queue enqueued within the future time, and execute them now.
        while (true) {
            Long peekWhen = mQueueWrapper.peekWhen();
            if (peekWhen == null || peekWhen > futureWhen) {
                break;
            }
                MESSAGE_WHEN_FIELD.set(msg, updatedWhen);
                msg = (Message) MESSAGE_NEXT_FIELD.get(msg);
            Message message = mQueueWrapper.poll();
            if (message != null) {
                mQueueWrapper.execute(message);
            }
        } catch (IllegalAccessException e) {
            throw new RuntimeException("Access failed in TestableLooper: set - Message.when", e);
        }
    }

    private Message getMessageLinkedList() {
        try {
            MessageQueue queue = mLooper.getQueue();
            return (Message) MESSAGE_QUEUE_MESSAGES_FIELD.get(queue);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(
                    "Access failed in TestableLooper: get - MessageQueue.mMessages",
                    e);
        }
    }