summaryrefslogtreecommitdiff
path: root/media/eco/tests/EcoDataTest.cpp
blob: f93b69273e32f7558098bafcecc02387aabb534e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

// Unit Test for EcoData.

//#define LOG_NDEBUG 0
#define LOG_TAG "ECODataTest"

#include <android-base/unique_fd.h>
#include <binder/Parcel.h>
#include <binder/Parcelable.h>
#include <cutils/ashmem.h>
#include <gtest/gtest.h>
#include <math.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <utils/Log.h>

#include "eco/ECOData.h"
#include "eco/ECODataKey.h"

namespace android {
namespace media {
namespace eco {

TEST(EcoDataTest, TestConstructor1) {
    std::unique_ptr<ECOData> data = std::make_unique<ECOData>();
    EXPECT_EQ(data->getDataType(), ECOData::DATA_TYPE_UNKNOWN);
    EXPECT_EQ(data->getDataTimeUs(), -1);
}

TEST(EcoDataTest, TestConstructor2) {
    std::unique_ptr<ECOData> data = std::make_unique<ECOData>(ECOData::DATA_TYPE_STATS);
    EXPECT_EQ(data->getDataType(), ECOData::DATA_TYPE_STATS);
    EXPECT_EQ(data->getDataTimeUs(), -1);
}

TEST(EcoDataTest, TestConstructor3) {
    std::unique_ptr<ECOData> data = std::make_unique<ECOData>(ECOData::DATA_TYPE_STATS, 1000);
    EXPECT_EQ(data->getDataType(), ECOData::DATA_TYPE_STATS);
    EXPECT_EQ(data->getDataTimeUs(), 1000);
}

TEST(EcoDataTest, TestNormalSetAndFindString) {
    std::unique_ptr<ECOData> data = std::make_unique<ECOData>(ECOData::DATA_TYPE_STATS, 1000);

    data->setString(ENCODER_TYPE, "avc");
    std::string testValue;
    EXPECT_TRUE(data->findString(ENCODER_TYPE, &testValue) == ECODataStatus::OK);
    EXPECT_EQ(testValue, "avc");

    // Override existing key.
    data->setString(ENCODER_TYPE, "hevc");
    EXPECT_EQ(data->findString(ENCODER_TYPE, &testValue), ECODataStatus::OK);
    EXPECT_EQ(testValue, "hevc");
}

TEST(EcoDataTest, TestSetAndFindMultipleString) {
    std::unique_ptr<ECOData> data = std::make_unique<ECOData>(ECOData::DATA_TYPE_STATS, 1000);

    std::unordered_map<std::string, std::string> inputEntries = {
            {"name1", "avc"},  {"name2", "avc2"},   {"name3", "avc3"},   {"name4", "avc4"},
            {"name5", "avc5"}, {"name6", "avc6"},   {"name7", "avc7"},   {"name8", "avc8"},
            {"name9", "avc9"}, {"name10", "avc10"}, {"name11", "avc11"}, {"name12", "avc12"}};
    for (auto it = inputEntries.begin(); it != inputEntries.end(); ++it) {
        data->setString(it->first, it->second);
    }

    // Checks if the string exist in the ECOData.
    for (auto it = inputEntries.begin(); it != inputEntries.end(); ++it) {
        std::string testValue;
        EXPECT_TRUE(data->findString(it->first, &testValue) == ECODataStatus::OK);
        EXPECT_EQ(testValue, it->second);
    }
}

TEST(EcoDataTest, TestSetAndFindInvalidString) {
    std::unique_ptr<ECOData> data = std::make_unique<ECOData>(ECOData::DATA_TYPE_STATS, 1000);

    // Test read to null ptr and expect failure
    EXPECT_TRUE(data->findString("encoder-name", nullptr) != ECODataStatus::OK);

    // Test find non-existing key and expect failure.
    std::string testValue;
    EXPECT_TRUE(data->findString("encoder-name", &testValue) != ECODataStatus::OK);

    // Test set empty key and expect failure
    EXPECT_TRUE(data->setString("", "avc") != ECODataStatus::OK);

    // Test read empty key and expect failure
    EXPECT_TRUE(data->findString("", &testValue) != ECODataStatus::OK);
}

TEST(EcoDataTest, TestNormalSetAndFindInt32) {
    std::unique_ptr<ECOData> data = std::make_unique<ECOData>(ECOData::DATA_TYPE_STATS, 1000);

    data->setInt32(ENCODER_TARGET_BITRATE_BPS, 2000000);
    int32_t testValue;
    EXPECT_TRUE(data->findInt32(ENCODER_TARGET_BITRATE_BPS, &testValue) == ECODataStatus::OK);
    EXPECT_EQ(testValue, 2000000);

    // Override existing key.
    data->setInt32(ENCODER_TARGET_BITRATE_BPS, 2200000);
    EXPECT_EQ(data->findInt32(ENCODER_TARGET_BITRATE_BPS, &testValue), ECODataStatus::OK);
    EXPECT_EQ(testValue, 2200000);
}

TEST(EcoDataTest, TestSetAndFindMultipleInt32) {
    std::unique_ptr<ECOData> data = std::make_unique<ECOData>(ECOData::DATA_TYPE_STATS, 1000);

    std::unordered_map<std::string, int32_t> inputEntries = {
            {"name1", 100}, {"name2", 200},    {"name3", 300},     {"name4", 400},
            {"name5", 500}, {"name6", 600},    {"name7", 700},     {"name8", 800},
            {"name9", 900}, {"name10", 10000}, {"name11", 110000}, {"name12", 120000}};
    for (auto it = inputEntries.begin(); it != inputEntries.end(); ++it) {
        data->setInt32(it->first, it->second);
    }

    // Checks if the string exist in the ECOData.
    for (auto it = inputEntries.begin(); it != inputEntries.end(); ++it) {
        int32_t testValue;
        EXPECT_TRUE(data->findInt32(it->first, &testValue) == ECODataStatus::OK);
        EXPECT_EQ(testValue, it->second);
    }
}

TEST(EcoDataTest, TestSetAndFindInvalidInt32) {
    std::unique_ptr<ECOData> data = std::make_unique<ECOData>(ECOData::DATA_TYPE_STATS, 1000);

    // Test read to null ptr and expect failure
    EXPECT_TRUE(data->findInt32("encoder-name", nullptr) != ECODataStatus::OK);

    // Test find non-existing key and expect failure.
    int32_t testValue;
    EXPECT_TRUE(data->findInt32("encoder-name", &testValue) != ECODataStatus::OK);

    // Test set empty key and expect failure
    EXPECT_TRUE(data->setInt32("", 1000) != ECODataStatus::OK);

    // Test read empty key and expect failure
    EXPECT_TRUE(data->findInt32("", &testValue) != ECODataStatus::OK);
}

TEST(EcoDataTest, TestNormalSetAndFindInt64) {
    std::unique_ptr<ECOData> data = std::make_unique<ECOData>(ECOData::DATA_TYPE_STATS, 1000);

    data->setInt64(ENCODER_TARGET_BITRATE_BPS, 2000000);
    int64_t testValue;
    EXPECT_TRUE(data->findInt64(ENCODER_TARGET_BITRATE_BPS, &testValue) == ECODataStatus::OK);
    EXPECT_EQ(testValue, 2000000);

    // Override existing key.
    data->setInt64(ENCODER_TARGET_BITRATE_BPS, 2200000);
    EXPECT_EQ(data->findInt64(ENCODER_TARGET_BITRATE_BPS, &testValue), ECODataStatus::OK);
    EXPECT_EQ(testValue, 2200000);
}

TEST(EcoDataTest, TestNormalSetAndFindMultipleInt64) {
    std::unique_ptr<ECOData> data = std::make_unique<ECOData>(ECOData::DATA_TYPE_STATS, 1000);

    std::unordered_map<std::string, int64_t> inputEntries = {
            {"name1", 100}, {"name2", 200},    {"name3", 300},     {"name4", 400},
            {"name5", 500}, {"name6", 600},    {"name7", 700},     {"name8", 800},
            {"name9", 900}, {"name10", 10000}, {"name11", 110000}, {"name12", 120000}};
    for (auto it = inputEntries.begin(); it != inputEntries.end(); ++it) {
        data->setInt64(it->first, it->second);
    }

    // Checks if the string exist in the ECOData.
    for (auto it = inputEntries.begin(); it != inputEntries.end(); ++it) {
        int64_t testValue;
        EXPECT_TRUE(data->findInt64(it->first, &testValue) == ECODataStatus::OK);
        EXPECT_EQ(testValue, it->second);
    }
}

TEST(EcoDataTest, TestSetAndFindInvalidInt64) {
    std::unique_ptr<ECOData> data = std::make_unique<ECOData>(ECOData::DATA_TYPE_STATS, 1000);

    // Test read to null ptr and expect failure
    EXPECT_TRUE(data->findInt64("encoder-name", nullptr) != ECODataStatus::OK);

    // Test find non-existing key and expect failure.
    int64_t testValue;
    EXPECT_TRUE(data->findInt64("encoder-name", &testValue) != ECODataStatus::OK);

    // Test set empty key and expect failure
    EXPECT_TRUE(data->setInt64("", 1000) != ECODataStatus::OK);

    // Test read empty key and expect failure
    EXPECT_TRUE(data->findInt64("", &testValue) != ECODataStatus::OK);
}

TEST(EcoDataTest, TestNormalSetAndFindFloat) {
    std::unique_ptr<ECOData> data = std::make_unique<ECOData>(ECOData::DATA_TYPE_STATS, 1000);

    data->setFloat(ENCODER_TARGET_BITRATE_BPS, 2000000.0);
    float testValue;
    EXPECT_TRUE(data->findFloat(ENCODER_TARGET_BITRATE_BPS, &testValue) == ECODataStatus::OK);
    EXPECT_FLOAT_EQ(testValue, 2000000.0);

    // Override existing key.
    data->setFloat(ENCODER_TARGET_BITRATE_BPS, 2200000.0);
    EXPECT_TRUE(data->findFloat(ENCODER_TARGET_BITRATE_BPS, &testValue) == ECODataStatus::OK);
    EXPECT_FLOAT_EQ(testValue, 2200000.0);
}

TEST(EcoDataTest, TestNormalSetAndFindMultipleFloat) {
    std::unique_ptr<ECOData> data = std::make_unique<ECOData>(ECOData::DATA_TYPE_STATS, 1000);

    std::unordered_map<std::string, float> inputEntries = {
            {"name1", 100.0}, {"name2", 200.0},    {"name3", 300.0},     {"name4", 400.0},
            {"name5", 500.0}, {"name6", 600.0},    {"name7", 700.0},     {"name8", 800.0},
            {"name9", 900.0}, {"name10", 10000.0}, {"name11", 110000.0}, {"name12", 120000.0}};
    for (auto it = inputEntries.begin(); it != inputEntries.end(); ++it) {
        data->setFloat(it->first, it->second);
    }

    // Checks if the string exist in the ECOData.
    for (auto it = inputEntries.begin(); it != inputEntries.end(); ++it) {
        float testValue;
        EXPECT_TRUE(data->findFloat(it->first, &testValue) == ECODataStatus::OK);
        EXPECT_FLOAT_EQ(testValue, it->second);
    }
}

TEST(EcoDataTest, TestSetAndFindInvalidFloat) {
    std::unique_ptr<ECOData> data = std::make_unique<ECOData>(ECOData::DATA_TYPE_STATS, 1000);

    // Test read to null ptr and expect failure
    EXPECT_TRUE(data->findFloat("encoder-name", nullptr) != ECODataStatus::OK);

    // Test find non-existing key and expect failure.
    float testValue;
    EXPECT_TRUE(data->findFloat("encoder-name", &testValue) != ECODataStatus::OK);

    // Test set empty key and expect failure
    EXPECT_TRUE(data->setFloat("", 1000.0) != ECODataStatus::OK);

    // Test read empty key and expect failure
    EXPECT_TRUE(data->findFloat("", &testValue) != ECODataStatus::OK);
}

TEST(EcoDataTest, TestNormalSetAndFindMixedDataType) {
    std::unique_ptr<ECOData> data = std::make_unique<ECOData>(ECOData::DATA_TYPE_STATS, 1000);

    std::unordered_map<std::string, ECOData::ECODataValueType> inputEntries = {
            {"name1", "google-encoder"}, {"name2", "avc"}, {"profile", 1}, {"level", 2},
            {"framerate", 4.1},          {"kfi", 30}};
    for (auto it = inputEntries.begin(); it != inputEntries.end(); ++it) {
        data->set(it->first, it->second);
    }

    // Checks if the string exist in the ECOData.
    for (auto it = inputEntries.begin(); it != inputEntries.end(); ++it) {
        ECOData::ECODataValueType testValue;
        EXPECT_TRUE(data->find(it->first, &testValue) == ECODataStatus::OK);
        EXPECT_EQ(testValue, it->second);
    }
}

TEST(EcoDataTest, TestSetAndFindInvalidDataType) {
    std::unique_ptr<ECOData> data = std::make_unique<ECOData>(ECOData::DATA_TYPE_STATS, 1000);

    // Test read to null ptr and expect failure
    EXPECT_TRUE(data->find("encoder-name", nullptr) != ECODataStatus::OK);

    // Test find non-existing key and expect failure.
    ECOData::ECODataValueType testValue;
    EXPECT_TRUE(data->find("encoder-name2", &testValue) != ECODataStatus::OK);

    // Test set empty key and expect failure
    EXPECT_TRUE(data->set("", 1000) != ECODataStatus::OK);

    // Test read empty key and expect failure
    EXPECT_TRUE(data->find("", &testValue) != ECODataStatus::OK);
}

TEST(EcoDataTest, TestNormalWriteReadParcel) {
    constexpr int32_t kDataType = ECOData::DATA_TYPE_STATS;
    constexpr int64_t kDataTimeUs = 1000;

    std::unique_ptr<ECOData> sourceData = std::make_unique<ECOData>(kDataType, kDataTimeUs);

    std::unordered_map<std::string, ECOData::ECODataValueType> inputEntries = {
            {"name1", "google-encoder"}, {"name2", "avc"}, {"profile", 1}, {"level", 2},
            {"framerate", 4.1},          {"kfi", 30}};
    for (auto it = inputEntries.begin(); it != inputEntries.end(); ++it) {
        sourceData->set(it->first, it->second);
    }

    std::unique_ptr<Parcel> parcel = std::make_unique<Parcel>();
    EXPECT_TRUE(sourceData->writeToParcel(parcel.get()) == NO_ERROR);

    // Rewind the data position of the parcel for this test. Otherwise, the following read will not
    // start from the beginning.
    parcel->setDataPosition(0);

    // Reads the parcel back into a new ECOData
    std::unique_ptr<ECOData> dstData = std::make_unique<ECOData>();
    EXPECT_TRUE(dstData->readFromParcel(parcel.get()) == NO_ERROR);

    // Checks the data type, time and number of entries.
    EXPECT_EQ(sourceData->getNumOfEntries(), dstData->getNumOfEntries());
    EXPECT_EQ(dstData->getDataType(), kDataType);
    EXPECT_EQ(dstData->getDataTimeUs(), kDataTimeUs);

    for (auto it = inputEntries.begin(); it != inputEntries.end(); ++it) {
        ECOData::ECODataValueType testValue;
        EXPECT_TRUE(dstData->find(it->first, &testValue) == ECODataStatus::OK);
        EXPECT_EQ(testValue, it->second);
    }
}

TEST(EcoDataTest, TestWriteInvalidParcel) {
    constexpr int32_t kDataType = ECOData::DATA_TYPE_STATS;
    constexpr int64_t kDataTimeUs = 1000;

    std::unique_ptr<ECOData> sourceData = std::make_unique<ECOData>(kDataType, kDataTimeUs);

    std::unique_ptr<Parcel> parcel = std::make_unique<Parcel>();
    EXPECT_TRUE(sourceData->writeToParcel(nullptr) != NO_ERROR);
}

TEST(EcoDataTest, TestReadInvalidParcel) {
    constexpr int32_t kDataType = ECOData::DATA_TYPE_STATS;
    constexpr int64_t kDataTimeUs = 1000;

    std::unique_ptr<ECOData> sourceData = std::make_unique<ECOData>(kDataType, kDataTimeUs);

    std::unordered_map<std::string, ECOData::ECODataValueType> inputEntries = {
            {"name1", "google-encoder"}, {"name2", "avc"}, {"profile", 1}, {"level", 2},
            {"framerate", 4.1},          {"kfi", 30}};
    for (auto it = inputEntries.begin(); it != inputEntries.end(); ++it) {
        sourceData->set(it->first, it->second);
    }

    std::unique_ptr<Parcel> parcel = std::make_unique<Parcel>();
    EXPECT_TRUE(sourceData->writeToParcel(parcel.get()) == NO_ERROR);

    // Corrupt the parcel by write random data to the beginning.
    parcel->setDataPosition(4);
    parcel->writeCString("invalid-data");

    parcel->setDataPosition(0);

    // Reads the parcel back into a new ECOData
    std::unique_ptr<ECOData> dstData = std::make_unique<ECOData>();
    EXPECT_TRUE(dstData->readFromParcel(parcel.get()) != NO_ERROR);
}

}  // namespace eco
}  // namespace media
}  // namespace android