1 /*
2  * Copyright (C) 2019 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_TAG "HalCameraMetadataTests"
18 #include <log/log.h>
19 
20 #include <gtest/gtest.h>
21 #include <hal_camera_metadata.h>
22 #include <system/camera_metadata.h>
23 
24 namespace android {
25 namespace google_camera_hal {
26 
27 static constexpr uint32_t kDataBytes = 256;
28 static constexpr uint32_t kNumEntries = 10;
29 static constexpr uint32_t kDefaultDataBytes = 1;
30 static constexpr uint32_t kDefaultNumEntries = 1;
31 #define ARRAY_SIZE(a) (sizeof(a) / sizeof(a[0]))
32 
33 // Test creating HalCameraMetadata with sizes.
TEST(HalCameraMetadataTests,CreateWithSizes)34 TEST(HalCameraMetadataTests, CreateWithSizes) {
35   // Create HalCameraMetadata with sizes.
36   auto hal_metadata = HalCameraMetadata::Create(kNumEntries, kDataBytes);
37   ASSERT_NE(hal_metadata, nullptr) << "Creating hal_metadata failed.";
38 }
39 
40 // Test creating HalCameraMetadata with a camera_metadata.
TEST(HalCameraMetadataTests,CreateToOwn)41 TEST(HalCameraMetadataTests, CreateToOwn) {
42   camera_metadata_t* metadata =
43       allocate_camera_metadata(kNumEntries, kDataBytes);
44   ASSERT_NE(metadata, nullptr) << "Creating metadata failed.";
45 
46   auto hal_metadata = HalCameraMetadata::Create(metadata);
47   ASSERT_NE(hal_metadata, nullptr) << "Creating hal_metadata failed.";
48 }
49 
TEST(HalCameraMetadataTests,Clone)50 TEST(HalCameraMetadataTests, Clone) {
51   camera_metadata_t* m = allocate_camera_metadata(kNumEntries, kDataBytes);
52   auto metadata = HalCameraMetadata::Clone(m);
53   ASSERT_NE(metadata, nullptr) << "Cloning metadata failed.";
54 
55   free_camera_metadata(m);
56 }
57 
TEST(HalCameraMetadataTests,GetCameraMetadataSize)58 TEST(HalCameraMetadataTests, GetCameraMetadataSize) {
59   auto hal_metadata = HalCameraMetadata::Create(kNumEntries, kDataBytes);
60   ASSERT_NE(hal_metadata, nullptr) << "Creating hal_metadata failed.";
61 
62   // Verify the metadata size is >= what we asked for.
63   ASSERT_GE(hal_metadata->GetCameraMetadataSize(), kDataBytes);
64 }
65 
TEST(HalCameraMetadataTests,ReleaseCameraMetadata)66 TEST(HalCameraMetadataTests, ReleaseCameraMetadata) {
67   auto hal_metadata = HalCameraMetadata::Create(kNumEntries, kDataBytes);
68   ASSERT_NE(hal_metadata, nullptr) << "Creating hal_metadata failed.";
69 
70   camera_metadata_t* metadata = hal_metadata->ReleaseCameraMetadata();
71   ASSERT_NE(metadata, nullptr) << "Releasing hal_metadata failed.";
72 
73   free_camera_metadata(metadata);
74   metadata = nullptr;
75 
76   // Verify hal_metadata doesn't hold any metadata anymore.
77   ASSERT_EQ(hal_metadata->ReleaseCameraMetadata(), nullptr);
78   ASSERT_EQ(hal_metadata->GetCameraMetadataSize(), (uint32_t)0);
79 }
80 
TEST(HalCameraMetadataTests,GetRawCameraMetadata)81 TEST(HalCameraMetadataTests, GetRawCameraMetadata) {
82   camera_metadata_t* raw_metadata =
83       allocate_camera_metadata(kNumEntries, kDataBytes);
84   auto hal_metadata = HalCameraMetadata::Create(raw_metadata);
85   ASSERT_NE(hal_metadata, nullptr) << "Creating hal_metadata failed.";
86 
87   const camera_metadata_t* metadata = hal_metadata->GetRawCameraMetadata();
88   // GetRawCameraMetadata() must return the same pointer we used for Create()
89   ASSERT_EQ(metadata, raw_metadata) << "Getting hal_metadata failed.";
90   // Verify hal_metadata hasn't released the metadata
91   ASSERT_GE(hal_metadata->GetCameraMetadataSize(), (uint32_t)kDataBytes);
92 }
93 
SetGetMetadata(std::unique_ptr<HalCameraMetadata> hal_metadata,bool dump)94 void SetGetMetadata(std::unique_ptr<HalCameraMetadata> hal_metadata, bool dump) {
95   // int64 case
96   int64_t exposure_time_ns = 1000000000;
97   status_t res =
98       hal_metadata->Set(ANDROID_SENSOR_EXPOSURE_TIME, &exposure_time_ns, 1);
99   ASSERT_EQ(res, OK) << "Set int64 failed";
100 
101   camera_metadata_ro_entry entry;
102   res = hal_metadata->Get(ANDROID_SENSOR_EXPOSURE_TIME, &entry);
103   ASSERT_EQ(res, OK) << "Get ANDROID_SENSOR_EXPOSURE_TIME failed";
104   ASSERT_EQ(entry.count, (uint32_t)1) << "Get int64_t count failed.";
105   ASSERT_NE(entry.data.i64, nullptr) << "Get int64_t data nullptr.";
106   ASSERT_EQ(exposure_time_ns, *entry.data.i64) << "Get int64_t data failed.";
107 
108   // int32 case
109   int32_t sensitivity = 200;
110   res = hal_metadata->Set(ANDROID_SENSOR_SENSITIVITY, &sensitivity, 1);
111   ASSERT_EQ(res, OK) << "Set int32 failed";
112 
113   res = hal_metadata->Get(ANDROID_SENSOR_SENSITIVITY, &entry);
114   ASSERT_EQ(res, OK) << "Get ANDROID_SENSOR_SENSITIVITY failed";
115   ASSERT_EQ(entry.count, (uint32_t)1) << "Get int32_t count failed.";
116   ASSERT_NE(entry.data.i32, nullptr) << "Get int32_t data nullptr.";
117   ASSERT_EQ(sensitivity, *entry.data.i32) << "Get int32_t data failed.";
118 
119   // float case
120   float focus_distance = 0.5f;
121   res = hal_metadata->Set(ANDROID_LENS_FOCUS_DISTANCE, &focus_distance, 1);
122   ASSERT_EQ(res, OK) << "Set float failed";
123 
124   res = hal_metadata->Get(ANDROID_LENS_FOCUS_DISTANCE, &entry);
125   ASSERT_EQ(res, OK) << "Get ANDROID_LENS_FOCUS_DISTANCE failed";
126   ASSERT_EQ(entry.count, (uint32_t)1) << "Get float count failed.";
127   ASSERT_NE(entry.data.f, nullptr) << "Get float data nullptr.";
128   ASSERT_EQ(focus_distance, *entry.data.f) << "Get float data failed.";
129 
130   // rational case
131   camera_metadata_rational_t ae_compensation_step[] = {{0, 1}};
132   res =
133       hal_metadata->Set(ANDROID_CONTROL_AE_COMPENSATION_STEP,
134                         ae_compensation_step, ARRAY_SIZE(ae_compensation_step));
135   ASSERT_EQ(res, OK) << "Set rational failed";
136 
137   res = hal_metadata->Get(ANDROID_CONTROL_AE_COMPENSATION_STEP, &entry);
138   ASSERT_EQ(res, OK) << "Get ANDROID_CONTROL_AE_COMPENSATION_STEP failed";
139   ASSERT_EQ(entry.count, ARRAY_SIZE(ae_compensation_step))
140       << "Get rational count failed.";
141   for (uint32_t i = 0; i < entry.count; i++) {
142     ASSERT_EQ(ae_compensation_step[i].numerator, entry.data.r[i].numerator)
143         << "Get rational numerator failed at" << i;
144     ASSERT_EQ(ae_compensation_step[i].denominator, entry.data.r[i].denominator)
145         << "Get rational denominator failed at" << i;
146   }
147 
148   // uint8 case
149   uint8_t mode = ANDROID_CONTROL_SCENE_MODE_DISABLED;
150   res = hal_metadata->Set(ANDROID_CONTROL_AVAILABLE_SCENE_MODES, &mode, 1);
151   ASSERT_EQ(res, OK) << "Set uint8 failed";
152 
153   res = hal_metadata->Get(ANDROID_CONTROL_AVAILABLE_SCENE_MODES, &entry);
154   ASSERT_EQ(res, OK) << "Get ANDROID_CONTROL_AVAILABLE_SCENE_MODES failed";
155   ASSERT_EQ(entry.count, (uint32_t)1) << "Get uint8 count failed.";
156   ASSERT_NE(entry.data.u8, nullptr) << "Get uint8 data nullptr.";
157   ASSERT_EQ(mode, *entry.data.u8) << "Get uint8 data failed.";
158 
159   // double case
160   double noise[] = {1.234, 2.345};
161   res =
162       hal_metadata->Set(ANDROID_SENSOR_NOISE_PROFILE, noise, ARRAY_SIZE(noise));
163   ASSERT_EQ(res, OK) << "Set double failed";
164 
165   res = hal_metadata->Get(ANDROID_SENSOR_NOISE_PROFILE, &entry);
166   ASSERT_EQ(res, OK) << "Get ANDROID_SENSOR_NOISE_PROFILE failed";
167   ASSERT_EQ(entry.count, ARRAY_SIZE(noise)) << "Get double count failed.";
168   for (uint32_t i = 0; i < entry.count; i++) {
169     ASSERT_EQ(noise[i], entry.data.d[i])
170         << "Get double numerator failed at" << i;
171   }
172 
173   // string case
174   std::string string("1234");
175   res = hal_metadata->Set(ANDROID_LOGICAL_MULTI_CAMERA_PHYSICAL_IDS, string);
176   ASSERT_EQ(res, OK) << "Set string failed";
177 
178   res = hal_metadata->Get(ANDROID_LOGICAL_MULTI_CAMERA_PHYSICAL_IDS, &entry);
179   ASSERT_EQ(res, OK) << "Get ANDROID_LOGICAL_MULTI_CAMERA_PHYSICAL_IDS failed";
180   ASSERT_EQ(entry.count, string.length() + 1) << "Get string count failed.";
181   ASSERT_NE(entry.data.u8, nullptr) << "Get string data nullptr.";
182   ASSERT_STREQ(string.c_str(), reinterpret_cast<const char*>(entry.data.u8))
183       << "Get string data failed.";
184 
185   // Check the dump metadata to file
186   if (dump) {
187     std::FILE* f = std::tmpfile();
188     ASSERT_NE(f, nullptr);
189 
190     std::string test_string_key = "sensitivity";
191     std::string test_string_value = std::to_string(sensitivity);
192     hal_metadata->Dump(fileno(f), MetadataDumpVerbosity::kAllInformation,
193                        /*indentation*/ 0);
194 
195     char line[512];
196     bool found_test_string = false;
197 
198     // find key
199     std::rewind(f);
200     while (fgets(line, sizeof(line), f) != nullptr) {
201       if (std::string(line).find(test_string_key) != std::string::npos) {
202         found_test_string = true;
203       }
204     }
205     EXPECT_EQ(found_test_string, true) << "find sensitivity key failed";
206 
207     // find value
208     found_test_string = false;
209     std::rewind(f);
210     while (fgets(line, sizeof(line), f) != nullptr) {
211       if (std::string(line).find(test_string_value) != std::string::npos) {
212         found_test_string = true;
213       }
214     }
215     fclose(f);
216     EXPECT_EQ(found_test_string, true) << "find sensitivity value failed";
217 
218     // Dump file to logcat when fd < 0
219     hal_metadata->Dump(/*fd*/ -1, MetadataDumpVerbosity::kAllInformation,
220                        /*indentation*/ 0);
221   }
222 }
223 
TEST(HalCameraMetadataTests,SetGetMetadataWithoutResize)224 TEST(HalCameraMetadataTests, SetGetMetadataWithoutResize) {
225   auto hal_metadata = HalCameraMetadata::Create(kNumEntries, kDataBytes);
226   ASSERT_NE(hal_metadata, nullptr) << "Creating hal_metadata failed.";
227 
228   SetGetMetadata(std::move(hal_metadata), false);
229 }
230 
TEST(HalCameraMetadataTests,SetGetMetadataWithResize)231 TEST(HalCameraMetadataTests, SetGetMetadataWithResize) {
232   // only create 1 entry and 1 data byte metadata.
233   // It needs resize when set more metadata.
234   auto hal_metadata =
235       HalCameraMetadata::Create(kDefaultNumEntries, kDefaultDataBytes);
236   ASSERT_NE(hal_metadata, nullptr) << "Creating hal_metadata failed.";
237 
238   SetGetMetadata(std::move(hal_metadata), false);
239 }
240 
TEST(HalCameraMetadataTests,MetadataWithInvalidType)241 TEST(HalCameraMetadataTests, MetadataWithInvalidType) {
242   auto hal_metadata = HalCameraMetadata::Create(kNumEntries, kDataBytes);
243   ASSERT_NE(hal_metadata, nullptr) << "Creating hal_metadata failed.";
244 
245   // ANDROID_SENSOR_EXPOSURE_TIME is int64_t type
246   // And will set metadata fail if type is int32_t
247   int32_t exposure_time_ns = 100;
248   status_t res =
249       hal_metadata->Set(ANDROID_SENSOR_EXPOSURE_TIME, &exposure_time_ns, 1);
250   ASSERT_NE(res, OK) << "Set invalid type failed";
251 }
252 
TEST(HalCameraMetadataTests,EraseMetadata)253 TEST(HalCameraMetadataTests, EraseMetadata) {
254   auto hal_metadata = HalCameraMetadata::Create(kNumEntries, kDataBytes);
255   ASSERT_NE(hal_metadata, nullptr) << "Creating hal_metadata failed.";
256 
257   // int64 case
258   int64_t exposure_time_ns = 1000000000;
259   status_t res =
260       hal_metadata->Set(ANDROID_SENSOR_EXPOSURE_TIME, &exposure_time_ns, 1);
261   ASSERT_EQ(res, OK) << "Set int64 failed";
262 
263   camera_metadata_ro_entry entry;
264   res = hal_metadata->Get(ANDROID_SENSOR_EXPOSURE_TIME, &entry);
265   ASSERT_EQ(res, OK) << "Get ANDROID_SENSOR_EXPOSURE_TIME failed";
266   ASSERT_EQ(entry.count, (uint32_t)1) << "Get int64_t count failed.";
267   ASSERT_NE(entry.data.i64, nullptr) << "Get int64_t data nullptr.";
268   ASSERT_EQ(exposure_time_ns, *entry.data.i64) << "Get int64_t data failed.";
269 
270   res = hal_metadata->Erase(ANDROID_SENSOR_EXPOSURE_TIME);
271   ASSERT_EQ(res, OK) << "Erase failed";
272 
273   res = hal_metadata->Get(ANDROID_SENSOR_EXPOSURE_TIME, &entry);
274   ASSERT_EQ(res, NAME_NOT_FOUND) << "Erase and check tag failed";
275 
276   res = hal_metadata->Get(ANDROID_SENSOR_EXPOSURE_TIME, nullptr);
277   ASSERT_EQ(res, BAD_VALUE) << "Get with nullptr did not return BAD_VALUE";
278 }
279 
TEST(HalCameraMetadataTests,Dump)280 TEST(HalCameraMetadataTests, Dump) {
281   auto hal_metadata = HalCameraMetadata::Create(kNumEntries, kDataBytes);
282   ASSERT_NE(hal_metadata, nullptr) << "Creating hal_metadata failed.";
283 
284   SetGetMetadata(std::move(hal_metadata), true);
285 }
286 
TEST(HalCameraMetadataTests,AppendMetadata)287 TEST(HalCameraMetadataTests, AppendMetadata) {
288   auto hal_metadata = HalCameraMetadata::Create(kNumEntries, kDataBytes);
289   ASSERT_NE(hal_metadata, nullptr) << "Creating hal_metadata failed.";
290 
291   int64_t exposure_time_ns = 1000000000;
292   status_t res =
293       hal_metadata->Set(ANDROID_SENSOR_EXPOSURE_TIME, &exposure_time_ns, 1);
294   ASSERT_EQ(res, OK) << "Set int64 failed";
295 
296   camera_metadata_ro_entry entry;
297   res = hal_metadata->Get(ANDROID_SENSOR_EXPOSURE_TIME, &entry);
298   ASSERT_EQ(res, OK) << "Get ANDROID_SENSOR_EXPOSURE_TIME failed";
299   ASSERT_EQ(entry.count, (uint32_t)1) << "Get int64_t count failed.";
300   ASSERT_NE(entry.data.i64, nullptr) << "Get int64_t data nullptr.";
301   ASSERT_EQ(exposure_time_ns, *entry.data.i64) << "Get int64_t data failed.";
302 
303   auto hal_metadata_dst =
304       HalCameraMetadata::Create(kDefaultNumEntries, kDefaultDataBytes);
305   ASSERT_NE(hal_metadata_dst, nullptr) << "Creating hal_metadata_dst failed.";
306 
307   res = hal_metadata_dst->Append(nullptr);
308   ASSERT_EQ(res, BAD_VALUE) << "Append nullptr failed";
309 
310   res = hal_metadata_dst->Append(std::move(hal_metadata));
311   ASSERT_EQ(res, OK) << "Append failed";
312 
313   res = hal_metadata_dst->Get(ANDROID_SENSOR_EXPOSURE_TIME, &entry);
314   ASSERT_EQ(res, OK) << "Get ANDROID_SENSOR_EXPOSURE_TIME failed after append";
315 }
316 
TEST(HalCameraMetadataTests,GetEntryCount)317 TEST(HalCameraMetadataTests, GetEntryCount) {
318   auto hal_metadata = HalCameraMetadata::Create(kNumEntries, kDataBytes);
319   ASSERT_NE(hal_metadata, nullptr) << "Creating hal_metadata failed.";
320 
321   auto size = hal_metadata->GetEntryCount();
322   ASSERT_EQ(size, (uint32_t)0) << "Get empty entry count failed.";
323 
324   int64_t exposure_time_ns = 1000000000;
325   status_t res =
326       hal_metadata->Set(ANDROID_SENSOR_EXPOSURE_TIME, &exposure_time_ns, 1);
327   ASSERT_EQ(res, OK) << "Set int64 failed";
328 
329   size = hal_metadata->GetEntryCount();
330   ASSERT_EQ(size, (uint32_t)1) << "Get entry count failed.";
331 }
332 
TEST(HalCameraMetadataTests,GetByEntryIndex)333 TEST(HalCameraMetadataTests, GetByEntryIndex) {
334   auto hal_metadata = HalCameraMetadata::Create(kNumEntries, kDataBytes);
335   ASSERT_NE(hal_metadata, nullptr) << "Creating hal_metadata failed.";
336 
337   camera_metadata_ro_entry entry;
338   status_t res = hal_metadata->GetByIndex(&entry, /*entry_index*/ 0);
339   ASSERT_NE(res, OK) << "Get invalid index 0 failed";
340 
341   int64_t exposure_time_ns = 1000000000;
342   res = hal_metadata->Set(ANDROID_SENSOR_EXPOSURE_TIME, &exposure_time_ns, 1);
343   ASSERT_EQ(res, OK) << "Set int64 failed";
344 
345   auto size = hal_metadata->GetEntryCount();
346   ASSERT_EQ(size, (uint32_t)1) << "Get entry count failed.";
347 
348   res = hal_metadata->GetByIndex(&entry, /*entry_index*/ 0);
349   ASSERT_EQ(res, OK) << "Get ANDROID_SENSOR_EXPOSURE_TIME failed";
350   ASSERT_EQ(entry.count, (uint32_t)1) << "Get int64_t count failed.";
351   ASSERT_NE(entry.data.i64, nullptr) << "Get int64_t data nullptr.";
352   ASSERT_EQ(exposure_time_ns, *entry.data.i64) << "Get int64_t data failed.";
353 
354   res = hal_metadata->GetByIndex(&entry, /*entry_index*/ 1);
355   ASSERT_NE(res, OK) << "Get invalid index 1 failed";
356 }
357 
358 }  // namespace google_camera_hal
359 }  // namespace android
360