@Test
public void createVirtualDisplayCleansResourcesAppropriatelyBeforeGettingScreenshot() {

  PowerMockito.mockStatic(ImageReader.class);
  when(mediaProjectionSupplierMock.get()).thenReturn(mediaProjectionMock);
  when(displayMetricsSupplierMock.get()).thenReturn(displayMetricsStub);
  when(ImageReader.newInstance(
          displayMetricsStub.widthPixels,
          displayMetricsStub.heightPixels,
          PixelFormat.RGBA_8888,
          2))
      .thenReturn(imageReaderMock);
  when(imageReaderMock.getSurface()).thenReturn(surfaceMock);
  when(mediaProjectionMock.createVirtualDisplay(
          "myDisplay",
          displayMetricsStub.widthPixels,
          displayMetricsStub.heightPixels,
          displayMetricsStub.densityDpi,
          DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR,
          surfaceMock,
          null,
          null))
      .thenReturn(displayMock);
  testSubject.getScreenshotWithMediaProjection(bitmapConsumerMock);
  testSubject.getScreenshotWithMediaProjection(bitmapConsumerMock);
  verify(displayMock, times(1)).release();
  verify(imageReaderMock, times(1)).close();
}