/*
 * Copyright (C) 2016 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.server.wm;

import static android.app.WindowConfiguration.ACTIVITY_TYPE_ASSISTANT;
import static android.app.WindowConfiguration.ACTIVITY_TYPE_HOME;
import static android.app.WindowConfiguration.ACTIVITY_TYPE_STANDARD;
import static android.app.WindowConfiguration.WINDOWING_MODE_FULLSCREEN;
import static android.app.WindowConfiguration.WINDOWING_MODE_MULTI_WINDOW;
import static android.app.WindowConfiguration.WINDOWING_MODE_PINNED;
import static android.view.WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM;
import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION;
import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_ABOVE_SUB_PANEL;
import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_MEDIA;
import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_MEDIA_OVERLAY;
import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_PANEL;
import static android.view.WindowManager.LayoutParams.TYPE_APPLICATION_SUB_PANEL;
import static android.view.WindowManager.LayoutParams.TYPE_BASE_APPLICATION;
import static android.view.WindowManager.LayoutParams.TYPE_NAVIGATION_BAR_PANEL;
import static android.view.WindowManager.LayoutParams.TYPE_SECURE_SYSTEM_OVERLAY;
import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR_ADDITIONAL;
import static android.view.WindowManager.LayoutParams.TYPE_STATUS_BAR_SUB_PANEL;
import static android.view.WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY;

import static com.android.dx.mockito.inline.extended.ExtendedMockito.doReturn;
import static com.android.dx.mockito.inline.extended.ExtendedMockito.spyOn;
import static com.android.dx.mockito.inline.extended.ExtendedMockito.verify;
import static com.android.server.wm.WindowStateAnimator.PRESERVED_SURFACE_LAYER;

import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertWithMessage;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;

import android.graphics.PixelFormat;
import android.graphics.Rect;
import android.platform.test.annotations.Presubmit;
import android.view.SurfaceControl;
import android.window.ScreenCaptureInternal;

import androidx.test.filters.SmallTest;

import org.junit.After;
import org.junit.Test;
import org.junit.runner.RunWith;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.function.Supplier;

/**
 * Tests for the {@link DisplayContent#assignChildLayers(SurfaceControl.Transaction)} method.
 *
 * Build/Install/Run:
 *  atest WmTests:ZOrderingTests
 */
@SmallTest
@Presubmit
@WindowTestsBase.UseTestDisplay(addAllCommonWindows = true)
@RunWith(WindowTestRunner.class)
public class ZOrderingTests extends WindowTestsBase {

    private static class LayerRecordingTransaction extends SurfaceControl.Transaction {
        // We have WM use our Hierarchy recording subclass of SurfaceControl.Builder
        // such that we can keep track of the parents of Surfaces as they are constructed.
        private final HashMap<SurfaceControl, SurfaceControl> mParentFor = new HashMap<>();
        HashMap<SurfaceControl, Integer> mLayersForControl = new HashMap<>();
        HashMap<SurfaceControl, SurfaceControl> mRelativeLayersForControl = new HashMap<>();

        @Override
        public SurfaceControl.Transaction setLayer(SurfaceControl sc, int layer) {
            mRelativeLayersForControl.remove(sc);
            mLayersForControl.put(sc, layer);
            return this;
        }

        @Override
        public SurfaceControl.Transaction setRelativeLayer(SurfaceControl sc,
                SurfaceControl relativeTo,
                int layer) {
            mRelativeLayersForControl.put(sc, relativeTo);
            mLayersForControl.put(sc, layer);
            return this;
        }

        private int getLayer(SurfaceControl sc) {
            return mLayersForControl.getOrDefault(sc, 0);
        }

        private SurfaceControl getRelativeLayer(SurfaceControl sc) {
            return mRelativeLayersForControl.get(sc);
        }

        void addParentFor(SurfaceControl child, SurfaceControl parent) {
            mParentFor.put(child, parent);
        }

        SurfaceControl getParentFor(SurfaceControl child) {
            return mParentFor.get(child);
        }

        @Override
        public void close() {

        }
    }

    private static class HierarchyRecorder extends SurfaceControl.Builder {
        private LayerRecordingTransaction mTransaction;
        private SurfaceControl mPendingParent;

