summaryrefslogtreecommitdiff
path: root/tests/camera/src/android/hardware/camera2/cts/CameraExtensionCharacteristicsTest.java
blob: d725a8a49a930422fb4b007fe79782020b2135e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
 * Copyright 2020 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package android.hardware.camera2.cts;

import android.content.Context;
import android.graphics.ImageFormat;
import android.graphics.SurfaceTexture;
import android.hardware.camera2.CameraExtensionCharacteristics;
import android.hardware.camera2.cts.helpers.StaticMetadata;
import android.hardware.camera2.cts.testcases.Camera2AndroidTestRule;
import android.platform.test.annotations.AppModeFull;
import android.renderscript.Allocation;
import android.util.Log;
import android.util.Range;
import android.util.Size;

import androidx.test.InstrumentationRegistry;

import com.android.compatibility.common.util.PropertyUtil;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static org.junit.Assert.*;

import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

@RunWith(JUnit4.class)
public class CameraExtensionCharacteristicsTest {
    private static final String TAG = "CameraExtensionManagerTest";
    private static final boolean VERBOSE = Log.isLoggable(TAG, Log.VERBOSE);
    private static final List<Integer> EXTENSIONS = Arrays.asList(
            CameraExtensionCharacteristics.EXTENSION_AUTOMATIC,
            CameraExtensionCharacteristics.EXTENSION_BEAUTY,
            CameraExtensionCharacteristics.EXTENSION_BOKEH,
            CameraExtensionCharacteristics.EXTENSION_HDR,
            CameraExtensionCharacteristics.EXTENSION_NIGHT);

    private final Context mContext = InstrumentationRegistry.getTargetContext();

    @Rule
    public final Camera2AndroidTestRule mTestRule = new Camera2AndroidTestRule(mContext);

    private void openDevice(String cameraId) throws Exception {
        mTestRule.setCamera(CameraTestUtils.openCamera(
                mTestRule.getCameraManager(), cameraId,
                mTestRule.getCameraListener(), mTestRule.getHandler()));
        mTestRule.getCollector().setCameraId(cameraId);
        mTestRule.setStaticInfo(new StaticMetadata(
                mTestRule.getCameraManager().getCameraCharacteristics(cameraId),
                StaticMetadata.CheckLevel.ASSERT, /*collector*/null));
    }

    private <T> void verifySupportedExtension(CameraExtensionCharacteristics chars, String cameraId,
            Integer extension, Class<T> klass) {
        List<Size> availableSizes = chars.getExtensionSupportedSizes(extension, klass);
        assertTrue(String.format("Supported extension %d on camera id: %s doesn't " +
                        "include any valid resolutions!", extension, cameraId),
                (availableSizes != null) && (!availableSizes.isEmpty()));
    }

    private <T> void verifySupportedSizes(CameraExtensionCharacteristics chars, String cameraId,
            Integer extension, Class<T> klass) throws Exception {
        verifySupportedExtension(chars, cameraId, extension, klass);
        try {
            openDevice(cameraId);
            List<Size> extensionSizes = chars.getExtensionSupportedSizes(extension, klass);
            List<Size> cameraSizes = Arrays.asList(
                    mTestRule.getStaticInfo().getAvailableSizesForFormatChecked(ImageFormat.PRIVATE,
                            StaticMetadata.StreamDirection.Output));
            for (Size extensionSize : extensionSizes) {
                assertTrue(String.format("Supported extension %d on camera id: %s advertises " +
                                " resolution %s unsupported by camera", extension, cameraId,
                        extensionSize), cameraSizes.contains(extensionSize));
            }
        } finally {
            mTestRule.closeDevice(cameraId);
        }
    }

    private void verifySupportedSizes(CameraExtensionCharacteristics chars, String cameraId,
            Integer extension, int format) throws Exception {
        List<Size> extensionSizes = chars.getExtensionSupportedSizes(extension, format);
        assertFalse(String.format("No available sizes for extension %d on camera id: %s " +
                "using format: %x", extension, cameraId, format), extensionSizes.isEmpty());
        try {
            openDevice(cameraId);
            List<Size> cameraSizes = Arrays.asList(
                    mTestRule.getStaticInfo().getAvailableSizesForFormatChecked(format,
                            StaticMetadata.StreamDirection.Output));
            for (Size extensionSize : extensionSizes) {
                assertTrue(String.format("Supported extension %d on camera id: %s advertises " +
                                " resolution %s unsupported by camera", extension, cameraId,
                        extensionSize), cameraSizes.contains(extensionSize));
            }
        } finally {
            mTestRule.closeDevice(cameraId);
        }
    }

    private <T> void verifyUnsupportedExtension(CameraExtensionCharacteristics chars,
            Integer extension, Class<T> klass) {
        try {
            chars.getExtensionSupportedSizes(extension, klass);
            fail("should get IllegalArgumentException due to unsupported extension");
        } catch (IllegalArgumentException e) {
            // Expected
        }
    }

