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

Commit ee595457 authored by Evan Rosky's avatar Evan Rosky
Browse files

Fix Transition test logic and consolidate some boilerplate

Moved things to use TransitionInfoBuilder instead of constructing
TransitionInfos directly.

Also make the testapi for ChangeInfo properly populate itself based
on the container. This exposed some incorrectness in the tests, so
fixed those as well

Bug: 261418859
Test: TransitionTests ShellTransitionTests
Change-Id: Ibe1945986ac0342fae0df0f952c727821e4a60a2
parent b4cf7ff0
Loading
Loading
Loading
Loading
+78 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.wm.shell;

import static com.android.dx.mockito.inline.extended.ExtendedMockito.doReturn;

import static org.mockito.Mockito.mock;

import android.app.ActivityManager;
import android.view.SurfaceControl;
import android.view.WindowManager;
import android.window.TransitionInfo;

/**
 * Utility for creating/editing synthetic TransitionInfos for tests.
 */
public class TransitionInfoBuilder {
    final TransitionInfo mInfo;

    public TransitionInfoBuilder(@WindowManager.TransitionType int type) {
        this(type, 0 /* flags */);
    }

    public TransitionInfoBuilder(@WindowManager.TransitionType int type,
            @WindowManager.TransitionFlags int flags) {
        mInfo = new TransitionInfo(type, flags);
        mInfo.setRootLeash(createMockSurface(true /* valid */), 0, 0);
    }

    public TransitionInfoBuilder addChange(@WindowManager.TransitionType int mode,
            @TransitionInfo.ChangeFlags int flags, ActivityManager.RunningTaskInfo taskInfo) {
        final TransitionInfo.Change change = new TransitionInfo.Change(
                taskInfo != null ? taskInfo.token : null, createMockSurface(true /* valid */));
        change.setMode(mode);
        change.setFlags(flags);
        change.setTaskInfo(taskInfo);
        return addChange(change);
    }

    public TransitionInfoBuilder addChange(@WindowManager.TransitionType int mode,
            ActivityManager.RunningTaskInfo taskInfo) {
        return addChange(mode, TransitionInfo.FLAG_NONE, taskInfo);
    }

    public TransitionInfoBuilder addChange(@WindowManager.TransitionType int mode) {
        return addChange(mode, TransitionInfo.FLAG_NONE, null /* taskInfo */);
    }

    public TransitionInfoBuilder addChange(TransitionInfo.Change change) {
        mInfo.addChange(change);
        return this;
    }

    public TransitionInfo build() {
        return mInfo;
    }

    private static SurfaceControl createMockSurface(boolean valid) {
        SurfaceControl sc = mock(SurfaceControl.class);
        doReturn(valid).when(sc).isValid();
        doReturn("TestSurface").when(sc).toString();
        return sc;
    }
}
+8 −8
Original line number Diff line number Diff line
@@ -35,6 +35,8 @@ import android.window.TransitionInfo;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.SmallTest;

