ScreenshotController { public void getScreenshotWithMediaProjection(Consumer<Bitmap> bitmapConsumer) { MediaProjection sharedMediaProjection = mediaProjectionSupplier.get(); if (sharedMediaProjection == null) { bitmapConsumer.accept(null); return; } if (imageReader != null) { imageReader.close(); } if (display != null) { display.release(); } metrics = displayMetricsSupplier.get(); imageReader = getImageReader(metrics, bitmapConsumer); display = sharedMediaProjection.createVirtualDisplay( "myDisplay", metrics.widthPixels, metrics.heightPixels, metrics.densityDpi, DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR, imageReader.getSurface(), null, null); } ScreenshotController(
      Supplier<DisplayMetrics> displayMetricsSupplier,
      Handler handler,
      OnScreenshotAvailableProvider onScreenshotAvailableProvider,
      BitmapProvider bitmapProvider,
      Supplier<MediaProjection> mediaProjectionSupplier); void getScreenshotWithMediaProjection(Consumer<Bitmap> bitmapConsumer);  }
@Test public void nullBitmapReturnedWhenSharedMediaProjectionIsNull() { when(mediaProjectionSupplierMock.get()).thenReturn(null); testSubject.getScreenshotWithMediaProjection(bitmapConsumerMock); verify(bitmapConsumerMock, times(1)).accept(null); }
@Test public void createVirtualDisplayWithExpectedImageReader() { PowerMockito.mockStatic(ImageReader.class); bitmapConsumerCallback = ArgumentCaptor.forClass(Consumer.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(onScreenshotAvailableProviderMock.getOnScreenshotAvailable( bitmapConsumerCallback.capture(), eq(displayMetricsStub), eq(bitmapProviderMock))) .thenReturn(onScreenshotAvailableMock); when(mediaProjectionMock.createVirtualDisplay( "myDisplay", displayMetricsStub.widthPixels, displayMetricsStub.heightPixels, displayMetricsStub.densityDpi, DisplayManager.VIRTUAL_DISPLAY_FLAG_AUTO_MIRROR, surfaceMock, null, null)) .thenReturn(displayMock); testSubject.getScreenshotWithMediaProjection(bitmapConsumerMock); bitmapConsumerCallback.getValue().accept(bitmapMock); verify(displayMock, times(1)).release(); verify(bitmapConsumerMock, times(1)).accept(bitmapMock); }
@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(); }