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 RemoteFastAudioRenderSinkUnitTest : 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<IAudioRenderSink> sink_;
37 static IAudioSinkAttr attr_;
38 };
39
40 uint32_t RemoteFastAudioRenderSinkUnitTest::id_ = HDI_INVALID_ID;
41 std::shared_ptr<IAudioRenderSink> RemoteFastAudioRenderSinkUnitTest::sink_ = nullptr;
42 IAudioSinkAttr RemoteFastAudioRenderSinkUnitTest::attr_ = {};
43
SetUpTestCase()44 void RemoteFastAudioRenderSinkUnitTest::SetUpTestCase()
45 {
46 id_ = HdiAdapterManager::GetInstance().GetId(HDI_ID_BASE_RENDER, HDI_ID_TYPE_REMOTE_FAST, "test", true);
47 }
48
TearDownTestCase()49 void RemoteFastAudioRenderSinkUnitTest::TearDownTestCase()
50 {
51 HdiAdapterManager::GetInstance().ReleaseId(id_);
52 }
53
SetUp()54 void RemoteFastAudioRenderSinkUnitTest::SetUp()
55 {
56 sink_ = HdiAdapterManager::GetInstance().GetRenderSink(id_, true);
57 if (sink_ == nullptr) {
58 return;
59 }
60 attr_.adapterName = "test";
61 attr_.channel = 2; // 2: channel
62 sink_->Init(attr_);
63 }
64
TearDown()65 void RemoteFastAudioRenderSinkUnitTest::TearDown()
66 {
67 if (sink_ && sink_->IsInited()) {
68 sink_->DeInit();
69 }
70 sink_ = nullptr;
71 }
72
73 /**
74 * @tc.name : Test RemoteFastSink API
75 * @tc.number : RemoteFastSinkUnitTest_001
76 * @tc.desc : Test remote fast sink create
77 */
78 HWTEST_F(RemoteFastAudioRenderSinkUnitTest, RemoteFastSinkUnitTest_001, TestSize.Level1)
79 {
80 EXPECT_TRUE(sink_);
81 }
82
83 /**
84 * @tc.name : Test RemoteFastSink API
85 * @tc.number : RemoteFastSinkUnitTest_002
86 * @tc.desc : Test remote fast sink deinit
87 */
88 HWTEST_F(RemoteFastAudioRenderSinkUnitTest, RemoteFastSinkUnitTest_002, TestSize.Level1)
89 {
90 EXPECT_TRUE(sink_);
91 sink_->DeInit();
92 EXPECT_FALSE(sink_->IsInited());
93 }
94
95 /**
96 * @tc.name : Test RemoteFastSink API
97 * @tc.number : RemoteFastSinkUnitTest_003
98 * @tc.desc : Test remote fast sink start, stop, resume, pause, flush, reset
99 */
100 HWTEST_F(RemoteFastAudioRenderSinkUnitTest, RemoteFastSinkUnitTest_003, TestSize.Level1)
101 {
102 EXPECT_TRUE(sink_);
103 int32_t ret = sink_->Start();
104 EXPECT_EQ(ret, ERR_NOT_STARTED);
105 ret = sink_->Stop();
106 EXPECT_EQ(ret, SUCCESS);
107 ret = sink_->Resume();
108 EXPECT_EQ(ret, ERR_ILLEGAL_STATE);
109 ret = sink_->Pause();
110 EXPECT_EQ(ret, ERR_ILLEGAL_STATE);
111 ret = sink_->Flush();
112 EXPECT_EQ(ret, ERR_ILLEGAL_STATE);
113 ret = sink_->Reset();
114 EXPECT_EQ(ret, ERR_ILLEGAL_STATE);
115 ret = sink_->Stop();
116 EXPECT_EQ(ret, SUCCESS);
117 }
118
119 /**
120 * @tc.name : Test RemoteFastSink API
121 * @tc.number : RemoteFastSinkUnitTest_004
122 * @tc.desc : Test remote fast sink set/get volume
123 */
124 HWTEST_F(RemoteFastAudioRenderSinkUnitTest, RemoteFastSinkUnitTest_004, TestSize.Level1)
125 {
126 EXPECT_TRUE(sink_);
127 int32_t ret = sink_->SetVolume(0.0f, 0.0f);
128 EXPECT_EQ(ret, ERR_INVALID_HANDLE);
129 ret = sink_->SetVolume(0.0f, 1.0f);
130 EXPECT_EQ(ret, ERR_INVALID_HANDLE);
131 ret = sink_->SetVolume(1.0f, 0.0f);
132 EXPECT_EQ(ret, ERR_INVALID_HANDLE);
133 ret = sink_->SetVolume(1.0f, 1.0f);
134 EXPECT_EQ(ret, ERR_INVALID_HANDLE);
135 float left;
136 float right;
137 ret = sink_->GetVolume(left, right);
138 EXPECT_EQ(ret, SUCCESS);
139 }
140
141 /**
142 * @tc.name : Test RemoteFastSink API
143 * @tc.number : RemoteFastSinkUnitTest_005
144 * @tc.desc : Test remote fast sink set audio scene
145 */
146 HWTEST_F(RemoteFastAudioRenderSinkUnitTest, RemoteFastSinkUnitTest_005, TestSize.Level1)
147 {
148 EXPECT_TRUE(sink_);
149 int32_t ret = sink_->SetAudioScene(AUDIO_SCENE_DEFAULT);
150 EXPECT_EQ(ret, SUCCESS);
151 }
152
153 /**
154 * @tc.name : Test RemoteFastSink API
155 * @tc.number : RemoteFastSinkUnitTest_006
156 * @tc.desc : Test remote fast sink update active device
157 */
158 HWTEST_F(RemoteFastAudioRenderSinkUnitTest, RemoteFastSinkUnitTest_006, TestSize.Level1)
159 {
160 EXPECT_TRUE(sink_);
161 std::vector<DeviceType> deviceTypes = { DEVICE_TYPE_SPEAKER };
162 int32_t ret = sink_->UpdateActiveDevice(deviceTypes);
163 EXPECT_EQ(ret, ERR_NOT_SUPPORTED);
164 }
165
166 } // namespace AudioStandard
167 } // namespace OHOS
168