• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package android.hardware.camera2.cts;
18 
19 import android.graphics.ColorSpace;
20 import android.graphics.ImageFormat;
21 import android.graphics.Point;
22 import android.graphics.Rect;
23 import android.hardware.camera2.CameraCharacteristics;
24 import android.hardware.camera2.CaptureRequest;
25 import android.hardware.camera2.CaptureResult;
26 import android.hardware.camera2.params.BlackLevelPattern;
27 import android.hardware.camera2.params.Capability;
28 import android.hardware.camera2.params.ColorSpaceProfiles;
29 import android.hardware.camera2.params.DeviceStateSensorOrientationMap;
30 import android.hardware.camera2.params.DynamicRangeProfiles;
31 import android.hardware.camera2.params.Face;
32 import android.util.Range;
33 import android.util.Size;
34 
35 import androidx.test.runner.AndroidJUnit4;
36 
37 import com.android.compatibility.common.util.ApiTest;
38 
39 import org.junit.Assert;
40 import org.junit.Test;
41 import org.junit.runner.RunWith;
42 
43 import java.util.Set;
44 
45 /**
46  * Test CaptureRequest/Result/CameraCharacteristics.Key objects.
47  */
48 @RunWith(AndroidJUnit4.class)
49 @SuppressWarnings("EqualsIncompatibleType")
50 public class SimpleObjectsTest {
51 
52     @Test
cameraKeysTest()53     public void cameraKeysTest() throws Exception {
54         String keyName = "android.testing.Key";
55         String keyName2 = "android.testing.Key2";
56 
57         CaptureRequest.Key<Integer> testRequestKey =
58                 new CaptureRequest.Key<>(keyName, Integer.class);
59         Assert.assertEquals("Request key name not correct",
60                 testRequestKey.getName(), keyName);
61 
62         CaptureResult.Key<Integer> testResultKey =
63                 new CaptureResult.Key<>(keyName, Integer.class);
64         Assert.assertEquals("Result key name not correct",
65                 testResultKey.getName(), keyName);
66 
67         CameraCharacteristics.Key<Integer> testCharacteristicsKey =
68                 new CameraCharacteristics.Key<>(keyName, Integer.class);
69         Assert.assertEquals("Characteristics key name not correct",
70                 testCharacteristicsKey.getName(), keyName);
71 
72         CaptureRequest.Key<Integer> testRequestKey2 =
73                 new CaptureRequest.Key<>(keyName, Integer.class);
74         Assert.assertEquals("Two request keys with same name/type should be equal",
75                 testRequestKey, testRequestKey2);
76         CaptureRequest.Key<Byte> testRequestKey3 =
77                 new CaptureRequest.Key<>(keyName, Byte.class);
78         Assert.assertTrue("Two request keys with different types should not be equal",
79                 !testRequestKey.equals(testRequestKey3));
80         CaptureRequest.Key<Integer> testRequestKey4 =
81                 new CaptureRequest.Key<>(keyName2, Integer.class);
82         Assert.assertTrue("Two request keys with different names should not be equal",
83                 !testRequestKey.equals(testRequestKey4));
84         CaptureRequest.Key<Byte> testRequestKey5 =
85                 new CaptureRequest.Key<>(keyName2, Byte.class);
86         Assert.assertTrue("Two request keys with different types and names should not be equal",
87                 !testRequestKey.equals(testRequestKey5));
88 
89         CaptureResult.Key<Integer> testResultKey2 =
90                 new CaptureResult.Key<>(keyName, Integer.class);
91         Assert.assertEquals("Two result keys with same name/type should be equal",
92                 testResultKey, testResultKey2);
93         CaptureResult.Key<Byte> testResultKey3 =
94                 new CaptureResult.Key<>(keyName, Byte.class);
95         Assert.assertTrue("Two result keys with different types should not be equal",
96                 !testResultKey.equals(testResultKey3));
97         CaptureResult.Key<Integer> testResultKey4 =
98                 new CaptureResult.Key<>(keyName2, Integer.class);
99         Assert.assertTrue("Two result keys with different names should not be equal",
100                 !testResultKey.equals(testResultKey4));
101         CaptureResult.Key<Byte> testResultKey5 =
102                 new CaptureResult.Key<>(keyName2, Byte.class);
103         Assert.assertTrue("Two result keys with different types and names should not be equal",
104                 !testResultKey.equals(testResultKey5));
105 
106         CameraCharacteristics.Key<Integer> testCharacteristicsKey2 =
107                 new CameraCharacteristics.Key<>(keyName, Integer.class);
108         Assert.assertEquals("Two characteristics keys with same name/type should be equal",
109                 testCharacteristicsKey, testCharacteristicsKey2);
110         CameraCharacteristics.Key<Byte> testCharacteristicsKey3 =
111                 new CameraCharacteristics.Key<>(keyName, Byte.class);
112         Assert.assertTrue("Two characteristics keys with different types should not be equal",
113                 !testCharacteristicsKey.equals(testCharacteristicsKey3));
114         CameraCharacteristics.Key<Integer> testCharacteristicsKey4 =
115                 new CameraCharacteristics.Key<>(keyName2, Integer.class);
116         Assert.assertTrue("Two characteristics keys with different names should not be equal",
117                 !testCharacteristicsKey.equals(testCharacteristicsKey4));
118         CameraCharacteristics.Key<Byte> testCharacteristicsKey5 =
119                 new CameraCharacteristics.Key<>(keyName2, Byte.class);
120         Assert.assertTrue(
121                 "Two characteristics keys with different types and names should not be equal",
122                 !testCharacteristicsKey.equals(testCharacteristicsKey5));
123 
124     }
125 
126     @Test
blackLevelPatternConstructionTest()127     public void blackLevelPatternConstructionTest() {
128         int[] offsets = { 5, 5, 5, 5 };
129         BlackLevelPattern blackLevelPattern = new BlackLevelPattern(offsets);
130 
131         try {
132             blackLevelPattern = new BlackLevelPattern(/*offsets*/ null);
133             Assert.fail("BlackLevelPattern did not throw a NullPointerException for null offsets");
134         } catch (NullPointerException e) {
135             // Do nothing
136         }
137 
138         try {
139             int[] badOffsets = { 5 };
140             blackLevelPattern = new BlackLevelPattern(badOffsets);
141             Assert.fail("BlackLevelPattern did not throw an IllegalArgumentException for incorrect"
142                     + " offsets length");
143         } catch (IllegalArgumentException e) {
144             // Do nothing
145         }
146     }
147 
148     @Test
capabilityConstructionTest()149     public void capabilityConstructionTest() {
150         Capability capability = new Capability(/*mode*/ 0, /*maxStreamingSize*/ new Size(128, 128),
151                 /*zoomRatioRange*/ new Range<Float>(0.2f, 2.0f));
152 
153         try {
154             capability = new Capability(/*mode*/ 0, /*maxStreamingSize*/ new Size(-1, 128),
155                     /*zoomRatioRange*/ new Range<Float>(0.2f, 2.0f));
156             Assert.fail("Capability did not throw an IllegalArgumentException for negative "
157                     + "maxStreamingWidth");
158         } catch (IllegalArgumentException e) {
159             // Do nothing
160         }
161 
162         try {
163             capability = new Capability(/*mode*/ 0, /*maxStreamingSize*/ new Size(128, -1),
164                      /*zoomRatioRange*/ new Range<Float>(0.2f, 2.0f));
165             Assert.fail("Capability did not throw an IllegalArgumentException for negative "
166                     + "maxStreamingHeight");
167         } catch (IllegalArgumentException e) {
168             // Do nothing
169         }
170 
171         try {
172             capability = new Capability(/*mode*/ 0, /*maxStreamingSize*/ new Size(128, 128),
173                     /*zoomRatioRange*/ new Range<Float>(-3.0f, -2.0f));
174             Assert.fail("Capability did not throw an IllegalArgumentException for negative "
175                     + "zoom ratios");
176         } catch (IllegalArgumentException e) {
177             // Do nothing
178         }
179 
180         try {
181             capability = new Capability(/*mode*/ 0, /*maxStreamingSize*/ new Size(128, 128),
182                     /*zoomRatioRange*/ new Range<Float>(3.0f, 2.0f));
183             Assert.fail("Capability did not throw an IllegalArgumentException for minZoomRatio "
184                     + "> maxZoomRatio");
185         } catch (IllegalArgumentException e) {
186             // Do nothing
187         }
188     }
189 
190     @Test
deviceStateSensorOrientationMapConstructionTest()191     public void deviceStateSensorOrientationMapConstructionTest() {
192         DeviceStateSensorOrientationMap.Builder builder =
193                 new DeviceStateSensorOrientationMap.Builder();
194         DeviceStateSensorOrientationMap deviceStateSensorOrientationMap =
195                 builder.addOrientationForState(/*deviceState*/ 0, /*angle*/ 90)
196                        .addOrientationForState(/*deviceState*/ 1, /*angle*/ 180)
197                        .addOrientationForState(/*deviceState*/ 2, /*angle*/ 270)
198                        .build();
199 
200         try {
201             builder = new DeviceStateSensorOrientationMap.Builder();
202             deviceStateSensorOrientationMap = builder.build();
203             Assert.fail("DeviceStateSensorOrientationMap did not throw an IllegalStateException for"
204                     + " zero elements");
205         } catch (IllegalStateException e) {
206             // Do nothing
207         }
208 
209         try {
210             builder = new DeviceStateSensorOrientationMap.Builder();
211             deviceStateSensorOrientationMap = builder.addOrientationForState(/*deviceState*/ 0, 55)
212                                                      .build();
213             Assert.fail("DeviceStateSensorOrientationMap did not throw an IllegalArgumentException "
214                     + "for incorrect elements values");
215         } catch (IllegalArgumentException e) {
216             // Do nothing
217         }
218     }
219 
220     @Test
faceConstructionTest()221     public void faceConstructionTest() {
222         Rect bounds = new Rect();
223         int score = 50;
224         int id = 1;
225         Point leftEyePosition = new Point();
226         Point rightEyePosition = new Point();
227         Point mouthPosition = new Point();
228 
229         Face.Builder builder = new Face.Builder();
230         Face face = builder.setBounds(bounds)
231                            .setScore(score)
232                            .setId(id)
233                            .setLeftEyePosition(leftEyePosition)
234                            .setRightEyePosition(rightEyePosition)
235                            .setMouthPosition(mouthPosition)
236                            .build();
237 
238         builder = new Face.Builder();
239         face = builder.setBounds(bounds)
240                       .setScore(score)
241                       .build();
242 
243         try {
244             builder = new Face.Builder();
245             face = builder.build();
246             Assert.fail("Face.Builder did not throw an IllegalStateException for unset bounds "
247                     + "and score.");
248         } catch (IllegalStateException e) {
249             // Do nothing
250         }
251 
252         try {
253             builder = new Face.Builder();
254             face = builder.setBounds(bounds)
255                           .build();
256             Assert.fail("Face.Builder did not throw an IllegalStateException for unset score.");
257         } catch (IllegalStateException e) {
258             // Do nothing
259         }
260 
261         try {
262             builder = new Face.Builder();
263             face = builder.setBounds(null)
264                     .setScore(score)
265                     .setId(id)
266                     .setLeftEyePosition(leftEyePosition)
267                     .setRightEyePosition(rightEyePosition)
268                     .setMouthPosition(mouthPosition)
269                     .build();
270             Assert.fail("Face did not throw an IllegalArgumentException for null bounds");
271         } catch (IllegalArgumentException e) {
272             // Do nothing
273         }
274 
275         try {
276             builder = new Face.Builder();
277             face = builder.setBounds(bounds)
278                           .setScore(Face.SCORE_MIN - 1)
279                           .setId(id)
280                           .setLeftEyePosition(leftEyePosition)
281                           .setRightEyePosition(rightEyePosition)
282                           .setMouthPosition(mouthPosition)
283                           .build();
284             Assert.fail("Face did not throw an IllegalArgumentException for score below range");
285         } catch (IllegalArgumentException e) {
286             // Do nothing
287         }
288 
289         try {
290             builder = new Face.Builder();
291             face = builder.setBounds(bounds)
292                           .setScore(Face.SCORE_MAX + 1)
293                           .setId(id)
294                           .setLeftEyePosition(leftEyePosition)
295                           .setRightEyePosition(rightEyePosition)
296                           .setMouthPosition(mouthPosition)
297                           .build();
298             Assert.fail("Face did not throw an IllegalArgumentException for score above range");
299         } catch (IllegalArgumentException e) {
300             // Do nothing
301         }
302 
303         try {
304             builder = new Face.Builder();
305             face = builder.setBounds(bounds)
306                           .setScore(score)
307                           .setId(Face.ID_UNSUPPORTED)
308                           .setLeftEyePosition(leftEyePosition)
309                           .setRightEyePosition(rightEyePosition)
310                           .setMouthPosition(mouthPosition)
311                           .build();
312             Assert.fail("Face did not throw an IllegalArgumentException for non-null positions when"
313                     + " id is ID_UNSUPPORTED.");
314         } catch (IllegalArgumentException e) {
315             // Do nothing
316         }
317 
318         try {
319             builder = new Face.Builder();
320             face = builder.setBounds(bounds)
321                           .setScore(score)
322                           .setId(Face.ID_UNSUPPORTED)
323                           .setLeftEyePosition(leftEyePosition)
324                           .setRightEyePosition(rightEyePosition)
325                           .setMouthPosition(null)
326                           .build();
327             Assert.fail("Face did not throw an IllegalArgumentException for partially defined "
328                     + "face features");
329         } catch (IllegalArgumentException e) {
330             // Do nothing
331         }
332     }
333 
334     @Test
335     @ApiTest(apis = {
336             "android.hardware.camera2.params.ColorSpaceProfiles#getSupportedColorSpaces",
337             "android.hardware.camera2.params.ColorSpaceProfiles#getSupportedColorSpacesForDynamicRange",
338             "android.hardware.camera2.params.ColorSpaceProfiles#getSupportedImageFormatsForColorSpace",
339             "android.hardware.camera2.params.ColorSpaceProfiles#getSupportedDynamicRangeProfiles"})
colorSpaceProfilesTest()340     public void colorSpaceProfilesTest() {
341         long[] elements = {
342                 ColorSpace.Named.DISPLAY_P3.ordinal(), ImageFormat.YUV_420_888,
343                 DynamicRangeProfiles.STANDARD
344         };
345 
346         ColorSpaceProfiles colorSpaceProfiles = new ColorSpaceProfiles(elements);
347         Set<ColorSpace.Named> supportedColorSpaces =
348                 colorSpaceProfiles.getSupportedColorSpaces(ImageFormat.UNKNOWN);
349         ColorSpace.Named[] arrColorSpaces = supportedColorSpaces.toArray(new ColorSpace.Named[0]);
350         Assert.assertEquals(arrColorSpaces.length, 1);
351         Assert.assertEquals(arrColorSpaces[0], ColorSpace.Named.DISPLAY_P3);
352 
353         supportedColorSpaces =
354                 colorSpaceProfiles.getSupportedColorSpaces(ImageFormat.YUV_420_888);
355         arrColorSpaces = supportedColorSpaces.toArray(new ColorSpace.Named[0]);
356         Assert.assertEquals(arrColorSpaces.length, 1);
357         Assert.assertEquals(arrColorSpaces[0], ColorSpace.Named.DISPLAY_P3);
358 
359         // getSupportedColorSpaces should return an empty set on an unsupported image format
360         supportedColorSpaces =
361                 colorSpaceProfiles.getSupportedColorSpaces(ImageFormat.PRIVATE);
362         arrColorSpaces = supportedColorSpaces.toArray(new ColorSpace.Named[0]);
363         Assert.assertEquals(arrColorSpaces.length, 0);
364 
365         Set<Integer> imageFormats =
366                 colorSpaceProfiles.getSupportedImageFormatsForColorSpace(
367                                 ColorSpace.Named.DISPLAY_P3);
368         Integer[] arrImageFormats = imageFormats.toArray(new Integer[0]);
369         Assert.assertEquals(arrImageFormats.length, 1);
370         Assert.assertTrue(arrImageFormats[0] == ImageFormat.YUV_420_888);
371 
372         // getSupportedImageFormatsForColorSpace should return an empty set on an unsupported color
373         // space
374         imageFormats =
375                 colorSpaceProfiles.getSupportedImageFormatsForColorSpace(ColorSpace.Named.SRGB);
376         arrImageFormats = imageFormats.toArray(new Integer[0]);
377         Assert.assertEquals(arrImageFormats.length, 0);
378 
379         Set<Long> dynamicRangeProfiles =
380                 colorSpaceProfiles.getSupportedDynamicRangeProfiles(ColorSpace.Named.DISPLAY_P3,
381                         ImageFormat.UNKNOWN);
382         Long[] arrDynamicRangeProfiles = dynamicRangeProfiles.toArray(new Long[0]);
383         Assert.assertEquals(arrDynamicRangeProfiles.length, 1);
384         Assert.assertTrue(arrDynamicRangeProfiles[0] == DynamicRangeProfiles.STANDARD);
385 
386         dynamicRangeProfiles =
387                 colorSpaceProfiles.getSupportedDynamicRangeProfiles(ColorSpace.Named.DISPLAY_P3,
388                         ImageFormat.YUV_420_888);
389         arrDynamicRangeProfiles = dynamicRangeProfiles.toArray(new Long[0]);
390         Assert.assertEquals(arrDynamicRangeProfiles.length, 1);
391         Assert.assertTrue(arrDynamicRangeProfiles[0] == DynamicRangeProfiles.STANDARD);
392 
393         // getSupportedDynamicRangeProfiles should return an empty set on an unsupported image
394         // format
395         dynamicRangeProfiles =
396                 colorSpaceProfiles.getSupportedDynamicRangeProfiles(ColorSpace.Named.DISPLAY_P3,
397                         ImageFormat.PRIVATE);
398         arrDynamicRangeProfiles = dynamicRangeProfiles.toArray(new Long[0]);
399         Assert.assertEquals(arrDynamicRangeProfiles.length, 0);
400 
401         // getSupportedDynamicRangeProfiles should return an empty set on an unsupported color space
402         dynamicRangeProfiles =
403                 colorSpaceProfiles.getSupportedDynamicRangeProfiles(ColorSpace.Named.SRGB,
404                         ImageFormat.UNKNOWN);
405         arrDynamicRangeProfiles = dynamicRangeProfiles.toArray(new Long[0]);
406         Assert.assertEquals(arrDynamicRangeProfiles.length, 0);
407 
408         supportedColorSpaces =
409                 colorSpaceProfiles.getSupportedColorSpacesForDynamicRange(ImageFormat.UNKNOWN,
410                         DynamicRangeProfiles.STANDARD);
411         arrColorSpaces = supportedColorSpaces.toArray(new ColorSpace.Named[0]);
412         Assert.assertEquals(arrColorSpaces.length, 1);
413         Assert.assertEquals(arrColorSpaces[0], ColorSpace.Named.DISPLAY_P3);
414 
415         supportedColorSpaces =
416                 colorSpaceProfiles.getSupportedColorSpacesForDynamicRange(ImageFormat.YUV_420_888,
417                         DynamicRangeProfiles.STANDARD);
418         arrColorSpaces = supportedColorSpaces.toArray(new ColorSpace.Named[0]);
419         Assert.assertEquals(arrColorSpaces.length, 1);
420         Assert.assertEquals(arrColorSpaces[0], ColorSpace.Named.DISPLAY_P3);
421 
422         // getSupportedColorSpacesForDynamicRange should return an empty set un an unsupported image
423         // format
424         supportedColorSpaces =
425                 colorSpaceProfiles.getSupportedColorSpacesForDynamicRange(ImageFormat.PRIVATE,
426                         DynamicRangeProfiles.STANDARD);
427         arrColorSpaces = supportedColorSpaces.toArray(new ColorSpace.Named[0]);
428         Assert.assertEquals(arrColorSpaces.length, 0);
429 
430         // getSupportedColorSpacesForDynamicRange should return an empty set un an unsupported
431         // dynamic range profile
432         supportedColorSpaces =
433                 colorSpaceProfiles.getSupportedColorSpacesForDynamicRange(ImageFormat.UNKNOWN,
434                         DynamicRangeProfiles.HLG10);
435         arrColorSpaces = supportedColorSpaces.toArray(new ColorSpace.Named[0]);
436         Assert.assertEquals(arrColorSpaces.length, 0);
437     }
438 
439 }
440