Home | History | Annotate | Download | only in tests
      1 /*
      2  * Copyright (C) 2012 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 <errno.h>
     18 
     19 #include "gtest/gtest.h"
     20 #include "system/camera_metadata.h"
     21 
     22 #include "camera_metadata_tests_fake_vendor.h"
     23 
     24 #define EXPECT_NULL(x)     EXPECT_EQ((void*)0, x)
     25 #define EXPECT_NOT_NULL(x) EXPECT_NE((void*)0, x)
     26 
     27 #define OK    0
     28 #define ERROR 1
     29 #define NOT_FOUND (-ENOENT)
     30 
     31 TEST(camera_metadata, allocate_normal) {
     32     camera_metadata_t *m = NULL;
     33     const size_t entry_capacity = 5;
     34     const size_t data_capacity = 32;
     35 
     36     m = allocate_camera_metadata(entry_capacity, data_capacity);
     37 
     38     EXPECT_NOT_NULL(m);
     39     EXPECT_EQ((size_t)0, get_camera_metadata_entry_count(m));
     40     EXPECT_EQ(entry_capacity, get_camera_metadata_entry_capacity(m));
     41     EXPECT_EQ((size_t)0, get_camera_metadata_data_count(m));
     42     EXPECT_EQ(data_capacity, get_camera_metadata_data_capacity(m));
     43 
     44     free_camera_metadata(m);
     45 }
     46 
     47 TEST(camera_metadata, allocate_nodata) {
     48     camera_metadata_t *m = NULL;
     49 
     50     m = allocate_camera_metadata(1, 0);
     51 
     52     EXPECT_NOT_NULL(m);
     53     EXPECT_EQ((size_t)0, get_camera_metadata_entry_count(m));
     54     EXPECT_EQ((size_t)1, get_camera_metadata_entry_capacity(m));
     55     EXPECT_EQ((size_t)0, get_camera_metadata_data_count(m));
     56     EXPECT_EQ((size_t)0, get_camera_metadata_data_capacity(m));
     57 
     58     free_camera_metadata(m);
     59 }
     60 
     61 TEST(camera_metadata, allocate_nothing) {
     62     camera_metadata_t *m = NULL;
     63 
     64     m = allocate_camera_metadata(0, 0);
     65 
     66     EXPECT_NULL(m);
     67 }
     68 
     69 TEST(camera_metadata, place_normal) {
     70     camera_metadata_t *m = NULL;
     71     void *buf = NULL;
     72 
     73     const size_t entry_capacity = 5;
     74     const size_t data_capacity = 32;
     75 
     76     size_t buf_size = calculate_camera_metadata_size(entry_capacity,
     77             data_capacity);
     78 
     79     EXPECT_TRUE(buf_size > 0);
     80 
     81     buf = malloc(buf_size);
     82 
     83     EXPECT_NOT_NULL(buf);
     84 
     85     m = place_camera_metadata(buf, buf_size, entry_capacity, data_capacity);
     86 
     87     EXPECT_EQ(buf, (uint8_t*)m);
     88     EXPECT_EQ((size_t)0, get_camera_metadata_entry_count(m));
     89     EXPECT_EQ(entry_capacity, get_camera_metadata_entry_capacity(m));
     90     EXPECT_EQ((size_t)0, get_camera_metadata_data_count(m));
     91     EXPECT_EQ(data_capacity, get_camera_metadata_data_capacity(m));
     92 
     93     free(buf);
     94 }
     95 
     96 TEST(camera_metadata, place_nospace) {
     97     camera_metadata_t *m = NULL;
     98     void *buf = NULL;
     99 
    100     const size_t entry_capacity = 5;
    101     const size_t data_capacity = 32;
    102 
    103     size_t buf_size = calculate_camera_metadata_size(entry_capacity,
    104             data_capacity);
    105 
    106     EXPECT_GT(buf_size, (size_t)0);
    107 
    108     buf_size--;
    109 
    110     buf = malloc(buf_size);
    111 
    112     EXPECT_NOT_NULL(buf);
    113 
    114     m = place_camera_metadata(buf, buf_size, entry_capacity, data_capacity);
    115 
    116     EXPECT_NULL(m);
    117 
    118     free(buf);
    119 }
    120 
    121 TEST(camera_metadata, place_extraspace) {
    122     camera_metadata_t *m = NULL;
    123     uint8_t *buf = NULL;
    124 
    125     const size_t entry_capacity = 5;
    126     const size_t data_capacity = 32;
    127     const size_t extra_space = 10;
    128 
    129     size_t buf_size = calculate_camera_metadata_size(entry_capacity,
    130             data_capacity);
    131 
    132     EXPECT_GT(buf_size, (size_t)0);
    133 
    134     buf_size += extra_space;
    135 
    136     buf = (uint8_t*)malloc(buf_size);
    137 
    138     EXPECT_NOT_NULL(buf);
    139 
    140     m = place_camera_metadata(buf, buf_size, entry_capacity, data_capacity);
    141 
    142     EXPECT_EQ((uint8_t*)m, buf);
    143     EXPECT_EQ((size_t)0, get_camera_metadata_entry_count(m));
    144     EXPECT_EQ(entry_capacity, get_camera_metadata_entry_capacity(m));
    145     EXPECT_EQ((size_t)0, get_camera_metadata_data_count(m));
    146     EXPECT_EQ(data_capacity, get_camera_metadata_data_capacity(m));
    147     EXPECT_EQ(buf + buf_size - extra_space, (uint8_t*)m + get_camera_metadata_size(m));
    148 
    149     free(buf);
    150 }
    151 
    152 TEST(camera_metadata, get_size) {
    153     camera_metadata_t *m = NULL;
    154     const size_t entry_capacity = 5;
    155     const size_t data_capacity = 32;
    156 
    157     m = allocate_camera_metadata(entry_capacity, data_capacity);
    158 
    159     EXPECT_EQ(calculate_camera_metadata_size(entry_capacity, data_capacity),
    160             get_camera_metadata_size(m) );
    161 
    162     EXPECT_EQ(calculate_camera_metadata_size(0,0),
    163             get_camera_metadata_compact_size(m) );
    164 
    165     free_camera_metadata(m);
    166 }
    167 
    168 TEST(camera_metadata, add_get_normal) {
    169     camera_metadata_t *m = NULL;
    170     const size_t entry_capacity = 5;
    171     const size_t data_capacity = 80;
    172 
    173     m = allocate_camera_metadata(entry_capacity, data_capacity);
    174 
    175     int result;
    176     size_t data_used = 0;
    177     size_t entries_used = 0;
    178 
    179     // INT64
    180 
    181     int64_t exposure_time = 1000000000;
    182     result = add_camera_metadata_entry(m,
    183             ANDROID_SENSOR_EXPOSURE_TIME,
    184             &exposure_time, 1);
    185     EXPECT_EQ(OK, result);
    186     data_used += calculate_camera_metadata_entry_data_size(
    187             get_camera_metadata_tag_type(ANDROID_SENSOR_EXPOSURE_TIME), 1);
    188     entries_used++;
    189 
    190     // INT32
    191 
    192     int32_t sensitivity = 800;
    193     result = add_camera_metadata_entry(m,
    194             ANDROID_SENSOR_SENSITIVITY,
    195             &sensitivity, 1);
    196     EXPECT_EQ(OK, result);
    197     data_used += calculate_camera_metadata_entry_data_size(
    198             get_camera_metadata_tag_type(ANDROID_SENSOR_SENSITIVITY), 1);
    199     entries_used++;
    200 
    201     // FLOAT
    202 
    203     float focusDistance = 0.5f;
    204     result = add_camera_metadata_entry(m,
    205             ANDROID_LENS_FOCUS_DISTANCE,
    206             &focusDistance, 1);
    207     EXPECT_EQ(OK, result);
    208     data_used += calculate_camera_metadata_entry_data_size(
    209             get_camera_metadata_tag_type(ANDROID_LENS_FOCUS_DISTANCE), 1);
    210     entries_used++;
    211 
    212     // Array of FLOAT
    213 
    214     float colorTransform[9] = {
    215         0.9f, 0.0f, 0.0f,
    216         0.2f, 0.5f, 0.0f,
    217         0.0f, 0.1f, 0.7f
    218     };
    219     result = add_camera_metadata_entry(m,
    220             ANDROID_COLOR_TRANSFORM,
    221             colorTransform, 9);
    222     EXPECT_EQ(OK, result);
    223     data_used += calculate_camera_metadata_entry_data_size(
    224             get_camera_metadata_tag_type(ANDROID_COLOR_TRANSFORM), 9);
    225     entries_used++;
    226 
    227     // Check added entries
    228 
    229     uint32_t tag = 0;
    230     uint8_t type = 0;
    231     int32_t *data_int32;
    232     int64_t *data_int64;
    233     float *data_float;
    234     size_t data_count = 0;
    235 
    236     result = get_camera_metadata_entry(m,
    237             0, &tag, &type, (void**)&data_int64, &data_count);
    238     EXPECT_EQ(OK, result);
    239     EXPECT_EQ(ANDROID_SENSOR_EXPOSURE_TIME, tag);
    240     EXPECT_EQ(TYPE_INT64, type);
    241     EXPECT_EQ((size_t)1, data_count);
    242     EXPECT_EQ(exposure_time, *data_int64);
    243 
    244     result = get_camera_metadata_entry(m,
    245             1, &tag, &type, (void**)&data_int32, &data_count);
    246     EXPECT_EQ(OK, result);
    247     EXPECT_EQ(ANDROID_SENSOR_SENSITIVITY, tag);
    248     EXPECT_EQ(TYPE_INT32, type);
    249     EXPECT_EQ((size_t)1, data_count);
    250     EXPECT_EQ(sensitivity, *data_int32);
    251 
    252     result = get_camera_metadata_entry(m,
    253             2, &tag, &type, (void**)&data_float, &data_count);
    254     EXPECT_EQ(OK, result);
    255     EXPECT_EQ(ANDROID_LENS_FOCUS_DISTANCE, tag);
    256     EXPECT_EQ(TYPE_FLOAT, type);
    257     EXPECT_EQ((size_t)1, data_count);
    258     EXPECT_EQ(focusDistance, *data_float);
    259 
    260     result = get_camera_metadata_entry(m,
    261             3, &tag, &type, (void**)&data_float, &data_count);
    262     EXPECT_EQ(OK, result);
    263     EXPECT_EQ(ANDROID_COLOR_TRANSFORM, tag);
    264     EXPECT_EQ(TYPE_FLOAT, type);
    265     EXPECT_EQ((size_t)9, data_count);
    266     for (unsigned int i=0; i < data_count; i++) {
    267         EXPECT_EQ(colorTransform[i], data_float[i] );
    268     }
    269 
    270     EXPECT_EQ(calculate_camera_metadata_size(entry_capacity, data_capacity),
    271             get_camera_metadata_size(m) );
    272 
    273     EXPECT_EQ(calculate_camera_metadata_size(entries_used, data_used),
    274             get_camera_metadata_compact_size(m) );
    275 
    276     dump_camera_metadata(m, 0, 2);
    277 
    278     free_camera_metadata(m);
    279 }
    280 
    281 void add_test_metadata(camera_metadata_t *m, int entry_count) {
    282 
    283     EXPECT_NOT_NULL(m);
    284 
    285     int result;
    286     size_t data_used = 0;
    287     size_t entries_used = 0;
    288     int64_t exposure_time;
    289     for (int i=0; i < entry_count; i++ ) {
    290         exposure_time = 100 + i * 100;
    291         result = add_camera_metadata_entry(m,
    292                 ANDROID_SENSOR_EXPOSURE_TIME,
    293                 &exposure_time, 1);
    294         EXPECT_EQ(OK, result);
    295         data_used += calculate_camera_metadata_entry_data_size(
    296                 get_camera_metadata_tag_type(ANDROID_SENSOR_EXPOSURE_TIME), 1);
    297         entries_used++;
    298     }
    299     EXPECT_EQ(data_used, get_camera_metadata_data_count(m));
    300     EXPECT_EQ(entries_used, get_camera_metadata_entry_count(m));
    301     EXPECT_GT(get_camera_metadata_data_capacity(m),
    302             get_camera_metadata_data_count(m));
    303 }
    304 
    305 TEST(camera_metadata, add_get_toomany) {
    306     camera_metadata_t *m = NULL;
    307     const size_t entry_capacity = 5;
    308     const size_t data_capacity = 50;
    309     int result;
    310 
    311     m = allocate_camera_metadata(entry_capacity, data_capacity);
    312 
    313     add_test_metadata(m, entry_capacity);
    314 
    315     int32_t sensitivity = 100;
    316     result = add_camera_metadata_entry(m,
    317             ANDROID_SENSOR_SENSITIVITY,
    318             &sensitivity, 1);
    319 
    320     EXPECT_EQ(ERROR, result);
    321 
    322     uint32_t tag = 0;
    323     uint8_t type = 0;
    324     int32_t *data_int32;
    325     size_t data_count = 0;
    326     for (unsigned int i=0; i < entry_capacity; i++) {
    327         int64_t exposure_time = 100 + i * 100;
    328         result = get_camera_metadata_entry(m,
    329                 i, &tag, &type, (void**)&data_int32, &data_count);
    330         EXPECT_EQ(OK, result);
    331         EXPECT_EQ(ANDROID_SENSOR_EXPOSURE_TIME, tag);
    332         EXPECT_EQ(TYPE_INT64, type);
    333         EXPECT_EQ((size_t)1, data_count);
    334         EXPECT_EQ(exposure_time, *data_int32);
    335     }
    336     tag = 0;
    337     type = 0;
    338     data_int32 = NULL;
    339     data_count = 0;
    340     result = get_camera_metadata_entry(m,
    341             entry_capacity, &tag, &type, (void**)&data_int32, &data_count);
    342     EXPECT_EQ(ERROR, result);
    343     EXPECT_EQ((uint32_t)0, tag);
    344     EXPECT_EQ((uint8_t)0, type);
    345     EXPECT_EQ((size_t)0, data_count);
    346     EXPECT_EQ(NULL, data_int32);
    347 
    348     dump_camera_metadata(m, 0, 2);
    349 
    350     free_camera_metadata(m);
    351 }
    352 
    353 TEST(camera_metadata, copy_metadata) {
    354     camera_metadata_t *m = NULL;
    355     const size_t entry_capacity = 50;
    356     const size_t data_capacity = 450;
    357 
    358     int result;
    359 
    360     m = allocate_camera_metadata(entry_capacity, data_capacity);
    361 
    362     add_test_metadata(m, entry_capacity);
    363 
    364     size_t buf_size = get_camera_metadata_compact_size(m);
    365     EXPECT_LT((size_t)0, buf_size);
    366 
    367     uint8_t *buf = (uint8_t*)malloc(buf_size);
    368     EXPECT_NOT_NULL(buf);
    369 
    370     camera_metadata_t *m2 = copy_camera_metadata(buf, buf_size, m);
    371     EXPECT_NOT_NULL(m2);
    372     EXPECT_EQ(buf, (uint8_t*)m2);
    373     EXPECT_EQ(get_camera_metadata_entry_count(m),
    374             get_camera_metadata_entry_count(m2));
    375     EXPECT_EQ(get_camera_metadata_data_count(m),
    376             get_camera_metadata_data_count(m2));
    377     EXPECT_EQ(get_camera_metadata_entry_capacity(m2),
    378             get_camera_metadata_entry_count(m2));
    379     EXPECT_EQ(get_camera_metadata_data_capacity(m2),
    380             get_camera_metadata_data_count(m2));
    381 
    382     for (unsigned int i=0; i < get_camera_metadata_entry_count(m); i++) {
    383         uint32_t tag, tag2;
    384         uint8_t type, type2;
    385         uint8_t *data, *data2;
    386         size_t   data_count, data_count2;
    387 
    388         int result;
    389         result = get_camera_metadata_entry(m,
    390                 i, &tag, &type, (void**)&data, &data_count);
    391         EXPECT_EQ(OK, result);
    392         result = get_camera_metadata_entry(m2,
    393                 i, &tag2, &type2, (void**)&data2, &data_count2);
    394         EXPECT_EQ(OK, result);
    395         EXPECT_EQ(tag, tag2);
    396         EXPECT_EQ(type, type2);
    397         EXPECT_EQ(data_count, data_count2);
    398         for (unsigned int j=0; j < data_count; j++) {
    399             EXPECT_EQ(data[j], data2[j]);
    400         }
    401     }
    402 
    403     free(buf);
    404 
    405     free_camera_metadata(m);
    406 }
    407 
    408 TEST(camera_metadata, copy_metadata_extraspace) {
    409     camera_metadata_t *m = NULL;
    410     const size_t entry_capacity = 12;
    411     const size_t data_capacity = 100;
    412 
    413     const size_t extra_space = 10;
    414 
    415     int result;
    416 
    417     m = allocate_camera_metadata(entry_capacity, data_capacity);
    418 
    419     add_test_metadata(m, entry_capacity);
    420 
    421     size_t buf_size = get_camera_metadata_compact_size(m);
    422     EXPECT_LT((size_t)0, buf_size);
    423     buf_size += extra_space;
    424 
    425     uint8_t *buf = (uint8_t*)malloc(buf_size);
    426     EXPECT_NOT_NULL(buf);
    427 
    428     camera_metadata_t *m2 = copy_camera_metadata(buf, buf_size, m);
    429     EXPECT_NOT_NULL(m2);
    430     EXPECT_EQ(buf, (uint8_t*)m2);
    431     EXPECT_EQ(get_camera_metadata_entry_count(m),
    432             get_camera_metadata_entry_count(m2));
    433     EXPECT_EQ(get_camera_metadata_data_count(m),
    434             get_camera_metadata_data_count(m2));
    435     EXPECT_EQ(get_camera_metadata_entry_capacity(m2),
    436             get_camera_metadata_entry_count(m2));
    437     EXPECT_EQ(get_camera_metadata_data_capacity(m2),
    438             get_camera_metadata_data_count(m2));
    439     EXPECT_EQ(buf + buf_size - extra_space,
    440             (uint8_t*)m2 + get_camera_metadata_size(m2) );
    441 
    442     for (unsigned int i=0; i < get_camera_metadata_entry_count(m); i++) {
    443         uint32_t tag, tag2;
    444         uint8_t type, type2;
    445         uint8_t *data, *data2;
    446         size_t   data_count, data_count2;
    447 
    448         int result;
    449         result = get_camera_metadata_entry(m,
    450                 i, &tag, &type, (void**)&data, &data_count);
    451         EXPECT_EQ(OK, result);
    452         result = get_camera_metadata_entry(m2,
    453                 i, &tag2, &type2, (void**)&data2, &data_count2);
    454         EXPECT_EQ(OK, result);
    455         EXPECT_EQ(tag, tag2);
    456         EXPECT_EQ(type, type2);
    457         EXPECT_EQ(data_count, data_count2);
    458         for (unsigned int j=0; j < data_count; j++) {
    459             EXPECT_EQ(data[j], data2[j]);
    460         }
    461     }
    462 
    463     free(buf);
    464 
    465     free_camera_metadata(m);
    466 }
    467 
    468 TEST(camera_metadata, copy_metadata_nospace) {
    469     camera_metadata_t *m = NULL;
    470     const size_t entry_capacity = 5;
    471     const size_t data_capacity = 50;
    472 
    473     int result;
    474 
    475     m = allocate_camera_metadata(entry_capacity, data_capacity);
    476 
    477     add_test_metadata(m, entry_capacity);
    478 
    479     size_t buf_size = get_camera_metadata_compact_size(m);
    480     EXPECT_LT((size_t)0, buf_size);
    481 
    482     buf_size--;
    483 
    484     uint8_t *buf = (uint8_t*)malloc(buf_size);
    485     EXPECT_NOT_NULL(buf);
    486 
    487     camera_metadata_t *m2 = copy_camera_metadata(buf, buf_size, m);
    488     EXPECT_NULL(m2);
    489 
    490     free(buf);
    491 
    492     free_camera_metadata(m);
    493 }
    494 
    495 TEST(camera_metadata, append_metadata) {
    496     camera_metadata_t *m = NULL;
    497     const size_t entry_capacity = 5;
    498     const size_t data_capacity = 50;
    499 
    500     int result;
    501 
    502     m = allocate_camera_metadata(entry_capacity, data_capacity);
    503 
    504     add_test_metadata(m, entry_capacity);
    505 
    506     camera_metadata_t *m2 = NULL;
    507 
    508     m2 = allocate_camera_metadata(entry_capacity*2, data_capacity*2);
    509     EXPECT_NOT_NULL(m2);
    510 
    511     result = append_camera_metadata(m2, m);
    512 
    513     EXPECT_EQ(OK, result);
    514 
    515     EXPECT_EQ(get_camera_metadata_entry_count(m), get_camera_metadata_entry_count(m2));
    516     EXPECT_EQ(get_camera_metadata_data_count(m),  get_camera_metadata_data_count(m2));
    517     EXPECT_EQ(entry_capacity*2, get_camera_metadata_entry_capacity(m2));
    518     EXPECT_EQ(data_capacity*2,  get_camera_metadata_data_capacity(m2));
    519 
    520     for (unsigned int i=0; i < get_camera_metadata_entry_count(m); i++) {
    521         uint32_t tag, tag2;
    522         uint8_t type, type2;
    523         uint8_t *data, *data2;
    524         size_t   data_count, data_count2;
    525 
    526         int result;
    527         result = get_camera_metadata_entry(m,
    528                 i, &tag, &type, (void**)&data, &data_count);
    529         EXPECT_EQ(OK, result);
    530         result = get_camera_metadata_entry(m2,
    531                 i, &tag2, &type2, (void**)&data2, &data_count2);
    532         EXPECT_EQ(OK, result);
    533         EXPECT_EQ(tag, tag2);
    534         EXPECT_EQ(type, type2);
    535         EXPECT_EQ(data_count, data_count2);
    536         for (unsigned int j=0; j < data_count; j++) {
    537             EXPECT_EQ(data[j], data2[j]);
    538         }
    539     }
    540 
    541     result = append_camera_metadata(m2, m);
    542 
    543     EXPECT_EQ(OK, result);
    544 
    545     EXPECT_EQ(get_camera_metadata_entry_count(m)*2, get_camera_metadata_entry_count(m2));
    546     EXPECT_EQ(get_camera_metadata_data_count(m)*2,  get_camera_metadata_data_count(m2));
    547     EXPECT_EQ(entry_capacity*2, get_camera_metadata_entry_capacity(m2));
    548     EXPECT_EQ(data_capacity*2,  get_camera_metadata_data_capacity(m2));
    549 
    550     for (unsigned int i=0; i < get_camera_metadata_entry_count(m2); i++) {
    551         uint32_t tag, tag2;
    552         uint8_t type, type2;
    553         uint8_t *data, *data2;
    554         size_t   data_count, data_count2;
    555 
    556         int result;
    557         result = get_camera_metadata_entry(m,
    558                 i % entry_capacity, &tag, &type, (void**)&data, &data_count);
    559         EXPECT_EQ(OK, result);
    560         result = get_camera_metadata_entry(m2,
    561                 i, &tag2, &type2, (void**)&data2, &data_count2);
    562         EXPECT_EQ(OK, result);
    563         EXPECT_EQ(tag, tag2);
    564         EXPECT_EQ(type, type2);
    565         EXPECT_EQ(data_count, data_count2);
    566         for (unsigned int j=0; j < data_count; j++) {
    567             EXPECT_EQ(data[j], data2[j]);
    568         }
    569     }
    570 
    571     free_camera_metadata(m);
    572     free_camera_metadata(m2);
    573 }
    574 
    575 TEST(camera_metadata, append_metadata_nospace) {
    576     camera_metadata_t *m = NULL;
    577     const size_t entry_capacity = 5;
    578     const size_t data_capacity = 50;
    579 
    580     int result;
    581 
    582     m = allocate_camera_metadata(entry_capacity, data_capacity);
    583 
    584     add_test_metadata(m, entry_capacity);
    585 
    586     camera_metadata_t *m2 = NULL;
    587 
    588     m2 = allocate_camera_metadata(entry_capacity-1, data_capacity);
    589     EXPECT_NOT_NULL(m2);
    590 
    591     result = append_camera_metadata(m2, m);
    592 
    593     EXPECT_EQ(ERROR, result);
    594     EXPECT_EQ((size_t)0, get_camera_metadata_entry_count(m2));
    595     EXPECT_EQ((size_t)0, get_camera_metadata_data_count(m2));
    596 
    597     free_camera_metadata(m);
    598     free_camera_metadata(m2);
    599 }
    600 
    601 TEST(camera_metadata, append_metadata_onespace) {
    602     camera_metadata_t *m = NULL;
    603     const size_t entry_capacity = 5;
    604     const size_t data_capacity = 50;
    605     const size_t entry_capacity2 = entry_capacity * 2 - 2;
    606     const size_t data_capacity2 = data_capacity * 2;
    607     int result;
    608 
    609     m = allocate_camera_metadata(entry_capacity, data_capacity);
    610 
    611     add_test_metadata(m, entry_capacity);
    612 
    613     camera_metadata_t *m2 = NULL;
    614 
    615     m2 = allocate_camera_metadata(entry_capacity2, data_capacity2);
    616     EXPECT_NOT_NULL(m2);
    617 
    618     result = append_camera_metadata(m2, m);
    619 
    620     EXPECT_EQ(OK, result);
    621 
    622     EXPECT_EQ(get_camera_metadata_entry_count(m), get_camera_metadata_entry_count(m2));
    623     EXPECT_EQ(get_camera_metadata_data_count(m),  get_camera_metadata_data_count(m2));
    624     EXPECT_EQ(entry_capacity2, get_camera_metadata_entry_capacity(m2));
    625     EXPECT_EQ(data_capacity2,  get_camera_metadata_data_capacity(m2));
    626 
    627     for (unsigned int i=0; i < get_camera_metadata_entry_count(m); i++) {
    628         uint32_t tag, tag2;
    629         uint8_t type, type2;
    630         uint8_t *data, *data2;
    631         size_t   data_count, data_count2;
    632 
    633         int result;
    634         result = get_camera_metadata_entry(m,
    635                 i, &tag, &type, (void**)&data, &data_count);
    636         EXPECT_EQ(OK, result);
    637         result = get_camera_metadata_entry(m2,
    638                 i, &tag2, &type2, (void**)&data2, &data_count2);
    639         EXPECT_EQ(OK, result);
    640         EXPECT_EQ(tag, tag2);
    641         EXPECT_EQ(type, type2);
    642         EXPECT_EQ(data_count, data_count2);
    643         for (unsigned int j=0; j < data_count; j++) {
    644             EXPECT_EQ(data[j], data2[j]);
    645         }
    646     }
    647 
    648     result = append_camera_metadata(m2, m);
    649 
    650     EXPECT_EQ(ERROR, result);
    651     EXPECT_EQ(entry_capacity, get_camera_metadata_entry_count(m2));
    652     EXPECT_EQ(get_camera_metadata_data_count(m),
    653             get_camera_metadata_data_count(m2));
    654     EXPECT_EQ(entry_capacity2, get_camera_metadata_entry_capacity(m2));
    655     EXPECT_EQ(data_capacity2,  get_camera_metadata_data_capacity(m2));
    656 
    657     for (unsigned int i=0; i < get_camera_metadata_entry_count(m2); i++) {
    658         uint32_t tag, tag2;
    659         uint8_t type, type2;
    660         uint8_t *data, *data2;
    661         size_t   data_count, data_count2;
    662 
    663         int result;
    664         result = get_camera_metadata_entry(m,
    665                 i % entry_capacity, &tag, &type, (void**)&data, &data_count);
    666         EXPECT_EQ(OK, result);
    667         result = get_camera_metadata_entry(m2,
    668                 i, &tag2, &type2, (void**)&data2, &data_count2);
    669         EXPECT_EQ(OK, result);
    670         EXPECT_EQ(tag, tag2);
    671         EXPECT_EQ(type, type2);
    672         EXPECT_EQ(data_count, data_count2);
    673         for (unsigned int j=0; j < data_count; j++) {
    674             EXPECT_EQ(data[j], data2[j]);
    675         }
    676     }
    677 
    678     free_camera_metadata(m);
    679     free_camera_metadata(m2);
    680 }
    681 
    682 TEST(camera_metadata, vendor_tags) {
    683     camera_metadata_t *m = NULL;
    684     const size_t entry_capacity = 5;
    685     const size_t data_capacity = 50;
    686     int result;
    687 
    688     m = allocate_camera_metadata(entry_capacity, data_capacity);
    689 
    690     uint8_t superMode = 5;
    691     result = add_camera_metadata_entry(m,
    692             FAKEVENDOR_SENSOR_SUPERMODE,
    693             &superMode, 1);
    694     EXPECT_EQ(ERROR, result);
    695 
    696     result = add_camera_metadata_entry(m,
    697             ANDROID_REQUEST_METADATA_MODE,
    698             &superMode, 1);
    699     EXPECT_EQ(OK, result);
    700 
    701     EXPECT_NULL(get_camera_metadata_section_name(FAKEVENDOR_SENSOR_SUPERMODE));
    702     EXPECT_NULL(get_camera_metadata_tag_name(FAKEVENDOR_SENSOR_SUPERMODE));
    703     EXPECT_EQ(-1, get_camera_metadata_tag_type(FAKEVENDOR_SENSOR_SUPERMODE));
    704 
    705     set_camera_metadata_vendor_tag_ops(&fakevendor_query_ops);
    706 
    707     result = add_camera_metadata_entry(m,
    708             FAKEVENDOR_SENSOR_SUPERMODE,
    709             &superMode, 1);
    710     EXPECT_EQ(OK, result);
    711 
    712     result = add_camera_metadata_entry(m,
    713             ANDROID_REQUEST_METADATA_MODE,
    714             &superMode, 1);
    715     EXPECT_EQ(OK, result);
    716 
    717     result = add_camera_metadata_entry(m,
    718             FAKEVENDOR_SCALER_END,
    719             &superMode, 1);
    720     EXPECT_EQ(ERROR, result);
    721 
    722     EXPECT_STREQ("com.fakevendor.sensor",
    723             get_camera_metadata_section_name(FAKEVENDOR_SENSOR_SUPERMODE));
    724     EXPECT_STREQ("superMode",
    725             get_camera_metadata_tag_name(FAKEVENDOR_SENSOR_SUPERMODE));
    726     EXPECT_EQ(TYPE_BYTE,
    727             get_camera_metadata_tag_type(FAKEVENDOR_SENSOR_SUPERMODE));
    728 
    729     EXPECT_STREQ("com.fakevendor.scaler",
    730             get_camera_metadata_section_name(FAKEVENDOR_SCALER_END));
    731     EXPECT_NULL(get_camera_metadata_tag_name(FAKEVENDOR_SCALER_END));
    732     EXPECT_EQ(-1, get_camera_metadata_tag_type(FAKEVENDOR_SCALER_END));
    733 
    734     set_camera_metadata_vendor_tag_ops(NULL);
    735 
    736     result = add_camera_metadata_entry(m,
    737             FAKEVENDOR_SENSOR_SUPERMODE,
    738             &superMode, 1);
    739     EXPECT_EQ(ERROR, result);
    740 
    741     result = add_camera_metadata_entry(m,
    742             ANDROID_REQUEST_METADATA_MODE,
    743             &superMode, 1);
    744     EXPECT_EQ(OK, result);
    745 
    746     EXPECT_NULL(get_camera_metadata_section_name(FAKEVENDOR_SENSOR_SUPERMODE));
    747     EXPECT_NULL(get_camera_metadata_tag_name(FAKEVENDOR_SENSOR_SUPERMODE));
    748     EXPECT_EQ(-1, get_camera_metadata_tag_type(FAKEVENDOR_SENSOR_SUPERMODE));
    749 
    750     free_camera_metadata(m);
    751 }
    752 
    753 TEST(camera_metadata, add_all_tags) {
    754     int total_tag_count = 0;
    755     for (int i = 0; i < ANDROID_SECTION_COUNT; i++) {
    756         total_tag_count += camera_metadata_section_bounds[i][1] -
    757                 camera_metadata_section_bounds[i][0];
    758     }
    759     int entry_data_count = 3;
    760     int conservative_data_space = total_tag_count * entry_data_count * 8;
    761     uint8_t data[entry_data_count * 8];
    762     int32_t *data_int32 = (int32_t *)data;
    763     float *data_float   = (float *)data;
    764     int64_t *data_int64 = (int64_t *)data;
    765     double *data_double = (double *)data;
    766     camera_metadata_rational_t *data_rational = (camera_metadata_rational_t *)data;
    767 
    768     camera_metadata_t *m = allocate_camera_metadata(total_tag_count, conservative_data_space);
    769 
    770     ASSERT_NE((void*)NULL, (void*)m);
    771 
    772     int result;
    773 
    774     int counter = 0;
    775     for (int i = 0; i < ANDROID_SECTION_COUNT; i++) {
    776         for (uint32_t tag = camera_metadata_section_bounds[i][0];
    777                 tag < camera_metadata_section_bounds[i][1];
    778              tag++, counter++) {
    779             int type = get_camera_metadata_tag_type(tag);
    780             ASSERT_NE(-1, type);
    781 
    782             switch (type) {
    783                 case TYPE_BYTE:
    784                     data[0] = tag & 0xFF;
    785                     data[1] = (tag >> 8) & 0xFF;
    786                     data[2] = (tag >> 16) & 0xFF;
    787                     break;
    788                 case TYPE_INT32:
    789                     data_int32[0] = tag;
    790                     data_int32[1] = i;
    791                     data_int32[2] = counter;
    792                     break;
    793                 case TYPE_FLOAT:
    794                     data_float[0] = tag;
    795                     data_float[1] = i;
    796                     data_float[2] = counter / (float)total_tag_count;
    797                     break;
    798                 case TYPE_INT64:
    799                     data_int64[0] = (int64_t)tag | ( (int64_t)tag << 32);
    800                     data_int64[1] = i;
    801                     data_int64[2] = counter;
    802                     break;
    803                 case TYPE_DOUBLE:
    804                     data_double[0] = tag;
    805                     data_double[1] = i;
    806                     data_double[2] = counter / (double)total_tag_count;
    807                     break;
    808                 case TYPE_RATIONAL:
    809                     data_rational[0].numerator = tag;
    810                     data_rational[0].denominator = 1;
    811                     data_rational[1].numerator = i;
    812                     data_rational[1].denominator = 1;
    813                     data_rational[2].numerator = counter;
    814                     data_rational[2].denominator = total_tag_count;
    815                     break;
    816                 default:
    817                     FAIL() << "Unknown type field encountered:" << type;
    818                     break;
    819             }
    820             result = add_camera_metadata_entry(m,
    821                     tag,
    822                     data,
    823                     entry_data_count);
    824             ASSERT_EQ(OK, result);
    825 
    826         }
    827     }
    828 
    829     dump_camera_metadata(m, 0, 2);
    830 
    831     free_camera_metadata(m);
    832 }
    833 
    834 TEST(camera_metadata, sort_metadata) {
    835     camera_metadata_t *m = NULL;
    836     const size_t entry_capacity = 5;
    837     const size_t data_capacity = 100;
    838 
    839     int result;
    840 
    841     m = allocate_camera_metadata(entry_capacity, data_capacity);
    842 
    843     // Add several unique entries in non-sorted order
    844 
    845     float colorTransform[9] = {
    846         0.9f, 0.0f, 0.0f,
    847         0.2f, 0.5f, 0.0f,
    848         0.0f, 0.1f, 0.7f
    849     };
    850     result = add_camera_metadata_entry(m,
    851             ANDROID_COLOR_TRANSFORM,
    852             colorTransform, 9);
    853     EXPECT_EQ(OK, result);
    854 
    855     float focus_distance = 0.5f;
    856     result = add_camera_metadata_entry(m,
    857             ANDROID_LENS_FOCUS_DISTANCE,
    858             &focus_distance, 1);
    859     EXPECT_EQ(OK, result);
    860 
    861     int64_t exposure_time = 1000000000;
    862     result = add_camera_metadata_entry(m,
    863             ANDROID_SENSOR_EXPOSURE_TIME,
    864             &exposure_time, 1);
    865     EXPECT_EQ(OK, result);
    866 
    867     int32_t sensitivity = 800;
    868     result = add_camera_metadata_entry(m,
    869             ANDROID_SENSOR_SENSITIVITY,
    870             &sensitivity, 1);
    871     EXPECT_EQ(OK, result);
    872 
    873     // Test unsorted find
    874     uint8_t type;
    875     float  *f;
    876     size_t  data_count;
    877     result = find_camera_metadata_entry(m,
    878             ANDROID_LENS_FOCUS_DISTANCE,
    879             &type,
    880             (void**)&f,
    881             &data_count);
    882     EXPECT_EQ(OK, result);
    883     EXPECT_EQ(TYPE_FLOAT, type);
    884     EXPECT_EQ(1, (int)data_count);
    885     EXPECT_EQ(focus_distance, *f);
    886 
    887     result = find_camera_metadata_entry(m,
    888             ANDROID_NOISE_STRENGTH,
    889             &type,
    890             (void**)&f,
    891             &data_count);
    892     EXPECT_EQ(NOT_FOUND, result);
    893 
    894     // Sort
    895     std::cout << "Pre-sorted metadata" << std::endl;
    896     dump_camera_metadata(m, 0, 2);
    897 
    898     result = sort_camera_metadata(m);
    899     EXPECT_EQ(OK, result);
    900 
    901     std::cout << "Sorted metadata" << std::endl;
    902     dump_camera_metadata(m, 0, 2);
    903 
    904     // Test sorted find
    905 
    906     result = find_camera_metadata_entry(m,
    907             ANDROID_LENS_FOCUS_DISTANCE,
    908             &type,
    909             (void**)&f,
    910             &data_count);
    911     EXPECT_EQ(OK, result);
    912     EXPECT_EQ(TYPE_FLOAT, type);
    913     EXPECT_EQ(1, (int)data_count);
    914     EXPECT_EQ(focus_distance, *f);
    915 
    916     result = find_camera_metadata_entry(m,
    917             ANDROID_NOISE_STRENGTH,
    918             &type,
    919             (void**)&f,
    920             &data_count);
    921     EXPECT_EQ(NOT_FOUND, result);
    922 
    923 
    924     free_camera_metadata(m);
    925 }
    926