1 /*
2 * Copyright (c) 2022 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 "dspeaker_client_test.h"
17
18 #include <thread>
19 #include <chrono>
20
21 #include "av_trans_types.h"
22
23 using namespace testing::ext;
24
25 namespace OHOS {
26 namespace DistributedHardware {
SetUpTestCase(void)27 void DSpeakerClientTest::SetUpTestCase(void) {}
28
TearDownTestCase(void)29 void DSpeakerClientTest::TearDownTestCase(void) {}
30
SetUp()31 void DSpeakerClientTest::SetUp()
32 {
33 std::string devId = "hello";
34 clientCallback_ = std::make_shared<MockIAudioEventCallback>();
35 speakerClient_ = std::make_shared<DSpeakerClient>(devId, clientCallback_);
36 speakerClient_->speakerTrans_ = std::make_shared<MockIAudioDataTransport>();
37
38 audioParam_.comParam.codecType = AudioCodecType::AUDIO_CODEC_AAC;
39 audioParam_.comParam.sampleRate = AudioSampleRate::SAMPLE_RATE_48000;
40 audioParam_.comParam.bitFormat = AudioSampleFormat::SAMPLE_S16LE;
41 audioParam_.comParam.channelMask = AudioChannel::STEREO;
42 audioParam_.renderOpts.contentType = ContentType::CONTENT_TYPE_MUSIC;
43 audioParam_.renderOpts.streamUsage = StreamUsage::STREAM_USAGE_MEDIA;
44 }
45
TearDown()46 void DSpeakerClientTest::TearDown()
47 {
48 speakerClient_ = nullptr;
49 clientCallback_ = nullptr;
50 }
51
52 /**
53 * @tc.name: InitReceiverEngine_001
54 * @tc.desc: Verify the InitReceiverEngine function.
55 * @tc.type: FUNC
56 * @tc.require: AR000H0E6G
57 */
58 HWTEST_F(DSpeakerClientTest, InitReceiverEngine_001, TestSize.Level1)
59 {
60 IAVEngineProvider *providerPtr = nullptr;
61
62 AVTransEvent event1 = { EventType::EVENT_START_SUCCESS, "", ""};
63 speakerClient_->OnEngineTransEvent(event1);
64 AVTransEvent event2 = { EventType::EVENT_STOP_SUCCESS, "", ""};
65 speakerClient_->OnEngineTransEvent(event2);
66 auto message = std::make_shared<AVTransMessage>();
67 speakerClient_->OnEngineTransMessage(message);
68 auto data = std::make_shared<AudioData>(4096);
69 speakerClient_->OnEngineTransDataAvailable(data);
70 EXPECT_EQ(DH_SUCCESS, speakerClient_->InitReceiverEngine(providerPtr));
71 }
72
73 /**
74 * @tc.name: OnStateChange_001
75 * @tc.desc: Verify the OnStateChange function.
76 * @tc.type: FUNC
77 * @tc.require: AR000H0E6G
78 */
79 HWTEST_F(DSpeakerClientTest, OnStateChange_001, TestSize.Level1)
80 {
81 AudioStandard::VolumeEvent event;
82 event.volume = 1;
83 event.updateUi = 1;
84 event.volumeGroupId = 1;
85
86 speakerClient_->OnVolumeKeyEvent(event);
87 EXPECT_EQ(DH_SUCCESS, speakerClient_->OnStateChange(AudioEventType::DATA_OPENED));
88 EXPECT_EQ(DH_SUCCESS, speakerClient_->OnStateChange(AudioEventType::DATA_CLOSED));
89 EXPECT_NE(DH_SUCCESS, speakerClient_->OnStateChange(AudioEventType::SPEAKER_OPENED));
90 EXPECT_EQ(ERR_DH_AUDIO_CLIENT_STATE_IS_INVALID, speakerClient_->OnStateChange(AudioEventType::EVENT_UNKNOWN));
91 }
92
93 /**
94 * @tc.name: SetUp_001
95 * @tc.desc: Verify the SetUp function.
96 * @tc.type: FUNC
97 * @tc.require: AR000H0E6G
98 */
99 HWTEST_F(DSpeakerClientTest, SetUp_001, TestSize.Level1)
100 {
101 AudioParam audioParam;
102 EXPECT_EQ(DH_SUCCESS, speakerClient_->SetUp(audioParam));
103 EXPECT_EQ(DH_SUCCESS, speakerClient_->SetUp(audioParam_));
104 speakerClient_->speakerTrans_ = nullptr;
105 EXPECT_EQ(ERR_DH_AUDIO_NULLPTR, speakerClient_->SetUp(audioParam));
106 EXPECT_EQ(DH_SUCCESS, speakerClient_->Release());
107 speakerClient_->clientStatus_ = AudioStatus::STATUS_READY;
108 EXPECT_EQ(ERR_DH_AUDIO_CLIENT_RENDER_RELEASE_FAILED, speakerClient_->Release());
109 }
110
111 /**
112 * @tc.name: StartRender_001
113 * @tc.desc: Verify the StartRender and StopRender function.
114 * @tc.type: FUNC
115 * @tc.require: AR000H0E6G
116 */
117 HWTEST_F(DSpeakerClientTest, StartRender001, TestSize.Level1)
118 {
119 EXPECT_NE(DH_SUCCESS, speakerClient_->StartRender());
120 EXPECT_NE(DH_SUCCESS, speakerClient_->StopRender());
121
122 speakerClient_->clientStatus_ = STATUS_START;
123 EXPECT_EQ(ERR_DH_AUDIO_SA_STATUS_ERR, speakerClient_->StartRender());
124 EXPECT_EQ(ERR_DH_AUDIO_SA_STATUS_ERR, speakerClient_->StopRender());
125 speakerClient_->isRenderReady_.store(true);
126 EXPECT_EQ(ERR_DH_AUDIO_CLIENT_RENDER_OR_TRANS_IS_NULL, speakerClient_->StopRender());
127 speakerClient_->CreateAudioRenderer(audioParam_);
128 EXPECT_EQ(ERR_DH_AUDIO_CLIENT_RENDER_STOP_FAILED, speakerClient_->StopRender());
129 }
130
131 /**
132 * @tc.name: StopRender_001
133 * @tc.desc: Verify the StopRender function.
134 * @tc.type: FUNC
135 * @tc.require: AR000H0E6G
136 */
137 HWTEST_F(DSpeakerClientTest, StopRender001, TestSize.Level1)
138 {
139 EXPECT_NE(DH_SUCCESS, speakerClient_->StopRender());
140 std::string args = "args";
141 AudioEvent event;
142 speakerClient_->PlayStatusChange(args);
143 speakerClient_->SetAudioParameters(event);
144 speakerClient_->SetMute(event);
145 for (size_t i = 0; i < 10; i++) {
146 std::shared_ptr<AudioData> data = std::make_shared<AudioData>(4096);
147 speakerClient_->dataQueue_.push(data);
148 }
149 args = "restart";
150 speakerClient_->PlayStatusChange(args);
151
152 if (speakerClient_->renderDataThread_.joinable()) {
153 speakerClient_->isRenderReady_.store(false);
154 speakerClient_->renderDataThread_.join();
155 }
156 event.content = "AUDIO_VOLUME_TYPE=2;";
157 auto ret = speakerClient_->SetAudioParameters(event);
158 EXPECT_NE(DH_SUCCESS, ret);
159 }
160
161 /**
162 * @tc.name: OnDecodeTransDataDone_001
163 * @tc.desc: Verify the OnDecodeTransDataDone function.
164 * @tc.type: FUNC
165 * @tc.require: AR000H0E6G
166 */
167 HWTEST_F(DSpeakerClientTest, OnDecodeTransDataDone001, TestSize.Level1)
168 {
169 std::shared_ptr<AudioData> audioData = nullptr;
170 EXPECT_EQ(ERR_DH_AUDIO_CLIENT_PARAM_IS_NULL, speakerClient_->OnDecodeTransDataDone(audioData));
171 for (size_t i = 0; i < 11; i++) {
172 std::shared_ptr<AudioData> data = std::make_shared<AudioData>(4096);
173 speakerClient_->dataQueue_.push(data);
174 }
175 audioData = std::make_shared<AudioData>(4096);
176 EXPECT_EQ(DH_SUCCESS, speakerClient_->OnDecodeTransDataDone(audioData));
177 }
178
179 /**
180 * @tc.name: Release_001
181 * @tc.desc: Verify the Release function.
182 * @tc.type: FUNC
183 * @tc.require: AR000H0E6G
184 */
185 HWTEST_F(DSpeakerClientTest, Release001, TestSize.Level1)
186 {
187 speakerClient_->Pause();
188 EXPECT_EQ(ERR_DH_AUDIO_SA_STATUS_ERR, speakerClient_->Release());
189 }
190
191 /**
192 * @tc.name: GetVolumeLevel_001
193 * @tc.desc: Verify the GetVolumeLevel function.
194 * @tc.type: FUNC
195 * @tc.require: AR000H0E6G
196 */
197 HWTEST_F(DSpeakerClientTest, GetVolumeLevel_001, TestSize.Level1)
198 {
199 AudioStandard::InterruptEvent eventType = {static_cast<AudioStandard::InterruptType>(1),
200 static_cast<AudioStandard::InterruptForceType>(0), static_cast<AudioStandard::InterruptHint>(0)};
201 speakerClient_->OnInterrupt(eventType);
202
203 std::string volEvent = speakerClient_->GetVolumeLevel();
204 EXPECT_NE("", volEvent);
205 }
206
207 /**
208 * @tc.name: SendMessage_001
209 * @tc.desc: Verify the SendMessage function.
210 * @tc.type: FUNC
211 * @tc.require: AR000H0E6G
212 */
213 HWTEST_F(DSpeakerClientTest, SendMessage_001, TestSize.Level1)
214 {
215 std::string content = "content";
216 std::string dstDevId = "dstDevId";
217 EXPECT_EQ(ERR_DH_AUDIO_NULLPTR, speakerClient_->SendMessage(EVENT_UNKNOWN, content, dstDevId));
218 EXPECT_EQ(DH_SUCCESS, speakerClient_->SendMessage(NOTIFY_OPEN_SPEAKER_RESULT, content, dstDevId));
219 speakerClient_->speakerTrans_ = nullptr;
220 EXPECT_EQ(ERR_DH_AUDIO_NULLPTR, speakerClient_->SendMessage(NOTIFY_OPEN_SPEAKER_RESULT, content, dstDevId));
221 }
222 } // DistributedHardware
223 } // OHOS
224