1 /*
2  * Copyright (C) 2014 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 com.android.ex.camera2.portability;
18 
19 import static android.hardware.camera2.CaptureRequest.*;
20 import static org.junit.Assert.assertEquals;
21 import static org.junit.Assert.fail;
22 
23 import android.content.Context;
24 import android.graphics.Rect;
25 import android.hardware.camera2.CameraAccessException;
26 import android.hardware.camera2.CameraCharacteristics;
27 import android.hardware.camera2.CameraDevice;
28 import android.hardware.camera2.CameraManager;
29 import android.hardware.camera2.CaptureRequest;
30 
31 import com.android.ex.camera2.portability.CameraCapabilities.FlashMode;
32 import com.android.ex.camera2.portability.CameraCapabilities.FocusMode;
33 import com.android.ex.camera2.portability.CameraCapabilities.SceneMode;
34 import com.android.ex.camera2.portability.CameraCapabilities.Stringifier;
35 import com.android.ex.camera2.portability.CameraCapabilities.WhiteBalance;
36 import com.android.ex.camera2.utils.Camera2DeviceTester;
37 import org.junit.Test;
38 
39 import java.lang.reflect.Method;
40 import java.lang.reflect.ParameterizedType;
41 
42 public class Camera2PortabilityTest extends Camera2DeviceTester {
43     /**
44      * Ensure that applying {@code Stringifier#.*FromString()} reverses
45      * {@link Stringifier#stringify} for {@link FocusMode}, {@link FlashMode},
46      * {@link SceneMode}, and {@link WhiteBalance}.
47      */
48     @Test
cameraCapabilitiesStringifier()49     public void cameraCapabilitiesStringifier() {
50         Stringifier strfy = new Stringifier();
51         for(FocusMode val : FocusMode.values()) {
52             assertEquals(val, strfy.focusModeFromString(strfy.stringify(val)));
53         }
54         for(FlashMode val : FlashMode.values()) {
55             assertEquals(val, strfy.flashModeFromString(strfy.stringify(val)));
56         }
57         for(SceneMode val : SceneMode.values()) {
58             assertEquals(val, strfy.sceneModeFromString(strfy.stringify(val)));
59         }
60         for(WhiteBalance val : WhiteBalance.values()) {
61             assertEquals(val, strfy.whiteBalanceFromString(strfy.stringify(val)));
62         }
63     }
64 
65     /**
66      * Ensure that {@code Stringifier#.*FromString()} default to the correct
67      * {@link FocusMode}, {@link FlashMode}, {@link SceneMode}, and
68      * {@link WhiteBalance} when given a {@code null}.
69      */
70     @Test
cameraCapabilitiesStringifierNull()71     public void cameraCapabilitiesStringifierNull() {
72         Stringifier strfy = new Stringifier();
73         assertEquals(strfy.focusModeFromString(null), FocusMode.AUTO);
74         assertEquals(strfy.flashModeFromString(null), FlashMode.NO_FLASH);
75         assertEquals(strfy.sceneModeFromString(null), SceneMode.NO_SCENE_MODE);
76         assertEquals(strfy.whiteBalanceFromString(null), WhiteBalance.AUTO);
77     }
78 
79     /**
80      * Ensure that {@code Stringifier#.*FromString()} default to the correct
81      * {@link FocusMode}, {@link FlashMode}, {@link SceneMode}, and
82      * {@link WhiteBalance} when given an unrecognized string.
83      */
84     @Test
cameraCapabilitiesStringifierInvalid()85     public void cameraCapabilitiesStringifierInvalid() {
86         Stringifier strfy = new Stringifier();
87         assertEquals(strfy.focusModeFromString("crap"), FocusMode.AUTO);
88         assertEquals(strfy.flashModeFromString("crap"), FlashMode.NO_FLASH);
89         assertEquals(strfy.sceneModeFromString("crap"), SceneMode.NO_SCENE_MODE);
90         assertEquals(strfy.whiteBalanceFromString("crap"), WhiteBalance.AUTO);
91     }
92 
buildFrameworkCharacteristics()93     private CameraCharacteristics buildFrameworkCharacteristics() throws CameraAccessException {
94         CameraManager manager = (CameraManager) mContext.getSystemService(Context.CAMERA_SERVICE);
95         String id = manager.getCameraIdList()[0];
96         return manager.getCameraCharacteristics(id);
97     }
98 
camera2SettingsCheckSingleOption(AndroidCamera2Settings setts, Key<?> apiKey, int apiVal)99     private void camera2SettingsCheckSingleOption(AndroidCamera2Settings setts,
100                                                       Key<?> apiKey, int apiVal) {
101         assertEquals(apiVal, setts.getRequestSettings().get(apiKey));
102     }
103 
104     /**
105      * Ensure that {@link AndroidCamera2Settings} correctly translates its
106      * {@code FocusMode}, {@code SceneMode}, and {@code WhiteBalance} to the
107      * corresponding framework API 2 representations.
108      */
109     @Test
camera2SettingsSetOptionsAndGetRequestSettings()110     public void camera2SettingsSetOptionsAndGetRequestSettings() throws CameraAccessException {
111         // We're only testing the focus modes, scene modes and white balances,
112         // and won't use the activeArray, previewSize, or photoSize. The
113         // constructor requires the former, so pass a degenerate rectangle.
114         AndroidCamera2Settings set = new AndroidCamera2Settings(
115                 mCamera, CameraDevice.TEMPLATE_PREVIEW, /*activeArray*/new Rect(),
116                 /*previewSize*/null, /*photoSize*/null);
117 
118         // Focus modes
119         set.setFocusMode(FocusMode.AUTO);
120         camera2SettingsCheckSingleOption(set, CONTROL_AF_MODE, CONTROL_AF_MODE_AUTO);
121         set.setFocusMode(FocusMode.CONTINUOUS_PICTURE);
122         camera2SettingsCheckSingleOption(set, CONTROL_AF_MODE, CONTROL_AF_MODE_CONTINUOUS_PICTURE);
123         set.setFocusMode(FocusMode.CONTINUOUS_VIDEO);
124         camera2SettingsCheckSingleOption(set, CONTROL_AF_MODE, CONTROL_AF_MODE_CONTINUOUS_VIDEO);
125         set.setFocusMode(FocusMode.EXTENDED_DOF);
126         camera2SettingsCheckSingleOption(set, CONTROL_AF_MODE, CONTROL_AF_MODE_EDOF);
127         set.setFocusMode(FocusMode.FIXED);
128         camera2SettingsCheckSingleOption(set, CONTROL_AF_MODE, CONTROL_AF_MODE_OFF);
129         set.setFocusMode(FocusMode.MACRO);
130         camera2SettingsCheckSingleOption(set, CONTROL_AF_MODE, CONTROL_AF_MODE_MACRO);
131 
132         // Scene modes
133         set.setSceneMode(SceneMode.AUTO);
134         camera2SettingsCheckSingleOption(set, CONTROL_SCENE_MODE, CONTROL_SCENE_MODE_DISABLED);
135         set.setSceneMode(SceneMode.ACTION);
136         camera2SettingsCheckSingleOption(set, CONTROL_SCENE_MODE, CONTROL_SCENE_MODE_ACTION);
137         set.setSceneMode(SceneMode.BARCODE);
138         camera2SettingsCheckSingleOption(set, CONTROL_SCENE_MODE, CONTROL_SCENE_MODE_BARCODE);
139         set.setSceneMode(SceneMode.BEACH);
140         camera2SettingsCheckSingleOption(set, CONTROL_SCENE_MODE, CONTROL_SCENE_MODE_BEACH);
141         set.setSceneMode(SceneMode.CANDLELIGHT);
142         camera2SettingsCheckSingleOption(set, CONTROL_SCENE_MODE, CONTROL_SCENE_MODE_CANDLELIGHT);
143         set.setSceneMode(SceneMode.FIREWORKS);
144         camera2SettingsCheckSingleOption(set, CONTROL_SCENE_MODE, CONTROL_SCENE_MODE_FIREWORKS);
145         set.setSceneMode(SceneMode.LANDSCAPE);
146         camera2SettingsCheckSingleOption(set, CONTROL_SCENE_MODE, CONTROL_SCENE_MODE_LANDSCAPE);
147         set.setSceneMode(SceneMode.NIGHT);
148         camera2SettingsCheckSingleOption(set, CONTROL_SCENE_MODE, CONTROL_SCENE_MODE_NIGHT);
149         set.setSceneMode(SceneMode.PARTY);
150         camera2SettingsCheckSingleOption(set, CONTROL_SCENE_MODE, CONTROL_SCENE_MODE_PARTY);
151         set.setSceneMode(SceneMode.PORTRAIT);
152         camera2SettingsCheckSingleOption(set, CONTROL_SCENE_MODE, CONTROL_SCENE_MODE_PORTRAIT);
153         set.setSceneMode(SceneMode.SNOW);
154         camera2SettingsCheckSingleOption(set, CONTROL_SCENE_MODE, CONTROL_SCENE_MODE_SNOW);
155         set.setSceneMode(SceneMode.SPORTS);
156         camera2SettingsCheckSingleOption(set, CONTROL_SCENE_MODE, CONTROL_SCENE_MODE_SPORTS);
157         set.setSceneMode(SceneMode.STEADYPHOTO);
158         camera2SettingsCheckSingleOption(set, CONTROL_SCENE_MODE, CONTROL_SCENE_MODE_STEADYPHOTO);
159         set.setSceneMode(SceneMode.SUNSET);
160         camera2SettingsCheckSingleOption(set, CONTROL_SCENE_MODE, CONTROL_SCENE_MODE_SUNSET);
161         set.setSceneMode(SceneMode.THEATRE);
162         camera2SettingsCheckSingleOption(set, CONTROL_SCENE_MODE, CONTROL_SCENE_MODE_THEATRE);
163 
164         // White balances
165         set.setWhiteBalance(WhiteBalance.AUTO);
166         camera2SettingsCheckSingleOption(set, CONTROL_AWB_MODE, CONTROL_AWB_MODE_AUTO);
167         set.setWhiteBalance(WhiteBalance.CLOUDY_DAYLIGHT);
168         camera2SettingsCheckSingleOption(set, CONTROL_AWB_MODE, CONTROL_AWB_MODE_CLOUDY_DAYLIGHT);
169         set.setWhiteBalance(WhiteBalance.DAYLIGHT);
170         camera2SettingsCheckSingleOption(set, CONTROL_AWB_MODE, CONTROL_AWB_MODE_DAYLIGHT);
171         set.setWhiteBalance(WhiteBalance.FLUORESCENT);
172         camera2SettingsCheckSingleOption(set, CONTROL_AWB_MODE, CONTROL_AWB_MODE_FLUORESCENT);
173         set.setWhiteBalance(WhiteBalance.INCANDESCENT);
174         camera2SettingsCheckSingleOption(set, CONTROL_AWB_MODE, CONTROL_AWB_MODE_INCANDESCENT);
175         set.setWhiteBalance(WhiteBalance.SHADE);
176         camera2SettingsCheckSingleOption(set, CONTROL_AWB_MODE, CONTROL_AWB_MODE_SHADE);
177         set.setWhiteBalance(WhiteBalance.TWILIGHT);
178         camera2SettingsCheckSingleOption(set, CONTROL_AWB_MODE, CONTROL_AWB_MODE_TWILIGHT);
179         set.setWhiteBalance(WhiteBalance.WARM_FLUORESCENT);
180         camera2SettingsCheckSingleOption(set, CONTROL_AWB_MODE, CONTROL_AWB_MODE_WARM_FLUORESCENT);
181     }
182 
183     // TODO: Add a test checking whether stringification matches API 1 representation
184 
185     /**
186      * Ensure that {@code AndroidCamera2Capabilities#.*FromInt} correctly
187      * translates from framework API 2 representations to the equivalent
188      * {@code FocusMode}s, {@code SceneMode}s, and {@code WhiteBalance}s.
189      */
190     @Test
camera2CapabilitiesFocusModeFromInt()191     public void camera2CapabilitiesFocusModeFromInt() throws CameraAccessException {
192         CameraCharacteristics chars = buildFrameworkCharacteristics();
193         AndroidCamera2Capabilities intstr = new AndroidCamera2Capabilities(chars);
194 
195         // Focus modes
196         assertEquals(intstr.focusModeFromInt(CONTROL_AF_MODE_AUTO), FocusMode.AUTO);
197         assertEquals(intstr.focusModeFromInt(CONTROL_AF_MODE_CONTINUOUS_PICTURE),
198                 FocusMode.CONTINUOUS_PICTURE);
199         assertEquals(intstr.focusModeFromInt(CONTROL_AF_MODE_CONTINUOUS_VIDEO),
200                 FocusMode.CONTINUOUS_VIDEO);
201         assertEquals(intstr.focusModeFromInt(CONTROL_AF_MODE_EDOF), FocusMode.EXTENDED_DOF);
202         assertEquals(intstr.focusModeFromInt(CONTROL_AF_MODE_OFF), FocusMode.FIXED);
203         assertEquals(intstr.focusModeFromInt(CONTROL_AF_MODE_MACRO), FocusMode.MACRO);
204 
205         // Scene modes
206         assertEquals(intstr.sceneModeFromInt(CONTROL_SCENE_MODE_DISABLED), SceneMode.AUTO);
207         assertEquals(intstr.sceneModeFromInt(CONTROL_SCENE_MODE_ACTION), SceneMode.ACTION);
208         assertEquals(intstr.sceneModeFromInt(CONTROL_SCENE_MODE_BARCODE), SceneMode.BARCODE);
209         assertEquals(intstr.sceneModeFromInt(CONTROL_SCENE_MODE_BEACH), SceneMode.BEACH);
210         assertEquals(intstr.sceneModeFromInt(CONTROL_SCENE_MODE_CANDLELIGHT), SceneMode.CANDLELIGHT);
211         assertEquals(intstr.sceneModeFromInt(CONTROL_SCENE_MODE_FIREWORKS), SceneMode.FIREWORKS);
212         assertEquals(intstr.sceneModeFromInt(CONTROL_SCENE_MODE_LANDSCAPE), SceneMode.LANDSCAPE);
213         assertEquals(intstr.sceneModeFromInt(CONTROL_SCENE_MODE_NIGHT), SceneMode.NIGHT);
214         assertEquals(intstr.sceneModeFromInt(CONTROL_SCENE_MODE_PARTY), SceneMode.PARTY);
215         assertEquals(intstr.sceneModeFromInt(CONTROL_SCENE_MODE_PORTRAIT), SceneMode.PORTRAIT);
216         assertEquals(intstr.sceneModeFromInt(CONTROL_SCENE_MODE_SNOW), SceneMode.SNOW);
217         assertEquals(intstr.sceneModeFromInt(CONTROL_SCENE_MODE_SPORTS), SceneMode.SPORTS);
218         assertEquals(intstr.sceneModeFromInt(CONTROL_SCENE_MODE_STEADYPHOTO),
219                 SceneMode.STEADYPHOTO);
220         assertEquals(intstr.sceneModeFromInt(CONTROL_SCENE_MODE_SUNSET), SceneMode.SUNSET);
221         assertEquals(intstr.sceneModeFromInt(CONTROL_SCENE_MODE_THEATRE), SceneMode.THEATRE);
222 
223         // White balances
224         assertEquals(intstr.whiteBalanceFromInt(CONTROL_AWB_MODE_AUTO), WhiteBalance.AUTO);
225         assertEquals(intstr.whiteBalanceFromInt(CONTROL_AWB_MODE_CLOUDY_DAYLIGHT),
226                 WhiteBalance.CLOUDY_DAYLIGHT);
227         assertEquals(intstr.whiteBalanceFromInt(CONTROL_AWB_MODE_DAYLIGHT), WhiteBalance.DAYLIGHT);
228         assertEquals(intstr.whiteBalanceFromInt(CONTROL_AWB_MODE_FLUORESCENT),
229                 WhiteBalance.FLUORESCENT);
230         assertEquals(intstr.whiteBalanceFromInt(CONTROL_AWB_MODE_INCANDESCENT),
231                 WhiteBalance.INCANDESCENT);
232         assertEquals(intstr.whiteBalanceFromInt(CONTROL_AWB_MODE_SHADE), WhiteBalance.SHADE);
233         assertEquals(intstr.whiteBalanceFromInt(CONTROL_AWB_MODE_TWILIGHT), WhiteBalance.TWILIGHT);
234         assertEquals(intstr.whiteBalanceFromInt(CONTROL_AWB_MODE_WARM_FLUORESCENT),
235                 WhiteBalance.WARM_FLUORESCENT);
236     }
237 }
238