From 5d2377969de9fc3788d5e84a91dbd04a53bbf69b Mon Sep 17 00:00:00 2001 From: Dmitry Zaytsev Date: Wed, 24 May 2017 19:20:21 +0200 Subject: [PATCH] It was possible that there is no preview size with the same aspect ratio as picture size. In such cases we fall back to normal preview size selection. --- .../provider/InitialParametersProvider.java | 17 +++++++--- .../InitialParametersProviderTest.java | 31 +++++++++++++++++++ 2 files changed, 44 insertions(+), 4 deletions(-) diff --git a/fotoapparat/src/main/java/io/fotoapparat/parameter/provider/InitialParametersProvider.java b/fotoapparat/src/main/java/io/fotoapparat/parameter/provider/InitialParametersProvider.java index d82742d8..0338b8af 100644 --- a/fotoapparat/src/main/java/io/fotoapparat/parameter/provider/InitialParametersProvider.java +++ b/fotoapparat/src/main/java/io/fotoapparat/parameter/provider/InitialParametersProvider.java @@ -7,8 +7,10 @@ import io.fotoapparat.parameter.FocusMode; import io.fotoapparat.parameter.Parameters; import io.fotoapparat.parameter.Size; -import io.fotoapparat.parameter.selector.AspectRatioSelectors; import io.fotoapparat.parameter.selector.SelectorFunction; +import io.fotoapparat.parameter.selector.Selectors; + +import static io.fotoapparat.parameter.selector.AspectRatioSelectors.aspectRatio; /** * Provides initial {@link Parameters} for {@link CameraDevice}. @@ -59,15 +61,22 @@ private void putPreviewSize(Capabilities capabilities, Parameters parameters) { parameters.putValue( Parameters.Type.PREVIEW_SIZE, - AspectRatioSelectors - .aspectRatio( - photoSize.getAspectRatio(), + Selectors + .firstAvailable( + previewWithSameAspectRatio(photoSize), previewSizeSelector ) .select(capabilities.supportedPreviewSizes()) ); } + private SelectorFunction previewWithSameAspectRatio(Size photoSize) { + return aspectRatio( + photoSize.getAspectRatio(), + previewSizeSelector + ); + } + private void putPictureSize(Capabilities capabilities, Parameters parameters) { parameters.putValue( Parameters.Type.PICTURE_SIZE, diff --git a/fotoapparat/src/test/java/io/fotoapparat/parameter/provider/InitialParametersProviderTest.java b/fotoapparat/src/test/java/io/fotoapparat/parameter/provider/InitialParametersProviderTest.java index 036a7fbb..1b8ca292 100644 --- a/fotoapparat/src/test/java/io/fotoapparat/parameter/provider/InitialParametersProviderTest.java +++ b/fotoapparat/src/test/java/io/fotoapparat/parameter/provider/InitialParametersProviderTest.java @@ -17,6 +17,7 @@ import io.fotoapparat.parameter.selector.SelectorFunction; import static io.fotoapparat.test.TestUtils.asSet; +import static java.util.Collections.singleton; import static junit.framework.Assert.assertEquals; import static org.mockito.ArgumentMatchers.any; import static org.mockito.BDDMockito.given; @@ -133,6 +134,36 @@ public void selectPreviewSize_WithValidAspectRatio() throws Exception { ); } + @Test + public void selectPreviewSize_SameAspectRatioNotAvailable() throws Exception { + // Given + Size photoSize = new Size(10000, 100); + Set photoSizes = singleton(photoSize); + + given(photoSizeSelector.select(photoSizes)) + .willReturn(photoSize); + + given(cameraDevice.getCapabilities()) + .willReturn(new Capabilities( + photoSizes, + ALL_PREVIEW_SIZES, + FOCUS_MODES, + FLASH + )); + + given(previewSizeSelector.select(ALL_PREVIEW_SIZES)) + .willReturn(PREVIEW_SIZE); + + // When + Parameters parameters = testee.initialParameters(); + + // Then + assertEquals( + PREVIEW_SIZE, + parameters.getValue(Parameters.Type.PREVIEW_SIZE) + ); + } + @Test public void parameterValidation() throws Exception { // Given