        HierarchyRecorder(LayerRecordingTransaction transaction) {
            mTransaction = transaction;
        }

        @Override
        public SurfaceControl.Builder setParent(SurfaceControl sc) {
            mPendingParent = sc;
            return super.setParent(sc);
        }

        @Override
        public SurfaceControl build() {
            final SurfaceControl sc = super.build();
            mTransaction.addParentFor(sc, mPendingParent);
            mPendingParent = null;
            return sc;
        }
    }

    private static class HierarchyRecordingBuilderFactory
            implements Supplier<SurfaceControl.Builder> {
        private LayerRecordingTransaction mTransaction;

        HierarchyRecordingBuilderFactory(LayerRecordingTransaction transaction) {
            mTransaction = transaction;
        }

        @Override
        public SurfaceControl.Builder get() {
            return new HierarchyRecorder(mTransaction);
        }
    }

    private LayerRecordingTransaction mTransaction;

    @Override
    void beforeCreateTestDisplay() {
        // We can't use @Before here because it may happen after WindowTestsBase @Before
        // which is after construction of the DisplayContent, meaning the HierarchyRecorder
        // would miss construction of the top-level layers.
        mTransaction = new LayerRecordingTransaction();
        mWm.mSurfaceControlFactory = new HierarchyRecordingBuilderFactory(mTransaction);
        mWm.mTransactionFactory = () -> mTransaction;
    }

    @After
    public void tearDown() {
        mTransaction.close();
    }

    private static LinkedList<SurfaceControl> getAncestors(LayerRecordingTransaction t,
            SurfaceControl sc) {
        LinkedList<SurfaceControl> p = new LinkedList<>();
        SurfaceControl current = sc;
        do {
            p.addLast(current);

            SurfaceControl rs = t.getRelativeLayer(current);
            if (rs != null) {
                current = rs;
            } else {
                current = t.getParentFor(current);
            }
        } while (current != null);
        return p;
    }


    private static void assertZOrderGreaterThan(LayerRecordingTransaction t, SurfaceControl left,
            SurfaceControl right) {
        final LinkedList<SurfaceControl> leftParentChain = getAncestors(t, left);
        final LinkedList<SurfaceControl> rightParentChain = getAncestors(t, right);

        SurfaceControl leftTop = leftParentChain.peekLast();
        SurfaceControl rightTop = rightParentChain.peekLast();
        while (leftTop != null && rightTop != null && leftTop == rightTop) {
            leftParentChain.removeLast();
            rightParentChain.removeLast();
            leftTop = leftParentChain.peekLast();
            rightTop = rightParentChain.peekLast();
        }

        if (rightTop == null) { // right is the parent of left.
            assertThat(t.getLayer(leftTop)).isGreaterThan(0);
        } else if (leftTop == null) { // left is the parent of right.
            assertThat(t.getLayer(rightTop)).isLessThan(0);
        } else {
            assertThat(t.getLayer(leftTop)).isGreaterThan(t.getLayer(rightTop));
        }
    }

    void assertWindowHigher(WindowState left, WindowState right) {
        assertZOrderGreaterThan(mTransaction, left.getSurfaceControl(), right.getSurfaceControl());
    }

    WindowState createWindow(String name) {
        return newWindowBuilder(name, TYPE_BASE_APPLICATION).setDisplay(mDisplayContent).build();
    }

    @Test
    public void testAssignWindowLayers_ForImeWithNoTarget() {
        mDisplayContent.setImeLayeringTarget(null /* target */);
        mDisplayContent.assignChildLayers(mTransaction);

        // The Ime has an higher base layer than app windows and lower base layer than system
        // windows, so it should be above app windows and below system windows if there isn't an IME
        // target.
        assertWindowHigher(mImeWindow, mChildAppWindowAbove);
        assertWindowHigher(mImeWindow, mAppWindow);
        assertWindowHigher(mNavBarWindow, mImeWindow);
        assertWindowHigher(mStatusBarWindow, mImeWindow);

        // And, IME dialogs should always have an higher layer than the IME.
        assertWindowHigher(mImeDialogWindow, mImeWindow);
    }

