diff options
| -rw-r--r-- | services/tests/wmtests/AndroidTest.xml | 7 | ||||
| -rw-r--r-- | services/tests/wmtests/src/com/android/server/wm/ScreenshotTests.java | 147 |
2 files changed, 111 insertions, 43 deletions
diff --git a/services/tests/wmtests/AndroidTest.xml b/services/tests/wmtests/AndroidTest.xml index 46e87dceb8d4..2512ee592ef3 100644 --- a/services/tests/wmtests/AndroidTest.xml +++ b/services/tests/wmtests/AndroidTest.xml @@ -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> diff --git a/services/tests/wmtests/src/com/android/server/wm/ScreenshotTests.java b/services/tests/wmtests/src/com/android/server/wm/ScreenshotTests.java index 0fcae92fdbfc..280fe4c30739 100644 --- a/services/tests/wmtests/src/com/android/server/wm/ScreenshotTests.java +++ b/services/tests/wmtests/src/com/android/server/wm/ScreenshotTests.java @@ -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); - - Bitmap swBitmap = screenshot.copy(Bitmap.Config.ARGB_8888, false); - screenshot.recycle(); - - 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); + 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); + + 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(); + + 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,36 +201,65 @@ 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); - - SynchronousScreenCaptureListener syncScreenCapture = - ScreenCapture.createSyncCaptureListener(); - windowManager.captureDisplay(DEFAULT_DISPLAY, null, syncScreenCapture); - ScreenshotHardwareBuffer hardwareBuffer = syncScreenCapture.getBuffer(); - assertNotNull(hardwareBuffer); - - Bitmap screenshot = hardwareBuffer.asBitmap(); - assertNotNull(screenshot); - - Bitmap swBitmap = screenshot.copy(Bitmap.Config.ARGB_8888, false); - screenshot.recycle(); - - BitmapPixelChecker bitmapPixelChecker = new BitmapPixelChecker(Color.RED); - 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; - - if (!success) { - SaveBitmapHelper.saveBitmap(swBitmap, getClass(), mTestName, "failedImage"); + .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)); + + 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]); + + screenshot = screenCapture[0].asBitmap(); + assertNotNull("Screenshot from bitmap is null", screenshot); + + 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); + int numMatchingPixels = bitmapPixelChecker.getNumMatchingPixels(swBitmap, bounds); + int pixelMatchSize = bounds.width() * bounds.height(); + boolean success = numMatchingPixels == pixelMatchSize; + + if (!success) { + SaveBitmapHelper.saveBitmap(swBitmap, getClass(), mTestName, "failedImage"); + } + 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(); + } } - swBitmap.recycle(); - assertTrue("numMatchingPixels=" + numMatchingPixels + " pixelMatchSize=" + pixelMatchSize, - success); } public static class ScreenshotActivity extends Activity { |