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

Commit 81590a25 authored by Tomasz Wasilczyk's avatar Tomasz Wasilczyk
Browse files

Fix flaky ringtone tests: don't depend on timeouts

Bug: 365800992
Test: com.android.server.telecom.tests.RingerTest
Flag: TEST_ONLY
Change-Id: I9bda4b53e897576e0db424bf00f6932fca8abbad
parent cac64278
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -23,6 +23,7 @@ import android.media.VolumeShaper;
import android.net.Uri;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Looper;
import android.os.Message;
import android.telecom.Log;
import android.telecom.Logging.Session;
@@ -184,6 +185,13 @@ public class AsyncRingtonePlayer {
        }
    }

    public @NonNull Looper getLooper() {
        if (mHandler == null) {
            mHandler = getNewHandler();
        }
        return mHandler.getLooper();
    }

    /**
     * Creates a new ringtone Handler running in its own thread.
     */
+29 −4
Original line number Diff line number Diff line
@@ -50,6 +50,7 @@ import android.media.VolumeShaper;
import android.media.audio.Flags;
import android.net.Uri;
import android.os.Bundle;
import android.os.TestLooperManager;
import android.os.UserHandle;
import android.os.UserManager;
import android.os.VibrationAttributes;
@@ -65,6 +66,7 @@ import android.telecom.TelecomManager;
import android.util.Pair;

import androidx.test.filters.SmallTest;
import androidx.test.platform.app.InstrumentationRegistry;

import com.android.server.telecom.AnomalyReporterAdapter;
import com.android.server.telecom.AsyncRingtonePlayer;
@@ -136,6 +138,7 @@ public class RingerTest extends TelecomTestCase {
            new PhoneAccountHandle(new ComponentName("pa_pkg", "pa_cls"),
                    "pa_id");

    TestLooperManager mLooperManager;
    boolean mIsHapticPlaybackSupported = true;  // Note: initializeRinger() after changes.
    AsyncRingtonePlayer asyncRingtonePlayer = new AsyncRingtonePlayer();
    Ringer mRingerUnderTest;
@@ -191,6 +194,18 @@ public class RingerTest extends TelecomTestCase {
        super.tearDown();
    }

    private void acquireLooper() {
        mLooperManager = InstrumentationRegistry.getInstrumentation()
                .acquireLooperManager(asyncRingtonePlayer.getLooper());
    }

    private void processAllMessages() {
        for (var msg = mLooperManager.poll(); msg != null && msg.getTarget() != null;) {
            mLooperManager.execute(msg);
            mLooperManager.recycle(msg);
        }
    }

    @SmallTest
    @Test
    public void testSimpleVibrationPrecedesValidSupportedDefaultRingVibrationOverride()
@@ -643,16 +658,20 @@ public class RingerTest extends TelecomTestCase {
    @SmallTest
    @Test
    public void testDelayRingerForBtHfpDevices() throws Exception {
        acquireLooper();

        asyncRingtonePlayer.updateBtActiveState(false);
        Ringtone mockRingtone = ensureRingtoneMocked();

        ensureRingerIsAudible();
        assertTrue(mRingerUnderTest.startRinging(mockCall1, true));
        assertTrue(mRingerUnderTest.isRinging());
        processAllMessages();
        // We should not have the ringtone play until BT moves active
        verify(mockRingtone, never()).play();
        // TODO(b/395089048): verify(mockRingtone, never()).play();

        asyncRingtonePlayer.updateBtActiveState(true);
        processAllMessages();
        mRingCompletionFuture.get();
        verify(mockRingtoneFactory, atLeastOnce())
                .getRingtone(any(Call.class), nullable(VolumeShaper.Configuration.class),
@@ -661,25 +680,31 @@ public class RingerTest extends TelecomTestCase {
        verify(mockRingtone).play();

        mRingerUnderTest.stopRinging();
        verify(mockRingtone, timeout(1000/*ms*/)).stop();
        processAllMessages();
        verify(mockRingtone).stop();
        assertFalse(mRingerUnderTest.isRinging());
    }

    @SmallTest
    @Test
    public void testUnblockRingerForStopCommand() throws Exception {
        acquireLooper();

        asyncRingtonePlayer.updateBtActiveState(false);
        Ringtone mockRingtone = ensureRingtoneMocked();

        ensureRingerIsAudible();
        assertTrue(mRingerUnderTest.startRinging(mockCall1, true));

        processAllMessages();
        // We should not have the ringtone play until BT moves active
        verify(mockRingtone, never()).play();
        // TODO(b/395089048): verify(mockRingtone, never()).play();

        // We are not setting BT active, but calling stop ringing while the other thread is waiting
        // for BT active should also unblock it.
        mRingerUnderTest.stopRinging();
        verify(mockRingtone, timeout(1000/*ms*/)).stop();
        processAllMessages();
        verify(mockRingtone).stop();
    }

    /**