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

Commit a89925c4 authored by Hongwei Wang's avatar Hongwei Wang
Browse files

Wait for onPictureInPictureUiStateChanged callback

For ActivityThreadTests, there are chances that WM is trying to deliver
PipStateTransactionItem to the app after it's been destroyed by the test
itself. This transaction item is newly introduced from ag/26031127 and
it happens at the very beginning of the entering PiP animation.

Added a countdown latch to wait for the onPictureInPictureUiStateChanged
callback finishes.

Bug: 325128917
Flag: ENABLE_PIP_UI_STATE_CALLBACK_ON_ENTERING
Test: atest --iteration 10 FrameworksCoreTests:ActivityThreadTest
Change-Id: I29f9d6e42ec478023224eed6b138fabcd0bb2e31
parent 7e6717a3
Loading
Loading
Loading
Loading
+24 −0
Original line number Diff line number Diff line
@@ -40,6 +40,7 @@ import android.app.ActivityThread.ActivityClientRecord;
import android.app.Application;
import android.app.IApplicationThread;
import android.app.PictureInPictureParams;
import android.app.PictureInPictureUiState;
import android.app.ResourcesManager;
import android.app.servertransaction.ActivityConfigurationChangeItem;
import android.app.servertransaction.ActivityLifecycleItem;
@@ -706,6 +707,9 @@ public class ActivityThreadTest {
        final TestActivity activity = mActivityTestRule.launchActivity(startIntent);
        final ActivityThread activityThread = activity.getActivityThread();
        final ActivityClientRecord r = getActivityClientRecord(activity);
        if (android.app.Flags.enablePipUiStateCallbackOnEntering()) {
            activity.mPipUiStateLatch = new CountDownLatch(1);
        }

        InstrumentationRegistry.getInstrumentation().runOnMainSync(() -> {
            activityThread.handlePictureInPictureRequested(r);
@@ -940,6 +944,11 @@ public class ActivityThreadTest {
         * latch reaches 0.
         */
        volatile CountDownLatch mConfigLatch;
        /**
         * A latch used to notify tests that we're about to wait for the
         * onPictureInPictureUiStateChanged callback.
         */
        volatile CountDownLatch mPipUiStateLatch;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
@@ -974,6 +983,14 @@ public class ActivityThreadTest {
            if (getIntent().getBooleanExtra(PIP_REQUESTED_OVERRIDE_ENTER, false)) {
                enterPictureInPictureMode(new PictureInPictureParams.Builder().build());
                mPipEntered = true;
                // Await for onPictureInPictureUiStateChanged callback if applicable
                if (mPipUiStateLatch != null) {
                    try {
                        mPipUiStateLatch.await(TIMEOUT_SEC, TimeUnit.SECONDS);
                    } catch (InterruptedException e) {
                        throw new IllegalStateException(e);
                    }
                }
                return true;
            } else if (getIntent().getBooleanExtra(PIP_REQUESTED_OVERRIDE_SKIP, false)) {
                mPipEnterSkipped = true;
@@ -982,6 +999,13 @@ public class ActivityThreadTest {
            return super.onPictureInPictureRequested();
        }

        @Override
        public void onPictureInPictureUiStateChanged(PictureInPictureUiState pipState) {
            if (mPipUiStateLatch != null && pipState.isEnteringPip()) {
                mPipUiStateLatch.countDown();
            }
        }

        boolean pipRequested() {
            return mPipRequested;
        }