diff options
| author | 2025-01-24 14:52:55 -0800 | |
|---|---|---|
| committer | 2025-01-24 14:52:55 -0800 | |
| commit | b30488c62c78c169dd7eee5529440bed06662025 (patch) | |
| tree | 62ee39f93582b9b720cc7a387c8d82e7e820a16b | |
| parent | 8c054dd0db7cc5dfc6adf9bf8d315945d223ea5f (diff) | |
Remove MessageQueue reflection from TestableLooper
Bug: 379472827
Change-Id: I4b43e6a205e00c726dd2cd2399ed50ab43a4a871
Flag: android.os.message_queue_testability
| -rw-r--r-- | tests/testables/src/android/testing/TestableLooper.java | 52 |
1 files changed, 13 insertions, 39 deletions
diff --git a/tests/testables/src/android/testing/TestableLooper.java b/tests/testables/src/android/testing/TestableLooper.java index be5c84c0353c..9f12f68722d9 100644 --- a/tests/testables/src/android/testing/TestableLooper.java +++ b/tests/testables/src/android/testing/TestableLooper.java @@ -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; - } - MESSAGE_WHEN_FIELD.set(msg, updatedWhen); - msg = (Message) MESSAGE_NEXT_FIELD.get(msg); + 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 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); } } |