    @Test
    @AppModeFull(reason = "Instant apps can't access Test API")
    public void testExtensionAvailability() throws Exception {
        boolean extensionsAdvertised = false;
        for (String id : mTestRule.getCameraIdsUnderTest()) {
            StaticMetadata staticMeta =
                    new StaticMetadata(mTestRule.getCameraManager().getCameraCharacteristics(id));
            if (!staticMeta.isColorOutputSupported()) {
                continue;
            }
            CameraExtensionCharacteristics extensionChars =
                    mTestRule.getCameraManager().getCameraExtensionCharacteristics(id);
            ArrayList<Integer> unsupportedExtensions = new ArrayList<>(EXTENSIONS);
            List<Integer> supportedExtensions = extensionChars.getSupportedExtensions();
            if (!extensionsAdvertised && !supportedExtensions.isEmpty()) {
                extensionsAdvertised = true;
            }
            for (Integer extension : supportedExtensions) {
                verifySupportedExtension(extensionChars, id, extension, SurfaceTexture.class);
                unsupportedExtensions.remove(extension);
            }

            // Unsupported extension size queries must throw corresponding exception.
            for (Integer extension : unsupportedExtensions) {
                verifyUnsupportedExtension(extensionChars, extension, SurfaceTexture.class);
            }
        }
        boolean extensionsEnabledProp = PropertyUtil.areCameraXExtensionsEnabled();
        assertEquals("Extensions system property : " + extensionsEnabledProp + " does not match " +
                "with the advertised extensions: " + extensionsAdvertised, extensionsEnabledProp,
                extensionsAdvertised);
    }

    @Test
    public void testExtensionSizes() throws Exception {
        for (String id : mTestRule.getCameraIdsUnderTest()) {
            StaticMetadata staticMeta =
                    new StaticMetadata(mTestRule.getCameraManager().getCameraCharacteristics(id));
            if (!staticMeta.isColorOutputSupported()) {
                continue;
            }
            CameraExtensionCharacteristics extensionChars =
                    mTestRule.getCameraManager().getCameraExtensionCharacteristics(id);
            List<Integer> supportedExtensions = extensionChars.getSupportedExtensions();
            for (Integer extension : supportedExtensions) {
                verifySupportedSizes(extensionChars, id, extension, SurfaceTexture.class);
                verifySupportedSizes(extensionChars, id, extension, ImageFormat.JPEG);
            }
        }
    }

    @Test
    public void testIllegalArguments() throws Exception {
        try {
            mTestRule.getCameraManager().getCameraExtensionCharacteristics("InvalidCameraId!");
            fail("should get IllegalArgumentException due to invalid camera id");
        } catch (IllegalArgumentException e) {
            // Expected
        }

        for (String id : mTestRule.getCameraIdsUnderTest()) {
            CameraExtensionCharacteristics extensionChars =
                    mTestRule.getCameraManager().getCameraExtensionCharacteristics(id);
            List<Integer> supportedExtensions = extensionChars.getSupportedExtensions();
            for (Integer extension : supportedExtensions) {
                try {
                    extensionChars.getExtensionSupportedSizes(extension, ImageFormat.UNKNOWN);
                    fail("should get IllegalArgumentException due to invalid pixel format");
                } catch (IllegalArgumentException e) {
                    // Expected
                }

                try {
                    List<Size> ret = extensionChars.getExtensionSupportedSizes(extension,
                            Allocation.class);
                    assertTrue("should get empty resolution list for unsupported " +
                            "surface type", ret.isEmpty());
                } catch (IllegalArgumentException e) {
                    fail("should not get IllegalArgumentException due to unsupported surface " +
                            "type");
                }
            }
        }
    }

    @Test
    public void testExtensionLatencyRanges() throws Exception {
        final int testFormat = ImageFormat.JPEG;
        for (String id : mTestRule.getCameraIdsUnderTest()) {
            StaticMetadata staticMeta =
                    new StaticMetadata(mTestRule.getCameraManager().getCameraCharacteristics(id));
            if (!staticMeta.isColorOutputSupported()) {
                continue;
            }

            CameraExtensionCharacteristics chars =
                    mTestRule.getCameraManager().getCameraExtensionCharacteristics(id);
            List<Integer> supportedExtensions = chars.getSupportedExtensions();
            for (Integer extension : supportedExtensions) {
                List<Size> extensionSizes = chars.getExtensionSupportedSizes(extension, testFormat);
                for (Size sz : extensionSizes) {
                    Range<Long> latencyRange = chars.getEstimatedCaptureLatencyRangeMillis(
                            extension, sz, testFormat);
                    if (latencyRange != null) {
                        assertTrue("Negative range surface type", (latencyRange.getLower() > 0) &&
                                (latencyRange.getUpper() > 0));
                        assertTrue("Lower range value must be smaller compared to the upper",
                                (latencyRange.getLower() < latencyRange.getUpper()));
                    }
                }
            }
        }
    }
}