1 /*
2 * Copyright (c) 2022-2023 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 "decode_transport_test.h"
17
18 #include <memory>
19
20 #include "audio_data.h"
21 #include "audio_event.h"
22 #include "audio_param.h"
23 #include "daudio_log.h"
24 #include "daudio_util.h"
25 #include "mock_audio_data_channel.h"
26 #include "mock_audio_processor.h"
27 #include "mock_audio_transport_callback.h"
28 #include "securec.h"
29
30 using namespace testing::ext;
31
32 namespace OHOS {
33 namespace DistributedHardware {
34 const std::string RMT_DEV_ID_TEST = "RemoteTest";
35 const PortCapType ROLE_TEST = CAP_SPK;
36
SetUpTestCase(void)37 void DecodeTransportTest::SetUpTestCase(void)
38 {
39 }
40
TearDownTestCase(void)41 void DecodeTransportTest::TearDownTestCase(void)
42 {
43 }
44
SetUp(void)45 void DecodeTransportTest::SetUp(void)
46 {
47 transCallback_ = std::make_shared<MockAudioTransportCallback>();
48 decodeTrans_ = std::make_shared<AudioDecodeTransport>(RMT_DEV_ID_TEST);
49 }
50
TearDown(void)51 void DecodeTransportTest::TearDown(void)
52 {
53 transCallback_ = nullptr;
54 decodeTrans_ = nullptr;
55 }
56
57 /**
58 * @tc.name: decode_transport_test_001
59 * @tc.desc: Verify the configure processor function.
60 * @tc.type: FUNC
61 * @tc.require: AR000H0E5U
62 */
63 HWTEST_F(DecodeTransportTest, decode_transport_test_001, TestSize.Level1)
64 {
65 AudioParam testLocalParaEnc = {
66 {
67 SAMPLE_RATE_48000,
68 STEREO,
69 SAMPLE_S16LE,
70 AUDIO_CODEC_FLAC
71 },
72 {
73 SOURCE_TYPE_INVALID,
74 NORMAL_MODE
75 },
76 {
77 CONTENT_TYPE_UNKNOWN,
78 STREAM_USAGE_UNKNOWN,
79 NORMAL_MODE
80 }
81 };
82 AudioParam testRemoteParaEnc = {
83 {
84 SAMPLE_RATE_48000,
85 STEREO,
86 SAMPLE_S16LE,
87 AUDIO_CODEC_FLAC
88 },
89 {
90 SOURCE_TYPE_INVALID,
91 NORMAL_MODE
92 },
93 {
94 CONTENT_TYPE_UNKNOWN,
95 STREAM_USAGE_UNKNOWN,
96 NORMAL_MODE
97 }
98 };
99 EXPECT_NE(DH_SUCCESS, decodeTrans_->SetUp(testLocalParaEnc, testRemoteParaEnc, transCallback_, ROLE_TEST));
100 std::shared_ptr<IAudioDataTransCallback> callback = nullptr;
101 EXPECT_NE(DH_SUCCESS, decodeTrans_->SetUp(testLocalParaEnc, testRemoteParaEnc, callback, ROLE_TEST));
102 EXPECT_EQ(DH_SUCCESS, decodeTrans_->Release());
103 }
104
105 /**
106 * @tc.name: decode_transport_test_002
107 * @tc.desc: Verify the start processor without configure processor function.
108 * @tc.type: FUNC
109 * @tc.require: AR000H0E5U
110 */
111 HWTEST_F(DecodeTransportTest, decode_transport_test_002, TestSize.Level1)
112 {
113 EXPECT_NE(DH_SUCCESS, decodeTrans_->Start());
114 EXPECT_EQ(ERR_DH_AUDIO_NULLPTR, decodeTrans_->Stop());
115 EXPECT_EQ(DH_SUCCESS, decodeTrans_->Release());
116
117 decodeTrans_->audioChannel_ = std::make_shared<MockIAudioChannel>();
118 decodeTrans_->context_ = std::make_shared<AudioTransportContext>();
119 decodeTrans_->capType_ = CAP_MIC;
120 EXPECT_EQ(ERR_DH_AUDIO_TRANS_SESSION_NOT_OPEN, decodeTrans_->Start());
121 EXPECT_NE(DH_SUCCESS, decodeTrans_->Release());
122 }
123
124 /**
125 * @tc.name: decode_transport_test_003
126 * @tc.desc: Verify the pause and processor without configure processor function.
127 * @tc.type: FUNC
128 * @tc.require: AR000H0E5U
129 */
130 HWTEST_F(DecodeTransportTest, decode_transport_test_003, TestSize.Level1)
131 {
132 AudioParam testLocalParaEnc = {
133 {
134 SAMPLE_RATE_48000,
135 STEREO,
136 SAMPLE_S16LE,
137 AUDIO_CODEC_FLAC
138 },
139 {
140 SOURCE_TYPE_INVALID,
141 NORMAL_MODE
142 },
143 {
144 CONTENT_TYPE_UNKNOWN,
145 STREAM_USAGE_UNKNOWN,
146 NORMAL_MODE
147 }
148 };
149 AudioParam testRemoteParaEnc = {
150 {
151 SAMPLE_RATE_48000,
152 STEREO,
153 SAMPLE_S16LE,
154 AUDIO_CODEC_FLAC
155 },
156 {
157 SOURCE_TYPE_INVALID,
158 NORMAL_MODE
159 },
160 {
161 CONTENT_TYPE_UNKNOWN,
162 STREAM_USAGE_UNKNOWN,
163 NORMAL_MODE
164 }
165 };
166 EXPECT_EQ(ERR_DH_AUDIO_BAD_VALUE, decodeTrans_->RegisterProcessorListener(testLocalParaEnc, testRemoteParaEnc));
167 EXPECT_EQ(ERR_DH_AUDIO_TRANS_ERROR,
168 decodeTrans_->InitAudioDecodeTransport(testLocalParaEnc, testRemoteParaEnc, ROLE_TEST));
169 EXPECT_EQ(ERR_DH_AUDIO_NULLPTR, decodeTrans_->Pause());
170 EXPECT_EQ(ERR_DH_AUDIO_TRANS_ERROR, decodeTrans_->Restart(testLocalParaEnc, testRemoteParaEnc));
171 }
172
173 /**
174 * @tc.name: decode_transport_test_004
175 * @tc.desc: Verify the FeedAudioData function.
176 * @tc.type: FUNC
177 * @tc.require: AR000H0E5U
178 */
179 HWTEST_F(DecodeTransportTest, decode_transport_test_004, TestSize.Level1)
180 {
181 std::shared_ptr<AudioData> audioData = nullptr;
182 AudioEvent event;
183 decodeTrans_->OnSessionOpened();
184 decodeTrans_->OnSessionClosed();
185 decodeTrans_->OnDataReceived(audioData);
186 decodeTrans_->OnEventReceived(event);
187 decodeTrans_->OnStateNotify(event);
188 decodeTrans_->OnAudioDataDone(audioData);
189 decodeTrans_->dataTransCallback_ = std::make_shared<MockAudioTransportCallback>();
190 decodeTrans_->OnSessionOpened();
191 decodeTrans_->OnSessionClosed();
192 decodeTrans_->OnAudioDataDone(audioData);
193
194 EXPECT_EQ(DH_SUCCESS, decodeTrans_->FeedAudioData(audioData));
195 }
196
197 /**
198 * @tc.name: decode_transport_test_005
199 * @tc.desc: Verify the RegisterChannelListener function.
200 * @tc.type: FUNC
201 * @tc.require: AR000H0E5U
202 */
203 HWTEST_F(DecodeTransportTest, decode_transport_test_005, TestSize.Level1)
204 {
205 IAVEngineProvider *providerPtr = nullptr;
206 uint32_t type = 0;
207 std::string content = "content";
208 std::string dstDevId = "dstDevId";
209 EXPECT_EQ(ERR_DH_AUDIO_TRANS_ERROR, decodeTrans_->RegisterChannelListener(ROLE_TEST));
210 EXPECT_EQ(DH_SUCCESS, decodeTrans_->CreateCtrl());
211 EXPECT_EQ(DH_SUCCESS, decodeTrans_->InitEngine(providerPtr));
212 EXPECT_EQ(DH_SUCCESS, decodeTrans_->SendMessage(type, content, dstDevId));
213 }
214 } // namespace DistributedHardware
215 } // namespace OHOS
216