    @Test
    public void testAssignWindowLayers_ForImeWithAppTarget() {
        final WindowState imeAppTarget = createWindow("imeAppTarget");
        mDisplayContent.setImeLayeringTarget(imeAppTarget);

        mDisplayContent.assignChildLayers(mTransaction);

        // Ime should be above all app windows and below system windows if it is targeting an app
        // window.
        assertWindowHigher(mImeWindow, imeAppTarget);
        assertWindowHigher(mImeWindow, mChildAppWindowAbove);
        assertWindowHigher(mImeWindow, mAppWindow);
        assertWindowHigher(mNavBarWindow, mImeWindow);
        assertWindowHigher(mStatusBarWindow, mImeWindow);

        // And, IME dialogs should always have an higher layer than the IME.
        assertWindowHigher(mImeDialogWindow, mImeWindow);
    }

    @Test
    public void testAssignWindowLayers_ForImeWithAppTargetWithChildWindows() {
        final WindowState imeAppTarget = createWindow("imeAppTarget");
        final WindowState imeAppTargetChildAboveWindow = newWindowBuilder(
                "imeAppTargetChildAboveWindow", TYPE_APPLICATION_ATTACHED_DIALOG).setParent(
                imeAppTarget).setWindowToken(imeAppTarget.mToken).build();
        final WindowState imeAppTargetChildBelowWindow = newWindowBuilder(
                "imeAppTargetChildBelowWindow", TYPE_APPLICATION_MEDIA_OVERLAY).setParent(
                imeAppTarget).setWindowToken(imeAppTarget.mToken).build();

        mDisplayContent.setImeLayeringTarget(imeAppTarget);
        makeWindowVisible(mImeWindow);
        mDisplayContent.assignChildLayers(mTransaction);

        // Ime should be above all app windows except for child windows that are z-ordered above it
        // and below system windows if it is targeting an app window.
        assertWindowHigher(mImeWindow, imeAppTarget);
        assertWindowHigher(imeAppTargetChildAboveWindow, mImeWindow);
        assertWindowHigher(mImeWindow, mChildAppWindowAbove);
        assertWindowHigher(mImeWindow, mAppWindow);
        assertWindowHigher(mNavBarWindow, mImeWindow);
        assertWindowHigher(mStatusBarWindow, mImeWindow);

        // And, IME dialogs should always have an higher layer than the IME.
        assertWindowHigher(mImeDialogWindow, mImeWindow);
    }

    @Test
    public void testAssignWindowLayers_ForImeWithAppTargetAndAppAbove() {
        final WindowState appBelowImeTarget = createWindow("appBelowImeTarget");
        final WindowState imeAppTarget = createWindow("imeAppTarget");
        final WindowState appAboveImeTarget = createWindow("appAboveImeTarget");

        mDisplayContent.setImeInputTarget(imeAppTarget);
        mDisplayContent.setImeLayeringTarget(imeAppTarget);
        assertWithMessage("IME control target was updated")
                .that(mDisplayContent.getImeControlTarget()).isEqualTo(imeAppTarget);
        mDisplayContent.mTransitionController.mBuildingTransitionLayers = true;
        mDisplayContent.assignChildLayers(mTransaction);

        // Ime should be above all app windows except for non-fullscreen app window above it and
        // below system windows if it is targeting an app window.
        assertWindowHigher(mImeWindow, imeAppTarget);
        assertWindowHigher(mImeWindow, appBelowImeTarget);
        assertWindowHigher(appAboveImeTarget, mImeWindow);
        assertWindowHigher(mImeWindow, mChildAppWindowAbove);
        assertWindowHigher(mImeWindow, mAppWindow);
        assertWindowHigher(mNavBarWindow, mImeWindow);
        assertWindowHigher(mStatusBarWindow, mImeWindow);

        // And, IME dialogs should always have an higher layer than the IME.
        assertWindowHigher(mImeDialogWindow, mImeWindow);
    }

