1 /*
2 * Copyright 2021 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 "NdkMediaFormat_test"
19
20 #include <gtest/gtest.h>
21 #include <utils/RefBase.h>
22
23 #include <media/NdkMediaFormat.h>
24
25 namespace android {
26
27 class NdkMediaFormatTest : public ::testing::Test {
28 };
29
30
TEST(NdkMediaFormat_tests,test_create)31 TEST(NdkMediaFormat_tests, test_create) {
32
33 AMediaFormat *fmt1 = AMediaFormat_new();
34 AMediaFormat *fmt2 = AMediaFormat_new();
35
36 EXPECT_NE(fmt1, fmt2);
37 EXPECT_NE(fmt1, nullptr);
38 EXPECT_NE(fmt2, nullptr);
39
40 AMediaFormat_delete(fmt1);
41 AMediaFormat_delete(fmt2);
42 }
43
TEST(NdkMediaFormat_tests,test_int32)44 TEST(NdkMediaFormat_tests, test_int32) {
45 AMediaFormat *fmt1 = AMediaFormat_new();
46 int32_t i32;
47 int64_t i64;
48 AMediaFormat_setInt32(fmt1, "five", 5);
49
50 EXPECT_TRUE(AMediaFormat_getInt32(fmt1, "five", &i32));
51 EXPECT_FALSE(AMediaFormat_getInt64(fmt1, "five", &i64));
52 EXPECT_EQ(i32, 5);
53
54 AMediaFormat_delete(fmt1);
55 }
56
TEST(NdkMediaFormat_tests,test_int64)57 TEST(NdkMediaFormat_tests, test_int64) {
58 AMediaFormat *fmt1 = AMediaFormat_new();
59 int64_t i64;
60 AMediaFormat_setInt64(fmt1, "verylarge", INT64_MAX);
61
62 EXPECT_TRUE(AMediaFormat_getInt64(fmt1, "verylarge", &i64));
63 EXPECT_EQ(i64, INT64_MAX);
64
65 // return unchanged if not found
66 i64 = -1;
67 EXPECT_FALSE(AMediaFormat_getInt64(fmt1, "five", &i64));
68 EXPECT_EQ(i64, -1);
69
70 AMediaFormat_delete(fmt1);
71 }
72
TEST(NdkMediaFormat_tests,test_size)73 TEST(NdkMediaFormat_tests, test_size) {
74 AMediaFormat *fmt1 = AMediaFormat_new();
75
76 size_t size = -15;
77 AMediaFormat_setSize(fmt1, "small", 1);
78 AMediaFormat_setSize(fmt1, "medium", 10);
79 AMediaFormat_setSize(fmt1, "large", 100);
80 EXPECT_TRUE(AMediaFormat_getSize(fmt1, "medium", &size));
81 EXPECT_EQ(size, 10);
82
83 AMediaFormat_delete(fmt1);
84 }
85
TEST(NdkMediaFormat_tests,test_float)86 TEST(NdkMediaFormat_tests, test_float) {
87 AMediaFormat *fmt1 = AMediaFormat_new();
88 float f;
89 AMediaFormat_setFloat(fmt1, "boat", 1.5);
90 AMediaFormat_setFloat(fmt1, "ship", 0.5);
91 EXPECT_TRUE(AMediaFormat_getFloat(fmt1, "boat", &f));
92 EXPECT_EQ(f, 1.5);
93 AMediaFormat_delete(fmt1);
94 }
95
TEST(NdkMediaFormat_tests,test_double)96 TEST(NdkMediaFormat_tests, test_double) {
97 AMediaFormat *fmt1 = AMediaFormat_new();
98 double d;
99 AMediaFormat_setDouble(fmt1, "trouble", 100.5);
100 AMediaFormat_setDouble(fmt1, "dip", 0.5);
101 EXPECT_TRUE(AMediaFormat_getDouble(fmt1, "trouble", &d));
102 EXPECT_EQ(d, 100.5);
103 AMediaFormat_delete(fmt1);
104 }
105
TEST(NdkMediaFormat_tests,test_string)106 TEST(NdkMediaFormat_tests, test_string) {
107 AMediaFormat *fmt1 = AMediaFormat_new();
108
109 const char *content = "This is my test string";
110 const char *out = nullptr;
111 AMediaFormat_setString(fmt1, "stringtheory", content);
112 EXPECT_TRUE(AMediaFormat_getString(fmt1, "stringtheory", &out));
113 EXPECT_NE(out, nullptr);
114 EXPECT_EQ(strcmp(out,content), 0);
115
116 AMediaFormat_delete(fmt1);
117 }
118
119
TEST(NdkMediaFormat_tests,test_clear)120 TEST(NdkMediaFormat_tests, test_clear) {
121 AMediaFormat *fmt1 = AMediaFormat_new();
122
123 int32_t i32;
124 AMediaFormat_setInt32(fmt1, "five", 5);
125 size_t size = -15;
126 AMediaFormat_setSize(fmt1, "medium", 10);
127 float f;
128 AMediaFormat_setFloat(fmt1, "boat", 1.5);
129
130 AMediaFormat_clear(fmt1);
131 EXPECT_FALSE(AMediaFormat_getInt32(fmt1, "five", &i32));
132 EXPECT_FALSE(AMediaFormat_getSize(fmt1, "medium", &size));
133 EXPECT_FALSE(AMediaFormat_getFloat(fmt1, "boat", &f));
134
135 AMediaFormat_delete(fmt1);
136 }
137
TEST(NdkMediaFormat_tests,test_copy)138 TEST(NdkMediaFormat_tests, test_copy) {
139 AMediaFormat *fmt1 = AMediaFormat_new();
140 AMediaFormat *fmt2 = AMediaFormat_new();
141
142 double d;
143 int32_t i32;
144
145 // test copy functionality (NB: we cleared everything just above here)
146 AMediaFormat_setDouble(fmt1, "trouble", 100.5);
147 EXPECT_TRUE(AMediaFormat_getDouble(fmt1, "trouble", &d));
148 EXPECT_FALSE(AMediaFormat_getDouble(fmt2, "trouble", &d));
149
150 EXPECT_EQ(AMEDIA_OK, AMediaFormat_copy(fmt2, fmt1));
151
152 EXPECT_TRUE(AMediaFormat_getDouble(fmt2, "trouble", &d));
153 EXPECT_EQ(d, 100.5);
154
155 AMediaFormat *fmt3 = nullptr;
156 EXPECT_NE(AMEDIA_OK, AMediaFormat_copy(fmt3, fmt1));
157 EXPECT_NE(AMEDIA_OK, AMediaFormat_copy(fmt1, fmt3));
158
159 // we should lose an entry when we copy over it
160 AMediaFormat_setInt32(fmt2, "vanishing", 50);
161 EXPECT_FALSE(AMediaFormat_getInt32(fmt1, "vanishing", &i32));
162 EXPECT_TRUE(AMediaFormat_getInt32(fmt2, "vanishing", &i32));
163 EXPECT_EQ(AMEDIA_OK, AMediaFormat_copy(fmt2, fmt1));
164 EXPECT_FALSE(AMediaFormat_getInt32(fmt2, "vanishing", &i32));
165
166 AMediaFormat_delete(fmt1);
167 AMediaFormat_delete(fmt2);
168 }
169
TEST(NdkMediaFormat_tests,test_buffer)170 TEST(NdkMediaFormat_tests, test_buffer) {
171 AMediaFormat *fmt1 = AMediaFormat_new();
172
173 typedef struct blockomem {
174 int leading;
175 int filled[100];
176 int trailing;
177 } block_t;
178 block_t buf = {};
179 buf.leading = 1;
180 buf.trailing = 2;
181 void *data;
182 size_t bsize;
183
184 AMediaFormat_setBuffer(fmt1, "mybuffer", &buf, sizeof(buf));
185 EXPECT_TRUE(AMediaFormat_getBuffer(fmt1, "mybuffer", &data, &bsize));
186 EXPECT_NE(&buf, data);
187 EXPECT_EQ(sizeof(buf), bsize);
188 block_t *bufp = (block_t*) data;
189 EXPECT_EQ(bufp->leading, buf.leading);
190 EXPECT_EQ(bufp->trailing, buf.trailing);
191 EXPECT_EQ(0, memcmp(&buf, data, bsize));
192
193 AMediaFormat_delete(fmt1);
194 }
195
196 } // namespace android
197