1 /*
2 * Copyright (c) 2022-2024 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 "daudio_hdi_handler_test.h"
17
18 using namespace testing::ext;
19
20 namespace OHOS {
21 namespace DistributedHardware {
SetUpTestCase(void)22 void DAudioHdiHandlerTest::SetUpTestCase(void) {}
23
TearDownTestCase(void)24 void DAudioHdiHandlerTest::TearDownTestCase(void) {}
25
SetUp()26 void DAudioHdiHandlerTest::SetUp()
27 {
28 hdiHandler_ = std::make_shared<DAudioHdiHandler>();
29 }
30
TearDown()31 void DAudioHdiHandlerTest::TearDown()
32 {
33 hdiHandler_ = nullptr;
34 }
35
36 /**
37 * @tc.name: InitHdiHandler_001
38 * @tc.desc: Verify the InitHdiHandler function.
39 * @tc.type: FUNC
40 * @tc.require: AR000H0E6H
41 */
42 HWTEST_F(DAudioHdiHandlerTest, InitHdiHandler_001, TestSize.Level1)
43 {
44 if (hdiHandler_ == nullptr) {
45 return;
46 }
47 EXPECT_EQ(HDF_SUCCESS, hdiHandler_->InitHdiHandler());
48 EXPECT_EQ(HDF_SUCCESS, hdiHandler_->InitHdiHandler()); // test repeated initialization
49 }
50
51 /**
52 * @tc.name: RegisterAudioDevice_001
53 * @tc.desc: Verify the RegisterAudioDevice function.
54 * @tc.type: FUNC
55 * @tc.require: AR000H0E6H
56 */
57 HWTEST_F(DAudioHdiHandlerTest, RegisterAudioDevice_001, TestSize.Level1)
58 {
59 if (hdiHandler_ == nullptr) {
60 return;
61 }
62 EXPECT_EQ(HDF_SUCCESS, hdiHandler_->InitHdiHandler());
63 hdiHandler_->audioSrvHdf_ = nullptr;
64 std::shared_ptr<IDAudioHdiCallback> callbackObjParam = std::make_shared<MockIDAudioHdiCallback>();
65 EXPECT_NE(HDF_SUCCESS, hdiHandler_->RegisterAudioDevice(devId_,
66 PIN_OUT_DAUDIO_DEFAULT, capability_, callbackObjParam));
67 EXPECT_NE(HDF_SUCCESS, hdiHandler_->RegisterAudioDevice(devId_,
68 PIN_IN_DAUDIO_DEFAULT, capability_, callbackObjParam));
69 EXPECT_NE(HDF_SUCCESS, hdiHandler_->RegisterAudioDevice(devId_, -1, capability_, callbackObjParam));
70 EXPECT_NE(HDF_SUCCESS, hdiHandler_->UnRegisterAudioDevice(devId_, PIN_OUT_DAUDIO_DEFAULT));
71 EXPECT_NE(HDF_SUCCESS, hdiHandler_->UnRegisterAudioDevice(devId_, PIN_IN_DAUDIO_DEFAULT));
72 EXPECT_NE(HDF_SUCCESS, hdiHandler_->UnRegisterAudioDevice("errorId", PIN_IN_DAUDIO_DEFAULT));
73 }
74
75 /**
76 * @tc.name: RegisterAudioDevice_002
77 * @tc.desc: Verify the RegisterAudioDevice function.
78 * @tc.type: FUNC
79 * @tc.require: AR000H0E6H
80 */
81 HWTEST_F(DAudioHdiHandlerTest, RegisterAudioDevice_002, TestSize.Level1)
82 {
83 if (hdiHandler_ == nullptr) {
84 return;
85 }
86 EXPECT_EQ(HDF_SUCCESS, hdiHandler_->InitHdiHandler());
87 std::shared_ptr<IDAudioHdiCallback> callbackObjParam = std::make_shared<MockIDAudioHdiCallback>();
88 EXPECT_EQ(ERR_DH_AUDIO_HDI_CALL_FAILED,
89 hdiHandler_->RegisterAudioDevice(devId_, dhId_, capability_, callbackObjParam));
90 EXPECT_EQ(ERR_DH_AUDIO_HDI_CALL_FAILED, hdiHandler_->UnRegisterAudioDevice(devId_, dhId_));
91 }
92
93 /**
94 * @tc.name: NotifyEvent_001
95 * @tc.desc: Verify the NotifyEvent function.
96 * @tc.type: FUNC
97 * @tc.require: AR000H0E6H
98 */
99 HWTEST_F(DAudioHdiHandlerTest, NotifyEvent_001, TestSize.Level1)
100 {
101 if (hdiHandler_ == nullptr) {
102 return;
103 }
104 EXPECT_EQ(HDF_SUCCESS, hdiHandler_->InitHdiHandler());
105 hdiHandler_->audioSrvHdf_ = nullptr;
106 AudioEvent audioEvent;
107 EXPECT_NE(HDF_SUCCESS, hdiHandler_->NotifyEvent(devId_, dhId_, streamId_, audioEvent));
108 }
109
110 /**
111 * @tc.name: NotifyEvent_002
112 * @tc.desc: Verify the NotifyEvent function.
113 * @tc.type: FUNC
114 * @tc.require: AR000H0E6H
115 */
116 HWTEST_F(DAudioHdiHandlerTest, NotifyEvent_002, TestSize.Level1)
117 {
118 if (hdiHandler_ == nullptr) {
119 return;
120 }
121 EXPECT_EQ(HDF_SUCCESS, hdiHandler_->InitHdiHandler());
122 hdiHandler_->audioSrvHdf_ = new MockIDAudioManager();
123 AudioEvent audioEvent1(AudioEventType::NOTIFY_OPEN_SPEAKER_RESULT, "");
124 EXPECT_EQ(HDF_SUCCESS, hdiHandler_->NotifyEvent(devId_, dhId_, streamId_, audioEvent1));
125 AudioEvent audioEvent2(AudioEventType::NOTIFY_CLOSE_SPEAKER_RESULT, "");
126 EXPECT_EQ(HDF_SUCCESS, hdiHandler_->NotifyEvent(devId_, dhId_, streamId_, audioEvent2));
127 AudioEvent audioEvent3(AudioEventType::NOTIFY_OPEN_MIC_RESULT, "");
128 EXPECT_EQ(HDF_SUCCESS, hdiHandler_->NotifyEvent(devId_, dhId_, streamId_, audioEvent3));
129 AudioEvent audioEvent4(AudioEventType::NOTIFY_CLOSE_MIC_RESULT, "");
130 EXPECT_EQ(HDF_SUCCESS, hdiHandler_->NotifyEvent(devId_, dhId_, streamId_, audioEvent4));
131 AudioEvent audioEvent5(AudioEventType::VOLUME_CHANGE, "");
132 EXPECT_EQ(HDF_SUCCESS, hdiHandler_->NotifyEvent(devId_, dhId_, streamId_, audioEvent5));
133 AudioEvent audioEvent6(AudioEventType::SPEAKER_CLOSED, "");
134 EXPECT_EQ(HDF_SUCCESS, hdiHandler_->NotifyEvent(devId_, dhId_, streamId_, audioEvent6));
135 AudioEvent audioEvent7(AudioEventType::MIC_CLOSED, "");
136 EXPECT_EQ(HDF_SUCCESS, hdiHandler_->NotifyEvent(devId_, dhId_, streamId_, audioEvent7));
137 AudioEvent audioEvent8(AudioEventType::AUDIO_FOCUS_CHANGE, "");
138 EXPECT_EQ(HDF_SUCCESS, hdiHandler_->NotifyEvent(devId_, dhId_, streamId_, audioEvent8));
139 AudioEvent audioEvent9(AudioEventType::AUDIO_RENDER_STATE_CHANGE, "");
140 EXPECT_EQ(HDF_SUCCESS, hdiHandler_->NotifyEvent(devId_, dhId_, streamId_, audioEvent9));
141 AudioEvent audioEvent(-1, "");
142 EXPECT_EQ(HDF_SUCCESS, hdiHandler_->NotifyEvent(devId_, dhId_, streamId_, audioEvent));
143 }
144
145 /**
146 * @tc.name: UnRegisterAudioDevice_001
147 * @tc.desc: Verify the UnRegisterAudioDevice function.
148 * @tc.type: FUNC
149 * @tc.require: AR000H0E6H
150 */
151 HWTEST_F(DAudioHdiHandlerTest, UnRegisterAudioDevice_001, TestSize.Level1)
152 {
153 if (hdiHandler_ == nullptr) {
154 return;
155 }
156 hdiHandler_->audioSrvHdf_ = nullptr;
157 EXPECT_NE(HDF_SUCCESS, hdiHandler_->UnRegisterAudioDevice(devId_, dhId_));
158 }
159
160 /**
161 * @tc.name: UnInitHdiHandler_001
162 * @tc.desc: Verify the UnInitHdiHandler function.
163 * @tc.type: FUNC
164 * @tc.require: AR000H0E6H
165 */
166 HWTEST_F(DAudioHdiHandlerTest, UnInitHdiHandler_001, TestSize.Level1)
167 {
168 if (hdiHandler_ == nullptr) {
169 return;
170 }
171 hdiHandler_->audioSrvHdf_ = nullptr;
172 EXPECT_EQ(ERR_DH_AUDIO_NULLPTR, hdiHandler_->UninitHdiHandler());
173 }
174 } // DistributedHardware
175 } // OHOS
176