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 // verify detecting some bad parameters.
55 AMediaFormat_setInt32(nullptr, "whatever", 6);
56 AMediaFormat_setInt32(fmt1, nullptr, 6);
57
58 EXPECT_FALSE(AMediaFormat_getInt32(nullptr, "whatever", &i32));
59 EXPECT_FALSE(AMediaFormat_getInt32(fmt1, nullptr, &i32));
60
61 AMediaFormat_delete(fmt1);
62 }
63
TEST(NdkMediaFormat_tests,test_int64)64 TEST(NdkMediaFormat_tests, test_int64) {
65 AMediaFormat *fmt1 = AMediaFormat_new();
66 int64_t i64;
67 AMediaFormat_setInt64(fmt1, "verylarge", INT64_MAX);
68
69 EXPECT_TRUE(AMediaFormat_getInt64(fmt1, "verylarge", &i64));
70 EXPECT_EQ(i64, INT64_MAX);
71
72 // return unchanged if not found
73 i64 = -1;
74 EXPECT_FALSE(AMediaFormat_getInt64(fmt1, "five", &i64));
75 EXPECT_EQ(i64, -1);
76
77 // verify detecting some bad parameters.
78 AMediaFormat_setInt64(nullptr, "whatever", 6);
79 AMediaFormat_setInt64(fmt1, nullptr, 6);
80
81 EXPECT_FALSE(AMediaFormat_getInt64(nullptr, "whatever", &i64));
82 EXPECT_FALSE(AMediaFormat_getInt64(fmt1, nullptr, &i64));
83
84 AMediaFormat_delete(fmt1);
85 }
86
TEST(NdkMediaFormat_tests,test_size)87 TEST(NdkMediaFormat_tests, test_size) {
88 AMediaFormat *fmt1 = AMediaFormat_new();
89
90 size_t size = -15;
91 AMediaFormat_setSize(fmt1, "small", 1);
92 AMediaFormat_setSize(fmt1, "medium", 10);
93 AMediaFormat_setSize(fmt1, "large", 100);
94 EXPECT_TRUE(AMediaFormat_getSize(fmt1, "medium", &size));
95 EXPECT_EQ(size, 10);
96
97 // verify detecting some bad parameters.
98 AMediaFormat_setSize(nullptr, "whatever", 6);
99 AMediaFormat_setSize(fmt1, nullptr, 6);
100
101 EXPECT_FALSE(AMediaFormat_getSize(nullptr, "whatever", &size));
102 EXPECT_FALSE(AMediaFormat_getSize(fmt1, nullptr, &size));
103
104 AMediaFormat_delete(fmt1);
105 }
106
TEST(NdkMediaFormat_tests,test_float)107 TEST(NdkMediaFormat_tests, test_float) {
108 AMediaFormat *fmt1 = AMediaFormat_new();
109 float f;
110 AMediaFormat_setFloat(fmt1, "boat", 1.5);
111 AMediaFormat_setFloat(fmt1, "ship", 0.5);
112 EXPECT_TRUE(AMediaFormat_getFloat(fmt1, "boat", &f));
113 EXPECT_EQ(f, 1.5);
114
115 // verify detecting some bad parameters.
116 AMediaFormat_setFloat(nullptr, "whatever", 1.5);
117 AMediaFormat_setFloat(fmt1, nullptr, 1.5);
118
119 EXPECT_FALSE(AMediaFormat_getFloat(nullptr, "whatever", &f));
120 EXPECT_FALSE(AMediaFormat_getFloat(fmt1, nullptr, &f));
121
122 AMediaFormat_delete(fmt1);
123 }
124
TEST(NdkMediaFormat_tests,test_double)125 TEST(NdkMediaFormat_tests, test_double) {
126 AMediaFormat *fmt1 = AMediaFormat_new();
127 double d;
128 AMediaFormat_setDouble(fmt1, "trouble", 100.5);
129 AMediaFormat_setDouble(fmt1, "dip", 0.5);
130 EXPECT_TRUE(AMediaFormat_getDouble(fmt1, "trouble", &d));
131 EXPECT_EQ(d, 100.5);
132
133 // verify detecting some bad parameters.
134 AMediaFormat_setDouble(nullptr, "whatever", 1.5);
135 AMediaFormat_setDouble(fmt1, nullptr, 1.5);
136
137 EXPECT_FALSE(AMediaFormat_getDouble(nullptr, "whatever", &d));
138 EXPECT_FALSE(AMediaFormat_getDouble(fmt1, nullptr, &d));
139
140 AMediaFormat_delete(fmt1);
141 }
142
TEST(NdkMediaFormat_tests,test_string)143 TEST(NdkMediaFormat_tests, test_string) {
144 AMediaFormat *fmt1 = AMediaFormat_new();
145
146 const char *content = "This is my test string";
147 const char *out = nullptr;
148 AMediaFormat_setString(fmt1, "stringtheory", content);
149 EXPECT_TRUE(AMediaFormat_getString(fmt1, "stringtheory", &out));
150 EXPECT_NE(out, nullptr);
151 EXPECT_NE(out, content); // should not be the original
152 EXPECT_EQ(strcmp(out,content), 0);
153
154 // verify detecting some bad parameters.
155 AMediaFormat_setString(nullptr, "whatever", content);
156 AMediaFormat_setString(fmt1, nullptr, content);
157
158 EXPECT_FALSE(AMediaFormat_getString(nullptr, "whatever", &out));
159 EXPECT_FALSE(AMediaFormat_getString(fmt1, nullptr, &out));
160
161 AMediaFormat_delete(fmt1);
162 }
163
164
TEST(NdkMediaFormat_tests,test_clear)165 TEST(NdkMediaFormat_tests, test_clear) {
166 AMediaFormat *fmt1 = AMediaFormat_new();
167
168 int32_t i32;
169 AMediaFormat_setInt32(fmt1, "five", 5);
170 size_t size = -15;
171 AMediaFormat_setSize(fmt1, "medium", 10);
172 float f;
173 AMediaFormat_setFloat(fmt1, "boat", 1.5);
174
175 AMediaFormat_clear(fmt1);
176 EXPECT_FALSE(AMediaFormat_getInt32(fmt1, "five", &i32));
177 EXPECT_FALSE(AMediaFormat_getSize(fmt1, "medium", &size));
178 EXPECT_FALSE(AMediaFormat_getFloat(fmt1, "boat", &f));
179
180 AMediaFormat_delete(fmt1);
181 }
182
TEST(NdkMediaFormat_tests,test_copy)183 TEST(NdkMediaFormat_tests, test_copy) {
184 AMediaFormat *fmt1 = AMediaFormat_new();
185 AMediaFormat *fmt2 = AMediaFormat_new();
186
187 double d;
188 int32_t i32;
189
190 // test copy functionality (NB: we cleared everything just above here)
191 AMediaFormat_setDouble(fmt1, "trouble", 100.5);
192 EXPECT_TRUE(AMediaFormat_getDouble(fmt1, "trouble", &d));
193 EXPECT_FALSE(AMediaFormat_getDouble(fmt2, "trouble", &d));
194
195 EXPECT_EQ(AMEDIA_OK, AMediaFormat_copy(fmt2, fmt1));
196
197 EXPECT_TRUE(AMediaFormat_getDouble(fmt2, "trouble", &d));
198 EXPECT_EQ(d, 100.5);
199
200 AMediaFormat *fmt3 = nullptr;
201 EXPECT_NE(AMEDIA_OK, AMediaFormat_copy(fmt3, fmt1));
202 EXPECT_NE(AMEDIA_OK, AMediaFormat_copy(fmt1, fmt3));
203
204 // we should lose an entry when we copy over it
205 AMediaFormat_setInt32(fmt2, "vanishing", 50);
206 EXPECT_FALSE(AMediaFormat_getInt32(fmt1, "vanishing", &i32));
207 EXPECT_TRUE(AMediaFormat_getInt32(fmt2, "vanishing", &i32));
208 EXPECT_EQ(AMEDIA_OK, AMediaFormat_copy(fmt2, fmt1));
209 EXPECT_FALSE(AMediaFormat_getInt32(fmt2, "vanishing", &i32));
210
211 AMediaFormat_delete(fmt1);
212 AMediaFormat_delete(fmt2);
213 }
214
TEST(NdkMediaFormat_tests,test_buffer)215 TEST(NdkMediaFormat_tests, test_buffer) {
216 AMediaFormat *fmt1 = AMediaFormat_new();
217
218 typedef struct blockomem {
219 int leading;
220 int filled[100];
221 int trailing;
222 } block_t;
223 block_t buf = {};
224 buf.leading = 1;
225 buf.trailing = 2;
226 void *data;
227 size_t bsize;
228
229 AMediaFormat_setBuffer(fmt1, "mybuffer", &buf, sizeof(buf));
230 EXPECT_TRUE(AMediaFormat_getBuffer(fmt1, "mybuffer", &data, &bsize));
231 EXPECT_NE(&buf, data);
232 EXPECT_EQ(sizeof(buf), bsize);
233 block_t *bufp = (block_t*) data;
234 EXPECT_EQ(bufp->leading, buf.leading);
235 EXPECT_EQ(bufp->trailing, buf.trailing);
236 EXPECT_EQ(0, memcmp(&buf, data, bsize));
237
238 AMediaFormat_delete(fmt1);
239 }
240
241 } // namespace android
242