Camera2: Refactor ImageReader CTS test code

Move some classes and functions to CameraTestUtils class to make the test better
organized.

Bug: 9802344
Change-Id: Ie7deb0ad0ad0f08ae516961e9a043df20115a2da
diff --git a/tests/tests/hardware/src/android/hardware/camera2/cts/CameraTestUtils.java b/tests/tests/hardware/src/android/hardware/camera2/cts/CameraTestUtils.java
index b53b2fa..293811a 100644
--- a/tests/tests/hardware/src/android/hardware/camera2/cts/CameraTestUtils.java
+++ b/tests/tests/hardware/src/android/hardware/camera2/cts/CameraTestUtils.java
@@ -29,6 +29,7 @@
 
 import java.io.FileOutputStream;
 import java.io.IOException;
+import java.lang.reflect.Array;
 import java.nio.ByteBuffer;
 import java.util.Arrays;
 
@@ -39,6 +40,33 @@
     private static final String TAG = "CameraTestUtils";
     private static final boolean VERBOSE = Log.isLoggable(TAG, Log.VERBOSE);
 
+    public static <T> void assertArrayNotEmpty(T arr, String message) {
+        assertTrue(message, arr != null && Array.getLength(arr) > 0);
+    }
+
+    /**
+     * Check if the format is a legal YUV format camera supported.
+     */
+    public static void checkYuvFormat(int format) {
+        if ((format != ImageFormat.YUV_420_888) &&
+                (format != ImageFormat.NV21) &&
+                (format != ImageFormat.YV12) &&
+                (format != ImageFormat.Y8) &&
+                (format != ImageFormat.Y16)) {
+            fail("Wrong formats: " + format);
+        }
+    }
+
+    /**
+     * Check if image size and format match given size and format.
+     */
+    public static void checkImage(Image image, int width, int height, int format) {
+        assertNotNull("Input image is invalid", image);
+        assertEquals("Format doesn't match", format, image.getFormat());
+        assertEquals("Width doesn't match", width, image.getWidth());
+        assertEquals("Height doesn't match", height, image.getHeight());
+    }
+
     /**
      * <p>Read data from all planes of an Image into a contiguous unpadded, unpacked
      * 1-D linear byte array, such that it can be write into disk, or accessed by
@@ -108,7 +136,7 @@
                     offset += length;
                 } else {
                     // Generic case: should work for any pixelStride but slower.
-                    // Use use intermediate buffer to avoid read byte-by-byte from
+                    // Use intermediate buffer to avoid read byte-by-byte from
                     // DirectByteBuffer, which is very bad for performance
                     buffer.get(rowData, 0, rowStride);
                     for (int col = 0; col < w; col++) {
diff --git a/tests/tests/hardware/src/android/hardware/camera2/cts/ImageReaderTest.java b/tests/tests/hardware/src/android/hardware/camera2/cts/ImageReaderTest.java
index f9964e0..984d40e 100644
--- a/tests/tests/hardware/src/android/hardware/camera2/cts/ImageReaderTest.java
+++ b/tests/tests/hardware/src/android/hardware/camera2/cts/ImageReaderTest.java
@@ -16,6 +16,8 @@
 
 package android.hardware.camera2.cts;
 
+import static android.hardware.camera2.cts.CameraTestUtils.*;
+
 import android.content.Context;
 import android.graphics.BitmapFactory;
 import android.graphics.ImageFormat;
@@ -27,14 +29,12 @@
 import android.hardware.camera2.Size;
 import android.media.Image;
 import android.media.ImageReader;
-import android.os.ConditionVariable;
 import android.os.Environment;
 import android.os.Handler;
 import android.test.AndroidTestCase;
 import android.util.Log;
 import android.view.Surface;
 
-import java.lang.reflect.Array;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
@@ -59,13 +59,12 @@
     // TODO: Need extend it to bigger number
     private static final int NUM_FRAME_VERIFIED = 1;
     // Max number of images can be accessed simultaneously from ImageReader.
-    private static final int MAX_NUM_IMAGES = 1;
+    private static final int MAX_NUM_IMAGES = 5;
 
     private CameraManager mCameraManager;
     private CameraDevice mCamera;
     private String[] mCameraIds;
     private ImageReader mReader = null;
-    private CameraTestUtils mTestUtil = null;
     private Handler mHandler = null;
     private SimpleImageListener mListener = null;
     private CameraTestThread mLooperThread = null;
@@ -81,7 +80,6 @@
     protected void setUp() throws Exception {
         super.setUp();
         mCameraIds = mCameraManager.getDeviceIdList();
-        mTestUtil = new CameraTestUtils();
         mLooperThread = new CameraTestThread();
         mHandler = mLooperThread.start();
     }
@@ -97,7 +95,6 @@
             mReader = null;
         }
         mLooperThread.close();
-        mTestUtil = null;
         mHandler = null;
         super.tearDown();
     }
@@ -141,13 +138,14 @@
          * move to util class.
          */
         int[] availableFormats = properties.get(CameraProperties.SCALER_AVAILABLE_FORMATS);
