Timothy Knight | d8b3e52 | 2013-07-25 15:54:46 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2013 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 | |
Zhijun He | c66004f | 2014-03-04 17:58:56 -0800 | [diff] [blame] | 19 | import static org.mockito.Mockito.*; |
| 20 | import static org.mockito.AdditionalMatchers.not; |
| 21 | import static org.mockito.AdditionalMatchers.and; |
| 22 | |
Timothy Knight | d8b3e52 | 2013-07-25 15:54:46 -0700 | [diff] [blame] | 23 | import android.content.Context; |
| 24 | import android.content.pm.PackageManager; |
| 25 | import android.hardware.camera2.CameraAccessException; |
Igor Murashkin | afce1a1 | 2013-09-10 12:29:54 -0700 | [diff] [blame] | 26 | import android.hardware.camera2.CameraCharacteristics; |
Timothy Knight | d8b3e52 | 2013-07-25 15:54:46 -0700 | [diff] [blame] | 27 | import android.hardware.camera2.CameraDevice; |
Eino-Ville Talvala | c0dd022 | 2014-09-04 15:07:58 -0700 | [diff] [blame] | 28 | import android.hardware.camera2.CameraDevice.StateCallback; |
Timothy Knight | d8b3e52 | 2013-07-25 15:54:46 -0700 | [diff] [blame] | 29 | import android.hardware.camera2.CameraManager; |
Eino-Ville Talvala | c0dd022 | 2014-09-04 15:07:58 -0700 | [diff] [blame] | 30 | import android.hardware.camera2.cts.CameraTestUtils.MockStateCallback; |
Zhijun He | 884dc00 | 2014-03-20 00:03:10 -0700 | [diff] [blame] | 31 | import android.hardware.camera2.cts.helpers.CameraErrorCollector; |
Eino-Ville Talvala | 92f595c | 2013-09-09 13:48:27 -0700 | [diff] [blame] | 32 | import android.os.Handler; |
Zhijun He | b5b458b | 2014-01-07 11:49:03 -0800 | [diff] [blame] | 33 | import android.os.HandlerThread; |
Timothy Knight | d8b3e52 | 2013-07-25 15:54:46 -0700 | [diff] [blame] | 34 | import android.test.AndroidTestCase; |
| 35 | import android.util.Log; |
| 36 | |
Eino-Ville Talvala | c0dd022 | 2014-09-04 15:07:58 -0700 | [diff] [blame] | 37 | import com.android.ex.camera2.blocking.BlockingStateCallback; |
Zhijun He | c66004f | 2014-03-04 17:58:56 -0800 | [diff] [blame] | 38 | |
| 39 | import org.mockito.ArgumentCaptor; |
| 40 | import org.mockito.InOrder; |
| 41 | |
| 42 | import java.util.ArrayList; |
Timothy Knight | d8b3e52 | 2013-07-25 15:54:46 -0700 | [diff] [blame] | 43 | import java.util.Arrays; |
Eino-Ville Talvala | 851d48e | 2014-10-29 16:16:40 -0700 | [diff] [blame] | 44 | import java.util.HashSet; |
Zhijun He | c66004f | 2014-03-04 17:58:56 -0800 | [diff] [blame] | 45 | import java.util.List; |
Eino-Ville Talvala | 851d48e | 2014-10-29 16:16:40 -0700 | [diff] [blame] | 46 | import java.util.concurrent.LinkedBlockingQueue; |
Timothy Knight | d8b3e52 | 2013-07-25 15:54:46 -0700 | [diff] [blame] | 47 | |
| 48 | /** |
| 49 | * <p>Basic test for CameraManager class.</p> |
| 50 | */ |
| 51 | public class CameraManagerTest extends AndroidTestCase { |
| 52 | private static final String TAG = "CameraManagerTest"; |
| 53 | private static final boolean VERBOSE = Log.isLoggable(TAG, Log.VERBOSE); |
Timothy Knight | d8b3e52 | 2013-07-25 15:54:46 -0700 | [diff] [blame] | 54 | private static final int NUM_CAMERA_REOPENS = 10; |
| 55 | |
| 56 | private PackageManager mPackageManager; |
| 57 | private CameraManager mCameraManager; |
| 58 | private NoopCameraListener mListener; |
Zhijun He | b5b458b | 2014-01-07 11:49:03 -0800 | [diff] [blame] | 59 | private HandlerThread mHandlerThread; |
Igor Murashkin | 14b7a5e | 2013-09-17 14:31:16 -0700 | [diff] [blame] | 60 | private Handler mHandler; |
Eino-Ville Talvala | c0dd022 | 2014-09-04 15:07:58 -0700 | [diff] [blame] | 61 | private BlockingStateCallback mCameraListener; |
Zhijun He | 884dc00 | 2014-03-20 00:03:10 -0700 | [diff] [blame] | 62 | private CameraErrorCollector mCollector; |
Timothy Knight | d8b3e52 | 2013-07-25 15:54:46 -0700 | [diff] [blame] | 63 | |
| 64 | @Override |
| 65 | public void setContext(Context context) { |
| 66 | super.setContext(context); |
| 67 | mCameraManager = (CameraManager)context.getSystemService(Context.CAMERA_SERVICE); |
| 68 | assertNotNull("Can't connect to camera manager", mCameraManager); |
| 69 | mPackageManager = context.getPackageManager(); |
| 70 | assertNotNull("Can't get package manager", mPackageManager); |
| 71 | mListener = new NoopCameraListener(); |
| 72 | } |
| 73 | |
| 74 | @Override |
| 75 | protected void setUp() throws Exception { |
| 76 | super.setUp(); |
Brett Chabot | 4d98e31 | 2014-03-17 16:54:41 -0700 | [diff] [blame] | 77 | |
Igor Murashkin | 99f0a85 | 2014-06-19 13:28:59 -0700 | [diff] [blame] | 78 | /** |
| 79 | * Workaround for mockito and JB-MR2 incompatibility |
| 80 | * |
| 81 | * Avoid java.lang.IllegalArgumentException: dexcache == null |
| 82 | * https://code.google.com/p/dexmaker/issues/detail?id=2 |
| 83 | */ |
| 84 | System.setProperty("dexmaker.dexcache", getContext().getCacheDir().toString()); |
| 85 | |
Eino-Ville Talvala | c0dd022 | 2014-09-04 15:07:58 -0700 | [diff] [blame] | 86 | mCameraListener = spy(new BlockingStateCallback()); |
Igor Murashkin | 14b7a5e | 2013-09-17 14:31:16 -0700 | [diff] [blame] | 87 | |
Zhijun He | b5b458b | 2014-01-07 11:49:03 -0800 | [diff] [blame] | 88 | mHandlerThread = new HandlerThread(TAG); |
| 89 | mHandlerThread.start(); |
| 90 | mHandler = new Handler(mHandlerThread.getLooper()); |
Zhijun He | 884dc00 | 2014-03-20 00:03:10 -0700 | [diff] [blame] | 91 | mCollector = new CameraErrorCollector(); |
Timothy Knight | d8b3e52 | 2013-07-25 15:54:46 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | @Override |
| 95 | protected void tearDown() throws Exception { |
Zhijun He | b5b458b | 2014-01-07 11:49:03 -0800 | [diff] [blame] | 96 | mHandlerThread.quitSafely(); |
Igor Murashkin | 14b7a5e | 2013-09-17 14:31:16 -0700 | [diff] [blame] | 97 | mHandler = null; |
| 98 | |
Zhijun He | 884dc00 | 2014-03-20 00:03:10 -0700 | [diff] [blame] | 99 | try { |
| 100 | mCollector.verify(); |
| 101 | } catch (Throwable e) { |
| 102 | // When new Exception(e) is used, exception info will be printed twice. |
| 103 | throw new Exception(e.getMessage()); |
| 104 | } finally { |
| 105 | super.tearDown(); |
| 106 | } |
Timothy Knight | d8b3e52 | 2013-07-25 15:54:46 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Zhijun He | c66004f | 2014-03-04 17:58:56 -0800 | [diff] [blame] | 109 | /** |
| 110 | * Verifies that the reason is in the range of public-only codes. |
| 111 | */ |
| 112 | private static int checkCameraAccessExceptionReason(CameraAccessException e) { |
| 113 | int reason = e.getReason(); |
| 114 | |
| 115 | switch (reason) { |
| 116 | case CameraAccessException.CAMERA_DISABLED: |
| 117 | case CameraAccessException.CAMERA_DISCONNECTED: |
| 118 | case CameraAccessException.CAMERA_ERROR: |
| 119 | return reason; |
| 120 | } |
| 121 | |
| 122 | fail("Invalid CameraAccessException code: " + reason); |
| 123 | |
| 124 | return -1; // unreachable |
| 125 | } |
| 126 | |
Timothy Knight | d8b3e52 | 2013-07-25 15:54:46 -0700 | [diff] [blame] | 127 | public void testCameraManagerGetDeviceIdList() throws Exception { |
| 128 | |
Eino-Ville Talvala | 92f595c | 2013-09-09 13:48:27 -0700 | [diff] [blame] | 129 | // Test: that the getCameraIdList method runs without exceptions. |
| 130 | String[] ids = mCameraManager.getCameraIdList(); |
Timothy Knight | d8b3e52 | 2013-07-25 15:54:46 -0700 | [diff] [blame] | 131 | if (VERBOSE) Log.v(TAG, "CameraManager ids: " + Arrays.toString(ids)); |
| 132 | |
Zhijun He | f639d7c | 2013-10-01 12:28:39 -0700 | [diff] [blame] | 133 | /** |
| 134 | * Test: that if there is at least one reported id, then the system must have |
| 135 | * the FEATURE_CAMERA_ANY feature. |
| 136 | */ |
| 137 | assertTrue("System camera feature and camera id list don't match", |
| 138 | ids.length == 0 || |
| 139 | mPackageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY)); |
Timothy Knight | d8b3e52 | 2013-07-25 15:54:46 -0700 | [diff] [blame] | 140 | |
| 141 | /** |
Zhijun He | f639d7c | 2013-10-01 12:28:39 -0700 | [diff] [blame] | 142 | * Test: that if the device has front or rear facing cameras, then there |
| 143 | * must be matched system features. |
Timothy Knight | d8b3e52 | 2013-07-25 15:54:46 -0700 | [diff] [blame] | 144 | */ |
Zhijun He | f639d7c | 2013-10-01 12:28:39 -0700 | [diff] [blame] | 145 | for (int i = 0; i < ids.length; i++) { |
| 146 | CameraCharacteristics props = mCameraManager.getCameraCharacteristics(ids[i]); |
| 147 | assertNotNull("Can't get camera characteristics for camera " + ids[i], props); |
| 148 | Integer lensFacing = props.get(CameraCharacteristics.LENS_FACING); |
| 149 | assertNotNull("Can't get lens facing info", lensFacing); |
| 150 | if (lensFacing == CameraCharacteristics.LENS_FACING_FRONT) { |
| 151 | assertTrue("System doesn't have front camera feature", |
Takayuki Hoshi | 658da1e | 2015-10-01 12:52:15 +0900 | [diff] [blame^] | 152 | mPackageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT) || |
| 153 | mPackageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA_EXTERNAL)); |
Zhijun He | f639d7c | 2013-10-01 12:28:39 -0700 | [diff] [blame] | 154 | } else if (lensFacing == CameraCharacteristics.LENS_FACING_BACK) { |
| 155 | assertTrue("System doesn't have back camera feature", |
| 156 | mPackageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA)); |
| 157 | } else { |
| 158 | fail("Unknown camera lens facing " + lensFacing.toString()); |
| 159 | } |
| 160 | } |
Timothy Knight | d8b3e52 | 2013-07-25 15:54:46 -0700 | [diff] [blame] | 161 | |
| 162 | /** |
| 163 | * Test: that if there is one camera device, then the system must have some |
| 164 | * specific features. |
| 165 | */ |
| 166 | assertTrue("Missing system feature: FEATURE_CAMERA_ANY", |
| 167 | ids.length == 0 |
| 168 | || mPackageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY)); |
| 169 | assertTrue("Missing system feature: FEATURE_CAMERA or FEATURE_CAMERA_FRONT", |
| 170 | ids.length == 0 |
| 171 | || mPackageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA) |
| 172 | || mPackageManager.hasSystemFeature(PackageManager.FEATURE_CAMERA_FRONT)); |
| 173 | } |
| 174 | |
| 175 | // Test: that properties can be queried from each device, without exceptions. |
Igor Murashkin | afce1a1 | 2013-09-10 12:29:54 -0700 | [diff] [blame] | 176 | public void testCameraManagerGetCameraCharacteristics() throws Exception { |
Eino-Ville Talvala | 92f595c | 2013-09-09 13:48:27 -0700 | [diff] [blame] | 177 | String[] ids = mCameraManager.getCameraIdList(); |
Timothy Knight | d8b3e52 | 2013-07-25 15:54:46 -0700 | [diff] [blame] | 178 | for (int i = 0; i < ids.length; i++) { |
Zhijun He | 9dbe417 | 2013-09-27 13:34:12 -0700 | [diff] [blame] | 179 | CameraCharacteristics props = mCameraManager.getCameraCharacteristics(ids[i]); |
Timothy Knight | d8b3e52 | 2013-07-25 15:54:46 -0700 | [diff] [blame] | 180 | assertNotNull( |
Zhijun He | 9dbe417 | 2013-09-27 13:34:12 -0700 | [diff] [blame] | 181 | String.format("Can't get camera characteristics from: ID %s", ids[i]), props); |
Timothy Knight | d8b3e52 | 2013-07-25 15:54:46 -0700 | [diff] [blame] | 182 | } |
| 183 | } |
| 184 | |
| 185 | // Test: that an exception is thrown if an invalid device id is passed down. |
| 186 | public void testCameraManagerInvalidDevice() throws Exception { |
Eino-Ville Talvala | 92f595c | 2013-09-09 13:48:27 -0700 | [diff] [blame] | 187 | String[] ids = mCameraManager.getCameraIdList(); |
Timothy Knight | d8b3e52 | 2013-07-25 15:54:46 -0700 | [diff] [blame] | 188 | // Create an invalid id by concatenating all the valid ids together. |
| 189 | StringBuilder invalidId = new StringBuilder(); |
| 190 | invalidId.append("INVALID"); |
| 191 | for (int i = 0; i < ids.length; i++) { |
| 192 | invalidId.append(ids[i]); |
| 193 | } |
Zhijun He | c66004f | 2014-03-04 17:58:56 -0800 | [diff] [blame] | 194 | |
Timothy Knight | d8b3e52 | 2013-07-25 15:54:46 -0700 | [diff] [blame] | 195 | try { |
Zhijun He | c66004f | 2014-03-04 17:58:56 -0800 | [diff] [blame] | 196 | mCameraManager.getCameraCharacteristics( |
Timothy Knight | d8b3e52 | 2013-07-25 15:54:46 -0700 | [diff] [blame] | 197 | invalidId.toString()); |
| 198 | fail(String.format("Accepted invalid camera ID: %s", invalidId.toString())); |
Zhijun He | c66004f | 2014-03-04 17:58:56 -0800 | [diff] [blame] | 199 | } catch (IllegalArgumentException e) { |
Timothy Knight | d8b3e52 | 2013-07-25 15:54:46 -0700 | [diff] [blame] | 200 | // This is the exception that should be thrown in this case. |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | // Test: that each camera device can be opened one at a time, several times. |
| 205 | public void testCameraManagerOpenCamerasSerially() throws Exception { |
Eino-Ville Talvala | 92f595c | 2013-09-09 13:48:27 -0700 | [diff] [blame] | 206 | String[] ids = mCameraManager.getCameraIdList(); |
Timothy Knight | d8b3e52 | 2013-07-25 15:54:46 -0700 | [diff] [blame] | 207 | for (int i = 0; i < ids.length; i++) { |
| 208 | for (int j = 0; j < NUM_CAMERA_REOPENS; j++) { |
Zhijun He | c66004f | 2014-03-04 17:58:56 -0800 | [diff] [blame] | 209 | CameraDevice camera = null; |
| 210 | try { |
Eino-Ville Talvala | c0dd022 | 2014-09-04 15:07:58 -0700 | [diff] [blame] | 211 | MockStateCallback mockListener = MockStateCallback.mock(); |
| 212 | mCameraListener = new BlockingStateCallback(mockListener); |
Zhijun He | c66004f | 2014-03-04 17:58:56 -0800 | [diff] [blame] | 213 | |
| 214 | mCameraManager.openCamera(ids[i], mCameraListener, mHandler); |
| 215 | |
| 216 | // Block until unConfigured |
Eino-Ville Talvala | c0dd022 | 2014-09-04 15:07:58 -0700 | [diff] [blame] | 217 | mCameraListener.waitForState(BlockingStateCallback.STATE_OPENED, |
Zhijun He | c66004f | 2014-03-04 17:58:56 -0800 | [diff] [blame] | 218 | CameraTestUtils.CAMERA_IDLE_TIMEOUT_MS); |
| 219 | |
| 220 | // Ensure state transitions are in right order: |
| 221 | // -- 1) Opened |
Zhijun He | c66004f | 2014-03-04 17:58:56 -0800 | [diff] [blame] | 222 | // Ensure no other state transitions have occurred: |
Eino-Ville Talvala | b80c661 | 2014-07-06 17:58:46 -0700 | [diff] [blame] | 223 | camera = verifyCameraStateOpened(ids[i], mockListener); |
Zhijun He | c66004f | 2014-03-04 17:58:56 -0800 | [diff] [blame] | 224 | } finally { |
| 225 | if (camera != null) { |
| 226 | camera.close(); |
| 227 | } |
| 228 | } |
Timothy Knight | d8b3e52 | 2013-07-25 15:54:46 -0700 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | /** |
Zhijun He | c66004f | 2014-03-04 17:58:56 -0800 | [diff] [blame] | 234 | * Test: one or more camera devices can be open at the same time, or the right error state |
| 235 | * is set if this can't be done. |
Timothy Knight | d8b3e52 | 2013-07-25 15:54:46 -0700 | [diff] [blame] | 236 | */ |
| 237 | public void testCameraManagerOpenAllCameras() throws Exception { |
Eino-Ville Talvala | 92f595c | 2013-09-09 13:48:27 -0700 | [diff] [blame] | 238 | String[] ids = mCameraManager.getCameraIdList(); |
Zhijun He | c66004f | 2014-03-04 17:58:56 -0800 | [diff] [blame] | 239 | assertNotNull("Camera ids shouldn't be null", ids); |
| 240 | |
| 241 | // Skip test if the device doesn't have multiple cameras. |
| 242 | if (ids.length <= 1) { |
| 243 | return; |
| 244 | } |
| 245 | |
| 246 | List<CameraDevice> cameraList = new ArrayList<CameraDevice>(); |
Eino-Ville Talvala | c0dd022 | 2014-09-04 15:07:58 -0700 | [diff] [blame] | 247 | List<MockStateCallback> listenerList = new ArrayList<MockStateCallback>(); |
| 248 | List<BlockingStateCallback> blockingListenerList = new ArrayList<BlockingStateCallback>(); |
Timothy Knight | d8b3e52 | 2013-07-25 15:54:46 -0700 | [diff] [blame] | 249 | try { |
| 250 | for (int i = 0; i < ids.length; i++) { |
Zhijun He | c66004f | 2014-03-04 17:58:56 -0800 | [diff] [blame] | 251 | // Ignore state changes from other cameras |
Eino-Ville Talvala | c0dd022 | 2014-09-04 15:07:58 -0700 | [diff] [blame] | 252 | MockStateCallback mockListener = MockStateCallback.mock(); |
| 253 | mCameraListener = new BlockingStateCallback(mockListener); |
Timothy Knight | d8b3e52 | 2013-07-25 15:54:46 -0700 | [diff] [blame] | 254 | |
Zhijun He | c66004f | 2014-03-04 17:58:56 -0800 | [diff] [blame] | 255 | /** |
| 256 | * Track whether or not we got a synchronous error from openCamera. |
| 257 | * |
| 258 | * A synchronous error must also be accompanied by an asynchronous |
Eino-Ville Talvala | c0dd022 | 2014-09-04 15:07:58 -0700 | [diff] [blame] | 259 | * StateCallback#onError callback. |
Zhijun He | c66004f | 2014-03-04 17:58:56 -0800 | [diff] [blame] | 260 | */ |
| 261 | boolean expectingError = false; |
| 262 | |
| 263 | String cameraId = ids[i]; |
| 264 | try { |
| 265 | mCameraManager.openCamera(cameraId, mCameraListener, |
| 266 | mHandler); |
| 267 | } catch (CameraAccessException e) { |
| 268 | if (checkCameraAccessExceptionReason(e) == CameraAccessException.CAMERA_ERROR) { |
| 269 | expectingError = true; |
| 270 | } else { |
| 271 | // TODO: We should handle a Disabled camera by passing here and elsewhere |
| 272 | fail("Camera must not be disconnected or disabled for this test" + ids[i]); |
| 273 | } |
Timothy Knight | d8b3e52 | 2013-07-25 15:54:46 -0700 | [diff] [blame] | 274 | } |
Zhijun He | c66004f | 2014-03-04 17:58:56 -0800 | [diff] [blame] | 275 | |
| 276 | List<Integer> expectedStates = new ArrayList<Integer>(); |
Eino-Ville Talvala | c0dd022 | 2014-09-04 15:07:58 -0700 | [diff] [blame] | 277 | expectedStates.add(BlockingStateCallback.STATE_OPENED); |
| 278 | expectedStates.add(BlockingStateCallback.STATE_ERROR); |
Zhijun He | c66004f | 2014-03-04 17:58:56 -0800 | [diff] [blame] | 279 | int state = mCameraListener.waitForAnyOfStates( |
| 280 | expectedStates, CameraTestUtils.CAMERA_IDLE_TIMEOUT_MS); |
| 281 | |
| 282 | // It's possible that we got an asynchronous error transition only. This is ok. |
| 283 | if (expectingError) { |
| 284 | assertEquals("Throwing a CAMERA_ERROR exception must be accompanied with a " + |
Eino-Ville Talvala | c0dd022 | 2014-09-04 15:07:58 -0700 | [diff] [blame] | 285 | "StateCallback#onError callback", |
| 286 | BlockingStateCallback.STATE_ERROR, state); |
Timothy Knight | d8b3e52 | 2013-07-25 15:54:46 -0700 | [diff] [blame] | 287 | } |
Zhijun He | c66004f | 2014-03-04 17:58:56 -0800 | [diff] [blame] | 288 | |
| 289 | /** |
| 290 | * Two situations are considered passing: |
| 291 | * 1) The camera opened successfully. |
| 292 | * => No error must be set. |
| 293 | * 2) The camera did not open because there were too many other cameras opened. |
| 294 | * => Only MAX_CAMERAS_IN_USE error must be set. |
| 295 | * |
| 296 | * Any other situation is considered a failure. |
| 297 | * |
| 298 | * For simplicity we treat disconnecting asynchronously as a failure, so |
| 299 | * camera devices should not be physically unplugged during this test. |
| 300 | */ |
| 301 | |
| 302 | CameraDevice camera; |
Eino-Ville Talvala | c0dd022 | 2014-09-04 15:07:58 -0700 | [diff] [blame] | 303 | if (state == BlockingStateCallback.STATE_ERROR) { |
Zhijun He | c66004f | 2014-03-04 17:58:56 -0800 | [diff] [blame] | 304 | // Camera did not open because too many other cameras were opened |
| 305 | // => onError called exactly once with a non-null camera |
| 306 | assertTrue("At least one camera must be opened successfully", |
| 307 | cameraList.size() > 0); |
| 308 | |
| 309 | ArgumentCaptor<CameraDevice> argument = |
| 310 | ArgumentCaptor.forClass(CameraDevice.class); |
| 311 | |
| 312 | verify(mockListener) |
| 313 | .onError( |
| 314 | argument.capture(), |
Eino-Ville Talvala | c0dd022 | 2014-09-04 15:07:58 -0700 | [diff] [blame] | 315 | eq(CameraDevice.StateCallback.ERROR_MAX_CAMERAS_IN_USE)); |
Zhijun He | c66004f | 2014-03-04 17:58:56 -0800 | [diff] [blame] | 316 | verifyNoMoreInteractions(mockListener); |
| 317 | |
| 318 | camera = argument.getValue(); |
| 319 | assertNotNull("Expected a non-null camera for the error transition for ID: " |
| 320 | + ids[i], camera); |
Eino-Ville Talvala | c0dd022 | 2014-09-04 15:07:58 -0700 | [diff] [blame] | 321 | } else if (state == BlockingStateCallback.STATE_OPENED) { |
Zhijun He | c66004f | 2014-03-04 17:58:56 -0800 | [diff] [blame] | 322 | // Camera opened successfully. |
Eino-Ville Talvala | b80c661 | 2014-07-06 17:58:46 -0700 | [diff] [blame] | 323 | // => onOpened called exactly once |
| 324 | camera = verifyCameraStateOpened(cameraId, |
Zhijun He | c66004f | 2014-03-04 17:58:56 -0800 | [diff] [blame] | 325 | mockListener); |
| 326 | } else { |
| 327 | fail("Unexpected state " + state); |
| 328 | camera = null; // unreachable. but need this for java compiler |
| 329 | } |
| 330 | |
| 331 | // Keep track of cameras so we can close it later |
| 332 | cameraList.add(camera); |
| 333 | listenerList.add(mockListener); |
Eino-Ville Talvala | f889261 | 2014-06-03 18:38:56 -0700 | [diff] [blame] | 334 | blockingListenerList.add(mCameraListener); |
Zhijun He | c66004f | 2014-03-04 17:58:56 -0800 | [diff] [blame] | 335 | } |
| 336 | } finally { |
| 337 | for (CameraDevice camera : cameraList) { |
| 338 | camera.close(); |
Timothy Knight | d8b3e52 | 2013-07-25 15:54:46 -0700 | [diff] [blame] | 339 | } |
Eino-Ville Talvala | c0dd022 | 2014-09-04 15:07:58 -0700 | [diff] [blame] | 340 | for (BlockingStateCallback blockingListener : blockingListenerList) { |
Eino-Ville Talvala | f889261 | 2014-06-03 18:38:56 -0700 | [diff] [blame] | 341 | blockingListener.waitForState( |
Eino-Ville Talvala | c0dd022 | 2014-09-04 15:07:58 -0700 | [diff] [blame] | 342 | BlockingStateCallback.STATE_CLOSED, |
Eino-Ville Talvala | f889261 | 2014-06-03 18:38:56 -0700 | [diff] [blame] | 343 | CameraTestUtils.CAMERA_IDLE_TIMEOUT_MS); |
| 344 | } |
Timothy Knight | d8b3e52 | 2013-07-25 15:54:46 -0700 | [diff] [blame] | 345 | } |
Zhijun He | c66004f | 2014-03-04 17:58:56 -0800 | [diff] [blame] | 346 | |
| 347 | /* |
| 348 | * Ensure that no state transitions have bled through from one camera to another |
| 349 | * after closing the cameras. |
| 350 | */ |
| 351 | int i = 0; |
Eino-Ville Talvala | c0dd022 | 2014-09-04 15:07:58 -0700 | [diff] [blame] | 352 | for (MockStateCallback listener : listenerList) { |
Zhijun He | c66004f | 2014-03-04 17:58:56 -0800 | [diff] [blame] | 353 | CameraDevice camera = cameraList.get(i); |
| 354 | |
| 355 | verify(listener).onClosed(eq(camera)); |
| 356 | verifyNoMoreInteractions(listener); |
Eino-Ville Talvala | f889261 | 2014-06-03 18:38:56 -0700 | [diff] [blame] | 357 | i++; |
Zhijun He | c66004f | 2014-03-04 17:58:56 -0800 | [diff] [blame] | 358 | // Only a #close can happen on the camera since we were done with it. |
| 359 | // Also nothing else should've happened between the close and the open. |
Timothy Knight | d8b3e52 | 2013-07-25 15:54:46 -0700 | [diff] [blame] | 360 | } |
| 361 | } |
| 362 | |
Zhijun He | c66004f | 2014-03-04 17:58:56 -0800 | [diff] [blame] | 363 | /** |
| 364 | * Verifies the camera in this listener was opened and then unconfigured exactly once. |
| 365 | * |
| 366 | * <p>This assumes that no other action to the camera has been done (e.g. |
| 367 | * it hasn't been configured, or closed, or disconnected). Verification is |
| 368 | * performed immediately without any timeouts.</p> |
| 369 | * |
| 370 | * <p>This checks that the state has previously changed first for opened and then unconfigured. |
| 371 | * Any other state transitions will fail. A test failure is thrown if verification fails.</p> |
| 372 | * |
| 373 | * @param cameraId Camera identifier |
| 374 | * @param listener Listener which was passed to {@link CameraManager#openCamera} |
| 375 | * |
| 376 | * @return The camera device (non-{@code null}). |
| 377 | */ |
Eino-Ville Talvala | b80c661 | 2014-07-06 17:58:46 -0700 | [diff] [blame] | 378 | private static CameraDevice verifyCameraStateOpened(String cameraId, |
Eino-Ville Talvala | c0dd022 | 2014-09-04 15:07:58 -0700 | [diff] [blame] | 379 | MockStateCallback listener) { |
Zhijun He | c66004f | 2014-03-04 17:58:56 -0800 | [diff] [blame] | 380 | ArgumentCaptor<CameraDevice> argument = |
| 381 | ArgumentCaptor.forClass(CameraDevice.class); |
| 382 | InOrder inOrder = inOrder(listener); |
| 383 | |
| 384 | /** |
| 385 | * State transitions (in that order): |
| 386 | * 1) onOpened |
Zhijun He | c66004f | 2014-03-04 17:58:56 -0800 | [diff] [blame] | 387 | * |
| 388 | * No other transitions must occur for successful #openCamera |
| 389 | */ |
| 390 | inOrder.verify(listener) |
| 391 | .onOpened(argument.capture()); |
| 392 | |
| 393 | CameraDevice camera = argument.getValue(); |
| 394 | assertNotNull( |
Eino-Ville Talvala | b80c661 | 2014-07-06 17:58:46 -0700 | [diff] [blame] | 395 | String.format("Failed to open camera device ID: %s", cameraId), |
Zhijun He | c66004f | 2014-03-04 17:58:56 -0800 | [diff] [blame] | 396 | camera); |
| 397 | |
Zhijun He | c66004f | 2014-03-04 17:58:56 -0800 | [diff] [blame] | 398 | // Do not use inOrder here since that would skip anything called before onOpened |
| 399 | verifyNoMoreInteractions(listener); |
| 400 | |
| 401 | return camera; |
| 402 | } |
| 403 | |
| 404 | /** |
| 405 | * Test: that opening the same device multiple times and make sure the right |
| 406 | * error state is set. |
| 407 | */ |
Timothy Knight | d8b3e52 | 2013-07-25 15:54:46 -0700 | [diff] [blame] | 408 | public void testCameraManagerOpenCameraTwice() throws Exception { |
Eino-Ville Talvala | 92f595c | 2013-09-09 13:48:27 -0700 | [diff] [blame] | 409 | String[] ids = mCameraManager.getCameraIdList(); |
Zhijun He | c66004f | 2014-03-04 17:58:56 -0800 | [diff] [blame] | 410 | |
Zhijun He | c66004f | 2014-03-04 17:58:56 -0800 | [diff] [blame] | 411 | // Test across every camera device. |
| 412 | for (int i = 0; i < ids.length; ++i) { |
| 413 | CameraDevice successCamera = null; |
Zhijun He | 884dc00 | 2014-03-20 00:03:10 -0700 | [diff] [blame] | 414 | mCollector.setCameraId(ids[i]); |
Zhijun He | c66004f | 2014-03-04 17:58:56 -0800 | [diff] [blame] | 415 | |
Timothy Knight | d8b3e52 | 2013-07-25 15:54:46 -0700 | [diff] [blame] | 416 | try { |
Eino-Ville Talvala | c0dd022 | 2014-09-04 15:07:58 -0700 | [diff] [blame] | 417 | MockStateCallback mockSuccessListener = MockStateCallback.mock(); |
| 418 | MockStateCallback mockFailListener = MockStateCallback.mock(); |
Zhijun He | c66004f | 2014-03-04 17:58:56 -0800 | [diff] [blame] | 419 | |
Eino-Ville Talvala | c0dd022 | 2014-09-04 15:07:58 -0700 | [diff] [blame] | 420 | BlockingStateCallback successListener = |
| 421 | new BlockingStateCallback(mockSuccessListener); |
| 422 | BlockingStateCallback failListener = |
| 423 | new BlockingStateCallback(mockFailListener); |
Zhijun He | c66004f | 2014-03-04 17:58:56 -0800 | [diff] [blame] | 424 | |
| 425 | mCameraManager.openCamera(ids[i], successListener, mHandler); |
| 426 | |
Timothy Knight | d8b3e52 | 2013-07-25 15:54:46 -0700 | [diff] [blame] | 427 | try { |
Eino-Ville Talvala | f889261 | 2014-06-03 18:38:56 -0700 | [diff] [blame] | 428 | mCameraManager.openCamera(ids[i], failListener, |
Zhijun He | c66004f | 2014-03-04 17:58:56 -0800 | [diff] [blame] | 429 | mHandler); |
| 430 | } catch (CameraAccessException e) { |
| 431 | // Optional (but common). Camera might fail asynchronously only. |
Zhijun He | 884dc00 | 2014-03-20 00:03:10 -0700 | [diff] [blame] | 432 | // Don't assert here, otherwise, all subsequent tests will fail because the |
| 433 | // opened camera is never closed. |
| 434 | mCollector.expectEquals( |
| 435 | "If second camera open fails immediately, must be due to" |
| 436 | + "camera being busy for ID: " + ids[i], |
| 437 | CameraAccessException.CAMERA_ERROR, e.getReason()); |
Timothy Knight | d8b3e52 | 2013-07-25 15:54:46 -0700 | [diff] [blame] | 438 | } |
Zhijun He | c66004f | 2014-03-04 17:58:56 -0800 | [diff] [blame] | 439 | |
Eino-Ville Talvala | c0dd022 | 2014-09-04 15:07:58 -0700 | [diff] [blame] | 440 | successListener.waitForState(BlockingStateCallback.STATE_OPENED, |
Zhijun He | 884dc00 | 2014-03-20 00:03:10 -0700 | [diff] [blame] | 441 | CameraTestUtils.CAMERA_IDLE_TIMEOUT_MS); |
| 442 | // Have to get the successCamera here, otherwise, it won't be |
| 443 | // closed if STATE_ERROR timeout exception occurs. |
| 444 | ArgumentCaptor<CameraDevice> argument = |
| 445 | ArgumentCaptor.forClass(CameraDevice.class); |
| 446 | verify(mockSuccessListener, atLeastOnce()).onOpened(argument.capture()); |
Zhijun He | 884dc00 | 2014-03-20 00:03:10 -0700 | [diff] [blame] | 447 | |
Eino-Ville Talvala | c0dd022 | 2014-09-04 15:07:58 -0700 | [diff] [blame] | 448 | failListener.waitForState(BlockingStateCallback.STATE_ERROR, |
Zhijun He | c66004f | 2014-03-04 17:58:56 -0800 | [diff] [blame] | 449 | CameraTestUtils.CAMERA_IDLE_TIMEOUT_MS); |
| 450 | |
Eino-Ville Talvala | b80c661 | 2014-07-06 17:58:46 -0700 | [diff] [blame] | 451 | successCamera = verifyCameraStateOpened( |
Zhijun He | 884dc00 | 2014-03-20 00:03:10 -0700 | [diff] [blame] | 452 | ids[i], mockSuccessListener); |
Zhijun He | c66004f | 2014-03-04 17:58:56 -0800 | [diff] [blame] | 453 | |
| 454 | verify(mockFailListener) |
| 455 | .onError( |
Eino-Ville Talvala | f889261 | 2014-06-03 18:38:56 -0700 | [diff] [blame] | 456 | and(notNull(CameraDevice.class), not(eq(successCamera))), |
Eino-Ville Talvala | c0dd022 | 2014-09-04 15:07:58 -0700 | [diff] [blame] | 457 | eq(StateCallback.ERROR_CAMERA_IN_USE)); |
Zhijun He | c66004f | 2014-03-04 17:58:56 -0800 | [diff] [blame] | 458 | verifyNoMoreInteractions(mockFailListener); |
| 459 | } finally { |
| 460 | if (successCamera != null) { |
| 461 | successCamera.close(); |
Timothy Knight | d8b3e52 | 2013-07-25 15:54:46 -0700 | [diff] [blame] | 462 | } |
| 463 | } |
| 464 | } |
| 465 | } |
| 466 | |
Eino-Ville Talvala | c0dd022 | 2014-09-04 15:07:58 -0700 | [diff] [blame] | 467 | private class NoopCameraListener extends CameraManager.AvailabilityCallback { |
Timothy Knight | d8b3e52 | 2013-07-25 15:54:46 -0700 | [diff] [blame] | 468 | @Override |
| 469 | public void onCameraAvailable(String cameraId) { |
| 470 | // No-op |
| 471 | } |
| 472 | |
| 473 | @Override |
| 474 | public void onCameraUnavailable(String cameraId) { |
| 475 | // No-op |
| 476 | } |
| 477 | } |
| 478 | |
| 479 | /** |
| 480 | * Test: that the APIs to register and unregister a listener run successfully; |
| 481 | * doesn't test that the listener actually gets invoked at the right time. |
| 482 | * Registering a listener multiple times should have no effect, and unregistering |
| 483 | * a listener that isn't registered should have no effect. |
| 484 | */ |
| 485 | public void testCameraManagerListener() throws Exception { |
Eino-Ville Talvala | c0dd022 | 2014-09-04 15:07:58 -0700 | [diff] [blame] | 486 | mCameraManager.unregisterAvailabilityCallback(mListener); |
| 487 | mCameraManager.registerAvailabilityCallback(mListener, mHandler); |
| 488 | mCameraManager.registerAvailabilityCallback(mListener, mHandler); |
| 489 | mCameraManager.unregisterAvailabilityCallback(mListener); |
| 490 | mCameraManager.unregisterAvailabilityCallback(mListener); |
Timothy Knight | d8b3e52 | 2013-07-25 15:54:46 -0700 | [diff] [blame] | 491 | } |
Eino-Ville Talvala | 851d48e | 2014-10-29 16:16:40 -0700 | [diff] [blame] | 492 | |
| 493 | /** |
| 494 | * Test that the availability callbacks fire when expected |
| 495 | */ |
| 496 | public void testCameraManagerListenerCallbacks() throws Exception { |
| 497 | final int AVAILABILITY_TIMEOUT_MS = 10; |
| 498 | |
| 499 | final LinkedBlockingQueue<String> availableEventQueue = new LinkedBlockingQueue<>(); |
| 500 | final LinkedBlockingQueue<String> unavailableEventQueue = new LinkedBlockingQueue<>(); |
| 501 | |
| 502 | CameraManager.AvailabilityCallback ac = new CameraManager.AvailabilityCallback() { |
| 503 | @Override |
| 504 | public void onCameraAvailable(String cameraId) { |
| 505 | availableEventQueue.offer(cameraId); |
| 506 | } |
| 507 | |
| 508 | @Override |
| 509 | public void onCameraUnavailable(String cameraId) { |
| 510 | unavailableEventQueue.offer(cameraId); |
| 511 | } |
| 512 | }; |
| 513 | |
| 514 | mCameraManager.registerAvailabilityCallback(ac, mHandler); |
| 515 | String[] cameras = mCameraManager.getCameraIdList(); |
| 516 | |
Eino-Ville Talvala | b7e5003 | 2014-12-10 15:35:00 -0800 | [diff] [blame] | 517 | if (cameras.length == 0) { |
| 518 | Log.i(TAG, "No cameras present, skipping test"); |
| 519 | return; |
| 520 | } |
| 521 | |
Eino-Ville Talvala | 851d48e | 2014-10-29 16:16:40 -0700 | [diff] [blame] | 522 | // Verify we received available for all cameras' initial state in a reasonable amount of time |
| 523 | HashSet<String> expectedAvailableCameras = new HashSet<String>(Arrays.asList(cameras)); |
| 524 | while (expectedAvailableCameras.size() > 0) { |
| 525 | String id = availableEventQueue.poll(AVAILABILITY_TIMEOUT_MS, |
| 526 | java.util.concurrent.TimeUnit.MILLISECONDS); |
| 527 | assertTrue("Did not receive initial availability notices for some cameras", |
| 528 | id != null); |
| 529 | expectedAvailableCameras.remove(id); |
| 530 | } |
| 531 | // Verify no unavailable cameras were reported |
| 532 | assertTrue("Some camera devices are initially unavailable", |
| 533 | unavailableEventQueue.size() == 0); |
| 534 | |
| 535 | // Verify transitions for individual cameras |
| 536 | for (String id : cameras) { |
| 537 | MockStateCallback mockListener = MockStateCallback.mock(); |
| 538 | mCameraListener = new BlockingStateCallback(mockListener); |
| 539 | |
| 540 | mCameraManager.openCamera(id, mCameraListener, mHandler); |
| 541 | |
| 542 | // Block until opened |
| 543 | mCameraListener.waitForState(BlockingStateCallback.STATE_OPENED, |
| 544 | CameraTestUtils.CAMERA_IDLE_TIMEOUT_MS); |
| 545 | // Then verify only open happened, and get the camera handle |
| 546 | CameraDevice camera = verifyCameraStateOpened(id, mockListener); |
| 547 | |
| 548 | // Verify that we see the expected 'unavailable' event. |
| 549 | String candidateId = unavailableEventQueue.poll(AVAILABILITY_TIMEOUT_MS, |
| 550 | java.util.concurrent.TimeUnit.MILLISECONDS); |
| 551 | assertTrue(String.format("Received unavailability notice for wrong ID " + |
| 552 | "(expected %s, got %s)", id, candidateId), |
| 553 | id == candidateId); |
| 554 | assertTrue("Availability events received unexpectedly", |
| 555 | availableEventQueue.size() == 0); |
| 556 | |
| 557 | // Verify that we see the expected 'available' event after closing the camera |
| 558 | |
| 559 | camera.close(); |
| 560 | |
| 561 | mCameraListener.waitForState(BlockingStateCallback.STATE_CLOSED, |
| 562 | CameraTestUtils.CAMERA_CLOSE_TIMEOUT_MS); |
| 563 | |
| 564 | candidateId = availableEventQueue.poll(AVAILABILITY_TIMEOUT_MS, |
| 565 | java.util.concurrent.TimeUnit.MILLISECONDS); |
| 566 | assertTrue(String.format("Received availability notice for wrong ID " + |
| 567 | "(expected %s, got %s)", id, candidateId), |
| 568 | id == candidateId); |
| 569 | assertTrue("Unavailability events received unexpectedly", |
| 570 | unavailableEventQueue.size() == 0); |
| 571 | |
| 572 | } |
| 573 | |
| 574 | // Verify that we can unregister the listener and see no more events |
| 575 | assertTrue("Availability events received unexpectedly", |
| 576 | availableEventQueue.size() == 0); |
| 577 | assertTrue("Unavailability events received unexpectedly", |
| 578 | unavailableEventQueue.size() == 0); |
| 579 | |
| 580 | mCameraManager.unregisterAvailabilityCallback(ac); |
| 581 | |
| 582 | { |
| 583 | // Open an arbitrary camera and make sure we don't hear about it |
| 584 | |
| 585 | MockStateCallback mockListener = MockStateCallback.mock(); |
| 586 | mCameraListener = new BlockingStateCallback(mockListener); |
| 587 | |
| 588 | mCameraManager.openCamera(cameras[0], mCameraListener, mHandler); |
| 589 | |
| 590 | // Block until opened |
| 591 | mCameraListener.waitForState(BlockingStateCallback.STATE_OPENED, |
| 592 | CameraTestUtils.CAMERA_IDLE_TIMEOUT_MS); |
| 593 | // Then verify only open happened, and close the camera |
| 594 | CameraDevice camera = verifyCameraStateOpened(cameras[0], mockListener); |
| 595 | |
| 596 | camera.close(); |
| 597 | |
| 598 | mCameraListener.waitForState(BlockingStateCallback.STATE_CLOSED, |
| 599 | CameraTestUtils.CAMERA_CLOSE_TIMEOUT_MS); |
| 600 | |
| 601 | // No unavailability or availability callback should have occured |
| 602 | String candidateId = unavailableEventQueue.poll(AVAILABILITY_TIMEOUT_MS, |
| 603 | java.util.concurrent.TimeUnit.MILLISECONDS); |
| 604 | assertTrue(String.format("Received unavailability notice for ID %s unexpectedly ", |
| 605 | candidateId), |
| 606 | candidateId == null); |
| 607 | |
| 608 | candidateId = availableEventQueue.poll(AVAILABILITY_TIMEOUT_MS, |
| 609 | java.util.concurrent.TimeUnit.MILLISECONDS); |
| 610 | assertTrue(String.format("Received availability notice for ID %s unexpectedly ", |
| 611 | candidateId), |
| 612 | candidateId == null); |
| 613 | |
| 614 | |
| 615 | } |
| 616 | |
| 617 | } // testCameraManagerListenerCallbacks |
| 618 | |
Timothy Knight | d8b3e52 | 2013-07-25 15:54:46 -0700 | [diff] [blame] | 619 | } |