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

Commit 435b89c6 authored by Chavi Weingarten's avatar Chavi Weingarten
Browse files

Cleaned up and stabilize ScreenshotTests

Fixes: 324224973
Test: atest ScreenshotTests on cf 100 times
Change-Id: I950bb26430cc5bc9fd567683b2eca09574b1e957
parent b3929115
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -34,4 +34,11 @@
    <target_preparer class="com.android.tradefed.targetprep.RunCommandTargetPreparer">
        <option name="run-command" value="settings put secure immersive_mode_confirmations confirmed" />
    </target_preparer>

    <!-- Collect the dumped files for debugging -->
    <metrics_collector class="com.android.tradefed.device.metric.FilePullerLogCollector">
        <option name="directory-keys" value="/storage/emulated/0/ScreenshotTests" />
        <option name="clean-up" value="true" />
        <option name="collect-on-run-ended-only" value="true" />
    </metrics_collector>
</configuration>
+104 −43
Original line number Diff line number Diff line
@@ -16,12 +16,14 @@

package com.android.server.wm;

import static android.server.wm.CtsWindowInfoUtils.waitForStableWindowGeometry;
import static android.view.Display.DEFAULT_DISPLAY;
import static android.view.WindowInsets.Type.displayCutout;
import static android.view.WindowInsets.Type.statusBars;

import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;

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

@@ -43,6 +45,7 @@ import android.os.Handler;
import android.os.Looper;
import android.os.ServiceManager;
import android.platform.test.annotations.Presubmit;
import android.server.wm.BuildUtils;
import android.view.IWindowManager;
import android.view.PointerIcon;
import android.view.SurfaceControl;
@@ -50,7 +53,6 @@ import android.view.cts.surfacevalidator.BitmapPixelChecker;
import android.view.cts.surfacevalidator.SaveBitmapHelper;
import android.window.ScreenCapture;
import android.window.ScreenCapture.ScreenshotHardwareBuffer;
import android.window.ScreenCapture.SynchronousScreenCaptureListener;

import androidx.annotation.Nullable;
import androidx.test.filters.SmallTest;
@@ -74,6 +76,7 @@ import java.util.concurrent.TimeUnit;
@SmallTest
@Presubmit
public class ScreenshotTests {
    private static final long WAIT_TIME_S = 5L * BuildUtils.HW_TIMEOUT_MULTIPLIER;
    private static final int BUFFER_WIDTH = 100;
    private static final int BUFFER_HEIGHT = 100;

@@ -119,32 +122,61 @@ public class ScreenshotTests {
        canvas.drawColor(Color.RED);
        buffer.unlockCanvasAndPost(canvas);

        CountDownLatch countDownLatch = new CountDownLatch(1);
        t.show(secureSC)
                .setBuffer(secureSC, HardwareBuffer.createFromGraphicBuffer(buffer))
                .setDataSpace(secureSC, DataSpace.DATASPACE_SRGB)
                .apply(true);
                .addTransactionCommittedListener(Runnable::run, countDownLatch::countDown)
                .apply();
        assertTrue("Failed to wait for transaction to get committed",
                countDownLatch.await(WAIT_TIME_S, TimeUnit.SECONDS));
        assertTrue("Failed to wait for stable geometry",
                waitForStableWindowGeometry(WAIT_TIME_S, TimeUnit.SECONDS));

        ScreenCapture.LayerCaptureArgs args = new ScreenCapture.LayerCaptureArgs.Builder(secureSC)
                .setCaptureSecureLayers(true)
                .setChildrenOnly(false)
                .build();
        ScreenCapture.ScreenshotHardwareBuffer hardwareBuffer = ScreenCapture.captureLayers(args);
        assertNotNull(hardwareBuffer);

        Bitmap screenshot = hardwareBuffer.asBitmap();
        assertNotNull(screenshot);
        ScreenshotHardwareBuffer[] screenCapture = new ScreenshotHardwareBuffer[1];
        Bitmap screenshot = null;
        Bitmap swBitmap = null;
        try {
            CountDownLatch screenshotComplete = new CountDownLatch(1);
            ScreenCapture.captureLayers(args, new ScreenCapture.ScreenCaptureListener(
                    (screenshotHardwareBuffer, result) -> {
                        if (result == 0) {
                            screenCapture[0] = screenshotHardwareBuffer;
                        }
                        screenshotComplete.countDown();
                    }));
            assertTrue("Failed to wait for screen capture",
                    screenshotComplete.await(WAIT_TIME_S, TimeUnit.SECONDS));
            assertNotNull("Screen capture buffer is null", screenCapture[0]);

            screenshot = screenCapture[0].asBitmap();
            assertNotNull("Screenshot from bitmap is null", screenshot);

        Bitmap swBitmap = screenshot.copy(Bitmap.Config.ARGB_8888, false);
        screenshot.recycle();
            swBitmap = screenshot.copy(Bitmap.Config.ARGB_8888, false);

            BitmapPixelChecker bitmapPixelChecker = new BitmapPixelChecker(Color.RED);
            Rect bounds = new Rect(0, 0, swBitmap.getWidth(), swBitmap.getHeight());
            int numMatchingPixels = bitmapPixelChecker.getNumMatchingPixels(swBitmap, bounds);
            int sizeOfBitmap = bounds.width() * bounds.height();
        boolean success = numMatchingPixels == sizeOfBitmap;
        swBitmap.recycle();

        assertTrue(success);
            assertEquals("numMatchingPixels=" + numMatchingPixels + " sizeOfBitmap=" + sizeOfBitmap,
                    sizeOfBitmap, numMatchingPixels);
        } finally {
            if (screenshot != null) {
                screenshot.recycle();
            }
            if (swBitmap != null) {
                swBitmap.recycle();
            }
            if (screenCapture[0].getHardwareBuffer() != null) {
                screenCapture[0].getHardwareBuffer().close();
            }
        }
    }

