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

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

Remove MessageQueue reflection from TestableLooper

Bug: 379472827 
Change-Id: I4b43e6a205e00c726dd2cd2399ed50ab43a4a871
Flag: android.os.message_queue_testability
parent 8c054dd0
Loading
Loading
Loading
Loading
+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;
@@ -50,9 +52,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;
@@ -61,19 +60,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);
    }
@@ -216,29 +202,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);
        }
    }