    @Test
    public void testAssignWindowLayers_ForImeNonAppImeLayeringTarget() {
        final WindowState imeSystemOverlayTarget = newWindowBuilder("imeSystemOverlayTarget",
                TYPE_SYSTEM_OVERLAY).setDisplay(mDisplayContent).setOwnerCanAddInternalSystemWindow(
                true).build();

        mDisplayContent.setImeLayeringTarget(imeSystemOverlayTarget);
        mDisplayContent.assignChildLayers(mTransaction);

        // The IME layering target base layer is higher than all window except for the nav bar
        // window, so the IME should be above all windows except for the nav bar.
        assertWindowHigher(mImeWindow, imeSystemOverlayTarget);
        assertWindowHigher(mImeWindow, mChildAppWindowAbove);
        assertWindowHigher(mImeWindow, mAppWindow);

        // The IME has a higher base layer than the status bar so we may expect it to go
        // above the status bar once they are both in the Non-App layer, as past versions of this
        // test enforced. However this seems like the wrong behavior unless the status bar is the
        // IME layering target.
        assertWindowHigher(mNavBarWindow, mImeWindow);
        assertWindowHigher(mStatusBarWindow, mImeWindow);

        // And, IME dialogs should always have an higher layer than the IME.
        assertWindowHigher(mImeDialogWindow, mImeWindow);
    }

    @Test
    public void testAssignWindowLayers_ForStatusBarImeTarget() {
        mDisplayContent.setImeInputTarget(mStatusBarWindow);
        mDisplayContent.setImeLayeringTarget(mStatusBarWindow);
        assertWithMessage("IME control target was updated")
                .that(mDisplayContent.getImeControlTarget()).isEqualTo(mStatusBarWindow);
        mDisplayContent.assignChildLayers(mTransaction);

        assertWindowHigher(mImeWindow, mChildAppWindowAbove);
        assertWindowHigher(mImeWindow, mAppWindow);
        assertWindowHigher(mImeWindow, mStatusBarWindow);

        // And, IME dialogs should always have an higher layer than the IME.
        assertWindowHigher(mImeDialogWindow, mImeWindow);
    }

    @Test
    public void testStackLayers() {
        final WindowState anyWindow1 = createWindow("anyWindow");
        final WindowState pinnedStackWindow = newWindowBuilder("pinnedStackWindow",
                TYPE_BASE_APPLICATION).setWindowingMode(WINDOWING_MODE_PINNED).setActivityType(
                ACTIVITY_TYPE_STANDARD).setDisplay(mDisplayContent).build();
        final WindowState dockedStackWindow = newWindowBuilder("dockedStackWindow",
                TYPE_BASE_APPLICATION).setWindowingMode(
                WINDOWING_MODE_MULTI_WINDOW).setActivityType(ACTIVITY_TYPE_STANDARD).setDisplay(
                mDisplayContent).build();
        final WindowState assistantStackWindow = newWindowBuilder("assistantStackWindow",
                TYPE_BASE_APPLICATION).setWindowingMode(WINDOWING_MODE_FULLSCREEN).setActivityType(
                ACTIVITY_TYPE_ASSISTANT).setDisplay(mDisplayContent).build();
        final WindowState homeActivityWindow = newWindowBuilder("homeActivityWindow",
                TYPE_BASE_APPLICATION).setWindowingMode(WINDOWING_MODE_FULLSCREEN).setActivityType(
                ACTIVITY_TYPE_HOME).setDisplay(mDisplayContent).build();
        final WindowState anyWindow2 = createWindow("anyWindow2");

        mDisplayContent.mTransitionController.mBuildingTransitionLayers = true;
        mDisplayContent.assignChildLayers(mTransaction);

        assertWindowHigher(dockedStackWindow, homeActivityWindow);
        assertWindowHigher(assistantStackWindow, homeActivityWindow);
        assertWindowHigher(pinnedStackWindow, homeActivityWindow);
        assertWindowHigher(anyWindow1, homeActivityWindow);
        assertWindowHigher(anyWindow2, homeActivityWindow);
        assertWindowHigher(pinnedStackWindow, anyWindow1);
        assertWindowHigher(pinnedStackWindow, anyWindow2);
        assertWindowHigher(pinnedStackWindow, dockedStackWindow);
        assertWindowHigher(pinnedStackWindow, assistantStackWindow);
    }

