1 /*
2 * Copyright (C) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16
17 #include "format_unit_test.h"
18 #include <cmath>
19 #include "gtest/gtest.h"
20 #include "media_errors.h"
21 #include "securec.h"
22
23 using namespace std;
24 using namespace OHOS;
25 using namespace OHOS::Media;
26 using namespace testing::ext;
27 using namespace OHOS::Media::FormatTestParam;
28
SetUpTestCase(void)29 void FormatUnitTest::SetUpTestCase(void) {}
30
TearDownTestCase(void)31 void FormatUnitTest::TearDownTestCase(void) {}
32
SetUp(void)33 void FormatUnitTest::SetUp(void)
34 {
35 format_ = AVCodecMockFactory::CreateFormat();
36 ASSERT_NE(nullptr, format_);
37 }
38
TearDown(void)39 void FormatUnitTest::TearDown(void)
40 {
41 if (format_ != nullptr) {
42 format_->Destroy();
43 }
44 }
45
46 /**
47 * @tc.name: format_value_0100
48 * @tc.desc: format set and get value
49 * @tc.type: FUNC
50 * @tc.require: issueI5OWXY issueI5OXCD
51 */
52 HWTEST_F(FormatUnitTest, format_value_0100, TestSize.Level0)
53 {
54 const std::string_view intKey = "IntKey";
55 const std::string_view longKey = "LongKey";
56 const std::string_view floatKey = "FloatKey";
57 const std::string_view doubleKey = "DoubleKey";
58 const std::string_view stringKey = "StringKey";
59 int32_t intValue = 1;
60 int64_t longValue = 1;
61 float floatValue = 1.0;
62 double doubleValue = 1.0;
63 const std::string stringValue = "StringValue";
64
65 int32_t getIntValue = 0;
66 int64_t getLongValue = 0;
67 float getFloatValue = 0.0;
68 double getDoubleValue = 0.0;
69 std::string getStringValue = "";
70
71 ASSERT_TRUE(format_->PutIntValue(intKey, intValue));
72 ASSERT_TRUE(format_->GetIntValue(intKey, getIntValue));
73 ASSERT_TRUE(intValue == getIntValue);
74
75 ASSERT_TRUE(format_->PutLongValue(longKey, longValue));
76 ASSERT_TRUE(format_->GetLongValue(longKey, getLongValue));
77 ASSERT_TRUE(longValue == getLongValue);
78
79 ASSERT_TRUE(format_->PutFloatValue(floatKey, floatValue));
80 ASSERT_TRUE(format_->GetFloatValue(floatKey, getFloatValue));
81 ASSERT_TRUE(fabs(floatValue - getFloatValue) < EPSINON_FLOAT);
82
83 ASSERT_TRUE(format_->PutDoubleValue(doubleKey, doubleValue));
84 ASSERT_TRUE(format_->GetDoubleValue(doubleKey, getDoubleValue));
85 ASSERT_TRUE(fabs(doubleValue - getDoubleValue) < EPSINON_DOUBLE);
86
87 ASSERT_TRUE(format_->PutStringValue(stringKey, stringValue.c_str()));
88 ASSERT_TRUE(format_->GetStringValue(stringKey, getStringValue));
89 ASSERT_TRUE(stringValue == getStringValue);
90 }
91
92 /**
93 * @tc.name: format_buffer_0100
94 * @tc.desc: format put and get buffer
95 * @tc.type: FUNC
96 * @tc.require: issueI5OWXY issueI5OXCD
97 */
98 HWTEST_F(FormatUnitTest, format_buffer_0100, TestSize.Level0)
99 {
100 constexpr int32_t num = 10;
101 const std::string_view key = "BufferKey";
102 size_t size = num * sizeof(uint8_t);
103 uint8_t *buffer = reinterpret_cast<uint8_t *>(malloc(size));
104 (void)memset_s(buffer, size, 1, size);
105
106 ASSERT_TRUE(format_->PutBuffer(key, buffer, size));
107 uint8_t *getBuffer;
108 size_t getSize;
109 ASSERT_TRUE(format_->GetBuffer(key, &getBuffer, getSize));
110 ASSERT_TRUE(size == getSize);
111 for (int32_t i = 0; i < num; i++) {
112 ASSERT_TRUE(buffer[i] == getBuffer[i]);
113 }
114 free(buffer);
115 }
116
117 /**
118 * @tc.name: format_dump_info_0100
119 * @tc.desc: format dump info
120 * @tc.type: FUNC
121 * @tc.require: issueI5OWXY issueI5OXCD
122 */
123 HWTEST_F(FormatUnitTest, format_dump_info_0100, TestSize.Level0)
124 {
125 ASSERT_TRUE(format_->PutIntValue("width", 1));
126 ASSERT_TRUE(format_->PutIntValue("height", 1));
127 ASSERT_TRUE(format_->PutStringValue("codec_mime", "video/avc"));
128 const char *info = format_->DumpInfo();
129 ASSERT_TRUE(info != nullptr);
130 std::cout << info << std::endl;
131 }