Merge "TestLooperManager methods that read/mutate the queue should ensure that LooperHolder was already popped" into main
diff --git a/core/java/android/os/TestLooperManager.java b/core/java/android/os/TestLooperManager.java
index d451109..ddfa379 100644
--- a/core/java/android/os/TestLooperManager.java
+++ b/core/java/android/os/TestLooperManager.java
@@ -84,17 +84,8 @@
* interactions with it have completed.
*/
public Message next() {
- // Wait for the looper block to come up, to make sure we don't accidentally get
- // the message for the block.
- while (!mLooperIsMyLooper && !mLooperBlocked) {
- synchronized (this) {
- try {
- wait();
- } catch (InterruptedException e) {
- }
- }
- }
checkReleased();
+ waitForLooperHolder();
return mQueue.next();
}
@@ -110,6 +101,7 @@
@Nullable
public Message poll() {
checkReleased();
+ waitForLooperHolder();
return mQueue.pollForTest();
}
@@ -124,6 +116,7 @@
@Nullable
public Long peekWhen() {
checkReleased();
+ waitForLooperHolder();
return mQueue.peekWhenForTest();
}
@@ -133,6 +126,7 @@
@FlaggedApi(Flags.FLAG_MESSAGE_QUEUE_TESTABILITY)
public boolean isBlockedOnSyncBarrier() {
checkReleased();
+ waitForLooperHolder();
return mQueue.isBlockedOnSyncBarrier();
}
@@ -221,6 +215,23 @@
}
}
+ /**
+ * Waits until the Looper is blocked by the LooperHolder, if one was posted.
+ *
+ * After this method returns, it's guaranteed that the LooperHolder Message
+ * is not in the underlying queue.
+ */
+ private void waitForLooperHolder() {
+ while (!mLooperIsMyLooper && !mLooperBlocked) {
+ synchronized (this) {
+ try {
+ wait();
+ } catch (InterruptedException e) {
+ }
+ }
+ }
+ }
+
private class LooperHolder implements Runnable {
@Override
public void run() {