VendorTagDescriptorTests.cpp revision d1176ef16677b6c94fb893edb6a864cdccc0b190
1/* 2 * Copyright (C) 2014 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#define LOG_NDEBUG 0 18#define LOG_TAG "VendorTagDescriptorTests" 19 20#include <binder/Parcel.h> 21#include <camera/VendorTagDescriptor.h> 22#include <camera_metadata_tests_fake_vendor.h> 23#include <camera_metadata_hidden.h> 24#include <system/camera_vendor_tags.h> 25#include <utils/Errors.h> 26#include <utils/Log.h> 27#include <utils/RefBase.h> 28 29#include <gtest/gtest.h> 30#include <stdint.h> 31 32using namespace android; 33 34enum { 35 BAD_TAG_ARRAY = 0xDEADBEEFu, 36 BAD_TAG = 0x8DEADBADu, 37}; 38 39#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0])) 40 41static bool ContainsTag(uint32_t* tagArray, size_t size, uint32_t tag) { 42 for (size_t i = 0; i < size; ++i) { 43 if (tag == tagArray[i]) return true; 44 } 45 return false; 46} 47 48#define EXPECT_CONTAINS_TAG(t, a) \ 49 EXPECT_TRUE(ContainsTag(a, ARRAY_SIZE(a), t)) 50 51#define ASSERT_NOT_NULL(x) \ 52 ASSERT_TRUE((x) != NULL) 53 54extern "C" { 55 56static int default_get_tag_count(const vendor_tag_ops_t* vOps) { 57 return VENDOR_TAG_COUNT_ERR; 58} 59 60static void default_get_all_tags(const vendor_tag_ops_t* vOps, uint32_t* tagArray) { 61 //Noop 62} 63 64static const char* default_get_section_name(const vendor_tag_ops_t* vOps, uint32_t tag) { 65 return VENDOR_SECTION_NAME_ERR; 66} 67 68static const char* default_get_tag_name(const vendor_tag_ops_t* vOps, uint32_t tag) { 69 return VENDOR_TAG_NAME_ERR; 70} 71 72static int default_get_tag_type(const vendor_tag_ops_t* vOps, uint32_t tag) { 73 return VENDOR_TAG_TYPE_ERR; 74} 75 76} /*extern "C"*/ 77 78// Set default vendor operations for a vendor_tag_ops struct 79static void FillWithDefaults(vendor_tag_ops_t* vOps) { 80 ASSERT_NOT_NULL(vOps); 81 vOps->get_tag_count = default_get_tag_count; 82 vOps->get_all_tags = default_get_all_tags; 83 vOps->get_section_name = default_get_section_name; 84 vOps->get_tag_name = default_get_tag_name; 85 vOps->get_tag_type = default_get_tag_type; 86} 87 88/** 89 * Test if values from VendorTagDescriptor methods match corresponding values 90 * from vendor_tag_ops functions. 91 */ 92TEST(VendorTagDescriptorTest, ConsistentWithVendorTags) { 93 sp<VendorTagDescriptor> vDesc; 94 const vendor_tag_ops_t *vOps = &fakevendor_ops; 95 EXPECT_EQ(OK, VendorTagDescriptor::createDescriptorFromOps(vOps, /*out*/vDesc)); 96 97 ASSERT_NOT_NULL(vDesc); 98 99 // Ensure reasonable tag count 100 int tagCount = vDesc->getTagCount(); 101 EXPECT_EQ(tagCount, vOps->get_tag_count(vOps)); 102 103 uint32_t descTagArray[tagCount]; 104 uint32_t opsTagArray[tagCount]; 105 106 // Get all tag ids 107 vDesc->getTagArray(descTagArray); 108 vOps->get_all_tags(vOps, opsTagArray); 109 110 ASSERT_NOT_NULL(descTagArray); 111 ASSERT_NOT_NULL(opsTagArray); 112 113 uint32_t tag; 114 for (int i = 0; i < tagCount; ++i) { 115 // For each tag id, check whether type, section name, tag name match 116 tag = descTagArray[i]; 117 EXPECT_CONTAINS_TAG(tag, opsTagArray); 118 EXPECT_EQ(vDesc->getTagType(tag), vOps->get_tag_type(vOps, tag)); 119 EXPECT_STREQ(vDesc->getSectionName(tag), vOps->get_section_name(vOps, tag)); 120 EXPECT_STREQ(vDesc->getTagName(tag), vOps->get_tag_name(vOps, tag)); 121 } 122} 123 124/** 125 * Test if values from VendorTagDescriptor methods stay consistent after being 126 * parcelled/unparcelled. 127 */ 128TEST(VendorTagDescriptorTest, ConsistentAcrossParcel) { 129 sp<VendorTagDescriptor> vDescOriginal, vDescParceled; 130 const vendor_tag_ops_t *vOps = &fakevendor_ops; 131 EXPECT_EQ(OK, VendorTagDescriptor::createDescriptorFromOps(vOps, /*out*/vDescOriginal)); 132 133 ASSERT_TRUE(vDescOriginal != NULL); 134 135 Parcel p; 136 137 // Check whether parcel read/write succeed 138 EXPECT_EQ(OK, vDescOriginal->writeToParcel(&p)); 139 p.setDataPosition(0); 140 ASSERT_EQ(OK, VendorTagDescriptor::createFromParcel(&p, vDescParceled)); 141 142 // Ensure consistent tag count 143 int tagCount = vDescOriginal->getTagCount(); 144 ASSERT_EQ(tagCount, vDescParceled->getTagCount()); 145 146 uint32_t descTagArray[tagCount]; 147 uint32_t desc2TagArray[tagCount]; 148 149 // Get all tag ids 150 vDescOriginal->getTagArray(descTagArray); 151 vDescParceled->getTagArray(desc2TagArray); 152 153 ASSERT_NOT_NULL(descTagArray); 154 ASSERT_NOT_NULL(desc2TagArray); 155 156 uint32_t tag; 157 for (int i = 0; i < tagCount; ++i) { 158 // For each tag id, check consistency between the two vendor tag 159 // descriptors for each type, section name, tag name 160 tag = descTagArray[i]; 161 EXPECT_CONTAINS_TAG(tag, desc2TagArray); 162 EXPECT_EQ(vDescOriginal->getTagType(tag), vDescParceled->getTagType(tag)); 163 EXPECT_STREQ(vDescOriginal->getSectionName(tag), vDescParceled->getSectionName(tag)); 164 EXPECT_STREQ(vDescOriginal->getTagName(tag), vDescParceled->getTagName(tag)); 165 } 166} 167 168/** 169 * Test defaults and error conditions. 170 */ 171TEST(VendorTagDescriptorTest, ErrorConditions) { 172 sp<VendorTagDescriptor> vDesc; 173 vendor_tag_ops_t vOps; 174 FillWithDefaults(&vOps); 175 176 // Ensure create fails when using null vOps 177 EXPECT_EQ(BAD_VALUE, VendorTagDescriptor::createDescriptorFromOps(/*vOps*/NULL, vDesc)); 178 179 // Ensure create works when there are no vtags defined in a well-formed vOps 180 ASSERT_EQ(OK, VendorTagDescriptor::createDescriptorFromOps(&vOps, vDesc)); 181 182 // Ensure defaults are returned when no vtags are defined, or tag is unknown 183 EXPECT_EQ(VENDOR_TAG_COUNT_ERR, vDesc->getTagCount()); 184 uint32_t* tagArray = reinterpret_cast<uint32_t*>(BAD_TAG_ARRAY); 185 uint32_t* testArray = tagArray; 186 vDesc->getTagArray(tagArray); 187 EXPECT_EQ(testArray, tagArray); 188 EXPECT_EQ(VENDOR_SECTION_NAME_ERR, vDesc->getSectionName(BAD_TAG)); 189 EXPECT_EQ(VENDOR_TAG_NAME_ERR, vDesc->getTagName(BAD_TAG)); 190 EXPECT_EQ(VENDOR_TAG_TYPE_ERR, vDesc->getTagType(BAD_TAG)); 191 192 // Make sure global can be set/cleared 193 const vendor_tag_ops_t *fakeOps = &fakevendor_ops; 194 sp<VendorTagDescriptor> prevGlobal = VendorTagDescriptor::getGlobalVendorTagDescriptor(); 195 VendorTagDescriptor::clearGlobalVendorTagDescriptor(); 196 197 EXPECT_TRUE(VendorTagDescriptor::getGlobalVendorTagDescriptor() == NULL); 198 EXPECT_EQ(OK, VendorTagDescriptor::setAsGlobalVendorTagDescriptor(vDesc)); 199 EXPECT_TRUE(VendorTagDescriptor::getGlobalVendorTagDescriptor() != NULL); 200 EXPECT_EQ(VENDOR_SECTION_NAME_ERR, vDesc->getSectionName(BAD_TAG)); 201 EXPECT_EQ(OK, VendorTagDescriptor::setAsGlobalVendorTagDescriptor(prevGlobal)); 202 EXPECT_EQ(prevGlobal, VendorTagDescriptor::getGlobalVendorTagDescriptor()); 203} 204 205