• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.robolectric.shadows;
2 
3 import static com.google.common.truth.Truth.assertThat;
4 
5 import android.graphics.ImageFormat;
6 import android.hardware.Camera;
7 import androidx.test.ext.junit.runners.AndroidJUnit4;
8 import com.google.common.collect.Lists;
9 import java.util.List;
10 import org.junit.Before;
11 import org.junit.Test;
12 import org.junit.runner.RunWith;
13 import org.robolectric.Shadows;
14 import org.robolectric.shadow.api.Shadow;
15 
16 @RunWith(AndroidJUnit4.class)
17 public class ShadowCameraParametersTest {
18 
19   private Camera.Parameters parameters;
20 
21   @Before
setUp()22   public void setUp() throws Exception {
23     parameters = Shadow.newInstanceOf(Camera.Parameters.class);
24   }
25 
26   @Test
testPictureSize()27   public void testPictureSize() throws Exception {
28     assertThat(Shadows.shadowOf(parameters).getPictureHeight()).isNotEqualTo(600);
29     assertThat(Shadows.shadowOf(parameters).getPictureWidth()).isNotEqualTo(800);
30     parameters.setPictureSize(800, 600);
31     Camera.Size pictureSize = parameters.getPictureSize();
32     assertThat(pictureSize.width).isEqualTo(800);
33     assertThat(pictureSize.height).isEqualTo(600);
34     assertThat(Shadows.shadowOf(parameters).getPictureHeight()).isEqualTo(600);
35     assertThat(Shadows.shadowOf(parameters).getPictureWidth()).isEqualTo(800);
36   }
37 
38   @Test
testPreviewFpsRange()39   public void testPreviewFpsRange() throws Exception {
40     int[] fpsRange = new int[2];
41     parameters.getPreviewFpsRange(fpsRange);
42     assertThat(fpsRange[1]).isNotEqualTo((15));
43     assertThat(fpsRange[0]).isNotEqualTo((25));
44     parameters.setPreviewFpsRange(15, 25);
45     parameters.getPreviewFpsRange(fpsRange);
46     assertThat(fpsRange[1]).isEqualTo(25);
47     assertThat(fpsRange[0]).isEqualTo(15);
48   }
49 
50   @Test
testPreviewFrameRate()51   public void testPreviewFrameRate() throws Exception {
52     assertThat(parameters.getPreviewFrameRate()).isNotEqualTo((15));
53     parameters.setPreviewFrameRate(15);
54     assertThat(parameters.getPreviewFrameRate()).isEqualTo(15);
55   }
56 
57   @Test
testPreviewSize()58   public void testPreviewSize() throws Exception {
59     assertThat(Shadows.shadowOf(parameters).getPreviewWidth()).isNotEqualTo(320);
60     assertThat(Shadows.shadowOf(parameters).getPreviewHeight()).isNotEqualTo(240);
61     parameters.setPreviewSize(320, 240);
62     Camera.Size size = parameters.getPreviewSize();
63     assertThat(size.width).isEqualTo(320);
64     assertThat(size.height).isEqualTo(240);
65     assertThat(Shadows.shadowOf(parameters).getPreviewWidth()).isEqualTo(320);
66     assertThat(Shadows.shadowOf(parameters).getPreviewHeight()).isEqualTo(240);
67   }
68 
69   @Test
testPreviewFormat()70   public void testPreviewFormat() throws Exception {
71     assertThat(parameters.getPreviewFormat()).isEqualTo(ImageFormat.NV21);
72     parameters.setPreviewFormat(ImageFormat.JPEG);
73     assertThat(parameters.getPreviewFormat()).isEqualTo(ImageFormat.JPEG);
74   }
75 
76   @Test
testGetSupportedPreviewFormats()77   public void testGetSupportedPreviewFormats() throws Exception {
78     List<Integer> supportedFormats = parameters.getSupportedPreviewFormats();
79     assertThat(supportedFormats).isNotNull();
80     assertThat(supportedFormats.size()).isNotEqualTo((0));
81     assertThat(supportedFormats).contains(ImageFormat.NV21);
82   }
83 
84   @Test
testGetSupportedPictureFormats()85   public void testGetSupportedPictureFormats() throws Exception {
86     List<Integer> supportedFormats = parameters.getSupportedPictureFormats();
87     assertThat(supportedFormats).isNotNull();
88     assertThat(supportedFormats.size()).isEqualTo(2);
89     assertThat(supportedFormats).contains(ImageFormat.NV21);
90   }
91 
92   @Test
testGetSupportedPictureSizes()93   public void testGetSupportedPictureSizes() throws Exception {
94     List<Camera.Size> supportedSizes = parameters.getSupportedPictureSizes();
95     assertThat(supportedSizes).isNotNull();
96     assertThat(supportedSizes.size()).isEqualTo(3);
97     assertThat(supportedSizes.get(0).width).isEqualTo(320);
98     assertThat(supportedSizes.get(0).height).isEqualTo(240);
99   }
100 
101   @Test
testGetSupportedPreviewSizes()102   public void testGetSupportedPreviewSizes() throws Exception {
103     List<Camera.Size> supportedSizes = parameters.getSupportedPreviewSizes();
104     assertThat(supportedSizes).isNotNull();
105     assertThat(supportedSizes.size()).isEqualTo(2);
106     assertThat(supportedSizes.get(0).width).isEqualTo(320);
107     assertThat(supportedSizes.get(0).height).isEqualTo(240);
108   }
109 
110   @Test
testInitSupportedPreviewSizes()111   public void testInitSupportedPreviewSizes() throws Exception {
112     Shadows.shadowOf(parameters).initSupportedPreviewSizes();
113     assertThat(parameters.getSupportedPreviewSizes()).isNotNull();
114     assertThat(parameters.getSupportedPreviewSizes()).isEmpty();
115   }
116 
117   @Test
testAddSupportedPreviewSizes()118   public void testAddSupportedPreviewSizes() throws Exception {
119     Shadows.shadowOf(parameters).initSupportedPreviewSizes();
120     Shadows.shadowOf(parameters).addSupportedPreviewSize(320, 240);
121     List<Camera.Size> supportedSizes = parameters.getSupportedPreviewSizes();
122     assertThat(supportedSizes).isNotNull();
123     assertThat(supportedSizes).hasSize(1);
124     assertThat(supportedSizes.get(0).width).isEqualTo(320);
125     assertThat(supportedSizes.get(0).height).isEqualTo(240);
126   }
127 
128   @Test
testGetSupportedPreviewFpsRange()129   public void testGetSupportedPreviewFpsRange() throws Exception {
130     List<int[]> supportedRanges = parameters.getSupportedPreviewFpsRange();
131     assertThat(supportedRanges).isNotNull();
132     assertThat(supportedRanges.size()).isEqualTo(2);
133     assertThat(supportedRanges.get(0)[0]).isEqualTo(15000);
134     assertThat(supportedRanges.get(0)[1]).isEqualTo(15000);
135     assertThat(supportedRanges.get(1)[0]).isEqualTo(10000);
136     assertThat(supportedRanges.get(1)[1]).isEqualTo(30000);
137   }
138 
139   @Test
testGetSupportedPreviewFrameRates()140   public void testGetSupportedPreviewFrameRates() throws Exception {
141     List<Integer> supportedRates = parameters.getSupportedPreviewFrameRates();
142     assertThat(supportedRates).isNotNull();
143     assertThat(supportedRates.size()).isEqualTo(3);
144     assertThat(supportedRates.get(0)).isEqualTo(10);
145   }
146 
147   @Test
testExposureCompensationLimits()148   public void testExposureCompensationLimits() {
149     assertThat(parameters.getMinExposureCompensation()).isEqualTo(-6);
150     assertThat(parameters.getMaxExposureCompensation()).isEqualTo(6);
151     assertThat(parameters.getExposureCompensationStep()).isEqualTo(0.5f);
152   }
153 
154   @Test
testExposureCompensationSetting()155   public void testExposureCompensationSetting() {
156     assertThat(parameters.getExposureCompensation()).isEqualTo(0);
157     parameters.setExposureCompensation(5);
158     assertThat(parameters.getExposureCompensation()).isEqualTo(5);
159   }
160 
161   @Test
testGetSupportedFocusModesDefaultValue()162   public void testGetSupportedFocusModesDefaultValue() {
163     List<String> supportedFocusModes = parameters.getSupportedFocusModes();
164     assertThat(supportedFocusModes).isEmpty();
165   }
166 
167   @Test
testSetSupportedFocusModes()168   public void testSetSupportedFocusModes() {
169     Shadows.shadowOf(parameters).setSupportedFocusModes("foo", "bar");
170     assertThat(parameters.getSupportedFocusModes()).isEqualTo(Lists.newArrayList("foo", "bar"));
171     Shadows.shadowOf(parameters).setSupportedFocusModes("baz");
172     assertThat(parameters.getSupportedFocusModes()).isEqualTo(Lists.newArrayList("baz"));
173   }
174 
175   @Test
testSetAndGetFocusMode()176   public void testSetAndGetFocusMode() {
177     parameters.setFocusMode("foo");
178     assertThat(parameters.getFocusMode()).isEqualTo("foo");
179   }
180 
181   @Test
testGetSupportedFlashModesDefaultValue()182   public void testGetSupportedFlashModesDefaultValue() {
183     List<String> supportedFlashModes = parameters.getSupportedFlashModes();
184     assertThat(supportedFlashModes).isEmpty();
185   }
186 
187   @Test
testSetSupportedFlashModes()188   public void testSetSupportedFlashModes() {
189     Shadows.shadowOf(parameters).setSupportedFlashModes("foo", "bar");
190     assertThat(parameters.getSupportedFlashModes()).containsExactly("foo", "bar").inOrder();
191     Shadows.shadowOf(parameters).setSupportedFlashModes("baz");
192     assertThat(parameters.getSupportedFlashModes()).containsExactly("baz");
193   }
194 
195   @Test
testSetAndGetFlashMode()196   public void testSetAndGetFlashMode() {
197     parameters.setFlashMode("foo");
198     assertThat(parameters.getFlashMode()).isEqualTo("foo");
199   }
200 }
201