• 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 "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 capture frame
128 */
129 HWTEST_F(WakeupAudioCaptureSourceUnitTest, WakeupSourceUnitTest_004, TestSize.Level1)
130 {
131     EXPECT_TRUE(source_ && source_->IsInited());
132     uint64_t replyBytes = 0;
133     std::vector<char> buffer{'8', '8', '8', '8', '8', '8', '8', '8'};
134     int32_t ret = source_->CaptureFrame(buffer.data(), buffer.size(), replyBytes);
135     EXPECT_EQ(ret, SUCCESS);
136 }
137 
138 /**
139  * @tc.name   : Test WakeupSource API
140  * @tc.number : WakeupSourceUnitTest_005
141  * @tc.desc   : Test wakeup source get param
142  */
143 HWTEST_F(WakeupAudioCaptureSourceUnitTest, WakeupSourceUnitTest_005, TestSize.Level1)
144 {
145     EXPECT_TRUE(source_ && source_->IsInited());
146     std::string param = source_->GetAudioParameter(USB_DEVICE, "");
147     EXPECT_EQ(param, "");
148 }
149 
150 /**
151  * @tc.name   : Test WakeupSource API
152  * @tc.number : WakeupSourceUnitTest_006
153  * @tc.desc   : Test wakeup source set/get volume
154  */
155 HWTEST_F(WakeupAudioCaptureSourceUnitTest, WakeupSourceUnitTest_006, TestSize.Level1)
156 {
157     EXPECT_TRUE(source_);
158     int32_t ret = source_->SetVolume(1.0f, 1.0f);
159     EXPECT_NE(ret, SUCCESS);
160     float left;
161     float right;
162     ret = source_->GetVolume(left, right);
163     EXPECT_EQ(ret, SUCCESS);
164 }
165 
166 /**
167  * @tc.name   : Test WakeupSource API
168  * @tc.number : WakeupSourceUnitTest_007
169  * @tc.desc   : Test wakeup source set/get mute
170  */
171 HWTEST_F(WakeupAudioCaptureSourceUnitTest, WakeupSourceUnitTest_007, TestSize.Level1)
172 {
173     EXPECT_TRUE(source_);
174     int32_t ret = source_->SetMute(false);
175     EXPECT_EQ(ret, SUCCESS);
176     bool mute = false;
177     ret = source_->GetMute(mute);
178     EXPECT_EQ(ret, SUCCESS);
179     EXPECT_FALSE(mute);
180 }
181 
182 /**
183  * @tc.name   : Test WakeupSource API
184  * @tc.number : WakeupSourceUnitTest_008
185  * @tc.desc   : Test wakeup source get transaction id
186  */
187 HWTEST_F(WakeupAudioCaptureSourceUnitTest, WakeupSourceUnitTest_008, TestSize.Level1)
188 {
189     EXPECT_TRUE(source_);
190     uint64_t transId = source_->GetTransactionId();
191     EXPECT_NE(transId, 0);
192 }
193 
194 /**
195  * @tc.name   : Test WakeupSource API
196  * @tc.number : WakeupSourceUnitTest_009
197  * @tc.desc   : Test wakeup source get max amplitude
198  */
199 HWTEST_F(WakeupAudioCaptureSourceUnitTest, WakeupSourceUnitTest_009, TestSize.Level1)
200 {
201     EXPECT_TRUE(source_);
202     float maxAmplitude = source_->GetMaxAmplitude();
203     EXPECT_EQ(maxAmplitude, 0.0f);
204 }
205 
206 /**
207  * @tc.name   : Test WakeupSource API
208  * @tc.number : WakeupSourceUnitTest_010
209  * @tc.desc   : Test wakeup source set audio scene
210  */
211 HWTEST_F(WakeupAudioCaptureSourceUnitTest, WakeupSourceUnitTest_010, TestSize.Level1)
212 {
213     EXPECT_TRUE(source_);
214     int32_t ret = source_->SetAudioScene(AUDIO_SCENE_DEFAULT);
215     EXPECT_EQ(ret, SUCCESS);
216 }
217 
218 /**
219  * @tc.name   : Test WakeupSource API
220  * @tc.number : WakeupSourceUnitTest_011
221  * @tc.desc   : Test wakeup source update source type
222  */
223 HWTEST_F(WakeupAudioCaptureSourceUnitTest, WakeupSourceUnitTest_011, TestSize.Level1)
224 {
225     EXPECT_TRUE(source_);
226     int32_t ret = source_->UpdateSourceType(SOURCE_TYPE_MIC);
227     EXPECT_EQ(ret, SUCCESS);
228 }
229 
230 /**
231  * @tc.name   : Test WakeupSource API
232  * @tc.number : WakeupSourceUnitTest_012
233  * @tc.desc   : Test wakeup source update apps uid
234  */
235 HWTEST_F(WakeupAudioCaptureSourceUnitTest, WakeupSourceUnitTest_012, TestSize.Level1)
236 {
237     EXPECT_TRUE(source_);
238     vector<int32_t> appsUid;
239     int32_t ret = source_->UpdateAppsUid(appsUid);
240     EXPECT_EQ(ret, SUCCESS);
241 }
242 
243 } // namespace AudioStandard
244 } // namespace OHOS
245