diff --git a/packages/camera/camera/android/src/main/java/io/flutter/plugins/camera/features/zoomlevel/CameraZoom.java b/packages/camera/camera/android/src/main/java/io/flutter/plugins/camera/features/zoomlevel/CameraZoom.java index 8a7909a29b72..e1d1158e2a0e 100644 --- a/packages/camera/camera/android/src/main/java/io/flutter/plugins/camera/features/zoomlevel/CameraZoom.java +++ b/packages/camera/camera/android/src/main/java/io/flutter/plugins/camera/features/zoomlevel/CameraZoom.java @@ -18,7 +18,11 @@ public final class CameraZoom { public final float maxZoom; public final boolean hasSupport; - public CameraZoom(@Nullable final Rect sensorArraySize, final Float maxZoom) { + public static CameraZoom create(@Nullable final Rect sensorArraySize, final Float maxZoom) { + return new CameraZoom(sensorArraySize, maxZoom); + } + + private CameraZoom(@Nullable final Rect sensorArraySize, final Float maxZoom) { this.sensorSize = sensorArraySize; if (this.sensorSize == null) { diff --git a/packages/camera/camera/android/src/main/java/io/flutter/plugins/camera/features/zoomlevel/ZoomLevelFeature.java b/packages/camera/camera/android/src/main/java/io/flutter/plugins/camera/features/zoomlevel/ZoomLevelFeature.java index 85499a47a053..e278fe377361 100644 --- a/packages/camera/camera/android/src/main/java/io/flutter/plugins/camera/features/zoomlevel/ZoomLevelFeature.java +++ b/packages/camera/camera/android/src/main/java/io/flutter/plugins/camera/features/zoomlevel/ZoomLevelFeature.java @@ -12,20 +12,20 @@ /** Exposure offset makes the image brighter or darker. */ public class ZoomLevelFeature extends CameraFeature { + private final CameraZoom cameraZoom; private Float currentSetting = CameraZoom.DEFAULT_ZOOM_FACTOR; - private CameraZoom cameraZoom; public ZoomLevelFeature(CameraProperties cameraProperties) { super(cameraProperties); this.cameraZoom = - new CameraZoom( + CameraZoom.create( cameraProperties.getSensorInfoActiveArraySize(), cameraProperties.getScalerAvailableMaxDigitalZoom()); } @Override public String getDebugName() { - return "ZoomLevel"; + return "ZoomLevelFeature"; } @Override diff --git a/packages/camera/camera/android/src/test/java/io/flutter/plugins/camera/features/zoomlevel/ZoomLevelFeatureTest.java b/packages/camera/camera/android/src/test/java/io/flutter/plugins/camera/features/zoomlevel/ZoomLevelFeatureTest.java new file mode 100644 index 000000000000..469c5006ffcb --- /dev/null +++ b/packages/camera/camera/android/src/test/java/io/flutter/plugins/camera/features/zoomlevel/ZoomLevelFeatureTest.java @@ -0,0 +1,97 @@ +package io.flutter.plugins.camera.features.zoomlevel; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.mockStatic; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +import android.graphics.Rect; +import android.hardware.camera2.CaptureRequest; +import io.flutter.plugins.camera.CameraProperties; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.mockito.MockedStatic; + +public class ZoomLevelFeatureTest { + private MockedStatic mockedStaticCameraZoom; + private CameraProperties mockCameraProperties; + private CameraZoom mockCameraZoom; + + @Before + public void before() { + mockedStaticCameraZoom = mockStatic(CameraZoom.class); + mockCameraProperties = mock(CameraProperties.class); + mockCameraZoom = mock(CameraZoom.class); + + mockedStaticCameraZoom.when(() -> CameraZoom.create(any(), any())).thenReturn(mockCameraZoom); + } + + @After + public void after() { + mockedStaticCameraZoom.close(); + } + + @Test + public void ctor_should_initiaze_camera_zoom_instance() { + ZoomLevelFeature zoomLevelFeature = new ZoomLevelFeature(mockCameraProperties); + + verify(mockCameraProperties, times(1)).getSensorInfoActiveArraySize(); + verify(mockCameraProperties, times(1)).getScalerAvailableMaxDigitalZoom(); + assertEquals(mockCameraZoom, zoomLevelFeature.getCameraZoom()); + } + + @Test + public void getDebugName_should_return_the_name_of_the_feature() { + ZoomLevelFeature zoomLevelFeature = new ZoomLevelFeature(mockCameraProperties); + + assertEquals("ZoomLevelFeature", zoomLevelFeature.getDebugName()); + } + + @Test + public void getValue_should_return_null_if_not_set() { + ZoomLevelFeature zoomLevelFeature = new ZoomLevelFeature(mockCameraProperties); + + assertEquals(1.0, (float) zoomLevelFeature.getValue(), 0); + } + + @Test + public void getValue_should_echo_setValue() { + ZoomLevelFeature zoomLevelFeature = new ZoomLevelFeature(mockCameraProperties); + + zoomLevelFeature.setValue(2.3f); + + assertEquals(2.3f, (float) zoomLevelFeature.getValue(), 0); + } + + @Test + public void checkIsSupport_returns_true() { + ZoomLevelFeature zoomLevelFeature = new ZoomLevelFeature(mockCameraProperties); + + assertTrue(zoomLevelFeature.checkIsSupported()); + } + + @Test + public void updateBuilder_should_set_scalar_crop_region_when_checkIsSupport_is_true() { + ZoomLevelFeature zoomLevelFeature = new ZoomLevelFeature(mockCameraProperties); + CaptureRequest.Builder mockBuilder = mock(CaptureRequest.Builder.class); + Rect mockRect = mock(Rect.class); + + when(mockCameraZoom.computeZoom(1.0f)).thenReturn(mockRect); + + zoomLevelFeature.updateBuilder(mockBuilder); + + verify(mockBuilder, times(1)).set(CaptureRequest.SCALER_CROP_REGION, mockRect); + } + + @Test + public void getCameraZoom_should_return_camera_zoom_instance_initialized_in_ctor() { + ZoomLevelFeature zoomLevelFeature = new ZoomLevelFeature(mockCameraProperties); + + assertEquals(mockCameraZoom, zoomLevelFeature.getCameraZoom()); + } +}