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.content.Context;
20 import android.content.pm.PackageManager;
21 import android.hardware.cts.helpers.CameraParameterizedTestCase;
22 import android.hardware.camera2.CameraCharacteristics;
23 import android.hardware.camera2.cts.CameraTestUtils;
24 import android.hardware.camera2.CameraManager;
25 import android.util.Log;
26 
27 import java.util.Arrays;
28 
29 import org.junit.Ignore;
30 
31 import static junit.framework.Assert.assertNotNull;
32 import static junit.framework.Assert.assertTrue;
33 
34 public class Camera2ParameterizedTestCase extends CameraParameterizedTestCase {
35     private static final String TAG = "Camera2ParameterizedTestCase";
36 
37     // The list of camera ids we're testing. If we're testing system cameras
38     // (mAdoptShellPerm == true), we have only system camera ids in the array and not normal camera
39     // ids.
40     private String[] mCameraIdsUnderTest;
41 
42     protected CameraManager mCameraManager;
43     protected boolean mUseAll = false;
44 
45     @Override
setUp()46     public void setUp() throws Exception {
47         setUp(/*useAll*/false);
48     }
49 
50     /**
51      * setUp camera2 related properties for parameterized cts tests
52      *
53      * @param useAll whether all camera ids are to be used for system camera tests
54      */
setUp(boolean useAll)55     public void setUp(boolean useAll) throws Exception {
56         super.setUp();
57         mUseAll = useAll;
58         /**
59          * Workaround for mockito and JB-MR2 incompatibility
60          *
61          * Avoid java.lang.IllegalArgumentException: dexcache == null
62          * https://code.google.com/p/dexmaker/issues/detail?id=2
63          */
64         System.setProperty("dexmaker.dexcache", mContext.getCacheDir().toString());
65         mCameraManager = (CameraManager) mContext.getSystemService(Context.CAMERA_SERVICE);
66         assertNotNull("Unable to get CameraManager", mCameraManager);
67         mCameraIdsUnderTest = deriveCameraIdsUnderTest();
68         assertNotNull("Unable to get camera ids", mCameraIdsUnderTest);
69     }
70 
71     @Override
tearDown()72     public void tearDown() throws Exception {
73         String[] cameraIdsPostTest = deriveCameraIdsUnderTest();
74         assertNotNull("Camera ids shouldn't be null", cameraIdsPostTest);
75         Log.i(TAG, "Camera ids in setup:" + Arrays.toString(mCameraIdsUnderTest));
76         Log.i(TAG, "Camera ids in tearDown:" + Arrays.toString(cameraIdsPostTest));
77         assertTrue(
78                 "Number of cameras changed from " + mCameraIdsUnderTest.length + " to " +
79                 cameraIdsPostTest.length,
80                 mCameraIdsUnderTest.length == cameraIdsPostTest.length);
81         super.tearDown();
82     }
83 
deriveCameraIdsUnderTest()84     private String[] deriveCameraIdsUnderTest() throws Exception {
85         String[] idsUnderTest =
86                 mAdoptShellPerm && mUseAll ? mCameraManager.getCameraIdListNoLazy() :
87                 CameraTestUtils.getCameraIdListForTesting(mCameraManager, mAdoptShellPerm);
88         assertNotNull("Camera ids shouldn't be null", idsUnderTest);
89         if (mOverrideCameraId != null) {
90             if (Arrays.asList(idsUnderTest).contains(mOverrideCameraId)) {
91                 idsUnderTest = new String[]{mOverrideCameraId};
92             } else {
93                 idsUnderTest = new String[]{};
94             }
95         }
96 
97         return idsUnderTest;
98     }
99 
getCameraIdsUnderTest()100     public String[] getCameraIdsUnderTest() throws Exception {
101         // If external camera is supported, verify that it is connected as part of the camera Ids
102         // under test. If the external camera is not connected, an exception will be thrown to
103         // prevent bypassing CTS testing for external camera
104         CameraTestUtils.verifyExternalCameraConnected(mCameraManager.getCameraIdListNoLazy(),
105                 mContext.getPackageManager(), mCameraManager);
106 
107         return mCameraIdsUnderTest;
108     }
109 }
110