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

Commit 12115255 authored by Sal Savage's avatar Sal Savage Committed by Automerger Merge Worker
Browse files

Merge "Address testAddressedPlayerChangedToSamePlayerId flake rate" am: c7932999 am: 290e7035

Original change: https://android-review.googlesource.com/c/platform/packages/apps/Bluetooth/+/1754983

Change-Id: Ic0484de1d6cab47e9572a021ef244f3389498996
parents 596a5f43 290e7035
Loading
Loading
Loading
Loading
+36 −0
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import android.bluetooth.BluetoothDevice;
import android.content.Intent;
import android.os.Handler;
import android.os.Looper;
import android.os.MessageQueue;

import androidx.test.InstrumentationRegistry;
import androidx.test.rule.ServiceTestRule;
@@ -245,6 +246,41 @@ public class TestUtils {
        });
    }

    /**
     * Wait for looper to become idle
     *
     * @param looper looper of interest
     */
    public static void waitForLooperToBeIdle(Looper looper) {
        class Idler implements MessageQueue.IdleHandler {
            private boolean mIdle = false;

            @Override
            public boolean queueIdle() {
                synchronized (this) {
                    mIdle = true;
                    notifyAll();
                }
                return false;
            }

            public synchronized void waitForIdle() {
                while (!mIdle) {
                    try {
                        wait();
                    } catch (InterruptedException e) {
                    }
                }
            }
        }

        Idler idle = new Idler();
        looper.getQueue().addIdleHandler(idle);
        // Ensure we are not Idle to begin with so the idle handler will run
        waitForLooperToFinishScheduledTask(looper);
        idle.waitForIdle();
    }

    /**
     * Run synchronously a runnable action on a looper.
     * The method will return after the action has been execution to completion.
+10 −2
Original line number Diff line number Diff line
@@ -979,6 +979,11 @@ public class AvrcpControllerStateMachineTest {
        // Set the addressed player so we can change to the same one
        mAvrcpStateMachine.sendMessage(
                AvrcpControllerStateMachine.MESSAGE_PROCESS_ADDRESSED_PLAYER_CHANGED, 1);

        // Wait until idle so Now Playing List is queried for, resolve it
        TestUtils.waitForLooperToBeIdle(mAvrcpStateMachine.getHandler().getLooper());
        mAvrcpStateMachine.sendMessage(
                AvrcpControllerStateMachine.MESSAGE_PROCESS_GET_FOLDER_ITEMS_OUT_OF_RANGE);
        TestUtils.waitForLooperToFinishScheduledTask(mAvrcpStateMachine.getHandler().getLooper());

        //Get the root of the device
@@ -1000,14 +1005,17 @@ public class AvrcpControllerStateMachineTest {
        mAvrcpStateMachine.sendMessage(AvrcpControllerStateMachine.MESSAGE_PROCESS_GET_PLAYER_ITEMS,
                testPlayers);

        // Wait for players to be processed
        // Wait until idle so Now Playing List is queried for again, resolve it again
        TestUtils.waitForLooperToBeIdle(mAvrcpStateMachine.getHandler().getLooper());
        mAvrcpStateMachine.sendMessage(
                AvrcpControllerStateMachine.MESSAGE_PROCESS_GET_FOLDER_ITEMS_OUT_OF_RANGE);
        TestUtils.waitForLooperToFinishScheduledTask(mAvrcpStateMachine.getHandler().getLooper());
        clearInvocations(mAvrcpControllerService);

        // Send an addressed player changed to the same player ID
        mAvrcpStateMachine.sendMessage(
                AvrcpControllerStateMachine.MESSAGE_PROCESS_ADDRESSED_PLAYER_CHANGED, 1);
        TestUtils.waitForLooperToFinishScheduledTask(mAvrcpStateMachine.getHandler().getLooper());
        TestUtils.waitForLooperToBeIdle(mAvrcpStateMachine.getHandler().getLooper());

        // Verify we make no assumptions about the player ID and still fetch metadata, play status
        // and now playing list (since player 1 supports it)