1 /*
2 * Copyright (C) 2025 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 #include <gtest/gtest.h>
17 #include <gmock/gmock.h>
18 #include "avbuffer_queue_supplement_unit_test.h"
19 #include "status.h"
20 #include "buffer/avbuffer_queue.h"
21
22 using namespace std;
23 using namespace testing::ext;
24
25 namespace OHOS {
26 namespace Media {
27 static const uint32_t NUM = -1;
28 static const uint32_t NUM_0 = 0;
29 static const uint32_t NUM_1 = 1;
30 static const uint32_t NUM_10 = 10;
31
SetUpTestCase(void)32 void AVBufferQueueSupplementUnitTest::SetUpTestCase(void) {}
33
TearDownTestCase(void)34 void AVBufferQueueSupplementUnitTest::TearDownTestCase(void) {}
35
SetUp(void)36 void AVBufferQueueSupplementUnitTest::SetUp(void)
37 {
38 name = "queue";
39 }
40
TearDown(void)41 void AVBufferQueueSupplementUnitTest::TearDown(void) {}
42
43 /**
44 * @tc.name: ClearBufferIfTest_001
45 * @tc.desc: Test ClearBufferIf interface
46 * @tc.type: cacheIt == cachedBufferMap_.end()
47 * cacheIt->second.state != AVBUFFER_STATE_PUSHED
48 */
49 HWTEST_F(AVBufferQueueSupplementUnitTest, ClearBufferIfTest, TestSize.Level1)
50 {
51 auto avBufferQueueImpl = std::make_shared<AVBufferQueueImpl>(name);
52 uint64_t uniqueId = NUM_1;
53 avBufferQueueImpl->dirtyBufferList_.push_back(uniqueId);
__anon938bd3140102(const std::shared_ptr<AVBuffer>& buffer) 54 auto isNewerSample = [](const std::shared_ptr<AVBuffer>& buffer) {
55 return buffer == nullptr;
56 };
57 EXPECT_EQ(Status::OK, avBufferQueueImpl->ClearBufferIf(isNewerSample));
58
59
60 AVBufferConfig config;
61 config.size = NUM;
62 config.capacity = NUM_0;
63 config.memoryType = MemoryType::VIRTUAL_MEMORY;
64
65 AVBufferElement ele = {
66 .config = config,
67 .state = AVBUFFER_STATE_RELEASED,
68 .isDeleting = false,
69 .buffer = nullptr,
70 };
71 avBufferQueueImpl->cachedBufferMap_[uniqueId] = ele;
72 EXPECT_EQ(Status::OK, avBufferQueueImpl->ClearBufferIf(isNewerSample));
73 }
74
75 /**
76 * @tc.name: ReturnBuffer
77 * @tc.desc: Test ReturnBuffer interface
78 * @tc.type: cachedBufferMap_[uniqueId].isDeleting
79 */
80 HWTEST_F(AVBufferQueueSupplementUnitTest, ReturnBuffer, TestSize.Level1)
81 {
82 auto avBufferQueueImpl = std::make_shared<AVBufferQueueImpl>(name);
83
84 uint64_t uniqueId = NUM_1;
85 AVBufferConfig config;
86 config.size = NUM_1;
87 config.capacity = NUM_1;
88 config.memoryType = MemoryType::VIRTUAL_MEMORY;
89 std::shared_ptr<AVBuffer> buffer = AVBuffer::CreateAVBuffer(config);
90
91 AVBufferElement ele = {
92 .config = config,
93 .state = AVBUFFER_STATE_RELEASED,
94 .isDeleting = true,
95 .buffer = nullptr,
96 };
97 avBufferQueueImpl->cachedBufferMap_[uniqueId] = ele;
98 EXPECT_EQ(avBufferQueueImpl->ReturnBuffer(uniqueId, true), Status::OK);
99 }
100
101 /**
102 * @tc.name: ReleaseBuffer
103 * @tc.desc: Test ReleaseBuffer interface
104 * @tc.type: cachedBufferMap_[uniqueId].isDeleting
105 */
106 HWTEST_F(AVBufferQueueSupplementUnitTest, ReleaseBuffer, TestSize.Level1)
107 {
108 auto avBufferQueueImpl = std::make_shared<AVBufferQueueImpl>(name);
109
110 uint64_t uniqueId = NUM_1;
111 AVBufferConfig config;
112 config.size = NUM_1;
113 config.capacity = NUM_1;
114 config.memoryType = MemoryType::VIRTUAL_MEMORY;
115 std::shared_ptr<AVBuffer> buffer = AVBuffer::CreateAVBuffer(config);
116
117 AVBufferElement ele = {
118 .config = config,
119 .state = AVBUFFER_STATE_ACQUIRED,
120 .isDeleting = true,
121 .buffer = nullptr,
122 };
123 avBufferQueueImpl->cachedBufferMap_[uniqueId] = ele;
124 EXPECT_EQ(avBufferQueueImpl->ReleaseBuffer(uniqueId), Status::OK);
125 }
126
127 /**
128 * @tc.name: PushBufferOnFilled
129 * @tc.desc: Test PushBufferOnFilled interface
130 * @tc.type: ret != Status::OK
131 */
132 HWTEST_F(AVBufferQueueSupplementUnitTest, PushBufferOnFilled, TestSize.Level1)
133 {
134 auto avBufferQueueImpl = std::make_shared<AVBufferQueueImpl>(name);
135 AVBufferConfig config;
136 config.size = NUM_1;
137 config.capacity = NUM_1;
138 config.memoryType = MemoryType::VIRTUAL_MEMORY;
139
140 EXPECT_EQ(avBufferQueueImpl->PushBufferOnFilled(NUM_1, false), Status::ERROR_INVALID_BUFFER_ID);
141 }
142
143 /**
144 * @tc.name: SetQueueSizeBeforeAttachBufferLocked_001
145 * @tc.desc: Test SetQueueSizeBeforeAttachBufferLocked interface
146 * @tc.type: size <= size_
147 */
148 HWTEST_F(AVBufferQueueSupplementUnitTest, SetQueueSizeBeforeAttachBufferLocked_001, TestSize.Level1)
149 {
150 auto avBufferQueueImpl = std::make_shared<AVBufferQueueImpl>(name);
151 uint32_t size = NUM_0;
152 avBufferQueueImpl->SetQueueSizeBeforeAttachBufferLocked(size);
153 EXPECT_EQ(avBufferQueueImpl->size_, size);
154 }
155
156 /**
157 * @tc.name: SetQueueSizeBeforeAttachBufferLocked_002
158 * @tc.desc: Test SetQueueSizeBeforeAttachBufferLocked interface
159 * @tc.type: disableAlloc_
160 */
161 HWTEST_F(AVBufferQueueSupplementUnitTest, SetQueueSizeBeforeAttachBufferLocked_002, TestSize.Level1)
162 {
163 auto avBufferQueueImpl = std::make_shared<AVBufferQueueImpl>(name);
164 uint32_t size = NUM_10;
165 avBufferQueueImpl->disableAlloc_ = false;
166 avBufferQueueImpl->SetQueueSizeBeforeAttachBufferLocked(size);
167 EXPECT_EQ(avBufferQueueImpl->size_, size);
168 }
169 } // namespace Media
170 } // namespace OHOS
171