Make raw tests optional, add sanity checks for format/resolutions static info.

Bug: 8654456
Change-Id: Iadd0d3c326d595dce2822a9bd3ff33ce8e8f6a36
diff --git a/tests/camera2/CameraMetadataTests.cpp b/tests/camera2/CameraMetadataTests.cpp
index 722e71e..8cae619 100644
--- a/tests/camera2/CameraMetadataTests.cpp
+++ b/tests/camera2/CameraMetadataTests.cpp
@@ -70,6 +70,22 @@
         return entry.type;
     }
 
+    int GetEntryCountFromStaticTag(uint32_t tag) const {
+        const CameraMetadata& staticInfo = mDevice->info();
+        camera_metadata_ro_entry entry = staticInfo.find(tag);
+        return entry.count;
+    }
+
+    bool HasElementInArrayFromStaticTag(uint32_t tag, int32_t element) const {
+        const CameraMetadata& staticInfo = mDevice->info();
+        camera_metadata_ro_entry entry = staticInfo.find(tag);
+        for (size_t i = 0; i < entry.count; ++i) {
+            if (entry.data.i32[i] == element)
+                return true;
+        }
+        return false;
+    }
+
 protected:
 
 };
@@ -115,6 +131,50 @@
     }
 }
 
+TEST_F(CameraMetadataTest, RequiredFormats) {
+    TEST_EXTENSION_FORKING_INIT;
+
+    EXPECT_TRUE(
+        HasElementInArrayFromStaticTag(ANDROID_SCALER_AVAILABLE_FORMATS,
+                                      HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED));
+
+    EXPECT_TRUE(
+        HasElementInArrayFromStaticTag(ANDROID_SCALER_AVAILABLE_FORMATS,
+                                       HAL_PIXEL_FORMAT_BLOB)); // JPEG
+
+    EXPECT_TRUE(
+        HasElementInArrayFromStaticTag(ANDROID_SCALER_AVAILABLE_FORMATS,
+                                       HAL_PIXEL_FORMAT_YCrCb_420_SP)); // NV21
+
+    EXPECT_TRUE(
+        HasElementInArrayFromStaticTag(ANDROID_SCALER_AVAILABLE_FORMATS,
+                                       HAL_PIXEL_FORMAT_YV12));
+}
+
+TEST_F(CameraMetadataTest, SaneResolutions) {
+    TEST_EXTENSION_FORKING_INIT;
+
+    // Iff there are listed raw resolutions, the format should be available
+    int rawResolutionsCount =
+            GetEntryCountFromStaticTag(HAL_PIXEL_FORMAT_RAW_SENSOR);
+    EXPECT_EQ(rawResolutionsCount > 0,
+        HasElementInArrayFromStaticTag(ANDROID_SCALER_AVAILABLE_FORMATS,
+                                        HAL_PIXEL_FORMAT_RAW_SENSOR));
+
+    // Required processed sizes.
+    int processedSizeCount =
+           GetEntryCountFromStaticTag(ANDROID_SCALER_AVAILABLE_PROCESSED_SIZES);
+    EXPECT_NE(0, processedSizeCount);
+    EXPECT_EQ(0, processedSizeCount % 2); // multiple of 2 (w,h)
+
+    // Required JPEG sizes
+    int jpegSizeCount =
+            GetEntryCountFromStaticTag(ANDROID_SCALER_AVAILABLE_JPEG_SIZES);
+    EXPECT_NE(0, jpegSizeCount);
+    EXPECT_EQ(0, jpegSizeCount % 2); // multiple of 2 (w,h)
+
+}
+
 }
 }
 }
diff --git a/tests/camera2/camera2.cpp b/tests/camera2/camera2.cpp
index 64803c0..0f75070 100644
--- a/tests/camera2/camera2.cpp
+++ b/tests/camera2/camera2.cpp
@@ -393,6 +393,18 @@
 
         getResolutionList(format,
                 &rawResolutions, &rawResolutionsCount);
+
+        if (rawResolutionsCount <= 0) {
+            const ::testing::TestInfo* const test_info =
+                ::testing::UnitTest::GetInstance()->current_test_info();
+            std::cerr << "Skipping test "
+                      << test_info->test_case_name() << "."
+                      << test_info->name()
+                      << " because the optional format was not available: "
+                      << "RAW_SENSOR" << std::endl;
+            return;
+        }
+
         ASSERT_LT((size_t)0, rawResolutionsCount);
 
         // Pick first available raw resolution
@@ -515,6 +527,18 @@
 
         getResolutionList(format,
                 &rawResolutions, &rawResolutionsCount);
+
+        if (rawResolutionsCount <= 0) {
+            const ::testing::TestInfo* const test_info =
+                ::testing::UnitTest::GetInstance()->current_test_info();
+            std::cerr << "Skipping test "
+                      << test_info->test_case_name() << "."
+                      << test_info->name()
+                      << " because the optional format was not available: "
+                      << "RAW_SENSOR" << std::endl;
+            return;
+        }
+
         ASSERT_LT((uint32_t)0, rawResolutionsCount);
 
         // Pick first available raw resolution
@@ -650,7 +674,7 @@
                     i,
                     &request);
             EXPECT_EQ(NO_ERROR, res) <<
-                    "Unable to construct request from template type %d", i;
+                    "Unable to construct request from template type " << i;
             EXPECT_TRUE(request != NULL);
             EXPECT_LT((size_t)0, get_camera_metadata_entry_count(request));
             EXPECT_LT((size_t)0, get_camera_metadata_data_count(request));