1 /*
2  * Copyright (C) 2024 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 "common/avsharedmemorypool.h"
18 #include "buffer/avsharedmemorybase.h"
19 
20 using namespace testing::ext;
21 using namespace OHOS::Media;
22 
23 namespace {
24 const std::string DUMP_PARAM = "w";
25 const std::string DUMP_FILE_NAME = "DumpBufferTest.es";
26 }
27 
28 namespace OHOS {
29 namespace Media {
30 class AVSharedMemoryPoolTest : public testing::Test {
31 public:
SetUpTestCase(void)32     static void SetUpTestCase(void) {};
TearDownTestCase(void)33     static void TearDownTestCase(void) {};
SetUp(void)34     void SetUp(void) {};
TearDown(void)35     void TearDown(void) {};
36 };
37 
38 /**
39  * @tc.name: ReleaseMemory
40  * @tc.desc: ReleaseMemory
41  * @tc.type: FUNC
42  */
43 HWTEST_F(AVSharedMemoryPoolTest, ReleaseMemory, TestSize.Level1)
44 {
45     int32_t size = 0;
46     std::shared_ptr<AVSharedMemoryPool> pool = std::make_shared<AVSharedMemoryPool>("releaseMemory");
47     AVSharedMemory *memory = nullptr;
48     pool->ReleaseMemory(memory);
49     bool result = pool->DoAcquireMemory(size, &memory);
50     EXPECT_EQ(true, result);
51 }
52 
53 /**
54  * @tc.name: DoAcquireMemory
55  * @tc.desc: DoAcquireMemory
56  * @tc.type: FUNC
57  */
58 HWTEST_F(AVSharedMemoryPoolTest, DoAcquireMemory, TestSize.Level1)
59 {
60     int32_t size = 0;
61     std::shared_ptr<AVSharedMemoryPool> pool = std::make_shared<AVSharedMemoryPool>("doAcquireMemory");
62     AVSharedMemory *memory = nullptr;
63     bool result = pool->DoAcquireMemory(size, &memory);
64     EXPECT_EQ(true, result);
65 }
66 
67 /**
68  * @tc.name: CheckSize
69  * @tc.desc: CheckSize
70  * @tc.type: FUNC
71  */
72 HWTEST_F(AVSharedMemoryPoolTest, CheckSize, TestSize.Level1)
73 {
74     std::shared_ptr<AVSharedMemoryPool> pool = std::make_shared<AVSharedMemoryPool>("checkSize");
75     EXPECT_EQ(false, pool->CheckSize(-2));
76     EXPECT_EQ(false, pool->CheckSize(1));
77     EXPECT_TRUE(pool->CheckSize(-1));
78 }
79 
80 /**
81  * @tc.name: AcquireMemory
82  * @tc.desc: AcquireMemory
83  * @tc.type: FUNC
84  */
85 HWTEST_F(AVSharedMemoryPoolTest, AcquireMemory, TestSize.Level1)
86 {
87     int32_t size = 0;
88     bool blocking = true;
89     std::shared_ptr<AVSharedMemoryPool> pool = std::make_shared<AVSharedMemoryPool>("acquireMemory");
90     pool->SetNonBlocking(true);
91     std::shared_ptr<AVSharedMemory> memory = pool->AcquireMemory(size, blocking);
92     EXPECT_TRUE(memory == nullptr);
93     pool->SetNonBlocking(false);
94     std::shared_ptr<AVSharedMemory> memory1 = pool->AcquireMemory(size, blocking);
95     EXPECT_TRUE(memory1 == nullptr);
96 }
97 } // namespace Media
98 } // namespace OHOS
99