Skip to content

Commit 5062735

Browse files
brettchabotcopybara-androidxtest
authored andcommitted
Replace now-unnecessary reflection from TestLooperManagerCompat.
Now that androidx.test's bazel build compiles against Android SDK 36, the new TestLooperManager APIs added in 36 cn be referenced directly. PiperOrigin-RevId: 792749810
1 parent 9413f3c commit 5062735

File tree

2 files changed

+6
-21
lines changed

2 files changed

+6
-21
lines changed

espresso/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ The following artifacts were released:
1616

1717
**Bug Fixes**
1818

19+
* Replace now-unnecessary reflection from TestLooperManagerCompat when using Android SDK 36 APIs
20+
1921
**New Features**
2022

2123
**Breaking Changes**

espresso/core/java/androidx/test/espresso/base/TestLooperManagerCompat.java

Lines changed: 4 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -26,27 +26,10 @@ final class TestLooperManagerCompat {
2626
private static Method messageQueueNextMethod;
2727
private static Field messageQueueHeadField;
2828
private static Method recycleUncheckedMethod;
29-
private static Method peekWhenMethod;
30-
private static Method blockedOnBarrierMethod;
31-
32-
private static boolean initTestLooperManager() {
33-
// TODO(b/112000181): update this check and remove reflection when compiling against Baklava
34-
if (VERSION.SDK_INT >= VERSION_CODES.VANILLA_ICE_CREAM) {
35-
try {
36-
peekWhenMethod = TestLooperManager.class.getDeclaredMethod("peekWhen");
37-
blockedOnBarrierMethod =
38-
TestLooperManager.class.getDeclaredMethod("isBlockedOnSyncBarrier");
39-
return true;
40-
} catch (ReflectiveOperationException e) {
41-
// fall through
42-
}
43-
}
44-
return false;
45-
}
4629

4730
static {
4831
try {
49-
if (!initTestLooperManager()) {
32+
if (VERSION.SDK_INT < VERSION_CODES.BAKLAVA) {
5033
messageQueueNextMethod = MessageQueue.class.getDeclaredMethod("next");
5134
messageQueueNextMethod.setAccessible(true);
5235
messageQueueHeadField = MessageQueue.class.getDeclaredField("mMessages");
@@ -76,7 +59,7 @@ private TestLooperManagerCompat(TestLooperManager testLooperManager) {
7659
}
7760

7861
static TestLooperManagerCompat acquire(Looper looper) {
79-
if (peekWhenMethod != null) {
62+
if (VERSION.SDK_INT >= VERSION_CODES.BAKLAVA) {
8063
// running on a newer Android version that has the supported TestLooperManagerCompat changes
8164
Checks.checkState(looper.isCurrentThread());
8265
TestLooperManager testLooperManager =
@@ -91,7 +74,7 @@ static TestLooperManagerCompat acquire(Looper looper) {
9174
Long peekWhen() {
9275
try {
9376
if (delegate != null) {
94-
return (Long) peekWhenMethod.invoke(delegate);
77+
return delegate.peekWhen();
9578
} else {
9679
Message msg = legacyPeek();
9780
if (msg != null && msg.getTarget() == null) {
@@ -131,7 +114,7 @@ void release() {
131114
boolean isBlockedOnSyncBarrier() {
132115
try {
133116
if (delegate != null) {
134-
return (boolean) blockedOnBarrierMethod.invoke(delegate);
117+
return delegate.isBlockedOnSyncBarrier();
135118
} else {
136119
Message msg = legacyPeek();
137120
return msg != null && msg.getTarget() == null;

0 commit comments

Comments
 (0)