• 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 BluetoothAudioCaptureSourceUnitTest : 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 BluetoothAudioCaptureSourceUnitTest::id_ = HDI_INVALID_ID;
41 std::shared_ptr<IAudioCaptureSource> BluetoothAudioCaptureSourceUnitTest::source_ = nullptr;
42 IAudioSourceAttr BluetoothAudioCaptureSourceUnitTest::attr_ = {};
43 
SetUpTestCase()44 void BluetoothAudioCaptureSourceUnitTest::SetUpTestCase()
45 {
46     id_ = HdiAdapterManager::GetInstance().GetId(HDI_ID_BASE_CAPTURE, HDI_ID_TYPE_BLUETOOTH, HDI_ID_INFO_DEFAULT, true);
47 }
48 
TearDownTestCase()49 void BluetoothAudioCaptureSourceUnitTest::TearDownTestCase()
50 {
51     HdiAdapterManager::GetInstance().ReleaseId(id_);
52 }
53 
SetUp()54 void BluetoothAudioCaptureSourceUnitTest::SetUp()
55 {
56     source_ = HdiAdapterManager::GetInstance().GetCaptureSource(id_, true);
57     if (source_ == nullptr) {
58         return;
59     }
60 }
61 
TearDown()62 void BluetoothAudioCaptureSourceUnitTest::TearDown()
63 {
64     if (source_ && source_->IsInited()) {
65         source_->DeInit();
66     }
67     source_ = nullptr;
68 }
69 
70 /**
71  * @tc.name   : Test BluetoothSource API
72  * @tc.number : BluetoothSourceUnitTest_001
73  * @tc.desc   : Test bluetooth source create
74  */
75 HWTEST_F(BluetoothAudioCaptureSourceUnitTest, BluetoothSourceUnitTest_001, TestSize.Level1)
76 {
77     EXPECT_TRUE(source_);
78 }
79 
80 /**
81  * @tc.name   : Test BluetoothSource API
82  * @tc.number : BluetoothSourceUnitTest_002
83  * @tc.desc   : Test bluetooth source deinit
84  */
85 HWTEST_F(BluetoothAudioCaptureSourceUnitTest, BluetoothSourceUnitTest_002, TestSize.Level1)
86 {
87     EXPECT_TRUE(source_);
88     if (source_->IsInited()) {
89         source_->DeInit();
90     }
91     EXPECT_FALSE(source_->IsInited());
92 }
93 
94 /**
95  * @tc.name   : Test BluetoothSource API
96  * @tc.number : BluetoothSourceUnitTest_003
97  * @tc.desc   : Test bluetooth source start, stop, resume, pause, flush, reset
98  */
99 HWTEST_F(BluetoothAudioCaptureSourceUnitTest, BluetoothSourceUnitTest_003, TestSize.Level1)
100 {
101     EXPECT_TRUE(source_);
102     int32_t ret = source_->Start();
103     EXPECT_EQ(ret, ERR_INVALID_HANDLE);
104     ret = source_->Stop();
105     EXPECT_EQ(ret, SUCCESS);
106     ret = source_->Resume();
107     EXPECT_EQ(ret, ERR_INVALID_HANDLE);
108     ret = source_->Pause();
109     EXPECT_EQ(ret, ERR_INVALID_HANDLE);
110     ret = source_->Flush();
111     EXPECT_EQ(ret, ERR_INVALID_HANDLE);
112     ret = source_->Reset();
113     EXPECT_EQ(ret, ERR_INVALID_HANDLE);
114     ret = source_->Stop();
115     EXPECT_EQ(ret, SUCCESS);
116 }
117 
118 /**
119  * @tc.name   : Test DirectSink API
120  * @tc.number : BluetoothSourceUnitTest_004
121  * @tc.desc   : Test bluetooth source set/get volume
122  */
123 HWTEST_F(BluetoothAudioCaptureSourceUnitTest, BluetoothSourceUnitTest_004, TestSize.Level1)
124 {
125     EXPECT_TRUE(source_);
126     int32_t ret = source_->SetVolume(0.0f, 0.0f);
127     EXPECT_EQ(ret, ERR_INVALID_HANDLE);
128     ret = source_->SetVolume(0.0f, 1.0f);
129     EXPECT_EQ(ret, ERR_INVALID_HANDLE);
130     ret = source_->SetVolume(1.0f, 0.0f);
131     EXPECT_EQ(ret, ERR_INVALID_HANDLE);
132     ret = source_->SetVolume(1.0f, 1.0f);
133     EXPECT_EQ(ret, ERR_INVALID_HANDLE);
134     float left;
135     float right;
136     ret = source_->GetVolume(left, right);
137     EXPECT_EQ(ret, ERR_INVALID_HANDLE);
138 }
139 
140 /**
141  * @tc.name   : Test BluetoothSource API
142  * @tc.number : BluetoothSourceUnitTest_005
143  * @tc.desc   : Test bluetooth source set/get mute
144  */
145 HWTEST_F(BluetoothAudioCaptureSourceUnitTest, BluetoothSourceUnitTest_005, TestSize.Level1)
146 {
147     EXPECT_TRUE(source_);
148     int32_t ret = source_->SetMute(false);
149     EXPECT_EQ(ret, ERR_INVALID_HANDLE);
150     bool isMute;
151     ret = source_->GetMute(isMute);
152     EXPECT_EQ(ret, ERR_INVALID_HANDLE);
153 }
154 
155 /**
156  * @tc.name   : Test BluetoothSource API
157  * @tc.number : BluetoothSourceUnitTest_006
158  * @tc.desc   : Test bluetooth source set audio scene
159  */
160 HWTEST_F(BluetoothAudioCaptureSourceUnitTest, BluetoothSourceUnitTest_006, TestSize.Level1)
161 {
162     EXPECT_TRUE(source_);
163     int32_t ret = source_->SetAudioScene(AUDIO_SCENE_DEFAULT);
164     EXPECT_EQ(ret, SUCCESS);
165 }
166 
167 /**
168  * @tc.name   : Test BluetoothSource API
169  * @tc.number : BluetoothSourceUnitTest_007
170  * @tc.desc   : Test bluetooth source update active device
171  */
172 HWTEST_F(BluetoothAudioCaptureSourceUnitTest, BluetoothSourceUnitTest_007, TestSize.Level1)
173 {
174     EXPECT_TRUE(source_);
175     int32_t ret = source_->UpdateActiveDevice(DEVICE_TYPE_SPEAKER);
176     EXPECT_EQ(ret, ERR_NOT_SUPPORTED);
177 }
178 
179 /**
180  * @tc.name   : Test BluetoothSource API
181  * @tc.number : BluetoothSourceUnitTest_008
182  * @tc.desc   : Test bluetooth source set invalid state
183  */
184 HWTEST_F(BluetoothAudioCaptureSourceUnitTest, BluetoothSourceUnitTest_008, TestSize.Level1)
185 {
186     EXPECT_TRUE(source_);
187     source_->SetInvalidState();
188     (void)source_->Init(attr_);
189     source_->DeInit();
190     EXPECT_FALSE(source_->IsInited());
191 }
192 
193 } // namespace AudioStandard
194 } // namespace OHOS
195