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

Commit 1e2293be authored by Shai Barack's avatar Shai Barack
Browse files

Remove MessageQueue reflection from TelephonyTest

Flag: EXEMPT test-only
Bug: 379472827
Change-Id: I4d11ec35c17a44eae6b5ab19bc3387f0e499649e
parent 545f9db0
Loading
Loading
Loading
Loading
+10 −58
Original line number Diff line number Diff line
@@ -174,23 +174,6 @@ public abstract class TelephonyTest {
            new ArrayList<String>(), EmergencyNumber.EMERGENCY_NUMBER_SOURCE_NETWORK_SIGNALING,
            EmergencyNumber.EMERGENCY_CALL_ROUTING_NORMAL);

    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);
        }
    }

    // Mocked classes
    protected FeatureFlags mFeatureFlags;
    protected GsmCdmaPhone mPhone;
@@ -1340,37 +1323,13 @@ public abstract class TelephonyTest {
        }
    }

    /**
     * @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;
@@ -1380,8 +1339,14 @@ public abstract class TelephonyTest {
     * 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();
        }
    }
@@ -1406,20 +1371,7 @@ public abstract class TelephonyTest {
     */
    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);
        }
    }
}