-        assertArrayNotEmpty(availableFormats, "availableFormats should not be empty");
+        assertArrayNotEmpty(availableFormats,
+                "availableFormats should not be empty");
         Arrays.sort(availableFormats);
         assertTrue("Can't find the format " + format + " in supported formats " +
                 Arrays.toString(availableFormats),
                 Arrays.binarySearch(availableFormats, format) >= 0);
 
-        Size[] availableSizes = CameraTestUtils.getSupportedSizeForFormat(format, mCamera);
+        Size[] availableSizes = getSupportedSizeForFormat(format, mCamera);
         assertArrayNotEmpty(availableSizes, "availableSizes should not be empty");
 
         // for each resolution, test imageReader:
@@ -284,10 +282,7 @@
     }
 
     private void validateImage(Image image, int width, int height, int format) {
-        assertNotNull("Input image is invalid", image);
-        assertEquals("Format doesn't match", format, image.getFormat());
-        assertEquals("Width doesn't match", width, image.getWidth());
-        assertEquals("Height doesn't match", height, image.getHeight());
+        checkImage(image, width, height, format);
 
         /**
          * TODO: validate timestamp:
@@ -296,7 +291,7 @@
          * 2. timestamps should be monotonically increasing for different requests
          */
         if(VERBOSE) Log.v(TAG, "validating Image");
-        byte[] data = CameraTestUtils.getDataFromImage(image);
+        byte[] data = getDataFromImage(image);
         assertTrue("Invalid image data", data != null && data.length > 0);
 
         if (format == ImageFormat.JPEG) {
@@ -322,18 +317,12 @@
         if (DUMP_FILE) {
             String fileName =
                     DEBUG_FILE_NAME_BASE + width + "x" + height + ".yuv";
-            CameraTestUtils.dumpFile(fileName, jpegData);
+            dumpFile(fileName, jpegData);
         }
     }
 
     private void validateYuvData(byte[] yuvData, int width, int height, int format, long ts) {
-        if ((format != ImageFormat.YUV_420_888) &&
-                (format != ImageFormat.NV21) &&
-                (format != ImageFormat.YV12) &&
-                (format != ImageFormat.Y8) &&
-                (format != ImageFormat.Y16)) {
-            fail("Wrong formats: " + format);
-        }
+        checkYuvFormat(format);
         if (VERBOSE) Log.v(TAG, "Validating YUV data");
         int expectedSize = width * height * ImageFormat.getBitsPerPixel(format) / 8;
         assertEquals("Yuv data doesn't match", expectedSize, yuvData.length);
@@ -343,11 +332,7 @@
         if (DUMP_FILE) {
             String fileName =
                     DEBUG_FILE_NAME_BASE + "/" + width + "x" + height + "_" + ts / 1e6 + ".yuv";
-            CameraTestUtils.dumpFile(fileName, yuvData);
+            dumpFile(fileName, yuvData);
         }
     }
-
-    private <T> void assertArrayNotEmpty(T arr, String message) {
-        assertTrue(message, arr != null && Array.getLength(arr) > 0);
-    }
 }