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 "audio_utils.h"
20 #include "common/hdi_adapter_info.h"
21 #include "manager/hdi_adapter_manager.h"
22
23 using namespace testing::ext;
24
25 namespace OHOS {
26 namespace AudioStandard {
27 class WakeupAudioCaptureSourceUnitTest : public testing::Test {
28 public:
29 static void SetUpTestCase();
30 static void TearDownTestCase();
31 virtual void SetUp();
32 virtual void TearDown();
33
34 protected:
35 static uint32_t id_;
36 static std::shared_ptr<IAudioCaptureSource> source_;
37 static IAudioSourceAttr attr_;
38 };
39
40 uint32_t WakeupAudioCaptureSourceUnitTest::id_ = HDI_INVALID_ID;
41 std::shared_ptr<IAudioCaptureSource> WakeupAudioCaptureSourceUnitTest::source_ = nullptr;
42 IAudioSourceAttr WakeupAudioCaptureSourceUnitTest::attr_ = {};
43
SetUpTestCase()44 void WakeupAudioCaptureSourceUnitTest::SetUpTestCase()
45 {
46 id_ = HdiAdapterManager::GetInstance().GetId(HDI_ID_BASE_CAPTURE, HDI_ID_TYPE_WAKEUP, "test", true);
47 }
48
TearDownTestCase()49 void WakeupAudioCaptureSourceUnitTest::TearDownTestCase()
50 {
51 HdiAdapterManager::GetInstance().ReleaseId(id_);
52 }
53
SetUp()54 void WakeupAudioCaptureSourceUnitTest::SetUp()
55 {
56 source_ = HdiAdapterManager::GetInstance().GetCaptureSource(id_, true);
57 if (source_ == nullptr) {
58 return;
59 }
60 attr_.adapterName = "primary";
61 attr_.sampleRate = 48000; // 48000: sample rate
62 attr_.channel = 2; // 2: channel
63 attr_.format = SAMPLE_S16LE;
64 attr_.channelLayout = 3; // 3: channel layout
65 attr_.deviceType = DEVICE_TYPE_MIC;
66 attr_.openMicSpeaker = 1;
67 source_->Init(attr_);
68 }
69
TearDown()70 void WakeupAudioCaptureSourceUnitTest::TearDown()
71 {
72 if (source_ && source_->IsInited()) {
73 source_->DeInit();
74 }
75 source_ = nullptr;
76 }
77
78 /**
79 * @tc.name : Test WakeupSource API
80 * @tc.number : WakeupSourceUnitTest_001
81 * @tc.desc : Test wakeup source create
82 */
83 HWTEST_F(WakeupAudioCaptureSourceUnitTest, WakeupSourceUnitTest_001, TestSize.Level1)
84 {
85 EXPECT_TRUE(source_);
86 }
87
88 /**
89 * @tc.name : Test WakeupSource API
90 * @tc.number : WakeupSourceUnitTest_002
91 * @tc.desc : Test wakeup source init
92 */
93 HWTEST_F(WakeupAudioCaptureSourceUnitTest, WakeupSourceUnitTest_002, TestSize.Level1)
94 {
95 EXPECT_TRUE(source_ && source_->IsInited());
96 }
97
98 /**
99 * @tc.name : Test WakeupSource API
100 * @tc.number : WakeupSourceUnitTest_003
101 * @tc.desc : Test wakeup source start, stop, resume, pause, flush, reset
102 */
103 HWTEST_F(WakeupAudioCaptureSourceUnitTest, WakeupSourceUnitTest_003, TestSize.Level1)
104 {
105 EXPECT_TRUE(source_ && source_->IsInited());
106 int32_t ret = source_->Start();
107 EXPECT_EQ(ret, SUCCESS);
108 ret = source_->Stop();
109 EXPECT_EQ(ret, SUCCESS);
110 ret = source_->Start();
111 EXPECT_EQ(ret, SUCCESS);
112 ret = source_->Resume();
113 EXPECT_EQ(ret, SUCCESS);
114 ret = source_->Pause();
115 EXPECT_NE(ret, SUCCESS);
116 ret = source_->Flush();
117 EXPECT_NE(ret, SUCCESS);
118 ret = source_->Reset();
119 EXPECT_NE(ret, SUCCESS);
120 ret = source_->Stop();
121 EXPECT_EQ(ret, SUCCESS);
122 }
123
124 /**
125 * @tc.name : Test WakeupSource API
126 * @tc.number : WakeupSourceUnitTest_004
127 * @tc.desc : Test wakeup source get param
128 */
129 HWTEST_F(WakeupAudioCaptureSourceUnitTest, WakeupSourceUnitTest_004, TestSize.Level1)
130 {
131 EXPECT_TRUE(source_ && source_->IsInited());
132 std::string param = source_->GetAudioParameter(USB_DEVICE, "");
133 EXPECT_EQ(param, "");
134 }
135
136 /**
137 * @tc.name : Test WakeupSource API
138 * @tc.number : WakeupSourceUnitTest_005
139 * @tc.desc : Test wakeup source set/get volume
140 */
141 HWTEST_F(WakeupAudioCaptureSourceUnitTest, WakeupSourceUnitTest_005, TestSize.Level1)
142 {
143 EXPECT_TRUE(source_);
144 int32_t ret = source_->SetVolume(1.0f, 1.0f);
145 EXPECT_NE(ret, SUCCESS);
146 float left;
147 float right;
148 ret = source_->GetVolume(left, right);
149 EXPECT_EQ(ret, SUCCESS);
150 }
151
152 /**
153 * @tc.name : Test PrimarySource API
154 * @tc.number : WakeupSourceUnitTest_006
155 * @tc.desc : Test wakeup source set/get mute
156 */
157 HWTEST_F(WakeupAudioCaptureSourceUnitTest, WakeupSourceUnitTest_006, TestSize.Level1)
158 {
159 EXPECT_TRUE(source_);
160 int32_t ret = source_->SetMute(false);
161 EXPECT_EQ(ret, SUCCESS);
162 bool mute = false;
163 ret = source_->GetMute(mute);
164 EXPECT_EQ(ret, SUCCESS);
165 EXPECT_FALSE(mute);
166 }
167
168 /**
169 * @tc.name : Test PrimarySource API
170 * @tc.number : WakeupSourceUnitTest_007
171 * @tc.desc : Test wakeup source get transaction id
172 */
173 HWTEST_F(WakeupAudioCaptureSourceUnitTest, WakeupSourceUnitTest_007, TestSize.Level1)
174 {
175 EXPECT_TRUE(source_);
176 uint64_t transId = source_->GetTransactionId();
177 EXPECT_NE(transId, 0);
178 }
179
180 /**
181 * @tc.name : Test PrimarySource API
182 * @tc.number : WakeupSourceUnitTest_008
183 * @tc.desc : Test wakeup source get max amplitude
184 */
185 HWTEST_F(WakeupAudioCaptureSourceUnitTest, WakeupSourceUnitTest_008, TestSize.Level1)
186 {
187 EXPECT_TRUE(source_);
188 float maxAmplitude = source_->GetMaxAmplitude();
189 EXPECT_EQ(maxAmplitude, 0.0f);
190 }
191
192 /**
193 * @tc.name : Test PrimarySource API
194 * @tc.number : WakeupSourceUnitTest_009
195 * @tc.desc : Test wakeup source set audio scene
196 */
197 HWTEST_F(WakeupAudioCaptureSourceUnitTest, WakeupSourceUnitTest_009, TestSize.Level1)
198 {
199 EXPECT_TRUE(source_);
200 int32_t ret = source_->SetAudioScene(AUDIO_SCENE_DEFAULT, DEVICE_TYPE_SPEAKER);
201 EXPECT_EQ(ret, SUCCESS);
202 }
203
204 /**
205 * @tc.name : Test PrimarySource API
206 * @tc.number : WakeupSourceUnitTest_010
207 * @tc.desc : Test wakeup source update source type
208 */
209 HWTEST_F(WakeupAudioCaptureSourceUnitTest, WakeupSourceUnitTest_010, TestSize.Level1)
210 {
211 EXPECT_TRUE(source_);
212 int32_t ret = source_->UpdateSourceType(SOURCE_TYPE_MIC);
213 EXPECT_EQ(ret, SUCCESS);
214 }
215
216 /**
217 * @tc.name : Test PrimarySource API
218 * @tc.number : WakeupSourceUnitTest_011
219 * @tc.desc : Test wakeup source update apps uid
220 */
221 HWTEST_F(WakeupAudioCaptureSourceUnitTest, WakeupSourceUnitTest_011, TestSize.Level1)
222 {
223 EXPECT_TRUE(source_);
224 vector<int32_t> appsUid;
225 int32_t ret = source_->UpdateAppsUid(appsUid);
226 EXPECT_EQ(ret, SUCCESS);
227 }
228
229 } // namespace AudioStandard
230 } // namespace OHOS
231