import com.android.wm.shell.TransitionInfoBuilder;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -60,10 +62,9 @@ public class ActivityEmbeddingAnimationRunnerTests extends ActivityEmbeddingAnim

    @Test
    public void testStartAnimation() {
        final TransitionInfo info = new TransitionInfo(TRANSIT_OPEN, 0);
        final TransitionInfo.Change embeddingChange = createChange();
        embeddingChange.setFlags(FLAG_IN_TASK_WITH_EMBEDDED_ACTIVITY);
        info.addChange(embeddingChange);
        final TransitionInfo info = new TransitionInfoBuilder(TRANSIT_OPEN, 0)
                .addChange(createChange(FLAG_IN_TASK_WITH_EMBEDDED_ACTIVITY))
                .build();
        doReturn(mAnimator).when(mAnimRunner).createAnimator(any(), any(), any(), any(), any());

        mAnimRunner.startAnimation(mTransition, info, mStartTransaction, mFinishTransaction);
@@ -84,10 +85,9 @@ public class ActivityEmbeddingAnimationRunnerTests extends ActivityEmbeddingAnim

    @Test
    public void testChangesBehindStartingWindow() {
        final TransitionInfo info = new TransitionInfo(TRANSIT_OPEN, 0);
        final TransitionInfo.Change embeddingChange = createChange();
        embeddingChange.setFlags(FLAG_IS_BEHIND_STARTING_WINDOW);
        info.addChange(embeddingChange);
        final TransitionInfo info = new TransitionInfoBuilder(TRANSIT_OPEN, 0)
                .addChange(createChange(FLAG_IS_BEHIND_STARTING_WINDOW))
                .build();
        final Animator animator = mAnimRunner.createAnimator(
                info, mStartTransaction, mFinishTransaction,
                () -> mFinishCallback.onTransitionFinished(null /* wct */, null /* wctCB */),
+5 −4
Original line number Diff line number Diff line
@@ -82,9 +82,11 @@ abstract class ActivityEmbeddingAnimationTestBase extends ShellTestCase {
    }

    /** Creates a mock {@link TransitionInfo.Change}. */
    static TransitionInfo.Change createChange() {
        return new TransitionInfo.Change(mock(WindowContainerToken.class),
    static TransitionInfo.Change createChange(@TransitionInfo.ChangeFlags int flags) {
        TransitionInfo.Change c = new TransitionInfo.Change(mock(WindowContainerToken.class),
                mock(SurfaceControl.class));
        c.setFlags(flags);
        return c;
    }

    /**
@@ -93,8 +95,7 @@ abstract class ActivityEmbeddingAnimationTestBase extends ShellTestCase {
     */
    static TransitionInfo.Change createEmbeddedChange(@NonNull Rect startBounds,
            @NonNull Rect endBounds, @NonNull Rect taskBounds) {
        final TransitionInfo.Change change = createChange();
        change.setFlags(FLAG_IN_TASK_WITH_EMBEDDED_ACTIVITY);
        final TransitionInfo.Change change = createChange(FLAG_IN_TASK_WITH_EMBEDDED_ACTIVITY);
        change.setStartAbsBounds(startBounds);
        change.setEndAbsBounds(endBounds);
        if (taskBounds.width() == startBounds.width()
+24 −26
Original line number Diff line number Diff line
@@ -34,6 +34,8 @@ import android.window.TransitionInfo;
import androidx.test.ext.junit.runners.AndroidJUnit4;
import androidx.test.filters.SmallTest;

import com.android.wm.shell.TransitionInfoBuilder;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -80,12 +82,11 @@ public class ActivityEmbeddingControllerTests extends ActivityEmbeddingAnimation

    @Test
    public void testStartAnimation_containsNonActivityEmbeddingChange() {
        final TransitionInfo info = new TransitionInfo(TRANSIT_OPEN, 0);
        final TransitionInfo.Change embeddingChange = createEmbeddedChange(EMBEDDED_LEFT_BOUNDS,
                EMBEDDED_LEFT_BOUNDS, TASK_BOUNDS);
        final TransitionInfo.Change nonEmbeddingChange = createChange();
        info.addChange(embeddingChange);
        info.addChange(nonEmbeddingChange);
        final TransitionInfo info = new TransitionInfoBuilder(TRANSIT_OPEN, 0)
                .addChange(createEmbeddedChange(
                        EMBEDDED_LEFT_BOUNDS, EMBEDDED_LEFT_BOUNDS, TASK_BOUNDS))
                .addChange(createChange(0 /* flags */))
                .build();

        // No-op because it contains non-embedded change.
        assertFalse(mController.startAnimation(mTransition, info, mStartTransaction,
@@ -98,10 +99,9 @@ public class ActivityEmbeddingControllerTests extends ActivityEmbeddingAnimation

    @Test
    public void testStartAnimation_containsOnlyFillTaskActivityEmbeddingChange() {
        final TransitionInfo info = new TransitionInfo(TRANSIT_OPEN, 0);
        final TransitionInfo.Change embeddingChange = createEmbeddedChange(TASK_BOUNDS, TASK_BOUNDS,
                TASK_BOUNDS);
        info.addChange(embeddingChange);
        final TransitionInfo info = new TransitionInfoBuilder(TRANSIT_OPEN, 0)
                .addChange(createEmbeddedChange(TASK_BOUNDS, TASK_BOUNDS, TASK_BOUNDS))
                .build();

        // No-op because it only contains embedded change that fills the Task. We will let the
        // default handler to animate such transition.
@@ -116,10 +116,10 @@ public class ActivityEmbeddingControllerTests extends ActivityEmbeddingAnimation
    @Test
    public void testStartAnimation_containsActivityEmbeddingSplitChange() {
        // Change that occupies only part of the Task.
        final TransitionInfo info = new TransitionInfo(TRANSIT_OPEN, 0);
        final TransitionInfo.Change embeddingChange = createEmbeddedChange(EMBEDDED_LEFT_BOUNDS,
                EMBEDDED_LEFT_BOUNDS, TASK_BOUNDS);
        info.addChange(embeddingChange);
        final TransitionInfo info = new TransitionInfoBuilder(TRANSIT_OPEN, 0)
                .addChange(createEmbeddedChange(
                        EMBEDDED_LEFT_BOUNDS, EMBEDDED_LEFT_BOUNDS, TASK_BOUNDS))
                .build();

        // ActivityEmbeddingController will handle such transition.
        assertTrue(mController.startAnimation(mTransition, info, mStartTransaction,
@@ -133,10 +133,9 @@ public class ActivityEmbeddingControllerTests extends ActivityEmbeddingAnimation
    @Test
    public void testStartAnimation_containsChangeEnterActivityEmbeddingSplit() {
        // Change that is entering ActivityEmbedding split.
        final TransitionInfo info = new TransitionInfo(TRANSIT_OPEN, 0);
        final TransitionInfo.Change embeddingChange = createEmbeddedChange(TASK_BOUNDS,
                EMBEDDED_LEFT_BOUNDS, TASK_BOUNDS);
        info.addChange(embeddingChange);
        final TransitionInfo info = new TransitionInfoBuilder(TRANSIT_OPEN, 0)
                .addChange(createEmbeddedChange(TASK_BOUNDS, EMBEDDED_LEFT_BOUNDS, TASK_BOUNDS))
                .build();

        // ActivityEmbeddingController will handle such transition.
        assertTrue(mController.startAnimation(mTransition, info, mStartTransaction,
@@ -150,10 +149,9 @@ public class ActivityEmbeddingControllerTests extends ActivityEmbeddingAnimation
    @Test
    public void testStartAnimation_containsChangeExitActivityEmbeddingSplit() {
        // Change that is exiting ActivityEmbedding split.
        final TransitionInfo info = new TransitionInfo(TRANSIT_OPEN, 0);
        final TransitionInfo.Change embeddingChange = createEmbeddedChange(EMBEDDED_RIGHT_BOUNDS,
                TASK_BOUNDS, TASK_BOUNDS);
        info.addChange(embeddingChange);
        final TransitionInfo info = new TransitionInfoBuilder(TRANSIT_OPEN, 0)
                .addChange(createEmbeddedChange(EMBEDDED_RIGHT_BOUNDS, TASK_BOUNDS, TASK_BOUNDS))
                .build();

        // ActivityEmbeddingController will handle such transition.
        assertTrue(mController.startAnimation(mTransition, info, mStartTransaction,
@@ -170,10 +168,10 @@ public class ActivityEmbeddingControllerTests extends ActivityEmbeddingAnimation
        assertThrows(IllegalStateException.class,
                () -> mController.onAnimationFinished(mTransition));

        final TransitionInfo info = new TransitionInfo(TRANSIT_OPEN, 0);
        final TransitionInfo.Change embeddingChange = createEmbeddedChange(EMBEDDED_LEFT_BOUNDS,
                EMBEDDED_LEFT_BOUNDS, TASK_BOUNDS);
        info.addChange(embeddingChange);
        final TransitionInfo info = new TransitionInfoBuilder(TRANSIT_OPEN, 0)
                .addChange(createEmbeddedChange(
                        EMBEDDED_LEFT_BOUNDS, EMBEDDED_LEFT_BOUNDS, TASK_BOUNDS))
                .build();
        mController.startAnimation(mTransition, info, mStartTransaction,
                mFinishTransaction, mFinishCallback);

+17 −16
Original line number Diff line number Diff line
@@ -37,6 +37,7 @@ import android.window.WindowContainerToken;

import androidx.test.filters.SmallTest;

import com.android.wm.shell.TransitionInfoBuilder;
import com.android.wm.shell.sysui.ShellInit;
import com.android.wm.shell.transition.Transitions;
import com.android.wm.shell.windowdecor.WindowDecorViewModel;
@@ -94,8 +95,8 @@ public class FreeformTaskTransitionObserverTest {
    public void testCreatesWindowDecorOnOpenTransition_freeform() {
        final TransitionInfo.Change change =
                createChange(TRANSIT_OPEN, 1, WINDOWING_MODE_FREEFORM);
        final TransitionInfo info = new TransitionInfo(TRANSIT_OPEN, 0);
        info.addChange(change);
        final TransitionInfo info = new TransitionInfoBuilder(TRANSIT_OPEN, 0)
                .addChange(change).build();

        final IBinder transition = mock(IBinder.class);
        final SurfaceControl.Transaction startT = mock(SurfaceControl.Transaction.class);
@@ -111,8 +112,8 @@ public class FreeformTaskTransitionObserverTest {
    public void testPreparesWindowDecorOnCloseTransition_freeform() {
        final TransitionInfo.Change change =
                createChange(TRANSIT_CLOSE, 1, WINDOWING_MODE_FREEFORM);
        final TransitionInfo info = new TransitionInfo(TRANSIT_CLOSE, 0);
        info.addChange(change);
        final TransitionInfo info = new TransitionInfoBuilder(TRANSIT_CLOSE, 0)
                .addChange(change).build();

        final IBinder transition = mock(IBinder.class);
        final SurfaceControl.Transaction startT = mock(SurfaceControl.Transaction.class);
@@ -128,8 +129,8 @@ public class FreeformTaskTransitionObserverTest {
    public void testDoesntCloseWindowDecorDuringCloseTransition() throws Exception {
        final TransitionInfo.Change change =
                createChange(TRANSIT_CLOSE, 1, WINDOWING_MODE_FREEFORM);
        final TransitionInfo info = new TransitionInfo(TRANSIT_CLOSE, 0);
        info.addChange(change);
        final TransitionInfo info = new TransitionInfoBuilder(TRANSIT_CLOSE, 0)
                .addChange(change).build();

        final IBinder transition = mock(IBinder.class);
        final SurfaceControl.Transaction startT = mock(SurfaceControl.Transaction.class);
@@ -144,8 +145,8 @@ public class FreeformTaskTransitionObserverTest {
    public void testClosesWindowDecorAfterCloseTransition() throws Exception {
        final TransitionInfo.Change change =
                createChange(TRANSIT_CLOSE, 1, WINDOWING_MODE_FREEFORM);
        final TransitionInfo info = new TransitionInfo(TRANSIT_CLOSE, 0);
        info.addChange(change);
        final TransitionInfo info = new TransitionInfoBuilder(TRANSIT_CLOSE, 0)
                .addChange(change).build();

        final AutoCloseable windowDecor = mock(AutoCloseable.class);

@@ -164,8 +165,8 @@ public class FreeformTaskTransitionObserverTest {
        // The playing transition
        final TransitionInfo.Change change1 =
                createChange(TRANSIT_OPEN, 1, WINDOWING_MODE_FREEFORM);
        final TransitionInfo info1 = new TransitionInfo(TRANSIT_OPEN, 0);
        info1.addChange(change1);
        final TransitionInfo info1 = new TransitionInfoBuilder(TRANSIT_OPEN, 0)
                .addChange(change1).build();

        final IBinder transition1 = mock(IBinder.class);
        final SurfaceControl.Transaction startT1 = mock(SurfaceControl.Transaction.class);
@@ -176,8 +177,8 @@ public class FreeformTaskTransitionObserverTest {
        // The merged transition
        final TransitionInfo.Change change2 =
                createChange(TRANSIT_CLOSE, 2, WINDOWING_MODE_FREEFORM);
        final TransitionInfo info2 = new TransitionInfo(TRANSIT_CLOSE, 0);
        info2.addChange(change2);
        final TransitionInfo info2 = new TransitionInfoBuilder(TRANSIT_CLOSE, 0)
                .addChange(change2).build();

        final IBinder transition2 = mock(IBinder.class);
        final SurfaceControl.Transaction startT2 = mock(SurfaceControl.Transaction.class);
@@ -195,8 +196,8 @@ public class FreeformTaskTransitionObserverTest {
        // The playing transition
        final TransitionInfo.Change change1 =
                createChange(TRANSIT_CLOSE, 1, WINDOWING_MODE_FREEFORM);
        final TransitionInfo info1 = new TransitionInfo(TRANSIT_CLOSE, 0);
        info1.addChange(change1);
        final TransitionInfo info1 = new TransitionInfoBuilder(TRANSIT_CLOSE, 0)
                .addChange(change1).build();

        final IBinder transition1 = mock(IBinder.class);
        final SurfaceControl.Transaction startT1 = mock(SurfaceControl.Transaction.class);
@@ -207,8 +208,8 @@ public class FreeformTaskTransitionObserverTest {
        // The merged transition
        final TransitionInfo.Change change2 =
                createChange(TRANSIT_CLOSE, 2, WINDOWING_MODE_FREEFORM);
        final TransitionInfo info2 = new TransitionInfo(TRANSIT_CLOSE, 0);
        info2.addChange(change2);
        final TransitionInfo info2 = new TransitionInfoBuilder(TRANSIT_CLOSE, 0)
                .addChange(change2).build();

        final IBinder transition2 = mock(IBinder.class);
        final SurfaceControl.Transaction startT2 = mock(SurfaceControl.Transaction.class);
Loading