|
| 1 | +// Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +package io.flutter.plugins.camera.features.noisereduction; |
| 6 | + |
| 7 | +import static org.junit.Assert.assertEquals; |
| 8 | +import static org.junit.Assert.assertFalse; |
| 9 | +import static org.junit.Assert.assertTrue; |
| 10 | +import static org.mockito.ArgumentMatchers.any; |
| 11 | +import static org.mockito.Mockito.mock; |
| 12 | +import static org.mockito.Mockito.never; |
| 13 | +import static org.mockito.Mockito.times; |
| 14 | +import static org.mockito.Mockito.verify; |
| 15 | +import static org.mockito.Mockito.when; |
| 16 | + |
| 17 | +import android.hardware.camera2.CaptureRequest; |
| 18 | +import android.os.Build.VERSION; |
| 19 | +import io.flutter.plugins.camera.CameraProperties; |
| 20 | +import io.flutter.plugins.camera.utils.TestUtils; |
| 21 | +import org.junit.After; |
| 22 | +import org.junit.Before; |
| 23 | +import org.junit.Test; |
| 24 | + |
| 25 | +public class NoiseReductionFeatureTest { |
| 26 | + @Before |
| 27 | + public void before() { |
| 28 | + // Make sure the VERSION.SDK_INT field returns 23, to allow using all available |
| 29 | + // noise reduction modes in tests. |
| 30 | + TestUtils.setFinalStatic(VERSION.class, "SDK_INT", 23); |
| 31 | + } |
| 32 | + |
| 33 | + @After |
| 34 | + public void after() { |
| 35 | + // Make sure we reset the VERSION.SDK_INT field to it's original value. |
| 36 | + TestUtils.setFinalStatic(VERSION.class, "SDK_INT", 0); |
| 37 | + } |
| 38 | + |
| 39 | + @Test |
| 40 | + public void getDebugName_should_return_the_name_of_the_feature() { |
| 41 | + CameraProperties mockCameraProperties = mock(CameraProperties.class); |
| 42 | + NoiseReductionFeature noiseReductionFeature = new NoiseReductionFeature(mockCameraProperties); |
| 43 | + |
| 44 | + assertEquals("NoiseReductionFeature", noiseReductionFeature.getDebugName()); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void getValue_should_return_fast_if_not_set() { |
| 49 | + CameraProperties mockCameraProperties = mock(CameraProperties.class); |
| 50 | + NoiseReductionFeature noiseReductionFeature = new NoiseReductionFeature(mockCameraProperties); |
| 51 | + |
| 52 | + assertEquals(NoiseReductionMode.fast, noiseReductionFeature.getValue()); |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + public void getValue_should_echo_the_set_value() { |
| 57 | + CameraProperties mockCameraProperties = mock(CameraProperties.class); |
| 58 | + NoiseReductionFeature noiseReductionFeature = new NoiseReductionFeature(mockCameraProperties); |
| 59 | + NoiseReductionMode expectedValue = NoiseReductionMode.fast; |
| 60 | + |
| 61 | + noiseReductionFeature.setValue(expectedValue); |
| 62 | + NoiseReductionMode actualValue = noiseReductionFeature.getValue(); |
| 63 | + |
| 64 | + assertEquals(expectedValue, actualValue); |
| 65 | + } |
| 66 | + |
| 67 | + @Test |
| 68 | + public void checkIsSupported_should_return_false_when_available_noise_reduction_modes_is_null() { |
| 69 | + CameraProperties mockCameraProperties = mock(CameraProperties.class); |
| 70 | + NoiseReductionFeature noiseReductionFeature = new NoiseReductionFeature(mockCameraProperties); |
| 71 | + |
| 72 | + when(mockCameraProperties.getAvailableNoiseReductionModes()).thenReturn(null); |
| 73 | + |
| 74 | + assertFalse(noiseReductionFeature.checkIsSupported()); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + public void |
| 79 | + checkIsSupported_should_return_false_when_available_noise_reduction_modes_returns_an_empty_array() { |
| 80 | + CameraProperties mockCameraProperties = mock(CameraProperties.class); |
| 81 | + NoiseReductionFeature noiseReductionFeature = new NoiseReductionFeature(mockCameraProperties); |
| 82 | + |
| 83 | + when(mockCameraProperties.getAvailableNoiseReductionModes()).thenReturn(new int[] {}); |
| 84 | + |
| 85 | + assertFalse(noiseReductionFeature.checkIsSupported()); |
| 86 | + } |
| 87 | + |
| 88 | + @Test |
| 89 | + public void |
| 90 | + checkIsSupported_should_return_true_when_available_noise_reduction_modes_returns_at_least_one_item() { |
| 91 | + CameraProperties mockCameraProperties = mock(CameraProperties.class); |
| 92 | + NoiseReductionFeature noiseReductionFeature = new NoiseReductionFeature(mockCameraProperties); |
| 93 | + |
| 94 | + when(mockCameraProperties.getAvailableNoiseReductionModes()).thenReturn(new int[] {1}); |
| 95 | + |
| 96 | + assertTrue(noiseReductionFeature.checkIsSupported()); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void updateBuilder_should_return_when_checkIsSupported_is_false() { |
| 101 | + CameraProperties mockCameraProperties = mock(CameraProperties.class); |
| 102 | + CaptureRequest.Builder mockBuilder = mock(CaptureRequest.Builder.class); |
| 103 | + NoiseReductionFeature noiseReductionFeature = new NoiseReductionFeature(mockCameraProperties); |
| 104 | + |
| 105 | + when(mockCameraProperties.getAvailableNoiseReductionModes()).thenReturn(new int[] {}); |
| 106 | + |
| 107 | + noiseReductionFeature.updateBuilder(mockBuilder); |
| 108 | + |
| 109 | + verify(mockBuilder, never()).set(any(), any()); |
| 110 | + } |
| 111 | + |
| 112 | + @Test |
| 113 | + public void updateBuilder_should_set_noise_reduction_mode_off_when_off() { |
| 114 | + testUpdateBuilderWith(NoiseReductionMode.off, CaptureRequest.NOISE_REDUCTION_MODE_OFF); |
| 115 | + } |
| 116 | + |
| 117 | + @Test |
| 118 | + public void updateBuilder_should_set_noise_reduction_mode_fast_when_fast() { |
| 119 | + testUpdateBuilderWith(NoiseReductionMode.fast, CaptureRequest.NOISE_REDUCTION_MODE_FAST); |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + public void updateBuilder_should_set_noise_reduction_mode_high_quality_when_high_quality() { |
| 124 | + testUpdateBuilderWith( |
| 125 | + NoiseReductionMode.highQuality, CaptureRequest.NOISE_REDUCTION_MODE_HIGH_QUALITY); |
| 126 | + } |
| 127 | + |
| 128 | + @Test |
| 129 | + public void updateBuilder_should_set_noise_reduction_mode_minimal_when_minimal() { |
| 130 | + testUpdateBuilderWith(NoiseReductionMode.minimal, CaptureRequest.NOISE_REDUCTION_MODE_MINIMAL); |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + public void |
| 135 | + updateBuilder_should_set_noise_reduction_mode_zero_shutter_lag_when_zero_shutter_lag() { |
| 136 | + testUpdateBuilderWith( |
| 137 | + NoiseReductionMode.zeroShutterLag, CaptureRequest.NOISE_REDUCTION_MODE_ZERO_SHUTTER_LAG); |
| 138 | + } |
| 139 | + |
| 140 | + private static void testUpdateBuilderWith(NoiseReductionMode mode, int expectedResult) { |
| 141 | + CameraProperties mockCameraProperties = mock(CameraProperties.class); |
| 142 | + CaptureRequest.Builder mockBuilder = mock(CaptureRequest.Builder.class); |
| 143 | + NoiseReductionFeature noiseReductionFeature = new NoiseReductionFeature(mockCameraProperties); |
| 144 | + |
| 145 | + when(mockCameraProperties.getAvailableNoiseReductionModes()).thenReturn(new int[] {1}); |
| 146 | + |
| 147 | + noiseReductionFeature.setValue(mode); |
| 148 | + noiseReductionFeature.updateBuilder(mockBuilder); |
| 149 | + verify(mockBuilder, times(1)).set(CaptureRequest.NOISE_REDUCTION_MODE, expectedResult); |
| 150 | + } |
| 151 | +} |
0 commit comments