testFlashTurnOff: take into account flash states for LEGACY devices.

For LEGACY devices:

Using android.control.aeMode == ON_ALWAYS_FLASH will always return FIRED.
Using android.flash.mode == TORCH will always return FIRED.
In all other conditions the state will not be available on LEGACY devices (i.e. it will be null).

Bug:142726828
Bug:143192067

Test: Test still passes on non LEGACY devices
Test: Vendor testing

Change-Id: I11426a57a970905fc6985f8284d38d9808cab360
Signed-off-by: Jayant Chowdhary <jchowdhary@google.com>
diff --git a/tests/camera/src/android/hardware/camera2/cts/CaptureRequestTest.java b/tests/camera/src/android/hardware/camera2/cts/CaptureRequestTest.java
index b02f13d..6b512e6 100644
--- a/tests/camera/src/android/hardware/camera2/cts/CaptureRequestTest.java
+++ b/tests/camera/src/android/hardware/camera2/cts/CaptureRequestTest.java
@@ -23,6 +23,7 @@
 import android.graphics.PointF;
 import android.graphics.Rect;
 import android.graphics.SurfaceTexture;
+import android.hardware.cts.helpers.CameraUtils;
 import android.hardware.camera2.CameraCharacteristics;
 import android.hardware.camera2.CameraDevice;
 import android.hardware.camera2.CameraMetadata;
@@ -529,18 +530,19 @@
                 Size maxPreviewSz = mOrderedPreviewSizes.get(0); // Max preview size.
 
                 startPreview(requestBuilder, maxPreviewSz, listener);
-                flashTurnOffTest(listener,
+                boolean isLegacy = CameraUtils.isLegacyHAL(mCameraManager, mCameraIdsUnderTest[i]);
+                flashTurnOffTest(listener, isLegacy,
                         /* initiaAeControl */CaptureRequest.CONTROL_AE_MODE_ON_ALWAYS_FLASH,
                         /* offAeControl */CaptureRequest.CONTROL_AE_MODE_ON_ALWAYS_FLASH);
-                flashTurnOffTest(listener,
+
+                flashTurnOffTest(listener, isLegacy,
                         /* initiaAeControl */CaptureRequest.CONTROL_AE_MODE_ON_ALWAYS_FLASH,
                         /* offAeControl */CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH);
 
-                flashTurnOffTest(listener,
+                flashTurnOffTest(listener, isLegacy,
                         /* initiaAeControl */CaptureRequest.CONTROL_AE_MODE_ON_ALWAYS_FLASH,
                         /* offAeControl */CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH_REDEYE);
 
-
                 stopPreview();
             } finally {
                 closeDevice();
@@ -1452,8 +1454,8 @@
      * @param flashOffAeControl The final AE_CONTROL mode which is expected to turn flash off for
      *        TEMPLATE_PREVIEW repeating requests.
      */
-    private void flashTurnOffTest(SimpleCaptureCallback listener, int initialAeControl,
-            int flashOffAeControl) throws Exception {
+    private void flashTurnOffTest(SimpleCaptureCallback listener, boolean isLegacy,
+            int initialAeControl, int flashOffAeControl) throws Exception {
         CaptureResult result;
         final int NUM_FLASH_REQUESTS_TESTED = 10;
         CaptureRequest.Builder requestBuilder = createRequestForPreview();
@@ -1528,10 +1530,40 @@
             mCollector.expectNotEquals("Result for flashModeTorch request null",
                     torchStateResults[j], null);
         }
-        checkTorchStates(torchStateResults, numAllowedTransitionStates, flashModeOffRequests,
-                flashModeTorchRequests);
+        if (isLegacy) {
+            // For LEGACY devices, flash state is null for all situations except:
+            // android.control.aeMode == ON_ALWAYS_FLASH, where flash.state will be FIRED
+            // android.flash.mode == TORCH, where flash.state will be FIRED
+            testLegacyTorchStates(torchStateResults, 0, flashModeOffRequests - 1, flashOffRequest);
+            testLegacyTorchStates(torchStateResults, flashModeOffRequests,
+                    torchStateResults.length -1,
+                    flashModeTorchRequest);
+        } else {
+            checkTorchStates(torchStateResults, numAllowedTransitionStates, flashModeOffRequests,
+                    flashModeTorchRequests);
+        }
     }
 
+    private void testLegacyTorchStates(CaptureResult []torchStateResults, int beg, int end,
+            CaptureRequest request) {
+        for (int i = beg; i <= end; i++) {
+            Integer requestControlAeMode = request.get(CaptureRequest.CONTROL_AE_MODE);
+            Integer requestFlashMode = request.get(CaptureRequest.FLASH_MODE);
+            Integer resultFlashState = torchStateResults[i].get(CaptureResult.FLASH_STATE);
+            if (requestControlAeMode == CaptureRequest.CONTROL_AE_MODE_ON_ALWAYS_FLASH ||
+                    requestFlashMode == CaptureRequest.FLASH_MODE_TORCH) {
+                mCollector.expectEquals("For LEGACY devices, flash state must be FIRED when" +
+                        "CONTROL_AE_MODE == CONTROL_AE_MODE_ON_ALWAYS_FLASH or FLASH_MODE == " +
+                        "TORCH, CONTROL_AE_MODE = " + requestControlAeMode + " FLASH_MODE = " +
+                        requestFlashMode, CaptureResult.FLASH_STATE_FIRED, resultFlashState);
+                continue;
+            }
+            mCollector.expectTrue("For LEGACY devices, flash state must be null when" +
+                        "CONTROL_AE_MODE != CONTROL_AE_MODE_ON_ALWAYS_FLASH or FLASH_MODE != " +
+                        "TORCH, CONTROL_AE_MODE = " + requestControlAeMode + " FLASH_MODE = " +
+                        requestFlashMode,  resultFlashState == null);
+        }
+    }
     // We check that torch states appear in the order expected. We don't necessarily know how many
     // times each state might appear, however we make sure that the states do not appear out of
     // order.
diff --git a/tests/camera/utils/src/android/hardware/cts/helpers/CameraUtils.java b/tests/camera/utils/src/android/hardware/cts/helpers/CameraUtils.java
index 0c143a5..82df26e 100644
--- a/tests/camera/utils/src/android/hardware/cts/helpers/CameraUtils.java
+++ b/tests/camera/utils/src/android/hardware/cts/helpers/CameraUtils.java
@@ -39,6 +39,18 @@
     public static boolean isLegacyHAL(Context context, int cameraId) throws Exception {
         CameraManager manager = (CameraManager) context.getSystemService(Context.CAMERA_SERVICE);
         String cameraIdStr = manager.getCameraIdListNoLazy()[cameraId];
+        return isLegacyHAL(manager, cameraIdStr);
+    }
+
+    /**
+     * Returns {@code true} if this device only supports {@code LEGACY} mode operation in the
+     * Camera2 API for the given camera ID.
+     *
+     * @param manager The {@link CameraManager} used to retrieve camera characteristics.
+     * @param cameraId the ID of the camera device to check.
+     * @return {@code true} if this device only supports {@code LEGACY} mode.
+     */
+    public static boolean isLegacyHAL(CameraManager manager, String cameraIdStr) throws Exception {
         CameraCharacteristics characteristics =
                 manager.getCameraCharacteristics(cameraIdStr);