diff options
| author | 2025-02-19 10:57:11 -0800 | |
|---|---|---|
| committer | 2025-02-19 10:57:11 -0800 | |
| commit | b52ca7d40f0dd9c1c28331a637478fb893583349 (patch) | |
| tree | fb8c6e7fcb46d52e5e56edea4fe46ffeff66780b | |
| parent | 094ef8fa93c2682c017192f1d9328efdab5d64bf (diff) | |
| parent | 949e5e48020db3c8d5b3b0a2b1b3d9120cab4ad2 (diff) | |
Merge "Use Flag instead of method check to detect new message queue testability API" into main
| -rw-r--r-- | tests/testables/src/android/testing/TestableLooper.java | 19 | ||||
| -rw-r--r-- | tests/utils/testutils/java/android/os/test/TestLooper.java | 44 |
2 files changed, 25 insertions, 38 deletions
diff --git a/tests/testables/src/android/testing/TestableLooper.java b/tests/testables/src/android/testing/TestableLooper.java index 8cd89ce89e83..7a19add1d1b9 100644 --- a/tests/testables/src/android/testing/TestableLooper.java +++ b/tests/testables/src/android/testing/TestableLooper.java @@ -34,7 +34,6 @@ import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target; import java.lang.reflect.Field; -import java.lang.reflect.Method; import java.util.ArrayDeque; import java.util.Map; import java.util.Objects; @@ -74,21 +73,15 @@ public class TestableLooper { /** * Baklava introduces new {@link TestLooperManager} APIs that we can use instead of reflection. */ - private static boolean isAtLeastBaklava() { - Method[] methods = TestLooperManager.class.getMethods(); - for (Method method : methods) { - if (method.getName().equals("peekWhen")) { - return true; - } - } - return false; + private static boolean newTestabilityApisSupported() { + return android.os.Flags.messageQueueTestability(); // TODO(shayba): delete the above, uncomment the below. // SDK_INT has not yet ramped to Baklava in all 25Q2 builds. // return Build.VERSION.SDK_INT >= Build.VERSION_CODES.BAKLAVA; } static { - if (isAtLeastBaklava()) { + if (newTestabilityApisSupported()) { MESSAGE_QUEUE_MESSAGES_FIELD = null; MESSAGE_NEXT_FIELD = null; MESSAGE_WHEN_FIELD = null; @@ -248,14 +241,14 @@ public class TestableLooper { } public void moveTimeForward(long milliSeconds) { - if (isAtLeastBaklava()) { - moveTimeForwardBaklava(milliSeconds); + if (newTestabilityApisSupported()) { + moveTimeForwardModern(milliSeconds); } else { moveTimeForwardLegacy(milliSeconds); } } - private void moveTimeForwardBaklava(long milliSeconds) { + private void moveTimeForwardModern(long milliSeconds) { // Drain all Messages from the queue. Queue<Message> messages = new ArrayDeque<>(); while (true) { diff --git a/tests/utils/testutils/java/android/os/test/TestLooper.java b/tests/utils/testutils/java/android/os/test/TestLooper.java index 4d379e45a81a..c7a36dd3f9d8 100644 --- a/tests/utils/testutils/java/android/os/test/TestLooper.java +++ b/tests/utils/testutils/java/android/os/test/TestLooper.java @@ -65,19 +65,13 @@ public class TestLooper { private AutoDispatchThread mAutoDispatchThread; /** - * Baklava introduces new {@link TestLooperManager} APIs that we can use instead of reflection. + * Modern introduces new {@link TestLooperManager} APIs that we can use instead of reflection. */ - private static boolean isAtLeastBaklava() { - Method[] methods = TestLooperManager.class.getMethods(); - for (Method method : methods) { - if (method.getName().equals("peekWhen")) { - return true; - } - } - return false; + private static boolean newTestabilityApisSupported() { + return android.os.Flags.messageQueueTestability(); // TODO(shayba): delete the above, uncomment the below. - // SDK_INT has not yet ramped to Baklava in all 25Q2 builds. - // return Build.VERSION.SDK_INT >= Build.VERSION_CODES.BAKLAVA; + // SDK_INT has not yet ramped to Modern in all 25Q2 builds. + // return Build.VERSION.SDK_INT >= Build.VERSION_CODES.Modern; } static { @@ -87,7 +81,7 @@ public class TestLooper { THREAD_LOCAL_LOOPER_FIELD = Looper.class.getDeclaredField("sThreadLocal"); THREAD_LOCAL_LOOPER_FIELD.setAccessible(true); - if (isAtLeastBaklava()) { + if (newTestabilityApisSupported()) { MESSAGE_QUEUE_MESSAGES_FIELD = null; MESSAGE_NEXT_FIELD = null; MESSAGE_WHEN_FIELD = null; @@ -136,7 +130,7 @@ public class TestLooper { throw new RuntimeException("Reflection error constructing or accessing looper", e); } - if (isAtLeastBaklava()) { + if (newTestabilityApisSupported()) { mTestLooperManager = InstrumentationRegistry.getInstrumentation().acquireLooperManager(mLooper); } else { @@ -165,14 +159,14 @@ public class TestLooper { } public void moveTimeForward(long milliSeconds) { - if (isAtLeastBaklava()) { - moveTimeForwardBaklava(milliSeconds); + if (newTestabilityApisSupported()) { + moveTimeForwardModern(milliSeconds); } else { moveTimeForwardLegacy(milliSeconds); } } - private void moveTimeForwardBaklava(long milliSeconds) { + private void moveTimeForwardModern(long milliSeconds) { // Drain all Messages from the queue. Queue<Message> messages = new ArrayDeque<>(); while (true) { @@ -265,14 +259,14 @@ public class TestLooper { * @return true if there are pending messages in the message queue */ public boolean isIdle() { - if (isAtLeastBaklava()) { - return isIdleBaklava(); + if (newTestabilityApisSupported()) { + return isIdleModern(); } else { return isIdleLegacy(); } } - private boolean isIdleBaklava() { + private boolean isIdleModern() { Long when = mTestLooperManager.peekWhen(); return when != null && currentTime() >= when; } @@ -286,14 +280,14 @@ public class TestLooper { * @return the next message in the Looper's message queue or null if there is none */ public Message nextMessage() { - if (isAtLeastBaklava()) { - return nextMessageBaklava(); + if (newTestabilityApisSupported()) { + return nextMessageModern(); } else { return nextMessageLegacy(); } } - private Message nextMessageBaklava() { + private Message nextMessageModern() { if (isIdle()) { return mTestLooperManager.poll(); } else { @@ -314,14 +308,14 @@ public class TestLooper { * Asserts that there is a message in the queue */ public void dispatchNext() { - if (isAtLeastBaklava()) { - dispatchNextBaklava(); + if (newTestabilityApisSupported()) { + dispatchNextModern(); } else { dispatchNextLegacy(); } } - private void dispatchNextBaklava() { + private void dispatchNextModern() { assertTrue(isIdle()); Message msg = mTestLooperManager.poll(); if (msg == null) { |