• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <iostream>
17 #include <gtest/gtest.h>
18 #include <gmock/gmock.h>
19 #include <vector>
20 #include "audio_utils.h"
21 #include "audio_errors.h"
22 #include "common/hdi_adapter_info.h"
23 #include "util/id_handler.h"
24 #include "util/audio_running_lock.h"
25 #include "util/ring_buffer_handler.h"
26 #include "util/callback_wrapper.h"
27 
28 using namespace testing::ext;
29 
30 namespace OHOS {
31 namespace AudioStandard {
32 class UtilUnitTest : public testing::Test {
33 public:
SetUpTestCase()34     static void SetUpTestCase() {}
TearDownTestCase()35     static void TearDownTestCase() {}
SetUp()36     virtual void SetUp() {}
TearDown()37     virtual void TearDown() {}
38 };
39 
40 /**
41  * @tc.name   : Test Util API
42  * @tc.number : UtilUnitTest_001
43  * @tc.desc   : Test IdHandler action
44  */
45 HWTEST_F(UtilUnitTest, UtilUnitTest_001, TestSize.Level1)
46 {
47     IdHandler &idHandler = IdHandler::GetInstance();
48     uint32_t id = idHandler.GetId(HDI_ID_BASE_RENDER, HDI_ID_TYPE_PRIMARY, HDI_ID_INFO_DEFAULT);
49     EXPECT_NE(id, HDI_INVALID_ID);
50 
51     id = idHandler.GetRenderIdByDeviceClass("");
52     EXPECT_EQ(id, HDI_INVALID_ID);
53 
54     id = idHandler.GetCaptureIdByDeviceClass("", SOURCE_TYPE_MIC);
55     EXPECT_EQ(id, HDI_INVALID_ID);
56 
57     idHandler.IncInfoIdUseCount(id);
58     idHandler.DecInfoIdUseCount(id);
59 
60     auto ret = idHandler.CheckId(id, HDI_ID_BASE_RENDER);
61     EXPECT_EQ(ret, false);
62 
63     uint32_t base = idHandler.ParseBase(0);
64     EXPECT_EQ(base, 0);
65 
66     uint32_t type = idHandler.ParseType(0);
67     EXPECT_EQ(type, 0);
68 
69     std::string info = idHandler.ParseInfo(HDI_INVALID_ID);
70     EXPECT_EQ(info, "");
71 }
72 
73 /**
74  * @tc.name   : Test Util API
75  * @tc.number : UtilUnitTest_002
76  * @tc.desc   : Test AudioRunningLock action
77  */
78 HWTEST_F(UtilUnitTest, UtilUnitTest_002, TestSize.Level1)
79 {
80 #ifdef FEATURE_POWER_MANAGER
81     std::shared_ptr<AudioRunningLock> runningLock = std::make_shared<AudioRunningLock>("test");
82     ASSERT_NE(runningLock, nullptr);
83 
84     auto ret = runningLock->Lock(-1); // -1: test
85     EXPECT_EQ(ret, SUCCESS);
86 
87     runningLock->UnLock();
88     vector<int32_t> vec = { 0 };
89     runningLock->UpdateAppsUid(vec.begin(), vec.end());
90 #endif
91 }
92 
93 /**
94  * @tc.name   : Test Util API
95  * @tc.number : UtilUnitTest_003
96  * @tc.desc   : Test RingBufferHandler action
97  */
98 HWTEST_F(UtilUnitTest, UtilUnitTest_003, TestSize.Level1)
99 {
100     std::shared_ptr<RingBufferHandler> handler = std::make_shared<RingBufferHandler>();
101     ASSERT_NE(handler, nullptr);
102 
103     handler->Init(10, 10, 10, 10, 10); // 10: test
104 
105     vector<uint8_t> bufferWrite = { 0 };
106     auto ret = handler->WriteDataToRingBuffer(bufferWrite.data(), bufferWrite.size());
107     EXPECT_NE(ret, SUCCESS);
108 
109     vector<uint8_t> bufferRead = { 0 };
110     bufferRead.resize(bufferWrite.size());
111     ret = handler->ReadDataFromRingBuffer(bufferRead.data(), bufferRead.size());
112     EXPECT_NE(ret, SUCCESS);
113 
114     handler->AddWriteIndex();
115     handler->AddReadIndex();
116 }
117 
118 /**
119  * @tc.name   : Test Util API
120  * @tc.number : UtilUnitTest_004
121  * @tc.desc   : Test CallbackWrapper action
122  */
123 HWTEST_F(UtilUnitTest, UtilUnitTest_004, TestSize.Level1)
124 {
125     SinkCallbackWrapper sinkCbWrapper;
126 
127     sinkCbWrapper.RegistCallback(100, nullptr); // 100: test
128 
129     auto sinkCb = sinkCbWrapper.GetCallback(100);
130     EXPECT_EQ(sinkCb, nullptr);
131 
132     auto sinkRawCb = sinkCbWrapper.GetRawCallback(100);
133     EXPECT_EQ(sinkRawCb, nullptr);
134 
135     SourceCallbackWrapper sourceCbWrapper;
136 
137     sourceCbWrapper.RegistCallback(100, nullptr); // 100: test
138 
139     auto sourceCb = sourceCbWrapper.GetCallback(100);
140     EXPECT_EQ(sourceCb, nullptr);
141 
142     auto sourceRawCb = sourceCbWrapper.GetRawCallback(100);
143     EXPECT_EQ(sourceRawCb, nullptr);
144 }
145 
146 } // namespace AudioStandard
147 } // namespace OHOS
148