    @Test
    public void testAssignWindowLayers_ForSysUiPanels() {
        final WindowState navBarPanel = newWindowBuilder("NavBarPanel",
                TYPE_NAVIGATION_BAR_PANEL).setDisplay(mDisplayContent).build();
        final WindowState statusBarPanel = newWindowBuilder("StatusBarAdditional",
                TYPE_STATUS_BAR_ADDITIONAL).setDisplay(mDisplayContent).build();
        final WindowState statusBarSubPanel = newWindowBuilder("StatusBarSubPanel",
                TYPE_STATUS_BAR_SUB_PANEL).setDisplay(mDisplayContent).build();
        mDisplayContent.assignChildLayers(mTransaction);

        // Ime should be above all app windows and below system windows if it is targeting an app
        // window.
        assertWindowHigher(navBarPanel, mNavBarWindow);
        assertWindowHigher(statusBarPanel, mStatusBarWindow);
        assertWindowHigher(statusBarSubPanel, statusBarPanel);
    }

    @Test
    public void testAssignWindowLayers_ForImeOnPopupImeLayeringTarget() {
        final WindowState imeAppTarget = newWindowBuilder("imeAppTarget",
                TYPE_APPLICATION).setWindowToken(mAppWindow.mActivityRecord).build();
        mDisplayContent.setImeInputTarget(imeAppTarget);
        mDisplayContent.setImeLayeringTarget(imeAppTarget);
        assertWithMessage("IME control target was updated")
                .that(mDisplayContent.getImeControlTarget()).isEqualTo(imeAppTarget);

        // Set a popup IME layering target and keeps the original IME control target behinds it.
        final WindowState popupImeTargetWin = newWindowBuilder("popupImeTargetWin",
                TYPE_APPLICATION_SUB_PANEL).setParent(imeAppTarget).setWindowToken(
                mAppWindow.mActivityRecord).build();
        mDisplayContent.setImeLayeringTarget(popupImeTargetWin);
        mDisplayContent.updateImeParent();

        // Ime should on top of the popup IME layering target window.
        assertWindowHigher(mImeWindow, popupImeTargetWin);
    }


    @Test
    public void testAssignWindowLayers_ForNegativelyZOrderedSubtype() {
        // TODO(b/70040778): We should aim to eliminate the last user of TYPE_APPLICATION_MEDIA
        // then we can drop all negative layering on the windowing side.

        final WindowState anyWindow = createWindow("anyWindow");
        final WindowState child = newWindowBuilder("TypeApplicationMediaChild",
                TYPE_APPLICATION_MEDIA).setParent(anyWindow).setDisplay(mDisplayContent).build();
        final WindowState mediaOverlayChild = newWindowBuilder("TypeApplicationMediaOverlayChild",
                TYPE_APPLICATION_MEDIA_OVERLAY).setParent(anyWindow).setDisplay(
                mDisplayContent).build();

        mDisplayContent.assignChildLayers(mTransaction);

        assertWindowHigher(anyWindow, mediaOverlayChild);
        assertWindowHigher(mediaOverlayChild, child);
    }

    @Test
    public void testAssignWindowLayers_ForPostivelyZOrderedSubtype() {
        final WindowState anyWindow = createWindow("anyWindow");
        final ArrayList<WindowState> childList = new ArrayList<>();
        childList.add(newWindowBuilder("TypeApplicationPanelChild",
                TYPE_APPLICATION_PANEL).setParent(anyWindow).setDisplay(mDisplayContent).build());
        childList.add(newWindowBuilder("TypeApplicationSubPanelChild",
                TYPE_APPLICATION_SUB_PANEL).setParent(anyWindow).setDisplay(
                mDisplayContent).build());
        childList.add(newWindowBuilder("TypeApplicationAttachedDialogChild",
                TYPE_APPLICATION_ATTACHED_DIALOG).setParent(anyWindow).setDisplay(
                mDisplayContent).build());
        childList.add(newWindowBuilder("TypeApplicationAboveSubPanelPanelChild",
                TYPE_APPLICATION_ABOVE_SUB_PANEL).setParent(anyWindow).setDisplay(
                mDisplayContent).build());

        final LayerRecordingTransaction t = mTransaction;
        mDisplayContent.assignChildLayers(t);

        for (int i = childList.size() - 1; i >= 0; i--) {
            assertThat(t.getLayer(childList.get(i).getSurfaceControl()))
                    .isGreaterThan(PRESERVED_SURFACE_LAYER);
        }
    }

