1 /*
2  * Copyright (C) 2016 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 #include "enum_converter.h"
18 
19 #include <gtest/gtest.h>
20 
21 using testing::Test;
22 
23 namespace v4l2_camera_hal {
24 
25 class EnumConverterTest : public Test {
26  protected:
SetUp()27   virtual void SetUp() {
28     converter_.reset(
29         new EnumConverter({{one_to_one_v4l2_, one_to_one_metadata_},
30                            {one_to_many_v4l2_, many_to_one_metadata_1_},
31                            {one_to_many_v4l2_, many_to_one_metadata_2_},
32                            {many_to_one_v4l2_1_, one_to_many_metadata_},
33                            {many_to_one_v4l2_2_, one_to_many_metadata_},
34                            {unused_v4l2_, unused_metadata_}}));
35   }
36 
37   std::unique_ptr<EnumConverter> converter_;
38 
39   const int32_t one_to_one_v4l2_ = 12;
40   const int32_t one_to_many_v4l2_ = 34;
41   const int32_t many_to_one_v4l2_1_ = 56;
42   const int32_t many_to_one_v4l2_2_ = 78;
43   const int32_t unused_v4l2_ = 910;
44   const uint8_t one_to_one_metadata_ = 109;
45   const uint8_t one_to_many_metadata_ = 87;
46   const uint8_t many_to_one_metadata_1_ = 65;
47   const uint8_t many_to_one_metadata_2_ = 43;
48   const uint8_t unused_metadata_ = 21;
49 };
50 
51 // Convert single.
TEST_F(EnumConverterTest,OneToOneConversion)52 TEST_F(EnumConverterTest, OneToOneConversion) {
53   uint8_t metadata_val = 1;
54   ASSERT_EQ(converter_->V4L2ToMetadata(one_to_one_v4l2_, &metadata_val), 0);
55   EXPECT_EQ(metadata_val, one_to_one_metadata_);
56 
57   int32_t v4l2_val = 1;
58   ASSERT_EQ(converter_->MetadataToV4L2(one_to_one_metadata_, &v4l2_val), 0);
59   EXPECT_EQ(v4l2_val, one_to_one_v4l2_);
60 }
61 
TEST_F(EnumConverterTest,OneToManyConversion)62 TEST_F(EnumConverterTest, OneToManyConversion) {
63   // Should be one of the acceptable values.
64   uint8_t metadata_val = 1;
65   ASSERT_EQ(converter_->V4L2ToMetadata(one_to_many_v4l2_, &metadata_val), 0);
66   EXPECT_TRUE(metadata_val == many_to_one_metadata_1_ ||
67               metadata_val == many_to_one_metadata_2_);
68 
69   int32_t v4l2_val = 1;
70   ASSERT_EQ(converter_->MetadataToV4L2(one_to_many_metadata_, &v4l2_val), 0);
71   EXPECT_TRUE(v4l2_val == many_to_one_v4l2_1_ ||
72               v4l2_val == many_to_one_v4l2_2_);
73 }
74 
TEST_F(EnumConverterTest,ManyToOneConversion)75 TEST_F(EnumConverterTest, ManyToOneConversion) {
76   uint8_t metadata_val = 1;
77   ASSERT_EQ(converter_->V4L2ToMetadata(many_to_one_v4l2_1_, &metadata_val), 0);
78   EXPECT_EQ(metadata_val, one_to_many_metadata_);
79   metadata_val = 1;  // Reset.
80   ASSERT_EQ(converter_->V4L2ToMetadata(many_to_one_v4l2_2_, &metadata_val), 0);
81   EXPECT_EQ(metadata_val, one_to_many_metadata_);
82 
83   int32_t v4l2_val = 1;
84   ASSERT_EQ(converter_->MetadataToV4L2(many_to_one_metadata_1_, &v4l2_val), 0);
85   EXPECT_EQ(v4l2_val, one_to_many_v4l2_);
86   v4l2_val = 1;  // Reset.
87   ASSERT_EQ(converter_->MetadataToV4L2(many_to_one_metadata_2_, &v4l2_val), 0);
88   EXPECT_EQ(v4l2_val, one_to_many_v4l2_);
89 }
90 
TEST_F(EnumConverterTest,InvalidConversion)91 TEST_F(EnumConverterTest, InvalidConversion) {
92   uint8_t metadata_val = 1;
93   EXPECT_EQ(converter_->V4L2ToMetadata(1, &metadata_val), -EINVAL);
94 
95   int32_t v4l2_val = 1;
96   EXPECT_EQ(converter_->MetadataToV4L2(1, &v4l2_val), -EINVAL);
97 }
98 
99 }  // namespace v4l2_camera_hal
100