    @Test
@@ -169,26 +201,44 @@ public class ScreenshotTests {
        buffer.unlockCanvasAndPost(canvas);

        Point point = mActivity.getPositionBelowStatusBar();
        CountDownLatch countDownLatch = new CountDownLatch(1);
        t.show(sc)
                .setBuffer(sc, HardwareBuffer.createFromGraphicBuffer(buffer))
                .setDataSpace(sc, DataSpace.DATASPACE_SRGB)
                .setPosition(sc, point.x, point.y)
                .apply(true);
                .addTransactionCommittedListener(Runnable::run, countDownLatch::countDown)
                .apply();

        assertTrue("Failed to wait for transaction to get committed",
                countDownLatch.await(WAIT_TIME_S, TimeUnit.SECONDS));
        assertTrue("Failed to wait for stable geometry",
                waitForStableWindowGeometry(WAIT_TIME_S, TimeUnit.SECONDS));

        SynchronousScreenCaptureListener syncScreenCapture =
                ScreenCapture.createSyncCaptureListener();
        windowManager.captureDisplay(DEFAULT_DISPLAY, null, syncScreenCapture);
        ScreenshotHardwareBuffer hardwareBuffer = syncScreenCapture.getBuffer();
        assertNotNull(hardwareBuffer);
        ScreenshotHardwareBuffer[] screenCapture = new ScreenshotHardwareBuffer[1];
        Bitmap screenshot = null;
        Bitmap swBitmap = null;
        try {
            CountDownLatch screenshotComplete = new CountDownLatch(1);
            windowManager.captureDisplay(DEFAULT_DISPLAY, null,
                    new ScreenCapture.ScreenCaptureListener(
                            (screenshotHardwareBuffer, result) -> {
                                if (result == 0) {
                                    screenCapture[0] = screenshotHardwareBuffer;
                                }
                                screenshotComplete.countDown();
                            }));
            assertTrue("Failed to wait for screen capture",
                    screenshotComplete.await(WAIT_TIME_S, TimeUnit.SECONDS));
            assertNotNull("Screen capture buffer is null", screenCapture[0]);

        Bitmap screenshot = hardwareBuffer.asBitmap();
        assertNotNull(screenshot);
            screenshot = screenCapture[0].asBitmap();
            assertNotNull("Screenshot from bitmap is null", screenshot);

        Bitmap swBitmap = screenshot.copy(Bitmap.Config.ARGB_8888, false);
        screenshot.recycle();
            swBitmap = screenshot.copy(Bitmap.Config.ARGB_8888, false);

            BitmapPixelChecker bitmapPixelChecker = new BitmapPixelChecker(Color.RED);
        Rect bounds = new Rect(point.x, point.y, BUFFER_WIDTH + point.x, BUFFER_HEIGHT + point.y);
            Rect bounds = new Rect(point.x, point.y, BUFFER_WIDTH + point.x,
                    BUFFER_HEIGHT + point.y);
            int numMatchingPixels = bitmapPixelChecker.getNumMatchingPixels(swBitmap, bounds);
            int pixelMatchSize = bounds.width() * bounds.height();
            boolean success = numMatchingPixels == pixelMatchSize;
@@ -196,9 +246,20 @@ public class ScreenshotTests {
            if (!success) {
                SaveBitmapHelper.saveBitmap(swBitmap, getClass(), mTestName, "failedImage");
            }
        swBitmap.recycle();
        assertTrue("numMatchingPixels=" + numMatchingPixels + " pixelMatchSize=" + pixelMatchSize,
            assertTrue(
                    "numMatchingPixels=" + numMatchingPixels + " pixelMatchSize=" + pixelMatchSize,
                    success);
        } finally {
            if (screenshot != null) {
                screenshot.recycle();
            }
            if (swBitmap != null) {
                swBitmap.recycle();
            }
            if (screenCapture[0].getHardwareBuffer() != null) {
                screenCapture[0].getHardwareBuffer().close();
            }
        }
    }

    public static class ScreenshotActivity extends Activity {