    @Test
    public void testPopupWindowAndParentIsImeTarget_expectHigherThanIme_inMultiWindow() {
        // Simulate the app window is in multi windowing mode and is IME layering and input target
        mAppWindow.getConfiguration().windowConfiguration.setWindowingMode(
                WINDOWING_MODE_MULTI_WINDOW);
        mDisplayContent.setImeInputTarget(mAppWindow);
        mDisplayContent.setImeLayeringTarget(mAppWindow);
        assertWithMessage("IME control target was updated")
                .that(mDisplayContent.getImeControlTarget()).isEqualTo(mAppWindow);
        makeWindowVisible(mImeWindow);

        // Create a popupWindow
        assertWindowHigher(mImeWindow, mAppWindow);
        final WindowState popupWindow = newWindowBuilder("PopupWindow",
                TYPE_APPLICATION_PANEL).setParent(mAppWindow).setDisplay(mDisplayContent).build();
        spyOn(popupWindow);

        mDisplayContent.assignChildLayers(mTransaction);

        // Verify the surface layer of the popupWindow should higher than IME
        verify(popupWindow).needsRelativeLayeringToIme();
        assertThat(popupWindow.needsRelativeLayeringToIme()).isTrue();
        assertZOrderGreaterThan(mTransaction, popupWindow.getSurfaceControl(),
                mDisplayContent.getImeContainer().getSurfaceControl());
    }

    @Test
    public void testSystemDialogWindow_expectHigherThanIme_inMultiWindow() {
        // Simulate the app window is in multi windowing mode and is IME layering and input target
        mAppWindow.getConfiguration().windowConfiguration.setWindowingMode(
                WINDOWING_MODE_MULTI_WINDOW);
        mDisplayContent.setImeInputTarget(mAppWindow);
        mDisplayContent.setImeLayeringTarget(mAppWindow);
        assertWithMessage("IME control target was updated")
                .that(mDisplayContent.getImeControlTarget()).isEqualTo(mAppWindow);
        makeWindowVisible(mImeWindow);

        // Create a popupWindow
        final WindowState systemDialogWindow = newWindowBuilder("SystemDialog",
                TYPE_SECURE_SYSTEM_OVERLAY).setDisplay(
                mDisplayContent).setOwnerCanAddInternalSystemWindow(true).build();
        systemDialogWindow.mAttrs.flags |= FLAG_ALT_FOCUSABLE_IM;
        spyOn(systemDialogWindow);

        mDisplayContent.assignChildLayers(mTransaction);

        // Verify the surface layer of the popupWindow should higher than IME
        verify(systemDialogWindow).needsRelativeLayeringToIme();
        assertThat(systemDialogWindow.needsRelativeLayeringToIme()).isTrue();
        assertZOrderGreaterThan(mTransaction, systemDialogWindow.getSurfaceControl(),
                mDisplayContent.getImeContainer().getSurfaceControl());
    }

    @Test
    public void testImeScreenshotLayer() {
        final Task task = createTask(mDisplayContent);
        final WindowState imeAppTarget = createAppWindow(task, TYPE_APPLICATION, "imeAppTarget");
        final Rect bounds = mImeWindow.getParentFrame();
        final ScreenCaptureInternal.ScreenshotHardwareBuffer imeBuffer =
                ScreenCaptureInternal.captureLayersExcluding(
                        mImeWindow.getSurfaceControl(), bounds, 1.0f, PixelFormat.RGB_565, null);

        spyOn(mDisplayContent.mWmService.mTaskSnapshotController);
        doReturn(imeBuffer).when(mDisplayContent.mWmService.mTaskSnapshotController)
                .screenshotImeFromAttachedTask(task);

        mDisplayContent.showImeScreenshot(imeAppTarget);

        assertEquals(imeAppTarget, mDisplayContent.mImeScreenshot.getImeTarget());
        assertNotNull(mDisplayContent.mImeScreenshot);
        assertNotNull(imeBuffer);
        assertZOrderGreaterThan(mTransaction, mDisplayContent.mImeScreenshot.getSurface(),
                imeAppTarget.mSurfaceControl);
    }
}
