-
Notifications
You must be signed in to change notification settings - Fork 316
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor Interrogator MessageQueue access to a TestLooperManagerCompa…
…t class. An upcoming TestLooperManager change will add the ability for Espresso's Interrogator class to use it directly, replacing the use of reflection to access MessageQueue internals. This commit prepares for this change by refactoring MessageQueue access to a TestLooperManagerCompat container class. A future change will enhance TestLooperManagerCompat to use the public TestLooperManager API when the Android runtime environment supports it. PiperOrigin-RevId: 706812503
- Loading branch information
1 parent
eb37e0e
commit 9c5afd7
Showing
7 changed files
with
163 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
espresso/core/java/androidx/test/espresso/base/TestLooperManagerCompat.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package androidx.test.espresso.base; | ||
|
||
import android.os.Looper; | ||
import android.os.Message; | ||
import android.os.MessageQueue; | ||
import androidx.annotation.Nullable; | ||
import androidx.test.internal.platform.reflect.ReflectiveField; | ||
import androidx.test.internal.platform.reflect.ReflectiveMethod; | ||
|
||
/** | ||
* Compat class that supports the {@link android.os.TestLooperManager} Baklava+ functionality on | ||
* older Android SDKs. | ||
* | ||
* <p>Unlike the real TestLooperManager this only supports being used from the Looper's thread. | ||
*/ | ||
class TestLooperManagerCompat { | ||
|
||
private static final ReflectiveMethod<Message> messageQueueNextMethod = | ||
new ReflectiveMethod<>(MessageQueue.class, "next"); | ||
|
||
private static final ReflectiveField<Message> messageQueueHeadField = | ||
new ReflectiveField<>(MessageQueue.class, "mMessages"); | ||
|
||
private static final ReflectiveMethod<Void> recycleUncheckedMethod = | ||
new ReflectiveMethod<>(Message.class, "recycleUnchecked"); | ||
private final MessageQueue queue; | ||
|
||
private TestLooperManagerCompat(MessageQueue queue) { | ||
this.queue = queue; | ||
} | ||
|
||
static TestLooperManagerCompat acquire(Looper looper) { | ||
return new TestLooperManagerCompat(looper.getQueue()); | ||
} | ||
|
||
@Nullable | ||
Long peekWhen() { | ||
Message msg = legacyPeek(); | ||
if (msg != null && msg.getTarget() == null) { | ||
return null; | ||
} | ||
return msg == null ? null : msg.getWhen(); | ||
} | ||
|
||
private @Nullable Message legacyPeek() { | ||
synchronized (queue) { | ||
return messageQueueHeadField.get(queue); | ||
} | ||
} | ||
|
||
void execute(Message message) { | ||
message.getTarget().dispatchMessage(message); | ||
} | ||
|
||
void release() { | ||
// ignore for now | ||
} | ||
|
||
boolean isBlockedOnSyncBarrier() { | ||
Message msg = legacyPeek(); | ||
return msg != null && msg.getTarget() == null; | ||
} | ||
|
||
Message next() { | ||
return messageQueueNextMethod.invoke(queue); | ||
} | ||
|
||
void recycle(Message m) { | ||
recycleUncheckedMethod.invoke(m); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.