Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 94fc9dda authored by Shai Barack's avatar Shai Barack
Browse files

TestLooperManager methods that read/mutate the queue should ensure that...

TestLooperManager methods that read/mutate the queue should ensure that LooperHolder was already popped

Bug: 385010271 
Change-Id: I632ac5cdf77248550a394a1d79a4f0f72a2053d8
Flag: android.os.message_queue_testability
parent 0af50eb3
Loading
Loading
Loading
Loading
+21 −10
Original line number Diff line number Diff line
@@ -84,17 +84,8 @@ public class TestLooperManager {
     * 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 @@ public class TestLooperManager {
    @Nullable
    public Message poll() {
        checkReleased();
        waitForLooperHolder();
        return mQueue.pollForTest();
    }

@@ -124,6 +116,7 @@ public class TestLooperManager {
    @Nullable
    public Long peekWhen() {
        checkReleased();
        waitForLooperHolder();
        return mQueue.peekWhenForTest();
    }

@@ -133,6 +126,7 @@ public class TestLooperManager {
    @FlaggedApi(Flags.FLAG_MESSAGE_QUEUE_TESTABILITY)
    public boolean isBlockedOnSyncBarrier() {
        checkReleased();
        waitForLooperHolder();
        return mQueue.isBlockedOnSyncBarrier();
    }

@@ -221,6 +215,23 @@ public class TestLooperManager {
        }
    }

    /**
     * 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() {