|
| 1 | +package io.flutter.plugins.camera.features.focuspoint; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertFalse; |
| 5 | +import static org.junit.Assert.assertTrue; |
| 6 | +import static org.mockito.Mockito.mock; |
| 7 | +import static org.mockito.Mockito.times; |
| 8 | +import static org.mockito.Mockito.verify; |
| 9 | +import static org.mockito.Mockito.when; |
| 10 | + |
| 11 | +import io.flutter.plugins.camera.CameraProperties; |
| 12 | +import io.flutter.plugins.camera.features.Point; |
| 13 | +import io.flutter.plugins.camera.features.regionboundaries.CameraRegions; |
| 14 | +import org.junit.Test; |
| 15 | + |
| 16 | +public class FocusPointFeatureTest { |
| 17 | + @Test |
| 18 | + public void getDebugName_should_return_the_name_of_the_feature() { |
| 19 | + CameraProperties mockCameraProperties = mock(CameraProperties.class); |
| 20 | + FocusPointFeature focusPointFeature = new FocusPointFeature(mockCameraProperties, () -> null); |
| 21 | + |
| 22 | + assertEquals("FocusPointFeature", focusPointFeature.getDebugName()); |
| 23 | + } |
| 24 | + |
| 25 | + @Test |
| 26 | + public void getValue_should_return_default_point_if_not_set() { |
| 27 | + CameraProperties mockCameraProperties = mock(CameraProperties.class); |
| 28 | + FocusPointFeature focusPointFeature = new FocusPointFeature(mockCameraProperties, () -> null); |
| 29 | + Point expectedPoint = new Point(0.0, 0.0); |
| 30 | + Point actualPoint = focusPointFeature.getValue(); |
| 31 | + |
| 32 | + assertEquals(expectedPoint.x, actualPoint.x); |
| 33 | + assertEquals(expectedPoint.y, actualPoint.y); |
| 34 | + } |
| 35 | + |
| 36 | + @Test |
| 37 | + public void getValue_should_echo_the_set_value() { |
| 38 | + CameraProperties mockCameraProperties = mock(CameraProperties.class); |
| 39 | + CameraRegions mockCameraRegions = mock(CameraRegions.class); |
| 40 | + FocusPointFeature focusPointFeature = new FocusPointFeature(mockCameraProperties, () -> mockCameraRegions); |
| 41 | + Point expectedPoint = new Point(0.0, 0.0); |
| 42 | + |
| 43 | + focusPointFeature.setValue(expectedPoint); |
| 44 | + Point actualPoint = focusPointFeature.getValue(); |
| 45 | + |
| 46 | + assertEquals(expectedPoint, actualPoint); |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + public void setValue_should_reset_point_when_x_coord_is_null() { |
| 51 | + CameraProperties mockCameraProperties = mock(CameraProperties.class); |
| 52 | + CameraRegions mockCameraRegions = mock(CameraRegions.class); |
| 53 | + FocusPointFeature focusPointFeature = new FocusPointFeature(mockCameraProperties, () -> mockCameraRegions); |
| 54 | + |
| 55 | + focusPointFeature.setValue(new Point(null, 0.0)); |
| 56 | + |
| 57 | + verify(mockCameraRegions, times(1)).resetAutoFocusMeteringRectangle(); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + public void setValue_should_reset_point_when_y_coord_is_null() { |
| 62 | + CameraProperties mockCameraProperties = mock(CameraProperties.class); |
| 63 | + CameraRegions mockCameraRegions = mock(CameraRegions.class); |
| 64 | + FocusPointFeature focusPointFeature = new FocusPointFeature(mockCameraProperties, () -> mockCameraRegions); |
| 65 | + |
| 66 | + focusPointFeature.setValue(new Point(0.0, null)); |
| 67 | + |
| 68 | + verify(mockCameraRegions, times(1)).resetAutoFocusMeteringRectangle(); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void setValue_should_reset_point_when_valid_coords_are_supplied() { |
| 73 | + CameraProperties mockCameraProperties = mock(CameraProperties.class); |
| 74 | + CameraRegions mockCameraRegions = mock(CameraRegions.class); |
| 75 | + FocusPointFeature focusPointFeature = new FocusPointFeature(mockCameraProperties, () -> mockCameraRegions); |
| 76 | + Point point = new Point(0.0, 0.0); |
| 77 | + |
| 78 | + focusPointFeature.setValue(point); |
| 79 | + |
| 80 | + verify(mockCameraRegions, times(1)).setAutoFocusMeteringRectangleFromPoint(point.x, point.y); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void checkIsSupported_should_return_false_when_max_regions_is_null() { |
| 85 | + CameraProperties mockCameraProperties = mock(CameraProperties.class); |
| 86 | + FocusPointFeature focusPointFeature = new FocusPointFeature(mockCameraProperties, () -> null); |
| 87 | + |
| 88 | + when(mockCameraProperties.getControlMaxRegionsAutoFocus()).thenReturn(null); |
| 89 | + |
| 90 | + assertFalse(focusPointFeature.checkIsSupported()); |
| 91 | + } |
| 92 | + |
| 93 | + @Test |
| 94 | + public void checkIsSupported_should_return_false_when_max_regions_is_zero() { |
| 95 | + CameraProperties mockCameraProperties = mock(CameraProperties.class); |
| 96 | + FocusPointFeature focusPointFeature = new FocusPointFeature(mockCameraProperties, () -> null); |
| 97 | + |
| 98 | + when(mockCameraProperties.getControlMaxRegionsAutoFocus()).thenReturn(0); |
| 99 | + |
| 100 | + assertFalse(focusPointFeature.checkIsSupported()); |
| 101 | + } |
| 102 | + |
| 103 | + @Test |
| 104 | + public void checkIsSupported_should_return_true_when_max_regions_is_bigger_then_zero() { |
| 105 | + CameraProperties mockCameraProperties = mock(CameraProperties.class); |
| 106 | + FocusPointFeature focusPointFeature = new FocusPointFeature(mockCameraProperties, () -> null); |
| 107 | + |
| 108 | + when(mockCameraProperties.getControlMaxRegionsAutoFocus()).thenReturn(1); |
| 109 | + |
| 110 | + assertTrue(focusPointFeature.checkIsSupported()); |
| 111 | + } |
| 112 | +} |
0 commit comments