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

Commit 98abaa6d authored by Shai Barack's avatar Shai Barack
Browse files

Remove MessageQueue reflection from ImsTestBase

Flag: EXEMPT test-only
Bug: 379472827
Change-Id: Ieffe20d7aeda5f50227276bfaf061d08a4b96993
parent 6de683d2
Loading
Loading
Loading
Loading
+11 −58
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.MessageQueue;
import android.os.SystemClock;
import android.testing.TestableLooper;

import androidx.test.InstrumentationRegistry;
@@ -31,7 +32,6 @@ import com.android.internal.telephony.TelephonyTest;

import org.mockito.MockitoAnnotations;

import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
@@ -41,22 +41,6 @@ import java.util.concurrent.TimeUnit;
 * Helper class to load Mockito Resources into a test.
 */
public class ImsTestBase {
    private static final Field MESSAGE_QUEUE_FIELD;
    private static final Field MESSAGE_WHEN_FIELD;
    private static final Field MESSAGE_NEXT_FIELD;

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

    protected Context mContext;
    protected List<TestableLooper> mTestableLoopers = new ArrayList<>();
@@ -130,37 +114,13 @@ public class ImsTestBase {
        }
    }

    /**
     * @return The longest delay from all the message queues.
     */
    private long getLongestDelay() {
        long delay = 0;
        for (TestableLooper looper : mTestableLoopers) {
            MessageQueue queue = looper.getLooper().getQueue();
            try {
                Message msg = (Message) MESSAGE_QUEUE_FIELD.get(queue);
                while (msg != null) {
                    delay = Math.max(msg.getWhen(), delay);
                    msg = (Message) MESSAGE_NEXT_FIELD.get(msg);
                }
            } catch (IllegalAccessException e) {
                throw new RuntimeException("Access failed in TelephonyTest", e);
            }
        }
        return delay;
    }

    /**
     * @return {@code true} if there are any messages in the queue.
     */
    private boolean messagesExist() {
        for (TestableLooper looper : mTestableLoopers) {
            MessageQueue queue = looper.getLooper().getQueue();
            try {
                Message msg = (Message) MESSAGE_QUEUE_FIELD.get(queue);
                if (msg != null) return true;
            } catch (IllegalAccessException e) {
                throw new RuntimeException("Access failed in TelephonyTest", e);
            if (looper.peekWhen() > 0) {
                return true;
            }
        }
        return false;
@@ -170,8 +130,14 @@ public class ImsTestBase {
     * Handle all messages including the delayed messages.
     */
    public void processAllFutureMessages() {
        final long now = SystemClock.uptimeMillis();
        while (messagesExist()) {
            moveTimeForward(getLongestDelay());
            for (TestableLooper looper : mTestableLoopers) {
                long nextDelay = looper.peekWhen() - now;
                if (nextDelay > 0) {
                    looper.moveTimeForward(nextDelay);
                }
            }
            processAllMessages();
        }
    }
@@ -196,20 +162,7 @@ public class ImsTestBase {
     */
    public void moveTimeForward(long milliSeconds) {
        for (TestableLooper looper : mTestableLoopers) {
            MessageQueue queue = looper.getLooper().getQueue();
            try {
                Message msg = (Message) MESSAGE_QUEUE_FIELD.get(queue);
                while (msg != null) {
                    long updatedWhen = msg.getWhen() - milliSeconds;
                    if (updatedWhen < 0) {
                        updatedWhen = 0;
                    }
                    MESSAGE_WHEN_FIELD.set(msg, updatedWhen);
                    msg = (Message) MESSAGE_NEXT_FIELD.get(msg);
                }
            } catch (IllegalAccessException e) {
                throw new RuntimeException("Access failed in TelephonyTest", e);
            }
            looper.moveTimeForward(milliSeconds);
        }
    }
}